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