]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/String.c
In AsciiStrncmp(), if length=0, should return 0
[mirror_edk2.git] / MdePkg / Library / BaseLib / String.c
index 86828e65fc2af52d4619c2d261311d2f2e242874..a59a9d08cbca22a3fb2d954d6a07dda6606fa247 100644 (file)
@@ -26,7 +26,8 @@
   If Source is NULL, then ASSERT().\r
   If Source and Destination overlap, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  PcdMaximumUnicodeStringLength Unicode characters not including the \r
+  Null-terminator, then ASSERT().\r
 \r
   @param  Destination Pointer to a Null-terminated Unicode string.\r
   @param  Source      Pointer to a Null-terminated Unicode string.\r
@@ -79,7 +80,8 @@ StrCpy (
   If Source is NULL, then ASSERT().\r
   If Source and Destination overlap, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  PcdMaximumUnicodeStringLength Unicode characters not including the \r
+  Null-terminator, then ASSERT().\r
 \r
   @param  Destination Pointer to a Null-terminated Unicode string.\r
   @param  Source      Pointer to a Null-terminated Unicode string.\r
@@ -133,7 +135,8 @@ StrnCpy (
 \r
   If String is NULL, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  PcdMaximumUnicodeStringLength Unicode characters not including the \r
+  Null-terminator, then ASSERT().\r
 \r
   @param  String  Pointer to a Null-terminated Unicode string.\r
 \r
@@ -155,8 +158,8 @@ StrLen (
     // If PcdMaximumUnicodeStringLength is not zero,\r
     // length should not more than PcdMaximumUnicodeStringLength\r
     //\r
-    if (FixedPcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
-      ASSERT (Length < FixedPcdGet32 (PcdMaximumUnicodeStringLength));\r
+    if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
+      ASSERT (Length < PcdGet32 (PcdMaximumUnicodeStringLength));\r
     }\r
   }\r
   return Length;\r
@@ -171,7 +174,8 @@ StrLen (
 \r
   If String is NULL, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  PcdMaximumUnicodeStringLength Unicode characters not including the \r
+  Null-terminator, then ASSERT().\r
 \r
   @param  String  Pointer to a Null-terminated Unicode string.\r
 \r
@@ -200,9 +204,11 @@ StrSize (
   If FirstString is NULL, then ASSERT().\r
   If SecondString is NULL, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more\r
-  than PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  than PcdMaximumUnicodeStringLength Unicode characters not including the \r
+  Null-terminator, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r
-  than PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  than PcdMaximumUnicodeStringLength Unicode characters not including the \r
+  Null-terminator, then ASSERT().\r
 \r
   @param  FirstString   Pointer to a Null-terminated Unicode string.\r
   @param  SecondString  Pointer to a Null-terminated Unicode string.\r
@@ -245,9 +251,11 @@ StrCmp (
   If FirstString is NULL, then ASSERT().\r
   If SecondString is NULL, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more\r
-  than PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  than PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  Null-terminator, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r
-  than PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  than PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  Null-terminator, then ASSERT().\r
 \r
   @param  FirstString   Pointer to a Null-terminated Unicode string.\r
   @param  SecondString  Pointer to a Null-terminated Unicode string.\r
@@ -277,7 +285,7 @@ StrnCmp (
   ASSERT (StrSize (SecondString) != 0);\r
 \r
   while ((*FirstString != L'\0') &&\r
-         (*FirstString != *SecondString) &&\r
+         (*FirstString == *SecondString) &&\r
          (Length > 1)) {\r
     FirstString++;\r
     SecondString++;\r
@@ -301,12 +309,15 @@ StrnCmp (
   If Source is NULL, then ASSERT().\r
   If Source and Destination overlap, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r
-  than PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  than PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  Null-terminator, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  Null-terminator, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r
   and Source results in a Unicode string with more than\r
-  PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  Null-terminator, then ASSERT().\r
 \r
   @param  Destination Pointer to a Null-terminated Unicode string.\r
   @param  Source      Pointer to a Null-terminated Unicode string.\r
@@ -348,12 +359,15 @@ StrCat (
   If Source is NULL, then ASSERT().\r
   If Source and Destination overlap, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r
-  than PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  than PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  Null-terminator, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
-  PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  Null-terminator, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r
   and Source results in a Unicode string with more than\r
-  PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r
+  PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  Null-terminator, then ASSERT().\r
 \r
   @param  Destination Pointer to a Null-terminated Unicode string.\r
   @param  Source      Pointer to a Null-terminated Unicode string.\r
@@ -393,7 +407,8 @@ StrnCat (
   If Source is NULL, then ASSERT().\r
   If Source and Destination overlap, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
+  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  then ASSERT().\r
 \r
   @param  Destination Pointer to a Null-terminated ASCII string.\r
   @param  Source      Pointer to a Null-terminated ASCII string.\r
@@ -445,7 +460,8 @@ AsciiStrCpy (
   If Source is NULL, then ASSERT().\r
   If Source and Destination overlap, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
+  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  then ASSERT().\r
 \r
   @param  Destination Pointer to a Null-terminated ASCII string.\r
   @param  Source      Pointer to a Null-terminated ASCII string.\r
@@ -498,7 +514,8 @@ AsciiStrnCpy (
 \r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and String contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
+  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  then ASSERT().\r
 \r
   @param  String  Pointer to a Null-terminated ASCII string.\r
 \r
@@ -520,8 +537,8 @@ AsciiStrLen (
     // If PcdMaximumUnicodeStringLength is not zero,\r
     // length should not more than PcdMaximumUnicodeStringLength\r
     //\r
-    if (FixedPcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
-      ASSERT (Length < FixedPcdGet32 (PcdMaximumAsciiStringLength));\r
+    if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
+      ASSERT (Length < PcdGet32 (PcdMaximumAsciiStringLength));\r
     }\r
   }\r
   return Length;\r
@@ -536,7 +553,8 @@ AsciiStrLen (
 \r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and String contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
+  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  then ASSERT().\r
 \r
   @param  String  Pointer to a Null-terminated ASCII string.\r
 \r
@@ -565,9 +583,11 @@ AsciiStrSize (
   If FirstString is NULL, then ASSERT().\r
   If SecondString is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
+  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
-  than PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
+  than PcdMaximumAsciiStringLength ASCII characters not including the\r
+  Null-terminator, then ASSERT().\r
 \r
   @param  FirstString   Pointer to a Null-terminated ASCII string.\r
   @param  SecondString  Pointer to a Null-terminated ASCII string.\r
@@ -621,9 +641,11 @@ AsciiToUpper (
   If FirstString is NULL, then ASSERT().\r
   If SecondString is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
+  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
-  than PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
+  than PcdMaximumAsciiStringLength ASCII characters not including the\r
+  Null-terminator, then ASSERT().\r
 \r
   @param  FirstString   Pointer to a Null-terminated ASCII string.\r
   @param  SecondString  Pointer to a Null-terminated ASCII string.\r
@@ -670,9 +692,11 @@ AsciiStriCmp (
   If FirstString is NULL, then ASSERT().\r
   If SecondString is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
-  than PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
+  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  then ASSERT().\r
+  If PcdMaximumAsciiStringLength is not zero and SecondString contains more than\r
+  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  then ASSERT().\r
 \r
   @param  FirstString   Pointer to a Null-terminated ASCII string.\r
   @param  SecondString  Pointer to a Null-terminated ASCII string.\r
@@ -689,6 +713,10 @@ AsciiStrnCmp (
   IN      UINTN                     Length\r
   )\r
 {\r
+  if (Length == 0) {\r
+    return 0;\r
+  }\r
+\r
   //\r
   // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
   //\r
@@ -696,7 +724,7 @@ AsciiStrnCmp (
   ASSERT (AsciiStrSize (SecondString));\r
 \r
   while ((*FirstString != '\0') &&\r
-         (*FirstString != *SecondString) &&\r
+         (*FirstString == *SecondString) &&\r
          (Length > 1)) {\r
     FirstString++;\r
     SecondString++;\r
@@ -717,9 +745,11 @@ AsciiStrnCmp (
   If Destination is NULL, then ASSERT().\r
   If Source is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and Destination contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
+  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
+  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero and concatenating Destination and\r
   Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
   ASCII characters, then ASSERT().\r
@@ -763,13 +793,15 @@ AsciiStrCat (
   If Destination is NULL, then ASSERT().\r
   If Source is NULL, then ASSERT().\r
   If Source and Destination overlap, then ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero, and Destination contains more\r
-  than PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
+  If PcdMaximumAsciiStringLength is not zero, and Destination contains more than\r
+  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r
+  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
+  then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and\r
   Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
-  ASCII characters, then ASSERT().\r
+  ASCII characters not including the Null-terminator, then ASSERT().\r
 \r
   @param  Destination Pointer to a Null-terminated ASCII string.\r
   @param  Source      Pointer to a Null-terminated ASCII string.\r
@@ -796,3 +828,51 @@ AsciiStrnCat (
   ASSERT (AsciiStrSize (Destination) != 0);\r
   return Destination;\r
 }\r
+\r
+/**\r
+  Converts an 8-bit value to an 8-bit BCD value.\r
+\r
+  Converts the 8-bit value specified by Value to BCD. The BCD value is\r
+  returned.\r
+\r
+  If Value >= 100, then ASSERT().\r
+\r
+  @param  Value The 8-bit value to convert to BCD. Range 0..99.\r
+\r
+  @return The BCD value\r
+\r
+**/\r
+UINT8\r
+EFIAPI\r
+DecimalToBcd8 (\r
+  IN      UINT8                     Value\r
+  )\r
+{\r
+  ASSERT (Value < 100);\r
+  return ((Value / 10) << 4) | (Value % 10);\r
+}\r
+\r
+/**\r
+  Converts an 8-bit BCD value to an 8-bit value.\r
+\r
+  Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit\r
+  value is returned.\r
+\r
+  If Value >= 0xA0, then ASSERT().\r
+  If (Value & 0x0F) >= 0x0A, then ASSERT().\r
+\r
+  @param  Value The 8-bit BCD value to convert to an 8-bit value.\r
+\r
+  @return The 8-bit value is returned.\r
+\r
+**/\r
+UINT8\r
+EFIAPI\r
+BcdToDecimal8 (\r
+  IN      UINT8                     Value\r
+  )\r
+{\r
+  ASSERT (Value < 0xa0);\r
+  ASSERT ((Value & 0xf) < 0xa);\r
+  return (Value >> 4) * 10 + (Value & 0xf);\r
+}\r