Tests: Fix memory leaks in test_block_header.
[xz/debian.git] / src / liblzma / check / crc32_tablegen.c
blob31a4d2751db2974d812dac730edd9472f56f54d8
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
12 // This file has been put into the public domain.
13 // You can do whatever you want with this file.
15 ///////////////////////////////////////////////////////////////////////////////
17 #include <stdio.h>
18 #include "../../common/tuklib_integer.h"
21 static uint32_t crc32_table[8][256];
24 static void
25 init_crc32_table(void)
27 static const uint32_t poly32 = UINT32_C(0xEDB88320);
29 for (size_t s = 0; s < 8; ++s) {
30 for (size_t b = 0; b < 256; ++b) {
31 uint32_t r = s == 0 ? b : crc32_table[s - 1][b];
33 for (size_t i = 0; i < 8; ++i) {
34 if (r & 1)
35 r = (r >> 1) ^ poly32;
36 else
37 r >>= 1;
40 crc32_table[s][b] = r;
44 #ifdef WORDS_BIGENDIAN
45 for (size_t s = 0; s < 8; ++s)
46 for (size_t b = 0; b < 256; ++b)
47 crc32_table[s][b] = bswap32(crc32_table[s][b]);
48 #endif
50 return;
54 static void
55 print_crc32_table(void)
57 printf("/* This file has been automatically generated by "
58 "crc32_tablegen.c. */\n\n"
59 "const uint32_t lzma_crc32_table[8][256] = {\n\t{");
61 for (size_t s = 0; s < 8; ++s) {
62 for (size_t b = 0; b < 256; ++b) {
63 if ((b % 4) == 0)
64 printf("\n\t\t");
66 printf("0x%08" PRIX32, crc32_table[s][b]);
68 if (b != 255)
69 printf(",%s", (b+1) % 4 == 0 ? "" : " ");
72 if (s == 7)
73 printf("\n\t}\n};\n");
74 else
75 printf("\n\t}, {");
78 return;
82 static void
83 print_lz_table(void)
85 printf("/* This file has been automatically generated by "
86 "crc32_tablegen.c. */\n\n"
87 "const uint32_t lzma_lz_hash_table[256] = {");
89 for (size_t b = 0; b < 256; ++b) {
90 if ((b % 4) == 0)
91 printf("\n\t");
93 printf("0x%08" PRIX32, crc32_table[0][b]);
95 if (b != 255)
96 printf(",%s", (b+1) % 4 == 0 ? "" : " ");
99 printf("\n};\n");
101 return;
106 main(void)
108 init_crc32_table();
110 #ifdef LZ_HASH_TABLE
111 print_lz_table();
112 #else
113 print_crc32_table();
114 #endif
116 return 0;