Implemented packing of value names
[reactos.git] / reactos / ntoskrnl / cm / cm.h
1 #ifndef __INCLUDE_CM_H
2 #define __INCLUDE_CM_H
3
4 #ifdef DBG
5 #define CHECKED 1
6 #else
7 #define CHECKED 0
8 #endif
9
10 #define REG_ROOT_KEY_NAME L"\\Registry"
11 #define REG_MACHINE_KEY_NAME L"\\Registry\\Machine"
12 #define REG_HARDWARE_KEY_NAME L"\\Registry\\Machine\\HARDWARE"
13 #define REG_DESCRIPTION_KEY_NAME L"\\Registry\\Machine\\HARDWARE\\DESCRIPTION"
14 #define REG_DEVICEMAP_KEY_NAME L"\\Registry\\Machine\\HARDWARE\\DEVICEMAP"
15 #define REG_RESOURCEMAP_KEY_NAME L"\\Registry\\Machine\\HARDWARE\\RESOURCEMAP"
16 #define REG_CLASSES_KEY_NAME L"\\Registry\\Machine\\Software\\Classes"
17 #define REG_SYSTEM_KEY_NAME L"\\Registry\\Machine\\System"
18 #define REG_SOFTWARE_KEY_NAME L"\\Registry\\Machine\\Software"
19 #define REG_SAM_KEY_NAME L"\\Registry\\Machine\\Sam"
20 #define REG_SEC_KEY_NAME L"\\Registry\\Machine\\Security"
21 #define REG_USERS_KEY_NAME L"\\Registry\\User"
22 #define REG_USER_KEY_NAME L"\\Registry\\User\\CurrentUser"
23 #define SYSTEM_REG_FILE L"\\SystemRoot\\System32\\Config\\SYSTEM"
24 #define SOFTWARE_REG_FILE L"\\SystemRoot\\System32\\Config\\SOFTWARE"
25 #define USER_REG_FILE L"\\SystemRoot\\System32\\Config\\DEFAULT"
26 #define SAM_REG_FILE L"\\SystemRoot\\System32\\Config\\SAM"
27 #define SEC_REG_FILE L"\\SystemRoot\\System32\\Config\\SECURITY"
28
29 #define REG_SYSTEM_FILE_NAME L"\\SYSTEM"
30 #define REG_SOFTWARE_FILE_NAME L"\\SOFTWARE"
31 #define REG_USER_FILE_NAME L"\\DEFAULT"
32 #define REG_SAM_FILE_NAME L"\\SAM"
33 #define REG_SEC_FILE_NAME L"\\SECURITY"
34
35 #define REG_BLOCK_SIZE 4096
36 #define REG_HBIN_DATA_OFFSET 32
37 #define REG_BIN_ID 0x6e696268
38 #define REG_INIT_BLOCK_LIST_SIZE 32
39 #define REG_INIT_HASH_TABLE_SIZE 3
40 #define REG_EXTEND_HASH_TABLE_SIZE 4
41 #define REG_VALUE_LIST_CELL_MULTIPLE 4
42 #define REG_KEY_CELL_ID 0x6b6e
43 #define REG_HASH_TABLE_BLOCK_ID 0x666c
44 #define REG_VALUE_CELL_ID 0x6b76
45 #define REG_HIVE_ID 0x66676572
46
47 #define REGISTRY_FILE_MAGIC "REGEDIT4"
48
49 #define REG_MACHINE_STD_HANDLE_NAME "HKEY_LOCAL_MACHINE"
50 #define REG_CLASSES_STD_HANDLE_NAME "HKEY_CLASSES_ROOT"
51 #define REG_USERS_STD_HANDLE_NAME "HKEY_USERS"
52 #define REG_USER_STD_HANDLE_NAME "HKEY_CURRENT_USER"
53 #define REG_CONFIG_STD_HANDLE_NAME "HKEY_CURRENT_CONFIG"
54 #define REG_DYN_STD_HANDLE_NAME "HKEY_DYN_DATA"
55 #define MAX_REG_STD_HANDLE_NAME 19
56
57 // BLOCK_OFFSET = offset in file after header block
58 typedef DWORD BLOCK_OFFSET;
59
60 /* header for registry hive file : */
61 typedef struct _HIVE_HEADER
62 {
63 /* Hive identifier "regf" (0x66676572) */
64 ULONG BlockId;
65
66 /* File version ? */
67 ULONG Version;
68
69 /* File version ? - same as Version */
70 ULONG VersionOld;
71
72 /* When this hive file was last modified */
73 FILETIME DateModified;
74
75 /* Registry format version ? (1?) */
76 ULONG Unused3;
77
78 /* Registry format version ? (3?) */
79 ULONG Unused4;
80
81 /* Registry format version ? (0?) */
82 ULONG Unused5;
83
84 /* Registry format version ? (1?) */
85 ULONG Unused6;
86
87 /* Offset into file from the byte after the end of the base block.
88 If the hive is volatile, this is the actual pointer to the KEY_CELL */
89 BLOCK_OFFSET RootKeyCell;
90
91 /* Size of each hive block ? */
92 ULONG BlockSize;
93
94 /* (1?) */
95 ULONG Unused7;
96
97 /* Name of hive file */
98 WCHAR FileName[64];
99
100 /* ? */
101 ULONG Unused8[83];
102
103 /* Checksum of first 0x200 bytes */
104 ULONG Checksum;
105 } __attribute__((packed)) HIVE_HEADER, *PHIVE_HEADER;
106
107 typedef struct _HBIN
108 {
109 /* Bin identifier "hbin" (0x6E696268) */
110 ULONG BlockId;
111
112 /* Block offset of this bin */
113 BLOCK_OFFSET BlockOffset;
114
115 /* Size in bytes, multiple of the block size (4KB) */
116 ULONG BlockSize;
117
118 /* ? */
119 ULONG Unused1;
120
121 /* When this bin was last modified */
122 FILETIME DateModified;
123
124 /* ? */
125 ULONG Unused2;
126 } __attribute__((packed)) HBIN, *PHBIN;
127
128 typedef struct _CELL_HEADER
129 {
130 /* <0 if used, >0 if free */
131 LONG CellSize;
132 } __attribute__((packed)) CELL_HEADER, *PCELL_HEADER;
133
134 typedef struct _KEY_CELL
135 {
136 /* Size of this cell */
137 LONG CellSize;
138
139 /* Key cell identifier "kn" (0x6b6e) */
140 USHORT Id;
141
142 /* ? */
143 USHORT Type;
144
145 /* Time of last flush */
146 FILETIME LastWriteTime;
147
148 /* ? */
149 ULONG UnUsed1;
150
151 /* Block offset of parent key cell */
152 BLOCK_OFFSET ParentKeyOffset;
153
154 /* Count of sub keys for the key in this key cell */
155 ULONG NumberOfSubKeys;
156
157 /* ? */
158 ULONG UnUsed2;
159
160 /* Block offset of has table for FIXME: subkeys/values? */
161 BLOCK_OFFSET HashTableOffset;
162
163 /* ? */
164 ULONG UnUsed3;
165
166 /* Count of values contained in this key cell */
167 ULONG NumberOfValues;
168
169 /* Block offset of VALUE_LIST_CELL */
170 BLOCK_OFFSET ValuesOffset;
171
172 /* Block offset of security cell */
173 BLOCK_OFFSET SecurityKeyOffset;
174
175 /* Block offset of registry key class */
176 BLOCK_OFFSET ClassNameOffset;
177
178 /* ? */
179 ULONG Unused4[5];
180
181 /* Size in bytes of key name */
182 USHORT NameSize;
183
184 /* Size of class name in bytes */
185 USHORT ClassSize;
186
187 /* Name of key (not zero terminated) */
188 UCHAR Name[0];
189 } __attribute__((packed)) KEY_CELL, *PKEY_CELL;
190
191 /* KEY_CELL.Type constants */
192 #define REG_LINK_KEY_CELL_TYPE 0x10
193 #define REG_KEY_CELL_TYPE 0x20
194 #define REG_ROOT_KEY_CELL_TYPE 0x2c
195
196
197 // hash record :
198 // HashValue=four letters of value's name
199 typedef struct _HASH_RECORD
200 {
201 BLOCK_OFFSET KeyOffset;
202 ULONG HashValue;
203 } __attribute__((packed)) HASH_RECORD, *PHASH_RECORD;
204
205 typedef struct _HASH_TABLE_CELL
206 {
207 LONG CellSize;
208 USHORT Id;
209 USHORT HashTableSize;
210 HASH_RECORD Table[0];
211 } __attribute__((packed)) HASH_TABLE_CELL, *PHASH_TABLE_CELL;
212
213 typedef struct _VALUE_LIST_CELL
214 {
215 LONG CellSize;
216 BLOCK_OFFSET Values[0];
217 } __attribute__((packed)) VALUE_LIST_CELL, *PVALUE_LIST_CELL;
218
219 typedef struct _VALUE_CELL
220 {
221 LONG CellSize;
222 USHORT Id; // "kv"
223 USHORT NameSize; // length of Name
224 LONG DataSize; // length of datas in the cell pointed by DataOffset
225 BLOCK_OFFSET DataOffset;// datas are here if high bit of DataSize is set
226 ULONG DataType;
227 USHORT Flags;
228 USHORT Unused1;
229 UCHAR Name[0]; /* warning : not zero terminated */
230 } __attribute__((packed)) VALUE_CELL, *PVALUE_CELL;
231
232 /* VALUE_CELL.Flags constants */
233 #define REG_VALUE_NAME_PACKED 0x0001
234
235
236 typedef struct _DATA_CELL
237 {
238 LONG CellSize;
239 UCHAR Data[0];
240 } __attribute__((packed)) DATA_CELL, *PDATA_CELL;
241
242 typedef struct _REGISTRY_HIVE
243 {
244 LIST_ENTRY HiveList;
245 ULONG Flags;
246 UNICODE_STRING Filename;
247 ULONG FileSize;
248 PFILE_OBJECT FileObject;
249 PVOID Bcb;
250 PHIVE_HEADER HiveHeader;
251 ULONG BlockListSize;
252 PHBIN *BlockList;
253 ULONG FreeListSize;
254 ULONG FreeListMax;
255 PCELL_HEADER *FreeList;
256 BLOCK_OFFSET *FreeListOffset;
257 ERESOURCE HiveResource;
258
259 // NTSTATUS (*Extend)(ULONG NewSize);
260 // PVOID (*Flush)(VOID);
261 } REGISTRY_HIVE, *PREGISTRY_HIVE;
262
263 /* REGISTRY_HIVE.Flags constants */
264 #define HIVE_VOLATILE 0x00000001
265
266 #define IsVolatileHive(Hive)(Hive->Flags & HIVE_VOLATILE)
267 #define IsPermanentHive(Hive)(!(Hive->Flags & HIVE_VOLATILE))
268
269 #define IsFreeCell(Cell)(Cell->CellSize >= 0)
270 #define IsUsedCell(Cell)(Cell->CellSize < 0)
271
272
273 /* KEY_OBJECT.Flags */
274
275 /* When set, the key is scheduled for deletion, and all
276 attempts to access the key must not succeed */
277 #define KO_MARKED_FOR_DELETE 0x00000001
278
279
280 /* Type defining the Object Manager Key Object */
281 typedef struct _KEY_OBJECT
282 {
283 /* Fields used by the Object Manager */
284 CSHORT Type;
285 CSHORT Size;
286
287 /* Key flags */
288 ULONG Flags;
289
290 /* Length of Name */
291 USHORT NameSize;
292
293 /* Name of key */
294 PCHAR Name;
295
296 /* Registry hive the key belongs to */
297 PREGISTRY_HIVE RegistryHive;
298
299 /* Block offset of the key cell this key belongs in */
300 BLOCK_OFFSET BlockOffset;
301
302 /* KEY_CELL this key belong in */
303 PKEY_CELL KeyCell;
304
305 /* Link to the parent KEY_OBJECT for this key */
306 struct _KEY_OBJECT *ParentKey;
307
308 /* Subkeys loaded in SubKeys */
309 ULONG NumberOfSubKeys;
310
311 /* Space allocated in SubKeys */
312 ULONG SizeOfSubKeys;
313
314 /* List of subkeys loaded */
315 struct _KEY_OBJECT **SubKeys;
316 } KEY_OBJECT, *PKEY_OBJECT;
317
318 /* Bits 31-22 (top 10 bits) of the cell index is the directory index */
319 #define CmiDirectoryIndex(CellIndex)(CellIndex & 0xffc000000)
320 /* Bits 21-12 (middle 10 bits) of the cell index is the table index */
321 #define CmiTableIndex(Cellndex)(CellIndex & 0x003ff000)
322 /* Bits 11-0 (bottom 12 bits) of the cell index is the byte offset */
323 #define CmiByteOffset(Cellndex)(CellIndex & 0x00000fff)
324
325
326 extern BOOLEAN CmiDoVerify;
327 extern PREGISTRY_HIVE CmiVolatileHive;
328 extern POBJECT_TYPE CmiKeyType;
329 extern KSPIN_LOCK CmiKeyListLock;
330
331 extern LIST_ENTRY CmiHiveListHead;
332 extern KSPIN_LOCK CmiHiveListLock;
333
334
335 VOID
336 CmiVerifyBinCell(PHBIN BinCell);
337 VOID
338 CmiVerifyKeyCell(PKEY_CELL KeyCell);
339 VOID
340 CmiVerifyRootKeyCell(PKEY_CELL RootKeyCell);
341 VOID
342 CmiVerifyKeyObject(PKEY_OBJECT KeyObject);
343 VOID
344 CmiVerifyRegistryHive(PREGISTRY_HIVE RegistryHive);
345
346 #ifdef DBG
347 #define VERIFY_BIN_CELL CmiVerifyBinCell
348 #define VERIFY_KEY_CELL CmiVerifyKeyCell
349 #define VERIFY_ROOT_KEY_CELL CmiVerifyRootKeyCell
350 #define VERIFY_VALUE_CELL CmiVerifyValueCell
351 #define VERIFY_VALUE_LIST_CELL CmiVerifyValueListCell
352 #define VERIFY_KEY_OBJECT CmiVerifyKeyObject
353 #define VERIFY_REGISTRY_HIVE CmiVerifyRegistryHive
354 #else
355 #define VERIFY_BIN_CELL(x)
356 #define VERIFY_KEY_CELL(x)
357 #define VERIFY_ROOT_KEY_CELL(x)
358 #define VERIFY_VALUE_CELL(x)
359 #define VERIFY_VALUE_LIST_CELL(x)
360 #define VERIFY_KEY_OBJECT(x)
361 #define VERIFY_REGISTRY_HIVE(x)
362 #endif
363
364 NTSTATUS STDCALL
365 CmiObjectParse(IN PVOID ParsedObject,
366 OUT PVOID *NextObject,
367 IN PUNICODE_STRING FullPath,
368 IN OUT PWSTR *Path,
369 IN ULONG Attribute);
370
371 NTSTATUS STDCALL
372 CmiObjectCreate(PVOID ObjectBody,
373 PVOID Parent,
374 PWSTR RemainingPath,
375 struct _OBJECT_ATTRIBUTES* ObjectAttributes);
376
377 VOID STDCALL
378 CmiObjectDelete(PVOID DeletedObject);
379
380 VOID
381 CmiAddKeyToList(PKEY_OBJECT ParentKey,
382 IN PKEY_OBJECT NewKey);
383
384 NTSTATUS
385 CmiRemoveKeyFromList(IN PKEY_OBJECT NewKey);
386
387 PKEY_OBJECT CmiScanKeyList(IN PKEY_OBJECT Parent,
388 IN PCHAR KeyNameBuf,
389 IN ULONG Attributes);
390
391 NTSTATUS
392 CmiCreateRegistryHive(PWSTR Filename,
393 PREGISTRY_HIVE *RegistryHive,
394 BOOLEAN CreateNew);
395
396 NTSTATUS
397 CmiRemoveRegistryHive(PREGISTRY_HIVE RegistryHive);
398
399 ULONG
400 CmiGetMaxNameLength(IN PREGISTRY_HIVE RegistryHive,
401 IN PKEY_CELL KeyCell);
402
403 ULONG
404 CmiGetMaxClassLength(IN PREGISTRY_HIVE RegistryHive,
405 IN PKEY_CELL KeyCell);
406
407 ULONG
408 CmiGetMaxValueNameLength(IN PREGISTRY_HIVE RegistryHive,
409 IN PKEY_CELL KeyCell);
410
411 ULONG
412 CmiGetMaxValueDataLength(IN PREGISTRY_HIVE RegistryHive,
413 IN PKEY_CELL KeyCell);
414
415 NTSTATUS
416 CmiScanForSubKey(IN PREGISTRY_HIVE RegistryHive,
417 IN PKEY_CELL KeyCell,
418 OUT PKEY_CELL *SubKeyCell,
419 OUT BLOCK_OFFSET *BlockOffset,
420 IN PCHAR KeyName,
421 IN ACCESS_MASK DesiredAccess,
422 IN ULONG Attributes);
423
424 NTSTATUS
425 CmiAddSubKey(IN PREGISTRY_HIVE RegistryHive,
426 IN PKEY_OBJECT Parent,
427 OUT PKEY_OBJECT SubKey,
428 IN PWSTR NewSubKeyName,
429 IN USHORT NewSubKeyNameSize,
430 IN ULONG TitleIndex,
431 IN PUNICODE_STRING Class,
432 IN ULONG CreateOptions);
433
434 NTSTATUS
435 CmiScanKeyForValue(IN PREGISTRY_HIVE RegistryHive,
436 IN PKEY_CELL KeyCell,
437 IN PUNICODE_STRING ValueName,
438 OUT PVALUE_CELL *ValueCell,
439 OUT BLOCK_OFFSET *VBOffset);
440
441 NTSTATUS
442 CmiGetValueFromKeyByIndex(IN PREGISTRY_HIVE RegistryHive,
443 IN PKEY_CELL KeyCell,
444 IN ULONG Index,
445 OUT PVALUE_CELL *ValueCell);
446
447 NTSTATUS
448 CmiAddValueToKey(IN PREGISTRY_HIVE RegistryHive,
449 IN PKEY_CELL KeyCell,
450 IN PUNICODE_STRING ValueName,
451 OUT PVALUE_CELL *pValueCell,
452 OUT BLOCK_OFFSET *pVBOffset);
453
454 NTSTATUS
455 CmiDeleteValueFromKey(IN PREGISTRY_HIVE RegistryHive,
456 IN PKEY_CELL KeyCell,
457 IN PUNICODE_STRING ValueName);
458
459 NTSTATUS
460 CmiAllocateHashTableBlock(IN PREGISTRY_HIVE RegistryHive,
461 OUT PHASH_TABLE_CELL *HashBlock,
462 OUT BLOCK_OFFSET *HBOffset,
463 IN ULONG HashTableSize);
464
465 PKEY_CELL
466 CmiGetKeyFromHashByIndex(PREGISTRY_HIVE RegistryHive,
467 PHASH_TABLE_CELL HashBlock,
468 ULONG Index);
469
470 NTSTATUS
471 CmiAddKeyToHashTable(PREGISTRY_HIVE RegistryHive,
472 PHASH_TABLE_CELL HashBlock,
473 PKEY_CELL NewKeyCell,
474 BLOCK_OFFSET NKBOffset);
475
476 NTSTATUS
477 CmiAllocateValueCell(IN PREGISTRY_HIVE RegistryHive,
478 OUT PVALUE_CELL *ValueCell,
479 OUT BLOCK_OFFSET *VBOffset,
480 IN PUNICODE_STRING ValueName);
481
482 NTSTATUS
483 CmiDestroyValueCell(PREGISTRY_HIVE RegistryHive,
484 PVALUE_CELL ValueCell,
485 BLOCK_OFFSET VBOffset);
486
487 NTSTATUS
488 CmiAllocateBlock(PREGISTRY_HIVE RegistryHive,
489 PVOID *Block,
490 LONG BlockSize,
491 BLOCK_OFFSET * pBlockOffset);
492
493 NTSTATUS
494 CmiDestroyBlock(PREGISTRY_HIVE RegistryHive,
495 PVOID Block,
496 BLOCK_OFFSET Offset);
497
498 PVOID
499 CmiGetBlock(PREGISTRY_HIVE RegistryHive,
500 BLOCK_OFFSET BlockOffset,
501 OUT PHBIN * ppBin);
502
503 VOID
504 CmiLockBlock(PREGISTRY_HIVE RegistryHive,
505 PVOID Block);
506
507 VOID
508 CmiReleaseBlock(PREGISTRY_HIVE RegistryHive,
509 PVOID Block);
510
511 NTSTATUS
512 CmiAddFree(PREGISTRY_HIVE RegistryHive,
513 PCELL_HEADER FreeBlock,
514 BLOCK_OFFSET FreeOffset);
515
516 NTSTATUS
517 CmiInitHives(BOOLEAN SetUpBoot);
518
519 ULONG
520 CmiGetPackedNameLength(IN PUNICODE_STRING Name,
521 OUT PBOOLEAN Packable);
522
523 BOOLEAN
524 CmiComparePackedNames(IN PUNICODE_STRING Name,
525 IN PCHAR NameBuffer,
526 IN USHORT NameBufferSize,
527 IN BOOLEAN NamePacked);
528
529 VOID
530 CmiCopyPackedName(PWCHAR NameBuffer,
531 PCHAR PackedNameBuffer,
532 ULONG PackedNameSize);
533
534 #endif /*__INCLUDE_CM_H*/