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.

215 lines
5.3 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <windows.h>
  5. #include "shash.h"
  6. //
  7. // The following table is generated from this code :
  8. //
  9. // #define POLY 0x48000000L /* 31-bit polynomial (avoids sign problems) */
  10. // INT i, j;
  11. // DWORD sum;
  12. //
  13. // for (i = 0; i < 128; ++i) {
  14. // sum = 0;
  15. // for (j = 7 - 1; j >= 0; --j) {
  16. // if (i & (1 << j)) {
  17. // sum ^= POLY >> j;
  18. // }
  19. // }
  20. // CrcTable[i] = sum;
  21. //
  22. // These values are used when computing hash values,
  23. // and the result is a very good hash function with good distribution !
  24. //
  25. static long CrcTable[128] =
  26. {
  27. 0, 1207959552, 603979776, 1811939328,
  28. 301989888, 1509949440, 905969664, 2113929216,
  29. 150994944, 1090519040, 754974720, 1694498816,
  30. 452984832, 1392508928, 1056964608, 1996488704,
  31. 75497472, 1283457024, 545259520, 1753219072,
  32. 377487360, 1585446912, 847249408, 2055208960,
  33. 226492416, 1166016512, 696254464, 1635778560,
  34. 528482304, 1468006400, 998244352, 1937768448,
  35. 37748736, 1245708288, 641728512, 1849688064,
  36. 272629760, 1480589312, 876609536, 2084569088,
  37. 188743680, 1128267776, 792723456, 1732247552,
  38. 423624704, 1363148800, 1027604480, 1967128576,
  39. 113246208, 1321205760, 583008256, 1790967808,
  40. 348127232, 1556086784, 817889280, 2025848832,
  41. 264241152, 1203765248, 734003200, 1673527296,
  42. 499122176, 1438646272, 968884224, 1908408320,
  43. 18874368, 1226833920, 622854144, 1830813696,
  44. 320864256, 1528823808, 924844032, 2132803584,
  45. 136314880, 1075838976, 740294656, 1679818752,
  46. 438304768, 1377828864, 1042284544, 1981808640,
  47. 94371840, 1302331392, 564133888, 1772093440,
  48. 396361728, 1604321280, 866123776, 2074083328,
  49. 211812352, 1151336448, 681574400, 1621098496,
  50. 513802240, 1453326336, 983564288, 1923088384,
  51. 56623104, 1264582656, 660602880, 1868562432,
  52. 291504128, 1499463680, 895483904, 2103443456,
  53. 174063616, 1113587712, 778043392, 1717567488,
  54. 408944640, 1348468736, 1012924416, 1952448512,
  55. 132120576, 1340080128, 601882624, 1809842176,
  56. 367001600, 1574961152, 836763648, 2044723200,
  57. 249561088, 1189085184, 719323136, 1658847232,
  58. 484442112, 1423966208, 954204160, 1893728256,
  59. } ;
  60. ULONG
  61. SHashComputeHashValue(
  62. IN void* lpv
  63. )
  64. {
  65. PUNICODE_STRING Name = (PUNICODE_STRING) lpv ;
  66. DWORD sum = 0;
  67. LONG Length = 0;
  68. PWCHAR Key = NULL;
  69. WCHAR ch ;
  70. Length = Name->Length/sizeof(WCHAR);
  71. Key = Name->Buffer;
  72. while ( Length-- )
  73. {
  74. ch = RtlUpcaseUnicodeChar(*Key++) ;
  75. sum = (sum >> 7) ^ CrcTable[(sum ^ (ch)) & 0x7f];
  76. }
  77. return(sum);
  78. }
  79. /*++
  80. Routine Description :
  81. This function is provided to the hash tables to compare two keys.
  82. NOTE : we compare in a case insensitive fashion !
  83. Arguments :
  84. pvKey1, pvKey2 - two keys,
  85. Return Value :
  86. -1 iff pvKey1 < pvKey2
  87. 0 iff pvKey1 == pvKey2
  88. 1 iff pvKey1 > pvKey2
  89. --*/
  90. int
  91. SHashMatchNameKeysCaseInsensitive( void* pvKey1,
  92. void* pvKey2
  93. )
  94. {
  95. PUNICODE_STRING pKey1 = (PUNICODE_STRING)pvKey1 ;
  96. PUNICODE_STRING pKey2 = (PUNICODE_STRING)pvKey2 ;
  97. if( pKey1->Length == pKey2->Length )
  98. {
  99. return RtlCompareUnicodeString(
  100. pKey1,
  101. pKey2,
  102. TRUE
  103. ) ;
  104. }
  105. else
  106. {
  107. return (signed)pKey1->Length - (signed)pKey2->Length ;
  108. }
  109. }
  110. void*
  111. SHashAllocate( ULONG cbAlloc )
  112. {
  113. void * pMem = NULL;
  114. pMem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbAlloc);
  115. return pMem;
  116. }
  117. void
  118. SHashFree( void* lpv )
  119. {
  120. HeapFree( GetProcessHeap(), 0, lpv );
  121. }
  122. BOOLEAN SHashReadLockTable(PSHASH_TABLE pTable)
  123. {
  124. BOOLEAN fRet = TRUE;
  125. EnterCriticalSection(pTable->pLock);
  126. pTable->Flags |= SHASH_CAP_TABLE_LOCKED;
  127. return fRet;
  128. }
  129. BOOLEAN SHashWriteLockTable(PSHASH_TABLE pTable)
  130. {
  131. BOOLEAN fRet = TRUE;
  132. EnterCriticalSection(pTable->pLock);
  133. pTable->Flags |= SHASH_CAP_TABLE_LOCKED;
  134. return fRet;
  135. }
  136. BOOLEAN SHashReadUnLockTable(PSHASH_TABLE pTable)
  137. {
  138. pTable->Flags &= ~SHASH_CAP_TABLE_LOCKED;
  139. LeaveCriticalSection(pTable->pLock);
  140. return TRUE;
  141. }
  142. BOOLEAN SHashWriteUnLockTable(PSHASH_TABLE pTable)
  143. {
  144. pTable->Flags &= ~SHASH_CAP_TABLE_LOCKED;
  145. LeaveCriticalSection(pTable->pLock);
  146. return TRUE;
  147. }
  148. void * SHashAllocLock(void)
  149. {
  150. void * pMem = NULL;
  151. BOOL fCritInit = FALSE;
  152. DWORD Status = 0;
  153. pMem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CRITICAL_SECTION));
  154. if(pMem)
  155. {
  156. fCritInit = InitializeCriticalSectionAndSpinCount(pMem, SHASH_CRIT_SPIN_COUNT);
  157. if(!fCritInit)
  158. {
  159. Status = GetLastError();
  160. HeapFree( GetProcessHeap(), 0, pMem );
  161. pMem = NULL;
  162. SetLastError(Status);
  163. }
  164. }
  165. return pMem;
  166. }
  167. void SHashFreeLock(void * pMem)
  168. {
  169. DeleteCriticalSection(pMem);
  170. HeapFree( GetProcessHeap(), 0, pMem );
  171. }