xref: /freebsd/sys/contrib/xz-embedded/linux/lib/xz/xz_crc64.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 <https://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 	/*
24 	 * The ULL suffix is needed for -std=gnu89 compatibility
25 	 * on 32-bit platforms.
26 	 */
27 	const uint64_t poly = 0xC96C5795D7870F42ULL;
28 
29 	uint32_t i;
30 	uint32_t j;
31 	uint64_t r;
32 
33 	for (i = 0; i < 256; ++i) {
34 		r = i;
35 		for (j = 0; j < 8; ++j)
36 			r = (r >> 1) ^ (poly & ~((r & 1) - 1));
37 
38 		xz_crc64_table[i] = r;
39 	}
40 
41 	return;
42 }
43 
44 XZ_EXTERN uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc)
45 {
46 	crc = ~crc;
47 
48 	while (size != 0) {
49 		crc = xz_crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
50 		--size;
51 	}
52 
53 	return ~crc;
54 }
55