xref: /openbmc/linux/lib/vsprintf.c (revision 8a27f7c90ffcb791eed7574922b51fb60b08fc89)
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>
28*8a27f7c9SJoe 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_strtoul - convert a string to an unsigned 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 simple_strtoul(const char *cp, char **endp, unsigned int base)
561da177e4SLinus Torvalds {
57aa46a63eSHarvey Harrison 	unsigned 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 	}
74aa46a63eSHarvey Harrison 
751da177e4SLinus Torvalds 	if (endp)
761da177e4SLinus Torvalds 		*endp = (char *)cp;
771da177e4SLinus Torvalds 	return result;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoul);
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /**
821da177e4SLinus Torvalds  * simple_strtol - convert a string to a signed long
831da177e4SLinus Torvalds  * @cp: The start of the string
841da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
851da177e4SLinus Torvalds  * @base: The number base to use
861da177e4SLinus Torvalds  */
871da177e4SLinus Torvalds long simple_strtol(const char *cp, char **endp, unsigned int base)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	if(*cp == '-')
901da177e4SLinus Torvalds 		return -simple_strtoul(cp + 1, endp, base);
911da177e4SLinus Torvalds 	return simple_strtoul(cp, endp, base);
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtol);
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds /**
961da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
971da177e4SLinus Torvalds  * @cp: The start of the string
981da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
991da177e4SLinus Torvalds  * @base: The number base to use
1001da177e4SLinus Torvalds  */
1011da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
1021da177e4SLinus Torvalds {
103aa46a63eSHarvey Harrison 	unsigned long long result = 0;
1041da177e4SLinus Torvalds 
105aa46a63eSHarvey Harrison 	if (!base)
106aa46a63eSHarvey Harrison 		base = simple_guess_base(cp);
107aa46a63eSHarvey Harrison 
108aa46a63eSHarvey Harrison 	if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
1091da177e4SLinus Torvalds 		cp += 2;
110aa46a63eSHarvey Harrison 
111aa46a63eSHarvey Harrison 	while (isxdigit(*cp)) {
112aa46a63eSHarvey Harrison 		unsigned int value;
113aa46a63eSHarvey Harrison 
114aa46a63eSHarvey Harrison 		value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
115aa46a63eSHarvey Harrison 		if (value >= base)
116aa46a63eSHarvey Harrison 			break;
1171da177e4SLinus Torvalds 		result = result * base + value;
1181da177e4SLinus Torvalds 		cp++;
1191da177e4SLinus Torvalds 	}
120aa46a63eSHarvey Harrison 
1211da177e4SLinus Torvalds 	if (endp)
1221da177e4SLinus Torvalds 		*endp = (char *)cp;
1231da177e4SLinus Torvalds 	return result;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /**
1281da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
1291da177e4SLinus Torvalds  * @cp: The start of the string
1301da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
1311da177e4SLinus Torvalds  * @base: The number base to use
1321da177e4SLinus Torvalds  */
1331da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	if(*cp=='-')
1361da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1371da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
14006b2a76dSYi Yang /**
14106b2a76dSYi Yang  * strict_strtoul - convert a string to an unsigned long strictly
14206b2a76dSYi Yang  * @cp: The string to be converted
14306b2a76dSYi Yang  * @base: The number base to use
14406b2a76dSYi Yang  * @res: The converted result value
14506b2a76dSYi Yang  *
14606b2a76dSYi Yang  * strict_strtoul converts a string to an unsigned long only if the
14706b2a76dSYi Yang  * string is really an unsigned long string, any string containing
14806b2a76dSYi Yang  * any invalid char at the tail will be rejected and -EINVAL is returned,
14906b2a76dSYi Yang  * only a newline char at the tail is acceptible because people generally
15006b2a76dSYi Yang  * change a module parameter in the following way:
15106b2a76dSYi Yang  *
15206b2a76dSYi Yang  * 	echo 1024 > /sys/module/e1000/parameters/copybreak
15306b2a76dSYi Yang  *
15406b2a76dSYi Yang  * echo will append a newline to the tail.
15506b2a76dSYi Yang  *
15606b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
15706b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
15806b2a76dSYi Yang  *
15906b2a76dSYi Yang  * simple_strtoul just ignores the successive invalid characters and
16006b2a76dSYi Yang  * return the converted value of prefix part of the string.
16106b2a76dSYi Yang  */
1629d85db22SHarvey Harrison int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
1639d85db22SHarvey Harrison {
1649d85db22SHarvey Harrison 	char *tail;
1659d85db22SHarvey Harrison 	unsigned long val;
1669d85db22SHarvey Harrison 	size_t len;
1679d85db22SHarvey Harrison 
1689d85db22SHarvey Harrison 	*res = 0;
1699d85db22SHarvey Harrison 	len = strlen(cp);
1709d85db22SHarvey Harrison 	if (len == 0)
1719d85db22SHarvey Harrison 		return -EINVAL;
1729d85db22SHarvey Harrison 
1739d85db22SHarvey Harrison 	val = simple_strtoul(cp, &tail, base);
174e899aa82SPavel Machek 	if (tail == cp)
175e899aa82SPavel Machek 		return -EINVAL;
1769d85db22SHarvey Harrison 	if ((*tail == '\0') ||
1779d85db22SHarvey Harrison 		((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
1789d85db22SHarvey Harrison 		*res = val;
1799d85db22SHarvey Harrison 		return 0;
1809d85db22SHarvey Harrison 	}
1819d85db22SHarvey Harrison 
1829d85db22SHarvey Harrison 	return -EINVAL;
1839d85db22SHarvey Harrison }
1849d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoul);
18506b2a76dSYi Yang 
18606b2a76dSYi Yang /**
18706b2a76dSYi Yang  * strict_strtol - convert a string to a long strictly
18806b2a76dSYi Yang  * @cp: The string to be converted
18906b2a76dSYi Yang  * @base: The number base to use
19006b2a76dSYi Yang  * @res: The converted result value
19106b2a76dSYi Yang  *
19206b2a76dSYi Yang  * strict_strtol is similiar to strict_strtoul, but it allows the first
19306b2a76dSYi Yang  * character of a string is '-'.
19406b2a76dSYi Yang  *
19506b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
19606b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
19706b2a76dSYi Yang  */
1989d85db22SHarvey Harrison int strict_strtol(const char *cp, unsigned int base, long *res)
1999d85db22SHarvey Harrison {
2009d85db22SHarvey Harrison 	int ret;
2019d85db22SHarvey Harrison 	if (*cp == '-') {
2029d85db22SHarvey Harrison 		ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
2039d85db22SHarvey Harrison 		if (!ret)
2049d85db22SHarvey Harrison 			*res = -(*res);
2059d85db22SHarvey Harrison 	} else {
2069d85db22SHarvey Harrison 		ret = strict_strtoul(cp, base, (unsigned long *)res);
2079d85db22SHarvey Harrison 	}
2089d85db22SHarvey Harrison 
2099d85db22SHarvey Harrison 	return ret;
2109d85db22SHarvey Harrison }
2119d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtol);
21206b2a76dSYi Yang 
21306b2a76dSYi Yang /**
21406b2a76dSYi Yang  * strict_strtoull - convert a string to an unsigned long long strictly
21506b2a76dSYi Yang  * @cp: The string to be converted
21606b2a76dSYi Yang  * @base: The number base to use
21706b2a76dSYi Yang  * @res: The converted result value
21806b2a76dSYi Yang  *
21906b2a76dSYi Yang  * strict_strtoull converts a string to an unsigned long long only if the
22006b2a76dSYi Yang  * string is really an unsigned long long string, any string containing
22106b2a76dSYi Yang  * any invalid char at the tail will be rejected and -EINVAL is returned,
22206b2a76dSYi Yang  * only a newline char at the tail is acceptible because people generally
22306b2a76dSYi Yang  * change a module parameter in the following way:
22406b2a76dSYi Yang  *
22506b2a76dSYi Yang  * 	echo 1024 > /sys/module/e1000/parameters/copybreak
22606b2a76dSYi Yang  *
22706b2a76dSYi Yang  * echo will append a newline to the tail of the string.
22806b2a76dSYi Yang  *
22906b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
23006b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
23106b2a76dSYi Yang  *
23206b2a76dSYi Yang  * simple_strtoull just ignores the successive invalid characters and
23306b2a76dSYi Yang  * return the converted value of prefix part of the string.
23406b2a76dSYi Yang  */
2359d85db22SHarvey Harrison int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
2369d85db22SHarvey Harrison {
2379d85db22SHarvey Harrison 	char *tail;
2389d85db22SHarvey Harrison 	unsigned long long val;
2399d85db22SHarvey Harrison 	size_t len;
2409d85db22SHarvey Harrison 
2419d85db22SHarvey Harrison 	*res = 0;
2429d85db22SHarvey Harrison 	len = strlen(cp);
2439d85db22SHarvey Harrison 	if (len == 0)
2449d85db22SHarvey Harrison 		return -EINVAL;
2459d85db22SHarvey Harrison 
2469d85db22SHarvey Harrison 	val = simple_strtoull(cp, &tail, base);
247e899aa82SPavel Machek 	if (tail == cp)
248e899aa82SPavel Machek 		return -EINVAL;
2499d85db22SHarvey Harrison 	if ((*tail == '\0') ||
2509d85db22SHarvey Harrison 		((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
2519d85db22SHarvey Harrison 		*res = val;
2529d85db22SHarvey Harrison 		return 0;
2539d85db22SHarvey Harrison 	}
2549d85db22SHarvey Harrison 
2559d85db22SHarvey Harrison 	return -EINVAL;
2569d85db22SHarvey Harrison }
2579d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoull);
25806b2a76dSYi Yang 
25906b2a76dSYi Yang /**
26006b2a76dSYi Yang  * strict_strtoll - convert a string to a long long strictly
26106b2a76dSYi Yang  * @cp: The string to be converted
26206b2a76dSYi Yang  * @base: The number base to use
26306b2a76dSYi Yang  * @res: The converted result value
26406b2a76dSYi Yang  *
26506b2a76dSYi Yang  * strict_strtoll is similiar to strict_strtoull, but it allows the first
26606b2a76dSYi Yang  * character of a string is '-'.
26706b2a76dSYi Yang  *
26806b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
26906b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
27006b2a76dSYi Yang  */
2719d85db22SHarvey Harrison int strict_strtoll(const char *cp, unsigned int base, long long *res)
2729d85db22SHarvey Harrison {
2739d85db22SHarvey Harrison 	int ret;
2749d85db22SHarvey Harrison 	if (*cp == '-') {
2759d85db22SHarvey Harrison 		ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
2769d85db22SHarvey Harrison 		if (!ret)
2779d85db22SHarvey Harrison 			*res = -(*res);
2789d85db22SHarvey Harrison 	} else {
2799d85db22SHarvey Harrison 		ret = strict_strtoull(cp, base, (unsigned long long *)res);
2809d85db22SHarvey Harrison 	}
28106b2a76dSYi Yang 
2829d85db22SHarvey Harrison 	return ret;
2839d85db22SHarvey Harrison }
28406b2a76dSYi Yang EXPORT_SYMBOL(strict_strtoll);
28506b2a76dSYi Yang 
2861da177e4SLinus Torvalds static int skip_atoi(const char **s)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	int i=0;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	while (isdigit(**s))
2911da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
2921da177e4SLinus Torvalds 	return i;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2954277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used
2964277eeddSDenis Vlasenko  * for /proc and /sys data. This directly impacts e.g. top performance
2974277eeddSDenis Vlasenko  * with many processes running. We optimize it for speed
2984277eeddSDenis Vlasenko  * using code from
2994277eeddSDenis Vlasenko  * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
3004277eeddSDenis Vlasenko  * (with permission from the author, Douglas W. Jones). */
3014277eeddSDenis Vlasenko 
3024277eeddSDenis Vlasenko /* Formats correctly any integer in [0,99999].
3034277eeddSDenis Vlasenko  * Outputs from one to five digits depending on input.
3044277eeddSDenis Vlasenko  * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
3054277eeddSDenis Vlasenko static char* put_dec_trunc(char *buf, unsigned q)
3064277eeddSDenis Vlasenko {
3074277eeddSDenis Vlasenko 	unsigned d3, d2, d1, d0;
3084277eeddSDenis Vlasenko 	d1 = (q>>4) & 0xf;
3094277eeddSDenis Vlasenko 	d2 = (q>>8) & 0xf;
3104277eeddSDenis Vlasenko 	d3 = (q>>12);
3114277eeddSDenis Vlasenko 
3124277eeddSDenis Vlasenko 	d0 = 6*(d3 + d2 + d1) + (q & 0xf);
3134277eeddSDenis Vlasenko 	q = (d0 * 0xcd) >> 11;
3144277eeddSDenis Vlasenko 	d0 = d0 - 10*q;
3154277eeddSDenis Vlasenko 	*buf++ = d0 + '0'; /* least significant digit */
3164277eeddSDenis Vlasenko 	d1 = q + 9*d3 + 5*d2 + d1;
3174277eeddSDenis Vlasenko 	if (d1 != 0) {
3184277eeddSDenis Vlasenko 		q = (d1 * 0xcd) >> 11;
3194277eeddSDenis Vlasenko 		d1 = d1 - 10*q;
3204277eeddSDenis Vlasenko 		*buf++ = d1 + '0'; /* next digit */
3214277eeddSDenis Vlasenko 
3224277eeddSDenis Vlasenko 		d2 = q + 2*d2;
3234277eeddSDenis Vlasenko 		if ((d2 != 0) || (d3 != 0)) {
3244277eeddSDenis Vlasenko 			q = (d2 * 0xd) >> 7;
3254277eeddSDenis Vlasenko 			d2 = d2 - 10*q;
3264277eeddSDenis Vlasenko 			*buf++ = d2 + '0'; /* next digit */
3274277eeddSDenis Vlasenko 
3284277eeddSDenis Vlasenko 			d3 = q + 4*d3;
3294277eeddSDenis Vlasenko 			if (d3 != 0) {
3304277eeddSDenis Vlasenko 				q = (d3 * 0xcd) >> 11;
3314277eeddSDenis Vlasenko 				d3 = d3 - 10*q;
3324277eeddSDenis Vlasenko 				*buf++ = d3 + '0';  /* next digit */
3334277eeddSDenis Vlasenko 				if (q != 0)
3344277eeddSDenis Vlasenko 					*buf++ = q + '0';  /* most sign. digit */
3354277eeddSDenis Vlasenko 			}
3364277eeddSDenis Vlasenko 		}
3374277eeddSDenis Vlasenko 	}
3384277eeddSDenis Vlasenko 	return buf;
3394277eeddSDenis Vlasenko }
3404277eeddSDenis Vlasenko /* Same with if's removed. Always emits five digits */
3414277eeddSDenis Vlasenko static char* put_dec_full(char *buf, unsigned q)
3424277eeddSDenis Vlasenko {
3434277eeddSDenis Vlasenko 	/* BTW, if q is in [0,9999], 8-bit ints will be enough, */
3444277eeddSDenis Vlasenko 	/* but anyway, gcc produces better code with full-sized ints */
3454277eeddSDenis Vlasenko 	unsigned d3, d2, d1, d0;
3464277eeddSDenis Vlasenko 	d1 = (q>>4) & 0xf;
3474277eeddSDenis Vlasenko 	d2 = (q>>8) & 0xf;
3484277eeddSDenis Vlasenko 	d3 = (q>>12);
3494277eeddSDenis Vlasenko 
3504277eeddSDenis Vlasenko 	/* Possible ways to approx. divide by 10 */
3514277eeddSDenis Vlasenko 	/* gcc -O2 replaces multiply with shifts and adds */
3524277eeddSDenis Vlasenko 	// (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
3534277eeddSDenis Vlasenko 	// (x * 0x67) >> 10:  1100111
3544277eeddSDenis Vlasenko 	// (x * 0x34) >> 9:    110100 - same
3554277eeddSDenis Vlasenko 	// (x * 0x1a) >> 8:     11010 - same
3564277eeddSDenis Vlasenko 	// (x * 0x0d) >> 7:      1101 - same, shortest code (on i386)
3574277eeddSDenis Vlasenko 
3584277eeddSDenis Vlasenko 	d0 = 6*(d3 + d2 + d1) + (q & 0xf);
3594277eeddSDenis Vlasenko 	q = (d0 * 0xcd) >> 11;
3604277eeddSDenis Vlasenko 	d0 = d0 - 10*q;
3614277eeddSDenis Vlasenko 	*buf++ = d0 + '0';
3624277eeddSDenis Vlasenko 	d1 = q + 9*d3 + 5*d2 + d1;
3634277eeddSDenis Vlasenko 		q = (d1 * 0xcd) >> 11;
3644277eeddSDenis Vlasenko 		d1 = d1 - 10*q;
3654277eeddSDenis Vlasenko 		*buf++ = d1 + '0';
3664277eeddSDenis Vlasenko 
3674277eeddSDenis Vlasenko 		d2 = q + 2*d2;
3684277eeddSDenis Vlasenko 			q = (d2 * 0xd) >> 7;
3694277eeddSDenis Vlasenko 			d2 = d2 - 10*q;
3704277eeddSDenis Vlasenko 			*buf++ = d2 + '0';
3714277eeddSDenis Vlasenko 
3724277eeddSDenis Vlasenko 			d3 = q + 4*d3;
3734277eeddSDenis Vlasenko 				q = (d3 * 0xcd) >> 11; /* - shorter code */
3744277eeddSDenis Vlasenko 				/* q = (d3 * 0x67) >> 10; - would also work */
3754277eeddSDenis Vlasenko 				d3 = d3 - 10*q;
3764277eeddSDenis Vlasenko 				*buf++ = d3 + '0';
3774277eeddSDenis Vlasenko 					*buf++ = q + '0';
3784277eeddSDenis Vlasenko 	return buf;
3794277eeddSDenis Vlasenko }
3804277eeddSDenis Vlasenko /* No inlining helps gcc to use registers better */
3814277eeddSDenis Vlasenko static noinline char* put_dec(char *buf, unsigned long long num)
3824277eeddSDenis Vlasenko {
3834277eeddSDenis Vlasenko 	while (1) {
3844277eeddSDenis Vlasenko 		unsigned rem;
3854277eeddSDenis Vlasenko 		if (num < 100000)
3864277eeddSDenis Vlasenko 			return put_dec_trunc(buf, num);
3874277eeddSDenis Vlasenko 		rem = do_div(num, 100000);
3884277eeddSDenis Vlasenko 		buf = put_dec_full(buf, rem);
3894277eeddSDenis Vlasenko 	}
3904277eeddSDenis Vlasenko }
3914277eeddSDenis Vlasenko 
3921da177e4SLinus Torvalds #define ZEROPAD	1		/* pad with zero */
3931da177e4SLinus Torvalds #define SIGN	2		/* unsigned/signed long */
3941da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3951da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
3961da177e4SLinus Torvalds #define LEFT	16		/* left justified */
3979b706aeeSDenys Vlasenko #define SMALL	32		/* Must be 32 == 0x20 */
3989b706aeeSDenys Vlasenko #define SPECIAL	64		/* 0x */
3991da177e4SLinus Torvalds 
400fef20d9cSFrederic Weisbecker enum format_type {
401fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
402ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
403fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
404fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
405fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
406fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
407fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
408fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
409fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
410fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
411fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
412a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
413a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
414fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
415fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
416fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
417fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
418fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NRCHARS,
419fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
420fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
421fef20d9cSFrederic Weisbecker };
422fef20d9cSFrederic Weisbecker 
423fef20d9cSFrederic Weisbecker struct printf_spec {
424fef20d9cSFrederic Weisbecker 	enum format_type	type;
425fef20d9cSFrederic Weisbecker 	int			flags;		/* flags to number() */
426fef20d9cSFrederic Weisbecker 	int			field_width;	/* width of output field */
427fef20d9cSFrederic Weisbecker 	int			base;
428fef20d9cSFrederic Weisbecker 	int			precision;	/* # of digits/chars */
429fef20d9cSFrederic Weisbecker 	int			qualifier;
430fef20d9cSFrederic Weisbecker };
431fef20d9cSFrederic Weisbecker 
432fef20d9cSFrederic Weisbecker static char *number(char *buf, char *end, unsigned long long num,
433fef20d9cSFrederic Weisbecker 			struct printf_spec spec)
4341da177e4SLinus Torvalds {
4359b706aeeSDenys Vlasenko 	/* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
4369b706aeeSDenys Vlasenko 	static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
4379b706aeeSDenys Vlasenko 
4389b706aeeSDenys Vlasenko 	char tmp[66];
4399b706aeeSDenys Vlasenko 	char sign;
4409b706aeeSDenys Vlasenko 	char locase;
441fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
4421da177e4SLinus Torvalds 	int i;
4431da177e4SLinus Torvalds 
4449b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
4459b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
446fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
447fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
448fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
4491da177e4SLinus Torvalds 	sign = 0;
450fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4511da177e4SLinus Torvalds 		if ((signed long long) num < 0) {
4521da177e4SLinus Torvalds 			sign = '-';
4531da177e4SLinus Torvalds 			num = - (signed long long) num;
454fef20d9cSFrederic Weisbecker 			spec.field_width--;
455fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4561da177e4SLinus Torvalds 			sign = '+';
457fef20d9cSFrederic Weisbecker 			spec.field_width--;
458fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4591da177e4SLinus Torvalds 			sign = ' ';
460fef20d9cSFrederic Weisbecker 			spec.field_width--;
4611da177e4SLinus Torvalds 		}
4621da177e4SLinus Torvalds 	}
463b39a7340SDenis Vlasenko 	if (need_pfx) {
464fef20d9cSFrederic Weisbecker 		spec.field_width--;
465fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
466fef20d9cSFrederic Weisbecker 			spec.field_width--;
4671da177e4SLinus Torvalds 	}
468b39a7340SDenis Vlasenko 
469b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4701da177e4SLinus Torvalds 	i = 0;
4711da177e4SLinus Torvalds 	if (num == 0)
4721da177e4SLinus Torvalds 		tmp[i++] = '0';
4734277eeddSDenis Vlasenko 	/* Generic code, for any base:
4744277eeddSDenis Vlasenko 	else do {
4759b706aeeSDenys Vlasenko 		tmp[i++] = (digits[do_div(num,base)] | locase);
4764277eeddSDenis Vlasenko 	} while (num != 0);
4774277eeddSDenis Vlasenko 	*/
478fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
479fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
480b39a7340SDenis Vlasenko 		int shift = 3;
481fef20d9cSFrederic Weisbecker 		if (spec.base == 16) shift = 4;
482b39a7340SDenis Vlasenko 		do {
4839b706aeeSDenys Vlasenko 			tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
484b39a7340SDenis Vlasenko 			num >>= shift;
485b39a7340SDenis Vlasenko 		} while (num);
4864277eeddSDenis Vlasenko 	} else { /* base 10 */
4874277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4884277eeddSDenis Vlasenko 	}
489b39a7340SDenis Vlasenko 
490b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
491fef20d9cSFrederic Weisbecker 	if (i > spec.precision)
492fef20d9cSFrederic Weisbecker 		spec.precision = i;
493b39a7340SDenis Vlasenko 	/* leading space padding */
494fef20d9cSFrederic Weisbecker 	spec.field_width -= spec.precision;
495fef20d9cSFrederic Weisbecker 	if (!(spec.flags & (ZEROPAD+LEFT))) {
496fef20d9cSFrederic Weisbecker 		while(--spec.field_width >= 0) {
497f796937aSJeremy Fitzhardinge 			if (buf < end)
4981da177e4SLinus Torvalds 				*buf = ' ';
4991da177e4SLinus Torvalds 			++buf;
5001da177e4SLinus Torvalds 		}
5011da177e4SLinus Torvalds 	}
502b39a7340SDenis Vlasenko 	/* sign */
5031da177e4SLinus Torvalds 	if (sign) {
504f796937aSJeremy Fitzhardinge 		if (buf < end)
5051da177e4SLinus Torvalds 			*buf = sign;
5061da177e4SLinus Torvalds 		++buf;
5071da177e4SLinus Torvalds 	}
508b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
509b39a7340SDenis Vlasenko 	if (need_pfx) {
510f796937aSJeremy Fitzhardinge 		if (buf < end)
5111da177e4SLinus Torvalds 			*buf = '0';
5121da177e4SLinus Torvalds 		++buf;
513fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
514f796937aSJeremy Fitzhardinge 			if (buf < end)
5159b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
5161da177e4SLinus Torvalds 			++buf;
5171da177e4SLinus Torvalds 		}
5181da177e4SLinus Torvalds 	}
519b39a7340SDenis Vlasenko 	/* zero or space padding */
520fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
521fef20d9cSFrederic Weisbecker 		char c = (spec.flags & ZEROPAD) ? '0' : ' ';
522fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
523f796937aSJeremy Fitzhardinge 			if (buf < end)
5241da177e4SLinus Torvalds 				*buf = c;
5251da177e4SLinus Torvalds 			++buf;
5261da177e4SLinus Torvalds 		}
5271da177e4SLinus Torvalds 	}
528b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
529fef20d9cSFrederic Weisbecker 	while (i <= --spec.precision) {
530f796937aSJeremy Fitzhardinge 		if (buf < end)
5311da177e4SLinus Torvalds 			*buf = '0';
5321da177e4SLinus Torvalds 		++buf;
5331da177e4SLinus Torvalds 	}
534b39a7340SDenis Vlasenko 	/* actual digits of result */
535b39a7340SDenis Vlasenko 	while (--i >= 0) {
536f796937aSJeremy Fitzhardinge 		if (buf < end)
5371da177e4SLinus Torvalds 			*buf = tmp[i];
5381da177e4SLinus Torvalds 		++buf;
5391da177e4SLinus Torvalds 	}
540b39a7340SDenis Vlasenko 	/* trailing space padding */
541fef20d9cSFrederic Weisbecker 	while (--spec.field_width >= 0) {
542f796937aSJeremy Fitzhardinge 		if (buf < end)
5431da177e4SLinus Torvalds 			*buf = ' ';
5441da177e4SLinus Torvalds 		++buf;
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 	return buf;
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
549fef20d9cSFrederic Weisbecker static char *string(char *buf, char *end, char *s, struct printf_spec spec)
5500f9bfa56SLinus Torvalds {
5510f9bfa56SLinus Torvalds 	int len, i;
5520f9bfa56SLinus Torvalds 
5530f9bfa56SLinus Torvalds 	if ((unsigned long)s < PAGE_SIZE)
5540f9bfa56SLinus Torvalds 		s = "<NULL>";
5550f9bfa56SLinus Torvalds 
556fef20d9cSFrederic Weisbecker 	len = strnlen(s, spec.precision);
5570f9bfa56SLinus Torvalds 
558fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
559fef20d9cSFrederic Weisbecker 		while (len < spec.field_width--) {
5600f9bfa56SLinus Torvalds 			if (buf < end)
5610f9bfa56SLinus Torvalds 				*buf = ' ';
5620f9bfa56SLinus Torvalds 			++buf;
5630f9bfa56SLinus Torvalds 		}
5640f9bfa56SLinus Torvalds 	}
5650f9bfa56SLinus Torvalds 	for (i = 0; i < len; ++i) {
5660f9bfa56SLinus Torvalds 		if (buf < end)
5670f9bfa56SLinus Torvalds 			*buf = *s;
5680f9bfa56SLinus Torvalds 		++buf; ++s;
5690f9bfa56SLinus Torvalds 	}
570fef20d9cSFrederic Weisbecker 	while (len < spec.field_width--) {
5710f9bfa56SLinus Torvalds 		if (buf < end)
5720f9bfa56SLinus Torvalds 			*buf = ' ';
5730f9bfa56SLinus Torvalds 		++buf;
5740f9bfa56SLinus Torvalds 	}
5750f9bfa56SLinus Torvalds 	return buf;
5760f9bfa56SLinus Torvalds }
5770f9bfa56SLinus Torvalds 
578fef20d9cSFrederic Weisbecker static char *symbol_string(char *buf, char *end, void *ptr,
5790c8b946eSFrederic Weisbecker 				struct printf_spec spec, char ext)
5800fe1ef24SLinus Torvalds {
5810fe1ef24SLinus Torvalds 	unsigned long value = (unsigned long) ptr;
5820fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
5830fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
5840c8b946eSFrederic Weisbecker 	if (ext != 'f')
5850fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
5860c8b946eSFrederic Weisbecker 	else
5870c8b946eSFrederic Weisbecker 		kallsyms_lookup(value, NULL, NULL, NULL, sym);
588fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
5890fe1ef24SLinus Torvalds #else
590fef20d9cSFrederic Weisbecker 	spec.field_width = 2*sizeof(void *);
591fef20d9cSFrederic Weisbecker 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
592fef20d9cSFrederic Weisbecker 	spec.base = 16;
593fef20d9cSFrederic Weisbecker 	return number(buf, end, value, spec);
5940fe1ef24SLinus Torvalds #endif
5950fe1ef24SLinus Torvalds }
5960fe1ef24SLinus Torvalds 
597fef20d9cSFrederic Weisbecker static char *resource_string(char *buf, char *end, struct resource *res,
598fef20d9cSFrederic Weisbecker 				struct printf_spec spec)
599332d2e78SLinus Torvalds {
600332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
601332d2e78SLinus Torvalds #define IO_RSRC_PRINTK_SIZE	4
602332d2e78SLinus Torvalds #endif
603332d2e78SLinus Torvalds 
604332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
605332d2e78SLinus Torvalds #define MEM_RSRC_PRINTK_SIZE	8
606332d2e78SLinus Torvalds #endif
607fef20d9cSFrederic Weisbecker 	struct printf_spec num_spec = {
608fef20d9cSFrederic Weisbecker 		.base = 16,
609fef20d9cSFrederic Weisbecker 		.precision = -1,
610fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
611fef20d9cSFrederic Weisbecker 	};
612332d2e78SLinus Torvalds 	/* room for the actual numbers, the two "0x", -, [, ] and the final zero */
613332d2e78SLinus Torvalds 	char sym[4*sizeof(resource_size_t) + 8];
614332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
615332d2e78SLinus Torvalds 	int size = -1;
616332d2e78SLinus Torvalds 
617332d2e78SLinus Torvalds 	if (res->flags & IORESOURCE_IO)
618332d2e78SLinus Torvalds 		size = IO_RSRC_PRINTK_SIZE;
619332d2e78SLinus Torvalds 	else if (res->flags & IORESOURCE_MEM)
620332d2e78SLinus Torvalds 		size = MEM_RSRC_PRINTK_SIZE;
621332d2e78SLinus Torvalds 
622332d2e78SLinus Torvalds 	*p++ = '[';
623fef20d9cSFrederic Weisbecker 	num_spec.field_width = size;
624fef20d9cSFrederic Weisbecker 	p = number(p, pend, res->start, num_spec);
625332d2e78SLinus Torvalds 	*p++ = '-';
626fef20d9cSFrederic Weisbecker 	p = number(p, pend, res->end, num_spec);
627332d2e78SLinus Torvalds 	*p++ = ']';
628332d2e78SLinus Torvalds 	*p = 0;
629332d2e78SLinus Torvalds 
630fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
631332d2e78SLinus Torvalds }
632332d2e78SLinus Torvalds 
633fef20d9cSFrederic Weisbecker static char *mac_address_string(char *buf, char *end, u8 *addr,
634*8a27f7c9SJoe Perches 				struct printf_spec spec, const char *fmt)
635dd45c9cfSHarvey Harrison {
636*8a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
637dd45c9cfSHarvey Harrison 	char *p = mac_addr;
638dd45c9cfSHarvey Harrison 	int i;
639dd45c9cfSHarvey Harrison 
640dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
641dd45c9cfSHarvey Harrison 		p = pack_hex_byte(p, addr[i]);
642*8a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
643dd45c9cfSHarvey Harrison 			*p++ = ':';
644dd45c9cfSHarvey Harrison 	}
645dd45c9cfSHarvey Harrison 	*p = '\0';
646dd45c9cfSHarvey Harrison 
647fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
648dd45c9cfSHarvey Harrison }
649dd45c9cfSHarvey Harrison 
650*8a27f7c9SJoe Perches static char *ip4_string(char *p, const u8 *addr, bool leading_zeros)
651689afa7dSHarvey Harrison {
652689afa7dSHarvey Harrison 	int i;
653689afa7dSHarvey Harrison 
654*8a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
655*8a27f7c9SJoe Perches 		char temp[3];	/* hold each IP quad in reverse order */
656*8a27f7c9SJoe Perches 		int digits = put_dec_trunc(temp, addr[i]) - temp;
657*8a27f7c9SJoe Perches 		if (leading_zeros) {
658*8a27f7c9SJoe Perches 			if (digits < 3)
659*8a27f7c9SJoe Perches 				*p++ = '0';
660*8a27f7c9SJoe Perches 			if (digits < 2)
661*8a27f7c9SJoe Perches 				*p++ = '0';
662*8a27f7c9SJoe Perches 		}
663*8a27f7c9SJoe Perches 		/* reverse the digits in the quad */
664*8a27f7c9SJoe Perches 		while (digits--)
665*8a27f7c9SJoe Perches 			*p++ = temp[digits];
666*8a27f7c9SJoe Perches 		if (i < 3)
667*8a27f7c9SJoe Perches 			*p++ = '.';
668*8a27f7c9SJoe Perches 	}
669*8a27f7c9SJoe Perches 
670*8a27f7c9SJoe Perches 	*p = '\0';
671*8a27f7c9SJoe Perches 	return p;
672*8a27f7c9SJoe Perches }
673*8a27f7c9SJoe Perches 
674*8a27f7c9SJoe Perches static char *ip6_compressed_string(char *p, const struct in6_addr *addr)
675*8a27f7c9SJoe Perches {
676*8a27f7c9SJoe Perches 	int i;
677*8a27f7c9SJoe Perches 	int j;
678*8a27f7c9SJoe Perches 	int range;
679*8a27f7c9SJoe Perches 	unsigned char zerolength[8];
680*8a27f7c9SJoe Perches 	int longest = 1;
681*8a27f7c9SJoe Perches 	int colonpos = -1;
682*8a27f7c9SJoe Perches 	u16 word;
683*8a27f7c9SJoe Perches 	u8 hi;
684*8a27f7c9SJoe Perches 	u8 lo;
685*8a27f7c9SJoe Perches 	bool needcolon = false;
686*8a27f7c9SJoe Perches 	bool useIPv4 = ipv6_addr_v4mapped(addr) || ipv6_addr_is_isatap(addr);
687*8a27f7c9SJoe Perches 
688*8a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
689*8a27f7c9SJoe Perches 
690*8a27f7c9SJoe Perches 	if (useIPv4)
691*8a27f7c9SJoe Perches 		range = 6;
692*8a27f7c9SJoe Perches 	else
693*8a27f7c9SJoe Perches 		range = 8;
694*8a27f7c9SJoe Perches 
695*8a27f7c9SJoe Perches 	/* find position of longest 0 run */
696*8a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
697*8a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
698*8a27f7c9SJoe Perches 			if (addr->s6_addr16[j] != 0)
699*8a27f7c9SJoe Perches 				break;
700*8a27f7c9SJoe Perches 			zerolength[i]++;
701*8a27f7c9SJoe Perches 		}
702*8a27f7c9SJoe Perches 	}
703*8a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
704*8a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
705*8a27f7c9SJoe Perches 			longest = zerolength[i];
706*8a27f7c9SJoe Perches 			colonpos = i;
707*8a27f7c9SJoe Perches 		}
708*8a27f7c9SJoe Perches 	}
709*8a27f7c9SJoe Perches 
710*8a27f7c9SJoe Perches 	/* emit address */
711*8a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
712*8a27f7c9SJoe Perches 		if (i == colonpos) {
713*8a27f7c9SJoe Perches 			if (needcolon || i == 0)
714*8a27f7c9SJoe Perches 				*p++ = ':';
715*8a27f7c9SJoe Perches 			*p++ = ':';
716*8a27f7c9SJoe Perches 			needcolon = false;
717*8a27f7c9SJoe Perches 			i += longest - 1;
718*8a27f7c9SJoe Perches 			continue;
719*8a27f7c9SJoe Perches 		}
720*8a27f7c9SJoe Perches 		if (needcolon) {
721*8a27f7c9SJoe Perches 			*p++ = ':';
722*8a27f7c9SJoe Perches 			needcolon = false;
723*8a27f7c9SJoe Perches 		}
724*8a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
725*8a27f7c9SJoe Perches 		word = ntohs(addr->s6_addr16[i]);
726*8a27f7c9SJoe Perches 		hi = word >> 8;
727*8a27f7c9SJoe Perches 		lo = word & 0xff;
728*8a27f7c9SJoe Perches 		if (hi) {
729*8a27f7c9SJoe Perches 			if (hi > 0x0f)
730*8a27f7c9SJoe Perches 				p = pack_hex_byte(p, hi);
731*8a27f7c9SJoe Perches 			else
732*8a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
733*8a27f7c9SJoe Perches 		}
734*8a27f7c9SJoe Perches 		if (hi || lo > 0x0f)
735*8a27f7c9SJoe Perches 			p = pack_hex_byte(p, lo);
736*8a27f7c9SJoe Perches 		else
737*8a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
738*8a27f7c9SJoe Perches 		needcolon = true;
739*8a27f7c9SJoe Perches 	}
740*8a27f7c9SJoe Perches 
741*8a27f7c9SJoe Perches 	if (useIPv4) {
742*8a27f7c9SJoe Perches 		if (needcolon)
743*8a27f7c9SJoe Perches 			*p++ = ':';
744*8a27f7c9SJoe Perches 		p = ip4_string(p, &addr->s6_addr[12], false);
745*8a27f7c9SJoe Perches 	}
746*8a27f7c9SJoe Perches 
747*8a27f7c9SJoe Perches 	*p = '\0';
748*8a27f7c9SJoe Perches 	return p;
749*8a27f7c9SJoe Perches }
750*8a27f7c9SJoe Perches 
751*8a27f7c9SJoe Perches static char *ip6_string(char *p, const struct in6_addr *addr, const char *fmt)
752*8a27f7c9SJoe Perches {
753*8a27f7c9SJoe Perches 	int i;
754689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
755*8a27f7c9SJoe Perches 		p = pack_hex_byte(p, addr->s6_addr[2 * i]);
756*8a27f7c9SJoe Perches 		p = pack_hex_byte(p, addr->s6_addr[2 * i + 1]);
757*8a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
758689afa7dSHarvey Harrison 			*p++ = ':';
759689afa7dSHarvey Harrison 	}
760*8a27f7c9SJoe Perches 
761689afa7dSHarvey Harrison 	*p = '\0';
762*8a27f7c9SJoe Perches 	return p;
763*8a27f7c9SJoe Perches }
764*8a27f7c9SJoe Perches 
765*8a27f7c9SJoe Perches static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
766*8a27f7c9SJoe Perches 			     struct printf_spec spec, const char *fmt)
767*8a27f7c9SJoe Perches {
768*8a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
769*8a27f7c9SJoe Perches 
770*8a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
771*8a27f7c9SJoe Perches 		ip6_compressed_string(ip6_addr, (const struct in6_addr *)addr);
772*8a27f7c9SJoe Perches 	else
773*8a27f7c9SJoe Perches 		ip6_string(ip6_addr, (const struct in6_addr *)addr, fmt);
774689afa7dSHarvey Harrison 
775fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
776689afa7dSHarvey Harrison }
777689afa7dSHarvey Harrison 
778*8a27f7c9SJoe Perches static char *ip4_addr_string(char *buf, char *end, const u8 *addr,
779*8a27f7c9SJoe Perches 			     struct printf_spec spec, const char *fmt)
7804aa99606SHarvey Harrison {
781*8a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
7824aa99606SHarvey Harrison 
783*8a27f7c9SJoe Perches 	ip4_string(ip4_addr, addr, fmt[0] == 'i');
7844aa99606SHarvey Harrison 
785fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
7864aa99606SHarvey Harrison }
7874aa99606SHarvey Harrison 
7884d8a743cSLinus Torvalds /*
7894d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
7904d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
7914d8a743cSLinus Torvalds  * specifiers.
7924d8a743cSLinus Torvalds  *
793332d2e78SLinus Torvalds  * Right now we handle:
7940fe1ef24SLinus Torvalds  *
7950c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
7960c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
797332d2e78SLinus Torvalds  * - 'S' For symbolic direct pointers
798332d2e78SLinus Torvalds  * - 'R' For a struct resource pointer, it prints the range of
799332d2e78SLinus Torvalds  *       addresses (not the name nor the flags)
800dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
801dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
802*8a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
803*8a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
804*8a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
805*8a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
806*8a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
807*8a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
808*8a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
809*8a27f7c9SJoe Perches  * - 'I6c' for IPv6 addresses printed as specified by
810*8a27f7c9SJoe Perches  *       http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
811332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
812332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
813332d2e78SLinus Torvalds  * pointer to the real address.
8144d8a743cSLinus Torvalds  */
815fef20d9cSFrederic Weisbecker static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
816fef20d9cSFrederic Weisbecker 			struct printf_spec spec)
81778a8bf69SLinus Torvalds {
818d97106abSLinus Torvalds 	if (!ptr)
819fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
820d97106abSLinus Torvalds 
8210fe1ef24SLinus Torvalds 	switch (*fmt) {
8220fe1ef24SLinus Torvalds 	case 'F':
8230c8b946eSFrederic Weisbecker 	case 'f':
8240fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
8250fe1ef24SLinus Torvalds 		/* Fallthrough */
8260fe1ef24SLinus Torvalds 	case 'S':
8270c8b946eSFrederic Weisbecker 		return symbol_string(buf, end, ptr, spec, *fmt);
828332d2e78SLinus Torvalds 	case 'R':
829fef20d9cSFrederic Weisbecker 		return resource_string(buf, end, ptr, spec);
830*8a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
831*8a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
832*8a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
833*8a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
834*8a27f7c9SJoe Perches 					 * 4:	1.2.3.4
835*8a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
836*8a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
837*8a27f7c9SJoe Perches 					 */
838*8a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
839*8a27f7c9SJoe Perches 					 * 4:	001.002.003.004
840*8a27f7c9SJoe Perches 					 * 6:   000102...0f
841*8a27f7c9SJoe Perches 					 */
842*8a27f7c9SJoe Perches 		switch (fmt[1]) {
843*8a27f7c9SJoe Perches 		case '6':
844*8a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
845*8a27f7c9SJoe Perches 		case '4':
846*8a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
847*8a27f7c9SJoe Perches 		}
8484aa99606SHarvey Harrison 		break;
8490fe1ef24SLinus Torvalds 	}
850fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
851fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
852fef20d9cSFrederic Weisbecker 		spec.field_width = 2*sizeof(void *);
853fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
85478a8bf69SLinus Torvalds 	}
855fef20d9cSFrederic Weisbecker 	spec.base = 16;
856fef20d9cSFrederic Weisbecker 
857fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
858fef20d9cSFrederic Weisbecker }
859fef20d9cSFrederic Weisbecker 
860fef20d9cSFrederic Weisbecker /*
861fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
862fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
863fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
864fef20d9cSFrederic Weisbecker  * to go on the next call).
865fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
866fef20d9cSFrederic Weisbecker  *
867fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
868fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
869fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
870fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
871fef20d9cSFrederic Weisbecker  *
872fef20d9cSFrederic Weisbecker  * @fmt: the format string
873fef20d9cSFrederic Weisbecker  * @type of the token returned
874fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
875fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
876fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
877fef20d9cSFrederic Weisbecker  * @precision: precision of a number
878fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
879fef20d9cSFrederic Weisbecker  */
880fef20d9cSFrederic Weisbecker static int format_decode(const char *fmt, struct printf_spec *spec)
881fef20d9cSFrederic Weisbecker {
882fef20d9cSFrederic Weisbecker 	const char *start = fmt;
883fef20d9cSFrederic Weisbecker 
884fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
885ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
886fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
887fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
888fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
889fef20d9cSFrederic Weisbecker 		}
890fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
891fef20d9cSFrederic Weisbecker 		goto precision;
892fef20d9cSFrederic Weisbecker 	}
893fef20d9cSFrederic Weisbecker 
894fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
895fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
896fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
897fef20d9cSFrederic Weisbecker 			spec->precision = 0;
898fef20d9cSFrederic Weisbecker 
899fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
900fef20d9cSFrederic Weisbecker 		goto qualifier;
901fef20d9cSFrederic Weisbecker 	}
902fef20d9cSFrederic Weisbecker 
903fef20d9cSFrederic Weisbecker 	/* By default */
904fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
905fef20d9cSFrederic Weisbecker 
906fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
907fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
908fef20d9cSFrederic Weisbecker 			break;
909fef20d9cSFrederic Weisbecker 	}
910fef20d9cSFrederic Weisbecker 
911fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
912fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
913fef20d9cSFrederic Weisbecker 		return fmt - start;
914fef20d9cSFrederic Weisbecker 
915fef20d9cSFrederic Weisbecker 	/* Process flags */
916fef20d9cSFrederic Weisbecker 	spec->flags = 0;
917fef20d9cSFrederic Weisbecker 
918fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
919fef20d9cSFrederic Weisbecker 		bool found = true;
920fef20d9cSFrederic Weisbecker 
921fef20d9cSFrederic Weisbecker 		++fmt;
922fef20d9cSFrederic Weisbecker 
923fef20d9cSFrederic Weisbecker 		switch (*fmt) {
924fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
925fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
926fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
927fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
928fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
929fef20d9cSFrederic Weisbecker 		default:  found = false;
930fef20d9cSFrederic Weisbecker 		}
931fef20d9cSFrederic Weisbecker 
932fef20d9cSFrederic Weisbecker 		if (!found)
933fef20d9cSFrederic Weisbecker 			break;
934fef20d9cSFrederic Weisbecker 	}
935fef20d9cSFrederic Weisbecker 
936fef20d9cSFrederic Weisbecker 	/* get field width */
937fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
938fef20d9cSFrederic Weisbecker 
939fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
940fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
941fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
942fef20d9cSFrederic Weisbecker 		/* it's the next argument */
943ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
944fef20d9cSFrederic Weisbecker 		return ++fmt - start;
945fef20d9cSFrederic Weisbecker 	}
946fef20d9cSFrederic Weisbecker 
947fef20d9cSFrederic Weisbecker precision:
948fef20d9cSFrederic Weisbecker 	/* get the precision */
949fef20d9cSFrederic Weisbecker 	spec->precision = -1;
950fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
951fef20d9cSFrederic Weisbecker 		++fmt;
952fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
953fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
954fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
955fef20d9cSFrederic Weisbecker 				spec->precision = 0;
956fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
957fef20d9cSFrederic Weisbecker 			/* it's the next argument */
958adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
959fef20d9cSFrederic Weisbecker 			return ++fmt - start;
960fef20d9cSFrederic Weisbecker 		}
961fef20d9cSFrederic Weisbecker 	}
962fef20d9cSFrederic Weisbecker 
963fef20d9cSFrederic Weisbecker qualifier:
964fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
965fef20d9cSFrederic Weisbecker 	spec->qualifier = -1;
966fef20d9cSFrederic Weisbecker 	if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
967fef20d9cSFrederic Weisbecker 	    *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
968a4e94ef0SZhaolei 		spec->qualifier = *fmt++;
969a4e94ef0SZhaolei 		if (unlikely(spec->qualifier == *fmt)) {
970a4e94ef0SZhaolei 			if (spec->qualifier == 'l') {
971fef20d9cSFrederic Weisbecker 				spec->qualifier = 'L';
972fef20d9cSFrederic Weisbecker 				++fmt;
973a4e94ef0SZhaolei 			} else if (spec->qualifier == 'h') {
974a4e94ef0SZhaolei 				spec->qualifier = 'H';
975a4e94ef0SZhaolei 				++fmt;
976a4e94ef0SZhaolei 			}
977fef20d9cSFrederic Weisbecker 		}
978fef20d9cSFrederic Weisbecker 	}
979fef20d9cSFrederic Weisbecker 
980fef20d9cSFrederic Weisbecker 	/* default base */
981fef20d9cSFrederic Weisbecker 	spec->base = 10;
982fef20d9cSFrederic Weisbecker 	switch (*fmt) {
983fef20d9cSFrederic Weisbecker 	case 'c':
984fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
985fef20d9cSFrederic Weisbecker 		return ++fmt - start;
986fef20d9cSFrederic Weisbecker 
987fef20d9cSFrederic Weisbecker 	case 's':
988fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
989fef20d9cSFrederic Weisbecker 		return ++fmt - start;
990fef20d9cSFrederic Weisbecker 
991fef20d9cSFrederic Weisbecker 	case 'p':
992fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
993fef20d9cSFrederic Weisbecker 		return fmt - start;
994fef20d9cSFrederic Weisbecker 		/* skip alnum */
995fef20d9cSFrederic Weisbecker 
996fef20d9cSFrederic Weisbecker 	case 'n':
997fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NRCHARS;
998fef20d9cSFrederic Weisbecker 		return ++fmt - start;
999fef20d9cSFrederic Weisbecker 
1000fef20d9cSFrederic Weisbecker 	case '%':
1001fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1002fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1003fef20d9cSFrederic Weisbecker 
1004fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1005fef20d9cSFrederic Weisbecker 	case 'o':
1006fef20d9cSFrederic Weisbecker 		spec->base = 8;
1007fef20d9cSFrederic Weisbecker 		break;
1008fef20d9cSFrederic Weisbecker 
1009fef20d9cSFrederic Weisbecker 	case 'x':
1010fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1011fef20d9cSFrederic Weisbecker 
1012fef20d9cSFrederic Weisbecker 	case 'X':
1013fef20d9cSFrederic Weisbecker 		spec->base = 16;
1014fef20d9cSFrederic Weisbecker 		break;
1015fef20d9cSFrederic Weisbecker 
1016fef20d9cSFrederic Weisbecker 	case 'd':
1017fef20d9cSFrederic Weisbecker 	case 'i':
101839e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1019fef20d9cSFrederic Weisbecker 	case 'u':
1020fef20d9cSFrederic Weisbecker 		break;
1021fef20d9cSFrederic Weisbecker 
1022fef20d9cSFrederic Weisbecker 	default:
1023fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1024fef20d9cSFrederic Weisbecker 		return fmt - start;
1025fef20d9cSFrederic Weisbecker 	}
1026fef20d9cSFrederic Weisbecker 
1027fef20d9cSFrederic Weisbecker 	if (spec->qualifier == 'L')
1028fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1029fef20d9cSFrederic Weisbecker 	else if (spec->qualifier == 'l') {
103039e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1031fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_LONG;
1032fef20d9cSFrederic Weisbecker 		else
1033fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_ULONG;
1034fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
1035fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1036fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 't') {
1037fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1038a4e94ef0SZhaolei 	} else if (spec->qualifier == 'H') {
1039a4e94ef0SZhaolei 		if (spec->flags & SIGN)
1040a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_BYTE;
1041a4e94ef0SZhaolei 		else
1042a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_UBYTE;
1043fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'h') {
104439e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1045fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_SHORT;
1046fef20d9cSFrederic Weisbecker 		else
1047fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_USHORT;
1048fef20d9cSFrederic Weisbecker 	} else {
104939e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1050fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_INT;
1051fef20d9cSFrederic Weisbecker 		else
1052fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_UINT;
1053fef20d9cSFrederic Weisbecker 	}
1054fef20d9cSFrederic Weisbecker 
1055fef20d9cSFrederic Weisbecker 	return ++fmt - start;
105678a8bf69SLinus Torvalds }
105778a8bf69SLinus Torvalds 
10581da177e4SLinus Torvalds /**
10591da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
10601da177e4SLinus Torvalds  * @buf: The buffer to place the result into
10611da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
10621da177e4SLinus Torvalds  * @fmt: The format string to use
10631da177e4SLinus Torvalds  * @args: Arguments for the format string
10641da177e4SLinus Torvalds  *
106520036fdcSAndi Kleen  * This function follows C99 vsnprintf, but has some extensions:
106620036fdcSAndi Kleen  * %pS output the name of a text symbol
10670c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
10680c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
1069332d2e78SLinus Torvalds  * %pR output the address range in a struct resource
107020036fdcSAndi Kleen  *
10711da177e4SLinus Torvalds  * The return value is the number of characters which would
10721da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
10731da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
10741da177e4SLinus Torvalds  * number of characters written into @buf as return value
107572fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
10761da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
10771da177e4SLinus Torvalds  * string is truncated.
10781da177e4SLinus Torvalds  *
10791da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
108072fd4a35SRobert P. J. Day  * You probably want snprintf() instead.
10811da177e4SLinus Torvalds  */
10821da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
10831da177e4SLinus Torvalds {
10841da177e4SLinus Torvalds 	unsigned long long num;
10851da177e4SLinus Torvalds 	char *str, *end, c;
1086fef20d9cSFrederic Weisbecker 	int read;
1087fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
10881da177e4SLinus Torvalds 
1089f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1090f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
10911da177e4SLinus Torvalds 	if (unlikely((int) size < 0)) {
10921da177e4SLinus Torvalds 		/* There can be only one.. */
1093b39a7340SDenis Vlasenko 		static char warn = 1;
10941da177e4SLinus Torvalds 		WARN_ON(warn);
10951da177e4SLinus Torvalds 		warn = 0;
10961da177e4SLinus Torvalds 		return 0;
10971da177e4SLinus Torvalds 	}
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	str = buf;
1100f796937aSJeremy Fitzhardinge 	end = buf + size;
11011da177e4SLinus Torvalds 
1102f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1103f796937aSJeremy Fitzhardinge 	if (end < buf) {
11041da177e4SLinus Torvalds 		end = ((void *)-1);
1105f796937aSJeremy Fitzhardinge 		size = end - buf;
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds 
1108fef20d9cSFrederic Weisbecker 	while (*fmt) {
1109fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1110fef20d9cSFrederic Weisbecker 
1111fef20d9cSFrederic Weisbecker 		read = format_decode(fmt, &spec);
1112fef20d9cSFrederic Weisbecker 
1113fef20d9cSFrederic Weisbecker 		fmt += read;
1114fef20d9cSFrederic Weisbecker 
1115fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1116fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1117fef20d9cSFrederic Weisbecker 			int copy = read;
1118fef20d9cSFrederic Weisbecker 			if (str < end) {
1119fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1120fef20d9cSFrederic Weisbecker 					copy = end - str;
1121fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1122fef20d9cSFrederic Weisbecker 			}
1123fef20d9cSFrederic Weisbecker 			str += read;
1124fef20d9cSFrederic Weisbecker 			break;
11251da177e4SLinus Torvalds 		}
11261da177e4SLinus Torvalds 
1127ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1128fef20d9cSFrederic Weisbecker 			spec.field_width = va_arg(args, int);
1129fef20d9cSFrederic Weisbecker 			break;
11301da177e4SLinus Torvalds 
1131fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1132fef20d9cSFrederic Weisbecker 			spec.precision = va_arg(args, int);
1133fef20d9cSFrederic Weisbecker 			break;
11341da177e4SLinus Torvalds 
1135fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
1136fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1137fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1138f796937aSJeremy Fitzhardinge 					if (str < end)
11391da177e4SLinus Torvalds 						*str = ' ';
11401da177e4SLinus Torvalds 					++str;
1141fef20d9cSFrederic Weisbecker 
11421da177e4SLinus Torvalds 				}
11431da177e4SLinus Torvalds 			}
11441da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1145f796937aSJeremy Fitzhardinge 			if (str < end)
11461da177e4SLinus Torvalds 				*str = c;
11471da177e4SLinus Torvalds 			++str;
1148fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1149f796937aSJeremy Fitzhardinge 				if (str < end)
11501da177e4SLinus Torvalds 					*str = ' ';
11511da177e4SLinus Torvalds 				++str;
11521da177e4SLinus Torvalds 			}
1153fef20d9cSFrederic Weisbecker 			break;
11541da177e4SLinus Torvalds 
1155fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1156fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1157fef20d9cSFrederic Weisbecker 			break;
11581da177e4SLinus Torvalds 
1159fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1160fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, va_arg(args, void *),
1161fef20d9cSFrederic Weisbecker 				      spec);
1162fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
11634d8a743cSLinus Torvalds 				fmt++;
1164fef20d9cSFrederic Weisbecker 			break;
11651da177e4SLinus Torvalds 
1166fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1167f796937aSJeremy Fitzhardinge 			if (str < end)
11681da177e4SLinus Torvalds 				*str = '%';
11691da177e4SLinus Torvalds 			++str;
11701da177e4SLinus Torvalds 			break;
11711da177e4SLinus Torvalds 
1172fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1173f796937aSJeremy Fitzhardinge 			if (str < end)
11741da177e4SLinus Torvalds 				*str = '%';
11751da177e4SLinus Torvalds 			++str;
1176fef20d9cSFrederic Weisbecker 			break;
1177fef20d9cSFrederic Weisbecker 
1178fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
1179fef20d9cSFrederic Weisbecker 			int qualifier = spec.qualifier;
1180fef20d9cSFrederic Weisbecker 
1181fef20d9cSFrederic Weisbecker 			if (qualifier == 'l') {
1182fef20d9cSFrederic Weisbecker 				long *ip = va_arg(args, long *);
1183fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
1184fef20d9cSFrederic Weisbecker 			} else if (qualifier == 'Z' ||
1185fef20d9cSFrederic Weisbecker 					qualifier == 'z') {
1186fef20d9cSFrederic Weisbecker 				size_t *ip = va_arg(args, size_t *);
1187fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
11881da177e4SLinus Torvalds 			} else {
1189fef20d9cSFrederic Weisbecker 				int *ip = va_arg(args, int *);
1190fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
1191fef20d9cSFrederic Weisbecker 			}
1192fef20d9cSFrederic Weisbecker 			break;
1193fef20d9cSFrederic Weisbecker 		}
1194fef20d9cSFrederic Weisbecker 
1195fef20d9cSFrederic Weisbecker 		default:
1196fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1197fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1198fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1199fef20d9cSFrederic Weisbecker 				break;
1200fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1201fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
1202fef20d9cSFrederic Weisbecker 				break;
1203fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1204fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
1205fef20d9cSFrederic Weisbecker 				break;
1206fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1207fef20d9cSFrederic Weisbecker 				num = va_arg(args, size_t);
1208fef20d9cSFrederic Weisbecker 				break;
1209fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1210fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
1211fef20d9cSFrederic Weisbecker 				break;
1212a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1213a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
1214a4e94ef0SZhaolei 				break;
1215a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1216a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
1217a4e94ef0SZhaolei 				break;
1218fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1219fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
1220fef20d9cSFrederic Weisbecker 				break;
1221fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1222fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
1223fef20d9cSFrederic Weisbecker 				break;
122439e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
122539e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
1226fef20d9cSFrederic Weisbecker 				break;
1227fef20d9cSFrederic Weisbecker 			default:
1228fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
12291da177e4SLinus Torvalds 			}
1230fef20d9cSFrederic Weisbecker 
1231fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
12321da177e4SLinus Torvalds 		}
1233fef20d9cSFrederic Weisbecker 	}
1234fef20d9cSFrederic Weisbecker 
1235f796937aSJeremy Fitzhardinge 	if (size > 0) {
1236f796937aSJeremy Fitzhardinge 		if (str < end)
12371da177e4SLinus Torvalds 			*str = '\0';
1238f796937aSJeremy Fitzhardinge 		else
12390a6047eeSLinus Torvalds 			end[-1] = '\0';
1240f796937aSJeremy Fitzhardinge 	}
1241fef20d9cSFrederic Weisbecker 
1242f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
12431da177e4SLinus Torvalds 	return str-buf;
1244fef20d9cSFrederic Weisbecker 
12451da177e4SLinus Torvalds }
12461da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds /**
12491da177e4SLinus Torvalds  * vscnprintf - 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  *
12551da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
12561da177e4SLinus Torvalds  * the @buf not including the trailing '\0'. If @size is <= 0 the function
12571da177e4SLinus Torvalds  * returns 0.
12581da177e4SLinus Torvalds  *
12591da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
126072fd4a35SRobert P. J. Day  * You probably want scnprintf() instead.
126120036fdcSAndi Kleen  *
126220036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
12631da177e4SLinus Torvalds  */
12641da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
12651da177e4SLinus Torvalds {
12661da177e4SLinus Torvalds 	int i;
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 	i=vsnprintf(buf,size,fmt,args);
12691da177e4SLinus Torvalds 	return (i >= size) ? (size - 1) : i;
12701da177e4SLinus Torvalds }
12711da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds /**
12741da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
12751da177e4SLinus Torvalds  * @buf: The buffer to place the result into
12761da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
12771da177e4SLinus Torvalds  * @fmt: The format string to use
12781da177e4SLinus Torvalds  * @...: Arguments for the format string
12791da177e4SLinus Torvalds  *
12801da177e4SLinus Torvalds  * The return value is the number of characters which would be
12811da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
12821da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
12831da177e4SLinus Torvalds  * @size, the resulting string is truncated.
128420036fdcSAndi Kleen  *
128520036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
12861da177e4SLinus Torvalds  */
12871da177e4SLinus Torvalds int snprintf(char * buf, size_t size, const char *fmt, ...)
12881da177e4SLinus Torvalds {
12891da177e4SLinus Torvalds 	va_list args;
12901da177e4SLinus Torvalds 	int i;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	va_start(args, fmt);
12931da177e4SLinus Torvalds 	i=vsnprintf(buf,size,fmt,args);
12941da177e4SLinus Torvalds 	va_end(args);
12951da177e4SLinus Torvalds 	return i;
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds /**
13001da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
13011da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13021da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
13031da177e4SLinus Torvalds  * @fmt: The format string to use
13041da177e4SLinus Torvalds  * @...: Arguments for the format string
13051da177e4SLinus Torvalds  *
13061da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
1307ea6f3281SMartin Peschke  * the trailing '\0'. If @size is <= 0 the function returns 0.
13081da177e4SLinus Torvalds  */
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds int scnprintf(char * buf, size_t size, const char *fmt, ...)
13111da177e4SLinus Torvalds {
13121da177e4SLinus Torvalds 	va_list args;
13131da177e4SLinus Torvalds 	int i;
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	va_start(args, fmt);
13161da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
13171da177e4SLinus Torvalds 	va_end(args);
13181da177e4SLinus Torvalds 	return (i >= size) ? (size - 1) : i;
13191da177e4SLinus Torvalds }
13201da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds /**
13231da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
13241da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13251da177e4SLinus Torvalds  * @fmt: The format string to use
13261da177e4SLinus Torvalds  * @args: Arguments for the format string
13271da177e4SLinus Torvalds  *
13281da177e4SLinus Torvalds  * The function returns the number of characters written
132972fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
13301da177e4SLinus Torvalds  * buffer overflows.
13311da177e4SLinus Torvalds  *
13321da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
133372fd4a35SRobert P. J. Day  * You probably want sprintf() instead.
133420036fdcSAndi Kleen  *
133520036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
13361da177e4SLinus Torvalds  */
13371da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
13381da177e4SLinus Torvalds {
13391da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds /**
13441da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
13451da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13461da177e4SLinus Torvalds  * @fmt: The format string to use
13471da177e4SLinus Torvalds  * @...: Arguments for the format string
13481da177e4SLinus Torvalds  *
13491da177e4SLinus Torvalds  * The function returns the number of characters written
135072fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
13511da177e4SLinus Torvalds  * buffer overflows.
135220036fdcSAndi Kleen  *
135320036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
13541da177e4SLinus Torvalds  */
13551da177e4SLinus Torvalds int sprintf(char * buf, const char *fmt, ...)
13561da177e4SLinus Torvalds {
13571da177e4SLinus Torvalds 	va_list args;
13581da177e4SLinus Torvalds 	int i;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	va_start(args, fmt);
13611da177e4SLinus Torvalds 	i=vsnprintf(buf, INT_MAX, fmt, args);
13621da177e4SLinus Torvalds 	va_end(args);
13631da177e4SLinus Torvalds 	return i;
13641da177e4SLinus Torvalds }
13651da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
13661da177e4SLinus Torvalds 
13674370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
13684370aa4aSLai Jiangshan /*
13694370aa4aSLai Jiangshan  * bprintf service:
13704370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
13714370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
13724370aa4aSLai Jiangshan  */
13734370aa4aSLai Jiangshan 
13744370aa4aSLai Jiangshan /**
13754370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
13764370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
13774370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
13784370aa4aSLai Jiangshan  * @fmt: The format string to use
13794370aa4aSLai Jiangshan  * @args: Arguments for the format string
13804370aa4aSLai Jiangshan  *
13814370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
13824370aa4aSLai Jiangshan  * is skiped.
13834370aa4aSLai Jiangshan  *
13844370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
13854370aa4aSLai Jiangshan  * the given input.
13864370aa4aSLai Jiangshan  *
13874370aa4aSLai Jiangshan  * NOTE:
13884370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
13894370aa4aSLai Jiangshan  * valid for bstr_printf().
13904370aa4aSLai Jiangshan  */
13914370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
13924370aa4aSLai Jiangshan {
1393fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
13944370aa4aSLai Jiangshan 	char *str, *end;
1395fef20d9cSFrederic Weisbecker 	int read;
13964370aa4aSLai Jiangshan 
13974370aa4aSLai Jiangshan 	str = (char *)bin_buf;
13984370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
13994370aa4aSLai Jiangshan 
14004370aa4aSLai Jiangshan #define save_arg(type)							\
14014370aa4aSLai Jiangshan do {									\
14024370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
14034370aa4aSLai Jiangshan 		unsigned long long value;				\
14044370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
14054370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
14064370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
14074370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
14084370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
14094370aa4aSLai Jiangshan 		}							\
14104370aa4aSLai Jiangshan 	} else {							\
14114370aa4aSLai Jiangshan 		unsigned long value;					\
14124370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
14134370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
14144370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
14154370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
14164370aa4aSLai Jiangshan 	}								\
14174370aa4aSLai Jiangshan 	str += sizeof(type);						\
14184370aa4aSLai Jiangshan } while (0)
14194370aa4aSLai Jiangshan 
14204370aa4aSLai Jiangshan 
1421fef20d9cSFrederic Weisbecker 	while (*fmt) {
1422fef20d9cSFrederic Weisbecker 		read = format_decode(fmt, &spec);
14234370aa4aSLai Jiangshan 
1424fef20d9cSFrederic Weisbecker 		fmt += read;
1425fef20d9cSFrederic Weisbecker 
1426fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1427fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
1428fef20d9cSFrederic Weisbecker 			break;
1429fef20d9cSFrederic Weisbecker 
1430ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1431fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
14324370aa4aSLai Jiangshan 			save_arg(int);
1433fef20d9cSFrederic Weisbecker 			break;
14344370aa4aSLai Jiangshan 
1435fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
14364370aa4aSLai Jiangshan 			save_arg(char);
1437fef20d9cSFrederic Weisbecker 			break;
1438fef20d9cSFrederic Weisbecker 
1439fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
14404370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
14414370aa4aSLai Jiangshan 			size_t len;
14424370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
14434370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
14444370aa4aSLai Jiangshan 				save_str = "<NULL>";
14454370aa4aSLai Jiangshan 			len = strlen(save_str);
14464370aa4aSLai Jiangshan 			if (str + len + 1 < end)
14474370aa4aSLai Jiangshan 				memcpy(str, save_str, len + 1);
14484370aa4aSLai Jiangshan 			str += len + 1;
1449fef20d9cSFrederic Weisbecker 			break;
14504370aa4aSLai Jiangshan 		}
1451fef20d9cSFrederic Weisbecker 
1452fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
14534370aa4aSLai Jiangshan 			save_arg(void *);
14544370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
1455fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
14564370aa4aSLai Jiangshan 				fmt++;
1457fef20d9cSFrederic Weisbecker 			break;
1458fef20d9cSFrederic Weisbecker 
1459fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1460fef20d9cSFrederic Weisbecker 			break;
1461fef20d9cSFrederic Weisbecker 
1462fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1463fef20d9cSFrederic Weisbecker 			break;
1464fef20d9cSFrederic Weisbecker 
1465fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
14664370aa4aSLai Jiangshan 			/* skip %n 's argument */
1467fef20d9cSFrederic Weisbecker 			int qualifier = spec.qualifier;
14684370aa4aSLai Jiangshan 			void *skip_arg;
14694370aa4aSLai Jiangshan 			if (qualifier == 'l')
14704370aa4aSLai Jiangshan 				skip_arg = va_arg(args, long *);
14714370aa4aSLai Jiangshan 			else if (qualifier == 'Z' || qualifier == 'z')
14724370aa4aSLai Jiangshan 				skip_arg = va_arg(args, size_t *);
14734370aa4aSLai Jiangshan 			else
14744370aa4aSLai Jiangshan 				skip_arg = va_arg(args, int *);
1475fef20d9cSFrederic Weisbecker 			break;
14764370aa4aSLai Jiangshan 		}
14774370aa4aSLai Jiangshan 
1478fef20d9cSFrederic Weisbecker 		default:
1479fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1480fef20d9cSFrederic Weisbecker 
1481fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1482fef20d9cSFrederic Weisbecker 				save_arg(long long);
1483fef20d9cSFrederic Weisbecker 				break;
1484fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1485fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1486fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
1487fef20d9cSFrederic Weisbecker 				break;
1488fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1489fef20d9cSFrederic Weisbecker 				save_arg(size_t);
1490fef20d9cSFrederic Weisbecker 				break;
1491fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1492fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
1493fef20d9cSFrederic Weisbecker 				break;
1494a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1495a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1496a4e94ef0SZhaolei 				save_arg(char);
1497a4e94ef0SZhaolei 				break;
1498fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1499fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1500fef20d9cSFrederic Weisbecker 				save_arg(short);
1501fef20d9cSFrederic Weisbecker 				break;
1502fef20d9cSFrederic Weisbecker 			default:
1503fef20d9cSFrederic Weisbecker 				save_arg(int);
1504fef20d9cSFrederic Weisbecker 			}
1505fef20d9cSFrederic Weisbecker 		}
1506fef20d9cSFrederic Weisbecker 	}
15074370aa4aSLai Jiangshan 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1508fef20d9cSFrederic Weisbecker 
1509fef20d9cSFrederic Weisbecker #undef save_arg
15104370aa4aSLai Jiangshan }
15114370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
15124370aa4aSLai Jiangshan 
15134370aa4aSLai Jiangshan /**
15144370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
15154370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
15164370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
15174370aa4aSLai Jiangshan  * @fmt: The format string to use
15184370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
15194370aa4aSLai Jiangshan  *
15204370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
15214370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
15224370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
15234370aa4aSLai Jiangshan  *
15244370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
15254370aa4aSLai Jiangshan  * %pS output the name of a text symbol
15260c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
15270c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
15284370aa4aSLai Jiangshan  * %pR output the address range in a struct resource
15294370aa4aSLai Jiangshan  * %n is ignored
15304370aa4aSLai Jiangshan  *
15314370aa4aSLai Jiangshan  * The return value is the number of characters which would
15324370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
15334370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
15344370aa4aSLai Jiangshan  * number of characters written into @buf as return value
15354370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
15364370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
15374370aa4aSLai Jiangshan  * string is truncated.
15384370aa4aSLai Jiangshan  */
15394370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
15404370aa4aSLai Jiangshan {
15414370aa4aSLai Jiangshan 	unsigned long long num;
15424370aa4aSLai Jiangshan 	char *str, *end, c;
15434370aa4aSLai Jiangshan 	const char *args = (const char *)bin_buf;
15444370aa4aSLai Jiangshan 
1545fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
15464370aa4aSLai Jiangshan 
15474370aa4aSLai Jiangshan 	if (unlikely((int) size < 0)) {
15484370aa4aSLai Jiangshan 		/* There can be only one.. */
15494370aa4aSLai Jiangshan 		static char warn = 1;
15504370aa4aSLai Jiangshan 		WARN_ON(warn);
15514370aa4aSLai Jiangshan 		warn = 0;
15524370aa4aSLai Jiangshan 		return 0;
15534370aa4aSLai Jiangshan 	}
15544370aa4aSLai Jiangshan 
15554370aa4aSLai Jiangshan 	str = buf;
15564370aa4aSLai Jiangshan 	end = buf + size;
15574370aa4aSLai Jiangshan 
15584370aa4aSLai Jiangshan #define get_arg(type)							\
15594370aa4aSLai Jiangshan ({									\
15604370aa4aSLai Jiangshan 	typeof(type) value;						\
15614370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
15624370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
15634370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
15644370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
15654370aa4aSLai Jiangshan 	} else {							\
15664370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
15674370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
15684370aa4aSLai Jiangshan 	}								\
15694370aa4aSLai Jiangshan 	args += sizeof(type);						\
15704370aa4aSLai Jiangshan 	value;								\
15714370aa4aSLai Jiangshan })
15724370aa4aSLai Jiangshan 
15734370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
15744370aa4aSLai Jiangshan 	if (end < buf) {
15754370aa4aSLai Jiangshan 		end = ((void *)-1);
15764370aa4aSLai Jiangshan 		size = end - buf;
15774370aa4aSLai Jiangshan 	}
15784370aa4aSLai Jiangshan 
1579fef20d9cSFrederic Weisbecker 	while (*fmt) {
1580fef20d9cSFrederic Weisbecker 		int read;
1581fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1582fef20d9cSFrederic Weisbecker 
1583fef20d9cSFrederic Weisbecker 		read = format_decode(fmt, &spec);
1584fef20d9cSFrederic Weisbecker 
1585fef20d9cSFrederic Weisbecker 		fmt += read;
1586fef20d9cSFrederic Weisbecker 
1587fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1588fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1589fef20d9cSFrederic Weisbecker 			int copy = read;
1590fef20d9cSFrederic Weisbecker 			if (str < end) {
1591fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1592fef20d9cSFrederic Weisbecker 					copy = end - str;
1593fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1594fef20d9cSFrederic Weisbecker 			}
1595fef20d9cSFrederic Weisbecker 			str += read;
1596fef20d9cSFrederic Weisbecker 			break;
15974370aa4aSLai Jiangshan 		}
15984370aa4aSLai Jiangshan 
1599ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1600fef20d9cSFrederic Weisbecker 			spec.field_width = get_arg(int);
1601fef20d9cSFrederic Weisbecker 			break;
16024370aa4aSLai Jiangshan 
1603fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1604fef20d9cSFrederic Weisbecker 			spec.precision = get_arg(int);
1605fef20d9cSFrederic Weisbecker 			break;
16064370aa4aSLai Jiangshan 
1607fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
1608fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1609fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
16104370aa4aSLai Jiangshan 					if (str < end)
16114370aa4aSLai Jiangshan 						*str = ' ';
16124370aa4aSLai Jiangshan 					++str;
16134370aa4aSLai Jiangshan 				}
16144370aa4aSLai Jiangshan 			}
16154370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
16164370aa4aSLai Jiangshan 			if (str < end)
16174370aa4aSLai Jiangshan 				*str = c;
16184370aa4aSLai Jiangshan 			++str;
1619fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
16204370aa4aSLai Jiangshan 				if (str < end)
16214370aa4aSLai Jiangshan 					*str = ' ';
16224370aa4aSLai Jiangshan 				++str;
16234370aa4aSLai Jiangshan 			}
1624fef20d9cSFrederic Weisbecker 			break;
16254370aa4aSLai Jiangshan 
1626fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
16274370aa4aSLai Jiangshan 			const char *str_arg = args;
16284370aa4aSLai Jiangshan 			size_t len = strlen(str_arg);
16294370aa4aSLai Jiangshan 			args += len + 1;
1630fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
1631fef20d9cSFrederic Weisbecker 			break;
16324370aa4aSLai Jiangshan 		}
16334370aa4aSLai Jiangshan 
1634fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1635fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, get_arg(void *), spec);
1636fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
16374370aa4aSLai Jiangshan 				fmt++;
1638fef20d9cSFrederic Weisbecker 			break;
16394370aa4aSLai Jiangshan 
1640fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
16414370aa4aSLai Jiangshan 			if (str < end)
16424370aa4aSLai Jiangshan 				*str = '%';
16434370aa4aSLai Jiangshan 			++str;
16444370aa4aSLai Jiangshan 			break;
16454370aa4aSLai Jiangshan 
1646fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
16474370aa4aSLai Jiangshan 			if (str < end)
16484370aa4aSLai Jiangshan 				*str = '%';
16494370aa4aSLai Jiangshan 			++str;
1650fef20d9cSFrederic Weisbecker 			break;
1651fef20d9cSFrederic Weisbecker 
1652fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS:
1653fef20d9cSFrederic Weisbecker 			/* skip */
1654fef20d9cSFrederic Weisbecker 			break;
1655fef20d9cSFrederic Weisbecker 
1656fef20d9cSFrederic Weisbecker 		default:
1657fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1658fef20d9cSFrederic Weisbecker 
1659fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
16604370aa4aSLai Jiangshan 				num = get_arg(long long);
1661fef20d9cSFrederic Weisbecker 				break;
1662fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
16634370aa4aSLai Jiangshan 				num = get_arg(unsigned long);
1664fef20d9cSFrederic Weisbecker 				break;
1665fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1666fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
1667fef20d9cSFrederic Weisbecker 				break;
1668fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
16694370aa4aSLai Jiangshan 				num = get_arg(size_t);
1670fef20d9cSFrederic Weisbecker 				break;
1671fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
16724370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
1673fef20d9cSFrederic Weisbecker 				break;
1674a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1675a4e94ef0SZhaolei 				num = get_arg(unsigned char);
1676a4e94ef0SZhaolei 				break;
1677a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1678a4e94ef0SZhaolei 				num = get_arg(signed char);
1679a4e94ef0SZhaolei 				break;
1680fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1681fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
1682fef20d9cSFrederic Weisbecker 				break;
1683fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1684fef20d9cSFrederic Weisbecker 				num = get_arg(short);
1685fef20d9cSFrederic Weisbecker 				break;
1686fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
16874370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
1688fef20d9cSFrederic Weisbecker 				break;
1689fef20d9cSFrederic Weisbecker 			default:
1690fef20d9cSFrederic Weisbecker 				num = get_arg(int);
16914370aa4aSLai Jiangshan 			}
1692fef20d9cSFrederic Weisbecker 
1693fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
16944370aa4aSLai Jiangshan 		}
1695fef20d9cSFrederic Weisbecker 	}
1696fef20d9cSFrederic Weisbecker 
16974370aa4aSLai Jiangshan 	if (size > 0) {
16984370aa4aSLai Jiangshan 		if (str < end)
16994370aa4aSLai Jiangshan 			*str = '\0';
17004370aa4aSLai Jiangshan 		else
17014370aa4aSLai Jiangshan 			end[-1] = '\0';
17024370aa4aSLai Jiangshan 	}
1703fef20d9cSFrederic Weisbecker 
17044370aa4aSLai Jiangshan #undef get_arg
17054370aa4aSLai Jiangshan 
17064370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
17074370aa4aSLai Jiangshan 	return str - buf;
17084370aa4aSLai Jiangshan }
17094370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
17104370aa4aSLai Jiangshan 
17114370aa4aSLai Jiangshan /**
17124370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
17134370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
17144370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
17154370aa4aSLai Jiangshan  * @fmt: The format string to use
17164370aa4aSLai Jiangshan  * @...: Arguments for the format string
17174370aa4aSLai Jiangshan  *
17184370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
17194370aa4aSLai Jiangshan  * into @bin_buf.
17204370aa4aSLai Jiangshan  */
17214370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
17224370aa4aSLai Jiangshan {
17234370aa4aSLai Jiangshan 	va_list args;
17244370aa4aSLai Jiangshan 	int ret;
17254370aa4aSLai Jiangshan 
17264370aa4aSLai Jiangshan 	va_start(args, fmt);
17274370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
17284370aa4aSLai Jiangshan 	va_end(args);
17294370aa4aSLai Jiangshan 	return ret;
17304370aa4aSLai Jiangshan }
17314370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
17324370aa4aSLai Jiangshan 
17334370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
17344370aa4aSLai Jiangshan 
17351da177e4SLinus Torvalds /**
17361da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
17371da177e4SLinus Torvalds  * @buf:	input buffer
17381da177e4SLinus Torvalds  * @fmt:	format of buffer
17391da177e4SLinus Torvalds  * @args:	arguments
17401da177e4SLinus Torvalds  */
17411da177e4SLinus Torvalds int vsscanf(const char * buf, const char * fmt, va_list args)
17421da177e4SLinus Torvalds {
17431da177e4SLinus Torvalds 	const char *str = buf;
17441da177e4SLinus Torvalds 	char *next;
17451da177e4SLinus Torvalds 	char digit;
17461da177e4SLinus Torvalds 	int num = 0;
17471da177e4SLinus Torvalds 	int qualifier;
17481da177e4SLinus Torvalds 	int base;
17491da177e4SLinus Torvalds 	int field_width;
17501da177e4SLinus Torvalds 	int is_sign = 0;
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	while(*fmt && *str) {
17531da177e4SLinus Torvalds 		/* skip any white space in format */
17541da177e4SLinus Torvalds 		/* white space in format matchs any amount of
17551da177e4SLinus Torvalds 		 * white space, including none, in the input.
17561da177e4SLinus Torvalds 		 */
17571da177e4SLinus Torvalds 		if (isspace(*fmt)) {
17581da177e4SLinus Torvalds 			while (isspace(*fmt))
17591da177e4SLinus Torvalds 				++fmt;
17601da177e4SLinus Torvalds 			while (isspace(*str))
17611da177e4SLinus Torvalds 				++str;
17621da177e4SLinus Torvalds 		}
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
17651da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
17661da177e4SLinus Torvalds 			if (*fmt++ != *str++)
17671da177e4SLinus Torvalds 				break;
17681da177e4SLinus Torvalds 			continue;
17691da177e4SLinus Torvalds 		}
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 		if (!*fmt)
17721da177e4SLinus Torvalds 			break;
17731da177e4SLinus Torvalds 		++fmt;
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds 		/* skip this conversion.
17761da177e4SLinus Torvalds 		 * advance both strings to next white space
17771da177e4SLinus Torvalds 		 */
17781da177e4SLinus Torvalds 		if (*fmt == '*') {
17791da177e4SLinus Torvalds 			while (!isspace(*fmt) && *fmt)
17801da177e4SLinus Torvalds 				fmt++;
17811da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
17821da177e4SLinus Torvalds 				str++;
17831da177e4SLinus Torvalds 			continue;
17841da177e4SLinus Torvalds 		}
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds 		/* get field width */
17871da177e4SLinus Torvalds 		field_width = -1;
17881da177e4SLinus Torvalds 		if (isdigit(*fmt))
17891da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds 		/* get conversion qualifier */
17921da177e4SLinus Torvalds 		qualifier = -1;
17931da177e4SLinus Torvalds 		if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
17941da177e4SLinus Torvalds 		    *fmt == 'Z' || *fmt == 'z') {
17951da177e4SLinus Torvalds 			qualifier = *fmt++;
17961da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
17971da177e4SLinus Torvalds 				if (qualifier == 'h') {
17981da177e4SLinus Torvalds 					qualifier = 'H';
17991da177e4SLinus Torvalds 					fmt++;
18001da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
18011da177e4SLinus Torvalds 					qualifier = 'L';
18021da177e4SLinus Torvalds 					fmt++;
18031da177e4SLinus Torvalds 				}
18041da177e4SLinus Torvalds 			}
18051da177e4SLinus Torvalds 		}
18061da177e4SLinus Torvalds 		base = 10;
18071da177e4SLinus Torvalds 		is_sign = 0;
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 		if (!*fmt || !*str)
18101da177e4SLinus Torvalds 			break;
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 		switch(*fmt++) {
18131da177e4SLinus Torvalds 		case 'c':
18141da177e4SLinus Torvalds 		{
18151da177e4SLinus Torvalds 			char *s = (char *) va_arg(args,char*);
18161da177e4SLinus Torvalds 			if (field_width == -1)
18171da177e4SLinus Torvalds 				field_width = 1;
18181da177e4SLinus Torvalds 			do {
18191da177e4SLinus Torvalds 				*s++ = *str++;
18201da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
18211da177e4SLinus Torvalds 			num++;
18221da177e4SLinus Torvalds 		}
18231da177e4SLinus Torvalds 		continue;
18241da177e4SLinus Torvalds 		case 's':
18251da177e4SLinus Torvalds 		{
18261da177e4SLinus Torvalds 			char *s = (char *) va_arg(args, char *);
18271da177e4SLinus Torvalds 			if(field_width == -1)
18281da177e4SLinus Torvalds 				field_width = INT_MAX;
18291da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
18301da177e4SLinus Torvalds 			while (isspace(*str))
18311da177e4SLinus Torvalds 				str++;
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 			/* now copy until next white space */
18341da177e4SLinus Torvalds 			while (*str && !isspace(*str) && field_width--) {
18351da177e4SLinus Torvalds 				*s++ = *str++;
18361da177e4SLinus Torvalds 			}
18371da177e4SLinus Torvalds 			*s = '\0';
18381da177e4SLinus Torvalds 			num++;
18391da177e4SLinus Torvalds 		}
18401da177e4SLinus Torvalds 		continue;
18411da177e4SLinus Torvalds 		case 'n':
18421da177e4SLinus Torvalds 			/* return number of characters read so far */
18431da177e4SLinus Torvalds 		{
18441da177e4SLinus Torvalds 			int *i = (int *)va_arg(args,int*);
18451da177e4SLinus Torvalds 			*i = str - buf;
18461da177e4SLinus Torvalds 		}
18471da177e4SLinus Torvalds 		continue;
18481da177e4SLinus Torvalds 		case 'o':
18491da177e4SLinus Torvalds 			base = 8;
18501da177e4SLinus Torvalds 			break;
18511da177e4SLinus Torvalds 		case 'x':
18521da177e4SLinus Torvalds 		case 'X':
18531da177e4SLinus Torvalds 			base = 16;
18541da177e4SLinus Torvalds 			break;
18551da177e4SLinus Torvalds 		case 'i':
18561da177e4SLinus Torvalds                         base = 0;
18571da177e4SLinus Torvalds 		case 'd':
18581da177e4SLinus Torvalds 			is_sign = 1;
18591da177e4SLinus Torvalds 		case 'u':
18601da177e4SLinus Torvalds 			break;
18611da177e4SLinus Torvalds 		case '%':
18621da177e4SLinus Torvalds 			/* looking for '%' in str */
18631da177e4SLinus Torvalds 			if (*str++ != '%')
18641da177e4SLinus Torvalds 				return num;
18651da177e4SLinus Torvalds 			continue;
18661da177e4SLinus Torvalds 		default:
18671da177e4SLinus Torvalds 			/* invalid format; stop here */
18681da177e4SLinus Torvalds 			return num;
18691da177e4SLinus Torvalds 		}
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 		/* have some sort of integer conversion.
18721da177e4SLinus Torvalds 		 * first, skip white space in buffer.
18731da177e4SLinus Torvalds 		 */
18741da177e4SLinus Torvalds 		while (isspace(*str))
18751da177e4SLinus Torvalds 			str++;
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 		digit = *str;
18781da177e4SLinus Torvalds 		if (is_sign && digit == '-')
18791da177e4SLinus Torvalds 			digit = *(str + 1);
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds 		if (!digit
18821da177e4SLinus Torvalds                     || (base == 16 && !isxdigit(digit))
18831da177e4SLinus Torvalds                     || (base == 10 && !isdigit(digit))
18841da177e4SLinus Torvalds                     || (base == 8 && (!isdigit(digit) || digit > '7'))
18851da177e4SLinus Torvalds                     || (base == 0 && !isdigit(digit)))
18861da177e4SLinus Torvalds 				break;
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds 		switch(qualifier) {
18891da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
18901da177e4SLinus Torvalds 			if (is_sign) {
18911da177e4SLinus Torvalds 				signed char *s = (signed char *) va_arg(args,signed char *);
18921da177e4SLinus Torvalds 				*s = (signed char) simple_strtol(str,&next,base);
18931da177e4SLinus Torvalds 			} else {
18941da177e4SLinus Torvalds 				unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
18951da177e4SLinus Torvalds 				*s = (unsigned char) simple_strtoul(str, &next, base);
18961da177e4SLinus Torvalds 			}
18971da177e4SLinus Torvalds 			break;
18981da177e4SLinus Torvalds 		case 'h':
18991da177e4SLinus Torvalds 			if (is_sign) {
19001da177e4SLinus Torvalds 				short *s = (short *) va_arg(args,short *);
19011da177e4SLinus Torvalds 				*s = (short) simple_strtol(str,&next,base);
19021da177e4SLinus Torvalds 			} else {
19031da177e4SLinus Torvalds 				unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
19041da177e4SLinus Torvalds 				*s = (unsigned short) simple_strtoul(str, &next, base);
19051da177e4SLinus Torvalds 			}
19061da177e4SLinus Torvalds 			break;
19071da177e4SLinus Torvalds 		case 'l':
19081da177e4SLinus Torvalds 			if (is_sign) {
19091da177e4SLinus Torvalds 				long *l = (long *) va_arg(args,long *);
19101da177e4SLinus Torvalds 				*l = simple_strtol(str,&next,base);
19111da177e4SLinus Torvalds 			} else {
19121da177e4SLinus Torvalds 				unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
19131da177e4SLinus Torvalds 				*l = simple_strtoul(str,&next,base);
19141da177e4SLinus Torvalds 			}
19151da177e4SLinus Torvalds 			break;
19161da177e4SLinus Torvalds 		case 'L':
19171da177e4SLinus Torvalds 			if (is_sign) {
19181da177e4SLinus Torvalds 				long long *l = (long long*) va_arg(args,long long *);
19191da177e4SLinus Torvalds 				*l = simple_strtoll(str,&next,base);
19201da177e4SLinus Torvalds 			} else {
19211da177e4SLinus Torvalds 				unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
19221da177e4SLinus Torvalds 				*l = simple_strtoull(str,&next,base);
19231da177e4SLinus Torvalds 			}
19241da177e4SLinus Torvalds 			break;
19251da177e4SLinus Torvalds 		case 'Z':
19261da177e4SLinus Torvalds 		case 'z':
19271da177e4SLinus Torvalds 		{
19281da177e4SLinus Torvalds 			size_t *s = (size_t*) va_arg(args,size_t*);
19291da177e4SLinus Torvalds 			*s = (size_t) simple_strtoul(str,&next,base);
19301da177e4SLinus Torvalds 		}
19311da177e4SLinus Torvalds 		break;
19321da177e4SLinus Torvalds 		default:
19331da177e4SLinus Torvalds 			if (is_sign) {
19341da177e4SLinus Torvalds 				int *i = (int *) va_arg(args, int*);
19351da177e4SLinus Torvalds 				*i = (int) simple_strtol(str,&next,base);
19361da177e4SLinus Torvalds 			} else {
19371da177e4SLinus Torvalds 				unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
19381da177e4SLinus Torvalds 				*i = (unsigned int) simple_strtoul(str,&next,base);
19391da177e4SLinus Torvalds 			}
19401da177e4SLinus Torvalds 			break;
19411da177e4SLinus Torvalds 		}
19421da177e4SLinus Torvalds 		num++;
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 		if (!next)
19451da177e4SLinus Torvalds 			break;
19461da177e4SLinus Torvalds 		str = next;
19471da177e4SLinus Torvalds 	}
1948c6b40d16SJohannes Berg 
1949c6b40d16SJohannes Berg 	/*
1950c6b40d16SJohannes Berg 	 * Now we've come all the way through so either the input string or the
1951c6b40d16SJohannes Berg 	 * format ended. In the former case, there can be a %n at the current
1952c6b40d16SJohannes Berg 	 * position in the format that needs to be filled.
1953c6b40d16SJohannes Berg 	 */
1954c6b40d16SJohannes Berg 	if (*fmt == '%' && *(fmt + 1) == 'n') {
1955c6b40d16SJohannes Berg 		int *p = (int *)va_arg(args, int *);
1956c6b40d16SJohannes Berg 		*p = str - buf;
1957c6b40d16SJohannes Berg 	}
1958c6b40d16SJohannes Berg 
19591da177e4SLinus Torvalds 	return num;
19601da177e4SLinus Torvalds }
19611da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds /**
19641da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
19651da177e4SLinus Torvalds  * @buf:	input buffer
19661da177e4SLinus Torvalds  * @fmt:	formatting of buffer
19671da177e4SLinus Torvalds  * @...:	resulting arguments
19681da177e4SLinus Torvalds  */
19691da177e4SLinus Torvalds int sscanf(const char * buf, const char * fmt, ...)
19701da177e4SLinus Torvalds {
19711da177e4SLinus Torvalds 	va_list args;
19721da177e4SLinus Torvalds 	int i;
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 	va_start(args,fmt);
19751da177e4SLinus Torvalds 	i = vsscanf(buf,fmt,args);
19761da177e4SLinus Torvalds 	va_end(args);
19771da177e4SLinus Torvalds 	return i;
19781da177e4SLinus Torvalds }
19791da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
1980