3 * Copyright (C) 2003, 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/reginf.h
22 * PURPOSE: Inf file import code
23 * PROGRAMMER: Eric Kohl
27 /* INCLUDES *****************************************************************/
36 #define FLG_ADDREG_BINVALUETYPE 0x00000001
37 #define FLG_ADDREG_NOCLOBBER 0x00000002
38 #define FLG_ADDREG_DELVAL 0x00000004
39 #define FLG_ADDREG_APPEND 0x00000008
40 #define FLG_ADDREG_KEYONLY 0x00000010
41 #define FLG_ADDREG_OVERWRITEONLY 0x00000020
42 #define FLG_ADDREG_TYPE_SZ 0x00000000
43 #define FLG_ADDREG_TYPE_MULTI_SZ 0x00010000
44 #define FLG_ADDREG_TYPE_EXPAND_SZ 0x00020000
45 #define FLG_ADDREG_TYPE_BINARY (0x00000000 | FLG_ADDREG_BINVALUETYPE)
46 #define FLG_ADDREG_TYPE_DWORD (0x00010000 | FLG_ADDREG_BINVALUETYPE)
47 #define FLG_ADDREG_TYPE_NONE (0x00020000 | FLG_ADDREG_BINVALUETYPE)
48 #define FLG_ADDREG_TYPE_MASK (0xFFFF0000 | FLG_ADDREG_BINVALUETYPE)
51 /* FUNCTIONS ****************************************************************/
54 GetRootKey (PCHAR Name
)
56 if (!strcasecmp (Name
, "HKCR"))
58 strcpy (Name
, "\\Registry\\Machine\\SOFTWARE\\Classes\\");
62 if (!strcasecmp (Name
, "HKCU"))
64 strcpy (Name
, "\\Registry\\User\\.DEFAULT\\");
68 if (!strcasecmp (Name
, "HKLM"))
70 strcpy (Name
, "\\Registry\\Machine\\");
74 if (!strcasecmp (Name
, "HKU"))
76 strcpy (Name
, "\\Registry\\User\\");
81 if (!strcasecmp (Name
, "HKR"))
89 /***********************************************************************
92 * Append a multisz string to a multisz registry value.
95 AppendMultiSzValue (HKEY KeyHandle
,
108 Error
= RegQueryValueExA (KeyHandle
,
114 if ((Error
!= ERROR_SUCCESS
) ||
115 (Type
!= REG_MULTI_SZ
))
118 Buffer
= malloc (Size
+ StringSize
);
122 Error
= RegQueryValueExA (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 RegSetValueExA (KeyHandle
,
166 /***********************************************************************
169 * Perform an add/delete registry operation depending on the flags.
172 do_reg_operation(HKEY KeyHandle
,
177 CHAR EmptyStr
= (CHAR
)0;
182 if (Flags
& FLG_ADDREG_DELVAL
) /* deletion */
186 RegDeleteValueA (KeyHandle
,
191 RegDeleteKeyA (KeyHandle
,
198 if (Flags
& FLG_ADDREG_KEYONLY
)
201 if (Flags
& (FLG_ADDREG_NOCLOBBER
| FLG_ADDREG_OVERWRITEONLY
))
203 Error
= RegQueryValueExA (KeyHandle
,
209 if ((Error
== ERROR_SUCCESS
) &&
210 (Flags
& FLG_ADDREG_NOCLOBBER
))
213 if ((Error
!= ERROR_SUCCESS
) &&
214 (Flags
& FLG_ADDREG_OVERWRITEONLY
))
218 switch (Flags
& FLG_ADDREG_TYPE_MASK
)
220 case FLG_ADDREG_TYPE_SZ
:
224 case FLG_ADDREG_TYPE_MULTI_SZ
:
228 case FLG_ADDREG_TYPE_EXPAND_SZ
:
229 Type
= REG_EXPAND_SZ
;
232 case FLG_ADDREG_TYPE_BINARY
:
236 case FLG_ADDREG_TYPE_DWORD
:
240 case FLG_ADDREG_TYPE_NONE
:
249 if (!(Flags
& FLG_ADDREG_BINVALUETYPE
) ||
250 (Type
== REG_DWORD
&& InfHostGetFieldCount (Context
) == 5))
254 if (Type
== REG_MULTI_SZ
)
256 if (InfHostGetMultiSzField (Context
, 5, NULL
, 0, &Size
) != 0)
265 InfHostGetMultiSzField (Context
, 5, Str
, Size
, NULL
);
268 if (Flags
& FLG_ADDREG_APPEND
)
273 AppendMultiSzValue (KeyHandle
,
281 /* else fall through to normal string handling */
285 if (InfHostGetStringField (Context
, 5, NULL
, 0, &Size
) != 0)
294 InfHostGetStringField (Context
, 5, Str
, Size
, NULL
);
298 if (Type
== REG_DWORD
)
300 ULONG dw
= Str
? strtoul (Str
, NULL
, 0) : 0;
302 DPRINT("setting dword %s to %lx\n", ValueName
, dw
);
304 RegSetValueExA (KeyHandle
,
313 DPRINT("setting value %s to %s\n", ValueName
, Str
);
317 RegSetValueExA (KeyHandle
,
326 RegSetValueExA (KeyHandle
,
331 (ULONG
)sizeof(CHAR
));
336 else /* get the binary data */
340 if (InfHostGetBinaryField (Context
, 5, NULL
, 0, &Size
) != 0)
345 Data
= malloc (Size
);
349 DPRINT("setting binary data %s len %lu\n", ValueName
, Size
);
350 InfHostGetBinaryField (Context
, 5, Data
, Size
, NULL
);
353 RegSetValueExA (KeyHandle
,
367 /***********************************************************************
370 * Called once for each AddReg and DelReg entry in a given section.
373 registry_callback (HINF hInf
, PCHAR Section
, BOOL Delete
)
375 CHAR Buffer
[MAX_INF_STRING_LENGTH
];
380 PINFCONTEXT Context
= NULL
;
385 Ok
= InfHostFindFirstLine (hInf
, Section
, NULL
, &Context
) == 0;
387 return TRUE
; /* Don't fail if the section isn't present */
389 for (;Ok
; Ok
= (InfHostFindNextLine (Context
, Context
) == 0))
392 if (InfHostGetStringField (Context
, 1, Buffer
, MAX_INF_STRING_LENGTH
, NULL
) != 0)
394 if (!GetRootKey (Buffer
))
398 Length
= strlen (Buffer
);
399 if (InfHostGetStringField (Context
, 2, Buffer
+ Length
, MAX_INF_STRING_LENGTH
- Length
, NULL
) != 0)
402 DPRINT("KeyName: <%s>\n", Buffer
);
406 Flags
= FLG_ADDREG_DELVAL
;
411 if (InfHostGetIntField (Context
, 4, (PLONG
)&Flags
) != 0)
415 DPRINT("Flags: %lx\n", Flags
);
417 if (Delete
|| (Flags
& FLG_ADDREG_OVERWRITEONLY
))
419 if (RegOpenKeyA (NULL
, Buffer
, &KeyHandle
) != ERROR_SUCCESS
)
421 DPRINT("RegOpenKey(%s) failed\n", Buffer
);
422 continue; /* ignore if it doesn't exist */
427 if (RegCreateKeyA (NULL
, Buffer
, &KeyHandle
) != ERROR_SUCCESS
)
429 DPRINT("RegCreateKey(%s) failed\n", Buffer
);
435 if (InfHostGetStringField (Context
, 3, Buffer
, MAX_INF_STRING_LENGTH
, NULL
) == 0)
445 if (!do_reg_operation (KeyHandle
, ValuePtr
, Context
, Flags
))
451 InfHostFreeContext(Context
);
458 ImportRegistryFile(PCHAR FileName
)
463 /* Load inf file from install media. */
464 if (InfHostOpenFile(&hInf
, FileName
, &ErrorLine
) != 0)
466 DPRINT1 ("InfHostOpenFile() failed\n");
470 if (!registry_callback (hInf
, "DelReg", TRUE
))
472 DPRINT1 ("registry_callback() failed\n");
475 if (!registry_callback (hInf
, "AddReg", FALSE
))
477 DPRINT1 ("registry_callback() failed\n");
480 InfHostCloseFile (hInf
);