]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/ParseInf.c
385758f8366f5412b8f9a93510e84a9e13f49dde
3 Copyright (c) 2004 - 2010, 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 parsing INF files.
26 #include "EfiUtilityMsgs.h"
31 IN MEMORY_FILE
*InputFile
,
32 IN OUT CHAR8
*InputBuffer
,
39 This function reads a line, stripping any comments.
40 The function reads a string from the input stream argument and stores it in
41 the input string. ReadLine reads characters from the current file position
42 to and including the first newline character, to the end of the stream, or
43 until the number of characters read is equal to MaxLength - 1, whichever
44 comes first. The newline character, if read, is replaced with a \0.
48 InputFile Memory file image.
49 InputBuffer Buffer to read into, must be _MAX_PATH size.
50 MaxLength The maximum size of the input buffer.
64 // Verify input parameters are not null
67 assert (InputFile
->FileImage
);
68 assert (InputFile
->Eof
);
69 assert (InputFile
->CurrentFilePointer
);
72 // Check for end of file condition
74 if (InputFile
->CurrentFilePointer
>= InputFile
->Eof
) {
78 // Find the next newline char
80 EndOfLine
= strchr (InputFile
->CurrentFilePointer
, '\n');
83 // Determine the number of characters to copy.
87 // If no newline found, copy to the end of the file.
89 CharsToCopy
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
90 } else if (EndOfLine
>= InputFile
->Eof
) {
92 // If the newline found was beyond the end of file, copy to the eof.
94 CharsToCopy
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
97 // Newline found in the file.
99 CharsToCopy
= EndOfLine
- InputFile
->CurrentFilePointer
;
102 // If the end of line is too big for the current buffer, set it to the max
103 // size of the buffer (leaving room for the \0.
105 if (CharsToCopy
> MaxLength
- 1) {
106 CharsToCopy
= MaxLength
- 1;
111 memcpy (InputBuffer
, InputFile
->CurrentFilePointer
, CharsToCopy
);
114 // Add the null termination over the 0x0D
116 if (InputBuffer
[CharsToCopy
- 1] == '\r') {
118 InputBuffer
[CharsToCopy
- 1] = '\0';
122 InputBuffer
[CharsToCopy
] = '\0';
127 // Increment the current file pointer (include the 0x0A)
129 InputFile
->CurrentFilePointer
+= CharsToCopy
+ 1;
132 // Strip any comments
134 CharPtr
= strstr (InputBuffer
, "//");
146 IN MEMORY_FILE
*InputFile
,
153 This function parses a file from the beginning to find a section.
154 The section string may be anywhere within a line.
158 InputFile Memory file image.
159 Section Section to search for
163 FALSE if error or EOF
164 TRUE if section found
168 CHAR8 InputBuffer
[_MAX_PATH
];
172 // Verify input is not NULL
174 assert (InputFile
->FileImage
);
175 assert (InputFile
->Eof
);
176 assert (InputFile
->CurrentFilePointer
);
180 // Rewind to beginning of file
182 InputFile
->CurrentFilePointer
= InputFile
->FileImage
;
185 // Read lines until the section is found
187 while (InputFile
->CurrentFilePointer
< InputFile
->Eof
) {
191 ReadLine (InputFile
, InputBuffer
, _MAX_PATH
);
194 // Check if the section is found
196 CurrentToken
= strstr (InputBuffer
, Section
);
197 if (CurrentToken
!= NULL
) {
207 IN MEMORY_FILE
*InputFile
,
217 Finds a token value given the section and token to search for.
221 InputFile Memory file image.
222 Section The section to search for, a string within [].
223 Token The token to search for, e.g. EFI_PEIM_RECOVERY, followed by an = in the INF file.
224 Instance The instance of the token to search for. Zero is the first instance.
225 Value The string that holds the value following the =. Must be _MAX_PATH in size.
229 EFI_SUCCESS Value found.
230 EFI_ABORTED Format error detected in INF file.
231 EFI_INVALID_PARAMETER Input argument was null.
232 EFI_LOAD_ERROR Error reading from the file.
233 EFI_NOT_FOUND Section/Token/Value not found.
237 CHAR8 InputBuffer
[_MAX_PATH
];
245 // Check input parameters
247 if (InputFile
->FileImage
== NULL
||
248 InputFile
->Eof
== NULL
||
249 InputFile
->CurrentFilePointer
== NULL
||
251 strlen (Section
) == 0 ||
253 strlen (Token
) == 0 ||
256 return EFI_INVALID_PARAMETER
;
259 // Initialize error codes
265 // Initialize our instance counter for the search token
269 if (FindSection (InputFile
, Section
)) {
271 // Found the desired section, find and read the desired token
275 // Read a line from the file
277 if (ReadLine (InputFile
, InputBuffer
, _MAX_PATH
) == NULL
) {
279 // Error reading from input file
285 // Get the first non-whitespace string
287 Delimiter
= strchr (InputBuffer
, '=');
288 if (Delimiter
!= NULL
) {
292 CurrentToken
= strtok (InputBuffer
, " \t\n");
293 if (CurrentToken
== NULL
|| Delimiter
== NULL
) {
295 // Whitespace line found (or comment) so continue
297 CurrentToken
= InputBuffer
;
301 // Make sure we have not reached the end of the current section
303 if (CurrentToken
[0] == '[') {
307 // Compare the current token with the desired token
309 if (strcmp (CurrentToken
, Token
) == 0) {
314 // Check if it is the correct instance
316 if (Instance
== Occurrance
) {
318 // Copy the contents following the =
320 CurrentToken
= Delimiter
+ 1;
321 if (*CurrentToken
== 0) {
323 // Nothing found, parsing error
328 // Strip leading white space
330 while (*CurrentToken
== ' ' || *CurrentToken
== '\t') {
334 // Copy the current token to the output value
336 strcpy (Value
, CurrentToken
);
338 // Strip trailing white space
340 while (strlen(Value
) > 0 && (*(Value
+ strlen(Value
) - 1) == ' ' || *(Value
+ strlen(Value
) - 1) == '\t')) {
341 *(Value
+ strlen(Value
) - 1) = 0;
347 // Increment the occurrance found
355 InputFile
->CurrentFilePointer
< InputFile
->Eof
&&
356 CurrentToken
[0] != '[' &&
357 Occurrance
<= Instance
361 // Distinguish between read errors and INF file format errors.
364 return EFI_LOAD_ERROR
;
371 return EFI_NOT_FOUND
;
376 IN CHAR8
*AsciiGuidBuffer
,
377 OUT EFI_GUID
*GuidBuffer
383 Converts a string to an EFI_GUID. The string must be in the
384 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format.
388 AsciiGuidBuffer - pointer to ascii string
389 GuidBuffer - pointer to destination Guid
393 EFI_ABORTED Could not convert the string
394 EFI_SUCCESS The string was successfully converted
395 EFI_INVALID_PARAMETER Input parameter is invalid.
405 if (AsciiGuidBuffer
== NULL
|| GuidBuffer
== NULL
) {
406 return EFI_INVALID_PARAMETER
;
409 // Check Guid Format strictly xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
411 for (Index
= 0; AsciiGuidBuffer
[Index
] != '\0' && Index
< 37; Index
++) {
412 if (Index
== 8 || Index
== 13 || Index
== 18 || Index
== 23) {
413 if (AsciiGuidBuffer
[Index
] != '-') {
417 if (((AsciiGuidBuffer
[Index
] >= '0') && (AsciiGuidBuffer
[Index
] <= '9')) ||
418 ((AsciiGuidBuffer
[Index
] >= 'a') && (AsciiGuidBuffer
[Index
] <= 'f')) ||
419 ((AsciiGuidBuffer
[Index
] >= 'A') && (AsciiGuidBuffer
[Index
] <= 'F'))) {
427 if (Index
< 36 || AsciiGuidBuffer
[36] != '\0') {
428 Error (NULL
, 0, 1003, "Invalid option value", "Incorrect GUID \"%s\"\n Correct Format \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"", AsciiGuidBuffer
);
433 // Scan the guid string into the buffer
437 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
452 // Verify the correct number of items were scanned.
455 Error (NULL
, 0, 1003, "Invalid option value", "Incorrect GUID \"%s\"\n Correct Format \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"", AsciiGuidBuffer
);
459 // Copy the data into our GUID.
461 GuidBuffer
->Data1
= (UINT32
) Data1
;
462 GuidBuffer
->Data2
= (UINT16
) Data2
;
463 GuidBuffer
->Data3
= (UINT16
) Data3
;
464 GuidBuffer
->Data4
[0] = (UINT8
) Data4
[0];
465 GuidBuffer
->Data4
[1] = (UINT8
) Data4
[1];
466 GuidBuffer
->Data4
[2] = (UINT8
) Data4
[2];
467 GuidBuffer
->Data4
[3] = (UINT8
) Data4
[3];
468 GuidBuffer
->Data4
[4] = (UINT8
) Data4
[4];
469 GuidBuffer
->Data4
[5] = (UINT8
) Data4
[5];
470 GuidBuffer
->Data4
[6] = (UINT8
) Data4
[6];
471 GuidBuffer
->Data4
[7] = (UINT8
) Data4
[7];
477 AsciiStringToUint64 (
478 IN CONST CHAR8
*AsciiString
,
480 OUT UINT64
*ReturnValue
486 Converts a null terminated ascii string that represents a number into a
487 UINT64 value. A hex number may be preceeded by a 0x, but may not be
488 succeeded by an h. A number without 0x or 0X is considered to be base 10
489 unless the IsHex input is true.
493 AsciiString The string to convert.
494 IsHex Force the string to be treated as a hex number.
495 ReturnValue The return value.
499 EFI_SUCCESS Number successfully converted.
500 EFI_ABORTED Invalid character encountered.
509 // Initialize the result
515 // Check input paramter
517 if (AsciiString
== NULL
|| ReturnValue
== NULL
) {
518 return EFI_INVALID_PARAMETER
;
520 while (AsciiString
[Index
] == ' ') {
525 // Add each character to the result
529 // Skip first two chars only if the string starts with '0x' or '0X'
531 if (AsciiString
[Index
] == '0' && (AsciiString
[Index
+ 1] == 'x' || AsciiString
[Index
+ 1] == 'X')) {
537 // Convert the hex string.
539 for (; AsciiString
[Index
] != '\0'; Index
++) {
540 CurrentChar
= AsciiString
[Index
];
541 if (CurrentChar
== ' ') {
547 if (isxdigit ((int)CurrentChar
) == 0) {
554 if (CurrentChar
>= '0' && CurrentChar
<= '9') {
555 Value
+= CurrentChar
- '0';
556 } else if (CurrentChar
>= 'a' && CurrentChar
<= 'f') {
557 Value
+= CurrentChar
- 'a' + 10;
558 } else if (CurrentChar
>= 'A' && CurrentChar
<= 'F') {
559 Value
+= CurrentChar
- 'A' + 10;
563 *ReturnValue
= Value
;
566 // Convert dec string is a number
568 for (; Index
< strlen (AsciiString
); Index
++) {
569 CurrentChar
= AsciiString
[Index
];
570 if (CurrentChar
== ' ') {
576 if (isdigit ((int)CurrentChar
) == 0) {
583 Value
+= CurrentChar
- '0';
586 *ReturnValue
= Value
;
595 IN OUT CHAR8
*InputBuffer
601 This function reads a line, stripping any comments.
602 // BUGBUG: This is obsolete once genmake goes away...
606 InputFile Stream pointer.
607 InputBuffer Buffer to read into, must be _MAX_PATH size.
612 InputBuffer otherwise
619 // Verify input parameters are not null
622 assert (InputBuffer
);
627 if (fgets (InputBuffer
, _MAX_PATH
, InputFile
) == NULL
) {
631 // Strip any comments
633 CharPtr
= strstr (InputBuffer
, "//");
638 CharPtr
= strstr (InputBuffer
, "#");
649 FindSectionInStream (
657 This function parses a stream file from the beginning to find a section.
658 The section string may be anywhere within a line.
659 // BUGBUG: This is obsolete once genmake goes away...
663 InputFile Stream pointer.
664 Section Section to search for
668 FALSE if error or EOF
669 TRUE if section found
673 CHAR8 InputBuffer
[_MAX_PATH
];
677 // Verify input is not NULL
683 // Rewind to beginning of file
685 if (fseek (InputFile
, 0, SEEK_SET
) != 0) {
689 // Read lines until the section is found
691 while (feof (InputFile
) == 0) {
695 ReadLineInStream (InputFile
, InputBuffer
);
698 // Check if the section is found
700 CurrentToken
= strstr (InputBuffer
, Section
);
701 if (CurrentToken
!= NULL
) {