This repository has been archived by the owner on Dec 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
crc32.cpp
91 lines (72 loc) · 1.54 KB
/
crc32.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "crc32.h"
DWORD CalcFileCrc32(LPSTR lpFileName, LPBOOL pbSuccess)
{
HANDLE hFile;
DWORD m_pdwCrc32Table[256];
DWORD dwCrc32 = 0xFFFFFFFF;
DWORD dwByteCount;
BYTE bBuffer[1024];
LPBYTE pByte;
// Prepare for the worst
if(pbSuccess)
*pbSuccess = FALSE;
hFile = CreateFile(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE)
return NULL;
MakeCrc32Table(m_pdwCrc32Table);
if(!ReadFile(hFile, bBuffer, 1024, &dwByteCount, NULL))
{
CloseHandle(hFile);
return NULL;
}
while(dwByteCount)
{
pByte = bBuffer;
while(dwByteCount-- > 0)
{
dwCrc32 = ((dwCrc32) >> 8) ^ m_pdwCrc32Table[(*pByte) ^ ((dwCrc32) & 0x000000FF)];
pByte++;
}
if(!ReadFile(hFile, bBuffer, 1024, &dwByteCount, NULL))
{
CloseHandle(hFile);
return NULL;
}
}
CloseHandle(hFile);
dwCrc32 = ~dwCrc32;
if(pbSuccess)
*pbSuccess = TRUE;
return dwCrc32;
}
DWORD CalcMemoryCrc32(LPBYTE pByte, DWORD dwByteCount)
{
DWORD m_pdwCrc32Table[256];
DWORD dwCrc32 = 0xFFFFFFFF;
MakeCrc32Table(m_pdwCrc32Table);
while(dwByteCount-- > 0)
{
dwCrc32 = ((dwCrc32) >> 8) ^ m_pdwCrc32Table[(*pByte) ^ ((dwCrc32) & 0x000000FF)];
pByte++;
}
dwCrc32 = ~dwCrc32;
return dwCrc32;
}
void MakeCrc32Table(DWORD m_pdwCrc32Table[])
{
DWORD dwPolynomial = 0xEDB88320;
DWORD dwCrc;
int i, j;
for(i=0; i<256; i++)
{
dwCrc = i;
for(j=8; j>0; j--)
{
if(dwCrc & 1)
dwCrc = (dwCrc >> 1) ^ dwPolynomial;
else
dwCrc >>= 1;
}
m_pdwCrc32Table[i] = dwCrc;
}
}