1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       crc32_tablegen.c
4207753Smm/// \brief      Generate crc32_table_le.h and crc32_table_be.h
5207753Smm///
6207753Smm/// Compiling: gcc -std=c99 -o crc32_tablegen crc32_tablegen.c
7207753Smm/// Add -DWORDS_BIGENDIAN to generate big endian table.
8207753Smm/// Add -DLZ_HASH_TABLE to generate lz_encoder_hash_table.h (little endian).
9207753Smm//
10207753Smm//  Author:     Lasse Collin
11207753Smm//
12207753Smm//  This file has been put into the public domain.
13207753Smm//  You can do whatever you want with this file.
14207753Smm//
15207753Smm///////////////////////////////////////////////////////////////////////////////
16207753Smm
17207753Smm#include <stdio.h>
18207753Smm#include "../../common/tuklib_integer.h"
19207753Smm
20207753Smm
21207753Smmstatic uint32_t crc32_table[8][256];
22207753Smm
23207753Smm
24207753Smmstatic void
25207753Smminit_crc32_table(void)
26207753Smm{
27207753Smm	static const uint32_t poly32 = UINT32_C(0xEDB88320);
28207753Smm
29207753Smm	for (size_t s = 0; s < 8; ++s) {
30207753Smm		for (size_t b = 0; b < 256; ++b) {
31207753Smm			uint32_t r = s == 0 ? b : crc32_table[s - 1][b];
32207753Smm
33207753Smm			for (size_t i = 0; i < 8; ++i) {
34207753Smm				if (r & 1)
35207753Smm					r = (r >> 1) ^ poly32;
36207753Smm				else
37207753Smm					r >>= 1;
38207753Smm			}
39207753Smm
40207753Smm			crc32_table[s][b] = r;
41207753Smm		}
42207753Smm	}
43207753Smm
44207753Smm#ifdef WORDS_BIGENDIAN
45207753Smm	for (size_t s = 0; s < 8; ++s)
46207753Smm		for (size_t b = 0; b < 256; ++b)
47207753Smm			crc32_table[s][b] = bswap32(crc32_table[s][b]);
48207753Smm#endif
49207753Smm
50207753Smm	return;
51207753Smm}
52207753Smm
53207753Smm
54207753Smmstatic void
55207753Smmprint_crc32_table(void)
56207753Smm{
57207753Smm	printf("/* This file has been automatically generated by "
58207753Smm			"crc32_tablegen.c. */\n\n"
59207753Smm			"const uint32_t lzma_crc32_table[8][256] = {\n\t{");
60207753Smm
61207753Smm	for (size_t s = 0; s < 8; ++s) {
62207753Smm		for (size_t b = 0; b < 256; ++b) {
63207753Smm			if ((b % 4) == 0)
64207753Smm				printf("\n\t\t");
65207753Smm
66207753Smm			printf("0x%08" PRIX32, crc32_table[s][b]);
67207753Smm
68207753Smm			if (b != 255)
69207753Smm				printf(",%s", (b+1) % 4 == 0 ? "" : " ");
70207753Smm		}
71207753Smm
72207753Smm		if (s == 7)
73207753Smm			printf("\n\t}\n};\n");
74207753Smm		else
75207753Smm			printf("\n\t}, {");
76207753Smm	}
77207753Smm
78207753Smm	return;
79207753Smm}
80207753Smm
81207753Smm
82207753Smmstatic void
83207753Smmprint_lz_table(void)
84207753Smm{
85207753Smm	printf("/* This file has been automatically generated by "
86207753Smm			"crc32_tablegen.c. */\n\n"
87207753Smm			"const uint32_t lzma_lz_hash_table[256] = {");
88207753Smm
89207753Smm	for (size_t b = 0; b < 256; ++b) {
90207753Smm		if ((b % 4) == 0)
91207753Smm			printf("\n\t");
92207753Smm
93207753Smm		printf("0x%08" PRIX32, crc32_table[0][b]);
94207753Smm
95207753Smm		if (b != 255)
96207753Smm			printf(",%s", (b+1) % 4 == 0 ? "" : " ");
97207753Smm	}
98207753Smm
99207753Smm	printf("\n};\n");
100207753Smm
101207753Smm	return;
102207753Smm}
103207753Smm
104207753Smm
105207753Smmint
106207753Smmmain(void)
107207753Smm{
108207753Smm	init_crc32_table();
109207753Smm
110207753Smm#ifdef LZ_HASH_TABLE
111207753Smm	print_lz_table();
112207753Smm#else
113207753Smm	print_crc32_table();
114207753Smm#endif
115207753Smm
116207753Smm	return 0;
117207753Smm}
118