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