1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: expandtab:ts=8:sw=4:softtabstop=4:
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       crc64_small.c
6 /// \brief      CRC64 calculation (size-optimized)
7 //
8 //  Author:     Lasse Collin
9 //
10 //  This file has been put into the public domain.
11 //  You can do whatever you want with this file.
12 //
13 ///////////////////////////////////////////////////////////////////////////////
14 
15 #include "check.h"
16 
17 
18 static uint64_t crc64_table[256];
19 
20 
21 static void
crc64_init(void)22 crc64_init(void)
23 {
24 	static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42);
25 
26 	for (size_t b = 0; b < 256; ++b) {
27 		uint64_t r = b;
28 		for (size_t i = 0; i < 8; ++i) {
29 			if (r & 1)
30 				r = (r >> 1) ^ poly64;
31 			else
32 				r >>= 1;
33 		}
34 
35 		crc64_table[b] = r;
36 	}
37 
38 	return;
39 }
40 
41 
42 extern LZMA_API(uint64_t)
lzma_crc64(const uint8_t * buf,size_t size,uint64_t crc)43 lzma_crc64(const uint8_t *buf, size_t size, uint64_t crc)
44 {
45 	mythread_once(crc64_init);
46 
47 	crc = ~crc;
48 
49 	while (size != 0) {
50 		crc = crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
51 		--size;
52 	}
53 
54 	return ~crc;
55 }
56