]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/crc32_tablegen.c
Put the interesting parts of XZ Utils into the public domain.
[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 //  Author:     Lasse Collin
10 //
11 //  This file has been put into the public domain.
12 //  You can do whatever you want with this file.
13 //
14 ///////////////////////////////////////////////////////////////////////////////
15
16 #include <inttypes.h>
17 #include <stdio.h>
18
19 #ifdef WORDS_BIGENDIAN
20 #       include "../../common/bswap.h"
21 #endif
22
23
24 static uint32_t crc32_table[8][256];
25
26
27 static void
28 init_crc32_table(void)
29 {
30         static const uint32_t poly32 = UINT32_C(0xEDB88320);
31
32         for (size_t s = 0; s < 8; ++s) {
33                 for (size_t b = 0; b < 256; ++b) {
34                         uint32_t r = s == 0 ? b : crc32_table[s - 1][b];
35
36                         for (size_t i = 0; i < 8; ++i) {
37                                 if (r & 1)
38                                         r = (r >> 1) ^ poly32;
39                                 else
40                                         r >>= 1;
41                         }
42
43                         crc32_table[s][b] = r;
44                 }
45         }
46
47 #ifdef WORDS_BIGENDIAN
48         for (size_t s = 0; s < 8; ++s)
49                 for (size_t b = 0; b < 256; ++b)
50                         crc32_table[s][b] = bswap_32(crc32_table[s][b]);
51 #endif
52
53         return;
54 }
55
56
57 static void
58 print_crc32_table(void)
59 {
60         printf("/* This file has been automatically generated by "
61                         "crc32_tablegen.c. */\n\n"
62                         "const uint32_t lzma_crc32_table[8][256] = {\n\t{");
63
64         for (size_t s = 0; s < 8; ++s) {
65                 for (size_t b = 0; b < 256; ++b) {
66                         if ((b % 4) == 0)
67                                 printf("\n\t\t");
68
69                         printf("0x%08" PRIX32, crc32_table[s][b]);
70
71                         if (b != 255)
72                                 printf(",%s", (b+1) % 4 == 0 ? "" : " ");
73                 }
74
75                 if (s == 7)
76                         printf("\n\t}\n};\n");
77                 else
78                         printf("\n\t}, {");
79         }
80
81         return;
82 }
83
84
85 int
86 main(void)
87 {
88         init_crc32_table();
89         print_crc32_table();
90         return 0;
91 }