zephyr/lib/os/crc16_sw.c
Carles Cufi 7f6524949a lib: os: crc: Rework the crc16() implementation
As described in #42403, there was an issue with the existing crc16_ansi()
implementation, since it was not calculating the CRC-16-ANSI (aka
CRC-16-MODBUS). This is because the  existing crc16() function only
supported non-reflected input and output (and the CRC-16-ANSI requires
reflection on both) and also it did not seem to support correctly inial
seeds different from 0x0000 (and, again, the CRC-16-ANSI requires 0xffff
as an initial seed).

This commit replaces the existing crc16() with a functional pair,
crc16() and crc16_reflect(), that also work with any poly, any initial seed
and allow to select whether reflection is performed.
It also adapts crc16_ansi() so that it actually returns the correct CRC.

Fixes #42403.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2022-02-04 12:33:22 -05:00

77 lines
1.3 KiB
C

/*
* Copyright (c) 2017 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <sys/crc.h>
uint16_t crc16(uint16_t poly, uint16_t seed, const uint8_t *src, size_t len)
{
uint16_t crc = seed;
size_t i, j;
for (i = 0; i < len; i++) {
crc ^= (uint16_t)(src[i] << 8U);
for (j = 0; j < 8; j++) {
if (crc & 0x8000UL) {
crc = (crc << 1U) ^ poly;
} else {
crc = crc << 1U;
}
}
}
return crc;
}
uint16_t crc16_reflect(uint16_t poly, uint16_t seed, const uint8_t *src, size_t len)
{
uint16_t crc = seed;
size_t i, j;
for (i = 0; i < len; i++) {
crc ^= (uint16_t)src[i];
for (j = 0; j < 8; j++) {
if (crc & 0x0001UL) {
crc = (crc >> 1U) ^ poly;
} else {
crc = crc >> 1U;
}
}
}
return crc;
}
uint16_t crc16_ccitt(uint16_t seed, const uint8_t *src, size_t len)
{
for (; len > 0; len--) {
uint8_t e, f;
e = seed ^ *src++;
f = e ^ (e << 4);
seed = (seed >> 8) ^ ((uint16_t)f << 8) ^ ((uint16_t)f << 3) ^ ((uint16_t)f >> 4);
}
return seed;
}
uint16_t crc16_itu_t(uint16_t seed, const uint8_t *src, size_t len)
{
for (; len > 0; len--) {
seed = (seed >> 8U) | (seed << 8U);
seed ^= *src++;
seed ^= (seed & 0xffU) >> 4U;
seed ^= seed << 12U;
seed ^= (seed & 0xffU) << 5U;
}
return seed;
}