lib: os: Introduce support for CRC32C algorithm
This introduces the support for CRC32C (Castagnoli) algorithm. The generator polynomial used is 0x1EDC6F41UL. Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
This commit is contained in:
parent
77bca9bc1e
commit
4269ecd2bc
3 changed files with 56 additions and 0 deletions
|
@ -161,6 +161,21 @@ uint32_t crc32_ieee(const uint8_t *data, size_t len);
|
|||
*/
|
||||
uint32_t crc32_ieee_update(uint32_t crc, const uint8_t *data, size_t len);
|
||||
|
||||
/**
|
||||
* @brief Calculate CRC32C (Castagnoli) checksum.
|
||||
*
|
||||
* @param crc CRC32C checksum that needs to be updated.
|
||||
* @param *data Pointer to data on which the CRC should be calculated.
|
||||
* @param len Data length.
|
||||
* @param first_pkt Whether this is the first packet in the stream.
|
||||
* @param last_pkt Whether this is the last packet in the stream.
|
||||
*
|
||||
* @return CRC32 value.
|
||||
*
|
||||
*/
|
||||
uint32_t crc32_c(uint32_t crc, const uint8_t *data,
|
||||
size_t len, bool first_pkt, bool last_pkt);
|
||||
|
||||
/**
|
||||
* @brief Compute CCITT variant of CRC 8
|
||||
*
|
||||
|
|
|
@ -4,6 +4,7 @@ zephyr_sources_ifdef(CONFIG_BASE64 base64.c)
|
|||
|
||||
zephyr_sources(
|
||||
cbprintf.c
|
||||
crc32c_sw.c
|
||||
crc32_sw.c
|
||||
crc16_sw.c
|
||||
crc8_sw.c
|
||||
|
|
40
lib/os/crc32c_sw.c
Normal file
40
lib/os/crc32c_sw.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Workaround GmbH.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <sys/crc.h>
|
||||
|
||||
/* crc table generated from polynomial 0x1EDC6F41UL (Castagnoli) */
|
||||
static const uint32_t crc32c_table[16] = {
|
||||
0x00000000UL, 0x105EC76FUL, 0x20BD8EDEUL, 0x30E349B1UL,
|
||||
0x417B1DBCUL, 0x5125DAD3UL, 0x61C69362UL, 0x7198540DUL,
|
||||
0x82F63B78UL, 0x92A8FC17UL, 0xA24BB5A6UL, 0xB21572C9UL,
|
||||
0xC38D26C4UL, 0xD3D3E1ABUL, 0xE330A81AUL, 0xF36E6F75UL
|
||||
};
|
||||
|
||||
/* This value needs to be XORed with the final crc value once crc for
|
||||
* the entire stream is calculated. This is a requirement of crc32c algo.
|
||||
*/
|
||||
#define CRC32C_XOR_OUT 0xFFFFFFFFUL
|
||||
|
||||
/* The crc32c algorithm requires the below value as Init value at the
|
||||
* beginning of the stream.
|
||||
*/
|
||||
#define CRC32C_INIT 0xFFFFFFFFUL
|
||||
|
||||
uint32_t crc32_c(uint32_t crc, const uint8_t *data,
|
||||
size_t len, bool first_pkt, bool last_pkt)
|
||||
{
|
||||
if (first_pkt) {
|
||||
crc = CRC32C_INIT;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
crc = crc32c_table[(crc ^ data[i]) & 0x0F] ^ (crc >> 4);
|
||||
crc = crc32c_table[(crc ^ (data[i] >> 4)) & 0x0F] ^ (crc >> 4);
|
||||
}
|
||||
|
||||
return last_pkt ? (crc ^ CRC32C_XOR_OUT) : crc;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue