xref: /openbmc/linux/lib/vsprintf.c (revision 5e0579812834ab7fa072db4a15ebdff68d62e2e7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/lib/vsprintf.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds  * Wirzenius wrote this portably, Torvalds fucked it up :-)
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /*
131da177e4SLinus Torvalds  * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
141da177e4SLinus Torvalds  * - changed to provide snprintf and vsnprintf functions
151da177e4SLinus Torvalds  * So Feb  1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
161da177e4SLinus Torvalds  * - scnprintf and vscnprintf
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <stdarg.h>
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/types.h>
221da177e4SLinus Torvalds #include <linux/string.h>
231da177e4SLinus Torvalds #include <linux/ctype.h>
241da177e4SLinus Torvalds #include <linux/kernel.h>
250fe1ef24SLinus Torvalds #include <linux/kallsyms.h>
260fe1ef24SLinus Torvalds #include <linux/uaccess.h>
27332d2e78SLinus Torvalds #include <linux/ioport.h>
288a27f7c9SJoe Perches #include <net/addrconf.h>
291da177e4SLinus Torvalds 
304e57b681STim Schmielau #include <asm/page.h>		/* for PAGE_SIZE */
311da177e4SLinus Torvalds #include <asm/div64.h>
32deac93dfSJames Bottomley #include <asm/sections.h>	/* for dereference_function_descriptor() */
331da177e4SLinus Torvalds 
349b706aeeSDenys Vlasenko /* Works only for digits and letters, but small and fast */
359b706aeeSDenys Vlasenko #define TOLOWER(x) ((x) | 0x20)
369b706aeeSDenys Vlasenko 
37aa46a63eSHarvey Harrison static unsigned int simple_guess_base(const char *cp)
38aa46a63eSHarvey Harrison {
39aa46a63eSHarvey Harrison 	if (cp[0] == '0') {
40aa46a63eSHarvey Harrison 		if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
41aa46a63eSHarvey Harrison 			return 16;
42aa46a63eSHarvey Harrison 		else
43aa46a63eSHarvey Harrison 			return 8;
44aa46a63eSHarvey Harrison 	} else {
45aa46a63eSHarvey Harrison 		return 10;
46aa46a63eSHarvey Harrison 	}
47aa46a63eSHarvey Harrison }
48aa46a63eSHarvey Harrison 
491da177e4SLinus Torvalds /**
501da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
511da177e4SLinus Torvalds  * @cp: The start of the string
521da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
531da177e4SLinus Torvalds  * @base: The number base to use
541da177e4SLinus Torvalds  */
551da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
561da177e4SLinus Torvalds {
57aa46a63eSHarvey Harrison 	unsigned long long result = 0;
581da177e4SLinus Torvalds 
59aa46a63eSHarvey Harrison 	if (!base)
60aa46a63eSHarvey Harrison 		base = simple_guess_base(cp);
61aa46a63eSHarvey Harrison 
62aa46a63eSHarvey Harrison 	if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
631da177e4SLinus Torvalds 		cp += 2;
64aa46a63eSHarvey Harrison 
65aa46a63eSHarvey Harrison 	while (isxdigit(*cp)) {
66aa46a63eSHarvey Harrison 		unsigned int value;
67aa46a63eSHarvey Harrison 
68aa46a63eSHarvey Harrison 		value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
69aa46a63eSHarvey Harrison 		if (value >= base)
70aa46a63eSHarvey Harrison 			break;
711da177e4SLinus Torvalds 		result = result * base + value;
721da177e4SLinus Torvalds 		cp++;
731da177e4SLinus Torvalds 	}
741da177e4SLinus Torvalds 	if (endp)
751da177e4SLinus Torvalds 		*endp = (char *)cp;
767b9186f5SAndré Goddard Rosa 
771da177e4SLinus Torvalds 	return result;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /**
82922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
83922ac25cSAndré Goddard Rosa  * @cp: The start of the string
84922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
85922ac25cSAndré Goddard Rosa  * @base: The number base to use
86922ac25cSAndré Goddard Rosa  */
87922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
88922ac25cSAndré Goddard Rosa {
89922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
90922ac25cSAndré Goddard Rosa }
91922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
92922ac25cSAndré Goddard Rosa 
93922ac25cSAndré Goddard Rosa /**
94922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
95922ac25cSAndré Goddard Rosa  * @cp: The start of the string
96922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
97922ac25cSAndré Goddard Rosa  * @base: The number base to use
98922ac25cSAndré Goddard Rosa  */
99922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
100922ac25cSAndré Goddard Rosa {
101922ac25cSAndré Goddard Rosa 	if (*cp == '-')
102922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
103922ac25cSAndré Goddard Rosa 
104922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
105922ac25cSAndré Goddard Rosa }
106922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
107922ac25cSAndré Goddard Rosa 
108922ac25cSAndré Goddard Rosa /**
1091da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
1101da177e4SLinus Torvalds  * @cp: The start of the string
1111da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
1121da177e4SLinus Torvalds  * @base: The number base to use
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	if (*cp == '-')
1171da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1187b9186f5SAndré Goddard Rosa 
1191da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1201da177e4SLinus Torvalds }
12198d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1221da177e4SLinus Torvalds 
12306b2a76dSYi Yang /**
12406b2a76dSYi Yang  * strict_strtoul - convert a string to an unsigned long strictly
12506b2a76dSYi Yang  * @cp: The string to be converted
12606b2a76dSYi Yang  * @base: The number base to use
12706b2a76dSYi Yang  * @res: The converted result value
12806b2a76dSYi Yang  *
12906b2a76dSYi Yang  * strict_strtoul converts a string to an unsigned long only if the
13006b2a76dSYi Yang  * string is really an unsigned long string, any string containing
13106b2a76dSYi Yang  * any invalid char at the tail will be rejected and -EINVAL is returned,
13206b2a76dSYi Yang  * only a newline char at the tail is acceptible because people generally
13306b2a76dSYi Yang  * change a module parameter in the following way:
13406b2a76dSYi Yang  *
13506b2a76dSYi Yang  * 	echo 1024 > /sys/module/e1000/parameters/copybreak
13606b2a76dSYi Yang  *
13706b2a76dSYi Yang  * echo will append a newline to the tail.
13806b2a76dSYi Yang  *
13906b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
14006b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
14106b2a76dSYi Yang  *
14206b2a76dSYi Yang  * simple_strtoul just ignores the successive invalid characters and
14306b2a76dSYi Yang  * return the converted value of prefix part of the string.
14406b2a76dSYi Yang  */
1459d85db22SHarvey Harrison int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
1469d85db22SHarvey Harrison {
1479d85db22SHarvey Harrison 	char *tail;
1489d85db22SHarvey Harrison 	unsigned long val;
1499d85db22SHarvey Harrison 
1509d85db22SHarvey Harrison 	*res = 0;
151559b140aSMichal Nazarewicz 	if (!*cp)
1529d85db22SHarvey Harrison 		return -EINVAL;
1539d85db22SHarvey Harrison 
1549d85db22SHarvey Harrison 	val = simple_strtoul(cp, &tail, base);
155e899aa82SPavel Machek 	if (tail == cp)
156e899aa82SPavel Machek 		return -EINVAL;
1577b9186f5SAndré Goddard Rosa 
158559b140aSMichal Nazarewicz 	if ((tail[0] == '\0') || (tail[0] == '\n' && tail[1] == '\0')) {
1599d85db22SHarvey Harrison 		*res = val;
1609d85db22SHarvey Harrison 		return 0;
1619d85db22SHarvey Harrison 	}
1629d85db22SHarvey Harrison 
1639d85db22SHarvey Harrison 	return -EINVAL;
1649d85db22SHarvey Harrison }
1659d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoul);
16606b2a76dSYi Yang 
16706b2a76dSYi Yang /**
16806b2a76dSYi Yang  * strict_strtol - convert a string to a long strictly
16906b2a76dSYi Yang  * @cp: The string to be converted
17006b2a76dSYi Yang  * @base: The number base to use
17106b2a76dSYi Yang  * @res: The converted result value
17206b2a76dSYi Yang  *
17306b2a76dSYi Yang  * strict_strtol is similiar to strict_strtoul, but it allows the first
17406b2a76dSYi Yang  * character of a string is '-'.
17506b2a76dSYi Yang  *
17606b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
17706b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
17806b2a76dSYi Yang  */
1799d85db22SHarvey Harrison int strict_strtol(const char *cp, unsigned int base, long *res)
1809d85db22SHarvey Harrison {
1819d85db22SHarvey Harrison 	int ret;
1829d85db22SHarvey Harrison 	if (*cp == '-') {
1839d85db22SHarvey Harrison 		ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
1849d85db22SHarvey Harrison 		if (!ret)
1859d85db22SHarvey Harrison 			*res = -(*res);
1869d85db22SHarvey Harrison 	} else {
1879d85db22SHarvey Harrison 		ret = strict_strtoul(cp, base, (unsigned long *)res);
1889d85db22SHarvey Harrison 	}
1899d85db22SHarvey Harrison 
1909d85db22SHarvey Harrison 	return ret;
1919d85db22SHarvey Harrison }
1929d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtol);
19306b2a76dSYi Yang 
19406b2a76dSYi Yang /**
19506b2a76dSYi Yang  * strict_strtoull - convert a string to an unsigned long long strictly
19606b2a76dSYi Yang  * @cp: The string to be converted
19706b2a76dSYi Yang  * @base: The number base to use
19806b2a76dSYi Yang  * @res: The converted result value
19906b2a76dSYi Yang  *
20006b2a76dSYi Yang  * strict_strtoull converts a string to an unsigned long long only if the
20106b2a76dSYi Yang  * string is really an unsigned long long string, any string containing
20206b2a76dSYi Yang  * any invalid char at the tail will be rejected and -EINVAL is returned,
20306b2a76dSYi Yang  * only a newline char at the tail is acceptible because people generally
20406b2a76dSYi Yang  * change a module parameter in the following way:
20506b2a76dSYi Yang  *
20606b2a76dSYi Yang  * 	echo 1024 > /sys/module/e1000/parameters/copybreak
20706b2a76dSYi Yang  *
20806b2a76dSYi Yang  * echo will append a newline to the tail of the string.
20906b2a76dSYi Yang  *
21006b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
21106b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
21206b2a76dSYi Yang  *
21306b2a76dSYi Yang  * simple_strtoull just ignores the successive invalid characters and
21406b2a76dSYi Yang  * return the converted value of prefix part of the string.
21506b2a76dSYi Yang  */
2169d85db22SHarvey Harrison int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
2179d85db22SHarvey Harrison {
2189d85db22SHarvey Harrison 	char *tail;
2199d85db22SHarvey Harrison 	unsigned long long val;
2209d85db22SHarvey Harrison 
2219d85db22SHarvey Harrison 	*res = 0;
222559b140aSMichal Nazarewicz 	if (!*cp)
2239d85db22SHarvey Harrison 		return -EINVAL;
2249d85db22SHarvey Harrison 
2259d85db22SHarvey Harrison 	val = simple_strtoull(cp, &tail, base);
226e899aa82SPavel Machek 	if (tail == cp)
227e899aa82SPavel Machek 		return -EINVAL;
228559b140aSMichal Nazarewicz 	if ((tail[0] == '\0') || (tail[0] == '\n' && tail[1] == '\0')) {
2299d85db22SHarvey Harrison 		*res = val;
2309d85db22SHarvey Harrison 		return 0;
2319d85db22SHarvey Harrison 	}
2329d85db22SHarvey Harrison 
2339d85db22SHarvey Harrison 	return -EINVAL;
2349d85db22SHarvey Harrison }
2359d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoull);
23606b2a76dSYi Yang 
23706b2a76dSYi Yang /**
23806b2a76dSYi Yang  * strict_strtoll - convert a string to a long long strictly
23906b2a76dSYi Yang  * @cp: The string to be converted
24006b2a76dSYi Yang  * @base: The number base to use
24106b2a76dSYi Yang  * @res: The converted result value
24206b2a76dSYi Yang  *
24306b2a76dSYi Yang  * strict_strtoll is similiar to strict_strtoull, but it allows the first
24406b2a76dSYi Yang  * character of a string is '-'.
24506b2a76dSYi Yang  *
24606b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
24706b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
24806b2a76dSYi Yang  */
2499d85db22SHarvey Harrison int strict_strtoll(const char *cp, unsigned int base, long long *res)
2509d85db22SHarvey Harrison {
2519d85db22SHarvey Harrison 	int ret;
2529d85db22SHarvey Harrison 	if (*cp == '-') {
2539d85db22SHarvey Harrison 		ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
2549d85db22SHarvey Harrison 		if (!ret)
2559d85db22SHarvey Harrison 			*res = -(*res);
2569d85db22SHarvey Harrison 	} else {
2579d85db22SHarvey Harrison 		ret = strict_strtoull(cp, base, (unsigned long long *)res);
2589d85db22SHarvey Harrison 	}
25906b2a76dSYi Yang 
2609d85db22SHarvey Harrison 	return ret;
2619d85db22SHarvey Harrison }
26206b2a76dSYi Yang EXPORT_SYMBOL(strict_strtoll);
26306b2a76dSYi Yang 
264cf3b429bSJoe Perches static noinline_for_stack
265cf3b429bSJoe Perches int skip_atoi(const char **s)
2661da177e4SLinus Torvalds {
2671da177e4SLinus Torvalds 	int i = 0;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	while (isdigit(**s))
2701da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
2717b9186f5SAndré Goddard Rosa 
2721da177e4SLinus Torvalds 	return i;
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2754277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used
2764277eeddSDenis Vlasenko  * for /proc and /sys data. This directly impacts e.g. top performance
2774277eeddSDenis Vlasenko  * with many processes running. We optimize it for speed
2784277eeddSDenis Vlasenko  * using code from
2794277eeddSDenis Vlasenko  * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
2804277eeddSDenis Vlasenko  * (with permission from the author, Douglas W. Jones). */
2814277eeddSDenis Vlasenko 
2824277eeddSDenis Vlasenko /* Formats correctly any integer in [0,99999].
2834277eeddSDenis Vlasenko  * Outputs from one to five digits depending on input.
2844277eeddSDenis Vlasenko  * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
285cf3b429bSJoe Perches static noinline_for_stack
286cf3b429bSJoe Perches char *put_dec_trunc(char *buf, unsigned q)
2874277eeddSDenis Vlasenko {
2884277eeddSDenis Vlasenko 	unsigned d3, d2, d1, d0;
2894277eeddSDenis Vlasenko 	d1 = (q>>4) & 0xf;
2904277eeddSDenis Vlasenko 	d2 = (q>>8) & 0xf;
2914277eeddSDenis Vlasenko 	d3 = (q>>12);
2924277eeddSDenis Vlasenko 
2934277eeddSDenis Vlasenko 	d0 = 6*(d3 + d2 + d1) + (q & 0xf);
2944277eeddSDenis Vlasenko 	q = (d0 * 0xcd) >> 11;
2954277eeddSDenis Vlasenko 	d0 = d0 - 10*q;
2964277eeddSDenis Vlasenko 	*buf++ = d0 + '0'; /* least significant digit */
2974277eeddSDenis Vlasenko 	d1 = q + 9*d3 + 5*d2 + d1;
2984277eeddSDenis Vlasenko 	if (d1 != 0) {
2994277eeddSDenis Vlasenko 		q = (d1 * 0xcd) >> 11;
3004277eeddSDenis Vlasenko 		d1 = d1 - 10*q;
3014277eeddSDenis Vlasenko 		*buf++ = d1 + '0'; /* next digit */
3024277eeddSDenis Vlasenko 
3034277eeddSDenis Vlasenko 		d2 = q + 2*d2;
3044277eeddSDenis Vlasenko 		if ((d2 != 0) || (d3 != 0)) {
3054277eeddSDenis Vlasenko 			q = (d2 * 0xd) >> 7;
3064277eeddSDenis Vlasenko 			d2 = d2 - 10*q;
3074277eeddSDenis Vlasenko 			*buf++ = d2 + '0'; /* next digit */
3084277eeddSDenis Vlasenko 
3094277eeddSDenis Vlasenko 			d3 = q + 4*d3;
3104277eeddSDenis Vlasenko 			if (d3 != 0) {
3114277eeddSDenis Vlasenko 				q = (d3 * 0xcd) >> 11;
3124277eeddSDenis Vlasenko 				d3 = d3 - 10*q;
3134277eeddSDenis Vlasenko 				*buf++ = d3 + '0';  /* next digit */
3144277eeddSDenis Vlasenko 				if (q != 0)
3154277eeddSDenis Vlasenko 					*buf++ = q + '0'; /* most sign. digit */
3164277eeddSDenis Vlasenko 			}
3174277eeddSDenis Vlasenko 		}
3184277eeddSDenis Vlasenko 	}
3197b9186f5SAndré Goddard Rosa 
3204277eeddSDenis Vlasenko 	return buf;
3214277eeddSDenis Vlasenko }
3224277eeddSDenis Vlasenko /* Same with if's removed. Always emits five digits */
323cf3b429bSJoe Perches static noinline_for_stack
324cf3b429bSJoe Perches char *put_dec_full(char *buf, unsigned q)
3254277eeddSDenis Vlasenko {
3264277eeddSDenis Vlasenko 	/* BTW, if q is in [0,9999], 8-bit ints will be enough, */
3274277eeddSDenis Vlasenko 	/* but anyway, gcc produces better code with full-sized ints */
3284277eeddSDenis Vlasenko 	unsigned d3, d2, d1, d0;
3294277eeddSDenis Vlasenko 	d1 = (q>>4) & 0xf;
3304277eeddSDenis Vlasenko 	d2 = (q>>8) & 0xf;
3314277eeddSDenis Vlasenko 	d3 = (q>>12);
3324277eeddSDenis Vlasenko 
3337b9186f5SAndré Goddard Rosa 	/*
3347b9186f5SAndré Goddard Rosa 	 * Possible ways to approx. divide by 10
3357b9186f5SAndré Goddard Rosa 	 * gcc -O2 replaces multiply with shifts and adds
3367b9186f5SAndré Goddard Rosa 	 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
3377b9186f5SAndré Goddard Rosa 	 * (x * 0x67) >> 10:  1100111
3387b9186f5SAndré Goddard Rosa 	 * (x * 0x34) >> 9:    110100 - same
3397b9186f5SAndré Goddard Rosa 	 * (x * 0x1a) >> 8:     11010 - same
3407b9186f5SAndré Goddard Rosa 	 * (x * 0x0d) >> 7:      1101 - same, shortest code (on i386)
3417b9186f5SAndré Goddard Rosa 	 */
3424277eeddSDenis Vlasenko 	d0 = 6*(d3 + d2 + d1) + (q & 0xf);
3434277eeddSDenis Vlasenko 	q = (d0 * 0xcd) >> 11;
3444277eeddSDenis Vlasenko 	d0 = d0 - 10*q;
3454277eeddSDenis Vlasenko 	*buf++ = d0 + '0';
3464277eeddSDenis Vlasenko 	d1 = q + 9*d3 + 5*d2 + d1;
3474277eeddSDenis Vlasenko 		q = (d1 * 0xcd) >> 11;
3484277eeddSDenis Vlasenko 		d1 = d1 - 10*q;
3494277eeddSDenis Vlasenko 		*buf++ = d1 + '0';
3504277eeddSDenis Vlasenko 
3514277eeddSDenis Vlasenko 		d2 = q + 2*d2;
3524277eeddSDenis Vlasenko 			q = (d2 * 0xd) >> 7;
3534277eeddSDenis Vlasenko 			d2 = d2 - 10*q;
3544277eeddSDenis Vlasenko 			*buf++ = d2 + '0';
3554277eeddSDenis Vlasenko 
3564277eeddSDenis Vlasenko 			d3 = q + 4*d3;
3574277eeddSDenis Vlasenko 				q = (d3 * 0xcd) >> 11; /* - shorter code */
3584277eeddSDenis Vlasenko 				/* q = (d3 * 0x67) >> 10; - would also work */
3594277eeddSDenis Vlasenko 				d3 = d3 - 10*q;
3604277eeddSDenis Vlasenko 				*buf++ = d3 + '0';
3614277eeddSDenis Vlasenko 					*buf++ = q + '0';
3627b9186f5SAndré Goddard Rosa 
3634277eeddSDenis Vlasenko 	return buf;
3644277eeddSDenis Vlasenko }
3654277eeddSDenis Vlasenko /* No inlining helps gcc to use registers better */
366cf3b429bSJoe Perches static noinline_for_stack
367cf3b429bSJoe Perches char *put_dec(char *buf, unsigned long long num)
3684277eeddSDenis Vlasenko {
3694277eeddSDenis Vlasenko 	while (1) {
3704277eeddSDenis Vlasenko 		unsigned rem;
3714277eeddSDenis Vlasenko 		if (num < 100000)
3724277eeddSDenis Vlasenko 			return put_dec_trunc(buf, num);
3734277eeddSDenis Vlasenko 		rem = do_div(num, 100000);
3744277eeddSDenis Vlasenko 		buf = put_dec_full(buf, rem);
3754277eeddSDenis Vlasenko 	}
3764277eeddSDenis Vlasenko }
3774277eeddSDenis Vlasenko 
3781da177e4SLinus Torvalds #define ZEROPAD	1		/* pad with zero */
3791da177e4SLinus Torvalds #define SIGN	2		/* unsigned/signed long */
3801da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3811da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
3821da177e4SLinus Torvalds #define LEFT	16		/* left justified */
383b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
384b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
3851da177e4SLinus Torvalds 
386fef20d9cSFrederic Weisbecker enum format_type {
387fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
388ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
389fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
390fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
391fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
392fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
393fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
394fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
395fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
396fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
397fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
398a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
399a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
400fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
401fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
402fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
403fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
404fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NRCHARS,
405fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
406fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
407fef20d9cSFrederic Weisbecker };
408fef20d9cSFrederic Weisbecker 
409fef20d9cSFrederic Weisbecker struct printf_spec {
4104e310fdaSJoe Perches 	u8	type;		/* format_type enum */
411ef0658f3SJoe Perches 	u8	flags;		/* flags to number() */
4124e310fdaSJoe Perches 	u8	base;		/* number base, 8, 10 or 16 only */
4134e310fdaSJoe Perches 	u8	qualifier;	/* number qualifier, one of 'hHlLtzZ' */
4144e310fdaSJoe Perches 	s16	field_width;	/* width of output field */
4154e310fdaSJoe Perches 	s16	precision;	/* # of digits/chars */
416fef20d9cSFrederic Weisbecker };
417fef20d9cSFrederic Weisbecker 
418cf3b429bSJoe Perches static noinline_for_stack
419cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
420fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
4211da177e4SLinus Torvalds {
4229b706aeeSDenys Vlasenko 	/* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
4239b706aeeSDenys Vlasenko 	static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
4249b706aeeSDenys Vlasenko 
4259b706aeeSDenys Vlasenko 	char tmp[66];
4269b706aeeSDenys Vlasenko 	char sign;
4279b706aeeSDenys Vlasenko 	char locase;
428fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
4291da177e4SLinus Torvalds 	int i;
4301da177e4SLinus Torvalds 
4319b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
4329b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
433fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
434fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
435fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
4361da177e4SLinus Torvalds 	sign = 0;
437fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4381da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4391da177e4SLinus Torvalds 			sign = '-';
4401da177e4SLinus Torvalds 			num = -(signed long long)num;
441fef20d9cSFrederic Weisbecker 			spec.field_width--;
442fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4431da177e4SLinus Torvalds 			sign = '+';
444fef20d9cSFrederic Weisbecker 			spec.field_width--;
445fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4461da177e4SLinus Torvalds 			sign = ' ';
447fef20d9cSFrederic Weisbecker 			spec.field_width--;
4481da177e4SLinus Torvalds 		}
4491da177e4SLinus Torvalds 	}
450b39a7340SDenis Vlasenko 	if (need_pfx) {
451fef20d9cSFrederic Weisbecker 		spec.field_width--;
452fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
453fef20d9cSFrederic Weisbecker 			spec.field_width--;
4541da177e4SLinus Torvalds 	}
455b39a7340SDenis Vlasenko 
456b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4571da177e4SLinus Torvalds 	i = 0;
4581da177e4SLinus Torvalds 	if (num == 0)
4591da177e4SLinus Torvalds 		tmp[i++] = '0';
4604277eeddSDenis Vlasenko 	/* Generic code, for any base:
4614277eeddSDenis Vlasenko 	else do {
4629b706aeeSDenys Vlasenko 		tmp[i++] = (digits[do_div(num,base)] | locase);
4634277eeddSDenis Vlasenko 	} while (num != 0);
4644277eeddSDenis Vlasenko 	*/
465fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
466fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
467b39a7340SDenis Vlasenko 		int shift = 3;
4687b9186f5SAndré Goddard Rosa 
4697b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4707b9186f5SAndré Goddard Rosa 			shift = 4;
471b39a7340SDenis Vlasenko 		do {
4729b706aeeSDenys Vlasenko 			tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
473b39a7340SDenis Vlasenko 			num >>= shift;
474b39a7340SDenis Vlasenko 		} while (num);
4754277eeddSDenis Vlasenko 	} else { /* base 10 */
4764277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4774277eeddSDenis Vlasenko 	}
478b39a7340SDenis Vlasenko 
479b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
480fef20d9cSFrederic Weisbecker 	if (i > spec.precision)
481fef20d9cSFrederic Weisbecker 		spec.precision = i;
482b39a7340SDenis Vlasenko 	/* leading space padding */
483fef20d9cSFrederic Weisbecker 	spec.field_width -= spec.precision;
484fef20d9cSFrederic Weisbecker 	if (!(spec.flags & (ZEROPAD+LEFT))) {
485fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
486f796937aSJeremy Fitzhardinge 			if (buf < end)
4871da177e4SLinus Torvalds 				*buf = ' ';
4881da177e4SLinus Torvalds 			++buf;
4891da177e4SLinus Torvalds 		}
4901da177e4SLinus Torvalds 	}
491b39a7340SDenis Vlasenko 	/* sign */
4921da177e4SLinus Torvalds 	if (sign) {
493f796937aSJeremy Fitzhardinge 		if (buf < end)
4941da177e4SLinus Torvalds 			*buf = sign;
4951da177e4SLinus Torvalds 		++buf;
4961da177e4SLinus Torvalds 	}
497b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
498b39a7340SDenis Vlasenko 	if (need_pfx) {
499f796937aSJeremy Fitzhardinge 		if (buf < end)
5001da177e4SLinus Torvalds 			*buf = '0';
5011da177e4SLinus Torvalds 		++buf;
502fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
503f796937aSJeremy Fitzhardinge 			if (buf < end)
5049b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
5051da177e4SLinus Torvalds 			++buf;
5061da177e4SLinus Torvalds 		}
5071da177e4SLinus Torvalds 	}
508b39a7340SDenis Vlasenko 	/* zero or space padding */
509fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
510fef20d9cSFrederic Weisbecker 		char c = (spec.flags & ZEROPAD) ? '0' : ' ';
511fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
512f796937aSJeremy Fitzhardinge 			if (buf < end)
5131da177e4SLinus Torvalds 				*buf = c;
5141da177e4SLinus Torvalds 			++buf;
5151da177e4SLinus Torvalds 		}
5161da177e4SLinus Torvalds 	}
517b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
518fef20d9cSFrederic Weisbecker 	while (i <= --spec.precision) {
519f796937aSJeremy Fitzhardinge 		if (buf < end)
5201da177e4SLinus Torvalds 			*buf = '0';
5211da177e4SLinus Torvalds 		++buf;
5221da177e4SLinus Torvalds 	}
523b39a7340SDenis Vlasenko 	/* actual digits of result */
524b39a7340SDenis Vlasenko 	while (--i >= 0) {
525f796937aSJeremy Fitzhardinge 		if (buf < end)
5261da177e4SLinus Torvalds 			*buf = tmp[i];
5271da177e4SLinus Torvalds 		++buf;
5281da177e4SLinus Torvalds 	}
529b39a7340SDenis Vlasenko 	/* trailing space padding */
530fef20d9cSFrederic Weisbecker 	while (--spec.field_width >= 0) {
531f796937aSJeremy Fitzhardinge 		if (buf < end)
5321da177e4SLinus Torvalds 			*buf = ' ';
5331da177e4SLinus Torvalds 		++buf;
5341da177e4SLinus Torvalds 	}
5357b9186f5SAndré Goddard Rosa 
5361da177e4SLinus Torvalds 	return buf;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
539cf3b429bSJoe Perches static noinline_for_stack
540cf3b429bSJoe Perches char *string(char *buf, char *end, const char *s, struct printf_spec spec)
5410f9bfa56SLinus Torvalds {
5420f9bfa56SLinus Torvalds 	int len, i;
5430f9bfa56SLinus Torvalds 
5440f9bfa56SLinus Torvalds 	if ((unsigned long)s < PAGE_SIZE)
5450f4f81dcSAndré Goddard Rosa 		s = "(null)";
5460f9bfa56SLinus Torvalds 
547fef20d9cSFrederic Weisbecker 	len = strnlen(s, spec.precision);
5480f9bfa56SLinus Torvalds 
549fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
550fef20d9cSFrederic Weisbecker 		while (len < spec.field_width--) {
5510f9bfa56SLinus Torvalds 			if (buf < end)
5520f9bfa56SLinus Torvalds 				*buf = ' ';
5530f9bfa56SLinus Torvalds 			++buf;
5540f9bfa56SLinus Torvalds 		}
5550f9bfa56SLinus Torvalds 	}
5560f9bfa56SLinus Torvalds 	for (i = 0; i < len; ++i) {
5570f9bfa56SLinus Torvalds 		if (buf < end)
5580f9bfa56SLinus Torvalds 			*buf = *s;
5590f9bfa56SLinus Torvalds 		++buf; ++s;
5600f9bfa56SLinus Torvalds 	}
561fef20d9cSFrederic Weisbecker 	while (len < spec.field_width--) {
5620f9bfa56SLinus Torvalds 		if (buf < end)
5630f9bfa56SLinus Torvalds 			*buf = ' ';
5640f9bfa56SLinus Torvalds 		++buf;
5650f9bfa56SLinus Torvalds 	}
5667b9186f5SAndré Goddard Rosa 
5670f9bfa56SLinus Torvalds 	return buf;
5680f9bfa56SLinus Torvalds }
5690f9bfa56SLinus Torvalds 
570cf3b429bSJoe Perches static noinline_for_stack
571cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
5720c8b946eSFrederic Weisbecker 		    struct printf_spec spec, char ext)
5730fe1ef24SLinus Torvalds {
5740fe1ef24SLinus Torvalds 	unsigned long value = (unsigned long) ptr;
5750fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
5760fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
57791adcd2cSSteven Rostedt 	if (ext != 'f' && ext != 's')
5780fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
5790c8b946eSFrederic Weisbecker 	else
5800c8b946eSFrederic Weisbecker 		kallsyms_lookup(value, NULL, NULL, NULL, sym);
5817b9186f5SAndré Goddard Rosa 
582fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
5830fe1ef24SLinus Torvalds #else
584fef20d9cSFrederic Weisbecker 	spec.field_width = 2 * sizeof(void *);
585fef20d9cSFrederic Weisbecker 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
586fef20d9cSFrederic Weisbecker 	spec.base = 16;
5877b9186f5SAndré Goddard Rosa 
588fef20d9cSFrederic Weisbecker 	return number(buf, end, value, spec);
5890fe1ef24SLinus Torvalds #endif
5900fe1ef24SLinus Torvalds }
5910fe1ef24SLinus Torvalds 
592cf3b429bSJoe Perches static noinline_for_stack
593cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
594fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
595332d2e78SLinus Torvalds {
596332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
59728405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
598332d2e78SLinus Torvalds #endif
599332d2e78SLinus Torvalds 
600332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
60128405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
602332d2e78SLinus Torvalds #endif
6034da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
604fef20d9cSFrederic Weisbecker 		.base = 16,
6054da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
606fef20d9cSFrederic Weisbecker 		.precision = -1,
607fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
608fef20d9cSFrederic Weisbecker 	};
6094da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
6104da0b66cSBjorn Helgaas 		.base = 16,
6114da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
6124da0b66cSBjorn Helgaas 		.precision = -1,
6134da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
6144da0b66cSBjorn Helgaas 	};
6150f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
6160f4050c7SBjorn Helgaas 		.base = 16,
6170f4050c7SBjorn Helgaas 		.field_width = 2,
6180f4050c7SBjorn Helgaas 		.precision = -1,
6190f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
6200f4050c7SBjorn Helgaas 	};
6214da0b66cSBjorn Helgaas 	static const struct printf_spec dec_spec = {
622c91d3376SBjorn Helgaas 		.base = 10,
623c91d3376SBjorn Helgaas 		.precision = -1,
624c91d3376SBjorn Helgaas 		.flags = 0,
625c91d3376SBjorn Helgaas 	};
6264da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
627fd95541eSBjorn Helgaas 		.field_width = -1,
628fd95541eSBjorn Helgaas 		.precision = 10,
629fd95541eSBjorn Helgaas 		.flags = LEFT,
630fd95541eSBjorn Helgaas 	};
6314da0b66cSBjorn Helgaas 	static const struct printf_spec flag_spec = {
632fd95541eSBjorn Helgaas 		.base = 16,
633fd95541eSBjorn Helgaas 		.precision = -1,
634fd95541eSBjorn Helgaas 		.flags = SPECIAL | SMALL,
635fd95541eSBjorn Helgaas 	};
636c7dabef8SBjorn Helgaas 
637c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
638c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
639c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
640c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
6419d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
642c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
643c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
644c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
645c7dabef8SBjorn Helgaas 
646332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
647c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
6484da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
649332d2e78SLinus Torvalds 
650332d2e78SLinus Torvalds 	*p++ = '[';
6514da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
652fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
6534da0b66cSBjorn Helgaas 		specp = &io_spec;
6544da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
655fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
6564da0b66cSBjorn Helgaas 		specp = &mem_spec;
6574da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
658fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
6594da0b66cSBjorn Helgaas 		specp = &dec_spec;
6604da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
661fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
6624da0b66cSBjorn Helgaas 		specp = &dec_spec;
6630f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
6640f4050c7SBjorn Helgaas 		p = string(p, pend, "bus ", str_spec);
6650f4050c7SBjorn Helgaas 		specp = &bus_spec;
6664da0b66cSBjorn Helgaas 	} else {
667c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
6684da0b66cSBjorn Helgaas 		specp = &mem_spec;
669c7dabef8SBjorn Helgaas 		decode = 0;
670fd95541eSBjorn Helgaas 	}
6714da0b66cSBjorn Helgaas 	p = number(p, pend, res->start, *specp);
672c91d3376SBjorn Helgaas 	if (res->start != res->end) {
673332d2e78SLinus Torvalds 		*p++ = '-';
6744da0b66cSBjorn Helgaas 		p = number(p, pend, res->end, *specp);
675c91d3376SBjorn Helgaas 	}
676c7dabef8SBjorn Helgaas 	if (decode) {
677fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
678fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
679fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
680fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
6819d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
6829d7cca04SBjorn Helgaas 			p = string(p, pend, " window", str_spec);
683fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
684fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
685c7dabef8SBjorn Helgaas 	} else {
686fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
687c7dabef8SBjorn Helgaas 		p = number(p, pend, res->flags, flag_spec);
688fd95541eSBjorn Helgaas 	}
689332d2e78SLinus Torvalds 	*p++ = ']';
690c7dabef8SBjorn Helgaas 	*p = '\0';
691332d2e78SLinus Torvalds 
692fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
693332d2e78SLinus Torvalds }
694332d2e78SLinus Torvalds 
695cf3b429bSJoe Perches static noinline_for_stack
696cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
6978a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
698dd45c9cfSHarvey Harrison {
6998a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
700dd45c9cfSHarvey Harrison 	char *p = mac_addr;
701dd45c9cfSHarvey Harrison 	int i;
702bc7259a2SJoe Perches 	char separator;
703bc7259a2SJoe Perches 
704bc7259a2SJoe Perches 	if (fmt[1] == 'F') {		/* FDDI canonical format */
705bc7259a2SJoe Perches 		separator = '-';
706bc7259a2SJoe Perches 	} else {
707bc7259a2SJoe Perches 		separator = ':';
708bc7259a2SJoe Perches 	}
709dd45c9cfSHarvey Harrison 
710dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
711c8e00060SJoe Perches 		p = pack_hex_byte(p, addr[i]);
7128a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
713bc7259a2SJoe Perches 			*p++ = separator;
714dd45c9cfSHarvey Harrison 	}
715dd45c9cfSHarvey Harrison 	*p = '\0';
716dd45c9cfSHarvey Harrison 
717fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
718dd45c9cfSHarvey Harrison }
719dd45c9cfSHarvey Harrison 
720cf3b429bSJoe Perches static noinline_for_stack
721cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
722689afa7dSHarvey Harrison {
723689afa7dSHarvey Harrison 	int i;
7240159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
7250159f24eSJoe Perches 	int index;
7260159f24eSJoe Perches 	int step;
727689afa7dSHarvey Harrison 
7280159f24eSJoe Perches 	switch (fmt[2]) {
7290159f24eSJoe Perches 	case 'h':
7300159f24eSJoe Perches #ifdef __BIG_ENDIAN
7310159f24eSJoe Perches 		index = 0;
7320159f24eSJoe Perches 		step = 1;
7330159f24eSJoe Perches #else
7340159f24eSJoe Perches 		index = 3;
7350159f24eSJoe Perches 		step = -1;
7360159f24eSJoe Perches #endif
7370159f24eSJoe Perches 		break;
7380159f24eSJoe Perches 	case 'l':
7390159f24eSJoe Perches 		index = 3;
7400159f24eSJoe Perches 		step = -1;
7410159f24eSJoe Perches 		break;
7420159f24eSJoe Perches 	case 'n':
7430159f24eSJoe Perches 	case 'b':
7440159f24eSJoe Perches 	default:
7450159f24eSJoe Perches 		index = 0;
7460159f24eSJoe Perches 		step = 1;
7470159f24eSJoe Perches 		break;
7480159f24eSJoe Perches 	}
7498a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
7508a27f7c9SJoe Perches 		char temp[3];	/* hold each IP quad in reverse order */
7510159f24eSJoe Perches 		int digits = put_dec_trunc(temp, addr[index]) - temp;
7528a27f7c9SJoe Perches 		if (leading_zeros) {
7538a27f7c9SJoe Perches 			if (digits < 3)
7548a27f7c9SJoe Perches 				*p++ = '0';
7558a27f7c9SJoe Perches 			if (digits < 2)
7568a27f7c9SJoe Perches 				*p++ = '0';
7578a27f7c9SJoe Perches 		}
7588a27f7c9SJoe Perches 		/* reverse the digits in the quad */
7598a27f7c9SJoe Perches 		while (digits--)
7608a27f7c9SJoe Perches 			*p++ = temp[digits];
7618a27f7c9SJoe Perches 		if (i < 3)
7628a27f7c9SJoe Perches 			*p++ = '.';
7630159f24eSJoe Perches 		index += step;
7648a27f7c9SJoe Perches 	}
7658a27f7c9SJoe Perches 	*p = '\0';
7667b9186f5SAndré Goddard Rosa 
7678a27f7c9SJoe Perches 	return p;
7688a27f7c9SJoe Perches }
7698a27f7c9SJoe Perches 
770cf3b429bSJoe Perches static noinline_for_stack
771cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
7728a27f7c9SJoe Perches {
7737b9186f5SAndré Goddard Rosa 	int i, j, range;
7748a27f7c9SJoe Perches 	unsigned char zerolength[8];
7758a27f7c9SJoe Perches 	int longest = 1;
7768a27f7c9SJoe Perches 	int colonpos = -1;
7778a27f7c9SJoe Perches 	u16 word;
7787b9186f5SAndré Goddard Rosa 	u8 hi, lo;
7798a27f7c9SJoe Perches 	bool needcolon = false;
780eb78cd26SJoe Perches 	bool useIPv4;
781eb78cd26SJoe Perches 	struct in6_addr in6;
782eb78cd26SJoe Perches 
783eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
784eb78cd26SJoe Perches 
785eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
7868a27f7c9SJoe Perches 
7878a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
7888a27f7c9SJoe Perches 
7898a27f7c9SJoe Perches 	if (useIPv4)
7908a27f7c9SJoe Perches 		range = 6;
7918a27f7c9SJoe Perches 	else
7928a27f7c9SJoe Perches 		range = 8;
7938a27f7c9SJoe Perches 
7948a27f7c9SJoe Perches 	/* find position of longest 0 run */
7958a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
7968a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
797eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
7988a27f7c9SJoe Perches 				break;
7998a27f7c9SJoe Perches 			zerolength[i]++;
8008a27f7c9SJoe Perches 		}
8018a27f7c9SJoe Perches 	}
8028a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
8038a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
8048a27f7c9SJoe Perches 			longest = zerolength[i];
8058a27f7c9SJoe Perches 			colonpos = i;
8068a27f7c9SJoe Perches 		}
8078a27f7c9SJoe Perches 	}
8088a27f7c9SJoe Perches 
8098a27f7c9SJoe Perches 	/* emit address */
8108a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
8118a27f7c9SJoe Perches 		if (i == colonpos) {
8128a27f7c9SJoe Perches 			if (needcolon || i == 0)
8138a27f7c9SJoe Perches 				*p++ = ':';
8148a27f7c9SJoe Perches 			*p++ = ':';
8158a27f7c9SJoe Perches 			needcolon = false;
8168a27f7c9SJoe Perches 			i += longest - 1;
8178a27f7c9SJoe Perches 			continue;
8188a27f7c9SJoe Perches 		}
8198a27f7c9SJoe Perches 		if (needcolon) {
8208a27f7c9SJoe Perches 			*p++ = ':';
8218a27f7c9SJoe Perches 			needcolon = false;
8228a27f7c9SJoe Perches 		}
8238a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
824eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
8258a27f7c9SJoe Perches 		hi = word >> 8;
8268a27f7c9SJoe Perches 		lo = word & 0xff;
8278a27f7c9SJoe Perches 		if (hi) {
8288a27f7c9SJoe Perches 			if (hi > 0x0f)
8298a27f7c9SJoe Perches 				p = pack_hex_byte(p, hi);
8308a27f7c9SJoe Perches 			else
8318a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
832b5ff992bSAndré Goddard Rosa 			p = pack_hex_byte(p, lo);
8338a27f7c9SJoe Perches 		}
834b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
8358a27f7c9SJoe Perches 			p = pack_hex_byte(p, lo);
8368a27f7c9SJoe Perches 		else
8378a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
8388a27f7c9SJoe Perches 		needcolon = true;
8398a27f7c9SJoe Perches 	}
8408a27f7c9SJoe Perches 
8418a27f7c9SJoe Perches 	if (useIPv4) {
8428a27f7c9SJoe Perches 		if (needcolon)
8438a27f7c9SJoe Perches 			*p++ = ':';
8440159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
8458a27f7c9SJoe Perches 	}
8468a27f7c9SJoe Perches 	*p = '\0';
8477b9186f5SAndré Goddard Rosa 
8488a27f7c9SJoe Perches 	return p;
8498a27f7c9SJoe Perches }
8508a27f7c9SJoe Perches 
851cf3b429bSJoe Perches static noinline_for_stack
852cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
8538a27f7c9SJoe Perches {
8548a27f7c9SJoe Perches 	int i;
8557b9186f5SAndré Goddard Rosa 
856689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
857eb78cd26SJoe Perches 		p = pack_hex_byte(p, *addr++);
858eb78cd26SJoe Perches 		p = pack_hex_byte(p, *addr++);
8598a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
860689afa7dSHarvey Harrison 			*p++ = ':';
861689afa7dSHarvey Harrison 	}
862689afa7dSHarvey Harrison 	*p = '\0';
8637b9186f5SAndré Goddard Rosa 
8648a27f7c9SJoe Perches 	return p;
8658a27f7c9SJoe Perches }
8668a27f7c9SJoe Perches 
867cf3b429bSJoe Perches static noinline_for_stack
868cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
8698a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
8708a27f7c9SJoe Perches {
8718a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
8728a27f7c9SJoe Perches 
8738a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
874eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
8758a27f7c9SJoe Perches 	else
876eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
877689afa7dSHarvey Harrison 
878fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
879689afa7dSHarvey Harrison }
880689afa7dSHarvey Harrison 
881cf3b429bSJoe Perches static noinline_for_stack
882cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
8838a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
8844aa99606SHarvey Harrison {
8858a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
8864aa99606SHarvey Harrison 
8870159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
8884aa99606SHarvey Harrison 
889fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
8904aa99606SHarvey Harrison }
8914aa99606SHarvey Harrison 
892cf3b429bSJoe Perches static noinline_for_stack
893cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
8949ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
8959ac6e44eSJoe Perches {
8969ac6e44eSJoe Perches 	char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
8979ac6e44eSJoe Perches 	char *p = uuid;
8989ac6e44eSJoe Perches 	int i;
8999ac6e44eSJoe Perches 	static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
9009ac6e44eSJoe Perches 	static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
9019ac6e44eSJoe Perches 	const u8 *index = be;
9029ac6e44eSJoe Perches 	bool uc = false;
9039ac6e44eSJoe Perches 
9049ac6e44eSJoe Perches 	switch (*(++fmt)) {
9059ac6e44eSJoe Perches 	case 'L':
9069ac6e44eSJoe Perches 		uc = true;		/* fall-through */
9079ac6e44eSJoe Perches 	case 'l':
9089ac6e44eSJoe Perches 		index = le;
9099ac6e44eSJoe Perches 		break;
9109ac6e44eSJoe Perches 	case 'B':
9119ac6e44eSJoe Perches 		uc = true;
9129ac6e44eSJoe Perches 		break;
9139ac6e44eSJoe Perches 	}
9149ac6e44eSJoe Perches 
9159ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
9169ac6e44eSJoe Perches 		p = pack_hex_byte(p, addr[index[i]]);
9179ac6e44eSJoe Perches 		switch (i) {
9189ac6e44eSJoe Perches 		case 3:
9199ac6e44eSJoe Perches 		case 5:
9209ac6e44eSJoe Perches 		case 7:
9219ac6e44eSJoe Perches 		case 9:
9229ac6e44eSJoe Perches 			*p++ = '-';
9239ac6e44eSJoe Perches 			break;
9249ac6e44eSJoe Perches 		}
9259ac6e44eSJoe Perches 	}
9269ac6e44eSJoe Perches 
9279ac6e44eSJoe Perches 	*p = 0;
9289ac6e44eSJoe Perches 
9299ac6e44eSJoe Perches 	if (uc) {
9309ac6e44eSJoe Perches 		p = uuid;
9319ac6e44eSJoe Perches 		do {
9329ac6e44eSJoe Perches 			*p = toupper(*p);
9339ac6e44eSJoe Perches 		} while (*(++p));
9349ac6e44eSJoe Perches 	}
9359ac6e44eSJoe Perches 
9369ac6e44eSJoe Perches 	return string(buf, end, uuid, spec);
9379ac6e44eSJoe Perches }
9389ac6e44eSJoe Perches 
9394d8a743cSLinus Torvalds /*
9404d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
9414d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
9424d8a743cSLinus Torvalds  * specifiers.
9434d8a743cSLinus Torvalds  *
944332d2e78SLinus Torvalds  * Right now we handle:
9450fe1ef24SLinus Torvalds  *
9460c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
9470c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
9480efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
9490efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
950c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
951c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
952dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
953dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
9548a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
955bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
956c8e00060SJoe Perches  *       with a dash-separated hex notation
9578a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
9588a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
9598a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
9608a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
9618a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
9628a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
9630159f24eSJoe Perches  * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
9648a27f7c9SJoe Perches  * - 'I6c' for IPv6 addresses printed as specified by
9653f472402SUwe Kleine-König  *       http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00
9669ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
9679ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
9689ac6e44eSJoe Perches  *       Options for %pU are:
9699ac6e44eSJoe Perches  *         b big endian lower case hex (default)
9709ac6e44eSJoe Perches  *         B big endian UPPER case hex
9719ac6e44eSJoe Perches  *         l little endian lower case hex
9729ac6e44eSJoe Perches  *         L little endian UPPER case hex
9739ac6e44eSJoe Perches  *           big endian output byte order is:
9749ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
9759ac6e44eSJoe Perches  *           little endian output byte order is:
9769ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
9777db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
9787db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
9797db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
9807db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
9817db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
9829ac6e44eSJoe Perches  *
983332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
984332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
985332d2e78SLinus Torvalds  * pointer to the real address.
9864d8a743cSLinus Torvalds  */
987cf3b429bSJoe Perches static noinline_for_stack
988cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
989fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
99078a8bf69SLinus Torvalds {
991*5e057981SJoe Perches 	if (!ptr) {
992*5e057981SJoe Perches 		/*
993*5e057981SJoe Perches 		 * Print (null) with the same width as a pointer so it makes
994*5e057981SJoe Perches 		 * tabular output look nice.
995*5e057981SJoe Perches 		 */
996*5e057981SJoe Perches 		if (spec.field_width == -1)
997*5e057981SJoe Perches 			spec.field_width = 2 * sizeof(void *);
998fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
999*5e057981SJoe Perches 	}
1000d97106abSLinus Torvalds 
10010fe1ef24SLinus Torvalds 	switch (*fmt) {
10020fe1ef24SLinus Torvalds 	case 'F':
10030c8b946eSFrederic Weisbecker 	case 'f':
10040fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
10050fe1ef24SLinus Torvalds 		/* Fallthrough */
10060fe1ef24SLinus Torvalds 	case 'S':
10079ac6e44eSJoe Perches 	case 's':
10080c8b946eSFrederic Weisbecker 		return symbol_string(buf, end, ptr, spec, *fmt);
1009332d2e78SLinus Torvalds 	case 'R':
1010c7dabef8SBjorn Helgaas 	case 'r':
1011fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
10128a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
10138a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
1014bc7259a2SJoe Perches 					/* [mM]F (FDDI, bit reversed) */
10158a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
10168a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
10178a27f7c9SJoe Perches 					 * 4:	1.2.3.4
10188a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
10198a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
10208a27f7c9SJoe Perches 					 */
10218a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
10228a27f7c9SJoe Perches 					 * 4:	001.002.003.004
10238a27f7c9SJoe Perches 					 * 6:   000102...0f
10248a27f7c9SJoe Perches 					 */
10258a27f7c9SJoe Perches 		switch (fmt[1]) {
10268a27f7c9SJoe Perches 		case '6':
10278a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
10288a27f7c9SJoe Perches 		case '4':
10298a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
10308a27f7c9SJoe Perches 		}
10314aa99606SHarvey Harrison 		break;
10329ac6e44eSJoe Perches 	case 'U':
10339ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
10347db6f5fbSJoe Perches 	case 'V':
10357db6f5fbSJoe Perches 		return buf + vsnprintf(buf, end - buf,
10367db6f5fbSJoe Perches 				       ((struct va_format *)ptr)->fmt,
10377db6f5fbSJoe Perches 				       *(((struct va_format *)ptr)->va));
10380fe1ef24SLinus Torvalds 	}
1039fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
1040fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
1041fef20d9cSFrederic Weisbecker 		spec.field_width = 2 * sizeof(void *);
1042fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
104378a8bf69SLinus Torvalds 	}
1044fef20d9cSFrederic Weisbecker 	spec.base = 16;
1045fef20d9cSFrederic Weisbecker 
1046fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
1047fef20d9cSFrederic Weisbecker }
1048fef20d9cSFrederic Weisbecker 
1049fef20d9cSFrederic Weisbecker /*
1050fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
1051fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
1052fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
1053fef20d9cSFrederic Weisbecker  * to go on the next call).
1054fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
1055fef20d9cSFrederic Weisbecker  *
1056fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
1057fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
1058fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
1059fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
1060fef20d9cSFrederic Weisbecker  *
1061fef20d9cSFrederic Weisbecker  * @fmt: the format string
1062fef20d9cSFrederic Weisbecker  * @type of the token returned
1063fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
1064fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
1065fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
1066fef20d9cSFrederic Weisbecker  * @precision: precision of a number
1067fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
1068fef20d9cSFrederic Weisbecker  */
1069cf3b429bSJoe Perches static noinline_for_stack
1070cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
1071fef20d9cSFrederic Weisbecker {
1072fef20d9cSFrederic Weisbecker 	const char *start = fmt;
1073fef20d9cSFrederic Weisbecker 
1074fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
1075ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
1076fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
1077fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
1078fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
1079fef20d9cSFrederic Weisbecker 		}
1080fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1081fef20d9cSFrederic Weisbecker 		goto precision;
1082fef20d9cSFrederic Weisbecker 	}
1083fef20d9cSFrederic Weisbecker 
1084fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
1085fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
1086fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
1087fef20d9cSFrederic Weisbecker 			spec->precision = 0;
1088fef20d9cSFrederic Weisbecker 
1089fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1090fef20d9cSFrederic Weisbecker 		goto qualifier;
1091fef20d9cSFrederic Weisbecker 	}
1092fef20d9cSFrederic Weisbecker 
1093fef20d9cSFrederic Weisbecker 	/* By default */
1094fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
1095fef20d9cSFrederic Weisbecker 
1096fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
1097fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
1098fef20d9cSFrederic Weisbecker 			break;
1099fef20d9cSFrederic Weisbecker 	}
1100fef20d9cSFrederic Weisbecker 
1101fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
1102fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
1103fef20d9cSFrederic Weisbecker 		return fmt - start;
1104fef20d9cSFrederic Weisbecker 
1105fef20d9cSFrederic Weisbecker 	/* Process flags */
1106fef20d9cSFrederic Weisbecker 	spec->flags = 0;
1107fef20d9cSFrederic Weisbecker 
1108fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
1109fef20d9cSFrederic Weisbecker 		bool found = true;
1110fef20d9cSFrederic Weisbecker 
1111fef20d9cSFrederic Weisbecker 		++fmt;
1112fef20d9cSFrederic Weisbecker 
1113fef20d9cSFrederic Weisbecker 		switch (*fmt) {
1114fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
1115fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
1116fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
1117fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
1118fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
1119fef20d9cSFrederic Weisbecker 		default:  found = false;
1120fef20d9cSFrederic Weisbecker 		}
1121fef20d9cSFrederic Weisbecker 
1122fef20d9cSFrederic Weisbecker 		if (!found)
1123fef20d9cSFrederic Weisbecker 			break;
1124fef20d9cSFrederic Weisbecker 	}
1125fef20d9cSFrederic Weisbecker 
1126fef20d9cSFrederic Weisbecker 	/* get field width */
1127fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
1128fef20d9cSFrederic Weisbecker 
1129fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
1130fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
1131fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
1132fef20d9cSFrederic Weisbecker 		/* it's the next argument */
1133ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
1134fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1135fef20d9cSFrederic Weisbecker 	}
1136fef20d9cSFrederic Weisbecker 
1137fef20d9cSFrederic Weisbecker precision:
1138fef20d9cSFrederic Weisbecker 	/* get the precision */
1139fef20d9cSFrederic Weisbecker 	spec->precision = -1;
1140fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
1141fef20d9cSFrederic Weisbecker 		++fmt;
1142fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
1143fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
1144fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
1145fef20d9cSFrederic Weisbecker 				spec->precision = 0;
1146fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
1147fef20d9cSFrederic Weisbecker 			/* it's the next argument */
1148adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
1149fef20d9cSFrederic Weisbecker 			return ++fmt - start;
1150fef20d9cSFrederic Weisbecker 		}
1151fef20d9cSFrederic Weisbecker 	}
1152fef20d9cSFrederic Weisbecker 
1153fef20d9cSFrederic Weisbecker qualifier:
1154fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
1155fef20d9cSFrederic Weisbecker 	spec->qualifier = -1;
115608562cb2SAndré Goddard Rosa 	if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
115708562cb2SAndré Goddard Rosa 	    TOLOWER(*fmt) == 'z' || *fmt == 't') {
1158a4e94ef0SZhaolei 		spec->qualifier = *fmt++;
1159a4e94ef0SZhaolei 		if (unlikely(spec->qualifier == *fmt)) {
1160a4e94ef0SZhaolei 			if (spec->qualifier == 'l') {
1161fef20d9cSFrederic Weisbecker 				spec->qualifier = 'L';
1162fef20d9cSFrederic Weisbecker 				++fmt;
1163a4e94ef0SZhaolei 			} else if (spec->qualifier == 'h') {
1164a4e94ef0SZhaolei 				spec->qualifier = 'H';
1165a4e94ef0SZhaolei 				++fmt;
1166a4e94ef0SZhaolei 			}
1167fef20d9cSFrederic Weisbecker 		}
1168fef20d9cSFrederic Weisbecker 	}
1169fef20d9cSFrederic Weisbecker 
1170fef20d9cSFrederic Weisbecker 	/* default base */
1171fef20d9cSFrederic Weisbecker 	spec->base = 10;
1172fef20d9cSFrederic Weisbecker 	switch (*fmt) {
1173fef20d9cSFrederic Weisbecker 	case 'c':
1174fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
1175fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1176fef20d9cSFrederic Weisbecker 
1177fef20d9cSFrederic Weisbecker 	case 's':
1178fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
1179fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1180fef20d9cSFrederic Weisbecker 
1181fef20d9cSFrederic Weisbecker 	case 'p':
1182fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
1183fef20d9cSFrederic Weisbecker 		return fmt - start;
1184fef20d9cSFrederic Weisbecker 		/* skip alnum */
1185fef20d9cSFrederic Weisbecker 
1186fef20d9cSFrederic Weisbecker 	case 'n':
1187fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NRCHARS;
1188fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1189fef20d9cSFrederic Weisbecker 
1190fef20d9cSFrederic Weisbecker 	case '%':
1191fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1192fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1193fef20d9cSFrederic Weisbecker 
1194fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1195fef20d9cSFrederic Weisbecker 	case 'o':
1196fef20d9cSFrederic Weisbecker 		spec->base = 8;
1197fef20d9cSFrederic Weisbecker 		break;
1198fef20d9cSFrederic Weisbecker 
1199fef20d9cSFrederic Weisbecker 	case 'x':
1200fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1201fef20d9cSFrederic Weisbecker 
1202fef20d9cSFrederic Weisbecker 	case 'X':
1203fef20d9cSFrederic Weisbecker 		spec->base = 16;
1204fef20d9cSFrederic Weisbecker 		break;
1205fef20d9cSFrederic Weisbecker 
1206fef20d9cSFrederic Weisbecker 	case 'd':
1207fef20d9cSFrederic Weisbecker 	case 'i':
120839e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1209fef20d9cSFrederic Weisbecker 	case 'u':
1210fef20d9cSFrederic Weisbecker 		break;
1211fef20d9cSFrederic Weisbecker 
1212fef20d9cSFrederic Weisbecker 	default:
1213fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1214fef20d9cSFrederic Weisbecker 		return fmt - start;
1215fef20d9cSFrederic Weisbecker 	}
1216fef20d9cSFrederic Weisbecker 
1217fef20d9cSFrederic Weisbecker 	if (spec->qualifier == 'L')
1218fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1219fef20d9cSFrederic Weisbecker 	else if (spec->qualifier == 'l') {
122039e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1221fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_LONG;
1222fef20d9cSFrederic Weisbecker 		else
1223fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_ULONG;
122408562cb2SAndré Goddard Rosa 	} else if (TOLOWER(spec->qualifier) == 'z') {
1225fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1226fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 't') {
1227fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1228a4e94ef0SZhaolei 	} else if (spec->qualifier == 'H') {
1229a4e94ef0SZhaolei 		if (spec->flags & SIGN)
1230a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_BYTE;
1231a4e94ef0SZhaolei 		else
1232a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_UBYTE;
1233fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'h') {
123439e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1235fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_SHORT;
1236fef20d9cSFrederic Weisbecker 		else
1237fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_USHORT;
1238fef20d9cSFrederic Weisbecker 	} else {
123939e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1240fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_INT;
1241fef20d9cSFrederic Weisbecker 		else
1242fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_UINT;
1243fef20d9cSFrederic Weisbecker 	}
1244fef20d9cSFrederic Weisbecker 
1245fef20d9cSFrederic Weisbecker 	return ++fmt - start;
124678a8bf69SLinus Torvalds }
124778a8bf69SLinus Torvalds 
12481da177e4SLinus Torvalds /**
12491da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
12501da177e4SLinus Torvalds  * @buf: The buffer to place the result into
12511da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
12521da177e4SLinus Torvalds  * @fmt: The format string to use
12531da177e4SLinus Torvalds  * @args: Arguments for the format string
12541da177e4SLinus Torvalds  *
125520036fdcSAndi Kleen  * This function follows C99 vsnprintf, but has some extensions:
125691adcd2cSSteven Rostedt  * %pS output the name of a text symbol with offset
125791adcd2cSSteven Rostedt  * %ps output the name of a text symbol without offset
12580c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
12590c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
12608a79503aSUwe Kleine-König  * %pR output the address range in a struct resource with decoded flags
12618a79503aSUwe Kleine-König  * %pr output the address range in a struct resource with raw flags
12628a79503aSUwe Kleine-König  * %pM output a 6-byte MAC address with colons
12638a79503aSUwe Kleine-König  * %pm output a 6-byte MAC address without colons
12648a79503aSUwe Kleine-König  * %pI4 print an IPv4 address without leading zeros
12658a79503aSUwe Kleine-König  * %pi4 print an IPv4 address with leading zeros
12668a79503aSUwe Kleine-König  * %pI6 print an IPv6 address with colons
12678a79503aSUwe Kleine-König  * %pi6 print an IPv6 address without colons
12688a79503aSUwe Kleine-König  * %pI6c print an IPv6 address as specified by
12693f472402SUwe Kleine-König  *   http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00
12708a79503aSUwe Kleine-König  * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
12718a79503aSUwe Kleine-König  *   case.
12720efb4d20SSteven Rostedt  * %n is ignored
127320036fdcSAndi Kleen  *
12741da177e4SLinus Torvalds  * The return value is the number of characters which would
12751da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
12761da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
12771da177e4SLinus Torvalds  * number of characters written into @buf as return value
127872fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
12791da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
12801da177e4SLinus Torvalds  * string is truncated.
12811da177e4SLinus Torvalds  *
12821da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
128372fd4a35SRobert P. J. Day  * You probably want snprintf() instead.
12841da177e4SLinus Torvalds  */
12851da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
12861da177e4SLinus Torvalds {
12871da177e4SLinus Torvalds 	unsigned long long num;
1288d4be151bSAndré Goddard Rosa 	char *str, *end;
1289fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
12901da177e4SLinus Torvalds 
1291f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1292f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
12932f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
12941da177e4SLinus Torvalds 		return 0;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	str = buf;
1297f796937aSJeremy Fitzhardinge 	end = buf + size;
12981da177e4SLinus Torvalds 
1299f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1300f796937aSJeremy Fitzhardinge 	if (end < buf) {
13011da177e4SLinus Torvalds 		end = ((void *)-1);
1302f796937aSJeremy Fitzhardinge 		size = end - buf;
13031da177e4SLinus Torvalds 	}
13041da177e4SLinus Torvalds 
1305fef20d9cSFrederic Weisbecker 	while (*fmt) {
1306fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1307d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1308fef20d9cSFrederic Weisbecker 
1309fef20d9cSFrederic Weisbecker 		fmt += read;
1310fef20d9cSFrederic Weisbecker 
1311fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1312fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1313fef20d9cSFrederic Weisbecker 			int copy = read;
1314fef20d9cSFrederic Weisbecker 			if (str < end) {
1315fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1316fef20d9cSFrederic Weisbecker 					copy = end - str;
1317fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1318fef20d9cSFrederic Weisbecker 			}
1319fef20d9cSFrederic Weisbecker 			str += read;
1320fef20d9cSFrederic Weisbecker 			break;
13211da177e4SLinus Torvalds 		}
13221da177e4SLinus Torvalds 
1323ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1324fef20d9cSFrederic Weisbecker 			spec.field_width = va_arg(args, int);
1325fef20d9cSFrederic Weisbecker 			break;
13261da177e4SLinus Torvalds 
1327fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1328fef20d9cSFrederic Weisbecker 			spec.precision = va_arg(args, int);
1329fef20d9cSFrederic Weisbecker 			break;
13301da177e4SLinus Torvalds 
1331d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1332d4be151bSAndré Goddard Rosa 			char c;
1333d4be151bSAndré Goddard Rosa 
1334fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1335fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1336f796937aSJeremy Fitzhardinge 					if (str < end)
13371da177e4SLinus Torvalds 						*str = ' ';
13381da177e4SLinus Torvalds 					++str;
1339fef20d9cSFrederic Weisbecker 
13401da177e4SLinus Torvalds 				}
13411da177e4SLinus Torvalds 			}
13421da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1343f796937aSJeremy Fitzhardinge 			if (str < end)
13441da177e4SLinus Torvalds 				*str = c;
13451da177e4SLinus Torvalds 			++str;
1346fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1347f796937aSJeremy Fitzhardinge 				if (str < end)
13481da177e4SLinus Torvalds 					*str = ' ';
13491da177e4SLinus Torvalds 				++str;
13501da177e4SLinus Torvalds 			}
1351fef20d9cSFrederic Weisbecker 			break;
1352d4be151bSAndré Goddard Rosa 		}
13531da177e4SLinus Torvalds 
1354fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1355fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1356fef20d9cSFrederic Weisbecker 			break;
13571da177e4SLinus Torvalds 
1358fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1359fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, va_arg(args, void *),
1360fef20d9cSFrederic Weisbecker 				      spec);
1361fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
13624d8a743cSLinus Torvalds 				fmt++;
1363fef20d9cSFrederic Weisbecker 			break;
13641da177e4SLinus Torvalds 
1365fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1366f796937aSJeremy Fitzhardinge 			if (str < end)
13671da177e4SLinus Torvalds 				*str = '%';
13681da177e4SLinus Torvalds 			++str;
13691da177e4SLinus Torvalds 			break;
13701da177e4SLinus Torvalds 
1371fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1372f796937aSJeremy Fitzhardinge 			if (str < end)
13731da177e4SLinus Torvalds 				*str = '%';
13741da177e4SLinus Torvalds 			++str;
1375fef20d9cSFrederic Weisbecker 			break;
1376fef20d9cSFrederic Weisbecker 
1377fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
1378ef0658f3SJoe Perches 			u8 qualifier = spec.qualifier;
1379fef20d9cSFrederic Weisbecker 
1380fef20d9cSFrederic Weisbecker 			if (qualifier == 'l') {
1381fef20d9cSFrederic Weisbecker 				long *ip = va_arg(args, long *);
1382fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
138308562cb2SAndré Goddard Rosa 			} else if (TOLOWER(qualifier) == 'z') {
1384fef20d9cSFrederic Weisbecker 				size_t *ip = va_arg(args, size_t *);
1385fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
13861da177e4SLinus Torvalds 			} else {
1387fef20d9cSFrederic Weisbecker 				int *ip = va_arg(args, int *);
1388fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
1389fef20d9cSFrederic Weisbecker 			}
1390fef20d9cSFrederic Weisbecker 			break;
1391fef20d9cSFrederic Weisbecker 		}
1392fef20d9cSFrederic Weisbecker 
1393fef20d9cSFrederic Weisbecker 		default:
1394fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1395fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1396fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1397fef20d9cSFrederic Weisbecker 				break;
1398fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1399fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
1400fef20d9cSFrederic Weisbecker 				break;
1401fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1402fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
1403fef20d9cSFrederic Weisbecker 				break;
1404fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1405fef20d9cSFrederic Weisbecker 				num = va_arg(args, size_t);
1406fef20d9cSFrederic Weisbecker 				break;
1407fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1408fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
1409fef20d9cSFrederic Weisbecker 				break;
1410a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1411a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
1412a4e94ef0SZhaolei 				break;
1413a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1414a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
1415a4e94ef0SZhaolei 				break;
1416fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1417fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
1418fef20d9cSFrederic Weisbecker 				break;
1419fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1420fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
1421fef20d9cSFrederic Weisbecker 				break;
142239e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
142339e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
1424fef20d9cSFrederic Weisbecker 				break;
1425fef20d9cSFrederic Weisbecker 			default:
1426fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
14271da177e4SLinus Torvalds 			}
1428fef20d9cSFrederic Weisbecker 
1429fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
14301da177e4SLinus Torvalds 		}
1431fef20d9cSFrederic Weisbecker 	}
1432fef20d9cSFrederic Weisbecker 
1433f796937aSJeremy Fitzhardinge 	if (size > 0) {
1434f796937aSJeremy Fitzhardinge 		if (str < end)
14351da177e4SLinus Torvalds 			*str = '\0';
1436f796937aSJeremy Fitzhardinge 		else
14370a6047eeSLinus Torvalds 			end[-1] = '\0';
1438f796937aSJeremy Fitzhardinge 	}
1439fef20d9cSFrederic Weisbecker 
1440f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
14411da177e4SLinus Torvalds 	return str-buf;
1442fef20d9cSFrederic Weisbecker 
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds /**
14471da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
14481da177e4SLinus Torvalds  * @buf: The buffer to place the result into
14491da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
14501da177e4SLinus Torvalds  * @fmt: The format string to use
14511da177e4SLinus Torvalds  * @args: Arguments for the format string
14521da177e4SLinus Torvalds  *
14531da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
14541da177e4SLinus Torvalds  * the @buf not including the trailing '\0'. If @size is <= 0 the function
14551da177e4SLinus Torvalds  * returns 0.
14561da177e4SLinus Torvalds  *
14571da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
145872fd4a35SRobert P. J. Day  * You probably want scnprintf() instead.
145920036fdcSAndi Kleen  *
146020036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
14611da177e4SLinus Torvalds  */
14621da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
14631da177e4SLinus Torvalds {
14641da177e4SLinus Torvalds 	int i;
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
14677b9186f5SAndré Goddard Rosa 
14681da177e4SLinus Torvalds 	return (i >= size) ? (size - 1) : i;
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds /**
14731da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
14741da177e4SLinus Torvalds  * @buf: The buffer to place the result into
14751da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
14761da177e4SLinus Torvalds  * @fmt: The format string to use
14771da177e4SLinus Torvalds  * @...: Arguments for the format string
14781da177e4SLinus Torvalds  *
14791da177e4SLinus Torvalds  * The return value is the number of characters which would be
14801da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
14811da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
14821da177e4SLinus Torvalds  * @size, the resulting string is truncated.
148320036fdcSAndi Kleen  *
148420036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
14851da177e4SLinus Torvalds  */
14861da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
14871da177e4SLinus Torvalds {
14881da177e4SLinus Torvalds 	va_list args;
14891da177e4SLinus Torvalds 	int i;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	va_start(args, fmt);
14921da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
14931da177e4SLinus Torvalds 	va_end(args);
14947b9186f5SAndré Goddard Rosa 
14951da177e4SLinus Torvalds 	return i;
14961da177e4SLinus Torvalds }
14971da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds /**
15001da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
15011da177e4SLinus Torvalds  * @buf: The buffer to place the result into
15021da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
15031da177e4SLinus Torvalds  * @fmt: The format string to use
15041da177e4SLinus Torvalds  * @...: Arguments for the format string
15051da177e4SLinus Torvalds  *
15061da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
1507ea6f3281SMartin Peschke  * the trailing '\0'. If @size is <= 0 the function returns 0.
15081da177e4SLinus Torvalds  */
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
15111da177e4SLinus Torvalds {
15121da177e4SLinus Torvalds 	va_list args;
15131da177e4SLinus Torvalds 	int i;
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 	va_start(args, fmt);
15161da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
15171da177e4SLinus Torvalds 	va_end(args);
15187b9186f5SAndré Goddard Rosa 
15191da177e4SLinus Torvalds 	return (i >= size) ? (size - 1) : i;
15201da177e4SLinus Torvalds }
15211da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds /**
15241da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
15251da177e4SLinus Torvalds  * @buf: The buffer to place the result into
15261da177e4SLinus Torvalds  * @fmt: The format string to use
15271da177e4SLinus Torvalds  * @args: Arguments for the format string
15281da177e4SLinus Torvalds  *
15291da177e4SLinus Torvalds  * The function returns the number of characters written
153072fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
15311da177e4SLinus Torvalds  * buffer overflows.
15321da177e4SLinus Torvalds  *
15331da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
153472fd4a35SRobert P. J. Day  * You probably want sprintf() instead.
153520036fdcSAndi Kleen  *
153620036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
15371da177e4SLinus Torvalds  */
15381da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
15391da177e4SLinus Torvalds {
15401da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
15411da177e4SLinus Torvalds }
15421da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds /**
15451da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
15461da177e4SLinus Torvalds  * @buf: The buffer to place the result into
15471da177e4SLinus Torvalds  * @fmt: The format string to use
15481da177e4SLinus Torvalds  * @...: Arguments for the format string
15491da177e4SLinus Torvalds  *
15501da177e4SLinus Torvalds  * The function returns the number of characters written
155172fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
15521da177e4SLinus Torvalds  * buffer overflows.
155320036fdcSAndi Kleen  *
155420036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
15551da177e4SLinus Torvalds  */
15561da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
15571da177e4SLinus Torvalds {
15581da177e4SLinus Torvalds 	va_list args;
15591da177e4SLinus Torvalds 	int i;
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	va_start(args, fmt);
15621da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
15631da177e4SLinus Torvalds 	va_end(args);
15647b9186f5SAndré Goddard Rosa 
15651da177e4SLinus Torvalds 	return i;
15661da177e4SLinus Torvalds }
15671da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
15681da177e4SLinus Torvalds 
15694370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
15704370aa4aSLai Jiangshan /*
15714370aa4aSLai Jiangshan  * bprintf service:
15724370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
15734370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
15744370aa4aSLai Jiangshan  */
15754370aa4aSLai Jiangshan 
15764370aa4aSLai Jiangshan /**
15774370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
15784370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
15794370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
15804370aa4aSLai Jiangshan  * @fmt: The format string to use
15814370aa4aSLai Jiangshan  * @args: Arguments for the format string
15824370aa4aSLai Jiangshan  *
15834370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
15844370aa4aSLai Jiangshan  * is skiped.
15854370aa4aSLai Jiangshan  *
15864370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
15874370aa4aSLai Jiangshan  * the given input.
15884370aa4aSLai Jiangshan  *
15894370aa4aSLai Jiangshan  * NOTE:
15904370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
15914370aa4aSLai Jiangshan  * valid for bstr_printf().
15924370aa4aSLai Jiangshan  */
15934370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
15944370aa4aSLai Jiangshan {
1595fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
15964370aa4aSLai Jiangshan 	char *str, *end;
15974370aa4aSLai Jiangshan 
15984370aa4aSLai Jiangshan 	str = (char *)bin_buf;
15994370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
16004370aa4aSLai Jiangshan 
16014370aa4aSLai Jiangshan #define save_arg(type)							\
16024370aa4aSLai Jiangshan do {									\
16034370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
16044370aa4aSLai Jiangshan 		unsigned long long value;				\
16054370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
16064370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
16074370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
16084370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
16094370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
16104370aa4aSLai Jiangshan 		}							\
16114370aa4aSLai Jiangshan 	} else {							\
16124370aa4aSLai Jiangshan 		unsigned long value;					\
16134370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
16144370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
16154370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
16164370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
16174370aa4aSLai Jiangshan 	}								\
16184370aa4aSLai Jiangshan 	str += sizeof(type);						\
16194370aa4aSLai Jiangshan } while (0)
16204370aa4aSLai Jiangshan 
1621fef20d9cSFrederic Weisbecker 	while (*fmt) {
1622d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
16234370aa4aSLai Jiangshan 
1624fef20d9cSFrederic Weisbecker 		fmt += read;
1625fef20d9cSFrederic Weisbecker 
1626fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1627fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
1628d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_INVALID:
1629d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
1630fef20d9cSFrederic Weisbecker 			break;
1631fef20d9cSFrederic Weisbecker 
1632ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1633fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
16344370aa4aSLai Jiangshan 			save_arg(int);
1635fef20d9cSFrederic Weisbecker 			break;
16364370aa4aSLai Jiangshan 
1637fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
16384370aa4aSLai Jiangshan 			save_arg(char);
1639fef20d9cSFrederic Weisbecker 			break;
1640fef20d9cSFrederic Weisbecker 
1641fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
16424370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
16434370aa4aSLai Jiangshan 			size_t len;
16446c356634SAndré Goddard Rosa 
16454370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
16464370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
16470f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
16486c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
16496c356634SAndré Goddard Rosa 			if (str + len < end)
16506c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
16516c356634SAndré Goddard Rosa 			str += len;
1652fef20d9cSFrederic Weisbecker 			break;
16534370aa4aSLai Jiangshan 		}
1654fef20d9cSFrederic Weisbecker 
1655fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
16564370aa4aSLai Jiangshan 			save_arg(void *);
16574370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
1658fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
16594370aa4aSLai Jiangshan 				fmt++;
1660fef20d9cSFrederic Weisbecker 			break;
1661fef20d9cSFrederic Weisbecker 
1662fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
16634370aa4aSLai Jiangshan 			/* skip %n 's argument */
1664ef0658f3SJoe Perches 			u8 qualifier = spec.qualifier;
16654370aa4aSLai Jiangshan 			void *skip_arg;
16664370aa4aSLai Jiangshan 			if (qualifier == 'l')
16674370aa4aSLai Jiangshan 				skip_arg = va_arg(args, long *);
166808562cb2SAndré Goddard Rosa 			else if (TOLOWER(qualifier) == 'z')
16694370aa4aSLai Jiangshan 				skip_arg = va_arg(args, size_t *);
16704370aa4aSLai Jiangshan 			else
16714370aa4aSLai Jiangshan 				skip_arg = va_arg(args, int *);
1672fef20d9cSFrederic Weisbecker 			break;
16734370aa4aSLai Jiangshan 		}
16744370aa4aSLai Jiangshan 
1675fef20d9cSFrederic Weisbecker 		default:
1676fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1677fef20d9cSFrederic Weisbecker 
1678fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1679fef20d9cSFrederic Weisbecker 				save_arg(long long);
1680fef20d9cSFrederic Weisbecker 				break;
1681fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1682fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1683fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
1684fef20d9cSFrederic Weisbecker 				break;
1685fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1686fef20d9cSFrederic Weisbecker 				save_arg(size_t);
1687fef20d9cSFrederic Weisbecker 				break;
1688fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1689fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
1690fef20d9cSFrederic Weisbecker 				break;
1691a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1692a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1693a4e94ef0SZhaolei 				save_arg(char);
1694a4e94ef0SZhaolei 				break;
1695fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1696fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1697fef20d9cSFrederic Weisbecker 				save_arg(short);
1698fef20d9cSFrederic Weisbecker 				break;
1699fef20d9cSFrederic Weisbecker 			default:
1700fef20d9cSFrederic Weisbecker 				save_arg(int);
1701fef20d9cSFrederic Weisbecker 			}
1702fef20d9cSFrederic Weisbecker 		}
1703fef20d9cSFrederic Weisbecker 	}
1704fef20d9cSFrederic Weisbecker 
17057b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1706fef20d9cSFrederic Weisbecker #undef save_arg
17074370aa4aSLai Jiangshan }
17084370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
17094370aa4aSLai Jiangshan 
17104370aa4aSLai Jiangshan /**
17114370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
17124370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
17134370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
17144370aa4aSLai Jiangshan  * @fmt: The format string to use
17154370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
17164370aa4aSLai Jiangshan  *
17174370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
17184370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
17194370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
17204370aa4aSLai Jiangshan  *
17214370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
17220efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
17234370aa4aSLai Jiangshan  *
17244370aa4aSLai Jiangshan  * The return value is the number of characters which would
17254370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
17264370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
17274370aa4aSLai Jiangshan  * number of characters written into @buf as return value
17284370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
17294370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
17304370aa4aSLai Jiangshan  * string is truncated.
17314370aa4aSLai Jiangshan  */
17324370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
17334370aa4aSLai Jiangshan {
1734fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
1735d4be151bSAndré Goddard Rosa 	char *str, *end;
1736d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
17374370aa4aSLai Jiangshan 
17382f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
17394370aa4aSLai Jiangshan 		return 0;
17404370aa4aSLai Jiangshan 
17414370aa4aSLai Jiangshan 	str = buf;
17424370aa4aSLai Jiangshan 	end = buf + size;
17434370aa4aSLai Jiangshan 
17444370aa4aSLai Jiangshan #define get_arg(type)							\
17454370aa4aSLai Jiangshan ({									\
17464370aa4aSLai Jiangshan 	typeof(type) value;						\
17474370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
17484370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
17494370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
17504370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
17514370aa4aSLai Jiangshan 	} else {							\
17524370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
17534370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
17544370aa4aSLai Jiangshan 	}								\
17554370aa4aSLai Jiangshan 	args += sizeof(type);						\
17564370aa4aSLai Jiangshan 	value;								\
17574370aa4aSLai Jiangshan })
17584370aa4aSLai Jiangshan 
17594370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
17604370aa4aSLai Jiangshan 	if (end < buf) {
17614370aa4aSLai Jiangshan 		end = ((void *)-1);
17624370aa4aSLai Jiangshan 		size = end - buf;
17634370aa4aSLai Jiangshan 	}
17644370aa4aSLai Jiangshan 
1765fef20d9cSFrederic Weisbecker 	while (*fmt) {
1766fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1767d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1768fef20d9cSFrederic Weisbecker 
1769fef20d9cSFrederic Weisbecker 		fmt += read;
1770fef20d9cSFrederic Weisbecker 
1771fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1772fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1773fef20d9cSFrederic Weisbecker 			int copy = read;
1774fef20d9cSFrederic Weisbecker 			if (str < end) {
1775fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1776fef20d9cSFrederic Weisbecker 					copy = end - str;
1777fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1778fef20d9cSFrederic Weisbecker 			}
1779fef20d9cSFrederic Weisbecker 			str += read;
1780fef20d9cSFrederic Weisbecker 			break;
17814370aa4aSLai Jiangshan 		}
17824370aa4aSLai Jiangshan 
1783ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1784fef20d9cSFrederic Weisbecker 			spec.field_width = get_arg(int);
1785fef20d9cSFrederic Weisbecker 			break;
17864370aa4aSLai Jiangshan 
1787fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1788fef20d9cSFrederic Weisbecker 			spec.precision = get_arg(int);
1789fef20d9cSFrederic Weisbecker 			break;
17904370aa4aSLai Jiangshan 
1791d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1792d4be151bSAndré Goddard Rosa 			char c;
1793d4be151bSAndré Goddard Rosa 
1794fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1795fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
17964370aa4aSLai Jiangshan 					if (str < end)
17974370aa4aSLai Jiangshan 						*str = ' ';
17984370aa4aSLai Jiangshan 					++str;
17994370aa4aSLai Jiangshan 				}
18004370aa4aSLai Jiangshan 			}
18014370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
18024370aa4aSLai Jiangshan 			if (str < end)
18034370aa4aSLai Jiangshan 				*str = c;
18044370aa4aSLai Jiangshan 			++str;
1805fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
18064370aa4aSLai Jiangshan 				if (str < end)
18074370aa4aSLai Jiangshan 					*str = ' ';
18084370aa4aSLai Jiangshan 				++str;
18094370aa4aSLai Jiangshan 			}
1810fef20d9cSFrederic Weisbecker 			break;
1811d4be151bSAndré Goddard Rosa 		}
18124370aa4aSLai Jiangshan 
1813fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
18144370aa4aSLai Jiangshan 			const char *str_arg = args;
1815d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
1816fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
1817fef20d9cSFrederic Weisbecker 			break;
18184370aa4aSLai Jiangshan 		}
18194370aa4aSLai Jiangshan 
1820fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1821fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, get_arg(void *), spec);
1822fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
18234370aa4aSLai Jiangshan 				fmt++;
1824fef20d9cSFrederic Weisbecker 			break;
18254370aa4aSLai Jiangshan 
1826fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1827fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
18284370aa4aSLai Jiangshan 			if (str < end)
18294370aa4aSLai Jiangshan 				*str = '%';
18304370aa4aSLai Jiangshan 			++str;
1831fef20d9cSFrederic Weisbecker 			break;
1832fef20d9cSFrederic Weisbecker 
1833fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS:
1834fef20d9cSFrederic Weisbecker 			/* skip */
1835fef20d9cSFrederic Weisbecker 			break;
1836fef20d9cSFrederic Weisbecker 
1837d4be151bSAndré Goddard Rosa 		default: {
1838d4be151bSAndré Goddard Rosa 			unsigned long long num;
1839d4be151bSAndré Goddard Rosa 
1840fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1841fef20d9cSFrederic Weisbecker 
1842fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
18434370aa4aSLai Jiangshan 				num = get_arg(long long);
1844fef20d9cSFrederic Weisbecker 				break;
1845fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1846fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1847fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
1848fef20d9cSFrederic Weisbecker 				break;
1849fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
18504370aa4aSLai Jiangshan 				num = get_arg(size_t);
1851fef20d9cSFrederic Weisbecker 				break;
1852fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
18534370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
1854fef20d9cSFrederic Weisbecker 				break;
1855a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1856a4e94ef0SZhaolei 				num = get_arg(unsigned char);
1857a4e94ef0SZhaolei 				break;
1858a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1859a4e94ef0SZhaolei 				num = get_arg(signed char);
1860a4e94ef0SZhaolei 				break;
1861fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1862fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
1863fef20d9cSFrederic Weisbecker 				break;
1864fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1865fef20d9cSFrederic Weisbecker 				num = get_arg(short);
1866fef20d9cSFrederic Weisbecker 				break;
1867fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
18684370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
1869fef20d9cSFrederic Weisbecker 				break;
1870fef20d9cSFrederic Weisbecker 			default:
1871fef20d9cSFrederic Weisbecker 				num = get_arg(int);
18724370aa4aSLai Jiangshan 			}
1873fef20d9cSFrederic Weisbecker 
1874fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
1875d4be151bSAndré Goddard Rosa 		} /* default: */
1876d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
1877d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
1878fef20d9cSFrederic Weisbecker 
18794370aa4aSLai Jiangshan 	if (size > 0) {
18804370aa4aSLai Jiangshan 		if (str < end)
18814370aa4aSLai Jiangshan 			*str = '\0';
18824370aa4aSLai Jiangshan 		else
18834370aa4aSLai Jiangshan 			end[-1] = '\0';
18844370aa4aSLai Jiangshan 	}
1885fef20d9cSFrederic Weisbecker 
18864370aa4aSLai Jiangshan #undef get_arg
18874370aa4aSLai Jiangshan 
18884370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
18894370aa4aSLai Jiangshan 	return str - buf;
18904370aa4aSLai Jiangshan }
18914370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
18924370aa4aSLai Jiangshan 
18934370aa4aSLai Jiangshan /**
18944370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
18954370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
18964370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
18974370aa4aSLai Jiangshan  * @fmt: The format string to use
18984370aa4aSLai Jiangshan  * @...: Arguments for the format string
18994370aa4aSLai Jiangshan  *
19004370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
19014370aa4aSLai Jiangshan  * into @bin_buf.
19024370aa4aSLai Jiangshan  */
19034370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
19044370aa4aSLai Jiangshan {
19054370aa4aSLai Jiangshan 	va_list args;
19064370aa4aSLai Jiangshan 	int ret;
19074370aa4aSLai Jiangshan 
19084370aa4aSLai Jiangshan 	va_start(args, fmt);
19094370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
19104370aa4aSLai Jiangshan 	va_end(args);
19117b9186f5SAndré Goddard Rosa 
19124370aa4aSLai Jiangshan 	return ret;
19134370aa4aSLai Jiangshan }
19144370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
19154370aa4aSLai Jiangshan 
19164370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
19174370aa4aSLai Jiangshan 
19181da177e4SLinus Torvalds /**
19191da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
19201da177e4SLinus Torvalds  * @buf:	input buffer
19211da177e4SLinus Torvalds  * @fmt:	format of buffer
19221da177e4SLinus Torvalds  * @args:	arguments
19231da177e4SLinus Torvalds  */
19241da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
19251da177e4SLinus Torvalds {
19261da177e4SLinus Torvalds 	const char *str = buf;
19271da177e4SLinus Torvalds 	char *next;
19281da177e4SLinus Torvalds 	char digit;
19291da177e4SLinus Torvalds 	int num = 0;
1930ef0658f3SJoe Perches 	u8 qualifier;
1931ef0658f3SJoe Perches 	u8 base;
1932ef0658f3SJoe Perches 	s16 field_width;
1933d4be151bSAndré Goddard Rosa 	bool is_sign;
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	while (*fmt && *str) {
19361da177e4SLinus Torvalds 		/* skip any white space in format */
19371da177e4SLinus Torvalds 		/* white space in format matchs any amount of
19381da177e4SLinus Torvalds 		 * white space, including none, in the input.
19391da177e4SLinus Torvalds 		 */
19401da177e4SLinus Torvalds 		if (isspace(*fmt)) {
1941e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
1942e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
19431da177e4SLinus Torvalds 		}
19441da177e4SLinus Torvalds 
19451da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
19461da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
19471da177e4SLinus Torvalds 			if (*fmt++ != *str++)
19481da177e4SLinus Torvalds 				break;
19491da177e4SLinus Torvalds 			continue;
19501da177e4SLinus Torvalds 		}
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 		if (!*fmt)
19531da177e4SLinus Torvalds 			break;
19541da177e4SLinus Torvalds 		++fmt;
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 		/* skip this conversion.
19571da177e4SLinus Torvalds 		 * advance both strings to next white space
19581da177e4SLinus Torvalds 		 */
19591da177e4SLinus Torvalds 		if (*fmt == '*') {
19608fccae2cSAndy Spencer 			while (!isspace(*fmt) && *fmt != '%' && *fmt)
19611da177e4SLinus Torvalds 				fmt++;
19621da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
19631da177e4SLinus Torvalds 				str++;
19641da177e4SLinus Torvalds 			continue;
19651da177e4SLinus Torvalds 		}
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds 		/* get field width */
19681da177e4SLinus Torvalds 		field_width = -1;
19691da177e4SLinus Torvalds 		if (isdigit(*fmt))
19701da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds 		/* get conversion qualifier */
19731da177e4SLinus Torvalds 		qualifier = -1;
197408562cb2SAndré Goddard Rosa 		if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
197508562cb2SAndré Goddard Rosa 		    TOLOWER(*fmt) == 'z') {
19761da177e4SLinus Torvalds 			qualifier = *fmt++;
19771da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
19781da177e4SLinus Torvalds 				if (qualifier == 'h') {
19791da177e4SLinus Torvalds 					qualifier = 'H';
19801da177e4SLinus Torvalds 					fmt++;
19811da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
19821da177e4SLinus Torvalds 					qualifier = 'L';
19831da177e4SLinus Torvalds 					fmt++;
19841da177e4SLinus Torvalds 				}
19851da177e4SLinus Torvalds 			}
19861da177e4SLinus Torvalds 		}
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds 		if (!*fmt || !*str)
19891da177e4SLinus Torvalds 			break;
19901da177e4SLinus Torvalds 
1991d4be151bSAndré Goddard Rosa 		base = 10;
1992d4be151bSAndré Goddard Rosa 		is_sign = 0;
1993d4be151bSAndré Goddard Rosa 
19941da177e4SLinus Torvalds 		switch (*fmt++) {
19951da177e4SLinus Torvalds 		case 'c':
19961da177e4SLinus Torvalds 		{
19971da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
19981da177e4SLinus Torvalds 			if (field_width == -1)
19991da177e4SLinus Torvalds 				field_width = 1;
20001da177e4SLinus Torvalds 			do {
20011da177e4SLinus Torvalds 				*s++ = *str++;
20021da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
20031da177e4SLinus Torvalds 			num++;
20041da177e4SLinus Torvalds 		}
20051da177e4SLinus Torvalds 		continue;
20061da177e4SLinus Torvalds 		case 's':
20071da177e4SLinus Torvalds 		{
20081da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
20091da177e4SLinus Torvalds 			if (field_width == -1)
20104be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
20111da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
2012e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds 			/* now copy until next white space */
20157b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
20161da177e4SLinus Torvalds 				*s++ = *str++;
20171da177e4SLinus Torvalds 			*s = '\0';
20181da177e4SLinus Torvalds 			num++;
20191da177e4SLinus Torvalds 		}
20201da177e4SLinus Torvalds 		continue;
20211da177e4SLinus Torvalds 		case 'n':
20221da177e4SLinus Torvalds 			/* return number of characters read so far */
20231da177e4SLinus Torvalds 		{
20241da177e4SLinus Torvalds 			int *i = (int *)va_arg(args, int*);
20251da177e4SLinus Torvalds 			*i = str - buf;
20261da177e4SLinus Torvalds 		}
20271da177e4SLinus Torvalds 		continue;
20281da177e4SLinus Torvalds 		case 'o':
20291da177e4SLinus Torvalds 			base = 8;
20301da177e4SLinus Torvalds 			break;
20311da177e4SLinus Torvalds 		case 'x':
20321da177e4SLinus Torvalds 		case 'X':
20331da177e4SLinus Torvalds 			base = 16;
20341da177e4SLinus Torvalds 			break;
20351da177e4SLinus Torvalds 		case 'i':
20361da177e4SLinus Torvalds 			base = 0;
20371da177e4SLinus Torvalds 		case 'd':
20381da177e4SLinus Torvalds 			is_sign = 1;
20391da177e4SLinus Torvalds 		case 'u':
20401da177e4SLinus Torvalds 			break;
20411da177e4SLinus Torvalds 		case '%':
20421da177e4SLinus Torvalds 			/* looking for '%' in str */
20431da177e4SLinus Torvalds 			if (*str++ != '%')
20441da177e4SLinus Torvalds 				return num;
20451da177e4SLinus Torvalds 			continue;
20461da177e4SLinus Torvalds 		default:
20471da177e4SLinus Torvalds 			/* invalid format; stop here */
20481da177e4SLinus Torvalds 			return num;
20491da177e4SLinus Torvalds 		}
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 		/* have some sort of integer conversion.
20521da177e4SLinus Torvalds 		 * first, skip white space in buffer.
20531da177e4SLinus Torvalds 		 */
2054e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 		digit = *str;
20571da177e4SLinus Torvalds 		if (is_sign && digit == '-')
20581da177e4SLinus Torvalds 			digit = *(str + 1);
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds 		if (!digit
20611da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
20621da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
20631da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
20641da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
20651da177e4SLinus Torvalds 			break;
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds 		switch (qualifier) {
20681da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
20691da177e4SLinus Torvalds 			if (is_sign) {
20701da177e4SLinus Torvalds 				signed char *s = (signed char *)va_arg(args, signed char *);
20711da177e4SLinus Torvalds 				*s = (signed char)simple_strtol(str, &next, base);
20721da177e4SLinus Torvalds 			} else {
20731da177e4SLinus Torvalds 				unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
20741da177e4SLinus Torvalds 				*s = (unsigned char)simple_strtoul(str, &next, base);
20751da177e4SLinus Torvalds 			}
20761da177e4SLinus Torvalds 			break;
20771da177e4SLinus Torvalds 		case 'h':
20781da177e4SLinus Torvalds 			if (is_sign) {
20791da177e4SLinus Torvalds 				short *s = (short *)va_arg(args, short *);
20801da177e4SLinus Torvalds 				*s = (short)simple_strtol(str, &next, base);
20811da177e4SLinus Torvalds 			} else {
20821da177e4SLinus Torvalds 				unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
20831da177e4SLinus Torvalds 				*s = (unsigned short)simple_strtoul(str, &next, base);
20841da177e4SLinus Torvalds 			}
20851da177e4SLinus Torvalds 			break;
20861da177e4SLinus Torvalds 		case 'l':
20871da177e4SLinus Torvalds 			if (is_sign) {
20881da177e4SLinus Torvalds 				long *l = (long *)va_arg(args, long *);
20891da177e4SLinus Torvalds 				*l = simple_strtol(str, &next, base);
20901da177e4SLinus Torvalds 			} else {
20911da177e4SLinus Torvalds 				unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
20921da177e4SLinus Torvalds 				*l = simple_strtoul(str, &next, base);
20931da177e4SLinus Torvalds 			}
20941da177e4SLinus Torvalds 			break;
20951da177e4SLinus Torvalds 		case 'L':
20961da177e4SLinus Torvalds 			if (is_sign) {
20971da177e4SLinus Torvalds 				long long *l = (long long *)va_arg(args, long long *);
20981da177e4SLinus Torvalds 				*l = simple_strtoll(str, &next, base);
20991da177e4SLinus Torvalds 			} else {
21001da177e4SLinus Torvalds 				unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
21011da177e4SLinus Torvalds 				*l = simple_strtoull(str, &next, base);
21021da177e4SLinus Torvalds 			}
21031da177e4SLinus Torvalds 			break;
21041da177e4SLinus Torvalds 		case 'Z':
21051da177e4SLinus Torvalds 		case 'z':
21061da177e4SLinus Torvalds 		{
21071da177e4SLinus Torvalds 			size_t *s = (size_t *)va_arg(args, size_t *);
21081da177e4SLinus Torvalds 			*s = (size_t)simple_strtoul(str, &next, base);
21091da177e4SLinus Torvalds 		}
21101da177e4SLinus Torvalds 		break;
21111da177e4SLinus Torvalds 		default:
21121da177e4SLinus Torvalds 			if (is_sign) {
21131da177e4SLinus Torvalds 				int *i = (int *)va_arg(args, int *);
21141da177e4SLinus Torvalds 				*i = (int)simple_strtol(str, &next, base);
21151da177e4SLinus Torvalds 			} else {
21161da177e4SLinus Torvalds 				unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
21171da177e4SLinus Torvalds 				*i = (unsigned int)simple_strtoul(str, &next, base);
21181da177e4SLinus Torvalds 			}
21191da177e4SLinus Torvalds 			break;
21201da177e4SLinus Torvalds 		}
21211da177e4SLinus Torvalds 		num++;
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 		if (!next)
21241da177e4SLinus Torvalds 			break;
21251da177e4SLinus Torvalds 		str = next;
21261da177e4SLinus Torvalds 	}
2127c6b40d16SJohannes Berg 
2128c6b40d16SJohannes Berg 	/*
2129c6b40d16SJohannes Berg 	 * Now we've come all the way through so either the input string or the
2130c6b40d16SJohannes Berg 	 * format ended. In the former case, there can be a %n at the current
2131c6b40d16SJohannes Berg 	 * position in the format that needs to be filled.
2132c6b40d16SJohannes Berg 	 */
2133c6b40d16SJohannes Berg 	if (*fmt == '%' && *(fmt + 1) == 'n') {
2134c6b40d16SJohannes Berg 		int *p = (int *)va_arg(args, int *);
2135c6b40d16SJohannes Berg 		*p = str - buf;
2136c6b40d16SJohannes Berg 	}
2137c6b40d16SJohannes Berg 
21381da177e4SLinus Torvalds 	return num;
21391da177e4SLinus Torvalds }
21401da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds /**
21431da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
21441da177e4SLinus Torvalds  * @buf:	input buffer
21451da177e4SLinus Torvalds  * @fmt:	formatting of buffer
21461da177e4SLinus Torvalds  * @...:	resulting arguments
21471da177e4SLinus Torvalds  */
21481da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
21491da177e4SLinus Torvalds {
21501da177e4SLinus Torvalds 	va_list args;
21511da177e4SLinus Torvalds 	int i;
21521da177e4SLinus Torvalds 
21531da177e4SLinus Torvalds 	va_start(args, fmt);
21541da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
21551da177e4SLinus Torvalds 	va_end(args);
21567b9186f5SAndré Goddard Rosa 
21571da177e4SLinus Torvalds 	return i;
21581da177e4SLinus Torvalds }
21591da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
2160