Create a branch for header work.
[reactos.git] / lib / inflib / infhostgen.c
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 "infhost.h"
13
14 #define NDEBUG
15 #include <debug.h>
16
17 /* PUBLIC FUNCTIONS *********************************************************/
18
19 int
20 InfHostOpenBufferedFile(PHINF InfHandle,
21 void *Buffer,
22 ULONG BufferSize,
23 ULONG *ErrorLine)
24 {
25 INFSTATUS Status;
26 PINFCACHE Cache;
27 CHAR *FileBuffer;
28
29 *InfHandle = NULL;
30 *ErrorLine = (ULONG)-1;
31
32 /* Allocate file buffer */
33 FileBuffer = MALLOC(BufferSize + 1);
34 if (FileBuffer == NULL)
35 {
36 DPRINT1("MALLOC() failed\n");
37 return(INF_STATUS_INSUFFICIENT_RESOURCES);
38 }
39
40 MEMCPY(FileBuffer, Buffer, BufferSize);
41
42 /* Append string terminator */
43 FileBuffer[BufferSize] = 0;
44
45 /* Allocate infcache header */
46 Cache = (PINFCACHE)MALLOC(sizeof(INFCACHE));
47 if (Cache == NULL)
48 {
49 DPRINT1("MALLOC() failed\n");
50 FREE(FileBuffer);
51 return(INF_STATUS_INSUFFICIENT_RESOURCES);
52 }
53
54 /* Initialize inicache header */
55 ZEROMEMORY(Cache,
56 sizeof(INFCACHE));
57
58 /* Parse the inf buffer */
59 Status = InfpParseBuffer (Cache,
60 FileBuffer,
61 FileBuffer + BufferSize,
62 ErrorLine);
63 if (!INF_SUCCESS(Status))
64 {
65 FREE(Cache);
66 Cache = NULL;
67 }
68
69 /* Free file buffer */
70 FREE(FileBuffer);
71
72 *InfHandle = (HINF)Cache;
73
74 return INF_SUCCESS(Status) ? 0 : -1;
75 }
76
77
78 int
79 InfHostOpenFile(PHINF InfHandle,
80 const CHAR *FileName,
81 ULONG *ErrorLine)
82 {
83 FILE *File;
84 CHAR *FileBuffer;
85 ULONG FileLength;
86 PINFCACHE Cache;
87 INFSTATUS Status;
88
89 *InfHandle = NULL;
90 *ErrorLine = (ULONG)-1;
91
92 /* Open the inf file */
93 File = fopen(FileName, "rb");
94 if (NULL == File)
95 {
96 DPRINT1("fopen() failed (errno %d)\n", errno);
97 return -1;
98 }
99
100 DPRINT("fopen() successful\n");
101
102 /* Query file size */
103 if (fseek(File, (size_t)0, SEEK_END))
104 {
105 DPRINT1("fseek() to EOF failed (errno %d)\n", errno);
106 fclose(File);
107 return -1;
108 }
109
110 FileLength = (ULONG)ftell(File);
111 if ((ULONG) -1 == FileLength)
112 {
113 DPRINT1("ftell() failed (errno %d)\n", errno);
114 fclose(File);
115 return -1;
116 }
117 DPRINT("File size: %u\n", (UINT)FileLength);
118
119 /* Rewind */
120 if (fseek(File, (size_t)0, SEEK_SET))
121 {
122 DPRINT1("fseek() to BOF failed (errno %d)\n", errno);
123 fclose(File);
124 return -1;
125 }
126
127 /* Allocate file buffer */
128 FileBuffer = MALLOC(FileLength + 1);
129 if (FileBuffer == NULL)
130 {
131 DPRINT1("MALLOC() failed\n");
132 fclose(File);
133 return -1;
134 }
135
136 /* Read file */
137 if (FileLength != fread(FileBuffer, (size_t)1, (size_t)FileLength, File))
138 {
139 DPRINT1("fread() failed (errno %d)\n", errno);
140 fclose(File);
141 return -1;
142 }
143
144 fclose(File);
145
146 /* Append string terminator */
147 FileBuffer[FileLength] = 0;
148
149 /* Allocate infcache header */
150 Cache = (PINFCACHE)MALLOC(sizeof(INFCACHE));
151 if (Cache == NULL)
152 {
153 DPRINT1("MALLOC() failed\n");
154 FREE(FileBuffer);
155 return -1;
156 }
157
158 /* Initialize inicache header */
159 ZEROMEMORY(Cache,
160 sizeof(INFCACHE));
161
162 /* Parse the inf buffer */
163 Status = InfpParseBuffer (Cache,
164 FileBuffer,
165 FileBuffer + FileLength,
166 ErrorLine);
167 if (!INF_SUCCESS(Status))
168 {
169 FREE(Cache);
170 Cache = NULL;
171 }
172
173 /* Free file buffer */
174 FREE(FileBuffer);
175
176 *InfHandle = (HINF)Cache;
177
178 return INF_SUCCESS(Status) ? 0 : -1;
179 }
180
181
182 void
183 InfHostCloseFile(HINF InfHandle)
184 {
185 PINFCACHE Cache;
186
187 Cache = (PINFCACHE)InfHandle;
188
189 if (Cache == NULL)
190 {
191 return;
192 }
193
194 while (Cache->FirstSection != NULL)
195 {
196 Cache->FirstSection = InfpFreeSection(Cache->FirstSection);
197 }
198 Cache->LastSection = NULL;
199
200 FREE(Cache);
201 }
202
203 /* EOF */