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