]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BasePrintLib/PrintLibInternal.c
ShellPkg: refine the logic for cp command
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLibInternal.c
CommitLineData
e1f414b6 1/** @file\r
eceb3a4c 2 Print Library internal worker functions.\r
e1f414b6 3\r
3bbe68a3 4 Copyright (c) 2006 - 2012, 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
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
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
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
216 Flags &= (~PREFIX_ZERO);\r
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
376\r
e389c39b 377 //\r
378 // Reserve space for the Null terminator.\r
379 //\r
380 BufferSize--;\r
381 OriginalBuffer = Buffer;\r
2075236e 382\r
e389c39b 383 //\r
384 // Set the tag for the end of the input Buffer.\r
385 //\r
386 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;\r
387\r
388 if ((Flags & FORMAT_UNICODE) != 0) {\r
389 //\r
390 // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength\r
391 // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
392 //\r
393 ASSERT (StrSize ((CHAR16 *) Format) != 0);\r
394 BytesPerFormatCharacter = 2;\r
395 FormatMask = 0xffff;\r
396 } else {\r
397 //\r
398 // Make sure format string cannot contain more than PcdMaximumAsciiStringLength\r
399 // Ascii characters if PcdMaximumAsciiStringLength is not zero. \r
400 //\r
401 ASSERT (AsciiStrSize (Format) != 0);\r
402 BytesPerFormatCharacter = 1;\r
403 FormatMask = 0xff;\r
404 }\r
405\r
e389c39b 406 //\r
407 // Get the first character from the format string\r
408 //\r
409 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
410\r
411 //\r
412 // Loop until the end of the format string is reached or the output buffer is full\r
413 //\r
414 while (FormatCharacter != 0 && Buffer < EndBuffer) {\r
415 //\r
416 // Clear all the flag bits except those that may have been passed in\r
417 //\r
f405c067 418 Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);\r
e389c39b 419\r
420 //\r
421 // Set the default width to zero, and the default precision to 1\r
422 //\r
423 Width = 0;\r
424 Precision = 1;\r
425 Prefix = 0;\r
426 Comma = FALSE;\r
427 ZeroPad = FALSE;\r
428 Count = 0;\r
429 Digits = 0;\r
430\r
431 switch (FormatCharacter) {\r
432 case '%':\r
433 //\r
434 // Parse Flags and Width\r
435 //\r
436 for (Done = FALSE; !Done; ) {\r
437 Format += BytesPerFormatCharacter;\r
438 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
439 switch (FormatCharacter) {\r
440 case '.': \r
441 Flags |= PRECISION; \r
442 break;\r
443 case '-': \r
444 Flags |= LEFT_JUSTIFY; \r
445 break;\r
446 case '+': \r
447 Flags |= PREFIX_SIGN; \r
448 break;\r
449 case ' ': \r
450 Flags |= PREFIX_BLANK; \r
451 break;\r
452 case ',': \r
453 Flags |= COMMA_TYPE; \r
454 break;\r
455 case 'L':\r
456 case 'l': \r
457 Flags |= LONG_TYPE; \r
458 break;\r
459 case '*':\r
460 if ((Flags & PRECISION) == 0) {\r
461 Flags |= PAD_TO_WIDTH;\r
2075236e 462 if (BaseListMarker == NULL) {\r
463 Width = VA_ARG (VaListMarker, UINTN);\r
464 } else {\r
465 Width = BASE_ARG (BaseListMarker, UINTN);\r
466 }\r
e389c39b 467 } else {\r
2075236e 468 if (BaseListMarker == NULL) {\r
469 Precision = VA_ARG (VaListMarker, UINTN);\r
470 } else {\r
471 Precision = BASE_ARG (BaseListMarker, UINTN);\r
472 }\r
e389c39b 473 }\r
474 break;\r
475 case '0':\r
476 if ((Flags & PRECISION) == 0) {\r
477 Flags |= PREFIX_ZERO;\r
478 }\r
479 case '1':\r
480 case '2':\r
481 case '3':\r
482 case '4':\r
483 case '5':\r
484 case '6':\r
485 case '7':\r
486 case '8':\r
487 case '9':\r
488 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){\r
489 Count = (Count * 10) + FormatCharacter - '0';\r
490 Format += BytesPerFormatCharacter;\r
491 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
492 }\r
493 Format -= BytesPerFormatCharacter;\r
494 if ((Flags & PRECISION) == 0) {\r
495 Flags |= PAD_TO_WIDTH;\r
496 Width = Count;\r
497 } else {\r
498 Precision = Count;\r
499 }\r
500 break;\r
501 \r
502 case '\0':\r
503 //\r
504 // Make no output if Format string terminates unexpectedly when\r
505 // looking up for flag, width, precision and type. \r
506 //\r
507 Format -= BytesPerFormatCharacter;\r
508 Precision = 0;\r
509 //\r
510 // break skipped on purpose.\r
511 //\r
512 default:\r
513 Done = TRUE;\r
514 break;\r
515 }\r
516 } \r
517\r
518 //\r
519 // Handle each argument type\r
520 //\r
521 switch (FormatCharacter) {\r
522 case 'p':\r
523 //\r
524 // Flag space, +, 0, L & l are invalid for type p.\r
525 //\r
526 Flags &= ~(PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE);\r
527 if (sizeof (VOID *) > 4) {\r
528 Flags |= LONG_TYPE;\r
529 }\r
d6bee311
ED
530 //\r
531 // break skipped on purpose\r
532 //\r
e389c39b 533 case 'X':\r
534 Flags |= PREFIX_ZERO;\r
535 //\r
536 // break skipped on purpose\r
537 //\r
538 case 'x':\r
539 Flags |= RADIX_HEX;\r
540 //\r
541 // break skipped on purpose\r
542 //\r
543 case 'd':\r
544 if ((Flags & LONG_TYPE) == 0) {\r
57dd2382 545 //\r
18e81d77 546 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
547 // This assumption is made so the format string definition is compatible with the ANSI C\r
57dd2382 548 // Specification for formatted strings. It is recommended that the Base Types be used \r
549 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
550 // provides an implementation that is compatible with that largest possible set of CPU \r
551 // architectures. This is why the type "int" is used in this one case.\r
552 //\r
2075236e 553 if (BaseListMarker == NULL) {\r
554 Value = VA_ARG (VaListMarker, int);\r
555 } else {\r
556 Value = BASE_ARG (BaseListMarker, int);\r
557 }\r
e389c39b 558 } else {\r
2075236e 559 if (BaseListMarker == NULL) {\r
560 Value = VA_ARG (VaListMarker, INT64);\r
561 } else {\r
562 Value = BASE_ARG (BaseListMarker, INT64);\r
563 }\r
e389c39b 564 }\r
565 if ((Flags & PREFIX_BLANK) != 0) {\r
566 Prefix = ' ';\r
567 }\r
568 if ((Flags & PREFIX_SIGN) != 0) {\r
569 Prefix = '+';\r
570 }\r
571 if ((Flags & COMMA_TYPE) != 0) {\r
572 Comma = TRUE;\r
573 }\r
574 if ((Flags & RADIX_HEX) == 0) {\r
575 Radix = 10;\r
576 if (Comma) {\r
577 Flags &= (~PREFIX_ZERO);\r
578 Precision = 1;\r
579 }\r
580 if (Value < 0) {\r
581 Flags |= PREFIX_SIGN;\r
582 Prefix = '-';\r
583 Value = -Value;\r
584 }\r
585 } else {\r
586 Radix = 16;\r
587 Comma = FALSE;\r
588 if ((Flags & LONG_TYPE) == 0 && Value < 0) {\r
57dd2382 589 //\r
18e81d77 590 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
591 // This assumption is made so the format string definition is compatible with the ANSI C\r
57dd2382 592 // Specification for formatted strings. It is recommended that the Base Types be used \r
593 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
594 // provides an implementation that is compatible with that largest possible set of CPU \r
595 // architectures. This is why the type "unsigned int" is used in this one case.\r
596 //\r
e389c39b 597 Value = (unsigned int)Value;\r
598 }\r
599 }\r
600 //\r
601 // Convert Value to a reversed string\r
602 //\r
7c905091 603 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;\r
e389c39b 604 if (Value == 0 && Precision == 0) {\r
605 Count = 0;\r
606 }\r
607 ArgumentString = (CHAR8 *)ValueBuffer + Count;\r
608 \r
609 Digits = Count % 3;\r
610 if (Digits != 0) {\r
611 Digits = 3 - Digits;\r
612 }\r
613 if (Comma && Count != 0) {\r
614 Count += ((Count - 1) / 3);\r
615 }\r
616 if (Prefix != 0) {\r
617 Count++;\r
618 Precision++;\r
619 }\r
620 Flags |= ARGUMENT_REVERSED;\r
621 ZeroPad = TRUE;\r
622 if ((Flags & PREFIX_ZERO) != 0) {\r
623 if ((Flags & LEFT_JUSTIFY) == 0) {\r
624 if ((Flags & PAD_TO_WIDTH) != 0) {\r
625 if ((Flags & PRECISION) == 0) {\r
626 Precision = Width;\r
627 }\r
628 }\r
629 }\r
630 }\r
631 break;\r
632\r
633 case 's':\r
634 case 'S':\r
635 Flags |= ARGUMENT_UNICODE;\r
636 //\r
637 // break skipped on purpose\r
638 //\r
639 case 'a':\r
2075236e 640 if (BaseListMarker == NULL) {\r
641 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);\r
642 } else {\r
643 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);\r
644 }\r
e389c39b 645 if (ArgumentString == NULL) {\r
646 Flags &= (~ARGUMENT_UNICODE);\r
647 ArgumentString = "<null string>";\r
648 }\r
649 //\r
650 // Set the default precision for string to be zero if not specified.\r
651 //\r
652 if ((Flags & PRECISION) == 0) {\r
653 Precision = 0;\r
654 }\r
655 break;\r
656\r
657 case 'c':\r
2075236e 658 if (BaseListMarker == NULL) {\r
659 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;\r
660 } else {\r
661 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;\r
662 }\r
e389c39b 663 ArgumentString = (CHAR8 *)&Character;\r
664 Flags |= ARGUMENT_UNICODE;\r
665 break;\r
666\r
667 case 'g':\r
2075236e 668 if (BaseListMarker == NULL) {\r
669 TmpGuid = VA_ARG (VaListMarker, GUID *);\r
670 } else {\r
671 TmpGuid = BASE_ARG (BaseListMarker, GUID *);\r
672 }\r
e389c39b 673 if (TmpGuid == NULL) {\r
674 ArgumentString = "<null guid>";\r
675 } else {\r
4b0f378c
LG
676 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));\r
677 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));\r
678 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));\r
e389c39b 679 BasePrintLibSPrint (\r
680 ValueBuffer,\r
681 MAXIMUM_VALUE_CHARACTERS, \r
682 0,\r
683 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
4b0f378c
LG
684 GuidData1,\r
685 GuidData2,\r
686 GuidData3,\r
e389c39b 687 TmpGuid->Data4[0],\r
688 TmpGuid->Data4[1],\r
689 TmpGuid->Data4[2],\r
690 TmpGuid->Data4[3],\r
691 TmpGuid->Data4[4],\r
692 TmpGuid->Data4[5],\r
693 TmpGuid->Data4[6],\r
694 TmpGuid->Data4[7]\r
695 );\r
696 ArgumentString = ValueBuffer;\r
697 }\r
698 break;\r
699\r
700 case 't':\r
2075236e 701 if (BaseListMarker == NULL) {\r
702 TmpTime = VA_ARG (VaListMarker, TIME *); \r
703 } else {\r
704 TmpTime = BASE_ARG (BaseListMarker, TIME *); \r
705 }\r
e389c39b 706 if (TmpTime == NULL) {\r
707 ArgumentString = "<null time>";\r
708 } else {\r
709 BasePrintLibSPrint (\r
710 ValueBuffer,\r
711 MAXIMUM_VALUE_CHARACTERS,\r
712 0,\r
713 "%02d/%02d/%04d %02d:%02d",\r
714 TmpTime->Month,\r
715 TmpTime->Day,\r
716 TmpTime->Year,\r
717 TmpTime->Hour,\r
718 TmpTime->Minute\r
719 );\r
720 ArgumentString = ValueBuffer;\r
721 }\r
722 break;\r
723\r
724 case 'r':\r
2075236e 725 if (BaseListMarker == NULL) {\r
726 Status = VA_ARG (VaListMarker, RETURN_STATUS);\r
727 } else {\r
728 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);\r
729 }\r
e389c39b 730 ArgumentString = ValueBuffer;\r
731 if (RETURN_ERROR (Status)) {\r
732 //\r
733 // Clear error bit\r
734 //\r
735 Index = Status & ~MAX_BIT;\r
736 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {\r
737 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];\r
738 }\r
739 } else {\r
740 Index = Status;\r
741 if (Index <= WARNING_STATUS_NUMBER) {\r
742 ArgumentString = mStatusString [Index];\r
743 }\r
744 }\r
745 if (ArgumentString == ValueBuffer) {\r
746 BasePrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);\r
747 }\r
748 break;\r
749\r
c553db4b 750 case '\r':\r
751 Format += BytesPerFormatCharacter;\r
752 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
753 if (FormatCharacter == '\n') {\r
754 //\r
755 // Translate '\r\n' to '\r\n'\r
756 //\r
757 ArgumentString = "\r\n";\r
758 } else {\r
759 //\r
760 // Translate '\r' to '\r'\r
761 //\r
762 ArgumentString = "\r";\r
763 Format -= BytesPerFormatCharacter;\r
764 }\r
765 break;\r
766\r
e389c39b 767 case '\n':\r
c553db4b 768 //\r
769 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
770 //\r
771 ArgumentString = "\r\n";\r
772 Format += BytesPerFormatCharacter;\r
773 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
774 if (FormatCharacter != '\r') {\r
775 Format -= BytesPerFormatCharacter;\r
776 }\r
e389c39b 777 break;\r
778\r
779 case '%':\r
780 default:\r
781 //\r
782 // if the type is '%' or unknown, then print it to the screen\r
783 //\r
784 ArgumentString = (CHAR8 *)&FormatCharacter;\r
785 Flags |= ARGUMENT_UNICODE;\r
786 break;\r
787 }\r
788 break;\r
789 \r
c553db4b 790 case '\r':\r
791 Format += BytesPerFormatCharacter;\r
792 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
793 if (FormatCharacter == '\n') {\r
794 //\r
795 // Translate '\r\n' to '\r\n'\r
796 //\r
797 ArgumentString = "\r\n";\r
798 } else {\r
799 //\r
800 // Translate '\r' to '\r'\r
801 //\r
802 ArgumentString = "\r";\r
803 Format -= BytesPerFormatCharacter;\r
804 }\r
805 break;\r
806\r
e389c39b 807 case '\n':\r
c553db4b 808 //\r
809 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
810 //\r
811 ArgumentString = "\r\n";\r
812 Format += BytesPerFormatCharacter;\r
813 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
814 if (FormatCharacter != '\r') {\r
815 Format -= BytesPerFormatCharacter;\r
816 }\r
e389c39b 817 break;\r
818\r
819 default:\r
820 ArgumentString = (CHAR8 *)&FormatCharacter;\r
821 Flags |= ARGUMENT_UNICODE;\r
822 break;\r
823 }\r
824\r
825 //\r
826 // Retrieve the ArgumentString attriubutes\r
827 //\r
828 if ((Flags & ARGUMENT_UNICODE) != 0) {\r
829 ArgumentMask = 0xffff;\r
830 BytesPerArgumentCharacter = 2;\r
831 } else {\r
832 ArgumentMask = 0xff;\r
833 BytesPerArgumentCharacter = 1;\r
834 }\r
835 if ((Flags & ARGUMENT_REVERSED) != 0) {\r
836 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;\r
837 } else {\r
838 //\r
839 // Compute the number of characters in ArgumentString and store it in Count\r
840 // ArgumentString is either null-terminated, or it contains Precision characters\r
841 //\r
842 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {\r
843 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;\r
844 if (ArgumentCharacter == 0) {\r
845 break;\r
846 }\r
847 }\r
848 }\r
849\r
850 if (Precision < Count) {\r
851 Precision = Count;\r
852 }\r
853\r
854 //\r
855 // Pad before the string\r
856 //\r
857 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {\r
6bae492f 858 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
6709bbd1 859 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 860 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
861 }\r
e389c39b 862 }\r
863\r
864 if (ZeroPad) {\r
865 if (Prefix != 0) {\r
6bae492f 866 LengthToReturn += (1 * BytesPerOutputCharacter);\r
6709bbd1 867 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 868 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
869 }\r
870 }\r
6bae492f 871 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
6709bbd1 872 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 873 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);\r
e389c39b 874 }\r
e389c39b 875 } else {\r
6bae492f 876 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
6709bbd1 877 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 878 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);\r
879 }\r
e389c39b 880 if (Prefix != 0) {\r
6bae492f 881 LengthToReturn += (1 * BytesPerOutputCharacter);\r
6709bbd1 882 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 883 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
884 }\r
e389c39b 885 }\r
886 }\r
887\r
888 //\r
889 // Output the Prefix character if it is present\r
890 //\r
891 Index = 0;\r
892 if (Prefix != 0) {\r
893 Index++;\r
894 }\r
895\r
896 //\r
897 // Copy the string into the output buffer performing the required type conversions\r
898 //\r
899 while (Index < Count) {\r
900 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;\r
901\r
6bae492f 902 LengthToReturn += (1 * BytesPerOutputCharacter);\r
6709bbd1 903 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 904 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);\r
905 }\r
e389c39b 906 ArgumentString += BytesPerArgumentCharacter;\r
907 Index++;\r
908 if (Comma) {\r
909 Digits++;\r
910 if (Digits == 3) {\r
911 Digits = 0;\r
912 Index++;\r
913 if (Index < Count) {\r
6bae492f 914 LengthToReturn += (1 * BytesPerOutputCharacter);\r
6709bbd1 915 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 916 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);\r
917 }\r
e389c39b 918 }\r
919 }\r
920 }\r
921 }\r
922\r
923 //\r
924 // Pad after the string\r
925 //\r
926 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {\r
6bae492f 927 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
6709bbd1 928 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 929 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
930 }\r
e389c39b 931 }\r
932\r
933 //\r
934 // Get the next character from the format string\r
935 //\r
936 Format += BytesPerFormatCharacter;\r
937\r
938 //\r
939 // Get the next character from the format string\r
940 //\r
941 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
942 }\r
943\r
4ae2e63c 944 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r
f405c067 945 return (LengthToReturn / BytesPerOutputCharacter);\r
946 }\r
947\r
6709bbd1 948 ASSERT (Buffer != NULL);\r
e389c39b 949 //\r
950 // Null terminate the Unicode or ASCII string\r
951 //\r
952 BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);\r
953 //\r
954 // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength\r
955 // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
956 //\r
957 ASSERT ((((Flags & OUTPUT_UNICODE) == 0)) || (StrSize ((CHAR16 *) OriginalBuffer) != 0));\r
958 //\r
959 // Make sure output buffer cannot contain more than PcdMaximumAsciiStringLength\r
960 // ASCII characters if PcdMaximumAsciiStringLength is not zero. \r
961 //\r
962 ASSERT ((((Flags & OUTPUT_UNICODE) != 0)) || (AsciiStrSize (OriginalBuffer) != 0));\r
963\r
964 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);\r
965}\r
966\r
967/**\r
968 Worker function that produces a Null-terminated string in an output buffer \r
969 based on a Null-terminated format string and variable argument list.\r
970\r
971 VSPrint function to process format and place the results in Buffer. Since a \r
7c905091 972 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
efb23117 973 this is the main print working routine\r
e389c39b 974\r
2fc59a00 975 @param StartOfBuffer The character buffer to print the results of the parsing\r
e389c39b 976 of Format into.\r
2fc59a00 977 @param BufferSize The maximum number of characters to put into buffer.\r
e389c39b 978 Zero means no limit.\r
7c905091 979 @param Flags Initial flags value.\r
e389c39b 980 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set\r
2fc59a00 981 @param FormatString A Null-terminated format string.\r
e389c39b 982 @param ... The variable argument list.\r
983\r
2fc59a00 984 @return The number of characters printed.\r
e389c39b 985\r
986**/\r
987UINTN\r
932d66a9 988EFIAPI\r
e389c39b 989BasePrintLibSPrint (\r
990 OUT CHAR8 *StartOfBuffer,\r
991 IN UINTN BufferSize,\r
992 IN UINTN Flags,\r
993 IN CONST CHAR8 *FormatString,\r
994 ...\r
995 )\r
996{\r
997 VA_LIST Marker;\r
3bbe68a3 998 UINTN NumberOfPrinted;\r
e389c39b 999\r
1000 VA_START (Marker, FormatString);\r
3bbe68a3 1001 NumberOfPrinted = BasePrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);\r
1002 VA_END (Marker);\r
1003 return NumberOfPrinted;\r
e389c39b 1004}\r