]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BasePrintLib/PrintLib.c
Fixed problem with sign extension in print lib. PrintXY could not print out French...
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLib.c
index 33da6cb6b07795e7f56ec7a56d0a868b2c495c61..145ca3494aa73d8ae4951e00d615dd8d37323bef 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Print Library.\r
 \r
-  Copyright (c) 2006, Intel Corporation<BR>\r
+  Copyright (c) 2006 - 2007, Intel Corporation<BR>\r
   All rights reserved. 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
   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:  PrintLib.c\r
-\r
 **/\r
 \r
+//\r
+// Include common header file for this module.\r
+//\r
+\r
+\r
 #include "PrintLibInternal.h"\r
 \r
-#define WARNING_STATUS_NUMBER          4\r
-#define ERROR_STATUS_NUMBER            24\r
+#define WARNING_STATUS_NUMBER         4\r
+#define ERROR_STATUS_NUMBER           24\r
+#define ASSERT_UNICODE_BUFFER(Buffer) ASSERT ((((UINTN) (Buffer)) & 0x01) == 0)\r
 \r
-STATIC CONST CHAR8 *StatusString [] = {\r
+GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *StatusString [] = {\r
   "Success",                      //  RETURN_SUCCESS                = 0\r
   "Warning Unknown Glyph",        //  RETURN_WARN_UNKNOWN_GLYPH     = 1\r
   "Warning Delete Failure",       //  RETURN_WARN_DELETE_FAILURE    = 2\r
@@ -80,6 +84,7 @@ BasePrintLibVSPrint (
   )\r
 {\r
   CHAR8           *OriginalBuffer;\r
+  CHAR8           *EndBuffer;\r
   CHAR8           ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
   UINTN           BytesPerOutputCharacter;\r
   UINTN           BytesPerFormatCharacter;\r
@@ -110,13 +115,22 @@ BasePrintLibVSPrint (
   }\r
   ASSERT (Buffer != NULL);\r
 \r
-  OriginalBuffer = Buffer;\r
-\r
   if ((Flags & OUTPUT_UNICODE) != 0) {\r
     BytesPerOutputCharacter = 2;\r
   } else {\r
     BytesPerOutputCharacter = 1;\r
   }\r
+\r
+  //\r
+  // Reserve space for the Null terminator.\r
+  //\r
+  BufferSize--;\r
+  OriginalBuffer = Buffer;\r
+  //\r
+  // Set the tag for the end of the input Buffer.\r
+  //\r
+  EndBuffer      = Buffer + BufferSize * BytesPerOutputCharacter;\r
+\r
   if ((Flags & FORMAT_UNICODE) != 0) {\r
     //\r
     // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength\r
@@ -135,20 +149,17 @@ BasePrintLibVSPrint (
     FormatMask = 0xff;\r
   }\r
 \r
-  //\r
-  // Reserve space for the Null terminator.\r
-  //\r
-  BufferSize--;\r
+\r
 \r
   //\r
   // Get the first character from the format string\r
   //\r
-  FormatCharacter = (*Format | (*(Format + 1) << 8)) & FormatMask;\r
+  FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
 \r
   //\r
   // Loop until the end of the format string is reached or the output buffer is full\r
   //\r
-  while (FormatCharacter != 0 && BufferSize > 0) {\r
+  while (FormatCharacter != 0 && Buffer < EndBuffer) {\r
     //\r
     // Clear all the flag bits except those that may have been passed in\r
     //\r
@@ -172,7 +183,7 @@ BasePrintLibVSPrint (
       //\r
       for (Done = FALSE; !Done; ) {\r
         Format += BytesPerFormatCharacter;\r
-        FormatCharacter = (*Format | (*(Format + 1) << 8)) & FormatMask;\r
+        FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
         switch (FormatCharacter) {\r
         case '.': \r
           Flags |= PRECISION; \r
@@ -217,7 +228,7 @@ BasePrintLibVSPrint (
           for (Count = 0; ((FormatCharacter >= '0') &&  (FormatCharacter <= '9')); ){\r
             Count = (Count * 10) + FormatCharacter - '0';\r
             Format += BytesPerFormatCharacter;\r
-            FormatCharacter = (*Format | (*(Format + 1) << 8)) & FormatMask;\r
+            FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
           }\r
           Format -= BytesPerFormatCharacter;\r
           if ((Flags & PRECISION) == 0) {\r
@@ -244,13 +255,6 @@ BasePrintLibVSPrint (
         }\r
       } \r
 \r
-      //\r
-      // Limit the maximum field width to the remaining characters in the output buffer\r
-      //\r
-      if (Width > BufferSize) {\r
-        Width = BufferSize;\r
-      }\r
-\r
       //\r
       // Handle each argument type\r
       //\r
@@ -477,12 +481,6 @@ BasePrintLibVSPrint (
       }\r
     }\r
 \r
-    //\r
-    // Limit the length of the string to append to the remaining characters in the output buffer\r
-    //\r
-    if (Count > BufferSize) {\r
-      Count = BufferSize;\r
-    }\r
     if (Precision < Count) {\r
       Precision = Count;\r
     }\r
@@ -491,18 +489,18 @@ BasePrintLibVSPrint (
     // Pad before the string\r
     //\r
     if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {\r
-      Buffer = BasePrintLibFillBuffer (Buffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
+      Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
     }\r
 \r
     if (ZeroPad) {\r
       if (Prefix != 0) {\r
-        Buffer = BasePrintLibFillBuffer (Buffer, 1, Prefix, BytesPerOutputCharacter);\r
+        Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
       }\r
-      Buffer = BasePrintLibFillBuffer (Buffer, Precision - Count, '0', BytesPerOutputCharacter);\r
+      Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);\r
     } else {\r
-      Buffer = BasePrintLibFillBuffer (Buffer, Precision - Count, ' ', BytesPerOutputCharacter);\r
+      Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);\r
       if (Prefix != 0) {\r
-        Buffer = BasePrintLibFillBuffer (Buffer, 1, Prefix, BytesPerOutputCharacter);\r
+        Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
       }\r
     }\r
 \r
@@ -520,7 +518,7 @@ BasePrintLibVSPrint (
     while (Index < Count) {\r
       ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;\r
 \r
-      Buffer = BasePrintLibFillBuffer (Buffer, 1, ArgumentCharacter, BytesPerOutputCharacter);\r
+      Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);\r
       ArgumentString    += BytesPerArgumentCharacter;\r
       Index++;\r
       if (Comma) {\r
@@ -529,7 +527,7 @@ BasePrintLibVSPrint (
           Digits = 0;\r
           Index++;\r
           if (Index < Count) {\r
-            Buffer = BasePrintLibFillBuffer (Buffer, 1, ',', BytesPerOutputCharacter);\r
+            Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);\r
           }\r
         }\r
       }\r
@@ -539,14 +537,9 @@ BasePrintLibVSPrint (
     // Pad after the string\r
     //\r
     if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {\r
-      Buffer = BasePrintLibFillBuffer (Buffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
+      Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
     }\r
 \r
-    //\r
-    // Reduce the number of characters\r
-    //\r
-    BufferSize -= Count;\r
-\r
     //\r
     // Get the next character from the format string\r
     //\r
@@ -555,13 +548,13 @@ BasePrintLibVSPrint (
     //\r
     // Get the next character from the format string\r
     //\r
-    FormatCharacter = (*Format | (*(Format + 1) << 8)) & FormatMask;\r
+    FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
   }\r
 \r
   //\r
   // Null terminate the Unicode or ASCII string\r
   //\r
-  BasePrintLibFillBuffer (Buffer, 1, 0, BytesPerOutputCharacter);\r
+  BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);\r
   //\r
   // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength\r
   // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
@@ -624,7 +617,9 @@ BasePrintLibSPrint (
   If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
 \r
   If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().\r
+  If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
   If BufferSize > 1 and FormatString is NULL, then ASSERT().\r
+  If BufferSize > 1 and FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than \r
   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
   ASSERT().\r
@@ -651,6 +646,8 @@ UnicodeVSPrint (
   IN  VA_LIST       Marker\r
   )\r
 {\r
+  ASSERT_UNICODE_BUFFER(StartOfBuffer);\r
+  ASSERT_UNICODE_BUFFER(FormatString);\r
   return BasePrintLibVSPrint ((CHAR8 *)StartOfBuffer, BufferSize >> 1, FORMAT_UNICODE | OUTPUT_UNICODE, (CHAR8 *)FormatString, Marker);\r
 }\r
 \r
@@ -667,7 +664,9 @@ UnicodeVSPrint (
   If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
 \r
   If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().\r
+  If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
   If BufferSize > 1 and FormatString is NULL, then ASSERT().\r
+  If BufferSize > 1 and FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than \r
   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
   ASSERT().\r
@@ -713,6 +712,7 @@ UnicodeSPrint (
   If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
 \r
   If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().\r
+  If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
   If BufferSize > 1 and FormatString is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then\r
@@ -740,6 +740,7 @@ UnicodeVSPrintAsciiFormat (
   IN  VA_LIST      Marker\r
   )\r
 {\r
+  ASSERT_UNICODE_BUFFER(StartOfBuffer);\r
   return BasePrintLibVSPrint ((CHAR8 *)StartOfBuffer, BufferSize >> 1, OUTPUT_UNICODE,FormatString, Marker);\r
 }\r
 \r
@@ -757,6 +758,7 @@ UnicodeVSPrintAsciiFormat (
   If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
 \r
   If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().\r
+  If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
   If BufferSize > 1 and FormatString is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then\r
@@ -789,6 +791,60 @@ UnicodeSPrintAsciiFormat (
   return UnicodeVSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker);\r
 }\r
 \r
+/**\r
+  Converts a decimal value to a Null-terminated Unicode string.\r
+  \r
+  Converts the decimal number specified by Value to a Null-terminated Unicode \r
+  string specified by Buffer containing at most Width characters. No padding of spaces \r
+  is ever performed. If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
+  The number of Unicode characters in Buffer is returned not including the Null-terminator.\r
+  If the conversion contains more than Width characters, then only the first\r
+  Width characters are returned, and the total number of characters \r
+  required to perform the conversion is returned.\r
+  Additional conversion parameters are specified in Flags.  \r
+  \r
+  The Flags bit LEFT_JUSTIFY is always ignored.\r
+  All conversions are left justified in Buffer.\r
+  If Width is 0, PREFIX_ZERO is ignored in Flags.\r
+  If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r
+  are inserted every 3rd digit starting from the right.\r
+  If HEX_RADIX is set in Flags, then the output buffer will be \r
+  formatted in hexadecimal format.\r
+  If Value is < 0 and HEX_RADIX is not set in Flags, then the fist character in Buffer is a '-'.\r
+  If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r
+  then Buffer is padded with '0' characters so the combination of the optional '-' \r
+  sign character, '0' characters, digit characters for Value, and the Null-terminator\r
+  add up to Width characters.\r
+  If both COMMA_TYPE and HEX_RADIX are set in Flags, then ASSERT().\r
+  If Buffer is NULL, then ASSERT().\r
+  If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r
+  If unsupported bits are set in Flags, then ASSERT().\r
+  If both COMMA_TYPE and HEX_RADIX are set in Flags, then ASSERT().\r
+  If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r
+\r
+  @param  Buffer  Pointer to the output buffer for the produced Null-terminated\r
+                  Unicode string.\r
+  @param  Flags   The bitmask of flags that specify left justification, zero pad, and commas.\r
+  @param  Value   The 64-bit signed value to convert to a string.\r
+  @param  Width   The maximum number of Unicode characters to place in Buffer, not including\r
+                  the Null-terminator.\r
+  \r
+  @return The number of Unicode characters in Buffer not including the Null-terminator.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+UnicodeValueToString (\r
+  IN OUT CHAR16  *Buffer,\r
+  IN UINTN       Flags,\r
+  IN INT64       Value,\r
+  IN UINTN       Width\r
+  )\r
+{\r
+  ASSERT_UNICODE_BUFFER(Buffer);\r
+  return BasePrintLibConvertValueToString ((CHAR8 *)Buffer, Flags, Value, Width, 2);\r
+}\r
+\r
 /**\r
   Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
   ASCII format string and a VA_LIST argument list.\r
@@ -894,6 +950,7 @@ AsciiSPrint (
 \r
   If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().\r
   If BufferSize > 0 and FormatString is NULL, then ASSERT().\r
+  If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
   ASSERT().\r
@@ -920,6 +977,7 @@ AsciiVSPrintUnicodeFormat (
   IN  VA_LIST       Marker\r
   )\r
 {\r
+  ASSERT_UNICODE_BUFFER (FormatString);\r
   return BasePrintLibVSPrint (StartOfBuffer, BufferSize, FORMAT_UNICODE, (CHAR8 *)FormatString, Marker);\r
 }\r
 \r
@@ -938,6 +996,7 @@ AsciiVSPrintUnicodeFormat (
 \r
   If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().\r
   If BufferSize > 0 and FormatString is NULL, then ASSERT().\r
+  If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
   If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
   ASSERT().\r
@@ -969,58 +1028,13 @@ AsciiSPrintUnicodeFormat (
   return AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);\r
 }\r
 \r
-/**\r
-  Converts a decimal value to a Null-terminated Unicode string.\r
-  \r
-  Converts the decimal number specified by Value to a Null-terminated Unicode \r
-  string specified by Buffer containing at most Width characters.\r
-  If Width is 0 then a width of  MAXIMUM_VALUE_CHARACTERS is assumed.\r
-  The number of Unicode characters in Buffer is returned not including the Null-terminator.\r
-  If the conversion contains more than Width characters, then only the first\r
-  Width characters are returned, and the total number of characters \r
-  required to perform the conversion is returned.\r
-  Additional conversion parameters are specified in Flags.  \r
-  The Flags bit LEFT_JUSTIFY is always ignored.\r
-  All conversions are left justified in Buffer.\r
-  If Width is 0, PREFIX_ZERO is ignored in Flags.\r
-  If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r
-  are inserted every 3rd digit starting from the right.\r
-  If Value is < 0, then the fist character in Buffer is a '-'.\r
-  If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r
-  then Buffer is padded with '0' characters so the combination of the optional '-' \r
-  sign character, '0' characters, digit characters for Value, and the Null-terminator\r
-  add up to Width characters.\r
-\r
-  If Buffer is NULL, then ASSERT().\r
-  If unsupported bits are set in Flags, then ASSERT().\r
-  If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r
-\r
-  @param  Buffer  Pointer to the output buffer for the produced Null-terminated\r
-                  Unicode string.\r
-  @param  Flags   The bitmask of flags that specify left justification, zero pad, and commas.\r
-  @param  Value   The 64-bit signed value to convert to a string.\r
-  @param  Width   The maximum number of Unicode characters to place in Buffer.\r
-  \r
-  @return The number of Unicode characters in Buffer not including the Null-terminator.\r
-\r
-**/\r
-UINTN\r
-EFIAPI\r
-UnicodeValueToString (\r
-  IN OUT CHAR16  *Buffer,\r
-  IN UINTN       Flags,\r
-  IN INT64       Value,\r
-  IN UINTN       Width\r
-  )\r
-{\r
-  return BasePrintLibConvertValueToString ((CHAR8 *)Buffer, Flags, Value, Width, 2);\r
-}\r
 \r
 /**\r
   Converts a decimal value to a Null-terminated ASCII string.\r
   \r
   Converts the decimal number specified by Value to a Null-terminated ASCII string \r
-  specified by Buffer containing at most Width characters.\r
+  specified by Buffer containing at most Width characters. No padding of spaces \r
+  is ever performed.\r
   If Width is 0 then a width of  MAXIMUM_VALUE_CHARACTERS is assumed.\r
   The number of ASCII characters in Buffer is returned not including the Null-terminator.\r
   If the conversion contains more than Width characters, then only the first Width\r
@@ -1030,23 +1044,27 @@ UnicodeValueToString (
   The Flags bit LEFT_JUSTIFY is always ignored.\r
   All conversions are left justified in Buffer.\r
   If Width is 0, PREFIX_ZERO is ignored in Flags.\r
-  If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas \r
+  If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r
   are inserted every 3rd digit starting from the right.\r
-  If Value is < 0, then the fist character in Buffer is a '-'.\r
-  If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, then Buffer\r
-  is padded with '0' characters so the combination of the optional '-'\r
-  sign character, '0' characters, digit characters for Value, and the \r
-  Null-terminator add up to Width characters.\r
-\r
+  If HEX_RADIX is set in Flags, then the output buffer will be \r
+  formatted in hexadecimal format.\r
+  If Value is < 0 and HEX_RADIX is not set in Flags, then the fist character in Buffer is a '-'.\r
+  If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r
+  then Buffer is padded with '0' characters so the combination of the optional '-' \r
+  sign character, '0' characters, digit characters for Value, and the Null-terminator\r
+  add up to Width characters.\r
+  \r
   If Buffer is NULL, then ASSERT().\r
   If unsupported bits are set in Flags, then ASSERT().\r
+  If both COMMA_TYPE and HEX_RADIX are set in Flags, then ASSERT().\r
   If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r
 \r
   @param  Buffer  Pointer to the output buffer for the produced Null-terminated\r
                   ASCII string.\r
   @param  Flags   The bitmask of flags that specify left justification, zero pad, and commas.\r
   @param  Value   The 64-bit signed value to convert to a string.\r
-  @param  Width   The maximum number of ASCII characters to place in Buffer.\r
+  @param  Width   The maximum number of ASCII characters to place in Buffer, not including\r
+                  the Null-terminator.\r
   \r
   @return The number of ASCII characters in Buffer not including the Null-terminator.\r
 \r
@@ -1060,5 +1078,6 @@ AsciiValueToString (
   IN UINTN      Width\r
   )\r
 {\r
-  return BasePrintLibConvertValueToString ((CHAR8 *)Buffer, Flags, Value, Width, 1);\r
+  return BasePrintLibConvertValueToString (Buffer, Flags, Value, Width, 1);\r
 }\r
+\r