]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/Common/ParseInf.c
There is a limitation on WINDOWS OS for the length of entire file path can’t be large...
[mirror_edk2.git] / BaseTools / Source / C / Common / ParseInf.c
index b39c5bde4022527333b78ce2914b0e1952cb8452..00ae0e65a10b8aee9fa193689e79dc3c4c746558 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 \r
-Copyright (c) 2004 - 2008, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
+Copyright (c) 2004 - 2014, 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
@@ -25,6 +25,7 @@ Abstract:
 #include <stdlib.h>\r
 #include "EfiUtilityMsgs.h"\r
 #include "ParseInf.h"\r
+#include "CommonLib.h"\r
 \r
 CHAR8 *\r
 ReadLine (\r
@@ -46,7 +47,7 @@ Routine Description:
 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 +166,7 @@ Returns:
 \r
 --*/\r
 {\r
-  CHAR8 InputBuffer[_MAX_PATH];\r
+  CHAR8 InputBuffer[MAX_LONG_FILE_PATH];\r
   CHAR8 *CurrentToken;\r
 \r
   //\r
@@ -188,7 +189,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 +223,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,8 +235,9 @@ 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
@@ -273,7 +275,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 +285,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
@@ -311,17 +318,29 @@ Returns:
           //\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
@@ -484,13 +503,14 @@ Returns:
 --*/\r
 {\r
   UINT8   Index;\r
-  UINT64  HexNumber;\r
+  UINT64  Value;\r
   CHAR8   CurrentChar;\r
   \r
   //\r
   // Initialize the result\r
   //\r
-  HexNumber = 0;\r
+  Value = 0;\r
+  Index = 0;\r
   \r
   //\r
   // Check input paramter\r
@@ -498,50 +518,73 @@ Returns:
   if (AsciiString == NULL || ReturnValue == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
+  while (AsciiString[Index] == ' ') {\r
+    Index ++;\r
+  }\r
+  \r
   //\r
   // Add each character to the result\r
   //\r
-  if (IsHex || (AsciiString[0] == '0' && (AsciiString[1] == 'x' || AsciiString[1] == 'X'))) {\r
-    //\r
-    // Verify string is a hex number\r
-    //\r
-    for (Index = 2; Index < strlen (AsciiString); Index++) {\r
-      if (isxdigit (AsciiString[Index]) == 0) {\r
-        return EFI_ABORTED;\r
-      }\r
-    }\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 = 2; AsciiString[Index] != '\0'; Index++) {\r
+    for (; AsciiString[Index] != '\0'; Index++) {\r
       CurrentChar = AsciiString[Index];\r
-      HexNumber *= 16;\r
+      if (CurrentChar == ' ') {\r
+        break;\r
+      }\r
+      //\r
+      // Verify Hex string\r
+      //\r
+      if (isxdigit ((int)CurrentChar) == 0) {\r
+        return EFI_ABORTED;\r
+      }\r
+      //\r
+      // Add hex value\r
+      //\r
+      Value *= 16;\r
       if (CurrentChar >= '0' && CurrentChar <= '9') {\r
-        HexNumber += CurrentChar - '0';\r
+        Value += CurrentChar - '0';\r
       } else if (CurrentChar >= 'a' && CurrentChar <= 'f') {\r
-        HexNumber += CurrentChar - 'a' + 10;\r
+        Value += CurrentChar - 'a' + 10;\r
       } else if (CurrentChar >= 'A' && CurrentChar <= 'F') {\r
-        HexNumber += CurrentChar - 'A' + 10;\r
-      } else {\r
-        //\r
-        // Unrecognized character\r
-        //\r
-        return EFI_ABORTED;\r
+        Value += CurrentChar - 'A' + 10;\r
       }\r
     }\r
 \r
-    *ReturnValue = HexNumber;\r
+    *ReturnValue = Value;\r
   } else {\r
     //\r
-    // Verify string is a number\r
+    // Convert dec string is a number\r
     //\r
-    for (Index = 0; Index < strlen (AsciiString); Index++) {\r
-      if (isdigit (AsciiString[Index]) == 0) {\r
+    for (; Index < strlen (AsciiString); Index++) {\r
+      CurrentChar = AsciiString[Index];\r
+      if (CurrentChar == ' ') {\r
+        break;\r
+      }\r
+      //\r
+      // Verify Dec string\r
+      //\r
+      if (isdigit ((int)CurrentChar) == 0) {\r
         return EFI_ABORTED;\r
       }\r
+      //\r
+      // Add dec value\r
+      //\r
+      Value = Value * 10;\r
+      Value += CurrentChar - '0';\r
     }\r
 \r
-    *ReturnValue = atol (AsciiString);\r
+    *ReturnValue = Value;\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -562,7 +605,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
@@ -582,7 +625,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
@@ -628,7 +671,7 @@ Returns:
 \r
 --*/\r
 {\r
-  CHAR8 InputBuffer[_MAX_PATH];\r
+  CHAR8 InputBuffer[MAX_LONG_FILE_PATH];\r
   CHAR8 *CurrentToken;\r
 \r
   //\r