]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/SafeString.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdePkg / Library / BaseLib / SafeString.c
index 315059e5dcdc983a47fc12c9f428325435c5e2dc..b75b33381732a3c6b7998218bd896d2e8dcf9194 100644 (file)
@@ -1,14 +1,8 @@
 /** @file\r
   Safe String functions.\r
 \r
-  Copyright (c) 2014 - 2017, Intel Corporation. All rights reserved.<BR>\r
-  This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions of the BSD License\r
-  which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php.\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  Copyright (c) 2014 - 2019, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 \r
 #define SAFE_STRING_CONSTRAINT_CHECK(Expression, Status)  \\r
   do { \\r
-    ASSERT (Expression); \\r
     if (!(Expression)) { \\r
+      DEBUG ((DEBUG_VERBOSE, \\r
+        "%a(%d) %a: SAFE_STRING_CONSTRAINT_CHECK(%a) failed.  Return %r\n", \\r
+        __FILE__, DEBUG_LINE_NUMBER, __FUNCTION__, DEBUG_EXPRESSION_STRING (Expression), Status)); \\r
       return Status; \\r
     } \\r
   } while (FALSE)\r
 **/\r
 BOOLEAN\r
 InternalSafeStringIsOverlap (\r
-  IN VOID    *Base1,\r
-  IN UINTN   Size1,\r
-  IN VOID    *Base2,\r
-  IN UINTN   Size2\r
+  IN VOID   *Base1,\r
+  IN UINTN  Size1,\r
+  IN VOID   *Base2,\r
+  IN UINTN  Size2\r
   )\r
 {\r
   if ((((UINTN)Base1 >= (UINTN)Base2) && ((UINTN)Base1 < (UINTN)Base2 + Size2)) ||\r
-      (((UINTN)Base2 >= (UINTN)Base1) && ((UINTN)Base2 < (UINTN)Base1 + Size1))) {\r
+      (((UINTN)Base2 >= (UINTN)Base1) && ((UINTN)Base2 < (UINTN)Base1 + Size1)))\r
+  {\r
     return TRUE;\r
   }\r
+\r
   return FALSE;\r
 }\r
 \r
@@ -73,7 +71,7 @@ InternalSafeStringNoStrOverlap (
   IN UINTN   Size2\r
   )\r
 {\r
-  return !InternalSafeStringIsOverlap (Str1, Size1 * sizeof(CHAR16), Str2, Size2 * sizeof(CHAR16));\r
+  return !InternalSafeStringIsOverlap (Str1, Size1 * sizeof (CHAR16), Str2, Size2 * sizeof (CHAR16));\r
 }\r
 \r
 /**\r
@@ -91,10 +89,10 @@ InternalSafeStringNoStrOverlap (
 **/\r
 BOOLEAN\r
 InternalSafeStringNoAsciiStrOverlap (\r
-  IN CHAR8   *Str1,\r
-  IN UINTN   Size1,\r
-  IN CHAR8   *Str2,\r
-  IN UINTN   Size2\r
+  IN CHAR8  *Str1,\r
+  IN UINTN  Size1,\r
+  IN CHAR8  *Str2,\r
+  IN UINTN  Size2\r
   )\r
 {\r
   return !InternalSafeStringIsOverlap (Str1, Size1, Str2, Size2);\r
@@ -119,18 +117,18 @@ InternalSafeStringNoAsciiStrOverlap (
 UINTN\r
 EFIAPI\r
 StrnLenS (\r
-  IN CONST CHAR16              *String,\r
-  IN UINTN                     MaxSize\r
+  IN CONST CHAR16  *String,\r
+  IN UINTN         MaxSize\r
   )\r
 {\r
-  UINTN                             Length;\r
+  UINTN  Length;\r
 \r
-  ASSERT (((UINTN) String & BIT0) == 0);\r
+  ASSERT (((UINTN)String & BIT0) == 0);\r
 \r
   //\r
-  // If String is a null pointer, then the StrnLenS function returns zero.\r
+  // If String is a null pointer or MaxSize is 0, then the StrnLenS function returns zero.\r
   //\r
-  if (String == NULL) {\r
+  if ((String == NULL) || (MaxSize == 0)) {\r
     return 0;\r
   }\r
 \r
@@ -145,8 +143,10 @@ StrnLenS (
     if (Length >= MaxSize - 1) {\r
       return MaxSize;\r
     }\r
+\r
     Length++;\r
   }\r
+\r
   return Length;\r
 }\r
 \r
@@ -174,8 +174,8 @@ StrnLenS (
 UINTN\r
 EFIAPI\r
 StrnSizeS (\r
-  IN CONST CHAR16              *String,\r
-  IN UINTN                     MaxSize\r
+  IN CONST CHAR16  *String,\r
+  IN UINTN         MaxSize\r
   )\r
 {\r
   //\r
@@ -203,7 +203,6 @@ StrnSizeS (
 \r
   If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
   If Source is not aligned on a 16-bit boundary, then ASSERT().\r
-  If an error would be returned, then the function will also ASSERT().\r
 \r
   If an error is returned, then the Destination is unmodified.\r
 \r
@@ -217,7 +216,7 @@ StrnSizeS (
   @retval RETURN_INVALID_PARAMETER If Destination is NULL.\r
                                    If Source is NULL.\r
                                    If PcdMaximumUnicodeStringLength is not zero,\r
-                                    and DestMax is greater than \r
+                                    and DestMax is greater than\r
                                     PcdMaximumUnicodeStringLength.\r
                                    If DestMax is 0.\r
   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.\r
@@ -225,15 +224,15 @@ StrnSizeS (
 RETURN_STATUS\r
 EFIAPI\r
 StrCpyS (\r
-  OUT CHAR16       *Destination,\r
-  IN  UINTN        DestMax,\r
-  IN  CONST CHAR16 *Source\r
+  OUT CHAR16        *Destination,\r
+  IN  UINTN         DestMax,\r
+  IN  CONST CHAR16  *Source\r
   )\r
 {\r
-  UINTN            SourceLen;\r
-  \r
-  ASSERT (((UINTN) Destination & BIT0) == 0);\r
-  ASSERT (((UINTN) Source & BIT0) == 0);\r
+  UINTN  SourceLen;\r
+\r
+  ASSERT (((UINTN)Destination & BIT0) == 0);\r
+  ASSERT (((UINTN)Source & BIT0) == 0);\r
 \r
   //\r
   // 1. Neither Destination nor Source shall be a null pointer.\r
@@ -271,6 +270,7 @@ StrCpyS (
   while (*Source != 0) {\r
     *(Destination++) = *(Source++);\r
   }\r
+\r
   *Destination = 0;\r
 \r
   return RETURN_SUCCESS;\r
@@ -285,7 +285,6 @@ StrCpyS (
 \r
   If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().\r
   If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().\r
-  If an error would be returned, then the function will also ASSERT().\r
 \r
   If an error is returned, then the Destination is unmodified.\r
 \r
@@ -296,12 +295,12 @@ StrCpyS (
   @param  Length                   The maximum number of Unicode characters to copy.\r
 \r
   @retval RETURN_SUCCESS           String is copied.\r
-  @retval RETURN_BUFFER_TOO_SMALL  If DestMax is NOT greater than \r
+  @retval RETURN_BUFFER_TOO_SMALL  If DestMax is NOT greater than\r
                                    MIN(StrLen(Source), Length).\r
   @retval RETURN_INVALID_PARAMETER If Destination is NULL.\r
                                    If Source is NULL.\r
                                    If PcdMaximumUnicodeStringLength is not zero,\r
-                                    and DestMax is greater than \r
+                                    and DestMax is greater than\r
                                     PcdMaximumUnicodeStringLength.\r
                                    If DestMax is 0.\r
   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.\r
@@ -309,16 +308,16 @@ StrCpyS (
 RETURN_STATUS\r
 EFIAPI\r
 StrnCpyS (\r
-  OUT CHAR16       *Destination,\r
-  IN  UINTN        DestMax,\r
-  IN  CONST CHAR16 *Source,\r
-  IN  UINTN        Length\r
+  OUT CHAR16        *Destination,\r
+  IN  UINTN         DestMax,\r
+  IN  CONST CHAR16  *Source,\r
+  IN  UINTN         Length\r
   )\r
 {\r
-  UINTN            SourceLen;\r
+  UINTN  SourceLen;\r
 \r
-  ASSERT (((UINTN) Destination & BIT0) == 0);\r
-  ASSERT (((UINTN) Source & BIT0) == 0);\r
+  ASSERT (((UINTN)Destination & BIT0) == 0);\r
+  ASSERT (((UINTN)Source & BIT0) == 0);\r
 \r
   //\r
   // 1. Neither Destination nor Source shall be a null pointer.\r
@@ -342,7 +341,7 @@ StrnCpyS (
   //\r
   // 4. If Length is not less than DestMax, then DestMax shall be greater than StrnLenS(Source, DestMax).\r
   //\r
-  SourceLen = StrnLenS (Source, DestMax);\r
+  SourceLen = StrnLenS (Source, MIN (DestMax, Length));\r
   if (Length >= DestMax) {\r
     SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL);\r
   }\r
@@ -353,6 +352,7 @@ StrnCpyS (
   if (SourceLen > Length) {\r
     SourceLen = Length;\r
   }\r
+\r
   SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoStrOverlap (Destination, DestMax, (CHAR16 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);\r
 \r
   //\r
@@ -361,10 +361,11 @@ StrnCpyS (
   // pointed to by Destination. If no null character was copied from Source, then Destination[Length] is set to a null\r
   // character.\r
   //\r
-  while ((*Source != 0) && (SourceLen > 0)) {\r
+  while ((SourceLen > 0) && (*Source != 0)) {\r
     *(Destination++) = *(Source++);\r
     SourceLen--;\r
   }\r
+\r
   *Destination = 0;\r
 \r
   return RETURN_SUCCESS;\r
@@ -378,7 +379,6 @@ StrnCpyS (
 \r
   If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
   If Source is not aligned on a 16-bit boundary, then ASSERT().\r
-  If an error would be returned, then the function will also ASSERT().\r
 \r
   If an error is returned, then the Destination is unmodified.\r
 \r
@@ -388,14 +388,14 @@ StrnCpyS (
   @param  Source                   A pointer to a Null-terminated Unicode string.\r
 \r
   @retval RETURN_SUCCESS           String is appended.\r
-  @retval RETURN_BAD_BUFFER_SIZE   If DestMax is NOT greater than \r
+  @retval RETURN_BAD_BUFFER_SIZE   If DestMax is NOT greater than\r
                                    StrLen(Destination).\r
   @retval RETURN_BUFFER_TOO_SMALL  If (DestMax - StrLen(Destination)) is NOT\r
                                    greater than StrLen(Source).\r
   @retval RETURN_INVALID_PARAMETER If Destination is NULL.\r
                                    If Source is NULL.\r
                                    If PcdMaximumUnicodeStringLength is not zero,\r
-                                    and DestMax is greater than \r
+                                    and DestMax is greater than\r
                                     PcdMaximumUnicodeStringLength.\r
                                    If DestMax is 0.\r
   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.\r
@@ -403,17 +403,17 @@ StrnCpyS (
 RETURN_STATUS\r
 EFIAPI\r
 StrCatS (\r
-  IN OUT CHAR16       *Destination,\r
-  IN     UINTN        DestMax,\r
-  IN     CONST CHAR16 *Source\r
+  IN OUT CHAR16        *Destination,\r
+  IN     UINTN         DestMax,\r
+  IN     CONST CHAR16  *Source\r
   )\r
 {\r
-  UINTN               DestLen;\r
-  UINTN               CopyLen;\r
-  UINTN               SourceLen;\r
-  \r
-  ASSERT (((UINTN) Destination & BIT0) == 0);\r
-  ASSERT (((UINTN) Source & BIT0) == 0);\r
+  UINTN  DestLen;\r
+  UINTN  CopyLen;\r
+  UINTN  SourceLen;\r
+\r
+  ASSERT (((UINTN)Destination & BIT0) == 0);\r
+  ASSERT (((UINTN)Source & BIT0) == 0);\r
 \r
   //\r
   // Let CopyLen denote the value DestMax - StrnLenS(Destination, DestMax) upon entry to StrCatS.\r
@@ -464,6 +464,7 @@ StrCatS (
   while (*Source != 0) {\r
     *(Destination++) = *(Source++);\r
   }\r
+\r
   *Destination = 0;\r
 \r
   return RETURN_SUCCESS;\r
@@ -479,7 +480,6 @@ StrCatS (
 \r
   If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
   If Source is not aligned on a 16-bit boundary, then ASSERT().\r
-  If an error would be returned, then the function will also ASSERT().\r
 \r
   If an error is returned, then the Destination is unmodified.\r
 \r
@@ -497,7 +497,7 @@ StrCatS (
   @retval RETURN_INVALID_PARAMETER If Destination is NULL.\r
                                    If Source is NULL.\r
                                    If PcdMaximumUnicodeStringLength is not zero,\r
-                                    and DestMax is greater than \r
+                                    and DestMax is greater than\r
                                     PcdMaximumUnicodeStringLength.\r
                                    If DestMax is 0.\r
   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.\r
@@ -505,18 +505,18 @@ StrCatS (
 RETURN_STATUS\r
 EFIAPI\r
 StrnCatS (\r
-  IN OUT CHAR16       *Destination,\r
-  IN     UINTN        DestMax,\r
-  IN     CONST CHAR16 *Source,\r
-  IN     UINTN        Length\r
+  IN OUT CHAR16        *Destination,\r
+  IN     UINTN         DestMax,\r
+  IN     CONST CHAR16  *Source,\r
+  IN     UINTN         Length\r
   )\r
 {\r
-  UINTN               DestLen;\r
-  UINTN               CopyLen;\r
-  UINTN               SourceLen;\r
-  \r
-  ASSERT (((UINTN) Destination & BIT0) == 0);\r
-  ASSERT (((UINTN) Source & BIT0) == 0);\r
+  UINTN  DestLen;\r
+  UINTN  CopyLen;\r
+  UINTN  SourceLen;\r
+\r
+  ASSERT (((UINTN)Destination & BIT0) == 0);\r
+  ASSERT (((UINTN)Source & BIT0) == 0);\r
 \r
   //\r
   // Let CopyLen denote the value DestMax - StrnLenS(Destination, DestMax) upon entry to StrnCatS.\r
@@ -551,7 +551,7 @@ StrnCatS (
   //\r
   // 5. If Length is not less than CopyLen, then CopyLen shall be greater than StrnLenS(Source, CopyLen).\r
   //\r
-  SourceLen = StrnLenS (Source, CopyLen);\r
+  SourceLen = StrnLenS (Source, MIN (CopyLen, Length));\r
   if (Length >= CopyLen) {\r
     SAFE_STRING_CONSTRAINT_CHECK ((CopyLen > SourceLen), RETURN_BUFFER_TOO_SMALL);\r
   }\r
@@ -562,6 +562,7 @@ StrnCatS (
   if (SourceLen > Length) {\r
     SourceLen = Length;\r
   }\r
+\r
   SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoStrOverlap (Destination, DestMax, (CHAR16 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);\r
 \r
   //\r
@@ -572,10 +573,11 @@ StrnCatS (
   // a null character.\r
   //\r
   Destination = Destination + DestLen;\r
-  while ((*Source != 0) && (SourceLen > 0)) {\r
+  while ((SourceLen > 0) && (*Source != 0)) {\r
     *(Destination++) = *(Source++);\r
     SourceLen--;\r
   }\r
+\r
   *Destination = 0;\r
 \r
   return RETURN_SUCCESS;\r
@@ -596,12 +598,7 @@ StrnCatS (
   be ignored. Then, the function stops at the first character that is a not a\r
   valid decimal character or a Null-terminator, whichever one comes first.\r
 \r
-  If String is NULL, then ASSERT().\r
-  If Data is NULL, then ASSERT().\r
   If String is not aligned in 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
-  Null-terminator, then ASSERT().\r
 \r
   If String has no valid decimal digits in the above format, then 0 is stored\r
   at the location pointed to by Data.\r
@@ -632,12 +629,12 @@ StrnCatS (
 RETURN_STATUS\r
 EFIAPI\r
 StrDecimalToUintnS (\r
-  IN  CONST CHAR16             *String,\r
-  OUT       CHAR16             **EndPointer,  OPTIONAL\r
-  OUT       UINTN              *Data\r
+  IN  CONST CHAR16  *String,\r
+  OUT       CHAR16  **EndPointer   OPTIONAL,\r
+  OUT       UINTN   *Data\r
   )\r
 {\r
-  ASSERT (((UINTN) String & BIT0) == 0);\r
+  ASSERT (((UINTN)String & BIT0) == 0);\r
 \r
   //\r
   // 1. Neither String nor Data shall be a null pointer.\r
@@ -653,7 +650,7 @@ StrDecimalToUintnS (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR16 *) String;\r
+    *EndPointer = (CHAR16 *)String;\r
   }\r
 \r
   //\r
@@ -681,8 +678,9 @@ StrDecimalToUintnS (
     if (*Data > ((MAX_UINTN - (*String - L'0')) / 10)) {\r
       *Data = MAX_UINTN;\r
       if (EndPointer != NULL) {\r
-        *EndPointer = (CHAR16 *) String;\r
+        *EndPointer = (CHAR16 *)String;\r
       }\r
+\r
       return RETURN_UNSUPPORTED;\r
     }\r
 \r
@@ -691,8 +689,9 @@ StrDecimalToUintnS (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR16 *) String;\r
+    *EndPointer = (CHAR16 *)String;\r
   }\r
+\r
   return RETURN_SUCCESS;\r
 }\r
 \r
@@ -711,12 +710,7 @@ StrDecimalToUintnS (
   be ignored. Then, the function stops at the first character that is a not a\r
   valid decimal character or a Null-terminator, whichever one comes first.\r
 \r
-  If String is NULL, then ASSERT().\r
-  If Data is NULL, then ASSERT().\r
   If String is not aligned in 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
-  Null-terminator, then ASSERT().\r
 \r
   If String has no valid decimal digits in the above format, then 0 is stored\r
   at the location pointed to by Data.\r
@@ -747,12 +741,12 @@ StrDecimalToUintnS (
 RETURN_STATUS\r
 EFIAPI\r
 StrDecimalToUint64S (\r
-  IN  CONST CHAR16             *String,\r
-  OUT       CHAR16             **EndPointer,  OPTIONAL\r
-  OUT       UINT64             *Data\r
+  IN  CONST CHAR16  *String,\r
+  OUT       CHAR16  **EndPointer   OPTIONAL,\r
+  OUT       UINT64  *Data\r
   )\r
 {\r
-  ASSERT (((UINTN) String & BIT0) == 0);\r
+  ASSERT (((UINTN)String & BIT0) == 0);\r
 \r
   //\r
   // 1. Neither String nor Data shall be a null pointer.\r
@@ -768,7 +762,7 @@ StrDecimalToUint64S (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR16 *) String;\r
+    *EndPointer = (CHAR16 *)String;\r
   }\r
 \r
   //\r
@@ -796,8 +790,9 @@ StrDecimalToUint64S (
     if (*Data > DivU64x32 (MAX_UINT64 - (*String - L'0'), 10)) {\r
       *Data = MAX_UINT64;\r
       if (EndPointer != NULL) {\r
-        *EndPointer = (CHAR16 *) String;\r
+        *EndPointer = (CHAR16 *)String;\r
       }\r
+\r
       return RETURN_UNSUPPORTED;\r
     }\r
 \r
@@ -806,8 +801,9 @@ StrDecimalToUint64S (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR16 *) String;\r
+    *EndPointer = (CHAR16 *)String;\r
   }\r
+\r
   return RETURN_SUCCESS;\r
 }\r
 \r
@@ -831,12 +827,7 @@ StrDecimalToUint64S (
   the first character that is a not a valid hexadecimal character or NULL,\r
   whichever one comes first.\r
 \r
-  If String is NULL, then ASSERT().\r
-  If Data is NULL, then ASSERT().\r
   If String is not aligned in 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
-  Null-terminator, then ASSERT().\r
 \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
@@ -867,12 +858,15 @@ StrDecimalToUint64S (
 RETURN_STATUS\r
 EFIAPI\r
 StrHexToUintnS (\r
-  IN  CONST CHAR16             *String,\r
-  OUT       CHAR16             **EndPointer,  OPTIONAL\r
-  OUT       UINTN              *Data\r
+  IN  CONST CHAR16  *String,\r
+  OUT       CHAR16  **EndPointer   OPTIONAL,\r
+  OUT       UINTN   *Data\r
   )\r
 {\r
-  ASSERT (((UINTN) String & BIT0) == 0);\r
+  BOOLEAN  FoundLeadingZero;\r
+\r
+  FoundLeadingZero = FALSE;\r
+  ASSERT (((UINTN)String & BIT0) == 0);\r
 \r
   //\r
   // 1. Neither String nor Data shall be a null pointer.\r
@@ -888,7 +882,7 @@ StrHexToUintnS (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR16 *) String;\r
+    *EndPointer = (CHAR16 *)String;\r
   }\r
 \r
   //\r
@@ -901,15 +895,18 @@ StrHexToUintnS (
   //\r
   // Ignore leading Zeros after the spaces\r
   //\r
+\r
+  FoundLeadingZero = *String == L'0';\r
   while (*String == L'0') {\r
     String++;\r
   }\r
 \r
-  if (InternalCharToUpper (*String) == L'X') {\r
-    if (*(String - 1) != L'0') {\r
+  if (CharToUpper (*String) == L'X') {\r
+    if (!FoundLeadingZero) {\r
       *Data = 0;\r
       return RETURN_SUCCESS;\r
     }\r
+\r
     //\r
     // Skip the 'X'\r
     //\r
@@ -927,8 +924,9 @@ StrHexToUintnS (
     if (*Data > ((MAX_UINTN - InternalHexCharToUintn (*String)) >> 4)) {\r
       *Data = MAX_UINTN;\r
       if (EndPointer != NULL) {\r
-        *EndPointer = (CHAR16 *) String;\r
+        *EndPointer = (CHAR16 *)String;\r
       }\r
+\r
       return RETURN_UNSUPPORTED;\r
     }\r
 \r
@@ -937,8 +935,9 @@ StrHexToUintnS (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR16 *) String;\r
+    *EndPointer = (CHAR16 *)String;\r
   }\r
+\r
   return RETURN_SUCCESS;\r
 }\r
 \r
@@ -962,12 +961,7 @@ StrHexToUintnS (
   the first character that is a not a valid hexadecimal character or NULL,\r
   whichever one comes first.\r
 \r
-  If String is NULL, then ASSERT().\r
-  If Data is NULL, then ASSERT().\r
   If String is not aligned in 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
-  Null-terminator, then ASSERT().\r
 \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
@@ -998,12 +992,15 @@ StrHexToUintnS (
 RETURN_STATUS\r
 EFIAPI\r
 StrHexToUint64S (\r
-  IN  CONST CHAR16             *String,\r
-  OUT       CHAR16             **EndPointer,  OPTIONAL\r
-  OUT       UINT64             *Data\r
+  IN  CONST CHAR16  *String,\r
+  OUT       CHAR16  **EndPointer   OPTIONAL,\r
+  OUT       UINT64  *Data\r
   )\r
 {\r
-  ASSERT (((UINTN) String & BIT0) == 0);\r
+  BOOLEAN  FoundLeadingZero;\r
+\r
+  FoundLeadingZero = FALSE;\r
+  ASSERT (((UINTN)String & BIT0) == 0);\r
 \r
   //\r
   // 1. Neither String nor Data shall be a null pointer.\r
@@ -1019,7 +1016,7 @@ StrHexToUint64S (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR16 *) String;\r
+    *EndPointer = (CHAR16 *)String;\r
   }\r
 \r
   //\r
@@ -1032,15 +1029,17 @@ StrHexToUint64S (
   //\r
   // Ignore leading Zeros after the spaces\r
   //\r
+  FoundLeadingZero = *String == L'0';\r
   while (*String == L'0') {\r
     String++;\r
   }\r
 \r
-  if (InternalCharToUpper (*String) == L'X') {\r
-    if (*(String - 1) != L'0') {\r
+  if (CharToUpper (*String) == L'X') {\r
+    if (!FoundLeadingZero) {\r
       *Data = 0;\r
       return RETURN_SUCCESS;\r
     }\r
+\r
     //\r
     // Skip the 'X'\r
     //\r
@@ -1058,8 +1057,9 @@ StrHexToUint64S (
     if (*Data > RShiftU64 (MAX_UINT64 - InternalHexCharToUintn (*String), 4)) {\r
       *Data = MAX_UINT64;\r
       if (EndPointer != NULL) {\r
-        *EndPointer = (CHAR16 *) String;\r
+        *EndPointer = (CHAR16 *)String;\r
       }\r
+\r
       return RETURN_UNSUPPORTED;\r
     }\r
 \r
@@ -1068,8 +1068,612 @@ StrHexToUint64S (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR16 *) String;\r
+    *EndPointer = (CHAR16 *)String;\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  Convert a Null-terminated Unicode string to IPv6 address and prefix length.\r
+\r
+  This function outputs a value of type IPv6_ADDRESS and may output a value\r
+  of type UINT8 by interpreting the contents of the Unicode string specified\r
+  by String. The format of the input Unicode string String is as follows:\r
+\r
+                  X:X:X:X:X:X:X:X[/P]\r
+\r
+  X contains one to four hexadecimal digit characters in the range [0-9], [a-f] and\r
+  [A-F]. X is converted to a value of type UINT16, whose low byte is stored in low\r
+  memory address and high byte is stored in high memory address. P contains decimal\r
+  digit characters in the range [0-9]. The running zero in the beginning of P will\r
+  be ignored. /P is optional.\r
+\r
+  When /P is not in the String, the function stops at the first character that is\r
+  not a valid hexadecimal digit character after eight X's are converted.\r
+\r
+  When /P is in the String, the function stops at the first character that is not\r
+  a valid decimal digit character after P is converted.\r
+\r
+  "::" can be used to compress one or more groups of X when X contains only 0.\r
+  The "::" can only appear once in the String.\r
+\r
+  If String is not aligned in a 16-bit boundary, then ASSERT().\r
+\r
+  If EndPointer is not NULL and Address is translated from String, a pointer\r
+  to the character that stopped the scan is stored at the location pointed to\r
+  by EndPointer.\r
+\r
+  @param  String                   Pointer to a Null-terminated Unicode string.\r
+  @param  EndPointer               Pointer to character that stops scan.\r
+  @param  Address                  Pointer to the converted IPv6 address.\r
+  @param  PrefixLength             Pointer to the converted IPv6 address prefix\r
+                                   length. MAX_UINT8 is returned when /P is\r
+                                   not in the String.\r
+\r
+  @retval RETURN_SUCCESS           Address is translated from String.\r
+  @retval RETURN_INVALID_PARAMETER If String is NULL.\r
+                                   If Data is NULL.\r
+  @retval RETURN_UNSUPPORTED       If X contains more than four hexadecimal\r
+                                    digit characters.\r
+                                   If String contains "::" and number of X\r
+                                    is not less than 8.\r
+                                   If P starts with character that is not a\r
+                                    valid decimal digit character.\r
+                                   If the decimal number converted from P\r
+                                    exceeds 128.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+StrToIpv6Address (\r
+  IN  CONST CHAR16  *String,\r
+  OUT CHAR16        **EndPointer  OPTIONAL,\r
+  OUT IPv6_ADDRESS  *Address,\r
+  OUT UINT8         *PrefixLength OPTIONAL\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+  UINTN          AddressIndex;\r
+  UINTN          Uintn;\r
+  IPv6_ADDRESS   LocalAddress;\r
+  UINT8          LocalPrefixLength;\r
+  CONST CHAR16   *Pointer;\r
+  CHAR16         *End;\r
+  UINTN          CompressStart;\r
+  BOOLEAN        ExpectPrefix;\r
+\r
+  LocalPrefixLength = MAX_UINT8;\r
+  CompressStart     = ARRAY_SIZE (Address->Addr);\r
+  ExpectPrefix      = FALSE;\r
+\r
+  ASSERT (((UINTN)String & BIT0) == 0);\r
+\r
+  //\r
+  // 1. None of String or Guid shall be a null pointer.\r
+  //\r
+  SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);\r
+  SAFE_STRING_CONSTRAINT_CHECK ((Address != NULL), RETURN_INVALID_PARAMETER);\r
+\r
+  for (Pointer = String, AddressIndex = 0; AddressIndex < ARRAY_SIZE (Address->Addr) + 1;) {\r
+    if (!InternalIsHexaDecimalDigitCharacter (*Pointer)) {\r
+      if (*Pointer != L':') {\r
+        //\r
+        // ":" or "/" should be followed by digit characters.\r
+        //\r
+        return RETURN_UNSUPPORTED;\r
+      }\r
+\r
+      //\r
+      // Meet second ":" after previous ":" or "/"\r
+      // or meet first ":" in the beginning of String.\r
+      //\r
+      if (ExpectPrefix) {\r
+        //\r
+        // ":" shall not be after "/"\r
+        //\r
+        return RETURN_UNSUPPORTED;\r
+      }\r
+\r
+      if ((CompressStart != ARRAY_SIZE (Address->Addr)) || (AddressIndex == ARRAY_SIZE (Address->Addr))) {\r
+        //\r
+        // "::" can only appear once.\r
+        // "::" can only appear when address is not full length.\r
+        //\r
+        return RETURN_UNSUPPORTED;\r
+      } else {\r
+        //\r
+        // Remember the start of zero compressing.\r
+        //\r
+        CompressStart = AddressIndex;\r
+        Pointer++;\r
+\r
+        if (CompressStart == 0) {\r
+          if (*Pointer != L':') {\r
+            //\r
+            // Single ":" shall not be in the beginning of String.\r
+            //\r
+            return RETURN_UNSUPPORTED;\r
+          }\r
+\r
+          Pointer++;\r
+        }\r
+      }\r
+    }\r
+\r
+    if (!InternalIsHexaDecimalDigitCharacter (*Pointer)) {\r
+      if (*Pointer == L'/') {\r
+        //\r
+        // Might be optional "/P" after "::".\r
+        //\r
+        if (CompressStart != AddressIndex) {\r
+          return RETURN_UNSUPPORTED;\r
+        }\r
+      } else {\r
+        break;\r
+      }\r
+    } else {\r
+      if (!ExpectPrefix) {\r
+        //\r
+        // Get X.\r
+        //\r
+        Status = StrHexToUintnS (Pointer, &End, &Uintn);\r
+        if (RETURN_ERROR (Status) || (End - Pointer > 4)) {\r
+          //\r
+          // Number of hexadecimal digit characters is no more than 4.\r
+          //\r
+          return RETURN_UNSUPPORTED;\r
+        }\r
+\r
+        Pointer = End;\r
+        //\r
+        // Uintn 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
+        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
+          //\r
+          // Prefix length should not exceed 128.\r
+          //\r
+          return RETURN_UNSUPPORTED;\r
+        }\r
+\r
+        LocalPrefixLength = (UINT8)Uintn;\r
+        Pointer           = End;\r
+        break;\r
+      }\r
+    }\r
+\r
+    //\r
+    // Skip ':' or "/"\r
+    //\r
+    if (*Pointer == L'/') {\r
+      ExpectPrefix = TRUE;\r
+    } else if (*Pointer == L':') {\r
+      if (AddressIndex == ARRAY_SIZE (Address->Addr)) {\r
+        //\r
+        // Meet additional ":" after all 8 16-bit address\r
+        //\r
+        break;\r
+      }\r
+    } else {\r
+      //\r
+      // Meet other character that is not "/" or ":" after all 8 16-bit address\r
+      //\r
+      break;\r
+    }\r
+\r
+    Pointer++;\r
+  }\r
+\r
+  if (((AddressIndex == ARRAY_SIZE (Address->Addr)) && (CompressStart != ARRAY_SIZE (Address->Addr))) ||\r
+      ((AddressIndex != ARRAY_SIZE (Address->Addr)) && (CompressStart == ARRAY_SIZE (Address->Addr)))\r
+      )\r
+  {\r
+    //\r
+    // Full length of address shall not have compressing zeros.\r
+    // Non-full length of address shall have compressing zeros.\r
+    //\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  CopyMem (&Address->Addr[0], &LocalAddress.Addr[0], CompressStart);\r
+  ZeroMem (&Address->Addr[CompressStart], ARRAY_SIZE (Address->Addr) - AddressIndex);\r
+  if (AddressIndex > CompressStart) {\r
+    CopyMem (\r
+      &Address->Addr[CompressStart + ARRAY_SIZE (Address->Addr) - AddressIndex],\r
+      &LocalAddress.Addr[CompressStart],\r
+      AddressIndex - CompressStart\r
+      );\r
+  }\r
+\r
+  if (PrefixLength != NULL) {\r
+    *PrefixLength = LocalPrefixLength;\r
+  }\r
+\r
+  if (EndPointer != NULL) {\r
+    *EndPointer = (CHAR16 *)Pointer;\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  Convert a Null-terminated Unicode string to IPv4 address and prefix length.\r
+\r
+  This function outputs a value of type IPv4_ADDRESS and may output a value\r
+  of type UINT8 by interpreting the contents of the Unicode string specified\r
+  by String. The format of the input Unicode string String is as follows:\r
+\r
+                  D.D.D.D[/P]\r
+\r
+  D and P are decimal digit characters in the range [0-9]. The running zero in\r
+  the beginning of D and P will be ignored. /P is optional.\r
+\r
+  When /P is not in the String, the function stops at the first character that is\r
+  not a valid decimal digit character after four D's are converted.\r
+\r
+  When /P is in the String, the function stops at the first character that is not\r
+  a valid decimal digit character after P is converted.\r
+\r
+  If String is not aligned in a 16-bit boundary, then ASSERT().\r
+\r
+  If EndPointer is not NULL and Address is translated from String, a pointer\r
+  to the character that stopped the scan is stored at the location pointed to\r
+  by EndPointer.\r
+\r
+  @param  String                   Pointer to a Null-terminated Unicode string.\r
+  @param  EndPointer               Pointer to character that stops scan.\r
+  @param  Address                  Pointer to the converted IPv4 address.\r
+  @param  PrefixLength             Pointer to the converted IPv4 address prefix\r
+                                   length. MAX_UINT8 is returned when /P is\r
+                                   not in the String.\r
+\r
+  @retval RETURN_SUCCESS           Address is translated from String.\r
+  @retval RETURN_INVALID_PARAMETER If String is NULL.\r
+                                   If Data is NULL.\r
+  @retval RETURN_UNSUPPORTED       If String is not in the correct format.\r
+                                   If any decimal number converted from D\r
+                                    exceeds 255.\r
+                                   If the decimal number converted from P\r
+                                    exceeds 32.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+StrToIpv4Address (\r
+  IN  CONST CHAR16  *String,\r
+  OUT CHAR16        **EndPointer  OPTIONAL,\r
+  OUT IPv4_ADDRESS  *Address,\r
+  OUT UINT8         *PrefixLength OPTIONAL\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+  UINTN          AddressIndex;\r
+  UINTN          Uintn;\r
+  IPv4_ADDRESS   LocalAddress;\r
+  UINT8          LocalPrefixLength;\r
+  CHAR16         *Pointer;\r
+\r
+  LocalPrefixLength = MAX_UINT8;\r
+\r
+  ASSERT (((UINTN)String & BIT0) == 0);\r
+\r
+  //\r
+  // 1. None of String or Guid shall be a null pointer.\r
+  //\r
+  SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);\r
+  SAFE_STRING_CONSTRAINT_CHECK ((Address != NULL), RETURN_INVALID_PARAMETER);\r
+\r
+  for (Pointer = (CHAR16 *)String, AddressIndex = 0; AddressIndex < ARRAY_SIZE (Address->Addr) + 1;) {\r
+    if (!InternalIsDecimalDigitCharacter (*Pointer)) {\r
+      //\r
+      // D or P contains invalid characters.\r
+      //\r
+      break;\r
+    }\r
+\r
+    //\r
+    // Get D or P.\r
+    //\r
+    Status = StrDecimalToUintnS ((CONST CHAR16 *)Pointer, &Pointer, &Uintn);\r
+    if (RETURN_ERROR (Status)) {\r
+      return RETURN_UNSUPPORTED;\r
+    }\r
+\r
+    if (AddressIndex == ARRAY_SIZE (Address->Addr)) {\r
+      //\r
+      // It's P.\r
+      //\r
+      if (Uintn > 32) {\r
+        return RETURN_UNSUPPORTED;\r
+      }\r
+\r
+      LocalPrefixLength = (UINT8)Uintn;\r
+    } else {\r
+      //\r
+      // It's D.\r
+      //\r
+      if (Uintn > MAX_UINT8) {\r
+        return RETURN_UNSUPPORTED;\r
+      }\r
+\r
+      LocalAddress.Addr[AddressIndex] = (UINT8)Uintn;\r
+      AddressIndex++;\r
+    }\r
+\r
+    //\r
+    // Check the '.' or '/', depending on the AddressIndex.\r
+    //\r
+    if (AddressIndex == ARRAY_SIZE (Address->Addr)) {\r
+      if (*Pointer == L'/') {\r
+        //\r
+        // '/P' is in the String.\r
+        // Skip "/" and get P in next loop.\r
+        //\r
+        Pointer++;\r
+      } else {\r
+        //\r
+        // '/P' is not in the String.\r
+        //\r
+        break;\r
+      }\r
+    } else if (AddressIndex < ARRAY_SIZE (Address->Addr)) {\r
+      if (*Pointer == L'.') {\r
+        //\r
+        // D should be followed by '.'\r
+        //\r
+        Pointer++;\r
+      } else {\r
+        return RETURN_UNSUPPORTED;\r
+      }\r
+    }\r
+  }\r
+\r
+  if (AddressIndex < ARRAY_SIZE (Address->Addr)) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  CopyMem (Address, &LocalAddress, sizeof (*Address));\r
+  if (PrefixLength != NULL) {\r
+    *PrefixLength = LocalPrefixLength;\r
+  }\r
+\r
+  if (EndPointer != NULL) {\r
+    *EndPointer = Pointer;\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  Convert a Null-terminated Unicode GUID string to a value of type\r
+  EFI_GUID.\r
+\r
+  This function outputs a GUID value by interpreting the contents of\r
+  the Unicode string specified by String. The format of the input\r
+  Unicode string String consists of 36 characters, as follows:\r
+\r
+                  aabbccdd-eeff-gghh-iijj-kkllmmnnoopp\r
+\r
+  The pairs aa - pp are two characters in the range [0-9], [a-f] and\r
+  [A-F], with each pair representing a single byte hexadecimal value.\r
+\r
+  The mapping between String and the EFI_GUID structure is as follows:\r
+                  aa          Data1[24:31]\r
+                  bb          Data1[16:23]\r
+                  cc          Data1[8:15]\r
+                  dd          Data1[0:7]\r
+                  ee          Data2[8:15]\r
+                  ff          Data2[0:7]\r
+                  gg          Data3[8:15]\r
+                  hh          Data3[0:7]\r
+                  ii          Data4[0:7]\r
+                  jj          Data4[8:15]\r
+                  kk          Data4[16:23]\r
+                  ll          Data4[24:31]\r
+                  mm          Data4[32:39]\r
+                  nn          Data4[40:47]\r
+                  oo          Data4[48:55]\r
+                  pp          Data4[56:63]\r
+\r
+  If String is not aligned in a 16-bit boundary, then ASSERT().\r
+\r
+  @param  String                   Pointer to a Null-terminated Unicode string.\r
+  @param  Guid                     Pointer to the converted GUID.\r
+\r
+  @retval RETURN_SUCCESS           Guid is translated from String.\r
+  @retval RETURN_INVALID_PARAMETER If String is NULL.\r
+                                   If Data is NULL.\r
+  @retval RETURN_UNSUPPORTED       If String is not as the above format.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+StrToGuid (\r
+  IN  CONST CHAR16  *String,\r
+  OUT GUID          *Guid\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+  GUID           LocalGuid;\r
+\r
+  ASSERT (((UINTN)String & BIT0) == 0);\r
+\r
+  //\r
+  // 1. None of String or Guid shall be a null pointer.\r
+  //\r
+  SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);\r
+  SAFE_STRING_CONSTRAINT_CHECK ((Guid != NULL), RETURN_INVALID_PARAMETER);\r
+\r
+  //\r
+  // Get aabbccdd in big-endian.\r
+  //\r
+  Status = StrHexToBytes (String, 2 * sizeof (LocalGuid.Data1), (UINT8 *)&LocalGuid.Data1, sizeof (LocalGuid.Data1));\r
+  if (RETURN_ERROR (Status) || (String[2 * sizeof (LocalGuid.Data1)] != L'-')) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // Convert big-endian to little-endian.\r
+  //\r
+  LocalGuid.Data1 = SwapBytes32 (LocalGuid.Data1);\r
+  String         += 2 * sizeof (LocalGuid.Data1) + 1;\r
+\r
+  //\r
+  // Get eeff in big-endian.\r
+  //\r
+  Status = StrHexToBytes (String, 2 * sizeof (LocalGuid.Data2), (UINT8 *)&LocalGuid.Data2, sizeof (LocalGuid.Data2));\r
+  if (RETURN_ERROR (Status) || (String[2 * sizeof (LocalGuid.Data2)] != L'-')) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // Convert big-endian to little-endian.\r
+  //\r
+  LocalGuid.Data2 = SwapBytes16 (LocalGuid.Data2);\r
+  String         += 2 * sizeof (LocalGuid.Data2) + 1;\r
+\r
+  //\r
+  // Get gghh in big-endian.\r
+  //\r
+  Status = StrHexToBytes (String, 2 * sizeof (LocalGuid.Data3), (UINT8 *)&LocalGuid.Data3, sizeof (LocalGuid.Data3));\r
+  if (RETURN_ERROR (Status) || (String[2 * sizeof (LocalGuid.Data3)] != L'-')) {\r
+    return RETURN_UNSUPPORTED;\r
   }\r
+\r
+  //\r
+  // Convert big-endian to little-endian.\r
+  //\r
+  LocalGuid.Data3 = SwapBytes16 (LocalGuid.Data3);\r
+  String         += 2 * sizeof (LocalGuid.Data3) + 1;\r
+\r
+  //\r
+  // Get iijj.\r
+  //\r
+  Status = StrHexToBytes (String, 2 * 2, &LocalGuid.Data4[0], 2);\r
+  if (RETURN_ERROR (Status) || (String[2 * 2] != L'-')) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  String += 2 * 2 + 1;\r
+\r
+  //\r
+  // Get kkllmmnnoopp.\r
+  //\r
+  Status = StrHexToBytes (String, 2 * 6, &LocalGuid.Data4[2], 6);\r
+  if (RETURN_ERROR (Status)) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  CopyGuid (Guid, &LocalGuid);\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  Convert a Null-terminated Unicode hexadecimal string to a byte array.\r
+\r
+  This function outputs a byte array by interpreting the contents of\r
+  the Unicode string specified by String in hexadecimal format. The format of\r
+  the input Unicode string String is:\r
+\r
+                  [XX]*\r
+\r
+  X is a hexadecimal digit character in the range [0-9], [a-f] and [A-F].\r
+  The function decodes every two hexadecimal digit characters as one byte. The\r
+  decoding stops after Length of characters and outputs Buffer containing\r
+  (Length / 2) bytes.\r
+\r
+  If String is not aligned in a 16-bit boundary, then ASSERT().\r
+\r
+  @param  String                   Pointer to a Null-terminated Unicode string.\r
+  @param  Length                   The number of Unicode characters to decode.\r
+  @param  Buffer                   Pointer to the converted bytes array.\r
+  @param  MaxBufferSize            The maximum size of Buffer.\r
+\r
+  @retval RETURN_SUCCESS           Buffer is translated from String.\r
+  @retval RETURN_INVALID_PARAMETER If String is NULL.\r
+                                   If Data is NULL.\r
+                                   If Length is not multiple of 2.\r
+                                   If PcdMaximumUnicodeStringLength is not zero,\r
+                                    and Length is greater than\r
+                                    PcdMaximumUnicodeStringLength.\r
+  @retval RETURN_UNSUPPORTED       If Length of characters from String contain\r
+                                    a character that is not valid hexadecimal\r
+                                    digit characters, or a Null-terminator.\r
+  @retval RETURN_BUFFER_TOO_SMALL  If MaxBufferSize is less than (Length / 2).\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+StrHexToBytes (\r
+  IN  CONST CHAR16  *String,\r
+  IN  UINTN         Length,\r
+  OUT UINT8         *Buffer,\r
+  IN  UINTN         MaxBufferSize\r
+  )\r
+{\r
+  UINTN  Index;\r
+\r
+  ASSERT (((UINTN)String & BIT0) == 0);\r
+\r
+  //\r
+  // 1. None of String or Buffer shall be a null pointer.\r
+  //\r
+  SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);\r
+  SAFE_STRING_CONSTRAINT_CHECK ((Buffer != NULL), RETURN_INVALID_PARAMETER);\r
+\r
+  //\r
+  // 2. Length shall not be greater than RSIZE_MAX.\r
+  //\r
+  if (RSIZE_MAX != 0) {\r
+    SAFE_STRING_CONSTRAINT_CHECK ((Length <= RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
+  }\r
+\r
+  //\r
+  // 3. Length shall not be odd.\r
+  //\r
+  SAFE_STRING_CONSTRAINT_CHECK (((Length & BIT0) == 0), RETURN_INVALID_PARAMETER);\r
+\r
+  //\r
+  // 4. MaxBufferSize shall equal to or greater than Length / 2.\r
+  //\r
+  SAFE_STRING_CONSTRAINT_CHECK ((MaxBufferSize >= Length / 2), RETURN_BUFFER_TOO_SMALL);\r
+\r
+  //\r
+  // 5. String shall not contains invalid hexadecimal digits.\r
+  //\r
+  for (Index = 0; Index < Length; Index++) {\r
+    if (!InternalIsHexaDecimalDigitCharacter (String[Index])) {\r
+      break;\r
+    }\r
+  }\r
+\r
+  if (Index != Length) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // Convert the hex string to bytes.\r
+  //\r
+  for (Index = 0; Index < Length; Index++) {\r
+    //\r
+    // For even characters, write the upper nibble for each buffer byte,\r
+    // and for even characters, the lower nibble.\r
+    //\r
+    if ((Index & BIT0) == 0) {\r
+      Buffer[Index / 2] = (UINT8)InternalHexCharToUintn (String[Index]) << 4;\r
+    } else {\r
+      Buffer[Index / 2] |= (UINT8)InternalHexCharToUintn (String[Index]);\r
+    }\r
+  }\r
+\r
   return RETURN_SUCCESS;\r
 }\r
 \r
@@ -1090,16 +1694,16 @@ StrHexToUint64S (
 UINTN\r
 EFIAPI\r
 AsciiStrnLenS (\r
-  IN CONST CHAR8               *String,\r
-  IN UINTN                     MaxSize\r
+  IN CONST CHAR8  *String,\r
+  IN UINTN        MaxSize\r
   )\r
 {\r
-  UINTN                             Length;\r
+  UINTN  Length;\r
 \r
   //\r
-  // If String is a null pointer, then the AsciiStrnLenS function returns zero.\r
+  // If String is a null pointer or MaxSize is 0, then the AsciiStrnLenS function returns zero.\r
   //\r
-  if (String == NULL) {\r
+  if ((String == NULL) || (MaxSize == 0)) {\r
     return 0;\r
   }\r
 \r
@@ -1114,8 +1718,10 @@ AsciiStrnLenS (
     if (Length >= MaxSize - 1) {\r
       return MaxSize;\r
     }\r
+\r
     Length++;\r
   }\r
+\r
   return Length;\r
 }\r
 \r
@@ -1141,8 +1747,8 @@ AsciiStrnLenS (
 UINTN\r
 EFIAPI\r
 AsciiStrnSizeS (\r
-  IN CONST CHAR8               *String,\r
-  IN UINTN                     MaxSize\r
+  IN CONST CHAR8  *String,\r
+  IN UINTN        MaxSize\r
   )\r
 {\r
   //\r
@@ -1169,8 +1775,6 @@ AsciiStrnSizeS (
 \r
   This function is similar as strcpy_s defined in C11.\r
 \r
-  If an error would be returned, then the function will also ASSERT().\r
-\r
   If an error is returned, then the Destination is unmodified.\r
 \r
   @param  Destination              A pointer to a Null-terminated Ascii string.\r
@@ -1183,7 +1787,7 @@ AsciiStrnSizeS (
   @retval RETURN_INVALID_PARAMETER If Destination is NULL.\r
                                    If Source is NULL.\r
                                    If PcdMaximumAsciiStringLength is not zero,\r
-                                    and DestMax is greater than \r
+                                    and DestMax is greater than\r
                                     PcdMaximumAsciiStringLength.\r
                                    If DestMax is 0.\r
   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.\r
@@ -1196,8 +1800,8 @@ AsciiStrCpyS (
   IN  CONST CHAR8  *Source\r
   )\r
 {\r
-  UINTN            SourceLen;\r
-  \r
+  UINTN  SourceLen;\r
+\r
   //\r
   // 1. Neither Destination nor Source shall be a null pointer.\r
   //\r
@@ -1234,6 +1838,7 @@ AsciiStrCpyS (
   while (*Source != 0) {\r
     *(Destination++) = *(Source++);\r
   }\r
+\r
   *Destination = 0;\r
 \r
   return RETURN_SUCCESS;\r
@@ -1246,8 +1851,6 @@ AsciiStrCpyS (
 \r
   This function is similar as strncpy_s defined in C11.\r
 \r
-  If an error would be returned, then the function will also ASSERT().\r
-\r
   If an error is returned, then the Destination is unmodified.\r
 \r
   @param  Destination              A pointer to a Null-terminated Ascii string.\r
@@ -1257,12 +1860,12 @@ AsciiStrCpyS (
   @param  Length                   The maximum number of Ascii characters to copy.\r
 \r
   @retval RETURN_SUCCESS           String is copied.\r
-  @retval RETURN_BUFFER_TOO_SMALL  If DestMax is NOT greater than \r
+  @retval RETURN_BUFFER_TOO_SMALL  If DestMax is NOT greater than\r
                                    MIN(StrLen(Source), Length).\r
   @retval RETURN_INVALID_PARAMETER If Destination is NULL.\r
                                    If Source is NULL.\r
                                    If PcdMaximumAsciiStringLength is not zero,\r
-                                    and DestMax is greater than \r
+                                    and DestMax is greater than\r
                                     PcdMaximumAsciiStringLength.\r
                                    If DestMax is 0.\r
   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.\r
@@ -1276,7 +1879,7 @@ AsciiStrnCpyS (
   IN  UINTN        Length\r
   )\r
 {\r
-  UINTN            SourceLen;\r
+  UINTN  SourceLen;\r
 \r
   //\r
   // 1. Neither Destination nor Source shall be a null pointer.\r
@@ -1300,7 +1903,7 @@ AsciiStrnCpyS (
   //\r
   // 4. If Length is not less than DestMax, then DestMax shall be greater than AsciiStrnLenS(Source, DestMax).\r
   //\r
-  SourceLen = AsciiStrnLenS (Source, DestMax);\r
+  SourceLen = AsciiStrnLenS (Source, MIN (DestMax, Length));\r
   if (Length >= DestMax) {\r
     SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL);\r
   }\r
@@ -1311,6 +1914,7 @@ AsciiStrnCpyS (
   if (SourceLen > Length) {\r
     SourceLen = Length;\r
   }\r
+\r
   SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoAsciiStrOverlap (Destination, DestMax, (CHAR8 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);\r
 \r
   //\r
@@ -1319,10 +1923,11 @@ AsciiStrnCpyS (
   // pointed to by Destination. If no null character was copied from Source, then Destination[Length] is set to a null\r
   // character.\r
   //\r
-  while ((*Source != 0) && (SourceLen > 0)) {\r
+  while ((SourceLen > 0) && (*Source != 0)) {\r
     *(Destination++) = *(Source++);\r
     SourceLen--;\r
   }\r
+\r
   *Destination = 0;\r
 \r
   return RETURN_SUCCESS;\r
@@ -1334,8 +1939,6 @@ AsciiStrnCpyS (
 \r
   This function is similar as strcat_s defined in C11.\r
 \r
-  If an error would be returned, then the function will also ASSERT().\r
-\r
   If an error is returned, then the Destination is unmodified.\r
 \r
   @param  Destination              A pointer to a Null-terminated Ascii string.\r
@@ -1344,14 +1947,14 @@ AsciiStrnCpyS (
   @param  Source                   A pointer to a Null-terminated Ascii string.\r
 \r
   @retval RETURN_SUCCESS           String is appended.\r
-  @retval RETURN_BAD_BUFFER_SIZE   If DestMax is NOT greater than \r
+  @retval RETURN_BAD_BUFFER_SIZE   If DestMax is NOT greater than\r
                                    StrLen(Destination).\r
   @retval RETURN_BUFFER_TOO_SMALL  If (DestMax - StrLen(Destination)) is NOT\r
                                    greater than StrLen(Source).\r
   @retval RETURN_INVALID_PARAMETER If Destination is NULL.\r
                                    If Source is NULL.\r
                                    If PcdMaximumAsciiStringLength is not zero,\r
-                                    and DestMax is greater than \r
+                                    and DestMax is greater than\r
                                     PcdMaximumAsciiStringLength.\r
                                    If DestMax is 0.\r
   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.\r
@@ -1364,10 +1967,10 @@ AsciiStrCatS (
   IN     CONST CHAR8  *Source\r
   )\r
 {\r
-  UINTN               DestLen;\r
-  UINTN               CopyLen;\r
-  UINTN               SourceLen;\r
-  \r
+  UINTN  DestLen;\r
+  UINTN  CopyLen;\r
+  UINTN  SourceLen;\r
+\r
   //\r
   // Let CopyLen denote the value DestMax - AsciiStrnLenS(Destination, DestMax) upon entry to AsciiStrCatS.\r
   //\r
@@ -1417,6 +2020,7 @@ AsciiStrCatS (
   while (*Source != 0) {\r
     *(Destination++) = *(Source++);\r
   }\r
+\r
   *Destination = 0;\r
 \r
   return RETURN_SUCCESS;\r
@@ -1430,8 +2034,6 @@ AsciiStrCatS (
 \r
   This function is similar as strncat_s defined in C11.\r
 \r
-  If an error would be returned, then the function will also ASSERT().\r
-\r
   If an error is returned, then the Destination is unmodified.\r
 \r
   @param  Destination              A pointer to a Null-terminated Ascii string.\r
@@ -1448,7 +2050,7 @@ AsciiStrCatS (
   @retval RETURN_INVALID_PARAMETER If Destination is NULL.\r
                                    If Source is NULL.\r
                                    If PcdMaximumAsciiStringLength is not zero,\r
-                                    and DestMax is greater than \r
+                                    and DestMax is greater than\r
                                     PcdMaximumAsciiStringLength.\r
                                    If DestMax is 0.\r
   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.\r
@@ -1462,10 +2064,10 @@ AsciiStrnCatS (
   IN     UINTN        Length\r
   )\r
 {\r
-  UINTN               DestLen;\r
-  UINTN               CopyLen;\r
-  UINTN               SourceLen;\r
-  \r
+  UINTN  DestLen;\r
+  UINTN  CopyLen;\r
+  UINTN  SourceLen;\r
+\r
   //\r
   // Let CopyLen denote the value DestMax - AsciiStrnLenS(Destination, DestMax) upon entry to AsciiStrnCatS.\r
   //\r
@@ -1499,7 +2101,7 @@ AsciiStrnCatS (
   //\r
   // 5. If Length is not less than CopyLen, then CopyLen shall be greater than AsciiStrnLenS(Source, CopyLen).\r
   //\r
-  SourceLen = AsciiStrnLenS (Source, CopyLen);\r
+  SourceLen = AsciiStrnLenS (Source, MIN (CopyLen, Length));\r
   if (Length >= CopyLen) {\r
     SAFE_STRING_CONSTRAINT_CHECK ((CopyLen > SourceLen), RETURN_BUFFER_TOO_SMALL);\r
   }\r
@@ -1510,6 +2112,7 @@ AsciiStrnCatS (
   if (SourceLen > Length) {\r
     SourceLen = Length;\r
   }\r
+\r
   SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoAsciiStrOverlap (Destination, DestMax, (CHAR8 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);\r
 \r
   //\r
@@ -1520,10 +2123,11 @@ AsciiStrnCatS (
   // a null character.\r
   //\r
   Destination = Destination + DestLen;\r
-  while ((*Source != 0) && (SourceLen > 0)) {\r
+  while ((SourceLen > 0) && (*Source != 0)) {\r
     *(Destination++) = *(Source++);\r
     SourceLen--;\r
   }\r
+\r
   *Destination = 0;\r
 \r
   return RETURN_SUCCESS;\r
@@ -1544,12 +2148,6 @@ AsciiStrnCatS (
   be ignored. Then, the function stops at the first character that is a not a\r
   valid decimal character or a Null-terminator, whichever one comes first.\r
 \r
-  If String is NULL, then ASSERT().\r
-  If Data is NULL, then ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
-  PcdMaximumAsciiStringLength Ascii characters, not including the\r
-  Null-terminator, then ASSERT().\r
-\r
   If String has no valid decimal digits in the above format, then 0 is stored\r
   at the location pointed to by Data.\r
   If the number represented by String exceeds the range defined by UINTN, then\r
@@ -1579,9 +2177,9 @@ AsciiStrnCatS (
 RETURN_STATUS\r
 EFIAPI\r
 AsciiStrDecimalToUintnS (\r
-  IN  CONST CHAR8              *String,\r
-  OUT       CHAR8              **EndPointer,  OPTIONAL\r
-  OUT       UINTN              *Data\r
+  IN  CONST CHAR8  *String,\r
+  OUT       CHAR8  **EndPointer   OPTIONAL,\r
+  OUT       UINTN  *Data\r
   )\r
 {\r
   //\r
@@ -1598,7 +2196,7 @@ AsciiStrDecimalToUintnS (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR8 *) String;\r
+    *EndPointer = (CHAR8 *)String;\r
   }\r
 \r
   //\r
@@ -1626,8 +2224,9 @@ AsciiStrDecimalToUintnS (
     if (*Data > ((MAX_UINTN - (*String - '0')) / 10)) {\r
       *Data = MAX_UINTN;\r
       if (EndPointer != NULL) {\r
-        *EndPointer = (CHAR8 *) String;\r
+        *EndPointer = (CHAR8 *)String;\r
       }\r
+\r
       return RETURN_UNSUPPORTED;\r
     }\r
 \r
@@ -1636,8 +2235,9 @@ AsciiStrDecimalToUintnS (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR8 *) String;\r
+    *EndPointer = (CHAR8 *)String;\r
   }\r
+\r
   return RETURN_SUCCESS;\r
 }\r
 \r
@@ -1656,12 +2256,6 @@ AsciiStrDecimalToUintnS (
   be ignored. Then, the function stops at the first character that is a not a\r
   valid decimal character or a Null-terminator, whichever one comes first.\r
 \r
-  If String is NULL, then ASSERT().\r
-  If Data is NULL, then ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
-  PcdMaximumAsciiStringLength Ascii characters, not including the\r
-  Null-terminator, then ASSERT().\r
-\r
   If String has no valid decimal digits in the above format, then 0 is stored\r
   at the location pointed to by Data.\r
   If the number represented by String exceeds the range defined by UINT64, then\r
@@ -1691,9 +2285,9 @@ AsciiStrDecimalToUintnS (
 RETURN_STATUS\r
 EFIAPI\r
 AsciiStrDecimalToUint64S (\r
-  IN  CONST CHAR8              *String,\r
-  OUT       CHAR8              **EndPointer,  OPTIONAL\r
-  OUT       UINT64             *Data\r
+  IN  CONST CHAR8   *String,\r
+  OUT       CHAR8   **EndPointer   OPTIONAL,\r
+  OUT       UINT64  *Data\r
   )\r
 {\r
   //\r
@@ -1710,7 +2304,7 @@ AsciiStrDecimalToUint64S (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR8 *) String;\r
+    *EndPointer = (CHAR8 *)String;\r
   }\r
 \r
   //\r
@@ -1738,8 +2332,9 @@ AsciiStrDecimalToUint64S (
     if (*Data > DivU64x32 (MAX_UINT64 - (*String - '0'), 10)) {\r
       *Data = MAX_UINT64;\r
       if (EndPointer != NULL) {\r
-        *EndPointer = (CHAR8 *) String;\r
+        *EndPointer = (CHAR8 *)String;\r
       }\r
+\r
       return RETURN_UNSUPPORTED;\r
     }\r
 \r
@@ -1748,8 +2343,9 @@ AsciiStrDecimalToUint64S (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR8 *) String;\r
+    *EndPointer = (CHAR8 *)String;\r
   }\r
+\r
   return RETURN_SUCCESS;\r
 }\r
 \r
@@ -1772,12 +2368,6 @@ AsciiStrDecimalToUint64S (
   character that is a not a valid hexadecimal character or Null-terminator,\r
   whichever on comes first.\r
 \r
-  If String is NULL, then ASSERT().\r
-  If Data is NULL, then ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
-  PcdMaximumAsciiStringLength Ascii characters, not including the\r
-  Null-terminator, then ASSERT().\r
-\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
@@ -1807,11 +2397,14 @@ AsciiStrDecimalToUint64S (
 RETURN_STATUS\r
 EFIAPI\r
 AsciiStrHexToUintnS (\r
-  IN  CONST CHAR8              *String,\r
-  OUT       CHAR8              **EndPointer,  OPTIONAL\r
-  OUT       UINTN              *Data\r
+  IN  CONST CHAR8  *String,\r
+  OUT       CHAR8  **EndPointer   OPTIONAL,\r
+  OUT       UINTN  *Data\r
   )\r
 {\r
+  BOOLEAN  FoundLeadingZero;\r
+\r
+  FoundLeadingZero = FALSE;\r
   //\r
   // 1. Neither String nor Data shall be a null pointer.\r
   //\r
@@ -1826,7 +2419,7 @@ AsciiStrHexToUintnS (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR8 *) String;\r
+    *EndPointer = (CHAR8 *)String;\r
   }\r
 \r
   //\r
@@ -1839,15 +2432,17 @@ AsciiStrHexToUintnS (
   //\r
   // Ignore leading Zeros after the spaces\r
   //\r
+  FoundLeadingZero = *String == '0';\r
   while (*String == '0') {\r
     String++;\r
   }\r
 \r
-  if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
-    if (*(String - 1) != '0') {\r
+  if (AsciiCharToUpper (*String) == 'X') {\r
+    if (!FoundLeadingZero) {\r
       *Data = 0;\r
       return RETURN_SUCCESS;\r
     }\r
+\r
     //\r
     // Skip the 'X'\r
     //\r
@@ -1865,8 +2460,9 @@ AsciiStrHexToUintnS (
     if (*Data > ((MAX_UINTN - InternalAsciiHexCharToUintn (*String)) >> 4)) {\r
       *Data = MAX_UINTN;\r
       if (EndPointer != NULL) {\r
-        *EndPointer = (CHAR8 *) String;\r
+        *EndPointer = (CHAR8 *)String;\r
       }\r
+\r
       return RETURN_UNSUPPORTED;\r
     }\r
 \r
@@ -1875,8 +2471,9 @@ AsciiStrHexToUintnS (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR8 *) String;\r
+    *EndPointer = (CHAR8 *)String;\r
   }\r
+\r
   return RETURN_SUCCESS;\r
 }\r
 \r
@@ -1899,12 +2496,6 @@ AsciiStrHexToUintnS (
   character that is a not a valid hexadecimal character or Null-terminator,\r
   whichever on comes first.\r
 \r
-  If String is NULL, then ASSERT().\r
-  If Data is NULL, then ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
-  PcdMaximumAsciiStringLength Ascii characters, not including the\r
-  Null-terminator, then ASSERT().\r
-\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 UINT64, then\r
@@ -1934,11 +2525,14 @@ AsciiStrHexToUintnS (
 RETURN_STATUS\r
 EFIAPI\r
 AsciiStrHexToUint64S (\r
-  IN  CONST CHAR8              *String,\r
-  OUT       CHAR8              **EndPointer,  OPTIONAL\r
-  OUT       UINT64             *Data\r
+  IN  CONST CHAR8   *String,\r
+  OUT       CHAR8   **EndPointer   OPTIONAL,\r
+  OUT       UINT64  *Data\r
   )\r
 {\r
+  BOOLEAN  FoundLeadingZero;\r
+\r
+  FoundLeadingZero = FALSE;\r
   //\r
   // 1. Neither String nor Data shall be a null pointer.\r
   //\r
@@ -1953,7 +2547,7 @@ AsciiStrHexToUint64S (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR8 *) String;\r
+    *EndPointer = (CHAR8 *)String;\r
   }\r
 \r
   //\r
@@ -1966,15 +2560,17 @@ AsciiStrHexToUint64S (
   //\r
   // Ignore leading Zeros after the spaces\r
   //\r
+  FoundLeadingZero = *String == '0';\r
   while (*String == '0') {\r
     String++;\r
   }\r
 \r
-  if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
-    if (*(String - 1) != '0') {\r
+  if (AsciiCharToUpper (*String) == 'X') {\r
+    if (!FoundLeadingZero) {\r
       *Data = 0;\r
       return RETURN_SUCCESS;\r
     }\r
+\r
     //\r
     // Skip the 'X'\r
     //\r
@@ -1992,8 +2588,9 @@ AsciiStrHexToUint64S (
     if (*Data > RShiftU64 (MAX_UINT64 - InternalAsciiHexCharToUintn (*String), 4)) {\r
       *Data = MAX_UINT64;\r
       if (EndPointer != NULL) {\r
-        *EndPointer = (CHAR8 *) String;\r
+        *EndPointer = (CHAR8 *)String;\r
       }\r
+\r
       return RETURN_UNSUPPORTED;\r
     }\r
 \r
@@ -2002,8 +2599,9 @@ AsciiStrHexToUint64S (
   }\r
 \r
   if (EndPointer != NULL) {\r
-    *EndPointer = (CHAR8 *) String;\r
+    *EndPointer = (CHAR8 *)String;\r
   }\r
+\r
   return RETURN_SUCCESS;\r
 }\r
 \r
@@ -2025,7 +2623,6 @@ AsciiStrHexToUint64S (
   the upper 8 bits, then ASSERT().\r
 \r
   If Source is not aligned on a 16-bit boundary, then ASSERT().\r
-  If an error would be returned, then the function will also ASSERT().\r
 \r
   If an error is returned, then the Destination is unmodified.\r
 \r
@@ -2051,14 +2648,14 @@ AsciiStrHexToUint64S (
 RETURN_STATUS\r
 EFIAPI\r
 UnicodeStrToAsciiStrS (\r
-  IN      CONST CHAR16              *Source,\r
-  OUT     CHAR8                     *Destination,\r
-  IN      UINTN                     DestMax\r
+  IN      CONST CHAR16  *Source,\r
+  OUT     CHAR8         *Destination,\r
+  IN      UINTN         DestMax\r
   )\r
 {\r
-  UINTN            SourceLen;\r
+  UINTN  SourceLen;\r
 \r
-  ASSERT (((UINTN) Source & BIT0) == 0);\r
+  ASSERT (((UINTN)Source & BIT0) == 0);\r
 \r
   //\r
   // 1. Neither Destination nor Source shall be a null pointer.\r
@@ -2072,6 +2669,7 @@ UnicodeStrToAsciiStrS (
   if (ASCII_RSIZE_MAX != 0) {\r
     SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
   }\r
+\r
   if (RSIZE_MAX != 0) {\r
     SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
   }\r
@@ -2090,7 +2688,7 @@ UnicodeStrToAsciiStrS (
   //\r
   // 5. Copying shall not take place between objects that overlap.\r
   //\r
-  SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination, DestMax, (VOID *)Source, (SourceLen + 1) * sizeof(CHAR16)), RETURN_ACCESS_DENIED);\r
+  SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination, DestMax, (VOID *)Source, (SourceLen + 1) * sizeof (CHAR16)), RETURN_ACCESS_DENIED);\r
 \r
   //\r
   // convert string\r
@@ -2101,8 +2699,9 @@ UnicodeStrToAsciiStrS (
     // non-zero value in the upper 8 bits, then ASSERT().\r
     //\r
     ASSERT (*Source < 0x100);\r
-    *(Destination++) = (CHAR8) *(Source++);\r
+    *(Destination++) = (CHAR8)*(Source++);\r
   }\r
+\r
   *Destination = '\0';\r
 \r
   return RETURN_SUCCESS;\r
@@ -2125,7 +2724,6 @@ UnicodeStrToAsciiStrS (
   If any Unicode characters in Source contain non-zero value in the upper 8\r
   bits, then ASSERT().\r
   If Source is not aligned on a 16-bit boundary, then ASSERT().\r
-  If an error would be returned, then the function will also ASSERT().\r
 \r
   If an error is returned, then Destination and DestinationLength are\r
   unmodified.\r
@@ -2157,16 +2755,16 @@ UnicodeStrToAsciiStrS (
 RETURN_STATUS\r
 EFIAPI\r
 UnicodeStrnToAsciiStrS (\r
-  IN      CONST CHAR16              *Source,\r
-  IN      UINTN                     Length,\r
-  OUT     CHAR8                     *Destination,\r
-  IN      UINTN                     DestMax,\r
-  OUT     UINTN                     *DestinationLength\r
+  IN      CONST CHAR16  *Source,\r
+  IN      UINTN         Length,\r
+  OUT     CHAR8         *Destination,\r
+  IN      UINTN         DestMax,\r
+  OUT     UINTN         *DestinationLength\r
   )\r
 {\r
-  UINTN            SourceLen;\r
+  UINTN  SourceLen;\r
 \r
-  ASSERT (((UINTN) Source & BIT0) == 0);\r
+  ASSERT (((UINTN)Source & BIT0) == 0);\r
 \r
   //\r
   // 1. None of Destination, Source or DestinationLength shall be a null\r
@@ -2184,6 +2782,7 @@ UnicodeStrnToAsciiStrS (
     SAFE_STRING_CONSTRAINT_CHECK ((Length <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
     SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
   }\r
+\r
   if (RSIZE_MAX != 0) {\r
     SAFE_STRING_CONSTRAINT_CHECK ((Length <= RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
     SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
@@ -2209,7 +2808,8 @@ UnicodeStrnToAsciiStrS (
   if (SourceLen > Length) {\r
     SourceLen = Length;\r
   }\r
-  SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination, DestMax, (VOID *)Source, (SourceLen + 1) * sizeof(CHAR16)), RETURN_ACCESS_DENIED);\r
+\r
+  SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination, DestMax, (VOID *)Source, (SourceLen + 1) * sizeof (CHAR16)), RETURN_ACCESS_DENIED);\r
 \r
   *DestinationLength = 0;\r
 \r
@@ -2222,10 +2822,11 @@ UnicodeStrnToAsciiStrS (
     // 8 bits, then ASSERT().\r
     //\r
     ASSERT (*Source < 0x100);\r
-    *(Destination++) = (CHAR8) *(Source++);\r
+    *(Destination++) = (CHAR8)*(Source++);\r
     SourceLen--;\r
     (*DestinationLength)++;\r
   }\r
+\r
   *Destination = 0;\r
 \r
   return RETURN_SUCCESS;\r
@@ -2245,7 +2846,6 @@ UnicodeStrnToAsciiStrS (
   equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.\r
 \r
   If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
-  If an error would be returned, then the function will also ASSERT().\r
 \r
   If an error is returned, then the Destination is unmodified.\r
 \r
@@ -2271,14 +2871,14 @@ UnicodeStrnToAsciiStrS (
 RETURN_STATUS\r
 EFIAPI\r
 AsciiStrToUnicodeStrS (\r
-  IN      CONST CHAR8               *Source,\r
-  OUT     CHAR16                    *Destination,\r
-  IN      UINTN                     DestMax\r
+  IN      CONST CHAR8  *Source,\r
+  OUT     CHAR16       *Destination,\r
+  IN      UINTN        DestMax\r
   )\r
 {\r
-  UINTN            SourceLen;\r
+  UINTN  SourceLen;\r
 \r
-  ASSERT (((UINTN) Destination & BIT0) == 0);\r
+  ASSERT (((UINTN)Destination & BIT0) == 0);\r
 \r
   //\r
   // 1. Neither Destination nor Source shall be a null pointer.\r
@@ -2292,6 +2892,7 @@ AsciiStrToUnicodeStrS (
   if (RSIZE_MAX != 0) {\r
     SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
   }\r
+\r
   if (ASCII_RSIZE_MAX != 0) {\r
     SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
   }\r
@@ -2310,14 +2911,15 @@ AsciiStrToUnicodeStrS (
   //\r
   // 5. Copying shall not take place between objects that overlap.\r
   //\r
-  SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination, DestMax * sizeof(CHAR16), (VOID *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);\r
+  SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination, DestMax * sizeof (CHAR16), (VOID *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);\r
 \r
   //\r
   // Convert string\r
   //\r
   while (*Source != '\0') {\r
-    *(Destination++) = (CHAR16)*(Source++);\r
+    *(Destination++) = (CHAR16)(UINT8)*(Source++);\r
   }\r
+\r
   *Destination = '\0';\r
 \r
   return RETURN_SUCCESS;\r
@@ -2338,7 +2940,6 @@ AsciiStrToUnicodeStrS (
   ((MIN(AsciiStrLen(Source), Length) + 1) * sizeof (CHAR8)) in bytes.\r
 \r
   If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
-  If an error would be returned, then the function will also ASSERT().\r
 \r
   If an error is returned, then Destination and DestinationLength are\r
   unmodified.\r
@@ -2369,16 +2970,16 @@ AsciiStrToUnicodeStrS (
 RETURN_STATUS\r
 EFIAPI\r
 AsciiStrnToUnicodeStrS (\r
-  IN      CONST CHAR8               *Source,\r
-  IN      UINTN                     Length,\r
-  OUT     CHAR16                    *Destination,\r
-  IN      UINTN                     DestMax,\r
-  OUT     UINTN                     *DestinationLength\r
+  IN      CONST CHAR8  *Source,\r
+  IN      UINTN        Length,\r
+  OUT     CHAR16       *Destination,\r
+  IN      UINTN        DestMax,\r
+  OUT     UINTN        *DestinationLength\r
   )\r
 {\r
-  UINTN            SourceLen;\r
+  UINTN  SourceLen;\r
 \r
-  ASSERT (((UINTN) Destination & BIT0) == 0);\r
+  ASSERT (((UINTN)Destination & BIT0) == 0);\r
 \r
   //\r
   // 1. None of Destination, Source or DestinationLength shall be a null\r
@@ -2396,6 +2997,7 @@ AsciiStrnToUnicodeStrS (
     SAFE_STRING_CONSTRAINT_CHECK ((Length <= RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
     SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
   }\r
+\r
   if (ASCII_RSIZE_MAX != 0) {\r
     SAFE_STRING_CONSTRAINT_CHECK ((Length <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
     SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
@@ -2421,7 +3023,8 @@ AsciiStrnToUnicodeStrS (
   if (SourceLen > Length) {\r
     SourceLen = Length;\r
   }\r
-  SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination, DestMax * sizeof(CHAR16), (VOID *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);\r
+\r
+  SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination, DestMax * sizeof (CHAR16), (VOID *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);\r
 \r
   *DestinationLength = 0;\r
 \r
@@ -2429,11 +3032,599 @@ AsciiStrnToUnicodeStrS (
   // Convert string\r
   //\r
   while ((*Source != 0) && (SourceLen > 0)) {\r
-    *(Destination++) = (CHAR16)*(Source++);\r
+    *(Destination++) = (CHAR16)(UINT8)*(Source++);\r
     SourceLen--;\r
     (*DestinationLength)++;\r
   }\r
+\r
   *Destination = 0;\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
+/**\r
+  Convert a Null-terminated ASCII string to IPv6 address and prefix length.\r
+\r
+  This function outputs a value of type IPv6_ADDRESS and may output a value\r
+  of type UINT8 by interpreting the contents of the ASCII string specified\r
+  by String. The format of the input ASCII string String is as follows:\r
+\r
+                  X:X:X:X:X:X:X:X[/P]\r
+\r
+  X contains one to four hexadecimal digit characters in the range [0-9], [a-f] and\r
+  [A-F]. X is converted to a value of type UINT16, whose low byte is stored in low\r
+  memory address and high byte is stored in high memory address. P contains decimal\r
+  digit characters in the range [0-9]. The running zero in the beginning of P will\r
+  be ignored. /P is optional.\r
+\r
+  When /P is not in the String, the function stops at the first character that is\r
+  not a valid hexadecimal digit character after eight X's are converted.\r
+\r
+  When /P is in the String, the function stops at the first character that is not\r
+  a valid decimal digit character after P is converted.\r
+\r
+  "::" can be used to compress one or more groups of X when X contains only 0.\r
+  The "::" can only appear once in the String.\r
+\r
+  If EndPointer is not NULL and Address is translated from String, a pointer\r
+  to the character that stopped the scan is stored at the location pointed to\r
+  by EndPointer.\r
+\r
+  @param  String                   Pointer to a Null-terminated ASCII string.\r
+  @param  EndPointer               Pointer to character that stops scan.\r
+  @param  Address                  Pointer to the converted IPv6 address.\r
+  @param  PrefixLength             Pointer to the converted IPv6 address prefix\r
+                                   length. MAX_UINT8 is returned when /P is\r
+                                   not in the String.\r
+\r
+  @retval RETURN_SUCCESS           Address is translated from String.\r
+  @retval RETURN_INVALID_PARAMETER If String is NULL.\r
+                                   If Data is NULL.\r
+  @retval RETURN_UNSUPPORTED       If X contains more than four hexadecimal\r
+                                    digit characters.\r
+                                   If String contains "::" and number of X\r
+                                    is not less than 8.\r
+                                   If P starts with character that is not a\r
+                                    valid decimal digit character.\r
+                                   If the decimal number converted from P\r
+                                    exceeds 128.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+AsciiStrToIpv6Address (\r
+  IN  CONST CHAR8   *String,\r
+  OUT CHAR8         **EndPointer  OPTIONAL,\r
+  OUT IPv6_ADDRESS  *Address,\r
+  OUT UINT8         *PrefixLength OPTIONAL\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+  UINTN          AddressIndex;\r
+  UINTN          Uintn;\r
+  IPv6_ADDRESS   LocalAddress;\r
+  UINT8          LocalPrefixLength;\r
+  CONST CHAR8    *Pointer;\r
+  CHAR8          *End;\r
+  UINTN          CompressStart;\r
+  BOOLEAN        ExpectPrefix;\r
+\r
+  LocalPrefixLength = MAX_UINT8;\r
+  CompressStart     = ARRAY_SIZE (Address->Addr);\r
+  ExpectPrefix      = FALSE;\r
+\r
+  //\r
+  // None of String or Address shall be a null pointer.\r
+  //\r
+  SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);\r
+  SAFE_STRING_CONSTRAINT_CHECK ((Address != NULL), RETURN_INVALID_PARAMETER);\r
+\r
+  for (Pointer = String, AddressIndex = 0; AddressIndex < ARRAY_SIZE (Address->Addr) + 1;) {\r
+    if (!InternalAsciiIsHexaDecimalDigitCharacter (*Pointer)) {\r
+      if (*Pointer != ':') {\r
+        //\r
+        // ":" or "/" should be followed by digit characters.\r
+        //\r
+        return RETURN_UNSUPPORTED;\r
+      }\r
+\r
+      //\r
+      // Meet second ":" after previous ":" or "/"\r
+      // or meet first ":" in the beginning of String.\r
+      //\r
+      if (ExpectPrefix) {\r
+        //\r
+        // ":" shall not be after "/"\r
+        //\r
+        return RETURN_UNSUPPORTED;\r
+      }\r
+\r
+      if ((CompressStart != ARRAY_SIZE (Address->Addr)) || (AddressIndex == ARRAY_SIZE (Address->Addr))) {\r
+        //\r
+        // "::" can only appear once.\r
+        // "::" can only appear when address is not full length.\r
+        //\r
+        return RETURN_UNSUPPORTED;\r
+      } else {\r
+        //\r
+        // Remember the start of zero compressing.\r
+        //\r
+        CompressStart = AddressIndex;\r
+        Pointer++;\r
+\r
+        if (CompressStart == 0) {\r
+          if (*Pointer != ':') {\r
+            //\r
+            // Single ":" shall not be in the beginning of String.\r
+            //\r
+            return RETURN_UNSUPPORTED;\r
+          }\r
+\r
+          Pointer++;\r
+        }\r
+      }\r
+    }\r
+\r
+    if (!InternalAsciiIsHexaDecimalDigitCharacter (*Pointer)) {\r
+      if (*Pointer == '/') {\r
+        //\r
+        // Might be optional "/P" after "::".\r
+        //\r
+        if (CompressStart != AddressIndex) {\r
+          return RETURN_UNSUPPORTED;\r
+        }\r
+      } else {\r
+        break;\r
+      }\r
+    } else {\r
+      if (!ExpectPrefix) {\r
+        //\r
+        // Get X.\r
+        //\r
+        Status = AsciiStrHexToUintnS (Pointer, &End, &Uintn);\r
+        if (RETURN_ERROR (Status) || (End - Pointer > 4)) {\r
+          //\r
+          // Number of hexadecimal digit characters is no more than 4.\r
+          //\r
+          return RETURN_UNSUPPORTED;\r
+        }\r
+\r
+        Pointer = End;\r
+        //\r
+        // Uintn 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
+        AddressIndex                       += 2;\r
+      } else {\r
+        //\r
+        // Get P, then exit the loop.\r
+        //\r
+        Status = AsciiStrDecimalToUintnS (Pointer, &End, &Uintn);\r
+        if (RETURN_ERROR (Status) || (End == Pointer) || (Uintn > 128)) {\r
+          //\r
+          // Prefix length should not exceed 128.\r
+          //\r
+          return RETURN_UNSUPPORTED;\r
+        }\r
+\r
+        LocalPrefixLength = (UINT8)Uintn;\r
+        Pointer           = End;\r
+        break;\r
+      }\r
+    }\r
+\r
+    //\r
+    // Skip ':' or "/"\r
+    //\r
+    if (*Pointer == '/') {\r
+      ExpectPrefix = TRUE;\r
+    } else if (*Pointer == ':') {\r
+      if (AddressIndex == ARRAY_SIZE (Address->Addr)) {\r
+        //\r
+        // Meet additional ":" after all 8 16-bit address\r
+        //\r
+        break;\r
+      }\r
+    } else {\r
+      //\r
+      // Meet other character that is not "/" or ":" after all 8 16-bit address\r
+      //\r
+      break;\r
+    }\r
+\r
+    Pointer++;\r
+  }\r
+\r
+  if (((AddressIndex == ARRAY_SIZE (Address->Addr)) && (CompressStart != ARRAY_SIZE (Address->Addr))) ||\r
+      ((AddressIndex != ARRAY_SIZE (Address->Addr)) && (CompressStart == ARRAY_SIZE (Address->Addr)))\r
+      )\r
+  {\r
+    //\r
+    // Full length of address shall not have compressing zeros.\r
+    // Non-full length of address shall have compressing zeros.\r
+    //\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  CopyMem (&Address->Addr[0], &LocalAddress.Addr[0], CompressStart);\r
+  ZeroMem (&Address->Addr[CompressStart], ARRAY_SIZE (Address->Addr) - AddressIndex);\r
+  if (AddressIndex > CompressStart) {\r
+    CopyMem (\r
+      &Address->Addr[CompressStart + ARRAY_SIZE (Address->Addr) - AddressIndex],\r
+      &LocalAddress.Addr[CompressStart],\r
+      AddressIndex - CompressStart\r
+      );\r
+  }\r
+\r
+  if (PrefixLength != NULL) {\r
+    *PrefixLength = LocalPrefixLength;\r
+  }\r
+\r
+  if (EndPointer != NULL) {\r
+    *EndPointer = (CHAR8 *)Pointer;\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  Convert a Null-terminated ASCII string to IPv4 address and prefix length.\r
+\r
+  This function outputs a value of type IPv4_ADDRESS and may output a value\r
+  of type UINT8 by interpreting the contents of the ASCII string specified\r
+  by String. The format of the input ASCII string String is as follows:\r
+\r
+                  D.D.D.D[/P]\r
+\r
+  D and P are decimal digit characters in the range [0-9]. The running zero in\r
+  the beginning of D and P will be ignored. /P is optional.\r
+\r
+  When /P is not in the String, the function stops at the first character that is\r
+  not a valid decimal digit character after four D's are converted.\r
+\r
+  When /P is in the String, the function stops at the first character that is not\r
+  a valid decimal digit character after P is converted.\r
+\r
+  If EndPointer is not NULL and Address is translated from String, a pointer\r
+  to the character that stopped the scan is stored at the location pointed to\r
+  by EndPointer.\r
+\r
+  @param  String                   Pointer to a Null-terminated ASCII string.\r
+  @param  EndPointer               Pointer to character that stops scan.\r
+  @param  Address                  Pointer to the converted IPv4 address.\r
+  @param  PrefixLength             Pointer to the converted IPv4 address prefix\r
+                                   length. MAX_UINT8 is returned when /P is\r
+                                   not in the String.\r
+\r
+  @retval RETURN_SUCCESS           Address is translated from String.\r
+  @retval RETURN_INVALID_PARAMETER If String is NULL.\r
+                                   If Data is NULL.\r
+  @retval RETURN_UNSUPPORTED       If String is not in the correct format.\r
+                                   If any decimal number converted from D\r
+                                    exceeds 255.\r
+                                   If the decimal number converted from P\r
+                                    exceeds 32.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+AsciiStrToIpv4Address (\r
+  IN  CONST CHAR8   *String,\r
+  OUT CHAR8         **EndPointer  OPTIONAL,\r
+  OUT IPv4_ADDRESS  *Address,\r
+  OUT UINT8         *PrefixLength OPTIONAL\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+  UINTN          AddressIndex;\r
+  UINTN          Uintn;\r
+  IPv4_ADDRESS   LocalAddress;\r
+  UINT8          LocalPrefixLength;\r
+  CHAR8          *Pointer;\r
+\r
+  LocalPrefixLength = MAX_UINT8;\r
+\r
+  //\r
+  // None of String or Address shall be a null pointer.\r
+  //\r
+  SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);\r
+  SAFE_STRING_CONSTRAINT_CHECK ((Address != NULL), RETURN_INVALID_PARAMETER);\r
+\r
+  for (Pointer = (CHAR8 *)String, AddressIndex = 0; AddressIndex < ARRAY_SIZE (Address->Addr) + 1;) {\r
+    if (!InternalAsciiIsDecimalDigitCharacter (*Pointer)) {\r
+      //\r
+      // D or P contains invalid characters.\r
+      //\r
+      break;\r
+    }\r
+\r
+    //\r
+    // Get D or P.\r
+    //\r
+    Status = AsciiStrDecimalToUintnS ((CONST CHAR8 *)Pointer, &Pointer, &Uintn);\r
+    if (RETURN_ERROR (Status)) {\r
+      return RETURN_UNSUPPORTED;\r
+    }\r
+\r
+    if (AddressIndex == ARRAY_SIZE (Address->Addr)) {\r
+      //\r
+      // It's P.\r
+      //\r
+      if (Uintn > 32) {\r
+        return RETURN_UNSUPPORTED;\r
+      }\r
+\r
+      LocalPrefixLength = (UINT8)Uintn;\r
+    } else {\r
+      //\r
+      // It's D.\r
+      //\r
+      if (Uintn > MAX_UINT8) {\r
+        return RETURN_UNSUPPORTED;\r
+      }\r
+\r
+      LocalAddress.Addr[AddressIndex] = (UINT8)Uintn;\r
+      AddressIndex++;\r
+    }\r
+\r
+    //\r
+    // Check the '.' or '/', depending on the AddressIndex.\r
+    //\r
+    if (AddressIndex == ARRAY_SIZE (Address->Addr)) {\r
+      if (*Pointer == '/') {\r
+        //\r
+        // '/P' is in the String.\r
+        // Skip "/" and get P in next loop.\r
+        //\r
+        Pointer++;\r
+      } else {\r
+        //\r
+        // '/P' is not in the String.\r
+        //\r
+        break;\r
+      }\r
+    } else if (AddressIndex < ARRAY_SIZE (Address->Addr)) {\r
+      if (*Pointer == '.') {\r
+        //\r
+        // D should be followed by '.'\r
+        //\r
+        Pointer++;\r
+      } else {\r
+        return RETURN_UNSUPPORTED;\r
+      }\r
+    }\r
+  }\r
+\r
+  if (AddressIndex < ARRAY_SIZE (Address->Addr)) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  CopyMem (Address, &LocalAddress, sizeof (*Address));\r
+  if (PrefixLength != NULL) {\r
+    *PrefixLength = LocalPrefixLength;\r
+  }\r
+\r
+  if (EndPointer != NULL) {\r
+    *EndPointer = Pointer;\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  Convert a Null-terminated ASCII GUID string to a value of type\r
+  EFI_GUID.\r
+\r
+  This function outputs a GUID value by interpreting the contents of\r
+  the ASCII string specified by String. The format of the input\r
+  ASCII string String consists of 36 characters, as follows:\r
+\r
+                  aabbccdd-eeff-gghh-iijj-kkllmmnnoopp\r
+\r
+  The pairs aa - pp are two characters in the range [0-9], [a-f] and\r
+  [A-F], with each pair representing a single byte hexadecimal value.\r
+\r
+  The mapping between String and the EFI_GUID structure is as follows:\r
+                  aa          Data1[24:31]\r
+                  bb          Data1[16:23]\r
+                  cc          Data1[8:15]\r
+                  dd          Data1[0:7]\r
+                  ee          Data2[8:15]\r
+                  ff          Data2[0:7]\r
+                  gg          Data3[8:15]\r
+                  hh          Data3[0:7]\r
+                  ii          Data4[0:7]\r
+                  jj          Data4[8:15]\r
+                  kk          Data4[16:23]\r
+                  ll          Data4[24:31]\r
+                  mm          Data4[32:39]\r
+                  nn          Data4[40:47]\r
+                  oo          Data4[48:55]\r
+                  pp          Data4[56:63]\r
+\r
+  @param  String                   Pointer to a Null-terminated ASCII string.\r
+  @param  Guid                     Pointer to the converted GUID.\r
+\r
+  @retval RETURN_SUCCESS           Guid is translated from String.\r
+  @retval RETURN_INVALID_PARAMETER If String is NULL.\r
+                                   If Data is NULL.\r
+  @retval RETURN_UNSUPPORTED       If String is not as the above format.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+AsciiStrToGuid (\r
+  IN  CONST CHAR8  *String,\r
+  OUT GUID         *Guid\r
+  )\r
+{\r
+  RETURN_STATUS  Status;\r
+  GUID           LocalGuid;\r
+\r
+  //\r
+  // None of String or Guid shall be a null pointer.\r
+  //\r
+  SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);\r
+  SAFE_STRING_CONSTRAINT_CHECK ((Guid != NULL), RETURN_INVALID_PARAMETER);\r
+\r
+  //\r
+  // Get aabbccdd in big-endian.\r
+  //\r
+  Status = AsciiStrHexToBytes (String, 2 * sizeof (LocalGuid.Data1), (UINT8 *)&LocalGuid.Data1, sizeof (LocalGuid.Data1));\r
+  if (RETURN_ERROR (Status) || (String[2 * sizeof (LocalGuid.Data1)] != '-')) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // Convert big-endian to little-endian.\r
+  //\r
+  LocalGuid.Data1 = SwapBytes32 (LocalGuid.Data1);\r
+  String         += 2 * sizeof (LocalGuid.Data1) + 1;\r
+\r
+  //\r
+  // Get eeff in big-endian.\r
+  //\r
+  Status = AsciiStrHexToBytes (String, 2 * sizeof (LocalGuid.Data2), (UINT8 *)&LocalGuid.Data2, sizeof (LocalGuid.Data2));\r
+  if (RETURN_ERROR (Status) || (String[2 * sizeof (LocalGuid.Data2)] != '-')) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // Convert big-endian to little-endian.\r
+  //\r
+  LocalGuid.Data2 = SwapBytes16 (LocalGuid.Data2);\r
+  String         += 2 * sizeof (LocalGuid.Data2) + 1;\r
+\r
+  //\r
+  // Get gghh in big-endian.\r
+  //\r
+  Status = AsciiStrHexToBytes (String, 2 * sizeof (LocalGuid.Data3), (UINT8 *)&LocalGuid.Data3, sizeof (LocalGuid.Data3));\r
+  if (RETURN_ERROR (Status) || (String[2 * sizeof (LocalGuid.Data3)] != '-')) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // Convert big-endian to little-endian.\r
+  //\r
+  LocalGuid.Data3 = SwapBytes16 (LocalGuid.Data3);\r
+  String         += 2 * sizeof (LocalGuid.Data3) + 1;\r
+\r
+  //\r
+  // Get iijj.\r
+  //\r
+  Status = AsciiStrHexToBytes (String, 2 * 2, &LocalGuid.Data4[0], 2);\r
+  if (RETURN_ERROR (Status) || (String[2 * 2] != '-')) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  String += 2 * 2 + 1;\r
+\r
+  //\r
+  // Get kkllmmnnoopp.\r
+  //\r
+  Status = AsciiStrHexToBytes (String, 2 * 6, &LocalGuid.Data4[2], 6);\r
+  if (RETURN_ERROR (Status)) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  CopyGuid (Guid, &LocalGuid);\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  Convert a Null-terminated ASCII hexadecimal string to a byte array.\r
+\r
+  This function outputs a byte array by interpreting the contents of\r
+  the ASCII string specified by String in hexadecimal format. The format of\r
+  the input ASCII string String is:\r
+\r
+                  [XX]*\r
+\r
+  X is a hexadecimal digit character in the range [0-9], [a-f] and [A-F].\r
+  The function decodes every two hexadecimal digit characters as one byte. The\r
+  decoding stops after Length of characters and outputs Buffer containing\r
+  (Length / 2) bytes.\r
+\r
+  @param  String                   Pointer to a Null-terminated ASCII string.\r
+  @param  Length                   The number of ASCII characters to decode.\r
+  @param  Buffer                   Pointer to the converted bytes array.\r
+  @param  MaxBufferSize            The maximum size of Buffer.\r
+\r
+  @retval RETURN_SUCCESS           Buffer is translated from String.\r
+  @retval RETURN_INVALID_PARAMETER If String is NULL.\r
+                                   If Data is NULL.\r
+                                   If Length is not multiple of 2.\r
+                                   If PcdMaximumAsciiStringLength is not zero,\r
+                                    and Length is greater than\r
+                                    PcdMaximumAsciiStringLength.\r
+  @retval RETURN_UNSUPPORTED       If Length of characters from String contain\r
+                                    a character that is not valid hexadecimal\r
+                                    digit characters, or a Null-terminator.\r
+  @retval RETURN_BUFFER_TOO_SMALL  If MaxBufferSize is less than (Length / 2).\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+AsciiStrHexToBytes (\r
+  IN  CONST CHAR8  *String,\r
+  IN  UINTN        Length,\r
+  OUT UINT8        *Buffer,\r
+  IN  UINTN        MaxBufferSize\r
+  )\r
+{\r
+  UINTN  Index;\r
+\r
+  //\r
+  // 1. None of String or Buffer shall be a null pointer.\r
+  //\r
+  SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);\r
+  SAFE_STRING_CONSTRAINT_CHECK ((Buffer != NULL), RETURN_INVALID_PARAMETER);\r
+\r
+  //\r
+  // 2. Length shall not be greater than ASCII_RSIZE_MAX.\r
+  //\r
+  if (ASCII_RSIZE_MAX != 0) {\r
+    SAFE_STRING_CONSTRAINT_CHECK ((Length <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
+  }\r
+\r
+  //\r
+  // 3. Length shall not be odd.\r
+  //\r
+  SAFE_STRING_CONSTRAINT_CHECK (((Length & BIT0) == 0), RETURN_INVALID_PARAMETER);\r
+\r
+  //\r
+  // 4. MaxBufferSize shall equal to or greater than Length / 2.\r
+  //\r
+  SAFE_STRING_CONSTRAINT_CHECK ((MaxBufferSize >= Length / 2), RETURN_BUFFER_TOO_SMALL);\r
+\r
+  //\r
+  // 5. String shall not contains invalid hexadecimal digits.\r
+  //\r
+  for (Index = 0; Index < Length; Index++) {\r
+    if (!InternalAsciiIsHexaDecimalDigitCharacter (String[Index])) {\r
+      break;\r
+    }\r
+  }\r
+\r
+  if (Index != Length) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // Convert the hex string to bytes.\r
+  //\r
+  for (Index = 0; Index < Length; Index++) {\r
+    //\r
+    // For even characters, write the upper nibble for each buffer byte,\r
+    // and for even characters, the lower nibble.\r
+    //\r
+    if ((Index & BIT0) == 0) {\r
+      Buffer[Index / 2] = (UINT8)InternalAsciiHexCharToUintn (String[Index]) << 4;\r
+    } else {\r
+      Buffer[Index / 2] |= (UINT8)InternalAsciiHexCharToUintn (String[Index]);\r
+    }\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r