Zephyr Project API 3.7.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
33/* Initial value expected to be used at the beginning of the OpenPGP CRC-24 computation. */
34#define CRC24_PGP_INITIAL_VALUE 0x00B704CEU
35/*
36 * The CRC-24 value is stored on a 32-bit value, only the 3 least significant bytes
37 * are meaningful. Use the following mask to only keep the CRC-24 value.
38 */
39#define CRC24_FINAL_VALUE_MASK 0x00FFFFFFU
40
71
91uint16_t crc16(uint16_t poly, uint16_t seed, const uint8_t *src, size_t len);
92
121uint16_t crc16_reflect(uint16_t poly, uint16_t seed, const uint8_t *src, size_t len);
137uint8_t crc8(const uint8_t *src, size_t len, uint8_t polynomial, uint8_t initial_value,
138 bool reversed);
139
171uint16_t crc16_ccitt(uint16_t seed, const uint8_t *src, size_t len);
172
208uint16_t crc16_itu_t(uint16_t seed, const uint8_t *src, size_t len);
209
221static inline uint16_t crc16_ansi(const uint8_t *src, size_t len)
222{
223 return crc16_reflect(0xA001, 0xffff, src, len);
224}
225
235uint32_t crc32_ieee(const uint8_t *data, size_t len);
236
248
262 size_t len, bool first_pkt, bool last_pkt);
263
275uint8_t crc8_ccitt(uint8_t initial_value, const void *buf, size_t len);
276
290uint8_t crc7_be(uint8_t seed, const uint8_t *src, size_t len);
291
305uint8_t crc4_ti(uint8_t seed, const uint8_t *src, size_t len);
306
324uint8_t crc4(const uint8_t *src, size_t len, uint8_t polynomial, uint8_t initial_value,
325 bool reversed);
326
335uint32_t crc24_pgp(const uint8_t *data, size_t len);
336
349
372static inline uint32_t crc_by_type(enum crc_type type, const uint8_t *src, size_t len,
373 uint32_t seed, uint32_t poly, bool reflect, bool first,
374 bool last)
375{
376 switch (type) {
377 case CRC4:
378 return crc4(src, len, poly, seed, reflect);
379 case CRC4_TI:
380 return crc4_ti(seed, src, len);
381 case CRC7_BE:
382 return crc7_be(seed, src, len);
383 case CRC8:
384 return crc8(src, len, poly, seed, reflect);
385 case CRC8_CCITT:
386 return crc8_ccitt(seed, src, len);
387 case CRC16:
388 if (reflect) {
389 return crc16_reflect(poly, seed, src, len);
390 } else {
391 return crc16(poly, seed, src, len);
392 }
393 case CRC16_ANSI:
394 return crc16_ansi(src, len);
395 case CRC16_CCITT:
396 return crc16_ccitt(seed, src, len);
397 case CRC16_ITU_T:
398 return crc16_itu_t(seed, src, len);
399 case CRC24_PGP: {
400 uint32_t crc = crc24_pgp_update(seed, src, len);
401
402 if (last)
404 return crc;
405 }
406 case CRC32_C:
407 return crc32_c(seed, src, len, first, last);
408 case CRC32_IEEE:
409 return crc32_ieee_update(seed, src, len);
410 default:
411 break;
412 }
413
414 __ASSERT_NO_MSG(false);
415 return -1;
416}
417
422#ifdef __cplusplus
423}
424#endif
425
426#endif
#define CRC24_FINAL_VALUE_MASK
Definition crc.h:39
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:372
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.
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:221
crc_type
CRC algorithm enumeration.
Definition crc.h:57
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:60
@ CRC16
Use crc16.
Definition crc.h:63
@ CRC4_TI
Use crc4_ti.
Definition crc.h:59
@ CRC16_ITU_T
Use crc16_itu_t.
Definition crc.h:66
@ CRC32_C
Use crc32_c.
Definition crc.h:68
@ CRC16_ANSI
Use crc16_ansi.
Definition crc.h:64
@ CRC8
Use crc8.
Definition crc.h:61
@ CRC24_PGP
Use crc24_pgp.
Definition crc.h:67
@ CRC16_CCITT
Use crc16_ccitt.
Definition crc.h:65
@ CRC8_CCITT
Use crc8_ccitt.
Definition crc.h:62
@ CRC32_IEEE
Use crc32_ieee.
Definition crc.h:69
@ CRC4
Use crc4.
Definition crc.h:58
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
static fdata_t data[2]
Definition test_fifo_contexts.c:15