2 Print Library internal worker functions.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "PrintLibInternal.h"
11 #define WARNING_STATUS_NUMBER 5
12 #define ERROR_STATUS_NUMBER 33
17 #define RSIZE_MAX (PcdGet32 (PcdMaximumUnicodeStringLength))
18 #define ASCII_RSIZE_MAX (PcdGet32 (PcdMaximumAsciiStringLength))
20 #define SAFE_PRINT_CONSTRAINT_CHECK(Expression, RetVal) \
22 ASSERT (Expression); \
23 if (!(Expression)) { \
28 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr
[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
30 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
* CONST mStatusString
[] = {
31 "Success", // RETURN_SUCCESS = 0
32 "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH = 1
33 "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE = 2
34 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE = 3
35 "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL = 4
36 "Warning Stale Data", // RETURN_WARN_STALE_DATA = 5
37 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT
38 "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT
39 "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT
40 "Bad Buffer Size", // RETURN_BAD_BUFFER_SIZE = 4 | MAX_BIT
41 "Buffer Too Small", // RETURN_BUFFER_TOO_SMALL, = 5 | MAX_BIT
42 "Not Ready", // RETURN_NOT_READY = 6 | MAX_BIT
43 "Device Error", // RETURN_DEVICE_ERROR = 7 | MAX_BIT
44 "Write Protected", // RETURN_WRITE_PROTECTED = 8 | MAX_BIT
45 "Out of Resources", // RETURN_OUT_OF_RESOURCES = 9 | MAX_BIT
46 "Volume Corrupt", // RETURN_VOLUME_CORRUPTED = 10 | MAX_BIT
47 "Volume Full", // RETURN_VOLUME_FULL = 11 | MAX_BIT
48 "No Media", // RETURN_NO_MEDIA = 12 | MAX_BIT
49 "Media changed", // RETURN_MEDIA_CHANGED = 13 | MAX_BIT
50 "Not Found", // RETURN_NOT_FOUND = 14 | MAX_BIT
51 "Access Denied", // RETURN_ACCESS_DENIED = 15 | MAX_BIT
52 "No Response", // RETURN_NO_RESPONSE = 16 | MAX_BIT
53 "No mapping", // RETURN_NO_MAPPING = 17 | MAX_BIT
54 "Time out", // RETURN_TIMEOUT = 18 | MAX_BIT
55 "Not started", // RETURN_NOT_STARTED = 19 | MAX_BIT
56 "Already started", // RETURN_ALREADY_STARTED = 20 | MAX_BIT
57 "Aborted", // RETURN_ABORTED = 21 | MAX_BIT
58 "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT
59 "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT
60 "Protocol Error", // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT
61 "Incompatible Version", // RETURN_INCOMPATIBLE_VERSION = 25 | MAX_BIT
62 "Security Violation", // RETURN_SECURITY_VIOLATION = 26 | MAX_BIT
63 "CRC Error", // RETURN_CRC_ERROR = 27 | MAX_BIT
64 "End of Media", // RETURN_END_OF_MEDIA = 28 | MAX_BIT
65 "Reserved (29)", // RESERVED = 29 | MAX_BIT
66 "Reserved (30)", // RESERVED = 30 | MAX_BIT
67 "End of File", // RETURN_END_OF_FILE = 31 | MAX_BIT
68 "Invalid Language", // RETURN_INVALID_LANGUAGE = 32 | MAX_BIT
69 "Compromised Data" // RETURN_COMPROMISED_DATA = 33 | MAX_BIT
74 Internal function that places the character into the Buffer.
76 Internal function that places ASCII or Unicode character into the Buffer.
78 @param Buffer The buffer to place the Unicode or ASCII string.
79 @param EndBuffer The end of the input Buffer. No characters will be
81 @param Length The count of character to be placed into Buffer.
82 (Negative value indicates no buffer fill.)
83 @param Character The character to be placed into Buffer.
84 @param Increment The character increment in Buffer.
90 BasePrintLibFillBuffer (
100 for (Index
= 0; Index
< Length
&& Buffer
< EndBuffer
; Index
++) {
101 *Buffer
= (CHAR8
) Character
;
102 if (Increment
!= 1) {
103 *(Buffer
+ 1) = (CHAR8
)(Character
>> 8);
112 Internal function that convert a number to a string in Buffer.
114 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.
116 @param Buffer Location to place the ASCII string of Value.
117 @param Value The value to convert to a Decimal or Hexadecimal string in Buffer.
118 @param Radix Radix of the value
120 @return A pointer to the end of buffer filled with ASCII string.
124 BasePrintLibValueToString (
125 IN OUT CHAR8
*Buffer
,
133 // Loop to convert one digit at a time in reverse order
137 Value
= (INT64
)DivU64x32Remainder ((UINT64
)Value
, (UINT32
)Radix
, &Remainder
);
138 *(++Buffer
) = mHexStr
[Remainder
];
139 } while (Value
!= 0);
142 // Return pointer of the end of filled buffer.
148 Internal function that converts a decimal value to a Null-terminated string.
150 Converts the decimal number specified by Value to a Null-terminated
151 string specified by Buffer containing at most Width characters.
152 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
153 The total number of characters placed in Buffer is returned.
154 If the conversion contains more than Width characters, then only the first
155 Width characters are returned, and the total number of characters
156 required to perform the conversion is returned.
157 Additional conversion parameters are specified in Flags.
158 The Flags bit LEFT_JUSTIFY is always ignored.
159 All conversions are left justified in Buffer.
160 If Width is 0, PREFIX_ZERO is ignored in Flags.
161 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
162 are inserted every 3rd digit starting from the right.
163 If Value is < 0, then the fist character in Buffer is a '-'.
164 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
165 then Buffer is padded with '0' characters so the combination of the optional '-'
166 sign character, '0' characters, digit characters for Value, and the Null-terminator
167 add up to Width characters.
169 If Buffer is NULL, then ASSERT().
170 If unsupported bits are set in Flags, then ASSERT().
171 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
173 @param Buffer The pointer to the output buffer for the produced Null-terminated
175 @param Flags The bitmask of flags that specify left justification, zero pad,
177 @param Value The 64-bit signed value to convert to a string.
178 @param Width The maximum number of characters to place in Buffer, not including
180 @param Increment The character increment in Buffer.
182 @return Total number of characters required to perform the conversion.
186 BasePrintLibConvertValueToString (
187 IN OUT CHAR8
*Buffer
,
194 CHAR8
*OriginalBuffer
;
196 CHAR8 ValueBuffer
[MAXIMUM_VALUE_CHARACTERS
];
197 CHAR8
*ValueBufferPtr
;
204 // Make sure Buffer is not NULL and Width < MAXIMUM
206 ASSERT (Buffer
!= NULL
);
207 ASSERT (Width
< MAXIMUM_VALUE_CHARACTERS
);
209 // Make sure Flags can only contain supported bits.
211 ASSERT ((Flags
& ~(LEFT_JUSTIFY
| COMMA_TYPE
| PREFIX_ZERO
| RADIX_HEX
)) == 0);
214 // If both COMMA_TYPE and RADIX_HEX are set, then ASSERT ()
216 ASSERT (((Flags
& COMMA_TYPE
) == 0) || ((Flags
& RADIX_HEX
) == 0));
218 OriginalBuffer
= Buffer
;
221 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.
223 if (Width
== 0 || (Flags
& COMMA_TYPE
) != 0) {
224 Flags
&= ~((UINTN
) PREFIX_ZERO
);
227 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
230 Width
= MAXIMUM_VALUE_CHARACTERS
- 1;
233 // Set the tag for the end of the input Buffer.
235 EndBuffer
= Buffer
+ Width
* Increment
;
238 // Convert decimal negative
240 if ((Value
< 0) && ((Flags
& RADIX_HEX
) == 0)) {
242 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, '-', Increment
);
247 // Count the length of the value string.
249 Radix
= ((Flags
& RADIX_HEX
) == 0)? 10 : 16;
250 ValueBufferPtr
= BasePrintLibValueToString (ValueBuffer
, Value
, Radix
);
251 Count
= ValueBufferPtr
- ValueBuffer
;
256 if ((Flags
& PREFIX_ZERO
) != 0) {
257 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, Width
- Count
, '0', Increment
);
261 // Print Comma type for every 3 characters
267 for (Index
= 0; Index
< Count
; Index
++) {
268 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, *ValueBufferPtr
--, Increment
);
269 if ((Flags
& COMMA_TYPE
) != 0) {
273 if ((Index
+ 1) < Count
) {
274 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, ',', Increment
);
281 // Print Null-terminator
283 BasePrintLibFillBuffer (Buffer
, EndBuffer
+ Increment
, 1, 0, Increment
);
285 return ((Buffer
- OriginalBuffer
) / Increment
);
289 Internal function that converts a decimal value to a Null-terminated string.
291 Converts the decimal number specified by Value to a Null-terminated string
292 specified by Buffer containing at most Width characters. If Width is 0 then a
293 width of MAXIMUM_VALUE_CHARACTERS is assumed. If the conversion contains more
294 than Width characters, then only the first Width characters are placed in
295 Buffer. Additional conversion parameters are specified in Flags.
296 The Flags bit LEFT_JUSTIFY is always ignored.
297 All conversions are left justified in Buffer.
298 If Width is 0, PREFIX_ZERO is ignored in Flags.
299 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and
300 commas are inserted every 3rd digit starting from the right.
301 If Value is < 0, then the fist character in Buffer is a '-'.
302 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
303 then Buffer is padded with '0' characters so the combination of the optional
304 '-' sign character, '0' characters, digit characters for Value, and the
305 Null-terminator add up to Width characters.
307 If an error would be returned, the function will ASSERT().
309 @param Buffer The pointer to the output buffer for the produced
310 Null-terminated string.
311 @param BufferSize The size of Buffer in bytes, including the
313 @param Flags The bitmask of flags that specify left justification,
314 zero pad, and commas.
315 @param Value The 64-bit signed value to convert to a string.
316 @param Width The maximum number of characters to place in Buffer,
317 not including the Null-terminator.
318 @param Increment The character increment in Buffer.
320 @retval RETURN_SUCCESS The decimal value is converted.
321 @retval RETURN_BUFFER_TOO_SMALL If BufferSize cannot hold the converted
323 @retval RETURN_INVALID_PARAMETER If Buffer is NULL.
324 If Increment is 1 and
325 PcdMaximumAsciiStringLength is not zero,
326 BufferSize is greater than
327 PcdMaximumAsciiStringLength.
328 If Increment is not 1 and
329 PcdMaximumUnicodeStringLength is not zero,
330 BufferSize is greater than
331 (PcdMaximumUnicodeStringLength *
332 sizeof (CHAR16) + 1).
333 If unsupported bits are set in Flags.
334 If both COMMA_TYPE and RADIX_HEX are set in
336 If Width >= MAXIMUM_VALUE_CHARACTERS.
340 BasePrintLibConvertValueToStringS (
341 IN OUT CHAR8
*Buffer
,
350 CHAR8 ValueBuffer
[MAXIMUM_VALUE_CHARACTERS
];
351 CHAR8
*ValueBufferPtr
;
358 // 1. Buffer shall not be a null pointer.
360 SAFE_PRINT_CONSTRAINT_CHECK ((Buffer
!= NULL
), RETURN_INVALID_PARAMETER
);
363 // 2. BufferSize shall not be greater than (RSIZE_MAX * sizeof (CHAR16)) for
364 // Unicode output string or shall not be greater than ASCII_RSIZE_MAX for
365 // Ascii output string.
367 if (Increment
== 1) {
369 // Ascii output string
371 if (ASCII_RSIZE_MAX
!= 0) {
372 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize
<= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
376 // Unicode output string
378 if (RSIZE_MAX
!= 0) {
379 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize
<= RSIZE_MAX
* sizeof (CHAR16
) + 1), RETURN_INVALID_PARAMETER
);
384 // 3. Flags shall be set properly.
386 SAFE_PRINT_CONSTRAINT_CHECK (((Flags
& ~(LEFT_JUSTIFY
| COMMA_TYPE
| PREFIX_ZERO
| RADIX_HEX
)) == 0), RETURN_INVALID_PARAMETER
);
387 SAFE_PRINT_CONSTRAINT_CHECK ((((Flags
& COMMA_TYPE
) == 0) || ((Flags
& RADIX_HEX
) == 0)), RETURN_INVALID_PARAMETER
);
390 // 4. Width shall be smaller than MAXIMUM_VALUE_CHARACTERS.
392 SAFE_PRINT_CONSTRAINT_CHECK ((Width
< MAXIMUM_VALUE_CHARACTERS
), RETURN_INVALID_PARAMETER
);
395 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.
397 if (Width
== 0 || (Flags
& COMMA_TYPE
) != 0) {
398 Flags
&= ~((UINTN
) PREFIX_ZERO
);
401 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
404 Width
= MAXIMUM_VALUE_CHARACTERS
- 1;
408 // Count the characters of the output string.
411 Radix
= ((Flags
& RADIX_HEX
) == 0)? 10 : 16;
413 if ((Flags
& PREFIX_ZERO
) != 0) {
416 if ((Value
< 0) && ((Flags
& RADIX_HEX
) == 0)) {
417 Count
++; // minus sign
418 ValueBufferPtr
= BasePrintLibValueToString (ValueBuffer
, -Value
, Radix
);
420 ValueBufferPtr
= BasePrintLibValueToString (ValueBuffer
, Value
, Radix
);
422 Digits
= ValueBufferPtr
- ValueBuffer
;
425 if ((Flags
& COMMA_TYPE
) != 0) {
426 Count
+= (Digits
- 1) / 3; // commas
430 Width
= MIN (Count
, Width
);
433 // 5. BufferSize shall be large enough to hold the converted string.
435 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize
>= (Width
+ 1) * Increment
), RETURN_BUFFER_TOO_SMALL
);
438 // Set the tag for the end of the input Buffer.
440 EndBuffer
= Buffer
+ Width
* Increment
;
443 // Convert decimal negative
445 if ((Value
< 0) && ((Flags
& RADIX_HEX
) == 0)) {
447 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, '-', Increment
);
452 // Count the length of the value string.
454 ValueBufferPtr
= BasePrintLibValueToString (ValueBuffer
, Value
, Radix
);
455 Count
= ValueBufferPtr
- ValueBuffer
;
460 if ((Flags
& PREFIX_ZERO
) != 0) {
461 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, Width
- Count
, '0', Increment
);
465 // Print Comma type for every 3 characters
471 for (Index
= 0; Index
< Count
; Index
++) {
472 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, *ValueBufferPtr
--, Increment
);
473 if ((Flags
& COMMA_TYPE
) != 0) {
477 if ((Index
+ 1) < Count
) {
478 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, ',', Increment
);
485 // Print Null-terminator
487 BasePrintLibFillBuffer (Buffer
, EndBuffer
+ Increment
, 1, 0, Increment
);
489 return RETURN_SUCCESS
;
493 Worker function that produces a Null-terminated string in an output buffer
494 based on a Null-terminated format string and a VA_LIST argument list.
496 VSPrint function to process format and place the results in Buffer. Since a
497 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
498 this is the main print working routine.
500 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.
502 @param[out] Buffer The character buffer to print the results of the
503 parsing of Format into.
504 @param[in] BufferSize The maximum number of characters to put into
506 @param[in] Flags Initial flags value.
507 Can only have FORMAT_UNICODE, OUTPUT_UNICODE,
508 and COUNT_ONLY_NO_PRINT set.
509 @param[in] Format A Null-terminated format string.
510 @param[in] VaListMarker VA_LIST style variable argument list consumed by
512 @param[in] BaseListMarker BASE_LIST style variable argument list consumed
513 by processing Format.
515 @return The number of characters printed not including the Null-terminator.
516 If COUNT_ONLY_NO_PRINT was set returns the same, but without any
517 modification to Buffer.
521 BasePrintLibSPrintMarker (
525 IN CONST CHAR8
*Format
,
526 IN VA_LIST VaListMarker
, OPTIONAL
527 IN BASE_LIST BaseListMarker OPTIONAL
530 CHAR8
*OriginalBuffer
;
532 CHAR8 ValueBuffer
[MAXIMUM_VALUE_CHARACTERS
];
533 UINT32 BytesPerOutputCharacter
;
534 UINTN BytesPerFormatCharacter
;
536 UINTN FormatCharacter
;
540 CONST CHAR8
*ArgumentString
;
546 INTN BytesPerArgumentCharacter
;
547 UINTN ArgumentCharacter
;
555 RETURN_STATUS Status
;
559 UINTN LengthToReturn
;
562 // If you change this code be sure to match the 2 versions of this function.
563 // Nearly identical logic is found in the BasePrintLib and
564 // DxePrintLibPrint2Protocol (both PrintLib instances).
568 // 1. Buffer shall not be a null pointer when both BufferSize > 0 and
569 // COUNT_ONLY_NO_PRINT is not set in Flags.
571 if ((BufferSize
> 0) && ((Flags
& COUNT_ONLY_NO_PRINT
) == 0)) {
572 SAFE_PRINT_CONSTRAINT_CHECK ((Buffer
!= NULL
), 0);
576 // 2. Format shall not be a null pointer when BufferSize > 0 or when
577 // COUNT_ONLY_NO_PRINT is set in Flags.
579 if ((BufferSize
> 0) || ((Flags
& COUNT_ONLY_NO_PRINT
) != 0)) {
580 SAFE_PRINT_CONSTRAINT_CHECK ((Format
!= NULL
), 0);
584 // 3. BufferSize shall not be greater than RSIZE_MAX for Unicode output or
585 // ASCII_RSIZE_MAX for Ascii output.
587 if ((Flags
& OUTPUT_UNICODE
) != 0) {
588 if (RSIZE_MAX
!= 0) {
589 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize
<= RSIZE_MAX
), 0);
591 BytesPerOutputCharacter
= 2;
593 if (ASCII_RSIZE_MAX
!= 0) {
594 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize
<= ASCII_RSIZE_MAX
), 0);
596 BytesPerOutputCharacter
= 1;
600 // 4. Format shall not contain more than RSIZE_MAX Unicode characters or
601 // ASCII_RSIZE_MAX Ascii characters.
603 if ((Flags
& FORMAT_UNICODE
) != 0) {
604 if (RSIZE_MAX
!= 0) {
605 SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16
*)Format
, RSIZE_MAX
+ 1) <= RSIZE_MAX
), 0);
607 BytesPerFormatCharacter
= 2;
610 if (ASCII_RSIZE_MAX
!= 0) {
611 SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format
, ASCII_RSIZE_MAX
+ 1) <= ASCII_RSIZE_MAX
), 0);
613 BytesPerFormatCharacter
= 1;
617 if ((Flags
& COUNT_ONLY_NO_PRINT
) != 0) {
618 if (BufferSize
== 0) {
623 // We can run without a Buffer for counting only.
625 if (BufferSize
== 0) {
632 OriginalBuffer
= NULL
;
635 // Reserve space for the Null terminator.
637 if (Buffer
!= NULL
) {
639 OriginalBuffer
= Buffer
;
642 // Set the tag for the end of the input Buffer.
644 EndBuffer
= Buffer
+ BufferSize
* BytesPerOutputCharacter
;
648 // Get the first character from the format string
650 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
653 // Loop until the end of the format string is reached or the output buffer is full
655 while (FormatCharacter
!= 0) {
656 if ((Buffer
!= NULL
) && (Buffer
>= EndBuffer
)) {
660 // Clear all the flag bits except those that may have been passed in
662 Flags
&= (UINTN
) (OUTPUT_UNICODE
| FORMAT_UNICODE
| COUNT_ONLY_NO_PRINT
);
665 // Set the default width to zero, and the default precision to 1
675 switch (FormatCharacter
) {
678 // Parse Flags and Width
680 for (Done
= FALSE
; !Done
; ) {
681 Format
+= BytesPerFormatCharacter
;
682 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
683 switch (FormatCharacter
) {
688 Flags
|= LEFT_JUSTIFY
;
691 Flags
|= PREFIX_SIGN
;
694 Flags
|= PREFIX_BLANK
;
704 if ((Flags
& PRECISION
) == 0) {
705 Flags
|= PAD_TO_WIDTH
;
706 if (BaseListMarker
== NULL
) {
707 Width
= VA_ARG (VaListMarker
, UINTN
);
709 Width
= BASE_ARG (BaseListMarker
, UINTN
);
712 if (BaseListMarker
== NULL
) {
713 Precision
= VA_ARG (VaListMarker
, UINTN
);
715 Precision
= BASE_ARG (BaseListMarker
, UINTN
);
720 if ((Flags
& PRECISION
) == 0) {
721 Flags
|= PREFIX_ZERO
;
732 for (Count
= 0; ((FormatCharacter
>= '0') && (FormatCharacter
<= '9')); ){
733 Count
= (Count
* 10) + FormatCharacter
- '0';
734 Format
+= BytesPerFormatCharacter
;
735 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
737 Format
-= BytesPerFormatCharacter
;
738 if ((Flags
& PRECISION
) == 0) {
739 Flags
|= PAD_TO_WIDTH
;
748 // Make no output if Format string terminates unexpectedly when
749 // looking up for flag, width, precision and type.
751 Format
-= BytesPerFormatCharacter
;
754 // break skipped on purpose.
763 // Handle each argument type
765 switch (FormatCharacter
) {
768 // Flag space, +, 0, L & l are invalid for type p.
770 Flags
&= ~((UINTN
) (PREFIX_BLANK
| PREFIX_SIGN
| PREFIX_ZERO
| LONG_TYPE
));
771 if (sizeof (VOID
*) > 4) {
775 // break skipped on purpose
778 Flags
|= PREFIX_ZERO
;
780 // break skipped on purpose
785 // break skipped on purpose
788 if ((Flags
& RADIX_HEX
) == 0) {
789 Flags
&= ~((UINTN
) (PREFIX_SIGN
));
790 Flags
|= UNSIGNED_TYPE
;
793 // break skipped on purpose
796 if ((Flags
& LONG_TYPE
) == 0) {
798 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
799 // This assumption is made so the format string definition is compatible with the ANSI C
800 // Specification for formatted strings. It is recommended that the Base Types be used
801 // everywhere, but in this one case, compliance with ANSI C is more important, and
802 // provides an implementation that is compatible with that largest possible set of CPU
803 // architectures. This is why the type "int" is used in this one case.
805 if (BaseListMarker
== NULL
) {
806 Value
= VA_ARG (VaListMarker
, int);
808 Value
= BASE_ARG (BaseListMarker
, int);
811 if (BaseListMarker
== NULL
) {
812 Value
= VA_ARG (VaListMarker
, INT64
);
814 Value
= BASE_ARG (BaseListMarker
, INT64
);
817 if ((Flags
& PREFIX_BLANK
) != 0) {
820 if ((Flags
& PREFIX_SIGN
) != 0) {
823 if ((Flags
& COMMA_TYPE
) != 0) {
826 if ((Flags
& RADIX_HEX
) == 0) {
829 Flags
&= ~((UINTN
) PREFIX_ZERO
);
832 if (Value
< 0 && (Flags
& UNSIGNED_TYPE
) == 0) {
833 Flags
|= PREFIX_SIGN
;
836 } else if ((Flags
& UNSIGNED_TYPE
) != 0 && (Flags
& LONG_TYPE
) == 0) {
838 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
839 // This assumption is made so the format string definition is compatible with the ANSI C
840 // Specification for formatted strings. It is recommended that the Base Types be used
841 // everywhere, but in this one case, compliance with ANSI C is more important, and
842 // provides an implementation that is compatible with that largest possible set of CPU
843 // architectures. This is why the type "unsigned int" is used in this one case.
845 Value
= (unsigned int)Value
;
850 if ((Flags
& LONG_TYPE
) == 0 && Value
< 0) {
852 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
853 // This assumption is made so the format string definition is compatible with the ANSI C
854 // Specification for formatted strings. It is recommended that the Base Types be used
855 // everywhere, but in this one case, compliance with ANSI C is more important, and
856 // provides an implementation that is compatible with that largest possible set of CPU
857 // architectures. This is why the type "unsigned int" is used in this one case.
859 Value
= (unsigned int)Value
;
863 // Convert Value to a reversed string
865 Count
= BasePrintLibValueToString (ValueBuffer
, Value
, Radix
) - ValueBuffer
;
866 if (Value
== 0 && Precision
== 0) {
869 ArgumentString
= (CHAR8
*)ValueBuffer
+ Count
;
875 if (Comma
&& Count
!= 0) {
876 Count
+= ((Count
- 1) / 3);
882 Flags
|= ARGUMENT_REVERSED
;
884 if ((Flags
& PREFIX_ZERO
) != 0) {
885 if ((Flags
& LEFT_JUSTIFY
) == 0) {
886 if ((Flags
& PAD_TO_WIDTH
) != 0) {
887 if ((Flags
& PRECISION
) == 0) {
897 Flags
|= ARGUMENT_UNICODE
;
899 // break skipped on purpose
902 if (BaseListMarker
== NULL
) {
903 ArgumentString
= VA_ARG (VaListMarker
, CHAR8
*);
905 ArgumentString
= BASE_ARG (BaseListMarker
, CHAR8
*);
907 if (ArgumentString
== NULL
) {
908 Flags
&= ~((UINTN
) ARGUMENT_UNICODE
);
909 ArgumentString
= "<null string>";
912 // Set the default precision for string to be zero if not specified.
914 if ((Flags
& PRECISION
) == 0) {
920 if (BaseListMarker
== NULL
) {
921 Character
= VA_ARG (VaListMarker
, UINTN
) & 0xffff;
923 Character
= BASE_ARG (BaseListMarker
, UINTN
) & 0xffff;
925 ArgumentString
= (CHAR8
*)&Character
;
926 Flags
|= ARGUMENT_UNICODE
;
930 if (BaseListMarker
== NULL
) {
931 TmpGuid
= VA_ARG (VaListMarker
, GUID
*);
933 TmpGuid
= BASE_ARG (BaseListMarker
, GUID
*);
935 if (TmpGuid
== NULL
) {
936 ArgumentString
= "<null guid>";
938 GuidData1
= ReadUnaligned32 (&(TmpGuid
->Data1
));
939 GuidData2
= ReadUnaligned16 (&(TmpGuid
->Data2
));
940 GuidData3
= ReadUnaligned16 (&(TmpGuid
->Data3
));
943 MAXIMUM_VALUE_CHARACTERS
,
945 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
958 ArgumentString
= ValueBuffer
;
963 if (BaseListMarker
== NULL
) {
964 TmpTime
= VA_ARG (VaListMarker
, TIME
*);
966 TmpTime
= BASE_ARG (BaseListMarker
, TIME
*);
968 if (TmpTime
== NULL
) {
969 ArgumentString
= "<null time>";
973 MAXIMUM_VALUE_CHARACTERS
,
975 "%02d/%02d/%04d %02d:%02d",
982 ArgumentString
= ValueBuffer
;
987 if (BaseListMarker
== NULL
) {
988 Status
= VA_ARG (VaListMarker
, RETURN_STATUS
);
990 Status
= BASE_ARG (BaseListMarker
, RETURN_STATUS
);
992 ArgumentString
= ValueBuffer
;
993 if (RETURN_ERROR (Status
)) {
997 Index
= Status
& ~MAX_BIT
;
998 if (Index
> 0 && Index
<= ERROR_STATUS_NUMBER
) {
999 ArgumentString
= mStatusString
[Index
+ WARNING_STATUS_NUMBER
];
1003 if (Index
<= WARNING_STATUS_NUMBER
) {
1004 ArgumentString
= mStatusString
[Index
];
1007 if (ArgumentString
== ValueBuffer
) {
1008 BasePrintLibSPrint ((CHAR8
*) ValueBuffer
, MAXIMUM_VALUE_CHARACTERS
, 0, "%08X", Status
);
1013 Format
+= BytesPerFormatCharacter
;
1014 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
1015 if (FormatCharacter
== '\n') {
1017 // Translate '\r\n' to '\r\n'
1019 ArgumentString
= "\r\n";
1022 // Translate '\r' to '\r'
1024 ArgumentString
= "\r";
1025 Format
-= BytesPerFormatCharacter
;
1031 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1033 ArgumentString
= "\r\n";
1034 Format
+= BytesPerFormatCharacter
;
1035 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
1036 if (FormatCharacter
!= '\r') {
1037 Format
-= BytesPerFormatCharacter
;
1044 // if the type is '%' or unknown, then print it to the screen
1046 ArgumentString
= (CHAR8
*)&FormatCharacter
;
1047 Flags
|= ARGUMENT_UNICODE
;
1053 Format
+= BytesPerFormatCharacter
;
1054 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
1055 if (FormatCharacter
== '\n') {
1057 // Translate '\r\n' to '\r\n'
1059 ArgumentString
= "\r\n";
1062 // Translate '\r' to '\r'
1064 ArgumentString
= "\r";
1065 Format
-= BytesPerFormatCharacter
;
1071 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1073 ArgumentString
= "\r\n";
1074 Format
+= BytesPerFormatCharacter
;
1075 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
1076 if (FormatCharacter
!= '\r') {
1077 Format
-= BytesPerFormatCharacter
;
1082 ArgumentString
= (CHAR8
*)&FormatCharacter
;
1083 Flags
|= ARGUMENT_UNICODE
;
1088 // Retrieve the ArgumentString attriubutes
1090 if ((Flags
& ARGUMENT_UNICODE
) != 0) {
1091 ArgumentMask
= 0xffff;
1092 BytesPerArgumentCharacter
= 2;
1094 ArgumentMask
= 0xff;
1095 BytesPerArgumentCharacter
= 1;
1097 if ((Flags
& ARGUMENT_REVERSED
) != 0) {
1098 BytesPerArgumentCharacter
= -BytesPerArgumentCharacter
;
1101 // Compute the number of characters in ArgumentString and store it in Count
1102 // ArgumentString is either null-terminated, or it contains Precision characters
1105 (ArgumentString
[Count
* BytesPerArgumentCharacter
] != '\0' ||
1106 (BytesPerArgumentCharacter
> 1 &&
1107 ArgumentString
[Count
* BytesPerArgumentCharacter
+ 1]!= '\0')) &&
1108 (Count
< Precision
|| ((Flags
& PRECISION
) == 0));
1110 ArgumentCharacter
= ((ArgumentString
[Count
* BytesPerArgumentCharacter
] & 0xff) | ((ArgumentString
[Count
* BytesPerArgumentCharacter
+ 1]) << 8)) & ArgumentMask
;
1111 if (ArgumentCharacter
== 0) {
1117 if (Precision
< Count
) {
1122 // Pad before the string
1124 if ((Flags
& (PAD_TO_WIDTH
| LEFT_JUSTIFY
)) == (PAD_TO_WIDTH
)) {
1125 LengthToReturn
+= ((Width
- Precision
) * BytesPerOutputCharacter
);
1126 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1127 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, Width
- Precision
, ' ', BytesPerOutputCharacter
);
1133 LengthToReturn
+= (1 * BytesPerOutputCharacter
);
1134 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1135 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, Prefix
, BytesPerOutputCharacter
);
1138 LengthToReturn
+= ((Precision
- Count
) * BytesPerOutputCharacter
);
1139 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1140 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, Precision
- Count
, '0', BytesPerOutputCharacter
);
1143 LengthToReturn
+= ((Precision
- Count
) * BytesPerOutputCharacter
);
1144 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1145 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, Precision
- Count
, ' ', BytesPerOutputCharacter
);
1148 LengthToReturn
+= (1 * BytesPerOutputCharacter
);
1149 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1150 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, Prefix
, BytesPerOutputCharacter
);
1156 // Output the Prefix character if it is present
1164 // Copy the string into the output buffer performing the required type conversions
1166 while (Index
< Count
&&
1167 (ArgumentString
[0] != '\0' ||
1168 (BytesPerArgumentCharacter
> 1 && ArgumentString
[1] != '\0'))) {
1169 ArgumentCharacter
= ((*ArgumentString
& 0xff) | (((UINT8
)*(ArgumentString
+ 1)) << 8)) & ArgumentMask
;
1171 LengthToReturn
+= (1 * BytesPerOutputCharacter
);
1172 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1173 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, ArgumentCharacter
, BytesPerOutputCharacter
);
1175 ArgumentString
+= BytesPerArgumentCharacter
;
1182 if (Index
< Count
) {
1183 LengthToReturn
+= (1 * BytesPerOutputCharacter
);
1184 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1185 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, ',', BytesPerOutputCharacter
);
1193 // Pad after the string
1195 if ((Flags
& (PAD_TO_WIDTH
| LEFT_JUSTIFY
)) == (PAD_TO_WIDTH
| LEFT_JUSTIFY
)) {
1196 LengthToReturn
+= ((Width
- Precision
) * BytesPerOutputCharacter
);
1197 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1198 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, Width
- Precision
, ' ', BytesPerOutputCharacter
);
1203 // Get the next character from the format string
1205 Format
+= BytesPerFormatCharacter
;
1208 // Get the next character from the format string
1210 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
1213 if ((Flags
& COUNT_ONLY_NO_PRINT
) != 0) {
1214 return (LengthToReturn
/ BytesPerOutputCharacter
);
1217 ASSERT (Buffer
!= NULL
);
1219 // Null terminate the Unicode or ASCII string
1221 BasePrintLibFillBuffer (Buffer
, EndBuffer
+ BytesPerOutputCharacter
, 1, 0, BytesPerOutputCharacter
);
1223 return ((Buffer
- OriginalBuffer
) / BytesPerOutputCharacter
);
1227 Worker function that produces a Null-terminated string in an output buffer
1228 based on a Null-terminated format string and variable argument list.
1230 VSPrint function to process format and place the results in Buffer. Since a
1231 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1232 this is the main print working routine
1234 @param StartOfBuffer The character buffer to print the results of the parsing
1236 @param BufferSize The maximum number of characters to put into buffer.
1237 Zero means no limit.
1238 @param Flags Initial flags value.
1239 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set
1240 @param FormatString A Null-terminated format string.
1241 @param ... The variable argument list.
1243 @return The number of characters printed.
1248 BasePrintLibSPrint (
1249 OUT CHAR8
*StartOfBuffer
,
1250 IN UINTN BufferSize
,
1252 IN CONST CHAR8
*FormatString
,
1257 UINTN NumberOfPrinted
;
1259 VA_START (Marker
, FormatString
);
1260 NumberOfPrinted
= BasePrintLibSPrintMarker (StartOfBuffer
, BufferSize
, Flags
, FormatString
, Marker
, NULL
);
1262 return NumberOfPrinted
;