-
Notifications
You must be signed in to change notification settings - Fork 4
/
crc32.c
73 lines (56 loc) · 1.22 KB
/
crc32.c
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
/*C
(c) 2005 bl0rg.net
**/
#include "conf.h"
#include "crc32.h"
void crc32_init(crc32_t *crc, unsigned long poly,
unsigned long init, unsigned long xor) {
crc->poly = poly;
crc->init = init;
crc->xor = xor;
int i;
for (i = 0; i < 256; i++) {
unsigned long r = (unsigned long)i;
r <<= 24;
int j;
for (j = 0; j < 8; j++) {
unsigned long bit = r & (1 << 31);
r <<= 1;
if (bit)
r ^= crc->poly;
}
r &= 0xFFFFFFFF;
crc->table[i] = r;
}
}
unsigned long crc32(crc32_t *crc,
unsigned char *data, unsigned long len) {
unsigned long r = crc->init;
unsigned char *ptr = data;
while (len--)
r = (r << 8) ^ crc->table[(r >> 24) ^ *ptr++];
r ^= crc->xor;
r &= 0xFFFFFFFF;
return r;
}
#ifdef CRC32_TEST
#include <stdio.h>
#include <string.h>
static void usage(void) {
fprintf(stderr, "Usage: ./crc32test polynom messagestring\n");
}
int main(int argc, char *argv[]) {
if (argc != 3) {
usage();
return 1;
}
unsigned long poly;
sscanf(argv[1], "%lx", &poly);
crc32_t crc;
crc32_init(&crc, poly, 0, 0);
printf("crc: %lx\n", crc32(&crc, argv[2], strlen(argv[2])));
return 0;
}
#endif /* CRC32_TEST */
/*C
**/