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