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