]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c
MdePkg/BasePrintLib: Refine the comment for AsciiValueToStringS API
[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
809e2bbf 229 if (((UINTN)BaseListMarker - (UINTN)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
695e4c49
HW
601#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
602\r
2ad4dad0 603/**\r
695e4c49
HW
604 [ATTENTION] This function is deprecated for security reason.\r
605\r
2ad4dad0
LG
606 Converts a decimal value to a Null-terminated Unicode string.\r
607 \r
608 Converts the decimal number specified by Value to a Null-terminated Unicode \r
609 string specified by Buffer containing at most Width characters. No padding of spaces \r
610 is ever performed. If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
611 The number of Unicode characters in Buffer is returned not including the Null-terminator.\r
612 If the conversion contains more than Width characters, then only the first\r
613 Width characters are returned, and the total number of characters \r
614 required to perform the conversion is returned.\r
615 Additional conversion parameters are specified in Flags. \r
616 \r
617 The Flags bit LEFT_JUSTIFY is always ignored.\r
618 All conversions are left justified in Buffer.\r
619 If Width is 0, PREFIX_ZERO is ignored in Flags.\r
620 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r
621 are inserted every 3rd digit starting from the right.\r
df8d0595 622 If RADIX_HEX is set in Flags, then the output buffer will be \r
2ad4dad0 623 formatted in hexadecimal format.\r
df8d0595 624 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in Buffer is a '-'.\r
2ad4dad0
LG
625 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r
626 then Buffer is padded with '0' characters so the combination of the optional '-' \r
627 sign character, '0' characters, digit characters for Value, and the Null-terminator\r
628 add up to Width characters.\r
df8d0595 629 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().\r
2ad4dad0
LG
630 If Buffer is NULL, then ASSERT().\r
631 If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r
632 If unsupported bits are set in Flags, then ASSERT().\r
df8d0595 633 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().\r
2ad4dad0
LG
634 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r
635\r
636 @param Buffer Pointer to the output buffer for the produced Null-terminated\r
637 Unicode string.\r
638 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.\r
639 @param Value The 64-bit signed value to convert to a string.\r
640 @param Width The maximum number of Unicode characters to place in Buffer, not including\r
641 the Null-terminator.\r
642 \r
643 @return The number of Unicode characters in Buffer not including the Null-terminator.\r
644\r
645**/\r
646UINTN\r
647EFIAPI\r
648UnicodeValueToString (\r
649 IN OUT CHAR16 *Buffer,\r
650 IN UINTN Flags,\r
651 IN INT64 Value,\r
652 IN UINTN Width\r
653 )\r
654{\r
f0c74f5a
HW
655 RETURN_STATUS Status;\r
656 UINTN BufferSize;\r
657\r
658 if (Width == 0) {\r
659 BufferSize = (MAXIMUM_VALUE_CHARACTERS + 1) * sizeof (CHAR16);\r
660 } else {\r
661 BufferSize = (Width + 1) * sizeof (CHAR16);\r
662 }\r
663\r
664 Status = mPrint2SProtocol->UnicodeValueToStringS (Buffer, BufferSize, Flags, Value, Width);\r
665 if (RETURN_ERROR (Status)) {\r
666 return 0;\r
667 }\r
668\r
669 return StrnLenS (Buffer, BufferSize / sizeof (CHAR16));\r
2ad4dad0 670}\r
a0afd019 671\r
695e4c49
HW
672#endif\r
673\r
9e32e971
HW
674/**\r
675 Converts a decimal value to a Null-terminated Unicode string.\r
676\r
677 Converts the decimal number specified by Value to a Null-terminated Unicode\r
678 string specified by Buffer containing at most Width characters. No padding of\r
679 spaces is ever performed. If Width is 0 then a width of\r
680 MAXIMUM_VALUE_CHARACTERS is assumed. If the conversion contains more than\r
681 Width characters, then only the first Width characters are placed in Buffer.\r
682 Additional conversion parameters are specified in Flags.\r
683\r
684 The Flags bit LEFT_JUSTIFY is always ignored.\r
685 All conversions are left justified in Buffer.\r
686 If Width is 0, PREFIX_ZERO is ignored in Flags.\r
687 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and\r
688 commas are inserted every 3rd digit starting from the right.\r
689 If RADIX_HEX is set in Flags, then the output buffer will be formatted in\r
690 hexadecimal format.\r
691 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in\r
692 Buffer is a '-'.\r
693 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, then\r
694 Buffer is padded with '0' characters so the combination of the optional '-'\r
695 sign character, '0' characters, digit characters for Value, and the\r
696 Null-terminator add up to Width characters.\r
697\r
698 If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r
699 If an error would be returned, then the function will also ASSERT().\r
700\r
701 @param Buffer The pointer to the output buffer for the produced\r
702 Null-terminated Unicode string.\r
703 @param BufferSize The size of Buffer in bytes, including the\r
704 Null-terminator.\r
705 @param Flags The bitmask of flags that specify left justification,\r
706 zero pad, and commas.\r
707 @param Value The 64-bit signed value to convert to a string.\r
708 @param Width The maximum number of Unicode characters to place in\r
709 Buffer, not including the Null-terminator.\r
710\r
711 @retval RETURN_SUCCESS The decimal value is converted.\r
712 @retval RETURN_BUFFER_TOO_SMALL If BufferSize cannot hold the converted\r
713 value.\r
714 @retval RETURN_INVALID_PARAMETER If Buffer is NULL.\r
715 If PcdMaximumUnicodeStringLength is not\r
716 zero, and BufferSize is greater than\r
717 (PcdMaximumUnicodeStringLength *\r
718 sizeof (CHAR16) + 1).\r
719 If unsupported bits are set in Flags.\r
720 If both COMMA_TYPE and RADIX_HEX are set in\r
721 Flags.\r
722 If Width >= MAXIMUM_VALUE_CHARACTERS.\r
723\r
724**/\r
725RETURN_STATUS\r
726EFIAPI\r
727UnicodeValueToStringS (\r
728 IN OUT CHAR16 *Buffer,\r
729 IN UINTN BufferSize,\r
730 IN UINTN Flags,\r
731 IN INT64 Value,\r
732 IN UINTN Width\r
733 )\r
734{\r
735 return mPrint2SProtocol->UnicodeValueToStringS (Buffer, BufferSize, Flags, Value, Width);\r
736}\r
737\r
2ad4dad0
LG
738/**\r
739 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
740 ASCII format string and a VA_LIST argument list.\r
50418baa
HW
741\r
742 This function is similar as vsnprintf_s defined in C11.\r
743\r
2ad4dad0
LG
744 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
745 and BufferSize.\r
746 The ASCII string is produced by parsing the format string specified by FormatString.\r
50418baa 747 Arguments are pulled from the variable argument list specified by Marker based on\r
2ad4dad0
LG
748 the contents of the format string.\r
749 The number of ASCII characters in the produced output buffer is returned not including\r
750 the Null-terminator.\r
2ad4dad0 751\r
50418baa
HW
752 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
753 unmodified and 0 is returned.\r
754 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
755 unmodified and 0 is returned.\r
756 If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
757 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
758 is unmodified and 0 is returned.\r
2ad4dad0 759 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
50418baa
HW
760 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
761 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
762\r
763 If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
2ad4dad0 764\r
50418baa 765 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
2ad4dad0
LG
766 ASCII string.\r
767 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa 768 @param FormatString A Null-terminated ASCII format string.\r
2ad4dad0 769 @param Marker VA_LIST marker for the variable argument list.\r
50418baa 770\r
2ad4dad0
LG
771 @return The number of ASCII characters in the produced output buffer not including the\r
772 Null-terminator.\r
773\r
774**/\r
775UINTN\r
776EFIAPI\r
777AsciiVSPrint (\r
778 OUT CHAR8 *StartOfBuffer,\r
779 IN UINTN BufferSize,\r
780 IN CONST CHAR8 *FormatString,\r
781 IN VA_LIST Marker\r
782 )\r
a0afd019 783{\r
7c6075e2
HW
784 UINT64 BaseListMarker[256 / sizeof (UINT64)];\r
785 BOOLEAN Converted;\r
786\r
787 Converted = DxePrintLibPrint2ProtocolVaListToBaseList (\r
788 TRUE,\r
789 FormatString,\r
790 Marker,\r
791 (BASE_LIST)BaseListMarker,\r
792 sizeof (BaseListMarker) - 8\r
793 );\r
794 if (!Converted) {\r
795 return 0;\r
796 }\r
504dcb0a 797\r
798 return AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);\r
799}\r
800\r
801/**\r
802 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
803 ASCII format string and a BASE_LIST argument list.\r
50418baa 804\r
504dcb0a 805 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
806 and BufferSize.\r
807 The ASCII string is produced by parsing the format string specified by FormatString.\r
50418baa 808 Arguments are pulled from the variable argument list specified by Marker based on\r
504dcb0a 809 the contents of the format string.\r
810 The number of ASCII characters in the produced output buffer is returned not including\r
811 the Null-terminator.\r
504dcb0a 812\r
50418baa
HW
813 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
814 unmodified and 0 is returned.\r
815 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
816 unmodified and 0 is returned.\r
817 If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
818 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
819 is unmodified and 0 is returned.\r
504dcb0a 820 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
50418baa
HW
821 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
822 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
504dcb0a 823\r
50418baa
HW
824 If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
825\r
826 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
504dcb0a 827 ASCII string.\r
828 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa 829 @param FormatString A Null-terminated ASCII format string.\r
504dcb0a 830 @param Marker BASE_LIST marker for the variable argument list.\r
50418baa 831\r
504dcb0a 832 @return The number of ASCII characters in the produced output buffer not including the\r
833 Null-terminator.\r
834\r
835**/\r
836UINTN\r
837EFIAPI\r
838AsciiBSPrint (\r
839 OUT CHAR8 *StartOfBuffer,\r
840 IN UINTN BufferSize,\r
841 IN CONST CHAR8 *FormatString,\r
842 IN BASE_LIST Marker\r
843 )\r
844{\r
f0c74f5a 845 return mPrint2SProtocol->AsciiBSPrint (StartOfBuffer, BufferSize, FormatString, Marker);\r
a0afd019 846}\r
847\r
2ad4dad0
LG
848/**\r
849 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
850 ASCII format string and variable argument list.\r
50418baa
HW
851\r
852 This function is similar as snprintf_s defined in C11.\r
853\r
2ad4dad0
LG
854 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
855 and BufferSize.\r
856 The ASCII string is produced by parsing the format string specified by FormatString.\r
50418baa 857 Arguments are pulled from the variable argument list based on the contents of the\r
2ad4dad0
LG
858 format string.\r
859 The number of ASCII characters in the produced output buffer is returned not including\r
860 the Null-terminator.\r
2ad4dad0 861\r
50418baa
HW
862 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
863 unmodified and 0 is returned.\r
864 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
865 unmodified and 0 is returned.\r
866 If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
867 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
868 is unmodified and 0 is returned.\r
2ad4dad0 869 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
50418baa
HW
870 PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
871 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
872\r
873 If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
2ad4dad0 874\r
50418baa 875 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
2ad4dad0
LG
876 ASCII string.\r
877 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa
HW
878 @param FormatString A Null-terminated ASCII format string.\r
879 @param ... Variable argument list whose contents are accessed based on the\r
71898234 880 format string specified by FormatString.\r
ce95aa7a 881\r
2ad4dad0
LG
882 @return The number of ASCII characters in the produced output buffer not including the\r
883 Null-terminator.\r
884\r
885**/\r
a0afd019 886UINTN\r
2ad4dad0 887EFIAPI\r
a0afd019 888AsciiSPrint (\r
889 OUT CHAR8 *StartOfBuffer,\r
890 IN UINTN BufferSize,\r
2ad4dad0 891 IN CONST CHAR8 *FormatString,\r
a0afd019 892 ...\r
893 )\r
2ad4dad0
LG
894{\r
895 VA_LIST Marker;\r
3bbe68a3 896 UINTN NumberOfPrinted;\r
2ad4dad0
LG
897\r
898 VA_START (Marker, FormatString);\r
3bbe68a3 899 NumberOfPrinted = AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);\r
900 VA_END (Marker);\r
901 return NumberOfPrinted;\r
2ad4dad0 902}\r
a0afd019 903\r
2ad4dad0
LG
904/**\r
905 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
50418baa
HW
906 Unicode format string and a VA_LIST argument list.\r
907\r
908 This function is similar as vsnprintf_s defined in C11.\r
909\r
2ad4dad0
LG
910 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
911 and BufferSize.\r
912 The ASCII string is produced by parsing the format string specified by FormatString.\r
50418baa 913 Arguments are pulled from the variable argument list specified by Marker based on\r
2ad4dad0
LG
914 the contents of the format string.\r
915 The number of ASCII characters in the produced output buffer is returned not including\r
916 the Null-terminator.\r
2ad4dad0 917\r
50418baa
HW
918 If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
919\r
920 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
921 unmodified and 0 is returned.\r
922 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
923 unmodified and 0 is returned.\r
924 If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
925 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
926 is unmodified and 0 is returned.\r
2ad4dad0
LG
927 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
928 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
50418baa 929 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
2ad4dad0 930\r
50418baa
HW
931 If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
932\r
933 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
2ad4dad0
LG
934 ASCII string.\r
935 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa 936 @param FormatString A Null-terminated Unicode format string.\r
2ad4dad0 937 @param Marker VA_LIST marker for the variable argument list.\r
50418baa 938\r
2ad4dad0
LG
939 @return The number of ASCII characters in the produced output buffer not including the\r
940 Null-terminator.\r
941\r
942**/\r
943UINTN\r
944EFIAPI\r
945AsciiVSPrintUnicodeFormat (\r
946 OUT CHAR8 *StartOfBuffer,\r
947 IN UINTN BufferSize,\r
948 IN CONST CHAR16 *FormatString,\r
949 IN VA_LIST Marker\r
950 )\r
a0afd019 951{\r
7c6075e2
HW
952 UINT64 BaseListMarker[256 / sizeof (UINT64)];\r
953 BOOLEAN Converted;\r
954\r
50418baa
HW
955 ASSERT_UNICODE_BUFFER (FormatString);\r
956\r
7c6075e2
HW
957 Converted = DxePrintLibPrint2ProtocolVaListToBaseList (\r
958 FALSE,\r
959 (CHAR8 *)FormatString,\r
960 Marker,\r
961 (BASE_LIST)BaseListMarker,\r
962 sizeof (BaseListMarker) - 8\r
963 );\r
964 if (!Converted) {\r
965 return 0;\r
966 }\r
504dcb0a 967\r
968 return AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, (BASE_LIST)BaseListMarker);\r
969}\r
970\r
971/**\r
972 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
50418baa
HW
973 Unicode format string and a BASE_LIST argument list.\r
974\r
504dcb0a 975 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
976 and BufferSize.\r
977 The ASCII string is produced by parsing the format string specified by FormatString.\r
50418baa 978 Arguments are pulled from the variable argument list specified by Marker based on\r
504dcb0a 979 the contents of the format string.\r
980 The number of ASCII characters in the produced output buffer is returned not including\r
981 the Null-terminator.\r
504dcb0a 982\r
50418baa
HW
983 If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
984\r
985 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
986 unmodified and 0 is returned.\r
987 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
988 unmodified and 0 is returned.\r
989 If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
990 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
991 is unmodified and 0 is returned.\r
504dcb0a 992 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
993 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
50418baa 994 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
504dcb0a 995\r
50418baa
HW
996 If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
997\r
998 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
504dcb0a 999 ASCII string.\r
1000 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa 1001 @param FormatString A Null-terminated Unicode format string.\r
504dcb0a 1002 @param Marker BASE_LIST marker for the variable argument list.\r
50418baa 1003\r
504dcb0a 1004 @return The number of ASCII characters in the produced output buffer not including the\r
1005 Null-terminator.\r
1006\r
1007**/\r
1008UINTN\r
1009EFIAPI\r
1010AsciiBSPrintUnicodeFormat (\r
1011 OUT CHAR8 *StartOfBuffer,\r
1012 IN UINTN BufferSize,\r
1013 IN CONST CHAR16 *FormatString,\r
1014 IN BASE_LIST Marker\r
1015 )\r
1016{\r
50418baa 1017 ASSERT_UNICODE_BUFFER (FormatString);\r
f0c74f5a 1018 return mPrint2SProtocol->AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);\r
2ad4dad0
LG
1019}\r
1020\r
1021/**\r
1022 Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
50418baa
HW
1023 Unicode format string and variable argument list.\r
1024\r
1025 This function is similar as snprintf_s defined in C11.\r
1026\r
2ad4dad0
LG
1027 Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
1028 and BufferSize.\r
1029 The ASCII string is produced by parsing the format string specified by FormatString.\r
50418baa 1030 Arguments are pulled from the variable argument list based on the contents of the\r
2ad4dad0
LG
1031 format string.\r
1032 The number of ASCII characters in the produced output buffer is returned not including\r
1033 the Null-terminator.\r
2ad4dad0 1034\r
50418baa
HW
1035 If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
1036\r
1037 If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
1038 unmodified and 0 is returned.\r
1039 If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
1040 unmodified and 0 is returned.\r
1041 If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
1042 (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
1043 is unmodified and 0 is returned.\r
2ad4dad0
LG
1044 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
1045 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
50418baa 1046 ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
2ad4dad0 1047\r
50418baa
HW
1048 If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
1049\r
1050 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
2ad4dad0
LG
1051 ASCII string.\r
1052 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
50418baa
HW
1053 @param FormatString A Null-terminated Unicode format string.\r
1054 @param ... Variable argument list whose contents are accessed based on the\r
71898234 1055 format string specified by FormatString.\r
ce95aa7a 1056\r
2ad4dad0
LG
1057 @return The number of ASCII characters in the produced output buffer not including the\r
1058 Null-terminator.\r
1059\r
1060**/\r
1061UINTN\r
1062EFIAPI\r
1063AsciiSPrintUnicodeFormat (\r
1064 OUT CHAR8 *StartOfBuffer,\r
1065 IN UINTN BufferSize,\r
1066 IN CONST CHAR16 *FormatString,\r
1067 ...\r
1068 )\r
1069{\r
1070 VA_LIST Marker;\r
3bbe68a3 1071 UINTN NumberOfPrinted;\r
a0afd019 1072\r
1073 VA_START (Marker, FormatString);\r
3bbe68a3 1074 NumberOfPrinted = AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);\r
1075 VA_END (Marker);\r
1076 return NumberOfPrinted;\r
2ad4dad0
LG
1077}\r
1078\r
1079\r
695e4c49
HW
1080#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
1081\r
2ad4dad0 1082/**\r
695e4c49
HW
1083 [ATTENTION] This function is deprecated for security reason.\r
1084\r
2ad4dad0
LG
1085 Converts a decimal value to a Null-terminated ASCII string.\r
1086 \r
1087 Converts the decimal number specified by Value to a Null-terminated ASCII string \r
1088 specified by Buffer containing at most Width characters. No padding of spaces \r
1089 is ever performed.\r
1090 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
1091 The number of ASCII characters in Buffer is returned not including the Null-terminator.\r
1092 If the conversion contains more than Width characters, then only the first Width\r
1093 characters are returned, and the total number of characters required to perform\r
1094 the conversion is returned.\r
1095 Additional conversion parameters are specified in Flags. \r
1096 The Flags bit LEFT_JUSTIFY is always ignored.\r
1097 All conversions are left justified in Buffer.\r
1098 If Width is 0, PREFIX_ZERO is ignored in Flags.\r
1099 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r
1100 are inserted every 3rd digit starting from the right.\r
df8d0595 1101 If RADIX_HEX is set in Flags, then the output buffer will be \r
2ad4dad0 1102 formatted in hexadecimal format.\r
df8d0595 1103 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in Buffer is a '-'.\r
2ad4dad0
LG
1104 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r
1105 then Buffer is padded with '0' characters so the combination of the optional '-' \r
1106 sign character, '0' characters, digit characters for Value, and the Null-terminator\r
1107 add up to Width characters.\r
1108 \r
1109 If Buffer is NULL, then ASSERT().\r
1110 If unsupported bits are set in Flags, then ASSERT().\r
df8d0595 1111 If both COMMA_TYPE and RADIX_HEX are set in Flags, then ASSERT().\r
2ad4dad0
LG
1112 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r
1113\r
1114 @param Buffer Pointer to the output buffer for the produced Null-terminated\r
1115 ASCII string.\r
1116 @param Flags The bitmask of flags that specify left justification, zero pad, and commas.\r
1117 @param Value The 64-bit signed value to convert to a string.\r
1118 @param Width The maximum number of ASCII characters to place in Buffer, not including\r
1119 the Null-terminator.\r
1120 \r
1121 @return The number of ASCII characters in Buffer not including the Null-terminator.\r
1122\r
1123**/\r
1124UINTN\r
1125EFIAPI\r
1126AsciiValueToString (\r
ea99d5d1 1127 OUT CHAR8 *Buffer,\r
1128 IN UINTN Flags,\r
1129 IN INT64 Value,\r
1130 IN UINTN Width\r
2ad4dad0
LG
1131 )\r
1132{\r
f0c74f5a
HW
1133 RETURN_STATUS Status;\r
1134 UINTN BufferSize;\r
1135\r
1136 if (Width == 0) {\r
1137 BufferSize = (MAXIMUM_VALUE_CHARACTERS + 1) * sizeof (CHAR8);\r
1138 } else {\r
1139 BufferSize = (Width + 1) * sizeof (CHAR8);\r
1140 }\r
1141\r
1142 Status = mPrint2SProtocol->AsciiValueToStringS (Buffer, BufferSize, Flags, Value, Width);\r
1143 if (RETURN_ERROR (Status)) {\r
1144 return 0;\r
1145 }\r
1146\r
1147 return AsciiStrnLenS (Buffer, BufferSize / sizeof (CHAR8));\r
a0afd019 1148}\r
f405c067 1149\r
695e4c49
HW
1150#endif\r
1151\r
9e32e971
HW
1152/**\r
1153 Converts a decimal value to a Null-terminated Ascii string.\r
1154\r
1155 Converts the decimal number specified by Value to a Null-terminated Ascii\r
1156 string specified by Buffer containing at most Width characters. No padding of\r
1157 spaces is ever performed. If Width is 0 then a width of\r
1158 MAXIMUM_VALUE_CHARACTERS is assumed. If the conversion contains more than\r
1159 Width characters, then only the first Width characters are placed in Buffer.\r
1160 Additional conversion parameters are specified in Flags.\r
1161\r
1162 The Flags bit LEFT_JUSTIFY is always ignored.\r
1163 All conversions are left justified in Buffer.\r
1164 If Width is 0, PREFIX_ZERO is ignored in Flags.\r
1165 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and\r
1166 commas are inserted every 3rd digit starting from the right.\r
1167 If RADIX_HEX is set in Flags, then the output buffer will be formatted in\r
1168 hexadecimal format.\r
1169 If Value is < 0 and RADIX_HEX is not set in Flags, then the fist character in\r
1170 Buffer is a '-'.\r
1171 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, then\r
1172 Buffer is padded with '0' characters so the combination of the optional '-'\r
1173 sign character, '0' characters, digit characters for Value, and the\r
1174 Null-terminator add up to Width characters.\r
1175\r
1176 If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r
1177 If an error would be returned, then the function will also ASSERT().\r
1178\r
1179 @param Buffer The pointer to the output buffer for the produced\r
1180 Null-terminated Ascii string.\r
1181 @param BufferSize The size of Buffer in bytes, including the\r
1182 Null-terminator.\r
1183 @param Flags The bitmask of flags that specify left justification,\r
1184 zero pad, and commas.\r
1185 @param Value The 64-bit signed value to convert to a string.\r
1186 @param Width The maximum number of Ascii characters to place in\r
1187 Buffer, not including the Null-terminator.\r
1188\r
1189 @retval RETURN_SUCCESS The decimal value is converted.\r
1190 @retval RETURN_BUFFER_TOO_SMALL If BufferSize cannot hold the converted\r
1191 value.\r
1192 @retval RETURN_INVALID_PARAMETER If Buffer is NULL.\r
1193 If PcdMaximumAsciiStringLength is not\r
1194 zero, and BufferSize is greater than\r
1195 PcdMaximumAsciiStringLength.\r
1196 If unsupported bits are set in Flags.\r
1197 If both COMMA_TYPE and RADIX_HEX are set in\r
1198 Flags.\r
1199 If Width >= MAXIMUM_VALUE_CHARACTERS.\r
1200\r
1201**/\r
1202RETURN_STATUS\r
1203EFIAPI\r
1204AsciiValueToStringS (\r
1205 IN OUT CHAR8 *Buffer,\r
1206 IN UINTN BufferSize,\r
1207 IN UINTN Flags,\r
1208 IN INT64 Value,\r
1209 IN UINTN Width\r
1210 )\r
1211{\r
1212 return mPrint2SProtocol->AsciiValueToStringS (Buffer, BufferSize, Flags, Value, Width);\r
1213}\r
1214\r
f405c067 1215#define PREFIX_SIGN BIT1\r
1216#define PREFIX_BLANK BIT2\r
1217#define LONG_TYPE BIT4\r
1218#define OUTPUT_UNICODE BIT6\r
1219#define FORMAT_UNICODE BIT8\r
1220#define PAD_TO_WIDTH BIT9\r
1221#define ARGUMENT_UNICODE BIT10\r
1222#define PRECISION BIT11\r
1223#define ARGUMENT_REVERSED BIT12\r
1224#define COUNT_ONLY_NO_PRINT BIT13\r
796fecd8 1225#define UNSIGNED_TYPE BIT14\r
f405c067 1226\r
1227//\r
1228// Record date and time information\r
1229//\r
1230typedef struct {\r
1231 UINT16 Year;\r
1232 UINT8 Month;\r
1233 UINT8 Day;\r
1234 UINT8 Hour;\r
1235 UINT8 Minute;\r
1236 UINT8 Second;\r
1237 UINT8 Pad1;\r
1238 UINT32 Nanosecond;\r
1239 INT16 TimeZone;\r
1240 UINT8 Daylight;\r
1241 UINT8 Pad2;\r
1242} TIME;\r
1243\r
1244GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};\r
1245\r
1246/**\r
1247 Internal function that convert a number to a string in Buffer.\r
1248\r
1249 Print worker function that converts a decimal or hexadecimal number to an ASCII string in Buffer.\r
1250\r
1251 @param Buffer Location to place the ASCII string of Value.\r
1252 @param Value The value to convert to a Decimal or Hexadecimal string in Buffer.\r
1253 @param Radix Radix of the value\r
1254\r
1255 @return A pointer to the end of buffer filled with ASCII string.\r
1256\r
1257**/\r
1258CHAR8 *\r
1259InternalPrintLibValueToString (\r
1260 IN OUT CHAR8 *Buffer, \r
1261 IN INT64 Value, \r
1262 IN UINTN Radix\r
1263 )\r
1264{\r
1265 UINT32 Remainder;\r
1266\r
1267 //\r
1268 // Loop to convert one digit at a time in reverse order\r
1269 //\r
1270 *Buffer = 0;\r
1271 do {\r
1272 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r
1273 *(++Buffer) = mHexStr[Remainder];\r
1274 } while (Value != 0);\r
1275\r
1276 //\r
1277 // Return pointer of the end of filled buffer.\r
1278 //\r
1279 return Buffer;\r
1280}\r
1281\r
1282/**\r
1283 Worker function that produces a Null-terminated string in an output buffer \r
1284 based on a Null-terminated format string and a VA_LIST argument list.\r
1285\r
1286 VSPrint function to process format and place the results in Buffer. Since a \r
1287 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
1288 this is the main print working routine.\r
1289\r
1290 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.\r
1291\r
1292 @param[out] Buffer The character buffer to print the results of the \r
1293 parsing of Format into.\r
1294 @param[in] BufferSize The maximum number of characters to put into \r
1295 buffer.\r
1296 @param[in] Flags Initial flags value.\r
1297 Can only have FORMAT_UNICODE, OUTPUT_UNICODE, \r
1298 and COUNT_ONLY_NO_PRINT set.\r
1299 @param[in] Format A Null-terminated format string.\r
1300 @param[in] VaListMarker VA_LIST style variable argument list consumed by\r
1301 processing Format.\r
1302 @param[in] BaseListMarker BASE_LIST style variable argument list consumed\r
1303 by processing Format.\r
1304\r
1305 @return The number of characters printed not including the Null-terminator.\r
1306 If COUNT_ONLY_NO_PRINT was set returns the same, but without any\r
1307 modification to Buffer.\r
1308\r
1309**/\r
1310UINTN\r
1311InternalPrintLibSPrintMarker (\r
1312 OUT CHAR8 *Buffer,\r
1313 IN UINTN BufferSize,\r
1314 IN UINTN Flags,\r
1315 IN CONST CHAR8 *Format,\r
1316 IN VA_LIST VaListMarker, OPTIONAL\r
1317 IN BASE_LIST BaseListMarker OPTIONAL\r
1318 );\r
1319\r
1320/**\r
1321 Worker function that produces a Null-terminated string in an output buffer \r
1322 based on a Null-terminated format string and variable argument list.\r
1323\r
1324 VSPrint function to process format and place the results in Buffer. Since a \r
1325 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
1326 this is the main print working routine\r
1327\r
1328 @param StartOfBuffer The character buffer to print the results of the parsing\r
1329 of Format into.\r
1330 @param BufferSize The maximum number of characters to put into buffer.\r
1331 Zero means no limit.\r
1332 @param Flags Initial flags value.\r
1333 Can only have FORMAT_UNICODE and OUTPUT_UNICODE set\r
1334 @param FormatString A Null-terminated format string.\r
1335 @param ... The variable argument list.\r
1336\r
1337 @return The number of characters printed.\r
1338\r
1339**/\r
1340UINTN\r
1341EFIAPI\r
1342InternalPrintLibSPrint (\r
1343 OUT CHAR8 *StartOfBuffer,\r
1344 IN UINTN BufferSize,\r
1345 IN UINTN Flags,\r
1346 IN CONST CHAR8 *FormatString,\r
1347 ...\r
1348 )\r
1349{\r
1350 VA_LIST Marker;\r
3bbe68a3 1351 UINTN NumberOfPrinted;\r
f405c067 1352\r
1353 VA_START (Marker, FormatString);\r
3bbe68a3 1354 NumberOfPrinted = InternalPrintLibSPrintMarker (StartOfBuffer, BufferSize, Flags, FormatString, Marker, NULL);\r
1355 VA_END (Marker);\r
1356 return NumberOfPrinted;\r
f405c067 1357}\r
1358\r
586d81d1
LG
1359#define WARNING_STATUS_NUMBER 5\r
1360#define ERROR_STATUS_NUMBER 33\r
f405c067 1361\r
4c10f953 1362GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 * CONST mStatusString[] = {\r
f405c067 1363 "Success", // RETURN_SUCCESS = 0\r
1364 "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH = 1\r
1365 "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE = 2\r
1366 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE = 3\r
1367 "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL = 4\r
586d81d1 1368 "Warning Stale Data", // RETURN_WARN_STALE_DATA = 5\r
f405c067 1369 "Load Error", // RETURN_LOAD_ERROR = 1 | MAX_BIT\r
1370 "Invalid Parameter", // RETURN_INVALID_PARAMETER = 2 | MAX_BIT\r
1371 "Unsupported", // RETURN_UNSUPPORTED = 3 | MAX_BIT\r
1372 "Bad Buffer Size", // RETURN_BAD_BUFFER_SIZE = 4 | MAX_BIT\r
1373 "Buffer Too Small", // RETURN_BUFFER_TOO_SMALL, = 5 | MAX_BIT\r
1374 "Not Ready", // RETURN_NOT_READY = 6 | MAX_BIT\r
1375 "Device Error", // RETURN_DEVICE_ERROR = 7 | MAX_BIT\r
1376 "Write Protected", // RETURN_WRITE_PROTECTED = 8 | MAX_BIT\r
1377 "Out of Resources", // RETURN_OUT_OF_RESOURCES = 9 | MAX_BIT\r
1378 "Volume Corrupt", // RETURN_VOLUME_CORRUPTED = 10 | MAX_BIT\r
1379 "Volume Full", // RETURN_VOLUME_FULL = 11 | MAX_BIT\r
1380 "No Media", // RETURN_NO_MEDIA = 12 | MAX_BIT\r
1381 "Media changed", // RETURN_MEDIA_CHANGED = 13 | MAX_BIT\r
1382 "Not Found", // RETURN_NOT_FOUND = 14 | MAX_BIT\r
1383 "Access Denied", // RETURN_ACCESS_DENIED = 15 | MAX_BIT\r
1384 "No Response", // RETURN_NO_RESPONSE = 16 | MAX_BIT\r
1385 "No mapping", // RETURN_NO_MAPPING = 17 | MAX_BIT\r
1386 "Time out", // RETURN_TIMEOUT = 18 | MAX_BIT\r
1387 "Not started", // RETURN_NOT_STARTED = 19 | MAX_BIT\r
1388 "Already started", // RETURN_ALREADY_STARTED = 20 | MAX_BIT\r
1389 "Aborted", // RETURN_ABORTED = 21 | MAX_BIT\r
1390 "ICMP Error", // RETURN_ICMP_ERROR = 22 | MAX_BIT\r
1391 "TFTP Error", // RETURN_TFTP_ERROR = 23 | MAX_BIT\r
586d81d1
LG
1392 "Protocol Error", // RETURN_PROTOCOL_ERROR = 24 | MAX_BIT\r
1393 "Incompatible Version", // RETURN_INCOMPATIBLE_VERSION = 25 | MAX_BIT\r
1394 "Security Violation", // RETURN_SECURITY_VIOLATION = 26 | MAX_BIT\r
1395 "CRC Error", // RETURN_CRC_ERROR = 27 | MAX_BIT\r
1396 "End of Media", // RETURN_END_OF_MEDIA = 28 | MAX_BIT\r
1397 "Reserved (29)", // RESERVED = 29 | MAX_BIT\r
1398 "Reserved (30)", // RESERVED = 30 | MAX_BIT\r
1399 "End of File", // RETURN_END_OF_FILE = 31 | MAX_BIT\r
1400 "Invalid Language", // RETURN_INVALID_LANGUAGE = 32 | MAX_BIT\r
1401 "Compromised Data" // RETURN_COMPROMISED_DATA = 33 | MAX_BIT\r
f405c067 1402};\r
1403\r
586d81d1
LG
1404/**\r
1405 Internal function that places the character into the Buffer.\r
1406\r
1407 Internal function that places ASCII or Unicode character into the Buffer.\r
1408\r
1409 @param Buffer The buffer to place the Unicode or ASCII string.\r
1410 @param EndBuffer The end of the input Buffer. No characters will be\r
1411 placed after that. \r
1412 @param Length The count of character to be placed into Buffer.\r
1413 (Negative value indicates no buffer fill.)\r
1414 @param Character The character to be placed into Buffer.\r
1415 @param Increment The character increment in Buffer.\r
1416\r
1417 @return Buffer.\r
1418\r
1419**/\r
1420CHAR8 *\r
1421InternalPrintLibFillBuffer (\r
1422 OUT CHAR8 *Buffer,\r
1423 IN CHAR8 *EndBuffer,\r
1424 IN INTN Length,\r
1425 IN UINTN Character,\r
1426 IN INTN Increment\r
1427 )\r
1428{\r
1429 INTN Index;\r
1430 \r
1431 for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {\r
1432 *Buffer = (CHAR8) Character;\r
1433 if (Increment != 1) {\r
1434 *(Buffer + 1) = (CHAR8)(Character >> 8);\r
1435 }\r
1436 Buffer += Increment;\r
1437 }\r
1438\r
1439 return Buffer;\r
1440}\r
1441\r
f405c067 1442/**\r
1443 Worker function that produces a Null-terminated string in an output buffer \r
1444 based on a Null-terminated format string and a VA_LIST argument list.\r
1445\r
1446 VSPrint function to process format and place the results in Buffer. Since a \r
1447 VA_LIST is used this routine allows the nesting of Vararg routines. Thus \r
1448 this is the main print working routine.\r
1449\r
1450 If COUNT_ONLY_NO_PRINT is set in Flags, Buffer will not be modified at all.\r
1451\r
1452 @param[out] Buffer The character buffer to print the results of the \r
1453 parsing of Format into.\r
1454 @param[in] BufferSize The maximum number of characters to put into \r
1455 buffer.\r
1456 @param[in] Flags Initial flags value.\r
1457 Can only have FORMAT_UNICODE, OUTPUT_UNICODE, \r
1458 and COUNT_ONLY_NO_PRINT set.\r
1459 @param[in] Format A Null-terminated format string.\r
1460 @param[in] VaListMarker VA_LIST style variable argument list consumed by\r
1461 processing Format.\r
1462 @param[in] BaseListMarker BASE_LIST style variable argument list consumed\r
1463 by processing Format.\r
1464\r
1465 @return The number of characters printed not including the Null-terminator.\r
1466 If COUNT_ONLY_NO_PRINT was set returns the same, but without any\r
1467 modification to Buffer.\r
1468\r
1469**/\r
1470UINTN\r
1471InternalPrintLibSPrintMarker (\r
1472 OUT CHAR8 *Buffer,\r
1473 IN UINTN BufferSize,\r
1474 IN UINTN Flags,\r
1475 IN CONST CHAR8 *Format,\r
1476 IN VA_LIST VaListMarker, OPTIONAL\r
1477 IN BASE_LIST BaseListMarker OPTIONAL\r
1478 )\r
1479{\r
586d81d1 1480 CHAR8 *OriginalBuffer;\r
f405c067 1481 CHAR8 *EndBuffer;\r
1482 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
1483 UINT32 BytesPerOutputCharacter;\r
1484 UINTN BytesPerFormatCharacter;\r
1485 UINTN FormatMask;\r
1486 UINTN FormatCharacter;\r
1487 UINTN Width;\r
1488 UINTN Precision;\r
1489 INT64 Value;\r
1490 CONST CHAR8 *ArgumentString;\r
1491 UINTN Character;\r
1492 GUID *TmpGuid;\r
1493 TIME *TmpTime;\r
1494 UINTN Count;\r
1495 UINTN ArgumentMask;\r
1496 INTN BytesPerArgumentCharacter;\r
1497 UINTN ArgumentCharacter;\r
1498 BOOLEAN Done;\r
1499 UINTN Index;\r
1500 CHAR8 Prefix;\r
1501 BOOLEAN ZeroPad;\r
1502 BOOLEAN Comma;\r
1503 UINTN Digits;\r
1504 UINTN Radix;\r
1505 RETURN_STATUS Status;\r
1506 UINT32 GuidData1;\r
1507 UINT16 GuidData2;\r
1508 UINT16 GuidData3;\r
be5ec8c5 1509 UINTN LengthToReturn;\r
f405c067 1510\r
1511 //\r
1512 // If you change this code be sure to match the 2 versions of this function.\r
1513 // Nearly identical logic is found in the BasePrintLib and \r
1514 // DxePrintLibPrint2Protocol (both PrintLib instances).\r
1515 //\r
1516\r
50418baa
HW
1517 //\r
1518 // 1. Buffer shall not be a null pointer when both BufferSize > 0 and\r
1519 // COUNT_ONLY_NO_PRINT is not set in Flags.\r
1520 //\r
1521 if ((BufferSize > 0) && ((Flags & COUNT_ONLY_NO_PRINT) == 0)) {\r
1522 SAFE_PRINT_CONSTRAINT_CHECK ((Buffer != NULL), 0);\r
1523 }\r
1524\r
1525 //\r
1526 // 2. Format shall not be a null pointer when BufferSize > 0 or when\r
1527 // COUNT_ONLY_NO_PRINT is set in Flags.\r
1528 //\r
1529 if ((BufferSize > 0) || ((Flags & COUNT_ONLY_NO_PRINT) != 0)) {\r
1530 SAFE_PRINT_CONSTRAINT_CHECK ((Format != NULL), 0);\r
1531 }\r
1532\r
1533 //\r
1534 // 3. BufferSize shall not be greater than RSIZE_MAX for Unicode output or\r
1535 // ASCII_RSIZE_MAX for Ascii output.\r
1536 //\r
1537 if ((Flags & OUTPUT_UNICODE) != 0) {\r
1538 if (RSIZE_MAX != 0) {\r
1539 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= RSIZE_MAX), 0);\r
1540 }\r
1541 BytesPerOutputCharacter = 2;\r
1542 } else {\r
1543 if (ASCII_RSIZE_MAX != 0) {\r
1544 SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= ASCII_RSIZE_MAX), 0);\r
1545 }\r
1546 BytesPerOutputCharacter = 1;\r
1547 }\r
1548\r
1549 //\r
1550 // 4. Format shall not contain more than RSIZE_MAX Unicode characters or\r
1551 // ASCII_RSIZE_MAX Ascii characters.\r
1552 //\r
1553 if ((Flags & FORMAT_UNICODE) != 0) {\r
1554 if (RSIZE_MAX != 0) {\r
1555 SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16 *)Format, RSIZE_MAX + 1) <= RSIZE_MAX), 0);\r
1556 }\r
1557 BytesPerFormatCharacter = 2;\r
1558 FormatMask = 0xffff;\r
1559 } else {\r
1560 if (ASCII_RSIZE_MAX != 0) {\r
1561 SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), 0);\r
1562 }\r
1563 BytesPerFormatCharacter = 1;\r
1564 FormatMask = 0xff;\r
1565 }\r
1566\r
586d81d1
LG
1567 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r
1568 if (BufferSize == 0) {\r
1569 Buffer = NULL;\r
1570 }\r
1571 } else {\r
1572 //\r
1573 // We can run without a Buffer for counting only.\r
1574 //\r
1575 if (BufferSize == 0) {\r
1576 return 0;\r
1577 }\r
f405c067 1578 }\r
1579\r
1580 LengthToReturn = 0;\r
586d81d1
LG
1581 EndBuffer = NULL;\r
1582 OriginalBuffer = NULL;\r
f405c067 1583\r
1584 //\r
1585 // Reserve space for the Null terminator.\r
1586 //\r
586d81d1
LG
1587 if (Buffer != NULL) {\r
1588 BufferSize--;\r
1589 OriginalBuffer = Buffer;\r
f405c067 1590\r
586d81d1
LG
1591 //\r
1592 // Set the tag for the end of the input Buffer.\r
1593 //\r
1594 EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;\r
1595 }\r
f405c067 1596\r
f405c067 1597 //\r
1598 // Get the first character from the format string\r
1599 //\r
1600 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
1601\r
1602 //\r
1603 // Loop until the end of the format string is reached or the output buffer is full\r
1604 //\r
586d81d1
LG
1605 while (FormatCharacter != 0) {\r
1606 if ((Buffer != NULL) && (Buffer >= EndBuffer)) {\r
1607 break;\r
1608 }\r
f405c067 1609 //\r
1610 // Clear all the flag bits except those that may have been passed in\r
1611 //\r
586d81d1 1612 Flags &= (UINTN) (OUTPUT_UNICODE | FORMAT_UNICODE | COUNT_ONLY_NO_PRINT);\r
f405c067 1613\r
1614 //\r
1615 // Set the default width to zero, and the default precision to 1\r
1616 //\r
1617 Width = 0;\r
1618 Precision = 1;\r
1619 Prefix = 0;\r
1620 Comma = FALSE;\r
1621 ZeroPad = FALSE;\r
1622 Count = 0;\r
1623 Digits = 0;\r
1624\r
1625 switch (FormatCharacter) {\r
1626 case '%':\r
1627 //\r
1628 // Parse Flags and Width\r
1629 //\r
1630 for (Done = FALSE; !Done; ) {\r
1631 Format += BytesPerFormatCharacter;\r
1632 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
1633 switch (FormatCharacter) {\r
1634 case '.': \r
1635 Flags |= PRECISION; \r
1636 break;\r
1637 case '-': \r
1638 Flags |= LEFT_JUSTIFY; \r
1639 break;\r
1640 case '+': \r
1641 Flags |= PREFIX_SIGN; \r
1642 break;\r
1643 case ' ': \r
1644 Flags |= PREFIX_BLANK; \r
1645 break;\r
1646 case ',': \r
1647 Flags |= COMMA_TYPE; \r
1648 break;\r
1649 case 'L':\r
1650 case 'l': \r
1651 Flags |= LONG_TYPE; \r
1652 break;\r
1653 case '*':\r
1654 if ((Flags & PRECISION) == 0) {\r
1655 Flags |= PAD_TO_WIDTH;\r
1656 if (BaseListMarker == NULL) {\r
1657 Width = VA_ARG (VaListMarker, UINTN);\r
1658 } else {\r
1659 Width = BASE_ARG (BaseListMarker, UINTN);\r
1660 }\r
1661 } else {\r
1662 if (BaseListMarker == NULL) {\r
1663 Precision = VA_ARG (VaListMarker, UINTN);\r
1664 } else {\r
1665 Precision = BASE_ARG (BaseListMarker, UINTN);\r
1666 }\r
1667 }\r
1668 break;\r
1669 case '0':\r
1670 if ((Flags & PRECISION) == 0) {\r
1671 Flags |= PREFIX_ZERO;\r
1672 }\r
1673 case '1':\r
1674 case '2':\r
1675 case '3':\r
1676 case '4':\r
1677 case '5':\r
1678 case '6':\r
1679 case '7':\r
1680 case '8':\r
1681 case '9':\r
1682 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){\r
1683 Count = (Count * 10) + FormatCharacter - '0';\r
1684 Format += BytesPerFormatCharacter;\r
1685 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
1686 }\r
1687 Format -= BytesPerFormatCharacter;\r
1688 if ((Flags & PRECISION) == 0) {\r
1689 Flags |= PAD_TO_WIDTH;\r
1690 Width = Count;\r
1691 } else {\r
1692 Precision = Count;\r
1693 }\r
1694 break;\r
1695 \r
1696 case '\0':\r
1697 //\r
1698 // Make no output if Format string terminates unexpectedly when\r
1699 // looking up for flag, width, precision and type. \r
1700 //\r
1701 Format -= BytesPerFormatCharacter;\r
1702 Precision = 0;\r
1703 //\r
1704 // break skipped on purpose.\r
1705 //\r
1706 default:\r
1707 Done = TRUE;\r
1708 break;\r
1709 }\r
1710 } \r
1711\r
1712 //\r
1713 // Handle each argument type\r
1714 //\r
1715 switch (FormatCharacter) {\r
1716 case 'p':\r
1717 //\r
1718 // Flag space, +, 0, L & l are invalid for type p.\r
1719 //\r
586d81d1 1720 Flags &= ~((UINTN) (PREFIX_BLANK | PREFIX_SIGN | PREFIX_ZERO | LONG_TYPE));\r
f405c067 1721 if (sizeof (VOID *) > 4) {\r
1722 Flags |= LONG_TYPE;\r
1723 }\r
586d81d1
LG
1724 //\r
1725 // break skipped on purpose\r
1726 //\r
f405c067 1727 case 'X':\r
1728 Flags |= PREFIX_ZERO;\r
1729 //\r
1730 // break skipped on purpose\r
1731 //\r
1732 case 'x':\r
1733 Flags |= RADIX_HEX;\r
1734 //\r
1735 // break skipped on purpose\r
1736 //\r
796fecd8
QS
1737 case 'u':\r
1738 if ((Flags & RADIX_HEX) == 0) {\r
1739 Flags &= ~((UINTN) (PREFIX_SIGN));\r
1740 Flags |= UNSIGNED_TYPE;\r
1741 }\r
1742 //\r
1743 // break skipped on purpose\r
1744 //\r
f405c067 1745 case 'd':\r
1746 if ((Flags & LONG_TYPE) == 0) {\r
1747 //\r
796fecd8 1748 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
f405c067 1749 // This assumption is made so the format string definition is compatible with the ANSI C\r
1750 // Specification for formatted strings. It is recommended that the Base Types be used \r
1751 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
1752 // provides an implementation that is compatible with that largest possible set of CPU \r
1753 // architectures. This is why the type "int" is used in this one case.\r
1754 //\r
1755 if (BaseListMarker == NULL) {\r
1756 Value = VA_ARG (VaListMarker, int);\r
1757 } else {\r
1758 Value = BASE_ARG (BaseListMarker, int);\r
1759 }\r
1760 } else {\r
1761 if (BaseListMarker == NULL) {\r
1762 Value = VA_ARG (VaListMarker, INT64);\r
1763 } else {\r
1764 Value = BASE_ARG (BaseListMarker, INT64);\r
1765 }\r
1766 }\r
1767 if ((Flags & PREFIX_BLANK) != 0) {\r
1768 Prefix = ' ';\r
1769 }\r
1770 if ((Flags & PREFIX_SIGN) != 0) {\r
1771 Prefix = '+';\r
1772 }\r
1773 if ((Flags & COMMA_TYPE) != 0) {\r
1774 Comma = TRUE;\r
1775 }\r
1776 if ((Flags & RADIX_HEX) == 0) {\r
1777 Radix = 10;\r
1778 if (Comma) {\r
586d81d1 1779 Flags &= ~((UINTN) PREFIX_ZERO);\r
f405c067 1780 Precision = 1;\r
1781 }\r
796fecd8 1782 if (Value < 0 && (Flags & UNSIGNED_TYPE) == 0) {\r
f405c067 1783 Flags |= PREFIX_SIGN;\r
1784 Prefix = '-';\r
1785 Value = -Value;\r
796fecd8
QS
1786 } else if ((Flags & UNSIGNED_TYPE) != 0 && (Flags & LONG_TYPE) == 0) {\r
1787 //\r
1788 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
1789 // This assumption is made so the format string definition is compatible with the ANSI C\r
1790 // Specification for formatted strings. It is recommended that the Base Types be used \r
1791 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
1792 // provides an implementation that is compatible with that largest possible set of CPU \r
1793 // architectures. This is why the type "unsigned int" is used in this one case.\r
1794 //\r
1795 Value = (unsigned int)Value;\r
f405c067 1796 }\r
1797 } else {\r
1798 Radix = 16;\r
1799 Comma = FALSE;\r
1800 if ((Flags & LONG_TYPE) == 0 && Value < 0) {\r
1801 //\r
796fecd8 1802 // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are assumed to be type "int".\r
f405c067 1803 // This assumption is made so the format string definition is compatible with the ANSI C\r
1804 // Specification for formatted strings. It is recommended that the Base Types be used \r
1805 // everywhere, but in this one case, compliance with ANSI C is more important, and \r
1806 // provides an implementation that is compatible with that largest possible set of CPU \r
1807 // architectures. This is why the type "unsigned int" is used in this one case.\r
1808 //\r
1809 Value = (unsigned int)Value;\r
1810 }\r
1811 }\r
1812 //\r
1813 // Convert Value to a reversed string\r
1814 //\r
1815 Count = InternalPrintLibValueToString (ValueBuffer, Value, Radix) - ValueBuffer;\r
1816 if (Value == 0 && Precision == 0) {\r
1817 Count = 0;\r
1818 }\r
1819 ArgumentString = (CHAR8 *)ValueBuffer + Count;\r
1820 \r
1821 Digits = Count % 3;\r
1822 if (Digits != 0) {\r
1823 Digits = 3 - Digits;\r
1824 }\r
1825 if (Comma && Count != 0) {\r
1826 Count += ((Count - 1) / 3);\r
1827 }\r
1828 if (Prefix != 0) {\r
1829 Count++;\r
1830 Precision++;\r
1831 }\r
1832 Flags |= ARGUMENT_REVERSED;\r
1833 ZeroPad = TRUE;\r
1834 if ((Flags & PREFIX_ZERO) != 0) {\r
1835 if ((Flags & LEFT_JUSTIFY) == 0) {\r
1836 if ((Flags & PAD_TO_WIDTH) != 0) {\r
1837 if ((Flags & PRECISION) == 0) {\r
1838 Precision = Width;\r
1839 }\r
1840 }\r
1841 }\r
1842 }\r
1843 break;\r
1844\r
1845 case 's':\r
1846 case 'S':\r
1847 Flags |= ARGUMENT_UNICODE;\r
1848 //\r
1849 // break skipped on purpose\r
1850 //\r
1851 case 'a':\r
1852 if (BaseListMarker == NULL) {\r
1853 ArgumentString = VA_ARG (VaListMarker, CHAR8 *);\r
1854 } else {\r
1855 ArgumentString = BASE_ARG (BaseListMarker, CHAR8 *);\r
1856 }\r
1857 if (ArgumentString == NULL) {\r
6e1e5405 1858 Flags &= (~(UINTN)ARGUMENT_UNICODE);\r
f405c067 1859 ArgumentString = "<null string>";\r
1860 }\r
1861 //\r
1862 // Set the default precision for string to be zero if not specified.\r
1863 //\r
1864 if ((Flags & PRECISION) == 0) {\r
1865 Precision = 0;\r
1866 }\r
1867 break;\r
1868\r
1869 case 'c':\r
1870 if (BaseListMarker == NULL) {\r
1871 Character = VA_ARG (VaListMarker, UINTN) & 0xffff;\r
1872 } else {\r
1873 Character = BASE_ARG (BaseListMarker, UINTN) & 0xffff;\r
1874 }\r
1875 ArgumentString = (CHAR8 *)&Character;\r
1876 Flags |= ARGUMENT_UNICODE;\r
1877 break;\r
1878\r
1879 case 'g':\r
1880 if (BaseListMarker == NULL) {\r
1881 TmpGuid = VA_ARG (VaListMarker, GUID *);\r
1882 } else {\r
1883 TmpGuid = BASE_ARG (BaseListMarker, GUID *);\r
1884 }\r
1885 if (TmpGuid == NULL) {\r
1886 ArgumentString = "<null guid>";\r
1887 } else {\r
1888 GuidData1 = ReadUnaligned32 (&(TmpGuid->Data1));\r
1889 GuidData2 = ReadUnaligned16 (&(TmpGuid->Data2));\r
1890 GuidData3 = ReadUnaligned16 (&(TmpGuid->Data3));\r
1891 InternalPrintLibSPrint (\r
1892 ValueBuffer,\r
1893 MAXIMUM_VALUE_CHARACTERS, \r
1894 0,\r
1895 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
1896 GuidData1,\r
1897 GuidData2,\r
1898 GuidData3,\r
1899 TmpGuid->Data4[0],\r
1900 TmpGuid->Data4[1],\r
1901 TmpGuid->Data4[2],\r
1902 TmpGuid->Data4[3],\r
1903 TmpGuid->Data4[4],\r
1904 TmpGuid->Data4[5],\r
1905 TmpGuid->Data4[6],\r
1906 TmpGuid->Data4[7]\r
1907 );\r
1908 ArgumentString = ValueBuffer;\r
1909 }\r
1910 break;\r
1911\r
1912 case 't':\r
1913 if (BaseListMarker == NULL) {\r
1914 TmpTime = VA_ARG (VaListMarker, TIME *); \r
1915 } else {\r
1916 TmpTime = BASE_ARG (BaseListMarker, TIME *); \r
1917 }\r
1918 if (TmpTime == NULL) {\r
1919 ArgumentString = "<null time>";\r
1920 } else {\r
1921 InternalPrintLibSPrint (\r
1922 ValueBuffer,\r
1923 MAXIMUM_VALUE_CHARACTERS,\r
1924 0,\r
1925 "%02d/%02d/%04d %02d:%02d",\r
1926 TmpTime->Month,\r
1927 TmpTime->Day,\r
1928 TmpTime->Year,\r
1929 TmpTime->Hour,\r
1930 TmpTime->Minute\r
1931 );\r
1932 ArgumentString = ValueBuffer;\r
1933 }\r
1934 break;\r
1935\r
1936 case 'r':\r
1937 if (BaseListMarker == NULL) {\r
1938 Status = VA_ARG (VaListMarker, RETURN_STATUS);\r
1939 } else {\r
1940 Status = BASE_ARG (BaseListMarker, RETURN_STATUS);\r
1941 }\r
1942 ArgumentString = ValueBuffer;\r
1943 if (RETURN_ERROR (Status)) {\r
1944 //\r
1945 // Clear error bit\r
1946 //\r
1947 Index = Status & ~MAX_BIT;\r
1948 if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {\r
1949 ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];\r
1950 }\r
1951 } else {\r
1952 Index = Status;\r
1953 if (Index <= WARNING_STATUS_NUMBER) {\r
1954 ArgumentString = mStatusString [Index];\r
1955 }\r
1956 }\r
1957 if (ArgumentString == ValueBuffer) {\r
1958 InternalPrintLibSPrint ((CHAR8 *) ValueBuffer, MAXIMUM_VALUE_CHARACTERS, 0, "%08X", Status);\r
1959 }\r
1960 break;\r
1961\r
1962 case '\r':\r
1963 Format += BytesPerFormatCharacter;\r
1964 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
1965 if (FormatCharacter == '\n') {\r
1966 //\r
1967 // Translate '\r\n' to '\r\n'\r
1968 //\r
1969 ArgumentString = "\r\n";\r
1970 } else {\r
1971 //\r
1972 // Translate '\r' to '\r'\r
1973 //\r
1974 ArgumentString = "\r";\r
1975 Format -= BytesPerFormatCharacter;\r
1976 }\r
1977 break;\r
1978\r
1979 case '\n':\r
1980 //\r
1981 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
1982 //\r
1983 ArgumentString = "\r\n";\r
1984 Format += BytesPerFormatCharacter;\r
1985 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
1986 if (FormatCharacter != '\r') {\r
1987 Format -= BytesPerFormatCharacter;\r
1988 }\r
1989 break;\r
1990\r
1991 case '%':\r
1992 default:\r
1993 //\r
1994 // if the type is '%' or unknown, then print it to the screen\r
1995 //\r
1996 ArgumentString = (CHAR8 *)&FormatCharacter;\r
1997 Flags |= ARGUMENT_UNICODE;\r
1998 break;\r
1999 }\r
2000 break;\r
2001 \r
2002 case '\r':\r
2003 Format += BytesPerFormatCharacter;\r
2004 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
2005 if (FormatCharacter == '\n') {\r
2006 //\r
2007 // Translate '\r\n' to '\r\n'\r
2008 //\r
2009 ArgumentString = "\r\n";\r
2010 } else {\r
2011 //\r
2012 // Translate '\r' to '\r'\r
2013 //\r
2014 ArgumentString = "\r";\r
2015 Format -= BytesPerFormatCharacter;\r
2016 }\r
2017 break;\r
2018\r
2019 case '\n':\r
2020 //\r
2021 // Translate '\n' to '\r\n' and '\n\r' to '\r\n'\r
2022 //\r
2023 ArgumentString = "\r\n";\r
2024 Format += BytesPerFormatCharacter;\r
2025 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
2026 if (FormatCharacter != '\r') {\r
2027 Format -= BytesPerFormatCharacter;\r
2028 }\r
2029 break;\r
2030\r
2031 default:\r
2032 ArgumentString = (CHAR8 *)&FormatCharacter;\r
2033 Flags |= ARGUMENT_UNICODE;\r
2034 break;\r
2035 }\r
2036\r
2037 //\r
2038 // Retrieve the ArgumentString attriubutes\r
2039 //\r
2040 if ((Flags & ARGUMENT_UNICODE) != 0) {\r
2041 ArgumentMask = 0xffff;\r
2042 BytesPerArgumentCharacter = 2;\r
2043 } else {\r
2044 ArgumentMask = 0xff;\r
2045 BytesPerArgumentCharacter = 1;\r
2046 }\r
2047 if ((Flags & ARGUMENT_REVERSED) != 0) {\r
2048 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;\r
2049 } else {\r
2050 //\r
2051 // Compute the number of characters in ArgumentString and store it in Count\r
2052 // ArgumentString is either null-terminated, or it contains Precision characters\r
2053 //\r
2054 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {\r
2055 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;\r
2056 if (ArgumentCharacter == 0) {\r
2057 break;\r
2058 }\r
2059 }\r
2060 }\r
2061\r
2062 if (Precision < Count) {\r
2063 Precision = Count;\r
2064 }\r
2065\r
2066 //\r
2067 // Pad before the string\r
2068 //\r
2069 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {\r
6bae492f 2070 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
586d81d1
LG
2071 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2072 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
2073 }\r
f405c067 2074 }\r
2075\r
2076 if (ZeroPad) {\r
2077 if (Prefix != 0) {\r
6bae492f 2078 LengthToReturn += (1 * BytesPerOutputCharacter);\r
586d81d1
LG
2079 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2080 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
2081 }\r
f405c067 2082 }\r
6bae492f 2083 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
586d81d1
LG
2084 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2085 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);\r
2086 }\r
f405c067 2087 } else {\r
6bae492f 2088 LengthToReturn += ((Precision - Count) * BytesPerOutputCharacter);\r
586d81d1
LG
2089 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2090 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);\r
2091 }\r
f405c067 2092 if (Prefix != 0) {\r
6bae492f 2093 LengthToReturn += (1 * BytesPerOutputCharacter);\r
586d81d1
LG
2094 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2095 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
2096 }\r
f405c067 2097 }\r
2098 }\r
2099\r
2100 //\r
2101 // Output the Prefix character if it is present\r
2102 //\r
2103 Index = 0;\r
2104 if (Prefix != 0) {\r
2105 Index++;\r
2106 }\r
2107\r
2108 //\r
2109 // Copy the string into the output buffer performing the required type conversions\r
2110 //\r
2111 while (Index < Count) {\r
2112 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;\r
2113\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, ArgumentCharacter, BytesPerOutputCharacter);\r
2117 }\r
f405c067 2118 ArgumentString += BytesPerArgumentCharacter;\r
2119 Index++;\r
2120 if (Comma) {\r
2121 Digits++;\r
2122 if (Digits == 3) {\r
2123 Digits = 0;\r
2124 Index++;\r
2125 if (Index < Count) {\r
6bae492f 2126 LengthToReturn += (1 * BytesPerOutputCharacter);\r
586d81d1
LG
2127 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2128 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);\r
2129 }\r
f405c067 2130 }\r
2131 }\r
2132 }\r
2133 }\r
2134\r
2135 //\r
2136 // Pad after the string\r
2137 //\r
2138 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {\r
6bae492f 2139 LengthToReturn += ((Width - Precision) * BytesPerOutputCharacter);\r
586d81d1
LG
2140 if ((Flags & COUNT_ONLY_NO_PRINT) == 0 && Buffer != NULL) {\r
2141 Buffer = InternalPrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
2142 }\r
f405c067 2143 }\r
2144\r
2145 //\r
2146 // Get the next character from the format string\r
2147 //\r
2148 Format += BytesPerFormatCharacter;\r
2149\r
2150 //\r
2151 // Get the next character from the format string\r
2152 //\r
2153 FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
2154 }\r
2155\r
586d81d1
LG
2156 if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r
2157 return (LengthToReturn / BytesPerOutputCharacter);\r
2158 }\r
2159\r
2160 ASSERT (Buffer != NULL);\r
2161 //\r
2162 // Null terminate the Unicode or ASCII string\r
2163 //\r
2164 InternalPrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);\r
586d81d1
LG
2165\r
2166 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);\r
f405c067 2167}\r
2168\r
2169/**\r
2170 Returns the number of characters that would be produced by if the formatted \r
2171 output were produced not including the Null-terminator.\r
2172\r
f405c067 2173 If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
2174\r
50418baa
HW
2175 If FormatString is NULL, then ASSERT() and 0 is returned.\r
2176 If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more\r
2177 than PcdMaximumUnicodeStringLength Unicode characters not including the\r
2178 Null-terminator, then ASSERT() and 0 is returned.\r
2179\r
f405c067 2180 @param[in] FormatString A Null-terminated Unicode format string.\r
2181 @param[in] Marker VA_LIST marker for the variable argument list.\r
2182\r
2183 @return The number of characters that would be produced, not including the \r
2184 Null-terminator.\r
2185**/\r
2186UINTN\r
2187EFIAPI\r
2188SPrintLength (\r
2189 IN CONST CHAR16 *FormatString,\r
2190 IN VA_LIST Marker\r
2191 )\r
2192{\r
50418baa 2193 ASSERT_UNICODE_BUFFER (FormatString);\r
f405c067 2194 return InternalPrintLibSPrintMarker (NULL, 0, FORMAT_UNICODE | OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);\r
2195}\r
2196\r
2197/**\r
2198 Returns the number of characters that would be produced by if the formatted \r
2199 output were produced not including the Null-terminator.\r
2200\r
50418baa
HW
2201 If FormatString is NULL, then ASSERT() and 0 is returned.\r
2202 If PcdMaximumAsciiStringLength is not zero, and FormatString contains more\r
2203 than PcdMaximumAsciiStringLength Ascii characters not including the\r
2204 Null-terminator, then ASSERT() and 0 is returned.\r
f405c067 2205\r
2206 @param[in] FormatString A Null-terminated ASCII format string.\r
2207 @param[in] Marker VA_LIST marker for the variable argument list.\r
2208\r
2209 @return The number of characters that would be produced, not including the \r
2210 Null-terminator.\r
2211**/\r
2212UINTN\r
2213EFIAPI\r
2214SPrintLengthAsciiFormat (\r
2215 IN CONST CHAR8 *FormatString,\r
2216 IN VA_LIST Marker\r
2217 )\r
2218{\r
f405c067 2219 return InternalPrintLibSPrintMarker (NULL, 0, OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);\r
2220}\r