Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.0 KiB

  1. /* 7zCrc.c -- CRC32 init
  2. 2013-11-12 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include "7zCrc.h"
  5. #include "CpuArch.h"
  6. #define kCrcPoly 0xEDB88320
  7. #ifdef MY_CPU_X86_OR_AMD64
  8. #define CRC_NUM_TABLES 8
  9. UInt32 MY_FAST_CALL CrcUpdateT8(UInt32 v, const void *data, size_t size, const UInt32 *table);
  10. #elif defined(MY_CPU_LE)
  11. #define CRC_NUM_TABLES 4
  12. #else
  13. #define CRC_NUM_TABLES 5
  14. #define CRC_UINT32_SWAP(v) ((v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | (v << 24))
  15. UInt32 MY_FAST_CALL CrcUpdateT1_BeT4(UInt32 v, const void *data, size_t size, const UInt32 *table);
  16. #endif
  17. #ifndef MY_CPU_BE
  18. UInt32 MY_FAST_CALL CrcUpdateT4(UInt32 v, const void *data, size_t size, const UInt32 *table);
  19. #endif
  20. typedef UInt32 (MY_FAST_CALL *CRC_FUNC)(UInt32 v, const void *data, size_t size, const UInt32 *table);
  21. CRC_FUNC g_CrcUpdate;
  22. UInt32 g_CrcTable[256 * CRC_NUM_TABLES];
  23. UInt32 MY_FAST_CALL CrcUpdate(UInt32 v, const void *data, size_t size)
  24. {
  25. return g_CrcUpdate(v, data, size, g_CrcTable);
  26. }
  27. UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size)
  28. {
  29. return g_CrcUpdate(CRC_INIT_VAL, data, size, g_CrcTable) ^ CRC_INIT_VAL;
  30. }
  31. void MY_FAST_CALL CrcGenerateTable()
  32. {
  33. UInt32 i;
  34. for (i = 0; i < 256; i++)
  35. {
  36. UInt32 r = i;
  37. unsigned j;
  38. for (j = 0; j < 8; j++)
  39. r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
  40. g_CrcTable[i] = r;
  41. }
  42. for (; i < 256 * CRC_NUM_TABLES; i++)
  43. {
  44. UInt32 r = g_CrcTable[i - 256];
  45. g_CrcTable[i] = g_CrcTable[r & 0xFF] ^ (r >> 8);
  46. }
  47. #ifdef MY_CPU_LE
  48. g_CrcUpdate = CrcUpdateT4;
  49. #if CRC_NUM_TABLES == 8
  50. if (!CPU_Is_InOrder())
  51. g_CrcUpdate = CrcUpdateT8;
  52. #endif
  53. #else
  54. {
  55. #ifndef MY_CPU_BE
  56. UInt32 k = 1;
  57. if (*(const Byte *)&k == 1)
  58. g_CrcUpdate = CrcUpdateT4;
  59. else
  60. #endif
  61. {
  62. for (i = 256 * CRC_NUM_TABLES - 1; i >= 256; i--)
  63. {
  64. UInt32 x = g_CrcTable[i - 256];
  65. g_CrcTable[i] = CRC_UINT32_SWAP(x);
  66. }
  67. g_CrcUpdate = CrcUpdateT1_BeT4;
  68. }
  69. }
  70. #endif
  71. }