3 * Copyright (C) 2003 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.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS hive maker
22 * FILE: tools/mkhive/reginf.h
23 * PURPOSE: Inf file import code
24 * PROGRAMMER: Eric Kohl
27 /* INCLUDES *****************************************************************/
38 #define FLG_ADDREG_BINVALUETYPE 0x00000001
39 #define FLG_ADDREG_NOCLOBBER 0x00000002
40 #define FLG_ADDREG_DELVAL 0x00000004
41 #define FLG_ADDREG_APPEND 0x00000008
42 #define FLG_ADDREG_KEYONLY 0x00000010
43 #define FLG_ADDREG_OVERWRITEONLY 0x00000020
44 #define FLG_ADDREG_TYPE_SZ 0x00000000
45 #define FLG_ADDREG_TYPE_MULTI_SZ 0x00010000
46 #define FLG_ADDREG_TYPE_EXPAND_SZ 0x00020000
47 #define FLG_ADDREG_TYPE_BINARY (0x00000000 | FLG_ADDREG_BINVALUETYPE)
48 #define FLG_ADDREG_TYPE_DWORD (0x00010000 | FLG_ADDREG_BINVALUETYPE)
49 #define FLG_ADDREG_TYPE_NONE (0x00020000 | FLG_ADDREG_BINVALUETYPE)
50 #define FLG_ADDREG_TYPE_MASK (0xFFFF0000 | FLG_ADDREG_BINVALUETYPE)
53 /* FUNCTIONS ****************************************************************/
56 GetRootKey (PCHAR Name)
58 if (!strcasecmp (Name, "HKCR"))
60 strcpy (Name, "\\Registry\\Machine\\SOFTWARE\\Classes\\");
64 if (!strcasecmp (Name, "HKCU"))
66 strcpy (Name, "\\Registry\\User\\.DEFAULT\\");
70 if (!strcasecmp (Name, "HKLM"))
72 strcpy (Name, "\\Registry\\Machine\\");
76 if (!strcasecmp (Name, "HKU"))
78 strcpy (Name, "\\Registry\\User\\");
83 if (!strcasecmp (Name, "HKR"))
91 /***********************************************************************
94 * Append a multisz string to a multisz registry value.
97 AppendMultiSzValue (HKEY KeyHandle,
110 Error = RegQueryValue (KeyHandle,
115 if ((Error != ERROR_SUCCESS) ||
116 (Type != REG_MULTI_SZ))
119 Buffer = malloc (Size + StringSize);
123 Error = RegQueryValue (KeyHandle,
128 if (Error != ERROR_SUCCESS)
131 /* compare each string against all the existing ones */
133 while (*Strings != 0)
135 len = strlen (Strings) + 1;
137 for (p = Buffer; *p != 0; p += strlen (p) + 1)
138 if (!strcasecmp (p, Strings))
141 if (*p == 0) /* not found, need to append it */
143 memcpy (p, Strings, len);
152 DPRINT ("setting value %s to %s\n", ValueName, Buffer);
153 RegSetValue (KeyHandle,
165 /***********************************************************************
168 * Perform an add/delete registry operation depending on the flags.
171 do_reg_operation(HKEY KeyHandle,
176 CHAR EmptyStr = (CHAR)0;
181 if (Flags & FLG_ADDREG_DELVAL) /* deletion */
185 RegDeleteValue (KeyHandle,
190 RegDeleteKey (KeyHandle,
197 if (Flags & FLG_ADDREG_KEYONLY)
200 if (Flags & (FLG_ADDREG_NOCLOBBER | FLG_ADDREG_OVERWRITEONLY))
202 Error = RegQueryValue (KeyHandle,
207 if ((Error == ERROR_SUCCESS) &&
208 (Flags & FLG_ADDREG_NOCLOBBER))
211 if ((Error != ERROR_SUCCESS) &&
212 (Flags & FLG_ADDREG_OVERWRITEONLY))
216 switch (Flags & FLG_ADDREG_TYPE_MASK)
218 case FLG_ADDREG_TYPE_SZ:
222 case FLG_ADDREG_TYPE_MULTI_SZ:
226 case FLG_ADDREG_TYPE_EXPAND_SZ:
227 Type = REG_EXPAND_SZ;
230 case FLG_ADDREG_TYPE_BINARY:
234 case FLG_ADDREG_TYPE_DWORD:
238 case FLG_ADDREG_TYPE_NONE:
247 if (!(Flags & FLG_ADDREG_BINVALUETYPE) ||
248 (Type == REG_DWORD && InfGetFieldCount (Context) == 5))
252 if (Type == REG_MULTI_SZ)
254 if (!InfGetMultiSzField (Context, 5, NULL, 0, &Size))
263 InfGetMultiSzField (Context, 5, Str, Size, NULL);
266 if (Flags & FLG_ADDREG_APPEND)
271 AppendMultiSzValue (KeyHandle,
279 /* else fall through to normal string handling */
283 if (!InfGetStringField (Context, 5, NULL, 0, &Size))
292 InfGetStringField (Context, 5, Str, Size, NULL);
296 if (Type == REG_DWORD)
298 ULONG dw = Str ? strtoul (Str, NULL, 0) : 0;
300 DPRINT("setting dword %s to %lx\n", ValueName, dw);
302 RegSetValue (KeyHandle,
310 DPRINT ("setting value %s to %s\n", ValueName, Str);
314 RegSetValue (KeyHandle,
322 RegSetValue (KeyHandle,
331 else /* get the binary data */
335 if (!InfGetBinaryField (Context, 5, NULL, 0, &Size))
340 Data = malloc (Size);
344 DPRINT("setting binary data %s len %lu\n", ValueName, Size);
345 InfGetBinaryField (Context, 5, Data, Size, NULL);
348 RegSetValue (KeyHandle,
361 /***********************************************************************
364 * Called once for each AddReg and DelReg entry in a given section.
367 registry_callback (HINF hInf, PCHAR Section, BOOL Delete)
369 CHAR Buffer[MAX_INF_STRING_LENGTH];
379 Ok = InfFindFirstLine (hInf, Section, NULL, &Context);
383 for (;Ok; Ok = InfFindNextLine (&Context, &Context))
386 if (!InfGetStringField (&Context, 1, Buffer, MAX_INF_STRING_LENGTH, NULL))
388 if (!GetRootKey (Buffer))
392 Length = strlen (Buffer);
393 if (!InfGetStringField (&Context, 2, Buffer + Length, MAX_INF_STRING_LENGTH - Length, NULL))
396 DPRINT("KeyName: <%s>\n", Buffer);
399 if (!InfGetIntField (&Context, 4, (PLONG)&Flags))
402 DPRINT("Flags: %lx\n", Flags);
404 if (Delete || (Flags & FLG_ADDREG_OVERWRITEONLY))
406 if (RegOpenKey (NULL, Buffer, &KeyHandle) != ERROR_SUCCESS)
408 DPRINT("RegOpenKey(%s) failed\n", Buffer);
409 continue; /* ignore if it doesn't exist */
414 if (RegCreateKey (NULL, Buffer, &KeyHandle) != ERROR_SUCCESS)
416 DPRINT("RegCreateKey(%s) failed\n", Buffer);
422 if (InfGetStringField (&Context, 3, Buffer, MAX_INF_STRING_LENGTH, NULL))
432 if (!do_reg_operation (KeyHandle, ValuePtr, &Context, Flags))
443 ImportRegistryFile(PCHAR FileName,
450 /* Load inf file from install media. */
451 if (!InfOpenFile(&hInf, FileName, &ErrorLine))
453 DPRINT1 ("InfOpenFile() failed\n");
457 if (!registry_callback (hInf, "AddReg", FALSE))
459 DPRINT1 ("registry_callback() failed\n");