]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/crc32_tablegen.c
Remove lzma_init() and other init functions from liblzma API.
[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 //
9 //  This code has been put into the public domain.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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(", ");
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 int
87 main(void)
88 {
89         init_crc32_table();
90         print_crc32_table();
91         return 0;
92 }