xref: /openbmc/linux/lib/vsprintf.c (revision 3f623eba2a7fc01b0341f7989aa6c5ed91b9adb6)
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>
208bc3bcc9SPaul Gortmaker #include <linux/module.h>	/* for KSYM_SYMBOL_LEN */
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>
2653809751SJan Beulich #include <linux/math64.h>
270fe1ef24SLinus Torvalds #include <linux/uaccess.h>
28332d2e78SLinus Torvalds #include <linux/ioport.h>
294b6ccca7SAl Viro #include <linux/dcache.h>
30312b4e22SRyan Mallon #include <linux/cred.h>
318a27f7c9SJoe Perches #include <net/addrconf.h>
321da177e4SLinus Torvalds 
334e57b681STim Schmielau #include <asm/page.h>		/* for PAGE_SIZE */
34deac93dfSJames Bottomley #include <asm/sections.h>	/* for dereference_function_descriptor() */
351da177e4SLinus Torvalds 
361dff46d6SAlexey Dobriyan #include "kstrtox.h"
37aa46a63eSHarvey Harrison 
381da177e4SLinus Torvalds /**
391da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
401da177e4SLinus Torvalds  * @cp: The start of the string
411da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
421da177e4SLinus Torvalds  * @base: The number base to use
43462e4711SEldad Zack  *
44462e4711SEldad Zack  * This function is obsolete. Please use kstrtoull instead.
451da177e4SLinus Torvalds  */
461da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
471da177e4SLinus Torvalds {
481dff46d6SAlexey Dobriyan 	unsigned long long result;
491dff46d6SAlexey Dobriyan 	unsigned int rv;
501da177e4SLinus Torvalds 
511dff46d6SAlexey Dobriyan 	cp = _parse_integer_fixup_radix(cp, &base);
521dff46d6SAlexey Dobriyan 	rv = _parse_integer(cp, base, &result);
531dff46d6SAlexey Dobriyan 	/* FIXME */
541dff46d6SAlexey Dobriyan 	cp += (rv & ~KSTRTOX_OVERFLOW);
55aa46a63eSHarvey Harrison 
561da177e4SLinus Torvalds 	if (endp)
571da177e4SLinus Torvalds 		*endp = (char *)cp;
587b9186f5SAndré Goddard Rosa 
591da177e4SLinus Torvalds 	return result;
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds /**
64922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
65922ac25cSAndré Goddard Rosa  * @cp: The start of the string
66922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
67922ac25cSAndré Goddard Rosa  * @base: The number base to use
68462e4711SEldad Zack  *
69462e4711SEldad Zack  * This function is obsolete. Please use kstrtoul instead.
70922ac25cSAndré Goddard Rosa  */
71922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
72922ac25cSAndré Goddard Rosa {
73922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
74922ac25cSAndré Goddard Rosa }
75922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
76922ac25cSAndré Goddard Rosa 
77922ac25cSAndré Goddard Rosa /**
78922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
79922ac25cSAndré Goddard Rosa  * @cp: The start of the string
80922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
81922ac25cSAndré Goddard Rosa  * @base: The number base to use
82462e4711SEldad Zack  *
83462e4711SEldad Zack  * This function is obsolete. Please use kstrtol instead.
84922ac25cSAndré Goddard Rosa  */
85922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
86922ac25cSAndré Goddard Rosa {
87922ac25cSAndré Goddard Rosa 	if (*cp == '-')
88922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
89922ac25cSAndré Goddard Rosa 
90922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
91922ac25cSAndré Goddard Rosa }
92922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
93922ac25cSAndré Goddard Rosa 
94922ac25cSAndré Goddard Rosa /**
951da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
961da177e4SLinus Torvalds  * @cp: The start of the string
971da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
981da177e4SLinus Torvalds  * @base: The number base to use
99462e4711SEldad Zack  *
100462e4711SEldad Zack  * This function is obsolete. Please use kstrtoll instead.
1011da177e4SLinus Torvalds  */
1021da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	if (*cp == '-')
1051da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1067b9186f5SAndré Goddard Rosa 
1071da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1081da177e4SLinus Torvalds }
10998d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1101da177e4SLinus Torvalds 
111cf3b429bSJoe Perches static noinline_for_stack
112cf3b429bSJoe Perches int skip_atoi(const char **s)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	int i = 0;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	while (isdigit(**s))
1171da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
1187b9186f5SAndré Goddard Rosa 
1191da177e4SLinus Torvalds 	return i;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1224277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used
1234277eeddSDenis Vlasenko  * for /proc and /sys data. This directly impacts e.g. top performance
1244277eeddSDenis Vlasenko  * with many processes running. We optimize it for speed
125133fd9f5SDenys Vlasenko  * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
126133fd9f5SDenys Vlasenko  * (with permission from the author, Douglas W. Jones).
127133fd9f5SDenys Vlasenko  */
1284277eeddSDenis Vlasenko 
129133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
130133fd9f5SDenys Vlasenko /* Formats correctly any integer in [0, 999999999] */
131cf3b429bSJoe Perches static noinline_for_stack
132133fd9f5SDenys Vlasenko char *put_dec_full9(char *buf, unsigned q)
1334277eeddSDenis Vlasenko {
134133fd9f5SDenys Vlasenko 	unsigned r;
1354277eeddSDenis Vlasenko 
1367b9186f5SAndré Goddard Rosa 	/*
1377b9186f5SAndré Goddard Rosa 	 * Possible ways to approx. divide by 10
138133fd9f5SDenys Vlasenko 	 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
139133fd9f5SDenys Vlasenko 	 * (x * 0xcccd) >> 19     x <      81920 (x < 262149 when 64-bit mul)
140133fd9f5SDenys Vlasenko 	 * (x * 0x6667) >> 18     x <      43699
141133fd9f5SDenys Vlasenko 	 * (x * 0x3334) >> 17     x <      16389
142133fd9f5SDenys Vlasenko 	 * (x * 0x199a) >> 16     x <      16389
143133fd9f5SDenys Vlasenko 	 * (x * 0x0ccd) >> 15     x <      16389
144133fd9f5SDenys Vlasenko 	 * (x * 0x0667) >> 14     x <       2739
145133fd9f5SDenys Vlasenko 	 * (x * 0x0334) >> 13     x <       1029
146133fd9f5SDenys Vlasenko 	 * (x * 0x019a) >> 12     x <       1029
147133fd9f5SDenys Vlasenko 	 * (x * 0x00cd) >> 11     x <       1029 shorter code than * 0x67 (on i386)
148133fd9f5SDenys Vlasenko 	 * (x * 0x0067) >> 10     x <        179
149133fd9f5SDenys Vlasenko 	 * (x * 0x0034) >>  9     x <         69 same
150133fd9f5SDenys Vlasenko 	 * (x * 0x001a) >>  8     x <         69 same
151133fd9f5SDenys Vlasenko 	 * (x * 0x000d) >>  7     x <         69 same, shortest code (on i386)
152133fd9f5SDenys Vlasenko 	 * (x * 0x0007) >>  6     x <         19
153133fd9f5SDenys Vlasenko 	 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
1547b9186f5SAndré Goddard Rosa 	 */
155133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
156133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 1 */
157133fd9f5SDenys Vlasenko 	q      = (r * (uint64_t)0x1999999a) >> 32;
158133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 2 */
159133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
160133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 3 */
161133fd9f5SDenys Vlasenko 	q      = (r * (uint64_t)0x1999999a) >> 32;
162133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 4 */
163133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
164133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 5 */
165133fd9f5SDenys Vlasenko 	/* Now value is under 10000, can avoid 64-bit multiply */
166133fd9f5SDenys Vlasenko 	q      = (r * 0x199a) >> 16;
167133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q)  + '0'; /* 6 */
168133fd9f5SDenys Vlasenko 	r      = (q * 0xcd) >> 11;
169133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r)  + '0'; /* 7 */
170133fd9f5SDenys Vlasenko 	q      = (r * 0xcd) >> 11;
171133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 8 */
172133fd9f5SDenys Vlasenko 	*buf++ = q + '0'; /* 9 */
173133fd9f5SDenys Vlasenko 	return buf;
174133fd9f5SDenys Vlasenko }
175133fd9f5SDenys Vlasenko #endif
1764277eeddSDenis Vlasenko 
177133fd9f5SDenys Vlasenko /* Similar to above but do not pad with zeros.
178133fd9f5SDenys Vlasenko  * Code can be easily arranged to print 9 digits too, but our callers
179133fd9f5SDenys Vlasenko  * always call put_dec_full9() instead when the number has 9 decimal digits.
180133fd9f5SDenys Vlasenko  */
181133fd9f5SDenys Vlasenko static noinline_for_stack
182133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r)
183133fd9f5SDenys Vlasenko {
184133fd9f5SDenys Vlasenko 	unsigned q;
1854277eeddSDenis Vlasenko 
186133fd9f5SDenys Vlasenko 	/* Copy of previous function's body with added early returns */
187cb239d0aSGeorge Spelvin 	while (r >= 10000) {
188cb239d0aSGeorge Spelvin 		q = r + '0';
189cb239d0aSGeorge Spelvin 		r  = (r * (uint64_t)0x1999999a) >> 32;
190cb239d0aSGeorge Spelvin 		*buf++ = q - 10*r;
191cb239d0aSGeorge Spelvin 	}
192cb239d0aSGeorge Spelvin 
193f4000516SGeorge Spelvin 	q      = (r * 0x199a) >> 16;	/* r <= 9999 */
194f4000516SGeorge Spelvin 	*buf++ = (r - 10 * q)  + '0';
195133fd9f5SDenys Vlasenko 	if (q == 0)
196133fd9f5SDenys Vlasenko 		return buf;
197f4000516SGeorge Spelvin 	r      = (q * 0xcd) >> 11;	/* q <= 999 */
198f4000516SGeorge Spelvin 	*buf++ = (q - 10 * r)  + '0';
199133fd9f5SDenys Vlasenko 	if (r == 0)
200133fd9f5SDenys Vlasenko 		return buf;
201f4000516SGeorge Spelvin 	q      = (r * 0xcd) >> 11;	/* r <= 99 */
202f4000516SGeorge Spelvin 	*buf++ = (r - 10 * q) + '0';
203133fd9f5SDenys Vlasenko 	if (q == 0)
204133fd9f5SDenys Vlasenko 		return buf;
205f4000516SGeorge Spelvin 	*buf++ = q + '0';		 /* q <= 9 */
206133fd9f5SDenys Vlasenko 	return buf;
207133fd9f5SDenys Vlasenko }
208133fd9f5SDenys Vlasenko 
209133fd9f5SDenys Vlasenko /* There are two algorithms to print larger numbers.
210133fd9f5SDenys Vlasenko  * One is generic: divide by 1000000000 and repeatedly print
211133fd9f5SDenys Vlasenko  * groups of (up to) 9 digits. It's conceptually simple,
212133fd9f5SDenys Vlasenko  * but requires a (unsigned long long) / 1000000000 division.
213133fd9f5SDenys Vlasenko  *
214133fd9f5SDenys Vlasenko  * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
215133fd9f5SDenys Vlasenko  * manipulates them cleverly and generates groups of 4 decimal digits.
216133fd9f5SDenys Vlasenko  * It so happens that it does NOT require long long division.
217133fd9f5SDenys Vlasenko  *
218133fd9f5SDenys Vlasenko  * If long is > 32 bits, division of 64-bit values is relatively easy,
219133fd9f5SDenys Vlasenko  * and we will use the first algorithm.
220133fd9f5SDenys Vlasenko  * If long long is > 64 bits (strange architecture with VERY large long long),
221133fd9f5SDenys Vlasenko  * second algorithm can't be used, and we again use the first one.
222133fd9f5SDenys Vlasenko  *
223133fd9f5SDenys Vlasenko  * Else (if long is 32 bits and long long is 64 bits) we use second one.
224133fd9f5SDenys Vlasenko  */
225133fd9f5SDenys Vlasenko 
226133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
227133fd9f5SDenys Vlasenko 
228133fd9f5SDenys Vlasenko /* First algorithm: generic */
229133fd9f5SDenys Vlasenko 
230133fd9f5SDenys Vlasenko static
231133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
232133fd9f5SDenys Vlasenko {
233133fd9f5SDenys Vlasenko 	if (n >= 100*1000*1000) {
234133fd9f5SDenys Vlasenko 		while (n >= 1000*1000*1000)
235133fd9f5SDenys Vlasenko 			buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
236133fd9f5SDenys Vlasenko 		if (n >= 100*1000*1000)
237133fd9f5SDenys Vlasenko 			return put_dec_full9(buf, n);
238133fd9f5SDenys Vlasenko 	}
239133fd9f5SDenys Vlasenko 	return put_dec_trunc8(buf, n);
240133fd9f5SDenys Vlasenko }
241133fd9f5SDenys Vlasenko 
242133fd9f5SDenys Vlasenko #else
243133fd9f5SDenys Vlasenko 
244133fd9f5SDenys Vlasenko /* Second algorithm: valid only for 64-bit long longs */
245133fd9f5SDenys Vlasenko 
246e49317d4SGeorge Spelvin /* See comment in put_dec_full9 for choice of constants */
247133fd9f5SDenys Vlasenko static noinline_for_stack
2482359172aSGeorge Spelvin void put_dec_full4(char *buf, unsigned q)
249133fd9f5SDenys Vlasenko {
250133fd9f5SDenys Vlasenko 	unsigned r;
251e49317d4SGeorge Spelvin 	r      = (q * 0xccd) >> 15;
2522359172aSGeorge Spelvin 	buf[0] = (q - 10 * r) + '0';
253e49317d4SGeorge Spelvin 	q      = (r * 0xcd) >> 11;
2542359172aSGeorge Spelvin 	buf[1] = (r - 10 * q)  + '0';
255133fd9f5SDenys Vlasenko 	r      = (q * 0xcd) >> 11;
2562359172aSGeorge Spelvin 	buf[2] = (q - 10 * r)  + '0';
2572359172aSGeorge Spelvin 	buf[3] = r + '0';
2582359172aSGeorge Spelvin }
2592359172aSGeorge Spelvin 
2602359172aSGeorge Spelvin /*
2612359172aSGeorge Spelvin  * Call put_dec_full4 on x % 10000, return x / 10000.
2622359172aSGeorge Spelvin  * The approximation x/10000 == (x * 0x346DC5D7) >> 43
2632359172aSGeorge Spelvin  * holds for all x < 1,128,869,999.  The largest value this
2642359172aSGeorge Spelvin  * helper will ever be asked to convert is 1,125,520,955.
2652359172aSGeorge Spelvin  * (d1 in the put_dec code, assuming n is all-ones).
2662359172aSGeorge Spelvin  */
2672359172aSGeorge Spelvin static
2682359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x)
2692359172aSGeorge Spelvin {
2702359172aSGeorge Spelvin         uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
2712359172aSGeorge Spelvin 
2722359172aSGeorge Spelvin         put_dec_full4(buf, x - q * 10000);
2732359172aSGeorge Spelvin         return q;
274133fd9f5SDenys Vlasenko }
275133fd9f5SDenys Vlasenko 
276133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at
277133fd9f5SDenys Vlasenko  * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
278133fd9f5SDenys Vlasenko  * (with permission from the author).
279133fd9f5SDenys Vlasenko  * Performs no 64-bit division and hence should be fast on 32-bit machines.
280133fd9f5SDenys Vlasenko  */
281133fd9f5SDenys Vlasenko static
282133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
283133fd9f5SDenys Vlasenko {
284133fd9f5SDenys Vlasenko 	uint32_t d3, d2, d1, q, h;
285133fd9f5SDenys Vlasenko 
286133fd9f5SDenys Vlasenko 	if (n < 100*1000*1000)
287133fd9f5SDenys Vlasenko 		return put_dec_trunc8(buf, n);
288133fd9f5SDenys Vlasenko 
289133fd9f5SDenys Vlasenko 	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
290133fd9f5SDenys Vlasenko 	h   = (n >> 32);
291133fd9f5SDenys Vlasenko 	d2  = (h      ) & 0xffff;
292133fd9f5SDenys Vlasenko 	d3  = (h >> 16); /* implicit "& 0xffff" */
293133fd9f5SDenys Vlasenko 
294133fd9f5SDenys Vlasenko 	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
2952359172aSGeorge Spelvin 	q = put_dec_helper4(buf, q);
296133fd9f5SDenys Vlasenko 
2972359172aSGeorge Spelvin 	q += 7671 * d3 + 9496 * d2 + 6 * d1;
2982359172aSGeorge Spelvin 	q = put_dec_helper4(buf+4, q);
299133fd9f5SDenys Vlasenko 
3002359172aSGeorge Spelvin 	q += 4749 * d3 + 42 * d2;
3012359172aSGeorge Spelvin 	q = put_dec_helper4(buf+8, q);
302133fd9f5SDenys Vlasenko 
3032359172aSGeorge Spelvin 	q += 281 * d3;
3042359172aSGeorge Spelvin 	buf += 12;
3052359172aSGeorge Spelvin 	if (q)
3062359172aSGeorge Spelvin 		buf = put_dec_trunc8(buf, q);
3072359172aSGeorge Spelvin 	else while (buf[-1] == '0')
308133fd9f5SDenys Vlasenko 		--buf;
3097b9186f5SAndré Goddard Rosa 
3104277eeddSDenis Vlasenko 	return buf;
3114277eeddSDenis Vlasenko }
312133fd9f5SDenys Vlasenko 
313133fd9f5SDenys Vlasenko #endif
3144277eeddSDenis Vlasenko 
3151ac101a5SKAMEZAWA Hiroyuki /*
3161ac101a5SKAMEZAWA Hiroyuki  * Convert passed number to decimal string.
3171ac101a5SKAMEZAWA Hiroyuki  * Returns the length of string.  On buffer overflow, returns 0.
3181ac101a5SKAMEZAWA Hiroyuki  *
3191ac101a5SKAMEZAWA Hiroyuki  * If speed is not important, use snprintf(). It's easy to read the code.
3201ac101a5SKAMEZAWA Hiroyuki  */
3211ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num)
3221ac101a5SKAMEZAWA Hiroyuki {
323133fd9f5SDenys Vlasenko 	char tmp[sizeof(num) * 3];
3241ac101a5SKAMEZAWA Hiroyuki 	int idx, len;
3251ac101a5SKAMEZAWA Hiroyuki 
326133fd9f5SDenys Vlasenko 	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
327133fd9f5SDenys Vlasenko 	if (num <= 9) {
328133fd9f5SDenys Vlasenko 		tmp[0] = '0' + num;
329133fd9f5SDenys Vlasenko 		len = 1;
330133fd9f5SDenys Vlasenko 	} else {
3311ac101a5SKAMEZAWA Hiroyuki 		len = put_dec(tmp, num) - tmp;
332133fd9f5SDenys Vlasenko 	}
3331ac101a5SKAMEZAWA Hiroyuki 
3341ac101a5SKAMEZAWA Hiroyuki 	if (len > size)
3351ac101a5SKAMEZAWA Hiroyuki 		return 0;
3361ac101a5SKAMEZAWA Hiroyuki 	for (idx = 0; idx < len; ++idx)
3371ac101a5SKAMEZAWA Hiroyuki 		buf[idx] = tmp[len - idx - 1];
3381ac101a5SKAMEZAWA Hiroyuki 	return len;
3391ac101a5SKAMEZAWA Hiroyuki }
3401ac101a5SKAMEZAWA Hiroyuki 
3411da177e4SLinus Torvalds #define ZEROPAD	1		/* pad with zero */
3421da177e4SLinus Torvalds #define SIGN	2		/* unsigned/signed long */
3431da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3441da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
3451da177e4SLinus Torvalds #define LEFT	16		/* left justified */
346b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
347b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
3481da177e4SLinus Torvalds 
349fef20d9cSFrederic Weisbecker enum format_type {
350fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
351ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
352fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
353fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
354fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
355fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
356fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
357fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
358fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
359fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
360fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
361a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
362a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
363fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
364fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
365fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
366fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
367fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
368fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
369fef20d9cSFrederic Weisbecker };
370fef20d9cSFrederic Weisbecker 
371fef20d9cSFrederic Weisbecker struct printf_spec {
3724e310fdaSJoe Perches 	u8	type;		/* format_type enum */
373ef0658f3SJoe Perches 	u8	flags;		/* flags to number() */
3744e310fdaSJoe Perches 	u8	base;		/* number base, 8, 10 or 16 only */
3754e310fdaSJoe Perches 	u8	qualifier;	/* number qualifier, one of 'hHlLtzZ' */
3764e310fdaSJoe Perches 	s16	field_width;	/* width of output field */
3774e310fdaSJoe Perches 	s16	precision;	/* # of digits/chars */
378fef20d9cSFrederic Weisbecker };
379fef20d9cSFrederic Weisbecker 
380cf3b429bSJoe Perches static noinline_for_stack
381cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
382fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
3831da177e4SLinus Torvalds {
3849b706aeeSDenys Vlasenko 	/* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
3859b706aeeSDenys Vlasenko 	static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
3869b706aeeSDenys Vlasenko 
3879b706aeeSDenys Vlasenko 	char tmp[66];
3889b706aeeSDenys Vlasenko 	char sign;
3899b706aeeSDenys Vlasenko 	char locase;
390fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
3911da177e4SLinus Torvalds 	int i;
3927c203422SPierre Carrier 	bool is_zero = num == 0LL;
3931da177e4SLinus Torvalds 
3949b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
3959b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
396fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
397fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
398fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
3991da177e4SLinus Torvalds 	sign = 0;
400fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4011da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4021da177e4SLinus Torvalds 			sign = '-';
4031da177e4SLinus Torvalds 			num = -(signed long long)num;
404fef20d9cSFrederic Weisbecker 			spec.field_width--;
405fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4061da177e4SLinus Torvalds 			sign = '+';
407fef20d9cSFrederic Weisbecker 			spec.field_width--;
408fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4091da177e4SLinus Torvalds 			sign = ' ';
410fef20d9cSFrederic Weisbecker 			spec.field_width--;
4111da177e4SLinus Torvalds 		}
4121da177e4SLinus Torvalds 	}
413b39a7340SDenis Vlasenko 	if (need_pfx) {
414fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
4157c203422SPierre Carrier 			spec.field_width -= 2;
4167c203422SPierre Carrier 		else if (!is_zero)
417fef20d9cSFrederic Weisbecker 			spec.field_width--;
4181da177e4SLinus Torvalds 	}
419b39a7340SDenis Vlasenko 
420b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4211da177e4SLinus Torvalds 	i = 0;
422133fd9f5SDenys Vlasenko 	if (num < spec.base)
423133fd9f5SDenys Vlasenko 		tmp[i++] = digits[num] | locase;
4244277eeddSDenis Vlasenko 	/* Generic code, for any base:
4254277eeddSDenis Vlasenko 	else do {
4269b706aeeSDenys Vlasenko 		tmp[i++] = (digits[do_div(num,base)] | locase);
4274277eeddSDenis Vlasenko 	} while (num != 0);
4284277eeddSDenis Vlasenko 	*/
429fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
430fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
431b39a7340SDenis Vlasenko 		int shift = 3;
4327b9186f5SAndré Goddard Rosa 
4337b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4347b9186f5SAndré Goddard Rosa 			shift = 4;
435b39a7340SDenis Vlasenko 		do {
4369b706aeeSDenys Vlasenko 			tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
437b39a7340SDenis Vlasenko 			num >>= shift;
438b39a7340SDenis Vlasenko 		} while (num);
4394277eeddSDenis Vlasenko 	} else { /* base 10 */
4404277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4414277eeddSDenis Vlasenko 	}
442b39a7340SDenis Vlasenko 
443b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
444fef20d9cSFrederic Weisbecker 	if (i > spec.precision)
445fef20d9cSFrederic Weisbecker 		spec.precision = i;
446b39a7340SDenis Vlasenko 	/* leading space padding */
447fef20d9cSFrederic Weisbecker 	spec.field_width -= spec.precision;
448fef20d9cSFrederic Weisbecker 	if (!(spec.flags & (ZEROPAD+LEFT))) {
449fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
450f796937aSJeremy Fitzhardinge 			if (buf < end)
4511da177e4SLinus Torvalds 				*buf = ' ';
4521da177e4SLinus Torvalds 			++buf;
4531da177e4SLinus Torvalds 		}
4541da177e4SLinus Torvalds 	}
455b39a7340SDenis Vlasenko 	/* sign */
4561da177e4SLinus Torvalds 	if (sign) {
457f796937aSJeremy Fitzhardinge 		if (buf < end)
4581da177e4SLinus Torvalds 			*buf = sign;
4591da177e4SLinus Torvalds 		++buf;
4601da177e4SLinus Torvalds 	}
461b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
462b39a7340SDenis Vlasenko 	if (need_pfx) {
4637c203422SPierre Carrier 		if (spec.base == 16 || !is_zero) {
464f796937aSJeremy Fitzhardinge 			if (buf < end)
4651da177e4SLinus Torvalds 				*buf = '0';
4661da177e4SLinus Torvalds 			++buf;
4677c203422SPierre Carrier 		}
468fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
469f796937aSJeremy Fitzhardinge 			if (buf < end)
4709b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
4711da177e4SLinus Torvalds 			++buf;
4721da177e4SLinus Torvalds 		}
4731da177e4SLinus Torvalds 	}
474b39a7340SDenis Vlasenko 	/* zero or space padding */
475fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
476fef20d9cSFrederic Weisbecker 		char c = (spec.flags & ZEROPAD) ? '0' : ' ';
477fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
478f796937aSJeremy Fitzhardinge 			if (buf < end)
4791da177e4SLinus Torvalds 				*buf = c;
4801da177e4SLinus Torvalds 			++buf;
4811da177e4SLinus Torvalds 		}
4821da177e4SLinus Torvalds 	}
483b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
484fef20d9cSFrederic Weisbecker 	while (i <= --spec.precision) {
485f796937aSJeremy Fitzhardinge 		if (buf < end)
4861da177e4SLinus Torvalds 			*buf = '0';
4871da177e4SLinus Torvalds 		++buf;
4881da177e4SLinus Torvalds 	}
489b39a7340SDenis Vlasenko 	/* actual digits of result */
490b39a7340SDenis Vlasenko 	while (--i >= 0) {
491f796937aSJeremy Fitzhardinge 		if (buf < end)
4921da177e4SLinus Torvalds 			*buf = tmp[i];
4931da177e4SLinus Torvalds 		++buf;
4941da177e4SLinus Torvalds 	}
495b39a7340SDenis Vlasenko 	/* trailing space padding */
496fef20d9cSFrederic Weisbecker 	while (--spec.field_width >= 0) {
497f796937aSJeremy Fitzhardinge 		if (buf < end)
4981da177e4SLinus Torvalds 			*buf = ' ';
4991da177e4SLinus Torvalds 		++buf;
5001da177e4SLinus Torvalds 	}
5017b9186f5SAndré Goddard Rosa 
5021da177e4SLinus Torvalds 	return buf;
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
505cf3b429bSJoe Perches static noinline_for_stack
506cf3b429bSJoe Perches char *string(char *buf, char *end, const char *s, struct printf_spec spec)
5070f9bfa56SLinus Torvalds {
5080f9bfa56SLinus Torvalds 	int len, i;
5090f9bfa56SLinus Torvalds 
5100f9bfa56SLinus Torvalds 	if ((unsigned long)s < PAGE_SIZE)
5110f4f81dcSAndré Goddard Rosa 		s = "(null)";
5120f9bfa56SLinus Torvalds 
513fef20d9cSFrederic Weisbecker 	len = strnlen(s, spec.precision);
5140f9bfa56SLinus Torvalds 
515fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
516fef20d9cSFrederic Weisbecker 		while (len < spec.field_width--) {
5170f9bfa56SLinus Torvalds 			if (buf < end)
5180f9bfa56SLinus Torvalds 				*buf = ' ';
5190f9bfa56SLinus Torvalds 			++buf;
5200f9bfa56SLinus Torvalds 		}
5210f9bfa56SLinus Torvalds 	}
5220f9bfa56SLinus Torvalds 	for (i = 0; i < len; ++i) {
5230f9bfa56SLinus Torvalds 		if (buf < end)
5240f9bfa56SLinus Torvalds 			*buf = *s;
5250f9bfa56SLinus Torvalds 		++buf; ++s;
5260f9bfa56SLinus Torvalds 	}
527fef20d9cSFrederic Weisbecker 	while (len < spec.field_width--) {
5280f9bfa56SLinus Torvalds 		if (buf < end)
5290f9bfa56SLinus Torvalds 			*buf = ' ';
5300f9bfa56SLinus Torvalds 		++buf;
5310f9bfa56SLinus Torvalds 	}
5327b9186f5SAndré Goddard Rosa 
5330f9bfa56SLinus Torvalds 	return buf;
5340f9bfa56SLinus Torvalds }
5350f9bfa56SLinus Torvalds 
5364b6ccca7SAl Viro static void widen(char *buf, char *end, unsigned len, unsigned spaces)
5374b6ccca7SAl Viro {
5384b6ccca7SAl Viro 	size_t size;
5394b6ccca7SAl Viro 	if (buf >= end)	/* nowhere to put anything */
5404b6ccca7SAl Viro 		return;
5414b6ccca7SAl Viro 	size = end - buf;
5424b6ccca7SAl Viro 	if (size <= spaces) {
5434b6ccca7SAl Viro 		memset(buf, ' ', size);
5444b6ccca7SAl Viro 		return;
5454b6ccca7SAl Viro 	}
5464b6ccca7SAl Viro 	if (len) {
5474b6ccca7SAl Viro 		if (len > size - spaces)
5484b6ccca7SAl Viro 			len = size - spaces;
5494b6ccca7SAl Viro 		memmove(buf + spaces, buf, len);
5504b6ccca7SAl Viro 	}
5514b6ccca7SAl Viro 	memset(buf, ' ', spaces);
5524b6ccca7SAl Viro }
5534b6ccca7SAl Viro 
5544b6ccca7SAl Viro static noinline_for_stack
5554b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
5564b6ccca7SAl Viro 		  const char *fmt)
5574b6ccca7SAl Viro {
5584b6ccca7SAl Viro 	const char *array[4], *s;
5594b6ccca7SAl Viro 	const struct dentry *p;
5604b6ccca7SAl Viro 	int depth;
5614b6ccca7SAl Viro 	int i, n;
5624b6ccca7SAl Viro 
5634b6ccca7SAl Viro 	switch (fmt[1]) {
5644b6ccca7SAl Viro 		case '2': case '3': case '4':
5654b6ccca7SAl Viro 			depth = fmt[1] - '0';
5664b6ccca7SAl Viro 			break;
5674b6ccca7SAl Viro 		default:
5684b6ccca7SAl Viro 			depth = 1;
5694b6ccca7SAl Viro 	}
5704b6ccca7SAl Viro 
5714b6ccca7SAl Viro 	rcu_read_lock();
5724b6ccca7SAl Viro 	for (i = 0; i < depth; i++, d = p) {
5734b6ccca7SAl Viro 		p = ACCESS_ONCE(d->d_parent);
5744b6ccca7SAl Viro 		array[i] = ACCESS_ONCE(d->d_name.name);
5754b6ccca7SAl Viro 		if (p == d) {
5764b6ccca7SAl Viro 			if (i)
5774b6ccca7SAl Viro 				array[i] = "";
5784b6ccca7SAl Viro 			i++;
5794b6ccca7SAl Viro 			break;
5804b6ccca7SAl Viro 		}
5814b6ccca7SAl Viro 	}
5824b6ccca7SAl Viro 	s = array[--i];
5834b6ccca7SAl Viro 	for (n = 0; n != spec.precision; n++, buf++) {
5844b6ccca7SAl Viro 		char c = *s++;
5854b6ccca7SAl Viro 		if (!c) {
5864b6ccca7SAl Viro 			if (!i)
5874b6ccca7SAl Viro 				break;
5884b6ccca7SAl Viro 			c = '/';
5894b6ccca7SAl Viro 			s = array[--i];
5904b6ccca7SAl Viro 		}
5914b6ccca7SAl Viro 		if (buf < end)
5924b6ccca7SAl Viro 			*buf = c;
5934b6ccca7SAl Viro 	}
5944b6ccca7SAl Viro 	rcu_read_unlock();
5954b6ccca7SAl Viro 	if (n < spec.field_width) {
5964b6ccca7SAl Viro 		/* we want to pad the sucker */
5974b6ccca7SAl Viro 		unsigned spaces = spec.field_width - n;
5984b6ccca7SAl Viro 		if (!(spec.flags & LEFT)) {
5994b6ccca7SAl Viro 			widen(buf - n, end, n, spaces);
6004b6ccca7SAl Viro 			return buf + spaces;
6014b6ccca7SAl Viro 		}
6024b6ccca7SAl Viro 		while (spaces--) {
6034b6ccca7SAl Viro 			if (buf < end)
6044b6ccca7SAl Viro 				*buf = ' ';
6054b6ccca7SAl Viro 			++buf;
6064b6ccca7SAl Viro 		}
6074b6ccca7SAl Viro 	}
6084b6ccca7SAl Viro 	return buf;
6094b6ccca7SAl Viro }
6104b6ccca7SAl Viro 
611cf3b429bSJoe Perches static noinline_for_stack
612cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
613b0d33c2bSJoe Perches 		    struct printf_spec spec, const char *fmt)
6140fe1ef24SLinus Torvalds {
615b0d33c2bSJoe Perches 	unsigned long value;
6160fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
6170fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
618b0d33c2bSJoe Perches #endif
619b0d33c2bSJoe Perches 
620b0d33c2bSJoe Perches 	if (fmt[1] == 'R')
621b0d33c2bSJoe Perches 		ptr = __builtin_extract_return_addr(ptr);
622b0d33c2bSJoe Perches 	value = (unsigned long)ptr;
623b0d33c2bSJoe Perches 
624b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS
625b0d33c2bSJoe Perches 	if (*fmt == 'B')
6260f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
627b0d33c2bSJoe Perches 	else if (*fmt != 'f' && *fmt != 's')
6280fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
6290c8b946eSFrederic Weisbecker 	else
6304796dd20SStephen Boyd 		sprint_symbol_no_offset(sym, value);
6317b9186f5SAndré Goddard Rosa 
632fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
6330fe1ef24SLinus Torvalds #else
634fef20d9cSFrederic Weisbecker 	spec.field_width = 2 * sizeof(void *);
635fef20d9cSFrederic Weisbecker 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
636fef20d9cSFrederic Weisbecker 	spec.base = 16;
6377b9186f5SAndré Goddard Rosa 
638fef20d9cSFrederic Weisbecker 	return number(buf, end, value, spec);
6390fe1ef24SLinus Torvalds #endif
6400fe1ef24SLinus Torvalds }
6410fe1ef24SLinus Torvalds 
642cf3b429bSJoe Perches static noinline_for_stack
643cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
644fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
645332d2e78SLinus Torvalds {
646332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
64728405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
648332d2e78SLinus Torvalds #endif
649332d2e78SLinus Torvalds 
650332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
65128405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
652332d2e78SLinus Torvalds #endif
6534da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
654fef20d9cSFrederic Weisbecker 		.base = 16,
6554da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
656fef20d9cSFrederic Weisbecker 		.precision = -1,
657fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
658fef20d9cSFrederic Weisbecker 	};
6594da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
6604da0b66cSBjorn Helgaas 		.base = 16,
6614da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
6624da0b66cSBjorn Helgaas 		.precision = -1,
6634da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
6644da0b66cSBjorn Helgaas 	};
6650f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
6660f4050c7SBjorn Helgaas 		.base = 16,
6670f4050c7SBjorn Helgaas 		.field_width = 2,
6680f4050c7SBjorn Helgaas 		.precision = -1,
6690f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
6700f4050c7SBjorn Helgaas 	};
6714da0b66cSBjorn Helgaas 	static const struct printf_spec dec_spec = {
672c91d3376SBjorn Helgaas 		.base = 10,
673c91d3376SBjorn Helgaas 		.precision = -1,
674c91d3376SBjorn Helgaas 		.flags = 0,
675c91d3376SBjorn Helgaas 	};
6764da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
677fd95541eSBjorn Helgaas 		.field_width = -1,
678fd95541eSBjorn Helgaas 		.precision = 10,
679fd95541eSBjorn Helgaas 		.flags = LEFT,
680fd95541eSBjorn Helgaas 	};
6814da0b66cSBjorn Helgaas 	static const struct printf_spec flag_spec = {
682fd95541eSBjorn Helgaas 		.base = 16,
683fd95541eSBjorn Helgaas 		.precision = -1,
684fd95541eSBjorn Helgaas 		.flags = SPECIAL | SMALL,
685fd95541eSBjorn Helgaas 	};
686c7dabef8SBjorn Helgaas 
687c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
688c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
689c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
690c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
6919d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
692c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
693c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
694c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
695c7dabef8SBjorn Helgaas 
696332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
697c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
6984da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
699332d2e78SLinus Torvalds 
700332d2e78SLinus Torvalds 	*p++ = '[';
7014da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
702fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
7034da0b66cSBjorn Helgaas 		specp = &io_spec;
7044da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
705fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
7064da0b66cSBjorn Helgaas 		specp = &mem_spec;
7074da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
708fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
7094da0b66cSBjorn Helgaas 		specp = &dec_spec;
7104da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
711fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
7124da0b66cSBjorn Helgaas 		specp = &dec_spec;
7130f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
7140f4050c7SBjorn Helgaas 		p = string(p, pend, "bus ", str_spec);
7150f4050c7SBjorn Helgaas 		specp = &bus_spec;
7164da0b66cSBjorn Helgaas 	} else {
717c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
7184da0b66cSBjorn Helgaas 		specp = &mem_spec;
719c7dabef8SBjorn Helgaas 		decode = 0;
720fd95541eSBjorn Helgaas 	}
721d19cb803SBjorn Helgaas 	if (decode && res->flags & IORESOURCE_UNSET) {
722d19cb803SBjorn Helgaas 		p = string(p, pend, "size ", str_spec);
723d19cb803SBjorn Helgaas 		p = number(p, pend, resource_size(res), *specp);
724d19cb803SBjorn Helgaas 	} else {
7254da0b66cSBjorn Helgaas 		p = number(p, pend, res->start, *specp);
726c91d3376SBjorn Helgaas 		if (res->start != res->end) {
727332d2e78SLinus Torvalds 			*p++ = '-';
7284da0b66cSBjorn Helgaas 			p = number(p, pend, res->end, *specp);
729c91d3376SBjorn Helgaas 		}
730d19cb803SBjorn Helgaas 	}
731c7dabef8SBjorn Helgaas 	if (decode) {
732fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
733fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
734fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
735fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
7369d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
7379d7cca04SBjorn Helgaas 			p = string(p, pend, " window", str_spec);
738fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
739fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
740c7dabef8SBjorn Helgaas 	} else {
741fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
742c7dabef8SBjorn Helgaas 		p = number(p, pend, res->flags, flag_spec);
743fd95541eSBjorn Helgaas 	}
744332d2e78SLinus Torvalds 	*p++ = ']';
745c7dabef8SBjorn Helgaas 	*p = '\0';
746332d2e78SLinus Torvalds 
747fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
748332d2e78SLinus Torvalds }
749332d2e78SLinus Torvalds 
750cf3b429bSJoe Perches static noinline_for_stack
75131550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
75231550a16SAndy Shevchenko 		 const char *fmt)
75331550a16SAndy Shevchenko {
754360603a1SSteven Rostedt 	int i, len = 1;		/* if we pass '%ph[CDN]', field width remains
75531550a16SAndy Shevchenko 				   negative value, fallback to the default */
75631550a16SAndy Shevchenko 	char separator;
75731550a16SAndy Shevchenko 
75831550a16SAndy Shevchenko 	if (spec.field_width == 0)
75931550a16SAndy Shevchenko 		/* nothing to print */
76031550a16SAndy Shevchenko 		return buf;
76131550a16SAndy Shevchenko 
76231550a16SAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
76331550a16SAndy Shevchenko 		/* NULL pointer */
76431550a16SAndy Shevchenko 		return string(buf, end, NULL, spec);
76531550a16SAndy Shevchenko 
76631550a16SAndy Shevchenko 	switch (fmt[1]) {
76731550a16SAndy Shevchenko 	case 'C':
76831550a16SAndy Shevchenko 		separator = ':';
76931550a16SAndy Shevchenko 		break;
77031550a16SAndy Shevchenko 	case 'D':
77131550a16SAndy Shevchenko 		separator = '-';
77231550a16SAndy Shevchenko 		break;
77331550a16SAndy Shevchenko 	case 'N':
77431550a16SAndy Shevchenko 		separator = 0;
77531550a16SAndy Shevchenko 		break;
77631550a16SAndy Shevchenko 	default:
77731550a16SAndy Shevchenko 		separator = ' ';
77831550a16SAndy Shevchenko 		break;
77931550a16SAndy Shevchenko 	}
78031550a16SAndy Shevchenko 
78131550a16SAndy Shevchenko 	if (spec.field_width > 0)
78231550a16SAndy Shevchenko 		len = min_t(int, spec.field_width, 64);
78331550a16SAndy Shevchenko 
78431550a16SAndy Shevchenko 	for (i = 0; i < len && buf < end - 1; i++) {
78531550a16SAndy Shevchenko 		buf = hex_byte_pack(buf, addr[i]);
78631550a16SAndy Shevchenko 
78731550a16SAndy Shevchenko 		if (buf < end && separator && i != len - 1)
78831550a16SAndy Shevchenko 			*buf++ = separator;
78931550a16SAndy Shevchenko 	}
79031550a16SAndy Shevchenko 
79131550a16SAndy Shevchenko 	return buf;
79231550a16SAndy Shevchenko }
79331550a16SAndy Shevchenko 
79431550a16SAndy Shevchenko static noinline_for_stack
795cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
7968a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
797dd45c9cfSHarvey Harrison {
7988a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
799dd45c9cfSHarvey Harrison 	char *p = mac_addr;
800dd45c9cfSHarvey Harrison 	int i;
801bc7259a2SJoe Perches 	char separator;
80276597ff9SAndrei Emeltchenko 	bool reversed = false;
803bc7259a2SJoe Perches 
80476597ff9SAndrei Emeltchenko 	switch (fmt[1]) {
80576597ff9SAndrei Emeltchenko 	case 'F':
806bc7259a2SJoe Perches 		separator = '-';
80776597ff9SAndrei Emeltchenko 		break;
80876597ff9SAndrei Emeltchenko 
80976597ff9SAndrei Emeltchenko 	case 'R':
81076597ff9SAndrei Emeltchenko 		reversed = true;
81176597ff9SAndrei Emeltchenko 		/* fall through */
81276597ff9SAndrei Emeltchenko 
81376597ff9SAndrei Emeltchenko 	default:
814bc7259a2SJoe Perches 		separator = ':';
81576597ff9SAndrei Emeltchenko 		break;
816bc7259a2SJoe Perches 	}
817dd45c9cfSHarvey Harrison 
818dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
81976597ff9SAndrei Emeltchenko 		if (reversed)
82076597ff9SAndrei Emeltchenko 			p = hex_byte_pack(p, addr[5 - i]);
82176597ff9SAndrei Emeltchenko 		else
82255036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[i]);
82376597ff9SAndrei Emeltchenko 
8248a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
825bc7259a2SJoe Perches 			*p++ = separator;
826dd45c9cfSHarvey Harrison 	}
827dd45c9cfSHarvey Harrison 	*p = '\0';
828dd45c9cfSHarvey Harrison 
829fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
830dd45c9cfSHarvey Harrison }
831dd45c9cfSHarvey Harrison 
832cf3b429bSJoe Perches static noinline_for_stack
833cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
834689afa7dSHarvey Harrison {
835689afa7dSHarvey Harrison 	int i;
8360159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
8370159f24eSJoe Perches 	int index;
8380159f24eSJoe Perches 	int step;
839689afa7dSHarvey Harrison 
8400159f24eSJoe Perches 	switch (fmt[2]) {
8410159f24eSJoe Perches 	case 'h':
8420159f24eSJoe Perches #ifdef __BIG_ENDIAN
8430159f24eSJoe Perches 		index = 0;
8440159f24eSJoe Perches 		step = 1;
8450159f24eSJoe Perches #else
8460159f24eSJoe Perches 		index = 3;
8470159f24eSJoe Perches 		step = -1;
8480159f24eSJoe Perches #endif
8490159f24eSJoe Perches 		break;
8500159f24eSJoe Perches 	case 'l':
8510159f24eSJoe Perches 		index = 3;
8520159f24eSJoe Perches 		step = -1;
8530159f24eSJoe Perches 		break;
8540159f24eSJoe Perches 	case 'n':
8550159f24eSJoe Perches 	case 'b':
8560159f24eSJoe Perches 	default:
8570159f24eSJoe Perches 		index = 0;
8580159f24eSJoe Perches 		step = 1;
8590159f24eSJoe Perches 		break;
8600159f24eSJoe Perches 	}
8618a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
8628a27f7c9SJoe Perches 		char temp[3];	/* hold each IP quad in reverse order */
863133fd9f5SDenys Vlasenko 		int digits = put_dec_trunc8(temp, addr[index]) - temp;
8648a27f7c9SJoe Perches 		if (leading_zeros) {
8658a27f7c9SJoe Perches 			if (digits < 3)
8668a27f7c9SJoe Perches 				*p++ = '0';
8678a27f7c9SJoe Perches 			if (digits < 2)
8688a27f7c9SJoe Perches 				*p++ = '0';
8698a27f7c9SJoe Perches 		}
8708a27f7c9SJoe Perches 		/* reverse the digits in the quad */
8718a27f7c9SJoe Perches 		while (digits--)
8728a27f7c9SJoe Perches 			*p++ = temp[digits];
8738a27f7c9SJoe Perches 		if (i < 3)
8748a27f7c9SJoe Perches 			*p++ = '.';
8750159f24eSJoe Perches 		index += step;
8768a27f7c9SJoe Perches 	}
8778a27f7c9SJoe Perches 	*p = '\0';
8787b9186f5SAndré Goddard Rosa 
8798a27f7c9SJoe Perches 	return p;
8808a27f7c9SJoe Perches }
8818a27f7c9SJoe Perches 
882cf3b429bSJoe Perches static noinline_for_stack
883cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
8848a27f7c9SJoe Perches {
8857b9186f5SAndré Goddard Rosa 	int i, j, range;
8868a27f7c9SJoe Perches 	unsigned char zerolength[8];
8878a27f7c9SJoe Perches 	int longest = 1;
8888a27f7c9SJoe Perches 	int colonpos = -1;
8898a27f7c9SJoe Perches 	u16 word;
8907b9186f5SAndré Goddard Rosa 	u8 hi, lo;
8918a27f7c9SJoe Perches 	bool needcolon = false;
892eb78cd26SJoe Perches 	bool useIPv4;
893eb78cd26SJoe Perches 	struct in6_addr in6;
894eb78cd26SJoe Perches 
895eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
896eb78cd26SJoe Perches 
897eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
8988a27f7c9SJoe Perches 
8998a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
9008a27f7c9SJoe Perches 
9018a27f7c9SJoe Perches 	if (useIPv4)
9028a27f7c9SJoe Perches 		range = 6;
9038a27f7c9SJoe Perches 	else
9048a27f7c9SJoe Perches 		range = 8;
9058a27f7c9SJoe Perches 
9068a27f7c9SJoe Perches 	/* find position of longest 0 run */
9078a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
9088a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
909eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
9108a27f7c9SJoe Perches 				break;
9118a27f7c9SJoe Perches 			zerolength[i]++;
9128a27f7c9SJoe Perches 		}
9138a27f7c9SJoe Perches 	}
9148a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
9158a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
9168a27f7c9SJoe Perches 			longest = zerolength[i];
9178a27f7c9SJoe Perches 			colonpos = i;
9188a27f7c9SJoe Perches 		}
9198a27f7c9SJoe Perches 	}
92029cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
92129cf519eSJoe Perches 		colonpos = -1;
9228a27f7c9SJoe Perches 
9238a27f7c9SJoe Perches 	/* emit address */
9248a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
9258a27f7c9SJoe Perches 		if (i == colonpos) {
9268a27f7c9SJoe Perches 			if (needcolon || i == 0)
9278a27f7c9SJoe Perches 				*p++ = ':';
9288a27f7c9SJoe Perches 			*p++ = ':';
9298a27f7c9SJoe Perches 			needcolon = false;
9308a27f7c9SJoe Perches 			i += longest - 1;
9318a27f7c9SJoe Perches 			continue;
9328a27f7c9SJoe Perches 		}
9338a27f7c9SJoe Perches 		if (needcolon) {
9348a27f7c9SJoe Perches 			*p++ = ':';
9358a27f7c9SJoe Perches 			needcolon = false;
9368a27f7c9SJoe Perches 		}
9378a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
938eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
9398a27f7c9SJoe Perches 		hi = word >> 8;
9408a27f7c9SJoe Perches 		lo = word & 0xff;
9418a27f7c9SJoe Perches 		if (hi) {
9428a27f7c9SJoe Perches 			if (hi > 0x0f)
94355036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
9448a27f7c9SJoe Perches 			else
9458a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
94655036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
9478a27f7c9SJoe Perches 		}
948b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
94955036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
9508a27f7c9SJoe Perches 		else
9518a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
9528a27f7c9SJoe Perches 		needcolon = true;
9538a27f7c9SJoe Perches 	}
9548a27f7c9SJoe Perches 
9558a27f7c9SJoe Perches 	if (useIPv4) {
9568a27f7c9SJoe Perches 		if (needcolon)
9578a27f7c9SJoe Perches 			*p++ = ':';
9580159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
9598a27f7c9SJoe Perches 	}
9608a27f7c9SJoe Perches 	*p = '\0';
9617b9186f5SAndré Goddard Rosa 
9628a27f7c9SJoe Perches 	return p;
9638a27f7c9SJoe Perches }
9648a27f7c9SJoe Perches 
965cf3b429bSJoe Perches static noinline_for_stack
966cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
9678a27f7c9SJoe Perches {
9688a27f7c9SJoe Perches 	int i;
9697b9186f5SAndré Goddard Rosa 
970689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
97155036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
97255036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
9738a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
974689afa7dSHarvey Harrison 			*p++ = ':';
975689afa7dSHarvey Harrison 	}
976689afa7dSHarvey Harrison 	*p = '\0';
9777b9186f5SAndré Goddard Rosa 
9788a27f7c9SJoe Perches 	return p;
9798a27f7c9SJoe Perches }
9808a27f7c9SJoe Perches 
981cf3b429bSJoe Perches static noinline_for_stack
982cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
9838a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
9848a27f7c9SJoe Perches {
9858a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
9868a27f7c9SJoe Perches 
9878a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
988eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
9898a27f7c9SJoe Perches 	else
990eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
991689afa7dSHarvey Harrison 
992fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
993689afa7dSHarvey Harrison }
994689afa7dSHarvey Harrison 
995cf3b429bSJoe Perches static noinline_for_stack
996cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
9978a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
9984aa99606SHarvey Harrison {
9998a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
10004aa99606SHarvey Harrison 
10010159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
10024aa99606SHarvey Harrison 
1003fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
10044aa99606SHarvey Harrison }
10054aa99606SHarvey Harrison 
1006cf3b429bSJoe Perches static noinline_for_stack
100710679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
100810679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
100910679643SDaniel Borkmann {
101010679643SDaniel Borkmann 	bool have_p = false, have_s = false, have_f = false, have_c = false;
101110679643SDaniel Borkmann 	char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
101210679643SDaniel Borkmann 		      sizeof(":12345") + sizeof("/123456789") +
101310679643SDaniel Borkmann 		      sizeof("%1234567890")];
101410679643SDaniel Borkmann 	char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
101510679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin6_addr;
101610679643SDaniel Borkmann 	char fmt6[2] = { fmt[0], '6' };
101710679643SDaniel Borkmann 	u8 off = 0;
101810679643SDaniel Borkmann 
101910679643SDaniel Borkmann 	fmt++;
102010679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
102110679643SDaniel Borkmann 		switch (*fmt) {
102210679643SDaniel Borkmann 		case 'p':
102310679643SDaniel Borkmann 			have_p = true;
102410679643SDaniel Borkmann 			break;
102510679643SDaniel Borkmann 		case 'f':
102610679643SDaniel Borkmann 			have_f = true;
102710679643SDaniel Borkmann 			break;
102810679643SDaniel Borkmann 		case 's':
102910679643SDaniel Borkmann 			have_s = true;
103010679643SDaniel Borkmann 			break;
103110679643SDaniel Borkmann 		case 'c':
103210679643SDaniel Borkmann 			have_c = true;
103310679643SDaniel Borkmann 			break;
103410679643SDaniel Borkmann 		}
103510679643SDaniel Borkmann 	}
103610679643SDaniel Borkmann 
103710679643SDaniel Borkmann 	if (have_p || have_s || have_f) {
103810679643SDaniel Borkmann 		*p = '[';
103910679643SDaniel Borkmann 		off = 1;
104010679643SDaniel Borkmann 	}
104110679643SDaniel Borkmann 
104210679643SDaniel Borkmann 	if (fmt6[0] == 'I' && have_c)
104310679643SDaniel Borkmann 		p = ip6_compressed_string(ip6_addr + off, addr);
104410679643SDaniel Borkmann 	else
104510679643SDaniel Borkmann 		p = ip6_string(ip6_addr + off, addr, fmt6);
104610679643SDaniel Borkmann 
104710679643SDaniel Borkmann 	if (have_p || have_s || have_f)
104810679643SDaniel Borkmann 		*p++ = ']';
104910679643SDaniel Borkmann 
105010679643SDaniel Borkmann 	if (have_p) {
105110679643SDaniel Borkmann 		*p++ = ':';
105210679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin6_port), spec);
105310679643SDaniel Borkmann 	}
105410679643SDaniel Borkmann 	if (have_f) {
105510679643SDaniel Borkmann 		*p++ = '/';
105610679643SDaniel Borkmann 		p = number(p, pend, ntohl(sa->sin6_flowinfo &
105710679643SDaniel Borkmann 					  IPV6_FLOWINFO_MASK), spec);
105810679643SDaniel Borkmann 	}
105910679643SDaniel Borkmann 	if (have_s) {
106010679643SDaniel Borkmann 		*p++ = '%';
106110679643SDaniel Borkmann 		p = number(p, pend, sa->sin6_scope_id, spec);
106210679643SDaniel Borkmann 	}
106310679643SDaniel Borkmann 	*p = '\0';
106410679643SDaniel Borkmann 
106510679643SDaniel Borkmann 	return string(buf, end, ip6_addr, spec);
106610679643SDaniel Borkmann }
106710679643SDaniel Borkmann 
106810679643SDaniel Borkmann static noinline_for_stack
106910679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
107010679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
107110679643SDaniel Borkmann {
107210679643SDaniel Borkmann 	bool have_p = false;
107310679643SDaniel Borkmann 	char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
107410679643SDaniel Borkmann 	char *pend = ip4_addr + sizeof(ip4_addr);
107510679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
107610679643SDaniel Borkmann 	char fmt4[3] = { fmt[0], '4', 0 };
107710679643SDaniel Borkmann 
107810679643SDaniel Borkmann 	fmt++;
107910679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
108010679643SDaniel Borkmann 		switch (*fmt) {
108110679643SDaniel Borkmann 		case 'p':
108210679643SDaniel Borkmann 			have_p = true;
108310679643SDaniel Borkmann 			break;
108410679643SDaniel Borkmann 		case 'h':
108510679643SDaniel Borkmann 		case 'l':
108610679643SDaniel Borkmann 		case 'n':
108710679643SDaniel Borkmann 		case 'b':
108810679643SDaniel Borkmann 			fmt4[2] = *fmt;
108910679643SDaniel Borkmann 			break;
109010679643SDaniel Borkmann 		}
109110679643SDaniel Borkmann 	}
109210679643SDaniel Borkmann 
109310679643SDaniel Borkmann 	p = ip4_string(ip4_addr, addr, fmt4);
109410679643SDaniel Borkmann 	if (have_p) {
109510679643SDaniel Borkmann 		*p++ = ':';
109610679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin_port), spec);
109710679643SDaniel Borkmann 	}
109810679643SDaniel Borkmann 	*p = '\0';
109910679643SDaniel Borkmann 
110010679643SDaniel Borkmann 	return string(buf, end, ip4_addr, spec);
110110679643SDaniel Borkmann }
110210679643SDaniel Borkmann 
110310679643SDaniel Borkmann static noinline_for_stack
1104cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
11059ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
11069ac6e44eSJoe Perches {
11079ac6e44eSJoe Perches 	char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
11089ac6e44eSJoe Perches 	char *p = uuid;
11099ac6e44eSJoe Perches 	int i;
11109ac6e44eSJoe Perches 	static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
11119ac6e44eSJoe Perches 	static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
11129ac6e44eSJoe Perches 	const u8 *index = be;
11139ac6e44eSJoe Perches 	bool uc = false;
11149ac6e44eSJoe Perches 
11159ac6e44eSJoe Perches 	switch (*(++fmt)) {
11169ac6e44eSJoe Perches 	case 'L':
11179ac6e44eSJoe Perches 		uc = true;		/* fall-through */
11189ac6e44eSJoe Perches 	case 'l':
11199ac6e44eSJoe Perches 		index = le;
11209ac6e44eSJoe Perches 		break;
11219ac6e44eSJoe Perches 	case 'B':
11229ac6e44eSJoe Perches 		uc = true;
11239ac6e44eSJoe Perches 		break;
11249ac6e44eSJoe Perches 	}
11259ac6e44eSJoe Perches 
11269ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
112755036ba7SAndy Shevchenko 		p = hex_byte_pack(p, addr[index[i]]);
11289ac6e44eSJoe Perches 		switch (i) {
11299ac6e44eSJoe Perches 		case 3:
11309ac6e44eSJoe Perches 		case 5:
11319ac6e44eSJoe Perches 		case 7:
11329ac6e44eSJoe Perches 		case 9:
11339ac6e44eSJoe Perches 			*p++ = '-';
11349ac6e44eSJoe Perches 			break;
11359ac6e44eSJoe Perches 		}
11369ac6e44eSJoe Perches 	}
11379ac6e44eSJoe Perches 
11389ac6e44eSJoe Perches 	*p = 0;
11399ac6e44eSJoe Perches 
11409ac6e44eSJoe Perches 	if (uc) {
11419ac6e44eSJoe Perches 		p = uuid;
11429ac6e44eSJoe Perches 		do {
11439ac6e44eSJoe Perches 			*p = toupper(*p);
11449ac6e44eSJoe Perches 		} while (*(++p));
11459ac6e44eSJoe Perches 	}
11469ac6e44eSJoe Perches 
11479ac6e44eSJoe Perches 	return string(buf, end, uuid, spec);
11489ac6e44eSJoe Perches }
11499ac6e44eSJoe Perches 
1150c8f44affSMichał Mirosław static
1151c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1152c8f44affSMichał Mirosław 		      struct printf_spec spec)
1153c8f44affSMichał Mirosław {
1154c8f44affSMichał Mirosław 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
1155c8f44affSMichał Mirosław 	if (spec.field_width == -1)
1156c8f44affSMichał Mirosław 		spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1157c8f44affSMichał Mirosław 	spec.base = 16;
1158c8f44affSMichał Mirosław 
1159c8f44affSMichał Mirosław 	return number(buf, end, *(const netdev_features_t *)addr, spec);
1160c8f44affSMichał Mirosław }
1161c8f44affSMichał Mirosław 
1162aaf07621SJoe Perches static noinline_for_stack
1163aaf07621SJoe Perches char *address_val(char *buf, char *end, const void *addr,
1164aaf07621SJoe Perches 		  struct printf_spec spec, const char *fmt)
1165aaf07621SJoe Perches {
1166aaf07621SJoe Perches 	unsigned long long num;
1167aaf07621SJoe Perches 
1168aaf07621SJoe Perches 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
1169aaf07621SJoe Perches 	spec.base = 16;
1170aaf07621SJoe Perches 
1171aaf07621SJoe Perches 	switch (fmt[1]) {
1172aaf07621SJoe Perches 	case 'd':
1173aaf07621SJoe Perches 		num = *(const dma_addr_t *)addr;
1174aaf07621SJoe Perches 		spec.field_width = sizeof(dma_addr_t) * 2 + 2;
1175aaf07621SJoe Perches 		break;
1176aaf07621SJoe Perches 	case 'p':
1177aaf07621SJoe Perches 	default:
1178aaf07621SJoe Perches 		num = *(const phys_addr_t *)addr;
1179aaf07621SJoe Perches 		spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1180aaf07621SJoe Perches 		break;
1181aaf07621SJoe Perches 	}
1182aaf07621SJoe Perches 
1183aaf07621SJoe Perches 	return number(buf, end, num, spec);
1184aaf07621SJoe Perches }
1185aaf07621SJoe Perches 
1186411f05f1SIngo Molnar int kptr_restrict __read_mostly;
1187455cd5abSDan Rosenberg 
11884d8a743cSLinus Torvalds /*
11894d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
11904d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
11914d8a743cSLinus Torvalds  * specifiers.
11924d8a743cSLinus Torvalds  *
1193332d2e78SLinus Torvalds  * Right now we handle:
11940fe1ef24SLinus Torvalds  *
11950c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
11960c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
11970efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
11980efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
1199b0d33c2bSJoe Perches  * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
12000f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
1201c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1202c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1203dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
1204dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
12058a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1206bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1207c8e00060SJoe Perches  *       with a dash-separated hex notation
12087c59154eSAndy Shevchenko  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
12098a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
12108a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
12118a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
121210679643SDaniel Borkmann  *       [S][pfs]
121310679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
121410679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
12158a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
12168a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
12178a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
121810679643SDaniel Borkmann  *       [S][pfs]
121910679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
122010679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
122110679643SDaniel Borkmann  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
122210679643SDaniel Borkmann  * - 'I[6S]c' for IPv6 addresses printed as specified by
122329cf519eSJoe Perches  *       http://tools.ietf.org/html/rfc5952
12249ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
12259ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
12269ac6e44eSJoe Perches  *       Options for %pU are:
12279ac6e44eSJoe Perches  *         b big endian lower case hex (default)
12289ac6e44eSJoe Perches  *         B big endian UPPER case hex
12299ac6e44eSJoe Perches  *         l little endian lower case hex
12309ac6e44eSJoe Perches  *         L little endian UPPER case hex
12319ac6e44eSJoe Perches  *           big endian output byte order is:
12329ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
12339ac6e44eSJoe Perches  *           little endian output byte order is:
12349ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
12357db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
12367db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
12377db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
12387db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
12397db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
1240455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
1241c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
124231550a16SAndy Shevchenko  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
124331550a16SAndy Shevchenko  *            a certain separator (' ' by default):
124431550a16SAndy Shevchenko  *              C colon
124531550a16SAndy Shevchenko  *              D dash
124631550a16SAndy Shevchenko  *              N no separator
124731550a16SAndy Shevchenko  *            The maximum supported length is 64 bytes of the input. Consider
124831550a16SAndy Shevchenko  *            to use print_hex_dump() for the larger input.
1249aaf07621SJoe Perches  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1250aaf07621SJoe Perches  *           (default assumed to be phys_addr_t, passed by reference)
1251c0d92a57SOlof Johansson  * - 'd[234]' For a dentry name (optionally 2-4 last components)
1252c0d92a57SOlof Johansson  * - 'D[234]' Same as 'd' but for a struct file
12539ac6e44eSJoe Perches  *
1254332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1255332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
1256332d2e78SLinus Torvalds  * pointer to the real address.
12574d8a743cSLinus Torvalds  */
1258cf3b429bSJoe Perches static noinline_for_stack
1259cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1260fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
126178a8bf69SLinus Torvalds {
1262725fe002SGrant Likely 	int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1263725fe002SGrant Likely 
12649f36e2c4SKees Cook 	if (!ptr && *fmt != 'K') {
12655e057981SJoe Perches 		/*
12665e057981SJoe Perches 		 * Print (null) with the same width as a pointer so it makes
12675e057981SJoe Perches 		 * tabular output look nice.
12685e057981SJoe Perches 		 */
12695e057981SJoe Perches 		if (spec.field_width == -1)
1270725fe002SGrant Likely 			spec.field_width = default_width;
1271fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
12725e057981SJoe Perches 	}
1273d97106abSLinus Torvalds 
12740fe1ef24SLinus Torvalds 	switch (*fmt) {
12750fe1ef24SLinus Torvalds 	case 'F':
12760c8b946eSFrederic Weisbecker 	case 'f':
12770fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
12780fe1ef24SLinus Torvalds 		/* Fallthrough */
12790fe1ef24SLinus Torvalds 	case 'S':
12809ac6e44eSJoe Perches 	case 's':
12810f77a8d3SNamhyung Kim 	case 'B':
1282b0d33c2bSJoe Perches 		return symbol_string(buf, end, ptr, spec, fmt);
1283332d2e78SLinus Torvalds 	case 'R':
1284c7dabef8SBjorn Helgaas 	case 'r':
1285fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
128631550a16SAndy Shevchenko 	case 'h':
128731550a16SAndy Shevchenko 		return hex_string(buf, end, ptr, spec, fmt);
12888a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
12898a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
129076597ff9SAndrei Emeltchenko 					/* [mM]F (FDDI) */
129176597ff9SAndrei Emeltchenko 					/* [mM]R (Reverse order; Bluetooth) */
12928a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
12938a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
12948a27f7c9SJoe Perches 					 * 4:	1.2.3.4
12958a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
12968a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
12978a27f7c9SJoe Perches 					 */
12988a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
12998a27f7c9SJoe Perches 					 * 4:	001.002.003.004
13008a27f7c9SJoe Perches 					 * 6:   000102...0f
13018a27f7c9SJoe Perches 					 */
13028a27f7c9SJoe Perches 		switch (fmt[1]) {
13038a27f7c9SJoe Perches 		case '6':
13048a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
13058a27f7c9SJoe Perches 		case '4':
13068a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
130710679643SDaniel Borkmann 		case 'S': {
130810679643SDaniel Borkmann 			const union {
130910679643SDaniel Borkmann 				struct sockaddr		raw;
131010679643SDaniel Borkmann 				struct sockaddr_in	v4;
131110679643SDaniel Borkmann 				struct sockaddr_in6	v6;
131210679643SDaniel Borkmann 			} *sa = ptr;
131310679643SDaniel Borkmann 
131410679643SDaniel Borkmann 			switch (sa->raw.sa_family) {
131510679643SDaniel Borkmann 			case AF_INET:
131610679643SDaniel Borkmann 				return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
131710679643SDaniel Borkmann 			case AF_INET6:
131810679643SDaniel Borkmann 				return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
131910679643SDaniel Borkmann 			default:
132010679643SDaniel Borkmann 				return string(buf, end, "(invalid address)", spec);
132110679643SDaniel Borkmann 			}}
13228a27f7c9SJoe Perches 		}
13234aa99606SHarvey Harrison 		break;
13249ac6e44eSJoe Perches 	case 'U':
13259ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
13267db6f5fbSJoe Perches 	case 'V':
13275756b76eSJan Beulich 		{
13285756b76eSJan Beulich 			va_list va;
13295756b76eSJan Beulich 
13305756b76eSJan Beulich 			va_copy(va, *((struct va_format *)ptr)->va);
13315756b76eSJan Beulich 			buf += vsnprintf(buf, end > buf ? end - buf : 0,
13325756b76eSJan Beulich 					 ((struct va_format *)ptr)->fmt, va);
13335756b76eSJan Beulich 			va_end(va);
13345756b76eSJan Beulich 			return buf;
13355756b76eSJan Beulich 		}
1336455cd5abSDan Rosenberg 	case 'K':
1337455cd5abSDan Rosenberg 		/*
1338455cd5abSDan Rosenberg 		 * %pK cannot be used in IRQ context because its test
1339455cd5abSDan Rosenberg 		 * for CAP_SYSLOG would be meaningless.
1340455cd5abSDan Rosenberg 		 */
13413715c530SDan Rosenberg 		if (kptr_restrict && (in_irq() || in_serving_softirq() ||
13423715c530SDan Rosenberg 				      in_nmi())) {
1343455cd5abSDan Rosenberg 			if (spec.field_width == -1)
1344725fe002SGrant Likely 				spec.field_width = default_width;
1345455cd5abSDan Rosenberg 			return string(buf, end, "pK-error", spec);
1346455cd5abSDan Rosenberg 		}
1347312b4e22SRyan Mallon 
1348312b4e22SRyan Mallon 		switch (kptr_restrict) {
1349312b4e22SRyan Mallon 		case 0:
1350312b4e22SRyan Mallon 			/* Always print %pK values */
1351312b4e22SRyan Mallon 			break;
1352312b4e22SRyan Mallon 		case 1: {
1353312b4e22SRyan Mallon 			/*
1354312b4e22SRyan Mallon 			 * Only print the real pointer value if the current
1355312b4e22SRyan Mallon 			 * process has CAP_SYSLOG and is running with the
1356312b4e22SRyan Mallon 			 * same credentials it started with. This is because
1357312b4e22SRyan Mallon 			 * access to files is checked at open() time, but %pK
1358312b4e22SRyan Mallon 			 * checks permission at read() time. We don't want to
1359312b4e22SRyan Mallon 			 * leak pointer values if a binary opens a file using
1360312b4e22SRyan Mallon 			 * %pK and then elevates privileges before reading it.
1361312b4e22SRyan Mallon 			 */
1362312b4e22SRyan Mallon 			const struct cred *cred = current_cred();
1363312b4e22SRyan Mallon 
1364312b4e22SRyan Mallon 			if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1365312b4e22SRyan Mallon 			    !uid_eq(cred->euid, cred->uid) ||
1366312b4e22SRyan Mallon 			    !gid_eq(cred->egid, cred->gid))
136726297607SJoe Perches 				ptr = NULL;
136826297607SJoe Perches 			break;
1369312b4e22SRyan Mallon 		}
1370312b4e22SRyan Mallon 		case 2:
1371312b4e22SRyan Mallon 		default:
1372312b4e22SRyan Mallon 			/* Always print 0's for %pK */
1373312b4e22SRyan Mallon 			ptr = NULL;
1374312b4e22SRyan Mallon 			break;
1375312b4e22SRyan Mallon 		}
1376312b4e22SRyan Mallon 		break;
1377312b4e22SRyan Mallon 
1378c8f44affSMichał Mirosław 	case 'N':
1379c8f44affSMichał Mirosław 		switch (fmt[1]) {
1380c8f44affSMichał Mirosław 		case 'F':
1381c8f44affSMichał Mirosław 			return netdev_feature_string(buf, end, ptr, spec);
1382c8f44affSMichał Mirosław 		}
1383c8f44affSMichał Mirosław 		break;
13847d799210SStepan Moskovchenko 	case 'a':
1385aaf07621SJoe Perches 		return address_val(buf, end, ptr, spec, fmt);
13864b6ccca7SAl Viro 	case 'd':
13874b6ccca7SAl Viro 		return dentry_name(buf, end, ptr, spec, fmt);
13884b6ccca7SAl Viro 	case 'D':
13894b6ccca7SAl Viro 		return dentry_name(buf, end,
13904b6ccca7SAl Viro 				   ((const struct file *)ptr)->f_path.dentry,
13914b6ccca7SAl Viro 				   spec, fmt);
13920fe1ef24SLinus Torvalds 	}
1393fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
1394fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
1395725fe002SGrant Likely 		spec.field_width = default_width;
1396fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
139778a8bf69SLinus Torvalds 	}
1398fef20d9cSFrederic Weisbecker 	spec.base = 16;
1399fef20d9cSFrederic Weisbecker 
1400fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
1401fef20d9cSFrederic Weisbecker }
1402fef20d9cSFrederic Weisbecker 
1403fef20d9cSFrederic Weisbecker /*
1404fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
1405fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
1406fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
1407fef20d9cSFrederic Weisbecker  * to go on the next call).
1408fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
1409fef20d9cSFrederic Weisbecker  *
1410fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
1411fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
1412fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
1413fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
1414fef20d9cSFrederic Weisbecker  *
1415fef20d9cSFrederic Weisbecker  * @fmt: the format string
1416fef20d9cSFrederic Weisbecker  * @type of the token returned
1417fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
1418fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
1419fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
1420fef20d9cSFrederic Weisbecker  * @precision: precision of a number
1421fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
1422fef20d9cSFrederic Weisbecker  */
1423cf3b429bSJoe Perches static noinline_for_stack
1424cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
1425fef20d9cSFrederic Weisbecker {
1426fef20d9cSFrederic Weisbecker 	const char *start = fmt;
1427fef20d9cSFrederic Weisbecker 
1428fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
1429ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
1430fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
1431fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
1432fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
1433fef20d9cSFrederic Weisbecker 		}
1434fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1435fef20d9cSFrederic Weisbecker 		goto precision;
1436fef20d9cSFrederic Weisbecker 	}
1437fef20d9cSFrederic Weisbecker 
1438fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
1439fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
1440fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
1441fef20d9cSFrederic Weisbecker 			spec->precision = 0;
1442fef20d9cSFrederic Weisbecker 
1443fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1444fef20d9cSFrederic Weisbecker 		goto qualifier;
1445fef20d9cSFrederic Weisbecker 	}
1446fef20d9cSFrederic Weisbecker 
1447fef20d9cSFrederic Weisbecker 	/* By default */
1448fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
1449fef20d9cSFrederic Weisbecker 
1450fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
1451fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
1452fef20d9cSFrederic Weisbecker 			break;
1453fef20d9cSFrederic Weisbecker 	}
1454fef20d9cSFrederic Weisbecker 
1455fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
1456fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
1457fef20d9cSFrederic Weisbecker 		return fmt - start;
1458fef20d9cSFrederic Weisbecker 
1459fef20d9cSFrederic Weisbecker 	/* Process flags */
1460fef20d9cSFrederic Weisbecker 	spec->flags = 0;
1461fef20d9cSFrederic Weisbecker 
1462fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
1463fef20d9cSFrederic Weisbecker 		bool found = true;
1464fef20d9cSFrederic Weisbecker 
1465fef20d9cSFrederic Weisbecker 		++fmt;
1466fef20d9cSFrederic Weisbecker 
1467fef20d9cSFrederic Weisbecker 		switch (*fmt) {
1468fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
1469fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
1470fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
1471fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
1472fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
1473fef20d9cSFrederic Weisbecker 		default:  found = false;
1474fef20d9cSFrederic Weisbecker 		}
1475fef20d9cSFrederic Weisbecker 
1476fef20d9cSFrederic Weisbecker 		if (!found)
1477fef20d9cSFrederic Weisbecker 			break;
1478fef20d9cSFrederic Weisbecker 	}
1479fef20d9cSFrederic Weisbecker 
1480fef20d9cSFrederic Weisbecker 	/* get field width */
1481fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
1482fef20d9cSFrederic Weisbecker 
1483fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
1484fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
1485fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
1486fef20d9cSFrederic Weisbecker 		/* it's the next argument */
1487ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
1488fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1489fef20d9cSFrederic Weisbecker 	}
1490fef20d9cSFrederic Weisbecker 
1491fef20d9cSFrederic Weisbecker precision:
1492fef20d9cSFrederic Weisbecker 	/* get the precision */
1493fef20d9cSFrederic Weisbecker 	spec->precision = -1;
1494fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
1495fef20d9cSFrederic Weisbecker 		++fmt;
1496fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
1497fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
1498fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
1499fef20d9cSFrederic Weisbecker 				spec->precision = 0;
1500fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
1501fef20d9cSFrederic Weisbecker 			/* it's the next argument */
1502adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
1503fef20d9cSFrederic Weisbecker 			return ++fmt - start;
1504fef20d9cSFrederic Weisbecker 		}
1505fef20d9cSFrederic Weisbecker 	}
1506fef20d9cSFrederic Weisbecker 
1507fef20d9cSFrederic Weisbecker qualifier:
1508fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
1509fef20d9cSFrederic Weisbecker 	spec->qualifier = -1;
151075fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
151175fb8f26SAndy Shevchenko 	    _tolower(*fmt) == 'z' || *fmt == 't') {
1512a4e94ef0SZhaolei 		spec->qualifier = *fmt++;
1513a4e94ef0SZhaolei 		if (unlikely(spec->qualifier == *fmt)) {
1514a4e94ef0SZhaolei 			if (spec->qualifier == 'l') {
1515fef20d9cSFrederic Weisbecker 				spec->qualifier = 'L';
1516fef20d9cSFrederic Weisbecker 				++fmt;
1517a4e94ef0SZhaolei 			} else if (spec->qualifier == 'h') {
1518a4e94ef0SZhaolei 				spec->qualifier = 'H';
1519a4e94ef0SZhaolei 				++fmt;
1520a4e94ef0SZhaolei 			}
1521fef20d9cSFrederic Weisbecker 		}
1522fef20d9cSFrederic Weisbecker 	}
1523fef20d9cSFrederic Weisbecker 
1524fef20d9cSFrederic Weisbecker 	/* default base */
1525fef20d9cSFrederic Weisbecker 	spec->base = 10;
1526fef20d9cSFrederic Weisbecker 	switch (*fmt) {
1527fef20d9cSFrederic Weisbecker 	case 'c':
1528fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
1529fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1530fef20d9cSFrederic Weisbecker 
1531fef20d9cSFrederic Weisbecker 	case 's':
1532fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
1533fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1534fef20d9cSFrederic Weisbecker 
1535fef20d9cSFrederic Weisbecker 	case 'p':
1536fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
1537fef20d9cSFrederic Weisbecker 		return fmt - start;
1538fef20d9cSFrederic Weisbecker 		/* skip alnum */
1539fef20d9cSFrederic Weisbecker 
1540fef20d9cSFrederic Weisbecker 	case '%':
1541fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1542fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1543fef20d9cSFrederic Weisbecker 
1544fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1545fef20d9cSFrederic Weisbecker 	case 'o':
1546fef20d9cSFrederic Weisbecker 		spec->base = 8;
1547fef20d9cSFrederic Weisbecker 		break;
1548fef20d9cSFrederic Weisbecker 
1549fef20d9cSFrederic Weisbecker 	case 'x':
1550fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1551fef20d9cSFrederic Weisbecker 
1552fef20d9cSFrederic Weisbecker 	case 'X':
1553fef20d9cSFrederic Weisbecker 		spec->base = 16;
1554fef20d9cSFrederic Weisbecker 		break;
1555fef20d9cSFrederic Weisbecker 
1556fef20d9cSFrederic Weisbecker 	case 'd':
1557fef20d9cSFrederic Weisbecker 	case 'i':
155839e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1559fef20d9cSFrederic Weisbecker 	case 'u':
1560fef20d9cSFrederic Weisbecker 		break;
1561fef20d9cSFrederic Weisbecker 
1562708d96fdSRyan Mallon 	case 'n':
1563708d96fdSRyan Mallon 		/*
1564708d96fdSRyan Mallon 		 * Since %n poses a greater security risk than utility, treat
1565708d96fdSRyan Mallon 		 * it as an invalid format specifier. Warn about its use so
1566708d96fdSRyan Mallon 		 * that new instances don't get added.
1567708d96fdSRyan Mallon 		 */
1568708d96fdSRyan Mallon 		WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt);
1569708d96fdSRyan Mallon 		/* Fall-through */
1570708d96fdSRyan Mallon 
1571fef20d9cSFrederic Weisbecker 	default:
1572fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1573fef20d9cSFrederic Weisbecker 		return fmt - start;
1574fef20d9cSFrederic Weisbecker 	}
1575fef20d9cSFrederic Weisbecker 
1576fef20d9cSFrederic Weisbecker 	if (spec->qualifier == 'L')
1577fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1578fef20d9cSFrederic Weisbecker 	else if (spec->qualifier == 'l') {
157939e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1580fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_LONG;
1581fef20d9cSFrederic Weisbecker 		else
1582fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_ULONG;
158375fb8f26SAndy Shevchenko 	} else if (_tolower(spec->qualifier) == 'z') {
1584fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1585fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 't') {
1586fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1587a4e94ef0SZhaolei 	} else if (spec->qualifier == 'H') {
1588a4e94ef0SZhaolei 		if (spec->flags & SIGN)
1589a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_BYTE;
1590a4e94ef0SZhaolei 		else
1591a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_UBYTE;
1592fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'h') {
159339e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1594fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_SHORT;
1595fef20d9cSFrederic Weisbecker 		else
1596fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_USHORT;
1597fef20d9cSFrederic Weisbecker 	} else {
159839e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1599fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_INT;
1600fef20d9cSFrederic Weisbecker 		else
1601fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_UINT;
1602fef20d9cSFrederic Weisbecker 	}
1603fef20d9cSFrederic Weisbecker 
1604fef20d9cSFrederic Weisbecker 	return ++fmt - start;
160578a8bf69SLinus Torvalds }
160678a8bf69SLinus Torvalds 
16071da177e4SLinus Torvalds /**
16081da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
16091da177e4SLinus Torvalds  * @buf: The buffer to place the result into
16101da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
16111da177e4SLinus Torvalds  * @fmt: The format string to use
16121da177e4SLinus Torvalds  * @args: Arguments for the format string
16131da177e4SLinus Torvalds  *
161420036fdcSAndi Kleen  * This function follows C99 vsnprintf, but has some extensions:
161591adcd2cSSteven Rostedt  * %pS output the name of a text symbol with offset
161691adcd2cSSteven Rostedt  * %ps output the name of a text symbol without offset
16170c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
16180c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
16190f77a8d3SNamhyung Kim  * %pB output the name of a backtrace symbol with its offset
16208a79503aSUwe Kleine-König  * %pR output the address range in a struct resource with decoded flags
16218a79503aSUwe Kleine-König  * %pr output the address range in a struct resource with raw flags
16228a79503aSUwe Kleine-König  * %pM output a 6-byte MAC address with colons
16237c59154eSAndy Shevchenko  * %pMR output a 6-byte MAC address with colons in reversed order
16247c59154eSAndy Shevchenko  * %pMF output a 6-byte MAC address with dashes
16258a79503aSUwe Kleine-König  * %pm output a 6-byte MAC address without colons
16267c59154eSAndy Shevchenko  * %pmR output a 6-byte MAC address without colons in reversed order
16278a79503aSUwe Kleine-König  * %pI4 print an IPv4 address without leading zeros
16288a79503aSUwe Kleine-König  * %pi4 print an IPv4 address with leading zeros
16298a79503aSUwe Kleine-König  * %pI6 print an IPv6 address with colons
16308a79503aSUwe Kleine-König  * %pi6 print an IPv6 address without colons
1631f996f208SJan Engelhardt  * %pI6c print an IPv6 address as specified by RFC 5952
163210679643SDaniel Borkmann  * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
163310679643SDaniel Borkmann  * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
16348a79503aSUwe Kleine-König  * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
16358a79503aSUwe Kleine-König  *   case.
163631550a16SAndy Shevchenko  * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
163731550a16SAndy Shevchenko  *           bytes of the input)
16380efb4d20SSteven Rostedt  * %n is ignored
163920036fdcSAndi Kleen  *
164080f548e0SAndrew Morton  * ** Please update Documentation/printk-formats.txt when making changes **
164180f548e0SAndrew Morton  *
16421da177e4SLinus Torvalds  * The return value is the number of characters which would
16431da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
16441da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
16451da177e4SLinus Torvalds  * number of characters written into @buf as return value
164672fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
16471da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
16481da177e4SLinus Torvalds  * string is truncated.
16491da177e4SLinus Torvalds  *
1650ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
16511da177e4SLinus Torvalds  */
16521da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
16531da177e4SLinus Torvalds {
16541da177e4SLinus Torvalds 	unsigned long long num;
1655d4be151bSAndré Goddard Rosa 	char *str, *end;
1656fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
16571da177e4SLinus Torvalds 
1658f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1659f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
16602f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
16611da177e4SLinus Torvalds 		return 0;
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	str = buf;
1664f796937aSJeremy Fitzhardinge 	end = buf + size;
16651da177e4SLinus Torvalds 
1666f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1667f796937aSJeremy Fitzhardinge 	if (end < buf) {
16681da177e4SLinus Torvalds 		end = ((void *)-1);
1669f796937aSJeremy Fitzhardinge 		size = end - buf;
16701da177e4SLinus Torvalds 	}
16711da177e4SLinus Torvalds 
1672fef20d9cSFrederic Weisbecker 	while (*fmt) {
1673fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1674d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1675fef20d9cSFrederic Weisbecker 
1676fef20d9cSFrederic Weisbecker 		fmt += read;
1677fef20d9cSFrederic Weisbecker 
1678fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1679fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1680fef20d9cSFrederic Weisbecker 			int copy = read;
1681fef20d9cSFrederic Weisbecker 			if (str < end) {
1682fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1683fef20d9cSFrederic Weisbecker 					copy = end - str;
1684fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1685fef20d9cSFrederic Weisbecker 			}
1686fef20d9cSFrederic Weisbecker 			str += read;
1687fef20d9cSFrederic Weisbecker 			break;
16881da177e4SLinus Torvalds 		}
16891da177e4SLinus Torvalds 
1690ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1691fef20d9cSFrederic Weisbecker 			spec.field_width = va_arg(args, int);
1692fef20d9cSFrederic Weisbecker 			break;
16931da177e4SLinus Torvalds 
1694fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1695fef20d9cSFrederic Weisbecker 			spec.precision = va_arg(args, int);
1696fef20d9cSFrederic Weisbecker 			break;
16971da177e4SLinus Torvalds 
1698d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1699d4be151bSAndré Goddard Rosa 			char c;
1700d4be151bSAndré Goddard Rosa 
1701fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1702fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1703f796937aSJeremy Fitzhardinge 					if (str < end)
17041da177e4SLinus Torvalds 						*str = ' ';
17051da177e4SLinus Torvalds 					++str;
1706fef20d9cSFrederic Weisbecker 
17071da177e4SLinus Torvalds 				}
17081da177e4SLinus Torvalds 			}
17091da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1710f796937aSJeremy Fitzhardinge 			if (str < end)
17111da177e4SLinus Torvalds 				*str = c;
17121da177e4SLinus Torvalds 			++str;
1713fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1714f796937aSJeremy Fitzhardinge 				if (str < end)
17151da177e4SLinus Torvalds 					*str = ' ';
17161da177e4SLinus Torvalds 				++str;
17171da177e4SLinus Torvalds 			}
1718fef20d9cSFrederic Weisbecker 			break;
1719d4be151bSAndré Goddard Rosa 		}
17201da177e4SLinus Torvalds 
1721fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1722fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1723fef20d9cSFrederic Weisbecker 			break;
17241da177e4SLinus Torvalds 
1725fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1726fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, va_arg(args, void *),
1727fef20d9cSFrederic Weisbecker 				      spec);
1728fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
17294d8a743cSLinus Torvalds 				fmt++;
1730fef20d9cSFrederic Weisbecker 			break;
17311da177e4SLinus Torvalds 
1732fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1733f796937aSJeremy Fitzhardinge 			if (str < end)
17341da177e4SLinus Torvalds 				*str = '%';
17351da177e4SLinus Torvalds 			++str;
17361da177e4SLinus Torvalds 			break;
17371da177e4SLinus Torvalds 
1738fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1739f796937aSJeremy Fitzhardinge 			if (str < end)
17401da177e4SLinus Torvalds 				*str = '%';
17411da177e4SLinus Torvalds 			++str;
1742fef20d9cSFrederic Weisbecker 			break;
1743fef20d9cSFrederic Weisbecker 
1744fef20d9cSFrederic Weisbecker 		default:
1745fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1746fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1747fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1748fef20d9cSFrederic Weisbecker 				break;
1749fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1750fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
1751fef20d9cSFrederic Weisbecker 				break;
1752fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1753fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
1754fef20d9cSFrederic Weisbecker 				break;
1755fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1756ef124960SJason Gunthorpe 				if (spec.flags & SIGN)
1757ef124960SJason Gunthorpe 					num = va_arg(args, ssize_t);
1758ef124960SJason Gunthorpe 				else
1759fef20d9cSFrederic Weisbecker 					num = va_arg(args, size_t);
1760fef20d9cSFrederic Weisbecker 				break;
1761fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1762fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
1763fef20d9cSFrederic Weisbecker 				break;
1764a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1765a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
1766a4e94ef0SZhaolei 				break;
1767a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1768a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
1769a4e94ef0SZhaolei 				break;
1770fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1771fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
1772fef20d9cSFrederic Weisbecker 				break;
1773fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1774fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
1775fef20d9cSFrederic Weisbecker 				break;
177639e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
177739e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
1778fef20d9cSFrederic Weisbecker 				break;
1779fef20d9cSFrederic Weisbecker 			default:
1780fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
17811da177e4SLinus Torvalds 			}
1782fef20d9cSFrederic Weisbecker 
1783fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
17841da177e4SLinus Torvalds 		}
1785fef20d9cSFrederic Weisbecker 	}
1786fef20d9cSFrederic Weisbecker 
1787f796937aSJeremy Fitzhardinge 	if (size > 0) {
1788f796937aSJeremy Fitzhardinge 		if (str < end)
17891da177e4SLinus Torvalds 			*str = '\0';
1790f796937aSJeremy Fitzhardinge 		else
17910a6047eeSLinus Torvalds 			end[-1] = '\0';
1792f796937aSJeremy Fitzhardinge 	}
1793fef20d9cSFrederic Weisbecker 
1794f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
17951da177e4SLinus Torvalds 	return str-buf;
1796fef20d9cSFrederic Weisbecker 
17971da177e4SLinus Torvalds }
17981da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds /**
18011da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
18021da177e4SLinus Torvalds  * @buf: The buffer to place the result into
18031da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
18041da177e4SLinus Torvalds  * @fmt: The format string to use
18051da177e4SLinus Torvalds  * @args: Arguments for the format string
18061da177e4SLinus Torvalds  *
18071da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
1808b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
18091da177e4SLinus Torvalds  * returns 0.
18101da177e4SLinus Torvalds  *
1811ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
181220036fdcSAndi Kleen  *
181320036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
18141da177e4SLinus Torvalds  */
18151da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
18161da177e4SLinus Torvalds {
18171da177e4SLinus Torvalds 	int i;
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
18207b9186f5SAndré Goddard Rosa 
1821b921c69fSAnton Arapov 	if (likely(i < size))
1822b921c69fSAnton Arapov 		return i;
1823b921c69fSAnton Arapov 	if (size != 0)
1824b921c69fSAnton Arapov 		return size - 1;
1825b921c69fSAnton Arapov 	return 0;
18261da177e4SLinus Torvalds }
18271da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds /**
18301da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
18311da177e4SLinus Torvalds  * @buf: The buffer to place the result into
18321da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
18331da177e4SLinus Torvalds  * @fmt: The format string to use
18341da177e4SLinus Torvalds  * @...: Arguments for the format string
18351da177e4SLinus Torvalds  *
18361da177e4SLinus Torvalds  * The return value is the number of characters which would be
18371da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
18381da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
18391da177e4SLinus Torvalds  * @size, the resulting string is truncated.
184020036fdcSAndi Kleen  *
184120036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
18421da177e4SLinus Torvalds  */
18431da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
18441da177e4SLinus Torvalds {
18451da177e4SLinus Torvalds 	va_list args;
18461da177e4SLinus Torvalds 	int i;
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds 	va_start(args, fmt);
18491da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
18501da177e4SLinus Torvalds 	va_end(args);
18517b9186f5SAndré Goddard Rosa 
18521da177e4SLinus Torvalds 	return i;
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds /**
18571da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
18581da177e4SLinus Torvalds  * @buf: The buffer to place the result into
18591da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
18601da177e4SLinus Torvalds  * @fmt: The format string to use
18611da177e4SLinus Torvalds  * @...: Arguments for the format string
18621da177e4SLinus Torvalds  *
18631da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
1864b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
18651da177e4SLinus Torvalds  */
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
18681da177e4SLinus Torvalds {
18691da177e4SLinus Torvalds 	va_list args;
18701da177e4SLinus Torvalds 	int i;
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds 	va_start(args, fmt);
1873b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
18741da177e4SLinus Torvalds 	va_end(args);
18757b9186f5SAndré Goddard Rosa 
1876b903c0b8SChangli Gao 	return i;
18771da177e4SLinus Torvalds }
18781da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds /**
18811da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
18821da177e4SLinus Torvalds  * @buf: The buffer to place the result into
18831da177e4SLinus Torvalds  * @fmt: The format string to use
18841da177e4SLinus Torvalds  * @args: Arguments for the format string
18851da177e4SLinus Torvalds  *
18861da177e4SLinus Torvalds  * The function returns the number of characters written
188772fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
18881da177e4SLinus Torvalds  * buffer overflows.
18891da177e4SLinus Torvalds  *
1890ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
189120036fdcSAndi Kleen  *
189220036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
18931da177e4SLinus Torvalds  */
18941da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
18951da177e4SLinus Torvalds {
18961da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
18971da177e4SLinus Torvalds }
18981da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds /**
19011da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
19021da177e4SLinus Torvalds  * @buf: The buffer to place the result into
19031da177e4SLinus Torvalds  * @fmt: The format string to use
19041da177e4SLinus Torvalds  * @...: Arguments for the format string
19051da177e4SLinus Torvalds  *
19061da177e4SLinus Torvalds  * The function returns the number of characters written
190772fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
19081da177e4SLinus Torvalds  * buffer overflows.
190920036fdcSAndi Kleen  *
191020036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
19111da177e4SLinus Torvalds  */
19121da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
19131da177e4SLinus Torvalds {
19141da177e4SLinus Torvalds 	va_list args;
19151da177e4SLinus Torvalds 	int i;
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds 	va_start(args, fmt);
19181da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
19191da177e4SLinus Torvalds 	va_end(args);
19207b9186f5SAndré Goddard Rosa 
19211da177e4SLinus Torvalds 	return i;
19221da177e4SLinus Torvalds }
19231da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
19241da177e4SLinus Torvalds 
19254370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
19264370aa4aSLai Jiangshan /*
19274370aa4aSLai Jiangshan  * bprintf service:
19284370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
19294370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
19304370aa4aSLai Jiangshan  */
19314370aa4aSLai Jiangshan 
19324370aa4aSLai Jiangshan /**
19334370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
19344370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
19354370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
19364370aa4aSLai Jiangshan  * @fmt: The format string to use
19374370aa4aSLai Jiangshan  * @args: Arguments for the format string
19384370aa4aSLai Jiangshan  *
19394370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
19404370aa4aSLai Jiangshan  * is skiped.
19414370aa4aSLai Jiangshan  *
19424370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
19434370aa4aSLai Jiangshan  * the given input.
19444370aa4aSLai Jiangshan  *
19454370aa4aSLai Jiangshan  * NOTE:
19464370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
19474370aa4aSLai Jiangshan  * valid for bstr_printf().
19484370aa4aSLai Jiangshan  */
19494370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
19504370aa4aSLai Jiangshan {
1951fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
19524370aa4aSLai Jiangshan 	char *str, *end;
19534370aa4aSLai Jiangshan 
19544370aa4aSLai Jiangshan 	str = (char *)bin_buf;
19554370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
19564370aa4aSLai Jiangshan 
19574370aa4aSLai Jiangshan #define save_arg(type)							\
19584370aa4aSLai Jiangshan do {									\
19594370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
19604370aa4aSLai Jiangshan 		unsigned long long value;				\
19614370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
19624370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
19634370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
19644370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
19654370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
19664370aa4aSLai Jiangshan 		}							\
19674370aa4aSLai Jiangshan 	} else {							\
19684370aa4aSLai Jiangshan 		unsigned long value;					\
19694370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
19704370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
19714370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
19724370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
19734370aa4aSLai Jiangshan 	}								\
19744370aa4aSLai Jiangshan 	str += sizeof(type);						\
19754370aa4aSLai Jiangshan } while (0)
19764370aa4aSLai Jiangshan 
1977fef20d9cSFrederic Weisbecker 	while (*fmt) {
1978d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
19794370aa4aSLai Jiangshan 
1980fef20d9cSFrederic Weisbecker 		fmt += read;
1981fef20d9cSFrederic Weisbecker 
1982fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1983fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
1984d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_INVALID:
1985d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
1986fef20d9cSFrederic Weisbecker 			break;
1987fef20d9cSFrederic Weisbecker 
1988ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1989fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
19904370aa4aSLai Jiangshan 			save_arg(int);
1991fef20d9cSFrederic Weisbecker 			break;
19924370aa4aSLai Jiangshan 
1993fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
19944370aa4aSLai Jiangshan 			save_arg(char);
1995fef20d9cSFrederic Weisbecker 			break;
1996fef20d9cSFrederic Weisbecker 
1997fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
19984370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
19994370aa4aSLai Jiangshan 			size_t len;
20006c356634SAndré Goddard Rosa 
20014370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
20024370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
20030f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
20046c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
20056c356634SAndré Goddard Rosa 			if (str + len < end)
20066c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
20076c356634SAndré Goddard Rosa 			str += len;
2008fef20d9cSFrederic Weisbecker 			break;
20094370aa4aSLai Jiangshan 		}
2010fef20d9cSFrederic Weisbecker 
2011fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
20124370aa4aSLai Jiangshan 			save_arg(void *);
20134370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
2014fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
20154370aa4aSLai Jiangshan 				fmt++;
2016fef20d9cSFrederic Weisbecker 			break;
2017fef20d9cSFrederic Weisbecker 
2018fef20d9cSFrederic Weisbecker 		default:
2019fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2020fef20d9cSFrederic Weisbecker 
2021fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2022fef20d9cSFrederic Weisbecker 				save_arg(long long);
2023fef20d9cSFrederic Weisbecker 				break;
2024fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2025fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2026fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
2027fef20d9cSFrederic Weisbecker 				break;
2028fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2029fef20d9cSFrederic Weisbecker 				save_arg(size_t);
2030fef20d9cSFrederic Weisbecker 				break;
2031fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2032fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
2033fef20d9cSFrederic Weisbecker 				break;
2034a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2035a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2036a4e94ef0SZhaolei 				save_arg(char);
2037a4e94ef0SZhaolei 				break;
2038fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2039fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2040fef20d9cSFrederic Weisbecker 				save_arg(short);
2041fef20d9cSFrederic Weisbecker 				break;
2042fef20d9cSFrederic Weisbecker 			default:
2043fef20d9cSFrederic Weisbecker 				save_arg(int);
2044fef20d9cSFrederic Weisbecker 			}
2045fef20d9cSFrederic Weisbecker 		}
2046fef20d9cSFrederic Weisbecker 	}
2047fef20d9cSFrederic Weisbecker 
20487b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2049fef20d9cSFrederic Weisbecker #undef save_arg
20504370aa4aSLai Jiangshan }
20514370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
20524370aa4aSLai Jiangshan 
20534370aa4aSLai Jiangshan /**
20544370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
20554370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
20564370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
20574370aa4aSLai Jiangshan  * @fmt: The format string to use
20584370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
20594370aa4aSLai Jiangshan  *
20604370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
20614370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
20624370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
20634370aa4aSLai Jiangshan  *
20644370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
20650efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
20664370aa4aSLai Jiangshan  *
20674370aa4aSLai Jiangshan  * The return value is the number of characters which would
20684370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
20694370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
20704370aa4aSLai Jiangshan  * number of characters written into @buf as return value
20714370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
20724370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
20734370aa4aSLai Jiangshan  * string is truncated.
20744370aa4aSLai Jiangshan  */
20754370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
20764370aa4aSLai Jiangshan {
2077fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
2078d4be151bSAndré Goddard Rosa 	char *str, *end;
2079d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
20804370aa4aSLai Jiangshan 
20812f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
20824370aa4aSLai Jiangshan 		return 0;
20834370aa4aSLai Jiangshan 
20844370aa4aSLai Jiangshan 	str = buf;
20854370aa4aSLai Jiangshan 	end = buf + size;
20864370aa4aSLai Jiangshan 
20874370aa4aSLai Jiangshan #define get_arg(type)							\
20884370aa4aSLai Jiangshan ({									\
20894370aa4aSLai Jiangshan 	typeof(type) value;						\
20904370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
20914370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
20924370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
20934370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
20944370aa4aSLai Jiangshan 	} else {							\
20954370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
20964370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
20974370aa4aSLai Jiangshan 	}								\
20984370aa4aSLai Jiangshan 	args += sizeof(type);						\
20994370aa4aSLai Jiangshan 	value;								\
21004370aa4aSLai Jiangshan })
21014370aa4aSLai Jiangshan 
21024370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
21034370aa4aSLai Jiangshan 	if (end < buf) {
21044370aa4aSLai Jiangshan 		end = ((void *)-1);
21054370aa4aSLai Jiangshan 		size = end - buf;
21064370aa4aSLai Jiangshan 	}
21074370aa4aSLai Jiangshan 
2108fef20d9cSFrederic Weisbecker 	while (*fmt) {
2109fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
2110d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
2111fef20d9cSFrederic Weisbecker 
2112fef20d9cSFrederic Weisbecker 		fmt += read;
2113fef20d9cSFrederic Weisbecker 
2114fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2115fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2116fef20d9cSFrederic Weisbecker 			int copy = read;
2117fef20d9cSFrederic Weisbecker 			if (str < end) {
2118fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2119fef20d9cSFrederic Weisbecker 					copy = end - str;
2120fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2121fef20d9cSFrederic Weisbecker 			}
2122fef20d9cSFrederic Weisbecker 			str += read;
2123fef20d9cSFrederic Weisbecker 			break;
21244370aa4aSLai Jiangshan 		}
21254370aa4aSLai Jiangshan 
2126ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2127fef20d9cSFrederic Weisbecker 			spec.field_width = get_arg(int);
2128fef20d9cSFrederic Weisbecker 			break;
21294370aa4aSLai Jiangshan 
2130fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
2131fef20d9cSFrederic Weisbecker 			spec.precision = get_arg(int);
2132fef20d9cSFrederic Weisbecker 			break;
21334370aa4aSLai Jiangshan 
2134d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2135d4be151bSAndré Goddard Rosa 			char c;
2136d4be151bSAndré Goddard Rosa 
2137fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2138fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
21394370aa4aSLai Jiangshan 					if (str < end)
21404370aa4aSLai Jiangshan 						*str = ' ';
21414370aa4aSLai Jiangshan 					++str;
21424370aa4aSLai Jiangshan 				}
21434370aa4aSLai Jiangshan 			}
21444370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
21454370aa4aSLai Jiangshan 			if (str < end)
21464370aa4aSLai Jiangshan 				*str = c;
21474370aa4aSLai Jiangshan 			++str;
2148fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
21494370aa4aSLai Jiangshan 				if (str < end)
21504370aa4aSLai Jiangshan 					*str = ' ';
21514370aa4aSLai Jiangshan 				++str;
21524370aa4aSLai Jiangshan 			}
2153fef20d9cSFrederic Weisbecker 			break;
2154d4be151bSAndré Goddard Rosa 		}
21554370aa4aSLai Jiangshan 
2156fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
21574370aa4aSLai Jiangshan 			const char *str_arg = args;
2158d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
2159fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
2160fef20d9cSFrederic Weisbecker 			break;
21614370aa4aSLai Jiangshan 		}
21624370aa4aSLai Jiangshan 
2163fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2164fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, get_arg(void *), spec);
2165fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
21664370aa4aSLai Jiangshan 				fmt++;
2167fef20d9cSFrederic Weisbecker 			break;
21684370aa4aSLai Jiangshan 
2169fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
2170fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
21714370aa4aSLai Jiangshan 			if (str < end)
21724370aa4aSLai Jiangshan 				*str = '%';
21734370aa4aSLai Jiangshan 			++str;
2174fef20d9cSFrederic Weisbecker 			break;
2175fef20d9cSFrederic Weisbecker 
2176d4be151bSAndré Goddard Rosa 		default: {
2177d4be151bSAndré Goddard Rosa 			unsigned long long num;
2178d4be151bSAndré Goddard Rosa 
2179fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2180fef20d9cSFrederic Weisbecker 
2181fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
21824370aa4aSLai Jiangshan 				num = get_arg(long long);
2183fef20d9cSFrederic Weisbecker 				break;
2184fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2185fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2186fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
2187fef20d9cSFrederic Weisbecker 				break;
2188fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
21894370aa4aSLai Jiangshan 				num = get_arg(size_t);
2190fef20d9cSFrederic Weisbecker 				break;
2191fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
21924370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
2193fef20d9cSFrederic Weisbecker 				break;
2194a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2195a4e94ef0SZhaolei 				num = get_arg(unsigned char);
2196a4e94ef0SZhaolei 				break;
2197a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2198a4e94ef0SZhaolei 				num = get_arg(signed char);
2199a4e94ef0SZhaolei 				break;
2200fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2201fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
2202fef20d9cSFrederic Weisbecker 				break;
2203fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2204fef20d9cSFrederic Weisbecker 				num = get_arg(short);
2205fef20d9cSFrederic Weisbecker 				break;
2206fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
22074370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
2208fef20d9cSFrederic Weisbecker 				break;
2209fef20d9cSFrederic Weisbecker 			default:
2210fef20d9cSFrederic Weisbecker 				num = get_arg(int);
22114370aa4aSLai Jiangshan 			}
2212fef20d9cSFrederic Weisbecker 
2213fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
2214d4be151bSAndré Goddard Rosa 		} /* default: */
2215d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
2216d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
2217fef20d9cSFrederic Weisbecker 
22184370aa4aSLai Jiangshan 	if (size > 0) {
22194370aa4aSLai Jiangshan 		if (str < end)
22204370aa4aSLai Jiangshan 			*str = '\0';
22214370aa4aSLai Jiangshan 		else
22224370aa4aSLai Jiangshan 			end[-1] = '\0';
22234370aa4aSLai Jiangshan 	}
2224fef20d9cSFrederic Weisbecker 
22254370aa4aSLai Jiangshan #undef get_arg
22264370aa4aSLai Jiangshan 
22274370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
22284370aa4aSLai Jiangshan 	return str - buf;
22294370aa4aSLai Jiangshan }
22304370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
22314370aa4aSLai Jiangshan 
22324370aa4aSLai Jiangshan /**
22334370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
22344370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
22354370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
22364370aa4aSLai Jiangshan  * @fmt: The format string to use
22374370aa4aSLai Jiangshan  * @...: Arguments for the format string
22384370aa4aSLai Jiangshan  *
22394370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
22404370aa4aSLai Jiangshan  * into @bin_buf.
22414370aa4aSLai Jiangshan  */
22424370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
22434370aa4aSLai Jiangshan {
22444370aa4aSLai Jiangshan 	va_list args;
22454370aa4aSLai Jiangshan 	int ret;
22464370aa4aSLai Jiangshan 
22474370aa4aSLai Jiangshan 	va_start(args, fmt);
22484370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
22494370aa4aSLai Jiangshan 	va_end(args);
22507b9186f5SAndré Goddard Rosa 
22514370aa4aSLai Jiangshan 	return ret;
22524370aa4aSLai Jiangshan }
22534370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
22544370aa4aSLai Jiangshan 
22554370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
22564370aa4aSLai Jiangshan 
22571da177e4SLinus Torvalds /**
22581da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
22591da177e4SLinus Torvalds  * @buf:	input buffer
22601da177e4SLinus Torvalds  * @fmt:	format of buffer
22611da177e4SLinus Torvalds  * @args:	arguments
22621da177e4SLinus Torvalds  */
22631da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
22641da177e4SLinus Torvalds {
22651da177e4SLinus Torvalds 	const char *str = buf;
22661da177e4SLinus Torvalds 	char *next;
22671da177e4SLinus Torvalds 	char digit;
22681da177e4SLinus Torvalds 	int num = 0;
2269ef0658f3SJoe Perches 	u8 qualifier;
227053809751SJan Beulich 	unsigned int base;
227153809751SJan Beulich 	union {
227253809751SJan Beulich 		long long s;
227353809751SJan Beulich 		unsigned long long u;
227453809751SJan Beulich 	} val;
2275ef0658f3SJoe Perches 	s16 field_width;
2276d4be151bSAndré Goddard Rosa 	bool is_sign;
22771da177e4SLinus Torvalds 
2278da99075cSJan Beulich 	while (*fmt) {
22791da177e4SLinus Torvalds 		/* skip any white space in format */
22801da177e4SLinus Torvalds 		/* white space in format matchs any amount of
22811da177e4SLinus Torvalds 		 * white space, including none, in the input.
22821da177e4SLinus Torvalds 		 */
22831da177e4SLinus Torvalds 		if (isspace(*fmt)) {
2284e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
2285e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
22861da177e4SLinus Torvalds 		}
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
22891da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
22901da177e4SLinus Torvalds 			if (*fmt++ != *str++)
22911da177e4SLinus Torvalds 				break;
22921da177e4SLinus Torvalds 			continue;
22931da177e4SLinus Torvalds 		}
22941da177e4SLinus Torvalds 
22951da177e4SLinus Torvalds 		if (!*fmt)
22961da177e4SLinus Torvalds 			break;
22971da177e4SLinus Torvalds 		++fmt;
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds 		/* skip this conversion.
23001da177e4SLinus Torvalds 		 * advance both strings to next white space
23011da177e4SLinus Torvalds 		 */
23021da177e4SLinus Torvalds 		if (*fmt == '*') {
2303da99075cSJan Beulich 			if (!*str)
2304da99075cSJan Beulich 				break;
23058fccae2cSAndy Spencer 			while (!isspace(*fmt) && *fmt != '%' && *fmt)
23061da177e4SLinus Torvalds 				fmt++;
23071da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
23081da177e4SLinus Torvalds 				str++;
23091da177e4SLinus Torvalds 			continue;
23101da177e4SLinus Torvalds 		}
23111da177e4SLinus Torvalds 
23121da177e4SLinus Torvalds 		/* get field width */
23131da177e4SLinus Torvalds 		field_width = -1;
231453809751SJan Beulich 		if (isdigit(*fmt)) {
23151da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
231653809751SJan Beulich 			if (field_width <= 0)
231753809751SJan Beulich 				break;
231853809751SJan Beulich 		}
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds 		/* get conversion qualifier */
23211da177e4SLinus Torvalds 		qualifier = -1;
232275fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
232375fb8f26SAndy Shevchenko 		    _tolower(*fmt) == 'z') {
23241da177e4SLinus Torvalds 			qualifier = *fmt++;
23251da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
23261da177e4SLinus Torvalds 				if (qualifier == 'h') {
23271da177e4SLinus Torvalds 					qualifier = 'H';
23281da177e4SLinus Torvalds 					fmt++;
23291da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
23301da177e4SLinus Torvalds 					qualifier = 'L';
23311da177e4SLinus Torvalds 					fmt++;
23321da177e4SLinus Torvalds 				}
23331da177e4SLinus Torvalds 			}
23341da177e4SLinus Torvalds 		}
23351da177e4SLinus Torvalds 
2336da99075cSJan Beulich 		if (!*fmt)
2337da99075cSJan Beulich 			break;
2338da99075cSJan Beulich 
2339da99075cSJan Beulich 		if (*fmt == 'n') {
2340da99075cSJan Beulich 			/* return number of characters read so far */
2341da99075cSJan Beulich 			*va_arg(args, int *) = str - buf;
2342da99075cSJan Beulich 			++fmt;
2343da99075cSJan Beulich 			continue;
2344da99075cSJan Beulich 		}
2345da99075cSJan Beulich 
2346da99075cSJan Beulich 		if (!*str)
23471da177e4SLinus Torvalds 			break;
23481da177e4SLinus Torvalds 
2349d4be151bSAndré Goddard Rosa 		base = 10;
2350*3f623ebaSFabian Frederick 		is_sign = false;
2351d4be151bSAndré Goddard Rosa 
23521da177e4SLinus Torvalds 		switch (*fmt++) {
23531da177e4SLinus Torvalds 		case 'c':
23541da177e4SLinus Torvalds 		{
23551da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
23561da177e4SLinus Torvalds 			if (field_width == -1)
23571da177e4SLinus Torvalds 				field_width = 1;
23581da177e4SLinus Torvalds 			do {
23591da177e4SLinus Torvalds 				*s++ = *str++;
23601da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
23611da177e4SLinus Torvalds 			num++;
23621da177e4SLinus Torvalds 		}
23631da177e4SLinus Torvalds 		continue;
23641da177e4SLinus Torvalds 		case 's':
23651da177e4SLinus Torvalds 		{
23661da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
23671da177e4SLinus Torvalds 			if (field_width == -1)
23684be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
23691da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
2370e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 			/* now copy until next white space */
23737b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
23741da177e4SLinus Torvalds 				*s++ = *str++;
23751da177e4SLinus Torvalds 			*s = '\0';
23761da177e4SLinus Torvalds 			num++;
23771da177e4SLinus Torvalds 		}
23781da177e4SLinus Torvalds 		continue;
23791da177e4SLinus Torvalds 		case 'o':
23801da177e4SLinus Torvalds 			base = 8;
23811da177e4SLinus Torvalds 			break;
23821da177e4SLinus Torvalds 		case 'x':
23831da177e4SLinus Torvalds 		case 'X':
23841da177e4SLinus Torvalds 			base = 16;
23851da177e4SLinus Torvalds 			break;
23861da177e4SLinus Torvalds 		case 'i':
23871da177e4SLinus Torvalds 			base = 0;
23881da177e4SLinus Torvalds 		case 'd':
2389*3f623ebaSFabian Frederick 			is_sign = true;
23901da177e4SLinus Torvalds 		case 'u':
23911da177e4SLinus Torvalds 			break;
23921da177e4SLinus Torvalds 		case '%':
23931da177e4SLinus Torvalds 			/* looking for '%' in str */
23941da177e4SLinus Torvalds 			if (*str++ != '%')
23951da177e4SLinus Torvalds 				return num;
23961da177e4SLinus Torvalds 			continue;
23971da177e4SLinus Torvalds 		default:
23981da177e4SLinus Torvalds 			/* invalid format; stop here */
23991da177e4SLinus Torvalds 			return num;
24001da177e4SLinus Torvalds 		}
24011da177e4SLinus Torvalds 
24021da177e4SLinus Torvalds 		/* have some sort of integer conversion.
24031da177e4SLinus Torvalds 		 * first, skip white space in buffer.
24041da177e4SLinus Torvalds 		 */
2405e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds 		digit = *str;
24081da177e4SLinus Torvalds 		if (is_sign && digit == '-')
24091da177e4SLinus Torvalds 			digit = *(str + 1);
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds 		if (!digit
24121da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
24131da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
24141da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
24151da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
24161da177e4SLinus Torvalds 			break;
24171da177e4SLinus Torvalds 
241853809751SJan Beulich 		if (is_sign)
241953809751SJan Beulich 			val.s = qualifier != 'L' ?
242053809751SJan Beulich 				simple_strtol(str, &next, base) :
242153809751SJan Beulich 				simple_strtoll(str, &next, base);
242253809751SJan Beulich 		else
242353809751SJan Beulich 			val.u = qualifier != 'L' ?
242453809751SJan Beulich 				simple_strtoul(str, &next, base) :
242553809751SJan Beulich 				simple_strtoull(str, &next, base);
242653809751SJan Beulich 
242753809751SJan Beulich 		if (field_width > 0 && next - str > field_width) {
242853809751SJan Beulich 			if (base == 0)
242953809751SJan Beulich 				_parse_integer_fixup_radix(str, &base);
243053809751SJan Beulich 			while (next - str > field_width) {
243153809751SJan Beulich 				if (is_sign)
243253809751SJan Beulich 					val.s = div_s64(val.s, base);
243353809751SJan Beulich 				else
243453809751SJan Beulich 					val.u = div_u64(val.u, base);
243553809751SJan Beulich 				--next;
243653809751SJan Beulich 			}
243753809751SJan Beulich 		}
243853809751SJan Beulich 
24391da177e4SLinus Torvalds 		switch (qualifier) {
24401da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
244153809751SJan Beulich 			if (is_sign)
244253809751SJan Beulich 				*va_arg(args, signed char *) = val.s;
244353809751SJan Beulich 			else
244453809751SJan Beulich 				*va_arg(args, unsigned char *) = val.u;
24451da177e4SLinus Torvalds 			break;
24461da177e4SLinus Torvalds 		case 'h':
244753809751SJan Beulich 			if (is_sign)
244853809751SJan Beulich 				*va_arg(args, short *) = val.s;
244953809751SJan Beulich 			else
245053809751SJan Beulich 				*va_arg(args, unsigned short *) = val.u;
24511da177e4SLinus Torvalds 			break;
24521da177e4SLinus Torvalds 		case 'l':
245353809751SJan Beulich 			if (is_sign)
245453809751SJan Beulich 				*va_arg(args, long *) = val.s;
245553809751SJan Beulich 			else
245653809751SJan Beulich 				*va_arg(args, unsigned long *) = val.u;
24571da177e4SLinus Torvalds 			break;
24581da177e4SLinus Torvalds 		case 'L':
245953809751SJan Beulich 			if (is_sign)
246053809751SJan Beulich 				*va_arg(args, long long *) = val.s;
246153809751SJan Beulich 			else
246253809751SJan Beulich 				*va_arg(args, unsigned long long *) = val.u;
24631da177e4SLinus Torvalds 			break;
24641da177e4SLinus Torvalds 		case 'Z':
24651da177e4SLinus Torvalds 		case 'z':
246653809751SJan Beulich 			*va_arg(args, size_t *) = val.u;
24671da177e4SLinus Torvalds 			break;
24681da177e4SLinus Torvalds 		default:
246953809751SJan Beulich 			if (is_sign)
247053809751SJan Beulich 				*va_arg(args, int *) = val.s;
247153809751SJan Beulich 			else
247253809751SJan Beulich 				*va_arg(args, unsigned int *) = val.u;
24731da177e4SLinus Torvalds 			break;
24741da177e4SLinus Torvalds 		}
24751da177e4SLinus Torvalds 		num++;
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds 		if (!next)
24781da177e4SLinus Torvalds 			break;
24791da177e4SLinus Torvalds 		str = next;
24801da177e4SLinus Torvalds 	}
2481c6b40d16SJohannes Berg 
24821da177e4SLinus Torvalds 	return num;
24831da177e4SLinus Torvalds }
24841da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
24851da177e4SLinus Torvalds 
24861da177e4SLinus Torvalds /**
24871da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
24881da177e4SLinus Torvalds  * @buf:	input buffer
24891da177e4SLinus Torvalds  * @fmt:	formatting of buffer
24901da177e4SLinus Torvalds  * @...:	resulting arguments
24911da177e4SLinus Torvalds  */
24921da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
24931da177e4SLinus Torvalds {
24941da177e4SLinus Torvalds 	va_list args;
24951da177e4SLinus Torvalds 	int i;
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 	va_start(args, fmt);
24981da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
24991da177e4SLinus Torvalds 	va_end(args);
25007b9186f5SAndré Goddard Rosa 
25011da177e4SLinus Torvalds 	return i;
25021da177e4SLinus Torvalds }
25031da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
2504