blob: 85b7221053a93120f511fc9a9608b7481a597f32 [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Xiang Wang4e39c822019-11-05 12:00:39 +08002
3#ifndef CRC_BYTE_H
4#define CRC_BYTE_H
5
Elyes HAOUAS5817c562020-07-12 09:03:22 +02006#include <stddef.h>
Xiang Wang4e39c822019-11-05 12:00:39 +08007#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 */
16uint8_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 */
25uint16_t crc16_byte(uint16_t prev_crc, uint8_t data);
26
Patrick Rudolphcc0b6f12019-12-15 18:03:36 +010027/* 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 */
35uint32_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})
Xiang Wang4e39c822019-11-05 12:00:39 +080046
47#endif /* CRC_BYTE_H */