From 0247cb7cd4024cf2ce45996c293be53e5b87c3cc Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Tue, 16 Feb 2021 19:05:43 +0000 Subject: [PATCH] Add AsciiPrint and AsciiVSPrint Per https://github.com/tianocore/edk2/blob/master/MdePkg/Include/Library/PrintLib.h AsciiPrint() is the official name of APrint() so declare it as such and define an APrint alias for compatibility. Also add an AsciiVSPrint() to print a formatted ASCII string to a buffer using a va_list. AsciiPrint() too is defined in EDK2's PrintLib.h, though our implementation just invokes the Unicode version and then converts the buffer to ASCII. --- inc/efilib.h | 13 ++++++++++- lib/print.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/inc/efilib.h b/inc/efilib.h index ed332aa..40e3745 100644 --- a/inc/efilib.h +++ b/inc/efilib.h @@ -580,11 +580,22 @@ IPrintAt ( ); UINTN -APrint ( +AsciiPrint ( IN CONST CHAR8 *fmt, ... ); +/* For compatibility with previous versions */ +#define APrint AsciiPrint + +UINTN +AsciiVSPrint( + OUT CHAR8 *Str, + IN UINTN StrSize, + IN CONST CHAR8 *fmt, + va_list args +); + VOID ValueToHex ( IN CHAR16 *Buffer, diff --git a/lib/print.c b/lib/print.c index 35e43c0..8bd48d4 100644 --- a/lib/print.c +++ b/lib/print.c @@ -405,7 +405,7 @@ _PoolCatPrint ( IN OUT POOL_PRINT *spc, IN INTN (EFIAPI *Output)(VOID *context, CHAR16 *str) ) -// Dispath function for SPrint, PoolPrint, and CatPrint +// Dispatch function for SPrint, PoolPrint, and CatPrint { PRINT_STATE ps; @@ -810,7 +810,7 @@ _IPrint ( UINTN -APrint ( +AsciiPrint ( IN CONST CHAR8 *fmt, ... ) @@ -842,6 +842,64 @@ Returns: } +UINTN +AsciiVSPrint ( + OUT CHAR8 *Str, + IN UINTN StrSize, + IN CONST CHAR8 *fmt, + va_list args +) +/*++ + +Routine Description: + + Prints a formatted ascii string to a buffer using a va_list + +Arguments: + + Str - Output buffer to print the formatted string into + + StrSize - Size of Str. String is truncated to this size. + A size of 0 means there is no limit + + fmt - The format string + + args - va_list + + +Returns: + + String length returned in buffer + +--*/ +// Use Unicode VSPrint() and convert back to ASCII +{ + CHAR16 *UnicodeStr, *UnicodeFmt; + UINTN i, Len; + + UnicodeStr = AllocatePool(StrSize * sizeof(CHAR16)); + if (!UnicodeStr) + return 0; + + UnicodeFmt = PoolPrint(L"%a", fmt); + if (!UnicodeFmt) { + FreePool(UnicodeStr); + return 0; + } + + Len = VSPrint(UnicodeStr, StrSize, UnicodeFmt, args); + FreePool(UnicodeFmt); + + // The strings are ASCII so just do a plain Unicode conversion + for (i = 0; i < Len; i++) + Str[i] = (CHAR8)UnicodeStr[i]; + Str[Len] = 0; + FreePool(UnicodeStr); + + return Len; +} + + STATIC VOID PFLUSH (