]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/C/Common/MemoryFile.c
License header updated to match correct format.
[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
97fa0ee9 4Copyright (c) 2004 - 2014, 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
72 return EFI_OUT_OF_RESOURCES;\r
73 }\r
74\r
75 NewMemoryFile->FileImage = InputFileImage;\r
76 NewMemoryFile->CurrentFilePointer = InputFileImage;\r
77 NewMemoryFile->Eof = InputFileImage + BytesRead;\r
78\r
79 *OutputMemoryFile = (EFI_HANDLE)NewMemoryFile;\r
80\r
81 CheckMemoryFileState (*OutputMemoryFile);\r
82\r
83 return EFI_SUCCESS;\r
84}\r
85\r
86\r
87EFI_STATUS\r
88FreeMemoryFile (\r
89 IN EFI_HANDLE InputMemoryFile\r
90 )\r
91/*++\r
92\r
93Routine Description:\r
94\r
95 Frees all memory associated with the input memory file.\r
96\r
97Arguments:\r
98\r
99 InputMemoryFile Handle to memory file\r
100\r
101Returns:\r
102\r
103 EFI_STATUS\r
104\r
105--*/\r
106{\r
107 MEMORY_FILE *MemoryFile;\r
108\r
109 CheckMemoryFileState (InputMemoryFile);\r
110\r
111 MemoryFile = (MEMORY_FILE*)InputMemoryFile;\r
112\r
113 free (MemoryFile->FileImage);\r
114\r
115 //\r
116 // Invalidate state of MEMORY_FILE structure to catch invalid usage.\r
117 //\r
118 memset (MemoryFile, 0xcc, sizeof (*MemoryFile));\r
119 MemoryFile->Eof -= 1;\r
120\r
121 free (MemoryFile);\r
122\r
123 return EFI_SUCCESS;\r
124}\r
125\r
126\r
127CHAR8 *\r
128ReadMemoryFileLine (\r
129 IN EFI_HANDLE InputMemoryFile\r
130 )\r
131/*++\r
132\r
133Routine Description:\r
134\r
135 This function reads a line from the memory file. The newline characters\r
136 are stripped and a null terminated string is returned.\r
137\r
138 If the string pointer returned is non-NULL, then the caller must free the\r
139 memory associated with this string.\r
140\r
141Arguments:\r
142\r
143 InputMemoryFile Handle to memory file\r
144\r
145Returns:\r
146\r
147 NULL if error or EOF\r
148 NULL character termincated string otherwise (MUST BE FREED BY CALLER)\r
149\r
150--*/\r
151{\r
152 CHAR8 *EndOfLine;\r
153 UINTN CharsToCopy;\r
154 MEMORY_FILE *InputFile;\r
155 UINTN BytesToEof;\r
156 CHAR8 *OutputString;\r
157\r
158 //\r
159 // Verify input parameters are not null\r
160 //\r
161 CheckMemoryFileState (InputMemoryFile);\r
162\r
163 InputFile = (MEMORY_FILE*)InputMemoryFile;\r
164\r
165 //\r
166 // Check for end of file condition\r
167 //\r
168 if (InputFile->CurrentFilePointer >= InputFile->Eof) {\r
169 return NULL;\r
170 }\r
171\r
172 //\r
173 // Determine the number of bytes remaining until the EOF\r
174 //\r
175 BytesToEof = InputFile->Eof - InputFile->CurrentFilePointer;\r
176\r
177 //\r
178 // Find the next newline char\r
179 //\r
180 EndOfLine = memchr (InputFile->CurrentFilePointer, '\n', BytesToEof);\r
181\r
182 //\r
183 // Determine the number of characters to copy.\r
184 //\r
185 if (EndOfLine == 0) {\r
186 //\r
187 // If no newline found, copy to the end of the file.\r
188 //\r
189 CharsToCopy = InputFile->Eof - InputFile->CurrentFilePointer;\r
190 } else {\r
191 //\r
192 // Newline found in the file.\r
193 //\r
194 CharsToCopy = EndOfLine - InputFile->CurrentFilePointer;\r
195 }\r
196\r
197 OutputString = malloc (CharsToCopy);\r
198 if (OutputString == NULL) {\r
199 return NULL;\r
200 }\r
201\r
202 //\r
203 // Copy the line.\r
204 //\r
205 memcpy (OutputString, InputFile->CurrentFilePointer, CharsToCopy);\r
206\r
207 //\r
208 // Add the null termination over the 0x0D\r
209 //\r
210 if (OutputString[CharsToCopy - 1] == '\r') {\r
211\r
212 OutputString[CharsToCopy - 1] = '\0';\r
213\r
214 } else {\r
215\r
216 OutputString[CharsToCopy] = '\0';\r
217\r
218 }\r
219\r
220 //\r
221 // Increment the current file pointer (include the 0x0A)\r
222 //\r
223 InputFile->CurrentFilePointer += CharsToCopy + 1;\r
224 CheckMemoryFileState (InputMemoryFile);\r
225\r
226 //\r
227 // Return the string\r
228 //\r
229 return OutputString;\r
230}\r
231\r
232\r
233STATIC\r
234VOID\r
235CheckMemoryFileState (\r
236 IN EFI_HANDLE InputMemoryFile\r
237 )\r
238{\r
239 MEMORY_FILE *MemoryFile;\r
240\r
241 assert (InputMemoryFile != NULL);\r
242\r
243 MemoryFile = (MEMORY_FILE*)InputMemoryFile;\r
244\r
245 assert (MemoryFile->FileImage != NULL);\r
246 assert (MemoryFile->CurrentFilePointer != NULL);\r
247 assert (MemoryFile->Eof != NULL);\r
248 assert (MemoryFile->Eof >= MemoryFile->FileImage);\r
249 assert (MemoryFile->CurrentFilePointer >= MemoryFile->FileImage);\r
250 assert (MemoryFile->CurrentFilePointer <= MemoryFile->Eof);\r
251}\r
252\r
253\r