X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdePkg%2FLibrary%2FBasePrintLib%2FPrintLibInternal.c;h=e1e8c08c5519b57f3af7cd47b14758904867582e;hp=9314b609119626be20906b5a00325b4a65eab4a1;hb=1efcc4ae46f52e3845923ffbab68426e068709d2;hpb=24e25d11c0460dfb39fade685375c0e58cbcb40e diff --git a/MdePkg/Library/BasePrintLib/PrintLibInternal.c b/MdePkg/Library/BasePrintLib/PrintLibInternal.c index 9314b60911..e1e8c08c55 100644 --- a/MdePkg/Library/BasePrintLib/PrintLibInternal.c +++ b/MdePkg/Library/BasePrintLib/PrintLibInternal.c @@ -1,7 +1,7 @@ /** @file - Print Library worker functions. + Print Library internal worker functions. - Copyright (c) 2006, Intel Corporation
+ Copyright (c) 2006 - 2008, 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 @@ -10,13 +10,14 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - Module Name: PrintLibInternal.c - **/ + + + #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,24 +26,27 @@ 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. - @return Number of characters printed. + @return Buffer Buffer filled with the input Character. **/ CHAR8 * BasePrintLibFillBuffer ( - CHAR8 *Buffer, - INTN Length, - UINTN Character, - INTN Increment + OUT CHAR8 *Buffer, + IN CHAR8 *EndBuffer, + IN INTN Length, + IN UINTN Character, + IN INTN Increment ) { 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; @@ -63,7 +67,6 @@ BasePrintLibFillBuffer ( **/ UINTN -EFIAPI BasePrintLibValueToString ( IN OUT CHAR8 *Buffer, IN INT64 Value, @@ -83,6 +86,10 @@ BasePrintLibValueToString ( *(Buffer++) = mHexStr[Remainder]; Digits++; } while (Value != 0); + + // + // the length of Buffer string converted from Value + // return Digits; } @@ -92,7 +99,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 +109,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 +126,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,54 +143,94 @@ BasePrintLibConvertValueToString ( ) { CHAR8 *OriginalBuffer; + CHAR8 *EndBuffer; CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS]; UINTN Count; UINTN Digits; UINTN Index; + UINTN Radix; + // + // Make sure Buffer is not NULL and Width < MAXIMUM + // ASSERT (Buffer != NULL); ASSERT (Width < MAXIMUM_VALUE_CHARACTERS); // // Make sure Flags can only contain supported bits. // - ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO)) == 0); + ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0); - OriginalBuffer = Buffer; + // + // If both COMMA_TYPE and HEX_RADIX are set, then ASSERT () + // + ASSERT (((Flags & COMMA_TYPE) != 0 && (Flags & RADIX_HEX) != 0) == FALSE); + OriginalBuffer = Buffer; + + // + // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored. + // if (Width == 0 || (Flags & COMMA_TYPE) != 0) { Flags &= (~PREFIX_ZERO); } - + // + // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed. + // if (Width == 0) { Width = MAXIMUM_VALUE_CHARACTERS - 1; } - - if (Value < 0) { + // + // Set the tag for the end of the input Buffer. + // + EndBuffer = Buffer + Width * Increment; + + // + // Convert decimal negative + // + 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); - + + // + // Count the length of the value string. + // + Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16; + Count = BasePrintLibValueToString (ValueBuffer, Value, Radix); + + // + // Append Zero + // if ((Flags & PREFIX_ZERO) != 0) { - Buffer = BasePrintLibFillBuffer (Buffer, Width - Count, '0', Increment); + Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment); + } + + // + // Print Comma type for every 3 characters + // + Digits = Count % 3; + if (Digits != 0) { + Digits = 3 - Digits; } - - Digits = 3 - (Count % 3); 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); + + // + // Print Null-terminator + // + BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment); return ((Buffer - OriginalBuffer) / Increment); } +