From nobody Sat Nov 2 14:33:21 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1486620179236781.3908799977744; Wed, 8 Feb 2017 22:02:59 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id DEF9F82095; Wed, 8 Feb 2017 22:02:57 -0800 (PST) Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 07C2781FB0 for ; Wed, 8 Feb 2017 22:02:56 -0800 (PST) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga104.jf.intel.com with ESMTP; 08 Feb 2017 22:02:56 -0800 Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.13]) by orsmga001.jf.intel.com with ESMTP; 08 Feb 2017 22:02:54 -0800 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,349,1484035200"; d="scan'208";a="1092615574" From: Hao Wu To: edk2-devel@lists.01.org Date: Thu, 9 Feb 2017 14:02:47 +0800 Message-Id: <1486620170-38136-2-git-send-email-hao.a.wu@intel.com> X-Mailer: git-send-email 1.9.5.msysgit.0 In-Reply-To: <1486620170-38136-1-git-send-email-hao.a.wu@intel.com> References: <1486620170-38136-1-git-send-email-hao.a.wu@intel.com> Subject: [edk2] [PATCH v2 1/4] MdePkg/BasePrintLib: Add safe print functions [A|U]ValueToStringS X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Hao Wu , Michael Kinney , Jiewen Yao , Liming Gao MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add the following 2 APIs: UnicodeValueToStringS AsciiValueToStringS These safe version APIs are used to enhance their counterpart (APIs without trailing 'S' in function names). They perform checks to the input parameters and will return relative status to reflect the check result. Return RETURN_INVALID_PARAMETER when: 1). The input Buffer is NULL. 2). The input BufferSize is greater than (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1) for UnicodeValueToStringS or greater than PcdMaximumAsciiStringLength for AsciiValueToStringS. 3). The input Flags is not set properly. 4). The input Width is not smaller than MAXIMUM_VALUE_CHARACTERS. Return RETURN_BUFFER_TOO_SMALL when: 1). The input BufferSize cannot hold the converted value. Cc: Jiewen Yao Cc: Liming Gao Cc: Michael Kinney Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Hao Wu Reviewed-by: Liming Gao --- MdePkg/Include/Library/PrintLib.h | 121 ++++++++++++ MdePkg/Library/BasePrintLib/PrintLib.c | 128 ++++++++++++ MdePkg/Library/BasePrintLib/PrintLibInternal.c | 204 ++++++++++++++++++++ MdePkg/Library/BasePrintLib/PrintLibInternal.h | 61 ++++++ 4 files changed, 514 insertions(+) diff --git a/MdePkg/Include/Library/PrintLib.h b/MdePkg/Include/Library/Pri= ntLib.h index 5f66323..8c11dab 100644 --- a/MdePkg/Include/Library/PrintLib.h +++ b/MdePkg/Include/Library/PrintLib.h @@ -542,6 +542,67 @@ UnicodeValueToString ( ); =20 /** + Converts a decimal value to a Null-terminated Unicode string. + + Converts the decimal number specified by Value to a Null-terminated Unic= ode + string specified by Buffer containing at most Width characters. No paddi= ng 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 Buf= fer. + 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 charact= er 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 se= t in + Flags. + If Width >=3D MAXIMUM_VALUE_CHARACTERS. + +**/ +RETURN_STATUS +EFIAPI +UnicodeValueToStringS ( + IN OUT CHAR16 *Buffer, + IN UINTN BufferSize, + IN UINTN Flags, + IN INT64 Value, + IN UINTN Width + ); + +/** Produces a Null-terminated ASCII string in an output buffer based on a N= ull-terminated ASCII format string and a VA_LIST argument list. =20 @@ -871,6 +932,66 @@ AsciiValueToString ( ); =20 /** + 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 paddi= ng 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 Buf= fer. + 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 charact= er 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 se= t in + Flags. + If Width >=3D MAXIMUM_VALUE_CHARACTERS. + +**/ +RETURN_STATUS +EFIAPI +AsciiValueToStringS ( + IN OUT CHAR8 *Buffer, + IN UINTN BufferSize, + IN UINTN Flags, + IN INT64 Value, + IN UINTN Width + ); + +/** Returns the number of characters that would be produced by if the format= ted=20 output were produced not including the Null-terminator. =20 diff --git a/MdePkg/Library/BasePrintLib/PrintLib.c b/MdePkg/Library/BasePr= intLib/PrintLib.c index bf8c7bf..221b52e 100644 --- a/MdePkg/Library/BasePrintLib/PrintLib.c +++ b/MdePkg/Library/BasePrintLib/PrintLib.c @@ -404,6 +404,71 @@ UnicodeValueToString ( } =20 /** + Converts a decimal value to a Null-terminated Unicode string. + + Converts the decimal number specified by Value to a Null-terminated Unic= ode + string specified by Buffer containing at most Width characters. No paddi= ng 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 Buf= fer. + 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 charact= er 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 se= t in + Flags. + If Width >=3D MAXIMUM_VALUE_CHARACTERS. + +**/ +RETURN_STATUS +EFIAPI +UnicodeValueToStringS ( + IN OUT CHAR16 *Buffer, + IN UINTN BufferSize, + IN UINTN Flags, + IN INT64 Value, + IN UINTN Width + ) +{ + ASSERT_UNICODE_BUFFER(Buffer); + return BasePrintLibConvertValueToStringS ((CHAR8 *)Buffer, BufferSize, F= lags, Value, Width, 2); +} + +/** Produces a Null-terminated ASCII string in an output buffer based on a N= ull-terminated ASCII format string and a VA_LIST argument list. =20 @@ -769,6 +834,69 @@ AsciiValueToString ( } =20 /** + 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 paddi= ng 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 Buf= fer. + 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 charact= er 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 se= t in + Flags. + If Width >=3D MAXIMUM_VALUE_CHARACTERS. + +**/ +RETURN_STATUS +EFIAPI +AsciiValueToStringS ( + IN OUT CHAR8 *Buffer, + IN UINTN BufferSize, + IN UINTN Flags, + IN INT64 Value, + IN UINTN Width + ) +{ + return BasePrintLibConvertValueToStringS (Buffer, BufferSize, Flags, Val= ue, Width, 1); +} + +/** Returns the number of characters that would be produced by if the format= ted=20 output were produced not including the Null-terminator. =20 diff --git a/MdePkg/Library/BasePrintLib/PrintLibInternal.c b/MdePkg/Librar= y/BasePrintLib/PrintLibInternal.c index 155fe6a..9b15a07 100644 --- a/MdePkg/Library/BasePrintLib/PrintLibInternal.c +++ b/MdePkg/Library/BasePrintLib/PrintLibInternal.c @@ -292,6 +292,210 @@ BasePrintLibConvertValueToString ( } =20 /** + Internal function that converts a decimal value to a Null-terminated str= ing. + + Converts the decimal number specified by Value to a Null-terminated stri= ng + specified by Buffer containing at most Width characters. If Width is 0 t= hen 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 Value is < 0, 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 opti= onal + '-' sign character, '0' characters, digit characters for Value, and the + Null-terminator add up to Width characters. + + If an error would be returned, the function will ASSERT(). + + @param Buffer The pointer to the output buffer for the produced + Null-terminated 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 characters to place in Buffer, + not including the Null-terminator. + @param Increment The character increment in Buffer. + + @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 Increment is 1 and + PcdMaximumAsciiStringLength is not zero, + BufferSize is greater than + PcdMaximumAsciiStringLength. + If Increment is not 1 and + PcdMaximumUnicodeStringLength is not ze= ro, + BufferSize is greater than + (PcdMaximumUnicodeStringLength * + sizeof (CHAR16) + 1). + If unsupported bits are set in Flags. + If both COMMA_TYPE and RADIX_HEX are se= t in + Flags. + If Width >=3D MAXIMUM_VALUE_CHARACTERS. + +**/ +RETURN_STATUS +BasePrintLibConvertValueToStringS ( + IN OUT CHAR8 *Buffer, + IN UINTN BufferSize, + IN UINTN Flags, + IN INT64 Value, + IN UINTN Width, + IN UINTN Increment + ) +{ + CHAR8 *EndBuffer; + CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS]; + CHAR8 *ValueBufferPtr; + UINTN Count; + UINTN Digits; + UINTN Index; + UINTN Radix; + + // + // 1. Buffer shall not be a null pointer. + // + SAFE_PRINT_CONSTRAINT_CHECK ((Buffer !=3D NULL), RETURN_INVALID_PARAMETE= R); + + // + // 2. BufferSize shall not be greater than (RSIZE_MAX * sizeof (CHAR16))= for + // Unicode output string or shall not be greater than ASCII_RSIZE_MAX= for + // Ascii output string. + // + if (Increment =3D=3D 1) { + // + // Ascii output string + // + if (ASCII_RSIZE_MAX !=3D 0) { + SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <=3D ASCII_RSIZE_MAX), RETU= RN_INVALID_PARAMETER); + } + } else { + // + // Unicode output string + // + if (RSIZE_MAX !=3D 0) { + SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <=3D RSIZE_MAX * sizeof (CH= AR16) + 1), RETURN_INVALID_PARAMETER); + } + } + + // + // 3. Flags shall be set properly. + // + SAFE_PRINT_CONSTRAINT_CHECK (((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PRE= FIX_ZERO | RADIX_HEX)) =3D=3D 0), RETURN_INVALID_PARAMETER); + SAFE_PRINT_CONSTRAINT_CHECK ((((Flags & COMMA_TYPE) =3D=3D 0) || ((Flags= & RADIX_HEX) =3D=3D 0)), RETURN_INVALID_PARAMETER); + + // + // 4. Width shall be smaller than MAXIMUM_VALUE_CHARACTERS. + // + SAFE_PRINT_CONSTRAINT_CHECK ((Width < MAXIMUM_VALUE_CHARACTERS), RETURN_= INVALID_PARAMETER); + + // + // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored. + // + if (Width =3D=3D 0 || (Flags & COMMA_TYPE) !=3D 0) { + Flags &=3D ~((UINTN) PREFIX_ZERO); + } + // + // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed. + // + if (Width =3D=3D 0) { + Width =3D MAXIMUM_VALUE_CHARACTERS - 1; + } + + // + // Count the characters of the output string. + // + Count =3D 0; + Radix =3D ((Flags & RADIX_HEX) =3D=3D 0)? 10 : 16; + + if ((Flags & PREFIX_ZERO) !=3D 0) { + Count =3D Width; + } else { + if ((Value < 0) && ((Flags & RADIX_HEX) =3D=3D 0)) { + Count++; // minus sign + ValueBufferPtr =3D BasePrintLibValueToString (ValueBuffer, -Value, R= adix); + } else { + ValueBufferPtr =3D BasePrintLibValueToString (ValueBuffer, Value, Ra= dix); + } + Digits =3D ValueBufferPtr - ValueBuffer; + Count +=3D Digits; + + if ((Flags & COMMA_TYPE) !=3D 0) { + Count +=3D (Digits - 1) / 3; // commas + } + } + + Width =3D MIN (Count, Width); + + // + // 5. BufferSize shall be large enough to hold the converted string. + // + SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize >=3D (Width + 1) * Increment), = RETURN_BUFFER_TOO_SMALL); + + // + // Set the tag for the end of the input Buffer. + // + EndBuffer =3D Buffer + Width * Increment; + + // + // Convert decimal negative + // + if ((Value < 0) && ((Flags & RADIX_HEX) =3D=3D 0)) { + Value =3D -Value; + Buffer =3D BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Incremen= t); + Width--; + } + + // + // Count the length of the value string. + // + ValueBufferPtr =3D BasePrintLibValueToString (ValueBuffer, Value, Radix); + Count =3D ValueBufferPtr - ValueBuffer; + + // + // Append Zero + // + if ((Flags & PREFIX_ZERO) !=3D 0) { + Buffer =3D BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '= 0', Increment); + } + + // + // Print Comma type for every 3 characters + // + Digits =3D Count % 3; + if (Digits !=3D 0) { + Digits =3D 3 - Digits; + } + for (Index =3D 0; Index < Count; Index++) { + Buffer =3D BasePrintLibFillBuffer (Buffer, EndBuffer, 1, *ValueBufferP= tr--, Increment); + if ((Flags & COMMA_TYPE) !=3D 0) { + Digits++; + if (Digits =3D=3D 3) { + Digits =3D 0; + if ((Index + 1) < Count) { + Buffer =3D BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', In= crement); + } + } + } + } + + // + // Print Null-terminator + // + BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment); + + return RETURN_SUCCESS; +} + +/** Worker function that produces a Null-terminated string in an output buff= er=20 based on a Null-terminated format string and a VA_LIST argument list. =20 diff --git a/MdePkg/Library/BasePrintLib/PrintLibInternal.h b/MdePkg/Librar= y/BasePrintLib/PrintLibInternal.h index fccef9b..052e699 100644 --- a/MdePkg/Library/BasePrintLib/PrintLibInternal.h +++ b/MdePkg/Library/BasePrintLib/PrintLibInternal.h @@ -213,4 +213,65 @@ BasePrintLibConvertValueToString ( IN UINTN Increment ); =20 +/** + Internal function that converts a decimal value to a Null-terminated str= ing. + + Converts the decimal number specified by Value to a Null-terminated stri= ng + specified by Buffer containing at most Width characters. If Width is 0 t= hen 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 Value is < 0, 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 opti= onal + '-' sign character, '0' characters, digit characters for Value, and the + Null-terminator add up to Width characters. + + If an error would be returned, the function will ASSERT(). + + @param Buffer The pointer to the output buffer for the produced + Null-terminated 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 characters to place in Buffer, + not including the Null-terminator. + @param Increment The character increment in Buffer. + + @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 Increment is 1 and + PcdMaximumAsciiStringLength is not zero, + BufferSize is greater than + PcdMaximumAsciiStringLength. + If Increment is not 1 and + PcdMaximumUnicodeStringLength is not ze= ro, + BufferSize is greater than + (PcdMaximumUnicodeStringLength * + sizeof (CHAR16) + 1). + If unsupported bits are set in Flags. + If both COMMA_TYPE and RADIX_HEX are se= t in + Flags. + If Width >=3D MAXIMUM_VALUE_CHARACTERS. + +**/ +RETURN_STATUS +BasePrintLibConvertValueToStringS ( + IN OUT CHAR8 *Buffer, + IN UINTN BufferSize, + IN UINTN Flags, + IN INT64 Value, + IN UINTN Width, + IN UINTN Increment + ); + #endif --=20 1.9.5.msysgit.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Sat Nov 2 14:33:21 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1486620181080438.3600257080624; Wed, 8 Feb 2017 22:03:01 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 225E2820B7; Wed, 8 Feb 2017 22:02:58 -0800 (PST) Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 5B7ED8208E for ; Wed, 8 Feb 2017 22:02:57 -0800 (PST) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga104.jf.intel.com with ESMTP; 08 Feb 2017 22:02:57 -0800 Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.13]) by orsmga001.jf.intel.com with ESMTP; 08 Feb 2017 22:02:56 -0800 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,349,1484035200"; d="scan'208";a="1092615583" From: Hao Wu To: edk2-devel@lists.01.org Date: Thu, 9 Feb 2017 14:02:48 +0800 Message-Id: <1486620170-38136-3-git-send-email-hao.a.wu@intel.com> X-Mailer: git-send-email 1.9.5.msysgit.0 In-Reply-To: <1486620170-38136-1-git-send-email-hao.a.wu@intel.com> References: <1486620170-38136-1-git-send-email-hao.a.wu@intel.com> Subject: [edk2] [PATCH v2 2/4] MdeModulePkg: Add the EFI_PRINT2S_PROTOCOL X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Hao Wu , Michael Kinney , Jiewen Yao , Liming Gao MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add the EFI_PRINT2S_PROTOCOL as a safe version of the EFI_PRINT2_PROTOCOL, the EFI_PRINT2S_PROTOCOL replaces the following 2 services in EFI_PRINT2_PROTOCOL: UNICODE_VALUE_TO_STRING ASCII_VALUE_TO_STRING with: UNICODE_VALUE_TO_STRING_S ASCII_VALUE_TO_STRING_S The 2 new services perform checks to the input parameters and will return relative status to reflect the check result. Return RETURN_INVALID_PARAMETER when: 1). The input Buffer is NULL. 2). The input BufferSize is greater than (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1) for UnicodeValueToStringS or greater than PcdMaximumAsciiStringLength for AsciiValueToStringS. 3). The input Flags is not set properly. 4). The input Width is not smaller than MAXIMUM_VALUE_CHARACTERS. Return RETURN_BUFFER_TOO_SMALL when: 1). The input BufferSize cannot hold the converted value. Cc: Jiewen Yao Cc: Liming Gao Cc: Michael Kinney Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Hao Wu Reviewed-by: Liming Gao --- MdeModulePkg/Include/Protocol/Print2.h | 150 +++++++++++++++++++- MdeModulePkg/MdeModulePkg.dec | 3 +- MdeModulePkg/Universal/PrintDxe/Print.c | 18 ++- MdeModulePkg/Universal/PrintDxe/PrintDxe.inf | 7 +- MdeModulePkg/Universal/PrintDxe/PrintDxe.uni | 10 +- 5 files changed, 175 insertions(+), 13 deletions(-) diff --git a/MdeModulePkg/Include/Protocol/Print2.h b/MdeModulePkg/Include/= Protocol/Print2.h index 8cad6fd..e4dd6f2 100644 --- a/MdeModulePkg/Include/Protocol/Print2.h +++ b/MdeModulePkg/Include/Protocol/Print2.h @@ -1,7 +1,8 @@ /** @file =20 - This print protocol defines six basic print functions to=20 - print the format unicode and ascii string. + Produces EFI_PRINT2_PROTOCOL and EFI_PRINT2S_PROTOCOL. + These protocols define basic print functions to print the format unicod= e and + ascii string. =20 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made availabl= e under=20 @@ -515,4 +516,149 @@ struct _EFI_PRINT2_PROTOCOL { =20 extern EFI_GUID gEfiPrint2ProtocolGuid; =20 + +#define EFI_PRINT2S_PROTOCOL_GUID \ + { 0xcc252d2, 0xc106, 0x4661, { 0xb5, 0xbd, 0x31, 0x47, 0xa4, 0xf8, 0x1f,= 0x92 } } + +// +// Forward reference for pure ANSI compatability +// +typedef struct _EFI_PRINT2S_PROTOCOL EFI_PRINT2S_PROTOCOL; + +/** + Converts a decimal value to a Null-terminated Unicode string. + + Converts the decimal number specified by Value to a Null-terminated Unic= ode + string specified by Buffer containing at most Width characters. No paddi= ng 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 Buf= fer. + 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 charact= er 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 se= t in + Flags. + If Width >=3D MAXIMUM_VALUE_CHARACTERS. + +**/ +typedef +RETURN_STATUS +(EFIAPI *UNICODE_VALUE_TO_STRING_S)( + IN OUT CHAR16 *Buffer, + IN UINTN BufferSize, + IN UINTN Flags, + IN INT64 Value, + IN UINTN Width + ); + +/** + 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 paddi= ng 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 Buf= fer. + 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 charact= er 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 se= t in + Flags. + If Width >=3D MAXIMUM_VALUE_CHARACTERS. + +**/ +typedef +RETURN_STATUS +(EFIAPI *ASCII_VALUE_TO_STRING_S)( + IN OUT CHAR8 *Buffer, + IN UINTN BufferSize, + IN UINTN Flags, + IN INT64 Value, + IN UINTN Width + ); + +struct _EFI_PRINT2S_PROTOCOL { + UNICODE_BS_PRINT UnicodeBSPrint; + UNICODE_S_PRINT UnicodeSPrint; + UNICODE_BS_PRINT_ASCII_FORMAT UnicodeBSPrintAsciiFormat; + UNICODE_S_PRINT_ASCII_FORMAT UnicodeSPrintAsciiFormat; + UNICODE_VALUE_TO_STRING_S UnicodeValueToStringS; + ASCII_BS_PRINT AsciiBSPrint; + ASCII_S_PRINT AsciiSPrint; + ASCII_BS_PRINT_UNICODE_FORMAT AsciiBSPrintUnicodeFormat; + ASCII_S_PRINT_UNICODE_FORMAT AsciiSPrintUnicodeFormat; + ASCII_VALUE_TO_STRING_S AsciiValueToStringS; +}; + +extern EFI_GUID gEfiPrint2SProtocolGuid; + #endif diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index 273cd7e..c95633c 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -427,9 +427,10 @@ # If developer need implement such functionality, they should use BaseP= eCoffLib. gEfiLoadPeImageProtocolGuid =3D { 0x5CB5C776, 0x60D5, 0x45EE, { 0x88,= 0x3C, 0x45, 0x27, 0x08, 0xCD, 0x74, 0x3F }} =20 - ## Print protocol defines basic print functions to print the format unic= ode and ascii string. + ## Print protocols define basic print functions to print the format unic= ode and ascii string. # Include/Protocol/Print2.h gEfiPrint2ProtocolGuid =3D { 0xf05976ef, 0x83f1, 0x4f3d, { 0x86= , 0x19, 0xf7, 0x59, 0x5d, 0x41, 0xe5, 0x38 } } + gEfiPrint2SProtocolGuid =3D { 0xcc252d2, 0xc106, 0x4661, { 0xb5,= 0xbd, 0x31, 0x47, 0xa4, 0xf8, 0x1f, 0x92 } } =20 ## This protocol defines the generic memory test interfaces in Dxe phase. # Include/Protocol/GenericMemoryTest.h diff --git a/MdeModulePkg/Universal/PrintDxe/Print.c b/MdeModulePkg/Univers= al/PrintDxe/Print.c index af55acf..85bc724 100644 --- a/MdeModulePkg/Universal/PrintDxe/Print.c +++ b/MdeModulePkg/Universal/PrintDxe/Print.c @@ -1,7 +1,7 @@ /** @file - This driver produces Print2 protocol layered on top of the PrintLib from= the MdePkg. + This driver produces Print2 protocols layered on top of the PrintLib fro= m the MdePkg. =20 -Copyright (c) 2009, 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 @@ -35,6 +35,19 @@ CONST EFI_PRINT2_PROTOCOL mPrint2Protocol =3D { AsciiValueToString }; =20 +CONST EFI_PRINT2S_PROTOCOL mPrint2SProtocol =3D { + UnicodeBSPrint, + UnicodeSPrint, + UnicodeBSPrintAsciiFormat, + UnicodeSPrintAsciiFormat, + UnicodeValueToStringS, + AsciiBSPrint, + AsciiSPrint, + AsciiBSPrintUnicodeFormat, + AsciiSPrintUnicodeFormat, + AsciiValueToStringS +}; + /** The user Entry Point for Print module. =20 @@ -59,6 +72,7 @@ PrintEntryPoint ( Status =3D gBS->InstallMultipleProtocolInterfaces ( &mPrintThunkHandle, &gEfiPrint2ProtocolGuid, &mPrint2Protocol, + &gEfiPrint2SProtocolGuid, &mPrint2SProtocol, NULL ); ASSERT_EFI_ERROR (Status); diff --git a/MdeModulePkg/Universal/PrintDxe/PrintDxe.inf b/MdeModulePkg/Un= iversal/PrintDxe/PrintDxe.inf index 9ea0652..19eef5a 100644 --- a/MdeModulePkg/Universal/PrintDxe/PrintDxe.inf +++ b/MdeModulePkg/Universal/PrintDxe/PrintDxe.inf @@ -1,9 +1,9 @@ ## @file -# Print DXE driver that produces Print2 Protocol. +# Print DXE driver that produces Print2 Protocols. # -# This driver produces Print2 protocol layered on top of the PrintLib fro= m the MdePkg. +# This driver produces Print2 protocols layered on top of the PrintLib fr= om the MdePkg. # -# Copyright (c) 2009 - 2014, 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 B= SD License # which accompanies this distribution. The full text of the license may = be found at @@ -44,6 +44,7 @@ =20 [Protocols] gEfiPrint2ProtocolGuid ## PRODUCES + gEfiPrint2SProtocolGuid ## PRODUCES =20 [Depex] TRUE diff --git a/MdeModulePkg/Universal/PrintDxe/PrintDxe.uni b/MdeModulePkg/Un= iversal/PrintDxe/PrintDxe.uni index accb114..10f0ff8 100644 --- a/MdeModulePkg/Universal/PrintDxe/PrintDxe.uni +++ b/MdeModulePkg/Universal/PrintDxe/PrintDxe.uni @@ -1,9 +1,9 @@ // /** @file -// Print DXE driver that produces Print2 Protocol. +// Print DXE driver that produces Print2 Protocols. // -// This driver produces Print2 protocol layered on top of the PrintLib fro= m the MdePkg. +// This driver produces Print2 protocols layered on top of the PrintLib fr= om the MdePkg. // -// Copyright (c) 2009 - 2014, 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 B= SD License @@ -16,7 +16,7 @@ // **/ =20 =20 -#string STR_MODULE_ABSTRACT #language en-US "Print DXE driver = that produces Print2 Protocol" +#string STR_MODULE_ABSTRACT #language en-US "Print DXE driver = that produces Print2 Protocols" =20 -#string STR_MODULE_DESCRIPTION #language en-US "This driver produ= ces Print2 protocol layered on top of the PrintLib from the MdePkg." +#string STR_MODULE_DESCRIPTION #language en-US "This driver produ= ces Print2 protocols layered on top of the PrintLib from the MdePkg." =20 --=20 1.9.5.msysgit.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Sat Nov 2 14:33:21 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1486620183143342.7848491808073; Wed, 8 Feb 2017 22:03:03 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 4F636820B0; Wed, 8 Feb 2017 22:03:00 -0800 (PST) Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id C5A5981FB0 for ; Wed, 8 Feb 2017 22:02:58 -0800 (PST) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga104.jf.intel.com with ESMTP; 08 Feb 2017 22:02:58 -0800 Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.13]) by orsmga001.jf.intel.com with ESMTP; 08 Feb 2017 22:02:57 -0800 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,349,1484035200"; d="scan'208";a="1092615589" From: Hao Wu To: edk2-devel@lists.01.org Date: Thu, 9 Feb 2017 14:02:49 +0800 Message-Id: <1486620170-38136-4-git-send-email-hao.a.wu@intel.com> X-Mailer: git-send-email 1.9.5.msysgit.0 In-Reply-To: <1486620170-38136-1-git-send-email-hao.a.wu@intel.com> References: <1486620170-38136-1-git-send-email-hao.a.wu@intel.com> Subject: [edk2] [PATCH v2 3/4] MdeModulePkg/PrintLib: Use EFI_PRINT2S_PROTOCOL for this instance X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Hao Wu , Michael Kinney , Jiewen Yao , Liming Gao MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The commit updates the PrintLib instance MdeModulePkg/Library/DxePrintLibPrint2Protocol to use EFI_PRINT2S_PROTOCOL to implement the APIs. Cc: Jiewen Yao Cc: Liming Gao Cc: Michael Kinney Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Hao Wu Reviewed-by: Liming Gao --- MdeModulePkg/Library/DxePrintLibPrint2Protocol/DxePrintLibPrint2Protocol.i= nf | 4 +- MdeModulePkg/Library/DxePrintLibPrint2Protocol/DxePrintLibPrint2Protocol.u= ni | 10 ++-- MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c = | 56 +++++++++++++++----- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/DxePrintLibPrin= t2Protocol.inf b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/DxePrintLib= Print2Protocol.inf index 55ee940..1cda2dc 100644 --- a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/DxePrintLibPrint2Proto= col.inf +++ b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/DxePrintLibPrint2Proto= col.inf @@ -36,11 +36,11 @@ PcdLib =20 [Protocols] - gEfiPrint2ProtocolGuid ## CONSUMES + gEfiPrint2SProtocolGuid ## CONSUMES =20 [Pcd] gEfiMdePkgTokenSpaceGuid.PcdMaximumAsciiStringLength ## SOMETIMES_CO= NSUMES gEfiMdePkgTokenSpaceGuid.PcdMaximumUnicodeStringLength ## SOMETIMES_CO= NSUMES =20 [Depex.common.DXE_DRIVER, Depex.common.DXE_RUNTIME_DRIVER, Depex.common.DX= E_SAL_DRIVER, Depex.common.DXE_SMM_DRIVER] - gEfiPrint2ProtocolGuid + gEfiPrint2SProtocolGuid diff --git a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/DxePrintLibPrin= t2Protocol.uni b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/DxePrintLib= Print2Protocol.uni index 29aa37c..3a9fb58 100644 --- a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/DxePrintLibPrint2Proto= col.uni +++ b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/DxePrintLibPrint2Proto= col.uni @@ -1,9 +1,9 @@ // /** @file -// Library instance that implements Print Library class based on protocol = gEfiPrint2ProtocolGuid. +// Library instance that implements Print Library class based on protocol = gEfiPrint2SProtocolGuid. // -// Library instance that implements Print Library class based on protocol = gEfiPrint2ProtocolGuid. +// Library instance that implements Print Library class based on protocol = gEfiPrint2SProtocolGuid. // -// Copyright (c) 2009 - 2014, 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 B= SD License @@ -15,7 +15,7 @@ // **/ =20 =20 -#string STR_MODULE_ABSTRACT #language en-US "Implements Print = Library class based on protocol gEfiPrint2ProtocolGuid" +#string STR_MODULE_ABSTRACT #language en-US "Implements Print = Library class based on protocol gEfiPrint2SProtocolGuid" =20 -#string STR_MODULE_DESCRIPTION #language en-US "Library instance = that implements Print Library class based on protocol gEfiPrint2ProtocolGui= d." +#string STR_MODULE_DESCRIPTION #language en-US "Library instance = that implements Print Library class based on protocol gEfiPrint2SProtocolGu= id." =20 diff --git a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c b/Md= eModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c index 438ac9e..b3e7cb2 100644 --- a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c +++ b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c @@ -1,8 +1,8 @@ /** @file - Instance of Print Library based on gEfiPrint2ProtocolGuid. + Instance of Print Library based on gEfiPrint2SProtocolGuid. =20 Implement the print library instance by wrap the interface=20 - provided in the Print2 protocol. This protocol is defined as the internal + provided in the Print2S protocol. This protocol is defined as the intern= al protocol related to this implementation, not in the public spec. So, thi= s=20 library instance is only for this code base. =20 @@ -43,12 +43,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITH= ER EXPRESS OR IMPLIED. } \ } while (FALSE) =20 -EFI_PRINT2_PROTOCOL *mPrint2Protocol =3D NULL; +EFI_PRINT2S_PROTOCOL *mPrint2SProtocol =3D NULL; =20 /** - The constructor function caches the pointer to Print2 protocol. + The constructor function caches the pointer to Print2S protocol. =20 - 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_S= UCCESS.=20 =20 @param ImageHandle The firmware allocated handle for the EFI image. @@ -67,12 +67,12 @@ PrintLibConstructor ( EFI_STATUS Status; =20 Status =3D SystemTable->BootServices->LocateProtocol ( - &gEfiPrint2ProtocolGuid, + &gEfiPrint2SProtocolGuid, NULL, - (VOID**) &mPrint2Protocol + (VOID**) &mPrint2SProtocol ); ASSERT_EFI_ERROR (Status); - ASSERT (mPrint2Protocol !=3D NULL); + ASSERT (mPrint2SProtocol !=3D NULL); =20 return Status; } @@ -362,7 +362,7 @@ UnicodeBSPrint ( { ASSERT_UNICODE_BUFFER (StartOfBuffer); ASSERT_UNICODE_BUFFER (FormatString); - return mPrint2Protocol->UnicodeBSPrint (StartOfBuffer, BufferSize, Forma= tString, Marker); + return mPrint2SProtocol->UnicodeBSPrint (StartOfBuffer, BufferSize, Form= atString, Marker); } =20 /** @@ -537,7 +537,7 @@ UnicodeBSPrintAsciiFormat ( ) { ASSERT_UNICODE_BUFFER (StartOfBuffer); - return mPrint2Protocol->UnicodeBSPrintAsciiFormat (StartOfBuffer, Buffer= Size, FormatString, Marker); + return mPrint2SProtocol->UnicodeBSPrintAsciiFormat (StartOfBuffer, Buffe= rSize, FormatString, Marker); } =20 /** @@ -648,7 +648,21 @@ UnicodeValueToString ( IN UINTN Width ) { - return mPrint2Protocol->UnicodeValueToString (Buffer, Flags, Value, Widt= h); + RETURN_STATUS Status; + UINTN BufferSize; + + if (Width =3D=3D 0) { + BufferSize =3D (MAXIMUM_VALUE_CHARACTERS + 1) * sizeof (CHAR16); + } else { + BufferSize =3D (Width + 1) * sizeof (CHAR16); + } + + Status =3D mPrint2SProtocol->UnicodeValueToStringS (Buffer, BufferSize, = Flags, Value, Width); + if (RETURN_ERROR (Status)) { + return 0; + } + + return StrnLenS (Buffer, BufferSize / sizeof (CHAR16)); } =20 /** @@ -758,7 +772,7 @@ AsciiBSPrint ( IN BASE_LIST Marker ) { - return mPrint2Protocol->AsciiBSPrint (StartOfBuffer, BufferSize, FormatS= tring, Marker); + return mPrint2SProtocol->AsciiBSPrint (StartOfBuffer, BufferSize, Format= String, Marker); } =20 /** @@ -931,7 +945,7 @@ AsciiBSPrintUnicodeFormat ( ) { ASSERT_UNICODE_BUFFER (FormatString); - return mPrint2Protocol->AsciiBSPrintUnicodeFormat (StartOfBuffer, Buffer= Size, FormatString, Marker); + return mPrint2SProtocol->AsciiBSPrintUnicodeFormat (StartOfBuffer, Buffe= rSize, FormatString, Marker); } =20 /** @@ -1042,7 +1056,21 @@ AsciiValueToString ( IN UINTN Width ) { - return mPrint2Protocol->AsciiValueToString (Buffer, Flags, Value, Width); + RETURN_STATUS Status; + UINTN BufferSize; + + if (Width =3D=3D 0) { + BufferSize =3D (MAXIMUM_VALUE_CHARACTERS + 1) * sizeof (CHAR8); + } else { + BufferSize =3D (Width + 1) * sizeof (CHAR8); + } + + Status =3D mPrint2SProtocol->AsciiValueToStringS (Buffer, BufferSize, Fl= ags, Value, Width); + if (RETURN_ERROR (Status)) { + return 0; + } + + return AsciiStrnLenS (Buffer, BufferSize / sizeof (CHAR8)); } =20 #define PREFIX_SIGN BIT1 --=20 1.9.5.msysgit.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Sat Nov 2 14:33:21 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1486620185427995.2554855769941; Wed, 8 Feb 2017 22:03:05 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 7D87B820D4; Wed, 8 Feb 2017 22:03:01 -0800 (PST) Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 35B3C81FB0 for ; Wed, 8 Feb 2017 22:03:00 -0800 (PST) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga104.jf.intel.com with ESMTP; 08 Feb 2017 22:03:00 -0800 Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.13]) by orsmga001.jf.intel.com with ESMTP; 08 Feb 2017 22:02:58 -0800 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,349,1484035200"; d="scan'208";a="1092615599" From: Hao Wu To: edk2-devel@lists.01.org Date: Thu, 9 Feb 2017 14:02:50 +0800 Message-Id: <1486620170-38136-5-git-send-email-hao.a.wu@intel.com> X-Mailer: git-send-email 1.9.5.msysgit.0 In-Reply-To: <1486620170-38136-1-git-send-email-hao.a.wu@intel.com> References: <1486620170-38136-1-git-send-email-hao.a.wu@intel.com> Subject: [edk2] [PATCH v2 4/4] MdeModulePkg/PrintLib: Add safe print functions [A|U]ValueToStringS X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Hao Wu , Michael Kinney , Jiewen Yao , Liming Gao MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add the following 2 APIs: UnicodeValueToStringS AsciiValueToStringS These safe version APIs are used to enhance their counterpart (APIs without trailing 'S' in function names). They perform checks to the input parameters and will return relative status to reflect the check result. Return RETURN_INVALID_PARAMETER when: 1). The input Buffer is NULL. 2). The input BufferSize is greater than (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1) for UnicodeValueToStringS or greater than PcdMaximumAsciiStringLength for AsciiValueToStringS. 3). The input Flags is not set properly. 4). The input Width is not smaller than MAXIMUM_VALUE_CHARACTERS. Return RETURN_BUFFER_TOO_SMALL when: 1). The input BufferSize cannot hold the converted value. These APIs in the MdeModulePkg/DxePrintLibPrint2Protocol instance follow the same rules with MdePkg/BasePrintLib. Cc: Jiewen Yao Cc: Liming Gao Cc: Michael Kinney Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Hao Wu Reviewed-by: Liming Gao --- MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c | 127 ++++++++++= ++++++++++ 1 file changed, 127 insertions(+) diff --git a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c b/Md= eModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c index b3e7cb2..589d4db 100644 --- a/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c +++ b/MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c @@ -666,6 +666,70 @@ UnicodeValueToString ( } =20 /** + Converts a decimal value to a Null-terminated Unicode string. + + Converts the decimal number specified by Value to a Null-terminated Unic= ode + string specified by Buffer containing at most Width characters. No paddi= ng 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 Buf= fer. + 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 charact= er 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 se= t in + Flags. + If Width >=3D 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, Flag= s, Value, Width); +} + +/** Produces a Null-terminated ASCII string in an output buffer based on a N= ull-terminated ASCII format string and a VA_LIST argument list. =20 @@ -1073,6 +1137,69 @@ AsciiValueToString ( return AsciiStrnLenS (Buffer, BufferSize / sizeof (CHAR8)); } =20 +/** + 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 paddi= ng 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 Buf= fer. + 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 charact= er 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 se= t in + Flags. + If Width >=3D 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 #define PREFIX_BLANK BIT2 #define LONG_TYPE BIT4 --=20 1.9.5.msysgit.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel