Commit | Line | Data |
---|---|---|
e1f414b6 | 1 | /** @file\r |
eceb3a4c | 2 | Print Library internal worker functions.\r |
e1f414b6 | 3 | \r |
eceb3a4c | 4 | Copyright (c) 2006 - 2008, Intel Corporation<BR>\r |
e1f414b6 | 5 | All rights reserved. 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 | |
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 | 20 | GLOBAL_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 | 22 | GLOBAL_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 | |
60 | @param Buffer 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 Count of character to be placed into Buffer.\r | |
64 | @param Character Character to be placed into Buffer.\r | |
65 | @param Increment Character increment in Buffer.\r | |
66 | \r | |
eceb3a4c | 67 | @return Buffer Buffer filled with the input Character.\r |
e1f414b6 | 68 | \r |
69 | **/\r | |
70 | CHAR8 *\r | |
71 | BasePrintLibFillBuffer (\r | |
eceb3a4c LG |
72 | OUT CHAR8 *Buffer,\r |
73 | IN CHAR8 *EndBuffer,\r | |
74 | IN INTN Length,\r | |
75 | IN UINTN Character,\r | |
76 | IN INTN Increment\r | |
e1f414b6 | 77 | )\r |
78 | {\r | |
bcccf0b0 | 79 | UINTN FillBufferSize;\r |
80 | \r | |
81 | if(Increment == 1) {\r | |
82 | FillBufferSize = MIN (Length, (EndBuffer - Buffer));\r | |
83 | Buffer = SetMem (Buffer, FillBufferSize, (UINT8) Character);\r | |
d6039b1d | 84 | } else {\r |
bcccf0b0 | 85 | FillBufferSize = MIN (Length << 1, (EndBuffer - Buffer));\r |
86 | Buffer = SetMem16 (Buffer, FillBufferSize, (UINT16) Character);\r | |
e1f414b6 | 87 | }\r |
bcccf0b0 | 88 | \r |
89 | return Buffer + FillBufferSize;\r | |
e1f414b6 | 90 | }\r |
91 | \r | |
92 | /**\r | |
93 | Internal function that convert a decimal number to a string in Buffer.\r | |
94 | \r | |
95 | Print worker function that convert a decimal number to a string in Buffer.\r | |
96 | \r | |
97 | @param Buffer Location to place the Unicode or ASCII string of Value.\r | |
bcccf0b0 | 98 | @param Value Value to convert to a Decimal or Hexadecimal string in Buffer.\r |
e1f414b6 | 99 | @param Radix Radix of the value\r |
100 | \r | |
101 | @return Number of characters printed.\r | |
102 | \r | |
103 | **/\r | |
104 | UINTN\r | |
e1f414b6 | 105 | BasePrintLibValueToString (\r |
efb23117 | 106 | IN OUT CHAR8 *Buffer, \r |
107 | IN INT64 Value, \r | |
e1f414b6 | 108 | IN UINTN Radix\r |
109 | )\r | |
110 | {\r | |
111 | UINTN Digits;\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 | Digits = 0;\r | |
119 | do {\r | |
120 | Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r | |
121 | *(Buffer++) = mHexStr[Remainder];\r | |
122 | Digits++;\r | |
123 | } while (Value != 0);\r | |
eceb3a4c LG |
124 | \r |
125 | //\r | |
126 | // the length of Buffer string converted from Value\r | |
127 | //\r | |
e1f414b6 | 128 | return Digits;\r |
129 | }\r | |
130 | \r | |
131 | /**\r | |
132 | Internal function that converts a decimal value to a Null-terminated string.\r | |
133 | \r | |
134 | Converts the decimal number specified by Value to a Null-terminated \r | |
135 | string specified by Buffer containing at most Width characters.\r | |
136 | If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r | |
efb23117 | 137 | The total number of characters placed in Buffer is returned.\r |
e1f414b6 | 138 | If the conversion contains more than Width characters, then only the first\r |
139 | Width characters are returned, and the total number of characters \r | |
140 | required to perform the conversion is returned.\r | |
141 | Additional conversion parameters are specified in Flags. \r | |
142 | The Flags bit LEFT_JUSTIFY is always ignored.\r | |
143 | All conversions are left justified in Buffer.\r | |
144 | If Width is 0, PREFIX_ZERO is ignored in Flags.\r | |
145 | If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r | |
146 | are inserted every 3rd digit starting from the right.\r | |
efb23117 | 147 | If Value is < 0, then the fist character in Buffer is a '-'.\r |
e1f414b6 | 148 | If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r |
149 | then Buffer is padded with '0' characters so the combination of the optional '-' \r | |
150 | sign character, '0' characters, digit characters for Value, and the Null-terminator\r | |
151 | add up to Width characters.\r | |
e1f414b6 | 152 | \r |
153 | If Buffer is NULL, then ASSERT().\r | |
154 | If unsupported bits are set in Flags, then ASSERT().\r | |
155 | If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r | |
156 | \r | |
157 | @param Buffer Pointer to the output buffer for the produced Null-terminated\r | |
158 | string.\r | |
159 | @param Flags The bitmask of flags that specify left justification, zero pad,\r | |
160 | and commas.\r | |
161 | @param Value The 64-bit signed value to convert to a string.\r | |
162 | @param Width The maximum number of characters to place in Buffer, not including\r | |
163 | the Null-terminator.\r | |
164 | @param Increment Character increment in Buffer.\r | |
165 | \r | |
efb23117 | 166 | @return Total number of characters required to perform the conversion.\r |
e1f414b6 | 167 | \r |
168 | **/\r | |
169 | UINTN\r | |
170 | BasePrintLibConvertValueToString (\r | |
171 | IN OUT CHAR8 *Buffer,\r | |
172 | IN UINTN Flags,\r | |
173 | IN INT64 Value,\r | |
174 | IN UINTN Width,\r | |
175 | IN UINTN Increment\r | |
176 | )\r | |
177 | {\r | |
178 | CHAR8 *OriginalBuffer;\r | |
179 | CHAR8 *EndBuffer;\r | |
180 | CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r | |
181 | UINTN Count;\r | |
182 | UINTN Digits;\r | |
183 | UINTN Index;\r | |
184 | UINTN Radix;\r | |
185 | \r | |
eceb3a4c LG |
186 | //\r |
187 | // Make sure Buffer is not NULL and Width < MAXIMUM\r | |
188 | //\r | |
e1f414b6 | 189 | ASSERT (Buffer != NULL);\r |
190 | ASSERT (Width < MAXIMUM_VALUE_CHARACTERS);\r | |
191 | //\r | |
192 | // Make sure Flags can only contain supported bits.\r | |
193 | //\r | |
194 | ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0);\r | |
195 | \r | |
196 | //\r | |
197 | // If both COMMA_TYPE and HEX_RADIX are set, then ASSERT ()\r | |
198 | //\r | |
199 | ASSERT (((Flags & COMMA_TYPE) != 0 && (Flags & RADIX_HEX) != 0) == FALSE);\r | |
200 | \r | |
201 | OriginalBuffer = Buffer;\r | |
eceb3a4c LG |
202 | \r |
203 | //\r | |
204 | // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.\r | |
205 | //\r | |
e1f414b6 | 206 | if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r |
207 | Flags &= (~PREFIX_ZERO);\r | |
208 | }\r | |
eceb3a4c LG |
209 | //\r |
210 | // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r | |
211 | //\r | |
e1f414b6 | 212 | if (Width == 0) {\r |
213 | Width = MAXIMUM_VALUE_CHARACTERS - 1;\r | |
214 | }\r | |
215 | //\r | |
216 | // Set the tag for the end of the input Buffer.\r | |
217 | //\r | |
218 | EndBuffer = Buffer + Width * Increment;\r | |
eceb3a4c LG |
219 | \r |
220 | //\r | |
221 | // Convert decimal negative\r | |
222 | //\r | |
e1f414b6 | 223 | if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {\r |
224 | Value = -Value;\r | |
225 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);\r | |
226 | Width--;\r | |
227 | }\r | |
eceb3a4c LG |
228 | \r |
229 | //\r | |
230 | // Count the length of the value string.\r | |
231 | //\r | |
e1f414b6 | 232 | Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;\r |
233 | Count = BasePrintLibValueToString (ValueBuffer, Value, Radix);\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 | |
250 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ValueBuffer[Count - Index], 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 | |
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 | |
275 | VA_LIST is used this rountine allows the nesting of Vararg routines. Thus \r | |
276 | this is the main print working routine.\r | |
277 | \r | |
278 | @param Buffer Character buffer to print the results of the parsing\r | |
279 | of Format into.\r | |
280 | @param BufferSize Maximum number of characters to put into buffer.\r | |
281 | @param Flags Intial flags value.\r | |
282 | Can only have FORMAT_UNICODE and OUTPUT_UNICODE set.\r | |
283 | @param Format Null-terminated format string.\r | |
284 | @param Marker Vararg list consumed by processing Format.\r | |
285 | \r | |
286 | @return Number of characters printed not including the Null-terminator.\r | |
287 | \r | |
288 | **/\r | |
289 | UINTN\r | |
290 | BasePrintLibVSPrint (\r | |
291 | OUT CHAR8 *Buffer,\r | |
292 | IN UINTN BufferSize,\r | |
293 | IN UINTN Flags,\r | |
294 | IN CONST CHAR8 *Format,\r | |
295 | IN VA_LIST Marker\r | |
296 | )\r | |
297 | {\r | |
298 | CHAR8 *OriginalBuffer;\r | |
299 | CHAR8 *EndBuffer;\r | |
300 | CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r | |
301 | UINTN BytesPerOutputCharacter;\r | |
302 | UINTN BytesPerFormatCharacter;\r | |
303 | UINTN FormatMask;\r | |
304 | UINTN FormatCharacter;\r | |
305 | UINTN Width;\r | |
306 | UINTN Precision;\r | |
307 | INT64 Value;\r | |
308 | CONST CHAR8 *ArgumentString;\r | |
309 | UINTN Character;\r | |
310 | GUID *TmpGuid;\r | |
311 | TIME *TmpTime;\r | |
312 | UINTN Count;\r | |
313 | UINTN ArgumentMask;\r | |
314 | INTN BytesPerArgumentCharacter;\r | |
315 | UINTN ArgumentCharacter;\r | |
316 | BOOLEAN Done;\r | |
317 | UINTN Index;\r | |
318 | CHAR8 Prefix;\r | |
319 | BOOLEAN ZeroPad;\r | |
320 | BOOLEAN Comma;\r | |
321 | UINTN Digits;\r | |
322 | UINTN Radix;\r | |
323 | RETURN_STATUS Status;\r | |
324 | \r | |
325 | if (BufferSize == 0) {\r | |
326 | return 0;\r | |
327 | }\r | |
328 | ASSERT (Buffer != NULL);\r | |
329 | \r | |
330 | if ((Flags & OUTPUT_UNICODE) != 0) {\r | |
331 | BytesPerOutputCharacter = 2;\r | |
332 | } else {\r | |
333 | BytesPerOutputCharacter = 1;\r | |
334 | }\r | |
335 | \r | |
336 | //\r | |
337 | // Reserve space for the Null terminator.\r | |
338 | //\r | |
339 | BufferSize--;\r | |
340 | OriginalBuffer = Buffer;\r | |
341 | //\r | |
342 | // Set the tag for the end of the input Buffer.\r | |
343 | //\r | |
344 | EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;\r | |
345 | \r | |
346 | if ((Flags & FORMAT_UNICODE) != 0) {\r | |
347 | //\r | |
348 | // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength\r | |
349 | // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r | |
350 | //\r | |
351 | ASSERT (StrSize ((CHAR16 *) Format) != 0);\r | |
352 | BytesPerFormatCharacter = 2;\r | |
353 | FormatMask = 0xffff;\r | |
354 | } else {\r | |
355 | //\r | |
356 | // Make sure format string cannot contain more than PcdMaximumAsciiStringLength\r | |
357 | // Ascii characters if PcdMaximumAsciiStringLength is not zero. \r | |
358 | //\r | |
359 | ASSERT (AsciiStrSize (Format) != 0);\r | |
360 | BytesPerFormatCharacter = 1;\r | |
361 | FormatMask = 0xff;\r | |
362 | }\r | |
363 | \r | |
364 | \r | |
365 | \r | |
366 | //\r | |
367 | // Get the first character from the format string\r | |
368 | //\r | |
369 | FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r | |
370 | \r | |
371 | //\r | |
372 | // Loop until the end of the format string is reached or the output buffer is full\r | |
373 | //\r | |
374 | while (FormatCharacter != 0 && Buffer < EndBuffer) {\r | |
375 | //\r | |
376 | // Clear all the flag bits except those that may have been passed in\r | |
377 | //\r | |
378 | Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE);\r | |
379 | \r | |
380 | //\r | |
381 | // Set the default width to zero, and the default precision to 1\r | |
382 | //\r | |
383 | Width = 0;\r | |
384 | Precision = 1;\r | |
385 | Prefix = 0;\r | |
386 | Comma = FALSE;\r | |
387 | ZeroPad = FALSE;\r | |
388 | Count = 0;\r | |
389 | Digits = 0;\r | |
390 | \r | |
391 | switch (FormatCharacter) {\r | |
392 | case '%':\r | |
393 | //\r | |
394 | // Parse Flags and Width\r | |
395 | //\r | |
396 | for (Done = FALSE; !Done; ) {\r | |
397 | Format += BytesPerFormatCharacter;\r | |
398 | FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r | |
399 | switch (FormatCharacter) {\r | |
400 | case '.': \r | |
401 | Flags |= PRECISION; \r | |
402 | break;\r | |
403 | case '-': \r | |
404 | Flags |= LEFT_JUSTIFY; \r | |
405 | break;\r | |
406 | case '+': \r | |
407 | Flags |= PREFIX_SIGN; \r | |
408 | break;\r | |
409 | case ' ': \r | |
410 | Flags |= PREFIX_BLANK; \r | |
411 | break;\r | |
412 | case ',': \r | |
413 | Flags |= COMMA_TYPE; \r | |
414 | break;\r | |
415 | case 'L':\r | |
416 | case 'l': \r | |
417 | Flags |= LONG_TYPE; \r | |
418 | break;\r | |
419 | case '*':\r | |
420 | if ((Flags & PRECISION) == 0) {\r | |
421 | Flags |= PAD_TO_WIDTH;\r | |
422 | Width = VA_ARG (Marker, UINTN);\r | |
423 | } else {\r | |
424 | Precision = VA_ARG (Marker, UINTN);\r | |
425 | }\r | |
426 | break;\r | |
427 | case '0':\r | |
428 | if ((Flags & PRECISION) == 0) {\r | |
429 | Flags |= PREFIX_ZERO;\r | |
430 | }\r | |
431 | case '1':\r | |
432 | case '2':\r | |
433 | case '3':\r | |
434 | case '4':\r | |
435 | case '5':\r | |
436 | case '6':\r | |
437 | case '7':\r | |
438 | case '8':\r | |
439 | case '9':\r | |
440 | for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){\r | |
441 | Count = (Count * 10) + FormatCharacter - '0';\r | |
442 | Format += BytesPerFormatCharacter;\r | |
443 | FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r | |
444 | }\r | |
445 | Format -= BytesPerFormatCharacter;\r | |
446 | if ((Flags & PRECISION) == 0) {\r | |
447 | Flags |= PAD_TO_WIDTH;\r | |
448 | Width = Count;\r | |
449 | } else {\r | |
450 | Precision = Count;\r | |
451 | }\r | |
452 | break;\r | |
453 | \r | |
454 | case '\0':\r | |
455 | //\r | |
456 | // Make no output if Format string terminates unexpectedly when\r | |
457 | // looking up for flag, width, precision and type. \r | |
458 | //\r | |
459 | Format -= BytesPerFormatCharacter;\r | |
460 | Precision = 0;\r | |
461 | //\r | |
462 | // break skipped on purpose.\r | |
463 | //\r | |
464 | default:\r | |
465 | Done = TRUE;\r | |
466 | break;\r | |
467 | }\r | |
468 | } \r | |
469 | \r | |
470 | //\r | |
471 | // Handle each argument type\r | |
472 | //\r | |
473 | switch (FormatCharacter) {\r | |
474 | case 'p':\r | |
475 | //\r | |
476 | // Flag space, +, 0, L & l are invalid for type p.\r | |
477 | //\r | |
478 | Flags &= ~(PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE);\r | |
479 | if (sizeof (VOID *) > 4) {\r | |
480 | Flags |= LONG_TYPE;\r | |
481 | }\r | |
482 | case 'X':\r | |
483 | Flags |= PREFIX_ZERO;\r | |
484 | //\r | |
485 | // break skipped on purpose\r | |
486 | //\r | |
487 | case 'x':\r | |
488 | Flags |= RADIX_HEX;\r | |
489 | //\r | |
490 | // break skipped on purpose\r | |
491 | //\r | |
492 | case 'd':\r | |
493 | if ((Flags & LONG_TYPE) == 0) {\r | |
494 | Value = (VA_ARG (Marker, int));\r | |
495 | } else {\r | |
496 | Value = VA_ARG (Marker, INT64);\r | |
497 | }\r | |
498 | if ((Flags & PREFIX_BLANK) != 0) {\r | |
499 | Prefix = ' ';\r | |
500 | }\r | |
501 | if ((Flags & PREFIX_SIGN) != 0) {\r | |
502 | Prefix = '+';\r | |
503 | }\r | |
504 | if ((Flags & COMMA_TYPE) != 0) {\r | |
505 | Comma = TRUE;\r | |
506 | }\r | |
507 | if ((Flags & RADIX_HEX) == 0) {\r | |
508 | Radix = 10;\r | |
509 | if (Comma) {\r | |
510 | Flags &= (~PREFIX_ZERO);\r | |
511 | Precision = 1;\r | |
512 | }\r | |
513 | if (Value < 0) {\r | |
514 | Flags |= PREFIX_SIGN;\r | |
515 | Prefix = '-';\r | |
516 | Value = -Value;\r | |
517 | }\r | |
518 | } else {\r | |
519 | Radix = 16;\r | |
520 | Comma = FALSE;\r | |
521 | if ((Flags & LONG_TYPE) == 0 && Value < 0) {\r | |
522 | Value = (unsigned int)Value;\r | |
523 | }\r | |
524 | }\r | |
525 | //\r | |
526 | // Convert Value to a reversed string\r | |
527 | //\r | |
528 | Count = BasePrintLibValueToString (ValueBuffer, Value, Radix);\r | |
529 | if (Value == 0 && Precision == 0) {\r | |
530 | Count = 0;\r | |
531 | }\r | |
532 | ArgumentString = (CHAR8 *)ValueBuffer + Count;\r | |
533 | \r | |
534 | Digits = Count % 3;\r | |
535 | if (Digits != 0) {\r | |
536 | Digits = 3 - Digits;\r | |
537 | }\r | |
538 | if (Comma && Count != 0) {\r | |
539 | Count += ((Count - 1) / 3);\r | |
540 | }\r | |
541 | if (Prefix != 0) {\r | |
542 | Count++;\r | |
543 | Precision++;\r | |
544 | }\r | |
545 | Flags |= ARGUMENT_REVERSED;\r | |
546 | ZeroPad = TRUE;\r | |
547 | if ((Flags & PREFIX_ZERO) != 0) {\r | |
548 | if ((Flags & LEFT_JUSTIFY) == 0) {\r | |
549 | if ((Flags & PAD_TO_WIDTH) != 0) {\r | |
550 | if ((Flags & PRECISION) == 0) {\r | |
551 | Precision = Width;\r | |
552 | }\r | |
553 | }\r | |
554 | }\r | |
555 | }\r | |
556 | break;\r | |
557 | \r | |
558 | case 's':\r | |
559 | case 'S':\r | |
560 | Flags |= ARGUMENT_UNICODE;\r | |
561 | //\r | |
562 | // break skipped on purpose\r | |
563 | //\r | |
564 | case 'a':\r | |
565 | ArgumentString = (CHAR8 *)VA_ARG (Marker, CHAR8 *);\r | |
566 | if (ArgumentString == NULL) {\r | |
567 | Flags &= (~ARGUMENT_UNICODE);\r | |
568 | ArgumentString = "<null string>";\r | |
569 | }\r | |
570 | //\r | |
571 | // Set the default precision for string to be zero if not specified.\r | |
572 | //\r | |
573 | if ((Flags & PRECISION) == 0) {\r | |
574 | Precision = 0;\r | |
575 | }\r | |
576 | break;\r | |
577 | \r | |
578 | case 'c':\r | |
579 | Character = VA_ARG (Marker, UINTN) & 0xffff;\r | |
580 | ArgumentString = (CHAR8 *)&Character;\r | |
581 | Flags |= ARGUMENT_UNICODE;\r | |
582 | break;\r | |
583 | \r | |
584 | case 'g':\r | |
585 | TmpGuid = VA_ARG (Marker, GUID *);\r | |
586 | if (TmpGuid == NULL) {\r | |
587 | ArgumentString = "<null guid>";\r | |
588 | } else {\r | |
589 | BasePrintLibSPrint (\r | |
590 | ValueBuffer,\r | |
591 | MAXIMUM_VALUE_CHARACTERS, \r | |
592 | 0,\r | |
593 | "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r | |
594 | TmpGuid->Data1,\r | |
595 | TmpGuid->Data2,\r | |
596 | TmpGuid->Data3,\r | |
597 | TmpGuid->Data4[0],\r | |
598 | TmpGuid->Data4[1],\r | |
599 | TmpGuid->Data4[2],\r | |
600 | TmpGuid->Data4[3],\r | |
601 | TmpGuid->Data4[4],\r | |
602 | TmpGuid->Data4[5],\r | |
603 | TmpGuid->Data4[6],\r | |
604 | TmpGuid->Data4[7]\r | |
605 | );\r | |
606 | ArgumentString = ValueBuffer;\r | |
607 | }\r | |
608 | break;\r | |
609 | \r | |
610 | case 't':\r | |
611 | TmpTime = VA_ARG (Marker, TIME *); \r | |
612 | if (TmpTime == NULL) {\r | |
613 | ArgumentString = "<null time>";\r | |
614 | } else {\r | |
615 | BasePrintLibSPrint (\r | |
616 | ValueBuffer,\r | |
617 | MAXIMUM_VALUE_CHARACTERS,\r | |
618 | 0,\r | |
619 | "%02d/%02d/%04d %02d:%02d",\r | |
620 | TmpTime->Month,\r | |
621 | TmpTime->Day,\r | |
622 | TmpTime->Year,\r | |
623 | TmpTime->Hour,\r | |
624 | TmpTime->Minute\r | |
625 | );\r | |
626 | ArgumentString = ValueBuffer;\r | |
627 | }\r | |
628 | break;\r | |
629 | \r | |
630 | case 'r':\r | |
631 | Status = VA_ARG (Marker, RETURN_STATUS);\r | |
632 | ArgumentString = ValueBuffer;\r | |
633 | if (RETURN_ERROR (Status)) {\r | |
634 | //\r | |
635 | // Clear error bit\r | |
636 | //\r | |
637 | Index = Status & ~MAX_BIT;\r | |
638 | if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {\r | |
639 | ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];\r | |
640 | }\r | |
641 | } else {\r | |
642 | Index = Status;\r | |
643 | if (Index <= WARNING_STATUS_NUMBER) {\r | |
644 | ArgumentString = mStatusString [Index];\r | |
645 | }\r | |
646 | }\r | |
647 | if (ArgumentString == ValueBuffer) {\r | |
648 | BasePrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);\r | |
649 | }\r | |
650 | break;\r | |
651 | \r | |
652 | case '\n':\r | |
653 | ArgumentString = "\n\r";\r | |
654 | break;\r | |
655 | \r | |
656 | case '%':\r | |
657 | default:\r | |
658 | //\r | |
659 | // if the type is '%' or unknown, then print it to the screen\r | |
660 | //\r | |
661 | ArgumentString = (CHAR8 *)&FormatCharacter;\r | |
662 | Flags |= ARGUMENT_UNICODE;\r | |
663 | break;\r | |
664 | }\r | |
665 | break;\r | |
666 | \r | |
667 | case '\n':\r | |
668 | ArgumentString = "\n\r";\r | |
669 | break;\r | |
670 | \r | |
671 | default:\r | |
672 | ArgumentString = (CHAR8 *)&FormatCharacter;\r | |
673 | Flags |= ARGUMENT_UNICODE;\r | |
674 | break;\r | |
675 | }\r | |
676 | \r | |
677 | //\r | |
678 | // Retrieve the ArgumentString attriubutes\r | |
679 | //\r | |
680 | if ((Flags & ARGUMENT_UNICODE) != 0) {\r | |
681 | ArgumentMask = 0xffff;\r | |
682 | BytesPerArgumentCharacter = 2;\r | |
683 | } else {\r | |
684 | ArgumentMask = 0xff;\r | |
685 | BytesPerArgumentCharacter = 1;\r | |
686 | }\r | |
687 | if ((Flags & ARGUMENT_REVERSED) != 0) {\r | |
688 | BytesPerArgumentCharacter = -BytesPerArgumentCharacter;\r | |
689 | } else {\r | |
690 | //\r | |
691 | // Compute the number of characters in ArgumentString and store it in Count\r | |
692 | // ArgumentString is either null-terminated, or it contains Precision characters\r | |
693 | //\r | |
694 | for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {\r | |
695 | ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;\r | |
696 | if (ArgumentCharacter == 0) {\r | |
697 | break;\r | |
698 | }\r | |
699 | }\r | |
700 | }\r | |
701 | \r | |
702 | if (Precision < Count) {\r | |
703 | Precision = Count;\r | |
704 | }\r | |
705 | \r | |
706 | //\r | |
707 | // Pad before the string\r | |
708 | //\r | |
709 | if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {\r | |
710 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r | |
711 | }\r | |
712 | \r | |
713 | if (ZeroPad) {\r | |
714 | if (Prefix != 0) {\r | |
715 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r | |
716 | }\r | |
717 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);\r | |
718 | } else {\r | |
719 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);\r | |
720 | if (Prefix != 0) {\r | |
721 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r | |
722 | }\r | |
723 | }\r | |
724 | \r | |
725 | //\r | |
726 | // Output the Prefix character if it is present\r | |
727 | //\r | |
728 | Index = 0;\r | |
729 | if (Prefix != 0) {\r | |
730 | Index++;\r | |
731 | }\r | |
732 | \r | |
733 | //\r | |
734 | // Copy the string into the output buffer performing the required type conversions\r | |
735 | //\r | |
736 | while (Index < Count) {\r | |
737 | ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;\r | |
738 | \r | |
739 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);\r | |
740 | ArgumentString += BytesPerArgumentCharacter;\r | |
741 | Index++;\r | |
742 | if (Comma) {\r | |
743 | Digits++;\r | |
744 | if (Digits == 3) {\r | |
745 | Digits = 0;\r | |
746 | Index++;\r | |
747 | if (Index < Count) {\r | |
748 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);\r | |
749 | }\r | |
750 | }\r | |
751 | }\r | |
752 | }\r | |
753 | \r | |
754 | //\r | |
755 | // Pad after the string\r | |
756 | //\r | |
757 | if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {\r | |
758 | Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r | |
759 | }\r | |
760 | \r | |
761 | //\r | |
762 | // Get the next character from the format string\r | |
763 | //\r | |
764 | Format += BytesPerFormatCharacter;\r | |
765 | \r | |
766 | //\r | |
767 | // Get the next character from the format string\r | |
768 | //\r | |
769 | FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r | |
770 | }\r | |
771 | \r | |
772 | //\r | |
773 | // Null terminate the Unicode or ASCII string\r | |
774 | //\r | |
775 | BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);\r | |
776 | //\r | |
777 | // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength\r | |
778 | // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r | |
779 | //\r | |
780 | ASSERT ((((Flags & OUTPUT_UNICODE) == 0)) || (StrSize ((CHAR16 *) OriginalBuffer) != 0));\r | |
781 | //\r | |
782 | // Make sure output buffer cannot contain more than PcdMaximumAsciiStringLength\r | |
783 | // ASCII characters if PcdMaximumAsciiStringLength is not zero. \r | |
784 | //\r | |
785 | ASSERT ((((Flags & OUTPUT_UNICODE) != 0)) || (AsciiStrSize (OriginalBuffer) != 0));\r | |
786 | \r | |
787 | return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);\r | |
788 | }\r | |
789 | \r | |
790 | /**\r | |
791 | Worker function that produces a Null-terminated string in an output buffer \r | |
792 | based on a Null-terminated format string and variable argument list.\r | |
793 | \r | |
794 | VSPrint function to process format and place the results in Buffer. Since a \r | |
795 | VA_LIST is used this rountine allows the nesting of Vararg routines. Thus \r | |
efb23117 | 796 | this is the main print working routine\r |
e389c39b | 797 | \r |
798 | @param StartOfBuffer Character buffer to print the results of the parsing\r | |
799 | of Format into.\r | |
800 | @param BufferSize Maximum number of characters to put into buffer.\r | |
801 | Zero means no limit.\r | |
802 | @param Flags Intial flags value.\r | |
803 | Can only have FORMAT_UNICODE and OUTPUT_UNICODE set\r | |
804 | @param FormatString Null-terminated format string.\r | |
805 | @param ... The variable argument list.\r | |
806 | \r | |
efb23117 | 807 | @return Number of characters printed.\r |
e389c39b | 808 | \r |
809 | **/\r | |
810 | UINTN\r | |
811 | BasePrintLibSPrint (\r | |
812 | OUT CHAR8 *StartOfBuffer,\r | |
813 | IN UINTN BufferSize,\r | |
814 | IN UINTN Flags,\r | |
815 | IN CONST CHAR8 *FormatString,\r | |
816 | ...\r | |
817 | )\r | |
818 | {\r | |
819 | VA_LIST Marker;\r | |
820 | \r | |
821 | VA_START (Marker, FormatString);\r | |
822 | return BasePrintLibVSPrint (StartOfBuffer, BufferSize, Flags, FormatString, Marker);\r | |
823 | }\r |