1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file crc32_tablegen.c
6 /// \brief Generate crc32_table_le.h and crc32_table_be.h
8 /// Compiling: gcc -std=c99 -o crc32_tablegen crc32_tablegen.c
9 /// Add -DWORDS_BIGENDIAN to generate big endian table.
10 /// Add -DLZ_HASH_TABLE to generate lz_encoder_hash_table.h (little endian).
12 // Author: Lasse Collin
14 ///////////////////////////////////////////////////////////////////////////////
17 #include "../../common/tuklib_integer.h"
20 static uint32_t crc32_table
[8][256];
24 init_crc32_table(void)
26 static const uint32_t poly32
= UINT32_C(0xEDB88320);
28 for (size_t s
= 0; s
< 8; ++s
) {
29 for (size_t b
= 0; b
< 256; ++b
) {
30 uint32_t r
= s
== 0 ? b
: crc32_table
[s
- 1][b
];
32 for (size_t i
= 0; i
< 8; ++i
) {
34 r
= (r
>> 1) ^ poly32
;
39 crc32_table
[s
][b
] = r
;
43 #ifdef WORDS_BIGENDIAN
44 for (size_t s
= 0; s
< 8; ++s
)
45 for (size_t b
= 0; b
< 256; ++b
)
46 crc32_table
[s
][b
] = byteswap32(crc32_table
[s
][b
]);
54 print_crc32_table(void)
56 // Split the SPDX string so that it won't accidentally match
57 // when tools search for the string.
58 printf("// SPDX" "-License-Identifier" ": 0BSD\n\n"
59 "// This file has been generated by crc32_tablegen.c.\n\n"
60 "const uint32_t lzma_crc32_table[8][256] = {\n\t{");
62 for (size_t s
= 0; s
< 8; ++s
) {
63 for (size_t b
= 0; b
< 256; ++b
) {
67 printf("0x%08" PRIX32
, crc32_table
[s
][b
]);
70 printf(",%s", (b
+1) % 4 == 0 ? "" : " ");
74 printf("\n\t}\n};\n");
86 // Split the SPDX string so that it won't accidentally match
87 // when tools search for the string.
88 printf("// SPDX" "-License-Identifier" ": 0BSD\n\n"
89 "// This file has been generated by crc32_tablegen.c.\n\n"
90 "const uint32_t lzma_lz_hash_table[256] = {");
92 for (size_t b
= 0; b
< 256; ++b
) {
96 printf("0x%08" PRIX32
, crc32_table
[0][b
]);
99 printf(",%s", (b
+1) % 4 == 0 ? "" : " ");