-
Notifications
You must be signed in to change notification settings - Fork 1
/
CRC.cpp
63 lines (49 loc) · 1.14 KB
/
CRC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <windows.h>
#define CRC32_POLYNOMIAL 0x04C11DB7
unsigned long CRCTable[256];
DWORD Reflect(DWORD ref, char ch)
{
DWORD value = 0;
// Swap bit 0 for bit 7
// bit 1 for bit 6, etc.
for (int i = 1; i < (ch + 1); i++)
{
if (ref & 1)
value |= 1 << (ch - i);
ref >>= 1;
}
return value;
}
void CRC_BuildTable()
{
DWORD crc;
for (int i = 0; i <= 255; i++)
{
crc = Reflect(i, 8) << 24;
for (int j = 0; j < 8; j++)
crc = (crc << 1) ^ (crc & (1 << 31) ? CRC32_POLYNOMIAL : 0);
CRCTable[i] = Reflect(crc, 32);
}
}
DWORD CRC_Calculate(DWORD crc, void* buffer, DWORD count)
{
BYTE* p;
DWORD orig = crc;
p = (BYTE*)buffer;
while (count--)
crc = (crc >> 8) ^ CRCTable[(crc & 0xFF) ^ *p++];
return crc ^ orig;
}
DWORD CRC_CalculatePalette(DWORD crc, void* buffer, DWORD count)
{
BYTE* p;
DWORD orig = crc;
p = (BYTE*)buffer;
while (count--)
{
crc = (crc >> 8) ^ CRCTable[(crc & 0xFF) ^ *p++];
crc = (crc >> 8) ^ CRCTable[(crc & 0xFF) ^ *p++];
p += 6;
}
return crc ^ orig;
}