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.

428 lines
7.4 KiB

  1. //
  2. // #defines
  3. //
  4. //
  5. // This is our version stamp. Change MEMDB_VERSION only.
  6. //
  7. #define MEMDB_VERSION L"v9 "
  8. #define VERSION_BASE_SIGNATURE L"memdb dat file "
  9. #define MEMDB_DEBUG_SIGNATURE L"debug"
  10. #define MEMDB_NODBG_SIGNATURE L"nodbg"
  11. #define VERSION_SIGNATURE VERSION_BASE_SIGNATURE MEMDB_VERSION
  12. #define DEBUG_FILE_SIGNATURE VERSION_SIGNATURE MEMDB_DEBUG_SIGNATURE
  13. #define RETAIL_FILE_SIGNATURE VERSION_SIGNATURE MEMDB_NODBG_SIGNATURE
  14. #define SIGNATURE 0xab12e87d
  15. //
  16. // We must reserve 5 bits. In a KEYSTRUCT node, 2 bits are used for AVL
  17. // balancing, 1 bit for endpoint, 1 bit for proxy nodes, and 1 bit for binary
  18. // nodes. In a hash table entry, the top 5 bits provide the hive index.
  19. //
  20. #define RESERVED_BITS 27
  21. #define RESERVED_MASK 0xf8000000
  22. #define OFFSET_MASK (~RESERVED_MASK)
  23. //
  24. // KEYSTRUCT flags
  25. //
  26. #define KSF_ENDPOINT 0x08000000
  27. #define KSF_BINARY 0x40000000
  28. #define KSF_PROXY_NODE 0x80000000
  29. #define KSF_BALANCE_MASK 0x30000000
  30. #define KSF_BALANCE_SHIFT 28 // bit pos of KSF_RIGHT_HEAVY
  31. #define KSF_RIGHT_HEAVY 0x10000000
  32. #define KSF_LEFT_HEAVY 0x20000000
  33. #define KSF_USERFLAG_MASK OFFSET_MASK
  34. //
  35. // Binary tree allocation parameters
  36. //
  37. #define ALLOC_TOLERANCE 32
  38. #define BLOCK_SIZE 0x00010000
  39. #define MAX_HIVE_NAME 64
  40. #define TOKENBUCKETS 511
  41. //
  42. // Typedefs
  43. //
  44. //
  45. // The DATABASE structure holds all pieces of information necessary
  46. // to maintain a portion of the overall memory database. There is a
  47. // root DATABASE structure that always exists (its index is zero),
  48. // and there are additional DATABASE structures for each database
  49. // the caller creates via the MemDbCreateDatabase call. Callers create
  50. // additional databases when a node is needed for temporary processing.
  51. //
  52. typedef struct {
  53. DWORD AllocSize;
  54. DWORD End;
  55. DWORD FirstLevelRoot;
  56. DWORD FirstDeleted;
  57. DWORD TokenBuckets[TOKENBUCKETS];
  58. PBYTE Buf;
  59. WCHAR Hive[MAX_HIVE_NAME];
  60. } DATABASE, *PDATABASE;
  61. //
  62. // Hive struct (for KSF_HIVE type)
  63. //
  64. typedef struct {
  65. DATABASE DatabaseInfo;
  66. } HIVE, *PHIVE;
  67. //
  68. // Binary block struct (for KSF_BINARY type of the key struct)
  69. //
  70. typedef struct _tagBINARYBLOCK {
  71. #ifdef DEBUG
  72. DWORD Signature;
  73. #endif
  74. DWORD Size;
  75. struct _tagBINARYBLOCK *NextPtr, *PrevPtr;
  76. DWORD OwningKey;
  77. BYTE Data[];
  78. } BINARYBLOCK, *PBINARYBLOCK;
  79. //
  80. // KEYSTRUCT holds each piece of memdb entries. A single KEYSTRUCT
  81. // holds a portion of a key (delimited by a backslash), and the
  82. // KEYSTRUCTs are organized into a binary tree. Each KEYSTRUCT
  83. // can also contain additional binary trees. This is what makes
  84. // memdb so versitle--many relationships can be established by
  85. // formatting key strings in various ways.
  86. //
  87. typedef struct {
  88. DWORD Signature;
  89. // Offsets for data struct
  90. DWORD Left, Right;
  91. DWORD Parent;
  92. union {
  93. struct {
  94. union {
  95. DWORD dwValue;
  96. PBINARYBLOCK BinaryPtr;
  97. };
  98. DWORD Flags;
  99. // Other properties here
  100. };
  101. DWORD NextDeleted; // for deleted items, we keep a list of free blocks
  102. };
  103. DWORD NextLevelRoot;
  104. DWORD PrevLevelNode;
  105. DWORD KeyToken;
  106. } KEYSTRUCT_DEBUG, *PKEYSTRUCT_DEBUG;
  107. typedef struct {
  108. // Offsets for data struct
  109. DWORD Left, Right;
  110. DWORD Parent;
  111. union {
  112. struct {
  113. union {
  114. DWORD dwValue;
  115. PBINARYBLOCK BinaryPtr;
  116. };
  117. DWORD Flags;
  118. // Other properties here
  119. };
  120. DWORD NextDeleted; // for deleted items, we keep a list of free blocks
  121. };
  122. DWORD NextLevelRoot;
  123. DWORD PrevLevelNode;
  124. DWORD KeyToken;
  125. } KEYSTRUCT_RETAIL, *PKEYSTRUCT_RETAIL;
  126. typedef struct {
  127. DWORD Right;
  128. WCHAR String[];
  129. } TOKENSTRUCT, *PTOKENSTRUCT;
  130. #ifdef DEBUG
  131. #define KEYSTRUCT KEYSTRUCT_DEBUG
  132. #define PKEYSTRUCT PKEYSTRUCT_DEBUG
  133. #else
  134. #define KEYSTRUCT KEYSTRUCT_RETAIL
  135. #define PKEYSTRUCT PKEYSTRUCT_RETAIL
  136. #endif
  137. //
  138. // Globals
  139. //
  140. extern PDATABASE g_db;
  141. extern BYTE g_SelectedDatabase; // current index of active database
  142. extern PHIVE g_HeadHive;
  143. extern CRITICAL_SECTION g_MemDbCs;
  144. #ifdef DEBUG
  145. extern BOOL g_UseDebugStructs;
  146. #endif
  147. //
  148. // memdb.c routines
  149. //
  150. PCWSTR
  151. SelectHive (
  152. PCWSTR FullKeyStr
  153. );
  154. BOOL
  155. PrivateMemDbSetValueA (
  156. IN PCSTR Key,
  157. IN DWORD Val,
  158. IN DWORD SetFlags,
  159. IN DWORD ClearFlags,
  160. OUT PDWORD Offset OPTIONAL
  161. );
  162. BOOL
  163. PrivateMemDbSetValueW (
  164. IN PCWSTR Key,
  165. IN DWORD Val,
  166. IN DWORD SetFlags,
  167. IN DWORD ClearFlags,
  168. OUT PDWORD Offset OPTIONAL
  169. );
  170. BOOL
  171. PrivateMemDbSetBinaryValueA (
  172. IN PCSTR Key,
  173. IN PCBYTE BinaryData,
  174. IN DWORD DataSize,
  175. OUT PDWORD Offset OPTIONAL
  176. );
  177. BOOL
  178. PrivateMemDbSetBinaryValueW (
  179. IN PCWSTR Key,
  180. IN PCBYTE BinaryData,
  181. IN DWORD DataSize,
  182. OUT PDWORD Offset OPTIONAL
  183. );
  184. //
  185. // hash.c routines
  186. //
  187. BOOL
  188. InitializeHashBlock (
  189. VOID
  190. );
  191. VOID
  192. FreeHashBlock (
  193. VOID
  194. );
  195. BOOL
  196. SaveHashBlock (
  197. HANDLE File
  198. );
  199. BOOL
  200. LoadHashBlock (
  201. HANDLE File
  202. );
  203. BOOL
  204. AddHashTableEntry (
  205. IN PCWSTR FullString,
  206. IN DWORD Offset
  207. );
  208. DWORD
  209. FindStringInHashTable (
  210. IN PCWSTR FullString,
  211. OUT PBYTE DatabaseId OPTIONAL
  212. );
  213. BOOL
  214. RemoveHashTableEntry (
  215. IN PCWSTR FullString
  216. );
  217. //
  218. // binval.c
  219. //
  220. PCBYTE
  221. GetKeyStructBinaryData (
  222. PKEYSTRUCT KeyStruct
  223. );
  224. DWORD
  225. GetKeyStructBinarySize (
  226. PKEYSTRUCT KeyStruct
  227. );
  228. PBINARYBLOCK
  229. AllocBinaryBlock (
  230. IN PCBYTE Data,
  231. IN DWORD DataSize,
  232. IN DWORD OwningKey
  233. );
  234. VOID
  235. FreeKeyStructBinaryBlock (
  236. PKEYSTRUCT KeyStruct
  237. );
  238. VOID
  239. FreeAllBinaryBlocks (
  240. VOID
  241. );
  242. BOOL
  243. LoadBinaryBlocks (
  244. HANDLE File
  245. );
  246. BOOL
  247. SaveBinaryBlocks (
  248. HANDLE File
  249. );
  250. //
  251. // bintree.c
  252. //
  253. PKEYSTRUCT
  254. GetKeyStruct (
  255. DWORD Offset
  256. );
  257. DWORD
  258. FindKeyStruct (
  259. IN DWORD RootOffset,
  260. IN PCWSTR KeyName
  261. );
  262. DWORD
  263. GetFirstOffset (
  264. DWORD RootOffset
  265. );
  266. DWORD
  267. GetNextOffset (
  268. DWORD NodeOffset
  269. );
  270. DWORD
  271. FindKey (
  272. IN PCWSTR FullKeyPath
  273. );
  274. DWORD
  275. FindPatternKey (
  276. IN DWORD RootOffset,
  277. IN PCWSTR FullKeyPath,
  278. IN BOOL EndPatternAllowed
  279. );
  280. DWORD
  281. FindKeyUsingPattern (
  282. IN DWORD RootOffset,
  283. IN PCWSTR FullKeyPath
  284. );
  285. DWORD
  286. FindPatternKeyUsingPattern (
  287. IN DWORD RootOffset,
  288. IN PCWSTR FullKeyPath
  289. );
  290. DWORD
  291. NewKey (
  292. IN PCWSTR KeyStr,
  293. IN PCWSTR KeyStrWithHive
  294. );
  295. VOID
  296. DeleteKey (
  297. IN PCWSTR KeyStr,
  298. IN OUT PDWORD RootPtr,
  299. IN BOOL MustMatch
  300. );
  301. VOID
  302. CopyValToPtr (
  303. PKEYSTRUCT KeyStruct,
  304. PDWORD ValPtr
  305. );
  306. VOID
  307. CopyFlagsToPtr (
  308. PKEYSTRUCT KeyStruct,
  309. PDWORD ValPtr
  310. );
  311. BOOL
  312. PrivateBuildKeyFromOffset (
  313. IN DWORD StartLevel, // zero-based
  314. IN DWORD TailOffset,
  315. OUT PWSTR Buffer, OPTIONAL
  316. OUT PDWORD ValPtr, OPTIONAL
  317. OUT PDWORD UserFlagsPtr, OPTIONAL
  318. OUT PDWORD CharCount OPTIONAL
  319. );
  320. BOOL
  321. SelectDatabase (
  322. IN BYTE DatabaseId
  323. );
  324. #ifdef DEBUG
  325. VOID
  326. DumpBinTreeStats (
  327. VOID
  328. );
  329. #else
  330. #define DumpBinTreeStats()
  331. #endif
  332. PCWSTR
  333. GetKeyToken (
  334. IN DWORD Token
  335. );