X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdePkg%2FLibrary%2FBasePrintLib%2FPrintLibInternal.c;h=72c05e4dc5dbd88c515e51826a4a43079884ced4;hp=89d18b09a2c5ffeadcbf622403ea06f8979ccfd9;hb=e1f414b6a7d8a0424e0e01f655b09a4612b4d0e8;hpb=3f9f540dacca2249904d204836496334826de58f diff --git a/MdePkg/Library/BasePrintLib/PrintLibInternal.c b/MdePkg/Library/BasePrintLib/PrintLibInternal.c index 89d18b09a2..72c05e4dc5 100644 --- a/MdePkg/Library/BasePrintLib/PrintLibInternal.c +++ b/MdePkg/Library/BasePrintLib/PrintLibInternal.c @@ -1,7 +1,7 @@ /** @file Print Library worker functions. - Copyright (c) 2006, Intel Corporation
+ Copyright (c) 2006 - 2007, 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 @@ -14,9 +14,14 @@ **/ +// +// Include common header file for this module. +// +#include "CommonHeader.h" + #include "PrintLibInternal.h" -static CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; +GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; /** @@ -25,6 +30,8 @@ static CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B', Internal function that places ASCII or Unicode character into the Buffer. @param Buffer 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 Count of character to be placed into Buffer. @param Character Character to be placed into Buffer. @param Increment Character increment in Buffer. @@ -35,6 +42,7 @@ static CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B', CHAR8 * BasePrintLibFillBuffer ( CHAR8 *Buffer, + CHAR8 *EndBuffer, INTN Length, UINTN Character, INTN Increment @@ -42,7 +50,7 @@ BasePrintLibFillBuffer ( { INTN Index; - for (Index = 0; Index < Length; Index++) { + for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) { *Buffer = (CHAR8) Character; *(Buffer + 1) = (CHAR8) (Character >> 8); Buffer += Increment; @@ -92,7 +100,7 @@ BasePrintLibValueToString ( Converts the decimal number specified by Value to a Null-terminated string specified by Buffer containing at most Width characters. If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed. - The total number of characters placed in Buffer is returned. + The number of characters in Buffer is returned not including the Null-terminator. If the conversion contains more than Width characters, then only the first Width characters are returned, and the total number of characters required to perform the conversion is returned. @@ -102,11 +110,13 @@ BasePrintLibValueToString ( 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 HEX_RADIX is set in Flags, then the output buffer will be formatted in hexadecimal format. + If Value is < 0 and HEX_RADIX 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 both COMMA_TYPE and HEX_RADIX are set in Flags, then ASSERT(). If Buffer is NULL, then ASSERT(). If unsupported bits are set in Flags, then ASSERT(). @@ -117,10 +127,11 @@ BasePrintLibValueToString ( @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. + @param Width The maximum number of characters to place in Buffer, not including + the Null-terminator. @param Increment Character increment in Buffer. - @return Total number of characters required to perform the conversion. + @return The number of characters in Buffer not including the Null-terminator. **/ UINTN @@ -133,10 +144,24 @@ BasePrintLibConvertValueToString ( ) { CHAR8 *OriginalBuffer; + CHAR8 *EndBuffer; CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS]; UINTN Count; UINTN Digits; UINTN Index; + UINTN Radix; + + ASSERT (Buffer != NULL); + ASSERT (Width < MAXIMUM_VALUE_CHARACTERS); + // + // Make sure Flags can only contain supported bits. + // + ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0); + + // + // If both COMMA_TYPE and HEX_RADIX are set, then ASSERT () + // + ASSERT (((Flags & COMMA_TYPE) != 0 && (Flags & RADIX_HEX) != 0) == FALSE); OriginalBuffer = Buffer; @@ -144,36 +169,46 @@ BasePrintLibConvertValueToString ( Flags &= (~PREFIX_ZERO); } - if (Width == 0 || Width > (MAXIMUM_VALUE_CHARACTERS - 1)) { + if (Width == 0) { Width = MAXIMUM_VALUE_CHARACTERS - 1; } + // + // Set the tag for the end of the input Buffer. + // + EndBuffer = Buffer + Width * Increment; - if (Value < 0) { + if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) { Value = -Value; - Buffer = BasePrintLibFillBuffer (Buffer, 1, '-', Increment); + Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment); + Width--; } - Count = BasePrintLibValueToString (ValueBuffer, Value, 10); + Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16; + Count = BasePrintLibValueToString (ValueBuffer, Value, Radix); if ((Flags & PREFIX_ZERO) != 0) { - Buffer = BasePrintLibFillBuffer (Buffer, Width - Count, '0', Increment); + Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment); } - Digits = 3 - (Count % 3); + Digits = Count % 3; + if (Digits != 0) { + Digits = 3 - Digits; + } for (Index = 0; Index < Count; Index++) { - Buffer = BasePrintLibFillBuffer (Buffer, 1, ValueBuffer[Count - Index], Increment); + Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ValueBuffer[Count - Index], Increment); if ((Flags & COMMA_TYPE) != 0) { Digits++; if (Digits == 3) { Digits = 0; if ((Index + 1) < Count) { - Buffer = BasePrintLibFillBuffer (Buffer, 1, ',', Increment); + Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment); } } } } - Buffer = BasePrintLibFillBuffer (Buffer, 1, 0, Increment); + BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment); return ((Buffer - OriginalBuffer) / Increment); } +