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>
23 #define noinline __attribute__((noinline))
25 /* we use this so that we can do without the ctype library */
26 #define is_digit(c) ((c) >= '0' && (c) <= '9')
28 static int skip_atoi(const char **s)
33 i = i * 10 + *((*s)++) - '0';
38 /* Decimal conversion is by far the most typical, and is used
39 * for /proc and /sys data. This directly impacts e.g. top performance
40 * with many processes running. We optimize it for speed
42 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
43 * (with permission from the author, Douglas W. Jones). */
45 /* Formats correctly any integer in [0,99999].
46 * Outputs from one to five digits depending on input.
47 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
48 static char *put_dec_trunc(char *buf, unsigned q)
50 unsigned d3, d2, d1, d0;
55 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
56 q = (d0 * 0xcd) >> 11;
58 *buf++ = d0 + '0'; /* least significant digit */
59 d1 = q + 9*d3 + 5*d2 + d1;
61 q = (d1 * 0xcd) >> 11;
63 *buf++ = d1 + '0'; /* next digit */
66 if ((d2 != 0) || (d3 != 0)) {
69 *buf++ = d2 + '0'; /* next digit */
73 q = (d3 * 0xcd) >> 11;
75 *buf++ = d3 + '0'; /* next digit */
77 *buf++ = q + '0'; /* most sign. digit */
83 /* Same with if's removed. Always emits five digits */
84 static char *put_dec_full(char *buf, unsigned q)
86 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
87 /* but anyway, gcc produces better code with full-sized ints */
88 unsigned d3, d2, d1, d0;
94 * Possible ways to approx. divide by 10
95 * gcc -O2 replaces multiply with shifts and adds
96 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
97 * (x * 0x67) >> 10: 1100111
98 * (x * 0x34) >> 9: 110100 - same
99 * (x * 0x1a) >> 8: 11010 - same
100 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
103 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
104 q = (d0 * 0xcd) >> 11;
107 d1 = q + 9*d3 + 5*d2 + d1;
108 q = (d1 * 0xcd) >> 11;
118 q = (d3 * 0xcd) >> 11; /* - shorter code */
119 /* q = (d3 * 0x67) >> 10; - would also work */
125 /* No inlining helps gcc to use registers better */
126 static noinline char *put_dec(char *buf, uint64_t num)
131 return put_dec_trunc(buf, num);
132 rem = do_div(num, 100000);
133 buf = put_dec_full(buf, rem);
137 #define ZEROPAD 1 /* pad with zero */
138 #define SIGN 2 /* unsigned/signed long */
139 #define PLUS 4 /* show plus */
140 #define SPACE 8 /* space if plus */
141 #define LEFT 16 /* left justified */
142 #define SMALL 32 /* Must be 32 == 0x20 */
143 #define SPECIAL 64 /* 0x */
146 * Macro to add a new character to our output string, but only if it will
147 * fit. The macro moves to the next character position in the output string.
149 #define ADDCH(str, ch) do { \
155 static char *number(char *buf, char *end, u64 num,
156 int base, int size, int precision, int type)
158 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
159 static const char digits[16] = "0123456789ABCDEF";
164 int need_pfx = ((type & SPECIAL) && base != 10);
167 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
168 * produces same digits or (maybe lowercased) letters */
169 locase = (type & SMALL);
178 } else if (type & PLUS) {
181 } else if (type & SPACE) {
192 /* generate full string in tmp[], in reverse order */
196 /* Generic code, for any base:
198 tmp[i++] = (digits[do_div(num,base)] | locase);
201 else if (base != 10) { /* 8 or 16 */
209 tmp[i++] = (digits[((unsigned char)num) & mask]
213 } else { /* base 10 */
214 i = put_dec(tmp, num) - tmp;
217 /* printing 100 using %2d gives "100", not "00" */
220 /* leading space padding */
222 if (!(type & (ZEROPAD + LEFT))) {
229 /* "0x" / "0" prefix */
233 ADDCH(buf, 'X' | locase);
235 /* zero or space padding */
236 if (!(type & LEFT)) {
237 char c = (type & ZEROPAD) ? '0' : ' ';
242 /* hmm even more zero padding? */
243 while (i <= --precision)
245 /* actual digits of result */
248 /* trailing space padding */
254 static char *string(char *buf, char *end, char *s, int field_width,
255 int precision, int flags)
262 len = strnlen(s, precision);
265 while (len < field_width--)
267 for (i = 0; i < len; ++i)
269 while (len < field_width--)
274 static char *string16(char *buf, char *end, u16 *s, int field_width,
275 int precision, int flags)
277 u16 *str = s ? s : L"<NULL>";
278 int utf16_len = utf16_strnlen(str, precision);
279 u8 utf8[utf16_len * MAX_UTF8_PER_UTF16];
282 utf8_len = utf16_to_utf8(utf8, str, utf16_len) - utf8;
285 while (utf8_len < field_width--)
287 for (i = 0; i < utf8_len; ++i)
289 while (utf8_len < field_width--)
294 #ifdef CONFIG_CMD_NET
295 static const char hex_asc[] = "0123456789abcdef";
296 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
297 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
299 static inline char *pack_hex_byte(char *buf, u8 byte)
301 *buf++ = hex_asc_hi(byte);
302 *buf++ = hex_asc_lo(byte);
306 static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
307 int precision, int flags)
309 /* (6 * 2 hex digits), 5 colons and trailing zero */
310 char mac_addr[6 * 3];
314 for (i = 0; i < 6; i++) {
315 p = pack_hex_byte(p, addr[i]);
316 if (!(flags & SPECIAL) && i != 5)
321 return string(buf, end, mac_addr, field_width, precision,
325 static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
326 int precision, int flags)
328 /* (8 * 4 hex digits), 7 colons and trailing zero */
329 char ip6_addr[8 * 5];
333 for (i = 0; i < 8; i++) {
334 p = pack_hex_byte(p, addr[2 * i]);
335 p = pack_hex_byte(p, addr[2 * i + 1]);
336 if (!(flags & SPECIAL) && i != 7)
341 return string(buf, end, ip6_addr, field_width, precision,
345 static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
346 int precision, int flags)
348 /* (4 * 3 decimal digits), 3 dots and trailing zero */
349 char ip4_addr[4 * 4];
350 char temp[3]; /* hold each IP quad in reverse order */
354 for (i = 0; i < 4; i++) {
355 digits = put_dec_trunc(temp, addr[i]) - temp;
356 /* reverse the digits in the quad */
364 return string(buf, end, ip4_addr, field_width, precision,
370 * Show a '%p' thing. A kernel extension is that the '%p' is followed
371 * by an extra set of alphanumeric characters that are extended format
374 * Right now we handle:
376 * - 'M' For a 6-byte MAC address, it prints the address in the
377 * usual colon-separated hex notation
378 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
379 * decimal for v4 and colon separated network-order 16 bit hex for v6)
380 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
383 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
384 * function pointers are really function descriptors, which contain a
385 * pointer to the real address.
387 static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
388 int field_width, int precision, int flags)
390 u64 num = (uintptr_t)ptr;
393 * Being a boot loader, we explicitly allow pointers to
394 * (physical) address null.
398 return string(buf, end, "(null)", field_width, precision,
402 #ifdef CONFIG_CMD_NET
405 flags |= SPECIAL | ZEROPAD;
410 field_width = sizeof(phys_addr_t) * 2 + 2;
411 num = *(phys_addr_t *)ptr;
419 return mac_address_string(buf, end, ptr, field_width,
426 return ip6_addr_string(buf, end, ptr, field_width,
429 return ip4_addr_string(buf, end, ptr, field_width,
436 if (field_width == -1) {
437 field_width = 2*sizeof(void *);
440 return number(buf, end, num, 16, field_width, precision, flags);
443 static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
450 int flags; /* flags to number() */
452 int field_width; /* width of output field */
453 int precision; /* min. # of digits for integers; max
454 number of chars for from string */
455 int qualifier; /* 'h', 'l', or 'L' for integer fields */
456 /* 'z' support added 23/7/1999 S.H. */
457 /* 'z' changed to 'Z' --davidm 1/25/99 */
458 /* 't' added for ptrdiff_t */
459 char *end = buf + size;
461 /* Make sure end is always >= buf - do we want this in U-Boot? */
468 for (; *fmt ; ++fmt) {
477 ++fmt; /* this also skips first '%' */
496 /* get field width */
499 field_width = skip_atoi(&fmt);
500 else if (*fmt == '*') {
502 /* it's the next argument */
503 field_width = va_arg(args, int);
504 if (field_width < 0) {
505 field_width = -field_width;
510 /* get the precision */
515 precision = skip_atoi(&fmt);
516 else if (*fmt == '*') {
518 /* it's the next argument */
519 precision = va_arg(args, int);
525 /* get the conversion qualifier */
527 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
528 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
531 if (qualifier == 'l' && *fmt == 'l') {
542 if (!(flags & LEFT)) {
543 while (--field_width > 0)
546 ADDCH(str, (unsigned char) va_arg(args, int));
547 while (--field_width > 0)
552 if (qualifier == 'l' && !IS_ENABLED(CONFIG_SPL_BUILD)) {
553 str = string16(str, end, va_arg(args, u16 *),
554 field_width, precision, flags);
556 str = string(str, end, va_arg(args, char *),
557 field_width, precision, flags);
562 str = pointer(fmt + 1, str, end,
563 va_arg(args, void *),
564 field_width, precision, flags);
565 /* Skip all alphanumeric pointer suffixes */
566 while (isalnum(fmt[1]))
571 if (qualifier == 'l') {
572 long *ip = va_arg(args, long *);
575 int *ip = va_arg(args, int *);
584 /* integer number formats - set up the flags and "break" */
609 if (qualifier == 'L') /* "quad" for 64 bit variables */
610 num = va_arg(args, unsigned long long);
611 else if (qualifier == 'l') {
612 num = va_arg(args, unsigned long);
614 num = (signed long) num;
615 } else if (qualifier == 'Z' || qualifier == 'z') {
616 num = va_arg(args, size_t);
617 } else if (qualifier == 't') {
618 num = va_arg(args, ptrdiff_t);
619 } else if (qualifier == 'h') {
620 num = (unsigned short) va_arg(args, int);
622 num = (signed short) num;
624 num = va_arg(args, unsigned int);
626 num = (signed int) num;
628 str = number(str, end, num, base, field_width, precision,
638 /* the trailing null byte doesn't count towards the total */
642 int vsnprintf(char *buf, size_t size, const char *fmt,
645 return vsnprintf_internal(buf, size, fmt, args);
648 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
652 i = vsnprintf(buf, size, fmt, args);
654 if (likely(i < size))
661 int snprintf(char *buf, size_t size, const char *fmt, ...)
667 i = vsnprintf(buf, size, fmt, args);
673 int scnprintf(char *buf, size_t size, const char *fmt, ...)
679 i = vscnprintf(buf, size, fmt, args);
686 * Format a string and place it in a buffer (va_list version)
688 * @param buf The buffer to place the result into
689 * @param fmt The format string to use
690 * @param args Arguments for the format string
692 * The function returns the number of characters written
693 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
696 * If you're not already dealing with a va_list consider using sprintf().
698 int vsprintf(char *buf, const char *fmt, va_list args)
700 return vsnprintf_internal(buf, INT_MAX, fmt, args);
703 int sprintf(char *buf, const char *fmt, ...)
709 i = vsprintf(buf, fmt, args);
714 int printf(const char *fmt, ...)
718 char printbuffer[CONFIG_SYS_PBSIZE];
723 * For this to work, printbuffer must be larger than
724 * anything we ever want to print.
726 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
729 /* Print the string */
734 int vprintf(const char *fmt, va_list args)
737 char printbuffer[CONFIG_SYS_PBSIZE];
740 * For this to work, printbuffer must be larger than
741 * anything we ever want to print.
743 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
745 /* Print the string */
751 void __assert_fail(const char *assertion, const char *file, unsigned line,
752 const char *function)
754 /* This will not return */
755 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
759 char *simple_itoa(ulong i)
761 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
762 static char local[22];
763 char *p = &local[21];
773 /* We don't seem to have %'d in U-Boot */
774 void print_grouped_ull(unsigned long long int_val, int digits)
779 digits = (digits + 2) / 3;
780 sprintf(str, "%*llu", digits * 3, int_val);
781 for (s = str; *s; s += grab) {
783 putc(s[-1] != ' ' ? ',' : ' ');
784 printf("%.*s", grab, s);
789 bool str2off(const char *p, loff_t *num)
793 *num = simple_strtoull(p, &endptr, 16);
794 return *p != '\0' && *endptr == '\0';
797 bool str2long(const char *p, ulong *num)
801 *num = simple_strtoul(p, &endptr, 16);
802 return *p != '\0' && *endptr == '\0';