]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/Common/ParseInf.c
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / C / Common / ParseInf.c
index eb01d305275c097b61dc847b13ae768f087879f4..5ef8f1306d65d949b44c63cf9e91dd147337c7f0 100644 (file)
@@ -1,23 +1,10 @@
 /** @file\r
+This contains some useful functions for parsing INF files.\r
 \r
-Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
-Module Name:\r
-\r
-  ParseInf.c\r
-\r
-Abstract:\r
-\r
-  This contains some useful functions for parsing INF files.\r
-\r
---*/\r
+**/\r
 \r
 #include <assert.h>\r
 #include <string.h>\r
@@ -25,6 +12,7 @@ Abstract:
 #include <stdlib.h>\r
 #include "EfiUtilityMsgs.h"\r
 #include "ParseInf.h"\r
+#include "CommonLib.h"\r
 \r
 CHAR8 *\r
 ReadLine (\r
@@ -37,16 +25,16 @@ ReadLine (
 Routine Description:\r
 \r
   This function reads a line, stripping any comments.\r
-  The function reads a string from the input stream argument and stores it in \r
-  the input string. ReadLine reads characters from the current file position \r
-  to and including the first newline character, to the end of the stream, or \r
-  until the number of characters read is equal to MaxLength - 1, whichever \r
-  comes first.  The newline character, if read, is replaced with a \0. \r
+  The function reads a string from the input stream argument and stores it in\r
+  the input string. ReadLine reads characters from the current file position\r
+  to and including the first newline character, to the end of the stream, or\r
+  until the number of characters read is equal to MaxLength - 1, whichever\r
+  comes first.  The newline character, if read, is replaced with a \0.\r
 \r
 Arguments:\r
 \r
   InputFile     Memory file image.\r
-  InputBuffer   Buffer to read into, must be _MAX_PATH size.\r
+  InputBuffer   Buffer to read into, must be MaxLength size.\r
   MaxLength     The maximum size of the input buffer.\r
 \r
 Returns:\r
@@ -165,7 +153,7 @@ Returns:
 \r
 --*/\r
 {\r
-  CHAR8 InputBuffer[_MAX_PATH];\r
+  CHAR8 InputBuffer[MAX_LONG_FILE_PATH];\r
   CHAR8 *CurrentToken;\r
 \r
   //\r
@@ -188,7 +176,7 @@ Returns:
     //\r
     // Read a line\r
     //\r
-    ReadLine (InputFile, InputBuffer, _MAX_PATH);\r
+    ReadLine (InputFile, InputBuffer, MAX_LONG_FILE_PATH);\r
 \r
     //\r
     // Check if the section is found\r
@@ -222,7 +210,7 @@ Arguments:
   Section   The section to search for, a string within [].\r
   Token     The token to search for, e.g. EFI_PEIM_RECOVERY, followed by an = in the INF file.\r
   Instance  The instance of the token to search for.  Zero is the first instance.\r
-  Value     The string that holds the value following the =.  Must be _MAX_PATH in size.\r
+  Value     The string that holds the value following the =.  Must be MAX_LONG_FILE_PATH in size.\r
 \r
 Returns:\r
 \r
@@ -234,11 +222,12 @@ Returns:
 \r
 --*/\r
 {\r
-  CHAR8   InputBuffer[_MAX_PATH];\r
+  CHAR8   InputBuffer[MAX_LONG_FILE_PATH];\r
   CHAR8   *CurrentToken;\r
+  CHAR8   *Delimiter;\r
   BOOLEAN ParseError;\r
   BOOLEAN ReadError;\r
-  UINTN   Occurrance;\r
+  UINTN   Occurrence;\r
 \r
   //\r
   // Check input parameters\r
@@ -263,7 +252,7 @@ Returns:
   //\r
   // Initialize our instance counter for the search token\r
   //\r
-  Occurrance = 0;\r
+  Occurrence = 0;\r
 \r
   if (FindSection (InputFile, Section)) {\r
     //\r
@@ -273,7 +262,7 @@ Returns:
       //\r
       // Read a line from the file\r
       //\r
-      if (ReadLine (InputFile, InputBuffer, _MAX_PATH) == NULL) {\r
+      if (ReadLine (InputFile, InputBuffer, MAX_LONG_FILE_PATH) == NULL) {\r
         //\r
         // Error reading from input file\r
         //\r
@@ -283,8 +272,13 @@ Returns:
       //\r
       // Get the first non-whitespace string\r
       //\r
+      Delimiter = strchr (InputBuffer, '=');\r
+      if (Delimiter != NULL) {\r
+        *Delimiter = 0;\r
+      }\r
+\r
       CurrentToken = strtok (InputBuffer, " \t\n");\r
-      if (CurrentToken == NULL) {\r
+      if (CurrentToken == NULL || Delimiter == NULL) {\r
         //\r
         // Whitespace line found (or comment) so continue\r
         //\r
@@ -307,28 +301,40 @@ Returns:
         //\r
         // Check if it is the correct instance\r
         //\r
-        if (Instance == Occurrance) {\r
+        if (Instance == Occurrence) {\r
           //\r
           // Copy the contents following the =\r
           //\r
-          CurrentToken = strtok (NULL, "= \t\n");\r
-          if (CurrentToken == NULL) {\r
+          CurrentToken = Delimiter + 1;\r
+          if (*CurrentToken == 0) {\r
             //\r
             // Nothing found, parsing error\r
             //\r
             ParseError = TRUE;\r
           } else {\r
+            //\r
+            // Strip leading white space\r
+            //\r
+            while (*CurrentToken == ' ' || *CurrentToken == '\t') {\r
+              CurrentToken++;\r
+            }\r
             //\r
             // Copy the current token to the output value\r
             //\r
             strcpy (Value, CurrentToken);\r
+            //\r
+            // Strip trailing white space\r
+            //\r
+            while (strlen(Value) > 0 && (*(Value + strlen(Value) - 1) == ' ' || *(Value + strlen(Value) - 1) == '\t')) {\r
+              *(Value + strlen(Value) - 1) = 0;\r
+            }\r
             return EFI_SUCCESS;\r
           }\r
         } else {\r
           //\r
-          // Increment the occurrance found\r
+          // Increment the occurrence found\r
           //\r
-          Occurrance++;\r
+          Occurrence++;\r
         }\r
       }\r
     } while (\r
@@ -336,7 +342,7 @@ Returns:
       !ReadError &&\r
       InputFile->CurrentFilePointer < InputFile->Eof &&\r
       CurrentToken[0] != '[' &&\r
-      Occurrance <= Instance\r
+      Occurrence <= Instance\r
     );\r
   }\r
   //\r
@@ -360,17 +366,17 @@ StringToGuid (
   )\r
 /*++\r
 \r
-Routine Description: \r
+Routine Description:\r
 \r
-  Converts a string to an EFI_GUID.  The string must be in the \r
+  Converts a string to an EFI_GUID.  The string must be in the\r
   xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format.\r
 \r
-Arguments:  \r
+Arguments:\r
 \r
   AsciiGuidBuffer - pointer to ascii string\r
   GuidBuffer      - pointer to destination Guid\r
 \r
-Returns:  \r
+Returns:\r
 \r
   EFI_ABORTED             Could not convert the string\r
   EFI_SUCCESS             The string was successfully converted\r
@@ -379,10 +385,10 @@ Returns:
 --*/\r
 {\r
   INT32 Index;\r
-  unsigned Data1;\r
-  unsigned Data2;\r
-  unsigned Data3;\r
-  unsigned Data4[8];\r
+  int   Data1;\r
+  int   Data2;\r
+  int   Data3;\r
+  int   Data4[8];\r
 \r
   if (AsciiGuidBuffer == NULL || GuidBuffer == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -396,7 +402,7 @@ Returns:
         break;\r
       }\r
     } else {\r
-      if (((AsciiGuidBuffer[Index] >= '0') && (AsciiGuidBuffer[Index] <= '9')) || \r
+      if (((AsciiGuidBuffer[Index] >= '0') && (AsciiGuidBuffer[Index] <= '9')) ||\r
          ((AsciiGuidBuffer[Index] >= 'a') && (AsciiGuidBuffer[Index] <= 'f')) ||\r
          ((AsciiGuidBuffer[Index] >= 'A') && (AsciiGuidBuffer[Index] <= 'F'))) {\r
         continue;\r
@@ -405,12 +411,12 @@ Returns:
       }\r
     }\r
   }\r
-  \r
+\r
   if (Index < 36 || AsciiGuidBuffer[36] != '\0') {\r
     Error (NULL, 0, 1003, "Invalid option value", "Incorrect GUID \"%s\"\n  Correct Format \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"", AsciiGuidBuffer);\r
     return EFI_ABORTED;\r
   }\r
-  \r
+\r
   //\r
   // Scan the guid string into the buffer\r
   //\r
@@ -465,9 +471,9 @@ AsciiStringToUint64 (
 \r
 Routine Description:\r
 \r
-  Converts a null terminated ascii string that represents a number into a \r
-  UINT64 value.  A hex number may be preceeded by a 0x, but may not be \r
-  succeeded by an h.  A number without 0x or 0X is considered to be base 10 \r
+  Converts a null terminated ascii string that represents a number into a\r
+  UINT64 value.  A hex number may be preceded by a 0x, but may not be\r
+  succeeded by an h.  A number without 0x or 0X is considered to be base 10\r
   unless the IsHex input is true.\r
 \r
 Arguments:\r
@@ -486,31 +492,39 @@ Returns:
   UINT8   Index;\r
   UINT64  Value;\r
   CHAR8   CurrentChar;\r
-  \r
+\r
   //\r
   // Initialize the result\r
   //\r
   Value = 0;\r
   Index = 0;\r
-  \r
+\r
   //\r
-  // Check input paramter\r
+  // Check input parameter\r
   //\r
-  if (AsciiString == NULL || ReturnValue == NULL) {\r
+  if (AsciiString == NULL || ReturnValue == NULL || strlen(AsciiString) > 0xFF) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   while (AsciiString[Index] == ' ') {\r
     Index ++;\r
   }\r
-  \r
+\r
   //\r
   // Add each character to the result\r
   //\r
-  if (IsHex || (AsciiString[Index] == '0' && (AsciiString[Index + 1] == 'x' || AsciiString[Index + 1] == 'X'))) {\r
+\r
+  //\r
+  // Skip first two chars only if the string starts with '0x' or '0X'\r
+  //\r
+  if (AsciiString[Index] == '0' && (AsciiString[Index + 1] == 'x' || AsciiString[Index + 1] == 'X')) {\r
+    IsHex = TRUE;\r
+    Index += 2;\r
+  }\r
+  if (IsHex) {\r
     //\r
     // Convert the hex string.\r
     //\r
-    for (Index = Index + 2; AsciiString[Index] != '\0'; Index++) {\r
+    for (; AsciiString[Index] != '\0'; Index++) {\r
       CurrentChar = AsciiString[Index];\r
       if (CurrentChar == ' ') {\r
         break;\r
@@ -578,7 +592,7 @@ Routine Description:
 Arguments:\r
 \r
   InputFile     Stream pointer.\r
-  InputBuffer   Buffer to read into, must be _MAX_PATH size.\r
+  InputBuffer   Buffer to read into, must be MAX_LONG_FILE_PATH size.\r
 \r
 Returns:\r
 \r
@@ -598,7 +612,7 @@ Returns:
   //\r
   // Read a line\r
   //\r
-  if (fgets (InputBuffer, _MAX_PATH, InputFile) == NULL) {\r
+  if (fgets (InputBuffer, MAX_LONG_FILE_PATH, InputFile) == NULL) {\r
     return NULL;\r
   }\r
   //\r
@@ -644,7 +658,7 @@ Returns:
 \r
 --*/\r
 {\r
-  CHAR8 InputBuffer[_MAX_PATH];\r
+  CHAR8 InputBuffer[MAX_LONG_FILE_PATH];\r
   CHAR8 *CurrentToken;\r
 \r
   //\r