4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
20 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/ctype.h>
24 #include <linux/kernel.h>
25 #include <linux/kallsyms.h>
26 #include <linux/uaccess.h>
27 #include <linux/ioport.h>
28 #include <net/addrconf.h>
30 #include <asm/page.h> /* for PAGE_SIZE */
31 #include <asm/div64.h>
32 #include <asm/sections.h> /* for dereference_function_descriptor() */
37 * simple_strtoull - convert a string to an unsigned long long
38 * @cp: The start of the string
39 * @endp: A pointer to the end of the parsed string will be placed here
40 * @base: The number base to use
42 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
44 unsigned long long result;
47 cp = _parse_integer_fixup_radix(cp, &base);
48 rv = _parse_integer(cp, base, &result);
50 cp += (rv & ~KSTRTOX_OVERFLOW);
57 EXPORT_SYMBOL(simple_strtoull);
60 * simple_strtoul - convert a string to an unsigned long
61 * @cp: The start of the string
62 * @endp: A pointer to the end of the parsed string will be placed here
63 * @base: The number base to use
65 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
67 return simple_strtoull(cp, endp, base);
69 EXPORT_SYMBOL(simple_strtoul);
72 * simple_strtol - convert a string to a signed long
73 * @cp: The start of the string
74 * @endp: A pointer to the end of the parsed string will be placed here
75 * @base: The number base to use
77 long simple_strtol(const char *cp, char **endp, unsigned int base)
80 return -simple_strtoul(cp + 1, endp, base);
82 return simple_strtoul(cp, endp, base);
84 EXPORT_SYMBOL(simple_strtol);
87 * simple_strtoll - convert a string to a signed long long
88 * @cp: The start of the string
89 * @endp: A pointer to the end of the parsed string will be placed here
90 * @base: The number base to use
92 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
95 return -simple_strtoull(cp + 1, endp, base);
97 return simple_strtoull(cp, endp, base);
99 EXPORT_SYMBOL(simple_strtoll);
101 static noinline_for_stack
102 int skip_atoi(const char **s)
107 i = i*10 + *((*s)++) - '0';
112 /* Decimal conversion is by far the most typical, and is used
113 * for /proc and /sys data. This directly impacts e.g. top performance
114 * with many processes running. We optimize it for speed
116 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
117 * (with permission from the author, Douglas W. Jones). */
119 /* Formats correctly any integer in [0,99999].
120 * Outputs from one to five digits depending on input.
121 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
122 static noinline_for_stack
123 char *put_dec_trunc(char *buf, unsigned q)
125 unsigned d3, d2, d1, d0;
130 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
131 q = (d0 * 0xcd) >> 11;
133 *buf++ = d0 + '0'; /* least significant digit */
134 d1 = q + 9*d3 + 5*d2 + d1;
136 q = (d1 * 0xcd) >> 11;
138 *buf++ = d1 + '0'; /* next digit */
141 if ((d2 != 0) || (d3 != 0)) {
144 *buf++ = d2 + '0'; /* next digit */
148 q = (d3 * 0xcd) >> 11;
150 *buf++ = d3 + '0'; /* next digit */
152 *buf++ = q + '0'; /* most sign. digit */
159 /* Same with if's removed. Always emits five digits */
160 static noinline_for_stack
161 char *put_dec_full(char *buf, unsigned q)
163 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
164 /* but anyway, gcc produces better code with full-sized ints */
165 unsigned d3, d2, d1, d0;
171 * Possible ways to approx. divide by 10
172 * gcc -O2 replaces multiply with shifts and adds
173 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
174 * (x * 0x67) >> 10: 1100111
175 * (x * 0x34) >> 9: 110100 - same
176 * (x * 0x1a) >> 8: 11010 - same
177 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
179 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
180 q = (d0 * 0xcd) >> 11;
183 d1 = q + 9*d3 + 5*d2 + d1;
184 q = (d1 * 0xcd) >> 11;
194 q = (d3 * 0xcd) >> 11; /* - shorter code */
195 /* q = (d3 * 0x67) >> 10; - would also work */
202 /* No inlining helps gcc to use registers better */
203 static noinline_for_stack
204 char *put_dec(char *buf, unsigned long long num)
209 return put_dec_trunc(buf, num);
210 rem = do_div(num, 100000);
211 buf = put_dec_full(buf, rem);
216 * Convert passed number to decimal string.
217 * Returns the length of string. On buffer overflow, returns 0.
219 * If speed is not important, use snprintf(). It's easy to read the code.
221 int num_to_str(char *buf, int size, unsigned long long num)
223 char tmp[21]; /* Enough for 2^64 in decimal */
226 len = put_dec(tmp, num) - tmp;
230 for (idx = 0; idx < len; ++idx)
231 buf[idx] = tmp[len - idx - 1];
235 #define ZEROPAD 1 /* pad with zero */
236 #define SIGN 2 /* unsigned/signed long */
237 #define PLUS 4 /* show plus */
238 #define SPACE 8 /* space if plus */
239 #define LEFT 16 /* left justified */
240 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
241 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
244 FORMAT_TYPE_NONE, /* Just a string part */
246 FORMAT_TYPE_PRECISION,
250 FORMAT_TYPE_PERCENT_CHAR,
252 FORMAT_TYPE_LONG_LONG,
267 u8 type; /* format_type enum */
268 u8 flags; /* flags to number() */
269 u8 base; /* number base, 8, 10 or 16 only */
270 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
271 s16 field_width; /* width of output field */
272 s16 precision; /* # of digits/chars */
275 static noinline_for_stack
276 char *number(char *buf, char *end, unsigned long long num,
277 struct printf_spec spec)
279 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
280 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
285 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
288 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
289 * produces same digits or (maybe lowercased) letters */
290 locase = (spec.flags & SMALL);
291 if (spec.flags & LEFT)
292 spec.flags &= ~ZEROPAD;
294 if (spec.flags & SIGN) {
295 if ((signed long long)num < 0) {
297 num = -(signed long long)num;
299 } else if (spec.flags & PLUS) {
302 } else if (spec.flags & SPACE) {
313 /* generate full string in tmp[], in reverse order */
317 /* Generic code, for any base:
319 tmp[i++] = (digits[do_div(num,base)] | locase);
322 else if (spec.base != 10) { /* 8 or 16 */
323 int mask = spec.base - 1;
329 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
332 } else { /* base 10 */
333 i = put_dec(tmp, num) - tmp;
336 /* printing 100 using %2d gives "100", not "00" */
337 if (i > spec.precision)
339 /* leading space padding */
340 spec.field_width -= spec.precision;
341 if (!(spec.flags & (ZEROPAD+LEFT))) {
342 while (--spec.field_width >= 0) {
354 /* "0x" / "0" prefix */
359 if (spec.base == 16) {
361 *buf = ('X' | locase);
365 /* zero or space padding */
366 if (!(spec.flags & LEFT)) {
367 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
368 while (--spec.field_width >= 0) {
374 /* hmm even more zero padding? */
375 while (i <= --spec.precision) {
380 /* actual digits of result */
386 /* trailing space padding */
387 while (--spec.field_width >= 0) {
396 static noinline_for_stack
397 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
401 if ((unsigned long)s < PAGE_SIZE)
404 len = strnlen(s, spec.precision);
406 if (!(spec.flags & LEFT)) {
407 while (len < spec.field_width--) {
413 for (i = 0; i < len; ++i) {
418 while (len < spec.field_width--) {
427 static noinline_for_stack
428 char *symbol_string(char *buf, char *end, void *ptr,
429 struct printf_spec spec, char ext)
431 unsigned long value = (unsigned long) ptr;
432 #ifdef CONFIG_KALLSYMS
433 char sym[KSYM_SYMBOL_LEN];
435 sprint_backtrace(sym, value);
436 else if (ext != 'f' && ext != 's')
437 sprint_symbol(sym, value);
439 kallsyms_lookup(value, NULL, NULL, NULL, sym);
441 return string(buf, end, sym, spec);
443 spec.field_width = 2 * sizeof(void *);
444 spec.flags |= SPECIAL | SMALL | ZEROPAD;
447 return number(buf, end, value, spec);
451 static noinline_for_stack
452 char *resource_string(char *buf, char *end, struct resource *res,
453 struct printf_spec spec, const char *fmt)
455 #ifndef IO_RSRC_PRINTK_SIZE
456 #define IO_RSRC_PRINTK_SIZE 6
459 #ifndef MEM_RSRC_PRINTK_SIZE
460 #define MEM_RSRC_PRINTK_SIZE 10
462 static const struct printf_spec io_spec = {
464 .field_width = IO_RSRC_PRINTK_SIZE,
466 .flags = SPECIAL | SMALL | ZEROPAD,
468 static const struct printf_spec mem_spec = {
470 .field_width = MEM_RSRC_PRINTK_SIZE,
472 .flags = SPECIAL | SMALL | ZEROPAD,
474 static const struct printf_spec bus_spec = {
478 .flags = SMALL | ZEROPAD,
480 static const struct printf_spec dec_spec = {
485 static const struct printf_spec str_spec = {
490 static const struct printf_spec flag_spec = {
493 .flags = SPECIAL | SMALL,
496 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
497 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
498 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
499 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
500 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
501 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
502 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
503 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
505 char *p = sym, *pend = sym + sizeof(sym);
506 int decode = (fmt[0] == 'R') ? 1 : 0;
507 const struct printf_spec *specp;
510 if (res->flags & IORESOURCE_IO) {
511 p = string(p, pend, "io ", str_spec);
513 } else if (res->flags & IORESOURCE_MEM) {
514 p = string(p, pend, "mem ", str_spec);
516 } else if (res->flags & IORESOURCE_IRQ) {
517 p = string(p, pend, "irq ", str_spec);
519 } else if (res->flags & IORESOURCE_DMA) {
520 p = string(p, pend, "dma ", str_spec);
522 } else if (res->flags & IORESOURCE_BUS) {
523 p = string(p, pend, "bus ", str_spec);
526 p = string(p, pend, "??? ", str_spec);
530 p = number(p, pend, res->start, *specp);
531 if (res->start != res->end) {
533 p = number(p, pend, res->end, *specp);
536 if (res->flags & IORESOURCE_MEM_64)
537 p = string(p, pend, " 64bit", str_spec);
538 if (res->flags & IORESOURCE_PREFETCH)
539 p = string(p, pend, " pref", str_spec);
540 if (res->flags & IORESOURCE_WINDOW)
541 p = string(p, pend, " window", str_spec);
542 if (res->flags & IORESOURCE_DISABLED)
543 p = string(p, pend, " disabled", str_spec);
545 p = string(p, pend, " flags ", str_spec);
546 p = number(p, pend, res->flags, flag_spec);
551 return string(buf, end, sym, spec);
554 static noinline_for_stack
555 char *mac_address_string(char *buf, char *end, u8 *addr,
556 struct printf_spec spec, const char *fmt)
558 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
563 if (fmt[1] == 'F') { /* FDDI canonical format */
569 for (i = 0; i < 6; i++) {
570 p = hex_byte_pack(p, addr[i]);
571 if (fmt[0] == 'M' && i != 5)
576 return string(buf, end, mac_addr, spec);
579 static noinline_for_stack
580 char *ip4_string(char *p, const u8 *addr, const char *fmt)
583 bool leading_zeros = (fmt[0] == 'i');
608 for (i = 0; i < 4; i++) {
609 char temp[3]; /* hold each IP quad in reverse order */
610 int digits = put_dec_trunc(temp, addr[index]) - temp;
617 /* reverse the digits in the quad */
629 static noinline_for_stack
630 char *ip6_compressed_string(char *p, const char *addr)
633 unsigned char zerolength[8];
638 bool needcolon = false;
642 memcpy(&in6, addr, sizeof(struct in6_addr));
644 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
646 memset(zerolength, 0, sizeof(zerolength));
653 /* find position of longest 0 run */
654 for (i = 0; i < range; i++) {
655 for (j = i; j < range; j++) {
656 if (in6.s6_addr16[j] != 0)
661 for (i = 0; i < range; i++) {
662 if (zerolength[i] > longest) {
663 longest = zerolength[i];
667 if (longest == 1) /* don't compress a single 0 */
671 for (i = 0; i < range; i++) {
673 if (needcolon || i == 0)
684 /* hex u16 without leading 0s */
685 word = ntohs(in6.s6_addr16[i]);
690 p = hex_byte_pack(p, hi);
692 *p++ = hex_asc_lo(hi);
693 p = hex_byte_pack(p, lo);
696 p = hex_byte_pack(p, lo);
698 *p++ = hex_asc_lo(lo);
705 p = ip4_string(p, &in6.s6_addr[12], "I4");
712 static noinline_for_stack
713 char *ip6_string(char *p, const char *addr, const char *fmt)
717 for (i = 0; i < 8; i++) {
718 p = hex_byte_pack(p, *addr++);
719 p = hex_byte_pack(p, *addr++);
720 if (fmt[0] == 'I' && i != 7)
728 static noinline_for_stack
729 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
730 struct printf_spec spec, const char *fmt)
732 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
734 if (fmt[0] == 'I' && fmt[2] == 'c')
735 ip6_compressed_string(ip6_addr, addr);
737 ip6_string(ip6_addr, addr, fmt);
739 return string(buf, end, ip6_addr, spec);
742 static noinline_for_stack
743 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
744 struct printf_spec spec, const char *fmt)
746 char ip4_addr[sizeof("255.255.255.255")];
748 ip4_string(ip4_addr, addr, fmt);
750 return string(buf, end, ip4_addr, spec);
753 static noinline_for_stack
754 char *uuid_string(char *buf, char *end, const u8 *addr,
755 struct printf_spec spec, const char *fmt)
757 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
760 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
761 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
762 const u8 *index = be;
767 uc = true; /* fall-through */
776 for (i = 0; i < 16; i++) {
777 p = hex_byte_pack(p, addr[index[i]]);
797 return string(buf, end, uuid, spec);
801 char *netdev_feature_string(char *buf, char *end, const u8 *addr,
802 struct printf_spec spec)
804 spec.flags |= SPECIAL | SMALL | ZEROPAD;
805 if (spec.field_width == -1)
806 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
809 return number(buf, end, *(const netdev_features_t *)addr, spec);
812 int kptr_restrict __read_mostly;
815 * Show a '%p' thing. A kernel extension is that the '%p' is followed
816 * by an extra set of alphanumeric characters that are extended format
819 * Right now we handle:
821 * - 'F' For symbolic function descriptor pointers with offset
822 * - 'f' For simple symbolic function names without offset
823 * - 'S' For symbolic direct pointers with offset
824 * - 's' For symbolic direct pointers without offset
825 * - 'B' For backtraced symbolic direct pointers with offset
826 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
827 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
828 * - 'M' For a 6-byte MAC address, it prints the address in the
829 * usual colon-separated hex notation
830 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
831 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
832 * with a dash-separated hex notation
833 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
834 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
835 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
836 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
837 * IPv6 omits the colons (01020304...0f)
838 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
839 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
840 * - 'I6c' for IPv6 addresses printed as specified by
841 * http://tools.ietf.org/html/rfc5952
842 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
843 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
844 * Options for %pU are:
845 * b big endian lower case hex (default)
846 * B big endian UPPER case hex
847 * l little endian lower case hex
848 * L little endian UPPER case hex
849 * big endian output byte order is:
850 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
851 * little endian output byte order is:
852 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
853 * - 'V' For a struct va_format which contains a format string * and va_list *,
854 * call vsnprintf(->format, *->va_list).
855 * Implements a "recursive vsnprintf".
856 * Do not use this feature without some mechanism to verify the
857 * correctness of the format string and va_list arguments.
858 * - 'K' For a kernel pointer that should be hidden from unprivileged users
859 * - 'NF' For a netdev_features_t
861 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
862 * function pointers are really function descriptors, which contain a
863 * pointer to the real address.
865 static noinline_for_stack
866 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
867 struct printf_spec spec)
869 if (!ptr && *fmt != 'K') {
871 * Print (null) with the same width as a pointer so it makes
872 * tabular output look nice.
874 if (spec.field_width == -1)
875 spec.field_width = 2 * sizeof(void *);
876 return string(buf, end, "(null)", spec);
882 ptr = dereference_function_descriptor(ptr);
887 return symbol_string(buf, end, ptr, spec, *fmt);
890 return resource_string(buf, end, ptr, spec, fmt);
891 case 'M': /* Colon separated: 00:01:02:03:04:05 */
892 case 'm': /* Contiguous: 000102030405 */
893 /* [mM]F (FDDI, bit reversed) */
894 return mac_address_string(buf, end, ptr, spec, fmt);
895 case 'I': /* Formatted IP supported
897 * 6: 0001:0203:...:0708
898 * 6c: 1::708 or 1::1.2.3.4
900 case 'i': /* Contiguous:
906 return ip6_addr_string(buf, end, ptr, spec, fmt);
908 return ip4_addr_string(buf, end, ptr, spec, fmt);
912 return uuid_string(buf, end, ptr, spec, fmt);
917 va_copy(va, *((struct va_format *)ptr)->va);
918 buf += vsnprintf(buf, end > buf ? end - buf : 0,
919 ((struct va_format *)ptr)->fmt, va);
925 * %pK cannot be used in IRQ context because its test
926 * for CAP_SYSLOG would be meaningless.
928 if (in_irq() || in_serving_softirq() || in_nmi()) {
929 if (spec.field_width == -1)
930 spec.field_width = 2 * sizeof(void *);
931 return string(buf, end, "pK-error", spec);
933 if (!((kptr_restrict == 0) ||
934 (kptr_restrict == 1 &&
935 has_capability_noaudit(current, CAP_SYSLOG))))
941 return netdev_feature_string(buf, end, ptr, spec);
946 if (spec.field_width == -1) {
947 spec.field_width = 2 * sizeof(void *);
948 spec.flags |= ZEROPAD;
952 return number(buf, end, (unsigned long) ptr, spec);
956 * Helper function to decode printf style format.
957 * Each call decode a token from the format and return the
958 * number of characters read (or likely the delta where it wants
959 * to go on the next call).
960 * The decoded token is returned through the parameters
962 * 'h', 'l', or 'L' for integer fields
963 * 'z' support added 23/7/1999 S.H.
964 * 'z' changed to 'Z' --davidm 1/25/99
965 * 't' added for ptrdiff_t
967 * @fmt: the format string
968 * @type of the token returned
969 * @flags: various flags such as +, -, # tokens..
970 * @field_width: overwritten width
971 * @base: base of the number (octal, hex, ...)
972 * @precision: precision of a number
973 * @qualifier: qualifier of a number (long, size_t, ...)
975 static noinline_for_stack
976 int format_decode(const char *fmt, struct printf_spec *spec)
978 const char *start = fmt;
980 /* we finished early by reading the field width */
981 if (spec->type == FORMAT_TYPE_WIDTH) {
982 if (spec->field_width < 0) {
983 spec->field_width = -spec->field_width;
986 spec->type = FORMAT_TYPE_NONE;
990 /* we finished early by reading the precision */
991 if (spec->type == FORMAT_TYPE_PRECISION) {
992 if (spec->precision < 0)
995 spec->type = FORMAT_TYPE_NONE;
1000 spec->type = FORMAT_TYPE_NONE;
1002 for (; *fmt ; ++fmt) {
1007 /* Return the current non-format string */
1008 if (fmt != start || !*fmt)
1014 while (1) { /* this also skips first '%' */
1020 case '-': spec->flags |= LEFT; break;
1021 case '+': spec->flags |= PLUS; break;
1022 case ' ': spec->flags |= SPACE; break;
1023 case '#': spec->flags |= SPECIAL; break;
1024 case '0': spec->flags |= ZEROPAD; break;
1025 default: found = false;
1032 /* get field width */
1033 spec->field_width = -1;
1036 spec->field_width = skip_atoi(&fmt);
1037 else if (*fmt == '*') {
1038 /* it's the next argument */
1039 spec->type = FORMAT_TYPE_WIDTH;
1040 return ++fmt - start;
1044 /* get the precision */
1045 spec->precision = -1;
1048 if (isdigit(*fmt)) {
1049 spec->precision = skip_atoi(&fmt);
1050 if (spec->precision < 0)
1051 spec->precision = 0;
1052 } else if (*fmt == '*') {
1053 /* it's the next argument */
1054 spec->type = FORMAT_TYPE_PRECISION;
1055 return ++fmt - start;
1060 /* get the conversion qualifier */
1061 spec->qualifier = -1;
1062 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1063 _tolower(*fmt) == 'z' || *fmt == 't') {
1064 spec->qualifier = *fmt++;
1065 if (unlikely(spec->qualifier == *fmt)) {
1066 if (spec->qualifier == 'l') {
1067 spec->qualifier = 'L';
1069 } else if (spec->qualifier == 'h') {
1070 spec->qualifier = 'H';
1080 spec->type = FORMAT_TYPE_CHAR;
1081 return ++fmt - start;
1084 spec->type = FORMAT_TYPE_STR;
1085 return ++fmt - start;
1088 spec->type = FORMAT_TYPE_PTR;
1093 spec->type = FORMAT_TYPE_NRCHARS;
1094 return ++fmt - start;
1097 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1098 return ++fmt - start;
1100 /* integer number formats - set up the flags and "break" */
1106 spec->flags |= SMALL;
1114 spec->flags |= SIGN;
1119 spec->type = FORMAT_TYPE_INVALID;
1123 if (spec->qualifier == 'L')
1124 spec->type = FORMAT_TYPE_LONG_LONG;
1125 else if (spec->qualifier == 'l') {
1126 if (spec->flags & SIGN)
1127 spec->type = FORMAT_TYPE_LONG;
1129 spec->type = FORMAT_TYPE_ULONG;
1130 } else if (_tolower(spec->qualifier) == 'z') {
1131 spec->type = FORMAT_TYPE_SIZE_T;
1132 } else if (spec->qualifier == 't') {
1133 spec->type = FORMAT_TYPE_PTRDIFF;
1134 } else if (spec->qualifier == 'H') {
1135 if (spec->flags & SIGN)
1136 spec->type = FORMAT_TYPE_BYTE;
1138 spec->type = FORMAT_TYPE_UBYTE;
1139 } else if (spec->qualifier == 'h') {
1140 if (spec->flags & SIGN)
1141 spec->type = FORMAT_TYPE_SHORT;
1143 spec->type = FORMAT_TYPE_USHORT;
1145 if (spec->flags & SIGN)
1146 spec->type = FORMAT_TYPE_INT;
1148 spec->type = FORMAT_TYPE_UINT;
1151 return ++fmt - start;
1155 * vsnprintf - Format a string and place it in a buffer
1156 * @buf: The buffer to place the result into
1157 * @size: The size of the buffer, including the trailing null space
1158 * @fmt: The format string to use
1159 * @args: Arguments for the format string
1161 * This function follows C99 vsnprintf, but has some extensions:
1162 * %pS output the name of a text symbol with offset
1163 * %ps output the name of a text symbol without offset
1164 * %pF output the name of a function pointer with its offset
1165 * %pf output the name of a function pointer without its offset
1166 * %pB output the name of a backtrace symbol with its offset
1167 * %pR output the address range in a struct resource with decoded flags
1168 * %pr output the address range in a struct resource with raw flags
1169 * %pM output a 6-byte MAC address with colons
1170 * %pm output a 6-byte MAC address without colons
1171 * %pI4 print an IPv4 address without leading zeros
1172 * %pi4 print an IPv4 address with leading zeros
1173 * %pI6 print an IPv6 address with colons
1174 * %pi6 print an IPv6 address without colons
1175 * %pI6c print an IPv6 address as specified by RFC 5952
1176 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1180 * The return value is the number of characters which would
1181 * be generated for the given input, excluding the trailing
1182 * '\0', as per ISO C99. If you want to have the exact
1183 * number of characters written into @buf as return value
1184 * (not including the trailing '\0'), use vscnprintf(). If the
1185 * return is greater than or equal to @size, the resulting
1186 * string is truncated.
1188 * If you're not already dealing with a va_list consider using snprintf().
1190 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1192 unsigned long long num;
1194 struct printf_spec spec = {0};
1196 /* Reject out-of-range values early. Large positive sizes are
1197 used for unknown buffer sizes. */
1198 if (WARN_ON_ONCE((int) size < 0))
1204 /* Make sure end is always >= buf */
1211 const char *old_fmt = fmt;
1212 int read = format_decode(fmt, &spec);
1216 switch (spec.type) {
1217 case FORMAT_TYPE_NONE: {
1220 if (copy > end - str)
1222 memcpy(str, old_fmt, copy);
1228 case FORMAT_TYPE_WIDTH:
1229 spec.field_width = va_arg(args, int);
1232 case FORMAT_TYPE_PRECISION:
1233 spec.precision = va_arg(args, int);
1236 case FORMAT_TYPE_CHAR: {
1239 if (!(spec.flags & LEFT)) {
1240 while (--spec.field_width > 0) {
1247 c = (unsigned char) va_arg(args, int);
1251 while (--spec.field_width > 0) {
1259 case FORMAT_TYPE_STR:
1260 str = string(str, end, va_arg(args, char *), spec);
1263 case FORMAT_TYPE_PTR:
1264 str = pointer(fmt+1, str, end, va_arg(args, void *),
1266 while (isalnum(*fmt))
1270 case FORMAT_TYPE_PERCENT_CHAR:
1276 case FORMAT_TYPE_INVALID:
1282 case FORMAT_TYPE_NRCHARS: {
1283 u8 qualifier = spec.qualifier;
1285 if (qualifier == 'l') {
1286 long *ip = va_arg(args, long *);
1288 } else if (_tolower(qualifier) == 'z') {
1289 size_t *ip = va_arg(args, size_t *);
1292 int *ip = va_arg(args, int *);
1299 switch (spec.type) {
1300 case FORMAT_TYPE_LONG_LONG:
1301 num = va_arg(args, long long);
1303 case FORMAT_TYPE_ULONG:
1304 num = va_arg(args, unsigned long);
1306 case FORMAT_TYPE_LONG:
1307 num = va_arg(args, long);
1309 case FORMAT_TYPE_SIZE_T:
1310 num = va_arg(args, size_t);
1312 case FORMAT_TYPE_PTRDIFF:
1313 num = va_arg(args, ptrdiff_t);
1315 case FORMAT_TYPE_UBYTE:
1316 num = (unsigned char) va_arg(args, int);
1318 case FORMAT_TYPE_BYTE:
1319 num = (signed char) va_arg(args, int);
1321 case FORMAT_TYPE_USHORT:
1322 num = (unsigned short) va_arg(args, int);
1324 case FORMAT_TYPE_SHORT:
1325 num = (short) va_arg(args, int);
1327 case FORMAT_TYPE_INT:
1328 num = (int) va_arg(args, int);
1331 num = va_arg(args, unsigned int);
1334 str = number(str, end, num, spec);
1345 /* the trailing null byte doesn't count towards the total */
1349 EXPORT_SYMBOL(vsnprintf);
1352 * vscnprintf - Format a string and place it in a buffer
1353 * @buf: The buffer to place the result into
1354 * @size: The size of the buffer, including the trailing null space
1355 * @fmt: The format string to use
1356 * @args: Arguments for the format string
1358 * The return value is the number of characters which have been written into
1359 * the @buf not including the trailing '\0'. If @size is == 0 the function
1362 * If you're not already dealing with a va_list consider using scnprintf().
1364 * See the vsnprintf() documentation for format string extensions over C99.
1366 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1370 i = vsnprintf(buf, size, fmt, args);
1372 if (likely(i < size))
1378 EXPORT_SYMBOL(vscnprintf);
1381 * snprintf - Format a string and place it in a buffer
1382 * @buf: The buffer to place the result into
1383 * @size: The size of the buffer, including the trailing null space
1384 * @fmt: The format string to use
1385 * @...: Arguments for the format string
1387 * The return value is the number of characters which would be
1388 * generated for the given input, excluding the trailing null,
1389 * as per ISO C99. If the return is greater than or equal to
1390 * @size, the resulting string is truncated.
1392 * See the vsnprintf() documentation for format string extensions over C99.
1394 int snprintf(char *buf, size_t size, const char *fmt, ...)
1399 va_start(args, fmt);
1400 i = vsnprintf(buf, size, fmt, args);
1405 EXPORT_SYMBOL(snprintf);
1408 * scnprintf - Format a string and place it in a buffer
1409 * @buf: The buffer to place the result into
1410 * @size: The size of the buffer, including the trailing null space
1411 * @fmt: The format string to use
1412 * @...: Arguments for the format string
1414 * The return value is the number of characters written into @buf not including
1415 * the trailing '\0'. If @size is == 0 the function returns 0.
1418 int scnprintf(char *buf, size_t size, const char *fmt, ...)
1423 va_start(args, fmt);
1424 i = vscnprintf(buf, size, fmt, args);
1429 EXPORT_SYMBOL(scnprintf);
1432 * vsprintf - Format a string and place it in a buffer
1433 * @buf: The buffer to place the result into
1434 * @fmt: The format string to use
1435 * @args: Arguments for the format string
1437 * The function returns the number of characters written
1438 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1441 * If you're not already dealing with a va_list consider using sprintf().
1443 * See the vsnprintf() documentation for format string extensions over C99.
1445 int vsprintf(char *buf, const char *fmt, va_list args)
1447 return vsnprintf(buf, INT_MAX, fmt, args);
1449 EXPORT_SYMBOL(vsprintf);
1452 * sprintf - Format a string and place it in a buffer
1453 * @buf: The buffer to place the result into
1454 * @fmt: The format string to use
1455 * @...: Arguments for the format string
1457 * The function returns the number of characters written
1458 * into @buf. Use snprintf() or scnprintf() in order to avoid
1461 * See the vsnprintf() documentation for format string extensions over C99.
1463 int sprintf(char *buf, const char *fmt, ...)
1468 va_start(args, fmt);
1469 i = vsnprintf(buf, INT_MAX, fmt, args);
1474 EXPORT_SYMBOL(sprintf);
1476 #ifdef CONFIG_BINARY_PRINTF
1479 * vbin_printf() - VA arguments to binary data
1480 * bstr_printf() - Binary data to text string
1484 * vbin_printf - Parse a format string and place args' binary value in a buffer
1485 * @bin_buf: The buffer to place args' binary value
1486 * @size: The size of the buffer(by words(32bits), not characters)
1487 * @fmt: The format string to use
1488 * @args: Arguments for the format string
1490 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1493 * The return value is the number of words(32bits) which would be generated for
1497 * If the return value is greater than @size, the resulting bin_buf is NOT
1498 * valid for bstr_printf().
1500 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1502 struct printf_spec spec = {0};
1505 str = (char *)bin_buf;
1506 end = (char *)(bin_buf + size);
1508 #define save_arg(type) \
1510 if (sizeof(type) == 8) { \
1511 unsigned long long value; \
1512 str = PTR_ALIGN(str, sizeof(u32)); \
1513 value = va_arg(args, unsigned long long); \
1514 if (str + sizeof(type) <= end) { \
1515 *(u32 *)str = *(u32 *)&value; \
1516 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1519 unsigned long value; \
1520 str = PTR_ALIGN(str, sizeof(type)); \
1521 value = va_arg(args, int); \
1522 if (str + sizeof(type) <= end) \
1523 *(typeof(type) *)str = (type)value; \
1525 str += sizeof(type); \
1529 int read = format_decode(fmt, &spec);
1533 switch (spec.type) {
1534 case FORMAT_TYPE_NONE:
1535 case FORMAT_TYPE_INVALID:
1536 case FORMAT_TYPE_PERCENT_CHAR:
1539 case FORMAT_TYPE_WIDTH:
1540 case FORMAT_TYPE_PRECISION:
1544 case FORMAT_TYPE_CHAR:
1548 case FORMAT_TYPE_STR: {
1549 const char *save_str = va_arg(args, char *);
1552 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1553 || (unsigned long)save_str < PAGE_SIZE)
1554 save_str = "(null)";
1555 len = strlen(save_str) + 1;
1556 if (str + len < end)
1557 memcpy(str, save_str, len);
1562 case FORMAT_TYPE_PTR:
1564 /* skip all alphanumeric pointer suffixes */
1565 while (isalnum(*fmt))
1569 case FORMAT_TYPE_NRCHARS: {
1570 /* skip %n 's argument */
1571 u8 qualifier = spec.qualifier;
1573 if (qualifier == 'l')
1574 skip_arg = va_arg(args, long *);
1575 else if (_tolower(qualifier) == 'z')
1576 skip_arg = va_arg(args, size_t *);
1578 skip_arg = va_arg(args, int *);
1583 switch (spec.type) {
1585 case FORMAT_TYPE_LONG_LONG:
1586 save_arg(long long);
1588 case FORMAT_TYPE_ULONG:
1589 case FORMAT_TYPE_LONG:
1590 save_arg(unsigned long);
1592 case FORMAT_TYPE_SIZE_T:
1595 case FORMAT_TYPE_PTRDIFF:
1596 save_arg(ptrdiff_t);
1598 case FORMAT_TYPE_UBYTE:
1599 case FORMAT_TYPE_BYTE:
1602 case FORMAT_TYPE_USHORT:
1603 case FORMAT_TYPE_SHORT:
1612 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1615 EXPORT_SYMBOL_GPL(vbin_printf);
1618 * bstr_printf - Format a string from binary arguments and place it in a buffer
1619 * @buf: The buffer to place the result into
1620 * @size: The size of the buffer, including the trailing null space
1621 * @fmt: The format string to use
1622 * @bin_buf: Binary arguments for the format string
1624 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1625 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1626 * a binary buffer that generated by vbin_printf.
1628 * The format follows C99 vsnprintf, but has some extensions:
1629 * see vsnprintf comment for details.
1631 * The return value is the number of characters which would
1632 * be generated for the given input, excluding the trailing
1633 * '\0', as per ISO C99. If you want to have the exact
1634 * number of characters written into @buf as return value
1635 * (not including the trailing '\0'), use vscnprintf(). If the
1636 * return is greater than or equal to @size, the resulting
1637 * string is truncated.
1639 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1641 struct printf_spec spec = {0};
1643 const char *args = (const char *)bin_buf;
1645 if (WARN_ON_ONCE((int) size < 0))
1651 #define get_arg(type) \
1653 typeof(type) value; \
1654 if (sizeof(type) == 8) { \
1655 args = PTR_ALIGN(args, sizeof(u32)); \
1656 *(u32 *)&value = *(u32 *)args; \
1657 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1659 args = PTR_ALIGN(args, sizeof(type)); \
1660 value = *(typeof(type) *)args; \
1662 args += sizeof(type); \
1666 /* Make sure end is always >= buf */
1673 const char *old_fmt = fmt;
1674 int read = format_decode(fmt, &spec);
1678 switch (spec.type) {
1679 case FORMAT_TYPE_NONE: {
1682 if (copy > end - str)
1684 memcpy(str, old_fmt, copy);
1690 case FORMAT_TYPE_WIDTH:
1691 spec.field_width = get_arg(int);
1694 case FORMAT_TYPE_PRECISION:
1695 spec.precision = get_arg(int);
1698 case FORMAT_TYPE_CHAR: {
1701 if (!(spec.flags & LEFT)) {
1702 while (--spec.field_width > 0) {
1708 c = (unsigned char) get_arg(char);
1712 while (--spec.field_width > 0) {
1720 case FORMAT_TYPE_STR: {
1721 const char *str_arg = args;
1722 args += strlen(str_arg) + 1;
1723 str = string(str, end, (char *)str_arg, spec);
1727 case FORMAT_TYPE_PTR:
1728 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1729 while (isalnum(*fmt))
1733 case FORMAT_TYPE_PERCENT_CHAR:
1734 case FORMAT_TYPE_INVALID:
1740 case FORMAT_TYPE_NRCHARS:
1745 unsigned long long num;
1747 switch (spec.type) {
1749 case FORMAT_TYPE_LONG_LONG:
1750 num = get_arg(long long);
1752 case FORMAT_TYPE_ULONG:
1753 case FORMAT_TYPE_LONG:
1754 num = get_arg(unsigned long);
1756 case FORMAT_TYPE_SIZE_T:
1757 num = get_arg(size_t);
1759 case FORMAT_TYPE_PTRDIFF:
1760 num = get_arg(ptrdiff_t);
1762 case FORMAT_TYPE_UBYTE:
1763 num = get_arg(unsigned char);
1765 case FORMAT_TYPE_BYTE:
1766 num = get_arg(signed char);
1768 case FORMAT_TYPE_USHORT:
1769 num = get_arg(unsigned short);
1771 case FORMAT_TYPE_SHORT:
1772 num = get_arg(short);
1774 case FORMAT_TYPE_UINT:
1775 num = get_arg(unsigned int);
1781 str = number(str, end, num, spec);
1783 } /* switch(spec.type) */
1795 /* the trailing null byte doesn't count towards the total */
1798 EXPORT_SYMBOL_GPL(bstr_printf);
1801 * bprintf - Parse a format string and place args' binary value in a buffer
1802 * @bin_buf: The buffer to place args' binary value
1803 * @size: The size of the buffer(by words(32bits), not characters)
1804 * @fmt: The format string to use
1805 * @...: Arguments for the format string
1807 * The function returns the number of words(u32) written
1810 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1815 va_start(args, fmt);
1816 ret = vbin_printf(bin_buf, size, fmt, args);
1821 EXPORT_SYMBOL_GPL(bprintf);
1823 #endif /* CONFIG_BINARY_PRINTF */
1826 * vsscanf - Unformat a buffer into a list of arguments
1827 * @buf: input buffer
1828 * @fmt: format of buffer
1831 int vsscanf(const char *buf, const char *fmt, va_list args)
1833 const char *str = buf;
1842 while (*fmt && *str) {
1843 /* skip any white space in format */
1844 /* white space in format matchs any amount of
1845 * white space, including none, in the input.
1847 if (isspace(*fmt)) {
1848 fmt = skip_spaces(++fmt);
1849 str = skip_spaces(str);
1852 /* anything that is not a conversion must match exactly */
1853 if (*fmt != '%' && *fmt) {
1854 if (*fmt++ != *str++)
1863 /* skip this conversion.
1864 * advance both strings to next white space
1867 while (!isspace(*fmt) && *fmt != '%' && *fmt)
1869 while (!isspace(*str) && *str)
1874 /* get field width */
1877 field_width = skip_atoi(&fmt);
1879 /* get conversion qualifier */
1881 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1882 _tolower(*fmt) == 'z') {
1884 if (unlikely(qualifier == *fmt)) {
1885 if (qualifier == 'h') {
1888 } else if (qualifier == 'l') {
1904 char *s = (char *)va_arg(args, char*);
1905 if (field_width == -1)
1909 } while (--field_width > 0 && *str);
1915 char *s = (char *)va_arg(args, char *);
1916 if (field_width == -1)
1917 field_width = SHRT_MAX;
1918 /* first, skip leading white space in buffer */
1919 str = skip_spaces(str);
1921 /* now copy until next white space */
1922 while (*str && !isspace(*str) && field_width--)
1929 /* return number of characters read so far */
1931 int *i = (int *)va_arg(args, int*);
1949 /* looking for '%' in str */
1954 /* invalid format; stop here */
1958 /* have some sort of integer conversion.
1959 * first, skip white space in buffer.
1961 str = skip_spaces(str);
1964 if (is_sign && digit == '-')
1968 || (base == 16 && !isxdigit(digit))
1969 || (base == 10 && !isdigit(digit))
1970 || (base == 8 && (!isdigit(digit) || digit > '7'))
1971 || (base == 0 && !isdigit(digit)))
1974 switch (qualifier) {
1975 case 'H': /* that's 'hh' in format */
1977 signed char *s = (signed char *)va_arg(args, signed char *);
1978 *s = (signed char)simple_strtol(str, &next, base);
1980 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
1981 *s = (unsigned char)simple_strtoul(str, &next, base);
1986 short *s = (short *)va_arg(args, short *);
1987 *s = (short)simple_strtol(str, &next, base);
1989 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
1990 *s = (unsigned short)simple_strtoul(str, &next, base);
1995 long *l = (long *)va_arg(args, long *);
1996 *l = simple_strtol(str, &next, base);
1998 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
1999 *l = simple_strtoul(str, &next, base);
2004 long long *l = (long long *)va_arg(args, long long *);
2005 *l = simple_strtoll(str, &next, base);
2007 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2008 *l = simple_strtoull(str, &next, base);
2014 size_t *s = (size_t *)va_arg(args, size_t *);
2015 *s = (size_t)simple_strtoul(str, &next, base);
2020 int *i = (int *)va_arg(args, int *);
2021 *i = (int)simple_strtol(str, &next, base);
2023 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2024 *i = (unsigned int)simple_strtoul(str, &next, base);
2036 * Now we've come all the way through so either the input string or the
2037 * format ended. In the former case, there can be a %n at the current
2038 * position in the format that needs to be filled.
2040 if (*fmt == '%' && *(fmt + 1) == 'n') {
2041 int *p = (int *)va_arg(args, int *);
2047 EXPORT_SYMBOL(vsscanf);
2050 * sscanf - Unformat a buffer into a list of arguments
2051 * @buf: input buffer
2052 * @fmt: formatting of buffer
2053 * @...: resulting arguments
2055 int sscanf(const char *buf, const char *fmt, ...)
2060 va_start(args, fmt);
2061 i = vsscanf(buf, fmt, args);
2066 EXPORT_SYMBOL(sscanf);