aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crc8.c
blob: 8b68a29e40be79d95c85ce88c9c6bd65b643a1be (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
/*
 * Copyright (c) 2013 Google, Inc
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include "linux/crc8.h"

unsigned int crc8(const unsigned char *vptr, int len)
{
	const unsigned char *data = vptr;
	unsigned int crc = 0;
	int i, j;

	for (j = len; j; j--, data++) {
		crc ^= (*data << 8);
		for (i = 8; i; i--) {
			if (crc & 0x8000)
				crc ^= (0x1070 << 3);
			crc <<= 1;
		}
	}

	return (crc >> 8) & 0xff;
}