]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/crc64_small.c
Remove lzma_init() and other init functions from liblzma API.
[icculus/xz.git] / src / liblzma / check / crc64_small.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       crc64_small.c
4 /// \brief      CRC64 calculation (size-optimized)
5 //
6 //  This code has been put into the public domain.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 //
12 ///////////////////////////////////////////////////////////////////////////////
13
14 #include "check.h"
15
16
17 static uint64_t crc64_table[256];
18
19
20 static void
21 crc64_init(void)
22 {
23         static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42);
24
25         for (size_t b = 0; b < 256; ++b) {
26                 uint64_t r = b;
27                 for (size_t i = 0; i < 8; ++i) {
28                         if (r & 1)
29                                 r = (r >> 1) ^ poly64;
30                         else
31                                 r >>= 1;
32                 }
33
34                 crc64_table[b] = r;
35         }
36
37         return;
38 }
39
40
41 extern LZMA_API uint64_t
42 lzma_crc64(const uint8_t *buf, size_t size, uint64_t crc)
43 {
44         mythread_once(crc64_init);
45
46         crc = ~crc;
47
48         while (size != 0) {
49                 crc = crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
50                 --size;
51         }
52
53         return ~crc;
54 }