2018-04-24 19:56:45 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Workaround GmbH.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 17:23:05 +08:00
|
|
|
#include <zephyr/sys/crc.h>
|
2018-04-24 19:56:45 +08:00
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t crc32_ieee(const uint8_t *data, size_t len)
|
2018-04-24 19:56:45 +08:00
|
|
|
{
|
|
|
|
return crc32_ieee_update(0x0, data, len);
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t crc32_ieee_update(uint32_t crc, const uint8_t *data, size_t len)
|
2018-04-24 19:56:45 +08:00
|
|
|
{
|
2021-01-15 23:24:21 +08:00
|
|
|
/* crc table generated from polynomial 0xedb88320 */
|
|
|
|
static const uint32_t table[16] = {
|
2021-11-12 23:45:12 +08:00
|
|
|
0x00000000U, 0x1db71064U, 0x3b6e20c8U, 0x26d930acU,
|
|
|
|
0x76dc4190U, 0x6b6b51f4U, 0x4db26158U, 0x5005713cU,
|
|
|
|
0xedb88320U, 0xf00f9344U, 0xd6d6a3e8U, 0xcb61b38cU,
|
|
|
|
0x9b64c2b0U, 0x86d3d2d4U, 0xa00ae278U, 0xbdbdf21cU,
|
2021-01-15 23:24:21 +08:00
|
|
|
};
|
|
|
|
|
2018-04-24 19:56:45 +08:00
|
|
|
crc = ~crc;
|
2021-01-15 23:24:21 +08:00
|
|
|
|
2018-04-24 19:56:45 +08:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
2021-01-15 23:24:21 +08:00
|
|
|
uint8_t byte = data[i];
|
2018-04-24 19:56:45 +08:00
|
|
|
|
2021-01-15 23:24:21 +08:00
|
|
|
crc = (crc >> 4) ^ table[(crc ^ byte) & 0x0f];
|
2021-06-10 15:50:18 +08:00
|
|
|
crc = (crc >> 4) ^ table[(crc ^ ((uint32_t)byte >> 4)) & 0x0f];
|
2018-04-24 19:56:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (~crc);
|
|
|
|
}
|