summaryrefslogtreecommitdiff
path: root/src/include/crc_byte.h
blob: c0df5b0ce4e28f6324da23b4af82e8a5057f0c8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2019 HardenedLinux
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#ifndef CRC_BYTE_H
#define CRC_BYTE_H

#include <stdint.h>

/* This function is used to calculate crc7 byte by byte, with polynomial
 * x^7 + x^3 + 1.
 *
 * prev_crc: old crc result (0 for first)
 * data: new byte
 * return value: new crc result
 */
uint8_t crc7_byte(uint8_t prev_crc, uint8_t data);

/* This function is used to calculate crc16 byte by byte, with polynomial
 * x^16 + x^12 + x^5 + 1.
 *
 * prev_crc: old crc result (0 for first)
 * data: new byte
 * return value: new crc result
 */
uint16_t crc16_byte(uint16_t prev_crc, uint8_t data);

/* This function is used to calculate crc32 byte by byte, with polynomial
 * x^32 + x^26 +  x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 +
 * x^5 + x^4 + x^2 + x + 1
 *
 * prev_crc: old crc result (0 for first)
 * data: new byte
 * return value: new crc result
 */
uint32_t crc32_byte(uint32_t prev_crc, uint8_t data);

#define CRC(buf, size, crc_func) ({ \
	const uint8_t *_crc_local_buf = (const uint8_t *)buf; \
	size_t _crc_local_size = size; \
	__typeof__(crc_func(0, 0)) _crc_local_result = 0; \
	while (_crc_local_size--) { \
		_crc_local_result = crc_func(_crc_local_result, *_crc_local_buf++); \
	} \
	_crc_local_result; \
})

#endif /* CRC_BYTE_H */