]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/BasePrintLib/PrintLibInternal.c
Add type cast for better coding style.
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLibInternal.c
... / ...
CommitLineData
1/** @file\r
2 Print Library internal worker functions.\r
3\r
4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "PrintLibInternal.h"\r
16\r
17#define WARNING_STATUS_NUMBER 5\r
18#define ERROR_STATUS_NUMBER 33\r
19\r
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
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 "Warning Stale Data", // RETURN_WARN_STALE_DATA = 5\r
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
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
62};\r
63\r
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
70 @param Buffer The buffer to place the Unicode or ASCII string.\r
71 @param EndBuffer The end of the input Buffer. No characters will be\r
72 placed after that. \r
73 @param Length The count of character to be placed into Buffer.\r
74 (Negative value indicates no buffer fill.)\r
75 @param Character The character to be placed into Buffer.\r
76 @param Increment The character increment in Buffer.\r
77\r
78 @return Buffer.\r
79\r
80**/\r
81CHAR8 *\r
82BasePrintLibFillBuffer (\r
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
88 )\r
89{\r
90 INTN Index;\r
91 \r
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
101}\r
102\r
103/**\r
104 Internal function that convert a number to a string in Buffer.\r
105\r
106 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.\r
107\r
108 @param Buffer Location to place the ASCII string of Value.\r
109 @param Value The value to convert to a Decimal or Hexadecimal string in Buffer.\r
110 @param Radix Radix of the value\r
111\r
112 @return A pointer to the end of buffer filled with ASCII string.\r
113\r
114**/\r
115CHAR8 *\r
116BasePrintLibValueToString (\r
117 IN OUT CHAR8 *Buffer, \r
118 IN INT64 Value, \r
119 IN UINTN Radix\r
120 )\r
121{\r
122 UINT32 Remainder;\r
123\r
124 //\r
125 // Loop to convert one digit at a time in reverse order\r
126 //\r
127 *Buffer = 0;\r
128 do {\r
129 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r
130 *(++Buffer) = mHexStr[Remainder];\r
131 } while (Value != 0);\r
132\r
133 //\r
134 // Return pointer of the end of filled buffer.\r
135 //\r
136 return Buffer;\r
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
145 The total number of characters placed in Buffer is returned.\r
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
155 If Value is < 0, then the fist character in Buffer is a '-'.\r
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
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
165 @param Buffer The pointer to the output buffer for the produced Null-terminated\r
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
172 @param Increment The character increment in Buffer.\r
173 \r
174 @return Total number of characters required to perform the conversion.\r
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
189 CHAR8 *ValueBufferPtr;\r
190 UINTN Count;\r
191 UINTN Digits;\r
192 UINTN Index;\r
193 UINTN Radix;\r
194\r
195 //\r
196 // Make sure Buffer is not NULL and Width < MAXIMUM\r
197 //\r
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
206 // If both COMMA_TYPE and RADIX_HEX are set, then ASSERT ()\r
207 //\r
208 ASSERT (((Flags & COMMA_TYPE) == 0) || ((Flags & RADIX_HEX) == 0));\r
209\r
210 OriginalBuffer = Buffer;\r
211 \r
212 //\r
213 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.\r
214 //\r
215 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r
216 Flags &= ~((UINTN) PREFIX_ZERO);\r
217 }\r
218 //\r
219 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
220 //\r
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
228 \r
229 //\r
230 // Convert decimal negative\r
231 //\r
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
237 \r
238 //\r
239 // Count the length of the value string.\r
240 //\r
241 Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;\r
242 ValueBufferPtr = BasePrintLibValueToString (ValueBuffer, Value, Radix);\r
243 Count = ValueBufferPtr - ValueBuffer;\r
244 \r
245 //\r
246 // Append Zero\r
247 //\r
248 if ((Flags & PREFIX_ZERO) != 0) {\r
249 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);\r
250 }\r
251 \r
252 //\r
253 // Print Comma type for every 3 characters\r
254 //\r
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
260 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, *ValueBufferPtr--, Increment);\r
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
271 \r
272 //\r
273 // Print Null-terminator\r
274 //\r
275 BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);\r
276\r
277 return ((Buffer - OriginalBuffer) / Increment);\r
278}\r
279\r
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
285 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
286 this is the main print working routine.\r
287\r
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
302\r
303 @return The number of characters printed not including the Null-terminator.\r
304 If COUNT_ONLY_NO_PRINT was set returns the same, but without any\r
305 modification to Buffer.\r
306\r
307**/\r
308UINTN\r
309BasePrintLibSPrintMarker (\r
310 OUT CHAR8 *Buffer,\r
311 IN UINTN BufferSize,\r
312 IN UINTN Flags,\r
313 IN CONST CHAR8 *Format,\r
314 IN VA_LIST VaListMarker, OPTIONAL\r
315 IN BASE_LIST BaseListMarker OPTIONAL\r
316 )\r
317{\r
318 CHAR8 *OriginalBuffer;\r
319 CHAR8 *EndBuffer;\r
320 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
321 UINT32 BytesPerOutputCharacter;\r
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
344 UINT32 GuidData1;\r
345 UINT16 GuidData2;\r
346 UINT16 GuidData3;\r
347 UINTN LengthToReturn;\r
348\r
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
355 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r
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
367 }\r
368\r
369 if ((Flags & OUTPUT_UNICODE) != 0) {\r
370 BytesPerOutputCharacter = 2;\r
371 } else {\r
372 BytesPerOutputCharacter = 1;\r
373 }\r
374\r
375 LengthToReturn = 0;\r
376 EndBuffer = NULL;\r
377 OriginalBuffer = NULL;\r
378\r
379 //\r
380 // Reserve space for the Null terminator.\r
381 //\r
382 if (Buffer != NULL) {\r
383 BufferSize--;\r
384 OriginalBuffer = Buffer;\r
385\r
386 //\r
387 // Set the tag for the end of the input Buffer.\r
388 //\r
389 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;\r
390 }\r
391\r
392 if ((Flags & FORMAT_UNICODE) != 0) {\r
393 //\r
394 // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength\r
395 // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
396 //\r
397 ASSERT (StrSize ((CHAR16 *) Format) != 0);\r
398 BytesPerFormatCharacter = 2;\r
399 FormatMask = 0xffff;\r
400 } else {\r
401 //\r
402 // Make sure format string cannot contain more than PcdMaximumAsciiStringLength\r
403 // Ascii characters if PcdMaximumAsciiStringLength is not zero. \r
404 //\r
405 ASSERT (AsciiStrSize (Format) != 0);\r
406 BytesPerFormatCharacter = 1;\r
407 FormatMask = 0xff;\r
408 }\r
409\r
410 //\r
411 // Get the first character from the format string\r
412 //\r
413 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
414\r
415 //\r
416 // Loop until the end of the format string is reached or the output buffer is full\r
417 //\r
418 while (FormatCharacter != 0) {\r
419 if ((Buffer != NULL) && (Buffer >= EndBuffer)) {\r
420 break;\r
421 }\r
422 //\r
423 // Clear all the flag bits except those that may have been passed in\r
424 //\r
425 Flags &= (UINTN) (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);\r
426\r
427 //\r
428 // Set the default width to zero, and the default precision to 1\r
429 //\r
430 Width = 0;\r
431 Precision = 1;\r
432 Prefix = 0;\r
433 Comma = FALSE;\r
434 ZeroPad = FALSE;\r
435 Count = 0;\r
436 Digits = 0;\r
437\r
438 switch (FormatCharacter) {\r
439 case '%':\r
440 //\r
441 // Parse Flags and Width\r
442 //\r
443 for (Done = FALSE; !Done; ) {\r
444 Format += BytesPerFormatCharacter;\r
445 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
446 switch (FormatCharacter) {\r
447 case '.': \r
448 Flags |= PRECISION; \r
449 break;\r
450 case '-': \r
451 Flags |= LEFT_JUSTIFY; \r
452 break;\r
453 case '+': \r
454 Flags |= PREFIX_SIGN; \r
455 break;\r
456 case ' ': \r
457 Flags |= PREFIX_BLANK; \r
458 break;\r
459 case ',': \r
460 Flags |= COMMA_TYPE; \r
461 break;\r
462 case 'L':\r
463 case 'l': \r
464 Flags |= LONG_TYPE; \r
465 break;\r
466 case '*':\r
467 if ((Flags & PRECISION) == 0) {\r
468 Flags |= PAD_TO_WIDTH;\r
469 if (BaseListMarker == NULL) {\r
470 Width = VA_ARG (VaListMarker, UINTN);\r
471 } else {\r
472 Width = BASE_ARG (BaseListMarker, UINTN);\r
473 }\r
474 } else {\r
475 if (BaseListMarker == NULL) {\r
476 Precision = VA_ARG (VaListMarker, UINTN);\r
477 } else {\r
478 Precision = BASE_ARG (BaseListMarker, UINTN);\r
479 }\r
480 }\r
481 break;\r
482 case '0':\r
483 if ((Flags & PRECISION) == 0) {\r
484 Flags |= PREFIX_ZERO;\r
485 }\r
486 case '1':\r
487 case '2':\r
488 case '3':\r
489 case '4':\r
490 case '5':\r
491 case '6':\r
492 case '7':\r
493 case '8':\r
494 case '9':\r
495 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){\r
496 Count = (Count * 10) + FormatCharacter - '0';\r
497 Format += BytesPerFormatCharacter;\r
498 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
499 }\r
500 Format -= BytesPerFormatCharacter;\r
501 if ((Flags & PRECISION) == 0) {\r
502 Flags |= PAD_TO_WIDTH;\r
503 Width = Count;\r
504 } else {\r
505 Precision = Count;\r
506 }\r
507 break;\r
508 \r
509 case '\0':\r
510 //\r
511 // Make no output if Format string terminates unexpectedly when\r
512 // looking up for flag, width, precision and type. \r
513 //\r
514 Format -= BytesPerFormatCharacter;\r
515 Precision = 0;\r
516 //\r
517 // break skipped on purpose.\r
518 //\r
519 default:\r
520 Done = TRUE;\r
521 break;\r
522 }\r
523 } \r
524\r
525 //\r
526 // Handle each argument type\r
527 //\r
528 switch (FormatCharacter) {\r
529 case 'p':\r
530 //\r
531 // Flag space, +, 0, L & l are invalid for type p.\r
532 //\r
533 Flags &= ~((UINTN) (PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE));\r
534 if (sizeof (VOID *) > 4) {\r
535 Flags |= LONG_TYPE;\r
536 }\r
537 //\r
538 // break skipped on purpose\r
539 //\r
540 case 'X':\r
541 Flags |= PREFIX_ZERO;\r
542 //\r
543 // break skipped on purpose\r
544 //\r
545 case 'x':\r
546 Flags |= RADIX_HEX;\r
547 //\r
548 // break skipped on purpose\r
549 //\r
550 case 'd':\r
551 if ((Flags & LONG_TYPE) == 0) {\r
552 //\r
553 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
554 // This assumption is made so the format string definition is compatible with the ANSI C\r
555 // Specification for formatted strings. It is recommended that the Base Types be used \r
556 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
557 // provides an implementation that is compatible with that largest possible set of CPU \r
558 // architectures. This is why the type "int" is used in this one case.\r
559 //\r
560 if (BaseListMarker == NULL) {\r
561 Value = VA_ARG (VaListMarker, int);\r
562 } else {\r
563 Value = BASE_ARG (BaseListMarker, int);\r
564 }\r
565 } else {\r
566 if (BaseListMarker == NULL) {\r
567 Value = VA_ARG (VaListMarker, INT64);\r
568 } else {\r
569 Value = BASE_ARG (BaseListMarker, INT64);\r
570 }\r
571 }\r
572 if ((Flags & PREFIX_BLANK) != 0) {\r
573 Prefix = ' ';\r
574 }\r
575 if ((Flags & PREFIX_SIGN) != 0) {\r
576 Prefix = '+';\r
577 }\r
578 if ((Flags & COMMA_TYPE) != 0) {\r
579 Comma = TRUE;\r
580 }\r
581 if ((Flags & RADIX_HEX) == 0) {\r
582 Radix = 10;\r
583 if (Comma) {\r
584 Flags &= ~((UINTN) PREFIX_ZERO);\r
585 Precision = 1;\r
586 }\r
587 if (Value < 0) {\r
588 Flags |= PREFIX_SIGN;\r
589 Prefix = '-';\r
590 Value = -Value;\r
591 }\r
592 } else {\r
593 Radix = 16;\r
594 Comma = FALSE;\r
595 if ((Flags & LONG_TYPE) == 0 && Value < 0) {\r
596 //\r
597 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
598 // This assumption is made so the format string definition is compatible with the ANSI C\r
599 // Specification for formatted strings. It is recommended that the Base Types be used \r
600 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
601 // provides an implementation that is compatible with that largest possible set of CPU \r
602 // architectures. This is why the type "unsigned int" is used in this one case.\r
603 //\r
604 Value = (unsigned int)Value;\r
605 }\r
606 }\r
607 //\r
608 // Convert Value to a reversed string\r
609 //\r
610 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;\r
611 if (Value == 0 && Precision == 0) {\r
612 Count = 0;\r
613 }\r
614 ArgumentString = (CHAR8 *)ValueBuffer + Count;\r
615 \r
616 Digits = Count % 3;\r
617 if (Digits != 0) {\r
618 Digits = 3 - Digits;\r
619 }\r
620 if (Comma && Count != 0) {\r
621 Count += ((Count - 1) / 3);\r
622 }\r
623 if (Prefix != 0) {\r
624 Count++;\r
625 Precision++;\r
626 }\r
627 Flags |= ARGUMENT_REVERSED;\r
628 ZeroPad = TRUE;\r
629 if ((Flags & PREFIX_ZERO) != 0) {\r
630 if ((Flags & LEFT_JUSTIFY) == 0) {\r
631 if ((Flags & PAD_TO_WIDTH) != 0) {\r
632 if ((Flags & PRECISION) == 0) {\r
633 Precision = Width;\r
634 }\r
635 }\r
636 }\r
637 }\r
638 break;\r
639\r
640 case 's':\r
641 case 'S':\r
642 Flags |= ARGUMENT_UNICODE;\r
643 //\r
644 // break skipped on purpose\r
645 //\r
646 case 'a':\r
647 if (BaseListMarker == NULL) {\r
648 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);\r
649 } else {\r
650 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);\r
651 }\r
652 if (ArgumentString == NULL) {\r
653 Flags &= ~((UINTN) ARGUMENT_UNICODE);\r
654 ArgumentString = "<null string>";\r
655 }\r
656 //\r
657 // Set the default precision for string to be zero if not specified.\r
658 //\r
659 if ((Flags & PRECISION) == 0) {\r
660 Precision = 0;\r
661 }\r
662 break;\r
663\r
664 case 'c':\r
665 if (BaseListMarker == NULL) {\r
666 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;\r
667 } else {\r
668 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;\r
669 }\r
670 ArgumentString = (CHAR8 *)&Character;\r
671 Flags |= ARGUMENT_UNICODE;\r
672 break;\r
673\r
674 case 'g':\r
675 if (BaseListMarker == NULL) {\r
676 TmpGuid = VA_ARG (VaListMarker, GUID *);\r
677 } else {\r
678 TmpGuid = BASE_ARG (BaseListMarker, GUID *);\r
679 }\r
680 if (TmpGuid == NULL) {\r
681 ArgumentString = "<null guid>";\r
682 } else {\r
683 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));\r
684 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));\r
685 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));\r
686 BasePrintLibSPrint (\r
687 ValueBuffer,\r
688 MAXIMUM_VALUE_CHARACTERS, \r
689 0,\r
690 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
691 GuidData1,\r
692 GuidData2,\r
693 GuidData3,\r
694 TmpGuid->Data4[0],\r
695 TmpGuid->Data4[1],\r
696 TmpGuid->Data4[2],\r
697 TmpGuid->Data4[3],\r
698 TmpGuid->Data4[4],\r
699 TmpGuid->Data4[5],\r
700 TmpGuid->Data4[6],\r
701 TmpGuid->Data4[7]\r
702 );\r
703 ArgumentString = ValueBuffer;\r
704 }\r
705 break;\r
706\r
707 case 't':\r
708 if (BaseListMarker == NULL) {\r
709 TmpTime = VA_ARG (VaListMarker, TIME *); \r
710 } else {\r
711 TmpTime = BASE_ARG (BaseListMarker, TIME *); \r
712 }\r
713 if (TmpTime == NULL) {\r
714 ArgumentString = "<null time>";\r
715 } else {\r
716 BasePrintLibSPrint (\r
717 ValueBuffer,\r
718 MAXIMUM_VALUE_CHARACTERS,\r
719 0,\r
720 "%02d/%02d/%04d %02d:%02d",\r
721 TmpTime->Month,\r
722 TmpTime->Day,\r
723 TmpTime->Year,\r
724 TmpTime->Hour,\r
725 TmpTime->Minute\r
726 );\r
727 ArgumentString = ValueBuffer;\r
728 }\r
729 break;\r
730\r
731 case 'r':\r
732 if (BaseListMarker == NULL) {\r
733 Status = VA_ARG (VaListMarker, RETURN_STATUS);\r
734 } else {\r
735 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);\r
736 }\r
737 ArgumentString = ValueBuffer;\r
738 if (RETURN_ERROR (Status)) {\r
739 //\r
740 // Clear error bit\r
741 //\r
742 Index = Status & ~MAX_BIT;\r
743 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {\r
744 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];\r
745 }\r
746 } else {\r
747 Index = Status;\r
748 if (Index <= WARNING_STATUS_NUMBER) {\r
749 ArgumentString = mStatusString [Index];\r
750 }\r
751 }\r
752 if (ArgumentString == ValueBuffer) {\r
753 BasePrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);\r
754 }\r
755 break;\r
756\r
757 case '\r':\r
758 Format += BytesPerFormatCharacter;\r
759 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
760 if (FormatCharacter == '\n') {\r
761 //\r
762 // Translate '\r\n' to '\r\n'\r
763 //\r
764 ArgumentString = "\r\n";\r
765 } else {\r
766 //\r
767 // Translate '\r' to '\r'\r
768 //\r
769 ArgumentString = "\r";\r
770 Format -= BytesPerFormatCharacter;\r
771 }\r
772 break;\r
773\r
774 case '\n':\r
775 //\r
776 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
777 //\r
778 ArgumentString = "\r\n";\r
779 Format += BytesPerFormatCharacter;\r
780 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
781 if (FormatCharacter != '\r') {\r
782 Format -= BytesPerFormatCharacter;\r
783 }\r
784 break;\r
785\r
786 case '%':\r
787 default:\r
788 //\r
789 // if the type is '%' or unknown, then print it to the screen\r
790 //\r
791 ArgumentString = (CHAR8 *)&FormatCharacter;\r
792 Flags |= ARGUMENT_UNICODE;\r
793 break;\r
794 }\r
795 break;\r
796 \r
797 case '\r':\r
798 Format += BytesPerFormatCharacter;\r
799 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
800 if (FormatCharacter == '\n') {\r
801 //\r
802 // Translate '\r\n' to '\r\n'\r
803 //\r
804 ArgumentString = "\r\n";\r
805 } else {\r
806 //\r
807 // Translate '\r' to '\r'\r
808 //\r
809 ArgumentString = "\r";\r
810 Format -= BytesPerFormatCharacter;\r
811 }\r
812 break;\r
813\r
814 case '\n':\r
815 //\r
816 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
817 //\r
818 ArgumentString = "\r\n";\r
819 Format += BytesPerFormatCharacter;\r
820 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
821 if (FormatCharacter != '\r') {\r
822 Format -= BytesPerFormatCharacter;\r
823 }\r
824 break;\r
825\r
826 default:\r
827 ArgumentString = (CHAR8 *)&FormatCharacter;\r
828 Flags |= ARGUMENT_UNICODE;\r
829 break;\r
830 }\r
831\r
832 //\r
833 // Retrieve the ArgumentString attriubutes\r
834 //\r
835 if ((Flags & ARGUMENT_UNICODE) != 0) {\r
836 ArgumentMask = 0xffff;\r
837 BytesPerArgumentCharacter = 2;\r
838 } else {\r
839 ArgumentMask = 0xff;\r
840 BytesPerArgumentCharacter = 1;\r
841 }\r
842 if ((Flags & ARGUMENT_REVERSED) != 0) {\r
843 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;\r
844 } else {\r
845 //\r
846 // Compute the number of characters in ArgumentString and store it in Count\r
847 // ArgumentString is either null-terminated, or it contains Precision characters\r
848 //\r
849 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {\r
850 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;\r
851 if (ArgumentCharacter == 0) {\r
852 break;\r
853 }\r
854 }\r
855 }\r
856\r
857 if (Precision < Count) {\r
858 Precision = Count;\r
859 }\r
860\r
861 //\r
862 // Pad before the string\r
863 //\r
864 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {\r
865 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
866 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
867 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
868 }\r
869 }\r
870\r
871 if (ZeroPad) {\r
872 if (Prefix != 0) {\r
873 LengthToReturn += (1 * BytesPerOutputCharacter);\r
874 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
875 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
876 }\r
877 }\r
878 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
879 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
880 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);\r
881 }\r
882 } else {\r
883 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
884 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
885 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);\r
886 }\r
887 if (Prefix != 0) {\r
888 LengthToReturn += (1 * BytesPerOutputCharacter);\r
889 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
890 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
891 }\r
892 }\r
893 }\r
894\r
895 //\r
896 // Output the Prefix character if it is present\r
897 //\r
898 Index = 0;\r
899 if (Prefix != 0) {\r
900 Index++;\r
901 }\r
902\r
903 //\r
904 // Copy the string into the output buffer performing the required type conversions\r
905 //\r
906 while (Index < Count) {\r
907 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;\r
908\r
909 LengthToReturn += (1 * BytesPerOutputCharacter);\r
910 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
911 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);\r
912 }\r
913 ArgumentString += BytesPerArgumentCharacter;\r
914 Index++;\r
915 if (Comma) {\r
916 Digits++;\r
917 if (Digits == 3) {\r
918 Digits = 0;\r
919 Index++;\r
920 if (Index < Count) {\r
921 LengthToReturn += (1 * BytesPerOutputCharacter);\r
922 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
923 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);\r
924 }\r
925 }\r
926 }\r
927 }\r
928 }\r
929\r
930 //\r
931 // Pad after the string\r
932 //\r
933 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {\r
934 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
935 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
936 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
937 }\r
938 }\r
939\r
940 //\r
941 // Get the next character from the format string\r
942 //\r
943 Format += BytesPerFormatCharacter;\r
944\r
945 //\r
946 // Get the next character from the format string\r
947 //\r
948 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
949 }\r
950\r
951 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r
952 return (LengthToReturn / BytesPerOutputCharacter);\r
953 }\r
954\r
955 ASSERT (Buffer != NULL);\r
956 //\r
957 // Null terminate the Unicode or ASCII string\r
958 //\r
959 BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);\r
960 //\r
961 // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength\r
962 // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
963 //\r
964 ASSERT ((((Flags & OUTPUT_UNICODE) == 0)) || (StrSize ((CHAR16 *) OriginalBuffer) != 0));\r
965 //\r
966 // Make sure output buffer cannot contain more than PcdMaximumAsciiStringLength\r
967 // ASCII characters if PcdMaximumAsciiStringLength is not zero. \r
968 //\r
969 ASSERT ((((Flags & OUTPUT_UNICODE) != 0)) || (AsciiStrSize (OriginalBuffer) != 0));\r
970\r
971 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);\r
972}\r
973\r
974/**\r
975 Worker function that produces a Null-terminated string in an output buffer \r
976 based on a Null-terminated format string and variable argument list.\r
977\r
978 VSPrint function to process format and place the results in Buffer. Since a \r
979 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
980 this is the main print working routine\r
981\r
982 @param StartOfBuffer The character buffer to print the results of the parsing\r
983 of Format into.\r
984 @param BufferSize The maximum number of characters to put into buffer.\r
985 Zero means no limit.\r
986 @param Flags Initial flags value.\r
987 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set\r
988 @param FormatString A Null-terminated format string.\r
989 @param ... The variable argument list.\r
990\r
991 @return The number of characters printed.\r
992\r
993**/\r
994UINTN\r
995EFIAPI\r
996BasePrintLibSPrint (\r
997 OUT CHAR8 *StartOfBuffer,\r
998 IN UINTN BufferSize,\r
999 IN UINTN Flags,\r
1000 IN CONST CHAR8 *FormatString,\r
1001 ...\r
1002 )\r
1003{\r
1004 VA_LIST Marker;\r
1005 UINTN NumberOfPrinted;\r
1006\r
1007 VA_START (Marker, FormatString);\r
1008 NumberOfPrinted = BasePrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);\r
1009 VA_END (Marker);\r
1010 return NumberOfPrinted;\r
1011}\r