Branch data Line data Source code
1 : : /* SPDX-FileCopyrightText: Copyright 2026 Cozens Software Solutions Limited
2 : : * SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 OR LicenseRef-PolyForm-Internal-Use-1.0.0 OR LicenseRef-COSOSO-Commercial
3 : : */
4 : :
5 : : /*
6 : : * CRC-16/CCITT-FALSE (ITU-T V.41)
7 : : * Polynomial: 0x1021 Init: 0xFFFF RefIn: false RefOut: false XorOut: 0x0000
8 : : *
9 : : * Check value 0x29B1 from Greg Cook's CRC catalogue
10 : : * (reveng.sourceforge.io/crc-catalogue/16.htm, entry crc-16-ibm-3740).
11 : : */
12 : :
13 : : #include "SolidSyslogCrc16.h"
14 : :
15 : : enum
16 : : {
17 : : CRC16_CCITT_INIT = 0xFFFFU,
18 : : CRC16_CCITT_POLY = 0x1021U,
19 : : MSB_MASK = 0x8000U
20 : : };
21 : :
22 : 245046 : uint16_t SolidSyslogCrc16_Compute(const uint8_t* data, uint16_t length)
23 : : {
24 : 245046 : uint16_t crc = CRC16_CCITT_INIT;
25 : :
26 [ + + ]: 9088427 : for (uint16_t i = 0U; i < length; i++)
27 : : {
28 : 8843381 : crc ^= (uint16_t) ((uint16_t) data[i] << 8U);
29 : :
30 [ + + ]: 79590429 : for (uint_fast8_t bit = 0U; bit < 8U; bit++)
31 : : {
32 [ + + ]: 70747048 : if ((crc & MSB_MASK) != 0U)
33 : : {
34 : 36371622 : crc = (uint16_t) ((crc << 1) ^ CRC16_CCITT_POLY);
35 : : }
36 : : else
37 : : {
38 : 34375426 : crc = (uint16_t) (crc << 1);
39 : : }
40 : : }
41 : : }
42 : :
43 : 245046 : return crc;
44 : : }
|