xref: /reactos/sdk/lib/cmlib/hivedata.h (revision f3141fb2)
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 // Hive operations
12 //
13 #define HINIT_CREATE                    0
14 #define HINIT_MEMORY                    1
15 #define HINIT_FILE                      2
16 #define HINIT_MEMORY_INPLACE            3
17 #define HINIT_FLAT                      4
18 #define HINIT_MAPFILE                   5
19 
20 //
21 // Hive flags
22 //
23 #define HIVE_VOLATILE                   1
24 #define HIVE_NOLAZYFLUSH                2
25 #define HIVE_HAS_BEEN_REPLACED          4
26 #define HIVE_HAS_BEEN_FREED             8
27 #define HIVE_UNKNOWN                    0x10
28 #define HIVE_IS_UNLOADING               0x20
29 
30 //
31 // Hive types
32 //
33 #define HFILE_TYPE_PRIMARY              0
34 #define HFILE_TYPE_LOG                  1
35 #define HFILE_TYPE_EXTERNAL             2
36 #define HFILE_TYPE_ALTERNATE            3 // Technically a HFILE_TYPE_PRIMARY but for mirror backup hives. ONLY USED for the SYSTEM hive!
37 #define HFILE_TYPE_MAX                  4
38 
39 //
40 // Hive sizes
41 //
42 #define HBLOCK_SIZE                     0x1000
43 #define HSECTOR_SIZE                    0x200
44 #define HSECTOR_COUNT                   8
45 
46 #define HV_LOG_HEADER_SIZE              FIELD_OFFSET(HBASE_BLOCK, Reserved2)
47 
48 //
49 // Clean Block identifier
50 //
51 #define HV_CLEAN_BLOCK 0U
52 
53 //
54 // Hive Log identifiers
55 //
56 #define HV_LOG_DIRTY_BLOCK 0xFF
57 #define HV_LOG_DIRTY_SIGNATURE 0x54524944 // "DIRT"
58 
59 //
60 // Hive structure identifiers
61 //
62 #define HV_HHIVE_SIGNATURE              0xbee0bee0
63 #define HV_HBLOCK_SIGNATURE             0x66676572  // "regf"
64 #define HV_HBIN_SIGNATURE               0x6e696268  // "hbin"
65 
66 //
67 // Hive versions
68 //
69 #define HSYS_MAJOR                      1
70 #define HSYS_MINOR                      3
71 #define HSYS_WHISTLER_BETA1             4
72 #define HSYS_WHISTLER                   5
73 #define HSYS_MINOR_SUPPORTED            HSYS_WHISTLER
74 
75 //
76 // Hive formats
77 //
78 #define HBASE_FORMAT_MEMORY             1
79 
80 //
81 // Hive storage
82 //
83 #define HTYPE_COUNT                     2
84 
85 //
86 // Hive boot types
87 //
88 #define HBOOT_TYPE_REGULAR         0
89 #define HBOOT_TYPE_SELF_HEAL       4
90 
91 //
92 // Hive boot recover types
93 //
94 #define HBOOT_NO_BOOT_RECOVER                   0
95 #define HBOOT_BOOT_RECOVERED_BY_HIVE_LOG        1
96 #define HBOOT_BOOT_RECOVERED_BY_ALTERNATE_HIVE  2
97 
98 /**
99  * @name HCELL_INDEX
100  *
101  * A handle to cell index. The highest bit specifies the cell storage and
102  * the other bits specify index into the hive file. The value HCELL_NULL
103  * (-1) is reserved for marking invalid cells.
104  */
105 typedef ULONG HCELL_INDEX, *PHCELL_INDEX;
106 
107 //
108 // Cell Magic Values
109 //
110 #define HCELL_NIL                       MAXULONG
111 #define HCELL_CACHED                    1
112 
113 #define HCELL_TYPE_MASK                 0x80000000
114 #define HCELL_BLOCK_MASK                0x7ffff000
115 #define HCELL_OFFSET_MASK               0x00000fff
116 #define HCELL_TYPE_SHIFT                31
117 #define HCELL_BLOCK_SHIFT               12
118 #define HCELL_OFFSET_SHIFT              0
119 
120 #define HvGetCellType(Cell)             \
121     ((ULONG)(((Cell) & HCELL_TYPE_MASK) >> HCELL_TYPE_SHIFT))
122 #define HvGetCellBlock(Cell)            \
123     ((ULONG)(((Cell) & HCELL_BLOCK_MASK) >> HCELL_BLOCK_SHIFT))
124 
125 typedef enum
126 {
127     Stable   = 0,
128     Volatile = 1
129 } HSTORAGE_TYPE;
130 
131 #include <pshpack1.h>
132 
133 /**
134  * @name HBASE_BLOCK
135  *
136  * On-disk header for registry hive file.
137  */
138 
139 #define HIVE_FILENAME_MAXLEN            31
140 
141 typedef struct _HBASE_BLOCK
142 {
143     /* Hive base block identifier "regf" (0x66676572) */
144     ULONG Signature;
145 
146     /* Update counters */
147     ULONG Sequence1;
148     ULONG Sequence2;
149 
150     /* When this hive file was last modified */
151     LARGE_INTEGER TimeStamp;
152 
153     /* Registry format major version (1) */
154     ULONG Major;
155 
156     /* Registry format minor version (3)
157        Version 3 added fast indexes, version 5 has large value optimizations */
158     ULONG Minor;
159 
160     /* Registry file type (0 - Primary, 1 - Log) */
161     ULONG Type;
162 
163     /* Registry format (1 is the only defined value so far) */
164     ULONG Format;
165 
166     /* Offset into file from the byte after the end of the base block.
167        If the hive is volatile, this is the actual pointer to the CM_KEY_NODE */
168     HCELL_INDEX RootCell;
169 
170     /* Size in bytes of the full hive, minus the header, multiple of the block size (4KB) */
171     ULONG Length;
172 
173     /* (1?) */
174     ULONG Cluster;
175 
176     /* Last 31 UNICODE characters, plus terminating NULL character,
177        of the full name of the hive file */
178     WCHAR FileName[HIVE_FILENAME_MAXLEN + 1];
179 
180     ULONG Reserved1[99];
181 
182     /* Checksum of first 0x200 bytes */
183     ULONG CheckSum;
184 
185     ULONG Reserved2[0x37E];
186     ULONG BootType;
187     ULONG BootRecover;
188 } HBASE_BLOCK, *PHBASE_BLOCK;
189 
190 C_ASSERT(sizeof(HBASE_BLOCK) == HBLOCK_SIZE);
191 
192 typedef struct _HBIN
193 {
194     /* Hive bin identifier "hbin" (0x6E696268) */
195     ULONG Signature;
196 
197     /* Block offset of this bin */
198     HCELL_INDEX FileOffset;
199 
200     /* Size in bytes of this bin, multiple of the block size (4KB) */
201     ULONG Size;
202 
203     ULONG Reserved1[2];
204 
205     /* When this bin was last modified */
206     LARGE_INTEGER TimeStamp;
207 
208     /* Unused (In-memory only) */
209     ULONG Spare;
210 } HBIN, *PHBIN;
211 
212 typedef struct _HCELL
213 {
214     /* <0 if used, >0 if free */
215     LONG Size;
216 } HCELL, *PHCELL;
217 
218 #include <poppack.h>
219 
220 struct _HHIVE;
221 
222 typedef struct _CELL_DATA*
223 (CMAPI *PGET_CELL_ROUTINE)(
224     struct _HHIVE *Hive,
225     HCELL_INDEX Cell
226 );
227 
228 typedef VOID
229 (CMAPI *PRELEASE_CELL_ROUTINE)(
230     struct _HHIVE *Hive,
231     HCELL_INDEX Cell
232 );
233 
234 typedef PVOID
235 (CMAPI *PALLOCATE_ROUTINE)(
236     SIZE_T Size,
237     BOOLEAN Paged,
238     ULONG Tag
239 );
240 
241 typedef VOID
242 (CMAPI *PFREE_ROUTINE)(
243     PVOID Ptr,
244     ULONG Quota
245 );
246 
247 typedef BOOLEAN
248 (CMAPI *PFILE_READ_ROUTINE)(
249     struct _HHIVE *RegistryHive,
250     ULONG FileType,
251     PULONG FileOffset,
252     PVOID Buffer,
253     SIZE_T BufferLength
254 );
255 
256 typedef BOOLEAN
257 (CMAPI *PFILE_WRITE_ROUTINE)(
258     struct _HHIVE *RegistryHive,
259     ULONG FileType,
260     PULONG FileOffset,
261     PVOID Buffer,
262     SIZE_T BufferLength
263 );
264 
265 typedef BOOLEAN
266 (CMAPI *PFILE_SET_SIZE_ROUTINE)(
267     struct _HHIVE *RegistryHive,
268     ULONG FileType,
269     ULONG FileSize,
270     ULONG OldfileSize
271 );
272 
273 typedef BOOLEAN
274 (CMAPI *PFILE_FLUSH_ROUTINE)(
275     struct _HHIVE *RegistryHive,
276     ULONG FileType,
277     PLARGE_INTEGER FileOffset,
278     ULONG Length
279 );
280 
281 typedef struct _HMAP_ENTRY
282 {
283     ULONG_PTR BlockAddress;
284     ULONG_PTR BinAddress;
285     struct _CM_VIEW_OF_FILE *CmView;
286     ULONG MemAlloc;
287 } HMAP_ENTRY, *PHMAP_ENTRY;
288 
289 typedef struct _HMAP_TABLE
290 {
291     HMAP_ENTRY Table[512];
292 } HMAP_TABLE, *PHMAP_TABLE;
293 
294 typedef struct _HMAP_DIRECTORY
295 {
296     PHMAP_TABLE Directory[2048];
297 } HMAP_DIRECTORY, *PHMAP_DIRECTORY;
298 
299 typedef struct _DUAL
300 {
301     ULONG Length;
302     PHMAP_DIRECTORY Map;
303     PHMAP_ENTRY BlockList; // PHMAP_TABLE SmallDir;
304     ULONG Guard;
305     HCELL_INDEX FreeDisplay[24]; // FREE_DISPLAY FreeDisplay[24];
306     ULONG FreeSummary;
307     LIST_ENTRY FreeBins;
308 } DUAL, *PDUAL;
309 
310 typedef struct _HHIVE
311 {
312     /* Hive identifier (0xBEE0BEE0) */
313     ULONG Signature;
314 
315     /* Callbacks */
316     PGET_CELL_ROUTINE GetCellRoutine;
317     PRELEASE_CELL_ROUTINE ReleaseCellRoutine;
318     PALLOCATE_ROUTINE Allocate;
319     PFREE_ROUTINE Free;
320     PFILE_SET_SIZE_ROUTINE FileSetSize;
321     PFILE_WRITE_ROUTINE FileWrite;
322     PFILE_READ_ROUTINE FileRead;
323     PFILE_FLUSH_ROUTINE FileFlush;
324 
325 #if (NTDDI_VERSION >= NTDDI_WIN7)
326     PVOID HiveLoadFailure; // PHIVE_LOAD_FAILURE
327 #endif
328     PHBASE_BLOCK BaseBlock;
329     RTL_BITMAP DirtyVector;
330     ULONG DirtyCount;
331     ULONG DirtyAlloc;
332     ULONG BaseBlockAlloc;
333     ULONG Cluster;
334     BOOLEAN Flat;
335     BOOLEAN ReadOnly;
336 #if (NTDDI_VERSION < NTDDI_VISTA) // NTDDI_LONGHORN
337     BOOLEAN Log;
338     BOOLEAN Alternate;
339 #endif
340     BOOLEAN DirtyFlag;
341 #if (NTDDI_VERSION >= NTDDI_VISTA) // NTDDI_LONGHORN
342     ULONG HvBinHeadersUse;
343     ULONG HvFreeCellsUse;
344     ULONG HvUsedCellsUse;
345     ULONG CmUsedCellsUse;
346 #endif
347     ULONG HiveFlags;
348 #if (NTDDI_VERSION < NTDDI_VISTA) // NTDDI_LONGHORN
349     ULONG LogSize;
350 #else
351     ULONG CurrentLog;
352     ULONG LogSize[2];
353 #endif
354     ULONG RefreshCount;
355     ULONG StorageTypeCount;
356     ULONG Version;
357     DUAL Storage[HTYPE_COUNT];
358 } HHIVE, *PHHIVE;
359 
360 #define IsFreeCell(Cell)    ((Cell)->Size >= 0)
361 #define IsUsedCell(Cell)    ((Cell)->Size <  0)
362