xref: /openbmc/linux/lib/vsprintf.c (revision 8fccae2c95506270f74ee8429c273b0924e89c83)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/lib/vsprintf.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds  * Wirzenius wrote this portably, Torvalds fucked it up :-)
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /*
131da177e4SLinus Torvalds  * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
141da177e4SLinus Torvalds  * - changed to provide snprintf and vsnprintf functions
151da177e4SLinus Torvalds  * So Feb  1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
161da177e4SLinus Torvalds  * - scnprintf and vscnprintf
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <stdarg.h>
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/types.h>
221da177e4SLinus Torvalds #include <linux/string.h>
231da177e4SLinus Torvalds #include <linux/ctype.h>
241da177e4SLinus Torvalds #include <linux/kernel.h>
250fe1ef24SLinus Torvalds #include <linux/kallsyms.h>
260fe1ef24SLinus Torvalds #include <linux/uaccess.h>
27332d2e78SLinus Torvalds #include <linux/ioport.h>
288a27f7c9SJoe Perches #include <net/addrconf.h>
291da177e4SLinus Torvalds 
304e57b681STim Schmielau #include <asm/page.h>		/* for PAGE_SIZE */
311da177e4SLinus Torvalds #include <asm/div64.h>
32deac93dfSJames Bottomley #include <asm/sections.h>	/* for dereference_function_descriptor() */
331da177e4SLinus Torvalds 
349b706aeeSDenys Vlasenko /* Works only for digits and letters, but small and fast */
359b706aeeSDenys Vlasenko #define TOLOWER(x) ((x) | 0x20)
369b706aeeSDenys Vlasenko 
37aa46a63eSHarvey Harrison static unsigned int simple_guess_base(const char *cp)
38aa46a63eSHarvey Harrison {
39aa46a63eSHarvey Harrison 	if (cp[0] == '0') {
40aa46a63eSHarvey Harrison 		if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
41aa46a63eSHarvey Harrison 			return 16;
42aa46a63eSHarvey Harrison 		else
43aa46a63eSHarvey Harrison 			return 8;
44aa46a63eSHarvey Harrison 	} else {
45aa46a63eSHarvey Harrison 		return 10;
46aa46a63eSHarvey Harrison 	}
47aa46a63eSHarvey Harrison }
48aa46a63eSHarvey Harrison 
491da177e4SLinus Torvalds /**
501da177e4SLinus Torvalds  * simple_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];
58491adcd2cSSteven Rostedt 	if (ext != 'f' && ext != 's')
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,
6348a27f7c9SJoe Perches 				struct printf_spec spec, const char *fmt)
635dd45c9cfSHarvey Harrison {
6368a27f7c9SJoe 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]);
6428a27f7c9SJoe 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 
6508a27f7c9SJoe Perches static char *ip4_string(char *p, const u8 *addr, bool leading_zeros)
651689afa7dSHarvey Harrison {
652689afa7dSHarvey Harrison 	int i;
653689afa7dSHarvey Harrison 
6548a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
6558a27f7c9SJoe Perches 		char temp[3];	/* hold each IP quad in reverse order */
6568a27f7c9SJoe Perches 		int digits = put_dec_trunc(temp, addr[i]) - temp;
6578a27f7c9SJoe Perches 		if (leading_zeros) {
6588a27f7c9SJoe Perches 			if (digits < 3)
6598a27f7c9SJoe Perches 				*p++ = '0';
6608a27f7c9SJoe Perches 			if (digits < 2)
6618a27f7c9SJoe Perches 				*p++ = '0';
6628a27f7c9SJoe Perches 		}
6638a27f7c9SJoe Perches 		/* reverse the digits in the quad */
6648a27f7c9SJoe Perches 		while (digits--)
6658a27f7c9SJoe Perches 			*p++ = temp[digits];
6668a27f7c9SJoe Perches 		if (i < 3)
6678a27f7c9SJoe Perches 			*p++ = '.';
6688a27f7c9SJoe Perches 	}
6698a27f7c9SJoe Perches 
6708a27f7c9SJoe Perches 	*p = '\0';
6718a27f7c9SJoe Perches 	return p;
6728a27f7c9SJoe Perches }
6738a27f7c9SJoe Perches 
674eb78cd26SJoe Perches static char *ip6_compressed_string(char *p, const char *addr)
6758a27f7c9SJoe Perches {
6768a27f7c9SJoe Perches 	int i;
6778a27f7c9SJoe Perches 	int j;
6788a27f7c9SJoe Perches 	int range;
6798a27f7c9SJoe Perches 	unsigned char zerolength[8];
6808a27f7c9SJoe Perches 	int longest = 1;
6818a27f7c9SJoe Perches 	int colonpos = -1;
6828a27f7c9SJoe Perches 	u16 word;
6838a27f7c9SJoe Perches 	u8 hi;
6848a27f7c9SJoe Perches 	u8 lo;
6858a27f7c9SJoe Perches 	bool needcolon = false;
686eb78cd26SJoe Perches 	bool useIPv4;
687eb78cd26SJoe Perches 	struct in6_addr in6;
688eb78cd26SJoe Perches 
689eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
690eb78cd26SJoe Perches 
691eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
6928a27f7c9SJoe Perches 
6938a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
6948a27f7c9SJoe Perches 
6958a27f7c9SJoe Perches 	if (useIPv4)
6968a27f7c9SJoe Perches 		range = 6;
6978a27f7c9SJoe Perches 	else
6988a27f7c9SJoe Perches 		range = 8;
6998a27f7c9SJoe Perches 
7008a27f7c9SJoe Perches 	/* find position of longest 0 run */
7018a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
7028a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
703eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
7048a27f7c9SJoe Perches 				break;
7058a27f7c9SJoe Perches 			zerolength[i]++;
7068a27f7c9SJoe Perches 		}
7078a27f7c9SJoe Perches 	}
7088a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
7098a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
7108a27f7c9SJoe Perches 			longest = zerolength[i];
7118a27f7c9SJoe Perches 			colonpos = i;
7128a27f7c9SJoe Perches 		}
7138a27f7c9SJoe Perches 	}
7148a27f7c9SJoe Perches 
7158a27f7c9SJoe Perches 	/* emit address */
7168a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
7178a27f7c9SJoe Perches 		if (i == colonpos) {
7188a27f7c9SJoe Perches 			if (needcolon || i == 0)
7198a27f7c9SJoe Perches 				*p++ = ':';
7208a27f7c9SJoe Perches 			*p++ = ':';
7218a27f7c9SJoe Perches 			needcolon = false;
7228a27f7c9SJoe Perches 			i += longest - 1;
7238a27f7c9SJoe Perches 			continue;
7248a27f7c9SJoe Perches 		}
7258a27f7c9SJoe Perches 		if (needcolon) {
7268a27f7c9SJoe Perches 			*p++ = ':';
7278a27f7c9SJoe Perches 			needcolon = false;
7288a27f7c9SJoe Perches 		}
7298a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
730eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
7318a27f7c9SJoe Perches 		hi = word >> 8;
7328a27f7c9SJoe Perches 		lo = word & 0xff;
7338a27f7c9SJoe Perches 		if (hi) {
7348a27f7c9SJoe Perches 			if (hi > 0x0f)
7358a27f7c9SJoe Perches 				p = pack_hex_byte(p, hi);
7368a27f7c9SJoe Perches 			else
7378a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
7388a27f7c9SJoe Perches 		}
7398a27f7c9SJoe Perches 		if (hi || lo > 0x0f)
7408a27f7c9SJoe Perches 			p = pack_hex_byte(p, lo);
7418a27f7c9SJoe Perches 		else
7428a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
7438a27f7c9SJoe Perches 		needcolon = true;
7448a27f7c9SJoe Perches 	}
7458a27f7c9SJoe Perches 
7468a27f7c9SJoe Perches 	if (useIPv4) {
7478a27f7c9SJoe Perches 		if (needcolon)
7488a27f7c9SJoe Perches 			*p++ = ':';
749eb78cd26SJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], false);
7508a27f7c9SJoe Perches 	}
7518a27f7c9SJoe Perches 
7528a27f7c9SJoe Perches 	*p = '\0';
7538a27f7c9SJoe Perches 	return p;
7548a27f7c9SJoe Perches }
7558a27f7c9SJoe Perches 
756eb78cd26SJoe Perches static char *ip6_string(char *p, const char *addr, const char *fmt)
7578a27f7c9SJoe Perches {
7588a27f7c9SJoe Perches 	int i;
759689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
760eb78cd26SJoe Perches 		p = pack_hex_byte(p, *addr++);
761eb78cd26SJoe Perches 		p = pack_hex_byte(p, *addr++);
7628a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
763689afa7dSHarvey Harrison 			*p++ = ':';
764689afa7dSHarvey Harrison 	}
7658a27f7c9SJoe Perches 
766689afa7dSHarvey Harrison 	*p = '\0';
7678a27f7c9SJoe Perches 	return p;
7688a27f7c9SJoe Perches }
7698a27f7c9SJoe Perches 
7708a27f7c9SJoe Perches static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
7718a27f7c9SJoe Perches 			     struct printf_spec spec, const char *fmt)
7728a27f7c9SJoe Perches {
7738a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
7748a27f7c9SJoe Perches 
7758a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
776eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
7778a27f7c9SJoe Perches 	else
778eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
779689afa7dSHarvey Harrison 
780fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
781689afa7dSHarvey Harrison }
782689afa7dSHarvey Harrison 
7838a27f7c9SJoe Perches static char *ip4_addr_string(char *buf, char *end, const u8 *addr,
7848a27f7c9SJoe Perches 			     struct printf_spec spec, const char *fmt)
7854aa99606SHarvey Harrison {
7868a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
7874aa99606SHarvey Harrison 
7888a27f7c9SJoe Perches 	ip4_string(ip4_addr, addr, fmt[0] == 'i');
7894aa99606SHarvey Harrison 
790fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
7914aa99606SHarvey Harrison }
7924aa99606SHarvey Harrison 
7934d8a743cSLinus Torvalds /*
7944d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
7954d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
7964d8a743cSLinus Torvalds  * specifiers.
7974d8a743cSLinus Torvalds  *
798332d2e78SLinus Torvalds  * Right now we handle:
7990fe1ef24SLinus Torvalds  *
8000c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
8010c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
8020efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
8030efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
804332d2e78SLinus Torvalds  * - 'R' For a struct resource pointer, it prints the range of
805332d2e78SLinus Torvalds  *       addresses (not the name nor the flags)
806dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
807dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
8088a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
8098a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
8108a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
8118a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
8128a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
8138a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
8148a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
8158a27f7c9SJoe Perches  * - 'I6c' for IPv6 addresses printed as specified by
8168a27f7c9SJoe Perches  *       http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
817332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
818332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
819332d2e78SLinus Torvalds  * pointer to the real address.
8204d8a743cSLinus Torvalds  */
821fef20d9cSFrederic Weisbecker static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
822fef20d9cSFrederic Weisbecker 			struct printf_spec spec)
82378a8bf69SLinus Torvalds {
824d97106abSLinus Torvalds 	if (!ptr)
825fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
826d97106abSLinus Torvalds 
8270fe1ef24SLinus Torvalds 	switch (*fmt) {
8280fe1ef24SLinus Torvalds 	case 'F':
8290c8b946eSFrederic Weisbecker 	case 'f':
8300fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
83191adcd2cSSteven Rostedt 	case 's':
8320fe1ef24SLinus Torvalds 		/* Fallthrough */
8330fe1ef24SLinus Torvalds 	case 'S':
8340c8b946eSFrederic Weisbecker 		return symbol_string(buf, end, ptr, spec, *fmt);
835332d2e78SLinus Torvalds 	case 'R':
836fef20d9cSFrederic Weisbecker 		return resource_string(buf, end, ptr, spec);
8378a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
8388a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
8398a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
8408a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
8418a27f7c9SJoe Perches 					 * 4:	1.2.3.4
8428a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
8438a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
8448a27f7c9SJoe Perches 					 */
8458a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
8468a27f7c9SJoe Perches 					 * 4:	001.002.003.004
8478a27f7c9SJoe Perches 					 * 6:   000102...0f
8488a27f7c9SJoe Perches 					 */
8498a27f7c9SJoe Perches 		switch (fmt[1]) {
8508a27f7c9SJoe Perches 		case '6':
8518a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
8528a27f7c9SJoe Perches 		case '4':
8538a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
8548a27f7c9SJoe Perches 		}
8554aa99606SHarvey Harrison 		break;
8560fe1ef24SLinus Torvalds 	}
857fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
858fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
859fef20d9cSFrederic Weisbecker 		spec.field_width = 2*sizeof(void *);
860fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
86178a8bf69SLinus Torvalds 	}
862fef20d9cSFrederic Weisbecker 	spec.base = 16;
863fef20d9cSFrederic Weisbecker 
864fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
865fef20d9cSFrederic Weisbecker }
866fef20d9cSFrederic Weisbecker 
867fef20d9cSFrederic Weisbecker /*
868fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
869fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
870fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
871fef20d9cSFrederic Weisbecker  * to go on the next call).
872fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
873fef20d9cSFrederic Weisbecker  *
874fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
875fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
876fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
877fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
878fef20d9cSFrederic Weisbecker  *
879fef20d9cSFrederic Weisbecker  * @fmt: the format string
880fef20d9cSFrederic Weisbecker  * @type of the token returned
881fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
882fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
883fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
884fef20d9cSFrederic Weisbecker  * @precision: precision of a number
885fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
886fef20d9cSFrederic Weisbecker  */
887fef20d9cSFrederic Weisbecker static int format_decode(const char *fmt, struct printf_spec *spec)
888fef20d9cSFrederic Weisbecker {
889fef20d9cSFrederic Weisbecker 	const char *start = fmt;
890fef20d9cSFrederic Weisbecker 
891fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
892ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
893fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
894fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
895fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
896fef20d9cSFrederic Weisbecker 		}
897fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
898fef20d9cSFrederic Weisbecker 		goto precision;
899fef20d9cSFrederic Weisbecker 	}
900fef20d9cSFrederic Weisbecker 
901fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
902fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
903fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
904fef20d9cSFrederic Weisbecker 			spec->precision = 0;
905fef20d9cSFrederic Weisbecker 
906fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
907fef20d9cSFrederic Weisbecker 		goto qualifier;
908fef20d9cSFrederic Weisbecker 	}
909fef20d9cSFrederic Weisbecker 
910fef20d9cSFrederic Weisbecker 	/* By default */
911fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
912fef20d9cSFrederic Weisbecker 
913fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
914fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
915fef20d9cSFrederic Weisbecker 			break;
916fef20d9cSFrederic Weisbecker 	}
917fef20d9cSFrederic Weisbecker 
918fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
919fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
920fef20d9cSFrederic Weisbecker 		return fmt - start;
921fef20d9cSFrederic Weisbecker 
922fef20d9cSFrederic Weisbecker 	/* Process flags */
923fef20d9cSFrederic Weisbecker 	spec->flags = 0;
924fef20d9cSFrederic Weisbecker 
925fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
926fef20d9cSFrederic Weisbecker 		bool found = true;
927fef20d9cSFrederic Weisbecker 
928fef20d9cSFrederic Weisbecker 		++fmt;
929fef20d9cSFrederic Weisbecker 
930fef20d9cSFrederic Weisbecker 		switch (*fmt) {
931fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
932fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
933fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
934fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
935fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
936fef20d9cSFrederic Weisbecker 		default:  found = false;
937fef20d9cSFrederic Weisbecker 		}
938fef20d9cSFrederic Weisbecker 
939fef20d9cSFrederic Weisbecker 		if (!found)
940fef20d9cSFrederic Weisbecker 			break;
941fef20d9cSFrederic Weisbecker 	}
942fef20d9cSFrederic Weisbecker 
943fef20d9cSFrederic Weisbecker 	/* get field width */
944fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
945fef20d9cSFrederic Weisbecker 
946fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
947fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
948fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
949fef20d9cSFrederic Weisbecker 		/* it's the next argument */
950ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
951fef20d9cSFrederic Weisbecker 		return ++fmt - start;
952fef20d9cSFrederic Weisbecker 	}
953fef20d9cSFrederic Weisbecker 
954fef20d9cSFrederic Weisbecker precision:
955fef20d9cSFrederic Weisbecker 	/* get the precision */
956fef20d9cSFrederic Weisbecker 	spec->precision = -1;
957fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
958fef20d9cSFrederic Weisbecker 		++fmt;
959fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
960fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
961fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
962fef20d9cSFrederic Weisbecker 				spec->precision = 0;
963fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
964fef20d9cSFrederic Weisbecker 			/* it's the next argument */
965adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
966fef20d9cSFrederic Weisbecker 			return ++fmt - start;
967fef20d9cSFrederic Weisbecker 		}
968fef20d9cSFrederic Weisbecker 	}
969fef20d9cSFrederic Weisbecker 
970fef20d9cSFrederic Weisbecker qualifier:
971fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
972fef20d9cSFrederic Weisbecker 	spec->qualifier = -1;
973fef20d9cSFrederic Weisbecker 	if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
974fef20d9cSFrederic Weisbecker 	    *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
975a4e94ef0SZhaolei 		spec->qualifier = *fmt++;
976a4e94ef0SZhaolei 		if (unlikely(spec->qualifier == *fmt)) {
977a4e94ef0SZhaolei 			if (spec->qualifier == 'l') {
978fef20d9cSFrederic Weisbecker 				spec->qualifier = 'L';
979fef20d9cSFrederic Weisbecker 				++fmt;
980a4e94ef0SZhaolei 			} else if (spec->qualifier == 'h') {
981a4e94ef0SZhaolei 				spec->qualifier = 'H';
982a4e94ef0SZhaolei 				++fmt;
983a4e94ef0SZhaolei 			}
984fef20d9cSFrederic Weisbecker 		}
985fef20d9cSFrederic Weisbecker 	}
986fef20d9cSFrederic Weisbecker 
987fef20d9cSFrederic Weisbecker 	/* default base */
988fef20d9cSFrederic Weisbecker 	spec->base = 10;
989fef20d9cSFrederic Weisbecker 	switch (*fmt) {
990fef20d9cSFrederic Weisbecker 	case 'c':
991fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
992fef20d9cSFrederic Weisbecker 		return ++fmt - start;
993fef20d9cSFrederic Weisbecker 
994fef20d9cSFrederic Weisbecker 	case 's':
995fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
996fef20d9cSFrederic Weisbecker 		return ++fmt - start;
997fef20d9cSFrederic Weisbecker 
998fef20d9cSFrederic Weisbecker 	case 'p':
999fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
1000fef20d9cSFrederic Weisbecker 		return fmt - start;
1001fef20d9cSFrederic Weisbecker 		/* skip alnum */
1002fef20d9cSFrederic Weisbecker 
1003fef20d9cSFrederic Weisbecker 	case 'n':
1004fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NRCHARS;
1005fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1006fef20d9cSFrederic Weisbecker 
1007fef20d9cSFrederic Weisbecker 	case '%':
1008fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1009fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1010fef20d9cSFrederic Weisbecker 
1011fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1012fef20d9cSFrederic Weisbecker 	case 'o':
1013fef20d9cSFrederic Weisbecker 		spec->base = 8;
1014fef20d9cSFrederic Weisbecker 		break;
1015fef20d9cSFrederic Weisbecker 
1016fef20d9cSFrederic Weisbecker 	case 'x':
1017fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1018fef20d9cSFrederic Weisbecker 
1019fef20d9cSFrederic Weisbecker 	case 'X':
1020fef20d9cSFrederic Weisbecker 		spec->base = 16;
1021fef20d9cSFrederic Weisbecker 		break;
1022fef20d9cSFrederic Weisbecker 
1023fef20d9cSFrederic Weisbecker 	case 'd':
1024fef20d9cSFrederic Weisbecker 	case 'i':
102539e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1026fef20d9cSFrederic Weisbecker 	case 'u':
1027fef20d9cSFrederic Weisbecker 		break;
1028fef20d9cSFrederic Weisbecker 
1029fef20d9cSFrederic Weisbecker 	default:
1030fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1031fef20d9cSFrederic Weisbecker 		return fmt - start;
1032fef20d9cSFrederic Weisbecker 	}
1033fef20d9cSFrederic Weisbecker 
1034fef20d9cSFrederic Weisbecker 	if (spec->qualifier == 'L')
1035fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1036fef20d9cSFrederic Weisbecker 	else if (spec->qualifier == 'l') {
103739e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1038fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_LONG;
1039fef20d9cSFrederic Weisbecker 		else
1040fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_ULONG;
1041fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
1042fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1043fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 't') {
1044fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1045a4e94ef0SZhaolei 	} else if (spec->qualifier == 'H') {
1046a4e94ef0SZhaolei 		if (spec->flags & SIGN)
1047a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_BYTE;
1048a4e94ef0SZhaolei 		else
1049a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_UBYTE;
1050fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'h') {
105139e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1052fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_SHORT;
1053fef20d9cSFrederic Weisbecker 		else
1054fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_USHORT;
1055fef20d9cSFrederic Weisbecker 	} else {
105639e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1057fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_INT;
1058fef20d9cSFrederic Weisbecker 		else
1059fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_UINT;
1060fef20d9cSFrederic Weisbecker 	}
1061fef20d9cSFrederic Weisbecker 
1062fef20d9cSFrederic Weisbecker 	return ++fmt - start;
106378a8bf69SLinus Torvalds }
106478a8bf69SLinus Torvalds 
10651da177e4SLinus Torvalds /**
10661da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
10671da177e4SLinus Torvalds  * @buf: The buffer to place the result into
10681da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
10691da177e4SLinus Torvalds  * @fmt: The format string to use
10701da177e4SLinus Torvalds  * @args: Arguments for the format string
10711da177e4SLinus Torvalds  *
107220036fdcSAndi Kleen  * This function follows C99 vsnprintf, but has some extensions:
107391adcd2cSSteven Rostedt  * %pS output the name of a text symbol with offset
107491adcd2cSSteven Rostedt  * %ps output the name of a text symbol without offset
10750c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
10760c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
1077332d2e78SLinus Torvalds  * %pR output the address range in a struct resource
10780efb4d20SSteven Rostedt  * %n is ignored
107920036fdcSAndi Kleen  *
10801da177e4SLinus Torvalds  * The return value is the number of characters which would
10811da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
10821da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
10831da177e4SLinus Torvalds  * number of characters written into @buf as return value
108472fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
10851da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
10861da177e4SLinus Torvalds  * string is truncated.
10871da177e4SLinus Torvalds  *
10881da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
108972fd4a35SRobert P. J. Day  * You probably want snprintf() instead.
10901da177e4SLinus Torvalds  */
10911da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
10921da177e4SLinus Torvalds {
10931da177e4SLinus Torvalds 	unsigned long long num;
10941da177e4SLinus Torvalds 	char *str, *end, c;
1095fef20d9cSFrederic Weisbecker 	int read;
1096fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
10971da177e4SLinus Torvalds 
1098f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1099f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
11002f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
11011da177e4SLinus Torvalds 		return 0;
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	str = buf;
1104f796937aSJeremy Fitzhardinge 	end = buf + size;
11051da177e4SLinus Torvalds 
1106f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1107f796937aSJeremy Fitzhardinge 	if (end < buf) {
11081da177e4SLinus Torvalds 		end = ((void *)-1);
1109f796937aSJeremy Fitzhardinge 		size = end - buf;
11101da177e4SLinus Torvalds 	}
11111da177e4SLinus Torvalds 
1112fef20d9cSFrederic Weisbecker 	while (*fmt) {
1113fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1114fef20d9cSFrederic Weisbecker 
1115fef20d9cSFrederic Weisbecker 		read = format_decode(fmt, &spec);
1116fef20d9cSFrederic Weisbecker 
1117fef20d9cSFrederic Weisbecker 		fmt += read;
1118fef20d9cSFrederic Weisbecker 
1119fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1120fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1121fef20d9cSFrederic Weisbecker 			int copy = read;
1122fef20d9cSFrederic Weisbecker 			if (str < end) {
1123fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1124fef20d9cSFrederic Weisbecker 					copy = end - str;
1125fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1126fef20d9cSFrederic Weisbecker 			}
1127fef20d9cSFrederic Weisbecker 			str += read;
1128fef20d9cSFrederic Weisbecker 			break;
11291da177e4SLinus Torvalds 		}
11301da177e4SLinus Torvalds 
1131ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1132fef20d9cSFrederic Weisbecker 			spec.field_width = va_arg(args, int);
1133fef20d9cSFrederic Weisbecker 			break;
11341da177e4SLinus Torvalds 
1135fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1136fef20d9cSFrederic Weisbecker 			spec.precision = va_arg(args, int);
1137fef20d9cSFrederic Weisbecker 			break;
11381da177e4SLinus Torvalds 
1139fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
1140fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1141fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1142f796937aSJeremy Fitzhardinge 					if (str < end)
11431da177e4SLinus Torvalds 						*str = ' ';
11441da177e4SLinus Torvalds 					++str;
1145fef20d9cSFrederic Weisbecker 
11461da177e4SLinus Torvalds 				}
11471da177e4SLinus Torvalds 			}
11481da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1149f796937aSJeremy Fitzhardinge 			if (str < end)
11501da177e4SLinus Torvalds 				*str = c;
11511da177e4SLinus Torvalds 			++str;
1152fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1153f796937aSJeremy Fitzhardinge 				if (str < end)
11541da177e4SLinus Torvalds 					*str = ' ';
11551da177e4SLinus Torvalds 				++str;
11561da177e4SLinus Torvalds 			}
1157fef20d9cSFrederic Weisbecker 			break;
11581da177e4SLinus Torvalds 
1159fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1160fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1161fef20d9cSFrederic Weisbecker 			break;
11621da177e4SLinus Torvalds 
1163fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1164fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, va_arg(args, void *),
1165fef20d9cSFrederic Weisbecker 				      spec);
1166fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
11674d8a743cSLinus Torvalds 				fmt++;
1168fef20d9cSFrederic Weisbecker 			break;
11691da177e4SLinus Torvalds 
1170fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1171f796937aSJeremy Fitzhardinge 			if (str < end)
11721da177e4SLinus Torvalds 				*str = '%';
11731da177e4SLinus Torvalds 			++str;
11741da177e4SLinus Torvalds 			break;
11751da177e4SLinus Torvalds 
1176fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1177f796937aSJeremy Fitzhardinge 			if (str < end)
11781da177e4SLinus Torvalds 				*str = '%';
11791da177e4SLinus Torvalds 			++str;
1180fef20d9cSFrederic Weisbecker 			break;
1181fef20d9cSFrederic Weisbecker 
1182fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
1183fef20d9cSFrederic Weisbecker 			int qualifier = spec.qualifier;
1184fef20d9cSFrederic Weisbecker 
1185fef20d9cSFrederic Weisbecker 			if (qualifier == 'l') {
1186fef20d9cSFrederic Weisbecker 				long *ip = va_arg(args, long *);
1187fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
1188fef20d9cSFrederic Weisbecker 			} else if (qualifier == 'Z' ||
1189fef20d9cSFrederic Weisbecker 					qualifier == 'z') {
1190fef20d9cSFrederic Weisbecker 				size_t *ip = va_arg(args, size_t *);
1191fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
11921da177e4SLinus Torvalds 			} else {
1193fef20d9cSFrederic Weisbecker 				int *ip = va_arg(args, int *);
1194fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
1195fef20d9cSFrederic Weisbecker 			}
1196fef20d9cSFrederic Weisbecker 			break;
1197fef20d9cSFrederic Weisbecker 		}
1198fef20d9cSFrederic Weisbecker 
1199fef20d9cSFrederic Weisbecker 		default:
1200fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1201fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1202fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1203fef20d9cSFrederic Weisbecker 				break;
1204fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1205fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
1206fef20d9cSFrederic Weisbecker 				break;
1207fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1208fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
1209fef20d9cSFrederic Weisbecker 				break;
1210fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1211fef20d9cSFrederic Weisbecker 				num = va_arg(args, size_t);
1212fef20d9cSFrederic Weisbecker 				break;
1213fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1214fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
1215fef20d9cSFrederic Weisbecker 				break;
1216a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1217a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
1218a4e94ef0SZhaolei 				break;
1219a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1220a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
1221a4e94ef0SZhaolei 				break;
1222fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1223fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
1224fef20d9cSFrederic Weisbecker 				break;
1225fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1226fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
1227fef20d9cSFrederic Weisbecker 				break;
122839e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
122939e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
1230fef20d9cSFrederic Weisbecker 				break;
1231fef20d9cSFrederic Weisbecker 			default:
1232fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
12331da177e4SLinus Torvalds 			}
1234fef20d9cSFrederic Weisbecker 
1235fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
12361da177e4SLinus Torvalds 		}
1237fef20d9cSFrederic Weisbecker 	}
1238fef20d9cSFrederic Weisbecker 
1239f796937aSJeremy Fitzhardinge 	if (size > 0) {
1240f796937aSJeremy Fitzhardinge 		if (str < end)
12411da177e4SLinus Torvalds 			*str = '\0';
1242f796937aSJeremy Fitzhardinge 		else
12430a6047eeSLinus Torvalds 			end[-1] = '\0';
1244f796937aSJeremy Fitzhardinge 	}
1245fef20d9cSFrederic Weisbecker 
1246f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
12471da177e4SLinus Torvalds 	return str-buf;
1248fef20d9cSFrederic Weisbecker 
12491da177e4SLinus Torvalds }
12501da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds /**
12531da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
12541da177e4SLinus Torvalds  * @buf: The buffer to place the result into
12551da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
12561da177e4SLinus Torvalds  * @fmt: The format string to use
12571da177e4SLinus Torvalds  * @args: Arguments for the format string
12581da177e4SLinus Torvalds  *
12591da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
12601da177e4SLinus Torvalds  * the @buf not including the trailing '\0'. If @size is <= 0 the function
12611da177e4SLinus Torvalds  * returns 0.
12621da177e4SLinus Torvalds  *
12631da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
126472fd4a35SRobert P. J. Day  * You probably want scnprintf() instead.
126520036fdcSAndi Kleen  *
126620036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
12671da177e4SLinus Torvalds  */
12681da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
12691da177e4SLinus Torvalds {
12701da177e4SLinus Torvalds 	int i;
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	i=vsnprintf(buf,size,fmt,args);
12731da177e4SLinus Torvalds 	return (i >= size) ? (size - 1) : i;
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds /**
12781da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
12791da177e4SLinus Torvalds  * @buf: The buffer to place the result into
12801da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
12811da177e4SLinus Torvalds  * @fmt: The format string to use
12821da177e4SLinus Torvalds  * @...: Arguments for the format string
12831da177e4SLinus Torvalds  *
12841da177e4SLinus Torvalds  * The return value is the number of characters which would be
12851da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
12861da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
12871da177e4SLinus Torvalds  * @size, the resulting string is truncated.
128820036fdcSAndi Kleen  *
128920036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
12901da177e4SLinus Torvalds  */
12911da177e4SLinus Torvalds int snprintf(char * buf, size_t size, const char *fmt, ...)
12921da177e4SLinus Torvalds {
12931da177e4SLinus Torvalds 	va_list args;
12941da177e4SLinus Torvalds 	int i;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	va_start(args, fmt);
12971da177e4SLinus Torvalds 	i=vsnprintf(buf,size,fmt,args);
12981da177e4SLinus Torvalds 	va_end(args);
12991da177e4SLinus Torvalds 	return i;
13001da177e4SLinus Torvalds }
13011da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds /**
13041da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
13051da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13061da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
13071da177e4SLinus Torvalds  * @fmt: The format string to use
13081da177e4SLinus Torvalds  * @...: Arguments for the format string
13091da177e4SLinus Torvalds  *
13101da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
1311ea6f3281SMartin Peschke  * the trailing '\0'. If @size is <= 0 the function returns 0.
13121da177e4SLinus Torvalds  */
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds int scnprintf(char * buf, size_t size, const char *fmt, ...)
13151da177e4SLinus Torvalds {
13161da177e4SLinus Torvalds 	va_list args;
13171da177e4SLinus Torvalds 	int i;
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	va_start(args, fmt);
13201da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
13211da177e4SLinus Torvalds 	va_end(args);
13221da177e4SLinus Torvalds 	return (i >= size) ? (size - 1) : i;
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds /**
13271da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
13281da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13291da177e4SLinus Torvalds  * @fmt: The format string to use
13301da177e4SLinus Torvalds  * @args: Arguments for the format string
13311da177e4SLinus Torvalds  *
13321da177e4SLinus Torvalds  * The function returns the number of characters written
133372fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
13341da177e4SLinus Torvalds  * buffer overflows.
13351da177e4SLinus Torvalds  *
13361da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
133772fd4a35SRobert P. J. Day  * You probably want sprintf() instead.
133820036fdcSAndi Kleen  *
133920036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
13401da177e4SLinus Torvalds  */
13411da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
13421da177e4SLinus Torvalds {
13431da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds /**
13481da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
13491da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13501da177e4SLinus Torvalds  * @fmt: The format string to use
13511da177e4SLinus Torvalds  * @...: Arguments for the format string
13521da177e4SLinus Torvalds  *
13531da177e4SLinus Torvalds  * The function returns the number of characters written
135472fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
13551da177e4SLinus Torvalds  * buffer overflows.
135620036fdcSAndi Kleen  *
135720036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
13581da177e4SLinus Torvalds  */
13591da177e4SLinus Torvalds int sprintf(char * buf, const char *fmt, ...)
13601da177e4SLinus Torvalds {
13611da177e4SLinus Torvalds 	va_list args;
13621da177e4SLinus Torvalds 	int i;
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	va_start(args, fmt);
13651da177e4SLinus Torvalds 	i=vsnprintf(buf, INT_MAX, fmt, args);
13661da177e4SLinus Torvalds 	va_end(args);
13671da177e4SLinus Torvalds 	return i;
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
13701da177e4SLinus Torvalds 
13714370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
13724370aa4aSLai Jiangshan /*
13734370aa4aSLai Jiangshan  * bprintf service:
13744370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
13754370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
13764370aa4aSLai Jiangshan  */
13774370aa4aSLai Jiangshan 
13784370aa4aSLai Jiangshan /**
13794370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
13804370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
13814370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
13824370aa4aSLai Jiangshan  * @fmt: The format string to use
13834370aa4aSLai Jiangshan  * @args: Arguments for the format string
13844370aa4aSLai Jiangshan  *
13854370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
13864370aa4aSLai Jiangshan  * is skiped.
13874370aa4aSLai Jiangshan  *
13884370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
13894370aa4aSLai Jiangshan  * the given input.
13904370aa4aSLai Jiangshan  *
13914370aa4aSLai Jiangshan  * NOTE:
13924370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
13934370aa4aSLai Jiangshan  * valid for bstr_printf().
13944370aa4aSLai Jiangshan  */
13954370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
13964370aa4aSLai Jiangshan {
1397fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
13984370aa4aSLai Jiangshan 	char *str, *end;
1399fef20d9cSFrederic Weisbecker 	int read;
14004370aa4aSLai Jiangshan 
14014370aa4aSLai Jiangshan 	str = (char *)bin_buf;
14024370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
14034370aa4aSLai Jiangshan 
14044370aa4aSLai Jiangshan #define save_arg(type)							\
14054370aa4aSLai Jiangshan do {									\
14064370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
14074370aa4aSLai Jiangshan 		unsigned long long value;				\
14084370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
14094370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
14104370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
14114370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
14124370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
14134370aa4aSLai Jiangshan 		}							\
14144370aa4aSLai Jiangshan 	} else {							\
14154370aa4aSLai Jiangshan 		unsigned long value;					\
14164370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
14174370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
14184370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
14194370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
14204370aa4aSLai Jiangshan 	}								\
14214370aa4aSLai Jiangshan 	str += sizeof(type);						\
14224370aa4aSLai Jiangshan } while (0)
14234370aa4aSLai Jiangshan 
14244370aa4aSLai Jiangshan 
1425fef20d9cSFrederic Weisbecker 	while (*fmt) {
1426fef20d9cSFrederic Weisbecker 		read = format_decode(fmt, &spec);
14274370aa4aSLai Jiangshan 
1428fef20d9cSFrederic Weisbecker 		fmt += read;
1429fef20d9cSFrederic Weisbecker 
1430fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1431fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
1432fef20d9cSFrederic Weisbecker 			break;
1433fef20d9cSFrederic Weisbecker 
1434ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1435fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
14364370aa4aSLai Jiangshan 			save_arg(int);
1437fef20d9cSFrederic Weisbecker 			break;
14384370aa4aSLai Jiangshan 
1439fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
14404370aa4aSLai Jiangshan 			save_arg(char);
1441fef20d9cSFrederic Weisbecker 			break;
1442fef20d9cSFrederic Weisbecker 
1443fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
14444370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
14454370aa4aSLai Jiangshan 			size_t len;
14464370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
14474370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
14484370aa4aSLai Jiangshan 				save_str = "<NULL>";
14494370aa4aSLai Jiangshan 			len = strlen(save_str);
14504370aa4aSLai Jiangshan 			if (str + len + 1 < end)
14514370aa4aSLai Jiangshan 				memcpy(str, save_str, len + 1);
14524370aa4aSLai Jiangshan 			str += len + 1;
1453fef20d9cSFrederic Weisbecker 			break;
14544370aa4aSLai Jiangshan 		}
1455fef20d9cSFrederic Weisbecker 
1456fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
14574370aa4aSLai Jiangshan 			save_arg(void *);
14584370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
1459fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
14604370aa4aSLai Jiangshan 				fmt++;
1461fef20d9cSFrederic Weisbecker 			break;
1462fef20d9cSFrederic Weisbecker 
1463fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1464fef20d9cSFrederic Weisbecker 			break;
1465fef20d9cSFrederic Weisbecker 
1466fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1467fef20d9cSFrederic Weisbecker 			break;
1468fef20d9cSFrederic Weisbecker 
1469fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
14704370aa4aSLai Jiangshan 			/* skip %n 's argument */
1471fef20d9cSFrederic Weisbecker 			int qualifier = spec.qualifier;
14724370aa4aSLai Jiangshan 			void *skip_arg;
14734370aa4aSLai Jiangshan 			if (qualifier == 'l')
14744370aa4aSLai Jiangshan 				skip_arg = va_arg(args, long *);
14754370aa4aSLai Jiangshan 			else if (qualifier == 'Z' || qualifier == 'z')
14764370aa4aSLai Jiangshan 				skip_arg = va_arg(args, size_t *);
14774370aa4aSLai Jiangshan 			else
14784370aa4aSLai Jiangshan 				skip_arg = va_arg(args, int *);
1479fef20d9cSFrederic Weisbecker 			break;
14804370aa4aSLai Jiangshan 		}
14814370aa4aSLai Jiangshan 
1482fef20d9cSFrederic Weisbecker 		default:
1483fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1484fef20d9cSFrederic Weisbecker 
1485fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1486fef20d9cSFrederic Weisbecker 				save_arg(long long);
1487fef20d9cSFrederic Weisbecker 				break;
1488fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1489fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1490fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
1491fef20d9cSFrederic Weisbecker 				break;
1492fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1493fef20d9cSFrederic Weisbecker 				save_arg(size_t);
1494fef20d9cSFrederic Weisbecker 				break;
1495fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1496fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
1497fef20d9cSFrederic Weisbecker 				break;
1498a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1499a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1500a4e94ef0SZhaolei 				save_arg(char);
1501a4e94ef0SZhaolei 				break;
1502fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1503fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1504fef20d9cSFrederic Weisbecker 				save_arg(short);
1505fef20d9cSFrederic Weisbecker 				break;
1506fef20d9cSFrederic Weisbecker 			default:
1507fef20d9cSFrederic Weisbecker 				save_arg(int);
1508fef20d9cSFrederic Weisbecker 			}
1509fef20d9cSFrederic Weisbecker 		}
1510fef20d9cSFrederic Weisbecker 	}
15114370aa4aSLai Jiangshan 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1512fef20d9cSFrederic Weisbecker 
1513fef20d9cSFrederic Weisbecker #undef save_arg
15144370aa4aSLai Jiangshan }
15154370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
15164370aa4aSLai Jiangshan 
15174370aa4aSLai Jiangshan /**
15184370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
15194370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
15204370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
15214370aa4aSLai Jiangshan  * @fmt: The format string to use
15224370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
15234370aa4aSLai Jiangshan  *
15244370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
15254370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
15264370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
15274370aa4aSLai Jiangshan  *
15284370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
15290efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
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 
15472f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
15484370aa4aSLai Jiangshan 		return 0;
15494370aa4aSLai Jiangshan 
15504370aa4aSLai Jiangshan 	str = buf;
15514370aa4aSLai Jiangshan 	end = buf + size;
15524370aa4aSLai Jiangshan 
15534370aa4aSLai Jiangshan #define get_arg(type)							\
15544370aa4aSLai Jiangshan ({									\
15554370aa4aSLai Jiangshan 	typeof(type) value;						\
15564370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
15574370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
15584370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
15594370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
15604370aa4aSLai Jiangshan 	} else {							\
15614370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
15624370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
15634370aa4aSLai Jiangshan 	}								\
15644370aa4aSLai Jiangshan 	args += sizeof(type);						\
15654370aa4aSLai Jiangshan 	value;								\
15664370aa4aSLai Jiangshan })
15674370aa4aSLai Jiangshan 
15684370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
15694370aa4aSLai Jiangshan 	if (end < buf) {
15704370aa4aSLai Jiangshan 		end = ((void *)-1);
15714370aa4aSLai Jiangshan 		size = end - buf;
15724370aa4aSLai Jiangshan 	}
15734370aa4aSLai Jiangshan 
1574fef20d9cSFrederic Weisbecker 	while (*fmt) {
1575fef20d9cSFrederic Weisbecker 		int read;
1576fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1577fef20d9cSFrederic Weisbecker 
1578fef20d9cSFrederic Weisbecker 		read = format_decode(fmt, &spec);
1579fef20d9cSFrederic Weisbecker 
1580fef20d9cSFrederic Weisbecker 		fmt += read;
1581fef20d9cSFrederic Weisbecker 
1582fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1583fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1584fef20d9cSFrederic Weisbecker 			int copy = read;
1585fef20d9cSFrederic Weisbecker 			if (str < end) {
1586fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1587fef20d9cSFrederic Weisbecker 					copy = end - str;
1588fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1589fef20d9cSFrederic Weisbecker 			}
1590fef20d9cSFrederic Weisbecker 			str += read;
1591fef20d9cSFrederic Weisbecker 			break;
15924370aa4aSLai Jiangshan 		}
15934370aa4aSLai Jiangshan 
1594ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1595fef20d9cSFrederic Weisbecker 			spec.field_width = get_arg(int);
1596fef20d9cSFrederic Weisbecker 			break;
15974370aa4aSLai Jiangshan 
1598fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1599fef20d9cSFrederic Weisbecker 			spec.precision = get_arg(int);
1600fef20d9cSFrederic Weisbecker 			break;
16014370aa4aSLai Jiangshan 
1602fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
1603fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1604fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
16054370aa4aSLai Jiangshan 					if (str < end)
16064370aa4aSLai Jiangshan 						*str = ' ';
16074370aa4aSLai Jiangshan 					++str;
16084370aa4aSLai Jiangshan 				}
16094370aa4aSLai Jiangshan 			}
16104370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
16114370aa4aSLai Jiangshan 			if (str < end)
16124370aa4aSLai Jiangshan 				*str = c;
16134370aa4aSLai Jiangshan 			++str;
1614fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
16154370aa4aSLai Jiangshan 				if (str < end)
16164370aa4aSLai Jiangshan 					*str = ' ';
16174370aa4aSLai Jiangshan 				++str;
16184370aa4aSLai Jiangshan 			}
1619fef20d9cSFrederic Weisbecker 			break;
16204370aa4aSLai Jiangshan 
1621fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
16224370aa4aSLai Jiangshan 			const char *str_arg = args;
16234370aa4aSLai Jiangshan 			size_t len = strlen(str_arg);
16244370aa4aSLai Jiangshan 			args += len + 1;
1625fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
1626fef20d9cSFrederic Weisbecker 			break;
16274370aa4aSLai Jiangshan 		}
16284370aa4aSLai Jiangshan 
1629fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1630fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, get_arg(void *), spec);
1631fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
16324370aa4aSLai Jiangshan 				fmt++;
1633fef20d9cSFrederic Weisbecker 			break;
16344370aa4aSLai Jiangshan 
1635fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
16364370aa4aSLai Jiangshan 			if (str < end)
16374370aa4aSLai Jiangshan 				*str = '%';
16384370aa4aSLai Jiangshan 			++str;
16394370aa4aSLai Jiangshan 			break;
16404370aa4aSLai Jiangshan 
1641fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
16424370aa4aSLai Jiangshan 			if (str < end)
16434370aa4aSLai Jiangshan 				*str = '%';
16444370aa4aSLai Jiangshan 			++str;
1645fef20d9cSFrederic Weisbecker 			break;
1646fef20d9cSFrederic Weisbecker 
1647fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS:
1648fef20d9cSFrederic Weisbecker 			/* skip */
1649fef20d9cSFrederic Weisbecker 			break;
1650fef20d9cSFrederic Weisbecker 
1651fef20d9cSFrederic Weisbecker 		default:
1652fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1653fef20d9cSFrederic Weisbecker 
1654fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
16554370aa4aSLai Jiangshan 				num = get_arg(long long);
1656fef20d9cSFrederic Weisbecker 				break;
1657fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
16584370aa4aSLai Jiangshan 				num = get_arg(unsigned long);
1659fef20d9cSFrederic Weisbecker 				break;
1660fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1661fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
1662fef20d9cSFrederic Weisbecker 				break;
1663fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
16644370aa4aSLai Jiangshan 				num = get_arg(size_t);
1665fef20d9cSFrederic Weisbecker 				break;
1666fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
16674370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
1668fef20d9cSFrederic Weisbecker 				break;
1669a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1670a4e94ef0SZhaolei 				num = get_arg(unsigned char);
1671a4e94ef0SZhaolei 				break;
1672a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1673a4e94ef0SZhaolei 				num = get_arg(signed char);
1674a4e94ef0SZhaolei 				break;
1675fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1676fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
1677fef20d9cSFrederic Weisbecker 				break;
1678fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1679fef20d9cSFrederic Weisbecker 				num = get_arg(short);
1680fef20d9cSFrederic Weisbecker 				break;
1681fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
16824370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
1683fef20d9cSFrederic Weisbecker 				break;
1684fef20d9cSFrederic Weisbecker 			default:
1685fef20d9cSFrederic Weisbecker 				num = get_arg(int);
16864370aa4aSLai Jiangshan 			}
1687fef20d9cSFrederic Weisbecker 
1688fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
16894370aa4aSLai Jiangshan 		}
1690fef20d9cSFrederic Weisbecker 	}
1691fef20d9cSFrederic Weisbecker 
16924370aa4aSLai Jiangshan 	if (size > 0) {
16934370aa4aSLai Jiangshan 		if (str < end)
16944370aa4aSLai Jiangshan 			*str = '\0';
16954370aa4aSLai Jiangshan 		else
16964370aa4aSLai Jiangshan 			end[-1] = '\0';
16974370aa4aSLai Jiangshan 	}
1698fef20d9cSFrederic Weisbecker 
16994370aa4aSLai Jiangshan #undef get_arg
17004370aa4aSLai Jiangshan 
17014370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
17024370aa4aSLai Jiangshan 	return str - buf;
17034370aa4aSLai Jiangshan }
17044370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
17054370aa4aSLai Jiangshan 
17064370aa4aSLai Jiangshan /**
17074370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
17084370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
17094370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
17104370aa4aSLai Jiangshan  * @fmt: The format string to use
17114370aa4aSLai Jiangshan  * @...: Arguments for the format string
17124370aa4aSLai Jiangshan  *
17134370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
17144370aa4aSLai Jiangshan  * into @bin_buf.
17154370aa4aSLai Jiangshan  */
17164370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
17174370aa4aSLai Jiangshan {
17184370aa4aSLai Jiangshan 	va_list args;
17194370aa4aSLai Jiangshan 	int ret;
17204370aa4aSLai Jiangshan 
17214370aa4aSLai Jiangshan 	va_start(args, fmt);
17224370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
17234370aa4aSLai Jiangshan 	va_end(args);
17244370aa4aSLai Jiangshan 	return ret;
17254370aa4aSLai Jiangshan }
17264370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
17274370aa4aSLai Jiangshan 
17284370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
17294370aa4aSLai Jiangshan 
17301da177e4SLinus Torvalds /**
17311da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
17321da177e4SLinus Torvalds  * @buf:	input buffer
17331da177e4SLinus Torvalds  * @fmt:	format of buffer
17341da177e4SLinus Torvalds  * @args:	arguments
17351da177e4SLinus Torvalds  */
17361da177e4SLinus Torvalds int vsscanf(const char * buf, const char * fmt, va_list args)
17371da177e4SLinus Torvalds {
17381da177e4SLinus Torvalds 	const char *str = buf;
17391da177e4SLinus Torvalds 	char *next;
17401da177e4SLinus Torvalds 	char digit;
17411da177e4SLinus Torvalds 	int num = 0;
17421da177e4SLinus Torvalds 	int qualifier;
17431da177e4SLinus Torvalds 	int base;
17441da177e4SLinus Torvalds 	int field_width;
17451da177e4SLinus Torvalds 	int is_sign = 0;
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds 	while(*fmt && *str) {
17481da177e4SLinus Torvalds 		/* skip any white space in format */
17491da177e4SLinus Torvalds 		/* white space in format matchs any amount of
17501da177e4SLinus Torvalds 		 * white space, including none, in the input.
17511da177e4SLinus Torvalds 		 */
17521da177e4SLinus Torvalds 		if (isspace(*fmt)) {
17531da177e4SLinus Torvalds 			while (isspace(*fmt))
17541da177e4SLinus Torvalds 				++fmt;
17551da177e4SLinus Torvalds 			while (isspace(*str))
17561da177e4SLinus Torvalds 				++str;
17571da177e4SLinus Torvalds 		}
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
17601da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
17611da177e4SLinus Torvalds 			if (*fmt++ != *str++)
17621da177e4SLinus Torvalds 				break;
17631da177e4SLinus Torvalds 			continue;
17641da177e4SLinus Torvalds 		}
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds 		if (!*fmt)
17671da177e4SLinus Torvalds 			break;
17681da177e4SLinus Torvalds 		++fmt;
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 		/* skip this conversion.
17711da177e4SLinus Torvalds 		 * advance both strings to next white space
17721da177e4SLinus Torvalds 		 */
17731da177e4SLinus Torvalds 		if (*fmt == '*') {
1774*8fccae2cSAndy Spencer 			while (!isspace(*fmt) && *fmt != '%' && *fmt)
17751da177e4SLinus Torvalds 				fmt++;
17761da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
17771da177e4SLinus Torvalds 				str++;
17781da177e4SLinus Torvalds 			continue;
17791da177e4SLinus Torvalds 		}
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 		/* get field width */
17821da177e4SLinus Torvalds 		field_width = -1;
17831da177e4SLinus Torvalds 		if (isdigit(*fmt))
17841da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds 		/* get conversion qualifier */
17871da177e4SLinus Torvalds 		qualifier = -1;
17881da177e4SLinus Torvalds 		if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
17891da177e4SLinus Torvalds 		    *fmt == 'Z' || *fmt == 'z') {
17901da177e4SLinus Torvalds 			qualifier = *fmt++;
17911da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
17921da177e4SLinus Torvalds 				if (qualifier == 'h') {
17931da177e4SLinus Torvalds 					qualifier = 'H';
17941da177e4SLinus Torvalds 					fmt++;
17951da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
17961da177e4SLinus Torvalds 					qualifier = 'L';
17971da177e4SLinus Torvalds 					fmt++;
17981da177e4SLinus Torvalds 				}
17991da177e4SLinus Torvalds 			}
18001da177e4SLinus Torvalds 		}
18011da177e4SLinus Torvalds 		base = 10;
18021da177e4SLinus Torvalds 		is_sign = 0;
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds 		if (!*fmt || !*str)
18051da177e4SLinus Torvalds 			break;
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 		switch(*fmt++) {
18081da177e4SLinus Torvalds 		case 'c':
18091da177e4SLinus Torvalds 		{
18101da177e4SLinus Torvalds 			char *s = (char *) va_arg(args,char*);
18111da177e4SLinus Torvalds 			if (field_width == -1)
18121da177e4SLinus Torvalds 				field_width = 1;
18131da177e4SLinus Torvalds 			do {
18141da177e4SLinus Torvalds 				*s++ = *str++;
18151da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
18161da177e4SLinus Torvalds 			num++;
18171da177e4SLinus Torvalds 		}
18181da177e4SLinus Torvalds 		continue;
18191da177e4SLinus Torvalds 		case 's':
18201da177e4SLinus Torvalds 		{
18211da177e4SLinus Torvalds 			char *s = (char *) va_arg(args, char *);
18221da177e4SLinus Torvalds 			if(field_width == -1)
18231da177e4SLinus Torvalds 				field_width = INT_MAX;
18241da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
18251da177e4SLinus Torvalds 			while (isspace(*str))
18261da177e4SLinus Torvalds 				str++;
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 			/* now copy until next white space */
18291da177e4SLinus Torvalds 			while (*str && !isspace(*str) && field_width--) {
18301da177e4SLinus Torvalds 				*s++ = *str++;
18311da177e4SLinus Torvalds 			}
18321da177e4SLinus Torvalds 			*s = '\0';
18331da177e4SLinus Torvalds 			num++;
18341da177e4SLinus Torvalds 		}
18351da177e4SLinus Torvalds 		continue;
18361da177e4SLinus Torvalds 		case 'n':
18371da177e4SLinus Torvalds 			/* return number of characters read so far */
18381da177e4SLinus Torvalds 		{
18391da177e4SLinus Torvalds 			int *i = (int *)va_arg(args,int*);
18401da177e4SLinus Torvalds 			*i = str - buf;
18411da177e4SLinus Torvalds 		}
18421da177e4SLinus Torvalds 		continue;
18431da177e4SLinus Torvalds 		case 'o':
18441da177e4SLinus Torvalds 			base = 8;
18451da177e4SLinus Torvalds 			break;
18461da177e4SLinus Torvalds 		case 'x':
18471da177e4SLinus Torvalds 		case 'X':
18481da177e4SLinus Torvalds 			base = 16;
18491da177e4SLinus Torvalds 			break;
18501da177e4SLinus Torvalds 		case 'i':
18511da177e4SLinus Torvalds                         base = 0;
18521da177e4SLinus Torvalds 		case 'd':
18531da177e4SLinus Torvalds 			is_sign = 1;
18541da177e4SLinus Torvalds 		case 'u':
18551da177e4SLinus Torvalds 			break;
18561da177e4SLinus Torvalds 		case '%':
18571da177e4SLinus Torvalds 			/* looking for '%' in str */
18581da177e4SLinus Torvalds 			if (*str++ != '%')
18591da177e4SLinus Torvalds 				return num;
18601da177e4SLinus Torvalds 			continue;
18611da177e4SLinus Torvalds 		default:
18621da177e4SLinus Torvalds 			/* invalid format; stop here */
18631da177e4SLinus Torvalds 			return num;
18641da177e4SLinus Torvalds 		}
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 		/* have some sort of integer conversion.
18671da177e4SLinus Torvalds 		 * first, skip white space in buffer.
18681da177e4SLinus Torvalds 		 */
18691da177e4SLinus Torvalds 		while (isspace(*str))
18701da177e4SLinus Torvalds 			str++;
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds 		digit = *str;
18731da177e4SLinus Torvalds 		if (is_sign && digit == '-')
18741da177e4SLinus Torvalds 			digit = *(str + 1);
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds 		if (!digit
18771da177e4SLinus Torvalds                     || (base == 16 && !isxdigit(digit))
18781da177e4SLinus Torvalds                     || (base == 10 && !isdigit(digit))
18791da177e4SLinus Torvalds                     || (base == 8 && (!isdigit(digit) || digit > '7'))
18801da177e4SLinus Torvalds                     || (base == 0 && !isdigit(digit)))
18811da177e4SLinus Torvalds 				break;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 		switch(qualifier) {
18841da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
18851da177e4SLinus Torvalds 			if (is_sign) {
18861da177e4SLinus Torvalds 				signed char *s = (signed char *) va_arg(args,signed char *);
18871da177e4SLinus Torvalds 				*s = (signed char) simple_strtol(str,&next,base);
18881da177e4SLinus Torvalds 			} else {
18891da177e4SLinus Torvalds 				unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
18901da177e4SLinus Torvalds 				*s = (unsigned char) simple_strtoul(str, &next, base);
18911da177e4SLinus Torvalds 			}
18921da177e4SLinus Torvalds 			break;
18931da177e4SLinus Torvalds 		case 'h':
18941da177e4SLinus Torvalds 			if (is_sign) {
18951da177e4SLinus Torvalds 				short *s = (short *) va_arg(args,short *);
18961da177e4SLinus Torvalds 				*s = (short) simple_strtol(str,&next,base);
18971da177e4SLinus Torvalds 			} else {
18981da177e4SLinus Torvalds 				unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
18991da177e4SLinus Torvalds 				*s = (unsigned short) simple_strtoul(str, &next, base);
19001da177e4SLinus Torvalds 			}
19011da177e4SLinus Torvalds 			break;
19021da177e4SLinus Torvalds 		case 'l':
19031da177e4SLinus Torvalds 			if (is_sign) {
19041da177e4SLinus Torvalds 				long *l = (long *) va_arg(args,long *);
19051da177e4SLinus Torvalds 				*l = simple_strtol(str,&next,base);
19061da177e4SLinus Torvalds 			} else {
19071da177e4SLinus Torvalds 				unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
19081da177e4SLinus Torvalds 				*l = simple_strtoul(str,&next,base);
19091da177e4SLinus Torvalds 			}
19101da177e4SLinus Torvalds 			break;
19111da177e4SLinus Torvalds 		case 'L':
19121da177e4SLinus Torvalds 			if (is_sign) {
19131da177e4SLinus Torvalds 				long long *l = (long long*) va_arg(args,long long *);
19141da177e4SLinus Torvalds 				*l = simple_strtoll(str,&next,base);
19151da177e4SLinus Torvalds 			} else {
19161da177e4SLinus Torvalds 				unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
19171da177e4SLinus Torvalds 				*l = simple_strtoull(str,&next,base);
19181da177e4SLinus Torvalds 			}
19191da177e4SLinus Torvalds 			break;
19201da177e4SLinus Torvalds 		case 'Z':
19211da177e4SLinus Torvalds 		case 'z':
19221da177e4SLinus Torvalds 		{
19231da177e4SLinus Torvalds 			size_t *s = (size_t*) va_arg(args,size_t*);
19241da177e4SLinus Torvalds 			*s = (size_t) simple_strtoul(str,&next,base);
19251da177e4SLinus Torvalds 		}
19261da177e4SLinus Torvalds 		break;
19271da177e4SLinus Torvalds 		default:
19281da177e4SLinus Torvalds 			if (is_sign) {
19291da177e4SLinus Torvalds 				int *i = (int *) va_arg(args, int*);
19301da177e4SLinus Torvalds 				*i = (int) simple_strtol(str,&next,base);
19311da177e4SLinus Torvalds 			} else {
19321da177e4SLinus Torvalds 				unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
19331da177e4SLinus Torvalds 				*i = (unsigned int) simple_strtoul(str,&next,base);
19341da177e4SLinus Torvalds 			}
19351da177e4SLinus Torvalds 			break;
19361da177e4SLinus Torvalds 		}
19371da177e4SLinus Torvalds 		num++;
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds 		if (!next)
19401da177e4SLinus Torvalds 			break;
19411da177e4SLinus Torvalds 		str = next;
19421da177e4SLinus Torvalds 	}
1943c6b40d16SJohannes Berg 
1944c6b40d16SJohannes Berg 	/*
1945c6b40d16SJohannes Berg 	 * Now we've come all the way through so either the input string or the
1946c6b40d16SJohannes Berg 	 * format ended. In the former case, there can be a %n at the current
1947c6b40d16SJohannes Berg 	 * position in the format that needs to be filled.
1948c6b40d16SJohannes Berg 	 */
1949c6b40d16SJohannes Berg 	if (*fmt == '%' && *(fmt + 1) == 'n') {
1950c6b40d16SJohannes Berg 		int *p = (int *)va_arg(args, int *);
1951c6b40d16SJohannes Berg 		*p = str - buf;
1952c6b40d16SJohannes Berg 	}
1953c6b40d16SJohannes Berg 
19541da177e4SLinus Torvalds 	return num;
19551da177e4SLinus Torvalds }
19561da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds /**
19591da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
19601da177e4SLinus Torvalds  * @buf:	input buffer
19611da177e4SLinus Torvalds  * @fmt:	formatting of buffer
19621da177e4SLinus Torvalds  * @...:	resulting arguments
19631da177e4SLinus Torvalds  */
19641da177e4SLinus Torvalds int sscanf(const char * buf, const char * fmt, ...)
19651da177e4SLinus Torvalds {
19661da177e4SLinus Torvalds 	va_list args;
19671da177e4SLinus Torvalds 	int i;
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds 	va_start(args,fmt);
19701da177e4SLinus Torvalds 	i = vsscanf(buf,fmt,args);
19711da177e4SLinus Torvalds 	va_end(args);
19721da177e4SLinus Torvalds 	return i;
19731da177e4SLinus Torvalds }
19741da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
1975