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 <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() */
34 /* Works only for digits and letters, but small and fast */
35 #define TOLOWER(x) ((x) | 0x20)
37 static unsigned int simple_guess_base(const char *cp)
40 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
50 * simple_strtoul - convert a string to an unsigned long
51 * @cp: The start of the string
52 * @endp: A pointer to the end of the parsed string will be placed here
53 * @base: The number base to use
55 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
57 unsigned long result = 0;
60 base = simple_guess_base(cp);
62 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
65 while (isxdigit(*cp)) {
68 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
71 result = result * base + value;
79 EXPORT_SYMBOL(simple_strtoul);
82 * simple_strtol - convert a string to a signed long
83 * @cp: The start of the string
84 * @endp: A pointer to the end of the parsed string will be placed here
85 * @base: The number base to use
87 long simple_strtol(const char *cp, char **endp, unsigned int base)
90 return -simple_strtoul(cp + 1, endp, base);
92 return simple_strtoul(cp, endp, base);
94 EXPORT_SYMBOL(simple_strtol);
97 * simple_strtoull - convert a string to an unsigned long long
98 * @cp: The start of the string
99 * @endp: A pointer to the end of the parsed string will be placed here
100 * @base: The number base to use
102 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
104 unsigned long long result = 0;
107 base = simple_guess_base(cp);
109 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
112 while (isxdigit(*cp)) {
115 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
118 result = result * base + value;
126 EXPORT_SYMBOL(simple_strtoull);
129 * simple_strtoll - convert a string to a signed long long
130 * @cp: The start of the string
131 * @endp: A pointer to the end of the parsed string will be placed here
132 * @base: The number base to use
134 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
137 return -simple_strtoull(cp + 1, endp, base);
139 return simple_strtoull(cp, endp, base);
143 * strict_strtoul - convert a string to an unsigned long strictly
144 * @cp: The string to be converted
145 * @base: The number base to use
146 * @res: The converted result value
148 * strict_strtoul converts a string to an unsigned long only if the
149 * string is really an unsigned long string, any string containing
150 * any invalid char at the tail will be rejected and -EINVAL is returned,
151 * only a newline char at the tail is acceptible because people generally
152 * change a module parameter in the following way:
154 * echo 1024 > /sys/module/e1000/parameters/copybreak
156 * echo will append a newline to the tail.
158 * It returns 0 if conversion is successful and *res is set to the converted
159 * value, otherwise it returns -EINVAL and *res is set to 0.
161 * simple_strtoul just ignores the successive invalid characters and
162 * return the converted value of prefix part of the string.
164 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
175 val = simple_strtoul(cp, &tail, base);
179 if ((*tail == '\0') ||
180 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
187 EXPORT_SYMBOL(strict_strtoul);
190 * strict_strtol - convert a string to a long strictly
191 * @cp: The string to be converted
192 * @base: The number base to use
193 * @res: The converted result value
195 * strict_strtol is similiar to strict_strtoul, but it allows the first
196 * character of a string is '-'.
198 * It returns 0 if conversion is successful and *res is set to the converted
199 * value, otherwise it returns -EINVAL and *res is set to 0.
201 int strict_strtol(const char *cp, unsigned int base, long *res)
205 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
209 ret = strict_strtoul(cp, base, (unsigned long *)res);
214 EXPORT_SYMBOL(strict_strtol);
217 * strict_strtoull - convert a string to an unsigned long long strictly
218 * @cp: The string to be converted
219 * @base: The number base to use
220 * @res: The converted result value
222 * strict_strtoull converts a string to an unsigned long long only if the
223 * string is really an unsigned long long string, any string containing
224 * any invalid char at the tail will be rejected and -EINVAL is returned,
225 * only a newline char at the tail is acceptible because people generally
226 * change a module parameter in the following way:
228 * echo 1024 > /sys/module/e1000/parameters/copybreak
230 * echo will append a newline to the tail of the string.
232 * It returns 0 if conversion is successful and *res is set to the converted
233 * value, otherwise it returns -EINVAL and *res is set to 0.
235 * simple_strtoull just ignores the successive invalid characters and
236 * return the converted value of prefix part of the string.
238 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
241 unsigned long long val;
249 val = simple_strtoull(cp, &tail, base);
252 if ((*tail == '\0') ||
253 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
260 EXPORT_SYMBOL(strict_strtoull);
263 * strict_strtoll - convert a string to a long long strictly
264 * @cp: The string to be converted
265 * @base: The number base to use
266 * @res: The converted result value
268 * strict_strtoll is similiar to strict_strtoull, but it allows the first
269 * character of a string is '-'.
271 * It returns 0 if conversion is successful and *res is set to the converted
272 * value, otherwise it returns -EINVAL and *res is set to 0.
274 int strict_strtoll(const char *cp, unsigned int base, long long *res)
278 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
282 ret = strict_strtoull(cp, base, (unsigned long long *)res);
287 EXPORT_SYMBOL(strict_strtoll);
289 static int skip_atoi(const char **s)
294 i = i*10 + *((*s)++) - '0';
299 /* Decimal conversion is by far the most typical, and is used
300 * for /proc and /sys data. This directly impacts e.g. top performance
301 * with many processes running. We optimize it for speed
303 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
304 * (with permission from the author, Douglas W. Jones). */
306 /* Formats correctly any integer in [0,99999].
307 * Outputs from one to five digits depending on input.
308 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
309 static char *put_dec_trunc(char *buf, unsigned q)
311 unsigned d3, d2, d1, d0;
316 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
317 q = (d0 * 0xcd) >> 11;
319 *buf++ = d0 + '0'; /* least significant digit */
320 d1 = q + 9*d3 + 5*d2 + d1;
322 q = (d1 * 0xcd) >> 11;
324 *buf++ = d1 + '0'; /* next digit */
327 if ((d2 != 0) || (d3 != 0)) {
330 *buf++ = d2 + '0'; /* next digit */
334 q = (d3 * 0xcd) >> 11;
336 *buf++ = d3 + '0'; /* next digit */
338 *buf++ = q + '0'; /* most sign. digit */
345 /* Same with if's removed. Always emits five digits */
346 static char *put_dec_full(char *buf, unsigned q)
348 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
349 /* but anyway, gcc produces better code with full-sized ints */
350 unsigned d3, d2, d1, d0;
356 * Possible ways to approx. divide by 10
357 * gcc -O2 replaces multiply with shifts and adds
358 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
359 * (x * 0x67) >> 10: 1100111
360 * (x * 0x34) >> 9: 110100 - same
361 * (x * 0x1a) >> 8: 11010 - same
362 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
364 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
365 q = (d0 * 0xcd) >> 11;
368 d1 = q + 9*d3 + 5*d2 + d1;
369 q = (d1 * 0xcd) >> 11;
379 q = (d3 * 0xcd) >> 11; /* - shorter code */
380 /* q = (d3 * 0x67) >> 10; - would also work */
387 /* No inlining helps gcc to use registers better */
388 static noinline char *put_dec(char *buf, unsigned long long num)
393 return put_dec_trunc(buf, num);
394 rem = do_div(num, 100000);
395 buf = put_dec_full(buf, rem);
399 #define ZEROPAD 1 /* pad with zero */
400 #define SIGN 2 /* unsigned/signed long */
401 #define PLUS 4 /* show plus */
402 #define SPACE 8 /* space if plus */
403 #define LEFT 16 /* left justified */
404 #define SMALL 32 /* Must be 32 == 0x20 */
405 #define SPECIAL 64 /* 0x */
408 FORMAT_TYPE_NONE, /* Just a string part */
410 FORMAT_TYPE_PRECISION,
414 FORMAT_TYPE_PERCENT_CHAR,
416 FORMAT_TYPE_LONG_LONG,
431 enum format_type type;
432 int flags; /* flags to number() */
433 int field_width; /* width of output field */
435 int precision; /* # of digits/chars */
439 static char *number(char *buf, char *end, unsigned long long num,
440 struct printf_spec spec)
442 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
443 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
448 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
451 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
452 * produces same digits or (maybe lowercased) letters */
453 locase = (spec.flags & SMALL);
454 if (spec.flags & LEFT)
455 spec.flags &= ~ZEROPAD;
457 if (spec.flags & SIGN) {
458 if ((signed long long)num < 0) {
460 num = -(signed long long)num;
462 } else if (spec.flags & PLUS) {
465 } else if (spec.flags & SPACE) {
476 /* generate full string in tmp[], in reverse order */
480 /* Generic code, for any base:
482 tmp[i++] = (digits[do_div(num,base)] | locase);
485 else if (spec.base != 10) { /* 8 or 16 */
486 int mask = spec.base - 1;
492 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
495 } else { /* base 10 */
496 i = put_dec(tmp, num) - tmp;
499 /* printing 100 using %2d gives "100", not "00" */
500 if (i > spec.precision)
502 /* leading space padding */
503 spec.field_width -= spec.precision;
504 if (!(spec.flags & (ZEROPAD+LEFT))) {
505 while (--spec.field_width >= 0) {
517 /* "0x" / "0" prefix */
522 if (spec.base == 16) {
524 *buf = ('X' | locase);
528 /* zero or space padding */
529 if (!(spec.flags & LEFT)) {
530 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
531 while (--spec.field_width >= 0) {
537 /* hmm even more zero padding? */
538 while (i <= --spec.precision) {
543 /* actual digits of result */
549 /* trailing space padding */
550 while (--spec.field_width >= 0) {
559 static char *string(char *buf, char *end, const char *s, struct printf_spec spec)
563 if ((unsigned long)s < PAGE_SIZE)
566 len = strnlen(s, spec.precision);
568 if (!(spec.flags & LEFT)) {
569 while (len < spec.field_width--) {
575 for (i = 0; i < len; ++i) {
580 while (len < spec.field_width--) {
589 static char *symbol_string(char *buf, char *end, void *ptr,
590 struct printf_spec spec, char ext)
592 unsigned long value = (unsigned long) ptr;
593 #ifdef CONFIG_KALLSYMS
594 char sym[KSYM_SYMBOL_LEN];
595 if (ext != 'f' && ext != 's')
596 sprint_symbol(sym, value);
598 kallsyms_lookup(value, NULL, NULL, NULL, sym);
600 return string(buf, end, sym, spec);
602 spec.field_width = 2 * sizeof(void *);
603 spec.flags |= SPECIAL | SMALL | ZEROPAD;
606 return number(buf, end, value, spec);
610 static char *resource_string(char *buf, char *end, struct resource *res,
611 struct printf_spec spec, const char *fmt)
613 #ifndef IO_RSRC_PRINTK_SIZE
614 #define IO_RSRC_PRINTK_SIZE 6
617 #ifndef MEM_RSRC_PRINTK_SIZE
618 #define MEM_RSRC_PRINTK_SIZE 10
620 struct printf_spec hex_spec = {
623 .flags = SPECIAL | SMALL | ZEROPAD,
625 struct printf_spec dec_spec = {
630 struct printf_spec str_spec = {
635 struct printf_spec flag_spec = {
638 .flags = SPECIAL | SMALL,
641 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
642 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
643 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
644 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
645 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref disabled]")
646 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
647 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
648 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
650 char *p = sym, *pend = sym + sizeof(sym);
651 int size = -1, addr = 0;
652 int decode = (fmt[0] == 'R') ? 1 : 0;
654 if (res->flags & IORESOURCE_IO) {
655 size = IO_RSRC_PRINTK_SIZE;
657 } else if (res->flags & IORESOURCE_MEM) {
658 size = MEM_RSRC_PRINTK_SIZE;
663 if (res->flags & IORESOURCE_IO)
664 p = string(p, pend, "io ", str_spec);
665 else if (res->flags & IORESOURCE_MEM)
666 p = string(p, pend, "mem ", str_spec);
667 else if (res->flags & IORESOURCE_IRQ)
668 p = string(p, pend, "irq ", str_spec);
669 else if (res->flags & IORESOURCE_DMA)
670 p = string(p, pend, "dma ", str_spec);
672 p = string(p, pend, "??? ", str_spec);
675 hex_spec.field_width = size;
676 p = number(p, pend, res->start, addr ? hex_spec : dec_spec);
677 if (res->start != res->end) {
679 p = number(p, pend, res->end, addr ? hex_spec : dec_spec);
682 if (res->flags & IORESOURCE_MEM_64)
683 p = string(p, pend, " 64bit", str_spec);
684 if (res->flags & IORESOURCE_PREFETCH)
685 p = string(p, pend, " pref", str_spec);
686 if (res->flags & IORESOURCE_DISABLED)
687 p = string(p, pend, " disabled", str_spec);
689 p = string(p, pend, " flags ", str_spec);
690 p = number(p, pend, res->flags, flag_spec);
695 return string(buf, end, sym, spec);
698 static char *mac_address_string(char *buf, char *end, u8 *addr,
699 struct printf_spec spec, const char *fmt)
701 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
705 for (i = 0; i < 6; i++) {
706 p = pack_hex_byte(p, addr[i]);
707 if (fmt[0] == 'M' && i != 5)
712 return string(buf, end, mac_addr, spec);
715 static char *ip4_string(char *p, const u8 *addr, bool leading_zeros)
719 for (i = 0; i < 4; i++) {
720 char temp[3]; /* hold each IP quad in reverse order */
721 int digits = put_dec_trunc(temp, addr[i]) - temp;
728 /* reverse the digits in the quad */
739 static char *ip6_compressed_string(char *p, const char *addr)
742 unsigned char zerolength[8];
747 bool needcolon = false;
751 memcpy(&in6, addr, sizeof(struct in6_addr));
753 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
755 memset(zerolength, 0, sizeof(zerolength));
762 /* find position of longest 0 run */
763 for (i = 0; i < range; i++) {
764 for (j = i; j < range; j++) {
765 if (in6.s6_addr16[j] != 0)
770 for (i = 0; i < range; i++) {
771 if (zerolength[i] > longest) {
772 longest = zerolength[i];
778 for (i = 0; i < range; i++) {
780 if (needcolon || i == 0)
791 /* hex u16 without leading 0s */
792 word = ntohs(in6.s6_addr16[i]);
797 p = pack_hex_byte(p, hi);
799 *p++ = hex_asc_lo(hi);
802 p = pack_hex_byte(p, lo);
804 *p++ = hex_asc_lo(lo);
811 p = ip4_string(p, &in6.s6_addr[12], false);
818 static char *ip6_string(char *p, const char *addr, const char *fmt)
822 for (i = 0; i < 8; i++) {
823 p = pack_hex_byte(p, *addr++);
824 p = pack_hex_byte(p, *addr++);
825 if (fmt[0] == 'I' && i != 7)
833 static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
834 struct printf_spec spec, const char *fmt)
836 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
838 if (fmt[0] == 'I' && fmt[2] == 'c')
839 ip6_compressed_string(ip6_addr, addr);
841 ip6_string(ip6_addr, addr, fmt);
843 return string(buf, end, ip6_addr, spec);
846 static char *ip4_addr_string(char *buf, char *end, const u8 *addr,
847 struct printf_spec spec, const char *fmt)
849 char ip4_addr[sizeof("255.255.255.255")];
851 ip4_string(ip4_addr, addr, fmt[0] == 'i');
853 return string(buf, end, ip4_addr, spec);
857 * Show a '%p' thing. A kernel extension is that the '%p' is followed
858 * by an extra set of alphanumeric characters that are extended format
861 * Right now we handle:
863 * - 'F' For symbolic function descriptor pointers with offset
864 * - 'f' For simple symbolic function names without offset
865 * - 'S' For symbolic direct pointers with offset
866 * - 's' For symbolic direct pointers without offset
867 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
868 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
869 * - 'M' For a 6-byte MAC address, it prints the address in the
870 * usual colon-separated hex notation
871 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
872 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
873 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
874 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
875 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
876 * IPv6 omits the colons (01020304...0f)
877 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
878 * - 'I6c' for IPv6 addresses printed as specified by
879 * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
880 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
881 * function pointers are really function descriptors, which contain a
882 * pointer to the real address.
884 static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
885 struct printf_spec spec)
888 return string(buf, end, "(null)", spec);
893 ptr = dereference_function_descriptor(ptr);
897 return symbol_string(buf, end, ptr, spec, *fmt);
900 return resource_string(buf, end, ptr, spec, fmt);
901 case 'M': /* Colon separated: 00:01:02:03:04:05 */
902 case 'm': /* Contiguous: 000102030405 */
903 return mac_address_string(buf, end, ptr, spec, fmt);
904 case 'I': /* Formatted IP supported
906 * 6: 0001:0203:...:0708
907 * 6c: 1::708 or 1::1.2.3.4
909 case 'i': /* Contiguous:
915 return ip6_addr_string(buf, end, ptr, spec, fmt);
917 return ip4_addr_string(buf, end, ptr, spec, fmt);
922 if (spec.field_width == -1) {
923 spec.field_width = 2*sizeof(void *);
924 spec.flags |= ZEROPAD;
928 return number(buf, end, (unsigned long) ptr, spec);
932 * Helper function to decode printf style format.
933 * Each call decode a token from the format and return the
934 * number of characters read (or likely the delta where it wants
935 * to go on the next call).
936 * The decoded token is returned through the parameters
938 * 'h', 'l', or 'L' for integer fields
939 * 'z' support added 23/7/1999 S.H.
940 * 'z' changed to 'Z' --davidm 1/25/99
941 * 't' added for ptrdiff_t
943 * @fmt: the format string
944 * @type of the token returned
945 * @flags: various flags such as +, -, # tokens..
946 * @field_width: overwritten width
947 * @base: base of the number (octal, hex, ...)
948 * @precision: precision of a number
949 * @qualifier: qualifier of a number (long, size_t, ...)
951 static int format_decode(const char *fmt, struct printf_spec *spec)
953 const char *start = fmt;
955 /* we finished early by reading the field width */
956 if (spec->type == FORMAT_TYPE_WIDTH) {
957 if (spec->field_width < 0) {
958 spec->field_width = -spec->field_width;
961 spec->type = FORMAT_TYPE_NONE;
965 /* we finished early by reading the precision */
966 if (spec->type == FORMAT_TYPE_PRECISION) {
967 if (spec->precision < 0)
970 spec->type = FORMAT_TYPE_NONE;
975 spec->type = FORMAT_TYPE_NONE;
977 for (; *fmt ; ++fmt) {
982 /* Return the current non-format string */
983 if (fmt != start || !*fmt)
989 while (1) { /* this also skips first '%' */
995 case '-': spec->flags |= LEFT; break;
996 case '+': spec->flags |= PLUS; break;
997 case ' ': spec->flags |= SPACE; break;
998 case '#': spec->flags |= SPECIAL; break;
999 case '0': spec->flags |= ZEROPAD; break;
1000 default: found = false;
1007 /* get field width */
1008 spec->field_width = -1;
1011 spec->field_width = skip_atoi(&fmt);
1012 else if (*fmt == '*') {
1013 /* it's the next argument */
1014 spec->type = FORMAT_TYPE_WIDTH;
1015 return ++fmt - start;
1019 /* get the precision */
1020 spec->precision = -1;
1023 if (isdigit(*fmt)) {
1024 spec->precision = skip_atoi(&fmt);
1025 if (spec->precision < 0)
1026 spec->precision = 0;
1027 } else if (*fmt == '*') {
1028 /* it's the next argument */
1029 spec->type = FORMAT_TYPE_PRECISION;
1030 return ++fmt - start;
1035 /* get the conversion qualifier */
1036 spec->qualifier = -1;
1037 if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
1038 TOLOWER(*fmt) == 'z' || *fmt == 't') {
1039 spec->qualifier = *fmt++;
1040 if (unlikely(spec->qualifier == *fmt)) {
1041 if (spec->qualifier == 'l') {
1042 spec->qualifier = 'L';
1044 } else if (spec->qualifier == 'h') {
1045 spec->qualifier = 'H';
1055 spec->type = FORMAT_TYPE_CHAR;
1056 return ++fmt - start;
1059 spec->type = FORMAT_TYPE_STR;
1060 return ++fmt - start;
1063 spec->type = FORMAT_TYPE_PTR;
1068 spec->type = FORMAT_TYPE_NRCHARS;
1069 return ++fmt - start;
1072 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1073 return ++fmt - start;
1075 /* integer number formats - set up the flags and "break" */
1081 spec->flags |= SMALL;
1089 spec->flags |= SIGN;
1094 spec->type = FORMAT_TYPE_INVALID;
1098 if (spec->qualifier == 'L')
1099 spec->type = FORMAT_TYPE_LONG_LONG;
1100 else if (spec->qualifier == 'l') {
1101 if (spec->flags & SIGN)
1102 spec->type = FORMAT_TYPE_LONG;
1104 spec->type = FORMAT_TYPE_ULONG;
1105 } else if (TOLOWER(spec->qualifier) == 'z') {
1106 spec->type = FORMAT_TYPE_SIZE_T;
1107 } else if (spec->qualifier == 't') {
1108 spec->type = FORMAT_TYPE_PTRDIFF;
1109 } else if (spec->qualifier == 'H') {
1110 if (spec->flags & SIGN)
1111 spec->type = FORMAT_TYPE_BYTE;
1113 spec->type = FORMAT_TYPE_UBYTE;
1114 } else if (spec->qualifier == 'h') {
1115 if (spec->flags & SIGN)
1116 spec->type = FORMAT_TYPE_SHORT;
1118 spec->type = FORMAT_TYPE_USHORT;
1120 if (spec->flags & SIGN)
1121 spec->type = FORMAT_TYPE_INT;
1123 spec->type = FORMAT_TYPE_UINT;
1126 return ++fmt - start;
1130 * vsnprintf - Format a string and place it in a buffer
1131 * @buf: The buffer to place the result into
1132 * @size: The size of the buffer, including the trailing null space
1133 * @fmt: The format string to use
1134 * @args: Arguments for the format string
1136 * This function follows C99 vsnprintf, but has some extensions:
1137 * %pS output the name of a text symbol with offset
1138 * %ps output the name of a text symbol without offset
1139 * %pF output the name of a function pointer with its offset
1140 * %pf output the name of a function pointer without its offset
1141 * %pR output the address range in a struct resource
1144 * The return value is the number of characters which would
1145 * be generated for the given input, excluding the trailing
1146 * '\0', as per ISO C99. If you want to have the exact
1147 * number of characters written into @buf as return value
1148 * (not including the trailing '\0'), use vscnprintf(). If the
1149 * return is greater than or equal to @size, the resulting
1150 * string is truncated.
1152 * Call this function if you are already dealing with a va_list.
1153 * You probably want snprintf() instead.
1155 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1157 unsigned long long num;
1160 struct printf_spec spec = {0};
1162 /* Reject out-of-range values early. Large positive sizes are
1163 used for unknown buffer sizes. */
1164 if (WARN_ON_ONCE((int) size < 0))
1170 /* Make sure end is always >= buf */
1177 const char *old_fmt = fmt;
1179 read = format_decode(fmt, &spec);
1183 switch (spec.type) {
1184 case FORMAT_TYPE_NONE: {
1187 if (copy > end - str)
1189 memcpy(str, old_fmt, copy);
1195 case FORMAT_TYPE_WIDTH:
1196 spec.field_width = va_arg(args, int);
1199 case FORMAT_TYPE_PRECISION:
1200 spec.precision = va_arg(args, int);
1203 case FORMAT_TYPE_CHAR:
1204 if (!(spec.flags & LEFT)) {
1205 while (--spec.field_width > 0) {
1212 c = (unsigned char) va_arg(args, int);
1216 while (--spec.field_width > 0) {
1223 case FORMAT_TYPE_STR:
1224 str = string(str, end, va_arg(args, char *), spec);
1227 case FORMAT_TYPE_PTR:
1228 str = pointer(fmt+1, str, end, va_arg(args, void *),
1230 while (isalnum(*fmt))
1234 case FORMAT_TYPE_PERCENT_CHAR:
1240 case FORMAT_TYPE_INVALID:
1246 case FORMAT_TYPE_NRCHARS: {
1247 int qualifier = spec.qualifier;
1249 if (qualifier == 'l') {
1250 long *ip = va_arg(args, long *);
1252 } else if (TOLOWER(qualifier) == 'z') {
1253 size_t *ip = va_arg(args, size_t *);
1256 int *ip = va_arg(args, int *);
1263 switch (spec.type) {
1264 case FORMAT_TYPE_LONG_LONG:
1265 num = va_arg(args, long long);
1267 case FORMAT_TYPE_ULONG:
1268 num = va_arg(args, unsigned long);
1270 case FORMAT_TYPE_LONG:
1271 num = va_arg(args, long);
1273 case FORMAT_TYPE_SIZE_T:
1274 num = va_arg(args, size_t);
1276 case FORMAT_TYPE_PTRDIFF:
1277 num = va_arg(args, ptrdiff_t);
1279 case FORMAT_TYPE_UBYTE:
1280 num = (unsigned char) va_arg(args, int);
1282 case FORMAT_TYPE_BYTE:
1283 num = (signed char) va_arg(args, int);
1285 case FORMAT_TYPE_USHORT:
1286 num = (unsigned short) va_arg(args, int);
1288 case FORMAT_TYPE_SHORT:
1289 num = (short) va_arg(args, int);
1291 case FORMAT_TYPE_INT:
1292 num = (int) va_arg(args, int);
1295 num = va_arg(args, unsigned int);
1298 str = number(str, end, num, spec);
1309 /* the trailing null byte doesn't count towards the total */
1313 EXPORT_SYMBOL(vsnprintf);
1316 * vscnprintf - Format a string and place it in a buffer
1317 * @buf: The buffer to place the result into
1318 * @size: The size of the buffer, including the trailing null space
1319 * @fmt: The format string to use
1320 * @args: Arguments for the format string
1322 * The return value is the number of characters which have been written into
1323 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1326 * Call this function if you are already dealing with a va_list.
1327 * You probably want scnprintf() instead.
1329 * See the vsnprintf() documentation for format string extensions over C99.
1331 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1335 i = vsnprintf(buf, size, fmt, args);
1337 return (i >= size) ? (size - 1) : i;
1339 EXPORT_SYMBOL(vscnprintf);
1342 * snprintf - Format a string and place it in a buffer
1343 * @buf: The buffer to place the result into
1344 * @size: The size of the buffer, including the trailing null space
1345 * @fmt: The format string to use
1346 * @...: Arguments for the format string
1348 * The return value is the number of characters which would be
1349 * generated for the given input, excluding the trailing null,
1350 * as per ISO C99. If the return is greater than or equal to
1351 * @size, the resulting string is truncated.
1353 * See the vsnprintf() documentation for format string extensions over C99.
1355 int snprintf(char *buf, size_t size, const char *fmt, ...)
1360 va_start(args, fmt);
1361 i = vsnprintf(buf, size, fmt, args);
1366 EXPORT_SYMBOL(snprintf);
1369 * scnprintf - Format a string and place it in a buffer
1370 * @buf: The buffer to place the result into
1371 * @size: The size of the buffer, including the trailing null space
1372 * @fmt: The format string to use
1373 * @...: Arguments for the format string
1375 * The return value is the number of characters written into @buf not including
1376 * the trailing '\0'. If @size is <= 0 the function returns 0.
1379 int scnprintf(char *buf, size_t size, const char *fmt, ...)
1384 va_start(args, fmt);
1385 i = vsnprintf(buf, size, fmt, args);
1388 return (i >= size) ? (size - 1) : i;
1390 EXPORT_SYMBOL(scnprintf);
1393 * vsprintf - Format a string and place it in a buffer
1394 * @buf: The buffer to place the result into
1395 * @fmt: The format string to use
1396 * @args: Arguments for the format string
1398 * The function returns the number of characters written
1399 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1402 * Call this function if you are already dealing with a va_list.
1403 * You probably want sprintf() instead.
1405 * See the vsnprintf() documentation for format string extensions over C99.
1407 int vsprintf(char *buf, const char *fmt, va_list args)
1409 return vsnprintf(buf, INT_MAX, fmt, args);
1411 EXPORT_SYMBOL(vsprintf);
1414 * sprintf - 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 * @...: Arguments for the format string
1419 * The function returns the number of characters written
1420 * into @buf. Use snprintf() or scnprintf() in order to avoid
1423 * See the vsnprintf() documentation for format string extensions over C99.
1425 int sprintf(char *buf, const char *fmt, ...)
1430 va_start(args, fmt);
1431 i = vsnprintf(buf, INT_MAX, fmt, args);
1436 EXPORT_SYMBOL(sprintf);
1438 #ifdef CONFIG_BINARY_PRINTF
1441 * vbin_printf() - VA arguments to binary data
1442 * bstr_printf() - Binary data to text string
1446 * vbin_printf - Parse a format string and place args' binary value in a buffer
1447 * @bin_buf: The buffer to place args' binary value
1448 * @size: The size of the buffer(by words(32bits), not characters)
1449 * @fmt: The format string to use
1450 * @args: Arguments for the format string
1452 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1455 * The return value is the number of words(32bits) which would be generated for
1459 * If the return value is greater than @size, the resulting bin_buf is NOT
1460 * valid for bstr_printf().
1462 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1464 struct printf_spec spec = {0};
1468 str = (char *)bin_buf;
1469 end = (char *)(bin_buf + size);
1471 #define save_arg(type) \
1473 if (sizeof(type) == 8) { \
1474 unsigned long long value; \
1475 str = PTR_ALIGN(str, sizeof(u32)); \
1476 value = va_arg(args, unsigned long long); \
1477 if (str + sizeof(type) <= end) { \
1478 *(u32 *)str = *(u32 *)&value; \
1479 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1482 unsigned long value; \
1483 str = PTR_ALIGN(str, sizeof(type)); \
1484 value = va_arg(args, int); \
1485 if (str + sizeof(type) <= end) \
1486 *(typeof(type) *)str = (type)value; \
1488 str += sizeof(type); \
1492 read = format_decode(fmt, &spec);
1496 switch (spec.type) {
1497 case FORMAT_TYPE_NONE:
1500 case FORMAT_TYPE_WIDTH:
1501 case FORMAT_TYPE_PRECISION:
1505 case FORMAT_TYPE_CHAR:
1509 case FORMAT_TYPE_STR: {
1510 const char *save_str = va_arg(args, char *);
1513 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1514 || (unsigned long)save_str < PAGE_SIZE)
1515 save_str = "(null)";
1516 len = strlen(save_str) + 1;
1517 if (str + len < end)
1518 memcpy(str, save_str, len);
1523 case FORMAT_TYPE_PTR:
1525 /* skip all alphanumeric pointer suffixes */
1526 while (isalnum(*fmt))
1530 case FORMAT_TYPE_PERCENT_CHAR:
1533 case FORMAT_TYPE_INVALID:
1536 case FORMAT_TYPE_NRCHARS: {
1537 /* skip %n 's argument */
1538 int qualifier = spec.qualifier;
1540 if (qualifier == 'l')
1541 skip_arg = va_arg(args, long *);
1542 else if (TOLOWER(qualifier) == 'z')
1543 skip_arg = va_arg(args, size_t *);
1545 skip_arg = va_arg(args, int *);
1550 switch (spec.type) {
1552 case FORMAT_TYPE_LONG_LONG:
1553 save_arg(long long);
1555 case FORMAT_TYPE_ULONG:
1556 case FORMAT_TYPE_LONG:
1557 save_arg(unsigned long);
1559 case FORMAT_TYPE_SIZE_T:
1562 case FORMAT_TYPE_PTRDIFF:
1563 save_arg(ptrdiff_t);
1565 case FORMAT_TYPE_UBYTE:
1566 case FORMAT_TYPE_BYTE:
1569 case FORMAT_TYPE_USHORT:
1570 case FORMAT_TYPE_SHORT:
1579 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1582 EXPORT_SYMBOL_GPL(vbin_printf);
1585 * bstr_printf - Format a string from binary arguments and place it in a buffer
1586 * @buf: The buffer to place the result into
1587 * @size: The size of the buffer, including the trailing null space
1588 * @fmt: The format string to use
1589 * @bin_buf: Binary arguments for the format string
1591 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1592 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1593 * a binary buffer that generated by vbin_printf.
1595 * The format follows C99 vsnprintf, but has some extensions:
1596 * see vsnprintf comment for details.
1598 * The return value is the number of characters which would
1599 * be generated for the given input, excluding the trailing
1600 * '\0', as per ISO C99. If you want to have the exact
1601 * number of characters written into @buf as return value
1602 * (not including the trailing '\0'), use vscnprintf(). If the
1603 * return is greater than or equal to @size, the resulting
1604 * string is truncated.
1606 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1608 unsigned long long num;
1610 const char *args = (const char *)bin_buf;
1611 struct printf_spec spec = {0};
1613 if (WARN_ON_ONCE((int) size < 0))
1619 #define get_arg(type) \
1621 typeof(type) value; \
1622 if (sizeof(type) == 8) { \
1623 args = PTR_ALIGN(args, sizeof(u32)); \
1624 *(u32 *)&value = *(u32 *)args; \
1625 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1627 args = PTR_ALIGN(args, sizeof(type)); \
1628 value = *(typeof(type) *)args; \
1630 args += sizeof(type); \
1634 /* Make sure end is always >= buf */
1642 const char *old_fmt = fmt;
1644 read = format_decode(fmt, &spec);
1648 switch (spec.type) {
1649 case FORMAT_TYPE_NONE: {
1652 if (copy > end - str)
1654 memcpy(str, old_fmt, copy);
1660 case FORMAT_TYPE_WIDTH:
1661 spec.field_width = get_arg(int);
1664 case FORMAT_TYPE_PRECISION:
1665 spec.precision = get_arg(int);
1668 case FORMAT_TYPE_CHAR:
1669 if (!(spec.flags & LEFT)) {
1670 while (--spec.field_width > 0) {
1676 c = (unsigned char) get_arg(char);
1680 while (--spec.field_width > 0) {
1687 case FORMAT_TYPE_STR: {
1688 const char *str_arg = args;
1689 size_t len = strlen(str_arg);
1691 str = string(str, end, (char *)str_arg, spec);
1695 case FORMAT_TYPE_PTR:
1696 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1697 while (isalnum(*fmt))
1701 case FORMAT_TYPE_PERCENT_CHAR:
1707 case FORMAT_TYPE_INVALID:
1713 case FORMAT_TYPE_NRCHARS:
1718 switch (spec.type) {
1720 case FORMAT_TYPE_LONG_LONG:
1721 num = get_arg(long long);
1723 case FORMAT_TYPE_ULONG:
1724 num = get_arg(unsigned long);
1726 case FORMAT_TYPE_LONG:
1727 num = get_arg(unsigned long);
1729 case FORMAT_TYPE_SIZE_T:
1730 num = get_arg(size_t);
1732 case FORMAT_TYPE_PTRDIFF:
1733 num = get_arg(ptrdiff_t);
1735 case FORMAT_TYPE_UBYTE:
1736 num = get_arg(unsigned char);
1738 case FORMAT_TYPE_BYTE:
1739 num = get_arg(signed char);
1741 case FORMAT_TYPE_USHORT:
1742 num = get_arg(unsigned short);
1744 case FORMAT_TYPE_SHORT:
1745 num = get_arg(short);
1747 case FORMAT_TYPE_UINT:
1748 num = get_arg(unsigned int);
1754 str = number(str, end, num, spec);
1767 /* the trailing null byte doesn't count towards the total */
1770 EXPORT_SYMBOL_GPL(bstr_printf);
1773 * bprintf - Parse a format string and place args' binary value in a buffer
1774 * @bin_buf: The buffer to place args' binary value
1775 * @size: The size of the buffer(by words(32bits), not characters)
1776 * @fmt: The format string to use
1777 * @...: Arguments for the format string
1779 * The function returns the number of words(u32) written
1782 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1787 va_start(args, fmt);
1788 ret = vbin_printf(bin_buf, size, fmt, args);
1793 EXPORT_SYMBOL_GPL(bprintf);
1795 #endif /* CONFIG_BINARY_PRINTF */
1798 * vsscanf - Unformat a buffer into a list of arguments
1799 * @buf: input buffer
1800 * @fmt: format of buffer
1803 int vsscanf(const char *buf, const char *fmt, va_list args)
1805 const char *str = buf;
1809 int qualifier, base, field_width;
1812 while (*fmt && *str) {
1813 /* skip any white space in format */
1814 /* white space in format matchs any amount of
1815 * white space, including none, in the input.
1817 if (isspace(*fmt)) {
1818 while (isspace(*fmt))
1820 while (isspace(*str))
1824 /* anything that is not a conversion must match exactly */
1825 if (*fmt != '%' && *fmt) {
1826 if (*fmt++ != *str++)
1835 /* skip this conversion.
1836 * advance both strings to next white space
1839 while (!isspace(*fmt) && *fmt != '%' && *fmt)
1841 while (!isspace(*str) && *str)
1846 /* get field width */
1849 field_width = skip_atoi(&fmt);
1851 /* get conversion qualifier */
1853 if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
1854 TOLOWER(*fmt) == 'z') {
1856 if (unlikely(qualifier == *fmt)) {
1857 if (qualifier == 'h') {
1860 } else if (qualifier == 'l') {
1875 char *s = (char *)va_arg(args, char*);
1876 if (field_width == -1)
1880 } while (--field_width > 0 && *str);
1886 char *s = (char *)va_arg(args, char *);
1887 if (field_width == -1)
1888 field_width = INT_MAX;
1889 /* first, skip leading white space in buffer */
1890 while (isspace(*str))
1893 /* now copy until next white space */
1894 while (*str && !isspace(*str) && field_width--)
1901 /* return number of characters read so far */
1903 int *i = (int *)va_arg(args, int*);
1921 /* looking for '%' in str */
1926 /* invalid format; stop here */
1930 /* have some sort of integer conversion.
1931 * first, skip white space in buffer.
1933 while (isspace(*str))
1937 if (is_sign && digit == '-')
1941 || (base == 16 && !isxdigit(digit))
1942 || (base == 10 && !isdigit(digit))
1943 || (base == 8 && (!isdigit(digit) || digit > '7'))
1944 || (base == 0 && !isdigit(digit)))
1947 switch (qualifier) {
1948 case 'H': /* that's 'hh' in format */
1950 signed char *s = (signed char *)va_arg(args, signed char *);
1951 *s = (signed char)simple_strtol(str, &next, base);
1953 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
1954 *s = (unsigned char)simple_strtoul(str, &next, base);
1959 short *s = (short *)va_arg(args, short *);
1960 *s = (short)simple_strtol(str, &next, base);
1962 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
1963 *s = (unsigned short)simple_strtoul(str, &next, base);
1968 long *l = (long *)va_arg(args, long *);
1969 *l = simple_strtol(str, &next, base);
1971 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
1972 *l = simple_strtoul(str, &next, base);
1977 long long *l = (long long *)va_arg(args, long long *);
1978 *l = simple_strtoll(str, &next, base);
1980 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
1981 *l = simple_strtoull(str, &next, base);
1987 size_t *s = (size_t *)va_arg(args, size_t *);
1988 *s = (size_t)simple_strtoul(str, &next, base);
1993 int *i = (int *)va_arg(args, int *);
1994 *i = (int)simple_strtol(str, &next, base);
1996 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
1997 *i = (unsigned int)simple_strtoul(str, &next, base);
2009 * Now we've come all the way through so either the input string or the
2010 * format ended. In the former case, there can be a %n at the current
2011 * position in the format that needs to be filled.
2013 if (*fmt == '%' && *(fmt + 1) == 'n') {
2014 int *p = (int *)va_arg(args, int *);
2020 EXPORT_SYMBOL(vsscanf);
2023 * sscanf - Unformat a buffer into a list of arguments
2024 * @buf: input buffer
2025 * @fmt: formatting of buffer
2026 * @...: resulting arguments
2028 int sscanf(const char *buf, const char *fmt, ...)
2033 va_start(args, fmt);
2034 i = vsscanf(buf, fmt, args);
2039 EXPORT_SYMBOL(sscanf);