blob: 73e2c45acfdb61f655a6691769fceb55aae3f067 [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
440 kallsyms_lookup(value, NULL, NULL, NULL, sym);
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 */
929 if (in_irq() || in_serving_softirq() || in_nmi()) {
930 if (spec.field_width == -1)
931 spec.field_width = 2 * sizeof(void *);
932 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800933 }
Ryan Mallon22363fb2013-11-12 15:08:51 -0800934
935 switch (kptr_restrict) {
936 case 0:
937 /* Always print %pK values */
938 break;
939 case 1: {
940 /*
941 * Only print the real pointer value if the current
942 * process has CAP_SYSLOG and is running with the
943 * same credentials it started with. This is because
944 * access to files is checked at open() time, but %pK
945 * checks permission at read() time. We don't want to
946 * leak pointer values if a binary opens a file using
947 * %pK and then elevates privileges before reading it.
948 */
949 const struct cred *cred = current_cred();
950
951 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
952 (cred->euid != cred->uid) ||
953 (cred->egid != cred->gid))
954 ptr = NULL;
955 break;
956 }
957 case 2:
958 default:
959 /* Always print 0's for %pK */
Joe Perches26297602011-03-22 16:34:19 -0700960 ptr = NULL;
Ryan Mallon22363fb2013-11-12 15:08:51 -0800961 break;
962 }
Joe Perches26297602011-03-22 16:34:19 -0700963 break;
Ryan Mallon22363fb2013-11-12 15:08:51 -0800964
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000965 case 'N':
966 switch (fmt[1]) {
967 case 'F':
968 return netdev_feature_string(buf, end, ptr, spec);
969 }
970 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700971 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100972 spec.flags |= SMALL;
973 if (spec.field_width == -1) {
Joe Perches5e057982010-10-26 14:22:50 -0700974 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100975 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700976 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100977 spec.base = 16;
978
979 return number(buf, end, (unsigned long) ptr, spec);
980}
981
982/*
983 * Helper function to decode printf style format.
984 * Each call decode a token from the format and return the
985 * number of characters read (or likely the delta where it wants
986 * to go on the next call).
987 * The decoded token is returned through the parameters
988 *
989 * 'h', 'l', or 'L' for integer fields
990 * 'z' support added 23/7/1999 S.H.
991 * 'z' changed to 'Z' --davidm 1/25/99
992 * 't' added for ptrdiff_t
993 *
994 * @fmt: the format string
995 * @type of the token returned
996 * @flags: various flags such as +, -, # tokens..
997 * @field_width: overwritten width
998 * @base: base of the number (octal, hex, ...)
999 * @precision: precision of a number
1000 * @qualifier: qualifier of a number (long, size_t, ...)
1001 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001002static noinline_for_stack
1003int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001004{
1005 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001006
1007 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001008 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001009 if (spec->field_width < 0) {
1010 spec->field_width = -spec->field_width;
1011 spec->flags |= LEFT;
1012 }
1013 spec->type = FORMAT_TYPE_NONE;
1014 goto precision;
1015 }
1016
1017 /* we finished early by reading the precision */
1018 if (spec->type == FORMAT_TYPE_PRECISION) {
1019 if (spec->precision < 0)
1020 spec->precision = 0;
1021
1022 spec->type = FORMAT_TYPE_NONE;
1023 goto qualifier;
1024 }
1025
1026 /* By default */
1027 spec->type = FORMAT_TYPE_NONE;
1028
1029 for (; *fmt ; ++fmt) {
1030 if (*fmt == '%')
1031 break;
1032 }
1033
1034 /* Return the current non-format string */
1035 if (fmt != start || !*fmt)
1036 return fmt - start;
1037
1038 /* Process flags */
1039 spec->flags = 0;
1040
1041 while (1) { /* this also skips first '%' */
1042 bool found = true;
1043
1044 ++fmt;
1045
1046 switch (*fmt) {
1047 case '-': spec->flags |= LEFT; break;
1048 case '+': spec->flags |= PLUS; break;
1049 case ' ': spec->flags |= SPACE; break;
1050 case '#': spec->flags |= SPECIAL; break;
1051 case '0': spec->flags |= ZEROPAD; break;
1052 default: found = false;
1053 }
1054
1055 if (!found)
1056 break;
1057 }
1058
1059 /* get field width */
1060 spec->field_width = -1;
1061
1062 if (isdigit(*fmt))
1063 spec->field_width = skip_atoi(&fmt);
1064 else if (*fmt == '*') {
1065 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001066 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001067 return ++fmt - start;
1068 }
1069
1070precision:
1071 /* get the precision */
1072 spec->precision = -1;
1073 if (*fmt == '.') {
1074 ++fmt;
1075 if (isdigit(*fmt)) {
1076 spec->precision = skip_atoi(&fmt);
1077 if (spec->precision < 0)
1078 spec->precision = 0;
1079 } else if (*fmt == '*') {
1080 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001081 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001082 return ++fmt - start;
1083 }
1084 }
1085
1086qualifier:
1087 /* get the conversion qualifier */
1088 spec->qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001089 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1090 _tolower(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001091 spec->qualifier = *fmt++;
1092 if (unlikely(spec->qualifier == *fmt)) {
1093 if (spec->qualifier == 'l') {
1094 spec->qualifier = 'L';
1095 ++fmt;
1096 } else if (spec->qualifier == 'h') {
1097 spec->qualifier = 'H';
1098 ++fmt;
1099 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001100 }
1101 }
1102
1103 /* default base */
1104 spec->base = 10;
1105 switch (*fmt) {
1106 case 'c':
1107 spec->type = FORMAT_TYPE_CHAR;
1108 return ++fmt - start;
1109
1110 case 's':
1111 spec->type = FORMAT_TYPE_STR;
1112 return ++fmt - start;
1113
1114 case 'p':
1115 spec->type = FORMAT_TYPE_PTR;
1116 return fmt - start;
1117 /* skip alnum */
1118
1119 case 'n':
1120 spec->type = FORMAT_TYPE_NRCHARS;
1121 return ++fmt - start;
1122
1123 case '%':
1124 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1125 return ++fmt - start;
1126
1127 /* integer number formats - set up the flags and "break" */
1128 case 'o':
1129 spec->base = 8;
1130 break;
1131
1132 case 'x':
1133 spec->flags |= SMALL;
1134
1135 case 'X':
1136 spec->base = 16;
1137 break;
1138
1139 case 'd':
1140 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001141 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001142 case 'u':
1143 break;
1144
1145 default:
1146 spec->type = FORMAT_TYPE_INVALID;
1147 return fmt - start;
1148 }
1149
1150 if (spec->qualifier == 'L')
1151 spec->type = FORMAT_TYPE_LONG_LONG;
1152 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001153 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001154 spec->type = FORMAT_TYPE_LONG;
1155 else
1156 spec->type = FORMAT_TYPE_ULONG;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001157 } else if (_tolower(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001158 spec->type = FORMAT_TYPE_SIZE_T;
1159 } else if (spec->qualifier == 't') {
1160 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001161 } else if (spec->qualifier == 'H') {
1162 if (spec->flags & SIGN)
1163 spec->type = FORMAT_TYPE_BYTE;
1164 else
1165 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001166 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001167 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001168 spec->type = FORMAT_TYPE_SHORT;
1169 else
1170 spec->type = FORMAT_TYPE_USHORT;
1171 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001172 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001173 spec->type = FORMAT_TYPE_INT;
1174 else
1175 spec->type = FORMAT_TYPE_UINT;
1176 }
1177
1178 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001179}
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181/**
1182 * vsnprintf - Format a string and place it in a buffer
1183 * @buf: The buffer to place the result into
1184 * @size: The size of the buffer, including the trailing null space
1185 * @fmt: The format string to use
1186 * @args: Arguments for the format string
1187 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001188 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001189 * %pS output the name of a text symbol with offset
1190 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001191 * %pF output the name of a function pointer with its offset
1192 * %pf output the name of a function pointer without its offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001193 * %pB output the name of a backtrace symbol with its offset
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001194 * %pR output the address range in a struct resource with decoded flags
1195 * %pr output the address range in a struct resource with raw flags
1196 * %pM output a 6-byte MAC address with colons
1197 * %pm output a 6-byte MAC address without colons
1198 * %pI4 print an IPv4 address without leading zeros
1199 * %pi4 print an IPv4 address with leading zeros
1200 * %pI6 print an IPv6 address with colons
1201 * %pi6 print an IPv6 address without colons
Jan Engelhardtf996f202011-07-14 18:48:56 +02001202 * %pI6c print an IPv6 address as specified by RFC 5952
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001203 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1204 * case.
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001205 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001206 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 * The return value is the number of characters which would
1208 * be generated for the given input, excluding the trailing
1209 * '\0', as per ISO C99. If you want to have the exact
1210 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001211 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 * return is greater than or equal to @size, the resulting
1213 * string is truncated.
1214 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001215 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
1217int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1218{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001220 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001221 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001223 /* Reject out-of-range values early. Large positive sizes are
1224 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001225 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001229 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001231 /* Make sure end is always >= buf */
1232 if (end < buf) {
1233 end = ((void *)-1);
1234 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001237 while (*fmt) {
1238 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001239 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001240
1241 fmt += read;
1242
1243 switch (spec.type) {
1244 case FORMAT_TYPE_NONE: {
1245 int copy = read;
1246 if (str < end) {
1247 if (copy > end - str)
1248 copy = end - str;
1249 memcpy(str, old_fmt, copy);
1250 }
1251 str += read;
1252 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
Vegard Nossumed681a92009-03-14 12:08:50 +01001255 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001256 spec.field_width = va_arg(args, int);
1257 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001259 case FORMAT_TYPE_PRECISION:
1260 spec.precision = va_arg(args, int);
1261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
André Goddard Rosad4be1512009-12-14 18:00:59 -08001263 case FORMAT_TYPE_CHAR: {
1264 char c;
1265
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001266 if (!(spec.flags & LEFT)) {
1267 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001268 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 *str = ' ';
1270 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001273 }
1274 c = (unsigned char) va_arg(args, int);
1275 if (str < end)
1276 *str = c;
1277 ++str;
1278 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001279 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001280 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001282 }
1283 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001286 case FORMAT_TYPE_STR:
1287 str = string(str, end, va_arg(args, char *), spec);
1288 break;
1289
1290 case FORMAT_TYPE_PTR:
1291 str = pointer(fmt+1, str, end, va_arg(args, void *),
1292 spec);
1293 while (isalnum(*fmt))
1294 fmt++;
1295 break;
1296
1297 case FORMAT_TYPE_PERCENT_CHAR:
1298 if (str < end)
1299 *str = '%';
1300 ++str;
1301 break;
1302
1303 case FORMAT_TYPE_INVALID:
1304 if (str < end)
1305 *str = '%';
1306 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001307 break;
1308
1309 case FORMAT_TYPE_NRCHARS: {
Joe Perchesef0658f2010-03-06 17:10:14 -08001310 u8 qualifier = spec.qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001311
1312 if (qualifier == 'l') {
1313 long *ip = va_arg(args, long *);
1314 *ip = (str - buf);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001315 } else if (_tolower(qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001316 size_t *ip = va_arg(args, size_t *);
1317 *ip = (str - buf);
1318 } else {
1319 int *ip = va_arg(args, int *);
1320 *ip = (str - buf);
1321 }
1322 break;
1323 }
1324
1325 default:
1326 switch (spec.type) {
1327 case FORMAT_TYPE_LONG_LONG:
1328 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001330 case FORMAT_TYPE_ULONG:
1331 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001333 case FORMAT_TYPE_LONG:
1334 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001336 case FORMAT_TYPE_SIZE_T:
1337 num = va_arg(args, size_t);
1338 break;
1339 case FORMAT_TYPE_PTRDIFF:
1340 num = va_arg(args, ptrdiff_t);
1341 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001342 case FORMAT_TYPE_UBYTE:
1343 num = (unsigned char) va_arg(args, int);
1344 break;
1345 case FORMAT_TYPE_BYTE:
1346 num = (signed char) va_arg(args, int);
1347 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001348 case FORMAT_TYPE_USHORT:
1349 num = (unsigned short) va_arg(args, int);
1350 break;
1351 case FORMAT_TYPE_SHORT:
1352 num = (short) va_arg(args, int);
1353 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001354 case FORMAT_TYPE_INT:
1355 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001356 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001358 num = va_arg(args, unsigned int);
1359 }
1360
1361 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001364
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001365 if (size > 0) {
1366 if (str < end)
1367 *str = '\0';
1368 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001369 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001370 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001371
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001372 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376EXPORT_SYMBOL(vsnprintf);
1377
1378/**
1379 * vscnprintf - Format a string and place it in a buffer
1380 * @buf: The buffer to place the result into
1381 * @size: The size of the buffer, including the trailing null space
1382 * @fmt: The format string to use
1383 * @args: Arguments for the format string
1384 *
1385 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08001386 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * returns 0.
1388 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001389 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001390 *
1391 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 */
1393int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1394{
1395 int i;
1396
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001397 i = vsnprintf(buf, size, fmt, args);
1398
Anton Arapovb921c692011-01-12 16:59:49 -08001399 if (likely(i < size))
1400 return i;
1401 if (size != 0)
1402 return size - 1;
1403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405EXPORT_SYMBOL(vscnprintf);
1406
1407/**
1408 * snprintf - Format a string and place it in a buffer
1409 * @buf: The buffer to place the result into
1410 * @size: The size of the buffer, including the trailing null space
1411 * @fmt: The format string to use
1412 * @...: Arguments for the format string
1413 *
1414 * The return value is the number of characters which would be
1415 * generated for the given input, excluding the trailing null,
1416 * as per ISO C99. If the return is greater than or equal to
1417 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001418 *
1419 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001421int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 va_list args;
1424 int i;
1425
1426 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001427 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return i;
1431}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432EXPORT_SYMBOL(snprintf);
1433
1434/**
1435 * scnprintf - Format a string and place it in a buffer
1436 * @buf: The buffer to place the result into
1437 * @size: The size of the buffer, including the trailing null space
1438 * @fmt: The format string to use
1439 * @...: Arguments for the format string
1440 *
1441 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07001442 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
1444
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001445int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 va_list args;
1448 int i;
1449
1450 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08001451 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001453
Anton Arapovb921c692011-01-12 16:59:49 -08001454 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456EXPORT_SYMBOL(scnprintf);
1457
1458/**
1459 * vsprintf - Format a string and place it in a buffer
1460 * @buf: The buffer to place the result into
1461 * @fmt: The format string to use
1462 * @args: Arguments for the format string
1463 *
1464 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001465 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 * buffer overflows.
1467 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001468 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001469 *
1470 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 */
1472int vsprintf(char *buf, const char *fmt, va_list args)
1473{
1474 return vsnprintf(buf, INT_MAX, fmt, args);
1475}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476EXPORT_SYMBOL(vsprintf);
1477
1478/**
1479 * sprintf - Format a string and place it in a buffer
1480 * @buf: The buffer to place the result into
1481 * @fmt: The format string to use
1482 * @...: Arguments for the format string
1483 *
1484 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001485 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001487 *
1488 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001490int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 va_list args;
1493 int i;
1494
1495 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001496 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return i;
1500}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501EXPORT_SYMBOL(sprintf);
1502
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001503#ifdef CONFIG_BINARY_PRINTF
1504/*
1505 * bprintf service:
1506 * vbin_printf() - VA arguments to binary data
1507 * bstr_printf() - Binary data to text string
1508 */
1509
1510/**
1511 * vbin_printf - Parse a format string and place args' binary value in a buffer
1512 * @bin_buf: The buffer to place args' binary value
1513 * @size: The size of the buffer(by words(32bits), not characters)
1514 * @fmt: The format string to use
1515 * @args: Arguments for the format string
1516 *
1517 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1518 * is skiped.
1519 *
1520 * The return value is the number of words(32bits) which would be generated for
1521 * the given input.
1522 *
1523 * NOTE:
1524 * If the return value is greater than @size, the resulting bin_buf is NOT
1525 * valid for bstr_printf().
1526 */
1527int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1528{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001529 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001530 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001531
1532 str = (char *)bin_buf;
1533 end = (char *)(bin_buf + size);
1534
1535#define save_arg(type) \
1536do { \
1537 if (sizeof(type) == 8) { \
1538 unsigned long long value; \
1539 str = PTR_ALIGN(str, sizeof(u32)); \
1540 value = va_arg(args, unsigned long long); \
1541 if (str + sizeof(type) <= end) { \
1542 *(u32 *)str = *(u32 *)&value; \
1543 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1544 } \
1545 } else { \
1546 unsigned long value; \
1547 str = PTR_ALIGN(str, sizeof(type)); \
1548 value = va_arg(args, int); \
1549 if (str + sizeof(type) <= end) \
1550 *(typeof(type) *)str = (type)value; \
1551 } \
1552 str += sizeof(type); \
1553} while (0)
1554
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001555 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08001556 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001557
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001558 fmt += read;
1559
1560 switch (spec.type) {
1561 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08001562 case FORMAT_TYPE_INVALID:
1563 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001564 break;
1565
Vegard Nossumed681a92009-03-14 12:08:50 +01001566 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001567 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001568 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001569 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001570
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001571 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001572 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001573 break;
1574
1575 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001576 const char *save_str = va_arg(args, char *);
1577 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08001578
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001579 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1580 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08001581 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08001582 len = strlen(save_str) + 1;
1583 if (str + len < end)
1584 memcpy(str, save_str, len);
1585 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001586 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001587 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001588
1589 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001590 save_arg(void *);
1591 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001592 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001593 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001594 break;
1595
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001596 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001597 /* skip %n 's argument */
Joe Perchesef0658f2010-03-06 17:10:14 -08001598 u8 qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001599 void *skip_arg;
1600 if (qualifier == 'l')
1601 skip_arg = va_arg(args, long *);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001602 else if (_tolower(qualifier) == 'z')
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001603 skip_arg = va_arg(args, size_t *);
1604 else
1605 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001606 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001607 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001608
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001609 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001610 switch (spec.type) {
1611
1612 case FORMAT_TYPE_LONG_LONG:
1613 save_arg(long long);
1614 break;
1615 case FORMAT_TYPE_ULONG:
1616 case FORMAT_TYPE_LONG:
1617 save_arg(unsigned long);
1618 break;
1619 case FORMAT_TYPE_SIZE_T:
1620 save_arg(size_t);
1621 break;
1622 case FORMAT_TYPE_PTRDIFF:
1623 save_arg(ptrdiff_t);
1624 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001625 case FORMAT_TYPE_UBYTE:
1626 case FORMAT_TYPE_BYTE:
1627 save_arg(char);
1628 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001629 case FORMAT_TYPE_USHORT:
1630 case FORMAT_TYPE_SHORT:
1631 save_arg(short);
1632 break;
1633 default:
1634 save_arg(int);
1635 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001636 }
1637 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001638
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001639 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001640#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001641}
1642EXPORT_SYMBOL_GPL(vbin_printf);
1643
1644/**
1645 * bstr_printf - Format a string from binary arguments and place it in a buffer
1646 * @buf: The buffer to place the result into
1647 * @size: The size of the buffer, including the trailing null space
1648 * @fmt: The format string to use
1649 * @bin_buf: Binary arguments for the format string
1650 *
1651 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1652 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1653 * a binary buffer that generated by vbin_printf.
1654 *
1655 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001656 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001657 *
1658 * The return value is the number of characters which would
1659 * be generated for the given input, excluding the trailing
1660 * '\0', as per ISO C99. If you want to have the exact
1661 * number of characters written into @buf as return value
1662 * (not including the trailing '\0'), use vscnprintf(). If the
1663 * return is greater than or equal to @size, the resulting
1664 * string is truncated.
1665 */
1666int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1667{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001668 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08001669 char *str, *end;
1670 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001671
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001672 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001673 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001674
1675 str = buf;
1676 end = buf + size;
1677
1678#define get_arg(type) \
1679({ \
1680 typeof(type) value; \
1681 if (sizeof(type) == 8) { \
1682 args = PTR_ALIGN(args, sizeof(u32)); \
1683 *(u32 *)&value = *(u32 *)args; \
1684 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1685 } else { \
1686 args = PTR_ALIGN(args, sizeof(type)); \
1687 value = *(typeof(type) *)args; \
1688 } \
1689 args += sizeof(type); \
1690 value; \
1691})
1692
1693 /* Make sure end is always >= buf */
1694 if (end < buf) {
1695 end = ((void *)-1);
1696 size = end - buf;
1697 }
1698
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001699 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001700 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001701 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001702
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001703 fmt += read;
1704
1705 switch (spec.type) {
1706 case FORMAT_TYPE_NONE: {
1707 int copy = read;
1708 if (str < end) {
1709 if (copy > end - str)
1710 copy = end - str;
1711 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001712 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001713 str += read;
1714 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001715 }
1716
Vegard Nossumed681a92009-03-14 12:08:50 +01001717 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001718 spec.field_width = get_arg(int);
1719 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001720
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001721 case FORMAT_TYPE_PRECISION:
1722 spec.precision = get_arg(int);
1723 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001724
André Goddard Rosad4be1512009-12-14 18:00:59 -08001725 case FORMAT_TYPE_CHAR: {
1726 char c;
1727
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001728 if (!(spec.flags & LEFT)) {
1729 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001730 if (str < end)
1731 *str = ' ';
1732 ++str;
1733 }
1734 }
1735 c = (unsigned char) get_arg(char);
1736 if (str < end)
1737 *str = c;
1738 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001739 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001740 if (str < end)
1741 *str = ' ';
1742 ++str;
1743 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001744 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001745 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001746
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001747 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001748 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001749 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001750 str = string(str, end, (char *)str_arg, spec);
1751 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001752 }
1753
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001754 case FORMAT_TYPE_PTR:
1755 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1756 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001757 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001758 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001759
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001760 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001761 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001762 if (str < end)
1763 *str = '%';
1764 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001765 break;
1766
1767 case FORMAT_TYPE_NRCHARS:
1768 /* skip */
1769 break;
1770
André Goddard Rosad4be1512009-12-14 18:00:59 -08001771 default: {
1772 unsigned long long num;
1773
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001774 switch (spec.type) {
1775
1776 case FORMAT_TYPE_LONG_LONG:
1777 num = get_arg(long long);
1778 break;
1779 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001780 case FORMAT_TYPE_LONG:
1781 num = get_arg(unsigned long);
1782 break;
1783 case FORMAT_TYPE_SIZE_T:
1784 num = get_arg(size_t);
1785 break;
1786 case FORMAT_TYPE_PTRDIFF:
1787 num = get_arg(ptrdiff_t);
1788 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001789 case FORMAT_TYPE_UBYTE:
1790 num = get_arg(unsigned char);
1791 break;
1792 case FORMAT_TYPE_BYTE:
1793 num = get_arg(signed char);
1794 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001795 case FORMAT_TYPE_USHORT:
1796 num = get_arg(unsigned short);
1797 break;
1798 case FORMAT_TYPE_SHORT:
1799 num = get_arg(short);
1800 break;
1801 case FORMAT_TYPE_UINT:
1802 num = get_arg(unsigned int);
1803 break;
1804 default:
1805 num = get_arg(int);
1806 }
1807
1808 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08001809 } /* default: */
1810 } /* switch(spec.type) */
1811 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001812
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001813 if (size > 0) {
1814 if (str < end)
1815 *str = '\0';
1816 else
1817 end[-1] = '\0';
1818 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001819
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001820#undef get_arg
1821
1822 /* the trailing null byte doesn't count towards the total */
1823 return str - buf;
1824}
1825EXPORT_SYMBOL_GPL(bstr_printf);
1826
1827/**
1828 * bprintf - Parse a format string and place args' binary value in a buffer
1829 * @bin_buf: The buffer to place args' binary value
1830 * @size: The size of the buffer(by words(32bits), not characters)
1831 * @fmt: The format string to use
1832 * @...: Arguments for the format string
1833 *
1834 * The function returns the number of words(u32) written
1835 * into @bin_buf.
1836 */
1837int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1838{
1839 va_list args;
1840 int ret;
1841
1842 va_start(args, fmt);
1843 ret = vbin_printf(bin_buf, size, fmt, args);
1844 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001845
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001846 return ret;
1847}
1848EXPORT_SYMBOL_GPL(bprintf);
1849
1850#endif /* CONFIG_BINARY_PRINTF */
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852/**
1853 * vsscanf - Unformat a buffer into a list of arguments
1854 * @buf: input buffer
1855 * @fmt: format of buffer
1856 * @args: arguments
1857 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001858int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 const char *str = buf;
1861 char *next;
1862 char digit;
1863 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08001864 u8 qualifier;
1865 u8 base;
1866 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001867 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001869 while (*fmt && *str) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /* skip any white space in format */
1871 /* white space in format matchs any amount of
1872 * white space, including none, in the input.
1873 */
1874 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08001875 fmt = skip_spaces(++fmt);
1876 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878
1879 /* anything that is not a conversion must match exactly */
1880 if (*fmt != '%' && *fmt) {
1881 if (*fmt++ != *str++)
1882 break;
1883 continue;
1884 }
1885
1886 if (!*fmt)
1887 break;
1888 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /* skip this conversion.
1891 * advance both strings to next white space
1892 */
1893 if (*fmt == '*') {
Andy Spencer8fccae22009-10-01 15:44:27 -07001894 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 fmt++;
1896 while (!isspace(*str) && *str)
1897 str++;
1898 continue;
1899 }
1900
1901 /* get field width */
1902 field_width = -1;
1903 if (isdigit(*fmt))
1904 field_width = skip_atoi(&fmt);
1905
1906 /* get conversion qualifier */
1907 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001908 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1909 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 qualifier = *fmt++;
1911 if (unlikely(qualifier == *fmt)) {
1912 if (qualifier == 'h') {
1913 qualifier = 'H';
1914 fmt++;
1915 } else if (qualifier == 'l') {
1916 qualifier = 'L';
1917 fmt++;
1918 }
1919 }
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 if (!*fmt || !*str)
1923 break;
1924
André Goddard Rosad4be1512009-12-14 18:00:59 -08001925 base = 10;
1926 is_sign = 0;
1927
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001928 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 case 'c':
1930 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001931 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (field_width == -1)
1933 field_width = 1;
1934 do {
1935 *s++ = *str++;
1936 } while (--field_width > 0 && *str);
1937 num++;
1938 }
1939 continue;
1940 case 's':
1941 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001942 char *s = (char *)va_arg(args, char *);
1943 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07001944 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08001946 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001949 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 *s = '\0';
1952 num++;
1953 }
1954 continue;
1955 case 'n':
1956 /* return number of characters read so far */
1957 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001958 int *i = (int *)va_arg(args, int*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 *i = str - buf;
1960 }
1961 continue;
1962 case 'o':
1963 base = 8;
1964 break;
1965 case 'x':
1966 case 'X':
1967 base = 16;
1968 break;
1969 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001970 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 case 'd':
1972 is_sign = 1;
1973 case 'u':
1974 break;
1975 case '%':
1976 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001977 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return num;
1979 continue;
1980 default:
1981 /* invalid format; stop here */
1982 return num;
1983 }
1984
1985 /* have some sort of integer conversion.
1986 * first, skip white space in buffer.
1987 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08001988 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 digit = *str;
1991 if (is_sign && digit == '-')
1992 digit = *(str + 1);
1993
1994 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001995 || (base == 16 && !isxdigit(digit))
1996 || (base == 10 && !isdigit(digit))
1997 || (base == 8 && (!isdigit(digit) || digit > '7'))
1998 || (base == 0 && !isdigit(digit)))
1999 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002001 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 case 'H': /* that's 'hh' in format */
2003 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002004 signed char *s = (signed char *)va_arg(args, signed char *);
2005 *s = (signed char)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002007 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
2008 *s = (unsigned char)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010 break;
2011 case 'h':
2012 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002013 short *s = (short *)va_arg(args, short *);
2014 *s = (short)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002016 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
2017 *s = (unsigned short)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
2019 break;
2020 case 'l':
2021 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002022 long *l = (long *)va_arg(args, long *);
2023 *l = simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002025 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
2026 *l = simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028 break;
2029 case 'L':
2030 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002031 long long *l = (long long *)va_arg(args, long long *);
2032 *l = simple_strtoll(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002034 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2035 *l = simple_strtoull(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
2037 break;
2038 case 'Z':
2039 case 'z':
2040 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002041 size_t *s = (size_t *)va_arg(args, size_t *);
2042 *s = (size_t)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044 break;
2045 default:
2046 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002047 int *i = (int *)va_arg(args, int *);
2048 *i = (int)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002050 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2051 *i = (unsigned int)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
2053 break;
2054 }
2055 num++;
2056
2057 if (!next)
2058 break;
2059 str = next;
2060 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002061
2062 /*
2063 * Now we've come all the way through so either the input string or the
2064 * format ended. In the former case, there can be a %n at the current
2065 * position in the format that needs to be filled.
2066 */
2067 if (*fmt == '%' && *(fmt + 1) == 'n') {
2068 int *p = (int *)va_arg(args, int *);
2069 *p = str - buf;
2070 }
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return num;
2073}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074EXPORT_SYMBOL(vsscanf);
2075
2076/**
2077 * sscanf - Unformat a buffer into a list of arguments
2078 * @buf: input buffer
2079 * @fmt: formatting of buffer
2080 * @...: resulting arguments
2081 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002082int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 va_list args;
2085 int i;
2086
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002087 va_start(args, fmt);
2088 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return i;
2092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093EXPORT_SYMBOL(sscanf);