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 RegDeleteKey()
29 * - Implement RegEnumValue()
30 * - Implement RegQueryValueExA()
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
);
90 Key
->LinkedKey
= NULL
;
98 IN BOOL AllowCreation
,
103 UNICODE_STRING KeyString
;
108 PCM_KEY_NODE SubKeyCell
;
109 HCELL_INDEX BlockOffset
;
111 DPRINT("RegpCreateOpenKey('%S')\n", KeyName
);
113 if (*KeyName
== L
'\\')
118 else if (hParentKey
== NULL
)
124 ParentKey
= HKEY_TO_MEMKEY(RootKey
);
127 LocalKeyName
= (PWSTR
)KeyName
;
130 End
= (PWSTR
) utf16_wcschr(LocalKeyName
, '\\');
133 KeyString
.Buffer
= LocalKeyName
;
134 KeyString
.Length
= KeyString
.MaximumLength
=
135 (USHORT
)((ULONG_PTR
)End
- (ULONG_PTR
)LocalKeyName
);
138 RtlInitUnicodeString(&KeyString
, LocalKeyName
);
140 /* Redirect from 'CurrentControlSet' to 'ControlSet001' */
141 if (!utf16_wcsncmp(LocalKeyName
, L
"CurrentControlSet", 17) &&
142 ParentKey
->NameSize
== 12 &&
143 !memcmp(ParentKey
->Name
, L
"SYSTEM", 12))
144 RtlInitUnicodeString(&KeyString
, L
"ControlSet001");
146 /* Check subkey in memory structure */
147 Ptr
= ParentKey
->SubKeyList
.Flink
;
148 while (Ptr
!= &ParentKey
->SubKeyList
)
150 CurrentKey
= CONTAINING_RECORD(Ptr
, KEY
, KeyList
);
151 if (CurrentKey
->NameSize
== KeyString
.Length
152 && memcmp(CurrentKey
->Name
, KeyString
.Buffer
, KeyString
.Length
) == 0)
160 Status
= CmiScanForSubKey(
161 ParentKey
->RegistryHive
,
164 OBJ_CASE_INSENSITIVE
,
167 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
169 Status
= CmiAddSubKey(
170 ParentKey
->RegistryHive
,
172 ParentKey
->KeyCellOffset
,
178 if (!NT_SUCCESS(Status
))
179 return ERROR_UNSUCCESSFUL
;
181 /* Now, SubKeyCell/BlockOffset are valid */
182 CurrentKey
= CreateInMemoryStructure(
183 ParentKey
->RegistryHive
,
187 return ERROR_OUTOFMEMORY
;
189 /* Add CurrentKey in ParentKey */
190 InsertTailList(&ParentKey
->SubKeyList
, &CurrentKey
->KeyList
);
191 ParentKey
->SubKeyCount
++;
194 ParentKey
= CurrentKey
;
196 LocalKeyName
= End
+ 1;
201 *Key
= MEMKEY_TO_HKEY(ParentKey
);
203 return ERROR_SUCCESS
;
212 return RegpOpenOrCreateKey(hKey
, lpSubKey
, TRUE
, phkResult
);
217 IN PCSTR MultiByteString
)
220 UNICODE_STRING Destination
;
223 RtlInitAnsiString(&Source
, MultiByteString
);
224 Status
= RtlAnsiStringToUnicodeString(&Destination
, &Source
, TRUE
);
225 if (!NT_SUCCESS(Status
))
227 return Destination
.Buffer
;
239 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
241 return ERROR_OUTOFMEMORY
;
243 rc
= RegCreateKeyW(hKey
, lpSubKeyW
, phkResult
);
249 RegDeleteKeyA(HKEY Key
,
252 if (Name
!= NULL
&& strchr(Name
, '\\') != NULL
)
253 return(ERROR_INVALID_PARAMETER
);
257 return(ERROR_SUCCESS
);
266 return RegpOpenOrCreateKey(hKey
, lpSubKey
, FALSE
, phkResult
);
278 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
280 return ERROR_OUTOFMEMORY
;
282 rc
= RegOpenKeyW(hKey
, lpSubKeyW
, phkResult
);
288 RegpOpenOrCreateValue(
290 IN LPCWSTR ValueName
,
291 IN BOOL AllowCreation
,
292 OUT PCM_KEY_VALUE
*ValueCell
,
293 OUT PHCELL_INDEX ValueCellOffset
)
296 UNICODE_STRING ValueString
;
299 ParentKey
= HKEY_TO_MEMKEY(hKey
);
300 RtlInitUnicodeString(&ValueString
, ValueName
);
302 Status
= CmiScanForValueKey(
303 ParentKey
->RegistryHive
,
308 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
310 Status
= CmiAddValueKey(
311 ParentKey
->RegistryHive
,
313 ParentKey
->KeyCellOffset
,
318 if (!NT_SUCCESS(Status
))
319 return ERROR_UNSUCCESSFUL
;
320 return ERROR_SUCCESS
;
326 IN LPCWSTR lpValueName OPTIONAL
,
329 IN
const UCHAR
* lpData
,
334 PCM_KEY_VALUE ValueCell
;
335 HCELL_INDEX ValueCellOffset
;
340 if (dwType
== REG_LINK
)
342 /* Special handling of registry links */
343 if (cbData
!= sizeof(PVOID
))
344 return STATUS_INVALID_PARAMETER
;
345 phKey
= (PHKEY
)lpData
;
346 Key
= HKEY_TO_MEMKEY(hKey
);
347 DestKey
= HKEY_TO_MEMKEY(*phKey
);
349 /* Create the link in memory */
350 Key
->DataType
= REG_LINK
;
351 Key
->LinkedKey
= DestKey
;
353 /* Create the link in registry hive (if applicable) */
354 if (Key
->RegistryHive
!= DestKey
->RegistryHive
)
355 return STATUS_SUCCESS
;
356 DPRINT1("Save link to registry\n");
357 return STATUS_NOT_IMPLEMENTED
;
360 if ((cbData
& REG_DATA_SIZE_MASK
) != cbData
)
361 return STATUS_UNSUCCESSFUL
;
363 Key
= HKEY_TO_MEMKEY(hKey
);
365 Status
= RegpOpenOrCreateValue(hKey
, lpValueName
, TRUE
, &ValueCell
, &ValueCellOffset
);
366 if (!NT_SUCCESS(Status
))
367 return ERROR_UNSUCCESSFUL
;
369 /* Get size of the allocated cellule (if any) */
370 if (!(ValueCell
->DataLength
& REG_DATA_IN_OFFSET
) &&
371 (ValueCell
->DataLength
& REG_DATA_SIZE_MASK
) != 0)
373 DataCell
= HvGetCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
375 return ERROR_UNSUCCESSFUL
;
376 DataCellSize
= -HvGetCellSize(&Key
->RegistryHive
->Hive
, DataCell
);
384 if (cbData
<= sizeof(HCELL_INDEX
))
386 /* If data size <= sizeof(HCELL_INDEX) then store data in the data offset */
387 DPRINT("ValueCell->DataLength %u\n", ValueCell
->DataLength
);
389 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
391 RtlCopyMemory(&ValueCell
->Data
, lpData
, cbData
);
392 ValueCell
->DataLength
= (ULONG
)(cbData
| REG_DATA_IN_OFFSET
);
393 ValueCell
->Type
= dwType
;
394 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
, FALSE
);
398 if (cbData
> (SIZE_T
)DataCellSize
)
400 /* New data size is larger than the current, destroy current
401 * data block and allocate a new one. */
402 HCELL_INDEX NewOffset
;
404 DPRINT("ValueCell->DataLength %u\n", ValueCell
->DataLength
);
406 NewOffset
= HvAllocateCell(&Key
->RegistryHive
->Hive
, cbData
, Stable
, HCELL_NIL
);
407 if (NewOffset
== HCELL_NIL
)
409 DPRINT("HvAllocateCell() failed with status 0x%08x\n", Status
);
410 return ERROR_UNSUCCESSFUL
;
414 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
416 ValueCell
->Data
= NewOffset
;
417 DataCell
= (PVOID
)HvGetCell(&Key
->RegistryHive
->Hive
, NewOffset
);
420 /* Copy new contents to cellule */
421 RtlCopyMemory(DataCell
, lpData
, cbData
);
422 ValueCell
->DataLength
= (ULONG
)(cbData
& REG_DATA_SIZE_MASK
);
423 ValueCell
->Type
= dwType
;
424 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCell
->Data
, FALSE
);
425 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
, FALSE
);
428 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, Key
->KeyCellOffset
, FALSE
);
430 DPRINT("Return status 0x%08x\n", Status
);
437 IN LPCSTR lpValueName OPTIONAL
,
440 IN
const UCHAR
* lpData
,
443 LPWSTR lpValueNameW
= NULL
;
444 const UCHAR
* lpDataW
;
446 LONG rc
= ERROR_SUCCESS
;
448 DPRINT("RegSetValueA(%s)\n", lpValueName
);
451 lpValueNameW
= MultiByteToWideChar(lpValueName
);
453 return ERROR_OUTOFMEMORY
;
456 if ((dwType
== REG_SZ
|| dwType
== REG_EXPAND_SZ
|| dwType
== REG_MULTI_SZ
)
459 ANSI_STRING AnsiString
;
462 if (lpData
[cbData
- 1] != '\0')
464 RtlInitAnsiString(&AnsiString
, NULL
);
465 AnsiString
.Buffer
= (PSTR
)lpData
;
466 AnsiString
.Length
= (USHORT
)cbData
- 1;
467 AnsiString
.MaximumLength
= (USHORT
)cbData
;
468 RtlAnsiStringToUnicodeString (&Data
, &AnsiString
, TRUE
);
469 lpDataW
= (const UCHAR
*)Data
.Buffer
;
470 cbDataW
= Data
.MaximumLength
;
475 cbDataW
= (USHORT
)cbData
;
478 if (rc
== ERROR_SUCCESS
)
479 rc
= RegSetValueExW(hKey
, lpValueNameW
, 0, dwType
, lpDataW
, cbDataW
);
482 if (lpData
!= lpDataW
)
483 free((PVOID
)lpDataW
);
490 IN LPCWSTR lpValueName
,
491 IN PULONG lpReserved
,
494 OUT PSIZE_T lpcbData
)
496 //ParentKey = HKEY_TO_MEMKEY(RootKey);
497 PCM_KEY_VALUE ValueCell
;
498 HCELL_INDEX ValueCellOffset
;
501 rc
= RegpOpenOrCreateValue(
507 if (rc
!= ERROR_SUCCESS
)
510 DPRINT1("RegQueryValueExW(%S) not implemented\n", lpValueName
);
511 /* ValueCell and ValueCellOffset are valid */
513 return ERROR_UNSUCCESSFUL
;
519 IN LPCSTR lpValueName
,
520 IN PULONG lpReserved
,
523 OUT PSIZE_T lpcbData
)
525 LPWSTR lpValueNameW
= NULL
;
530 lpValueNameW
= MultiByteToWideChar(lpValueName
);
532 return ERROR_OUTOFMEMORY
;
535 rc
= RegQueryValueExW(hKey
, lpValueNameW
, lpReserved
, lpType
, lpData
, lpcbData
);
538 return ERROR_UNSUCCESSFUL
;
544 IN LPCWSTR lpValueName OPTIONAL
)
546 DPRINT1("RegDeleteValueW() unimplemented\n");
547 return ERROR_UNSUCCESSFUL
;
553 IN LPCSTR lpValueName OPTIONAL
)
560 lpValueNameW
= MultiByteToWideChar(lpValueName
);
562 return ERROR_OUTOFMEMORY
;
563 rc
= RegDeleteValueW(hKey
, lpValueNameW
);
567 rc
= RegDeleteValueW(hKey
, NULL
);
574 IN PCMHIVE HiveToConnect
,
581 Status
= CmiInitializeTempHive(HiveToConnect
);
582 if (!NT_SUCCESS(Status
))
584 DPRINT1("CmiInitializeTempHive() failed with status 0x%08x\n", Status
);
593 if (rc
!= ERROR_SUCCESS
)
596 NewKey
->RegistryHive
= HiveToConnect
;
597 NewKey
->KeyCellOffset
= HiveToConnect
->Hive
.BaseBlock
->RootCell
;
598 NewKey
->KeyCell
= (PCM_KEY_NODE
)HvGetCell (&HiveToConnect
->Hive
, NewKey
->KeyCellOffset
);
602 LIST_ENTRY CmiHiveListHead
;
605 RegInitializeRegistry(VOID
)
607 UNICODE_STRING RootKeyName
= RTL_CONSTANT_STRING(L
"\\");
611 InitializeListHead(&CmiHiveListHead
);
613 Status
= CmiInitializeTempHive(&RootHive
);
614 if (!NT_SUCCESS(Status
))
616 DPRINT1("CmiInitializeTempHive() failed with status 0x%08x\n", Status
);
620 RootKey
= CreateInMemoryStructure(
622 RootHive
.Hive
.BaseBlock
->RootCell
,
625 /* Create DEFAULT key */
629 L
"Registry\\User\\.DEFAULT");
635 L
"Registry\\Machine\\SAM");
637 /* Create SECURITY key */
641 L
"Registry\\Machine\\SECURITY");
643 /* Create SOFTWARE key */
647 L
"Registry\\Machine\\SOFTWARE");
649 /* Create SYSTEM key */
653 L
"Registry\\Machine\\SYSTEM");
655 /* Create 'ControlSet001' key */
658 L
"Registry\\Machine\\SYSTEM\\ControlSet001",