Crc64.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Crc64.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. //#include "stdafx.h"
  4. #include "Crc64.h"
  5. #ifdef OLDCRC
  6. #define POLY64REV 0xd800000000000000ULL
  7. #define INITIALCRC 0x0000000000000000ULL
  8. #else
  9. #define POLY64REV 0x95AC9329AC4BC9B5ULL
  10. #define INITIALCRC 0xFFFFFFFFFFFFFFFFULL
  11. #define POLY32REV 0xEDB88320
  12. #define INITIALCRC32 (0xFFFFFFFF)
  13. #endif
  14. int crc32_table[256];
  15. unsigned long long CRCTable[256] = { 0 };
  16. // 这是已导出类的构造函数。
  17. // 有关类定义的信息,请参阅 Crc64.h
  18. CCrc64::CCrc64()
  19. {
  20. int i, j;
  21. unsigned long long part;
  22. for (i = 0; i < 256; i++)
  23. {
  24. part = i;
  25. for (j = 0; j < 8; j++)
  26. {
  27. if (part & 1)
  28. part = (part >> 1) ^ POLY64REV;
  29. else
  30. part >>= 1;
  31. }
  32. CRCTable[i] = part;
  33. }
  34. return;
  35. }
  36. CCrc32::CCrc32()
  37. {
  38. int c;
  39. int i = 0;
  40. int bit = 0;
  41. for (i = 0; i < 256; i++)
  42. {
  43. c = (int)i;
  44. for (bit = 0; bit < 8; bit++)
  45. {
  46. if (c & 1)
  47. {
  48. c = (c >> 1) ^ (POLY32REV);
  49. }
  50. else
  51. {
  52. c = c >> 1;
  53. }
  54. }
  55. crc32_table[i] = c;
  56. }
  57. }
  58. CCrc64 g_C_C_r_c_64;//useless .
  59. CCrc32 g_C_C_r_c_32;//useless .
  60. CRC64_API int GetCrc32(const char *pSrc, size_t size)
  61. {
  62. int crc = INITIALCRC32;
  63. while (size--)
  64. crc = (crc >> 8) ^ (crc32_table[(crc ^ *pSrc++) & 0xff]);
  65. return crc;
  66. }
  67. CRC64_API UINT64 GetCrc64(const char *pSrc, DWORD size)
  68. {
  69. unsigned long long crc = INITIALCRC;
  70. for (DWORD i = 0; i < size; i++)
  71. {
  72. crc = CRCTable[(crc ^ pSrc[i]) & 0xff] ^ (crc >> 8);
  73. }
  74. return crc;
  75. }