liblzma: Fix a typo in a comment
[xz/debian.git] / src / liblzma / check / crc32_tablegen.c
blobb8cf459f8e76693dd63c4a46756a046d2e40e209
1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file crc32_tablegen.c
6 /// \brief Generate crc32_table_le.h and crc32_table_be.h
7 ///
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 ///////////////////////////////////////////////////////////////////////////////
16 #include <stdio.h>
17 #include "../../common/tuklib_integer.h"
20 static uint32_t crc32_table[8][256];
23 static void
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) {
33 if (r & 1)
34 r = (r >> 1) ^ poly32;
35 else
36 r >>= 1;
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]);
47 #endif
49 return;
53 static void
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) {
64 if ((b % 4) == 0)
65 printf("\n\t\t");
67 printf("0x%08" PRIX32, crc32_table[s][b]);
69 if (b != 255)
70 printf(",%s", (b+1) % 4 == 0 ? "" : " ");
73 if (s == 7)
74 printf("\n\t}\n};\n");
75 else
76 printf("\n\t}, {");
79 return;
83 static void
84 print_lz_table(void)
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) {
93 if ((b % 4) == 0)
94 printf("\n\t");
96 printf("0x%08" PRIX32, crc32_table[0][b]);
98 if (b != 255)
99 printf(",%s", (b+1) % 4 == 0 ? "" : " ");
102 printf("\n};\n");
104 return;
109 main(void)
111 init_crc32_table();
113 #ifdef LZ_HASH_TABLE
114 print_lz_table();
115 #else
116 print_crc32_table();
117 #endif
119 return 0;