3 * Copyright (C) 2006 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 /* COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS hive maker
21 * FILE: tools/mkhive/registry.c
22 * PURPOSE: Registry code
23 * PROGRAMMER: Hervé Poussineau
28 * - Implement RegDeleteKey()
29 * - Implement RegEnumValue()
30 * - Implement RegQueryValueExA()
40 static EREGISTRY_HIVE RootHive
;
41 static MEMKEY RootKey
;
42 EREGISTRY_HIVE DefaultHive
; /* \Registry\User\.DEFAULT */
43 EREGISTRY_HIVE SamHive
; /* \Registry\Machine\SAM */
44 EREGISTRY_HIVE SecurityHive
; /* \Registry\Machine\SECURITY */
45 EREGISTRY_HIVE SoftwareHive
; /* \Registry\Machine\SOFTWARE */
46 EREGISTRY_HIVE SystemHive
; /* \Registry\Machine\SYSTEM */
49 CreateInMemoryStructure(
50 IN PEREGISTRY_HIVE RegistryHive
,
51 IN HCELL_INDEX KeyCellOffset
,
52 IN PCUNICODE_STRING KeyName
)
56 Key
= (MEMKEY
) malloc (sizeof(KEY
));
60 InitializeListHead (&Key
->SubKeyList
);
61 InitializeListHead (&Key
->ValueList
);
62 InitializeListHead (&Key
->KeyList
);
67 Key
->NameSize
= KeyName
->Length
;
68 Key
->Name
= malloc (Key
->NameSize
);
71 memcpy(Key
->Name
, KeyName
->Buffer
, KeyName
->Length
);
77 Key
->RegistryHive
= RegistryHive
;
78 Key
->KeyCellOffset
= KeyCellOffset
;
79 Key
->KeyCell
= HvGetCell (&RegistryHive
->Hive
, Key
->KeyCellOffset
);
85 Key
->LinkedKey
= NULL
;
93 IN BOOL AllowCreation
,
98 UNICODE_STRING KeyString
;
103 PCM_KEY_NODE SubKeyCell
;
104 HCELL_INDEX BlockOffset
;
106 DPRINT("RegpCreateOpenKey('%S')\n", KeyName
);
108 if (*KeyName
== L
'\\')
113 else if (hParentKey
== NULL
)
119 ParentKey
= HKEY_TO_MEMKEY(RootKey
);
122 LocalKeyName
= (PWSTR
)KeyName
;
125 End
= wcschr(LocalKeyName
, '\\');
128 KeyString
.Buffer
= LocalKeyName
;
129 KeyString
.Length
= KeyString
.MaximumLength
=
130 (USHORT
)((ULONG_PTR
)End
- (ULONG_PTR
)LocalKeyName
);
133 RtlInitUnicodeString(&KeyString
, LocalKeyName
);
135 /* Check subkey in memory structure */
136 Ptr
= ParentKey
->SubKeyList
.Flink
;
137 while (Ptr
!= &ParentKey
->SubKeyList
)
139 CurrentKey
= CONTAINING_RECORD(Ptr
, KEY
, KeyList
);
140 if (CurrentKey
->NameSize
== KeyString
.Length
141 && memcmp(CurrentKey
->Name
, KeyString
.Buffer
, KeyString
.Length
) == 0)
149 Status
= CmiScanForSubKey(
150 ParentKey
->RegistryHive
,
156 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
158 Status
= CmiAddSubKey(
159 ParentKey
->RegistryHive
,
161 ParentKey
->KeyCellOffset
,
167 if (!NT_SUCCESS(Status
))
168 return ERROR_UNSUCCESSFUL
;
170 /* Now, SubKeyCell/BlockOffset are valid */
171 CurrentKey
= CreateInMemoryStructure(
172 ParentKey
->RegistryHive
,
176 return ERROR_OUTOFMEMORY
;
178 /* Add CurrentKey in ParentKey */
179 InsertTailList(&ParentKey
->SubKeyList
, &CurrentKey
->KeyList
);
180 ParentKey
->SubKeyCount
++;
183 /* Follow registry links */
184 while (CurrentKey
->LinkedKey
)
185 CurrentKey
= CurrentKey
->LinkedKey
;
186 ParentKey
= CurrentKey
;
188 LocalKeyName
= End
+ 1;
193 *Key
= MEMKEY_TO_HKEY(ParentKey
);
195 return ERROR_SUCCESS
;
204 return RegpOpenOrCreateKey(hKey
, lpSubKey
, TRUE
, phkResult
);
209 IN PCSTR MultiByteString
)
212 UNICODE_STRING Destination
;
215 RtlInitAnsiString(&Source
, MultiByteString
);
216 Status
= RtlAnsiStringToUnicodeString(&Destination
, &Source
, TRUE
);
217 if (!NT_SUCCESS(Status
))
219 return Destination
.Buffer
;
231 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
233 return ERROR_OUTOFMEMORY
;
235 rc
= RegCreateKeyW(hKey
, lpSubKeyW
, phkResult
);
241 RegDeleteKeyA(HKEY Key
,
244 if (Name
!= NULL
&& strchr(Name
, '\\') != NULL
)
245 return(ERROR_INVALID_PARAMETER
);
249 return(ERROR_SUCCESS
);
258 return RegpOpenOrCreateKey(hKey
, lpSubKey
, FALSE
, phkResult
);
270 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
272 return ERROR_OUTOFMEMORY
;
274 rc
= RegOpenKeyW(hKey
, lpSubKeyW
, phkResult
);
280 RegpOpenOrCreateValue(
282 IN LPCWSTR ValueName
,
283 IN BOOL AllowCreation
,
284 OUT PCM_KEY_VALUE
*ValueCell
,
285 OUT PHCELL_INDEX ValueCellOffset
)
288 UNICODE_STRING ValueString
;
291 ParentKey
= HKEY_TO_MEMKEY(hKey
);
292 RtlInitUnicodeString(&ValueString
, ValueName
);
294 Status
= CmiScanForValueKey(
295 ParentKey
->RegistryHive
,
300 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
302 Status
= CmiAddValueKey(
303 ParentKey
->RegistryHive
,
305 ParentKey
->KeyCellOffset
,
310 if (!NT_SUCCESS(Status
))
312 return STATUS_SUCCESS
;
318 IN LPCWSTR lpValueName OPTIONAL
,
321 IN
const PUCHAR lpData
,
326 PCM_KEY_VALUE ValueCell
;
327 HCELL_INDEX ValueCellOffset
;
332 if (dwType
== REG_LINK
)
334 /* Special handling of registry links */
335 if (cbData
!= sizeof(PVOID
))
336 return STATUS_INVALID_PARAMETER
;
337 phKey
= (PHKEY
)lpData
;
338 Key
= HKEY_TO_MEMKEY(hKey
);
339 DestKey
= HKEY_TO_MEMKEY(*phKey
);
341 /* Create the link in memory */
342 Key
->LinkedKey
= DestKey
;
344 /* Create the link in registry hive (if applicable) */
345 if (Key
->RegistryHive
!= DestKey
->RegistryHive
)
346 return STATUS_SUCCESS
;
347 DPRINT1("Save link to registry\n");
348 return STATUS_NOT_IMPLEMENTED
;
351 if ((cbData
& REG_DATA_SIZE_MASK
) != cbData
)
352 return STATUS_UNSUCCESSFUL
;
354 Key
= HKEY_TO_MEMKEY(hKey
);
356 Status
= RegpOpenOrCreateValue(hKey
, lpValueName
, TRUE
, &ValueCell
, &ValueCellOffset
);
357 if (!NT_SUCCESS(Status
))
358 return ERROR_UNSUCCESSFUL
;
360 /* Get size of the allocated cellule (if any) */
361 if (!(ValueCell
->DataSize
& REG_DATA_IN_OFFSET
) &&
362 (ValueCell
->DataSize
& REG_DATA_SIZE_MASK
) != 0)
364 DataCell
= HvGetCell(&Key
->RegistryHive
->Hive
, ValueCell
->DataOffset
);
366 return ERROR_UNSUCCESSFUL
;
367 DataCellSize
= -HvGetCellSize(&Key
->RegistryHive
->Hive
, DataCell
);
375 if (cbData
<= sizeof(HCELL_INDEX
))
377 /* If data size <= sizeof(HCELL_INDEX) then store data in the data offset */
378 DPRINT("ValueCell->DataSize %lu\n", ValueCell
->DataSize
);
380 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->DataOffset
);
382 RtlCopyMemory(&ValueCell
->DataOffset
, lpData
, cbData
);
383 ValueCell
->DataSize
= (ULONG
)(cbData
| REG_DATA_IN_OFFSET
);
384 ValueCell
->DataType
= dwType
;
385 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
);
389 if (cbData
> (SIZE_T
)DataCellSize
)
391 /* New data size is larger than the current, destroy current
392 * data block and allocate a new one. */
393 HCELL_INDEX NewOffset
;
395 DPRINT("ValueCell->DataSize %lu\n", ValueCell
->DataSize
);
397 NewOffset
= HvAllocateCell(&Key
->RegistryHive
->Hive
, cbData
, HvStable
);
398 if (NewOffset
== HCELL_NULL
)
400 DPRINT("HvAllocateCell() failed with status 0x%08lx\n", Status
);
401 return ERROR_UNSUCCESSFUL
;
405 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->DataOffset
);
407 ValueCell
->DataOffset
= NewOffset
;
408 DataCell
= HvGetCell(&Key
->RegistryHive
->Hive
, NewOffset
);
411 /* Copy new contents to cellule */
412 RtlCopyMemory(DataCell
, lpData
, cbData
);
413 ValueCell
->DataSize
= (ULONG
)(cbData
& REG_DATA_SIZE_MASK
);
414 ValueCell
->DataType
= dwType
;
415 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCell
->DataOffset
);
416 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
);
419 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, Key
->KeyCellOffset
);
421 DPRINT("Return status 0x%08lx\n", Status
);
428 IN LPCSTR lpValueName OPTIONAL
,
431 IN
const PUCHAR lpData
,
434 LPWSTR lpValueNameW
= NULL
;
437 LONG rc
= ERROR_SUCCESS
;
439 DPRINT("RegSetValueA(%s)\n", lpValueName
);
442 lpValueNameW
= MultiByteToWideChar(lpValueName
);
444 return ERROR_OUTOFMEMORY
;
447 if ((dwType
== REG_SZ
|| dwType
== REG_EXPAND_SZ
|| dwType
== REG_MULTI_SZ
)
450 ANSI_STRING AnsiString
;
453 if (lpData
[cbData
- 1] != '\0')
455 RtlInitAnsiString(&AnsiString
, NULL
);
456 AnsiString
.Buffer
= (PSTR
)lpData
;
457 AnsiString
.Length
= (USHORT
)cbData
- 1;
458 AnsiString
.MaximumLength
= (USHORT
)cbData
;
459 RtlAnsiStringToUnicodeString (&Data
, &AnsiString
, TRUE
);
460 lpDataW
= (const PUCHAR
)Data
.Buffer
;
461 cbDataW
= Data
.MaximumLength
;
466 cbDataW
= (USHORT
)cbData
;
469 if (rc
== ERROR_SUCCESS
)
470 rc
= RegSetValueExW(hKey
, lpValueNameW
, 0, dwType
, lpDataW
, cbDataW
);
473 if (lpData
!= lpDataW
)
481 IN LPCWSTR lpValueName
,
482 IN PULONG lpReserved
,
485 OUT PSIZE_T lpcbData
)
487 //ParentKey = HKEY_TO_MEMKEY(RootKey);
488 PCM_KEY_VALUE ValueCell
;
489 HCELL_INDEX ValueCellOffset
;
492 Status
= RegpOpenOrCreateValue(
499 //Status = CmiScanForValueKey(
501 return ERROR_UNSUCCESSFUL
;
507 IN LPCSTR lpValueName
,
508 IN PULONG lpReserved
,
511 OUT PSIZE_T lpcbData
)
513 LPWSTR lpValueNameW
= NULL
;
516 DPRINT1("RegQueryValueExA(%s) not implemented\n", lpValueName
);
519 lpValueNameW
= MultiByteToWideChar(lpValueName
);
521 return ERROR_OUTOFMEMORY
;
524 rc
= RegQueryValueExW(hKey
, lpValueNameW
, lpReserved
, lpType
, lpData
, lpcbData
);
527 return ERROR_UNSUCCESSFUL
;
533 IN LPCWSTR lpValueName OPTIONAL
)
535 DPRINT1("RegDeleteValueW() unimplemented\n");
536 return ERROR_UNSUCCESSFUL
;
542 IN LPCSTR lpValueName OPTIONAL
)
549 lpValueNameW
= MultiByteToWideChar(lpValueName
);
551 return ERROR_OUTOFMEMORY
;
552 rc
= RegDeleteValueW(hKey
, lpValueNameW
);
556 rc
= RegDeleteValueW(hKey
, NULL
);
563 IN PEREGISTRY_HIVE HiveToConnect
,
570 Status
= CmiInitializeTempHive(HiveToConnect
);
571 if (!NT_SUCCESS(Status
))
573 DPRINT1("CmiInitializeTempHive() failed with status 0x%08lx\n", Status
);
582 if (rc
!= ERROR_SUCCESS
)
585 NewKey
->RegistryHive
= HiveToConnect
;
586 NewKey
->KeyCellOffset
= HiveToConnect
->Hive
.HiveHeader
->RootCell
;
587 NewKey
->KeyCell
= HvGetCell (&HiveToConnect
->Hive
, NewKey
->KeyCellOffset
);
592 MyExportBinaryHive (PCHAR FileName
,
593 PEREGISTRY_HIVE RootHive
)
598 /* Create new hive file */
599 File
= fopen (FileName
, "w+b");
602 printf(" Error creating/opening file\n");
606 fseek (File
, 0, SEEK_SET
);
608 RootHive
->HiveHandle
= (HANDLE
)File
;
609 ret
= HvWriteHive(&RootHive
->Hive
);
614 LIST_ENTRY CmiHiveListHead
;
617 RegInitializeRegistry(VOID
)
619 UNICODE_STRING RootKeyName
= RTL_CONSTANT_STRING(L
"\\");
621 HKEY ControlSetKey
, LinkKey
;
623 InitializeListHead(&CmiHiveListHead
);
625 Status
= CmiInitializeTempHive(&RootHive
);
626 if (!NT_SUCCESS(Status
))
628 DPRINT1("CmiInitializeTempHive() failed with status 0x%08lx\n", Status
);
632 RootKey
= CreateInMemoryStructure(
634 RootHive
.Hive
.HiveHeader
->RootCell
,
637 /* Create DEFAULT key */
641 L
"Registry\\User\\.DEFAULT");
647 L
"Registry\\Machine\\SAM");
649 /* Create SECURITY key */
653 L
"Registry\\Machine\\SECURITY");
655 /* Create SOFTWARE key */
659 L
"Registry\\Machine\\SOFTWARE");
661 /* Create SYSTEM key */
665 L
"Registry\\Machine\\SYSTEM");
667 /* Create link 'CurrentControlSet' --> 'ControlSet001' */
670 L
"Registry\\Machine\\SYSTEM\\ControlSet001",
674 L
"Registry\\Machine\\SYSTEM\\CurrentControlSet",
676 printf("ControlSetKey %p\n", ControlSetKey
);
677 RegSetValueExW(LinkKey
, NULL
, 0, REG_LINK
, (PCHAR
)&ControlSetKey
, sizeof(PVOID
));