589d4db619f24ecf55356d9db0892348bff32dea
[mirror_edk2.git] / MdeModulePkg / Library / DxePrintLibPrint2Protocol / PrintLib.c
1 /** @file
2 Instance of Print Library based on gEfiPrint2SProtocolGuid.
3
4 Implement the print library instance by wrap the interface
5 provided in the Print2S protocol. This protocol is defined as the internal
6 protocol related to this implementation, not in the public spec. So, this
7 library instance is only for this code base.
8
9 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
10 This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
14
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17
18 **/
19
20 #include <Uefi.h>
21 #include <Base.h>
22 #include <Protocol/Print2.h>
23
24 #include <Library/PrintLib.h>
25
26 #include <Library/BaseLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/PcdLib.h>
29
30 #define ASSERT_UNICODE_BUFFER(Buffer) ASSERT ((((UINTN) (Buffer)) & 0x01) == 0)
31
32 //
33 // Safe print checks
34 //
35 #define RSIZE_MAX (PcdGet32 (PcdMaximumUnicodeStringLength))
36 #define ASCII_RSIZE_MAX (PcdGet32 (PcdMaximumAsciiStringLength))
37
38 #define SAFE_PRINT_CONSTRAINT_CHECK(Expression, RetVal) \
39 do { \
40 ASSERT (Expression); \
41 if (!(Expression)) { \
42 return RetVal; \
43 } \
44 } while (FALSE)
45
46 EFI_PRINT2S_PROTOCOL *mPrint2SProtocol = NULL;
47
48 /**
49 The constructor function caches the pointer to Print2S protocol.
50
51 The constructor function locates Print2S protocol from protocol database.
52 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
53
54 @param ImageHandle The firmware allocated handle for the EFI image.
55 @param SystemTable A pointer to the EFI System Table.
56
57 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
58
59 **/
60 EFI_STATUS
61 EFIAPI
62 PrintLibConstructor (
63 IN EFI_HANDLE ImageHandle,
64 IN EFI_SYSTEM_TABLE *SystemTable
65 )
66 {
67 EFI_STATUS Status;
68
69 Status = SystemTable->BootServices->LocateProtocol (
70 &gEfiPrint2SProtocolGuid,
71 NULL,
72 (VOID**) &mPrint2SProtocol
73 );
74 ASSERT_EFI_ERROR (Status);
75 ASSERT (mPrint2SProtocol != NULL);
76
77 return Status;
78 }
79
80
81 /**
82 Worker function that converts a VA_LIST to a BASE_LIST based on a Null-terminated
83 format string.
84
85 @param AsciiFormat TRUE if Format is an ASCII string. FALSE if Format is a Unicode string.
86 @param Format Null-terminated format string.
87 @param VaListMarker VA_LIST style variable argument list consumed by processing Format.
88 @param BaseListMarker BASE_LIST style variable argument list consumed by processing Format.
89 @param Size The size, in bytes, of the BaseListMarker buffer.
90
91 @return TRUE The VA_LIST has been converted to BASE_LIST.
92 @return FALSE The VA_LIST has not been converted to BASE_LIST.
93
94 **/
95 BOOLEAN
96 DxePrintLibPrint2ProtocolVaListToBaseList (
97 IN BOOLEAN AsciiFormat,
98 IN CONST CHAR8 *Format,
99 IN VA_LIST VaListMarker,
100 OUT BASE_LIST BaseListMarker,
101 IN UINTN Size
102 )
103 {
104 BASE_LIST BaseListStart;
105 UINTN BytesPerFormatCharacter;
106 UINTN FormatMask;
107 UINTN FormatCharacter;
108 BOOLEAN Long;
109 BOOLEAN Done;
110
111 ASSERT (BaseListMarker != NULL);
112 SAFE_PRINT_CONSTRAINT_CHECK ((Format != NULL), FALSE);
113
114 BaseListStart = BaseListMarker;
115
116 if (AsciiFormat) {
117 if (ASCII_RSIZE_MAX != 0) {
118 SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), FALSE);
119 }
120 BytesPerFormatCharacter = 1;
121 FormatMask = 0xff;
122 } else {
123 if (RSIZE_MAX != 0) {
124 SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16 *)Format, RSIZE_MAX + 1) <= RSIZE_MAX), FALSE);
125 }
126 BytesPerFormatCharacter = 2;
127 FormatMask = 0xffff;
128 }
129
130 //
131 // Get the first character from the format string
132 //
133 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
134
135 while (FormatCharacter != 0) {
136 if (FormatCharacter == '%') {
137 Long = FALSE;
138
139 //
140 // Parse Flags and Width
141 //
142 for (Done = FALSE; !Done; ) {
143 //
144 // Get the next character from the format string
145 //
146 Format += BytesPerFormatCharacter;
147
148 //
149 // Get the next character from the format string
150 //
151 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
152
153 switch (FormatCharacter) {
154 case '.':
155 case '-':
156 case '+':
157 case ' ':
158 case ',':
159 case '0':
160 case '1':
161 case '2':
162 case '3':
163 case '4':
164 case '5':
165 case '6':
166 case '7':
167 case '8':
168 case '9':
169 break;
170 case 'L':
171 case 'l':
172 Long = TRUE;
173 break;
174 case '*':
175 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);
176 break;
177 case '\0':
178 //
179 // Make no output if Format string terminates unexpectedly when
180 // looking up for flag, width, precision and type.
181 //
182 Format -= BytesPerFormatCharacter;
183 //
184 // break skipped on purpose.
185 //
186 default:
187 Done = TRUE;
188 break;
189 }
190 }
191
192 //
193 // Handle each argument type
194 //
195 switch (FormatCharacter) {
196 case 'p':
197 if (sizeof (VOID *) > 4) {
198 Long = TRUE;
199 }
200 case 'X':
201 case 'x':
202 case 'u':
203 case 'd':
204 if (Long) {
205 BASE_ARG (BaseListMarker, INT64) = VA_ARG (VaListMarker, INT64);
206 } else {
207 BASE_ARG (BaseListMarker, int) = VA_ARG (VaListMarker, int);
208 }
209 break;
210 case 's':
211 case 'S':
212 case 'a':
213 case 'g':
214 case 't':
215 BASE_ARG (BaseListMarker, VOID *) = VA_ARG (VaListMarker, VOID *);
216 break;
217 case 'c':
218 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);
219 break;
220 case 'r':
221 BASE_ARG (BaseListMarker, RETURN_STATUS) = VA_ARG (VaListMarker, RETURN_STATUS);
222 break;
223 }
224 }
225
226 //
227 // If BASE_LIST is larger than Size, then return FALSE
228 //
229 if ((UINTN)((UINT8 *)BaseListMarker - (UINT8 *)BaseListStart) > Size) {
230 DEBUG ((DEBUG_ERROR, "The input variable argument list is too long. Please consider breaking into multiple print calls.\n"));
231 return FALSE;
232 }
233
234 //
235 // Get the next character from the format string
236 //
237 Format += BytesPerFormatCharacter;
238
239 //
240 // Get the next character from the format string
241 //
242 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
243 }
244 return TRUE;
245 }
246
247 /**
248 Produces a Null-terminated Unicode string in an output buffer based on
249 a Null-terminated Unicode format string and a VA_LIST argument list.
250
251 This function is similar as vsnprintf_s defined in C11.
252
253 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
254 and BufferSize.
255 The Unicode string is produced by parsing the format string specified by FormatString.
256 Arguments are pulled from the variable argument list specified by Marker based on the
257 contents of the format string.
258 The number of Unicode characters in the produced output buffer is returned not including
259 the Null-terminator.
260
261 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
262 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
263
264 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
265 unmodified and 0 is returned.
266 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is
267 unmodified and 0 is returned.
268 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >
269 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output
270 buffer is unmodified and 0 is returned.
271 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
272 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
273 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
274
275 If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned.
276
277 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
278 Unicode string.
279 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
280 @param FormatString A Null-terminated Unicode format string.
281 @param Marker VA_LIST marker for the variable argument list.
282
283 @return The number of Unicode characters in the produced output buffer not including the
284 Null-terminator.
285
286 **/
287 UINTN
288 EFIAPI
289 UnicodeVSPrint (
290 OUT CHAR16 *StartOfBuffer,
291 IN UINTN BufferSize,
292 IN CONST CHAR16 *FormatString,
293 IN VA_LIST Marker
294 )
295 {
296 UINT64 BaseListMarker[256 / sizeof (UINT64)];
297 BOOLEAN Converted;
298
299 ASSERT_UNICODE_BUFFER (StartOfBuffer);
300 ASSERT_UNICODE_BUFFER (FormatString);
301
302 Converted = DxePrintLibPrint2ProtocolVaListToBaseList (
303 FALSE,
304 (CHAR8 *)FormatString,
305 Marker,
306 (BASE_LIST)BaseListMarker,
307 sizeof (BaseListMarker) - 8
308 );
309 if (!Converted) {
310 return 0;
311 }
312
313 return UnicodeBSPrint (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);
314 }
315
316 /**
317 Produces a Null-terminated Unicode string in an output buffer based on
318 a Null-terminated Unicode format string and a BASE_LIST argument list.
319
320 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
321 and BufferSize.
322 The Unicode string is produced by parsing the format string specified by FormatString.
323 Arguments are pulled from the variable argument list specified by Marker based on the
324 contents of the format string.
325 The number of Unicode characters in the produced output buffer is returned not including
326 the Null-terminator.
327
328 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
329 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
330
331 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
332 unmodified and 0 is returned.
333 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is
334 unmodified and 0 is returned.
335 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >
336 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output
337 buffer is unmodified and 0 is returned.
338 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
339 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
340 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
341
342 If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned.
343
344 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
345 Unicode string.
346 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
347 @param FormatString A Null-terminated Unicode format string.
348 @param Marker BASE_LIST marker for the variable argument list.
349
350 @return The number of Unicode characters in the produced output buffer not including the
351 Null-terminator.
352
353 **/
354 UINTN
355 EFIAPI
356 UnicodeBSPrint (
357 OUT CHAR16 *StartOfBuffer,
358 IN UINTN BufferSize,
359 IN CONST CHAR16 *FormatString,
360 IN BASE_LIST Marker
361 )
362 {
363 ASSERT_UNICODE_BUFFER (StartOfBuffer);
364 ASSERT_UNICODE_BUFFER (FormatString);
365 return mPrint2SProtocol->UnicodeBSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
366 }
367
368 /**
369 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
370 Unicode format string and variable argument list.
371
372 This function is similar as snprintf_s defined in C11.
373
374 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
375 and BufferSize.
376 The Unicode string is produced by parsing the format string specified by FormatString.
377 Arguments are pulled from the variable argument list based on the contents of the format string.
378 The number of Unicode characters in the produced output buffer is returned not including
379 the Null-terminator.
380
381 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
382 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
383
384 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
385 unmodified and 0 is returned.
386 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is
387 unmodified and 0 is returned.
388 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >
389 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output
390 buffer is unmodified and 0 is returned.
391 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
392 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
393 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
394
395 If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned.
396
397 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
398 Unicode string.
399 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
400 @param FormatString A Null-terminated Unicode format string.
401 @param ... Variable argument list whose contents are accessed based on the
402 format string specified by FormatString.
403
404 @return The number of Unicode characters in the produced output buffer not including the
405 Null-terminator.
406
407 **/
408 UINTN
409 EFIAPI
410 UnicodeSPrint (
411 OUT CHAR16 *StartOfBuffer,
412 IN UINTN BufferSize,
413 IN CONST CHAR16 *FormatString,
414 ...
415 )
416 {
417 VA_LIST Marker;
418 UINTN NumberOfPrinted;
419
420 VA_START (Marker, FormatString);
421 NumberOfPrinted = UnicodeVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
422 VA_END (Marker);
423 return NumberOfPrinted;
424 }
425
426 /**
427 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
428 ASCII format string and a VA_LIST argument list.
429
430 This function is similar as vsnprintf_s defined in C11.
431
432 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
433 and BufferSize.
434 The Unicode string is produced by parsing the format string specified by FormatString.
435 Arguments are pulled from the variable argument list specified by Marker based on the
436 contents of the format string.
437 The number of Unicode characters in the produced output buffer is returned not including
438 the Null-terminator.
439
440 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
441
442 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
443 unmodified and 0 is returned.
444 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is
445 unmodified and 0 is returned.
446 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >
447 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output
448 buffer is unmodified and 0 is returned.
449 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
450 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
451 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
452
453 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
454
455 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
456 Unicode string.
457 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
458 @param FormatString A Null-terminated ASCII format string.
459 @param Marker VA_LIST marker for the variable argument list.
460
461 @return The number of Unicode characters in the produced output buffer not including the
462 Null-terminator.
463
464 **/
465 UINTN
466 EFIAPI
467 UnicodeVSPrintAsciiFormat (
468 OUT CHAR16 *StartOfBuffer,
469 IN UINTN BufferSize,
470 IN CONST CHAR8 *FormatString,
471 IN VA_LIST Marker
472 )
473 {
474 UINT64 BaseListMarker[256 / sizeof (UINT64)];
475 BOOLEAN Converted;
476
477 ASSERT_UNICODE_BUFFER (StartOfBuffer);
478
479 Converted = DxePrintLibPrint2ProtocolVaListToBaseList (
480 TRUE,
481 FormatString,
482 Marker,
483 (BASE_LIST)BaseListMarker,
484 sizeof (BaseListMarker) - 8
485 );
486 if (!Converted) {
487 return 0;
488 }
489
490 return UnicodeBSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);
491 }
492
493 /**
494 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
495 ASCII format string and a BASE_LIST argument list.
496
497 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
498 and BufferSize.
499 The Unicode string is produced by parsing the format string specified by FormatString.
500 Arguments are pulled from the variable argument list specified by Marker based on the
501 contents of the format string.
502 The number of Unicode characters in the produced output buffer is returned not including
503 the Null-terminator.
504
505 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
506
507 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
508 unmodified and 0 is returned.
509 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is
510 unmodified and 0 is returned.
511 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >
512 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output
513 buffer is unmodified and 0 is returned.
514 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
515 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
516 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
517
518 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
519
520 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
521 Unicode string.
522 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
523 @param FormatString A Null-terminated ASCII format string.
524 @param Marker BASE_LIST marker for the variable argument list.
525
526 @return The number of Unicode characters in the produced output buffer not including the
527 Null-terminator.
528
529 **/
530 UINTN
531 EFIAPI
532 UnicodeBSPrintAsciiFormat (
533 OUT CHAR16 *StartOfBuffer,
534 IN UINTN BufferSize,
535 IN CONST CHAR8 *FormatString,
536 IN BASE_LIST Marker
537 )
538 {
539 ASSERT_UNICODE_BUFFER (StartOfBuffer);
540 return mPrint2SProtocol->UnicodeBSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker);
541 }
542
543 /**
544 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
545 ASCII format string and variable argument list.
546
547 This function is similar as snprintf_s defined in C11.
548
549 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer
550 and BufferSize.
551 The Unicode string is produced by parsing the format string specified by FormatString.
552 Arguments are pulled from the variable argument list based on the contents of the
553 format string.
554 The number of Unicode characters in the produced output buffer is returned not including
555 the Null-terminator.
556
557 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().
558
559 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
560 unmodified and 0 is returned.
561 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is
562 unmodified and 0 is returned.
563 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >
564 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output
565 buffer is unmodified and 0 is returned.
566 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
567 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
568 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
569
570 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.
571
572 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
573 Unicode string.
574 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
575 @param FormatString A Null-terminated ASCII format string.
576 @param ... Variable argument list whose contents are accessed based on the
577 format string specified by FormatString.
578
579 @return The number of Unicode characters in the produced output buffer not including the
580 Null-terminator.
581
582 **/
583 UINTN
584 EFIAPI
585 UnicodeSPrintAsciiFormat (
586 OUT CHAR16 *StartOfBuffer,
587 IN UINTN BufferSize,
588 IN CONST CHAR8 *FormatString,
589 ...
590 )
591 {
592 VA_LIST Marker;
593 UINTN NumberOfPrinted;
594
595 VA_START (Marker, FormatString);
596 NumberOfPrinted = UnicodeVSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker);
597 VA_END (Marker);
598 return NumberOfPrinted;
599 }
600
601 /**
602 Converts a decimal value to a Null-terminated Unicode string.
603
604 Converts the decimal number specified by Value to a Null-terminated Unicode
605 string specified by Buffer containing at most Width characters. No padding of spaces
606 is ever performed. If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
607 The number of Unicode characters in Buffer is returned not including the Null-terminator.
608 If the conversion contains more than Width characters, then only the first
609 Width characters are returned, and the total number of characters
610 required to perform the conversion is returned.
611 Additional conversion parameters are specified in Flags.
612
613 The Flags bit LEFT_JUSTIFY is always ignored.
614 All conversions are left justified in Buffer.
615 If Width is 0, PREFIX_ZERO is ignored in Flags.
616 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
617 are inserted every 3rd digit starting from the right.
618 If RADIX_HEX is set in Flags, then the output buffer will be
619 formatted in hexadecimal format.
620 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in Buffer is a '-'.
621 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
622 then Buffer is padded with '0' characters so the combination of the optional '-'
623 sign character, '0' characters, digit characters for Value, and the Null-terminator
624 add up to Width characters.
625 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().
626 If Buffer is NULL, then ASSERT().
627 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
628 If unsupported bits are set in Flags, then ASSERT().
629 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().
630 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
631
632 @param Buffer Pointer to the output buffer for the produced Null-terminated
633 Unicode string.
634 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
635 @param Value The 64-bit signed value to convert to a string.
636 @param Width The maximum number of Unicode characters to place in Buffer, not including
637 the Null-terminator.
638
639 @return The number of Unicode characters in Buffer not including the Null-terminator.
640
641 **/
642 UINTN
643 EFIAPI
644 UnicodeValueToString (
645 IN OUT CHAR16 *Buffer,
646 IN UINTN Flags,
647 IN INT64 Value,
648 IN UINTN Width
649 )
650 {
651 RETURN_STATUS Status;
652 UINTN BufferSize;
653
654 if (Width == 0) {
655 BufferSize = (MAXIMUM_VALUE_CHARACTERS + 1) * sizeof (CHAR16);
656 } else {
657 BufferSize = (Width + 1) * sizeof (CHAR16);
658 }
659
660 Status = mPrint2SProtocol->UnicodeValueToStringS (Buffer, BufferSize, Flags, Value, Width);
661 if (RETURN_ERROR (Status)) {
662 return 0;
663 }
664
665 return StrnLenS (Buffer, BufferSize / sizeof (CHAR16));
666 }
667
668 /**
669 Converts a decimal value to a Null-terminated Unicode string.
670
671 Converts the decimal number specified by Value to a Null-terminated Unicode
672 string specified by Buffer containing at most Width characters. No padding of
673 spaces is ever performed. If Width is 0 then a width of
674 MAXIMUM_VALUE_CHARACTERS is assumed. If the conversion contains more than
675 Width characters, then only the first Width characters are placed in Buffer.
676 Additional conversion parameters are specified in Flags.
677
678 The Flags bit LEFT_JUSTIFY is always ignored.
679 All conversions are left justified in Buffer.
680 If Width is 0, PREFIX_ZERO is ignored in Flags.
681 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and
682 commas are inserted every 3rd digit starting from the right.
683 If RADIX_HEX is set in Flags, then the output buffer will be formatted in
684 hexadecimal format.
685 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in
686 Buffer is a '-'.
687 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, then
688 Buffer is padded with '0' characters so the combination of the optional '-'
689 sign character, '0' characters, digit characters for Value, and the
690 Null-terminator add up to Width characters.
691
692 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
693 If an error would be returned, then the function will also ASSERT().
694
695 @param Buffer The pointer to the output buffer for the produced
696 Null-terminated Unicode string.
697 @param BufferSize The size of Buffer in bytes, including the
698 Null-terminator.
699 @param Flags The bitmask of flags that specify left justification,
700 zero pad, and commas.
701 @param Value The 64-bit signed value to convert to a string.
702 @param Width The maximum number of Unicode characters to place in
703 Buffer, not including the Null-terminator.
704
705 @retval RETURN_SUCCESS The decimal value is converted.
706 @retval RETURN_BUFFER_TOO_SMALL If BufferSize cannot hold the converted
707 value.
708 @retval RETURN_INVALID_PARAMETER If Buffer is NULL.
709 If PcdMaximumUnicodeStringLength is not
710 zero, and BufferSize is greater than
711 (PcdMaximumUnicodeStringLength *
712 sizeof (CHAR16) + 1).
713 If unsupported bits are set in Flags.
714 If both COMMA_TYPE and RADIX_HEX are set in
715 Flags.
716 If Width >= MAXIMUM_VALUE_CHARACTERS.
717
718 **/
719 RETURN_STATUS
720 EFIAPI
721 UnicodeValueToStringS (
722 IN OUT CHAR16 *Buffer,
723 IN UINTN BufferSize,
724 IN UINTN Flags,
725 IN INT64 Value,
726 IN UINTN Width
727 )
728 {
729 return mPrint2SProtocol->UnicodeValueToStringS (Buffer, BufferSize, Flags, Value, Width);
730 }
731
732 /**
733 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
734 ASCII format string and a VA_LIST argument list.
735
736 This function is similar as vsnprintf_s defined in C11.
737
738 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
739 and BufferSize.
740 The ASCII string is produced by parsing the format string specified by FormatString.
741 Arguments are pulled from the variable argument list specified by Marker based on
742 the contents of the format string.
743 The number of ASCII characters in the produced output buffer is returned not including
744 the Null-terminator.
745
746 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
747 unmodified and 0 is returned.
748 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
749 unmodified and 0 is returned.
750 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
751 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
752 is unmodified and 0 is returned.
753 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
754 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
755 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
756
757 If BufferSize is 0, then no output buffer is produced and 0 is returned.
758
759 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
760 ASCII string.
761 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
762 @param FormatString A Null-terminated ASCII format string.
763 @param Marker VA_LIST marker for the variable argument list.
764
765 @return The number of ASCII characters in the produced output buffer not including the
766 Null-terminator.
767
768 **/
769 UINTN
770 EFIAPI
771 AsciiVSPrint (
772 OUT CHAR8 *StartOfBuffer,
773 IN UINTN BufferSize,
774 IN CONST CHAR8 *FormatString,
775 IN VA_LIST Marker
776 )
777 {
778 UINT64 BaseListMarker[256 / sizeof (UINT64)];
779 BOOLEAN Converted;
780
781 Converted = DxePrintLibPrint2ProtocolVaListToBaseList (
782 TRUE,
783 FormatString,
784 Marker,
785 (BASE_LIST)BaseListMarker,
786 sizeof (BaseListMarker) - 8
787 );
788 if (!Converted) {
789 return 0;
790 }
791
792 return AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);
793 }
794
795 /**
796 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
797 ASCII format string and a BASE_LIST argument list.
798
799 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
800 and BufferSize.
801 The ASCII string is produced by parsing the format string specified by FormatString.
802 Arguments are pulled from the variable argument list specified by Marker based on
803 the contents of the format string.
804 The number of ASCII characters in the produced output buffer is returned not including
805 the Null-terminator.
806
807 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
808 unmodified and 0 is returned.
809 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
810 unmodified and 0 is returned.
811 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
812 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
813 is unmodified and 0 is returned.
814 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
815 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
816 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
817
818 If BufferSize is 0, then no output buffer is produced and 0 is returned.
819
820 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
821 ASCII string.
822 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
823 @param FormatString A Null-terminated ASCII format string.
824 @param Marker BASE_LIST marker for the variable argument list.
825
826 @return The number of ASCII characters in the produced output buffer not including the
827 Null-terminator.
828
829 **/
830 UINTN
831 EFIAPI
832 AsciiBSPrint (
833 OUT CHAR8 *StartOfBuffer,
834 IN UINTN BufferSize,
835 IN CONST CHAR8 *FormatString,
836 IN BASE_LIST Marker
837 )
838 {
839 return mPrint2SProtocol->AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
840 }
841
842 /**
843 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
844 ASCII format string and variable argument list.
845
846 This function is similar as snprintf_s defined in C11.
847
848 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
849 and BufferSize.
850 The ASCII string is produced by parsing the format string specified by FormatString.
851 Arguments are pulled from the variable argument list based on the contents of the
852 format string.
853 The number of ASCII characters in the produced output buffer is returned not including
854 the Null-terminator.
855
856 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
857 unmodified and 0 is returned.
858 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
859 unmodified and 0 is returned.
860 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
861 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
862 is unmodified and 0 is returned.
863 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
864 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
865 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
866
867 If BufferSize is 0, then no output buffer is produced and 0 is returned.
868
869 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
870 ASCII string.
871 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
872 @param FormatString A Null-terminated ASCII format string.
873 @param ... Variable argument list whose contents are accessed based on the
874 format string specified by FormatString.
875
876 @return The number of ASCII characters in the produced output buffer not including the
877 Null-terminator.
878
879 **/
880 UINTN
881 EFIAPI
882 AsciiSPrint (
883 OUT CHAR8 *StartOfBuffer,
884 IN UINTN BufferSize,
885 IN CONST CHAR8 *FormatString,
886 ...
887 )
888 {
889 VA_LIST Marker;
890 UINTN NumberOfPrinted;
891
892 VA_START (Marker, FormatString);
893 NumberOfPrinted = AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
894 VA_END (Marker);
895 return NumberOfPrinted;
896 }
897
898 /**
899 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
900 Unicode format string and a VA_LIST argument list.
901
902 This function is similar as vsnprintf_s defined in C11.
903
904 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
905 and BufferSize.
906 The ASCII string is produced by parsing the format string specified by FormatString.
907 Arguments are pulled from the variable argument list specified by Marker based on
908 the contents of the format string.
909 The number of ASCII characters in the produced output buffer is returned not including
910 the Null-terminator.
911
912 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
913
914 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
915 unmodified and 0 is returned.
916 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
917 unmodified and 0 is returned.
918 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
919 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
920 is unmodified and 0 is returned.
921 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
922 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
923 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
924
925 If BufferSize is 0, then no output buffer is produced and 0 is returned.
926
927 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
928 ASCII string.
929 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
930 @param FormatString A Null-terminated Unicode format string.
931 @param Marker VA_LIST marker for the variable argument list.
932
933 @return The number of ASCII characters in the produced output buffer not including the
934 Null-terminator.
935
936 **/
937 UINTN
938 EFIAPI
939 AsciiVSPrintUnicodeFormat (
940 OUT CHAR8 *StartOfBuffer,
941 IN UINTN BufferSize,
942 IN CONST CHAR16 *FormatString,
943 IN VA_LIST Marker
944 )
945 {
946 UINT64 BaseListMarker[256 / sizeof (UINT64)];
947 BOOLEAN Converted;
948
949 ASSERT_UNICODE_BUFFER (FormatString);
950
951 Converted = DxePrintLibPrint2ProtocolVaListToBaseList (
952 FALSE,
953 (CHAR8 *)FormatString,
954 Marker,
955 (BASE_LIST)BaseListMarker,
956 sizeof (BaseListMarker) - 8
957 );
958 if (!Converted) {
959 return 0;
960 }
961
962 return AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);
963 }
964
965 /**
966 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
967 Unicode format string and a BASE_LIST argument list.
968
969 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
970 and BufferSize.
971 The ASCII string is produced by parsing the format string specified by FormatString.
972 Arguments are pulled from the variable argument list specified by Marker based on
973 the contents of the format string.
974 The number of ASCII characters in the produced output buffer is returned not including
975 the Null-terminator.
976
977 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
978
979 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
980 unmodified and 0 is returned.
981 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
982 unmodified and 0 is returned.
983 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
984 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
985 is unmodified and 0 is returned.
986 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
987 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
988 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
989
990 If BufferSize is 0, then no output buffer is produced and 0 is returned.
991
992 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
993 ASCII string.
994 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
995 @param FormatString A Null-terminated Unicode format string.
996 @param Marker BASE_LIST marker for the variable argument list.
997
998 @return The number of ASCII characters in the produced output buffer not including the
999 Null-terminator.
1000
1001 **/
1002 UINTN
1003 EFIAPI
1004 AsciiBSPrintUnicodeFormat (
1005 OUT CHAR8 *StartOfBuffer,
1006 IN UINTN BufferSize,
1007 IN CONST CHAR16 *FormatString,
1008 IN BASE_LIST Marker
1009 )
1010 {
1011 ASSERT_UNICODE_BUFFER (FormatString);
1012 return mPrint2SProtocol->AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);
1013 }
1014
1015 /**
1016 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
1017 Unicode format string and variable argument list.
1018
1019 This function is similar as snprintf_s defined in C11.
1020
1021 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
1022 and BufferSize.
1023 The ASCII string is produced by parsing the format string specified by FormatString.
1024 Arguments are pulled from the variable argument list based on the contents of the
1025 format string.
1026 The number of ASCII characters in the produced output buffer is returned not including
1027 the Null-terminator.
1028
1029 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
1030
1031 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
1032 unmodified and 0 is returned.
1033 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
1034 unmodified and 0 is returned.
1035 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
1036 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
1037 is unmodified and 0 is returned.
1038 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
1039 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
1040 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
1041
1042 If BufferSize is 0, then no output buffer is produced and 0 is returned.
1043
1044 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
1045 ASCII string.
1046 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
1047 @param FormatString A Null-terminated Unicode format string.
1048 @param ... Variable argument list whose contents are accessed based on the
1049 format string specified by FormatString.
1050
1051 @return The number of ASCII characters in the produced output buffer not including the
1052 Null-terminator.
1053
1054 **/
1055 UINTN
1056 EFIAPI
1057 AsciiSPrintUnicodeFormat (
1058 OUT CHAR8 *StartOfBuffer,
1059 IN UINTN BufferSize,
1060 IN CONST CHAR16 *FormatString,
1061 ...
1062 )
1063 {
1064 VA_LIST Marker;
1065 UINTN NumberOfPrinted;
1066
1067 VA_START (Marker, FormatString);
1068 NumberOfPrinted = AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);
1069 VA_END (Marker);
1070 return NumberOfPrinted;
1071 }
1072
1073
1074 /**
1075 Converts a decimal value to a Null-terminated ASCII string.
1076
1077 Converts the decimal number specified by Value to a Null-terminated ASCII string
1078 specified by Buffer containing at most Width characters. No padding of spaces
1079 is ever performed.
1080 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
1081 The number of ASCII characters in Buffer is returned not including the Null-terminator.
1082 If the conversion contains more than Width characters, then only the first Width
1083 characters are returned, and the total number of characters required to perform
1084 the conversion is returned.
1085 Additional conversion parameters are specified in Flags.
1086 The Flags bit LEFT_JUSTIFY is always ignored.
1087 All conversions are left justified in Buffer.
1088 If Width is 0, PREFIX_ZERO is ignored in Flags.
1089 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
1090 are inserted every 3rd digit starting from the right.
1091 If RADIX_HEX is set in Flags, then the output buffer will be
1092 formatted in hexadecimal format.
1093 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in Buffer is a '-'.
1094 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
1095 then Buffer is padded with '0' characters so the combination of the optional '-'
1096 sign character, '0' characters, digit characters for Value, and the Null-terminator
1097 add up to Width characters.
1098
1099 If Buffer is NULL, then ASSERT().
1100 If unsupported bits are set in Flags, then ASSERT().
1101 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().
1102 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
1103
1104 @param Buffer Pointer to the output buffer for the produced Null-terminated
1105 ASCII string.
1106 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
1107 @param Value The 64-bit signed value to convert to a string.
1108 @param Width The maximum number of ASCII characters to place in Buffer, not including
1109 the Null-terminator.
1110
1111 @return The number of ASCII characters in Buffer not including the Null-terminator.
1112
1113 **/
1114 UINTN
1115 EFIAPI
1116 AsciiValueToString (
1117 OUT CHAR8 *Buffer,
1118 IN UINTN Flags,
1119 IN INT64 Value,
1120 IN UINTN Width
1121 )
1122 {
1123 RETURN_STATUS Status;
1124 UINTN BufferSize;
1125
1126 if (Width == 0) {
1127 BufferSize = (MAXIMUM_VALUE_CHARACTERS + 1) * sizeof (CHAR8);
1128 } else {
1129 BufferSize = (Width + 1) * sizeof (CHAR8);
1130 }
1131
1132 Status = mPrint2SProtocol->AsciiValueToStringS (Buffer, BufferSize, Flags, Value, Width);
1133 if (RETURN_ERROR (Status)) {
1134 return 0;
1135 }
1136
1137 return AsciiStrnLenS (Buffer, BufferSize / sizeof (CHAR8));
1138 }
1139
1140 /**
1141 Converts a decimal value to a Null-terminated Ascii string.
1142
1143 Converts the decimal number specified by Value to a Null-terminated Ascii
1144 string specified by Buffer containing at most Width characters. No padding of
1145 spaces is ever performed. If Width is 0 then a width of
1146 MAXIMUM_VALUE_CHARACTERS is assumed. If the conversion contains more than
1147 Width characters, then only the first Width characters are placed in Buffer.
1148 Additional conversion parameters are specified in Flags.
1149
1150 The Flags bit LEFT_JUSTIFY is always ignored.
1151 All conversions are left justified in Buffer.
1152 If Width is 0, PREFIX_ZERO is ignored in Flags.
1153 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and
1154 commas are inserted every 3rd digit starting from the right.
1155 If RADIX_HEX is set in Flags, then the output buffer will be formatted in
1156 hexadecimal format.
1157 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in
1158 Buffer is a '-'.
1159 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, then
1160 Buffer is padded with '0' characters so the combination of the optional '-'
1161 sign character, '0' characters, digit characters for Value, and the
1162 Null-terminator add up to Width characters.
1163
1164 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
1165 If an error would be returned, then the function will also ASSERT().
1166
1167 @param Buffer The pointer to the output buffer for the produced
1168 Null-terminated Ascii string.
1169 @param BufferSize The size of Buffer in bytes, including the
1170 Null-terminator.
1171 @param Flags The bitmask of flags that specify left justification,
1172 zero pad, and commas.
1173 @param Value The 64-bit signed value to convert to a string.
1174 @param Width The maximum number of Ascii characters to place in
1175 Buffer, not including the Null-terminator.
1176
1177 @retval RETURN_SUCCESS The decimal value is converted.
1178 @retval RETURN_BUFFER_TOO_SMALL If BufferSize cannot hold the converted
1179 value.
1180 @retval RETURN_INVALID_PARAMETER If Buffer is NULL.
1181 If PcdMaximumAsciiStringLength is not
1182 zero, and BufferSize is greater than
1183 PcdMaximumAsciiStringLength.
1184 If unsupported bits are set in Flags.
1185 If both COMMA_TYPE and RADIX_HEX are set in
1186 Flags.
1187 If Width >= MAXIMUM_VALUE_CHARACTERS.
1188
1189 **/
1190 RETURN_STATUS
1191 EFIAPI
1192 AsciiValueToStringS (
1193 IN OUT CHAR8 *Buffer,
1194 IN UINTN BufferSize,
1195 IN UINTN Flags,
1196 IN INT64 Value,
1197 IN UINTN Width
1198 )
1199 {
1200 return mPrint2SProtocol->AsciiValueToStringS (Buffer, BufferSize, Flags, Value, Width);
1201 }
1202
1203 #define PREFIX_SIGN BIT1
1204 #define PREFIX_BLANK BIT2
1205 #define LONG_TYPE BIT4
1206 #define OUTPUT_UNICODE BIT6
1207 #define FORMAT_UNICODE BIT8
1208 #define PAD_TO_WIDTH BIT9
1209 #define ARGUMENT_UNICODE BIT10
1210 #define PRECISION BIT11
1211 #define ARGUMENT_REVERSED BIT12
1212 #define COUNT_ONLY_NO_PRINT BIT13
1213 #define UNSIGNED_TYPE BIT14
1214
1215 //
1216 // Record date and time information
1217 //
1218 typedef struct {
1219 UINT16 Year;
1220 UINT8 Month;
1221 UINT8 Day;
1222 UINT8 Hour;
1223 UINT8 Minute;
1224 UINT8 Second;
1225 UINT8 Pad1;
1226 UINT32 Nanosecond;
1227 INT16 TimeZone;
1228 UINT8 Daylight;
1229 UINT8 Pad2;
1230 } TIME;
1231
1232 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
1233
1234 /**
1235 Internal function that convert a number to a string in Buffer.
1236
1237 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.
1238
1239 @param Buffer Location to place the ASCII string of Value.
1240 @param Value The value to convert to a Decimal or Hexadecimal string in Buffer.
1241 @param Radix Radix of the value
1242
1243 @return A pointer to the end of buffer filled with ASCII string.
1244
1245 **/
1246 CHAR8 *
1247 InternalPrintLibValueToString (
1248 IN OUT CHAR8 *Buffer,
1249 IN INT64 Value,
1250 IN UINTN Radix
1251 )
1252 {
1253 UINT32 Remainder;
1254
1255 //
1256 // Loop to convert one digit at a time in reverse order
1257 //
1258 *Buffer = 0;
1259 do {
1260 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);
1261 *(++Buffer) = mHexStr[Remainder];
1262 } while (Value != 0);
1263
1264 //
1265 // Return pointer of the end of filled buffer.
1266 //
1267 return Buffer;
1268 }
1269
1270 /**
1271 Worker function that produces a Null-terminated string in an output buffer
1272 based on a Null-terminated format string and a VA_LIST argument list.
1273
1274 VSPrint function to process format and place the results in Buffer. Since a
1275 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1276 this is the main print working routine.
1277
1278 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.
1279
1280 @param[out] Buffer The character buffer to print the results of the
1281 parsing of Format into.
1282 @param[in] BufferSize The maximum number of characters to put into
1283 buffer.
1284 @param[in] Flags Initial flags value.
1285 Can only have FORMAT_UNICODE, OUTPUT_UNICODE,
1286 and COUNT_ONLY_NO_PRINT set.
1287 @param[in] Format A Null-terminated format string.
1288 @param[in] VaListMarker VA_LIST style variable argument list consumed by
1289 processing Format.
1290 @param[in] BaseListMarker BASE_LIST style variable argument list consumed
1291 by processing Format.
1292
1293 @return The number of characters printed not including the Null-terminator.
1294 If COUNT_ONLY_NO_PRINT was set returns the same, but without any
1295 modification to Buffer.
1296
1297 **/
1298 UINTN
1299 InternalPrintLibSPrintMarker (
1300 OUT CHAR8 *Buffer,
1301 IN UINTN BufferSize,
1302 IN UINTN Flags,
1303 IN CONST CHAR8 *Format,
1304 IN VA_LIST VaListMarker, OPTIONAL
1305 IN BASE_LIST BaseListMarker OPTIONAL
1306 );
1307
1308 /**
1309 Worker function that produces a Null-terminated string in an output buffer
1310 based on a Null-terminated format string and variable argument list.
1311
1312 VSPrint function to process format and place the results in Buffer. Since a
1313 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1314 this is the main print working routine
1315
1316 @param StartOfBuffer The character buffer to print the results of the parsing
1317 of Format into.
1318 @param BufferSize The maximum number of characters to put into buffer.
1319 Zero means no limit.
1320 @param Flags Initial flags value.
1321 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set
1322 @param FormatString A Null-terminated format string.
1323 @param ... The variable argument list.
1324
1325 @return The number of characters printed.
1326
1327 **/
1328 UINTN
1329 EFIAPI
1330 InternalPrintLibSPrint (
1331 OUT CHAR8 *StartOfBuffer,
1332 IN UINTN BufferSize,
1333 IN UINTN Flags,
1334 IN CONST CHAR8 *FormatString,
1335 ...
1336 )
1337 {
1338 VA_LIST Marker;
1339 UINTN NumberOfPrinted;
1340
1341 VA_START (Marker, FormatString);
1342 NumberOfPrinted = InternalPrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);
1343 VA_END (Marker);
1344 return NumberOfPrinted;
1345 }
1346
1347 #define WARNING_STATUS_NUMBER 5
1348 #define ERROR_STATUS_NUMBER 33
1349
1350 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 * CONST mStatusString[] = {
1351 "Success", // RETURN_SUCCESS = 0
1352 "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH = 1
1353 "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE = 2
1354 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE = 3
1355 "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL = 4
1356 "Warning Stale Data", // RETURN_WARN_STALE_DATA = 5
1357 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT
1358 "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT
1359 "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT
1360 "Bad Buffer Size", // RETURN_BAD_BUFFER_SIZE = 4 | MAX_BIT
1361 "Buffer Too Small", // RETURN_BUFFER_TOO_SMALL, = 5 | MAX_BIT
1362 "Not Ready", // RETURN_NOT_READY = 6 | MAX_BIT
1363 "Device Error", // RETURN_DEVICE_ERROR = 7 | MAX_BIT
1364 "Write Protected", // RETURN_WRITE_PROTECTED = 8 | MAX_BIT
1365 "Out of Resources", // RETURN_OUT_OF_RESOURCES = 9 | MAX_BIT
1366 "Volume Corrupt", // RETURN_VOLUME_CORRUPTED = 10 | MAX_BIT
1367 "Volume Full", // RETURN_VOLUME_FULL = 11 | MAX_BIT
1368 "No Media", // RETURN_NO_MEDIA = 12 | MAX_BIT
1369 "Media changed", // RETURN_MEDIA_CHANGED = 13 | MAX_BIT
1370 "Not Found", // RETURN_NOT_FOUND = 14 | MAX_BIT
1371 "Access Denied", // RETURN_ACCESS_DENIED = 15 | MAX_BIT
1372 "No Response", // RETURN_NO_RESPONSE = 16 | MAX_BIT
1373 "No mapping", // RETURN_NO_MAPPING = 17 | MAX_BIT
1374 "Time out", // RETURN_TIMEOUT = 18 | MAX_BIT
1375 "Not started", // RETURN_NOT_STARTED = 19 | MAX_BIT
1376 "Already started", // RETURN_ALREADY_STARTED = 20 | MAX_BIT
1377 "Aborted", // RETURN_ABORTED = 21 | MAX_BIT
1378 "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT
1379 "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT
1380 "Protocol Error", // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT
1381 "Incompatible Version", // RETURN_INCOMPATIBLE_VERSION = 25 | MAX_BIT
1382 "Security Violation", // RETURN_SECURITY_VIOLATION = 26 | MAX_BIT
1383 "CRC Error", // RETURN_CRC_ERROR = 27 | MAX_BIT
1384 "End of Media", // RETURN_END_OF_MEDIA = 28 | MAX_BIT
1385 "Reserved (29)", // RESERVED = 29 | MAX_BIT
1386 "Reserved (30)", // RESERVED = 30 | MAX_BIT
1387 "End of File", // RETURN_END_OF_FILE = 31 | MAX_BIT
1388 "Invalid Language", // RETURN_INVALID_LANGUAGE = 32 | MAX_BIT
1389 "Compromised Data" // RETURN_COMPROMISED_DATA = 33 | MAX_BIT
1390 };
1391
1392 /**
1393 Internal function that places the character into the Buffer.
1394
1395 Internal function that places ASCII or Unicode character into the Buffer.
1396
1397 @param Buffer The buffer to place the Unicode or ASCII string.
1398 @param EndBuffer The end of the input Buffer. No characters will be
1399 placed after that.
1400 @param Length The count of character to be placed into Buffer.
1401 (Negative value indicates no buffer fill.)
1402 @param Character The character to be placed into Buffer.
1403 @param Increment The character increment in Buffer.
1404
1405 @return Buffer.
1406
1407 **/
1408 CHAR8 *
1409 InternalPrintLibFillBuffer (
1410 OUT CHAR8 *Buffer,
1411 IN CHAR8 *EndBuffer,
1412 IN INTN Length,
1413 IN UINTN Character,
1414 IN INTN Increment
1415 )
1416 {
1417 INTN Index;
1418
1419 for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {
1420 *Buffer = (CHAR8) Character;
1421 if (Increment != 1) {
1422 *(Buffer + 1) = (CHAR8)(Character >> 8);
1423 }
1424 Buffer += Increment;
1425 }
1426
1427 return Buffer;
1428 }
1429
1430 /**
1431 Worker function that produces a Null-terminated string in an output buffer
1432 based on a Null-terminated format string and a VA_LIST argument list.
1433
1434 VSPrint function to process format and place the results in Buffer. Since a
1435 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1436 this is the main print working routine.
1437
1438 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.
1439
1440 @param[out] Buffer The character buffer to print the results of the
1441 parsing of Format into.
1442 @param[in] BufferSize The maximum number of characters to put into
1443 buffer.
1444 @param[in] Flags Initial flags value.
1445 Can only have FORMAT_UNICODE, OUTPUT_UNICODE,
1446 and COUNT_ONLY_NO_PRINT set.
1447 @param[in] Format A Null-terminated format string.
1448 @param[in] VaListMarker VA_LIST style variable argument list consumed by
1449 processing Format.
1450 @param[in] BaseListMarker BASE_LIST style variable argument list consumed
1451 by processing Format.
1452
1453 @return The number of characters printed not including the Null-terminator.
1454 If COUNT_ONLY_NO_PRINT was set returns the same, but without any
1455 modification to Buffer.
1456
1457 **/
1458 UINTN
1459 InternalPrintLibSPrintMarker (
1460 OUT CHAR8 *Buffer,
1461 IN UINTN BufferSize,
1462 IN UINTN Flags,
1463 IN CONST CHAR8 *Format,
1464 IN VA_LIST VaListMarker, OPTIONAL
1465 IN BASE_LIST BaseListMarker OPTIONAL
1466 )
1467 {
1468 CHAR8 *OriginalBuffer;
1469 CHAR8 *EndBuffer;
1470 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
1471 UINT32 BytesPerOutputCharacter;
1472 UINTN BytesPerFormatCharacter;
1473 UINTN FormatMask;
1474 UINTN FormatCharacter;
1475 UINTN Width;
1476 UINTN Precision;
1477 INT64 Value;
1478 CONST CHAR8 *ArgumentString;
1479 UINTN Character;
1480 GUID *TmpGuid;
1481 TIME *TmpTime;
1482 UINTN Count;
1483 UINTN ArgumentMask;
1484 INTN BytesPerArgumentCharacter;
1485 UINTN ArgumentCharacter;
1486 BOOLEAN Done;
1487 UINTN Index;
1488 CHAR8 Prefix;
1489 BOOLEAN ZeroPad;
1490 BOOLEAN Comma;
1491 UINTN Digits;
1492 UINTN Radix;
1493 RETURN_STATUS Status;
1494 UINT32 GuidData1;
1495 UINT16 GuidData2;
1496 UINT16 GuidData3;
1497 UINTN LengthToReturn;
1498
1499 //
1500 // If you change this code be sure to match the 2 versions of this function.
1501 // Nearly identical logic is found in the BasePrintLib and
1502 // DxePrintLibPrint2Protocol (both PrintLib instances).
1503 //
1504
1505 //
1506 // 1. Buffer shall not be a null pointer when both BufferSize > 0 and
1507 // COUNT_ONLY_NO_PRINT is not set in Flags.
1508 //
1509 if ((BufferSize > 0) && ((Flags & COUNT_ONLY_NO_PRINT) == 0)) {
1510 SAFE_PRINT_CONSTRAINT_CHECK ((Buffer != NULL), 0);
1511 }
1512
1513 //
1514 // 2. Format shall not be a null pointer when BufferSize > 0 or when
1515 // COUNT_ONLY_NO_PRINT is set in Flags.
1516 //
1517 if ((BufferSize > 0) || ((Flags & COUNT_ONLY_NO_PRINT) != 0)) {
1518 SAFE_PRINT_CONSTRAINT_CHECK ((Format != NULL), 0);
1519 }
1520
1521 //
1522 // 3. BufferSize shall not be greater than RSIZE_MAX for Unicode output or
1523 // ASCII_RSIZE_MAX for Ascii output.
1524 //
1525 if ((Flags & OUTPUT_UNICODE) != 0) {
1526 if (RSIZE_MAX != 0) {
1527 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= RSIZE_MAX), 0);
1528 }
1529 BytesPerOutputCharacter = 2;
1530 } else {
1531 if (ASCII_RSIZE_MAX != 0) {
1532 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= ASCII_RSIZE_MAX), 0);
1533 }
1534 BytesPerOutputCharacter = 1;
1535 }
1536
1537 //
1538 // 4. Format shall not contain more than RSIZE_MAX Unicode characters or
1539 // ASCII_RSIZE_MAX Ascii characters.
1540 //
1541 if ((Flags & FORMAT_UNICODE) != 0) {
1542 if (RSIZE_MAX != 0) {
1543 SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16 *)Format, RSIZE_MAX + 1) <= RSIZE_MAX), 0);
1544 }
1545 BytesPerFormatCharacter = 2;
1546 FormatMask = 0xffff;
1547 } else {
1548 if (ASCII_RSIZE_MAX != 0) {
1549 SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), 0);
1550 }
1551 BytesPerFormatCharacter = 1;
1552 FormatMask = 0xff;
1553 }
1554
1555 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {
1556 if (BufferSize == 0) {
1557 Buffer = NULL;
1558 }
1559 } else {
1560 //
1561 // We can run without a Buffer for counting only.
1562 //
1563 if (BufferSize == 0) {
1564 return 0;
1565 }
1566 }
1567
1568 LengthToReturn = 0;
1569 EndBuffer = NULL;
1570 OriginalBuffer = NULL;
1571
1572 //
1573 // Reserve space for the Null terminator.
1574 //
1575 if (Buffer != NULL) {
1576 BufferSize--;
1577 OriginalBuffer = Buffer;
1578
1579 //
1580 // Set the tag for the end of the input Buffer.
1581 //
1582 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;
1583 }
1584
1585 //
1586 // Get the first character from the format string
1587 //
1588 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1589
1590 //
1591 // Loop until the end of the format string is reached or the output buffer is full
1592 //
1593 while (FormatCharacter != 0) {
1594 if ((Buffer != NULL) && (Buffer >= EndBuffer)) {
1595 break;
1596 }
1597 //
1598 // Clear all the flag bits except those that may have been passed in
1599 //
1600 Flags &= (UINTN) (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);
1601
1602 //
1603 // Set the default width to zero, and the default precision to 1
1604 //
1605 Width = 0;
1606 Precision = 1;
1607 Prefix = 0;
1608 Comma = FALSE;
1609 ZeroPad = FALSE;
1610 Count = 0;
1611 Digits = 0;
1612
1613 switch (FormatCharacter) {
1614 case '%':
1615 //
1616 // Parse Flags and Width
1617 //
1618 for (Done = FALSE; !Done; ) {
1619 Format += BytesPerFormatCharacter;
1620 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1621 switch (FormatCharacter) {
1622 case '.':
1623 Flags |= PRECISION;
1624 break;
1625 case '-':
1626 Flags |= LEFT_JUSTIFY;
1627 break;
1628 case '+':
1629 Flags |= PREFIX_SIGN;
1630 break;
1631 case ' ':
1632 Flags |= PREFIX_BLANK;
1633 break;
1634 case ',':
1635 Flags |= COMMA_TYPE;
1636 break;
1637 case 'L':
1638 case 'l':
1639 Flags |= LONG_TYPE;
1640 break;
1641 case '*':
1642 if ((Flags & PRECISION) == 0) {
1643 Flags |= PAD_TO_WIDTH;
1644 if (BaseListMarker == NULL) {
1645 Width = VA_ARG (VaListMarker, UINTN);
1646 } else {
1647 Width = BASE_ARG (BaseListMarker, UINTN);
1648 }
1649 } else {
1650 if (BaseListMarker == NULL) {
1651 Precision = VA_ARG (VaListMarker, UINTN);
1652 } else {
1653 Precision = BASE_ARG (BaseListMarker, UINTN);
1654 }
1655 }
1656 break;
1657 case '0':
1658 if ((Flags & PRECISION) == 0) {
1659 Flags |= PREFIX_ZERO;
1660 }
1661 case '1':
1662 case '2':
1663 case '3':
1664 case '4':
1665 case '5':
1666 case '6':
1667 case '7':
1668 case '8':
1669 case '9':
1670 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){
1671 Count = (Count * 10) + FormatCharacter - '0';
1672 Format += BytesPerFormatCharacter;
1673 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1674 }
1675 Format -= BytesPerFormatCharacter;
1676 if ((Flags & PRECISION) == 0) {
1677 Flags |= PAD_TO_WIDTH;
1678 Width = Count;
1679 } else {
1680 Precision = Count;
1681 }
1682 break;
1683
1684 case '\0':
1685 //
1686 // Make no output if Format string terminates unexpectedly when
1687 // looking up for flag, width, precision and type.
1688 //
1689 Format -= BytesPerFormatCharacter;
1690 Precision = 0;
1691 //
1692 // break skipped on purpose.
1693 //
1694 default:
1695 Done = TRUE;
1696 break;
1697 }
1698 }
1699
1700 //
1701 // Handle each argument type
1702 //
1703 switch (FormatCharacter) {
1704 case 'p':
1705 //
1706 // Flag space, +, 0, L & l are invalid for type p.
1707 //
1708 Flags &= ~((UINTN) (PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE));
1709 if (sizeof (VOID *) > 4) {
1710 Flags |= LONG_TYPE;
1711 }
1712 //
1713 // break skipped on purpose
1714 //
1715 case 'X':
1716 Flags |= PREFIX_ZERO;
1717 //
1718 // break skipped on purpose
1719 //
1720 case 'x':
1721 Flags |= RADIX_HEX;
1722 //
1723 // break skipped on purpose
1724 //
1725 case 'u':
1726 if ((Flags & RADIX_HEX) == 0) {
1727 Flags &= ~((UINTN) (PREFIX_SIGN));
1728 Flags |= UNSIGNED_TYPE;
1729 }
1730 //
1731 // break skipped on purpose
1732 //
1733 case 'd':
1734 if ((Flags & LONG_TYPE) == 0) {
1735 //
1736 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
1737 // This assumption is made so the format string definition is compatible with the ANSI C
1738 // Specification for formatted strings. It is recommended that the Base Types be used
1739 // everywhere, but in this one case, compliance with ANSI C is more important, and
1740 // provides an implementation that is compatible with that largest possible set of CPU
1741 // architectures. This is why the type "int" is used in this one case.
1742 //
1743 if (BaseListMarker == NULL) {
1744 Value = VA_ARG (VaListMarker, int);
1745 } else {
1746 Value = BASE_ARG (BaseListMarker, int);
1747 }
1748 } else {
1749 if (BaseListMarker == NULL) {
1750 Value = VA_ARG (VaListMarker, INT64);
1751 } else {
1752 Value = BASE_ARG (BaseListMarker, INT64);
1753 }
1754 }
1755 if ((Flags & PREFIX_BLANK) != 0) {
1756 Prefix = ' ';
1757 }
1758 if ((Flags & PREFIX_SIGN) != 0) {
1759 Prefix = '+';
1760 }
1761 if ((Flags & COMMA_TYPE) != 0) {
1762 Comma = TRUE;
1763 }
1764 if ((Flags & RADIX_HEX) == 0) {
1765 Radix = 10;
1766 if (Comma) {
1767 Flags &= ~((UINTN) PREFIX_ZERO);
1768 Precision = 1;
1769 }
1770 if (Value < 0 && (Flags & UNSIGNED_TYPE) == 0) {
1771 Flags |= PREFIX_SIGN;
1772 Prefix = '-';
1773 Value = -Value;
1774 } else if ((Flags & UNSIGNED_TYPE) != 0 && (Flags & LONG_TYPE) == 0) {
1775 //
1776 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
1777 // This assumption is made so the format string definition is compatible with the ANSI C
1778 // Specification for formatted strings. It is recommended that the Base Types be used
1779 // everywhere, but in this one case, compliance with ANSI C is more important, and
1780 // provides an implementation that is compatible with that largest possible set of CPU
1781 // architectures. This is why the type "unsigned int" is used in this one case.
1782 //
1783 Value = (unsigned int)Value;
1784 }
1785 } else {
1786 Radix = 16;
1787 Comma = FALSE;
1788 if ((Flags & LONG_TYPE) == 0 && Value < 0) {
1789 //
1790 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
1791 // This assumption is made so the format string definition is compatible with the ANSI C
1792 // Specification for formatted strings. It is recommended that the Base Types be used
1793 // everywhere, but in this one case, compliance with ANSI C is more important, and
1794 // provides an implementation that is compatible with that largest possible set of CPU
1795 // architectures. This is why the type "unsigned int" is used in this one case.
1796 //
1797 Value = (unsigned int)Value;
1798 }
1799 }
1800 //
1801 // Convert Value to a reversed string
1802 //
1803 Count = InternalPrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;
1804 if (Value == 0 && Precision == 0) {
1805 Count = 0;
1806 }
1807 ArgumentString = (CHAR8 *)ValueBuffer + Count;
1808
1809 Digits = Count % 3;
1810 if (Digits != 0) {
1811 Digits = 3 - Digits;
1812 }
1813 if (Comma && Count != 0) {
1814 Count += ((Count - 1) / 3);
1815 }
1816 if (Prefix != 0) {
1817 Count++;
1818 Precision++;
1819 }
1820 Flags |= ARGUMENT_REVERSED;
1821 ZeroPad = TRUE;
1822 if ((Flags & PREFIX_ZERO) != 0) {
1823 if ((Flags & LEFT_JUSTIFY) == 0) {
1824 if ((Flags & PAD_TO_WIDTH) != 0) {
1825 if ((Flags & PRECISION) == 0) {
1826 Precision = Width;
1827 }
1828 }
1829 }
1830 }
1831 break;
1832
1833 case 's':
1834 case 'S':
1835 Flags |= ARGUMENT_UNICODE;
1836 //
1837 // break skipped on purpose
1838 //
1839 case 'a':
1840 if (BaseListMarker == NULL) {
1841 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);
1842 } else {
1843 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);
1844 }
1845 if (ArgumentString == NULL) {
1846 Flags &= (~(UINTN)ARGUMENT_UNICODE);
1847 ArgumentString = "<null string>";
1848 }
1849 //
1850 // Set the default precision for string to be zero if not specified.
1851 //
1852 if ((Flags & PRECISION) == 0) {
1853 Precision = 0;
1854 }
1855 break;
1856
1857 case 'c':
1858 if (BaseListMarker == NULL) {
1859 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;
1860 } else {
1861 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;
1862 }
1863 ArgumentString = (CHAR8 *)&Character;
1864 Flags |= ARGUMENT_UNICODE;
1865 break;
1866
1867 case 'g':
1868 if (BaseListMarker == NULL) {
1869 TmpGuid = VA_ARG (VaListMarker, GUID *);
1870 } else {
1871 TmpGuid = BASE_ARG (BaseListMarker, GUID *);
1872 }
1873 if (TmpGuid == NULL) {
1874 ArgumentString = "<null guid>";
1875 } else {
1876 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));
1877 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));
1878 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));
1879 InternalPrintLibSPrint (
1880 ValueBuffer,
1881 MAXIMUM_VALUE_CHARACTERS,
1882 0,
1883 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1884 GuidData1,
1885 GuidData2,
1886 GuidData3,
1887 TmpGuid->Data4[0],
1888 TmpGuid->Data4[1],
1889 TmpGuid->Data4[2],
1890 TmpGuid->Data4[3],
1891 TmpGuid->Data4[4],
1892 TmpGuid->Data4[5],
1893 TmpGuid->Data4[6],
1894 TmpGuid->Data4[7]
1895 );
1896 ArgumentString = ValueBuffer;
1897 }
1898 break;
1899
1900 case 't':
1901 if (BaseListMarker == NULL) {
1902 TmpTime = VA_ARG (VaListMarker, TIME *);
1903 } else {
1904 TmpTime = BASE_ARG (BaseListMarker, TIME *);
1905 }
1906 if (TmpTime == NULL) {
1907 ArgumentString = "<null time>";
1908 } else {
1909 InternalPrintLibSPrint (
1910 ValueBuffer,
1911 MAXIMUM_VALUE_CHARACTERS,
1912 0,
1913 "%02d/%02d/%04d %02d:%02d",
1914 TmpTime->Month,
1915 TmpTime->Day,
1916 TmpTime->Year,
1917 TmpTime->Hour,
1918 TmpTime->Minute
1919 );
1920 ArgumentString = ValueBuffer;
1921 }
1922 break;
1923
1924 case 'r':
1925 if (BaseListMarker == NULL) {
1926 Status = VA_ARG (VaListMarker, RETURN_STATUS);
1927 } else {
1928 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);
1929 }
1930 ArgumentString = ValueBuffer;
1931 if (RETURN_ERROR (Status)) {
1932 //
1933 // Clear error bit
1934 //
1935 Index = Status & ~MAX_BIT;
1936 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {
1937 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];
1938 }
1939 } else {
1940 Index = Status;
1941 if (Index <= WARNING_STATUS_NUMBER) {
1942 ArgumentString = mStatusString [Index];
1943 }
1944 }
1945 if (ArgumentString == ValueBuffer) {
1946 InternalPrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);
1947 }
1948 break;
1949
1950 case '\r':
1951 Format += BytesPerFormatCharacter;
1952 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1953 if (FormatCharacter == '\n') {
1954 //
1955 // Translate '\r\n' to '\r\n'
1956 //
1957 ArgumentString = "\r\n";
1958 } else {
1959 //
1960 // Translate '\r' to '\r'
1961 //
1962 ArgumentString = "\r";
1963 Format -= BytesPerFormatCharacter;
1964 }
1965 break;
1966
1967 case '\n':
1968 //
1969 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1970 //
1971 ArgumentString = "\r\n";
1972 Format += BytesPerFormatCharacter;
1973 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1974 if (FormatCharacter != '\r') {
1975 Format -= BytesPerFormatCharacter;
1976 }
1977 break;
1978
1979 case '%':
1980 default:
1981 //
1982 // if the type is '%' or unknown, then print it to the screen
1983 //
1984 ArgumentString = (CHAR8 *)&FormatCharacter;
1985 Flags |= ARGUMENT_UNICODE;
1986 break;
1987 }
1988 break;
1989
1990 case '\r':
1991 Format += BytesPerFormatCharacter;
1992 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1993 if (FormatCharacter == '\n') {
1994 //
1995 // Translate '\r\n' to '\r\n'
1996 //
1997 ArgumentString = "\r\n";
1998 } else {
1999 //
2000 // Translate '\r' to '\r'
2001 //
2002 ArgumentString = "\r";
2003 Format -= BytesPerFormatCharacter;
2004 }
2005 break;
2006
2007 case '\n':
2008 //
2009 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
2010 //
2011 ArgumentString = "\r\n";
2012 Format += BytesPerFormatCharacter;
2013 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
2014 if (FormatCharacter != '\r') {
2015 Format -= BytesPerFormatCharacter;
2016 }
2017 break;
2018
2019 default:
2020 ArgumentString = (CHAR8 *)&FormatCharacter;
2021 Flags |= ARGUMENT_UNICODE;
2022 break;
2023 }
2024
2025 //
2026 // Retrieve the ArgumentString attriubutes
2027 //
2028 if ((Flags & ARGUMENT_UNICODE) != 0) {
2029 ArgumentMask = 0xffff;
2030 BytesPerArgumentCharacter = 2;
2031 } else {
2032 ArgumentMask = 0xff;
2033 BytesPerArgumentCharacter = 1;
2034 }
2035 if ((Flags & ARGUMENT_REVERSED) != 0) {
2036 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;
2037 } else {
2038 //
2039 // Compute the number of characters in ArgumentString and store it in Count
2040 // ArgumentString is either null-terminated, or it contains Precision characters
2041 //
2042 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {
2043 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;
2044 if (ArgumentCharacter == 0) {
2045 break;
2046 }
2047 }
2048 }
2049
2050 if (Precision < Count) {
2051 Precision = Count;
2052 }
2053
2054 //
2055 // Pad before the string
2056 //
2057 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {
2058 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
2059 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
2060 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
2061 }
2062 }
2063
2064 if (ZeroPad) {
2065 if (Prefix != 0) {
2066 LengthToReturn += (1 * BytesPerOutputCharacter);
2067 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
2068 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
2069 }
2070 }
2071 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
2072 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
2073 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);
2074 }
2075 } else {
2076 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
2077 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
2078 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);
2079 }
2080 if (Prefix != 0) {
2081 LengthToReturn += (1 * BytesPerOutputCharacter);
2082 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
2083 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
2084 }
2085 }
2086 }
2087
2088 //
2089 // Output the Prefix character if it is present
2090 //
2091 Index = 0;
2092 if (Prefix != 0) {
2093 Index++;
2094 }
2095
2096 //
2097 // Copy the string into the output buffer performing the required type conversions
2098 //
2099 while (Index < Count) {
2100 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;
2101
2102 LengthToReturn += (1 * BytesPerOutputCharacter);
2103 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
2104 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);
2105 }
2106 ArgumentString += BytesPerArgumentCharacter;
2107 Index++;
2108 if (Comma) {
2109 Digits++;
2110 if (Digits == 3) {
2111 Digits = 0;
2112 Index++;
2113 if (Index < Count) {
2114 LengthToReturn += (1 * BytesPerOutputCharacter);
2115 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
2116 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);
2117 }
2118 }
2119 }
2120 }
2121 }
2122
2123 //
2124 // Pad after the string
2125 //
2126 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {
2127 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
2128 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
2129 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
2130 }
2131 }
2132
2133 //
2134 // Get the next character from the format string
2135 //
2136 Format += BytesPerFormatCharacter;
2137
2138 //
2139 // Get the next character from the format string
2140 //
2141 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
2142 }
2143
2144 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {
2145 return (LengthToReturn / BytesPerOutputCharacter);
2146 }
2147
2148 ASSERT (Buffer != NULL);
2149 //
2150 // Null terminate the Unicode or ASCII string
2151 //
2152 InternalPrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);
2153
2154 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);
2155 }
2156
2157 /**
2158 Returns the number of characters that would be produced by if the formatted
2159 output were produced not including the Null-terminator.
2160
2161 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
2162
2163 If FormatString is NULL, then ASSERT() and 0 is returned.
2164 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more
2165 than PcdMaximumUnicodeStringLength Unicode characters not including the
2166 Null-terminator, then ASSERT() and 0 is returned.
2167
2168 @param[in] FormatString A Null-terminated Unicode format string.
2169 @param[in] Marker VA_LIST marker for the variable argument list.
2170
2171 @return The number of characters that would be produced, not including the
2172 Null-terminator.
2173 **/
2174 UINTN
2175 EFIAPI
2176 SPrintLength (
2177 IN CONST CHAR16 *FormatString,
2178 IN VA_LIST Marker
2179 )
2180 {
2181 ASSERT_UNICODE_BUFFER (FormatString);
2182 return InternalPrintLibSPrintMarker (NULL, 0, FORMAT_UNICODE | OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);
2183 }
2184
2185 /**
2186 Returns the number of characters that would be produced by if the formatted
2187 output were produced not including the Null-terminator.
2188
2189 If FormatString is NULL, then ASSERT() and 0 is returned.
2190 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more
2191 than PcdMaximumAsciiStringLength Ascii characters not including the
2192 Null-terminator, then ASSERT() and 0 is returned.
2193
2194 @param[in] FormatString A Null-terminated ASCII format string.
2195 @param[in] Marker VA_LIST marker for the variable argument list.
2196
2197 @return The number of characters that would be produced, not including the
2198 Null-terminator.
2199 **/
2200 UINTN
2201 EFIAPI
2202 SPrintLengthAsciiFormat (
2203 IN CONST CHAR8 *FormatString,
2204 IN VA_LIST Marker
2205 )
2206 {
2207 return InternalPrintLibSPrintMarker (NULL, 0, OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);
2208 }