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
= (PWSTR
) xwcschr(LocalKeyName
, '\\');
128 KeyString
.Buffer
= LocalKeyName
;
129 KeyString
.Length
= KeyString
.MaximumLength
=
130 (USHORT
)((ULONG_PTR
)End
- (ULONG_PTR
)LocalKeyName
);
133 RtlInitUnicodeString(&KeyString
, LocalKeyName
);
135 /* Redirect from 'CurrentControlSet' to 'ControlSet001' */
136 if (!wcsncmp(LocalKeyName
, L
"CurrentControlSet", 17))
137 RtlInitUnicodeString(&KeyString
, L
"ControlSet001");
139 /* Check subkey in memory structure */
140 Ptr
= ParentKey
->SubKeyList
.Flink
;
141 while (Ptr
!= &ParentKey
->SubKeyList
)
143 CurrentKey
= CONTAINING_RECORD(Ptr
, KEY
, KeyList
);
144 if (CurrentKey
->NameSize
== KeyString
.Length
145 && memcmp(CurrentKey
->Name
, KeyString
.Buffer
, KeyString
.Length
) == 0)
153 Status
= CmiScanForSubKey(
154 ParentKey
->RegistryHive
,
157 OBJ_CASE_INSENSITIVE
,
160 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
162 Status
= CmiAddSubKey(
163 ParentKey
->RegistryHive
,
165 ParentKey
->KeyCellOffset
,
171 if (!NT_SUCCESS(Status
))
172 return ERROR_UNSUCCESSFUL
;
174 /* Now, SubKeyCell/BlockOffset are valid */
175 CurrentKey
= CreateInMemoryStructure(
176 ParentKey
->RegistryHive
,
180 return ERROR_OUTOFMEMORY
;
182 /* Add CurrentKey in ParentKey */
183 InsertTailList(&ParentKey
->SubKeyList
, &CurrentKey
->KeyList
);
184 ParentKey
->SubKeyCount
++;
187 ParentKey
= CurrentKey
;
189 LocalKeyName
= End
+ 1;
194 *Key
= MEMKEY_TO_HKEY(ParentKey
);
196 return ERROR_SUCCESS
;
205 return RegpOpenOrCreateKey(hKey
, lpSubKey
, TRUE
, phkResult
);
210 IN PCSTR MultiByteString
)
213 UNICODE_STRING Destination
;
216 RtlInitAnsiString(&Source
, MultiByteString
);
217 Status
= RtlAnsiStringToUnicodeString(&Destination
, &Source
, TRUE
);
218 if (!NT_SUCCESS(Status
))
220 return Destination
.Buffer
;
232 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
234 return ERROR_OUTOFMEMORY
;
236 rc
= RegCreateKeyW(hKey
, lpSubKeyW
, phkResult
);
242 RegDeleteKeyA(HKEY Key
,
245 if (Name
!= NULL
&& strchr(Name
, '\\') != NULL
)
246 return(ERROR_INVALID_PARAMETER
);
250 return(ERROR_SUCCESS
);
259 return RegpOpenOrCreateKey(hKey
, lpSubKey
, FALSE
, phkResult
);
271 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
273 return ERROR_OUTOFMEMORY
;
275 rc
= RegOpenKeyW(hKey
, lpSubKeyW
, phkResult
);
281 RegpOpenOrCreateValue(
283 IN LPCWSTR ValueName
,
284 IN BOOL AllowCreation
,
285 OUT PCM_KEY_VALUE
*ValueCell
,
286 OUT PHCELL_INDEX ValueCellOffset
)
289 UNICODE_STRING ValueString
;
292 ParentKey
= HKEY_TO_MEMKEY(hKey
);
293 RtlInitUnicodeString(&ValueString
, ValueName
);
295 Status
= CmiScanForValueKey(
296 ParentKey
->RegistryHive
,
301 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
303 Status
= CmiAddValueKey(
304 ParentKey
->RegistryHive
,
306 ParentKey
->KeyCellOffset
,
311 if (!NT_SUCCESS(Status
))
312 return ERROR_UNSUCCESSFUL
;
313 return ERROR_SUCCESS
;
319 IN LPCWSTR lpValueName OPTIONAL
,
322 IN
const UCHAR
* lpData
,
327 PCM_KEY_VALUE ValueCell
;
328 HCELL_INDEX ValueCellOffset
;
333 if (dwType
== REG_LINK
)
335 /* Special handling of registry links */
336 if (cbData
!= sizeof(PVOID
))
337 return STATUS_INVALID_PARAMETER
;
338 phKey
= (PHKEY
)lpData
;
339 Key
= HKEY_TO_MEMKEY(hKey
);
340 DestKey
= HKEY_TO_MEMKEY(*phKey
);
342 /* Create the link in memory */
343 Key
->DataType
= REG_LINK
;
344 Key
->LinkedKey
= DestKey
;
346 /* Create the link in registry hive (if applicable) */
347 if (Key
->RegistryHive
!= DestKey
->RegistryHive
)
348 return STATUS_SUCCESS
;
349 DPRINT1("Save link to registry\n");
350 return STATUS_NOT_IMPLEMENTED
;
353 if ((cbData
& REG_DATA_SIZE_MASK
) != cbData
)
354 return STATUS_UNSUCCESSFUL
;
356 Key
= HKEY_TO_MEMKEY(hKey
);
358 Status
= RegpOpenOrCreateValue(hKey
, lpValueName
, TRUE
, &ValueCell
, &ValueCellOffset
);
359 if (!NT_SUCCESS(Status
))
360 return ERROR_UNSUCCESSFUL
;
362 /* Get size of the allocated cellule (if any) */
363 if (!(ValueCell
->DataSize
& REG_DATA_IN_OFFSET
) &&
364 (ValueCell
->DataSize
& REG_DATA_SIZE_MASK
) != 0)
366 DataCell
= HvGetCell(&Key
->RegistryHive
->Hive
, ValueCell
->DataOffset
);
368 return ERROR_UNSUCCESSFUL
;
369 DataCellSize
= -HvGetCellSize(&Key
->RegistryHive
->Hive
, DataCell
);
377 if (cbData
<= sizeof(HCELL_INDEX
))
379 /* If data size <= sizeof(HCELL_INDEX) then store data in the data offset */
380 DPRINT("ValueCell->DataSize %lu\n", ValueCell
->DataSize
);
382 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->DataOffset
);
384 RtlCopyMemory(&ValueCell
->DataOffset
, lpData
, cbData
);
385 ValueCell
->DataSize
= (ULONG
)(cbData
| REG_DATA_IN_OFFSET
);
386 ValueCell
->DataType
= dwType
;
387 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
);
391 if (cbData
> (SIZE_T
)DataCellSize
)
393 /* New data size is larger than the current, destroy current
394 * data block and allocate a new one. */
395 HCELL_INDEX NewOffset
;
397 DPRINT("ValueCell->DataSize %lu\n", ValueCell
->DataSize
);
399 NewOffset
= HvAllocateCell(&Key
->RegistryHive
->Hive
, cbData
, HvStable
);
400 if (NewOffset
== HCELL_NULL
)
402 DPRINT("HvAllocateCell() failed with status 0x%08lx\n", Status
);
403 return ERROR_UNSUCCESSFUL
;
407 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->DataOffset
);
409 ValueCell
->DataOffset
= NewOffset
;
410 DataCell
= HvGetCell(&Key
->RegistryHive
->Hive
, NewOffset
);
413 /* Copy new contents to cellule */
414 RtlCopyMemory(DataCell
, lpData
, cbData
);
415 ValueCell
->DataSize
= (ULONG
)(cbData
& REG_DATA_SIZE_MASK
);
416 ValueCell
->DataType
= dwType
;
417 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCell
->DataOffset
);
418 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
);
421 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, Key
->KeyCellOffset
);
423 DPRINT("Return status 0x%08lx\n", Status
);
430 IN LPCSTR lpValueName OPTIONAL
,
433 IN
const UCHAR
* lpData
,
436 LPWSTR lpValueNameW
= NULL
;
437 const UCHAR
* lpDataW
;
439 LONG rc
= ERROR_SUCCESS
;
441 DPRINT("RegSetValueA(%s)\n", lpValueName
);
444 lpValueNameW
= MultiByteToWideChar(lpValueName
);
446 return ERROR_OUTOFMEMORY
;
449 if ((dwType
== REG_SZ
|| dwType
== REG_EXPAND_SZ
|| dwType
== REG_MULTI_SZ
)
452 ANSI_STRING AnsiString
;
455 if (lpData
[cbData
- 1] != '\0')
457 RtlInitAnsiString(&AnsiString
, NULL
);
458 AnsiString
.Buffer
= (PSTR
)lpData
;
459 AnsiString
.Length
= (USHORT
)cbData
- 1;
460 AnsiString
.MaximumLength
= (USHORT
)cbData
;
461 RtlAnsiStringToUnicodeString (&Data
, &AnsiString
, TRUE
);
462 lpDataW
= (const UCHAR
*)Data
.Buffer
;
463 cbDataW
= Data
.MaximumLength
;
468 cbDataW
= (USHORT
)cbData
;
471 if (rc
== ERROR_SUCCESS
)
472 rc
= RegSetValueExW(hKey
, lpValueNameW
, 0, dwType
, lpDataW
, cbDataW
);
475 if (lpData
!= lpDataW
)
476 free((PVOID
)lpDataW
);
483 IN LPCWSTR lpValueName
,
484 IN PULONG lpReserved
,
487 OUT PSIZE_T lpcbData
)
489 //ParentKey = HKEY_TO_MEMKEY(RootKey);
490 PCM_KEY_VALUE ValueCell
;
491 HCELL_INDEX ValueCellOffset
;
494 rc
= RegpOpenOrCreateValue(
500 if (rc
!= ERROR_SUCCESS
)
503 DPRINT1("RegQueryValueExW(%S) not implemented\n", lpValueName
);
504 /* ValueCell and ValueCellOffset are valid */
506 return ERROR_UNSUCCESSFUL
;
512 IN LPCSTR lpValueName
,
513 IN PULONG lpReserved
,
516 OUT PSIZE_T lpcbData
)
518 LPWSTR lpValueNameW
= NULL
;
523 lpValueNameW
= MultiByteToWideChar(lpValueName
);
525 return ERROR_OUTOFMEMORY
;
528 rc
= RegQueryValueExW(hKey
, lpValueNameW
, lpReserved
, lpType
, lpData
, lpcbData
);
531 return ERROR_UNSUCCESSFUL
;
537 IN LPCWSTR lpValueName OPTIONAL
)
539 DPRINT1("RegDeleteValueW() unimplemented\n");
540 return ERROR_UNSUCCESSFUL
;
546 IN LPCSTR lpValueName OPTIONAL
)
553 lpValueNameW
= MultiByteToWideChar(lpValueName
);
555 return ERROR_OUTOFMEMORY
;
556 rc
= RegDeleteValueW(hKey
, lpValueNameW
);
560 rc
= RegDeleteValueW(hKey
, NULL
);
567 IN PEREGISTRY_HIVE HiveToConnect
,
574 Status
= CmiInitializeTempHive(HiveToConnect
);
575 if (!NT_SUCCESS(Status
))
577 DPRINT1("CmiInitializeTempHive() failed with status 0x%08lx\n", Status
);
586 if (rc
!= ERROR_SUCCESS
)
589 NewKey
->RegistryHive
= HiveToConnect
;
590 NewKey
->KeyCellOffset
= HiveToConnect
->Hive
.HiveHeader
->RootCell
;
591 NewKey
->KeyCell
= HvGetCell (&HiveToConnect
->Hive
, NewKey
->KeyCellOffset
);
596 MyExportBinaryHive (PCHAR FileName
,
597 PEREGISTRY_HIVE RootHive
)
602 /* Create new hive file */
603 File
= fopen (FileName
, "w+b");
606 printf(" Error creating/opening file\n");
610 fseek (File
, 0, SEEK_SET
);
612 RootHive
->HiveHandle
= (HANDLE
)File
;
613 ret
= HvWriteHive(&RootHive
->Hive
);
618 LIST_ENTRY CmiHiveListHead
;
621 RegInitializeRegistry(VOID
)
623 UNICODE_STRING RootKeyName
= RTL_CONSTANT_STRING(L
"\\");
625 HKEY ControlSetKey
, LinkKey
;
627 InitializeListHead(&CmiHiveListHead
);
629 Status
= CmiInitializeTempHive(&RootHive
);
630 if (!NT_SUCCESS(Status
))
632 DPRINT1("CmiInitializeTempHive() failed with status 0x%08lx\n", Status
);
636 RootKey
= CreateInMemoryStructure(
638 RootHive
.Hive
.HiveHeader
->RootCell
,
641 /* Create DEFAULT key */
645 L
"Registry\\User\\.DEFAULT");
651 L
"Registry\\Machine\\SAM");
653 /* Create SECURITY key */
657 L
"Registry\\Machine\\SECURITY");
659 /* Create SOFTWARE key */
663 L
"Registry\\Machine\\SOFTWARE");
665 /* Create SYSTEM key */
669 L
"Registry\\Machine\\SYSTEM");
671 /* Create 'ControlSet001' key */
674 L
"Registry\\Machine\\SYSTEM\\ControlSet001",