]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePrintLib/PrintLibInternal.c
Synchronize MdePkg h files to Library/Base* c files.
[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 @param Character Character to be placed into Buffer.
65 @param Increment Character increment in Buffer.
66
67 @return Buffer Buffer filled with the input Character.
68
69 **/
70 CHAR8 *
71 BasePrintLibFillBuffer (
72 OUT CHAR8 *Buffer,
73 IN CHAR8 *EndBuffer,
74 IN INTN Length,
75 IN UINTN Character,
76 IN INTN Increment
77 )
78 {
79 INTN Index;
80
81 for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {
82 *Buffer = (CHAR8) Character;
83 *(Buffer + 1) = (CHAR8) (Character >> 8);
84 Buffer += Increment;
85 }
86 return Buffer;
87 }
88
89 /**
90 Internal function that convert a decimal number to a string in Buffer.
91
92 Print worker function that convert a decimal number to a string in Buffer.
93
94 @param Buffer Location to place the Unicode or ASCII string of Value.
95 @param Value Value to convert to a Decimal or Hexidecimal string in Buffer.
96 @param Radix Radix of the value
97
98 @return Number of characters printed.
99
100 **/
101 UINTN
102 BasePrintLibValueToString (
103 IN OUT CHAR8 *Buffer,
104 IN INT64 Value,
105 IN UINTN Radix
106 )
107 {
108 UINTN Digits;
109 UINT32 Remainder;
110
111 //
112 // Loop to convert one digit at a time in reverse order
113 //
114 *(Buffer++) = 0;
115 Digits = 0;
116 do {
117 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);
118 *(Buffer++) = mHexStr[Remainder];
119 Digits++;
120 } while (Value != 0);
121
122 //
123 // the length of Buffer string converted from Value
124 //
125 return Digits;
126 }
127
128 /**
129 Internal function that converts a decimal value to a Null-terminated string.
130
131 Converts the decimal number specified by Value to a Null-terminated
132 string specified by Buffer containing at most Width characters.
133 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
134 The total number of characters placed in Buffer is returned.
135 If the conversion contains more than Width characters, then only the first
136 Width characters are returned, and the total number of characters
137 required to perform the conversion is returned.
138 Additional conversion parameters are specified in Flags.
139 The Flags bit LEFT_JUSTIFY is always ignored.
140 All conversions are left justified in Buffer.
141 If Width is 0, PREFIX_ZERO is ignored in Flags.
142 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
143 are inserted every 3rd digit starting from the right.
144 If Value is < 0, then the fist character in Buffer is a '-'.
145 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
146 then Buffer is padded with '0' characters so the combination of the optional '-'
147 sign character, '0' characters, digit characters for Value, and the Null-terminator
148 add up to Width characters.
149
150 If Buffer is NULL, then ASSERT().
151 If unsupported bits are set in Flags, then ASSERT().
152 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
153
154 @param Buffer Pointer to the output buffer for the produced Null-terminated
155 string.
156 @param Flags The bitmask of flags that specify left justification, zero pad,
157 and commas.
158 @param Value The 64-bit signed value to convert to a string.
159 @param Width The maximum number of characters to place in Buffer, not including
160 the Null-terminator.
161 @param Increment Character increment in Buffer.
162
163 @return Total number of characters required to perform the conversion.
164
165 **/
166 UINTN
167 BasePrintLibConvertValueToString (
168 IN OUT CHAR8 *Buffer,
169 IN UINTN Flags,
170 IN INT64 Value,
171 IN UINTN Width,
172 IN UINTN Increment
173 )
174 {
175 CHAR8 *OriginalBuffer;
176 CHAR8 *EndBuffer;
177 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
178 UINTN Count;
179 UINTN Digits;
180 UINTN Index;
181 UINTN Radix;
182
183 //
184 // Make sure Buffer is not NULL and Width < MAXIMUM
185 //
186 ASSERT (Buffer != NULL);
187 ASSERT (Width < MAXIMUM_VALUE_CHARACTERS);
188 //
189 // Make sure Flags can only contain supported bits.
190 //
191 ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0);
192
193 //
194 // If both COMMA_TYPE and HEX_RADIX are set, then ASSERT ()
195 //
196 ASSERT (((Flags & COMMA_TYPE) != 0 && (Flags & RADIX_HEX) != 0) == FALSE);
197
198 OriginalBuffer = Buffer;
199
200 //
201 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.
202 //
203 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {
204 Flags &= (~PREFIX_ZERO);
205 }
206 //
207 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
208 //
209 if (Width == 0) {
210 Width = MAXIMUM_VALUE_CHARACTERS - 1;
211 }
212 //
213 // Set the tag for the end of the input Buffer.
214 //
215 EndBuffer = Buffer + Width * Increment;
216
217 //
218 // Convert decimal negative
219 //
220 if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {
221 Value = -Value;
222 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);
223 Width--;
224 }
225
226 //
227 // Count the length of the value string.
228 //
229 Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;
230 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix);
231
232 //
233 // Append Zero
234 //
235 if ((Flags & PREFIX_ZERO) != 0) {
236 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);
237 }
238
239 //
240 // Print Comma type for every 3 characters
241 //
242 Digits = Count % 3;
243 if (Digits != 0) {
244 Digits = 3 - Digits;
245 }
246 for (Index = 0; Index < Count; Index++) {
247 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ValueBuffer[Count - Index], Increment);
248 if ((Flags & COMMA_TYPE) != 0) {
249 Digits++;
250 if (Digits == 3) {
251 Digits = 0;
252 if ((Index + 1) < Count) {
253 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);
254 }
255 }
256 }
257 }
258
259 //
260 // Print Null-terminator
261 //
262 BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);
263
264 return ((Buffer - OriginalBuffer) / Increment);
265 }
266
267 /**
268 Worker function that produces a Null-terminated string in an output buffer
269 based on a Null-terminated format string and a VA_LIST argument list.
270
271 VSPrint function to process format and place the results in Buffer. Since a
272 VA_LIST is used this rountine allows the nesting of Vararg routines. Thus
273 this is the main print working routine.
274
275 @param Buffer Character buffer to print the results of the parsing
276 of Format into.
277 @param BufferSize Maximum number of characters to put into buffer.
278 @param Flags Intial flags value.
279 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set.
280 @param Format Null-terminated format string.
281 @param Marker Vararg list consumed by processing Format.
282
283 @return Number of characters printed not including the Null-terminator.
284
285 **/
286 UINTN
287 BasePrintLibVSPrint (
288 OUT CHAR8 *Buffer,
289 IN UINTN BufferSize,
290 IN UINTN Flags,
291 IN CONST CHAR8 *Format,
292 IN VA_LIST Marker
293 )
294 {
295 CHAR8 *OriginalBuffer;
296 CHAR8 *EndBuffer;
297 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
298 UINTN BytesPerOutputCharacter;
299 UINTN BytesPerFormatCharacter;
300 UINTN FormatMask;
301 UINTN FormatCharacter;
302 UINTN Width;
303 UINTN Precision;
304 INT64 Value;
305 CONST CHAR8 *ArgumentString;
306 UINTN Character;
307 GUID *TmpGuid;
308 TIME *TmpTime;
309 UINTN Count;
310 UINTN ArgumentMask;
311 INTN BytesPerArgumentCharacter;
312 UINTN ArgumentCharacter;
313 BOOLEAN Done;
314 UINTN Index;
315 CHAR8 Prefix;
316 BOOLEAN ZeroPad;
317 BOOLEAN Comma;
318 UINTN Digits;
319 UINTN Radix;
320 RETURN_STATUS Status;
321
322 if (BufferSize == 0) {
323 return 0;
324 }
325 ASSERT (Buffer != NULL);
326
327 if ((Flags & OUTPUT_UNICODE) != 0) {
328 BytesPerOutputCharacter = 2;
329 } else {
330 BytesPerOutputCharacter = 1;
331 }
332
333 //
334 // Reserve space for the Null terminator.
335 //
336 BufferSize--;
337 OriginalBuffer = Buffer;
338 //
339 // Set the tag for the end of the input Buffer.
340 //
341 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;
342
343 if ((Flags & FORMAT_UNICODE) != 0) {
344 //
345 // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength
346 // Unicode characters if PcdMaximumUnicodeStringLength is not zero.
347 //
348 ASSERT (StrSize ((CHAR16 *) Format) != 0);
349 BytesPerFormatCharacter = 2;
350 FormatMask = 0xffff;
351 } else {
352 //
353 // Make sure format string cannot contain more than PcdMaximumAsciiStringLength
354 // Ascii characters if PcdMaximumAsciiStringLength is not zero.
355 //
356 ASSERT (AsciiStrSize (Format) != 0);
357 BytesPerFormatCharacter = 1;
358 FormatMask = 0xff;
359 }
360
361
362
363 //
364 // Get the first character from the format string
365 //
366 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
367
368 //
369 // Loop until the end of the format string is reached or the output buffer is full
370 //
371 while (FormatCharacter != 0 && Buffer < EndBuffer) {
372 //
373 // Clear all the flag bits except those that may have been passed in
374 //
375 Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE);
376
377 //
378 // Set the default width to zero, and the default precision to 1
379 //
380 Width = 0;
381 Precision = 1;
382 Prefix = 0;
383 Comma = FALSE;
384 ZeroPad = FALSE;
385 Count = 0;
386 Digits = 0;
387
388 switch (FormatCharacter) {
389 case '%':
390 //
391 // Parse Flags and Width
392 //
393 for (Done = FALSE; !Done; ) {
394 Format += BytesPerFormatCharacter;
395 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
396 switch (FormatCharacter) {
397 case '.':
398 Flags |= PRECISION;
399 break;
400 case '-':
401 Flags |= LEFT_JUSTIFY;
402 break;
403 case '+':
404 Flags |= PREFIX_SIGN;
405 break;
406 case ' ':
407 Flags |= PREFIX_BLANK;
408 break;
409 case ',':
410 Flags |= COMMA_TYPE;
411 break;
412 case 'L':
413 case 'l':
414 Flags |= LONG_TYPE;
415 break;
416 case '*':
417 if ((Flags & PRECISION) == 0) {
418 Flags |= PAD_TO_WIDTH;
419 Width = VA_ARG (Marker, UINTN);
420 } else {
421 Precision = VA_ARG (Marker, UINTN);
422 }
423 break;
424 case '0':
425 if ((Flags & PRECISION) == 0) {
426 Flags |= PREFIX_ZERO;
427 }
428 case '1':
429 case '2':
430 case '3':
431 case '4':
432 case '5':
433 case '6':
434 case '7':
435 case '8':
436 case '9':
437 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){
438 Count = (Count * 10) + FormatCharacter - '0';
439 Format += BytesPerFormatCharacter;
440 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
441 }
442 Format -= BytesPerFormatCharacter;
443 if ((Flags & PRECISION) == 0) {
444 Flags |= PAD_TO_WIDTH;
445 Width = Count;
446 } else {
447 Precision = Count;
448 }
449 break;
450
451 case '\0':
452 //
453 // Make no output if Format string terminates unexpectedly when
454 // looking up for flag, width, precision and type.
455 //
456 Format -= BytesPerFormatCharacter;
457 Precision = 0;
458 //
459 // break skipped on purpose.
460 //
461 default:
462 Done = TRUE;
463 break;
464 }
465 }
466
467 //
468 // Handle each argument type
469 //
470 switch (FormatCharacter) {
471 case 'p':
472 //
473 // Flag space, +, 0, L & l are invalid for type p.
474 //
475 Flags &= ~(PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE);
476 if (sizeof (VOID *) > 4) {
477 Flags |= LONG_TYPE;
478 }
479 case 'X':
480 Flags |= PREFIX_ZERO;
481 //
482 // break skipped on purpose
483 //
484 case 'x':
485 Flags |= RADIX_HEX;
486 //
487 // break skipped on purpose
488 //
489 case 'd':
490 if ((Flags & LONG_TYPE) == 0) {
491 Value = (VA_ARG (Marker, int));
492 } else {
493 Value = VA_ARG (Marker, INT64);
494 }
495 if ((Flags & PREFIX_BLANK) != 0) {
496 Prefix = ' ';
497 }
498 if ((Flags & PREFIX_SIGN) != 0) {
499 Prefix = '+';
500 }
501 if ((Flags & COMMA_TYPE) != 0) {
502 Comma = TRUE;
503 }
504 if ((Flags & RADIX_HEX) == 0) {
505 Radix = 10;
506 if (Comma) {
507 Flags &= (~PREFIX_ZERO);
508 Precision = 1;
509 }
510 if (Value < 0) {
511 Flags |= PREFIX_SIGN;
512 Prefix = '-';
513 Value = -Value;
514 }
515 } else {
516 Radix = 16;
517 Comma = FALSE;
518 if ((Flags & LONG_TYPE) == 0 && Value < 0) {
519 Value = (unsigned int)Value;
520 }
521 }
522 //
523 // Convert Value to a reversed string
524 //
525 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix);
526 if (Value == 0 && Precision == 0) {
527 Count = 0;
528 }
529 ArgumentString = (CHAR8 *)ValueBuffer + Count;
530
531 Digits = Count % 3;
532 if (Digits != 0) {
533 Digits = 3 - Digits;
534 }
535 if (Comma && Count != 0) {
536 Count += ((Count - 1) / 3);
537 }
538 if (Prefix != 0) {
539 Count++;
540 Precision++;
541 }
542 Flags |= ARGUMENT_REVERSED;
543 ZeroPad = TRUE;
544 if ((Flags & PREFIX_ZERO) != 0) {
545 if ((Flags & LEFT_JUSTIFY) == 0) {
546 if ((Flags & PAD_TO_WIDTH) != 0) {
547 if ((Flags & PRECISION) == 0) {
548 Precision = Width;
549 }
550 }
551 }
552 }
553 break;
554
555 case 's':
556 case 'S':
557 Flags |= ARGUMENT_UNICODE;
558 //
559 // break skipped on purpose
560 //
561 case 'a':
562 ArgumentString = (CHAR8 *)VA_ARG (Marker, CHAR8 *);
563 if (ArgumentString == NULL) {
564 Flags &= (~ARGUMENT_UNICODE);
565 ArgumentString = "<null string>";
566 }
567 //
568 // Set the default precision for string to be zero if not specified.
569 //
570 if ((Flags & PRECISION) == 0) {
571 Precision = 0;
572 }
573 break;
574
575 case 'c':
576 Character = VA_ARG (Marker, UINTN) & 0xffff;
577 ArgumentString = (CHAR8 *)&Character;
578 Flags |= ARGUMENT_UNICODE;
579 break;
580
581 case 'g':
582 TmpGuid = VA_ARG (Marker, GUID *);
583 if (TmpGuid == NULL) {
584 ArgumentString = "<null guid>";
585 } else {
586 BasePrintLibSPrint (
587 ValueBuffer,
588 MAXIMUM_VALUE_CHARACTERS,
589 0,
590 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
591 TmpGuid->Data1,
592 TmpGuid->Data2,
593 TmpGuid->Data3,
594 TmpGuid->Data4[0],
595 TmpGuid->Data4[1],
596 TmpGuid->Data4[2],
597 TmpGuid->Data4[3],
598 TmpGuid->Data4[4],
599 TmpGuid->Data4[5],
600 TmpGuid->Data4[6],
601 TmpGuid->Data4[7]
602 );
603 ArgumentString = ValueBuffer;
604 }
605 break;
606
607 case 't':
608 TmpTime = VA_ARG (Marker, TIME *);
609 if (TmpTime == NULL) {
610 ArgumentString = "<null time>";
611 } else {
612 BasePrintLibSPrint (
613 ValueBuffer,
614 MAXIMUM_VALUE_CHARACTERS,
615 0,
616 "%02d/%02d/%04d %02d:%02d",
617 TmpTime->Month,
618 TmpTime->Day,
619 TmpTime->Year,
620 TmpTime->Hour,
621 TmpTime->Minute
622 );
623 ArgumentString = ValueBuffer;
624 }
625 break;
626
627 case 'r':
628 Status = VA_ARG (Marker, RETURN_STATUS);
629 ArgumentString = ValueBuffer;
630 if (RETURN_ERROR (Status)) {
631 //
632 // Clear error bit
633 //
634 Index = Status & ~MAX_BIT;
635 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {
636 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];
637 }
638 } else {
639 Index = Status;
640 if (Index <= WARNING_STATUS_NUMBER) {
641 ArgumentString = mStatusString [Index];
642 }
643 }
644 if (ArgumentString == ValueBuffer) {
645 BasePrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);
646 }
647 break;
648
649 case '\n':
650 ArgumentString = "\n\r";
651 break;
652
653 case '%':
654 default:
655 //
656 // if the type is '%' or unknown, then print it to the screen
657 //
658 ArgumentString = (CHAR8 *)&FormatCharacter;
659 Flags |= ARGUMENT_UNICODE;
660 break;
661 }
662 break;
663
664 case '\n':
665 ArgumentString = "\n\r";
666 break;
667
668 default:
669 ArgumentString = (CHAR8 *)&FormatCharacter;
670 Flags |= ARGUMENT_UNICODE;
671 break;
672 }
673
674 //
675 // Retrieve the ArgumentString attriubutes
676 //
677 if ((Flags & ARGUMENT_UNICODE) != 0) {
678 ArgumentMask = 0xffff;
679 BytesPerArgumentCharacter = 2;
680 } else {
681 ArgumentMask = 0xff;
682 BytesPerArgumentCharacter = 1;
683 }
684 if ((Flags & ARGUMENT_REVERSED) != 0) {
685 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;
686 } else {
687 //
688 // Compute the number of characters in ArgumentString and store it in Count
689 // ArgumentString is either null-terminated, or it contains Precision characters
690 //
691 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {
692 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;
693 if (ArgumentCharacter == 0) {
694 break;
695 }
696 }
697 }
698
699 if (Precision < Count) {
700 Precision = Count;
701 }
702
703 //
704 // Pad before the string
705 //
706 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {
707 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
708 }
709
710 if (ZeroPad) {
711 if (Prefix != 0) {
712 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
713 }
714 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);
715 } else {
716 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);
717 if (Prefix != 0) {
718 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
719 }
720 }
721
722 //
723 // Output the Prefix character if it is present
724 //
725 Index = 0;
726 if (Prefix != 0) {
727 Index++;
728 }
729
730 //
731 // Copy the string into the output buffer performing the required type conversions
732 //
733 while (Index < Count) {
734 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;
735
736 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);
737 ArgumentString += BytesPerArgumentCharacter;
738 Index++;
739 if (Comma) {
740 Digits++;
741 if (Digits == 3) {
742 Digits = 0;
743 Index++;
744 if (Index < Count) {
745 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);
746 }
747 }
748 }
749 }
750
751 //
752 // Pad after the string
753 //
754 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {
755 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
756 }
757
758 //
759 // Get the next character from the format string
760 //
761 Format += BytesPerFormatCharacter;
762
763 //
764 // Get the next character from the format string
765 //
766 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;
767 }
768
769 //
770 // Null terminate the Unicode or ASCII string
771 //
772 BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);
773 //
774 // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength
775 // Unicode characters if PcdMaximumUnicodeStringLength is not zero.
776 //
777 ASSERT ((((Flags & OUTPUT_UNICODE) == 0)) || (StrSize ((CHAR16 *) OriginalBuffer) != 0));
778 //
779 // Make sure output buffer cannot contain more than PcdMaximumAsciiStringLength
780 // ASCII characters if PcdMaximumAsciiStringLength is not zero.
781 //
782 ASSERT ((((Flags & OUTPUT_UNICODE) != 0)) || (AsciiStrSize (OriginalBuffer) != 0));
783
784 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);
785 }
786
787 /**
788 Worker function that produces a Null-terminated string in an output buffer
789 based on a Null-terminated format string and variable argument list.
790
791 VSPrint function to process format and place the results in Buffer. Since a
792 VA_LIST is used this rountine allows the nesting of Vararg routines. Thus
793 this is the main print working routine
794
795 @param StartOfBuffer Character buffer to print the results of the parsing
796 of Format into.
797 @param BufferSize Maximum number of characters to put into buffer.
798 Zero means no limit.
799 @param Flags Intial flags value.
800 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set
801 @param FormatString Null-terminated format string.
802 @param ... The variable argument list.
803
804 @return Number of characters printed.
805
806 **/
807 UINTN
808 BasePrintLibSPrint (
809 OUT CHAR8 *StartOfBuffer,
810 IN UINTN BufferSize,
811 IN UINTN Flags,
812 IN CONST CHAR8 *FormatString,
813 ...
814 )
815 {
816 VA_LIST Marker;
817
818 VA_START (Marker, FormatString);
819 return BasePrintLibVSPrint (StartOfBuffer, BufferSize, Flags, FormatString, Marker);
820 }