-
Notifications
You must be signed in to change notification settings - Fork 17
/
Crclib.cpp
71 lines (60 loc) · 1.29 KB
/
Crclib.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
//
// CRCチェック用ライブラリクラス
//
#include "Crclib.h"
BOOL CRC::m_Init = FALSE;
BOOL CRC::m_InitRev = FALSE;
DWORD CRC::m_CrcTable[ UCHAR_MAX+1 ];
DWORD CRC::m_CrcTableRev[ UCHAR_MAX+1 ];
#define CRCPOLY1 0x04C11DB7UL
#define CRCPOLY2 0xEDB88320UL
DWORD CRC::Crc( INT size, LPBYTE c )
{
if( !m_Init ) {
MakeTable();
m_Init = TRUE;
}
DWORD r = 0xFFFFFFFFUL;
while( --size >= 0 ) {
r = (r << CHAR_BIT) ^ m_CrcTable[(BYTE)(r >> (32 - CHAR_BIT)) ^ *c++];
}
return ~r & 0xFFFFFFFFUL;
}
DWORD CRC::CrcRev( INT size, LPBYTE c )
{
if( !m_InitRev ) {
MakeTableRev();
m_InitRev = TRUE;
}
DWORD r = 0xFFFFFFFFUL;
while( --size >= 0 ) {
r = (r >> CHAR_BIT) ^ m_CrcTableRev[(BYTE)r ^ *c++];
}
return r ^ 0xFFFFFFFFUL;
}
void CRC::MakeTable()
{
INT i, j;
DWORD r;
for( i = 0; i <= UCHAR_MAX; i++ ) {
r = (DWORD)i << (32 - CHAR_BIT);
for( j = 0; j < CHAR_BIT; j++ ) {
if( r & 0x80000000UL ) r = (r << 1) ^ CRCPOLY1;
else r <<= 1;
}
m_CrcTable[i] = r & 0xFFFFFFFFUL;
}
}
void CRC::MakeTableRev()
{
INT i, j;
DWORD r;
for( i = 0; i <= UCHAR_MAX; i++ ) {
r = i;
for( j = 0; j < CHAR_BIT; j++ ) {
if( r & 1 ) r = (r >> 1) ^ CRCPOLY2;
else r >>= 1;
}
m_CrcTableRev[i] = r;
}
}