blob: 9dc5942967f2b65ca533cdf109d621468bdc4b6e (
plain)
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
|
/*
* Ouroboros - Copyright (C) 2016 - 2026
*
* 16-bit Cyclic Redundancy Check (CCITT-FALSE variant)
*
* Dimitri Staessens <dimitri@ouroboros.rocks>
* Sander Vrijders <sander@ouroboros.rocks>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., http://www.fsf.org/about/contact/.
*/
/*
* CRC-16/CCITT-FALSE (reveng catalog, alias CRC-16/IBM-3740):
* poly = 0x1021
* init = 0xffff
* refin = false
* refout = false
* xorout = 0x0000
* check = crc16_ccitt_false("123456789") == 0x29b1
*/
#include "config.h"
#include <ouroboros/crc16.h>
/* Bit-by-bit MSB-first CRC. */
void crc16_ccitt_false(uint16_t * crc,
const void * buf,
size_t len)
{
const uint8_t * p;
uint16_t c;
size_t n;
int i;
p = (const uint8_t *) buf;
c = *crc ^ 0xffff;
for (n = 0; n < len; n++) {
c ^= ((uint16_t) p[n]) << 8;
for (i = 0; i < 8; i++) {
if (c & 0x8000)
c = (uint16_t) ((c << 1) ^ 0x1021);
else
c = (uint16_t) (c << 1);
}
}
*crc = c;
}
|