7zCrc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* 7zCrc.c -- CRC32 init
  2. 2017-06-06 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include "7zCrc.h"
  5. #include "CpuArch.h"
  6. #define kCrcPoly 0xEDB88320
  7. #ifdef MY_CPU_LE
  8. #define CRC_NUM_TABLES 8
  9. #else
  10. #define CRC_NUM_TABLES 9
  11. #define CRC_UINT32_SWAP(v) ((v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | (v << 24))
  12. UInt32 MY_FAST_CALL CrcUpdateT1_BeT4(UInt32 v, const void *data, size_t size, const UInt32 *table);
  13. UInt32 MY_FAST_CALL CrcUpdateT1_BeT8(UInt32 v, const void *data, size_t size, const UInt32 *table);
  14. #endif
  15. #ifndef MY_CPU_BE
  16. UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table);
  17. UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table);
  18. #endif
  19. typedef UInt32 (MY_FAST_CALL *CRC_FUNC)(UInt32 v, const void *data, size_t size, const UInt32 *table);
  20. CRC_FUNC g_CrcUpdateT4;
  21. CRC_FUNC g_CrcUpdateT8;
  22. CRC_FUNC g_CrcUpdate;
  23. UInt32 g_CrcTable[256 * CRC_NUM_TABLES];
  24. UInt32 MY_FAST_CALL CrcUpdate(UInt32 v, const void *data, size_t size)
  25. {
  26. return g_CrcUpdate(v, data, size, g_CrcTable);
  27. }
  28. UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size)
  29. {
  30. return g_CrcUpdate(CRC_INIT_VAL, data, size, g_CrcTable) ^ CRC_INIT_VAL;
  31. }
  32. #define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
  33. UInt32 MY_FAST_CALL CrcUpdateT1(UInt32 v, const void *data, size_t size, const UInt32 *table)
  34. {
  35. const Byte *p = (const Byte *)data;
  36. const Byte *pEnd = p + size;
  37. for (; p != pEnd; p++)
  38. v = CRC_UPDATE_BYTE_2(v, *p);
  39. return v;
  40. }
  41. void MY_FAST_CALL CrcGenerateTable()
  42. {
  43. UInt32 i;
  44. for (i = 0; i < 256; i++)
  45. {
  46. UInt32 r = i;
  47. unsigned j;
  48. for (j = 0; j < 8; j++)
  49. r = (r >> 1) ^ (kCrcPoly & ((UInt32)0 - (r & 1)));
  50. g_CrcTable[i] = r;
  51. }
  52. for (i = 256; i < 256 * CRC_NUM_TABLES; i++)
  53. {
  54. UInt32 r = g_CrcTable[(size_t)i - 256];
  55. g_CrcTable[i] = g_CrcTable[r & 0xFF] ^ (r >> 8);
  56. }
  57. #if CRC_NUM_TABLES < 4
  58. g_CrcUpdate = CrcUpdateT1;
  59. #else
  60. #ifdef MY_CPU_LE
  61. g_CrcUpdateT4 = CrcUpdateT4;
  62. g_CrcUpdate = CrcUpdateT4;
  63. #if CRC_NUM_TABLES >= 8
  64. g_CrcUpdateT8 = CrcUpdateT8;
  65. #ifdef MY_CPU_X86_OR_AMD64
  66. if (!CPU_Is_InOrder())
  67. #endif
  68. g_CrcUpdate = CrcUpdateT8;
  69. #endif
  70. #else
  71. {
  72. #ifndef MY_CPU_BE
  73. UInt32 k = 0x01020304;
  74. const Byte *p = (const Byte *)&k;
  75. if (p[0] == 4 && p[1] == 3)
  76. {
  77. g_CrcUpdateT4 = CrcUpdateT4;
  78. g_CrcUpdate = CrcUpdateT4;
  79. #if CRC_NUM_TABLES >= 8
  80. g_CrcUpdateT8 = CrcUpdateT8;
  81. g_CrcUpdate = CrcUpdateT8;
  82. #endif
  83. }
  84. else if (p[0] != 1 || p[1] != 2)
  85. g_CrcUpdate = CrcUpdateT1;
  86. else
  87. #endif
  88. {
  89. for (i = 256 * CRC_NUM_TABLES - 1; i >= 256; i--)
  90. {
  91. UInt32 x = g_CrcTable[(size_t)i - 256];
  92. g_CrcTable[i] = CRC_UINT32_SWAP(x);
  93. }
  94. g_CrcUpdateT4 = CrcUpdateT1_BeT4;
  95. g_CrcUpdate = CrcUpdateT1_BeT4;
  96. #if CRC_NUM_TABLES >= 8
  97. g_CrcUpdateT8 = CrcUpdateT1_BeT8;
  98. g_CrcUpdate = CrcUpdateT1_BeT8;
  99. #endif
  100. }
  101. }
  102. #endif
  103. #endif
  104. }