[USETUP] Check for invalid INF handles before doing operations.
[reactos.git] / base / setup / usetup / spapisup / infsupp.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS text-mode setup
22 * FILE: base/setup/lib/infsupp.c
23 * PURPOSE: Interfacing with Setup* API .INF Files support functions
24 * PROGRAMMERS: Hervé Poussineau
25 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
26 */
27
28 /* INCLUDES ******************************************************************/
29
30 #include "usetup.h"
31
32 #define NDEBUG
33 #include <debug.h>
34
35 /* SETUP* API COMPATIBILITY FUNCTIONS ****************************************/
36
37 /* Functions from the INFLIB library */
38
39 extern VOID InfCloseFile(HINF InfHandle);
40 // #define SetupCloseInfFile InfCloseFile
41 VOID
42 WINAPI
43 SetupCloseInfFile(
44 IN HINF InfHandle)
45 {
46 if (InfHandle == INVALID_HANDLE_VALUE)
47 return;
48 InfCloseFile(InfHandle);
49 }
50
51 // #define SetupFindFirstLineW InfpFindFirstLineW
52 BOOL
53 WINAPI
54 SetupFindFirstLineW(
55 IN HINF InfHandle,
56 IN PCWSTR Section,
57 IN PCWSTR Key,
58 IN OUT PINFCONTEXT Context)
59 {
60 PINFCONTEXT pContext;
61 BOOL ret;
62
63 if (InfHandle == INVALID_HANDLE_VALUE)
64 return FALSE;
65
66 ret = InfFindFirstLine(InfHandle, Section, Key, &pContext);
67 if (!ret)
68 return FALSE;
69
70 memcpy(Context, pContext, sizeof(INFCONTEXT));
71 InfFreeContext(pContext);
72 return TRUE;
73 }
74
75 extern BOOLEAN InfFindNextLine(PINFCONTEXT ContextIn,
76 PINFCONTEXT ContextOut);
77 // #define SetupFindNextLine InfFindNextLine
78 BOOL
79 WINAPI
80 SetupFindNextLine(
81 IN PINFCONTEXT ContextIn,
82 OUT PINFCONTEXT ContextOut)
83 {
84 return !!InfFindNextLine(ContextIn, ContextOut);
85 }
86
87 extern LONG InfGetFieldCount(PINFCONTEXT Context);
88 // #define SetupGetFieldCount InfGetFieldCount
89 ULONG
90 WINAPI
91 SetupGetFieldCount(
92 IN PINFCONTEXT Context)
93 {
94 return (ULONG)InfGetFieldCount(Context);
95 }
96
97 /*
98 * This function corresponds to an undocumented but exported SetupAPI function
99 * that exists since WinNT4 and is still present in Win10.
100 * The returned string pointer is a read-only pointer to a string in the
101 * maintained INF cache, and is always in UNICODE (on NT systems).
102 */
103 extern BOOLEAN InfGetDataField(PINFCONTEXT Context,
104 ULONG FieldIndex,
105 PWCHAR *Data);
106 PCWSTR
107 WINAPI
108 pSetupGetField(
109 IN PINFCONTEXT Context,
110 IN ULONG FieldIndex)
111 {
112 PWCHAR Data = NULL;
113 if (!InfGetDataField(Context, FieldIndex, &Data))
114 return NULL;
115 return Data;
116 }
117
118 extern BOOLEAN InfGetBinaryField(PINFCONTEXT Context,
119 ULONG FieldIndex,
120 PUCHAR ReturnBuffer,
121 ULONG ReturnBufferSize,
122 PULONG RequiredSize);
123 // #define SetupGetBinaryField InfGetBinaryField
124 BOOL
125 WINAPI
126 SetupGetBinaryField(
127 IN PINFCONTEXT Context,
128 IN ULONG FieldIndex,
129 OUT PUCHAR ReturnBuffer,
130 IN ULONG ReturnBufferSize,
131 OUT PULONG RequiredSize)
132 {
133 return !!InfGetBinaryField(Context,
134 FieldIndex,
135 ReturnBuffer,
136 ReturnBufferSize,
137 RequiredSize);
138 }
139
140 extern BOOLEAN InfGetIntField(PINFCONTEXT Context,
141 ULONG FieldIndex,
142 INT *IntegerValue);
143 // #define SetupGetIntField InfGetIntField
144 BOOL
145 WINAPI
146 SetupGetIntField(
147 IN PINFCONTEXT Context,
148 IN ULONG FieldIndex,
149 OUT INT *IntegerValue) // PINT
150 {
151 return !!InfGetIntField(Context, FieldIndex, IntegerValue);
152 }
153
154 extern BOOLEAN InfGetMultiSzField(PINFCONTEXT Context,
155 ULONG FieldIndex,
156 PWSTR ReturnBuffer,
157 ULONG ReturnBufferSize,
158 PULONG RequiredSize);
159 // #define SetupGetMultiSzFieldW InfGetMultiSzField
160 BOOL
161 WINAPI
162 SetupGetMultiSzFieldW(
163 IN PINFCONTEXT Context,
164 IN ULONG FieldIndex,
165 OUT PWSTR ReturnBuffer,
166 IN ULONG ReturnBufferSize,
167 OUT PULONG RequiredSize)
168 {
169 return !!InfGetMultiSzField(Context,
170 FieldIndex,
171 ReturnBuffer,
172 ReturnBufferSize,
173 RequiredSize);
174 }
175
176 extern BOOLEAN InfGetStringField(PINFCONTEXT Context,
177 ULONG FieldIndex,
178 PWSTR ReturnBuffer,
179 ULONG ReturnBufferSize,
180 PULONG RequiredSize);
181 // #define SetupGetStringFieldW InfGetStringField
182 BOOL
183 WINAPI
184 SetupGetStringFieldW(
185 IN PINFCONTEXT Context,
186 IN ULONG FieldIndex,
187 OUT PWSTR ReturnBuffer,
188 IN ULONG ReturnBufferSize,
189 OUT PULONG RequiredSize)
190 {
191 return !!InfGetStringField(Context,
192 FieldIndex,
193 ReturnBuffer,
194 ReturnBufferSize,
195 RequiredSize);
196 }
197
198 /* SetupOpenInfFileW with support for a user-provided LCID */
199 // #define SetupOpenInfFileExW InfpOpenInfFileW
200 HINF
201 WINAPI
202 SetupOpenInfFileExW(
203 IN PCWSTR FileName,
204 IN PCWSTR InfClass,
205 IN DWORD InfStyle,
206 IN LCID LocaleId,
207 OUT PUINT ErrorLine)
208 {
209 HINF hInf = NULL;
210 UNICODE_STRING FileNameU;
211 ULONG ErrorLineUL;
212 NTSTATUS Status;
213
214 RtlInitUnicodeString(&FileNameU, FileName);
215 Status = InfOpenFile(&hInf,
216 &FileNameU,
217 LANGIDFROMLCID(LocaleId),
218 &ErrorLineUL);
219 *ErrorLine = (UINT)ErrorLineUL;
220 if (!NT_SUCCESS(Status))
221 return INVALID_HANDLE_VALUE;
222
223 return hInf;
224 }
225
226
227 /* GLOBALS *******************************************************************/
228
229 pSpInfCloseInfFile SpInfCloseInfFile = SetupCloseInfFile;
230 pSpInfFindFirstLine SpInfFindFirstLine = SetupFindFirstLineW;
231 pSpInfFindNextLine SpInfFindNextLine = SetupFindNextLine;
232 pSpInfGetFieldCount SpInfGetFieldCount = SetupGetFieldCount;
233 pSpInfGetBinaryField SpInfGetBinaryField = SetupGetBinaryField;
234 pSpInfGetIntField SpInfGetIntField = SetupGetIntField;
235 pSpInfGetMultiSzField SpInfGetMultiSzField = SetupGetMultiSzFieldW;
236 pSpInfGetStringField SpInfGetStringField = SetupGetStringFieldW;
237 pSpInfGetField SpInfGetField = pSetupGetField;
238 pSpInfOpenInfFile SpInfOpenInfFile = SetupOpenInfFileExW;
239
240
241 /* HELPER FUNCTIONS **********************************************************/
242
243 HINF WINAPI
244 INF_OpenBufferedFileA(
245 IN PSTR FileBuffer,
246 IN ULONG FileSize,
247 IN PCSTR InfClass,
248 IN DWORD InfStyle,
249 IN LCID LocaleId,
250 OUT PUINT ErrorLine)
251 {
252 HINF hInf = NULL;
253 ULONG ErrorLineUL;
254 NTSTATUS Status;
255
256 Status = InfOpenBufferedFile(&hInf,
257 FileBuffer,
258 FileSize,
259 LANGIDFROMLCID(LocaleId),
260 &ErrorLineUL);
261 *ErrorLine = (UINT)ErrorLineUL;
262 if (!NT_SUCCESS(Status))
263 return INVALID_HANDLE_VALUE;
264
265 return hInf;
266 }
267
268 /* EOF */