liblzma: Improve documentation in index.h
[xz/debian.git] / src / liblzma / check / crc64_small.c
blob420f7cfbb475ade8a0b88ddb11cd687424e99224
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file crc64_small.c
4 /// \brief CRC64 calculation (size-optimized)
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "check.h"
16 static uint64_t crc64_table[256];
19 #ifdef HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR
20 __attribute__((__constructor__))
21 #endif
22 static void
23 crc64_init(void)
25 static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42);
27 for (size_t b = 0; b < 256; ++b) {
28 uint64_t r = b;
29 for (size_t i = 0; i < 8; ++i) {
30 if (r & 1)
31 r = (r >> 1) ^ poly64;
32 else
33 r >>= 1;
36 crc64_table[b] = r;
39 return;
43 extern LZMA_API(uint64_t)
44 lzma_crc64(const uint8_t *buf, size_t size, uint64_t crc)
46 #ifndef HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR
47 mythread_once(crc64_init);
48 #endif
50 crc = ~crc;
52 while (size != 0) {
53 crc = crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
54 --size;
57 return ~crc;