xref: /openbmc/linux/lib/vsprintf.c (revision 08562cb27da6a1472be15898173105b46801a73b)
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 	}
741da177e4SLinus Torvalds 	if (endp)
751da177e4SLinus Torvalds 		*endp = (char *)cp;
767b9186f5SAndré Goddard Rosa 
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);
917b9186f5SAndré Goddard Rosa 
921da177e4SLinus Torvalds 	return simple_strtoul(cp, endp, base);
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtol);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /**
971da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
981da177e4SLinus Torvalds  * @cp: The start of the string
991da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
1001da177e4SLinus Torvalds  * @base: The number base to use
1011da177e4SLinus Torvalds  */
1021da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
1031da177e4SLinus Torvalds {
104aa46a63eSHarvey Harrison 	unsigned long long result = 0;
1051da177e4SLinus Torvalds 
106aa46a63eSHarvey Harrison 	if (!base)
107aa46a63eSHarvey Harrison 		base = simple_guess_base(cp);
108aa46a63eSHarvey Harrison 
109aa46a63eSHarvey Harrison 	if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
1101da177e4SLinus Torvalds 		cp += 2;
111aa46a63eSHarvey Harrison 
112aa46a63eSHarvey Harrison 	while (isxdigit(*cp)) {
113aa46a63eSHarvey Harrison 		unsigned int value;
114aa46a63eSHarvey Harrison 
115aa46a63eSHarvey Harrison 		value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
116aa46a63eSHarvey Harrison 		if (value >= base)
117aa46a63eSHarvey Harrison 			break;
1181da177e4SLinus Torvalds 		result = result * base + value;
1191da177e4SLinus Torvalds 		cp++;
1201da177e4SLinus Torvalds 	}
1211da177e4SLinus Torvalds 	if (endp)
1221da177e4SLinus Torvalds 		*endp = (char *)cp;
1237b9186f5SAndré Goddard Rosa 
1241da177e4SLinus Torvalds 	return result;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /**
1291da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
1301da177e4SLinus Torvalds  * @cp: The start of the string
1311da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
1321da177e4SLinus Torvalds  * @base: The number base to use
1331da177e4SLinus Torvalds  */
1341da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	if (*cp == '-')
1371da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1387b9186f5SAndré Goddard Rosa 
1391da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
14206b2a76dSYi Yang /**
14306b2a76dSYi Yang  * strict_strtoul - convert a string to an unsigned long strictly
14406b2a76dSYi Yang  * @cp: The string to be converted
14506b2a76dSYi Yang  * @base: The number base to use
14606b2a76dSYi Yang  * @res: The converted result value
14706b2a76dSYi Yang  *
14806b2a76dSYi Yang  * strict_strtoul converts a string to an unsigned long only if the
14906b2a76dSYi Yang  * string is really an unsigned long string, any string containing
15006b2a76dSYi Yang  * any invalid char at the tail will be rejected and -EINVAL is returned,
15106b2a76dSYi Yang  * only a newline char at the tail is acceptible because people generally
15206b2a76dSYi Yang  * change a module parameter in the following way:
15306b2a76dSYi Yang  *
15406b2a76dSYi Yang  * 	echo 1024 > /sys/module/e1000/parameters/copybreak
15506b2a76dSYi Yang  *
15606b2a76dSYi Yang  * echo will append a newline to the tail.
15706b2a76dSYi Yang  *
15806b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
15906b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
16006b2a76dSYi Yang  *
16106b2a76dSYi Yang  * simple_strtoul just ignores the successive invalid characters and
16206b2a76dSYi Yang  * return the converted value of prefix part of the string.
16306b2a76dSYi Yang  */
1649d85db22SHarvey Harrison int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
1659d85db22SHarvey Harrison {
1669d85db22SHarvey Harrison 	char *tail;
1679d85db22SHarvey Harrison 	unsigned long val;
1689d85db22SHarvey Harrison 	size_t len;
1699d85db22SHarvey Harrison 
1709d85db22SHarvey Harrison 	*res = 0;
1719d85db22SHarvey Harrison 	len = strlen(cp);
1729d85db22SHarvey Harrison 	if (len == 0)
1739d85db22SHarvey Harrison 		return -EINVAL;
1749d85db22SHarvey Harrison 
1759d85db22SHarvey Harrison 	val = simple_strtoul(cp, &tail, base);
176e899aa82SPavel Machek 	if (tail == cp)
177e899aa82SPavel Machek 		return -EINVAL;
1787b9186f5SAndré Goddard Rosa 
1799d85db22SHarvey Harrison 	if ((*tail == '\0') ||
1809d85db22SHarvey Harrison 		((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
1819d85db22SHarvey Harrison 		*res = val;
1829d85db22SHarvey Harrison 		return 0;
1839d85db22SHarvey Harrison 	}
1849d85db22SHarvey Harrison 
1859d85db22SHarvey Harrison 	return -EINVAL;
1869d85db22SHarvey Harrison }
1879d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoul);
18806b2a76dSYi Yang 
18906b2a76dSYi Yang /**
19006b2a76dSYi Yang  * strict_strtol - convert a string to a long strictly
19106b2a76dSYi Yang  * @cp: The string to be converted
19206b2a76dSYi Yang  * @base: The number base to use
19306b2a76dSYi Yang  * @res: The converted result value
19406b2a76dSYi Yang  *
19506b2a76dSYi Yang  * strict_strtol is similiar to strict_strtoul, but it allows the first
19606b2a76dSYi Yang  * character of a string is '-'.
19706b2a76dSYi Yang  *
19806b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
19906b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
20006b2a76dSYi Yang  */
2019d85db22SHarvey Harrison int strict_strtol(const char *cp, unsigned int base, long *res)
2029d85db22SHarvey Harrison {
2039d85db22SHarvey Harrison 	int ret;
2049d85db22SHarvey Harrison 	if (*cp == '-') {
2059d85db22SHarvey Harrison 		ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
2069d85db22SHarvey Harrison 		if (!ret)
2079d85db22SHarvey Harrison 			*res = -(*res);
2089d85db22SHarvey Harrison 	} else {
2099d85db22SHarvey Harrison 		ret = strict_strtoul(cp, base, (unsigned long *)res);
2109d85db22SHarvey Harrison 	}
2119d85db22SHarvey Harrison 
2129d85db22SHarvey Harrison 	return ret;
2139d85db22SHarvey Harrison }
2149d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtol);
21506b2a76dSYi Yang 
21606b2a76dSYi Yang /**
21706b2a76dSYi Yang  * strict_strtoull - convert a string to an unsigned long long strictly
21806b2a76dSYi Yang  * @cp: The string to be converted
21906b2a76dSYi Yang  * @base: The number base to use
22006b2a76dSYi Yang  * @res: The converted result value
22106b2a76dSYi Yang  *
22206b2a76dSYi Yang  * strict_strtoull converts a string to an unsigned long long only if the
22306b2a76dSYi Yang  * string is really an unsigned long long string, any string containing
22406b2a76dSYi Yang  * any invalid char at the tail will be rejected and -EINVAL is returned,
22506b2a76dSYi Yang  * only a newline char at the tail is acceptible because people generally
22606b2a76dSYi Yang  * change a module parameter in the following way:
22706b2a76dSYi Yang  *
22806b2a76dSYi Yang  * 	echo 1024 > /sys/module/e1000/parameters/copybreak
22906b2a76dSYi Yang  *
23006b2a76dSYi Yang  * echo will append a newline to the tail of the string.
23106b2a76dSYi Yang  *
23206b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
23306b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
23406b2a76dSYi Yang  *
23506b2a76dSYi Yang  * simple_strtoull just ignores the successive invalid characters and
23606b2a76dSYi Yang  * return the converted value of prefix part of the string.
23706b2a76dSYi Yang  */
2389d85db22SHarvey Harrison int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
2399d85db22SHarvey Harrison {
2409d85db22SHarvey Harrison 	char *tail;
2419d85db22SHarvey Harrison 	unsigned long long val;
2429d85db22SHarvey Harrison 	size_t len;
2439d85db22SHarvey Harrison 
2449d85db22SHarvey Harrison 	*res = 0;
2459d85db22SHarvey Harrison 	len = strlen(cp);
2469d85db22SHarvey Harrison 	if (len == 0)
2479d85db22SHarvey Harrison 		return -EINVAL;
2489d85db22SHarvey Harrison 
2499d85db22SHarvey Harrison 	val = simple_strtoull(cp, &tail, base);
250e899aa82SPavel Machek 	if (tail == cp)
251e899aa82SPavel Machek 		return -EINVAL;
2529d85db22SHarvey Harrison 	if ((*tail == '\0') ||
2539d85db22SHarvey Harrison 		((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
2549d85db22SHarvey Harrison 		*res = val;
2559d85db22SHarvey Harrison 		return 0;
2569d85db22SHarvey Harrison 	}
2579d85db22SHarvey Harrison 
2589d85db22SHarvey Harrison 	return -EINVAL;
2599d85db22SHarvey Harrison }
2609d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoull);
26106b2a76dSYi Yang 
26206b2a76dSYi Yang /**
26306b2a76dSYi Yang  * strict_strtoll - convert a string to a long long strictly
26406b2a76dSYi Yang  * @cp: The string to be converted
26506b2a76dSYi Yang  * @base: The number base to use
26606b2a76dSYi Yang  * @res: The converted result value
26706b2a76dSYi Yang  *
26806b2a76dSYi Yang  * strict_strtoll is similiar to strict_strtoull, but it allows the first
26906b2a76dSYi Yang  * character of a string is '-'.
27006b2a76dSYi Yang  *
27106b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
27206b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
27306b2a76dSYi Yang  */
2749d85db22SHarvey Harrison int strict_strtoll(const char *cp, unsigned int base, long long *res)
2759d85db22SHarvey Harrison {
2769d85db22SHarvey Harrison 	int ret;
2779d85db22SHarvey Harrison 	if (*cp == '-') {
2789d85db22SHarvey Harrison 		ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
2799d85db22SHarvey Harrison 		if (!ret)
2809d85db22SHarvey Harrison 			*res = -(*res);
2819d85db22SHarvey Harrison 	} else {
2829d85db22SHarvey Harrison 		ret = strict_strtoull(cp, base, (unsigned long long *)res);
2839d85db22SHarvey Harrison 	}
28406b2a76dSYi Yang 
2859d85db22SHarvey Harrison 	return ret;
2869d85db22SHarvey Harrison }
28706b2a76dSYi Yang EXPORT_SYMBOL(strict_strtoll);
28806b2a76dSYi Yang 
2891da177e4SLinus Torvalds static int skip_atoi(const char **s)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	int i = 0;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	while (isdigit(**s))
2941da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
2957b9186f5SAndré Goddard Rosa 
2961da177e4SLinus Torvalds 	return i;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2994277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used
3004277eeddSDenis Vlasenko  * for /proc and /sys data. This directly impacts e.g. top performance
3014277eeddSDenis Vlasenko  * with many processes running. We optimize it for speed
3024277eeddSDenis Vlasenko  * using code from
3034277eeddSDenis Vlasenko  * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
3044277eeddSDenis Vlasenko  * (with permission from the author, Douglas W. Jones). */
3054277eeddSDenis Vlasenko 
3064277eeddSDenis Vlasenko /* Formats correctly any integer in [0,99999].
3074277eeddSDenis Vlasenko  * Outputs from one to five digits depending on input.
3084277eeddSDenis Vlasenko  * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
3094277eeddSDenis Vlasenko static char *put_dec_trunc(char *buf, unsigned q)
3104277eeddSDenis Vlasenko {
3114277eeddSDenis Vlasenko 	unsigned d3, d2, d1, d0;
3124277eeddSDenis Vlasenko 	d1 = (q>>4) & 0xf;
3134277eeddSDenis Vlasenko 	d2 = (q>>8) & 0xf;
3144277eeddSDenis Vlasenko 	d3 = (q>>12);
3154277eeddSDenis Vlasenko 
3164277eeddSDenis Vlasenko 	d0 = 6*(d3 + d2 + d1) + (q & 0xf);
3174277eeddSDenis Vlasenko 	q = (d0 * 0xcd) >> 11;
3184277eeddSDenis Vlasenko 	d0 = d0 - 10*q;
3194277eeddSDenis Vlasenko 	*buf++ = d0 + '0'; /* least significant digit */
3204277eeddSDenis Vlasenko 	d1 = q + 9*d3 + 5*d2 + d1;
3214277eeddSDenis Vlasenko 	if (d1 != 0) {
3224277eeddSDenis Vlasenko 		q = (d1 * 0xcd) >> 11;
3234277eeddSDenis Vlasenko 		d1 = d1 - 10*q;
3244277eeddSDenis Vlasenko 		*buf++ = d1 + '0'; /* next digit */
3254277eeddSDenis Vlasenko 
3264277eeddSDenis Vlasenko 		d2 = q + 2*d2;
3274277eeddSDenis Vlasenko 		if ((d2 != 0) || (d3 != 0)) {
3284277eeddSDenis Vlasenko 			q = (d2 * 0xd) >> 7;
3294277eeddSDenis Vlasenko 			d2 = d2 - 10*q;
3304277eeddSDenis Vlasenko 			*buf++ = d2 + '0'; /* next digit */
3314277eeddSDenis Vlasenko 
3324277eeddSDenis Vlasenko 			d3 = q + 4*d3;
3334277eeddSDenis Vlasenko 			if (d3 != 0) {
3344277eeddSDenis Vlasenko 				q = (d3 * 0xcd) >> 11;
3354277eeddSDenis Vlasenko 				d3 = d3 - 10*q;
3364277eeddSDenis Vlasenko 				*buf++ = d3 + '0';  /* next digit */
3374277eeddSDenis Vlasenko 				if (q != 0)
3384277eeddSDenis Vlasenko 					*buf++ = q + '0'; /* most sign. digit */
3394277eeddSDenis Vlasenko 			}
3404277eeddSDenis Vlasenko 		}
3414277eeddSDenis Vlasenko 	}
3427b9186f5SAndré Goddard Rosa 
3434277eeddSDenis Vlasenko 	return buf;
3444277eeddSDenis Vlasenko }
3454277eeddSDenis Vlasenko /* Same with if's removed. Always emits five digits */
3464277eeddSDenis Vlasenko static char *put_dec_full(char *buf, unsigned q)
3474277eeddSDenis Vlasenko {
3484277eeddSDenis Vlasenko 	/* BTW, if q is in [0,9999], 8-bit ints will be enough, */
3494277eeddSDenis Vlasenko 	/* but anyway, gcc produces better code with full-sized ints */
3504277eeddSDenis Vlasenko 	unsigned d3, d2, d1, d0;
3514277eeddSDenis Vlasenko 	d1 = (q>>4) & 0xf;
3524277eeddSDenis Vlasenko 	d2 = (q>>8) & 0xf;
3534277eeddSDenis Vlasenko 	d3 = (q>>12);
3544277eeddSDenis Vlasenko 
3557b9186f5SAndré Goddard Rosa 	/*
3567b9186f5SAndré Goddard Rosa 	 * Possible ways to approx. divide by 10
3577b9186f5SAndré Goddard Rosa 	 * gcc -O2 replaces multiply with shifts and adds
3587b9186f5SAndré Goddard Rosa 	 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
3597b9186f5SAndré Goddard Rosa 	 * (x * 0x67) >> 10:  1100111
3607b9186f5SAndré Goddard Rosa 	 * (x * 0x34) >> 9:    110100 - same
3617b9186f5SAndré Goddard Rosa 	 * (x * 0x1a) >> 8:     11010 - same
3627b9186f5SAndré Goddard Rosa 	 * (x * 0x0d) >> 7:      1101 - same, shortest code (on i386)
3637b9186f5SAndré Goddard Rosa 	 */
3644277eeddSDenis Vlasenko 	d0 = 6*(d3 + d2 + d1) + (q & 0xf);
3654277eeddSDenis Vlasenko 	q = (d0 * 0xcd) >> 11;
3664277eeddSDenis Vlasenko 	d0 = d0 - 10*q;
3674277eeddSDenis Vlasenko 	*buf++ = d0 + '0';
3684277eeddSDenis Vlasenko 	d1 = q + 9*d3 + 5*d2 + d1;
3694277eeddSDenis Vlasenko 		q = (d1 * 0xcd) >> 11;
3704277eeddSDenis Vlasenko 		d1 = d1 - 10*q;
3714277eeddSDenis Vlasenko 		*buf++ = d1 + '0';
3724277eeddSDenis Vlasenko 
3734277eeddSDenis Vlasenko 		d2 = q + 2*d2;
3744277eeddSDenis Vlasenko 			q = (d2 * 0xd) >> 7;
3754277eeddSDenis Vlasenko 			d2 = d2 - 10*q;
3764277eeddSDenis Vlasenko 			*buf++ = d2 + '0';
3774277eeddSDenis Vlasenko 
3784277eeddSDenis Vlasenko 			d3 = q + 4*d3;
3794277eeddSDenis Vlasenko 				q = (d3 * 0xcd) >> 11; /* - shorter code */
3804277eeddSDenis Vlasenko 				/* q = (d3 * 0x67) >> 10; - would also work */
3814277eeddSDenis Vlasenko 				d3 = d3 - 10*q;
3824277eeddSDenis Vlasenko 				*buf++ = d3 + '0';
3834277eeddSDenis Vlasenko 					*buf++ = q + '0';
3847b9186f5SAndré Goddard Rosa 
3854277eeddSDenis Vlasenko 	return buf;
3864277eeddSDenis Vlasenko }
3874277eeddSDenis Vlasenko /* No inlining helps gcc to use registers better */
3884277eeddSDenis Vlasenko static noinline char *put_dec(char *buf, unsigned long long num)
3894277eeddSDenis Vlasenko {
3904277eeddSDenis Vlasenko 	while (1) {
3914277eeddSDenis Vlasenko 		unsigned rem;
3924277eeddSDenis Vlasenko 		if (num < 100000)
3934277eeddSDenis Vlasenko 			return put_dec_trunc(buf, num);
3944277eeddSDenis Vlasenko 		rem = do_div(num, 100000);
3954277eeddSDenis Vlasenko 		buf = put_dec_full(buf, rem);
3964277eeddSDenis Vlasenko 	}
3974277eeddSDenis Vlasenko }
3984277eeddSDenis Vlasenko 
3991da177e4SLinus Torvalds #define ZEROPAD	1		/* pad with zero */
4001da177e4SLinus Torvalds #define SIGN	2		/* unsigned/signed long */
4011da177e4SLinus Torvalds #define PLUS	4		/* show plus */
4021da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
4031da177e4SLinus Torvalds #define LEFT	16		/* left justified */
4049b706aeeSDenys Vlasenko #define SMALL	32		/* Must be 32 == 0x20 */
4059b706aeeSDenys Vlasenko #define SPECIAL	64		/* 0x */
4061da177e4SLinus Torvalds 
407fef20d9cSFrederic Weisbecker enum format_type {
408fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
409ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
410fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
411fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
412fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
413fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
414fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
415fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
416fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
417fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
418fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
419a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
420a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
421fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
422fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
423fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
424fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
425fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NRCHARS,
426fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
427fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
428fef20d9cSFrederic Weisbecker };
429fef20d9cSFrederic Weisbecker 
430fef20d9cSFrederic Weisbecker struct printf_spec {
431fef20d9cSFrederic Weisbecker 	enum format_type	type;
432fef20d9cSFrederic Weisbecker 	int			flags;		/* flags to number() */
433fef20d9cSFrederic Weisbecker 	int			field_width;	/* width of output field */
434fef20d9cSFrederic Weisbecker 	int			base;
435fef20d9cSFrederic Weisbecker 	int			precision;	/* # of digits/chars */
436fef20d9cSFrederic Weisbecker 	int			qualifier;
437fef20d9cSFrederic Weisbecker };
438fef20d9cSFrederic Weisbecker 
439fef20d9cSFrederic Weisbecker static char *number(char *buf, char *end, unsigned long long num,
440fef20d9cSFrederic Weisbecker 			struct printf_spec spec)
4411da177e4SLinus Torvalds {
4429b706aeeSDenys Vlasenko 	/* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
4439b706aeeSDenys Vlasenko 	static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
4449b706aeeSDenys Vlasenko 
4459b706aeeSDenys Vlasenko 	char tmp[66];
4469b706aeeSDenys Vlasenko 	char sign;
4479b706aeeSDenys Vlasenko 	char locase;
448fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
4491da177e4SLinus Torvalds 	int i;
4501da177e4SLinus Torvalds 
4519b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
4529b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
453fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
454fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
455fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
4561da177e4SLinus Torvalds 	sign = 0;
457fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4581da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4591da177e4SLinus Torvalds 			sign = '-';
4601da177e4SLinus Torvalds 			num = -(signed long long)num;
461fef20d9cSFrederic Weisbecker 			spec.field_width--;
462fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4631da177e4SLinus Torvalds 			sign = '+';
464fef20d9cSFrederic Weisbecker 			spec.field_width--;
465fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4661da177e4SLinus Torvalds 			sign = ' ';
467fef20d9cSFrederic Weisbecker 			spec.field_width--;
4681da177e4SLinus Torvalds 		}
4691da177e4SLinus Torvalds 	}
470b39a7340SDenis Vlasenko 	if (need_pfx) {
471fef20d9cSFrederic Weisbecker 		spec.field_width--;
472fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
473fef20d9cSFrederic Weisbecker 			spec.field_width--;
4741da177e4SLinus Torvalds 	}
475b39a7340SDenis Vlasenko 
476b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4771da177e4SLinus Torvalds 	i = 0;
4781da177e4SLinus Torvalds 	if (num == 0)
4791da177e4SLinus Torvalds 		tmp[i++] = '0';
4804277eeddSDenis Vlasenko 	/* Generic code, for any base:
4814277eeddSDenis Vlasenko 	else do {
4829b706aeeSDenys Vlasenko 		tmp[i++] = (digits[do_div(num,base)] | locase);
4834277eeddSDenis Vlasenko 	} while (num != 0);
4844277eeddSDenis Vlasenko 	*/
485fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
486fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
487b39a7340SDenis Vlasenko 		int shift = 3;
4887b9186f5SAndré Goddard Rosa 
4897b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4907b9186f5SAndré Goddard Rosa 			shift = 4;
491b39a7340SDenis Vlasenko 		do {
4929b706aeeSDenys Vlasenko 			tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
493b39a7340SDenis Vlasenko 			num >>= shift;
494b39a7340SDenis Vlasenko 		} while (num);
4954277eeddSDenis Vlasenko 	} else { /* base 10 */
4964277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4974277eeddSDenis Vlasenko 	}
498b39a7340SDenis Vlasenko 
499b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
500fef20d9cSFrederic Weisbecker 	if (i > spec.precision)
501fef20d9cSFrederic Weisbecker 		spec.precision = i;
502b39a7340SDenis Vlasenko 	/* leading space padding */
503fef20d9cSFrederic Weisbecker 	spec.field_width -= spec.precision;
504fef20d9cSFrederic Weisbecker 	if (!(spec.flags & (ZEROPAD+LEFT))) {
505fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
506f796937aSJeremy Fitzhardinge 			if (buf < end)
5071da177e4SLinus Torvalds 				*buf = ' ';
5081da177e4SLinus Torvalds 			++buf;
5091da177e4SLinus Torvalds 		}
5101da177e4SLinus Torvalds 	}
511b39a7340SDenis Vlasenko 	/* sign */
5121da177e4SLinus Torvalds 	if (sign) {
513f796937aSJeremy Fitzhardinge 		if (buf < end)
5141da177e4SLinus Torvalds 			*buf = sign;
5151da177e4SLinus Torvalds 		++buf;
5161da177e4SLinus Torvalds 	}
517b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
518b39a7340SDenis Vlasenko 	if (need_pfx) {
519f796937aSJeremy Fitzhardinge 		if (buf < end)
5201da177e4SLinus Torvalds 			*buf = '0';
5211da177e4SLinus Torvalds 		++buf;
522fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
523f796937aSJeremy Fitzhardinge 			if (buf < end)
5249b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
5251da177e4SLinus Torvalds 			++buf;
5261da177e4SLinus Torvalds 		}
5271da177e4SLinus Torvalds 	}
528b39a7340SDenis Vlasenko 	/* zero or space padding */
529fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
530fef20d9cSFrederic Weisbecker 		char c = (spec.flags & ZEROPAD) ? '0' : ' ';
531fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
532f796937aSJeremy Fitzhardinge 			if (buf < end)
5331da177e4SLinus Torvalds 				*buf = c;
5341da177e4SLinus Torvalds 			++buf;
5351da177e4SLinus Torvalds 		}
5361da177e4SLinus Torvalds 	}
537b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
538fef20d9cSFrederic Weisbecker 	while (i <= --spec.precision) {
539f796937aSJeremy Fitzhardinge 		if (buf < end)
5401da177e4SLinus Torvalds 			*buf = '0';
5411da177e4SLinus Torvalds 		++buf;
5421da177e4SLinus Torvalds 	}
543b39a7340SDenis Vlasenko 	/* actual digits of result */
544b39a7340SDenis Vlasenko 	while (--i >= 0) {
545f796937aSJeremy Fitzhardinge 		if (buf < end)
5461da177e4SLinus Torvalds 			*buf = tmp[i];
5471da177e4SLinus Torvalds 		++buf;
5481da177e4SLinus Torvalds 	}
549b39a7340SDenis Vlasenko 	/* trailing space padding */
550fef20d9cSFrederic Weisbecker 	while (--spec.field_width >= 0) {
551f796937aSJeremy Fitzhardinge 		if (buf < end)
5521da177e4SLinus Torvalds 			*buf = ' ';
5531da177e4SLinus Torvalds 		++buf;
5541da177e4SLinus Torvalds 	}
5557b9186f5SAndré Goddard Rosa 
5561da177e4SLinus Torvalds 	return buf;
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5590f4f81dcSAndré Goddard Rosa static char *string(char *buf, char *end, const char *s, struct printf_spec spec)
5600f9bfa56SLinus Torvalds {
5610f9bfa56SLinus Torvalds 	int len, i;
5620f9bfa56SLinus Torvalds 
5630f9bfa56SLinus Torvalds 	if ((unsigned long)s < PAGE_SIZE)
5640f4f81dcSAndré Goddard Rosa 		s = "(null)";
5650f9bfa56SLinus Torvalds 
566fef20d9cSFrederic Weisbecker 	len = strnlen(s, spec.precision);
5670f9bfa56SLinus Torvalds 
568fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
569fef20d9cSFrederic Weisbecker 		while (len < spec.field_width--) {
5700f9bfa56SLinus Torvalds 			if (buf < end)
5710f9bfa56SLinus Torvalds 				*buf = ' ';
5720f9bfa56SLinus Torvalds 			++buf;
5730f9bfa56SLinus Torvalds 		}
5740f9bfa56SLinus Torvalds 	}
5750f9bfa56SLinus Torvalds 	for (i = 0; i < len; ++i) {
5760f9bfa56SLinus Torvalds 		if (buf < end)
5770f9bfa56SLinus Torvalds 			*buf = *s;
5780f9bfa56SLinus Torvalds 		++buf; ++s;
5790f9bfa56SLinus Torvalds 	}
580fef20d9cSFrederic Weisbecker 	while (len < spec.field_width--) {
5810f9bfa56SLinus Torvalds 		if (buf < end)
5820f9bfa56SLinus Torvalds 			*buf = ' ';
5830f9bfa56SLinus Torvalds 		++buf;
5840f9bfa56SLinus Torvalds 	}
5857b9186f5SAndré Goddard Rosa 
5860f9bfa56SLinus Torvalds 	return buf;
5870f9bfa56SLinus Torvalds }
5880f9bfa56SLinus Torvalds 
589fef20d9cSFrederic Weisbecker static char *symbol_string(char *buf, char *end, void *ptr,
5900c8b946eSFrederic Weisbecker 				struct printf_spec spec, char ext)
5910fe1ef24SLinus Torvalds {
5920fe1ef24SLinus Torvalds 	unsigned long value = (unsigned long) ptr;
5930fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
5940fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
59591adcd2cSSteven Rostedt 	if (ext != 'f' && ext != 's')
5960fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
5970c8b946eSFrederic Weisbecker 	else
5980c8b946eSFrederic Weisbecker 		kallsyms_lookup(value, NULL, NULL, NULL, sym);
5997b9186f5SAndré Goddard Rosa 
600fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
6010fe1ef24SLinus Torvalds #else
602fef20d9cSFrederic Weisbecker 	spec.field_width = 2 * sizeof(void *);
603fef20d9cSFrederic Weisbecker 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
604fef20d9cSFrederic Weisbecker 	spec.base = 16;
6057b9186f5SAndré Goddard Rosa 
606fef20d9cSFrederic Weisbecker 	return number(buf, end, value, spec);
6070fe1ef24SLinus Torvalds #endif
6080fe1ef24SLinus Torvalds }
6090fe1ef24SLinus Torvalds 
610fef20d9cSFrederic Weisbecker static char *resource_string(char *buf, char *end, struct resource *res,
611fd95541eSBjorn Helgaas 				struct printf_spec spec, const char *fmt)
612332d2e78SLinus Torvalds {
613332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
61428405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
615332d2e78SLinus Torvalds #endif
616332d2e78SLinus Torvalds 
617332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
61828405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
619332d2e78SLinus Torvalds #endif
620c91d3376SBjorn Helgaas 	struct printf_spec hex_spec = {
621fef20d9cSFrederic Weisbecker 		.base = 16,
622fef20d9cSFrederic Weisbecker 		.precision = -1,
623fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
624fef20d9cSFrederic Weisbecker 	};
625c91d3376SBjorn Helgaas 	struct printf_spec dec_spec = {
626c91d3376SBjorn Helgaas 		.base = 10,
627c91d3376SBjorn Helgaas 		.precision = -1,
628c91d3376SBjorn Helgaas 		.flags = 0,
629c91d3376SBjorn Helgaas 	};
630fd95541eSBjorn Helgaas 	struct printf_spec str_spec = {
631fd95541eSBjorn Helgaas 		.field_width = -1,
632fd95541eSBjorn Helgaas 		.precision = 10,
633fd95541eSBjorn Helgaas 		.flags = LEFT,
634fd95541eSBjorn Helgaas 	};
635fd95541eSBjorn Helgaas 	struct printf_spec flag_spec = {
636fd95541eSBjorn Helgaas 		.base = 16,
637fd95541eSBjorn Helgaas 		.precision = -1,
638fd95541eSBjorn Helgaas 		.flags = SPECIAL | SMALL,
639fd95541eSBjorn Helgaas 	};
640c7dabef8SBjorn Helgaas 
641c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
642c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
643c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
644c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
645c7dabef8SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref disabled]")
646c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
647c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
648c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
649c7dabef8SBjorn Helgaas 
650332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
651c91d3376SBjorn Helgaas 	int size = -1, addr = 0;
652c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
653332d2e78SLinus Torvalds 
654c91d3376SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
655332d2e78SLinus Torvalds 		size = IO_RSRC_PRINTK_SIZE;
656c91d3376SBjorn Helgaas 		addr = 1;
657c91d3376SBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
658332d2e78SLinus Torvalds 		size = MEM_RSRC_PRINTK_SIZE;
659c91d3376SBjorn Helgaas 		addr = 1;
660c91d3376SBjorn Helgaas 	}
661332d2e78SLinus Torvalds 
662332d2e78SLinus Torvalds 	*p++ = '[';
663fd95541eSBjorn Helgaas 	if (res->flags & IORESOURCE_IO)
664fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
665fd95541eSBjorn Helgaas 	else if (res->flags & IORESOURCE_MEM)
666fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
667fd95541eSBjorn Helgaas 	else if (res->flags & IORESOURCE_IRQ)
668fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
669fd95541eSBjorn Helgaas 	else if (res->flags & IORESOURCE_DMA)
670fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
671c7dabef8SBjorn Helgaas 	else {
672c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
673c7dabef8SBjorn Helgaas 		decode = 0;
674fd95541eSBjorn Helgaas 	}
675c91d3376SBjorn Helgaas 	hex_spec.field_width = size;
676c91d3376SBjorn Helgaas 	p = number(p, pend, res->start, addr ? hex_spec : dec_spec);
677c91d3376SBjorn Helgaas 	if (res->start != res->end) {
678332d2e78SLinus Torvalds 		*p++ = '-';
679c91d3376SBjorn Helgaas 		p = number(p, pend, res->end, addr ? hex_spec : dec_spec);
680c91d3376SBjorn Helgaas 	}
681c7dabef8SBjorn Helgaas 	if (decode) {
682fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
683fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
684fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
685fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
686fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
687fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
688c7dabef8SBjorn Helgaas 	} else {
689fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
690c7dabef8SBjorn Helgaas 		p = number(p, pend, res->flags, flag_spec);
691fd95541eSBjorn Helgaas 	}
692332d2e78SLinus Torvalds 	*p++ = ']';
693c7dabef8SBjorn Helgaas 	*p = '\0';
694332d2e78SLinus Torvalds 
695fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
696332d2e78SLinus Torvalds }
697332d2e78SLinus Torvalds 
698fef20d9cSFrederic Weisbecker static char *mac_address_string(char *buf, char *end, u8 *addr,
6998a27f7c9SJoe Perches 				struct printf_spec spec, const char *fmt)
700dd45c9cfSHarvey Harrison {
7018a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
702dd45c9cfSHarvey Harrison 	char *p = mac_addr;
703dd45c9cfSHarvey Harrison 	int i;
704dd45c9cfSHarvey Harrison 
705dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
706dd45c9cfSHarvey Harrison 		p = pack_hex_byte(p, addr[i]);
7078a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
708dd45c9cfSHarvey Harrison 			*p++ = ':';
709dd45c9cfSHarvey Harrison 	}
710dd45c9cfSHarvey Harrison 	*p = '\0';
711dd45c9cfSHarvey Harrison 
712fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
713dd45c9cfSHarvey Harrison }
714dd45c9cfSHarvey Harrison 
7158a27f7c9SJoe Perches static char *ip4_string(char *p, const u8 *addr, bool leading_zeros)
716689afa7dSHarvey Harrison {
717689afa7dSHarvey Harrison 	int i;
718689afa7dSHarvey Harrison 
7198a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
7208a27f7c9SJoe Perches 		char temp[3];	/* hold each IP quad in reverse order */
7218a27f7c9SJoe Perches 		int digits = put_dec_trunc(temp, addr[i]) - temp;
7228a27f7c9SJoe Perches 		if (leading_zeros) {
7238a27f7c9SJoe Perches 			if (digits < 3)
7248a27f7c9SJoe Perches 				*p++ = '0';
7258a27f7c9SJoe Perches 			if (digits < 2)
7268a27f7c9SJoe Perches 				*p++ = '0';
7278a27f7c9SJoe Perches 		}
7288a27f7c9SJoe Perches 		/* reverse the digits in the quad */
7298a27f7c9SJoe Perches 		while (digits--)
7308a27f7c9SJoe Perches 			*p++ = temp[digits];
7318a27f7c9SJoe Perches 		if (i < 3)
7328a27f7c9SJoe Perches 			*p++ = '.';
7338a27f7c9SJoe Perches 	}
7348a27f7c9SJoe Perches 	*p = '\0';
7357b9186f5SAndré Goddard Rosa 
7368a27f7c9SJoe Perches 	return p;
7378a27f7c9SJoe Perches }
7388a27f7c9SJoe Perches 
739eb78cd26SJoe Perches static char *ip6_compressed_string(char *p, const char *addr)
7408a27f7c9SJoe Perches {
7417b9186f5SAndré Goddard Rosa 	int i, j, range;
7428a27f7c9SJoe Perches 	unsigned char zerolength[8];
7438a27f7c9SJoe Perches 	int longest = 1;
7448a27f7c9SJoe Perches 	int colonpos = -1;
7458a27f7c9SJoe Perches 	u16 word;
7467b9186f5SAndré Goddard Rosa 	u8 hi, lo;
7478a27f7c9SJoe Perches 	bool needcolon = false;
748eb78cd26SJoe Perches 	bool useIPv4;
749eb78cd26SJoe Perches 	struct in6_addr in6;
750eb78cd26SJoe Perches 
751eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
752eb78cd26SJoe Perches 
753eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
7548a27f7c9SJoe Perches 
7558a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
7568a27f7c9SJoe Perches 
7578a27f7c9SJoe Perches 	if (useIPv4)
7588a27f7c9SJoe Perches 		range = 6;
7598a27f7c9SJoe Perches 	else
7608a27f7c9SJoe Perches 		range = 8;
7618a27f7c9SJoe Perches 
7628a27f7c9SJoe Perches 	/* find position of longest 0 run */
7638a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
7648a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
765eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
7668a27f7c9SJoe Perches 				break;
7678a27f7c9SJoe Perches 			zerolength[i]++;
7688a27f7c9SJoe Perches 		}
7698a27f7c9SJoe Perches 	}
7708a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
7718a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
7728a27f7c9SJoe Perches 			longest = zerolength[i];
7738a27f7c9SJoe Perches 			colonpos = i;
7748a27f7c9SJoe Perches 		}
7758a27f7c9SJoe Perches 	}
7768a27f7c9SJoe Perches 
7778a27f7c9SJoe Perches 	/* emit address */
7788a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
7798a27f7c9SJoe Perches 		if (i == colonpos) {
7808a27f7c9SJoe Perches 			if (needcolon || i == 0)
7818a27f7c9SJoe Perches 				*p++ = ':';
7828a27f7c9SJoe Perches 			*p++ = ':';
7838a27f7c9SJoe Perches 			needcolon = false;
7848a27f7c9SJoe Perches 			i += longest - 1;
7858a27f7c9SJoe Perches 			continue;
7868a27f7c9SJoe Perches 		}
7878a27f7c9SJoe Perches 		if (needcolon) {
7888a27f7c9SJoe Perches 			*p++ = ':';
7898a27f7c9SJoe Perches 			needcolon = false;
7908a27f7c9SJoe Perches 		}
7918a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
792eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
7938a27f7c9SJoe Perches 		hi = word >> 8;
7948a27f7c9SJoe Perches 		lo = word & 0xff;
7958a27f7c9SJoe Perches 		if (hi) {
7968a27f7c9SJoe Perches 			if (hi > 0x0f)
7978a27f7c9SJoe Perches 				p = pack_hex_byte(p, hi);
7988a27f7c9SJoe Perches 			else
7998a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
8008a27f7c9SJoe Perches 		}
8018a27f7c9SJoe Perches 		if (hi || lo > 0x0f)
8028a27f7c9SJoe Perches 			p = pack_hex_byte(p, lo);
8038a27f7c9SJoe Perches 		else
8048a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
8058a27f7c9SJoe Perches 		needcolon = true;
8068a27f7c9SJoe Perches 	}
8078a27f7c9SJoe Perches 
8088a27f7c9SJoe Perches 	if (useIPv4) {
8098a27f7c9SJoe Perches 		if (needcolon)
8108a27f7c9SJoe Perches 			*p++ = ':';
811eb78cd26SJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], false);
8128a27f7c9SJoe Perches 	}
8138a27f7c9SJoe Perches 	*p = '\0';
8147b9186f5SAndré Goddard Rosa 
8158a27f7c9SJoe Perches 	return p;
8168a27f7c9SJoe Perches }
8178a27f7c9SJoe Perches 
818eb78cd26SJoe Perches static char *ip6_string(char *p, const char *addr, const char *fmt)
8198a27f7c9SJoe Perches {
8208a27f7c9SJoe Perches 	int i;
8217b9186f5SAndré Goddard Rosa 
822689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
823eb78cd26SJoe Perches 		p = pack_hex_byte(p, *addr++);
824eb78cd26SJoe Perches 		p = pack_hex_byte(p, *addr++);
8258a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
826689afa7dSHarvey Harrison 			*p++ = ':';
827689afa7dSHarvey Harrison 	}
828689afa7dSHarvey Harrison 	*p = '\0';
8297b9186f5SAndré Goddard Rosa 
8308a27f7c9SJoe Perches 	return p;
8318a27f7c9SJoe Perches }
8328a27f7c9SJoe Perches 
8338a27f7c9SJoe Perches static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
8348a27f7c9SJoe Perches 			     struct printf_spec spec, const char *fmt)
8358a27f7c9SJoe Perches {
8368a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
8378a27f7c9SJoe Perches 
8388a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
839eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
8408a27f7c9SJoe Perches 	else
841eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
842689afa7dSHarvey Harrison 
843fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
844689afa7dSHarvey Harrison }
845689afa7dSHarvey Harrison 
8468a27f7c9SJoe Perches static char *ip4_addr_string(char *buf, char *end, const u8 *addr,
8478a27f7c9SJoe Perches 			     struct printf_spec spec, const char *fmt)
8484aa99606SHarvey Harrison {
8498a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
8504aa99606SHarvey Harrison 
8518a27f7c9SJoe Perches 	ip4_string(ip4_addr, addr, fmt[0] == 'i');
8524aa99606SHarvey Harrison 
853fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
8544aa99606SHarvey Harrison }
8554aa99606SHarvey Harrison 
8564d8a743cSLinus Torvalds /*
8574d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
8584d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
8594d8a743cSLinus Torvalds  * specifiers.
8604d8a743cSLinus Torvalds  *
861332d2e78SLinus Torvalds  * Right now we handle:
8620fe1ef24SLinus Torvalds  *
8630c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
8640c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
8650efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
8660efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
867c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
868c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
869dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
870dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
8718a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
8728a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
8738a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
8748a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
8758a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
8768a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
8778a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
8788a27f7c9SJoe Perches  * - 'I6c' for IPv6 addresses printed as specified by
8798a27f7c9SJoe Perches  *       http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
880332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
881332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
882332d2e78SLinus Torvalds  * pointer to the real address.
8834d8a743cSLinus Torvalds  */
884fef20d9cSFrederic Weisbecker static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
885fef20d9cSFrederic Weisbecker 			struct printf_spec spec)
88678a8bf69SLinus Torvalds {
887d97106abSLinus Torvalds 	if (!ptr)
888fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
889d97106abSLinus Torvalds 
8900fe1ef24SLinus Torvalds 	switch (*fmt) {
8910fe1ef24SLinus Torvalds 	case 'F':
8920c8b946eSFrederic Weisbecker 	case 'f':
8930fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
89491adcd2cSSteven Rostedt 	case 's':
8950fe1ef24SLinus Torvalds 		/* Fallthrough */
8960fe1ef24SLinus Torvalds 	case 'S':
8970c8b946eSFrederic Weisbecker 		return symbol_string(buf, end, ptr, spec, *fmt);
898332d2e78SLinus Torvalds 	case 'R':
899c7dabef8SBjorn Helgaas 	case 'r':
900fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
9018a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
9028a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
9038a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
9048a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
9058a27f7c9SJoe Perches 					 * 4:	1.2.3.4
9068a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
9078a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
9088a27f7c9SJoe Perches 					 */
9098a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
9108a27f7c9SJoe Perches 					 * 4:	001.002.003.004
9118a27f7c9SJoe Perches 					 * 6:   000102...0f
9128a27f7c9SJoe Perches 					 */
9138a27f7c9SJoe Perches 		switch (fmt[1]) {
9148a27f7c9SJoe Perches 		case '6':
9158a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
9168a27f7c9SJoe Perches 		case '4':
9178a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
9188a27f7c9SJoe Perches 		}
9194aa99606SHarvey Harrison 		break;
9200fe1ef24SLinus Torvalds 	}
921fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
922fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
923fef20d9cSFrederic Weisbecker 		spec.field_width = 2*sizeof(void *);
924fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
92578a8bf69SLinus Torvalds 	}
926fef20d9cSFrederic Weisbecker 	spec.base = 16;
927fef20d9cSFrederic Weisbecker 
928fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
929fef20d9cSFrederic Weisbecker }
930fef20d9cSFrederic Weisbecker 
931fef20d9cSFrederic Weisbecker /*
932fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
933fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
934fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
935fef20d9cSFrederic Weisbecker  * to go on the next call).
936fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
937fef20d9cSFrederic Weisbecker  *
938fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
939fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
940fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
941fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
942fef20d9cSFrederic Weisbecker  *
943fef20d9cSFrederic Weisbecker  * @fmt: the format string
944fef20d9cSFrederic Weisbecker  * @type of the token returned
945fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
946fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
947fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
948fef20d9cSFrederic Weisbecker  * @precision: precision of a number
949fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
950fef20d9cSFrederic Weisbecker  */
951fef20d9cSFrederic Weisbecker static int format_decode(const char *fmt, struct printf_spec *spec)
952fef20d9cSFrederic Weisbecker {
953fef20d9cSFrederic Weisbecker 	const char *start = fmt;
954fef20d9cSFrederic Weisbecker 
955fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
956ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
957fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
958fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
959fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
960fef20d9cSFrederic Weisbecker 		}
961fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
962fef20d9cSFrederic Weisbecker 		goto precision;
963fef20d9cSFrederic Weisbecker 	}
964fef20d9cSFrederic Weisbecker 
965fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
966fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
967fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
968fef20d9cSFrederic Weisbecker 			spec->precision = 0;
969fef20d9cSFrederic Weisbecker 
970fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
971fef20d9cSFrederic Weisbecker 		goto qualifier;
972fef20d9cSFrederic Weisbecker 	}
973fef20d9cSFrederic Weisbecker 
974fef20d9cSFrederic Weisbecker 	/* By default */
975fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
976fef20d9cSFrederic Weisbecker 
977fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
978fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
979fef20d9cSFrederic Weisbecker 			break;
980fef20d9cSFrederic Weisbecker 	}
981fef20d9cSFrederic Weisbecker 
982fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
983fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
984fef20d9cSFrederic Weisbecker 		return fmt - start;
985fef20d9cSFrederic Weisbecker 
986fef20d9cSFrederic Weisbecker 	/* Process flags */
987fef20d9cSFrederic Weisbecker 	spec->flags = 0;
988fef20d9cSFrederic Weisbecker 
989fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
990fef20d9cSFrederic Weisbecker 		bool found = true;
991fef20d9cSFrederic Weisbecker 
992fef20d9cSFrederic Weisbecker 		++fmt;
993fef20d9cSFrederic Weisbecker 
994fef20d9cSFrederic Weisbecker 		switch (*fmt) {
995fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
996fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
997fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
998fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
999fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
1000fef20d9cSFrederic Weisbecker 		default:  found = false;
1001fef20d9cSFrederic Weisbecker 		}
1002fef20d9cSFrederic Weisbecker 
1003fef20d9cSFrederic Weisbecker 		if (!found)
1004fef20d9cSFrederic Weisbecker 			break;
1005fef20d9cSFrederic Weisbecker 	}
1006fef20d9cSFrederic Weisbecker 
1007fef20d9cSFrederic Weisbecker 	/* get field width */
1008fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
1009fef20d9cSFrederic Weisbecker 
1010fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
1011fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
1012fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
1013fef20d9cSFrederic Weisbecker 		/* it's the next argument */
1014ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
1015fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1016fef20d9cSFrederic Weisbecker 	}
1017fef20d9cSFrederic Weisbecker 
1018fef20d9cSFrederic Weisbecker precision:
1019fef20d9cSFrederic Weisbecker 	/* get the precision */
1020fef20d9cSFrederic Weisbecker 	spec->precision = -1;
1021fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
1022fef20d9cSFrederic Weisbecker 		++fmt;
1023fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
1024fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
1025fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
1026fef20d9cSFrederic Weisbecker 				spec->precision = 0;
1027fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
1028fef20d9cSFrederic Weisbecker 			/* it's the next argument */
1029adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
1030fef20d9cSFrederic Weisbecker 			return ++fmt - start;
1031fef20d9cSFrederic Weisbecker 		}
1032fef20d9cSFrederic Weisbecker 	}
1033fef20d9cSFrederic Weisbecker 
1034fef20d9cSFrederic Weisbecker qualifier:
1035fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
1036fef20d9cSFrederic Weisbecker 	spec->qualifier = -1;
1037*08562cb2SAndré Goddard Rosa 	if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
1038*08562cb2SAndré Goddard Rosa 	    TOLOWER(*fmt) == 'z' || *fmt == 't') {
1039a4e94ef0SZhaolei 		spec->qualifier = *fmt++;
1040a4e94ef0SZhaolei 		if (unlikely(spec->qualifier == *fmt)) {
1041a4e94ef0SZhaolei 			if (spec->qualifier == 'l') {
1042fef20d9cSFrederic Weisbecker 				spec->qualifier = 'L';
1043fef20d9cSFrederic Weisbecker 				++fmt;
1044a4e94ef0SZhaolei 			} else if (spec->qualifier == 'h') {
1045a4e94ef0SZhaolei 				spec->qualifier = 'H';
1046a4e94ef0SZhaolei 				++fmt;
1047a4e94ef0SZhaolei 			}
1048fef20d9cSFrederic Weisbecker 		}
1049fef20d9cSFrederic Weisbecker 	}
1050fef20d9cSFrederic Weisbecker 
1051fef20d9cSFrederic Weisbecker 	/* default base */
1052fef20d9cSFrederic Weisbecker 	spec->base = 10;
1053fef20d9cSFrederic Weisbecker 	switch (*fmt) {
1054fef20d9cSFrederic Weisbecker 	case 'c':
1055fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
1056fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1057fef20d9cSFrederic Weisbecker 
1058fef20d9cSFrederic Weisbecker 	case 's':
1059fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
1060fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1061fef20d9cSFrederic Weisbecker 
1062fef20d9cSFrederic Weisbecker 	case 'p':
1063fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
1064fef20d9cSFrederic Weisbecker 		return fmt - start;
1065fef20d9cSFrederic Weisbecker 		/* skip alnum */
1066fef20d9cSFrederic Weisbecker 
1067fef20d9cSFrederic Weisbecker 	case 'n':
1068fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NRCHARS;
1069fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1070fef20d9cSFrederic Weisbecker 
1071fef20d9cSFrederic Weisbecker 	case '%':
1072fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1073fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1074fef20d9cSFrederic Weisbecker 
1075fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1076fef20d9cSFrederic Weisbecker 	case 'o':
1077fef20d9cSFrederic Weisbecker 		spec->base = 8;
1078fef20d9cSFrederic Weisbecker 		break;
1079fef20d9cSFrederic Weisbecker 
1080fef20d9cSFrederic Weisbecker 	case 'x':
1081fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1082fef20d9cSFrederic Weisbecker 
1083fef20d9cSFrederic Weisbecker 	case 'X':
1084fef20d9cSFrederic Weisbecker 		spec->base = 16;
1085fef20d9cSFrederic Weisbecker 		break;
1086fef20d9cSFrederic Weisbecker 
1087fef20d9cSFrederic Weisbecker 	case 'd':
1088fef20d9cSFrederic Weisbecker 	case 'i':
108939e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1090fef20d9cSFrederic Weisbecker 	case 'u':
1091fef20d9cSFrederic Weisbecker 		break;
1092fef20d9cSFrederic Weisbecker 
1093fef20d9cSFrederic Weisbecker 	default:
1094fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1095fef20d9cSFrederic Weisbecker 		return fmt - start;
1096fef20d9cSFrederic Weisbecker 	}
1097fef20d9cSFrederic Weisbecker 
1098fef20d9cSFrederic Weisbecker 	if (spec->qualifier == 'L')
1099fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1100fef20d9cSFrederic Weisbecker 	else if (spec->qualifier == 'l') {
110139e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1102fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_LONG;
1103fef20d9cSFrederic Weisbecker 		else
1104fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_ULONG;
1105*08562cb2SAndré Goddard Rosa 	} else if (TOLOWER(spec->qualifier) == 'z') {
1106fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1107fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 't') {
1108fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1109a4e94ef0SZhaolei 	} else if (spec->qualifier == 'H') {
1110a4e94ef0SZhaolei 		if (spec->flags & SIGN)
1111a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_BYTE;
1112a4e94ef0SZhaolei 		else
1113a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_UBYTE;
1114fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'h') {
111539e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1116fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_SHORT;
1117fef20d9cSFrederic Weisbecker 		else
1118fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_USHORT;
1119fef20d9cSFrederic Weisbecker 	} else {
112039e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1121fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_INT;
1122fef20d9cSFrederic Weisbecker 		else
1123fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_UINT;
1124fef20d9cSFrederic Weisbecker 	}
1125fef20d9cSFrederic Weisbecker 
1126fef20d9cSFrederic Weisbecker 	return ++fmt - start;
112778a8bf69SLinus Torvalds }
112878a8bf69SLinus Torvalds 
11291da177e4SLinus Torvalds /**
11301da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
11311da177e4SLinus Torvalds  * @buf: The buffer to place the result into
11321da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
11331da177e4SLinus Torvalds  * @fmt: The format string to use
11341da177e4SLinus Torvalds  * @args: Arguments for the format string
11351da177e4SLinus Torvalds  *
113620036fdcSAndi Kleen  * This function follows C99 vsnprintf, but has some extensions:
113791adcd2cSSteven Rostedt  * %pS output the name of a text symbol with offset
113891adcd2cSSteven Rostedt  * %ps output the name of a text symbol without offset
11390c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
11400c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
1141332d2e78SLinus Torvalds  * %pR output the address range in a struct resource
11420efb4d20SSteven Rostedt  * %n is ignored
114320036fdcSAndi Kleen  *
11441da177e4SLinus Torvalds  * The return value is the number of characters which would
11451da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
11461da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
11471da177e4SLinus Torvalds  * number of characters written into @buf as return value
114872fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
11491da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
11501da177e4SLinus Torvalds  * string is truncated.
11511da177e4SLinus Torvalds  *
11521da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
115372fd4a35SRobert P. J. Day  * You probably want snprintf() instead.
11541da177e4SLinus Torvalds  */
11551da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
11561da177e4SLinus Torvalds {
11571da177e4SLinus Torvalds 	unsigned long long num;
11581da177e4SLinus Torvalds 	char *str, *end, c;
1159fef20d9cSFrederic Weisbecker 	int read;
1160fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
11611da177e4SLinus Torvalds 
1162f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1163f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
11642f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
11651da177e4SLinus Torvalds 		return 0;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	str = buf;
1168f796937aSJeremy Fitzhardinge 	end = buf + size;
11691da177e4SLinus Torvalds 
1170f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1171f796937aSJeremy Fitzhardinge 	if (end < buf) {
11721da177e4SLinus Torvalds 		end = ((void *)-1);
1173f796937aSJeremy Fitzhardinge 		size = end - buf;
11741da177e4SLinus Torvalds 	}
11751da177e4SLinus Torvalds 
1176fef20d9cSFrederic Weisbecker 	while (*fmt) {
1177fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1178fef20d9cSFrederic Weisbecker 
1179fef20d9cSFrederic Weisbecker 		read = format_decode(fmt, &spec);
1180fef20d9cSFrederic Weisbecker 
1181fef20d9cSFrederic Weisbecker 		fmt += read;
1182fef20d9cSFrederic Weisbecker 
1183fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1184fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1185fef20d9cSFrederic Weisbecker 			int copy = read;
1186fef20d9cSFrederic Weisbecker 			if (str < end) {
1187fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1188fef20d9cSFrederic Weisbecker 					copy = end - str;
1189fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1190fef20d9cSFrederic Weisbecker 			}
1191fef20d9cSFrederic Weisbecker 			str += read;
1192fef20d9cSFrederic Weisbecker 			break;
11931da177e4SLinus Torvalds 		}
11941da177e4SLinus Torvalds 
1195ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1196fef20d9cSFrederic Weisbecker 			spec.field_width = va_arg(args, int);
1197fef20d9cSFrederic Weisbecker 			break;
11981da177e4SLinus Torvalds 
1199fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1200fef20d9cSFrederic Weisbecker 			spec.precision = va_arg(args, int);
1201fef20d9cSFrederic Weisbecker 			break;
12021da177e4SLinus Torvalds 
1203fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
1204fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1205fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1206f796937aSJeremy Fitzhardinge 					if (str < end)
12071da177e4SLinus Torvalds 						*str = ' ';
12081da177e4SLinus Torvalds 					++str;
1209fef20d9cSFrederic Weisbecker 
12101da177e4SLinus Torvalds 				}
12111da177e4SLinus Torvalds 			}
12121da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1213f796937aSJeremy Fitzhardinge 			if (str < end)
12141da177e4SLinus Torvalds 				*str = c;
12151da177e4SLinus Torvalds 			++str;
1216fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1217f796937aSJeremy Fitzhardinge 				if (str < end)
12181da177e4SLinus Torvalds 					*str = ' ';
12191da177e4SLinus Torvalds 				++str;
12201da177e4SLinus Torvalds 			}
1221fef20d9cSFrederic Weisbecker 			break;
12221da177e4SLinus Torvalds 
1223fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1224fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1225fef20d9cSFrederic Weisbecker 			break;
12261da177e4SLinus Torvalds 
1227fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1228fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, va_arg(args, void *),
1229fef20d9cSFrederic Weisbecker 				      spec);
1230fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
12314d8a743cSLinus Torvalds 				fmt++;
1232fef20d9cSFrederic Weisbecker 			break;
12331da177e4SLinus Torvalds 
1234fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1235f796937aSJeremy Fitzhardinge 			if (str < end)
12361da177e4SLinus Torvalds 				*str = '%';
12371da177e4SLinus Torvalds 			++str;
12381da177e4SLinus Torvalds 			break;
12391da177e4SLinus Torvalds 
1240fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1241f796937aSJeremy Fitzhardinge 			if (str < end)
12421da177e4SLinus Torvalds 				*str = '%';
12431da177e4SLinus Torvalds 			++str;
1244fef20d9cSFrederic Weisbecker 			break;
1245fef20d9cSFrederic Weisbecker 
1246fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
1247fef20d9cSFrederic Weisbecker 			int qualifier = spec.qualifier;
1248fef20d9cSFrederic Weisbecker 
1249fef20d9cSFrederic Weisbecker 			if (qualifier == 'l') {
1250fef20d9cSFrederic Weisbecker 				long *ip = va_arg(args, long *);
1251fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
1252*08562cb2SAndré Goddard Rosa 			} else if (TOLOWER(qualifier) == 'z') {
1253fef20d9cSFrederic Weisbecker 				size_t *ip = va_arg(args, size_t *);
1254fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
12551da177e4SLinus Torvalds 			} else {
1256fef20d9cSFrederic Weisbecker 				int *ip = va_arg(args, int *);
1257fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
1258fef20d9cSFrederic Weisbecker 			}
1259fef20d9cSFrederic Weisbecker 			break;
1260fef20d9cSFrederic Weisbecker 		}
1261fef20d9cSFrederic Weisbecker 
1262fef20d9cSFrederic Weisbecker 		default:
1263fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1264fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1265fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1266fef20d9cSFrederic Weisbecker 				break;
1267fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1268fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
1269fef20d9cSFrederic Weisbecker 				break;
1270fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1271fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
1272fef20d9cSFrederic Weisbecker 				break;
1273fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1274fef20d9cSFrederic Weisbecker 				num = va_arg(args, size_t);
1275fef20d9cSFrederic Weisbecker 				break;
1276fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1277fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
1278fef20d9cSFrederic Weisbecker 				break;
1279a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1280a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
1281a4e94ef0SZhaolei 				break;
1282a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1283a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
1284a4e94ef0SZhaolei 				break;
1285fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1286fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
1287fef20d9cSFrederic Weisbecker 				break;
1288fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1289fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
1290fef20d9cSFrederic Weisbecker 				break;
129139e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
129239e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
1293fef20d9cSFrederic Weisbecker 				break;
1294fef20d9cSFrederic Weisbecker 			default:
1295fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
12961da177e4SLinus Torvalds 			}
1297fef20d9cSFrederic Weisbecker 
1298fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
12991da177e4SLinus Torvalds 		}
1300fef20d9cSFrederic Weisbecker 	}
1301fef20d9cSFrederic Weisbecker 
1302f796937aSJeremy Fitzhardinge 	if (size > 0) {
1303f796937aSJeremy Fitzhardinge 		if (str < end)
13041da177e4SLinus Torvalds 			*str = '\0';
1305f796937aSJeremy Fitzhardinge 		else
13060a6047eeSLinus Torvalds 			end[-1] = '\0';
1307f796937aSJeremy Fitzhardinge 	}
1308fef20d9cSFrederic Weisbecker 
1309f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
13101da177e4SLinus Torvalds 	return str-buf;
1311fef20d9cSFrederic Weisbecker 
13121da177e4SLinus Torvalds }
13131da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds /**
13161da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
13171da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13181da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
13191da177e4SLinus Torvalds  * @fmt: The format string to use
13201da177e4SLinus Torvalds  * @args: Arguments for the format string
13211da177e4SLinus Torvalds  *
13221da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
13231da177e4SLinus Torvalds  * the @buf not including the trailing '\0'. If @size is <= 0 the function
13241da177e4SLinus Torvalds  * returns 0.
13251da177e4SLinus Torvalds  *
13261da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
132772fd4a35SRobert P. J. Day  * You probably want scnprintf() instead.
132820036fdcSAndi Kleen  *
132920036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
13301da177e4SLinus Torvalds  */
13311da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
13321da177e4SLinus Torvalds {
13331da177e4SLinus Torvalds 	int i;
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
13367b9186f5SAndré Goddard Rosa 
13371da177e4SLinus Torvalds 	return (i >= size) ? (size - 1) : i;
13381da177e4SLinus Torvalds }
13391da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds /**
13421da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
13431da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13441da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
13451da177e4SLinus Torvalds  * @fmt: The format string to use
13461da177e4SLinus Torvalds  * @...: Arguments for the format string
13471da177e4SLinus Torvalds  *
13481da177e4SLinus Torvalds  * The return value is the number of characters which would be
13491da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
13501da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
13511da177e4SLinus Torvalds  * @size, the resulting string is truncated.
135220036fdcSAndi Kleen  *
135320036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
13541da177e4SLinus Torvalds  */
13551da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
13561da177e4SLinus Torvalds {
13571da177e4SLinus Torvalds 	va_list args;
13581da177e4SLinus Torvalds 	int i;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	va_start(args, fmt);
13611da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
13621da177e4SLinus Torvalds 	va_end(args);
13637b9186f5SAndré Goddard Rosa 
13641da177e4SLinus Torvalds 	return i;
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds /**
13691da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
13701da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13711da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
13721da177e4SLinus Torvalds  * @fmt: The format string to use
13731da177e4SLinus Torvalds  * @...: Arguments for the format string
13741da177e4SLinus Torvalds  *
13751da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
1376ea6f3281SMartin Peschke  * the trailing '\0'. If @size is <= 0 the function returns 0.
13771da177e4SLinus Torvalds  */
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
13801da177e4SLinus Torvalds {
13811da177e4SLinus Torvalds 	va_list args;
13821da177e4SLinus Torvalds 	int i;
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 	va_start(args, fmt);
13851da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
13861da177e4SLinus Torvalds 	va_end(args);
13877b9186f5SAndré Goddard Rosa 
13881da177e4SLinus Torvalds 	return (i >= size) ? (size - 1) : i;
13891da177e4SLinus Torvalds }
13901da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds /**
13931da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
13941da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13951da177e4SLinus Torvalds  * @fmt: The format string to use
13961da177e4SLinus Torvalds  * @args: Arguments for the format string
13971da177e4SLinus Torvalds  *
13981da177e4SLinus Torvalds  * The function returns the number of characters written
139972fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
14001da177e4SLinus Torvalds  * buffer overflows.
14011da177e4SLinus Torvalds  *
14021da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
140372fd4a35SRobert P. J. Day  * You probably want sprintf() instead.
140420036fdcSAndi Kleen  *
140520036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
14061da177e4SLinus Torvalds  */
14071da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
14081da177e4SLinus Torvalds {
14091da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
14101da177e4SLinus Torvalds }
14111da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds /**
14141da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
14151da177e4SLinus Torvalds  * @buf: The buffer to place the result into
14161da177e4SLinus Torvalds  * @fmt: The format string to use
14171da177e4SLinus Torvalds  * @...: Arguments for the format string
14181da177e4SLinus Torvalds  *
14191da177e4SLinus Torvalds  * The function returns the number of characters written
142072fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
14211da177e4SLinus Torvalds  * buffer overflows.
142220036fdcSAndi Kleen  *
142320036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
14241da177e4SLinus Torvalds  */
14251da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
14261da177e4SLinus Torvalds {
14271da177e4SLinus Torvalds 	va_list args;
14281da177e4SLinus Torvalds 	int i;
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 	va_start(args, fmt);
14311da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
14321da177e4SLinus Torvalds 	va_end(args);
14337b9186f5SAndré Goddard Rosa 
14341da177e4SLinus Torvalds 	return i;
14351da177e4SLinus Torvalds }
14361da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
14371da177e4SLinus Torvalds 
14384370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
14394370aa4aSLai Jiangshan /*
14404370aa4aSLai Jiangshan  * bprintf service:
14414370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
14424370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
14434370aa4aSLai Jiangshan  */
14444370aa4aSLai Jiangshan 
14454370aa4aSLai Jiangshan /**
14464370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
14474370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
14484370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
14494370aa4aSLai Jiangshan  * @fmt: The format string to use
14504370aa4aSLai Jiangshan  * @args: Arguments for the format string
14514370aa4aSLai Jiangshan  *
14524370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
14534370aa4aSLai Jiangshan  * is skiped.
14544370aa4aSLai Jiangshan  *
14554370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
14564370aa4aSLai Jiangshan  * the given input.
14574370aa4aSLai Jiangshan  *
14584370aa4aSLai Jiangshan  * NOTE:
14594370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
14604370aa4aSLai Jiangshan  * valid for bstr_printf().
14614370aa4aSLai Jiangshan  */
14624370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
14634370aa4aSLai Jiangshan {
1464fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
14654370aa4aSLai Jiangshan 	char *str, *end;
1466fef20d9cSFrederic Weisbecker 	int read;
14674370aa4aSLai Jiangshan 
14684370aa4aSLai Jiangshan 	str = (char *)bin_buf;
14694370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
14704370aa4aSLai Jiangshan 
14714370aa4aSLai Jiangshan #define save_arg(type)							\
14724370aa4aSLai Jiangshan do {									\
14734370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
14744370aa4aSLai Jiangshan 		unsigned long long value;				\
14754370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
14764370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
14774370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
14784370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
14794370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
14804370aa4aSLai Jiangshan 		}							\
14814370aa4aSLai Jiangshan 	} else {							\
14824370aa4aSLai Jiangshan 		unsigned long value;					\
14834370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
14844370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
14854370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
14864370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
14874370aa4aSLai Jiangshan 	}								\
14884370aa4aSLai Jiangshan 	str += sizeof(type);						\
14894370aa4aSLai Jiangshan } while (0)
14904370aa4aSLai Jiangshan 
1491fef20d9cSFrederic Weisbecker 	while (*fmt) {
1492fef20d9cSFrederic Weisbecker 		read = format_decode(fmt, &spec);
14934370aa4aSLai Jiangshan 
1494fef20d9cSFrederic Weisbecker 		fmt += read;
1495fef20d9cSFrederic Weisbecker 
1496fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1497fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
1498fef20d9cSFrederic Weisbecker 			break;
1499fef20d9cSFrederic Weisbecker 
1500ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1501fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
15024370aa4aSLai Jiangshan 			save_arg(int);
1503fef20d9cSFrederic Weisbecker 			break;
15044370aa4aSLai Jiangshan 
1505fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
15064370aa4aSLai Jiangshan 			save_arg(char);
1507fef20d9cSFrederic Weisbecker 			break;
1508fef20d9cSFrederic Weisbecker 
1509fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
15104370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
15114370aa4aSLai Jiangshan 			size_t len;
15126c356634SAndré Goddard Rosa 
15134370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
15144370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
15150f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
15166c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
15176c356634SAndré Goddard Rosa 			if (str + len < end)
15186c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
15196c356634SAndré Goddard Rosa 			str += len;
1520fef20d9cSFrederic Weisbecker 			break;
15214370aa4aSLai Jiangshan 		}
1522fef20d9cSFrederic Weisbecker 
1523fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
15244370aa4aSLai Jiangshan 			save_arg(void *);
15254370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
1526fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
15274370aa4aSLai Jiangshan 				fmt++;
1528fef20d9cSFrederic Weisbecker 			break;
1529fef20d9cSFrederic Weisbecker 
1530fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1531fef20d9cSFrederic Weisbecker 			break;
1532fef20d9cSFrederic Weisbecker 
1533fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1534fef20d9cSFrederic Weisbecker 			break;
1535fef20d9cSFrederic Weisbecker 
1536fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
15374370aa4aSLai Jiangshan 			/* skip %n 's argument */
1538fef20d9cSFrederic Weisbecker 			int qualifier = spec.qualifier;
15394370aa4aSLai Jiangshan 			void *skip_arg;
15404370aa4aSLai Jiangshan 			if (qualifier == 'l')
15414370aa4aSLai Jiangshan 				skip_arg = va_arg(args, long *);
1542*08562cb2SAndré Goddard Rosa 			else if (TOLOWER(qualifier) == 'z')
15434370aa4aSLai Jiangshan 				skip_arg = va_arg(args, size_t *);
15444370aa4aSLai Jiangshan 			else
15454370aa4aSLai Jiangshan 				skip_arg = va_arg(args, int *);
1546fef20d9cSFrederic Weisbecker 			break;
15474370aa4aSLai Jiangshan 		}
15484370aa4aSLai Jiangshan 
1549fef20d9cSFrederic Weisbecker 		default:
1550fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1551fef20d9cSFrederic Weisbecker 
1552fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1553fef20d9cSFrederic Weisbecker 				save_arg(long long);
1554fef20d9cSFrederic Weisbecker 				break;
1555fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1556fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1557fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
1558fef20d9cSFrederic Weisbecker 				break;
1559fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1560fef20d9cSFrederic Weisbecker 				save_arg(size_t);
1561fef20d9cSFrederic Weisbecker 				break;
1562fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1563fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
1564fef20d9cSFrederic Weisbecker 				break;
1565a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1566a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1567a4e94ef0SZhaolei 				save_arg(char);
1568a4e94ef0SZhaolei 				break;
1569fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1570fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1571fef20d9cSFrederic Weisbecker 				save_arg(short);
1572fef20d9cSFrederic Weisbecker 				break;
1573fef20d9cSFrederic Weisbecker 			default:
1574fef20d9cSFrederic Weisbecker 				save_arg(int);
1575fef20d9cSFrederic Weisbecker 			}
1576fef20d9cSFrederic Weisbecker 		}
1577fef20d9cSFrederic Weisbecker 	}
1578fef20d9cSFrederic Weisbecker 
15797b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1580fef20d9cSFrederic Weisbecker #undef save_arg
15814370aa4aSLai Jiangshan }
15824370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
15834370aa4aSLai Jiangshan 
15844370aa4aSLai Jiangshan /**
15854370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
15864370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
15874370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
15884370aa4aSLai Jiangshan  * @fmt: The format string to use
15894370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
15904370aa4aSLai Jiangshan  *
15914370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
15924370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
15934370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
15944370aa4aSLai Jiangshan  *
15954370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
15960efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
15974370aa4aSLai Jiangshan  *
15984370aa4aSLai Jiangshan  * The return value is the number of characters which would
15994370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
16004370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
16014370aa4aSLai Jiangshan  * number of characters written into @buf as return value
16024370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
16034370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
16044370aa4aSLai Jiangshan  * string is truncated.
16054370aa4aSLai Jiangshan  */
16064370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
16074370aa4aSLai Jiangshan {
16084370aa4aSLai Jiangshan 	unsigned long long num;
16094370aa4aSLai Jiangshan 	char *str, *end, c;
16104370aa4aSLai Jiangshan 	const char *args = (const char *)bin_buf;
1611fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
16124370aa4aSLai Jiangshan 
16132f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
16144370aa4aSLai Jiangshan 		return 0;
16154370aa4aSLai Jiangshan 
16164370aa4aSLai Jiangshan 	str = buf;
16174370aa4aSLai Jiangshan 	end = buf + size;
16184370aa4aSLai Jiangshan 
16194370aa4aSLai Jiangshan #define get_arg(type)							\
16204370aa4aSLai Jiangshan ({									\
16214370aa4aSLai Jiangshan 	typeof(type) value;						\
16224370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
16234370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
16244370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
16254370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
16264370aa4aSLai Jiangshan 	} else {							\
16274370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
16284370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
16294370aa4aSLai Jiangshan 	}								\
16304370aa4aSLai Jiangshan 	args += sizeof(type);						\
16314370aa4aSLai Jiangshan 	value;								\
16324370aa4aSLai Jiangshan })
16334370aa4aSLai Jiangshan 
16344370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
16354370aa4aSLai Jiangshan 	if (end < buf) {
16364370aa4aSLai Jiangshan 		end = ((void *)-1);
16374370aa4aSLai Jiangshan 		size = end - buf;
16384370aa4aSLai Jiangshan 	}
16394370aa4aSLai Jiangshan 
1640fef20d9cSFrederic Weisbecker 	while (*fmt) {
1641fef20d9cSFrederic Weisbecker 		int read;
1642fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1643fef20d9cSFrederic Weisbecker 
1644fef20d9cSFrederic Weisbecker 		read = format_decode(fmt, &spec);
1645fef20d9cSFrederic Weisbecker 
1646fef20d9cSFrederic Weisbecker 		fmt += read;
1647fef20d9cSFrederic Weisbecker 
1648fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1649fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1650fef20d9cSFrederic Weisbecker 			int copy = read;
1651fef20d9cSFrederic Weisbecker 			if (str < end) {
1652fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1653fef20d9cSFrederic Weisbecker 					copy = end - str;
1654fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1655fef20d9cSFrederic Weisbecker 			}
1656fef20d9cSFrederic Weisbecker 			str += read;
1657fef20d9cSFrederic Weisbecker 			break;
16584370aa4aSLai Jiangshan 		}
16594370aa4aSLai Jiangshan 
1660ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1661fef20d9cSFrederic Weisbecker 			spec.field_width = get_arg(int);
1662fef20d9cSFrederic Weisbecker 			break;
16634370aa4aSLai Jiangshan 
1664fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1665fef20d9cSFrederic Weisbecker 			spec.precision = get_arg(int);
1666fef20d9cSFrederic Weisbecker 			break;
16674370aa4aSLai Jiangshan 
1668fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
1669fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1670fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
16714370aa4aSLai Jiangshan 					if (str < end)
16724370aa4aSLai Jiangshan 						*str = ' ';
16734370aa4aSLai Jiangshan 					++str;
16744370aa4aSLai Jiangshan 				}
16754370aa4aSLai Jiangshan 			}
16764370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
16774370aa4aSLai Jiangshan 			if (str < end)
16784370aa4aSLai Jiangshan 				*str = c;
16794370aa4aSLai Jiangshan 			++str;
1680fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
16814370aa4aSLai Jiangshan 				if (str < end)
16824370aa4aSLai Jiangshan 					*str = ' ';
16834370aa4aSLai Jiangshan 				++str;
16844370aa4aSLai Jiangshan 			}
1685fef20d9cSFrederic Weisbecker 			break;
16864370aa4aSLai Jiangshan 
1687fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
16884370aa4aSLai Jiangshan 			const char *str_arg = args;
16894370aa4aSLai Jiangshan 			size_t len = strlen(str_arg);
16904370aa4aSLai Jiangshan 			args += len + 1;
1691fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
1692fef20d9cSFrederic Weisbecker 			break;
16934370aa4aSLai Jiangshan 		}
16944370aa4aSLai Jiangshan 
1695fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1696fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, get_arg(void *), spec);
1697fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
16984370aa4aSLai Jiangshan 				fmt++;
1699fef20d9cSFrederic Weisbecker 			break;
17004370aa4aSLai Jiangshan 
1701fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
17024370aa4aSLai Jiangshan 			if (str < end)
17034370aa4aSLai Jiangshan 				*str = '%';
17044370aa4aSLai Jiangshan 			++str;
17054370aa4aSLai Jiangshan 			break;
17064370aa4aSLai Jiangshan 
1707fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
17084370aa4aSLai Jiangshan 			if (str < end)
17094370aa4aSLai Jiangshan 				*str = '%';
17104370aa4aSLai Jiangshan 			++str;
1711fef20d9cSFrederic Weisbecker 			break;
1712fef20d9cSFrederic Weisbecker 
1713fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS:
1714fef20d9cSFrederic Weisbecker 			/* skip */
1715fef20d9cSFrederic Weisbecker 			break;
1716fef20d9cSFrederic Weisbecker 
1717fef20d9cSFrederic Weisbecker 		default:
1718fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1719fef20d9cSFrederic Weisbecker 
1720fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
17214370aa4aSLai Jiangshan 				num = get_arg(long long);
1722fef20d9cSFrederic Weisbecker 				break;
1723fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
17244370aa4aSLai Jiangshan 				num = get_arg(unsigned long);
1725fef20d9cSFrederic Weisbecker 				break;
1726fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1727fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
1728fef20d9cSFrederic Weisbecker 				break;
1729fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
17304370aa4aSLai Jiangshan 				num = get_arg(size_t);
1731fef20d9cSFrederic Weisbecker 				break;
1732fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
17334370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
1734fef20d9cSFrederic Weisbecker 				break;
1735a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1736a4e94ef0SZhaolei 				num = get_arg(unsigned char);
1737a4e94ef0SZhaolei 				break;
1738a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1739a4e94ef0SZhaolei 				num = get_arg(signed char);
1740a4e94ef0SZhaolei 				break;
1741fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1742fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
1743fef20d9cSFrederic Weisbecker 				break;
1744fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1745fef20d9cSFrederic Weisbecker 				num = get_arg(short);
1746fef20d9cSFrederic Weisbecker 				break;
1747fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
17484370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
1749fef20d9cSFrederic Weisbecker 				break;
1750fef20d9cSFrederic Weisbecker 			default:
1751fef20d9cSFrederic Weisbecker 				num = get_arg(int);
17524370aa4aSLai Jiangshan 			}
1753fef20d9cSFrederic Weisbecker 
1754fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
17554370aa4aSLai Jiangshan 		}
1756fef20d9cSFrederic Weisbecker 	}
1757fef20d9cSFrederic Weisbecker 
17584370aa4aSLai Jiangshan 	if (size > 0) {
17594370aa4aSLai Jiangshan 		if (str < end)
17604370aa4aSLai Jiangshan 			*str = '\0';
17614370aa4aSLai Jiangshan 		else
17624370aa4aSLai Jiangshan 			end[-1] = '\0';
17634370aa4aSLai Jiangshan 	}
1764fef20d9cSFrederic Weisbecker 
17654370aa4aSLai Jiangshan #undef get_arg
17664370aa4aSLai Jiangshan 
17674370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
17684370aa4aSLai Jiangshan 	return str - buf;
17694370aa4aSLai Jiangshan }
17704370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
17714370aa4aSLai Jiangshan 
17724370aa4aSLai Jiangshan /**
17734370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
17744370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
17754370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
17764370aa4aSLai Jiangshan  * @fmt: The format string to use
17774370aa4aSLai Jiangshan  * @...: Arguments for the format string
17784370aa4aSLai Jiangshan  *
17794370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
17804370aa4aSLai Jiangshan  * into @bin_buf.
17814370aa4aSLai Jiangshan  */
17824370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
17834370aa4aSLai Jiangshan {
17844370aa4aSLai Jiangshan 	va_list args;
17854370aa4aSLai Jiangshan 	int ret;
17864370aa4aSLai Jiangshan 
17874370aa4aSLai Jiangshan 	va_start(args, fmt);
17884370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
17894370aa4aSLai Jiangshan 	va_end(args);
17907b9186f5SAndré Goddard Rosa 
17914370aa4aSLai Jiangshan 	return ret;
17924370aa4aSLai Jiangshan }
17934370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
17944370aa4aSLai Jiangshan 
17954370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
17964370aa4aSLai Jiangshan 
17971da177e4SLinus Torvalds /**
17981da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
17991da177e4SLinus Torvalds  * @buf:	input buffer
18001da177e4SLinus Torvalds  * @fmt:	format of buffer
18011da177e4SLinus Torvalds  * @args:	arguments
18021da177e4SLinus Torvalds  */
18031da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
18041da177e4SLinus Torvalds {
18051da177e4SLinus Torvalds 	const char *str = buf;
18061da177e4SLinus Torvalds 	char *next;
18071da177e4SLinus Torvalds 	char digit;
18081da177e4SLinus Torvalds 	int num = 0;
18097b9186f5SAndré Goddard Rosa 	int qualifier, base, field_width;
18101da177e4SLinus Torvalds 	int is_sign = 0;
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 	while (*fmt && *str) {
18131da177e4SLinus Torvalds 		/* skip any white space in format */
18141da177e4SLinus Torvalds 		/* white space in format matchs any amount of
18151da177e4SLinus Torvalds 		 * white space, including none, in the input.
18161da177e4SLinus Torvalds 		 */
18171da177e4SLinus Torvalds 		if (isspace(*fmt)) {
18181da177e4SLinus Torvalds 			while (isspace(*fmt))
18191da177e4SLinus Torvalds 				++fmt;
18201da177e4SLinus Torvalds 			while (isspace(*str))
18211da177e4SLinus Torvalds 				++str;
18221da177e4SLinus Torvalds 		}
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
18251da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
18261da177e4SLinus Torvalds 			if (*fmt++ != *str++)
18271da177e4SLinus Torvalds 				break;
18281da177e4SLinus Torvalds 			continue;
18291da177e4SLinus Torvalds 		}
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds 		if (!*fmt)
18321da177e4SLinus Torvalds 			break;
18331da177e4SLinus Torvalds 		++fmt;
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 		/* skip this conversion.
18361da177e4SLinus Torvalds 		 * advance both strings to next white space
18371da177e4SLinus Torvalds 		 */
18381da177e4SLinus Torvalds 		if (*fmt == '*') {
18398fccae2cSAndy Spencer 			while (!isspace(*fmt) && *fmt != '%' && *fmt)
18401da177e4SLinus Torvalds 				fmt++;
18411da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
18421da177e4SLinus Torvalds 				str++;
18431da177e4SLinus Torvalds 			continue;
18441da177e4SLinus Torvalds 		}
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 		/* get field width */
18471da177e4SLinus Torvalds 		field_width = -1;
18481da177e4SLinus Torvalds 		if (isdigit(*fmt))
18491da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 		/* get conversion qualifier */
18521da177e4SLinus Torvalds 		qualifier = -1;
1853*08562cb2SAndré Goddard Rosa 		if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
1854*08562cb2SAndré Goddard Rosa 		    TOLOWER(*fmt) == 'z') {
18551da177e4SLinus Torvalds 			qualifier = *fmt++;
18561da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
18571da177e4SLinus Torvalds 				if (qualifier == 'h') {
18581da177e4SLinus Torvalds 					qualifier = 'H';
18591da177e4SLinus Torvalds 					fmt++;
18601da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
18611da177e4SLinus Torvalds 					qualifier = 'L';
18621da177e4SLinus Torvalds 					fmt++;
18631da177e4SLinus Torvalds 				}
18641da177e4SLinus Torvalds 			}
18651da177e4SLinus Torvalds 		}
18661da177e4SLinus Torvalds 		base = 10;
18671da177e4SLinus Torvalds 		is_sign = 0;
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 		if (!*fmt || !*str)
18701da177e4SLinus Torvalds 			break;
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds 		switch (*fmt++) {
18731da177e4SLinus Torvalds 		case 'c':
18741da177e4SLinus Torvalds 		{
18751da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
18761da177e4SLinus Torvalds 			if (field_width == -1)
18771da177e4SLinus Torvalds 				field_width = 1;
18781da177e4SLinus Torvalds 			do {
18791da177e4SLinus Torvalds 				*s++ = *str++;
18801da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
18811da177e4SLinus Torvalds 			num++;
18821da177e4SLinus Torvalds 		}
18831da177e4SLinus Torvalds 		continue;
18841da177e4SLinus Torvalds 		case 's':
18851da177e4SLinus Torvalds 		{
18861da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
18871da177e4SLinus Torvalds 			if (field_width == -1)
18881da177e4SLinus Torvalds 				field_width = INT_MAX;
18891da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
18901da177e4SLinus Torvalds 			while (isspace(*str))
18911da177e4SLinus Torvalds 				str++;
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 			/* now copy until next white space */
18947b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
18951da177e4SLinus Torvalds 				*s++ = *str++;
18961da177e4SLinus Torvalds 			*s = '\0';
18971da177e4SLinus Torvalds 			num++;
18981da177e4SLinus Torvalds 		}
18991da177e4SLinus Torvalds 		continue;
19001da177e4SLinus Torvalds 		case 'n':
19011da177e4SLinus Torvalds 			/* return number of characters read so far */
19021da177e4SLinus Torvalds 		{
19031da177e4SLinus Torvalds 			int *i = (int *)va_arg(args, int*);
19041da177e4SLinus Torvalds 			*i = str - buf;
19051da177e4SLinus Torvalds 		}
19061da177e4SLinus Torvalds 		continue;
19071da177e4SLinus Torvalds 		case 'o':
19081da177e4SLinus Torvalds 			base = 8;
19091da177e4SLinus Torvalds 			break;
19101da177e4SLinus Torvalds 		case 'x':
19111da177e4SLinus Torvalds 		case 'X':
19121da177e4SLinus Torvalds 			base = 16;
19131da177e4SLinus Torvalds 			break;
19141da177e4SLinus Torvalds 		case 'i':
19151da177e4SLinus Torvalds 			base = 0;
19161da177e4SLinus Torvalds 		case 'd':
19171da177e4SLinus Torvalds 			is_sign = 1;
19181da177e4SLinus Torvalds 		case 'u':
19191da177e4SLinus Torvalds 			break;
19201da177e4SLinus Torvalds 		case '%':
19211da177e4SLinus Torvalds 			/* looking for '%' in str */
19221da177e4SLinus Torvalds 			if (*str++ != '%')
19231da177e4SLinus Torvalds 				return num;
19241da177e4SLinus Torvalds 			continue;
19251da177e4SLinus Torvalds 		default:
19261da177e4SLinus Torvalds 			/* invalid format; stop here */
19271da177e4SLinus Torvalds 			return num;
19281da177e4SLinus Torvalds 		}
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 		/* have some sort of integer conversion.
19311da177e4SLinus Torvalds 		 * first, skip white space in buffer.
19321da177e4SLinus Torvalds 		 */
19331da177e4SLinus Torvalds 		while (isspace(*str))
19341da177e4SLinus Torvalds 			str++;
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds 		digit = *str;
19371da177e4SLinus Torvalds 		if (is_sign && digit == '-')
19381da177e4SLinus Torvalds 			digit = *(str + 1);
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 		if (!digit
19411da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
19421da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
19431da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
19441da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
19451da177e4SLinus Torvalds 			break;
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 		switch (qualifier) {
19481da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
19491da177e4SLinus Torvalds 			if (is_sign) {
19501da177e4SLinus Torvalds 				signed char *s = (signed char *)va_arg(args, signed char *);
19511da177e4SLinus Torvalds 				*s = (signed char)simple_strtol(str, &next, base);
19521da177e4SLinus Torvalds 			} else {
19531da177e4SLinus Torvalds 				unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
19541da177e4SLinus Torvalds 				*s = (unsigned char)simple_strtoul(str, &next, base);
19551da177e4SLinus Torvalds 			}
19561da177e4SLinus Torvalds 			break;
19571da177e4SLinus Torvalds 		case 'h':
19581da177e4SLinus Torvalds 			if (is_sign) {
19591da177e4SLinus Torvalds 				short *s = (short *)va_arg(args, short *);
19601da177e4SLinus Torvalds 				*s = (short)simple_strtol(str, &next, base);
19611da177e4SLinus Torvalds 			} else {
19621da177e4SLinus Torvalds 				unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
19631da177e4SLinus Torvalds 				*s = (unsigned short)simple_strtoul(str, &next, base);
19641da177e4SLinus Torvalds 			}
19651da177e4SLinus Torvalds 			break;
19661da177e4SLinus Torvalds 		case 'l':
19671da177e4SLinus Torvalds 			if (is_sign) {
19681da177e4SLinus Torvalds 				long *l = (long *)va_arg(args, long *);
19691da177e4SLinus Torvalds 				*l = simple_strtol(str, &next, base);
19701da177e4SLinus Torvalds 			} else {
19711da177e4SLinus Torvalds 				unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
19721da177e4SLinus Torvalds 				*l = simple_strtoul(str, &next, base);
19731da177e4SLinus Torvalds 			}
19741da177e4SLinus Torvalds 			break;
19751da177e4SLinus Torvalds 		case 'L':
19761da177e4SLinus Torvalds 			if (is_sign) {
19771da177e4SLinus Torvalds 				long long *l = (long long *)va_arg(args, long long *);
19781da177e4SLinus Torvalds 				*l = simple_strtoll(str, &next, base);
19791da177e4SLinus Torvalds 			} else {
19801da177e4SLinus Torvalds 				unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
19811da177e4SLinus Torvalds 				*l = simple_strtoull(str, &next, base);
19821da177e4SLinus Torvalds 			}
19831da177e4SLinus Torvalds 			break;
19841da177e4SLinus Torvalds 		case 'Z':
19851da177e4SLinus Torvalds 		case 'z':
19861da177e4SLinus Torvalds 		{
19871da177e4SLinus Torvalds 			size_t *s = (size_t *)va_arg(args, size_t *);
19881da177e4SLinus Torvalds 			*s = (size_t)simple_strtoul(str, &next, base);
19891da177e4SLinus Torvalds 		}
19901da177e4SLinus Torvalds 		break;
19911da177e4SLinus Torvalds 		default:
19921da177e4SLinus Torvalds 			if (is_sign) {
19931da177e4SLinus Torvalds 				int *i = (int *)va_arg(args, int *);
19941da177e4SLinus Torvalds 				*i = (int)simple_strtol(str, &next, base);
19951da177e4SLinus Torvalds 			} else {
19961da177e4SLinus Torvalds 				unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
19971da177e4SLinus Torvalds 				*i = (unsigned int)simple_strtoul(str, &next, base);
19981da177e4SLinus Torvalds 			}
19991da177e4SLinus Torvalds 			break;
20001da177e4SLinus Torvalds 		}
20011da177e4SLinus Torvalds 		num++;
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 		if (!next)
20041da177e4SLinus Torvalds 			break;
20051da177e4SLinus Torvalds 		str = next;
20061da177e4SLinus Torvalds 	}
2007c6b40d16SJohannes Berg 
2008c6b40d16SJohannes Berg 	/*
2009c6b40d16SJohannes Berg 	 * Now we've come all the way through so either the input string or the
2010c6b40d16SJohannes Berg 	 * format ended. In the former case, there can be a %n at the current
2011c6b40d16SJohannes Berg 	 * position in the format that needs to be filled.
2012c6b40d16SJohannes Berg 	 */
2013c6b40d16SJohannes Berg 	if (*fmt == '%' && *(fmt + 1) == 'n') {
2014c6b40d16SJohannes Berg 		int *p = (int *)va_arg(args, int *);
2015c6b40d16SJohannes Berg 		*p = str - buf;
2016c6b40d16SJohannes Berg 	}
2017c6b40d16SJohannes Berg 
20181da177e4SLinus Torvalds 	return num;
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds /**
20231da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
20241da177e4SLinus Torvalds  * @buf:	input buffer
20251da177e4SLinus Torvalds  * @fmt:	formatting of buffer
20261da177e4SLinus Torvalds  * @...:	resulting arguments
20271da177e4SLinus Torvalds  */
20281da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
20291da177e4SLinus Torvalds {
20301da177e4SLinus Torvalds 	va_list args;
20311da177e4SLinus Torvalds 	int i;
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds 	va_start(args, fmt);
20341da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
20351da177e4SLinus Torvalds 	va_end(args);
20367b9186f5SAndré Goddard Rosa 
20371da177e4SLinus Torvalds 	return i;
20381da177e4SLinus Torvalds }
20391da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
2040