xref: /openbmc/linux/lib/vsprintf.c (revision e7d2860b690d4f3bed6824757c540579638e3d1e)
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 
349b706aeeSDenys Vlasenko /* Works only for digits and letters, but small and fast */
359b706aeeSDenys Vlasenko #define TOLOWER(x) ((x) | 0x20)
369b706aeeSDenys Vlasenko 
37aa46a63eSHarvey Harrison static unsigned int simple_guess_base(const char *cp)
38aa46a63eSHarvey Harrison {
39aa46a63eSHarvey Harrison 	if (cp[0] == '0') {
40aa46a63eSHarvey Harrison 		if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
41aa46a63eSHarvey Harrison 			return 16;
42aa46a63eSHarvey Harrison 		else
43aa46a63eSHarvey Harrison 			return 8;
44aa46a63eSHarvey Harrison 	} else {
45aa46a63eSHarvey Harrison 		return 10;
46aa46a63eSHarvey Harrison 	}
47aa46a63eSHarvey Harrison }
48aa46a63eSHarvey Harrison 
491da177e4SLinus Torvalds /**
501da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
511da177e4SLinus Torvalds  * @cp: The start of the string
521da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
531da177e4SLinus Torvalds  * @base: The number base to use
541da177e4SLinus Torvalds  */
551da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
561da177e4SLinus Torvalds {
57aa46a63eSHarvey Harrison 	unsigned long long result = 0;
581da177e4SLinus Torvalds 
59aa46a63eSHarvey Harrison 	if (!base)
60aa46a63eSHarvey Harrison 		base = simple_guess_base(cp);
61aa46a63eSHarvey Harrison 
62aa46a63eSHarvey Harrison 	if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
631da177e4SLinus Torvalds 		cp += 2;
64aa46a63eSHarvey Harrison 
65aa46a63eSHarvey Harrison 	while (isxdigit(*cp)) {
66aa46a63eSHarvey Harrison 		unsigned int value;
67aa46a63eSHarvey Harrison 
68aa46a63eSHarvey Harrison 		value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
69aa46a63eSHarvey Harrison 		if (value >= base)
70aa46a63eSHarvey Harrison 			break;
711da177e4SLinus Torvalds 		result = result * base + value;
721da177e4SLinus Torvalds 		cp++;
731da177e4SLinus Torvalds 	}
741da177e4SLinus Torvalds 	if (endp)
751da177e4SLinus Torvalds 		*endp = (char *)cp;
767b9186f5SAndré Goddard Rosa 
771da177e4SLinus Torvalds 	return result;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /**
82922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
83922ac25cSAndré Goddard Rosa  * @cp: The start of the string
84922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
85922ac25cSAndré Goddard Rosa  * @base: The number base to use
86922ac25cSAndré Goddard Rosa  */
87922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
88922ac25cSAndré Goddard Rosa {
89922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
90922ac25cSAndré Goddard Rosa }
91922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
92922ac25cSAndré Goddard Rosa 
93922ac25cSAndré Goddard Rosa /**
94922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
95922ac25cSAndré Goddard Rosa  * @cp: The start of the string
96922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
97922ac25cSAndré Goddard Rosa  * @base: The number base to use
98922ac25cSAndré Goddard Rosa  */
99922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
100922ac25cSAndré Goddard Rosa {
101922ac25cSAndré Goddard Rosa 	if (*cp == '-')
102922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
103922ac25cSAndré Goddard Rosa 
104922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
105922ac25cSAndré Goddard Rosa }
106922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
107922ac25cSAndré Goddard Rosa 
108922ac25cSAndré Goddard Rosa /**
1091da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
1101da177e4SLinus Torvalds  * @cp: The start of the string
1111da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
1121da177e4SLinus Torvalds  * @base: The number base to use
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	if (*cp == '-')
1171da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1187b9186f5SAndré Goddard Rosa 
1191da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
12206b2a76dSYi Yang /**
12306b2a76dSYi Yang  * strict_strtoul - convert a string to an unsigned long strictly
12406b2a76dSYi Yang  * @cp: The string to be converted
12506b2a76dSYi Yang  * @base: The number base to use
12606b2a76dSYi Yang  * @res: The converted result value
12706b2a76dSYi Yang  *
12806b2a76dSYi Yang  * strict_strtoul converts a string to an unsigned long only if the
12906b2a76dSYi Yang  * string is really an unsigned long string, any string containing
13006b2a76dSYi Yang  * any invalid char at the tail will be rejected and -EINVAL is returned,
13106b2a76dSYi Yang  * only a newline char at the tail is acceptible because people generally
13206b2a76dSYi Yang  * change a module parameter in the following way:
13306b2a76dSYi Yang  *
13406b2a76dSYi Yang  * 	echo 1024 > /sys/module/e1000/parameters/copybreak
13506b2a76dSYi Yang  *
13606b2a76dSYi Yang  * echo will append a newline to the tail.
13706b2a76dSYi Yang  *
13806b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
13906b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
14006b2a76dSYi Yang  *
14106b2a76dSYi Yang  * simple_strtoul just ignores the successive invalid characters and
14206b2a76dSYi Yang  * return the converted value of prefix part of the string.
14306b2a76dSYi Yang  */
1449d85db22SHarvey Harrison int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
1459d85db22SHarvey Harrison {
1469d85db22SHarvey Harrison 	char *tail;
1479d85db22SHarvey Harrison 	unsigned long val;
1489d85db22SHarvey Harrison 	size_t len;
1499d85db22SHarvey Harrison 
1509d85db22SHarvey Harrison 	*res = 0;
1519d85db22SHarvey Harrison 	len = strlen(cp);
1529d85db22SHarvey Harrison 	if (len == 0)
1539d85db22SHarvey Harrison 		return -EINVAL;
1549d85db22SHarvey Harrison 
1559d85db22SHarvey Harrison 	val = simple_strtoul(cp, &tail, base);
156e899aa82SPavel Machek 	if (tail == cp)
157e899aa82SPavel Machek 		return -EINVAL;
1587b9186f5SAndré Goddard Rosa 
1599d85db22SHarvey Harrison 	if ((*tail == '\0') ||
1609d85db22SHarvey Harrison 		((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
1619d85db22SHarvey Harrison 		*res = val;
1629d85db22SHarvey Harrison 		return 0;
1639d85db22SHarvey Harrison 	}
1649d85db22SHarvey Harrison 
1659d85db22SHarvey Harrison 	return -EINVAL;
1669d85db22SHarvey Harrison }
1679d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoul);
16806b2a76dSYi Yang 
16906b2a76dSYi Yang /**
17006b2a76dSYi Yang  * strict_strtol - convert a string to a long strictly
17106b2a76dSYi Yang  * @cp: The string to be converted
17206b2a76dSYi Yang  * @base: The number base to use
17306b2a76dSYi Yang  * @res: The converted result value
17406b2a76dSYi Yang  *
17506b2a76dSYi Yang  * strict_strtol is similiar to strict_strtoul, but it allows the first
17606b2a76dSYi Yang  * character of a string is '-'.
17706b2a76dSYi Yang  *
17806b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
17906b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
18006b2a76dSYi Yang  */
1819d85db22SHarvey Harrison int strict_strtol(const char *cp, unsigned int base, long *res)
1829d85db22SHarvey Harrison {
1839d85db22SHarvey Harrison 	int ret;
1849d85db22SHarvey Harrison 	if (*cp == '-') {
1859d85db22SHarvey Harrison 		ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
1869d85db22SHarvey Harrison 		if (!ret)
1879d85db22SHarvey Harrison 			*res = -(*res);
1889d85db22SHarvey Harrison 	} else {
1899d85db22SHarvey Harrison 		ret = strict_strtoul(cp, base, (unsigned long *)res);
1909d85db22SHarvey Harrison 	}
1919d85db22SHarvey Harrison 
1929d85db22SHarvey Harrison 	return ret;
1939d85db22SHarvey Harrison }
1949d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtol);
19506b2a76dSYi Yang 
19606b2a76dSYi Yang /**
19706b2a76dSYi Yang  * strict_strtoull - convert a string to an unsigned long long strictly
19806b2a76dSYi Yang  * @cp: The string to be converted
19906b2a76dSYi Yang  * @base: The number base to use
20006b2a76dSYi Yang  * @res: The converted result value
20106b2a76dSYi Yang  *
20206b2a76dSYi Yang  * strict_strtoull converts a string to an unsigned long long only if the
20306b2a76dSYi Yang  * string is really an unsigned long long string, any string containing
20406b2a76dSYi Yang  * any invalid char at the tail will be rejected and -EINVAL is returned,
20506b2a76dSYi Yang  * only a newline char at the tail is acceptible because people generally
20606b2a76dSYi Yang  * change a module parameter in the following way:
20706b2a76dSYi Yang  *
20806b2a76dSYi Yang  * 	echo 1024 > /sys/module/e1000/parameters/copybreak
20906b2a76dSYi Yang  *
21006b2a76dSYi Yang  * echo will append a newline to the tail of the string.
21106b2a76dSYi Yang  *
21206b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
21306b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
21406b2a76dSYi Yang  *
21506b2a76dSYi Yang  * simple_strtoull just ignores the successive invalid characters and
21606b2a76dSYi Yang  * return the converted value of prefix part of the string.
21706b2a76dSYi Yang  */
2189d85db22SHarvey Harrison int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
2199d85db22SHarvey Harrison {
2209d85db22SHarvey Harrison 	char *tail;
2219d85db22SHarvey Harrison 	unsigned long long val;
2229d85db22SHarvey Harrison 	size_t len;
2239d85db22SHarvey Harrison 
2249d85db22SHarvey Harrison 	*res = 0;
2259d85db22SHarvey Harrison 	len = strlen(cp);
2269d85db22SHarvey Harrison 	if (len == 0)
2279d85db22SHarvey Harrison 		return -EINVAL;
2289d85db22SHarvey Harrison 
2299d85db22SHarvey Harrison 	val = simple_strtoull(cp, &tail, base);
230e899aa82SPavel Machek 	if (tail == cp)
231e899aa82SPavel Machek 		return -EINVAL;
2329d85db22SHarvey Harrison 	if ((*tail == '\0') ||
2339d85db22SHarvey Harrison 		((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
2349d85db22SHarvey Harrison 		*res = val;
2359d85db22SHarvey Harrison 		return 0;
2369d85db22SHarvey Harrison 	}
2379d85db22SHarvey Harrison 
2389d85db22SHarvey Harrison 	return -EINVAL;
2399d85db22SHarvey Harrison }
2409d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoull);
24106b2a76dSYi Yang 
24206b2a76dSYi Yang /**
24306b2a76dSYi Yang  * strict_strtoll - convert a string to a long long strictly
24406b2a76dSYi Yang  * @cp: The string to be converted
24506b2a76dSYi Yang  * @base: The number base to use
24606b2a76dSYi Yang  * @res: The converted result value
24706b2a76dSYi Yang  *
24806b2a76dSYi Yang  * strict_strtoll is similiar to strict_strtoull, but it allows the first
24906b2a76dSYi Yang  * character of a string is '-'.
25006b2a76dSYi Yang  *
25106b2a76dSYi Yang  * It returns 0 if conversion is successful and *res is set to the converted
25206b2a76dSYi Yang  * value, otherwise it returns -EINVAL and *res is set to 0.
25306b2a76dSYi Yang  */
2549d85db22SHarvey Harrison int strict_strtoll(const char *cp, unsigned int base, long long *res)
2559d85db22SHarvey Harrison {
2569d85db22SHarvey Harrison 	int ret;
2579d85db22SHarvey Harrison 	if (*cp == '-') {
2589d85db22SHarvey Harrison 		ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
2599d85db22SHarvey Harrison 		if (!ret)
2609d85db22SHarvey Harrison 			*res = -(*res);
2619d85db22SHarvey Harrison 	} else {
2629d85db22SHarvey Harrison 		ret = strict_strtoull(cp, base, (unsigned long long *)res);
2639d85db22SHarvey Harrison 	}
26406b2a76dSYi Yang 
2659d85db22SHarvey Harrison 	return ret;
2669d85db22SHarvey Harrison }
26706b2a76dSYi Yang EXPORT_SYMBOL(strict_strtoll);
26806b2a76dSYi Yang 
2691da177e4SLinus Torvalds static int skip_atoi(const char **s)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	int i = 0;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	while (isdigit(**s))
2741da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
2757b9186f5SAndré Goddard Rosa 
2761da177e4SLinus Torvalds 	return i;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
2794277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used
2804277eeddSDenis Vlasenko  * for /proc and /sys data. This directly impacts e.g. top performance
2814277eeddSDenis Vlasenko  * with many processes running. We optimize it for speed
2824277eeddSDenis Vlasenko  * using code from
2834277eeddSDenis Vlasenko  * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
2844277eeddSDenis Vlasenko  * (with permission from the author, Douglas W. Jones). */
2854277eeddSDenis Vlasenko 
2864277eeddSDenis Vlasenko /* Formats correctly any integer in [0,99999].
2874277eeddSDenis Vlasenko  * Outputs from one to five digits depending on input.
2884277eeddSDenis Vlasenko  * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
2894277eeddSDenis Vlasenko static char *put_dec_trunc(char *buf, unsigned q)
2904277eeddSDenis Vlasenko {
2914277eeddSDenis Vlasenko 	unsigned d3, d2, d1, d0;
2924277eeddSDenis Vlasenko 	d1 = (q>>4) & 0xf;
2934277eeddSDenis Vlasenko 	d2 = (q>>8) & 0xf;
2944277eeddSDenis Vlasenko 	d3 = (q>>12);
2954277eeddSDenis Vlasenko 
2964277eeddSDenis Vlasenko 	d0 = 6*(d3 + d2 + d1) + (q & 0xf);
2974277eeddSDenis Vlasenko 	q = (d0 * 0xcd) >> 11;
2984277eeddSDenis Vlasenko 	d0 = d0 - 10*q;
2994277eeddSDenis Vlasenko 	*buf++ = d0 + '0'; /* least significant digit */
3004277eeddSDenis Vlasenko 	d1 = q + 9*d3 + 5*d2 + d1;
3014277eeddSDenis Vlasenko 	if (d1 != 0) {
3024277eeddSDenis Vlasenko 		q = (d1 * 0xcd) >> 11;
3034277eeddSDenis Vlasenko 		d1 = d1 - 10*q;
3044277eeddSDenis Vlasenko 		*buf++ = d1 + '0'; /* next digit */
3054277eeddSDenis Vlasenko 
3064277eeddSDenis Vlasenko 		d2 = q + 2*d2;
3074277eeddSDenis Vlasenko 		if ((d2 != 0) || (d3 != 0)) {
3084277eeddSDenis Vlasenko 			q = (d2 * 0xd) >> 7;
3094277eeddSDenis Vlasenko 			d2 = d2 - 10*q;
3104277eeddSDenis Vlasenko 			*buf++ = d2 + '0'; /* next digit */
3114277eeddSDenis Vlasenko 
3124277eeddSDenis Vlasenko 			d3 = q + 4*d3;
3134277eeddSDenis Vlasenko 			if (d3 != 0) {
3144277eeddSDenis Vlasenko 				q = (d3 * 0xcd) >> 11;
3154277eeddSDenis Vlasenko 				d3 = d3 - 10*q;
3164277eeddSDenis Vlasenko 				*buf++ = d3 + '0';  /* next digit */
3174277eeddSDenis Vlasenko 				if (q != 0)
3184277eeddSDenis Vlasenko 					*buf++ = q + '0'; /* most sign. digit */
3194277eeddSDenis Vlasenko 			}
3204277eeddSDenis Vlasenko 		}
3214277eeddSDenis Vlasenko 	}
3227b9186f5SAndré Goddard Rosa 
3234277eeddSDenis Vlasenko 	return buf;
3244277eeddSDenis Vlasenko }
3254277eeddSDenis Vlasenko /* Same with if's removed. Always emits five digits */
3264277eeddSDenis Vlasenko static char *put_dec_full(char *buf, unsigned q)
3274277eeddSDenis Vlasenko {
3284277eeddSDenis Vlasenko 	/* BTW, if q is in [0,9999], 8-bit ints will be enough, */
3294277eeddSDenis Vlasenko 	/* but anyway, gcc produces better code with full-sized ints */
3304277eeddSDenis Vlasenko 	unsigned d3, d2, d1, d0;
3314277eeddSDenis Vlasenko 	d1 = (q>>4) & 0xf;
3324277eeddSDenis Vlasenko 	d2 = (q>>8) & 0xf;
3334277eeddSDenis Vlasenko 	d3 = (q>>12);
3344277eeddSDenis Vlasenko 
3357b9186f5SAndré Goddard Rosa 	/*
3367b9186f5SAndré Goddard Rosa 	 * Possible ways to approx. divide by 10
3377b9186f5SAndré Goddard Rosa 	 * gcc -O2 replaces multiply with shifts and adds
3387b9186f5SAndré Goddard Rosa 	 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
3397b9186f5SAndré Goddard Rosa 	 * (x * 0x67) >> 10:  1100111
3407b9186f5SAndré Goddard Rosa 	 * (x * 0x34) >> 9:    110100 - same
3417b9186f5SAndré Goddard Rosa 	 * (x * 0x1a) >> 8:     11010 - same
3427b9186f5SAndré Goddard Rosa 	 * (x * 0x0d) >> 7:      1101 - same, shortest code (on i386)
3437b9186f5SAndré Goddard Rosa 	 */
3444277eeddSDenis Vlasenko 	d0 = 6*(d3 + d2 + d1) + (q & 0xf);
3454277eeddSDenis Vlasenko 	q = (d0 * 0xcd) >> 11;
3464277eeddSDenis Vlasenko 	d0 = d0 - 10*q;
3474277eeddSDenis Vlasenko 	*buf++ = d0 + '0';
3484277eeddSDenis Vlasenko 	d1 = q + 9*d3 + 5*d2 + d1;
3494277eeddSDenis Vlasenko 		q = (d1 * 0xcd) >> 11;
3504277eeddSDenis Vlasenko 		d1 = d1 - 10*q;
3514277eeddSDenis Vlasenko 		*buf++ = d1 + '0';
3524277eeddSDenis Vlasenko 
3534277eeddSDenis Vlasenko 		d2 = q + 2*d2;
3544277eeddSDenis Vlasenko 			q = (d2 * 0xd) >> 7;
3554277eeddSDenis Vlasenko 			d2 = d2 - 10*q;
3564277eeddSDenis Vlasenko 			*buf++ = d2 + '0';
3574277eeddSDenis Vlasenko 
3584277eeddSDenis Vlasenko 			d3 = q + 4*d3;
3594277eeddSDenis Vlasenko 				q = (d3 * 0xcd) >> 11; /* - shorter code */
3604277eeddSDenis Vlasenko 				/* q = (d3 * 0x67) >> 10; - would also work */
3614277eeddSDenis Vlasenko 				d3 = d3 - 10*q;
3624277eeddSDenis Vlasenko 				*buf++ = d3 + '0';
3634277eeddSDenis Vlasenko 					*buf++ = q + '0';
3647b9186f5SAndré Goddard Rosa 
3654277eeddSDenis Vlasenko 	return buf;
3664277eeddSDenis Vlasenko }
3674277eeddSDenis Vlasenko /* No inlining helps gcc to use registers better */
3684277eeddSDenis Vlasenko static noinline char *put_dec(char *buf, unsigned long long num)
3694277eeddSDenis Vlasenko {
3704277eeddSDenis Vlasenko 	while (1) {
3714277eeddSDenis Vlasenko 		unsigned rem;
3724277eeddSDenis Vlasenko 		if (num < 100000)
3734277eeddSDenis Vlasenko 			return put_dec_trunc(buf, num);
3744277eeddSDenis Vlasenko 		rem = do_div(num, 100000);
3754277eeddSDenis Vlasenko 		buf = put_dec_full(buf, rem);
3764277eeddSDenis Vlasenko 	}
3774277eeddSDenis Vlasenko }
3784277eeddSDenis Vlasenko 
3791da177e4SLinus Torvalds #define ZEROPAD	1		/* pad with zero */
3801da177e4SLinus Torvalds #define SIGN	2		/* unsigned/signed long */
3811da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3821da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
3831da177e4SLinus Torvalds #define LEFT	16		/* left justified */
3849b706aeeSDenys Vlasenko #define SMALL	32		/* Must be 32 == 0x20 */
3859b706aeeSDenys Vlasenko #define SPECIAL	64		/* 0x */
3861da177e4SLinus Torvalds 
387fef20d9cSFrederic Weisbecker enum format_type {
388fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
389ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
390fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
391fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
392fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
393fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
394fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
395fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
396fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
397fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
398fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
399a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
400a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
401fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
402fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
403fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
404fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
405fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NRCHARS,
406fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
407fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
408fef20d9cSFrederic Weisbecker };
409fef20d9cSFrederic Weisbecker 
410fef20d9cSFrederic Weisbecker struct printf_spec {
411fef20d9cSFrederic Weisbecker 	enum format_type	type;
412fef20d9cSFrederic Weisbecker 	int			flags;		/* flags to number() */
413fef20d9cSFrederic Weisbecker 	int			field_width;	/* width of output field */
414fef20d9cSFrederic Weisbecker 	int			base;
415fef20d9cSFrederic Weisbecker 	int			precision;	/* # of digits/chars */
416fef20d9cSFrederic Weisbecker 	int			qualifier;
417fef20d9cSFrederic Weisbecker };
418fef20d9cSFrederic Weisbecker 
419fef20d9cSFrederic Weisbecker static char *number(char *buf, char *end, unsigned long long num,
420fef20d9cSFrederic Weisbecker 			struct printf_spec spec)
4211da177e4SLinus Torvalds {
4229b706aeeSDenys Vlasenko 	/* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
4239b706aeeSDenys Vlasenko 	static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
4249b706aeeSDenys Vlasenko 
4259b706aeeSDenys Vlasenko 	char tmp[66];
4269b706aeeSDenys Vlasenko 	char sign;
4279b706aeeSDenys Vlasenko 	char locase;
428fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
4291da177e4SLinus Torvalds 	int i;
4301da177e4SLinus Torvalds 
4319b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
4329b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
433fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
434fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
435fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
4361da177e4SLinus Torvalds 	sign = 0;
437fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4381da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4391da177e4SLinus Torvalds 			sign = '-';
4401da177e4SLinus Torvalds 			num = -(signed long long)num;
441fef20d9cSFrederic Weisbecker 			spec.field_width--;
442fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4431da177e4SLinus Torvalds 			sign = '+';
444fef20d9cSFrederic Weisbecker 			spec.field_width--;
445fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4461da177e4SLinus Torvalds 			sign = ' ';
447fef20d9cSFrederic Weisbecker 			spec.field_width--;
4481da177e4SLinus Torvalds 		}
4491da177e4SLinus Torvalds 	}
450b39a7340SDenis Vlasenko 	if (need_pfx) {
451fef20d9cSFrederic Weisbecker 		spec.field_width--;
452fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
453fef20d9cSFrederic Weisbecker 			spec.field_width--;
4541da177e4SLinus Torvalds 	}
455b39a7340SDenis Vlasenko 
456b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4571da177e4SLinus Torvalds 	i = 0;
4581da177e4SLinus Torvalds 	if (num == 0)
4591da177e4SLinus Torvalds 		tmp[i++] = '0';
4604277eeddSDenis Vlasenko 	/* Generic code, for any base:
4614277eeddSDenis Vlasenko 	else do {
4629b706aeeSDenys Vlasenko 		tmp[i++] = (digits[do_div(num,base)] | locase);
4634277eeddSDenis Vlasenko 	} while (num != 0);
4644277eeddSDenis Vlasenko 	*/
465fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
466fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
467b39a7340SDenis Vlasenko 		int shift = 3;
4687b9186f5SAndré Goddard Rosa 
4697b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4707b9186f5SAndré Goddard Rosa 			shift = 4;
471b39a7340SDenis Vlasenko 		do {
4729b706aeeSDenys Vlasenko 			tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
473b39a7340SDenis Vlasenko 			num >>= shift;
474b39a7340SDenis Vlasenko 		} while (num);
4754277eeddSDenis Vlasenko 	} else { /* base 10 */
4764277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4774277eeddSDenis Vlasenko 	}
478b39a7340SDenis Vlasenko 
479b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
480fef20d9cSFrederic Weisbecker 	if (i > spec.precision)
481fef20d9cSFrederic Weisbecker 		spec.precision = i;
482b39a7340SDenis Vlasenko 	/* leading space padding */
483fef20d9cSFrederic Weisbecker 	spec.field_width -= spec.precision;
484fef20d9cSFrederic Weisbecker 	if (!(spec.flags & (ZEROPAD+LEFT))) {
485fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
486f796937aSJeremy Fitzhardinge 			if (buf < end)
4871da177e4SLinus Torvalds 				*buf = ' ';
4881da177e4SLinus Torvalds 			++buf;
4891da177e4SLinus Torvalds 		}
4901da177e4SLinus Torvalds 	}
491b39a7340SDenis Vlasenko 	/* sign */
4921da177e4SLinus Torvalds 	if (sign) {
493f796937aSJeremy Fitzhardinge 		if (buf < end)
4941da177e4SLinus Torvalds 			*buf = sign;
4951da177e4SLinus Torvalds 		++buf;
4961da177e4SLinus Torvalds 	}
497b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
498b39a7340SDenis Vlasenko 	if (need_pfx) {
499f796937aSJeremy Fitzhardinge 		if (buf < end)
5001da177e4SLinus Torvalds 			*buf = '0';
5011da177e4SLinus Torvalds 		++buf;
502fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
503f796937aSJeremy Fitzhardinge 			if (buf < end)
5049b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
5051da177e4SLinus Torvalds 			++buf;
5061da177e4SLinus Torvalds 		}
5071da177e4SLinus Torvalds 	}
508b39a7340SDenis Vlasenko 	/* zero or space padding */
509fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
510fef20d9cSFrederic Weisbecker 		char c = (spec.flags & ZEROPAD) ? '0' : ' ';
511fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
512f796937aSJeremy Fitzhardinge 			if (buf < end)
5131da177e4SLinus Torvalds 				*buf = c;
5141da177e4SLinus Torvalds 			++buf;
5151da177e4SLinus Torvalds 		}
5161da177e4SLinus Torvalds 	}
517b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
518fef20d9cSFrederic Weisbecker 	while (i <= --spec.precision) {
519f796937aSJeremy Fitzhardinge 		if (buf < end)
5201da177e4SLinus Torvalds 			*buf = '0';
5211da177e4SLinus Torvalds 		++buf;
5221da177e4SLinus Torvalds 	}
523b39a7340SDenis Vlasenko 	/* actual digits of result */
524b39a7340SDenis Vlasenko 	while (--i >= 0) {
525f796937aSJeremy Fitzhardinge 		if (buf < end)
5261da177e4SLinus Torvalds 			*buf = tmp[i];
5271da177e4SLinus Torvalds 		++buf;
5281da177e4SLinus Torvalds 	}
529b39a7340SDenis Vlasenko 	/* trailing space padding */
530fef20d9cSFrederic Weisbecker 	while (--spec.field_width >= 0) {
531f796937aSJeremy Fitzhardinge 		if (buf < end)
5321da177e4SLinus Torvalds 			*buf = ' ';
5331da177e4SLinus Torvalds 		++buf;
5341da177e4SLinus Torvalds 	}
5357b9186f5SAndré Goddard Rosa 
5361da177e4SLinus Torvalds 	return buf;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
5390f4f81dcSAndré Goddard Rosa static char *string(char *buf, char *end, const char *s, struct printf_spec spec)
5400f9bfa56SLinus Torvalds {
5410f9bfa56SLinus Torvalds 	int len, i;
5420f9bfa56SLinus Torvalds 
5430f9bfa56SLinus Torvalds 	if ((unsigned long)s < PAGE_SIZE)
5440f4f81dcSAndré Goddard Rosa 		s = "(null)";
5450f9bfa56SLinus Torvalds 
546fef20d9cSFrederic Weisbecker 	len = strnlen(s, spec.precision);
5470f9bfa56SLinus Torvalds 
548fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
549fef20d9cSFrederic Weisbecker 		while (len < spec.field_width--) {
5500f9bfa56SLinus Torvalds 			if (buf < end)
5510f9bfa56SLinus Torvalds 				*buf = ' ';
5520f9bfa56SLinus Torvalds 			++buf;
5530f9bfa56SLinus Torvalds 		}
5540f9bfa56SLinus Torvalds 	}
5550f9bfa56SLinus Torvalds 	for (i = 0; i < len; ++i) {
5560f9bfa56SLinus Torvalds 		if (buf < end)
5570f9bfa56SLinus Torvalds 			*buf = *s;
5580f9bfa56SLinus Torvalds 		++buf; ++s;
5590f9bfa56SLinus Torvalds 	}
560fef20d9cSFrederic Weisbecker 	while (len < spec.field_width--) {
5610f9bfa56SLinus Torvalds 		if (buf < end)
5620f9bfa56SLinus Torvalds 			*buf = ' ';
5630f9bfa56SLinus Torvalds 		++buf;
5640f9bfa56SLinus Torvalds 	}
5657b9186f5SAndré Goddard Rosa 
5660f9bfa56SLinus Torvalds 	return buf;
5670f9bfa56SLinus Torvalds }
5680f9bfa56SLinus Torvalds 
569fef20d9cSFrederic Weisbecker static char *symbol_string(char *buf, char *end, void *ptr,
5700c8b946eSFrederic Weisbecker 				struct printf_spec spec, char ext)
5710fe1ef24SLinus Torvalds {
5720fe1ef24SLinus Torvalds 	unsigned long value = (unsigned long) ptr;
5730fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
5740fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
57591adcd2cSSteven Rostedt 	if (ext != 'f' && ext != 's')
5760fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
5770c8b946eSFrederic Weisbecker 	else
5780c8b946eSFrederic Weisbecker 		kallsyms_lookup(value, NULL, NULL, NULL, sym);
5797b9186f5SAndré Goddard Rosa 
580fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
5810fe1ef24SLinus Torvalds #else
582fef20d9cSFrederic Weisbecker 	spec.field_width = 2 * sizeof(void *);
583fef20d9cSFrederic Weisbecker 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
584fef20d9cSFrederic Weisbecker 	spec.base = 16;
5857b9186f5SAndré Goddard Rosa 
586fef20d9cSFrederic Weisbecker 	return number(buf, end, value, spec);
5870fe1ef24SLinus Torvalds #endif
5880fe1ef24SLinus Torvalds }
5890fe1ef24SLinus Torvalds 
590fef20d9cSFrederic Weisbecker static char *resource_string(char *buf, char *end, struct resource *res,
591fd95541eSBjorn Helgaas 				struct printf_spec spec, const char *fmt)
592332d2e78SLinus Torvalds {
593332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
59428405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
595332d2e78SLinus Torvalds #endif
596332d2e78SLinus Torvalds 
597332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
59828405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
599332d2e78SLinus Torvalds #endif
600c91d3376SBjorn Helgaas 	struct printf_spec hex_spec = {
601fef20d9cSFrederic Weisbecker 		.base = 16,
602fef20d9cSFrederic Weisbecker 		.precision = -1,
603fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
604fef20d9cSFrederic Weisbecker 	};
605c91d3376SBjorn Helgaas 	struct printf_spec dec_spec = {
606c91d3376SBjorn Helgaas 		.base = 10,
607c91d3376SBjorn Helgaas 		.precision = -1,
608c91d3376SBjorn Helgaas 		.flags = 0,
609c91d3376SBjorn Helgaas 	};
610fd95541eSBjorn Helgaas 	struct printf_spec str_spec = {
611fd95541eSBjorn Helgaas 		.field_width = -1,
612fd95541eSBjorn Helgaas 		.precision = 10,
613fd95541eSBjorn Helgaas 		.flags = LEFT,
614fd95541eSBjorn Helgaas 	};
615fd95541eSBjorn Helgaas 	struct printf_spec flag_spec = {
616fd95541eSBjorn Helgaas 		.base = 16,
617fd95541eSBjorn Helgaas 		.precision = -1,
618fd95541eSBjorn Helgaas 		.flags = SPECIAL | SMALL,
619fd95541eSBjorn Helgaas 	};
620c7dabef8SBjorn Helgaas 
621c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
622c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
623c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
624c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
625c7dabef8SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref disabled]")
626c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
627c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
628c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
629c7dabef8SBjorn Helgaas 
630332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
631c91d3376SBjorn Helgaas 	int size = -1, addr = 0;
632c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
633332d2e78SLinus Torvalds 
634c91d3376SBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
635332d2e78SLinus Torvalds 		size = IO_RSRC_PRINTK_SIZE;
636c91d3376SBjorn Helgaas 		addr = 1;
637c91d3376SBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
638332d2e78SLinus Torvalds 		size = MEM_RSRC_PRINTK_SIZE;
639c91d3376SBjorn Helgaas 		addr = 1;
640c91d3376SBjorn Helgaas 	}
641332d2e78SLinus Torvalds 
642332d2e78SLinus Torvalds 	*p++ = '[';
643fd95541eSBjorn Helgaas 	if (res->flags & IORESOURCE_IO)
644fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
645fd95541eSBjorn Helgaas 	else if (res->flags & IORESOURCE_MEM)
646fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
647fd95541eSBjorn Helgaas 	else if (res->flags & IORESOURCE_IRQ)
648fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
649fd95541eSBjorn Helgaas 	else if (res->flags & IORESOURCE_DMA)
650fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
651c7dabef8SBjorn Helgaas 	else {
652c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
653c7dabef8SBjorn Helgaas 		decode = 0;
654fd95541eSBjorn Helgaas 	}
655c91d3376SBjorn Helgaas 	hex_spec.field_width = size;
656c91d3376SBjorn Helgaas 	p = number(p, pend, res->start, addr ? hex_spec : dec_spec);
657c91d3376SBjorn Helgaas 	if (res->start != res->end) {
658332d2e78SLinus Torvalds 		*p++ = '-';
659c91d3376SBjorn Helgaas 		p = number(p, pend, res->end, addr ? hex_spec : dec_spec);
660c91d3376SBjorn Helgaas 	}
661c7dabef8SBjorn Helgaas 	if (decode) {
662fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
663fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
664fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
665fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
666fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
667fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
668c7dabef8SBjorn Helgaas 	} else {
669fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
670c7dabef8SBjorn Helgaas 		p = number(p, pend, res->flags, flag_spec);
671fd95541eSBjorn Helgaas 	}
672332d2e78SLinus Torvalds 	*p++ = ']';
673c7dabef8SBjorn Helgaas 	*p = '\0';
674332d2e78SLinus Torvalds 
675fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
676332d2e78SLinus Torvalds }
677332d2e78SLinus Torvalds 
678fef20d9cSFrederic Weisbecker static char *mac_address_string(char *buf, char *end, u8 *addr,
6798a27f7c9SJoe Perches 				struct printf_spec spec, const char *fmt)
680dd45c9cfSHarvey Harrison {
6818a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
682dd45c9cfSHarvey Harrison 	char *p = mac_addr;
683dd45c9cfSHarvey Harrison 	int i;
684dd45c9cfSHarvey Harrison 
685dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
686dd45c9cfSHarvey Harrison 		p = pack_hex_byte(p, addr[i]);
6878a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
688dd45c9cfSHarvey Harrison 			*p++ = ':';
689dd45c9cfSHarvey Harrison 	}
690dd45c9cfSHarvey Harrison 	*p = '\0';
691dd45c9cfSHarvey Harrison 
692fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
693dd45c9cfSHarvey Harrison }
694dd45c9cfSHarvey Harrison 
6958a27f7c9SJoe Perches static char *ip4_string(char *p, const u8 *addr, bool leading_zeros)
696689afa7dSHarvey Harrison {
697689afa7dSHarvey Harrison 	int i;
698689afa7dSHarvey Harrison 
6998a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
7008a27f7c9SJoe Perches 		char temp[3];	/* hold each IP quad in reverse order */
7018a27f7c9SJoe Perches 		int digits = put_dec_trunc(temp, addr[i]) - temp;
7028a27f7c9SJoe Perches 		if (leading_zeros) {
7038a27f7c9SJoe Perches 			if (digits < 3)
7048a27f7c9SJoe Perches 				*p++ = '0';
7058a27f7c9SJoe Perches 			if (digits < 2)
7068a27f7c9SJoe Perches 				*p++ = '0';
7078a27f7c9SJoe Perches 		}
7088a27f7c9SJoe Perches 		/* reverse the digits in the quad */
7098a27f7c9SJoe Perches 		while (digits--)
7108a27f7c9SJoe Perches 			*p++ = temp[digits];
7118a27f7c9SJoe Perches 		if (i < 3)
7128a27f7c9SJoe Perches 			*p++ = '.';
7138a27f7c9SJoe Perches 	}
7148a27f7c9SJoe Perches 	*p = '\0';
7157b9186f5SAndré Goddard Rosa 
7168a27f7c9SJoe Perches 	return p;
7178a27f7c9SJoe Perches }
7188a27f7c9SJoe Perches 
719eb78cd26SJoe Perches static char *ip6_compressed_string(char *p, const char *addr)
7208a27f7c9SJoe Perches {
7217b9186f5SAndré Goddard Rosa 	int i, j, range;
7228a27f7c9SJoe Perches 	unsigned char zerolength[8];
7238a27f7c9SJoe Perches 	int longest = 1;
7248a27f7c9SJoe Perches 	int colonpos = -1;
7258a27f7c9SJoe Perches 	u16 word;
7267b9186f5SAndré Goddard Rosa 	u8 hi, lo;
7278a27f7c9SJoe Perches 	bool needcolon = false;
728eb78cd26SJoe Perches 	bool useIPv4;
729eb78cd26SJoe Perches 	struct in6_addr in6;
730eb78cd26SJoe Perches 
731eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
732eb78cd26SJoe Perches 
733eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
7348a27f7c9SJoe Perches 
7358a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
7368a27f7c9SJoe Perches 
7378a27f7c9SJoe Perches 	if (useIPv4)
7388a27f7c9SJoe Perches 		range = 6;
7398a27f7c9SJoe Perches 	else
7408a27f7c9SJoe Perches 		range = 8;
7418a27f7c9SJoe Perches 
7428a27f7c9SJoe Perches 	/* find position of longest 0 run */
7438a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
7448a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
745eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
7468a27f7c9SJoe Perches 				break;
7478a27f7c9SJoe Perches 			zerolength[i]++;
7488a27f7c9SJoe Perches 		}
7498a27f7c9SJoe Perches 	}
7508a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
7518a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
7528a27f7c9SJoe Perches 			longest = zerolength[i];
7538a27f7c9SJoe Perches 			colonpos = i;
7548a27f7c9SJoe Perches 		}
7558a27f7c9SJoe Perches 	}
7568a27f7c9SJoe Perches 
7578a27f7c9SJoe Perches 	/* emit address */
7588a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
7598a27f7c9SJoe Perches 		if (i == colonpos) {
7608a27f7c9SJoe Perches 			if (needcolon || i == 0)
7618a27f7c9SJoe Perches 				*p++ = ':';
7628a27f7c9SJoe Perches 			*p++ = ':';
7638a27f7c9SJoe Perches 			needcolon = false;
7648a27f7c9SJoe Perches 			i += longest - 1;
7658a27f7c9SJoe Perches 			continue;
7668a27f7c9SJoe Perches 		}
7678a27f7c9SJoe Perches 		if (needcolon) {
7688a27f7c9SJoe Perches 			*p++ = ':';
7698a27f7c9SJoe Perches 			needcolon = false;
7708a27f7c9SJoe Perches 		}
7718a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
772eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
7738a27f7c9SJoe Perches 		hi = word >> 8;
7748a27f7c9SJoe Perches 		lo = word & 0xff;
7758a27f7c9SJoe Perches 		if (hi) {
7768a27f7c9SJoe Perches 			if (hi > 0x0f)
7778a27f7c9SJoe Perches 				p = pack_hex_byte(p, hi);
7788a27f7c9SJoe Perches 			else
7798a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
780b5ff992bSAndré Goddard Rosa 			p = pack_hex_byte(p, lo);
7818a27f7c9SJoe Perches 		}
782b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
7838a27f7c9SJoe Perches 			p = pack_hex_byte(p, lo);
7848a27f7c9SJoe Perches 		else
7858a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
7868a27f7c9SJoe Perches 		needcolon = true;
7878a27f7c9SJoe Perches 	}
7888a27f7c9SJoe Perches 
7898a27f7c9SJoe Perches 	if (useIPv4) {
7908a27f7c9SJoe Perches 		if (needcolon)
7918a27f7c9SJoe Perches 			*p++ = ':';
792eb78cd26SJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], false);
7938a27f7c9SJoe Perches 	}
7948a27f7c9SJoe Perches 	*p = '\0';
7957b9186f5SAndré Goddard Rosa 
7968a27f7c9SJoe Perches 	return p;
7978a27f7c9SJoe Perches }
7988a27f7c9SJoe Perches 
799eb78cd26SJoe Perches static char *ip6_string(char *p, const char *addr, const char *fmt)
8008a27f7c9SJoe Perches {
8018a27f7c9SJoe Perches 	int i;
8027b9186f5SAndré Goddard Rosa 
803689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
804eb78cd26SJoe Perches 		p = pack_hex_byte(p, *addr++);
805eb78cd26SJoe Perches 		p = pack_hex_byte(p, *addr++);
8068a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
807689afa7dSHarvey Harrison 			*p++ = ':';
808689afa7dSHarvey Harrison 	}
809689afa7dSHarvey Harrison 	*p = '\0';
8107b9186f5SAndré Goddard Rosa 
8118a27f7c9SJoe Perches 	return p;
8128a27f7c9SJoe Perches }
8138a27f7c9SJoe Perches 
8148a27f7c9SJoe Perches static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
8158a27f7c9SJoe Perches 			     struct printf_spec spec, const char *fmt)
8168a27f7c9SJoe Perches {
8178a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
8188a27f7c9SJoe Perches 
8198a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
820eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
8218a27f7c9SJoe Perches 	else
822eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
823689afa7dSHarvey Harrison 
824fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
825689afa7dSHarvey Harrison }
826689afa7dSHarvey Harrison 
8278a27f7c9SJoe Perches static char *ip4_addr_string(char *buf, char *end, const u8 *addr,
8288a27f7c9SJoe Perches 			     struct printf_spec spec, const char *fmt)
8294aa99606SHarvey Harrison {
8308a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
8314aa99606SHarvey Harrison 
8328a27f7c9SJoe Perches 	ip4_string(ip4_addr, addr, fmt[0] == 'i');
8334aa99606SHarvey Harrison 
834fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
8354aa99606SHarvey Harrison }
8364aa99606SHarvey Harrison 
8374d8a743cSLinus Torvalds /*
8384d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
8394d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
8404d8a743cSLinus Torvalds  * specifiers.
8414d8a743cSLinus Torvalds  *
842332d2e78SLinus Torvalds  * Right now we handle:
8430fe1ef24SLinus Torvalds  *
8440c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
8450c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
8460efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
8470efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
848c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
849c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
850dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
851dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
8528a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
8538a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
8548a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
8558a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
8568a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
8578a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
8588a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
8598a27f7c9SJoe Perches  * - 'I6c' for IPv6 addresses printed as specified by
8608a27f7c9SJoe Perches  *       http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
861332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
862332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
863332d2e78SLinus Torvalds  * pointer to the real address.
8644d8a743cSLinus Torvalds  */
865fef20d9cSFrederic Weisbecker static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
866fef20d9cSFrederic Weisbecker 			struct printf_spec spec)
86778a8bf69SLinus Torvalds {
868d97106abSLinus Torvalds 	if (!ptr)
869fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
870d97106abSLinus Torvalds 
8710fe1ef24SLinus Torvalds 	switch (*fmt) {
8720fe1ef24SLinus Torvalds 	case 'F':
8730c8b946eSFrederic Weisbecker 	case 'f':
8740fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
8750fe1ef24SLinus Torvalds 		/* Fallthrough */
876d4be151bSAndré Goddard Rosa 	case 's':
8770fe1ef24SLinus Torvalds 	case 'S':
8780c8b946eSFrederic Weisbecker 		return symbol_string(buf, end, ptr, spec, *fmt);
879332d2e78SLinus Torvalds 	case 'R':
880c7dabef8SBjorn Helgaas 	case 'r':
881fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
8828a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
8838a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
8848a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
8858a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
8868a27f7c9SJoe Perches 					 * 4:	1.2.3.4
8878a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
8888a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
8898a27f7c9SJoe Perches 					 */
8908a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
8918a27f7c9SJoe Perches 					 * 4:	001.002.003.004
8928a27f7c9SJoe Perches 					 * 6:   000102...0f
8938a27f7c9SJoe Perches 					 */
8948a27f7c9SJoe Perches 		switch (fmt[1]) {
8958a27f7c9SJoe Perches 		case '6':
8968a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
8978a27f7c9SJoe Perches 		case '4':
8988a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
8998a27f7c9SJoe Perches 		}
9004aa99606SHarvey Harrison 		break;
9010fe1ef24SLinus Torvalds 	}
902fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
903fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
904fef20d9cSFrederic Weisbecker 		spec.field_width = 2*sizeof(void *);
905fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
90678a8bf69SLinus Torvalds 	}
907fef20d9cSFrederic Weisbecker 	spec.base = 16;
908fef20d9cSFrederic Weisbecker 
909fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
910fef20d9cSFrederic Weisbecker }
911fef20d9cSFrederic Weisbecker 
912fef20d9cSFrederic Weisbecker /*
913fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
914fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
915fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
916fef20d9cSFrederic Weisbecker  * to go on the next call).
917fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
918fef20d9cSFrederic Weisbecker  *
919fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
920fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
921fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
922fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
923fef20d9cSFrederic Weisbecker  *
924fef20d9cSFrederic Weisbecker  * @fmt: the format string
925fef20d9cSFrederic Weisbecker  * @type of the token returned
926fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
927fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
928fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
929fef20d9cSFrederic Weisbecker  * @precision: precision of a number
930fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
931fef20d9cSFrederic Weisbecker  */
932fef20d9cSFrederic Weisbecker static int format_decode(const char *fmt, struct printf_spec *spec)
933fef20d9cSFrederic Weisbecker {
934fef20d9cSFrederic Weisbecker 	const char *start = fmt;
935fef20d9cSFrederic Weisbecker 
936fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
937ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
938fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
939fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
940fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
941fef20d9cSFrederic Weisbecker 		}
942fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
943fef20d9cSFrederic Weisbecker 		goto precision;
944fef20d9cSFrederic Weisbecker 	}
945fef20d9cSFrederic Weisbecker 
946fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
947fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
948fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
949fef20d9cSFrederic Weisbecker 			spec->precision = 0;
950fef20d9cSFrederic Weisbecker 
951fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
952fef20d9cSFrederic Weisbecker 		goto qualifier;
953fef20d9cSFrederic Weisbecker 	}
954fef20d9cSFrederic Weisbecker 
955fef20d9cSFrederic Weisbecker 	/* By default */
956fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
957fef20d9cSFrederic Weisbecker 
958fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
959fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
960fef20d9cSFrederic Weisbecker 			break;
961fef20d9cSFrederic Weisbecker 	}
962fef20d9cSFrederic Weisbecker 
963fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
964fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
965fef20d9cSFrederic Weisbecker 		return fmt - start;
966fef20d9cSFrederic Weisbecker 
967fef20d9cSFrederic Weisbecker 	/* Process flags */
968fef20d9cSFrederic Weisbecker 	spec->flags = 0;
969fef20d9cSFrederic Weisbecker 
970fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
971fef20d9cSFrederic Weisbecker 		bool found = true;
972fef20d9cSFrederic Weisbecker 
973fef20d9cSFrederic Weisbecker 		++fmt;
974fef20d9cSFrederic Weisbecker 
975fef20d9cSFrederic Weisbecker 		switch (*fmt) {
976fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
977fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
978fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
979fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
980fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
981fef20d9cSFrederic Weisbecker 		default:  found = false;
982fef20d9cSFrederic Weisbecker 		}
983fef20d9cSFrederic Weisbecker 
984fef20d9cSFrederic Weisbecker 		if (!found)
985fef20d9cSFrederic Weisbecker 			break;
986fef20d9cSFrederic Weisbecker 	}
987fef20d9cSFrederic Weisbecker 
988fef20d9cSFrederic Weisbecker 	/* get field width */
989fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
990fef20d9cSFrederic Weisbecker 
991fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
992fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
993fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
994fef20d9cSFrederic Weisbecker 		/* it's the next argument */
995ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
996fef20d9cSFrederic Weisbecker 		return ++fmt - start;
997fef20d9cSFrederic Weisbecker 	}
998fef20d9cSFrederic Weisbecker 
999fef20d9cSFrederic Weisbecker precision:
1000fef20d9cSFrederic Weisbecker 	/* get the precision */
1001fef20d9cSFrederic Weisbecker 	spec->precision = -1;
1002fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
1003fef20d9cSFrederic Weisbecker 		++fmt;
1004fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
1005fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
1006fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
1007fef20d9cSFrederic Weisbecker 				spec->precision = 0;
1008fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
1009fef20d9cSFrederic Weisbecker 			/* it's the next argument */
1010adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
1011fef20d9cSFrederic Weisbecker 			return ++fmt - start;
1012fef20d9cSFrederic Weisbecker 		}
1013fef20d9cSFrederic Weisbecker 	}
1014fef20d9cSFrederic Weisbecker 
1015fef20d9cSFrederic Weisbecker qualifier:
1016fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
1017fef20d9cSFrederic Weisbecker 	spec->qualifier = -1;
101808562cb2SAndré Goddard Rosa 	if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
101908562cb2SAndré Goddard Rosa 	    TOLOWER(*fmt) == 'z' || *fmt == 't') {
1020a4e94ef0SZhaolei 		spec->qualifier = *fmt++;
1021a4e94ef0SZhaolei 		if (unlikely(spec->qualifier == *fmt)) {
1022a4e94ef0SZhaolei 			if (spec->qualifier == 'l') {
1023fef20d9cSFrederic Weisbecker 				spec->qualifier = 'L';
1024fef20d9cSFrederic Weisbecker 				++fmt;
1025a4e94ef0SZhaolei 			} else if (spec->qualifier == 'h') {
1026a4e94ef0SZhaolei 				spec->qualifier = 'H';
1027a4e94ef0SZhaolei 				++fmt;
1028a4e94ef0SZhaolei 			}
1029fef20d9cSFrederic Weisbecker 		}
1030fef20d9cSFrederic Weisbecker 	}
1031fef20d9cSFrederic Weisbecker 
1032fef20d9cSFrederic Weisbecker 	/* default base */
1033fef20d9cSFrederic Weisbecker 	spec->base = 10;
1034fef20d9cSFrederic Weisbecker 	switch (*fmt) {
1035fef20d9cSFrederic Weisbecker 	case 'c':
1036fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
1037fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1038fef20d9cSFrederic Weisbecker 
1039fef20d9cSFrederic Weisbecker 	case 's':
1040fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
1041fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1042fef20d9cSFrederic Weisbecker 
1043fef20d9cSFrederic Weisbecker 	case 'p':
1044fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
1045fef20d9cSFrederic Weisbecker 		return fmt - start;
1046fef20d9cSFrederic Weisbecker 		/* skip alnum */
1047fef20d9cSFrederic Weisbecker 
1048fef20d9cSFrederic Weisbecker 	case 'n':
1049fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NRCHARS;
1050fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1051fef20d9cSFrederic Weisbecker 
1052fef20d9cSFrederic Weisbecker 	case '%':
1053fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1054fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1055fef20d9cSFrederic Weisbecker 
1056fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1057fef20d9cSFrederic Weisbecker 	case 'o':
1058fef20d9cSFrederic Weisbecker 		spec->base = 8;
1059fef20d9cSFrederic Weisbecker 		break;
1060fef20d9cSFrederic Weisbecker 
1061fef20d9cSFrederic Weisbecker 	case 'x':
1062fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1063fef20d9cSFrederic Weisbecker 
1064fef20d9cSFrederic Weisbecker 	case 'X':
1065fef20d9cSFrederic Weisbecker 		spec->base = 16;
1066fef20d9cSFrederic Weisbecker 		break;
1067fef20d9cSFrederic Weisbecker 
1068fef20d9cSFrederic Weisbecker 	case 'd':
1069fef20d9cSFrederic Weisbecker 	case 'i':
107039e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1071fef20d9cSFrederic Weisbecker 	case 'u':
1072fef20d9cSFrederic Weisbecker 		break;
1073fef20d9cSFrederic Weisbecker 
1074fef20d9cSFrederic Weisbecker 	default:
1075fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1076fef20d9cSFrederic Weisbecker 		return fmt - start;
1077fef20d9cSFrederic Weisbecker 	}
1078fef20d9cSFrederic Weisbecker 
1079fef20d9cSFrederic Weisbecker 	if (spec->qualifier == 'L')
1080fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1081fef20d9cSFrederic Weisbecker 	else if (spec->qualifier == 'l') {
108239e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1083fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_LONG;
1084fef20d9cSFrederic Weisbecker 		else
1085fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_ULONG;
108608562cb2SAndré Goddard Rosa 	} else if (TOLOWER(spec->qualifier) == 'z') {
1087fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1088fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 't') {
1089fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1090a4e94ef0SZhaolei 	} else if (spec->qualifier == 'H') {
1091a4e94ef0SZhaolei 		if (spec->flags & SIGN)
1092a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_BYTE;
1093a4e94ef0SZhaolei 		else
1094a4e94ef0SZhaolei 			spec->type = FORMAT_TYPE_UBYTE;
1095fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'h') {
109639e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1097fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_SHORT;
1098fef20d9cSFrederic Weisbecker 		else
1099fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_USHORT;
1100fef20d9cSFrederic Weisbecker 	} else {
110139e874f8SFrederic Weisbecker 		if (spec->flags & SIGN)
1102fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_INT;
1103fef20d9cSFrederic Weisbecker 		else
1104fef20d9cSFrederic Weisbecker 			spec->type = FORMAT_TYPE_UINT;
1105fef20d9cSFrederic Weisbecker 	}
1106fef20d9cSFrederic Weisbecker 
1107fef20d9cSFrederic Weisbecker 	return ++fmt - start;
110878a8bf69SLinus Torvalds }
110978a8bf69SLinus Torvalds 
11101da177e4SLinus Torvalds /**
11111da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
11121da177e4SLinus Torvalds  * @buf: The buffer to place the result into
11131da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
11141da177e4SLinus Torvalds  * @fmt: The format string to use
11151da177e4SLinus Torvalds  * @args: Arguments for the format string
11161da177e4SLinus Torvalds  *
111720036fdcSAndi Kleen  * This function follows C99 vsnprintf, but has some extensions:
111891adcd2cSSteven Rostedt  * %pS output the name of a text symbol with offset
111991adcd2cSSteven Rostedt  * %ps output the name of a text symbol without offset
11200c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
11210c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
1122332d2e78SLinus Torvalds  * %pR output the address range in a struct resource
11230efb4d20SSteven Rostedt  * %n is ignored
112420036fdcSAndi Kleen  *
11251da177e4SLinus Torvalds  * The return value is the number of characters which would
11261da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
11271da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
11281da177e4SLinus Torvalds  * number of characters written into @buf as return value
112972fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
11301da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
11311da177e4SLinus Torvalds  * string is truncated.
11321da177e4SLinus Torvalds  *
11331da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
113472fd4a35SRobert P. J. Day  * You probably want snprintf() instead.
11351da177e4SLinus Torvalds  */
11361da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
11371da177e4SLinus Torvalds {
11381da177e4SLinus Torvalds 	unsigned long long num;
1139d4be151bSAndré Goddard Rosa 	char *str, *end;
1140fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
11411da177e4SLinus Torvalds 
1142f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1143f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
11442f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
11451da177e4SLinus Torvalds 		return 0;
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	str = buf;
1148f796937aSJeremy Fitzhardinge 	end = buf + size;
11491da177e4SLinus Torvalds 
1150f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1151f796937aSJeremy Fitzhardinge 	if (end < buf) {
11521da177e4SLinus Torvalds 		end = ((void *)-1);
1153f796937aSJeremy Fitzhardinge 		size = end - buf;
11541da177e4SLinus Torvalds 	}
11551da177e4SLinus Torvalds 
1156fef20d9cSFrederic Weisbecker 	while (*fmt) {
1157fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1158d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1159fef20d9cSFrederic Weisbecker 
1160fef20d9cSFrederic Weisbecker 		fmt += read;
1161fef20d9cSFrederic Weisbecker 
1162fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1163fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1164fef20d9cSFrederic Weisbecker 			int copy = read;
1165fef20d9cSFrederic Weisbecker 			if (str < end) {
1166fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1167fef20d9cSFrederic Weisbecker 					copy = end - str;
1168fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1169fef20d9cSFrederic Weisbecker 			}
1170fef20d9cSFrederic Weisbecker 			str += read;
1171fef20d9cSFrederic Weisbecker 			break;
11721da177e4SLinus Torvalds 		}
11731da177e4SLinus Torvalds 
1174ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1175fef20d9cSFrederic Weisbecker 			spec.field_width = va_arg(args, int);
1176fef20d9cSFrederic Weisbecker 			break;
11771da177e4SLinus Torvalds 
1178fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1179fef20d9cSFrederic Weisbecker 			spec.precision = va_arg(args, int);
1180fef20d9cSFrederic Weisbecker 			break;
11811da177e4SLinus Torvalds 
1182d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1183d4be151bSAndré Goddard Rosa 			char c;
1184d4be151bSAndré Goddard Rosa 
1185fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1186fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1187f796937aSJeremy Fitzhardinge 					if (str < end)
11881da177e4SLinus Torvalds 						*str = ' ';
11891da177e4SLinus Torvalds 					++str;
1190fef20d9cSFrederic Weisbecker 
11911da177e4SLinus Torvalds 				}
11921da177e4SLinus Torvalds 			}
11931da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1194f796937aSJeremy Fitzhardinge 			if (str < end)
11951da177e4SLinus Torvalds 				*str = c;
11961da177e4SLinus Torvalds 			++str;
1197fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1198f796937aSJeremy Fitzhardinge 				if (str < end)
11991da177e4SLinus Torvalds 					*str = ' ';
12001da177e4SLinus Torvalds 				++str;
12011da177e4SLinus Torvalds 			}
1202fef20d9cSFrederic Weisbecker 			break;
1203d4be151bSAndré Goddard Rosa 		}
12041da177e4SLinus Torvalds 
1205fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1206fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1207fef20d9cSFrederic Weisbecker 			break;
12081da177e4SLinus Torvalds 
1209fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1210fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, va_arg(args, void *),
1211fef20d9cSFrederic Weisbecker 				      spec);
1212fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
12134d8a743cSLinus Torvalds 				fmt++;
1214fef20d9cSFrederic Weisbecker 			break;
12151da177e4SLinus Torvalds 
1216fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1217f796937aSJeremy Fitzhardinge 			if (str < end)
12181da177e4SLinus Torvalds 				*str = '%';
12191da177e4SLinus Torvalds 			++str;
12201da177e4SLinus Torvalds 			break;
12211da177e4SLinus Torvalds 
1222fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1223f796937aSJeremy Fitzhardinge 			if (str < end)
12241da177e4SLinus Torvalds 				*str = '%';
12251da177e4SLinus Torvalds 			++str;
1226fef20d9cSFrederic Weisbecker 			break;
1227fef20d9cSFrederic Weisbecker 
1228fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
1229fef20d9cSFrederic Weisbecker 			int qualifier = spec.qualifier;
1230fef20d9cSFrederic Weisbecker 
1231fef20d9cSFrederic Weisbecker 			if (qualifier == 'l') {
1232fef20d9cSFrederic Weisbecker 				long *ip = va_arg(args, long *);
1233fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
123408562cb2SAndré Goddard Rosa 			} else if (TOLOWER(qualifier) == 'z') {
1235fef20d9cSFrederic Weisbecker 				size_t *ip = va_arg(args, size_t *);
1236fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
12371da177e4SLinus Torvalds 			} else {
1238fef20d9cSFrederic Weisbecker 				int *ip = va_arg(args, int *);
1239fef20d9cSFrederic Weisbecker 				*ip = (str - buf);
1240fef20d9cSFrederic Weisbecker 			}
1241fef20d9cSFrederic Weisbecker 			break;
1242fef20d9cSFrederic Weisbecker 		}
1243fef20d9cSFrederic Weisbecker 
1244fef20d9cSFrederic Weisbecker 		default:
1245fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1246fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1247fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1248fef20d9cSFrederic Weisbecker 				break;
1249fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1250fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
1251fef20d9cSFrederic Weisbecker 				break;
1252fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1253fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
1254fef20d9cSFrederic Weisbecker 				break;
1255fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1256fef20d9cSFrederic Weisbecker 				num = va_arg(args, size_t);
1257fef20d9cSFrederic Weisbecker 				break;
1258fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1259fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
1260fef20d9cSFrederic Weisbecker 				break;
1261a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1262a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
1263a4e94ef0SZhaolei 				break;
1264a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1265a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
1266a4e94ef0SZhaolei 				break;
1267fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1268fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
1269fef20d9cSFrederic Weisbecker 				break;
1270fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1271fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
1272fef20d9cSFrederic Weisbecker 				break;
127339e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
127439e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
1275fef20d9cSFrederic Weisbecker 				break;
1276fef20d9cSFrederic Weisbecker 			default:
1277fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
12781da177e4SLinus Torvalds 			}
1279fef20d9cSFrederic Weisbecker 
1280fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
12811da177e4SLinus Torvalds 		}
1282fef20d9cSFrederic Weisbecker 	}
1283fef20d9cSFrederic Weisbecker 
1284f796937aSJeremy Fitzhardinge 	if (size > 0) {
1285f796937aSJeremy Fitzhardinge 		if (str < end)
12861da177e4SLinus Torvalds 			*str = '\0';
1287f796937aSJeremy Fitzhardinge 		else
12880a6047eeSLinus Torvalds 			end[-1] = '\0';
1289f796937aSJeremy Fitzhardinge 	}
1290fef20d9cSFrederic Weisbecker 
1291f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
12921da177e4SLinus Torvalds 	return str-buf;
1293fef20d9cSFrederic Weisbecker 
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds /**
12981da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
12991da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13001da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
13011da177e4SLinus Torvalds  * @fmt: The format string to use
13021da177e4SLinus Torvalds  * @args: Arguments for the format string
13031da177e4SLinus Torvalds  *
13041da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
13051da177e4SLinus Torvalds  * the @buf not including the trailing '\0'. If @size is <= 0 the function
13061da177e4SLinus Torvalds  * returns 0.
13071da177e4SLinus Torvalds  *
13081da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
130972fd4a35SRobert P. J. Day  * You probably want scnprintf() instead.
131020036fdcSAndi Kleen  *
131120036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
13121da177e4SLinus Torvalds  */
13131da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
13141da177e4SLinus Torvalds {
13151da177e4SLinus Torvalds 	int i;
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
13187b9186f5SAndré Goddard Rosa 
13191da177e4SLinus Torvalds 	return (i >= size) ? (size - 1) : i;
13201da177e4SLinus Torvalds }
13211da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds /**
13241da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
13251da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13261da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
13271da177e4SLinus Torvalds  * @fmt: The format string to use
13281da177e4SLinus Torvalds  * @...: Arguments for the format string
13291da177e4SLinus Torvalds  *
13301da177e4SLinus Torvalds  * The return value is the number of characters which would be
13311da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
13321da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
13331da177e4SLinus Torvalds  * @size, the resulting string is truncated.
133420036fdcSAndi Kleen  *
133520036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
13361da177e4SLinus Torvalds  */
13371da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
13381da177e4SLinus Torvalds {
13391da177e4SLinus Torvalds 	va_list args;
13401da177e4SLinus Torvalds 	int i;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	va_start(args, fmt);
13431da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
13441da177e4SLinus Torvalds 	va_end(args);
13457b9186f5SAndré Goddard Rosa 
13461da177e4SLinus Torvalds 	return i;
13471da177e4SLinus Torvalds }
13481da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds /**
13511da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
13521da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13531da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
13541da177e4SLinus Torvalds  * @fmt: The format string to use
13551da177e4SLinus Torvalds  * @...: Arguments for the format string
13561da177e4SLinus Torvalds  *
13571da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
1358ea6f3281SMartin Peschke  * the trailing '\0'. If @size is <= 0 the function returns 0.
13591da177e4SLinus Torvalds  */
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
13621da177e4SLinus Torvalds {
13631da177e4SLinus Torvalds 	va_list args;
13641da177e4SLinus Torvalds 	int i;
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	va_start(args, fmt);
13671da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
13681da177e4SLinus Torvalds 	va_end(args);
13697b9186f5SAndré Goddard Rosa 
13701da177e4SLinus Torvalds 	return (i >= size) ? (size - 1) : i;
13711da177e4SLinus Torvalds }
13721da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds /**
13751da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
13761da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13771da177e4SLinus Torvalds  * @fmt: The format string to use
13781da177e4SLinus Torvalds  * @args: Arguments for the format string
13791da177e4SLinus Torvalds  *
13801da177e4SLinus Torvalds  * The function returns the number of characters written
138172fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
13821da177e4SLinus Torvalds  * buffer overflows.
13831da177e4SLinus Torvalds  *
13841da177e4SLinus Torvalds  * Call this function if you are already dealing with a va_list.
138572fd4a35SRobert P. J. Day  * You probably want sprintf() instead.
138620036fdcSAndi Kleen  *
138720036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
13881da177e4SLinus Torvalds  */
13891da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
13901da177e4SLinus Torvalds {
13911da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds /**
13961da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
13971da177e4SLinus Torvalds  * @buf: The buffer to place the result into
13981da177e4SLinus Torvalds  * @fmt: The format string to use
13991da177e4SLinus Torvalds  * @...: Arguments for the format string
14001da177e4SLinus Torvalds  *
14011da177e4SLinus Torvalds  * The function returns the number of characters written
140272fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
14031da177e4SLinus Torvalds  * buffer overflows.
140420036fdcSAndi Kleen  *
140520036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
14061da177e4SLinus Torvalds  */
14071da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
14081da177e4SLinus Torvalds {
14091da177e4SLinus Torvalds 	va_list args;
14101da177e4SLinus Torvalds 	int i;
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	va_start(args, fmt);
14131da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
14141da177e4SLinus Torvalds 	va_end(args);
14157b9186f5SAndré Goddard Rosa 
14161da177e4SLinus Torvalds 	return i;
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
14191da177e4SLinus Torvalds 
14204370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
14214370aa4aSLai Jiangshan /*
14224370aa4aSLai Jiangshan  * bprintf service:
14234370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
14244370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
14254370aa4aSLai Jiangshan  */
14264370aa4aSLai Jiangshan 
14274370aa4aSLai Jiangshan /**
14284370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
14294370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
14304370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
14314370aa4aSLai Jiangshan  * @fmt: The format string to use
14324370aa4aSLai Jiangshan  * @args: Arguments for the format string
14334370aa4aSLai Jiangshan  *
14344370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
14354370aa4aSLai Jiangshan  * is skiped.
14364370aa4aSLai Jiangshan  *
14374370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
14384370aa4aSLai Jiangshan  * the given input.
14394370aa4aSLai Jiangshan  *
14404370aa4aSLai Jiangshan  * NOTE:
14414370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
14424370aa4aSLai Jiangshan  * valid for bstr_printf().
14434370aa4aSLai Jiangshan  */
14444370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
14454370aa4aSLai Jiangshan {
1446fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
14474370aa4aSLai Jiangshan 	char *str, *end;
14484370aa4aSLai Jiangshan 
14494370aa4aSLai Jiangshan 	str = (char *)bin_buf;
14504370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
14514370aa4aSLai Jiangshan 
14524370aa4aSLai Jiangshan #define save_arg(type)							\
14534370aa4aSLai Jiangshan do {									\
14544370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
14554370aa4aSLai Jiangshan 		unsigned long long value;				\
14564370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
14574370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
14584370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
14594370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
14604370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
14614370aa4aSLai Jiangshan 		}							\
14624370aa4aSLai Jiangshan 	} else {							\
14634370aa4aSLai Jiangshan 		unsigned long value;					\
14644370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
14654370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
14664370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
14674370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
14684370aa4aSLai Jiangshan 	}								\
14694370aa4aSLai Jiangshan 	str += sizeof(type);						\
14704370aa4aSLai Jiangshan } while (0)
14714370aa4aSLai Jiangshan 
1472fef20d9cSFrederic Weisbecker 	while (*fmt) {
1473d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
14744370aa4aSLai Jiangshan 
1475fef20d9cSFrederic Weisbecker 		fmt += read;
1476fef20d9cSFrederic Weisbecker 
1477fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1478fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
1479d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_INVALID:
1480d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
1481fef20d9cSFrederic Weisbecker 			break;
1482fef20d9cSFrederic Weisbecker 
1483ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1484fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
14854370aa4aSLai Jiangshan 			save_arg(int);
1486fef20d9cSFrederic Weisbecker 			break;
14874370aa4aSLai Jiangshan 
1488fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
14894370aa4aSLai Jiangshan 			save_arg(char);
1490fef20d9cSFrederic Weisbecker 			break;
1491fef20d9cSFrederic Weisbecker 
1492fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
14934370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
14944370aa4aSLai Jiangshan 			size_t len;
14956c356634SAndré Goddard Rosa 
14964370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
14974370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
14980f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
14996c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
15006c356634SAndré Goddard Rosa 			if (str + len < end)
15016c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
15026c356634SAndré Goddard Rosa 			str += len;
1503fef20d9cSFrederic Weisbecker 			break;
15044370aa4aSLai Jiangshan 		}
1505fef20d9cSFrederic Weisbecker 
1506fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
15074370aa4aSLai Jiangshan 			save_arg(void *);
15084370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
1509fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
15104370aa4aSLai Jiangshan 				fmt++;
1511fef20d9cSFrederic Weisbecker 			break;
1512fef20d9cSFrederic Weisbecker 
1513fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS: {
15144370aa4aSLai Jiangshan 			/* skip %n 's argument */
1515fef20d9cSFrederic Weisbecker 			int qualifier = spec.qualifier;
15164370aa4aSLai Jiangshan 			void *skip_arg;
15174370aa4aSLai Jiangshan 			if (qualifier == 'l')
15184370aa4aSLai Jiangshan 				skip_arg = va_arg(args, long *);
151908562cb2SAndré Goddard Rosa 			else if (TOLOWER(qualifier) == 'z')
15204370aa4aSLai Jiangshan 				skip_arg = va_arg(args, size_t *);
15214370aa4aSLai Jiangshan 			else
15224370aa4aSLai Jiangshan 				skip_arg = va_arg(args, int *);
1523fef20d9cSFrederic Weisbecker 			break;
15244370aa4aSLai Jiangshan 		}
15254370aa4aSLai Jiangshan 
1526fef20d9cSFrederic Weisbecker 		default:
1527fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1528fef20d9cSFrederic Weisbecker 
1529fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1530fef20d9cSFrederic Weisbecker 				save_arg(long long);
1531fef20d9cSFrederic Weisbecker 				break;
1532fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1533fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1534fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
1535fef20d9cSFrederic Weisbecker 				break;
1536fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1537fef20d9cSFrederic Weisbecker 				save_arg(size_t);
1538fef20d9cSFrederic Weisbecker 				break;
1539fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1540fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
1541fef20d9cSFrederic Weisbecker 				break;
1542a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1543a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1544a4e94ef0SZhaolei 				save_arg(char);
1545a4e94ef0SZhaolei 				break;
1546fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1547fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1548fef20d9cSFrederic Weisbecker 				save_arg(short);
1549fef20d9cSFrederic Weisbecker 				break;
1550fef20d9cSFrederic Weisbecker 			default:
1551fef20d9cSFrederic Weisbecker 				save_arg(int);
1552fef20d9cSFrederic Weisbecker 			}
1553fef20d9cSFrederic Weisbecker 		}
1554fef20d9cSFrederic Weisbecker 	}
1555fef20d9cSFrederic Weisbecker 
15567b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1557fef20d9cSFrederic Weisbecker #undef save_arg
15584370aa4aSLai Jiangshan }
15594370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
15604370aa4aSLai Jiangshan 
15614370aa4aSLai Jiangshan /**
15624370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
15634370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
15644370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
15654370aa4aSLai Jiangshan  * @fmt: The format string to use
15664370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
15674370aa4aSLai Jiangshan  *
15684370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
15694370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
15704370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
15714370aa4aSLai Jiangshan  *
15724370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
15730efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
15744370aa4aSLai Jiangshan  *
15754370aa4aSLai Jiangshan  * The return value is the number of characters which would
15764370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
15774370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
15784370aa4aSLai Jiangshan  * number of characters written into @buf as return value
15794370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
15804370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
15814370aa4aSLai Jiangshan  * string is truncated.
15824370aa4aSLai Jiangshan  */
15834370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
15844370aa4aSLai Jiangshan {
1585fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
1586d4be151bSAndré Goddard Rosa 	char *str, *end;
1587d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
15884370aa4aSLai Jiangshan 
15892f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
15904370aa4aSLai Jiangshan 		return 0;
15914370aa4aSLai Jiangshan 
15924370aa4aSLai Jiangshan 	str = buf;
15934370aa4aSLai Jiangshan 	end = buf + size;
15944370aa4aSLai Jiangshan 
15954370aa4aSLai Jiangshan #define get_arg(type)							\
15964370aa4aSLai Jiangshan ({									\
15974370aa4aSLai Jiangshan 	typeof(type) value;						\
15984370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
15994370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
16004370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
16014370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
16024370aa4aSLai Jiangshan 	} else {							\
16034370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
16044370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
16054370aa4aSLai Jiangshan 	}								\
16064370aa4aSLai Jiangshan 	args += sizeof(type);						\
16074370aa4aSLai Jiangshan 	value;								\
16084370aa4aSLai Jiangshan })
16094370aa4aSLai Jiangshan 
16104370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
16114370aa4aSLai Jiangshan 	if (end < buf) {
16124370aa4aSLai Jiangshan 		end = ((void *)-1);
16134370aa4aSLai Jiangshan 		size = end - buf;
16144370aa4aSLai Jiangshan 	}
16154370aa4aSLai Jiangshan 
1616fef20d9cSFrederic Weisbecker 	while (*fmt) {
1617fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1618d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1619fef20d9cSFrederic Weisbecker 
1620fef20d9cSFrederic Weisbecker 		fmt += read;
1621fef20d9cSFrederic Weisbecker 
1622fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1623fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1624fef20d9cSFrederic Weisbecker 			int copy = read;
1625fef20d9cSFrederic Weisbecker 			if (str < end) {
1626fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1627fef20d9cSFrederic Weisbecker 					copy = end - str;
1628fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1629fef20d9cSFrederic Weisbecker 			}
1630fef20d9cSFrederic Weisbecker 			str += read;
1631fef20d9cSFrederic Weisbecker 			break;
16324370aa4aSLai Jiangshan 		}
16334370aa4aSLai Jiangshan 
1634ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1635fef20d9cSFrederic Weisbecker 			spec.field_width = get_arg(int);
1636fef20d9cSFrederic Weisbecker 			break;
16374370aa4aSLai Jiangshan 
1638fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1639fef20d9cSFrederic Weisbecker 			spec.precision = get_arg(int);
1640fef20d9cSFrederic Weisbecker 			break;
16414370aa4aSLai Jiangshan 
1642d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1643d4be151bSAndré Goddard Rosa 			char c;
1644d4be151bSAndré Goddard Rosa 
1645fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1646fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
16474370aa4aSLai Jiangshan 					if (str < end)
16484370aa4aSLai Jiangshan 						*str = ' ';
16494370aa4aSLai Jiangshan 					++str;
16504370aa4aSLai Jiangshan 				}
16514370aa4aSLai Jiangshan 			}
16524370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
16534370aa4aSLai Jiangshan 			if (str < end)
16544370aa4aSLai Jiangshan 				*str = c;
16554370aa4aSLai Jiangshan 			++str;
1656fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
16574370aa4aSLai Jiangshan 				if (str < end)
16584370aa4aSLai Jiangshan 					*str = ' ';
16594370aa4aSLai Jiangshan 				++str;
16604370aa4aSLai Jiangshan 			}
1661fef20d9cSFrederic Weisbecker 			break;
1662d4be151bSAndré Goddard Rosa 		}
16634370aa4aSLai Jiangshan 
1664fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
16654370aa4aSLai Jiangshan 			const char *str_arg = args;
1666d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
1667fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
1668fef20d9cSFrederic Weisbecker 			break;
16694370aa4aSLai Jiangshan 		}
16704370aa4aSLai Jiangshan 
1671fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1672fef20d9cSFrederic Weisbecker 			str = pointer(fmt+1, str, end, get_arg(void *), spec);
1673fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
16744370aa4aSLai Jiangshan 				fmt++;
1675fef20d9cSFrederic Weisbecker 			break;
16764370aa4aSLai Jiangshan 
1677fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1678fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
16794370aa4aSLai Jiangshan 			if (str < end)
16804370aa4aSLai Jiangshan 				*str = '%';
16814370aa4aSLai Jiangshan 			++str;
1682fef20d9cSFrederic Weisbecker 			break;
1683fef20d9cSFrederic Weisbecker 
1684fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NRCHARS:
1685fef20d9cSFrederic Weisbecker 			/* skip */
1686fef20d9cSFrederic Weisbecker 			break;
1687fef20d9cSFrederic Weisbecker 
1688d4be151bSAndré Goddard Rosa 		default: {
1689d4be151bSAndré Goddard Rosa 			unsigned long long num;
1690d4be151bSAndré Goddard Rosa 
1691fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1692fef20d9cSFrederic Weisbecker 
1693fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
16944370aa4aSLai Jiangshan 				num = get_arg(long long);
1695fef20d9cSFrederic Weisbecker 				break;
1696fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1697fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1698fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
1699fef20d9cSFrederic Weisbecker 				break;
1700fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
17014370aa4aSLai Jiangshan 				num = get_arg(size_t);
1702fef20d9cSFrederic Weisbecker 				break;
1703fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
17044370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
1705fef20d9cSFrederic Weisbecker 				break;
1706a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1707a4e94ef0SZhaolei 				num = get_arg(unsigned char);
1708a4e94ef0SZhaolei 				break;
1709a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1710a4e94ef0SZhaolei 				num = get_arg(signed char);
1711a4e94ef0SZhaolei 				break;
1712fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1713fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
1714fef20d9cSFrederic Weisbecker 				break;
1715fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1716fef20d9cSFrederic Weisbecker 				num = get_arg(short);
1717fef20d9cSFrederic Weisbecker 				break;
1718fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
17194370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
1720fef20d9cSFrederic Weisbecker 				break;
1721fef20d9cSFrederic Weisbecker 			default:
1722fef20d9cSFrederic Weisbecker 				num = get_arg(int);
17234370aa4aSLai Jiangshan 			}
1724fef20d9cSFrederic Weisbecker 
1725fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
1726d4be151bSAndré Goddard Rosa 		} /* default: */
1727d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
1728d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
1729fef20d9cSFrederic Weisbecker 
17304370aa4aSLai Jiangshan 	if (size > 0) {
17314370aa4aSLai Jiangshan 		if (str < end)
17324370aa4aSLai Jiangshan 			*str = '\0';
17334370aa4aSLai Jiangshan 		else
17344370aa4aSLai Jiangshan 			end[-1] = '\0';
17354370aa4aSLai Jiangshan 	}
1736fef20d9cSFrederic Weisbecker 
17374370aa4aSLai Jiangshan #undef get_arg
17384370aa4aSLai Jiangshan 
17394370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
17404370aa4aSLai Jiangshan 	return str - buf;
17414370aa4aSLai Jiangshan }
17424370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
17434370aa4aSLai Jiangshan 
17444370aa4aSLai Jiangshan /**
17454370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
17464370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
17474370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
17484370aa4aSLai Jiangshan  * @fmt: The format string to use
17494370aa4aSLai Jiangshan  * @...: Arguments for the format string
17504370aa4aSLai Jiangshan  *
17514370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
17524370aa4aSLai Jiangshan  * into @bin_buf.
17534370aa4aSLai Jiangshan  */
17544370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
17554370aa4aSLai Jiangshan {
17564370aa4aSLai Jiangshan 	va_list args;
17574370aa4aSLai Jiangshan 	int ret;
17584370aa4aSLai Jiangshan 
17594370aa4aSLai Jiangshan 	va_start(args, fmt);
17604370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
17614370aa4aSLai Jiangshan 	va_end(args);
17627b9186f5SAndré Goddard Rosa 
17634370aa4aSLai Jiangshan 	return ret;
17644370aa4aSLai Jiangshan }
17654370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
17664370aa4aSLai Jiangshan 
17674370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
17684370aa4aSLai Jiangshan 
17691da177e4SLinus Torvalds /**
17701da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
17711da177e4SLinus Torvalds  * @buf:	input buffer
17721da177e4SLinus Torvalds  * @fmt:	format of buffer
17731da177e4SLinus Torvalds  * @args:	arguments
17741da177e4SLinus Torvalds  */
17751da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
17761da177e4SLinus Torvalds {
17771da177e4SLinus Torvalds 	const char *str = buf;
17781da177e4SLinus Torvalds 	char *next;
17791da177e4SLinus Torvalds 	char digit;
17801da177e4SLinus Torvalds 	int num = 0;
17817b9186f5SAndré Goddard Rosa 	int qualifier, base, field_width;
1782d4be151bSAndré Goddard Rosa 	bool is_sign;
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	while (*fmt && *str) {
17851da177e4SLinus Torvalds 		/* skip any white space in format */
17861da177e4SLinus Torvalds 		/* white space in format matchs any amount of
17871da177e4SLinus Torvalds 		 * white space, including none, in the input.
17881da177e4SLinus Torvalds 		 */
17891da177e4SLinus Torvalds 		if (isspace(*fmt)) {
1790*e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
1791*e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
17921da177e4SLinus Torvalds 		}
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
17951da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
17961da177e4SLinus Torvalds 			if (*fmt++ != *str++)
17971da177e4SLinus Torvalds 				break;
17981da177e4SLinus Torvalds 			continue;
17991da177e4SLinus Torvalds 		}
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds 		if (!*fmt)
18021da177e4SLinus Torvalds 			break;
18031da177e4SLinus Torvalds 		++fmt;
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 		/* skip this conversion.
18061da177e4SLinus Torvalds 		 * advance both strings to next white space
18071da177e4SLinus Torvalds 		 */
18081da177e4SLinus Torvalds 		if (*fmt == '*') {
18098fccae2cSAndy Spencer 			while (!isspace(*fmt) && *fmt != '%' && *fmt)
18101da177e4SLinus Torvalds 				fmt++;
18111da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
18121da177e4SLinus Torvalds 				str++;
18131da177e4SLinus Torvalds 			continue;
18141da177e4SLinus Torvalds 		}
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds 		/* get field width */
18171da177e4SLinus Torvalds 		field_width = -1;
18181da177e4SLinus Torvalds 		if (isdigit(*fmt))
18191da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 		/* get conversion qualifier */
18221da177e4SLinus Torvalds 		qualifier = -1;
182308562cb2SAndré Goddard Rosa 		if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
182408562cb2SAndré Goddard Rosa 		    TOLOWER(*fmt) == 'z') {
18251da177e4SLinus Torvalds 			qualifier = *fmt++;
18261da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
18271da177e4SLinus Torvalds 				if (qualifier == 'h') {
18281da177e4SLinus Torvalds 					qualifier = 'H';
18291da177e4SLinus Torvalds 					fmt++;
18301da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
18311da177e4SLinus Torvalds 					qualifier = 'L';
18321da177e4SLinus Torvalds 					fmt++;
18331da177e4SLinus Torvalds 				}
18341da177e4SLinus Torvalds 			}
18351da177e4SLinus Torvalds 		}
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 		if (!*fmt || !*str)
18381da177e4SLinus Torvalds 			break;
18391da177e4SLinus Torvalds 
1840d4be151bSAndré Goddard Rosa 		base = 10;
1841d4be151bSAndré Goddard Rosa 		is_sign = 0;
1842d4be151bSAndré Goddard Rosa 
18431da177e4SLinus Torvalds 		switch (*fmt++) {
18441da177e4SLinus Torvalds 		case 'c':
18451da177e4SLinus Torvalds 		{
18461da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
18471da177e4SLinus Torvalds 			if (field_width == -1)
18481da177e4SLinus Torvalds 				field_width = 1;
18491da177e4SLinus Torvalds 			do {
18501da177e4SLinus Torvalds 				*s++ = *str++;
18511da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
18521da177e4SLinus Torvalds 			num++;
18531da177e4SLinus Torvalds 		}
18541da177e4SLinus Torvalds 		continue;
18551da177e4SLinus Torvalds 		case 's':
18561da177e4SLinus Torvalds 		{
18571da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
18581da177e4SLinus Torvalds 			if (field_width == -1)
18591da177e4SLinus Torvalds 				field_width = INT_MAX;
18601da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
1861*e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 			/* now copy until next white space */
18647b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
18651da177e4SLinus Torvalds 				*s++ = *str++;
18661da177e4SLinus Torvalds 			*s = '\0';
18671da177e4SLinus Torvalds 			num++;
18681da177e4SLinus Torvalds 		}
18691da177e4SLinus Torvalds 		continue;
18701da177e4SLinus Torvalds 		case 'n':
18711da177e4SLinus Torvalds 			/* return number of characters read so far */
18721da177e4SLinus Torvalds 		{
18731da177e4SLinus Torvalds 			int *i = (int *)va_arg(args, int*);
18741da177e4SLinus Torvalds 			*i = str - buf;
18751da177e4SLinus Torvalds 		}
18761da177e4SLinus Torvalds 		continue;
18771da177e4SLinus Torvalds 		case 'o':
18781da177e4SLinus Torvalds 			base = 8;
18791da177e4SLinus Torvalds 			break;
18801da177e4SLinus Torvalds 		case 'x':
18811da177e4SLinus Torvalds 		case 'X':
18821da177e4SLinus Torvalds 			base = 16;
18831da177e4SLinus Torvalds 			break;
18841da177e4SLinus Torvalds 		case 'i':
18851da177e4SLinus Torvalds 			base = 0;
18861da177e4SLinus Torvalds 		case 'd':
18871da177e4SLinus Torvalds 			is_sign = 1;
18881da177e4SLinus Torvalds 		case 'u':
18891da177e4SLinus Torvalds 			break;
18901da177e4SLinus Torvalds 		case '%':
18911da177e4SLinus Torvalds 			/* looking for '%' in str */
18921da177e4SLinus Torvalds 			if (*str++ != '%')
18931da177e4SLinus Torvalds 				return num;
18941da177e4SLinus Torvalds 			continue;
18951da177e4SLinus Torvalds 		default:
18961da177e4SLinus Torvalds 			/* invalid format; stop here */
18971da177e4SLinus Torvalds 			return num;
18981da177e4SLinus Torvalds 		}
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 		/* have some sort of integer conversion.
19011da177e4SLinus Torvalds 		 * first, skip white space in buffer.
19021da177e4SLinus Torvalds 		 */
1903*e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
19041da177e4SLinus Torvalds 
19051da177e4SLinus Torvalds 		digit = *str;
19061da177e4SLinus Torvalds 		if (is_sign && digit == '-')
19071da177e4SLinus Torvalds 			digit = *(str + 1);
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds 		if (!digit
19101da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
19111da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
19121da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
19131da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
19141da177e4SLinus Torvalds 			break;
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds 		switch (qualifier) {
19171da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
19181da177e4SLinus Torvalds 			if (is_sign) {
19191da177e4SLinus Torvalds 				signed char *s = (signed char *)va_arg(args, signed char *);
19201da177e4SLinus Torvalds 				*s = (signed char)simple_strtol(str, &next, base);
19211da177e4SLinus Torvalds 			} else {
19221da177e4SLinus Torvalds 				unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
19231da177e4SLinus Torvalds 				*s = (unsigned char)simple_strtoul(str, &next, base);
19241da177e4SLinus Torvalds 			}
19251da177e4SLinus Torvalds 			break;
19261da177e4SLinus Torvalds 		case 'h':
19271da177e4SLinus Torvalds 			if (is_sign) {
19281da177e4SLinus Torvalds 				short *s = (short *)va_arg(args, short *);
19291da177e4SLinus Torvalds 				*s = (short)simple_strtol(str, &next, base);
19301da177e4SLinus Torvalds 			} else {
19311da177e4SLinus Torvalds 				unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
19321da177e4SLinus Torvalds 				*s = (unsigned short)simple_strtoul(str, &next, base);
19331da177e4SLinus Torvalds 			}
19341da177e4SLinus Torvalds 			break;
19351da177e4SLinus Torvalds 		case 'l':
19361da177e4SLinus Torvalds 			if (is_sign) {
19371da177e4SLinus Torvalds 				long *l = (long *)va_arg(args, long *);
19381da177e4SLinus Torvalds 				*l = simple_strtol(str, &next, base);
19391da177e4SLinus Torvalds 			} else {
19401da177e4SLinus Torvalds 				unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
19411da177e4SLinus Torvalds 				*l = simple_strtoul(str, &next, base);
19421da177e4SLinus Torvalds 			}
19431da177e4SLinus Torvalds 			break;
19441da177e4SLinus Torvalds 		case 'L':
19451da177e4SLinus Torvalds 			if (is_sign) {
19461da177e4SLinus Torvalds 				long long *l = (long long *)va_arg(args, long long *);
19471da177e4SLinus Torvalds 				*l = simple_strtoll(str, &next, base);
19481da177e4SLinus Torvalds 			} else {
19491da177e4SLinus Torvalds 				unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
19501da177e4SLinus Torvalds 				*l = simple_strtoull(str, &next, base);
19511da177e4SLinus Torvalds 			}
19521da177e4SLinus Torvalds 			break;
19531da177e4SLinus Torvalds 		case 'Z':
19541da177e4SLinus Torvalds 		case 'z':
19551da177e4SLinus Torvalds 		{
19561da177e4SLinus Torvalds 			size_t *s = (size_t *)va_arg(args, size_t *);
19571da177e4SLinus Torvalds 			*s = (size_t)simple_strtoul(str, &next, base);
19581da177e4SLinus Torvalds 		}
19591da177e4SLinus Torvalds 		break;
19601da177e4SLinus Torvalds 		default:
19611da177e4SLinus Torvalds 			if (is_sign) {
19621da177e4SLinus Torvalds 				int *i = (int *)va_arg(args, int *);
19631da177e4SLinus Torvalds 				*i = (int)simple_strtol(str, &next, base);
19641da177e4SLinus Torvalds 			} else {
19651da177e4SLinus Torvalds 				unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
19661da177e4SLinus Torvalds 				*i = (unsigned int)simple_strtoul(str, &next, base);
19671da177e4SLinus Torvalds 			}
19681da177e4SLinus Torvalds 			break;
19691da177e4SLinus Torvalds 		}
19701da177e4SLinus Torvalds 		num++;
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds 		if (!next)
19731da177e4SLinus Torvalds 			break;
19741da177e4SLinus Torvalds 		str = next;
19751da177e4SLinus Torvalds 	}
1976c6b40d16SJohannes Berg 
1977c6b40d16SJohannes Berg 	/*
1978c6b40d16SJohannes Berg 	 * Now we've come all the way through so either the input string or the
1979c6b40d16SJohannes Berg 	 * format ended. In the former case, there can be a %n at the current
1980c6b40d16SJohannes Berg 	 * position in the format that needs to be filled.
1981c6b40d16SJohannes Berg 	 */
1982c6b40d16SJohannes Berg 	if (*fmt == '%' && *(fmt + 1) == 'n') {
1983c6b40d16SJohannes Berg 		int *p = (int *)va_arg(args, int *);
1984c6b40d16SJohannes Berg 		*p = str - buf;
1985c6b40d16SJohannes Berg 	}
1986c6b40d16SJohannes Berg 
19871da177e4SLinus Torvalds 	return num;
19881da177e4SLinus Torvalds }
19891da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds /**
19921da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
19931da177e4SLinus Torvalds  * @buf:	input buffer
19941da177e4SLinus Torvalds  * @fmt:	formatting of buffer
19951da177e4SLinus Torvalds  * @...:	resulting arguments
19961da177e4SLinus Torvalds  */
19971da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
19981da177e4SLinus Torvalds {
19991da177e4SLinus Torvalds 	va_list args;
20001da177e4SLinus Torvalds 	int i;
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds 	va_start(args, fmt);
20031da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
20041da177e4SLinus Torvalds 	va_end(args);
20057b9186f5SAndré Goddard Rosa 
20061da177e4SLinus Torvalds 	return i;
20071da177e4SLinus Torvalds }
20081da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
2009