1 /*
2  * Copyright (c) 2018 Workaround GmbH.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/sys/crc.h>
8 
crc32_ieee(const uint8_t * data,size_t len)9 uint32_t crc32_ieee(const uint8_t *data, size_t len)
10 {
11 	return crc32_ieee_update(0x0, data, len);
12 }
13 
crc32_ieee_update(uint32_t crc,const uint8_t * data,size_t len)14 uint32_t crc32_ieee_update(uint32_t crc, const uint8_t *data, size_t len)
15 {
16 	/* crc table generated from polynomial 0xedb88320 */
17 	static const uint32_t table[16] = {
18 		0x00000000U, 0x1db71064U, 0x3b6e20c8U, 0x26d930acU,
19 		0x76dc4190U, 0x6b6b51f4U, 0x4db26158U, 0x5005713cU,
20 		0xedb88320U, 0xf00f9344U, 0xd6d6a3e8U, 0xcb61b38cU,
21 		0x9b64c2b0U, 0x86d3d2d4U, 0xa00ae278U, 0xbdbdf21cU,
22 	};
23 
24 	crc = ~crc;
25 
26 	for (size_t i = 0; i < len; i++) {
27 		uint8_t byte = data[i];
28 
29 		crc = (crc >> 4) ^ table[(crc ^ byte) & 0x0f];
30 		crc = (crc >> 4) ^ table[(crc ^ ((uint32_t)byte >> 4)) & 0x0f];
31 	}
32 
33 	return (~crc);
34 }
35