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