4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
11 * from hush: simple_itoa() was lifted from boa-0.93.15
15 #include <linux/types.h>
16 #include <linux/string.h>
17 #include <linux/ctype.h>
24 #define noinline __attribute__((noinline))
26 /* we use this so that we can do without the ctype library */
27 #define is_digit(c) ((c) >= '0' && (c) <= '9')
29 static int skip_atoi(const char **s)
34 i = i * 10 + *((*s)++) - '0';
39 /* Decimal conversion is by far the most typical, and is used
40 * for /proc and /sys data. This directly impacts e.g. top performance
41 * with many processes running. We optimize it for speed
43 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
44 * (with permission from the author, Douglas W. Jones). */
46 /* Formats correctly any integer in [0,99999].
47 * Outputs from one to five digits depending on input.
48 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
49 static char *put_dec_trunc(char *buf, unsigned q)
51 unsigned d3, d2, d1, d0;
56 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
57 q = (d0 * 0xcd) >> 11;
59 *buf++ = d0 + '0'; /* least significant digit */
60 d1 = q + 9*d3 + 5*d2 + d1;
62 q = (d1 * 0xcd) >> 11;
64 *buf++ = d1 + '0'; /* next digit */
67 if ((d2 != 0) || (d3 != 0)) {
70 *buf++ = d2 + '0'; /* next digit */
74 q = (d3 * 0xcd) >> 11;
76 *buf++ = d3 + '0'; /* next digit */
78 *buf++ = q + '0'; /* most sign. digit */
84 /* Same with if's removed. Always emits five digits */
85 static char *put_dec_full(char *buf, unsigned q)
87 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
88 /* but anyway, gcc produces better code with full-sized ints */
89 unsigned d3, d2, d1, d0;
95 * Possible ways to approx. divide by 10
96 * gcc -O2 replaces multiply with shifts and adds
97 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
98 * (x * 0x67) >> 10: 1100111
99 * (x * 0x34) >> 9: 110100 - same
100 * (x * 0x1a) >> 8: 11010 - same
101 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
104 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
105 q = (d0 * 0xcd) >> 11;
108 d1 = q + 9*d3 + 5*d2 + d1;
109 q = (d1 * 0xcd) >> 11;
119 q = (d3 * 0xcd) >> 11; /* - shorter code */
120 /* q = (d3 * 0x67) >> 10; - would also work */
126 /* No inlining helps gcc to use registers better */
127 static noinline char *put_dec(char *buf, uint64_t num)
132 return put_dec_trunc(buf, num);
133 rem = do_div(num, 100000);
134 buf = put_dec_full(buf, rem);
138 #define ZEROPAD 1 /* pad with zero */
139 #define SIGN 2 /* unsigned/signed long */
140 #define PLUS 4 /* show plus */
141 #define SPACE 8 /* space if plus */
142 #define LEFT 16 /* left justified */
143 #define SMALL 32 /* Must be 32 == 0x20 */
144 #define SPECIAL 64 /* 0x */
147 * Macro to add a new character to our output string, but only if it will
148 * fit. The macro moves to the next character position in the output string.
150 #define ADDCH(str, ch) do { \
156 static char *number(char *buf, char *end, u64 num,
157 int base, int size, int precision, int type)
159 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
160 static const char digits[16] = "0123456789ABCDEF";
165 int need_pfx = ((type & SPECIAL) && base != 10);
168 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
169 * produces same digits or (maybe lowercased) letters */
170 locase = (type & SMALL);
179 } else if (type & PLUS) {
182 } else if (type & SPACE) {
193 /* generate full string in tmp[], in reverse order */
197 /* Generic code, for any base:
199 tmp[i++] = (digits[do_div(num,base)] | locase);
202 else if (base != 10) { /* 8 or 16 */
210 tmp[i++] = (digits[((unsigned char)num) & mask]
214 } else { /* base 10 */
215 i = put_dec(tmp, num) - tmp;
218 /* printing 100 using %2d gives "100", not "00" */
221 /* leading space padding */
223 if (!(type & (ZEROPAD + LEFT))) {
230 /* "0x" / "0" prefix */
234 ADDCH(buf, 'X' | locase);
236 /* zero or space padding */
237 if (!(type & LEFT)) {
238 char c = (type & ZEROPAD) ? '0' : ' ';
243 /* hmm even more zero padding? */
244 while (i <= --precision)
246 /* actual digits of result */
249 /* trailing space padding */
255 static char *string(char *buf, char *end, char *s, int field_width,
256 int precision, int flags)
263 len = strnlen(s, precision);
266 while (len < field_width--)
268 for (i = 0; i < len; ++i)
270 while (len < field_width--)
275 static char *string16(char *buf, char *end, u16 *s, int field_width,
276 int precision, int flags)
278 u16 *str = s ? s : L"<NULL>";
279 int utf16_len = utf16_strnlen(str, precision);
280 u8 utf8[utf16_len * MAX_UTF8_PER_UTF16];
283 utf8_len = utf16_to_utf8(utf8, str, utf16_len) - utf8;
286 while (utf8_len < field_width--)
288 for (i = 0; i < utf8_len; ++i)
290 while (utf8_len < field_width--)
295 #ifdef CONFIG_CMD_NET
296 static const char hex_asc[] = "0123456789abcdef";
297 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
298 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
300 static inline char *pack_hex_byte(char *buf, u8 byte)
302 *buf++ = hex_asc_hi(byte);
303 *buf++ = hex_asc_lo(byte);
307 static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
308 int precision, int flags)
310 /* (6 * 2 hex digits), 5 colons and trailing zero */
311 char mac_addr[6 * 3];
315 for (i = 0; i < 6; i++) {
316 p = pack_hex_byte(p, addr[i]);
317 if (!(flags & SPECIAL) && i != 5)
322 return string(buf, end, mac_addr, field_width, precision,
326 static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
327 int precision, int flags)
329 /* (8 * 4 hex digits), 7 colons and trailing zero */
330 char ip6_addr[8 * 5];
334 for (i = 0; i < 8; i++) {
335 p = pack_hex_byte(p, addr[2 * i]);
336 p = pack_hex_byte(p, addr[2 * i + 1]);
337 if (!(flags & SPECIAL) && i != 7)
342 return string(buf, end, ip6_addr, field_width, precision,
346 static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
347 int precision, int flags)
349 /* (4 * 3 decimal digits), 3 dots and trailing zero */
350 char ip4_addr[4 * 4];
351 char temp[3]; /* hold each IP quad in reverse order */
355 for (i = 0; i < 4; i++) {
356 digits = put_dec_trunc(temp, addr[i]) - temp;
357 /* reverse the digits in the quad */
365 return string(buf, end, ip4_addr, field_width, precision,
370 #ifdef CONFIG_LIB_UUID
372 * This works (roughly) the same way as linux's, but we currently always
373 * print lower-case (ie. we just keep %pUB and %pUL for compat with linux),
374 * mostly just because that is what uuid_bin_to_str() supports.
376 * %pUb: 01020304-0506-0708-090a-0b0c0d0e0f10
377 * %pUl: 04030201-0605-0807-090a-0b0c0d0e0f10
379 static char *uuid_string(char *buf, char *end, u8 *addr, int field_width,
380 int precision, int flags, const char *fmt)
382 char uuid[UUID_STR_LEN + 1];
383 int str_format = UUID_STR_FORMAT_STD;
388 str_format = UUID_STR_FORMAT_GUID;
392 /* this is the default */
398 uuid_bin_to_str(addr, uuid, str_format);
400 return string(buf, end, uuid, field_width, precision, flags);
405 * Show a '%p' thing. A kernel extension is that the '%p' is followed
406 * by an extra set of alphanumeric characters that are extended format
409 * Right now we handle:
411 * - 'M' For a 6-byte MAC address, it prints the address in the
412 * usual colon-separated hex notation
413 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
414 * decimal for v4 and colon separated network-order 16 bit hex for v6)
415 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
418 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
419 * function pointers are really function descriptors, which contain a
420 * pointer to the real address.
422 static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
423 int field_width, int precision, int flags)
425 u64 num = (uintptr_t)ptr;
428 * Being a boot loader, we explicitly allow pointers to
429 * (physical) address null.
433 return string(buf, end, "(null)", field_width, precision,
438 #ifdef CONFIG_CMD_NET
440 flags |= SPECIAL | ZEROPAD;
445 field_width = sizeof(phys_addr_t) * 2 + 2;
446 num = *(phys_addr_t *)ptr;
454 return mac_address_string(buf, end, ptr, field_width,
461 return ip6_addr_string(buf, end, ptr, field_width,
464 return ip4_addr_string(buf, end, ptr, field_width,
469 #ifdef CONFIG_LIB_UUID
471 return uuid_string(buf, end, ptr, field_width, precision,
478 if (field_width == -1) {
479 field_width = 2*sizeof(void *);
482 return number(buf, end, num, 16, field_width, precision, flags);
485 static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
492 int flags; /* flags to number() */
494 int field_width; /* width of output field */
495 int precision; /* min. # of digits for integers; max
496 number of chars for from string */
497 int qualifier; /* 'h', 'l', or 'L' for integer fields */
498 /* 'z' support added 23/7/1999 S.H. */
499 /* 'z' changed to 'Z' --davidm 1/25/99 */
500 /* 't' added for ptrdiff_t */
501 char *end = buf + size;
503 /* Make sure end is always >= buf - do we want this in U-Boot? */
510 for (; *fmt ; ++fmt) {
519 ++fmt; /* this also skips first '%' */
538 /* get field width */
541 field_width = skip_atoi(&fmt);
542 else if (*fmt == '*') {
544 /* it's the next argument */
545 field_width = va_arg(args, int);
546 if (field_width < 0) {
547 field_width = -field_width;
552 /* get the precision */
557 precision = skip_atoi(&fmt);
558 else if (*fmt == '*') {
560 /* it's the next argument */
561 precision = va_arg(args, int);
567 /* get the conversion qualifier */
569 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
570 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
573 if (qualifier == 'l' && *fmt == 'l') {
584 if (!(flags & LEFT)) {
585 while (--field_width > 0)
588 ADDCH(str, (unsigned char) va_arg(args, int));
589 while (--field_width > 0)
594 if (qualifier == 'l' && !IS_ENABLED(CONFIG_SPL_BUILD)) {
595 str = string16(str, end, va_arg(args, u16 *),
596 field_width, precision, flags);
598 str = string(str, end, va_arg(args, char *),
599 field_width, precision, flags);
604 str = pointer(fmt + 1, str, end,
605 va_arg(args, void *),
606 field_width, precision, flags);
607 /* Skip all alphanumeric pointer suffixes */
608 while (isalnum(fmt[1]))
613 if (qualifier == 'l') {
614 long *ip = va_arg(args, long *);
617 int *ip = va_arg(args, int *);
626 /* integer number formats - set up the flags and "break" */
651 if (qualifier == 'L') /* "quad" for 64 bit variables */
652 num = va_arg(args, unsigned long long);
653 else if (qualifier == 'l') {
654 num = va_arg(args, unsigned long);
656 num = (signed long) num;
657 } else if (qualifier == 'Z' || qualifier == 'z') {
658 num = va_arg(args, size_t);
659 } else if (qualifier == 't') {
660 num = va_arg(args, ptrdiff_t);
661 } else if (qualifier == 'h') {
662 num = (unsigned short) va_arg(args, int);
664 num = (signed short) num;
666 num = va_arg(args, unsigned int);
668 num = (signed int) num;
670 str = number(str, end, num, base, field_width, precision,
680 /* the trailing null byte doesn't count towards the total */
684 int vsnprintf(char *buf, size_t size, const char *fmt,
687 return vsnprintf_internal(buf, size, fmt, args);
690 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
694 i = vsnprintf(buf, size, fmt, args);
696 if (likely(i < size))
703 int snprintf(char *buf, size_t size, const char *fmt, ...)
709 i = vsnprintf(buf, size, fmt, args);
715 int scnprintf(char *buf, size_t size, const char *fmt, ...)
721 i = vscnprintf(buf, size, fmt, args);
728 * Format a string and place it in a buffer (va_list version)
730 * @param buf The buffer to place the result into
731 * @param fmt The format string to use
732 * @param args Arguments for the format string
734 * The function returns the number of characters written
735 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
738 * If you're not already dealing with a va_list consider using sprintf().
740 int vsprintf(char *buf, const char *fmt, va_list args)
742 return vsnprintf_internal(buf, INT_MAX, fmt, args);
745 int sprintf(char *buf, const char *fmt, ...)
751 i = vsprintf(buf, fmt, args);
756 int printf(const char *fmt, ...)
760 char printbuffer[CONFIG_SYS_PBSIZE];
765 * For this to work, printbuffer must be larger than
766 * anything we ever want to print.
768 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
771 /* Print the string */
776 int vprintf(const char *fmt, va_list args)
779 char printbuffer[CONFIG_SYS_PBSIZE];
782 * For this to work, printbuffer must be larger than
783 * anything we ever want to print.
785 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
787 /* Print the string */
793 void __assert_fail(const char *assertion, const char *file, unsigned line,
794 const char *function)
796 /* This will not return */
797 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
801 char *simple_itoa(ulong i)
803 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
804 static char local[22];
805 char *p = &local[21];
815 /* We don't seem to have %'d in U-Boot */
816 void print_grouped_ull(unsigned long long int_val, int digits)
821 digits = (digits + 2) / 3;
822 sprintf(str, "%*llu", digits * 3, int_val);
823 for (s = str; *s; s += grab) {
825 putc(s[-1] != ' ' ? ',' : ' ');
826 printf("%.*s", grab, s);
831 bool str2off(const char *p, loff_t *num)
835 *num = simple_strtoull(p, &endptr, 16);
836 return *p != '\0' && *endptr == '\0';
839 bool str2long(const char *p, ulong *num)
843 *num = simple_strtoul(p, &endptr, 16);
844 return *p != '\0' && *endptr == '\0';