crc32_tablegen.c 2.2 KB

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