forked from sabrogden/Ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crc32Dynamic.cpp
78 lines (64 loc) · 1.32 KB
/
Crc32Dynamic.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
#include "stdafx.h"
#include "Crc32Dynamic.h"
//#include <fstream.h>
CCrc32Dynamic::CCrc32Dynamic() : m_pdwCrc32Table(NULL)
{
Init();
}
CCrc32Dynamic::~CCrc32Dynamic()
{
Free();
}
void CCrc32Dynamic::Init()
{
// This is the official polynomial used by CRC32 in PKZip.
// Often times the polynomial shown reversed as 0x04C11DB7.
DWORD dwPolynomial = 0xEDB88320;
int i, j;
Free();
m_pdwCrc32Table = new DWORD[256];
DWORD dwCrc;
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;
}
}
void CCrc32Dynamic::Free()
{
delete m_pdwCrc32Table;
m_pdwCrc32Table = NULL;
}
inline void CCrc32Dynamic::CalcCrc32(const BYTE byte, DWORD &dwCrc32) const
{
dwCrc32 = ((dwCrc32) >> 8) ^ m_pdwCrc32Table[(byte) ^ ((dwCrc32) & 0x000000FF)];
}
DWORD CCrc32Dynamic::GenerateCrc32(const LPBYTE lpbArray, DWORD dSize, DWORD &dwCrc32)
{
DWORD dwErrorCode = NO_ERROR;
// dwCrc32 = 0xFFFFFFFF;
try
{
// Is the table initialized?
if(m_pdwCrc32Table == NULL)
throw 0;
for(DWORD i = 0; i < dSize; i++)
{
CalcCrc32(lpbArray[i], dwCrc32);
}
}
catch(...)
{
// An unknown exception happened, or the table isn't initialized
dwErrorCode = ERROR_CRC;
}
// dwCrc32 = ~dwCrc32;
return dwErrorCode;
}