]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/ParseInf.c
2 This contains some useful functions for parsing INF files.
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include "EfiUtilityMsgs.h"
15 #include "CommonLib.h"
19 IN MEMORY_FILE
*InputFile
,
20 IN OUT CHAR8
*InputBuffer
,
27 This function reads a line, stripping any comments.
28 The function reads a string from the input stream argument and stores it in
29 the input string. ReadLine reads characters from the current file position
30 to and including the first newline character, to the end of the stream, or
31 until the number of characters read is equal to MaxLength - 1, whichever
32 comes first. The newline character, if read, is replaced with a \0.
36 InputFile Memory file image.
37 InputBuffer Buffer to read into, must be MaxLength size.
38 MaxLength The maximum size of the input buffer.
52 // Verify input parameters are not null
55 assert (InputFile
->FileImage
);
56 assert (InputFile
->Eof
);
57 assert (InputFile
->CurrentFilePointer
);
60 // Check for end of file condition
62 if (InputFile
->CurrentFilePointer
>= InputFile
->Eof
) {
66 // Find the next newline char
68 EndOfLine
= strchr (InputFile
->CurrentFilePointer
, '\n');
71 // Determine the number of characters to copy.
75 // If no newline found, copy to the end of the file.
77 CharsToCopy
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
78 } else if (EndOfLine
>= InputFile
->Eof
) {
80 // If the newline found was beyond the end of file, copy to the eof.
82 CharsToCopy
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
85 // Newline found in the file.
87 CharsToCopy
= EndOfLine
- InputFile
->CurrentFilePointer
;
90 // If the end of line is too big for the current buffer, set it to the max
91 // size of the buffer (leaving room for the \0.
93 if (CharsToCopy
> MaxLength
- 1) {
94 CharsToCopy
= MaxLength
- 1;
99 memcpy (InputBuffer
, InputFile
->CurrentFilePointer
, CharsToCopy
);
102 // Add the null termination over the 0x0D
104 if (InputBuffer
[CharsToCopy
- 1] == '\r') {
106 InputBuffer
[CharsToCopy
- 1] = '\0';
110 InputBuffer
[CharsToCopy
] = '\0';
115 // Increment the current file pointer (include the 0x0A)
117 InputFile
->CurrentFilePointer
+= CharsToCopy
+ 1;
120 // Strip any comments
122 CharPtr
= strstr (InputBuffer
, "//");
134 IN MEMORY_FILE
*InputFile
,
141 This function parses a file from the beginning to find a section.
142 The section string may be anywhere within a line.
146 InputFile Memory file image.
147 Section Section to search for
151 FALSE if error or EOF
152 TRUE if section found
156 CHAR8 InputBuffer
[MAX_LONG_FILE_PATH
];
160 // Verify input is not NULL
162 assert (InputFile
->FileImage
);
163 assert (InputFile
->Eof
);
164 assert (InputFile
->CurrentFilePointer
);
168 // Rewind to beginning of file
170 InputFile
->CurrentFilePointer
= InputFile
->FileImage
;
173 // Read lines until the section is found
175 while (InputFile
->CurrentFilePointer
< InputFile
->Eof
) {
179 ReadLine (InputFile
, InputBuffer
, MAX_LONG_FILE_PATH
);
182 // Check if the section is found
184 CurrentToken
= strstr (InputBuffer
, Section
);
185 if (CurrentToken
!= NULL
) {
195 IN MEMORY_FILE
*InputFile
,
205 Finds a token value given the section and token to search for.
209 InputFile Memory file image.
210 Section The section to search for, a string within [].
211 Token The token to search for, e.g. EFI_PEIM_RECOVERY, followed by an = in the INF file.
212 Instance The instance of the token to search for. Zero is the first instance.
213 Value The string that holds the value following the =. Must be MAX_LONG_FILE_PATH in size.
217 EFI_SUCCESS Value found.
218 EFI_ABORTED Format error detected in INF file.
219 EFI_INVALID_PARAMETER Input argument was null.
220 EFI_LOAD_ERROR Error reading from the file.
221 EFI_NOT_FOUND Section/Token/Value not found.
225 CHAR8 InputBuffer
[MAX_LONG_FILE_PATH
];
233 // Check input parameters
235 if (InputFile
->FileImage
== NULL
||
236 InputFile
->Eof
== NULL
||
237 InputFile
->CurrentFilePointer
== NULL
||
239 strlen (Section
) == 0 ||
241 strlen (Token
) == 0 ||
244 return EFI_INVALID_PARAMETER
;
247 // Initialize error codes
253 // Initialize our instance counter for the search token
257 if (FindSection (InputFile
, Section
)) {
259 // Found the desired section, find and read the desired token
263 // Read a line from the file
265 if (ReadLine (InputFile
, InputBuffer
, MAX_LONG_FILE_PATH
) == NULL
) {
267 // Error reading from input file
273 // Get the first non-whitespace string
275 Delimiter
= strchr (InputBuffer
, '=');
276 if (Delimiter
!= NULL
) {
280 CurrentToken
= strtok (InputBuffer
, " \t\n");
281 if (CurrentToken
== NULL
|| Delimiter
== NULL
) {
283 // Whitespace line found (or comment) so continue
285 CurrentToken
= InputBuffer
;
289 // Make sure we have not reached the end of the current section
291 if (CurrentToken
[0] == '[') {
295 // Compare the current token with the desired token
297 if (strcmp (CurrentToken
, Token
) == 0) {
302 // Check if it is the correct instance
304 if (Instance
== Occurrence
) {
306 // Copy the contents following the =
308 CurrentToken
= Delimiter
+ 1;
309 if (*CurrentToken
== 0) {
311 // Nothing found, parsing error
316 // Strip leading white space
318 while (*CurrentToken
== ' ' || *CurrentToken
== '\t') {
322 // Copy the current token to the output value
324 strcpy (Value
, CurrentToken
);
326 // Strip trailing white space
328 while (strlen(Value
) > 0 && (*(Value
+ strlen(Value
) - 1) == ' ' || *(Value
+ strlen(Value
) - 1) == '\t')) {
329 *(Value
+ strlen(Value
) - 1) = 0;
335 // Increment the occurrence found
343 InputFile
->CurrentFilePointer
< InputFile
->Eof
&&
344 CurrentToken
[0] != '[' &&
345 Occurrence
<= Instance
349 // Distinguish between read errors and INF file format errors.
352 return EFI_LOAD_ERROR
;
359 return EFI_NOT_FOUND
;
364 IN CHAR8
*AsciiGuidBuffer
,
365 OUT EFI_GUID
*GuidBuffer
371 Converts a string to an EFI_GUID. The string must be in the
372 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format.
376 AsciiGuidBuffer - pointer to ascii string
377 GuidBuffer - pointer to destination Guid
381 EFI_ABORTED Could not convert the string
382 EFI_SUCCESS The string was successfully converted
383 EFI_INVALID_PARAMETER Input parameter is invalid.
393 if (AsciiGuidBuffer
== NULL
|| GuidBuffer
== NULL
) {
394 return EFI_INVALID_PARAMETER
;
397 // Check Guid Format strictly xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
399 for (Index
= 0; AsciiGuidBuffer
[Index
] != '\0' && Index
< 37; Index
++) {
400 if (Index
== 8 || Index
== 13 || Index
== 18 || Index
== 23) {
401 if (AsciiGuidBuffer
[Index
] != '-') {
405 if (((AsciiGuidBuffer
[Index
] >= '0') && (AsciiGuidBuffer
[Index
] <= '9')) ||
406 ((AsciiGuidBuffer
[Index
] >= 'a') && (AsciiGuidBuffer
[Index
] <= 'f')) ||
407 ((AsciiGuidBuffer
[Index
] >= 'A') && (AsciiGuidBuffer
[Index
] <= 'F'))) {
415 if (Index
< 36 || AsciiGuidBuffer
[36] != '\0') {
416 Error (NULL
, 0, 1003, "Invalid option value", "Incorrect GUID \"%s\"\n Correct Format \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"", AsciiGuidBuffer
);
421 // Scan the guid string into the buffer
425 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
440 // Verify the correct number of items were scanned.
443 Error (NULL
, 0, 1003, "Invalid option value", "Incorrect GUID \"%s\"\n Correct Format \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"", AsciiGuidBuffer
);
447 // Copy the data into our GUID.
449 GuidBuffer
->Data1
= (UINT32
) Data1
;
450 GuidBuffer
->Data2
= (UINT16
) Data2
;
451 GuidBuffer
->Data3
= (UINT16
) Data3
;
452 GuidBuffer
->Data4
[0] = (UINT8
) Data4
[0];
453 GuidBuffer
->Data4
[1] = (UINT8
) Data4
[1];
454 GuidBuffer
->Data4
[2] = (UINT8
) Data4
[2];
455 GuidBuffer
->Data4
[3] = (UINT8
) Data4
[3];
456 GuidBuffer
->Data4
[4] = (UINT8
) Data4
[4];
457 GuidBuffer
->Data4
[5] = (UINT8
) Data4
[5];
458 GuidBuffer
->Data4
[6] = (UINT8
) Data4
[6];
459 GuidBuffer
->Data4
[7] = (UINT8
) Data4
[7];
465 AsciiStringToUint64 (
466 IN CONST CHAR8
*AsciiString
,
468 OUT UINT64
*ReturnValue
474 Converts a null terminated ascii string that represents a number into a
475 UINT64 value. A hex number may be preceded by a 0x, but may not be
476 succeeded by an h. A number without 0x or 0X is considered to be base 10
477 unless the IsHex input is true.
481 AsciiString The string to convert.
482 IsHex Force the string to be treated as a hex number.
483 ReturnValue The return value.
487 EFI_SUCCESS Number successfully converted.
488 EFI_ABORTED Invalid character encountered.
497 // Initialize the result
503 // Check input parameter
505 if (AsciiString
== NULL
|| ReturnValue
== NULL
|| strlen(AsciiString
) > 0xFF) {
506 return EFI_INVALID_PARAMETER
;
508 while (AsciiString
[Index
] == ' ') {
513 // Add each character to the result
517 // Skip first two chars only if the string starts with '0x' or '0X'
519 if (AsciiString
[Index
] == '0' && (AsciiString
[Index
+ 1] == 'x' || AsciiString
[Index
+ 1] == 'X')) {
525 // Convert the hex string.
527 for (; AsciiString
[Index
] != '\0'; Index
++) {
528 CurrentChar
= AsciiString
[Index
];
529 if (CurrentChar
== ' ') {
535 if (isxdigit ((int)CurrentChar
) == 0) {
542 if (CurrentChar
>= '0' && CurrentChar
<= '9') {
543 Value
+= CurrentChar
- '0';
544 } else if (CurrentChar
>= 'a' && CurrentChar
<= 'f') {
545 Value
+= CurrentChar
- 'a' + 10;
546 } else if (CurrentChar
>= 'A' && CurrentChar
<= 'F') {
547 Value
+= CurrentChar
- 'A' + 10;
551 *ReturnValue
= Value
;
554 // Convert dec string is a number
556 for (; Index
< strlen (AsciiString
); Index
++) {
557 CurrentChar
= AsciiString
[Index
];
558 if (CurrentChar
== ' ') {
564 if (isdigit ((int)CurrentChar
) == 0) {
571 Value
+= CurrentChar
- '0';
574 *ReturnValue
= Value
;
583 IN OUT CHAR8
*InputBuffer
589 This function reads a line, stripping any comments.
590 // BUGBUG: This is obsolete once genmake goes away...
594 InputFile Stream pointer.
595 InputBuffer Buffer to read into, must be MAX_LONG_FILE_PATH size.
600 InputBuffer otherwise
607 // Verify input parameters are not null
610 assert (InputBuffer
);
615 if (fgets (InputBuffer
, MAX_LONG_FILE_PATH
, InputFile
) == NULL
) {
619 // Strip any comments
621 CharPtr
= strstr (InputBuffer
, "//");
626 CharPtr
= strstr (InputBuffer
, "#");
637 FindSectionInStream (
645 This function parses a stream file from the beginning to find a section.
646 The section string may be anywhere within a line.
647 // BUGBUG: This is obsolete once genmake goes away...
651 InputFile Stream pointer.
652 Section Section to search for
656 FALSE if error or EOF
657 TRUE if section found
661 CHAR8 InputBuffer
[MAX_LONG_FILE_PATH
];
665 // Verify input is not NULL
671 // Rewind to beginning of file
673 if (fseek (InputFile
, 0, SEEK_SET
) != 0) {
677 // Read lines until the section is found
679 while (feof (InputFile
) == 0) {
683 ReadLineInStream (InputFile
, InputBuffer
);
686 // Check if the section is found
688 CurrentToken
= strstr (InputBuffer
, Section
);
689 if (CurrentToken
!= NULL
) {