]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BasePrintLib/PrintLibInternal.c
Add plain-text ReadMe files and delete the PDF version.
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLibInternal.c
CommitLineData
e1f414b6 1/** @file\r
eceb3a4c 2 Print Library internal worker functions.\r
e1f414b6 3\r
244bff4e 4 Copyright (c) 2006 - 2011, 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
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
2fc59a00 60 @param Buffer The buffer to place the Unicode or ASCII string.\r
e1f414b6 61 @param EndBuffer The end of the input Buffer. No characters will be\r
62 placed after that. \r
2fc59a00 63 @param Length The count of character to be placed into Buffer.\r
a2bc29c4 64 (Negative value indicates no buffer fill.)\r
2fc59a00 65 @param Character The character to be placed into Buffer.\r
66 @param Increment The character increment in Buffer.\r
e1f414b6 67\r
2fc59a00 68 @return Buffer.\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
2fc59a00 99 @param Value The 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
2fc59a00 155 @param Buffer The pointer to the output buffer for the produced Null-terminated\r
e1f414b6 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
2fc59a00 162 @param Increment The character increment in Buffer.\r
e1f414b6 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
df8d0595 196 // If both COMMA_TYPE and RADIX_HEX are set, then ASSERT ()\r
e1f414b6 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
2fc59a00 278 @param Buffer The character buffer to print the results of the parsing\r
2075236e 279 of Format into.\r
2fc59a00 280 @param BufferSize The maximum number of characters to put into buffer.\r
2075236e 281 @param Flags Initial flags value.\r
282 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set.\r
2fc59a00 283 @param Format A Null-terminated format string.\r
2075236e 284 @param VaListMarker VA_LIST style variable argument list consumed by processing Format.\r
285 @param BaseListMarker BASE_LIST style variable argument list consumed by processing Format.\r
e389c39b 286\r
2fc59a00 287 @return The number of characters printed not including the Null-terminator.\r
e389c39b 288\r
289**/\r
290UINTN\r
2075236e 291BasePrintLibSPrintMarker (\r
e389c39b 292 OUT CHAR8 *Buffer,\r
293 IN UINTN BufferSize,\r
294 IN UINTN Flags,\r
295 IN CONST CHAR8 *Format,\r
2075236e 296 IN VA_LIST VaListMarker, OPTIONAL\r
297 IN BASE_LIST BaseListMarker OPTIONAL\r
e389c39b 298 )\r
299{\r
2075236e 300 CHAR8 *OriginalBuffer;\r
301 CHAR8 *EndBuffer;\r
302 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
303 UINTN BytesPerOutputCharacter;\r
304 UINTN BytesPerFormatCharacter;\r
305 UINTN FormatMask;\r
306 UINTN FormatCharacter;\r
307 UINTN Width;\r
308 UINTN Precision;\r
309 INT64 Value;\r
310 CONST CHAR8 *ArgumentString;\r
311 UINTN Character;\r
312 GUID *TmpGuid;\r
313 TIME *TmpTime;\r
314 UINTN Count;\r
315 UINTN ArgumentMask;\r
316 INTN BytesPerArgumentCharacter;\r
317 UINTN ArgumentCharacter;\r
318 BOOLEAN Done;\r
319 UINTN Index;\r
320 CHAR8 Prefix;\r
321 BOOLEAN ZeroPad;\r
322 BOOLEAN Comma;\r
323 UINTN Digits;\r
324 UINTN Radix;\r
325 RETURN_STATUS Status;\r
4b0f378c
LG
326 UINT32 GuidData1;\r
327 UINT16 GuidData2;\r
328 UINT16 GuidData3;\r
e389c39b 329\r
330 if (BufferSize == 0) {\r
331 return 0;\r
332 }\r
333 ASSERT (Buffer != NULL);\r
334\r
335 if ((Flags & OUTPUT_UNICODE) != 0) {\r
336 BytesPerOutputCharacter = 2;\r
337 } else {\r
338 BytesPerOutputCharacter = 1;\r
339 }\r
340\r
341 //\r
342 // Reserve space for the Null terminator.\r
343 //\r
344 BufferSize--;\r
345 OriginalBuffer = Buffer;\r
2075236e 346\r
e389c39b 347 //\r
348 // Set the tag for the end of the input Buffer.\r
349 //\r
350 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;\r
351\r
352 if ((Flags & FORMAT_UNICODE) != 0) {\r
353 //\r
354 // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength\r
355 // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
356 //\r
357 ASSERT (StrSize ((CHAR16 *) Format) != 0);\r
358 BytesPerFormatCharacter = 2;\r
359 FormatMask = 0xffff;\r
360 } else {\r
361 //\r
362 // Make sure format string cannot contain more than PcdMaximumAsciiStringLength\r
363 // Ascii characters if PcdMaximumAsciiStringLength is not zero. \r
364 //\r
365 ASSERT (AsciiStrSize (Format) != 0);\r
366 BytesPerFormatCharacter = 1;\r
367 FormatMask = 0xff;\r
368 }\r
369\r
e389c39b 370 //\r
371 // Get the first character from the format string\r
372 //\r
373 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
374\r
375 //\r
376 // Loop until the end of the format string is reached or the output buffer is full\r
377 //\r
378 while (FormatCharacter != 0 && Buffer < EndBuffer) {\r
379 //\r
380 // Clear all the flag bits except those that may have been passed in\r
381 //\r
382 Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE);\r
383\r
384 //\r
385 // Set the default width to zero, and the default precision to 1\r
386 //\r
387 Width = 0;\r
388 Precision = 1;\r
389 Prefix = 0;\r
390 Comma = FALSE;\r
391 ZeroPad = FALSE;\r
392 Count = 0;\r
393 Digits = 0;\r
394\r
395 switch (FormatCharacter) {\r
396 case '%':\r
397 //\r
398 // Parse Flags and Width\r
399 //\r
400 for (Done = FALSE; !Done; ) {\r
401 Format += BytesPerFormatCharacter;\r
402 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
403 switch (FormatCharacter) {\r
404 case '.': \r
405 Flags |= PRECISION; \r
406 break;\r
407 case '-': \r
408 Flags |= LEFT_JUSTIFY; \r
409 break;\r
410 case '+': \r
411 Flags |= PREFIX_SIGN; \r
412 break;\r
413 case ' ': \r
414 Flags |= PREFIX_BLANK; \r
415 break;\r
416 case ',': \r
417 Flags |= COMMA_TYPE; \r
418 break;\r
419 case 'L':\r
420 case 'l': \r
421 Flags |= LONG_TYPE; \r
422 break;\r
423 case '*':\r
424 if ((Flags & PRECISION) == 0) {\r
425 Flags |= PAD_TO_WIDTH;\r
2075236e 426 if (BaseListMarker == NULL) {\r
427 Width = VA_ARG (VaListMarker, UINTN);\r
428 } else {\r
429 Width = BASE_ARG (BaseListMarker, UINTN);\r
430 }\r
e389c39b 431 } else {\r
2075236e 432 if (BaseListMarker == NULL) {\r
433 Precision = VA_ARG (VaListMarker, UINTN);\r
434 } else {\r
435 Precision = BASE_ARG (BaseListMarker, UINTN);\r
436 }\r
e389c39b 437 }\r
438 break;\r
439 case '0':\r
440 if ((Flags & PRECISION) == 0) {\r
441 Flags |= PREFIX_ZERO;\r
442 }\r
443 case '1':\r
444 case '2':\r
445 case '3':\r
446 case '4':\r
447 case '5':\r
448 case '6':\r
449 case '7':\r
450 case '8':\r
451 case '9':\r
452 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){\r
453 Count = (Count * 10) + FormatCharacter - '0';\r
454 Format += BytesPerFormatCharacter;\r
455 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
456 }\r
457 Format -= BytesPerFormatCharacter;\r
458 if ((Flags & PRECISION) == 0) {\r
459 Flags |= PAD_TO_WIDTH;\r
460 Width = Count;\r
461 } else {\r
462 Precision = Count;\r
463 }\r
464 break;\r
465 \r
466 case '\0':\r
467 //\r
468 // Make no output if Format string terminates unexpectedly when\r
469 // looking up for flag, width, precision and type. \r
470 //\r
471 Format -= BytesPerFormatCharacter;\r
472 Precision = 0;\r
473 //\r
474 // break skipped on purpose.\r
475 //\r
476 default:\r
477 Done = TRUE;\r
478 break;\r
479 }\r
480 } \r
481\r
482 //\r
483 // Handle each argument type\r
484 //\r
485 switch (FormatCharacter) {\r
486 case 'p':\r
487 //\r
488 // Flag space, +, 0, L & l are invalid for type p.\r
489 //\r
490 Flags &= ~(PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE);\r
491 if (sizeof (VOID *) > 4) {\r
492 Flags |= LONG_TYPE;\r
493 }\r
494 case 'X':\r
495 Flags |= PREFIX_ZERO;\r
496 //\r
497 // break skipped on purpose\r
498 //\r
499 case 'x':\r
500 Flags |= RADIX_HEX;\r
501 //\r
502 // break skipped on purpose\r
503 //\r
504 case 'd':\r
505 if ((Flags & LONG_TYPE) == 0) {\r
57dd2382 506 //\r
18e81d77 507 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
508 // This assumption is made so the format string definition is compatible with the ANSI C\r
57dd2382 509 // Specification for formatted strings. It is recommended that the Base Types be used \r
510 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
511 // provides an implementation that is compatible with that largest possible set of CPU \r
512 // architectures. This is why the type "int" is used in this one case.\r
513 //\r
2075236e 514 if (BaseListMarker == NULL) {\r
515 Value = VA_ARG (VaListMarker, int);\r
516 } else {\r
517 Value = BASE_ARG (BaseListMarker, int);\r
518 }\r
e389c39b 519 } else {\r
2075236e 520 if (BaseListMarker == NULL) {\r
521 Value = VA_ARG (VaListMarker, INT64);\r
522 } else {\r
523 Value = BASE_ARG (BaseListMarker, INT64);\r
524 }\r
e389c39b 525 }\r
526 if ((Flags & PREFIX_BLANK) != 0) {\r
527 Prefix = ' ';\r
528 }\r
529 if ((Flags & PREFIX_SIGN) != 0) {\r
530 Prefix = '+';\r
531 }\r
532 if ((Flags & COMMA_TYPE) != 0) {\r
533 Comma = TRUE;\r
534 }\r
535 if ((Flags & RADIX_HEX) == 0) {\r
536 Radix = 10;\r
537 if (Comma) {\r
538 Flags &= (~PREFIX_ZERO);\r
539 Precision = 1;\r
540 }\r
541 if (Value < 0) {\r
542 Flags |= PREFIX_SIGN;\r
543 Prefix = '-';\r
544 Value = -Value;\r
545 }\r
546 } else {\r
547 Radix = 16;\r
548 Comma = FALSE;\r
549 if ((Flags & LONG_TYPE) == 0 && Value < 0) {\r
57dd2382 550 //\r
18e81d77 551 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
552 // This assumption is made so the format string definition is compatible with the ANSI C\r
57dd2382 553 // Specification for formatted strings. It is recommended that the Base Types be used \r
554 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
555 // provides an implementation that is compatible with that largest possible set of CPU \r
556 // architectures. This is why the type "unsigned int" is used in this one case.\r
557 //\r
e389c39b 558 Value = (unsigned int)Value;\r
559 }\r
560 }\r
561 //\r
562 // Convert Value to a reversed string\r
563 //\r
7c905091 564 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;\r
e389c39b 565 if (Value == 0 && Precision == 0) {\r
566 Count = 0;\r
567 }\r
568 ArgumentString = (CHAR8 *)ValueBuffer + Count;\r
569 \r
570 Digits = Count % 3;\r
571 if (Digits != 0) {\r
572 Digits = 3 - Digits;\r
573 }\r
574 if (Comma && Count != 0) {\r
575 Count += ((Count - 1) / 3);\r
576 }\r
577 if (Prefix != 0) {\r
578 Count++;\r
579 Precision++;\r
580 }\r
581 Flags |= ARGUMENT_REVERSED;\r
582 ZeroPad = TRUE;\r
583 if ((Flags & PREFIX_ZERO) != 0) {\r
584 if ((Flags & LEFT_JUSTIFY) == 0) {\r
585 if ((Flags & PAD_TO_WIDTH) != 0) {\r
586 if ((Flags & PRECISION) == 0) {\r
587 Precision = Width;\r
588 }\r
589 }\r
590 }\r
591 }\r
592 break;\r
593\r
594 case 's':\r
595 case 'S':\r
596 Flags |= ARGUMENT_UNICODE;\r
597 //\r
598 // break skipped on purpose\r
599 //\r
600 case 'a':\r
2075236e 601 if (BaseListMarker == NULL) {\r
602 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);\r
603 } else {\r
604 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);\r
605 }\r
e389c39b 606 if (ArgumentString == NULL) {\r
607 Flags &= (~ARGUMENT_UNICODE);\r
608 ArgumentString = "<null string>";\r
609 }\r
610 //\r
611 // Set the default precision for string to be zero if not specified.\r
612 //\r
613 if ((Flags & PRECISION) == 0) {\r
614 Precision = 0;\r
615 }\r
616 break;\r
617\r
618 case 'c':\r
2075236e 619 if (BaseListMarker == NULL) {\r
620 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;\r
621 } else {\r
622 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;\r
623 }\r
e389c39b 624 ArgumentString = (CHAR8 *)&Character;\r
625 Flags |= ARGUMENT_UNICODE;\r
626 break;\r
627\r
628 case 'g':\r
2075236e 629 if (BaseListMarker == NULL) {\r
630 TmpGuid = VA_ARG (VaListMarker, GUID *);\r
631 } else {\r
632 TmpGuid = BASE_ARG (BaseListMarker, GUID *);\r
633 }\r
e389c39b 634 if (TmpGuid == NULL) {\r
635 ArgumentString = "<null guid>";\r
636 } else {\r
4b0f378c
LG
637 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));\r
638 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));\r
639 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));\r
e389c39b 640 BasePrintLibSPrint (\r
641 ValueBuffer,\r
642 MAXIMUM_VALUE_CHARACTERS, \r
643 0,\r
644 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
4b0f378c
LG
645 GuidData1,\r
646 GuidData2,\r
647 GuidData3,\r
e389c39b 648 TmpGuid->Data4[0],\r
649 TmpGuid->Data4[1],\r
650 TmpGuid->Data4[2],\r
651 TmpGuid->Data4[3],\r
652 TmpGuid->Data4[4],\r
653 TmpGuid->Data4[5],\r
654 TmpGuid->Data4[6],\r
655 TmpGuid->Data4[7]\r
656 );\r
657 ArgumentString = ValueBuffer;\r
658 }\r
659 break;\r
660\r
661 case 't':\r
2075236e 662 if (BaseListMarker == NULL) {\r
663 TmpTime = VA_ARG (VaListMarker, TIME *); \r
664 } else {\r
665 TmpTime = BASE_ARG (BaseListMarker, TIME *); \r
666 }\r
e389c39b 667 if (TmpTime == NULL) {\r
668 ArgumentString = "<null time>";\r
669 } else {\r
670 BasePrintLibSPrint (\r
671 ValueBuffer,\r
672 MAXIMUM_VALUE_CHARACTERS,\r
673 0,\r
674 "%02d/%02d/%04d %02d:%02d",\r
675 TmpTime->Month,\r
676 TmpTime->Day,\r
677 TmpTime->Year,\r
678 TmpTime->Hour,\r
679 TmpTime->Minute\r
680 );\r
681 ArgumentString = ValueBuffer;\r
682 }\r
683 break;\r
684\r
685 case 'r':\r
2075236e 686 if (BaseListMarker == NULL) {\r
687 Status = VA_ARG (VaListMarker, RETURN_STATUS);\r
688 } else {\r
689 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);\r
690 }\r
e389c39b 691 ArgumentString = ValueBuffer;\r
692 if (RETURN_ERROR (Status)) {\r
693 //\r
694 // Clear error bit\r
695 //\r
696 Index = Status & ~MAX_BIT;\r
697 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {\r
698 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];\r
699 }\r
700 } else {\r
701 Index = Status;\r
702 if (Index <= WARNING_STATUS_NUMBER) {\r
703 ArgumentString = mStatusString [Index];\r
704 }\r
705 }\r
706 if (ArgumentString == ValueBuffer) {\r
707 BasePrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);\r
708 }\r
709 break;\r
710\r
c553db4b 711 case '\r':\r
712 Format += BytesPerFormatCharacter;\r
713 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
714 if (FormatCharacter == '\n') {\r
715 //\r
716 // Translate '\r\n' to '\r\n'\r
717 //\r
718 ArgumentString = "\r\n";\r
719 } else {\r
720 //\r
721 // Translate '\r' to '\r'\r
722 //\r
723 ArgumentString = "\r";\r
724 Format -= BytesPerFormatCharacter;\r
725 }\r
726 break;\r
727\r
e389c39b 728 case '\n':\r
c553db4b 729 //\r
730 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
731 //\r
732 ArgumentString = "\r\n";\r
733 Format += BytesPerFormatCharacter;\r
734 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
735 if (FormatCharacter != '\r') {\r
736 Format -= BytesPerFormatCharacter;\r
737 }\r
e389c39b 738 break;\r
739\r
740 case '%':\r
741 default:\r
742 //\r
743 // if the type is '%' or unknown, then print it to the screen\r
744 //\r
745 ArgumentString = (CHAR8 *)&FormatCharacter;\r
746 Flags |= ARGUMENT_UNICODE;\r
747 break;\r
748 }\r
749 break;\r
750 \r
c553db4b 751 case '\r':\r
752 Format += BytesPerFormatCharacter;\r
753 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
754 if (FormatCharacter == '\n') {\r
755 //\r
756 // Translate '\r\n' to '\r\n'\r
757 //\r
758 ArgumentString = "\r\n";\r
759 } else {\r
760 //\r
761 // Translate '\r' to '\r'\r
762 //\r
763 ArgumentString = "\r";\r
764 Format -= BytesPerFormatCharacter;\r
765 }\r
766 break;\r
767\r
e389c39b 768 case '\n':\r
c553db4b 769 //\r
770 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
771 //\r
772 ArgumentString = "\r\n";\r
773 Format += BytesPerFormatCharacter;\r
774 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
775 if (FormatCharacter != '\r') {\r
776 Format -= BytesPerFormatCharacter;\r
777 }\r
e389c39b 778 break;\r
779\r
780 default:\r
781 ArgumentString = (CHAR8 *)&FormatCharacter;\r
782 Flags |= ARGUMENT_UNICODE;\r
783 break;\r
784 }\r
785\r
786 //\r
787 // Retrieve the ArgumentString attriubutes\r
788 //\r
789 if ((Flags & ARGUMENT_UNICODE) != 0) {\r
790 ArgumentMask = 0xffff;\r
791 BytesPerArgumentCharacter = 2;\r
792 } else {\r
793 ArgumentMask = 0xff;\r
794 BytesPerArgumentCharacter = 1;\r
795 }\r
796 if ((Flags & ARGUMENT_REVERSED) != 0) {\r
797 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;\r
798 } else {\r
799 //\r
800 // Compute the number of characters in ArgumentString and store it in Count\r
801 // ArgumentString is either null-terminated, or it contains Precision characters\r
802 //\r
803 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {\r
804 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;\r
805 if (ArgumentCharacter == 0) {\r
806 break;\r
807 }\r
808 }\r
809 }\r
810\r
811 if (Precision < Count) {\r
812 Precision = Count;\r
813 }\r
814\r
815 //\r
816 // Pad before the string\r
817 //\r
818 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {\r
819 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
820 }\r
821\r
822 if (ZeroPad) {\r
823 if (Prefix != 0) {\r
824 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
825 }\r
826 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);\r
827 } else {\r
828 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);\r
829 if (Prefix != 0) {\r
830 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
831 }\r
832 }\r
833\r
834 //\r
835 // Output the Prefix character if it is present\r
836 //\r
837 Index = 0;\r
838 if (Prefix != 0) {\r
839 Index++;\r
840 }\r
841\r
842 //\r
843 // Copy the string into the output buffer performing the required type conversions\r
844 //\r
845 while (Index < Count) {\r
846 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;\r
847\r
848 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);\r
849 ArgumentString += BytesPerArgumentCharacter;\r
850 Index++;\r
851 if (Comma) {\r
852 Digits++;\r
853 if (Digits == 3) {\r
854 Digits = 0;\r
855 Index++;\r
856 if (Index < Count) {\r
857 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);\r
858 }\r
859 }\r
860 }\r
861 }\r
862\r
863 //\r
864 // Pad after the string\r
865 //\r
866 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {\r
867 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
868 }\r
869\r
870 //\r
871 // Get the next character from the format string\r
872 //\r
873 Format += BytesPerFormatCharacter;\r
874\r
875 //\r
876 // Get the next character from the format string\r
877 //\r
878 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
879 }\r
880\r
881 //\r
882 // Null terminate the Unicode or ASCII string\r
883 //\r
884 BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);\r
885 //\r
886 // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength\r
887 // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
888 //\r
889 ASSERT ((((Flags & OUTPUT_UNICODE) == 0)) || (StrSize ((CHAR16 *) OriginalBuffer) != 0));\r
890 //\r
891 // Make sure output buffer cannot contain more than PcdMaximumAsciiStringLength\r
892 // ASCII characters if PcdMaximumAsciiStringLength is not zero. \r
893 //\r
894 ASSERT ((((Flags & OUTPUT_UNICODE) != 0)) || (AsciiStrSize (OriginalBuffer) != 0));\r
895\r
896 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);\r
897}\r
898\r
899/**\r
900 Worker function that produces a Null-terminated string in an output buffer \r
901 based on a Null-terminated format string and variable argument list.\r
902\r
903 VSPrint function to process format and place the results in Buffer. Since a \r
7c905091 904 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
efb23117 905 this is the main print working routine\r
e389c39b 906\r
2fc59a00 907 @param StartOfBuffer The character buffer to print the results of the parsing\r
e389c39b 908 of Format into.\r
2fc59a00 909 @param BufferSize The maximum number of characters to put into buffer.\r
e389c39b 910 Zero means no limit.\r
7c905091 911 @param Flags Initial flags value.\r
e389c39b 912 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set\r
2fc59a00 913 @param FormatString A Null-terminated format string.\r
e389c39b 914 @param ... The variable argument list.\r
915\r
2fc59a00 916 @return The number of characters printed.\r
e389c39b 917\r
918**/\r
919UINTN\r
932d66a9 920EFIAPI\r
e389c39b 921BasePrintLibSPrint (\r
922 OUT CHAR8 *StartOfBuffer,\r
923 IN UINTN BufferSize,\r
924 IN UINTN Flags,\r
925 IN CONST CHAR8 *FormatString,\r
926 ...\r
927 )\r
928{\r
929 VA_LIST Marker;\r
930\r
931 VA_START (Marker, FormatString);\r
2075236e 932 return BasePrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);\r
e389c39b 933}\r