2 This contains some useful functions for accessing files.
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include "CommonLib.h"
14 #include "MemoryFile.h"
18 // Local (static) function prototypes
22 CheckMemoryFileState (
23 IN EFI_HANDLE InputMemoryFile
27 // Function implementations
32 IN CHAR8
*InputFileName
,
33 OUT EFI_HANDLE
*OutputMemoryFile
39 This opens a file, reads it into memory and returns a memory file
44 InputFile Memory file image.
45 OutputMemoryFile Handle to memory file
50 OutputMemoryFile is valid if !EFI_ERROR
55 CHAR8
*InputFileImage
;
57 MEMORY_FILE
*NewMemoryFile
;
59 Status
= GetFileImage (InputFileName
, &InputFileImage
, &BytesRead
);
60 if (EFI_ERROR (Status
)) {
64 NewMemoryFile
= malloc (sizeof (*NewMemoryFile
));
65 if (NewMemoryFile
== NULL
) {
66 free (InputFileImage
);
67 return EFI_OUT_OF_RESOURCES
;
70 NewMemoryFile
->FileImage
= InputFileImage
;
71 NewMemoryFile
->CurrentFilePointer
= InputFileImage
;
72 NewMemoryFile
->Eof
= InputFileImage
+ BytesRead
;
74 *OutputMemoryFile
= (EFI_HANDLE
)NewMemoryFile
;
76 CheckMemoryFileState (*OutputMemoryFile
);
84 IN EFI_HANDLE InputMemoryFile
90 Frees all memory associated with the input memory file.
94 InputMemoryFile Handle to memory file
102 MEMORY_FILE
*MemoryFile
;
104 CheckMemoryFileState (InputMemoryFile
);
106 MemoryFile
= (MEMORY_FILE
*)InputMemoryFile
;
108 free (MemoryFile
->FileImage
);
111 // Invalidate state of MEMORY_FILE structure to catch invalid usage.
113 memset (MemoryFile
, 0xcc, sizeof (*MemoryFile
));
114 MemoryFile
->Eof
-= 1;
124 IN EFI_HANDLE InputMemoryFile
130 This function reads a line from the memory file. The newline characters
131 are stripped and a null terminated string is returned.
133 If the string pointer returned is non-NULL, then the caller must free the
134 memory associated with this string.
138 InputMemoryFile Handle to memory file
143 NULL character termincated string otherwise (MUST BE FREED BY CALLER)
149 MEMORY_FILE
*InputFile
;
154 // Verify input parameters are not null
156 CheckMemoryFileState (InputMemoryFile
);
158 InputFile
= (MEMORY_FILE
*)InputMemoryFile
;
161 // Check for end of file condition
163 if (InputFile
->CurrentFilePointer
>= InputFile
->Eof
) {
168 // Determine the number of bytes remaining until the EOF
170 BytesToEof
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
173 // Find the next newline char
175 EndOfLine
= memchr (InputFile
->CurrentFilePointer
, '\n', BytesToEof
);
178 // Determine the number of characters to copy.
180 if (EndOfLine
== 0) {
182 // If no newline found, copy to the end of the file.
184 CharsToCopy
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
187 // Newline found in the file.
189 CharsToCopy
= EndOfLine
- InputFile
->CurrentFilePointer
;
192 OutputString
= malloc (CharsToCopy
+ 1);
193 if (OutputString
== NULL
) {
200 memcpy (OutputString
, InputFile
->CurrentFilePointer
, CharsToCopy
);
203 // Add the null termination over the 0x0D
205 if (OutputString
[CharsToCopy
- 1] == '\r') {
207 OutputString
[CharsToCopy
- 1] = '\0';
211 OutputString
[CharsToCopy
] = '\0';
216 // Increment the current file pointer (include the 0x0A)
218 InputFile
->CurrentFilePointer
+= CharsToCopy
+ 1;
219 if (InputFile
->CurrentFilePointer
> InputFile
->Eof
) {
220 InputFile
->CurrentFilePointer
= InputFile
->Eof
;
222 CheckMemoryFileState (InputMemoryFile
);
233 CheckMemoryFileState (
234 IN EFI_HANDLE InputMemoryFile
237 MEMORY_FILE
*MemoryFile
;
239 assert (InputMemoryFile
!= NULL
);
241 MemoryFile
= (MEMORY_FILE
*)InputMemoryFile
;
243 assert (MemoryFile
->FileImage
!= NULL
);
244 assert (MemoryFile
->CurrentFilePointer
!= NULL
);
245 assert (MemoryFile
->Eof
!= NULL
);
246 assert (MemoryFile
->Eof
>= MemoryFile
->FileImage
);
247 assert (MemoryFile
->CurrentFilePointer
>= MemoryFile
->FileImage
);
248 assert (MemoryFile
->CurrentFilePointer
<= MemoryFile
->Eof
);