]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePrintLib/PrintLibInternal.c
Add the missing "," for the patch to fix status code print string.
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLibInternal.c
1 /** @file
2 Print Library internal worker functions.
3
4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "PrintLibInternal.h"
16
17 #define WARNING_STATUS_NUMBER 5
18 #define ERROR_STATUS_NUMBER 33
19
20 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
21
22 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mStatusString[] = {
23 "Success", // RETURN_SUCCESS = 0
24 "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH = 1
25 "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE = 2
26 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE = 3
27 "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL = 4
28 "Warning Stale Data", // RETURN_WARN_STALE_DATA = 5
29 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT
30 "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT
31 "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT
32 "Bad Buffer Size", // RETURN_BAD_BUFFER_SIZE = 4 | MAX_BIT
33 "Buffer Too Small", // RETURN_BUFFER_TOO_SMALL, = 5 | MAX_BIT
34 "Not Ready", // RETURN_NOT_READY = 6 | MAX_BIT
35 "Device Error", // RETURN_DEVICE_ERROR = 7 | MAX_BIT
36 "Write Protected", // RETURN_WRITE_PROTECTED = 8 | MAX_BIT
37 "Out of Resources", // RETURN_OUT_OF_RESOURCES = 9 | MAX_BIT
38 "Volume Corrupt", // RETURN_VOLUME_CORRUPTED = 10 | MAX_BIT
39 "Volume Full", // RETURN_VOLUME_FULL = 11 | MAX_BIT
40 "No Media", // RETURN_NO_MEDIA = 12 | MAX_BIT
41 "Media changed", // RETURN_MEDIA_CHANGED = 13 | MAX_BIT
42 "Not Found", // RETURN_NOT_FOUND = 14 | MAX_BIT
43 "Access Denied", // RETURN_ACCESS_DENIED = 15 | MAX_BIT
44 "No Response", // RETURN_NO_RESPONSE = 16 | MAX_BIT
45 "No mapping", // RETURN_NO_MAPPING = 17 | MAX_BIT
46 "Time out", // RETURN_TIMEOUT = 18 | MAX_BIT
47 "Not started", // RETURN_NOT_STARTED = 19 | MAX_BIT
48 "Already started", // RETURN_ALREADY_STARTED = 20 | MAX_BIT
49 "Aborted", // RETURN_ABORTED = 21 | MAX_BIT
50 "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT
51 "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT
52 "Protocol Error", // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT
53 "Incompatible Version", // RETURN_INCOMPATIBLE_VERSION = 25 | MAX_BIT
54 "Security Violation", // RETURN_SECURITY_VIOLATION = 26 | MAX_BIT
55 "CRC Error", // RETURN_CRC_ERROR = 27 | MAX_BIT
56 "End of Media", // RETURN_END_OF_MEDIA = 28 | MAX_BIT
57 "Reserved (29)", // RESERVED = 29 | MAX_BIT
58 "Reserved (30)", // RESERVED = 30 | MAX_BIT
59 "End of File", // RETURN_END_OF_FILE = 31 | MAX_BIT
60 "Invalid Language", // RETURN_INVALID_LANGUAGE = 32 | MAX_BIT
61 "Compromised Data" // RETURN_COMPROMISED_DATA = 33 | MAX_BIT
62 };
63
64
65 /**
66 Internal function that places the character into the Buffer.
67
68 Internal function that places ASCII or Unicode character into the Buffer.
69
70 @param Buffer The buffer to place the Unicode or ASCII string.
71 @param EndBuffer The end of the input Buffer. No characters will be
72 placed after that.
73 @param Length The count of character to be placed into Buffer.
74 (Negative value indicates no buffer fill.)
75 @param Character The character to be placed into Buffer.
76 @param Increment The character increment in Buffer.
77
78 @return Buffer.
79
80 **/
81 CHAR8 *
82 BasePrintLibFillBuffer (
83 OUT CHAR8 *Buffer,
84 IN CHAR8 *EndBuffer,
85 IN INTN Length,
86 IN UINTN Character,
87 IN INTN Increment
88 )
89 {
90 INTN Index;
91
92 for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {
93 *Buffer = (CHAR8) Character;
94 if (Increment != 1) {
95 *(Buffer + 1) = (CHAR8)(Character >> 8);
96 }
97 Buffer += Increment;
98 }
99
100 return Buffer;
101 }
102
103 /**
104 Internal function that convert a number to a string in Buffer.
105
106 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.
107
108 @param Buffer Location to place the ASCII string of Value.
109 @param Value The value to convert to a Decimal or Hexadecimal string in Buffer.
110 @param Radix Radix of the value
111
112 @return A pointer to the end of buffer filled with ASCII string.
113
114 **/
115 CHAR8 *
116 BasePrintLibValueToString (
117 IN OUT CHAR8 *Buffer,
118 IN INT64 Value,
119 IN UINTN Radix
120 )
121 {
122 UINT32 Remainder;
123
124 //
125 // Loop to convert one digit at a time in reverse order
126 //
127 *Buffer = 0;
128 do {
129 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);
130 *(++Buffer) = mHexStr[Remainder];
131 } while (Value != 0);
132
133 //
134 // Return pointer of the end of filled buffer.
135 //
136 return Buffer;
137 }
138
139 /**
140 Internal function that converts a decimal value to a Null-terminated string.
141
142 Converts the decimal number specified by Value to a Null-terminated
143 string specified by Buffer containing at most Width characters.
144 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
145 The total number of characters placed in Buffer is returned.
146 If the conversion contains more than Width characters, then only the first
147 Width characters are returned, and the total number of characters
148 required to perform the conversion is returned.
149 Additional conversion parameters are specified in Flags.
150 The Flags bit LEFT_JUSTIFY is always ignored.
151 All conversions are left justified in Buffer.
152 If Width is 0, PREFIX_ZERO is ignored in Flags.
153 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
154 are inserted every 3rd digit starting from the right.
155 If Value is < 0, then the fist character in Buffer is a '-'.
156 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
157 then Buffer is padded with '0' characters so the combination of the optional '-'
158 sign character, '0' characters, digit characters for Value, and the Null-terminator
159 add up to Width characters.
160
161 If Buffer is NULL, then ASSERT().
162 If unsupported bits are set in Flags, then ASSERT().
163 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
164
165 @param Buffer The pointer to the output buffer for the produced Null-terminated
166 string.
167 @param Flags The bitmask of flags that specify left justification, zero pad,
168 and commas.
169 @param Value The 64-bit signed value to convert to a string.
170 @param Width The maximum number of characters to place in Buffer, not including
171 the Null-terminator.
172 @param Increment The character increment in Buffer.
173
174 @return Total number of characters required to perform the conversion.
175
176 **/
177 UINTN
178 BasePrintLibConvertValueToString (
179 IN OUT CHAR8 *Buffer,
180 IN UINTN Flags,
181 IN INT64 Value,
182 IN UINTN Width,
183 IN UINTN Increment
184 )
185 {
186 CHAR8 *OriginalBuffer;
187 CHAR8 *EndBuffer;
188 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
189 CHAR8 *ValueBufferPtr;
190 UINTN Count;
191 UINTN Digits;
192 UINTN Index;
193 UINTN Radix;
194
195 //
196 // Make sure Buffer is not NULL and Width < MAXIMUM
197 //
198 ASSERT (Buffer != NULL);
199 ASSERT (Width < MAXIMUM_VALUE_CHARACTERS);
200 //
201 // Make sure Flags can only contain supported bits.
202 //
203 ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0);
204
205 //
206 // If both COMMA_TYPE and RADIX_HEX are set, then ASSERT ()
207 //
208 ASSERT (((Flags & COMMA_TYPE) == 0) || ((Flags & RADIX_HEX) == 0));
209
210 OriginalBuffer = Buffer;
211
212 //
213 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.
214 //
215 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {
216 Flags &= (~PREFIX_ZERO);
217 }
218 //
219 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
220 //
221 if (Width == 0) {
222 Width = MAXIMUM_VALUE_CHARACTERS - 1;
223 }
224 //
225 // Set the tag for the end of the input Buffer.
226 //
227 EndBuffer = Buffer + Width * Increment;
228
229 //
230 // Convert decimal negative
231 //
232 if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {
233 Value = -Value;
234 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);
235 Width--;
236 }
237
238 //
239 // Count the length of the value string.
240 //
241 Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;
242 ValueBufferPtr = BasePrintLibValueToString (ValueBuffer, Value, Radix);
243 Count = ValueBufferPtr - ValueBuffer;
244
245 //
246 // Append Zero
247 //
248 if ((Flags & PREFIX_ZERO) != 0) {
249 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);
250 }
251
252 //
253 // Print Comma type for every 3 characters
254 //
255 Digits = Count % 3;
256 if (Digits != 0) {
257 Digits = 3 - Digits;
258 }
259 for (Index = 0; Index < Count; Index++) {
260 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, *ValueBufferPtr--, Increment);
261 if ((Flags & COMMA_TYPE) != 0) {
262 Digits++;
263 if (Digits == 3) {
264 Digits = 0;
265 if ((Index + 1) < Count) {
266 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);
267 }
268 }
269 }
270 }
271
272 //
273 // Print Null-terminator
274 //
275 BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);
276
277 return ((Buffer - OriginalBuffer) / Increment);
278 }
279
280 /**
281 Worker function that produces a Null-terminated string in an output buffer
282 based on a Null-terminated format string and a VA_LIST argument list.
283
284 VSPrint function to process format and place the results in Buffer. Since a
285 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
286 this is the main print working routine.
287
288 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.
289
290 @param[out] Buffer The character buffer to print the results of the
291 parsing of Format into.
292 @param[in] BufferSize The maximum number of characters to put into
293 buffer.
294 @param[in] Flags Initial flags value.
295 Can only have FORMAT_UNICODE, OUTPUT_UNICODE,
296 and COUNT_ONLY_NO_PRINT set.
297 @param[in] Format A Null-terminated format string.
298 @param[in] VaListMarker VA_LIST style variable argument list consumed by
299 processing Format.
300 @param[in] BaseListMarker BASE_LIST style variable argument list consumed
301 by processing Format.
302
303 @return The number of characters printed not including the Null-terminator.
304 If COUNT_ONLY_NO_PRINT was set returns the same, but without any
305 modification to Buffer.
306
307 **/
308 UINTN
309 BasePrintLibSPrintMarker (
310 OUT CHAR8 *Buffer,
311 IN UINTN BufferSize,
312 IN UINTN Flags,
313 IN CONST CHAR8 *Format,
314 IN VA_LIST VaListMarker, OPTIONAL
315 IN BASE_LIST BaseListMarker OPTIONAL
316 )
317 {
318 CHAR8 *OriginalBuffer;
319 CHAR8 *EndBuffer;
320 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
321 UINT32 BytesPerOutputCharacter;
322 UINTN BytesPerFormatCharacter;
323 UINTN FormatMask;
324 UINTN FormatCharacter;
325 UINTN Width;
326 UINTN Precision;
327 INT64 Value;
328 CONST CHAR8 *ArgumentString;
329 UINTN Character;
330 GUID *TmpGuid;
331 TIME *TmpTime;
332 UINTN Count;
333 UINTN ArgumentMask;
334 INTN BytesPerArgumentCharacter;
335 UINTN ArgumentCharacter;
336 BOOLEAN Done;
337 UINTN Index;
338 CHAR8 Prefix;
339 BOOLEAN ZeroPad;
340 BOOLEAN Comma;
341 UINTN Digits;
342 UINTN Radix;
343 RETURN_STATUS Status;
344 UINT32 GuidData1;
345 UINT16 GuidData2;
346 UINT16 GuidData3;
347 UINTN LengthToReturn;
348
349 //
350 // If you change this code be sure to match the 2 versions of this function.
351 // Nearly identical logic is found in the BasePrintLib and
352 // DxePrintLibPrint2Protocol (both PrintLib instances).
353 //
354
355 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {
356 if (BufferSize == 0) {
357 Buffer = NULL;
358 }
359 } else {
360 //
361 // We can run without a Buffer for counting only.
362 //
363 if (BufferSize == 0) {
364 return 0;
365 }
366 ASSERT (Buffer != NULL);
367 }
368
369 if ((Flags & OUTPUT_UNICODE) != 0) {
370 BytesPerOutputCharacter = 2;
371 } else {
372 BytesPerOutputCharacter = 1;
373 }
374
375 LengthToReturn = 0;
376
377 //
378 // Reserve space for the Null terminator.
379 //
380 BufferSize--;
381 OriginalBuffer = Buffer;
382
383 //
384 // Set the tag for the end of the input Buffer.
385 //
386 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;
387
388 if ((Flags & FORMAT_UNICODE) != 0) {
389 //
390 // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength
391 // Unicode characters if PcdMaximumUnicodeStringLength is not zero.
392 //
393 ASSERT (StrSize ((CHAR16 *) Format) != 0);
394 BytesPerFormatCharacter = 2;
395 FormatMask = 0xffff;
396 } else {
397 //
398 // Make sure format string cannot contain more than PcdMaximumAsciiStringLength
399 // Ascii characters if PcdMaximumAsciiStringLength is not zero.
400 //
401 ASSERT (AsciiStrSize (Format) != 0);
402 BytesPerFormatCharacter = 1;
403 FormatMask = 0xff;
404 }
405
406 //
407 // Get the first character from the format string
408 //
409 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
410
411 //
412 // Loop until the end of the format string is reached or the output buffer is full
413 //
414 while (FormatCharacter != 0 && Buffer < EndBuffer) {
415 //
416 // Clear all the flag bits except those that may have been passed in
417 //
418 Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);
419
420 //
421 // Set the default width to zero, and the default precision to 1
422 //
423 Width = 0;
424 Precision = 1;
425 Prefix = 0;
426 Comma = FALSE;
427 ZeroPad = FALSE;
428 Count = 0;
429 Digits = 0;
430
431 switch (FormatCharacter) {
432 case '%':
433 //
434 // Parse Flags and Width
435 //
436 for (Done = FALSE; !Done; ) {
437 Format += BytesPerFormatCharacter;
438 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
439 switch (FormatCharacter) {
440 case '.':
441 Flags |= PRECISION;
442 break;
443 case '-':
444 Flags |= LEFT_JUSTIFY;
445 break;
446 case '+':
447 Flags |= PREFIX_SIGN;
448 break;
449 case ' ':
450 Flags |= PREFIX_BLANK;
451 break;
452 case ',':
453 Flags |= COMMA_TYPE;
454 break;
455 case 'L':
456 case 'l':
457 Flags |= LONG_TYPE;
458 break;
459 case '*':
460 if ((Flags & PRECISION) == 0) {
461 Flags |= PAD_TO_WIDTH;
462 if (BaseListMarker == NULL) {
463 Width = VA_ARG (VaListMarker, UINTN);
464 } else {
465 Width = BASE_ARG (BaseListMarker, UINTN);
466 }
467 } else {
468 if (BaseListMarker == NULL) {
469 Precision = VA_ARG (VaListMarker, UINTN);
470 } else {
471 Precision = BASE_ARG (BaseListMarker, UINTN);
472 }
473 }
474 break;
475 case '0':
476 if ((Flags & PRECISION) == 0) {
477 Flags |= PREFIX_ZERO;
478 }
479 case '1':
480 case '2':
481 case '3':
482 case '4':
483 case '5':
484 case '6':
485 case '7':
486 case '8':
487 case '9':
488 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){
489 Count = (Count * 10) + FormatCharacter - '0';
490 Format += BytesPerFormatCharacter;
491 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
492 }
493 Format -= BytesPerFormatCharacter;
494 if ((Flags & PRECISION) == 0) {
495 Flags |= PAD_TO_WIDTH;
496 Width = Count;
497 } else {
498 Precision = Count;
499 }
500 break;
501
502 case '\0':
503 //
504 // Make no output if Format string terminates unexpectedly when
505 // looking up for flag, width, precision and type.
506 //
507 Format -= BytesPerFormatCharacter;
508 Precision = 0;
509 //
510 // break skipped on purpose.
511 //
512 default:
513 Done = TRUE;
514 break;
515 }
516 }
517
518 //
519 // Handle each argument type
520 //
521 switch (FormatCharacter) {
522 case 'p':
523 //
524 // Flag space, +, 0, L & l are invalid for type p.
525 //
526 Flags &= ~(PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE);
527 if (sizeof (VOID *) > 4) {
528 Flags |= LONG_TYPE;
529 }
530 //
531 // break skipped on purpose
532 //
533 case 'X':
534 Flags |= PREFIX_ZERO;
535 //
536 // break skipped on purpose
537 //
538 case 'x':
539 Flags |= RADIX_HEX;
540 //
541 // break skipped on purpose
542 //
543 case 'd':
544 if ((Flags & LONG_TYPE) == 0) {
545 //
546 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
547 // This assumption is made so the format string definition is compatible with the ANSI C
548 // Specification for formatted strings. It is recommended that the Base Types be used
549 // everywhere, but in this one case, compliance with ANSI C is more important, and
550 // provides an implementation that is compatible with that largest possible set of CPU
551 // architectures. This is why the type "int" is used in this one case.
552 //
553 if (BaseListMarker == NULL) {
554 Value = VA_ARG (VaListMarker, int);
555 } else {
556 Value = BASE_ARG (BaseListMarker, int);
557 }
558 } else {
559 if (BaseListMarker == NULL) {
560 Value = VA_ARG (VaListMarker, INT64);
561 } else {
562 Value = BASE_ARG (BaseListMarker, INT64);
563 }
564 }
565 if ((Flags & PREFIX_BLANK) != 0) {
566 Prefix = ' ';
567 }
568 if ((Flags & PREFIX_SIGN) != 0) {
569 Prefix = '+';
570 }
571 if ((Flags & COMMA_TYPE) != 0) {
572 Comma = TRUE;
573 }
574 if ((Flags & RADIX_HEX) == 0) {
575 Radix = 10;
576 if (Comma) {
577 Flags &= (~PREFIX_ZERO);
578 Precision = 1;
579 }
580 if (Value < 0) {
581 Flags |= PREFIX_SIGN;
582 Prefix = '-';
583 Value = -Value;
584 }
585 } else {
586 Radix = 16;
587 Comma = FALSE;
588 if ((Flags & LONG_TYPE) == 0 && Value < 0) {
589 //
590 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
591 // This assumption is made so the format string definition is compatible with the ANSI C
592 // Specification for formatted strings. It is recommended that the Base Types be used
593 // everywhere, but in this one case, compliance with ANSI C is more important, and
594 // provides an implementation that is compatible with that largest possible set of CPU
595 // architectures. This is why the type "unsigned int" is used in this one case.
596 //
597 Value = (unsigned int)Value;
598 }
599 }
600 //
601 // Convert Value to a reversed string
602 //
603 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;
604 if (Value == 0 && Precision == 0) {
605 Count = 0;
606 }
607 ArgumentString = (CHAR8 *)ValueBuffer + Count;
608
609 Digits = Count % 3;
610 if (Digits != 0) {
611 Digits = 3 - Digits;
612 }
613 if (Comma && Count != 0) {
614 Count += ((Count - 1) / 3);
615 }
616 if (Prefix != 0) {
617 Count++;
618 Precision++;
619 }
620 Flags |= ARGUMENT_REVERSED;
621 ZeroPad = TRUE;
622 if ((Flags & PREFIX_ZERO) != 0) {
623 if ((Flags & LEFT_JUSTIFY) == 0) {
624 if ((Flags & PAD_TO_WIDTH) != 0) {
625 if ((Flags & PRECISION) == 0) {
626 Precision = Width;
627 }
628 }
629 }
630 }
631 break;
632
633 case 's':
634 case 'S':
635 Flags |= ARGUMENT_UNICODE;
636 //
637 // break skipped on purpose
638 //
639 case 'a':
640 if (BaseListMarker == NULL) {
641 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);
642 } else {
643 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);
644 }
645 if (ArgumentString == NULL) {
646 Flags &= (~ARGUMENT_UNICODE);
647 ArgumentString = "<null string>";
648 }
649 //
650 // Set the default precision for string to be zero if not specified.
651 //
652 if ((Flags & PRECISION) == 0) {
653 Precision = 0;
654 }
655 break;
656
657 case 'c':
658 if (BaseListMarker == NULL) {
659 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;
660 } else {
661 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;
662 }
663 ArgumentString = (CHAR8 *)&Character;
664 Flags |= ARGUMENT_UNICODE;
665 break;
666
667 case 'g':
668 if (BaseListMarker == NULL) {
669 TmpGuid = VA_ARG (VaListMarker, GUID *);
670 } else {
671 TmpGuid = BASE_ARG (BaseListMarker, GUID *);
672 }
673 if (TmpGuid == NULL) {
674 ArgumentString = "<null guid>";
675 } else {
676 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));
677 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));
678 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));
679 BasePrintLibSPrint (
680 ValueBuffer,
681 MAXIMUM_VALUE_CHARACTERS,
682 0,
683 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
684 GuidData1,
685 GuidData2,
686 GuidData3,
687 TmpGuid->Data4[0],
688 TmpGuid->Data4[1],
689 TmpGuid->Data4[2],
690 TmpGuid->Data4[3],
691 TmpGuid->Data4[4],
692 TmpGuid->Data4[5],
693 TmpGuid->Data4[6],
694 TmpGuid->Data4[7]
695 );
696 ArgumentString = ValueBuffer;
697 }
698 break;
699
700 case 't':
701 if (BaseListMarker == NULL) {
702 TmpTime = VA_ARG (VaListMarker, TIME *);
703 } else {
704 TmpTime = BASE_ARG (BaseListMarker, TIME *);
705 }
706 if (TmpTime == NULL) {
707 ArgumentString = "<null time>";
708 } else {
709 BasePrintLibSPrint (
710 ValueBuffer,
711 MAXIMUM_VALUE_CHARACTERS,
712 0,
713 "%02d/%02d/%04d %02d:%02d",
714 TmpTime->Month,
715 TmpTime->Day,
716 TmpTime->Year,
717 TmpTime->Hour,
718 TmpTime->Minute
719 );
720 ArgumentString = ValueBuffer;
721 }
722 break;
723
724 case 'r':
725 if (BaseListMarker == NULL) {
726 Status = VA_ARG (VaListMarker, RETURN_STATUS);
727 } else {
728 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);
729 }
730 ArgumentString = ValueBuffer;
731 if (RETURN_ERROR (Status)) {
732 //
733 // Clear error bit
734 //
735 Index = Status & ~MAX_BIT;
736 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {
737 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];
738 }
739 } else {
740 Index = Status;
741 if (Index <= WARNING_STATUS_NUMBER) {
742 ArgumentString = mStatusString [Index];
743 }
744 }
745 if (ArgumentString == ValueBuffer) {
746 BasePrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);
747 }
748 break;
749
750 case '\r':
751 Format += BytesPerFormatCharacter;
752 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
753 if (FormatCharacter == '\n') {
754 //
755 // Translate '\r\n' to '\r\n'
756 //
757 ArgumentString = "\r\n";
758 } else {
759 //
760 // Translate '\r' to '\r'
761 //
762 ArgumentString = "\r";
763 Format -= BytesPerFormatCharacter;
764 }
765 break;
766
767 case '\n':
768 //
769 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
770 //
771 ArgumentString = "\r\n";
772 Format += BytesPerFormatCharacter;
773 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
774 if (FormatCharacter != '\r') {
775 Format -= BytesPerFormatCharacter;
776 }
777 break;
778
779 case '%':
780 default:
781 //
782 // if the type is '%' or unknown, then print it to the screen
783 //
784 ArgumentString = (CHAR8 *)&FormatCharacter;
785 Flags |= ARGUMENT_UNICODE;
786 break;
787 }
788 break;
789
790 case '\r':
791 Format += BytesPerFormatCharacter;
792 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
793 if (FormatCharacter == '\n') {
794 //
795 // Translate '\r\n' to '\r\n'
796 //
797 ArgumentString = "\r\n";
798 } else {
799 //
800 // Translate '\r' to '\r'
801 //
802 ArgumentString = "\r";
803 Format -= BytesPerFormatCharacter;
804 }
805 break;
806
807 case '\n':
808 //
809 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
810 //
811 ArgumentString = "\r\n";
812 Format += BytesPerFormatCharacter;
813 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
814 if (FormatCharacter != '\r') {
815 Format -= BytesPerFormatCharacter;
816 }
817 break;
818
819 default:
820 ArgumentString = (CHAR8 *)&FormatCharacter;
821 Flags |= ARGUMENT_UNICODE;
822 break;
823 }
824
825 //
826 // Retrieve the ArgumentString attriubutes
827 //
828 if ((Flags & ARGUMENT_UNICODE) != 0) {
829 ArgumentMask = 0xffff;
830 BytesPerArgumentCharacter = 2;
831 } else {
832 ArgumentMask = 0xff;
833 BytesPerArgumentCharacter = 1;
834 }
835 if ((Flags & ARGUMENT_REVERSED) != 0) {
836 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;
837 } else {
838 //
839 // Compute the number of characters in ArgumentString and store it in Count
840 // ArgumentString is either null-terminated, or it contains Precision characters
841 //
842 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {
843 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;
844 if (ArgumentCharacter == 0) {
845 break;
846 }
847 }
848 }
849
850 if (Precision < Count) {
851 Precision = Count;
852 }
853
854 //
855 // Pad before the string
856 //
857 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {
858 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
859 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
860 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
861 }
862 }
863
864 if (ZeroPad) {
865 if (Prefix != 0) {
866 LengthToReturn += (1 * BytesPerOutputCharacter);
867 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
868 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
869 }
870 }
871 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
872 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
873 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);
874 }
875 } else {
876 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
877 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
878 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);
879 }
880 if (Prefix != 0) {
881 LengthToReturn += (1 * BytesPerOutputCharacter);
882 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
883 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
884 }
885 }
886 }
887
888 //
889 // Output the Prefix character if it is present
890 //
891 Index = 0;
892 if (Prefix != 0) {
893 Index++;
894 }
895
896 //
897 // Copy the string into the output buffer performing the required type conversions
898 //
899 while (Index < Count) {
900 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;
901
902 LengthToReturn += (1 * BytesPerOutputCharacter);
903 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
904 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);
905 }
906 ArgumentString += BytesPerArgumentCharacter;
907 Index++;
908 if (Comma) {
909 Digits++;
910 if (Digits == 3) {
911 Digits = 0;
912 Index++;
913 if (Index < Count) {
914 LengthToReturn += (1 * BytesPerOutputCharacter);
915 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
916 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);
917 }
918 }
919 }
920 }
921 }
922
923 //
924 // Pad after the string
925 //
926 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {
927 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
928 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
929 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
930 }
931 }
932
933 //
934 // Get the next character from the format string
935 //
936 Format += BytesPerFormatCharacter;
937
938 //
939 // Get the next character from the format string
940 //
941 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
942 }
943
944 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {
945 return (LengthToReturn / BytesPerOutputCharacter);
946 }
947
948 ASSERT (Buffer != NULL);
949 //
950 // Null terminate the Unicode or ASCII string
951 //
952 BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);
953 //
954 // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength
955 // Unicode characters if PcdMaximumUnicodeStringLength is not zero.
956 //
957 ASSERT ((((Flags & OUTPUT_UNICODE) == 0)) || (StrSize ((CHAR16 *) OriginalBuffer) != 0));
958 //
959 // Make sure output buffer cannot contain more than PcdMaximumAsciiStringLength
960 // ASCII characters if PcdMaximumAsciiStringLength is not zero.
961 //
962 ASSERT ((((Flags & OUTPUT_UNICODE) != 0)) || (AsciiStrSize (OriginalBuffer) != 0));
963
964 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);
965 }
966
967 /**
968 Worker function that produces a Null-terminated string in an output buffer
969 based on a Null-terminated format string and variable argument list.
970
971 VSPrint function to process format and place the results in Buffer. Since a
972 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
973 this is the main print working routine
974
975 @param StartOfBuffer The character buffer to print the results of the parsing
976 of Format into.
977 @param BufferSize The maximum number of characters to put into buffer.
978 Zero means no limit.
979 @param Flags Initial flags value.
980 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set
981 @param FormatString A Null-terminated format string.
982 @param ... The variable argument list.
983
984 @return The number of characters printed.
985
986 **/
987 UINTN
988 EFIAPI
989 BasePrintLibSPrint (
990 OUT CHAR8 *StartOfBuffer,
991 IN UINTN BufferSize,
992 IN UINTN Flags,
993 IN CONST CHAR8 *FormatString,
994 ...
995 )
996 {
997 VA_LIST Marker;
998 UINTN NumberOfPrinted;
999
1000 VA_START (Marker, FormatString);
1001 NumberOfPrinted = BasePrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);
1002 VA_END (Marker);
1003 return NumberOfPrinted;
1004 }