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