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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 static EREGISTRY_HIVE RootHive
;
41 static MEMKEY RootKey
;
42 EREGISTRY_HIVE DefaultHive
; /* \Registry\User\.DEFAULT */
43 EREGISTRY_HIVE SamHive
; /* \Registry\Machine\SAM */
44 EREGISTRY_HIVE SecurityHive
; /* \Registry\Machine\SECURITY */
45 EREGISTRY_HIVE SoftwareHive
; /* \Registry\Machine\SOFTWARE */
46 EREGISTRY_HIVE SystemHive
; /* \Registry\Machine\SYSTEM */
49 CreateInMemoryStructure(
50 IN PEREGISTRY_HIVE RegistryHive
,
51 IN HCELL_INDEX KeyCellOffset
,
52 IN PCUNICODE_STRING KeyName
)
56 Key
= (MEMKEY
) malloc (sizeof(KEY
));
60 InitializeListHead (&Key
->SubKeyList
);
61 InitializeListHead (&Key
->ValueList
);
62 InitializeListHead (&Key
->KeyList
);
67 Key
->NameSize
= KeyName
->Length
;
68 Key
->Name
= malloc (Key
->NameSize
);
71 memcpy(Key
->Name
, KeyName
->Buffer
, KeyName
->Length
);
77 Key
->RegistryHive
= RegistryHive
;
78 Key
->KeyCellOffset
= KeyCellOffset
;
79 Key
->KeyCell
= HvGetCell (&RegistryHive
->Hive
, Key
->KeyCellOffset
);
85 Key
->LinkedKey
= NULL
;
93 IN BOOL AllowCreation
,
98 UNICODE_STRING KeyString
;
103 PCM_KEY_NODE SubKeyCell
;
104 HCELL_INDEX BlockOffset
;
106 DPRINT("RegpCreateOpenKey('%S')\n", KeyName
);
108 if (*KeyName
== L
'\\')
113 else if (hParentKey
== NULL
)
119 ParentKey
= HKEY_TO_MEMKEY(RootKey
);
122 LocalKeyName
= (PWSTR
)KeyName
;
125 End
= (PWSTR
) xwcschr(LocalKeyName
, '\\');
128 KeyString
.Buffer
= LocalKeyName
;
129 KeyString
.Length
= KeyString
.MaximumLength
=
130 (USHORT
)((ULONG_PTR
)End
- (ULONG_PTR
)LocalKeyName
);
133 RtlInitUnicodeString(&KeyString
, LocalKeyName
);
135 while (ParentKey
->DataType
== REG_LINK
)
136 ParentKey
= ParentKey
->LinkedKey
;
138 /* Check subkey in memory structure */
139 Ptr
= ParentKey
->SubKeyList
.Flink
;
140 while (Ptr
!= &ParentKey
->SubKeyList
)
142 CurrentKey
= CONTAINING_RECORD(Ptr
, KEY
, KeyList
);
143 if (CurrentKey
->NameSize
== KeyString
.Length
144 && memcmp(CurrentKey
->Name
, KeyString
.Buffer
, KeyString
.Length
) == 0)
152 Status
= CmiScanForSubKey(
153 ParentKey
->RegistryHive
,
156 OBJ_CASE_INSENSITIVE
,
159 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
161 Status
= CmiAddSubKey(
162 ParentKey
->RegistryHive
,
164 ParentKey
->KeyCellOffset
,
170 if (!NT_SUCCESS(Status
))
171 return ERROR_UNSUCCESSFUL
;
173 /* Now, SubKeyCell/BlockOffset are valid */
174 CurrentKey
= CreateInMemoryStructure(
175 ParentKey
->RegistryHive
,
179 return ERROR_OUTOFMEMORY
;
181 /* Add CurrentKey in ParentKey */
182 InsertTailList(&ParentKey
->SubKeyList
, &CurrentKey
->KeyList
);
183 ParentKey
->SubKeyCount
++;
186 ParentKey
= CurrentKey
;
188 LocalKeyName
= End
+ 1;
193 *Key
= MEMKEY_TO_HKEY(ParentKey
);
195 return ERROR_SUCCESS
;
204 return RegpOpenOrCreateKey(hKey
, lpSubKey
, TRUE
, phkResult
);
209 IN PCSTR MultiByteString
)
212 UNICODE_STRING Destination
;
215 RtlInitAnsiString(&Source
, MultiByteString
);
216 Status
= RtlAnsiStringToUnicodeString(&Destination
, &Source
, TRUE
);
217 if (!NT_SUCCESS(Status
))
219 return Destination
.Buffer
;
231 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
233 return ERROR_OUTOFMEMORY
;
235 rc
= RegCreateKeyW(hKey
, lpSubKeyW
, phkResult
);
241 RegDeleteKeyA(HKEY Key
,
244 if (Name
!= NULL
&& strchr(Name
, '\\') != NULL
)
245 return(ERROR_INVALID_PARAMETER
);
249 return(ERROR_SUCCESS
);
258 return RegpOpenOrCreateKey(hKey
, lpSubKey
, FALSE
, phkResult
);
270 lpSubKeyW
= MultiByteToWideChar(lpSubKey
);
272 return ERROR_OUTOFMEMORY
;
274 rc
= RegOpenKeyW(hKey
, lpSubKeyW
, phkResult
);
280 RegpOpenOrCreateValue(
282 IN LPCWSTR ValueName
,
283 IN BOOL AllowCreation
,
284 OUT PCM_KEY_VALUE
*ValueCell
,
285 OUT PHCELL_INDEX ValueCellOffset
)
288 UNICODE_STRING ValueString
;
291 ParentKey
= HKEY_TO_MEMKEY(hKey
);
292 RtlInitUnicodeString(&ValueString
, ValueName
);
294 Status
= CmiScanForValueKey(
295 ParentKey
->RegistryHive
,
300 if (AllowCreation
&& Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
302 Status
= CmiAddValueKey(
303 ParentKey
->RegistryHive
,
305 ParentKey
->KeyCellOffset
,
310 if (!NT_SUCCESS(Status
))
311 return ERROR_UNSUCCESSFUL
;
312 return ERROR_SUCCESS
;
318 IN LPCWSTR lpValueName OPTIONAL
,
321 IN
const UCHAR
* lpData
,
326 PCM_KEY_VALUE ValueCell
;
327 HCELL_INDEX ValueCellOffset
;
332 if (dwType
== REG_LINK
)
334 /* Special handling of registry links */
335 if (cbData
!= sizeof(PVOID
))
336 return STATUS_INVALID_PARAMETER
;
337 phKey
= (PHKEY
)lpData
;
338 Key
= HKEY_TO_MEMKEY(hKey
);
339 DestKey
= HKEY_TO_MEMKEY(*phKey
);
341 /* Create the link in memory */
342 Key
->DataType
= REG_LINK
;
343 Key
->LinkedKey
= DestKey
;
345 /* Create the link in registry hive (if applicable) */
346 if (Key
->RegistryHive
!= DestKey
->RegistryHive
)
347 return STATUS_SUCCESS
;
348 DPRINT1("Save link to registry\n");
349 return STATUS_NOT_IMPLEMENTED
;
352 if ((cbData
& REG_DATA_SIZE_MASK
) != cbData
)
353 return STATUS_UNSUCCESSFUL
;
355 Key
= HKEY_TO_MEMKEY(hKey
);
357 Status
= RegpOpenOrCreateValue(hKey
, lpValueName
, TRUE
, &ValueCell
, &ValueCellOffset
);
358 if (!NT_SUCCESS(Status
))
359 return ERROR_UNSUCCESSFUL
;
361 /* Get size of the allocated cellule (if any) */
362 if (!(ValueCell
->DataSize
& REG_DATA_IN_OFFSET
) &&
363 (ValueCell
->DataSize
& REG_DATA_SIZE_MASK
) != 0)
365 DataCell
= HvGetCell(&Key
->RegistryHive
->Hive
, ValueCell
->DataOffset
);
367 return ERROR_UNSUCCESSFUL
;
368 DataCellSize
= -HvGetCellSize(&Key
->RegistryHive
->Hive
, DataCell
);
376 if (cbData
<= sizeof(HCELL_INDEX
))
378 /* If data size <= sizeof(HCELL_INDEX) then store data in the data offset */
379 DPRINT("ValueCell->DataSize %lu\n", ValueCell
->DataSize
);
381 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->DataOffset
);
383 RtlCopyMemory(&ValueCell
->DataOffset
, lpData
, cbData
);
384 ValueCell
->DataSize
= (ULONG
)(cbData
| REG_DATA_IN_OFFSET
);
385 ValueCell
->DataType
= dwType
;
386 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
);
390 if (cbData
> (SIZE_T
)DataCellSize
)
392 /* New data size is larger than the current, destroy current
393 * data block and allocate a new one. */
394 HCELL_INDEX NewOffset
;
396 DPRINT("ValueCell->DataSize %lu\n", ValueCell
->DataSize
);
398 NewOffset
= HvAllocateCell(&Key
->RegistryHive
->Hive
, cbData
, HvStable
);
399 if (NewOffset
== HCELL_NULL
)
401 DPRINT("HvAllocateCell() failed with status 0x%08lx\n", Status
);
402 return ERROR_UNSUCCESSFUL
;
406 HvFreeCell(&Key
->RegistryHive
->Hive
, ValueCell
->DataOffset
);
408 ValueCell
->DataOffset
= NewOffset
;
409 DataCell
= HvGetCell(&Key
->RegistryHive
->Hive
, NewOffset
);
412 /* Copy new contents to cellule */
413 RtlCopyMemory(DataCell
, lpData
, cbData
);
414 ValueCell
->DataSize
= (ULONG
)(cbData
& REG_DATA_SIZE_MASK
);
415 ValueCell
->DataType
= dwType
;
416 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCell
->DataOffset
);
417 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, ValueCellOffset
);
420 HvMarkCellDirty(&Key
->RegistryHive
->Hive
, Key
->KeyCellOffset
);
422 DPRINT("Return status 0x%08lx\n", Status
);
429 IN LPCSTR lpValueName OPTIONAL
,
432 IN
const UCHAR
* lpData
,
435 LPWSTR lpValueNameW
= NULL
;
436 const UCHAR
* lpDataW
;
438 LONG rc
= ERROR_SUCCESS
;
440 DPRINT("RegSetValueA(%s)\n", lpValueName
);
443 lpValueNameW
= MultiByteToWideChar(lpValueName
);
445 return ERROR_OUTOFMEMORY
;
448 if ((dwType
== REG_SZ
|| dwType
== REG_EXPAND_SZ
|| dwType
== REG_MULTI_SZ
)
451 ANSI_STRING AnsiString
;
454 if (lpData
[cbData
- 1] != '\0')
456 RtlInitAnsiString(&AnsiString
, NULL
);
457 AnsiString
.Buffer
= (PSTR
)lpData
;
458 AnsiString
.Length
= (USHORT
)cbData
- 1;
459 AnsiString
.MaximumLength
= (USHORT
)cbData
;
460 RtlAnsiStringToUnicodeString (&Data
, &AnsiString
, TRUE
);
461 lpDataW
= (const UCHAR
*)Data
.Buffer
;
462 cbDataW
= Data
.MaximumLength
;
467 cbDataW
= (USHORT
)cbData
;
470 if (rc
== ERROR_SUCCESS
)
471 rc
= RegSetValueExW(hKey
, lpValueNameW
, 0, dwType
, lpDataW
, cbDataW
);
474 if (lpData
!= lpDataW
)
475 free((PVOID
)lpDataW
);
482 IN LPCWSTR lpValueName
,
483 IN PULONG lpReserved
,
486 OUT PSIZE_T lpcbData
)
488 //ParentKey = HKEY_TO_MEMKEY(RootKey);
489 PCM_KEY_VALUE ValueCell
;
490 HCELL_INDEX ValueCellOffset
;
493 rc
= RegpOpenOrCreateValue(
499 if (rc
!= ERROR_SUCCESS
)
502 DPRINT1("RegQueryValueExW(%S) not implemented\n", lpValueName
);
503 /* ValueCell and ValueCellOffset are valid */
505 return ERROR_UNSUCCESSFUL
;
511 IN LPCSTR lpValueName
,
512 IN PULONG lpReserved
,
515 OUT PSIZE_T lpcbData
)
517 LPWSTR lpValueNameW
= NULL
;
522 lpValueNameW
= MultiByteToWideChar(lpValueName
);
524 return ERROR_OUTOFMEMORY
;
527 rc
= RegQueryValueExW(hKey
, lpValueNameW
, lpReserved
, lpType
, lpData
, lpcbData
);
530 return ERROR_UNSUCCESSFUL
;
536 IN LPCWSTR lpValueName OPTIONAL
)
538 DPRINT1("RegDeleteValueW() unimplemented\n");
539 return ERROR_UNSUCCESSFUL
;
545 IN LPCSTR lpValueName OPTIONAL
)
552 lpValueNameW
= MultiByteToWideChar(lpValueName
);
554 return ERROR_OUTOFMEMORY
;
555 rc
= RegDeleteValueW(hKey
, lpValueNameW
);
559 rc
= RegDeleteValueW(hKey
, NULL
);
566 IN PEREGISTRY_HIVE HiveToConnect
,
573 Status
= CmiInitializeTempHive(HiveToConnect
);
574 if (!NT_SUCCESS(Status
))
576 DPRINT1("CmiInitializeTempHive() failed with status 0x%08lx\n", Status
);
585 if (rc
!= ERROR_SUCCESS
)
588 NewKey
->RegistryHive
= HiveToConnect
;
589 NewKey
->KeyCellOffset
= HiveToConnect
->Hive
.HiveHeader
->RootCell
;
590 NewKey
->KeyCell
= HvGetCell (&HiveToConnect
->Hive
, NewKey
->KeyCellOffset
);
595 MyExportBinaryHive (PCHAR FileName
,
596 PEREGISTRY_HIVE RootHive
)
601 /* Create new hive file */
602 File
= fopen (FileName
, "w+b");
605 printf(" Error creating/opening file\n");
609 fseek (File
, 0, SEEK_SET
);
611 RootHive
->HiveHandle
= (HANDLE
)File
;
612 ret
= HvWriteHive(&RootHive
->Hive
);
617 LIST_ENTRY CmiHiveListHead
;
620 RegInitializeRegistry(VOID
)
622 UNICODE_STRING RootKeyName
= RTL_CONSTANT_STRING(L
"\\");
624 HKEY ControlSetKey
, LinkKey
;
626 InitializeListHead(&CmiHiveListHead
);
628 Status
= CmiInitializeTempHive(&RootHive
);
629 if (!NT_SUCCESS(Status
))
631 DPRINT1("CmiInitializeTempHive() failed with status 0x%08lx\n", Status
);
635 RootKey
= CreateInMemoryStructure(
637 RootHive
.Hive
.HiveHeader
->RootCell
,
640 /* Create DEFAULT key */
644 L
"Registry\\User\\.DEFAULT");
650 L
"Registry\\Machine\\SAM");
652 /* Create SECURITY key */
656 L
"Registry\\Machine\\SECURITY");
658 /* Create SOFTWARE key */
662 L
"Registry\\Machine\\SOFTWARE");
664 /* Create SYSTEM key */
668 L
"Registry\\Machine\\SYSTEM");
670 /* Create link 'CurrentControlSet' --> 'ControlSet001' */
673 L
"Registry\\Machine\\SYSTEM\\ControlSet001",
677 L
"Registry\\Machine\\SYSTEM\\CurrentControlSet",
679 RegSetValueExW(LinkKey
, NULL
, 0, REG_LINK
, (PCHAR
)&ControlSetKey
, sizeof(PVOID
));