-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc32.c
42 lines (34 loc) · 920 Bytes
/
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
static uint32_t crc_table[256];
void init_crc_table(void)
{
uint32_t c;
uint32_t i, j;
for (i = 0; i < 256; i++) {
c = i;
for (j = 0; j < 8; j++) {
if (c & 1)
c = 0xedb88320L ^ (c >> 1);
else
c = c >> 1;
}
crc_table[i] = c;
}
}
uint32_t crc32(unsigned int crc,unsigned char *buffer, unsigned int size)
{
unsigned int i;
for (i = 0; i < size; i++) {
crc = crc_table[(crc ^ buffer[i]) & 0xff] ^ (crc >> 8);
}
return crc ;
}
uint32_t cal_crc(void)
{
int i;
uint32_t crc = 0xffffffff;
init_crc_table();
/* user add, you shou give you calculate buffer and size, this call can repeat for slicing big file.
crc = crc32(crc, buffer, size);
*/
return (crc^0xffffffff);
}