Skip to content

Commit 4269ecd

Browse files
rgundicarlescufi
authored andcommitted
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 <[email protected]>
1 parent 77bca9b commit 4269ecd

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

include/sys/crc.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,21 @@ uint32_t crc32_ieee(const uint8_t *data, size_t len);
161161
*/
162162
uint32_t crc32_ieee_update(uint32_t crc, const uint8_t *data, size_t len);
163163

164+
/**
165+
* @brief Calculate CRC32C (Castagnoli) checksum.
166+
*
167+
* @param crc CRC32C checksum that needs to be updated.
168+
* @param *data Pointer to data on which the CRC should be calculated.
169+
* @param len Data length.
170+
* @param first_pkt Whether this is the first packet in the stream.
171+
* @param last_pkt Whether this is the last packet in the stream.
172+
*
173+
* @return CRC32 value.
174+
*
175+
*/
176+
uint32_t crc32_c(uint32_t crc, const uint8_t *data,
177+
size_t len, bool first_pkt, bool last_pkt);
178+
164179
/**
165180
* @brief Compute CCITT variant of CRC 8
166181
*

lib/os/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ zephyr_sources_ifdef(CONFIG_BASE64 base64.c)
44

55
zephyr_sources(
66
cbprintf.c
7+
crc32c_sw.c
78
crc32_sw.c
89
crc16_sw.c
910
crc8_sw.c

lib/os/crc32c_sw.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2021 Workaround GmbH.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <sys/crc.h>
8+
9+
/* crc table generated from polynomial 0x1EDC6F41UL (Castagnoli) */
10+
static const uint32_t crc32c_table[16] = {
11+
0x00000000UL, 0x105EC76FUL, 0x20BD8EDEUL, 0x30E349B1UL,
12+
0x417B1DBCUL, 0x5125DAD3UL, 0x61C69362UL, 0x7198540DUL,
13+
0x82F63B78UL, 0x92A8FC17UL, 0xA24BB5A6UL, 0xB21572C9UL,
14+
0xC38D26C4UL, 0xD3D3E1ABUL, 0xE330A81AUL, 0xF36E6F75UL
15+
};
16+
17+
/* This value needs to be XORed with the final crc value once crc for
18+
* the entire stream is calculated. This is a requirement of crc32c algo.
19+
*/
20+
#define CRC32C_XOR_OUT 0xFFFFFFFFUL
21+
22+
/* The crc32c algorithm requires the below value as Init value at the
23+
* beginning of the stream.
24+
*/
25+
#define CRC32C_INIT 0xFFFFFFFFUL
26+
27+
uint32_t crc32_c(uint32_t crc, const uint8_t *data,
28+
size_t len, bool first_pkt, bool last_pkt)
29+
{
30+
if (first_pkt) {
31+
crc = CRC32C_INIT;
32+
}
33+
34+
for (size_t i = 0; i < len; i++) {
35+
crc = crc32c_table[(crc ^ data[i]) & 0x0F] ^ (crc >> 4);
36+
crc = crc32c_table[(crc ^ (data[i] >> 4)) & 0x0F] ^ (crc >> 4);
37+
}
38+
39+
return last_pkt ? (crc ^ CRC32C_XOR_OUT) : crc;
40+
}

0 commit comments

Comments
 (0)