3 Copyright (c) 2004 - 2008, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 This contains some useful functions for accessing files.
26 #include "CommonLib.h"
27 #include "MemoryFile.h"
31 // Local (static) function prototypes
35 CheckMemoryFileState (
36 IN EFI_HANDLE InputMemoryFile
40 // Function implementations
45 IN CHAR8
*InputFileName
,
46 OUT EFI_HANDLE
*OutputMemoryFile
52 This opens a file, reads it into memory and returns a memory file
57 InputFile Memory file image.
58 OutputMemoryFile Handle to memory file
63 OutputMemoryFile is valid if !EFI_ERROR
68 CHAR8
*InputFileImage
;
70 MEMORY_FILE
*NewMemoryFile
;
72 Status
= GetFileImage (InputFileName
, &InputFileImage
, &BytesRead
);
73 if (EFI_ERROR (Status
)) {
77 NewMemoryFile
= malloc (sizeof (*NewMemoryFile
));
78 if (NewMemoryFile
== NULL
) {
79 return EFI_OUT_OF_RESOURCES
;
82 NewMemoryFile
->FileImage
= InputFileImage
;
83 NewMemoryFile
->CurrentFilePointer
= InputFileImage
;
84 NewMemoryFile
->Eof
= InputFileImage
+ BytesRead
;
86 *OutputMemoryFile
= (EFI_HANDLE
)NewMemoryFile
;
88 CheckMemoryFileState (*OutputMemoryFile
);
96 IN EFI_HANDLE InputMemoryFile
102 Frees all memory associated with the input memory file.
106 InputMemoryFile Handle to memory file
114 MEMORY_FILE
*MemoryFile
;
116 CheckMemoryFileState (InputMemoryFile
);
118 MemoryFile
= (MEMORY_FILE
*)InputMemoryFile
;
120 free (MemoryFile
->FileImage
);
123 // Invalidate state of MEMORY_FILE structure to catch invalid usage.
125 memset (MemoryFile
, 0xcc, sizeof (*MemoryFile
));
126 MemoryFile
->Eof
-= 1;
136 IN EFI_HANDLE InputMemoryFile
142 This function reads a line from the memory file. The newline characters
143 are stripped and a null terminated string is returned.
145 If the string pointer returned is non-NULL, then the caller must free the
146 memory associated with this string.
150 InputMemoryFile Handle to memory file
155 NULL character termincated string otherwise (MUST BE FREED BY CALLER)
161 MEMORY_FILE
*InputFile
;
166 // Verify input parameters are not null
168 CheckMemoryFileState (InputMemoryFile
);
170 InputFile
= (MEMORY_FILE
*)InputMemoryFile
;
173 // Check for end of file condition
175 if (InputFile
->CurrentFilePointer
>= InputFile
->Eof
) {
180 // Determine the number of bytes remaining until the EOF
182 BytesToEof
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
185 // Find the next newline char
187 EndOfLine
= memchr (InputFile
->CurrentFilePointer
, '\n', BytesToEof
);
190 // Determine the number of characters to copy.
192 if (EndOfLine
== 0) {
194 // If no newline found, copy to the end of the file.
196 CharsToCopy
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
199 // Newline found in the file.
201 CharsToCopy
= EndOfLine
- InputFile
->CurrentFilePointer
;
204 OutputString
= malloc (CharsToCopy
);
205 if (OutputString
== NULL
) {
212 memcpy (OutputString
, InputFile
->CurrentFilePointer
, CharsToCopy
);
215 // Add the null termination over the 0x0D
217 if (OutputString
[CharsToCopy
- 1] == '\r') {
219 OutputString
[CharsToCopy
- 1] = '\0';
223 OutputString
[CharsToCopy
] = '\0';
228 // Increment the current file pointer (include the 0x0A)
230 InputFile
->CurrentFilePointer
+= CharsToCopy
+ 1;
231 CheckMemoryFileState (InputMemoryFile
);
242 CheckMemoryFileState (
243 IN EFI_HANDLE InputMemoryFile
246 MEMORY_FILE
*MemoryFile
;
248 assert (InputMemoryFile
!= NULL
);
250 MemoryFile
= (MEMORY_FILE
*)InputMemoryFile
;
252 assert (MemoryFile
->FileImage
!= NULL
);
253 assert (MemoryFile
->CurrentFilePointer
!= NULL
);
254 assert (MemoryFile
->Eof
!= NULL
);
255 assert (MemoryFile
->Eof
>= MemoryFile
->FileImage
);
256 assert (MemoryFile
->CurrentFilePointer
>= MemoryFile
->FileImage
);
257 assert (MemoryFile
->CurrentFilePointer
<= MemoryFile
->Eof
);