]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/String/PrintLib.c
Removed one useless makefile, and corrected some BSD licenses
[mirror_edk2.git] / Tools / Source / TianoTools / 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 = "\r\n";
407 break;
408 default:
409 ArgumentString = (CHAR8 *)&FormatCharacter;
410 Flags |= ARGUMENT_UNICODE;
411 break;
412 }
413
414 //
415 // Retrieve the ArgumentString attriubutes
416 //
417 if ((Flags & ARGUMENT_UNICODE) != 0) {
418 ArgumentMask = 0xffff;
419 BytesPerArgumentCharacter = 2;
420 } else {
421 ArgumentMask = 0xff;
422 BytesPerArgumentCharacter = 1;
423 }
424 if ((Flags & ARGUMENT_REVERSED) != 0) {
425 BytesPerArgumentCharacter = -BytesPerArgumentCharacter;
426 } else {
427 //
428 // Compute the number of characters in ArgumentString and store it in Count
429 // ArgumentString is either null-terminated, or it contains Precision characters
430 //
431 for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {
432 ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;
433 if (ArgumentCharacter == 0) {
434 break;
435 }
436 }
437 }
438
439 //
440 // Limit the length of the string to append to the remaining characters in the output buffer
441 //
442 if (Count > BufferSize) {
443 Count = BufferSize;
444 }
445 if (Precision < Count) {
446 Precision = Count;
447 }
448
449 //
450 // Pad before the string
451 //
452 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {
453 Buffer = BasePrintLibFillBuffer (Buffer, Width - Precision, ' ', BytesPerOutputCharacter);
454 }
455
456 if (ZeroPad) {
457 if (Prefix != 0) {
458 Buffer = BasePrintLibFillBuffer (Buffer, 1, Prefix, BytesPerOutputCharacter);
459 }
460 Buffer = BasePrintLibFillBuffer (Buffer, Precision - Count, '0', BytesPerOutputCharacter);
461 } else {
462 Buffer = BasePrintLibFillBuffer (Buffer, Precision - Count, ' ', BytesPerOutputCharacter);
463 if (Prefix != 0) {
464 Buffer = BasePrintLibFillBuffer (Buffer, 1, Prefix, BytesPerOutputCharacter);
465 }
466 }
467
468 //
469 // Output the Prefix character if it is present
470 //
471 Index = 0;
472 if (Prefix) {
473 Index++;
474 }
475
476 //
477 // Copy the string into the output buffer performing the required type conversions
478 //
479 while (Index < Count) {
480 ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;
481
482 Buffer = BasePrintLibFillBuffer (Buffer, 1, ArgumentCharacter, BytesPerOutputCharacter);
483 ArgumentString += BytesPerArgumentCharacter;
484 Index++;
485 if (Comma) {
486 Digits++;
487 if (Digits == 3) {
488 Digits = 0;
489 Index++;
490 if (Index < Count) {
491 Buffer = BasePrintLibFillBuffer (Buffer, 1, ',', BytesPerOutputCharacter);
492 }
493 }
494 }
495 }
496
497 //
498 // Pad after the string
499 //
500 if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {
501 Buffer = BasePrintLibFillBuffer (Buffer, Width - Precision, ' ', BytesPerOutputCharacter);
502 }
503
504 //
505 // Reduce the number of characters
506 //
507 BufferSize -= Count;
508
509 //
510 // Get the next character from the format string
511 //
512 Format += BytesPerFormatCharacter;
513
514 //
515 // Get the next character from the format string
516 //
517 FormatCharacter = (*Format | (*(Format + 1) << 8)) & FormatMask;
518 }
519
520 //
521 // Null terminate the Unicode or ASCII string
522 //
523 Buffer = BasePrintLibFillBuffer (Buffer, 1, 0, BytesPerOutputCharacter);
524
525 return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);
526 }
527
528 UINTN
529 BasePrintLibSPrint (
530 OUT CHAR8 *StartOfBuffer,
531 IN UINTN BufferSize,
532 IN UINTN Flags,
533 IN CONST CHAR8 *FormatString,
534 ...
535 )
536 {
537 VA_LIST Marker;
538
539 VA_START (Marker, FormatString);
540 return BasePrintLibVSPrint (StartOfBuffer, BufferSize, Flags, FormatString, Marker);
541 }
542
543 UINTN
544 EFIAPI
545 UnicodeVSPrint (
546 OUT CHAR16 *StartOfBuffer,
547 IN UINTN BufferSize,
548 IN CONST CHAR16 *FormatString,
549 IN VA_LIST Marker
550 )
551 {
552 return BasePrintLibVSPrint ((CHAR8 *)StartOfBuffer, BufferSize >> 1, FORMAT_UNICODE | OUTPUT_UNICODE, (CHAR8 *)FormatString, Marker);
553 }
554
555 UINTN
556 EFIAPI
557 UnicodeSPrint (
558 OUT CHAR16 *StartOfBuffer,
559 IN UINTN BufferSize,
560 IN CONST CHAR16 *FormatString,
561 ...
562 )
563 {
564 VA_LIST Marker;
565
566 VA_START (Marker, FormatString);
567 return UnicodeVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
568 }
569
570 UINTN
571 EFIAPI
572 UnicodeVSPrintAsciiFormat (
573 OUT CHAR16 *StartOfBuffer,
574 IN UINTN BufferSize,
575 IN CONST CHAR8 *FormatString,
576 IN VA_LIST Marker
577 )
578 {
579 return BasePrintLibVSPrint ((CHAR8 *)StartOfBuffer, BufferSize >> 1, OUTPUT_UNICODE,FormatString, Marker);
580 }
581
582 UINTN
583 EFIAPI
584 UnicodeSPrintAsciiFormat (
585 OUT CHAR16 *StartOfBuffer,
586 IN UINTN BufferSize,
587 IN CONST CHAR8 *FormatString,
588 ...
589 )
590 {
591 VA_LIST Marker;
592
593 VA_START (Marker, FormatString);
594 return UnicodeVSPrintAsciiFormat (StartOfBuffer, BufferSize >> 1, FormatString, Marker);
595 }
596
597 UINTN
598 EFIAPI
599 AsciiVSPrint (
600 OUT CHAR8 *StartOfBuffer,
601 IN UINTN BufferSize,
602 IN CONST CHAR8 *FormatString,
603 IN VA_LIST Marker
604 )
605 {
606 return BasePrintLibVSPrint (StartOfBuffer, BufferSize, 0, FormatString, Marker);
607 }
608
609 UINTN
610 EFIAPI
611 AsciiSPrint (
612 OUT CHAR8 *StartOfBuffer,
613 IN UINTN BufferSize,
614 IN CONST CHAR8 *FormatString,
615 ...
616 )
617 {
618 VA_LIST Marker;
619
620 VA_START (Marker, FormatString);
621 return AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
622 }
623
624 UINTN
625 EFIAPI
626 AsciiVSPrintUnicodeFormat (
627 OUT CHAR8 *StartOfBuffer,
628 IN UINTN BufferSize,
629 IN CONST CHAR16 *FormatString,
630 IN VA_LIST Marker
631 )
632 {
633 return BasePrintLibVSPrint (StartOfBuffer, BufferSize, FORMAT_UNICODE, (CHAR8 *)FormatString, Marker);
634 }
635
636 UINTN
637 EFIAPI
638 AsciiSPrintUnicodeFormat (
639 OUT CHAR8 *StartOfBuffer,
640 IN UINTN BufferSize,
641 IN CONST CHAR16 *FormatString,
642 ...
643 )
644 {
645 VA_LIST Marker;
646
647 VA_START (Marker, FormatString);
648 return AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);
649 }
650
651 UINTN
652 EFIAPI
653 UnicodeValueToString (
654 IN OUT CHAR16 *Buffer,
655 IN UINTN Flags,
656 IN INT64 Value,
657 IN UINTN Width
658 )
659 {
660 return BasePrintLibConvertValueToString ((CHAR8 *)Buffer, Flags, Value, Width, 2);
661 }
662
663 UINTN
664 EFIAPI
665 AsciiValueToString (
666 IN OUT CHAR8 *Buffer,
667 IN UINTN Flags,
668 IN INT64 Value,
669 IN UINTN Width
670 )
671 {
672 return BasePrintLibConvertValueToString ((CHAR8 *)Buffer, Flags, Value, Width, 1);
673 }