summaryrefslogtreecommitdiff
path: root/src/lib/crc_byte.c
blob: 55529a2346144aabe0c63257f59b6de70e3e637c (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
/*
 * This file is part of the coreboot project.
 *
 *
 * 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.
 */

#include <crc_byte.h>

uint8_t crc7_byte(uint8_t prev_crc, uint8_t data)
{
	const uint8_t g = 0x89;
	prev_crc ^= data;
	for (int i = 0; i < 8; i++) {
		if (prev_crc & 0x80)
			prev_crc ^= g;
		prev_crc <<= 1;
	}
	return prev_crc;
}

uint16_t crc16_byte(uint16_t prev_crc, uint8_t data)
{
	prev_crc  = (uint8_t)(prev_crc >> 8)|(prev_crc << 8);
	prev_crc ^=  data;
	prev_crc ^= (uint8_t)(prev_crc & 0xff) >> 4;
	prev_crc ^= (prev_crc << 8) << 4;
	prev_crc ^= ((prev_crc & 0xff) << 4) << 1;
	return prev_crc;
}

uint32_t crc32_byte(uint32_t prev_crc, uint8_t data)
{
	prev_crc ^= (uint32_t)data << 24;

	for (int i = 0; i < 8; i++) {
		if ((prev_crc & 0x80000000UL) != 0)
			prev_crc = ((prev_crc << 1) ^ 0x04C11DB7UL);
		else
			prev_crc <<= 1;
	}

	return prev_crc;
}