]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/Common/CommonLib.c
BaseTools/CommonLib: get rid of 'native' type string parsing routines
[mirror_edk2.git] / BaseTools / Source / C / Common / CommonLib.c
index 15509e3453091eb1e4fe1685b4aee3c89fda403a..42dfa821624d4de40eda7b6fe1fdd7ffe5e59156 100644 (file)
@@ -2,13 +2,13 @@
 Common basic Library Functions\r
 \r
 Copyright (c) 2004 - 2018, 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
+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
 \r
 **/\r
 \r
@@ -136,7 +136,7 @@ Arguments:
 \r
 Returns:\r
   =  0  if Guid1 == Guid2\r
-  != 0  if Guid1 != Guid2 \r
+  != 0  if Guid1 != Guid2\r
 \r
 --*/\r
 {\r
@@ -169,7 +169,7 @@ GetFileImage (
 \r
 Routine Description:\r
 \r
-  This function opens a file and reads it into a memory buffer.  The function \r
+  This function opens a file and reads it into a memory buffer.  The function\r
   will allocate the memory buffer and returns the size of the buffer.\r
 \r
 Arguments:\r
@@ -332,7 +332,7 @@ CalculateChecksum8 (
   IN UINTN        Size\r
   )\r
 /*++\r
-  \r
+\r
 Routine Description:\r
 \r
   This function calculates the value needed for a valid UINT8 checksum\r
@@ -357,7 +357,7 @@ CalculateSum8 (
   IN UINTN  Size\r
   )\r
 /*++\r
-  \r
+\r
 Routine Description::\r
 \r
   This function calculates the UINT8 sum for the requested region.\r
@@ -394,7 +394,7 @@ CalculateChecksum16 (
   IN UINTN        Size\r
   )\r
 /*++\r
-  \r
+\r
 Routine Description::\r
 \r
   This function calculates the value needed for a valid UINT16 checksum\r
@@ -419,7 +419,7 @@ CalculateSum16 (
   IN UINTN        Size\r
   )\r
 /*++\r
-  \r
+\r
 Routine Description:\r
 \r
   This function calculates the UINT16 sum for the requested region.\r
@@ -518,7 +518,7 @@ Returns:
   EFI_SUCCESS             The GUID was printed.\r
   EFI_INVALID_PARAMETER   The input was NULL.\r
   EFI_BUFFER_TOO_SMALL    The input buffer was not big enough\r
-  \r
+\r
 --*/\r
 {\r
   if (Guid == NULL) {\r
@@ -593,7 +593,7 @@ char *strlwr(char *s)
 #define WINDOWS_UNC_EXTENSION_PATH "\\\\?\\UNC"\r
 \r
 //\r
-// Global data to store full file path. It is not required to be free. \r
+// Global data to store full file path. It is not required to be free.\r
 //\r
 CHAR8 mCommonLibFullPath[MAX_LONG_FILE_PATH];\r
 \r
@@ -604,32 +604,32 @@ LongFilePath (
 /*++\r
 \r
 Routine Description:\r
-  Convert FileName to the long file path, which can support larger than 260 length. \r
+  Convert FileName to the long file path, which can support larger than 260 length.\r
 \r
 Arguments:\r
-  FileName         - FileName. \r
+  FileName         - FileName.\r
 \r
 Returns:\r
   LongFilePath      A pointer to the converted long file path.\r
-  \r
+\r
 --*/\r
 {\r
 #ifdef __GNUC__\r
   //\r
-  // __GNUC__ may not be good way to differentiate unix and windows. Need more investigation here. \r
-  // unix has no limitation on file path. Just return FileName. \r
+  // __GNUC__ may not be good way to differentiate unix and windows. Need more investigation here.\r
+  // unix has no limitation on file path. Just return FileName.\r
   //\r
   return FileName;\r
 #else\r
   CHAR8 *RootPath;\r
   CHAR8 *PathPointer;\r
   CHAR8 *NextPointer;\r
-  \r
+\r
   PathPointer = (CHAR8 *) FileName;\r
-  \r
+\r
   if (FileName != NULL) {\r
     //\r
-    // Add the extension string first to support long file path. \r
+    // Add the extension string first to support long file path.\r
     //\r
     mCommonLibFullPath[0] = 0;\r
     strcpy (mCommonLibFullPath, WINDOWS_EXTENSION_PATH);\r
@@ -642,7 +642,7 @@ Returns:
       FileName ++;\r
     } else if (strlen (FileName) < 3 || FileName[1] != ':' || (FileName[2] != '\\' && FileName[2] != '/')) {\r
       //\r
-      // Relative file path. Convert it to absolute path. \r
+      // Relative file path. Convert it to absolute path.\r
       //\r
       RootPath = getcwd (NULL, 0);\r
       if (RootPath != NULL) {\r
@@ -675,7 +675,7 @@ Returns:
       return NULL;\r
     }\r
     strncat (mCommonLibFullPath, FileName, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);\r
-    \r
+\r
     //\r
     // Convert directory separator '/' to '\\'\r
     //\r
@@ -685,7 +685,7 @@ Returns:
         *PathPointer = '\\';\r
       }\r
     } while (*PathPointer ++ != '\0');\r
-    \r
+\r
     //\r
     // Convert ":\\\\" to ":\\", because it doesn't work with WINDOWS_EXTENSION_PATH.\r
     //\r
@@ -693,7 +693,7 @@ Returns:
       *(PathPointer + 2) = '\0';\r
       strncat (mCommonLibFullPath, PathPointer + 3, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);\r
     }\r
-    \r
+\r
     //\r
     // Convert ".\\" to "", because it doesn't work with WINDOWS_EXTENSION_PATH.\r
     //\r
@@ -701,7 +701,7 @@ Returns:
       *PathPointer = '\0';\r
       strncat (mCommonLibFullPath, PathPointer + 2, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);\r
     }\r
-        \r
+\r
     //\r
     // Convert "\\.\\" to "\\", because it doesn't work with WINDOWS_EXTENSION_PATH.\r
     //\r
@@ -732,10 +732,10 @@ Returns:
         break;\r
       }\r
     }\r
-    \r
+\r
     PathPointer = mCommonLibFullPath;\r
   }\r
-  \r
+\r
   return PathPointer;\r
 #endif\r
 }\r
@@ -882,72 +882,6 @@ InternalSafeStringNoStrOverlap (
   return !InternalSafeStringIsOverlap (Str1, Size1 * sizeof(CHAR16), Str2, Size2 * sizeof(CHAR16));\r
 }\r
 \r
-RETURN_STATUS\r
-StrDecimalToUintnS (\r
-    CONST CHAR16             *String,\r
-         CHAR16             **EndPointer,  OPTIONAL\r
-         UINTN              *Data\r
-  )\r
-{\r
-  ASSERT (((UINTN) String & BIT0) == 0);\r
-\r
-  //\r
-  // 1. Neither String nor Data shall be a null pointer.\r
-  //\r
-  SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);\r
-  SAFE_STRING_CONSTRAINT_CHECK ((Data != NULL), RETURN_INVALID_PARAMETER);\r
-\r
-  //\r
-  // 2. The length of String shall not be greater than RSIZE_MAX.\r
-  //\r
-  if (RSIZE_MAX != 0) {\r
-    SAFE_STRING_CONSTRAINT_CHECK ((StrnLenS (String, RSIZE_MAX + 1) <= RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
-  }\r
-\r
-  if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR16 *) String;\r
-  }\r
-\r
-  //\r
-  // Ignore the pad spaces (space or tab)\r
-  //\r
-  while ((*String == L' ') || (*String == L'\t')) {\r
-    String++;\r
-  }\r
-\r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == L'0') {\r
-    String++;\r
-  }\r
-\r
-  *Data = 0;\r
-\r
-  while (InternalIsDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the number represented by String overflows according to the range\r
-    // defined by UINTN, then MAX_UINTN is stored in *Data and\r
-    // RETURN_UNSUPPORTED is returned.\r
-    //\r
-    if (*Data > ((MAX_UINTN - (*String - L'0')) / 10)) {\r
-      *Data = MAX_UINTN;\r
-      if (EndPointer != NULL) {\r
-        *EndPointer = (CHAR16 *) String;\r
-      }\r
-      return RETURN_UNSUPPORTED;\r
-    }\r
-\r
-    *Data = *Data * 10 + (*String - L'0');\r
-    String++;\r
-  }\r
-\r
-  if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR16 *) String;\r
-  }\r
-  return RETURN_SUCCESS;\r
-}\r
-\r
 /**\r
   Convert a Null-terminated Unicode decimal string to a value of type UINT64.\r
 \r
@@ -1064,9 +998,9 @@ StrDecimalToUint64S (
 \r
 /**\r
   Convert a Null-terminated Unicode hexadecimal string to a value of type\r
-  UINTN.\r
+  UINT64.\r
 \r
-  This function outputs a value of type UINTN by interpreting the contents of\r
+  This function outputs a value of type UINT64 by interpreting the contents of\r
   the Unicode string specified by String as a hexadecimal number. The format of\r
   the input Unicode string String is:\r
 \r
@@ -1091,8 +1025,8 @@ StrDecimalToUint64S (
 \r
   If String has no valid hexadecimal digits in the above format, then 0 is\r
   stored at the location pointed to by Data.\r
-  If the number represented by String exceeds the range defined by UINTN, then\r
-  MAX_UINTN is stored at the location pointed to by Data.\r
+  If the number represented by String exceeds the range defined by UINT64, then\r
+  MAX_UINT64 is stored at the location pointed to by Data.\r
 \r
   If EndPointer is not NULL, a pointer to the character that stopped the scan\r
   is stored at the location pointed to by EndPointer. If String has no valid\r
@@ -1112,86 +1046,10 @@ StrDecimalToUint64S (
                                    characters, not including the\r
                                    Null-terminator.\r
   @retval RETURN_UNSUPPORTED       If the number represented by String exceeds\r
-                                   the range defined by UINTN.\r
+                                   the range defined by UINT64.\r
 \r
 **/\r
 RETURN_STATUS\r
-StrHexToUintnS (\r
-    CONST CHAR16             *String,\r
-         CHAR16             **EndPointer,  OPTIONAL\r
-         UINTN              *Data\r
-  )\r
-{\r
-  ASSERT (((UINTN) String & BIT0) == 0);\r
-\r
-  //\r
-  // 1. Neither String nor Data shall be a null pointer.\r
-  //\r
-  SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);\r
-  SAFE_STRING_CONSTRAINT_CHECK ((Data != NULL), RETURN_INVALID_PARAMETER);\r
-\r
-  //\r
-  // 2. The length of String shall not be greater than RSIZE_MAX.\r
-  //\r
-  if (RSIZE_MAX != 0) {\r
-    SAFE_STRING_CONSTRAINT_CHECK ((StrnLenS (String, RSIZE_MAX + 1) <= RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
-  }\r
-\r
-  if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR16 *) String;\r
-  }\r
-\r
-  //\r
-  // Ignore the pad spaces (space or tab)\r
-  //\r
-  while ((*String == L' ') || (*String == L'\t')) {\r
-    String++;\r
-  }\r
-\r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == L'0') {\r
-    String++;\r
-  }\r
-\r
-  if (InternalCharToUpper (*String) == L'X') {\r
-    if (*(String - 1) != L'0') {\r
-      *Data = 0;\r
-      return RETURN_SUCCESS;\r
-    }\r
-    //\r
-    // Skip the 'X'\r
-    //\r
-    String++;\r
-  }\r
-\r
-  *Data = 0;\r
-\r
-  while (InternalIsHexaDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the number represented by String overflows according to the range\r
-    // defined by UINTN, then MAX_UINTN is stored in *Data and\r
-    // RETURN_UNSUPPORTED is returned.\r
-    //\r
-    if (*Data > ((MAX_UINTN - InternalHexCharToUintn (*String)) >> 4)) {\r
-      *Data = MAX_UINTN;\r
-      if (EndPointer != NULL) {\r
-        *EndPointer = (CHAR16 *) String;\r
-      }\r
-      return RETURN_UNSUPPORTED;\r
-    }\r
-\r
-    *Data = (*Data << 4) + InternalHexCharToUintn (*String);\r
-    String++;\r
-  }\r
-\r
-  if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR16 *) String;\r
-  }\r
-  return RETURN_SUCCESS;\r
-}\r
-RETURN_STATUS\r
 StrHexToUint64S (\r
     CONST CHAR16             *String,\r
          CHAR16             **EndPointer,  OPTIONAL\r
@@ -1291,28 +1149,6 @@ StrHexToUint64 (
   return Result;\r
 }\r
 \r
-UINTN\r
-StrDecimalToUintn (\r
-  CONST CHAR16              *String\r
-  )\r
-{\r
-  UINTN     Result;\r
-\r
-  StrDecimalToUintnS (String, (CHAR16 **) NULL, &Result);\r
-  return Result;\r
-}\r
-\r
-UINTN\r
-StrHexToUintn (\r
-  CONST CHAR16              *String\r
-  )\r
-{\r
-  UINTN     Result;\r
-\r
-  StrHexToUintnS (String, (CHAR16 **) NULL, &Result);\r
-  return Result;\r
-}\r
-\r
 UINTN\r
 StrSize (\r
   CONST CHAR16              *String\r
@@ -1785,7 +1621,7 @@ StrToIpv4Address (
 {\r
   RETURN_STATUS          Status;\r
   UINTN                  AddressIndex;\r
-  UINTN                  Uintn;\r
+  UINT64                 Uint64;\r
   EFI_IPv4_ADDRESS       LocalAddress;\r
   UINT8                  LocalPrefixLength;\r
   CHAR16                 *Pointer;\r
@@ -1812,7 +1648,7 @@ StrToIpv4Address (
     //\r
     // Get D or P.\r
     //\r
-    Status = StrDecimalToUintnS ((CONST CHAR16 *) Pointer, &Pointer, &Uintn);\r
+    Status = StrDecimalToUint64S ((CONST CHAR16 *) Pointer, &Pointer, &Uint64);\r
     if (RETURN_ERROR (Status)) {\r
       return RETURN_UNSUPPORTED;\r
     }\r
@@ -1820,18 +1656,18 @@ StrToIpv4Address (
       //\r
       // It's P.\r
       //\r
-      if (Uintn > 32) {\r
+      if (Uint64 > 32) {\r
         return RETURN_UNSUPPORTED;\r
       }\r
-      LocalPrefixLength = (UINT8) Uintn;\r
+      LocalPrefixLength = (UINT8) Uint64;\r
     } else {\r
       //\r
       // It's D.\r
       //\r
-      if (Uintn > MAX_UINT8) {\r
+      if (Uint64 > MAX_UINT8) {\r
         return RETURN_UNSUPPORTED;\r
       }\r
-      LocalAddress.Addr[AddressIndex] = (UINT8) Uintn;\r
+      LocalAddress.Addr[AddressIndex] = (UINT8) Uint64;\r
       AddressIndex++;\r
     }\r
 \r
@@ -1888,7 +1724,7 @@ StrToIpv6Address (
 {\r
   RETURN_STATUS          Status;\r
   UINTN                  AddressIndex;\r
-  UINTN                  Uintn;\r
+  UINT64                 Uint64;\r
   EFI_IPv6_ADDRESS       LocalAddress;\r
   UINT8                  LocalPrefixLength;\r
   CONST CHAR16           *Pointer;\r
@@ -1969,7 +1805,7 @@ StrToIpv6Address (
         //\r
         // Get X.\r
         //\r
-        Status = StrHexToUintnS (Pointer, &End, &Uintn);\r
+        Status = StrHexToUint64S (Pointer, &End, &Uint64);\r
         if (RETURN_ERROR (Status) || End - Pointer > 4) {\r
           //\r
           // Number of hexadecimal digit characters is no more than 4.\r
@@ -1978,24 +1814,24 @@ StrToIpv6Address (
         }\r
         Pointer = End;\r
         //\r
-        // Uintn won't exceed MAX_UINT16 if number of hexadecimal digit characters is no more than 4.\r
+        // Uint64 won't exceed MAX_UINT16 if number of hexadecimal digit characters is no more than 4.\r
         //\r
         ASSERT (AddressIndex + 1 < ARRAY_SIZE (Address->Addr));\r
-        LocalAddress.Addr[AddressIndex] = (UINT8) ((UINT16) Uintn >> 8);\r
-        LocalAddress.Addr[AddressIndex + 1] = (UINT8) Uintn;\r
+        LocalAddress.Addr[AddressIndex] = (UINT8) ((UINT16) Uint64 >> 8);\r
+        LocalAddress.Addr[AddressIndex + 1] = (UINT8) Uint64;\r
         AddressIndex += 2;\r
       } else {\r
         //\r
         // Get P, then exit the loop.\r
         //\r
-        Status = StrDecimalToUintnS (Pointer, &End, &Uintn);\r
-        if (RETURN_ERROR (Status) || End == Pointer || Uintn > 128) {\r
+        Status = StrDecimalToUint64S (Pointer, &End, &Uint64);\r
+        if (RETURN_ERROR (Status) || End == Pointer || Uint64 > 128) {\r
           //\r
           // Prefix length should not exceed 128.\r
           //\r
           return RETURN_UNSUPPORTED;\r
         }\r
-        LocalPrefixLength = (UINT8) Uintn;\r
+        LocalPrefixLength = (UINT8) Uint64;\r
         Pointer = End;\r
         break;\r
       }\r
@@ -2252,9 +2088,9 @@ Strtoi (
   )\r
 {\r
   if (IsHexStr (Str)) {\r
-    return StrHexToUintn (Str);\r
+    return (UINTN)StrHexToUint64 (Str);\r
   } else {\r
-    return StrDecimalToUintn (Str);\r
+    return (UINTN)StrDecimalToUint64 (Str);\r
   }\r
 }\r
 \r