]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePrintLib/PrintLibInternal.c
Enhance the code to make the code logic more clear.
[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 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 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.
279
280 @param[out] Buffer The character buffer to print the results of the
281 parsing of Format into.
282 @param[in] BufferSize The maximum number of characters to put into
283 buffer.
284 @param[in] Flags Initial flags value.
285 Can only have FORMAT_UNICODE, OUTPUT_UNICODE,
286 and COUNT_ONLY_NO_PRINT set.
287 @param[in] Format A Null-terminated format string.
288 @param[in] VaListMarker VA_LIST style variable argument list consumed by
289 processing Format.
290 @param[in] BaseListMarker BASE_LIST style variable argument list consumed
291 by processing Format.
292
293 @return The number of characters printed not including the Null-terminator.
294 If COUNT_ONLY_NO_PRINT was set returns the same, but without any
295 modification to Buffer.
296
297 **/
298 UINTN
299 BasePrintLibSPrintMarker (
300 OUT CHAR8 *Buffer,
301 IN UINTN BufferSize,
302 IN UINTN Flags,
303 IN CONST CHAR8 *Format,
304 IN VA_LIST VaListMarker, OPTIONAL
305 IN BASE_LIST BaseListMarker OPTIONAL
306 )
307 {
308 CHAR8 *OriginalBuffer;
309 CHAR8 *EndBuffer;
310 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
311 UINT32 BytesPerOutputCharacter;
312 UINTN BytesPerFormatCharacter;
313 UINTN FormatMask;
314 UINTN FormatCharacter;
315 UINTN Width;
316 UINTN Precision;
317 INT64 Value;
318 CONST CHAR8 *ArgumentString;
319 UINTN Character;
320 GUID *TmpGuid;
321 TIME *TmpTime;
322 UINTN Count;
323 UINTN ArgumentMask;
324 INTN BytesPerArgumentCharacter;
325 UINTN ArgumentCharacter;
326 BOOLEAN Done;
327 UINTN Index;
328 CHAR8 Prefix;
329 BOOLEAN ZeroPad;
330 BOOLEAN Comma;
331 UINTN Digits;
332 UINTN Radix;
333 RETURN_STATUS Status;
334 UINT32 GuidData1;
335 UINT16 GuidData2;
336 UINT16 GuidData3;
337 UINTN LengthToReturn;
338
339 //
340 // If you change this code be sure to match the 2 versions of this function.
341 // Nearly identical logic is found in the BasePrintLib and
342 // DxePrintLibPrint2Protocol (both PrintLib instances).
343 //
344
345 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {
346 if (BufferSize == 0) {
347 Buffer = NULL;
348 }
349 } else {
350 //
351 // We can run without a Buffer for counting only.
352 //
353 if (BufferSize == 0) {
354 return 0;
355 }
356 ASSERT (Buffer != NULL);
357 }
358
359 if ((Flags & OUTPUT_UNICODE) != 0) {
360 BytesPerOutputCharacter = 2;
361 } else {
362 BytesPerOutputCharacter = 1;
363 }
364
365 LengthToReturn = 0;
366
367 //
368 // Reserve space for the Null terminator.
369 //
370 BufferSize--;
371 OriginalBuffer = Buffer;
372
373 //
374 // Set the tag for the end of the input Buffer.
375 //
376 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;
377
378 if ((Flags & FORMAT_UNICODE) != 0) {
379 //
380 // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength
381 // Unicode characters if PcdMaximumUnicodeStringLength is not zero.
382 //
383 ASSERT (StrSize ((CHAR16 *) Format) != 0);
384 BytesPerFormatCharacter = 2;
385 FormatMask = 0xffff;
386 } else {
387 //
388 // Make sure format string cannot contain more than PcdMaximumAsciiStringLength
389 // Ascii characters if PcdMaximumAsciiStringLength is not zero.
390 //
391 ASSERT (AsciiStrSize (Format) != 0);
392 BytesPerFormatCharacter = 1;
393 FormatMask = 0xff;
394 }
395
396 //
397 // Get the first character from the format string
398 //
399 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
400
401 //
402 // Loop until the end of the format string is reached or the output buffer is full
403 //
404 while (FormatCharacter != 0 && Buffer < EndBuffer) {
405 //
406 // Clear all the flag bits except those that may have been passed in
407 //
408 Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);
409
410 //
411 // Set the default width to zero, and the default precision to 1
412 //
413 Width = 0;
414 Precision = 1;
415 Prefix = 0;
416 Comma = FALSE;
417 ZeroPad = FALSE;
418 Count = 0;
419 Digits = 0;
420
421 switch (FormatCharacter) {
422 case '%':
423 //
424 // Parse Flags and Width
425 //
426 for (Done = FALSE; !Done; ) {
427 Format += BytesPerFormatCharacter;
428 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
429 switch (FormatCharacter) {
430 case '.':
431 Flags |= PRECISION;
432 break;
433 case '-':
434 Flags |= LEFT_JUSTIFY;
435 break;
436 case '+':
437 Flags |= PREFIX_SIGN;
438 break;
439 case ' ':
440 Flags |= PREFIX_BLANK;
441 break;
442 case ',':
443 Flags |= COMMA_TYPE;
444 break;
445 case 'L':
446 case 'l':
447 Flags |= LONG_TYPE;
448 break;
449 case '*':
450 if ((Flags & PRECISION) == 0) {
451 Flags |= PAD_TO_WIDTH;
452 if (BaseListMarker == NULL) {
453 Width = VA_ARG (VaListMarker, UINTN);
454 } else {
455 Width = BASE_ARG (BaseListMarker, UINTN);
456 }
457 } else {
458 if (BaseListMarker == NULL) {
459 Precision = VA_ARG (VaListMarker, UINTN);
460 } else {
461 Precision = BASE_ARG (BaseListMarker, UINTN);
462 }
463 }
464 break;
465 case '0':
466 if ((Flags & PRECISION) == 0) {
467 Flags |= PREFIX_ZERO;
468 }
469 case '1':
470 case '2':
471 case '3':
472 case '4':
473 case '5':
474 case '6':
475 case '7':
476 case '8':
477 case '9':
478 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){
479 Count = (Count * 10) + FormatCharacter - '0';
480 Format += BytesPerFormatCharacter;
481 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
482 }
483 Format -= BytesPerFormatCharacter;
484 if ((Flags & PRECISION) == 0) {
485 Flags |= PAD_TO_WIDTH;
486 Width = Count;
487 } else {
488 Precision = Count;
489 }
490 break;
491
492 case '\0':
493 //
494 // Make no output if Format string terminates unexpectedly when
495 // looking up for flag, width, precision and type.
496 //
497 Format -= BytesPerFormatCharacter;
498 Precision = 0;
499 //
500 // break skipped on purpose.
501 //
502 default:
503 Done = TRUE;
504 break;
505 }
506 }
507
508 //
509 // Handle each argument type
510 //
511 switch (FormatCharacter) {
512 case 'p':
513 //
514 // Flag space, +, 0, L & l are invalid for type p.
515 //
516 Flags &= ~(PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE);
517 if (sizeof (VOID *) > 4) {
518 Flags |= LONG_TYPE;
519 }
520 //
521 // break skipped on purpose
522 //
523 case 'X':
524 Flags |= PREFIX_ZERO;
525 //
526 // break skipped on purpose
527 //
528 case 'x':
529 Flags |= RADIX_HEX;
530 //
531 // break skipped on purpose
532 //
533 case 'd':
534 if ((Flags & LONG_TYPE) == 0) {
535 //
536 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
537 // This assumption is made so the format string definition is compatible with the ANSI C
538 // Specification for formatted strings. It is recommended that the Base Types be used
539 // everywhere, but in this one case, compliance with ANSI C is more important, and
540 // provides an implementation that is compatible with that largest possible set of CPU
541 // architectures. This is why the type "int" is used in this one case.
542 //
543 if (BaseListMarker == NULL) {
544 Value = VA_ARG (VaListMarker, int);
545 } else {
546 Value = BASE_ARG (BaseListMarker, int);
547 }
548 } else {
549 if (BaseListMarker == NULL) {
550 Value = VA_ARG (VaListMarker, INT64);
551 } else {
552 Value = BASE_ARG (BaseListMarker, INT64);
553 }
554 }
555 if ((Flags & PREFIX_BLANK) != 0) {
556 Prefix = ' ';
557 }
558 if ((Flags & PREFIX_SIGN) != 0) {
559 Prefix = '+';
560 }
561 if ((Flags & COMMA_TYPE) != 0) {
562 Comma = TRUE;
563 }
564 if ((Flags & RADIX_HEX) == 0) {
565 Radix = 10;
566 if (Comma) {
567 Flags &= (~PREFIX_ZERO);
568 Precision = 1;
569 }
570 if (Value < 0) {
571 Flags |= PREFIX_SIGN;
572 Prefix = '-';
573 Value = -Value;
574 }
575 } else {
576 Radix = 16;
577 Comma = FALSE;
578 if ((Flags & LONG_TYPE) == 0 && Value < 0) {
579 //
580 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".
581 // This assumption is made so the format string definition is compatible with the ANSI C
582 // Specification for formatted strings. It is recommended that the Base Types be used
583 // everywhere, but in this one case, compliance with ANSI C is more important, and
584 // provides an implementation that is compatible with that largest possible set of CPU
585 // architectures. This is why the type "unsigned int" is used in this one case.
586 //
587 Value = (unsigned int)Value;
588 }
589 }
590 //
591 // Convert Value to a reversed string
592 //
593 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;
594 if (Value == 0 && Precision == 0) {
595 Count = 0;
596 }
597 ArgumentString = (CHAR8 *)ValueBuffer + Count;
598
599 Digits = Count % 3;
600 if (Digits != 0) {
601 Digits = 3 - Digits;
602 }
603 if (Comma && Count != 0) {
604 Count += ((Count - 1) / 3);
605 }
606 if (Prefix != 0) {
607 Count++;
608 Precision++;
609 }
610 Flags |= ARGUMENT_REVERSED;
611 ZeroPad = TRUE;
612 if ((Flags & PREFIX_ZERO) != 0) {
613 if ((Flags & LEFT_JUSTIFY) == 0) {
614 if ((Flags & PAD_TO_WIDTH) != 0) {
615 if ((Flags & PRECISION) == 0) {
616 Precision = Width;
617 }
618 }
619 }
620 }
621 break;
622
623 case 's':
624 case 'S':
625 Flags |= ARGUMENT_UNICODE;
626 //
627 // break skipped on purpose
628 //
629 case 'a':
630 if (BaseListMarker == NULL) {
631 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);
632 } else {
633 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);
634 }
635 if (ArgumentString == NULL) {
636 Flags &= (~ARGUMENT_UNICODE);
637 ArgumentString = "<null string>";
638 }
639 //
640 // Set the default precision for string to be zero if not specified.
641 //
642 if ((Flags & PRECISION) == 0) {
643 Precision = 0;
644 }
645 break;
646
647 case 'c':
648 if (BaseListMarker == NULL) {
649 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;
650 } else {
651 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;
652 }
653 ArgumentString = (CHAR8 *)&Character;
654 Flags |= ARGUMENT_UNICODE;
655 break;
656
657 case 'g':
658 if (BaseListMarker == NULL) {
659 TmpGuid = VA_ARG (VaListMarker, GUID *);
660 } else {
661 TmpGuid = BASE_ARG (BaseListMarker, GUID *);
662 }
663 if (TmpGuid == NULL) {
664 ArgumentString = "<null guid>";
665 } else {
666 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));
667 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));
668 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));
669 BasePrintLibSPrint (
670 ValueBuffer,
671 MAXIMUM_VALUE_CHARACTERS,
672 0,
673 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
674 GuidData1,
675 GuidData2,
676 GuidData3,
677 TmpGuid->Data4[0],
678 TmpGuid->Data4[1],
679 TmpGuid->Data4[2],
680 TmpGuid->Data4[3],
681 TmpGuid->Data4[4],
682 TmpGuid->Data4[5],
683 TmpGuid->Data4[6],
684 TmpGuid->Data4[7]
685 );
686 ArgumentString = ValueBuffer;
687 }
688 break;
689
690 case 't':
691 if (BaseListMarker == NULL) {
692 TmpTime = VA_ARG (VaListMarker, TIME *);
693 } else {
694 TmpTime = BASE_ARG (BaseListMarker, TIME *);
695 }
696 if (TmpTime == NULL) {
697 ArgumentString = "<null time>";
698 } else {
699 BasePrintLibSPrint (
700 ValueBuffer,
701 MAXIMUM_VALUE_CHARACTERS,
702 0,
703 "%02d/%02d/%04d %02d:%02d",
704 TmpTime->Month,
705 TmpTime->Day,
706 TmpTime->Year,
707 TmpTime->Hour,
708 TmpTime->Minute
709 );
710 ArgumentString = ValueBuffer;
711 }
712 break;
713
714 case 'r':
715 if (BaseListMarker == NULL) {
716 Status = VA_ARG (VaListMarker, RETURN_STATUS);
717 } else {
718 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);
719 }
720 ArgumentString = ValueBuffer;
721 if (RETURN_ERROR (Status)) {
722 //
723 // Clear error bit
724 //
725 Index = Status & ~MAX_BIT;
726 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {
727 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];
728 }
729 } else {
730 Index = Status;
731 if (Index <= WARNING_STATUS_NUMBER) {
732 ArgumentString = mStatusString [Index];
733 }
734 }
735 if (ArgumentString == ValueBuffer) {
736 BasePrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);
737 }
738 break;
739
740 case '\r':
741 Format += BytesPerFormatCharacter;
742 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
743 if (FormatCharacter == '\n') {
744 //
745 // Translate '\r\n' to '\r\n'
746 //
747 ArgumentString = "\r\n";
748 } else {
749 //
750 // Translate '\r' to '\r'
751 //
752 ArgumentString = "\r";
753 Format -= BytesPerFormatCharacter;
754 }
755 break;
756
757 case '\n':
758 //
759 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
760 //
761 ArgumentString = "\r\n";
762 Format += BytesPerFormatCharacter;
763 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
764 if (FormatCharacter != '\r') {
765 Format -= BytesPerFormatCharacter;
766 }
767 break;
768
769 case '%':
770 default:
771 //
772 // if the type is '%' or unknown, then print it to the screen
773 //
774 ArgumentString = (CHAR8 *)&FormatCharacter;
775 Flags |= ARGUMENT_UNICODE;
776 break;
777 }
778 break;
779
780 case '\r':
781 Format += BytesPerFormatCharacter;
782 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
783 if (FormatCharacter == '\n') {
784 //
785 // Translate '\r\n' to '\r\n'
786 //
787 ArgumentString = "\r\n";
788 } else {
789 //
790 // Translate '\r' to '\r'
791 //
792 ArgumentString = "\r";
793 Format -= BytesPerFormatCharacter;
794 }
795 break;
796
797 case '\n':
798 //
799 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'
800 //
801 ArgumentString = "\r\n";
802 Format += BytesPerFormatCharacter;
803 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
804 if (FormatCharacter != '\r') {
805 Format -= BytesPerFormatCharacter;
806 }
807 break;
808
809 default:
810 ArgumentString = (CHAR8 *)&FormatCharacter;
811 Flags |= ARGUMENT_UNICODE;
812 break;
813 }
814
815 //
816 // Retrieve the ArgumentString attriubutes
817 //
818 if ((Flags & ARGUMENT_UNICODE) != 0) {
819 ArgumentMask = 0xffff;
820 BytesPerArgumentCharacter = 2;
821 } else {
822 ArgumentMask = 0xff;
823 BytesPerArgumentCharacter = 1;
824 }
825 if ((Flags & ARGUMENT_REVERSED) != 0) {
826 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;
827 } else {
828 //
829 // Compute the number of characters in ArgumentString and store it in Count
830 // ArgumentString is either null-terminated, or it contains Precision characters
831 //
832 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {
833 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;
834 if (ArgumentCharacter == 0) {
835 break;
836 }
837 }
838 }
839
840 if (Precision < Count) {
841 Precision = Count;
842 }
843
844 //
845 // Pad before the string
846 //
847 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {
848 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
849 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
850 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
851 }
852 }
853
854 if (ZeroPad) {
855 if (Prefix != 0) {
856 LengthToReturn += (1 * BytesPerOutputCharacter);
857 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
858 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
859 }
860 }
861 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
862 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
863 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);
864 }
865 } else {
866 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);
867 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
868 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);
869 }
870 if (Prefix != 0) {
871 LengthToReturn += (1 * BytesPerOutputCharacter);
872 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
873 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
874 }
875 }
876 }
877
878 //
879 // Output the Prefix character if it is present
880 //
881 Index = 0;
882 if (Prefix != 0) {
883 Index++;
884 }
885
886 //
887 // Copy the string into the output buffer performing the required type conversions
888 //
889 while (Index < Count) {
890 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;
891
892 LengthToReturn += (1 * BytesPerOutputCharacter);
893 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
894 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);
895 }
896 ArgumentString += BytesPerArgumentCharacter;
897 Index++;
898 if (Comma) {
899 Digits++;
900 if (Digits == 3) {
901 Digits = 0;
902 Index++;
903 if (Index < Count) {
904 LengthToReturn += (1 * BytesPerOutputCharacter);
905 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
906 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);
907 }
908 }
909 }
910 }
911 }
912
913 //
914 // Pad after the string
915 //
916 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {
917 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);
918 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {
919 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
920 }
921 }
922
923 //
924 // Get the next character from the format string
925 //
926 Format += BytesPerFormatCharacter;
927
928 //
929 // Get the next character from the format string
930 //
931 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
932 }
933
934 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {
935 return (LengthToReturn / BytesPerOutputCharacter);
936 }
937
938 ASSERT (Buffer != NULL);
939 //
940 // Null terminate the Unicode or ASCII string
941 //
942 BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);
943 //
944 // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength
945 // Unicode characters if PcdMaximumUnicodeStringLength is not zero.
946 //
947 ASSERT ((((Flags & OUTPUT_UNICODE) == 0)) || (StrSize ((CHAR16 *) OriginalBuffer) != 0));
948 //
949 // Make sure output buffer cannot contain more than PcdMaximumAsciiStringLength
950 // ASCII characters if PcdMaximumAsciiStringLength is not zero.
951 //
952 ASSERT ((((Flags & OUTPUT_UNICODE) != 0)) || (AsciiStrSize (OriginalBuffer) != 0));
953
954 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);
955 }
956
957 /**
958 Worker function that produces a Null-terminated string in an output buffer
959 based on a Null-terminated format string and variable argument list.
960
961 VSPrint function to process format and place the results in Buffer. Since a
962 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
963 this is the main print working routine
964
965 @param StartOfBuffer The character buffer to print the results of the parsing
966 of Format into.
967 @param BufferSize The maximum number of characters to put into buffer.
968 Zero means no limit.
969 @param Flags Initial flags value.
970 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set
971 @param FormatString A Null-terminated format string.
972 @param ... The variable argument list.
973
974 @return The number of characters printed.
975
976 **/
977 UINTN
978 EFIAPI
979 BasePrintLibSPrint (
980 OUT CHAR8 *StartOfBuffer,
981 IN UINTN BufferSize,
982 IN UINTN Flags,
983 IN CONST CHAR8 *FormatString,
984 ...
985 )
986 {
987 VA_LIST Marker;
988 UINTN NumberOfPrinted;
989
990 VA_START (Marker, FormatString);
991 NumberOfPrinted = BasePrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);
992 VA_END (Marker);
993 return NumberOfPrinted;
994 }