]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePrintLib/PrintLibInternal.c
Add comments in platform DSC files to remind that binaries are only listed in FDF...
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLibInternal.c
1 /** @file
2 Print Library internal worker functions.
3
4 Copyright (c) 2006 - 2008, Intel Corporation<BR>
5 All rights reserved. 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 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 Count of character to be placed into Buffer.
64 (Negative value indicates no buffer fill.)
65 @param Character Character to be placed into Buffer.
66 @param Increment Character increment in Buffer.
67
68 @return Buffer Buffer filled with the input Character.
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 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 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 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 HEX_RADIX 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 Character buffer to print the results of the parsing
279 of Format into.
280 @param BufferSize 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 Null-terminated format string.
284 @param Marker Vararg list consumed by processing Format.
285
286 @return Number of characters printed not including the Null-terminator.
287
288 **/
289 UINTN
290 BasePrintLibVSPrint (
291 OUT CHAR8 *Buffer,
292 IN UINTN BufferSize,
293 IN UINTN Flags,
294 IN CONST CHAR8 *Format,
295 IN VA_LIST Marker
296 )
297 {
298 CHAR8 *OriginalBuffer;
299 CHAR8 *EndBuffer;
300 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
301 UINTN BytesPerOutputCharacter;
302 UINTN BytesPerFormatCharacter;
303 UINTN FormatMask;
304 UINTN FormatCharacter;
305 UINTN Width;
306 UINTN Precision;
307 INT64 Value;
308 CONST CHAR8 *ArgumentString;
309 UINTN Character;
310 GUID *TmpGuid;
311 TIME *TmpTime;
312 UINTN Count;
313 UINTN ArgumentMask;
314 INTN BytesPerArgumentCharacter;
315 UINTN ArgumentCharacter;
316 BOOLEAN Done;
317 UINTN Index;
318 CHAR8 Prefix;
319 BOOLEAN ZeroPad;
320 BOOLEAN Comma;
321 UINTN Digits;
322 UINTN Radix;
323 RETURN_STATUS Status;
324
325 if (BufferSize == 0) {
326 return 0;
327 }
328 ASSERT (Buffer != NULL);
329
330 if ((Flags & OUTPUT_UNICODE) != 0) {
331 BytesPerOutputCharacter = 2;
332 } else {
333 BytesPerOutputCharacter = 1;
334 }
335
336 //
337 // Reserve space for the Null terminator.
338 //
339 BufferSize--;
340 OriginalBuffer = Buffer;
341 //
342 // Set the tag for the end of the input Buffer.
343 //
344 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;
345
346 if ((Flags & FORMAT_UNICODE) != 0) {
347 //
348 // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength
349 // Unicode characters if PcdMaximumUnicodeStringLength is not zero.
350 //
351 ASSERT (StrSize ((CHAR16 *) Format) != 0);
352 BytesPerFormatCharacter = 2;
353 FormatMask = 0xffff;
354 } else {
355 //
356 // Make sure format string cannot contain more than PcdMaximumAsciiStringLength
357 // Ascii characters if PcdMaximumAsciiStringLength is not zero.
358 //
359 ASSERT (AsciiStrSize (Format) != 0);
360 BytesPerFormatCharacter = 1;
361 FormatMask = 0xff;
362 }
363
364 //
365 // Get the first character from the format string
366 //
367 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
368
369 //
370 // Loop until the end of the format string is reached or the output buffer is full
371 //
372 while (FormatCharacter != 0 && Buffer < EndBuffer) {
373 //
374 // Clear all the flag bits except those that may have been passed in
375 //
376 Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE);
377
378 //
379 // Set the default width to zero, and the default precision to 1
380 //
381 Width = 0;
382 Precision = 1;
383 Prefix = 0;
384 Comma = FALSE;
385 ZeroPad = FALSE;
386 Count = 0;
387 Digits = 0;
388
389 switch (FormatCharacter) {
390 case '%':
391 //
392 // Parse Flags and Width
393 //
394 for (Done = FALSE; !Done; ) {
395 Format += BytesPerFormatCharacter;
396 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
397 switch (FormatCharacter) {
398 case '.':
399 Flags |= PRECISION;
400 break;
401 case '-':
402 Flags |= LEFT_JUSTIFY;
403 break;
404 case '+':
405 Flags |= PREFIX_SIGN;
406 break;
407 case ' ':
408 Flags |= PREFIX_BLANK;
409 break;
410 case ',':
411 Flags |= COMMA_TYPE;
412 break;
413 case 'L':
414 case 'l':
415 Flags |= LONG_TYPE;
416 break;
417 case '*':
418 if ((Flags & PRECISION) == 0) {
419 Flags |= PAD_TO_WIDTH;
420 Width = VA_ARG (Marker, UINTN);
421 } else {
422 Precision = VA_ARG (Marker, UINTN);
423 }
424 break;
425 case '0':
426 if ((Flags & PRECISION) == 0) {
427 Flags |= PREFIX_ZERO;
428 }
429 case '1':
430 case '2':
431 case '3':
432 case '4':
433 case '5':
434 case '6':
435 case '7':
436 case '8':
437 case '9':
438 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){
439 Count = (Count * 10) + FormatCharacter - '0';
440 Format += BytesPerFormatCharacter;
441 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
442 }
443 Format -= BytesPerFormatCharacter;
444 if ((Flags & PRECISION) == 0) {
445 Flags |= PAD_TO_WIDTH;
446 Width = Count;
447 } else {
448 Precision = Count;
449 }
450 break;
451
452 case '\0':
453 //
454 // Make no output if Format string terminates unexpectedly when
455 // looking up for flag, width, precision and type.
456 //
457 Format -= BytesPerFormatCharacter;
458 Precision = 0;
459 //
460 // break skipped on purpose.
461 //
462 default:
463 Done = TRUE;
464 break;
465 }
466 }
467
468 //
469 // Handle each argument type
470 //
471 switch (FormatCharacter) {
472 case 'p':
473 //
474 // Flag space, +, 0, L & l are invalid for type p.
475 //
476 Flags &= ~(PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE);
477 if (sizeof (VOID *) > 4) {
478 Flags |= LONG_TYPE;
479 }
480 case 'X':
481 Flags |= PREFIX_ZERO;
482 //
483 // break skipped on purpose
484 //
485 case 'x':
486 Flags |= RADIX_HEX;
487 //
488 // break skipped on purpose
489 //
490 case 'd':
491 if ((Flags & LONG_TYPE) == 0) {
492 //
493 // 'd','x', and 'X' that are not preceeded by 'l' or 'L' are assumed to be type "int".
494 // This assumption is made so the format string defintion is compatible with the ANSI C
495 // Specification for formatted strings. It is recommended that the Base Types be used
496 // everywhere, but in this one case, compliance with ANSI C is more important, and
497 // provides an implementation that is compatible with that largest possible set of CPU
498 // architectures. This is why the type "int" is used in this one case.
499 //
500 Value = (VA_ARG (Marker, int));
501 } else {
502 Value = VA_ARG (Marker, INT64);
503 }
504 if ((Flags & PREFIX_BLANK) != 0) {
505 Prefix = ' ';
506 }
507 if ((Flags & PREFIX_SIGN) != 0) {
508 Prefix = '+';
509 }
510 if ((Flags & COMMA_TYPE) != 0) {
511 Comma = TRUE;
512 }
513 if ((Flags & RADIX_HEX) == 0) {
514 Radix = 10;
515 if (Comma) {
516 Flags &= (~PREFIX_ZERO);
517 Precision = 1;
518 }
519 if (Value < 0) {
520 Flags |= PREFIX_SIGN;
521 Prefix = '-';
522 Value = -Value;
523 }
524 } else {
525 Radix = 16;
526 Comma = FALSE;
527 if ((Flags & LONG_TYPE) == 0 && Value < 0) {
528 //
529 // 'd','x', and 'X' that are not preceeded by 'l' or 'L' are assumed to be type "int".
530 // This assumption is made so the format string defintion is compatible with the ANSI C
531 // Specification for formatted strings. It is recommended that the Base Types be used
532 // everywhere, but in this one case, compliance with ANSI C is more important, and
533 // provides an implementation that is compatible with that largest possible set of CPU
534 // architectures. This is why the type "unsigned int" is used in this one case.
535 //
536 Value = (unsigned int)Value;
537 }
538 }
539 //
540 // Convert Value to a reversed string
541 //
542 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;
543 if (Value == 0 && Precision == 0) {
544 Count = 0;
545 }
546 ArgumentString = (CHAR8 *)ValueBuffer + Count;
547
548 Digits = Count % 3;
549 if (Digits != 0) {
550 Digits = 3 - Digits;
551 }
552 if (Comma && Count != 0) {
553 Count += ((Count - 1) / 3);
554 }
555 if (Prefix != 0) {
556 Count++;
557 Precision++;
558 }
559 Flags |= ARGUMENT_REVERSED;
560 ZeroPad = TRUE;
561 if ((Flags & PREFIX_ZERO) != 0) {
562 if ((Flags & LEFT_JUSTIFY) == 0) {
563 if ((Flags & PAD_TO_WIDTH) != 0) {
564 if ((Flags & PRECISION) == 0) {
565 Precision = Width;
566 }
567 }
568 }
569 }
570 break;
571
572 case 's':
573 case 'S':
574 Flags |= ARGUMENT_UNICODE;
575 //
576 // break skipped on purpose
577 //
578 case 'a':
579 ArgumentString = (CHAR8 *)VA_ARG (Marker, CHAR8 *);
580 if (ArgumentString == NULL) {
581 Flags &= (~ARGUMENT_UNICODE);
582 ArgumentString = "<null string>";
583 }
584 //
585 // Set the default precision for string to be zero if not specified.
586 //
587 if ((Flags & PRECISION) == 0) {
588 Precision = 0;
589 }
590 break;
591
592 case 'c':
593 Character = VA_ARG (Marker, UINTN) & 0xffff;
594 ArgumentString = (CHAR8 *)&Character;
595 Flags |= ARGUMENT_UNICODE;
596 break;
597
598 case 'g':
599 TmpGuid = VA_ARG (Marker, GUID *);
600 if (TmpGuid == NULL) {
601 ArgumentString = "<null guid>";
602 } else {
603 BasePrintLibSPrint (
604 ValueBuffer,
605 MAXIMUM_VALUE_CHARACTERS,
606 0,
607 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
608 TmpGuid->Data1,
609 TmpGuid->Data2,
610 TmpGuid->Data3,
611 TmpGuid->Data4[0],
612 TmpGuid->Data4[1],
613 TmpGuid->Data4[2],
614 TmpGuid->Data4[3],
615 TmpGuid->Data4[4],
616 TmpGuid->Data4[5],
617 TmpGuid->Data4[6],
618 TmpGuid->Data4[7]
619 );
620 ArgumentString = ValueBuffer;
621 }
622 break;
623
624 case 't':
625 TmpTime = VA_ARG (Marker, TIME *);
626 if (TmpTime == NULL) {
627 ArgumentString = "<null time>";
628 } else {
629 BasePrintLibSPrint (
630 ValueBuffer,
631 MAXIMUM_VALUE_CHARACTERS,
632 0,
633 "%02d/%02d/%04d %02d:%02d",
634 TmpTime->Month,
635 TmpTime->Day,
636 TmpTime->Year,
637 TmpTime->Hour,
638 TmpTime->Minute
639 );
640 ArgumentString = ValueBuffer;
641 }
642 break;
643
644 case 'r':
645 Status = VA_ARG (Marker, RETURN_STATUS);
646 ArgumentString = ValueBuffer;
647 if (RETURN_ERROR (Status)) {
648 //
649 // Clear error bit
650 //
651 Index = Status & ~MAX_BIT;
652 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {
653 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];
654 }
655 } else {
656 Index = Status;
657 if (Index <= WARNING_STATUS_NUMBER) {
658 ArgumentString = mStatusString [Index];
659 }
660 }
661 if (ArgumentString == ValueBuffer) {
662 BasePrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);
663 }
664 break;
665
666 case '\n':
667 ArgumentString = "\n\r";
668 break;
669
670 case '%':
671 default:
672 //
673 // if the type is '%' or unknown, then print it to the screen
674 //
675 ArgumentString = (CHAR8 *)&FormatCharacter;
676 Flags |= ARGUMENT_UNICODE;
677 break;
678 }
679 break;
680
681 case '\n':
682 ArgumentString = "\n\r";
683 break;
684
685 default:
686 ArgumentString = (CHAR8 *)&FormatCharacter;
687 Flags |= ARGUMENT_UNICODE;
688 break;
689 }
690
691 //
692 // Retrieve the ArgumentString attriubutes
693 //
694 if ((Flags & ARGUMENT_UNICODE) != 0) {
695 ArgumentMask = 0xffff;
696 BytesPerArgumentCharacter = 2;
697 } else {
698 ArgumentMask = 0xff;
699 BytesPerArgumentCharacter = 1;
700 }
701 if ((Flags & ARGUMENT_REVERSED) != 0) {
702 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;
703 } else {
704 //
705 // Compute the number of characters in ArgumentString and store it in Count
706 // ArgumentString is either null-terminated, or it contains Precision characters
707 //
708 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {
709 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;
710 if (ArgumentCharacter == 0) {
711 break;
712 }
713 }
714 }
715
716 if (Precision < Count) {
717 Precision = Count;
718 }
719
720 //
721 // Pad before the string
722 //
723 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {
724 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
725 }
726
727 if (ZeroPad) {
728 if (Prefix != 0) {
729 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
730 }
731 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);
732 } else {
733 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);
734 if (Prefix != 0) {
735 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
736 }
737 }
738
739 //
740 // Output the Prefix character if it is present
741 //
742 Index = 0;
743 if (Prefix != 0) {
744 Index++;
745 }
746
747 //
748 // Copy the string into the output buffer performing the required type conversions
749 //
750 while (Index < Count) {
751 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;
752
753 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);
754 ArgumentString += BytesPerArgumentCharacter;
755 Index++;
756 if (Comma) {
757 Digits++;
758 if (Digits == 3) {
759 Digits = 0;
760 Index++;
761 if (Index < Count) {
762 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);
763 }
764 }
765 }
766 }
767
768 //
769 // Pad after the string
770 //
771 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {
772 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
773 }
774
775 //
776 // Get the next character from the format string
777 //
778 Format += BytesPerFormatCharacter;
779
780 //
781 // Get the next character from the format string
782 //
783 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
784 }
785
786 //
787 // Null terminate the Unicode or ASCII string
788 //
789 BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);
790 //
791 // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength
792 // Unicode characters if PcdMaximumUnicodeStringLength is not zero.
793 //
794 ASSERT ((((Flags & OUTPUT_UNICODE) == 0)) || (StrSize ((CHAR16 *) OriginalBuffer) != 0));
795 //
796 // Make sure output buffer cannot contain more than PcdMaximumAsciiStringLength
797 // ASCII characters if PcdMaximumAsciiStringLength is not zero.
798 //
799 ASSERT ((((Flags & OUTPUT_UNICODE) != 0)) || (AsciiStrSize (OriginalBuffer) != 0));
800
801 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);
802 }
803
804 /**
805 Worker function that produces a Null-terminated string in an output buffer
806 based on a Null-terminated format string and variable argument list.
807
808 VSPrint function to process format and place the results in Buffer. Since a
809 VA_LIST is used this routine allows the nesting of Vararg routines. Thus
810 this is the main print working routine
811
812 @param StartOfBuffer Character buffer to print the results of the parsing
813 of Format into.
814 @param BufferSize Maximum number of characters to put into buffer.
815 Zero means no limit.
816 @param Flags Initial flags value.
817 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set
818 @param FormatString Null-terminated format string.
819 @param ... The variable argument list.
820
821 @return Number of characters printed.
822
823 **/
824 UINTN
825 BasePrintLibSPrint (
826 OUT CHAR8 *StartOfBuffer,
827 IN UINTN BufferSize,
828 IN UINTN Flags,
829 IN CONST CHAR8 *FormatString,
830 ...
831 )
832 {
833 VA_LIST Marker;
834
835 VA_START (Marker, FormatString);
836 return BasePrintLibVSPrint (StartOfBuffer, BufferSize, Flags, FormatString, Marker);
837 }