RarCRC.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. namespace SharpCompress.Compressors.Rar
  3. {
  4. internal static class RarCRC
  5. {
  6. private static readonly uint[] crcTab;
  7. public static uint CheckCrc(uint startCrc, byte b) {
  8. return (crcTab[((int) ((int) startCrc ^ (int) b)) & 0xff] ^ (startCrc >> 8));
  9. }
  10. public static uint CheckCrc(uint startCrc, byte[] data, int offset, int count)
  11. {
  12. int size = Math.Min(data.Length - offset, count);
  13. for (int i = 0; i < size; i++)
  14. {
  15. startCrc = (crcTab[((int)startCrc ^ data[offset + i]) & 0xff] ^ (startCrc >> 8));
  16. }
  17. return (startCrc);
  18. }
  19. static RarCRC()
  20. {
  21. {
  22. crcTab = new uint[256];
  23. for (uint i = 0; i < 256; i++)
  24. {
  25. uint c = i;
  26. for (int j = 0; j < 8; j++)
  27. {
  28. if ((c & 1) != 0)
  29. {
  30. c = c >> 1;
  31. c ^= 0xEDB88320;
  32. }
  33. else
  34. {
  35. c = c >> 1;
  36. }
  37. }
  38. crcTab[i] = c;
  39. }
  40. }
  41. }
  42. }
  43. }