Leaked source code of windows server 2003
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.

121 lines
2.7 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. crchash.cpp
  5. Abstract:
  6. CRC Hash function
  7. */
  8. //
  9. //
  10. // Hashing function adopted from the INN code (see copyright below)
  11. //
  12. /*
  13. Copyright 1988 Jon Zeeff (zeeff@b-tech.ann-arbor.mi.us)
  14. You can use this code in any manner, as long as you leave my name on it
  15. and don't hold me responsible for any problems with it.
  16. * This is a simplified version of the pathalias hashing function.
  17. * Thanks to Steve Belovin and Peter Honeyman
  18. *
  19. * hash a string into a long int. 31 bit crc (from andrew appel).
  20. * the crc table is computed at run time by crcinit() -- we could
  21. * precompute, but it takes 1 clock tick on a 750.
  22. *
  23. * This fast table calculation works only if POLY is a prime polynomial
  24. * in the field of integers modulo 2. Since the coefficients of a
  25. * 32-bit polynomial won't fit in a 32-bit word, the high-order bit is
  26. * implicit. IT MUST ALSO BE THE CASE that the coefficients of orders
  27. * 31 down to 25 are zero. Happily, we have candidates, from
  28. * E. J. Watson, "Primitive Polynomials (Mod 2)", Math. Comp. 16 (1962):
  29. * x^32 + x^7 + x^5 + x^3 + x^2 + x^1 + x^0
  30. * x^31 + x^3 + x^0
  31. *
  32. * We reverse the bits to get:
  33. * 111101010000000000000000000000001 but drop the last 1
  34. * f 5 0 0 0 0 0 0
  35. * 010010000000000000000000000000001 ditto, for 31-bit crc
  36. * 4 8 0 0 0 0 0 0
  37. */
  38. #include <windows.h>
  39. #include "crchash.h"
  40. static long CrcTable[128];
  41. static BOOL bInitialized = FALSE;
  42. /*
  43. - crcinit - initialize tables for hash function
  44. */
  45. void crcinit()
  46. {
  47. INT i, j;
  48. DWORD sum;
  49. if(bInitialized) return;
  50. for (i = 0; i < 128; ++i) {
  51. sum = 0;
  52. for (j = 7 - 1; j >= 0; --j) {
  53. if (i & (1 << j)) {
  54. sum ^= POLY >> j;
  55. }
  56. }
  57. CrcTable[i] = sum;
  58. }
  59. bInitialized = TRUE;
  60. } // crcinit
  61. /*
  62. - hash - Honeyman's nice hashing function
  63. */
  64. DWORD CRCHash(const BYTE* Key, DWORD Length)
  65. {
  66. DWORD sum = 0;
  67. while ( Length-- ) {
  68. sum = (sum >> 7) ^ CrcTable[(sum ^ (*Key++)) & 0x7f];
  69. }
  70. return(sum);
  71. }
  72. /*
  73. - hash - Honeyman's nice hashing function
  74. */
  75. DWORD CRCHashNoCase(const BYTE* Key, DWORD Length)
  76. {
  77. DWORD sum = 0;
  78. while ( Length-- ) {
  79. BYTE thisKey = (BYTE) tolower((char) (*Key++));
  80. sum = (sum >> 7) ^ CrcTable[(sum ^ thisKey) & 0x7f];
  81. }
  82. return(sum);
  83. }
  84. /*
  85. - hash - Honeyman's nice hashing function
  86. */
  87. DWORD CRCHashWithPrecompute( DWORD sum, const BYTE* Key, DWORD Length)
  88. {
  89. while ( Length-- ) {
  90. sum = (sum >> 7) ^ CrcTable[(sum ^ (*Key++)) & 0x7f];
  91. }
  92. return(sum);
  93. }