CRC.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Common/CRC.cs
  2. namespace SevenZip
  3. {
  4. class CRC
  5. {
  6. public static readonly uint[] Table;
  7. static CRC()
  8. {
  9. Table = new uint[256];
  10. const uint kPoly = 0xEDB88320;
  11. for (uint i = 0; i < 256; i++)
  12. {
  13. uint r = i;
  14. for (int j = 0; j < 8; j++)
  15. if ((r & 1) != 0)
  16. r = (r >> 1) ^ kPoly;
  17. else
  18. r >>= 1;
  19. Table[i] = r;
  20. }
  21. }
  22. uint _value = 0xFFFFFFFF;
  23. public void Init() { _value = 0xFFFFFFFF; }
  24. public void UpdateByte(byte b)
  25. {
  26. _value = Table[(((byte)(_value)) ^ b)] ^ (_value >> 8);
  27. }
  28. public void Update(byte[] data, uint offset, uint size)
  29. {
  30. for (uint i = 0; i < size; i++)
  31. _value = Table[(((byte)(_value)) ^ data[offset + i])] ^ (_value >> 8);
  32. }
  33. public uint GetDigest() { return _value ^ 0xFFFFFFFF; }
  34. static uint CalculateDigest(byte[] data, uint offset, uint size)
  35. {
  36. CRC crc = new CRC();
  37. // crc.Init();
  38. crc.Update(data, offset, size);
  39. return crc.GetDigest();
  40. }
  41. static bool VerifyDigest(uint digest, byte[] data, uint offset, uint size)
  42. {
  43. return (CalculateDigest(data, offset, size) == digest);
  44. }
  45. }
  46. }