summaryrefslogtreecommitdiffstats
path: root/contrib/syslinux-4.02/core/fs/btrfs/crc32c.h
blob: 2c317384fa63663bd51d4654b3b6a65bc1f62bae (plain) (blame)
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
/*
 * Copied from Linux kernel crypto/crc32c.c
 * Copyright (c) 2004 Cisco Systems, Inc.
 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
 *
 * 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; either version 2 of the License, or (at your option)
 * any later version.
 *
 */

/*
 * This is the CRC-32C table
 * Generated with:
 * width = 32 bits
 * poly = 0x1EDC6F41
 * reflect input bytes = true
 * reflect output bytes = true
 */

static u32 crc32c_table[256];

/*
 * Steps through buffer one byte at at time, calculates reflected
 * crc using table.
 */

static inline u32 crc32c_le(u32 crc, const char *data, size_t length)
{
	while (length--)
		crc = crc32c_table[(u8)(crc ^ *data++)] ^ (crc >> 8);

	return crc;
}

static inline void btrfs_init_crc32c(void)
{
	int i, j;
	u32 v;
	const u32 poly = 0x82F63B78; /* Bit-reflected CRC32C polynomial */

	for (i = 0; i < 256; i++) {
		v = i;
		for (j = 0; j < 8; j++) {
			v = (v >> 1) ^ ((v & 1) ? poly : 0);
		}
		crc32c_table[i] = v;
	}
}