]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BasePrintLib/PrintLibInternal.c
MdePkg/BasePrintLib: Avoid reading content beyond the format string
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLibInternal.c
CommitLineData
e1f414b6 1/** @file\r
eceb3a4c 2 Print Library internal worker functions.\r
e1f414b6 3\r
9b002aa4 4 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
19388d29 5 This program and the accompanying materials\r
e1f414b6 6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
2fc59a00 8 http://opensource.org/licenses/bsd-license.php.\r
e1f414b6 9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
e1f414b6 13**/\r
14\r
e1f414b6 15#include "PrintLibInternal.h"\r
16\r
0df625fa
LG
17#define WARNING_STATUS_NUMBER 5\r
18#define ERROR_STATUS_NUMBER 33\r
e389c39b 19\r
9b002aa4
HW
20//\r
21// Safe print checks\r
22//\r
23#define RSIZE_MAX (PcdGet32 (PcdMaximumUnicodeStringLength))\r
24#define ASCII_RSIZE_MAX (PcdGet32 (PcdMaximumAsciiStringLength))\r
25\r
26#define SAFE_PRINT_CONSTRAINT_CHECK(Expression, RetVal) \\r
27 do { \\r
28 ASSERT (Expression); \\r
29 if (!(Expression)) { \\r
30 return RetVal; \\r
31 } \\r
32 } while (FALSE)\r
33\r
e1f414b6 34GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};\r
35\r
d3265fe1 36GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 * CONST mStatusString[] = {\r
e389c39b 37 "Success", // RETURN_SUCCESS = 0\r
38 "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH = 1\r
39 "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE = 2\r
40 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE = 3\r
41 "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL = 4\r
0df625fa 42 "Warning Stale Data", // RETURN_WARN_STALE_DATA = 5\r
e389c39b 43 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT\r
44 "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT\r
45 "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT\r
46 "Bad Buffer Size", // RETURN_BAD_BUFFER_SIZE = 4 | MAX_BIT\r
47 "Buffer Too Small", // RETURN_BUFFER_TOO_SMALL, = 5 | MAX_BIT\r
48 "Not Ready", // RETURN_NOT_READY = 6 | MAX_BIT\r
49 "Device Error", // RETURN_DEVICE_ERROR = 7 | MAX_BIT\r
50 "Write Protected", // RETURN_WRITE_PROTECTED = 8 | MAX_BIT\r
51 "Out of Resources", // RETURN_OUT_OF_RESOURCES = 9 | MAX_BIT\r
52 "Volume Corrupt", // RETURN_VOLUME_CORRUPTED = 10 | MAX_BIT\r
53 "Volume Full", // RETURN_VOLUME_FULL = 11 | MAX_BIT\r
54 "No Media", // RETURN_NO_MEDIA = 12 | MAX_BIT\r
55 "Media changed", // RETURN_MEDIA_CHANGED = 13 | MAX_BIT\r
56 "Not Found", // RETURN_NOT_FOUND = 14 | MAX_BIT\r
57 "Access Denied", // RETURN_ACCESS_DENIED = 15 | MAX_BIT\r
58 "No Response", // RETURN_NO_RESPONSE = 16 | MAX_BIT\r
59 "No mapping", // RETURN_NO_MAPPING = 17 | MAX_BIT\r
60 "Time out", // RETURN_TIMEOUT = 18 | MAX_BIT\r
61 "Not started", // RETURN_NOT_STARTED = 19 | MAX_BIT\r
62 "Already started", // RETURN_ALREADY_STARTED = 20 | MAX_BIT\r
63 "Aborted", // RETURN_ABORTED = 21 | MAX_BIT\r
64 "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT\r
65 "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT\r
5e2fd937 66 "Protocol Error", // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT\r
0df625fa
LG
67 "Incompatible Version", // RETURN_INCOMPATIBLE_VERSION = 25 | MAX_BIT\r
68 "Security Violation", // RETURN_SECURITY_VIOLATION = 26 | MAX_BIT\r
69 "CRC Error", // RETURN_CRC_ERROR = 27 | MAX_BIT\r
70 "End of Media", // RETURN_END_OF_MEDIA = 28 | MAX_BIT\r
71 "Reserved (29)", // RESERVED = 29 | MAX_BIT\r
72 "Reserved (30)", // RESERVED = 30 | MAX_BIT\r
73 "End of File", // RETURN_END_OF_FILE = 31 | MAX_BIT\r
74 "Invalid Language", // RETURN_INVALID_LANGUAGE = 32 | MAX_BIT\r
75 "Compromised Data" // RETURN_COMPROMISED_DATA = 33 | MAX_BIT\r
e389c39b 76};\r
77\r
e1f414b6 78\r
79/**\r
80 Internal function that places the character into the Buffer.\r
81\r
82 Internal function that places ASCII or Unicode character into the Buffer.\r
83\r
2fc59a00 84 @param Buffer The buffer to place the Unicode or ASCII string.\r
e1f414b6 85 @param EndBuffer The end of the input Buffer. No characters will be\r
86 placed after that. \r
2fc59a00 87 @param Length The count of character to be placed into Buffer.\r
a2bc29c4 88 (Negative value indicates no buffer fill.)\r
2fc59a00 89 @param Character The character to be placed into Buffer.\r
90 @param Increment The character increment in Buffer.\r
e1f414b6 91\r
2fc59a00 92 @return Buffer.\r
e1f414b6 93\r
94**/\r
95CHAR8 *\r
96BasePrintLibFillBuffer (\r
eceb3a4c
LG
97 OUT CHAR8 *Buffer,\r
98 IN CHAR8 *EndBuffer,\r
99 IN INTN Length,\r
100 IN UINTN Character,\r
101 IN INTN Increment\r
e1f414b6 102 )\r
103{\r
a2bc29c4 104 INTN Index;\r
bcccf0b0 105 \r
a2bc29c4 106 for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {\r
107 *Buffer = (CHAR8) Character;\r
108 if (Increment != 1) {\r
109 *(Buffer + 1) = (CHAR8)(Character >> 8);\r
110 }\r
111 Buffer += Increment;\r
112 }\r
113\r
114 return Buffer;\r
e1f414b6 115}\r
116\r
117/**\r
7c905091 118 Internal function that convert a number to a string in Buffer.\r
e1f414b6 119\r
7c905091 120 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.\r
e1f414b6 121\r
7c905091 122 @param Buffer Location to place the ASCII string of Value.\r
2fc59a00 123 @param Value The value to convert to a Decimal or Hexadecimal string in Buffer.\r
e1f414b6 124 @param Radix Radix of the value\r
125\r
7c905091 126 @return A pointer to the end of buffer filled with ASCII string.\r
e1f414b6 127\r
128**/\r
7c905091 129CHAR8 *\r
e1f414b6 130BasePrintLibValueToString (\r
efb23117 131 IN OUT CHAR8 *Buffer, \r
132 IN INT64 Value, \r
e1f414b6 133 IN UINTN Radix\r
134 )\r
135{\r
e1f414b6 136 UINT32 Remainder;\r
137\r
138 //\r
139 // Loop to convert one digit at a time in reverse order\r
140 //\r
7c905091 141 *Buffer = 0;\r
e1f414b6 142 do {\r
143 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r
7c905091 144 *(++Buffer) = mHexStr[Remainder];\r
e1f414b6 145 } while (Value != 0);\r
eceb3a4c
LG
146\r
147 //\r
7c905091 148 // Return pointer of the end of filled buffer.\r
eceb3a4c 149 //\r
7c905091 150 return Buffer;\r
e1f414b6 151}\r
152\r
153/**\r
154 Internal function that converts a decimal value to a Null-terminated string.\r
155 \r
156 Converts the decimal number specified by Value to a Null-terminated \r
157 string specified by Buffer containing at most Width characters.\r
158 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
efb23117 159 The total number of characters placed in Buffer is returned.\r
e1f414b6 160 If the conversion contains more than Width characters, then only the first\r
161 Width characters are returned, and the total number of characters \r
162 required to perform the conversion is returned.\r
163 Additional conversion parameters are specified in Flags. \r
164 The Flags bit LEFT_JUSTIFY is always ignored.\r
165 All conversions are left justified in Buffer.\r
166 If Width is 0, PREFIX_ZERO is ignored in Flags.\r
167 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r
168 are inserted every 3rd digit starting from the right.\r
efb23117 169 If Value is < 0, then the fist character in Buffer is a '-'.\r
e1f414b6 170 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r
171 then Buffer is padded with '0' characters so the combination of the optional '-' \r
172 sign character, '0' characters, digit characters for Value, and the Null-terminator\r
173 add up to Width characters.\r
e1f414b6 174\r
175 If Buffer is NULL, then ASSERT().\r
176 If unsupported bits are set in Flags, then ASSERT().\r
177 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r
178\r
2fc59a00 179 @param Buffer The pointer to the output buffer for the produced Null-terminated\r
e1f414b6 180 string.\r
181 @param Flags The bitmask of flags that specify left justification, zero pad,\r
182 and commas.\r
183 @param Value The 64-bit signed value to convert to a string.\r
184 @param Width The maximum number of characters to place in Buffer, not including\r
185 the Null-terminator.\r
2fc59a00 186 @param Increment The character increment in Buffer.\r
e1f414b6 187 \r
efb23117 188 @return Total number of characters required to perform the conversion.\r
e1f414b6 189\r
190**/\r
191UINTN\r
192BasePrintLibConvertValueToString (\r
193 IN OUT CHAR8 *Buffer,\r
194 IN UINTN Flags,\r
195 IN INT64 Value,\r
196 IN UINTN Width,\r
197 IN UINTN Increment\r
198 )\r
199{\r
200 CHAR8 *OriginalBuffer;\r
201 CHAR8 *EndBuffer;\r
202 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
7c905091 203 CHAR8 *ValueBufferPtr;\r
e1f414b6 204 UINTN Count;\r
205 UINTN Digits;\r
206 UINTN Index;\r
207 UINTN Radix;\r
208\r
eceb3a4c
LG
209 //\r
210 // Make sure Buffer is not NULL and Width < MAXIMUM\r
211 //\r
e1f414b6 212 ASSERT (Buffer != NULL);\r
213 ASSERT (Width < MAXIMUM_VALUE_CHARACTERS);\r
214 //\r
215 // Make sure Flags can only contain supported bits.\r
216 //\r
217 ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0);\r
218\r
219 //\r
df8d0595 220 // If both COMMA_TYPE and RADIX_HEX are set, then ASSERT ()\r
e1f414b6 221 //\r
841c0770 222 ASSERT (((Flags & COMMA_TYPE) == 0) || ((Flags & RADIX_HEX) == 0));\r
e1f414b6 223\r
224 OriginalBuffer = Buffer;\r
eceb3a4c
LG
225 \r
226 //\r
227 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.\r
228 //\r
e1f414b6 229 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r
e935092f 230 Flags &= ~((UINTN) PREFIX_ZERO);\r
e1f414b6 231 }\r
eceb3a4c
LG
232 //\r
233 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
234 //\r
e1f414b6 235 if (Width == 0) {\r
236 Width = MAXIMUM_VALUE_CHARACTERS - 1;\r
237 }\r
238 //\r
239 // Set the tag for the end of the input Buffer.\r
240 //\r
241 EndBuffer = Buffer + Width * Increment;\r
eceb3a4c
LG
242 \r
243 //\r
244 // Convert decimal negative\r
245 //\r
e1f414b6 246 if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {\r
247 Value = -Value;\r
248 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);\r
249 Width--;\r
250 }\r
eceb3a4c
LG
251 \r
252 //\r
253 // Count the length of the value string.\r
254 //\r
e1f414b6 255 Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;\r
7c905091 256 ValueBufferPtr = BasePrintLibValueToString (ValueBuffer, Value, Radix);\r
257 Count = ValueBufferPtr - ValueBuffer;\r
eceb3a4c
LG
258 \r
259 //\r
260 // Append Zero\r
261 //\r
e1f414b6 262 if ((Flags & PREFIX_ZERO) != 0) {\r
263 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);\r
264 }\r
eceb3a4c
LG
265 \r
266 //\r
267 // Print Comma type for every 3 characters\r
268 //\r
e1f414b6 269 Digits = Count % 3;\r
270 if (Digits != 0) {\r
271 Digits = 3 - Digits;\r
272 }\r
273 for (Index = 0; Index < Count; Index++) {\r
7c905091 274 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, *ValueBufferPtr--, Increment);\r
e1f414b6 275 if ((Flags & COMMA_TYPE) != 0) {\r
276 Digits++;\r
277 if (Digits == 3) {\r
278 Digits = 0;\r
279 if ((Index + 1) < Count) {\r
280 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);\r
281 }\r
282 }\r
283 }\r
284 }\r
eceb3a4c
LG
285 \r
286 //\r
287 // Print Null-terminator\r
288 //\r
e1f414b6 289 BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);\r
290\r
291 return ((Buffer - OriginalBuffer) / Increment);\r
292}\r
293\r
51f0ceb4
HW
294/**\r
295 Internal function that converts a decimal value to a Null-terminated string.\r
296\r
297 Converts the decimal number specified by Value to a Null-terminated string\r
298 specified by Buffer containing at most Width characters. If Width is 0 then a\r
299 width of MAXIMUM_VALUE_CHARACTERS is assumed. If the conversion contains more\r
300 than Width characters, then only the first Width characters are placed in\r
301 Buffer. Additional conversion parameters are specified in Flags.\r
302 The Flags bit LEFT_JUSTIFY is always ignored.\r
303 All conversions are left justified in Buffer.\r
304 If Width is 0, PREFIX_ZERO is ignored in Flags.\r
305 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and\r
306 commas are inserted every 3rd digit starting from the right.\r
307 If Value is < 0, then the fist character in Buffer is a '-'.\r
308 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,\r
309 then Buffer is padded with '0' characters so the combination of the optional\r
310 '-' sign character, '0' characters, digit characters for Value, and the\r
311 Null-terminator add up to Width characters.\r
312\r
313 If an error would be returned, the function will ASSERT().\r
314\r
315 @param Buffer The pointer to the output buffer for the produced\r
316 Null-terminated string.\r
317 @param BufferSize The size of Buffer in bytes, including the\r
318 Null-terminator.\r
319 @param Flags The bitmask of flags that specify left justification,\r
320 zero pad, and commas.\r
321 @param Value The 64-bit signed value to convert to a string.\r
322 @param Width The maximum number of characters to place in Buffer,\r
323 not including the Null-terminator.\r
324 @param Increment The character increment in Buffer.\r
325\r
326 @retval RETURN_SUCCESS The decimal value is converted.\r
327 @retval RETURN_BUFFER_TOO_SMALL If BufferSize cannot hold the converted\r
328 value.\r
329 @retval RETURN_INVALID_PARAMETER If Buffer is NULL.\r
330 If Increment is 1 and\r
331 PcdMaximumAsciiStringLength is not zero,\r
332 BufferSize is greater than\r
333 PcdMaximumAsciiStringLength.\r
334 If Increment is not 1 and\r
335 PcdMaximumUnicodeStringLength is not zero,\r
336 BufferSize is greater than\r
337 (PcdMaximumUnicodeStringLength *\r
338 sizeof (CHAR16) + 1).\r
339 If unsupported bits are set in Flags.\r
340 If both COMMA_TYPE and RADIX_HEX are set in\r
341 Flags.\r
342 If Width >= MAXIMUM_VALUE_CHARACTERS.\r
343\r
344**/\r
345RETURN_STATUS\r
346BasePrintLibConvertValueToStringS (\r
347 IN OUT CHAR8 *Buffer,\r
348 IN UINTN BufferSize,\r
349 IN UINTN Flags,\r
350 IN INT64 Value,\r
351 IN UINTN Width,\r
352 IN UINTN Increment\r
353 )\r
354{\r
355 CHAR8 *EndBuffer;\r
356 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
357 CHAR8 *ValueBufferPtr;\r
358 UINTN Count;\r
359 UINTN Digits;\r
360 UINTN Index;\r
361 UINTN Radix;\r
362\r
363 //\r
364 // 1. Buffer shall not be a null pointer.\r
365 //\r
366 SAFE_PRINT_CONSTRAINT_CHECK ((Buffer != NULL), RETURN_INVALID_PARAMETER);\r
367\r
368 //\r
369 // 2. BufferSize shall not be greater than (RSIZE_MAX * sizeof (CHAR16)) for\r
370 // Unicode output string or shall not be greater than ASCII_RSIZE_MAX for\r
371 // Ascii output string.\r
372 //\r
373 if (Increment == 1) {\r
374 //\r
375 // Ascii output string\r
376 //\r
377 if (ASCII_RSIZE_MAX != 0) {\r
378 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);\r
379 }\r
380 } else {\r
381 //\r
382 // Unicode output string\r
383 //\r
384 if (RSIZE_MAX != 0) {\r
385 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= RSIZE_MAX * sizeof (CHAR16) + 1), RETURN_INVALID_PARAMETER);\r
386 }\r
387 }\r
388\r
389 //\r
390 // 3. Flags shall be set properly.\r
391 //\r
392 SAFE_PRINT_CONSTRAINT_CHECK (((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0), RETURN_INVALID_PARAMETER);\r
393 SAFE_PRINT_CONSTRAINT_CHECK ((((Flags & COMMA_TYPE) == 0) || ((Flags & RADIX_HEX) == 0)), RETURN_INVALID_PARAMETER);\r
394\r
395 //\r
396 // 4. Width shall be smaller than MAXIMUM_VALUE_CHARACTERS.\r
397 //\r
398 SAFE_PRINT_CONSTRAINT_CHECK ((Width < MAXIMUM_VALUE_CHARACTERS), RETURN_INVALID_PARAMETER);\r
399\r
400 //\r
401 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.\r
402 //\r
403 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r
404 Flags &= ~((UINTN) PREFIX_ZERO);\r
405 }\r
406 //\r
407 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
408 //\r
409 if (Width == 0) {\r
410 Width = MAXIMUM_VALUE_CHARACTERS - 1;\r
411 }\r
412\r
413 //\r
414 // Count the characters of the output string.\r
415 //\r
416 Count = 0;\r
417 Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;\r
418\r
419 if ((Flags & PREFIX_ZERO) != 0) {\r
420 Count = Width;\r
421 } else {\r
422 if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {\r
423 Count++; // minus sign\r
424 ValueBufferPtr = BasePrintLibValueToString (ValueBuffer, -Value, Radix);\r
425 } else {\r
426 ValueBufferPtr = BasePrintLibValueToString (ValueBuffer, Value, Radix);\r
427 }\r
428 Digits = ValueBufferPtr - ValueBuffer;\r
429 Count += Digits;\r
430\r
431 if ((Flags & COMMA_TYPE) != 0) {\r
432 Count += (Digits - 1) / 3; // commas\r
433 }\r
434 }\r
435\r
436 Width = MIN (Count, Width);\r
437\r
438 //\r
439 // 5. BufferSize shall be large enough to hold the converted string.\r
440 //\r
441 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize >= (Width + 1) * Increment), RETURN_BUFFER_TOO_SMALL);\r
442\r
443 //\r
444 // Set the tag for the end of the input Buffer.\r
445 //\r
446 EndBuffer = Buffer + Width * Increment;\r
447\r
448 //\r
449 // Convert decimal negative\r
450 //\r
451 if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {\r
452 Value = -Value;\r
453 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);\r
454 Width--;\r
455 }\r
456\r
457 //\r
458 // Count the length of the value string.\r
459 //\r
460 ValueBufferPtr = BasePrintLibValueToString (ValueBuffer, Value, Radix);\r
461 Count = ValueBufferPtr - ValueBuffer;\r
462\r
463 //\r
464 // Append Zero\r
465 //\r
466 if ((Flags & PREFIX_ZERO) != 0) {\r
467 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);\r
468 }\r
469\r
470 //\r
471 // Print Comma type for every 3 characters\r
472 //\r
473 Digits = Count % 3;\r
474 if (Digits != 0) {\r
475 Digits = 3 - Digits;\r
476 }\r
477 for (Index = 0; Index < Count; Index++) {\r
478 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, *ValueBufferPtr--, Increment);\r
479 if ((Flags & COMMA_TYPE) != 0) {\r
480 Digits++;\r
481 if (Digits == 3) {\r
482 Digits = 0;\r
483 if ((Index + 1) < Count) {\r
484 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);\r
485 }\r
486 }\r
487 }\r
488 }\r
489\r
490 //\r
491 // Print Null-terminator\r
492 //\r
493 BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);\r
494\r
495 return RETURN_SUCCESS;\r
496}\r
497\r
e389c39b 498/**\r
499 Worker function that produces a Null-terminated string in an output buffer \r
500 based on a Null-terminated format string and a VA_LIST argument list.\r
501\r
502 VSPrint function to process format and place the results in Buffer. Since a \r
7c905091 503 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
e389c39b 504 this is the main print working routine.\r
505\r
f405c067 506 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.\r
507\r
508 @param[out] Buffer The character buffer to print the results of the \r
509 parsing of Format into.\r
510 @param[in] BufferSize The maximum number of characters to put into \r
511 buffer.\r
512 @param[in] Flags Initial flags value.\r
513 Can only have FORMAT_UNICODE, OUTPUT_UNICODE, \r
514 and COUNT_ONLY_NO_PRINT set.\r
515 @param[in] Format A Null-terminated format string.\r
516 @param[in] VaListMarker VA_LIST style variable argument list consumed by\r
517 processing Format.\r
518 @param[in] BaseListMarker BASE_LIST style variable argument list consumed\r
519 by processing Format.\r
e389c39b 520\r
2fc59a00 521 @return The number of characters printed not including the Null-terminator.\r
f405c067 522 If COUNT_ONLY_NO_PRINT was set returns the same, but without any\r
523 modification to Buffer.\r
e389c39b 524\r
525**/\r
526UINTN\r
2075236e 527BasePrintLibSPrintMarker (\r
e389c39b 528 OUT CHAR8 *Buffer,\r
529 IN UINTN BufferSize,\r
530 IN UINTN Flags,\r
531 IN CONST CHAR8 *Format,\r
2075236e 532 IN VA_LIST VaListMarker, OPTIONAL\r
533 IN BASE_LIST BaseListMarker OPTIONAL\r
e389c39b 534 )\r
535{\r
2075236e 536 CHAR8 *OriginalBuffer;\r
537 CHAR8 *EndBuffer;\r
538 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
f405c067 539 UINT32 BytesPerOutputCharacter;\r
2075236e 540 UINTN BytesPerFormatCharacter;\r
541 UINTN FormatMask;\r
542 UINTN FormatCharacter;\r
543 UINTN Width;\r
544 UINTN Precision;\r
545 INT64 Value;\r
546 CONST CHAR8 *ArgumentString;\r
547 UINTN Character;\r
548 GUID *TmpGuid;\r
549 TIME *TmpTime;\r
550 UINTN Count;\r
551 UINTN ArgumentMask;\r
552 INTN BytesPerArgumentCharacter;\r
553 UINTN ArgumentCharacter;\r
554 BOOLEAN Done;\r
555 UINTN Index;\r
556 CHAR8 Prefix;\r
557 BOOLEAN ZeroPad;\r
558 BOOLEAN Comma;\r
559 UINTN Digits;\r
560 UINTN Radix;\r
561 RETURN_STATUS Status;\r
4b0f378c
LG
562 UINT32 GuidData1;\r
563 UINT16 GuidData2;\r
564 UINT16 GuidData3;\r
41342801 565 UINTN LengthToReturn;\r
e389c39b 566\r
f405c067 567 //\r
568 // If you change this code be sure to match the 2 versions of this function.\r
569 // Nearly identical logic is found in the BasePrintLib and \r
570 // DxePrintLibPrint2Protocol (both PrintLib instances).\r
571 //\r
572\r
9b002aa4
HW
573 //\r
574 // 1. Buffer shall not be a null pointer when both BufferSize > 0 and\r
575 // COUNT_ONLY_NO_PRINT is not set in Flags.\r
576 //\r
577 if ((BufferSize > 0) && ((Flags & COUNT_ONLY_NO_PRINT) == 0)) {\r
578 SAFE_PRINT_CONSTRAINT_CHECK ((Buffer != NULL), 0);\r
579 }\r
580\r
581 //\r
582 // 2. Format shall not be a null pointer when BufferSize > 0 or when\r
583 // COUNT_ONLY_NO_PRINT is set in Flags.\r
584 //\r
585 if ((BufferSize > 0) || ((Flags & COUNT_ONLY_NO_PRINT) != 0)) {\r
586 SAFE_PRINT_CONSTRAINT_CHECK ((Format != NULL), 0);\r
587 }\r
588\r
589 //\r
590 // 3. BufferSize shall not be greater than RSIZE_MAX for Unicode output or\r
591 // ASCII_RSIZE_MAX for Ascii output.\r
592 //\r
593 if ((Flags & OUTPUT_UNICODE) != 0) {\r
594 if (RSIZE_MAX != 0) {\r
595 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= RSIZE_MAX), 0);\r
596 }\r
597 BytesPerOutputCharacter = 2;\r
598 } else {\r
599 if (ASCII_RSIZE_MAX != 0) {\r
600 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= ASCII_RSIZE_MAX), 0);\r
601 }\r
602 BytesPerOutputCharacter = 1;\r
603 }\r
604\r
605 //\r
606 // 4. Format shall not contain more than RSIZE_MAX Unicode characters or\r
607 // ASCII_RSIZE_MAX Ascii characters.\r
608 //\r
609 if ((Flags & FORMAT_UNICODE) != 0) {\r
610 if (RSIZE_MAX != 0) {\r
611 SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16 *)Format, RSIZE_MAX + 1) <= RSIZE_MAX), 0);\r
612 }\r
613 BytesPerFormatCharacter = 2;\r
614 FormatMask = 0xffff;\r
615 } else {\r
616 if (ASCII_RSIZE_MAX != 0) {\r
617 SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), 0);\r
618 }\r
619 BytesPerFormatCharacter = 1;\r
620 FormatMask = 0xff;\r
621 }\r
622\r
83f1f16a 623 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r
f405c067 624 if (BufferSize == 0) {\r
625 Buffer = NULL;\r
626 }\r
627 } else {\r
628 //\r
629 // We can run without a Buffer for counting only.\r
630 //\r
631 if (BufferSize == 0) {\r
632 return 0;\r
633 }\r
e389c39b 634 }\r
635\r
f405c067 636 LengthToReturn = 0;\r
28087ef0
HL
637 EndBuffer = NULL;\r
638 OriginalBuffer = NULL;\r
f405c067 639\r
e389c39b 640 //\r
641 // Reserve space for the Null terminator.\r
642 //\r
28087ef0
HL
643 if (Buffer != NULL) {\r
644 BufferSize--;\r
645 OriginalBuffer = Buffer;\r
2075236e 646\r
28087ef0
HL
647 //\r
648 // Set the tag for the end of the input Buffer.\r
649 //\r
650 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;\r
651 }\r
e389c39b 652\r
e389c39b 653 //\r
654 // Get the first character from the format string\r
655 //\r
b1d4b965 656 FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
e389c39b 657\r
658 //\r
659 // Loop until the end of the format string is reached or the output buffer is full\r
660 //\r
28087ef0
HL
661 while (FormatCharacter != 0) {\r
662 if ((Buffer != NULL) && (Buffer >= EndBuffer)) {\r
663 break;\r
664 }\r
e389c39b 665 //\r
666 // Clear all the flag bits except those that may have been passed in\r
667 //\r
e935092f 668 Flags &= (UINTN) (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);\r
e389c39b 669\r
670 //\r
671 // Set the default width to zero, and the default precision to 1\r
672 //\r
673 Width = 0;\r
674 Precision = 1;\r
675 Prefix = 0;\r
676 Comma = FALSE;\r
677 ZeroPad = FALSE;\r
678 Count = 0;\r
679 Digits = 0;\r
680\r
681 switch (FormatCharacter) {\r
682 case '%':\r
683 //\r
684 // Parse Flags and Width\r
685 //\r
686 for (Done = FALSE; !Done; ) {\r
687 Format += BytesPerFormatCharacter;\r
b1d4b965 688 FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
e389c39b 689 switch (FormatCharacter) {\r
690 case '.': \r
691 Flags |= PRECISION; \r
692 break;\r
693 case '-': \r
694 Flags |= LEFT_JUSTIFY; \r
695 break;\r
696 case '+': \r
697 Flags |= PREFIX_SIGN; \r
698 break;\r
699 case ' ': \r
700 Flags |= PREFIX_BLANK; \r
701 break;\r
702 case ',': \r
703 Flags |= COMMA_TYPE; \r
704 break;\r
705 case 'L':\r
706 case 'l': \r
707 Flags |= LONG_TYPE; \r
708 break;\r
709 case '*':\r
710 if ((Flags & PRECISION) == 0) {\r
711 Flags |= PAD_TO_WIDTH;\r
2075236e 712 if (BaseListMarker == NULL) {\r
713 Width = VA_ARG (VaListMarker, UINTN);\r
714 } else {\r
715 Width = BASE_ARG (BaseListMarker, UINTN);\r
716 }\r
e389c39b 717 } else {\r
2075236e 718 if (BaseListMarker == NULL) {\r
719 Precision = VA_ARG (VaListMarker, UINTN);\r
720 } else {\r
721 Precision = BASE_ARG (BaseListMarker, UINTN);\r
722 }\r
e389c39b 723 }\r
724 break;\r
725 case '0':\r
726 if ((Flags & PRECISION) == 0) {\r
727 Flags |= PREFIX_ZERO;\r
728 }\r
729 case '1':\r
730 case '2':\r
731 case '3':\r
732 case '4':\r
733 case '5':\r
734 case '6':\r
735 case '7':\r
736 case '8':\r
737 case '9':\r
738 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){\r
739 Count = (Count * 10) + FormatCharacter - '0';\r
740 Format += BytesPerFormatCharacter;\r
b1d4b965 741 FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
e389c39b 742 }\r
743 Format -= BytesPerFormatCharacter;\r
744 if ((Flags & PRECISION) == 0) {\r
745 Flags |= PAD_TO_WIDTH;\r
746 Width = Count;\r
747 } else {\r
748 Precision = Count;\r
749 }\r
750 break;\r
751 \r
752 case '\0':\r
753 //\r
754 // Make no output if Format string terminates unexpectedly when\r
755 // looking up for flag, width, precision and type. \r
756 //\r
757 Format -= BytesPerFormatCharacter;\r
758 Precision = 0;\r
759 //\r
760 // break skipped on purpose.\r
761 //\r
762 default:\r
763 Done = TRUE;\r
764 break;\r
765 }\r
766 } \r
767\r
768 //\r
769 // Handle each argument type\r
770 //\r
771 switch (FormatCharacter) {\r
772 case 'p':\r
773 //\r
774 // Flag space, +, 0, L & l are invalid for type p.\r
775 //\r
e935092f 776 Flags &= ~((UINTN) (PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE));\r
e389c39b 777 if (sizeof (VOID *) > 4) {\r
778 Flags |= LONG_TYPE;\r
779 }\r
d6bee311
ED
780 //\r
781 // break skipped on purpose\r
782 //\r
e389c39b 783 case 'X':\r
784 Flags |= PREFIX_ZERO;\r
785 //\r
786 // break skipped on purpose\r
787 //\r
788 case 'x':\r
789 Flags |= RADIX_HEX;\r
790 //\r
791 // break skipped on purpose\r
792 //\r
9ade4339
QS
793 case 'u':\r
794 if ((Flags & RADIX_HEX) == 0) {\r
795 Flags &= ~((UINTN) (PREFIX_SIGN));\r
796 Flags |= UNSIGNED_TYPE;\r
797 }\r
798 //\r
799 // break skipped on purpose\r
800 //\r
e389c39b 801 case 'd':\r
802 if ((Flags & LONG_TYPE) == 0) {\r
57dd2382 803 //\r
9ade4339 804 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
18e81d77 805 // This assumption is made so the format string definition is compatible with the ANSI C\r
57dd2382 806 // Specification for formatted strings. It is recommended that the Base Types be used \r
807 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
808 // provides an implementation that is compatible with that largest possible set of CPU \r
809 // architectures. This is why the type "int" is used in this one case.\r
810 //\r
2075236e 811 if (BaseListMarker == NULL) {\r
812 Value = VA_ARG (VaListMarker, int);\r
813 } else {\r
814 Value = BASE_ARG (BaseListMarker, int);\r
815 }\r
e389c39b 816 } else {\r
2075236e 817 if (BaseListMarker == NULL) {\r
818 Value = VA_ARG (VaListMarker, INT64);\r
819 } else {\r
820 Value = BASE_ARG (BaseListMarker, INT64);\r
821 }\r
e389c39b 822 }\r
823 if ((Flags & PREFIX_BLANK) != 0) {\r
824 Prefix = ' ';\r
825 }\r
826 if ((Flags & PREFIX_SIGN) != 0) {\r
827 Prefix = '+';\r
828 }\r
829 if ((Flags & COMMA_TYPE) != 0) {\r
830 Comma = TRUE;\r
831 }\r
832 if ((Flags & RADIX_HEX) == 0) {\r
833 Radix = 10;\r
834 if (Comma) {\r
e935092f 835 Flags &= ~((UINTN) PREFIX_ZERO);\r
e389c39b 836 Precision = 1;\r
837 }\r
9ade4339 838 if (Value < 0 && (Flags & UNSIGNED_TYPE) == 0) {\r
e389c39b 839 Flags |= PREFIX_SIGN;\r
840 Prefix = '-';\r
841 Value = -Value;\r
9ade4339
QS
842 } else if ((Flags & UNSIGNED_TYPE) != 0 && (Flags & LONG_TYPE) == 0) {\r
843 //\r
844 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
845 // This assumption is made so the format string definition is compatible with the ANSI C\r
846 // Specification for formatted strings. It is recommended that the Base Types be used \r
847 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
848 // provides an implementation that is compatible with that largest possible set of CPU \r
849 // architectures. This is why the type "unsigned int" is used in this one case.\r
850 //\r
851 Value = (unsigned int)Value;\r
e389c39b 852 }\r
853 } else {\r
854 Radix = 16;\r
855 Comma = FALSE;\r
856 if ((Flags & LONG_TYPE) == 0 && Value < 0) {\r
57dd2382 857 //\r
9ade4339 858 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
18e81d77 859 // This assumption is made so the format string definition is compatible with the ANSI C\r
57dd2382 860 // Specification for formatted strings. It is recommended that the Base Types be used \r
861 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
862 // provides an implementation that is compatible with that largest possible set of CPU \r
863 // architectures. This is why the type "unsigned int" is used in this one case.\r
864 //\r
e389c39b 865 Value = (unsigned int)Value;\r
866 }\r
867 }\r
868 //\r
869 // Convert Value to a reversed string\r
870 //\r
7c905091 871 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;\r
e389c39b 872 if (Value == 0 && Precision == 0) {\r
873 Count = 0;\r
874 }\r
875 ArgumentString = (CHAR8 *)ValueBuffer + Count;\r
876 \r
877 Digits = Count % 3;\r
878 if (Digits != 0) {\r
879 Digits = 3 - Digits;\r
880 }\r
881 if (Comma && Count != 0) {\r
882 Count += ((Count - 1) / 3);\r
883 }\r
884 if (Prefix != 0) {\r
885 Count++;\r
886 Precision++;\r
887 }\r
888 Flags |= ARGUMENT_REVERSED;\r
889 ZeroPad = TRUE;\r
890 if ((Flags & PREFIX_ZERO) != 0) {\r
891 if ((Flags & LEFT_JUSTIFY) == 0) {\r
892 if ((Flags & PAD_TO_WIDTH) != 0) {\r
893 if ((Flags & PRECISION) == 0) {\r
894 Precision = Width;\r
895 }\r
896 }\r
897 }\r
898 }\r
899 break;\r
900\r
901 case 's':\r
902 case 'S':\r
903 Flags |= ARGUMENT_UNICODE;\r
904 //\r
905 // break skipped on purpose\r
906 //\r
907 case 'a':\r
2075236e 908 if (BaseListMarker == NULL) {\r
909 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);\r
910 } else {\r
911 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);\r
912 }\r
e389c39b 913 if (ArgumentString == NULL) {\r
e935092f 914 Flags &= ~((UINTN) ARGUMENT_UNICODE);\r
e389c39b 915 ArgumentString = "<null string>";\r
916 }\r
917 //\r
918 // Set the default precision for string to be zero if not specified.\r
919 //\r
920 if ((Flags & PRECISION) == 0) {\r
921 Precision = 0;\r
922 }\r
923 break;\r
924\r
925 case 'c':\r
2075236e 926 if (BaseListMarker == NULL) {\r
927 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;\r
928 } else {\r
929 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;\r
930 }\r
e389c39b 931 ArgumentString = (CHAR8 *)&Character;\r
932 Flags |= ARGUMENT_UNICODE;\r
933 break;\r
934\r
935 case 'g':\r
2075236e 936 if (BaseListMarker == NULL) {\r
937 TmpGuid = VA_ARG (VaListMarker, GUID *);\r
938 } else {\r
939 TmpGuid = BASE_ARG (BaseListMarker, GUID *);\r
940 }\r
e389c39b 941 if (TmpGuid == NULL) {\r
942 ArgumentString = "<null guid>";\r
943 } else {\r
4b0f378c
LG
944 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));\r
945 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));\r
946 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));\r
e389c39b 947 BasePrintLibSPrint (\r
948 ValueBuffer,\r
949 MAXIMUM_VALUE_CHARACTERS, \r
950 0,\r
951 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
4b0f378c
LG
952 GuidData1,\r
953 GuidData2,\r
954 GuidData3,\r
e389c39b 955 TmpGuid->Data4[0],\r
956 TmpGuid->Data4[1],\r
957 TmpGuid->Data4[2],\r
958 TmpGuid->Data4[3],\r
959 TmpGuid->Data4[4],\r
960 TmpGuid->Data4[5],\r
961 TmpGuid->Data4[6],\r
962 TmpGuid->Data4[7]\r
963 );\r
964 ArgumentString = ValueBuffer;\r
965 }\r
966 break;\r
967\r
968 case 't':\r
2075236e 969 if (BaseListMarker == NULL) {\r
970 TmpTime = VA_ARG (VaListMarker, TIME *); \r
971 } else {\r
972 TmpTime = BASE_ARG (BaseListMarker, TIME *); \r
973 }\r
e389c39b 974 if (TmpTime == NULL) {\r
975 ArgumentString = "<null time>";\r
976 } else {\r
977 BasePrintLibSPrint (\r
978 ValueBuffer,\r
979 MAXIMUM_VALUE_CHARACTERS,\r
980 0,\r
981 "%02d/%02d/%04d %02d:%02d",\r
982 TmpTime->Month,\r
983 TmpTime->Day,\r
984 TmpTime->Year,\r
985 TmpTime->Hour,\r
986 TmpTime->Minute\r
987 );\r
988 ArgumentString = ValueBuffer;\r
989 }\r
990 break;\r
991\r
992 case 'r':\r
2075236e 993 if (BaseListMarker == NULL) {\r
994 Status = VA_ARG (VaListMarker, RETURN_STATUS);\r
995 } else {\r
996 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);\r
997 }\r
e389c39b 998 ArgumentString = ValueBuffer;\r
999 if (RETURN_ERROR (Status)) {\r
1000 //\r
1001 // Clear error bit\r
1002 //\r
1003 Index = Status & ~MAX_BIT;\r
1004 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {\r
1005 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];\r
1006 }\r
1007 } else {\r
1008 Index = Status;\r
1009 if (Index <= WARNING_STATUS_NUMBER) {\r
1010 ArgumentString = mStatusString [Index];\r
1011 }\r
1012 }\r
1013 if (ArgumentString == ValueBuffer) {\r
1014 BasePrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);\r
1015 }\r
1016 break;\r
1017\r
c553db4b 1018 case '\r':\r
1019 Format += BytesPerFormatCharacter;\r
b1d4b965 1020 FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
c553db4b 1021 if (FormatCharacter == '\n') {\r
1022 //\r
1023 // Translate '\r\n' to '\r\n'\r
1024 //\r
1025 ArgumentString = "\r\n";\r
1026 } else {\r
1027 //\r
1028 // Translate '\r' to '\r'\r
1029 //\r
1030 ArgumentString = "\r";\r
1031 Format -= BytesPerFormatCharacter;\r
1032 }\r
1033 break;\r
1034\r
e389c39b 1035 case '\n':\r
c553db4b 1036 //\r
1037 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
1038 //\r
1039 ArgumentString = "\r\n";\r
1040 Format += BytesPerFormatCharacter;\r
b1d4b965 1041 FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
c553db4b 1042 if (FormatCharacter != '\r') {\r
1043 Format -= BytesPerFormatCharacter;\r
1044 }\r
e389c39b 1045 break;\r
1046\r
1047 case '%':\r
1048 default:\r
1049 //\r
1050 // if the type is '%' or unknown, then print it to the screen\r
1051 //\r
1052 ArgumentString = (CHAR8 *)&FormatCharacter;\r
1053 Flags |= ARGUMENT_UNICODE;\r
1054 break;\r
1055 }\r
1056 break;\r
1057 \r
c553db4b 1058 case '\r':\r
1059 Format += BytesPerFormatCharacter;\r
b1d4b965 1060 FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
c553db4b 1061 if (FormatCharacter == '\n') {\r
1062 //\r
1063 // Translate '\r\n' to '\r\n'\r
1064 //\r
1065 ArgumentString = "\r\n";\r
1066 } else {\r
1067 //\r
1068 // Translate '\r' to '\r'\r
1069 //\r
1070 ArgumentString = "\r";\r
1071 Format -= BytesPerFormatCharacter;\r
1072 }\r
1073 break;\r
1074\r
e389c39b 1075 case '\n':\r
c553db4b 1076 //\r
1077 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
1078 //\r
1079 ArgumentString = "\r\n";\r
1080 Format += BytesPerFormatCharacter;\r
b1d4b965 1081 FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
c553db4b 1082 if (FormatCharacter != '\r') {\r
1083 Format -= BytesPerFormatCharacter;\r
1084 }\r
e389c39b 1085 break;\r
1086\r
1087 default:\r
1088 ArgumentString = (CHAR8 *)&FormatCharacter;\r
1089 Flags |= ARGUMENT_UNICODE;\r
1090 break;\r
1091 }\r
1092\r
1093 //\r
1094 // Retrieve the ArgumentString attriubutes\r
1095 //\r
1096 if ((Flags & ARGUMENT_UNICODE) != 0) {\r
1097 ArgumentMask = 0xffff;\r
1098 BytesPerArgumentCharacter = 2;\r
1099 } else {\r
1100 ArgumentMask = 0xff;\r
1101 BytesPerArgumentCharacter = 1;\r
1102 }\r
1103 if ((Flags & ARGUMENT_REVERSED) != 0) {\r
1104 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;\r
1105 } else {\r
1106 //\r
1107 // Compute the number of characters in ArgumentString and store it in Count\r
1108 // ArgumentString is either null-terminated, or it contains Precision characters\r
1109 //\r
1110 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {\r
1111 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;\r
1112 if (ArgumentCharacter == 0) {\r
1113 break;\r
1114 }\r
1115 }\r
1116 }\r
1117\r
1118 if (Precision < Count) {\r
1119 Precision = Count;\r
1120 }\r
1121\r
1122 //\r
1123 // Pad before the string\r
1124 //\r
1125 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {\r
6bae492f 1126 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
6709bbd1 1127 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 1128 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
1129 }\r
e389c39b 1130 }\r
1131\r
1132 if (ZeroPad) {\r
1133 if (Prefix != 0) {\r
6bae492f 1134 LengthToReturn += (1 * BytesPerOutputCharacter);\r
6709bbd1 1135 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 1136 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
1137 }\r
1138 }\r
6bae492f 1139 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
6709bbd1 1140 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 1141 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);\r
e389c39b 1142 }\r
e389c39b 1143 } else {\r
6bae492f 1144 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
6709bbd1 1145 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 1146 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);\r
1147 }\r
e389c39b 1148 if (Prefix != 0) {\r
6bae492f 1149 LengthToReturn += (1 * BytesPerOutputCharacter);\r
6709bbd1 1150 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 1151 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
1152 }\r
e389c39b 1153 }\r
1154 }\r
1155\r
1156 //\r
1157 // Output the Prefix character if it is present\r
1158 //\r
1159 Index = 0;\r
1160 if (Prefix != 0) {\r
1161 Index++;\r
1162 }\r
1163\r
1164 //\r
1165 // Copy the string into the output buffer performing the required type conversions\r
1166 //\r
1167 while (Index < Count) {\r
1168 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;\r
1169\r
6bae492f 1170 LengthToReturn += (1 * BytesPerOutputCharacter);\r
6709bbd1 1171 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 1172 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);\r
1173 }\r
e389c39b 1174 ArgumentString += BytesPerArgumentCharacter;\r
1175 Index++;\r
1176 if (Comma) {\r
1177 Digits++;\r
1178 if (Digits == 3) {\r
1179 Digits = 0;\r
1180 Index++;\r
1181 if (Index < Count) {\r
6bae492f 1182 LengthToReturn += (1 * BytesPerOutputCharacter);\r
6709bbd1 1183 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 1184 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);\r
1185 }\r
e389c39b 1186 }\r
1187 }\r
1188 }\r
1189 }\r
1190\r
1191 //\r
1192 // Pad after the string\r
1193 //\r
1194 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {\r
6bae492f 1195 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
6709bbd1 1196 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
f405c067 1197 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
1198 }\r
e389c39b 1199 }\r
1200\r
1201 //\r
1202 // Get the next character from the format string\r
1203 //\r
1204 Format += BytesPerFormatCharacter;\r
1205\r
1206 //\r
1207 // Get the next character from the format string\r
1208 //\r
b1d4b965 1209 FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
e389c39b 1210 }\r
1211\r
4ae2e63c 1212 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r
f405c067 1213 return (LengthToReturn / BytesPerOutputCharacter);\r
1214 }\r
1215\r
6709bbd1 1216 ASSERT (Buffer != NULL);\r
e389c39b 1217 //\r
1218 // Null terminate the Unicode or ASCII string\r
1219 //\r
1220 BasePrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);\r
e389c39b 1221\r
1222 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);\r
1223}\r
1224\r
1225/**\r
1226 Worker function that produces a Null-terminated string in an output buffer \r
1227 based on a Null-terminated format string and variable argument list.\r
1228\r
1229 VSPrint function to process format and place the results in Buffer. Since a \r
7c905091 1230 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
efb23117 1231 this is the main print working routine\r
e389c39b 1232\r
2fc59a00 1233 @param StartOfBuffer The character buffer to print the results of the parsing\r
e389c39b 1234 of Format into.\r
2fc59a00 1235 @param BufferSize The maximum number of characters to put into buffer.\r
e389c39b 1236 Zero means no limit.\r
7c905091 1237 @param Flags Initial flags value.\r
e389c39b 1238 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set\r
2fc59a00 1239 @param FormatString A Null-terminated format string.\r
e389c39b 1240 @param ... The variable argument list.\r
1241\r
2fc59a00 1242 @return The number of characters printed.\r
e389c39b 1243\r
1244**/\r
1245UINTN\r
932d66a9 1246EFIAPI\r
e389c39b 1247BasePrintLibSPrint (\r
1248 OUT CHAR8 *StartOfBuffer,\r
1249 IN UINTN BufferSize,\r
1250 IN UINTN Flags,\r
1251 IN CONST CHAR8 *FormatString,\r
1252 ...\r
1253 )\r
1254{\r
1255 VA_LIST Marker;\r
3bbe68a3 1256 UINTN NumberOfPrinted;\r
e389c39b 1257\r
1258 VA_START (Marker, FormatString);\r
3bbe68a3 1259 NumberOfPrinted = BasePrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);\r
1260 VA_END (Marker);\r
1261 return NumberOfPrinted;\r
e389c39b 1262}\r