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>
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 <linux/cred.h>
29 #include <net/addrconf.h>
31 #include <asm/page.h> /* for PAGE_SIZE */
32 #include <asm/div64.h>
33 #include <asm/sections.h> /* for dereference_function_descriptor() */
38 * simple_strtoull - convert a string to an unsigned long long
39 * @cp: The start of the string
40 * @endp: A pointer to the end of the parsed string will be placed here
41 * @base: The number base to use
43 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
45 unsigned long long result;
48 cp = _parse_integer_fixup_radix(cp, &base);
49 rv = _parse_integer(cp, base, &result);
51 cp += (rv & ~KSTRTOX_OVERFLOW);
58 EXPORT_SYMBOL(simple_strtoull);
61 * simple_strtoul - convert a string to an unsigned long
62 * @cp: The start of the string
63 * @endp: A pointer to the end of the parsed string will be placed here
64 * @base: The number base to use
66 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
68 return simple_strtoull(cp, endp, base);
70 EXPORT_SYMBOL(simple_strtoul);
73 * simple_strtol - convert a string to a signed long
74 * @cp: The start of the string
75 * @endp: A pointer to the end of the parsed string will be placed here
76 * @base: The number base to use
78 long simple_strtol(const char *cp, char **endp, unsigned int base)
81 return -simple_strtoul(cp + 1, endp, base);
83 return simple_strtoul(cp, endp, base);
85 EXPORT_SYMBOL(simple_strtol);
88 * simple_strtoll - convert a string to a signed long long
89 * @cp: The start of the string
90 * @endp: A pointer to the end of the parsed string will be placed here
91 * @base: The number base to use
93 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
96 return -simple_strtoull(cp + 1, endp, base);
98 return simple_strtoull(cp, endp, base);
100 EXPORT_SYMBOL(simple_strtoll);
102 static noinline_for_stack
103 int skip_atoi(const char **s)
108 i = i*10 + *((*s)++) - '0';
113 /* Decimal conversion is by far the most typical, and is used
114 * for /proc and /sys data. This directly impacts e.g. top performance
115 * with many processes running. We optimize it for speed
117 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
118 * (with permission from the author, Douglas W. Jones). */
120 /* Formats correctly any integer in [0,99999].
121 * Outputs from one to five digits depending on input.
122 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
123 static noinline_for_stack
124 char *put_dec_trunc(char *buf, unsigned q)
126 unsigned d3, d2, d1, d0;
131 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
132 q = (d0 * 0xcd) >> 11;
134 *buf++ = d0 + '0'; /* least significant digit */
135 d1 = q + 9*d3 + 5*d2 + d1;
137 q = (d1 * 0xcd) >> 11;
139 *buf++ = d1 + '0'; /* next digit */
142 if ((d2 != 0) || (d3 != 0)) {
145 *buf++ = d2 + '0'; /* next digit */
149 q = (d3 * 0xcd) >> 11;
151 *buf++ = d3 + '0'; /* next digit */
153 *buf++ = q + '0'; /* most sign. digit */
160 /* Same with if's removed. Always emits five digits */
161 static noinline_for_stack
162 char *put_dec_full(char *buf, unsigned q)
164 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
165 /* but anyway, gcc produces better code with full-sized ints */
166 unsigned d3, d2, d1, d0;
172 * Possible ways to approx. divide by 10
173 * gcc -O2 replaces multiply with shifts and adds
174 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
175 * (x * 0x67) >> 10: 1100111
176 * (x * 0x34) >> 9: 110100 - same
177 * (x * 0x1a) >> 8: 11010 - same
178 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
180 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
181 q = (d0 * 0xcd) >> 11;
184 d1 = q + 9*d3 + 5*d2 + d1;
185 q = (d1 * 0xcd) >> 11;
195 q = (d3 * 0xcd) >> 11; /* - shorter code */
196 /* q = (d3 * 0x67) >> 10; - would also work */
203 /* No inlining helps gcc to use registers better */
204 static noinline_for_stack
205 char *put_dec(char *buf, unsigned long long num)
210 return put_dec_trunc(buf, num);
211 rem = do_div(num, 100000);
212 buf = put_dec_full(buf, rem);
216 #define ZEROPAD 1 /* pad with zero */
217 #define SIGN 2 /* unsigned/signed long */
218 #define PLUS 4 /* show plus */
219 #define SPACE 8 /* space if plus */
220 #define LEFT 16 /* left justified */
221 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
222 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
225 FORMAT_TYPE_NONE, /* Just a string part */
227 FORMAT_TYPE_PRECISION,
231 FORMAT_TYPE_PERCENT_CHAR,
233 FORMAT_TYPE_LONG_LONG,
248 u8 type; /* format_type enum */
249 u8 flags; /* flags to number() */
250 u8 base; /* number base, 8, 10 or 16 only */
251 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
252 s16 field_width; /* width of output field */
253 s16 precision; /* # of digits/chars */
256 static noinline_for_stack
257 char *number(char *buf, char *end, unsigned long long num,
258 struct printf_spec spec)
260 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
261 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
266 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
269 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
270 * produces same digits or (maybe lowercased) letters */
271 locase = (spec.flags & SMALL);
272 if (spec.flags & LEFT)
273 spec.flags &= ~ZEROPAD;
275 if (spec.flags & SIGN) {
276 if ((signed long long)num < 0) {
278 num = -(signed long long)num;
280 } else if (spec.flags & PLUS) {
283 } else if (spec.flags & SPACE) {
294 /* generate full string in tmp[], in reverse order */
298 /* Generic code, for any base:
300 tmp[i++] = (digits[do_div(num,base)] | locase);
303 else if (spec.base != 10) { /* 8 or 16 */
304 int mask = spec.base - 1;
310 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
313 } else { /* base 10 */
314 i = put_dec(tmp, num) - tmp;
317 /* printing 100 using %2d gives "100", not "00" */
318 if (i > spec.precision)
320 /* leading space padding */
321 spec.field_width -= spec.precision;
322 if (!(spec.flags & (ZEROPAD+LEFT))) {
323 while (--spec.field_width >= 0) {
335 /* "0x" / "0" prefix */
340 if (spec.base == 16) {
342 *buf = ('X' | locase);
346 /* zero or space padding */
347 if (!(spec.flags & LEFT)) {
348 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
349 while (--spec.field_width >= 0) {
355 /* hmm even more zero padding? */
356 while (i <= --spec.precision) {
361 /* actual digits of result */
367 /* trailing space padding */
368 while (--spec.field_width >= 0) {
377 static noinline_for_stack
378 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
382 if ((unsigned long)s < PAGE_SIZE)
385 len = strnlen(s, spec.precision);
387 if (!(spec.flags & LEFT)) {
388 while (len < spec.field_width--) {
394 for (i = 0; i < len; ++i) {
399 while (len < spec.field_width--) {
408 static noinline_for_stack
409 char *symbol_string(char *buf, char *end, void *ptr,
410 struct printf_spec spec, char ext)
412 unsigned long value = (unsigned long) ptr;
413 #ifdef CONFIG_KALLSYMS
414 char sym[KSYM_SYMBOL_LEN];
416 sprint_backtrace(sym, value);
417 else if (ext != 'f' && ext != 's')
418 sprint_symbol(sym, value);
420 kallsyms_lookup(value, NULL, NULL, NULL, sym);
422 return string(buf, end, sym, spec);
424 spec.field_width = 2 * sizeof(void *);
425 spec.flags |= SPECIAL | SMALL | ZEROPAD;
428 return number(buf, end, value, spec);
432 static noinline_for_stack
433 char *resource_string(char *buf, char *end, struct resource *res,
434 struct printf_spec spec, const char *fmt)
436 #ifndef IO_RSRC_PRINTK_SIZE
437 #define IO_RSRC_PRINTK_SIZE 6
440 #ifndef MEM_RSRC_PRINTK_SIZE
441 #define MEM_RSRC_PRINTK_SIZE 10
443 static const struct printf_spec io_spec = {
445 .field_width = IO_RSRC_PRINTK_SIZE,
447 .flags = SPECIAL | SMALL | ZEROPAD,
449 static const struct printf_spec mem_spec = {
451 .field_width = MEM_RSRC_PRINTK_SIZE,
453 .flags = SPECIAL | SMALL | ZEROPAD,
455 static const struct printf_spec bus_spec = {
459 .flags = SMALL | ZEROPAD,
461 static const struct printf_spec dec_spec = {
466 static const struct printf_spec str_spec = {
471 static const struct printf_spec flag_spec = {
474 .flags = SPECIAL | SMALL,
477 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
478 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
479 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
480 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
481 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
482 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
483 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
484 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
486 char *p = sym, *pend = sym + sizeof(sym);
487 int decode = (fmt[0] == 'R') ? 1 : 0;
488 const struct printf_spec *specp;
491 if (res->flags & IORESOURCE_IO) {
492 p = string(p, pend, "io ", str_spec);
494 } else if (res->flags & IORESOURCE_MEM) {
495 p = string(p, pend, "mem ", str_spec);
497 } else if (res->flags & IORESOURCE_IRQ) {
498 p = string(p, pend, "irq ", str_spec);
500 } else if (res->flags & IORESOURCE_DMA) {
501 p = string(p, pend, "dma ", str_spec);
503 } else if (res->flags & IORESOURCE_BUS) {
504 p = string(p, pend, "bus ", str_spec);
507 p = string(p, pend, "??? ", str_spec);
511 p = number(p, pend, res->start, *specp);
512 if (res->start != res->end) {
514 p = number(p, pend, res->end, *specp);
517 if (res->flags & IORESOURCE_MEM_64)
518 p = string(p, pend, " 64bit", str_spec);
519 if (res->flags & IORESOURCE_PREFETCH)
520 p = string(p, pend, " pref", str_spec);
521 if (res->flags & IORESOURCE_WINDOW)
522 p = string(p, pend, " window", str_spec);
523 if (res->flags & IORESOURCE_DISABLED)
524 p = string(p, pend, " disabled", str_spec);
526 p = string(p, pend, " flags ", str_spec);
527 p = number(p, pend, res->flags, flag_spec);
532 return string(buf, end, sym, spec);
535 static noinline_for_stack
536 char *mac_address_string(char *buf, char *end, u8 *addr,
537 struct printf_spec spec, const char *fmt)
539 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
544 if (fmt[1] == 'F') { /* FDDI canonical format */
550 for (i = 0; i < 6; i++) {
551 p = hex_byte_pack(p, addr[i]);
552 if (fmt[0] == 'M' && i != 5)
557 return string(buf, end, mac_addr, spec);
560 static noinline_for_stack
561 char *ip4_string(char *p, const u8 *addr, const char *fmt)
564 bool leading_zeros = (fmt[0] == 'i');
589 for (i = 0; i < 4; i++) {
590 char temp[3]; /* hold each IP quad in reverse order */
591 int digits = put_dec_trunc(temp, addr[index]) - temp;
598 /* reverse the digits in the quad */
610 static noinline_for_stack
611 char *ip6_compressed_string(char *p, const char *addr)
614 unsigned char zerolength[8];
619 bool needcolon = false;
623 memcpy(&in6, addr, sizeof(struct in6_addr));
625 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
627 memset(zerolength, 0, sizeof(zerolength));
634 /* find position of longest 0 run */
635 for (i = 0; i < range; i++) {
636 for (j = i; j < range; j++) {
637 if (in6.s6_addr16[j] != 0)
642 for (i = 0; i < range; i++) {
643 if (zerolength[i] > longest) {
644 longest = zerolength[i];
648 if (longest == 1) /* don't compress a single 0 */
652 for (i = 0; i < range; i++) {
654 if (needcolon || i == 0)
665 /* hex u16 without leading 0s */
666 word = ntohs(in6.s6_addr16[i]);
671 p = hex_byte_pack(p, hi);
673 *p++ = hex_asc_lo(hi);
674 p = hex_byte_pack(p, lo);
677 p = hex_byte_pack(p, lo);
679 *p++ = hex_asc_lo(lo);
686 p = ip4_string(p, &in6.s6_addr[12], "I4");
693 static noinline_for_stack
694 char *ip6_string(char *p, const char *addr, const char *fmt)
698 for (i = 0; i < 8; i++) {
699 p = hex_byte_pack(p, *addr++);
700 p = hex_byte_pack(p, *addr++);
701 if (fmt[0] == 'I' && i != 7)
709 static noinline_for_stack
710 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
711 struct printf_spec spec, const char *fmt)
713 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
715 if (fmt[0] == 'I' && fmt[2] == 'c')
716 ip6_compressed_string(ip6_addr, addr);
718 ip6_string(ip6_addr, addr, fmt);
720 return string(buf, end, ip6_addr, spec);
723 static noinline_for_stack
724 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
725 struct printf_spec spec, const char *fmt)
727 char ip4_addr[sizeof("255.255.255.255")];
729 ip4_string(ip4_addr, addr, fmt);
731 return string(buf, end, ip4_addr, spec);
734 static noinline_for_stack
735 char *uuid_string(char *buf, char *end, const u8 *addr,
736 struct printf_spec spec, const char *fmt)
738 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
741 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
742 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
743 const u8 *index = be;
748 uc = true; /* fall-through */
757 for (i = 0; i < 16; i++) {
758 p = hex_byte_pack(p, addr[index[i]]);
778 return string(buf, end, uuid, spec);
781 int kptr_restrict __read_mostly;
784 * Show a '%p' thing. A kernel extension is that the '%p' is followed
785 * by an extra set of alphanumeric characters that are extended format
788 * Right now we handle:
790 * - 'F' For symbolic function descriptor pointers with offset
791 * - 'f' For simple symbolic function names without offset
792 * - 'S' For symbolic direct pointers with offset
793 * - 's' For symbolic direct pointers without offset
794 * - 'B' For backtraced symbolic direct pointers with offset
795 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
796 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
797 * - 'M' For a 6-byte MAC address, it prints the address in the
798 * usual colon-separated hex notation
799 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
800 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
801 * with a dash-separated hex notation
802 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
803 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
804 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
805 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
806 * IPv6 omits the colons (01020304...0f)
807 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
808 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
809 * - 'I6c' for IPv6 addresses printed as specified by
810 * http://tools.ietf.org/html/rfc5952
811 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
812 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
813 * Options for %pU are:
814 * b big endian lower case hex (default)
815 * B big endian UPPER case hex
816 * l little endian lower case hex
817 * L little endian UPPER case hex
818 * big endian output byte order is:
819 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
820 * little endian output byte order is:
821 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
822 * - 'V' For a struct va_format which contains a format string * and va_list *,
823 * call vsnprintf(->format, *->va_list).
824 * Implements a "recursive vsnprintf".
825 * Do not use this feature without some mechanism to verify the
826 * correctness of the format string and va_list arguments.
827 * - 'K' For a kernel pointer that should be hidden from unprivileged users
829 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
830 * function pointers are really function descriptors, which contain a
831 * pointer to the real address.
833 static noinline_for_stack
834 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
835 struct printf_spec spec)
837 if (!ptr && *fmt != 'K') {
839 * Print (null) with the same width as a pointer so it makes
840 * tabular output look nice.
842 if (spec.field_width == -1)
843 spec.field_width = 2 * sizeof(void *);
844 return string(buf, end, "(null)", spec);
850 ptr = dereference_function_descriptor(ptr);
855 return symbol_string(buf, end, ptr, spec, *fmt);
858 return resource_string(buf, end, ptr, spec, fmt);
859 case 'M': /* Colon separated: 00:01:02:03:04:05 */
860 case 'm': /* Contiguous: 000102030405 */
861 /* [mM]F (FDDI, bit reversed) */
862 return mac_address_string(buf, end, ptr, spec, fmt);
863 case 'I': /* Formatted IP supported
865 * 6: 0001:0203:...:0708
866 * 6c: 1::708 or 1::1.2.3.4
868 case 'i': /* Contiguous:
874 return ip6_addr_string(buf, end, ptr, spec, fmt);
876 return ip4_addr_string(buf, end, ptr, spec, fmt);
880 return uuid_string(buf, end, ptr, spec, fmt);
882 return buf + vsnprintf(buf, end > buf ? end - buf : 0,
883 ((struct va_format *)ptr)->fmt,
884 *(((struct va_format *)ptr)->va));
887 * %pK cannot be used in IRQ context because its test
888 * for CAP_SYSLOG would be meaningless.
890 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
892 if (spec.field_width == -1)
893 spec.field_width = 2 * sizeof(void *);
894 return string(buf, end, "pK-error", spec);
897 switch (kptr_restrict) {
899 /* Always print %pK values */
903 * Only print the real pointer value if the current
904 * process has CAP_SYSLOG and is running with the
905 * same credentials it started with. This is because
906 * access to files is checked at open() time, but %pK
907 * checks permission at read() time. We don't want to
908 * leak pointer values if a binary opens a file using
909 * %pK and then elevates privileges before reading it.
911 const struct cred *cred = current_cred();
913 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
914 cred->euid != cred->uid ||
915 cred->egid != cred->gid)
921 /* Always print 0's for %pK */
928 if (spec.field_width == -1) {
929 spec.field_width = 2 * sizeof(void *);
930 spec.flags |= ZEROPAD;
934 return number(buf, end, (unsigned long) ptr, spec);
938 * Helper function to decode printf style format.
939 * Each call decode a token from the format and return the
940 * number of characters read (or likely the delta where it wants
941 * to go on the next call).
942 * The decoded token is returned through the parameters
944 * 'h', 'l', or 'L' for integer fields
945 * 'z' support added 23/7/1999 S.H.
946 * 'z' changed to 'Z' --davidm 1/25/99
947 * 't' added for ptrdiff_t
949 * @fmt: the format string
950 * @type of the token returned
951 * @flags: various flags such as +, -, # tokens..
952 * @field_width: overwritten width
953 * @base: base of the number (octal, hex, ...)
954 * @precision: precision of a number
955 * @qualifier: qualifier of a number (long, size_t, ...)
957 static noinline_for_stack
958 int format_decode(const char *fmt, struct printf_spec *spec)
960 const char *start = fmt;
962 /* we finished early by reading the field width */
963 if (spec->type == FORMAT_TYPE_WIDTH) {
964 if (spec->field_width < 0) {
965 spec->field_width = -spec->field_width;
968 spec->type = FORMAT_TYPE_NONE;
972 /* we finished early by reading the precision */
973 if (spec->type == FORMAT_TYPE_PRECISION) {
974 if (spec->precision < 0)
977 spec->type = FORMAT_TYPE_NONE;
982 spec->type = FORMAT_TYPE_NONE;
984 for (; *fmt ; ++fmt) {
989 /* Return the current non-format string */
990 if (fmt != start || !*fmt)
996 while (1) { /* this also skips first '%' */
1002 case '-': spec->flags |= LEFT; break;
1003 case '+': spec->flags |= PLUS; break;
1004 case ' ': spec->flags |= SPACE; break;
1005 case '#': spec->flags |= SPECIAL; break;
1006 case '0': spec->flags |= ZEROPAD; break;
1007 default: found = false;
1014 /* get field width */
1015 spec->field_width = -1;
1018 spec->field_width = skip_atoi(&fmt);
1019 else if (*fmt == '*') {
1020 /* it's the next argument */
1021 spec->type = FORMAT_TYPE_WIDTH;
1022 return ++fmt - start;
1026 /* get the precision */
1027 spec->precision = -1;
1030 if (isdigit(*fmt)) {
1031 spec->precision = skip_atoi(&fmt);
1032 if (spec->precision < 0)
1033 spec->precision = 0;
1034 } else if (*fmt == '*') {
1035 /* it's the next argument */
1036 spec->type = FORMAT_TYPE_PRECISION;
1037 return ++fmt - start;
1042 /* get the conversion qualifier */
1043 spec->qualifier = -1;
1044 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1045 _tolower(*fmt) == 'z' || *fmt == 't') {
1046 spec->qualifier = *fmt++;
1047 if (unlikely(spec->qualifier == *fmt)) {
1048 if (spec->qualifier == 'l') {
1049 spec->qualifier = 'L';
1051 } else if (spec->qualifier == 'h') {
1052 spec->qualifier = 'H';
1062 spec->type = FORMAT_TYPE_CHAR;
1063 return ++fmt - start;
1066 spec->type = FORMAT_TYPE_STR;
1067 return ++fmt - start;
1070 spec->type = FORMAT_TYPE_PTR;
1075 spec->type = FORMAT_TYPE_NRCHARS;
1076 return ++fmt - start;
1079 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1080 return ++fmt - start;
1082 /* integer number formats - set up the flags and "break" */
1088 spec->flags |= SMALL;
1096 spec->flags |= SIGN;
1101 spec->type = FORMAT_TYPE_INVALID;
1105 if (spec->qualifier == 'L')
1106 spec->type = FORMAT_TYPE_LONG_LONG;
1107 else if (spec->qualifier == 'l') {
1108 if (spec->flags & SIGN)
1109 spec->type = FORMAT_TYPE_LONG;
1111 spec->type = FORMAT_TYPE_ULONG;
1112 } else if (_tolower(spec->qualifier) == 'z') {
1113 spec->type = FORMAT_TYPE_SIZE_T;
1114 } else if (spec->qualifier == 't') {
1115 spec->type = FORMAT_TYPE_PTRDIFF;
1116 } else if (spec->qualifier == 'H') {
1117 if (spec->flags & SIGN)
1118 spec->type = FORMAT_TYPE_BYTE;
1120 spec->type = FORMAT_TYPE_UBYTE;
1121 } else if (spec->qualifier == 'h') {
1122 if (spec->flags & SIGN)
1123 spec->type = FORMAT_TYPE_SHORT;
1125 spec->type = FORMAT_TYPE_USHORT;
1127 if (spec->flags & SIGN)
1128 spec->type = FORMAT_TYPE_INT;
1130 spec->type = FORMAT_TYPE_UINT;
1133 return ++fmt - start;
1137 * vsnprintf - Format a string and place it in a buffer
1138 * @buf: The buffer to place the result into
1139 * @size: The size of the buffer, including the trailing null space
1140 * @fmt: The format string to use
1141 * @args: Arguments for the format string
1143 * This function follows C99 vsnprintf, but has some extensions:
1144 * %pS output the name of a text symbol with offset
1145 * %ps output the name of a text symbol without offset
1146 * %pF output the name of a function pointer with its offset
1147 * %pf output the name of a function pointer without its offset
1148 * %pB output the name of a backtrace symbol with its offset
1149 * %pR output the address range in a struct resource with decoded flags
1150 * %pr output the address range in a struct resource with raw flags
1151 * %pM output a 6-byte MAC address with colons
1152 * %pm output a 6-byte MAC address without colons
1153 * %pI4 print an IPv4 address without leading zeros
1154 * %pi4 print an IPv4 address with leading zeros
1155 * %pI6 print an IPv6 address with colons
1156 * %pi6 print an IPv6 address without colons
1157 * %pI6c print an IPv6 address as specified by RFC 5952
1158 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1162 * The return value is the number of characters which would
1163 * be generated for the given input, excluding the trailing
1164 * '\0', as per ISO C99. If you want to have the exact
1165 * number of characters written into @buf as return value
1166 * (not including the trailing '\0'), use vscnprintf(). If the
1167 * return is greater than or equal to @size, the resulting
1168 * string is truncated.
1170 * If you're not already dealing with a va_list consider using snprintf().
1172 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1174 unsigned long long num;
1176 struct printf_spec spec = {0};
1178 /* Reject out-of-range values early. Large positive sizes are
1179 used for unknown buffer sizes. */
1180 if (WARN_ON_ONCE((int) size < 0))
1186 /* Make sure end is always >= buf */
1193 const char *old_fmt = fmt;
1194 int read = format_decode(fmt, &spec);
1198 switch (spec.type) {
1199 case FORMAT_TYPE_NONE: {
1202 if (copy > end - str)
1204 memcpy(str, old_fmt, copy);
1210 case FORMAT_TYPE_WIDTH:
1211 spec.field_width = va_arg(args, int);
1214 case FORMAT_TYPE_PRECISION:
1215 spec.precision = va_arg(args, int);
1218 case FORMAT_TYPE_CHAR: {
1221 if (!(spec.flags & LEFT)) {
1222 while (--spec.field_width > 0) {
1229 c = (unsigned char) va_arg(args, int);
1233 while (--spec.field_width > 0) {
1241 case FORMAT_TYPE_STR:
1242 str = string(str, end, va_arg(args, char *), spec);
1245 case FORMAT_TYPE_PTR:
1246 str = pointer(fmt+1, str, end, va_arg(args, void *),
1248 while (isalnum(*fmt))
1252 case FORMAT_TYPE_PERCENT_CHAR:
1258 case FORMAT_TYPE_INVALID:
1264 case FORMAT_TYPE_NRCHARS: {
1265 u8 qualifier = spec.qualifier;
1267 if (qualifier == 'l') {
1268 long *ip = va_arg(args, long *);
1270 } else if (_tolower(qualifier) == 'z') {
1271 size_t *ip = va_arg(args, size_t *);
1274 int *ip = va_arg(args, int *);
1281 switch (spec.type) {
1282 case FORMAT_TYPE_LONG_LONG:
1283 num = va_arg(args, long long);
1285 case FORMAT_TYPE_ULONG:
1286 num = va_arg(args, unsigned long);
1288 case FORMAT_TYPE_LONG:
1289 num = va_arg(args, long);
1291 case FORMAT_TYPE_SIZE_T:
1292 num = va_arg(args, size_t);
1294 case FORMAT_TYPE_PTRDIFF:
1295 num = va_arg(args, ptrdiff_t);
1297 case FORMAT_TYPE_UBYTE:
1298 num = (unsigned char) va_arg(args, int);
1300 case FORMAT_TYPE_BYTE:
1301 num = (signed char) va_arg(args, int);
1303 case FORMAT_TYPE_USHORT:
1304 num = (unsigned short) va_arg(args, int);
1306 case FORMAT_TYPE_SHORT:
1307 num = (short) va_arg(args, int);
1309 case FORMAT_TYPE_INT:
1310 num = (int) va_arg(args, int);
1313 num = va_arg(args, unsigned int);
1316 str = number(str, end, num, spec);
1327 /* the trailing null byte doesn't count towards the total */
1331 EXPORT_SYMBOL(vsnprintf);
1334 * vscnprintf - Format a string and place it in a buffer
1335 * @buf: The buffer to place the result into
1336 * @size: The size of the buffer, including the trailing null space
1337 * @fmt: The format string to use
1338 * @args: Arguments for the format string
1340 * The return value is the number of characters which have been written into
1341 * the @buf not including the trailing '\0'. If @size is == 0 the function
1344 * If you're not already dealing with a va_list consider using scnprintf().
1346 * See the vsnprintf() documentation for format string extensions over C99.
1348 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1352 i = vsnprintf(buf, size, fmt, args);
1354 if (likely(i < size))
1360 EXPORT_SYMBOL(vscnprintf);
1363 * snprintf - Format a string and place it in a buffer
1364 * @buf: The buffer to place the result into
1365 * @size: The size of the buffer, including the trailing null space
1366 * @fmt: The format string to use
1367 * @...: Arguments for the format string
1369 * The return value is the number of characters which would be
1370 * generated for the given input, excluding the trailing null,
1371 * as per ISO C99. If the return is greater than or equal to
1372 * @size, the resulting string is truncated.
1374 * See the vsnprintf() documentation for format string extensions over C99.
1376 int snprintf(char *buf, size_t size, const char *fmt, ...)
1381 va_start(args, fmt);
1382 i = vsnprintf(buf, size, fmt, args);
1387 EXPORT_SYMBOL(snprintf);
1390 * scnprintf - Format a string and place it in a buffer
1391 * @buf: The buffer to place the result into
1392 * @size: The size of the buffer, including the trailing null space
1393 * @fmt: The format string to use
1394 * @...: Arguments for the format string
1396 * The return value is the number of characters written into @buf not including
1397 * the trailing '\0'. If @size is == 0 the function returns 0.
1400 int scnprintf(char *buf, size_t size, const char *fmt, ...)
1405 va_start(args, fmt);
1406 i = vscnprintf(buf, size, fmt, args);
1411 EXPORT_SYMBOL(scnprintf);
1414 * vsprintf - Format a string and place it in a buffer
1415 * @buf: The buffer to place the result into
1416 * @fmt: The format string to use
1417 * @args: Arguments for the format string
1419 * The function returns the number of characters written
1420 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1423 * If you're not already dealing with a va_list consider using sprintf().
1425 * See the vsnprintf() documentation for format string extensions over C99.
1427 int vsprintf(char *buf, const char *fmt, va_list args)
1429 return vsnprintf(buf, INT_MAX, fmt, args);
1431 EXPORT_SYMBOL(vsprintf);
1434 * sprintf - Format a string and place it in a buffer
1435 * @buf: The buffer to place the result into
1436 * @fmt: The format string to use
1437 * @...: Arguments for the format string
1439 * The function returns the number of characters written
1440 * into @buf. Use snprintf() or scnprintf() in order to avoid
1443 * See the vsnprintf() documentation for format string extensions over C99.
1445 int sprintf(char *buf, const char *fmt, ...)
1450 va_start(args, fmt);
1451 i = vsnprintf(buf, INT_MAX, fmt, args);
1456 EXPORT_SYMBOL(sprintf);
1458 #ifdef CONFIG_BINARY_PRINTF
1461 * vbin_printf() - VA arguments to binary data
1462 * bstr_printf() - Binary data to text string
1466 * vbin_printf - Parse a format string and place args' binary value in a buffer
1467 * @bin_buf: The buffer to place args' binary value
1468 * @size: The size of the buffer(by words(32bits), not characters)
1469 * @fmt: The format string to use
1470 * @args: Arguments for the format string
1472 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1475 * The return value is the number of words(32bits) which would be generated for
1479 * If the return value is greater than @size, the resulting bin_buf is NOT
1480 * valid for bstr_printf().
1482 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1484 struct printf_spec spec = {0};
1487 str = (char *)bin_buf;
1488 end = (char *)(bin_buf + size);
1490 #define save_arg(type) \
1492 if (sizeof(type) == 8) { \
1493 unsigned long long value; \
1494 str = PTR_ALIGN(str, sizeof(u32)); \
1495 value = va_arg(args, unsigned long long); \
1496 if (str + sizeof(type) <= end) { \
1497 *(u32 *)str = *(u32 *)&value; \
1498 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1501 unsigned long value; \
1502 str = PTR_ALIGN(str, sizeof(type)); \
1503 value = va_arg(args, int); \
1504 if (str + sizeof(type) <= end) \
1505 *(typeof(type) *)str = (type)value; \
1507 str += sizeof(type); \
1511 int read = format_decode(fmt, &spec);
1515 switch (spec.type) {
1516 case FORMAT_TYPE_NONE:
1517 case FORMAT_TYPE_INVALID:
1518 case FORMAT_TYPE_PERCENT_CHAR:
1521 case FORMAT_TYPE_WIDTH:
1522 case FORMAT_TYPE_PRECISION:
1526 case FORMAT_TYPE_CHAR:
1530 case FORMAT_TYPE_STR: {
1531 const char *save_str = va_arg(args, char *);
1534 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1535 || (unsigned long)save_str < PAGE_SIZE)
1536 save_str = "(null)";
1537 len = strlen(save_str) + 1;
1538 if (str + len < end)
1539 memcpy(str, save_str, len);
1544 case FORMAT_TYPE_PTR:
1546 /* skip all alphanumeric pointer suffixes */
1547 while (isalnum(*fmt))
1551 case FORMAT_TYPE_NRCHARS: {
1552 /* skip %n 's argument */
1553 u8 qualifier = spec.qualifier;
1555 if (qualifier == 'l')
1556 skip_arg = va_arg(args, long *);
1557 else if (_tolower(qualifier) == 'z')
1558 skip_arg = va_arg(args, size_t *);
1560 skip_arg = va_arg(args, int *);
1565 switch (spec.type) {
1567 case FORMAT_TYPE_LONG_LONG:
1568 save_arg(long long);
1570 case FORMAT_TYPE_ULONG:
1571 case FORMAT_TYPE_LONG:
1572 save_arg(unsigned long);
1574 case FORMAT_TYPE_SIZE_T:
1577 case FORMAT_TYPE_PTRDIFF:
1578 save_arg(ptrdiff_t);
1580 case FORMAT_TYPE_UBYTE:
1581 case FORMAT_TYPE_BYTE:
1584 case FORMAT_TYPE_USHORT:
1585 case FORMAT_TYPE_SHORT:
1594 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1597 EXPORT_SYMBOL_GPL(vbin_printf);
1600 * bstr_printf - Format a string from binary arguments and place it in a buffer
1601 * @buf: The buffer to place the result into
1602 * @size: The size of the buffer, including the trailing null space
1603 * @fmt: The format string to use
1604 * @bin_buf: Binary arguments for the format string
1606 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1607 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1608 * a binary buffer that generated by vbin_printf.
1610 * The format follows C99 vsnprintf, but has some extensions:
1611 * see vsnprintf comment for details.
1613 * The return value is the number of characters which would
1614 * be generated for the given input, excluding the trailing
1615 * '\0', as per ISO C99. If you want to have the exact
1616 * number of characters written into @buf as return value
1617 * (not including the trailing '\0'), use vscnprintf(). If the
1618 * return is greater than or equal to @size, the resulting
1619 * string is truncated.
1621 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1623 struct printf_spec spec = {0};
1625 const char *args = (const char *)bin_buf;
1627 if (WARN_ON_ONCE((int) size < 0))
1633 #define get_arg(type) \
1635 typeof(type) value; \
1636 if (sizeof(type) == 8) { \
1637 args = PTR_ALIGN(args, sizeof(u32)); \
1638 *(u32 *)&value = *(u32 *)args; \
1639 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1641 args = PTR_ALIGN(args, sizeof(type)); \
1642 value = *(typeof(type) *)args; \
1644 args += sizeof(type); \
1648 /* Make sure end is always >= buf */
1655 const char *old_fmt = fmt;
1656 int read = format_decode(fmt, &spec);
1660 switch (spec.type) {
1661 case FORMAT_TYPE_NONE: {
1664 if (copy > end - str)
1666 memcpy(str, old_fmt, copy);
1672 case FORMAT_TYPE_WIDTH:
1673 spec.field_width = get_arg(int);
1676 case FORMAT_TYPE_PRECISION:
1677 spec.precision = get_arg(int);
1680 case FORMAT_TYPE_CHAR: {
1683 if (!(spec.flags & LEFT)) {
1684 while (--spec.field_width > 0) {
1690 c = (unsigned char) get_arg(char);
1694 while (--spec.field_width > 0) {
1702 case FORMAT_TYPE_STR: {
1703 const char *str_arg = args;
1704 args += strlen(str_arg) + 1;
1705 str = string(str, end, (char *)str_arg, spec);
1709 case FORMAT_TYPE_PTR:
1710 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1711 while (isalnum(*fmt))
1715 case FORMAT_TYPE_PERCENT_CHAR:
1716 case FORMAT_TYPE_INVALID:
1722 case FORMAT_TYPE_NRCHARS:
1727 unsigned long long num;
1729 switch (spec.type) {
1731 case FORMAT_TYPE_LONG_LONG:
1732 num = get_arg(long long);
1734 case FORMAT_TYPE_ULONG:
1735 case FORMAT_TYPE_LONG:
1736 num = get_arg(unsigned long);
1738 case FORMAT_TYPE_SIZE_T:
1739 num = get_arg(size_t);
1741 case FORMAT_TYPE_PTRDIFF:
1742 num = get_arg(ptrdiff_t);
1744 case FORMAT_TYPE_UBYTE:
1745 num = get_arg(unsigned char);
1747 case FORMAT_TYPE_BYTE:
1748 num = get_arg(signed char);
1750 case FORMAT_TYPE_USHORT:
1751 num = get_arg(unsigned short);
1753 case FORMAT_TYPE_SHORT:
1754 num = get_arg(short);
1756 case FORMAT_TYPE_UINT:
1757 num = get_arg(unsigned int);
1763 str = number(str, end, num, spec);
1765 } /* switch(spec.type) */
1777 /* the trailing null byte doesn't count towards the total */
1780 EXPORT_SYMBOL_GPL(bstr_printf);
1783 * bprintf - Parse a format string and place args' binary value in a buffer
1784 * @bin_buf: The buffer to place args' binary value
1785 * @size: The size of the buffer(by words(32bits), not characters)
1786 * @fmt: The format string to use
1787 * @...: Arguments for the format string
1789 * The function returns the number of words(u32) written
1792 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1797 va_start(args, fmt);
1798 ret = vbin_printf(bin_buf, size, fmt, args);
1803 EXPORT_SYMBOL_GPL(bprintf);
1805 #endif /* CONFIG_BINARY_PRINTF */
1808 * vsscanf - Unformat a buffer into a list of arguments
1809 * @buf: input buffer
1810 * @fmt: format of buffer
1813 int vsscanf(const char *buf, const char *fmt, va_list args)
1815 const char *str = buf;
1824 while (*fmt && *str) {
1825 /* skip any white space in format */
1826 /* white space in format matchs any amount of
1827 * white space, including none, in the input.
1829 if (isspace(*fmt)) {
1830 fmt = skip_spaces(++fmt);
1831 str = skip_spaces(str);
1834 /* anything that is not a conversion must match exactly */
1835 if (*fmt != '%' && *fmt) {
1836 if (*fmt++ != *str++)
1845 /* skip this conversion.
1846 * advance both strings to next white space
1849 while (!isspace(*fmt) && *fmt != '%' && *fmt)
1851 while (!isspace(*str) && *str)
1856 /* get field width */
1859 field_width = skip_atoi(&fmt);
1861 /* get conversion qualifier */
1863 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1864 _tolower(*fmt) == 'z') {
1866 if (unlikely(qualifier == *fmt)) {
1867 if (qualifier == 'h') {
1870 } else if (qualifier == 'l') {
1886 char *s = (char *)va_arg(args, char*);
1887 if (field_width == -1)
1891 } while (--field_width > 0 && *str);
1897 char *s = (char *)va_arg(args, char *);
1898 if (field_width == -1)
1899 field_width = SHRT_MAX;
1900 /* first, skip leading white space in buffer */
1901 str = skip_spaces(str);
1903 /* now copy until next white space */
1904 while (*str && !isspace(*str) && field_width--)
1911 /* return number of characters read so far */
1913 int *i = (int *)va_arg(args, int*);
1931 /* looking for '%' in str */
1936 /* invalid format; stop here */
1940 /* have some sort of integer conversion.
1941 * first, skip white space in buffer.
1943 str = skip_spaces(str);
1946 if (is_sign && digit == '-')
1950 || (base == 16 && !isxdigit(digit))
1951 || (base == 10 && !isdigit(digit))
1952 || (base == 8 && (!isdigit(digit) || digit > '7'))
1953 || (base == 0 && !isdigit(digit)))
1956 switch (qualifier) {
1957 case 'H': /* that's 'hh' in format */
1959 signed char *s = (signed char *)va_arg(args, signed char *);
1960 *s = (signed char)simple_strtol(str, &next, base);
1962 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
1963 *s = (unsigned char)simple_strtoul(str, &next, base);
1968 short *s = (short *)va_arg(args, short *);
1969 *s = (short)simple_strtol(str, &next, base);
1971 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
1972 *s = (unsigned short)simple_strtoul(str, &next, base);
1977 long *l = (long *)va_arg(args, long *);
1978 *l = simple_strtol(str, &next, base);
1980 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
1981 *l = simple_strtoul(str, &next, base);
1986 long long *l = (long long *)va_arg(args, long long *);
1987 *l = simple_strtoll(str, &next, base);
1989 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
1990 *l = simple_strtoull(str, &next, base);
1996 size_t *s = (size_t *)va_arg(args, size_t *);
1997 *s = (size_t)simple_strtoul(str, &next, base);
2002 int *i = (int *)va_arg(args, int *);
2003 *i = (int)simple_strtol(str, &next, base);
2005 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2006 *i = (unsigned int)simple_strtoul(str, &next, base);
2018 * Now we've come all the way through so either the input string or the
2019 * format ended. In the former case, there can be a %n at the current
2020 * position in the format that needs to be filled.
2022 if (*fmt == '%' && *(fmt + 1) == 'n') {
2023 int *p = (int *)va_arg(args, int *);
2029 EXPORT_SYMBOL(vsscanf);
2032 * sscanf - Unformat a buffer into a list of arguments
2033 * @buf: input buffer
2034 * @fmt: formatting of buffer
2035 * @...: resulting arguments
2037 int sscanf(const char *buf, const char *fmt, ...)
2042 va_start(args, fmt);
2043 i = vsscanf(buf, fmt, args);
2048 EXPORT_SYMBOL(sscanf);