Zephyr API Documentation 4.2.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
crc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Workaround GmbH.
3 * Copyright (c) 2017 Intel Corporation.
4 * Copyright (c) 2017 Nordic Semiconductor ASA
5 * Copyright (c) 2015 Runtime Inc
6 * Copyright (c) 2018 Google LLC.
7 * Copyright (c) 2022 Meta
8 * Copyright (c) 2024 Intercreate, Inc.
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
15
16#ifndef ZEPHYR_INCLUDE_SYS_CRC_H_
17#define ZEPHYR_INCLUDE_SYS_CRC_H_
18
19#include <zephyr/types.h>
20#include <stdbool.h>
21#include <stddef.h>
22
23#include <zephyr/sys/__assert.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29/* Initial value expected to be used at the beginning of the crc8_ccitt
30 * computation.
31 */
32#define CRC8_CCITT_INITIAL_VALUE 0xFF
33#define CRC8_ROHC_INITIAL_VALUE 0xFF
34
35/* Initial value expected to be used at the beginning of the OpenPGP CRC-24 computation. */
36#define CRC24_PGP_INITIAL_VALUE 0x00B704CEU
37/*
38 * The CRC-24 value is stored on a 32-bit value, only the 3 least significant bytes
39 * are meaningful. Use the following mask to only keep the CRC-24 value.
40 */
41#define CRC24_FINAL_VALUE_MASK 0x00FFFFFFU
42
47
53
60
62#define CRC4_POLY 0x3
63
65#define CRC4_REFLECT_POLY 0xC
66
68#define CRC7_BE_POLY 0x09
69
71#define CRC8_POLY 0x07
72
74#define CRC8_REFLECT_POLY 0xE0
75
77#define CRC16_POLY 0x8005
78
80#define CRC16_REFLECT_POLY 0xA001
81
83#define CRC16_CCITT_POLY 0x1021
84
86#define CRC24_PGP_POLY 0x01864CFBU
87
89#define CRC32_IEEE_POLY 0x04C11DB7U
90
92#define CRC32C_POLY 0x1EDC6F41U
93
95#define CRC32K_4_2_POLY 0x93A409EBU
96
98
120
140uint16_t crc16(uint16_t poly, uint16_t seed, const uint8_t *src, size_t len);
141
170uint16_t crc16_reflect(uint16_t poly, uint16_t seed, const uint8_t *src, size_t len);
186uint8_t crc8(const uint8_t *src, size_t len, uint8_t polynomial, uint8_t initial_value,
187 bool reversed);
188
220uint16_t crc16_ccitt(uint16_t seed, const uint8_t *src, size_t len);
221
257uint16_t crc16_itu_t(uint16_t seed, const uint8_t *src, size_t len);
258
270static inline uint16_t crc16_ansi(const uint8_t *src, size_t len)
271{
272 return crc16_reflect(0xA001, 0xffff, src, len);
273}
274
284uint32_t crc32_ieee(const uint8_t *data, size_t len);
285
296uint32_t crc32_ieee_update(uint32_t crc, const uint8_t *data, size_t len);
297
311 size_t len, bool first_pkt, bool last_pkt);
312
337uint32_t crc32_k_4_2_update(uint32_t crc, const uint8_t *data, size_t len);
338
350uint8_t crc8_ccitt(uint8_t initial_value, const void *buf, size_t len);
351
364uint8_t crc8_rohc(uint8_t initial_value, const void *buf, size_t len);
365
379uint8_t crc7_be(uint8_t seed, const uint8_t *src, size_t len);
380
394uint8_t crc4_ti(uint8_t seed, const uint8_t *src, size_t len);
395
413uint8_t crc4(const uint8_t *src, size_t len, uint8_t polynomial, uint8_t initial_value,
414 bool reversed);
415
424uint32_t crc24_pgp(const uint8_t *data, size_t len);
425
437uint32_t crc24_pgp_update(uint32_t crc, const uint8_t *data, size_t len);
438
448uint32_t crc24q_rtcm3(const uint8_t *data, size_t len);
449
472static inline uint32_t crc_by_type(enum crc_type type, const uint8_t *src, size_t len,
473 uint32_t seed, uint32_t poly, bool reflect, bool first,
474 bool last)
475{
476 switch (type) {
477 case CRC4:
478 return crc4(src, len, poly, seed, reflect);
479 case CRC4_TI:
480 return crc4_ti(seed, src, len);
481 case CRC7_BE:
482 return crc7_be(seed, src, len);
483 case CRC8:
484 return crc8(src, len, poly, seed, reflect);
485 case CRC8_CCITT:
486 return crc8_ccitt(seed, src, len);
487 case CRC8_ROHC:
488 return crc8_rohc(seed, src, len);
489 case CRC16:
490 if (reflect) {
491 return crc16_reflect(poly, seed, src, len);
492 } else {
493 return crc16(poly, seed, src, len);
494 }
495 case CRC16_ANSI:
496 return crc16_ansi(src, len);
497 case CRC16_CCITT:
498 return crc16_ccitt(seed, src, len);
499 case CRC16_ITU_T:
500 return crc16_itu_t(seed, src, len);
501 case CRC24_PGP: {
502 uint32_t crc = crc24_pgp_update(seed, src, len);
503
504 if (last) {
506 }
507 return crc;
508 }
509 case CRC32_C:
510 return crc32_c(seed, src, len, first, last);
511 case CRC32_IEEE:
512 return crc32_ieee_update(seed, src, len);
513 case CRC32_K_4_2:
514 return crc32_k_4_2_update(seed, src, len);
515 default:
516 break;
517 }
518
519 __ASSERT_NO_MSG(false);
520 return -1;
521}
522
526
527#ifdef __cplusplus
528}
529#endif
530
531#endif
uint32_t crc24q_rtcm3(const uint8_t *data, size_t len)
Calculate an RTCM3 CRC24Q frame checksum.
uint8_t crc7_be(uint8_t seed, const uint8_t *src, size_t len)
Compute the CRC-7 checksum of a buffer.
uint32_t crc32_ieee_update(uint32_t crc, const uint8_t *data, size_t len)
Update an IEEE conforming CRC32 checksum.
uint16_t crc16(uint16_t poly, uint16_t seed, const uint8_t *src, size_t len)
Generic function for computing a CRC-16 without input or output reflection.
uint8_t crc4_ti(uint8_t seed, const uint8_t *src, size_t len)
Compute the CRC-4 checksum of a buffer.
uint8_t crc4(const uint8_t *src, size_t len, uint8_t polynomial, uint8_t initial_value, bool reversed)
Generic function for computing CRC 4.
uint32_t crc24_pgp_update(uint32_t crc, const uint8_t *data, size_t len)
Update an OpenPGP CRC-24 checksum.
uint16_t crc16_itu_t(uint16_t seed, const uint8_t *src, size_t len)
Compute the checksum of a buffer with polynomial 0x1021, no reflection of input or output.
static uint32_t crc_by_type(enum crc_type type, const uint8_t *src, size_t len, uint32_t seed, uint32_t poly, bool reflect, bool first, bool last)
Compute a CRC checksum, in a generic way.
Definition crc.h:472
uint16_t crc16_ccitt(uint16_t seed, const uint8_t *src, size_t len)
Compute the checksum of a buffer with polynomial 0x1021, reflecting input and output.
uint8_t crc8_rohc(uint8_t initial_value, const void *buf, size_t len)
Compute ROHC variant of CRC 8.
uint32_t crc32_c(uint32_t crc, const uint8_t *data, size_t len, bool first_pkt, bool last_pkt)
Calculate CRC32C (Castagnoli) checksum.
uint8_t crc8_ccitt(uint8_t initial_value, const void *buf, size_t len)
Compute CCITT variant of CRC 8.
uint8_t crc8(const uint8_t *src, size_t len, uint8_t polynomial, uint8_t initial_value, bool reversed)
Generic function for computing CRC 8.
static uint16_t crc16_ansi(const uint8_t *src, size_t len)
Compute the ANSI (or Modbus) variant of CRC-16.
Definition crc.h:270
uint32_t crc32_k_4_2_update(uint32_t crc, const uint8_t *data, size_t len)
Update a CRC-32K/4.2 (*op) (Koopman) checksum.
crc_type
CRC algorithm enumeration.
Definition crc.h:104
uint16_t crc16_reflect(uint16_t poly, uint16_t seed, const uint8_t *src, size_t len)
Generic function for computing a CRC-16 with input and output reflection.
uint32_t crc24_pgp(const uint8_t *data, size_t len)
Generate an OpenPGP CRC-24 checksum as defined in RFC 4880 section 6.1.
uint32_t crc32_ieee(const uint8_t *data, size_t len)
Generate IEEE conform CRC32 checksum.
@ CRC7_BE
Use crc7_be.
Definition crc.h:107
@ CRC16
Use crc16.
Definition crc.h:111
@ CRC4_TI
Use crc4_ti.
Definition crc.h:106
@ CRC16_ITU_T
Use crc16_itu_t.
Definition crc.h:114
@ CRC32_C
Use crc32_c.
Definition crc.h:116
@ CRC16_ANSI
Use crc16_ansi.
Definition crc.h:112
@ CRC32_K_4_2
Use crc32_k_4_2_update.
Definition crc.h:118
@ CRC8
Use crc8.
Definition crc.h:108
@ CRC24_PGP
Use crc24_pgp.
Definition crc.h:115
@ CRC16_CCITT
Use crc16_ccitt.
Definition crc.h:113
@ CRC8_ROHC
Use crc8_rohc.
Definition crc.h:110
@ CRC8_CCITT
Use crc8_ccitt.
Definition crc.h:109
@ CRC32_IEEE
Use crc32_ieee.
Definition crc.h:117
@ CRC4
Use crc4.
Definition crc.h:105
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
#define CRC24_FINAL_VALUE_MASK
Definition crc.h:41