2 Print Library internal worker functions.
4 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "PrintLibInternal.h"
17 #define WARNING_STATUS_NUMBER 5
18 #define ERROR_STATUS_NUMBER 33
23 #define RSIZE_MAX (PcdGet32 (PcdMaximumUnicodeStringLength))
24 #define ASCII_RSIZE_MAX (PcdGet32 (PcdMaximumAsciiStringLength))
26 #define SAFE_PRINT_CONSTRAINT_CHECK(Expression, RetVal) \
28 ASSERT (Expression); \
29 if (!(Expression)) { \
34 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr
[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
36 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
* CONST mStatusString
[] = {
37 "Success", // RETURN_SUCCESS = 0
38 "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH = 1
39 "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE = 2
40 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE = 3
41 "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL = 4
42 "Warning Stale Data", // RETURN_WARN_STALE_DATA = 5
43 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT
44 "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT
45 "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT
46 "Bad Buffer Size", // RETURN_BAD_BUFFER_SIZE = 4 | MAX_BIT
47 "Buffer Too Small", // RETURN_BUFFER_TOO_SMALL, = 5 | MAX_BIT
48 "Not Ready", // RETURN_NOT_READY = 6 | MAX_BIT
49 "Device Error", // RETURN_DEVICE_ERROR = 7 | MAX_BIT
50 "Write Protected", // RETURN_WRITE_PROTECTED = 8 | MAX_BIT
51 "Out of Resources", // RETURN_OUT_OF_RESOURCES = 9 | MAX_BIT
52 "Volume Corrupt", // RETURN_VOLUME_CORRUPTED = 10 | MAX_BIT
53 "Volume Full", // RETURN_VOLUME_FULL = 11 | MAX_BIT
54 "No Media", // RETURN_NO_MEDIA = 12 | MAX_BIT
55 "Media changed", // RETURN_MEDIA_CHANGED = 13 | MAX_BIT
56 "Not Found", // RETURN_NOT_FOUND = 14 | MAX_BIT
57 "Access Denied", // RETURN_ACCESS_DENIED = 15 | MAX_BIT
58 "No Response", // RETURN_NO_RESPONSE = 16 | MAX_BIT
59 "No mapping", // RETURN_NO_MAPPING = 17 | MAX_BIT
60 "Time out", // RETURN_TIMEOUT = 18 | MAX_BIT
61 "Not started", // RETURN_NOT_STARTED = 19 | MAX_BIT
62 "Already started", // RETURN_ALREADY_STARTED = 20 | MAX_BIT
63 "Aborted", // RETURN_ABORTED = 21 | MAX_BIT
64 "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT
65 "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT
66 "Protocol Error", // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT
67 "Incompatible Version", // RETURN_INCOMPATIBLE_VERSION = 25 | MAX_BIT
68 "Security Violation", // RETURN_SECURITY_VIOLATION = 26 | MAX_BIT
69 "CRC Error", // RETURN_CRC_ERROR = 27 | MAX_BIT
70 "End of Media", // RETURN_END_OF_MEDIA = 28 | MAX_BIT
71 "Reserved (29)", // RESERVED = 29 | MAX_BIT
72 "Reserved (30)", // RESERVED = 30 | MAX_BIT
73 "End of File", // RETURN_END_OF_FILE = 31 | MAX_BIT
74 "Invalid Language", // RETURN_INVALID_LANGUAGE = 32 | MAX_BIT
75 "Compromised Data" // RETURN_COMPROMISED_DATA = 33 | MAX_BIT
80 Internal function that places the character into the Buffer.
82 Internal function that places ASCII or Unicode character into the Buffer.
84 @param Buffer The buffer to place the Unicode or ASCII string.
85 @param EndBuffer The end of the input Buffer. No characters will be
87 @param Length The count of character to be placed into Buffer.
88 (Negative value indicates no buffer fill.)
89 @param Character The character to be placed into Buffer.
90 @param Increment The character increment in Buffer.
96 BasePrintLibFillBuffer (
106 for (Index
= 0; Index
< Length
&& Buffer
< EndBuffer
; Index
++) {
107 *Buffer
= (CHAR8
) Character
;
108 if (Increment
!= 1) {
109 *(Buffer
+ 1) = (CHAR8
)(Character
>> 8);
118 Internal function that convert a number to a string in Buffer.
120 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.
122 @param Buffer Location to place the ASCII string of Value.
123 @param Value The value to convert to a Decimal or Hexadecimal string in Buffer.
124 @param Radix Radix of the value
126 @return A pointer to the end of buffer filled with ASCII string.
130 BasePrintLibValueToString (
131 IN OUT CHAR8
*Buffer
,
139 // Loop to convert one digit at a time in reverse order
143 Value
= (INT64
)DivU64x32Remainder ((UINT64
)Value
, (UINT32
)Radix
, &Remainder
);
144 *(++Buffer
) = mHexStr
[Remainder
];
145 } while (Value
!= 0);
148 // Return pointer of the end of filled buffer.
154 Internal function that converts a decimal value to a Null-terminated string.
156 Converts the decimal number specified by Value to a Null-terminated
157 string specified by Buffer containing at most Width characters.
158 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
159 The total number of characters placed in Buffer is returned.
160 If the conversion contains more than Width characters, then only the first
161 Width characters are returned, and the total number of characters
162 required to perform the conversion is returned.
163 Additional conversion parameters are specified in Flags.
164 The Flags bit LEFT_JUSTIFY is always ignored.
165 All conversions are left justified in Buffer.
166 If Width is 0, PREFIX_ZERO is ignored in Flags.
167 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
168 are inserted every 3rd digit starting from the right.
169 If Value is < 0, then the fist character in Buffer is a '-'.
170 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
171 then Buffer is padded with '0' characters so the combination of the optional '-'
172 sign character, '0' characters, digit characters for Value, and the Null-terminator
173 add up to Width characters.
175 If Buffer is NULL, then ASSERT().
176 If unsupported bits are set in Flags, then ASSERT().
177 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
179 @param Buffer The pointer to the output buffer for the produced Null-terminated
181 @param Flags The bitmask of flags that specify left justification, zero pad,
183 @param Value The 64-bit signed value to convert to a string.
184 @param Width The maximum number of characters to place in Buffer, not including
186 @param Increment The character increment in Buffer.
188 @return Total number of characters required to perform the conversion.
192 BasePrintLibConvertValueToString (
193 IN OUT CHAR8
*Buffer
,
200 CHAR8
*OriginalBuffer
;
202 CHAR8 ValueBuffer
[MAXIMUM_VALUE_CHARACTERS
];
203 CHAR8
*ValueBufferPtr
;
210 // Make sure Buffer is not NULL and Width < MAXIMUM
212 ASSERT (Buffer
!= NULL
);
213 ASSERT (Width
< MAXIMUM_VALUE_CHARACTERS
);
215 // Make sure Flags can only contain supported bits.
217 ASSERT ((Flags
& ~(LEFT_JUSTIFY
| COMMA_TYPE
| PREFIX_ZERO
| RADIX_HEX
)) == 0);
220 // If both COMMA_TYPE and RADIX_HEX are set, then ASSERT ()
222 ASSERT (((Flags
& COMMA_TYPE
) == 0) || ((Flags
& RADIX_HEX
) == 0));
224 OriginalBuffer
= Buffer
;
227 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.
229 if (Width
== 0 || (Flags
& COMMA_TYPE
) != 0) {
230 Flags
&= ~((UINTN
) PREFIX_ZERO
);
233 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
236 Width
= MAXIMUM_VALUE_CHARACTERS
- 1;
239 // Set the tag for the end of the input Buffer.
241 EndBuffer
= Buffer
+ Width
* Increment
;
244 // Convert decimal negative
246 if ((Value
< 0) && ((Flags
& RADIX_HEX
) == 0)) {
248 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, '-', Increment
);
253 // Count the length of the value string.
255 Radix
= ((Flags
& RADIX_HEX
) == 0)? 10 : 16;
256 ValueBufferPtr
= BasePrintLibValueToString (ValueBuffer
, Value
, Radix
);
257 Count
= ValueBufferPtr
- ValueBuffer
;
262 if ((Flags
& PREFIX_ZERO
) != 0) {
263 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, Width
- Count
, '0', Increment
);
267 // Print Comma type for every 3 characters
273 for (Index
= 0; Index
< Count
; Index
++) {
274 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, *ValueBufferPtr
--, Increment
);
275 if ((Flags
& COMMA_TYPE
) != 0) {
279 if ((Index
+ 1) < Count
) {
280 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, ',', Increment
);
287 // Print Null-terminator
289 BasePrintLibFillBuffer (Buffer
, EndBuffer
+ Increment
, 1, 0, Increment
);
291 return ((Buffer
- OriginalBuffer
) / Increment
);
295 Internal function that converts a decimal value to a Null-terminated string.
297 Converts the decimal number specified by Value to a Null-terminated string
298 specified by Buffer containing at most Width characters. If Width is 0 then a
299 width of MAXIMUM_VALUE_CHARACTERS is assumed. If the conversion contains more
300 than Width characters, then only the first Width characters are placed in
301 Buffer. Additional conversion parameters are specified in Flags.
302 The Flags bit LEFT_JUSTIFY is always ignored.
303 All conversions are left justified in Buffer.
304 If Width is 0, PREFIX_ZERO is ignored in Flags.
305 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and
306 commas are inserted every 3rd digit starting from the right.
307 If Value is < 0, then the fist character in Buffer is a '-'.
308 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
309 then Buffer is padded with '0' characters so the combination of the optional
310 '-' sign character, '0' characters, digit characters for Value, and the
311 Null-terminator add up to Width characters.
313 If an error would be returned, the function will ASSERT().
315 @param Buffer The pointer to the output buffer for the produced
316 Null-terminated string.
317 @param BufferSize The size of Buffer in bytes, including the
319 @param Flags The bitmask of flags that specify left justification,
320 zero pad, and commas.
321 @param Value The 64-bit signed value to convert to a string.
322 @param Width The maximum number of characters to place in Buffer,
323 not including the Null-terminator.
324 @param Increment The character increment in Buffer.
326 @retval RETURN_SUCCESS The decimal value is converted.
327 @retval RETURN_BUFFER_TOO_SMALL If BufferSize cannot hold the converted
329 @retval RETURN_INVALID_PARAMETER If Buffer is NULL.
330 If Increment is 1 and
331 PcdMaximumAsciiStringLength is not zero,
332 BufferSize is greater than
333 PcdMaximumAsciiStringLength.
334 If Increment is not 1 and
335 PcdMaximumUnicodeStringLength is not zero,
336 BufferSize is greater than
337 (PcdMaximumUnicodeStringLength *
338 sizeof (CHAR16) + 1).
339 If unsupported bits are set in Flags.
340 If both COMMA_TYPE and RADIX_HEX are set in
342 If Width >= MAXIMUM_VALUE_CHARACTERS.
346 BasePrintLibConvertValueToStringS (
347 IN OUT CHAR8
*Buffer
,
356 CHAR8 ValueBuffer
[MAXIMUM_VALUE_CHARACTERS
];
357 CHAR8
*ValueBufferPtr
;
364 // 1. Buffer shall not be a null pointer.
366 SAFE_PRINT_CONSTRAINT_CHECK ((Buffer
!= NULL
), RETURN_INVALID_PARAMETER
);
369 // 2. BufferSize shall not be greater than (RSIZE_MAX * sizeof (CHAR16)) for
370 // Unicode output string or shall not be greater than ASCII_RSIZE_MAX for
371 // Ascii output string.
373 if (Increment
== 1) {
375 // Ascii output string
377 if (ASCII_RSIZE_MAX
!= 0) {
378 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize
<= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
382 // Unicode output string
384 if (RSIZE_MAX
!= 0) {
385 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize
<= RSIZE_MAX
* sizeof (CHAR16
) + 1), RETURN_INVALID_PARAMETER
);
390 // 3. Flags shall be set properly.
392 SAFE_PRINT_CONSTRAINT_CHECK (((Flags
& ~(LEFT_JUSTIFY
| COMMA_TYPE
| PREFIX_ZERO
| RADIX_HEX
)) == 0), RETURN_INVALID_PARAMETER
);
393 SAFE_PRINT_CONSTRAINT_CHECK ((((Flags
& COMMA_TYPE
) == 0) || ((Flags
& RADIX_HEX
) == 0)), RETURN_INVALID_PARAMETER
);
396 // 4. Width shall be smaller than MAXIMUM_VALUE_CHARACTERS.
398 SAFE_PRINT_CONSTRAINT_CHECK ((Width
< MAXIMUM_VALUE_CHARACTERS
), RETURN_INVALID_PARAMETER
);
401 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.
403 if (Width
== 0 || (Flags
& COMMA_TYPE
) != 0) {
404 Flags
&= ~((UINTN
) PREFIX_ZERO
);
407 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
410 Width
= MAXIMUM_VALUE_CHARACTERS
- 1;
414 // Count the characters of the output string.
417 Radix
= ((Flags
& RADIX_HEX
) == 0)? 10 : 16;
419 if ((Flags
& PREFIX_ZERO
) != 0) {
422 if ((Value
< 0) && ((Flags
& RADIX_HEX
) == 0)) {
423 Count
++; // minus sign
424 ValueBufferPtr
= BasePrintLibValueToString (ValueBuffer
, -Value
, Radix
);
426 ValueBufferPtr
= BasePrintLibValueToString (ValueBuffer
, Value
, Radix
);
428 Digits
= ValueBufferPtr
- ValueBuffer
;
431 if ((Flags
& COMMA_TYPE
) != 0) {
432 Count
+= (Digits
- 1) / 3; // commas
436 Width
= MIN (Count
, Width
);
439 // 5. BufferSize shall be large enough to hold the converted string.
441 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize
>= (Width
+ 1) * Increment
), RETURN_BUFFER_TOO_SMALL
);
444 // Set the tag for the end of the input Buffer.
446 EndBuffer
= Buffer
+ Width
* Increment
;
449 // Convert decimal negative
451 if ((Value
< 0) && ((Flags
& RADIX_HEX
) == 0)) {
453 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, '-', Increment
);
458 // Count the length of the value string.
460 ValueBufferPtr
= BasePrintLibValueToString (ValueBuffer
, Value
, Radix
);
461 Count
= ValueBufferPtr
- ValueBuffer
;
466 if ((Flags
& PREFIX_ZERO
) != 0) {
467 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, Width
- Count
, '0', Increment
);
471 // Print Comma type for every 3 characters
477 for (Index
= 0; Index
< Count
; Index
++) {
478 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, *ValueBufferPtr
--, Increment
);
479 if ((Flags
& COMMA_TYPE
) != 0) {
483 if ((Index
+ 1) < Count
) {
484 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, ',', Increment
);
491 // Print Null-terminator
493 BasePrintLibFillBuffer (Buffer
, EndBuffer
+ Increment
, 1, 0, Increment
);
495 return RETURN_SUCCESS
;
499 Worker function that produces a Null-terminated string in an output buffer
500 based on a Null-terminated format string and a VA_LIST argument list.
502 VSPrint function to process format and place the results in Buffer. Since a
503 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
504 this is the main print working routine.
506 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.
508 @param[out] Buffer The character buffer to print the results of the
509 parsing of Format into.
510 @param[in] BufferSize The maximum number of characters to put into
512 @param[in] Flags Initial flags value.
513 Can only have FORMAT_UNICODE, OUTPUT_UNICODE,
514 and COUNT_ONLY_NO_PRINT set.
515 @param[in] Format A Null-terminated format string.
516 @param[in] VaListMarker VA_LIST style variable argument list consumed by
518 @param[in] BaseListMarker BASE_LIST style variable argument list consumed
519 by processing Format.
521 @return The number of characters printed not including the Null-terminator.
522 If COUNT_ONLY_NO_PRINT was set returns the same, but without any
523 modification to Buffer.
527 BasePrintLibSPrintMarker (
531 IN CONST CHAR8
*Format
,
532 IN VA_LIST VaListMarker
, OPTIONAL
533 IN BASE_LIST BaseListMarker OPTIONAL
536 CHAR8
*OriginalBuffer
;
538 CHAR8 ValueBuffer
[MAXIMUM_VALUE_CHARACTERS
];
539 UINT32 BytesPerOutputCharacter
;
540 UINTN BytesPerFormatCharacter
;
542 UINTN FormatCharacter
;
546 CONST CHAR8
*ArgumentString
;
552 INTN BytesPerArgumentCharacter
;
553 UINTN ArgumentCharacter
;
561 RETURN_STATUS Status
;
565 UINTN LengthToReturn
;
568 // If you change this code be sure to match the 2 versions of this function.
569 // Nearly identical logic is found in the BasePrintLib and
570 // DxePrintLibPrint2Protocol (both PrintLib instances).
574 // 1. Buffer shall not be a null pointer when both BufferSize > 0 and
575 // COUNT_ONLY_NO_PRINT is not set in Flags.
577 if ((BufferSize
> 0) && ((Flags
& COUNT_ONLY_NO_PRINT
) == 0)) {
578 SAFE_PRINT_CONSTRAINT_CHECK ((Buffer
!= NULL
), 0);
582 // 2. Format shall not be a null pointer when BufferSize > 0 or when
583 // COUNT_ONLY_NO_PRINT is set in Flags.
585 if ((BufferSize
> 0) || ((Flags
& COUNT_ONLY_NO_PRINT
) != 0)) {
586 SAFE_PRINT_CONSTRAINT_CHECK ((Format
!= NULL
), 0);
590 // 3. BufferSize shall not be greater than RSIZE_MAX for Unicode output or
591 // ASCII_RSIZE_MAX for Ascii output.
593 if ((Flags
& OUTPUT_UNICODE
) != 0) {
594 if (RSIZE_MAX
!= 0) {
595 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize
<= RSIZE_MAX
), 0);
597 BytesPerOutputCharacter
= 2;
599 if (ASCII_RSIZE_MAX
!= 0) {
600 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize
<= ASCII_RSIZE_MAX
), 0);
602 BytesPerOutputCharacter
= 1;
606 // 4. Format shall not contain more than RSIZE_MAX Unicode characters or
607 // ASCII_RSIZE_MAX Ascii characters.
609 if ((Flags
& FORMAT_UNICODE
) != 0) {
610 if (RSIZE_MAX
!= 0) {
611 SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16
*)Format
, RSIZE_MAX
+ 1) <= RSIZE_MAX
), 0);
613 BytesPerFormatCharacter
= 2;
616 if (ASCII_RSIZE_MAX
!= 0) {
617 SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format
, ASCII_RSIZE_MAX
+ 1) <= ASCII_RSIZE_MAX
), 0);
619 BytesPerFormatCharacter
= 1;
623 if ((Flags
& COUNT_ONLY_NO_PRINT
) != 0) {
624 if (BufferSize
== 0) {
629 // We can run without a Buffer for counting only.
631 if (BufferSize
== 0) {
638 OriginalBuffer
= NULL
;
641 // Reserve space for the Null terminator.
643 if (Buffer
!= NULL
) {
645 OriginalBuffer
= Buffer
;
648 // Set the tag for the end of the input Buffer.
650 EndBuffer
= Buffer
+ BufferSize
* BytesPerOutputCharacter
;
654 // Get the first character from the format string
656 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
659 // Loop until the end of the format string is reached or the output buffer is full
661 while (FormatCharacter
!= 0) {
662 if ((Buffer
!= NULL
) && (Buffer
>= EndBuffer
)) {
666 // Clear all the flag bits except those that may have been passed in
668 Flags
&= (UINTN
) (OUTPUT_UNICODE
| FORMAT_UNICODE
| COUNT_ONLY_NO_PRINT
);
671 // Set the default width to zero, and the default precision to 1
681 switch (FormatCharacter
) {
684 // Parse Flags and Width
686 for (Done
= FALSE
; !Done
; ) {
687 Format
+= BytesPerFormatCharacter
;
688 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
689 switch (FormatCharacter
) {
694 Flags
|= LEFT_JUSTIFY
;
697 Flags
|= PREFIX_SIGN
;
700 Flags
|= PREFIX_BLANK
;
710 if ((Flags
& PRECISION
) == 0) {
711 Flags
|= PAD_TO_WIDTH
;
712 if (BaseListMarker
== NULL
) {
713 Width
= VA_ARG (VaListMarker
, UINTN
);
715 Width
= BASE_ARG (BaseListMarker
, UINTN
);
718 if (BaseListMarker
== NULL
) {
719 Precision
= VA_ARG (VaListMarker
, UINTN
);
721 Precision
= BASE_ARG (BaseListMarker
, UINTN
);
726 if ((Flags
& PRECISION
) == 0) {
727 Flags
|= PREFIX_ZERO
;
738 for (Count
= 0; ((FormatCharacter
>= '0') && (FormatCharacter
<= '9')); ){
739 Count
= (Count
* 10) + FormatCharacter
- '0';
740 Format
+= BytesPerFormatCharacter
;
741 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
743 Format
-= BytesPerFormatCharacter
;
744 if ((Flags
& PRECISION
) == 0) {
745 Flags
|= PAD_TO_WIDTH
;
754 // Make no output if Format string terminates unexpectedly when
755 // looking up for flag, width, precision and type.
757 Format
-= BytesPerFormatCharacter
;
760 // break skipped on purpose.
769 // Handle each argument type
771 switch (FormatCharacter
) {
774 // Flag space, +, 0, L & l are invalid for type p.
776 Flags
&= ~((UINTN
) (PREFIX_BLANK
| PREFIX_SIGN
| PREFIX_ZERO
| LONG_TYPE
));
777 if (sizeof (VOID
*) > 4) {
781 // break skipped on purpose
784 Flags
|= PREFIX_ZERO
;
786 // break skipped on purpose
791 // break skipped on purpose
794 if ((Flags
& RADIX_HEX
) == 0) {
795 Flags
&= ~((UINTN
) (PREFIX_SIGN
));
796 Flags
|= UNSIGNED_TYPE
;
799 // break skipped on purpose
802 if ((Flags
& LONG_TYPE
) == 0) {
804 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
805 // This assumption is made so the format string definition is compatible with the ANSI C
806 // Specification for formatted strings. It is recommended that the Base Types be used
807 // everywhere, but in this one case, compliance with ANSI C is more important, and
808 // provides an implementation that is compatible with that largest possible set of CPU
809 // architectures. This is why the type "int" is used in this one case.
811 if (BaseListMarker
== NULL
) {
812 Value
= VA_ARG (VaListMarker
, int);
814 Value
= BASE_ARG (BaseListMarker
, int);
817 if (BaseListMarker
== NULL
) {
818 Value
= VA_ARG (VaListMarker
, INT64
);
820 Value
= BASE_ARG (BaseListMarker
, INT64
);
823 if ((Flags
& PREFIX_BLANK
) != 0) {
826 if ((Flags
& PREFIX_SIGN
) != 0) {
829 if ((Flags
& COMMA_TYPE
) != 0) {
832 if ((Flags
& RADIX_HEX
) == 0) {
835 Flags
&= ~((UINTN
) PREFIX_ZERO
);
838 if (Value
< 0 && (Flags
& UNSIGNED_TYPE
) == 0) {
839 Flags
|= PREFIX_SIGN
;
842 } else if ((Flags
& UNSIGNED_TYPE
) != 0 && (Flags
& LONG_TYPE
) == 0) {
844 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
845 // This assumption is made so the format string definition is compatible with the ANSI C
846 // Specification for formatted strings. It is recommended that the Base Types be used
847 // everywhere, but in this one case, compliance with ANSI C is more important, and
848 // provides an implementation that is compatible with that largest possible set of CPU
849 // architectures. This is why the type "unsigned int" is used in this one case.
851 Value
= (unsigned int)Value
;
856 if ((Flags
& LONG_TYPE
) == 0 && Value
< 0) {
858 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
859 // This assumption is made so the format string definition is compatible with the ANSI C
860 // Specification for formatted strings. It is recommended that the Base Types be used
861 // everywhere, but in this one case, compliance with ANSI C is more important, and
862 // provides an implementation that is compatible with that largest possible set of CPU
863 // architectures. This is why the type "unsigned int" is used in this one case.
865 Value
= (unsigned int)Value
;
869 // Convert Value to a reversed string
871 Count
= BasePrintLibValueToString (ValueBuffer
, Value
, Radix
) - ValueBuffer
;
872 if (Value
== 0 && Precision
== 0) {
875 ArgumentString
= (CHAR8
*)ValueBuffer
+ Count
;
881 if (Comma
&& Count
!= 0) {
882 Count
+= ((Count
- 1) / 3);
888 Flags
|= ARGUMENT_REVERSED
;
890 if ((Flags
& PREFIX_ZERO
) != 0) {
891 if ((Flags
& LEFT_JUSTIFY
) == 0) {
892 if ((Flags
& PAD_TO_WIDTH
) != 0) {
893 if ((Flags
& PRECISION
) == 0) {
903 Flags
|= ARGUMENT_UNICODE
;
905 // break skipped on purpose
908 if (BaseListMarker
== NULL
) {
909 ArgumentString
= VA_ARG (VaListMarker
, CHAR8
*);
911 ArgumentString
= BASE_ARG (BaseListMarker
, CHAR8
*);
913 if (ArgumentString
== NULL
) {
914 Flags
&= ~((UINTN
) ARGUMENT_UNICODE
);
915 ArgumentString
= "<null string>";
918 // Set the default precision for string to be zero if not specified.
920 if ((Flags
& PRECISION
) == 0) {
926 if (BaseListMarker
== NULL
) {
927 Character
= VA_ARG (VaListMarker
, UINTN
) & 0xffff;
929 Character
= BASE_ARG (BaseListMarker
, UINTN
) & 0xffff;
931 ArgumentString
= (CHAR8
*)&Character
;
932 Flags
|= ARGUMENT_UNICODE
;
936 if (BaseListMarker
== NULL
) {
937 TmpGuid
= VA_ARG (VaListMarker
, GUID
*);
939 TmpGuid
= BASE_ARG (BaseListMarker
, GUID
*);
941 if (TmpGuid
== NULL
) {
942 ArgumentString
= "<null guid>";
944 GuidData1
= ReadUnaligned32 (&(TmpGuid
->Data1
));
945 GuidData2
= ReadUnaligned16 (&(TmpGuid
->Data2
));
946 GuidData3
= ReadUnaligned16 (&(TmpGuid
->Data3
));
949 MAXIMUM_VALUE_CHARACTERS
,
951 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
964 ArgumentString
= ValueBuffer
;
969 if (BaseListMarker
== NULL
) {
970 TmpTime
= VA_ARG (VaListMarker
, TIME
*);
972 TmpTime
= BASE_ARG (BaseListMarker
, TIME
*);
974 if (TmpTime
== NULL
) {
975 ArgumentString
= "<null time>";
979 MAXIMUM_VALUE_CHARACTERS
,
981 "%02d/%02d/%04d %02d:%02d",
988 ArgumentString
= ValueBuffer
;
993 if (BaseListMarker
== NULL
) {
994 Status
= VA_ARG (VaListMarker
, RETURN_STATUS
);
996 Status
= BASE_ARG (BaseListMarker
, RETURN_STATUS
);
998 ArgumentString
= ValueBuffer
;
999 if (RETURN_ERROR (Status
)) {
1003 Index
= Status
& ~MAX_BIT
;
1004 if (Index
> 0 && Index
<= ERROR_STATUS_NUMBER
) {
1005 ArgumentString
= mStatusString
[Index
+ WARNING_STATUS_NUMBER
];
1009 if (Index
<= WARNING_STATUS_NUMBER
) {
1010 ArgumentString
= mStatusString
[Index
];
1013 if (ArgumentString
== ValueBuffer
) {
1014 BasePrintLibSPrint ((CHAR8
*) ValueBuffer
, MAXIMUM_VALUE_CHARACTERS
, 0, "%08X", Status
);
1019 Format
+= BytesPerFormatCharacter
;
1020 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
1021 if (FormatCharacter
== '\n') {
1023 // Translate '\r\n' to '\r\n'
1025 ArgumentString
= "\r\n";
1028 // Translate '\r' to '\r'
1030 ArgumentString
= "\r";
1031 Format
-= BytesPerFormatCharacter
;
1037 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1039 ArgumentString
= "\r\n";
1040 Format
+= BytesPerFormatCharacter
;
1041 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
1042 if (FormatCharacter
!= '\r') {
1043 Format
-= BytesPerFormatCharacter
;
1050 // if the type is '%' or unknown, then print it to the screen
1052 ArgumentString
= (CHAR8
*)&FormatCharacter
;
1053 Flags
|= ARGUMENT_UNICODE
;
1059 Format
+= BytesPerFormatCharacter
;
1060 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
1061 if (FormatCharacter
== '\n') {
1063 // Translate '\r\n' to '\r\n'
1065 ArgumentString
= "\r\n";
1068 // Translate '\r' to '\r'
1070 ArgumentString
= "\r";
1071 Format
-= BytesPerFormatCharacter
;
1077 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1079 ArgumentString
= "\r\n";
1080 Format
+= BytesPerFormatCharacter
;
1081 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
1082 if (FormatCharacter
!= '\r') {
1083 Format
-= BytesPerFormatCharacter
;
1088 ArgumentString
= (CHAR8
*)&FormatCharacter
;
1089 Flags
|= ARGUMENT_UNICODE
;
1094 // Retrieve the ArgumentString attriubutes
1096 if ((Flags
& ARGUMENT_UNICODE
) != 0) {
1097 ArgumentMask
= 0xffff;
1098 BytesPerArgumentCharacter
= 2;
1100 ArgumentMask
= 0xff;
1101 BytesPerArgumentCharacter
= 1;
1103 if ((Flags
& ARGUMENT_REVERSED
) != 0) {
1104 BytesPerArgumentCharacter
= -BytesPerArgumentCharacter
;
1107 // Compute the number of characters in ArgumentString and store it in Count
1108 // ArgumentString is either null-terminated, or it contains Precision characters
1111 (ArgumentString
[Count
* BytesPerArgumentCharacter
] != '\0' ||
1112 (BytesPerArgumentCharacter
> 1 &&
1113 ArgumentString
[Count
* BytesPerArgumentCharacter
+ 1]!= '\0')) &&
1114 (Count
< Precision
|| ((Flags
& PRECISION
) == 0));
1116 ArgumentCharacter
= ((ArgumentString
[Count
* BytesPerArgumentCharacter
] & 0xff) | ((ArgumentString
[Count
* BytesPerArgumentCharacter
+ 1]) << 8)) & ArgumentMask
;
1117 if (ArgumentCharacter
== 0) {
1123 if (Precision
< Count
) {
1128 // Pad before the string
1130 if ((Flags
& (PAD_TO_WIDTH
| LEFT_JUSTIFY
)) == (PAD_TO_WIDTH
)) {
1131 LengthToReturn
+= ((Width
- Precision
) * BytesPerOutputCharacter
);
1132 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1133 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, Width
- Precision
, ' ', BytesPerOutputCharacter
);
1139 LengthToReturn
+= (1 * BytesPerOutputCharacter
);
1140 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1141 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, Prefix
, BytesPerOutputCharacter
);
1144 LengthToReturn
+= ((Precision
- Count
) * BytesPerOutputCharacter
);
1145 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1146 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, Precision
- Count
, '0', BytesPerOutputCharacter
);
1149 LengthToReturn
+= ((Precision
- Count
) * BytesPerOutputCharacter
);
1150 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1151 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, Precision
- Count
, ' ', BytesPerOutputCharacter
);
1154 LengthToReturn
+= (1 * BytesPerOutputCharacter
);
1155 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1156 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, Prefix
, BytesPerOutputCharacter
);
1162 // Output the Prefix character if it is present
1170 // Copy the string into the output buffer performing the required type conversions
1172 while (Index
< Count
&&
1173 (ArgumentString
[0] != '\0' ||
1174 (BytesPerArgumentCharacter
> 1 && ArgumentString
[1] != '\0'))) {
1175 ArgumentCharacter
= ((*ArgumentString
& 0xff) | (((UINT8
)*(ArgumentString
+ 1)) << 8)) & ArgumentMask
;
1177 LengthToReturn
+= (1 * BytesPerOutputCharacter
);
1178 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1179 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, ArgumentCharacter
, BytesPerOutputCharacter
);
1181 ArgumentString
+= BytesPerArgumentCharacter
;
1188 if (Index
< Count
) {
1189 LengthToReturn
+= (1 * BytesPerOutputCharacter
);
1190 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1191 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, 1, ',', BytesPerOutputCharacter
);
1199 // Pad after the string
1201 if ((Flags
& (PAD_TO_WIDTH
| LEFT_JUSTIFY
)) == (PAD_TO_WIDTH
| LEFT_JUSTIFY
)) {
1202 LengthToReturn
+= ((Width
- Precision
) * BytesPerOutputCharacter
);
1203 if ((Flags
& COUNT_ONLY_NO_PRINT
) == 0 && Buffer
!= NULL
) {
1204 Buffer
= BasePrintLibFillBuffer (Buffer
, EndBuffer
, Width
- Precision
, ' ', BytesPerOutputCharacter
);
1209 // Get the next character from the format string
1211 Format
+= BytesPerFormatCharacter
;
1214 // Get the next character from the format string
1216 FormatCharacter
= ((*Format
& 0xff) | ((BytesPerFormatCharacter
== 1) ? 0 : (*(Format
+ 1) << 8))) & FormatMask
;
1219 if ((Flags
& COUNT_ONLY_NO_PRINT
) != 0) {
1220 return (LengthToReturn
/ BytesPerOutputCharacter
);
1223 ASSERT (Buffer
!= NULL
);
1225 // Null terminate the Unicode or ASCII string
1227 BasePrintLibFillBuffer (Buffer
, EndBuffer
+ BytesPerOutputCharacter
, 1, 0, BytesPerOutputCharacter
);
1229 return ((Buffer
- OriginalBuffer
) / BytesPerOutputCharacter
);
1233 Worker function that produces a Null-terminated string in an output buffer
1234 based on a Null-terminated format string and variable argument list.
1236 VSPrint function to process format and place the results in Buffer. Since a
1237 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1238 this is the main print working routine
1240 @param StartOfBuffer The character buffer to print the results of the parsing
1242 @param BufferSize The maximum number of characters to put into buffer.
1243 Zero means no limit.
1244 @param Flags Initial flags value.
1245 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set
1246 @param FormatString A Null-terminated format string.
1247 @param ... The variable argument list.
1249 @return The number of characters printed.
1254 BasePrintLibSPrint (
1255 OUT CHAR8
*StartOfBuffer
,
1256 IN UINTN BufferSize
,
1258 IN CONST CHAR8
*FormatString
,
1263 UINTN NumberOfPrinted
;
1265 VA_START (Marker
, FormatString
);
1266 NumberOfPrinted
= BasePrintLibSPrintMarker (StartOfBuffer
, BufferSize
, Flags
, FormatString
, Marker
, NULL
);
1268 return NumberOfPrinted
;