blob: ac1f0325c3fed7368b726a3adaf350ab85e4bea5 [file] [log] [blame]
/*
* Copyright (c) 2017 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/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;
}