-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc.c
54 lines (42 loc) · 984 Bytes
/
crc.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
#include "crc.h"
// Adapted from https://stackoverflow.com/a/15031244/4454028
uint32_t crc32(const void * data, uint32_t len) {
const uint8_t * bytes = (const uint8_t *) data;
uint32_t crc = 0xFFFFFFFF;
while (len) {
crc ^= *bytes;
for (int k = 0; k < 8; k++) {
if (crc & 1) {
crc = (crc >> 1) ^ 0xEDB88320;
} else {
crc = (crc >> 1);
}
}
bytes++;
len--;
}
return ~crc;
}
inline uint32_t crc32_inner(uint32_t crc, const uint8_t *bytes, uint32_t len)
{
while (len) {
crc ^= *bytes;
for (int k = 0; k < 8; k++) {
if (crc & 1) {
crc = (crc >> 1) ^ 0xEDB88320;
} else {
crc = (crc >> 1);
}
}
bytes++;
len--;
}
return crc;
}
uint32_t crc32_frame(const uint8_t *startdata, const uint16_t startlen, const void * data, const uint16_t size)
{
uint32_t crc = 0xFFFFFFFF;
crc = crc32_inner(crc, startdata, startlen);
crc = crc32_inner(crc, data, size);
return ~crc;
}