xref: /freebsd/sys/contrib/xz-embedded/linux/lib/xz/xz_crc64.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /*
2  * CRC64 using the polynomial from ECMA-182
3  *
4  * This file is similar to xz_crc32.c. See the comments there.
5  *
6  * Authors: Lasse Collin <lasse.collin@tukaani.org>
7  *          Igor Pavlov <http://7-zip.org/>
8  *
9  * This file has been put into the public domain.
10  * You can do whatever you want with this file.
11  */
12 
13 #include "xz_private.h"
14 
15 #ifndef STATIC_RW_DATA
16 #	define STATIC_RW_DATA static
17 #endif
18 
19 STATIC_RW_DATA uint64_t xz_crc64_table[256];
20 
21 XZ_EXTERN void xz_crc64_init(void)
22 {
23 	const uint64_t poly = 0xC96C5795D7870F42;
24 
25 	uint32_t i;
26 	uint32_t j;
27 	uint64_t r;
28 
29 	for (i = 0; i < 256; ++i) {
30 		r = i;
31 		for (j = 0; j < 8; ++j)
32 			r = (r >> 1) ^ (poly & ~((r & 1) - 1));
33 
34 		xz_crc64_table[i] = r;
35 	}
36 
37 	return;
38 }
39 
40 XZ_EXTERN uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc)
41 {
42 	crc = ~crc;
43 
44 	while (size != 0) {
45 		crc = xz_crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
46 		--size;
47 	}
48 
49 	return ~crc;
50 }
51