xref: /openbmc/linux/lib/vsprintf.c (revision e49317d415f5a44bad8377a208d61902d752303e)
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>
260fe1ef24SLinus Torvalds #include <linux/uaccess.h>
27332d2e78SLinus Torvalds #include <linux/ioport.h>
288a27f7c9SJoe Perches #include <net/addrconf.h>
291da177e4SLinus Torvalds 
304e57b681STim Schmielau #include <asm/page.h>		/* for PAGE_SIZE */
311da177e4SLinus Torvalds #include <asm/div64.h>
32deac93dfSJames Bottomley #include <asm/sections.h>	/* for dereference_function_descriptor() */
331da177e4SLinus Torvalds 
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
411da177e4SLinus Torvalds  */
421da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
431da177e4SLinus Torvalds {
441dff46d6SAlexey Dobriyan 	unsigned long long result;
451dff46d6SAlexey Dobriyan 	unsigned int rv;
461da177e4SLinus Torvalds 
471dff46d6SAlexey Dobriyan 	cp = _parse_integer_fixup_radix(cp, &base);
481dff46d6SAlexey Dobriyan 	rv = _parse_integer(cp, base, &result);
491dff46d6SAlexey Dobriyan 	/* FIXME */
501dff46d6SAlexey Dobriyan 	cp += (rv & ~KSTRTOX_OVERFLOW);
51aa46a63eSHarvey Harrison 
521da177e4SLinus Torvalds 	if (endp)
531da177e4SLinus Torvalds 		*endp = (char *)cp;
547b9186f5SAndré Goddard Rosa 
551da177e4SLinus Torvalds 	return result;
561da177e4SLinus Torvalds }
571da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /**
60922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
61922ac25cSAndré Goddard Rosa  * @cp: The start of the string
62922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
63922ac25cSAndré Goddard Rosa  * @base: The number base to use
64922ac25cSAndré Goddard Rosa  */
65922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
66922ac25cSAndré Goddard Rosa {
67922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
68922ac25cSAndré Goddard Rosa }
69922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
70922ac25cSAndré Goddard Rosa 
71922ac25cSAndré Goddard Rosa /**
72922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
73922ac25cSAndré Goddard Rosa  * @cp: The start of the string
74922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
75922ac25cSAndré Goddard Rosa  * @base: The number base to use
76922ac25cSAndré Goddard Rosa  */
77922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
78922ac25cSAndré Goddard Rosa {
79922ac25cSAndré Goddard Rosa 	if (*cp == '-')
80922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
81922ac25cSAndré Goddard Rosa 
82922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
83922ac25cSAndré Goddard Rosa }
84922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
85922ac25cSAndré Goddard Rosa 
86922ac25cSAndré Goddard Rosa /**
871da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
881da177e4SLinus Torvalds  * @cp: The start of the string
891da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
901da177e4SLinus Torvalds  * @base: The number base to use
911da177e4SLinus Torvalds  */
921da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds 	if (*cp == '-')
951da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
967b9186f5SAndré Goddard Rosa 
971da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
981da177e4SLinus Torvalds }
9998d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1001da177e4SLinus Torvalds 
101cf3b429bSJoe Perches static noinline_for_stack
102cf3b429bSJoe Perches int skip_atoi(const char **s)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	int i = 0;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	while (isdigit(**s))
1071da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
1087b9186f5SAndré Goddard Rosa 
1091da177e4SLinus Torvalds 	return i;
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1124277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used
1134277eeddSDenis Vlasenko  * for /proc and /sys data. This directly impacts e.g. top performance
1144277eeddSDenis Vlasenko  * with many processes running. We optimize it for speed
115133fd9f5SDenys Vlasenko  * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
116133fd9f5SDenys Vlasenko  * (with permission from the author, Douglas W. Jones).
117133fd9f5SDenys Vlasenko  */
1184277eeddSDenis Vlasenko 
119133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
120133fd9f5SDenys Vlasenko /* Formats correctly any integer in [0, 999999999] */
121cf3b429bSJoe Perches static noinline_for_stack
122133fd9f5SDenys Vlasenko char *put_dec_full9(char *buf, unsigned q)
1234277eeddSDenis Vlasenko {
124133fd9f5SDenys Vlasenko 	unsigned r;
1254277eeddSDenis Vlasenko 
1267b9186f5SAndré Goddard Rosa 	/*
1277b9186f5SAndré Goddard Rosa 	 * Possible ways to approx. divide by 10
128133fd9f5SDenys Vlasenko 	 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
129133fd9f5SDenys Vlasenko 	 * (x * 0xcccd) >> 19     x <      81920 (x < 262149 when 64-bit mul)
130133fd9f5SDenys Vlasenko 	 * (x * 0x6667) >> 18     x <      43699
131133fd9f5SDenys Vlasenko 	 * (x * 0x3334) >> 17     x <      16389
132133fd9f5SDenys Vlasenko 	 * (x * 0x199a) >> 16     x <      16389
133133fd9f5SDenys Vlasenko 	 * (x * 0x0ccd) >> 15     x <      16389
134133fd9f5SDenys Vlasenko 	 * (x * 0x0667) >> 14     x <       2739
135133fd9f5SDenys Vlasenko 	 * (x * 0x0334) >> 13     x <       1029
136133fd9f5SDenys Vlasenko 	 * (x * 0x019a) >> 12     x <       1029
137133fd9f5SDenys Vlasenko 	 * (x * 0x00cd) >> 11     x <       1029 shorter code than * 0x67 (on i386)
138133fd9f5SDenys Vlasenko 	 * (x * 0x0067) >> 10     x <        179
139133fd9f5SDenys Vlasenko 	 * (x * 0x0034) >>  9     x <         69 same
140133fd9f5SDenys Vlasenko 	 * (x * 0x001a) >>  8     x <         69 same
141133fd9f5SDenys Vlasenko 	 * (x * 0x000d) >>  7     x <         69 same, shortest code (on i386)
142133fd9f5SDenys Vlasenko 	 * (x * 0x0007) >>  6     x <         19
143133fd9f5SDenys Vlasenko 	 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
1447b9186f5SAndré Goddard Rosa 	 */
145133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
146133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 1 */
147133fd9f5SDenys Vlasenko 	q      = (r * (uint64_t)0x1999999a) >> 32;
148133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 2 */
149133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
150133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 3 */
151133fd9f5SDenys Vlasenko 	q      = (r * (uint64_t)0x1999999a) >> 32;
152133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 4 */
153133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
154133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 5 */
155133fd9f5SDenys Vlasenko 	/* Now value is under 10000, can avoid 64-bit multiply */
156133fd9f5SDenys Vlasenko 	q      = (r * 0x199a) >> 16;
157133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q)  + '0'; /* 6 */
158133fd9f5SDenys Vlasenko 	r      = (q * 0xcd) >> 11;
159133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r)  + '0'; /* 7 */
160133fd9f5SDenys Vlasenko 	q      = (r * 0xcd) >> 11;
161133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 8 */
162133fd9f5SDenys Vlasenko 	*buf++ = q + '0'; /* 9 */
163133fd9f5SDenys Vlasenko 	return buf;
164133fd9f5SDenys Vlasenko }
165133fd9f5SDenys Vlasenko #endif
1664277eeddSDenis Vlasenko 
167133fd9f5SDenys Vlasenko /* Similar to above but do not pad with zeros.
168133fd9f5SDenys Vlasenko  * Code can be easily arranged to print 9 digits too, but our callers
169133fd9f5SDenys Vlasenko  * always call put_dec_full9() instead when the number has 9 decimal digits.
170133fd9f5SDenys Vlasenko  */
171133fd9f5SDenys Vlasenko static noinline_for_stack
172133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r)
173133fd9f5SDenys Vlasenko {
174133fd9f5SDenys Vlasenko 	unsigned q;
1754277eeddSDenis Vlasenko 
176133fd9f5SDenys Vlasenko 	/* Copy of previous function's body with added early returns */
177133fd9f5SDenys Vlasenko 	q      = (r * (uint64_t)0x1999999a) >> 32;
178133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 2 */
179133fd9f5SDenys Vlasenko 	if (q == 0)
180133fd9f5SDenys Vlasenko 		return buf;
181133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
182133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 3 */
183133fd9f5SDenys Vlasenko 	if (r == 0)
184133fd9f5SDenys Vlasenko 		return buf;
185133fd9f5SDenys Vlasenko 	q      = (r * (uint64_t)0x1999999a) >> 32;
186133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 4 */
187133fd9f5SDenys Vlasenko 	if (q == 0)
188133fd9f5SDenys Vlasenko 		return buf;
189133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
190133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 5 */
191133fd9f5SDenys Vlasenko 	if (r == 0)
192133fd9f5SDenys Vlasenko 		return buf;
193133fd9f5SDenys Vlasenko 	q      = (r * 0x199a) >> 16;
194133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q)  + '0'; /* 6 */
195133fd9f5SDenys Vlasenko 	if (q == 0)
196133fd9f5SDenys Vlasenko 		return buf;
197133fd9f5SDenys Vlasenko 	r      = (q * 0xcd) >> 11;
198133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r)  + '0'; /* 7 */
199133fd9f5SDenys Vlasenko 	if (r == 0)
200133fd9f5SDenys Vlasenko 		return buf;
201133fd9f5SDenys Vlasenko 	q      = (r * 0xcd) >> 11;
202133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 8 */
203133fd9f5SDenys Vlasenko 	if (q == 0)
204133fd9f5SDenys Vlasenko 		return buf;
205133fd9f5SDenys Vlasenko 	*buf++ = q + '0'; /* 9 */
206133fd9f5SDenys Vlasenko 	return buf;
207133fd9f5SDenys Vlasenko }
208133fd9f5SDenys Vlasenko 
209133fd9f5SDenys Vlasenko /* There are two algorithms to print larger numbers.
210133fd9f5SDenys Vlasenko  * One is generic: divide by 1000000000 and repeatedly print
211133fd9f5SDenys Vlasenko  * groups of (up to) 9 digits. It's conceptually simple,
212133fd9f5SDenys Vlasenko  * but requires a (unsigned long long) / 1000000000 division.
213133fd9f5SDenys Vlasenko  *
214133fd9f5SDenys Vlasenko  * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
215133fd9f5SDenys Vlasenko  * manipulates them cleverly and generates groups of 4 decimal digits.
216133fd9f5SDenys Vlasenko  * It so happens that it does NOT require long long division.
217133fd9f5SDenys Vlasenko  *
218133fd9f5SDenys Vlasenko  * If long is > 32 bits, division of 64-bit values is relatively easy,
219133fd9f5SDenys Vlasenko  * and we will use the first algorithm.
220133fd9f5SDenys Vlasenko  * If long long is > 64 bits (strange architecture with VERY large long long),
221133fd9f5SDenys Vlasenko  * second algorithm can't be used, and we again use the first one.
222133fd9f5SDenys Vlasenko  *
223133fd9f5SDenys Vlasenko  * Else (if long is 32 bits and long long is 64 bits) we use second one.
224133fd9f5SDenys Vlasenko  */
225133fd9f5SDenys Vlasenko 
226133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
227133fd9f5SDenys Vlasenko 
228133fd9f5SDenys Vlasenko /* First algorithm: generic */
229133fd9f5SDenys Vlasenko 
230133fd9f5SDenys Vlasenko static
231133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
232133fd9f5SDenys Vlasenko {
233133fd9f5SDenys Vlasenko 	if (n >= 100*1000*1000) {
234133fd9f5SDenys Vlasenko 		while (n >= 1000*1000*1000)
235133fd9f5SDenys Vlasenko 			buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
236133fd9f5SDenys Vlasenko 		if (n >= 100*1000*1000)
237133fd9f5SDenys Vlasenko 			return put_dec_full9(buf, n);
238133fd9f5SDenys Vlasenko 	}
239133fd9f5SDenys Vlasenko 	return put_dec_trunc8(buf, n);
240133fd9f5SDenys Vlasenko }
241133fd9f5SDenys Vlasenko 
242133fd9f5SDenys Vlasenko #else
243133fd9f5SDenys Vlasenko 
244133fd9f5SDenys Vlasenko /* Second algorithm: valid only for 64-bit long longs */
245133fd9f5SDenys Vlasenko 
246*e49317d4SGeorge Spelvin /* See comment in put_dec_full9 for choice of constants */
247133fd9f5SDenys Vlasenko static noinline_for_stack
248133fd9f5SDenys Vlasenko char *put_dec_full4(char *buf, unsigned q)
249133fd9f5SDenys Vlasenko {
250133fd9f5SDenys Vlasenko 	unsigned r;
251*e49317d4SGeorge Spelvin 	r      = (q * 0xccd) >> 15;
252133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0';
253*e49317d4SGeorge Spelvin 	q      = (r * 0xcd) >> 11;
254133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q)  + '0';
255133fd9f5SDenys Vlasenko 	r      = (q * 0xcd) >> 11;
256133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r)  + '0';
257133fd9f5SDenys Vlasenko 	*buf++ = r + '0';
258133fd9f5SDenys Vlasenko 	return buf;
259133fd9f5SDenys Vlasenko }
260133fd9f5SDenys Vlasenko 
261133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at
262133fd9f5SDenys Vlasenko  * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
263133fd9f5SDenys Vlasenko  * (with permission from the author).
264133fd9f5SDenys Vlasenko  * Performs no 64-bit division and hence should be fast on 32-bit machines.
265133fd9f5SDenys Vlasenko  */
266133fd9f5SDenys Vlasenko static
267133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
268133fd9f5SDenys Vlasenko {
269133fd9f5SDenys Vlasenko 	uint32_t d3, d2, d1, q, h;
270133fd9f5SDenys Vlasenko 
271133fd9f5SDenys Vlasenko 	if (n < 100*1000*1000)
272133fd9f5SDenys Vlasenko 		return put_dec_trunc8(buf, n);
273133fd9f5SDenys Vlasenko 
274133fd9f5SDenys Vlasenko 	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
275133fd9f5SDenys Vlasenko 	h   = (n >> 32);
276133fd9f5SDenys Vlasenko 	d2  = (h      ) & 0xffff;
277133fd9f5SDenys Vlasenko 	d3  = (h >> 16); /* implicit "& 0xffff" */
278133fd9f5SDenys Vlasenko 
279133fd9f5SDenys Vlasenko 	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
280133fd9f5SDenys Vlasenko 
281133fd9f5SDenys Vlasenko 	buf = put_dec_full4(buf, q % 10000);
282133fd9f5SDenys Vlasenko 	q   = q / 10000;
283133fd9f5SDenys Vlasenko 
284133fd9f5SDenys Vlasenko 	d1  = q + 7671 * d3 + 9496 * d2 + 6 * d1;
285133fd9f5SDenys Vlasenko 	buf = put_dec_full4(buf, d1 % 10000);
286133fd9f5SDenys Vlasenko 	q   = d1 / 10000;
287133fd9f5SDenys Vlasenko 
288133fd9f5SDenys Vlasenko 	d2  = q + 4749 * d3 + 42 * d2;
289133fd9f5SDenys Vlasenko 	buf = put_dec_full4(buf, d2 % 10000);
290133fd9f5SDenys Vlasenko 	q   = d2 / 10000;
291133fd9f5SDenys Vlasenko 
292133fd9f5SDenys Vlasenko 	d3  = q + 281 * d3;
293133fd9f5SDenys Vlasenko 	if (!d3)
294133fd9f5SDenys Vlasenko 		goto done;
295133fd9f5SDenys Vlasenko 	buf = put_dec_full4(buf, d3 % 10000);
296133fd9f5SDenys Vlasenko 	q   = d3 / 10000;
297133fd9f5SDenys Vlasenko 	if (!q)
298133fd9f5SDenys Vlasenko 		goto done;
299133fd9f5SDenys Vlasenko 	buf = put_dec_full4(buf, q);
300133fd9f5SDenys Vlasenko  done:
301133fd9f5SDenys Vlasenko 	while (buf[-1] == '0')
302133fd9f5SDenys Vlasenko 		--buf;
3037b9186f5SAndré Goddard Rosa 
3044277eeddSDenis Vlasenko 	return buf;
3054277eeddSDenis Vlasenko }
306133fd9f5SDenys Vlasenko 
307133fd9f5SDenys Vlasenko #endif
3084277eeddSDenis Vlasenko 
3091ac101a5SKAMEZAWA Hiroyuki /*
3101ac101a5SKAMEZAWA Hiroyuki  * Convert passed number to decimal string.
3111ac101a5SKAMEZAWA Hiroyuki  * Returns the length of string.  On buffer overflow, returns 0.
3121ac101a5SKAMEZAWA Hiroyuki  *
3131ac101a5SKAMEZAWA Hiroyuki  * If speed is not important, use snprintf(). It's easy to read the code.
3141ac101a5SKAMEZAWA Hiroyuki  */
3151ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num)
3161ac101a5SKAMEZAWA Hiroyuki {
317133fd9f5SDenys Vlasenko 	char tmp[sizeof(num) * 3];
3181ac101a5SKAMEZAWA Hiroyuki 	int idx, len;
3191ac101a5SKAMEZAWA Hiroyuki 
320133fd9f5SDenys Vlasenko 	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
321133fd9f5SDenys Vlasenko 	if (num <= 9) {
322133fd9f5SDenys Vlasenko 		tmp[0] = '0' + num;
323133fd9f5SDenys Vlasenko 		len = 1;
324133fd9f5SDenys Vlasenko 	} else {
3251ac101a5SKAMEZAWA Hiroyuki 		len = put_dec(tmp, num) - tmp;
326133fd9f5SDenys Vlasenko 	}
3271ac101a5SKAMEZAWA Hiroyuki 
3281ac101a5SKAMEZAWA Hiroyuki 	if (len > size)
3291ac101a5SKAMEZAWA Hiroyuki 		return 0;
3301ac101a5SKAMEZAWA Hiroyuki 	for (idx = 0; idx < len; ++idx)
3311ac101a5SKAMEZAWA Hiroyuki 		buf[idx] = tmp[len - idx - 1];
3321ac101a5SKAMEZAWA Hiroyuki 	return len;
3331ac101a5SKAMEZAWA Hiroyuki }
3341ac101a5SKAMEZAWA Hiroyuki 
3351da177e4SLinus Torvalds #define ZEROPAD	1		/* pad with zero */
3361da177e4SLinus Torvalds #define SIGN	2		/* unsigned/signed long */
3371da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3381da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
3391da177e4SLinus Torvalds #define LEFT	16		/* left justified */
340b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
341b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
3421da177e4SLinus Torvalds 
343fef20d9cSFrederic Weisbecker enum format_type {
344fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
345ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
346fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
347fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
348fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
349fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
350fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
351fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
352fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
353fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
354fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
355a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
356a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
357fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
358fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
359fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
360fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
361fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NRCHARS,
362fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
363fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
364fef20d9cSFrederic Weisbecker };
365fef20d9cSFrederic Weisbecker 
366fef20d9cSFrederic Weisbecker struct printf_spec {
3674e310fdaSJoe Perches 	u8	type;		/* format_type enum */
368ef0658f3SJoe Perches 	u8	flags;		/* flags to number() */
3694e310fdaSJoe Perches 	u8	base;		/* number base, 8, 10 or 16 only */
3704e310fdaSJoe Perches 	u8	qualifier;	/* number qualifier, one of 'hHlLtzZ' */
3714e310fdaSJoe Perches 	s16	field_width;	/* width of output field */
3724e310fdaSJoe Perches 	s16	precision;	/* # of digits/chars */
373fef20d9cSFrederic Weisbecker };
374fef20d9cSFrederic Weisbecker 
375cf3b429bSJoe Perches static noinline_for_stack
376cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
377fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
3781da177e4SLinus Torvalds {
3799b706aeeSDenys Vlasenko 	/* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
3809b706aeeSDenys Vlasenko 	static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
3819b706aeeSDenys Vlasenko 
3829b706aeeSDenys Vlasenko 	char tmp[66];
3839b706aeeSDenys Vlasenko 	char sign;
3849b706aeeSDenys Vlasenko 	char locase;
385fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
3861da177e4SLinus Torvalds 	int i;
3877c203422SPierre Carrier 	bool is_zero = num == 0LL;
3881da177e4SLinus Torvalds 
3899b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
3909b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
391fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
392fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
393fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
3941da177e4SLinus Torvalds 	sign = 0;
395fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
3961da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
3971da177e4SLinus Torvalds 			sign = '-';
3981da177e4SLinus Torvalds 			num = -(signed long long)num;
399fef20d9cSFrederic Weisbecker 			spec.field_width--;
400fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4011da177e4SLinus Torvalds 			sign = '+';
402fef20d9cSFrederic Weisbecker 			spec.field_width--;
403fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4041da177e4SLinus Torvalds 			sign = ' ';
405fef20d9cSFrederic Weisbecker 			spec.field_width--;
4061da177e4SLinus Torvalds 		}
4071da177e4SLinus Torvalds 	}
408b39a7340SDenis Vlasenko 	if (need_pfx) {
409fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
4107c203422SPierre Carrier 			spec.field_width -= 2;
4117c203422SPierre Carrier 		else if (!is_zero)
412fef20d9cSFrederic Weisbecker 			spec.field_width--;
4131da177e4SLinus Torvalds 	}
414b39a7340SDenis Vlasenko 
415b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4161da177e4SLinus Torvalds 	i = 0;
417133fd9f5SDenys Vlasenko 	if (num < spec.base)
418133fd9f5SDenys Vlasenko 		tmp[i++] = digits[num] | locase;
4194277eeddSDenis Vlasenko 	/* Generic code, for any base:
4204277eeddSDenis Vlasenko 	else do {
4219b706aeeSDenys Vlasenko 		tmp[i++] = (digits[do_div(num,base)] | locase);
4224277eeddSDenis Vlasenko 	} while (num != 0);
4234277eeddSDenis Vlasenko 	*/
424fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
425fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
426b39a7340SDenis Vlasenko 		int shift = 3;
4277b9186f5SAndré Goddard Rosa 
4287b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4297b9186f5SAndré Goddard Rosa 			shift = 4;
430b39a7340SDenis Vlasenko 		do {
4319b706aeeSDenys Vlasenko 			tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
432b39a7340SDenis Vlasenko 			num >>= shift;
433b39a7340SDenis Vlasenko 		} while (num);
4344277eeddSDenis Vlasenko 	} else { /* base 10 */
4354277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4364277eeddSDenis Vlasenko 	}
437b39a7340SDenis Vlasenko 
438b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
439fef20d9cSFrederic Weisbecker 	if (i > spec.precision)
440fef20d9cSFrederic Weisbecker 		spec.precision = i;
441b39a7340SDenis Vlasenko 	/* leading space padding */
442fef20d9cSFrederic Weisbecker 	spec.field_width -= spec.precision;
443fef20d9cSFrederic Weisbecker 	if (!(spec.flags & (ZEROPAD+LEFT))) {
444fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
445f796937aSJeremy Fitzhardinge 			if (buf < end)
4461da177e4SLinus Torvalds 				*buf = ' ';
4471da177e4SLinus Torvalds 			++buf;
4481da177e4SLinus Torvalds 		}
4491da177e4SLinus Torvalds 	}
450b39a7340SDenis Vlasenko 	/* sign */
4511da177e4SLinus Torvalds 	if (sign) {
452f796937aSJeremy Fitzhardinge 		if (buf < end)
4531da177e4SLinus Torvalds 			*buf = sign;
4541da177e4SLinus Torvalds 		++buf;
4551da177e4SLinus Torvalds 	}
456b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
457b39a7340SDenis Vlasenko 	if (need_pfx) {
4587c203422SPierre Carrier 		if (spec.base == 16 || !is_zero) {
459f796937aSJeremy Fitzhardinge 			if (buf < end)
4601da177e4SLinus Torvalds 				*buf = '0';
4611da177e4SLinus Torvalds 			++buf;
4627c203422SPierre Carrier 		}
463fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
464f796937aSJeremy Fitzhardinge 			if (buf < end)
4659b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
4661da177e4SLinus Torvalds 			++buf;
4671da177e4SLinus Torvalds 		}
4681da177e4SLinus Torvalds 	}
469b39a7340SDenis Vlasenko 	/* zero or space padding */
470fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
471fef20d9cSFrederic Weisbecker 		char c = (spec.flags & ZEROPAD) ? '0' : ' ';
472fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
473f796937aSJeremy Fitzhardinge 			if (buf < end)
4741da177e4SLinus Torvalds 				*buf = c;
4751da177e4SLinus Torvalds 			++buf;
4761da177e4SLinus Torvalds 		}
4771da177e4SLinus Torvalds 	}
478b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
479fef20d9cSFrederic Weisbecker 	while (i <= --spec.precision) {
480f796937aSJeremy Fitzhardinge 		if (buf < end)
4811da177e4SLinus Torvalds 			*buf = '0';
4821da177e4SLinus Torvalds 		++buf;
4831da177e4SLinus Torvalds 	}
484b39a7340SDenis Vlasenko 	/* actual digits of result */
485b39a7340SDenis Vlasenko 	while (--i >= 0) {
486f796937aSJeremy Fitzhardinge 		if (buf < end)
4871da177e4SLinus Torvalds 			*buf = tmp[i];
4881da177e4SLinus Torvalds 		++buf;
4891da177e4SLinus Torvalds 	}
490b39a7340SDenis Vlasenko 	/* trailing space padding */
491fef20d9cSFrederic Weisbecker 	while (--spec.field_width >= 0) {
492f796937aSJeremy Fitzhardinge 		if (buf < end)
4931da177e4SLinus Torvalds 			*buf = ' ';
4941da177e4SLinus Torvalds 		++buf;
4951da177e4SLinus Torvalds 	}
4967b9186f5SAndré Goddard Rosa 
4971da177e4SLinus Torvalds 	return buf;
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
500cf3b429bSJoe Perches static noinline_for_stack
501cf3b429bSJoe Perches char *string(char *buf, char *end, const char *s, struct printf_spec spec)
5020f9bfa56SLinus Torvalds {
5030f9bfa56SLinus Torvalds 	int len, i;
5040f9bfa56SLinus Torvalds 
5050f9bfa56SLinus Torvalds 	if ((unsigned long)s < PAGE_SIZE)
5060f4f81dcSAndré Goddard Rosa 		s = "(null)";
5070f9bfa56SLinus Torvalds 
508fef20d9cSFrederic Weisbecker 	len = strnlen(s, spec.precision);
5090f9bfa56SLinus Torvalds 
510fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
511fef20d9cSFrederic Weisbecker 		while (len < spec.field_width--) {
5120f9bfa56SLinus Torvalds 			if (buf < end)
5130f9bfa56SLinus Torvalds 				*buf = ' ';
5140f9bfa56SLinus Torvalds 			++buf;
5150f9bfa56SLinus Torvalds 		}
5160f9bfa56SLinus Torvalds 	}
5170f9bfa56SLinus Torvalds 	for (i = 0; i < len; ++i) {
5180f9bfa56SLinus Torvalds 		if (buf < end)
5190f9bfa56SLinus Torvalds 			*buf = *s;
5200f9bfa56SLinus Torvalds 		++buf; ++s;
5210f9bfa56SLinus Torvalds 	}
522fef20d9cSFrederic Weisbecker 	while (len < spec.field_width--) {
5230f9bfa56SLinus Torvalds 		if (buf < end)
5240f9bfa56SLinus Torvalds 			*buf = ' ';
5250f9bfa56SLinus Torvalds 		++buf;
5260f9bfa56SLinus Torvalds 	}
5277b9186f5SAndré Goddard Rosa 
5280f9bfa56SLinus Torvalds 	return buf;
5290f9bfa56SLinus Torvalds }
5300f9bfa56SLinus Torvalds 
531cf3b429bSJoe Perches static noinline_for_stack
532cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
5330c8b946eSFrederic Weisbecker 		    struct printf_spec spec, char ext)
5340fe1ef24SLinus Torvalds {
5350fe1ef24SLinus Torvalds 	unsigned long value = (unsigned long) ptr;
5360fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
5370fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
5380f77a8d3SNamhyung Kim 	if (ext == 'B')
5390f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
5400f77a8d3SNamhyung Kim 	else if (ext != 'f' && ext != 's')
5410fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
5420c8b946eSFrederic Weisbecker 	else
5434796dd20SStephen Boyd 		sprint_symbol_no_offset(sym, value);
5447b9186f5SAndré Goddard Rosa 
545fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
5460fe1ef24SLinus Torvalds #else
547fef20d9cSFrederic Weisbecker 	spec.field_width = 2 * sizeof(void *);
548fef20d9cSFrederic Weisbecker 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
549fef20d9cSFrederic Weisbecker 	spec.base = 16;
5507b9186f5SAndré Goddard Rosa 
551fef20d9cSFrederic Weisbecker 	return number(buf, end, value, spec);
5520fe1ef24SLinus Torvalds #endif
5530fe1ef24SLinus Torvalds }
5540fe1ef24SLinus Torvalds 
555cf3b429bSJoe Perches static noinline_for_stack
556cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
557fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
558332d2e78SLinus Torvalds {
559332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
56028405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
561332d2e78SLinus Torvalds #endif
562332d2e78SLinus Torvalds 
563332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
56428405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
565332d2e78SLinus Torvalds #endif
5664da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
567fef20d9cSFrederic Weisbecker 		.base = 16,
5684da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
569fef20d9cSFrederic Weisbecker 		.precision = -1,
570fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
571fef20d9cSFrederic Weisbecker 	};
5724da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
5734da0b66cSBjorn Helgaas 		.base = 16,
5744da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
5754da0b66cSBjorn Helgaas 		.precision = -1,
5764da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
5774da0b66cSBjorn Helgaas 	};
5780f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
5790f4050c7SBjorn Helgaas 		.base = 16,
5800f4050c7SBjorn Helgaas 		.field_width = 2,
5810f4050c7SBjorn Helgaas 		.precision = -1,
5820f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
5830f4050c7SBjorn Helgaas 	};
5844da0b66cSBjorn Helgaas 	static const struct printf_spec dec_spec = {
585c91d3376SBjorn Helgaas 		.base = 10,
586c91d3376SBjorn Helgaas 		.precision = -1,
587c91d3376SBjorn Helgaas 		.flags = 0,
588c91d3376SBjorn Helgaas 	};
5894da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
590fd95541eSBjorn Helgaas 		.field_width = -1,
591fd95541eSBjorn Helgaas 		.precision = 10,
592fd95541eSBjorn Helgaas 		.flags = LEFT,
593fd95541eSBjorn Helgaas 	};
5944da0b66cSBjorn Helgaas 	static const struct printf_spec flag_spec = {
595fd95541eSBjorn Helgaas 		.base = 16,
596fd95541eSBjorn Helgaas 		.precision = -1,
597fd95541eSBjorn Helgaas 		.flags = SPECIAL | SMALL,
598fd95541eSBjorn Helgaas 	};
599c7dabef8SBjorn Helgaas 
600c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
601c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
602c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
603c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
6049d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
605c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
606c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
607c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
608c7dabef8SBjorn Helgaas 
609332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
610c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
6114da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
612332d2e78SLinus Torvalds 
613332d2e78SLinus Torvalds 	*p++ = '[';
6144da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
615fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
6164da0b66cSBjorn Helgaas 		specp = &io_spec;
6174da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
618fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
6194da0b66cSBjorn Helgaas 		specp = &mem_spec;
6204da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
621fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
6224da0b66cSBjorn Helgaas 		specp = &dec_spec;
6234da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
624fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
6254da0b66cSBjorn Helgaas 		specp = &dec_spec;
6260f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
6270f4050c7SBjorn Helgaas 		p = string(p, pend, "bus ", str_spec);
6280f4050c7SBjorn Helgaas 		specp = &bus_spec;
6294da0b66cSBjorn Helgaas 	} else {
630c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
6314da0b66cSBjorn Helgaas 		specp = &mem_spec;
632c7dabef8SBjorn Helgaas 		decode = 0;
633fd95541eSBjorn Helgaas 	}
6344da0b66cSBjorn Helgaas 	p = number(p, pend, res->start, *specp);
635c91d3376SBjorn Helgaas 	if (res->start != res->end) {
636332d2e78SLinus Torvalds 		*p++ = '-';
6374da0b66cSBjorn Helgaas 		p = number(p, pend, res->end, *specp);
638c91d3376SBjorn Helgaas 	}
639c7dabef8SBjorn Helgaas 	if (decode) {
640fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
641fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
642fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
643fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
6449d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
6459d7cca04SBjorn Helgaas 			p = string(p, pend, " window", str_spec);
646fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
647fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
648c7dabef8SBjorn Helgaas 	} else {
649fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
650c7dabef8SBjorn Helgaas 		p = number(p, pend, res->flags, flag_spec);
651fd95541eSBjorn Helgaas 	}
652332d2e78SLinus Torvalds 	*p++ = ']';
653c7dabef8SBjorn Helgaas 	*p = '\0';
654332d2e78SLinus Torvalds 
655fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
656332d2e78SLinus Torvalds }
657332d2e78SLinus Torvalds 
658cf3b429bSJoe Perches static noinline_for_stack
65931550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
66031550a16SAndy Shevchenko 		 const char *fmt)
66131550a16SAndy Shevchenko {
66231550a16SAndy Shevchenko 	int i, len = 1;		/* if we pass '%ph[CDN]', field witdh remains
66331550a16SAndy Shevchenko 				   negative value, fallback to the default */
66431550a16SAndy Shevchenko 	char separator;
66531550a16SAndy Shevchenko 
66631550a16SAndy Shevchenko 	if (spec.field_width == 0)
66731550a16SAndy Shevchenko 		/* nothing to print */
66831550a16SAndy Shevchenko 		return buf;
66931550a16SAndy Shevchenko 
67031550a16SAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
67131550a16SAndy Shevchenko 		/* NULL pointer */
67231550a16SAndy Shevchenko 		return string(buf, end, NULL, spec);
67331550a16SAndy Shevchenko 
67431550a16SAndy Shevchenko 	switch (fmt[1]) {
67531550a16SAndy Shevchenko 	case 'C':
67631550a16SAndy Shevchenko 		separator = ':';
67731550a16SAndy Shevchenko 		break;
67831550a16SAndy Shevchenko 	case 'D':
67931550a16SAndy Shevchenko 		separator = '-';
68031550a16SAndy Shevchenko 		break;
68131550a16SAndy Shevchenko 	case 'N':
68231550a16SAndy Shevchenko 		separator = 0;
68331550a16SAndy Shevchenko 		break;
68431550a16SAndy Shevchenko 	default:
68531550a16SAndy Shevchenko 		separator = ' ';
68631550a16SAndy Shevchenko 		break;
68731550a16SAndy Shevchenko 	}
68831550a16SAndy Shevchenko 
68931550a16SAndy Shevchenko 	if (spec.field_width > 0)
69031550a16SAndy Shevchenko 		len = min_t(int, spec.field_width, 64);
69131550a16SAndy Shevchenko 
69231550a16SAndy Shevchenko 	for (i = 0; i < len && buf < end - 1; i++) {
69331550a16SAndy Shevchenko 		buf = hex_byte_pack(buf, addr[i]);
69431550a16SAndy Shevchenko 
69531550a16SAndy Shevchenko 		if (buf < end && separator && i != len - 1)
69631550a16SAndy Shevchenko 			*buf++ = separator;
69731550a16SAndy Shevchenko 	}
69831550a16SAndy Shevchenko 
69931550a16SAndy Shevchenko 	return buf;
70031550a16SAndy Shevchenko }
70131550a16SAndy Shevchenko 
70231550a16SAndy Shevchenko static noinline_for_stack
703cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
7048a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
705dd45c9cfSHarvey Harrison {
7068a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
707dd45c9cfSHarvey Harrison 	char *p = mac_addr;
708dd45c9cfSHarvey Harrison 	int i;
709bc7259a2SJoe Perches 	char separator;
71076597ff9SAndrei Emeltchenko 	bool reversed = false;
711bc7259a2SJoe Perches 
71276597ff9SAndrei Emeltchenko 	switch (fmt[1]) {
71376597ff9SAndrei Emeltchenko 	case 'F':
714bc7259a2SJoe Perches 		separator = '-';
71576597ff9SAndrei Emeltchenko 		break;
71676597ff9SAndrei Emeltchenko 
71776597ff9SAndrei Emeltchenko 	case 'R':
71876597ff9SAndrei Emeltchenko 		reversed = true;
71976597ff9SAndrei Emeltchenko 		/* fall through */
72076597ff9SAndrei Emeltchenko 
72176597ff9SAndrei Emeltchenko 	default:
722bc7259a2SJoe Perches 		separator = ':';
72376597ff9SAndrei Emeltchenko 		break;
724bc7259a2SJoe Perches 	}
725dd45c9cfSHarvey Harrison 
726dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
72776597ff9SAndrei Emeltchenko 		if (reversed)
72876597ff9SAndrei Emeltchenko 			p = hex_byte_pack(p, addr[5 - i]);
72976597ff9SAndrei Emeltchenko 		else
73055036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[i]);
73176597ff9SAndrei Emeltchenko 
7328a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
733bc7259a2SJoe Perches 			*p++ = separator;
734dd45c9cfSHarvey Harrison 	}
735dd45c9cfSHarvey Harrison 	*p = '\0';
736dd45c9cfSHarvey Harrison 
737fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
738dd45c9cfSHarvey Harrison }
739dd45c9cfSHarvey Harrison 
740cf3b429bSJoe Perches static noinline_for_stack
741cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
742689afa7dSHarvey Harrison {
743689afa7dSHarvey Harrison 	int i;
7440159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
7450159f24eSJoe Perches 	int index;
7460159f24eSJoe Perches 	int step;
747689afa7dSHarvey Harrison 
7480159f24eSJoe Perches 	switch (fmt[2]) {
7490159f24eSJoe Perches 	case 'h':
7500159f24eSJoe Perches #ifdef __BIG_ENDIAN
7510159f24eSJoe Perches 		index = 0;
7520159f24eSJoe Perches 		step = 1;
7530159f24eSJoe Perches #else
7540159f24eSJoe Perches 		index = 3;
7550159f24eSJoe Perches 		step = -1;
7560159f24eSJoe Perches #endif
7570159f24eSJoe Perches 		break;
7580159f24eSJoe Perches 	case 'l':
7590159f24eSJoe Perches 		index = 3;
7600159f24eSJoe Perches 		step = -1;
7610159f24eSJoe Perches 		break;
7620159f24eSJoe Perches 	case 'n':
7630159f24eSJoe Perches 	case 'b':
7640159f24eSJoe Perches 	default:
7650159f24eSJoe Perches 		index = 0;
7660159f24eSJoe Perches 		step = 1;
7670159f24eSJoe Perches 		break;
7680159f24eSJoe Perches 	}
7698a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
7708a27f7c9SJoe Perches 		char temp[3];	/* hold each IP quad in reverse order */
771133fd9f5SDenys Vlasenko 		int digits = put_dec_trunc8(temp, addr[index]) - temp;
7728a27f7c9SJoe Perches 		if (leading_zeros) {
7738a27f7c9SJoe Perches 			if (digits < 3)
7748a27f7c9SJoe Perches 				*p++ = '0';
7758a27f7c9SJoe Perches 			if (digits < 2)
7768a27f7c9SJoe Perches 				*p++ = '0';
7778a27f7c9SJoe Perches 		}
7788a27f7c9SJoe Perches 		/* reverse the digits in the quad */
7798a27f7c9SJoe Perches 		while (digits--)
7808a27f7c9SJoe Perches 			*p++ = temp[digits];
7818a27f7c9SJoe Perches 		if (i < 3)
7828a27f7c9SJoe Perches 			*p++ = '.';
7830159f24eSJoe Perches 		index += step;
7848a27f7c9SJoe Perches 	}
7858a27f7c9SJoe Perches 	*p = '\0';
7867b9186f5SAndré Goddard Rosa 
7878a27f7c9SJoe Perches 	return p;
7888a27f7c9SJoe Perches }
7898a27f7c9SJoe Perches 
790cf3b429bSJoe Perches static noinline_for_stack
791cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
7928a27f7c9SJoe Perches {
7937b9186f5SAndré Goddard Rosa 	int i, j, range;
7948a27f7c9SJoe Perches 	unsigned char zerolength[8];
7958a27f7c9SJoe Perches 	int longest = 1;
7968a27f7c9SJoe Perches 	int colonpos = -1;
7978a27f7c9SJoe Perches 	u16 word;
7987b9186f5SAndré Goddard Rosa 	u8 hi, lo;
7998a27f7c9SJoe Perches 	bool needcolon = false;
800eb78cd26SJoe Perches 	bool useIPv4;
801eb78cd26SJoe Perches 	struct in6_addr in6;
802eb78cd26SJoe Perches 
803eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
804eb78cd26SJoe Perches 
805eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
8068a27f7c9SJoe Perches 
8078a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
8088a27f7c9SJoe Perches 
8098a27f7c9SJoe Perches 	if (useIPv4)
8108a27f7c9SJoe Perches 		range = 6;
8118a27f7c9SJoe Perches 	else
8128a27f7c9SJoe Perches 		range = 8;
8138a27f7c9SJoe Perches 
8148a27f7c9SJoe Perches 	/* find position of longest 0 run */
8158a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
8168a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
817eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
8188a27f7c9SJoe Perches 				break;
8198a27f7c9SJoe Perches 			zerolength[i]++;
8208a27f7c9SJoe Perches 		}
8218a27f7c9SJoe Perches 	}
8228a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
8238a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
8248a27f7c9SJoe Perches 			longest = zerolength[i];
8258a27f7c9SJoe Perches 			colonpos = i;
8268a27f7c9SJoe Perches 		}
8278a27f7c9SJoe Perches 	}
82829cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
82929cf519eSJoe Perches 		colonpos = -1;
8308a27f7c9SJoe Perches 
8318a27f7c9SJoe Perches 	/* emit address */
8328a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
8338a27f7c9SJoe Perches 		if (i == colonpos) {
8348a27f7c9SJoe Perches 			if (needcolon || i == 0)
8358a27f7c9SJoe Perches 				*p++ = ':';
8368a27f7c9SJoe Perches 			*p++ = ':';
8378a27f7c9SJoe Perches 			needcolon = false;
8388a27f7c9SJoe Perches 			i += longest - 1;
8398a27f7c9SJoe Perches 			continue;
8408a27f7c9SJoe Perches 		}
8418a27f7c9SJoe Perches 		if (needcolon) {
8428a27f7c9SJoe Perches 			*p++ = ':';
8438a27f7c9SJoe Perches 			needcolon = false;
8448a27f7c9SJoe Perches 		}
8458a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
846eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
8478a27f7c9SJoe Perches 		hi = word >> 8;
8488a27f7c9SJoe Perches 		lo = word & 0xff;
8498a27f7c9SJoe Perches 		if (hi) {
8508a27f7c9SJoe Perches 			if (hi > 0x0f)
85155036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
8528a27f7c9SJoe Perches 			else
8538a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
85455036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
8558a27f7c9SJoe Perches 		}
856b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
85755036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
8588a27f7c9SJoe Perches 		else
8598a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
8608a27f7c9SJoe Perches 		needcolon = true;
8618a27f7c9SJoe Perches 	}
8628a27f7c9SJoe Perches 
8638a27f7c9SJoe Perches 	if (useIPv4) {
8648a27f7c9SJoe Perches 		if (needcolon)
8658a27f7c9SJoe Perches 			*p++ = ':';
8660159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
8678a27f7c9SJoe Perches 	}
8688a27f7c9SJoe Perches 	*p = '\0';
8697b9186f5SAndré Goddard Rosa 
8708a27f7c9SJoe Perches 	return p;
8718a27f7c9SJoe Perches }
8728a27f7c9SJoe Perches 
873cf3b429bSJoe Perches static noinline_for_stack
874cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
8758a27f7c9SJoe Perches {
8768a27f7c9SJoe Perches 	int i;
8777b9186f5SAndré Goddard Rosa 
878689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
87955036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
88055036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
8818a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
882689afa7dSHarvey Harrison 			*p++ = ':';
883689afa7dSHarvey Harrison 	}
884689afa7dSHarvey Harrison 	*p = '\0';
8857b9186f5SAndré Goddard Rosa 
8868a27f7c9SJoe Perches 	return p;
8878a27f7c9SJoe Perches }
8888a27f7c9SJoe Perches 
889cf3b429bSJoe Perches static noinline_for_stack
890cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
8918a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
8928a27f7c9SJoe Perches {
8938a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
8948a27f7c9SJoe Perches 
8958a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
896eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
8978a27f7c9SJoe Perches 	else
898eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
899689afa7dSHarvey Harrison 
900fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
901689afa7dSHarvey Harrison }
902689afa7dSHarvey Harrison 
903cf3b429bSJoe Perches static noinline_for_stack
904cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
9058a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
9064aa99606SHarvey Harrison {
9078a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
9084aa99606SHarvey Harrison 
9090159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
9104aa99606SHarvey Harrison 
911fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
9124aa99606SHarvey Harrison }
9134aa99606SHarvey Harrison 
914cf3b429bSJoe Perches static noinline_for_stack
915cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
9169ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
9179ac6e44eSJoe Perches {
9189ac6e44eSJoe Perches 	char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
9199ac6e44eSJoe Perches 	char *p = uuid;
9209ac6e44eSJoe Perches 	int i;
9219ac6e44eSJoe Perches 	static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
9229ac6e44eSJoe Perches 	static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
9239ac6e44eSJoe Perches 	const u8 *index = be;
9249ac6e44eSJoe Perches 	bool uc = false;
9259ac6e44eSJoe Perches 
9269ac6e44eSJoe Perches 	switch (*(++fmt)) {
9279ac6e44eSJoe Perches 	case 'L':
9289ac6e44eSJoe Perches 		uc = true;		/* fall-through */
9299ac6e44eSJoe Perches 	case 'l':
9309ac6e44eSJoe Perches 		index = le;
9319ac6e44eSJoe Perches 		break;
9329ac6e44eSJoe Perches 	case 'B':
9339ac6e44eSJoe Perches 		uc = true;
9349ac6e44eSJoe Perches 		break;
9359ac6e44eSJoe Perches 	}
9369ac6e44eSJoe Perches 
9379ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
93855036ba7SAndy Shevchenko 		p = hex_byte_pack(p, addr[index[i]]);
9399ac6e44eSJoe Perches 		switch (i) {
9409ac6e44eSJoe Perches 		case 3:
9419ac6e44eSJoe Perches 		case 5:
9429ac6e44eSJoe Perches 		case 7:
9439ac6e44eSJoe Perches 		case 9:
9449ac6e44eSJoe Perches 			*p++ = '-';
9459ac6e44eSJoe Perches 			break;
9469ac6e44eSJoe Perches 		}
9479ac6e44eSJoe Perches 	}
9489ac6e44eSJoe Perches 
9499ac6e44eSJoe Perches 	*p = 0;
9509ac6e44eSJoe Perches 
9519ac6e44eSJoe Perches 	if (uc) {
9529ac6e44eSJoe Perches 		p = uuid;
9539ac6e44eSJoe Perches 		do {
9549ac6e44eSJoe Perches 			*p = toupper(*p);
9559ac6e44eSJoe Perches 		} while (*(++p));
9569ac6e44eSJoe Perches 	}
9579ac6e44eSJoe Perches 
9589ac6e44eSJoe Perches 	return string(buf, end, uuid, spec);
9599ac6e44eSJoe Perches }
9609ac6e44eSJoe Perches 
961c8f44affSMichał Mirosław static
962c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr,
963c8f44affSMichał Mirosław 		      struct printf_spec spec)
964c8f44affSMichał Mirosław {
965c8f44affSMichał Mirosław 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
966c8f44affSMichał Mirosław 	if (spec.field_width == -1)
967c8f44affSMichał Mirosław 		spec.field_width = 2 + 2 * sizeof(netdev_features_t);
968c8f44affSMichał Mirosław 	spec.base = 16;
969c8f44affSMichał Mirosław 
970c8f44affSMichał Mirosław 	return number(buf, end, *(const netdev_features_t *)addr, spec);
971c8f44affSMichał Mirosław }
972c8f44affSMichał Mirosław 
973411f05f1SIngo Molnar int kptr_restrict __read_mostly;
974455cd5abSDan Rosenberg 
9754d8a743cSLinus Torvalds /*
9764d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
9774d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
9784d8a743cSLinus Torvalds  * specifiers.
9794d8a743cSLinus Torvalds  *
980332d2e78SLinus Torvalds  * Right now we handle:
9810fe1ef24SLinus Torvalds  *
9820c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
9830c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
9840efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
9850efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
9860f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
987c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
988c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
989dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
990dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
9918a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
992bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
993c8e00060SJoe Perches  *       with a dash-separated hex notation
99476597ff9SAndrei Emeltchenko  * - '[mM]R For a 6-byte MAC address, Reverse order (Bluetooth)
9958a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
9968a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
9978a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
9988a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
9998a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
10008a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
10010159f24eSJoe Perches  * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
10028a27f7c9SJoe Perches  * - 'I6c' for IPv6 addresses printed as specified by
100329cf519eSJoe Perches  *       http://tools.ietf.org/html/rfc5952
10049ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
10059ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
10069ac6e44eSJoe Perches  *       Options for %pU are:
10079ac6e44eSJoe Perches  *         b big endian lower case hex (default)
10089ac6e44eSJoe Perches  *         B big endian UPPER case hex
10099ac6e44eSJoe Perches  *         l little endian lower case hex
10109ac6e44eSJoe Perches  *         L little endian UPPER case hex
10119ac6e44eSJoe Perches  *           big endian output byte order is:
10129ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
10139ac6e44eSJoe Perches  *           little endian output byte order is:
10149ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
10157db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
10167db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
10177db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
10187db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
10197db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
1020455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
1021c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
102231550a16SAndy Shevchenko  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
102331550a16SAndy Shevchenko  *            a certain separator (' ' by default):
102431550a16SAndy Shevchenko  *              C colon
102531550a16SAndy Shevchenko  *              D dash
102631550a16SAndy Shevchenko  *              N no separator
102731550a16SAndy Shevchenko  *            The maximum supported length is 64 bytes of the input. Consider
102831550a16SAndy Shevchenko  *            to use print_hex_dump() for the larger input.
10299ac6e44eSJoe Perches  *
1030332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1031332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
1032332d2e78SLinus Torvalds  * pointer to the real address.
10334d8a743cSLinus Torvalds  */
1034cf3b429bSJoe Perches static noinline_for_stack
1035cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1036fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
103778a8bf69SLinus Torvalds {
1038725fe002SGrant Likely 	int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1039725fe002SGrant Likely 
10409f36e2c4SKees Cook 	if (!ptr && *fmt != 'K') {
10415e057981SJoe Perches 		/*
10425e057981SJoe Perches 		 * Print (null) with the same width as a pointer so it makes
10435e057981SJoe Perches 		 * tabular output look nice.
10445e057981SJoe Perches 		 */
10455e057981SJoe Perches 		if (spec.field_width == -1)
1046725fe002SGrant Likely 			spec.field_width = default_width;
1047fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
10485e057981SJoe Perches 	}
1049d97106abSLinus Torvalds 
10500fe1ef24SLinus Torvalds 	switch (*fmt) {
10510fe1ef24SLinus Torvalds 	case 'F':
10520c8b946eSFrederic Weisbecker 	case 'f':
10530fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
10540fe1ef24SLinus Torvalds 		/* Fallthrough */
10550fe1ef24SLinus Torvalds 	case 'S':
10569ac6e44eSJoe Perches 	case 's':
10570f77a8d3SNamhyung Kim 	case 'B':
10580c8b946eSFrederic Weisbecker 		return symbol_string(buf, end, ptr, spec, *fmt);
1059332d2e78SLinus Torvalds 	case 'R':
1060c7dabef8SBjorn Helgaas 	case 'r':
1061fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
106231550a16SAndy Shevchenko 	case 'h':
106331550a16SAndy Shevchenko 		return hex_string(buf, end, ptr, spec, fmt);
10648a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
10658a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
106676597ff9SAndrei Emeltchenko 					/* [mM]F (FDDI) */
106776597ff9SAndrei Emeltchenko 					/* [mM]R (Reverse order; Bluetooth) */
10688a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
10698a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
10708a27f7c9SJoe Perches 					 * 4:	1.2.3.4
10718a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
10728a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
10738a27f7c9SJoe Perches 					 */
10748a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
10758a27f7c9SJoe Perches 					 * 4:	001.002.003.004
10768a27f7c9SJoe Perches 					 * 6:   000102...0f
10778a27f7c9SJoe Perches 					 */
10788a27f7c9SJoe Perches 		switch (fmt[1]) {
10798a27f7c9SJoe Perches 		case '6':
10808a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
10818a27f7c9SJoe Perches 		case '4':
10828a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
10838a27f7c9SJoe Perches 		}
10844aa99606SHarvey Harrison 		break;
10859ac6e44eSJoe Perches 	case 'U':
10869ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
10877db6f5fbSJoe Perches 	case 'V':
10885756b76eSJan Beulich 		{
10895756b76eSJan Beulich 			va_list va;
10905756b76eSJan Beulich 
10915756b76eSJan Beulich 			va_copy(va, *((struct va_format *)ptr)->va);
10925756b76eSJan Beulich 			buf += vsnprintf(buf, end > buf ? end - buf : 0,
10935756b76eSJan Beulich 					 ((struct va_format *)ptr)->fmt, va);
10945756b76eSJan Beulich 			va_end(va);
10955756b76eSJan Beulich 			return buf;
10965756b76eSJan Beulich 		}
1097455cd5abSDan Rosenberg 	case 'K':
1098455cd5abSDan Rosenberg 		/*
1099455cd5abSDan Rosenberg 		 * %pK cannot be used in IRQ context because its test
1100455cd5abSDan Rosenberg 		 * for CAP_SYSLOG would be meaningless.
1101455cd5abSDan Rosenberg 		 */
11023715c530SDan Rosenberg 		if (kptr_restrict && (in_irq() || in_serving_softirq() ||
11033715c530SDan Rosenberg 				      in_nmi())) {
1104455cd5abSDan Rosenberg 			if (spec.field_width == -1)
1105725fe002SGrant Likely 				spec.field_width = default_width;
1106455cd5abSDan Rosenberg 			return string(buf, end, "pK-error", spec);
1107455cd5abSDan Rosenberg 		}
110826297607SJoe Perches 		if (!((kptr_restrict == 0) ||
110926297607SJoe Perches 		      (kptr_restrict == 1 &&
111026297607SJoe Perches 		       has_capability_noaudit(current, CAP_SYSLOG))))
111126297607SJoe Perches 			ptr = NULL;
111226297607SJoe Perches 		break;
1113c8f44affSMichał Mirosław 	case 'N':
1114c8f44affSMichał Mirosław 		switch (fmt[1]) {
1115c8f44affSMichał Mirosław 		case 'F':
1116c8f44affSMichał Mirosław 			return netdev_feature_string(buf, end, ptr, spec);
1117c8f44affSMichał Mirosław 		}
1118c8f44affSMichał Mirosław 		break;
11190fe1ef24SLinus Torvalds 	}
1120fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
1121fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
1122725fe002SGrant Likely 		spec.field_width = default_width;
1123fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
112478a8bf69SLinus Torvalds 	}
1125fef20d9cSFrederic Weisbecker 	spec.base = 16;
1126fef20d9cSFrederic Weisbecker 
1127fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
1128fef20d9cSFrederic Weisbecker }
1129fef20d9cSFrederic Weisbecker 
1130fef20d9cSFrederic Weisbecker /*
1131fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
1132fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
1133fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
1134fef20d9cSFrederic Weisbecker  * to go on the next call).
1135fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
1136fef20d9cSFrederic Weisbecker  *
1137fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
1138fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
1139fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
1140fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
1141fef20d9cSFrederic Weisbecker  *
1142fef20d9cSFrederic Weisbecker  * @fmt: the format string
1143fef20d9cSFrederic Weisbecker  * @type of the token returned
1144fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
1145fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
1146fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
1147fef20d9cSFrederic Weisbecker  * @precision: precision of a number
1148fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
1149fef20d9cSFrederic Weisbecker  */
1150cf3b429bSJoe Perches static noinline_for_stack
1151cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
1152fef20d9cSFrederic Weisbecker {
1153fef20d9cSFrederic Weisbecker 	const char *start = fmt;
1154fef20d9cSFrederic Weisbecker 
1155fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
1156ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
1157fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
1158fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
1159fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
1160fef20d9cSFrederic Weisbecker 		}
1161fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1162fef20d9cSFrederic Weisbecker 		goto precision;
1163fef20d9cSFrederic Weisbecker 	}
1164fef20d9cSFrederic Weisbecker 
1165fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
1166fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
1167fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
1168fef20d9cSFrederic Weisbecker 			spec->precision = 0;
1169fef20d9cSFrederic Weisbecker 
1170fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1171fef20d9cSFrederic Weisbecker 		goto qualifier;
1172fef20d9cSFrederic Weisbecker 	}
1173fef20d9cSFrederic Weisbecker 
1174fef20d9cSFrederic Weisbecker 	/* By default */
1175fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
1176fef20d9cSFrederic Weisbecker 
1177fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
1178fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
1179fef20d9cSFrederic Weisbecker 			break;
1180fef20d9cSFrederic Weisbecker 	}
1181fef20d9cSFrederic Weisbecker 
1182fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
1183fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
1184fef20d9cSFrederic Weisbecker 		return fmt - start;
1185fef20d9cSFrederic Weisbecker 
1186fef20d9cSFrederic Weisbecker 	/* Process flags */
1187fef20d9cSFrederic Weisbecker 	spec->flags = 0;
1188fef20d9cSFrederic Weisbecker 
1189fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
1190fef20d9cSFrederic Weisbecker 		bool found = true;
1191fef20d9cSFrederic Weisbecker 
1192fef20d9cSFrederic Weisbecker 		++fmt;
1193fef20d9cSFrederic Weisbecker 
1194fef20d9cSFrederic Weisbecker 		switch (*fmt) {
1195fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
1196fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
1197fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
1198fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
1199fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
1200fef20d9cSFrederic Weisbecker 		default:  found = false;
1201fef20d9cSFrederic Weisbecker 		}
1202fef20d9cSFrederic Weisbecker 
1203fef20d9cSFrederic Weisbecker 		if (!found)
1204fef20d9cSFrederic Weisbecker 			break;
1205fef20d9cSFrederic Weisbecker 	}
1206fef20d9cSFrederic Weisbecker 
1207fef20d9cSFrederic Weisbecker 	/* get field width */
1208fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
1209fef20d9cSFrederic Weisbecker 
1210fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
1211fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
1212fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
1213fef20d9cSFrederic Weisbecker 		/* it's the next argument */
1214ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
1215fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1216fef20d9cSFrederic Weisbecker 	}
1217fef20d9cSFrederic Weisbecker 
1218fef20d9cSFrederic Weisbecker precision:
1219fef20d9cSFrederic Weisbecker 	/* get the precision */
1220fef20d9cSFrederic Weisbecker 	spec->precision = -1;
1221fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
1222fef20d9cSFrederic Weisbecker 		++fmt;
1223fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
1224fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
1225fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
1226fef20d9cSFrederic Weisbecker 				spec->precision = 0;
1227fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
1228fef20d9cSFrederic Weisbecker 			/* it's the next argument */
1229adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
1230fef20d9cSFrederic Weisbecker 			return ++fmt - start;
1231fef20d9cSFrederic Weisbecker 		}
1232fef20d9cSFrederic Weisbecker 	}
1233fef20d9cSFrederic Weisbecker 
1234fef20d9cSFrederic Weisbecker qualifier:
1235fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
1236fef20d9cSFrederic Weisbecker 	spec->qualifier = -1;
123775fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
123875fb8f26SAndy Shevchenko 	    _tolower(*fmt) == 'z' || *fmt == 't') {
1239a4e94ef0SZhaolei 		spec->qualifier = *fmt++;
1240a4e94ef0SZhaolei 		if (unlikely(spec->qualifier == *fmt)) {
1241a4e94ef0SZhaolei 			if (spec->qualifier == 'l') {
1242fef20d9cSFrederic Weisbecker 				spec->qualifier = 'L';
1243fef20d9cSFrederic Weisbecker 				++fmt;
1244a4e94ef0SZhaolei 			} else if (spec->qualifier == 'h') {
1245a4e94ef0SZhaolei 				spec->qualifier = 'H';
1246a4e94ef0SZhaolei 				++fmt;
1247a4e94ef0SZhaolei 			}
1248fef20d9cSFrederic Weisbecker 		}
1249fef20d9cSFrederic Weisbecker 	}
1250fef20d9cSFrederic Weisbecker 
1251fef20d9cSFrederic Weisbecker 	/* default base */
1252fef20d9cSFrederic Weisbecker 	spec->base = 10;
1253fef20d9cSFrederic Weisbecker 	switch (*fmt) {
1254fef20d9cSFrederic Weisbecker 	case 'c':
1255fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
1256fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1257fef20d9cSFrederic Weisbecker 
1258fef20d9cSFrederic Weisbecker 	case 's':
1259fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
1260fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1261fef20d9cSFrederic Weisbecker 
1262fef20d9cSFrederic Weisbecker 	case 'p':
1263fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
1264fef20d9cSFrederic Weisbecker 		return fmt - start;
1265fef20d9cSFrederic Weisbecker 		/* skip alnum */
1266fef20d9cSFrederic Weisbecker 
1267fef20d9cSFrederic Weisbecker 	case 'n':
1268fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NRCHARS;
1269fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1270fef20d9cSFrederic Weisbecker 
1271fef20d9cSFrederic Weisbecker 	case '%':
1272fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1273fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1274fef20d9cSFrederic Weisbecker 
1275fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1276fef20d9cSFrederic Weisbecker 	case 'o':
1277fef20d9cSFrederic Weisbecker 		spec->base = 8;
1278fef20d9cSFrederic Weisbecker 		break;
1279fef20d9cSFrederic Weisbecker 
1280fef20d9cSFrederic Weisbecker 	case 'x':
1281fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1282fef20d9cSFrederic Weisbecker 
1283fef20d9cSFrederic Weisbecker 	case 'X':
1284fef20d9cSFrederic Weisbecker 		spec->base = 16;
1285fef20d9cSFrederic Weisbecker 		break;
1286fef20d9cSFrederic Weisbecker 
1287fef20d9cSFrederic Weisbecker 	case 'd':
1288fef20d9cSFrederic Weisbecker 	case 'i':
128939e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1290fef20d9cSFrederic Weisbecker 	case 'u':
1291fef20d9cSFrederic Weisbecker 		break;
1292fef20d9cSFrederic Weisbecker 
1293fef20d9cSFrederic Weisbecker 	default:
1294fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1295fef20d9cSFrederic Weisbecker 		return fmt - start;
1296fef20d9cSFrederic Weisbecker 	}
1297fef20d9cSFrederic Weisbecker 
1298fef20d9cSFrederic Weisbecker 	if (spec->qualifier == 'L')
1299fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1300fef20d9cSFrederic Weisbecker 	else if (spec->qualifier == 'l') {
130139e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1302fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_LONG;
1303fef20d9cSFrederic Weisbecker 		else
1304fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_ULONG;
130575fb8f26SAndy Shevchenko 	} else if (_tolower(spec->qualifier) == 'z') {
1306fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1307fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 't') {
1308fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1309a4e94ef0SZhaolei 	} else if (spec->qualifier == 'H') {
1310a4e94ef0SZhaolei 		if (spec->flags & SIGN)
1311a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_BYTE;
1312a4e94ef0SZhaolei 		else
1313a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_UBYTE;
1314fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'h') {
131539e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1316fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_SHORT;
1317fef20d9cSFrederic Weisbecker 		else
1318fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_USHORT;
1319fef20d9cSFrederic Weisbecker 	} else {
132039e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1321fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_INT;
1322fef20d9cSFrederic Weisbecker 		else
1323fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_UINT;
1324fef20d9cSFrederic Weisbecker 	}
1325fef20d9cSFrederic Weisbecker 
1326fef20d9cSFrederic Weisbecker 	return ++fmt - start;
132778a8bf69SLinus Torvalds }
132878a8bf69SLinus Torvalds 
13291da177e4SLinus Torvalds /**
13301da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
13311da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13321da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
13331da177e4SLinus Torvalds  * @fmt: The format string to use
13341da177e4SLinus Torvalds  * @args: Arguments for the format string
13351da177e4SLinus Torvalds  *
133620036fdcSAndi Kleen  * This function follows C99 vsnprintf, but has some extensions:
133791adcd2cSSteven Rostedt  * %pS output the name of a text symbol with offset
133891adcd2cSSteven Rostedt  * %ps output the name of a text symbol without offset
13390c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
13400c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
13410f77a8d3SNamhyung Kim  * %pB output the name of a backtrace symbol with its offset
13428a79503aSUwe Kleine-König  * %pR output the address range in a struct resource with decoded flags
13438a79503aSUwe Kleine-König  * %pr output the address range in a struct resource with raw flags
13448a79503aSUwe Kleine-König  * %pM output a 6-byte MAC address with colons
13458a79503aSUwe Kleine-König  * %pm output a 6-byte MAC address without colons
13468a79503aSUwe Kleine-König  * %pI4 print an IPv4 address without leading zeros
13478a79503aSUwe Kleine-König  * %pi4 print an IPv4 address with leading zeros
13488a79503aSUwe Kleine-König  * %pI6 print an IPv6 address with colons
13498a79503aSUwe Kleine-König  * %pi6 print an IPv6 address without colons
1350f996f208SJan Engelhardt  * %pI6c print an IPv6 address as specified by RFC 5952
13518a79503aSUwe Kleine-König  * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
13528a79503aSUwe Kleine-König  *   case.
135331550a16SAndy Shevchenko  * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
135431550a16SAndy Shevchenko  *           bytes of the input)
13550efb4d20SSteven Rostedt  * %n is ignored
135620036fdcSAndi Kleen  *
135780f548e0SAndrew Morton  * ** Please update Documentation/printk-formats.txt when making changes **
135880f548e0SAndrew Morton  *
13591da177e4SLinus Torvalds  * The return value is the number of characters which would
13601da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
13611da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
13621da177e4SLinus Torvalds  * number of characters written into @buf as return value
136372fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
13641da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
13651da177e4SLinus Torvalds  * string is truncated.
13661da177e4SLinus Torvalds  *
1367ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
13681da177e4SLinus Torvalds  */
13691da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
13701da177e4SLinus Torvalds {
13711da177e4SLinus Torvalds 	unsigned long long num;
1372d4be151bSAndré Goddard Rosa 	char *str, *end;
1373fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
13741da177e4SLinus Torvalds 
1375f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1376f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
13772f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
13781da177e4SLinus Torvalds 		return 0;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	str = buf;
1381f796937aSJeremy Fitzhardinge 	end = buf + size;
13821da177e4SLinus Torvalds 
1383f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1384f796937aSJeremy Fitzhardinge 	if (end < buf) {
13851da177e4SLinus Torvalds 		end = ((void *)-1);
1386f796937aSJeremy Fitzhardinge 		size = end - buf;
13871da177e4SLinus Torvalds 	}
13881da177e4SLinus Torvalds 
1389fef20d9cSFrederic Weisbecker 	while (*fmt) {
1390fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1391d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1392fef20d9cSFrederic Weisbecker 
1393fef20d9cSFrederic Weisbecker 		fmt += read;
1394fef20d9cSFrederic Weisbecker 
1395fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1396fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1397fef20d9cSFrederic Weisbecker 			int copy = read;
1398fef20d9cSFrederic Weisbecker 			if (str < end) {
1399fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1400fef20d9cSFrederic Weisbecker 					copy = end - str;
1401fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1402fef20d9cSFrederic Weisbecker 			}
1403fef20d9cSFrederic Weisbecker 			str += read;
1404fef20d9cSFrederic Weisbecker 			break;
14051da177e4SLinus Torvalds 		}
14061da177e4SLinus Torvalds 
1407ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1408fef20d9cSFrederic Weisbecker 			spec.field_width = va_arg(args, int);
1409fef20d9cSFrederic Weisbecker 			break;
14101da177e4SLinus Torvalds 
1411fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1412fef20d9cSFrederic Weisbecker 			spec.precision = va_arg(args, int);
1413fef20d9cSFrederic Weisbecker 			break;
14141da177e4SLinus Torvalds 
1415d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1416d4be151bSAndré Goddard Rosa 			char c;
1417d4be151bSAndré Goddard Rosa 
1418fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1419fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1420f796937aSJeremy Fitzhardinge 					if (str < end)
14211da177e4SLinus Torvalds 						*str = ' ';
14221da177e4SLinus Torvalds 					++str;
1423fef20d9cSFrederic Weisbecker 
14241da177e4SLinus Torvalds 				}
14251da177e4SLinus Torvalds 			}
14261da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1427f796937aSJeremy Fitzhardinge 			if (str < end)
14281da177e4SLinus Torvalds 				*str = c;
14291da177e4SLinus Torvalds 			++str;
1430fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1431f796937aSJeremy Fitzhardinge 				if (str < end)
14321da177e4SLinus Torvalds 					*str = ' ';
14331da177e4SLinus Torvalds 				++str;
14341da177e4SLinus Torvalds 			}
1435fef20d9cSFrederic Weisbecker 			break;
1436d4be151bSAndré Goddard Rosa 		}
14371da177e4SLinus Torvalds 
1438fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1439fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1440fef20d9cSFrederic Weisbecker 			break;
14411da177e4SLinus Torvalds 
1442fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1443fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, va_arg(args, void *),
1444fef20d9cSFrederic Weisbecker 				      spec);
1445fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
14464d8a743cSLinus Torvalds 				fmt++;
1447fef20d9cSFrederic Weisbecker 			break;
14481da177e4SLinus Torvalds 
1449fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1450f796937aSJeremy Fitzhardinge 			if (str < end)
14511da177e4SLinus Torvalds 				*str = '%';
14521da177e4SLinus Torvalds 			++str;
14531da177e4SLinus Torvalds 			break;
14541da177e4SLinus Torvalds 
1455fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1456f796937aSJeremy Fitzhardinge 			if (str < end)
14571da177e4SLinus Torvalds 				*str = '%';
14581da177e4SLinus Torvalds 			++str;
1459fef20d9cSFrederic Weisbecker 			break;
1460fef20d9cSFrederic Weisbecker 
1461fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
1462ef0658f3SJoe Perches 			u8 qualifier = spec.qualifier;
1463fef20d9cSFrederic Weisbecker 
1464fef20d9cSFrederic Weisbecker 			if (qualifier == 'l') {
1465fef20d9cSFrederic Weisbecker 				long *ip = va_arg(args, long *);
1466fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
146775fb8f26SAndy Shevchenko 			} else if (_tolower(qualifier) == 'z') {
1468fef20d9cSFrederic Weisbecker 				size_t *ip = va_arg(args, size_t *);
1469fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
14701da177e4SLinus Torvalds 			} else {
1471fef20d9cSFrederic Weisbecker 				int *ip = va_arg(args, int *);
1472fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
1473fef20d9cSFrederic Weisbecker 			}
1474fef20d9cSFrederic Weisbecker 			break;
1475fef20d9cSFrederic Weisbecker 		}
1476fef20d9cSFrederic Weisbecker 
1477fef20d9cSFrederic Weisbecker 		default:
1478fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1479fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1480fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1481fef20d9cSFrederic Weisbecker 				break;
1482fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1483fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
1484fef20d9cSFrederic Weisbecker 				break;
1485fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1486fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
1487fef20d9cSFrederic Weisbecker 				break;
1488fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1489fef20d9cSFrederic Weisbecker 				num = va_arg(args, size_t);
1490fef20d9cSFrederic Weisbecker 				break;
1491fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1492fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
1493fef20d9cSFrederic Weisbecker 				break;
1494a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1495a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
1496a4e94ef0SZhaolei 				break;
1497a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1498a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
1499a4e94ef0SZhaolei 				break;
1500fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1501fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
1502fef20d9cSFrederic Weisbecker 				break;
1503fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1504fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
1505fef20d9cSFrederic Weisbecker 				break;
150639e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
150739e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
1508fef20d9cSFrederic Weisbecker 				break;
1509fef20d9cSFrederic Weisbecker 			default:
1510fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
15111da177e4SLinus Torvalds 			}
1512fef20d9cSFrederic Weisbecker 
1513fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
15141da177e4SLinus Torvalds 		}
1515fef20d9cSFrederic Weisbecker 	}
1516fef20d9cSFrederic Weisbecker 
1517f796937aSJeremy Fitzhardinge 	if (size > 0) {
1518f796937aSJeremy Fitzhardinge 		if (str < end)
15191da177e4SLinus Torvalds 			*str = '\0';
1520f796937aSJeremy Fitzhardinge 		else
15210a6047eeSLinus Torvalds 			end[-1] = '\0';
1522f796937aSJeremy Fitzhardinge 	}
1523fef20d9cSFrederic Weisbecker 
1524f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
15251da177e4SLinus Torvalds 	return str-buf;
1526fef20d9cSFrederic Weisbecker 
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds /**
15311da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
15321da177e4SLinus Torvalds  * @buf: The buffer to place the result into
15331da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
15341da177e4SLinus Torvalds  * @fmt: The format string to use
15351da177e4SLinus Torvalds  * @args: Arguments for the format string
15361da177e4SLinus Torvalds  *
15371da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
1538b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
15391da177e4SLinus Torvalds  * returns 0.
15401da177e4SLinus Torvalds  *
1541ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
154220036fdcSAndi Kleen  *
154320036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
15441da177e4SLinus Torvalds  */
15451da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
15461da177e4SLinus Torvalds {
15471da177e4SLinus Torvalds 	int i;
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
15507b9186f5SAndré Goddard Rosa 
1551b921c69fSAnton Arapov 	if (likely(i < size))
1552b921c69fSAnton Arapov 		return i;
1553b921c69fSAnton Arapov 	if (size != 0)
1554b921c69fSAnton Arapov 		return size - 1;
1555b921c69fSAnton Arapov 	return 0;
15561da177e4SLinus Torvalds }
15571da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds /**
15601da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
15611da177e4SLinus Torvalds  * @buf: The buffer to place the result into
15621da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
15631da177e4SLinus Torvalds  * @fmt: The format string to use
15641da177e4SLinus Torvalds  * @...: Arguments for the format string
15651da177e4SLinus Torvalds  *
15661da177e4SLinus Torvalds  * The return value is the number of characters which would be
15671da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
15681da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
15691da177e4SLinus Torvalds  * @size, the resulting string is truncated.
157020036fdcSAndi Kleen  *
157120036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
15721da177e4SLinus Torvalds  */
15731da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
15741da177e4SLinus Torvalds {
15751da177e4SLinus Torvalds 	va_list args;
15761da177e4SLinus Torvalds 	int i;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	va_start(args, fmt);
15791da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
15801da177e4SLinus Torvalds 	va_end(args);
15817b9186f5SAndré Goddard Rosa 
15821da177e4SLinus Torvalds 	return i;
15831da177e4SLinus Torvalds }
15841da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds /**
15871da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
15881da177e4SLinus Torvalds  * @buf: The buffer to place the result into
15891da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
15901da177e4SLinus Torvalds  * @fmt: The format string to use
15911da177e4SLinus Torvalds  * @...: Arguments for the format string
15921da177e4SLinus Torvalds  *
15931da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
1594b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
15951da177e4SLinus Torvalds  */
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
15981da177e4SLinus Torvalds {
15991da177e4SLinus Torvalds 	va_list args;
16001da177e4SLinus Torvalds 	int i;
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 	va_start(args, fmt);
1603b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
16041da177e4SLinus Torvalds 	va_end(args);
16057b9186f5SAndré Goddard Rosa 
1606b903c0b8SChangli Gao 	return i;
16071da177e4SLinus Torvalds }
16081da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds /**
16111da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
16121da177e4SLinus Torvalds  * @buf: The buffer to place the result into
16131da177e4SLinus Torvalds  * @fmt: The format string to use
16141da177e4SLinus Torvalds  * @args: Arguments for the format string
16151da177e4SLinus Torvalds  *
16161da177e4SLinus Torvalds  * The function returns the number of characters written
161772fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
16181da177e4SLinus Torvalds  * buffer overflows.
16191da177e4SLinus Torvalds  *
1620ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
162120036fdcSAndi Kleen  *
162220036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
16231da177e4SLinus Torvalds  */
16241da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
16251da177e4SLinus Torvalds {
16261da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
16271da177e4SLinus Torvalds }
16281da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds /**
16311da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
16321da177e4SLinus Torvalds  * @buf: The buffer to place the result into
16331da177e4SLinus Torvalds  * @fmt: The format string to use
16341da177e4SLinus Torvalds  * @...: Arguments for the format string
16351da177e4SLinus Torvalds  *
16361da177e4SLinus Torvalds  * The function returns the number of characters written
163772fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
16381da177e4SLinus Torvalds  * buffer overflows.
163920036fdcSAndi Kleen  *
164020036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
16411da177e4SLinus Torvalds  */
16421da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
16431da177e4SLinus Torvalds {
16441da177e4SLinus Torvalds 	va_list args;
16451da177e4SLinus Torvalds 	int i;
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 	va_start(args, fmt);
16481da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
16491da177e4SLinus Torvalds 	va_end(args);
16507b9186f5SAndré Goddard Rosa 
16511da177e4SLinus Torvalds 	return i;
16521da177e4SLinus Torvalds }
16531da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
16541da177e4SLinus Torvalds 
16554370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
16564370aa4aSLai Jiangshan /*
16574370aa4aSLai Jiangshan  * bprintf service:
16584370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
16594370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
16604370aa4aSLai Jiangshan  */
16614370aa4aSLai Jiangshan 
16624370aa4aSLai Jiangshan /**
16634370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
16644370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
16654370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
16664370aa4aSLai Jiangshan  * @fmt: The format string to use
16674370aa4aSLai Jiangshan  * @args: Arguments for the format string
16684370aa4aSLai Jiangshan  *
16694370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
16704370aa4aSLai Jiangshan  * is skiped.
16714370aa4aSLai Jiangshan  *
16724370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
16734370aa4aSLai Jiangshan  * the given input.
16744370aa4aSLai Jiangshan  *
16754370aa4aSLai Jiangshan  * NOTE:
16764370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
16774370aa4aSLai Jiangshan  * valid for bstr_printf().
16784370aa4aSLai Jiangshan  */
16794370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
16804370aa4aSLai Jiangshan {
1681fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
16824370aa4aSLai Jiangshan 	char *str, *end;
16834370aa4aSLai Jiangshan 
16844370aa4aSLai Jiangshan 	str = (char *)bin_buf;
16854370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
16864370aa4aSLai Jiangshan 
16874370aa4aSLai Jiangshan #define save_arg(type)							\
16884370aa4aSLai Jiangshan do {									\
16894370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
16904370aa4aSLai Jiangshan 		unsigned long long value;				\
16914370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
16924370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
16934370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
16944370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
16954370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
16964370aa4aSLai Jiangshan 		}							\
16974370aa4aSLai Jiangshan 	} else {							\
16984370aa4aSLai Jiangshan 		unsigned long value;					\
16994370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
17004370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
17014370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
17024370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
17034370aa4aSLai Jiangshan 	}								\
17044370aa4aSLai Jiangshan 	str += sizeof(type);						\
17054370aa4aSLai Jiangshan } while (0)
17064370aa4aSLai Jiangshan 
1707fef20d9cSFrederic Weisbecker 	while (*fmt) {
1708d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
17094370aa4aSLai Jiangshan 
1710fef20d9cSFrederic Weisbecker 		fmt += read;
1711fef20d9cSFrederic Weisbecker 
1712fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1713fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
1714d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_INVALID:
1715d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
1716fef20d9cSFrederic Weisbecker 			break;
1717fef20d9cSFrederic Weisbecker 
1718ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1719fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
17204370aa4aSLai Jiangshan 			save_arg(int);
1721fef20d9cSFrederic Weisbecker 			break;
17224370aa4aSLai Jiangshan 
1723fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
17244370aa4aSLai Jiangshan 			save_arg(char);
1725fef20d9cSFrederic Weisbecker 			break;
1726fef20d9cSFrederic Weisbecker 
1727fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
17284370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
17294370aa4aSLai Jiangshan 			size_t len;
17306c356634SAndré Goddard Rosa 
17314370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
17324370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
17330f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
17346c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
17356c356634SAndré Goddard Rosa 			if (str + len < end)
17366c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
17376c356634SAndré Goddard Rosa 			str += len;
1738fef20d9cSFrederic Weisbecker 			break;
17394370aa4aSLai Jiangshan 		}
1740fef20d9cSFrederic Weisbecker 
1741fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
17424370aa4aSLai Jiangshan 			save_arg(void *);
17434370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
1744fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
17454370aa4aSLai Jiangshan 				fmt++;
1746fef20d9cSFrederic Weisbecker 			break;
1747fef20d9cSFrederic Weisbecker 
1748fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
17494370aa4aSLai Jiangshan 			/* skip %n 's argument */
1750ef0658f3SJoe Perches 			u8 qualifier = spec.qualifier;
17514370aa4aSLai Jiangshan 			void *skip_arg;
17524370aa4aSLai Jiangshan 			if (qualifier == 'l')
17534370aa4aSLai Jiangshan 				skip_arg = va_arg(args, long *);
175475fb8f26SAndy Shevchenko 			else if (_tolower(qualifier) == 'z')
17554370aa4aSLai Jiangshan 				skip_arg = va_arg(args, size_t *);
17564370aa4aSLai Jiangshan 			else
17574370aa4aSLai Jiangshan 				skip_arg = va_arg(args, int *);
1758fef20d9cSFrederic Weisbecker 			break;
17594370aa4aSLai Jiangshan 		}
17604370aa4aSLai Jiangshan 
1761fef20d9cSFrederic Weisbecker 		default:
1762fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1763fef20d9cSFrederic Weisbecker 
1764fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1765fef20d9cSFrederic Weisbecker 				save_arg(long long);
1766fef20d9cSFrederic Weisbecker 				break;
1767fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1768fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1769fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
1770fef20d9cSFrederic Weisbecker 				break;
1771fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1772fef20d9cSFrederic Weisbecker 				save_arg(size_t);
1773fef20d9cSFrederic Weisbecker 				break;
1774fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1775fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
1776fef20d9cSFrederic Weisbecker 				break;
1777a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1778a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1779a4e94ef0SZhaolei 				save_arg(char);
1780a4e94ef0SZhaolei 				break;
1781fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1782fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1783fef20d9cSFrederic Weisbecker 				save_arg(short);
1784fef20d9cSFrederic Weisbecker 				break;
1785fef20d9cSFrederic Weisbecker 			default:
1786fef20d9cSFrederic Weisbecker 				save_arg(int);
1787fef20d9cSFrederic Weisbecker 			}
1788fef20d9cSFrederic Weisbecker 		}
1789fef20d9cSFrederic Weisbecker 	}
1790fef20d9cSFrederic Weisbecker 
17917b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1792fef20d9cSFrederic Weisbecker #undef save_arg
17934370aa4aSLai Jiangshan }
17944370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
17954370aa4aSLai Jiangshan 
17964370aa4aSLai Jiangshan /**
17974370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
17984370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
17994370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
18004370aa4aSLai Jiangshan  * @fmt: The format string to use
18014370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
18024370aa4aSLai Jiangshan  *
18034370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
18044370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
18054370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
18064370aa4aSLai Jiangshan  *
18074370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
18080efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
18094370aa4aSLai Jiangshan  *
18104370aa4aSLai Jiangshan  * The return value is the number of characters which would
18114370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
18124370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
18134370aa4aSLai Jiangshan  * number of characters written into @buf as return value
18144370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
18154370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
18164370aa4aSLai Jiangshan  * string is truncated.
18174370aa4aSLai Jiangshan  */
18184370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
18194370aa4aSLai Jiangshan {
1820fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
1821d4be151bSAndré Goddard Rosa 	char *str, *end;
1822d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
18234370aa4aSLai Jiangshan 
18242f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
18254370aa4aSLai Jiangshan 		return 0;
18264370aa4aSLai Jiangshan 
18274370aa4aSLai Jiangshan 	str = buf;
18284370aa4aSLai Jiangshan 	end = buf + size;
18294370aa4aSLai Jiangshan 
18304370aa4aSLai Jiangshan #define get_arg(type)							\
18314370aa4aSLai Jiangshan ({									\
18324370aa4aSLai Jiangshan 	typeof(type) value;						\
18334370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
18344370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
18354370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
18364370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
18374370aa4aSLai Jiangshan 	} else {							\
18384370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
18394370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
18404370aa4aSLai Jiangshan 	}								\
18414370aa4aSLai Jiangshan 	args += sizeof(type);						\
18424370aa4aSLai Jiangshan 	value;								\
18434370aa4aSLai Jiangshan })
18444370aa4aSLai Jiangshan 
18454370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
18464370aa4aSLai Jiangshan 	if (end < buf) {
18474370aa4aSLai Jiangshan 		end = ((void *)-1);
18484370aa4aSLai Jiangshan 		size = end - buf;
18494370aa4aSLai Jiangshan 	}
18504370aa4aSLai Jiangshan 
1851fef20d9cSFrederic Weisbecker 	while (*fmt) {
1852fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1853d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1854fef20d9cSFrederic Weisbecker 
1855fef20d9cSFrederic Weisbecker 		fmt += read;
1856fef20d9cSFrederic Weisbecker 
1857fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1858fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1859fef20d9cSFrederic Weisbecker 			int copy = read;
1860fef20d9cSFrederic Weisbecker 			if (str < end) {
1861fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1862fef20d9cSFrederic Weisbecker 					copy = end - str;
1863fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1864fef20d9cSFrederic Weisbecker 			}
1865fef20d9cSFrederic Weisbecker 			str += read;
1866fef20d9cSFrederic Weisbecker 			break;
18674370aa4aSLai Jiangshan 		}
18684370aa4aSLai Jiangshan 
1869ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1870fef20d9cSFrederic Weisbecker 			spec.field_width = get_arg(int);
1871fef20d9cSFrederic Weisbecker 			break;
18724370aa4aSLai Jiangshan 
1873fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1874fef20d9cSFrederic Weisbecker 			spec.precision = get_arg(int);
1875fef20d9cSFrederic Weisbecker 			break;
18764370aa4aSLai Jiangshan 
1877d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1878d4be151bSAndré Goddard Rosa 			char c;
1879d4be151bSAndré Goddard Rosa 
1880fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1881fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
18824370aa4aSLai Jiangshan 					if (str < end)
18834370aa4aSLai Jiangshan 						*str = ' ';
18844370aa4aSLai Jiangshan 					++str;
18854370aa4aSLai Jiangshan 				}
18864370aa4aSLai Jiangshan 			}
18874370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
18884370aa4aSLai Jiangshan 			if (str < end)
18894370aa4aSLai Jiangshan 				*str = c;
18904370aa4aSLai Jiangshan 			++str;
1891fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
18924370aa4aSLai Jiangshan 				if (str < end)
18934370aa4aSLai Jiangshan 					*str = ' ';
18944370aa4aSLai Jiangshan 				++str;
18954370aa4aSLai Jiangshan 			}
1896fef20d9cSFrederic Weisbecker 			break;
1897d4be151bSAndré Goddard Rosa 		}
18984370aa4aSLai Jiangshan 
1899fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
19004370aa4aSLai Jiangshan 			const char *str_arg = args;
1901d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
1902fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
1903fef20d9cSFrederic Weisbecker 			break;
19044370aa4aSLai Jiangshan 		}
19054370aa4aSLai Jiangshan 
1906fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1907fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, get_arg(void *), spec);
1908fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
19094370aa4aSLai Jiangshan 				fmt++;
1910fef20d9cSFrederic Weisbecker 			break;
19114370aa4aSLai Jiangshan 
1912fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1913fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
19144370aa4aSLai Jiangshan 			if (str < end)
19154370aa4aSLai Jiangshan 				*str = '%';
19164370aa4aSLai Jiangshan 			++str;
1917fef20d9cSFrederic Weisbecker 			break;
1918fef20d9cSFrederic Weisbecker 
1919fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS:
1920fef20d9cSFrederic Weisbecker 			/* skip */
1921fef20d9cSFrederic Weisbecker 			break;
1922fef20d9cSFrederic Weisbecker 
1923d4be151bSAndré Goddard Rosa 		default: {
1924d4be151bSAndré Goddard Rosa 			unsigned long long num;
1925d4be151bSAndré Goddard Rosa 
1926fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1927fef20d9cSFrederic Weisbecker 
1928fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
19294370aa4aSLai Jiangshan 				num = get_arg(long long);
1930fef20d9cSFrederic Weisbecker 				break;
1931fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1932fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1933fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
1934fef20d9cSFrederic Weisbecker 				break;
1935fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
19364370aa4aSLai Jiangshan 				num = get_arg(size_t);
1937fef20d9cSFrederic Weisbecker 				break;
1938fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
19394370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
1940fef20d9cSFrederic Weisbecker 				break;
1941a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1942a4e94ef0SZhaolei 				num = get_arg(unsigned char);
1943a4e94ef0SZhaolei 				break;
1944a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1945a4e94ef0SZhaolei 				num = get_arg(signed char);
1946a4e94ef0SZhaolei 				break;
1947fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1948fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
1949fef20d9cSFrederic Weisbecker 				break;
1950fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1951fef20d9cSFrederic Weisbecker 				num = get_arg(short);
1952fef20d9cSFrederic Weisbecker 				break;
1953fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
19544370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
1955fef20d9cSFrederic Weisbecker 				break;
1956fef20d9cSFrederic Weisbecker 			default:
1957fef20d9cSFrederic Weisbecker 				num = get_arg(int);
19584370aa4aSLai Jiangshan 			}
1959fef20d9cSFrederic Weisbecker 
1960fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
1961d4be151bSAndré Goddard Rosa 		} /* default: */
1962d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
1963d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
1964fef20d9cSFrederic Weisbecker 
19654370aa4aSLai Jiangshan 	if (size > 0) {
19664370aa4aSLai Jiangshan 		if (str < end)
19674370aa4aSLai Jiangshan 			*str = '\0';
19684370aa4aSLai Jiangshan 		else
19694370aa4aSLai Jiangshan 			end[-1] = '\0';
19704370aa4aSLai Jiangshan 	}
1971fef20d9cSFrederic Weisbecker 
19724370aa4aSLai Jiangshan #undef get_arg
19734370aa4aSLai Jiangshan 
19744370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
19754370aa4aSLai Jiangshan 	return str - buf;
19764370aa4aSLai Jiangshan }
19774370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
19784370aa4aSLai Jiangshan 
19794370aa4aSLai Jiangshan /**
19804370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
19814370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
19824370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
19834370aa4aSLai Jiangshan  * @fmt: The format string to use
19844370aa4aSLai Jiangshan  * @...: Arguments for the format string
19854370aa4aSLai Jiangshan  *
19864370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
19874370aa4aSLai Jiangshan  * into @bin_buf.
19884370aa4aSLai Jiangshan  */
19894370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
19904370aa4aSLai Jiangshan {
19914370aa4aSLai Jiangshan 	va_list args;
19924370aa4aSLai Jiangshan 	int ret;
19934370aa4aSLai Jiangshan 
19944370aa4aSLai Jiangshan 	va_start(args, fmt);
19954370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
19964370aa4aSLai Jiangshan 	va_end(args);
19977b9186f5SAndré Goddard Rosa 
19984370aa4aSLai Jiangshan 	return ret;
19994370aa4aSLai Jiangshan }
20004370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
20014370aa4aSLai Jiangshan 
20024370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
20034370aa4aSLai Jiangshan 
20041da177e4SLinus Torvalds /**
20051da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
20061da177e4SLinus Torvalds  * @buf:	input buffer
20071da177e4SLinus Torvalds  * @fmt:	format of buffer
20081da177e4SLinus Torvalds  * @args:	arguments
20091da177e4SLinus Torvalds  */
20101da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
20111da177e4SLinus Torvalds {
20121da177e4SLinus Torvalds 	const char *str = buf;
20131da177e4SLinus Torvalds 	char *next;
20141da177e4SLinus Torvalds 	char digit;
20151da177e4SLinus Torvalds 	int num = 0;
2016ef0658f3SJoe Perches 	u8 qualifier;
2017ef0658f3SJoe Perches 	u8 base;
2018ef0658f3SJoe Perches 	s16 field_width;
2019d4be151bSAndré Goddard Rosa 	bool is_sign;
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	while (*fmt && *str) {
20221da177e4SLinus Torvalds 		/* skip any white space in format */
20231da177e4SLinus Torvalds 		/* white space in format matchs any amount of
20241da177e4SLinus Torvalds 		 * white space, including none, in the input.
20251da177e4SLinus Torvalds 		 */
20261da177e4SLinus Torvalds 		if (isspace(*fmt)) {
2027e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
2028e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
20291da177e4SLinus Torvalds 		}
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
20321da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
20331da177e4SLinus Torvalds 			if (*fmt++ != *str++)
20341da177e4SLinus Torvalds 				break;
20351da177e4SLinus Torvalds 			continue;
20361da177e4SLinus Torvalds 		}
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 		if (!*fmt)
20391da177e4SLinus Torvalds 			break;
20401da177e4SLinus Torvalds 		++fmt;
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 		/* skip this conversion.
20431da177e4SLinus Torvalds 		 * advance both strings to next white space
20441da177e4SLinus Torvalds 		 */
20451da177e4SLinus Torvalds 		if (*fmt == '*') {
20468fccae2cSAndy Spencer 			while (!isspace(*fmt) && *fmt != '%' && *fmt)
20471da177e4SLinus Torvalds 				fmt++;
20481da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
20491da177e4SLinus Torvalds 				str++;
20501da177e4SLinus Torvalds 			continue;
20511da177e4SLinus Torvalds 		}
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 		/* get field width */
20541da177e4SLinus Torvalds 		field_width = -1;
20551da177e4SLinus Torvalds 		if (isdigit(*fmt))
20561da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 		/* get conversion qualifier */
20591da177e4SLinus Torvalds 		qualifier = -1;
206075fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
206175fb8f26SAndy Shevchenko 		    _tolower(*fmt) == 'z') {
20621da177e4SLinus Torvalds 			qualifier = *fmt++;
20631da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
20641da177e4SLinus Torvalds 				if (qualifier == 'h') {
20651da177e4SLinus Torvalds 					qualifier = 'H';
20661da177e4SLinus Torvalds 					fmt++;
20671da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
20681da177e4SLinus Torvalds 					qualifier = 'L';
20691da177e4SLinus Torvalds 					fmt++;
20701da177e4SLinus Torvalds 				}
20711da177e4SLinus Torvalds 			}
20721da177e4SLinus Torvalds 		}
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 		if (!*fmt || !*str)
20751da177e4SLinus Torvalds 			break;
20761da177e4SLinus Torvalds 
2077d4be151bSAndré Goddard Rosa 		base = 10;
2078d4be151bSAndré Goddard Rosa 		is_sign = 0;
2079d4be151bSAndré Goddard Rosa 
20801da177e4SLinus Torvalds 		switch (*fmt++) {
20811da177e4SLinus Torvalds 		case 'c':
20821da177e4SLinus Torvalds 		{
20831da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
20841da177e4SLinus Torvalds 			if (field_width == -1)
20851da177e4SLinus Torvalds 				field_width = 1;
20861da177e4SLinus Torvalds 			do {
20871da177e4SLinus Torvalds 				*s++ = *str++;
20881da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
20891da177e4SLinus Torvalds 			num++;
20901da177e4SLinus Torvalds 		}
20911da177e4SLinus Torvalds 		continue;
20921da177e4SLinus Torvalds 		case 's':
20931da177e4SLinus Torvalds 		{
20941da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
20951da177e4SLinus Torvalds 			if (field_width == -1)
20964be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
20971da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
2098e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 			/* now copy until next white space */
21017b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
21021da177e4SLinus Torvalds 				*s++ = *str++;
21031da177e4SLinus Torvalds 			*s = '\0';
21041da177e4SLinus Torvalds 			num++;
21051da177e4SLinus Torvalds 		}
21061da177e4SLinus Torvalds 		continue;
21071da177e4SLinus Torvalds 		case 'n':
21081da177e4SLinus Torvalds 			/* return number of characters read so far */
21091da177e4SLinus Torvalds 		{
21101da177e4SLinus Torvalds 			int *i = (int *)va_arg(args, int*);
21111da177e4SLinus Torvalds 			*i = str - buf;
21121da177e4SLinus Torvalds 		}
21131da177e4SLinus Torvalds 		continue;
21141da177e4SLinus Torvalds 		case 'o':
21151da177e4SLinus Torvalds 			base = 8;
21161da177e4SLinus Torvalds 			break;
21171da177e4SLinus Torvalds 		case 'x':
21181da177e4SLinus Torvalds 		case 'X':
21191da177e4SLinus Torvalds 			base = 16;
21201da177e4SLinus Torvalds 			break;
21211da177e4SLinus Torvalds 		case 'i':
21221da177e4SLinus Torvalds 			base = 0;
21231da177e4SLinus Torvalds 		case 'd':
21241da177e4SLinus Torvalds 			is_sign = 1;
21251da177e4SLinus Torvalds 		case 'u':
21261da177e4SLinus Torvalds 			break;
21271da177e4SLinus Torvalds 		case '%':
21281da177e4SLinus Torvalds 			/* looking for '%' in str */
21291da177e4SLinus Torvalds 			if (*str++ != '%')
21301da177e4SLinus Torvalds 				return num;
21311da177e4SLinus Torvalds 			continue;
21321da177e4SLinus Torvalds 		default:
21331da177e4SLinus Torvalds 			/* invalid format; stop here */
21341da177e4SLinus Torvalds 			return num;
21351da177e4SLinus Torvalds 		}
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 		/* have some sort of integer conversion.
21381da177e4SLinus Torvalds 		 * first, skip white space in buffer.
21391da177e4SLinus Torvalds 		 */
2140e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds 		digit = *str;
21431da177e4SLinus Torvalds 		if (is_sign && digit == '-')
21441da177e4SLinus Torvalds 			digit = *(str + 1);
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 		if (!digit
21471da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
21481da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
21491da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
21501da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
21511da177e4SLinus Torvalds 			break;
21521da177e4SLinus Torvalds 
21531da177e4SLinus Torvalds 		switch (qualifier) {
21541da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
21551da177e4SLinus Torvalds 			if (is_sign) {
21561da177e4SLinus Torvalds 				signed char *s = (signed char *)va_arg(args, signed char *);
21571da177e4SLinus Torvalds 				*s = (signed char)simple_strtol(str, &next, base);
21581da177e4SLinus Torvalds 			} else {
21591da177e4SLinus Torvalds 				unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
21601da177e4SLinus Torvalds 				*s = (unsigned char)simple_strtoul(str, &next, base);
21611da177e4SLinus Torvalds 			}
21621da177e4SLinus Torvalds 			break;
21631da177e4SLinus Torvalds 		case 'h':
21641da177e4SLinus Torvalds 			if (is_sign) {
21651da177e4SLinus Torvalds 				short *s = (short *)va_arg(args, short *);
21661da177e4SLinus Torvalds 				*s = (short)simple_strtol(str, &next, base);
21671da177e4SLinus Torvalds 			} else {
21681da177e4SLinus Torvalds 				unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
21691da177e4SLinus Torvalds 				*s = (unsigned short)simple_strtoul(str, &next, base);
21701da177e4SLinus Torvalds 			}
21711da177e4SLinus Torvalds 			break;
21721da177e4SLinus Torvalds 		case 'l':
21731da177e4SLinus Torvalds 			if (is_sign) {
21741da177e4SLinus Torvalds 				long *l = (long *)va_arg(args, long *);
21751da177e4SLinus Torvalds 				*l = simple_strtol(str, &next, base);
21761da177e4SLinus Torvalds 			} else {
21771da177e4SLinus Torvalds 				unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
21781da177e4SLinus Torvalds 				*l = simple_strtoul(str, &next, base);
21791da177e4SLinus Torvalds 			}
21801da177e4SLinus Torvalds 			break;
21811da177e4SLinus Torvalds 		case 'L':
21821da177e4SLinus Torvalds 			if (is_sign) {
21831da177e4SLinus Torvalds 				long long *l = (long long *)va_arg(args, long long *);
21841da177e4SLinus Torvalds 				*l = simple_strtoll(str, &next, base);
21851da177e4SLinus Torvalds 			} else {
21861da177e4SLinus Torvalds 				unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
21871da177e4SLinus Torvalds 				*l = simple_strtoull(str, &next, base);
21881da177e4SLinus Torvalds 			}
21891da177e4SLinus Torvalds 			break;
21901da177e4SLinus Torvalds 		case 'Z':
21911da177e4SLinus Torvalds 		case 'z':
21921da177e4SLinus Torvalds 		{
21931da177e4SLinus Torvalds 			size_t *s = (size_t *)va_arg(args, size_t *);
21941da177e4SLinus Torvalds 			*s = (size_t)simple_strtoul(str, &next, base);
21951da177e4SLinus Torvalds 		}
21961da177e4SLinus Torvalds 		break;
21971da177e4SLinus Torvalds 		default:
21981da177e4SLinus Torvalds 			if (is_sign) {
21991da177e4SLinus Torvalds 				int *i = (int *)va_arg(args, int *);
22001da177e4SLinus Torvalds 				*i = (int)simple_strtol(str, &next, base);
22011da177e4SLinus Torvalds 			} else {
22021da177e4SLinus Torvalds 				unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
22031da177e4SLinus Torvalds 				*i = (unsigned int)simple_strtoul(str, &next, base);
22041da177e4SLinus Torvalds 			}
22051da177e4SLinus Torvalds 			break;
22061da177e4SLinus Torvalds 		}
22071da177e4SLinus Torvalds 		num++;
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 		if (!next)
22101da177e4SLinus Torvalds 			break;
22111da177e4SLinus Torvalds 		str = next;
22121da177e4SLinus Torvalds 	}
2213c6b40d16SJohannes Berg 
2214c6b40d16SJohannes Berg 	/*
2215c6b40d16SJohannes Berg 	 * Now we've come all the way through so either the input string or the
2216c6b40d16SJohannes Berg 	 * format ended. In the former case, there can be a %n at the current
2217c6b40d16SJohannes Berg 	 * position in the format that needs to be filled.
2218c6b40d16SJohannes Berg 	 */
2219c6b40d16SJohannes Berg 	if (*fmt == '%' && *(fmt + 1) == 'n') {
2220c6b40d16SJohannes Berg 		int *p = (int *)va_arg(args, int *);
2221c6b40d16SJohannes Berg 		*p = str - buf;
2222c6b40d16SJohannes Berg 	}
2223c6b40d16SJohannes Berg 
22241da177e4SLinus Torvalds 	return num;
22251da177e4SLinus Torvalds }
22261da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds /**
22291da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
22301da177e4SLinus Torvalds  * @buf:	input buffer
22311da177e4SLinus Torvalds  * @fmt:	formatting of buffer
22321da177e4SLinus Torvalds  * @...:	resulting arguments
22331da177e4SLinus Torvalds  */
22341da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
22351da177e4SLinus Torvalds {
22361da177e4SLinus Torvalds 	va_list args;
22371da177e4SLinus Torvalds 	int i;
22381da177e4SLinus Torvalds 
22391da177e4SLinus Torvalds 	va_start(args, fmt);
22401da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
22411da177e4SLinus Torvalds 	va_end(args);
22427b9186f5SAndré Goddard Rosa 
22431da177e4SLinus Torvalds 	return i;
22441da177e4SLinus Torvalds }
22451da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
2246