-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc.cpp
47 lines (36 loc) · 850 Bytes
/
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
#include "stdafx.h"
using namespace std;
bool crc32(char *message, std::string crctocheck)
{
int i, j;
unsigned int byte, crc;
// crc hex to rcr decimal
unsigned int crc2 = 0;
unsigned int digit = 1;
for (int f = crctocheck.length()-1; f >= 0; f--)
{
if (crctocheck[f]<='9')
{
crc2 += (crctocheck[f] - '0') * digit;
}
else
{
crc2 += (crctocheck[f]- 'a' + 10) * digit;
}
digit *= 16;
}
// get buffer crc
i = 0;
crc = 0xFFFFFFFF;
while (message[i] != 0)
{
byte = (unsigned char)message[i]; // Get next byte.
crc = crc ^ byte;
for(j = 7; j >= 0; j--)
{
crc = (crc >> 1) ^ (crc & 1 ? 0xEDB88320 : 0);
}
i = i + 1;
}
return (~crc == crc2);
}