2 This contains some useful functions for accessing files.
4 Copyright (c) 2004 - 2016, 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 free (InputFileImage
);
73 return EFI_OUT_OF_RESOURCES
;
76 NewMemoryFile
->FileImage
= InputFileImage
;
77 NewMemoryFile
->CurrentFilePointer
= InputFileImage
;
78 NewMemoryFile
->Eof
= InputFileImage
+ BytesRead
;
80 *OutputMemoryFile
= (EFI_HANDLE
)NewMemoryFile
;
82 CheckMemoryFileState (*OutputMemoryFile
);
90 IN EFI_HANDLE InputMemoryFile
96 Frees all memory associated with the input memory file.
100 InputMemoryFile Handle to memory file
108 MEMORY_FILE
*MemoryFile
;
110 CheckMemoryFileState (InputMemoryFile
);
112 MemoryFile
= (MEMORY_FILE
*)InputMemoryFile
;
114 free (MemoryFile
->FileImage
);
117 // Invalidate state of MEMORY_FILE structure to catch invalid usage.
119 memset (MemoryFile
, 0xcc, sizeof (*MemoryFile
));
120 MemoryFile
->Eof
-= 1;
130 IN EFI_HANDLE InputMemoryFile
136 This function reads a line from the memory file. The newline characters
137 are stripped and a null terminated string is returned.
139 If the string pointer returned is non-NULL, then the caller must free the
140 memory associated with this string.
144 InputMemoryFile Handle to memory file
149 NULL character termincated string otherwise (MUST BE FREED BY CALLER)
155 MEMORY_FILE
*InputFile
;
160 // Verify input parameters are not null
162 CheckMemoryFileState (InputMemoryFile
);
164 InputFile
= (MEMORY_FILE
*)InputMemoryFile
;
167 // Check for end of file condition
169 if (InputFile
->CurrentFilePointer
>= InputFile
->Eof
) {
174 // Determine the number of bytes remaining until the EOF
176 BytesToEof
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
179 // Find the next newline char
181 EndOfLine
= memchr (InputFile
->CurrentFilePointer
, '\n', BytesToEof
);
184 // Determine the number of characters to copy.
186 if (EndOfLine
== 0) {
188 // If no newline found, copy to the end of the file.
190 CharsToCopy
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
193 // Newline found in the file.
195 CharsToCopy
= EndOfLine
- InputFile
->CurrentFilePointer
;
198 OutputString
= malloc (CharsToCopy
+ 1);
199 if (OutputString
== NULL
) {
206 memcpy (OutputString
, InputFile
->CurrentFilePointer
, CharsToCopy
);
209 // Add the null termination over the 0x0D
211 if (OutputString
[CharsToCopy
- 1] == '\r') {
213 OutputString
[CharsToCopy
- 1] = '\0';
217 OutputString
[CharsToCopy
] = '\0';
222 // Increment the current file pointer (include the 0x0A)
224 InputFile
->CurrentFilePointer
+= CharsToCopy
+ 1;
225 if (InputFile
->CurrentFilePointer
> InputFile
->Eof
) {
226 InputFile
->CurrentFilePointer
= InputFile
->Eof
;
228 CheckMemoryFileState (InputMemoryFile
);
239 CheckMemoryFileState (
240 IN EFI_HANDLE InputMemoryFile
243 MEMORY_FILE
*MemoryFile
;
245 assert (InputMemoryFile
!= NULL
);
247 MemoryFile
= (MEMORY_FILE
*)InputMemoryFile
;
249 assert (MemoryFile
->FileImage
!= NULL
);
250 assert (MemoryFile
->CurrentFilePointer
!= NULL
);
251 assert (MemoryFile
->Eof
!= NULL
);
252 assert (MemoryFile
->Eof
>= MemoryFile
->FileImage
);
253 assert (MemoryFile
->CurrentFilePointer
>= MemoryFile
->FileImage
);
254 assert (MemoryFile
->CurrentFilePointer
<= MemoryFile
->Eof
);