blob: bcfbab66fbce6b6caec3f45488b27f371656428a [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 Mallon74d58172013-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
Stepan Moskovchenkoe9ba0642013-01-21 13:27:25 -0800861 * - 'a' For a phys_addr_t type and its derivative types (passed by reference)
Joe Perches9ac6e442009-12-14 18:01:09 -0800862 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100863 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
864 * function pointers are really function descriptors, which contain a
865 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700866 */
Joe Perchescf3b4292010-05-24 14:33:16 -0700867static noinline_for_stack
868char *pointer(const char *fmt, char *buf, char *end, void *ptr,
869 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700870{
Kees Cook9f36e2c2011-03-22 16:34:22 -0700871 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -0700872 /*
873 * Print (null) with the same width as a pointer so it makes
874 * tabular output look nice.
875 */
876 if (spec.field_width == -1)
877 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100878 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -0700879 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800880
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700881 switch (*fmt) {
882 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200883 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700884 ptr = dereference_function_descriptor(ptr);
885 /* Fallthrough */
886 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -0800887 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900888 case 'B':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200889 return symbol_string(buf, end, ptr, spec, *fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100890 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600891 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600892 return resource_string(buf, end, ptr, spec, fmt);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000893 case 'M': /* Colon separated: 00:01:02:03:04:05 */
894 case 'm': /* Contiguous: 000102030405 */
Joe Perchesbc7259a2010-01-07 11:43:50 +0000895 /* [mM]F (FDDI, bit reversed) */
Joe Perches8a27f7c2009-08-17 12:29:44 +0000896 return mac_address_string(buf, end, ptr, spec, fmt);
897 case 'I': /* Formatted IP supported
898 * 4: 1.2.3.4
899 * 6: 0001:0203:...:0708
900 * 6c: 1::708 or 1::1.2.3.4
901 */
902 case 'i': /* Contiguous:
903 * 4: 001.002.003.004
904 * 6: 000102...0f
905 */
906 switch (fmt[1]) {
907 case '6':
908 return ip6_addr_string(buf, end, ptr, spec, fmt);
909 case '4':
910 return ip4_addr_string(buf, end, ptr, spec, fmt);
911 }
Harvey Harrison4aa99602008-10-29 12:49:58 -0700912 break;
Joe Perches9ac6e442009-12-14 18:01:09 -0800913 case 'U':
914 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +0000915 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +0000916 {
917 va_list va;
918
919 va_copy(va, *((struct va_format *)ptr)->va);
920 buf += vsnprintf(buf, end > buf ? end - buf : 0,
921 ((struct va_format *)ptr)->fmt, va);
922 va_end(va);
923 return buf;
924 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800925 case 'K':
926 /*
927 * %pK cannot be used in IRQ context because its test
928 * for CAP_SYSLOG would be meaningless.
929 */
930 if (in_irq() || in_serving_softirq() || in_nmi()) {
931 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 Mallon74d58172013-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 Mallon74d58172013-11-12 15:08:51 -0800962 break;
963 }
Joe Perches26297602011-03-22 16:34:19 -0700964 break;
Ryan Mallon74d58172013-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;
Stepan Moskovchenkoe9ba0642013-01-21 13:27:25 -0800972 case 'a':
973 spec.flags |= SPECIAL | SMALL | ZEROPAD;
974 spec.field_width = sizeof(phys_addr_t) * 2 + 2;
975 spec.base = 16;
976 return number(buf, end,
977 (unsigned long long) *((phys_addr_t *)ptr), spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700978 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100979 spec.flags |= SMALL;
980 if (spec.field_width == -1) {
Joe Perches5e057982010-10-26 14:22:50 -0700981 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100982 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700983 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100984 spec.base = 16;
985
986 return number(buf, end, (unsigned long) ptr, spec);
987}
988
989/*
990 * Helper function to decode printf style format.
991 * Each call decode a token from the format and return the
992 * number of characters read (or likely the delta where it wants
993 * to go on the next call).
994 * The decoded token is returned through the parameters
995 *
996 * 'h', 'l', or 'L' for integer fields
997 * 'z' support added 23/7/1999 S.H.
998 * 'z' changed to 'Z' --davidm 1/25/99
999 * 't' added for ptrdiff_t
1000 *
1001 * @fmt: the format string
1002 * @type of the token returned
1003 * @flags: various flags such as +, -, # tokens..
1004 * @field_width: overwritten width
1005 * @base: base of the number (octal, hex, ...)
1006 * @precision: precision of a number
1007 * @qualifier: qualifier of a number (long, size_t, ...)
1008 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001009static noinline_for_stack
1010int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001011{
1012 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001013
1014 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001015 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001016 if (spec->field_width < 0) {
1017 spec->field_width = -spec->field_width;
1018 spec->flags |= LEFT;
1019 }
1020 spec->type = FORMAT_TYPE_NONE;
1021 goto precision;
1022 }
1023
1024 /* we finished early by reading the precision */
1025 if (spec->type == FORMAT_TYPE_PRECISION) {
1026 if (spec->precision < 0)
1027 spec->precision = 0;
1028
1029 spec->type = FORMAT_TYPE_NONE;
1030 goto qualifier;
1031 }
1032
1033 /* By default */
1034 spec->type = FORMAT_TYPE_NONE;
1035
1036 for (; *fmt ; ++fmt) {
1037 if (*fmt == '%')
1038 break;
1039 }
1040
1041 /* Return the current non-format string */
1042 if (fmt != start || !*fmt)
1043 return fmt - start;
1044
1045 /* Process flags */
1046 spec->flags = 0;
1047
1048 while (1) { /* this also skips first '%' */
1049 bool found = true;
1050
1051 ++fmt;
1052
1053 switch (*fmt) {
1054 case '-': spec->flags |= LEFT; break;
1055 case '+': spec->flags |= PLUS; break;
1056 case ' ': spec->flags |= SPACE; break;
1057 case '#': spec->flags |= SPECIAL; break;
1058 case '0': spec->flags |= ZEROPAD; break;
1059 default: found = false;
1060 }
1061
1062 if (!found)
1063 break;
1064 }
1065
1066 /* get field width */
1067 spec->field_width = -1;
1068
1069 if (isdigit(*fmt))
1070 spec->field_width = skip_atoi(&fmt);
1071 else if (*fmt == '*') {
1072 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001073 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001074 return ++fmt - start;
1075 }
1076
1077precision:
1078 /* get the precision */
1079 spec->precision = -1;
1080 if (*fmt == '.') {
1081 ++fmt;
1082 if (isdigit(*fmt)) {
1083 spec->precision = skip_atoi(&fmt);
1084 if (spec->precision < 0)
1085 spec->precision = 0;
1086 } else if (*fmt == '*') {
1087 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001088 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001089 return ++fmt - start;
1090 }
1091 }
1092
1093qualifier:
1094 /* get the conversion qualifier */
1095 spec->qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001096 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1097 _tolower(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001098 spec->qualifier = *fmt++;
1099 if (unlikely(spec->qualifier == *fmt)) {
1100 if (spec->qualifier == 'l') {
1101 spec->qualifier = 'L';
1102 ++fmt;
1103 } else if (spec->qualifier == 'h') {
1104 spec->qualifier = 'H';
1105 ++fmt;
1106 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001107 }
1108 }
1109
1110 /* default base */
1111 spec->base = 10;
1112 switch (*fmt) {
1113 case 'c':
1114 spec->type = FORMAT_TYPE_CHAR;
1115 return ++fmt - start;
1116
1117 case 's':
1118 spec->type = FORMAT_TYPE_STR;
1119 return ++fmt - start;
1120
1121 case 'p':
1122 spec->type = FORMAT_TYPE_PTR;
1123 return fmt - start;
1124 /* skip alnum */
1125
1126 case 'n':
1127 spec->type = FORMAT_TYPE_NRCHARS;
1128 return ++fmt - start;
1129
1130 case '%':
1131 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1132 return ++fmt - start;
1133
1134 /* integer number formats - set up the flags and "break" */
1135 case 'o':
1136 spec->base = 8;
1137 break;
1138
1139 case 'x':
1140 spec->flags |= SMALL;
1141
1142 case 'X':
1143 spec->base = 16;
1144 break;
1145
1146 case 'd':
1147 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001148 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001149 case 'u':
1150 break;
1151
1152 default:
1153 spec->type = FORMAT_TYPE_INVALID;
1154 return fmt - start;
1155 }
1156
1157 if (spec->qualifier == 'L')
1158 spec->type = FORMAT_TYPE_LONG_LONG;
1159 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001160 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001161 spec->type = FORMAT_TYPE_LONG;
1162 else
1163 spec->type = FORMAT_TYPE_ULONG;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001164 } else if (_tolower(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001165 spec->type = FORMAT_TYPE_SIZE_T;
1166 } else if (spec->qualifier == 't') {
1167 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001168 } else if (spec->qualifier == 'H') {
1169 if (spec->flags & SIGN)
1170 spec->type = FORMAT_TYPE_BYTE;
1171 else
1172 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001173 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001174 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001175 spec->type = FORMAT_TYPE_SHORT;
1176 else
1177 spec->type = FORMAT_TYPE_USHORT;
1178 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001179 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001180 spec->type = FORMAT_TYPE_INT;
1181 else
1182 spec->type = FORMAT_TYPE_UINT;
1183 }
1184
1185 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001186}
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188/**
1189 * vsnprintf - Format a string and place it in a buffer
1190 * @buf: The buffer to place the result into
1191 * @size: The size of the buffer, including the trailing null space
1192 * @fmt: The format string to use
1193 * @args: Arguments for the format string
1194 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001195 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001196 * %pS output the name of a text symbol with offset
1197 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001198 * %pF output the name of a function pointer with its offset
1199 * %pf output the name of a function pointer without its offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001200 * %pB output the name of a backtrace symbol with its offset
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001201 * %pR output the address range in a struct resource with decoded flags
1202 * %pr output the address range in a struct resource with raw flags
1203 * %pM output a 6-byte MAC address with colons
1204 * %pm output a 6-byte MAC address without colons
1205 * %pI4 print an IPv4 address without leading zeros
1206 * %pi4 print an IPv4 address with leading zeros
1207 * %pI6 print an IPv6 address with colons
1208 * %pi6 print an IPv6 address without colons
Jan Engelhardtf996f202011-07-14 18:48:56 +02001209 * %pI6c print an IPv6 address as specified by RFC 5952
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001210 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1211 * case.
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001212 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001213 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 * The return value is the number of characters which would
1215 * be generated for the given input, excluding the trailing
1216 * '\0', as per ISO C99. If you want to have the exact
1217 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001218 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 * return is greater than or equal to @size, the resulting
1220 * string is truncated.
1221 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001222 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 */
1224int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1225{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001227 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001228 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001230 /* Reject out-of-range values early. Large positive sizes are
1231 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001232 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001236 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001238 /* Make sure end is always >= buf */
1239 if (end < buf) {
1240 end = ((void *)-1);
1241 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
1243
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001244 while (*fmt) {
1245 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001246 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001247
1248 fmt += read;
1249
1250 switch (spec.type) {
1251 case FORMAT_TYPE_NONE: {
1252 int copy = read;
1253 if (str < end) {
1254 if (copy > end - str)
1255 copy = end - str;
1256 memcpy(str, old_fmt, copy);
1257 }
1258 str += read;
1259 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261
Vegard Nossumed681a92009-03-14 12:08:50 +01001262 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001263 spec.field_width = va_arg(args, int);
1264 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001266 case FORMAT_TYPE_PRECISION:
1267 spec.precision = va_arg(args, int);
1268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
André Goddard Rosad4be1512009-12-14 18:00:59 -08001270 case FORMAT_TYPE_CHAR: {
1271 char c;
1272
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001273 if (!(spec.flags & LEFT)) {
1274 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001275 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 *str = ' ';
1277 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001280 }
1281 c = (unsigned char) va_arg(args, int);
1282 if (str < end)
1283 *str = c;
1284 ++str;
1285 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001286 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001287 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001289 }
1290 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001293 case FORMAT_TYPE_STR:
1294 str = string(str, end, va_arg(args, char *), spec);
1295 break;
1296
1297 case FORMAT_TYPE_PTR:
1298 str = pointer(fmt+1, str, end, va_arg(args, void *),
1299 spec);
1300 while (isalnum(*fmt))
1301 fmt++;
1302 break;
1303
1304 case FORMAT_TYPE_PERCENT_CHAR:
1305 if (str < end)
1306 *str = '%';
1307 ++str;
1308 break;
1309
1310 case FORMAT_TYPE_INVALID:
1311 if (str < end)
1312 *str = '%';
1313 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001314 break;
1315
1316 case FORMAT_TYPE_NRCHARS: {
Kees Cook6aa14152013-11-14 14:31:58 -08001317 /*
1318 * Since %n poses a greater security risk than
1319 * utility, ignore %n and skip its argument.
1320 */
1321 void *skip_arg;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001322
Kees Cook6aa14152013-11-14 14:31:58 -08001323 WARN_ONCE(1, "Please remove ignored %%n in '%s'\n",
1324 old_fmt);
1325
1326 skip_arg = va_arg(args, void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001327 break;
1328 }
1329
1330 default:
1331 switch (spec.type) {
1332 case FORMAT_TYPE_LONG_LONG:
1333 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001335 case FORMAT_TYPE_ULONG:
1336 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001338 case FORMAT_TYPE_LONG:
1339 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001341 case FORMAT_TYPE_SIZE_T:
1342 num = va_arg(args, size_t);
1343 break;
1344 case FORMAT_TYPE_PTRDIFF:
1345 num = va_arg(args, ptrdiff_t);
1346 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001347 case FORMAT_TYPE_UBYTE:
1348 num = (unsigned char) va_arg(args, int);
1349 break;
1350 case FORMAT_TYPE_BYTE:
1351 num = (signed char) va_arg(args, int);
1352 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001353 case FORMAT_TYPE_USHORT:
1354 num = (unsigned short) va_arg(args, int);
1355 break;
1356 case FORMAT_TYPE_SHORT:
1357 num = (short) va_arg(args, int);
1358 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001359 case FORMAT_TYPE_INT:
1360 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001361 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001363 num = va_arg(args, unsigned int);
1364 }
1365
1366 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001369
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001370 if (size > 0) {
1371 if (str < end)
1372 *str = '\0';
1373 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001374 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001375 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001376
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001377 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381EXPORT_SYMBOL(vsnprintf);
1382
1383/**
1384 * vscnprintf - Format a string and place it in a buffer
1385 * @buf: The buffer to place the result into
1386 * @size: The size of the buffer, including the trailing null space
1387 * @fmt: The format string to use
1388 * @args: Arguments for the format string
1389 *
1390 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08001391 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * returns 0.
1393 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001394 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001395 *
1396 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 */
1398int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1399{
1400 int i;
1401
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001402 i = vsnprintf(buf, size, fmt, args);
1403
Anton Arapovb921c692011-01-12 16:59:49 -08001404 if (likely(i < size))
1405 return i;
1406 if (size != 0)
1407 return size - 1;
1408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410EXPORT_SYMBOL(vscnprintf);
1411
1412/**
1413 * snprintf - Format a string and place it in a buffer
1414 * @buf: The buffer to place the result into
1415 * @size: The size of the buffer, including the trailing null space
1416 * @fmt: The format string to use
1417 * @...: Arguments for the format string
1418 *
1419 * The return value is the number of characters which would be
1420 * generated for the given input, excluding the trailing null,
1421 * as per ISO C99. If the return is greater than or equal to
1422 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001423 *
1424 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001426int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 va_list args;
1429 int i;
1430
1431 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001432 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return i;
1436}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437EXPORT_SYMBOL(snprintf);
1438
1439/**
1440 * scnprintf - Format a string and place it in a buffer
1441 * @buf: The buffer to place the result into
1442 * @size: The size of the buffer, including the trailing null space
1443 * @fmt: The format string to use
1444 * @...: Arguments for the format string
1445 *
1446 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07001447 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 */
1449
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001450int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 va_list args;
1453 int i;
1454
1455 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08001456 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001458
Anton Arapovb921c692011-01-12 16:59:49 -08001459 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461EXPORT_SYMBOL(scnprintf);
1462
1463/**
1464 * vsprintf - Format a string and place it in a buffer
1465 * @buf: The buffer to place the result into
1466 * @fmt: The format string to use
1467 * @args: Arguments for the format string
1468 *
1469 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001470 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 * buffer overflows.
1472 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001473 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001474 *
1475 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 */
1477int vsprintf(char *buf, const char *fmt, va_list args)
1478{
1479 return vsnprintf(buf, INT_MAX, fmt, args);
1480}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481EXPORT_SYMBOL(vsprintf);
1482
1483/**
1484 * sprintf - Format a string and place it in a buffer
1485 * @buf: The buffer to place the result into
1486 * @fmt: The format string to use
1487 * @...: Arguments for the format string
1488 *
1489 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001490 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001492 *
1493 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001495int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 va_list args;
1498 int i;
1499
1500 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001501 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return i;
1505}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506EXPORT_SYMBOL(sprintf);
1507
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001508#ifdef CONFIG_BINARY_PRINTF
1509/*
1510 * bprintf service:
1511 * vbin_printf() - VA arguments to binary data
1512 * bstr_printf() - Binary data to text string
1513 */
1514
1515/**
1516 * vbin_printf - Parse a format string and place args' binary value in a buffer
1517 * @bin_buf: The buffer to place args' binary value
1518 * @size: The size of the buffer(by words(32bits), not characters)
1519 * @fmt: The format string to use
1520 * @args: Arguments for the format string
1521 *
1522 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1523 * is skiped.
1524 *
1525 * The return value is the number of words(32bits) which would be generated for
1526 * the given input.
1527 *
1528 * NOTE:
1529 * If the return value is greater than @size, the resulting bin_buf is NOT
1530 * valid for bstr_printf().
1531 */
1532int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1533{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001534 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001535 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001536
1537 str = (char *)bin_buf;
1538 end = (char *)(bin_buf + size);
1539
1540#define save_arg(type) \
1541do { \
1542 if (sizeof(type) == 8) { \
1543 unsigned long long value; \
1544 str = PTR_ALIGN(str, sizeof(u32)); \
1545 value = va_arg(args, unsigned long long); \
1546 if (str + sizeof(type) <= end) { \
1547 *(u32 *)str = *(u32 *)&value; \
1548 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1549 } \
1550 } else { \
1551 unsigned long value; \
1552 str = PTR_ALIGN(str, sizeof(type)); \
1553 value = va_arg(args, int); \
1554 if (str + sizeof(type) <= end) \
1555 *(typeof(type) *)str = (type)value; \
1556 } \
1557 str += sizeof(type); \
1558} while (0)
1559
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001560 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08001561 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001562
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001563 fmt += read;
1564
1565 switch (spec.type) {
1566 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08001567 case FORMAT_TYPE_INVALID:
1568 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001569 break;
1570
Vegard Nossumed681a92009-03-14 12:08:50 +01001571 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001572 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001573 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001574 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001575
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001576 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001577 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001578 break;
1579
1580 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001581 const char *save_str = va_arg(args, char *);
1582 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08001583
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001584 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1585 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08001586 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08001587 len = strlen(save_str) + 1;
1588 if (str + len < end)
1589 memcpy(str, save_str, len);
1590 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001591 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001592 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001593
1594 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001595 save_arg(void *);
1596 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001597 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001598 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001599 break;
1600
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001601 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001602 /* skip %n 's argument */
Joe Perchesef0658f2010-03-06 17:10:14 -08001603 u8 qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001604 void *skip_arg;
1605 if (qualifier == 'l')
1606 skip_arg = va_arg(args, long *);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001607 else if (_tolower(qualifier) == 'z')
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001608 skip_arg = va_arg(args, size_t *);
1609 else
1610 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001611 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001612 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001613
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001614 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001615 switch (spec.type) {
1616
1617 case FORMAT_TYPE_LONG_LONG:
1618 save_arg(long long);
1619 break;
1620 case FORMAT_TYPE_ULONG:
1621 case FORMAT_TYPE_LONG:
1622 save_arg(unsigned long);
1623 break;
1624 case FORMAT_TYPE_SIZE_T:
1625 save_arg(size_t);
1626 break;
1627 case FORMAT_TYPE_PTRDIFF:
1628 save_arg(ptrdiff_t);
1629 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001630 case FORMAT_TYPE_UBYTE:
1631 case FORMAT_TYPE_BYTE:
1632 save_arg(char);
1633 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001634 case FORMAT_TYPE_USHORT:
1635 case FORMAT_TYPE_SHORT:
1636 save_arg(short);
1637 break;
1638 default:
1639 save_arg(int);
1640 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001641 }
1642 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001643
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001644 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001645#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001646}
1647EXPORT_SYMBOL_GPL(vbin_printf);
1648
1649/**
1650 * bstr_printf - Format a string from binary arguments and place it in a buffer
1651 * @buf: The buffer to place the result into
1652 * @size: The size of the buffer, including the trailing null space
1653 * @fmt: The format string to use
1654 * @bin_buf: Binary arguments for the format string
1655 *
1656 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1657 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1658 * a binary buffer that generated by vbin_printf.
1659 *
1660 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001661 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001662 *
1663 * The return value is the number of characters which would
1664 * be generated for the given input, excluding the trailing
1665 * '\0', as per ISO C99. If you want to have the exact
1666 * number of characters written into @buf as return value
1667 * (not including the trailing '\0'), use vscnprintf(). If the
1668 * return is greater than or equal to @size, the resulting
1669 * string is truncated.
1670 */
1671int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1672{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001673 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08001674 char *str, *end;
1675 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001676
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001677 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001678 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001679
1680 str = buf;
1681 end = buf + size;
1682
1683#define get_arg(type) \
1684({ \
1685 typeof(type) value; \
1686 if (sizeof(type) == 8) { \
1687 args = PTR_ALIGN(args, sizeof(u32)); \
1688 *(u32 *)&value = *(u32 *)args; \
1689 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1690 } else { \
1691 args = PTR_ALIGN(args, sizeof(type)); \
1692 value = *(typeof(type) *)args; \
1693 } \
1694 args += sizeof(type); \
1695 value; \
1696})
1697
1698 /* Make sure end is always >= buf */
1699 if (end < buf) {
1700 end = ((void *)-1);
1701 size = end - buf;
1702 }
1703
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001704 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001705 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001706 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001707
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001708 fmt += read;
1709
1710 switch (spec.type) {
1711 case FORMAT_TYPE_NONE: {
1712 int copy = read;
1713 if (str < end) {
1714 if (copy > end - str)
1715 copy = end - str;
1716 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001717 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001718 str += read;
1719 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001720 }
1721
Vegard Nossumed681a92009-03-14 12:08:50 +01001722 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001723 spec.field_width = get_arg(int);
1724 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001725
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001726 case FORMAT_TYPE_PRECISION:
1727 spec.precision = get_arg(int);
1728 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001729
André Goddard Rosad4be1512009-12-14 18:00:59 -08001730 case FORMAT_TYPE_CHAR: {
1731 char c;
1732
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001733 if (!(spec.flags & LEFT)) {
1734 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001735 if (str < end)
1736 *str = ' ';
1737 ++str;
1738 }
1739 }
1740 c = (unsigned char) get_arg(char);
1741 if (str < end)
1742 *str = c;
1743 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001744 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001745 if (str < end)
1746 *str = ' ';
1747 ++str;
1748 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001749 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001750 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001751
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001752 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001753 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001754 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001755 str = string(str, end, (char *)str_arg, spec);
1756 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001757 }
1758
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001759 case FORMAT_TYPE_PTR:
1760 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1761 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001762 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001763 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001764
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001765 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001766 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001767 if (str < end)
1768 *str = '%';
1769 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001770 break;
1771
1772 case FORMAT_TYPE_NRCHARS:
1773 /* skip */
1774 break;
1775
André Goddard Rosad4be1512009-12-14 18:00:59 -08001776 default: {
1777 unsigned long long num;
1778
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001779 switch (spec.type) {
1780
1781 case FORMAT_TYPE_LONG_LONG:
1782 num = get_arg(long long);
1783 break;
1784 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001785 case FORMAT_TYPE_LONG:
1786 num = get_arg(unsigned long);
1787 break;
1788 case FORMAT_TYPE_SIZE_T:
1789 num = get_arg(size_t);
1790 break;
1791 case FORMAT_TYPE_PTRDIFF:
1792 num = get_arg(ptrdiff_t);
1793 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001794 case FORMAT_TYPE_UBYTE:
1795 num = get_arg(unsigned char);
1796 break;
1797 case FORMAT_TYPE_BYTE:
1798 num = get_arg(signed char);
1799 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001800 case FORMAT_TYPE_USHORT:
1801 num = get_arg(unsigned short);
1802 break;
1803 case FORMAT_TYPE_SHORT:
1804 num = get_arg(short);
1805 break;
1806 case FORMAT_TYPE_UINT:
1807 num = get_arg(unsigned int);
1808 break;
1809 default:
1810 num = get_arg(int);
1811 }
1812
1813 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08001814 } /* default: */
1815 } /* switch(spec.type) */
1816 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001817
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001818 if (size > 0) {
1819 if (str < end)
1820 *str = '\0';
1821 else
1822 end[-1] = '\0';
1823 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001824
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001825#undef get_arg
1826
1827 /* the trailing null byte doesn't count towards the total */
1828 return str - buf;
1829}
1830EXPORT_SYMBOL_GPL(bstr_printf);
1831
1832/**
1833 * bprintf - Parse a format string and place args' binary value in a buffer
1834 * @bin_buf: The buffer to place args' binary value
1835 * @size: The size of the buffer(by words(32bits), not characters)
1836 * @fmt: The format string to use
1837 * @...: Arguments for the format string
1838 *
1839 * The function returns the number of words(u32) written
1840 * into @bin_buf.
1841 */
1842int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1843{
1844 va_list args;
1845 int ret;
1846
1847 va_start(args, fmt);
1848 ret = vbin_printf(bin_buf, size, fmt, args);
1849 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001850
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001851 return ret;
1852}
1853EXPORT_SYMBOL_GPL(bprintf);
1854
1855#endif /* CONFIG_BINARY_PRINTF */
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857/**
1858 * vsscanf - Unformat a buffer into a list of arguments
1859 * @buf: input buffer
1860 * @fmt: format of buffer
1861 * @args: arguments
1862 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001863int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
1865 const char *str = buf;
1866 char *next;
1867 char digit;
1868 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08001869 u8 qualifier;
1870 u8 base;
1871 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001872 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001874 while (*fmt && *str) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 /* skip any white space in format */
1876 /* white space in format matchs any amount of
1877 * white space, including none, in the input.
1878 */
1879 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08001880 fmt = skip_spaces(++fmt);
1881 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883
1884 /* anything that is not a conversion must match exactly */
1885 if (*fmt != '%' && *fmt) {
1886 if (*fmt++ != *str++)
1887 break;
1888 continue;
1889 }
1890
1891 if (!*fmt)
1892 break;
1893 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 /* skip this conversion.
1896 * advance both strings to next white space
1897 */
1898 if (*fmt == '*') {
Andy Spencer8fccae22009-10-01 15:44:27 -07001899 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 fmt++;
1901 while (!isspace(*str) && *str)
1902 str++;
1903 continue;
1904 }
1905
1906 /* get field width */
1907 field_width = -1;
1908 if (isdigit(*fmt))
1909 field_width = skip_atoi(&fmt);
1910
1911 /* get conversion qualifier */
1912 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001913 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1914 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 qualifier = *fmt++;
1916 if (unlikely(qualifier == *fmt)) {
1917 if (qualifier == 'h') {
1918 qualifier = 'H';
1919 fmt++;
1920 } else if (qualifier == 'l') {
1921 qualifier = 'L';
1922 fmt++;
1923 }
1924 }
1925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 if (!*fmt || !*str)
1928 break;
1929
André Goddard Rosad4be1512009-12-14 18:00:59 -08001930 base = 10;
1931 is_sign = 0;
1932
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001933 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 case 'c':
1935 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001936 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (field_width == -1)
1938 field_width = 1;
1939 do {
1940 *s++ = *str++;
1941 } while (--field_width > 0 && *str);
1942 num++;
1943 }
1944 continue;
1945 case 's':
1946 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001947 char *s = (char *)va_arg(args, char *);
1948 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07001949 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08001951 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001954 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 *s = '\0';
1957 num++;
1958 }
1959 continue;
1960 case 'n':
1961 /* return number of characters read so far */
1962 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001963 int *i = (int *)va_arg(args, int*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 *i = str - buf;
1965 }
1966 continue;
1967 case 'o':
1968 base = 8;
1969 break;
1970 case 'x':
1971 case 'X':
1972 base = 16;
1973 break;
1974 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001975 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 case 'd':
1977 is_sign = 1;
1978 case 'u':
1979 break;
1980 case '%':
1981 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001982 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return num;
1984 continue;
1985 default:
1986 /* invalid format; stop here */
1987 return num;
1988 }
1989
1990 /* have some sort of integer conversion.
1991 * first, skip white space in buffer.
1992 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08001993 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 digit = *str;
1996 if (is_sign && digit == '-')
1997 digit = *(str + 1);
1998
1999 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002000 || (base == 16 && !isxdigit(digit))
2001 || (base == 10 && !isdigit(digit))
2002 || (base == 8 && (!isdigit(digit) || digit > '7'))
2003 || (base == 0 && !isdigit(digit)))
2004 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002006 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 case 'H': /* that's 'hh' in format */
2008 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002009 signed char *s = (signed char *)va_arg(args, signed char *);
2010 *s = (signed char)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002012 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
2013 *s = (unsigned char)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015 break;
2016 case 'h':
2017 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002018 short *s = (short *)va_arg(args, short *);
2019 *s = (short)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002021 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
2022 *s = (unsigned short)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
2024 break;
2025 case 'l':
2026 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002027 long *l = (long *)va_arg(args, long *);
2028 *l = simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002030 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
2031 *l = simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033 break;
2034 case 'L':
2035 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002036 long long *l = (long long *)va_arg(args, long long *);
2037 *l = simple_strtoll(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002039 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2040 *l = simple_strtoull(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
2042 break;
2043 case 'Z':
2044 case 'z':
2045 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002046 size_t *s = (size_t *)va_arg(args, size_t *);
2047 *s = (size_t)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
2049 break;
2050 default:
2051 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002052 int *i = (int *)va_arg(args, int *);
2053 *i = (int)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002055 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2056 *i = (unsigned int)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
2058 break;
2059 }
2060 num++;
2061
2062 if (!next)
2063 break;
2064 str = next;
2065 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002066
2067 /*
2068 * Now we've come all the way through so either the input string or the
2069 * format ended. In the former case, there can be a %n at the current
2070 * position in the format that needs to be filled.
2071 */
2072 if (*fmt == '%' && *(fmt + 1) == 'n') {
2073 int *p = (int *)va_arg(args, int *);
2074 *p = str - buf;
2075 }
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return num;
2078}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079EXPORT_SYMBOL(vsscanf);
2080
2081/**
2082 * sscanf - Unformat a buffer into a list of arguments
2083 * @buf: input buffer
2084 * @fmt: formatting of buffer
2085 * @...: resulting arguments
2086 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002087int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 va_list args;
2090 int i;
2091
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002092 va_start(args, fmt);
2093 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return i;
2097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098EXPORT_SYMBOL(sscanf);