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.

212 lines
4.3 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. keyfind.c
  5. Abstract:
  6. Routines that manage finding the memdb key structures.
  7. Author:
  8. Jim Schmidt (jimschm) 8-Aug-1996
  9. Revision History:
  10. mvander 13-Aug-1999 split from keystruct.c
  11. --*/
  12. #include "pch.h"
  13. #include "memdbp.h"
  14. UINT
  15. FindKeyStruct (
  16. PCWSTR Key
  17. )
  18. /*++
  19. Routine Description:
  20. FindKeyStruct takes a wack-delimited key string, and returns the Index
  21. of the keystruct. this is different than FindKey() because that only
  22. finds endpoints, so it is fast, because it can use the hash table.
  23. this recurses down the memdb database levels to the specified keystruct.
  24. Arguments:
  25. Key - String holding full path of key to be found
  26. Return Value:
  27. Index of key, or INVALID_OFFSET if function failed
  28. --*/
  29. {
  30. UINT TreeOffset, Index=INVALID_OFFSET;
  31. PWSTR p, q;
  32. WCHAR Temp[MEMDB_MAX];
  33. MYASSERT (g_CurrentDatabase);
  34. if (*Key==0) {
  35. return INVALID_OFFSET;
  36. }
  37. StringCopyW (Temp, Key);
  38. q = Temp;
  39. do {
  40. if (Index == INVALID_OFFSET) {
  41. TreeOffset = g_CurrentDatabase->FirstLevelTree;
  42. } else {
  43. TreeOffset = GetKeyStruct(Index)->NextLevelTree;
  44. }
  45. if (TreeOffset == INVALID_OFFSET) {
  46. return INVALID_OFFSET;
  47. }
  48. p = wcschr (q, L'\\');
  49. if (p) {
  50. *p = 0;
  51. }
  52. Index = FindKeyStructInTree (TreeOffset, q, FALSE);
  53. if (Index == INVALID_OFFSET) {
  54. return INVALID_OFFSET;
  55. }
  56. if (p) {
  57. q = p + 1;
  58. }
  59. } while (p);
  60. return Index;
  61. }
  62. UINT
  63. FindKey (
  64. IN PCWSTR FullKeyPath
  65. )
  66. /*++
  67. Routine Description:
  68. FindKey locates a complete key string and returns
  69. the Index to the KEYSTRUCT, or INVALID_OFFSET if
  70. the key path does not exist. The FullKeyPath
  71. must supply the complete path to the KEYSTRUCT.
  72. Arguments:
  73. FullKeyPath - A backslash-delimited key path to a value
  74. Return Value:
  75. An Index to the structure, or INVALID_OFFSET if the key
  76. was not found.
  77. --*/
  78. {
  79. MYASSERT (g_CurrentDatabase);
  80. return FindStringInHashTable (g_CurrentDatabase->HashTable, FullKeyPath);
  81. }
  82. UINT
  83. FindKeyStructUsingTreeOffset (
  84. IN UINT TreeOffset,
  85. IN OUT PUINT pTreeEnum,
  86. IN PCWSTR KeyStr
  87. )
  88. /*++
  89. Routine Description:
  90. FindKeyStructUsingTreeOffset takes a key pattern and looks
  91. for the Index in the tree specified by TreeOffset. The key
  92. name must not contain backslashes, but can contain wildcards.
  93. Arguments:
  94. TreeOffset - An offset to the tree
  95. pTreeEnum - The previous value from FindKeyStructUsingTreeOffset
  96. (for enumeration) or INVALID_OFFSET for the first
  97. call.
  98. KeyStr - The name of the key to find in the binary tree
  99. Return Value:
  100. An Index to the structure, or INVALID_OFFSET if the key
  101. was not found.
  102. --*/
  103. {
  104. PKEYSTRUCT KeyStruct;
  105. UINT KeyIndex;
  106. SIZE_T len1, len2;
  107. MYASSERT(pTreeEnum!=NULL);
  108. if (*pTreeEnum == INVALID_OFFSET) {
  109. KeyIndex = GetFirstIndex(TreeOffset, pTreeEnum);
  110. } else {
  111. KeyIndex = GetNextIndex(pTreeEnum);
  112. }
  113. //
  114. // Examine key as a pattern, then go to next node
  115. //
  116. while (KeyIndex != INVALID_OFFSET) {
  117. KeyStruct = GetKeyStruct(KeyIndex);
  118. len1 = CharCountW (KeyStr);
  119. len2 = *KeyStruct->KeyName;
  120. if ((len1 == len2) &&
  121. (StringIMatchCharCountW (KeyStr, KeyStruct->KeyName + 1, len1))
  122. ) {
  123. return KeyIndex;
  124. }
  125. //
  126. // No match yet - go to next node
  127. //
  128. KeyIndex = GetNextIndex(pTreeEnum);
  129. }
  130. return INVALID_OFFSET;
  131. }
  132. #ifdef DEBUG
  133. BOOL FindKeyStructInDatabase(UINT KeyOffset)
  134. {
  135. PKEYSTRUCT pKey;
  136. MYASSERT (g_CurrentDatabase);
  137. pKey = GetKeyStructFromOffset(KeyOffset);
  138. if (pKey->KeyFlags & KSF_DELETED) {
  139. return TRUE;
  140. }
  141. while (pKey->PrevLevelIndex!=INVALID_OFFSET) {
  142. pKey=GetKeyStruct(pKey->PrevLevelIndex);
  143. }
  144. return (FindKeyStructInTree(g_CurrentDatabase->FirstLevelTree, pKey->KeyName, TRUE)!=INVALID_OFFSET);
  145. }
  146. #endif