mirror of
https://github.com/zephyrproject-rtos/zephyr
synced 2025-09-04 13:41:56 +00:00
In file crc16_sw.c essential type of LHS operand (16 bit) is wider than essential type of composite expression in RHS operand (8 bit). In crc32c_sw.c and crc32_sw.c Essential type of LHS operand (32 bit) is wider than essential type of composite expression in RHS operand (8 bit) Found as a coding guideline violation (MISRA R10.7) by static coding scanning tool. Signed-off-by: Maksim Masalski <maksim.masalski@intel.com>
63 lines
1.2 KiB
C
63 lines
1.2 KiB
C
/*
|
|
* Copyright (c) 2017 Intel Corporation.
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include <sys/crc.h>
|
|
|
|
uint16_t crc16(const uint8_t *src, size_t len, uint16_t polynomial,
|
|
uint16_t initial_value, bool pad)
|
|
{
|
|
uint16_t crc = initial_value;
|
|
size_t padding = pad ? sizeof(crc) : 0;
|
|
size_t i, b;
|
|
|
|
/* src length + padding (if required) */
|
|
for (i = 0; i < len + padding; i++) {
|
|
|
|
for (b = 0; b < 8; b++) {
|
|
uint16_t divide = crc & 0x8000UL;
|
|
|
|
crc = (crc << 1U);
|
|
|
|
/* choose input bytes or implicit trailing zeros */
|
|
if (i < len) {
|
|
crc |= !!(src[i] & (0x80U >> b));
|
|
}
|
|
|
|
if (divide != 0U) {
|
|
crc = crc ^ polynomial;
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|