Source code of Windows XP (NT5)
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.

158 lines
3.1 KiB

  1. //***************************************************************************
  2. //
  3. // (c) 1999-2001 by Microsoft Corp. All Rights Reserved.
  4. //
  5. // crc64.cpp
  6. //
  7. // cvadai 12-Nov-99 created.
  8. //
  9. //***************************************************************************
  10. #define _CRC64_CPP_
  11. #include "precomp.h"
  12. #include <crc64.h>
  13. hash_t CRC64::CrcXor[256];
  14. hash_t CRC64::Poly[64+1];
  15. BOOL CRC64::bInit = FALSE;
  16. unsigned long GetHIDWord(__int64 in)
  17. {
  18. unsigned long lRet = (long)(in >> 0x20);
  19. return lRet;
  20. }
  21. unsigned long GetLODWord(__int64 in)
  22. {
  23. unsigned long lRet = (long)(in & 0xFFFFFFFF);
  24. return lRet;
  25. }
  26. __int64 MakeInt64( long val1, long val2)
  27. {
  28. __int64 ret = 0;
  29. ret = ((__int64)val1 << 0x20) + val2;
  30. return ret;
  31. }
  32. //***************************************************************************
  33. //
  34. // CRC64::GenerateHashValue
  35. //
  36. //***************************************************************************
  37. hash_t CRC64::GenerateHashValue(const wchar_t *p)
  38. {
  39. hash_t tRet = 0;
  40. if (!p)
  41. return 0;
  42. if (!bInit)
  43. {
  44. Initialize();
  45. bInit = TRUE;
  46. }
  47. long h1, h2;
  48. h1 = HINIT1, h2 = HINIT2;
  49. int s = 64-40;
  50. hint_t m = (hint_t)-1 >> (0);
  51. h1 &= m;
  52. while (*p)
  53. {
  54. int char1 = towupper(*p);
  55. int i = (h1 >> s) & 255;
  56. h1 = ((h1 << 8) & m) ^ (int)(h2 >> 24) ^ GetHIDWord(CrcXor[i]);
  57. h2 = (h2 << 8) ^ char1 ^ GetLODWord(CrcXor[i]);
  58. ++p;
  59. }
  60. tRet = MakeInt64(h1, h2);
  61. return(tRet);
  62. }
  63. //***************************************************************************
  64. //
  65. // CRC64::Initialize
  66. //
  67. //***************************************************************************
  68. void CRC64::Initialize(void)
  69. {
  70. int i;
  71. /*
  72. * Polynomials to use for various crc sizes. Start with the 64 bit
  73. * polynomial and shift it right to generate the polynomials for fewer
  74. * bits. Note that the polynomial for N bits has no bit set above N-8.
  75. * This allows us to do a simple table-driven CRC.
  76. */
  77. hint_t h1 = POLY1, h2 = POLY2;
  78. Poly[64] = MakeInt64(POLY1, POLY2);
  79. for (i = 63; i >= 16; --i)
  80. {
  81. h1 = (GetHIDWord(Poly[i+1])) >> 1;
  82. h2 = (GetLODWord(Poly[i+1])) >> 1 | ((GetHIDWord(Poly[i+1] ) & 1) << 31) | 1;
  83. Poly[i] = MakeInt64(h1, h2);
  84. }
  85. for (i = 0; i < 256; ++i)
  86. {
  87. int j;
  88. int v = i;
  89. h1 = 0, h2 = 0;
  90. for (j = 0; j < 8; ++j, (v <<= 1)) {
  91. h1 <<= 1;
  92. if (h2 & 0x80000000UL)
  93. h1 |= 1;
  94. h2 = (h2 << 1);
  95. if (v & 0x80) {
  96. h1 ^= GetHIDWord(Poly[64]);
  97. h2 ^= GetLODWord(Poly[64]);
  98. }
  99. }
  100. CrcXor[i] = MakeInt64(h1, h2);
  101. }
  102. }
  103. //***************************************************************************
  104. //
  105. // CRC64::RMalloc
  106. //
  107. //***************************************************************************
  108. void * CRC64::RMalloc(int bytes)
  109. {
  110. static wchar_t *RBuf = NULL;
  111. static int RSize = 0;
  112. bytes = (bytes + 3) & ~3;
  113. if (bytes > RSize)
  114. {
  115. RBuf = (wchar_t *)malloc(65536);
  116. RSize = 65536;
  117. }
  118. RBuf += bytes;
  119. RSize -= bytes;
  120. return(RBuf - bytes);
  121. }