X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FLibrary%2FDxePrintLibPrint2Protocol%2FPrintLib.c;h=589d4db619f24ecf55356d9db0892348bff32dea;hp=f0f206192fd76ab202b76d78ea0281117653e95f;hb=9e32e97;hpb=be5ec8c531e4f270b62514e792f1815a05922d3c diff --git a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c index f0f206192f..589d4db619 100644 --- a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c +++ b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c @@ -1,12 +1,12 @@ /** @file - Instance of Print Library based on gEfiPrint2ProtocolGuid. + Instance of Print Library based on gEfiPrint2SProtocolGuid. Implement the print library instance by wrap the interface - provided in the Print2 protocol. This protocol is defined as the internal + provided in the Print2S protocol. This protocol is defined as the internal protocol related to this implementation, not in the public spec. So, this library instance is only for this code base. -Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -24,15 +24,31 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include -#include #include +#include -EFI_PRINT2_PROTOCOL *mPrint2Protocol = NULL; +#define ASSERT_UNICODE_BUFFER(Buffer) ASSERT ((((UINTN) (Buffer)) & 0x01) == 0) + +// +// Safe print checks +// +#define RSIZE_MAX (PcdGet32 (PcdMaximumUnicodeStringLength)) +#define ASCII_RSIZE_MAX (PcdGet32 (PcdMaximumAsciiStringLength)) + +#define SAFE_PRINT_CONSTRAINT_CHECK(Expression, RetVal) \ + do { \ + ASSERT (Expression); \ + if (!(Expression)) { \ + return RetVal; \ + } \ + } while (FALSE) + +EFI_PRINT2S_PROTOCOL *mPrint2SProtocol = NULL; /** - The constructor function caches the pointer to Print2 protocol. + The constructor function caches the pointer to Print2S protocol. - The constructor function locates Print2 protocol from protocol database. + The constructor function locates Print2S protocol from protocol database. It will ASSERT() if that operation fails and it will always return EFI_SUCCESS. @param ImageHandle The firmware allocated handle for the EFI image. @@ -50,13 +66,13 @@ PrintLibConstructor ( { EFI_STATUS Status; - Status = gBS->LocateProtocol ( - &gEfiPrint2ProtocolGuid, - NULL, - (VOID**) &mPrint2Protocol - ); + Status = SystemTable->BootServices->LocateProtocol ( + &gEfiPrint2SProtocolGuid, + NULL, + (VOID**) &mPrint2SProtocol + ); ASSERT_EFI_ERROR (Status); - ASSERT (mPrint2Protocol != NULL); + ASSERT (mPrint2SProtocol != NULL); return Status; } @@ -72,7 +88,8 @@ PrintLibConstructor ( @param BaseListMarker BASE_LIST style variable argument list consumed by processing Format. @param Size The size, in bytes, of the BaseListMarker buffer. - @return The number of bytes in BaseListMarker. 0 if BaseListMarker is too small. + @return TRUE The VA_LIST has been converted to BASE_LIST. + @return FALSE The VA_LIST has not been converted to BASE_LIST. **/ BOOLEAN @@ -91,17 +108,21 @@ DxePrintLibPrint2ProtocolVaListToBaseList ( BOOLEAN Long; BOOLEAN Done; - ASSERT (Format != NULL); ASSERT (BaseListMarker != NULL); + SAFE_PRINT_CONSTRAINT_CHECK ((Format != NULL), FALSE); BaseListStart = BaseListMarker; if (AsciiFormat) { - ASSERT (AsciiStrSize (Format) != 0); + if (ASCII_RSIZE_MAX != 0) { + SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), FALSE); + } BytesPerFormatCharacter = 1; FormatMask = 0xff; } else { - ASSERT (StrSize ((CHAR16 *) Format) != 0); + if (RSIZE_MAX != 0) { + SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16 *)Format, RSIZE_MAX + 1) <= RSIZE_MAX), FALSE); + } BytesPerFormatCharacter = 2; FormatMask = 0xffff; } @@ -178,6 +199,7 @@ DxePrintLibPrint2ProtocolVaListToBaseList ( } case 'X': case 'x': + case 'u': case 'd': if (Long) { BASE_ARG (BaseListMarker, INT64) = VA_ARG (VaListMarker, INT64); @@ -205,6 +227,7 @@ DxePrintLibPrint2ProtocolVaListToBaseList ( // If BASE_LIST is larger than Size, then return FALSE // if ((UINTN)((UINT8 *)BaseListMarker - (UINT8 *)BaseListStart) > Size) { + DEBUG ((DEBUG_ERROR, "The input variable argument list is too long. Please consider breaking into multiple print calls.\n")); return FALSE; } @@ -222,35 +245,41 @@ DxePrintLibPrint2ProtocolVaListToBaseList ( } /** - Produces a Null-terminated Unicode string in an output buffer based on - a Null-terminated Unicode format string and a VA_LIST argument list - + Produces a Null-terminated Unicode string in an output buffer based on + a Null-terminated Unicode format string and a VA_LIST argument list. + + This function is similar as vsnprintf_s defined in C11. + Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer - and BufferSize. - The Unicode string is produced by parsing the format string specified by FormatString. - Arguments are pulled from the variable argument list specified by Marker based on the - contents of the format string. + and BufferSize. + The Unicode string is produced by parsing the format string specified by FormatString. + Arguments are pulled from the variable argument list specified by Marker based on the + contents of the format string. The number of Unicode characters in the produced output buffer is returned not including the Null-terminator. - If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned. - If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). - If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT(). - If BufferSize > 1 and FormatString is NULL, then ASSERT(). - If BufferSize > 1 and FormatString is not aligned on a 16-bit boundary, then ASSERT(). - If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than + If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT(). + If FormatString is not aligned on a 16-bit boundary, then ASSERT(). + + If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If PcdMaximumUnicodeStringLength is not zero, and BufferSize > + (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output + buffer is unmodified and 0 is returned. + If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then - ASSERT(). - If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string - contains more than PcdMaximumUnicodeStringLength Unicode characters not including the - Null-terminator, then ASSERT(). + ASSERT(). Also, the output buffer is unmodified and 0 is returned. - @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated + If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned. + + @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated Unicode string. @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer. - @param FormatString Null-terminated Unicode format string. + @param FormatString A Null-terminated Unicode format string. @param Marker VA_LIST marker for the variable argument list. - + @return The number of Unicode characters in the produced output buffer not including the Null-terminator. @@ -264,49 +293,60 @@ UnicodeVSPrint ( IN VA_LIST Marker ) { - UINT64 BaseListMarker[256 / sizeof (UINT64)]; - - DxePrintLibPrint2ProtocolVaListToBaseList ( - FALSE, - (CHAR8 *)FormatString, - Marker, - (BASE_LIST)BaseListMarker, - sizeof (BaseListMarker) - 8 - ); + UINT64 BaseListMarker[256 / sizeof (UINT64)]; + BOOLEAN Converted; + + ASSERT_UNICODE_BUFFER (StartOfBuffer); + ASSERT_UNICODE_BUFFER (FormatString); + + Converted = DxePrintLibPrint2ProtocolVaListToBaseList ( + FALSE, + (CHAR8 *)FormatString, + Marker, + (BASE_LIST)BaseListMarker, + sizeof (BaseListMarker) - 8 + ); + if (!Converted) { + return 0; + } return UnicodeBSPrint (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker); } /** - Produces a Null-terminated Unicode string in an output buffer based on - a Null-terminated Unicode format string and a BASE_LIST argument list - + Produces a Null-terminated Unicode string in an output buffer based on + a Null-terminated Unicode format string and a BASE_LIST argument list. + Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer - and BufferSize. - The Unicode string is produced by parsing the format string specified by FormatString. - Arguments are pulled from the variable argument list specified by Marker based on the - contents of the format string. + and BufferSize. + The Unicode string is produced by parsing the format string specified by FormatString. + Arguments are pulled from the variable argument list specified by Marker based on the + contents of the format string. The number of Unicode characters in the produced output buffer is returned not including the Null-terminator. - If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned. - If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). - If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT(). - If BufferSize > 1 and FormatString is NULL, then ASSERT(). - If BufferSize > 1 and FormatString is not aligned on a 16-bit boundary, then ASSERT(). - If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than + If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT(). + If FormatString is not aligned on a 16-bit boundary, then ASSERT(). + + If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If PcdMaximumUnicodeStringLength is not zero, and BufferSize > + (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output + buffer is unmodified and 0 is returned. + If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then - ASSERT(). - If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string - contains more than PcdMaximumUnicodeStringLength Unicode characters not including the - Null-terminator, then ASSERT(). + ASSERT(). Also, the output buffer is unmodified and 0 is returned. + + If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned. - @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated + @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated Unicode string. @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer. - @param FormatString Null-terminated Unicode format string. + @param FormatString A Null-terminated Unicode format string. @param Marker BASE_LIST marker for the variable argument list. - + @return The number of Unicode characters in the produced output buffer not including the Null-terminator. @@ -320,37 +360,45 @@ UnicodeBSPrint ( IN BASE_LIST Marker ) { - return mPrint2Protocol->UnicodeBSPrint (StartOfBuffer, BufferSize, FormatString, Marker); + ASSERT_UNICODE_BUFFER (StartOfBuffer); + ASSERT_UNICODE_BUFFER (FormatString); + return mPrint2SProtocol->UnicodeBSPrint (StartOfBuffer, BufferSize, FormatString, Marker); } /** - Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated + Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated Unicode format string and variable argument list. - + + This function is similar as snprintf_s defined in C11. + Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer and BufferSize. The Unicode string is produced by parsing the format string specified by FormatString. Arguments are pulled from the variable argument list based on the contents of the format string. The number of Unicode characters in the produced output buffer is returned not including the Null-terminator. - If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned. - If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). - If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT(). - If BufferSize > 1 and FormatString is NULL, then ASSERT(). - If BufferSize > 1 and FormatString is not aligned on a 16-bit boundary, then ASSERT(). - If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than + If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT(). + If FormatString is not aligned on a 16-bit boundary, then ASSERT(). + + If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If PcdMaximumUnicodeStringLength is not zero, and BufferSize > + (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output + buffer is unmodified and 0 is returned. + If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then - ASSERT(). - If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string - contains more than PcdMaximumUnicodeStringLength Unicode characters not including the - Null-terminator, then ASSERT(). + ASSERT(). Also, the output buffer is unmodified and 0 is returned. + + If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned. - @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated + @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated Unicode string. @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer. - @param FormatString Null-terminated Unicode format string. - @param ... Variable argument list whose contents are accessed based on the + @param FormatString A Null-terminated Unicode format string. + @param ... Variable argument list whose contents are accessed based on the format string specified by FormatString. @return The number of Unicode characters in the produced output buffer not including the @@ -367,40 +415,49 @@ UnicodeSPrint ( ) { VA_LIST Marker; + UINTN NumberOfPrinted; VA_START (Marker, FormatString); - return UnicodeVSPrint (StartOfBuffer, BufferSize, FormatString, Marker); + NumberOfPrinted = UnicodeVSPrint (StartOfBuffer, BufferSize, FormatString, Marker); + VA_END (Marker); + return NumberOfPrinted; } /** Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated - ASCII format string and a VA_LIST argument list - + ASCII format string and a VA_LIST argument list. + + This function is similar as vsnprintf_s defined in C11. + Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer and BufferSize. The Unicode string is produced by parsing the format string specified by FormatString. - Arguments are pulled from the variable argument list specified by Marker based on the + Arguments are pulled from the variable argument list specified by Marker based on the contents of the format string. The number of Unicode characters in the produced output buffer is returned not including the Null-terminator. - If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned. - If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). - If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT(). - If BufferSize > 1 and FormatString is NULL, then ASSERT(). + If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT(). + + If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If PcdMaximumUnicodeStringLength is not zero, and BufferSize > + (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output + buffer is unmodified and 0 is returned. If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than - PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then - ASSERT(). - If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string - contains more than PcdMaximumUnicodeStringLength Unicode characters not including the - Null-terminator, then ASSERT(). + PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then + ASSERT(). Also, the output buffer is unmodified and 0 is returned. + + If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned. - @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated + @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated Unicode string. @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer. - @param FormatString Null-terminated Unicode format string. + @param FormatString A Null-terminated ASCII format string. @param Marker VA_LIST marker for the variable argument list. - + @return The number of Unicode characters in the produced output buffer not including the Null-terminator. @@ -414,48 +471,58 @@ UnicodeVSPrintAsciiFormat ( IN VA_LIST Marker ) { - UINT64 BaseListMarker[256 / sizeof (UINT64)]; - - DxePrintLibPrint2ProtocolVaListToBaseList ( - TRUE, - FormatString, - Marker, - (BASE_LIST)BaseListMarker, - sizeof (BaseListMarker) - 8 - ); + UINT64 BaseListMarker[256 / sizeof (UINT64)]; + BOOLEAN Converted; + + ASSERT_UNICODE_BUFFER (StartOfBuffer); + + Converted = DxePrintLibPrint2ProtocolVaListToBaseList ( + TRUE, + FormatString, + Marker, + (BASE_LIST)BaseListMarker, + sizeof (BaseListMarker) - 8 + ); + if (!Converted) { + return 0; + } return UnicodeBSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker); } /** Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated - ASCII format string and a BASE_LIST argument list - + ASCII format string and a BASE_LIST argument list. + Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer and BufferSize. The Unicode string is produced by parsing the format string specified by FormatString. - Arguments are pulled from the variable argument list specified by Marker based on the + Arguments are pulled from the variable argument list specified by Marker based on the contents of the format string. The number of Unicode characters in the produced output buffer is returned not including the Null-terminator. - If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned. - If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). - If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT(). - If BufferSize > 1 and FormatString is NULL, then ASSERT(). + If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT(). + + If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If PcdMaximumUnicodeStringLength is not zero, and BufferSize > + (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output + buffer is unmodified and 0 is returned. If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than - PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then - ASSERT(). - If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string - contains more than PcdMaximumUnicodeStringLength Unicode characters not including the - Null-terminator, then ASSERT(). + PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then + ASSERT(). Also, the output buffer is unmodified and 0 is returned. - @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated + If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned. + + @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated Unicode string. @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer. - @param FormatString Null-terminated Unicode format string. + @param FormatString A Null-terminated ASCII format string. @param Marker BASE_LIST marker for the variable argument list. - + @return The number of Unicode characters in the produced output buffer not including the Null-terminator. @@ -469,39 +536,46 @@ UnicodeBSPrintAsciiFormat ( IN BASE_LIST Marker ) { - return mPrint2Protocol->UnicodeBSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker); + ASSERT_UNICODE_BUFFER (StartOfBuffer); + return mPrint2SProtocol->UnicodeBSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker); } /** - Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated + Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated ASCII format string and variable argument list. - + + This function is similar as snprintf_s defined in C11. + Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer and BufferSize. The Unicode string is produced by parsing the format string specified by FormatString. - Arguments are pulled from the variable argument list based on the contents of the + Arguments are pulled from the variable argument list based on the contents of the format string. The number of Unicode characters in the produced output buffer is returned not including the Null-terminator. - If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned. - If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). - If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT(). - If BufferSize > 1 and FormatString is NULL, then ASSERT(). + If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT(). + + If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If PcdMaximumUnicodeStringLength is not zero, and BufferSize > + (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output + buffer is unmodified and 0 is returned. If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than - PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then - ASSERT(). - If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string - contains more than PcdMaximumUnicodeStringLength Unicode characters not including the - Null-terminator, then ASSERT(). + PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then + ASSERT(). Also, the output buffer is unmodified and 0 is returned. - @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated + If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned. + + @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated Unicode string. @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer. - @param FormatString Null-terminated Unicode format string. - @param ... Variable argument list whose contents are accessed based on the + @param FormatString A Null-terminated ASCII format string. + @param ... Variable argument list whose contents are accessed based on the format string specified by FormatString. - + @return The number of Unicode characters in the produced output buffer not including the Null-terminator. @@ -516,9 +590,12 @@ UnicodeSPrintAsciiFormat ( ) { VA_LIST Marker; + UINTN NumberOfPrinted; VA_START (Marker, FormatString); - return UnicodeVSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker); + NumberOfPrinted = UnicodeVSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker); + VA_END (Marker); + return NumberOfPrinted; } /** @@ -571,37 +648,120 @@ UnicodeValueToString ( IN UINTN Width ) { - return mPrint2Protocol->UnicodeValueToString (Buffer, Flags, Value, Width); + RETURN_STATUS Status; + UINTN BufferSize; + + if (Width == 0) { + BufferSize = (MAXIMUM_VALUE_CHARACTERS + 1) * sizeof (CHAR16); + } else { + BufferSize = (Width + 1) * sizeof (CHAR16); + } + + Status = mPrint2SProtocol->UnicodeValueToStringS (Buffer, BufferSize, Flags, Value, Width); + if (RETURN_ERROR (Status)) { + return 0; + } + + return StrnLenS (Buffer, BufferSize / sizeof (CHAR16)); +} + +/** + Converts a decimal value to a Null-terminated Unicode string. + + Converts the decimal number specified by Value to a Null-terminated Unicode + string specified by Buffer containing at most Width characters. No padding of + spaces is ever performed. If Width is 0 then a width of + MAXIMUM_VALUE_CHARACTERS is assumed. If the conversion contains more than + Width characters, then only the first Width characters are placed in Buffer. + Additional conversion parameters are specified in Flags. + + The Flags bit LEFT_JUSTIFY is always ignored. + All conversions are left justified in Buffer. + If Width is 0, PREFIX_ZERO is ignored in Flags. + If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and + commas are inserted every 3rd digit starting from the right. + If RADIX_HEX is set in Flags, then the output buffer will be formatted in + hexadecimal format. + If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in + Buffer is a '-'. + If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, then + Buffer is padded with '0' characters so the combination of the optional '-' + sign character, '0' characters, digit characters for Value, and the + Null-terminator add up to Width characters. + + If Buffer is not aligned on a 16-bit boundary, then ASSERT(). + If an error would be returned, then the function will also ASSERT(). + + @param Buffer The pointer to the output buffer for the produced + Null-terminated Unicode string. + @param BufferSize The size of Buffer in bytes, including the + Null-terminator. + @param Flags The bitmask of flags that specify left justification, + zero pad, and commas. + @param Value The 64-bit signed value to convert to a string. + @param Width The maximum number of Unicode characters to place in + Buffer, not including the Null-terminator. + + @retval RETURN_SUCCESS The decimal value is converted. + @retval RETURN_BUFFER_TOO_SMALL If BufferSize cannot hold the converted + value. + @retval RETURN_INVALID_PARAMETER If Buffer is NULL. + If PcdMaximumUnicodeStringLength is not + zero, and BufferSize is greater than + (PcdMaximumUnicodeStringLength * + sizeof (CHAR16) + 1). + If unsupported bits are set in Flags. + If both COMMA_TYPE and RADIX_HEX are set in + Flags. + If Width >= MAXIMUM_VALUE_CHARACTERS. + +**/ +RETURN_STATUS +EFIAPI +UnicodeValueToStringS ( + IN OUT CHAR16 *Buffer, + IN UINTN BufferSize, + IN UINTN Flags, + IN INT64 Value, + IN UINTN Width + ) +{ + return mPrint2SProtocol->UnicodeValueToStringS (Buffer, BufferSize, Flags, Value, Width); } /** Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated ASCII format string and a VA_LIST argument list. - + + This function is similar as vsnprintf_s defined in C11. + Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer and BufferSize. The ASCII string is produced by parsing the format string specified by FormatString. - Arguments are pulled from the variable argument list specified by Marker based on + Arguments are pulled from the variable argument list specified by Marker based on the contents of the format string. The number of ASCII characters in the produced output buffer is returned not including the Null-terminator. - If BufferSize is 0, then no output buffer is produced and 0 is returned. - If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). - If BufferSize > 0 and FormatString is NULL, then ASSERT(). + If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If PcdMaximumAsciiStringLength is not zero, and BufferSize > + (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer + is unmodified and 0 is returned. If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than - PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then - ASSERT(). - If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string - contains more than PcdMaximumAsciiStringLength ASCII characters not including the - Null-terminator, then ASSERT(). + PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then + ASSERT(). Also, the output buffer is unmodified and 0 is returned. - @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated + If BufferSize is 0, then no output buffer is produced and 0 is returned. + + @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated ASCII string. @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer. - @param FormatString Null-terminated Unicode format string. + @param FormatString A Null-terminated ASCII format string. @param Marker VA_LIST marker for the variable argument list. - + @return The number of ASCII characters in the produced output buffer not including the Null-terminator. @@ -615,15 +775,19 @@ AsciiVSPrint ( IN VA_LIST Marker ) { - UINT64 BaseListMarker[256 / sizeof (UINT64)]; - - DxePrintLibPrint2ProtocolVaListToBaseList ( - TRUE, - FormatString, - Marker, - (BASE_LIST)BaseListMarker, - sizeof (BaseListMarker) - 8 - ); + UINT64 BaseListMarker[256 / sizeof (UINT64)]; + BOOLEAN Converted; + + Converted = DxePrintLibPrint2ProtocolVaListToBaseList ( + TRUE, + FormatString, + Marker, + (BASE_LIST)BaseListMarker, + sizeof (BaseListMarker) - 8 + ); + if (!Converted) { + return 0; + } return AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker); } @@ -631,31 +795,34 @@ AsciiVSPrint ( /** Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated ASCII format string and a BASE_LIST argument list. - + Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer and BufferSize. The ASCII string is produced by parsing the format string specified by FormatString. - Arguments are pulled from the variable argument list specified by Marker based on + Arguments are pulled from the variable argument list specified by Marker based on the contents of the format string. The number of ASCII characters in the produced output buffer is returned not including the Null-terminator. - If BufferSize is 0, then no output buffer is produced and 0 is returned. - If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). - If BufferSize > 0 and FormatString is NULL, then ASSERT(). + If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If PcdMaximumAsciiStringLength is not zero, and BufferSize > + (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer + is unmodified and 0 is returned. If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than - PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then - ASSERT(). - If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string - contains more than PcdMaximumAsciiStringLength ASCII characters not including the - Null-terminator, then ASSERT(). + PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then + ASSERT(). Also, the output buffer is unmodified and 0 is returned. + + If BufferSize is 0, then no output buffer is produced and 0 is returned. - @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated + @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated ASCII string. @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer. - @param FormatString Null-terminated Unicode format string. + @param FormatString A Null-terminated ASCII format string. @param Marker BASE_LIST marker for the variable argument list. - + @return The number of ASCII characters in the produced output buffer not including the Null-terminator. @@ -669,36 +836,41 @@ AsciiBSPrint ( IN BASE_LIST Marker ) { - return mPrint2Protocol->AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, Marker); + return mPrint2SProtocol->AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, Marker); } /** Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated ASCII format string and variable argument list. - + + This function is similar as snprintf_s defined in C11. + Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer and BufferSize. The ASCII string is produced by parsing the format string specified by FormatString. - Arguments are pulled from the variable argument list based on the contents of the + Arguments are pulled from the variable argument list based on the contents of the format string. The number of ASCII characters in the produced output buffer is returned not including the Null-terminator. - If BufferSize is 0, then no output buffer is produced and 0 is returned. - If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). - If BufferSize > 0 and FormatString is NULL, then ASSERT(). + If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If PcdMaximumAsciiStringLength is not zero, and BufferSize > + (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer + is unmodified and 0 is returned. If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than - PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then - ASSERT(). - If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string - contains more than PcdMaximumAsciiStringLength ASCII characters not including the - Null-terminator, then ASSERT(). + PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then + ASSERT(). Also, the output buffer is unmodified and 0 is returned. + + If BufferSize is 0, then no output buffer is produced and 0 is returned. - @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated + @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated ASCII string. @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer. - @param FormatString Null-terminated Unicode format string. - @param ... Variable argument list whose contents are accessed based on the + @param FormatString A Null-terminated ASCII format string. + @param ... Variable argument list whose contents are accessed based on the format string specified by FormatString. @return The number of ASCII characters in the produced output buffer not including the @@ -715,40 +887,49 @@ AsciiSPrint ( ) { VA_LIST Marker; + UINTN NumberOfPrinted; VA_START (Marker, FormatString); - return AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker); + NumberOfPrinted = AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker); + VA_END (Marker); + return NumberOfPrinted; } /** Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated - ASCII format string and a VA_LIST argument list. - + Unicode format string and a VA_LIST argument list. + + This function is similar as vsnprintf_s defined in C11. + Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer and BufferSize. The ASCII string is produced by parsing the format string specified by FormatString. - Arguments are pulled from the variable argument list specified by Marker based on + Arguments are pulled from the variable argument list specified by Marker based on the contents of the format string. The number of ASCII characters in the produced output buffer is returned not including the Null-terminator. - If BufferSize is 0, then no output buffer is produced and 0 is returned. - If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). - If BufferSize > 0 and FormatString is NULL, then ASSERT(). - If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT(). + If FormatString is not aligned on a 16-bit boundary, then ASSERT(). + + If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If PcdMaximumAsciiStringLength is not zero, and BufferSize > + (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer + is unmodified and 0 is returned. If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then - ASSERT(). - If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string - contains more than PcdMaximumAsciiStringLength ASCII characters not including the - Null-terminator, then ASSERT(). + ASSERT(). Also, the output buffer is unmodified and 0 is returned. + + If BufferSize is 0, then no output buffer is produced and 0 is returned. - @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated + @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated ASCII string. @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer. - @param FormatString Null-terminated Unicode format string. + @param FormatString A Null-terminated Unicode format string. @param Marker VA_LIST marker for the variable argument list. - + @return The number of ASCII characters in the produced output buffer not including the Null-terminator. @@ -762,48 +943,58 @@ AsciiVSPrintUnicodeFormat ( IN VA_LIST Marker ) { - UINT64 BaseListMarker[256 / sizeof (UINT64)]; - - DxePrintLibPrint2ProtocolVaListToBaseList ( - FALSE, - (CHAR8 *)FormatString, - Marker, - (BASE_LIST)BaseListMarker, - sizeof (BaseListMarker) - 8 - ); + UINT64 BaseListMarker[256 / sizeof (UINT64)]; + BOOLEAN Converted; + + ASSERT_UNICODE_BUFFER (FormatString); + + Converted = DxePrintLibPrint2ProtocolVaListToBaseList ( + FALSE, + (CHAR8 *)FormatString, + Marker, + (BASE_LIST)BaseListMarker, + sizeof (BaseListMarker) - 8 + ); + if (!Converted) { + return 0; + } return AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker); } /** Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated - ASCII format string and a BASE_LIST argument list. - + Unicode format string and a BASE_LIST argument list. + Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer and BufferSize. The ASCII string is produced by parsing the format string specified by FormatString. - Arguments are pulled from the variable argument list specified by Marker based on + Arguments are pulled from the variable argument list specified by Marker based on the contents of the format string. The number of ASCII characters in the produced output buffer is returned not including the Null-terminator. - If BufferSize is 0, then no output buffer is produced and 0 is returned. - If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). - If BufferSize > 0 and FormatString is NULL, then ASSERT(). - If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT(). + If FormatString is not aligned on a 16-bit boundary, then ASSERT(). + + If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If PcdMaximumAsciiStringLength is not zero, and BufferSize > + (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer + is unmodified and 0 is returned. If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then - ASSERT(). - If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string - contains more than PcdMaximumAsciiStringLength ASCII characters not including the - Null-terminator, then ASSERT(). + ASSERT(). Also, the output buffer is unmodified and 0 is returned. + + If BufferSize is 0, then no output buffer is produced and 0 is returned. - @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated + @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated ASCII string. @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer. - @param FormatString Null-terminated Unicode format string. + @param FormatString A Null-terminated Unicode format string. @param Marker BASE_LIST marker for the variable argument list. - + @return The number of ASCII characters in the produced output buffer not including the Null-terminator. @@ -817,37 +1008,44 @@ AsciiBSPrintUnicodeFormat ( IN BASE_LIST Marker ) { - return mPrint2Protocol->AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker); + ASSERT_UNICODE_BUFFER (FormatString); + return mPrint2SProtocol->AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker); } /** Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated - ASCII format string and variable argument list. - + Unicode format string and variable argument list. + + This function is similar as snprintf_s defined in C11. + Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer and BufferSize. The ASCII string is produced by parsing the format string specified by FormatString. - Arguments are pulled from the variable argument list based on the contents of the + Arguments are pulled from the variable argument list based on the contents of the format string. The number of ASCII characters in the produced output buffer is returned not including the Null-terminator. - If BufferSize is 0, then no output buffer is produced and 0 is returned. - If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). - If BufferSize > 0 and FormatString is NULL, then ASSERT(). - If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT(). + If FormatString is not aligned on a 16-bit boundary, then ASSERT(). + + If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is + unmodified and 0 is returned. + If PcdMaximumAsciiStringLength is not zero, and BufferSize > + (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer + is unmodified and 0 is returned. If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then - ASSERT(). - If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string - contains more than PcdMaximumAsciiStringLength ASCII characters not including the - Null-terminator, then ASSERT(). + ASSERT(). Also, the output buffer is unmodified and 0 is returned. + + If BufferSize is 0, then no output buffer is produced and 0 is returned. - @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated + @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated ASCII string. @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer. - @param FormatString Null-terminated Unicode format string. - @param ... Variable argument list whose contents are accessed based on the + @param FormatString A Null-terminated Unicode format string. + @param ... Variable argument list whose contents are accessed based on the format string specified by FormatString. @return The number of ASCII characters in the produced output buffer not including the @@ -864,9 +1062,12 @@ AsciiSPrintUnicodeFormat ( ) { VA_LIST Marker; + UINTN NumberOfPrinted; VA_START (Marker, FormatString); - return AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker); + NumberOfPrinted = AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker); + VA_END (Marker); + return NumberOfPrinted; } @@ -919,7 +1120,84 @@ AsciiValueToString ( IN UINTN Width ) { - return mPrint2Protocol->AsciiValueToString (Buffer, Flags, Value, Width); + RETURN_STATUS Status; + UINTN BufferSize; + + if (Width == 0) { + BufferSize = (MAXIMUM_VALUE_CHARACTERS + 1) * sizeof (CHAR8); + } else { + BufferSize = (Width + 1) * sizeof (CHAR8); + } + + Status = mPrint2SProtocol->AsciiValueToStringS (Buffer, BufferSize, Flags, Value, Width); + if (RETURN_ERROR (Status)) { + return 0; + } + + return AsciiStrnLenS (Buffer, BufferSize / sizeof (CHAR8)); +} + +/** + Converts a decimal value to a Null-terminated Ascii string. + + Converts the decimal number specified by Value to a Null-terminated Ascii + string specified by Buffer containing at most Width characters. No padding of + spaces is ever performed. If Width is 0 then a width of + MAXIMUM_VALUE_CHARACTERS is assumed. If the conversion contains more than + Width characters, then only the first Width characters are placed in Buffer. + Additional conversion parameters are specified in Flags. + + The Flags bit LEFT_JUSTIFY is always ignored. + All conversions are left justified in Buffer. + If Width is 0, PREFIX_ZERO is ignored in Flags. + If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and + commas are inserted every 3rd digit starting from the right. + If RADIX_HEX is set in Flags, then the output buffer will be formatted in + hexadecimal format. + If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in + Buffer is a '-'. + If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, then + Buffer is padded with '0' characters so the combination of the optional '-' + sign character, '0' characters, digit characters for Value, and the + Null-terminator add up to Width characters. + + If Buffer is not aligned on a 16-bit boundary, then ASSERT(). + If an error would be returned, then the function will also ASSERT(). + + @param Buffer The pointer to the output buffer for the produced + Null-terminated Ascii string. + @param BufferSize The size of Buffer in bytes, including the + Null-terminator. + @param Flags The bitmask of flags that specify left justification, + zero pad, and commas. + @param Value The 64-bit signed value to convert to a string. + @param Width The maximum number of Ascii characters to place in + Buffer, not including the Null-terminator. + + @retval RETURN_SUCCESS The decimal value is converted. + @retval RETURN_BUFFER_TOO_SMALL If BufferSize cannot hold the converted + value. + @retval RETURN_INVALID_PARAMETER If Buffer is NULL. + If PcdMaximumAsciiStringLength is not + zero, and BufferSize is greater than + PcdMaximumAsciiStringLength. + If unsupported bits are set in Flags. + If both COMMA_TYPE and RADIX_HEX are set in + Flags. + If Width >= MAXIMUM_VALUE_CHARACTERS. + +**/ +RETURN_STATUS +EFIAPI +AsciiValueToStringS ( + IN OUT CHAR8 *Buffer, + IN UINTN BufferSize, + IN UINTN Flags, + IN INT64 Value, + IN UINTN Width + ) +{ + return mPrint2SProtocol->AsciiValueToStringS (Buffer, BufferSize, Flags, Value, Width); } #define PREFIX_SIGN BIT1 @@ -932,6 +1210,7 @@ AsciiValueToString ( #define PRECISION BIT11 #define ARGUMENT_REVERSED BIT12 #define COUNT_ONLY_NO_PRINT BIT13 +#define UNSIGNED_TYPE BIT14 // // Record date and time information @@ -1057,20 +1336,24 @@ InternalPrintLibSPrint ( ) { VA_LIST Marker; + UINTN NumberOfPrinted; VA_START (Marker, FormatString); - return InternalPrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL); + NumberOfPrinted = InternalPrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL); + VA_END (Marker); + return NumberOfPrinted; } -#define WARNING_STATUS_NUMBER 4 -#define ERROR_STATUS_NUMBER 24 +#define WARNING_STATUS_NUMBER 5 +#define ERROR_STATUS_NUMBER 33 -GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mStatusString[] = { +GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 * CONST mStatusString[] = { "Success", // RETURN_SUCCESS = 0 "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH = 1 "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE = 2 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE = 3 "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL = 4 + "Warning Stale Data", // RETURN_WARN_STALE_DATA = 5 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT @@ -1094,9 +1377,56 @@ GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mStatusString[] = { "Aborted", // RETURN_ABORTED = 21 | MAX_BIT "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT - "Protocol Error" // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT + "Protocol Error", // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT + "Incompatible Version", // RETURN_INCOMPATIBLE_VERSION = 25 | MAX_BIT + "Security Violation", // RETURN_SECURITY_VIOLATION = 26 | MAX_BIT + "CRC Error", // RETURN_CRC_ERROR = 27 | MAX_BIT + "End of Media", // RETURN_END_OF_MEDIA = 28 | MAX_BIT + "Reserved (29)", // RESERVED = 29 | MAX_BIT + "Reserved (30)", // RESERVED = 30 | MAX_BIT + "End of File", // RETURN_END_OF_FILE = 31 | MAX_BIT + "Invalid Language", // RETURN_INVALID_LANGUAGE = 32 | MAX_BIT + "Compromised Data" // RETURN_COMPROMISED_DATA = 33 | MAX_BIT }; +/** + Internal function that places the character into the Buffer. + + Internal function that places ASCII or Unicode character into the Buffer. + + @param Buffer The buffer to place the Unicode or ASCII string. + @param EndBuffer The end of the input Buffer. No characters will be + placed after that. + @param Length The count of character to be placed into Buffer. + (Negative value indicates no buffer fill.) + @param Character The character to be placed into Buffer. + @param Increment The character increment in Buffer. + + @return Buffer. + +**/ +CHAR8 * +InternalPrintLibFillBuffer ( + OUT CHAR8 *Buffer, + IN CHAR8 *EndBuffer, + IN INTN Length, + IN UINTN Character, + IN INTN Increment + ) +{ + INTN Index; + + for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) { + *Buffer = (CHAR8) Character; + if (Increment != 1) { + *(Buffer + 1) = (CHAR8)(Character >> 8); + } + Buffer += Increment; + } + + return Buffer; +} + /** Worker function that produces a Null-terminated string in an output buffer based on a Null-terminated format string and a VA_LIST argument list. @@ -1172,45 +1502,86 @@ InternalPrintLibSPrintMarker ( // DxePrintLibPrint2Protocol (both PrintLib instances). // - ASSERT(Flags & COUNT_ONLY_NO_PRINT); - - if (Flags & OUTPUT_UNICODE) { - BytesPerOutputCharacter = 2; - } else { - BytesPerOutputCharacter = 1; + // + // 1. Buffer shall not be a null pointer when both BufferSize > 0 and + // COUNT_ONLY_NO_PRINT is not set in Flags. + // + if ((BufferSize > 0) && ((Flags & COUNT_ONLY_NO_PRINT) == 0)) { + SAFE_PRINT_CONSTRAINT_CHECK ((Buffer != NULL), 0); } - LengthToReturn = 0; - // - // Reserve space for the Null terminator. + // 2. Format shall not be a null pointer when BufferSize > 0 or when + // COUNT_ONLY_NO_PRINT is set in Flags. // - BufferSize--; - OriginalBuffer = Buffer; + if ((BufferSize > 0) || ((Flags & COUNT_ONLY_NO_PRINT) != 0)) { + SAFE_PRINT_CONSTRAINT_CHECK ((Format != NULL), 0); + } // - // Set the tag for the end of the input Buffer. + // 3. BufferSize shall not be greater than RSIZE_MAX for Unicode output or + // ASCII_RSIZE_MAX for Ascii output. // - EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter; + if ((Flags & OUTPUT_UNICODE) != 0) { + if (RSIZE_MAX != 0) { + SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= RSIZE_MAX), 0); + } + BytesPerOutputCharacter = 2; + } else { + if (ASCII_RSIZE_MAX != 0) { + SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= ASCII_RSIZE_MAX), 0); + } + BytesPerOutputCharacter = 1; + } + // + // 4. Format shall not contain more than RSIZE_MAX Unicode characters or + // ASCII_RSIZE_MAX Ascii characters. + // if ((Flags & FORMAT_UNICODE) != 0) { - // - // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength - // Unicode characters if PcdMaximumUnicodeStringLength is not zero. - // - ASSERT (StrSize ((CHAR16 *) Format) != 0); + if (RSIZE_MAX != 0) { + SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16 *)Format, RSIZE_MAX + 1) <= RSIZE_MAX), 0); + } BytesPerFormatCharacter = 2; FormatMask = 0xffff; } else { - // - // Make sure format string cannot contain more than PcdMaximumAsciiStringLength - // Ascii characters if PcdMaximumAsciiStringLength is not zero. - // - ASSERT (AsciiStrSize (Format) != 0); + if (ASCII_RSIZE_MAX != 0) { + SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), 0); + } BytesPerFormatCharacter = 1; FormatMask = 0xff; } + if ((Flags & COUNT_ONLY_NO_PRINT) != 0) { + if (BufferSize == 0) { + Buffer = NULL; + } + } else { + // + // We can run without a Buffer for counting only. + // + if (BufferSize == 0) { + return 0; + } + } + + LengthToReturn = 0; + EndBuffer = NULL; + OriginalBuffer = NULL; + + // + // Reserve space for the Null terminator. + // + if (Buffer != NULL) { + BufferSize--; + OriginalBuffer = Buffer; + + // + // Set the tag for the end of the input Buffer. + // + EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter; + } + // // Get the first character from the format string // @@ -1219,11 +1590,14 @@ InternalPrintLibSPrintMarker ( // // Loop until the end of the format string is reached or the output buffer is full // - while (FormatCharacter != 0 && Buffer < EndBuffer) { + while (FormatCharacter != 0) { + if ((Buffer != NULL) && (Buffer >= EndBuffer)) { + break; + } // // Clear all the flag bits except those that may have been passed in // - Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT); + Flags &= (UINTN) (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT); // // Set the default width to zero, and the default precision to 1 @@ -1331,10 +1705,13 @@ InternalPrintLibSPrintMarker ( // // Flag space, +, 0, L & l are invalid for type p. // - Flags &= ~(PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE); + Flags &= ~((UINTN) (PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE)); if (sizeof (VOID *) > 4) { Flags |= LONG_TYPE; } + // + // break skipped on purpose + // case 'X': Flags |= PREFIX_ZERO; // @@ -1345,10 +1722,18 @@ InternalPrintLibSPrintMarker ( // // break skipped on purpose // + case 'u': + if ((Flags & RADIX_HEX) == 0) { + Flags &= ~((UINTN) (PREFIX_SIGN)); + Flags |= UNSIGNED_TYPE; + } + // + // break skipped on purpose + // case 'd': if ((Flags & LONG_TYPE) == 0) { // - // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int". + // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int". // This assumption is made so the format string definition is compatible with the ANSI C // Specification for formatted strings. It is recommended that the Base Types be used // everywhere, but in this one case, compliance with ANSI C is more important, and @@ -1379,20 +1764,30 @@ InternalPrintLibSPrintMarker ( if ((Flags & RADIX_HEX) == 0) { Radix = 10; if (Comma) { - Flags &= (~PREFIX_ZERO); + Flags &= ~((UINTN) PREFIX_ZERO); Precision = 1; } - if (Value < 0) { + if (Value < 0 && (Flags & UNSIGNED_TYPE) == 0) { Flags |= PREFIX_SIGN; Prefix = '-'; Value = -Value; + } else if ((Flags & UNSIGNED_TYPE) != 0 && (Flags & LONG_TYPE) == 0) { + // + // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int". + // This assumption is made so the format string definition is compatible with the ANSI C + // Specification for formatted strings. It is recommended that the Base Types be used + // everywhere, but in this one case, compliance with ANSI C is more important, and + // provides an implementation that is compatible with that largest possible set of CPU + // architectures. This is why the type "unsigned int" is used in this one case. + // + Value = (unsigned int)Value; } } else { Radix = 16; Comma = FALSE; if ((Flags & LONG_TYPE) == 0 && Value < 0) { // - // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int". + // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int". // This assumption is made so the format string definition is compatible with the ANSI C // Specification for formatted strings. It is recommended that the Base Types be used // everywhere, but in this one case, compliance with ANSI C is more important, and @@ -1448,7 +1843,7 @@ InternalPrintLibSPrintMarker ( ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *); } if (ArgumentString == NULL) { - Flags &= (~ARGUMENT_UNICODE); + Flags &= (~(UINTN)ARGUMENT_UNICODE); ArgumentString = ""; } // @@ -1661,17 +2056,32 @@ InternalPrintLibSPrintMarker ( // if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) { LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter); + } } if (ZeroPad) { if (Prefix != 0) { LengthToReturn += (1 * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter); + } } LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter); + } } else { LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter); + } if (Prefix != 0) { LengthToReturn += (1 * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter); + } } } @@ -1690,6 +2100,9 @@ InternalPrintLibSPrintMarker ( ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask; LengthToReturn += (1 * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter); + } ArgumentString += BytesPerArgumentCharacter; Index++; if (Comma) { @@ -1699,6 +2112,9 @@ InternalPrintLibSPrintMarker ( Index++; if (Index < Count) { LengthToReturn += (1 * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter); + } } } } @@ -1709,6 +2125,9 @@ InternalPrintLibSPrintMarker ( // if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) { LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) { + Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter); + } } // @@ -1722,16 +2141,30 @@ InternalPrintLibSPrintMarker ( FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask; } - return (LengthToReturn / BytesPerOutputCharacter); + if ((Flags & COUNT_ONLY_NO_PRINT) != 0) { + return (LengthToReturn / BytesPerOutputCharacter); + } + + ASSERT (Buffer != NULL); + // + // Null terminate the Unicode or ASCII string + // + InternalPrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter); + + return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter); } /** Returns the number of characters that would be produced by if the formatted output were produced not including the Null-terminator. - If FormatString is NULL, then ASSERT(). If FormatString is not aligned on a 16-bit boundary, then ASSERT(). + If FormatString is NULL, then ASSERT() and 0 is returned. + If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more + than PcdMaximumUnicodeStringLength Unicode characters not including the + Null-terminator, then ASSERT() and 0 is returned. + @param[in] FormatString A Null-terminated Unicode format string. @param[in] Marker VA_LIST marker for the variable argument list. @@ -1745,7 +2178,7 @@ SPrintLength ( IN VA_LIST Marker ) { - ASSERT(FormatString != NULL); + ASSERT_UNICODE_BUFFER (FormatString); return InternalPrintLibSPrintMarker (NULL, 0, FORMAT_UNICODE | OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL); } @@ -1753,7 +2186,10 @@ SPrintLength ( Returns the number of characters that would be produced by if the formatted output were produced not including the Null-terminator. - If FormatString is NULL, then ASSERT(). + If FormatString is NULL, then ASSERT() and 0 is returned. + If PcdMaximumAsciiStringLength is not zero, and FormatString contains more + than PcdMaximumAsciiStringLength Ascii characters not including the + Null-terminator, then ASSERT() and 0 is returned. @param[in] FormatString A Null-terminated ASCII format string. @param[in] Marker VA_LIST marker for the variable argument list. @@ -1768,6 +2204,5 @@ SPrintLengthAsciiFormat ( IN VA_LIST Marker ) { - ASSERT(FormatString != NULL); return InternalPrintLibSPrintMarker (NULL, 0, OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL); }