Path: blob/master/Utilities/cmliblzma/liblzma/check/crc32_tablegen.c
3153 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file crc32_tablegen.c5/// \brief Generate crc32_table_le.h and crc32_table_be.h6///7/// Compiling: gcc -std=c99 -o crc32_tablegen crc32_tablegen.c8/// Add -DWORDS_BIGENDIAN to generate big endian table.9/// Add -DLZ_HASH_TABLE to generate lz_encoder_hash_table.h (little endian).10//11// Author: Lasse Collin12//13///////////////////////////////////////////////////////////////////////////////1415#include <stdio.h>16#include "../../common/tuklib_integer.h"171819static uint32_t crc32_table[8][256];202122static void23init_crc32_table(void)24{25static const uint32_t poly32 = UINT32_C(0xEDB88320);2627for (size_t s = 0; s < 8; ++s) {28for (size_t b = 0; b < 256; ++b) {29uint32_t r = s == 0 ? b : crc32_table[s - 1][b];3031for (size_t i = 0; i < 8; ++i) {32if (r & 1)33r = (r >> 1) ^ poly32;34else35r >>= 1;36}3738crc32_table[s][b] = r;39}40}4142#ifdef WORDS_BIGENDIAN43for (size_t s = 0; s < 8; ++s)44for (size_t b = 0; b < 256; ++b)45crc32_table[s][b] = byteswap32(crc32_table[s][b]);46#endif4748return;49}505152static void53print_crc32_table(void)54{55// Split the SPDX string so that it won't accidentally match56// when tools search for the string.57printf("// SPDX" "-License-Identifier" ": 0BSD\n\n"58"// This file has been generated by crc32_tablegen.c.\n\n"59"const uint32_t lzma_crc32_table[8][256] = {\n\t{");6061for (size_t s = 0; s < 8; ++s) {62for (size_t b = 0; b < 256; ++b) {63if ((b % 4) == 0)64printf("\n\t\t");6566printf("0x%08" PRIX32, crc32_table[s][b]);6768if (b != 255)69printf(",%s", (b+1) % 4 == 0 ? "" : " ");70}7172if (s == 7)73printf("\n\t}\n};\n");74else75printf("\n\t}, {");76}7778return;79}808182static void83print_lz_table(void)84{85// Split the SPDX string so that it won't accidentally match86// when tools search for the string.87printf("// SPDX" "-License-Identifier" ": 0BSD\n\n"88"// This file has been generated by crc32_tablegen.c.\n\n"89"const uint32_t lzma_lz_hash_table[256] = {");9091for (size_t b = 0; b < 256; ++b) {92if ((b % 4) == 0)93printf("\n\t");9495printf("0x%08" PRIX32, crc32_table[0][b]);9697if (b != 255)98printf(",%s", (b+1) % 4 == 0 ? "" : " ");99}100101printf("\n};\n");102103return;104}105106107int108main(void)109{110init_crc32_table();111112#ifdef LZ_HASH_TABLE113print_lz_table();114#else115print_crc32_table();116#endif117118return 0;119}120121122