]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c
MdeModulePkg/PrintLib: Refine the SPrint functions
[mirror_edk2.git] / MdeModulePkg / Library / DxePrintLibPrint2Protocol / PrintLib.c
1 /** @file
2 Instance of Print Library based on gEfiPrint2ProtocolGuid.
3
4 Implement the print library instance by wrap the interface
5 provided in the Print2 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_PRINT2_PROTOCOL *mPrint2Protocol = NULL;
47
48 /**
49 The constructor function caches the pointer to Print2 protocol.
50
51 The constructor function locates Print2 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 &gEfiPrint2ProtocolGuid,
71 NULL,
72 (VOID**) &mPrint2Protocol
73 );
74 ASSERT_EFI_ERROR (Status);
75 ASSERT (mPrint2Protocol != 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 mPrint2Protocol->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 mPrint2Protocol->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 mPrint2Protocol->UnicodeValueToString (Buffer, Flags, Value, Width);
652 }
653
654 /**
655 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
656 ASCII format string and a VA_LIST argument list.
657
658 This function is similar as vsnprintf_s defined in C11.
659
660 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
661 and BufferSize.
662 The ASCII string is produced by parsing the format string specified by FormatString.
663 Arguments are pulled from the variable argument list specified by Marker based on
664 the contents of the format string.
665 The number of ASCII characters in the produced output buffer is returned not including
666 the Null-terminator.
667
668 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
669 unmodified and 0 is returned.
670 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
671 unmodified and 0 is returned.
672 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
673 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
674 is unmodified and 0 is returned.
675 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
676 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
677 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
678
679 If BufferSize is 0, then no output buffer is produced and 0 is returned.
680
681 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
682 ASCII string.
683 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
684 @param FormatString A Null-terminated ASCII format string.
685 @param Marker VA_LIST marker for the variable argument list.
686
687 @return The number of ASCII characters in the produced output buffer not including the
688 Null-terminator.
689
690 **/
691 UINTN
692 EFIAPI
693 AsciiVSPrint (
694 OUT CHAR8 *StartOfBuffer,
695 IN UINTN BufferSize,
696 IN CONST CHAR8 *FormatString,
697 IN VA_LIST Marker
698 )
699 {
700 UINT64 BaseListMarker[256 / sizeof (UINT64)];
701 BOOLEAN Converted;
702
703 Converted = DxePrintLibPrint2ProtocolVaListToBaseList (
704 TRUE,
705 FormatString,
706 Marker,
707 (BASE_LIST)BaseListMarker,
708 sizeof (BaseListMarker) - 8
709 );
710 if (!Converted) {
711 return 0;
712 }
713
714 return AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);
715 }
716
717 /**
718 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
719 ASCII format string and a BASE_LIST argument list.
720
721 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
722 and BufferSize.
723 The ASCII string is produced by parsing the format string specified by FormatString.
724 Arguments are pulled from the variable argument list specified by Marker based on
725 the contents of the format string.
726 The number of ASCII characters in the produced output buffer is returned not including
727 the Null-terminator.
728
729 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
730 unmodified and 0 is returned.
731 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
732 unmodified and 0 is returned.
733 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
734 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
735 is unmodified and 0 is returned.
736 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
737 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
738 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
739
740 If BufferSize is 0, then no output buffer is produced and 0 is returned.
741
742 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
743 ASCII string.
744 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
745 @param FormatString A Null-terminated ASCII format string.
746 @param Marker BASE_LIST marker for the variable argument list.
747
748 @return The number of ASCII characters in the produced output buffer not including the
749 Null-terminator.
750
751 **/
752 UINTN
753 EFIAPI
754 AsciiBSPrint (
755 OUT CHAR8 *StartOfBuffer,
756 IN UINTN BufferSize,
757 IN CONST CHAR8 *FormatString,
758 IN BASE_LIST Marker
759 )
760 {
761 return mPrint2Protocol->AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
762 }
763
764 /**
765 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
766 ASCII format string and variable argument list.
767
768 This function is similar as snprintf_s defined in C11.
769
770 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
771 and BufferSize.
772 The ASCII string is produced by parsing the format string specified by FormatString.
773 Arguments are pulled from the variable argument list based on the contents of the
774 format string.
775 The number of ASCII characters in the produced output buffer is returned not including
776 the Null-terminator.
777
778 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
779 unmodified and 0 is returned.
780 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
781 unmodified and 0 is returned.
782 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
783 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
784 is unmodified and 0 is returned.
785 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than
786 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then
787 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
788
789 If BufferSize is 0, then no output buffer is produced and 0 is returned.
790
791 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
792 ASCII string.
793 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
794 @param FormatString A Null-terminated ASCII format string.
795 @param ... Variable argument list whose contents are accessed based on the
796 format string specified by FormatString.
797
798 @return The number of ASCII characters in the produced output buffer not including the
799 Null-terminator.
800
801 **/
802 UINTN
803 EFIAPI
804 AsciiSPrint (
805 OUT CHAR8 *StartOfBuffer,
806 IN UINTN BufferSize,
807 IN CONST CHAR8 *FormatString,
808 ...
809 )
810 {
811 VA_LIST Marker;
812 UINTN NumberOfPrinted;
813
814 VA_START (Marker, FormatString);
815 NumberOfPrinted = AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
816 VA_END (Marker);
817 return NumberOfPrinted;
818 }
819
820 /**
821 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
822 Unicode format string and a VA_LIST argument list.
823
824 This function is similar as vsnprintf_s defined in C11.
825
826 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
827 and BufferSize.
828 The ASCII string is produced by parsing the format string specified by FormatString.
829 Arguments are pulled from the variable argument list specified by Marker based on
830 the contents of the format string.
831 The number of ASCII characters in the produced output buffer is returned not including
832 the Null-terminator.
833
834 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
835
836 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
837 unmodified and 0 is returned.
838 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
839 unmodified and 0 is returned.
840 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
841 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
842 is unmodified and 0 is returned.
843 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
844 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
845 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
846
847 If BufferSize is 0, then no output buffer is produced and 0 is returned.
848
849 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
850 ASCII string.
851 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
852 @param FormatString A Null-terminated Unicode format string.
853 @param Marker VA_LIST marker for the variable argument list.
854
855 @return The number of ASCII characters in the produced output buffer not including the
856 Null-terminator.
857
858 **/
859 UINTN
860 EFIAPI
861 AsciiVSPrintUnicodeFormat (
862 OUT CHAR8 *StartOfBuffer,
863 IN UINTN BufferSize,
864 IN CONST CHAR16 *FormatString,
865 IN VA_LIST Marker
866 )
867 {
868 UINT64 BaseListMarker[256 / sizeof (UINT64)];
869 BOOLEAN Converted;
870
871 ASSERT_UNICODE_BUFFER (FormatString);
872
873 Converted = DxePrintLibPrint2ProtocolVaListToBaseList (
874 FALSE,
875 (CHAR8 *)FormatString,
876 Marker,
877 (BASE_LIST)BaseListMarker,
878 sizeof (BaseListMarker) - 8
879 );
880 if (!Converted) {
881 return 0;
882 }
883
884 return AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);
885 }
886
887 /**
888 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
889 Unicode format string and a BASE_LIST argument list.
890
891 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
892 and BufferSize.
893 The ASCII string is produced by parsing the format string specified by FormatString.
894 Arguments are pulled from the variable argument list specified by Marker based on
895 the contents of the format string.
896 The number of ASCII characters in the produced output buffer is returned not including
897 the Null-terminator.
898
899 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
900
901 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
902 unmodified and 0 is returned.
903 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
904 unmodified and 0 is returned.
905 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
906 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
907 is unmodified and 0 is returned.
908 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
909 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
910 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
911
912 If BufferSize is 0, then no output buffer is produced and 0 is returned.
913
914 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
915 ASCII string.
916 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
917 @param FormatString A Null-terminated Unicode format string.
918 @param Marker BASE_LIST marker for the variable argument list.
919
920 @return The number of ASCII characters in the produced output buffer not including the
921 Null-terminator.
922
923 **/
924 UINTN
925 EFIAPI
926 AsciiBSPrintUnicodeFormat (
927 OUT CHAR8 *StartOfBuffer,
928 IN UINTN BufferSize,
929 IN CONST CHAR16 *FormatString,
930 IN BASE_LIST Marker
931 )
932 {
933 ASSERT_UNICODE_BUFFER (FormatString);
934 return mPrint2Protocol->AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);
935 }
936
937 /**
938 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated
939 Unicode format string and variable argument list.
940
941 This function is similar as snprintf_s defined in C11.
942
943 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer
944 and BufferSize.
945 The ASCII string is produced by parsing the format string specified by FormatString.
946 Arguments are pulled from the variable argument list based on the contents of the
947 format string.
948 The number of ASCII characters in the produced output buffer is returned not including
949 the Null-terminator.
950
951 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
952
953 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is
954 unmodified and 0 is returned.
955 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is
956 unmodified and 0 is returned.
957 If PcdMaximumAsciiStringLength is not zero, and BufferSize >
958 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer
959 is unmodified and 0 is returned.
960 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than
961 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then
962 ASSERT(). Also, the output buffer is unmodified and 0 is returned.
963
964 If BufferSize is 0, then no output buffer is produced and 0 is returned.
965
966 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
967 ASCII string.
968 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
969 @param FormatString A Null-terminated Unicode format string.
970 @param ... Variable argument list whose contents are accessed based on the
971 format string specified by FormatString.
972
973 @return The number of ASCII characters in the produced output buffer not including the
974 Null-terminator.
975
976 **/
977 UINTN
978 EFIAPI
979 AsciiSPrintUnicodeFormat (
980 OUT CHAR8 *StartOfBuffer,
981 IN UINTN BufferSize,
982 IN CONST CHAR16 *FormatString,
983 ...
984 )
985 {
986 VA_LIST Marker;
987 UINTN NumberOfPrinted;
988
989 VA_START (Marker, FormatString);
990 NumberOfPrinted = AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);
991 VA_END (Marker);
992 return NumberOfPrinted;
993 }
994
995
996 /**
997 Converts a decimal value to a Null-terminated ASCII string.
998
999 Converts the decimal number specified by Value to a Null-terminated ASCII string
1000 specified by Buffer containing at most Width characters. No padding of spaces
1001 is ever performed.
1002 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
1003 The number of ASCII characters in Buffer is returned not including the Null-terminator.
1004 If the conversion contains more than Width characters, then only the first Width
1005 characters are returned, and the total number of characters required to perform
1006 the conversion is returned.
1007 Additional conversion parameters are specified in Flags.
1008 The Flags bit LEFT_JUSTIFY is always ignored.
1009 All conversions are left justified in Buffer.
1010 If Width is 0, PREFIX_ZERO is ignored in Flags.
1011 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
1012 are inserted every 3rd digit starting from the right.
1013 If RADIX_HEX is set in Flags, then the output buffer will be
1014 formatted in hexadecimal format.
1015 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in Buffer is a '-'.
1016 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
1017 then Buffer is padded with '0' characters so the combination of the optional '-'
1018 sign character, '0' characters, digit characters for Value, and the Null-terminator
1019 add up to Width characters.
1020
1021 If Buffer is NULL, then ASSERT().
1022 If unsupported bits are set in Flags, then ASSERT().
1023 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().
1024 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
1025
1026 @param Buffer Pointer to the output buffer for the produced Null-terminated
1027 ASCII string.
1028 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
1029 @param Value The 64-bit signed value to convert to a string.
1030 @param Width The maximum number of ASCII characters to place in Buffer, not including
1031 the Null-terminator.
1032
1033 @return The number of ASCII characters in Buffer not including the Null-terminator.
1034
1035 **/
1036 UINTN
1037 EFIAPI
1038 AsciiValueToString (
1039 OUT CHAR8 *Buffer,
1040 IN UINTN Flags,
1041 IN INT64 Value,
1042 IN UINTN Width
1043 )
1044 {
1045 return mPrint2Protocol->AsciiValueToString (Buffer, Flags, Value, Width);
1046 }
1047
1048 #define PREFIX_SIGN BIT1
1049 #define PREFIX_BLANK BIT2
1050 #define LONG_TYPE BIT4
1051 #define OUTPUT_UNICODE BIT6
1052 #define FORMAT_UNICODE BIT8
1053 #define PAD_TO_WIDTH BIT9
1054 #define ARGUMENT_UNICODE BIT10
1055 #define PRECISION BIT11
1056 #define ARGUMENT_REVERSED BIT12
1057 #define COUNT_ONLY_NO_PRINT BIT13
1058 #define UNSIGNED_TYPE BIT14
1059
1060 //
1061 // Record date and time information
1062 //
1063 typedef struct {
1064 UINT16 Year;
1065 UINT8 Month;
1066 UINT8 Day;
1067 UINT8 Hour;
1068 UINT8 Minute;
1069 UINT8 Second;
1070 UINT8 Pad1;
1071 UINT32 Nanosecond;
1072 INT16 TimeZone;
1073 UINT8 Daylight;
1074 UINT8 Pad2;
1075 } TIME;
1076
1077 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
1078
1079 /**
1080 Internal function that convert a number to a string in Buffer.
1081
1082 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.
1083
1084 @param Buffer Location to place the ASCII string of Value.
1085 @param Value The value to convert to a Decimal or Hexadecimal string in Buffer.
1086 @param Radix Radix of the value
1087
1088 @return A pointer to the end of buffer filled with ASCII string.
1089
1090 **/
1091 CHAR8 *
1092 InternalPrintLibValueToString (
1093 IN OUT CHAR8 *Buffer,
1094 IN INT64 Value,
1095 IN UINTN Radix
1096 )
1097 {
1098 UINT32 Remainder;
1099
1100 //
1101 // Loop to convert one digit at a time in reverse order
1102 //
1103 *Buffer = 0;
1104 do {
1105 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);
1106 *(++Buffer) = mHexStr[Remainder];
1107 } while (Value != 0);
1108
1109 //
1110 // Return pointer of the end of filled buffer.
1111 //
1112 return Buffer;
1113 }
1114
1115 /**
1116 Worker function that produces a Null-terminated string in an output buffer
1117 based on a Null-terminated format string and a VA_LIST argument list.
1118
1119 VSPrint function to process format and place the results in Buffer. Since a
1120 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1121 this is the main print working routine.
1122
1123 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.
1124
1125 @param[out] Buffer The character buffer to print the results of the
1126 parsing of Format into.
1127 @param[in] BufferSize The maximum number of characters to put into
1128 buffer.
1129 @param[in] Flags Initial flags value.
1130 Can only have FORMAT_UNICODE, OUTPUT_UNICODE,
1131 and COUNT_ONLY_NO_PRINT set.
1132 @param[in] Format A Null-terminated format string.
1133 @param[in] VaListMarker VA_LIST style variable argument list consumed by
1134 processing Format.
1135 @param[in] BaseListMarker BASE_LIST style variable argument list consumed
1136 by processing Format.
1137
1138 @return The number of characters printed not including the Null-terminator.
1139 If COUNT_ONLY_NO_PRINT was set returns the same, but without any
1140 modification to Buffer.
1141
1142 **/
1143 UINTN
1144 InternalPrintLibSPrintMarker (
1145 OUT CHAR8 *Buffer,
1146 IN UINTN BufferSize,
1147 IN UINTN Flags,
1148 IN CONST CHAR8 *Format,
1149 IN VA_LIST VaListMarker, OPTIONAL
1150 IN BASE_LIST BaseListMarker OPTIONAL
1151 );
1152
1153 /**
1154 Worker function that produces a Null-terminated string in an output buffer
1155 based on a Null-terminated format string and variable argument list.
1156
1157 VSPrint function to process format and place the results in Buffer. Since a
1158 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1159 this is the main print working routine
1160
1161 @param StartOfBuffer The character buffer to print the results of the parsing
1162 of Format into.
1163 @param BufferSize The maximum number of characters to put into buffer.
1164 Zero means no limit.
1165 @param Flags Initial flags value.
1166 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set
1167 @param FormatString A Null-terminated format string.
1168 @param ... The variable argument list.
1169
1170 @return The number of characters printed.
1171
1172 **/
1173 UINTN
1174 EFIAPI
1175 InternalPrintLibSPrint (
1176 OUT CHAR8 *StartOfBuffer,
1177 IN UINTN BufferSize,
1178 IN UINTN Flags,
1179 IN CONST CHAR8 *FormatString,
1180 ...
1181 )
1182 {
1183 VA_LIST Marker;
1184 UINTN NumberOfPrinted;
1185
1186 VA_START (Marker, FormatString);
1187 NumberOfPrinted = InternalPrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);
1188 VA_END (Marker);
1189 return NumberOfPrinted;
1190 }
1191
1192 #define WARNING_STATUS_NUMBER 5
1193 #define ERROR_STATUS_NUMBER 33
1194
1195 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 * CONST mStatusString[] = {
1196 "Success", // RETURN_SUCCESS = 0
1197 "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH = 1
1198 "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE = 2
1199 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE = 3
1200 "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL = 4
1201 "Warning Stale Data", // RETURN_WARN_STALE_DATA = 5
1202 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT
1203 "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT
1204 "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT
1205 "Bad Buffer Size", // RETURN_BAD_BUFFER_SIZE = 4 | MAX_BIT
1206 "Buffer Too Small", // RETURN_BUFFER_TOO_SMALL, = 5 | MAX_BIT
1207 "Not Ready", // RETURN_NOT_READY = 6 | MAX_BIT
1208 "Device Error", // RETURN_DEVICE_ERROR = 7 | MAX_BIT
1209 "Write Protected", // RETURN_WRITE_PROTECTED = 8 | MAX_BIT
1210 "Out of Resources", // RETURN_OUT_OF_RESOURCES = 9 | MAX_BIT
1211 "Volume Corrupt", // RETURN_VOLUME_CORRUPTED = 10 | MAX_BIT
1212 "Volume Full", // RETURN_VOLUME_FULL = 11 | MAX_BIT
1213 "No Media", // RETURN_NO_MEDIA = 12 | MAX_BIT
1214 "Media changed", // RETURN_MEDIA_CHANGED = 13 | MAX_BIT
1215 "Not Found", // RETURN_NOT_FOUND = 14 | MAX_BIT
1216 "Access Denied", // RETURN_ACCESS_DENIED = 15 | MAX_BIT
1217 "No Response", // RETURN_NO_RESPONSE = 16 | MAX_BIT
1218 "No mapping", // RETURN_NO_MAPPING = 17 | MAX_BIT
1219 "Time out", // RETURN_TIMEOUT = 18 | MAX_BIT
1220 "Not started", // RETURN_NOT_STARTED = 19 | MAX_BIT
1221 "Already started", // RETURN_ALREADY_STARTED = 20 | MAX_BIT
1222 "Aborted", // RETURN_ABORTED = 21 | MAX_BIT
1223 "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT
1224 "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT
1225 "Protocol Error", // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT
1226 "Incompatible Version", // RETURN_INCOMPATIBLE_VERSION = 25 | MAX_BIT
1227 "Security Violation", // RETURN_SECURITY_VIOLATION = 26 | MAX_BIT
1228 "CRC Error", // RETURN_CRC_ERROR = 27 | MAX_BIT
1229 "End of Media", // RETURN_END_OF_MEDIA = 28 | MAX_BIT
1230 "Reserved (29)", // RESERVED = 29 | MAX_BIT
1231 "Reserved (30)", // RESERVED = 30 | MAX_BIT
1232 "End of File", // RETURN_END_OF_FILE = 31 | MAX_BIT
1233 "Invalid Language", // RETURN_INVALID_LANGUAGE = 32 | MAX_BIT
1234 "Compromised Data" // RETURN_COMPROMISED_DATA = 33 | MAX_BIT
1235 };
1236
1237 /**
1238 Internal function that places the character into the Buffer.
1239
1240 Internal function that places ASCII or Unicode character into the Buffer.
1241
1242 @param Buffer The buffer to place the Unicode or ASCII string.
1243 @param EndBuffer The end of the input Buffer. No characters will be
1244 placed after that.
1245 @param Length The count of character to be placed into Buffer.
1246 (Negative value indicates no buffer fill.)
1247 @param Character The character to be placed into Buffer.
1248 @param Increment The character increment in Buffer.
1249
1250 @return Buffer.
1251
1252 **/
1253 CHAR8 *
1254 InternalPrintLibFillBuffer (
1255 OUT CHAR8 *Buffer,
1256 IN CHAR8 *EndBuffer,
1257 IN INTN Length,
1258 IN UINTN Character,
1259 IN INTN Increment
1260 )
1261 {
1262 INTN Index;
1263
1264 for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {
1265 *Buffer = (CHAR8) Character;
1266 if (Increment != 1) {
1267 *(Buffer + 1) = (CHAR8)(Character >> 8);
1268 }
1269 Buffer += Increment;
1270 }
1271
1272 return Buffer;
1273 }
1274
1275 /**
1276 Worker function that produces a Null-terminated string in an output buffer
1277 based on a Null-terminated format string and a VA_LIST argument list.
1278
1279 VSPrint function to process format and place the results in Buffer. Since a
1280 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
1281 this is the main print working routine.
1282
1283 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.
1284
1285 @param[out] Buffer The character buffer to print the results of the
1286 parsing of Format into.
1287 @param[in] BufferSize The maximum number of characters to put into
1288 buffer.
1289 @param[in] Flags Initial flags value.
1290 Can only have FORMAT_UNICODE, OUTPUT_UNICODE,
1291 and COUNT_ONLY_NO_PRINT set.
1292 @param[in] Format A Null-terminated format string.
1293 @param[in] VaListMarker VA_LIST style variable argument list consumed by
1294 processing Format.
1295 @param[in] BaseListMarker BASE_LIST style variable argument list consumed
1296 by processing Format.
1297
1298 @return The number of characters printed not including the Null-terminator.
1299 If COUNT_ONLY_NO_PRINT was set returns the same, but without any
1300 modification to Buffer.
1301
1302 **/
1303 UINTN
1304 InternalPrintLibSPrintMarker (
1305 OUT CHAR8 *Buffer,
1306 IN UINTN BufferSize,
1307 IN UINTN Flags,
1308 IN CONST CHAR8 *Format,
1309 IN VA_LIST VaListMarker, OPTIONAL
1310 IN BASE_LIST BaseListMarker OPTIONAL
1311 )
1312 {
1313 CHAR8 *OriginalBuffer;
1314 CHAR8 *EndBuffer;
1315 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
1316 UINT32 BytesPerOutputCharacter;
1317 UINTN BytesPerFormatCharacter;
1318 UINTN FormatMask;
1319 UINTN FormatCharacter;
1320 UINTN Width;
1321 UINTN Precision;
1322 INT64 Value;
1323 CONST CHAR8 *ArgumentString;
1324 UINTN Character;
1325 GUID *TmpGuid;
1326 TIME *TmpTime;
1327 UINTN Count;
1328 UINTN ArgumentMask;
1329 INTN BytesPerArgumentCharacter;
1330 UINTN ArgumentCharacter;
1331 BOOLEAN Done;
1332 UINTN Index;
1333 CHAR8 Prefix;
1334 BOOLEAN ZeroPad;
1335 BOOLEAN Comma;
1336 UINTN Digits;
1337 UINTN Radix;
1338 RETURN_STATUS Status;
1339 UINT32 GuidData1;
1340 UINT16 GuidData2;
1341 UINT16 GuidData3;
1342 UINTN LengthToReturn;
1343
1344 //
1345 // If you change this code be sure to match the 2 versions of this function.
1346 // Nearly identical logic is found in the BasePrintLib and
1347 // DxePrintLibPrint2Protocol (both PrintLib instances).
1348 //
1349
1350 //
1351 // 1. Buffer shall not be a null pointer when both BufferSize > 0 and
1352 // COUNT_ONLY_NO_PRINT is not set in Flags.
1353 //
1354 if ((BufferSize > 0) && ((Flags & COUNT_ONLY_NO_PRINT) == 0)) {
1355 SAFE_PRINT_CONSTRAINT_CHECK ((Buffer != NULL), 0);
1356 }
1357
1358 //
1359 // 2. Format shall not be a null pointer when BufferSize > 0 or when
1360 // COUNT_ONLY_NO_PRINT is set in Flags.
1361 //
1362 if ((BufferSize > 0) || ((Flags & COUNT_ONLY_NO_PRINT) != 0)) {
1363 SAFE_PRINT_CONSTRAINT_CHECK ((Format != NULL), 0);
1364 }
1365
1366 //
1367 // 3. BufferSize shall not be greater than RSIZE_MAX for Unicode output or
1368 // ASCII_RSIZE_MAX for Ascii output.
1369 //
1370 if ((Flags & OUTPUT_UNICODE) != 0) {
1371 if (RSIZE_MAX != 0) {
1372 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= RSIZE_MAX), 0);
1373 }
1374 BytesPerOutputCharacter = 2;
1375 } else {
1376 if (ASCII_RSIZE_MAX != 0) {
1377 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= ASCII_RSIZE_MAX), 0);
1378 }
1379 BytesPerOutputCharacter = 1;
1380 }
1381
1382 //
1383 // 4. Format shall not contain more than RSIZE_MAX Unicode characters or
1384 // ASCII_RSIZE_MAX Ascii characters.
1385 //
1386 if ((Flags & FORMAT_UNICODE) != 0) {
1387 if (RSIZE_MAX != 0) {
1388 SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16 *)Format, RSIZE_MAX + 1) <= RSIZE_MAX), 0);
1389 }
1390 BytesPerFormatCharacter = 2;
1391 FormatMask = 0xffff;
1392 } else {
1393 if (ASCII_RSIZE_MAX != 0) {
1394 SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), 0);
1395 }
1396 BytesPerFormatCharacter = 1;
1397 FormatMask = 0xff;
1398 }
1399
1400 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {
1401 if (BufferSize == 0) {
1402 Buffer = NULL;
1403 }
1404 } else {
1405 //
1406 // We can run without a Buffer for counting only.
1407 //
1408 if (BufferSize == 0) {
1409 return 0;
1410 }
1411 }
1412
1413 LengthToReturn = 0;
1414 EndBuffer = NULL;
1415 OriginalBuffer = NULL;
1416
1417 //
1418 // Reserve space for the Null terminator.
1419 //
1420 if (Buffer != NULL) {
1421 BufferSize--;
1422 OriginalBuffer = Buffer;
1423
1424 //
1425 // Set the tag for the end of the input Buffer.
1426 //
1427 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;
1428 }
1429
1430 //
1431 // Get the first character from the format string
1432 //
1433 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1434
1435 //
1436 // Loop until the end of the format string is reached or the output buffer is full
1437 //
1438 while (FormatCharacter != 0) {
1439 if ((Buffer != NULL) && (Buffer >= EndBuffer)) {
1440 break;
1441 }
1442 //
1443 // Clear all the flag bits except those that may have been passed in
1444 //
1445 Flags &= (UINTN) (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);
1446
1447 //
1448 // Set the default width to zero, and the default precision to 1
1449 //
1450 Width = 0;
1451 Precision = 1;
1452 Prefix = 0;
1453 Comma = FALSE;
1454 ZeroPad = FALSE;
1455 Count = 0;
1456 Digits = 0;
1457
1458 switch (FormatCharacter) {
1459 case '%':
1460 //
1461 // Parse Flags and Width
1462 //
1463 for (Done = FALSE; !Done; ) {
1464 Format += BytesPerFormatCharacter;
1465 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1466 switch (FormatCharacter) {
1467 case '.':
1468 Flags |= PRECISION;
1469 break;
1470 case '-':
1471 Flags |= LEFT_JUSTIFY;
1472 break;
1473 case '+':
1474 Flags |= PREFIX_SIGN;
1475 break;
1476 case ' ':
1477 Flags |= PREFIX_BLANK;
1478 break;
1479 case ',':
1480 Flags |= COMMA_TYPE;
1481 break;
1482 case 'L':
1483 case 'l':
1484 Flags |= LONG_TYPE;
1485 break;
1486 case '*':
1487 if ((Flags & PRECISION) == 0) {
1488 Flags |= PAD_TO_WIDTH;
1489 if (BaseListMarker == NULL) {
1490 Width = VA_ARG (VaListMarker, UINTN);
1491 } else {
1492 Width = BASE_ARG (BaseListMarker, UINTN);
1493 }
1494 } else {
1495 if (BaseListMarker == NULL) {
1496 Precision = VA_ARG (VaListMarker, UINTN);
1497 } else {
1498 Precision = BASE_ARG (BaseListMarker, UINTN);
1499 }
1500 }
1501 break;
1502 case '0':
1503 if ((Flags & PRECISION) == 0) {
1504 Flags |= PREFIX_ZERO;
1505 }
1506 case '1':
1507 case '2':
1508 case '3':
1509 case '4':
1510 case '5':
1511 case '6':
1512 case '7':
1513 case '8':
1514 case '9':
1515 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){
1516 Count = (Count * 10) + FormatCharacter - '0';
1517 Format += BytesPerFormatCharacter;
1518 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1519 }
1520 Format -= BytesPerFormatCharacter;
1521 if ((Flags & PRECISION) == 0) {
1522 Flags |= PAD_TO_WIDTH;
1523 Width = Count;
1524 } else {
1525 Precision = Count;
1526 }
1527 break;
1528
1529 case '\0':
1530 //
1531 // Make no output if Format string terminates unexpectedly when
1532 // looking up for flag, width, precision and type.
1533 //
1534 Format -= BytesPerFormatCharacter;
1535 Precision = 0;
1536 //
1537 // break skipped on purpose.
1538 //
1539 default:
1540 Done = TRUE;
1541 break;
1542 }
1543 }
1544
1545 //
1546 // Handle each argument type
1547 //
1548 switch (FormatCharacter) {
1549 case 'p':
1550 //
1551 // Flag space, +, 0, L & l are invalid for type p.
1552 //
1553 Flags &= ~((UINTN) (PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE));
1554 if (sizeof (VOID *) > 4) {
1555 Flags |= LONG_TYPE;
1556 }
1557 //
1558 // break skipped on purpose
1559 //
1560 case 'X':
1561 Flags |= PREFIX_ZERO;
1562 //
1563 // break skipped on purpose
1564 //
1565 case 'x':
1566 Flags |= RADIX_HEX;
1567 //
1568 // break skipped on purpose
1569 //
1570 case 'u':
1571 if ((Flags & RADIX_HEX) == 0) {
1572 Flags &= ~((UINTN) (PREFIX_SIGN));
1573 Flags |= UNSIGNED_TYPE;
1574 }
1575 //
1576 // break skipped on purpose
1577 //
1578 case 'd':
1579 if ((Flags & LONG_TYPE) == 0) {
1580 //
1581 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
1582 // This assumption is made so the format string definition is compatible with the ANSI C
1583 // Specification for formatted strings. It is recommended that the Base Types be used
1584 // everywhere, but in this one case, compliance with ANSI C is more important, and
1585 // provides an implementation that is compatible with that largest possible set of CPU
1586 // architectures. This is why the type "int" is used in this one case.
1587 //
1588 if (BaseListMarker == NULL) {
1589 Value = VA_ARG (VaListMarker, int);
1590 } else {
1591 Value = BASE_ARG (BaseListMarker, int);
1592 }
1593 } else {
1594 if (BaseListMarker == NULL) {
1595 Value = VA_ARG (VaListMarker, INT64);
1596 } else {
1597 Value = BASE_ARG (BaseListMarker, INT64);
1598 }
1599 }
1600 if ((Flags & PREFIX_BLANK) != 0) {
1601 Prefix = ' ';
1602 }
1603 if ((Flags & PREFIX_SIGN) != 0) {
1604 Prefix = '+';
1605 }
1606 if ((Flags & COMMA_TYPE) != 0) {
1607 Comma = TRUE;
1608 }
1609 if ((Flags & RADIX_HEX) == 0) {
1610 Radix = 10;
1611 if (Comma) {
1612 Flags &= ~((UINTN) PREFIX_ZERO);
1613 Precision = 1;
1614 }
1615 if (Value < 0 && (Flags & UNSIGNED_TYPE) == 0) {
1616 Flags |= PREFIX_SIGN;
1617 Prefix = '-';
1618 Value = -Value;
1619 } else if ((Flags & UNSIGNED_TYPE) != 0 && (Flags & LONG_TYPE) == 0) {
1620 //
1621 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
1622 // This assumption is made so the format string definition is compatible with the ANSI C
1623 // Specification for formatted strings. It is recommended that the Base Types be used
1624 // everywhere, but in this one case, compliance with ANSI C is more important, and
1625 // provides an implementation that is compatible with that largest possible set of CPU
1626 // architectures. This is why the type "unsigned int" is used in this one case.
1627 //
1628 Value = (unsigned int)Value;
1629 }
1630 } else {
1631 Radix = 16;
1632 Comma = FALSE;
1633 if ((Flags & LONG_TYPE) == 0 && Value < 0) {
1634 //
1635 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
1636 // This assumption is made so the format string definition is compatible with the ANSI C
1637 // Specification for formatted strings. It is recommended that the Base Types be used
1638 // everywhere, but in this one case, compliance with ANSI C is more important, and
1639 // provides an implementation that is compatible with that largest possible set of CPU
1640 // architectures. This is why the type "unsigned int" is used in this one case.
1641 //
1642 Value = (unsigned int)Value;
1643 }
1644 }
1645 //
1646 // Convert Value to a reversed string
1647 //
1648 Count = InternalPrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;
1649 if (Value == 0 && Precision == 0) {
1650 Count = 0;
1651 }
1652 ArgumentString = (CHAR8 *)ValueBuffer + Count;
1653
1654 Digits = Count % 3;
1655 if (Digits != 0) {
1656 Digits = 3 - Digits;
1657 }
1658 if (Comma && Count != 0) {
1659 Count += ((Count - 1) / 3);
1660 }
1661 if (Prefix != 0) {
1662 Count++;
1663 Precision++;
1664 }
1665 Flags |= ARGUMENT_REVERSED;
1666 ZeroPad = TRUE;
1667 if ((Flags & PREFIX_ZERO) != 0) {
1668 if ((Flags & LEFT_JUSTIFY) == 0) {
1669 if ((Flags & PAD_TO_WIDTH) != 0) {
1670 if ((Flags & PRECISION) == 0) {
1671 Precision = Width;
1672 }
1673 }
1674 }
1675 }
1676 break;
1677
1678 case 's':
1679 case 'S':
1680 Flags |= ARGUMENT_UNICODE;
1681 //
1682 // break skipped on purpose
1683 //
1684 case 'a':
1685 if (BaseListMarker == NULL) {
1686 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);
1687 } else {
1688 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);
1689 }
1690 if (ArgumentString == NULL) {
1691 Flags &= (~(UINTN)ARGUMENT_UNICODE);
1692 ArgumentString = "<null string>";
1693 }
1694 //
1695 // Set the default precision for string to be zero if not specified.
1696 //
1697 if ((Flags & PRECISION) == 0) {
1698 Precision = 0;
1699 }
1700 break;
1701
1702 case 'c':
1703 if (BaseListMarker == NULL) {
1704 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;
1705 } else {
1706 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;
1707 }
1708 ArgumentString = (CHAR8 *)&Character;
1709 Flags |= ARGUMENT_UNICODE;
1710 break;
1711
1712 case 'g':
1713 if (BaseListMarker == NULL) {
1714 TmpGuid = VA_ARG (VaListMarker, GUID *);
1715 } else {
1716 TmpGuid = BASE_ARG (BaseListMarker, GUID *);
1717 }
1718 if (TmpGuid == NULL) {
1719 ArgumentString = "<null guid>";
1720 } else {
1721 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));
1722 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));
1723 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));
1724 InternalPrintLibSPrint (
1725 ValueBuffer,
1726 MAXIMUM_VALUE_CHARACTERS,
1727 0,
1728 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1729 GuidData1,
1730 GuidData2,
1731 GuidData3,
1732 TmpGuid->Data4[0],
1733 TmpGuid->Data4[1],
1734 TmpGuid->Data4[2],
1735 TmpGuid->Data4[3],
1736 TmpGuid->Data4[4],
1737 TmpGuid->Data4[5],
1738 TmpGuid->Data4[6],
1739 TmpGuid->Data4[7]
1740 );
1741 ArgumentString = ValueBuffer;
1742 }
1743 break;
1744
1745 case 't':
1746 if (BaseListMarker == NULL) {
1747 TmpTime = VA_ARG (VaListMarker, TIME *);
1748 } else {
1749 TmpTime = BASE_ARG (BaseListMarker, TIME *);
1750 }
1751 if (TmpTime == NULL) {
1752 ArgumentString = "<null time>";
1753 } else {
1754 InternalPrintLibSPrint (
1755 ValueBuffer,
1756 MAXIMUM_VALUE_CHARACTERS,
1757 0,
1758 "%02d/%02d/%04d %02d:%02d",
1759 TmpTime->Month,
1760 TmpTime->Day,
1761 TmpTime->Year,
1762 TmpTime->Hour,
1763 TmpTime->Minute
1764 );
1765 ArgumentString = ValueBuffer;
1766 }
1767 break;
1768
1769 case 'r':
1770 if (BaseListMarker == NULL) {
1771 Status = VA_ARG (VaListMarker, RETURN_STATUS);
1772 } else {
1773 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);
1774 }
1775 ArgumentString = ValueBuffer;
1776 if (RETURN_ERROR (Status)) {
1777 //
1778 // Clear error bit
1779 //
1780 Index = Status & ~MAX_BIT;
1781 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {
1782 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];
1783 }
1784 } else {
1785 Index = Status;
1786 if (Index <= WARNING_STATUS_NUMBER) {
1787 ArgumentString = mStatusString [Index];
1788 }
1789 }
1790 if (ArgumentString == ValueBuffer) {
1791 InternalPrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);
1792 }
1793 break;
1794
1795 case '\r':
1796 Format += BytesPerFormatCharacter;
1797 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1798 if (FormatCharacter == '\n') {
1799 //
1800 // Translate '\r\n' to '\r\n'
1801 //
1802 ArgumentString = "\r\n";
1803 } else {
1804 //
1805 // Translate '\r' to '\r'
1806 //
1807 ArgumentString = "\r";
1808 Format -= BytesPerFormatCharacter;
1809 }
1810 break;
1811
1812 case '\n':
1813 //
1814 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1815 //
1816 ArgumentString = "\r\n";
1817 Format += BytesPerFormatCharacter;
1818 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1819 if (FormatCharacter != '\r') {
1820 Format -= BytesPerFormatCharacter;
1821 }
1822 break;
1823
1824 case '%':
1825 default:
1826 //
1827 // if the type is '%' or unknown, then print it to the screen
1828 //
1829 ArgumentString = (CHAR8 *)&FormatCharacter;
1830 Flags |= ARGUMENT_UNICODE;
1831 break;
1832 }
1833 break;
1834
1835 case '\r':
1836 Format += BytesPerFormatCharacter;
1837 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1838 if (FormatCharacter == '\n') {
1839 //
1840 // Translate '\r\n' to '\r\n'
1841 //
1842 ArgumentString = "\r\n";
1843 } else {
1844 //
1845 // Translate '\r' to '\r'
1846 //
1847 ArgumentString = "\r";
1848 Format -= BytesPerFormatCharacter;
1849 }
1850 break;
1851
1852 case '\n':
1853 //
1854 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
1855 //
1856 ArgumentString = "\r\n";
1857 Format += BytesPerFormatCharacter;
1858 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1859 if (FormatCharacter != '\r') {
1860 Format -= BytesPerFormatCharacter;
1861 }
1862 break;
1863
1864 default:
1865 ArgumentString = (CHAR8 *)&FormatCharacter;
1866 Flags |= ARGUMENT_UNICODE;
1867 break;
1868 }
1869
1870 //
1871 // Retrieve the ArgumentString attriubutes
1872 //
1873 if ((Flags & ARGUMENT_UNICODE) != 0) {
1874 ArgumentMask = 0xffff;
1875 BytesPerArgumentCharacter = 2;
1876 } else {
1877 ArgumentMask = 0xff;
1878 BytesPerArgumentCharacter = 1;
1879 }
1880 if ((Flags & ARGUMENT_REVERSED) != 0) {
1881 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;
1882 } else {
1883 //
1884 // Compute the number of characters in ArgumentString and store it in Count
1885 // ArgumentString is either null-terminated, or it contains Precision characters
1886 //
1887 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {
1888 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;
1889 if (ArgumentCharacter == 0) {
1890 break;
1891 }
1892 }
1893 }
1894
1895 if (Precision < Count) {
1896 Precision = Count;
1897 }
1898
1899 //
1900 // Pad before the string
1901 //
1902 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {
1903 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
1904 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1905 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
1906 }
1907 }
1908
1909 if (ZeroPad) {
1910 if (Prefix != 0) {
1911 LengthToReturn += (1 * BytesPerOutputCharacter);
1912 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1913 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
1914 }
1915 }
1916 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
1917 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1918 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);
1919 }
1920 } else {
1921 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
1922 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1923 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);
1924 }
1925 if (Prefix != 0) {
1926 LengthToReturn += (1 * BytesPerOutputCharacter);
1927 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1928 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
1929 }
1930 }
1931 }
1932
1933 //
1934 // Output the Prefix character if it is present
1935 //
1936 Index = 0;
1937 if (Prefix != 0) {
1938 Index++;
1939 }
1940
1941 //
1942 // Copy the string into the output buffer performing the required type conversions
1943 //
1944 while (Index < Count) {
1945 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;
1946
1947 LengthToReturn += (1 * BytesPerOutputCharacter);
1948 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1949 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);
1950 }
1951 ArgumentString += BytesPerArgumentCharacter;
1952 Index++;
1953 if (Comma) {
1954 Digits++;
1955 if (Digits == 3) {
1956 Digits = 0;
1957 Index++;
1958 if (Index < Count) {
1959 LengthToReturn += (1 * BytesPerOutputCharacter);
1960 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1961 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);
1962 }
1963 }
1964 }
1965 }
1966 }
1967
1968 //
1969 // Pad after the string
1970 //
1971 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {
1972 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
1973 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
1974 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
1975 }
1976 }
1977
1978 //
1979 // Get the next character from the format string
1980 //
1981 Format += BytesPerFormatCharacter;
1982
1983 //
1984 // Get the next character from the format string
1985 //
1986 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
1987 }
1988
1989 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {
1990 return (LengthToReturn / BytesPerOutputCharacter);
1991 }
1992
1993 ASSERT (Buffer != NULL);
1994 //
1995 // Null terminate the Unicode or ASCII string
1996 //
1997 InternalPrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);
1998
1999 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);
2000 }
2001
2002 /**
2003 Returns the number of characters that would be produced by if the formatted
2004 output were produced not including the Null-terminator.
2005
2006 If FormatString is not aligned on a 16-bit boundary, then ASSERT().
2007
2008 If FormatString is NULL, then ASSERT() and 0 is returned.
2009 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more
2010 than PcdMaximumUnicodeStringLength Unicode characters not including the
2011 Null-terminator, then ASSERT() and 0 is returned.
2012
2013 @param[in] FormatString A Null-terminated Unicode format string.
2014 @param[in] Marker VA_LIST marker for the variable argument list.
2015
2016 @return The number of characters that would be produced, not including the
2017 Null-terminator.
2018 **/
2019 UINTN
2020 EFIAPI
2021 SPrintLength (
2022 IN CONST CHAR16 *FormatString,
2023 IN VA_LIST Marker
2024 )
2025 {
2026 ASSERT_UNICODE_BUFFER (FormatString);
2027 return InternalPrintLibSPrintMarker (NULL, 0, FORMAT_UNICODE | OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);
2028 }
2029
2030 /**
2031 Returns the number of characters that would be produced by if the formatted
2032 output were produced not including the Null-terminator.
2033
2034 If FormatString is NULL, then ASSERT() and 0 is returned.
2035 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more
2036 than PcdMaximumAsciiStringLength Ascii characters not including the
2037 Null-terminator, then ASSERT() and 0 is returned.
2038
2039 @param[in] FormatString A Null-terminated ASCII format string.
2040 @param[in] Marker VA_LIST marker for the variable argument list.
2041
2042 @return The number of characters that would be produced, not including the
2043 Null-terminator.
2044 **/
2045 UINTN
2046 EFIAPI
2047 SPrintLengthAsciiFormat (
2048 IN CONST CHAR8 *FormatString,
2049 IN VA_LIST Marker
2050 )
2051 {
2052 return InternalPrintLibSPrintMarker (NULL, 0, OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);
2053 }