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