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