File: 2crc8.c

package info (click to toggle)
coreboot 24.12%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 210,640 kB
  • sloc: ansic: 1,640,478; sh: 15,676; python: 10,743; perl: 10,186; asm: 8,483; makefile: 5,097; cpp: 4,724; pascal: 2,327; ada: 1,928; yacc: 1,264; lex: 731; sed: 75; lisp: 5; ruby: 5; awk: 4
file content (31 lines) | stat: -rw-r--r-- 801 bytes parent folder | download | duplicates (2)
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
/* Copyright 2014 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Very simple 8-bit CRC function.
 */

#include "2crc8.h"
#include "2sysincludes.h"

/* Uses CRC-8 ITU version, with x^8 + x^2 + x + 1 polynomial.
   Note that result will evaluate to zero for a buffer of all zeroes. */
uint8_t vb2_crc8(const void *vptr, uint32_t size)
{
	const uint8_t *data = vptr;
	unsigned crc = 0;
	uint32_t i, j;

	/* Calculate CRC-8 directly.  A table-based algorithm would be faster,
	   but for only a few bytes it isn't worth the code size. */
	for (j = size; j; j--, data++) {
		crc ^= (*data << 8);
		for (i = 8; i; i--) {
			if (crc & 0x8000)
				crc ^= (0x1070 << 3);
			crc <<= 1;
		}
	}

	return (uint8_t)(crc >> 8);
}