1 // SPDX-License-Identifier: 0BSD 2 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). 11 // 12 // Author: Lasse Collin 13 // 14 /////////////////////////////////////////////////////////////////////////////// 15 16 #include <stdio.h> 17 #include "../../common/tuklib_integer.h" 18 19 20 static uint32_t crc32_table[8][256]; 21 22 23 static void 24 init_crc32_table(void) 25 { 26 static const uint32_t poly32 = UINT32_C(0xEDB88320); 27 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]; 31 32 for (size_t i = 0; i < 8; ++i) { 33 if (r & 1) 34 r = (r >> 1) ^ poly32; 35 else 36 r >>= 1; 37 } 38 39 crc32_table[s][b] = r; 40 } 41 } 42 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 48 49 return; 50 } 51 52 53 static void 54 print_crc32_table(void) 55 { 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{"); 61 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"); 66 67 printf("0x%08" PRIX32, crc32_table[s][b]); 68 69 if (b != 255) 70 printf(",%s", (b+1) % 4 == 0 ? "" : " "); 71 } 72 73 if (s == 7) 74 printf("\n\t}\n};\n"); 75 else 76 printf("\n\t}, {"); 77 } 78 79 return; 80 } 81 82 83 static void 84 print_lz_table(void) 85 { 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] = {"); 91 92 for (size_t b = 0; b < 256; ++b) { 93 if ((b % 4) == 0) 94 printf("\n\t"); 95 96 printf("0x%08" PRIX32, crc32_table[0][b]); 97 98 if (b != 255) 99 printf(",%s", (b+1) % 4 == 0 ? "" : " "); 100 } 101 102 printf("\n};\n"); 103 104 return; 105 } 106 107 108 int 109 main(void) 110 { 111 init_crc32_table(); 112 113 #ifdef LZ_HASH_TABLE 114 print_lz_table(); 115 #else 116 print_crc32_table(); 117 #endif 118 119 return 0; 120 } 121