]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/BasePrintLib/PrintLibInternal.c
EmbeddedPkg: Fix mispellings
[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 - 2011, 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 4\r
18#define ERROR_STATUS_NUMBER 24\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 "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
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
60 @param Buffer The buffer to place the Unicode or ASCII string.\r
61 @param EndBuffer The end of the input Buffer. No characters will be\r
62 placed after that. \r
63 @param Length The count of character to be placed into Buffer.\r
64 (Negative value indicates no buffer fill.)\r
65 @param Character The character to be placed into Buffer.\r
66 @param Increment The character increment in Buffer.\r
67\r
68 @return Buffer.\r
69\r
70**/\r
71CHAR8 *\r
72BasePrintLibFillBuffer (\r
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
78 )\r
79{\r
80 INTN Index;\r
81 \r
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
91}\r
92\r
93/**\r
94 Internal function that convert a number to a string in Buffer.\r
95\r
96 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.\r
97\r
98 @param Buffer Location to place the ASCII string of Value.\r
99 @param Value The value to convert to a Decimal or Hexadecimal string in Buffer.\r
100 @param Radix Radix of the value\r
101\r
102 @return A pointer to the end of buffer filled with ASCII string.\r
103\r
104**/\r
105CHAR8 *\r
106BasePrintLibValueToString (\r
107 IN OUT CHAR8 *Buffer, \r
108 IN INT64 Value, \r
109 IN UINTN Radix\r
110 )\r
111{\r
112 UINT32 Remainder;\r
113\r
114 //\r
115 // Loop to convert one digit at a time in reverse order\r
116 //\r
117 *Buffer = 0;\r
118 do {\r
119 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r
120 *(++Buffer) = mHexStr[Remainder];\r
121 } while (Value != 0);\r
122\r
123 //\r
124 // Return pointer of the end of filled buffer.\r
125 //\r
126 return Buffer;\r
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
135 The total number of characters placed in Buffer is returned.\r
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
145 If Value is < 0, then the fist character in Buffer is a '-'.\r
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
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
155 @param Buffer The pointer to the output buffer for the produced Null-terminated\r
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
162 @param Increment The character increment in Buffer.\r
163 \r
164 @return Total number of characters required to perform the conversion.\r
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
179 CHAR8 *ValueBufferPtr;\r
180 UINTN Count;\r
181 UINTN Digits;\r
182 UINTN Index;\r
183 UINTN Radix;\r
184\r
185 //\r
186 // Make sure Buffer is not NULL and Width < MAXIMUM\r
187 //\r
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
196 // If both COMMA_TYPE and RADIX_HEX are set, then ASSERT ()\r
197 //\r
198 ASSERT (((Flags & COMMA_TYPE) == 0) || ((Flags & RADIX_HEX) == 0));\r
199\r
200 OriginalBuffer = Buffer;\r
201 \r
202 //\r
203 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.\r
204 //\r
205 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r
206 Flags &= (~PREFIX_ZERO);\r
207 }\r
208 //\r
209 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
210 //\r
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
218 \r
219 //\r
220 // Convert decimal negative\r
221 //\r
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
227 \r
228 //\r
229 // Count the length of the value string.\r
230 //\r
231 Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;\r
232 ValueBufferPtr = BasePrintLibValueToString (ValueBuffer, Value, Radix);\r
233 Count = ValueBufferPtr - ValueBuffer;\r
234 \r
235 //\r
236 // Append Zero\r
237 //\r
238 if ((Flags & PREFIX_ZERO) != 0) {\r
239 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);\r
240 }\r
241 \r
242 //\r
243 // Print Comma type for every 3 characters\r
244 //\r
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
250 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, *ValueBufferPtr--, Increment);\r
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
261 \r
262 //\r
263 // Print Null-terminator\r
264 //\r
265 BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);\r
266\r
267 return ((Buffer - OriginalBuffer) / Increment);\r
268}\r
269\r
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
275 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
276 this is the main print working routine.\r
277\r
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
292\r
293 @return The number of characters printed not including the Null-terminator.\r
294 If COUNT_ONLY_NO_PRINT was set returns the same, but without any\r
295 modification to Buffer.\r
296\r
297**/\r
298UINTN\r
299BasePrintLibSPrintMarker (\r
300 OUT CHAR8 *Buffer,\r
301 IN UINTN BufferSize,\r
302 IN UINTN Flags,\r
303 IN CONST CHAR8 *Format,\r
304 IN VA_LIST VaListMarker, OPTIONAL\r
305 IN BASE_LIST BaseListMarker OPTIONAL\r
306 )\r
307{\r
308 CHAR8 *OriginalBuffer;\r
309 CHAR8 *EndBuffer;\r
310 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
311 UINT32 BytesPerOutputCharacter;\r
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
334 UINT32 GuidData1;\r
335 UINT16 GuidData2;\r
336 UINT16 GuidData3;\r
337 UINTN LengthToReturn;\r
338\r
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
345 if (Flags & COUNT_ONLY_NO_PRINT) {\r
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
357 }\r
358\r
359 if (Flags & OUTPUT_UNICODE) {\r
360 BytesPerOutputCharacter = 2;\r
361 } else {\r
362 BytesPerOutputCharacter = 1;\r
363 }\r
364\r
365 LengthToReturn = 0;\r
366\r
367 //\r
368 // Reserve space for the Null terminator.\r
369 //\r
370 BufferSize--;\r
371 OriginalBuffer = Buffer;\r
372\r
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
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
408 Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);\r
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
452 if (BaseListMarker == NULL) {\r
453 Width = VA_ARG (VaListMarker, UINTN);\r
454 } else {\r
455 Width = BASE_ARG (BaseListMarker, UINTN);\r
456 }\r
457 } else {\r
458 if (BaseListMarker == NULL) {\r
459 Precision = VA_ARG (VaListMarker, UINTN);\r
460 } else {\r
461 Precision = BASE_ARG (BaseListMarker, UINTN);\r
462 }\r
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
520 case 'X':\r
521 Flags |= PREFIX_ZERO;\r
522 //\r
523 // break skipped on purpose\r
524 //\r
525 case 'x':\r
526 Flags |= RADIX_HEX;\r
527 //\r
528 // break skipped on purpose\r
529 //\r
530 case 'd':\r
531 if ((Flags & LONG_TYPE) == 0) {\r
532 //\r
533 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
534 // This assumption is made so the format string definition is compatible with the ANSI C\r
535 // Specification for formatted strings. It is recommended that the Base Types be used \r
536 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
537 // provides an implementation that is compatible with that largest possible set of CPU \r
538 // architectures. This is why the type "int" is used in this one case.\r
539 //\r
540 if (BaseListMarker == NULL) {\r
541 Value = VA_ARG (VaListMarker, int);\r
542 } else {\r
543 Value = BASE_ARG (BaseListMarker, int);\r
544 }\r
545 } else {\r
546 if (BaseListMarker == NULL) {\r
547 Value = VA_ARG (VaListMarker, INT64);\r
548 } else {\r
549 Value = BASE_ARG (BaseListMarker, INT64);\r
550 }\r
551 }\r
552 if ((Flags & PREFIX_BLANK) != 0) {\r
553 Prefix = ' ';\r
554 }\r
555 if ((Flags & PREFIX_SIGN) != 0) {\r
556 Prefix = '+';\r
557 }\r
558 if ((Flags & COMMA_TYPE) != 0) {\r
559 Comma = TRUE;\r
560 }\r
561 if ((Flags & RADIX_HEX) == 0) {\r
562 Radix = 10;\r
563 if (Comma) {\r
564 Flags &= (~PREFIX_ZERO);\r
565 Precision = 1;\r
566 }\r
567 if (Value < 0) {\r
568 Flags |= PREFIX_SIGN;\r
569 Prefix = '-';\r
570 Value = -Value;\r
571 }\r
572 } else {\r
573 Radix = 16;\r
574 Comma = FALSE;\r
575 if ((Flags & LONG_TYPE) == 0 && Value < 0) {\r
576 //\r
577 // 'd','x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
578 // This assumption is made so the format string definition is compatible with the ANSI C\r
579 // Specification for formatted strings. It is recommended that the Base Types be used \r
580 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
581 // provides an implementation that is compatible with that largest possible set of CPU \r
582 // architectures. This is why the type "unsigned int" is used in this one case.\r
583 //\r
584 Value = (unsigned int)Value;\r
585 }\r
586 }\r
587 //\r
588 // Convert Value to a reversed string\r
589 //\r
590 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;\r
591 if (Value == 0 && Precision == 0) {\r
592 Count = 0;\r
593 }\r
594 ArgumentString = (CHAR8 *)ValueBuffer + Count;\r
595 \r
596 Digits = Count % 3;\r
597 if (Digits != 0) {\r
598 Digits = 3 - Digits;\r
599 }\r
600 if (Comma && Count != 0) {\r
601 Count += ((Count - 1) / 3);\r
602 }\r
603 if (Prefix != 0) {\r
604 Count++;\r
605 Precision++;\r
606 }\r
607 Flags |= ARGUMENT_REVERSED;\r
608 ZeroPad = TRUE;\r
609 if ((Flags & PREFIX_ZERO) != 0) {\r
610 if ((Flags & LEFT_JUSTIFY) == 0) {\r
611 if ((Flags & PAD_TO_WIDTH) != 0) {\r
612 if ((Flags & PRECISION) == 0) {\r
613 Precision = Width;\r
614 }\r
615 }\r
616 }\r
617 }\r
618 break;\r
619\r
620 case 's':\r
621 case 'S':\r
622 Flags |= ARGUMENT_UNICODE;\r
623 //\r
624 // break skipped on purpose\r
625 //\r
626 case 'a':\r
627 if (BaseListMarker == NULL) {\r
628 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);\r
629 } else {\r
630 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);\r
631 }\r
632 if (ArgumentString == NULL) {\r
633 Flags &= (~ARGUMENT_UNICODE);\r
634 ArgumentString = "<null string>";\r
635 }\r
636 //\r
637 // Set the default precision for string to be zero if not specified.\r
638 //\r
639 if ((Flags & PRECISION) == 0) {\r
640 Precision = 0;\r
641 }\r
642 break;\r
643\r
644 case 'c':\r
645 if (BaseListMarker == NULL) {\r
646 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;\r
647 } else {\r
648 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;\r
649 }\r
650 ArgumentString = (CHAR8 *)&Character;\r
651 Flags |= ARGUMENT_UNICODE;\r
652 break;\r
653\r
654 case 'g':\r
655 if (BaseListMarker == NULL) {\r
656 TmpGuid = VA_ARG (VaListMarker, GUID *);\r
657 } else {\r
658 TmpGuid = BASE_ARG (BaseListMarker, GUID *);\r
659 }\r
660 if (TmpGuid == NULL) {\r
661 ArgumentString = "<null guid>";\r
662 } else {\r
663 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));\r
664 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));\r
665 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));\r
666 BasePrintLibSPrint (\r
667 ValueBuffer,\r
668 MAXIMUM_VALUE_CHARACTERS, \r
669 0,\r
670 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
671 GuidData1,\r
672 GuidData2,\r
673 GuidData3,\r
674 TmpGuid->Data4[0],\r
675 TmpGuid->Data4[1],\r
676 TmpGuid->Data4[2],\r
677 TmpGuid->Data4[3],\r
678 TmpGuid->Data4[4],\r
679 TmpGuid->Data4[5],\r
680 TmpGuid->Data4[6],\r
681 TmpGuid->Data4[7]\r
682 );\r
683 ArgumentString = ValueBuffer;\r
684 }\r
685 break;\r
686\r
687 case 't':\r
688 if (BaseListMarker == NULL) {\r
689 TmpTime = VA_ARG (VaListMarker, TIME *); \r
690 } else {\r
691 TmpTime = BASE_ARG (BaseListMarker, TIME *); \r
692 }\r
693 if (TmpTime == NULL) {\r
694 ArgumentString = "<null time>";\r
695 } else {\r
696 BasePrintLibSPrint (\r
697 ValueBuffer,\r
698 MAXIMUM_VALUE_CHARACTERS,\r
699 0,\r
700 "%02d/%02d/%04d %02d:%02d",\r
701 TmpTime->Month,\r
702 TmpTime->Day,\r
703 TmpTime->Year,\r
704 TmpTime->Hour,\r
705 TmpTime->Minute\r
706 );\r
707 ArgumentString = ValueBuffer;\r
708 }\r
709 break;\r
710\r
711 case 'r':\r
712 if (BaseListMarker == NULL) {\r
713 Status = VA_ARG (VaListMarker, RETURN_STATUS);\r
714 } else {\r
715 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);\r
716 }\r
717 ArgumentString = ValueBuffer;\r
718 if (RETURN_ERROR (Status)) {\r
719 //\r
720 // Clear error bit\r
721 //\r
722 Index = Status & ~MAX_BIT;\r
723 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {\r
724 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];\r
725 }\r
726 } else {\r
727 Index = Status;\r
728 if (Index <= WARNING_STATUS_NUMBER) {\r
729 ArgumentString = mStatusString [Index];\r
730 }\r
731 }\r
732 if (ArgumentString == ValueBuffer) {\r
733 BasePrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);\r
734 }\r
735 break;\r
736\r
737 case '\r':\r
738 Format += BytesPerFormatCharacter;\r
739 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
740 if (FormatCharacter == '\n') {\r
741 //\r
742 // Translate '\r\n' to '\r\n'\r
743 //\r
744 ArgumentString = "\r\n";\r
745 } else {\r
746 //\r
747 // Translate '\r' to '\r'\r
748 //\r
749 ArgumentString = "\r";\r
750 Format -= BytesPerFormatCharacter;\r
751 }\r
752 break;\r
753\r
754 case '\n':\r
755 //\r
756 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
757 //\r
758 ArgumentString = "\r\n";\r
759 Format += BytesPerFormatCharacter;\r
760 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
761 if (FormatCharacter != '\r') {\r
762 Format -= BytesPerFormatCharacter;\r
763 }\r
764 break;\r
765\r
766 case '%':\r
767 default:\r
768 //\r
769 // if the type is '%' or unknown, then print it to the screen\r
770 //\r
771 ArgumentString = (CHAR8 *)&FormatCharacter;\r
772 Flags |= ARGUMENT_UNICODE;\r
773 break;\r
774 }\r
775 break;\r
776 \r
777 case '\r':\r
778 Format += BytesPerFormatCharacter;\r
779 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
780 if (FormatCharacter == '\n') {\r
781 //\r
782 // Translate '\r\n' to '\r\n'\r
783 //\r
784 ArgumentString = "\r\n";\r
785 } else {\r
786 //\r
787 // Translate '\r' to '\r'\r
788 //\r
789 ArgumentString = "\r";\r
790 Format -= BytesPerFormatCharacter;\r
791 }\r
792 break;\r
793\r
794 case '\n':\r
795 //\r
796 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
797 //\r
798 ArgumentString = "\r\n";\r
799 Format += BytesPerFormatCharacter;\r
800 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
801 if (FormatCharacter != '\r') {\r
802 Format -= BytesPerFormatCharacter;\r
803 }\r
804 break;\r
805\r
806 default:\r
807 ArgumentString = (CHAR8 *)&FormatCharacter;\r
808 Flags |= ARGUMENT_UNICODE;\r
809 break;\r
810 }\r
811\r
812 //\r
813 // Retrieve the ArgumentString attriubutes\r
814 //\r
815 if ((Flags & ARGUMENT_UNICODE) != 0) {\r
816 ArgumentMask = 0xffff;\r
817 BytesPerArgumentCharacter = 2;\r
818 } else {\r
819 ArgumentMask = 0xff;\r
820 BytesPerArgumentCharacter = 1;\r
821 }\r
822 if ((Flags & ARGUMENT_REVERSED) != 0) {\r
823 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;\r
824 } else {\r
825 //\r
826 // Compute the number of characters in ArgumentString and store it in Count\r
827 // ArgumentString is either null-terminated, or it contains Precision characters\r
828 //\r
829 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {\r
830 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;\r
831 if (ArgumentCharacter == 0) {\r
832 break;\r
833 }\r
834 }\r
835 }\r
836\r
837 if (Precision < Count) {\r
838 Precision = Count;\r
839 }\r
840\r
841 //\r
842 // Pad before the string\r
843 //\r
844 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {\r
845 if (Flags & COUNT_ONLY_NO_PRINT) {\r
846 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
847 } else {\r
848 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
849 }\r
850 }\r
851\r
852 if (ZeroPad) {\r
853 if (Prefix != 0) {\r
854 if (Flags & COUNT_ONLY_NO_PRINT) {\r
855 LengthToReturn += (1 * BytesPerOutputCharacter);\r
856 } else {\r
857 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
858 }\r
859 }\r
860 if (Flags & COUNT_ONLY_NO_PRINT) {\r
861 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
862 } else {\r
863 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);\r
864 }\r
865 } else {\r
866 if (Flags & COUNT_ONLY_NO_PRINT) {\r
867 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
868 } else {\r
869 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);\r
870 }\r
871 if (Prefix != 0) {\r
872 if (Flags & COUNT_ONLY_NO_PRINT) {\r
873 LengthToReturn += (1 * BytesPerOutputCharacter);\r
874 } else {\r
875 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
876 }\r
877 }\r
878 }\r
879\r
880 //\r
881 // Output the Prefix character if it is present\r
882 //\r
883 Index = 0;\r
884 if (Prefix != 0) {\r
885 Index++;\r
886 }\r
887\r
888 //\r
889 // Copy the string into the output buffer performing the required type conversions\r
890 //\r
891 while (Index < Count) {\r
892 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;\r
893\r
894 if (Flags & COUNT_ONLY_NO_PRINT) {\r
895 LengthToReturn += (1 * BytesPerOutputCharacter);\r
896 } else {\r
897 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);\r
898 }\r
899 ArgumentString += BytesPerArgumentCharacter;\r
900 Index++;\r
901 if (Comma) {\r
902 Digits++;\r
903 if (Digits == 3) {\r
904 Digits = 0;\r
905 Index++;\r
906 if (Index < Count) {\r
907 if (Flags & COUNT_ONLY_NO_PRINT) {\r
908 LengthToReturn += (1 * BytesPerOutputCharacter);\r
909 } else {\r
910 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);\r
911 }\r
912 }\r
913 }\r
914 }\r
915 }\r
916\r
917 //\r
918 // Pad after the string\r
919 //\r
920 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {\r
921 if (Flags & COUNT_ONLY_NO_PRINT) {\r
922 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
923 } else {\r
924 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
925 }\r
926 }\r
927\r
928 //\r
929 // Get the next character from the format string\r
930 //\r
931 Format += BytesPerFormatCharacter;\r
932\r
933 //\r
934 // Get the next character from the format string\r
935 //\r
936 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
937 }\r
938\r
939 if (Flags & COUNT_ONLY_NO_PRINT) {\r
940 return (LengthToReturn / BytesPerOutputCharacter);\r
941 }\r
942\r
943 //\r
944 // Null terminate the Unicode or ASCII string\r
945 //\r
946 BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);\r
947 //\r
948 // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength\r
949 // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
950 //\r
951 ASSERT ((((Flags & OUTPUT_UNICODE) == 0)) || (StrSize ((CHAR16 *) OriginalBuffer) != 0));\r
952 //\r
953 // Make sure output buffer cannot contain more than PcdMaximumAsciiStringLength\r
954 // ASCII characters if PcdMaximumAsciiStringLength is not zero. \r
955 //\r
956 ASSERT ((((Flags & OUTPUT_UNICODE) != 0)) || (AsciiStrSize (OriginalBuffer) != 0));\r
957\r
958 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);\r
959}\r
960\r
961/**\r
962 Worker function that produces a Null-terminated string in an output buffer \r
963 based on a Null-terminated format string and variable argument list.\r
964\r
965 VSPrint function to process format and place the results in Buffer. Since a \r
966 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
967 this is the main print working routine\r
968\r
969 @param StartOfBuffer The character buffer to print the results of the parsing\r
970 of Format into.\r
971 @param BufferSize The maximum number of characters to put into buffer.\r
972 Zero means no limit.\r
973 @param Flags Initial flags value.\r
974 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set\r
975 @param FormatString A Null-terminated format string.\r
976 @param ... The variable argument list.\r
977\r
978 @return The number of characters printed.\r
979\r
980**/\r
981UINTN\r
982EFIAPI\r
983BasePrintLibSPrint (\r
984 OUT CHAR8 *StartOfBuffer,\r
985 IN UINTN BufferSize,\r
986 IN UINTN Flags,\r
987 IN CONST CHAR8 *FormatString,\r
988 ...\r
989 )\r
990{\r
991 VA_LIST Marker;\r
992\r
993 VA_START (Marker, FormatString);\r
994 return BasePrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);\r
995}\r