]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/crc32_tablegen.c
Make liblzma produce the same output on both endiannesses.
[icculus/xz.git] / src / liblzma / check / crc32_tablegen.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       crc32_tablegen.c
4 /// \brief      Generate crc32_table_le.h and crc32_table_be.h
5 ///
6 /// Compiling: gcc -std=c99 -o crc32_tablegen crc32_tablegen.c
7 /// Add -DWORDS_BIGENDIAN to generate big endian table.
8 /// Add -DLZ_HASH_TABLE to generate lz_encoder_hash_table.h (little endian).
9 //
10 //  Author:     Lasse Collin
11 //
12 //  This file has been put into the public domain.
13 //  You can do whatever you want with this file.
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 uint32_t crc32_table[8][256];
26
27
28 static void
29 init_crc32_table(void)
30 {
31         static const uint32_t poly32 = UINT32_C(0xEDB88320);
32
33         for (size_t s = 0; s < 8; ++s) {
34                 for (size_t b = 0; b < 256; ++b) {
35                         uint32_t r = s == 0 ? b : crc32_table[s - 1][b];
36
37                         for (size_t i = 0; i < 8; ++i) {
38                                 if (r & 1)
39                                         r = (r >> 1) ^ poly32;
40                                 else
41                                         r >>= 1;
42                         }
43
44                         crc32_table[s][b] = r;
45                 }
46         }
47
48 #ifdef WORDS_BIGENDIAN
49         for (size_t s = 0; s < 8; ++s)
50                 for (size_t b = 0; b < 256; ++b)
51                         crc32_table[s][b] = bswap_32(crc32_table[s][b]);
52 #endif
53
54         return;
55 }
56
57
58 static void
59 print_crc32_table(void)
60 {
61         printf("/* This file has been automatically generated by "
62                         "crc32_tablegen.c. */\n\n"
63                         "const uint32_t lzma_crc32_table[8][256] = {\n\t{");
64
65         for (size_t s = 0; s < 8; ++s) {
66                 for (size_t b = 0; b < 256; ++b) {
67                         if ((b % 4) == 0)
68                                 printf("\n\t\t");
69
70                         printf("0x%08" PRIX32, crc32_table[s][b]);
71
72                         if (b != 255)
73                                 printf(",%s", (b+1) % 4 == 0 ? "" : " ");
74                 }
75
76                 if (s == 7)
77                         printf("\n\t}\n};\n");
78                 else
79                         printf("\n\t}, {");
80         }
81
82         return;
83 }
84
85
86 static void
87 print_lz_table(void)
88 {
89         printf("/* This file has been automatically generated by "
90                         "crc32_tablegen.c. */\n\n"
91                         "const uint32_t lzma_lz_hash_table[256] = {");
92
93         for (size_t b = 0; b < 256; ++b) {
94                 if ((b % 4) == 0)
95                         printf("\n\t");
96
97                 printf("0x%08" PRIX32, crc32_table[0][b]);
98
99                 if (b != 255)
100                         printf(",%s", (b+1) % 4 == 0 ? "" : " ");
101         }
102
103         printf("\n};\n");
104
105         return;
106 }
107
108
109 int
110 main(void)
111 {
112         init_crc32_table();
113
114 #ifdef LZ_HASH_TABLE
115         print_lz_table();
116 #else
117         print_crc32_table();
118 #endif
119
120         return 0;
121 }