[CMLIB]
[reactos.git] / reactos / lib / cmlib / cminit.c
1 /*
2 * PROJECT: registry manipulation library
3 * LICENSE: GPL - See COPYING in the top level directory
4 * COPYRIGHT: Copyright 2005 Filip Navara <navaraf@reactos.org>
5 * Copyright 2001 - 2005 Eric Kohl
6 */
7
8 #include "cmlib.h"
9 #define NDEBUG
10 #include <debug.h>
11
12 ULONG CmlibTraceLevel = 0;
13
14 BOOLEAN CMAPI
15 CmCreateRootNode(
16 PHHIVE Hive,
17 PCWSTR Name)
18 {
19 PCM_KEY_NODE KeyCell;
20 HCELL_INDEX RootCellIndex;
21 ULONG NameSize;
22
23 /* Allocate the cell */
24 NameSize = (ULONG)strlenW(Name) * sizeof(WCHAR);
25 RootCellIndex = HvAllocateCell(Hive,
26 FIELD_OFFSET(CM_KEY_NODE, Name) + NameSize,
27 Stable,
28 HCELL_NIL);
29 if (RootCellIndex == HCELL_NIL) return FALSE;
30
31 /* Seutp the base block */
32 Hive->BaseBlock->RootCell = RootCellIndex;
33 Hive->BaseBlock->CheckSum = HvpHiveHeaderChecksum(Hive->BaseBlock);
34
35 /* Get the key cell */
36 KeyCell = (PCM_KEY_NODE)HvGetCell(Hive, RootCellIndex);
37 if (!KeyCell) return FALSE;
38
39 /* Setup the cell */
40 KeyCell->Signature = (USHORT)CM_KEY_NODE_SIGNATURE;
41 KeyCell->Flags = KEY_HIVE_ENTRY | KEY_NO_DELETE;
42 KeyCell->LastWriteTime.QuadPart = 0;
43 KeyCell->Parent = HCELL_NIL;
44 KeyCell->SubKeyCounts[Stable] = 0;
45 KeyCell->SubKeyCounts[Volatile] = 0;
46 KeyCell->SubKeyLists[Stable] = HCELL_NIL;
47 KeyCell->SubKeyLists[Volatile] = HCELL_NIL;
48 KeyCell->ValueList.Count = 0;
49 KeyCell->ValueList.List = HCELL_NIL;
50 KeyCell->Security = HCELL_NIL;
51 KeyCell->Class = HCELL_NIL;
52 KeyCell->ClassLength = 0;
53 KeyCell->MaxNameLen = 0;
54 KeyCell->MaxClassLen = 0;
55 KeyCell->MaxValueNameLen = 0;
56 KeyCell->MaxValueDataLen = 0;
57
58 /* Write the name */
59 KeyCell->NameLength = (USHORT)NameSize;
60 RtlCopyMemory(KeyCell->Name, Name, NameSize);
61
62 /* Return success */
63 HvReleaseCell(Hive, RootCellIndex);
64 return TRUE;
65 }
66
67 static VOID CMAPI
68 CmpPrepareKey(
69 PHHIVE RegistryHive,
70 PCM_KEY_NODE KeyCell);
71
72 static VOID CMAPI
73 CmpPrepareIndexOfKeys(
74 PHHIVE RegistryHive,
75 PCM_KEY_INDEX IndexCell)
76 {
77 ULONG i;
78
79 if (IndexCell->Signature == CM_KEY_INDEX_ROOT ||
80 IndexCell->Signature == CM_KEY_INDEX_LEAF)
81 {
82 for (i = 0; i < IndexCell->Count; i++)
83 {
84 PCM_KEY_INDEX SubIndexCell = HvGetCell(RegistryHive, IndexCell->List[i]);
85 if (SubIndexCell->Signature == CM_KEY_NODE_SIGNATURE)
86 CmpPrepareKey(RegistryHive, (PCM_KEY_NODE)SubIndexCell);
87 else
88 CmpPrepareIndexOfKeys(RegistryHive, SubIndexCell);
89 }
90 }
91 else if (IndexCell->Signature == CM_KEY_FAST_LEAF ||
92 IndexCell->Signature == CM_KEY_HASH_LEAF)
93 {
94 PCM_KEY_FAST_INDEX HashCell = (PCM_KEY_FAST_INDEX)IndexCell;
95 for (i = 0; i < HashCell->Count; i++)
96 {
97 PCM_KEY_NODE SubKeyCell = HvGetCell(RegistryHive, HashCell->List[i].Cell);
98 CmpPrepareKey(RegistryHive, SubKeyCell);
99 }
100 }
101 else
102 {
103 DPRINT1("IndexCell->Signature %x\n", IndexCell->Signature);
104 ASSERT(FALSE);
105 }
106 }
107
108 static VOID CMAPI
109 CmpPrepareKey(
110 PHHIVE RegistryHive,
111 PCM_KEY_NODE KeyCell)
112 {
113 PCM_KEY_INDEX IndexCell;
114
115 ASSERT(KeyCell->Signature == CM_KEY_NODE_SIGNATURE);
116
117 KeyCell->SubKeyLists[Volatile] = HCELL_NIL;
118 KeyCell->SubKeyCounts[Volatile] = 0;
119
120 /* Enumerate and add subkeys */
121 if (KeyCell->SubKeyCounts[Stable] > 0)
122 {
123 IndexCell = HvGetCell(RegistryHive, KeyCell->SubKeyLists[Stable]);
124 CmpPrepareIndexOfKeys(RegistryHive, IndexCell);
125 }
126 }
127
128 VOID CMAPI
129 CmPrepareHive(
130 PHHIVE RegistryHive)
131 {
132 PCM_KEY_NODE RootCell;
133
134 RootCell = HvGetCell(RegistryHive, RegistryHive->BaseBlock->RootCell);
135 CmpPrepareKey(RegistryHive, RootCell);
136 }