]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BasePrintLib/PrintLibInternal.c
MdeModulePkg/PciBusDxe: Refine code to make it more readable
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLibInternal.c
CommitLineData
e1f414b6 1/** @file\r
eceb3a4c 2 Print Library internal worker functions.\r
e1f414b6 3\r
9ade4339 4 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
19388d29 5 This program and the accompanying materials\r
e1f414b6 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
2fc59a00 8 http://opensource.org/licenses/bsd-license.php.\r
e1f414b6 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
0df625fa
LG
17#define WARNING_STATUS_NUMBER 5\r
18#define ERROR_STATUS_NUMBER 33\r
e389c39b 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
d3265fe1 22GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 * CONST mStatusString[] = {\r
e389c39b 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
0df625fa 28 "Warning Stale Data", // RETURN_WARN_STALE_DATA = 5\r
e389c39b 29 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT\r
30 "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT\r
31 "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT\r
32 "Bad Buffer Size", // RETURN_BAD_BUFFER_SIZE = 4 | MAX_BIT\r
33 "Buffer Too Small", // RETURN_BUFFER_TOO_SMALL, = 5 | MAX_BIT\r
34 "Not Ready", // RETURN_NOT_READY = 6 | MAX_BIT\r
35 "Device Error", // RETURN_DEVICE_ERROR = 7 | MAX_BIT\r
36 "Write Protected", // RETURN_WRITE_PROTECTED = 8 | MAX_BIT\r
37 "Out of Resources", // RETURN_OUT_OF_RESOURCES = 9 | MAX_BIT\r
38 "Volume Corrupt", // RETURN_VOLUME_CORRUPTED = 10 | MAX_BIT\r
39 "Volume Full", // RETURN_VOLUME_FULL = 11 | MAX_BIT\r
40 "No Media", // RETURN_NO_MEDIA = 12 | MAX_BIT\r
41 "Media changed", // RETURN_MEDIA_CHANGED = 13 | MAX_BIT\r
42 "Not Found", // RETURN_NOT_FOUND = 14 | MAX_BIT\r
43 "Access Denied", // RETURN_ACCESS_DENIED = 15 | MAX_BIT\r
44 "No Response", // RETURN_NO_RESPONSE = 16 | MAX_BIT\r
45 "No mapping", // RETURN_NO_MAPPING = 17 | MAX_BIT\r
46 "Time out", // RETURN_TIMEOUT = 18 | MAX_BIT\r
47 "Not started", // RETURN_NOT_STARTED = 19 | MAX_BIT\r
48 "Already started", // RETURN_ALREADY_STARTED = 20 | MAX_BIT\r
49 "Aborted", // RETURN_ABORTED = 21 | MAX_BIT\r
50 "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT\r
51 "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT\r
5e2fd937 52 "Protocol Error", // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT\r
0df625fa
LG
53 "Incompatible Version", // RETURN_INCOMPATIBLE_VERSION = 25 | MAX_BIT\r
54 "Security Violation", // RETURN_SECURITY_VIOLATION = 26 | MAX_BIT\r
55 "CRC Error", // RETURN_CRC_ERROR = 27 | MAX_BIT\r
56 "End of Media", // RETURN_END_OF_MEDIA = 28 | MAX_BIT\r
57 "Reserved (29)", // RESERVED = 29 | MAX_BIT\r
58 "Reserved (30)", // RESERVED = 30 | MAX_BIT\r
59 "End of File", // RETURN_END_OF_FILE = 31 | MAX_BIT\r
60 "Invalid Language", // RETURN_INVALID_LANGUAGE = 32 | MAX_BIT\r
61 "Compromised Data" // RETURN_COMPROMISED_DATA = 33 | MAX_BIT\r
e389c39b 62};\r
63\r
e1f414b6 64\r
65/**\r
66 Internal function that places the character into the Buffer.\r
67\r
68 Internal function that places ASCII or Unicode character into the Buffer.\r
69\r
2fc59a00 70 @param Buffer The buffer to place the Unicode or ASCII string.\r
e1f414b6 71 @param EndBuffer The end of the input Buffer. No characters will be\r
72 placed after that. \r
2fc59a00 73 @param Length The count of character to be placed into Buffer.\r
a2bc29c4 74 (Negative value indicates no buffer fill.)\r
2fc59a00 75 @param Character The character to be placed into Buffer.\r
76 @param Increment The character increment in Buffer.\r
e1f414b6 77\r
2fc59a00 78 @return Buffer.\r
e1f414b6 79\r
80**/\r
81CHAR8 *\r
82BasePrintLibFillBuffer (\r
eceb3a4c
LG
83 OUT CHAR8 *Buffer,\r
84 IN CHAR8 *EndBuffer,\r
85 IN INTN Length,\r
86 IN UINTN Character,\r
87 IN INTN Increment\r
e1f414b6 88 )\r
89{\r
a2bc29c4 90 INTN Index;\r
bcccf0b0 91 \r
a2bc29c4 92 for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {\r
93 *Buffer = (CHAR8) Character;\r
94 if (Increment != 1) {\r
95 *(Buffer + 1) = (CHAR8)(Character >> 8);\r
96 }\r
97 Buffer += Increment;\r
98 }\r
99\r
100 return Buffer;\r
e1f414b6 101}\r
102\r
103/**\r
7c905091 104 Internal function that convert a number to a string in Buffer.\r
e1f414b6 105\r
7c905091 106 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.\r
e1f414b6 107\r
7c905091 108 @param Buffer Location to place the ASCII string of Value.\r
2fc59a00 109 @param Value The value to convert to a Decimal or Hexadecimal string in Buffer.\r
e1f414b6 110 @param Radix Radix of the value\r
111\r
7c905091 112 @return A pointer to the end of buffer filled with ASCII string.\r
e1f414b6 113\r
114**/\r
7c905091 115CHAR8 *\r
e1f414b6 116BasePrintLibValueToString (\r
efb23117 117 IN OUT CHAR8 *Buffer, \r
118 IN INT64 Value, \r
e1f414b6 119 IN UINTN Radix\r
120 )\r
121{\r
e1f414b6 122 UINT32 Remainder;\r
123\r
124 //\r
125 // Loop to convert one digit at a time in reverse order\r
126 //\r
7c905091 127 *Buffer = 0;\r
e1f414b6 128 do {\r
129 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r
7c905091 130 *(++Buffer) = mHexStr[Remainder];\r
e1f414b6 131 } while (Value != 0);\r
eceb3a4c
LG
132\r
133 //\r
7c905091 134 // Return pointer of the end of filled buffer.\r
eceb3a4c 135 //\r
7c905091 136 return Buffer;\r
e1f414b6 137}\r
138\r
139/**\r
140 Internal function that converts a decimal value to a Null-terminated string.\r
141 \r
142 Converts the decimal number specified by Value to a Null-terminated \r
143 string specified by Buffer containing at most Width characters.\r
144 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
efb23117 145 The total number of characters placed in Buffer is returned.\r
e1f414b6 146 If the conversion contains more than Width characters, then only the first\r
147 Width characters are returned, and the total number of characters \r
148 required to perform the conversion is returned.\r
149 Additional conversion parameters are specified in Flags. \r
150 The Flags bit LEFT_JUSTIFY is always ignored.\r
151 All conversions are left justified in Buffer.\r
152 If Width is 0, PREFIX_ZERO is ignored in Flags.\r
153 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r
154 are inserted every 3rd digit starting from the right.\r
efb23117 155 If Value is < 0, then the fist character in Buffer is a '-'.\r
e1f414b6 156 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r
157 then Buffer is padded with '0' characters so the combination of the optional '-' \r
158 sign character, '0' characters, digit characters for Value, and the Null-terminator\r
159 add up to Width characters.\r
e1f414b6 160\r
161 If Buffer is NULL, then ASSERT().\r
162 If unsupported bits are set in Flags, then ASSERT().\r
163 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r
164\r
2fc59a00 165 @param Buffer The pointer to the output buffer for the produced Null-terminated\r
e1f414b6 166 string.\r
167 @param Flags The bitmask of flags that specify left justification, zero pad,\r
168 and commas.\r
169 @param Value The 64-bit signed value to convert to a string.\r
170 @param Width The maximum number of characters to place in Buffer, not including\r
171 the Null-terminator.\r
2fc59a00 172 @param Increment The character increment in Buffer.\r
e1f414b6 173 \r
efb23117 174 @return Total number of characters required to perform the conversion.\r
e1f414b6 175\r
176**/\r
177UINTN\r
178BasePrintLibConvertValueToString (\r
179 IN OUT CHAR8 *Buffer,\r
180 IN UINTN Flags,\r
181 IN INT64 Value,\r
182 IN UINTN Width,\r
183 IN UINTN Increment\r
184 )\r
185{\r
186 CHAR8 *OriginalBuffer;\r
187 CHAR8 *EndBuffer;\r
188 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
7c905091 189 CHAR8 *ValueBufferPtr;\r
e1f414b6 190 UINTN Count;\r
191 UINTN Digits;\r
192 UINTN Index;\r
193 UINTN Radix;\r
194\r
eceb3a4c
LG
195 //\r
196 // Make sure Buffer is not NULL and Width < MAXIMUM\r
197 //\r
e1f414b6 198 ASSERT (Buffer != NULL);\r
199 ASSERT (Width < MAXIMUM_VALUE_CHARACTERS);\r
200 //\r
201 // Make sure Flags can only contain supported bits.\r
202 //\r
203 ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0);\r
204\r
205 //\r
df8d0595 206 // If both COMMA_TYPE and RADIX_HEX are set, then ASSERT ()\r
e1f414b6 207 //\r
841c0770 208 ASSERT (((Flags & COMMA_TYPE) == 0) || ((Flags & RADIX_HEX) == 0));\r
e1f414b6 209\r
210 OriginalBuffer = Buffer;\r
eceb3a4c
LG
211 \r
212 //\r
213 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.\r
214 //\r
e1f414b6 215 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r
e935092f 216 Flags &= ~((UINTN) PREFIX_ZERO);\r
e1f414b6 217 }\r
eceb3a4c
LG
218 //\r
219 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
220 //\r
e1f414b6 221 if (Width == 0) {\r
222 Width = MAXIMUM_VALUE_CHARACTERS - 1;\r
223 }\r
224 //\r
225 // Set the tag for the end of the input Buffer.\r
226 //\r
227 EndBuffer = Buffer + Width * Increment;\r
eceb3a4c
LG
228 \r
229 //\r
230 // Convert decimal negative\r
231 //\r
e1f414b6 232 if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {\r
233 Value = -Value;\r
234 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);\r
235 Width--;\r
236 }\r
eceb3a4c
LG
237 \r
238 //\r
239 // Count the length of the value string.\r
240 //\r
e1f414b6 241 Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;\r
7c905091 242 ValueBufferPtr = BasePrintLibValueToString (ValueBuffer, Value, Radix);\r
243 Count = ValueBufferPtr - ValueBuffer;\r
eceb3a4c
LG
244 \r
245 //\r
246 // Append Zero\r
247 //\r
e1f414b6 248 if ((Flags & PREFIX_ZERO) != 0) {\r
249 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);\r
250 }\r
eceb3a4c
LG
251 \r
252 //\r
253 // Print Comma type for every 3 characters\r
254 //\r
e1f414b6 255 Digits = Count % 3;\r
256 if (Digits != 0) {\r
257 Digits = 3 - Digits;\r
258 }\r
259 for (Index = 0; Index < Count; Index++) {\r
7c905091 260 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, *ValueBufferPtr--, Increment);\r
e1f414b6 261 if ((Flags & COMMA_TYPE) != 0) {\r
262 Digits++;\r
263 if (Digits == 3) {\r
264 Digits = 0;\r
265 if ((Index + 1) < Count) {\r
266 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);\r
267 }\r
268 }\r
269 }\r
270 }\r
eceb3a4c
LG
271 \r
272 //\r
273 // Print Null-terminator\r
274 //\r
e1f414b6 275 BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);\r
276\r
277 return ((Buffer - OriginalBuffer) / Increment);\r
278}\r
279\r
e389c39b 280/**\r
281 Worker function that produces a Null-terminated string in an output buffer \r
282 based on a Null-terminated format string and a VA_LIST argument list.\r
283\r
284 VSPrint function to process format and place the results in Buffer. Since a \r
7c905091 285 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
e389c39b 286 this is the main print working routine.\r
287\r
f405c067 288 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.\r
289\r
290 @param[out] Buffer The character buffer to print the results of the \r
291 parsing of Format into.\r
292 @param[in] BufferSize The maximum number of characters to put into \r
293 buffer.\r
294 @param[in] Flags Initial flags value.\r
295 Can only have FORMAT_UNICODE, OUTPUT_UNICODE, \r
296 and COUNT_ONLY_NO_PRINT set.\r
297 @param[in] Format A Null-terminated format string.\r
298 @param[in] VaListMarker VA_LIST style variable argument list consumed by\r
299 processing Format.\r
300 @param[in] BaseListMarker BASE_LIST style variable argument list consumed\r
301 by processing Format.\r
e389c39b 302\r
2fc59a00 303 @return The number of characters printed not including the Null-terminator.\r
f405c067 304 If COUNT_ONLY_NO_PRINT was set returns the same, but without any\r
305 modification to Buffer.\r
e389c39b 306\r
307**/\r
308UINTN\r
2075236e 309BasePrintLibSPrintMarker (\r
e389c39b 310 OUT CHAR8 *Buffer,\r
311 IN UINTN BufferSize,\r
312 IN UINTN Flags,\r
313 IN CONST CHAR8 *Format,\r
2075236e 314 IN VA_LIST VaListMarker, OPTIONAL\r
315 IN BASE_LIST BaseListMarker OPTIONAL\r
e389c39b 316 )\r
317{\r
2075236e 318 CHAR8 *OriginalBuffer;\r
319 CHAR8 *EndBuffer;\r
320 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
f405c067 321 UINT32 BytesPerOutputCharacter;\r
2075236e 322 UINTN BytesPerFormatCharacter;\r
323 UINTN FormatMask;\r
324 UINTN FormatCharacter;\r
325 UINTN Width;\r
326 UINTN Precision;\r
327 INT64 Value;\r
328 CONST CHAR8 *ArgumentString;\r
329 UINTN Character;\r
330 GUID *TmpGuid;\r
331 TIME *TmpTime;\r
332 UINTN Count;\r
333 UINTN ArgumentMask;\r
334 INTN BytesPerArgumentCharacter;\r
335 UINTN ArgumentCharacter;\r
336 BOOLEAN Done;\r
337 UINTN Index;\r
338 CHAR8 Prefix;\r
339 BOOLEAN ZeroPad;\r
340 BOOLEAN Comma;\r
341 UINTN Digits;\r
342 UINTN Radix;\r
343 RETURN_STATUS Status;\r
4b0f378c
LG
344 UINT32 GuidData1;\r
345 UINT16 GuidData2;\r
346 UINT16 GuidData3;\r
41342801 347 UINTN LengthToReturn;\r
e389c39b 348\r
f405c067 349 //\r
350 // If you change this code be sure to match the 2 versions of this function.\r
351 // Nearly identical logic is found in the BasePrintLib and \r
352 // DxePrintLibPrint2Protocol (both PrintLib instances).\r
353 //\r
354\r
83f1f16a 355 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r
f405c067 356 if (BufferSize == 0) {\r
357 Buffer = NULL;\r
358 }\r
359 } else {\r
360 //\r
361 // We can run without a Buffer for counting only.\r
362 //\r
363 if (BufferSize == 0) {\r
364 return 0;\r
365 }\r
366 ASSERT (Buffer != NULL);\r
e389c39b 367 }\r
e389c39b 368\r
5c451334 369 if ((Flags & OUTPUT_UNICODE) != 0) {\r
e389c39b 370 BytesPerOutputCharacter = 2;\r
371 } else {\r
372 BytesPerOutputCharacter = 1;\r
373 }\r
374\r
f405c067 375 LengthToReturn = 0;\r
28087ef0
HL
376 EndBuffer = NULL;\r
377 OriginalBuffer = NULL;\r
f405c067 378\r
e389c39b 379 //\r
380 // Reserve space for the Null terminator.\r
381 //\r
28087ef0
HL
382 if (Buffer != NULL) {\r
383 BufferSize--;\r
384 OriginalBuffer = Buffer;\r
2075236e 385\r
28087ef0
HL
386 //\r
387 // Set the tag for the end of the input Buffer.\r
388 //\r
389 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;\r
390 }\r
e389c39b 391\r
392 if ((Flags & FORMAT_UNICODE) != 0) {\r
393 //\r
394 // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength\r
395 // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
396 //\r
397 ASSERT (StrSize ((CHAR16 *) Format) != 0);\r
398 BytesPerFormatCharacter = 2;\r
399 FormatMask = 0xffff;\r
400 } else {\r
401 //\r
402 // Make sure format string cannot contain more than PcdMaximumAsciiStringLength\r
403 // Ascii characters if PcdMaximumAsciiStringLength is not zero. \r
404 //\r
405 ASSERT (AsciiStrSize (Format) != 0);\r
406 BytesPerFormatCharacter = 1;\r
407 FormatMask = 0xff;\r
408 }\r
409\r
e389c39b 410 //\r
411 // Get the first character from the format string\r
412 //\r
413 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
414\r
415 //\r
416 // Loop until the end of the format string is reached or the output buffer is full\r
417 //\r
28087ef0
HL
418 while (FormatCharacter != 0) {\r
419 if ((Buffer != NULL) && (Buffer >= EndBuffer)) {\r
420 break;\r
421 }\r
e389c39b 422 //\r
423 // Clear all the flag bits except those that may have been passed in\r
424 //\r
e935092f 425 Flags &= (UINTN) (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);\r
e389c39b 426\r
427 //\r
428 // Set the default width to zero, and the default precision to 1\r
429 //\r
430 Width = 0;\r
431 Precision = 1;\r
432 Prefix = 0;\r
433 Comma = FALSE;\r
434 ZeroPad = FALSE;\r
435 Count = 0;\r
436 Digits = 0;\r
437\r
438 switch (FormatCharacter) {\r
439 case '%':\r
440 //\r
441 // Parse Flags and Width\r
442 //\r
443 for (Done = FALSE; !Done; ) {\r
444 Format += BytesPerFormatCharacter;\r
445 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
446 switch (FormatCharacter) {\r
447 case '.': \r
448 Flags |= PRECISION; \r
449 break;\r
450 case '-': \r
451 Flags |= LEFT_JUSTIFY; \r
452 break;\r
453 case '+': \r
454 Flags |= PREFIX_SIGN; \r
455 break;\r
456 case ' ': \r
457 Flags |= PREFIX_BLANK; \r
458 break;\r
459 case ',': \r
460 Flags |= COMMA_TYPE; \r
461 break;\r
462 case 'L':\r
463 case 'l': \r
464 Flags |= LONG_TYPE; \r
465 break;\r
466 case '*':\r
467 if ((Flags & PRECISION) == 0) {\r
468 Flags |= PAD_TO_WIDTH;\r
2075236e 469 if (BaseListMarker == NULL) {\r
470 Width = VA_ARG (VaListMarker, UINTN);\r
471 } else {\r
472 Width = BASE_ARG (BaseListMarker, UINTN);\r
473 }\r
e389c39b 474 } else {\r
2075236e 475 if (BaseListMarker == NULL) {\r
476 Precision = VA_ARG (VaListMarker, UINTN);\r
477 } else {\r
478 Precision = BASE_ARG (BaseListMarker, UINTN);\r
479 }\r
e389c39b 480 }\r
481 break;\r
482 case '0':\r
483 if ((Flags & PRECISION) == 0) {\r
484 Flags |= PREFIX_ZERO;\r
485 }\r
486 case '1':\r
487 case '2':\r
488 case '3':\r
489 case '4':\r
490 case '5':\r
491 case '6':\r
492 case '7':\r
493 case '8':\r
494 case '9':\r
495 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){\r
496 Count = (Count * 10) + FormatCharacter - '0';\r
497 Format += BytesPerFormatCharacter;\r
498 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
499 }\r
500 Format -= BytesPerFormatCharacter;\r
501 if ((Flags & PRECISION) == 0) {\r
502 Flags |= PAD_TO_WIDTH;\r
503 Width = Count;\r
504 } else {\r
505 Precision = Count;\r
506 }\r
507 break;\r
508 \r
509 case '\0':\r
510 //\r
511 // Make no output if Format string terminates unexpectedly when\r
512 // looking up for flag, width, precision and type. \r
513 //\r
514 Format -= BytesPerFormatCharacter;\r
515 Precision = 0;\r
516 //\r
517 // break skipped on purpose.\r
518 //\r
519 default:\r
520 Done = TRUE;\r
521 break;\r
522 }\r
523 } \r
524\r
525 //\r
526 // Handle each argument type\r
527 //\r
528 switch (FormatCharacter) {\r
529 case 'p':\r
530 //\r
531 // Flag space, +, 0, L & l are invalid for type p.\r
532 //\r
e935092f 533 Flags &= ~((UINTN) (PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE));\r
e389c39b 534 if (sizeof (VOID *) > 4) {\r
535 Flags |= LONG_TYPE;\r
536 }\r
d6bee311
ED
537 //\r
538 // break skipped on purpose\r
539 //\r
e389c39b 540 case 'X':\r
541 Flags |= PREFIX_ZERO;\r
542 //\r
543 // break skipped on purpose\r
544 //\r
545 case 'x':\r
546 Flags |= RADIX_HEX;\r
547 //\r
548 // break skipped on purpose\r
549 //\r
9ade4339
QS
550 case 'u':\r
551 if ((Flags & RADIX_HEX) == 0) {\r
552 Flags &= ~((UINTN) (PREFIX_SIGN));\r
553 Flags |= UNSIGNED_TYPE;\r
554 }\r
555 //\r
556 // break skipped on purpose\r
557 //\r
e389c39b 558 case 'd':\r
559 if ((Flags & LONG_TYPE) == 0) {\r
57dd2382 560 //\r
9ade4339 561 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
18e81d77 562 // This assumption is made so the format string definition is compatible with the ANSI C\r
57dd2382 563 // Specification for formatted strings. It is recommended that the Base Types be used \r
564 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
565 // provides an implementation that is compatible with that largest possible set of CPU \r
566 // architectures. This is why the type "int" is used in this one case.\r
567 //\r
2075236e 568 if (BaseListMarker == NULL) {\r
569 Value = VA_ARG (VaListMarker, int);\r
570 } else {\r
571 Value = BASE_ARG (BaseListMarker, int);\r
572 }\r
e389c39b 573 } else {\r
2075236e 574 if (BaseListMarker == NULL) {\r
575 Value = VA_ARG (VaListMarker, INT64);\r
576 } else {\r
577 Value = BASE_ARG (BaseListMarker, INT64);\r
578 }\r
e389c39b 579 }\r
580 if ((Flags & PREFIX_BLANK) != 0) {\r
581 Prefix = ' ';\r
582 }\r
583 if ((Flags & PREFIX_SIGN) != 0) {\r
584 Prefix = '+';\r
585 }\r
586 if ((Flags & COMMA_TYPE) != 0) {\r
587 Comma = TRUE;\r
588 }\r
589 if ((Flags & RADIX_HEX) == 0) {\r
590 Radix = 10;\r
591 if (Comma) {\r
e935092f 592 Flags &= ~((UINTN) PREFIX_ZERO);\r
e389c39b 593 Precision = 1;\r
594 }\r
9ade4339 595 if (Value < 0 && (Flags & UNSIGNED_TYPE) == 0) {\r
e389c39b 596 Flags |= PREFIX_SIGN;\r
597 Prefix = '-';\r
598 Value = -Value;\r
9ade4339
QS
599 } else if ((Flags & UNSIGNED_TYPE) != 0 && (Flags & LONG_TYPE) == 0) {\r
600 //\r
601 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
602 // This assumption is made so the format string definition is compatible with the ANSI C\r
603 // Specification for formatted strings. It is recommended that the Base Types be used \r
604 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
605 // provides an implementation that is compatible with that largest possible set of CPU \r
606 // architectures. This is why the type "unsigned int" is used in this one case.\r
607 //\r
608 Value = (unsigned int)Value;\r
e389c39b 609 }\r
610 } else {\r
611 Radix = 16;\r
612 Comma = FALSE;\r
613 if ((Flags & LONG_TYPE) == 0 && Value < 0) {\r
57dd2382 614 //\r
9ade4339 615 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
18e81d77 616 // This assumption is made so the format string definition is compatible with the ANSI C\r
57dd2382 617 // Specification for formatted strings. It is recommended that the Base Types be used \r
618 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
619 // provides an implementation that is compatible with that largest possible set of CPU \r
620 // architectures. This is why the type "unsigned int" is used in this one case.\r
621 //\r
e389c39b 622 Value = (unsigned int)Value;\r
623 }\r
624 }\r
625 //\r
626 // Convert Value to a reversed string\r
627 //\r
7c905091 628 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;\r
e389c39b 629 if (Value == 0 && Precision == 0) {\r
630 Count = 0;\r
631 }\r
632 ArgumentString = (CHAR8 *)ValueBuffer + Count;\r
633 \r
634 Digits = Count % 3;\r
635 if (Digits != 0) {\r
636 Digits = 3 - Digits;\r
637 }\r
638 if (Comma && Count != 0) {\r
639 Count += ((Count - 1) / 3);\r
640 }\r
641 if (Prefix != 0) {\r
642 Count++;\r
643 Precision++;\r
644 }\r
645 Flags |= ARGUMENT_REVERSED;\r
646 ZeroPad = TRUE;\r
647 if ((Flags & PREFIX_ZERO) != 0) {\r
648 if ((Flags & LEFT_JUSTIFY) == 0) {\r
649 if ((Flags & PAD_TO_WIDTH) != 0) {\r
650 if ((Flags & PRECISION) == 0) {\r
651 Precision = Width;\r
652 }\r
653 }\r
654 }\r
655 }\r
656 break;\r
657\r
658 case 's':\r
659 case 'S':\r
660 Flags |= ARGUMENT_UNICODE;\r
661 //\r
662 // break skipped on purpose\r
663 //\r
664 case 'a':\r
2075236e 665 if (BaseListMarker == NULL) {\r
666 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);\r
667 } else {\r
668 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);\r
669 }\r
e389c39b 670 if (ArgumentString == NULL) {\r
e935092f 671 Flags &= ~((UINTN) ARGUMENT_UNICODE);\r
e389c39b 672 ArgumentString = "<null string>";\r
673 }\r
674 //\r
675 // Set the default precision for string to be zero if not specified.\r
676 //\r
677 if ((Flags & PRECISION) == 0) {\r
678 Precision = 0;\r
679 }\r
680 break;\r
681\r
682 case 'c':\r
2075236e 683 if (BaseListMarker == NULL) {\r
684 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;\r
685 } else {\r
686 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;\r
687 }\r
e389c39b 688 ArgumentString = (CHAR8 *)&Character;\r
689 Flags |= ARGUMENT_UNICODE;\r
690 break;\r
691\r
692 case 'g':\r
2075236e 693 if (BaseListMarker == NULL) {\r
694 TmpGuid = VA_ARG (VaListMarker, GUID *);\r
695 } else {\r
696 TmpGuid = BASE_ARG (BaseListMarker, GUID *);\r
697 }\r
e389c39b 698 if (TmpGuid == NULL) {\r
699 ArgumentString = "<null guid>";\r
700 } else {\r
4b0f378c
LG
701 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));\r
702 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));\r
703 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));\r
e389c39b 704 BasePrintLibSPrint (\r
705 ValueBuffer,\r
706 MAXIMUM_VALUE_CHARACTERS, \r
707 0,\r
708 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
4b0f378c
LG
709 GuidData1,\r
710 GuidData2,\r
711 GuidData3,\r
e389c39b 712 TmpGuid->Data4[0],\r
713 TmpGuid->Data4[1],\r
714 TmpGuid->Data4[2],\r
715 TmpGuid->Data4[3],\r
716 TmpGuid->Data4[4],\r
717 TmpGuid->Data4[5],\r
718 TmpGuid->Data4[6],\r
719 TmpGuid->Data4[7]\r
720 );\r
721 ArgumentString = ValueBuffer;\r
722 }\r
723 break;\r
724\r
725 case 't':\r
2075236e 726 if (BaseListMarker == NULL) {\r
727 TmpTime = VA_ARG (VaListMarker, TIME *); \r
728 } else {\r
729 TmpTime = BASE_ARG (BaseListMarker, TIME *); \r
730 }\r
e389c39b 731 if (TmpTime == NULL) {\r
732 ArgumentString = "<null time>";\r
733 } else {\r
734 BasePrintLibSPrint (\r
735 ValueBuffer,\r
736 MAXIMUM_VALUE_CHARACTERS,\r
737 0,\r
738 "%02d/%02d/%04d %02d:%02d",\r
739 TmpTime->Month,\r
740 TmpTime->Day,\r
741 TmpTime->Year,\r
742 TmpTime->Hour,\r
743 TmpTime->Minute\r
744 );\r
745 ArgumentString = ValueBuffer;\r
746 }\r
747 break;\r
748\r
749 case 'r':\r
2075236e 750 if (BaseListMarker == NULL) {\r
751 Status = VA_ARG (VaListMarker, RETURN_STATUS);\r
752 } else {\r
753 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);\r
754 }\r
e389c39b 755 ArgumentString = ValueBuffer;\r
756 if (RETURN_ERROR (Status)) {\r
757 //\r
758 // Clear error bit\r
759 //\r
760 Index = Status & ~MAX_BIT;\r
761 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {\r
762 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];\r
763 }\r
764 } else {\r
765 Index = Status;\r
766 if (Index <= WARNING_STATUS_NUMBER) {\r
767 ArgumentString = mStatusString [Index];\r
768 }\r
769 }\r
770 if (ArgumentString == ValueBuffer) {\r
771 BasePrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);\r
772 }\r
773 break;\r
774\r
c553db4b 775 case '\r':\r
776 Format += BytesPerFormatCharacter;\r
777 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
778 if (FormatCharacter == '\n') {\r
779 //\r
780 // Translate '\r\n' to '\r\n'\r
781 //\r
782 ArgumentString = "\r\n";\r
783 } else {\r
784 //\r
785 // Translate '\r' to '\r'\r
786 //\r
787 ArgumentString = "\r";\r
788 Format -= BytesPerFormatCharacter;\r
789 }\r
790 break;\r
791\r
e389c39b 792 case '\n':\r
c553db4b 793 //\r
794 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
795 //\r
796 ArgumentString = "\r\n";\r
797 Format += BytesPerFormatCharacter;\r
798 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
799 if (FormatCharacter != '\r') {\r
800 Format -= BytesPerFormatCharacter;\r
801 }\r
e389c39b 802 break;\r
803\r
804 case '%':\r
805 default:\r
806 //\r
807 // if the type is '%' or unknown, then print it to the screen\r
808 //\r
809 ArgumentString = (CHAR8 *)&FormatCharacter;\r
810 Flags |= ARGUMENT_UNICODE;\r
811 break;\r
812 }\r
813 break;\r
814 \r
c553db4b 815 case '\r':\r
816 Format += BytesPerFormatCharacter;\r
817 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
818 if (FormatCharacter == '\n') {\r
819 //\r
820 // Translate '\r\n' to '\r\n'\r
821 //\r
822 ArgumentString = "\r\n";\r
823 } else {\r
824 //\r
825 // Translate '\r' to '\r'\r
826 //\r
827 ArgumentString = "\r";\r
828 Format -= BytesPerFormatCharacter;\r
829 }\r
830 break;\r
831\r
e389c39b 832 case '\n':\r
c553db4b 833 //\r
834 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
835 //\r
836 ArgumentString = "\r\n";\r
837 Format += BytesPerFormatCharacter;\r
838 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
839 if (FormatCharacter != '\r') {\r
840 Format -= BytesPerFormatCharacter;\r
841 }\r
e389c39b 842 break;\r
843\r
844 default:\r
845 ArgumentString = (CHAR8 *)&FormatCharacter;\r
846 Flags |= ARGUMENT_UNICODE;\r
847 break;\r
848 }\r
849\r
850 //\r
851 // Retrieve the ArgumentString attriubutes\r
852 //\r
853 if ((Flags & ARGUMENT_UNICODE) != 0) {\r
854 ArgumentMask = 0xffff;\r
855 BytesPerArgumentCharacter = 2;\r
856 } else {\r
857 ArgumentMask = 0xff;\r
858 BytesPerArgumentCharacter = 1;\r
859 }\r
860 if ((Flags & ARGUMENT_REVERSED) != 0) {\r
861 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;\r
862 } else {\r
863 //\r
864 // Compute the number of characters in ArgumentString and store it in Count\r
865 // ArgumentString is either null-terminated, or it contains Precision characters\r
866 //\r
867 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {\r
868 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;\r
869 if (ArgumentCharacter == 0) {\r
870 break;\r
871 }\r
872 }\r
873 }\r
874\r
875 if (Precision < Count) {\r
876 Precision = Count;\r
877 }\r
878\r
879 //\r
880 // Pad before the string\r
881 //\r
882 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {\r
6bae492f 883 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
6709bbd1 884 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 885 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
886 }\r
e389c39b 887 }\r
888\r
889 if (ZeroPad) {\r
890 if (Prefix != 0) {\r
6bae492f 891 LengthToReturn += (1 * BytesPerOutputCharacter);\r
6709bbd1 892 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 893 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
894 }\r
895 }\r
6bae492f 896 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
6709bbd1 897 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 898 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);\r
e389c39b 899 }\r
e389c39b 900 } else {\r
6bae492f 901 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
6709bbd1 902 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 903 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);\r
904 }\r
e389c39b 905 if (Prefix != 0) {\r
6bae492f 906 LengthToReturn += (1 * BytesPerOutputCharacter);\r
6709bbd1 907 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 908 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
909 }\r
e389c39b 910 }\r
911 }\r
912\r
913 //\r
914 // Output the Prefix character if it is present\r
915 //\r
916 Index = 0;\r
917 if (Prefix != 0) {\r
918 Index++;\r
919 }\r
920\r
921 //\r
922 // Copy the string into the output buffer performing the required type conversions\r
923 //\r
924 while (Index < Count) {\r
925 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;\r
926\r
6bae492f 927 LengthToReturn += (1 * BytesPerOutputCharacter);\r
6709bbd1 928 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 929 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);\r
930 }\r
e389c39b 931 ArgumentString += BytesPerArgumentCharacter;\r
932 Index++;\r
933 if (Comma) {\r
934 Digits++;\r
935 if (Digits == 3) {\r
936 Digits = 0;\r
937 Index++;\r
938 if (Index < Count) {\r
6bae492f 939 LengthToReturn += (1 * BytesPerOutputCharacter);\r
6709bbd1 940 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 941 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);\r
942 }\r
e389c39b 943 }\r
944 }\r
945 }\r
946 }\r
947\r
948 //\r
949 // Pad after the string\r
950 //\r
951 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {\r
6bae492f 952 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
6709bbd1 953 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 954 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
955 }\r
e389c39b 956 }\r
957\r
958 //\r
959 // Get the next character from the format string\r
960 //\r
961 Format += BytesPerFormatCharacter;\r
962\r
963 //\r
964 // Get the next character from the format string\r
965 //\r
966 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
967 }\r
968\r
4ae2e63c 969 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r
f405c067 970 return (LengthToReturn / BytesPerOutputCharacter);\r
971 }\r
972\r
6709bbd1 973 ASSERT (Buffer != NULL);\r
e389c39b 974 //\r
975 // Null terminate the Unicode or ASCII string\r
976 //\r
977 BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);\r
978 //\r
979 // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength\r
980 // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
981 //\r
982 ASSERT ((((Flags & OUTPUT_UNICODE) == 0)) || (StrSize ((CHAR16 *) OriginalBuffer) != 0));\r
983 //\r
984 // Make sure output buffer cannot contain more than PcdMaximumAsciiStringLength\r
985 // ASCII characters if PcdMaximumAsciiStringLength is not zero. \r
986 //\r
987 ASSERT ((((Flags & OUTPUT_UNICODE) != 0)) || (AsciiStrSize (OriginalBuffer) != 0));\r
988\r
989 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);\r
990}\r
991\r
992/**\r
993 Worker function that produces a Null-terminated string in an output buffer \r
994 based on a Null-terminated format string and variable argument list.\r
995\r
996 VSPrint function to process format and place the results in Buffer. Since a \r
7c905091 997 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
efb23117 998 this is the main print working routine\r
e389c39b 999\r
2fc59a00 1000 @param StartOfBuffer The character buffer to print the results of the parsing\r
e389c39b 1001 of Format into.\r
2fc59a00 1002 @param BufferSize The maximum number of characters to put into buffer.\r
e389c39b 1003 Zero means no limit.\r
7c905091 1004 @param Flags Initial flags value.\r
e389c39b 1005 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set\r
2fc59a00 1006 @param FormatString A Null-terminated format string.\r
e389c39b 1007 @param ... The variable argument list.\r
1008\r
2fc59a00 1009 @return The number of characters printed.\r
e389c39b 1010\r
1011**/\r
1012UINTN\r
932d66a9 1013EFIAPI\r
e389c39b 1014BasePrintLibSPrint (\r
1015 OUT CHAR8 *StartOfBuffer,\r
1016 IN UINTN BufferSize,\r
1017 IN UINTN Flags,\r
1018 IN CONST CHAR8 *FormatString,\r
1019 ...\r
1020 )\r
1021{\r
1022 VA_LIST Marker;\r
3bbe68a3 1023 UINTN NumberOfPrinted;\r
e389c39b 1024\r
1025 VA_START (Marker, FormatString);\r
3bbe68a3 1026 NumberOfPrinted = BasePrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);\r
1027 VA_END (Marker);\r
1028 return NumberOfPrinted;\r
e389c39b 1029}\r