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