Zephyr API Documentation 4.0.0
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 *
9 * SPDX-License-Identifier: Apache-2.0
10 */
15#ifndef ZEPHYR_INCLUDE_SYS_CRC_H_
16#define ZEPHYR_INCLUDE_SYS_CRC_H_
17
18#include <zephyr/types.h>
19#include <stdbool.h>
20#include <stddef.h>
21
22#include <zephyr/sys/__assert.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28/* Initial value expected to be used at the beginning of the crc8_ccitt
29 * computation.
30 */
31#define CRC8_CCITT_INITIAL_VALUE 0xFF
32#define CRC8_ROHC_INITIAL_VALUE 0xFF
33
34/* Initial value expected to be used at the beginning of the OpenPGP CRC-24 computation. */
35#define CRC24_PGP_INITIAL_VALUE 0x00B704CEU
36/*
37 * The CRC-24 value is stored on a 32-bit value, only the 3 least significant bytes
38 * are meaningful. Use the following mask to only keep the CRC-24 value.
39 */
40#define CRC24_FINAL_VALUE_MASK 0x00FFFFFFU
41
73
93uint16_t crc16(uint16_t poly, uint16_t seed, const uint8_t *src, size_t len);
94
123uint16_t crc16_reflect(uint16_t poly, uint16_t seed, const uint8_t *src, size_t len);
139uint8_t crc8(const uint8_t *src, size_t len, uint8_t polynomial, uint8_t initial_value,
140 bool reversed);
141
173uint16_t crc16_ccitt(uint16_t seed, const uint8_t *src, size_t len);
174
210uint16_t crc16_itu_t(uint16_t seed, const uint8_t *src, size_t len);
211
223static inline uint16_t crc16_ansi(const uint8_t *src, size_t len)
224{
225 return crc16_reflect(0xA001, 0xffff, src, len);
226}
227
237uint32_t crc32_ieee(const uint8_t *data, size_t len);
238
249uint32_t crc32_ieee_update(uint32_t crc, const uint8_t *data, size_t len);
250
264 size_t len, bool first_pkt, bool last_pkt);
265
277uint8_t crc8_ccitt(uint8_t initial_value, const void *buf, size_t len);
278
291uint8_t crc8_rohc(uint8_t initial_value, const void *buf, size_t len);
292
306uint8_t crc7_be(uint8_t seed, const uint8_t *src, size_t len);
307
321uint8_t crc4_ti(uint8_t seed, const uint8_t *src, size_t len);
322
340uint8_t crc4(const uint8_t *src, size_t len, uint8_t polynomial, uint8_t initial_value,
341 bool reversed);
342
351uint32_t crc24_pgp(const uint8_t *data, size_t len);
352
364uint32_t crc24_pgp_update(uint32_t crc, const uint8_t *data, size_t len);
365
388static inline uint32_t crc_by_type(enum crc_type type, const uint8_t *src, size_t len,
389 uint32_t seed, uint32_t poly, bool reflect, bool first,
390 bool last)
391{
392 switch (type) {
393 case CRC4:
394 return crc4(src, len, poly, seed, reflect);
395 case CRC4_TI:
396 return crc4_ti(seed, src, len);
397 case CRC7_BE:
398 return crc7_be(seed, src, len);
399 case CRC8:
400 return crc8(src, len, poly, seed, reflect);
401 case CRC8_CCITT:
402 return crc8_ccitt(seed, src, len);
403 case CRC8_ROHC:
404 return crc8_rohc(seed, src, len);
405 case CRC16:
406 if (reflect) {
407 return crc16_reflect(poly, seed, src, len);
408 } else {
409 return crc16(poly, seed, src, len);
410 }
411 case CRC16_ANSI:
412 return crc16_ansi(src, len);
413 case CRC16_CCITT:
414 return crc16_ccitt(seed, src, len);
415 case CRC16_ITU_T:
416 return crc16_itu_t(seed, src, len);
417 case CRC24_PGP: {
418 uint32_t crc = crc24_pgp_update(seed, src, len);
419
420 if (last)
422 return crc;
423 }
424 case CRC32_C:
425 return crc32_c(seed, src, len, first, last);
426 case CRC32_IEEE:
427 return crc32_ieee_update(seed, src, len);
428 default:
429 break;
430 }
431
432 __ASSERT_NO_MSG(false);
433 return -1;
434}
435
440#ifdef __cplusplus
441}
442#endif
443
444#endif
#define CRC24_FINAL_VALUE_MASK
Definition crc.h:40
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:388
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:223
crc_type
CRC algorithm enumeration.
Definition crc.h:58
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:61
@ CRC16
Use crc16.
Definition crc.h:65
@ CRC4_TI
Use crc4_ti.
Definition crc.h:60
@ CRC16_ITU_T
Use crc16_itu_t.
Definition crc.h:68
@ CRC32_C
Use crc32_c.
Definition crc.h:70
@ CRC16_ANSI
Use crc16_ansi.
Definition crc.h:66
@ CRC8
Use crc8.
Definition crc.h:62
@ CRC24_PGP
Use crc24_pgp.
Definition crc.h:69
@ CRC16_CCITT
Use crc16_ccitt.
Definition crc.h:67
@ CRC8_ROHC
Use crc8_rohc.
Definition crc.h:64
@ CRC8_CCITT
Use crc8_ccitt.
Definition crc.h:63
@ CRC32_IEEE
Use crc32_ieee.
Definition crc.h:71
@ CRC4
Use crc4.
Definition crc.h:59
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89