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