xref: /openbmc/linux/lib/vsprintf.c (revision 1067964305df131ede2c08c2f3c9b3892640f1c6)
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>
298a27f7c9SJoe Perches #include <net/addrconf.h>
301da177e4SLinus Torvalds 
314e57b681STim Schmielau #include <asm/page.h>		/* for PAGE_SIZE */
32deac93dfSJames Bottomley #include <asm/sections.h>	/* for dereference_function_descriptor() */
331da177e4SLinus Torvalds 
341dff46d6SAlexey Dobriyan #include "kstrtox.h"
35aa46a63eSHarvey Harrison 
361da177e4SLinus Torvalds /**
371da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
381da177e4SLinus Torvalds  * @cp: The start of the string
391da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
401da177e4SLinus Torvalds  * @base: The number base to use
41462e4711SEldad Zack  *
42462e4711SEldad Zack  * This function is obsolete. Please use kstrtoull instead.
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
451da177e4SLinus Torvalds {
461dff46d6SAlexey Dobriyan 	unsigned long long result;
471dff46d6SAlexey Dobriyan 	unsigned int rv;
481da177e4SLinus Torvalds 
491dff46d6SAlexey Dobriyan 	cp = _parse_integer_fixup_radix(cp, &base);
501dff46d6SAlexey Dobriyan 	rv = _parse_integer(cp, base, &result);
511dff46d6SAlexey Dobriyan 	/* FIXME */
521dff46d6SAlexey Dobriyan 	cp += (rv & ~KSTRTOX_OVERFLOW);
53aa46a63eSHarvey Harrison 
541da177e4SLinus Torvalds 	if (endp)
551da177e4SLinus Torvalds 		*endp = (char *)cp;
567b9186f5SAndré Goddard Rosa 
571da177e4SLinus Torvalds 	return result;
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds /**
62922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
63922ac25cSAndré Goddard Rosa  * @cp: The start of the string
64922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
65922ac25cSAndré Goddard Rosa  * @base: The number base to use
66462e4711SEldad Zack  *
67462e4711SEldad Zack  * This function is obsolete. Please use kstrtoul instead.
68922ac25cSAndré Goddard Rosa  */
69922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
70922ac25cSAndré Goddard Rosa {
71922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
72922ac25cSAndré Goddard Rosa }
73922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
74922ac25cSAndré Goddard Rosa 
75922ac25cSAndré Goddard Rosa /**
76922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
77922ac25cSAndré Goddard Rosa  * @cp: The start of the string
78922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
79922ac25cSAndré Goddard Rosa  * @base: The number base to use
80462e4711SEldad Zack  *
81462e4711SEldad Zack  * This function is obsolete. Please use kstrtol instead.
82922ac25cSAndré Goddard Rosa  */
83922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
84922ac25cSAndré Goddard Rosa {
85922ac25cSAndré Goddard Rosa 	if (*cp == '-')
86922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
87922ac25cSAndré Goddard Rosa 
88922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
89922ac25cSAndré Goddard Rosa }
90922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
91922ac25cSAndré Goddard Rosa 
92922ac25cSAndré Goddard Rosa /**
931da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
941da177e4SLinus Torvalds  * @cp: The start of the string
951da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
961da177e4SLinus Torvalds  * @base: The number base to use
97462e4711SEldad Zack  *
98462e4711SEldad Zack  * This function is obsolete. Please use kstrtoll instead.
991da177e4SLinus Torvalds  */
1001da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	if (*cp == '-')
1031da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1047b9186f5SAndré Goddard Rosa 
1051da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1061da177e4SLinus Torvalds }
10798d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1081da177e4SLinus Torvalds 
109cf3b429bSJoe Perches static noinline_for_stack
110cf3b429bSJoe Perches int skip_atoi(const char **s)
1111da177e4SLinus Torvalds {
1121da177e4SLinus Torvalds 	int i = 0;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	while (isdigit(**s))
1151da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
1167b9186f5SAndré Goddard Rosa 
1171da177e4SLinus Torvalds 	return i;
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
1204277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used
1214277eeddSDenis Vlasenko  * for /proc and /sys data. This directly impacts e.g. top performance
1224277eeddSDenis Vlasenko  * with many processes running. We optimize it for speed
123133fd9f5SDenys Vlasenko  * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
124133fd9f5SDenys Vlasenko  * (with permission from the author, Douglas W. Jones).
125133fd9f5SDenys Vlasenko  */
1264277eeddSDenis Vlasenko 
127133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
128133fd9f5SDenys Vlasenko /* Formats correctly any integer in [0, 999999999] */
129cf3b429bSJoe Perches static noinline_for_stack
130133fd9f5SDenys Vlasenko char *put_dec_full9(char *buf, unsigned q)
1314277eeddSDenis Vlasenko {
132133fd9f5SDenys Vlasenko 	unsigned r;
1334277eeddSDenis Vlasenko 
1347b9186f5SAndré Goddard Rosa 	/*
1357b9186f5SAndré Goddard Rosa 	 * Possible ways to approx. divide by 10
136133fd9f5SDenys Vlasenko 	 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
137133fd9f5SDenys Vlasenko 	 * (x * 0xcccd) >> 19     x <      81920 (x < 262149 when 64-bit mul)
138133fd9f5SDenys Vlasenko 	 * (x * 0x6667) >> 18     x <      43699
139133fd9f5SDenys Vlasenko 	 * (x * 0x3334) >> 17     x <      16389
140133fd9f5SDenys Vlasenko 	 * (x * 0x199a) >> 16     x <      16389
141133fd9f5SDenys Vlasenko 	 * (x * 0x0ccd) >> 15     x <      16389
142133fd9f5SDenys Vlasenko 	 * (x * 0x0667) >> 14     x <       2739
143133fd9f5SDenys Vlasenko 	 * (x * 0x0334) >> 13     x <       1029
144133fd9f5SDenys Vlasenko 	 * (x * 0x019a) >> 12     x <       1029
145133fd9f5SDenys Vlasenko 	 * (x * 0x00cd) >> 11     x <       1029 shorter code than * 0x67 (on i386)
146133fd9f5SDenys Vlasenko 	 * (x * 0x0067) >> 10     x <        179
147133fd9f5SDenys Vlasenko 	 * (x * 0x0034) >>  9     x <         69 same
148133fd9f5SDenys Vlasenko 	 * (x * 0x001a) >>  8     x <         69 same
149133fd9f5SDenys Vlasenko 	 * (x * 0x000d) >>  7     x <         69 same, shortest code (on i386)
150133fd9f5SDenys Vlasenko 	 * (x * 0x0007) >>  6     x <         19
151133fd9f5SDenys Vlasenko 	 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
1527b9186f5SAndré Goddard Rosa 	 */
153133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
154133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 1 */
155133fd9f5SDenys Vlasenko 	q      = (r * (uint64_t)0x1999999a) >> 32;
156133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 2 */
157133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
158133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 3 */
159133fd9f5SDenys Vlasenko 	q      = (r * (uint64_t)0x1999999a) >> 32;
160133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 4 */
161133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
162133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 5 */
163133fd9f5SDenys Vlasenko 	/* Now value is under 10000, can avoid 64-bit multiply */
164133fd9f5SDenys Vlasenko 	q      = (r * 0x199a) >> 16;
165133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q)  + '0'; /* 6 */
166133fd9f5SDenys Vlasenko 	r      = (q * 0xcd) >> 11;
167133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r)  + '0'; /* 7 */
168133fd9f5SDenys Vlasenko 	q      = (r * 0xcd) >> 11;
169133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 8 */
170133fd9f5SDenys Vlasenko 	*buf++ = q + '0'; /* 9 */
171133fd9f5SDenys Vlasenko 	return buf;
172133fd9f5SDenys Vlasenko }
173133fd9f5SDenys Vlasenko #endif
1744277eeddSDenis Vlasenko 
175133fd9f5SDenys Vlasenko /* Similar to above but do not pad with zeros.
176133fd9f5SDenys Vlasenko  * Code can be easily arranged to print 9 digits too, but our callers
177133fd9f5SDenys Vlasenko  * always call put_dec_full9() instead when the number has 9 decimal digits.
178133fd9f5SDenys Vlasenko  */
179133fd9f5SDenys Vlasenko static noinline_for_stack
180133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r)
181133fd9f5SDenys Vlasenko {
182133fd9f5SDenys Vlasenko 	unsigned q;
1834277eeddSDenis Vlasenko 
184133fd9f5SDenys Vlasenko 	/* Copy of previous function's body with added early returns */
185cb239d0aSGeorge Spelvin 	while (r >= 10000) {
186cb239d0aSGeorge Spelvin 		q = r + '0';
187cb239d0aSGeorge Spelvin 		r  = (r * (uint64_t)0x1999999a) >> 32;
188cb239d0aSGeorge Spelvin 		*buf++ = q - 10*r;
189cb239d0aSGeorge Spelvin 	}
190cb239d0aSGeorge Spelvin 
191f4000516SGeorge Spelvin 	q      = (r * 0x199a) >> 16;	/* r <= 9999 */
192f4000516SGeorge Spelvin 	*buf++ = (r - 10 * q)  + '0';
193133fd9f5SDenys Vlasenko 	if (q == 0)
194133fd9f5SDenys Vlasenko 		return buf;
195f4000516SGeorge Spelvin 	r      = (q * 0xcd) >> 11;	/* q <= 999 */
196f4000516SGeorge Spelvin 	*buf++ = (q - 10 * r)  + '0';
197133fd9f5SDenys Vlasenko 	if (r == 0)
198133fd9f5SDenys Vlasenko 		return buf;
199f4000516SGeorge Spelvin 	q      = (r * 0xcd) >> 11;	/* r <= 99 */
200f4000516SGeorge Spelvin 	*buf++ = (r - 10 * q) + '0';
201133fd9f5SDenys Vlasenko 	if (q == 0)
202133fd9f5SDenys Vlasenko 		return buf;
203f4000516SGeorge Spelvin 	*buf++ = q + '0';		 /* q <= 9 */
204133fd9f5SDenys Vlasenko 	return buf;
205133fd9f5SDenys Vlasenko }
206133fd9f5SDenys Vlasenko 
207133fd9f5SDenys Vlasenko /* There are two algorithms to print larger numbers.
208133fd9f5SDenys Vlasenko  * One is generic: divide by 1000000000 and repeatedly print
209133fd9f5SDenys Vlasenko  * groups of (up to) 9 digits. It's conceptually simple,
210133fd9f5SDenys Vlasenko  * but requires a (unsigned long long) / 1000000000 division.
211133fd9f5SDenys Vlasenko  *
212133fd9f5SDenys Vlasenko  * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
213133fd9f5SDenys Vlasenko  * manipulates them cleverly and generates groups of 4 decimal digits.
214133fd9f5SDenys Vlasenko  * It so happens that it does NOT require long long division.
215133fd9f5SDenys Vlasenko  *
216133fd9f5SDenys Vlasenko  * If long is > 32 bits, division of 64-bit values is relatively easy,
217133fd9f5SDenys Vlasenko  * and we will use the first algorithm.
218133fd9f5SDenys Vlasenko  * If long long is > 64 bits (strange architecture with VERY large long long),
219133fd9f5SDenys Vlasenko  * second algorithm can't be used, and we again use the first one.
220133fd9f5SDenys Vlasenko  *
221133fd9f5SDenys Vlasenko  * Else (if long is 32 bits and long long is 64 bits) we use second one.
222133fd9f5SDenys Vlasenko  */
223133fd9f5SDenys Vlasenko 
224133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
225133fd9f5SDenys Vlasenko 
226133fd9f5SDenys Vlasenko /* First algorithm: generic */
227133fd9f5SDenys Vlasenko 
228133fd9f5SDenys Vlasenko static
229133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
230133fd9f5SDenys Vlasenko {
231133fd9f5SDenys Vlasenko 	if (n >= 100*1000*1000) {
232133fd9f5SDenys Vlasenko 		while (n >= 1000*1000*1000)
233133fd9f5SDenys Vlasenko 			buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
234133fd9f5SDenys Vlasenko 		if (n >= 100*1000*1000)
235133fd9f5SDenys Vlasenko 			return put_dec_full9(buf, n);
236133fd9f5SDenys Vlasenko 	}
237133fd9f5SDenys Vlasenko 	return put_dec_trunc8(buf, n);
238133fd9f5SDenys Vlasenko }
239133fd9f5SDenys Vlasenko 
240133fd9f5SDenys Vlasenko #else
241133fd9f5SDenys Vlasenko 
242133fd9f5SDenys Vlasenko /* Second algorithm: valid only for 64-bit long longs */
243133fd9f5SDenys Vlasenko 
244e49317d4SGeorge Spelvin /* See comment in put_dec_full9 for choice of constants */
245133fd9f5SDenys Vlasenko static noinline_for_stack
2462359172aSGeorge Spelvin void put_dec_full4(char *buf, unsigned q)
247133fd9f5SDenys Vlasenko {
248133fd9f5SDenys Vlasenko 	unsigned r;
249e49317d4SGeorge Spelvin 	r      = (q * 0xccd) >> 15;
2502359172aSGeorge Spelvin 	buf[0] = (q - 10 * r) + '0';
251e49317d4SGeorge Spelvin 	q      = (r * 0xcd) >> 11;
2522359172aSGeorge Spelvin 	buf[1] = (r - 10 * q)  + '0';
253133fd9f5SDenys Vlasenko 	r      = (q * 0xcd) >> 11;
2542359172aSGeorge Spelvin 	buf[2] = (q - 10 * r)  + '0';
2552359172aSGeorge Spelvin 	buf[3] = r + '0';
2562359172aSGeorge Spelvin }
2572359172aSGeorge Spelvin 
2582359172aSGeorge Spelvin /*
2592359172aSGeorge Spelvin  * Call put_dec_full4 on x % 10000, return x / 10000.
2602359172aSGeorge Spelvin  * The approximation x/10000 == (x * 0x346DC5D7) >> 43
2612359172aSGeorge Spelvin  * holds for all x < 1,128,869,999.  The largest value this
2622359172aSGeorge Spelvin  * helper will ever be asked to convert is 1,125,520,955.
2632359172aSGeorge Spelvin  * (d1 in the put_dec code, assuming n is all-ones).
2642359172aSGeorge Spelvin  */
2652359172aSGeorge Spelvin static
2662359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x)
2672359172aSGeorge Spelvin {
2682359172aSGeorge Spelvin         uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
2692359172aSGeorge Spelvin 
2702359172aSGeorge Spelvin         put_dec_full4(buf, x - q * 10000);
2712359172aSGeorge Spelvin         return q;
272133fd9f5SDenys Vlasenko }
273133fd9f5SDenys Vlasenko 
274133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at
275133fd9f5SDenys Vlasenko  * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
276133fd9f5SDenys Vlasenko  * (with permission from the author).
277133fd9f5SDenys Vlasenko  * Performs no 64-bit division and hence should be fast on 32-bit machines.
278133fd9f5SDenys Vlasenko  */
279133fd9f5SDenys Vlasenko static
280133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
281133fd9f5SDenys Vlasenko {
282133fd9f5SDenys Vlasenko 	uint32_t d3, d2, d1, q, h;
283133fd9f5SDenys Vlasenko 
284133fd9f5SDenys Vlasenko 	if (n < 100*1000*1000)
285133fd9f5SDenys Vlasenko 		return put_dec_trunc8(buf, n);
286133fd9f5SDenys Vlasenko 
287133fd9f5SDenys Vlasenko 	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
288133fd9f5SDenys Vlasenko 	h   = (n >> 32);
289133fd9f5SDenys Vlasenko 	d2  = (h      ) & 0xffff;
290133fd9f5SDenys Vlasenko 	d3  = (h >> 16); /* implicit "& 0xffff" */
291133fd9f5SDenys Vlasenko 
292133fd9f5SDenys Vlasenko 	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
2932359172aSGeorge Spelvin 	q = put_dec_helper4(buf, q);
294133fd9f5SDenys Vlasenko 
2952359172aSGeorge Spelvin 	q += 7671 * d3 + 9496 * d2 + 6 * d1;
2962359172aSGeorge Spelvin 	q = put_dec_helper4(buf+4, q);
297133fd9f5SDenys Vlasenko 
2982359172aSGeorge Spelvin 	q += 4749 * d3 + 42 * d2;
2992359172aSGeorge Spelvin 	q = put_dec_helper4(buf+8, q);
300133fd9f5SDenys Vlasenko 
3012359172aSGeorge Spelvin 	q += 281 * d3;
3022359172aSGeorge Spelvin 	buf += 12;
3032359172aSGeorge Spelvin 	if (q)
3042359172aSGeorge Spelvin 		buf = put_dec_trunc8(buf, q);
3052359172aSGeorge Spelvin 	else while (buf[-1] == '0')
306133fd9f5SDenys Vlasenko 		--buf;
3077b9186f5SAndré Goddard Rosa 
3084277eeddSDenis Vlasenko 	return buf;
3094277eeddSDenis Vlasenko }
310133fd9f5SDenys Vlasenko 
311133fd9f5SDenys Vlasenko #endif
3124277eeddSDenis Vlasenko 
3131ac101a5SKAMEZAWA Hiroyuki /*
3141ac101a5SKAMEZAWA Hiroyuki  * Convert passed number to decimal string.
3151ac101a5SKAMEZAWA Hiroyuki  * Returns the length of string.  On buffer overflow, returns 0.
3161ac101a5SKAMEZAWA Hiroyuki  *
3171ac101a5SKAMEZAWA Hiroyuki  * If speed is not important, use snprintf(). It's easy to read the code.
3181ac101a5SKAMEZAWA Hiroyuki  */
3191ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num)
3201ac101a5SKAMEZAWA Hiroyuki {
321133fd9f5SDenys Vlasenko 	char tmp[sizeof(num) * 3];
3221ac101a5SKAMEZAWA Hiroyuki 	int idx, len;
3231ac101a5SKAMEZAWA Hiroyuki 
324133fd9f5SDenys Vlasenko 	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
325133fd9f5SDenys Vlasenko 	if (num <= 9) {
326133fd9f5SDenys Vlasenko 		tmp[0] = '0' + num;
327133fd9f5SDenys Vlasenko 		len = 1;
328133fd9f5SDenys Vlasenko 	} else {
3291ac101a5SKAMEZAWA Hiroyuki 		len = put_dec(tmp, num) - tmp;
330133fd9f5SDenys Vlasenko 	}
3311ac101a5SKAMEZAWA Hiroyuki 
3321ac101a5SKAMEZAWA Hiroyuki 	if (len > size)
3331ac101a5SKAMEZAWA Hiroyuki 		return 0;
3341ac101a5SKAMEZAWA Hiroyuki 	for (idx = 0; idx < len; ++idx)
3351ac101a5SKAMEZAWA Hiroyuki 		buf[idx] = tmp[len - idx - 1];
3361ac101a5SKAMEZAWA Hiroyuki 	return len;
3371ac101a5SKAMEZAWA Hiroyuki }
3381ac101a5SKAMEZAWA Hiroyuki 
3391da177e4SLinus Torvalds #define ZEROPAD	1		/* pad with zero */
3401da177e4SLinus Torvalds #define SIGN	2		/* unsigned/signed long */
3411da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3421da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
3431da177e4SLinus Torvalds #define LEFT	16		/* left justified */
344b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
345b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
3461da177e4SLinus Torvalds 
347fef20d9cSFrederic Weisbecker enum format_type {
348fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
349ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
350fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
351fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
352fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
353fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
354fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
355fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
356fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
357fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
358fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
359a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
360a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
361fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
362fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
363fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
364fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
365fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NRCHARS,
366fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
367fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
368fef20d9cSFrederic Weisbecker };
369fef20d9cSFrederic Weisbecker 
370fef20d9cSFrederic Weisbecker struct printf_spec {
3714e310fdaSJoe Perches 	u8	type;		/* format_type enum */
372ef0658f3SJoe Perches 	u8	flags;		/* flags to number() */
3734e310fdaSJoe Perches 	u8	base;		/* number base, 8, 10 or 16 only */
3744e310fdaSJoe Perches 	u8	qualifier;	/* number qualifier, one of 'hHlLtzZ' */
3754e310fdaSJoe Perches 	s16	field_width;	/* width of output field */
3764e310fdaSJoe Perches 	s16	precision;	/* # of digits/chars */
377fef20d9cSFrederic Weisbecker };
378fef20d9cSFrederic Weisbecker 
379cf3b429bSJoe Perches static noinline_for_stack
380cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
381fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
3821da177e4SLinus Torvalds {
3839b706aeeSDenys Vlasenko 	/* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
3849b706aeeSDenys Vlasenko 	static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
3859b706aeeSDenys Vlasenko 
3869b706aeeSDenys Vlasenko 	char tmp[66];
3879b706aeeSDenys Vlasenko 	char sign;
3889b706aeeSDenys Vlasenko 	char locase;
389fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
3901da177e4SLinus Torvalds 	int i;
3917c203422SPierre Carrier 	bool is_zero = num == 0LL;
3921da177e4SLinus Torvalds 
3939b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
3949b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
395fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
396fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
397fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
3981da177e4SLinus Torvalds 	sign = 0;
399fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4001da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4011da177e4SLinus Torvalds 			sign = '-';
4021da177e4SLinus Torvalds 			num = -(signed long long)num;
403fef20d9cSFrederic Weisbecker 			spec.field_width--;
404fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4051da177e4SLinus Torvalds 			sign = '+';
406fef20d9cSFrederic Weisbecker 			spec.field_width--;
407fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4081da177e4SLinus Torvalds 			sign = ' ';
409fef20d9cSFrederic Weisbecker 			spec.field_width--;
4101da177e4SLinus Torvalds 		}
4111da177e4SLinus Torvalds 	}
412b39a7340SDenis Vlasenko 	if (need_pfx) {
413fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
4147c203422SPierre Carrier 			spec.field_width -= 2;
4157c203422SPierre Carrier 		else if (!is_zero)
416fef20d9cSFrederic Weisbecker 			spec.field_width--;
4171da177e4SLinus Torvalds 	}
418b39a7340SDenis Vlasenko 
419b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4201da177e4SLinus Torvalds 	i = 0;
421133fd9f5SDenys Vlasenko 	if (num < spec.base)
422133fd9f5SDenys Vlasenko 		tmp[i++] = digits[num] | locase;
4234277eeddSDenis Vlasenko 	/* Generic code, for any base:
4244277eeddSDenis Vlasenko 	else do {
4259b706aeeSDenys Vlasenko 		tmp[i++] = (digits[do_div(num,base)] | locase);
4264277eeddSDenis Vlasenko 	} while (num != 0);
4274277eeddSDenis Vlasenko 	*/
428fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
429fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
430b39a7340SDenis Vlasenko 		int shift = 3;
4317b9186f5SAndré Goddard Rosa 
4327b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4337b9186f5SAndré Goddard Rosa 			shift = 4;
434b39a7340SDenis Vlasenko 		do {
4359b706aeeSDenys Vlasenko 			tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
436b39a7340SDenis Vlasenko 			num >>= shift;
437b39a7340SDenis Vlasenko 		} while (num);
4384277eeddSDenis Vlasenko 	} else { /* base 10 */
4394277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4404277eeddSDenis Vlasenko 	}
441b39a7340SDenis Vlasenko 
442b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
443fef20d9cSFrederic Weisbecker 	if (i > spec.precision)
444fef20d9cSFrederic Weisbecker 		spec.precision = i;
445b39a7340SDenis Vlasenko 	/* leading space padding */
446fef20d9cSFrederic Weisbecker 	spec.field_width -= spec.precision;
447fef20d9cSFrederic Weisbecker 	if (!(spec.flags & (ZEROPAD+LEFT))) {
448fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
449f796937aSJeremy Fitzhardinge 			if (buf < end)
4501da177e4SLinus Torvalds 				*buf = ' ';
4511da177e4SLinus Torvalds 			++buf;
4521da177e4SLinus Torvalds 		}
4531da177e4SLinus Torvalds 	}
454b39a7340SDenis Vlasenko 	/* sign */
4551da177e4SLinus Torvalds 	if (sign) {
456f796937aSJeremy Fitzhardinge 		if (buf < end)
4571da177e4SLinus Torvalds 			*buf = sign;
4581da177e4SLinus Torvalds 		++buf;
4591da177e4SLinus Torvalds 	}
460b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
461b39a7340SDenis Vlasenko 	if (need_pfx) {
4627c203422SPierre Carrier 		if (spec.base == 16 || !is_zero) {
463f796937aSJeremy Fitzhardinge 			if (buf < end)
4641da177e4SLinus Torvalds 				*buf = '0';
4651da177e4SLinus Torvalds 			++buf;
4667c203422SPierre Carrier 		}
467fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
468f796937aSJeremy Fitzhardinge 			if (buf < end)
4699b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
4701da177e4SLinus Torvalds 			++buf;
4711da177e4SLinus Torvalds 		}
4721da177e4SLinus Torvalds 	}
473b39a7340SDenis Vlasenko 	/* zero or space padding */
474fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
475fef20d9cSFrederic Weisbecker 		char c = (spec.flags & ZEROPAD) ? '0' : ' ';
476fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
477f796937aSJeremy Fitzhardinge 			if (buf < end)
4781da177e4SLinus Torvalds 				*buf = c;
4791da177e4SLinus Torvalds 			++buf;
4801da177e4SLinus Torvalds 		}
4811da177e4SLinus Torvalds 	}
482b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
483fef20d9cSFrederic Weisbecker 	while (i <= --spec.precision) {
484f796937aSJeremy Fitzhardinge 		if (buf < end)
4851da177e4SLinus Torvalds 			*buf = '0';
4861da177e4SLinus Torvalds 		++buf;
4871da177e4SLinus Torvalds 	}
488b39a7340SDenis Vlasenko 	/* actual digits of result */
489b39a7340SDenis Vlasenko 	while (--i >= 0) {
490f796937aSJeremy Fitzhardinge 		if (buf < end)
4911da177e4SLinus Torvalds 			*buf = tmp[i];
4921da177e4SLinus Torvalds 		++buf;
4931da177e4SLinus Torvalds 	}
494b39a7340SDenis Vlasenko 	/* trailing space padding */
495fef20d9cSFrederic Weisbecker 	while (--spec.field_width >= 0) {
496f796937aSJeremy Fitzhardinge 		if (buf < end)
4971da177e4SLinus Torvalds 			*buf = ' ';
4981da177e4SLinus Torvalds 		++buf;
4991da177e4SLinus Torvalds 	}
5007b9186f5SAndré Goddard Rosa 
5011da177e4SLinus Torvalds 	return buf;
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds 
504cf3b429bSJoe Perches static noinline_for_stack
505cf3b429bSJoe Perches char *string(char *buf, char *end, const char *s, struct printf_spec spec)
5060f9bfa56SLinus Torvalds {
5070f9bfa56SLinus Torvalds 	int len, i;
5080f9bfa56SLinus Torvalds 
5090f9bfa56SLinus Torvalds 	if ((unsigned long)s < PAGE_SIZE)
5100f4f81dcSAndré Goddard Rosa 		s = "(null)";
5110f9bfa56SLinus Torvalds 
512fef20d9cSFrederic Weisbecker 	len = strnlen(s, spec.precision);
5130f9bfa56SLinus Torvalds 
514fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
515fef20d9cSFrederic Weisbecker 		while (len < spec.field_width--) {
5160f9bfa56SLinus Torvalds 			if (buf < end)
5170f9bfa56SLinus Torvalds 				*buf = ' ';
5180f9bfa56SLinus Torvalds 			++buf;
5190f9bfa56SLinus Torvalds 		}
5200f9bfa56SLinus Torvalds 	}
5210f9bfa56SLinus Torvalds 	for (i = 0; i < len; ++i) {
5220f9bfa56SLinus Torvalds 		if (buf < end)
5230f9bfa56SLinus Torvalds 			*buf = *s;
5240f9bfa56SLinus Torvalds 		++buf; ++s;
5250f9bfa56SLinus Torvalds 	}
526fef20d9cSFrederic Weisbecker 	while (len < spec.field_width--) {
5270f9bfa56SLinus Torvalds 		if (buf < end)
5280f9bfa56SLinus Torvalds 			*buf = ' ';
5290f9bfa56SLinus Torvalds 		++buf;
5300f9bfa56SLinus Torvalds 	}
5317b9186f5SAndré Goddard Rosa 
5320f9bfa56SLinus Torvalds 	return buf;
5330f9bfa56SLinus Torvalds }
5340f9bfa56SLinus Torvalds 
535cf3b429bSJoe Perches static noinline_for_stack
536cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
537b0d33c2bSJoe Perches 		    struct printf_spec spec, const char *fmt)
5380fe1ef24SLinus Torvalds {
539b0d33c2bSJoe Perches 	unsigned long value;
5400fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
5410fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
542b0d33c2bSJoe Perches #endif
543b0d33c2bSJoe Perches 
544b0d33c2bSJoe Perches 	if (fmt[1] == 'R')
545b0d33c2bSJoe Perches 		ptr = __builtin_extract_return_addr(ptr);
546b0d33c2bSJoe Perches 	value = (unsigned long)ptr;
547b0d33c2bSJoe Perches 
548b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS
549b0d33c2bSJoe Perches 	if (*fmt == 'B')
5500f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
551b0d33c2bSJoe Perches 	else if (*fmt != 'f' && *fmt != 's')
5520fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
5530c8b946eSFrederic Weisbecker 	else
5544796dd20SStephen Boyd 		sprint_symbol_no_offset(sym, value);
5557b9186f5SAndré Goddard Rosa 
556fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
5570fe1ef24SLinus Torvalds #else
558fef20d9cSFrederic Weisbecker 	spec.field_width = 2 * sizeof(void *);
559fef20d9cSFrederic Weisbecker 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
560fef20d9cSFrederic Weisbecker 	spec.base = 16;
5617b9186f5SAndré Goddard Rosa 
562fef20d9cSFrederic Weisbecker 	return number(buf, end, value, spec);
5630fe1ef24SLinus Torvalds #endif
5640fe1ef24SLinus Torvalds }
5650fe1ef24SLinus Torvalds 
566cf3b429bSJoe Perches static noinline_for_stack
567cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
568fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
569332d2e78SLinus Torvalds {
570332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
57128405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
572332d2e78SLinus Torvalds #endif
573332d2e78SLinus Torvalds 
574332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
57528405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
576332d2e78SLinus Torvalds #endif
5774da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
578fef20d9cSFrederic Weisbecker 		.base = 16,
5794da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
580fef20d9cSFrederic Weisbecker 		.precision = -1,
581fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
582fef20d9cSFrederic Weisbecker 	};
5834da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
5844da0b66cSBjorn Helgaas 		.base = 16,
5854da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
5864da0b66cSBjorn Helgaas 		.precision = -1,
5874da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
5884da0b66cSBjorn Helgaas 	};
5890f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
5900f4050c7SBjorn Helgaas 		.base = 16,
5910f4050c7SBjorn Helgaas 		.field_width = 2,
5920f4050c7SBjorn Helgaas 		.precision = -1,
5930f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
5940f4050c7SBjorn Helgaas 	};
5954da0b66cSBjorn Helgaas 	static const struct printf_spec dec_spec = {
596c91d3376SBjorn Helgaas 		.base = 10,
597c91d3376SBjorn Helgaas 		.precision = -1,
598c91d3376SBjorn Helgaas 		.flags = 0,
599c91d3376SBjorn Helgaas 	};
6004da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
601fd95541eSBjorn Helgaas 		.field_width = -1,
602fd95541eSBjorn Helgaas 		.precision = 10,
603fd95541eSBjorn Helgaas 		.flags = LEFT,
604fd95541eSBjorn Helgaas 	};
6054da0b66cSBjorn Helgaas 	static const struct printf_spec flag_spec = {
606fd95541eSBjorn Helgaas 		.base = 16,
607fd95541eSBjorn Helgaas 		.precision = -1,
608fd95541eSBjorn Helgaas 		.flags = SPECIAL | SMALL,
609fd95541eSBjorn Helgaas 	};
610c7dabef8SBjorn Helgaas 
611c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
612c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
613c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
614c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
6159d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
616c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
617c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
618c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
619c7dabef8SBjorn Helgaas 
620332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
621c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
6224da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
623332d2e78SLinus Torvalds 
624332d2e78SLinus Torvalds 	*p++ = '[';
6254da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
626fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
6274da0b66cSBjorn Helgaas 		specp = &io_spec;
6284da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
629fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
6304da0b66cSBjorn Helgaas 		specp = &mem_spec;
6314da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
632fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
6334da0b66cSBjorn Helgaas 		specp = &dec_spec;
6344da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
635fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
6364da0b66cSBjorn Helgaas 		specp = &dec_spec;
6370f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
6380f4050c7SBjorn Helgaas 		p = string(p, pend, "bus ", str_spec);
6390f4050c7SBjorn Helgaas 		specp = &bus_spec;
6404da0b66cSBjorn Helgaas 	} else {
641c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
6424da0b66cSBjorn Helgaas 		specp = &mem_spec;
643c7dabef8SBjorn Helgaas 		decode = 0;
644fd95541eSBjorn Helgaas 	}
6454da0b66cSBjorn Helgaas 	p = number(p, pend, res->start, *specp);
646c91d3376SBjorn Helgaas 	if (res->start != res->end) {
647332d2e78SLinus Torvalds 		*p++ = '-';
6484da0b66cSBjorn Helgaas 		p = number(p, pend, res->end, *specp);
649c91d3376SBjorn Helgaas 	}
650c7dabef8SBjorn Helgaas 	if (decode) {
651fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
652fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
653fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
654fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
6559d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
6569d7cca04SBjorn Helgaas 			p = string(p, pend, " window", str_spec);
657fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
658fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
659c7dabef8SBjorn Helgaas 	} else {
660fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
661c7dabef8SBjorn Helgaas 		p = number(p, pend, res->flags, flag_spec);
662fd95541eSBjorn Helgaas 	}
663332d2e78SLinus Torvalds 	*p++ = ']';
664c7dabef8SBjorn Helgaas 	*p = '\0';
665332d2e78SLinus Torvalds 
666fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
667332d2e78SLinus Torvalds }
668332d2e78SLinus Torvalds 
669cf3b429bSJoe Perches static noinline_for_stack
67031550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
67131550a16SAndy Shevchenko 		 const char *fmt)
67231550a16SAndy Shevchenko {
67331550a16SAndy Shevchenko 	int i, len = 1;		/* if we pass '%ph[CDN]', field witdh remains
67431550a16SAndy Shevchenko 				   negative value, fallback to the default */
67531550a16SAndy Shevchenko 	char separator;
67631550a16SAndy Shevchenko 
67731550a16SAndy Shevchenko 	if (spec.field_width == 0)
67831550a16SAndy Shevchenko 		/* nothing to print */
67931550a16SAndy Shevchenko 		return buf;
68031550a16SAndy Shevchenko 
68131550a16SAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
68231550a16SAndy Shevchenko 		/* NULL pointer */
68331550a16SAndy Shevchenko 		return string(buf, end, NULL, spec);
68431550a16SAndy Shevchenko 
68531550a16SAndy Shevchenko 	switch (fmt[1]) {
68631550a16SAndy Shevchenko 	case 'C':
68731550a16SAndy Shevchenko 		separator = ':';
68831550a16SAndy Shevchenko 		break;
68931550a16SAndy Shevchenko 	case 'D':
69031550a16SAndy Shevchenko 		separator = '-';
69131550a16SAndy Shevchenko 		break;
69231550a16SAndy Shevchenko 	case 'N':
69331550a16SAndy Shevchenko 		separator = 0;
69431550a16SAndy Shevchenko 		break;
69531550a16SAndy Shevchenko 	default:
69631550a16SAndy Shevchenko 		separator = ' ';
69731550a16SAndy Shevchenko 		break;
69831550a16SAndy Shevchenko 	}
69931550a16SAndy Shevchenko 
70031550a16SAndy Shevchenko 	if (spec.field_width > 0)
70131550a16SAndy Shevchenko 		len = min_t(int, spec.field_width, 64);
70231550a16SAndy Shevchenko 
70331550a16SAndy Shevchenko 	for (i = 0; i < len && buf < end - 1; i++) {
70431550a16SAndy Shevchenko 		buf = hex_byte_pack(buf, addr[i]);
70531550a16SAndy Shevchenko 
70631550a16SAndy Shevchenko 		if (buf < end && separator && i != len - 1)
70731550a16SAndy Shevchenko 			*buf++ = separator;
70831550a16SAndy Shevchenko 	}
70931550a16SAndy Shevchenko 
71031550a16SAndy Shevchenko 	return buf;
71131550a16SAndy Shevchenko }
71231550a16SAndy Shevchenko 
71331550a16SAndy Shevchenko static noinline_for_stack
714cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
7158a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
716dd45c9cfSHarvey Harrison {
7178a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
718dd45c9cfSHarvey Harrison 	char *p = mac_addr;
719dd45c9cfSHarvey Harrison 	int i;
720bc7259a2SJoe Perches 	char separator;
72176597ff9SAndrei Emeltchenko 	bool reversed = false;
722bc7259a2SJoe Perches 
72376597ff9SAndrei Emeltchenko 	switch (fmt[1]) {
72476597ff9SAndrei Emeltchenko 	case 'F':
725bc7259a2SJoe Perches 		separator = '-';
72676597ff9SAndrei Emeltchenko 		break;
72776597ff9SAndrei Emeltchenko 
72876597ff9SAndrei Emeltchenko 	case 'R':
72976597ff9SAndrei Emeltchenko 		reversed = true;
73076597ff9SAndrei Emeltchenko 		/* fall through */
73176597ff9SAndrei Emeltchenko 
73276597ff9SAndrei Emeltchenko 	default:
733bc7259a2SJoe Perches 		separator = ':';
73476597ff9SAndrei Emeltchenko 		break;
735bc7259a2SJoe Perches 	}
736dd45c9cfSHarvey Harrison 
737dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
73876597ff9SAndrei Emeltchenko 		if (reversed)
73976597ff9SAndrei Emeltchenko 			p = hex_byte_pack(p, addr[5 - i]);
74076597ff9SAndrei Emeltchenko 		else
74155036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[i]);
74276597ff9SAndrei Emeltchenko 
7438a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
744bc7259a2SJoe Perches 			*p++ = separator;
745dd45c9cfSHarvey Harrison 	}
746dd45c9cfSHarvey Harrison 	*p = '\0';
747dd45c9cfSHarvey Harrison 
748fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
749dd45c9cfSHarvey Harrison }
750dd45c9cfSHarvey Harrison 
751cf3b429bSJoe Perches static noinline_for_stack
752cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
753689afa7dSHarvey Harrison {
754689afa7dSHarvey Harrison 	int i;
7550159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
7560159f24eSJoe Perches 	int index;
7570159f24eSJoe Perches 	int step;
758689afa7dSHarvey Harrison 
7590159f24eSJoe Perches 	switch (fmt[2]) {
7600159f24eSJoe Perches 	case 'h':
7610159f24eSJoe Perches #ifdef __BIG_ENDIAN
7620159f24eSJoe Perches 		index = 0;
7630159f24eSJoe Perches 		step = 1;
7640159f24eSJoe Perches #else
7650159f24eSJoe Perches 		index = 3;
7660159f24eSJoe Perches 		step = -1;
7670159f24eSJoe Perches #endif
7680159f24eSJoe Perches 		break;
7690159f24eSJoe Perches 	case 'l':
7700159f24eSJoe Perches 		index = 3;
7710159f24eSJoe Perches 		step = -1;
7720159f24eSJoe Perches 		break;
7730159f24eSJoe Perches 	case 'n':
7740159f24eSJoe Perches 	case 'b':
7750159f24eSJoe Perches 	default:
7760159f24eSJoe Perches 		index = 0;
7770159f24eSJoe Perches 		step = 1;
7780159f24eSJoe Perches 		break;
7790159f24eSJoe Perches 	}
7808a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
7818a27f7c9SJoe Perches 		char temp[3];	/* hold each IP quad in reverse order */
782133fd9f5SDenys Vlasenko 		int digits = put_dec_trunc8(temp, addr[index]) - temp;
7838a27f7c9SJoe Perches 		if (leading_zeros) {
7848a27f7c9SJoe Perches 			if (digits < 3)
7858a27f7c9SJoe Perches 				*p++ = '0';
7868a27f7c9SJoe Perches 			if (digits < 2)
7878a27f7c9SJoe Perches 				*p++ = '0';
7888a27f7c9SJoe Perches 		}
7898a27f7c9SJoe Perches 		/* reverse the digits in the quad */
7908a27f7c9SJoe Perches 		while (digits--)
7918a27f7c9SJoe Perches 			*p++ = temp[digits];
7928a27f7c9SJoe Perches 		if (i < 3)
7938a27f7c9SJoe Perches 			*p++ = '.';
7940159f24eSJoe Perches 		index += step;
7958a27f7c9SJoe Perches 	}
7968a27f7c9SJoe Perches 	*p = '\0';
7977b9186f5SAndré Goddard Rosa 
7988a27f7c9SJoe Perches 	return p;
7998a27f7c9SJoe Perches }
8008a27f7c9SJoe Perches 
801cf3b429bSJoe Perches static noinline_for_stack
802cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
8038a27f7c9SJoe Perches {
8047b9186f5SAndré Goddard Rosa 	int i, j, range;
8058a27f7c9SJoe Perches 	unsigned char zerolength[8];
8068a27f7c9SJoe Perches 	int longest = 1;
8078a27f7c9SJoe Perches 	int colonpos = -1;
8088a27f7c9SJoe Perches 	u16 word;
8097b9186f5SAndré Goddard Rosa 	u8 hi, lo;
8108a27f7c9SJoe Perches 	bool needcolon = false;
811eb78cd26SJoe Perches 	bool useIPv4;
812eb78cd26SJoe Perches 	struct in6_addr in6;
813eb78cd26SJoe Perches 
814eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
815eb78cd26SJoe Perches 
816eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
8178a27f7c9SJoe Perches 
8188a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
8198a27f7c9SJoe Perches 
8208a27f7c9SJoe Perches 	if (useIPv4)
8218a27f7c9SJoe Perches 		range = 6;
8228a27f7c9SJoe Perches 	else
8238a27f7c9SJoe Perches 		range = 8;
8248a27f7c9SJoe Perches 
8258a27f7c9SJoe Perches 	/* find position of longest 0 run */
8268a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
8278a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
828eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
8298a27f7c9SJoe Perches 				break;
8308a27f7c9SJoe Perches 			zerolength[i]++;
8318a27f7c9SJoe Perches 		}
8328a27f7c9SJoe Perches 	}
8338a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
8348a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
8358a27f7c9SJoe Perches 			longest = zerolength[i];
8368a27f7c9SJoe Perches 			colonpos = i;
8378a27f7c9SJoe Perches 		}
8388a27f7c9SJoe Perches 	}
83929cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
84029cf519eSJoe Perches 		colonpos = -1;
8418a27f7c9SJoe Perches 
8428a27f7c9SJoe Perches 	/* emit address */
8438a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
8448a27f7c9SJoe Perches 		if (i == colonpos) {
8458a27f7c9SJoe Perches 			if (needcolon || i == 0)
8468a27f7c9SJoe Perches 				*p++ = ':';
8478a27f7c9SJoe Perches 			*p++ = ':';
8488a27f7c9SJoe Perches 			needcolon = false;
8498a27f7c9SJoe Perches 			i += longest - 1;
8508a27f7c9SJoe Perches 			continue;
8518a27f7c9SJoe Perches 		}
8528a27f7c9SJoe Perches 		if (needcolon) {
8538a27f7c9SJoe Perches 			*p++ = ':';
8548a27f7c9SJoe Perches 			needcolon = false;
8558a27f7c9SJoe Perches 		}
8568a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
857eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
8588a27f7c9SJoe Perches 		hi = word >> 8;
8598a27f7c9SJoe Perches 		lo = word & 0xff;
8608a27f7c9SJoe Perches 		if (hi) {
8618a27f7c9SJoe Perches 			if (hi > 0x0f)
86255036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
8638a27f7c9SJoe Perches 			else
8648a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
86555036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
8668a27f7c9SJoe Perches 		}
867b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
86855036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
8698a27f7c9SJoe Perches 		else
8708a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
8718a27f7c9SJoe Perches 		needcolon = true;
8728a27f7c9SJoe Perches 	}
8738a27f7c9SJoe Perches 
8748a27f7c9SJoe Perches 	if (useIPv4) {
8758a27f7c9SJoe Perches 		if (needcolon)
8768a27f7c9SJoe Perches 			*p++ = ':';
8770159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
8788a27f7c9SJoe Perches 	}
8798a27f7c9SJoe Perches 	*p = '\0';
8807b9186f5SAndré Goddard Rosa 
8818a27f7c9SJoe Perches 	return p;
8828a27f7c9SJoe Perches }
8838a27f7c9SJoe Perches 
884cf3b429bSJoe Perches static noinline_for_stack
885cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
8868a27f7c9SJoe Perches {
8878a27f7c9SJoe Perches 	int i;
8887b9186f5SAndré Goddard Rosa 
889689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
89055036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
89155036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
8928a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
893689afa7dSHarvey Harrison 			*p++ = ':';
894689afa7dSHarvey Harrison 	}
895689afa7dSHarvey Harrison 	*p = '\0';
8967b9186f5SAndré Goddard Rosa 
8978a27f7c9SJoe Perches 	return p;
8988a27f7c9SJoe Perches }
8998a27f7c9SJoe Perches 
900cf3b429bSJoe Perches static noinline_for_stack
901cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
9028a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
9038a27f7c9SJoe Perches {
9048a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
9058a27f7c9SJoe Perches 
9068a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
907eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
9088a27f7c9SJoe Perches 	else
909eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
910689afa7dSHarvey Harrison 
911fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
912689afa7dSHarvey Harrison }
913689afa7dSHarvey Harrison 
914cf3b429bSJoe Perches static noinline_for_stack
915cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
9168a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
9174aa99606SHarvey Harrison {
9188a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
9194aa99606SHarvey Harrison 
9200159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
9214aa99606SHarvey Harrison 
922fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
9234aa99606SHarvey Harrison }
9244aa99606SHarvey Harrison 
925cf3b429bSJoe Perches static noinline_for_stack
926*10679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
927*10679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
928*10679643SDaniel Borkmann {
929*10679643SDaniel Borkmann 	bool have_p = false, have_s = false, have_f = false, have_c = false;
930*10679643SDaniel Borkmann 	char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
931*10679643SDaniel Borkmann 		      sizeof(":12345") + sizeof("/123456789") +
932*10679643SDaniel Borkmann 		      sizeof("%1234567890")];
933*10679643SDaniel Borkmann 	char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
934*10679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin6_addr;
935*10679643SDaniel Borkmann 	char fmt6[2] = { fmt[0], '6' };
936*10679643SDaniel Borkmann 	u8 off = 0;
937*10679643SDaniel Borkmann 
938*10679643SDaniel Borkmann 	fmt++;
939*10679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
940*10679643SDaniel Borkmann 		switch (*fmt) {
941*10679643SDaniel Borkmann 		case 'p':
942*10679643SDaniel Borkmann 			have_p = true;
943*10679643SDaniel Borkmann 			break;
944*10679643SDaniel Borkmann 		case 'f':
945*10679643SDaniel Borkmann 			have_f = true;
946*10679643SDaniel Borkmann 			break;
947*10679643SDaniel Borkmann 		case 's':
948*10679643SDaniel Borkmann 			have_s = true;
949*10679643SDaniel Borkmann 			break;
950*10679643SDaniel Borkmann 		case 'c':
951*10679643SDaniel Borkmann 			have_c = true;
952*10679643SDaniel Borkmann 			break;
953*10679643SDaniel Borkmann 		}
954*10679643SDaniel Borkmann 	}
955*10679643SDaniel Borkmann 
956*10679643SDaniel Borkmann 	if (have_p || have_s || have_f) {
957*10679643SDaniel Borkmann 		*p = '[';
958*10679643SDaniel Borkmann 		off = 1;
959*10679643SDaniel Borkmann 	}
960*10679643SDaniel Borkmann 
961*10679643SDaniel Borkmann 	if (fmt6[0] == 'I' && have_c)
962*10679643SDaniel Borkmann 		p = ip6_compressed_string(ip6_addr + off, addr);
963*10679643SDaniel Borkmann 	else
964*10679643SDaniel Borkmann 		p = ip6_string(ip6_addr + off, addr, fmt6);
965*10679643SDaniel Borkmann 
966*10679643SDaniel Borkmann 	if (have_p || have_s || have_f)
967*10679643SDaniel Borkmann 		*p++ = ']';
968*10679643SDaniel Borkmann 
969*10679643SDaniel Borkmann 	if (have_p) {
970*10679643SDaniel Borkmann 		*p++ = ':';
971*10679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin6_port), spec);
972*10679643SDaniel Borkmann 	}
973*10679643SDaniel Borkmann 	if (have_f) {
974*10679643SDaniel Borkmann 		*p++ = '/';
975*10679643SDaniel Borkmann 		p = number(p, pend, ntohl(sa->sin6_flowinfo &
976*10679643SDaniel Borkmann 					  IPV6_FLOWINFO_MASK), spec);
977*10679643SDaniel Borkmann 	}
978*10679643SDaniel Borkmann 	if (have_s) {
979*10679643SDaniel Borkmann 		*p++ = '%';
980*10679643SDaniel Borkmann 		p = number(p, pend, sa->sin6_scope_id, spec);
981*10679643SDaniel Borkmann 	}
982*10679643SDaniel Borkmann 	*p = '\0';
983*10679643SDaniel Borkmann 
984*10679643SDaniel Borkmann 	return string(buf, end, ip6_addr, spec);
985*10679643SDaniel Borkmann }
986*10679643SDaniel Borkmann 
987*10679643SDaniel Borkmann static noinline_for_stack
988*10679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
989*10679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
990*10679643SDaniel Borkmann {
991*10679643SDaniel Borkmann 	bool have_p = false;
992*10679643SDaniel Borkmann 	char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
993*10679643SDaniel Borkmann 	char *pend = ip4_addr + sizeof(ip4_addr);
994*10679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
995*10679643SDaniel Borkmann 	char fmt4[3] = { fmt[0], '4', 0 };
996*10679643SDaniel Borkmann 
997*10679643SDaniel Borkmann 	fmt++;
998*10679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
999*10679643SDaniel Borkmann 		switch (*fmt) {
1000*10679643SDaniel Borkmann 		case 'p':
1001*10679643SDaniel Borkmann 			have_p = true;
1002*10679643SDaniel Borkmann 			break;
1003*10679643SDaniel Borkmann 		case 'h':
1004*10679643SDaniel Borkmann 		case 'l':
1005*10679643SDaniel Borkmann 		case 'n':
1006*10679643SDaniel Borkmann 		case 'b':
1007*10679643SDaniel Borkmann 			fmt4[2] = *fmt;
1008*10679643SDaniel Borkmann 			break;
1009*10679643SDaniel Borkmann 		}
1010*10679643SDaniel Borkmann 	}
1011*10679643SDaniel Borkmann 
1012*10679643SDaniel Borkmann 	p = ip4_string(ip4_addr, addr, fmt4);
1013*10679643SDaniel Borkmann 	if (have_p) {
1014*10679643SDaniel Borkmann 		*p++ = ':';
1015*10679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin_port), spec);
1016*10679643SDaniel Borkmann 	}
1017*10679643SDaniel Borkmann 	*p = '\0';
1018*10679643SDaniel Borkmann 
1019*10679643SDaniel Borkmann 	return string(buf, end, ip4_addr, spec);
1020*10679643SDaniel Borkmann }
1021*10679643SDaniel Borkmann 
1022*10679643SDaniel Borkmann static noinline_for_stack
1023cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
10249ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
10259ac6e44eSJoe Perches {
10269ac6e44eSJoe Perches 	char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
10279ac6e44eSJoe Perches 	char *p = uuid;
10289ac6e44eSJoe Perches 	int i;
10299ac6e44eSJoe Perches 	static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
10309ac6e44eSJoe Perches 	static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
10319ac6e44eSJoe Perches 	const u8 *index = be;
10329ac6e44eSJoe Perches 	bool uc = false;
10339ac6e44eSJoe Perches 
10349ac6e44eSJoe Perches 	switch (*(++fmt)) {
10359ac6e44eSJoe Perches 	case 'L':
10369ac6e44eSJoe Perches 		uc = true;		/* fall-through */
10379ac6e44eSJoe Perches 	case 'l':
10389ac6e44eSJoe Perches 		index = le;
10399ac6e44eSJoe Perches 		break;
10409ac6e44eSJoe Perches 	case 'B':
10419ac6e44eSJoe Perches 		uc = true;
10429ac6e44eSJoe Perches 		break;
10439ac6e44eSJoe Perches 	}
10449ac6e44eSJoe Perches 
10459ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
104655036ba7SAndy Shevchenko 		p = hex_byte_pack(p, addr[index[i]]);
10479ac6e44eSJoe Perches 		switch (i) {
10489ac6e44eSJoe Perches 		case 3:
10499ac6e44eSJoe Perches 		case 5:
10509ac6e44eSJoe Perches 		case 7:
10519ac6e44eSJoe Perches 		case 9:
10529ac6e44eSJoe Perches 			*p++ = '-';
10539ac6e44eSJoe Perches 			break;
10549ac6e44eSJoe Perches 		}
10559ac6e44eSJoe Perches 	}
10569ac6e44eSJoe Perches 
10579ac6e44eSJoe Perches 	*p = 0;
10589ac6e44eSJoe Perches 
10599ac6e44eSJoe Perches 	if (uc) {
10609ac6e44eSJoe Perches 		p = uuid;
10619ac6e44eSJoe Perches 		do {
10629ac6e44eSJoe Perches 			*p = toupper(*p);
10639ac6e44eSJoe Perches 		} while (*(++p));
10649ac6e44eSJoe Perches 	}
10659ac6e44eSJoe Perches 
10669ac6e44eSJoe Perches 	return string(buf, end, uuid, spec);
10679ac6e44eSJoe Perches }
10689ac6e44eSJoe Perches 
1069c8f44affSMichał Mirosław static
1070c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1071c8f44affSMichał Mirosław 		      struct printf_spec spec)
1072c8f44affSMichał Mirosław {
1073c8f44affSMichał Mirosław 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
1074c8f44affSMichał Mirosław 	if (spec.field_width == -1)
1075c8f44affSMichał Mirosław 		spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1076c8f44affSMichał Mirosław 	spec.base = 16;
1077c8f44affSMichał Mirosław 
1078c8f44affSMichał Mirosław 	return number(buf, end, *(const netdev_features_t *)addr, spec);
1079c8f44affSMichał Mirosław }
1080c8f44affSMichał Mirosław 
1081411f05f1SIngo Molnar int kptr_restrict __read_mostly;
1082455cd5abSDan Rosenberg 
10834d8a743cSLinus Torvalds /*
10844d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
10854d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
10864d8a743cSLinus Torvalds  * specifiers.
10874d8a743cSLinus Torvalds  *
1088332d2e78SLinus Torvalds  * Right now we handle:
10890fe1ef24SLinus Torvalds  *
10900c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
10910c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
10920efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
10930efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
1094b0d33c2bSJoe Perches  * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
10950f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
1096c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1097c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1098dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
1099dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
11008a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1101bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1102c8e00060SJoe Perches  *       with a dash-separated hex notation
11037c59154eSAndy Shevchenko  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
11048a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
11058a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
11068a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
1107*10679643SDaniel Borkmann  *       [S][pfs]
1108*10679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1109*10679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
11108a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
11118a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
11128a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1113*10679643SDaniel Borkmann  *       [S][pfs]
1114*10679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1115*10679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1116*10679643SDaniel Borkmann  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1117*10679643SDaniel Borkmann  * - 'I[6S]c' for IPv6 addresses printed as specified by
111829cf519eSJoe Perches  *       http://tools.ietf.org/html/rfc5952
11199ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
11209ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
11219ac6e44eSJoe Perches  *       Options for %pU are:
11229ac6e44eSJoe Perches  *         b big endian lower case hex (default)
11239ac6e44eSJoe Perches  *         B big endian UPPER case hex
11249ac6e44eSJoe Perches  *         l little endian lower case hex
11259ac6e44eSJoe Perches  *         L little endian UPPER case hex
11269ac6e44eSJoe Perches  *           big endian output byte order is:
11279ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
11289ac6e44eSJoe Perches  *           little endian output byte order is:
11299ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
11307db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
11317db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
11327db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
11337db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
11347db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
1135455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
1136c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
113731550a16SAndy Shevchenko  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
113831550a16SAndy Shevchenko  *            a certain separator (' ' by default):
113931550a16SAndy Shevchenko  *              C colon
114031550a16SAndy Shevchenko  *              D dash
114131550a16SAndy Shevchenko  *              N no separator
114231550a16SAndy Shevchenko  *            The maximum supported length is 64 bytes of the input. Consider
114331550a16SAndy Shevchenko  *            to use print_hex_dump() for the larger input.
11447d799210SStepan Moskovchenko  * - 'a' For a phys_addr_t type and its derivative types (passed by reference)
11459ac6e44eSJoe Perches  *
1146332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1147332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
1148332d2e78SLinus Torvalds  * pointer to the real address.
11494d8a743cSLinus Torvalds  */
1150cf3b429bSJoe Perches static noinline_for_stack
1151cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1152fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
115378a8bf69SLinus Torvalds {
1154725fe002SGrant Likely 	int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1155725fe002SGrant Likely 
11569f36e2c4SKees Cook 	if (!ptr && *fmt != 'K') {
11575e057981SJoe Perches 		/*
11585e057981SJoe Perches 		 * Print (null) with the same width as a pointer so it makes
11595e057981SJoe Perches 		 * tabular output look nice.
11605e057981SJoe Perches 		 */
11615e057981SJoe Perches 		if (spec.field_width == -1)
1162725fe002SGrant Likely 			spec.field_width = default_width;
1163fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
11645e057981SJoe Perches 	}
1165d97106abSLinus Torvalds 
11660fe1ef24SLinus Torvalds 	switch (*fmt) {
11670fe1ef24SLinus Torvalds 	case 'F':
11680c8b946eSFrederic Weisbecker 	case 'f':
11690fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
11700fe1ef24SLinus Torvalds 		/* Fallthrough */
11710fe1ef24SLinus Torvalds 	case 'S':
11729ac6e44eSJoe Perches 	case 's':
11730f77a8d3SNamhyung Kim 	case 'B':
1174b0d33c2bSJoe Perches 		return symbol_string(buf, end, ptr, spec, fmt);
1175332d2e78SLinus Torvalds 	case 'R':
1176c7dabef8SBjorn Helgaas 	case 'r':
1177fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
117831550a16SAndy Shevchenko 	case 'h':
117931550a16SAndy Shevchenko 		return hex_string(buf, end, ptr, spec, fmt);
11808a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
11818a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
118276597ff9SAndrei Emeltchenko 					/* [mM]F (FDDI) */
118376597ff9SAndrei Emeltchenko 					/* [mM]R (Reverse order; Bluetooth) */
11848a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
11858a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
11868a27f7c9SJoe Perches 					 * 4:	1.2.3.4
11878a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
11888a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
11898a27f7c9SJoe Perches 					 */
11908a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
11918a27f7c9SJoe Perches 					 * 4:	001.002.003.004
11928a27f7c9SJoe Perches 					 * 6:   000102...0f
11938a27f7c9SJoe Perches 					 */
11948a27f7c9SJoe Perches 		switch (fmt[1]) {
11958a27f7c9SJoe Perches 		case '6':
11968a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
11978a27f7c9SJoe Perches 		case '4':
11988a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
1199*10679643SDaniel Borkmann 		case 'S': {
1200*10679643SDaniel Borkmann 			const union {
1201*10679643SDaniel Borkmann 				struct sockaddr		raw;
1202*10679643SDaniel Borkmann 				struct sockaddr_in	v4;
1203*10679643SDaniel Borkmann 				struct sockaddr_in6	v6;
1204*10679643SDaniel Borkmann 			} *sa = ptr;
1205*10679643SDaniel Borkmann 
1206*10679643SDaniel Borkmann 			switch (sa->raw.sa_family) {
1207*10679643SDaniel Borkmann 			case AF_INET:
1208*10679643SDaniel Borkmann 				return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1209*10679643SDaniel Borkmann 			case AF_INET6:
1210*10679643SDaniel Borkmann 				return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1211*10679643SDaniel Borkmann 			default:
1212*10679643SDaniel Borkmann 				return string(buf, end, "(invalid address)", spec);
1213*10679643SDaniel Borkmann 			}}
12148a27f7c9SJoe Perches 		}
12154aa99606SHarvey Harrison 		break;
12169ac6e44eSJoe Perches 	case 'U':
12179ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
12187db6f5fbSJoe Perches 	case 'V':
12195756b76eSJan Beulich 		{
12205756b76eSJan Beulich 			va_list va;
12215756b76eSJan Beulich 
12225756b76eSJan Beulich 			va_copy(va, *((struct va_format *)ptr)->va);
12235756b76eSJan Beulich 			buf += vsnprintf(buf, end > buf ? end - buf : 0,
12245756b76eSJan Beulich 					 ((struct va_format *)ptr)->fmt, va);
12255756b76eSJan Beulich 			va_end(va);
12265756b76eSJan Beulich 			return buf;
12275756b76eSJan Beulich 		}
1228455cd5abSDan Rosenberg 	case 'K':
1229455cd5abSDan Rosenberg 		/*
1230455cd5abSDan Rosenberg 		 * %pK cannot be used in IRQ context because its test
1231455cd5abSDan Rosenberg 		 * for CAP_SYSLOG would be meaningless.
1232455cd5abSDan Rosenberg 		 */
12333715c530SDan Rosenberg 		if (kptr_restrict && (in_irq() || in_serving_softirq() ||
12343715c530SDan Rosenberg 				      in_nmi())) {
1235455cd5abSDan Rosenberg 			if (spec.field_width == -1)
1236725fe002SGrant Likely 				spec.field_width = default_width;
1237455cd5abSDan Rosenberg 			return string(buf, end, "pK-error", spec);
1238455cd5abSDan Rosenberg 		}
123926297607SJoe Perches 		if (!((kptr_restrict == 0) ||
124026297607SJoe Perches 		      (kptr_restrict == 1 &&
124126297607SJoe Perches 		       has_capability_noaudit(current, CAP_SYSLOG))))
124226297607SJoe Perches 			ptr = NULL;
124326297607SJoe Perches 		break;
1244c8f44affSMichał Mirosław 	case 'N':
1245c8f44affSMichał Mirosław 		switch (fmt[1]) {
1246c8f44affSMichał Mirosław 		case 'F':
1247c8f44affSMichał Mirosław 			return netdev_feature_string(buf, end, ptr, spec);
1248c8f44affSMichał Mirosław 		}
1249c8f44affSMichał Mirosław 		break;
12507d799210SStepan Moskovchenko 	case 'a':
12517d799210SStepan Moskovchenko 		spec.flags |= SPECIAL | SMALL | ZEROPAD;
12527d799210SStepan Moskovchenko 		spec.field_width = sizeof(phys_addr_t) * 2 + 2;
12537d799210SStepan Moskovchenko 		spec.base = 16;
12547d799210SStepan Moskovchenko 		return number(buf, end,
12557d799210SStepan Moskovchenko 			      (unsigned long long) *((phys_addr_t *)ptr), spec);
12560fe1ef24SLinus Torvalds 	}
1257fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
1258fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
1259725fe002SGrant Likely 		spec.field_width = default_width;
1260fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
126178a8bf69SLinus Torvalds 	}
1262fef20d9cSFrederic Weisbecker 	spec.base = 16;
1263fef20d9cSFrederic Weisbecker 
1264fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
1265fef20d9cSFrederic Weisbecker }
1266fef20d9cSFrederic Weisbecker 
1267fef20d9cSFrederic Weisbecker /*
1268fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
1269fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
1270fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
1271fef20d9cSFrederic Weisbecker  * to go on the next call).
1272fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
1273fef20d9cSFrederic Weisbecker  *
1274fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
1275fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
1276fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
1277fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
1278fef20d9cSFrederic Weisbecker  *
1279fef20d9cSFrederic Weisbecker  * @fmt: the format string
1280fef20d9cSFrederic Weisbecker  * @type of the token returned
1281fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
1282fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
1283fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
1284fef20d9cSFrederic Weisbecker  * @precision: precision of a number
1285fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
1286fef20d9cSFrederic Weisbecker  */
1287cf3b429bSJoe Perches static noinline_for_stack
1288cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
1289fef20d9cSFrederic Weisbecker {
1290fef20d9cSFrederic Weisbecker 	const char *start = fmt;
1291fef20d9cSFrederic Weisbecker 
1292fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
1293ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
1294fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
1295fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
1296fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
1297fef20d9cSFrederic Weisbecker 		}
1298fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1299fef20d9cSFrederic Weisbecker 		goto precision;
1300fef20d9cSFrederic Weisbecker 	}
1301fef20d9cSFrederic Weisbecker 
1302fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
1303fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
1304fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
1305fef20d9cSFrederic Weisbecker 			spec->precision = 0;
1306fef20d9cSFrederic Weisbecker 
1307fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1308fef20d9cSFrederic Weisbecker 		goto qualifier;
1309fef20d9cSFrederic Weisbecker 	}
1310fef20d9cSFrederic Weisbecker 
1311fef20d9cSFrederic Weisbecker 	/* By default */
1312fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
1313fef20d9cSFrederic Weisbecker 
1314fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
1315fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
1316fef20d9cSFrederic Weisbecker 			break;
1317fef20d9cSFrederic Weisbecker 	}
1318fef20d9cSFrederic Weisbecker 
1319fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
1320fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
1321fef20d9cSFrederic Weisbecker 		return fmt - start;
1322fef20d9cSFrederic Weisbecker 
1323fef20d9cSFrederic Weisbecker 	/* Process flags */
1324fef20d9cSFrederic Weisbecker 	spec->flags = 0;
1325fef20d9cSFrederic Weisbecker 
1326fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
1327fef20d9cSFrederic Weisbecker 		bool found = true;
1328fef20d9cSFrederic Weisbecker 
1329fef20d9cSFrederic Weisbecker 		++fmt;
1330fef20d9cSFrederic Weisbecker 
1331fef20d9cSFrederic Weisbecker 		switch (*fmt) {
1332fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
1333fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
1334fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
1335fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
1336fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
1337fef20d9cSFrederic Weisbecker 		default:  found = false;
1338fef20d9cSFrederic Weisbecker 		}
1339fef20d9cSFrederic Weisbecker 
1340fef20d9cSFrederic Weisbecker 		if (!found)
1341fef20d9cSFrederic Weisbecker 			break;
1342fef20d9cSFrederic Weisbecker 	}
1343fef20d9cSFrederic Weisbecker 
1344fef20d9cSFrederic Weisbecker 	/* get field width */
1345fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
1346fef20d9cSFrederic Weisbecker 
1347fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
1348fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
1349fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
1350fef20d9cSFrederic Weisbecker 		/* it's the next argument */
1351ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
1352fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1353fef20d9cSFrederic Weisbecker 	}
1354fef20d9cSFrederic Weisbecker 
1355fef20d9cSFrederic Weisbecker precision:
1356fef20d9cSFrederic Weisbecker 	/* get the precision */
1357fef20d9cSFrederic Weisbecker 	spec->precision = -1;
1358fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
1359fef20d9cSFrederic Weisbecker 		++fmt;
1360fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
1361fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
1362fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
1363fef20d9cSFrederic Weisbecker 				spec->precision = 0;
1364fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
1365fef20d9cSFrederic Weisbecker 			/* it's the next argument */
1366adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
1367fef20d9cSFrederic Weisbecker 			return ++fmt - start;
1368fef20d9cSFrederic Weisbecker 		}
1369fef20d9cSFrederic Weisbecker 	}
1370fef20d9cSFrederic Weisbecker 
1371fef20d9cSFrederic Weisbecker qualifier:
1372fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
1373fef20d9cSFrederic Weisbecker 	spec->qualifier = -1;
137475fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
137575fb8f26SAndy Shevchenko 	    _tolower(*fmt) == 'z' || *fmt == 't') {
1376a4e94ef0SZhaolei 		spec->qualifier = *fmt++;
1377a4e94ef0SZhaolei 		if (unlikely(spec->qualifier == *fmt)) {
1378a4e94ef0SZhaolei 			if (spec->qualifier == 'l') {
1379fef20d9cSFrederic Weisbecker 				spec->qualifier = 'L';
1380fef20d9cSFrederic Weisbecker 				++fmt;
1381a4e94ef0SZhaolei 			} else if (spec->qualifier == 'h') {
1382a4e94ef0SZhaolei 				spec->qualifier = 'H';
1383a4e94ef0SZhaolei 				++fmt;
1384a4e94ef0SZhaolei 			}
1385fef20d9cSFrederic Weisbecker 		}
1386fef20d9cSFrederic Weisbecker 	}
1387fef20d9cSFrederic Weisbecker 
1388fef20d9cSFrederic Weisbecker 	/* default base */
1389fef20d9cSFrederic Weisbecker 	spec->base = 10;
1390fef20d9cSFrederic Weisbecker 	switch (*fmt) {
1391fef20d9cSFrederic Weisbecker 	case 'c':
1392fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
1393fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1394fef20d9cSFrederic Weisbecker 
1395fef20d9cSFrederic Weisbecker 	case 's':
1396fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
1397fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1398fef20d9cSFrederic Weisbecker 
1399fef20d9cSFrederic Weisbecker 	case 'p':
1400fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
1401fef20d9cSFrederic Weisbecker 		return fmt - start;
1402fef20d9cSFrederic Weisbecker 		/* skip alnum */
1403fef20d9cSFrederic Weisbecker 
1404fef20d9cSFrederic Weisbecker 	case 'n':
1405fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NRCHARS;
1406fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1407fef20d9cSFrederic Weisbecker 
1408fef20d9cSFrederic Weisbecker 	case '%':
1409fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1410fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1411fef20d9cSFrederic Weisbecker 
1412fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1413fef20d9cSFrederic Weisbecker 	case 'o':
1414fef20d9cSFrederic Weisbecker 		spec->base = 8;
1415fef20d9cSFrederic Weisbecker 		break;
1416fef20d9cSFrederic Weisbecker 
1417fef20d9cSFrederic Weisbecker 	case 'x':
1418fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1419fef20d9cSFrederic Weisbecker 
1420fef20d9cSFrederic Weisbecker 	case 'X':
1421fef20d9cSFrederic Weisbecker 		spec->base = 16;
1422fef20d9cSFrederic Weisbecker 		break;
1423fef20d9cSFrederic Weisbecker 
1424fef20d9cSFrederic Weisbecker 	case 'd':
1425fef20d9cSFrederic Weisbecker 	case 'i':
142639e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1427fef20d9cSFrederic Weisbecker 	case 'u':
1428fef20d9cSFrederic Weisbecker 		break;
1429fef20d9cSFrederic Weisbecker 
1430fef20d9cSFrederic Weisbecker 	default:
1431fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1432fef20d9cSFrederic Weisbecker 		return fmt - start;
1433fef20d9cSFrederic Weisbecker 	}
1434fef20d9cSFrederic Weisbecker 
1435fef20d9cSFrederic Weisbecker 	if (spec->qualifier == 'L')
1436fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1437fef20d9cSFrederic Weisbecker 	else if (spec->qualifier == 'l') {
143839e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1439fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_LONG;
1440fef20d9cSFrederic Weisbecker 		else
1441fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_ULONG;
144275fb8f26SAndy Shevchenko 	} else if (_tolower(spec->qualifier) == 'z') {
1443fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1444fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 't') {
1445fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1446a4e94ef0SZhaolei 	} else if (spec->qualifier == 'H') {
1447a4e94ef0SZhaolei 		if (spec->flags & SIGN)
1448a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_BYTE;
1449a4e94ef0SZhaolei 		else
1450a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_UBYTE;
1451fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'h') {
145239e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1453fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_SHORT;
1454fef20d9cSFrederic Weisbecker 		else
1455fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_USHORT;
1456fef20d9cSFrederic Weisbecker 	} else {
145739e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1458fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_INT;
1459fef20d9cSFrederic Weisbecker 		else
1460fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_UINT;
1461fef20d9cSFrederic Weisbecker 	}
1462fef20d9cSFrederic Weisbecker 
1463fef20d9cSFrederic Weisbecker 	return ++fmt - start;
146478a8bf69SLinus Torvalds }
146578a8bf69SLinus Torvalds 
14661da177e4SLinus Torvalds /**
14671da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
14681da177e4SLinus Torvalds  * @buf: The buffer to place the result into
14691da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
14701da177e4SLinus Torvalds  * @fmt: The format string to use
14711da177e4SLinus Torvalds  * @args: Arguments for the format string
14721da177e4SLinus Torvalds  *
147320036fdcSAndi Kleen  * This function follows C99 vsnprintf, but has some extensions:
147491adcd2cSSteven Rostedt  * %pS output the name of a text symbol with offset
147591adcd2cSSteven Rostedt  * %ps output the name of a text symbol without offset
14760c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
14770c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
14780f77a8d3SNamhyung Kim  * %pB output the name of a backtrace symbol with its offset
14798a79503aSUwe Kleine-König  * %pR output the address range in a struct resource with decoded flags
14808a79503aSUwe Kleine-König  * %pr output the address range in a struct resource with raw flags
14818a79503aSUwe Kleine-König  * %pM output a 6-byte MAC address with colons
14827c59154eSAndy Shevchenko  * %pMR output a 6-byte MAC address with colons in reversed order
14837c59154eSAndy Shevchenko  * %pMF output a 6-byte MAC address with dashes
14848a79503aSUwe Kleine-König  * %pm output a 6-byte MAC address without colons
14857c59154eSAndy Shevchenko  * %pmR output a 6-byte MAC address without colons in reversed order
14868a79503aSUwe Kleine-König  * %pI4 print an IPv4 address without leading zeros
14878a79503aSUwe Kleine-König  * %pi4 print an IPv4 address with leading zeros
14888a79503aSUwe Kleine-König  * %pI6 print an IPv6 address with colons
14898a79503aSUwe Kleine-König  * %pi6 print an IPv6 address without colons
1490f996f208SJan Engelhardt  * %pI6c print an IPv6 address as specified by RFC 5952
1491*10679643SDaniel Borkmann  * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
1492*10679643SDaniel Borkmann  * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
14938a79503aSUwe Kleine-König  * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
14948a79503aSUwe Kleine-König  *   case.
149531550a16SAndy Shevchenko  * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
149631550a16SAndy Shevchenko  *           bytes of the input)
14970efb4d20SSteven Rostedt  * %n is ignored
149820036fdcSAndi Kleen  *
149980f548e0SAndrew Morton  * ** Please update Documentation/printk-formats.txt when making changes **
150080f548e0SAndrew Morton  *
15011da177e4SLinus Torvalds  * The return value is the number of characters which would
15021da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
15031da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
15041da177e4SLinus Torvalds  * number of characters written into @buf as return value
150572fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
15061da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
15071da177e4SLinus Torvalds  * string is truncated.
15081da177e4SLinus Torvalds  *
1509ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
15101da177e4SLinus Torvalds  */
15111da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
15121da177e4SLinus Torvalds {
15131da177e4SLinus Torvalds 	unsigned long long num;
1514d4be151bSAndré Goddard Rosa 	char *str, *end;
1515fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
15161da177e4SLinus Torvalds 
1517f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1518f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
15192f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
15201da177e4SLinus Torvalds 		return 0;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	str = buf;
1523f796937aSJeremy Fitzhardinge 	end = buf + size;
15241da177e4SLinus Torvalds 
1525f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1526f796937aSJeremy Fitzhardinge 	if (end < buf) {
15271da177e4SLinus Torvalds 		end = ((void *)-1);
1528f796937aSJeremy Fitzhardinge 		size = end - buf;
15291da177e4SLinus Torvalds 	}
15301da177e4SLinus Torvalds 
1531fef20d9cSFrederic Weisbecker 	while (*fmt) {
1532fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1533d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1534fef20d9cSFrederic Weisbecker 
1535fef20d9cSFrederic Weisbecker 		fmt += read;
1536fef20d9cSFrederic Weisbecker 
1537fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1538fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1539fef20d9cSFrederic Weisbecker 			int copy = read;
1540fef20d9cSFrederic Weisbecker 			if (str < end) {
1541fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1542fef20d9cSFrederic Weisbecker 					copy = end - str;
1543fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1544fef20d9cSFrederic Weisbecker 			}
1545fef20d9cSFrederic Weisbecker 			str += read;
1546fef20d9cSFrederic Weisbecker 			break;
15471da177e4SLinus Torvalds 		}
15481da177e4SLinus Torvalds 
1549ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1550fef20d9cSFrederic Weisbecker 			spec.field_width = va_arg(args, int);
1551fef20d9cSFrederic Weisbecker 			break;
15521da177e4SLinus Torvalds 
1553fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1554fef20d9cSFrederic Weisbecker 			spec.precision = va_arg(args, int);
1555fef20d9cSFrederic Weisbecker 			break;
15561da177e4SLinus Torvalds 
1557d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1558d4be151bSAndré Goddard Rosa 			char c;
1559d4be151bSAndré Goddard Rosa 
1560fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1561fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1562f796937aSJeremy Fitzhardinge 					if (str < end)
15631da177e4SLinus Torvalds 						*str = ' ';
15641da177e4SLinus Torvalds 					++str;
1565fef20d9cSFrederic Weisbecker 
15661da177e4SLinus Torvalds 				}
15671da177e4SLinus Torvalds 			}
15681da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1569f796937aSJeremy Fitzhardinge 			if (str < end)
15701da177e4SLinus Torvalds 				*str = c;
15711da177e4SLinus Torvalds 			++str;
1572fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1573f796937aSJeremy Fitzhardinge 				if (str < end)
15741da177e4SLinus Torvalds 					*str = ' ';
15751da177e4SLinus Torvalds 				++str;
15761da177e4SLinus Torvalds 			}
1577fef20d9cSFrederic Weisbecker 			break;
1578d4be151bSAndré Goddard Rosa 		}
15791da177e4SLinus Torvalds 
1580fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1581fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1582fef20d9cSFrederic Weisbecker 			break;
15831da177e4SLinus Torvalds 
1584fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1585fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, va_arg(args, void *),
1586fef20d9cSFrederic Weisbecker 				      spec);
1587fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
15884d8a743cSLinus Torvalds 				fmt++;
1589fef20d9cSFrederic Weisbecker 			break;
15901da177e4SLinus Torvalds 
1591fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1592f796937aSJeremy Fitzhardinge 			if (str < end)
15931da177e4SLinus Torvalds 				*str = '%';
15941da177e4SLinus Torvalds 			++str;
15951da177e4SLinus Torvalds 			break;
15961da177e4SLinus Torvalds 
1597fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1598f796937aSJeremy Fitzhardinge 			if (str < end)
15991da177e4SLinus Torvalds 				*str = '%';
16001da177e4SLinus Torvalds 			++str;
1601fef20d9cSFrederic Weisbecker 			break;
1602fef20d9cSFrederic Weisbecker 
1603fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
1604ef0658f3SJoe Perches 			u8 qualifier = spec.qualifier;
1605fef20d9cSFrederic Weisbecker 
1606fef20d9cSFrederic Weisbecker 			if (qualifier == 'l') {
1607fef20d9cSFrederic Weisbecker 				long *ip = va_arg(args, long *);
1608fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
160975fb8f26SAndy Shevchenko 			} else if (_tolower(qualifier) == 'z') {
1610fef20d9cSFrederic Weisbecker 				size_t *ip = va_arg(args, size_t *);
1611fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
16121da177e4SLinus Torvalds 			} else {
1613fef20d9cSFrederic Weisbecker 				int *ip = va_arg(args, int *);
1614fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
1615fef20d9cSFrederic Weisbecker 			}
1616fef20d9cSFrederic Weisbecker 			break;
1617fef20d9cSFrederic Weisbecker 		}
1618fef20d9cSFrederic Weisbecker 
1619fef20d9cSFrederic Weisbecker 		default:
1620fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1621fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1622fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1623fef20d9cSFrederic Weisbecker 				break;
1624fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1625fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
1626fef20d9cSFrederic Weisbecker 				break;
1627fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1628fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
1629fef20d9cSFrederic Weisbecker 				break;
1630fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1631ef124960SJason Gunthorpe 				if (spec.flags & SIGN)
1632ef124960SJason Gunthorpe 					num = va_arg(args, ssize_t);
1633ef124960SJason Gunthorpe 				else
1634fef20d9cSFrederic Weisbecker 					num = va_arg(args, size_t);
1635fef20d9cSFrederic Weisbecker 				break;
1636fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1637fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
1638fef20d9cSFrederic Weisbecker 				break;
1639a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1640a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
1641a4e94ef0SZhaolei 				break;
1642a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1643a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
1644a4e94ef0SZhaolei 				break;
1645fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1646fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
1647fef20d9cSFrederic Weisbecker 				break;
1648fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1649fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
1650fef20d9cSFrederic Weisbecker 				break;
165139e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
165239e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
1653fef20d9cSFrederic Weisbecker 				break;
1654fef20d9cSFrederic Weisbecker 			default:
1655fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
16561da177e4SLinus Torvalds 			}
1657fef20d9cSFrederic Weisbecker 
1658fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
16591da177e4SLinus Torvalds 		}
1660fef20d9cSFrederic Weisbecker 	}
1661fef20d9cSFrederic Weisbecker 
1662f796937aSJeremy Fitzhardinge 	if (size > 0) {
1663f796937aSJeremy Fitzhardinge 		if (str < end)
16641da177e4SLinus Torvalds 			*str = '\0';
1665f796937aSJeremy Fitzhardinge 		else
16660a6047eeSLinus Torvalds 			end[-1] = '\0';
1667f796937aSJeremy Fitzhardinge 	}
1668fef20d9cSFrederic Weisbecker 
1669f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
16701da177e4SLinus Torvalds 	return str-buf;
1671fef20d9cSFrederic Weisbecker 
16721da177e4SLinus Torvalds }
16731da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds /**
16761da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
16771da177e4SLinus Torvalds  * @buf: The buffer to place the result into
16781da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
16791da177e4SLinus Torvalds  * @fmt: The format string to use
16801da177e4SLinus Torvalds  * @args: Arguments for the format string
16811da177e4SLinus Torvalds  *
16821da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
1683b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
16841da177e4SLinus Torvalds  * returns 0.
16851da177e4SLinus Torvalds  *
1686ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
168720036fdcSAndi Kleen  *
168820036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
16891da177e4SLinus Torvalds  */
16901da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
16911da177e4SLinus Torvalds {
16921da177e4SLinus Torvalds 	int i;
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
16957b9186f5SAndré Goddard Rosa 
1696b921c69fSAnton Arapov 	if (likely(i < size))
1697b921c69fSAnton Arapov 		return i;
1698b921c69fSAnton Arapov 	if (size != 0)
1699b921c69fSAnton Arapov 		return size - 1;
1700b921c69fSAnton Arapov 	return 0;
17011da177e4SLinus Torvalds }
17021da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds /**
17051da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
17061da177e4SLinus Torvalds  * @buf: The buffer to place the result into
17071da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
17081da177e4SLinus Torvalds  * @fmt: The format string to use
17091da177e4SLinus Torvalds  * @...: Arguments for the format string
17101da177e4SLinus Torvalds  *
17111da177e4SLinus Torvalds  * The return value is the number of characters which would be
17121da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
17131da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
17141da177e4SLinus Torvalds  * @size, the resulting string is truncated.
171520036fdcSAndi Kleen  *
171620036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
17171da177e4SLinus Torvalds  */
17181da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
17191da177e4SLinus Torvalds {
17201da177e4SLinus Torvalds 	va_list args;
17211da177e4SLinus Torvalds 	int i;
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds 	va_start(args, fmt);
17241da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
17251da177e4SLinus Torvalds 	va_end(args);
17267b9186f5SAndré Goddard Rosa 
17271da177e4SLinus Torvalds 	return i;
17281da177e4SLinus Torvalds }
17291da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds /**
17321da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
17331da177e4SLinus Torvalds  * @buf: The buffer to place the result into
17341da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
17351da177e4SLinus Torvalds  * @fmt: The format string to use
17361da177e4SLinus Torvalds  * @...: Arguments for the format string
17371da177e4SLinus Torvalds  *
17381da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
1739b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
17401da177e4SLinus Torvalds  */
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
17431da177e4SLinus Torvalds {
17441da177e4SLinus Torvalds 	va_list args;
17451da177e4SLinus Torvalds 	int i;
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds 	va_start(args, fmt);
1748b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
17491da177e4SLinus Torvalds 	va_end(args);
17507b9186f5SAndré Goddard Rosa 
1751b903c0b8SChangli Gao 	return i;
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds /**
17561da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
17571da177e4SLinus Torvalds  * @buf: The buffer to place the result into
17581da177e4SLinus Torvalds  * @fmt: The format string to use
17591da177e4SLinus Torvalds  * @args: Arguments for the format string
17601da177e4SLinus Torvalds  *
17611da177e4SLinus Torvalds  * The function returns the number of characters written
176272fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
17631da177e4SLinus Torvalds  * buffer overflows.
17641da177e4SLinus Torvalds  *
1765ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
176620036fdcSAndi Kleen  *
176720036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
17681da177e4SLinus Torvalds  */
17691da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
17701da177e4SLinus Torvalds {
17711da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
17721da177e4SLinus Torvalds }
17731da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds /**
17761da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
17771da177e4SLinus Torvalds  * @buf: The buffer to place the result into
17781da177e4SLinus Torvalds  * @fmt: The format string to use
17791da177e4SLinus Torvalds  * @...: Arguments for the format string
17801da177e4SLinus Torvalds  *
17811da177e4SLinus Torvalds  * The function returns the number of characters written
178272fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
17831da177e4SLinus Torvalds  * buffer overflows.
178420036fdcSAndi Kleen  *
178520036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
17861da177e4SLinus Torvalds  */
17871da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
17881da177e4SLinus Torvalds {
17891da177e4SLinus Torvalds 	va_list args;
17901da177e4SLinus Torvalds 	int i;
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 	va_start(args, fmt);
17931da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
17941da177e4SLinus Torvalds 	va_end(args);
17957b9186f5SAndré Goddard Rosa 
17961da177e4SLinus Torvalds 	return i;
17971da177e4SLinus Torvalds }
17981da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
17991da177e4SLinus Torvalds 
18004370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
18014370aa4aSLai Jiangshan /*
18024370aa4aSLai Jiangshan  * bprintf service:
18034370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
18044370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
18054370aa4aSLai Jiangshan  */
18064370aa4aSLai Jiangshan 
18074370aa4aSLai Jiangshan /**
18084370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
18094370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
18104370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
18114370aa4aSLai Jiangshan  * @fmt: The format string to use
18124370aa4aSLai Jiangshan  * @args: Arguments for the format string
18134370aa4aSLai Jiangshan  *
18144370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
18154370aa4aSLai Jiangshan  * is skiped.
18164370aa4aSLai Jiangshan  *
18174370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
18184370aa4aSLai Jiangshan  * the given input.
18194370aa4aSLai Jiangshan  *
18204370aa4aSLai Jiangshan  * NOTE:
18214370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
18224370aa4aSLai Jiangshan  * valid for bstr_printf().
18234370aa4aSLai Jiangshan  */
18244370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
18254370aa4aSLai Jiangshan {
1826fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
18274370aa4aSLai Jiangshan 	char *str, *end;
18284370aa4aSLai Jiangshan 
18294370aa4aSLai Jiangshan 	str = (char *)bin_buf;
18304370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
18314370aa4aSLai Jiangshan 
18324370aa4aSLai Jiangshan #define save_arg(type)							\
18334370aa4aSLai Jiangshan do {									\
18344370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
18354370aa4aSLai Jiangshan 		unsigned long long value;				\
18364370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
18374370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
18384370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
18394370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
18404370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
18414370aa4aSLai Jiangshan 		}							\
18424370aa4aSLai Jiangshan 	} else {							\
18434370aa4aSLai Jiangshan 		unsigned long value;					\
18444370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
18454370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
18464370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
18474370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
18484370aa4aSLai Jiangshan 	}								\
18494370aa4aSLai Jiangshan 	str += sizeof(type);						\
18504370aa4aSLai Jiangshan } while (0)
18514370aa4aSLai Jiangshan 
1852fef20d9cSFrederic Weisbecker 	while (*fmt) {
1853d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
18544370aa4aSLai Jiangshan 
1855fef20d9cSFrederic Weisbecker 		fmt += read;
1856fef20d9cSFrederic Weisbecker 
1857fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1858fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
1859d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_INVALID:
1860d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
1861fef20d9cSFrederic Weisbecker 			break;
1862fef20d9cSFrederic Weisbecker 
1863ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1864fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
18654370aa4aSLai Jiangshan 			save_arg(int);
1866fef20d9cSFrederic Weisbecker 			break;
18674370aa4aSLai Jiangshan 
1868fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
18694370aa4aSLai Jiangshan 			save_arg(char);
1870fef20d9cSFrederic Weisbecker 			break;
1871fef20d9cSFrederic Weisbecker 
1872fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
18734370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
18744370aa4aSLai Jiangshan 			size_t len;
18756c356634SAndré Goddard Rosa 
18764370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
18774370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
18780f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
18796c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
18806c356634SAndré Goddard Rosa 			if (str + len < end)
18816c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
18826c356634SAndré Goddard Rosa 			str += len;
1883fef20d9cSFrederic Weisbecker 			break;
18844370aa4aSLai Jiangshan 		}
1885fef20d9cSFrederic Weisbecker 
1886fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
18874370aa4aSLai Jiangshan 			save_arg(void *);
18884370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
1889fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
18904370aa4aSLai Jiangshan 				fmt++;
1891fef20d9cSFrederic Weisbecker 			break;
1892fef20d9cSFrederic Weisbecker 
1893fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
18944370aa4aSLai Jiangshan 			/* skip %n 's argument */
1895ef0658f3SJoe Perches 			u8 qualifier = spec.qualifier;
18964370aa4aSLai Jiangshan 			void *skip_arg;
18974370aa4aSLai Jiangshan 			if (qualifier == 'l')
18984370aa4aSLai Jiangshan 				skip_arg = va_arg(args, long *);
189975fb8f26SAndy Shevchenko 			else if (_tolower(qualifier) == 'z')
19004370aa4aSLai Jiangshan 				skip_arg = va_arg(args, size_t *);
19014370aa4aSLai Jiangshan 			else
19024370aa4aSLai Jiangshan 				skip_arg = va_arg(args, int *);
1903fef20d9cSFrederic Weisbecker 			break;
19044370aa4aSLai Jiangshan 		}
19054370aa4aSLai Jiangshan 
1906fef20d9cSFrederic Weisbecker 		default:
1907fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1908fef20d9cSFrederic Weisbecker 
1909fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1910fef20d9cSFrederic Weisbecker 				save_arg(long long);
1911fef20d9cSFrederic Weisbecker 				break;
1912fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1913fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1914fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
1915fef20d9cSFrederic Weisbecker 				break;
1916fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1917fef20d9cSFrederic Weisbecker 				save_arg(size_t);
1918fef20d9cSFrederic Weisbecker 				break;
1919fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1920fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
1921fef20d9cSFrederic Weisbecker 				break;
1922a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1923a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1924a4e94ef0SZhaolei 				save_arg(char);
1925a4e94ef0SZhaolei 				break;
1926fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1927fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1928fef20d9cSFrederic Weisbecker 				save_arg(short);
1929fef20d9cSFrederic Weisbecker 				break;
1930fef20d9cSFrederic Weisbecker 			default:
1931fef20d9cSFrederic Weisbecker 				save_arg(int);
1932fef20d9cSFrederic Weisbecker 			}
1933fef20d9cSFrederic Weisbecker 		}
1934fef20d9cSFrederic Weisbecker 	}
1935fef20d9cSFrederic Weisbecker 
19367b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1937fef20d9cSFrederic Weisbecker #undef save_arg
19384370aa4aSLai Jiangshan }
19394370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
19404370aa4aSLai Jiangshan 
19414370aa4aSLai Jiangshan /**
19424370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
19434370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
19444370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
19454370aa4aSLai Jiangshan  * @fmt: The format string to use
19464370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
19474370aa4aSLai Jiangshan  *
19484370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
19494370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
19504370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
19514370aa4aSLai Jiangshan  *
19524370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
19530efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
19544370aa4aSLai Jiangshan  *
19554370aa4aSLai Jiangshan  * The return value is the number of characters which would
19564370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
19574370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
19584370aa4aSLai Jiangshan  * number of characters written into @buf as return value
19594370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
19604370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
19614370aa4aSLai Jiangshan  * string is truncated.
19624370aa4aSLai Jiangshan  */
19634370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
19644370aa4aSLai Jiangshan {
1965fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
1966d4be151bSAndré Goddard Rosa 	char *str, *end;
1967d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
19684370aa4aSLai Jiangshan 
19692f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
19704370aa4aSLai Jiangshan 		return 0;
19714370aa4aSLai Jiangshan 
19724370aa4aSLai Jiangshan 	str = buf;
19734370aa4aSLai Jiangshan 	end = buf + size;
19744370aa4aSLai Jiangshan 
19754370aa4aSLai Jiangshan #define get_arg(type)							\
19764370aa4aSLai Jiangshan ({									\
19774370aa4aSLai Jiangshan 	typeof(type) value;						\
19784370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
19794370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
19804370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
19814370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
19824370aa4aSLai Jiangshan 	} else {							\
19834370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
19844370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
19854370aa4aSLai Jiangshan 	}								\
19864370aa4aSLai Jiangshan 	args += sizeof(type);						\
19874370aa4aSLai Jiangshan 	value;								\
19884370aa4aSLai Jiangshan })
19894370aa4aSLai Jiangshan 
19904370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
19914370aa4aSLai Jiangshan 	if (end < buf) {
19924370aa4aSLai Jiangshan 		end = ((void *)-1);
19934370aa4aSLai Jiangshan 		size = end - buf;
19944370aa4aSLai Jiangshan 	}
19954370aa4aSLai Jiangshan 
1996fef20d9cSFrederic Weisbecker 	while (*fmt) {
1997fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1998d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1999fef20d9cSFrederic Weisbecker 
2000fef20d9cSFrederic Weisbecker 		fmt += read;
2001fef20d9cSFrederic Weisbecker 
2002fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2003fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2004fef20d9cSFrederic Weisbecker 			int copy = read;
2005fef20d9cSFrederic Weisbecker 			if (str < end) {
2006fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2007fef20d9cSFrederic Weisbecker 					copy = end - str;
2008fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2009fef20d9cSFrederic Weisbecker 			}
2010fef20d9cSFrederic Weisbecker 			str += read;
2011fef20d9cSFrederic Weisbecker 			break;
20124370aa4aSLai Jiangshan 		}
20134370aa4aSLai Jiangshan 
2014ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2015fef20d9cSFrederic Weisbecker 			spec.field_width = get_arg(int);
2016fef20d9cSFrederic Weisbecker 			break;
20174370aa4aSLai Jiangshan 
2018fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
2019fef20d9cSFrederic Weisbecker 			spec.precision = get_arg(int);
2020fef20d9cSFrederic Weisbecker 			break;
20214370aa4aSLai Jiangshan 
2022d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2023d4be151bSAndré Goddard Rosa 			char c;
2024d4be151bSAndré Goddard Rosa 
2025fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2026fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
20274370aa4aSLai Jiangshan 					if (str < end)
20284370aa4aSLai Jiangshan 						*str = ' ';
20294370aa4aSLai Jiangshan 					++str;
20304370aa4aSLai Jiangshan 				}
20314370aa4aSLai Jiangshan 			}
20324370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
20334370aa4aSLai Jiangshan 			if (str < end)
20344370aa4aSLai Jiangshan 				*str = c;
20354370aa4aSLai Jiangshan 			++str;
2036fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
20374370aa4aSLai Jiangshan 				if (str < end)
20384370aa4aSLai Jiangshan 					*str = ' ';
20394370aa4aSLai Jiangshan 				++str;
20404370aa4aSLai Jiangshan 			}
2041fef20d9cSFrederic Weisbecker 			break;
2042d4be151bSAndré Goddard Rosa 		}
20434370aa4aSLai Jiangshan 
2044fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
20454370aa4aSLai Jiangshan 			const char *str_arg = args;
2046d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
2047fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
2048fef20d9cSFrederic Weisbecker 			break;
20494370aa4aSLai Jiangshan 		}
20504370aa4aSLai Jiangshan 
2051fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2052fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, get_arg(void *), spec);
2053fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
20544370aa4aSLai Jiangshan 				fmt++;
2055fef20d9cSFrederic Weisbecker 			break;
20564370aa4aSLai Jiangshan 
2057fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
2058fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
20594370aa4aSLai Jiangshan 			if (str < end)
20604370aa4aSLai Jiangshan 				*str = '%';
20614370aa4aSLai Jiangshan 			++str;
2062fef20d9cSFrederic Weisbecker 			break;
2063fef20d9cSFrederic Weisbecker 
2064fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS:
2065fef20d9cSFrederic Weisbecker 			/* skip */
2066fef20d9cSFrederic Weisbecker 			break;
2067fef20d9cSFrederic Weisbecker 
2068d4be151bSAndré Goddard Rosa 		default: {
2069d4be151bSAndré Goddard Rosa 			unsigned long long num;
2070d4be151bSAndré Goddard Rosa 
2071fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2072fef20d9cSFrederic Weisbecker 
2073fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
20744370aa4aSLai Jiangshan 				num = get_arg(long long);
2075fef20d9cSFrederic Weisbecker 				break;
2076fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2077fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2078fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
2079fef20d9cSFrederic Weisbecker 				break;
2080fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
20814370aa4aSLai Jiangshan 				num = get_arg(size_t);
2082fef20d9cSFrederic Weisbecker 				break;
2083fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
20844370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
2085fef20d9cSFrederic Weisbecker 				break;
2086a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2087a4e94ef0SZhaolei 				num = get_arg(unsigned char);
2088a4e94ef0SZhaolei 				break;
2089a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2090a4e94ef0SZhaolei 				num = get_arg(signed char);
2091a4e94ef0SZhaolei 				break;
2092fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2093fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
2094fef20d9cSFrederic Weisbecker 				break;
2095fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2096fef20d9cSFrederic Weisbecker 				num = get_arg(short);
2097fef20d9cSFrederic Weisbecker 				break;
2098fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
20994370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
2100fef20d9cSFrederic Weisbecker 				break;
2101fef20d9cSFrederic Weisbecker 			default:
2102fef20d9cSFrederic Weisbecker 				num = get_arg(int);
21034370aa4aSLai Jiangshan 			}
2104fef20d9cSFrederic Weisbecker 
2105fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
2106d4be151bSAndré Goddard Rosa 		} /* default: */
2107d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
2108d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
2109fef20d9cSFrederic Weisbecker 
21104370aa4aSLai Jiangshan 	if (size > 0) {
21114370aa4aSLai Jiangshan 		if (str < end)
21124370aa4aSLai Jiangshan 			*str = '\0';
21134370aa4aSLai Jiangshan 		else
21144370aa4aSLai Jiangshan 			end[-1] = '\0';
21154370aa4aSLai Jiangshan 	}
2116fef20d9cSFrederic Weisbecker 
21174370aa4aSLai Jiangshan #undef get_arg
21184370aa4aSLai Jiangshan 
21194370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
21204370aa4aSLai Jiangshan 	return str - buf;
21214370aa4aSLai Jiangshan }
21224370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
21234370aa4aSLai Jiangshan 
21244370aa4aSLai Jiangshan /**
21254370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
21264370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
21274370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
21284370aa4aSLai Jiangshan  * @fmt: The format string to use
21294370aa4aSLai Jiangshan  * @...: Arguments for the format string
21304370aa4aSLai Jiangshan  *
21314370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
21324370aa4aSLai Jiangshan  * into @bin_buf.
21334370aa4aSLai Jiangshan  */
21344370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
21354370aa4aSLai Jiangshan {
21364370aa4aSLai Jiangshan 	va_list args;
21374370aa4aSLai Jiangshan 	int ret;
21384370aa4aSLai Jiangshan 
21394370aa4aSLai Jiangshan 	va_start(args, fmt);
21404370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
21414370aa4aSLai Jiangshan 	va_end(args);
21427b9186f5SAndré Goddard Rosa 
21434370aa4aSLai Jiangshan 	return ret;
21444370aa4aSLai Jiangshan }
21454370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
21464370aa4aSLai Jiangshan 
21474370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
21484370aa4aSLai Jiangshan 
21491da177e4SLinus Torvalds /**
21501da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
21511da177e4SLinus Torvalds  * @buf:	input buffer
21521da177e4SLinus Torvalds  * @fmt:	format of buffer
21531da177e4SLinus Torvalds  * @args:	arguments
21541da177e4SLinus Torvalds  */
21551da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
21561da177e4SLinus Torvalds {
21571da177e4SLinus Torvalds 	const char *str = buf;
21581da177e4SLinus Torvalds 	char *next;
21591da177e4SLinus Torvalds 	char digit;
21601da177e4SLinus Torvalds 	int num = 0;
2161ef0658f3SJoe Perches 	u8 qualifier;
216253809751SJan Beulich 	unsigned int base;
216353809751SJan Beulich 	union {
216453809751SJan Beulich 		long long s;
216553809751SJan Beulich 		unsigned long long u;
216653809751SJan Beulich 	} val;
2167ef0658f3SJoe Perches 	s16 field_width;
2168d4be151bSAndré Goddard Rosa 	bool is_sign;
21691da177e4SLinus Torvalds 
2170da99075cSJan Beulich 	while (*fmt) {
21711da177e4SLinus Torvalds 		/* skip any white space in format */
21721da177e4SLinus Torvalds 		/* white space in format matchs any amount of
21731da177e4SLinus Torvalds 		 * white space, including none, in the input.
21741da177e4SLinus Torvalds 		 */
21751da177e4SLinus Torvalds 		if (isspace(*fmt)) {
2176e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
2177e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
21781da177e4SLinus Torvalds 		}
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
21811da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
21821da177e4SLinus Torvalds 			if (*fmt++ != *str++)
21831da177e4SLinus Torvalds 				break;
21841da177e4SLinus Torvalds 			continue;
21851da177e4SLinus Torvalds 		}
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds 		if (!*fmt)
21881da177e4SLinus Torvalds 			break;
21891da177e4SLinus Torvalds 		++fmt;
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 		/* skip this conversion.
21921da177e4SLinus Torvalds 		 * advance both strings to next white space
21931da177e4SLinus Torvalds 		 */
21941da177e4SLinus Torvalds 		if (*fmt == '*') {
2195da99075cSJan Beulich 			if (!*str)
2196da99075cSJan Beulich 				break;
21978fccae2cSAndy Spencer 			while (!isspace(*fmt) && *fmt != '%' && *fmt)
21981da177e4SLinus Torvalds 				fmt++;
21991da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
22001da177e4SLinus Torvalds 				str++;
22011da177e4SLinus Torvalds 			continue;
22021da177e4SLinus Torvalds 		}
22031da177e4SLinus Torvalds 
22041da177e4SLinus Torvalds 		/* get field width */
22051da177e4SLinus Torvalds 		field_width = -1;
220653809751SJan Beulich 		if (isdigit(*fmt)) {
22071da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
220853809751SJan Beulich 			if (field_width <= 0)
220953809751SJan Beulich 				break;
221053809751SJan Beulich 		}
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds 		/* get conversion qualifier */
22131da177e4SLinus Torvalds 		qualifier = -1;
221475fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
221575fb8f26SAndy Shevchenko 		    _tolower(*fmt) == 'z') {
22161da177e4SLinus Torvalds 			qualifier = *fmt++;
22171da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
22181da177e4SLinus Torvalds 				if (qualifier == 'h') {
22191da177e4SLinus Torvalds 					qualifier = 'H';
22201da177e4SLinus Torvalds 					fmt++;
22211da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
22221da177e4SLinus Torvalds 					qualifier = 'L';
22231da177e4SLinus Torvalds 					fmt++;
22241da177e4SLinus Torvalds 				}
22251da177e4SLinus Torvalds 			}
22261da177e4SLinus Torvalds 		}
22271da177e4SLinus Torvalds 
2228da99075cSJan Beulich 		if (!*fmt)
2229da99075cSJan Beulich 			break;
2230da99075cSJan Beulich 
2231da99075cSJan Beulich 		if (*fmt == 'n') {
2232da99075cSJan Beulich 			/* return number of characters read so far */
2233da99075cSJan Beulich 			*va_arg(args, int *) = str - buf;
2234da99075cSJan Beulich 			++fmt;
2235da99075cSJan Beulich 			continue;
2236da99075cSJan Beulich 		}
2237da99075cSJan Beulich 
2238da99075cSJan Beulich 		if (!*str)
22391da177e4SLinus Torvalds 			break;
22401da177e4SLinus Torvalds 
2241d4be151bSAndré Goddard Rosa 		base = 10;
2242d4be151bSAndré Goddard Rosa 		is_sign = 0;
2243d4be151bSAndré Goddard Rosa 
22441da177e4SLinus Torvalds 		switch (*fmt++) {
22451da177e4SLinus Torvalds 		case 'c':
22461da177e4SLinus Torvalds 		{
22471da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
22481da177e4SLinus Torvalds 			if (field_width == -1)
22491da177e4SLinus Torvalds 				field_width = 1;
22501da177e4SLinus Torvalds 			do {
22511da177e4SLinus Torvalds 				*s++ = *str++;
22521da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
22531da177e4SLinus Torvalds 			num++;
22541da177e4SLinus Torvalds 		}
22551da177e4SLinus Torvalds 		continue;
22561da177e4SLinus Torvalds 		case 's':
22571da177e4SLinus Torvalds 		{
22581da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
22591da177e4SLinus Torvalds 			if (field_width == -1)
22604be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
22611da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
2262e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
22631da177e4SLinus Torvalds 
22641da177e4SLinus Torvalds 			/* now copy until next white space */
22657b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
22661da177e4SLinus Torvalds 				*s++ = *str++;
22671da177e4SLinus Torvalds 			*s = '\0';
22681da177e4SLinus Torvalds 			num++;
22691da177e4SLinus Torvalds 		}
22701da177e4SLinus Torvalds 		continue;
22711da177e4SLinus Torvalds 		case 'o':
22721da177e4SLinus Torvalds 			base = 8;
22731da177e4SLinus Torvalds 			break;
22741da177e4SLinus Torvalds 		case 'x':
22751da177e4SLinus Torvalds 		case 'X':
22761da177e4SLinus Torvalds 			base = 16;
22771da177e4SLinus Torvalds 			break;
22781da177e4SLinus Torvalds 		case 'i':
22791da177e4SLinus Torvalds 			base = 0;
22801da177e4SLinus Torvalds 		case 'd':
22811da177e4SLinus Torvalds 			is_sign = 1;
22821da177e4SLinus Torvalds 		case 'u':
22831da177e4SLinus Torvalds 			break;
22841da177e4SLinus Torvalds 		case '%':
22851da177e4SLinus Torvalds 			/* looking for '%' in str */
22861da177e4SLinus Torvalds 			if (*str++ != '%')
22871da177e4SLinus Torvalds 				return num;
22881da177e4SLinus Torvalds 			continue;
22891da177e4SLinus Torvalds 		default:
22901da177e4SLinus Torvalds 			/* invalid format; stop here */
22911da177e4SLinus Torvalds 			return num;
22921da177e4SLinus Torvalds 		}
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds 		/* have some sort of integer conversion.
22951da177e4SLinus Torvalds 		 * first, skip white space in buffer.
22961da177e4SLinus Torvalds 		 */
2297e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds 		digit = *str;
23001da177e4SLinus Torvalds 		if (is_sign && digit == '-')
23011da177e4SLinus Torvalds 			digit = *(str + 1);
23021da177e4SLinus Torvalds 
23031da177e4SLinus Torvalds 		if (!digit
23041da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
23051da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
23061da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
23071da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
23081da177e4SLinus Torvalds 			break;
23091da177e4SLinus Torvalds 
231053809751SJan Beulich 		if (is_sign)
231153809751SJan Beulich 			val.s = qualifier != 'L' ?
231253809751SJan Beulich 				simple_strtol(str, &next, base) :
231353809751SJan Beulich 				simple_strtoll(str, &next, base);
231453809751SJan Beulich 		else
231553809751SJan Beulich 			val.u = qualifier != 'L' ?
231653809751SJan Beulich 				simple_strtoul(str, &next, base) :
231753809751SJan Beulich 				simple_strtoull(str, &next, base);
231853809751SJan Beulich 
231953809751SJan Beulich 		if (field_width > 0 && next - str > field_width) {
232053809751SJan Beulich 			if (base == 0)
232153809751SJan Beulich 				_parse_integer_fixup_radix(str, &base);
232253809751SJan Beulich 			while (next - str > field_width) {
232353809751SJan Beulich 				if (is_sign)
232453809751SJan Beulich 					val.s = div_s64(val.s, base);
232553809751SJan Beulich 				else
232653809751SJan Beulich 					val.u = div_u64(val.u, base);
232753809751SJan Beulich 				--next;
232853809751SJan Beulich 			}
232953809751SJan Beulich 		}
233053809751SJan Beulich 
23311da177e4SLinus Torvalds 		switch (qualifier) {
23321da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
233353809751SJan Beulich 			if (is_sign)
233453809751SJan Beulich 				*va_arg(args, signed char *) = val.s;
233553809751SJan Beulich 			else
233653809751SJan Beulich 				*va_arg(args, unsigned char *) = val.u;
23371da177e4SLinus Torvalds 			break;
23381da177e4SLinus Torvalds 		case 'h':
233953809751SJan Beulich 			if (is_sign)
234053809751SJan Beulich 				*va_arg(args, short *) = val.s;
234153809751SJan Beulich 			else
234253809751SJan Beulich 				*va_arg(args, unsigned short *) = val.u;
23431da177e4SLinus Torvalds 			break;
23441da177e4SLinus Torvalds 		case 'l':
234553809751SJan Beulich 			if (is_sign)
234653809751SJan Beulich 				*va_arg(args, long *) = val.s;
234753809751SJan Beulich 			else
234853809751SJan Beulich 				*va_arg(args, unsigned long *) = val.u;
23491da177e4SLinus Torvalds 			break;
23501da177e4SLinus Torvalds 		case 'L':
235153809751SJan Beulich 			if (is_sign)
235253809751SJan Beulich 				*va_arg(args, long long *) = val.s;
235353809751SJan Beulich 			else
235453809751SJan Beulich 				*va_arg(args, unsigned long long *) = val.u;
23551da177e4SLinus Torvalds 			break;
23561da177e4SLinus Torvalds 		case 'Z':
23571da177e4SLinus Torvalds 		case 'z':
235853809751SJan Beulich 			*va_arg(args, size_t *) = val.u;
23591da177e4SLinus Torvalds 			break;
23601da177e4SLinus Torvalds 		default:
236153809751SJan Beulich 			if (is_sign)
236253809751SJan Beulich 				*va_arg(args, int *) = val.s;
236353809751SJan Beulich 			else
236453809751SJan Beulich 				*va_arg(args, unsigned int *) = val.u;
23651da177e4SLinus Torvalds 			break;
23661da177e4SLinus Torvalds 		}
23671da177e4SLinus Torvalds 		num++;
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds 		if (!next)
23701da177e4SLinus Torvalds 			break;
23711da177e4SLinus Torvalds 		str = next;
23721da177e4SLinus Torvalds 	}
2373c6b40d16SJohannes Berg 
23741da177e4SLinus Torvalds 	return num;
23751da177e4SLinus Torvalds }
23761da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds /**
23791da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
23801da177e4SLinus Torvalds  * @buf:	input buffer
23811da177e4SLinus Torvalds  * @fmt:	formatting of buffer
23821da177e4SLinus Torvalds  * @...:	resulting arguments
23831da177e4SLinus Torvalds  */
23841da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
23851da177e4SLinus Torvalds {
23861da177e4SLinus Torvalds 	va_list args;
23871da177e4SLinus Torvalds 	int i;
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds 	va_start(args, fmt);
23901da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
23911da177e4SLinus Torvalds 	va_end(args);
23927b9186f5SAndré Goddard Rosa 
23931da177e4SLinus Torvalds 	return i;
23941da177e4SLinus Torvalds }
23951da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
2396