blob: bed224a643f2950c14b8be2cdab44cfcc39e25e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080012/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
17 */
18
19#include <stdarg.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050020#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/ctype.h>
24#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070025#include <linux/kallsyms.h>
26#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110027#include <linux/ioport.h>
Ryan Mallon22363fb2013-11-12 15:08:51 -080028#include <linux/cred.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000029#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Tim Schmielau4e57b682005-10-30 15:03:48 -080031#include <asm/page.h> /* for PAGE_SIZE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/div64.h>
James Bottomleydeac93d2008-09-03 20:43:36 -050033#include <asm/sections.h> /* for dereference_function_descriptor() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070035#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * simple_strtoull - convert a string to an unsigned long long
39 * @cp: The start of the string
40 * @endp: A pointer to the end of the parsed string will be placed here
41 * @base: The number base to use
42 */
Harvey Harrison22d27052008-10-16 13:40:35 -070043unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070045 unsigned long long result;
46 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070048 cp = _parse_integer_fixup_radix(cp, &base);
49 rv = _parse_integer(cp, base, &result);
50 /* FIXME */
51 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (endp)
54 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return result;
57}
Linus Torvalds1da177e2005-04-16 15:20:36 -070058EXPORT_SYMBOL(simple_strtoull);
59
60/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080061 * simple_strtoul - convert a string to an unsigned long
62 * @cp: The start of the string
63 * @endp: A pointer to the end of the parsed string will be placed here
64 * @base: The number base to use
65 */
66unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
67{
68 return simple_strtoull(cp, endp, base);
69}
70EXPORT_SYMBOL(simple_strtoul);
71
72/**
73 * simple_strtol - convert a string to a signed long
74 * @cp: The start of the string
75 * @endp: A pointer to the end of the parsed string will be placed here
76 * @base: The number base to use
77 */
78long simple_strtol(const char *cp, char **endp, unsigned int base)
79{
80 if (*cp == '-')
81 return -simple_strtoul(cp + 1, endp, base);
82
83 return simple_strtoul(cp, endp, base);
84}
85EXPORT_SYMBOL(simple_strtol);
86
87/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * simple_strtoll - convert a string to a signed long long
89 * @cp: The start of the string
90 * @endp: A pointer to the end of the parsed string will be placed here
91 * @base: The number base to use
92 */
Harvey Harrison22d27052008-10-16 13:40:35 -070093long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080095 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -070096 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080097
Harvey Harrison22d27052008-10-16 13:40:35 -070098 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400100EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Joe Perchescf3b4292010-05-24 14:33:16 -0700102static noinline_for_stack
103int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800105 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 while (isdigit(**s))
108 i = i*10 + *((*s)++) - '0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return i;
111}
112
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700113/* Decimal conversion is by far the most typical, and is used
114 * for /proc and /sys data. This directly impacts e.g. top performance
115 * with many processes running. We optimize it for speed
116 * using code from
117 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
118 * (with permission from the author, Douglas W. Jones). */
119
120/* Formats correctly any integer in [0,99999].
121 * Outputs from one to five digits depending on input.
122 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
Joe Perchescf3b4292010-05-24 14:33:16 -0700123static noinline_for_stack
124char *put_dec_trunc(char *buf, unsigned q)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700125{
126 unsigned d3, d2, d1, d0;
127 d1 = (q>>4) & 0xf;
128 d2 = (q>>8) & 0xf;
129 d3 = (q>>12);
130
131 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
132 q = (d0 * 0xcd) >> 11;
133 d0 = d0 - 10*q;
134 *buf++ = d0 + '0'; /* least significant digit */
135 d1 = q + 9*d3 + 5*d2 + d1;
136 if (d1 != 0) {
137 q = (d1 * 0xcd) >> 11;
138 d1 = d1 - 10*q;
139 *buf++ = d1 + '0'; /* next digit */
140
141 d2 = q + 2*d2;
142 if ((d2 != 0) || (d3 != 0)) {
143 q = (d2 * 0xd) >> 7;
144 d2 = d2 - 10*q;
145 *buf++ = d2 + '0'; /* next digit */
146
147 d3 = q + 4*d3;
148 if (d3 != 0) {
149 q = (d3 * 0xcd) >> 11;
150 d3 = d3 - 10*q;
151 *buf++ = d3 + '0'; /* next digit */
152 if (q != 0)
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800153 *buf++ = q + '0'; /* most sign. digit */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700154 }
155 }
156 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800157
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700158 return buf;
159}
160/* Same with if's removed. Always emits five digits */
Joe Perchescf3b4292010-05-24 14:33:16 -0700161static noinline_for_stack
162char *put_dec_full(char *buf, unsigned q)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700163{
164 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
165 /* but anyway, gcc produces better code with full-sized ints */
166 unsigned d3, d2, d1, d0;
167 d1 = (q>>4) & 0xf;
168 d2 = (q>>8) & 0xf;
169 d3 = (q>>12);
170
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800171 /*
172 * Possible ways to approx. divide by 10
173 * gcc -O2 replaces multiply with shifts and adds
174 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
175 * (x * 0x67) >> 10: 1100111
176 * (x * 0x34) >> 9: 110100 - same
177 * (x * 0x1a) >> 8: 11010 - same
178 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
179 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700180 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
181 q = (d0 * 0xcd) >> 11;
182 d0 = d0 - 10*q;
183 *buf++ = d0 + '0';
184 d1 = q + 9*d3 + 5*d2 + d1;
185 q = (d1 * 0xcd) >> 11;
186 d1 = d1 - 10*q;
187 *buf++ = d1 + '0';
188
189 d2 = q + 2*d2;
190 q = (d2 * 0xd) >> 7;
191 d2 = d2 - 10*q;
192 *buf++ = d2 + '0';
193
194 d3 = q + 4*d3;
195 q = (d3 * 0xcd) >> 11; /* - shorter code */
196 /* q = (d3 * 0x67) >> 10; - would also work */
197 d3 = d3 - 10*q;
198 *buf++ = d3 + '0';
199 *buf++ = q + '0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800200
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700201 return buf;
202}
203/* No inlining helps gcc to use registers better */
Joe Perchescf3b4292010-05-24 14:33:16 -0700204static noinline_for_stack
205char *put_dec(char *buf, unsigned long long num)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700206{
207 while (1) {
208 unsigned rem;
209 if (num < 100000)
210 return put_dec_trunc(buf, num);
211 rem = do_div(num, 100000);
212 buf = put_dec_full(buf, rem);
213 }
214}
215
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700216/*
217 * Convert passed number to decimal string.
218 * Returns the length of string. On buffer overflow, returns 0.
219 *
220 * If speed is not important, use snprintf(). It's easy to read the code.
221 */
222int num_to_str(char *buf, int size, unsigned long long num)
223{
224 char tmp[21]; /* Enough for 2^64 in decimal */
225 int idx, len;
226
227 len = put_dec(tmp, num) - tmp;
228
229 if (len > size)
230 return 0;
231 for (idx = 0; idx < len; ++idx)
232 buf[idx] = tmp[len - idx - 1];
233 return len;
234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#define ZEROPAD 1 /* pad with zero */
237#define SIGN 2 /* unsigned/signed long */
238#define PLUS 4 /* show plus */
239#define SPACE 8 /* space if plus */
240#define LEFT 16 /* left justified */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700241#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
242#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100244enum format_type {
245 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100246 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100247 FORMAT_TYPE_PRECISION,
248 FORMAT_TYPE_CHAR,
249 FORMAT_TYPE_STR,
250 FORMAT_TYPE_PTR,
251 FORMAT_TYPE_PERCENT_CHAR,
252 FORMAT_TYPE_INVALID,
253 FORMAT_TYPE_LONG_LONG,
254 FORMAT_TYPE_ULONG,
255 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800256 FORMAT_TYPE_UBYTE,
257 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100258 FORMAT_TYPE_USHORT,
259 FORMAT_TYPE_SHORT,
260 FORMAT_TYPE_UINT,
261 FORMAT_TYPE_INT,
262 FORMAT_TYPE_NRCHARS,
263 FORMAT_TYPE_SIZE_T,
264 FORMAT_TYPE_PTRDIFF
265};
266
267struct printf_spec {
Joe Perches4e310fd2010-04-14 09:27:40 -0700268 u8 type; /* format_type enum */
Joe Perchesef0658f2010-03-06 17:10:14 -0800269 u8 flags; /* flags to number() */
Joe Perches4e310fd2010-04-14 09:27:40 -0700270 u8 base; /* number base, 8, 10 or 16 only */
271 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
272 s16 field_width; /* width of output field */
273 s16 precision; /* # of digits/chars */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100274};
275
Joe Perchescf3b4292010-05-24 14:33:16 -0700276static noinline_for_stack
277char *number(char *buf, char *end, unsigned long long num,
278 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100280 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
281 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
282
283 char tmp[66];
284 char sign;
285 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100286 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 int i;
288
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100289 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
290 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100291 locase = (spec.flags & SMALL);
292 if (spec.flags & LEFT)
293 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100295 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800296 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800298 num = -(signed long long)num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100299 spec.field_width--;
300 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100302 spec.field_width--;
303 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100305 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700308 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100309 spec.field_width--;
310 if (spec.base == 16)
311 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700313
314 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 i = 0;
316 if (num == 0)
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700317 tmp[i++] = '0';
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700318 /* Generic code, for any base:
319 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100320 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700321 } while (num != 0);
322 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100323 else if (spec.base != 10) { /* 8 or 16 */
324 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700325 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800326
327 if (spec.base == 16)
328 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700329 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100330 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700331 num >>= shift;
332 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700333 } else { /* base 10 */
334 i = put_dec(tmp, num) - tmp;
335 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700336
337 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100338 if (i > spec.precision)
339 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700340 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100341 spec.field_width -= spec.precision;
342 if (!(spec.flags & (ZEROPAD+LEFT))) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800343 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700344 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 *buf = ' ';
346 ++buf;
347 }
348 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700349 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700351 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 *buf = sign;
353 ++buf;
354 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700355 /* "0x" / "0" prefix */
356 if (need_pfx) {
357 if (buf < end)
358 *buf = '0';
359 ++buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100360 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700361 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100362 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 ++buf;
364 }
365 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700366 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100367 if (!(spec.flags & LEFT)) {
368 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
369 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700370 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 *buf = c;
372 ++buf;
373 }
374 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700375 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100376 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700377 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *buf = '0';
379 ++buf;
380 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700381 /* actual digits of result */
382 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700383 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 *buf = tmp[i];
385 ++buf;
386 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700387 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100388 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700389 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 *buf = ' ';
391 ++buf;
392 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return buf;
395}
396
Joe Perchescf3b4292010-05-24 14:33:16 -0700397static noinline_for_stack
398char *string(char *buf, char *end, const char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700399{
400 int len, i;
401
402 if ((unsigned long)s < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -0800403 s = "(null)";
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700404
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100405 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700406
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100407 if (!(spec.flags & LEFT)) {
408 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700409 if (buf < end)
410 *buf = ' ';
411 ++buf;
412 }
413 }
414 for (i = 0; i < len; ++i) {
415 if (buf < end)
416 *buf = *s;
417 ++buf; ++s;
418 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100419 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700420 if (buf < end)
421 *buf = ' ';
422 ++buf;
423 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800424
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700425 return buf;
426}
427
Joe Perchescf3b4292010-05-24 14:33:16 -0700428static noinline_for_stack
429char *symbol_string(char *buf, char *end, void *ptr,
430 struct printf_spec spec, char ext)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700431{
432 unsigned long value = (unsigned long) ptr;
433#ifdef CONFIG_KALLSYMS
434 char sym[KSYM_SYMBOL_LEN];
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900435 if (ext == 'B')
436 sprint_backtrace(sym, value);
437 else if (ext != 'f' && ext != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200438 sprint_symbol(sym, value);
439 else
Stephen Boyd364da7c2012-04-23 10:42:01 -0700440 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800441
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100442 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700443#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800444 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100445 spec.flags |= SPECIAL | SMALL | ZEROPAD;
446 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800447
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100448 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700449#endif
450}
451
Joe Perchescf3b4292010-05-24 14:33:16 -0700452static noinline_for_stack
453char *resource_string(char *buf, char *end, struct resource *res,
454 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100455{
456#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600457#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100458#endif
459
460#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600461#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100462#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700463 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100464 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700465 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100466 .precision = -1,
467 .flags = SPECIAL | SMALL | ZEROPAD,
468 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700469 static const struct printf_spec mem_spec = {
470 .base = 16,
471 .field_width = MEM_RSRC_PRINTK_SIZE,
472 .precision = -1,
473 .flags = SPECIAL | SMALL | ZEROPAD,
474 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700475 static const struct printf_spec bus_spec = {
476 .base = 16,
477 .field_width = 2,
478 .precision = -1,
479 .flags = SMALL | ZEROPAD,
480 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700481 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600482 .base = 10,
483 .precision = -1,
484 .flags = 0,
485 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700486 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600487 .field_width = -1,
488 .precision = 10,
489 .flags = LEFT,
490 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700491 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600492 .base = 16,
493 .precision = -1,
494 .flags = SPECIAL | SMALL,
495 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600496
497 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
498 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
499#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
500#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700501#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600502#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
503 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
504 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
505
Linus Torvalds332d2e72008-10-20 15:07:34 +1100506 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600507 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700508 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100509
510 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700511 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600512 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700513 specp = &io_spec;
514 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600515 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700516 specp = &mem_spec;
517 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600518 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700519 specp = &dec_spec;
520 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600521 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700522 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700523 } else if (res->flags & IORESOURCE_BUS) {
524 p = string(p, pend, "bus ", str_spec);
525 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700526 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600527 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700528 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600529 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600530 }
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700531 p = number(p, pend, res->start, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600532 if (res->start != res->end) {
533 *p++ = '-';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700534 p = number(p, pend, res->end, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600535 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600536 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600537 if (res->flags & IORESOURCE_MEM_64)
538 p = string(p, pend, " 64bit", str_spec);
539 if (res->flags & IORESOURCE_PREFETCH)
540 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700541 if (res->flags & IORESOURCE_WINDOW)
542 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600543 if (res->flags & IORESOURCE_DISABLED)
544 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600545 } else {
546 p = string(p, pend, " flags ", str_spec);
547 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600548 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100549 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600550 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100551
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100552 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100553}
554
Joe Perchescf3b4292010-05-24 14:33:16 -0700555static noinline_for_stack
556char *mac_address_string(char *buf, char *end, u8 *addr,
557 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700558{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000559 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700560 char *p = mac_addr;
561 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000562 char separator;
563
564 if (fmt[1] == 'F') { /* FDDI canonical format */
Joe Perchesbc7259a2010-01-07 11:43:50 +0000565 separator = '-';
566 } else {
Joe Perchesbc7259a2010-01-07 11:43:50 +0000567 separator = ':';
568 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700569
570 for (i = 0; i < 6; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700571 p = hex_byte_pack(p, addr[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000572 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000573 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700574 }
575 *p = '\0';
576
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100577 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700578}
579
Joe Perchescf3b4292010-05-24 14:33:16 -0700580static noinline_for_stack
581char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700582{
Harvey Harrison689afa72008-10-28 16:04:44 -0700583 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800584 bool leading_zeros = (fmt[0] == 'i');
585 int index;
586 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700587
Joe Perches0159f242010-01-13 20:23:30 -0800588 switch (fmt[2]) {
589 case 'h':
590#ifdef __BIG_ENDIAN
591 index = 0;
592 step = 1;
593#else
594 index = 3;
595 step = -1;
596#endif
597 break;
598 case 'l':
599 index = 3;
600 step = -1;
601 break;
602 case 'n':
603 case 'b':
604 default:
605 index = 0;
606 step = 1;
607 break;
608 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000609 for (i = 0; i < 4; i++) {
610 char temp[3]; /* hold each IP quad in reverse order */
Joe Perches0159f242010-01-13 20:23:30 -0800611 int digits = put_dec_trunc(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000612 if (leading_zeros) {
613 if (digits < 3)
614 *p++ = '0';
615 if (digits < 2)
616 *p++ = '0';
617 }
618 /* reverse the digits in the quad */
619 while (digits--)
620 *p++ = temp[digits];
621 if (i < 3)
622 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -0800623 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000624 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000625 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800626
Joe Perches8a27f7c2009-08-17 12:29:44 +0000627 return p;
628}
629
Joe Perchescf3b4292010-05-24 14:33:16 -0700630static noinline_for_stack
631char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000632{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800633 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000634 unsigned char zerolength[8];
635 int longest = 1;
636 int colonpos = -1;
637 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800638 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000639 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000640 bool useIPv4;
641 struct in6_addr in6;
642
643 memcpy(&in6, addr, sizeof(struct in6_addr));
644
645 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000646
647 memset(zerolength, 0, sizeof(zerolength));
648
649 if (useIPv4)
650 range = 6;
651 else
652 range = 8;
653
654 /* find position of longest 0 run */
655 for (i = 0; i < range; i++) {
656 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000657 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000658 break;
659 zerolength[i]++;
660 }
661 }
662 for (i = 0; i < range; i++) {
663 if (zerolength[i] > longest) {
664 longest = zerolength[i];
665 colonpos = i;
666 }
667 }
Joe Perches29cf5192011-06-09 11:23:37 -0700668 if (longest == 1) /* don't compress a single 0 */
669 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000670
671 /* emit address */
672 for (i = 0; i < range; i++) {
673 if (i == colonpos) {
674 if (needcolon || i == 0)
675 *p++ = ':';
676 *p++ = ':';
677 needcolon = false;
678 i += longest - 1;
679 continue;
680 }
681 if (needcolon) {
682 *p++ = ':';
683 needcolon = false;
684 }
685 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +0000686 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000687 hi = word >> 8;
688 lo = word & 0xff;
689 if (hi) {
690 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700691 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000692 else
693 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700694 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000695 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -0800696 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700697 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000698 else
699 *p++ = hex_asc_lo(lo);
700 needcolon = true;
701 }
702
703 if (useIPv4) {
704 if (needcolon)
705 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -0800706 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +0000707 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000708 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800709
Joe Perches8a27f7c2009-08-17 12:29:44 +0000710 return p;
711}
712
Joe Perchescf3b4292010-05-24 14:33:16 -0700713static noinline_for_stack
714char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000715{
716 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800717
Harvey Harrison689afa72008-10-28 16:04:44 -0700718 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700719 p = hex_byte_pack(p, *addr++);
720 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000721 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700722 *p++ = ':';
723 }
724 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800725
Joe Perches8a27f7c2009-08-17 12:29:44 +0000726 return p;
727}
728
Joe Perchescf3b4292010-05-24 14:33:16 -0700729static noinline_for_stack
730char *ip6_addr_string(char *buf, char *end, const u8 *addr,
731 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000732{
733 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
734
735 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +0000736 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000737 else
Joe Percheseb78cd22009-09-18 13:04:06 +0000738 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -0700739
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100740 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700741}
742
Joe Perchescf3b4292010-05-24 14:33:16 -0700743static noinline_for_stack
744char *ip4_addr_string(char *buf, char *end, const u8 *addr,
745 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700746{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000747 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700748
Joe Perches0159f242010-01-13 20:23:30 -0800749 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700750
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100751 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700752}
753
Joe Perchescf3b4292010-05-24 14:33:16 -0700754static noinline_for_stack
755char *uuid_string(char *buf, char *end, const u8 *addr,
756 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -0800757{
758 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
759 char *p = uuid;
760 int i;
761 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
762 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
763 const u8 *index = be;
764 bool uc = false;
765
766 switch (*(++fmt)) {
767 case 'L':
768 uc = true; /* fall-through */
769 case 'l':
770 index = le;
771 break;
772 case 'B':
773 uc = true;
774 break;
775 }
776
777 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700778 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -0800779 switch (i) {
780 case 3:
781 case 5:
782 case 7:
783 case 9:
784 *p++ = '-';
785 break;
786 }
787 }
788
789 *p = 0;
790
791 if (uc) {
792 p = uuid;
793 do {
794 *p = toupper(*p);
795 } while (*(++p));
796 }
797
798 return string(buf, end, uuid, spec);
799}
800
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000801static
802char *netdev_feature_string(char *buf, char *end, const u8 *addr,
803 struct printf_spec spec)
804{
805 spec.flags |= SPECIAL | SMALL | ZEROPAD;
806 if (spec.field_width == -1)
807 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
808 spec.base = 16;
809
810 return number(buf, end, *(const netdev_features_t *)addr, spec);
811}
812
Ingo Molnar411f05f2011-05-12 23:00:28 +0200813int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800814
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700815/*
816 * Show a '%p' thing. A kernel extension is that the '%p' is followed
817 * by an extra set of alphanumeric characters that are extended format
818 * specifiers.
819 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100820 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700821 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200822 * - 'F' For symbolic function descriptor pointers with offset
823 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -0400824 * - 'S' For symbolic direct pointers with offset
825 * - 's' For symbolic direct pointers without offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900826 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600827 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
828 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700829 * - 'M' For a 6-byte MAC address, it prints the address in the
830 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000831 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +0000832 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -0800833 * with a dash-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000834 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
835 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
836 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
837 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
838 * IPv6 omits the colons (01020304...0f)
839 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Joe Perches0159f242010-01-13 20:23:30 -0800840 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
Joe Perches8a27f7c2009-08-17 12:29:44 +0000841 * - 'I6c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -0700842 * http://tools.ietf.org/html/rfc5952
Joe Perches9ac6e442009-12-14 18:01:09 -0800843 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
844 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
845 * Options for %pU are:
846 * b big endian lower case hex (default)
847 * B big endian UPPER case hex
848 * l little endian lower case hex
849 * L little endian UPPER case hex
850 * big endian output byte order is:
851 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
852 * little endian output byte order is:
853 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +0000854 * - 'V' For a struct va_format which contains a format string * and va_list *,
855 * call vsnprintf(->format, *->va_list).
856 * Implements a "recursive vsnprintf".
857 * Do not use this feature without some mechanism to verify the
858 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800859 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000860 * - 'NF' For a netdev_features_t
Joe Perches9ac6e442009-12-14 18:01:09 -0800861 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100862 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
863 * function pointers are really function descriptors, which contain a
864 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700865 */
Joe Perchescf3b4292010-05-24 14:33:16 -0700866static noinline_for_stack
867char *pointer(const char *fmt, char *buf, char *end, void *ptr,
868 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700869{
Kees Cook9f36e2c2011-03-22 16:34:22 -0700870 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -0700871 /*
872 * Print (null) with the same width as a pointer so it makes
873 * tabular output look nice.
874 */
875 if (spec.field_width == -1)
876 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100877 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -0700878 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800879
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700880 switch (*fmt) {
881 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200882 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700883 ptr = dereference_function_descriptor(ptr);
884 /* Fallthrough */
885 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -0800886 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900887 case 'B':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200888 return symbol_string(buf, end, ptr, spec, *fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100889 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600890 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600891 return resource_string(buf, end, ptr, spec, fmt);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000892 case 'M': /* Colon separated: 00:01:02:03:04:05 */
893 case 'm': /* Contiguous: 000102030405 */
Joe Perchesbc7259a2010-01-07 11:43:50 +0000894 /* [mM]F (FDDI, bit reversed) */
Joe Perches8a27f7c2009-08-17 12:29:44 +0000895 return mac_address_string(buf, end, ptr, spec, fmt);
896 case 'I': /* Formatted IP supported
897 * 4: 1.2.3.4
898 * 6: 0001:0203:...:0708
899 * 6c: 1::708 or 1::1.2.3.4
900 */
901 case 'i': /* Contiguous:
902 * 4: 001.002.003.004
903 * 6: 000102...0f
904 */
905 switch (fmt[1]) {
906 case '6':
907 return ip6_addr_string(buf, end, ptr, spec, fmt);
908 case '4':
909 return ip4_addr_string(buf, end, ptr, spec, fmt);
910 }
Harvey Harrison4aa99602008-10-29 12:49:58 -0700911 break;
Joe Perches9ac6e442009-12-14 18:01:09 -0800912 case 'U':
913 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +0000914 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +0000915 {
916 va_list va;
917
918 va_copy(va, *((struct va_format *)ptr)->va);
919 buf += vsnprintf(buf, end > buf ? end - buf : 0,
920 ((struct va_format *)ptr)->fmt, va);
921 va_end(va);
922 return buf;
923 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800924 case 'K':
925 /*
926 * %pK cannot be used in IRQ context because its test
927 * for CAP_SYSLOG would be meaningless.
928 */
Dan Rosenberg26fead62012-07-30 14:40:26 -0700929 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
930 in_nmi())) {
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800931 if (spec.field_width == -1)
932 spec.field_width = 2 * sizeof(void *);
933 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800934 }
Ryan Mallon22363fb2013-11-12 15:08:51 -0800935
936 switch (kptr_restrict) {
937 case 0:
938 /* Always print %pK values */
939 break;
940 case 1: {
941 /*
942 * Only print the real pointer value if the current
943 * process has CAP_SYSLOG and is running with the
944 * same credentials it started with. This is because
945 * access to files is checked at open() time, but %pK
946 * checks permission at read() time. We don't want to
947 * leak pointer values if a binary opens a file using
948 * %pK and then elevates privileges before reading it.
949 */
950 const struct cred *cred = current_cred();
951
952 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
953 (cred->euid != cred->uid) ||
954 (cred->egid != cred->gid))
955 ptr = NULL;
956 break;
957 }
958 case 2:
959 default:
960 /* Always print 0's for %pK */
Joe Perches26297602011-03-22 16:34:19 -0700961 ptr = NULL;
Ryan Mallon22363fb2013-11-12 15:08:51 -0800962 break;
963 }
Joe Perches26297602011-03-22 16:34:19 -0700964 break;
Ryan Mallon22363fb2013-11-12 15:08:51 -0800965
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000966 case 'N':
967 switch (fmt[1]) {
968 case 'F':
969 return netdev_feature_string(buf, end, ptr, spec);
970 }
971 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700972 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100973 spec.flags |= SMALL;
974 if (spec.field_width == -1) {
Joe Perches5e057982010-10-26 14:22:50 -0700975 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100976 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700977 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100978 spec.base = 16;
979
980 return number(buf, end, (unsigned long) ptr, spec);
981}
982
983/*
984 * Helper function to decode printf style format.
985 * Each call decode a token from the format and return the
986 * number of characters read (or likely the delta where it wants
987 * to go on the next call).
988 * The decoded token is returned through the parameters
989 *
990 * 'h', 'l', or 'L' for integer fields
991 * 'z' support added 23/7/1999 S.H.
992 * 'z' changed to 'Z' --davidm 1/25/99
993 * 't' added for ptrdiff_t
994 *
995 * @fmt: the format string
996 * @type of the token returned
997 * @flags: various flags such as +, -, # tokens..
998 * @field_width: overwritten width
999 * @base: base of the number (octal, hex, ...)
1000 * @precision: precision of a number
1001 * @qualifier: qualifier of a number (long, size_t, ...)
1002 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001003static noinline_for_stack
1004int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001005{
1006 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001007
1008 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001009 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001010 if (spec->field_width < 0) {
1011 spec->field_width = -spec->field_width;
1012 spec->flags |= LEFT;
1013 }
1014 spec->type = FORMAT_TYPE_NONE;
1015 goto precision;
1016 }
1017
1018 /* we finished early by reading the precision */
1019 if (spec->type == FORMAT_TYPE_PRECISION) {
1020 if (spec->precision < 0)
1021 spec->precision = 0;
1022
1023 spec->type = FORMAT_TYPE_NONE;
1024 goto qualifier;
1025 }
1026
1027 /* By default */
1028 spec->type = FORMAT_TYPE_NONE;
1029
1030 for (; *fmt ; ++fmt) {
1031 if (*fmt == '%')
1032 break;
1033 }
1034
1035 /* Return the current non-format string */
1036 if (fmt != start || !*fmt)
1037 return fmt - start;
1038
1039 /* Process flags */
1040 spec->flags = 0;
1041
1042 while (1) { /* this also skips first '%' */
1043 bool found = true;
1044
1045 ++fmt;
1046
1047 switch (*fmt) {
1048 case '-': spec->flags |= LEFT; break;
1049 case '+': spec->flags |= PLUS; break;
1050 case ' ': spec->flags |= SPACE; break;
1051 case '#': spec->flags |= SPECIAL; break;
1052 case '0': spec->flags |= ZEROPAD; break;
1053 default: found = false;
1054 }
1055
1056 if (!found)
1057 break;
1058 }
1059
1060 /* get field width */
1061 spec->field_width = -1;
1062
1063 if (isdigit(*fmt))
1064 spec->field_width = skip_atoi(&fmt);
1065 else if (*fmt == '*') {
1066 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001067 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001068 return ++fmt - start;
1069 }
1070
1071precision:
1072 /* get the precision */
1073 spec->precision = -1;
1074 if (*fmt == '.') {
1075 ++fmt;
1076 if (isdigit(*fmt)) {
1077 spec->precision = skip_atoi(&fmt);
1078 if (spec->precision < 0)
1079 spec->precision = 0;
1080 } else if (*fmt == '*') {
1081 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001082 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001083 return ++fmt - start;
1084 }
1085 }
1086
1087qualifier:
1088 /* get the conversion qualifier */
1089 spec->qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001090 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1091 _tolower(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001092 spec->qualifier = *fmt++;
1093 if (unlikely(spec->qualifier == *fmt)) {
1094 if (spec->qualifier == 'l') {
1095 spec->qualifier = 'L';
1096 ++fmt;
1097 } else if (spec->qualifier == 'h') {
1098 spec->qualifier = 'H';
1099 ++fmt;
1100 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001101 }
1102 }
1103
1104 /* default base */
1105 spec->base = 10;
1106 switch (*fmt) {
1107 case 'c':
1108 spec->type = FORMAT_TYPE_CHAR;
1109 return ++fmt - start;
1110
1111 case 's':
1112 spec->type = FORMAT_TYPE_STR;
1113 return ++fmt - start;
1114
1115 case 'p':
1116 spec->type = FORMAT_TYPE_PTR;
1117 return fmt - start;
1118 /* skip alnum */
1119
1120 case 'n':
1121 spec->type = FORMAT_TYPE_NRCHARS;
1122 return ++fmt - start;
1123
1124 case '%':
1125 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1126 return ++fmt - start;
1127
1128 /* integer number formats - set up the flags and "break" */
1129 case 'o':
1130 spec->base = 8;
1131 break;
1132
1133 case 'x':
1134 spec->flags |= SMALL;
1135
1136 case 'X':
1137 spec->base = 16;
1138 break;
1139
1140 case 'd':
1141 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001142 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001143 case 'u':
1144 break;
1145
1146 default:
1147 spec->type = FORMAT_TYPE_INVALID;
1148 return fmt - start;
1149 }
1150
1151 if (spec->qualifier == 'L')
1152 spec->type = FORMAT_TYPE_LONG_LONG;
1153 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001154 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001155 spec->type = FORMAT_TYPE_LONG;
1156 else
1157 spec->type = FORMAT_TYPE_ULONG;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001158 } else if (_tolower(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001159 spec->type = FORMAT_TYPE_SIZE_T;
1160 } else if (spec->qualifier == 't') {
1161 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001162 } else if (spec->qualifier == 'H') {
1163 if (spec->flags & SIGN)
1164 spec->type = FORMAT_TYPE_BYTE;
1165 else
1166 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001167 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001168 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001169 spec->type = FORMAT_TYPE_SHORT;
1170 else
1171 spec->type = FORMAT_TYPE_USHORT;
1172 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001173 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001174 spec->type = FORMAT_TYPE_INT;
1175 else
1176 spec->type = FORMAT_TYPE_UINT;
1177 }
1178
1179 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001180}
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182/**
1183 * vsnprintf - Format a string and place it in a buffer
1184 * @buf: The buffer to place the result into
1185 * @size: The size of the buffer, including the trailing null space
1186 * @fmt: The format string to use
1187 * @args: Arguments for the format string
1188 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001189 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001190 * %pS output the name of a text symbol with offset
1191 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001192 * %pF output the name of a function pointer with its offset
1193 * %pf output the name of a function pointer without its offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001194 * %pB output the name of a backtrace symbol with its offset
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001195 * %pR output the address range in a struct resource with decoded flags
1196 * %pr output the address range in a struct resource with raw flags
1197 * %pM output a 6-byte MAC address with colons
1198 * %pm output a 6-byte MAC address without colons
1199 * %pI4 print an IPv4 address without leading zeros
1200 * %pi4 print an IPv4 address with leading zeros
1201 * %pI6 print an IPv6 address with colons
1202 * %pi6 print an IPv6 address without colons
Jan Engelhardtf996f202011-07-14 18:48:56 +02001203 * %pI6c print an IPv6 address as specified by RFC 5952
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001204 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1205 * case.
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001206 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001207 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 * The return value is the number of characters which would
1209 * be generated for the given input, excluding the trailing
1210 * '\0', as per ISO C99. If you want to have the exact
1211 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001212 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 * return is greater than or equal to @size, the resulting
1214 * string is truncated.
1215 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001216 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 */
1218int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001221 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001222 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001224 /* Reject out-of-range values early. Large positive sizes are
1225 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001226 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001230 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001232 /* Make sure end is always >= buf */
1233 if (end < buf) {
1234 end = ((void *)-1);
1235 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001238 while (*fmt) {
1239 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001240 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001241
1242 fmt += read;
1243
1244 switch (spec.type) {
1245 case FORMAT_TYPE_NONE: {
1246 int copy = read;
1247 if (str < end) {
1248 if (copy > end - str)
1249 copy = end - str;
1250 memcpy(str, old_fmt, copy);
1251 }
1252 str += read;
1253 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255
Vegard Nossumed681a92009-03-14 12:08:50 +01001256 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001257 spec.field_width = va_arg(args, int);
1258 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001260 case FORMAT_TYPE_PRECISION:
1261 spec.precision = va_arg(args, int);
1262 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
André Goddard Rosad4be1512009-12-14 18:00:59 -08001264 case FORMAT_TYPE_CHAR: {
1265 char c;
1266
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001267 if (!(spec.flags & LEFT)) {
1268 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001269 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 *str = ' ';
1271 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001274 }
1275 c = (unsigned char) va_arg(args, int);
1276 if (str < end)
1277 *str = c;
1278 ++str;
1279 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001280 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001281 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001283 }
1284 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001287 case FORMAT_TYPE_STR:
1288 str = string(str, end, va_arg(args, char *), spec);
1289 break;
1290
1291 case FORMAT_TYPE_PTR:
1292 str = pointer(fmt+1, str, end, va_arg(args, void *),
1293 spec);
1294 while (isalnum(*fmt))
1295 fmt++;
1296 break;
1297
1298 case FORMAT_TYPE_PERCENT_CHAR:
1299 if (str < end)
1300 *str = '%';
1301 ++str;
1302 break;
1303
1304 case FORMAT_TYPE_INVALID:
1305 if (str < end)
1306 *str = '%';
1307 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001308 break;
1309
1310 case FORMAT_TYPE_NRCHARS: {
Kees Cook7e268562013-11-14 14:31:58 -08001311 /*
1312 * Since %n poses a greater security risk than
1313 * utility, ignore %n and skip its argument.
1314 */
1315 void *skip_arg;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001316
Kees Cook7e268562013-11-14 14:31:58 -08001317 WARN_ONCE(1, "Please remove ignored %%n in '%s'\n",
1318 old_fmt);
1319
1320 skip_arg = va_arg(args, void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001321 break;
1322 }
1323
1324 default:
1325 switch (spec.type) {
1326 case FORMAT_TYPE_LONG_LONG:
1327 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001329 case FORMAT_TYPE_ULONG:
1330 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001332 case FORMAT_TYPE_LONG:
1333 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001335 case FORMAT_TYPE_SIZE_T:
1336 num = va_arg(args, size_t);
1337 break;
1338 case FORMAT_TYPE_PTRDIFF:
1339 num = va_arg(args, ptrdiff_t);
1340 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001341 case FORMAT_TYPE_UBYTE:
1342 num = (unsigned char) va_arg(args, int);
1343 break;
1344 case FORMAT_TYPE_BYTE:
1345 num = (signed char) va_arg(args, int);
1346 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001347 case FORMAT_TYPE_USHORT:
1348 num = (unsigned short) va_arg(args, int);
1349 break;
1350 case FORMAT_TYPE_SHORT:
1351 num = (short) va_arg(args, int);
1352 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001353 case FORMAT_TYPE_INT:
1354 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001355 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001357 num = va_arg(args, unsigned int);
1358 }
1359
1360 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001363
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001364 if (size > 0) {
1365 if (str < end)
1366 *str = '\0';
1367 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001368 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001369 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001370
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001371 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375EXPORT_SYMBOL(vsnprintf);
1376
1377/**
1378 * vscnprintf - Format a string and place it in a buffer
1379 * @buf: The buffer to place the result into
1380 * @size: The size of the buffer, including the trailing null space
1381 * @fmt: The format string to use
1382 * @args: Arguments for the format string
1383 *
1384 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08001385 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * returns 0.
1387 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001388 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001389 *
1390 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 */
1392int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1393{
1394 int i;
1395
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001396 i = vsnprintf(buf, size, fmt, args);
1397
Anton Arapovb921c692011-01-12 16:59:49 -08001398 if (likely(i < size))
1399 return i;
1400 if (size != 0)
1401 return size - 1;
1402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404EXPORT_SYMBOL(vscnprintf);
1405
1406/**
1407 * snprintf - Format a string and place it in a buffer
1408 * @buf: The buffer to place the result into
1409 * @size: The size of the buffer, including the trailing null space
1410 * @fmt: The format string to use
1411 * @...: Arguments for the format string
1412 *
1413 * The return value is the number of characters which would be
1414 * generated for the given input, excluding the trailing null,
1415 * as per ISO C99. If the return is greater than or equal to
1416 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001417 *
1418 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001420int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 va_list args;
1423 int i;
1424
1425 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001426 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return i;
1430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431EXPORT_SYMBOL(snprintf);
1432
1433/**
1434 * scnprintf - Format a string and place it in a buffer
1435 * @buf: The buffer to place the result into
1436 * @size: The size of the buffer, including the trailing null space
1437 * @fmt: The format string to use
1438 * @...: Arguments for the format string
1439 *
1440 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07001441 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 */
1443
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001444int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 va_list args;
1447 int i;
1448
1449 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08001450 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001452
Anton Arapovb921c692011-01-12 16:59:49 -08001453 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455EXPORT_SYMBOL(scnprintf);
1456
1457/**
1458 * vsprintf - Format a string and place it in a buffer
1459 * @buf: The buffer to place the result into
1460 * @fmt: The format string to use
1461 * @args: Arguments for the format string
1462 *
1463 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001464 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 * buffer overflows.
1466 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001467 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001468 *
1469 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 */
1471int vsprintf(char *buf, const char *fmt, va_list args)
1472{
1473 return vsnprintf(buf, INT_MAX, fmt, args);
1474}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475EXPORT_SYMBOL(vsprintf);
1476
1477/**
1478 * sprintf - Format a string and place it in a buffer
1479 * @buf: The buffer to place the result into
1480 * @fmt: The format string to use
1481 * @...: Arguments for the format string
1482 *
1483 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001484 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001486 *
1487 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001489int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 va_list args;
1492 int i;
1493
1494 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001495 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return i;
1499}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500EXPORT_SYMBOL(sprintf);
1501
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001502#ifdef CONFIG_BINARY_PRINTF
1503/*
1504 * bprintf service:
1505 * vbin_printf() - VA arguments to binary data
1506 * bstr_printf() - Binary data to text string
1507 */
1508
1509/**
1510 * vbin_printf - Parse a format string and place args' binary value in a buffer
1511 * @bin_buf: The buffer to place args' binary value
1512 * @size: The size of the buffer(by words(32bits), not characters)
1513 * @fmt: The format string to use
1514 * @args: Arguments for the format string
1515 *
1516 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1517 * is skiped.
1518 *
1519 * The return value is the number of words(32bits) which would be generated for
1520 * the given input.
1521 *
1522 * NOTE:
1523 * If the return value is greater than @size, the resulting bin_buf is NOT
1524 * valid for bstr_printf().
1525 */
1526int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1527{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001528 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001529 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001530
1531 str = (char *)bin_buf;
1532 end = (char *)(bin_buf + size);
1533
1534#define save_arg(type) \
1535do { \
1536 if (sizeof(type) == 8) { \
1537 unsigned long long value; \
1538 str = PTR_ALIGN(str, sizeof(u32)); \
1539 value = va_arg(args, unsigned long long); \
1540 if (str + sizeof(type) <= end) { \
1541 *(u32 *)str = *(u32 *)&value; \
1542 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1543 } \
1544 } else { \
1545 unsigned long value; \
1546 str = PTR_ALIGN(str, sizeof(type)); \
1547 value = va_arg(args, int); \
1548 if (str + sizeof(type) <= end) \
1549 *(typeof(type) *)str = (type)value; \
1550 } \
1551 str += sizeof(type); \
1552} while (0)
1553
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001554 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08001555 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001556
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001557 fmt += read;
1558
1559 switch (spec.type) {
1560 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08001561 case FORMAT_TYPE_INVALID:
1562 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001563 break;
1564
Vegard Nossumed681a92009-03-14 12:08:50 +01001565 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001566 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001567 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001568 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001569
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001570 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001571 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001572 break;
1573
1574 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001575 const char *save_str = va_arg(args, char *);
1576 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08001577
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001578 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1579 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08001580 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08001581 len = strlen(save_str) + 1;
1582 if (str + len < end)
1583 memcpy(str, save_str, len);
1584 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001585 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001586 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001587
1588 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001589 save_arg(void *);
1590 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001591 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001592 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001593 break;
1594
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001595 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001596 /* skip %n 's argument */
Joe Perchesef0658f2010-03-06 17:10:14 -08001597 u8 qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001598 void *skip_arg;
1599 if (qualifier == 'l')
1600 skip_arg = va_arg(args, long *);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001601 else if (_tolower(qualifier) == 'z')
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001602 skip_arg = va_arg(args, size_t *);
1603 else
1604 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001605 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001606 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001607
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001608 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001609 switch (spec.type) {
1610
1611 case FORMAT_TYPE_LONG_LONG:
1612 save_arg(long long);
1613 break;
1614 case FORMAT_TYPE_ULONG:
1615 case FORMAT_TYPE_LONG:
1616 save_arg(unsigned long);
1617 break;
1618 case FORMAT_TYPE_SIZE_T:
1619 save_arg(size_t);
1620 break;
1621 case FORMAT_TYPE_PTRDIFF:
1622 save_arg(ptrdiff_t);
1623 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001624 case FORMAT_TYPE_UBYTE:
1625 case FORMAT_TYPE_BYTE:
1626 save_arg(char);
1627 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001628 case FORMAT_TYPE_USHORT:
1629 case FORMAT_TYPE_SHORT:
1630 save_arg(short);
1631 break;
1632 default:
1633 save_arg(int);
1634 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001635 }
1636 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001637
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001638 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001639#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001640}
1641EXPORT_SYMBOL_GPL(vbin_printf);
1642
1643/**
1644 * bstr_printf - Format a string from binary arguments and place it in a buffer
1645 * @buf: The buffer to place the result into
1646 * @size: The size of the buffer, including the trailing null space
1647 * @fmt: The format string to use
1648 * @bin_buf: Binary arguments for the format string
1649 *
1650 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1651 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1652 * a binary buffer that generated by vbin_printf.
1653 *
1654 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001655 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001656 *
1657 * The return value is the number of characters which would
1658 * be generated for the given input, excluding the trailing
1659 * '\0', as per ISO C99. If you want to have the exact
1660 * number of characters written into @buf as return value
1661 * (not including the trailing '\0'), use vscnprintf(). If the
1662 * return is greater than or equal to @size, the resulting
1663 * string is truncated.
1664 */
1665int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1666{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001667 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08001668 char *str, *end;
1669 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001670
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001671 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001672 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001673
1674 str = buf;
1675 end = buf + size;
1676
1677#define get_arg(type) \
1678({ \
1679 typeof(type) value; \
1680 if (sizeof(type) == 8) { \
1681 args = PTR_ALIGN(args, sizeof(u32)); \
1682 *(u32 *)&value = *(u32 *)args; \
1683 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1684 } else { \
1685 args = PTR_ALIGN(args, sizeof(type)); \
1686 value = *(typeof(type) *)args; \
1687 } \
1688 args += sizeof(type); \
1689 value; \
1690})
1691
1692 /* Make sure end is always >= buf */
1693 if (end < buf) {
1694 end = ((void *)-1);
1695 size = end - buf;
1696 }
1697
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001698 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001699 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001700 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001701
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001702 fmt += read;
1703
1704 switch (spec.type) {
1705 case FORMAT_TYPE_NONE: {
1706 int copy = read;
1707 if (str < end) {
1708 if (copy > end - str)
1709 copy = end - str;
1710 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001711 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001712 str += read;
1713 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001714 }
1715
Vegard Nossumed681a92009-03-14 12:08:50 +01001716 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001717 spec.field_width = get_arg(int);
1718 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001719
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001720 case FORMAT_TYPE_PRECISION:
1721 spec.precision = get_arg(int);
1722 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001723
André Goddard Rosad4be1512009-12-14 18:00:59 -08001724 case FORMAT_TYPE_CHAR: {
1725 char c;
1726
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001727 if (!(spec.flags & LEFT)) {
1728 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001729 if (str < end)
1730 *str = ' ';
1731 ++str;
1732 }
1733 }
1734 c = (unsigned char) get_arg(char);
1735 if (str < end)
1736 *str = c;
1737 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001738 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001739 if (str < end)
1740 *str = ' ';
1741 ++str;
1742 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001743 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001744 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001745
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001746 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001747 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001748 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001749 str = string(str, end, (char *)str_arg, spec);
1750 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001751 }
1752
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001753 case FORMAT_TYPE_PTR:
1754 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1755 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001756 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001757 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001758
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001759 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001760 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001761 if (str < end)
1762 *str = '%';
1763 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001764 break;
1765
1766 case FORMAT_TYPE_NRCHARS:
1767 /* skip */
1768 break;
1769
André Goddard Rosad4be1512009-12-14 18:00:59 -08001770 default: {
1771 unsigned long long num;
1772
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001773 switch (spec.type) {
1774
1775 case FORMAT_TYPE_LONG_LONG:
1776 num = get_arg(long long);
1777 break;
1778 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001779 case FORMAT_TYPE_LONG:
1780 num = get_arg(unsigned long);
1781 break;
1782 case FORMAT_TYPE_SIZE_T:
1783 num = get_arg(size_t);
1784 break;
1785 case FORMAT_TYPE_PTRDIFF:
1786 num = get_arg(ptrdiff_t);
1787 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001788 case FORMAT_TYPE_UBYTE:
1789 num = get_arg(unsigned char);
1790 break;
1791 case FORMAT_TYPE_BYTE:
1792 num = get_arg(signed char);
1793 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001794 case FORMAT_TYPE_USHORT:
1795 num = get_arg(unsigned short);
1796 break;
1797 case FORMAT_TYPE_SHORT:
1798 num = get_arg(short);
1799 break;
1800 case FORMAT_TYPE_UINT:
1801 num = get_arg(unsigned int);
1802 break;
1803 default:
1804 num = get_arg(int);
1805 }
1806
1807 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08001808 } /* default: */
1809 } /* switch(spec.type) */
1810 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001811
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001812 if (size > 0) {
1813 if (str < end)
1814 *str = '\0';
1815 else
1816 end[-1] = '\0';
1817 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001818
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001819#undef get_arg
1820
1821 /* the trailing null byte doesn't count towards the total */
1822 return str - buf;
1823}
1824EXPORT_SYMBOL_GPL(bstr_printf);
1825
1826/**
1827 * bprintf - Parse a format string and place args' binary value in a buffer
1828 * @bin_buf: The buffer to place args' binary value
1829 * @size: The size of the buffer(by words(32bits), not characters)
1830 * @fmt: The format string to use
1831 * @...: Arguments for the format string
1832 *
1833 * The function returns the number of words(u32) written
1834 * into @bin_buf.
1835 */
1836int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1837{
1838 va_list args;
1839 int ret;
1840
1841 va_start(args, fmt);
1842 ret = vbin_printf(bin_buf, size, fmt, args);
1843 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001844
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001845 return ret;
1846}
1847EXPORT_SYMBOL_GPL(bprintf);
1848
1849#endif /* CONFIG_BINARY_PRINTF */
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851/**
1852 * vsscanf - Unformat a buffer into a list of arguments
1853 * @buf: input buffer
1854 * @fmt: format of buffer
1855 * @args: arguments
1856 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001857int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
1859 const char *str = buf;
1860 char *next;
1861 char digit;
1862 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08001863 u8 qualifier;
1864 u8 base;
1865 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001866 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001868 while (*fmt && *str) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /* skip any white space in format */
1870 /* white space in format matchs any amount of
1871 * white space, including none, in the input.
1872 */
1873 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08001874 fmt = skip_spaces(++fmt);
1875 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
1877
1878 /* anything that is not a conversion must match exactly */
1879 if (*fmt != '%' && *fmt) {
1880 if (*fmt++ != *str++)
1881 break;
1882 continue;
1883 }
1884
1885 if (!*fmt)
1886 break;
1887 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 /* skip this conversion.
1890 * advance both strings to next white space
1891 */
1892 if (*fmt == '*') {
Andy Spencer8fccae22009-10-01 15:44:27 -07001893 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 fmt++;
1895 while (!isspace(*str) && *str)
1896 str++;
1897 continue;
1898 }
1899
1900 /* get field width */
1901 field_width = -1;
1902 if (isdigit(*fmt))
1903 field_width = skip_atoi(&fmt);
1904
1905 /* get conversion qualifier */
1906 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001907 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1908 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 qualifier = *fmt++;
1910 if (unlikely(qualifier == *fmt)) {
1911 if (qualifier == 'h') {
1912 qualifier = 'H';
1913 fmt++;
1914 } else if (qualifier == 'l') {
1915 qualifier = 'L';
1916 fmt++;
1917 }
1918 }
1919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 if (!*fmt || !*str)
1922 break;
1923
André Goddard Rosad4be1512009-12-14 18:00:59 -08001924 base = 10;
1925 is_sign = 0;
1926
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001927 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 case 'c':
1929 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001930 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (field_width == -1)
1932 field_width = 1;
1933 do {
1934 *s++ = *str++;
1935 } while (--field_width > 0 && *str);
1936 num++;
1937 }
1938 continue;
1939 case 's':
1940 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001941 char *s = (char *)va_arg(args, char *);
1942 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07001943 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08001945 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001948 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 *s = '\0';
1951 num++;
1952 }
1953 continue;
1954 case 'n':
1955 /* return number of characters read so far */
1956 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001957 int *i = (int *)va_arg(args, int*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 *i = str - buf;
1959 }
1960 continue;
1961 case 'o':
1962 base = 8;
1963 break;
1964 case 'x':
1965 case 'X':
1966 base = 16;
1967 break;
1968 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001969 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 case 'd':
1971 is_sign = 1;
1972 case 'u':
1973 break;
1974 case '%':
1975 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001976 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return num;
1978 continue;
1979 default:
1980 /* invalid format; stop here */
1981 return num;
1982 }
1983
1984 /* have some sort of integer conversion.
1985 * first, skip white space in buffer.
1986 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08001987 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 digit = *str;
1990 if (is_sign && digit == '-')
1991 digit = *(str + 1);
1992
1993 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001994 || (base == 16 && !isxdigit(digit))
1995 || (base == 10 && !isdigit(digit))
1996 || (base == 8 && (!isdigit(digit) || digit > '7'))
1997 || (base == 0 && !isdigit(digit)))
1998 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002000 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 case 'H': /* that's 'hh' in format */
2002 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002003 signed char *s = (signed char *)va_arg(args, signed char *);
2004 *s = (signed char)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002006 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
2007 *s = (unsigned char)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009 break;
2010 case 'h':
2011 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002012 short *s = (short *)va_arg(args, short *);
2013 *s = (short)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002015 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
2016 *s = (unsigned short)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018 break;
2019 case 'l':
2020 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002021 long *l = (long *)va_arg(args, long *);
2022 *l = simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002024 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
2025 *l = simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027 break;
2028 case 'L':
2029 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002030 long long *l = (long long *)va_arg(args, long long *);
2031 *l = simple_strtoll(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002033 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2034 *l = simple_strtoull(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036 break;
2037 case 'Z':
2038 case 'z':
2039 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002040 size_t *s = (size_t *)va_arg(args, size_t *);
2041 *s = (size_t)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 break;
2044 default:
2045 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002046 int *i = (int *)va_arg(args, int *);
2047 *i = (int)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002049 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2050 *i = (unsigned int)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052 break;
2053 }
2054 num++;
2055
2056 if (!next)
2057 break;
2058 str = next;
2059 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002060
2061 /*
2062 * Now we've come all the way through so either the input string or the
2063 * format ended. In the former case, there can be a %n at the current
2064 * position in the format that needs to be filled.
2065 */
2066 if (*fmt == '%' && *(fmt + 1) == 'n') {
2067 int *p = (int *)va_arg(args, int *);
2068 *p = str - buf;
2069 }
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return num;
2072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073EXPORT_SYMBOL(vsscanf);
2074
2075/**
2076 * sscanf - Unformat a buffer into a list of arguments
2077 * @buf: input buffer
2078 * @fmt: formatting of buffer
2079 * @...: resulting arguments
2080 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002081int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 va_list args;
2084 int i;
2085
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002086 va_start(args, fmt);
2087 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return i;
2091}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092EXPORT_SYMBOL(sscanf);