1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       crc64_small.c
4207753Smm/// \brief      CRC64 calculation (size-optimized)
5207753Smm//
6207753Smm//  Author:     Lasse Collin
7207753Smm//
8207753Smm//  This file has been put into the public domain.
9207753Smm//  You can do whatever you want with this file.
10207753Smm//
11207753Smm///////////////////////////////////////////////////////////////////////////////
12207753Smm
13207753Smm#include "check.h"
14207753Smm
15207753Smm
16207753Smmstatic uint64_t crc64_table[256];
17207753Smm
18207753Smm
19207753Smmstatic void
20207753Smmcrc64_init(void)
21207753Smm{
22207753Smm	static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42);
23207753Smm
24207753Smm	for (size_t b = 0; b < 256; ++b) {
25207753Smm		uint64_t r = b;
26207753Smm		for (size_t i = 0; i < 8; ++i) {
27207753Smm			if (r & 1)
28207753Smm				r = (r >> 1) ^ poly64;
29207753Smm			else
30207753Smm				r >>= 1;
31207753Smm		}
32207753Smm
33207753Smm		crc64_table[b] = r;
34207753Smm	}
35207753Smm
36207753Smm	return;
37207753Smm}
38207753Smm
39207753Smm
40207753Smmextern LZMA_API(uint64_t)
41207753Smmlzma_crc64(const uint8_t *buf, size_t size, uint64_t crc)
42207753Smm{
43207753Smm	mythread_once(crc64_init);
44207753Smm
45207753Smm	crc = ~crc;
46207753Smm
47207753Smm	while (size != 0) {
48207753Smm		crc = crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
49207753Smm		--size;
50207753Smm	}
51207753Smm
52207753Smm	return ~crc;
53207753Smm}
54