3 * Copyright (C) 2003, 2004 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.
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 *****************************************************************/
39 #define REG_HIVE_ID 0x66676572
40 #define REG_BIN_ID 0x6e696268
41 #define REG_KEY_CELL_ID 0x6b6e
42 #define REG_HASH_TABLE_CELL_ID 0x666c
43 #define REG_VALUE_CELL_ID 0x6b76
45 #define REG_BLOCK_SIZE 4096
46 #define REG_HBIN_DATA_OFFSET 32
47 #define REG_INIT_BLOCK_LIST_SIZE 32
48 #define REG_INIT_HASH_TABLE_SIZE 3
49 #define REG_EXTEND_HASH_TABLE_SIZE 4
50 #define REG_VALUE_LIST_CELL_MULTIPLE 4
52 #define ROUND_UP(N, S) ((N) + (S) - ((N) % (S)))
53 #define ROUND_DOWN(N, S) ((N) - ((N) % (S)))
55 #define ABS_VALUE(V) (((V) < 0) ? -(V) : (V))
58 // BLOCK_OFFSET = offset in file after header block
59 typedef ULONG BLOCK_OFFSET
, *PBLOCK_OFFSET
;
62 typedef unsigned __int64 FILETIME
;
64 typedef unsigned long long FILETIME
;
68 #pragma pack ( push, hive_header, 1 )
71 /* header for registry hive file : */
72 typedef struct _HIVE_HEADER
74 /* Hive identifier "regf" (0x66676572) */
83 /* When this hive file was last modified */
84 FILETIME DateModified
;
86 /* Registry format version ? (1?) */
89 /* Registry format version ? (3?) */
92 /* Registry format version ? (0?) */
95 /* Registry format version ? (1?) */
98 /* Offset into file from the byte after the end of the base block.
99 If the hive is volatile, this is the actual pointer to the KEY_CELL */
100 BLOCK_OFFSET RootKeyOffset
;
102 /* Size of each hive block ? */
108 /* Name of hive file */
114 /* Checksum of first 0x200 bytes */
116 } GCC_PACKED HIVE_HEADER
, *PHIVE_HEADER
;
120 /* Bin identifier "hbin" (0x6E696268) */
123 /* Block offset of this bin */
124 BLOCK_OFFSET BinOffset
;
126 /* Size in bytes, multiple of the block size (4KB) */
132 /* When this bin was last modified */
133 FILETIME DateModified
;
137 } GCC_PACKED HBIN
, *PHBIN
;
139 typedef struct _CELL_HEADER
141 /* <0 if used, >0 if free */
143 } GCC_PACKED CELL_HEADER
, *PCELL_HEADER
;
145 typedef struct _KEY_CELL
147 /* Size of this cell */
150 /* Key cell identifier "kn" (0x6b6e) */
156 /* Time of last flush */
157 FILETIME LastWriteTime
;
162 /* Block offset of parent key cell */
163 BLOCK_OFFSET ParentKeyOffset
;
165 /* Count of sub keys for the key in this key cell */
166 ULONG NumberOfSubKeys
;
171 /* Block offset of has table for FIXME: subkeys/values? */
172 BLOCK_OFFSET HashTableOffset
;
177 /* Count of values contained in this key cell */
178 ULONG NumberOfValues
;
180 /* Block offset of VALUE_LIST_CELL */
181 BLOCK_OFFSET ValueListOffset
;
183 /* Block offset of security cell */
184 BLOCK_OFFSET SecurityKeyOffset
;
186 /* Block offset of registry key class */
187 BLOCK_OFFSET ClassNameOffset
;
192 /* Size in bytes of key name */
195 /* Size of class name in bytes */
198 /* Name of key (not zero terminated) */
200 } GCC_PACKED KEY_CELL
, *PKEY_CELL
;
202 /* KEY_CELL.Type constants */
203 #define REG_LINK_KEY_CELL_TYPE 0x10
204 #define REG_KEY_CELL_TYPE 0x20
205 #define REG_ROOT_KEY_CELL_TYPE 0x2c
209 // HashValue=four letters of value's name
210 typedef struct _HASH_RECORD
212 BLOCK_OFFSET KeyOffset
;
214 } GCC_PACKED HASH_RECORD
, *PHASH_RECORD
;
216 typedef struct _HASH_TABLE_CELL
220 USHORT HashTableSize
;
221 HASH_RECORD Table
[0];
222 } GCC_PACKED HASH_TABLE_CELL
, *PHASH_TABLE_CELL
;
224 typedef struct _VALUE_LIST_CELL
227 BLOCK_OFFSET ValueOffset
[0];
228 } GCC_PACKED VALUE_LIST_CELL
, *PVALUE_LIST_CELL
;
230 typedef struct _VALUE_CELL
234 USHORT NameSize
; // length of Name
235 ULONG DataSize
; // length of datas in the cell pointed by DataOffset
236 BLOCK_OFFSET DataOffset
;// datas are here if high bit of DataSize is set
240 UCHAR Name
[0]; /* warning : not zero terminated */
241 } GCC_PACKED VALUE_CELL
, *PVALUE_CELL
;
243 /* VALUE_CELL.Flags constants */
244 #define REG_VALUE_NAME_PACKED 0x0001
246 /* VALUE_CELL.DataSize mask constants */
247 #define REG_DATA_SIZE_MASK 0x7FFFFFFF
248 #define REG_DATA_IN_OFFSET 0x80000000
250 typedef struct _DATA_CELL
254 } GCC_PACKED DATA_CELL
, *PDATA_CELL
;
257 #pragma pack ( pop, hive_header )
260 typedef struct _REGISTRY_HIVE
263 PHIVE_HEADER HiveHeader
;
268 PCELL_HEADER
*FreeList
;
269 BLOCK_OFFSET
*FreeListOffset
;
270 } REGISTRY_HIVE
, *PREGISTRY_HIVE
;
272 /* FUNCTIONS ****************************************************************/
275 memexpand (PWCHAR Dst
,
281 for (i
= 0; i
< Length
; i
++)
282 Dst
[i
] = (WCHAR
)Src
[i
];
287 CmiCreateDefaultHiveHeader (PHIVE_HEADER Header
)
290 memset (Header
, 0, REG_BLOCK_SIZE
);
291 Header
->BlockId
= REG_HIVE_ID
;
292 Header
->UpdateCounter1
= 0;
293 Header
->UpdateCounter2
= 0;
294 Header
->DateModified
= 0;
300 Header
->RootKeyOffset
= -1;
301 Header
->BlockSize
= REG_BLOCK_SIZE
;
303 Header
->Checksum
= 0;
308 CmiCreateDefaultBinCell(PHBIN BinCell
)
311 memset (BinCell
, 0, REG_BLOCK_SIZE
);
312 BinCell
->HeaderId
= REG_BIN_ID
;
313 BinCell
->DateModified
= 0;
314 BinCell
->BinSize
= REG_BLOCK_SIZE
;
319 CmiCreateDefaultRootKeyCell(PKEY_CELL RootKeyCell
, PCHAR KeyName
)
325 assert (RootKeyCell
);
327 BaseKeyName
= strrchr(KeyName
, '\\') + 1;
328 NameSize
= strlen(BaseKeyName
);
329 CellSize
= ROUND_UP(sizeof(KEY_CELL
) + NameSize
- 1, 16);
331 memset (RootKeyCell
, 0, CellSize
);
332 RootKeyCell
->CellSize
= (ULONG
)-(LONG
)CellSize
;
333 RootKeyCell
->Id
= REG_KEY_CELL_ID
;
334 RootKeyCell
->Type
= REG_ROOT_KEY_CELL_TYPE
;
335 RootKeyCell
->LastWriteTime
= 0;
336 RootKeyCell
->ParentKeyOffset
= 0;
337 RootKeyCell
->NumberOfSubKeys
= 0;
338 RootKeyCell
->HashTableOffset
= -1;
339 RootKeyCell
->NumberOfValues
= 0;
340 RootKeyCell
->ValueListOffset
= -1;
341 RootKeyCell
->SecurityKeyOffset
= 0;
342 RootKeyCell
->ClassNameOffset
= -1;
343 RootKeyCell
->NameSize
= NameSize
;
344 RootKeyCell
->ClassSize
= 0;
345 memcpy (RootKeyCell
->Name
,
351 static PREGISTRY_HIVE
352 CmiCreateRegistryHive (PCHAR KeyName
)
355 PCELL_HEADER FreeCell
;
356 PKEY_CELL RootKeyCell
;
359 Hive
= (PREGISTRY_HIVE
) malloc (sizeof(REGISTRY_HIVE
));
364 memset (Hive
, 0, sizeof(REGISTRY_HIVE
));
366 DPRINT("Hive %p\n", Hive
);
368 /* Create hive beader (aka 'base block') */
369 Hive
->HiveHeader
= (PHIVE_HEADER
) malloc (REG_BLOCK_SIZE
);
370 if (Hive
->HiveHeader
== NULL
)
375 CmiCreateDefaultHiveHeader (Hive
->HiveHeader
);
376 Hive
->FileSize
= REG_BLOCK_SIZE
;
378 /* Allocate block list */
379 Hive
->BlockListSize
= 1;
380 Hive
->BlockList
= malloc (sizeof(PHBIN
) * Hive
->BlockListSize
);
381 if (Hive
->BlockList
== NULL
)
383 free (Hive
->HiveHeader
);
388 /* Allocate free cell list */
389 Hive
->FreeListMax
= 32;
390 Hive
->FreeList
= malloc(sizeof(PCELL_HEADER
) * Hive
->FreeListMax
);
391 if (Hive
->FreeList
== NULL
)
393 free (Hive
->BlockList
);
394 free (Hive
->HiveHeader
);
398 Hive
->FreeListOffset
= malloc(sizeof(BLOCK_OFFSET
) * Hive
->FreeListMax
);
399 if (Hive
->FreeListOffset
== NULL
)
401 free (Hive
->FreeList
);
402 free (Hive
->BlockList
);
403 free (Hive
->HiveHeader
);
408 /* Allocate first bin */
409 Hive
->BlockList
[0] = (PHBIN
) malloc (REG_BLOCK_SIZE
);
410 if (Hive
->BlockList
[0] == NULL
)
412 free (Hive
->FreeListOffset
);
413 free (Hive
->FreeList
);
414 free (Hive
->BlockList
);
415 free (Hive
->HiveHeader
);
419 Hive
->FileSize
+= REG_BLOCK_SIZE
;
422 BinCell
= (PHBIN
)Hive
->BlockList
[0];
423 CmiCreateDefaultBinCell (BinCell
);
424 BinCell
->BinOffset
= 0;
426 /* Init root key cell */
427 RootKeyCell
= (PKEY_CELL
)((ULONG_PTR
)BinCell
+ REG_HBIN_DATA_OFFSET
);
428 CmiCreateDefaultRootKeyCell (RootKeyCell
, KeyName
);
429 Hive
->HiveHeader
->RootKeyOffset
= REG_HBIN_DATA_OFFSET
;
432 FreeCell
= (PCELL_HEADER
)((ULONG_PTR
)RootKeyCell
- RootKeyCell
->CellSize
);
433 FreeCell
->CellSize
= REG_BLOCK_SIZE
- (REG_HBIN_DATA_OFFSET
- RootKeyCell
->CellSize
);
435 Hive
->FreeList
[0] = FreeCell
;
436 Hive
->FreeListOffset
[0] = REG_HBIN_DATA_OFFSET
- RootKeyCell
->CellSize
;
437 Hive
->FreeListSize
++;
444 CmiDestroyRegistryHive (PREGISTRY_HIVE Hive
)
452 /* Release free offset list */
453 if (Hive
->FreeListOffset
!= NULL
)
454 free (Hive
->FreeListOffset
);
456 /* Release free list */
457 if (Hive
->FreeList
!= NULL
)
458 free (Hive
->FreeList
);
460 if (Hive
->BlockList
!= NULL
)
464 for (i
= 0; i
< Hive
->BlockListSize
; i
++)
466 if ((Hive
->BlockList
[i
] != NULL
) &&
467 (Hive
->BlockList
[i
] != Bin
))
469 Bin
= Hive
->BlockList
[i
];
471 DPRINT ("Bin[%lu]: Offset 0x%lx Size 0x%lx\n",
472 i
, Bin
->BinOffset
, Bin
->BinSize
);
478 /* Release block list */
479 free (Hive
->BlockList
);
482 /* Release hive header */
483 if (Hive
->HiveHeader
!= NULL
)
484 free (Hive
->HiveHeader
);
492 CmiGetCell (PREGISTRY_HIVE Hive
,
493 BLOCK_OFFSET BlockOffset
,
502 if (BlockOffset
== (ULONG_PTR
) -1)
505 BlockIndex
= BlockOffset
/ 4096;
506 if (BlockIndex
>= Hive
->BlockListSize
)
509 pBin
= Hive
->BlockList
[BlockIndex
];
513 return (PVOID
)((ULONG_PTR
)pBin
+ (BlockOffset
- pBin
->BinOffset
));
518 CmiMergeFree(PREGISTRY_HIVE RegistryHive
,
519 PCELL_HEADER FreeBlock
,
520 BLOCK_OFFSET FreeOffset
)
522 BLOCK_OFFSET BlockOffset
;
523 BLOCK_OFFSET BinOffset
;
529 DPRINT("CmiMergeFree(Block %p Offset %lx Size %lx) called\n",
530 FreeBlock
, FreeOffset
, FreeBlock
->CellSize
);
532 CmiGetCell (RegistryHive
,
535 DPRINT("Bin %p\n", Bin
);
539 BinOffset
= Bin
->BinOffset
;
540 BinSize
= Bin
->BinSize
;
541 DPRINT("Bin %p Offset %lx Size %lx\n", Bin
, BinOffset
, BinSize
);
543 for (i
= 0; i
< RegistryHive
->FreeListSize
; i
++)
545 BlockOffset
= RegistryHive
->FreeListOffset
[i
];
546 BlockSize
= RegistryHive
->FreeList
[i
]->CellSize
;
547 if (BlockOffset
> BinOffset
&&
548 BlockOffset
< BinOffset
+ BinSize
)
550 DPRINT("Free block: Offset %lx Size %lx\n",
551 BlockOffset
, BlockSize
);
553 if ((i
< (RegistryHive
->FreeListSize
- 1)) &&
554 (BlockOffset
+ BlockSize
== FreeOffset
) &&
555 (FreeOffset
+ FreeBlock
->CellSize
== RegistryHive
->FreeListOffset
[i
+ 1]))
557 DPRINT("Merge current block with previous and next block\n");
559 RegistryHive
->FreeList
[i
]->CellSize
+=
560 (FreeBlock
->CellSize
+ RegistryHive
->FreeList
[i
+ 1]->CellSize
);
562 FreeBlock
->CellSize
= 0;
563 RegistryHive
->FreeList
[i
+ 1]->CellSize
= 0;
566 if ((i
+ 2) < RegistryHive
->FreeListSize
)
568 memmove (&RegistryHive
->FreeList
[i
+ 1],
569 &RegistryHive
->FreeList
[i
+ 2],
570 sizeof(RegistryHive
->FreeList
[0])
571 * (RegistryHive
->FreeListSize
- i
- 2));
572 memmove (&RegistryHive
->FreeListOffset
[i
+ 1],
573 &RegistryHive
->FreeListOffset
[i
+ 2],
574 sizeof(RegistryHive
->FreeListOffset
[0])
575 * (RegistryHive
->FreeListSize
- i
- 2));
577 RegistryHive
->FreeListSize
--;
581 else if (BlockOffset
+ BlockSize
== FreeOffset
)
583 DPRINT("Merge current block with previous block\n");
585 RegistryHive
->FreeList
[i
]->CellSize
+= FreeBlock
->CellSize
;
586 FreeBlock
->CellSize
= 0;
590 else if (FreeOffset
+ FreeBlock
->CellSize
== BlockOffset
)
592 DPRINT("Merge current block with next block\n");
594 FreeBlock
->CellSize
+= RegistryHive
->FreeList
[i
]->CellSize
;
595 RegistryHive
->FreeList
[i
]->CellSize
= 0;
596 RegistryHive
->FreeList
[i
] = FreeBlock
;
597 RegistryHive
->FreeListOffset
[i
] = FreeOffset
;
609 CmiAddFree(PREGISTRY_HIVE RegistryHive
,
610 PCELL_HEADER FreeBlock
,
611 BLOCK_OFFSET FreeOffset
,
612 BOOL MergeFreeBlocks
)
614 PCELL_HEADER
*tmpList
;
615 BLOCK_OFFSET
*tmpListOffset
;
620 assert(RegistryHive
);
623 DPRINT("FreeBlock %p FreeOffset %.08lx\n",
624 FreeBlock
, FreeOffset
);
626 /* Merge free blocks */
627 if (MergeFreeBlocks
== TRUE
)
629 if (CmiMergeFree(RegistryHive
, FreeBlock
, FreeOffset
))
633 if ((RegistryHive
->FreeListSize
+ 1) > RegistryHive
->FreeListMax
)
635 tmpList
= malloc (sizeof(PCELL_HEADER
) * (RegistryHive
->FreeListMax
+ 32));
641 tmpListOffset
= malloc (sizeof(BLOCK_OFFSET
) * (RegistryHive
->FreeListMax
+ 32));
642 if (tmpListOffset
== NULL
)
648 if (RegistryHive
->FreeListMax
)
651 RegistryHive
->FreeList
,
652 sizeof(PCELL_HEADER
) * (RegistryHive
->FreeListMax
));
653 memmove (tmpListOffset
,
654 RegistryHive
->FreeListOffset
,
655 sizeof(BLOCK_OFFSET
) * (RegistryHive
->FreeListMax
));
656 free (RegistryHive
->FreeList
);
657 free (RegistryHive
->FreeListOffset
);
659 RegistryHive
->FreeList
= tmpList
;
660 RegistryHive
->FreeListOffset
= tmpListOffset
;
661 RegistryHive
->FreeListMax
+= 32;
664 /* Add new offset to free list, maintaining list in ascending order */
665 if ((RegistryHive
->FreeListSize
== 0)
666 || (RegistryHive
->FreeListOffset
[RegistryHive
->FreeListSize
-1] < FreeOffset
))
668 /* Add to end of list */
669 RegistryHive
->FreeList
[RegistryHive
->FreeListSize
] = FreeBlock
;
670 RegistryHive
->FreeListOffset
[RegistryHive
->FreeListSize
++] = FreeOffset
;
672 else if (RegistryHive
->FreeListOffset
[0] > FreeOffset
)
674 /* Add to begin of list */
675 memmove (&RegistryHive
->FreeList
[1],
676 &RegistryHive
->FreeList
[0],
677 sizeof(RegistryHive
->FreeList
[0]) * RegistryHive
->FreeListSize
);
678 memmove (&RegistryHive
->FreeListOffset
[1],
679 &RegistryHive
->FreeListOffset
[0],
680 sizeof(RegistryHive
->FreeListOffset
[0]) * RegistryHive
->FreeListSize
);
681 RegistryHive
->FreeList
[0] = FreeBlock
;
682 RegistryHive
->FreeListOffset
[0] = FreeOffset
;
683 RegistryHive
->FreeListSize
++;
687 /* Search where to insert */
689 maxInd
= RegistryHive
->FreeListSize
- 1;
690 while ((maxInd
- minInd
) > 1)
692 medInd
= (minInd
+ maxInd
) / 2;
693 if (RegistryHive
->FreeListOffset
[medInd
] > FreeOffset
)
699 /* Insert before maxInd */
700 memmove (&RegistryHive
->FreeList
[maxInd
+1],
701 &RegistryHive
->FreeList
[maxInd
],
702 sizeof(RegistryHive
->FreeList
[0]) * (RegistryHive
->FreeListSize
- minInd
));
703 memmove (&RegistryHive
->FreeListOffset
[maxInd
+ 1],
704 &RegistryHive
->FreeListOffset
[maxInd
],
705 sizeof(RegistryHive
->FreeListOffset
[0]) * (RegistryHive
->FreeListSize
-minInd
));
706 RegistryHive
->FreeList
[maxInd
] = FreeBlock
;
707 RegistryHive
->FreeListOffset
[maxInd
] = FreeOffset
;
708 RegistryHive
->FreeListSize
++;
716 CmiAddBin(PREGISTRY_HIVE RegistryHive
,
719 PBLOCK_OFFSET NewBlockOffset
)
721 PCELL_HEADER tmpBlock
;
722 PHBIN
* tmpBlockList
;
727 BinSize
= BlockCount
*REG_BLOCK_SIZE
;
728 tmpBin
= malloc (BinSize
);
733 memset (tmpBin
, 0, BinSize
);
735 tmpBin
->HeaderId
= REG_BIN_ID
;
736 tmpBin
->BinOffset
= RegistryHive
->FileSize
- REG_BLOCK_SIZE
;
737 RegistryHive
->FileSize
+= BinSize
;
738 tmpBin
->BinSize
= BinSize
;
740 tmpBin
->DateModified
= 0;
743 /* Increase size of list of blocks */
744 tmpBlockList
= malloc (sizeof(PHBIN
) * (RegistryHive
->BlockListSize
+ BlockCount
));
745 if (tmpBlockList
== NULL
)
751 if (RegistryHive
->BlockListSize
> 0)
753 memcpy (tmpBlockList
,
754 RegistryHive
->BlockList
,
755 sizeof(PHBIN
) * RegistryHive
->BlockListSize
);
756 free (RegistryHive
->BlockList
);
759 RegistryHive
->BlockList
= tmpBlockList
;
760 for (i
= 0; i
< BlockCount
; i
++)
761 RegistryHive
->BlockList
[RegistryHive
->BlockListSize
+ i
] = tmpBin
;
762 RegistryHive
->BlockListSize
+= BlockCount
;
764 /* Initialize a free block in this heap : */
765 tmpBlock
= (PCELL_HEADER
)((ULONG_PTR
) tmpBin
+ REG_HBIN_DATA_OFFSET
);
766 tmpBlock
->CellSize
= (REG_BLOCK_SIZE
- REG_HBIN_DATA_OFFSET
);
768 *NewBlock
= (PVOID
) tmpBlock
;
771 *NewBlockOffset
= tmpBin
->BinOffset
+ REG_HBIN_DATA_OFFSET
;
778 CmiAllocateCell (PREGISTRY_HIVE RegistryHive
,
781 PBLOCK_OFFSET pBlockOffset
)
783 PCELL_HEADER NewBlock
;
788 /* Round to 16 bytes multiple */
789 CellSize
= ROUND_UP(CellSize
, 16);
791 /* first search in free blocks */
793 for (i
= 0; i
< RegistryHive
->FreeListSize
; i
++)
795 if (RegistryHive
->FreeList
[i
]->CellSize
>= CellSize
)
797 NewBlock
= RegistryHive
->FreeList
[i
];
799 *pBlockOffset
= RegistryHive
->FreeListOffset
[i
];
801 if ((i
+ 1) < RegistryHive
->FreeListSize
)
803 memmove (&RegistryHive
->FreeList
[i
],
804 &RegistryHive
->FreeList
[i
+ 1],
805 sizeof(RegistryHive
->FreeList
[0])
806 * (RegistryHive
->FreeListSize
- i
- 1));
807 memmove (&RegistryHive
->FreeListOffset
[i
],
808 &RegistryHive
->FreeListOffset
[i
+ 1],
809 sizeof(RegistryHive
->FreeListOffset
[0])
810 * (RegistryHive
->FreeListSize
- i
- 1));
812 RegistryHive
->FreeListSize
--;
817 /* Need to extend hive file : */
818 if (NewBlock
== NULL
)
820 /* Add a new block */
821 if (!CmiAddBin(RegistryHive
,
822 ((sizeof(HBIN
) + CellSize
- 1) / REG_BLOCK_SIZE
) + 1,
830 /* Split the block in two parts */
831 if (NewBlock
->CellSize
> CellSize
)
833 NewBlock
= (PCELL_HEADER
) ((ULONG_PTR
) NewBlock
+ CellSize
);
834 NewBlock
->CellSize
= ((PCELL_HEADER
) (*Block
))->CellSize
- CellSize
;
835 CmiAddFree (RegistryHive
,
837 *pBlockOffset
+ CellSize
,
840 else if (NewBlock
->CellSize
< CellSize
)
845 memset(*Block
, 0, CellSize
);
846 ((PCELL_HEADER
)(*Block
))->CellSize
= -CellSize
;
853 CmiAllocateHashTableCell (PREGISTRY_HIVE Hive
,
854 PBLOCK_OFFSET HBOffset
,
857 PHASH_TABLE_CELL HashCell
;
861 NewHashSize
= sizeof(HASH_TABLE_CELL
) +
862 (SubKeyCount
* sizeof(HASH_RECORD
));
863 Status
= CmiAllocateCell (Hive
,
867 if ((HashCell
== NULL
) || (Status
== FALSE
))
872 HashCell
->Id
= REG_HASH_TABLE_CELL_ID
;
873 HashCell
->HashTableSize
= SubKeyCount
;
880 CmiAddKeyToParentHashTable (PREGISTRY_HIVE Hive
,
881 BLOCK_OFFSET ParentKeyOffset
,
882 PKEY_CELL NewKeyCell
,
883 BLOCK_OFFSET NKBOffset
)
885 PHASH_TABLE_CELL HashBlock
;
886 PKEY_CELL ParentKeyCell
;
889 ParentKeyCell
= CmiGetCell (Hive
,
892 if (ParentKeyCell
== NULL
)
894 DPRINT1 ("CmiGetBlock() failed\n");
898 HashBlock
=CmiGetCell (Hive
,
899 ParentKeyCell
->HashTableOffset
,
901 if (HashBlock
== NULL
)
903 DPRINT1 ("CmiGetBlock() failed\n");
907 for (i
= 0; i
< HashBlock
->HashTableSize
; i
++)
909 if (HashBlock
->Table
[i
].KeyOffset
== 0)
911 HashBlock
->Table
[i
].KeyOffset
= NKBOffset
;
912 memcpy (&HashBlock
->Table
[i
].HashValue
,
915 ParentKeyCell
->NumberOfSubKeys
++;
925 CmiAllocateValueListCell (PREGISTRY_HIVE Hive
,
926 PBLOCK_OFFSET ValueListOffset
,
929 PVALUE_LIST_CELL ValueListCell
;
933 ValueListSize
= sizeof(VALUE_LIST_CELL
) +
934 (ValueCount
* sizeof(BLOCK_OFFSET
));
935 Status
= CmiAllocateCell (Hive
,
937 (PVOID
)&ValueListCell
,
939 if ((ValueListCell
== NULL
) || (Status
== FALSE
))
941 DPRINT1 ("CmiAllocateBlock() failed\n");
950 CmiAllocateValueCell(PREGISTRY_HIVE Hive
,
951 PVALUE_CELL
*ValueCell
,
952 BLOCK_OFFSET
*ValueCellOffset
,
955 PVALUE_CELL NewValueCell
;
959 NameSize
= (ValueName
== NULL
) ? 0 : strlen (ValueName
);
960 Status
= CmiAllocateCell (Hive
,
961 sizeof(VALUE_CELL
) + NameSize
,
962 (PVOID
*)&NewValueCell
,
964 if ((NewValueCell
== NULL
) || (Status
== FALSE
))
966 DPRINT1 ("CmiAllocateBlock() failed\n");
970 NewValueCell
->Id
= REG_VALUE_CELL_ID
;
971 NewValueCell
->NameSize
= NameSize
;
974 memcpy (NewValueCell
->Name
,
977 NewValueCell
->Flags
= REG_VALUE_NAME_PACKED
;
979 NewValueCell
->DataType
= 0;
980 NewValueCell
->DataSize
= 0;
981 NewValueCell
->DataOffset
= -1;
983 *ValueCell
= NewValueCell
;
990 CmiAddValueToKeyValueList(PREGISTRY_HIVE Hive
,
991 BLOCK_OFFSET KeyCellOffset
,
992 BLOCK_OFFSET ValueCellOffset
)
994 PVALUE_LIST_CELL ValueListCell
;
997 KeyCell
= CmiGetCell (Hive
, KeyCellOffset
, NULL
);
1000 DPRINT1 ("CmiGetBlock() failed\n");
1004 ValueListCell
= CmiGetCell (Hive
, KeyCell
->ValueListOffset
, NULL
);
1005 if (ValueListCell
== NULL
)
1007 DPRINT1 ("CmiGetBlock() failed\n");
1011 ValueListCell
->ValueOffset
[KeyCell
->NumberOfValues
] = ValueCellOffset
;
1012 KeyCell
->NumberOfValues
++;
1019 CmiExportValue (PREGISTRY_HIVE Hive
,
1020 BLOCK_OFFSET KeyCellOffset
,
1024 BLOCK_OFFSET ValueCellOffset
;
1025 BLOCK_OFFSET DataCellOffset
;
1026 PVALUE_CELL ValueCell
;
1027 PDATA_CELL DataCell
;
1032 BOOL Expand
= FALSE
;
1034 DPRINT ("CmiExportValue('%s') called\n", (Value
== NULL
) ? "<default>" : (PCHAR
)Value
->Name
);
1035 DPRINT ("DataSize %lu\n", (Value
== NULL
) ? Key
->DataSize
: Value
->DataSize
);
1037 /* Allocate value cell */
1038 if (!CmiAllocateValueCell(Hive
, &ValueCell
, &ValueCellOffset
, (Value
== NULL
) ? NULL
: Value
->Name
))
1043 if (!CmiAddValueToKeyValueList(Hive
, KeyCellOffset
, ValueCellOffset
))
1050 DataType
= Key
->DataType
;
1051 SrcDataSize
= Key
->DataSize
;
1056 DataType
= Value
->DataType
;
1057 SrcDataSize
= Value
->DataSize
;
1061 DstDataSize
= SrcDataSize
;
1062 if (DataType
== REG_SZ
||
1063 DataType
== REG_EXPAND_SZ
||
1064 DataType
== REG_MULTI_SZ
)
1066 DstDataSize
*= sizeof(WCHAR
);
1070 if ((DstDataSize
& REG_DATA_SIZE_MASK
) <= sizeof(BLOCK_OFFSET
))
1072 ValueCell
->DataSize
= DstDataSize
| REG_DATA_IN_OFFSET
;
1073 ValueCell
->DataType
= DataType
;
1076 memexpand ((PWCHAR
)&ValueCell
->DataOffset
,
1082 memcpy (&ValueCell
->DataOffset
,
1089 /* Allocate data cell */
1090 if (!CmiAllocateCell (Hive
,
1091 sizeof(CELL_HEADER
) + DstDataSize
,
1098 ValueCell
->DataOffset
= DataCellOffset
;
1099 ValueCell
->DataSize
= DstDataSize
& REG_DATA_SIZE_MASK
;
1100 ValueCell
->DataType
= DataType
;
1104 if (SrcDataSize
<= sizeof(BLOCK_OFFSET
))
1106 memexpand ((PWCHAR
)DataCell
->Data
,
1112 memexpand ((PWCHAR
)DataCell
->Data
,
1119 memcpy (DataCell
->Data
,
1130 CmiExportSubKey (PREGISTRY_HIVE Hive
,
1131 BLOCK_OFFSET ParentKeyOffset
,
1135 BLOCK_OFFSET NKBOffset
;
1136 PKEY_CELL NewKeyCell
;
1144 DPRINT ("CmiExportSubKey('%s') called\n", Key
->Name
);
1146 /* Don't export links */
1147 if (Key
->DataType
== REG_LINK
)
1150 /* Allocate key cell */
1151 KeyCellSize
= sizeof(KEY_CELL
) + Key
->NameSize
- 1;
1152 if (!CmiAllocateCell (Hive
, KeyCellSize
, (PVOID
)&NewKeyCell
, &NKBOffset
))
1154 DPRINT1 ("CmiAllocateBlock() failed\n");
1158 /* Initialize key cell */
1159 NewKeyCell
->Id
= REG_KEY_CELL_ID
;
1160 NewKeyCell
->Type
= REG_KEY_CELL_TYPE
;
1161 NewKeyCell
->LastWriteTime
= 0;
1162 NewKeyCell
->ParentKeyOffset
= ParentKeyOffset
;
1163 NewKeyCell
->NumberOfSubKeys
= 0;
1164 NewKeyCell
->HashTableOffset
= -1;
1165 NewKeyCell
->NumberOfValues
= 0;
1166 NewKeyCell
->ValueListOffset
= -1;
1167 NewKeyCell
->SecurityKeyOffset
= -1;
1168 NewKeyCell
->NameSize
= Key
->NameSize
- 1;
1169 NewKeyCell
->ClassNameOffset
= -1;
1170 memcpy (NewKeyCell
->Name
,
1174 /* Add key cell to the parent key's hash table */
1175 if (!CmiAddKeyToParentHashTable (Hive
,
1180 DPRINT1 ("CmiAddKeyToParentHashTable() failed\n");
1184 ValueCount
= RegGetValueCount (Key
);
1185 DPRINT ("ValueCount: %lu\n", ValueCount
);
1188 /* Allocate value list cell */
1189 CmiAllocateValueListCell (Hive
,
1190 &NewKeyCell
->ValueListOffset
,
1193 if (Key
->DataSize
!= 0)
1195 if (!CmiExportValue (Hive
, NKBOffset
, Key
, NULL
))
1199 /* Enumerate values */
1200 Entry
= Key
->ValueList
.Flink
;
1201 while (Entry
!= &Key
->ValueList
)
1203 Value
= CONTAINING_RECORD(Entry
,
1207 if (!CmiExportValue (Hive
, NKBOffset
, Key
, Value
))
1210 Entry
= Entry
->Flink
;
1214 SubKeyCount
= RegGetSubKeyCount (Key
);
1215 DPRINT ("SubKeyCount: %lu\n", SubKeyCount
);
1216 if (SubKeyCount
> 0)
1218 /* Allocate hash table cell */
1219 CmiAllocateHashTableCell (Hive
,
1220 &NewKeyCell
->HashTableOffset
,
1223 /* Enumerate subkeys */
1224 Entry
= Key
->SubKeyList
.Flink
;
1225 while (Entry
!= &Key
->SubKeyList
)
1227 SubKey
= CONTAINING_RECORD(Entry
,
1231 if (!CmiExportSubKey (Hive
, NKBOffset
, Key
, SubKey
))
1234 Entry
= Entry
->Flink
;
1243 CmiCalcHiveChecksum (PREGISTRY_HIVE Hive
)
1249 Buffer
= (PULONG
)Hive
->HiveHeader
;
1251 for (i
= 0; i
< 127; i
++)
1254 Hive
->HiveHeader
->Checksum
= Sum
;
1259 CmiExportHive (PREGISTRY_HIVE Hive
,
1270 DPRINT ("CmiExportHive(%p, '%s') called\n", Hive
, KeyName
);
1272 if (RegOpenKey (NULL
, KeyName
, &Key
) != ERROR_SUCCESS
)
1274 DPRINT1 ("RegOpenKey() failed\n");
1278 DPRINT ("Name: %s\n", KeyName
);
1280 KeyCell
= CmiGetCell (Hive
,
1281 Hive
->HiveHeader
->RootKeyOffset
,
1283 if (KeyCell
== NULL
)
1285 DPRINT1 ("CmiGetCell() failed\n");
1289 ValueCount
= RegGetValueCount (Key
);
1290 DPRINT ("ValueCount: %lu\n", ValueCount
);
1293 /* Allocate value list cell */
1294 CmiAllocateValueListCell (Hive
,
1295 &KeyCell
->ValueListOffset
,
1298 if (Key
->DataSize
!= 0)
1300 if (!CmiExportValue (Hive
, Hive
->HiveHeader
->RootKeyOffset
, Key
, NULL
))
1304 /* Enumerate values */
1305 Entry
= Key
->ValueList
.Flink
;
1306 while (Entry
!= &Key
->ValueList
)
1308 Value
= CONTAINING_RECORD(Entry
,
1312 if (!CmiExportValue (Hive
, Hive
->HiveHeader
->RootKeyOffset
, Key
, Value
))
1315 Entry
= Entry
->Flink
;
1319 SubKeyCount
= RegGetSubKeyCount (Key
);
1320 DPRINT ("SubKeyCount: %lu\n", SubKeyCount
);
1321 if (SubKeyCount
> 0)
1323 /* Allocate hash table cell */
1324 CmiAllocateHashTableCell (Hive
,
1325 &KeyCell
->HashTableOffset
,
1328 /* Enumerate subkeys */
1329 Entry
= Key
->SubKeyList
.Flink
;
1330 while (Entry
!= &Key
->SubKeyList
)
1332 SubKey
= CONTAINING_RECORD(Entry
,
1336 if (!CmiExportSubKey (Hive
, Hive
->HiveHeader
->RootKeyOffset
, Key
, SubKey
))
1339 Entry
= Entry
->Flink
;
1343 CmiCalcHiveChecksum (Hive
);
1350 CmiWriteHive(PREGISTRY_HIVE Hive
,
1358 /* Check for existing hive file */
1359 File
= fopen (FileName
, "rb");
1362 printf (" File already exists\n");
1368 /* Create new hive file */
1369 File
= fopen (FileName
, "w+b");
1372 printf(" Error creating/opening file\n");
1376 fseek (File
, 0, SEEK_SET
);
1378 /* Calculate header checksum */
1379 CmiCalcHiveChecksum (Hive
);
1381 /* Write hive header */
1382 fwrite (Hive
->HiveHeader
, REG_BLOCK_SIZE
, 1, File
);
1385 for (i
= 0; i
< Hive
->BlockListSize
; i
++)
1387 if (Hive
->BlockList
[i
] != Bin
)
1389 Bin
= Hive
->BlockList
[i
];
1391 DPRINT ("Bin[%lu]: Offset 0x%lx Size 0x%lx\n",
1392 i
, Bin
->BinOffset
, Bin
->BinSize
);
1394 fwrite (Bin
, Bin
->BinSize
, 1, File
);
1405 ExportBinaryHive (PCHAR FileName
,
1408 PREGISTRY_HIVE Hive
;
1410 printf (" Creating binary hive: %s\n", FileName
);
1412 Hive
= CmiCreateRegistryHive (KeyName
);
1416 if (!CmiExportHive (Hive
, KeyName
))
1418 CmiDestroyRegistryHive (Hive
);
1422 if (!CmiWriteHive (Hive
, FileName
))
1424 CmiDestroyRegistryHive (Hive
);
1428 CmiDestroyRegistryHive (Hive
);