]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/crc64_tablegen.c
Put the interesting parts of XZ Utils into the public domain.
[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 //  Author:     Lasse Collin
10 //
11 //  This file has been put into the public domain.
12 //  You can do whatever you want with this file.
13 //
14 ///////////////////////////////////////////////////////////////////////////////
15
16 #include <inttypes.h>
17 #include <stdio.h>
18
19 #ifdef WORDS_BIGENDIAN
20 #       include "../../common/bswap.h"
21 #endif
22
23
24 static uint64_t crc64_table[4][256];
25
26
27 extern void
28 init_crc64_table(void)
29 {
30         static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42);
31
32         for (size_t s = 0; s < 4; ++s) {
33                 for (size_t b = 0; b < 256; ++b) {
34                         uint64_t r = s == 0 ? b : crc64_table[s - 1][b];
35
36                         for (size_t i = 0; i < 8; ++i) {
37                                 if (r & 1)
38                                         r = (r >> 1) ^ poly64;
39                                 else
40                                         r >>= 1;
41                         }
42
43                         crc64_table[s][b] = r;
44                 }
45         }
46
47 #ifdef WORDS_BIGENDIAN
48         for (size_t s = 0; s < 4; ++s)
49                 for (size_t b = 0; b < 256; ++b)
50                         crc64_table[s][b] = bswap_64(crc64_table[s][b]);
51 #endif
52
53         return;
54 }
55
56
57 static void
58 print_crc64_table(void)
59 {
60         printf("/* This file has been automatically generated by "
61                         "crc64_tablegen.c. */\n\n"
62                         "const uint64_t lzma_crc64_table[4][256] = {\n\t{");
63
64         for (size_t s = 0; s < 4; ++s) {
65                 for (size_t b = 0; b < 256; ++b) {
66                         if ((b % 2) == 0)
67                                 printf("\n\t\t");
68
69                         printf("UINT64_C(0x%016" PRIX64 ")",
70                                         crc64_table[s][b]);
71
72                         if (b != 255)
73                                 printf(",%s", (b+1) % 2 == 0 ? "" : " ");
74                 }
75
76                 if (s == 3)
77                         printf("\n\t}\n};\n");
78                 else
79                         printf("\n\t}, {");
80         }
81
82         return;
83 }
84
85
86 int
87 main(void)
88 {
89         init_crc64_table();
90         print_crc64_table();
91         return 0;
92 }