]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/CCode/Source/String/PrintLib.c
More moves for Tool Packages
[mirror_edk2.git] / Tools / CCode / Source / String / PrintLib.c
1 /*++
2
3 Copyright (c) 2004-2006 Intel Corporation. All rights reserved
4 This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12
13 Module Name:
14
15 PrintLib.c
16
17 Abstract:
18
19 Print Library.
20
21 --*/
22
23 #include <Common/UefiBaseTypes.h>
24 #include <Library/PrintLib.h>
25
26 #include "CommonLib.h"
27 #include "PrintLibInternal.h"
28
29 typedef struct {
30 RETURN_STATUS Status;
31 CHAR8 *String;
32 } STATUS_LOOKUP_TABLE_ENTRY;
33
34 static CONST STATUS_LOOKUP_TABLE_ENTRY StatusString[] = {
35 { RETURN_SUCCESS, "Success" },
36 { RETURN_LOAD_ERROR, "Load Error" },
37 { RETURN_INVALID_PARAMETER, "Invalid Parameter" },
38 { RETURN_UNSUPPORTED, "Unsupported" },
39 { RETURN_BAD_BUFFER_SIZE, "Bad Buffer Size" },
40 { RETURN_BUFFER_TOO_SMALL, "Buffer Too Small" },
41 { RETURN_NOT_READY, "Not Ready" },
42 { RETURN_DEVICE_ERROR, "Device Error" },
43 { RETURN_WRITE_PROTECTED, "Write Protected" },
44 { RETURN_OUT_OF_RESOURCES, "Out of Resources" },
45 { RETURN_VOLUME_CORRUPTED, "Volume Corrupt" },
46 { RETURN_VOLUME_FULL, "Volume Full" },
47 { RETURN_NO_MEDIA, "No Media" },
48 { RETURN_MEDIA_CHANGED, "Media changed" },
49 { RETURN_NOT_FOUND, "Not Found" },
50 { RETURN_ACCESS_DENIED, "Access Denied" },
51 { RETURN_NO_RESPONSE, "No Response" },
52 { RETURN_NO_MAPPING, "No mapping" },
53 { RETURN_TIMEOUT, "Time out" },
54 { RETURN_NOT_STARTED, "Not started" },
55 { RETURN_ALREADY_STARTED, "Already started" },
56 { RETURN_ABORTED, "Aborted" },
57 { RETURN_ICMP_ERROR, "ICMP Error" },
58 { RETURN_TFTP_ERROR, "TFTP Error" },
59 { RETURN_PROTOCOL_ERROR, "Protocol Error" },
60 { RETURN_WARN_UNKNOWN_GLYPH, "Warning Unknown Glyph" },
61 { RETURN_WARN_DELETE_FAILURE, "Warning Delete Failure" },
62 { RETURN_WARN_WRITE_FAILURE, "Warning Write Failure" },
63 { RETURN_WARN_BUFFER_TOO_SMALL, "Warning Buffer Too Small" },
64 { 0, NULL }
65 };
66
67
68 /**
69 VSPrint function to process format and place the results in Buffer. Since a
70 VA_LIST is used this rountine allows the nesting of Vararg routines. Thus
71 this is the main print working routine
72
73 @param StartOfBuffer Unicode buffer to print the results of the parsing of Format into.
74
75 @param BufferSize Maximum number of characters to put into buffer. Zero means
76 no limit.
77
78 @param Flags Intial flags value. Can only have FORMAT_UNICODE and OUTPUT_UNICODE set
79
80 @param FormatString Unicode format string see file header for more details.
81
82 @param Marker Vararg list consumed by processing Format.
83
84 @return Number of characters printed.
85
86 **/
87 UINTN
88 BasePrintLibVSPrint (
89 OUT CHAR8 *Buffer,
90 IN UINTN BufferSize,
91 IN UINTN Flags,
92 IN CONST CHAR8 *Format,
93 IN VA_LIST Marker
94 )
95 {
96 CHAR8 *OriginalBuffer;
97 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
98 UINTN BytesPerOutputCharacter;
99 UINTN BytesPerFormatCharacter;
100 UINTN FormatMask;
101 UINTN FormatCharacter;
102 UINTN Width;
103 UINTN Precision;
104 INT64 Value;
105 CHAR8 *ArgumentString;
106 UINTN Character;
107 GUID *TmpGuid;
108 TIME *TmpTime;
109 UINTN Count;
110 UINTN ArgumentMask;
111 INTN BytesPerArgumentCharacter;
112 UINTN ArgumentCharacter;
113 BOOLEAN Done;
114 UINTN Index;
115 CHAR8 Prefix;
116 BOOLEAN ZeroPad;
117 BOOLEAN Comma;
118 UINTN Digits;
119 UINTN Radix;
120 RETURN_STATUS Status;
121
122 OriginalBuffer = Buffer;
123
124 if ((Flags & OUTPUT_UNICODE) != 0) {
125 BytesPerOutputCharacter = 2;
126 } else {
127 BytesPerOutputCharacter = 1;
128 }
129 if ((Flags & FORMAT_UNICODE) != 0) {
130 BytesPerFormatCharacter = 2;
131 FormatMask = 0xffff;
132 } else {
133 BytesPerFormatCharacter = 1;
134 FormatMask = 0xff;
135 }
136
137 //
138 // Reserve space for the Null terminator.
139 // If BufferSize is 0, this will set BufferSize to the max unsigned value
140 //
141 BufferSize--;
142
143 //
144 // Get the first character from the format string
145 //
146 FormatCharacter = (*Format | (*(Format + 1) << 8)) & FormatMask;
147
148 //
149 // Loop until the end of the format string is reached or the output buffer is full
150 //
151 while (FormatCharacter != 0 && BufferSize > 0) {
152 //
153 // Clear all the flag bits except those that may have been passed in
154 //
155 Flags &= (OUTPUT_UNICODE | FORMAT_UNICODE);
156
157 //
158 // Set the default width to zero, and the default precision to 1
159 //
160 Width = 0;
161 Precision = 1;
162 Prefix = 0;
163 Comma = FALSE;
164 ZeroPad = FALSE;
165 Count = 0;
166 Digits = 0;
167
168 switch (FormatCharacter) {
169 case '%':
170 //
171 // Parse Flags and Width
172 //
173 for (Done = FALSE; !Done; ) {
174 Format += BytesPerFormatCharacter;
175 FormatCharacter = (*Format | (*(Format + 1) << 8)) & FormatMask;
176 switch (FormatCharacter) {
177 case '.':
178 Flags |= PRECISION;
179 break;
180 case '-':
181 Flags |= LEFT_JUSTIFY;
182 break;
183 case '+':
184 Flags |= PREFIX_SIGN;
185 break;
186 case ' ':
187 Flags |= PREFIX_BLANK;
188 break;
189 case ',':
190 Flags |= COMMA_TYPE;
191 break;
192 case 'L':
193 case 'l':
194 Flags |= LONG_TYPE;
195 break;
196 case '*':
197 if ((Flags & PRECISION) == 0) {
198 Flags |= PAD_TO_WIDTH;
199 Width = VA_ARG (Marker, UINTN);
200 } else {
201 Precision = VA_ARG (Marker, UINTN);
202 }
203 break;
204 case '0':
205 if ((Flags & PRECISION) == 0) {
206 Flags |= PREFIX_ZERO;
207 }
208 case '1':
209 case '2':
210 case '3':
211 case '4':
212 case '5':
213 case '6':
214 case '7':
215 case '8':
216 case '9':
217 for (Count = 0; ((FormatCharacter >= '0') && (FormatCharacter <= '9')); ){
218 Count = (Count * 10) + FormatCharacter - '0';
219 Format += BytesPerFormatCharacter;
220 FormatCharacter = (*Format | (*(Format + 1) << 8)) & FormatMask;
221 }
222 Format -= BytesPerFormatCharacter;
223 if ((Flags & PRECISION) == 0) {
224 Flags |= PAD_TO_WIDTH;
225 Width = Count;
226 } else {
227 Precision = Count;
228 }
229 break;
230 default:
231 Done = TRUE;
232 break;
233 }
234 }
235
236 //
237 // Limit the maximum field width to the remaining characters in the output buffer
238 //
239 if (Width > BufferSize) {
240 Width = BufferSize;
241 }
242
243 //
244 // Handle each argument type
245 //
246 switch (FormatCharacter) {
247 case 'X':
248 Flags |= PREFIX_ZERO;
249 //
250 // break skiped on purpose
251 //
252 case 'x':
253 Flags |= RADIX_HEX;
254 //
255 // break skiped on purpose
256 //
257 case 'd':
258 if ((Flags & LONG_TYPE) == 0) {
259 Value = (VA_ARG (Marker, INTN));
260 } else {
261 Value = VA_ARG (Marker, INT64);
262 }
263 if ((Flags & PREFIX_BLANK) != 0) {
264 Prefix = ' ';
265 }
266 if ((Flags & PREFIX_SIGN) != 0) {
267 Prefix = '+';
268 }
269 if ((Flags & COMMA_TYPE) != 0) {
270 Comma = TRUE;
271 }
272 if ((Flags & RADIX_HEX) == 0) {
273 Radix = 10;
274 if (Comma) {
275 Flags &= (~PREFIX_ZERO);
276 Precision = 1;
277 }
278 if (Value < 0) {
279 Flags |= PREFIX_SIGN;
280 Prefix = '-';
281 Value = -Value;
282 }
283 } else {
284 Radix = 16;
285 Comma = FALSE;
286 if ((Flags & LONG_TYPE) == 0 && Value < 0) {
287 Value = (UINTN)Value;
288 }
289 }
290 //
291 // Convert Value to a reversed string
292 //
293 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix);
294 if (Value == 0 && Precision == 0) {
295 Count = 0;
296 }
297 ArgumentString = (CHAR8 *)ValueBuffer + Count;
298 Digits = 3 - (Count % 3);
299 if (Comma && Count != 0) {
300 Count += ((Count - 1) / 3);
301 }
302 if (Prefix != 0) {
303 Count++;
304 }
305 Flags |= ARGUMENT_REVERSED;
306 ZeroPad = TRUE;
307 if ((Flags & PREFIX_ZERO) != 0) {
308 if ((Flags & PAD_TO_WIDTH) != 0) {
309 if ((Flags & PRECISION) == 0) {
310 Precision = Width;
311 }
312 }
313 }
314 break;
315
316 case 's':
317 case 'S':
318 Flags |= ARGUMENT_UNICODE;
319 //
320 // break skipped on purpose
321 //
322 case 'a':
323 ArgumentString = (CHAR8 *)VA_ARG (Marker, CHAR8 *);
324 if (ArgumentString == NULL) {
325 Flags &= (~ARGUMENT_UNICODE);
326 ArgumentString = "<null string>";
327 }
328 break;
329
330 case 'c':
331 Character = VA_ARG (Marker, UINTN) & 0xffff;
332 ArgumentString = (CHAR8 *)&Character;
333 Flags |= ARGUMENT_UNICODE;
334 break;
335
336 case 'g':
337 TmpGuid = VA_ARG (Marker, GUID *);
338 if (TmpGuid == NULL) {
339 ArgumentString = "<null guid>";
340 } else {
341 BasePrintLibSPrint (
342 ValueBuffer,
343 0,
344 0,
345 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
346 TmpGuid->Data1,
347 TmpGuid->Data2,
348 TmpGuid->Data3,
349 TmpGuid->Data4[0],
350 TmpGuid->Data4[1],
351 TmpGuid->Data4[2],
352 TmpGuid->Data4[3],
353 TmpGuid->Data4[4],
354 TmpGuid->Data4[5],
355 TmpGuid->Data4[6],
356 TmpGuid->Data4[7]
357 );
358 ArgumentString = ValueBuffer;
359 }
360 break;
361
362 case 't':
363 TmpTime = VA_ARG (Marker, TIME *);
364 if (TmpTime == NULL) {
365 ArgumentString = "<null time>";
366 } else {
367 BasePrintLibSPrint (
368 ValueBuffer,
369 0,
370 0,
371 "%02d/%02d/%04d %02d:%02d",
372 TmpTime->Month,
373 TmpTime->Day,
374 TmpTime->Year,
375 TmpTime->Hour,
376 TmpTime->Minute
377 );
378 ArgumentString = ValueBuffer;
379 }
380 break;
381
382 case 'r':
383 Status = VA_ARG (Marker, RETURN_STATUS);
384 ArgumentString = ValueBuffer;
385 for (Index = 0; StatusString[Index].String != NULL; Index++) {
386 if (Status == StatusString[Index].Status) {
387 ArgumentString = StatusString[Index].String;
388 }
389 }
390 if (ArgumentString == ValueBuffer) {
391 BasePrintLibSPrint ((CHAR8 *) ValueBuffer, 0, 0, "%08X", Status);
392 }
393 break;
394
395 case '%':
396 default:
397 //
398 // if the type is '%' or unknown, then print it to the screen
399 //
400 ArgumentString = (CHAR8 *)&FormatCharacter;
401 Flags |= ARGUMENT_UNICODE;
402 break;
403 }
404 break;
405 case '\n':
406 ArgumentString = "\n";
407
408 break;
409 default:
410 ArgumentString = (CHAR8 *)&FormatCharacter;
411 Flags |= ARGUMENT_UNICODE;
412 break;
413 }
414
415 //
416 // Retrieve the ArgumentString attriubutes
417 //
418 if ((Flags & ARGUMENT_UNICODE) != 0) {
419 ArgumentMask = 0xffff;
420 BytesPerArgumentCharacter = 2;
421 } else {
422 ArgumentMask = 0xff;
423 BytesPerArgumentCharacter = 1;
424 }
425 if ((Flags & ARGUMENT_REVERSED) != 0) {
426 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;
427 } else {
428 //
429 // Compute the number of characters in ArgumentString and store it in Count
430 // ArgumentString is either null-terminated, or it contains Precision characters
431 //
432 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {
433 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;
434 if (ArgumentCharacter == 0) {
435 break;
436 }
437 }
438 }
439
440 //
441 // Limit the length of the string to append to the remaining characters in the output buffer
442 //
443 if (Count > BufferSize) {
444 Count = BufferSize;
445 }
446 if (Precision < Count) {
447 Precision = Count;
448 }
449
450 //
451 // Pad before the string
452 //
453 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {
454 Buffer = BasePrintLibFillBuffer (Buffer, Width - Precision, ' ', BytesPerOutputCharacter);
455 }
456
457 if (ZeroPad) {
458 if (Prefix != 0) {
459 Buffer = BasePrintLibFillBuffer (Buffer, 1, Prefix, BytesPerOutputCharacter);
460 }
461 Buffer = BasePrintLibFillBuffer (Buffer, Precision - Count, '0', BytesPerOutputCharacter);
462 } else {
463 Buffer = BasePrintLibFillBuffer (Buffer, Precision - Count, ' ', BytesPerOutputCharacter);
464 if (Prefix != 0) {
465 Buffer = BasePrintLibFillBuffer (Buffer, 1, Prefix, BytesPerOutputCharacter);
466 }
467 }
468
469 //
470 // Output the Prefix character if it is present
471 //
472 Index = 0;
473 if (Prefix) {
474 Index++;
475 }
476
477 //
478 // Copy the string into the output buffer performing the required type conversions
479 //
480 while (Index < Count) {
481 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;
482
483 Buffer = BasePrintLibFillBuffer (Buffer, 1, ArgumentCharacter, BytesPerOutputCharacter);
484 ArgumentString += BytesPerArgumentCharacter;
485 Index++;
486 if (Comma) {
487 Digits++;
488 if (Digits == 3) {
489 Digits = 0;
490 Index++;
491 if (Index < Count) {
492 Buffer = BasePrintLibFillBuffer (Buffer, 1, ',', BytesPerOutputCharacter);
493 }
494 }
495 }
496 }
497
498 //
499 // Pad after the string
500 //
501 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {
502 Buffer = BasePrintLibFillBuffer (Buffer, Width - Precision, ' ', BytesPerOutputCharacter);
503 }
504
505 //
506 // Reduce the number of characters
507 //
508 BufferSize -= Count;
509
510 //
511 // Get the next character from the format string
512 //
513 Format += BytesPerFormatCharacter;
514
515 //
516 // Get the next character from the format string
517 //
518 FormatCharacter = (*Format | (*(Format + 1) << 8)) & FormatMask;
519 }
520
521 //
522 // Null terminate the Unicode or ASCII string
523 //
524 Buffer = BasePrintLibFillBuffer (Buffer, 1, 0, BytesPerOutputCharacter);
525
526 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);
527 }
528
529 UINTN
530 BasePrintLibSPrint (
531 OUT CHAR8 *StartOfBuffer,
532 IN UINTN BufferSize,
533 IN UINTN Flags,
534 IN CONST CHAR8 *FormatString,
535 ...
536 )
537 {
538 VA_LIST Marker;
539
540 VA_START (Marker, FormatString);
541 return BasePrintLibVSPrint (StartOfBuffer, BufferSize, Flags, FormatString, Marker);
542 }
543
544 UINTN
545 EFIAPI
546 UnicodeVSPrint (
547 OUT CHAR16 *StartOfBuffer,
548 IN UINTN BufferSize,
549 IN CONST CHAR16 *FormatString,
550 IN VA_LIST Marker
551 )
552 {
553 return BasePrintLibVSPrint ((CHAR8 *)StartOfBuffer, BufferSize >> 1, FORMAT_UNICODE | OUTPUT_UNICODE, (CHAR8 *)FormatString, Marker);
554 }
555
556 UINTN
557 EFIAPI
558 UnicodeSPrint (
559 OUT CHAR16 *StartOfBuffer,
560 IN UINTN BufferSize,
561 IN CONST CHAR16 *FormatString,
562 ...
563 )
564 {
565 VA_LIST Marker;
566
567 VA_START (Marker, FormatString);
568 return UnicodeVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
569 }
570
571 UINTN
572 EFIAPI
573 UnicodeVSPrintAsciiFormat (
574 OUT CHAR16 *StartOfBuffer,
575 IN UINTN BufferSize,
576 IN CONST CHAR8 *FormatString,
577 IN VA_LIST Marker
578 )
579 {
580 return BasePrintLibVSPrint ((CHAR8 *)StartOfBuffer, BufferSize >> 1, OUTPUT_UNICODE,FormatString, Marker);
581 }
582
583 UINTN
584 EFIAPI
585 UnicodeSPrintAsciiFormat (
586 OUT CHAR16 *StartOfBuffer,
587 IN UINTN BufferSize,
588 IN CONST CHAR8 *FormatString,
589 ...
590 )
591 {
592 VA_LIST Marker;
593
594 VA_START (Marker, FormatString);
595 return UnicodeVSPrintAsciiFormat (StartOfBuffer, BufferSize >> 1, FormatString, Marker);
596 }
597
598 UINTN
599 EFIAPI
600 AsciiVSPrint (
601 OUT CHAR8 *StartOfBuffer,
602 IN UINTN BufferSize,
603 IN CONST CHAR8 *FormatString,
604 IN VA_LIST Marker
605 )
606 {
607 return BasePrintLibVSPrint (StartOfBuffer, BufferSize, 0, FormatString, Marker);
608 }
609
610 UINTN
611 EFIAPI
612 AsciiSPrint (
613 OUT CHAR8 *StartOfBuffer,
614 IN UINTN BufferSize,
615 IN CONST CHAR8 *FormatString,
616 ...
617 )
618 {
619 VA_LIST Marker;
620
621 VA_START (Marker, FormatString);
622 return AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
623 }
624
625 UINTN
626 EFIAPI
627 AsciiVSPrintUnicodeFormat (
628 OUT CHAR8 *StartOfBuffer,
629 IN UINTN BufferSize,
630 IN CONST CHAR16 *FormatString,
631 IN VA_LIST Marker
632 )
633 {
634 return BasePrintLibVSPrint (StartOfBuffer, BufferSize, FORMAT_UNICODE, (CHAR8 *)FormatString, Marker);
635 }
636
637 UINTN
638 EFIAPI
639 AsciiSPrintUnicodeFormat (
640 OUT CHAR8 *StartOfBuffer,
641 IN UINTN BufferSize,
642 IN CONST CHAR16 *FormatString,
643 ...
644 )
645 {
646 VA_LIST Marker;
647
648 VA_START (Marker, FormatString);
649 return AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);
650 }
651
652 UINTN
653 EFIAPI
654 UnicodeValueToString (
655 IN OUT CHAR16 *Buffer,
656 IN UINTN Flags,
657 IN INT64 Value,
658 IN UINTN Width
659 )
660 {
661 return BasePrintLibConvertValueToString ((CHAR8 *)Buffer, Flags, Value, Width, 2);
662 }
663
664 UINTN
665 EFIAPI
666 AsciiValueToString (
667 IN OUT CHAR8 *Buffer,
668 IN UINTN Flags,
669 IN INT64 Value,
670 IN UINTN Width
671 )
672 {
673 return BasePrintLibConvertValueToString ((CHAR8 *)Buffer, Flags, Value, Width, 1);
674 }