-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrc32.c
47 lines (40 loc) · 1022 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
43
44
45
46
47
/*
* crc_le(crc, buf, len, poly) -> crc:32,bitrev(poly),crc,true,true,0
* c.f Rocksoft Param. Model WIDTH,POLY,INIT,REFIN,REFOUT,XOROUT
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <stdlib.h>
#include <stdint.h>
#ifdef CFG_LUBI_INT_CRC32_TBL
static uint32_t crc32_le_tbl[256];
static int crc32_le_tbl_filled = 0;
static uint32_t crc32_le_init(uint32_t poly)
{
for (int i = 0; i < 256; i++) {
uint32_t crc = i;
for (int j = 0; j < 8; j++)
crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
crc32_le_tbl[i] = crc;
}
crc32_le_tbl_filled = 1;
return 0;
}
#endif
uint32_t crc32_le(uint32_t crc, const uint8_t *p, size_t len, uint32_t poly)
{
#ifdef CFG_LUBI_INT_CRC32_TBL
if (__builtin_expect(!crc32_le_tbl_filled, 0))
crc32_le_init(poly);
#endif
for (unsigned int i = 0; i < len; i++) {
#ifdef CFG_LUBI_INT_CRC32_TBL
crc = crc32_le_tbl[(crc & 0xff) ^ *p++] ^ (crc >> 8);
#else
crc ^= *p++;
for (int j = 0; j < 8; j++)
crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
#endif
}
return crc;
}