5 #define kCrcPoly 0xEDB88320
7 UInt32 g_CrcTable[256];
12 for (i = 0; i < 256; i++)
16 for (j = 0; j < 8; j++)
18 r = (r >> 1) ^ kCrcPoly;
25 void CrcInit(UInt32 *crc) { *crc = 0xFFFFFFFF; }
26 UInt32 CrcGetDigest(UInt32 *crc) { return *crc ^ 0xFFFFFFFF; }
28 void CrcUpdateByte(UInt32 *crc, Byte b)
30 *crc = g_CrcTable[((Byte)(*crc)) ^ b] ^ (*crc >> 8);
33 void CrcUpdateUInt16(UInt32 *crc, UInt16 v)
35 CrcUpdateByte(crc, (Byte)v);
36 CrcUpdateByte(crc, (Byte)(v >> 8));
39 void CrcUpdateUInt32(UInt32 *crc, UInt32 v)
42 for (i = 0; i < 4; i++)
43 CrcUpdateByte(crc, (Byte)(v >> (8 * i)));
46 void CrcUpdateUInt64(UInt32 *crc, UInt64 v)
49 for (i = 0; i < 8; i++)
51 CrcUpdateByte(crc, (Byte)(v));
56 void CrcUpdate(UInt32 *crc, const void *data, size_t size)
59 const Byte *p = (const Byte *)data;
60 for (; size > 0 ; size--, p++)
61 v = g_CrcTable[((Byte)(v)) ^ *p] ^ (v >> 8);
65 UInt32 CrcCalculateDigest(const void *data, size_t size)
69 CrcUpdate(&crc, data, size);
70 return CrcGetDigest(&crc);
73 int CrcVerifyDigest(UInt32 digest, const void *data, size_t size)
75 return (CrcCalculateDigest(data, size) == digest);