xref: /openbmc/linux/lib/vsprintf.c (revision dbc760bcc150cc27160f0131b15db76350df4334)
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 
3671dca95dSAndy Shevchenko #include <linux/string_helpers.h>
371dff46d6SAlexey Dobriyan #include "kstrtox.h"
38aa46a63eSHarvey Harrison 
391da177e4SLinus Torvalds /**
401da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
411da177e4SLinus Torvalds  * @cp: The start of the string
421da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
431da177e4SLinus Torvalds  * @base: The number base to use
44462e4711SEldad Zack  *
45462e4711SEldad Zack  * This function is obsolete. Please use kstrtoull instead.
461da177e4SLinus Torvalds  */
471da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
481da177e4SLinus Torvalds {
491dff46d6SAlexey Dobriyan 	unsigned long long result;
501dff46d6SAlexey Dobriyan 	unsigned int rv;
511da177e4SLinus Torvalds 
521dff46d6SAlexey Dobriyan 	cp = _parse_integer_fixup_radix(cp, &base);
531dff46d6SAlexey Dobriyan 	rv = _parse_integer(cp, base, &result);
541dff46d6SAlexey Dobriyan 	/* FIXME */
551dff46d6SAlexey Dobriyan 	cp += (rv & ~KSTRTOX_OVERFLOW);
56aa46a63eSHarvey Harrison 
571da177e4SLinus Torvalds 	if (endp)
581da177e4SLinus Torvalds 		*endp = (char *)cp;
597b9186f5SAndré Goddard Rosa 
601da177e4SLinus Torvalds 	return result;
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /**
65922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
66922ac25cSAndré Goddard Rosa  * @cp: The start of the string
67922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
68922ac25cSAndré Goddard Rosa  * @base: The number base to use
69462e4711SEldad Zack  *
70462e4711SEldad Zack  * This function is obsolete. Please use kstrtoul instead.
71922ac25cSAndré Goddard Rosa  */
72922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
73922ac25cSAndré Goddard Rosa {
74922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
75922ac25cSAndré Goddard Rosa }
76922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
77922ac25cSAndré Goddard Rosa 
78922ac25cSAndré Goddard Rosa /**
79922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
80922ac25cSAndré Goddard Rosa  * @cp: The start of the string
81922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
82922ac25cSAndré Goddard Rosa  * @base: The number base to use
83462e4711SEldad Zack  *
84462e4711SEldad Zack  * This function is obsolete. Please use kstrtol instead.
85922ac25cSAndré Goddard Rosa  */
86922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
87922ac25cSAndré Goddard Rosa {
88922ac25cSAndré Goddard Rosa 	if (*cp == '-')
89922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
90922ac25cSAndré Goddard Rosa 
91922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
92922ac25cSAndré Goddard Rosa }
93922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
94922ac25cSAndré Goddard Rosa 
95922ac25cSAndré Goddard Rosa /**
961da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
971da177e4SLinus Torvalds  * @cp: The start of the string
981da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
991da177e4SLinus Torvalds  * @base: The number base to use
100462e4711SEldad Zack  *
101462e4711SEldad Zack  * This function is obsolete. Please use kstrtoll instead.
1021da177e4SLinus Torvalds  */
1031da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	if (*cp == '-')
1061da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1077b9186f5SAndré Goddard Rosa 
1081da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1091da177e4SLinus Torvalds }
11098d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1111da177e4SLinus Torvalds 
112cf3b429bSJoe Perches static noinline_for_stack
113cf3b429bSJoe Perches int skip_atoi(const char **s)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	int i = 0;
1161da177e4SLinus Torvalds 
11743e5b666SRasmus Villemoes 	do {
1181da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
11943e5b666SRasmus Villemoes 	} while (isdigit(**s));
1207b9186f5SAndré Goddard Rosa 
1211da177e4SLinus Torvalds 	return i;
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
1244277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used
1254277eeddSDenis Vlasenko  * for /proc and /sys data. This directly impacts e.g. top performance
1264277eeddSDenis Vlasenko  * with many processes running. We optimize it for speed
127133fd9f5SDenys Vlasenko  * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
128133fd9f5SDenys Vlasenko  * (with permission from the author, Douglas W. Jones).
129133fd9f5SDenys Vlasenko  */
1304277eeddSDenis Vlasenko 
131133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
132133fd9f5SDenys Vlasenko /* Formats correctly any integer in [0, 999999999] */
133cf3b429bSJoe Perches static noinline_for_stack
134133fd9f5SDenys Vlasenko char *put_dec_full9(char *buf, unsigned q)
1354277eeddSDenis Vlasenko {
136133fd9f5SDenys Vlasenko 	unsigned r;
1374277eeddSDenis Vlasenko 
1387b9186f5SAndré Goddard Rosa 	/*
1397b9186f5SAndré Goddard Rosa 	 * Possible ways to approx. divide by 10
140133fd9f5SDenys Vlasenko 	 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
141133fd9f5SDenys Vlasenko 	 * (x * 0xcccd) >> 19     x <      81920 (x < 262149 when 64-bit mul)
142133fd9f5SDenys Vlasenko 	 * (x * 0x6667) >> 18     x <      43699
143133fd9f5SDenys Vlasenko 	 * (x * 0x3334) >> 17     x <      16389
144133fd9f5SDenys Vlasenko 	 * (x * 0x199a) >> 16     x <      16389
145133fd9f5SDenys Vlasenko 	 * (x * 0x0ccd) >> 15     x <      16389
146133fd9f5SDenys Vlasenko 	 * (x * 0x0667) >> 14     x <       2739
147133fd9f5SDenys Vlasenko 	 * (x * 0x0334) >> 13     x <       1029
148133fd9f5SDenys Vlasenko 	 * (x * 0x019a) >> 12     x <       1029
149133fd9f5SDenys Vlasenko 	 * (x * 0x00cd) >> 11     x <       1029 shorter code than * 0x67 (on i386)
150133fd9f5SDenys Vlasenko 	 * (x * 0x0067) >> 10     x <        179
151133fd9f5SDenys Vlasenko 	 * (x * 0x0034) >>  9     x <         69 same
152133fd9f5SDenys Vlasenko 	 * (x * 0x001a) >>  8     x <         69 same
153133fd9f5SDenys Vlasenko 	 * (x * 0x000d) >>  7     x <         69 same, shortest code (on i386)
154133fd9f5SDenys Vlasenko 	 * (x * 0x0007) >>  6     x <         19
155133fd9f5SDenys Vlasenko 	 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
1567b9186f5SAndré Goddard Rosa 	 */
157133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
158133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 1 */
159133fd9f5SDenys Vlasenko 	q      = (r * (uint64_t)0x1999999a) >> 32;
160133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 2 */
161133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
162133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 3 */
163133fd9f5SDenys Vlasenko 	q      = (r * (uint64_t)0x1999999a) >> 32;
164133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 4 */
165133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
166133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 5 */
167133fd9f5SDenys Vlasenko 	/* Now value is under 10000, can avoid 64-bit multiply */
168133fd9f5SDenys Vlasenko 	q      = (r * 0x199a) >> 16;
169133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q)  + '0'; /* 6 */
170133fd9f5SDenys Vlasenko 	r      = (q * 0xcd) >> 11;
171133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r)  + '0'; /* 7 */
172133fd9f5SDenys Vlasenko 	q      = (r * 0xcd) >> 11;
173133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 8 */
174133fd9f5SDenys Vlasenko 	*buf++ = q + '0'; /* 9 */
175133fd9f5SDenys Vlasenko 	return buf;
176133fd9f5SDenys Vlasenko }
177133fd9f5SDenys Vlasenko #endif
1784277eeddSDenis Vlasenko 
179133fd9f5SDenys Vlasenko /* Similar to above but do not pad with zeros.
180133fd9f5SDenys Vlasenko  * Code can be easily arranged to print 9 digits too, but our callers
181133fd9f5SDenys Vlasenko  * always call put_dec_full9() instead when the number has 9 decimal digits.
182133fd9f5SDenys Vlasenko  */
183133fd9f5SDenys Vlasenko static noinline_for_stack
184133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r)
185133fd9f5SDenys Vlasenko {
186133fd9f5SDenys Vlasenko 	unsigned q;
1874277eeddSDenis Vlasenko 
188133fd9f5SDenys Vlasenko 	/* Copy of previous function's body with added early returns */
189cb239d0aSGeorge Spelvin 	while (r >= 10000) {
190cb239d0aSGeorge Spelvin 		q = r + '0';
191cb239d0aSGeorge Spelvin 		r  = (r * (uint64_t)0x1999999a) >> 32;
192cb239d0aSGeorge Spelvin 		*buf++ = q - 10*r;
193cb239d0aSGeorge Spelvin 	}
194cb239d0aSGeorge Spelvin 
195f4000516SGeorge Spelvin 	q      = (r * 0x199a) >> 16;	/* r <= 9999 */
196f4000516SGeorge Spelvin 	*buf++ = (r - 10 * q)  + '0';
197133fd9f5SDenys Vlasenko 	if (q == 0)
198133fd9f5SDenys Vlasenko 		return buf;
199f4000516SGeorge Spelvin 	r      = (q * 0xcd) >> 11;	/* q <= 999 */
200f4000516SGeorge Spelvin 	*buf++ = (q - 10 * r)  + '0';
201133fd9f5SDenys Vlasenko 	if (r == 0)
202133fd9f5SDenys Vlasenko 		return buf;
203f4000516SGeorge Spelvin 	q      = (r * 0xcd) >> 11;	/* r <= 99 */
204f4000516SGeorge Spelvin 	*buf++ = (r - 10 * q) + '0';
205133fd9f5SDenys Vlasenko 	if (q == 0)
206133fd9f5SDenys Vlasenko 		return buf;
207f4000516SGeorge Spelvin 	*buf++ = q + '0';		 /* q <= 9 */
208133fd9f5SDenys Vlasenko 	return buf;
209133fd9f5SDenys Vlasenko }
210133fd9f5SDenys Vlasenko 
211133fd9f5SDenys Vlasenko /* There are two algorithms to print larger numbers.
212133fd9f5SDenys Vlasenko  * One is generic: divide by 1000000000 and repeatedly print
213133fd9f5SDenys Vlasenko  * groups of (up to) 9 digits. It's conceptually simple,
214133fd9f5SDenys Vlasenko  * but requires a (unsigned long long) / 1000000000 division.
215133fd9f5SDenys Vlasenko  *
216133fd9f5SDenys Vlasenko  * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
217133fd9f5SDenys Vlasenko  * manipulates them cleverly and generates groups of 4 decimal digits.
218133fd9f5SDenys Vlasenko  * It so happens that it does NOT require long long division.
219133fd9f5SDenys Vlasenko  *
220133fd9f5SDenys Vlasenko  * If long is > 32 bits, division of 64-bit values is relatively easy,
221133fd9f5SDenys Vlasenko  * and we will use the first algorithm.
222133fd9f5SDenys Vlasenko  * If long long is > 64 bits (strange architecture with VERY large long long),
223133fd9f5SDenys Vlasenko  * second algorithm can't be used, and we again use the first one.
224133fd9f5SDenys Vlasenko  *
225133fd9f5SDenys Vlasenko  * Else (if long is 32 bits and long long is 64 bits) we use second one.
226133fd9f5SDenys Vlasenko  */
227133fd9f5SDenys Vlasenko 
228133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
229133fd9f5SDenys Vlasenko 
230133fd9f5SDenys Vlasenko /* First algorithm: generic */
231133fd9f5SDenys Vlasenko 
232133fd9f5SDenys Vlasenko static
233133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
234133fd9f5SDenys Vlasenko {
235133fd9f5SDenys Vlasenko 	if (n >= 100*1000*1000) {
236133fd9f5SDenys Vlasenko 		while (n >= 1000*1000*1000)
237133fd9f5SDenys Vlasenko 			buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
238133fd9f5SDenys Vlasenko 		if (n >= 100*1000*1000)
239133fd9f5SDenys Vlasenko 			return put_dec_full9(buf, n);
240133fd9f5SDenys Vlasenko 	}
241133fd9f5SDenys Vlasenko 	return put_dec_trunc8(buf, n);
242133fd9f5SDenys Vlasenko }
243133fd9f5SDenys Vlasenko 
244133fd9f5SDenys Vlasenko #else
245133fd9f5SDenys Vlasenko 
246133fd9f5SDenys Vlasenko /* Second algorithm: valid only for 64-bit long longs */
247133fd9f5SDenys Vlasenko 
248e49317d4SGeorge Spelvin /* See comment in put_dec_full9 for choice of constants */
249133fd9f5SDenys Vlasenko static noinline_for_stack
2502359172aSGeorge Spelvin void put_dec_full4(char *buf, unsigned q)
251133fd9f5SDenys Vlasenko {
252133fd9f5SDenys Vlasenko 	unsigned r;
253e49317d4SGeorge Spelvin 	r      = (q * 0xccd) >> 15;
2542359172aSGeorge Spelvin 	buf[0] = (q - 10 * r) + '0';
255e49317d4SGeorge Spelvin 	q      = (r * 0xcd) >> 11;
2562359172aSGeorge Spelvin 	buf[1] = (r - 10 * q)  + '0';
257133fd9f5SDenys Vlasenko 	r      = (q * 0xcd) >> 11;
2582359172aSGeorge Spelvin 	buf[2] = (q - 10 * r)  + '0';
2592359172aSGeorge Spelvin 	buf[3] = r + '0';
2602359172aSGeorge Spelvin }
2612359172aSGeorge Spelvin 
2622359172aSGeorge Spelvin /*
2632359172aSGeorge Spelvin  * Call put_dec_full4 on x % 10000, return x / 10000.
2642359172aSGeorge Spelvin  * The approximation x/10000 == (x * 0x346DC5D7) >> 43
2652359172aSGeorge Spelvin  * holds for all x < 1,128,869,999.  The largest value this
2662359172aSGeorge Spelvin  * helper will ever be asked to convert is 1,125,520,955.
2672359172aSGeorge Spelvin  * (d1 in the put_dec code, assuming n is all-ones).
2682359172aSGeorge Spelvin  */
2692359172aSGeorge Spelvin static
2702359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x)
2712359172aSGeorge Spelvin {
2722359172aSGeorge Spelvin         uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
2732359172aSGeorge Spelvin 
2742359172aSGeorge Spelvin         put_dec_full4(buf, x - q * 10000);
2752359172aSGeorge Spelvin         return q;
276133fd9f5SDenys Vlasenko }
277133fd9f5SDenys Vlasenko 
278133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at
279133fd9f5SDenys Vlasenko  * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
280133fd9f5SDenys Vlasenko  * (with permission from the author).
281133fd9f5SDenys Vlasenko  * Performs no 64-bit division and hence should be fast on 32-bit machines.
282133fd9f5SDenys Vlasenko  */
283133fd9f5SDenys Vlasenko static
284133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
285133fd9f5SDenys Vlasenko {
286133fd9f5SDenys Vlasenko 	uint32_t d3, d2, d1, q, h;
287133fd9f5SDenys Vlasenko 
288133fd9f5SDenys Vlasenko 	if (n < 100*1000*1000)
289133fd9f5SDenys Vlasenko 		return put_dec_trunc8(buf, n);
290133fd9f5SDenys Vlasenko 
291133fd9f5SDenys Vlasenko 	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
292133fd9f5SDenys Vlasenko 	h   = (n >> 32);
293133fd9f5SDenys Vlasenko 	d2  = (h      ) & 0xffff;
294133fd9f5SDenys Vlasenko 	d3  = (h >> 16); /* implicit "& 0xffff" */
295133fd9f5SDenys Vlasenko 
296133fd9f5SDenys Vlasenko 	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
2972359172aSGeorge Spelvin 	q = put_dec_helper4(buf, q);
298133fd9f5SDenys Vlasenko 
2992359172aSGeorge Spelvin 	q += 7671 * d3 + 9496 * d2 + 6 * d1;
3002359172aSGeorge Spelvin 	q = put_dec_helper4(buf+4, q);
301133fd9f5SDenys Vlasenko 
3022359172aSGeorge Spelvin 	q += 4749 * d3 + 42 * d2;
3032359172aSGeorge Spelvin 	q = put_dec_helper4(buf+8, q);
304133fd9f5SDenys Vlasenko 
3052359172aSGeorge Spelvin 	q += 281 * d3;
3062359172aSGeorge Spelvin 	buf += 12;
3072359172aSGeorge Spelvin 	if (q)
3082359172aSGeorge Spelvin 		buf = put_dec_trunc8(buf, q);
3092359172aSGeorge Spelvin 	else while (buf[-1] == '0')
310133fd9f5SDenys Vlasenko 		--buf;
3117b9186f5SAndré Goddard Rosa 
3124277eeddSDenis Vlasenko 	return buf;
3134277eeddSDenis Vlasenko }
314133fd9f5SDenys Vlasenko 
315133fd9f5SDenys Vlasenko #endif
3164277eeddSDenis Vlasenko 
3171ac101a5SKAMEZAWA Hiroyuki /*
3181ac101a5SKAMEZAWA Hiroyuki  * Convert passed number to decimal string.
3191ac101a5SKAMEZAWA Hiroyuki  * Returns the length of string.  On buffer overflow, returns 0.
3201ac101a5SKAMEZAWA Hiroyuki  *
3211ac101a5SKAMEZAWA Hiroyuki  * If speed is not important, use snprintf(). It's easy to read the code.
3221ac101a5SKAMEZAWA Hiroyuki  */
3231ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num)
3241ac101a5SKAMEZAWA Hiroyuki {
325133fd9f5SDenys Vlasenko 	char tmp[sizeof(num) * 3];
3261ac101a5SKAMEZAWA Hiroyuki 	int idx, len;
3271ac101a5SKAMEZAWA Hiroyuki 
328133fd9f5SDenys Vlasenko 	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
329133fd9f5SDenys Vlasenko 	if (num <= 9) {
330133fd9f5SDenys Vlasenko 		tmp[0] = '0' + num;
331133fd9f5SDenys Vlasenko 		len = 1;
332133fd9f5SDenys Vlasenko 	} else {
3331ac101a5SKAMEZAWA Hiroyuki 		len = put_dec(tmp, num) - tmp;
334133fd9f5SDenys Vlasenko 	}
3351ac101a5SKAMEZAWA Hiroyuki 
3361ac101a5SKAMEZAWA Hiroyuki 	if (len > size)
3371ac101a5SKAMEZAWA Hiroyuki 		return 0;
3381ac101a5SKAMEZAWA Hiroyuki 	for (idx = 0; idx < len; ++idx)
3391ac101a5SKAMEZAWA Hiroyuki 		buf[idx] = tmp[len - idx - 1];
3401ac101a5SKAMEZAWA Hiroyuki 	return len;
3411ac101a5SKAMEZAWA Hiroyuki }
3421ac101a5SKAMEZAWA Hiroyuki 
3431da177e4SLinus Torvalds #define ZEROPAD	1		/* pad with zero */
3441da177e4SLinus Torvalds #define SIGN	2		/* unsigned/signed long */
3451da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3461da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
3471da177e4SLinus Torvalds #define LEFT	16		/* left justified */
348b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
349b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
3501da177e4SLinus Torvalds 
351fef20d9cSFrederic Weisbecker enum format_type {
352fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
353ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
354fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
355fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
356fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
357fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
358fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
359fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
360fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
361fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
362fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
363a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
364a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
365fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
366fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
367fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
368fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
369fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
370fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
371fef20d9cSFrederic Weisbecker };
372fef20d9cSFrederic Weisbecker 
373fef20d9cSFrederic Weisbecker struct printf_spec {
3744e310fdaSJoe Perches 	u8	type;		/* format_type enum */
375ef0658f3SJoe Perches 	u8	flags;		/* flags to number() */
3764e310fdaSJoe Perches 	u8	base;		/* number base, 8, 10 or 16 only */
3774e310fdaSJoe Perches 	u8	qualifier;	/* number qualifier, one of 'hHlLtzZ' */
3784e310fdaSJoe Perches 	s16	field_width;	/* width of output field */
3794e310fdaSJoe Perches 	s16	precision;	/* # of digits/chars */
380fef20d9cSFrederic Weisbecker };
381fef20d9cSFrederic Weisbecker 
382cf3b429bSJoe Perches static noinline_for_stack
383cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
384fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
3851da177e4SLinus Torvalds {
3869b706aeeSDenys Vlasenko 	/* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
3879b706aeeSDenys Vlasenko 	static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
3889b706aeeSDenys Vlasenko 
3899b706aeeSDenys Vlasenko 	char tmp[66];
3909b706aeeSDenys Vlasenko 	char sign;
3919b706aeeSDenys Vlasenko 	char locase;
392fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
3931da177e4SLinus Torvalds 	int i;
3947c203422SPierre Carrier 	bool is_zero = num == 0LL;
3951da177e4SLinus Torvalds 
3969b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
3979b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
398fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
399fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
400fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
4011da177e4SLinus Torvalds 	sign = 0;
402fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4031da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4041da177e4SLinus Torvalds 			sign = '-';
4051da177e4SLinus Torvalds 			num = -(signed long long)num;
406fef20d9cSFrederic Weisbecker 			spec.field_width--;
407fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4081da177e4SLinus Torvalds 			sign = '+';
409fef20d9cSFrederic Weisbecker 			spec.field_width--;
410fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4111da177e4SLinus Torvalds 			sign = ' ';
412fef20d9cSFrederic Weisbecker 			spec.field_width--;
4131da177e4SLinus Torvalds 		}
4141da177e4SLinus Torvalds 	}
415b39a7340SDenis Vlasenko 	if (need_pfx) {
416fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
4177c203422SPierre Carrier 			spec.field_width -= 2;
4187c203422SPierre Carrier 		else if (!is_zero)
419fef20d9cSFrederic Weisbecker 			spec.field_width--;
4201da177e4SLinus Torvalds 	}
421b39a7340SDenis Vlasenko 
422b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4231da177e4SLinus Torvalds 	i = 0;
424133fd9f5SDenys Vlasenko 	if (num < spec.base)
425133fd9f5SDenys Vlasenko 		tmp[i++] = digits[num] | locase;
4264277eeddSDenis Vlasenko 	/* Generic code, for any base:
4274277eeddSDenis Vlasenko 	else do {
4289b706aeeSDenys Vlasenko 		tmp[i++] = (digits[do_div(num,base)] | locase);
4294277eeddSDenis Vlasenko 	} while (num != 0);
4304277eeddSDenis Vlasenko 	*/
431fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
432fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
433b39a7340SDenis Vlasenko 		int shift = 3;
4347b9186f5SAndré Goddard Rosa 
4357b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4367b9186f5SAndré Goddard Rosa 			shift = 4;
437b39a7340SDenis Vlasenko 		do {
4389b706aeeSDenys Vlasenko 			tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
439b39a7340SDenis Vlasenko 			num >>= shift;
440b39a7340SDenis Vlasenko 		} while (num);
4414277eeddSDenis Vlasenko 	} else { /* base 10 */
4424277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4434277eeddSDenis Vlasenko 	}
444b39a7340SDenis Vlasenko 
445b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
446fef20d9cSFrederic Weisbecker 	if (i > spec.precision)
447fef20d9cSFrederic Weisbecker 		spec.precision = i;
448b39a7340SDenis Vlasenko 	/* leading space padding */
449fef20d9cSFrederic Weisbecker 	spec.field_width -= spec.precision;
450fef20d9cSFrederic Weisbecker 	if (!(spec.flags & (ZEROPAD+LEFT))) {
451fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
452f796937aSJeremy Fitzhardinge 			if (buf < end)
4531da177e4SLinus Torvalds 				*buf = ' ';
4541da177e4SLinus Torvalds 			++buf;
4551da177e4SLinus Torvalds 		}
4561da177e4SLinus Torvalds 	}
457b39a7340SDenis Vlasenko 	/* sign */
4581da177e4SLinus Torvalds 	if (sign) {
459f796937aSJeremy Fitzhardinge 		if (buf < end)
4601da177e4SLinus Torvalds 			*buf = sign;
4611da177e4SLinus Torvalds 		++buf;
4621da177e4SLinus Torvalds 	}
463b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
464b39a7340SDenis Vlasenko 	if (need_pfx) {
4657c203422SPierre Carrier 		if (spec.base == 16 || !is_zero) {
466f796937aSJeremy Fitzhardinge 			if (buf < end)
4671da177e4SLinus Torvalds 				*buf = '0';
4681da177e4SLinus Torvalds 			++buf;
4697c203422SPierre Carrier 		}
470fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
471f796937aSJeremy Fitzhardinge 			if (buf < end)
4729b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
4731da177e4SLinus Torvalds 			++buf;
4741da177e4SLinus Torvalds 		}
4751da177e4SLinus Torvalds 	}
476b39a7340SDenis Vlasenko 	/* zero or space padding */
477fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
478fef20d9cSFrederic Weisbecker 		char c = (spec.flags & ZEROPAD) ? '0' : ' ';
479fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
480f796937aSJeremy Fitzhardinge 			if (buf < end)
4811da177e4SLinus Torvalds 				*buf = c;
4821da177e4SLinus Torvalds 			++buf;
4831da177e4SLinus Torvalds 		}
4841da177e4SLinus Torvalds 	}
485b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
486fef20d9cSFrederic Weisbecker 	while (i <= --spec.precision) {
487f796937aSJeremy Fitzhardinge 		if (buf < end)
4881da177e4SLinus Torvalds 			*buf = '0';
4891da177e4SLinus Torvalds 		++buf;
4901da177e4SLinus Torvalds 	}
491b39a7340SDenis Vlasenko 	/* actual digits of result */
492b39a7340SDenis Vlasenko 	while (--i >= 0) {
493f796937aSJeremy Fitzhardinge 		if (buf < end)
4941da177e4SLinus Torvalds 			*buf = tmp[i];
4951da177e4SLinus Torvalds 		++buf;
4961da177e4SLinus Torvalds 	}
497b39a7340SDenis Vlasenko 	/* trailing space padding */
498fef20d9cSFrederic Weisbecker 	while (--spec.field_width >= 0) {
499f796937aSJeremy Fitzhardinge 		if (buf < end)
5001da177e4SLinus Torvalds 			*buf = ' ';
5011da177e4SLinus Torvalds 		++buf;
5021da177e4SLinus Torvalds 	}
5037b9186f5SAndré Goddard Rosa 
5041da177e4SLinus Torvalds 	return buf;
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
507cf3b429bSJoe Perches static noinline_for_stack
508cf3b429bSJoe Perches char *string(char *buf, char *end, const char *s, struct printf_spec spec)
5090f9bfa56SLinus Torvalds {
5100f9bfa56SLinus Torvalds 	int len, i;
5110f9bfa56SLinus Torvalds 
5120f9bfa56SLinus Torvalds 	if ((unsigned long)s < PAGE_SIZE)
5130f4f81dcSAndré Goddard Rosa 		s = "(null)";
5140f9bfa56SLinus Torvalds 
515fef20d9cSFrederic Weisbecker 	len = strnlen(s, spec.precision);
5160f9bfa56SLinus Torvalds 
517fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
518fef20d9cSFrederic Weisbecker 		while (len < spec.field_width--) {
5190f9bfa56SLinus Torvalds 			if (buf < end)
5200f9bfa56SLinus Torvalds 				*buf = ' ';
5210f9bfa56SLinus Torvalds 			++buf;
5220f9bfa56SLinus Torvalds 		}
5230f9bfa56SLinus Torvalds 	}
5240f9bfa56SLinus Torvalds 	for (i = 0; i < len; ++i) {
5250f9bfa56SLinus Torvalds 		if (buf < end)
5260f9bfa56SLinus Torvalds 			*buf = *s;
5270f9bfa56SLinus Torvalds 		++buf; ++s;
5280f9bfa56SLinus Torvalds 	}
529fef20d9cSFrederic Weisbecker 	while (len < spec.field_width--) {
5300f9bfa56SLinus Torvalds 		if (buf < end)
5310f9bfa56SLinus Torvalds 			*buf = ' ';
5320f9bfa56SLinus Torvalds 		++buf;
5330f9bfa56SLinus Torvalds 	}
5347b9186f5SAndré Goddard Rosa 
5350f9bfa56SLinus Torvalds 	return buf;
5360f9bfa56SLinus Torvalds }
5370f9bfa56SLinus Torvalds 
5384b6ccca7SAl Viro static void widen(char *buf, char *end, unsigned len, unsigned spaces)
5394b6ccca7SAl Viro {
5404b6ccca7SAl Viro 	size_t size;
5414b6ccca7SAl Viro 	if (buf >= end)	/* nowhere to put anything */
5424b6ccca7SAl Viro 		return;
5434b6ccca7SAl Viro 	size = end - buf;
5444b6ccca7SAl Viro 	if (size <= spaces) {
5454b6ccca7SAl Viro 		memset(buf, ' ', size);
5464b6ccca7SAl Viro 		return;
5474b6ccca7SAl Viro 	}
5484b6ccca7SAl Viro 	if (len) {
5494b6ccca7SAl Viro 		if (len > size - spaces)
5504b6ccca7SAl Viro 			len = size - spaces;
5514b6ccca7SAl Viro 		memmove(buf + spaces, buf, len);
5524b6ccca7SAl Viro 	}
5534b6ccca7SAl Viro 	memset(buf, ' ', spaces);
5544b6ccca7SAl Viro }
5554b6ccca7SAl Viro 
5564b6ccca7SAl Viro static noinline_for_stack
5574b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
5584b6ccca7SAl Viro 		  const char *fmt)
5594b6ccca7SAl Viro {
5604b6ccca7SAl Viro 	const char *array[4], *s;
5614b6ccca7SAl Viro 	const struct dentry *p;
5624b6ccca7SAl Viro 	int depth;
5634b6ccca7SAl Viro 	int i, n;
5644b6ccca7SAl Viro 
5654b6ccca7SAl Viro 	switch (fmt[1]) {
5664b6ccca7SAl Viro 		case '2': case '3': case '4':
5674b6ccca7SAl Viro 			depth = fmt[1] - '0';
5684b6ccca7SAl Viro 			break;
5694b6ccca7SAl Viro 		default:
5704b6ccca7SAl Viro 			depth = 1;
5714b6ccca7SAl Viro 	}
5724b6ccca7SAl Viro 
5734b6ccca7SAl Viro 	rcu_read_lock();
5744b6ccca7SAl Viro 	for (i = 0; i < depth; i++, d = p) {
5754b6ccca7SAl Viro 		p = ACCESS_ONCE(d->d_parent);
5764b6ccca7SAl Viro 		array[i] = ACCESS_ONCE(d->d_name.name);
5774b6ccca7SAl Viro 		if (p == d) {
5784b6ccca7SAl Viro 			if (i)
5794b6ccca7SAl Viro 				array[i] = "";
5804b6ccca7SAl Viro 			i++;
5814b6ccca7SAl Viro 			break;
5824b6ccca7SAl Viro 		}
5834b6ccca7SAl Viro 	}
5844b6ccca7SAl Viro 	s = array[--i];
5854b6ccca7SAl Viro 	for (n = 0; n != spec.precision; n++, buf++) {
5864b6ccca7SAl Viro 		char c = *s++;
5874b6ccca7SAl Viro 		if (!c) {
5884b6ccca7SAl Viro 			if (!i)
5894b6ccca7SAl Viro 				break;
5904b6ccca7SAl Viro 			c = '/';
5914b6ccca7SAl Viro 			s = array[--i];
5924b6ccca7SAl Viro 		}
5934b6ccca7SAl Viro 		if (buf < end)
5944b6ccca7SAl Viro 			*buf = c;
5954b6ccca7SAl Viro 	}
5964b6ccca7SAl Viro 	rcu_read_unlock();
5974b6ccca7SAl Viro 	if (n < spec.field_width) {
5984b6ccca7SAl Viro 		/* we want to pad the sucker */
5994b6ccca7SAl Viro 		unsigned spaces = spec.field_width - n;
6004b6ccca7SAl Viro 		if (!(spec.flags & LEFT)) {
6014b6ccca7SAl Viro 			widen(buf - n, end, n, spaces);
6024b6ccca7SAl Viro 			return buf + spaces;
6034b6ccca7SAl Viro 		}
6044b6ccca7SAl Viro 		while (spaces--) {
6054b6ccca7SAl Viro 			if (buf < end)
6064b6ccca7SAl Viro 				*buf = ' ';
6074b6ccca7SAl Viro 			++buf;
6084b6ccca7SAl Viro 		}
6094b6ccca7SAl Viro 	}
6104b6ccca7SAl Viro 	return buf;
6114b6ccca7SAl Viro }
6124b6ccca7SAl Viro 
613cf3b429bSJoe Perches static noinline_for_stack
614cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
615b0d33c2bSJoe Perches 		    struct printf_spec spec, const char *fmt)
6160fe1ef24SLinus Torvalds {
617b0d33c2bSJoe Perches 	unsigned long value;
6180fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
6190fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
620b0d33c2bSJoe Perches #endif
621b0d33c2bSJoe Perches 
622b0d33c2bSJoe Perches 	if (fmt[1] == 'R')
623b0d33c2bSJoe Perches 		ptr = __builtin_extract_return_addr(ptr);
624b0d33c2bSJoe Perches 	value = (unsigned long)ptr;
625b0d33c2bSJoe Perches 
626b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS
627b0d33c2bSJoe Perches 	if (*fmt == 'B')
6280f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
629b0d33c2bSJoe Perches 	else if (*fmt != 'f' && *fmt != 's')
6300fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
6310c8b946eSFrederic Weisbecker 	else
6324796dd20SStephen Boyd 		sprint_symbol_no_offset(sym, value);
6337b9186f5SAndré Goddard Rosa 
634fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
6350fe1ef24SLinus Torvalds #else
636fef20d9cSFrederic Weisbecker 	spec.field_width = 2 * sizeof(void *);
637fef20d9cSFrederic Weisbecker 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
638fef20d9cSFrederic Weisbecker 	spec.base = 16;
6397b9186f5SAndré Goddard Rosa 
640fef20d9cSFrederic Weisbecker 	return number(buf, end, value, spec);
6410fe1ef24SLinus Torvalds #endif
6420fe1ef24SLinus Torvalds }
6430fe1ef24SLinus Torvalds 
644cf3b429bSJoe Perches static noinline_for_stack
645cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
646fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
647332d2e78SLinus Torvalds {
648332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
64928405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
650332d2e78SLinus Torvalds #endif
651332d2e78SLinus Torvalds 
652332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
65328405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
654332d2e78SLinus Torvalds #endif
6554da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
656fef20d9cSFrederic Weisbecker 		.base = 16,
6574da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
658fef20d9cSFrederic Weisbecker 		.precision = -1,
659fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
660fef20d9cSFrederic Weisbecker 	};
6614da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
6624da0b66cSBjorn Helgaas 		.base = 16,
6634da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
6644da0b66cSBjorn Helgaas 		.precision = -1,
6654da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
6664da0b66cSBjorn Helgaas 	};
6670f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
6680f4050c7SBjorn Helgaas 		.base = 16,
6690f4050c7SBjorn Helgaas 		.field_width = 2,
6700f4050c7SBjorn Helgaas 		.precision = -1,
6710f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
6720f4050c7SBjorn Helgaas 	};
6734da0b66cSBjorn Helgaas 	static const struct printf_spec dec_spec = {
674c91d3376SBjorn Helgaas 		.base = 10,
675c91d3376SBjorn Helgaas 		.precision = -1,
676c91d3376SBjorn Helgaas 		.flags = 0,
677c91d3376SBjorn Helgaas 	};
6784da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
679fd95541eSBjorn Helgaas 		.field_width = -1,
680fd95541eSBjorn Helgaas 		.precision = 10,
681fd95541eSBjorn Helgaas 		.flags = LEFT,
682fd95541eSBjorn Helgaas 	};
6834da0b66cSBjorn Helgaas 	static const struct printf_spec flag_spec = {
684fd95541eSBjorn Helgaas 		.base = 16,
685fd95541eSBjorn Helgaas 		.precision = -1,
686fd95541eSBjorn Helgaas 		.flags = SPECIAL | SMALL,
687fd95541eSBjorn Helgaas 	};
688c7dabef8SBjorn Helgaas 
689c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
690c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
691c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
692c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
6939d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
694c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
695c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
696c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
697c7dabef8SBjorn Helgaas 
698332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
699c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
7004da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
701332d2e78SLinus Torvalds 
702332d2e78SLinus Torvalds 	*p++ = '[';
7034da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
704fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
7054da0b66cSBjorn Helgaas 		specp = &io_spec;
7064da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
707fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
7084da0b66cSBjorn Helgaas 		specp = &mem_spec;
7094da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
710fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
7114da0b66cSBjorn Helgaas 		specp = &dec_spec;
7124da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
713fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
7144da0b66cSBjorn Helgaas 		specp = &dec_spec;
7150f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
7160f4050c7SBjorn Helgaas 		p = string(p, pend, "bus ", str_spec);
7170f4050c7SBjorn Helgaas 		specp = &bus_spec;
7184da0b66cSBjorn Helgaas 	} else {
719c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
7204da0b66cSBjorn Helgaas 		specp = &mem_spec;
721c7dabef8SBjorn Helgaas 		decode = 0;
722fd95541eSBjorn Helgaas 	}
723d19cb803SBjorn Helgaas 	if (decode && res->flags & IORESOURCE_UNSET) {
724d19cb803SBjorn Helgaas 		p = string(p, pend, "size ", str_spec);
725d19cb803SBjorn Helgaas 		p = number(p, pend, resource_size(res), *specp);
726d19cb803SBjorn Helgaas 	} else {
7274da0b66cSBjorn Helgaas 		p = number(p, pend, res->start, *specp);
728c91d3376SBjorn Helgaas 		if (res->start != res->end) {
729332d2e78SLinus Torvalds 			*p++ = '-';
7304da0b66cSBjorn Helgaas 			p = number(p, pend, res->end, *specp);
731c91d3376SBjorn Helgaas 		}
732d19cb803SBjorn Helgaas 	}
733c7dabef8SBjorn Helgaas 	if (decode) {
734fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
735fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
736fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
737fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
7389d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
7399d7cca04SBjorn Helgaas 			p = string(p, pend, " window", str_spec);
740fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
741fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
742c7dabef8SBjorn Helgaas 	} else {
743fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
744c7dabef8SBjorn Helgaas 		p = number(p, pend, res->flags, flag_spec);
745fd95541eSBjorn Helgaas 	}
746332d2e78SLinus Torvalds 	*p++ = ']';
747c7dabef8SBjorn Helgaas 	*p = '\0';
748332d2e78SLinus Torvalds 
749fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
750332d2e78SLinus Torvalds }
751332d2e78SLinus Torvalds 
752cf3b429bSJoe Perches static noinline_for_stack
75331550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
75431550a16SAndy Shevchenko 		 const char *fmt)
75531550a16SAndy Shevchenko {
756360603a1SSteven Rostedt 	int i, len = 1;		/* if we pass '%ph[CDN]', field width remains
75731550a16SAndy Shevchenko 				   negative value, fallback to the default */
75831550a16SAndy Shevchenko 	char separator;
75931550a16SAndy Shevchenko 
76031550a16SAndy Shevchenko 	if (spec.field_width == 0)
76131550a16SAndy Shevchenko 		/* nothing to print */
76231550a16SAndy Shevchenko 		return buf;
76331550a16SAndy Shevchenko 
76431550a16SAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
76531550a16SAndy Shevchenko 		/* NULL pointer */
76631550a16SAndy Shevchenko 		return string(buf, end, NULL, spec);
76731550a16SAndy Shevchenko 
76831550a16SAndy Shevchenko 	switch (fmt[1]) {
76931550a16SAndy Shevchenko 	case 'C':
77031550a16SAndy Shevchenko 		separator = ':';
77131550a16SAndy Shevchenko 		break;
77231550a16SAndy Shevchenko 	case 'D':
77331550a16SAndy Shevchenko 		separator = '-';
77431550a16SAndy Shevchenko 		break;
77531550a16SAndy Shevchenko 	case 'N':
77631550a16SAndy Shevchenko 		separator = 0;
77731550a16SAndy Shevchenko 		break;
77831550a16SAndy Shevchenko 	default:
77931550a16SAndy Shevchenko 		separator = ' ';
78031550a16SAndy Shevchenko 		break;
78131550a16SAndy Shevchenko 	}
78231550a16SAndy Shevchenko 
78331550a16SAndy Shevchenko 	if (spec.field_width > 0)
78431550a16SAndy Shevchenko 		len = min_t(int, spec.field_width, 64);
78531550a16SAndy Shevchenko 
78631550a16SAndy Shevchenko 	for (i = 0; i < len && buf < end - 1; i++) {
78731550a16SAndy Shevchenko 		buf = hex_byte_pack(buf, addr[i]);
78831550a16SAndy Shevchenko 
78931550a16SAndy Shevchenko 		if (buf < end && separator && i != len - 1)
79031550a16SAndy Shevchenko 			*buf++ = separator;
79131550a16SAndy Shevchenko 	}
79231550a16SAndy Shevchenko 
79331550a16SAndy Shevchenko 	return buf;
79431550a16SAndy Shevchenko }
79531550a16SAndy Shevchenko 
79631550a16SAndy Shevchenko static noinline_for_stack
797*dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
798*dbc760bcSTejun Heo 		    struct printf_spec spec, const char *fmt)
799*dbc760bcSTejun Heo {
800*dbc760bcSTejun Heo 	const int CHUNKSZ = 32;
801*dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
802*dbc760bcSTejun Heo 	int i, chunksz;
803*dbc760bcSTejun Heo 	bool first = true;
804*dbc760bcSTejun Heo 
805*dbc760bcSTejun Heo 	/* reused to print numbers */
806*dbc760bcSTejun Heo 	spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
807*dbc760bcSTejun Heo 
808*dbc760bcSTejun Heo 	chunksz = nr_bits & (CHUNKSZ - 1);
809*dbc760bcSTejun Heo 	if (chunksz == 0)
810*dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
811*dbc760bcSTejun Heo 
812*dbc760bcSTejun Heo 	i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
813*dbc760bcSTejun Heo 	for (; i >= 0; i -= CHUNKSZ) {
814*dbc760bcSTejun Heo 		u32 chunkmask, val;
815*dbc760bcSTejun Heo 		int word, bit;
816*dbc760bcSTejun Heo 
817*dbc760bcSTejun Heo 		chunkmask = ((1ULL << chunksz) - 1);
818*dbc760bcSTejun Heo 		word = i / BITS_PER_LONG;
819*dbc760bcSTejun Heo 		bit = i % BITS_PER_LONG;
820*dbc760bcSTejun Heo 		val = (bitmap[word] >> bit) & chunkmask;
821*dbc760bcSTejun Heo 
822*dbc760bcSTejun Heo 		if (!first) {
823*dbc760bcSTejun Heo 			if (buf < end)
824*dbc760bcSTejun Heo 				*buf = ',';
825*dbc760bcSTejun Heo 			buf++;
826*dbc760bcSTejun Heo 		}
827*dbc760bcSTejun Heo 		first = false;
828*dbc760bcSTejun Heo 
829*dbc760bcSTejun Heo 		spec.field_width = DIV_ROUND_UP(chunksz, 4);
830*dbc760bcSTejun Heo 		buf = number(buf, end, val, spec);
831*dbc760bcSTejun Heo 
832*dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
833*dbc760bcSTejun Heo 	}
834*dbc760bcSTejun Heo 	return buf;
835*dbc760bcSTejun Heo }
836*dbc760bcSTejun Heo 
837*dbc760bcSTejun Heo static noinline_for_stack
838*dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
839*dbc760bcSTejun Heo 			 struct printf_spec spec, const char *fmt)
840*dbc760bcSTejun Heo {
841*dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
842*dbc760bcSTejun Heo 	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
843*dbc760bcSTejun Heo 	int cur, rbot, rtop;
844*dbc760bcSTejun Heo 	bool first = true;
845*dbc760bcSTejun Heo 
846*dbc760bcSTejun Heo 	/* reused to print numbers */
847*dbc760bcSTejun Heo 	spec = (struct printf_spec){ .base = 10 };
848*dbc760bcSTejun Heo 
849*dbc760bcSTejun Heo 	rbot = cur = find_first_bit(bitmap, nr_bits);
850*dbc760bcSTejun Heo 	while (cur < nr_bits) {
851*dbc760bcSTejun Heo 		rtop = cur;
852*dbc760bcSTejun Heo 		cur = find_next_bit(bitmap, nr_bits, cur + 1);
853*dbc760bcSTejun Heo 		if (cur < nr_bits && cur <= rtop + 1)
854*dbc760bcSTejun Heo 			continue;
855*dbc760bcSTejun Heo 
856*dbc760bcSTejun Heo 		if (!first) {
857*dbc760bcSTejun Heo 			if (buf < end)
858*dbc760bcSTejun Heo 				*buf = ',';
859*dbc760bcSTejun Heo 			buf++;
860*dbc760bcSTejun Heo 		}
861*dbc760bcSTejun Heo 		first = false;
862*dbc760bcSTejun Heo 
863*dbc760bcSTejun Heo 		buf = number(buf, end, rbot, spec);
864*dbc760bcSTejun Heo 		if (rbot < rtop) {
865*dbc760bcSTejun Heo 			if (buf < end)
866*dbc760bcSTejun Heo 				*buf = '-';
867*dbc760bcSTejun Heo 			buf++;
868*dbc760bcSTejun Heo 
869*dbc760bcSTejun Heo 			buf = number(buf, end, rtop, spec);
870*dbc760bcSTejun Heo 		}
871*dbc760bcSTejun Heo 
872*dbc760bcSTejun Heo 		rbot = cur;
873*dbc760bcSTejun Heo 	}
874*dbc760bcSTejun Heo 	return buf;
875*dbc760bcSTejun Heo }
876*dbc760bcSTejun Heo 
877*dbc760bcSTejun Heo static noinline_for_stack
878cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
8798a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
880dd45c9cfSHarvey Harrison {
8818a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
882dd45c9cfSHarvey Harrison 	char *p = mac_addr;
883dd45c9cfSHarvey Harrison 	int i;
884bc7259a2SJoe Perches 	char separator;
88576597ff9SAndrei Emeltchenko 	bool reversed = false;
886bc7259a2SJoe Perches 
88776597ff9SAndrei Emeltchenko 	switch (fmt[1]) {
88876597ff9SAndrei Emeltchenko 	case 'F':
889bc7259a2SJoe Perches 		separator = '-';
89076597ff9SAndrei Emeltchenko 		break;
89176597ff9SAndrei Emeltchenko 
89276597ff9SAndrei Emeltchenko 	case 'R':
89376597ff9SAndrei Emeltchenko 		reversed = true;
89476597ff9SAndrei Emeltchenko 		/* fall through */
89576597ff9SAndrei Emeltchenko 
89676597ff9SAndrei Emeltchenko 	default:
897bc7259a2SJoe Perches 		separator = ':';
89876597ff9SAndrei Emeltchenko 		break;
899bc7259a2SJoe Perches 	}
900dd45c9cfSHarvey Harrison 
901dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
90276597ff9SAndrei Emeltchenko 		if (reversed)
90376597ff9SAndrei Emeltchenko 			p = hex_byte_pack(p, addr[5 - i]);
90476597ff9SAndrei Emeltchenko 		else
90555036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[i]);
90676597ff9SAndrei Emeltchenko 
9078a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
908bc7259a2SJoe Perches 			*p++ = separator;
909dd45c9cfSHarvey Harrison 	}
910dd45c9cfSHarvey Harrison 	*p = '\0';
911dd45c9cfSHarvey Harrison 
912fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
913dd45c9cfSHarvey Harrison }
914dd45c9cfSHarvey Harrison 
915cf3b429bSJoe Perches static noinline_for_stack
916cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
917689afa7dSHarvey Harrison {
918689afa7dSHarvey Harrison 	int i;
9190159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
9200159f24eSJoe Perches 	int index;
9210159f24eSJoe Perches 	int step;
922689afa7dSHarvey Harrison 
9230159f24eSJoe Perches 	switch (fmt[2]) {
9240159f24eSJoe Perches 	case 'h':
9250159f24eSJoe Perches #ifdef __BIG_ENDIAN
9260159f24eSJoe Perches 		index = 0;
9270159f24eSJoe Perches 		step = 1;
9280159f24eSJoe Perches #else
9290159f24eSJoe Perches 		index = 3;
9300159f24eSJoe Perches 		step = -1;
9310159f24eSJoe Perches #endif
9320159f24eSJoe Perches 		break;
9330159f24eSJoe Perches 	case 'l':
9340159f24eSJoe Perches 		index = 3;
9350159f24eSJoe Perches 		step = -1;
9360159f24eSJoe Perches 		break;
9370159f24eSJoe Perches 	case 'n':
9380159f24eSJoe Perches 	case 'b':
9390159f24eSJoe Perches 	default:
9400159f24eSJoe Perches 		index = 0;
9410159f24eSJoe Perches 		step = 1;
9420159f24eSJoe Perches 		break;
9430159f24eSJoe Perches 	}
9448a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
9458a27f7c9SJoe Perches 		char temp[3];	/* hold each IP quad in reverse order */
946133fd9f5SDenys Vlasenko 		int digits = put_dec_trunc8(temp, addr[index]) - temp;
9478a27f7c9SJoe Perches 		if (leading_zeros) {
9488a27f7c9SJoe Perches 			if (digits < 3)
9498a27f7c9SJoe Perches 				*p++ = '0';
9508a27f7c9SJoe Perches 			if (digits < 2)
9518a27f7c9SJoe Perches 				*p++ = '0';
9528a27f7c9SJoe Perches 		}
9538a27f7c9SJoe Perches 		/* reverse the digits in the quad */
9548a27f7c9SJoe Perches 		while (digits--)
9558a27f7c9SJoe Perches 			*p++ = temp[digits];
9568a27f7c9SJoe Perches 		if (i < 3)
9578a27f7c9SJoe Perches 			*p++ = '.';
9580159f24eSJoe Perches 		index += step;
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_compressed_string(char *p, const char *addr)
9678a27f7c9SJoe Perches {
9687b9186f5SAndré Goddard Rosa 	int i, j, range;
9698a27f7c9SJoe Perches 	unsigned char zerolength[8];
9708a27f7c9SJoe Perches 	int longest = 1;
9718a27f7c9SJoe Perches 	int colonpos = -1;
9728a27f7c9SJoe Perches 	u16 word;
9737b9186f5SAndré Goddard Rosa 	u8 hi, lo;
9748a27f7c9SJoe Perches 	bool needcolon = false;
975eb78cd26SJoe Perches 	bool useIPv4;
976eb78cd26SJoe Perches 	struct in6_addr in6;
977eb78cd26SJoe Perches 
978eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
979eb78cd26SJoe Perches 
980eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
9818a27f7c9SJoe Perches 
9828a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
9838a27f7c9SJoe Perches 
9848a27f7c9SJoe Perches 	if (useIPv4)
9858a27f7c9SJoe Perches 		range = 6;
9868a27f7c9SJoe Perches 	else
9878a27f7c9SJoe Perches 		range = 8;
9888a27f7c9SJoe Perches 
9898a27f7c9SJoe Perches 	/* find position of longest 0 run */
9908a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
9918a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
992eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
9938a27f7c9SJoe Perches 				break;
9948a27f7c9SJoe Perches 			zerolength[i]++;
9958a27f7c9SJoe Perches 		}
9968a27f7c9SJoe Perches 	}
9978a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
9988a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
9998a27f7c9SJoe Perches 			longest = zerolength[i];
10008a27f7c9SJoe Perches 			colonpos = i;
10018a27f7c9SJoe Perches 		}
10028a27f7c9SJoe Perches 	}
100329cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
100429cf519eSJoe Perches 		colonpos = -1;
10058a27f7c9SJoe Perches 
10068a27f7c9SJoe Perches 	/* emit address */
10078a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10088a27f7c9SJoe Perches 		if (i == colonpos) {
10098a27f7c9SJoe Perches 			if (needcolon || i == 0)
10108a27f7c9SJoe Perches 				*p++ = ':';
10118a27f7c9SJoe Perches 			*p++ = ':';
10128a27f7c9SJoe Perches 			needcolon = false;
10138a27f7c9SJoe Perches 			i += longest - 1;
10148a27f7c9SJoe Perches 			continue;
10158a27f7c9SJoe Perches 		}
10168a27f7c9SJoe Perches 		if (needcolon) {
10178a27f7c9SJoe Perches 			*p++ = ':';
10188a27f7c9SJoe Perches 			needcolon = false;
10198a27f7c9SJoe Perches 		}
10208a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
1021eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
10228a27f7c9SJoe Perches 		hi = word >> 8;
10238a27f7c9SJoe Perches 		lo = word & 0xff;
10248a27f7c9SJoe Perches 		if (hi) {
10258a27f7c9SJoe Perches 			if (hi > 0x0f)
102655036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
10278a27f7c9SJoe Perches 			else
10288a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
102955036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
10308a27f7c9SJoe Perches 		}
1031b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
103255036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
10338a27f7c9SJoe Perches 		else
10348a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
10358a27f7c9SJoe Perches 		needcolon = true;
10368a27f7c9SJoe Perches 	}
10378a27f7c9SJoe Perches 
10388a27f7c9SJoe Perches 	if (useIPv4) {
10398a27f7c9SJoe Perches 		if (needcolon)
10408a27f7c9SJoe Perches 			*p++ = ':';
10410159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
10428a27f7c9SJoe Perches 	}
10438a27f7c9SJoe Perches 	*p = '\0';
10447b9186f5SAndré Goddard Rosa 
10458a27f7c9SJoe Perches 	return p;
10468a27f7c9SJoe Perches }
10478a27f7c9SJoe Perches 
1048cf3b429bSJoe Perches static noinline_for_stack
1049cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
10508a27f7c9SJoe Perches {
10518a27f7c9SJoe Perches 	int i;
10527b9186f5SAndré Goddard Rosa 
1053689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
105455036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
105555036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
10568a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
1057689afa7dSHarvey Harrison 			*p++ = ':';
1058689afa7dSHarvey Harrison 	}
1059689afa7dSHarvey Harrison 	*p = '\0';
10607b9186f5SAndré Goddard Rosa 
10618a27f7c9SJoe Perches 	return p;
10628a27f7c9SJoe Perches }
10638a27f7c9SJoe Perches 
1064cf3b429bSJoe Perches static noinline_for_stack
1065cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
10668a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
10678a27f7c9SJoe Perches {
10688a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
10698a27f7c9SJoe Perches 
10708a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
1071eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
10728a27f7c9SJoe Perches 	else
1073eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
1074689afa7dSHarvey Harrison 
1075fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
1076689afa7dSHarvey Harrison }
1077689afa7dSHarvey Harrison 
1078cf3b429bSJoe Perches static noinline_for_stack
1079cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
10808a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
10814aa99606SHarvey Harrison {
10828a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
10834aa99606SHarvey Harrison 
10840159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
10854aa99606SHarvey Harrison 
1086fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
10874aa99606SHarvey Harrison }
10884aa99606SHarvey Harrison 
1089cf3b429bSJoe Perches static noinline_for_stack
109010679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
109110679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
109210679643SDaniel Borkmann {
109310679643SDaniel Borkmann 	bool have_p = false, have_s = false, have_f = false, have_c = false;
109410679643SDaniel Borkmann 	char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
109510679643SDaniel Borkmann 		      sizeof(":12345") + sizeof("/123456789") +
109610679643SDaniel Borkmann 		      sizeof("%1234567890")];
109710679643SDaniel Borkmann 	char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
109810679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin6_addr;
109910679643SDaniel Borkmann 	char fmt6[2] = { fmt[0], '6' };
110010679643SDaniel Borkmann 	u8 off = 0;
110110679643SDaniel Borkmann 
110210679643SDaniel Borkmann 	fmt++;
110310679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
110410679643SDaniel Borkmann 		switch (*fmt) {
110510679643SDaniel Borkmann 		case 'p':
110610679643SDaniel Borkmann 			have_p = true;
110710679643SDaniel Borkmann 			break;
110810679643SDaniel Borkmann 		case 'f':
110910679643SDaniel Borkmann 			have_f = true;
111010679643SDaniel Borkmann 			break;
111110679643SDaniel Borkmann 		case 's':
111210679643SDaniel Borkmann 			have_s = true;
111310679643SDaniel Borkmann 			break;
111410679643SDaniel Borkmann 		case 'c':
111510679643SDaniel Borkmann 			have_c = true;
111610679643SDaniel Borkmann 			break;
111710679643SDaniel Borkmann 		}
111810679643SDaniel Borkmann 	}
111910679643SDaniel Borkmann 
112010679643SDaniel Borkmann 	if (have_p || have_s || have_f) {
112110679643SDaniel Borkmann 		*p = '[';
112210679643SDaniel Borkmann 		off = 1;
112310679643SDaniel Borkmann 	}
112410679643SDaniel Borkmann 
112510679643SDaniel Borkmann 	if (fmt6[0] == 'I' && have_c)
112610679643SDaniel Borkmann 		p = ip6_compressed_string(ip6_addr + off, addr);
112710679643SDaniel Borkmann 	else
112810679643SDaniel Borkmann 		p = ip6_string(ip6_addr + off, addr, fmt6);
112910679643SDaniel Borkmann 
113010679643SDaniel Borkmann 	if (have_p || have_s || have_f)
113110679643SDaniel Borkmann 		*p++ = ']';
113210679643SDaniel Borkmann 
113310679643SDaniel Borkmann 	if (have_p) {
113410679643SDaniel Borkmann 		*p++ = ':';
113510679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin6_port), spec);
113610679643SDaniel Borkmann 	}
113710679643SDaniel Borkmann 	if (have_f) {
113810679643SDaniel Borkmann 		*p++ = '/';
113910679643SDaniel Borkmann 		p = number(p, pend, ntohl(sa->sin6_flowinfo &
114010679643SDaniel Borkmann 					  IPV6_FLOWINFO_MASK), spec);
114110679643SDaniel Borkmann 	}
114210679643SDaniel Borkmann 	if (have_s) {
114310679643SDaniel Borkmann 		*p++ = '%';
114410679643SDaniel Borkmann 		p = number(p, pend, sa->sin6_scope_id, spec);
114510679643SDaniel Borkmann 	}
114610679643SDaniel Borkmann 	*p = '\0';
114710679643SDaniel Borkmann 
114810679643SDaniel Borkmann 	return string(buf, end, ip6_addr, spec);
114910679643SDaniel Borkmann }
115010679643SDaniel Borkmann 
115110679643SDaniel Borkmann static noinline_for_stack
115210679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
115310679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
115410679643SDaniel Borkmann {
115510679643SDaniel Borkmann 	bool have_p = false;
115610679643SDaniel Borkmann 	char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
115710679643SDaniel Borkmann 	char *pend = ip4_addr + sizeof(ip4_addr);
115810679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
115910679643SDaniel Borkmann 	char fmt4[3] = { fmt[0], '4', 0 };
116010679643SDaniel Borkmann 
116110679643SDaniel Borkmann 	fmt++;
116210679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
116310679643SDaniel Borkmann 		switch (*fmt) {
116410679643SDaniel Borkmann 		case 'p':
116510679643SDaniel Borkmann 			have_p = true;
116610679643SDaniel Borkmann 			break;
116710679643SDaniel Borkmann 		case 'h':
116810679643SDaniel Borkmann 		case 'l':
116910679643SDaniel Borkmann 		case 'n':
117010679643SDaniel Borkmann 		case 'b':
117110679643SDaniel Borkmann 			fmt4[2] = *fmt;
117210679643SDaniel Borkmann 			break;
117310679643SDaniel Borkmann 		}
117410679643SDaniel Borkmann 	}
117510679643SDaniel Borkmann 
117610679643SDaniel Borkmann 	p = ip4_string(ip4_addr, addr, fmt4);
117710679643SDaniel Borkmann 	if (have_p) {
117810679643SDaniel Borkmann 		*p++ = ':';
117910679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin_port), spec);
118010679643SDaniel Borkmann 	}
118110679643SDaniel Borkmann 	*p = '\0';
118210679643SDaniel Borkmann 
118310679643SDaniel Borkmann 	return string(buf, end, ip4_addr, spec);
118410679643SDaniel Borkmann }
118510679643SDaniel Borkmann 
118610679643SDaniel Borkmann static noinline_for_stack
118771dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
118871dca95dSAndy Shevchenko 		     const char *fmt)
118971dca95dSAndy Shevchenko {
119071dca95dSAndy Shevchenko 	bool found = true;
119171dca95dSAndy Shevchenko 	int count = 1;
119271dca95dSAndy Shevchenko 	unsigned int flags = 0;
119371dca95dSAndy Shevchenko 	int len;
119471dca95dSAndy Shevchenko 
119571dca95dSAndy Shevchenko 	if (spec.field_width == 0)
119671dca95dSAndy Shevchenko 		return buf;				/* nothing to print */
119771dca95dSAndy Shevchenko 
119871dca95dSAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
119971dca95dSAndy Shevchenko 		return string(buf, end, NULL, spec);	/* NULL pointer */
120071dca95dSAndy Shevchenko 
120171dca95dSAndy Shevchenko 
120271dca95dSAndy Shevchenko 	do {
120371dca95dSAndy Shevchenko 		switch (fmt[count++]) {
120471dca95dSAndy Shevchenko 		case 'a':
120571dca95dSAndy Shevchenko 			flags |= ESCAPE_ANY;
120671dca95dSAndy Shevchenko 			break;
120771dca95dSAndy Shevchenko 		case 'c':
120871dca95dSAndy Shevchenko 			flags |= ESCAPE_SPECIAL;
120971dca95dSAndy Shevchenko 			break;
121071dca95dSAndy Shevchenko 		case 'h':
121171dca95dSAndy Shevchenko 			flags |= ESCAPE_HEX;
121271dca95dSAndy Shevchenko 			break;
121371dca95dSAndy Shevchenko 		case 'n':
121471dca95dSAndy Shevchenko 			flags |= ESCAPE_NULL;
121571dca95dSAndy Shevchenko 			break;
121671dca95dSAndy Shevchenko 		case 'o':
121771dca95dSAndy Shevchenko 			flags |= ESCAPE_OCTAL;
121871dca95dSAndy Shevchenko 			break;
121971dca95dSAndy Shevchenko 		case 'p':
122071dca95dSAndy Shevchenko 			flags |= ESCAPE_NP;
122171dca95dSAndy Shevchenko 			break;
122271dca95dSAndy Shevchenko 		case 's':
122371dca95dSAndy Shevchenko 			flags |= ESCAPE_SPACE;
122471dca95dSAndy Shevchenko 			break;
122571dca95dSAndy Shevchenko 		default:
122671dca95dSAndy Shevchenko 			found = false;
122771dca95dSAndy Shevchenko 			break;
122871dca95dSAndy Shevchenko 		}
122971dca95dSAndy Shevchenko 	} while (found);
123071dca95dSAndy Shevchenko 
123171dca95dSAndy Shevchenko 	if (!flags)
123271dca95dSAndy Shevchenko 		flags = ESCAPE_ANY_NP;
123371dca95dSAndy Shevchenko 
123471dca95dSAndy Shevchenko 	len = spec.field_width < 0 ? 1 : spec.field_width;
123571dca95dSAndy Shevchenko 
123671dca95dSAndy Shevchenko 	/* Ignore the error. We print as many characters as we can */
123771dca95dSAndy Shevchenko 	string_escape_mem(addr, len, &buf, end - buf, flags, NULL);
123871dca95dSAndy Shevchenko 
123971dca95dSAndy Shevchenko 	return buf;
124071dca95dSAndy Shevchenko }
124171dca95dSAndy Shevchenko 
124271dca95dSAndy Shevchenko static noinline_for_stack
1243cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
12449ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
12459ac6e44eSJoe Perches {
12469ac6e44eSJoe Perches 	char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
12479ac6e44eSJoe Perches 	char *p = uuid;
12489ac6e44eSJoe Perches 	int i;
12499ac6e44eSJoe Perches 	static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
12509ac6e44eSJoe Perches 	static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
12519ac6e44eSJoe Perches 	const u8 *index = be;
12529ac6e44eSJoe Perches 	bool uc = false;
12539ac6e44eSJoe Perches 
12549ac6e44eSJoe Perches 	switch (*(++fmt)) {
12559ac6e44eSJoe Perches 	case 'L':
12569ac6e44eSJoe Perches 		uc = true;		/* fall-through */
12579ac6e44eSJoe Perches 	case 'l':
12589ac6e44eSJoe Perches 		index = le;
12599ac6e44eSJoe Perches 		break;
12609ac6e44eSJoe Perches 	case 'B':
12619ac6e44eSJoe Perches 		uc = true;
12629ac6e44eSJoe Perches 		break;
12639ac6e44eSJoe Perches 	}
12649ac6e44eSJoe Perches 
12659ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
126655036ba7SAndy Shevchenko 		p = hex_byte_pack(p, addr[index[i]]);
12679ac6e44eSJoe Perches 		switch (i) {
12689ac6e44eSJoe Perches 		case 3:
12699ac6e44eSJoe Perches 		case 5:
12709ac6e44eSJoe Perches 		case 7:
12719ac6e44eSJoe Perches 		case 9:
12729ac6e44eSJoe Perches 			*p++ = '-';
12739ac6e44eSJoe Perches 			break;
12749ac6e44eSJoe Perches 		}
12759ac6e44eSJoe Perches 	}
12769ac6e44eSJoe Perches 
12779ac6e44eSJoe Perches 	*p = 0;
12789ac6e44eSJoe Perches 
12799ac6e44eSJoe Perches 	if (uc) {
12809ac6e44eSJoe Perches 		p = uuid;
12819ac6e44eSJoe Perches 		do {
12829ac6e44eSJoe Perches 			*p = toupper(*p);
12839ac6e44eSJoe Perches 		} while (*(++p));
12849ac6e44eSJoe Perches 	}
12859ac6e44eSJoe Perches 
12869ac6e44eSJoe Perches 	return string(buf, end, uuid, spec);
12879ac6e44eSJoe Perches }
12889ac6e44eSJoe Perches 
1289c8f44affSMichał Mirosław static
1290c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1291c8f44affSMichał Mirosław 		      struct printf_spec spec)
1292c8f44affSMichał Mirosław {
1293c8f44affSMichał Mirosław 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
1294c8f44affSMichał Mirosław 	if (spec.field_width == -1)
1295c8f44affSMichał Mirosław 		spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1296c8f44affSMichał Mirosław 	spec.base = 16;
1297c8f44affSMichał Mirosław 
1298c8f44affSMichał Mirosław 	return number(buf, end, *(const netdev_features_t *)addr, spec);
1299c8f44affSMichał Mirosław }
1300c8f44affSMichał Mirosław 
1301aaf07621SJoe Perches static noinline_for_stack
1302aaf07621SJoe Perches char *address_val(char *buf, char *end, const void *addr,
1303aaf07621SJoe Perches 		  struct printf_spec spec, const char *fmt)
1304aaf07621SJoe Perches {
1305aaf07621SJoe Perches 	unsigned long long num;
1306aaf07621SJoe Perches 
1307aaf07621SJoe Perches 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
1308aaf07621SJoe Perches 	spec.base = 16;
1309aaf07621SJoe Perches 
1310aaf07621SJoe Perches 	switch (fmt[1]) {
1311aaf07621SJoe Perches 	case 'd':
1312aaf07621SJoe Perches 		num = *(const dma_addr_t *)addr;
1313aaf07621SJoe Perches 		spec.field_width = sizeof(dma_addr_t) * 2 + 2;
1314aaf07621SJoe Perches 		break;
1315aaf07621SJoe Perches 	case 'p':
1316aaf07621SJoe Perches 	default:
1317aaf07621SJoe Perches 		num = *(const phys_addr_t *)addr;
1318aaf07621SJoe Perches 		spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1319aaf07621SJoe Perches 		break;
1320aaf07621SJoe Perches 	}
1321aaf07621SJoe Perches 
1322aaf07621SJoe Perches 	return number(buf, end, num, spec);
1323aaf07621SJoe Perches }
1324aaf07621SJoe Perches 
1325411f05f1SIngo Molnar int kptr_restrict __read_mostly;
1326455cd5abSDan Rosenberg 
13274d8a743cSLinus Torvalds /*
13284d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
13294d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
13304d8a743cSLinus Torvalds  * specifiers.
13314d8a743cSLinus Torvalds  *
1332332d2e78SLinus Torvalds  * Right now we handle:
13330fe1ef24SLinus Torvalds  *
13340c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
13350c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
13360efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
13370efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
1338b0d33c2bSJoe Perches  * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
13390f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
1340c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1341c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1342*dbc760bcSTejun Heo  * - 'b[l]' For a bitmap, the number of bits is determined by the field
1343*dbc760bcSTejun Heo  *       width which must be explicitly specified either as part of the
1344*dbc760bcSTejun Heo  *       format string '%32b[l]' or through '%*b[l]', [l] selects
1345*dbc760bcSTejun Heo  *       range-list format instead of hex format
1346dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
1347dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
13488a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1349bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1350c8e00060SJoe Perches  *       with a dash-separated hex notation
13517c59154eSAndy Shevchenko  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
13528a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
13538a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
13548a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
135510679643SDaniel Borkmann  *       [S][pfs]
135610679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
135710679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
13588a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
13598a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
13608a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
136110679643SDaniel Borkmann  *       [S][pfs]
136210679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
136310679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
136410679643SDaniel Borkmann  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
136510679643SDaniel Borkmann  * - 'I[6S]c' for IPv6 addresses printed as specified by
136629cf519eSJoe Perches  *       http://tools.ietf.org/html/rfc5952
136771dca95dSAndy Shevchenko  * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
136871dca95dSAndy Shevchenko  *                of the following flags (see string_escape_mem() for the
136971dca95dSAndy Shevchenko  *                details):
137071dca95dSAndy Shevchenko  *                  a - ESCAPE_ANY
137171dca95dSAndy Shevchenko  *                  c - ESCAPE_SPECIAL
137271dca95dSAndy Shevchenko  *                  h - ESCAPE_HEX
137371dca95dSAndy Shevchenko  *                  n - ESCAPE_NULL
137471dca95dSAndy Shevchenko  *                  o - ESCAPE_OCTAL
137571dca95dSAndy Shevchenko  *                  p - ESCAPE_NP
137671dca95dSAndy Shevchenko  *                  s - ESCAPE_SPACE
137771dca95dSAndy Shevchenko  *                By default ESCAPE_ANY_NP is used.
13789ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
13799ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
13809ac6e44eSJoe Perches  *       Options for %pU are:
13819ac6e44eSJoe Perches  *         b big endian lower case hex (default)
13829ac6e44eSJoe Perches  *         B big endian UPPER case hex
13839ac6e44eSJoe Perches  *         l little endian lower case hex
13849ac6e44eSJoe Perches  *         L little endian UPPER case hex
13859ac6e44eSJoe Perches  *           big endian output byte order is:
13869ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
13879ac6e44eSJoe Perches  *           little endian output byte order is:
13889ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
13897db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
13907db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
13917db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
13927db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
13937db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
1394455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
1395c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
139631550a16SAndy Shevchenko  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
139731550a16SAndy Shevchenko  *            a certain separator (' ' by default):
139831550a16SAndy Shevchenko  *              C colon
139931550a16SAndy Shevchenko  *              D dash
140031550a16SAndy Shevchenko  *              N no separator
140131550a16SAndy Shevchenko  *            The maximum supported length is 64 bytes of the input. Consider
140231550a16SAndy Shevchenko  *            to use print_hex_dump() for the larger input.
1403aaf07621SJoe Perches  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1404aaf07621SJoe Perches  *           (default assumed to be phys_addr_t, passed by reference)
1405c0d92a57SOlof Johansson  * - 'd[234]' For a dentry name (optionally 2-4 last components)
1406c0d92a57SOlof Johansson  * - 'D[234]' Same as 'd' but for a struct file
14079ac6e44eSJoe Perches  *
1408332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1409332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
1410332d2e78SLinus Torvalds  * pointer to the real address.
14114d8a743cSLinus Torvalds  */
1412cf3b429bSJoe Perches static noinline_for_stack
1413cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1414fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
141578a8bf69SLinus Torvalds {
1416725fe002SGrant Likely 	int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1417725fe002SGrant Likely 
14189f36e2c4SKees Cook 	if (!ptr && *fmt != 'K') {
14195e057981SJoe Perches 		/*
14205e057981SJoe Perches 		 * Print (null) with the same width as a pointer so it makes
14215e057981SJoe Perches 		 * tabular output look nice.
14225e057981SJoe Perches 		 */
14235e057981SJoe Perches 		if (spec.field_width == -1)
1424725fe002SGrant Likely 			spec.field_width = default_width;
1425fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
14265e057981SJoe Perches 	}
1427d97106abSLinus Torvalds 
14280fe1ef24SLinus Torvalds 	switch (*fmt) {
14290fe1ef24SLinus Torvalds 	case 'F':
14300c8b946eSFrederic Weisbecker 	case 'f':
14310fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
14320fe1ef24SLinus Torvalds 		/* Fallthrough */
14330fe1ef24SLinus Torvalds 	case 'S':
14349ac6e44eSJoe Perches 	case 's':
14350f77a8d3SNamhyung Kim 	case 'B':
1436b0d33c2bSJoe Perches 		return symbol_string(buf, end, ptr, spec, fmt);
1437332d2e78SLinus Torvalds 	case 'R':
1438c7dabef8SBjorn Helgaas 	case 'r':
1439fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
144031550a16SAndy Shevchenko 	case 'h':
144131550a16SAndy Shevchenko 		return hex_string(buf, end, ptr, spec, fmt);
1442*dbc760bcSTejun Heo 	case 'b':
1443*dbc760bcSTejun Heo 		switch (fmt[1]) {
1444*dbc760bcSTejun Heo 		case 'l':
1445*dbc760bcSTejun Heo 			return bitmap_list_string(buf, end, ptr, spec, fmt);
1446*dbc760bcSTejun Heo 		default:
1447*dbc760bcSTejun Heo 			return bitmap_string(buf, end, ptr, spec, fmt);
1448*dbc760bcSTejun Heo 		}
14498a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
14508a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
145176597ff9SAndrei Emeltchenko 					/* [mM]F (FDDI) */
145276597ff9SAndrei Emeltchenko 					/* [mM]R (Reverse order; Bluetooth) */
14538a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
14548a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
14558a27f7c9SJoe Perches 					 * 4:	1.2.3.4
14568a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
14578a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
14588a27f7c9SJoe Perches 					 */
14598a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
14608a27f7c9SJoe Perches 					 * 4:	001.002.003.004
14618a27f7c9SJoe Perches 					 * 6:   000102...0f
14628a27f7c9SJoe Perches 					 */
14638a27f7c9SJoe Perches 		switch (fmt[1]) {
14648a27f7c9SJoe Perches 		case '6':
14658a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
14668a27f7c9SJoe Perches 		case '4':
14678a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
146810679643SDaniel Borkmann 		case 'S': {
146910679643SDaniel Borkmann 			const union {
147010679643SDaniel Borkmann 				struct sockaddr		raw;
147110679643SDaniel Borkmann 				struct sockaddr_in	v4;
147210679643SDaniel Borkmann 				struct sockaddr_in6	v6;
147310679643SDaniel Borkmann 			} *sa = ptr;
147410679643SDaniel Borkmann 
147510679643SDaniel Borkmann 			switch (sa->raw.sa_family) {
147610679643SDaniel Borkmann 			case AF_INET:
147710679643SDaniel Borkmann 				return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
147810679643SDaniel Borkmann 			case AF_INET6:
147910679643SDaniel Borkmann 				return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
148010679643SDaniel Borkmann 			default:
148110679643SDaniel Borkmann 				return string(buf, end, "(invalid address)", spec);
148210679643SDaniel Borkmann 			}}
14838a27f7c9SJoe Perches 		}
14844aa99606SHarvey Harrison 		break;
148571dca95dSAndy Shevchenko 	case 'E':
148671dca95dSAndy Shevchenko 		return escaped_string(buf, end, ptr, spec, fmt);
14879ac6e44eSJoe Perches 	case 'U':
14889ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
14897db6f5fbSJoe Perches 	case 'V':
14905756b76eSJan Beulich 		{
14915756b76eSJan Beulich 			va_list va;
14925756b76eSJan Beulich 
14935756b76eSJan Beulich 			va_copy(va, *((struct va_format *)ptr)->va);
14945756b76eSJan Beulich 			buf += vsnprintf(buf, end > buf ? end - buf : 0,
14955756b76eSJan Beulich 					 ((struct va_format *)ptr)->fmt, va);
14965756b76eSJan Beulich 			va_end(va);
14975756b76eSJan Beulich 			return buf;
14985756b76eSJan Beulich 		}
1499455cd5abSDan Rosenberg 	case 'K':
1500455cd5abSDan Rosenberg 		/*
1501455cd5abSDan Rosenberg 		 * %pK cannot be used in IRQ context because its test
1502455cd5abSDan Rosenberg 		 * for CAP_SYSLOG would be meaningless.
1503455cd5abSDan Rosenberg 		 */
15043715c530SDan Rosenberg 		if (kptr_restrict && (in_irq() || in_serving_softirq() ||
15053715c530SDan Rosenberg 				      in_nmi())) {
1506455cd5abSDan Rosenberg 			if (spec.field_width == -1)
1507725fe002SGrant Likely 				spec.field_width = default_width;
1508455cd5abSDan Rosenberg 			return string(buf, end, "pK-error", spec);
1509455cd5abSDan Rosenberg 		}
1510312b4e22SRyan Mallon 
1511312b4e22SRyan Mallon 		switch (kptr_restrict) {
1512312b4e22SRyan Mallon 		case 0:
1513312b4e22SRyan Mallon 			/* Always print %pK values */
1514312b4e22SRyan Mallon 			break;
1515312b4e22SRyan Mallon 		case 1: {
1516312b4e22SRyan Mallon 			/*
1517312b4e22SRyan Mallon 			 * Only print the real pointer value if the current
1518312b4e22SRyan Mallon 			 * process has CAP_SYSLOG and is running with the
1519312b4e22SRyan Mallon 			 * same credentials it started with. This is because
1520312b4e22SRyan Mallon 			 * access to files is checked at open() time, but %pK
1521312b4e22SRyan Mallon 			 * checks permission at read() time. We don't want to
1522312b4e22SRyan Mallon 			 * leak pointer values if a binary opens a file using
1523312b4e22SRyan Mallon 			 * %pK and then elevates privileges before reading it.
1524312b4e22SRyan Mallon 			 */
1525312b4e22SRyan Mallon 			const struct cred *cred = current_cred();
1526312b4e22SRyan Mallon 
1527312b4e22SRyan Mallon 			if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1528312b4e22SRyan Mallon 			    !uid_eq(cred->euid, cred->uid) ||
1529312b4e22SRyan Mallon 			    !gid_eq(cred->egid, cred->gid))
153026297607SJoe Perches 				ptr = NULL;
153126297607SJoe Perches 			break;
1532312b4e22SRyan Mallon 		}
1533312b4e22SRyan Mallon 		case 2:
1534312b4e22SRyan Mallon 		default:
1535312b4e22SRyan Mallon 			/* Always print 0's for %pK */
1536312b4e22SRyan Mallon 			ptr = NULL;
1537312b4e22SRyan Mallon 			break;
1538312b4e22SRyan Mallon 		}
1539312b4e22SRyan Mallon 		break;
1540312b4e22SRyan Mallon 
1541c8f44affSMichał Mirosław 	case 'N':
1542c8f44affSMichał Mirosław 		switch (fmt[1]) {
1543c8f44affSMichał Mirosław 		case 'F':
1544c8f44affSMichał Mirosław 			return netdev_feature_string(buf, end, ptr, spec);
1545c8f44affSMichał Mirosław 		}
1546c8f44affSMichał Mirosław 		break;
15477d799210SStepan Moskovchenko 	case 'a':
1548aaf07621SJoe Perches 		return address_val(buf, end, ptr, spec, fmt);
15494b6ccca7SAl Viro 	case 'd':
15504b6ccca7SAl Viro 		return dentry_name(buf, end, ptr, spec, fmt);
15514b6ccca7SAl Viro 	case 'D':
15524b6ccca7SAl Viro 		return dentry_name(buf, end,
15534b6ccca7SAl Viro 				   ((const struct file *)ptr)->f_path.dentry,
15544b6ccca7SAl Viro 				   spec, fmt);
15550fe1ef24SLinus Torvalds 	}
1556fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
1557fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
1558725fe002SGrant Likely 		spec.field_width = default_width;
1559fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
156078a8bf69SLinus Torvalds 	}
1561fef20d9cSFrederic Weisbecker 	spec.base = 16;
1562fef20d9cSFrederic Weisbecker 
1563fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
1564fef20d9cSFrederic Weisbecker }
1565fef20d9cSFrederic Weisbecker 
1566fef20d9cSFrederic Weisbecker /*
1567fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
1568fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
1569fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
1570fef20d9cSFrederic Weisbecker  * to go on the next call).
1571fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
1572fef20d9cSFrederic Weisbecker  *
1573fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
1574fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
1575fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
1576fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
1577fef20d9cSFrederic Weisbecker  *
1578fef20d9cSFrederic Weisbecker  * @fmt: the format string
1579fef20d9cSFrederic Weisbecker  * @type of the token returned
1580fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
1581fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
1582fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
1583fef20d9cSFrederic Weisbecker  * @precision: precision of a number
1584fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
1585fef20d9cSFrederic Weisbecker  */
1586cf3b429bSJoe Perches static noinline_for_stack
1587cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
1588fef20d9cSFrederic Weisbecker {
1589fef20d9cSFrederic Weisbecker 	const char *start = fmt;
1590fef20d9cSFrederic Weisbecker 
1591fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
1592ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
1593fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
1594fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
1595fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
1596fef20d9cSFrederic Weisbecker 		}
1597fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1598fef20d9cSFrederic Weisbecker 		goto precision;
1599fef20d9cSFrederic Weisbecker 	}
1600fef20d9cSFrederic Weisbecker 
1601fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
1602fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
1603fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
1604fef20d9cSFrederic Weisbecker 			spec->precision = 0;
1605fef20d9cSFrederic Weisbecker 
1606fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1607fef20d9cSFrederic Weisbecker 		goto qualifier;
1608fef20d9cSFrederic Weisbecker 	}
1609fef20d9cSFrederic Weisbecker 
1610fef20d9cSFrederic Weisbecker 	/* By default */
1611fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
1612fef20d9cSFrederic Weisbecker 
1613fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
1614fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
1615fef20d9cSFrederic Weisbecker 			break;
1616fef20d9cSFrederic Weisbecker 	}
1617fef20d9cSFrederic Weisbecker 
1618fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
1619fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
1620fef20d9cSFrederic Weisbecker 		return fmt - start;
1621fef20d9cSFrederic Weisbecker 
1622fef20d9cSFrederic Weisbecker 	/* Process flags */
1623fef20d9cSFrederic Weisbecker 	spec->flags = 0;
1624fef20d9cSFrederic Weisbecker 
1625fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
1626fef20d9cSFrederic Weisbecker 		bool found = true;
1627fef20d9cSFrederic Weisbecker 
1628fef20d9cSFrederic Weisbecker 		++fmt;
1629fef20d9cSFrederic Weisbecker 
1630fef20d9cSFrederic Weisbecker 		switch (*fmt) {
1631fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
1632fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
1633fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
1634fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
1635fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
1636fef20d9cSFrederic Weisbecker 		default:  found = false;
1637fef20d9cSFrederic Weisbecker 		}
1638fef20d9cSFrederic Weisbecker 
1639fef20d9cSFrederic Weisbecker 		if (!found)
1640fef20d9cSFrederic Weisbecker 			break;
1641fef20d9cSFrederic Weisbecker 	}
1642fef20d9cSFrederic Weisbecker 
1643fef20d9cSFrederic Weisbecker 	/* get field width */
1644fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
1645fef20d9cSFrederic Weisbecker 
1646fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
1647fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
1648fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
1649fef20d9cSFrederic Weisbecker 		/* it's the next argument */
1650ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
1651fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1652fef20d9cSFrederic Weisbecker 	}
1653fef20d9cSFrederic Weisbecker 
1654fef20d9cSFrederic Weisbecker precision:
1655fef20d9cSFrederic Weisbecker 	/* get the precision */
1656fef20d9cSFrederic Weisbecker 	spec->precision = -1;
1657fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
1658fef20d9cSFrederic Weisbecker 		++fmt;
1659fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
1660fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
1661fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
1662fef20d9cSFrederic Weisbecker 				spec->precision = 0;
1663fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
1664fef20d9cSFrederic Weisbecker 			/* it's the next argument */
1665adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
1666fef20d9cSFrederic Weisbecker 			return ++fmt - start;
1667fef20d9cSFrederic Weisbecker 		}
1668fef20d9cSFrederic Weisbecker 	}
1669fef20d9cSFrederic Weisbecker 
1670fef20d9cSFrederic Weisbecker qualifier:
1671fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
1672fef20d9cSFrederic Weisbecker 	spec->qualifier = -1;
167375fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
167475fb8f26SAndy Shevchenko 	    _tolower(*fmt) == 'z' || *fmt == 't') {
1675a4e94ef0SZhaolei 		spec->qualifier = *fmt++;
1676a4e94ef0SZhaolei 		if (unlikely(spec->qualifier == *fmt)) {
1677a4e94ef0SZhaolei 			if (spec->qualifier == 'l') {
1678fef20d9cSFrederic Weisbecker 				spec->qualifier = 'L';
1679fef20d9cSFrederic Weisbecker 				++fmt;
1680a4e94ef0SZhaolei 			} else if (spec->qualifier == 'h') {
1681a4e94ef0SZhaolei 				spec->qualifier = 'H';
1682a4e94ef0SZhaolei 				++fmt;
1683a4e94ef0SZhaolei 			}
1684fef20d9cSFrederic Weisbecker 		}
1685fef20d9cSFrederic Weisbecker 	}
1686fef20d9cSFrederic Weisbecker 
1687fef20d9cSFrederic Weisbecker 	/* default base */
1688fef20d9cSFrederic Weisbecker 	spec->base = 10;
1689fef20d9cSFrederic Weisbecker 	switch (*fmt) {
1690fef20d9cSFrederic Weisbecker 	case 'c':
1691fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
1692fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1693fef20d9cSFrederic Weisbecker 
1694fef20d9cSFrederic Weisbecker 	case 's':
1695fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
1696fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1697fef20d9cSFrederic Weisbecker 
1698fef20d9cSFrederic Weisbecker 	case 'p':
1699fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
1700ffbfed03SRasmus Villemoes 		return ++fmt - start;
1701fef20d9cSFrederic Weisbecker 
1702fef20d9cSFrederic Weisbecker 	case '%':
1703fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1704fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1705fef20d9cSFrederic Weisbecker 
1706fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1707fef20d9cSFrederic Weisbecker 	case 'o':
1708fef20d9cSFrederic Weisbecker 		spec->base = 8;
1709fef20d9cSFrederic Weisbecker 		break;
1710fef20d9cSFrederic Weisbecker 
1711fef20d9cSFrederic Weisbecker 	case 'x':
1712fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1713fef20d9cSFrederic Weisbecker 
1714fef20d9cSFrederic Weisbecker 	case 'X':
1715fef20d9cSFrederic Weisbecker 		spec->base = 16;
1716fef20d9cSFrederic Weisbecker 		break;
1717fef20d9cSFrederic Weisbecker 
1718fef20d9cSFrederic Weisbecker 	case 'd':
1719fef20d9cSFrederic Weisbecker 	case 'i':
172039e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1721fef20d9cSFrederic Weisbecker 	case 'u':
1722fef20d9cSFrederic Weisbecker 		break;
1723fef20d9cSFrederic Weisbecker 
1724708d96fdSRyan Mallon 	case 'n':
1725708d96fdSRyan Mallon 		/*
1726708d96fdSRyan Mallon 		 * Since %n poses a greater security risk than utility, treat
1727708d96fdSRyan Mallon 		 * it as an invalid format specifier. Warn about its use so
1728708d96fdSRyan Mallon 		 * that new instances don't get added.
1729708d96fdSRyan Mallon 		 */
1730708d96fdSRyan Mallon 		WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt);
1731708d96fdSRyan Mallon 		/* Fall-through */
1732708d96fdSRyan Mallon 
1733fef20d9cSFrederic Weisbecker 	default:
1734fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1735fef20d9cSFrederic Weisbecker 		return fmt - start;
1736fef20d9cSFrederic Weisbecker 	}
1737fef20d9cSFrederic Weisbecker 
1738fef20d9cSFrederic Weisbecker 	if (spec->qualifier == 'L')
1739fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1740fef20d9cSFrederic Weisbecker 	else if (spec->qualifier == 'l') {
174139e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1742fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_LONG;
1743fef20d9cSFrederic Weisbecker 		else
1744fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_ULONG;
174575fb8f26SAndy Shevchenko 	} else if (_tolower(spec->qualifier) == 'z') {
1746fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1747fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 't') {
1748fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1749a4e94ef0SZhaolei 	} else if (spec->qualifier == 'H') {
1750a4e94ef0SZhaolei 		if (spec->flags & SIGN)
1751a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_BYTE;
1752a4e94ef0SZhaolei 		else
1753a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_UBYTE;
1754fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'h') {
175539e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1756fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_SHORT;
1757fef20d9cSFrederic Weisbecker 		else
1758fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_USHORT;
1759fef20d9cSFrederic Weisbecker 	} else {
176039e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1761fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_INT;
1762fef20d9cSFrederic Weisbecker 		else
1763fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_UINT;
1764fef20d9cSFrederic Weisbecker 	}
1765fef20d9cSFrederic Weisbecker 
1766fef20d9cSFrederic Weisbecker 	return ++fmt - start;
176778a8bf69SLinus Torvalds }
176878a8bf69SLinus Torvalds 
17691da177e4SLinus Torvalds /**
17701da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
17711da177e4SLinus Torvalds  * @buf: The buffer to place the result into
17721da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
17731da177e4SLinus Torvalds  * @fmt: The format string to use
17741da177e4SLinus Torvalds  * @args: Arguments for the format string
17751da177e4SLinus Torvalds  *
177620036fdcSAndi Kleen  * This function follows C99 vsnprintf, but has some extensions:
177791adcd2cSSteven Rostedt  * %pS output the name of a text symbol with offset
177891adcd2cSSteven Rostedt  * %ps output the name of a text symbol without offset
17790c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
17800c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
17810f77a8d3SNamhyung Kim  * %pB output the name of a backtrace symbol with its offset
17828a79503aSUwe Kleine-König  * %pR output the address range in a struct resource with decoded flags
17838a79503aSUwe Kleine-König  * %pr output the address range in a struct resource with raw flags
1784*dbc760bcSTejun Heo  * %pb output the bitmap with field width as the number of bits
1785*dbc760bcSTejun Heo  * %pbl output the bitmap as range list with field width as the number of bits
17868a79503aSUwe Kleine-König  * %pM output a 6-byte MAC address with colons
17877c59154eSAndy Shevchenko  * %pMR output a 6-byte MAC address with colons in reversed order
17887c59154eSAndy Shevchenko  * %pMF output a 6-byte MAC address with dashes
17898a79503aSUwe Kleine-König  * %pm output a 6-byte MAC address without colons
17907c59154eSAndy Shevchenko  * %pmR output a 6-byte MAC address without colons in reversed order
17918a79503aSUwe Kleine-König  * %pI4 print an IPv4 address without leading zeros
17928a79503aSUwe Kleine-König  * %pi4 print an IPv4 address with leading zeros
17938a79503aSUwe Kleine-König  * %pI6 print an IPv6 address with colons
17948a79503aSUwe Kleine-König  * %pi6 print an IPv6 address without colons
1795f996f208SJan Engelhardt  * %pI6c print an IPv6 address as specified by RFC 5952
179610679643SDaniel Borkmann  * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
179710679643SDaniel Borkmann  * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
17988a79503aSUwe Kleine-König  * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
17998a79503aSUwe Kleine-König  *   case.
180071dca95dSAndy Shevchenko  * %*pE[achnops] print an escaped buffer
180131550a16SAndy Shevchenko  * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
180231550a16SAndy Shevchenko  *           bytes of the input)
18030efb4d20SSteven Rostedt  * %n is ignored
180420036fdcSAndi Kleen  *
180580f548e0SAndrew Morton  * ** Please update Documentation/printk-formats.txt when making changes **
180680f548e0SAndrew Morton  *
18071da177e4SLinus Torvalds  * The return value is the number of characters which would
18081da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
18091da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
18101da177e4SLinus Torvalds  * number of characters written into @buf as return value
181172fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
18121da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
18131da177e4SLinus Torvalds  * string is truncated.
18141da177e4SLinus Torvalds  *
1815ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
18161da177e4SLinus Torvalds  */
18171da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
18181da177e4SLinus Torvalds {
18191da177e4SLinus Torvalds 	unsigned long long num;
1820d4be151bSAndré Goddard Rosa 	char *str, *end;
1821fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
18221da177e4SLinus Torvalds 
1823f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1824f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
18252aa2f9e2SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
18261da177e4SLinus Torvalds 		return 0;
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 	str = buf;
1829f796937aSJeremy Fitzhardinge 	end = buf + size;
18301da177e4SLinus Torvalds 
1831f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1832f796937aSJeremy Fitzhardinge 	if (end < buf) {
18331da177e4SLinus Torvalds 		end = ((void *)-1);
1834f796937aSJeremy Fitzhardinge 		size = end - buf;
18351da177e4SLinus Torvalds 	}
18361da177e4SLinus Torvalds 
1837fef20d9cSFrederic Weisbecker 	while (*fmt) {
1838fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1839d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1840fef20d9cSFrederic Weisbecker 
1841fef20d9cSFrederic Weisbecker 		fmt += read;
1842fef20d9cSFrederic Weisbecker 
1843fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1844fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1845fef20d9cSFrederic Weisbecker 			int copy = read;
1846fef20d9cSFrederic Weisbecker 			if (str < end) {
1847fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1848fef20d9cSFrederic Weisbecker 					copy = end - str;
1849fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1850fef20d9cSFrederic Weisbecker 			}
1851fef20d9cSFrederic Weisbecker 			str += read;
1852fef20d9cSFrederic Weisbecker 			break;
18531da177e4SLinus Torvalds 		}
18541da177e4SLinus Torvalds 
1855ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1856fef20d9cSFrederic Weisbecker 			spec.field_width = va_arg(args, int);
1857fef20d9cSFrederic Weisbecker 			break;
18581da177e4SLinus Torvalds 
1859fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1860fef20d9cSFrederic Weisbecker 			spec.precision = va_arg(args, int);
1861fef20d9cSFrederic Weisbecker 			break;
18621da177e4SLinus Torvalds 
1863d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1864d4be151bSAndré Goddard Rosa 			char c;
1865d4be151bSAndré Goddard Rosa 
1866fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1867fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1868f796937aSJeremy Fitzhardinge 					if (str < end)
18691da177e4SLinus Torvalds 						*str = ' ';
18701da177e4SLinus Torvalds 					++str;
1871fef20d9cSFrederic Weisbecker 
18721da177e4SLinus Torvalds 				}
18731da177e4SLinus Torvalds 			}
18741da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1875f796937aSJeremy Fitzhardinge 			if (str < end)
18761da177e4SLinus Torvalds 				*str = c;
18771da177e4SLinus Torvalds 			++str;
1878fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1879f796937aSJeremy Fitzhardinge 				if (str < end)
18801da177e4SLinus Torvalds 					*str = ' ';
18811da177e4SLinus Torvalds 				++str;
18821da177e4SLinus Torvalds 			}
1883fef20d9cSFrederic Weisbecker 			break;
1884d4be151bSAndré Goddard Rosa 		}
18851da177e4SLinus Torvalds 
1886fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1887fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1888fef20d9cSFrederic Weisbecker 			break;
18891da177e4SLinus Torvalds 
1890fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1891ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, va_arg(args, void *),
1892fef20d9cSFrederic Weisbecker 				      spec);
1893fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
18944d8a743cSLinus Torvalds 				fmt++;
1895fef20d9cSFrederic Weisbecker 			break;
18961da177e4SLinus Torvalds 
1897fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1898f796937aSJeremy Fitzhardinge 			if (str < end)
18991da177e4SLinus Torvalds 				*str = '%';
19001da177e4SLinus Torvalds 			++str;
19011da177e4SLinus Torvalds 			break;
19021da177e4SLinus Torvalds 
1903fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1904f796937aSJeremy Fitzhardinge 			if (str < end)
19051da177e4SLinus Torvalds 				*str = '%';
19061da177e4SLinus Torvalds 			++str;
1907fef20d9cSFrederic Weisbecker 			break;
1908fef20d9cSFrederic Weisbecker 
1909fef20d9cSFrederic Weisbecker 		default:
1910fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1911fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1912fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1913fef20d9cSFrederic Weisbecker 				break;
1914fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1915fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
1916fef20d9cSFrederic Weisbecker 				break;
1917fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1918fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
1919fef20d9cSFrederic Weisbecker 				break;
1920fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1921ef124960SJason Gunthorpe 				if (spec.flags & SIGN)
1922ef124960SJason Gunthorpe 					num = va_arg(args, ssize_t);
1923ef124960SJason Gunthorpe 				else
1924fef20d9cSFrederic Weisbecker 					num = va_arg(args, size_t);
1925fef20d9cSFrederic Weisbecker 				break;
1926fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1927fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
1928fef20d9cSFrederic Weisbecker 				break;
1929a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1930a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
1931a4e94ef0SZhaolei 				break;
1932a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1933a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
1934a4e94ef0SZhaolei 				break;
1935fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1936fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
1937fef20d9cSFrederic Weisbecker 				break;
1938fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1939fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
1940fef20d9cSFrederic Weisbecker 				break;
194139e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
194239e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
1943fef20d9cSFrederic Weisbecker 				break;
1944fef20d9cSFrederic Weisbecker 			default:
1945fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
19461da177e4SLinus Torvalds 			}
1947fef20d9cSFrederic Weisbecker 
1948fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
19491da177e4SLinus Torvalds 		}
1950fef20d9cSFrederic Weisbecker 	}
1951fef20d9cSFrederic Weisbecker 
1952f796937aSJeremy Fitzhardinge 	if (size > 0) {
1953f796937aSJeremy Fitzhardinge 		if (str < end)
19541da177e4SLinus Torvalds 			*str = '\0';
1955f796937aSJeremy Fitzhardinge 		else
19560a6047eeSLinus Torvalds 			end[-1] = '\0';
1957f796937aSJeremy Fitzhardinge 	}
1958fef20d9cSFrederic Weisbecker 
1959f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
19601da177e4SLinus Torvalds 	return str-buf;
1961fef20d9cSFrederic Weisbecker 
19621da177e4SLinus Torvalds }
19631da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds /**
19661da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
19671da177e4SLinus Torvalds  * @buf: The buffer to place the result into
19681da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
19691da177e4SLinus Torvalds  * @fmt: The format string to use
19701da177e4SLinus Torvalds  * @args: Arguments for the format string
19711da177e4SLinus Torvalds  *
19721da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
1973b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
19741da177e4SLinus Torvalds  * returns 0.
19751da177e4SLinus Torvalds  *
1976ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
197720036fdcSAndi Kleen  *
197820036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
19791da177e4SLinus Torvalds  */
19801da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
19811da177e4SLinus Torvalds {
19821da177e4SLinus Torvalds 	int i;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
19857b9186f5SAndré Goddard Rosa 
1986b921c69fSAnton Arapov 	if (likely(i < size))
1987b921c69fSAnton Arapov 		return i;
1988b921c69fSAnton Arapov 	if (size != 0)
1989b921c69fSAnton Arapov 		return size - 1;
1990b921c69fSAnton Arapov 	return 0;
19911da177e4SLinus Torvalds }
19921da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds /**
19951da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
19961da177e4SLinus Torvalds  * @buf: The buffer to place the result into
19971da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
19981da177e4SLinus Torvalds  * @fmt: The format string to use
19991da177e4SLinus Torvalds  * @...: Arguments for the format string
20001da177e4SLinus Torvalds  *
20011da177e4SLinus Torvalds  * The return value is the number of characters which would be
20021da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
20031da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
20041da177e4SLinus Torvalds  * @size, the resulting string is truncated.
200520036fdcSAndi Kleen  *
200620036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
20071da177e4SLinus Torvalds  */
20081da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
20091da177e4SLinus Torvalds {
20101da177e4SLinus Torvalds 	va_list args;
20111da177e4SLinus Torvalds 	int i;
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds 	va_start(args, fmt);
20141da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
20151da177e4SLinus Torvalds 	va_end(args);
20167b9186f5SAndré Goddard Rosa 
20171da177e4SLinus Torvalds 	return i;
20181da177e4SLinus Torvalds }
20191da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds /**
20221da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
20231da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20241da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
20251da177e4SLinus Torvalds  * @fmt: The format string to use
20261da177e4SLinus Torvalds  * @...: Arguments for the format string
20271da177e4SLinus Torvalds  *
20281da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
2029b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
20301da177e4SLinus Torvalds  */
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
20331da177e4SLinus Torvalds {
20341da177e4SLinus Torvalds 	va_list args;
20351da177e4SLinus Torvalds 	int i;
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds 	va_start(args, fmt);
2038b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
20391da177e4SLinus Torvalds 	va_end(args);
20407b9186f5SAndré Goddard Rosa 
2041b903c0b8SChangli Gao 	return i;
20421da177e4SLinus Torvalds }
20431da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds /**
20461da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
20471da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20481da177e4SLinus Torvalds  * @fmt: The format string to use
20491da177e4SLinus Torvalds  * @args: Arguments for the format string
20501da177e4SLinus Torvalds  *
20511da177e4SLinus Torvalds  * The function returns the number of characters written
205272fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
20531da177e4SLinus Torvalds  * buffer overflows.
20541da177e4SLinus Torvalds  *
2055ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
205620036fdcSAndi Kleen  *
205720036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
20581da177e4SLinus Torvalds  */
20591da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
20601da177e4SLinus Torvalds {
20611da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
20621da177e4SLinus Torvalds }
20631da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds /**
20661da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
20671da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20681da177e4SLinus Torvalds  * @fmt: The format string to use
20691da177e4SLinus Torvalds  * @...: Arguments for the format string
20701da177e4SLinus Torvalds  *
20711da177e4SLinus Torvalds  * The function returns the number of characters written
207272fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
20731da177e4SLinus Torvalds  * buffer overflows.
207420036fdcSAndi Kleen  *
207520036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
20761da177e4SLinus Torvalds  */
20771da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
20781da177e4SLinus Torvalds {
20791da177e4SLinus Torvalds 	va_list args;
20801da177e4SLinus Torvalds 	int i;
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 	va_start(args, fmt);
20831da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
20841da177e4SLinus Torvalds 	va_end(args);
20857b9186f5SAndré Goddard Rosa 
20861da177e4SLinus Torvalds 	return i;
20871da177e4SLinus Torvalds }
20881da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
20891da177e4SLinus Torvalds 
20904370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
20914370aa4aSLai Jiangshan /*
20924370aa4aSLai Jiangshan  * bprintf service:
20934370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
20944370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
20954370aa4aSLai Jiangshan  */
20964370aa4aSLai Jiangshan 
20974370aa4aSLai Jiangshan /**
20984370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
20994370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
21004370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
21014370aa4aSLai Jiangshan  * @fmt: The format string to use
21024370aa4aSLai Jiangshan  * @args: Arguments for the format string
21034370aa4aSLai Jiangshan  *
21044370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
2105da3dae54SMasanari Iida  * is skipped.
21064370aa4aSLai Jiangshan  *
21074370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
21084370aa4aSLai Jiangshan  * the given input.
21094370aa4aSLai Jiangshan  *
21104370aa4aSLai Jiangshan  * NOTE:
21114370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
21124370aa4aSLai Jiangshan  * valid for bstr_printf().
21134370aa4aSLai Jiangshan  */
21144370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
21154370aa4aSLai Jiangshan {
2116fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
21174370aa4aSLai Jiangshan 	char *str, *end;
21184370aa4aSLai Jiangshan 
21194370aa4aSLai Jiangshan 	str = (char *)bin_buf;
21204370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
21214370aa4aSLai Jiangshan 
21224370aa4aSLai Jiangshan #define save_arg(type)							\
21234370aa4aSLai Jiangshan do {									\
21244370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
21254370aa4aSLai Jiangshan 		unsigned long long value;				\
21264370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
21274370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
21284370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
21294370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
21304370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
21314370aa4aSLai Jiangshan 		}							\
21324370aa4aSLai Jiangshan 	} else {							\
21334370aa4aSLai Jiangshan 		unsigned long value;					\
21344370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
21354370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
21364370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
21374370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
21384370aa4aSLai Jiangshan 	}								\
21394370aa4aSLai Jiangshan 	str += sizeof(type);						\
21404370aa4aSLai Jiangshan } while (0)
21414370aa4aSLai Jiangshan 
2142fef20d9cSFrederic Weisbecker 	while (*fmt) {
2143d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
21444370aa4aSLai Jiangshan 
2145fef20d9cSFrederic Weisbecker 		fmt += read;
2146fef20d9cSFrederic Weisbecker 
2147fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2148fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
2149d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_INVALID:
2150d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
2151fef20d9cSFrederic Weisbecker 			break;
2152fef20d9cSFrederic Weisbecker 
2153ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2154fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
21554370aa4aSLai Jiangshan 			save_arg(int);
2156fef20d9cSFrederic Weisbecker 			break;
21574370aa4aSLai Jiangshan 
2158fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
21594370aa4aSLai Jiangshan 			save_arg(char);
2160fef20d9cSFrederic Weisbecker 			break;
2161fef20d9cSFrederic Weisbecker 
2162fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
21634370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
21644370aa4aSLai Jiangshan 			size_t len;
21656c356634SAndré Goddard Rosa 
21664370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
21674370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
21680f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
21696c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
21706c356634SAndré Goddard Rosa 			if (str + len < end)
21716c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
21726c356634SAndré Goddard Rosa 			str += len;
2173fef20d9cSFrederic Weisbecker 			break;
21744370aa4aSLai Jiangshan 		}
2175fef20d9cSFrederic Weisbecker 
2176fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
21774370aa4aSLai Jiangshan 			save_arg(void *);
21784370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
2179fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
21804370aa4aSLai Jiangshan 				fmt++;
2181fef20d9cSFrederic Weisbecker 			break;
2182fef20d9cSFrederic Weisbecker 
2183fef20d9cSFrederic Weisbecker 		default:
2184fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2185fef20d9cSFrederic Weisbecker 
2186fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2187fef20d9cSFrederic Weisbecker 				save_arg(long long);
2188fef20d9cSFrederic Weisbecker 				break;
2189fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2190fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2191fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
2192fef20d9cSFrederic Weisbecker 				break;
2193fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2194fef20d9cSFrederic Weisbecker 				save_arg(size_t);
2195fef20d9cSFrederic Weisbecker 				break;
2196fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2197fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
2198fef20d9cSFrederic Weisbecker 				break;
2199a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2200a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2201a4e94ef0SZhaolei 				save_arg(char);
2202a4e94ef0SZhaolei 				break;
2203fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2204fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2205fef20d9cSFrederic Weisbecker 				save_arg(short);
2206fef20d9cSFrederic Weisbecker 				break;
2207fef20d9cSFrederic Weisbecker 			default:
2208fef20d9cSFrederic Weisbecker 				save_arg(int);
2209fef20d9cSFrederic Weisbecker 			}
2210fef20d9cSFrederic Weisbecker 		}
2211fef20d9cSFrederic Weisbecker 	}
2212fef20d9cSFrederic Weisbecker 
22137b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2214fef20d9cSFrederic Weisbecker #undef save_arg
22154370aa4aSLai Jiangshan }
22164370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
22174370aa4aSLai Jiangshan 
22184370aa4aSLai Jiangshan /**
22194370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
22204370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
22214370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
22224370aa4aSLai Jiangshan  * @fmt: The format string to use
22234370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
22244370aa4aSLai Jiangshan  *
22254370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
22264370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
22274370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
22284370aa4aSLai Jiangshan  *
22294370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
22300efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
22314370aa4aSLai Jiangshan  *
22324370aa4aSLai Jiangshan  * The return value is the number of characters which would
22334370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
22344370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
22354370aa4aSLai Jiangshan  * number of characters written into @buf as return value
22364370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
22374370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
22384370aa4aSLai Jiangshan  * string is truncated.
22394370aa4aSLai Jiangshan  */
22404370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
22414370aa4aSLai Jiangshan {
2242fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
2243d4be151bSAndré Goddard Rosa 	char *str, *end;
2244d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
22454370aa4aSLai Jiangshan 
22462f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
22474370aa4aSLai Jiangshan 		return 0;
22484370aa4aSLai Jiangshan 
22494370aa4aSLai Jiangshan 	str = buf;
22504370aa4aSLai Jiangshan 	end = buf + size;
22514370aa4aSLai Jiangshan 
22524370aa4aSLai Jiangshan #define get_arg(type)							\
22534370aa4aSLai Jiangshan ({									\
22544370aa4aSLai Jiangshan 	typeof(type) value;						\
22554370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
22564370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
22574370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
22584370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
22594370aa4aSLai Jiangshan 	} else {							\
22604370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
22614370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
22624370aa4aSLai Jiangshan 	}								\
22634370aa4aSLai Jiangshan 	args += sizeof(type);						\
22644370aa4aSLai Jiangshan 	value;								\
22654370aa4aSLai Jiangshan })
22664370aa4aSLai Jiangshan 
22674370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
22684370aa4aSLai Jiangshan 	if (end < buf) {
22694370aa4aSLai Jiangshan 		end = ((void *)-1);
22704370aa4aSLai Jiangshan 		size = end - buf;
22714370aa4aSLai Jiangshan 	}
22724370aa4aSLai Jiangshan 
2273fef20d9cSFrederic Weisbecker 	while (*fmt) {
2274fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
2275d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
2276fef20d9cSFrederic Weisbecker 
2277fef20d9cSFrederic Weisbecker 		fmt += read;
2278fef20d9cSFrederic Weisbecker 
2279fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2280fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2281fef20d9cSFrederic Weisbecker 			int copy = read;
2282fef20d9cSFrederic Weisbecker 			if (str < end) {
2283fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2284fef20d9cSFrederic Weisbecker 					copy = end - str;
2285fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2286fef20d9cSFrederic Weisbecker 			}
2287fef20d9cSFrederic Weisbecker 			str += read;
2288fef20d9cSFrederic Weisbecker 			break;
22894370aa4aSLai Jiangshan 		}
22904370aa4aSLai Jiangshan 
2291ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2292fef20d9cSFrederic Weisbecker 			spec.field_width = get_arg(int);
2293fef20d9cSFrederic Weisbecker 			break;
22944370aa4aSLai Jiangshan 
2295fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
2296fef20d9cSFrederic Weisbecker 			spec.precision = get_arg(int);
2297fef20d9cSFrederic Weisbecker 			break;
22984370aa4aSLai Jiangshan 
2299d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2300d4be151bSAndré Goddard Rosa 			char c;
2301d4be151bSAndré Goddard Rosa 
2302fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2303fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
23044370aa4aSLai Jiangshan 					if (str < end)
23054370aa4aSLai Jiangshan 						*str = ' ';
23064370aa4aSLai Jiangshan 					++str;
23074370aa4aSLai Jiangshan 				}
23084370aa4aSLai Jiangshan 			}
23094370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
23104370aa4aSLai Jiangshan 			if (str < end)
23114370aa4aSLai Jiangshan 				*str = c;
23124370aa4aSLai Jiangshan 			++str;
2313fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
23144370aa4aSLai Jiangshan 				if (str < end)
23154370aa4aSLai Jiangshan 					*str = ' ';
23164370aa4aSLai Jiangshan 				++str;
23174370aa4aSLai Jiangshan 			}
2318fef20d9cSFrederic Weisbecker 			break;
2319d4be151bSAndré Goddard Rosa 		}
23204370aa4aSLai Jiangshan 
2321fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
23224370aa4aSLai Jiangshan 			const char *str_arg = args;
2323d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
2324fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
2325fef20d9cSFrederic Weisbecker 			break;
23264370aa4aSLai Jiangshan 		}
23274370aa4aSLai Jiangshan 
2328fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2329ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, get_arg(void *), spec);
2330fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
23314370aa4aSLai Jiangshan 				fmt++;
2332fef20d9cSFrederic Weisbecker 			break;
23334370aa4aSLai Jiangshan 
2334fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
2335fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
23364370aa4aSLai Jiangshan 			if (str < end)
23374370aa4aSLai Jiangshan 				*str = '%';
23384370aa4aSLai Jiangshan 			++str;
2339fef20d9cSFrederic Weisbecker 			break;
2340fef20d9cSFrederic Weisbecker 
2341d4be151bSAndré Goddard Rosa 		default: {
2342d4be151bSAndré Goddard Rosa 			unsigned long long num;
2343d4be151bSAndré Goddard Rosa 
2344fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2345fef20d9cSFrederic Weisbecker 
2346fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
23474370aa4aSLai Jiangshan 				num = get_arg(long long);
2348fef20d9cSFrederic Weisbecker 				break;
2349fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2350fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2351fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
2352fef20d9cSFrederic Weisbecker 				break;
2353fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
23544370aa4aSLai Jiangshan 				num = get_arg(size_t);
2355fef20d9cSFrederic Weisbecker 				break;
2356fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
23574370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
2358fef20d9cSFrederic Weisbecker 				break;
2359a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2360a4e94ef0SZhaolei 				num = get_arg(unsigned char);
2361a4e94ef0SZhaolei 				break;
2362a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2363a4e94ef0SZhaolei 				num = get_arg(signed char);
2364a4e94ef0SZhaolei 				break;
2365fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2366fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
2367fef20d9cSFrederic Weisbecker 				break;
2368fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2369fef20d9cSFrederic Weisbecker 				num = get_arg(short);
2370fef20d9cSFrederic Weisbecker 				break;
2371fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
23724370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
2373fef20d9cSFrederic Weisbecker 				break;
2374fef20d9cSFrederic Weisbecker 			default:
2375fef20d9cSFrederic Weisbecker 				num = get_arg(int);
23764370aa4aSLai Jiangshan 			}
2377fef20d9cSFrederic Weisbecker 
2378fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
2379d4be151bSAndré Goddard Rosa 		} /* default: */
2380d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
2381d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
2382fef20d9cSFrederic Weisbecker 
23834370aa4aSLai Jiangshan 	if (size > 0) {
23844370aa4aSLai Jiangshan 		if (str < end)
23854370aa4aSLai Jiangshan 			*str = '\0';
23864370aa4aSLai Jiangshan 		else
23874370aa4aSLai Jiangshan 			end[-1] = '\0';
23884370aa4aSLai Jiangshan 	}
2389fef20d9cSFrederic Weisbecker 
23904370aa4aSLai Jiangshan #undef get_arg
23914370aa4aSLai Jiangshan 
23924370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
23934370aa4aSLai Jiangshan 	return str - buf;
23944370aa4aSLai Jiangshan }
23954370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
23964370aa4aSLai Jiangshan 
23974370aa4aSLai Jiangshan /**
23984370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
23994370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
24004370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
24014370aa4aSLai Jiangshan  * @fmt: The format string to use
24024370aa4aSLai Jiangshan  * @...: Arguments for the format string
24034370aa4aSLai Jiangshan  *
24044370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
24054370aa4aSLai Jiangshan  * into @bin_buf.
24064370aa4aSLai Jiangshan  */
24074370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
24084370aa4aSLai Jiangshan {
24094370aa4aSLai Jiangshan 	va_list args;
24104370aa4aSLai Jiangshan 	int ret;
24114370aa4aSLai Jiangshan 
24124370aa4aSLai Jiangshan 	va_start(args, fmt);
24134370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
24144370aa4aSLai Jiangshan 	va_end(args);
24157b9186f5SAndré Goddard Rosa 
24164370aa4aSLai Jiangshan 	return ret;
24174370aa4aSLai Jiangshan }
24184370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
24194370aa4aSLai Jiangshan 
24204370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
24214370aa4aSLai Jiangshan 
24221da177e4SLinus Torvalds /**
24231da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
24241da177e4SLinus Torvalds  * @buf:	input buffer
24251da177e4SLinus Torvalds  * @fmt:	format of buffer
24261da177e4SLinus Torvalds  * @args:	arguments
24271da177e4SLinus Torvalds  */
24281da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
24291da177e4SLinus Torvalds {
24301da177e4SLinus Torvalds 	const char *str = buf;
24311da177e4SLinus Torvalds 	char *next;
24321da177e4SLinus Torvalds 	char digit;
24331da177e4SLinus Torvalds 	int num = 0;
2434ef0658f3SJoe Perches 	u8 qualifier;
243553809751SJan Beulich 	unsigned int base;
243653809751SJan Beulich 	union {
243753809751SJan Beulich 		long long s;
243853809751SJan Beulich 		unsigned long long u;
243953809751SJan Beulich 	} val;
2440ef0658f3SJoe Perches 	s16 field_width;
2441d4be151bSAndré Goddard Rosa 	bool is_sign;
24421da177e4SLinus Torvalds 
2443da99075cSJan Beulich 	while (*fmt) {
24441da177e4SLinus Torvalds 		/* skip any white space in format */
24451da177e4SLinus Torvalds 		/* white space in format matchs any amount of
24461da177e4SLinus Torvalds 		 * white space, including none, in the input.
24471da177e4SLinus Torvalds 		 */
24481da177e4SLinus Torvalds 		if (isspace(*fmt)) {
2449e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
2450e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
24511da177e4SLinus Torvalds 		}
24521da177e4SLinus Torvalds 
24531da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
24541da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
24551da177e4SLinus Torvalds 			if (*fmt++ != *str++)
24561da177e4SLinus Torvalds 				break;
24571da177e4SLinus Torvalds 			continue;
24581da177e4SLinus Torvalds 		}
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 		if (!*fmt)
24611da177e4SLinus Torvalds 			break;
24621da177e4SLinus Torvalds 		++fmt;
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds 		/* skip this conversion.
24651da177e4SLinus Torvalds 		 * advance both strings to next white space
24661da177e4SLinus Torvalds 		 */
24671da177e4SLinus Torvalds 		if (*fmt == '*') {
2468da99075cSJan Beulich 			if (!*str)
2469da99075cSJan Beulich 				break;
24708fccae2cSAndy Spencer 			while (!isspace(*fmt) && *fmt != '%' && *fmt)
24711da177e4SLinus Torvalds 				fmt++;
24721da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
24731da177e4SLinus Torvalds 				str++;
24741da177e4SLinus Torvalds 			continue;
24751da177e4SLinus Torvalds 		}
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds 		/* get field width */
24781da177e4SLinus Torvalds 		field_width = -1;
247953809751SJan Beulich 		if (isdigit(*fmt)) {
24801da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
248153809751SJan Beulich 			if (field_width <= 0)
248253809751SJan Beulich 				break;
248353809751SJan Beulich 		}
24841da177e4SLinus Torvalds 
24851da177e4SLinus Torvalds 		/* get conversion qualifier */
24861da177e4SLinus Torvalds 		qualifier = -1;
248775fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
248875fb8f26SAndy Shevchenko 		    _tolower(*fmt) == 'z') {
24891da177e4SLinus Torvalds 			qualifier = *fmt++;
24901da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
24911da177e4SLinus Torvalds 				if (qualifier == 'h') {
24921da177e4SLinus Torvalds 					qualifier = 'H';
24931da177e4SLinus Torvalds 					fmt++;
24941da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
24951da177e4SLinus Torvalds 					qualifier = 'L';
24961da177e4SLinus Torvalds 					fmt++;
24971da177e4SLinus Torvalds 				}
24981da177e4SLinus Torvalds 			}
24991da177e4SLinus Torvalds 		}
25001da177e4SLinus Torvalds 
2501da99075cSJan Beulich 		if (!*fmt)
2502da99075cSJan Beulich 			break;
2503da99075cSJan Beulich 
2504da99075cSJan Beulich 		if (*fmt == 'n') {
2505da99075cSJan Beulich 			/* return number of characters read so far */
2506da99075cSJan Beulich 			*va_arg(args, int *) = str - buf;
2507da99075cSJan Beulich 			++fmt;
2508da99075cSJan Beulich 			continue;
2509da99075cSJan Beulich 		}
2510da99075cSJan Beulich 
2511da99075cSJan Beulich 		if (!*str)
25121da177e4SLinus Torvalds 			break;
25131da177e4SLinus Torvalds 
2514d4be151bSAndré Goddard Rosa 		base = 10;
25153f623ebaSFabian Frederick 		is_sign = false;
2516d4be151bSAndré Goddard Rosa 
25171da177e4SLinus Torvalds 		switch (*fmt++) {
25181da177e4SLinus Torvalds 		case 'c':
25191da177e4SLinus Torvalds 		{
25201da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
25211da177e4SLinus Torvalds 			if (field_width == -1)
25221da177e4SLinus Torvalds 				field_width = 1;
25231da177e4SLinus Torvalds 			do {
25241da177e4SLinus Torvalds 				*s++ = *str++;
25251da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
25261da177e4SLinus Torvalds 			num++;
25271da177e4SLinus Torvalds 		}
25281da177e4SLinus Torvalds 		continue;
25291da177e4SLinus Torvalds 		case 's':
25301da177e4SLinus Torvalds 		{
25311da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
25321da177e4SLinus Torvalds 			if (field_width == -1)
25334be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
25341da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
2535e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds 			/* now copy until next white space */
25387b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
25391da177e4SLinus Torvalds 				*s++ = *str++;
25401da177e4SLinus Torvalds 			*s = '\0';
25411da177e4SLinus Torvalds 			num++;
25421da177e4SLinus Torvalds 		}
25431da177e4SLinus Torvalds 		continue;
25441da177e4SLinus Torvalds 		case 'o':
25451da177e4SLinus Torvalds 			base = 8;
25461da177e4SLinus Torvalds 			break;
25471da177e4SLinus Torvalds 		case 'x':
25481da177e4SLinus Torvalds 		case 'X':
25491da177e4SLinus Torvalds 			base = 16;
25501da177e4SLinus Torvalds 			break;
25511da177e4SLinus Torvalds 		case 'i':
25521da177e4SLinus Torvalds 			base = 0;
25531da177e4SLinus Torvalds 		case 'd':
25543f623ebaSFabian Frederick 			is_sign = true;
25551da177e4SLinus Torvalds 		case 'u':
25561da177e4SLinus Torvalds 			break;
25571da177e4SLinus Torvalds 		case '%':
25581da177e4SLinus Torvalds 			/* looking for '%' in str */
25591da177e4SLinus Torvalds 			if (*str++ != '%')
25601da177e4SLinus Torvalds 				return num;
25611da177e4SLinus Torvalds 			continue;
25621da177e4SLinus Torvalds 		default:
25631da177e4SLinus Torvalds 			/* invalid format; stop here */
25641da177e4SLinus Torvalds 			return num;
25651da177e4SLinus Torvalds 		}
25661da177e4SLinus Torvalds 
25671da177e4SLinus Torvalds 		/* have some sort of integer conversion.
25681da177e4SLinus Torvalds 		 * first, skip white space in buffer.
25691da177e4SLinus Torvalds 		 */
2570e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
25711da177e4SLinus Torvalds 
25721da177e4SLinus Torvalds 		digit = *str;
25731da177e4SLinus Torvalds 		if (is_sign && digit == '-')
25741da177e4SLinus Torvalds 			digit = *(str + 1);
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds 		if (!digit
25771da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
25781da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
25791da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
25801da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
25811da177e4SLinus Torvalds 			break;
25821da177e4SLinus Torvalds 
258353809751SJan Beulich 		if (is_sign)
258453809751SJan Beulich 			val.s = qualifier != 'L' ?
258553809751SJan Beulich 				simple_strtol(str, &next, base) :
258653809751SJan Beulich 				simple_strtoll(str, &next, base);
258753809751SJan Beulich 		else
258853809751SJan Beulich 			val.u = qualifier != 'L' ?
258953809751SJan Beulich 				simple_strtoul(str, &next, base) :
259053809751SJan Beulich 				simple_strtoull(str, &next, base);
259153809751SJan Beulich 
259253809751SJan Beulich 		if (field_width > 0 && next - str > field_width) {
259353809751SJan Beulich 			if (base == 0)
259453809751SJan Beulich 				_parse_integer_fixup_radix(str, &base);
259553809751SJan Beulich 			while (next - str > field_width) {
259653809751SJan Beulich 				if (is_sign)
259753809751SJan Beulich 					val.s = div_s64(val.s, base);
259853809751SJan Beulich 				else
259953809751SJan Beulich 					val.u = div_u64(val.u, base);
260053809751SJan Beulich 				--next;
260153809751SJan Beulich 			}
260253809751SJan Beulich 		}
260353809751SJan Beulich 
26041da177e4SLinus Torvalds 		switch (qualifier) {
26051da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
260653809751SJan Beulich 			if (is_sign)
260753809751SJan Beulich 				*va_arg(args, signed char *) = val.s;
260853809751SJan Beulich 			else
260953809751SJan Beulich 				*va_arg(args, unsigned char *) = val.u;
26101da177e4SLinus Torvalds 			break;
26111da177e4SLinus Torvalds 		case 'h':
261253809751SJan Beulich 			if (is_sign)
261353809751SJan Beulich 				*va_arg(args, short *) = val.s;
261453809751SJan Beulich 			else
261553809751SJan Beulich 				*va_arg(args, unsigned short *) = val.u;
26161da177e4SLinus Torvalds 			break;
26171da177e4SLinus Torvalds 		case 'l':
261853809751SJan Beulich 			if (is_sign)
261953809751SJan Beulich 				*va_arg(args, long *) = val.s;
262053809751SJan Beulich 			else
262153809751SJan Beulich 				*va_arg(args, unsigned long *) = val.u;
26221da177e4SLinus Torvalds 			break;
26231da177e4SLinus Torvalds 		case 'L':
262453809751SJan Beulich 			if (is_sign)
262553809751SJan Beulich 				*va_arg(args, long long *) = val.s;
262653809751SJan Beulich 			else
262753809751SJan Beulich 				*va_arg(args, unsigned long long *) = val.u;
26281da177e4SLinus Torvalds 			break;
26291da177e4SLinus Torvalds 		case 'Z':
26301da177e4SLinus Torvalds 		case 'z':
263153809751SJan Beulich 			*va_arg(args, size_t *) = val.u;
26321da177e4SLinus Torvalds 			break;
26331da177e4SLinus Torvalds 		default:
263453809751SJan Beulich 			if (is_sign)
263553809751SJan Beulich 				*va_arg(args, int *) = val.s;
263653809751SJan Beulich 			else
263753809751SJan Beulich 				*va_arg(args, unsigned int *) = val.u;
26381da177e4SLinus Torvalds 			break;
26391da177e4SLinus Torvalds 		}
26401da177e4SLinus Torvalds 		num++;
26411da177e4SLinus Torvalds 
26421da177e4SLinus Torvalds 		if (!next)
26431da177e4SLinus Torvalds 			break;
26441da177e4SLinus Torvalds 		str = next;
26451da177e4SLinus Torvalds 	}
2646c6b40d16SJohannes Berg 
26471da177e4SLinus Torvalds 	return num;
26481da177e4SLinus Torvalds }
26491da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
26501da177e4SLinus Torvalds 
26511da177e4SLinus Torvalds /**
26521da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
26531da177e4SLinus Torvalds  * @buf:	input buffer
26541da177e4SLinus Torvalds  * @fmt:	formatting of buffer
26551da177e4SLinus Torvalds  * @...:	resulting arguments
26561da177e4SLinus Torvalds  */
26571da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
26581da177e4SLinus Torvalds {
26591da177e4SLinus Torvalds 	va_list args;
26601da177e4SLinus Torvalds 	int i;
26611da177e4SLinus Torvalds 
26621da177e4SLinus Torvalds 	va_start(args, fmt);
26631da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
26641da177e4SLinus Torvalds 	va_end(args);
26657b9186f5SAndré Goddard Rosa 
26661da177e4SLinus Torvalds 	return i;
26671da177e4SLinus Torvalds }
26681da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
2669