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