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.

430 lines
7.8 KiB

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