]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/C/Common/MemoryFile.c
BaseTools: Fix compile error on VS2010
[mirror_edk2.git] / BaseTools / Source / C / Common / MemoryFile.c
CommitLineData
30fdf114 1/** @file\r
97fa0ee9 2This contains some useful functions for accessing files.\r
30fdf114 3\r
aee34651 4Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
40d841f6 5This program and the accompanying materials \r
30fdf114
LG
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php \r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
30fdf114
LG
13**/\r
14\r
15#include <assert.h>\r
16#include <string.h>\r
17#include <ctype.h>\r
18#include <stdlib.h>\r
19#include "CommonLib.h"\r
20#include "MemoryFile.h"\r
21\r
22\r
23//\r
24// Local (static) function prototypes\r
25//\r
26STATIC\r
27VOID\r
28CheckMemoryFileState (\r
29 IN EFI_HANDLE InputMemoryFile\r
30 );\r
31\r
32//\r
33// Function implementations\r
34//\r
35\r
36EFI_STATUS\r
37GetMemoryFile (\r
38 IN CHAR8 *InputFileName,\r
39 OUT EFI_HANDLE *OutputMemoryFile\r
40 )\r
41/*++\r
42\r
43Routine Description:\r
44\r
45 This opens a file, reads it into memory and returns a memory file\r
46 object.\r
47\r
48Arguments:\r
49\r
50 InputFile Memory file image.\r
51 OutputMemoryFile Handle to memory file\r
52\r
53Returns:\r
54\r
55 EFI_STATUS\r
56 OutputMemoryFile is valid if !EFI_ERROR\r
57\r
58--*/\r
59{\r
60 EFI_STATUS Status;\r
61 CHAR8 *InputFileImage;\r
62 UINT32 BytesRead;\r
63 MEMORY_FILE *NewMemoryFile;\r
64\r
65 Status = GetFileImage (InputFileName, &InputFileImage, &BytesRead);\r
66 if (EFI_ERROR (Status)) {\r
67 return Status;\r
68 }\r
69\r
70 NewMemoryFile = malloc (sizeof (*NewMemoryFile));\r
71 if (NewMemoryFile == NULL) {\r
aee34651 72 free (InputFileImage);\r
30fdf114
LG
73 return EFI_OUT_OF_RESOURCES;\r
74 }\r
75\r
76 NewMemoryFile->FileImage = InputFileImage;\r
77 NewMemoryFile->CurrentFilePointer = InputFileImage;\r
78 NewMemoryFile->Eof = InputFileImage + BytesRead;\r
79\r
80 *OutputMemoryFile = (EFI_HANDLE)NewMemoryFile;\r
81\r
82 CheckMemoryFileState (*OutputMemoryFile);\r
83\r
84 return EFI_SUCCESS;\r
85}\r
86\r
87\r
88EFI_STATUS\r
89FreeMemoryFile (\r
90 IN EFI_HANDLE InputMemoryFile\r
91 )\r
92/*++\r
93\r
94Routine Description:\r
95\r
96 Frees all memory associated with the input memory file.\r
97\r
98Arguments:\r
99\r
100 InputMemoryFile Handle to memory file\r
101\r
102Returns:\r
103\r
104 EFI_STATUS\r
105\r
106--*/\r
107{\r
108 MEMORY_FILE *MemoryFile;\r
109\r
110 CheckMemoryFileState (InputMemoryFile);\r
111\r
112 MemoryFile = (MEMORY_FILE*)InputMemoryFile;\r
113\r
114 free (MemoryFile->FileImage);\r
115\r
116 //\r
117 // Invalidate state of MEMORY_FILE structure to catch invalid usage.\r
118 //\r
119 memset (MemoryFile, 0xcc, sizeof (*MemoryFile));\r
120 MemoryFile->Eof -= 1;\r
121\r
122 free (MemoryFile);\r
123\r
124 return EFI_SUCCESS;\r
125}\r
126\r
127\r
128CHAR8 *\r
129ReadMemoryFileLine (\r
130 IN EFI_HANDLE InputMemoryFile\r
131 )\r
132/*++\r
133\r
134Routine Description:\r
135\r
136 This function reads a line from the memory file. The newline characters\r
137 are stripped and a null terminated string is returned.\r
138\r
139 If the string pointer returned is non-NULL, then the caller must free the\r
140 memory associated with this string.\r
141\r
142Arguments:\r
143\r
144 InputMemoryFile Handle to memory file\r
145\r
146Returns:\r
147\r
148 NULL if error or EOF\r
149 NULL character termincated string otherwise (MUST BE FREED BY CALLER)\r
150\r
151--*/\r
152{\r
153 CHAR8 *EndOfLine;\r
154 UINTN CharsToCopy;\r
155 MEMORY_FILE *InputFile;\r
156 UINTN BytesToEof;\r
157 CHAR8 *OutputString;\r
158\r
159 //\r
160 // Verify input parameters are not null\r
161 //\r
162 CheckMemoryFileState (InputMemoryFile);\r
163\r
164 InputFile = (MEMORY_FILE*)InputMemoryFile;\r
165\r
166 //\r
167 // Check for end of file condition\r
168 //\r
169 if (InputFile->CurrentFilePointer >= InputFile->Eof) {\r
170 return NULL;\r
171 }\r
172\r
173 //\r
174 // Determine the number of bytes remaining until the EOF\r
175 //\r
176 BytesToEof = InputFile->Eof - InputFile->CurrentFilePointer;\r
177\r
178 //\r
179 // Find the next newline char\r
180 //\r
181 EndOfLine = memchr (InputFile->CurrentFilePointer, '\n', BytesToEof);\r
182\r
183 //\r
184 // Determine the number of characters to copy.\r
185 //\r
186 if (EndOfLine == 0) {\r
187 //\r
188 // If no newline found, copy to the end of the file.\r
189 //\r
190 CharsToCopy = InputFile->Eof - InputFile->CurrentFilePointer;\r
191 } else {\r
192 //\r
193 // Newline found in the file.\r
194 //\r
195 CharsToCopy = EndOfLine - InputFile->CurrentFilePointer;\r
196 }\r
197\r
2cec9ccd 198 OutputString = malloc (CharsToCopy + 1);\r
30fdf114
LG
199 if (OutputString == NULL) {\r
200 return NULL;\r
201 }\r
202\r
203 //\r
204 // Copy the line.\r
205 //\r
206 memcpy (OutputString, InputFile->CurrentFilePointer, CharsToCopy);\r
207\r
208 //\r
209 // Add the null termination over the 0x0D\r
210 //\r
211 if (OutputString[CharsToCopy - 1] == '\r') {\r
212\r
213 OutputString[CharsToCopy - 1] = '\0';\r
214\r
215 } else {\r
216\r
217 OutputString[CharsToCopy] = '\0';\r
218\r
219 }\r
220\r
221 //\r
222 // Increment the current file pointer (include the 0x0A)\r
223 //\r
224 InputFile->CurrentFilePointer += CharsToCopy + 1;\r
60a53782
LG
225 if (InputFile->CurrentFilePointer > InputFile->Eof) {\r
226 InputFile->CurrentFilePointer = InputFile->Eof;\r
227 }\r
30fdf114
LG
228 CheckMemoryFileState (InputMemoryFile);\r
229\r
230 //\r
231 // Return the string\r
232 //\r
233 return OutputString;\r
234}\r
235\r
236\r
237STATIC\r
238VOID\r
239CheckMemoryFileState (\r
240 IN EFI_HANDLE InputMemoryFile\r
241 )\r
242{\r
243 MEMORY_FILE *MemoryFile;\r
244\r
245 assert (InputMemoryFile != NULL);\r
246\r
247 MemoryFile = (MEMORY_FILE*)InputMemoryFile;\r
248\r
249 assert (MemoryFile->FileImage != NULL);\r
250 assert (MemoryFile->CurrentFilePointer != NULL);\r
251 assert (MemoryFile->Eof != NULL);\r
252 assert (MemoryFile->Eof >= MemoryFile->FileImage);\r
253 assert (MemoryFile->CurrentFilePointer >= MemoryFile->FileImage);\r
254 assert (MemoryFile->CurrentFilePointer <= MemoryFile->Eof);\r
255}\r
256\r
257\r