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