]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/String.c
MdePkg/BaseLib: Add bit field population calculating methods
[mirror_edk2.git] / MdePkg / Library / BaseLib / String.c
index afbad1d4e3277a5290569f41a0fd2c1c68ce1dd3..e7fe513aeca185d02dd6fcb2036fe1e34250dab6 100644 (file)
@@ -1,11 +1,11 @@
 /** @file\r
-  Unicode and ASCII string primatives.\r
+  Unicode and ASCII string primitives.\r
 \r
-  Copyright (c) 2006 - 2009, Intel Corporation<BR>\r
-  All rights reserved. This program and the accompanying materials\r
+  Copyright (c) 2006 - 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
+  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
 #include "BaseLibInternals.h"\r
 \r
-#define QUOTIENT_MAX_UINTN_DIVIDED_BY_10      ((UINTN) -1 / 10)\r
-#define REMAINDER_MAX_UINTN_DIVIDED_BY_10    ((UINTN) -1 % 10)\r
-\r
-#define QUOTIENT_MAX_UINTN_DIVIDED_BY_16      ((UINTN) -1 / 16)\r
-#define REMAINDER_MAX_UINTN_DIVIDED_BY_16    ((UINTN) -1 % 16)\r
-\r
-#define QUOTIENT_MAX_UINT64_DIVIDED_BY_10      ((UINT64) -1 / 10)\r
-#define REMAINDER_MAX_UINT64_DIVIDED_BY_10    ((UINT64) -1 % 10)\r
-\r
-#define QUOTIENT_MAX_UINT64_DIVIDED_BY_16      ((UINT64) -1 / 16)\r
-#define REMAINDER_MAX_UINT64_DIVIDED_BY_16    ((UINT64) -1 % 16)\r
+#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
 \r
 /**\r
+  [ATTENTION] This function will be deprecated for security reason.\r
+\r
   Copies one Null-terminated Unicode string to another Null-terminated Unicode\r
   string and returns the new Unicode string.\r
 \r
@@ -43,8 +35,8 @@
   PcdMaximumUnicodeStringLength Unicode characters, not including the\r
   Null-terminator, then ASSERT().\r
 \r
-  @param  Destination Pointer to a Null-terminated Unicode string.\r
-  @param  Source      Pointer to a Null-terminated Unicode string.\r
+  @param  Destination A pointer to a Null-terminated Unicode string.\r
+  @param  Source      A pointer to a Null-terminated Unicode string.\r
 \r
   @return Destination.\r
 \r
@@ -79,7 +71,9 @@ StrCpy (
 }\r
 \r
 /**\r
-  Copies up to a specified length from one Null-terminated Unicode string  to \r
+  [ATTENTION] This function will be deprecated for security reason.\r
+\r
+  Copies up to a specified length from one Null-terminated Unicode string  to\r
   another Null-terminated Unicode string and returns the new Unicode string.\r
 \r
   This function copies the contents of the Unicode string Source to the Unicode\r
@@ -95,15 +89,15 @@ StrCpy (
   If Length > 0 and Source is NULL, then ASSERT().\r
   If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().\r
   If Source and Destination overlap, then ASSERT().\r
-  If PcdMaximumUnicodeStringLength is not zero, and Length is greater than \r
+  If PcdMaximumUnicodeStringLength is not zero, and Length is greater than\r
   PcdMaximumUnicodeStringLength, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
   then ASSERT().\r
 \r
-  @param  Destination Pointer to a Null-terminated Unicode string.\r
-  @param  Source      Pointer to a Null-terminated Unicode string.\r
-  @param  Length      Maximum number of Unicode characters to copy.\r
+  @param  Destination A pointer to a Null-terminated Unicode string.\r
+  @param  Source      A pointer to a Null-terminated Unicode string.\r
+  @param  Length      The maximum number of Unicode characters to copy.\r
 \r
   @return Destination.\r
 \r
@@ -148,6 +142,7 @@ StrnCpy (
   ZeroMem (Destination, Length * sizeof (*Destination));\r
   return ReturnValue;\r
 }\r
+#endif\r
 \r
 /**\r
   Returns the length of a Null-terminated Unicode string.\r
@@ -161,7 +156,7 @@ StrnCpy (
   PcdMaximumUnicodeStringLength Unicode characters, not including the\r
   Null-terminator, then ASSERT().\r
 \r
-  @param  String  Pointer to a Null-terminated Unicode string.\r
+  @param  String  A pointer to a Null-terminated Unicode string.\r
 \r
   @return The length of String.\r
 \r
@@ -193,7 +188,7 @@ StrLen (
   Returns the size of a Null-terminated Unicode string in bytes, including the\r
   Null terminator.\r
 \r
-  This function returns the size, in bytes, of the Null-terminated Unicode string \r
+  This function returns the size, in bytes, of the Null-terminated Unicode string\r
   specified by String.\r
 \r
   If String is NULL, then ASSERT().\r
@@ -202,7 +197,7 @@ StrLen (
   PcdMaximumUnicodeStringLength Unicode characters, not including the\r
   Null-terminator, then ASSERT().\r
 \r
-  @param  String  Pointer to a Null-terminated Unicode string.\r
+  @param  String  A pointer to a Null-terminated Unicode string.\r
 \r
   @return The size of String.\r
 \r
@@ -237,8 +232,8 @@ StrSize (
   than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
   Null-terminator, then ASSERT().\r
 \r
-  @param  FirstString   Pointer to a Null-terminated Unicode string.\r
-  @param  SecondString  Pointer to a Null-terminated Unicode string.\r
+  @param  FirstString   A pointer to a Null-terminated Unicode string.\r
+  @param  SecondString  A pointer to a Null-terminated Unicode string.\r
 \r
   @retval 0      FirstString is identical to SecondString.\r
   @return others FirstString is not identical to SecondString.\r
@@ -267,7 +262,7 @@ StrCmp (
 /**\r
   Compares up to a specified length the contents of two Null-terminated Unicode strings,\r
   and returns the difference between the first mismatched Unicode characters.\r
-  \r
+\r
   This function compares the Null-terminated Unicode string FirstString to the\r
   Null-terminated Unicode string SecondString. At most, Length Unicode\r
   characters will be compared. If Length is 0, then 0 is returned. If\r
@@ -288,9 +283,9 @@ StrCmp (
   PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
   then ASSERT().\r
 \r
-  @param  FirstString   Pointer to a Null-terminated Unicode string.\r
-  @param  SecondString  Pointer to a Null-terminated Unicode string.\r
-  @param  Length        Maximum number of Unicode characters to compare.\r
+  @param  FirstString   A pointer to a Null-terminated Unicode string.\r
+  @param  SecondString  A pointer to a Null-terminated Unicode string.\r
+  @param  Length        The maximum number of Unicode characters to compare.\r
 \r
   @retval 0      FirstString is identical to SecondString.\r
   @return others FirstString is not identical to SecondString.\r
@@ -320,6 +315,7 @@ StrnCmp (
   }\r
 \r
   while ((*FirstString != L'\0') &&\r
+         (*SecondString != L'\0') &&\r
          (*FirstString == *SecondString) &&\r
          (Length > 1)) {\r
     FirstString++;\r
@@ -330,7 +326,11 @@ StrnCmp (
   return *FirstString - *SecondString;\r
 }\r
 \r
+#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
+\r
 /**\r
+  [ATTENTION] This function will be deprecated for security reason.\r
+\r
   Concatenates one Null-terminated Unicode string to another Null-terminated\r
   Unicode string, and returns the concatenated Unicode string.\r
 \r
@@ -356,8 +356,8 @@ StrnCmp (
   PcdMaximumUnicodeStringLength Unicode characters, not including the\r
   Null-terminator, then ASSERT().\r
 \r
-  @param  Destination Pointer to a Null-terminated Unicode string.\r
-  @param  Source      Pointer to a Null-terminated Unicode string.\r
+  @param  Destination A pointer to a Null-terminated Unicode string.\r
+  @param  Source      A pointer to a Null-terminated Unicode string.\r
 \r
   @return Destination.\r
 \r
@@ -380,8 +380,10 @@ StrCat (
 }\r
 \r
 /**\r
-  Concatenates up to a specified length one Null-terminated Unicode to the end \r
-  of another Null-terminated Unicode string, and returns the concatenated \r
+  [ATTENTION] This function will be deprecated for security reason.\r
+\r
+  Concatenates up to a specified length one Null-terminated Unicode to the end\r
+  of another Null-terminated Unicode string, and returns the concatenated\r
   Unicode string.\r
 \r
   This function concatenates two Null-terminated Unicode strings. The contents\r
@@ -397,7 +399,7 @@ StrCat (
   If Length > 0 and Source is NULL, then ASSERT().\r
   If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().\r
   If Source and Destination overlap, then ASSERT().\r
-  If PcdMaximumUnicodeStringLength is not zero, and Length is greater than \r
+  If PcdMaximumUnicodeStringLength is not zero, and Length is greater than\r
   PcdMaximumUnicodeStringLength, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r
   than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
@@ -409,9 +411,9 @@ StrCat (
   and Source results in a Unicode string with more than PcdMaximumUnicodeStringLength\r
   Unicode characters, not including the Null-terminator, then ASSERT().\r
 \r
-  @param  Destination Pointer to a Null-terminated Unicode string.\r
-  @param  Source      Pointer to a Null-terminated Unicode string.\r
-  @param  Length      Maximum number of Unicode characters to concatenate from\r
+  @param  Destination A pointer to a Null-terminated Unicode string.\r
+  @param  Source      A pointer to a Null-terminated Unicode string.\r
+  @param  Length      The maximum number of Unicode characters to concatenate from\r
                       Source.\r
 \r
   @return Destination.\r
@@ -438,6 +440,7 @@ StrnCat (
   ASSERT (StrSize (Destination) != 0);\r
   return Destination;\r
 }\r
+#endif\r
 \r
 /**\r
   Returns the first occurrence of a Null-terminated Unicode sub-string\r
@@ -458,8 +461,8 @@ StrnCat (
   or String contains more than PcdMaximumUnicodeStringLength Unicode\r
   characters, not including the Null-terminator, then ASSERT().\r
 \r
-  @param  String          Pointer to a Null-terminated Unicode string.\r
-  @param  SearchString    Pointer to a Null-terminated Unicode string to search for.\r
+  @param  String          A pointer to a Null-terminated Unicode string.\r
+  @param  SearchString    A pointer to a Null-terminated Unicode string to search for.\r
 \r
   @retval NULL            If the SearchString does not appear in String.\r
   @return others          If there is a match.\r
@@ -489,13 +492,13 @@ StrStr (
   while (*String != L'\0') {\r
     SearchStringTmp = SearchString;\r
     FirstMatch = String;\r
-    \r
-    while ((*String == *SearchStringTmp) \r
+\r
+    while ((*String == *SearchStringTmp)\r
             && (*String != L'\0')) {\r
       String++;\r
       SearchStringTmp++;\r
-    } \r
-    \r
+    }\r
+\r
     if (*SearchStringTmp == L'\0') {\r
       return (CHAR16 *) FirstMatch;\r
     }\r
@@ -513,7 +516,7 @@ StrStr (
 /**\r
   Check if a Unicode character is a decimal character.\r
 \r
-  This internal function checks if a Unicode character is a \r
+  This internal function checks if a Unicode character is a\r
   decimal character. The valid decimal character is from\r
   L'0' to L'9'.\r
 \r
@@ -533,7 +536,7 @@ InternalIsDecimalDigitCharacter (
 }\r
 \r
 /**\r
-  Convert a Unicode character to upper case only if \r
+  Convert a Unicode character to upper case only if\r
   it maps to a valid small-case ASCII character.\r
 \r
   This internal function only deal with Unicode character\r
@@ -565,7 +568,7 @@ InternalCharToUpper (
 \r
   This internal function only deal with Unicode character\r
   which maps to a valid hexadecimal ASII character, i.e.\r
-  L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other \r
+  L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other\r
   Unicode character, the value returned does not make sense.\r
 \r
   @param  Char  The character to convert.\r
@@ -583,14 +586,14 @@ InternalHexCharToUintn (
     return Char - L'0';\r
   }\r
 \r
-  return (UINTN) (10 + InternalCharToUpper (Char) - L'A');\r
+  return (10 + InternalCharToUpper (Char) - L'A');\r
 }\r
 \r
 /**\r
   Check if a Unicode character is a hexadecimal character.\r
 \r
-  This internal function checks if a Unicode character is a \r
-  decimal character.  The valid hexadecimal character is \r
+  This internal function checks if a Unicode character is a\r
+  decimal character.  The valid hexadecimal character is\r
   L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
 \r
 \r
@@ -635,13 +638,13 @@ InternalIsHexaDecimalDigitCharacter (
   If String has no pad spaces or valid decimal digits,\r
   then 0 is returned.\r
   If the number represented by String overflows according\r
-  to the range defined by UINTN, then ASSERT().\r
+  to the range defined by UINTN, then MAX_UINTN is returned.\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains\r
   more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
   the Null-terminator, then ASSERT().\r
 \r
-  @param  String      Pointer to a Null-terminated Unicode string.\r
+  @param  String      A pointer to a Null-terminated Unicode string.\r
 \r
   @retval Value translated from String.\r
 \r
@@ -653,43 +656,8 @@ StrDecimalToUintn (
   )\r
 {\r
   UINTN     Result;\r
-  \r
-  //\r
-  // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
-  // Length tests are performed inside StrLen().\r
-  //\r
-  ASSERT (StrSize (String) != 0);\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
-  Result = 0;\r
-\r
-  while (InternalIsDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_10) ||\r
-      ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_10) &&\r
-      (*String - L'0') <= REMAINDER_MAX_UINTN_DIVIDED_BY_10)\r
-      );\r
-\r
-    Result = Result * 10 + (*String - L'0');\r
-    String++;\r
-  }\r
-  \r
+  StrDecimalToUintnS (String, (CHAR16 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
@@ -717,13 +685,13 @@ StrDecimalToUintn (
   If String has no pad spaces or valid decimal digits,\r
   then 0 is returned.\r
   If the number represented by String overflows according\r
-  to the range defined by UINT64, then ASSERT().\r
+  to the range defined by UINT64, then MAX_UINT64 is returned.\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains\r
   more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
   the Null-terminator, then ASSERT().\r
 \r
-  @param  String          Pointer to a Null-terminated Unicode string.\r
+  @param  String          A pointer to a Null-terminated Unicode string.\r
 \r
   @retval Value translated from String.\r
 \r
@@ -735,43 +703,8 @@ StrDecimalToUint64 (
   )\r
 {\r
   UINT64     Result;\r
-  \r
-  //\r
-  // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
-  // Length tests are performed inside StrLen().\r
-  //\r
-  ASSERT (StrSize (String) != 0);\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
-  Result = 0;\r
-\r
-  while (InternalIsDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_10) || \r
-      ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_10) && \r
-      (*String - L'0') <= REMAINDER_MAX_UINT64_DIVIDED_BY_10)\r
-      );\r
 \r
-    Result = MultU64x32 (Result, 10) + (*String - L'0');\r
-    String++;\r
-  }\r
-  \r
+  StrDecimalToUint64S (String, (CHAR16 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
@@ -799,13 +732,13 @@ StrDecimalToUint64 (
   If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
   then zero is returned.\r
   If the number represented by String overflows according to the range defined by\r
-  UINTN, then ASSERT().\r
+  UINTN, then MAX_UINTN is returned.\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
   then ASSERT().\r
 \r
-  @param  String          Pointer to a Null-terminated Unicode string.\r
+  @param  String          A pointer to a Null-terminated Unicode string.\r
 \r
   @retval Value translated from String.\r
 \r
@@ -818,52 +751,7 @@ StrHexToUintn (
 {\r
   UINTN     Result;\r
 \r
-  //\r
-  // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
-  // Length tests are performed inside StrLen().\r
-  //\r
-  ASSERT (StrSize (String) != 0);\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
-      return 0;\r
-    }\r
-    //\r
-    // Skip the 'X'\r
-    //\r
-    String++;\r
-  }\r
-\r
-  Result = 0;\r
-  \r
-  while (InternalIsHexaDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the Hex Number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_16) ||\r
-      ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_16) && \r
-      (InternalHexCharToUintn (*String) <= REMAINDER_MAX_UINTN_DIVIDED_BY_16))\r
-      );\r
-\r
-    Result = (Result << 4) + InternalHexCharToUintn (*String);\r
-    String++;\r
-  }\r
-\r
+  StrHexToUintnS (String, (CHAR16 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
@@ -892,13 +780,13 @@ StrHexToUintn (
   If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
   then zero is returned.\r
   If the number represented by String overflows according to the range defined by\r
-  UINT64, then ASSERT().\r
+  UINT64, then MAX_UINT64 is returned.\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
   then ASSERT().\r
 \r
-  @param  String          Pointer to a Null-terminated Unicode string.\r
+  @param  String          A pointer to a Null-terminated Unicode string.\r
 \r
   @retval Value translated from String.\r
 \r
@@ -911,61 +799,14 @@ StrHexToUint64 (
 {\r
   UINT64    Result;\r
 \r
-  //\r
-  // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
-  // Length tests are performed inside StrLen().\r
-  //\r
-  ASSERT (StrSize (String) != 0);\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
-    ASSERT (*(String - 1) == L'0');\r
-    if (*(String - 1) != L'0') {\r
-      return 0;\r
-    }\r
-    //\r
-    // Skip the 'X'\r
-    //\r
-    String++;\r
-  }\r
-\r
-  Result = 0;\r
-  \r
-  while (InternalIsHexaDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the Hex Number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_16)|| \r
-      ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_16) && \r
-      (InternalHexCharToUintn (*String) <= REMAINDER_MAX_UINT64_DIVIDED_BY_16))\r
-      );\r
-\r
-    Result = LShiftU64 (Result, 4);\r
-    Result = Result + InternalHexCharToUintn (*String);\r
-    String++;\r
-  }\r
-\r
+  StrHexToUint64S (String, (CHAR16 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
 /**\r
   Check if a ASCII character is a decimal character.\r
 \r
-  This internal function checks if a Unicode character is a \r
+  This internal function checks if a Unicode character is a\r
   decimal character. The valid decimal character is from\r
   '0' to '9'.\r
 \r
@@ -987,8 +828,8 @@ InternalAsciiIsDecimalDigitCharacter (
 /**\r
   Check if a ASCII character is a hexadecimal character.\r
 \r
-  This internal function checks if a ASCII character is a \r
-  decimal character.  The valid hexadecimal character is \r
+  This internal function checks if a ASCII character is a\r
+  decimal character.  The valid hexadecimal character is\r
   L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
 \r
 \r
@@ -1010,7 +851,11 @@ InternalAsciiIsHexaDecimalDigitCharacter (
     (Char >= 'a' && Char <= 'f'));\r
 }\r
 \r
+#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
+\r
 /**\r
+  [ATTENTION] This function is deprecated for security reason.\r
+\r
   Convert a Null-terminated Unicode string to a Null-terminated\r
   ASCII string and returns the ASCII string.\r
 \r
@@ -1018,6 +863,9 @@ InternalAsciiIsHexaDecimalDigitCharacter (
   to the ASCII string Destination by copying the lower 8 bits of\r
   each Unicode character. It returns Destination.\r
 \r
+  The caller is responsible to make sure Destination points to a buffer with size\r
+  equal or greater than ((StrLen (Source) + 1) * sizeof (CHAR8)) in bytes.\r
+\r
   If any Unicode characters in Source contain non-zero value in\r
   the upper 8 bits, then ASSERT().\r
 \r
@@ -1034,8 +882,8 @@ InternalAsciiIsHexaDecimalDigitCharacter (
   than PcdMaximumAsciiStringLength Unicode characters, not including the\r
   Null-terminator, then ASSERT().\r
 \r
-  @param  Source        Pointer to a Null-terminated Unicode string.\r
-  @param  Destination   Pointer to a Null-terminated ASCII string.\r
+  @param  Source        A pointer to a Null-terminated Unicode string.\r
+  @param  Destination   A pointer to a Null-terminated ASCII string.\r
 \r
   @return Destination.\r
 \r
@@ -1060,14 +908,14 @@ UnicodeStrToAsciiStr (
   //\r
   // Source and Destination should not overlap\r
   //\r
-  ASSERT ((UINTN) ((CHAR16 *) Destination -  Source) > StrLen (Source));\r
+  ASSERT ((UINTN) (Destination - (CHAR8 *) Source) >= StrSize (Source));\r
   ASSERT ((UINTN) ((CHAR8 *) Source - Destination) > StrLen (Source));\r
 \r
 \r
   ReturnValue = Destination;\r
   while (*Source != '\0') {\r
     //\r
-    // If any Unicode characters in Source contain \r
+    // If any Unicode characters in Source contain\r
     // non-zero value in the upper 8 bits, then ASSERT().\r
     //\r
     ASSERT (*Source < 0x100);\r
@@ -1085,8 +933,9 @@ UnicodeStrToAsciiStr (
   return ReturnValue;\r
 }\r
 \r
-\r
 /**\r
+  [ATTENTION] This function will be deprecated for security reason.\r
+\r
   Copies one Null-terminated ASCII string to another Null-terminated ASCII\r
   string and returns the new ASCII string.\r
 \r
@@ -1101,8 +950,8 @@ UnicodeStrToAsciiStr (
   PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
   then ASSERT().\r
 \r
-  @param  Destination Pointer to a Null-terminated ASCII string.\r
-  @param  Source      Pointer to a Null-terminated ASCII string.\r
+  @param  Destination A pointer to a Null-terminated ASCII string.\r
+  @param  Source      A pointer to a Null-terminated ASCII string.\r
 \r
   @return Destination\r
 \r
@@ -1136,7 +985,9 @@ AsciiStrCpy (
 }\r
 \r
 /**\r
-  Copies up to a specified length one Null-terminated ASCII string to another \r
+  [ATTENTION] This function will be deprecated for security reason.\r
+\r
+  Copies up to a specified length one Null-terminated ASCII string to another\r
   Null-terminated ASCII string and returns the new ASCII string.\r
 \r
   This function copies the contents of the ASCII string Source to the ASCII\r
@@ -1149,15 +1000,15 @@ AsciiStrCpy (
   If Destination is NULL, then ASSERT().\r
   If Source is NULL, then ASSERT().\r
   If Source and Destination overlap, then ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero, and Length is greater than \r
+  If PcdMaximumAsciiStringLength is not zero, and Length is greater than\r
   PcdMaximumAsciiStringLength, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
   PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
   then ASSERT().\r
 \r
-  @param  Destination Pointer to a Null-terminated ASCII string.\r
-  @param  Source      Pointer to a Null-terminated ASCII string.\r
-  @param  Length      Maximum number of ASCII characters to copy.\r
+  @param  Destination A pointer to a Null-terminated ASCII string.\r
+  @param  Source      A pointer to a Null-terminated ASCII string.\r
+  @param  Length      The maximum number of ASCII characters to copy.\r
 \r
   @return Destination\r
 \r
@@ -1201,6 +1052,7 @@ AsciiStrnCpy (
   ZeroMem (Destination, Length * sizeof (*Destination));\r
   return ReturnValue;\r
 }\r
+#endif\r
 \r
 /**\r
   Returns the length of a Null-terminated ASCII string.\r
@@ -1214,7 +1066,7 @@ AsciiStrnCpy (
   PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
   then ASSERT().\r
 \r
-  @param  String  Pointer to a Null-terminated ASCII string.\r
+  @param  String  A pointer to a Null-terminated ASCII string.\r
 \r
   @return The length of String.\r
 \r
@@ -1253,7 +1105,7 @@ AsciiStrLen (
   PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
   then ASSERT().\r
 \r
-  @param  String  Pointer to a Null-terminated ASCII string.\r
+  @param  String  A pointer to a Null-terminated ASCII string.\r
 \r
   @return The size of String.\r
 \r
@@ -1286,8 +1138,8 @@ AsciiStrSize (
   than PcdMaximumAsciiStringLength ASCII characters, not including the\r
   Null-terminator, then ASSERT().\r
 \r
-  @param  FirstString   Pointer to a Null-terminated ASCII string.\r
-  @param  SecondString  Pointer to a Null-terminated ASCII string.\r
+  @param  FirstString   A pointer to a Null-terminated ASCII string.\r
+  @param  SecondString  A pointer to a Null-terminated ASCII string.\r
 \r
   @retval ==0      FirstString is identical to SecondString.\r
   @retval !=0      FirstString is not identical to SecondString.\r
@@ -1324,7 +1176,7 @@ AsciiStrCmp (
 \r
   @param  Chr   one Ascii character\r
 \r
-  @return The uppercase value of Ascii character \r
+  @return The uppercase value of Ascii character\r
 \r
 **/\r
 CHAR8\r
@@ -1341,7 +1193,7 @@ InternalBaseLibAsciiToUpper (
 \r
   This internal function only deal with Unicode character\r
   which maps to a valid hexadecimal ASII character, i.e.\r
-  '0' to '9', 'a' to 'f' or 'A' to 'F'. For other \r
+  '0' to '9', 'a' to 'f' or 'A' to 'F'. For other\r
   ASCII character, the value returned does not make sense.\r
 \r
   @param  Char  The character to convert.\r
@@ -1359,7 +1211,7 @@ InternalAsciiHexCharToUintn (
     return Char - '0';\r
   }\r
 \r
-  return (UINTN) (10 + InternalBaseLibAsciiToUpper (Char) - 'A');\r
+  return (10 + InternalBaseLibAsciiToUpper (Char) - 'A');\r
 }\r
 \r
 \r
@@ -1383,8 +1235,8 @@ InternalAsciiHexCharToUintn (
   than PcdMaximumAsciiStringLength ASCII characters, not including the\r
   Null-terminator, then ASSERT().\r
 \r
-  @param  FirstString   Pointer to a Null-terminated ASCII string.\r
-  @param  SecondString  Pointer to a Null-terminated ASCII string.\r
+  @param  FirstString   A pointer to a Null-terminated ASCII string.\r
+  @param  SecondString  A pointer to a Null-terminated ASCII string.\r
 \r
   @retval ==0    FirstString is identical to SecondString using case insensitive\r
                  comparisons.\r
@@ -1433,7 +1285,7 @@ AsciiStriCmp (
 \r
   If Length > 0 and FirstString is NULL, then ASSERT().\r
   If Length > 0 and SecondString is NULL, then ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero, and Length is greater than \r
+  If PcdMaximumAsciiStringLength is not zero, and Length is greater than\r
   PcdMaximumAsciiStringLength, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and FirstString contains more than\r
   PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
@@ -1442,10 +1294,10 @@ AsciiStriCmp (
   PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
   then ASSERT().\r
 \r
-  @param  FirstString   Pointer to a Null-terminated ASCII string.\r
-  @param  SecondString  Pointer to a Null-terminated ASCII string.\r
-  @param  Length        Maximum number of ASCII characters for compare.\r
-  \r
+  @param  FirstString   A pointer to a Null-terminated ASCII string.\r
+  @param  SecondString  A pointer to a Null-terminated ASCII string.\r
+  @param  Length        The maximum number of ASCII characters for compare.\r
+\r
   @retval ==0       FirstString is identical to SecondString.\r
   @retval !=0       FirstString is not identical to SecondString.\r
 \r
@@ -1473,6 +1325,7 @@ AsciiStrnCmp (
   }\r
 \r
   while ((*FirstString != '\0') &&\r
+         (*SecondString != '\0') &&\r
          (*FirstString == *SecondString) &&\r
          (Length > 1)) {\r
     FirstString++;\r
@@ -1482,7 +1335,11 @@ AsciiStrnCmp (
   return *FirstString - *SecondString;\r
 }\r
 \r
+#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
+\r
 /**\r
+  [ATTENTION] This function will be deprecated for security reason.\r
+\r
   Concatenates one Null-terminated ASCII string to another Null-terminated\r
   ASCII string, and returns the concatenated ASCII string.\r
 \r
@@ -1503,8 +1360,8 @@ AsciiStrnCmp (
   Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
   ASCII characters, then ASSERT().\r
 \r
-  @param  Destination Pointer to a Null-terminated ASCII string.\r
-  @param  Source      Pointer to a Null-terminated ASCII string.\r
+  @param  Destination A pointer to a Null-terminated ASCII string.\r
+  @param  Source      A pointer to a Null-terminated ASCII string.\r
 \r
   @return Destination\r
 \r
@@ -1527,8 +1384,10 @@ AsciiStrCat (
 }\r
 \r
 /**\r
-  Concatenates up to a specified length one Null-terminated ASCII string to \r
-  the end of another Null-terminated ASCII string, and returns the \r
+  [ATTENTION] This function will be deprecated for security reason.\r
+\r
+  Concatenates up to a specified length one Null-terminated ASCII string to\r
+  the end of another Null-terminated ASCII string, and returns the\r
   concatenated ASCII string.\r
 \r
   This function concatenates two Null-terminated ASCII strings. The contents\r
@@ -1554,9 +1413,9 @@ AsciiStrCat (
   Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
   ASCII characters, not including the Null-terminator, then ASSERT().\r
 \r
-  @param  Destination Pointer to a Null-terminated ASCII string.\r
-  @param  Source      Pointer to a Null-terminated ASCII string.\r
-  @param  Length      Maximum number of ASCII characters to concatenate from\r
+  @param  Destination A pointer to a Null-terminated ASCII string.\r
+  @param  Source      A pointer to a Null-terminated ASCII string.\r
+  @param  Length      The maximum number of ASCII characters to concatenate from\r
                       Source.\r
 \r
   @return Destination\r
@@ -1583,6 +1442,7 @@ AsciiStrnCat (
   ASSERT (AsciiStrSize (Destination) != 0);\r
   return Destination;\r
 }\r
+#endif\r
 \r
 /**\r
   Returns the first occurrence of a Null-terminated ASCII sub-string\r
@@ -1600,8 +1460,8 @@ AsciiStrnCat (
   String contains more than PcdMaximumAsciiStringLength Unicode characters\r
   not including the Null-terminator, then ASSERT().\r
 \r
-  @param  String          Pointer to a Null-terminated ASCII string.\r
-  @param  SearchString    Pointer to a Null-terminated ASCII string to search for.\r
+  @param  String          A pointer to a Null-terminated ASCII string.\r
+  @param  SearchString    A pointer to a Null-terminated ASCII string to search for.\r
 \r
   @retval NULL            If the SearchString does not appear in String.\r
   @retval others          If there is a match return the first occurrence of SearchingString.\r
@@ -1631,13 +1491,13 @@ AsciiStrStr (
   while (*String != '\0') {\r
     SearchStringTmp = SearchString;\r
     FirstMatch = String;\r
-    \r
-    while ((*String == *SearchStringTmp) \r
+\r
+    while ((*String == *SearchStringTmp)\r
             && (*String != '\0')) {\r
       String++;\r
       SearchStringTmp++;\r
-    } \r
-    \r
+    }\r
+\r
     if (*SearchStringTmp == '\0') {\r
       return (CHAR8 *) FirstMatch;\r
     }\r
@@ -1671,13 +1531,13 @@ AsciiStrStr (
   If String has only pad spaces, then 0 is returned.\r
   If String has no pad spaces or valid decimal digits, then 0 is returned.\r
   If the number represented by String overflows according to the range defined by\r
-  UINTN, then ASSERT().\r
+  UINTN, then MAX_UINTN is returned.\r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
   then ASSERT().\r
 \r
-  @param  String          Pointer to a Null-terminated ASCII string.\r
+  @param  String          A pointer to a Null-terminated ASCII string.\r
 \r
   @retval Value translated from String.\r
 \r
@@ -1689,42 +1549,8 @@ AsciiStrDecimalToUintn (
   )\r
 {\r
   UINTN     Result;\r
-  \r
-  //\r
-  // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
-  //\r
-  ASSERT (AsciiStrSize (String) != 0);\r
-\r
-  //\r
-  // Ignore the pad spaces (space or tab)\r
-  //\r
-  while ((*String == ' ') || (*String == '\t' )) {\r
-    String++;\r
-  }\r
-\r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == '0') {\r
-    String++;\r
-  }\r
 \r
-  Result = 0;\r
-\r
-  while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_10) ||\r
-      ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_10) && \r
-      (*String - '0') <= REMAINDER_MAX_UINTN_DIVIDED_BY_10)\r
-      );\r
-\r
-    Result = Result * 10 + (*String - '0');\r
-    String++;\r
-  }\r
-  \r
+  AsciiStrDecimalToUintnS (String, (CHAR8 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
@@ -1748,13 +1574,13 @@ AsciiStrDecimalToUintn (
   If String has only pad spaces, then 0 is returned.\r
   If String has no pad spaces or valid decimal digits, then 0 is returned.\r
   If the number represented by String overflows according to the range defined by\r
-  UINT64, then ASSERT().\r
+  UINT64, then MAX_UINT64 is returned.\r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
   then ASSERT().\r
 \r
-  @param  String          Pointer to a Null-terminated ASCII string.\r
+  @param  String          A pointer to a Null-terminated ASCII string.\r
 \r
   @retval Value translated from String.\r
 \r
@@ -1766,42 +1592,8 @@ AsciiStrDecimalToUint64 (
   )\r
 {\r
   UINT64     Result;\r
-  \r
-  //\r
-  // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
-  //\r
-  ASSERT (AsciiStrSize (String) != 0);\r
-\r
-  //\r
-  // Ignore the pad spaces (space or tab)\r
-  //\r
-  while ((*String == ' ') || (*String == '\t' )) {\r
-    String++;\r
-  }\r
 \r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == '0') {\r
-    String++;\r
-  }\r
-\r
-  Result = 0;\r
-\r
-  while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_10) || \r
-      ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_10) && \r
-      (*String - '0') <= REMAINDER_MAX_UINT64_DIVIDED_BY_10)\r
-      );\r
-\r
-    Result = MultU64x32 (Result, 10) + (*String - '0');\r
-    String++;\r
-  }\r
-  \r
+  AsciiStrDecimalToUint64S (String, (CHAR8 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
@@ -1828,13 +1620,13 @@ AsciiStrDecimalToUint64 (
   0 is returned.\r
 \r
   If the number represented by String overflows according to the range defined by UINTN,\r
-  then ASSERT().\r
+  then MAX_UINTN is returned.\r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero,\r
   and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
   the Null-terminator, then ASSERT().\r
 \r
-  @param  String          Pointer to a Null-terminated ASCII string.\r
+  @param  String          A pointer to a Null-terminated ASCII string.\r
 \r
   @retval Value translated from String.\r
 \r
@@ -1847,52 +1639,7 @@ AsciiStrHexToUintn (
 {\r
   UINTN     Result;\r
 \r
-  //\r
-  // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
-  //\r
-  ASSERT (AsciiStrSize (String) != 0);\r
-  \r
-  //\r
-  // Ignore the pad spaces (space or tab) \r
-  //\r
-  while ((*String == ' ') || (*String == '\t' )) {\r
-    String++;\r
-  }\r
-\r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == '0') {\r
-    String++;\r
-  }\r
-\r
-  if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
-    ASSERT (*(String - 1) == '0');\r
-    if (*(String - 1) != '0') {\r
-      return 0;\r
-    }\r
-    //\r
-    // Skip the 'X'\r
-    //\r
-    String++;\r
-  }\r
-\r
-  Result = 0;\r
-  \r
-  while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the Hex Number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-     ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_16) ||\r
-       ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_16) && \r
-       (InternalAsciiHexCharToUintn (*String) <= REMAINDER_MAX_UINTN_DIVIDED_BY_16))\r
-       );\r
-\r
-    Result = (Result << 4) + InternalAsciiHexCharToUintn (*String);\r
-    String++;\r
-  }\r
-\r
+  AsciiStrHexToUintnS (String, (CHAR8 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
@@ -1920,13 +1667,13 @@ AsciiStrHexToUintn (
   0 is returned.\r
 \r
   If the number represented by String overflows according to the range defined by UINT64,\r
-  then ASSERT().\r
+  then MAX_UINT64 is returned.\r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero,\r
   and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
   the Null-terminator, then ASSERT().\r
 \r
-  @param  String          Pointer to a Null-terminated ASCII string.\r
+  @param  String          A pointer to a Null-terminated ASCII string.\r
 \r
   @retval Value translated from String.\r
 \r
@@ -1939,61 +1686,15 @@ AsciiStrHexToUint64 (
 {\r
   UINT64    Result;\r
 \r
-  //\r
-  // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
-  //\r
-  ASSERT (AsciiStrSize (String) != 0);\r
-  \r
-  //\r
-  // Ignore the pad spaces (space or tab) and leading Zeros\r
-  //\r
-  //\r
-  // Ignore the pad spaces (space or tab) \r
-  //\r
-  while ((*String == ' ') || (*String == '\t' )) {\r
-    String++;\r
-  }\r
-\r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == '0') {\r
-    String++;\r
-  }\r
-\r
-  if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
-    ASSERT (*(String - 1) == '0');\r
-    if (*(String - 1) != '0') {\r
-      return 0;\r
-    }\r
-    //\r
-    // Skip the 'X'\r
-    //\r
-    String++;\r
-  }\r
-\r
-  Result = 0;\r
-  \r
-  while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the Hex Number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_16) ||\r
-      ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_16) && \r
-      (InternalAsciiHexCharToUintn (*String) <= REMAINDER_MAX_UINT64_DIVIDED_BY_16))\r
-      );\r
-\r
-    Result = LShiftU64 (Result, 4);\r
-    Result = Result + InternalAsciiHexCharToUintn (*String);\r
-    String++;\r
-  }\r
-\r
+  AsciiStrHexToUint64S (String, (CHAR8 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
+#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
 \r
 /**\r
+  [ATTENTION] This function is deprecated for security reason.\r
+\r
   Convert one Null-terminated ASCII string to a Null-terminated\r
   Unicode string and returns the Unicode string.\r
 \r
@@ -2014,8 +1715,8 @@ AsciiStrHexToUint64 (
   PcdMaximumUnicodeStringLength ASCII characters not including the\r
   Null-terminator, then ASSERT().\r
 \r
-  @param  Source        Pointer to a Null-terminated ASCII string.\r
-  @param  Destination   Pointer to a Null-terminated Unicode string.\r
+  @param  Source        A pointer to a Null-terminated ASCII string.\r
+  @param  Destination   A pointer to a Null-terminated Unicode string.\r
 \r
   @return Destination.\r
 \r
@@ -2040,9 +1741,9 @@ AsciiStrToUnicodeStr (
   // Source and Destination should not overlap\r
   //\r
   ASSERT ((UINTN) ((CHAR8 *) Destination - Source) > AsciiStrLen (Source));\r
-  ASSERT ((UINTN) (Source - (CHAR8 *) Destination) > (AsciiStrLen (Source) * sizeof (CHAR16)));\r
+  ASSERT ((UINTN) (Source - (CHAR8 *) Destination) >= (AsciiStrSize (Source) * sizeof (CHAR16)));\r
+\r
 \r
\r
   ReturnValue = Destination;\r
   while (*Source != '\0') {\r
     *(Destination++) = (CHAR16) *(Source++);\r
@@ -2060,6 +1761,8 @@ AsciiStrToUnicodeStr (
   return ReturnValue;\r
 }\r
 \r
+#endif\r
+\r
 /**\r
   Converts an 8-bit value to an 8-bit BCD value.\r
 \r