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
->LinkedKey
= NULL
;
99 IN BOOL AllowCreation
,
104 UNICODE_STRING KeyString
;
109 PCM_KEY_NODE SubKeyCell
;
110 HCELL_INDEX BlockOffset
;
112 DPRINT("RegpCreateOpenKey('%S')\n", KeyName
);
114 if (*KeyName
== L
'\\')
119 else if (hParentKey
== NULL
)
125 ParentKey
= HKEY_TO_MEMKEY(RootKey
);
128 LocalKeyName
= (PWSTR
)KeyName
;
131 End
= (PWSTR
)strchrW(LocalKeyName
, '\\');
134 KeyString
.Buffer
= LocalKeyName
;
135 KeyString
.Length
= KeyString
.MaximumLength
=
136 (USHORT
)((ULONG_PTR
)End
- (ULONG_PTR
)LocalKeyName
);
139 RtlInitUnicodeString(&KeyString
, LocalKeyName
);
141 /* Redirect from 'CurrentControlSet' to 'ControlSet001' */
142 if (!strncmpW(LocalKeyName
, L
"CurrentControlSet", 17) &&
143 ParentKey
->NameSize
== 12 &&
144 !memcmp(ParentKey
->Name
, L
"SYSTEM", 12))
145 RtlInitUnicodeString(&KeyString
, L
"ControlSet001");
147 /* Check subkey in memory structure */
148 Ptr
= ParentKey
->SubKeyList
.Flink
;
149 while (Ptr
!= &ParentKey
->SubKeyList
)
151 CurrentKey
= CONTAINING_RECORD(Ptr
, KEY
, KeyList
);
152 if (CurrentKey
->NameSize
== KeyString
.Length
153 && memcmp(CurrentKey
->Name
, KeyString
.Buffer
, KeyString
.Length
) == 0)
161 Status
= CmiScanForSubKey(
162 ParentKey
->RegistryHive
,
165 OBJ_CASE_INSENSITIVE
,
168 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
170 Status
= CmiAddSubKey(
171 ParentKey
->RegistryHive
,
173 ParentKey
->KeyCellOffset
,
179 if (!NT_SUCCESS(Status
))
180 return ERROR_UNSUCCESSFUL
;
182 /* Now, SubKeyCell/BlockOffset are valid */
183 CurrentKey
= CreateInMemoryStructure(
184 ParentKey
->RegistryHive
,
188 return ERROR_OUTOFMEMORY
;
190 /* Add CurrentKey in ParentKey */
191 InsertTailList(&ParentKey
->SubKeyList
, &CurrentKey
->KeyList
);
192 ParentKey
->SubKeyCount
++;
195 ParentKey
= CurrentKey
;
197 LocalKeyName
= End
+ 1;
202 *Key
= MEMKEY_TO_HKEY(ParentKey
);
204 return ERROR_SUCCESS
;
213 return RegpOpenOrCreateKey(hKey
, lpSubKey
, TRUE
, phkResult
);
218 IN PCSTR MultiByteString
)
221 UNICODE_STRING Destination
;
224 RtlInitAnsiString(&Source
, MultiByteString
);
225 Status
= RtlAnsiStringToUnicodeString(&Destination
, &Source
, TRUE
);
226 if (!NT_SUCCESS(Status
))
228 return Destination
.Buffer
;
240 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
242 return ERROR_OUTOFMEMORY
;
244 rc
= RegCreateKeyW(hKey
, lpSubKeyW
, phkResult
);
254 DPRINT1("FIXME: implement RegDeleteKeyW!\n");
255 return ERROR_SUCCESS
;
263 PWSTR lpSubKeyW
= NULL
;
266 if (lpSubKey
!= NULL
&& strchr(lpSubKey
, '\\') != NULL
)
267 return ERROR_INVALID_PARAMETER
;
271 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
273 return ERROR_OUTOFMEMORY
;
276 rc
= RegDeleteKeyW(hKey
, lpSubKeyW
);
290 return RegpOpenOrCreateKey(hKey
, lpSubKey
, FALSE
, phkResult
);
302 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
304 return ERROR_OUTOFMEMORY
;
306 rc
= RegOpenKeyW(hKey
, lpSubKeyW
, phkResult
);
312 RegpOpenOrCreateValue(
314 IN LPCWSTR ValueName
,
315 IN BOOL AllowCreation
,
316 OUT PCM_KEY_VALUE
*ValueCell
,
317 OUT PHCELL_INDEX ValueCellOffset
)
320 UNICODE_STRING ValueString
;
323 ParentKey
= HKEY_TO_MEMKEY(hKey
);
324 RtlInitUnicodeString(&ValueString
, ValueName
);
326 Status
= CmiScanForValueKey(
327 ParentKey
->RegistryHive
,
332 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
334 Status
= CmiAddValueKey(
335 ParentKey
->RegistryHive
,
337 ParentKey
->KeyCellOffset
,
342 if (!NT_SUCCESS(Status
))
343 return ERROR_UNSUCCESSFUL
;
344 return ERROR_SUCCESS
;
350 IN LPCWSTR lpValueName OPTIONAL
,
353 IN
const UCHAR
* lpData
,
358 PCM_KEY_VALUE ValueCell
;
359 HCELL_INDEX ValueCellOffset
;
364 if (dwType
== REG_LINK
)
366 /* Special handling of registry links */
367 if (cbData
!= sizeof(PVOID
))
368 return STATUS_INVALID_PARAMETER
;
369 phKey
= (PHKEY
)lpData
;
370 Key
= HKEY_TO_MEMKEY(hKey
);
371 DestKey
= HKEY_TO_MEMKEY(*phKey
);
373 /* Create the link in memory */
374 Key
->DataType
= REG_LINK
;
375 Key
->LinkedKey
= DestKey
;
377 /* Create the link in registry hive (if applicable) */
378 if (Key
->RegistryHive
!= DestKey
->RegistryHive
)
379 return STATUS_SUCCESS
;
380 DPRINT1("Save link to registry\n");
381 return STATUS_NOT_IMPLEMENTED
;
384 if ((cbData
& REG_DATA_SIZE_MASK
) != cbData
)
385 return STATUS_UNSUCCESSFUL
;
387 Key
= HKEY_TO_MEMKEY(hKey
);
389 Status
= RegpOpenOrCreateValue(hKey
, lpValueName
, TRUE
, &ValueCell
, &ValueCellOffset
);
390 if (!NT_SUCCESS(Status
))
391 return ERROR_UNSUCCESSFUL
;
393 /* Get size of the allocated cellule (if any) */
394 if (!(ValueCell
->DataLength
& REG_DATA_IN_OFFSET
) &&
395 (ValueCell
->DataLength
& REG_DATA_SIZE_MASK
) != 0)
397 DataCell
= HvGetCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
399 return ERROR_UNSUCCESSFUL
;
400 DataCellSize
= -HvGetCellSize(&Key
->RegistryHive
->Hive
, DataCell
);
408 if (cbData
<= sizeof(HCELL_INDEX
))
410 /* If data size <= sizeof(HCELL_INDEX) then store data in the data offset */
411 DPRINT("ValueCell->DataLength %u\n", ValueCell
->DataLength
);
413 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
415 RtlCopyMemory(&ValueCell
->Data
, lpData
, cbData
);
416 ValueCell
->DataLength
= (ULONG
)(cbData
| REG_DATA_IN_OFFSET
);
417 ValueCell
->Type
= dwType
;
418 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
, FALSE
);
422 if (cbData
> (SIZE_T
)DataCellSize
)
424 /* New data size is larger than the current, destroy current
425 * data block and allocate a new one. */
426 HCELL_INDEX NewOffset
;
428 DPRINT("ValueCell->DataLength %u\n", ValueCell
->DataLength
);
430 NewOffset
= HvAllocateCell(&Key
->RegistryHive
->Hive
, cbData
, Stable
, HCELL_NIL
);
431 if (NewOffset
== HCELL_NIL
)
433 DPRINT("HvAllocateCell() failed with status 0x%08x\n", Status
);
434 return ERROR_UNSUCCESSFUL
;
438 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->Data
);
440 ValueCell
->Data
= NewOffset
;
441 DataCell
= (PVOID
)HvGetCell(&Key
->RegistryHive
->Hive
, NewOffset
);
444 /* Copy new contents to cellule */
445 RtlCopyMemory(DataCell
, lpData
, cbData
);
446 ValueCell
->DataLength
= (ULONG
)(cbData
& REG_DATA_SIZE_MASK
);
447 ValueCell
->Type
= dwType
;
448 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCell
->Data
, FALSE
);
449 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
, FALSE
);
452 if (cbData
> Key
->KeyCell
->MaxValueDataLen
)
453 Key
->KeyCell
->MaxValueDataLen
= cbData
;
455 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, Key
->KeyCellOffset
, FALSE
);
457 DPRINT("Return status 0x%08x\n", Status
);
464 IN LPCSTR lpValueName OPTIONAL
,
467 IN
const UCHAR
* lpData
,
470 LPWSTR lpValueNameW
= NULL
;
471 const UCHAR
* lpDataW
;
473 LONG rc
= ERROR_SUCCESS
;
475 DPRINT("RegSetValueA(%s)\n", lpValueName
);
478 lpValueNameW
= MultiByteToWideChar(lpValueName
);
480 return ERROR_OUTOFMEMORY
;
483 if ((dwType
== REG_SZ
|| dwType
== REG_EXPAND_SZ
|| dwType
== REG_MULTI_SZ
)
486 ANSI_STRING AnsiString
;
489 if (lpData
[cbData
- 1] != '\0')
491 RtlInitAnsiString(&AnsiString
, NULL
);
492 AnsiString
.Buffer
= (PSTR
)lpData
;
493 AnsiString
.Length
= (USHORT
)cbData
- 1;
494 AnsiString
.MaximumLength
= (USHORT
)cbData
;
495 RtlAnsiStringToUnicodeString (&Data
, &AnsiString
, TRUE
);
496 lpDataW
= (const UCHAR
*)Data
.Buffer
;
497 cbDataW
= Data
.MaximumLength
;
502 cbDataW
= (USHORT
)cbData
;
505 if (rc
== ERROR_SUCCESS
)
506 rc
= RegSetValueExW(hKey
, lpValueNameW
, 0, dwType
, lpDataW
, cbDataW
);
509 if (lpData
!= lpDataW
)
510 free((PVOID
)lpDataW
);
517 IN LPCWSTR lpValueName
,
518 IN PULONG lpReserved
,
521 OUT PSIZE_T lpcbData
)
523 //ParentKey = HKEY_TO_MEMKEY(RootKey);
524 PCM_KEY_VALUE ValueCell
;
525 HCELL_INDEX ValueCellOffset
;
528 rc
= RegpOpenOrCreateValue(
534 if (rc
!= ERROR_SUCCESS
)
537 DPRINT1("RegQueryValueExW(%S) not implemented\n", lpValueName
);
538 /* ValueCell and ValueCellOffset are valid */
540 return ERROR_UNSUCCESSFUL
;
546 IN LPCSTR lpValueName
,
547 IN PULONG lpReserved
,
550 OUT PSIZE_T lpcbData
)
552 LPWSTR lpValueNameW
= NULL
;
557 lpValueNameW
= MultiByteToWideChar(lpValueName
);
559 return ERROR_OUTOFMEMORY
;
562 rc
= RegQueryValueExW(hKey
, lpValueNameW
, lpReserved
, lpType
, lpData
, lpcbData
);
571 IN LPCWSTR lpValueName OPTIONAL
)
573 DPRINT1("RegDeleteValueW() unimplemented\n");
574 return ERROR_UNSUCCESSFUL
;
580 IN LPCSTR lpValueName OPTIONAL
)
587 lpValueNameW
= MultiByteToWideChar(lpValueName
);
589 return ERROR_OUTOFMEMORY
;
590 rc
= RegDeleteValueW(hKey
, lpValueNameW
);
594 rc
= RegDeleteValueW(hKey
, NULL
);
601 IN PCMHIVE HiveToConnect
,
608 Status
= CmiInitializeTempHive(HiveToConnect
);
609 if (!NT_SUCCESS(Status
))
611 DPRINT1("CmiInitializeTempHive() failed with status 0x%08x\n", Status
);
620 if (rc
!= ERROR_SUCCESS
)
623 NewKey
->RegistryHive
= HiveToConnect
;
624 NewKey
->KeyCellOffset
= HiveToConnect
->Hive
.BaseBlock
->RootCell
;
625 NewKey
->KeyCell
= (PCM_KEY_NODE
)HvGetCell (&HiveToConnect
->Hive
, NewKey
->KeyCellOffset
);
629 LIST_ENTRY CmiHiveListHead
;
632 RegInitializeRegistry(VOID
)
634 UNICODE_STRING RootKeyName
= RTL_CONSTANT_STRING(L
"\\");
638 InitializeListHead(&CmiHiveListHead
);
640 Status
= CmiInitializeTempHive(&RootHive
);
641 if (!NT_SUCCESS(Status
))
643 DPRINT1("CmiInitializeTempHive() failed with status 0x%08x\n", Status
);
647 RootKey
= CreateInMemoryStructure(
649 RootHive
.Hive
.BaseBlock
->RootCell
,
652 /* Create DEFAULT key */
656 L
"Registry\\User\\.DEFAULT");
662 L
"Registry\\Machine\\SAM");
664 /* Create SECURITY key */
668 L
"Registry\\Machine\\SECURITY");
670 /* Create SOFTWARE key */
674 L
"Registry\\Machine\\SOFTWARE");
676 /* Create SYSTEM key */
680 L
"Registry\\Machine\\SYSTEM");
682 /* Create 'ControlSet001' key */
685 L
"Registry\\Machine\\SYSTEM\\ControlSet001",
690 RegShutdownRegistry(VOID
)
692 /* FIXME: clean up the complete hive */