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
);
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 RegDeleteKeyW(IN HKEY hKey
,
252 if (lpSubKey
!= NULL
&& wcschr(lpSubKey
, L
'\\') != NULL
)
253 return(ERROR_INVALID_PARAMETER
);
255 DPRINT1("RegDeleteKeyW: FIXME!\n");
257 return(ERROR_SUCCESS
);
261 RegDeleteKeyA(IN HKEY hKey
,
267 if (lpSubKey
!= NULL
&& strchr(lpSubKey
, '\\') != NULL
)
268 return(ERROR_INVALID_PARAMETER
);
270 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
272 return ERROR_OUTOFMEMORY
;
274 rc
= RegDeleteKeyW(hKey
, lpSubKeyW
);
285 return RegpOpenOrCreateKey(hKey
, lpSubKey
, FALSE
, phkResult
);
297 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
299 return ERROR_OUTOFMEMORY
;
301 rc
= RegOpenKeyW(hKey
, lpSubKeyW
, phkResult
);
307 RegpOpenOrCreateValue(
309 IN LPCWSTR ValueName
,
310 IN BOOL AllowCreation
,
311 OUT PCM_KEY_VALUE
*ValueCell
,
312 OUT PHCELL_INDEX ValueCellOffset
)
315 UNICODE_STRING ValueString
;
318 ParentKey
= HKEY_TO_MEMKEY(hKey
);
319 RtlInitUnicodeString(&ValueString
, ValueName
);
321 Status
= CmiScanForValueKey(
322 ParentKey
->RegistryHive
,
327 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
329 Status
= CmiAddValueKey(
330 ParentKey
->RegistryHive
,
332 ParentKey
->KeyCellOffset
,
337 if (!NT_SUCCESS(Status
))
338 return ERROR_UNSUCCESSFUL
;
339 return ERROR_SUCCESS
;
345 IN LPCWSTR lpValueName OPTIONAL
,
348 IN
const UCHAR
* lpData
,
353 PCM_KEY_VALUE ValueCell
;
354 HCELL_INDEX ValueCellOffset
;
359 if (dwType
== REG_LINK
)
361 /* Special handling of registry links */
362 if (cbData
!= sizeof(PVOID
))
363 return STATUS_INVALID_PARAMETER
;
364 phKey
= (PHKEY
)lpData
;
365 Key
= HKEY_TO_MEMKEY(hKey
);
366 DestKey
= HKEY_TO_MEMKEY(*phKey
);
368 /* Create the link in memory */
369 Key
->DataType
= REG_LINK
;
370 Key
->LinkedKey
= DestKey
;
372 /* Create the link in registry hive (if applicable) */
373 if (Key
->RegistryHive
!= DestKey
->RegistryHive
)
374 return STATUS_SUCCESS
;
375 DPRINT1("Save link to registry\n");
376 return STATUS_NOT_IMPLEMENTED
;
379 if ((cbData
& REG_DATA_SIZE_MASK
) != cbData
)
380 return STATUS_UNSUCCESSFUL
;
382 Key
= HKEY_TO_MEMKEY(hKey
);
384 Status
= RegpOpenOrCreateValue(hKey
, lpValueName
, TRUE
, &ValueCell
, &ValueCellOffset
);
385 if (!NT_SUCCESS(Status
))
386 return ERROR_UNSUCCESSFUL
;
388 /* Get size of the allocated cellule (if any) */
389 if (!(ValueCell
->DataLength
& REG_DATA_IN_OFFSET
) &&
390 (ValueCell
->DataLength
& REG_DATA_SIZE_MASK
) != 0)
392 DataCell
= HvGetCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
394 return ERROR_UNSUCCESSFUL
;
395 DataCellSize
= -HvGetCellSize(&Key
->RegistryHive
->Hive
, DataCell
);
403 if (cbData
<= sizeof(HCELL_INDEX
))
405 /* If data size <= sizeof(HCELL_INDEX) then store data in the data offset */
406 DPRINT("ValueCell->DataLength %u\n", ValueCell
->DataLength
);
408 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
410 RtlCopyMemory(&ValueCell
->Data
, lpData
, cbData
);
411 ValueCell
->DataLength
= (ULONG
)(cbData
| REG_DATA_IN_OFFSET
);
412 ValueCell
->Type
= dwType
;
413 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
, FALSE
);
417 if (cbData
> (SIZE_T
)DataCellSize
)
419 /* New data size is larger than the current, destroy current
420 * data block and allocate a new one. */
421 HCELL_INDEX NewOffset
;
423 DPRINT("ValueCell->DataLength %u\n", ValueCell
->DataLength
);
425 NewOffset
= HvAllocateCell(&Key
->RegistryHive
->Hive
, cbData
, Stable
, HCELL_NIL
);
426 if (NewOffset
== HCELL_NIL
)
428 DPRINT("HvAllocateCell() failed with status 0x%08x\n", Status
);
429 return ERROR_UNSUCCESSFUL
;
433 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
435 ValueCell
->Data
= NewOffset
;
436 DataCell
= (PVOID
)HvGetCell(&Key
->RegistryHive
->Hive
, NewOffset
);
439 /* Copy new contents to cellule */
440 RtlCopyMemory(DataCell
, lpData
, cbData
);
441 ValueCell
->DataLength
= (ULONG
)(cbData
& REG_DATA_SIZE_MASK
);
442 ValueCell
->Type
= dwType
;
443 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCell
->Data
, FALSE
);
444 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
, FALSE
);
447 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, Key
->KeyCellOffset
, FALSE
);
449 DPRINT("Return status 0x%08x\n", Status
);
456 IN LPCSTR lpValueName OPTIONAL
,
459 IN
const UCHAR
* lpData
,
462 LPWSTR lpValueNameW
= NULL
;
463 const UCHAR
* lpDataW
;
465 LONG rc
= ERROR_SUCCESS
;
467 DPRINT("RegSetValueA(%s)\n", lpValueName
);
470 lpValueNameW
= MultiByteToWideChar(lpValueName
);
472 return ERROR_OUTOFMEMORY
;
475 if ((dwType
== REG_SZ
|| dwType
== REG_EXPAND_SZ
|| dwType
== REG_MULTI_SZ
)
478 ANSI_STRING AnsiString
;
481 if (lpData
[cbData
- 1] != '\0')
483 RtlInitAnsiString(&AnsiString
, NULL
);
484 AnsiString
.Buffer
= (PSTR
)lpData
;
485 AnsiString
.Length
= (USHORT
)cbData
- 1;
486 AnsiString
.MaximumLength
= (USHORT
)cbData
;
487 RtlAnsiStringToUnicodeString (&Data
, &AnsiString
, TRUE
);
488 lpDataW
= (const UCHAR
*)Data
.Buffer
;
489 cbDataW
= Data
.MaximumLength
;
494 cbDataW
= (USHORT
)cbData
;
497 if (rc
== ERROR_SUCCESS
)
498 rc
= RegSetValueExW(hKey
, lpValueNameW
, 0, dwType
, lpDataW
, cbDataW
);
501 if (lpData
!= lpDataW
)
502 free((PVOID
)lpDataW
);
509 IN LPCWSTR lpValueName
,
510 IN PULONG lpReserved
,
513 OUT PSIZE_T lpcbData
)
515 //ParentKey = HKEY_TO_MEMKEY(RootKey);
516 PCM_KEY_VALUE ValueCell
;
517 HCELL_INDEX ValueCellOffset
;
520 rc
= RegpOpenOrCreateValue(
526 if (rc
!= ERROR_SUCCESS
)
529 DPRINT1("RegQueryValueExW(%S) not implemented\n", lpValueName
);
530 /* ValueCell and ValueCellOffset are valid */
532 return ERROR_UNSUCCESSFUL
;
538 IN LPCSTR lpValueName
,
539 IN PULONG lpReserved
,
542 OUT PSIZE_T lpcbData
)
544 LPWSTR lpValueNameW
= NULL
;
549 lpValueNameW
= MultiByteToWideChar(lpValueName
);
551 return ERROR_OUTOFMEMORY
;
554 rc
= RegQueryValueExW(hKey
, lpValueNameW
, lpReserved
, lpType
, lpData
, lpcbData
);
557 return ERROR_UNSUCCESSFUL
;
563 IN LPCWSTR lpValueName OPTIONAL
)
565 DPRINT1("RegDeleteValueW() unimplemented\n");
566 return ERROR_UNSUCCESSFUL
;
572 IN LPCSTR lpValueName OPTIONAL
)
579 lpValueNameW
= MultiByteToWideChar(lpValueName
);
581 return ERROR_OUTOFMEMORY
;
582 rc
= RegDeleteValueW(hKey
, lpValueNameW
);
586 rc
= RegDeleteValueW(hKey
, NULL
);
593 IN PCMHIVE HiveToConnect
,
600 Status
= CmiInitializeTempHive(HiveToConnect
);
601 if (!NT_SUCCESS(Status
))
603 DPRINT1("CmiInitializeTempHive() failed with status 0x%08x\n", Status
);
612 if (rc
!= ERROR_SUCCESS
)
615 NewKey
->RegistryHive
= HiveToConnect
;
616 NewKey
->KeyCellOffset
= HiveToConnect
->Hive
.BaseBlock
->RootCell
;
617 NewKey
->KeyCell
= (PCM_KEY_NODE
)HvGetCell (&HiveToConnect
->Hive
, NewKey
->KeyCellOffset
);
621 LIST_ENTRY CmiHiveListHead
;
624 RegInitializeRegistry(VOID
)
626 UNICODE_STRING RootKeyName
= RTL_CONSTANT_STRING(L
"\\");
630 InitializeListHead(&CmiHiveListHead
);
632 Status
= CmiInitializeTempHive(&RootHive
);
633 if (!NT_SUCCESS(Status
))
635 DPRINT1("CmiInitializeTempHive() failed with status 0x%08x\n", Status
);
639 RootKey
= CreateInMemoryStructure(
641 RootHive
.Hive
.BaseBlock
->RootCell
,
644 /* Create DEFAULT key */
648 L
"Registry\\User\\.DEFAULT");
654 L
"Registry\\Machine\\SAM");
656 /* Create SECURITY key */
660 L
"Registry\\Machine\\SECURITY");
662 /* Create SOFTWARE key */
666 L
"Registry\\Machine\\SOFTWARE");
668 /* Create SYSTEM key */
672 L
"Registry\\Machine\\SYSTEM");
674 /* Create 'ControlSet001' key */
677 L
"Registry\\Machine\\SYSTEM\\ControlSet001",