a5a0b473abb7580bf140f050e8591b17b89676cf
[reactos.git] / reactos / 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 //
9 // Debug support switch
10 //
11 #define _CMLIB_DEBUG_ 1
12
13 #ifdef CMLIB_HOST
14 #include <typedefs.h>
15 #include <stdio.h>
16 #include <string.h>
17
18 #ifdef _WIN32
19 #define strncasecmp _strnicmp
20 #define strcasecmp _stricmp
21 #endif//_WIN32
22
23 // Definitions copied from <ntstatus.h>
24 // We only want to include host headers, so we define them manually
25 #define STATUS_SUCCESS ((NTSTATUS)0x00000000)
26 #define STATUS_NOT_IMPLEMENTED ((NTSTATUS)0xC0000002)
27 #define STATUS_NO_MEMORY ((NTSTATUS)0xC0000017)
28 #define STATUS_INSUFFICIENT_RESOURCES ((NTSTATUS)0xC000009A)
29 #define STATUS_REGISTRY_CORRUPT ((NTSTATUS)0xC000014C)
30 #define STATUS_NOT_REGISTRY_FILE ((NTSTATUS)0xC000015C)
31 #define STATUS_REGISTRY_RECOVERED ((NTSTATUS)0x40000009)
32
33 #define REG_OPTION_VOLATILE 1
34 #define OBJ_CASE_INSENSITIVE 0x00000040L
35 #define USHORT_MAX USHRT_MAX
36
37 VOID NTAPI
38 KeQuerySystemTime(
39 OUT PLARGE_INTEGER CurrentTime);
40
41 VOID NTAPI
42 RtlInitializeBitMap(
43 IN PRTL_BITMAP BitMapHeader,
44 IN PULONG BitMapBuffer,
45 IN ULONG SizeOfBitMap);
46
47 ULONG NTAPI
48 RtlFindSetBits(
49 IN PRTL_BITMAP BitMapHeader,
50 IN ULONG NumberToFind,
51 IN ULONG HintIndex);
52
53 VOID NTAPI
54 RtlSetBits(
55 IN PRTL_BITMAP BitMapHeader,
56 IN ULONG StartingIndex,
57 IN ULONG NumberToSet);
58
59 VOID NTAPI
60 RtlClearAllBits(
61 IN PRTL_BITMAP BitMapHeader);
62
63 #define RtlCheckBit(BMH,BP) (((((PLONG)(BMH)->Buffer)[(BP) / 32]) >> ((BP) % 32)) & 0x1)
64 #define UNREFERENCED_PARAMETER(P) {(P)=(P);}
65
66 #define PKTHREAD PVOID
67 #define PKGUARDED_MUTEX PVOID
68 #define PERESOURCE PVOID
69 #define PFILE_OBJECT PVOID
70 #define PKEVENT PVOID
71 #define PWORK_QUEUE_ITEM PVOID
72 #define EX_PUSH_LOCK PULONG_PTR
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 #include <ntddk.h>
92
93 /* Prevent inclusion of Windows headers through <wine/unicode.h> */
94 #define _WINDEF_
95 #define _WINBASE_
96 #define _WINNLS_
97 #endif
98
99
100 //
101 // These define the Debug Masks Supported
102 //
103 #define CMLIB_HCELL_DEBUG 0x01
104
105 #ifndef ROUND_UP
106 #define ROUND_UP(a,b) ((((a)+(b)-1)/(b))*(b))
107 #define ROUND_DOWN(a,b) (((a)/(b))*(b))
108 #endif
109
110 //
111 // PAGE_SIZE definition
112 //
113 #ifndef PAGE_SIZE
114 #if defined(TARGET_i386) || defined(TARGET_amd64) || defined(TARGET_arm)
115 #define PAGE_SIZE 0x1000
116 #else
117 #error Local PAGE_SIZE definition required when built as host
118 #endif
119 #endif
120
121 #define TAG_CM 'CM25'
122
123 #define CMAPI NTAPI
124
125 #include <wine/unicode.h>
126 #include <wchar.h>
127 #include "hivedata.h"
128 #include "cmdata.h"
129
130 #if defined(_TYPEDEFS_HOST_H) || defined(__FREELDR_H)
131
132 #define PCM_KEY_SECURITY_CACHE_ENTRY PVOID
133 #define PCM_KEY_CONTROL_BLOCK PVOID
134 #define CMP_SECURITY_HASH_LISTS 64
135 #define PCM_CELL_REMAP_BLOCK PVOID
136
137 //
138 // Use Count Log and Entry
139 //
140 typedef struct _CM_USE_COUNT_LOG_ENTRY
141 {
142 HCELL_INDEX Cell;
143 PVOID Stack[7];
144 } CM_USE_COUNT_LOG_ENTRY, *PCM_USE_COUNT_LOG_ENTRY;
145
146 typedef struct _CM_USE_COUNT_LOG
147 {
148 USHORT Next;
149 USHORT Size;
150 CM_USE_COUNT_LOG_ENTRY Log[32];
151 } CM_USE_COUNT_LOG, *PCM_USE_COUNT_LOG;
152
153 //
154 // Configuration Manager Hive Structure
155 //
156 typedef struct _CMHIVE
157 {
158 HHIVE Hive;
159 HANDLE FileHandles[HFILE_TYPE_MAX];
160 LIST_ENTRY NotifyList;
161 LIST_ENTRY HiveList;
162 EX_PUSH_LOCK HiveLock;
163 PKTHREAD HiveLockOwner;
164 PKGUARDED_MUTEX ViewLock;
165 PKTHREAD ViewLockOwner;
166 EX_PUSH_LOCK WriterLock;
167 PKTHREAD WriterLockOwner;
168 PERESOURCE FlusherLock;
169 EX_PUSH_LOCK SecurityLock;
170 PKTHREAD HiveSecurityLockOwner;
171 LIST_ENTRY LRUViewListHead;
172 LIST_ENTRY PinViewListHead;
173 PFILE_OBJECT FileObject;
174 UNICODE_STRING FileFullPath;
175 UNICODE_STRING FileUserName;
176 USHORT MappedViews;
177 USHORT PinnedViews;
178 ULONG UseCount;
179 ULONG SecurityCount;
180 ULONG SecurityCacheSize;
181 LONG SecurityHitHint;
182 PCM_KEY_SECURITY_CACHE_ENTRY SecurityCache;
183 LIST_ENTRY SecurityHash[CMP_SECURITY_HASH_LISTS];
184 PKEVENT UnloadEvent;
185 PCM_KEY_CONTROL_BLOCK RootKcb;
186 BOOLEAN Frozen;
187 PWORK_QUEUE_ITEM UnloadWorkItem;
188 BOOLEAN GrowOnlyMode;
189 ULONG GrowOffset;
190 LIST_ENTRY KcbConvertListHead;
191 LIST_ENTRY KnodeConvertListHead;
192 PCM_CELL_REMAP_BLOCK CellRemapArray;
193 CM_USE_COUNT_LOG UseCountLog;
194 CM_USE_COUNT_LOG LockHiveLog;
195 ULONG Flags;
196 LIST_ENTRY TrustClassEntry;
197 ULONG FlushCount;
198 BOOLEAN HiveIsLoading;
199 PKTHREAD CreatorOwner;
200 } CMHIVE, *PCMHIVE;
201
202 #endif
203
204 typedef struct _HV_HIVE_CELL_PAIR
205 {
206 PHHIVE Hive;
207 HCELL_INDEX Cell;
208 } HV_HIVE_CELL_PAIR, *PHV_HIVE_CELL_PAIR;
209
210 #define STATIC_CELL_PAIR_COUNT 4
211 typedef struct _HV_TRACK_CELL_REF
212 {
213 USHORT Count;
214 USHORT Max;
215 PHV_HIVE_CELL_PAIR CellArray;
216 HV_HIVE_CELL_PAIR StaticArray[STATIC_CELL_PAIR_COUNT];
217 USHORT StaticCount;
218 } HV_TRACK_CELL_REF, *PHV_TRACK_CELL_REF;
219
220 extern ULONG CmlibTraceLevel;
221
222 /*
223 * Public functions.
224 */
225 NTSTATUS CMAPI
226 HvInitialize(
227 PHHIVE RegistryHive,
228 ULONG Operation,
229 ULONG HiveType,
230 ULONG HiveFlags,
231 PVOID HiveData OPTIONAL,
232 PALLOCATE_ROUTINE Allocate,
233 PFREE_ROUTINE Free,
234 PFILE_SET_SIZE_ROUTINE FileSetSize,
235 PFILE_WRITE_ROUTINE FileWrite,
236 PFILE_READ_ROUTINE FileRead,
237 PFILE_FLUSH_ROUTINE FileFlush,
238 ULONG Cluster OPTIONAL,
239 PUNICODE_STRING FileName);
240
241 VOID CMAPI
242 HvFree(
243 PHHIVE RegistryHive);
244
245 PVOID CMAPI
246 HvGetCell(
247 PHHIVE RegistryHive,
248 HCELL_INDEX CellOffset);
249
250 #define HvReleaseCell(h, c) \
251 do { \
252 if ((h)->ReleaseCellRoutine) \
253 (h)->ReleaseCellRoutine(h, c); \
254 } while(0)
255
256 LONG CMAPI
257 HvGetCellSize(
258 PHHIVE RegistryHive,
259 PVOID Cell);
260
261 HCELL_INDEX CMAPI
262 HvAllocateCell(
263 PHHIVE RegistryHive,
264 ULONG Size,
265 HSTORAGE_TYPE Storage,
266 IN HCELL_INDEX Vicinity);
267
268 BOOLEAN CMAPI
269 HvIsCellAllocated(
270 IN PHHIVE RegistryHive,
271 IN HCELL_INDEX CellIndex
272 );
273
274 HCELL_INDEX CMAPI
275 HvReallocateCell(
276 PHHIVE RegistryHive,
277 HCELL_INDEX CellOffset,
278 ULONG Size);
279
280 VOID CMAPI
281 HvFreeCell(
282 PHHIVE RegistryHive,
283 HCELL_INDEX CellOffset);
284
285 BOOLEAN CMAPI
286 HvMarkCellDirty(
287 PHHIVE RegistryHive,
288 HCELL_INDEX CellOffset,
289 BOOLEAN HoldingLock);
290
291 BOOLEAN CMAPI
292 HvIsCellDirty(
293 IN PHHIVE Hive,
294 IN HCELL_INDEX Cell
295 );
296
297 BOOLEAN
298 CMAPI
299 HvHiveWillShrink(
300 IN PHHIVE RegistryHive
301 );
302
303 BOOLEAN CMAPI
304 HvSyncHive(
305 PHHIVE RegistryHive);
306
307 BOOLEAN CMAPI
308 HvWriteHive(
309 PHHIVE RegistryHive);
310
311 BOOLEAN CMAPI
312 CmCreateRootNode(
313 PHHIVE Hive,
314 PCWSTR Name);
315
316 VOID CMAPI
317 CmPrepareHive(
318 PHHIVE RegistryHive);
319
320 BOOLEAN
321 NTAPI
322 CmCompareHash(
323 IN PCUNICODE_STRING KeyName,
324 IN PCHAR HashString,
325 IN BOOLEAN CaseInsensitive);
326
327 BOOLEAN
328 NTAPI
329 CmComparePackedNames(
330 IN PCUNICODE_STRING Name,
331 IN PVOID NameBuffer,
332 IN USHORT NameBufferSize,
333 IN BOOLEAN NamePacked,
334 IN BOOLEAN CaseInsensitive);
335
336 BOOLEAN
337 NTAPI
338 CmCompareKeyName(
339 IN PCM_KEY_NODE KeyCell,
340 IN PCUNICODE_STRING KeyName,
341 IN BOOLEAN CaseInsensitive);
342
343 BOOLEAN
344 NTAPI
345 CmCompareKeyValueName(
346 IN PCM_KEY_VALUE ValueCell,
347 IN PCUNICODE_STRING KeyName,
348 IN BOOLEAN CaseInsensitive);
349
350 ULONG
351 NTAPI
352 CmCopyKeyName(
353 IN PCM_KEY_NODE KeyNode,
354 _Out_ PWCHAR KeyNameBuffer,
355 _Inout_ ULONG BufferLength);
356
357 ULONG
358 NTAPI
359 CmCopyKeyValueName(
360 _In_ PCM_KEY_VALUE ValueCell,
361 _Out_ PWCHAR ValueNameBuffer,
362 _Inout_ ULONG BufferLength);
363
364 BOOLEAN
365 CMAPI
366 HvTrackCellRef(
367 PHV_TRACK_CELL_REF CellRef,
368 PHHIVE Hive,
369 HCELL_INDEX Cell
370 );
371
372 VOID
373 CMAPI
374 HvReleaseFreeCellRefArray(
375 PHV_TRACK_CELL_REF CellRef
376 );
377
378 /*
379 * Private functions.
380 */
381
382 PHBIN CMAPI
383 HvpAddBin(
384 PHHIVE RegistryHive,
385 ULONG Size,
386 HSTORAGE_TYPE Storage);
387
388 NTSTATUS CMAPI
389 HvpCreateHiveFreeCellList(
390 PHHIVE Hive);
391
392 ULONG CMAPI
393 HvpHiveHeaderChecksum(
394 PHBASE_BLOCK HiveHeader);