]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/crc64_tablegen.c
remove trailing blanks from all but .xz files
[icculus/xz.git] / src / liblzma / check / crc64_tablegen.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       crc64_tablegen.c
4 /// \brief      Generate crc64_table_le.h and crc64_table_be.h
5 ///
6 /// Compiling: gcc -std=c99 -o crc64_tablegen crc64_tablegen.c
7 /// Add -DWORDS_BIGENDIAN to generate big endian table.
8 //
9 //  This code has been put into the public domain.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #include <inttypes.h>
18 #include <stdio.h>
19
20 #ifdef WORDS_BIGENDIAN
21 #       include "../../common/bswap.h"
22 #endif
23
24
25 static uint64_t crc64_table[4][256];
26
27
28 extern void
29 init_crc64_table(void)
30 {
31         static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42);
32
33         for (size_t s = 0; s < 4; ++s) {
34                 for (size_t b = 0; b < 256; ++b) {
35                         uint64_t r = s == 0 ? b : crc64_table[s - 1][b];
36
37                         for (size_t i = 0; i < 8; ++i) {
38                                 if (r & 1)
39                                         r = (r >> 1) ^ poly64;
40                                 else
41                                         r >>= 1;
42                         }
43
44                         crc64_table[s][b] = r;
45                 }
46         }
47
48 #ifdef WORDS_BIGENDIAN
49         for (size_t s = 0; s < 4; ++s)
50                 for (size_t b = 0; b < 256; ++b)
51                         crc64_table[s][b] = bswap_64(crc64_table[s][b]);
52 #endif
53
54         return;
55 }
56
57
58 static void
59 print_crc64_table(void)
60 {
61         printf("/* This file has been automatically generated by "
62                         "crc64_tablegen.c. */\n\n"
63                         "const uint64_t lzma_crc64_table[4][256] = {\n\t{");
64
65         for (size_t s = 0; s < 4; ++s) {
66                 for (size_t b = 0; b < 256; ++b) {
67                         if ((b % 2) == 0)
68                                 printf("\n\t\t");
69
70                         printf("UINT64_C(0x%016" PRIX64 ")",
71                                         crc64_table[s][b]);
72
73                         if (b != 255)
74                                 printf(",%s", (b+1) % 2 == 0 ? "" : " ");
75                 }
76
77                 if (s == 3)
78                         printf("\n\t}\n};\n");
79                 else
80                         printf("\n\t}, {");
81         }
82
83         return;
84 }
85
86
87 int
88 main(void)
89 {
90         init_crc64_table();
91         print_crc64_table();
92         return 0;
93 }