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
);
254 return ERROR_SUCCESS
;
262 PWSTR lpSubKeyW
= NULL
;
265 if (lpSubKey
!= NULL
&& strchr(lpSubKey
, '\\') != NULL
)
266 return ERROR_INVALID_PARAMETER
;
270 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
272 return ERROR_OUTOFMEMORY
;
275 rc
= RegDeleteKeyW(hKey
, lpSubKeyW
);
289 return RegpOpenOrCreateKey(hKey
, lpSubKey
, FALSE
, phkResult
);
301 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
303 return ERROR_OUTOFMEMORY
;
305 rc
= RegOpenKeyW(hKey
, lpSubKeyW
, phkResult
);
311 RegpOpenOrCreateValue(
313 IN LPCWSTR ValueName
,
314 IN BOOL AllowCreation
,
315 OUT PCM_KEY_VALUE
*ValueCell
,
316 OUT PHCELL_INDEX ValueCellOffset
)
319 UNICODE_STRING ValueString
;
322 ParentKey
= HKEY_TO_MEMKEY(hKey
);
323 RtlInitUnicodeString(&ValueString
, ValueName
);
325 Status
= CmiScanForValueKey(
326 ParentKey
->RegistryHive
,
331 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
333 Status
= CmiAddValueKey(
334 ParentKey
->RegistryHive
,
336 ParentKey
->KeyCellOffset
,
341 if (!NT_SUCCESS(Status
))
342 return ERROR_UNSUCCESSFUL
;
343 return ERROR_SUCCESS
;
349 IN LPCWSTR lpValueName OPTIONAL
,
352 IN
const UCHAR
* lpData
,
357 PCM_KEY_VALUE ValueCell
;
358 HCELL_INDEX ValueCellOffset
;
363 if (dwType
== REG_LINK
)
365 /* Special handling of registry links */
366 if (cbData
!= sizeof(PVOID
))
367 return STATUS_INVALID_PARAMETER
;
368 phKey
= (PHKEY
)lpData
;
369 Key
= HKEY_TO_MEMKEY(hKey
);
370 DestKey
= HKEY_TO_MEMKEY(*phKey
);
372 /* Create the link in memory */
373 Key
->DataType
= REG_LINK
;
374 Key
->LinkedKey
= DestKey
;
376 /* Create the link in registry hive (if applicable) */
377 if (Key
->RegistryHive
!= DestKey
->RegistryHive
)
378 return STATUS_SUCCESS
;
379 DPRINT1("Save link to registry\n");
380 return STATUS_NOT_IMPLEMENTED
;
383 if ((cbData
& REG_DATA_SIZE_MASK
) != cbData
)
384 return STATUS_UNSUCCESSFUL
;
386 Key
= HKEY_TO_MEMKEY(hKey
);
388 Status
= RegpOpenOrCreateValue(hKey
, lpValueName
, TRUE
, &ValueCell
, &ValueCellOffset
);
389 if (!NT_SUCCESS(Status
))
390 return ERROR_UNSUCCESSFUL
;
392 /* Get size of the allocated cellule (if any) */
393 if (!(ValueCell
->DataLength
& REG_DATA_IN_OFFSET
) &&
394 (ValueCell
->DataLength
& REG_DATA_SIZE_MASK
) != 0)
396 DataCell
= HvGetCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
398 return ERROR_UNSUCCESSFUL
;
399 DataCellSize
= -HvGetCellSize(&Key
->RegistryHive
->Hive
, DataCell
);
407 if (cbData
<= sizeof(HCELL_INDEX
))
409 /* If data size <= sizeof(HCELL_INDEX) then store data in the data offset */
410 DPRINT("ValueCell->DataLength %u\n", ValueCell
->DataLength
);
412 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
414 RtlCopyMemory(&ValueCell
->Data
, lpData
, cbData
);
415 ValueCell
->DataLength
= (ULONG
)(cbData
| REG_DATA_IN_OFFSET
);
416 ValueCell
->Type
= dwType
;
417 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
, FALSE
);
421 if (cbData
> (SIZE_T
)DataCellSize
)
423 /* New data size is larger than the current, destroy current
424 * data block and allocate a new one. */
425 HCELL_INDEX NewOffset
;
427 DPRINT("ValueCell->DataLength %u\n", ValueCell
->DataLength
);
429 NewOffset
= HvAllocateCell(&Key
->RegistryHive
->Hive
, cbData
, Stable
, HCELL_NIL
);
430 if (NewOffset
== HCELL_NIL
)
432 DPRINT("HvAllocateCell() failed with status 0x%08x\n", Status
);
433 return ERROR_UNSUCCESSFUL
;
437 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
439 ValueCell
->Data
= NewOffset
;
440 DataCell
= (PVOID
)HvGetCell(&Key
->RegistryHive
->Hive
, NewOffset
);
443 /* Copy new contents to cellule */
444 RtlCopyMemory(DataCell
, lpData
, cbData
);
445 ValueCell
->DataLength
= (ULONG
)(cbData
& REG_DATA_SIZE_MASK
);
446 ValueCell
->Type
= dwType
;
447 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCell
->Data
, FALSE
);
448 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
, FALSE
);
451 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, Key
->KeyCellOffset
, FALSE
);
453 DPRINT("Return status 0x%08x\n", Status
);
460 IN LPCSTR lpValueName OPTIONAL
,
463 IN
const UCHAR
* lpData
,
466 LPWSTR lpValueNameW
= NULL
;
467 const UCHAR
* lpDataW
;
469 LONG rc
= ERROR_SUCCESS
;
471 DPRINT("RegSetValueA(%s)\n", lpValueName
);
474 lpValueNameW
= MultiByteToWideChar(lpValueName
);
476 return ERROR_OUTOFMEMORY
;
479 if ((dwType
== REG_SZ
|| dwType
== REG_EXPAND_SZ
|| dwType
== REG_MULTI_SZ
)
482 ANSI_STRING AnsiString
;
485 if (lpData
[cbData
- 1] != '\0')
487 RtlInitAnsiString(&AnsiString
, NULL
);
488 AnsiString
.Buffer
= (PSTR
)lpData
;
489 AnsiString
.Length
= (USHORT
)cbData
- 1;
490 AnsiString
.MaximumLength
= (USHORT
)cbData
;
491 RtlAnsiStringToUnicodeString (&Data
, &AnsiString
, TRUE
);
492 lpDataW
= (const UCHAR
*)Data
.Buffer
;
493 cbDataW
= Data
.MaximumLength
;
498 cbDataW
= (USHORT
)cbData
;
501 if (rc
== ERROR_SUCCESS
)
502 rc
= RegSetValueExW(hKey
, lpValueNameW
, 0, dwType
, lpDataW
, cbDataW
);
505 if (lpData
!= lpDataW
)
506 free((PVOID
)lpDataW
);
513 IN LPCWSTR lpValueName
,
514 IN PULONG lpReserved
,
517 OUT PSIZE_T lpcbData
)
519 //ParentKey = HKEY_TO_MEMKEY(RootKey);
520 PCM_KEY_VALUE ValueCell
;
521 HCELL_INDEX ValueCellOffset
;
524 rc
= RegpOpenOrCreateValue(
530 if (rc
!= ERROR_SUCCESS
)
533 DPRINT1("RegQueryValueExW(%S) not implemented\n", lpValueName
);
534 /* ValueCell and ValueCellOffset are valid */
536 return ERROR_UNSUCCESSFUL
;
542 IN LPCSTR lpValueName
,
543 IN PULONG lpReserved
,
546 OUT PSIZE_T lpcbData
)
548 LPWSTR lpValueNameW
= NULL
;
553 lpValueNameW
= MultiByteToWideChar(lpValueName
);
555 return ERROR_OUTOFMEMORY
;
558 rc
= RegQueryValueExW(hKey
, lpValueNameW
, lpReserved
, lpType
, lpData
, lpcbData
);
561 return ERROR_UNSUCCESSFUL
;
567 IN LPCWSTR lpValueName OPTIONAL
)
569 DPRINT1("RegDeleteValueW() unimplemented\n");
570 return ERROR_UNSUCCESSFUL
;
576 IN LPCSTR lpValueName OPTIONAL
)
583 lpValueNameW
= MultiByteToWideChar(lpValueName
);
585 return ERROR_OUTOFMEMORY
;
586 rc
= RegDeleteValueW(hKey
, lpValueNameW
);
590 rc
= RegDeleteValueW(hKey
, NULL
);
597 IN PCMHIVE HiveToConnect
,
604 Status
= CmiInitializeTempHive(HiveToConnect
);
605 if (!NT_SUCCESS(Status
))
607 DPRINT1("CmiInitializeTempHive() failed with status 0x%08x\n", Status
);
616 if (rc
!= ERROR_SUCCESS
)
619 NewKey
->RegistryHive
= HiveToConnect
;
620 NewKey
->KeyCellOffset
= HiveToConnect
->Hive
.BaseBlock
->RootCell
;
621 NewKey
->KeyCell
= (PCM_KEY_NODE
)HvGetCell (&HiveToConnect
->Hive
, NewKey
->KeyCellOffset
);
625 LIST_ENTRY CmiHiveListHead
;
628 RegInitializeRegistry(VOID
)
630 UNICODE_STRING RootKeyName
= RTL_CONSTANT_STRING(L
"\\");
634 InitializeListHead(&CmiHiveListHead
);
636 Status
= CmiInitializeTempHive(&RootHive
);
637 if (!NT_SUCCESS(Status
))
639 DPRINT1("CmiInitializeTempHive() failed with status 0x%08x\n", Status
);
643 RootKey
= CreateInMemoryStructure(
645 RootHive
.Hive
.BaseBlock
->RootCell
,
648 /* Create DEFAULT key */
652 L
"Registry\\User\\.DEFAULT");
658 L
"Registry\\Machine\\SAM");
660 /* Create SECURITY key */
664 L
"Registry\\Machine\\SECURITY");
666 /* Create SOFTWARE key */
670 L
"Registry\\Machine\\SOFTWARE");
672 /* Create SYSTEM key */
676 L
"Registry\\Machine\\SYSTEM");
678 /* Create 'ControlSet001' key */
681 L
"Registry\\Machine\\SYSTEM\\ControlSet001",