3 * Copyright (C) 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* $Id: binhive.c,v 1.2 2003/04/16 15:06:33 ekohl Exp $
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS hive maker
22 * FILE: tools/mkhive/binhive.c
23 * PURPOSE: Binary hive export code
24 * PROGRAMMER: Eric Kohl
27 /* INCLUDES *****************************************************************/
38 #define REG_HIVE_ID 0x66676572
39 #define REG_BIN_ID 0x6e696268
40 #define REG_KEY_CELL_ID 0x6b6e
41 #define REG_HASH_TABLE_BLOCK_ID 0x666c
42 #define REG_VALUE_CELL_ID 0x6b76
44 #define REG_BLOCK_SIZE 4096
45 #define REG_HBIN_DATA_OFFSET 32
46 #define REG_INIT_BLOCK_LIST_SIZE 32
47 #define REG_INIT_HASH_TABLE_SIZE 3
48 #define REG_EXTEND_HASH_TABLE_SIZE 4
49 #define REG_VALUE_LIST_CELL_MULTIPLE 4
51 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
52 #define ROUND_DOWN(N, S) ((N) - ((N) % (S)))
54 #define ABS_VALUE(V) (((V) < 0) ? -(V) : (V))
57 // BLOCK_OFFSET = offset in file after header block
58 typedef ULONG BLOCK_OFFSET
, *PBLOCK_OFFSET
;
60 typedef unsigned long long FILETIME
;
62 /* header for registry hive file : */
63 typedef struct _HIVE_HEADER
65 /* Hive identifier "regf" (0x66676572) */
74 /* When this hive file was last modified */
75 FILETIME DateModified
;
77 /* Registry format version ? (1?) */
80 /* Registry format version ? (3?) */
83 /* Registry format version ? (0?) */
86 /* Registry format version ? (1?) */
89 /* Offset into file from the byte after the end of the base block.
90 If the hive is volatile, this is the actual pointer to the KEY_CELL */
91 BLOCK_OFFSET RootKeyCell
;
93 /* Size of each hive block ? */
99 /* Name of hive file */
105 /* Checksum of first 0x200 bytes */
107 } __attribute__((packed
)) HIVE_HEADER
, *PHIVE_HEADER
;
111 /* Bin identifier "hbin" (0x6E696268) */
114 /* Block offset of this bin */
115 BLOCK_OFFSET BlockOffset
;
117 /* Size in bytes, multiple of the block size (4KB) */
123 /* When this bin was last modified */
124 FILETIME DateModified
;
128 } __attribute__((packed
)) HBIN
, *PHBIN
;
130 typedef struct _CELL_HEADER
132 /* <0 if used, >0 if free */
134 } __attribute__((packed
)) CELL_HEADER
, *PCELL_HEADER
;
136 typedef struct _KEY_CELL
138 /* Size of this cell */
141 /* Key cell identifier "kn" (0x6b6e) */
147 /* Time of last flush */
148 FILETIME LastWriteTime
;
153 /* Block offset of parent key cell */
154 BLOCK_OFFSET ParentKeyOffset
;
156 /* Count of sub keys for the key in this key cell */
157 ULONG NumberOfSubKeys
;
162 /* Block offset of has table for FIXME: subkeys/values? */
163 BLOCK_OFFSET HashTableOffset
;
168 /* Count of values contained in this key cell */
169 ULONG NumberOfValues
;
171 /* Block offset of VALUE_LIST_CELL */
172 BLOCK_OFFSET ValuesOffset
;
174 /* Block offset of security cell */
175 BLOCK_OFFSET SecurityKeyOffset
;
177 /* Block offset of registry key class */
178 BLOCK_OFFSET ClassNameOffset
;
183 /* Size in bytes of key name */
186 /* Size of class name in bytes */
189 /* Name of key (not zero terminated) */
191 } __attribute__((packed
)) KEY_CELL
, *PKEY_CELL
;
193 /* KEY_CELL.Type constants */
194 #define REG_LINK_KEY_CELL_TYPE 0x10
195 #define REG_KEY_CELL_TYPE 0x20
196 #define REG_ROOT_KEY_CELL_TYPE 0x2c
200 // HashValue=four letters of value's name
201 typedef struct _HASH_RECORD
203 BLOCK_OFFSET KeyOffset
;
205 } __attribute__((packed
)) HASH_RECORD
, *PHASH_RECORD
;
207 typedef struct _HASH_TABLE_CELL
211 USHORT HashTableSize
;
212 HASH_RECORD Table
[0];
213 } __attribute__((packed
)) HASH_TABLE_CELL
, *PHASH_TABLE_CELL
;
215 typedef struct _VALUE_LIST_CELL
218 BLOCK_OFFSET Values
[0];
219 } __attribute__((packed
)) VALUE_LIST_CELL
, *PVALUE_LIST_CELL
;
221 typedef struct _VALUE_CELL
225 USHORT NameSize
; // length of Name
226 LONG DataSize
; // length of datas in the cell pointed by DataOffset
227 BLOCK_OFFSET DataOffset
;// datas are here if high bit of DataSize is set
231 UCHAR Name
[0]; /* warning : not zero terminated */
232 } __attribute__((packed
)) VALUE_CELL
, *PVALUE_CELL
;
234 /* VALUE_CELL.Flags constants */
235 #define REG_VALUE_NAME_PACKED 0x0001
238 typedef struct _DATA_CELL
242 } __attribute__((packed
)) DATA_CELL
, *PDATA_CELL
;
244 typedef struct _REGISTRY_HIVE
247 PHIVE_HEADER HiveHeader
;
252 PCELL_HEADER
*FreeList
;
253 BLOCK_OFFSET
*FreeListOffset
;
254 } REGISTRY_HIVE
, *PREGISTRY_HIVE
;
256 /* FUNCTIONS ****************************************************************/
259 CmiCreateDefaultHiveHeader(PHIVE_HEADER Header
)
262 memset (Header
, 0, REG_BLOCK_SIZE
);
263 Header
->BlockId
= REG_HIVE_ID
;
264 Header
->UpdateCounter1
= 0;
265 Header
->UpdateCounter2
= 0;
266 Header
->DateModified
= 0ULL;
272 Header
->RootKeyCell
= 0;
273 Header
->BlockSize
= REG_BLOCK_SIZE
;
275 Header
->Checksum
= 0;
280 CmiCreateDefaultBinCell(PHBIN BinCell
)
283 memset (BinCell
, 0, REG_BLOCK_SIZE
);
284 BinCell
->BlockId
= REG_BIN_ID
;
285 BinCell
->DateModified
= 0ULL;
286 BinCell
->BlockSize
= REG_BLOCK_SIZE
;
291 CmiCreateDefaultRootKeyCell(PKEY_CELL RootKeyCell
)
294 memset (RootKeyCell
, 0, sizeof(KEY_CELL
));
295 RootKeyCell
->CellSize
= -sizeof(KEY_CELL
);
296 RootKeyCell
->Id
= REG_KEY_CELL_ID
;
297 RootKeyCell
->Type
= REG_ROOT_KEY_CELL_TYPE
;
298 RootKeyCell
->LastWriteTime
= 0ULL;
299 RootKeyCell
->ParentKeyOffset
= 0;
300 RootKeyCell
->NumberOfSubKeys
= 0;
301 RootKeyCell
->HashTableOffset
= -1;
302 RootKeyCell
->NumberOfValues
= 0;
303 RootKeyCell
->ValuesOffset
= -1;
304 RootKeyCell
->SecurityKeyOffset
= 0;
305 RootKeyCell
->ClassNameOffset
= -1;
306 RootKeyCell
->NameSize
= 0;
307 RootKeyCell
->ClassSize
= 0;
311 static PREGISTRY_HIVE
312 CmiCreateRegistryHive (VOID
)
315 PCELL_HEADER FreeCell
;
316 PKEY_CELL RootKeyCell
;
319 Hive
= (PREGISTRY_HIVE
) malloc (sizeof(REGISTRY_HIVE
));
324 memset (Hive
, 0, sizeof(REGISTRY_HIVE
));
326 DPRINT("Hive %x\n", Hive
);
328 /* Create hive beader (aka 'base block') */
329 Hive
->HiveHeader
= (PHIVE_HEADER
) malloc (REG_BLOCK_SIZE
);
330 if (Hive
->HiveHeader
== NULL
)
335 CmiCreateDefaultHiveHeader(Hive
->HiveHeader
);
336 Hive
->FileSize
= REG_BLOCK_SIZE
;
338 /* Allocate block list */
339 Hive
->BlockListSize
= 1;
340 Hive
->BlockList
= malloc (sizeof(PHBIN
) * Hive
->BlockListSize
);
341 if (Hive
->BlockList
== NULL
)
343 free (Hive
->HiveHeader
);
348 /* Allocate free cell list */
349 Hive
->FreeListMax
= 32;
350 Hive
->FreeList
= malloc(sizeof(PCELL_HEADER
) * Hive
->FreeListMax
);
351 if (Hive
->FreeList
== NULL
)
353 free (Hive
->BlockList
);
354 free (Hive
->HiveHeader
);
358 Hive
->FreeListOffset
= malloc(sizeof(BLOCK_OFFSET
) * Hive
->FreeListMax
);
359 if (Hive
->FreeListOffset
== NULL
)
361 free (Hive
->FreeList
);
362 free (Hive
->BlockList
);
363 free (Hive
->HiveHeader
);
368 /* Allocate first bin */
369 Hive
->BlockList
[0] = (PHBIN
) malloc (REG_BLOCK_SIZE
);
370 if (Hive
->BlockList
[0] == NULL
)
372 free (Hive
->FreeListOffset
);
373 free (Hive
->FreeList
);
374 free (Hive
->BlockList
);
375 free (Hive
->HiveHeader
);
379 Hive
->FileSize
+= REG_BLOCK_SIZE
;
382 BinCell
= (PHBIN
)Hive
->BlockList
[0];
383 CmiCreateDefaultBinCell(BinCell
);
384 BinCell
->BlockOffset
= 0;
386 /* Init root key cell */
387 RootKeyCell
= (PKEY_CELL
)((ULONG_PTR
)BinCell
+ REG_HBIN_DATA_OFFSET
);
388 CmiCreateDefaultRootKeyCell(RootKeyCell
);
389 Hive
->HiveHeader
->RootKeyCell
= REG_HBIN_DATA_OFFSET
;
392 FreeCell
= (PCELL_HEADER
)((ULONG_PTR
)RootKeyCell
+ sizeof(KEY_CELL
));
393 FreeCell
->CellSize
= REG_BLOCK_SIZE
- (REG_HBIN_DATA_OFFSET
+ sizeof(KEY_CELL
));
395 Hive
->FreeList
[0] = FreeCell
;
396 Hive
->FreeListOffset
[0] = REG_HBIN_DATA_OFFSET
+ sizeof(KEY_CELL
);
397 Hive
->FreeListSize
++;
404 CmiDestroyRegistryHive (PREGISTRY_HIVE Hive
)
412 /* Release free offset list */
413 if (Hive
->FreeListOffset
!= NULL
)
414 free (Hive
->FreeListOffset
);
416 /* Release free list */
417 if (Hive
->FreeList
!= NULL
)
418 free (Hive
->FreeList
);
420 if (Hive
->BlockList
!= NULL
)
424 for (i
= 0; i
< Hive
->BlockListSize
; i
++)
426 if ((Hive
->BlockList
[i
] != NULL
) &&
427 (Hive
->BlockList
[i
] != Bin
))
429 Bin
= Hive
->BlockList
[i
];
431 DPRINT ("Bin[%lu]: Offset 0x%lx Size 0x%lx\n",
432 i
, Bin
->BlockOffset
, Bin
->BlockSize
);
438 /* Release block list */
439 free (Hive
->BlockList
);
442 /* Release hive header */
443 if (Hive
->HiveHeader
!= NULL
)
444 free (Hive
->HiveHeader
);
452 CmiGetBlock(PREGISTRY_HIVE Hive
,
453 BLOCK_OFFSET BlockOffset
,
461 if ((BlockOffset
== 0) || (BlockOffset
== (ULONG_PTR
) -1))
464 pBin
= Hive
->BlockList
[BlockOffset
/ 4096];
468 return (PVOID
)((ULONG_PTR
)pBin
+ (BlockOffset
- pBin
->BlockOffset
));
473 CmiMergeFree(PREGISTRY_HIVE RegistryHive
,
474 PCELL_HEADER FreeBlock
,
475 BLOCK_OFFSET FreeOffset
)
477 BLOCK_OFFSET BlockOffset
;
478 BLOCK_OFFSET BinOffset
;
484 DPRINT("CmiMergeFree(Block %lx Offset %lx Size %lx) called\n",
485 FreeBlock
, FreeOffset
, FreeBlock
->CellSize
);
487 CmiGetBlock(RegistryHive
,
490 DPRINT("Bin %p\n", Bin
);
494 BinOffset
= Bin
->BlockOffset
;
495 BinSize
= Bin
->BlockSize
;
496 DPRINT("Bin %p Offset %lx Size %lx\n", Bin
, BinOffset
, BinSize
);
498 for (i
= 0; i
< RegistryHive
->FreeListSize
; i
++)
500 BlockOffset
= RegistryHive
->FreeListOffset
[i
];
501 BlockSize
= RegistryHive
->FreeList
[i
]->CellSize
;
502 if (BlockOffset
> BinOffset
&&
503 BlockOffset
< BinOffset
+ BinSize
)
505 DPRINT("Free block: Offset %lx Size %lx\n",
506 BlockOffset
, BlockSize
);
508 if ((i
< (RegistryHive
->FreeListSize
- 1)) &&
509 (BlockOffset
+ BlockSize
== FreeOffset
) &&
510 (FreeOffset
+ FreeBlock
->CellSize
== RegistryHive
->FreeListOffset
[i
+ 1]))
512 DPRINT("Merge current block with previous and next block\n");
514 RegistryHive
->FreeList
[i
]->CellSize
+=
515 (FreeBlock
->CellSize
+ RegistryHive
->FreeList
[i
+ 1]->CellSize
);
517 FreeBlock
->CellSize
= 0;
518 RegistryHive
->FreeList
[i
+ 1]->CellSize
= 0;
521 if ((i
+ 2) < RegistryHive
->FreeListSize
)
523 memmove (&RegistryHive
->FreeListOffset
[i
+ 1],
524 &RegistryHive
->FreeListOffset
[i
+ 2],
525 sizeof(RegistryHive
->FreeListOffset
[0])
526 * (RegistryHive
->FreeListSize
- i
- 2));
528 RegistryHive
->FreeListSize
--;
532 else if (BlockOffset
+ BlockSize
== FreeOffset
)
534 DPRINT("Merge current block with previous block\n");
536 RegistryHive
->FreeList
[i
]->CellSize
+= FreeBlock
->CellSize
;
537 FreeBlock
->CellSize
= 0;
541 else if (FreeOffset
+ FreeBlock
->CellSize
== BlockOffset
)
543 DPRINT("Merge current block with next block\n");
545 FreeBlock
->CellSize
+= RegistryHive
->FreeList
[i
]->CellSize
;
546 RegistryHive
->FreeList
[i
]->CellSize
= 0;
547 RegistryHive
->FreeList
[i
] = FreeBlock
;
548 RegistryHive
->FreeListOffset
[i
] = FreeOffset
;
560 CmiAddFree(PREGISTRY_HIVE RegistryHive
,
561 PCELL_HEADER FreeBlock
,
562 BLOCK_OFFSET FreeOffset
,
563 BOOL MergeFreeBlocks
)
565 PCELL_HEADER
*tmpList
;
566 BLOCK_OFFSET
*tmpListOffset
;
571 assert(RegistryHive
);
574 DPRINT("FreeBlock %.08lx FreeOffset %.08lx\n",
575 FreeBlock
, FreeOffset
);
577 /* Merge free blocks */
578 if (MergeFreeBlocks
== TRUE
)
580 if (CmiMergeFree(RegistryHive
, FreeBlock
, FreeOffset
))
584 if ((RegistryHive
->FreeListSize
+ 1) > RegistryHive
->FreeListMax
)
586 tmpList
= malloc (sizeof(PCELL_HEADER
) * (RegistryHive
->FreeListMax
+ 32));
592 tmpListOffset
= malloc (sizeof(BLOCK_OFFSET
) * (RegistryHive
->FreeListMax
+ 32));
593 if (tmpListOffset
== NULL
)
599 if (RegistryHive
->FreeListMax
)
602 RegistryHive
->FreeList
,
603 sizeof(PCELL_HEADER
) * (RegistryHive
->FreeListMax
));
604 memmove (tmpListOffset
,
605 RegistryHive
->FreeListOffset
,
606 sizeof(BLOCK_OFFSET
) * (RegistryHive
->FreeListMax
));
607 free (RegistryHive
->FreeList
);
608 free (RegistryHive
->FreeListOffset
);
610 RegistryHive
->FreeList
= tmpList
;
611 RegistryHive
->FreeListOffset
= tmpListOffset
;
612 RegistryHive
->FreeListMax
+= 32;
615 /* Add new offset to free list, maintaining list in ascending order */
616 if ((RegistryHive
->FreeListSize
== 0)
617 || (RegistryHive
->FreeListOffset
[RegistryHive
->FreeListSize
-1] < FreeOffset
))
619 /* Add to end of list */
620 RegistryHive
->FreeList
[RegistryHive
->FreeListSize
] = FreeBlock
;
621 RegistryHive
->FreeListOffset
[RegistryHive
->FreeListSize
++] = FreeOffset
;
623 else if (RegistryHive
->FreeListOffset
[0] > FreeOffset
)
625 /* Add to begin of list */
626 memmove (&RegistryHive
->FreeList
[1],
627 &RegistryHive
->FreeList
[0],
628 sizeof(RegistryHive
->FreeList
[0]) * RegistryHive
->FreeListSize
);
629 memmove (&RegistryHive
->FreeListOffset
[1],
630 &RegistryHive
->FreeListOffset
[0],
631 sizeof(RegistryHive
->FreeListOffset
[0]) * RegistryHive
->FreeListSize
);
632 RegistryHive
->FreeList
[0] = FreeBlock
;
633 RegistryHive
->FreeListOffset
[0] = FreeOffset
;
634 RegistryHive
->FreeListSize
++;
638 /* Search where to insert */
640 maxInd
= RegistryHive
->FreeListSize
- 1;
641 while ((maxInd
- minInd
) > 1)
643 medInd
= (minInd
+ maxInd
) / 2;
644 if (RegistryHive
->FreeListOffset
[medInd
] > FreeOffset
)
650 /* Insert before maxInd */
651 memmove (&RegistryHive
->FreeList
[maxInd
+1],
652 &RegistryHive
->FreeList
[maxInd
],
653 sizeof(RegistryHive
->FreeList
[0]) * (RegistryHive
->FreeListSize
- minInd
));
654 memmove (&RegistryHive
->FreeListOffset
[maxInd
+ 1],
655 &RegistryHive
->FreeListOffset
[maxInd
],
656 sizeof(RegistryHive
->FreeListOffset
[0]) * (RegistryHive
->FreeListSize
-minInd
));
657 RegistryHive
->FreeList
[maxInd
] = FreeBlock
;
658 RegistryHive
->FreeListOffset
[maxInd
] = FreeOffset
;
659 RegistryHive
->FreeListSize
++;
667 CmiAddBin(PREGISTRY_HIVE RegistryHive
,
669 PBLOCK_OFFSET NewBlockOffset
)
671 PCELL_HEADER tmpBlock
;
672 PHBIN
* tmpBlockList
;
675 tmpBin
= malloc (REG_BLOCK_SIZE
);
680 memset (tmpBin
, 0, REG_BLOCK_SIZE
);
682 tmpBin
->BlockId
= REG_BIN_ID
;
683 tmpBin
->BlockOffset
= RegistryHive
->FileSize
- REG_BLOCK_SIZE
;
684 RegistryHive
->FileSize
+= REG_BLOCK_SIZE
;
685 tmpBin
->BlockSize
= REG_BLOCK_SIZE
;
687 tmpBin
->DateModified
= 0ULL;
690 /* Increase size of list of blocks */
691 tmpBlockList
= malloc (sizeof(PHBIN
) * (RegistryHive
->BlockListSize
+ 1));
692 if (tmpBlockList
== NULL
)
698 if (RegistryHive
->BlockListSize
> 0)
700 memcpy (tmpBlockList
,
701 RegistryHive
->BlockList
,
702 sizeof(PHBIN
) * RegistryHive
->BlockListSize
);
703 free (RegistryHive
->BlockList
);
706 RegistryHive
->BlockList
= tmpBlockList
;
707 RegistryHive
->BlockList
[RegistryHive
->BlockListSize
++] = tmpBin
;
709 /* Initialize a free block in this heap : */
710 tmpBlock
= (PCELL_HEADER
)((ULONG_PTR
) tmpBin
+ REG_HBIN_DATA_OFFSET
);
711 tmpBlock
->CellSize
= (REG_BLOCK_SIZE
- REG_HBIN_DATA_OFFSET
);
713 *NewBlock
= (PVOID
) tmpBlock
;
716 *NewBlockOffset
= tmpBin
->BlockOffset
+ REG_HBIN_DATA_OFFSET
;
723 CmiAllocateBlock(PREGISTRY_HIVE RegistryHive
,
726 PBLOCK_OFFSET pBlockOffset
)
728 PCELL_HEADER NewBlock
;
734 /* Round to 16 bytes multiple */
735 BlockSize
= (BlockSize
+ sizeof(ULONG
) + 15) & 0xfffffff0;
737 /* first search in free blocks */
739 for (i
= 0; i
< RegistryHive
->FreeListSize
; i
++)
741 if (RegistryHive
->FreeList
[i
]->CellSize
>= BlockSize
)
743 NewBlock
= RegistryHive
->FreeList
[i
];
745 *pBlockOffset
= RegistryHive
->FreeListOffset
[i
];
747 if ((i
+ 1) < RegistryHive
->FreeListSize
)
749 memmove (&RegistryHive
->FreeList
[i
],
750 &RegistryHive
->FreeList
[i
+ 1],
751 sizeof(RegistryHive
->FreeList
[0])
752 * (RegistryHive
->FreeListSize
- i
- 1));
753 memmove (&RegistryHive
->FreeListOffset
[i
],
754 &RegistryHive
->FreeListOffset
[i
+ 1],
755 sizeof(RegistryHive
->FreeListOffset
[0])
756 * (RegistryHive
->FreeListSize
- i
- 1));
758 RegistryHive
->FreeListSize
--;
763 /* Need to extend hive file : */
764 if (NewBlock
== NULL
)
766 /* Add a new block */
767 if (!CmiAddBin(RegistryHive
, (PVOID
*)&NewBlock
, pBlockOffset
))
773 /* Split the block in two parts */
774 if (NewBlock
->CellSize
> BlockSize
)
776 NewBlock
= (PCELL_HEADER
) ((ULONG_PTR
) NewBlock
+BlockSize
);
777 NewBlock
->CellSize
= ((PCELL_HEADER
) (*Block
))->CellSize
- BlockSize
;
778 CmiAddFree (RegistryHive
,
780 *pBlockOffset
+ BlockSize
,
783 else if (NewBlock
->CellSize
< BlockSize
)
788 memset(*Block
, 0, BlockSize
);
789 ((PCELL_HEADER
)(*Block
))->CellSize
= -BlockSize
;
796 CmiAllocateHashTableCell (PREGISTRY_HIVE Hive
,
797 PBLOCK_OFFSET HBOffset
,
800 PHASH_TABLE_CELL HashCell
;
804 NewHashSize
= ROUND_UP(sizeof(HASH_TABLE_CELL
) +
805 (SubKeyCount
- 1) * sizeof(HASH_RECORD
),
807 Status
= CmiAllocateBlock (Hive
,
811 if ((HashCell
== NULL
) || (Status
== FALSE
))
816 HashCell
->Id
= REG_HASH_TABLE_BLOCK_ID
;
817 HashCell
->HashTableSize
= SubKeyCount
;
824 CmiAddKeyToParentHashTable (PREGISTRY_HIVE Hive
,
825 BLOCK_OFFSET ParentKeyOffset
,
826 PKEY_CELL NewKeyCell
,
827 BLOCK_OFFSET NKBOffset
)
829 PHASH_TABLE_CELL HashBlock
;
830 PKEY_CELL ParentKeyCell
;
833 ParentKeyCell
= CmiGetBlock (Hive
,
836 if (ParentKeyCell
== NULL
)
838 DPRINT1 ("CmiGetBlock() failed\n");
842 HashBlock
=CmiGetBlock (Hive
,
843 ParentKeyCell
->HashTableOffset
,
845 if (HashBlock
== NULL
)
847 DPRINT1 ("CmiGetBlock() failed\n");
851 for (i
= 0; i
< HashBlock
->HashTableSize
; i
++)
853 if (HashBlock
->Table
[i
].KeyOffset
== 0)
855 HashBlock
->Table
[i
].KeyOffset
= NKBOffset
;
856 memcpy (&HashBlock
->Table
[i
].HashValue
,
859 ParentKeyCell
->NumberOfSubKeys
++;
869 CmiAllocateValueListCell (PREGISTRY_HIVE Hive
,
870 PBLOCK_OFFSET ValueListOffset
,
873 PVALUE_LIST_CELL ValueListCell
;
877 ValueListSize
= ROUND_UP (ValueCount
* sizeof(BLOCK_OFFSET
),
879 Status
= CmiAllocateBlock (Hive
,
880 (PVOID
)&ValueListCell
,
883 if ((ValueListCell
== NULL
) || (Status
== FALSE
))
885 DPRINT1 ("CmiAllocateBlock() failed\n");
894 CmiAllocateValueCell(PREGISTRY_HIVE Hive
,
895 PVALUE_CELL
*ValueCell
,
896 BLOCK_OFFSET
*ValueCellOffset
,
899 PVALUE_CELL NewValueCell
;
903 NameSize
= (ValueName
== NULL
) ? 0 : strlen (ValueName
);
904 Status
= CmiAllocateBlock(Hive
,
905 (PVOID
*)&NewValueCell
,
906 sizeof(VALUE_CELL
) + NameSize
,
908 if ((NewValueCell
== NULL
) || (Status
== FALSE
))
910 DPRINT1 ("CmiAllocateBlock() failed\n");
914 NewValueCell
->Id
= REG_VALUE_CELL_ID
;
915 NewValueCell
->NameSize
= NameSize
;
918 memcpy (NewValueCell
->Name
,
921 NewValueCell
->Flags
= REG_VALUE_NAME_PACKED
;
923 NewValueCell
->DataType
= 0;
924 NewValueCell
->DataSize
= 0;
925 NewValueCell
->DataOffset
= -1;
927 *ValueCell
= NewValueCell
;
934 CmiAddValueToKeyValueList(PREGISTRY_HIVE Hive
,
935 BLOCK_OFFSET KeyCellOffset
,
936 BLOCK_OFFSET ValueCellOffset
)
938 PVALUE_LIST_CELL ValueListCell
;
941 KeyCell
= CmiGetBlock (Hive
, KeyCellOffset
, NULL
);
944 DPRINT1 ("CmiGetBlock() failed\n");
948 ValueListCell
= CmiGetBlock (Hive
, KeyCell
->ValuesOffset
, NULL
);
949 if (ValueListCell
== NULL
)
951 DPRINT1 ("CmiGetBlock() failed\n");
955 ValueListCell
->Values
[KeyCell
->NumberOfValues
] = ValueCellOffset
;
956 KeyCell
->NumberOfValues
++;
963 memexpand (PWCHAR Dst
,
969 for (i
= 0; i
< Length
; i
++)
970 Dst
[i
] = (WCHAR
)Src
[i
];
975 CmiExportValue (PREGISTRY_HIVE Hive
,
976 BLOCK_OFFSET KeyCellOffset
,
980 BLOCK_OFFSET ValueCellOffset
;
981 BLOCK_OFFSET DataCellOffset
;
982 PVALUE_CELL ValueCell
;
990 DPRINT ("CmiExportValue('%s') called\n", (Value
== NULL
) ? "<default>" : (PCHAR
)Value
->Name
);
991 DPRINT ("DataSize %lu\n", (Value
== NULL
) ? Key
->DataSize
: Value
->DataSize
);
993 /* Allocate value cell */
994 if (!CmiAllocateValueCell(Hive
, &ValueCell
, &ValueCellOffset
, (Value
== NULL
) ? NULL
: Value
->Name
))
999 if (!CmiAddValueToKeyValueList(Hive
, KeyCellOffset
, ValueCellOffset
))
1006 DataType
= Key
->DataType
;
1007 SrcDataSize
= Key
->DataSize
;
1012 DataType
= Value
->DataType
;
1013 SrcDataSize
= Value
->DataSize
;
1017 DstDataSize
= SrcDataSize
;
1018 if (DataType
== REG_SZ
||
1019 DataType
== REG_EXPAND_SZ
||
1020 DataType
== REG_MULTI_SZ
)
1022 DstDataSize
*= sizeof(WCHAR
);
1026 if (DstDataSize
<= sizeof(BLOCK_OFFSET
))
1028 ValueCell
->DataSize
= DstDataSize
| 0x80000000;
1029 ValueCell
->DataType
= DataType
;
1032 memexpand ((PWCHAR
)&ValueCell
->DataOffset
,
1038 memcpy (&ValueCell
->DataOffset
,
1045 if (!CmiAllocateBlock (Hive
,
1053 ValueCell
->DataOffset
= DataCellOffset
;
1054 ValueCell
->DataSize
= DstDataSize
;
1055 ValueCell
->DataType
= DataType
;
1059 if (SrcDataSize
<= sizeof(BLOCK_OFFSET
))
1061 memexpand ((PWCHAR
)DataCell
->Data
,
1067 memexpand ((PWCHAR
)DataCell
->Data
,
1074 memcpy (DataCell
->Data
,
1085 CmiExportSubKey (PREGISTRY_HIVE Hive
,
1086 BLOCK_OFFSET ParentKeyOffset
,
1090 BLOCK_OFFSET NKBOffset
;
1091 PKEY_CELL NewKeyCell
;
1099 DPRINT ("CmiExportSubKey('%s') called\n", Key
->Name
);
1101 /* Don't export links */
1102 if (Key
->DataType
== REG_LINK
)
1105 /* Allocate key cell */
1106 KeyCellSize
= sizeof(KEY_CELL
) + Key
->NameSize
- 1;
1107 if (!CmiAllocateBlock (Hive
, (PVOID
)&NewKeyCell
, KeyCellSize
, &NKBOffset
))
1109 DPRINT1 ("CmiAllocateBlock() failed\n");
1113 /* Initialize key cell */
1114 NewKeyCell
->Id
= REG_KEY_CELL_ID
;
1115 NewKeyCell
->Type
= REG_KEY_CELL_TYPE
;
1116 NewKeyCell
->LastWriteTime
= 0ULL;
1117 NewKeyCell
->ParentKeyOffset
= ParentKeyOffset
;
1118 NewKeyCell
->NumberOfSubKeys
= 0;
1119 NewKeyCell
->HashTableOffset
= -1;
1120 NewKeyCell
->NumberOfValues
= 0;
1121 NewKeyCell
->ValuesOffset
= -1;
1122 NewKeyCell
->SecurityKeyOffset
= -1;
1123 NewKeyCell
->NameSize
= Key
->NameSize
- 1;
1124 NewKeyCell
->ClassNameOffset
= -1;
1125 memcpy (NewKeyCell
->Name
,
1129 /* Add key cell to the parent key's hash table */
1130 if (!CmiAddKeyToParentHashTable (Hive
,
1135 DPRINT1 ("CmiAddKeyToParentHashTable() failed\n");
1139 ValueCount
= RegGetValueCount (Key
);
1140 DPRINT ("ValueCount: %lu\n", ValueCount
);
1143 /* Allocate value list cell */
1144 CmiAllocateValueListCell (Hive
,
1145 &NewKeyCell
->ValuesOffset
,
1148 if (Key
->DataSize
!= 0)
1150 if (!CmiExportValue (Hive
, NKBOffset
, Key
, NULL
))
1154 /* Enumerate values */
1155 Entry
= Key
->ValueList
.Flink
;
1156 while (Entry
!= &Key
->ValueList
)
1158 Value
= CONTAINING_RECORD(Entry
,
1162 if (!CmiExportValue (Hive
, NKBOffset
, Key
, Value
))
1165 Entry
= Entry
->Flink
;
1169 SubKeyCount
= RegGetSubKeyCount (Key
);
1170 DPRINT ("SubKeyCount: %lu\n", SubKeyCount
);
1171 if (SubKeyCount
> 0)
1173 /* Allocate hash table cell */
1174 CmiAllocateHashTableCell (Hive
,
1175 &NewKeyCell
->HashTableOffset
,
1178 /* Enumerate subkeys */
1179 Entry
= Key
->SubKeyList
.Flink
;
1180 while (Entry
!= &Key
->SubKeyList
)
1182 SubKey
= CONTAINING_RECORD(Entry
,
1186 if (!CmiExportSubKey (Hive
, NKBOffset
, Key
, SubKey
))
1189 Entry
= Entry
->Flink
;
1198 CmiCalcHiveChecksum (PREGISTRY_HIVE Hive
)
1204 Buffer
= (PULONG
)Hive
->HiveHeader
;
1206 for (i
= 0; i
< 127; i
++)
1209 Hive
->HiveHeader
->Checksum
= Sum
;
1214 CmiExportHive (PREGISTRY_HIVE Hive
,
1226 DPRINT ("CmiExportHive(%p, '%s') called\n", Hive
, KeyName
);
1228 if (RegOpenKey (NULL
, KeyName
, &Key
) != ERROR_SUCCESS
)
1230 DPRINT1 ("RegOpenKey() failed\n");
1234 DPRINT ("Name: %s\n", KeyName
);
1236 KeyCell
= CmiGetBlock (Hive
,
1237 Hive
->HiveHeader
->RootKeyCell
,
1239 if (KeyCell
== NULL
)
1241 DPRINT1 ("CmiGetBlock() failed\n");
1245 ValueCount
= RegGetValueCount (Key
);
1246 DPRINT ("ValueCount: %lu\n", ValueCount
);
1249 /* Allocate value list cell */
1250 CmiAllocateValueListCell (Hive
,
1251 &KeyCell
->ValuesOffset
,
1254 if (Key
->DataSize
!= 0)
1256 if (!CmiExportValue (Hive
, Hive
->HiveHeader
->RootKeyCell
, Key
, NULL
))
1260 /* Enumerate values */
1261 Entry
= Key
->ValueList
.Flink
;
1262 while (Entry
!= &Key
->ValueList
)
1264 Value
= CONTAINING_RECORD(Entry
,
1268 if (!CmiExportValue (Hive
, Hive
->HiveHeader
->RootKeyCell
, Key
, Value
))
1271 Entry
= Entry
->Flink
;
1275 SubKeyCount
= RegGetSubKeyCount (Key
);
1276 DPRINT ("SubKeyCount: %lu\n", SubKeyCount
);
1277 if (SubKeyCount
> 0)
1279 /* Allocate hash table cell */
1280 CmiAllocateHashTableCell (Hive
,
1281 &KeyCell
->HashTableOffset
,
1284 /* Enumerate subkeys */
1285 Entry
= Key
->SubKeyList
.Flink
;
1286 while (Entry
!= &Key
->SubKeyList
)
1288 SubKey
= CONTAINING_RECORD(Entry
,
1292 if (!CmiExportSubKey (Hive
, Hive
->HiveHeader
->RootKeyCell
, Key
, SubKey
))
1295 Entry
= Entry
->Flink
;
1299 CmiCalcHiveChecksum (Hive
);
1306 CmiWriteHive(PREGISTRY_HIVE Hive
,
1313 /* FIXME: Calculate header checksum */
1315 File
= fopen (FileName
, "w+b");
1322 fseek (File
, 0, SEEK_SET
);
1324 /* Write hive header */
1325 fwrite (Hive
->HiveHeader
, REG_BLOCK_SIZE
, 1, File
);
1328 for (i
= 0; i
< Hive
->BlockListSize
; i
++)
1330 if (Hive
->BlockList
[i
] != Bin
)
1332 Bin
= Hive
->BlockList
[i
];
1334 DPRINT ("Bin[%lu]: Offset 0x%lx Size 0x%lx\n",
1335 i
, Bin
->BlockOffset
, Bin
->BlockSize
);
1337 fwrite (Bin
, Bin
->BlockSize
, 1, File
);
1348 ExportBinaryHive (PCHAR FileName
,
1351 PREGISTRY_HIVE Hive
;
1353 printf (" Creating binary hive: %s\n", FileName
);
1355 Hive
= CmiCreateRegistryHive ();
1359 if (!CmiExportHive (Hive
, KeyName
))
1361 CmiDestroyRegistryHive (Hive
);
1365 if (!CmiWriteHive (Hive
, FileName
))
1367 CmiDestroyRegistryHive (Hive
);
1371 CmiDestroyRegistryHive (Hive
);