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