]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/String.c
Refine code.
[mirror_edk2.git] / MdePkg / Library / BaseLib / String.c
index fb3211d77c76c4d38da621490cf0e118af0ac4ef..273291a915b281ce1eebeaa611b63074da2a9a24 100644 (file)
@@ -1,11 +1,11 @@
 /** @file\r
   Unicode and ASCII string primatives.\r
 \r
-  Copyright (c) 2006 - 2008, Intel Corporation<BR>\r
-  All rights reserved. This program and the accompanying materials\r
+  Copyright (c) 2006 - 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
+  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
 \r
 /**\r
   Copies one Null-terminated Unicode string to another Null-terminated Unicode\r
   If 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 Source contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  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
@@ -93,15 +82,17 @@ StrCpy (
   If Length > 0 and Destination is NULL, then ASSERT().\r
   If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().\r
   If Length > 0 and Source is NULL, then ASSERT().\r
-  If Length > 0 and Source is not aligned on a 16-bit bounadry, 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
+  PcdMaximumUnicodeStringLength, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters not including the\r
-  Null-terminator, then ASSERT().\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
@@ -132,6 +123,10 @@ StrnCpy (
   ASSERT ((UINTN)(Destination - Source) > StrLen (Source));\r
   ASSERT ((UINTN)(Source - Destination) >= Length);\r
 \r
+  if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
+    ASSERT (Length <= PcdGet32 (PcdMaximumUnicodeStringLength));\r
+  }\r
+\r
   ReturnValue = Destination;\r
 \r
   while ((*Source != L'\0') && (Length > 0)) {\r
@@ -152,10 +147,10 @@ StrnCpy (
   If String is NULL, then ASSERT().\r
   If String is not aligned on a 16-bit boundary, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  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,10 +188,10 @@ StrLen (
   If String is NULL, then ASSERT().\r
   If String is not aligned on a 16-bit boundary, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  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
@@ -225,14 +220,14 @@ StrSize (
   If SecondString is NULL, then ASSERT().\r
   If SecondString is not aligned on a 16-bit boundary, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more\r
-  than PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
   Null-terminator, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r
-  than PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  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
@@ -261,7 +256,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
@@ -270,19 +265,21 @@ StrCmp (
   subtracted from the first mismatched Unicode character in FirstString.\r
 \r
   If Length > 0 and FirstString is NULL, then ASSERT().\r
-  If Length > 0 and FirstString is not aligned on a 16-bit bounadary, then ASSERT().\r
+  If Length > 0 and FirstString is not aligned on a 16-bit boundary, then ASSERT().\r
   If Length > 0 and SecondString is NULL, then ASSERT().\r
-  If Length > 0 and SecondString is not aligned on a 16-bit bounadary, then ASSERT().\r
-  If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more\r
-  than PcdMaximumUnicodeStringLength Unicode characters not including the\r
-  Null-terminator, then ASSERT().\r
-  If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r
-  than PcdMaximumUnicodeStringLength Unicode characters not including the\r
-  Null-terminator, then ASSERT().\r
+  If Length > 0 and SecondString is not aligned on a 16-bit boundary, then ASSERT().\r
+  If PcdMaximumUnicodeStringLength is not zero, and Length is greater than\r
+  PcdMaximumUnicodeStringLength, then ASSERT().\r
+  If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more than\r
+  PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
+  then ASSERT().\r
+  If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more than\r
+  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
@@ -307,6 +304,10 @@ StrnCmp (
   ASSERT (StrSize (FirstString) != 0);\r
   ASSERT (StrSize (SecondString) != 0);\r
 \r
+  if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
+    ASSERT (Length <= PcdGet32 (PcdMaximumUnicodeStringLength));\r
+  }\r
+\r
   while ((*FirstString != L'\0') &&\r
          (*FirstString == *SecondString) &&\r
          (Length > 1)) {\r
@@ -329,23 +330,23 @@ StrnCmp (
   results are undefined.\r
 \r
   If Destination is NULL, then ASSERT().\r
-  If Destination is not aligned on a 16-bit bounadary, then ASSERT().\r
+  If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
   If Source is NULL, then ASSERT().\r
-  If Source is not aligned on a 16-bit bounadary, then ASSERT().\r
+  If 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 Destination contains more\r
-  than PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
   Null-terminator, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  PcdMaximumUnicodeStringLength Unicode characters, not including the\r
   Null-terminator, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r
   and Source results in a Unicode string with more than\r
-  PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  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
@@ -385,20 +386,21 @@ 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
+  PcdMaximumUnicodeStringLength, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r
-  than PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
   Null-terminator, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  PcdMaximumUnicodeStringLength Unicode characters, not including the\r
   Null-terminator, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r
-  and Source results in a Unicode string with more than\r
-  PcdMaximumUnicodeStringLength Unicode characters not including the\r
-  Null-terminator, then ASSERT().\r
+  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
@@ -412,7 +414,11 @@ StrnCat (
   IN      UINTN                     Length\r
   )\r
 {\r
-  StrnCpy (Destination + StrLen (Destination), Source, Length);\r
+  UINTN   DestinationLen;\r
+\r
+  DestinationLen = StrLen (Destination);\r
+  StrnCpy (Destination + DestinationLen, Source, Length);\r
+  Destination[DestinationLen + Length] = L'\0';\r
 \r
   //\r
   // Size of the resulting string should never be zero.\r
@@ -439,10 +445,10 @@ StrnCat (
 \r
   If PcdMaximumUnicodeStringLength is not zero, and SearchString\r
   or String contains more than PcdMaximumUnicodeStringLength Unicode\r
-  characters not including the Null-terminator, then ASSERT().\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
@@ -451,8 +457,8 @@ StrnCat (
 CHAR16 *\r
 EFIAPI\r
 StrStr (\r
-  IN      CONST CHAR16                *String,\r
-  IN      CONST CHAR16                *SearchString\r
+  IN      CONST CHAR16              *String,\r
+  IN      CONST CHAR16              *SearchString\r
   )\r
 {\r
   CONST CHAR16 *FirstMatch;\r
@@ -465,29 +471,29 @@ StrStr (
   ASSERT (StrSize (String) != 0);\r
   ASSERT (StrSize (SearchString) != 0);\r
 \r
-  while (*String != '\0') {\r
+  if (*SearchString == L'\0') {\r
+    return (CHAR16 *) String;\r
+  }\r
+\r
+  while (*String != L'\0') {\r
     SearchStringTmp = SearchString;\r
     FirstMatch = String;\r
     \r
     while ((*String == *SearchStringTmp) \r
-            && (*SearchStringTmp != '\0') \r
-            && (*String != '\0')) {\r
+            && (*String != L'\0')) {\r
       String++;\r
       SearchStringTmp++;\r
     } \r
     \r
-    if (*SearchStringTmp == '\0') {\r
+    if (*SearchStringTmp == L'\0') {\r
       return (CHAR16 *) FirstMatch;\r
     }\r
 \r
-    if (SearchStringTmp == SearchString) {\r
-      //\r
-      // If no character from SearchString match,\r
-      // move the pointer to the String under search\r
-      // by one character.\r
-      //\r
-      String++;\r
+    if (*String == L'\0') {\r
+      return NULL;\r
     }\r
+\r
+    String = FirstMatch + 1;\r
   }\r
 \r
   return NULL;\r
@@ -603,7 +609,7 @@ InternalIsHexaDecimalDigitCharacter (
   of the Unicode string specified by String as a decimal number. The format\r
   of the input Unicode string String is:\r
 \r
-                    [spaces] [decimal digits].\r
+                  [spaces] [decimal digits].\r
 \r
   The valid decimal digit character is in the range [0-9]. The\r
   function will ignore the pad space, which includes spaces or\r
@@ -621,10 +627,10 @@ InternalIsHexaDecimalDigitCharacter (
   to the range defined by UINTN, then ASSERT().\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains\r
-  more than PcdMaximumUnicodeStringLength Unicode characters not including\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
@@ -632,7 +638,7 @@ InternalIsHexaDecimalDigitCharacter (
 UINTN\r
 EFIAPI\r
 StrDecimalToUintn (\r
-  IN      CONST CHAR16                *String\r
+  IN      CONST CHAR16              *String\r
   )\r
 {\r
   UINTN     Result;\r
@@ -664,10 +670,7 @@ StrDecimalToUintn (
     // 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
+    ASSERT (Result <= ((((UINTN) ~0) - (*String - L'0')) / 10));\r
 \r
     Result = Result * 10 + (*String - L'0');\r
     String++;\r
@@ -685,7 +688,7 @@ StrDecimalToUintn (
   of the Unicode string specified by String as a decimal number. The format\r
   of the input Unicode string String is:\r
 \r
-                   [spaces] [decimal digits].\r
+                  [spaces] [decimal digits].\r
 \r
   The valid decimal digit character is in the range [0-9]. The\r
   function will ignore the pad space, which includes spaces or\r
@@ -703,10 +706,10 @@ StrDecimalToUintn (
   to the range defined by UINT64, then ASSERT().\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains\r
-  more than PcdMaximumUnicodeStringLength Unicode characters not including\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
@@ -714,7 +717,7 @@ StrDecimalToUintn (
 UINT64\r
 EFIAPI\r
 StrDecimalToUint64 (\r
-  IN      CONST CHAR16                *String\r
+  IN      CONST CHAR16              *String\r
   )\r
 {\r
   UINT64     Result;\r
@@ -746,10 +749,7 @@ StrDecimalToUint64 (
     // 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
+    ASSERT (Result <= DivU64x32 (((UINT64) ~0) - (*String - L'0') , 10));\r
 \r
     Result = MultU64x32 (Result, 10) + (*String - L'0');\r
     String++;\r
@@ -785,10 +785,10 @@ StrDecimalToUint64 (
   UINTN, then ASSERT().\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,\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
@@ -796,7 +796,7 @@ StrDecimalToUint64 (
 UINTN\r
 EFIAPI\r
 StrHexToUintn (\r
-  IN      CONST CHAR16                *String\r
+  IN      CONST CHAR16              *String\r
   )\r
 {\r
   UINTN     Result;\r
@@ -822,7 +822,6 @@ StrHexToUintn (
   }\r
 \r
   if (InternalCharToUpper (*String) == L'X') {\r
-    ASSERT (*(String - 1) == L'0');\r
     if (*(String - 1) != L'0') {\r
       return 0;\r
     }\r
@@ -839,10 +838,7 @@ StrHexToUintn (
     // 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
+    ASSERT (Result <= ((((UINTN) ~0) - InternalHexCharToUintn (*String)) >> 4));\r
 \r
     Result = (Result << 4) + InternalHexCharToUintn (*String);\r
     String++;\r
@@ -879,18 +875,18 @@ StrHexToUintn (
   UINT64, then ASSERT().\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,\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
-  **/\r
+**/\r
 UINT64\r
 EFIAPI\r
 StrHexToUint64 (\r
-  IN      CONST CHAR16                *String\r
+  IN      CONST CHAR16             *String\r
   )\r
 {\r
   UINT64    Result;\r
@@ -933,10 +929,7 @@ StrHexToUint64 (
     // 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
+    ASSERT (Result <= RShiftU64 (((UINT64) ~0) - InternalHexCharToUintn (*String) , 4));\r
 \r
     Result = LShiftU64 (Result, 4);\r
     Result = Result + InternalHexCharToUintn (*String);\r
@@ -1011,15 +1004,15 @@ InternalAsciiIsHexaDecimalDigitCharacter (
   If Source and Destination overlap, then ASSERT().\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and Source contains\r
-  more than PcdMaximumUnicodeStringLength Unicode characters not including\r
+  more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
   the Null-terminator, then ASSERT().\r
 \r
   If PcdMaximumAsciiStringLength is not zero, and Source contains more\r
-  than PcdMaximumAsciiStringLength Unicode characters not including the\r
+  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
@@ -1027,8 +1020,8 @@ InternalAsciiIsHexaDecimalDigitCharacter (
 CHAR8 *\r
 EFIAPI\r
 UnicodeStrToAsciiStr (\r
-  IN      CONST CHAR16                *Source,\r
-  OUT     CHAR8                       *Destination\r
+  IN      CONST CHAR16              *Source,\r
+  OUT     CHAR8                     *Destination\r
   )\r
 {\r
   CHAR8                               *ReturnValue;\r
@@ -1082,11 +1075,11 @@ UnicodeStrToAsciiStr (
   If Source is NULL, then ASSERT().\r
   If Source and Destination overlap, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\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  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
@@ -1133,13 +1126,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
+  PcdMaximumAsciiStringLength, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\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
@@ -1169,6 +1164,10 @@ AsciiStrnCpy (
   ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r
   ASSERT ((UINTN)(Source - Destination) >= Length);\r
 \r
+  if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
+    ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));\r
+  }\r
+\r
   ReturnValue = Destination;\r
 \r
   while (*Source != 0 && Length > 0) {\r
@@ -1189,10 +1188,10 @@ AsciiStrnCpy (
   If Length > 0 and Destination is NULL, then ASSERT().\r
   If Length > 0 and Source 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
+  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
@@ -1228,10 +1227,10 @@ AsciiStrLen (
 \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
+  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
@@ -1258,14 +1257,14 @@ AsciiStrSize (
   If FirstString is NULL, then ASSERT().\r
   If SecondString is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
   then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
-  than PcdMaximumAsciiStringLength ASCII characters not including the\r
+  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
@@ -1307,7 +1306,7 @@ AsciiStrCmp (
 **/\r
 CHAR8\r
 EFIAPI\r
-AsciiToUpper (\r
+InternalBaseLibAsciiToUpper (\r
   IN      CHAR8                     Chr\r
   )\r
 {\r
@@ -1337,7 +1336,7 @@ InternalAsciiHexCharToUintn (
     return Char - '0';\r
   }\r
 \r
-  return (UINTN) (10 + AsciiToUpper (Char) - 'A');\r
+  return (UINTN) (10 + InternalBaseLibAsciiToUpper (Char) - 'A');\r
 }\r
 \r
 \r
@@ -1355,14 +1354,14 @@ InternalAsciiHexCharToUintn (
   If FirstString is NULL, then ASSERT().\r
   If SecondString is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
   then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
-  than PcdMaximumAsciiStringLength ASCII characters not including the\r
+  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
@@ -1386,13 +1385,13 @@ AsciiStriCmp (
   ASSERT (AsciiStrSize (FirstString));\r
   ASSERT (AsciiStrSize (SecondString));\r
 \r
-  UpperFirstString  = AsciiToUpper (*FirstString);\r
-  UpperSecondString = AsciiToUpper (*SecondString);\r
+  UpperFirstString  = InternalBaseLibAsciiToUpper (*FirstString);\r
+  UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);\r
   while ((*FirstString != '\0') && (UpperFirstString == UpperSecondString)) {\r
     FirstString++;\r
     SecondString++;\r
-    UpperFirstString  = AsciiToUpper (*FirstString);\r
-    UpperSecondString = AsciiToUpper (*SecondString);\r
+    UpperFirstString  = InternalBaseLibAsciiToUpper (*FirstString);\r
+    UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);\r
   }\r
 \r
   return UpperFirstString - UpperSecondString;\r
@@ -1411,17 +1410,19 @@ 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 FirstString contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\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
   then ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero and SecondString contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  If PcdMaximumAsciiStringLength is not zero, and SecondString contains more than\r
+  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
@@ -1444,6 +1445,10 @@ AsciiStrnCmp (
   ASSERT (AsciiStrSize (FirstString));\r
   ASSERT (AsciiStrSize (SecondString));\r
 \r
+  if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
+    ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));\r
+  }\r
+\r
   while ((*FirstString != '\0') &&\r
          (*FirstString == *SecondString) &&\r
          (Length > 1)) {\r
@@ -1466,17 +1471,17 @@ AsciiStrnCmp (
   If Destination is NULL, then ASSERT().\r
   If Source is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and Destination contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
   then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
   then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and concatenating Destination and\r
   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
@@ -1514,19 +1519,21 @@ AsciiStrCat (
   If Length > 0 and Destination is NULL, then ASSERT().\r
   If Length > 0 and 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
+  PcdMaximumAsciiStringLength, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and Destination contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
   then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
   then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and\r
   Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
-  ASCII characters not including the Null-terminator, then ASSERT().\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
@@ -1540,7 +1547,11 @@ AsciiStrnCat (
   IN      UINTN                     Length\r
   )\r
 {\r
-  AsciiStrnCpy (Destination + AsciiStrLen (Destination), Source, Length);\r
+  UINTN   DestinationLen;\r
+\r
+  DestinationLen = AsciiStrLen (Destination);\r
+  AsciiStrnCpy (Destination + DestinationLen, Source, Length);\r
+  Destination[DestinationLen + Length] = '\0';\r
 \r
   //\r
   // Size of the resulting string should never be zero.\r
@@ -1566,8 +1577,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
@@ -1577,8 +1588,8 @@ AsciiStrnCat (
 CHAR8 *\r
 EFIAPI\r
 AsciiStrStr (\r
-  IN      CONST CHAR8             *String,\r
-  IN      CONST CHAR8             *SearchString\r
+  IN      CONST CHAR8               *String,\r
+  IN      CONST CHAR8               *SearchString\r
   )\r
 {\r
   CONST CHAR8 *FirstMatch;\r
@@ -1590,12 +1601,15 @@ AsciiStrStr (
   ASSERT (AsciiStrSize (String) != 0);\r
   ASSERT (AsciiStrSize (SearchString) != 0);\r
 \r
+  if (*SearchString == '\0') {\r
+    return (CHAR8 *) String;\r
+  }\r
+\r
   while (*String != '\0') {\r
     SearchStringTmp = SearchString;\r
     FirstMatch = String;\r
     \r
     while ((*String == *SearchStringTmp) \r
-            && (*SearchStringTmp != '\0') \r
             && (*String != '\0')) {\r
       String++;\r
       SearchStringTmp++;\r
@@ -1605,15 +1619,11 @@ AsciiStrStr (
       return (CHAR8 *) FirstMatch;\r
     }\r
 \r
-    if (SearchStringTmp == SearchString) {\r
-      //\r
-      // If no character from SearchString match,\r
-      // move the pointer to the String under search\r
-      // by one character.\r
-      //\r
-      String++;\r
+    if (*String == '\0') {\r
+      return NULL;\r
     }\r
 \r
+    String = FirstMatch + 1;\r
   }\r
 \r
   return NULL;\r
@@ -1644,7 +1654,7 @@ AsciiStrStr (
   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
@@ -1683,10 +1693,7 @@ AsciiStrDecimalToUintn (
     // 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
+    ASSERT (Result <= ((((UINTN) ~0) - (*String - L'0')) / 10));\r
 \r
     Result = Result * 10 + (*String - '0');\r
     String++;\r
@@ -1721,7 +1728,7 @@ AsciiStrDecimalToUintn (
   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
@@ -1729,7 +1736,7 @@ AsciiStrDecimalToUintn (
 UINT64\r
 EFIAPI\r
 AsciiStrDecimalToUint64 (\r
-  IN      CONST CHAR8             *String\r
+  IN      CONST CHAR8               *String\r
   )\r
 {\r
   UINT64     Result;\r
@@ -1760,10 +1767,7 @@ AsciiStrDecimalToUint64 (
     // 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
+    ASSERT (Result <= DivU64x32 (((UINT64) ~0) - (*String - L'0') , 10));\r
 \r
     Result = MultU64x32 (Result, 10) + (*String - '0');\r
     String++;\r
@@ -1801,7 +1805,7 @@ AsciiStrDecimalToUint64 (
   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
@@ -1809,7 +1813,7 @@ AsciiStrDecimalToUint64 (
 UINTN\r
 EFIAPI\r
 AsciiStrHexToUintn (\r
-  IN      CONST CHAR8             *String\r
+  IN      CONST CHAR8               *String\r
   )\r
 {\r
   UINTN     Result;\r
@@ -1833,7 +1837,7 @@ AsciiStrHexToUintn (
     String++;\r
   }\r
 \r
-  if (AsciiToUpper (*String) == 'X') {\r
+  if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
     ASSERT (*(String - 1) == '0');\r
     if (*(String - 1) != '0') {\r
       return 0;\r
@@ -1851,10 +1855,7 @@ AsciiStrHexToUintn (
     // 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
+    ASSERT (Result <= ((((UINTN) ~0) - InternalHexCharToUintn (*String)) >> 4));\r
 \r
     Result = (Result << 4) + InternalAsciiHexCharToUintn (*String);\r
     String++;\r
@@ -1893,7 +1894,7 @@ AsciiStrHexToUintn (
   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
@@ -1901,7 +1902,7 @@ AsciiStrHexToUintn (
 UINT64\r
 EFIAPI\r
 AsciiStrHexToUint64 (\r
-  IN      CONST CHAR8             *String\r
+  IN      CONST CHAR8                *String\r
   )\r
 {\r
   UINT64    Result;\r
@@ -1928,7 +1929,7 @@ AsciiStrHexToUint64 (
     String++;\r
   }\r
 \r
-  if (AsciiToUpper (*String) == 'X') {\r
+  if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
     ASSERT (*(String - 1) == '0');\r
     if (*(String - 1) != '0') {\r
       return 0;\r
@@ -1946,10 +1947,7 @@ AsciiStrHexToUint64 (
     // 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
+    ASSERT (Result <= RShiftU64 (((UINT64) ~0) - InternalHexCharToUintn (*String) , 4));\r
 \r
     Result = LShiftU64 (Result, 4);\r
     Result = Result + InternalAsciiHexCharToUintn (*String);\r
@@ -1981,8 +1979,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