Revert 45697:
[reactos.git] / lib / cmlib / cmlib.h
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 #pragma once
9
10 //
11 // Debug support switch
12 //
13 #define _CMLIB_DEBUG_ 1
14
15 #ifdef CMLIB_HOST
16 #include <host/typedefs.h>
17 #include <stdio.h>
18 #include <string.h>
19
20 // Definitions copied from <ntstatus.h>
21 // We only want to include host headers, so we define them manually
22 #define STATUS_SUCCESS ((NTSTATUS)0x00000000)
23 #define STATUS_NOT_IMPLEMENTED ((NTSTATUS)0xC0000002)
24 #define STATUS_NO_MEMORY ((NTSTATUS)0xC0000017)
25 #define STATUS_INSUFFICIENT_RESOURCES ((NTSTATUS)0xC000009A)
26 #define STATUS_REGISTRY_CORRUPT ((NTSTATUS)0xC000014C)
27 #define STATUS_NOT_REGISTRY_FILE ((NTSTATUS)0xC000015C)
28 #define STATUS_REGISTRY_RECOVERED ((NTSTATUS)0x40000009)
29
30 #define REG_OPTION_VOLATILE 1
31 #define OBJ_CASE_INSENSITIVE 0x00000040L
32 #define USHORT_MAX USHRT_MAX
33
34 VOID NTAPI
35 KeQuerySystemTime(
36 OUT PLARGE_INTEGER CurrentTime);
37
38 VOID NTAPI
39 RtlInitializeBitMap(
40 IN PRTL_BITMAP BitMapHeader,
41 IN PULONG BitMapBuffer,
42 IN ULONG SizeOfBitMap);
43
44 ULONG NTAPI
45 RtlFindSetBits(
46 IN PRTL_BITMAP BitMapHeader,
47 IN ULONG NumberToFind,
48 IN ULONG HintIndex);
49
50 VOID NTAPI
51 RtlSetBits(
52 IN PRTL_BITMAP BitMapHeader,
53 IN ULONG StartingIndex,
54 IN ULONG NumberToSet);
55
56 VOID NTAPI
57 RtlClearAllBits(
58 IN PRTL_BITMAP BitMapHeader);
59
60 #define RtlCheckBit(BMH,BP) (((((PLONG)(BMH)->Buffer)[(BP) / 32]) >> ((BP) % 32)) & 0x1)
61 #define UNREFERENCED_PARAMETER(P) {(P)=(P);}
62
63 #define PKTHREAD PVOID
64 #define PKGUARDED_MUTEX PVOID
65 #define PERESOURCE PVOID
66 #define PFILE_OBJECT PVOID
67 #define PKEVENT PVOID
68 #define PWORK_QUEUE_ITEM PVOID
69 #define EX_PUSH_LOCK PULONG_PTR
70
71 /* For <host/wcsfuncs.h> */
72 #define USE_HOST_WCSFUNCS
73
74 #define CMLTRACE(x, ...)
75 #else
76 //
77 // Debug/Tracing support
78 //
79 #if _CMLIB_DEBUG_
80 #ifdef NEW_DEBUG_SYSTEM_IMPLEMENTED // enable when Debug Filters are implemented
81 #define CMLTRACE DbgPrintEx
82 #else
83 #define CMLTRACE(x, ...) \
84 if (x & CmlibTraceLevel) DbgPrint(__VA_ARGS__)
85 #endif
86 #else
87 #define CMLTRACE(x, ...) DPRINT(__VA_ARGS__)
88 #endif
89
90 #include <ntdef.h>
91 #undef DECLSPEC_IMPORT
92 #define DECLSPEC_IMPORT
93 #include <ntddk.h>
94 #endif
95
96 #include <host/wcsfuncs.h>
97
98 //
99 // These define the Debug Masks Supported
100 //
101 #define CMLIB_HCELL_DEBUG 0x01
102
103 #ifndef ROUND_UP
104 #define ROUND_UP(a,b) ((((a)+(b)-1)/(b))*(b))
105 #define ROUND_DOWN(a,b) (((a)/(b))*(b))
106 #endif
107
108 //
109 // PAGE_SIZE definition
110 //
111 #ifndef PAGE_SIZE
112 #if defined(TARGET_i386) || defined(TARGET_amd64) || defined(TARGET_arm)
113 #define PAGE_SIZE 0x1000
114 #else
115 #error Local PAGE_SIZE definition required when built as host
116 #endif
117 #endif
118
119 #define TAG_CM 0x68742020
120
121 #define CMAPI NTAPI
122
123 #include <wchar.h>
124 #include "hivedata.h"
125 #include "cmdata.h"
126
127 #if defined(_TYPEDEFS_HOST_H) || defined(__FREELDR_H)
128
129 #define PCM_KEY_SECURITY_CACHE_ENTRY PVOID
130 #define PCM_KEY_CONTROL_BLOCK PVOID
131 #define CMP_SECURITY_HASH_LISTS 64
132 #define PCM_CELL_REMAP_BLOCK PVOID
133
134 //
135 // Use Count Log and Entry
136 //
137 typedef struct _CM_USE_COUNT_LOG_ENTRY
138 {
139 HCELL_INDEX Cell;
140 PVOID Stack[7];
141 } CM_USE_COUNT_LOG_ENTRY, *PCM_USE_COUNT_LOG_ENTRY;
142
143 typedef struct _CM_USE_COUNT_LOG
144 {
145 USHORT Next;
146 USHORT Size;
147 CM_USE_COUNT_LOG_ENTRY Log[32];
148 } CM_USE_COUNT_LOG, *PCM_USE_COUNT_LOG;
149
150 //
151 // Configuration Manager Hive Structure
152 //
153 typedef struct _CMHIVE
154 {
155 HHIVE Hive;
156 HANDLE FileHandles[3];
157 LIST_ENTRY NotifyList;
158 LIST_ENTRY HiveList;
159 EX_PUSH_LOCK HiveLock;
160 PKTHREAD HiveLockOwner;
161 PKGUARDED_MUTEX ViewLock;
162 PKTHREAD ViewLockOwner;
163 EX_PUSH_LOCK WriterLock;
164 PKTHREAD WriterLockOwner;
165 PERESOURCE FlusherLock;
166 EX_PUSH_LOCK SecurityLock;
167 PKTHREAD HiveSecurityLockOwner;
168 LIST_ENTRY LRUViewListHead;
169 LIST_ENTRY PinViewListHead;
170 PFILE_OBJECT FileObject;
171 UNICODE_STRING FileFullPath;
172 UNICODE_STRING FileUserName;
173 USHORT MappedViews;
174 USHORT PinnedViews;
175 ULONG UseCount;
176 ULONG SecurityCount;
177 ULONG SecurityCacheSize;
178 LONG SecurityHitHint;
179 PCM_KEY_SECURITY_CACHE_ENTRY SecurityCache;
180 LIST_ENTRY SecurityHash[CMP_SECURITY_HASH_LISTS];
181 PKEVENT UnloadEvent;
182 PCM_KEY_CONTROL_BLOCK RootKcb;
183 BOOLEAN Frozen;
184 PWORK_QUEUE_ITEM UnloadWorkItem;
185 BOOLEAN GrowOnlyMode;
186 ULONG GrowOffset;
187 LIST_ENTRY KcbConvertListHead;
188 LIST_ENTRY KnodeConvertListHead;
189 PCM_CELL_REMAP_BLOCK CellRemapArray;
190 CM_USE_COUNT_LOG UseCountLog;
191 CM_USE_COUNT_LOG LockHiveLog;
192 ULONG Flags;
193 LIST_ENTRY TrustClassEntry;
194 ULONG FlushCount;
195 BOOLEAN HiveIsLoading;
196 PKTHREAD CreatorOwner;
197 } CMHIVE, *PCMHIVE;
198
199 #endif
200
201 extern ULONG CmlibTraceLevel;
202
203 /*
204 * Public functions.
205 */
206 NTSTATUS CMAPI
207 HvInitialize(
208 PHHIVE RegistryHive,
209 ULONG Operation,
210 ULONG HiveType,
211 ULONG HiveFlags,
212 PVOID HiveData OPTIONAL,
213 PALLOCATE_ROUTINE Allocate,
214 PFREE_ROUTINE Free,
215 PFILE_SET_SIZE_ROUTINE FileSetSize,
216 PFILE_WRITE_ROUTINE FileWrite,
217 PFILE_READ_ROUTINE FileRead,
218 PFILE_FLUSH_ROUTINE FileFlush,
219 ULONG Cluster OPTIONAL,
220 PUNICODE_STRING FileName);
221
222 VOID CMAPI
223 HvFree(
224 PHHIVE RegistryHive);
225
226 PVOID CMAPI
227 HvGetCell(
228 PHHIVE RegistryHive,
229 HCELL_INDEX CellOffset);
230
231 #define HvReleaseCell(h, c) \
232 if (h->ReleaseCellRoutine) h->ReleaseCellRoutine(h, c)
233
234 LONG CMAPI
235 HvGetCellSize(
236 PHHIVE RegistryHive,
237 PVOID Cell);
238
239 HCELL_INDEX CMAPI
240 HvAllocateCell(
241 PHHIVE RegistryHive,
242 SIZE_T Size,
243 HSTORAGE_TYPE Storage,
244 IN HCELL_INDEX Vicinity);
245
246 BOOLEAN CMAPI
247 HvIsCellAllocated(
248 IN PHHIVE RegistryHive,
249 IN HCELL_INDEX CellIndex
250 );
251
252 HCELL_INDEX CMAPI
253 HvReallocateCell(
254 PHHIVE RegistryHive,
255 HCELL_INDEX CellOffset,
256 ULONG Size);
257
258 VOID CMAPI
259 HvFreeCell(
260 PHHIVE RegistryHive,
261 HCELL_INDEX CellOffset);
262
263 BOOLEAN CMAPI
264 HvMarkCellDirty(
265 PHHIVE RegistryHive,
266 HCELL_INDEX CellOffset,
267 BOOLEAN HoldingLock);
268
269 BOOLEAN CMAPI
270 HvIsCellDirty(
271 IN PHHIVE Hive,
272 IN HCELL_INDEX Cell
273 );
274
275 BOOLEAN CMAPI
276 HvSyncHive(
277 PHHIVE RegistryHive);
278
279 BOOLEAN CMAPI
280 HvWriteHive(
281 PHHIVE RegistryHive);
282
283 BOOLEAN CMAPI
284 CmCreateRootNode(
285 PHHIVE Hive,
286 PCWSTR Name);
287
288 VOID CMAPI
289 CmPrepareHive(
290 PHHIVE RegistryHive);
291
292 /*
293 * Private functions.
294 */
295
296 PHBIN CMAPI
297 HvpAddBin(
298 PHHIVE RegistryHive,
299 ULONG Size,
300 HSTORAGE_TYPE Storage);
301
302 NTSTATUS CMAPI
303 HvpCreateHiveFreeCellList(
304 PHHIVE Hive);
305
306 ULONG CMAPI
307 HvpHiveHeaderChecksum(
308 PHBASE_BLOCK HiveHeader);