]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/String.c
Code Scrub for MdePkg.
[mirror_edk2.git] / MdePkg / Library / BaseLib / String.c
index 1c8fc227ea57ef1d87b724393eeabfda1517aed3..6b68bb2eeca8b3fa0a087709f40b3d893a81a2ab 100644 (file)
@@ -10,8 +10,6 @@
   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
-  Module Name:  String.c\r
-\r
 **/\r
 \r
 //\r
@@ -66,7 +64,7 @@ StrCpy (
   ASSERT ((UINTN)(Source - Destination) > StrLen (Source));\r
 \r
   ReturnValue = Destination;\r
-  while (*Source) {\r
+  while (*Source != 0) {\r
     *(Destination++) = *(Source++);\r
   }\r
   *Destination = 0;\r
@@ -195,7 +193,7 @@ StrLen (
 \r
   @param  String  Pointer to a Null-terminated Unicode string.\r
 \r
-  @return The size of String.\r
+  @return The size in bytes of String.\r
 \r
 **/\r
 UINTN\r
@@ -293,7 +291,7 @@ StrnCmp (
   IN      UINTN                     Length\r
   )\r
 {\r
-  if (Length == 0) {\r
+  if (0 == Length) {\r
     return 0;\r
   }\r
 \r
@@ -436,8 +434,8 @@ StrnCat (
   or String contains more than PcdMaximumUnicodeStringLength Unicode \r
   characters not including the Null-terminator, then ASSERT().\r
 \r
-  @param  String                                 Pointer to a Null-terminated Unicode string.\r
-  @param  SearchString Pointer to a Null-terminated Unicode string to search for.\r
+  @param  String                  Pointer to a Null-terminated Unicode string.\r
+  @param  SearchString  Pointer to a Null-terminated Unicode string to search for.\r
 \r
   @retval NULL            If the SearchString does not appear in String.\r
   @retval !NULL           If there is a match.\r
@@ -446,33 +444,19 @@ 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
   CONST CHAR16 *SearchStringTmp;\r
 \r
-  ASSERT (String != NULL);\r
-  ASSERT (((UINTN) String & 0x01) == 0);\r
-  ASSERT (SearchString != NULL);\r
-  ASSERT (((UINTN) SearchString & 0x01) == 0);\r
-\r
   //\r
-  // If PcdMaximumUnicodeStringLength is not zero,\r
-  // length of String should not more than PcdMaximumUnicodeStringLength\r
+  // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.\r
+  // Length tests are performed inside StrLen().\r
   //\r
-  if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
-    ASSERT (StrLen (String) < PcdGet32 (PcdMaximumUnicodeStringLength));\r
-  }\r
-\r
-  //\r
-  // If PcdMaximumUnicodeStringLength is not zero,\r
-  // length of SearchString should not more than PcdMaximumUnicodeStringLength\r
-  //\r
-  if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
-    ASSERT (StrLen (SearchString) < PcdGet32 (PcdMaximumAsciiStringLength));\r
-  }\r
+  ASSERT (StrSize (String) != 0);\r
+  ASSERT (StrSize (SearchString) != 0);\r
 \r
   while (*String != '\0') {\r
     SearchStringTmp = SearchString;\r
@@ -485,7 +469,7 @@ StrStr (
       SearchStringTmp++;\r
     } \r
     \r
-    if (*SearchStringTmp == '\0') {\r
+    if ('\0' == *SearchStringTmp) {\r
       return (CHAR16 *) FirstMatch;\r
     }\r
 \r
@@ -516,8 +500,8 @@ StrStr (
   @retval FALSE Otherwise.\r
 \r
 **/\r
-STATIC\r
 BOOLEAN\r
+EFIAPI\r
 InternalIsDecimalDigitCharacter (\r
   IN      CHAR16                    Char\r
   )\r
@@ -541,8 +525,8 @@ InternalIsDecimalDigitCharacter (
   @retval Unchanged        Otherwise.\r
 \r
 **/\r
-STATIC\r
 CHAR16\r
+EFIAPI\r
 InternalCharToUpper (\r
   IN      CHAR16                    Char\r
   )\r
@@ -567,8 +551,8 @@ InternalCharToUpper (
   @retval UINTN   The numerical value converted.\r
 \r
 **/\r
-STATIC\r
 UINTN\r
+EFIAPI\r
 InternalHexCharToUintn (\r
   IN      CHAR16                    Char\r
   )\r
@@ -594,8 +578,8 @@ InternalHexCharToUintn (
   @retval FALSE Otherwise.\r
 \r
 **/\r
-STATIC\r
 BOOLEAN\r
+EFIAPI\r
 InternalIsHexaDecimalDigitCharacter (\r
   IN      CHAR16                    Char\r
   )\r
@@ -635,7 +619,7 @@ InternalIsHexaDecimalDigitCharacter (
   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                Pointer to a Null-terminated Unicode string.\r
 \r
   @retval UINTN           \r
 \r
@@ -643,26 +627,28 @@ InternalIsHexaDecimalDigitCharacter (
 UINTN\r
 EFIAPI\r
 StrDecimalToUintn (\r
-  IN      CONST CHAR16               *String\r
+  IN      CONST CHAR16                *String\r
   )\r
 {\r
   UINTN     Result;\r
   \r
-  ASSERT (String != NULL);\r
-  ASSERT (((UINTN) String & 0x01) == 0);\r
-  ASSERT (StrLen (String) < PcdGet32 (PcdMaximumUnicodeStringLength));\r
+  //\r
+  // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
+  // Length tests are performed inside StrLen().\r
+  //\r
+  ASSERT (StrSize (String) != 0);\r
 \r
   //\r
   // Ignore the pad spaces (space or tab)\r
   //\r
-  while ((*String == L' ') || (*String == L'\t')) {\r
+  while ((L' ' ==*String) || (L'\t' == *String)) {\r
     String++;\r
   }\r
 \r
   //\r
   // Ignore leading Zeros after the spaces\r
   //\r
-  while (*String == L'0') {\r
+  while (L'0' == *String) {\r
     String++;\r
   }\r
 \r
@@ -674,7 +660,7 @@ StrDecimalToUintn (
     // to the range defined by UINTN, then ASSERT().\r
     //\r
     ASSERT ((Result < QUIENT_MAX_UINTN_DIVIDED_BY_10) ||\r
-      ((Result == QUIENT_MAX_UINTN_DIVIDED_BY_10) &&\r
+      ((QUIENT_MAX_UINTN_DIVIDED_BY_10 == Result) &&\r
       (*String - L'0') <= REMINDER_MAX_UINTN_DIVIDED_BY_10)\r
       );\r
 \r
@@ -715,7 +701,7 @@ StrDecimalToUintn (
   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                Pointer to a Null-terminated Unicode string.\r
 \r
   @retval UINT64           \r
 \r
@@ -723,26 +709,28 @@ StrDecimalToUintn (
 UINT64\r
 EFIAPI\r
 StrDecimalToUint64 (\r
-  IN      CONST CHAR16               *String\r
+  IN      CONST CHAR16                *String\r
   )\r
 {\r
   UINT64     Result;\r
   \r
-  ASSERT (String != NULL);\r
-  ASSERT (((UINTN) String & 0x01) == 0);\r
-  ASSERT (StrLen (String) < PcdGet32 (PcdMaximumUnicodeStringLength));\r
+  //\r
+  // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
+  // Length tests are performed inside StrLen().\r
+  //\r
+  ASSERT (StrSize (String) != 0);\r
 \r
   //\r
   // Ignore the pad spaces (space or tab)\r
   //\r
-  while ((*String == L' ') || (*String == L'\t')) {\r
+  while ((L' ' == *String) || (L'\t' == *String)) {\r
     String++;\r
   }\r
 \r
   //\r
   // Ignore leading Zeros after the spaces\r
   //\r
-  while (*String == L'0') {\r
+  while (L'0' == *String) {\r
     String++;\r
   }\r
 \r
@@ -754,7 +742,7 @@ StrDecimalToUint64 (
     // to the range defined by UINTN, then ASSERT().\r
     //\r
     ASSERT ((Result < QUIENT_MAX_UINT64_DIVIDED_BY_10) || \r
-      ((Result == QUIENT_MAX_UINT64_DIVIDED_BY_10) && \r
+      ((QUIENT_MAX_UINT64_DIVIDED_BY_10 == Result) && \r
       (*String - L'0') <= REMINDER_MAX_UINT64_DIVIDED_BY_10)\r
       );\r
 \r
@@ -795,7 +783,7 @@ StrDecimalToUint64 (
   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                Pointer to a Null-terminated Unicode string.\r
 \r
   @retval UINTN\r
 \r
@@ -803,31 +791,33 @@ StrDecimalToUint64 (
 UINTN\r
 EFIAPI\r
 StrHexToUintn (\r
-  IN      CONST CHAR16               *String\r
+  IN      CONST CHAR16                *String\r
   )\r
 {\r
   UINTN     Result;\r
 \r
-  ASSERT (String != NULL);\r
-  ASSERT (((UINTN) String & 0x01) == 0);\r
-  ASSERT (StrLen (String) < PcdGet32 (PcdMaximumUnicodeStringLength));\r
+  //\r
+  // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
+  // Length tests are performed inside StrLen().\r
+  //\r
+  ASSERT (StrSize (String) != 0);\r
   \r
   //\r
   // Ignore the pad spaces (space or tab) \r
   //\r
-  while ((*String == L' ') || (*String == L'\t')) {\r
+  while ((L' ' == *String) || (L'\t' == *String)) {\r
     String++;\r
   }\r
 \r
   //\r
   // Ignore leading Zeros after the spaces\r
   //\r
-  while (*String == L'0') {\r
+  while (L'0' == *String) {\r
     String++;\r
   }\r
 \r
   if (InternalCharToUpper (*String) == L'X') {\r
-    ASSERT (*(String - 1)  == L'0');\r
+    ASSERT (L'0' == *(String - 1));\r
     if (*(String - 1)  != L'0') {\r
       return 0;\r
     }\r
@@ -845,7 +835,7 @@ StrHexToUintn (
     // to the range defined by UINTN, then ASSERT().\r
     //\r
     ASSERT ((Result < QUIENT_MAX_UINTN_DIVIDED_BY_16) ||\r
-      ((Result == QUIENT_MAX_UINTN_DIVIDED_BY_16) && \r
+      ((QUIENT_MAX_UINTN_DIVIDED_BY_16 == Result) && \r
       (InternalHexCharToUintn (*String) <= REMINDER_MAX_UINTN_DIVIDED_BY_16))\r
       );\r
 \r
@@ -887,7 +877,7 @@ StrHexToUintn (
   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                Pointer to a Null-terminated Unicode string.\r
 \r
   @retval UINT64\r
 \r
@@ -895,31 +885,33 @@ StrHexToUintn (
 UINT64\r
 EFIAPI\r
 StrHexToUint64 (\r
-  IN      CONST CHAR16               *String\r
+  IN      CONST CHAR16                *String\r
   )\r
 {\r
   UINT64    Result;\r
 \r
-  ASSERT (String != NULL);\r
-  ASSERT (((UINTN) String & 0x01) == 0);\r
-  ASSERT (StrLen (String) < PcdGet32 (PcdMaximumUnicodeStringLength));\r
+  //\r
+  // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
+  // Length tests are performed inside StrLen().\r
+  //\r
+  ASSERT (StrSize (String) != 0);\r
   \r
   //\r
   // Ignore the pad spaces (space or tab) \r
   //\r
-  while ((*String == L' ') || (*String == L'\t')) {\r
+  while ((L' ' == *String) || (L'\t' == *String)) {\r
     String++;\r
   }\r
 \r
   //\r
   // Ignore leading Zeros after the spaces\r
   //\r
-  while (*String == L'0') {\r
+  while (L'0' == *String) {\r
     String++;\r
   }\r
 \r
   if (InternalCharToUpper (*String) == L'X') {\r
-    ASSERT (*(String - 1)  == L'0');\r
+    ASSERT (L'0' == *(String - 1));\r
     if (*(String - 1)  != L'0') {\r
       return 0;\r
     }\r
@@ -937,7 +929,7 @@ StrHexToUint64 (
     // to the range defined by UINTN, then ASSERT().\r
     //\r
     ASSERT ((Result < QUIENT_MAX_UINT64_DIVIDED_BY_16)|| \r
-      ((Result == QUIENT_MAX_UINT64_DIVIDED_BY_16) && \r
+      ((QUIENT_MAX_UINT64_DIVIDED_BY_16 == Result) && \r
       (InternalHexCharToUintn (*String) <= REMINDER_MAX_UINT64_DIVIDED_BY_16))\r
       );\r
 \r
@@ -962,8 +954,8 @@ StrHexToUint64 (
   @retval FALSE Otherwise.\r
 \r
 **/\r
-STATIC\r
 BOOLEAN\r
+EFIAPI\r
 InternalAsciiIsDecimalDigitCharacter (\r
   IN      CHAR8                     Char\r
   )\r
@@ -985,8 +977,8 @@ InternalAsciiIsDecimalDigitCharacter (
   @retval FALSE Otherwise.\r
 \r
 **/\r
-STATIC\r
 BOOLEAN\r
+EFIAPI\r
 InternalAsciiIsHexaDecimalDigitCharacter (\r
   IN      CHAR8                    Char\r
   )\r
@@ -1027,19 +1019,25 @@ InternalAsciiIsHexaDecimalDigitCharacter (
   @param  Source        Pointer to a Null-terminated Unicode string.\r
   @param  Destination   Pointer to a Null-terminated ASCII string.\r
 \r
-  @reture Destination\r
+  @return Destination\r
 \r
 **/\r
 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
+\r
   ASSERT (Destination != NULL);\r
-  ASSERT (Source != NULL);\r
-  ASSERT (((UINTN) Source & 0x01) == 0);\r
+\r
+  //\r
+  // ASSERT if Source is long than PcdMaximumUnicodeStringLength.\r
+  // Length tests are performed inside StrLen().\r
+  //\r
+  ASSERT (StrSize (Source) != 0);\r
 \r
   //\r
   // Source and Destination should not overlap\r
@@ -1047,14 +1045,8 @@ UnicodeStrToAsciiStr (
   ASSERT ((UINTN) ((CHAR16 *) Destination -  Source) > StrLen (Source));\r
   ASSERT ((UINTN) ((CHAR8 *) Source - Destination) > StrLen (Source));\r
 \r
-  //\r
-  // If PcdMaximumUnicodeStringLength is not zero,\r
-  // length of Source should not more than PcdMaximumUnicodeStringLength\r
-  //\r
-  if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
-    ASSERT (StrLen (Source) < PcdGet32 (PcdMaximumUnicodeStringLength));\r
-  }\r
 \r
+  ReturnValue = Destination;\r
   while (*Source != '\0') {\r
     //\r
     // If any Unicode characters in Source contain \r
@@ -1065,8 +1057,14 @@ UnicodeStrToAsciiStr (
   }\r
 \r
   *Destination = '\0';\r
-  \r
-  return Destination;\r
+\r
+  //\r
+  // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength.\r
+  // Length tests are performed inside AsciiStrLen().\r
+  //\r
+  ASSERT (AsciiStrSize (ReturnValue) != 0);\r
+\r
+  return ReturnValue;\r
 }\r
 \r
 \r
@@ -1112,7 +1110,7 @@ AsciiStrCpy (
   ASSERT ((UINTN)(Source - Destination) > AsciiStrLen (Source));\r
 \r
   ReturnValue = Destination;\r
-  while (*Source) {\r
+  while (*Source != 0) {\r
     *(Destination++) = *(Source++);\r
   }\r
   *Destination = 0;\r
@@ -1155,7 +1153,7 @@ AsciiStrnCpy (
 {\r
   CHAR8                             *ReturnValue;\r
 \r
-  if (Length == 0) {\r
+  if (0 == Length) {\r
     return Destination;\r
   }\r
 \r
@@ -1172,7 +1170,7 @@ AsciiStrnCpy (
 \r
   ReturnValue = Destination;\r
 \r
-  while (*Source && Length > 0) {\r
+  while (*Source != 0 && Length > 0) {\r
     *(Destination++) = *(Source++);\r
     Length--;\r
   }\r
@@ -1300,13 +1298,13 @@ AsciiStrCmp (
   If Value >= 0xA0, then ASSERT().\r
   If (Value & 0x0F) >= 0x0A, then ASSERT().\r
 \r
-  @param  chr   one Ascii character\r
+  @param  Chr   one Ascii character\r
 \r
   @return The uppercase value of Ascii character \r
 \r
 **/\r
-STATIC\r
 CHAR8\r
+EFIAPI\r
 AsciiToUpper (\r
   IN      CHAR8                     Chr\r
   )\r
@@ -1327,8 +1325,8 @@ AsciiToUpper (
   @retval UINTN   The numerical value converted.\r
 \r
 **/\r
-STATIC\r
 UINTN\r
+EFIAPI\r
 InternalAsciiHexCharToUintn (\r
   IN      CHAR8                    Char\r
   )\r
@@ -1420,7 +1418,8 @@ AsciiStriCmp (
 \r
   @param  FirstString   Pointer to a Null-terminated ASCII string.\r
   @param  SecondString  Pointer to a Null-terminated ASCII string.\r
-\r
+  @param  Length        Maximum number of ASCII characters to compare.\r
+                        \r
   @retval 0   FirstString is identical to SecondString.\r
   @retval !=0 FirstString is not identical to SecondString.\r
 \r
@@ -1433,7 +1432,7 @@ AsciiStrnCmp (
   IN      UINTN                     Length\r
   )\r
 {\r
-  if (Length == 0) {\r
+  if (0 == Length) {\r
     return 0;\r
   }\r
 \r
@@ -1565,8 +1564,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          Pointer to a Null-terminated ASCII string.\r
+  @param  SearchString    Pointer to a Null-terminated ASCII string to search for.\r
 \r
   @retval NULL            If the SearchString does not appear in String.\r
   @retval !NULL           If there is a match.\r
@@ -1575,31 +1574,18 @@ AsciiStrnCat (
 CHAR8 *\r
 EFIAPI\r
 AsciiStrStr (\r
-  IN      CONST CHAR8                *String,\r
+  IN      CONST CHAR8             *String,\r
   IN      CONST CHAR8             *SearchString\r
   )\r
 {\r
   CONST CHAR8 *FirstMatch;\r
   CONST CHAR8 *SearchStringTmp;\r
 \r
-  ASSERT (String != NULL);\r
-  ASSERT (SearchString != NULL);\r
-\r
   //\r
-  // If PcdMaximumUnicodeStringLength is not zero,\r
-  // length of String should not more than PcdMaximumUnicodeStringLength\r
-  //\r
-  if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
-    ASSERT (AsciiStrLen (String) < PcdGet32 (PcdMaximumAsciiStringLength));\r
-  }\r
-\r
-  //\r
-  // If PcdMaximumUnicodeStringLength is not zero,\r
-  // length of SearchString should not more than PcdMaximumUnicodeStringLength\r
+  // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
   //\r
-  if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
-    ASSERT (AsciiStrLen (SearchString) < PcdGet32 (PcdMaximumAsciiStringLength));\r
-  }\r
+  ASSERT (AsciiStrSize (String) != 0);\r
+  ASSERT (AsciiStrSize (SearchString) != 0);\r
 \r
   while (*String != '\0') {\r
     SearchStringTmp = SearchString;\r
@@ -1655,7 +1641,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                Pointer to a Null-terminated ASCII string.\r
 \r
   @retval UINTN           \r
 \r
@@ -1663,25 +1649,27 @@ AsciiStrStr (
 UINTN\r
 EFIAPI\r
 AsciiStrDecimalToUintn (\r
-  IN      CONST CHAR8                  *String\r
+  IN      CONST CHAR8               *String\r
   )\r
 {\r
   UINTN     Result;\r
   \r
-  ASSERT (String != NULL);\r
-  ASSERT (AsciiStrLen (String) < PcdGet32 (PcdMaximumAsciiStringLength));\r
+  //\r
+  // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
+  //\r
+  ASSERT (AsciiStrSize (String) != 0);\r
 \r
   //\r
   // Ignore the pad spaces (space or tab)\r
   //\r
-  while ((*String == ' ') || (*String == '\t')) {\r
+  while ((' ' == *String) || ('\t' == *String)) {\r
     String++;\r
   }\r
 \r
   //\r
   // Ignore leading Zeros after the spaces\r
   //\r
-  while (*String == '0') {\r
+  while ('0' == *String) {\r
     String++;\r
   }\r
 \r
@@ -1693,7 +1681,7 @@ AsciiStrDecimalToUintn (
     // to the range defined by UINTN, then ASSERT().\r
     //\r
     ASSERT ((Result < QUIENT_MAX_UINTN_DIVIDED_BY_10) ||\r
-      ((Result == QUIENT_MAX_UINTN_DIVIDED_BY_10) && \r
+      ((QUIENT_MAX_UINTN_DIVIDED_BY_10 == Result) && \r
       (*String - '0') <= REMINDER_MAX_UINTN_DIVIDED_BY_10)\r
       );\r
 \r
@@ -1730,7 +1718,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                Pointer to a Null-terminated ASCII string.\r
 \r
   @retval UINT64           \r
 \r
@@ -1738,25 +1726,27 @@ AsciiStrDecimalToUintn (
 UINT64\r
 EFIAPI\r
 AsciiStrDecimalToUint64 (\r
-  IN      CONST CHAR8                *String\r
+  IN      CONST CHAR8             *String\r
   )\r
 {\r
   UINT64     Result;\r
   \r
-  ASSERT (String != NULL);\r
-  ASSERT (AsciiStrLen (String) < PcdGet32 (PcdMaximumAsciiStringLength));\r
+  //\r
+  // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
+  //\r
+  ASSERT (AsciiStrSize (String) != 0);\r
 \r
   //\r
   // Ignore the pad spaces (space or tab)\r
   //\r
-  while ((*String == ' ') || (*String == '\t')) {\r
+  while ((' ' == *String) || ('\t' == *String)) {\r
     String++;\r
   }\r
 \r
   //\r
   // Ignore leading Zeros after the spaces\r
   //\r
-  while (*String == '0') {\r
+  while ('0' == *String) {\r
     String++;\r
   }\r
 \r
@@ -1768,7 +1758,7 @@ AsciiStrDecimalToUint64 (
     // to the range defined by UINTN, then ASSERT().\r
     //\r
     ASSERT ((Result < QUIENT_MAX_UINT64_DIVIDED_BY_10) || \r
-      ((Result == QUIENT_MAX_UINT64_DIVIDED_BY_10) && \r
+      ((QUIENT_MAX_UINT64_DIVIDED_BY_10 == Result) && \r
       (*String - '0') <= REMINDER_MAX_UINT64_DIVIDED_BY_10)\r
       );\r
 \r
@@ -1808,7 +1798,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                Pointer to a Null-terminated ASCII string.\r
 \r
   @retval UINTN\r
 \r
@@ -1816,30 +1806,32 @@ AsciiStrDecimalToUint64 (
 UINTN\r
 EFIAPI\r
 AsciiStrHexToUintn (\r
-  IN      CONST CHAR8                *String\r
+  IN      CONST CHAR8             *String\r
   )\r
 {\r
   UINTN     Result;\r
 \r
-  ASSERT (String != NULL);\r
-  ASSERT (AsciiStrLen (String) < PcdGet32 (PcdMaximumAsciiStringLength));\r
+  //\r
+  // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
+  //\r
+  ASSERT (AsciiStrSize (String) != 0);\r
   \r
   //\r
   // Ignore the pad spaces (space or tab) \r
   //\r
-  while ((*String == ' ') || (*String == '\t')) {\r
+  while ((' ' == *String) || ('\t' == *String)) {\r
     String++;\r
   }\r
 \r
   //\r
   // Ignore leading Zeros after the spaces\r
   //\r
-  while (*String == '0') {\r
+  while ('0' == *String) {\r
     String++;\r
   }\r
 \r
   if (AsciiToUpper (*String) == 'X') {\r
-    ASSERT (*(String - 1)  == '0');\r
+    ASSERT ('0' == *(String - 1));\r
     if (*(String - 1)  != '0') {\r
       return 0;\r
     }\r
@@ -1857,7 +1849,7 @@ AsciiStrHexToUintn (
     // to the range defined by UINTN, then ASSERT().\r
     //\r
      ASSERT ((Result < QUIENT_MAX_UINTN_DIVIDED_BY_16) ||\r
-       ((Result == QUIENT_MAX_UINTN_DIVIDED_BY_16) && \r
+       ((QUIENT_MAX_UINTN_DIVIDED_BY_16 == Result) && \r
        (InternalAsciiHexCharToUintn (*String) <= REMINDER_MAX_UINTN_DIVIDED_BY_16))\r
        );\r
 \r
@@ -1898,7 +1890,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                Pointer to a Null-terminated ASCII string.\r
 \r
   @retval UINT64\r
 \r
@@ -1906,13 +1898,15 @@ AsciiStrHexToUintn (
 UINT64\r
 EFIAPI\r
 AsciiStrHexToUint64 (\r
-  IN      CONST CHAR8                *String\r
+  IN      CONST CHAR8             *String\r
   )\r
 {\r
   UINT64    Result;\r
 \r
-  ASSERT (String != NULL);\r
-  ASSERT (AsciiStrLen (String) < PcdGet32 (PcdMaximumUnicodeStringLength));\r
+  //\r
+  // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
+  //\r
+  ASSERT (AsciiStrSize (String) != 0);\r
   \r
   //\r
   // Ignore the pad spaces (space or tab) and leading Zeros\r
@@ -1920,19 +1914,19 @@ AsciiStrHexToUint64 (
   //\r
   // Ignore the pad spaces (space or tab) \r
   //\r
-  while ((*String == ' ') || (*String == '\t')) {\r
+  while ((' ' == *String) || ('\t' == *String)) {\r
     String++;\r
   }\r
 \r
   //\r
   // Ignore leading Zeros after the spaces\r
   //\r
-  while (*String == '0') {\r
+  while ('0' == *String) {\r
     String++;\r
   }\r
 \r
   if (AsciiToUpper (*String) == 'X') {\r
-    ASSERT (*(String - 1)  == '0');\r
+    ASSERT ('0' == *(String - 1));\r
     if (*(String - 1)  != '0') {\r
       return 0;\r
     }\r
@@ -1950,7 +1944,7 @@ AsciiStrHexToUint64 (
     // to the range defined by UINTN, then ASSERT().\r
     //\r
     ASSERT ((Result < QUIENT_MAX_UINT64_DIVIDED_BY_16) ||\r
-      ((Result == QUIENT_MAX_UINT64_DIVIDED_BY_16) && \r
+      ((QUIENT_MAX_UINT64_DIVIDED_BY_16 == Result) && \r
       (InternalAsciiHexCharToUintn (*String) <= REMINDER_MAX_UINT64_DIVIDED_BY_16))\r
       );\r
 \r
@@ -1987,33 +1981,33 @@ AsciiStrHexToUint64 (
   @param  Source        Pointer to a Null-terminated ASCII string.\r
   @param  Destination   Pointer to a Null-terminated Unicode string.\r
 \r
-  @reture Destination\r
+  @return Destination\r
 \r
 **/\r
 CHAR16 *\r
 EFIAPI\r
 AsciiStrToUnicodeStr (\r
-  IN      CONST CHAR8                *Source,\r
-  OUT    CHAR16                        *Destination\r
+  IN      CONST CHAR8               *Source,\r
+  OUT     CHAR16                    *Destination\r
   )\r
 {\r
+  CHAR16                            *ReturnValue;\r
+\r
   ASSERT (Destination != NULL);\r
-  ASSERT (Source != NULL);\r
 \r
   //\r
-  // Source and Destination should not overlap\r
+  // ASSERT Source is less long than PcdMaximumAsciiStringLength\r
   //\r
-  ASSERT ((UINTN) ((CHAR8 *) Destination - Source) > AsciiStrLen (Source));\r
-  ASSERT ((UINTN) (Source - (CHAR8 *) Destination) > (AsciiStrLen (Source) * sizeof (CHAR16)));\r
+  ASSERT (AsciiStrSize (Source) != 0);\r
 \r
   //\r
-  // If PcdMaximumAsciiStringLength is not zero,\r
-  // length of Source should not more than PcdMaximumUnicodeStringLength\r
+  // Source and Destination should not overlap\r
   //\r
-  if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
-    ASSERT (AsciiStrLen (Source) < PcdGet32 (PcdMaximumAsciiStringLength));\r
-  }\r
+  ASSERT ((UINTN) ((CHAR8 *) Destination - Source) > AsciiStrLen (Source));\r
+  ASSERT ((UINTN) (Source - (CHAR8 *) Destination) > (AsciiStrLen (Source) * sizeof (CHAR16)));\r
 \r
\r
+  ReturnValue = Destination;\r
   while (*Source != '\0') {\r
     *(Destination++) = (CHAR16) *(Source++);\r
   }\r
@@ -2022,7 +2016,12 @@ AsciiStrToUnicodeStr (
   //\r
   *Destination = '\0';\r
 \r
-  return Destination;\r
+  //\r
+  // ASSERT Original Destination is less long than PcdMaximumUnicodeStringLength\r
+  //\r
+  ASSERT (StrSize (ReturnValue) != 0);\r
+\r
+  return ReturnValue;\r
 }\r
 \r
 /**\r
@@ -2074,3 +2073,242 @@ BcdToDecimal8 (
 }\r
 \r
 \r
+/**\r
+  Convert a nibble in the low 4 bits of a byte to a Unicode hexadecimal character.\r
+\r
+  This function converts a nibble in the low 4 bits of a byte to a Unicode hexadecimal \r
+  character  For example, the nibble  0x01 and 0x0A will converted to L'1' and L'A' \r
+  respectively.\r
+\r
+  The upper nibble in the input byte will be masked off.\r
+\r
+  @param Nibble     The nibble which is in the low 4 bits of the input byte.\r
+\r
+  @retval  CHAR16   The Unicode hexadecimal character.\r
+  \r
+**/\r
+CHAR16\r
+NibbleToHexChar (\r
+  IN UINT8      Nibble\r
+  )\r
+{\r
+  Nibble &= 0x0F;\r
+  if (Nibble <= 0x9) {\r
+    return (CHAR16)(Nibble + L'0');\r
+  }\r
+\r
+  return (CHAR16)(Nibble - 0xA + L'A');\r
+}\r
+\r
+/** \r
+  Convert binary buffer to a Unicode String in a specified sequence. \r
+\r
+  This function converts bytes in the binary Buffer Buf to a Unicode String Str. \r
+  Each byte will be represented by two Unicode characters. For example, byte 0xA1 will \r
+  be converted into two Unicode character L'A' and L'1'. In the output String, the Unicode Character \r
+  for the Most Significant Nibble will be put before the Unicode Character for the Least Significant\r
+  Nibble. The output string for the buffer containing a single byte 0xA1 will be L"A1". \r
+  For a buffer with multiple bytes, the Unicode character produced by the first byte will be put into the \r
+  the last character in the output string. The one next to first byte will be put into the\r
+  character before the last character. This rules applies to the rest of the bytes. The Unicode\r
+  character by the last byte will be put into the first character in the output string. For example,\r
+  the input buffer for a 64-bits unsigned integrer 0x12345678abcdef1234 will be converted to\r
+  a Unicode string equal to L"12345678abcdef1234".\r
+\r
+  @param String                        On input, String is pointed to the buffer allocated for the convertion.\r
+  @param StringLen                     The Length of String buffer to hold the output String. The length must include the tailing '\0' character.\r
+                                       The StringLen required to convert a N bytes Buffer will be a least equal to or greater \r
+                                       than 2*N + 1.\r
+  @param Buffer                        The pointer to a input buffer.\r
+  @param BufferSizeInBytes             Lenth in bytes of the input buffer.\r
+  \r
+\r
+  @retval  EFI_SUCCESS                 The convertion is successfull. All bytes in Buffer has been convert to the corresponding\r
+                                       Unicode character and placed into the right place in String.\r
+  @retval  EFI_BUFFER_TOO_SMALL        StringSizeInBytes is smaller than 2 * N + 1the number of bytes required to\r
+                                       complete the convertion. \r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+BufToHexString (\r
+  IN OUT       CHAR16               *String,\r
+  IN OUT       UINTN                *StringLen,\r
+  IN     CONST UINT8                *Buffer,\r
+  IN           UINTN                BufferSizeInBytes\r
+  )\r
+{\r
+  UINTN       Idx;\r
+  UINT8       Byte;\r
+  UINTN       StrLen;\r
+\r
+  //\r
+  // Make sure string is either passed or allocate enough.\r
+  // It takes 2 Unicode characters (4 bytes) to represent 1 byte of the binary buffer.\r
+  // Plus the Unicode termination character.\r
+  //\r
+  StrLen = BufferSizeInBytes * 2;\r
+  if (StrLen > ((*StringLen) - 1)) {\r
+    *StringLen = StrLen + 1;\r
+    return RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  *StringLen = StrLen + 1;\r
+  //\r
+  // Ends the string.\r
+  //\r
+  String[StrLen] = L'\0'; \r
+\r
+  for (Idx = 0; Idx < BufferSizeInBytes; Idx++) {\r
+\r
+    Byte = Buffer[Idx];\r
+    String[StrLen - 1 - Idx * 2] = NibbleToHexChar (Byte);\r
+    String[StrLen - 2 - Idx * 2] = NibbleToHexChar ((UINT8)(Byte >> 4));\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Convert a Unicode string consisting of hexadecimal characters to a output byte buffer.\r
+\r
+  This function converts a Unicode string consisting of characters in the range of Hexadecimal\r
+  character (L'0' to L'9', L'A' to L'F' and L'a' to L'f') to a output byte buffer. The function will stop\r
+  at the first non-hexadecimal character or the NULL character. The convertion process can be\r
+  simply viewed as the reverse operations defined by BufToHexString. Two Unicode characters will be \r
+  converted into one byte. The first Unicode character represents the Most Significant Nibble and the\r
+  second Unicode character represents the Least Significant Nibble in the output byte. \r
+  The first pair of Unicode characters represents the last byte in the output buffer. The second pair of Unicode \r
+  characters represent the  the byte preceding the last byte. This rule applies to the rest pairs of bytes. \r
+  The last pair represent the first byte in the output buffer. \r
+\r
+  For example, a Unciode String L"12345678" will be converted into a buffer wil the following bytes \r
+  (first byte is the byte in the lowest memory address): "0x78, 0x56, 0x34, 0x12".\r
+\r
+  If String has N valid hexadecimal characters for conversion,  the caller must make sure Buffer is at least \r
+  N/2 (if N is even) or (N+1)/2 (if N if odd) bytes. \r
+\r
+  @param Buffer                      The output buffer allocated by the caller.\r
+  @param BufferSizeInBytes           On input, the size in bytes of Buffer. On output, it is updated to \r
+                                     contain the size of the Buffer which is actually used for the converstion.\r
+                                     For Unicode string with 2*N hexadecimal characters (not including the \r
+                                     tailing NULL character), N bytes of Buffer will be used for the output.\r
+  @param String                      The input hexadecimal string.\r
+  @param ConvertedStrLen             The number of hexadecimal characters used to produce content in output\r
+                                     buffer Buffer.\r
+\r
+  @retval  RETURN_BUFFER_TOO_SMALL   The input BufferSizeInBytes is too small to hold the output. BufferSizeInBytes\r
+                                     will be updated to the size required for the converstion.\r
+  @retval  RETURN_SUCCESS            The convertion is successful or the first Unicode character from String\r
+                                     is hexadecimal. If ConvertedStrLen is not NULL, it is updated\r
+                                     to the number of hexadecimal character used for the converstion.\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+HexStringToBuf (\r
+  OUT          UINT8                    *Buffer,   \r
+  IN OUT       UINTN                    *BufferSizeInBytes,\r
+  IN     CONST CHAR16                   *String,\r
+  OUT          UINTN                    *ConvertedStrLen  OPTIONAL\r
+  )\r
+{\r
+  UINTN       HexCnt;\r
+  UINTN       Idx;\r
+  UINTN       BufferLength;\r
+  UINT8       Digit;\r
+  UINT8       Byte;\r
+\r
+  //\r
+  // Find out how many hex characters the string has.\r
+  //\r
+  for (Idx = 0, HexCnt = 0; IsHexDigit (&Digit, String[Idx]); Idx++, HexCnt++);\r
+\r
+  if (HexCnt == 0) {\r
+    *ConvertedStrLen = 0;\r
+    return RETURN_SUCCESS;\r
+  }\r
+  //\r
+  // Two Unicode characters make up 1 buffer byte. Round up.\r
+  //\r
+  BufferLength = (HexCnt + 1) / 2; \r
+\r
+  //\r
+  // Test if  buffer is passed enough.\r
+  //\r
+  if (BufferLength > (*BufferSizeInBytes)) {\r
+    *BufferSizeInBytes = BufferLength;\r
+    return RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  *BufferSizeInBytes = BufferLength;\r
+\r
+  for (Idx = 0; Idx < HexCnt; Idx++) {\r
+\r
+    IsHexDigit (&Digit, String[HexCnt - 1 - Idx]);\r
+\r
+    //\r
+    // For odd charaters, write the lower nibble for each buffer byte,\r
+    // and for even characters, the upper nibble.\r
+    //\r
+    if ((Idx & 1) == 0) {\r
+      Byte = Digit;\r
+    } else {\r
+      Byte = Buffer[Idx / 2];\r
+      Byte &= 0x0F;\r
+      Byte = (UINT8) (Byte | Digit << 4);\r
+    }\r
+\r
+    Buffer[Idx / 2] = Byte;\r
+  }\r
+\r
+  if (ConvertedStrLen != NULL) {\r
+    *ConvertedStrLen = HexCnt;\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Test if  a Unicode character is a hexadecimal digit. If true, the input\r
+  Unicode character is converted to a byte. \r
+\r
+  This function tests if a Unicode character is a hexadecimal digit. If true, the input\r
+  Unicode character is converted to a byte. For example, Unicode character\r
+  L'A' will be converted to 0x0A. \r
+\r
+  If Digit is NULL, then ASSERT.\r
+\r
+  @retval TRUE        Char is in the range of Hexadecimal number. Digit is updated\r
+                      to the byte value of the number.\r
+  @retval FALSE       Char is not in the range of Hexadecimal number. Digit is keep\r
+                      intact.\r
+  \r
+**/\r
+BOOLEAN\r
+IsHexDigit (\r
+  OUT UINT8      *Digit,\r
+  IN  CHAR16      Char\r
+  )\r
+{\r
+  ASSERT (Digit != NULL);\r
+  \r
+  if ((Char >= L'0') && (Char <= L'9')) {\r
+    *Digit = (UINT8) (Char - L'0');\r
+    return TRUE;\r
+  }\r
+\r
+  if ((Char >= L'A') && (Char <= L'F')) {\r
+    *Digit = (UINT8) (Char - L'A' + 0x0A);\r
+    return TRUE;\r
+  }\r
+\r
+  if ((Char >= L'a') && (Char <= L'f')) {\r
+    *Digit = (UINT8) (Char - L'a' + 0x0A);\r
+    return TRUE;\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
+\r