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.

741 lines
17 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. hash.c
  5. Abstract:
  6. Hashing routines used to speed lookup of memdb keys.
  7. Author:
  8. Jim Schmidt (jimschm) 8-Aug-1996
  9. Revision History:
  10. Jim Schmidt (jimschm) 21-Oct-1997 Split from memdb.c
  11. --*/
  12. #include "pch.h"
  13. #include "memdbp.h"
  14. #ifndef UNICODE
  15. #error UNICODE required
  16. #endif
  17. #define DBG_MEMDB "MemDb"
  18. //
  19. // Globals
  20. //
  21. DWORD g_HashSize;
  22. DWORD g_HashEnd;
  23. DWORD g_HashFreeHead;
  24. PBYTE g_HashBuf;
  25. //
  26. // #defines
  27. //
  28. // see memdbp.h for bit restrictions
  29. #define INVALID_OFFSET_MASKED (INVALID_OFFSET & OFFSET_MASK)
  30. #define ASSERT_OFFSET_ONLY(x) MYASSERT(((x) & RESERVED_MASK) == 0 || (x) == INVALID_OFFSET)
  31. #define UNMASK_OFFSET(x) ((x)==INVALID_OFFSET_MASKED ? INVALID_OFFSET : (x))
  32. #define MASK_OFFSET(x) ((x) & OFFSET_MASK)
  33. #define MASK_4BIT 0x0000000f
  34. #define INVALID_OFFSET_4BIT (INVALID_OFFSET & MASK_4BIT)
  35. #define ASSERT_4BIT(x) MYASSERT(((x) & (~MASK_4BIT)) == 0 || (x) == INVALID_OFFSET)
  36. #define CONVERT_4TO8(x) ((BYTE) ((x)==INVALID_OFFSET_4BIT ? INVALID_OFFSET : (x)))
  37. #define CONVERT_8TO4(x) ((x) & MASK_4BIT)
  38. #define HASH_BUCKETS 39989
  39. #define HASH_BLOCK_SIZE (HASH_BUCKETS * sizeof (BUCKETSTRUCT))
  40. #define HASHBUFPTR(offset) ((PBUCKETSTRUCT) (g_HashBuf + offset))
  41. //
  42. // Local privates
  43. //
  44. VOID
  45. pResetHashBlock (
  46. VOID
  47. );
  48. //
  49. // Implementation
  50. //
  51. BOOL
  52. InitializeHashBlock (
  53. VOID
  54. )
  55. {
  56. g_HashSize = HASH_BLOCK_SIZE * 2;
  57. g_HashBuf = (PBYTE) MemAlloc (g_hHeap, 0, g_HashSize);
  58. pResetHashBlock();
  59. return TRUE;
  60. }
  61. VOID
  62. pResetHashBlock (
  63. VOID
  64. )
  65. {
  66. PBUCKETSTRUCT BucketPtr;
  67. INT i;
  68. g_HashEnd = HASH_BLOCK_SIZE;
  69. g_HashFreeHead = INVALID_OFFSET;
  70. BucketPtr = (PBUCKETSTRUCT) g_HashBuf;
  71. for (i = 0 ; i < HASH_BUCKETS ; i++) {
  72. BucketPtr->Offset = INVALID_OFFSET;
  73. BucketPtr->Info.NextItem = INVALID_OFFSET_MASKED;
  74. BucketPtr->Info.Hive = 0;
  75. BucketPtr++;
  76. }
  77. }
  78. VOID
  79. FreeHashBlock (
  80. VOID
  81. )
  82. {
  83. if (g_HashBuf) {
  84. MemFree (g_hHeap, 0, g_HashBuf);
  85. g_HashBuf = NULL;
  86. }
  87. g_HashSize = 0;
  88. g_HashEnd = 0;
  89. g_HashFreeHead = INVALID_OFFSET;
  90. }
  91. BOOL
  92. EnumFirstHashEntry (
  93. OUT PHASHENUM EnumPtr
  94. )
  95. {
  96. ZeroMemory (EnumPtr, sizeof (HASHENUM));
  97. return EnumNextHashEntry (EnumPtr);
  98. }
  99. BOOL
  100. EnumNextHashEntry (
  101. IN OUT PHASHENUM EnumPtr
  102. )
  103. {
  104. for (;;) {
  105. if (EnumPtr->Bucket == HASH_BUCKETS) {
  106. //
  107. // The completion case
  108. //
  109. return FALSE;
  110. }
  111. if (!EnumPtr->BucketPtr) {
  112. //
  113. // This case occurs when we are begining to enumerate a bucket
  114. //
  115. EnumPtr->BucketPtr = (PBUCKETSTRUCT) g_HashBuf + EnumPtr->Bucket;
  116. if (EnumPtr->BucketPtr->Offset == INVALID_OFFSET) {
  117. EnumPtr->BucketPtr = NULL;
  118. EnumPtr->Bucket += 1;
  119. continue;
  120. }
  121. //
  122. // Return this first item in the bucket
  123. //
  124. EnumPtr->LastOffset = EnumPtr->BucketPtr->Offset;
  125. return TRUE;
  126. }
  127. //
  128. // This case occurs when we are continuing enumeration of a bucket
  129. //
  130. if (EnumPtr->BucketPtr->Offset == INVALID_OFFSET) {
  131. //
  132. // Current bucket item (and also the last bucket item) may have
  133. // been deleted -- check that now
  134. //
  135. if (!EnumPtr->PrevBucketPtr) {
  136. //
  137. // Last item has been deleted; continue to next bucket
  138. //
  139. EnumPtr->BucketPtr = NULL;
  140. EnumPtr->Bucket += 1;
  141. continue;
  142. }
  143. //
  144. // Previous bucket item is valid; use it.
  145. //
  146. EnumPtr->BucketPtr = EnumPtr->PrevBucketPtr;
  147. } else {
  148. //
  149. // Current bucket item may have been deleted, but another item was
  150. // moved to its place -- check that now
  151. //
  152. if (EnumPtr->BucketPtr->Offset != EnumPtr->LastOffset) {
  153. EnumPtr->LastOffset = EnumPtr->BucketPtr->Offset;
  154. return TRUE;
  155. }
  156. }
  157. //
  158. // We now know that the current bucket item was not changed, so it
  159. // becomes our previous item and we move on to the next item (if
  160. // one exists)
  161. //
  162. if (UNMASK_OFFSET (EnumPtr->BucketPtr->Info.NextItem) == INVALID_OFFSET) {
  163. //
  164. // End of bucket reached
  165. //
  166. EnumPtr->BucketPtr = NULL;
  167. EnumPtr->Bucket += 1;
  168. continue;
  169. }
  170. EnumPtr->PrevBucketPtr = EnumPtr->BucketPtr;
  171. EnumPtr->BucketPtr = HASHBUFPTR (UNMASK_OFFSET (EnumPtr->BucketPtr->Info.NextItem));
  172. EnumPtr->LastOffset = EnumPtr->BucketPtr->Offset;
  173. MYASSERT(EnumPtr->LastOffset != INVALID_OFFSET);
  174. break;
  175. }
  176. return TRUE;
  177. }
  178. typedef struct {
  179. BYTE Hive;
  180. DWORD Offset;
  181. } HASH_ITEM, *PHASH_ITEM;
  182. BOOL
  183. SaveHashBlock (
  184. HANDLE File
  185. )
  186. {
  187. BOOL b;
  188. DWORD Written;
  189. PBYTE BackupBlock;
  190. UINT OrgEnd, OrgSize, OrgFreeHead;
  191. PBYTE OrgBlock;
  192. WCHAR TempStr[MEMDB_MAX];
  193. GROWBUFFER GrowBuf = GROWBUF_INIT;
  194. HASHENUM e;
  195. PHASH_ITEM ItemPtr;
  196. UINT u;
  197. //
  198. // Back up the hash block
  199. //
  200. BackupBlock = MemAlloc (g_hHeap, 0, g_HashEnd);
  201. CopyMemory (BackupBlock, g_HashBuf, g_HashEnd);
  202. OrgEnd = g_HashEnd;
  203. OrgSize = g_HashSize;
  204. OrgFreeHead = g_HashFreeHead;
  205. OrgBlock = g_HashBuf;
  206. g_HashBuf = BackupBlock;
  207. //
  208. // Delete all hash entries that do not belong to the root database.
  209. // Do this by queueing the hash entry removal, so the EnumNextHashEntry
  210. // function will continue to work.
  211. //
  212. if (EnumFirstHashEntry (&e)) {
  213. do {
  214. if (e.BucketPtr->Info.Hive) {
  215. ItemPtr = (PHASH_ITEM) GrowBuffer (&GrowBuf, sizeof (HASH_ITEM));
  216. ItemPtr->Hive = (BYTE) (e.BucketPtr->Info.Hive);
  217. ItemPtr->Offset = e.BucketPtr->Offset;
  218. }
  219. } while (EnumNextHashEntry (&e));
  220. }
  221. ItemPtr = (PHASH_ITEM) GrowBuf.Buf;
  222. for (u = 0 ; u < GrowBuf.End ; u += sizeof (HASH_ITEM), ItemPtr++) {
  223. SelectDatabase (ItemPtr->Hive);
  224. if (PrivateBuildKeyFromOffset (
  225. 0,
  226. ItemPtr->Offset,
  227. TempStr,
  228. NULL,
  229. NULL,
  230. NULL
  231. )) {
  232. RemoveHashTableEntry (TempStr);
  233. }
  234. }
  235. //
  236. // Write the hash block end and deleted pointer
  237. //
  238. b = WriteFile (File, &g_HashEnd, sizeof (DWORD), &Written, NULL);
  239. if (b) {
  240. b = WriteFile (File, &g_HashFreeHead, sizeof (DWORD), &Written, NULL);
  241. }
  242. //
  243. // Write the hash block
  244. //
  245. if (b) {
  246. b = WriteFile (File, g_HashBuf, g_HashEnd, &Written, NULL);
  247. if (Written != g_HashEnd) {
  248. b = FALSE;
  249. }
  250. }
  251. //
  252. // Restore the hash block
  253. //
  254. PushError();
  255. g_HashEnd = OrgEnd;
  256. g_HashSize = OrgSize;
  257. g_HashFreeHead = OrgFreeHead;
  258. g_HashBuf = OrgBlock;
  259. SelectDatabase (0);
  260. MemFree (g_hHeap, 0, BackupBlock);
  261. PopError();
  262. return b;
  263. }
  264. BOOL
  265. LoadHashBlock (
  266. HANDLE File
  267. )
  268. {
  269. BOOL b;
  270. DWORD Read;
  271. PBYTE TempBuf = NULL;
  272. //
  273. // Read the hash block end and deleted pointer; allocate memory for block.
  274. //
  275. b = ReadFile (File, &g_HashEnd, sizeof (DWORD), &Read, NULL);
  276. if (b) {
  277. b = ReadFile (File, &g_HashFreeHead, sizeof (DWORD), &Read, NULL);
  278. }
  279. if (b) {
  280. g_HashSize = g_HashEnd;
  281. TempBuf = (PBYTE) MemAlloc (g_hHeap, 0, g_HashSize);
  282. if (TempBuf) {
  283. if (g_HashBuf) {
  284. MemFree (g_hHeap, 0, g_HashBuf);
  285. }
  286. g_HashBuf = TempBuf;
  287. TempBuf = NULL;
  288. } else {
  289. b = FALSE;
  290. }
  291. }
  292. //
  293. // Read the hash block
  294. //
  295. if (b) {
  296. b = ReadFile (File, g_HashBuf, g_HashSize, &Read, NULL);
  297. if (Read != g_HashSize) {
  298. b = FALSE;
  299. SetLastError (ERROR_BAD_FORMAT);
  300. }
  301. }
  302. return b;
  303. }
  304. DWORD
  305. pCalculateHashVal (
  306. IN PCWSTR String
  307. )
  308. {
  309. DWORD Hash = 0;
  310. while (*String) {
  311. Hash = (Hash << 3) | (Hash >> 29);
  312. Hash += towlower (*String);
  313. String++;
  314. }
  315. Hash %= HASH_BUCKETS;
  316. return Hash;
  317. }
  318. DWORD
  319. pAllocBucket (
  320. VOID
  321. )
  322. {
  323. DWORD rBucketOffset;
  324. PBYTE TempBuf;
  325. PBUCKETSTRUCT BucketPtr;
  326. if (g_HashFreeHead != INVALID_OFFSET) {
  327. rBucketOffset = g_HashFreeHead;
  328. BucketPtr = HASHBUFPTR (rBucketOffset);
  329. g_HashFreeHead = UNMASK_OFFSET (BucketPtr->Info.NextItem);
  330. MYASSERT (rBucketOffset < g_HashEnd);
  331. } else {
  332. if (g_HashEnd + sizeof (BUCKETSTRUCT) > g_HashSize) {
  333. g_HashSize += HASH_BLOCK_SIZE;
  334. TempBuf = MemReAlloc (g_hHeap, 0, g_HashBuf, g_HashSize);
  335. DEBUGMSG ((DBG_NAUSEA, "Realloc'd memdb hash table"));
  336. if (!TempBuf) {
  337. DEBUGMSG ((DBG_ERROR, "Out of memory!"));
  338. g_HashSize -= HASH_BLOCK_SIZE;
  339. return INVALID_OFFSET;
  340. }
  341. g_HashBuf = TempBuf;
  342. }
  343. rBucketOffset = g_HashEnd;
  344. g_HashEnd += sizeof (BUCKETSTRUCT);
  345. BucketPtr = HASHBUFPTR (rBucketOffset);
  346. }
  347. BucketPtr->Offset = INVALID_OFFSET;
  348. BucketPtr->Info.NextItem = INVALID_OFFSET_MASKED;
  349. ASSERT_4BIT (g_SelectedDatabase);
  350. BucketPtr->Info.Hive = CONVERT_8TO4 (g_SelectedDatabase);
  351. return rBucketOffset;
  352. }
  353. BOOL
  354. AddHashTableEntry (
  355. IN PCWSTR FullString,
  356. IN DWORD Offset
  357. )
  358. {
  359. DWORD Bucket;
  360. PBUCKETSTRUCT BucketPtr, PrevBucketPtr;
  361. DWORD BucketOffset;
  362. DWORD NewOffset;
  363. DWORD PrevBucketOffset;
  364. Bucket = pCalculateHashVal (FullString);
  365. BucketPtr = (PBUCKETSTRUCT) g_HashBuf + Bucket;
  366. //
  367. // See if root bucket item has been used or not
  368. //
  369. if (BucketPtr->Offset != INVALID_OFFSET) {
  370. //
  371. // Yes - add to end of the chain
  372. //
  373. BucketOffset = Bucket * sizeof (BUCKETSTRUCT);
  374. do {
  375. BucketPtr = HASHBUFPTR (BucketOffset);
  376. PrevBucketOffset = BucketOffset;
  377. BucketOffset = UNMASK_OFFSET (BucketPtr->Info.NextItem);
  378. } while (BucketOffset != INVALID_OFFSET);
  379. //
  380. // Add to the chain
  381. //
  382. NewOffset = pAllocBucket();
  383. PrevBucketPtr = HASHBUFPTR (PrevBucketOffset);
  384. ASSERT_OFFSET_ONLY (NewOffset);
  385. PrevBucketPtr->Info.NextItem = MASK_OFFSET (NewOffset);
  386. if (NewOffset == INVALID_OFFSET) {
  387. return FALSE;
  388. }
  389. BucketPtr = HASHBUFPTR (NewOffset);
  390. MYASSERT (BucketPtr->Info.NextItem == INVALID_OFFSET_MASKED);
  391. }
  392. BucketPtr->Offset = Offset;
  393. ASSERT_4BIT (g_SelectedDatabase);
  394. BucketPtr->Info.Hive = CONVERT_8TO4 (g_SelectedDatabase);
  395. #ifdef DEBUG
  396. {
  397. DWORD HashOffset;
  398. HashOffset = FindStringInHashTable (FullString, NULL);
  399. MYASSERT (HashOffset != INVALID_OFFSET);
  400. DEBUGMSG_IF ((HashOffset != Offset, DBG_MEMDB, "Duplicate in hash table: %s", FullString));
  401. }
  402. #endif
  403. return TRUE;
  404. }
  405. PBUCKETSTRUCT
  406. pFindBucketItemInHashTable (
  407. IN PCWSTR FullString,
  408. OUT PBUCKETSTRUCT *PrevBucketPtr, OPTIONAL
  409. OUT DWORD *HashOffsetPtr OPTIONAL
  410. )
  411. {
  412. DWORD Bucket;
  413. DWORD BucketOffset;
  414. PBUCKETSTRUCT BucketPtr = NULL;
  415. WCHAR TempStr[MEMDB_MAX];
  416. Bucket = pCalculateHashVal (FullString);
  417. BucketOffset = Bucket * sizeof (BUCKETSTRUCT);
  418. #ifdef MEMORY_TRACKING
  419. {
  420. //
  421. // Circular link check
  422. //
  423. DWORD Prev, Next;
  424. DWORD Turtle, Rabbit;
  425. BOOL Even = FALSE;
  426. Rabbit = BucketOffset;
  427. Turtle = Rabbit;
  428. while (Rabbit != INVALID_OFFSET) {
  429. // Make rabbit point to next item in chain
  430. Prev = Rabbit;
  431. BucketPtr = HASHBUFPTR (Rabbit);
  432. Rabbit = UNMASK_OFFSET (BucketPtr->Info.NextItem);
  433. // We should always be ahead of the turtle
  434. if (Rabbit == Turtle) {
  435. BucketPtr = HASHBUFPTR (Rabbit);
  436. Next = UNMASK_OFFSET (BucketPtr->Info.NextItem);
  437. DEBUGMSG ((
  438. DBG_WHOOPS,
  439. "Circular link detected in memdb hash table! Turtle=%u, Rabbit=%u, Next=%u, Prev=%u",
  440. Turtle,
  441. Rabbit,
  442. Next,
  443. Prev
  444. ));
  445. return NULL;
  446. }
  447. // Make turtle point to next item in chain (1 of every 2 passes)
  448. if (Even) {
  449. BucketPtr = HASHBUFPTR (Turtle);
  450. Turtle = UNMASK_OFFSET (BucketPtr->Info.NextItem);
  451. }
  452. Even = !Even;
  453. }
  454. }
  455. #endif
  456. BucketPtr = HASHBUFPTR (BucketOffset);
  457. if (PrevBucketPtr) {
  458. *PrevBucketPtr = BucketPtr;
  459. }
  460. //
  461. // If root bucket is not empty, scan bucket for FullString
  462. //
  463. if (BucketPtr->Offset != INVALID_OFFSET) {
  464. do {
  465. BucketPtr = HASHBUFPTR (BucketOffset);
  466. ASSERT_4BIT (g_SelectedDatabase);
  467. if (BucketPtr->Info.Hive == g_SelectedDatabase) {
  468. //
  469. // Build string using offset
  470. //
  471. PrivateBuildKeyFromOffset (
  472. 0,
  473. BucketPtr->Offset,
  474. TempStr,
  475. NULL,
  476. NULL,
  477. NULL
  478. );
  479. //
  480. // Do compare and return if match is found
  481. //
  482. if (StringIMatchW (FullString, TempStr)) {
  483. if (HashOffsetPtr) {
  484. *HashOffsetPtr = BucketOffset;
  485. }
  486. return BucketPtr;
  487. }
  488. }
  489. if (PrevBucketPtr) {
  490. *PrevBucketPtr = BucketPtr;
  491. }
  492. BucketOffset = UNMASK_OFFSET (BucketPtr->Info.NextItem);
  493. } while (BucketOffset != INVALID_OFFSET);
  494. }
  495. return NULL;
  496. }
  497. DWORD
  498. FindStringInHashTable (
  499. IN PCWSTR FullString,
  500. OUT PBYTE DatabaseId OPTIONAL
  501. )
  502. {
  503. PBUCKETSTRUCT BucketPtr;
  504. BucketPtr = pFindBucketItemInHashTable (FullString, NULL, NULL);
  505. if (BucketPtr) {
  506. if (DatabaseId) {
  507. *DatabaseId = (BYTE) (BucketPtr->Info.Hive);
  508. }
  509. return BucketPtr->Offset;
  510. }
  511. return INVALID_OFFSET;
  512. }
  513. BOOL
  514. RemoveHashTableEntry (
  515. IN PCWSTR FullString
  516. )
  517. {
  518. PBUCKETSTRUCT BucketPtr;
  519. PBUCKETSTRUCT PrevBucketPtr;
  520. DWORD NextOffset;
  521. PBUCKETSTRUCT NextBucketPtr;
  522. DWORD BucketOffset;
  523. BucketPtr = pFindBucketItemInHashTable (FullString, &PrevBucketPtr, &BucketOffset);
  524. if (!BucketPtr) {
  525. return FALSE;
  526. }
  527. if (PrevBucketPtr != BucketPtr) {
  528. //
  529. // If not at the first level (prev != current), give the block
  530. // to free space.
  531. //
  532. PrevBucketPtr->Info.NextItem = BucketPtr->Info.NextItem;
  533. ASSERT_OFFSET_ONLY (g_HashFreeHead);
  534. BucketPtr->Info.NextItem = MASK_OFFSET (g_HashFreeHead);
  535. BucketPtr->Offset = INVALID_OFFSET;
  536. g_HashFreeHead = BucketOffset;
  537. } else {
  538. //
  539. // Invalidate next item pointer if at the first level
  540. //
  541. if (UNMASK_OFFSET (BucketPtr->Info.NextItem) != INVALID_OFFSET) {
  542. //
  543. // Copy next item to root array
  544. //
  545. NextOffset = UNMASK_OFFSET (BucketPtr->Info.NextItem);
  546. NextBucketPtr = HASHBUFPTR (NextOffset);
  547. CopyMemory (BucketPtr, NextBucketPtr, sizeof (BUCKETSTRUCT));
  548. //
  549. // Donate next item to free space
  550. //
  551. ASSERT_OFFSET_ONLY (g_HashFreeHead);
  552. NextBucketPtr->Info.NextItem = MASK_OFFSET (g_HashFreeHead);
  553. NextBucketPtr->Offset = INVALID_OFFSET;
  554. g_HashFreeHead = NextOffset;
  555. } else {
  556. //
  557. // Delete of last item in bucket -- invalidate the root array item
  558. //
  559. BucketPtr->Info.NextItem = INVALID_OFFSET_MASKED;
  560. BucketPtr->Offset = INVALID_OFFSET;
  561. }
  562. }
  563. return TRUE;
  564. }