977384042730d11c6271abbe480d9ca733f25340
[supertux.git] / src / unison / physfs-1.1.1 / lzma / 7zCrc.c
1 /* 7zCrc.c */
2
3 #include "7zCrc.h"
4
5 #define kCrcPoly 0xEDB88320
6
7 UInt32 g_CrcTable[256];
8
9 void InitCrcTable()
10 {
11   UInt32 i;
12   for (i = 0; i < 256; i++)
13   {
14     UInt32 r = i;
15     int j;
16     for (j = 0; j < 8; j++)
17       if (r & 1) 
18         r = (r >> 1) ^ kCrcPoly;
19       else     
20         r >>= 1;
21     g_CrcTable[i] = r;
22   }
23 }
24
25 void CrcInit(UInt32 *crc) { *crc = 0xFFFFFFFF; }
26 UInt32 CrcGetDigest(UInt32 *crc) { return *crc ^ 0xFFFFFFFF; } 
27
28 void CrcUpdateByte(UInt32 *crc, Byte b)
29 {
30   *crc = g_CrcTable[((Byte)(*crc)) ^ b] ^ (*crc >> 8);
31 }
32
33 void CrcUpdateUInt16(UInt32 *crc, UInt16 v)
34 {
35   CrcUpdateByte(crc, (Byte)v);
36   CrcUpdateByte(crc, (Byte)(v >> 8));
37 }
38
39 void CrcUpdateUInt32(UInt32 *crc, UInt32 v)
40 {
41   int i;
42   for (i = 0; i < 4; i++)
43     CrcUpdateByte(crc, (Byte)(v >> (8 * i)));
44 }
45
46 void CrcUpdateUInt64(UInt32 *crc, UInt64 v)
47 {
48   int i;
49   for (i = 0; i < 8; i++)
50   {
51     CrcUpdateByte(crc, (Byte)(v));
52     v >>= 8;
53   }
54 }
55
56 void CrcUpdate(UInt32 *crc, const void *data, size_t size)
57 {
58   UInt32 v = *crc;
59   const Byte *p = (const Byte *)data;
60   for (; size > 0 ; size--, p++)
61     v = g_CrcTable[((Byte)(v)) ^ *p] ^ (v >> 8);
62   *crc = v;
63 }
64
65 UInt32 CrcCalculateDigest(const void *data, size_t size)
66 {
67   UInt32 crc;
68   CrcInit(&crc);
69   CrcUpdate(&crc, data, size);
70   return CrcGetDigest(&crc);
71 }
72
73 int CrcVerifyDigest(UInt32 digest, const void *data, size_t size)
74 {
75   return (CrcCalculateDigest(data, size) == digest);
76 }