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