]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/crc64_init.c
Imported to git.
[icculus/xz.git] / src / liblzma / check / crc64_init.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       crc64_init.c
4 /// \brief      CRC64 table initialization
5 //
6 //  This code is based on various public domain sources.
7 //  This code has been put into the public domain.
8 //
9 //  This library is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 //
13 ///////////////////////////////////////////////////////////////////////////////
14
15 #ifdef HAVE_CONFIG_H
16 #       include <config.h>
17 #endif
18
19 #include <sys/types.h>
20 #include <inttypes.h>
21
22 #ifdef WORDS_BIGENDIAN
23 #       include "check_byteswap.h"
24 #endif
25
26
27 uint64_t lzma_crc64_table[4][256];
28
29
30 extern void
31 lzma_crc64_init(void)
32 {
33         static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42);
34
35         for (size_t s = 0; s < 4; ++s) {
36                 for (size_t b = 0; b < 256; ++b) {
37                         uint64_t r = s == 0 ? b : lzma_crc64_table[s - 1][b];
38
39                         for (size_t i = 0; i < 8; ++i) {
40                                 if (r & 1)
41                                         r = (r >> 1) ^ poly64;
42                                 else
43                                         r >>= 1;
44                         }
45
46                         lzma_crc64_table[s][b] = r;
47                 }
48         }
49
50 #ifdef WORDS_BIGENDIAN
51         for (size_t s = 0; s < 4; ++s)
52                 for (size_t b = 0; b < 256; ++b)
53                         lzma_crc64_table[s][b]
54                                         = bswap_64(lzma_crc64_table[s][b]);
55 #endif
56
57         return;
58 }