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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 RegDeleteKeyW()
29 * - Implement RegEnumValue()
30 * - Implement RegQueryValueExW()
40 #define REG_DATA_SIZE_MASK 0x7FFFFFFF
41 #define REG_DATA_IN_OFFSET 0x80000000
43 static CMHIVE RootHive
;
44 static MEMKEY RootKey
;
45 CMHIVE DefaultHive
; /* \Registry\User\.DEFAULT */
46 CMHIVE SamHive
; /* \Registry\Machine\SAM */
47 CMHIVE SecurityHive
; /* \Registry\Machine\SECURITY */
48 CMHIVE SoftwareHive
; /* \Registry\Machine\SOFTWARE */
49 CMHIVE SystemHive
; /* \Registry\Machine\SYSTEM */
52 CreateInMemoryStructure(
53 IN PCMHIVE RegistryHive
,
54 IN HCELL_INDEX KeyCellOffset
,
55 IN PCUNICODE_STRING KeyName
)
59 Key
= (MEMKEY
) malloc (sizeof(KEY
));
63 InitializeListHead (&Key
->SubKeyList
);
64 InitializeListHead (&Key
->ValueList
);
65 InitializeListHead (&Key
->KeyList
);
70 Key
->NameSize
= KeyName
->Length
;
71 /* FIXME: It's not enough to allocate this way, because later
72 this memory gets overwritten with bigger names */
73 Key
->Name
= malloc (Key
->NameSize
);
76 memcpy(Key
->Name
, KeyName
->Buffer
, KeyName
->Length
);
82 Key
->RegistryHive
= RegistryHive
;
83 Key
->KeyCellOffset
= KeyCellOffset
;
84 Key
->KeyCell
= (PCM_KEY_NODE
)HvGetCell (&RegistryHive
->Hive
, Key
->KeyCellOffset
);
91 Key
->KeyCell
->SubKeyLists
[Stable
] = HCELL_NIL
;
92 Key
->KeyCell
->SubKeyLists
[Volatile
] = HCELL_NIL
;
93 Key
->LinkedKey
= NULL
;
101 IN BOOL AllowCreation
,
106 UNICODE_STRING KeyString
;
111 PCM_KEY_NODE SubKeyCell
;
112 HCELL_INDEX BlockOffset
;
114 DPRINT("RegpCreateOpenKey('%S')\n", KeyName
);
116 if (*KeyName
== L
'\\')
121 else if (hParentKey
== NULL
)
127 ParentKey
= HKEY_TO_MEMKEY(RootKey
);
130 LocalKeyName
= (PWSTR
)KeyName
;
133 End
= (PWSTR
)strchrW(LocalKeyName
, '\\');
136 KeyString
.Buffer
= LocalKeyName
;
137 KeyString
.Length
= KeyString
.MaximumLength
=
138 (USHORT
)((ULONG_PTR
)End
- (ULONG_PTR
)LocalKeyName
);
142 RtlInitUnicodeString(&KeyString
, LocalKeyName
);
143 if (KeyString
.Length
== 0)
145 /* Trailing backslash char; we're done */
150 /* Redirect from 'CurrentControlSet' to 'ControlSet001' */
151 if (!strncmpW(LocalKeyName
, L
"CurrentControlSet", 17) &&
152 ParentKey
->NameSize
== 12 &&
153 !memcmp(ParentKey
->Name
, L
"SYSTEM", 12))
154 RtlInitUnicodeString(&KeyString
, L
"ControlSet001");
156 /* Check subkey in memory structure */
157 Ptr
= ParentKey
->SubKeyList
.Flink
;
158 while (Ptr
!= &ParentKey
->SubKeyList
)
160 CurrentKey
= CONTAINING_RECORD(Ptr
, KEY
, KeyList
);
161 if (CurrentKey
->NameSize
== KeyString
.Length
162 && memcmp(CurrentKey
->Name
, KeyString
.Buffer
, KeyString
.Length
) == 0)
170 Status
= CmiScanForSubKey(
171 ParentKey
->RegistryHive
,
174 OBJ_CASE_INSENSITIVE
,
177 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
179 Status
= CmiAddSubKey(
180 ParentKey
->RegistryHive
,
182 ParentKey
->KeyCellOffset
,
188 if (!NT_SUCCESS(Status
))
189 return ERROR_UNSUCCESSFUL
;
191 /* Now, SubKeyCell/BlockOffset are valid */
192 CurrentKey
= CreateInMemoryStructure(
193 ParentKey
->RegistryHive
,
197 return ERROR_OUTOFMEMORY
;
199 /* Add CurrentKey in ParentKey */
200 InsertTailList(&ParentKey
->SubKeyList
, &CurrentKey
->KeyList
);
201 ParentKey
->SubKeyCount
++;
204 ParentKey
= CurrentKey
;
206 LocalKeyName
= End
+ 1;
211 *Key
= MEMKEY_TO_HKEY(ParentKey
);
213 return ERROR_SUCCESS
;
222 return RegpOpenOrCreateKey(hKey
, lpSubKey
, TRUE
, phkResult
);
227 IN PCSTR MultiByteString
)
230 UNICODE_STRING Destination
;
233 RtlInitAnsiString(&Source
, MultiByteString
);
234 Status
= RtlAnsiStringToUnicodeString(&Destination
, &Source
, TRUE
);
235 if (!NT_SUCCESS(Status
))
237 return Destination
.Buffer
;
249 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
251 return ERROR_OUTOFMEMORY
;
253 rc
= RegCreateKeyW(hKey
, lpSubKeyW
, phkResult
);
263 DPRINT1("FIXME: implement RegDeleteKeyW!\n");
264 return ERROR_SUCCESS
;
272 PWSTR lpSubKeyW
= NULL
;
275 if (lpSubKey
!= NULL
&& strchr(lpSubKey
, '\\') != NULL
)
276 return ERROR_INVALID_PARAMETER
;
280 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
282 return ERROR_OUTOFMEMORY
;
285 rc
= RegDeleteKeyW(hKey
, lpSubKeyW
);
299 return RegpOpenOrCreateKey(hKey
, lpSubKey
, FALSE
, phkResult
);
311 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
313 return ERROR_OUTOFMEMORY
;
315 rc
= RegOpenKeyW(hKey
, lpSubKeyW
, phkResult
);
321 RegpOpenOrCreateValue(
323 IN LPCWSTR ValueName
,
324 IN BOOL AllowCreation
,
325 OUT PCM_KEY_VALUE
*ValueCell
,
326 OUT PHCELL_INDEX ValueCellOffset
)
329 UNICODE_STRING ValueString
;
332 ParentKey
= HKEY_TO_MEMKEY(hKey
);
333 RtlInitUnicodeString(&ValueString
, ValueName
);
335 Status
= CmiScanForValueKey(
336 ParentKey
->RegistryHive
,
341 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
343 Status
= CmiAddValueKey(
344 ParentKey
->RegistryHive
,
346 ParentKey
->KeyCellOffset
,
351 if (!NT_SUCCESS(Status
))
352 return ERROR_UNSUCCESSFUL
;
353 return ERROR_SUCCESS
;
359 IN LPCWSTR lpValueName OPTIONAL
,
362 IN
const UCHAR
* lpData
,
367 PCM_KEY_VALUE ValueCell
;
368 HCELL_INDEX ValueCellOffset
;
373 if (dwType
== REG_LINK
)
375 /* Special handling of registry links */
376 if (cbData
!= sizeof(PVOID
))
377 return STATUS_INVALID_PARAMETER
;
378 phKey
= (PHKEY
)lpData
;
379 Key
= HKEY_TO_MEMKEY(hKey
);
380 DestKey
= HKEY_TO_MEMKEY(*phKey
);
382 /* Create the link in memory */
383 Key
->DataType
= REG_LINK
;
384 Key
->LinkedKey
= DestKey
;
386 /* Create the link in registry hive (if applicable) */
387 if (Key
->RegistryHive
!= DestKey
->RegistryHive
)
388 return STATUS_SUCCESS
;
389 DPRINT1("Save link to registry\n");
390 return STATUS_NOT_IMPLEMENTED
;
393 if ((cbData
& REG_DATA_SIZE_MASK
) != cbData
)
394 return STATUS_UNSUCCESSFUL
;
396 Key
= HKEY_TO_MEMKEY(hKey
);
398 Status
= RegpOpenOrCreateValue(hKey
, lpValueName
, TRUE
, &ValueCell
, &ValueCellOffset
);
399 if (!NT_SUCCESS(Status
))
400 return ERROR_UNSUCCESSFUL
;
402 /* Get size of the allocated cellule (if any) */
403 if (!(ValueCell
->DataLength
& REG_DATA_IN_OFFSET
) &&
404 (ValueCell
->DataLength
& REG_DATA_SIZE_MASK
) != 0)
406 DataCell
= HvGetCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
408 return ERROR_UNSUCCESSFUL
;
409 DataCellSize
= -HvGetCellSize(&Key
->RegistryHive
->Hive
, DataCell
);
417 if (cbData
<= sizeof(HCELL_INDEX
))
419 /* If data size <= sizeof(HCELL_INDEX) then store data in the data offset */
420 DPRINT("ValueCell->DataLength %u\n", ValueCell
->DataLength
);
422 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
424 RtlCopyMemory(&ValueCell
->Data
, lpData
, cbData
);
425 ValueCell
->DataLength
= (ULONG
)(cbData
| REG_DATA_IN_OFFSET
);
426 ValueCell
->Type
= dwType
;
427 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
, FALSE
);
431 if (cbData
> (SIZE_T
)DataCellSize
)
433 /* New data size is larger than the current, destroy current
434 * data block and allocate a new one. */
435 HCELL_INDEX NewOffset
;
437 DPRINT("ValueCell->DataLength %u\n", ValueCell
->DataLength
);
439 NewOffset
= HvAllocateCell(&Key
->RegistryHive
->Hive
, cbData
, Stable
, HCELL_NIL
);
440 if (NewOffset
== HCELL_NIL
)
442 DPRINT("HvAllocateCell() failed with status 0x%08x\n", Status
);
443 return ERROR_UNSUCCESSFUL
;
447 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
449 ValueCell
->Data
= NewOffset
;
450 DataCell
= (PVOID
)HvGetCell(&Key
->RegistryHive
->Hive
, NewOffset
);
453 /* Copy new contents to cellule */
454 RtlCopyMemory(DataCell
, lpData
, cbData
);
455 ValueCell
->DataLength
= (ULONG
)(cbData
& REG_DATA_SIZE_MASK
);
456 ValueCell
->Type
= dwType
;
457 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCell
->Data
, FALSE
);
458 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
, FALSE
);
461 if (cbData
> Key
->KeyCell
->MaxValueDataLen
)
462 Key
->KeyCell
->MaxValueDataLen
= cbData
;
464 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, Key
->KeyCellOffset
, FALSE
);
466 DPRINT("Return status 0x%08x\n", Status
);
473 IN LPCSTR lpValueName OPTIONAL
,
476 IN
const UCHAR
* lpData
,
479 LPWSTR lpValueNameW
= NULL
;
480 const UCHAR
* lpDataW
;
482 LONG rc
= ERROR_SUCCESS
;
484 DPRINT("RegSetValueA(%s)\n", lpValueName
);
487 lpValueNameW
= MultiByteToWideChar(lpValueName
);
489 return ERROR_OUTOFMEMORY
;
492 if ((dwType
== REG_SZ
|| dwType
== REG_EXPAND_SZ
|| dwType
== REG_MULTI_SZ
)
495 ANSI_STRING AnsiString
;
498 if (lpData
[cbData
- 1] != '\0')
500 RtlInitAnsiString(&AnsiString
, NULL
);
501 AnsiString
.Buffer
= (PSTR
)lpData
;
502 AnsiString
.Length
= (USHORT
)cbData
- 1;
503 AnsiString
.MaximumLength
= (USHORT
)cbData
;
504 RtlAnsiStringToUnicodeString (&Data
, &AnsiString
, TRUE
);
505 lpDataW
= (const UCHAR
*)Data
.Buffer
;
506 cbDataW
= Data
.MaximumLength
;
511 cbDataW
= (USHORT
)cbData
;
514 if (rc
== ERROR_SUCCESS
)
515 rc
= RegSetValueExW(hKey
, lpValueNameW
, 0, dwType
, lpDataW
, cbDataW
);
518 if (lpData
!= lpDataW
)
519 free((PVOID
)lpDataW
);
526 IN LPCWSTR lpValueName
,
527 IN PULONG lpReserved
,
530 OUT PSIZE_T lpcbData
)
532 //ParentKey = HKEY_TO_MEMKEY(RootKey);
533 PCM_KEY_VALUE ValueCell
;
534 HCELL_INDEX ValueCellOffset
;
537 rc
= RegpOpenOrCreateValue(
543 if (rc
!= ERROR_SUCCESS
)
546 DPRINT1("RegQueryValueExW(%S) not implemented\n", lpValueName
);
547 /* ValueCell and ValueCellOffset are valid */
549 return ERROR_UNSUCCESSFUL
;
555 IN LPCSTR lpValueName
,
556 IN PULONG lpReserved
,
559 OUT PSIZE_T lpcbData
)
561 LPWSTR lpValueNameW
= NULL
;
566 lpValueNameW
= MultiByteToWideChar(lpValueName
);
568 return ERROR_OUTOFMEMORY
;
571 rc
= RegQueryValueExW(hKey
, lpValueNameW
, lpReserved
, lpType
, lpData
, lpcbData
);
580 IN LPCWSTR lpValueName OPTIONAL
)
582 DPRINT1("RegDeleteValueW() unimplemented\n");
583 return ERROR_UNSUCCESSFUL
;
589 IN LPCSTR lpValueName OPTIONAL
)
596 lpValueNameW
= MultiByteToWideChar(lpValueName
);
598 return ERROR_OUTOFMEMORY
;
599 rc
= RegDeleteValueW(hKey
, lpValueNameW
);
603 rc
= RegDeleteValueW(hKey
, NULL
);
610 IN PCMHIVE HiveToConnect
,
617 Status
= CmiInitializeTempHive(HiveToConnect
);
618 if (!NT_SUCCESS(Status
))
620 DPRINT1("CmiInitializeTempHive() failed with status 0x%08x\n", Status
);
629 if (rc
!= ERROR_SUCCESS
)
632 NewKey
->RegistryHive
= HiveToConnect
;
633 NewKey
->KeyCellOffset
= HiveToConnect
->Hive
.BaseBlock
->RootCell
;
634 NewKey
->KeyCell
= (PCM_KEY_NODE
)HvGetCell (&HiveToConnect
->Hive
, NewKey
->KeyCellOffset
);
638 LIST_ENTRY CmiHiveListHead
;
641 RegInitializeRegistry(VOID
)
643 UNICODE_STRING RootKeyName
= RTL_CONSTANT_STRING(L
"\\");
647 InitializeListHead(&CmiHiveListHead
);
649 Status
= CmiInitializeTempHive(&RootHive
);
650 if (!NT_SUCCESS(Status
))
652 DPRINT1("CmiInitializeTempHive() failed with status 0x%08x\n", Status
);
656 RootKey
= CreateInMemoryStructure(
658 RootHive
.Hive
.BaseBlock
->RootCell
,
661 /* Create DEFAULT key */
665 L
"Registry\\User\\.DEFAULT");
671 L
"Registry\\Machine\\SAM");
673 /* Create SECURITY key */
677 L
"Registry\\Machine\\SECURITY");
679 /* Create SOFTWARE key */
683 L
"Registry\\Machine\\SOFTWARE");
685 /* Create SYSTEM key */
689 L
"Registry\\Machine\\SYSTEM");
691 /* Create 'ControlSet001' key */
694 L
"Registry\\Machine\\SYSTEM\\ControlSet001",
699 RegShutdownRegistry(VOID
)
701 /* FIXME: clean up the complete hive */