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