MdePkg/BasePrintLib: Add safe print functions [A|U]ValueToStringS
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLibInternal.c
1 /** @file
2 Print Library internal worker functions.
3
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.
9
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.
12
13 **/
14
15 #include "PrintLibInternal.h"
16
17 #define WARNING_STATUS_NUMBER 5
18 #define ERROR_STATUS_NUMBER 33
19
20 //
21 // Safe print checks
22 //
23 #define RSIZE_MAX (PcdGet32 (PcdMaximumUnicodeStringLength))
24 #define ASCII_RSIZE_MAX (PcdGet32 (PcdMaximumAsciiStringLength))
25
26 #define SAFE_PRINT_CONSTRAINT_CHECK(Expression, RetVal) \
27 do { \
28 ASSERT (Expression); \
29 if (!(Expression)) { \
30 return RetVal; \
31 } \
32 } while (FALSE)
33
34 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
35
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
76 };
77
78
79 /**
80 Internal function that places the character into the Buffer.
81
82 Internal function that places ASCII or Unicode character into the Buffer.
83
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
86 placed after that.
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.
91
92 @return Buffer.
93
94 **/
95 CHAR8 *
96 BasePrintLibFillBuffer (
97 OUT CHAR8 *Buffer,
98 IN CHAR8 *EndBuffer,
99 IN INTN Length,
100 IN UINTN Character,
101 IN INTN Increment
102 )
103 {
104 INTN Index;
105
106 for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {
107 *Buffer = (CHAR8) Character;
108 if (Increment != 1) {
109 *(Buffer + 1) = (CHAR8)(Character >> 8);
110 }
111 Buffer += Increment;
112 }
113
114 return Buffer;
115 }
116
117 /**
118 Internal function that convert a number to a string in Buffer.
119
120 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.
121
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
125
126 @return A pointer to the end of buffer filled with ASCII string.
127
128 **/
129 CHAR8 *
130 BasePrintLibValueToString (
131 IN OUT CHAR8 *Buffer,
132 IN INT64 Value,
133 IN UINTN Radix
134 )
135 {
136 UINT32 Remainder;
137
138 //
139 // Loop to convert one digit at a time in reverse order
140 //
141 *Buffer = 0;
142 do {
143 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);
144 *(++Buffer) = mHexStr[Remainder];
145 } while (Value != 0);
146
147 //
148 // Return pointer of the end of filled buffer.
149 //
150 return Buffer;
151 }
152
153 /**
154 Internal function that converts a decimal value to a Null-terminated string.
155
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.
174
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()
178
179 @param Buffer The pointer to the output buffer for the produced Null-terminated
180 string.
181 @param Flags The bitmask of flags that specify left justification, zero pad,
182 and commas.
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
185 the Null-terminator.
186 @param Increment The character increment in Buffer.
187
188 @return Total number of characters required to perform the conversion.
189
190 **/
191 UINTN
192 BasePrintLibConvertValueToString (
193 IN OUT CHAR8 *Buffer,
194 IN UINTN Flags,
195 IN INT64 Value,
196 IN UINTN Width,
197 IN UINTN Increment
198 )
199 {
200 CHAR8 *OriginalBuffer;
201 CHAR8 *EndBuffer;
202 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
203 CHAR8 *ValueBufferPtr;
204 UINTN Count;
205 UINTN Digits;
206 UINTN Index;
207 UINTN Radix;
208
209 //
210 // Make sure Buffer is not NULL and Width < MAXIMUM
211 //
212 ASSERT (Buffer != NULL);
213 ASSERT (Width < MAXIMUM_VALUE_CHARACTERS);
214 //
215 // Make sure Flags can only contain supported bits.
216 //
217 ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0);
218
219 //
220 // If both COMMA_TYPE and RADIX_HEX are set, then ASSERT ()
221 //
222 ASSERT (((Flags & COMMA_TYPE) == 0) || ((Flags & RADIX_HEX) == 0));
223
224 OriginalBuffer = Buffer;
225
226 //
227 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.
228 //
229 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {
230 Flags &= ~((UINTN) PREFIX_ZERO);
231 }
232 //
233 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
234 //
235 if (Width == 0) {
236 Width = MAXIMUM_VALUE_CHARACTERS - 1;
237 }
238 //
239 // Set the tag for the end of the input Buffer.
240 //
241 EndBuffer = Buffer + Width * Increment;
242
243 //
244 // Convert decimal negative
245 //
246 if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {
247 Value = -Value;
248 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);
249 Width--;
250 }
251
252 //
253 // Count the length of the value string.
254 //
255 Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;
256 ValueBufferPtr = BasePrintLibValueToString (ValueBuffer, Value, Radix);
257 Count = ValueBufferPtr - ValueBuffer;
258
259 //
260 // Append Zero
261 //
262 if ((Flags & PREFIX_ZERO) != 0) {
263 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);
264 }
265
266 //
267 // Print Comma type for every 3 characters
268 //
269 Digits = Count % 3;
270 if (Digits != 0) {
271 Digits = 3 - Digits;
272 }
273 for (Index = 0; Index < Count; Index++) {
274 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, *ValueBufferPtr--, Increment);
275 if ((Flags & COMMA_TYPE) != 0) {
276 Digits++;
277 if (Digits == 3) {
278 Digits = 0;
279 if ((Index + 1) < Count) {
280 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);
281 }
282 }
283 }
284 }
285
286 //
287 // Print Null-terminator
288 //
289 BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);
290
291 return ((Buffer - OriginalBuffer) / Increment);
292 }
293
294 /**
295 Internal function that converts a decimal value to a Null-terminated string.
296
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.
312
313 If an error would be returned, the function will ASSERT().
314
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
318 Null-terminator.
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.
325
326 @retval RETURN_SUCCESS The decimal value is converted.
327 @retval RETURN_BUFFER_TOO_SMALL If BufferSize cannot hold the converted
328 value.
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
341 Flags.
342 If Width >= MAXIMUM_VALUE_CHARACTERS.
343
344 **/
345 RETURN_STATUS
346 BasePrintLibConvertValueToStringS (
347 IN OUT CHAR8 *Buffer,
348 IN UINTN BufferSize,
349 IN UINTN Flags,
350 IN INT64 Value,
351 IN UINTN Width,
352 IN UINTN Increment
353 )
354 {
355 CHAR8 *EndBuffer;
356 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
357 CHAR8 *ValueBufferPtr;
358 UINTN Count;
359 UINTN Digits;
360 UINTN Index;
361 UINTN Radix;
362
363 //
364 // 1. Buffer shall not be a null pointer.
365 //
366 SAFE_PRINT_CONSTRAINT_CHECK ((Buffer != NULL), RETURN_INVALID_PARAMETER);
367
368 //
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.
372 //
373 if (Increment == 1) {
374 //
375 // Ascii output string
376 //
377 if (ASCII_RSIZE_MAX != 0) {
378 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
379 }
380 } else {
381 //
382 // Unicode output string
383 //
384 if (RSIZE_MAX != 0) {
385 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= RSIZE_MAX * sizeof (CHAR16) + 1), RETURN_INVALID_PARAMETER);
386 }
387 }
388
389 //
390 // 3. Flags shall be set properly.
391 //
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);
394
395 //
396 // 4. Width shall be smaller than MAXIMUM_VALUE_CHARACTERS.
397 //
398 SAFE_PRINT_CONSTRAINT_CHECK ((Width < MAXIMUM_VALUE_CHARACTERS), RETURN_INVALID_PARAMETER);
399
400 //
401 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.
402 //
403 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {
404 Flags &= ~((UINTN) PREFIX_ZERO);
405 }
406 //
407 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
408 //
409 if (Width == 0) {
410 Width = MAXIMUM_VALUE_CHARACTERS - 1;
411 }
412
413 //
414 // Count the characters of the output string.
415 //
416 Count = 0;
417 Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;
418
419 if ((Flags & PREFIX_ZERO) != 0) {
420 Count = Width;
421 } else {
422 if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {
423 Count++; // minus sign
424 ValueBufferPtr = BasePrintLibValueToString (ValueBuffer, -Value, Radix);
425 } else {
426 ValueBufferPtr = BasePrintLibValueToString (ValueBuffer, Value, Radix);
427 }
428 Digits = ValueBufferPtr - ValueBuffer;
429 Count += Digits;
430
431 if ((Flags & COMMA_TYPE) != 0) {
432 Count += (Digits - 1) / 3; // commas
433 }
434 }
435
436 Width = MIN (Count, Width);
437
438 //
439 // 5. BufferSize shall be large enough to hold the converted string.
440 //
441 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize >= (Width + 1) * Increment), RETURN_BUFFER_TOO_SMALL);
442
443 //
444 // Set the tag for the end of the input Buffer.
445 //
446 EndBuffer = Buffer + Width * Increment;
447
448 //
449 // Convert decimal negative
450 //
451 if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {
452 Value = -Value;
453 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);
454 Width--;
455 }
456
457 //
458 // Count the length of the value string.
459 //
460 ValueBufferPtr = BasePrintLibValueToString (ValueBuffer, Value, Radix);
461 Count = ValueBufferPtr - ValueBuffer;
462
463 //
464 // Append Zero
465 //
466 if ((Flags & PREFIX_ZERO) != 0) {
467 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);
468 }
469
470 //
471 // Print Comma type for every 3 characters
472 //
473 Digits = Count % 3;
474 if (Digits != 0) {
475 Digits = 3 - Digits;
476 }
477 for (Index = 0; Index < Count; Index++) {
478 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, *ValueBufferPtr--, Increment);
479 if ((Flags & COMMA_TYPE) != 0) {
480 Digits++;
481 if (Digits == 3) {
482 Digits = 0;
483 if ((Index + 1) < Count) {
484 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);
485 }
486 }
487 }
488 }
489
490 //
491 // Print Null-terminator
492 //
493 BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);
494
495 return RETURN_SUCCESS;
496 }
497
498 /**
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.
501
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.
505
506 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.
507
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
511 buffer.
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
517 processing Format.
518 @param[in] BaseListMarker BASE_LIST style variable argument list consumed
519 by processing Format.
520
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.
524
525 **/
526 UINTN
527 BasePrintLibSPrintMarker (
528 OUT CHAR8 *Buffer,
529 IN UINTN BufferSize,
530 IN UINTN Flags,
531 IN CONST CHAR8 *Format,
532 IN VA_LIST VaListMarker, OPTIONAL
533 IN BASE_LIST BaseListMarker OPTIONAL
534 )
535 {
536 CHAR8 *OriginalBuffer;
537 CHAR8 *EndBuffer;
538 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
539 UINT32 BytesPerOutputCharacter;
540 UINTN BytesPerFormatCharacter;
541 UINTN FormatMask;
542 UINTN FormatCharacter;
543 UINTN Width;
544 UINTN Precision;
545 INT64 Value;
546 CONST CHAR8 *ArgumentString;
547 UINTN Character;
548 GUID *TmpGuid;
549 TIME *TmpTime;
550 UINTN Count;
551 UINTN ArgumentMask;
552 INTN BytesPerArgumentCharacter;
553 UINTN ArgumentCharacter;
554 BOOLEAN Done;
555 UINTN Index;
556 CHAR8 Prefix;
557 BOOLEAN ZeroPad;
558 BOOLEAN Comma;
559 UINTN Digits;
560 UINTN Radix;
561 RETURN_STATUS Status;
562 UINT32 GuidData1;
563 UINT16 GuidData2;
564 UINT16 GuidData3;
565 UINTN LengthToReturn;
566
567 //
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).
571 //
572
573 //
574 // 1. Buffer shall not be a null pointer when both BufferSize > 0 and
575 // COUNT_ONLY_NO_PRINT is not set in Flags.
576 //
577 if ((BufferSize > 0) && ((Flags & COUNT_ONLY_NO_PRINT) == 0)) {
578 SAFE_PRINT_CONSTRAINT_CHECK ((Buffer != NULL), 0);
579 }
580
581 //
582 // 2. Format shall not be a null pointer when BufferSize > 0 or when
583 // COUNT_ONLY_NO_PRINT is set in Flags.
584 //
585 if ((BufferSize > 0) || ((Flags & COUNT_ONLY_NO_PRINT) != 0)) {
586 SAFE_PRINT_CONSTRAINT_CHECK ((Format != NULL), 0);
587 }
588
589 //
590 // 3. BufferSize shall not be greater than RSIZE_MAX for Unicode output or
591 // ASCII_RSIZE_MAX for Ascii output.
592 //
593 if ((Flags & OUTPUT_UNICODE) != 0) {
594 if (RSIZE_MAX != 0) {
595 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= RSIZE_MAX), 0);
596 }
597 BytesPerOutputCharacter = 2;
598 } else {
599 if (ASCII_RSIZE_MAX != 0) {
600 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= ASCII_RSIZE_MAX), 0);
601 }
602 BytesPerOutputCharacter = 1;
603 }
604
605 //
606 // 4. Format shall not contain more than RSIZE_MAX Unicode characters or
607 // ASCII_RSIZE_MAX Ascii characters.
608 //
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);
612 }
613 BytesPerFormatCharacter = 2;
614 FormatMask = 0xffff;
615 } else {
616 if (ASCII_RSIZE_MAX != 0) {
617 SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), 0);
618 }
619 BytesPerFormatCharacter = 1;
620 FormatMask = 0xff;
621 }
622
623 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {
624 if (BufferSize == 0) {
625 Buffer = NULL;
626 }
627 } else {
628 //
629 // We can run without a Buffer for counting only.
630 //
631 if (BufferSize == 0) {
632 return 0;
633 }
634 }
635
636 LengthToReturn = 0;
637 EndBuffer = NULL;
638 OriginalBuffer = NULL;
639
640 //
641 // Reserve space for the Null terminator.
642 //
643 if (Buffer != NULL) {
644 BufferSize--;
645 OriginalBuffer = Buffer;
646
647 //
648 // Set the tag for the end of the input Buffer.
649 //
650 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;
651 }
652
653 //
654 // Get the first character from the format string
655 //
656 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
657
658 //
659 // Loop until the end of the format string is reached or the output buffer is full
660 //
661 while (FormatCharacter != 0) {
662 if ((Buffer != NULL) && (Buffer >= EndBuffer)) {
663 break;
664 }
665 //
666 // Clear all the flag bits except those that may have been passed in
667 //
668 Flags &= (UINTN) (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);
669
670 //
671 // Set the default width to zero, and the default precision to 1
672 //
673 Width = 0;
674 Precision = 1;
675 Prefix = 0;
676 Comma = FALSE;
677 ZeroPad = FALSE;
678 Count = 0;
679 Digits = 0;
680
681 switch (FormatCharacter) {
682 case '%':
683 //
684 // Parse Flags and Width
685 //
686 for (Done = FALSE; !Done; ) {
687 Format += BytesPerFormatCharacter;
688 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
689 switch (FormatCharacter) {
690 case '.':
691 Flags |= PRECISION;
692 break;
693 case '-':
694 Flags |= LEFT_JUSTIFY;
695 break;
696 case '+':
697 Flags |= PREFIX_SIGN;
698 break;
699 case ' ':
700 Flags |= PREFIX_BLANK;
701 break;
702 case ',':
703 Flags |= COMMA_TYPE;
704 break;
705 case 'L':
706 case 'l':
707 Flags |= LONG_TYPE;
708 break;
709 case '*':
710 if ((Flags & PRECISION) == 0) {
711 Flags |= PAD_TO_WIDTH;
712 if (BaseListMarker == NULL) {
713 Width = VA_ARG (VaListMarker, UINTN);
714 } else {
715 Width = BASE_ARG (BaseListMarker, UINTN);
716 }
717 } else {
718 if (BaseListMarker == NULL) {
719 Precision = VA_ARG (VaListMarker, UINTN);
720 } else {
721 Precision = BASE_ARG (BaseListMarker, UINTN);
722 }
723 }
724 break;
725 case '0':
726 if ((Flags & PRECISION) == 0) {
727 Flags |= PREFIX_ZERO;
728 }
729 case '1':
730 case '2':
731 case '3':
732 case '4':
733 case '5':
734 case '6':
735 case '7':
736 case '8':
737 case '9':
738 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){
739 Count = (Count * 10) + FormatCharacter - '0';
740 Format += BytesPerFormatCharacter;
741 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
742 }
743 Format -= BytesPerFormatCharacter;
744 if ((Flags & PRECISION) == 0) {
745 Flags |= PAD_TO_WIDTH;
746 Width = Count;
747 } else {
748 Precision = Count;
749 }
750 break;
751
752 case '\0':
753 //
754 // Make no output if Format string terminates unexpectedly when
755 // looking up for flag, width, precision and type.
756 //
757 Format -= BytesPerFormatCharacter;
758 Precision = 0;
759 //
760 // break skipped on purpose.
761 //
762 default:
763 Done = TRUE;
764 break;
765 }
766 }
767
768 //
769 // Handle each argument type
770 //
771 switch (FormatCharacter) {
772 case 'p':
773 //
774 // Flag space, +, 0, L & l are invalid for type p.
775 //
776 Flags &= ~((UINTN) (PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE));
777 if (sizeof (VOID *) > 4) {
778 Flags |= LONG_TYPE;
779 }
780 //
781 // break skipped on purpose
782 //
783 case 'X':
784 Flags |= PREFIX_ZERO;
785 //
786 // break skipped on purpose
787 //
788 case 'x':
789 Flags |= RADIX_HEX;
790 //
791 // break skipped on purpose
792 //
793 case 'u':
794 if ((Flags & RADIX_HEX) == 0) {
795 Flags &= ~((UINTN) (PREFIX_SIGN));
796 Flags |= UNSIGNED_TYPE;
797 }
798 //
799 // break skipped on purpose
800 //
801 case 'd':
802 if ((Flags & LONG_TYPE) == 0) {
803 //
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.
810 //
811 if (BaseListMarker == NULL) {
812 Value = VA_ARG (VaListMarker, int);
813 } else {
814 Value = BASE_ARG (BaseListMarker, int);
815 }
816 } else {
817 if (BaseListMarker == NULL) {
818 Value = VA_ARG (VaListMarker, INT64);
819 } else {
820 Value = BASE_ARG (BaseListMarker, INT64);
821 }
822 }
823 if ((Flags & PREFIX_BLANK) != 0) {
824 Prefix = ' ';
825 }
826 if ((Flags & PREFIX_SIGN) != 0) {
827 Prefix = '+';
828 }
829 if ((Flags & COMMA_TYPE) != 0) {
830 Comma = TRUE;
831 }
832 if ((Flags & RADIX_HEX) == 0) {
833 Radix = 10;
834 if (Comma) {
835 Flags &= ~((UINTN) PREFIX_ZERO);
836 Precision = 1;
837 }
838 if (Value < 0 && (Flags & UNSIGNED_TYPE) == 0) {
839 Flags |= PREFIX_SIGN;
840 Prefix = '-';
841 Value = -Value;
842 } else if ((Flags & UNSIGNED_TYPE) != 0 && (Flags & LONG_TYPE) == 0) {
843 //
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.
850 //
851 Value = (unsigned int)Value;
852 }
853 } else {
854 Radix = 16;
855 Comma = FALSE;
856 if ((Flags & LONG_TYPE) == 0 && Value < 0) {
857 //
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.
864 //
865 Value = (unsigned int)Value;
866 }
867 }
868 //
869 // Convert Value to a reversed string
870 //
871 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;
872 if (Value == 0 && Precision == 0) {
873 Count = 0;
874 }
875 ArgumentString = (CHAR8 *)ValueBuffer + Count;
876
877 Digits = Count % 3;
878 if (Digits != 0) {
879 Digits = 3 - Digits;
880 }
881 if (Comma && Count != 0) {
882 Count += ((Count - 1) / 3);
883 }
884 if (Prefix != 0) {
885 Count++;
886 Precision++;
887 }
888 Flags |= ARGUMENT_REVERSED;
889 ZeroPad = TRUE;
890 if ((Flags & PREFIX_ZERO) != 0) {
891 if ((Flags & LEFT_JUSTIFY) == 0) {
892 if ((Flags & PAD_TO_WIDTH) != 0) {
893 if ((Flags & PRECISION) == 0) {
894 Precision = Width;
895 }
896 }
897 }
898 }
899 break;
900
901 case 's':
902 case 'S':
903 Flags |= ARGUMENT_UNICODE;
904 //
905 // break skipped on purpose
906 //
907 case 'a':
908 if (BaseListMarker == NULL) {
909 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);
910 } else {
911 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);
912 }
913 if (ArgumentString == NULL) {
914 Flags &= ~((UINTN) ARGUMENT_UNICODE);
915 ArgumentString = "<null string>";
916 }
917 //
918 // Set the default precision for string to be zero if not specified.
919 //
920 if ((Flags & PRECISION) == 0) {
921 Precision = 0;
922 }
923 break;
924
925 case 'c':
926 if (BaseListMarker == NULL) {
927 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;
928 } else {
929 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;
930 }
931 ArgumentString = (CHAR8 *)&Character;
932 Flags |= ARGUMENT_UNICODE;
933 break;
934
935 case 'g':
936 if (BaseListMarker == NULL) {
937 TmpGuid = VA_ARG (VaListMarker, GUID *);
938 } else {
939 TmpGuid = BASE_ARG (BaseListMarker, GUID *);
940 }
941 if (TmpGuid == NULL) {
942 ArgumentString = "<null guid>";
943 } else {
944 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));
945 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));
946 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));
947 BasePrintLibSPrint (
948 ValueBuffer,
949 MAXIMUM_VALUE_CHARACTERS,
950 0,
951 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
952 GuidData1,
953 GuidData2,
954 GuidData3,
955 TmpGuid->Data4[0],
956 TmpGuid->Data4[1],
957 TmpGuid->Data4[2],
958 TmpGuid->Data4[3],
959 TmpGuid->Data4[4],
960 TmpGuid->Data4[5],
961 TmpGuid->Data4[6],
962 TmpGuid->Data4[7]
963 );
964 ArgumentString = ValueBuffer;
965 }
966 break;
967
968 case 't':
969 if (BaseListMarker == NULL) {
970 TmpTime = VA_ARG (VaListMarker, TIME *);
971 } else {
972 TmpTime = BASE_ARG (BaseListMarker, TIME *);
973 }
974 if (TmpTime == NULL) {
975 ArgumentString = "<null time>";
976 } else {
977 BasePrintLibSPrint (
978 ValueBuffer,
979 MAXIMUM_VALUE_CHARACTERS,
980 0,
981 "%02d/%02d/%04d %02d:%02d",
982 TmpTime->Month,
983 TmpTime->Day,
984 TmpTime->Year,
985 TmpTime->Hour,
986 TmpTime->Minute
987 );
988 ArgumentString = ValueBuffer;
989 }
990 break;
991
992 case 'r':
993 if (BaseListMarker == NULL) {
994 Status = VA_ARG (VaListMarker, RETURN_STATUS);
995 } else {
996 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);
997 }
998 ArgumentString = ValueBuffer;
999 if (RETURN_ERROR (Status)) {
1000 //
1001 // Clear error bit
1002 //
1003 Index = Status & ~MAX_BIT;
1004 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {
1005 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];
1006 }
1007 } else {
1008 Index = Status;
1009 if (Index <= WARNING_STATUS_NUMBER) {
1010 ArgumentString = mStatusString [Index];
1011 }
1012 }
1013 if (ArgumentString == ValueBuffer) {
1014 BasePrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);
1015 }
1016 break;
1017
1018 case '\r':
1019 Format += BytesPerFormatCharacter;
1020 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1021 if (FormatCharacter == '\n') {
1022 //
1023 // Translate '\r\n' to '\r\n'
1024 //
1025 ArgumentString = "\r\n";
1026 } else {
1027 //
1028 // Translate '\r' to '\r'
1029 //
1030 ArgumentString = "\r";
1031 Format -= BytesPerFormatCharacter;
1032 }
1033 break;
1034
1035 case '\n':
1036 //
1037 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1038 //
1039 ArgumentString = "\r\n";
1040 Format += BytesPerFormatCharacter;
1041 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1042 if (FormatCharacter != '\r') {
1043 Format -= BytesPerFormatCharacter;
1044 }
1045 break;
1046
1047 case '%':
1048 default:
1049 //
1050 // if the type is '%' or unknown, then print it to the screen
1051 //
1052 ArgumentString = (CHAR8 *)&FormatCharacter;
1053 Flags |= ARGUMENT_UNICODE;
1054 break;
1055 }
1056 break;
1057
1058 case '\r':
1059 Format += BytesPerFormatCharacter;
1060 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1061 if (FormatCharacter == '\n') {
1062 //
1063 // Translate '\r\n' to '\r\n'
1064 //
1065 ArgumentString = "\r\n";
1066 } else {
1067 //
1068 // Translate '\r' to '\r'
1069 //
1070 ArgumentString = "\r";
1071 Format -= BytesPerFormatCharacter;
1072 }
1073 break;
1074
1075 case '\n':
1076 //
1077 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1078 //
1079 ArgumentString = "\r\n";
1080 Format += BytesPerFormatCharacter;
1081 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1082 if (FormatCharacter != '\r') {
1083 Format -= BytesPerFormatCharacter;
1084 }
1085 break;
1086
1087 default:
1088 ArgumentString = (CHAR8 *)&FormatCharacter;
1089 Flags |= ARGUMENT_UNICODE;
1090 break;
1091 }
1092
1093 //
1094 // Retrieve the ArgumentString attriubutes
1095 //
1096 if ((Flags & ARGUMENT_UNICODE) != 0) {
1097 ArgumentMask = 0xffff;
1098 BytesPerArgumentCharacter = 2;
1099 } else {
1100 ArgumentMask = 0xff;
1101 BytesPerArgumentCharacter = 1;
1102 }
1103 if ((Flags & ARGUMENT_REVERSED) != 0) {
1104 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;
1105 } else {
1106 //
1107 // Compute the number of characters in ArgumentString and store it in Count
1108 // ArgumentString is either null-terminated, or it contains Precision characters
1109 //
1110 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {
1111 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;
1112 if (ArgumentCharacter == 0) {
1113 break;
1114 }
1115 }
1116 }
1117
1118 if (Precision < Count) {
1119 Precision = Count;
1120 }
1121
1122 //
1123 // Pad before the string
1124 //
1125 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {
1126 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
1127 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1128 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
1129 }
1130 }
1131
1132 if (ZeroPad) {
1133 if (Prefix != 0) {
1134 LengthToReturn += (1 * BytesPerOutputCharacter);
1135 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1136 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
1137 }
1138 }
1139 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
1140 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1141 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);
1142 }
1143 } else {
1144 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
1145 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1146 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);
1147 }
1148 if (Prefix != 0) {
1149 LengthToReturn += (1 * BytesPerOutputCharacter);
1150 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1151 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
1152 }
1153 }
1154 }
1155
1156 //
1157 // Output the Prefix character if it is present
1158 //
1159 Index = 0;
1160 if (Prefix != 0) {
1161 Index++;
1162 }
1163
1164 //
1165 // Copy the string into the output buffer performing the required type conversions
1166 //
1167 while (Index < Count) {
1168 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;
1169
1170 LengthToReturn += (1 * BytesPerOutputCharacter);
1171 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1172 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);
1173 }
1174 ArgumentString += BytesPerArgumentCharacter;
1175 Index++;
1176 if (Comma) {
1177 Digits++;
1178 if (Digits == 3) {
1179 Digits = 0;
1180 Index++;
1181 if (Index < Count) {
1182 LengthToReturn += (1 * BytesPerOutputCharacter);
1183 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1184 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);
1185 }
1186 }
1187 }
1188 }
1189 }
1190
1191 //
1192 // Pad after the string
1193 //
1194 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {
1195 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
1196 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1197 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
1198 }
1199 }
1200
1201 //
1202 // Get the next character from the format string
1203 //
1204 Format += BytesPerFormatCharacter;
1205
1206 //
1207 // Get the next character from the format string
1208 //
1209 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1210 }
1211
1212 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {
1213 return (LengthToReturn / BytesPerOutputCharacter);
1214 }
1215
1216 ASSERT (Buffer != NULL);
1217 //
1218 // Null terminate the Unicode or ASCII string
1219 //
1220 BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);
1221
1222 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);
1223 }
1224
1225 /**
1226 Worker function that produces a Null-terminated string in an output buffer
1227 based on a Null-terminated format string and variable argument list.
1228
1229 VSPrint function to process format and place the results in Buffer. Since a
1230 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1231 this is the main print working routine
1232
1233 @param StartOfBuffer The character buffer to print the results of the parsing
1234 of Format into.
1235 @param BufferSize The maximum number of characters to put into buffer.
1236 Zero means no limit.
1237 @param Flags Initial flags value.
1238 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set
1239 @param FormatString A Null-terminated format string.
1240 @param ... The variable argument list.
1241
1242 @return The number of characters printed.
1243
1244 **/
1245 UINTN
1246 EFIAPI
1247 BasePrintLibSPrint (
1248 OUT CHAR8 *StartOfBuffer,
1249 IN UINTN BufferSize,
1250 IN UINTN Flags,
1251 IN CONST CHAR8 *FormatString,
1252 ...
1253 )
1254 {
1255 VA_LIST Marker;
1256 UINTN NumberOfPrinted;
1257
1258 VA_START (Marker, FormatString);
1259 NumberOfPrinted = BasePrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);
1260 VA_END (Marker);
1261 return NumberOfPrinted;
1262 }