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