MdeModulePkg/PrintLib: Add safe print functions [A|U]ValueToStringS
[mirror_edk2.git] / MdeModulePkg / Library / DxePrintLibPrint2Protocol / PrintLib.c
CommitLineData
504214c4 1/** @file\r
f0c74f5a 2 Instance of Print Library based on gEfiPrint2SProtocolGuid.\r
a0afd019 3\r
504214c4 4 Implement the print library instance by wrap the interface \r
f0c74f5a 5 provided in the Print2S protocol. This protocol is defined as the internal\r
504214c4
LG
6 protocol related to this implementation, not in the public spec. So, this \r
7 library instance is only for this code base.\r
8\r
7c6075e2 9Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 10This program and the accompanying materials\r
a0afd019 11are licensed and made available under the terms and conditions of the BSD License\r
12which accompanies this distribution. The full text of the license may be found at\r
13http://opensource.org/licenses/bsd-license.php\r
14\r
15THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
16WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
17\r
504214c4 18**/\r
a0afd019 19\r
60c93673 20#include <Uefi.h>\r
f405c067 21#include <Base.h>\r
0ed0c867 22#include <Protocol/Print2.h>\r
504dcb0a 23\r
24#include <Library/PrintLib.h>\r
25\r
26#include <Library/BaseLib.h>\r
bee67555 27#include <Library/DebugLib.h>\r
50418baa
HW
28#include <Library/PcdLib.h>\r
29\r
30#define ASSERT_UNICODE_BUFFER(Buffer) ASSERT ((((UINTN) (Buffer)) & 0x01) == 0)\r
31\r
32//\r
33// Safe print checks\r
34//\r
35#define RSIZE_MAX (PcdGet32 (PcdMaximumUnicodeStringLength))\r
36#define ASCII_RSIZE_MAX (PcdGet32 (PcdMaximumAsciiStringLength))\r
37\r
38#define SAFE_PRINT_CONSTRAINT_CHECK(Expression, RetVal) \\r
39 do { \\r
40 ASSERT (Expression); \\r
41 if (!(Expression)) { \\r
42 return RetVal; \\r
43 } \\r
44 } while (FALSE)\r
a0afd019 45\r
f0c74f5a 46EFI_PRINT2S_PROTOCOL *mPrint2SProtocol = NULL;\r
a0afd019 47\r
ce95aa7a 48/**\r
f0c74f5a 49 The constructor function caches the pointer to Print2S protocol.\r
bee67555 50 \r
f0c74f5a 51 The constructor function locates Print2S protocol from protocol database.\r
bee67555 52 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS. \r
ce95aa7a 53\r
bee67555 54 @param ImageHandle The firmware allocated handle for the EFI image.\r
55 @param SystemTable A pointer to the EFI System Table.\r
56 \r
57 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
ce95aa7a 58\r
59**/\r
2ad4dad0
LG
60EFI_STATUS\r
61EFIAPI\r
bee67555 62PrintLibConstructor (\r
63 IN EFI_HANDLE ImageHandle,\r
64 IN EFI_SYSTEM_TABLE *SystemTable\r
a0afd019 65 )\r
a0afd019 66{\r
bee67555 67 EFI_STATUS Status;\r
68\r
35289219 69 Status = SystemTable->BootServices->LocateProtocol (\r
f0c74f5a 70 &gEfiPrint2SProtocolGuid,\r
35289219 71 NULL,\r
f0c74f5a 72 (VOID**) &mPrint2SProtocol\r
35289219 73 );\r
bee67555 74 ASSERT_EFI_ERROR (Status);\r
f0c74f5a 75 ASSERT (mPrint2SProtocol != NULL);\r
bee67555 76\r
77 return Status;\r
2ad4dad0
LG
78}\r
79\r
bee67555 80\r
504dcb0a 81/**\r
82 Worker function that converts a VA_LIST to a BASE_LIST based on a Null-terminated \r
83 format string.\r
84\r
85 @param AsciiFormat TRUE if Format is an ASCII string. FALSE if Format is a Unicode string.\r
86 @param Format Null-terminated format string.\r
87 @param VaListMarker VA_LIST style variable argument list consumed by processing Format.\r
88 @param BaseListMarker BASE_LIST style variable argument list consumed by processing Format.\r
89 @param Size The size, in bytes, of the BaseListMarker buffer.\r
90\r
7c6075e2
HW
91 @return TRUE The VA_LIST has been converted to BASE_LIST.\r
92 @return FALSE The VA_LIST has not been converted to BASE_LIST.\r
504dcb0a 93\r
94**/\r
95BOOLEAN\r
96DxePrintLibPrint2ProtocolVaListToBaseList (\r
97 IN BOOLEAN AsciiFormat,\r
98 IN CONST CHAR8 *Format,\r
99 IN VA_LIST VaListMarker,\r
100 OUT BASE_LIST BaseListMarker,\r
101 IN UINTN Size\r
102 )\r
103{\r
104 BASE_LIST BaseListStart;\r
105 UINTN BytesPerFormatCharacter;\r
106 UINTN FormatMask;\r
107 UINTN FormatCharacter;\r
108 BOOLEAN Long;\r
109 BOOLEAN Done;\r
110\r
504dcb0a 111 ASSERT (BaseListMarker != NULL);\r
50418baa 112 SAFE_PRINT_CONSTRAINT_CHECK ((Format != NULL), FALSE);\r
504dcb0a 113\r
114 BaseListStart = BaseListMarker;\r
115\r
116 if (AsciiFormat) {\r
50418baa
HW
117 if (ASCII_RSIZE_MAX != 0) {\r
118 SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), FALSE);\r
119 }\r
504dcb0a 120 BytesPerFormatCharacter = 1;\r
121 FormatMask = 0xff;\r
122 } else {\r
50418baa
HW
123 if (RSIZE_MAX != 0) {\r
124 SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16 *)Format, RSIZE_MAX + 1) <= RSIZE_MAX), FALSE);\r
125 }\r
504dcb0a 126 BytesPerFormatCharacter = 2;\r
127 FormatMask = 0xffff;\r
128 }\r
129\r
130 //\r
131 // Get the first character from the format string\r
132 //\r
133 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
134\r
135 while (FormatCharacter != 0) {\r
136 if (FormatCharacter == '%') {\r
137 Long = FALSE;\r
138\r
139 //\r
140 // Parse Flags and Width\r
141 //\r
142 for (Done = FALSE; !Done; ) {\r
143 //\r
144 // Get the next character from the format string\r
145 //\r
146 Format += BytesPerFormatCharacter;\r
147\r
148 //\r
149 // Get the next character from the format string\r
150 //\r
151 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
152\r
153 switch (FormatCharacter) {\r
154 case '.': \r
155 case '-': \r
156 case '+': \r
157 case ' ': \r
158 case ',': \r
159 case '0':\r
160 case '1':\r
161 case '2':\r
162 case '3':\r
163 case '4':\r
164 case '5':\r
165 case '6':\r
166 case '7':\r
167 case '8':\r
168 case '9':\r
169 break;\r
170 case 'L':\r
171 case 'l': \r
172 Long = TRUE;\r
173 break;\r
174 case '*':\r
175 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);\r
176 break;\r
177 case '\0':\r
178 //\r
179 // Make no output if Format string terminates unexpectedly when\r
180 // looking up for flag, width, precision and type. \r
181 //\r
182 Format -= BytesPerFormatCharacter;\r
183 //\r
184 // break skipped on purpose.\r
185 //\r
186 default:\r
187 Done = TRUE;\r
188 break;\r
189 }\r
190 } \r
191 \r
192 //\r
193 // Handle each argument type\r
194 //\r
195 switch (FormatCharacter) {\r
196 case 'p':\r
197 if (sizeof (VOID *) > 4) {\r
198 Long = TRUE;\r
199 }\r
200 case 'X':\r
201 case 'x':\r
796fecd8 202 case 'u':\r
504dcb0a 203 case 'd':\r
204 if (Long) {\r
205 BASE_ARG (BaseListMarker, INT64) = VA_ARG (VaListMarker, INT64);\r
206 } else {\r
207 BASE_ARG (BaseListMarker, int) = VA_ARG (VaListMarker, int);\r
208 }\r
209 break;\r
210 case 's':\r
211 case 'S':\r
212 case 'a':\r
213 case 'g':\r
214 case 't':\r
215 BASE_ARG (BaseListMarker, VOID *) = VA_ARG (VaListMarker, VOID *);\r
216 break;\r
217 case 'c':\r
218 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);\r
219 break;\r
220 case 'r':\r
221 BASE_ARG (BaseListMarker, RETURN_STATUS) = VA_ARG (VaListMarker, RETURN_STATUS);\r
222 break;\r
223 }\r
224 }\r
225\r
226 //\r
227 // If BASE_LIST is larger than Size, then return FALSE\r
228 //\r
229 if ((UINTN)((UINT8 *)BaseListMarker - (UINT8 *)BaseListStart) > Size) {\r
7c6075e2 230 DEBUG ((DEBUG_ERROR, "The input variable argument list is too long. Please consider breaking into multiple print calls.\n"));\r
504dcb0a 231 return FALSE;\r
232 }\r
233\r
234 //\r
235 // Get the next character from the format string\r
236 //\r
237 Format += BytesPerFormatCharacter;\r
238\r
239 //\r
240 // Get the next character from the format string\r
241 //\r
242 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
243 }\r
244 return TRUE;\r
245}\r
246\r
2ad4dad0 247/**\r
50418baa
HW
248 Produces a Null-terminated Unicode string in an output buffer based on\r
249 a Null-terminated Unicode format string and a VA_LIST argument list.\r
250\r
251 This function is similar as vsnprintf_s defined in C11.\r
252\r
2ad4dad0 253 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer\r
50418baa
HW
254 and BufferSize.\r
255 The Unicode string is produced by parsing the format string specified by FormatString.\r
256 Arguments are pulled from the variable argument list specified by Marker based on the\r
257 contents of the format string.\r
2ad4dad0
LG
258 The number of Unicode characters in the produced output buffer is returned not including\r
259 the Null-terminator.\r
2ad4dad0 260\r
50418baa
HW
261 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
262 If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
263\r
264 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
265 unmodified and 0 is returned.\r
266 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
267 unmodified and 0 is returned.\r
268 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >\r
269 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output\r
270 buffer is unmodified and 0 is returned.\r
271 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
2ad4dad0 272 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
50418baa
HW
273 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
274\r
275 If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned.\r
2ad4dad0 276\r
50418baa 277 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
2ad4dad0
LG
278 Unicode string.\r
279 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa 280 @param FormatString A Null-terminated Unicode format string.\r
2ad4dad0 281 @param Marker VA_LIST marker for the variable argument list.\r
50418baa 282\r
2ad4dad0
LG
283 @return The number of Unicode characters in the produced output buffer not including the\r
284 Null-terminator.\r
285\r
286**/\r
287UINTN\r
288EFIAPI\r
289UnicodeVSPrint (\r
290 OUT CHAR16 *StartOfBuffer,\r
291 IN UINTN BufferSize,\r
292 IN CONST CHAR16 *FormatString,\r
293 IN VA_LIST Marker\r
294 )\r
295{\r
7c6075e2
HW
296 UINT64 BaseListMarker[256 / sizeof (UINT64)];\r
297 BOOLEAN Converted;\r
298\r
50418baa
HW
299 ASSERT_UNICODE_BUFFER (StartOfBuffer);\r
300 ASSERT_UNICODE_BUFFER (FormatString);\r
301\r
7c6075e2
HW
302 Converted = DxePrintLibPrint2ProtocolVaListToBaseList (\r
303 FALSE,\r
304 (CHAR8 *)FormatString,\r
305 Marker,\r
306 (BASE_LIST)BaseListMarker,\r
307 sizeof (BaseListMarker) - 8\r
308 );\r
309 if (!Converted) {\r
310 return 0;\r
311 }\r
504dcb0a 312\r
313 return UnicodeBSPrint (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);\r
314}\r
315\r
316/**\r
50418baa
HW
317 Produces a Null-terminated Unicode string in an output buffer based on\r
318 a Null-terminated Unicode format string and a BASE_LIST argument list.\r
319\r
504dcb0a 320 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer\r
50418baa
HW
321 and BufferSize.\r
322 The Unicode string is produced by parsing the format string specified by FormatString.\r
323 Arguments are pulled from the variable argument list specified by Marker based on the\r
324 contents of the format string.\r
504dcb0a 325 The number of Unicode characters in the produced output buffer is returned not including\r
326 the Null-terminator.\r
504dcb0a 327\r
50418baa
HW
328 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
329 If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
330\r
331 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
332 unmodified and 0 is returned.\r
333 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
334 unmodified and 0 is returned.\r
335 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >\r
336 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output\r
337 buffer is unmodified and 0 is returned.\r
338 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
504dcb0a 339 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
50418baa
HW
340 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
341\r
342 If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned.\r
504dcb0a 343\r
50418baa 344 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
504dcb0a 345 Unicode string.\r
346 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa 347 @param FormatString A Null-terminated Unicode format string.\r
504dcb0a 348 @param Marker BASE_LIST marker for the variable argument list.\r
50418baa 349\r
504dcb0a 350 @return The number of Unicode characters in the produced output buffer not including the\r
351 Null-terminator.\r
352\r
353**/\r
354UINTN\r
355EFIAPI\r
356UnicodeBSPrint (\r
357 OUT CHAR16 *StartOfBuffer,\r
358 IN UINTN BufferSize,\r
359 IN CONST CHAR16 *FormatString,\r
360 IN BASE_LIST Marker\r
361 )\r
362{\r
50418baa
HW
363 ASSERT_UNICODE_BUFFER (StartOfBuffer);\r
364 ASSERT_UNICODE_BUFFER (FormatString);\r
f0c74f5a 365 return mPrint2SProtocol->UnicodeBSPrint (StartOfBuffer, BufferSize, FormatString, Marker);\r
a0afd019 366}\r
367\r
2ad4dad0 368/**\r
50418baa 369 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated\r
2ad4dad0 370 Unicode format string and variable argument list.\r
50418baa
HW
371\r
372 This function is similar as snprintf_s defined in C11.\r
373\r
2ad4dad0
LG
374 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer\r
375 and BufferSize.\r
376 The Unicode string is produced by parsing the format string specified by FormatString.\r
377 Arguments are pulled from the variable argument list based on the contents of the format string.\r
378 The number of Unicode characters in the produced output buffer is returned not including\r
379 the Null-terminator.\r
2ad4dad0 380\r
50418baa
HW
381 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
382 If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
383\r
384 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
385 unmodified and 0 is returned.\r
386 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
387 unmodified and 0 is returned.\r
388 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >\r
389 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output\r
390 buffer is unmodified and 0 is returned.\r
391 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
2ad4dad0 392 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
50418baa
HW
393 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
394\r
395 If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned.\r
2ad4dad0 396\r
50418baa 397 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
2ad4dad0
LG
398 Unicode string.\r
399 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa
HW
400 @param FormatString A Null-terminated Unicode format string.\r
401 @param ... Variable argument list whose contents are accessed based on the\r
71898234 402 format string specified by FormatString.\r
ce95aa7a 403\r
2ad4dad0
LG
404 @return The number of Unicode characters in the produced output buffer not including the\r
405 Null-terminator.\r
406\r
407**/\r
a0afd019 408UINTN\r
2ad4dad0 409EFIAPI\r
a0afd019 410UnicodeSPrint (\r
411 OUT CHAR16 *StartOfBuffer,\r
412 IN UINTN BufferSize,\r
2ad4dad0 413 IN CONST CHAR16 *FormatString,\r
a0afd019 414 ...\r
415 )\r
a0afd019 416{\r
2ad4dad0 417 VA_LIST Marker;\r
3bbe68a3 418 UINTN NumberOfPrinted;\r
a0afd019 419\r
420 VA_START (Marker, FormatString);\r
3bbe68a3 421 NumberOfPrinted = UnicodeVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);\r
422 VA_END (Marker);\r
423 return NumberOfPrinted;\r
a0afd019 424}\r
425\r
2ad4dad0
LG
426/**\r
427 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated\r
50418baa
HW
428 ASCII format string and a VA_LIST argument list.\r
429\r
430 This function is similar as vsnprintf_s defined in C11.\r
431\r
2ad4dad0
LG
432 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer\r
433 and BufferSize.\r
434 The Unicode string is produced by parsing the format string specified by FormatString.\r
50418baa 435 Arguments are pulled from the variable argument list specified by Marker based on the\r
2ad4dad0
LG
436 contents of the format string.\r
437 The number of Unicode characters in the produced output buffer is returned not including\r
438 the Null-terminator.\r
2ad4dad0 439\r
50418baa
HW
440 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
441\r
442 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
443 unmodified and 0 is returned.\r
444 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
445 unmodified and 0 is returned.\r
446 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >\r
447 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output\r
448 buffer is unmodified and 0 is returned.\r
2ad4dad0 449 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
50418baa
HW
450 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
451 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
2ad4dad0 452\r
50418baa
HW
453 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
454\r
455 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
2ad4dad0
LG
456 Unicode string.\r
457 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa 458 @param FormatString A Null-terminated ASCII format string.\r
2ad4dad0 459 @param Marker VA_LIST marker for the variable argument list.\r
50418baa 460\r
2ad4dad0
LG
461 @return The number of Unicode characters in the produced output buffer not including the\r
462 Null-terminator.\r
463\r
464**/\r
a0afd019 465UINTN\r
2ad4dad0
LG
466EFIAPI\r
467UnicodeVSPrintAsciiFormat (\r
468 OUT CHAR16 *StartOfBuffer,\r
469 IN UINTN BufferSize,\r
470 IN CONST CHAR8 *FormatString,\r
471 IN VA_LIST Marker\r
a0afd019 472 )\r
2ad4dad0 473{\r
7c6075e2
HW
474 UINT64 BaseListMarker[256 / sizeof (UINT64)];\r
475 BOOLEAN Converted;\r
476\r
50418baa
HW
477 ASSERT_UNICODE_BUFFER (StartOfBuffer);\r
478\r
7c6075e2
HW
479 Converted = DxePrintLibPrint2ProtocolVaListToBaseList (\r
480 TRUE,\r
481 FormatString,\r
482 Marker,\r
483 (BASE_LIST)BaseListMarker,\r
484 sizeof (BaseListMarker) - 8\r
485 );\r
486 if (!Converted) {\r
487 return 0;\r
488 }\r
504dcb0a 489\r
490 return UnicodeBSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);\r
491}\r
492\r
493/**\r
494 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated\r
50418baa
HW
495 ASCII format string and a BASE_LIST argument list.\r
496\r
504dcb0a 497 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer\r
498 and BufferSize.\r
499 The Unicode string is produced by parsing the format string specified by FormatString.\r
50418baa 500 Arguments are pulled from the variable argument list specified by Marker based on the\r
504dcb0a 501 contents of the format string.\r
502 The number of Unicode characters in the produced output buffer is returned not including\r
503 the Null-terminator.\r
504dcb0a 504\r
50418baa
HW
505 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
506\r
507 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
508 unmodified and 0 is returned.\r
509 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
510 unmodified and 0 is returned.\r
511 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >\r
512 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output\r
513 buffer is unmodified and 0 is returned.\r
504dcb0a 514 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
50418baa
HW
515 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
516 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
517\r
518 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
504dcb0a 519\r
50418baa 520 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
504dcb0a 521 Unicode string.\r
522 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa 523 @param FormatString A Null-terminated ASCII format string.\r
504dcb0a 524 @param Marker BASE_LIST marker for the variable argument list.\r
50418baa 525\r
504dcb0a 526 @return The number of Unicode characters in the produced output buffer not including the\r
527 Null-terminator.\r
528\r
529**/\r
530UINTN\r
531EFIAPI\r
532UnicodeBSPrintAsciiFormat (\r
533 OUT CHAR16 *StartOfBuffer,\r
534 IN UINTN BufferSize,\r
535 IN CONST CHAR8 *FormatString,\r
536 IN BASE_LIST Marker\r
537 )\r
538{\r
50418baa 539 ASSERT_UNICODE_BUFFER (StartOfBuffer);\r
f0c74f5a 540 return mPrint2SProtocol->UnicodeBSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker);\r
2ad4dad0 541}\r
a0afd019 542\r
2ad4dad0 543/**\r
50418baa 544 Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated\r
2ad4dad0 545 ASCII format string and variable argument list.\r
50418baa
HW
546\r
547 This function is similar as snprintf_s defined in C11.\r
548\r
2ad4dad0
LG
549 Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer\r
550 and BufferSize.\r
551 The Unicode string is produced by parsing the format string specified by FormatString.\r
50418baa 552 Arguments are pulled from the variable argument list based on the contents of the\r
2ad4dad0
LG
553 format string.\r
554 The number of Unicode characters in the produced output buffer is returned not including\r
555 the Null-terminator.\r
2ad4dad0 556\r
50418baa
HW
557 If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
558\r
559 If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
560 unmodified and 0 is returned.\r
561 If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
562 unmodified and 0 is returned.\r
563 If PcdMaximumUnicodeStringLength is not zero, and BufferSize >\r
564 (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output\r
565 buffer is unmodified and 0 is returned.\r
2ad4dad0 566 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
50418baa
HW
567 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
568 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
569\r
570 If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
2ad4dad0 571\r
50418baa 572 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
2ad4dad0
LG
573 Unicode string.\r
574 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa
HW
575 @param FormatString A Null-terminated ASCII format string.\r
576 @param ... Variable argument list whose contents are accessed based on the\r
71898234 577 format string specified by FormatString.\r
50418baa 578\r
2ad4dad0
LG
579 @return The number of Unicode characters in the produced output buffer not including the\r
580 Null-terminator.\r
581\r
582**/\r
583UINTN\r
584EFIAPI\r
585UnicodeSPrintAsciiFormat (\r
586 OUT CHAR16 *StartOfBuffer,\r
587 IN UINTN BufferSize,\r
588 IN CONST CHAR8 *FormatString,\r
589 ...\r
590 )\r
591{\r
592 VA_LIST Marker;\r
3bbe68a3 593 UINTN NumberOfPrinted;\r
a0afd019 594\r
2ad4dad0 595 VA_START (Marker, FormatString);\r
3bbe68a3 596 NumberOfPrinted = UnicodeVSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker);\r
597 VA_END (Marker);\r
598 return NumberOfPrinted;\r
2ad4dad0 599}\r
a0afd019 600\r
2ad4dad0
LG
601/**\r
602 Converts a decimal value to a Null-terminated Unicode string.\r
603 \r
604 Converts the decimal number specified by Value to a Null-terminated Unicode \r
605 string specified by Buffer containing at most Width characters. No padding of spaces \r
606 is ever performed. If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
607 The number of Unicode characters in Buffer is returned not including the Null-terminator.\r
608 If the conversion contains more than Width characters, then only the first\r
609 Width characters are returned, and the total number of characters \r
610 required to perform the conversion is returned.\r
611 Additional conversion parameters are specified in Flags. \r
612 \r
613 The Flags bit LEFT_JUSTIFY is always ignored.\r
614 All conversions are left justified in Buffer.\r
615 If Width is 0, PREFIX_ZERO is ignored in Flags.\r
616 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r
617 are inserted every 3rd digit starting from the right.\r
df8d0595 618 If RADIX_HEX is set in Flags, then the output buffer will be \r
2ad4dad0 619 formatted in hexadecimal format.\r
df8d0595 620 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in Buffer is a '-'.\r
2ad4dad0
LG
621 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r
622 then Buffer is padded with '0' characters so the combination of the optional '-' \r
623 sign character, '0' characters, digit characters for Value, and the Null-terminator\r
624 add up to Width characters.\r
df8d0595 625 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().\r
2ad4dad0
LG
626 If Buffer is NULL, then ASSERT().\r
627 If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r
628 If unsupported bits are set in Flags, then ASSERT().\r
df8d0595 629 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().\r
2ad4dad0
LG
630 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r
631\r
632 @param Buffer Pointer to the output buffer for the produced Null-terminated\r
633 Unicode string.\r
634 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.\r
635 @param Value The 64-bit signed value to convert to a string.\r
636 @param Width The maximum number of Unicode characters to place in Buffer, not including\r
637 the Null-terminator.\r
638 \r
639 @return The number of Unicode characters in Buffer not including the Null-terminator.\r
640\r
641**/\r
642UINTN\r
643EFIAPI\r
644UnicodeValueToString (\r
645 IN OUT CHAR16 *Buffer,\r
646 IN UINTN Flags,\r
647 IN INT64 Value,\r
648 IN UINTN Width\r
649 )\r
650{\r
f0c74f5a
HW
651 RETURN_STATUS Status;\r
652 UINTN BufferSize;\r
653\r
654 if (Width == 0) {\r
655 BufferSize = (MAXIMUM_VALUE_CHARACTERS + 1) * sizeof (CHAR16);\r
656 } else {\r
657 BufferSize = (Width + 1) * sizeof (CHAR16);\r
658 }\r
659\r
660 Status = mPrint2SProtocol->UnicodeValueToStringS (Buffer, BufferSize, Flags, Value, Width);\r
661 if (RETURN_ERROR (Status)) {\r
662 return 0;\r
663 }\r
664\r
665 return StrnLenS (Buffer, BufferSize / sizeof (CHAR16));\r
2ad4dad0 666}\r
a0afd019 667\r
9e32e971
HW
668/**\r
669 Converts a decimal value to a Null-terminated Unicode string.\r
670\r
671 Converts the decimal number specified by Value to a Null-terminated Unicode\r
672 string specified by Buffer containing at most Width characters. No padding of\r
673 spaces is ever performed. If Width is 0 then a width of\r
674 MAXIMUM_VALUE_CHARACTERS is assumed. If the conversion contains more than\r
675 Width characters, then only the first Width characters are placed in Buffer.\r
676 Additional conversion parameters are specified in Flags.\r
677\r
678 The Flags bit LEFT_JUSTIFY is always ignored.\r
679 All conversions are left justified in Buffer.\r
680 If Width is 0, PREFIX_ZERO is ignored in Flags.\r
681 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and\r
682 commas are inserted every 3rd digit starting from the right.\r
683 If RADIX_HEX is set in Flags, then the output buffer will be formatted in\r
684 hexadecimal format.\r
685 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in\r
686 Buffer is a '-'.\r
687 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, then\r
688 Buffer is padded with '0' characters so the combination of the optional '-'\r
689 sign character, '0' characters, digit characters for Value, and the\r
690 Null-terminator add up to Width characters.\r
691\r
692 If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r
693 If an error would be returned, then the function will also ASSERT().\r
694\r
695 @param Buffer The pointer to the output buffer for the produced\r
696 Null-terminated Unicode string.\r
697 @param BufferSize The size of Buffer in bytes, including the\r
698 Null-terminator.\r
699 @param Flags The bitmask of flags that specify left justification,\r
700 zero pad, and commas.\r
701 @param Value The 64-bit signed value to convert to a string.\r
702 @param Width The maximum number of Unicode characters to place in\r
703 Buffer, not including the Null-terminator.\r
704\r
705 @retval RETURN_SUCCESS The decimal value is converted.\r
706 @retval RETURN_BUFFER_TOO_SMALL If BufferSize cannot hold the converted\r
707 value.\r
708 @retval RETURN_INVALID_PARAMETER If Buffer is NULL.\r
709 If PcdMaximumUnicodeStringLength is not\r
710 zero, and BufferSize is greater than\r
711 (PcdMaximumUnicodeStringLength *\r
712 sizeof (CHAR16) + 1).\r
713 If unsupported bits are set in Flags.\r
714 If both COMMA_TYPE and RADIX_HEX are set in\r
715 Flags.\r
716 If Width >= MAXIMUM_VALUE_CHARACTERS.\r
717\r
718**/\r
719RETURN_STATUS\r
720EFIAPI\r
721UnicodeValueToStringS (\r
722 IN OUT CHAR16 *Buffer,\r
723 IN UINTN BufferSize,\r
724 IN UINTN Flags,\r
725 IN INT64 Value,\r
726 IN UINTN Width\r
727 )\r
728{\r
729 return mPrint2SProtocol->UnicodeValueToStringS (Buffer, BufferSize, Flags, Value, Width);\r
730}\r
731\r
2ad4dad0
LG
732/**\r
733 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
734 ASCII format string and a VA_LIST argument list.\r
50418baa
HW
735\r
736 This function is similar as vsnprintf_s defined in C11.\r
737\r
2ad4dad0
LG
738 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
739 and BufferSize.\r
740 The ASCII string is produced by parsing the format string specified by FormatString.\r
50418baa 741 Arguments are pulled from the variable argument list specified by Marker based on\r
2ad4dad0
LG
742 the contents of the format string.\r
743 The number of ASCII characters in the produced output buffer is returned not including\r
744 the Null-terminator.\r
2ad4dad0 745\r
50418baa
HW
746 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
747 unmodified and 0 is returned.\r
748 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
749 unmodified and 0 is returned.\r
750 If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
751 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
752 is unmodified and 0 is returned.\r
2ad4dad0 753 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
50418baa
HW
754 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
755 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
756\r
757 If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
2ad4dad0 758\r
50418baa 759 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
2ad4dad0
LG
760 ASCII string.\r
761 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa 762 @param FormatString A Null-terminated ASCII format string.\r
2ad4dad0 763 @param Marker VA_LIST marker for the variable argument list.\r
50418baa 764\r
2ad4dad0
LG
765 @return The number of ASCII characters in the produced output buffer not including the\r
766 Null-terminator.\r
767\r
768**/\r
769UINTN\r
770EFIAPI\r
771AsciiVSPrint (\r
772 OUT CHAR8 *StartOfBuffer,\r
773 IN UINTN BufferSize,\r
774 IN CONST CHAR8 *FormatString,\r
775 IN VA_LIST Marker\r
776 )\r
a0afd019 777{\r
7c6075e2
HW
778 UINT64 BaseListMarker[256 / sizeof (UINT64)];\r
779 BOOLEAN Converted;\r
780\r
781 Converted = DxePrintLibPrint2ProtocolVaListToBaseList (\r
782 TRUE,\r
783 FormatString,\r
784 Marker,\r
785 (BASE_LIST)BaseListMarker,\r
786 sizeof (BaseListMarker) - 8\r
787 );\r
788 if (!Converted) {\r
789 return 0;\r
790 }\r
504dcb0a 791\r
792 return AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);\r
793}\r
794\r
795/**\r
796 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
797 ASCII format string and a BASE_LIST argument list.\r
50418baa 798\r
504dcb0a 799 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
800 and BufferSize.\r
801 The ASCII string is produced by parsing the format string specified by FormatString.\r
50418baa 802 Arguments are pulled from the variable argument list specified by Marker based on\r
504dcb0a 803 the contents of the format string.\r
804 The number of ASCII characters in the produced output buffer is returned not including\r
805 the Null-terminator.\r
504dcb0a 806\r
50418baa
HW
807 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
808 unmodified and 0 is returned.\r
809 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
810 unmodified and 0 is returned.\r
811 If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
812 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
813 is unmodified and 0 is returned.\r
504dcb0a 814 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
50418baa
HW
815 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
816 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
504dcb0a 817\r
50418baa
HW
818 If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
819\r
820 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
504dcb0a 821 ASCII string.\r
822 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa 823 @param FormatString A Null-terminated ASCII format string.\r
504dcb0a 824 @param Marker BASE_LIST marker for the variable argument list.\r
50418baa 825\r
504dcb0a 826 @return The number of ASCII characters in the produced output buffer not including the\r
827 Null-terminator.\r
828\r
829**/\r
830UINTN\r
831EFIAPI\r
832AsciiBSPrint (\r
833 OUT CHAR8 *StartOfBuffer,\r
834 IN UINTN BufferSize,\r
835 IN CONST CHAR8 *FormatString,\r
836 IN BASE_LIST Marker\r
837 )\r
838{\r
f0c74f5a 839 return mPrint2SProtocol->AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, Marker);\r
a0afd019 840}\r
841\r
2ad4dad0
LG
842/**\r
843 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
844 ASCII format string and variable argument list.\r
50418baa
HW
845\r
846 This function is similar as snprintf_s defined in C11.\r
847\r
2ad4dad0
LG
848 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
849 and BufferSize.\r
850 The ASCII string is produced by parsing the format string specified by FormatString.\r
50418baa 851 Arguments are pulled from the variable argument list based on the contents of the\r
2ad4dad0
LG
852 format string.\r
853 The number of ASCII characters in the produced output buffer is returned not including\r
854 the Null-terminator.\r
2ad4dad0 855\r
50418baa
HW
856 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
857 unmodified and 0 is returned.\r
858 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
859 unmodified and 0 is returned.\r
860 If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
861 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
862 is unmodified and 0 is returned.\r
2ad4dad0 863 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
50418baa
HW
864 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
865 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
866\r
867 If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
2ad4dad0 868\r
50418baa 869 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
2ad4dad0
LG
870 ASCII string.\r
871 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa
HW
872 @param FormatString A Null-terminated ASCII format string.\r
873 @param ... Variable argument list whose contents are accessed based on the\r
71898234 874 format string specified by FormatString.\r
ce95aa7a 875\r
2ad4dad0
LG
876 @return The number of ASCII characters in the produced output buffer not including the\r
877 Null-terminator.\r
878\r
879**/\r
a0afd019 880UINTN\r
2ad4dad0 881EFIAPI\r
a0afd019 882AsciiSPrint (\r
883 OUT CHAR8 *StartOfBuffer,\r
884 IN UINTN BufferSize,\r
2ad4dad0 885 IN CONST CHAR8 *FormatString,\r
a0afd019 886 ...\r
887 )\r
2ad4dad0
LG
888{\r
889 VA_LIST Marker;\r
3bbe68a3 890 UINTN NumberOfPrinted;\r
2ad4dad0
LG
891\r
892 VA_START (Marker, FormatString);\r
3bbe68a3 893 NumberOfPrinted = AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);\r
894 VA_END (Marker);\r
895 return NumberOfPrinted;\r
2ad4dad0 896}\r
a0afd019 897\r
2ad4dad0
LG
898/**\r
899 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
50418baa
HW
900 Unicode format string and a VA_LIST argument list.\r
901\r
902 This function is similar as vsnprintf_s defined in C11.\r
903\r
2ad4dad0
LG
904 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
905 and BufferSize.\r
906 The ASCII string is produced by parsing the format string specified by FormatString.\r
50418baa 907 Arguments are pulled from the variable argument list specified by Marker based on\r
2ad4dad0
LG
908 the contents of the format string.\r
909 The number of ASCII characters in the produced output buffer is returned not including\r
910 the Null-terminator.\r
2ad4dad0 911\r
50418baa
HW
912 If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
913\r
914 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
915 unmodified and 0 is returned.\r
916 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
917 unmodified and 0 is returned.\r
918 If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
919 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
920 is unmodified and 0 is returned.\r
2ad4dad0
LG
921 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
922 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
50418baa 923 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
2ad4dad0 924\r
50418baa
HW
925 If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
926\r
927 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
2ad4dad0
LG
928 ASCII string.\r
929 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa 930 @param FormatString A Null-terminated Unicode format string.\r
2ad4dad0 931 @param Marker VA_LIST marker for the variable argument list.\r
50418baa 932\r
2ad4dad0
LG
933 @return The number of ASCII characters in the produced output buffer not including the\r
934 Null-terminator.\r
935\r
936**/\r
937UINTN\r
938EFIAPI\r
939AsciiVSPrintUnicodeFormat (\r
940 OUT CHAR8 *StartOfBuffer,\r
941 IN UINTN BufferSize,\r
942 IN CONST CHAR16 *FormatString,\r
943 IN VA_LIST Marker\r
944 )\r
a0afd019 945{\r
7c6075e2
HW
946 UINT64 BaseListMarker[256 / sizeof (UINT64)];\r
947 BOOLEAN Converted;\r
948\r
50418baa
HW
949 ASSERT_UNICODE_BUFFER (FormatString);\r
950\r
7c6075e2
HW
951 Converted = DxePrintLibPrint2ProtocolVaListToBaseList (\r
952 FALSE,\r
953 (CHAR8 *)FormatString,\r
954 Marker,\r
955 (BASE_LIST)BaseListMarker,\r
956 sizeof (BaseListMarker) - 8\r
957 );\r
958 if (!Converted) {\r
959 return 0;\r
960 }\r
504dcb0a 961\r
962 return AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);\r
963}\r
964\r
965/**\r
966 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
50418baa
HW
967 Unicode format string and a BASE_LIST argument list.\r
968\r
504dcb0a 969 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
970 and BufferSize.\r
971 The ASCII string is produced by parsing the format string specified by FormatString.\r
50418baa 972 Arguments are pulled from the variable argument list specified by Marker based on\r
504dcb0a 973 the contents of the format string.\r
974 The number of ASCII characters in the produced output buffer is returned not including\r
975 the Null-terminator.\r
504dcb0a 976\r
50418baa
HW
977 If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
978\r
979 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
980 unmodified and 0 is returned.\r
981 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
982 unmodified and 0 is returned.\r
983 If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
984 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
985 is unmodified and 0 is returned.\r
504dcb0a 986 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
987 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
50418baa 988 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
504dcb0a 989\r
50418baa
HW
990 If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
991\r
992 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
504dcb0a 993 ASCII string.\r
994 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa 995 @param FormatString A Null-terminated Unicode format string.\r
504dcb0a 996 @param Marker BASE_LIST marker for the variable argument list.\r
50418baa 997\r
504dcb0a 998 @return The number of ASCII characters in the produced output buffer not including the\r
999 Null-terminator.\r
1000\r
1001**/\r
1002UINTN\r
1003EFIAPI\r
1004AsciiBSPrintUnicodeFormat (\r
1005 OUT CHAR8 *StartOfBuffer,\r
1006 IN UINTN BufferSize,\r
1007 IN CONST CHAR16 *FormatString,\r
1008 IN BASE_LIST Marker\r
1009 )\r
1010{\r
50418baa 1011 ASSERT_UNICODE_BUFFER (FormatString);\r
f0c74f5a 1012 return mPrint2SProtocol->AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);\r
2ad4dad0
LG
1013}\r
1014\r
1015/**\r
1016 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
50418baa
HW
1017 Unicode format string and variable argument list.\r
1018\r
1019 This function is similar as snprintf_s defined in C11.\r
1020\r
2ad4dad0
LG
1021 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
1022 and BufferSize.\r
1023 The ASCII string is produced by parsing the format string specified by FormatString.\r
50418baa 1024 Arguments are pulled from the variable argument list based on the contents of the\r
2ad4dad0
LG
1025 format string.\r
1026 The number of ASCII characters in the produced output buffer is returned not including\r
1027 the Null-terminator.\r
2ad4dad0 1028\r
50418baa
HW
1029 If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
1030\r
1031 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
1032 unmodified and 0 is returned.\r
1033 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
1034 unmodified and 0 is returned.\r
1035 If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
1036 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
1037 is unmodified and 0 is returned.\r
2ad4dad0
LG
1038 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
1039 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
50418baa 1040 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
2ad4dad0 1041\r
50418baa
HW
1042 If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
1043\r
1044 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
2ad4dad0
LG
1045 ASCII string.\r
1046 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa
HW
1047 @param FormatString A Null-terminated Unicode format string.\r
1048 @param ... Variable argument list whose contents are accessed based on the\r
71898234 1049 format string specified by FormatString.\r
ce95aa7a 1050\r
2ad4dad0
LG
1051 @return The number of ASCII characters in the produced output buffer not including the\r
1052 Null-terminator.\r
1053\r
1054**/\r
1055UINTN\r
1056EFIAPI\r
1057AsciiSPrintUnicodeFormat (\r
1058 OUT CHAR8 *StartOfBuffer,\r
1059 IN UINTN BufferSize,\r
1060 IN CONST CHAR16 *FormatString,\r
1061 ...\r
1062 )\r
1063{\r
1064 VA_LIST Marker;\r
3bbe68a3 1065 UINTN NumberOfPrinted;\r
a0afd019 1066\r
1067 VA_START (Marker, FormatString);\r
3bbe68a3 1068 NumberOfPrinted = AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);\r
1069 VA_END (Marker);\r
1070 return NumberOfPrinted;\r
2ad4dad0
LG
1071}\r
1072\r
1073\r
1074/**\r
1075 Converts a decimal value to a Null-terminated ASCII string.\r
1076 \r
1077 Converts the decimal number specified by Value to a Null-terminated ASCII string \r
1078 specified by Buffer containing at most Width characters. No padding of spaces \r
1079 is ever performed.\r
1080 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
1081 The number of ASCII characters in Buffer is returned not including the Null-terminator.\r
1082 If the conversion contains more than Width characters, then only the first Width\r
1083 characters are returned, and the total number of characters required to perform\r
1084 the conversion is returned.\r
1085 Additional conversion parameters are specified in Flags. \r
1086 The Flags bit LEFT_JUSTIFY is always ignored.\r
1087 All conversions are left justified in Buffer.\r
1088 If Width is 0, PREFIX_ZERO is ignored in Flags.\r
1089 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r
1090 are inserted every 3rd digit starting from the right.\r
df8d0595 1091 If RADIX_HEX is set in Flags, then the output buffer will be \r
2ad4dad0 1092 formatted in hexadecimal format.\r
df8d0595 1093 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in Buffer is a '-'.\r
2ad4dad0
LG
1094 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r
1095 then Buffer is padded with '0' characters so the combination of the optional '-' \r
1096 sign character, '0' characters, digit characters for Value, and the Null-terminator\r
1097 add up to Width characters.\r
1098 \r
1099 If Buffer is NULL, then ASSERT().\r
1100 If unsupported bits are set in Flags, then ASSERT().\r
df8d0595 1101 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().\r
2ad4dad0
LG
1102 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r
1103\r
1104 @param Buffer Pointer to the output buffer for the produced Null-terminated\r
1105 ASCII string.\r
1106 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.\r
1107 @param Value The 64-bit signed value to convert to a string.\r
1108 @param Width The maximum number of ASCII characters to place in Buffer, not including\r
1109 the Null-terminator.\r
1110 \r
1111 @return The number of ASCII characters in Buffer not including the Null-terminator.\r
1112\r
1113**/\r
1114UINTN\r
1115EFIAPI\r
1116AsciiValueToString (\r
ea99d5d1 1117 OUT CHAR8 *Buffer,\r
1118 IN UINTN Flags,\r
1119 IN INT64 Value,\r
1120 IN UINTN Width\r
2ad4dad0
LG
1121 )\r
1122{\r
f0c74f5a
HW
1123 RETURN_STATUS Status;\r
1124 UINTN BufferSize;\r
1125\r
1126 if (Width == 0) {\r
1127 BufferSize = (MAXIMUM_VALUE_CHARACTERS + 1) * sizeof (CHAR8);\r
1128 } else {\r
1129 BufferSize = (Width + 1) * sizeof (CHAR8);\r
1130 }\r
1131\r
1132 Status = mPrint2SProtocol->AsciiValueToStringS (Buffer, BufferSize, Flags, Value, Width);\r
1133 if (RETURN_ERROR (Status)) {\r
1134 return 0;\r
1135 }\r
1136\r
1137 return AsciiStrnLenS (Buffer, BufferSize / sizeof (CHAR8));\r
a0afd019 1138}\r
f405c067 1139\r
9e32e971
HW
1140/**\r
1141 Converts a decimal value to a Null-terminated Ascii string.\r
1142\r
1143 Converts the decimal number specified by Value to a Null-terminated Ascii\r
1144 string specified by Buffer containing at most Width characters. No padding of\r
1145 spaces is ever performed. If Width is 0 then a width of\r
1146 MAXIMUM_VALUE_CHARACTERS is assumed. If the conversion contains more than\r
1147 Width characters, then only the first Width characters are placed in Buffer.\r
1148 Additional conversion parameters are specified in Flags.\r
1149\r
1150 The Flags bit LEFT_JUSTIFY is always ignored.\r
1151 All conversions are left justified in Buffer.\r
1152 If Width is 0, PREFIX_ZERO is ignored in Flags.\r
1153 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and\r
1154 commas are inserted every 3rd digit starting from the right.\r
1155 If RADIX_HEX is set in Flags, then the output buffer will be formatted in\r
1156 hexadecimal format.\r
1157 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in\r
1158 Buffer is a '-'.\r
1159 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, then\r
1160 Buffer is padded with '0' characters so the combination of the optional '-'\r
1161 sign character, '0' characters, digit characters for Value, and the\r
1162 Null-terminator add up to Width characters.\r
1163\r
1164 If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r
1165 If an error would be returned, then the function will also ASSERT().\r
1166\r
1167 @param Buffer The pointer to the output buffer for the produced\r
1168 Null-terminated Ascii string.\r
1169 @param BufferSize The size of Buffer in bytes, including the\r
1170 Null-terminator.\r
1171 @param Flags The bitmask of flags that specify left justification,\r
1172 zero pad, and commas.\r
1173 @param Value The 64-bit signed value to convert to a string.\r
1174 @param Width The maximum number of Ascii characters to place in\r
1175 Buffer, not including the Null-terminator.\r
1176\r
1177 @retval RETURN_SUCCESS The decimal value is converted.\r
1178 @retval RETURN_BUFFER_TOO_SMALL If BufferSize cannot hold the converted\r
1179 value.\r
1180 @retval RETURN_INVALID_PARAMETER If Buffer is NULL.\r
1181 If PcdMaximumAsciiStringLength is not\r
1182 zero, and BufferSize is greater than\r
1183 PcdMaximumAsciiStringLength.\r
1184 If unsupported bits are set in Flags.\r
1185 If both COMMA_TYPE and RADIX_HEX are set in\r
1186 Flags.\r
1187 If Width >= MAXIMUM_VALUE_CHARACTERS.\r
1188\r
1189**/\r
1190RETURN_STATUS\r
1191EFIAPI\r
1192AsciiValueToStringS (\r
1193 IN OUT CHAR8 *Buffer,\r
1194 IN UINTN BufferSize,\r
1195 IN UINTN Flags,\r
1196 IN INT64 Value,\r
1197 IN UINTN Width\r
1198 )\r
1199{\r
1200 return mPrint2SProtocol->AsciiValueToStringS (Buffer, BufferSize, Flags, Value, Width);\r
1201}\r
1202\r
f405c067 1203#define PREFIX_SIGN BIT1\r
1204#define PREFIX_BLANK BIT2\r
1205#define LONG_TYPE BIT4\r
1206#define OUTPUT_UNICODE BIT6\r
1207#define FORMAT_UNICODE BIT8\r
1208#define PAD_TO_WIDTH BIT9\r
1209#define ARGUMENT_UNICODE BIT10\r
1210#define PRECISION BIT11\r
1211#define ARGUMENT_REVERSED BIT12\r
1212#define COUNT_ONLY_NO_PRINT BIT13\r
796fecd8 1213#define UNSIGNED_TYPE BIT14\r
f405c067 1214\r
1215//\r
1216// Record date and time information\r
1217//\r
1218typedef struct {\r
1219 UINT16 Year;\r
1220 UINT8 Month;\r
1221 UINT8 Day;\r
1222 UINT8 Hour;\r
1223 UINT8 Minute;\r
1224 UINT8 Second;\r
1225 UINT8 Pad1;\r
1226 UINT32 Nanosecond;\r
1227 INT16 TimeZone;\r
1228 UINT8 Daylight;\r
1229 UINT8 Pad2;\r
1230} TIME;\r
1231\r
1232GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};\r
1233\r
1234/**\r
1235 Internal function that convert a number to a string in Buffer.\r
1236\r
1237 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.\r
1238\r
1239 @param Buffer Location to place the ASCII string of Value.\r
1240 @param Value The value to convert to a Decimal or Hexadecimal string in Buffer.\r
1241 @param Radix Radix of the value\r
1242\r
1243 @return A pointer to the end of buffer filled with ASCII string.\r
1244\r
1245**/\r
1246CHAR8 *\r
1247InternalPrintLibValueToString (\r
1248 IN OUT CHAR8 *Buffer, \r
1249 IN INT64 Value, \r
1250 IN UINTN Radix\r
1251 )\r
1252{\r
1253 UINT32 Remainder;\r
1254\r
1255 //\r
1256 // Loop to convert one digit at a time in reverse order\r
1257 //\r
1258 *Buffer = 0;\r
1259 do {\r
1260 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r
1261 *(++Buffer) = mHexStr[Remainder];\r
1262 } while (Value != 0);\r
1263\r
1264 //\r
1265 // Return pointer of the end of filled buffer.\r
1266 //\r
1267 return Buffer;\r
1268}\r
1269\r
1270/**\r
1271 Worker function that produces a Null-terminated string in an output buffer \r
1272 based on a Null-terminated format string and a VA_LIST argument list.\r
1273\r
1274 VSPrint function to process format and place the results in Buffer. Since a \r
1275 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
1276 this is the main print working routine.\r
1277\r
1278 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.\r
1279\r
1280 @param[out] Buffer The character buffer to print the results of the \r
1281 parsing of Format into.\r
1282 @param[in] BufferSize The maximum number of characters to put into \r
1283 buffer.\r
1284 @param[in] Flags Initial flags value.\r
1285 Can only have FORMAT_UNICODE, OUTPUT_UNICODE, \r
1286 and COUNT_ONLY_NO_PRINT set.\r
1287 @param[in] Format A Null-terminated format string.\r
1288 @param[in] VaListMarker VA_LIST style variable argument list consumed by\r
1289 processing Format.\r
1290 @param[in] BaseListMarker BASE_LIST style variable argument list consumed\r
1291 by processing Format.\r
1292\r
1293 @return The number of characters printed not including the Null-terminator.\r
1294 If COUNT_ONLY_NO_PRINT was set returns the same, but without any\r
1295 modification to Buffer.\r
1296\r
1297**/\r
1298UINTN\r
1299InternalPrintLibSPrintMarker (\r
1300 OUT CHAR8 *Buffer,\r
1301 IN UINTN BufferSize,\r
1302 IN UINTN Flags,\r
1303 IN CONST CHAR8 *Format,\r
1304 IN VA_LIST VaListMarker, OPTIONAL\r
1305 IN BASE_LIST BaseListMarker OPTIONAL\r
1306 );\r
1307\r
1308/**\r
1309 Worker function that produces a Null-terminated string in an output buffer \r
1310 based on a Null-terminated format string and variable argument list.\r
1311\r
1312 VSPrint function to process format and place the results in Buffer. Since a \r
1313 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
1314 this is the main print working routine\r
1315\r
1316 @param StartOfBuffer The character buffer to print the results of the parsing\r
1317 of Format into.\r
1318 @param BufferSize The maximum number of characters to put into buffer.\r
1319 Zero means no limit.\r
1320 @param Flags Initial flags value.\r
1321 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set\r
1322 @param FormatString A Null-terminated format string.\r
1323 @param ... The variable argument list.\r
1324\r
1325 @return The number of characters printed.\r
1326\r
1327**/\r
1328UINTN\r
1329EFIAPI\r
1330InternalPrintLibSPrint (\r
1331 OUT CHAR8 *StartOfBuffer,\r
1332 IN UINTN BufferSize,\r
1333 IN UINTN Flags,\r
1334 IN CONST CHAR8 *FormatString,\r
1335 ...\r
1336 )\r
1337{\r
1338 VA_LIST Marker;\r
3bbe68a3 1339 UINTN NumberOfPrinted;\r
f405c067 1340\r
1341 VA_START (Marker, FormatString);\r
3bbe68a3 1342 NumberOfPrinted = InternalPrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);\r
1343 VA_END (Marker);\r
1344 return NumberOfPrinted;\r
f405c067 1345}\r
1346\r
586d81d1
LG
1347#define WARNING_STATUS_NUMBER 5\r
1348#define ERROR_STATUS_NUMBER 33\r
f405c067 1349\r
4c10f953 1350GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 * CONST mStatusString[] = {\r
f405c067 1351 "Success", // RETURN_SUCCESS = 0\r
1352 "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH = 1\r
1353 "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE = 2\r
1354 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE = 3\r
1355 "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL = 4\r
586d81d1 1356 "Warning Stale Data", // RETURN_WARN_STALE_DATA = 5\r
f405c067 1357 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT\r
1358 "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT\r
1359 "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT\r
1360 "Bad Buffer Size", // RETURN_BAD_BUFFER_SIZE = 4 | MAX_BIT\r
1361 "Buffer Too Small", // RETURN_BUFFER_TOO_SMALL, = 5 | MAX_BIT\r
1362 "Not Ready", // RETURN_NOT_READY = 6 | MAX_BIT\r
1363 "Device Error", // RETURN_DEVICE_ERROR = 7 | MAX_BIT\r
1364 "Write Protected", // RETURN_WRITE_PROTECTED = 8 | MAX_BIT\r
1365 "Out of Resources", // RETURN_OUT_OF_RESOURCES = 9 | MAX_BIT\r
1366 "Volume Corrupt", // RETURN_VOLUME_CORRUPTED = 10 | MAX_BIT\r
1367 "Volume Full", // RETURN_VOLUME_FULL = 11 | MAX_BIT\r
1368 "No Media", // RETURN_NO_MEDIA = 12 | MAX_BIT\r
1369 "Media changed", // RETURN_MEDIA_CHANGED = 13 | MAX_BIT\r
1370 "Not Found", // RETURN_NOT_FOUND = 14 | MAX_BIT\r
1371 "Access Denied", // RETURN_ACCESS_DENIED = 15 | MAX_BIT\r
1372 "No Response", // RETURN_NO_RESPONSE = 16 | MAX_BIT\r
1373 "No mapping", // RETURN_NO_MAPPING = 17 | MAX_BIT\r
1374 "Time out", // RETURN_TIMEOUT = 18 | MAX_BIT\r
1375 "Not started", // RETURN_NOT_STARTED = 19 | MAX_BIT\r
1376 "Already started", // RETURN_ALREADY_STARTED = 20 | MAX_BIT\r
1377 "Aborted", // RETURN_ABORTED = 21 | MAX_BIT\r
1378 "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT\r
1379 "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT\r
586d81d1
LG
1380 "Protocol Error", // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT\r
1381 "Incompatible Version", // RETURN_INCOMPATIBLE_VERSION = 25 | MAX_BIT\r
1382 "Security Violation", // RETURN_SECURITY_VIOLATION = 26 | MAX_BIT\r
1383 "CRC Error", // RETURN_CRC_ERROR = 27 | MAX_BIT\r
1384 "End of Media", // RETURN_END_OF_MEDIA = 28 | MAX_BIT\r
1385 "Reserved (29)", // RESERVED = 29 | MAX_BIT\r
1386 "Reserved (30)", // RESERVED = 30 | MAX_BIT\r
1387 "End of File", // RETURN_END_OF_FILE = 31 | MAX_BIT\r
1388 "Invalid Language", // RETURN_INVALID_LANGUAGE = 32 | MAX_BIT\r
1389 "Compromised Data" // RETURN_COMPROMISED_DATA = 33 | MAX_BIT\r
f405c067 1390};\r
1391\r
586d81d1
LG
1392/**\r
1393 Internal function that places the character into the Buffer.\r
1394\r
1395 Internal function that places ASCII or Unicode character into the Buffer.\r
1396\r
1397 @param Buffer The buffer to place the Unicode or ASCII string.\r
1398 @param EndBuffer The end of the input Buffer. No characters will be\r
1399 placed after that. \r
1400 @param Length The count of character to be placed into Buffer.\r
1401 (Negative value indicates no buffer fill.)\r
1402 @param Character The character to be placed into Buffer.\r
1403 @param Increment The character increment in Buffer.\r
1404\r
1405 @return Buffer.\r
1406\r
1407**/\r
1408CHAR8 *\r
1409InternalPrintLibFillBuffer (\r
1410 OUT CHAR8 *Buffer,\r
1411 IN CHAR8 *EndBuffer,\r
1412 IN INTN Length,\r
1413 IN UINTN Character,\r
1414 IN INTN Increment\r
1415 )\r
1416{\r
1417 INTN Index;\r
1418 \r
1419 for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {\r
1420 *Buffer = (CHAR8) Character;\r
1421 if (Increment != 1) {\r
1422 *(Buffer + 1) = (CHAR8)(Character >> 8);\r
1423 }\r
1424 Buffer += Increment;\r
1425 }\r
1426\r
1427 return Buffer;\r
1428}\r
1429\r
f405c067 1430/**\r
1431 Worker function that produces a Null-terminated string in an output buffer \r
1432 based on a Null-terminated format string and a VA_LIST argument list.\r
1433\r
1434 VSPrint function to process format and place the results in Buffer. Since a \r
1435 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
1436 this is the main print working routine.\r
1437\r
1438 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.\r
1439\r
1440 @param[out] Buffer The character buffer to print the results of the \r
1441 parsing of Format into.\r
1442 @param[in] BufferSize The maximum number of characters to put into \r
1443 buffer.\r
1444 @param[in] Flags Initial flags value.\r
1445 Can only have FORMAT_UNICODE, OUTPUT_UNICODE, \r
1446 and COUNT_ONLY_NO_PRINT set.\r
1447 @param[in] Format A Null-terminated format string.\r
1448 @param[in] VaListMarker VA_LIST style variable argument list consumed by\r
1449 processing Format.\r
1450 @param[in] BaseListMarker BASE_LIST style variable argument list consumed\r
1451 by processing Format.\r
1452\r
1453 @return The number of characters printed not including the Null-terminator.\r
1454 If COUNT_ONLY_NO_PRINT was set returns the same, but without any\r
1455 modification to Buffer.\r
1456\r
1457**/\r
1458UINTN\r
1459InternalPrintLibSPrintMarker (\r
1460 OUT CHAR8 *Buffer,\r
1461 IN UINTN BufferSize,\r
1462 IN UINTN Flags,\r
1463 IN CONST CHAR8 *Format,\r
1464 IN VA_LIST VaListMarker, OPTIONAL\r
1465 IN BASE_LIST BaseListMarker OPTIONAL\r
1466 )\r
1467{\r
586d81d1 1468 CHAR8 *OriginalBuffer;\r
f405c067 1469 CHAR8 *EndBuffer;\r
1470 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
1471 UINT32 BytesPerOutputCharacter;\r
1472 UINTN BytesPerFormatCharacter;\r
1473 UINTN FormatMask;\r
1474 UINTN FormatCharacter;\r
1475 UINTN Width;\r
1476 UINTN Precision;\r
1477 INT64 Value;\r
1478 CONST CHAR8 *ArgumentString;\r
1479 UINTN Character;\r
1480 GUID *TmpGuid;\r
1481 TIME *TmpTime;\r
1482 UINTN Count;\r
1483 UINTN ArgumentMask;\r
1484 INTN BytesPerArgumentCharacter;\r
1485 UINTN ArgumentCharacter;\r
1486 BOOLEAN Done;\r
1487 UINTN Index;\r
1488 CHAR8 Prefix;\r
1489 BOOLEAN ZeroPad;\r
1490 BOOLEAN Comma;\r
1491 UINTN Digits;\r
1492 UINTN Radix;\r
1493 RETURN_STATUS Status;\r
1494 UINT32 GuidData1;\r
1495 UINT16 GuidData2;\r
1496 UINT16 GuidData3;\r
be5ec8c5 1497 UINTN LengthToReturn;\r
f405c067 1498\r
1499 //\r
1500 // If you change this code be sure to match the 2 versions of this function.\r
1501 // Nearly identical logic is found in the BasePrintLib and \r
1502 // DxePrintLibPrint2Protocol (both PrintLib instances).\r
1503 //\r
1504\r
50418baa
HW
1505 //\r
1506 // 1. Buffer shall not be a null pointer when both BufferSize > 0 and\r
1507 // COUNT_ONLY_NO_PRINT is not set in Flags.\r
1508 //\r
1509 if ((BufferSize > 0) && ((Flags & COUNT_ONLY_NO_PRINT) == 0)) {\r
1510 SAFE_PRINT_CONSTRAINT_CHECK ((Buffer != NULL), 0);\r
1511 }\r
1512\r
1513 //\r
1514 // 2. Format shall not be a null pointer when BufferSize > 0 or when\r
1515 // COUNT_ONLY_NO_PRINT is set in Flags.\r
1516 //\r
1517 if ((BufferSize > 0) || ((Flags & COUNT_ONLY_NO_PRINT) != 0)) {\r
1518 SAFE_PRINT_CONSTRAINT_CHECK ((Format != NULL), 0);\r
1519 }\r
1520\r
1521 //\r
1522 // 3. BufferSize shall not be greater than RSIZE_MAX for Unicode output or\r
1523 // ASCII_RSIZE_MAX for Ascii output.\r
1524 //\r
1525 if ((Flags & OUTPUT_UNICODE) != 0) {\r
1526 if (RSIZE_MAX != 0) {\r
1527 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= RSIZE_MAX), 0);\r
1528 }\r
1529 BytesPerOutputCharacter = 2;\r
1530 } else {\r
1531 if (ASCII_RSIZE_MAX != 0) {\r
1532 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= ASCII_RSIZE_MAX), 0);\r
1533 }\r
1534 BytesPerOutputCharacter = 1;\r
1535 }\r
1536\r
1537 //\r
1538 // 4. Format shall not contain more than RSIZE_MAX Unicode characters or\r
1539 // ASCII_RSIZE_MAX Ascii characters.\r
1540 //\r
1541 if ((Flags & FORMAT_UNICODE) != 0) {\r
1542 if (RSIZE_MAX != 0) {\r
1543 SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16 *)Format, RSIZE_MAX + 1) <= RSIZE_MAX), 0);\r
1544 }\r
1545 BytesPerFormatCharacter = 2;\r
1546 FormatMask = 0xffff;\r
1547 } else {\r
1548 if (ASCII_RSIZE_MAX != 0) {\r
1549 SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), 0);\r
1550 }\r
1551 BytesPerFormatCharacter = 1;\r
1552 FormatMask = 0xff;\r
1553 }\r
1554\r
586d81d1
LG
1555 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r
1556 if (BufferSize == 0) {\r
1557 Buffer = NULL;\r
1558 }\r
1559 } else {\r
1560 //\r
1561 // We can run without a Buffer for counting only.\r
1562 //\r
1563 if (BufferSize == 0) {\r
1564 return 0;\r
1565 }\r
f405c067 1566 }\r
1567\r
1568 LengthToReturn = 0;\r
586d81d1
LG
1569 EndBuffer = NULL;\r
1570 OriginalBuffer = NULL;\r
f405c067 1571\r
1572 //\r
1573 // Reserve space for the Null terminator.\r
1574 //\r
586d81d1
LG
1575 if (Buffer != NULL) {\r
1576 BufferSize--;\r
1577 OriginalBuffer = Buffer;\r
f405c067 1578\r
586d81d1
LG
1579 //\r
1580 // Set the tag for the end of the input Buffer.\r
1581 //\r
1582 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;\r
1583 }\r
f405c067 1584\r
f405c067 1585 //\r
1586 // Get the first character from the format string\r
1587 //\r
1588 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
1589\r
1590 //\r
1591 // Loop until the end of the format string is reached or the output buffer is full\r
1592 //\r
586d81d1
LG
1593 while (FormatCharacter != 0) {\r
1594 if ((Buffer != NULL) && (Buffer >= EndBuffer)) {\r
1595 break;\r
1596 }\r
f405c067 1597 //\r
1598 // Clear all the flag bits except those that may have been passed in\r
1599 //\r
586d81d1 1600 Flags &= (UINTN) (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);\r
f405c067 1601\r
1602 //\r
1603 // Set the default width to zero, and the default precision to 1\r
1604 //\r
1605 Width = 0;\r
1606 Precision = 1;\r
1607 Prefix = 0;\r
1608 Comma = FALSE;\r
1609 ZeroPad = FALSE;\r
1610 Count = 0;\r
1611 Digits = 0;\r
1612\r
1613 switch (FormatCharacter) {\r
1614 case '%':\r
1615 //\r
1616 // Parse Flags and Width\r
1617 //\r
1618 for (Done = FALSE; !Done; ) {\r
1619 Format += BytesPerFormatCharacter;\r
1620 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
1621 switch (FormatCharacter) {\r
1622 case '.': \r
1623 Flags |= PRECISION; \r
1624 break;\r
1625 case '-': \r
1626 Flags |= LEFT_JUSTIFY; \r
1627 break;\r
1628 case '+': \r
1629 Flags |= PREFIX_SIGN; \r
1630 break;\r
1631 case ' ': \r
1632 Flags |= PREFIX_BLANK; \r
1633 break;\r
1634 case ',': \r
1635 Flags |= COMMA_TYPE; \r
1636 break;\r
1637 case 'L':\r
1638 case 'l': \r
1639 Flags |= LONG_TYPE; \r
1640 break;\r
1641 case '*':\r
1642 if ((Flags & PRECISION) == 0) {\r
1643 Flags |= PAD_TO_WIDTH;\r
1644 if (BaseListMarker == NULL) {\r
1645 Width = VA_ARG (VaListMarker, UINTN);\r
1646 } else {\r
1647 Width = BASE_ARG (BaseListMarker, UINTN);\r
1648 }\r
1649 } else {\r
1650 if (BaseListMarker == NULL) {\r
1651 Precision = VA_ARG (VaListMarker, UINTN);\r
1652 } else {\r
1653 Precision = BASE_ARG (BaseListMarker, UINTN);\r
1654 }\r
1655 }\r
1656 break;\r
1657 case '0':\r
1658 if ((Flags & PRECISION) == 0) {\r
1659 Flags |= PREFIX_ZERO;\r
1660 }\r
1661 case '1':\r
1662 case '2':\r
1663 case '3':\r
1664 case '4':\r
1665 case '5':\r
1666 case '6':\r
1667 case '7':\r
1668 case '8':\r
1669 case '9':\r
1670 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){\r
1671 Count = (Count * 10) + FormatCharacter - '0';\r
1672 Format += BytesPerFormatCharacter;\r
1673 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
1674 }\r
1675 Format -= BytesPerFormatCharacter;\r
1676 if ((Flags & PRECISION) == 0) {\r
1677 Flags |= PAD_TO_WIDTH;\r
1678 Width = Count;\r
1679 } else {\r
1680 Precision = Count;\r
1681 }\r
1682 break;\r
1683 \r
1684 case '\0':\r
1685 //\r
1686 // Make no output if Format string terminates unexpectedly when\r
1687 // looking up for flag, width, precision and type. \r
1688 //\r
1689 Format -= BytesPerFormatCharacter;\r
1690 Precision = 0;\r
1691 //\r
1692 // break skipped on purpose.\r
1693 //\r
1694 default:\r
1695 Done = TRUE;\r
1696 break;\r
1697 }\r
1698 } \r
1699\r
1700 //\r
1701 // Handle each argument type\r
1702 //\r
1703 switch (FormatCharacter) {\r
1704 case 'p':\r
1705 //\r
1706 // Flag space, +, 0, L & l are invalid for type p.\r
1707 //\r
586d81d1 1708 Flags &= ~((UINTN) (PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE));\r
f405c067 1709 if (sizeof (VOID *) > 4) {\r
1710 Flags |= LONG_TYPE;\r
1711 }\r
586d81d1
LG
1712 //\r
1713 // break skipped on purpose\r
1714 //\r
f405c067 1715 case 'X':\r
1716 Flags |= PREFIX_ZERO;\r
1717 //\r
1718 // break skipped on purpose\r
1719 //\r
1720 case 'x':\r
1721 Flags |= RADIX_HEX;\r
1722 //\r
1723 // break skipped on purpose\r
1724 //\r
796fecd8
QS
1725 case 'u':\r
1726 if ((Flags & RADIX_HEX) == 0) {\r
1727 Flags &= ~((UINTN) (PREFIX_SIGN));\r
1728 Flags |= UNSIGNED_TYPE;\r
1729 }\r
1730 //\r
1731 // break skipped on purpose\r
1732 //\r
f405c067 1733 case 'd':\r
1734 if ((Flags & LONG_TYPE) == 0) {\r
1735 //\r
796fecd8 1736 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
f405c067 1737 // This assumption is made so the format string definition is compatible with the ANSI C\r
1738 // Specification for formatted strings. It is recommended that the Base Types be used \r
1739 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
1740 // provides an implementation that is compatible with that largest possible set of CPU \r
1741 // architectures. This is why the type "int" is used in this one case.\r
1742 //\r
1743 if (BaseListMarker == NULL) {\r
1744 Value = VA_ARG (VaListMarker, int);\r
1745 } else {\r
1746 Value = BASE_ARG (BaseListMarker, int);\r
1747 }\r
1748 } else {\r
1749 if (BaseListMarker == NULL) {\r
1750 Value = VA_ARG (VaListMarker, INT64);\r
1751 } else {\r
1752 Value = BASE_ARG (BaseListMarker, INT64);\r
1753 }\r
1754 }\r
1755 if ((Flags & PREFIX_BLANK) != 0) {\r
1756 Prefix = ' ';\r
1757 }\r
1758 if ((Flags & PREFIX_SIGN) != 0) {\r
1759 Prefix = '+';\r
1760 }\r
1761 if ((Flags & COMMA_TYPE) != 0) {\r
1762 Comma = TRUE;\r
1763 }\r
1764 if ((Flags & RADIX_HEX) == 0) {\r
1765 Radix = 10;\r
1766 if (Comma) {\r
586d81d1 1767 Flags &= ~((UINTN) PREFIX_ZERO);\r
f405c067 1768 Precision = 1;\r
1769 }\r
796fecd8 1770 if (Value < 0 && (Flags & UNSIGNED_TYPE) == 0) {\r
f405c067 1771 Flags |= PREFIX_SIGN;\r
1772 Prefix = '-';\r
1773 Value = -Value;\r
796fecd8
QS
1774 } else if ((Flags & UNSIGNED_TYPE) != 0 && (Flags & LONG_TYPE) == 0) {\r
1775 //\r
1776 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
1777 // This assumption is made so the format string definition is compatible with the ANSI C\r
1778 // Specification for formatted strings. It is recommended that the Base Types be used \r
1779 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
1780 // provides an implementation that is compatible with that largest possible set of CPU \r
1781 // architectures. This is why the type "unsigned int" is used in this one case.\r
1782 //\r
1783 Value = (unsigned int)Value;\r
f405c067 1784 }\r
1785 } else {\r
1786 Radix = 16;\r
1787 Comma = FALSE;\r
1788 if ((Flags & LONG_TYPE) == 0 && Value < 0) {\r
1789 //\r
796fecd8 1790 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
f405c067 1791 // This assumption is made so the format string definition is compatible with the ANSI C\r
1792 // Specification for formatted strings. It is recommended that the Base Types be used \r
1793 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
1794 // provides an implementation that is compatible with that largest possible set of CPU \r
1795 // architectures. This is why the type "unsigned int" is used in this one case.\r
1796 //\r
1797 Value = (unsigned int)Value;\r
1798 }\r
1799 }\r
1800 //\r
1801 // Convert Value to a reversed string\r
1802 //\r
1803 Count = InternalPrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;\r
1804 if (Value == 0 && Precision == 0) {\r
1805 Count = 0;\r
1806 }\r
1807 ArgumentString = (CHAR8 *)ValueBuffer + Count;\r
1808 \r
1809 Digits = Count % 3;\r
1810 if (Digits != 0) {\r
1811 Digits = 3 - Digits;\r
1812 }\r
1813 if (Comma && Count != 0) {\r
1814 Count += ((Count - 1) / 3);\r
1815 }\r
1816 if (Prefix != 0) {\r
1817 Count++;\r
1818 Precision++;\r
1819 }\r
1820 Flags |= ARGUMENT_REVERSED;\r
1821 ZeroPad = TRUE;\r
1822 if ((Flags & PREFIX_ZERO) != 0) {\r
1823 if ((Flags & LEFT_JUSTIFY) == 0) {\r
1824 if ((Flags & PAD_TO_WIDTH) != 0) {\r
1825 if ((Flags & PRECISION) == 0) {\r
1826 Precision = Width;\r
1827 }\r
1828 }\r
1829 }\r
1830 }\r
1831 break;\r
1832\r
1833 case 's':\r
1834 case 'S':\r
1835 Flags |= ARGUMENT_UNICODE;\r
1836 //\r
1837 // break skipped on purpose\r
1838 //\r
1839 case 'a':\r
1840 if (BaseListMarker == NULL) {\r
1841 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);\r
1842 } else {\r
1843 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);\r
1844 }\r
1845 if (ArgumentString == NULL) {\r
6e1e5405 1846 Flags &= (~(UINTN)ARGUMENT_UNICODE);\r
f405c067 1847 ArgumentString = "<null string>";\r
1848 }\r
1849 //\r
1850 // Set the default precision for string to be zero if not specified.\r
1851 //\r
1852 if ((Flags & PRECISION) == 0) {\r
1853 Precision = 0;\r
1854 }\r
1855 break;\r
1856\r
1857 case 'c':\r
1858 if (BaseListMarker == NULL) {\r
1859 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;\r
1860 } else {\r
1861 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;\r
1862 }\r
1863 ArgumentString = (CHAR8 *)&Character;\r
1864 Flags |= ARGUMENT_UNICODE;\r
1865 break;\r
1866\r
1867 case 'g':\r
1868 if (BaseListMarker == NULL) {\r
1869 TmpGuid = VA_ARG (VaListMarker, GUID *);\r
1870 } else {\r
1871 TmpGuid = BASE_ARG (BaseListMarker, GUID *);\r
1872 }\r
1873 if (TmpGuid == NULL) {\r
1874 ArgumentString = "<null guid>";\r
1875 } else {\r
1876 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));\r
1877 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));\r
1878 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));\r
1879 InternalPrintLibSPrint (\r
1880 ValueBuffer,\r
1881 MAXIMUM_VALUE_CHARACTERS, \r
1882 0,\r
1883 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
1884 GuidData1,\r
1885 GuidData2,\r
1886 GuidData3,\r
1887 TmpGuid->Data4[0],\r
1888 TmpGuid->Data4[1],\r
1889 TmpGuid->Data4[2],\r
1890 TmpGuid->Data4[3],\r
1891 TmpGuid->Data4[4],\r
1892 TmpGuid->Data4[5],\r
1893 TmpGuid->Data4[6],\r
1894 TmpGuid->Data4[7]\r
1895 );\r
1896 ArgumentString = ValueBuffer;\r
1897 }\r
1898 break;\r
1899\r
1900 case 't':\r
1901 if (BaseListMarker == NULL) {\r
1902 TmpTime = VA_ARG (VaListMarker, TIME *); \r
1903 } else {\r
1904 TmpTime = BASE_ARG (BaseListMarker, TIME *); \r
1905 }\r
1906 if (TmpTime == NULL) {\r
1907 ArgumentString = "<null time>";\r
1908 } else {\r
1909 InternalPrintLibSPrint (\r
1910 ValueBuffer,\r
1911 MAXIMUM_VALUE_CHARACTERS,\r
1912 0,\r
1913 "%02d/%02d/%04d %02d:%02d",\r
1914 TmpTime->Month,\r
1915 TmpTime->Day,\r
1916 TmpTime->Year,\r
1917 TmpTime->Hour,\r
1918 TmpTime->Minute\r
1919 );\r
1920 ArgumentString = ValueBuffer;\r
1921 }\r
1922 break;\r
1923\r
1924 case 'r':\r
1925 if (BaseListMarker == NULL) {\r
1926 Status = VA_ARG (VaListMarker, RETURN_STATUS);\r
1927 } else {\r
1928 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);\r
1929 }\r
1930 ArgumentString = ValueBuffer;\r
1931 if (RETURN_ERROR (Status)) {\r
1932 //\r
1933 // Clear error bit\r
1934 //\r
1935 Index = Status & ~MAX_BIT;\r
1936 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {\r
1937 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];\r
1938 }\r
1939 } else {\r
1940 Index = Status;\r
1941 if (Index <= WARNING_STATUS_NUMBER) {\r
1942 ArgumentString = mStatusString [Index];\r
1943 }\r
1944 }\r
1945 if (ArgumentString == ValueBuffer) {\r
1946 InternalPrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);\r
1947 }\r
1948 break;\r
1949\r
1950 case '\r':\r
1951 Format += BytesPerFormatCharacter;\r
1952 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
1953 if (FormatCharacter == '\n') {\r
1954 //\r
1955 // Translate '\r\n' to '\r\n'\r
1956 //\r
1957 ArgumentString = "\r\n";\r
1958 } else {\r
1959 //\r
1960 // Translate '\r' to '\r'\r
1961 //\r
1962 ArgumentString = "\r";\r
1963 Format -= BytesPerFormatCharacter;\r
1964 }\r
1965 break;\r
1966\r
1967 case '\n':\r
1968 //\r
1969 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
1970 //\r
1971 ArgumentString = "\r\n";\r
1972 Format += BytesPerFormatCharacter;\r
1973 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
1974 if (FormatCharacter != '\r') {\r
1975 Format -= BytesPerFormatCharacter;\r
1976 }\r
1977 break;\r
1978\r
1979 case '%':\r
1980 default:\r
1981 //\r
1982 // if the type is '%' or unknown, then print it to the screen\r
1983 //\r
1984 ArgumentString = (CHAR8 *)&FormatCharacter;\r
1985 Flags |= ARGUMENT_UNICODE;\r
1986 break;\r
1987 }\r
1988 break;\r
1989 \r
1990 case '\r':\r
1991 Format += BytesPerFormatCharacter;\r
1992 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
1993 if (FormatCharacter == '\n') {\r
1994 //\r
1995 // Translate '\r\n' to '\r\n'\r
1996 //\r
1997 ArgumentString = "\r\n";\r
1998 } else {\r
1999 //\r
2000 // Translate '\r' to '\r'\r
2001 //\r
2002 ArgumentString = "\r";\r
2003 Format -= BytesPerFormatCharacter;\r
2004 }\r
2005 break;\r
2006\r
2007 case '\n':\r
2008 //\r
2009 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
2010 //\r
2011 ArgumentString = "\r\n";\r
2012 Format += BytesPerFormatCharacter;\r
2013 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
2014 if (FormatCharacter != '\r') {\r
2015 Format -= BytesPerFormatCharacter;\r
2016 }\r
2017 break;\r
2018\r
2019 default:\r
2020 ArgumentString = (CHAR8 *)&FormatCharacter;\r
2021 Flags |= ARGUMENT_UNICODE;\r
2022 break;\r
2023 }\r
2024\r
2025 //\r
2026 // Retrieve the ArgumentString attriubutes\r
2027 //\r
2028 if ((Flags & ARGUMENT_UNICODE) != 0) {\r
2029 ArgumentMask = 0xffff;\r
2030 BytesPerArgumentCharacter = 2;\r
2031 } else {\r
2032 ArgumentMask = 0xff;\r
2033 BytesPerArgumentCharacter = 1;\r
2034 }\r
2035 if ((Flags & ARGUMENT_REVERSED) != 0) {\r
2036 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;\r
2037 } else {\r
2038 //\r
2039 // Compute the number of characters in ArgumentString and store it in Count\r
2040 // ArgumentString is either null-terminated, or it contains Precision characters\r
2041 //\r
2042 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {\r
2043 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;\r
2044 if (ArgumentCharacter == 0) {\r
2045 break;\r
2046 }\r
2047 }\r
2048 }\r
2049\r
2050 if (Precision < Count) {\r
2051 Precision = Count;\r
2052 }\r
2053\r
2054 //\r
2055 // Pad before the string\r
2056 //\r
2057 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {\r
6bae492f 2058 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
586d81d1
LG
2059 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2060 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
2061 }\r
f405c067 2062 }\r
2063\r
2064 if (ZeroPad) {\r
2065 if (Prefix != 0) {\r
6bae492f 2066 LengthToReturn += (1 * BytesPerOutputCharacter);\r
586d81d1
LG
2067 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2068 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
2069 }\r
f405c067 2070 }\r
6bae492f 2071 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
586d81d1
LG
2072 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2073 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);\r
2074 }\r
f405c067 2075 } else {\r
6bae492f 2076 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
586d81d1
LG
2077 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2078 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);\r
2079 }\r
f405c067 2080 if (Prefix != 0) {\r
6bae492f 2081 LengthToReturn += (1 * BytesPerOutputCharacter);\r
586d81d1
LG
2082 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2083 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
2084 }\r
f405c067 2085 }\r
2086 }\r
2087\r
2088 //\r
2089 // Output the Prefix character if it is present\r
2090 //\r
2091 Index = 0;\r
2092 if (Prefix != 0) {\r
2093 Index++;\r
2094 }\r
2095\r
2096 //\r
2097 // Copy the string into the output buffer performing the required type conversions\r
2098 //\r
2099 while (Index < Count) {\r
2100 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;\r
2101\r
6bae492f 2102 LengthToReturn += (1 * BytesPerOutputCharacter);\r
586d81d1
LG
2103 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2104 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);\r
2105 }\r
f405c067 2106 ArgumentString += BytesPerArgumentCharacter;\r
2107 Index++;\r
2108 if (Comma) {\r
2109 Digits++;\r
2110 if (Digits == 3) {\r
2111 Digits = 0;\r
2112 Index++;\r
2113 if (Index < Count) {\r
6bae492f 2114 LengthToReturn += (1 * BytesPerOutputCharacter);\r
586d81d1
LG
2115 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2116 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);\r
2117 }\r
f405c067 2118 }\r
2119 }\r
2120 }\r
2121 }\r
2122\r
2123 //\r
2124 // Pad after the string\r
2125 //\r
2126 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {\r
6bae492f 2127 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
586d81d1
LG
2128 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2129 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
2130 }\r
f405c067 2131 }\r
2132\r
2133 //\r
2134 // Get the next character from the format string\r
2135 //\r
2136 Format += BytesPerFormatCharacter;\r
2137\r
2138 //\r
2139 // Get the next character from the format string\r
2140 //\r
2141 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
2142 }\r
2143\r
586d81d1
LG
2144 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r
2145 return (LengthToReturn / BytesPerOutputCharacter);\r
2146 }\r
2147\r
2148 ASSERT (Buffer != NULL);\r
2149 //\r
2150 // Null terminate the Unicode or ASCII string\r
2151 //\r
2152 InternalPrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);\r
586d81d1
LG
2153\r
2154 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);\r
f405c067 2155}\r
2156\r
2157/**\r
2158 Returns the number of characters that would be produced by if the formatted \r
2159 output were produced not including the Null-terminator.\r
2160\r
f405c067 2161 If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
2162\r
50418baa
HW
2163 If FormatString is NULL, then ASSERT() and 0 is returned.\r
2164 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more\r
2165 than PcdMaximumUnicodeStringLength Unicode characters not including the\r
2166 Null-terminator, then ASSERT() and 0 is returned.\r
2167\r
f405c067 2168 @param[in] FormatString A Null-terminated Unicode format string.\r
2169 @param[in] Marker VA_LIST marker for the variable argument list.\r
2170\r
2171 @return The number of characters that would be produced, not including the \r
2172 Null-terminator.\r
2173**/\r
2174UINTN\r
2175EFIAPI\r
2176SPrintLength (\r
2177 IN CONST CHAR16 *FormatString,\r
2178 IN VA_LIST Marker\r
2179 )\r
2180{\r
50418baa 2181 ASSERT_UNICODE_BUFFER (FormatString);\r
f405c067 2182 return InternalPrintLibSPrintMarker (NULL, 0, FORMAT_UNICODE | OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);\r
2183}\r
2184\r
2185/**\r
2186 Returns the number of characters that would be produced by if the formatted \r
2187 output were produced not including the Null-terminator.\r
2188\r
50418baa
HW
2189 If FormatString is NULL, then ASSERT() and 0 is returned.\r
2190 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more\r
2191 than PcdMaximumAsciiStringLength Ascii characters not including the\r
2192 Null-terminator, then ASSERT() and 0 is returned.\r
f405c067 2193\r
2194 @param[in] FormatString A Null-terminated ASCII format string.\r
2195 @param[in] Marker VA_LIST marker for the variable argument list.\r
2196\r
2197 @return The number of characters that would be produced, not including the \r
2198 Null-terminator.\r
2199**/\r
2200UINTN\r
2201EFIAPI\r
2202SPrintLengthAsciiFormat (\r
2203 IN CONST CHAR8 *FormatString,\r
2204 IN VA_LIST Marker\r
2205 )\r
2206{\r
f405c067 2207 return InternalPrintLibSPrintMarker (NULL, 0, OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);\r
2208}\r