xref: /aosp_15_r20/external/xz-embedded/linux/lib/xz/xz_crc64.c (revision d2c16535d139cb185e89120452531bba6b36d3c6)
1 // SPDX-License-Identifier: 0BSD
2 
3 /*
4  * CRC64 using the polynomial from ECMA-182
5  *
6  * This file is similar to xz_crc32.c. See the comments there.
7  *
8  * Authors: Lasse Collin <[email protected]>
9  *          Igor Pavlov <https://7-zip.org/>
10  */
11 
12 #include "xz_private.h"
13 
14 #ifndef STATIC_RW_DATA
15 #	define STATIC_RW_DATA static
16 #endif
17 
18 STATIC_RW_DATA uint64_t xz_crc64_table[256];
19 
xz_crc64_init(void)20 XZ_EXTERN void xz_crc64_init(void)
21 {
22 	/*
23 	 * The ULL suffix is needed for -std=gnu89 compatibility
24 	 * on 32-bit platforms.
25 	 */
26 	const uint64_t poly = 0xC96C5795D7870F42ULL;
27 
28 	uint32_t i;
29 	uint32_t j;
30 	uint64_t r;
31 
32 	for (i = 0; i < 256; ++i) {
33 		r = i;
34 		for (j = 0; j < 8; ++j)
35 			r = (r >> 1) ^ (poly & ~((r & 1) - 1));
36 
37 		xz_crc64_table[i] = r;
38 	}
39 
40 	return;
41 }
42 
xz_crc64(const uint8_t * buf,size_t size,uint64_t crc)43 XZ_EXTERN uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc)
44 {
45 	crc = ~crc;
46 
47 	while (size != 0) {
48 		crc = xz_crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
49 		--size;
50 	}
51 
52 	return ~crc;
53 }
54