xref: /openbmc/linux/lib/vsprintf.c (revision c8f44affb7244f2ac3e703cab13d55ede27621bb)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/lib/vsprintf.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds  * Wirzenius wrote this portably, Torvalds fucked it up :-)
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /*
131da177e4SLinus Torvalds  * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
141da177e4SLinus Torvalds  * - changed to provide snprintf and vsnprintf functions
151da177e4SLinus Torvalds  * So Feb  1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
161da177e4SLinus Torvalds  * - scnprintf and vscnprintf
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <stdarg.h>
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/types.h>
221da177e4SLinus Torvalds #include <linux/string.h>
231da177e4SLinus Torvalds #include <linux/ctype.h>
241da177e4SLinus Torvalds #include <linux/kernel.h>
250fe1ef24SLinus Torvalds #include <linux/kallsyms.h>
260fe1ef24SLinus Torvalds #include <linux/uaccess.h>
27332d2e78SLinus Torvalds #include <linux/ioport.h>
288a27f7c9SJoe Perches #include <net/addrconf.h>
291da177e4SLinus Torvalds 
304e57b681STim Schmielau #include <asm/page.h>		/* for PAGE_SIZE */
311da177e4SLinus Torvalds #include <asm/div64.h>
32deac93dfSJames Bottomley #include <asm/sections.h>	/* for dereference_function_descriptor() */
331da177e4SLinus Torvalds 
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
1154277eeddSDenis Vlasenko  * using code from
1164277eeddSDenis Vlasenko  * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
1174277eeddSDenis Vlasenko  * (with permission from the author, Douglas W. Jones). */
1184277eeddSDenis Vlasenko 
1194277eeddSDenis Vlasenko /* Formats correctly any integer in [0,99999].
1204277eeddSDenis Vlasenko  * Outputs from one to five digits depending on input.
1214277eeddSDenis Vlasenko  * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
122cf3b429bSJoe Perches static noinline_for_stack
123cf3b429bSJoe Perches char *put_dec_trunc(char *buf, unsigned q)
1244277eeddSDenis Vlasenko {
1254277eeddSDenis Vlasenko 	unsigned d3, d2, d1, d0;
1264277eeddSDenis Vlasenko 	d1 = (q>>4) & 0xf;
1274277eeddSDenis Vlasenko 	d2 = (q>>8) & 0xf;
1284277eeddSDenis Vlasenko 	d3 = (q>>12);
1294277eeddSDenis Vlasenko 
1304277eeddSDenis Vlasenko 	d0 = 6*(d3 + d2 + d1) + (q & 0xf);
1314277eeddSDenis Vlasenko 	q = (d0 * 0xcd) >> 11;
1324277eeddSDenis Vlasenko 	d0 = d0 - 10*q;
1334277eeddSDenis Vlasenko 	*buf++ = d0 + '0'; /* least significant digit */
1344277eeddSDenis Vlasenko 	d1 = q + 9*d3 + 5*d2 + d1;
1354277eeddSDenis Vlasenko 	if (d1 != 0) {
1364277eeddSDenis Vlasenko 		q = (d1 * 0xcd) >> 11;
1374277eeddSDenis Vlasenko 		d1 = d1 - 10*q;
1384277eeddSDenis Vlasenko 		*buf++ = d1 + '0'; /* next digit */
1394277eeddSDenis Vlasenko 
1404277eeddSDenis Vlasenko 		d2 = q + 2*d2;
1414277eeddSDenis Vlasenko 		if ((d2 != 0) || (d3 != 0)) {
1424277eeddSDenis Vlasenko 			q = (d2 * 0xd) >> 7;
1434277eeddSDenis Vlasenko 			d2 = d2 - 10*q;
1444277eeddSDenis Vlasenko 			*buf++ = d2 + '0'; /* next digit */
1454277eeddSDenis Vlasenko 
1464277eeddSDenis Vlasenko 			d3 = q + 4*d3;
1474277eeddSDenis Vlasenko 			if (d3 != 0) {
1484277eeddSDenis Vlasenko 				q = (d3 * 0xcd) >> 11;
1494277eeddSDenis Vlasenko 				d3 = d3 - 10*q;
1504277eeddSDenis Vlasenko 				*buf++ = d3 + '0';  /* next digit */
1514277eeddSDenis Vlasenko 				if (q != 0)
1524277eeddSDenis Vlasenko 					*buf++ = q + '0'; /* most sign. digit */
1534277eeddSDenis Vlasenko 			}
1544277eeddSDenis Vlasenko 		}
1554277eeddSDenis Vlasenko 	}
1567b9186f5SAndré Goddard Rosa 
1574277eeddSDenis Vlasenko 	return buf;
1584277eeddSDenis Vlasenko }
1594277eeddSDenis Vlasenko /* Same with if's removed. Always emits five digits */
160cf3b429bSJoe Perches static noinline_for_stack
161cf3b429bSJoe Perches char *put_dec_full(char *buf, unsigned q)
1624277eeddSDenis Vlasenko {
1634277eeddSDenis Vlasenko 	/* BTW, if q is in [0,9999], 8-bit ints will be enough, */
1644277eeddSDenis Vlasenko 	/* but anyway, gcc produces better code with full-sized ints */
1654277eeddSDenis Vlasenko 	unsigned d3, d2, d1, d0;
1664277eeddSDenis Vlasenko 	d1 = (q>>4) & 0xf;
1674277eeddSDenis Vlasenko 	d2 = (q>>8) & 0xf;
1684277eeddSDenis Vlasenko 	d3 = (q>>12);
1694277eeddSDenis Vlasenko 
1707b9186f5SAndré Goddard Rosa 	/*
1717b9186f5SAndré Goddard Rosa 	 * Possible ways to approx. divide by 10
1727b9186f5SAndré Goddard Rosa 	 * gcc -O2 replaces multiply with shifts and adds
1737b9186f5SAndré Goddard Rosa 	 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
1747b9186f5SAndré Goddard Rosa 	 * (x * 0x67) >> 10:  1100111
1757b9186f5SAndré Goddard Rosa 	 * (x * 0x34) >> 9:    110100 - same
1767b9186f5SAndré Goddard Rosa 	 * (x * 0x1a) >> 8:     11010 - same
1777b9186f5SAndré Goddard Rosa 	 * (x * 0x0d) >> 7:      1101 - same, shortest code (on i386)
1787b9186f5SAndré Goddard Rosa 	 */
1794277eeddSDenis Vlasenko 	d0 = 6*(d3 + d2 + d1) + (q & 0xf);
1804277eeddSDenis Vlasenko 	q = (d0 * 0xcd) >> 11;
1814277eeddSDenis Vlasenko 	d0 = d0 - 10*q;
1824277eeddSDenis Vlasenko 	*buf++ = d0 + '0';
1834277eeddSDenis Vlasenko 	d1 = q + 9*d3 + 5*d2 + d1;
1844277eeddSDenis Vlasenko 		q = (d1 * 0xcd) >> 11;
1854277eeddSDenis Vlasenko 		d1 = d1 - 10*q;
1864277eeddSDenis Vlasenko 		*buf++ = d1 + '0';
1874277eeddSDenis Vlasenko 
1884277eeddSDenis Vlasenko 		d2 = q + 2*d2;
1894277eeddSDenis Vlasenko 			q = (d2 * 0xd) >> 7;
1904277eeddSDenis Vlasenko 			d2 = d2 - 10*q;
1914277eeddSDenis Vlasenko 			*buf++ = d2 + '0';
1924277eeddSDenis Vlasenko 
1934277eeddSDenis Vlasenko 			d3 = q + 4*d3;
1944277eeddSDenis Vlasenko 				q = (d3 * 0xcd) >> 11; /* - shorter code */
1954277eeddSDenis Vlasenko 				/* q = (d3 * 0x67) >> 10; - would also work */
1964277eeddSDenis Vlasenko 				d3 = d3 - 10*q;
1974277eeddSDenis Vlasenko 				*buf++ = d3 + '0';
1984277eeddSDenis Vlasenko 					*buf++ = q + '0';
1997b9186f5SAndré Goddard Rosa 
2004277eeddSDenis Vlasenko 	return buf;
2014277eeddSDenis Vlasenko }
2024277eeddSDenis Vlasenko /* No inlining helps gcc to use registers better */
203cf3b429bSJoe Perches static noinline_for_stack
204cf3b429bSJoe Perches char *put_dec(char *buf, unsigned long long num)
2054277eeddSDenis Vlasenko {
2064277eeddSDenis Vlasenko 	while (1) {
2074277eeddSDenis Vlasenko 		unsigned rem;
2084277eeddSDenis Vlasenko 		if (num < 100000)
2094277eeddSDenis Vlasenko 			return put_dec_trunc(buf, num);
2104277eeddSDenis Vlasenko 		rem = do_div(num, 100000);
2114277eeddSDenis Vlasenko 		buf = put_dec_full(buf, rem);
2124277eeddSDenis Vlasenko 	}
2134277eeddSDenis Vlasenko }
2144277eeddSDenis Vlasenko 
2151da177e4SLinus Torvalds #define ZEROPAD	1		/* pad with zero */
2161da177e4SLinus Torvalds #define SIGN	2		/* unsigned/signed long */
2171da177e4SLinus Torvalds #define PLUS	4		/* show plus */
2181da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
2191da177e4SLinus Torvalds #define LEFT	16		/* left justified */
220b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
221b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
2221da177e4SLinus Torvalds 
223fef20d9cSFrederic Weisbecker enum format_type {
224fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
225ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
226fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
227fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
228fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
229fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
230fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
231fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
232fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
233fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
234fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
235a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
236a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
237fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
238fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
239fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
240fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
241fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NRCHARS,
242fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
243fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
244fef20d9cSFrederic Weisbecker };
245fef20d9cSFrederic Weisbecker 
246fef20d9cSFrederic Weisbecker struct printf_spec {
2474e310fdaSJoe Perches 	u8	type;		/* format_type enum */
248ef0658f3SJoe Perches 	u8	flags;		/* flags to number() */
2494e310fdaSJoe Perches 	u8	base;		/* number base, 8, 10 or 16 only */
2504e310fdaSJoe Perches 	u8	qualifier;	/* number qualifier, one of 'hHlLtzZ' */
2514e310fdaSJoe Perches 	s16	field_width;	/* width of output field */
2524e310fdaSJoe Perches 	s16	precision;	/* # of digits/chars */
253fef20d9cSFrederic Weisbecker };
254fef20d9cSFrederic Weisbecker 
255cf3b429bSJoe Perches static noinline_for_stack
256cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
257fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
2581da177e4SLinus Torvalds {
2599b706aeeSDenys Vlasenko 	/* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
2609b706aeeSDenys Vlasenko 	static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
2619b706aeeSDenys Vlasenko 
2629b706aeeSDenys Vlasenko 	char tmp[66];
2639b706aeeSDenys Vlasenko 	char sign;
2649b706aeeSDenys Vlasenko 	char locase;
265fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
2661da177e4SLinus Torvalds 	int i;
2671da177e4SLinus Torvalds 
2689b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
2699b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
270fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
271fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
272fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
2731da177e4SLinus Torvalds 	sign = 0;
274fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
2751da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
2761da177e4SLinus Torvalds 			sign = '-';
2771da177e4SLinus Torvalds 			num = -(signed long long)num;
278fef20d9cSFrederic Weisbecker 			spec.field_width--;
279fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
2801da177e4SLinus Torvalds 			sign = '+';
281fef20d9cSFrederic Weisbecker 			spec.field_width--;
282fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
2831da177e4SLinus Torvalds 			sign = ' ';
284fef20d9cSFrederic Weisbecker 			spec.field_width--;
2851da177e4SLinus Torvalds 		}
2861da177e4SLinus Torvalds 	}
287b39a7340SDenis Vlasenko 	if (need_pfx) {
288fef20d9cSFrederic Weisbecker 		spec.field_width--;
289fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
290fef20d9cSFrederic Weisbecker 			spec.field_width--;
2911da177e4SLinus Torvalds 	}
292b39a7340SDenis Vlasenko 
293b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
2941da177e4SLinus Torvalds 	i = 0;
2951da177e4SLinus Torvalds 	if (num == 0)
2961da177e4SLinus Torvalds 		tmp[i++] = '0';
2974277eeddSDenis Vlasenko 	/* Generic code, for any base:
2984277eeddSDenis Vlasenko 	else do {
2999b706aeeSDenys Vlasenko 		tmp[i++] = (digits[do_div(num,base)] | locase);
3004277eeddSDenis Vlasenko 	} while (num != 0);
3014277eeddSDenis Vlasenko 	*/
302fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
303fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
304b39a7340SDenis Vlasenko 		int shift = 3;
3057b9186f5SAndré Goddard Rosa 
3067b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
3077b9186f5SAndré Goddard Rosa 			shift = 4;
308b39a7340SDenis Vlasenko 		do {
3099b706aeeSDenys Vlasenko 			tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
310b39a7340SDenis Vlasenko 			num >>= shift;
311b39a7340SDenis Vlasenko 		} while (num);
3124277eeddSDenis Vlasenko 	} else { /* base 10 */
3134277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
3144277eeddSDenis Vlasenko 	}
315b39a7340SDenis Vlasenko 
316b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
317fef20d9cSFrederic Weisbecker 	if (i > spec.precision)
318fef20d9cSFrederic Weisbecker 		spec.precision = i;
319b39a7340SDenis Vlasenko 	/* leading space padding */
320fef20d9cSFrederic Weisbecker 	spec.field_width -= spec.precision;
321fef20d9cSFrederic Weisbecker 	if (!(spec.flags & (ZEROPAD+LEFT))) {
322fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
323f796937aSJeremy Fitzhardinge 			if (buf < end)
3241da177e4SLinus Torvalds 				*buf = ' ';
3251da177e4SLinus Torvalds 			++buf;
3261da177e4SLinus Torvalds 		}
3271da177e4SLinus Torvalds 	}
328b39a7340SDenis Vlasenko 	/* sign */
3291da177e4SLinus Torvalds 	if (sign) {
330f796937aSJeremy Fitzhardinge 		if (buf < end)
3311da177e4SLinus Torvalds 			*buf = sign;
3321da177e4SLinus Torvalds 		++buf;
3331da177e4SLinus Torvalds 	}
334b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
335b39a7340SDenis Vlasenko 	if (need_pfx) {
336f796937aSJeremy Fitzhardinge 		if (buf < end)
3371da177e4SLinus Torvalds 			*buf = '0';
3381da177e4SLinus Torvalds 		++buf;
339fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
340f796937aSJeremy Fitzhardinge 			if (buf < end)
3419b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
3421da177e4SLinus Torvalds 			++buf;
3431da177e4SLinus Torvalds 		}
3441da177e4SLinus Torvalds 	}
345b39a7340SDenis Vlasenko 	/* zero or space padding */
346fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
347fef20d9cSFrederic Weisbecker 		char c = (spec.flags & ZEROPAD) ? '0' : ' ';
348fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
349f796937aSJeremy Fitzhardinge 			if (buf < end)
3501da177e4SLinus Torvalds 				*buf = c;
3511da177e4SLinus Torvalds 			++buf;
3521da177e4SLinus Torvalds 		}
3531da177e4SLinus Torvalds 	}
354b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
355fef20d9cSFrederic Weisbecker 	while (i <= --spec.precision) {
356f796937aSJeremy Fitzhardinge 		if (buf < end)
3571da177e4SLinus Torvalds 			*buf = '0';
3581da177e4SLinus Torvalds 		++buf;
3591da177e4SLinus Torvalds 	}
360b39a7340SDenis Vlasenko 	/* actual digits of result */
361b39a7340SDenis Vlasenko 	while (--i >= 0) {
362f796937aSJeremy Fitzhardinge 		if (buf < end)
3631da177e4SLinus Torvalds 			*buf = tmp[i];
3641da177e4SLinus Torvalds 		++buf;
3651da177e4SLinus Torvalds 	}
366b39a7340SDenis Vlasenko 	/* trailing space padding */
367fef20d9cSFrederic Weisbecker 	while (--spec.field_width >= 0) {
368f796937aSJeremy Fitzhardinge 		if (buf < end)
3691da177e4SLinus Torvalds 			*buf = ' ';
3701da177e4SLinus Torvalds 		++buf;
3711da177e4SLinus Torvalds 	}
3727b9186f5SAndré Goddard Rosa 
3731da177e4SLinus Torvalds 	return buf;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
376cf3b429bSJoe Perches static noinline_for_stack
377cf3b429bSJoe Perches char *string(char *buf, char *end, const char *s, struct printf_spec spec)
3780f9bfa56SLinus Torvalds {
3790f9bfa56SLinus Torvalds 	int len, i;
3800f9bfa56SLinus Torvalds 
3810f9bfa56SLinus Torvalds 	if ((unsigned long)s < PAGE_SIZE)
3820f4f81dcSAndré Goddard Rosa 		s = "(null)";
3830f9bfa56SLinus Torvalds 
384fef20d9cSFrederic Weisbecker 	len = strnlen(s, spec.precision);
3850f9bfa56SLinus Torvalds 
386fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
387fef20d9cSFrederic Weisbecker 		while (len < spec.field_width--) {
3880f9bfa56SLinus Torvalds 			if (buf < end)
3890f9bfa56SLinus Torvalds 				*buf = ' ';
3900f9bfa56SLinus Torvalds 			++buf;
3910f9bfa56SLinus Torvalds 		}
3920f9bfa56SLinus Torvalds 	}
3930f9bfa56SLinus Torvalds 	for (i = 0; i < len; ++i) {
3940f9bfa56SLinus Torvalds 		if (buf < end)
3950f9bfa56SLinus Torvalds 			*buf = *s;
3960f9bfa56SLinus Torvalds 		++buf; ++s;
3970f9bfa56SLinus Torvalds 	}
398fef20d9cSFrederic Weisbecker 	while (len < spec.field_width--) {
3990f9bfa56SLinus Torvalds 		if (buf < end)
4000f9bfa56SLinus Torvalds 			*buf = ' ';
4010f9bfa56SLinus Torvalds 		++buf;
4020f9bfa56SLinus Torvalds 	}
4037b9186f5SAndré Goddard Rosa 
4040f9bfa56SLinus Torvalds 	return buf;
4050f9bfa56SLinus Torvalds }
4060f9bfa56SLinus Torvalds 
407cf3b429bSJoe Perches static noinline_for_stack
408cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
4090c8b946eSFrederic Weisbecker 		    struct printf_spec spec, char ext)
4100fe1ef24SLinus Torvalds {
4110fe1ef24SLinus Torvalds 	unsigned long value = (unsigned long) ptr;
4120fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
4130fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
4140f77a8d3SNamhyung Kim 	if (ext == 'B')
4150f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
4160f77a8d3SNamhyung Kim 	else if (ext != 'f' && ext != 's')
4170fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
4180c8b946eSFrederic Weisbecker 	else
4190c8b946eSFrederic Weisbecker 		kallsyms_lookup(value, NULL, NULL, NULL, sym);
4207b9186f5SAndré Goddard Rosa 
421fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
4220fe1ef24SLinus Torvalds #else
423fef20d9cSFrederic Weisbecker 	spec.field_width = 2 * sizeof(void *);
424fef20d9cSFrederic Weisbecker 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
425fef20d9cSFrederic Weisbecker 	spec.base = 16;
4267b9186f5SAndré Goddard Rosa 
427fef20d9cSFrederic Weisbecker 	return number(buf, end, value, spec);
4280fe1ef24SLinus Torvalds #endif
4290fe1ef24SLinus Torvalds }
4300fe1ef24SLinus Torvalds 
431cf3b429bSJoe Perches static noinline_for_stack
432cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
433fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
434332d2e78SLinus Torvalds {
435332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
43628405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
437332d2e78SLinus Torvalds #endif
438332d2e78SLinus Torvalds 
439332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
44028405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
441332d2e78SLinus Torvalds #endif
4424da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
443fef20d9cSFrederic Weisbecker 		.base = 16,
4444da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
445fef20d9cSFrederic Weisbecker 		.precision = -1,
446fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
447fef20d9cSFrederic Weisbecker 	};
4484da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
4494da0b66cSBjorn Helgaas 		.base = 16,
4504da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
4514da0b66cSBjorn Helgaas 		.precision = -1,
4524da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
4534da0b66cSBjorn Helgaas 	};
4540f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
4550f4050c7SBjorn Helgaas 		.base = 16,
4560f4050c7SBjorn Helgaas 		.field_width = 2,
4570f4050c7SBjorn Helgaas 		.precision = -1,
4580f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
4590f4050c7SBjorn Helgaas 	};
4604da0b66cSBjorn Helgaas 	static const struct printf_spec dec_spec = {
461c91d3376SBjorn Helgaas 		.base = 10,
462c91d3376SBjorn Helgaas 		.precision = -1,
463c91d3376SBjorn Helgaas 		.flags = 0,
464c91d3376SBjorn Helgaas 	};
4654da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
466fd95541eSBjorn Helgaas 		.field_width = -1,
467fd95541eSBjorn Helgaas 		.precision = 10,
468fd95541eSBjorn Helgaas 		.flags = LEFT,
469fd95541eSBjorn Helgaas 	};
4704da0b66cSBjorn Helgaas 	static const struct printf_spec flag_spec = {
471fd95541eSBjorn Helgaas 		.base = 16,
472fd95541eSBjorn Helgaas 		.precision = -1,
473fd95541eSBjorn Helgaas 		.flags = SPECIAL | SMALL,
474fd95541eSBjorn Helgaas 	};
475c7dabef8SBjorn Helgaas 
476c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
477c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
478c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
479c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
4809d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
481c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
482c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
483c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
484c7dabef8SBjorn Helgaas 
485332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
486c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
4874da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
488332d2e78SLinus Torvalds 
489332d2e78SLinus Torvalds 	*p++ = '[';
4904da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
491fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
4924da0b66cSBjorn Helgaas 		specp = &io_spec;
4934da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
494fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
4954da0b66cSBjorn Helgaas 		specp = &mem_spec;
4964da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
497fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
4984da0b66cSBjorn Helgaas 		specp = &dec_spec;
4994da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
500fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
5014da0b66cSBjorn Helgaas 		specp = &dec_spec;
5020f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
5030f4050c7SBjorn Helgaas 		p = string(p, pend, "bus ", str_spec);
5040f4050c7SBjorn Helgaas 		specp = &bus_spec;
5054da0b66cSBjorn Helgaas 	} else {
506c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
5074da0b66cSBjorn Helgaas 		specp = &mem_spec;
508c7dabef8SBjorn Helgaas 		decode = 0;
509fd95541eSBjorn Helgaas 	}
5104da0b66cSBjorn Helgaas 	p = number(p, pend, res->start, *specp);
511c91d3376SBjorn Helgaas 	if (res->start != res->end) {
512332d2e78SLinus Torvalds 		*p++ = '-';
5134da0b66cSBjorn Helgaas 		p = number(p, pend, res->end, *specp);
514c91d3376SBjorn Helgaas 	}
515c7dabef8SBjorn Helgaas 	if (decode) {
516fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
517fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
518fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
519fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
5209d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
5219d7cca04SBjorn Helgaas 			p = string(p, pend, " window", str_spec);
522fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
523fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
524c7dabef8SBjorn Helgaas 	} else {
525fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
526c7dabef8SBjorn Helgaas 		p = number(p, pend, res->flags, flag_spec);
527fd95541eSBjorn Helgaas 	}
528332d2e78SLinus Torvalds 	*p++ = ']';
529c7dabef8SBjorn Helgaas 	*p = '\0';
530332d2e78SLinus Torvalds 
531fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
532332d2e78SLinus Torvalds }
533332d2e78SLinus Torvalds 
534cf3b429bSJoe Perches static noinline_for_stack
535cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
5368a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
537dd45c9cfSHarvey Harrison {
5388a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
539dd45c9cfSHarvey Harrison 	char *p = mac_addr;
540dd45c9cfSHarvey Harrison 	int i;
541bc7259a2SJoe Perches 	char separator;
542bc7259a2SJoe Perches 
543bc7259a2SJoe Perches 	if (fmt[1] == 'F') {		/* FDDI canonical format */
544bc7259a2SJoe Perches 		separator = '-';
545bc7259a2SJoe Perches 	} else {
546bc7259a2SJoe Perches 		separator = ':';
547bc7259a2SJoe Perches 	}
548dd45c9cfSHarvey Harrison 
549dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
55055036ba7SAndy Shevchenko 		p = hex_byte_pack(p, addr[i]);
5518a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
552bc7259a2SJoe Perches 			*p++ = separator;
553dd45c9cfSHarvey Harrison 	}
554dd45c9cfSHarvey Harrison 	*p = '\0';
555dd45c9cfSHarvey Harrison 
556fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
557dd45c9cfSHarvey Harrison }
558dd45c9cfSHarvey Harrison 
559cf3b429bSJoe Perches static noinline_for_stack
560cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
561689afa7dSHarvey Harrison {
562689afa7dSHarvey Harrison 	int i;
5630159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
5640159f24eSJoe Perches 	int index;
5650159f24eSJoe Perches 	int step;
566689afa7dSHarvey Harrison 
5670159f24eSJoe Perches 	switch (fmt[2]) {
5680159f24eSJoe Perches 	case 'h':
5690159f24eSJoe Perches #ifdef __BIG_ENDIAN
5700159f24eSJoe Perches 		index = 0;
5710159f24eSJoe Perches 		step = 1;
5720159f24eSJoe Perches #else
5730159f24eSJoe Perches 		index = 3;
5740159f24eSJoe Perches 		step = -1;
5750159f24eSJoe Perches #endif
5760159f24eSJoe Perches 		break;
5770159f24eSJoe Perches 	case 'l':
5780159f24eSJoe Perches 		index = 3;
5790159f24eSJoe Perches 		step = -1;
5800159f24eSJoe Perches 		break;
5810159f24eSJoe Perches 	case 'n':
5820159f24eSJoe Perches 	case 'b':
5830159f24eSJoe Perches 	default:
5840159f24eSJoe Perches 		index = 0;
5850159f24eSJoe Perches 		step = 1;
5860159f24eSJoe Perches 		break;
5870159f24eSJoe Perches 	}
5888a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
5898a27f7c9SJoe Perches 		char temp[3];	/* hold each IP quad in reverse order */
5900159f24eSJoe Perches 		int digits = put_dec_trunc(temp, addr[index]) - temp;
5918a27f7c9SJoe Perches 		if (leading_zeros) {
5928a27f7c9SJoe Perches 			if (digits < 3)
5938a27f7c9SJoe Perches 				*p++ = '0';
5948a27f7c9SJoe Perches 			if (digits < 2)
5958a27f7c9SJoe Perches 				*p++ = '0';
5968a27f7c9SJoe Perches 		}
5978a27f7c9SJoe Perches 		/* reverse the digits in the quad */
5988a27f7c9SJoe Perches 		while (digits--)
5998a27f7c9SJoe Perches 			*p++ = temp[digits];
6008a27f7c9SJoe Perches 		if (i < 3)
6018a27f7c9SJoe Perches 			*p++ = '.';
6020159f24eSJoe Perches 		index += step;
6038a27f7c9SJoe Perches 	}
6048a27f7c9SJoe Perches 	*p = '\0';
6057b9186f5SAndré Goddard Rosa 
6068a27f7c9SJoe Perches 	return p;
6078a27f7c9SJoe Perches }
6088a27f7c9SJoe Perches 
609cf3b429bSJoe Perches static noinline_for_stack
610cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
6118a27f7c9SJoe Perches {
6127b9186f5SAndré Goddard Rosa 	int i, j, range;
6138a27f7c9SJoe Perches 	unsigned char zerolength[8];
6148a27f7c9SJoe Perches 	int longest = 1;
6158a27f7c9SJoe Perches 	int colonpos = -1;
6168a27f7c9SJoe Perches 	u16 word;
6177b9186f5SAndré Goddard Rosa 	u8 hi, lo;
6188a27f7c9SJoe Perches 	bool needcolon = false;
619eb78cd26SJoe Perches 	bool useIPv4;
620eb78cd26SJoe Perches 	struct in6_addr in6;
621eb78cd26SJoe Perches 
622eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
623eb78cd26SJoe Perches 
624eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
6258a27f7c9SJoe Perches 
6268a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
6278a27f7c9SJoe Perches 
6288a27f7c9SJoe Perches 	if (useIPv4)
6298a27f7c9SJoe Perches 		range = 6;
6308a27f7c9SJoe Perches 	else
6318a27f7c9SJoe Perches 		range = 8;
6328a27f7c9SJoe Perches 
6338a27f7c9SJoe Perches 	/* find position of longest 0 run */
6348a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
6358a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
636eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
6378a27f7c9SJoe Perches 				break;
6388a27f7c9SJoe Perches 			zerolength[i]++;
6398a27f7c9SJoe Perches 		}
6408a27f7c9SJoe Perches 	}
6418a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
6428a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
6438a27f7c9SJoe Perches 			longest = zerolength[i];
6448a27f7c9SJoe Perches 			colonpos = i;
6458a27f7c9SJoe Perches 		}
6468a27f7c9SJoe Perches 	}
64729cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
64829cf519eSJoe Perches 		colonpos = -1;
6498a27f7c9SJoe Perches 
6508a27f7c9SJoe Perches 	/* emit address */
6518a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
6528a27f7c9SJoe Perches 		if (i == colonpos) {
6538a27f7c9SJoe Perches 			if (needcolon || i == 0)
6548a27f7c9SJoe Perches 				*p++ = ':';
6558a27f7c9SJoe Perches 			*p++ = ':';
6568a27f7c9SJoe Perches 			needcolon = false;
6578a27f7c9SJoe Perches 			i += longest - 1;
6588a27f7c9SJoe Perches 			continue;
6598a27f7c9SJoe Perches 		}
6608a27f7c9SJoe Perches 		if (needcolon) {
6618a27f7c9SJoe Perches 			*p++ = ':';
6628a27f7c9SJoe Perches 			needcolon = false;
6638a27f7c9SJoe Perches 		}
6648a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
665eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
6668a27f7c9SJoe Perches 		hi = word >> 8;
6678a27f7c9SJoe Perches 		lo = word & 0xff;
6688a27f7c9SJoe Perches 		if (hi) {
6698a27f7c9SJoe Perches 			if (hi > 0x0f)
67055036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
6718a27f7c9SJoe Perches 			else
6728a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
67355036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
6748a27f7c9SJoe Perches 		}
675b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
67655036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
6778a27f7c9SJoe Perches 		else
6788a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
6798a27f7c9SJoe Perches 		needcolon = true;
6808a27f7c9SJoe Perches 	}
6818a27f7c9SJoe Perches 
6828a27f7c9SJoe Perches 	if (useIPv4) {
6838a27f7c9SJoe Perches 		if (needcolon)
6848a27f7c9SJoe Perches 			*p++ = ':';
6850159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
6868a27f7c9SJoe Perches 	}
6878a27f7c9SJoe Perches 	*p = '\0';
6887b9186f5SAndré Goddard Rosa 
6898a27f7c9SJoe Perches 	return p;
6908a27f7c9SJoe Perches }
6918a27f7c9SJoe Perches 
692cf3b429bSJoe Perches static noinline_for_stack
693cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
6948a27f7c9SJoe Perches {
6958a27f7c9SJoe Perches 	int i;
6967b9186f5SAndré Goddard Rosa 
697689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
69855036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
69955036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
7008a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
701689afa7dSHarvey Harrison 			*p++ = ':';
702689afa7dSHarvey Harrison 	}
703689afa7dSHarvey Harrison 	*p = '\0';
7047b9186f5SAndré Goddard Rosa 
7058a27f7c9SJoe Perches 	return p;
7068a27f7c9SJoe Perches }
7078a27f7c9SJoe Perches 
708cf3b429bSJoe Perches static noinline_for_stack
709cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
7108a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
7118a27f7c9SJoe Perches {
7128a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
7138a27f7c9SJoe Perches 
7148a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
715eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
7168a27f7c9SJoe Perches 	else
717eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
718689afa7dSHarvey Harrison 
719fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
720689afa7dSHarvey Harrison }
721689afa7dSHarvey Harrison 
722cf3b429bSJoe Perches static noinline_for_stack
723cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
7248a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
7254aa99606SHarvey Harrison {
7268a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
7274aa99606SHarvey Harrison 
7280159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
7294aa99606SHarvey Harrison 
730fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
7314aa99606SHarvey Harrison }
7324aa99606SHarvey Harrison 
733cf3b429bSJoe Perches static noinline_for_stack
734cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
7359ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
7369ac6e44eSJoe Perches {
7379ac6e44eSJoe Perches 	char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
7389ac6e44eSJoe Perches 	char *p = uuid;
7399ac6e44eSJoe Perches 	int i;
7409ac6e44eSJoe Perches 	static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
7419ac6e44eSJoe Perches 	static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
7429ac6e44eSJoe Perches 	const u8 *index = be;
7439ac6e44eSJoe Perches 	bool uc = false;
7449ac6e44eSJoe Perches 
7459ac6e44eSJoe Perches 	switch (*(++fmt)) {
7469ac6e44eSJoe Perches 	case 'L':
7479ac6e44eSJoe Perches 		uc = true;		/* fall-through */
7489ac6e44eSJoe Perches 	case 'l':
7499ac6e44eSJoe Perches 		index = le;
7509ac6e44eSJoe Perches 		break;
7519ac6e44eSJoe Perches 	case 'B':
7529ac6e44eSJoe Perches 		uc = true;
7539ac6e44eSJoe Perches 		break;
7549ac6e44eSJoe Perches 	}
7559ac6e44eSJoe Perches 
7569ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
75755036ba7SAndy Shevchenko 		p = hex_byte_pack(p, addr[index[i]]);
7589ac6e44eSJoe Perches 		switch (i) {
7599ac6e44eSJoe Perches 		case 3:
7609ac6e44eSJoe Perches 		case 5:
7619ac6e44eSJoe Perches 		case 7:
7629ac6e44eSJoe Perches 		case 9:
7639ac6e44eSJoe Perches 			*p++ = '-';
7649ac6e44eSJoe Perches 			break;
7659ac6e44eSJoe Perches 		}
7669ac6e44eSJoe Perches 	}
7679ac6e44eSJoe Perches 
7689ac6e44eSJoe Perches 	*p = 0;
7699ac6e44eSJoe Perches 
7709ac6e44eSJoe Perches 	if (uc) {
7719ac6e44eSJoe Perches 		p = uuid;
7729ac6e44eSJoe Perches 		do {
7739ac6e44eSJoe Perches 			*p = toupper(*p);
7749ac6e44eSJoe Perches 		} while (*(++p));
7759ac6e44eSJoe Perches 	}
7769ac6e44eSJoe Perches 
7779ac6e44eSJoe Perches 	return string(buf, end, uuid, spec);
7789ac6e44eSJoe Perches }
7799ac6e44eSJoe Perches 
780*c8f44affSMichał Mirosław static
781*c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr,
782*c8f44affSMichał Mirosław 		      struct printf_spec spec)
783*c8f44affSMichał Mirosław {
784*c8f44affSMichał Mirosław 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
785*c8f44affSMichał Mirosław 	if (spec.field_width == -1)
786*c8f44affSMichał Mirosław 		spec.field_width = 2 + 2 * sizeof(netdev_features_t);
787*c8f44affSMichał Mirosław 	spec.base = 16;
788*c8f44affSMichał Mirosław 
789*c8f44affSMichał Mirosław 	return number(buf, end, *(const netdev_features_t *)addr, spec);
790*c8f44affSMichał Mirosław }
791*c8f44affSMichał Mirosław 
792411f05f1SIngo Molnar int kptr_restrict __read_mostly;
793455cd5abSDan Rosenberg 
7944d8a743cSLinus Torvalds /*
7954d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
7964d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
7974d8a743cSLinus Torvalds  * specifiers.
7984d8a743cSLinus Torvalds  *
799332d2e78SLinus Torvalds  * Right now we handle:
8000fe1ef24SLinus Torvalds  *
8010c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
8020c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
8030efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
8040efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
8050f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
806c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
807c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
808dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
809dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
8108a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
811bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
812c8e00060SJoe Perches  *       with a dash-separated hex notation
8138a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
8148a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
8158a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
8168a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
8178a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
8188a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
8190159f24eSJoe Perches  * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
8208a27f7c9SJoe Perches  * - 'I6c' for IPv6 addresses printed as specified by
82129cf519eSJoe Perches  *       http://tools.ietf.org/html/rfc5952
8229ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
8239ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
8249ac6e44eSJoe Perches  *       Options for %pU are:
8259ac6e44eSJoe Perches  *         b big endian lower case hex (default)
8269ac6e44eSJoe Perches  *         B big endian UPPER case hex
8279ac6e44eSJoe Perches  *         l little endian lower case hex
8289ac6e44eSJoe Perches  *         L little endian UPPER case hex
8299ac6e44eSJoe Perches  *           big endian output byte order is:
8309ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
8319ac6e44eSJoe Perches  *           little endian output byte order is:
8329ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
8337db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
8347db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
8357db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
8367db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
8377db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
838455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
839*c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
8409ac6e44eSJoe Perches  *
841332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
842332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
843332d2e78SLinus Torvalds  * pointer to the real address.
8444d8a743cSLinus Torvalds  */
845cf3b429bSJoe Perches static noinline_for_stack
846cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
847fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
84878a8bf69SLinus Torvalds {
8499f36e2c4SKees Cook 	if (!ptr && *fmt != 'K') {
8505e057981SJoe Perches 		/*
8515e057981SJoe Perches 		 * Print (null) with the same width as a pointer so it makes
8525e057981SJoe Perches 		 * tabular output look nice.
8535e057981SJoe Perches 		 */
8545e057981SJoe Perches 		if (spec.field_width == -1)
8555e057981SJoe Perches 			spec.field_width = 2 * sizeof(void *);
856fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
8575e057981SJoe Perches 	}
858d97106abSLinus Torvalds 
8590fe1ef24SLinus Torvalds 	switch (*fmt) {
8600fe1ef24SLinus Torvalds 	case 'F':
8610c8b946eSFrederic Weisbecker 	case 'f':
8620fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
8630fe1ef24SLinus Torvalds 		/* Fallthrough */
8640fe1ef24SLinus Torvalds 	case 'S':
8659ac6e44eSJoe Perches 	case 's':
8660f77a8d3SNamhyung Kim 	case 'B':
8670c8b946eSFrederic Weisbecker 		return symbol_string(buf, end, ptr, spec, *fmt);
868332d2e78SLinus Torvalds 	case 'R':
869c7dabef8SBjorn Helgaas 	case 'r':
870fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
8718a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
8728a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
873bc7259a2SJoe Perches 					/* [mM]F (FDDI, bit reversed) */
8748a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
8758a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
8768a27f7c9SJoe Perches 					 * 4:	1.2.3.4
8778a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
8788a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
8798a27f7c9SJoe Perches 					 */
8808a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
8818a27f7c9SJoe Perches 					 * 4:	001.002.003.004
8828a27f7c9SJoe Perches 					 * 6:   000102...0f
8838a27f7c9SJoe Perches 					 */
8848a27f7c9SJoe Perches 		switch (fmt[1]) {
8858a27f7c9SJoe Perches 		case '6':
8868a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
8878a27f7c9SJoe Perches 		case '4':
8888a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
8898a27f7c9SJoe Perches 		}
8904aa99606SHarvey Harrison 		break;
8919ac6e44eSJoe Perches 	case 'U':
8929ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
8937db6f5fbSJoe Perches 	case 'V':
894d9be9b90SJan Beulich 		return buf + vsnprintf(buf, end > buf ? end - buf : 0,
8957db6f5fbSJoe Perches 				       ((struct va_format *)ptr)->fmt,
8967db6f5fbSJoe Perches 				       *(((struct va_format *)ptr)->va));
897455cd5abSDan Rosenberg 	case 'K':
898455cd5abSDan Rosenberg 		/*
899455cd5abSDan Rosenberg 		 * %pK cannot be used in IRQ context because its test
900455cd5abSDan Rosenberg 		 * for CAP_SYSLOG would be meaningless.
901455cd5abSDan Rosenberg 		 */
902455cd5abSDan Rosenberg 		if (in_irq() || in_serving_softirq() || in_nmi()) {
903455cd5abSDan Rosenberg 			if (spec.field_width == -1)
904455cd5abSDan Rosenberg 				spec.field_width = 2 * sizeof(void *);
905455cd5abSDan Rosenberg 			return string(buf, end, "pK-error", spec);
906455cd5abSDan Rosenberg 		}
90726297607SJoe Perches 		if (!((kptr_restrict == 0) ||
90826297607SJoe Perches 		      (kptr_restrict == 1 &&
90926297607SJoe Perches 		       has_capability_noaudit(current, CAP_SYSLOG))))
91026297607SJoe Perches 			ptr = NULL;
91126297607SJoe Perches 		break;
912*c8f44affSMichał Mirosław 	case 'N':
913*c8f44affSMichał Mirosław 		switch (fmt[1]) {
914*c8f44affSMichał Mirosław 		case 'F':
915*c8f44affSMichał Mirosław 			return netdev_feature_string(buf, end, ptr, spec);
916*c8f44affSMichał Mirosław 		}
917*c8f44affSMichał Mirosław 		break;
9180fe1ef24SLinus Torvalds 	}
919fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
920fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
921fef20d9cSFrederic Weisbecker 		spec.field_width = 2 * sizeof(void *);
922fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
92378a8bf69SLinus Torvalds 	}
924fef20d9cSFrederic Weisbecker 	spec.base = 16;
925fef20d9cSFrederic Weisbecker 
926fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
927fef20d9cSFrederic Weisbecker }
928fef20d9cSFrederic Weisbecker 
929fef20d9cSFrederic Weisbecker /*
930fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
931fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
932fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
933fef20d9cSFrederic Weisbecker  * to go on the next call).
934fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
935fef20d9cSFrederic Weisbecker  *
936fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
937fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
938fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
939fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
940fef20d9cSFrederic Weisbecker  *
941fef20d9cSFrederic Weisbecker  * @fmt: the format string
942fef20d9cSFrederic Weisbecker  * @type of the token returned
943fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
944fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
945fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
946fef20d9cSFrederic Weisbecker  * @precision: precision of a number
947fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
948fef20d9cSFrederic Weisbecker  */
949cf3b429bSJoe Perches static noinline_for_stack
950cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
951fef20d9cSFrederic Weisbecker {
952fef20d9cSFrederic Weisbecker 	const char *start = fmt;
953fef20d9cSFrederic Weisbecker 
954fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
955ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
956fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
957fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
958fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
959fef20d9cSFrederic Weisbecker 		}
960fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
961fef20d9cSFrederic Weisbecker 		goto precision;
962fef20d9cSFrederic Weisbecker 	}
963fef20d9cSFrederic Weisbecker 
964fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
965fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
966fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
967fef20d9cSFrederic Weisbecker 			spec->precision = 0;
968fef20d9cSFrederic Weisbecker 
969fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
970fef20d9cSFrederic Weisbecker 		goto qualifier;
971fef20d9cSFrederic Weisbecker 	}
972fef20d9cSFrederic Weisbecker 
973fef20d9cSFrederic Weisbecker 	/* By default */
974fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
975fef20d9cSFrederic Weisbecker 
976fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
977fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
978fef20d9cSFrederic Weisbecker 			break;
979fef20d9cSFrederic Weisbecker 	}
980fef20d9cSFrederic Weisbecker 
981fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
982fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
983fef20d9cSFrederic Weisbecker 		return fmt - start;
984fef20d9cSFrederic Weisbecker 
985fef20d9cSFrederic Weisbecker 	/* Process flags */
986fef20d9cSFrederic Weisbecker 	spec->flags = 0;
987fef20d9cSFrederic Weisbecker 
988fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
989fef20d9cSFrederic Weisbecker 		bool found = true;
990fef20d9cSFrederic Weisbecker 
991fef20d9cSFrederic Weisbecker 		++fmt;
992fef20d9cSFrederic Weisbecker 
993fef20d9cSFrederic Weisbecker 		switch (*fmt) {
994fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
995fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
996fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
997fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
998fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
999fef20d9cSFrederic Weisbecker 		default:  found = false;
1000fef20d9cSFrederic Weisbecker 		}
1001fef20d9cSFrederic Weisbecker 
1002fef20d9cSFrederic Weisbecker 		if (!found)
1003fef20d9cSFrederic Weisbecker 			break;
1004fef20d9cSFrederic Weisbecker 	}
1005fef20d9cSFrederic Weisbecker 
1006fef20d9cSFrederic Weisbecker 	/* get field width */
1007fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
1008fef20d9cSFrederic Weisbecker 
1009fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
1010fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
1011fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
1012fef20d9cSFrederic Weisbecker 		/* it's the next argument */
1013ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
1014fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1015fef20d9cSFrederic Weisbecker 	}
1016fef20d9cSFrederic Weisbecker 
1017fef20d9cSFrederic Weisbecker precision:
1018fef20d9cSFrederic Weisbecker 	/* get the precision */
1019fef20d9cSFrederic Weisbecker 	spec->precision = -1;
1020fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
1021fef20d9cSFrederic Weisbecker 		++fmt;
1022fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
1023fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
1024fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
1025fef20d9cSFrederic Weisbecker 				spec->precision = 0;
1026fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
1027fef20d9cSFrederic Weisbecker 			/* it's the next argument */
1028adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
1029fef20d9cSFrederic Weisbecker 			return ++fmt - start;
1030fef20d9cSFrederic Weisbecker 		}
1031fef20d9cSFrederic Weisbecker 	}
1032fef20d9cSFrederic Weisbecker 
1033fef20d9cSFrederic Weisbecker qualifier:
1034fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
1035fef20d9cSFrederic Weisbecker 	spec->qualifier = -1;
103675fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
103775fb8f26SAndy Shevchenko 	    _tolower(*fmt) == 'z' || *fmt == 't') {
1038a4e94ef0SZhaolei 		spec->qualifier = *fmt++;
1039a4e94ef0SZhaolei 		if (unlikely(spec->qualifier == *fmt)) {
1040a4e94ef0SZhaolei 			if (spec->qualifier == 'l') {
1041fef20d9cSFrederic Weisbecker 				spec->qualifier = 'L';
1042fef20d9cSFrederic Weisbecker 				++fmt;
1043a4e94ef0SZhaolei 			} else if (spec->qualifier == 'h') {
1044a4e94ef0SZhaolei 				spec->qualifier = 'H';
1045a4e94ef0SZhaolei 				++fmt;
1046a4e94ef0SZhaolei 			}
1047fef20d9cSFrederic Weisbecker 		}
1048fef20d9cSFrederic Weisbecker 	}
1049fef20d9cSFrederic Weisbecker 
1050fef20d9cSFrederic Weisbecker 	/* default base */
1051fef20d9cSFrederic Weisbecker 	spec->base = 10;
1052fef20d9cSFrederic Weisbecker 	switch (*fmt) {
1053fef20d9cSFrederic Weisbecker 	case 'c':
1054fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
1055fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1056fef20d9cSFrederic Weisbecker 
1057fef20d9cSFrederic Weisbecker 	case 's':
1058fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
1059fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1060fef20d9cSFrederic Weisbecker 
1061fef20d9cSFrederic Weisbecker 	case 'p':
1062fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
1063fef20d9cSFrederic Weisbecker 		return fmt - start;
1064fef20d9cSFrederic Weisbecker 		/* skip alnum */
1065fef20d9cSFrederic Weisbecker 
1066fef20d9cSFrederic Weisbecker 	case 'n':
1067fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NRCHARS;
1068fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1069fef20d9cSFrederic Weisbecker 
1070fef20d9cSFrederic Weisbecker 	case '%':
1071fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1072fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1073fef20d9cSFrederic Weisbecker 
1074fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1075fef20d9cSFrederic Weisbecker 	case 'o':
1076fef20d9cSFrederic Weisbecker 		spec->base = 8;
1077fef20d9cSFrederic Weisbecker 		break;
1078fef20d9cSFrederic Weisbecker 
1079fef20d9cSFrederic Weisbecker 	case 'x':
1080fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1081fef20d9cSFrederic Weisbecker 
1082fef20d9cSFrederic Weisbecker 	case 'X':
1083fef20d9cSFrederic Weisbecker 		spec->base = 16;
1084fef20d9cSFrederic Weisbecker 		break;
1085fef20d9cSFrederic Weisbecker 
1086fef20d9cSFrederic Weisbecker 	case 'd':
1087fef20d9cSFrederic Weisbecker 	case 'i':
108839e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1089fef20d9cSFrederic Weisbecker 	case 'u':
1090fef20d9cSFrederic Weisbecker 		break;
1091fef20d9cSFrederic Weisbecker 
1092fef20d9cSFrederic Weisbecker 	default:
1093fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1094fef20d9cSFrederic Weisbecker 		return fmt - start;
1095fef20d9cSFrederic Weisbecker 	}
1096fef20d9cSFrederic Weisbecker 
1097fef20d9cSFrederic Weisbecker 	if (spec->qualifier == 'L')
1098fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1099fef20d9cSFrederic Weisbecker 	else if (spec->qualifier == 'l') {
110039e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1101fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_LONG;
1102fef20d9cSFrederic Weisbecker 		else
1103fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_ULONG;
110475fb8f26SAndy Shevchenko 	} else if (_tolower(spec->qualifier) == 'z') {
1105fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1106fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 't') {
1107fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1108a4e94ef0SZhaolei 	} else if (spec->qualifier == 'H') {
1109a4e94ef0SZhaolei 		if (spec->flags & SIGN)
1110a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_BYTE;
1111a4e94ef0SZhaolei 		else
1112a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_UBYTE;
1113fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'h') {
111439e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1115fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_SHORT;
1116fef20d9cSFrederic Weisbecker 		else
1117fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_USHORT;
1118fef20d9cSFrederic Weisbecker 	} else {
111939e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1120fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_INT;
1121fef20d9cSFrederic Weisbecker 		else
1122fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_UINT;
1123fef20d9cSFrederic Weisbecker 	}
1124fef20d9cSFrederic Weisbecker 
1125fef20d9cSFrederic Weisbecker 	return ++fmt - start;
112678a8bf69SLinus Torvalds }
112778a8bf69SLinus Torvalds 
11281da177e4SLinus Torvalds /**
11291da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
11301da177e4SLinus Torvalds  * @buf: The buffer to place the result into
11311da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
11321da177e4SLinus Torvalds  * @fmt: The format string to use
11331da177e4SLinus Torvalds  * @args: Arguments for the format string
11341da177e4SLinus Torvalds  *
113520036fdcSAndi Kleen  * This function follows C99 vsnprintf, but has some extensions:
113691adcd2cSSteven Rostedt  * %pS output the name of a text symbol with offset
113791adcd2cSSteven Rostedt  * %ps output the name of a text symbol without offset
11380c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
11390c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
11400f77a8d3SNamhyung Kim  * %pB output the name of a backtrace symbol with its offset
11418a79503aSUwe Kleine-König  * %pR output the address range in a struct resource with decoded flags
11428a79503aSUwe Kleine-König  * %pr output the address range in a struct resource with raw flags
11438a79503aSUwe Kleine-König  * %pM output a 6-byte MAC address with colons
11448a79503aSUwe Kleine-König  * %pm output a 6-byte MAC address without colons
11458a79503aSUwe Kleine-König  * %pI4 print an IPv4 address without leading zeros
11468a79503aSUwe Kleine-König  * %pi4 print an IPv4 address with leading zeros
11478a79503aSUwe Kleine-König  * %pI6 print an IPv6 address with colons
11488a79503aSUwe Kleine-König  * %pi6 print an IPv6 address without colons
1149f996f208SJan Engelhardt  * %pI6c print an IPv6 address as specified by RFC 5952
11508a79503aSUwe Kleine-König  * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
11518a79503aSUwe Kleine-König  *   case.
11520efb4d20SSteven Rostedt  * %n is ignored
115320036fdcSAndi Kleen  *
11541da177e4SLinus Torvalds  * The return value is the number of characters which would
11551da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
11561da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
11571da177e4SLinus Torvalds  * number of characters written into @buf as return value
115872fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
11591da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
11601da177e4SLinus Torvalds  * string is truncated.
11611da177e4SLinus Torvalds  *
1162ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
11631da177e4SLinus Torvalds  */
11641da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
11651da177e4SLinus Torvalds {
11661da177e4SLinus Torvalds 	unsigned long long num;
1167d4be151bSAndré Goddard Rosa 	char *str, *end;
1168fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
11691da177e4SLinus Torvalds 
1170f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1171f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
11722f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
11731da177e4SLinus Torvalds 		return 0;
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	str = buf;
1176f796937aSJeremy Fitzhardinge 	end = buf + size;
11771da177e4SLinus Torvalds 
1178f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1179f796937aSJeremy Fitzhardinge 	if (end < buf) {
11801da177e4SLinus Torvalds 		end = ((void *)-1);
1181f796937aSJeremy Fitzhardinge 		size = end - buf;
11821da177e4SLinus Torvalds 	}
11831da177e4SLinus Torvalds 
1184fef20d9cSFrederic Weisbecker 	while (*fmt) {
1185fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1186d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1187fef20d9cSFrederic Weisbecker 
1188fef20d9cSFrederic Weisbecker 		fmt += read;
1189fef20d9cSFrederic Weisbecker 
1190fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1191fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1192fef20d9cSFrederic Weisbecker 			int copy = read;
1193fef20d9cSFrederic Weisbecker 			if (str < end) {
1194fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1195fef20d9cSFrederic Weisbecker 					copy = end - str;
1196fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1197fef20d9cSFrederic Weisbecker 			}
1198fef20d9cSFrederic Weisbecker 			str += read;
1199fef20d9cSFrederic Weisbecker 			break;
12001da177e4SLinus Torvalds 		}
12011da177e4SLinus Torvalds 
1202ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1203fef20d9cSFrederic Weisbecker 			spec.field_width = va_arg(args, int);
1204fef20d9cSFrederic Weisbecker 			break;
12051da177e4SLinus Torvalds 
1206fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1207fef20d9cSFrederic Weisbecker 			spec.precision = va_arg(args, int);
1208fef20d9cSFrederic Weisbecker 			break;
12091da177e4SLinus Torvalds 
1210d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1211d4be151bSAndré Goddard Rosa 			char c;
1212d4be151bSAndré Goddard Rosa 
1213fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1214fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1215f796937aSJeremy Fitzhardinge 					if (str < end)
12161da177e4SLinus Torvalds 						*str = ' ';
12171da177e4SLinus Torvalds 					++str;
1218fef20d9cSFrederic Weisbecker 
12191da177e4SLinus Torvalds 				}
12201da177e4SLinus Torvalds 			}
12211da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1222f796937aSJeremy Fitzhardinge 			if (str < end)
12231da177e4SLinus Torvalds 				*str = c;
12241da177e4SLinus Torvalds 			++str;
1225fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1226f796937aSJeremy Fitzhardinge 				if (str < end)
12271da177e4SLinus Torvalds 					*str = ' ';
12281da177e4SLinus Torvalds 				++str;
12291da177e4SLinus Torvalds 			}
1230fef20d9cSFrederic Weisbecker 			break;
1231d4be151bSAndré Goddard Rosa 		}
12321da177e4SLinus Torvalds 
1233fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1234fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1235fef20d9cSFrederic Weisbecker 			break;
12361da177e4SLinus Torvalds 
1237fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1238fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, va_arg(args, void *),
1239fef20d9cSFrederic Weisbecker 				      spec);
1240fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
12414d8a743cSLinus Torvalds 				fmt++;
1242fef20d9cSFrederic Weisbecker 			break;
12431da177e4SLinus Torvalds 
1244fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1245f796937aSJeremy Fitzhardinge 			if (str < end)
12461da177e4SLinus Torvalds 				*str = '%';
12471da177e4SLinus Torvalds 			++str;
12481da177e4SLinus Torvalds 			break;
12491da177e4SLinus Torvalds 
1250fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1251f796937aSJeremy Fitzhardinge 			if (str < end)
12521da177e4SLinus Torvalds 				*str = '%';
12531da177e4SLinus Torvalds 			++str;
1254fef20d9cSFrederic Weisbecker 			break;
1255fef20d9cSFrederic Weisbecker 
1256fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
1257ef0658f3SJoe Perches 			u8 qualifier = spec.qualifier;
1258fef20d9cSFrederic Weisbecker 
1259fef20d9cSFrederic Weisbecker 			if (qualifier == 'l') {
1260fef20d9cSFrederic Weisbecker 				long *ip = va_arg(args, long *);
1261fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
126275fb8f26SAndy Shevchenko 			} else if (_tolower(qualifier) == 'z') {
1263fef20d9cSFrederic Weisbecker 				size_t *ip = va_arg(args, size_t *);
1264fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
12651da177e4SLinus Torvalds 			} else {
1266fef20d9cSFrederic Weisbecker 				int *ip = va_arg(args, int *);
1267fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
1268fef20d9cSFrederic Weisbecker 			}
1269fef20d9cSFrederic Weisbecker 			break;
1270fef20d9cSFrederic Weisbecker 		}
1271fef20d9cSFrederic Weisbecker 
1272fef20d9cSFrederic Weisbecker 		default:
1273fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1274fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1275fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1276fef20d9cSFrederic Weisbecker 				break;
1277fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1278fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
1279fef20d9cSFrederic Weisbecker 				break;
1280fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1281fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
1282fef20d9cSFrederic Weisbecker 				break;
1283fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1284fef20d9cSFrederic Weisbecker 				num = va_arg(args, size_t);
1285fef20d9cSFrederic Weisbecker 				break;
1286fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1287fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
1288fef20d9cSFrederic Weisbecker 				break;
1289a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1290a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
1291a4e94ef0SZhaolei 				break;
1292a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1293a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
1294a4e94ef0SZhaolei 				break;
1295fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1296fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
1297fef20d9cSFrederic Weisbecker 				break;
1298fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1299fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
1300fef20d9cSFrederic Weisbecker 				break;
130139e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
130239e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
1303fef20d9cSFrederic Weisbecker 				break;
1304fef20d9cSFrederic Weisbecker 			default:
1305fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
13061da177e4SLinus Torvalds 			}
1307fef20d9cSFrederic Weisbecker 
1308fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
13091da177e4SLinus Torvalds 		}
1310fef20d9cSFrederic Weisbecker 	}
1311fef20d9cSFrederic Weisbecker 
1312f796937aSJeremy Fitzhardinge 	if (size > 0) {
1313f796937aSJeremy Fitzhardinge 		if (str < end)
13141da177e4SLinus Torvalds 			*str = '\0';
1315f796937aSJeremy Fitzhardinge 		else
13160a6047eeSLinus Torvalds 			end[-1] = '\0';
1317f796937aSJeremy Fitzhardinge 	}
1318fef20d9cSFrederic Weisbecker 
1319f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
13201da177e4SLinus Torvalds 	return str-buf;
1321fef20d9cSFrederic Weisbecker 
13221da177e4SLinus Torvalds }
13231da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds /**
13261da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
13271da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13281da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
13291da177e4SLinus Torvalds  * @fmt: The format string to use
13301da177e4SLinus Torvalds  * @args: Arguments for the format string
13311da177e4SLinus Torvalds  *
13321da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
1333b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
13341da177e4SLinus Torvalds  * returns 0.
13351da177e4SLinus Torvalds  *
1336ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
133720036fdcSAndi Kleen  *
133820036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
13391da177e4SLinus Torvalds  */
13401da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
13411da177e4SLinus Torvalds {
13421da177e4SLinus Torvalds 	int i;
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
13457b9186f5SAndré Goddard Rosa 
1346b921c69fSAnton Arapov 	if (likely(i < size))
1347b921c69fSAnton Arapov 		return i;
1348b921c69fSAnton Arapov 	if (size != 0)
1349b921c69fSAnton Arapov 		return size - 1;
1350b921c69fSAnton Arapov 	return 0;
13511da177e4SLinus Torvalds }
13521da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds /**
13551da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
13561da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13571da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
13581da177e4SLinus Torvalds  * @fmt: The format string to use
13591da177e4SLinus Torvalds  * @...: Arguments for the format string
13601da177e4SLinus Torvalds  *
13611da177e4SLinus Torvalds  * The return value is the number of characters which would be
13621da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
13631da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
13641da177e4SLinus Torvalds  * @size, the resulting string is truncated.
136520036fdcSAndi Kleen  *
136620036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
13671da177e4SLinus Torvalds  */
13681da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
13691da177e4SLinus Torvalds {
13701da177e4SLinus Torvalds 	va_list args;
13711da177e4SLinus Torvalds 	int i;
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 	va_start(args, fmt);
13741da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
13751da177e4SLinus Torvalds 	va_end(args);
13767b9186f5SAndré Goddard Rosa 
13771da177e4SLinus Torvalds 	return i;
13781da177e4SLinus Torvalds }
13791da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds /**
13821da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
13831da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13841da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
13851da177e4SLinus Torvalds  * @fmt: The format string to use
13861da177e4SLinus Torvalds  * @...: Arguments for the format string
13871da177e4SLinus Torvalds  *
13881da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
1389b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
13901da177e4SLinus Torvalds  */
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
13931da177e4SLinus Torvalds {
13941da177e4SLinus Torvalds 	va_list args;
13951da177e4SLinus Torvalds 	int i;
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 	va_start(args, fmt);
1398b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
13991da177e4SLinus Torvalds 	va_end(args);
14007b9186f5SAndré Goddard Rosa 
1401b903c0b8SChangli Gao 	return i;
14021da177e4SLinus Torvalds }
14031da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds /**
14061da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
14071da177e4SLinus Torvalds  * @buf: The buffer to place the result into
14081da177e4SLinus Torvalds  * @fmt: The format string to use
14091da177e4SLinus Torvalds  * @args: Arguments for the format string
14101da177e4SLinus Torvalds  *
14111da177e4SLinus Torvalds  * The function returns the number of characters written
141272fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
14131da177e4SLinus Torvalds  * buffer overflows.
14141da177e4SLinus Torvalds  *
1415ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
141620036fdcSAndi Kleen  *
141720036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
14181da177e4SLinus Torvalds  */
14191da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
14201da177e4SLinus Torvalds {
14211da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
14221da177e4SLinus Torvalds }
14231da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds /**
14261da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
14271da177e4SLinus Torvalds  * @buf: The buffer to place the result into
14281da177e4SLinus Torvalds  * @fmt: The format string to use
14291da177e4SLinus Torvalds  * @...: Arguments for the format string
14301da177e4SLinus Torvalds  *
14311da177e4SLinus Torvalds  * The function returns the number of characters written
143272fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
14331da177e4SLinus Torvalds  * buffer overflows.
143420036fdcSAndi Kleen  *
143520036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
14361da177e4SLinus Torvalds  */
14371da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
14381da177e4SLinus Torvalds {
14391da177e4SLinus Torvalds 	va_list args;
14401da177e4SLinus Torvalds 	int i;
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 	va_start(args, fmt);
14431da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
14441da177e4SLinus Torvalds 	va_end(args);
14457b9186f5SAndré Goddard Rosa 
14461da177e4SLinus Torvalds 	return i;
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
14491da177e4SLinus Torvalds 
14504370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
14514370aa4aSLai Jiangshan /*
14524370aa4aSLai Jiangshan  * bprintf service:
14534370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
14544370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
14554370aa4aSLai Jiangshan  */
14564370aa4aSLai Jiangshan 
14574370aa4aSLai Jiangshan /**
14584370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
14594370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
14604370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
14614370aa4aSLai Jiangshan  * @fmt: The format string to use
14624370aa4aSLai Jiangshan  * @args: Arguments for the format string
14634370aa4aSLai Jiangshan  *
14644370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
14654370aa4aSLai Jiangshan  * is skiped.
14664370aa4aSLai Jiangshan  *
14674370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
14684370aa4aSLai Jiangshan  * the given input.
14694370aa4aSLai Jiangshan  *
14704370aa4aSLai Jiangshan  * NOTE:
14714370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
14724370aa4aSLai Jiangshan  * valid for bstr_printf().
14734370aa4aSLai Jiangshan  */
14744370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
14754370aa4aSLai Jiangshan {
1476fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
14774370aa4aSLai Jiangshan 	char *str, *end;
14784370aa4aSLai Jiangshan 
14794370aa4aSLai Jiangshan 	str = (char *)bin_buf;
14804370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
14814370aa4aSLai Jiangshan 
14824370aa4aSLai Jiangshan #define save_arg(type)							\
14834370aa4aSLai Jiangshan do {									\
14844370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
14854370aa4aSLai Jiangshan 		unsigned long long value;				\
14864370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
14874370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
14884370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
14894370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
14904370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
14914370aa4aSLai Jiangshan 		}							\
14924370aa4aSLai Jiangshan 	} else {							\
14934370aa4aSLai Jiangshan 		unsigned long value;					\
14944370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
14954370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
14964370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
14974370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
14984370aa4aSLai Jiangshan 	}								\
14994370aa4aSLai Jiangshan 	str += sizeof(type);						\
15004370aa4aSLai Jiangshan } while (0)
15014370aa4aSLai Jiangshan 
1502fef20d9cSFrederic Weisbecker 	while (*fmt) {
1503d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
15044370aa4aSLai Jiangshan 
1505fef20d9cSFrederic Weisbecker 		fmt += read;
1506fef20d9cSFrederic Weisbecker 
1507fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1508fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
1509d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_INVALID:
1510d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
1511fef20d9cSFrederic Weisbecker 			break;
1512fef20d9cSFrederic Weisbecker 
1513ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1514fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
15154370aa4aSLai Jiangshan 			save_arg(int);
1516fef20d9cSFrederic Weisbecker 			break;
15174370aa4aSLai Jiangshan 
1518fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
15194370aa4aSLai Jiangshan 			save_arg(char);
1520fef20d9cSFrederic Weisbecker 			break;
1521fef20d9cSFrederic Weisbecker 
1522fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
15234370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
15244370aa4aSLai Jiangshan 			size_t len;
15256c356634SAndré Goddard Rosa 
15264370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
15274370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
15280f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
15296c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
15306c356634SAndré Goddard Rosa 			if (str + len < end)
15316c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
15326c356634SAndré Goddard Rosa 			str += len;
1533fef20d9cSFrederic Weisbecker 			break;
15344370aa4aSLai Jiangshan 		}
1535fef20d9cSFrederic Weisbecker 
1536fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
15374370aa4aSLai Jiangshan 			save_arg(void *);
15384370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
1539fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
15404370aa4aSLai Jiangshan 				fmt++;
1541fef20d9cSFrederic Weisbecker 			break;
1542fef20d9cSFrederic Weisbecker 
1543fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
15444370aa4aSLai Jiangshan 			/* skip %n 's argument */
1545ef0658f3SJoe Perches 			u8 qualifier = spec.qualifier;
15464370aa4aSLai Jiangshan 			void *skip_arg;
15474370aa4aSLai Jiangshan 			if (qualifier == 'l')
15484370aa4aSLai Jiangshan 				skip_arg = va_arg(args, long *);
154975fb8f26SAndy Shevchenko 			else if (_tolower(qualifier) == 'z')
15504370aa4aSLai Jiangshan 				skip_arg = va_arg(args, size_t *);
15514370aa4aSLai Jiangshan 			else
15524370aa4aSLai Jiangshan 				skip_arg = va_arg(args, int *);
1553fef20d9cSFrederic Weisbecker 			break;
15544370aa4aSLai Jiangshan 		}
15554370aa4aSLai Jiangshan 
1556fef20d9cSFrederic Weisbecker 		default:
1557fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1558fef20d9cSFrederic Weisbecker 
1559fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1560fef20d9cSFrederic Weisbecker 				save_arg(long long);
1561fef20d9cSFrederic Weisbecker 				break;
1562fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1563fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1564fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
1565fef20d9cSFrederic Weisbecker 				break;
1566fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1567fef20d9cSFrederic Weisbecker 				save_arg(size_t);
1568fef20d9cSFrederic Weisbecker 				break;
1569fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1570fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
1571fef20d9cSFrederic Weisbecker 				break;
1572a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1573a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1574a4e94ef0SZhaolei 				save_arg(char);
1575a4e94ef0SZhaolei 				break;
1576fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1577fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1578fef20d9cSFrederic Weisbecker 				save_arg(short);
1579fef20d9cSFrederic Weisbecker 				break;
1580fef20d9cSFrederic Weisbecker 			default:
1581fef20d9cSFrederic Weisbecker 				save_arg(int);
1582fef20d9cSFrederic Weisbecker 			}
1583fef20d9cSFrederic Weisbecker 		}
1584fef20d9cSFrederic Weisbecker 	}
1585fef20d9cSFrederic Weisbecker 
15867b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1587fef20d9cSFrederic Weisbecker #undef save_arg
15884370aa4aSLai Jiangshan }
15894370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
15904370aa4aSLai Jiangshan 
15914370aa4aSLai Jiangshan /**
15924370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
15934370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
15944370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
15954370aa4aSLai Jiangshan  * @fmt: The format string to use
15964370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
15974370aa4aSLai Jiangshan  *
15984370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
15994370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
16004370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
16014370aa4aSLai Jiangshan  *
16024370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
16030efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
16044370aa4aSLai Jiangshan  *
16054370aa4aSLai Jiangshan  * The return value is the number of characters which would
16064370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
16074370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
16084370aa4aSLai Jiangshan  * number of characters written into @buf as return value
16094370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
16104370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
16114370aa4aSLai Jiangshan  * string is truncated.
16124370aa4aSLai Jiangshan  */
16134370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
16144370aa4aSLai Jiangshan {
1615fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
1616d4be151bSAndré Goddard Rosa 	char *str, *end;
1617d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
16184370aa4aSLai Jiangshan 
16192f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
16204370aa4aSLai Jiangshan 		return 0;
16214370aa4aSLai Jiangshan 
16224370aa4aSLai Jiangshan 	str = buf;
16234370aa4aSLai Jiangshan 	end = buf + size;
16244370aa4aSLai Jiangshan 
16254370aa4aSLai Jiangshan #define get_arg(type)							\
16264370aa4aSLai Jiangshan ({									\
16274370aa4aSLai Jiangshan 	typeof(type) value;						\
16284370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
16294370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
16304370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
16314370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
16324370aa4aSLai Jiangshan 	} else {							\
16334370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
16344370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
16354370aa4aSLai Jiangshan 	}								\
16364370aa4aSLai Jiangshan 	args += sizeof(type);						\
16374370aa4aSLai Jiangshan 	value;								\
16384370aa4aSLai Jiangshan })
16394370aa4aSLai Jiangshan 
16404370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
16414370aa4aSLai Jiangshan 	if (end < buf) {
16424370aa4aSLai Jiangshan 		end = ((void *)-1);
16434370aa4aSLai Jiangshan 		size = end - buf;
16444370aa4aSLai Jiangshan 	}
16454370aa4aSLai Jiangshan 
1646fef20d9cSFrederic Weisbecker 	while (*fmt) {
1647fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1648d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1649fef20d9cSFrederic Weisbecker 
1650fef20d9cSFrederic Weisbecker 		fmt += read;
1651fef20d9cSFrederic Weisbecker 
1652fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1653fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1654fef20d9cSFrederic Weisbecker 			int copy = read;
1655fef20d9cSFrederic Weisbecker 			if (str < end) {
1656fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1657fef20d9cSFrederic Weisbecker 					copy = end - str;
1658fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1659fef20d9cSFrederic Weisbecker 			}
1660fef20d9cSFrederic Weisbecker 			str += read;
1661fef20d9cSFrederic Weisbecker 			break;
16624370aa4aSLai Jiangshan 		}
16634370aa4aSLai Jiangshan 
1664ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1665fef20d9cSFrederic Weisbecker 			spec.field_width = get_arg(int);
1666fef20d9cSFrederic Weisbecker 			break;
16674370aa4aSLai Jiangshan 
1668fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1669fef20d9cSFrederic Weisbecker 			spec.precision = get_arg(int);
1670fef20d9cSFrederic Weisbecker 			break;
16714370aa4aSLai Jiangshan 
1672d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1673d4be151bSAndré Goddard Rosa 			char c;
1674d4be151bSAndré Goddard Rosa 
1675fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1676fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
16774370aa4aSLai Jiangshan 					if (str < end)
16784370aa4aSLai Jiangshan 						*str = ' ';
16794370aa4aSLai Jiangshan 					++str;
16804370aa4aSLai Jiangshan 				}
16814370aa4aSLai Jiangshan 			}
16824370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
16834370aa4aSLai Jiangshan 			if (str < end)
16844370aa4aSLai Jiangshan 				*str = c;
16854370aa4aSLai Jiangshan 			++str;
1686fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
16874370aa4aSLai Jiangshan 				if (str < end)
16884370aa4aSLai Jiangshan 					*str = ' ';
16894370aa4aSLai Jiangshan 				++str;
16904370aa4aSLai Jiangshan 			}
1691fef20d9cSFrederic Weisbecker 			break;
1692d4be151bSAndré Goddard Rosa 		}
16934370aa4aSLai Jiangshan 
1694fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
16954370aa4aSLai Jiangshan 			const char *str_arg = args;
1696d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
1697fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
1698fef20d9cSFrederic Weisbecker 			break;
16994370aa4aSLai Jiangshan 		}
17004370aa4aSLai Jiangshan 
1701fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1702fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, get_arg(void *), spec);
1703fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
17044370aa4aSLai Jiangshan 				fmt++;
1705fef20d9cSFrederic Weisbecker 			break;
17064370aa4aSLai Jiangshan 
1707fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1708fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
17094370aa4aSLai Jiangshan 			if (str < end)
17104370aa4aSLai Jiangshan 				*str = '%';
17114370aa4aSLai Jiangshan 			++str;
1712fef20d9cSFrederic Weisbecker 			break;
1713fef20d9cSFrederic Weisbecker 
1714fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS:
1715fef20d9cSFrederic Weisbecker 			/* skip */
1716fef20d9cSFrederic Weisbecker 			break;
1717fef20d9cSFrederic Weisbecker 
1718d4be151bSAndré Goddard Rosa 		default: {
1719d4be151bSAndré Goddard Rosa 			unsigned long long num;
1720d4be151bSAndré Goddard Rosa 
1721fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1722fef20d9cSFrederic Weisbecker 
1723fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
17244370aa4aSLai Jiangshan 				num = get_arg(long long);
1725fef20d9cSFrederic Weisbecker 				break;
1726fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1727fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1728fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
1729fef20d9cSFrederic Weisbecker 				break;
1730fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
17314370aa4aSLai Jiangshan 				num = get_arg(size_t);
1732fef20d9cSFrederic Weisbecker 				break;
1733fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
17344370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
1735fef20d9cSFrederic Weisbecker 				break;
1736a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1737a4e94ef0SZhaolei 				num = get_arg(unsigned char);
1738a4e94ef0SZhaolei 				break;
1739a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1740a4e94ef0SZhaolei 				num = get_arg(signed char);
1741a4e94ef0SZhaolei 				break;
1742fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1743fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
1744fef20d9cSFrederic Weisbecker 				break;
1745fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1746fef20d9cSFrederic Weisbecker 				num = get_arg(short);
1747fef20d9cSFrederic Weisbecker 				break;
1748fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
17494370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
1750fef20d9cSFrederic Weisbecker 				break;
1751fef20d9cSFrederic Weisbecker 			default:
1752fef20d9cSFrederic Weisbecker 				num = get_arg(int);
17534370aa4aSLai Jiangshan 			}
1754fef20d9cSFrederic Weisbecker 
1755fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
1756d4be151bSAndré Goddard Rosa 		} /* default: */
1757d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
1758d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
1759fef20d9cSFrederic Weisbecker 
17604370aa4aSLai Jiangshan 	if (size > 0) {
17614370aa4aSLai Jiangshan 		if (str < end)
17624370aa4aSLai Jiangshan 			*str = '\0';
17634370aa4aSLai Jiangshan 		else
17644370aa4aSLai Jiangshan 			end[-1] = '\0';
17654370aa4aSLai Jiangshan 	}
1766fef20d9cSFrederic Weisbecker 
17674370aa4aSLai Jiangshan #undef get_arg
17684370aa4aSLai Jiangshan 
17694370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
17704370aa4aSLai Jiangshan 	return str - buf;
17714370aa4aSLai Jiangshan }
17724370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
17734370aa4aSLai Jiangshan 
17744370aa4aSLai Jiangshan /**
17754370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
17764370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
17774370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
17784370aa4aSLai Jiangshan  * @fmt: The format string to use
17794370aa4aSLai Jiangshan  * @...: Arguments for the format string
17804370aa4aSLai Jiangshan  *
17814370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
17824370aa4aSLai Jiangshan  * into @bin_buf.
17834370aa4aSLai Jiangshan  */
17844370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
17854370aa4aSLai Jiangshan {
17864370aa4aSLai Jiangshan 	va_list args;
17874370aa4aSLai Jiangshan 	int ret;
17884370aa4aSLai Jiangshan 
17894370aa4aSLai Jiangshan 	va_start(args, fmt);
17904370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
17914370aa4aSLai Jiangshan 	va_end(args);
17927b9186f5SAndré Goddard Rosa 
17934370aa4aSLai Jiangshan 	return ret;
17944370aa4aSLai Jiangshan }
17954370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
17964370aa4aSLai Jiangshan 
17974370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
17984370aa4aSLai Jiangshan 
17991da177e4SLinus Torvalds /**
18001da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
18011da177e4SLinus Torvalds  * @buf:	input buffer
18021da177e4SLinus Torvalds  * @fmt:	format of buffer
18031da177e4SLinus Torvalds  * @args:	arguments
18041da177e4SLinus Torvalds  */
18051da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
18061da177e4SLinus Torvalds {
18071da177e4SLinus Torvalds 	const char *str = buf;
18081da177e4SLinus Torvalds 	char *next;
18091da177e4SLinus Torvalds 	char digit;
18101da177e4SLinus Torvalds 	int num = 0;
1811ef0658f3SJoe Perches 	u8 qualifier;
1812ef0658f3SJoe Perches 	u8 base;
1813ef0658f3SJoe Perches 	s16 field_width;
1814d4be151bSAndré Goddard Rosa 	bool is_sign;
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds 	while (*fmt && *str) {
18171da177e4SLinus Torvalds 		/* skip any white space in format */
18181da177e4SLinus Torvalds 		/* white space in format matchs any amount of
18191da177e4SLinus Torvalds 		 * white space, including none, in the input.
18201da177e4SLinus Torvalds 		 */
18211da177e4SLinus Torvalds 		if (isspace(*fmt)) {
1822e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
1823e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
18241da177e4SLinus Torvalds 		}
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
18271da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
18281da177e4SLinus Torvalds 			if (*fmt++ != *str++)
18291da177e4SLinus Torvalds 				break;
18301da177e4SLinus Torvalds 			continue;
18311da177e4SLinus Torvalds 		}
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 		if (!*fmt)
18341da177e4SLinus Torvalds 			break;
18351da177e4SLinus Torvalds 		++fmt;
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 		/* skip this conversion.
18381da177e4SLinus Torvalds 		 * advance both strings to next white space
18391da177e4SLinus Torvalds 		 */
18401da177e4SLinus Torvalds 		if (*fmt == '*') {
18418fccae2cSAndy Spencer 			while (!isspace(*fmt) && *fmt != '%' && *fmt)
18421da177e4SLinus Torvalds 				fmt++;
18431da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
18441da177e4SLinus Torvalds 				str++;
18451da177e4SLinus Torvalds 			continue;
18461da177e4SLinus Torvalds 		}
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds 		/* get field width */
18491da177e4SLinus Torvalds 		field_width = -1;
18501da177e4SLinus Torvalds 		if (isdigit(*fmt))
18511da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 		/* get conversion qualifier */
18541da177e4SLinus Torvalds 		qualifier = -1;
185575fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
185675fb8f26SAndy Shevchenko 		    _tolower(*fmt) == 'z') {
18571da177e4SLinus Torvalds 			qualifier = *fmt++;
18581da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
18591da177e4SLinus Torvalds 				if (qualifier == 'h') {
18601da177e4SLinus Torvalds 					qualifier = 'H';
18611da177e4SLinus Torvalds 					fmt++;
18621da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
18631da177e4SLinus Torvalds 					qualifier = 'L';
18641da177e4SLinus Torvalds 					fmt++;
18651da177e4SLinus Torvalds 				}
18661da177e4SLinus Torvalds 			}
18671da177e4SLinus Torvalds 		}
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 		if (!*fmt || !*str)
18701da177e4SLinus Torvalds 			break;
18711da177e4SLinus Torvalds 
1872d4be151bSAndré Goddard Rosa 		base = 10;
1873d4be151bSAndré Goddard Rosa 		is_sign = 0;
1874d4be151bSAndré Goddard Rosa 
18751da177e4SLinus Torvalds 		switch (*fmt++) {
18761da177e4SLinus Torvalds 		case 'c':
18771da177e4SLinus Torvalds 		{
18781da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
18791da177e4SLinus Torvalds 			if (field_width == -1)
18801da177e4SLinus Torvalds 				field_width = 1;
18811da177e4SLinus Torvalds 			do {
18821da177e4SLinus Torvalds 				*s++ = *str++;
18831da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
18841da177e4SLinus Torvalds 			num++;
18851da177e4SLinus Torvalds 		}
18861da177e4SLinus Torvalds 		continue;
18871da177e4SLinus Torvalds 		case 's':
18881da177e4SLinus Torvalds 		{
18891da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
18901da177e4SLinus Torvalds 			if (field_width == -1)
18914be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
18921da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
1893e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 			/* now copy until next white space */
18967b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
18971da177e4SLinus Torvalds 				*s++ = *str++;
18981da177e4SLinus Torvalds 			*s = '\0';
18991da177e4SLinus Torvalds 			num++;
19001da177e4SLinus Torvalds 		}
19011da177e4SLinus Torvalds 		continue;
19021da177e4SLinus Torvalds 		case 'n':
19031da177e4SLinus Torvalds 			/* return number of characters read so far */
19041da177e4SLinus Torvalds 		{
19051da177e4SLinus Torvalds 			int *i = (int *)va_arg(args, int*);
19061da177e4SLinus Torvalds 			*i = str - buf;
19071da177e4SLinus Torvalds 		}
19081da177e4SLinus Torvalds 		continue;
19091da177e4SLinus Torvalds 		case 'o':
19101da177e4SLinus Torvalds 			base = 8;
19111da177e4SLinus Torvalds 			break;
19121da177e4SLinus Torvalds 		case 'x':
19131da177e4SLinus Torvalds 		case 'X':
19141da177e4SLinus Torvalds 			base = 16;
19151da177e4SLinus Torvalds 			break;
19161da177e4SLinus Torvalds 		case 'i':
19171da177e4SLinus Torvalds 			base = 0;
19181da177e4SLinus Torvalds 		case 'd':
19191da177e4SLinus Torvalds 			is_sign = 1;
19201da177e4SLinus Torvalds 		case 'u':
19211da177e4SLinus Torvalds 			break;
19221da177e4SLinus Torvalds 		case '%':
19231da177e4SLinus Torvalds 			/* looking for '%' in str */
19241da177e4SLinus Torvalds 			if (*str++ != '%')
19251da177e4SLinus Torvalds 				return num;
19261da177e4SLinus Torvalds 			continue;
19271da177e4SLinus Torvalds 		default:
19281da177e4SLinus Torvalds 			/* invalid format; stop here */
19291da177e4SLinus Torvalds 			return num;
19301da177e4SLinus Torvalds 		}
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 		/* have some sort of integer conversion.
19331da177e4SLinus Torvalds 		 * first, skip white space in buffer.
19341da177e4SLinus Torvalds 		 */
1935e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds 		digit = *str;
19381da177e4SLinus Torvalds 		if (is_sign && digit == '-')
19391da177e4SLinus Torvalds 			digit = *(str + 1);
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds 		if (!digit
19421da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
19431da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
19441da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
19451da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
19461da177e4SLinus Torvalds 			break;
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 		switch (qualifier) {
19491da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
19501da177e4SLinus Torvalds 			if (is_sign) {
19511da177e4SLinus Torvalds 				signed char *s = (signed char *)va_arg(args, signed char *);
19521da177e4SLinus Torvalds 				*s = (signed char)simple_strtol(str, &next, base);
19531da177e4SLinus Torvalds 			} else {
19541da177e4SLinus Torvalds 				unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
19551da177e4SLinus Torvalds 				*s = (unsigned char)simple_strtoul(str, &next, base);
19561da177e4SLinus Torvalds 			}
19571da177e4SLinus Torvalds 			break;
19581da177e4SLinus Torvalds 		case 'h':
19591da177e4SLinus Torvalds 			if (is_sign) {
19601da177e4SLinus Torvalds 				short *s = (short *)va_arg(args, short *);
19611da177e4SLinus Torvalds 				*s = (short)simple_strtol(str, &next, base);
19621da177e4SLinus Torvalds 			} else {
19631da177e4SLinus Torvalds 				unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
19641da177e4SLinus Torvalds 				*s = (unsigned short)simple_strtoul(str, &next, base);
19651da177e4SLinus Torvalds 			}
19661da177e4SLinus Torvalds 			break;
19671da177e4SLinus Torvalds 		case 'l':
19681da177e4SLinus Torvalds 			if (is_sign) {
19691da177e4SLinus Torvalds 				long *l = (long *)va_arg(args, long *);
19701da177e4SLinus Torvalds 				*l = simple_strtol(str, &next, base);
19711da177e4SLinus Torvalds 			} else {
19721da177e4SLinus Torvalds 				unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
19731da177e4SLinus Torvalds 				*l = simple_strtoul(str, &next, base);
19741da177e4SLinus Torvalds 			}
19751da177e4SLinus Torvalds 			break;
19761da177e4SLinus Torvalds 		case 'L':
19771da177e4SLinus Torvalds 			if (is_sign) {
19781da177e4SLinus Torvalds 				long long *l = (long long *)va_arg(args, long long *);
19791da177e4SLinus Torvalds 				*l = simple_strtoll(str, &next, base);
19801da177e4SLinus Torvalds 			} else {
19811da177e4SLinus Torvalds 				unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
19821da177e4SLinus Torvalds 				*l = simple_strtoull(str, &next, base);
19831da177e4SLinus Torvalds 			}
19841da177e4SLinus Torvalds 			break;
19851da177e4SLinus Torvalds 		case 'Z':
19861da177e4SLinus Torvalds 		case 'z':
19871da177e4SLinus Torvalds 		{
19881da177e4SLinus Torvalds 			size_t *s = (size_t *)va_arg(args, size_t *);
19891da177e4SLinus Torvalds 			*s = (size_t)simple_strtoul(str, &next, base);
19901da177e4SLinus Torvalds 		}
19911da177e4SLinus Torvalds 		break;
19921da177e4SLinus Torvalds 		default:
19931da177e4SLinus Torvalds 			if (is_sign) {
19941da177e4SLinus Torvalds 				int *i = (int *)va_arg(args, int *);
19951da177e4SLinus Torvalds 				*i = (int)simple_strtol(str, &next, base);
19961da177e4SLinus Torvalds 			} else {
19971da177e4SLinus Torvalds 				unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
19981da177e4SLinus Torvalds 				*i = (unsigned int)simple_strtoul(str, &next, base);
19991da177e4SLinus Torvalds 			}
20001da177e4SLinus Torvalds 			break;
20011da177e4SLinus Torvalds 		}
20021da177e4SLinus Torvalds 		num++;
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds 		if (!next)
20051da177e4SLinus Torvalds 			break;
20061da177e4SLinus Torvalds 		str = next;
20071da177e4SLinus Torvalds 	}
2008c6b40d16SJohannes Berg 
2009c6b40d16SJohannes Berg 	/*
2010c6b40d16SJohannes Berg 	 * Now we've come all the way through so either the input string or the
2011c6b40d16SJohannes Berg 	 * format ended. In the former case, there can be a %n at the current
2012c6b40d16SJohannes Berg 	 * position in the format that needs to be filled.
2013c6b40d16SJohannes Berg 	 */
2014c6b40d16SJohannes Berg 	if (*fmt == '%' && *(fmt + 1) == 'n') {
2015c6b40d16SJohannes Berg 		int *p = (int *)va_arg(args, int *);
2016c6b40d16SJohannes Berg 		*p = str - buf;
2017c6b40d16SJohannes Berg 	}
2018c6b40d16SJohannes Berg 
20191da177e4SLinus Torvalds 	return num;
20201da177e4SLinus Torvalds }
20211da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds /**
20241da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
20251da177e4SLinus Torvalds  * @buf:	input buffer
20261da177e4SLinus Torvalds  * @fmt:	formatting of buffer
20271da177e4SLinus Torvalds  * @...:	resulting arguments
20281da177e4SLinus Torvalds  */
20291da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
20301da177e4SLinus Torvalds {
20311da177e4SLinus Torvalds 	va_list args;
20321da177e4SLinus Torvalds 	int i;
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 	va_start(args, fmt);
20351da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
20361da177e4SLinus Torvalds 	va_end(args);
20377b9186f5SAndré Goddard Rosa 
20381da177e4SLinus Torvalds 	return i;
20391da177e4SLinus Torvalds }
20401da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
2041