2 This contains some useful functions for accessing files.
4 Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include "CommonLib.h"
20 #include "MemoryFile.h"
24 // Local (static) function prototypes
28 CheckMemoryFileState (
29 IN EFI_HANDLE InputMemoryFile
33 // Function implementations
38 IN CHAR8
*InputFileName
,
39 OUT EFI_HANDLE
*OutputMemoryFile
45 This opens a file, reads it into memory and returns a memory file
50 InputFile Memory file image.
51 OutputMemoryFile Handle to memory file
56 OutputMemoryFile is valid if !EFI_ERROR
61 CHAR8
*InputFileImage
;
63 MEMORY_FILE
*NewMemoryFile
;
65 Status
= GetFileImage (InputFileName
, &InputFileImage
, &BytesRead
);
66 if (EFI_ERROR (Status
)) {
70 NewMemoryFile
= malloc (sizeof (*NewMemoryFile
));
71 if (NewMemoryFile
== NULL
) {
72 return EFI_OUT_OF_RESOURCES
;
75 NewMemoryFile
->FileImage
= InputFileImage
;
76 NewMemoryFile
->CurrentFilePointer
= InputFileImage
;
77 NewMemoryFile
->Eof
= InputFileImage
+ BytesRead
;
79 *OutputMemoryFile
= (EFI_HANDLE
)NewMemoryFile
;
81 CheckMemoryFileState (*OutputMemoryFile
);
89 IN EFI_HANDLE InputMemoryFile
95 Frees all memory associated with the input memory file.
99 InputMemoryFile Handle to memory file
107 MEMORY_FILE
*MemoryFile
;
109 CheckMemoryFileState (InputMemoryFile
);
111 MemoryFile
= (MEMORY_FILE
*)InputMemoryFile
;
113 free (MemoryFile
->FileImage
);
116 // Invalidate state of MEMORY_FILE structure to catch invalid usage.
118 memset (MemoryFile
, 0xcc, sizeof (*MemoryFile
));
119 MemoryFile
->Eof
-= 1;
129 IN EFI_HANDLE InputMemoryFile
135 This function reads a line from the memory file. The newline characters
136 are stripped and a null terminated string is returned.
138 If the string pointer returned is non-NULL, then the caller must free the
139 memory associated with this string.
143 InputMemoryFile Handle to memory file
148 NULL character termincated string otherwise (MUST BE FREED BY CALLER)
154 MEMORY_FILE
*InputFile
;
159 // Verify input parameters are not null
161 CheckMemoryFileState (InputMemoryFile
);
163 InputFile
= (MEMORY_FILE
*)InputMemoryFile
;
166 // Check for end of file condition
168 if (InputFile
->CurrentFilePointer
>= InputFile
->Eof
) {
173 // Determine the number of bytes remaining until the EOF
175 BytesToEof
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
178 // Find the next newline char
180 EndOfLine
= memchr (InputFile
->CurrentFilePointer
, '\n', BytesToEof
);
183 // Determine the number of characters to copy.
185 if (EndOfLine
== 0) {
187 // If no newline found, copy to the end of the file.
189 CharsToCopy
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
192 // Newline found in the file.
194 CharsToCopy
= EndOfLine
- InputFile
->CurrentFilePointer
;
197 OutputString
= malloc (CharsToCopy
);
198 if (OutputString
== NULL
) {
205 memcpy (OutputString
, InputFile
->CurrentFilePointer
, CharsToCopy
);
208 // Add the null termination over the 0x0D
210 if (OutputString
[CharsToCopy
- 1] == '\r') {
212 OutputString
[CharsToCopy
- 1] = '\0';
216 OutputString
[CharsToCopy
] = '\0';
221 // Increment the current file pointer (include the 0x0A)
223 InputFile
->CurrentFilePointer
+= CharsToCopy
+ 1;
224 CheckMemoryFileState (InputMemoryFile
);
235 CheckMemoryFileState (
236 IN EFI_HANDLE InputMemoryFile
239 MEMORY_FILE
*MemoryFile
;
241 assert (InputMemoryFile
!= NULL
);
243 MemoryFile
= (MEMORY_FILE
*)InputMemoryFile
;
245 assert (MemoryFile
->FileImage
!= NULL
);
246 assert (MemoryFile
->CurrentFilePointer
!= NULL
);
247 assert (MemoryFile
->Eof
!= NULL
);
248 assert (MemoryFile
->Eof
>= MemoryFile
->FileImage
);
249 assert (MemoryFile
->CurrentFilePointer
>= MemoryFile
->FileImage
);
250 assert (MemoryFile
->CurrentFilePointer
<= MemoryFile
->Eof
);