-
Notifications
You must be signed in to change notification settings - Fork 12
/
crc16.c
48 lines (35 loc) · 823 Bytes
/
crc16.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
/*
File: crc16.c
Functions to calculate CRC16
*/
#include <inttypes.h>
uint16_t crc16_ccitt (const uint8_t *data, unsigned int length)
{
// Polynomial: x^16 + x^12 + x^5 + 1 (0x8408)
// Initial value: 0xffff
uint8_t x;
uint16_t crc = 0xffff;
while (length--) {
x = crc >> 8 ^ *data++;
x ^= x >> 4;
crc = (crc << 8) ^ ((uint16_t)(x << 12)) ^ ((uint16_t)(x <<5)) ^ ((uint16_t)x);
}
return crc;
}
uint16_t crc16 (const uint8_t *data, unsigned int length)
{
// Polynomial: x^16 + x^15 + x^2 + 1 (0xa001)
uint8_t x;
uint16_t crc = 0;
while (length--) {
int i;
crc ^= *data++;
for (i = 0 ; i < 8 ; ++i) {
if (crc & 1)
crc = (crc >> 1) ^ 0xa001;
else
crc = (crc >> 1);
}
}
return crc;
}