ReactOS 0.4.16-dev-117-g38f21f9
infrosgen.c
Go to the documentation of this file.
1/*
2 * PROJECT: .inf file parser
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PROGRAMMER: Royce Mitchell III
5 * Eric Kohl
6 * Ge van Geldorp <gvg@reactos.org>
7 */
8
9/* INCLUDES *****************************************************************/
10
11#include "inflib.h"
12#include "infros.h"
13
14#define NDEBUG
15#include <debug.h>
16
17/* PRIVATE FUNCTIONS ********************************************************/
18
20
21static VOID
23{
24 if (NULL == InfpHeap)
25 {
27 }
28 if (0 <= InfpHeapRefCount)
29 {
31 }
32}
33
34
35/* PUBLIC FUNCTIONS *********************************************************/
36
38
39VOID
41{
42 if (NULL == InfpHeap)
43 {
44 InfpHeap = Heap;
46 }
47}
48
49
54 LANGID LanguageId,
55 PULONG ErrorLine)
56{
59 PCHAR FileBuffer;
60 ULONG FileBufferSize;
61
62 CheckHeap();
63
64 *InfHandle = NULL;
65 *ErrorLine = (ULONG)-1;
66
67 /* Allocate file buffer */
68 FileBufferSize = BufferSize + 2;
69 FileBuffer = MALLOC(FileBufferSize);
70 if (FileBuffer == NULL)
71 {
72 DPRINT1("MALLOC() failed\n");
74 }
75
76 MEMCPY(FileBuffer, Buffer, BufferSize);
77
78 /* Append string terminator */
79 FileBuffer[BufferSize] = 0;
80 FileBuffer[BufferSize + 1] = 0;
81
82 /* Allocate infcache header */
83 Cache = (PINFCACHE)MALLOC(sizeof(INFCACHE));
84 if (Cache == NULL)
85 {
86 DPRINT("MALLOC() failed\n");
87 FREE(FileBuffer);
89 }
90
91 /* Initialize inicache header */
93 sizeof(INFCACHE));
94
95 Cache->LanguageId = LanguageId;
96
97 /* Parse the inf buffer */
98 if (!RtlIsTextUnicode(FileBuffer, FileBufferSize, NULL))
99 {
100// static const BYTE utf8_bom[3] = { 0xef, 0xbb, 0xbf };
101 WCHAR *new_buff;
102// UINT codepage = CP_ACP;
103 UINT offset = 0;
104
105// if (BufferSize > sizeof(utf8_bom) && !memcmp(FileBuffer, utf8_bom, sizeof(utf8_bom) ))
106// {
107// codepage = CP_UTF8;
108// offset = sizeof(utf8_bom);
109// }
110
111 new_buff = MALLOC(FileBufferSize * sizeof(WCHAR));
112 if (new_buff != NULL)
113 {
114 ULONG len;
116 FileBufferSize * sizeof(WCHAR),
117 &len,
118 (char *)FileBuffer + offset,
119 FileBufferSize - offset);
120
122 new_buff,
123 new_buff + len / sizeof(WCHAR),
124 ErrorLine);
125 FREE(new_buff);
126 }
127 else
129 }
130 else
131 {
132 WCHAR *new_buff = (WCHAR *)FileBuffer;
133 /* UCS-16 files should start with the Unicode BOM; we should skip it */
134 if (*new_buff == 0xfeff)
135 {
136 new_buff++;
137 FileBufferSize -= sizeof(WCHAR);
138 }
140 new_buff,
141 (WCHAR*)((char*)new_buff + FileBufferSize),
142 ErrorLine);
143 }
144
145 if (!INF_SUCCESS(Status))
146 {
147 FREE(Cache);
148 Cache = NULL;
149 }
150
151 /* Free file buffer */
152 FREE(FileBuffer);
153
154 *InfHandle = (HINF)Cache;
155
156 return(Status);
157}
158
159
163 LANGID LanguageId,
164 PULONG ErrorLine)
165{
171 PCHAR FileBuffer;
173 ULONG FileBufferLength;
176
177 CheckHeap();
178
179 *InfHandle = NULL;
180 *ErrorLine = (ULONG)-1;
181
182 /* Open the inf file */
184 FileName,
185 0,
186 NULL,
187 NULL);
188
195 if (!INF_SUCCESS(Status))
196 {
197 DPRINT("NtOpenFile() failed (Status %lx)\n", Status);
198 return(Status);
199 }
200
201 DPRINT("NtOpenFile() successful\n");
202
203 /* Query file size */
206 &FileInfo,
209 if (!INF_SUCCESS(Status))
210 {
211 DPRINT("NtQueryInformationFile() failed (Status %lx)\n", Status);
213 return(Status);
214 }
215
216 FileLength = FileInfo.EndOfFile.u.LowPart;
217
218 DPRINT("File size: %lu\n", FileLength);
219
220 /* Allocate file buffer */
221 FileBufferLength = FileLength + 2;
222 FileBuffer = MALLOC(FileBufferLength);
223 if (FileBuffer == NULL)
224 {
225 DPRINT1("MALLOC() failed\n");
228 }
229
230 /* Read file */
231 FileOffset.QuadPart = 0ULL;
233 NULL,
234 NULL,
235 NULL,
237 FileBuffer,
239 &FileOffset,
240 NULL);
241
242 /* Append string terminator */
243 FileBuffer[FileLength] = 0;
244 FileBuffer[FileLength + 1] = 0;
245
247
248 if (!INF_SUCCESS(Status))
249 {
250 DPRINT("NtReadFile() failed (Status %lx)\n", Status);
251 FREE(FileBuffer);
252 return(Status);
253 }
254
255 /* Allocate infcache header */
256 Cache = (PINFCACHE)MALLOC(sizeof(INFCACHE));
257 if (Cache == NULL)
258 {
259 DPRINT("MALLOC() failed\n");
260 FREE(FileBuffer);
262 }
263
264 /* Initialize inicache header */
266 sizeof(INFCACHE));
267
268 Cache->LanguageId = LanguageId;
269
270 /* Parse the inf buffer */
271 if (!RtlIsTextUnicode(FileBuffer, FileBufferLength, NULL))
272 {
273// static const BYTE utf8_bom[3] = { 0xef, 0xbb, 0xbf };
274 WCHAR *new_buff;
275// UINT codepage = CP_ACP;
276 UINT offset = 0;
277
278// if (FileLength > sizeof(utf8_bom) && !memcmp(FileBuffer, utf8_bom, sizeof(utf8_bom) ))
279// {
280// codepage = CP_UTF8;
281// offset = sizeof(utf8_bom);
282// }
283
284 new_buff = MALLOC(FileBufferLength * sizeof(WCHAR));
285 if (new_buff != NULL)
286 {
287 ULONG len;
289 FileBufferLength * sizeof(WCHAR),
290 &len,
291 (char *)FileBuffer + offset,
292 FileBufferLength - offset);
293
295 new_buff,
296 new_buff + len / sizeof(WCHAR),
297 ErrorLine);
298 FREE(new_buff);
299 }
300 else
302 }
303 else
304 {
305 WCHAR *new_buff = (WCHAR *)FileBuffer;
306 /* UCS-16 files should start with the Unicode BOM; we should skip it */
307 if (*new_buff == 0xfeff)
308 {
309 new_buff++;
310 FileBufferLength -= sizeof(WCHAR);
311 }
313 new_buff,
314 (WCHAR*)((char*)new_buff + FileBufferLength),
315 ErrorLine);
316 }
317
318 if (!INF_SUCCESS(Status))
319 {
320 FREE(Cache);
321 Cache = NULL;
322 }
323
324 /* Free file buffer */
325 FREE(FileBuffer);
326
327 *InfHandle = (HINF)Cache;
328
329 return(Status);
330}
331
332
333VOID
335{
337
338 Cache = (PINFCACHE)InfHandle;
339
340 if (Cache == NULL)
341 {
342 return;
343 }
344
345 while (Cache->FirstSection != NULL)
346 {
347 Cache->FirstSection = InfpFreeSection(Cache->FirstSection);
348 }
349 Cache->LastSection = NULL;
350
351 FREE(Cache);
352
353 if (0 < InfpHeapRefCount)
354 {
356 if (0 == InfpHeapRefCount)
357 {
359 InfpHeap = NULL;
360 }
361 }
362}
363
364
365/* EOF */
#define FREE(ptr, size)
Definition: auth_des.c:64
LONG NTSTATUS
Definition: precomp.h:26
#define FILE_NON_DIRECTORY_FILE
Definition: constants.h:492
#define DPRINT1
Definition: precomp.h:8
PULONG * PHINF
Definition: inffile.h:18
#define MALLOC(Size)
Definition: builddep.h:73
#define INF_SUCCESS(x)
Definition: builddep.h:82
#define ZEROMEMORY(Area, Size)
Definition: builddep.h:74
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:160
Definition: bufpool.h:45
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define BufferSize
Definition: mmc.h:75
#define NULL
Definition: types.h:112
#define GENERIC_READ
Definition: compat.h:135
#define FILE_SHARE_READ
Definition: compat.h:136
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
Status
Definition: gdiplustypes.h:25
GLenum GLsizei len
Definition: glext.h:6722
GLintptr offset
Definition: glext.h:5920
PINFCACHESECTION InfpFreeSection(PINFCACHESECTION Section)
Definition: infcore.c:119
static BOOLEAN InfpParseBuffer(PINFCACHE file, PCCHAR buffer, PCCHAR end, PULONG error_line)
Definition: inffile.c:878
struct _INFCACHE * PINFCACHE
int INFSTATUS
Definition: infpriv.h:77
#define INF_STATUS_INSUFFICIENT_RESOURCES
Definition: infpriv.h:15
VOID InfSetHeap(PVOID Heap)
Definition: infrosgen.c:40
NTSTATUS InfOpenFile(PHINF InfHandle, PUNICODE_STRING FileName, LANGID LanguageId, PULONG ErrorLine)
Definition: infrosgen.c:161
static int InfpHeapRefCount
Definition: infrosgen.c:19
PVOID InfpHeap
Definition: infrosgen.c:37
VOID InfCloseFile(HINF InfHandle)
Definition: infrosgen.c:334
static VOID CheckHeap(VOID)
Definition: infrosgen.c:22
NTSTATUS InfOpenBufferedFile(PHINF InfHandle, PVOID Buffer, ULONG BufferSize, LANGID LanguageId, PULONG ErrorLine)
Definition: infrosgen.c:51
PVOID HINF
Definition: infsupp.h:21
USHORT LANGID
Definition: mui.h:9
#define MEMCPY(DST, SRC, BYTES)
Definition: macros.h:231
#define ULL(a, b)
Definition: format_msg.c:27
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
unsigned int UINT
Definition: ndis.h:50
_Out_ PNDIS_HANDLE _Out_ PUINT FileLength
Definition: ndis.h:3228
NTSYSAPI BOOLEAN NTAPI RtlIsTextUnicode(_In_ CONST VOID *Buffer, _In_ INT Size, _Inout_opt_ INT *Flags)
_Use_decl_annotations_ NTSTATUS NTAPI RtlMultiByteToUnicodeN(_Out_ PWCH UnicodeString, _In_ ULONG UnicodeSize, _Out_opt_ PULONG ResultSize, _In_ PCCH MbString, _In_ ULONG MbSize)
Definition: nlsboot.c:62
NTSYSAPI PVOID NTAPI RtlDestroyHeap(IN PVOID HeapHandle)
NTSYSAPI NTSTATUS NTAPI NtOpenFile(OUT PHANDLE phFile, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG ShareMode, IN ULONG OpenMode)
Definition: file.c:3952
#define SYNCHRONIZE
Definition: nt_native.h:61
NTSYSAPI PVOID NTAPI RtlCreateHeap(IN ULONG Flags, IN PVOID HeapBase OPTIONAL, IN ULONG ReserveSize OPTIONAL, IN ULONG CommitSize OPTIONAL, IN PVOID Lock OPTIONAL, IN PRTL_HEAP_PARAMETERS Parameters OPTIONAL)
NTSYSAPI NTSTATUS NTAPI NtQueryInformationFile(IN HANDLE hFile, OUT PIO_STATUS_BLOCK pIoStatusBlock, OUT PVOID FileInformationBuffer, IN ULONG FileInformationBufferLength, IN FILE_INFORMATION_CLASS FileInfoClass)
#define HEAP_GROWABLE
Definition: nt_native.h:1693
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
#define FileStandardInformation
Definition: propsheet.cpp:61
NTSTATUS NTAPI NtReadFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key)
#define DPRINT
Definition: sndvol32.h:73
Definition: fatfs.h:173
uint32_t * PULONG
Definition: typedefs.h:59
uint32_t ULONG
Definition: typedefs.h:59
char * PCHAR
Definition: typedefs.h:51
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254
__wchar_t WCHAR
Definition: xmlstorage.h:180