mirror of
https://github.com/zephyrproject-rtos/zephyr
synced 2025-08-31 23:18:25 +00:00
'pad' parameter controls whether crc16() should add padding at the end of input bytes or not. This allows to compute CRC16 for data stored in non-contiguous buffers where CRC value is calculated using subsequent calls to crc16() with padding added only for last chunk. Signed-off-by: Andrzej Kaczmarek <andrzej.kaczmarek@codecoup.pl>
37 lines
646 B
C
37 lines
646 B
C
/*
|
|
* Copyright (c) 2017 Intel Corporation.
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include <crc16.h>
|
|
|
|
u16_t crc16(const u8_t *src, size_t len, u16_t polynomial,
|
|
u16_t initial_value, bool pad)
|
|
{
|
|
u16_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++) {
|
|
u16_t divide = crc & 0x8000;
|
|
|
|
crc = (crc << 1);
|
|
|
|
/* choose input bytes or implicit trailing zeros */
|
|
if (i < len) {
|
|
crc |= !!(src[i] & (0x80 >> b));
|
|
}
|
|
|
|
if (divide) {
|
|
crc = crc ^ polynomial;
|
|
}
|
|
}
|
|
}
|
|
|
|
return crc;
|
|
}
|