/ src / include / crc_byte.h
crc_byte.h
 1  /* SPDX-License-Identifier: GPL-2.0-only */
 2  
 3  #ifndef CRC_BYTE_H
 4  #define CRC_BYTE_H
 5  
 6  #include <stddef.h>
 7  #include <stdint.h>
 8  
 9  /* This function is used to calculate crc7 byte by byte, with polynomial
10   * x^7 + x^3 + 1.
11   *
12   * prev_crc: old crc result (0 for first)
13   * data: new byte
14   * return value: new crc result
15   */
16  uint8_t crc7_byte(uint8_t prev_crc, uint8_t data);
17  
18  /* This function is used to calculate crc16 byte by byte, with polynomial
19   * x^16 + x^12 + x^5 + 1.
20   *
21   * prev_crc: old crc result (0 for first)
22   * data: new byte
23   * return value: new crc result
24   */
25  uint16_t crc16_byte(uint16_t prev_crc, uint8_t data);
26  
27  /* This function is used to calculate crc32 byte by byte, with polynomial
28   * x^32 + x^26 +  x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 +
29   * x^5 + x^4 + x^2 + x + 1
30   *
31   * prev_crc: old crc result (0 for first)
32   * data: new byte
33   * return value: new crc result
34   */
35  uint32_t crc32_byte(uint32_t prev_crc, uint8_t data);
36  
37  #define CRC(buf, size, crc_func) ({ \
38  	const uint8_t *_crc_local_buf = (const uint8_t *)(buf); \
39  	size_t _crc_local_size = size; \
40  	__typeof__(crc_func(0, 0)) _crc_local_result = 0; \
41  	while (_crc_local_size--) { \
42  		_crc_local_result = crc_func(_crc_local_result, *_crc_local_buf++); \
43  	} \
44  	_crc_local_result; \
45  })
46  
47  #endif /* CRC_BYTE_H */