Skip to content

Commit 5cbc3b2

Browse files
committed
crc: Provide both crc16 and crc32 seprarately
Signed-off-by: Siddharth Chandrasekaran <sidcha.dev@gmail.com>
1 parent f694743 commit 5cbc3b2

4 files changed

Lines changed: 42 additions & 20 deletions

File tree

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef _UTILS_CRC32_H_
2-
#define _UTILS_CRC32_H_
1+
#ifndef _UTILS_CRC16_H_
2+
#define _UTILS_CRC16_H_
33

44
#include <stdint.h>
55
#include <stddef.h>
@@ -18,10 +18,8 @@ uint16_t crc16_itu_t(uint16_t seed, const uint8_t *src, size_t len);
1818

1919
#endif
2020

21-
uint32_t compute_crc32(uint32_t seed, const uint8_t *buffer, size_t length);
22-
2321
#ifdef __cplusplus
2422
}
2523
#endif
2624

27-
#endif /* _UTILS_CRC32_H_ */
25+
#endif /* _UTILS_CRC16_H_ */

include/utils/crc32.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef _UTILS_CRC32_H_
2+
#define _UTILS_CRC32_H_
3+
4+
#include <stdint.h>
5+
#include <stddef.h>
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
uint32_t compute_crc32(uint32_t seed, const uint8_t *buffer, size_t length);
12+
13+
#ifdef __cplusplus
14+
}
15+
#endif
16+
17+
#endif /* _UTILS_CRC32_H_ */

src/crc16.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2025 Siddharth Chandrasekaran <sidcha.dev@gmail.com>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdint.h>
8+
#include <stdlib.h>
9+
10+
/* From Zephyr, to retain the same name */
11+
uint16_t crc16_itu_t(uint16_t seed, const uint8_t *src, size_t len)
12+
{
13+
for (; len > 0; len--) {
14+
seed = (seed >> 8U) | (seed << 8U);
15+
seed ^= *src++;
16+
seed ^= (seed & 0xffU) >> 4U;
17+
seed ^= seed << 12U;
18+
seed ^= (seed & 0xffU) << 5U;
19+
}
20+
return seed;
21+
}

src/crc.c renamed to src/crc32.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* CRC32 code derived from work by Gary S. Brown.
4343
*/
4444

45-
#include <utils/crc.h>
45+
#include <utils/crc32.h>
4646

4747
const uint32_t crc32_tab[] = {
4848
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
@@ -748,17 +748,3 @@ uint32_t compute_crc32(uint32_t seed, const uint8_t *buffer, size_t length)
748748
return (multitable_crc32c(seed, buffer, length));
749749
}
750750
}
751-
752-
/* --- CRC-16 --- */
753-
754-
uint16_t crc16_itu_t(uint16_t seed, const uint8_t *src, size_t len)
755-
{
756-
for (; len > 0; len--) {
757-
seed = (seed >> 8U) | (seed << 8U);
758-
seed ^= *src++;
759-
seed ^= (seed & 0xffU) >> 4U;
760-
seed ^= seed << 12U;
761-
seed ^= (seed & 0xffU) << 5U;
762-
}
763-
return seed;
764-
}

0 commit comments

Comments
 (0)