blob: abbabec9720a1947ffa5364ae9aab9ec9696ecfe [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>
Joe Perches8a27f7c2009-08-17 12:29:44 +000028#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Tim Schmielau4e57b682005-10-30 15:03:48 -080030#include <asm/page.h> /* for PAGE_SIZE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/div64.h>
James Bottomleydeac93d2008-09-03 20:43:36 -050032#include <asm/sections.h> /* for dereference_function_descriptor() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070034#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * simple_strtoull - convert a string to an unsigned long long
38 * @cp: The start of the string
39 * @endp: A pointer to the end of the parsed string will be placed here
40 * @base: The number base to use
41 */
Harvey Harrison22d27052008-10-16 13:40:35 -070042unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070044 unsigned long long result;
45 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070047 cp = _parse_integer_fixup_radix(cp, &base);
48 rv = _parse_integer(cp, base, &result);
49 /* FIXME */
50 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (endp)
53 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return result;
56}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057EXPORT_SYMBOL(simple_strtoull);
58
59/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080060 * simple_strtoul - convert a string to an unsigned long
61 * @cp: The start of the string
62 * @endp: A pointer to the end of the parsed string will be placed here
63 * @base: The number base to use
64 */
65unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
66{
67 return simple_strtoull(cp, endp, base);
68}
69EXPORT_SYMBOL(simple_strtoul);
70
71/**
72 * simple_strtol - convert a string to a signed long
73 * @cp: The start of the string
74 * @endp: A pointer to the end of the parsed string will be placed here
75 * @base: The number base to use
76 */
77long simple_strtol(const char *cp, char **endp, unsigned int base)
78{
79 if (*cp == '-')
80 return -simple_strtoul(cp + 1, endp, base);
81
82 return simple_strtoul(cp, endp, base);
83}
84EXPORT_SYMBOL(simple_strtol);
85
86/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * simple_strtoll - convert a string to a signed long long
88 * @cp: The start of the string
89 * @endp: A pointer to the end of the parsed string will be placed here
90 * @base: The number base to use
91 */
Harvey Harrison22d27052008-10-16 13:40:35 -070092long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080094 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -070095 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080096
Harvey Harrison22d27052008-10-16 13:40:35 -070097 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
Hans Verkuil98d5ce02010-04-23 13:18:04 -040099EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Joe Perchescf3b4292010-05-24 14:33:16 -0700101static noinline_for_stack
102int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800104 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 while (isdigit(**s))
107 i = i*10 + *((*s)++) - '0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return i;
110}
111
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700112/* Decimal conversion is by far the most typical, and is used
113 * for /proc and /sys data. This directly impacts e.g. top performance
114 * with many processes running. We optimize it for speed
115 * using code from
116 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
117 * (with permission from the author, Douglas W. Jones). */
118
119/* Formats correctly any integer in [0,99999].
120 * Outputs from one to five digits depending on input.
121 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
Joe Perchescf3b4292010-05-24 14:33:16 -0700122static noinline_for_stack
123char *put_dec_trunc(char *buf, unsigned q)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700124{
125 unsigned d3, d2, d1, d0;
126 d1 = (q>>4) & 0xf;
127 d2 = (q>>8) & 0xf;
128 d3 = (q>>12);
129
130 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
131 q = (d0 * 0xcd) >> 11;
132 d0 = d0 - 10*q;
133 *buf++ = d0 + '0'; /* least significant digit */
134 d1 = q + 9*d3 + 5*d2 + d1;
135 if (d1 != 0) {
136 q = (d1 * 0xcd) >> 11;
137 d1 = d1 - 10*q;
138 *buf++ = d1 + '0'; /* next digit */
139
140 d2 = q + 2*d2;
141 if ((d2 != 0) || (d3 != 0)) {
142 q = (d2 * 0xd) >> 7;
143 d2 = d2 - 10*q;
144 *buf++ = d2 + '0'; /* next digit */
145
146 d3 = q + 4*d3;
147 if (d3 != 0) {
148 q = (d3 * 0xcd) >> 11;
149 d3 = d3 - 10*q;
150 *buf++ = d3 + '0'; /* next digit */
151 if (q != 0)
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800152 *buf++ = q + '0'; /* most sign. digit */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700153 }
154 }
155 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800156
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700157 return buf;
158}
159/* Same with if's removed. Always emits five digits */
Joe Perchescf3b4292010-05-24 14:33:16 -0700160static noinline_for_stack
161char *put_dec_full(char *buf, unsigned q)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700162{
163 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
164 /* but anyway, gcc produces better code with full-sized ints */
165 unsigned d3, d2, d1, d0;
166 d1 = (q>>4) & 0xf;
167 d2 = (q>>8) & 0xf;
168 d3 = (q>>12);
169
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800170 /*
171 * Possible ways to approx. divide by 10
172 * gcc -O2 replaces multiply with shifts and adds
173 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
174 * (x * 0x67) >> 10: 1100111
175 * (x * 0x34) >> 9: 110100 - same
176 * (x * 0x1a) >> 8: 11010 - same
177 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
178 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700179 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
180 q = (d0 * 0xcd) >> 11;
181 d0 = d0 - 10*q;
182 *buf++ = d0 + '0';
183 d1 = q + 9*d3 + 5*d2 + d1;
184 q = (d1 * 0xcd) >> 11;
185 d1 = d1 - 10*q;
186 *buf++ = d1 + '0';
187
188 d2 = q + 2*d2;
189 q = (d2 * 0xd) >> 7;
190 d2 = d2 - 10*q;
191 *buf++ = d2 + '0';
192
193 d3 = q + 4*d3;
194 q = (d3 * 0xcd) >> 11; /* - shorter code */
195 /* q = (d3 * 0x67) >> 10; - would also work */
196 d3 = d3 - 10*q;
197 *buf++ = d3 + '0';
198 *buf++ = q + '0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800199
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700200 return buf;
201}
202/* No inlining helps gcc to use registers better */
Joe Perchescf3b4292010-05-24 14:33:16 -0700203static noinline_for_stack
204char *put_dec(char *buf, unsigned long long num)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700205{
206 while (1) {
207 unsigned rem;
208 if (num < 100000)
209 return put_dec_trunc(buf, num);
210 rem = do_div(num, 100000);
211 buf = put_dec_full(buf, rem);
212 }
213}
214
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700215/*
216 * Convert passed number to decimal string.
217 * Returns the length of string. On buffer overflow, returns 0.
218 *
219 * If speed is not important, use snprintf(). It's easy to read the code.
220 */
221int num_to_str(char *buf, int size, unsigned long long num)
222{
223 char tmp[21]; /* Enough for 2^64 in decimal */
224 int idx, len;
225
226 len = put_dec(tmp, num) - tmp;
227
228 if (len > size)
229 return 0;
230 for (idx = 0; idx < len; ++idx)
231 buf[idx] = tmp[len - idx - 1];
232 return len;
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#define ZEROPAD 1 /* pad with zero */
236#define SIGN 2 /* unsigned/signed long */
237#define PLUS 4 /* show plus */
238#define SPACE 8 /* space if plus */
239#define LEFT 16 /* left justified */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700240#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
241#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100243enum format_type {
244 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100245 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100246 FORMAT_TYPE_PRECISION,
247 FORMAT_TYPE_CHAR,
248 FORMAT_TYPE_STR,
249 FORMAT_TYPE_PTR,
250 FORMAT_TYPE_PERCENT_CHAR,
251 FORMAT_TYPE_INVALID,
252 FORMAT_TYPE_LONG_LONG,
253 FORMAT_TYPE_ULONG,
254 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800255 FORMAT_TYPE_UBYTE,
256 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100257 FORMAT_TYPE_USHORT,
258 FORMAT_TYPE_SHORT,
259 FORMAT_TYPE_UINT,
260 FORMAT_TYPE_INT,
261 FORMAT_TYPE_NRCHARS,
262 FORMAT_TYPE_SIZE_T,
263 FORMAT_TYPE_PTRDIFF
264};
265
266struct printf_spec {
Joe Perches4e310fd2010-04-14 09:27:40 -0700267 u8 type; /* format_type enum */
Joe Perchesef0658f2010-03-06 17:10:14 -0800268 u8 flags; /* flags to number() */
Joe Perches4e310fd2010-04-14 09:27:40 -0700269 u8 base; /* number base, 8, 10 or 16 only */
270 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
271 s16 field_width; /* width of output field */
272 s16 precision; /* # of digits/chars */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100273};
274
Joe Perchescf3b4292010-05-24 14:33:16 -0700275static noinline_for_stack
276char *number(char *buf, char *end, unsigned long long num,
277 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100279 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
280 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
281
282 char tmp[66];
283 char sign;
284 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100285 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 int i;
287
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100288 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
289 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100290 locase = (spec.flags & SMALL);
291 if (spec.flags & LEFT)
292 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100294 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800295 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800297 num = -(signed long long)num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100298 spec.field_width--;
299 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100301 spec.field_width--;
302 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100304 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700307 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100308 spec.field_width--;
309 if (spec.base == 16)
310 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700312
313 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 i = 0;
315 if (num == 0)
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700316 tmp[i++] = '0';
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700317 /* Generic code, for any base:
318 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100319 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700320 } while (num != 0);
321 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100322 else if (spec.base != 10) { /* 8 or 16 */
323 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700324 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800325
326 if (spec.base == 16)
327 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700328 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100329 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700330 num >>= shift;
331 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700332 } else { /* base 10 */
333 i = put_dec(tmp, num) - tmp;
334 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700335
336 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100337 if (i > spec.precision)
338 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700339 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100340 spec.field_width -= spec.precision;
341 if (!(spec.flags & (ZEROPAD+LEFT))) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800342 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700343 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 *buf = ' ';
345 ++buf;
346 }
347 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700348 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700350 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *buf = sign;
352 ++buf;
353 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700354 /* "0x" / "0" prefix */
355 if (need_pfx) {
356 if (buf < end)
357 *buf = '0';
358 ++buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100359 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700360 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100361 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 ++buf;
363 }
364 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700365 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100366 if (!(spec.flags & LEFT)) {
367 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
368 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700369 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 *buf = c;
371 ++buf;
372 }
373 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700374 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100375 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700376 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *buf = '0';
378 ++buf;
379 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700380 /* actual digits of result */
381 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700382 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 *buf = tmp[i];
384 ++buf;
385 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700386 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100387 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700388 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 *buf = ' ';
390 ++buf;
391 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return buf;
394}
395
Joe Perchescf3b4292010-05-24 14:33:16 -0700396static noinline_for_stack
397char *string(char *buf, char *end, const char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700398{
399 int len, i;
400
401 if ((unsigned long)s < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -0800402 s = "(null)";
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700403
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100404 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700405
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100406 if (!(spec.flags & LEFT)) {
407 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700408 if (buf < end)
409 *buf = ' ';
410 ++buf;
411 }
412 }
413 for (i = 0; i < len; ++i) {
414 if (buf < end)
415 *buf = *s;
416 ++buf; ++s;
417 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100418 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700419 if (buf < end)
420 *buf = ' ';
421 ++buf;
422 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800423
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700424 return buf;
425}
426
Joe Perchescf3b4292010-05-24 14:33:16 -0700427static noinline_for_stack
428char *symbol_string(char *buf, char *end, void *ptr,
429 struct printf_spec spec, char ext)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700430{
431 unsigned long value = (unsigned long) ptr;
432#ifdef CONFIG_KALLSYMS
433 char sym[KSYM_SYMBOL_LEN];
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900434 if (ext == 'B')
435 sprint_backtrace(sym, value);
436 else if (ext != 'f' && ext != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200437 sprint_symbol(sym, value);
438 else
439 kallsyms_lookup(value, NULL, NULL, NULL, sym);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800440
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100441 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700442#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800443 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100444 spec.flags |= SPECIAL | SMALL | ZEROPAD;
445 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800446
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100447 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700448#endif
449}
450
Joe Perchescf3b4292010-05-24 14:33:16 -0700451static noinline_for_stack
452char *resource_string(char *buf, char *end, struct resource *res,
453 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100454{
455#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600456#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100457#endif
458
459#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600460#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100461#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700462 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100463 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700464 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100465 .precision = -1,
466 .flags = SPECIAL | SMALL | ZEROPAD,
467 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700468 static const struct printf_spec mem_spec = {
469 .base = 16,
470 .field_width = MEM_RSRC_PRINTK_SIZE,
471 .precision = -1,
472 .flags = SPECIAL | SMALL | ZEROPAD,
473 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700474 static const struct printf_spec bus_spec = {
475 .base = 16,
476 .field_width = 2,
477 .precision = -1,
478 .flags = SMALL | ZEROPAD,
479 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700480 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600481 .base = 10,
482 .precision = -1,
483 .flags = 0,
484 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700485 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600486 .field_width = -1,
487 .precision = 10,
488 .flags = LEFT,
489 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700490 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600491 .base = 16,
492 .precision = -1,
493 .flags = SPECIAL | SMALL,
494 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600495
496 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
497 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
498#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
499#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700500#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600501#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
502 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
503 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
504
Linus Torvalds332d2e72008-10-20 15:07:34 +1100505 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600506 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700507 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100508
509 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700510 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600511 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700512 specp = &io_spec;
513 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600514 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700515 specp = &mem_spec;
516 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600517 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700518 specp = &dec_spec;
519 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600520 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700521 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700522 } else if (res->flags & IORESOURCE_BUS) {
523 p = string(p, pend, "bus ", str_spec);
524 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700525 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600526 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700527 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600528 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600529 }
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700530 p = number(p, pend, res->start, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600531 if (res->start != res->end) {
532 *p++ = '-';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700533 p = number(p, pend, res->end, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600534 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600535 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600536 if (res->flags & IORESOURCE_MEM_64)
537 p = string(p, pend, " 64bit", str_spec);
538 if (res->flags & IORESOURCE_PREFETCH)
539 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700540 if (res->flags & IORESOURCE_WINDOW)
541 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600542 if (res->flags & IORESOURCE_DISABLED)
543 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600544 } else {
545 p = string(p, pend, " flags ", str_spec);
546 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600547 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100548 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600549 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100550
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100551 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100552}
553
Joe Perchescf3b4292010-05-24 14:33:16 -0700554static noinline_for_stack
555char *mac_address_string(char *buf, char *end, u8 *addr,
556 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700557{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000558 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700559 char *p = mac_addr;
560 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000561 char separator;
562
563 if (fmt[1] == 'F') { /* FDDI canonical format */
Joe Perchesbc7259a2010-01-07 11:43:50 +0000564 separator = '-';
565 } else {
Joe Perchesbc7259a2010-01-07 11:43:50 +0000566 separator = ':';
567 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700568
569 for (i = 0; i < 6; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700570 p = hex_byte_pack(p, addr[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000571 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000572 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700573 }
574 *p = '\0';
575
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100576 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700577}
578
Joe Perchescf3b4292010-05-24 14:33:16 -0700579static noinline_for_stack
580char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700581{
Harvey Harrison689afa72008-10-28 16:04:44 -0700582 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800583 bool leading_zeros = (fmt[0] == 'i');
584 int index;
585 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700586
Joe Perches0159f242010-01-13 20:23:30 -0800587 switch (fmt[2]) {
588 case 'h':
589#ifdef __BIG_ENDIAN
590 index = 0;
591 step = 1;
592#else
593 index = 3;
594 step = -1;
595#endif
596 break;
597 case 'l':
598 index = 3;
599 step = -1;
600 break;
601 case 'n':
602 case 'b':
603 default:
604 index = 0;
605 step = 1;
606 break;
607 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000608 for (i = 0; i < 4; i++) {
609 char temp[3]; /* hold each IP quad in reverse order */
Joe Perches0159f242010-01-13 20:23:30 -0800610 int digits = put_dec_trunc(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000611 if (leading_zeros) {
612 if (digits < 3)
613 *p++ = '0';
614 if (digits < 2)
615 *p++ = '0';
616 }
617 /* reverse the digits in the quad */
618 while (digits--)
619 *p++ = temp[digits];
620 if (i < 3)
621 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -0800622 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000623 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000624 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800625
Joe Perches8a27f7c2009-08-17 12:29:44 +0000626 return p;
627}
628
Joe Perchescf3b4292010-05-24 14:33:16 -0700629static noinline_for_stack
630char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000631{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800632 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000633 unsigned char zerolength[8];
634 int longest = 1;
635 int colonpos = -1;
636 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800637 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000638 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000639 bool useIPv4;
640 struct in6_addr in6;
641
642 memcpy(&in6, addr, sizeof(struct in6_addr));
643
644 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000645
646 memset(zerolength, 0, sizeof(zerolength));
647
648 if (useIPv4)
649 range = 6;
650 else
651 range = 8;
652
653 /* find position of longest 0 run */
654 for (i = 0; i < range; i++) {
655 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000656 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000657 break;
658 zerolength[i]++;
659 }
660 }
661 for (i = 0; i < range; i++) {
662 if (zerolength[i] > longest) {
663 longest = zerolength[i];
664 colonpos = i;
665 }
666 }
Joe Perches29cf5192011-06-09 11:23:37 -0700667 if (longest == 1) /* don't compress a single 0 */
668 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000669
670 /* emit address */
671 for (i = 0; i < range; i++) {
672 if (i == colonpos) {
673 if (needcolon || i == 0)
674 *p++ = ':';
675 *p++ = ':';
676 needcolon = false;
677 i += longest - 1;
678 continue;
679 }
680 if (needcolon) {
681 *p++ = ':';
682 needcolon = false;
683 }
684 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +0000685 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000686 hi = word >> 8;
687 lo = word & 0xff;
688 if (hi) {
689 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700690 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000691 else
692 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700693 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000694 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -0800695 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700696 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000697 else
698 *p++ = hex_asc_lo(lo);
699 needcolon = true;
700 }
701
702 if (useIPv4) {
703 if (needcolon)
704 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -0800705 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +0000706 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000707 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800708
Joe Perches8a27f7c2009-08-17 12:29:44 +0000709 return p;
710}
711
Joe Perchescf3b4292010-05-24 14:33:16 -0700712static noinline_for_stack
713char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000714{
715 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800716
Harvey Harrison689afa72008-10-28 16:04:44 -0700717 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700718 p = hex_byte_pack(p, *addr++);
719 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000720 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700721 *p++ = ':';
722 }
723 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800724
Joe Perches8a27f7c2009-08-17 12:29:44 +0000725 return p;
726}
727
Joe Perchescf3b4292010-05-24 14:33:16 -0700728static noinline_for_stack
729char *ip6_addr_string(char *buf, char *end, const u8 *addr,
730 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000731{
732 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
733
734 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +0000735 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000736 else
Joe Percheseb78cd22009-09-18 13:04:06 +0000737 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -0700738
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100739 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700740}
741
Joe Perchescf3b4292010-05-24 14:33:16 -0700742static noinline_for_stack
743char *ip4_addr_string(char *buf, char *end, const u8 *addr,
744 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700745{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000746 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700747
Joe Perches0159f242010-01-13 20:23:30 -0800748 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700749
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100750 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700751}
752
Joe Perchescf3b4292010-05-24 14:33:16 -0700753static noinline_for_stack
754char *uuid_string(char *buf, char *end, const u8 *addr,
755 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -0800756{
757 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
758 char *p = uuid;
759 int i;
760 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
761 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
762 const u8 *index = be;
763 bool uc = false;
764
765 switch (*(++fmt)) {
766 case 'L':
767 uc = true; /* fall-through */
768 case 'l':
769 index = le;
770 break;
771 case 'B':
772 uc = true;
773 break;
774 }
775
776 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700777 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -0800778 switch (i) {
779 case 3:
780 case 5:
781 case 7:
782 case 9:
783 *p++ = '-';
784 break;
785 }
786 }
787
788 *p = 0;
789
790 if (uc) {
791 p = uuid;
792 do {
793 *p = toupper(*p);
794 } while (*(++p));
795 }
796
797 return string(buf, end, uuid, spec);
798}
799
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000800static
801char *netdev_feature_string(char *buf, char *end, const u8 *addr,
802 struct printf_spec spec)
803{
804 spec.flags |= SPECIAL | SMALL | ZEROPAD;
805 if (spec.field_width == -1)
806 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
807 spec.base = 16;
808
809 return number(buf, end, *(const netdev_features_t *)addr, spec);
810}
811
Ingo Molnar411f05f2011-05-12 23:00:28 +0200812int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800813
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700814/*
815 * Show a '%p' thing. A kernel extension is that the '%p' is followed
816 * by an extra set of alphanumeric characters that are extended format
817 * specifiers.
818 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100819 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700820 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200821 * - 'F' For symbolic function descriptor pointers with offset
822 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -0400823 * - 'S' For symbolic direct pointers with offset
824 * - 's' For symbolic direct pointers without offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900825 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600826 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
827 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700828 * - 'M' For a 6-byte MAC address, it prints the address in the
829 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000830 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +0000831 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -0800832 * with a dash-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000833 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
834 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
835 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
836 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
837 * IPv6 omits the colons (01020304...0f)
838 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Joe Perches0159f242010-01-13 20:23:30 -0800839 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
Joe Perches8a27f7c2009-08-17 12:29:44 +0000840 * - 'I6c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -0700841 * http://tools.ietf.org/html/rfc5952
Joe Perches9ac6e442009-12-14 18:01:09 -0800842 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
843 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
844 * Options for %pU are:
845 * b big endian lower case hex (default)
846 * B big endian UPPER case hex
847 * l little endian lower case hex
848 * L little endian UPPER case hex
849 * big endian output byte order is:
850 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
851 * little endian output byte order is:
852 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +0000853 * - 'V' For a struct va_format which contains a format string * and va_list *,
854 * call vsnprintf(->format, *->va_list).
855 * Implements a "recursive vsnprintf".
856 * Do not use this feature without some mechanism to verify the
857 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800858 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000859 * - 'NF' For a netdev_features_t
Joe Perches9ac6e442009-12-14 18:01:09 -0800860 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100861 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
862 * function pointers are really function descriptors, which contain a
863 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700864 */
Joe Perchescf3b4292010-05-24 14:33:16 -0700865static noinline_for_stack
866char *pointer(const char *fmt, char *buf, char *end, void *ptr,
867 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700868{
Kees Cook9f36e2c2011-03-22 16:34:22 -0700869 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -0700870 /*
871 * Print (null) with the same width as a pointer so it makes
872 * tabular output look nice.
873 */
874 if (spec.field_width == -1)
875 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100876 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -0700877 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800878
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700879 switch (*fmt) {
880 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200881 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700882 ptr = dereference_function_descriptor(ptr);
883 /* Fallthrough */
884 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -0800885 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900886 case 'B':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200887 return symbol_string(buf, end, ptr, spec, *fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100888 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600889 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600890 return resource_string(buf, end, ptr, spec, fmt);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000891 case 'M': /* Colon separated: 00:01:02:03:04:05 */
892 case 'm': /* Contiguous: 000102030405 */
Joe Perchesbc7259a2010-01-07 11:43:50 +0000893 /* [mM]F (FDDI, bit reversed) */
Joe Perches8a27f7c2009-08-17 12:29:44 +0000894 return mac_address_string(buf, end, ptr, spec, fmt);
895 case 'I': /* Formatted IP supported
896 * 4: 1.2.3.4
897 * 6: 0001:0203:...:0708
898 * 6c: 1::708 or 1::1.2.3.4
899 */
900 case 'i': /* Contiguous:
901 * 4: 001.002.003.004
902 * 6: 000102...0f
903 */
904 switch (fmt[1]) {
905 case '6':
906 return ip6_addr_string(buf, end, ptr, spec, fmt);
907 case '4':
908 return ip4_addr_string(buf, end, ptr, spec, fmt);
909 }
Harvey Harrison4aa99602008-10-29 12:49:58 -0700910 break;
Joe Perches9ac6e442009-12-14 18:01:09 -0800911 case 'U':
912 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +0000913 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +0000914 {
915 va_list va;
916
917 va_copy(va, *((struct va_format *)ptr)->va);
918 buf += vsnprintf(buf, end > buf ? end - buf : 0,
919 ((struct va_format *)ptr)->fmt, va);
920 va_end(va);
921 return buf;
922 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800923 case 'K':
924 /*
925 * %pK cannot be used in IRQ context because its test
926 * for CAP_SYSLOG would be meaningless.
927 */
928 if (in_irq() || in_serving_softirq() || in_nmi()) {
929 if (spec.field_width == -1)
930 spec.field_width = 2 * sizeof(void *);
931 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800932 }
Joe Perches26297602011-03-22 16:34:19 -0700933 if (!((kptr_restrict == 0) ||
934 (kptr_restrict == 1 &&
935 has_capability_noaudit(current, CAP_SYSLOG))))
936 ptr = NULL;
937 break;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000938 case 'N':
939 switch (fmt[1]) {
940 case 'F':
941 return netdev_feature_string(buf, end, ptr, spec);
942 }
943 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700944 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100945 spec.flags |= SMALL;
946 if (spec.field_width == -1) {
Joe Perches5e057982010-10-26 14:22:50 -0700947 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100948 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700949 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100950 spec.base = 16;
951
952 return number(buf, end, (unsigned long) ptr, spec);
953}
954
955/*
956 * Helper function to decode printf style format.
957 * Each call decode a token from the format and return the
958 * number of characters read (or likely the delta where it wants
959 * to go on the next call).
960 * The decoded token is returned through the parameters
961 *
962 * 'h', 'l', or 'L' for integer fields
963 * 'z' support added 23/7/1999 S.H.
964 * 'z' changed to 'Z' --davidm 1/25/99
965 * 't' added for ptrdiff_t
966 *
967 * @fmt: the format string
968 * @type of the token returned
969 * @flags: various flags such as +, -, # tokens..
970 * @field_width: overwritten width
971 * @base: base of the number (octal, hex, ...)
972 * @precision: precision of a number
973 * @qualifier: qualifier of a number (long, size_t, ...)
974 */
Joe Perchescf3b4292010-05-24 14:33:16 -0700975static noinline_for_stack
976int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100977{
978 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100979
980 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +0100981 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100982 if (spec->field_width < 0) {
983 spec->field_width = -spec->field_width;
984 spec->flags |= LEFT;
985 }
986 spec->type = FORMAT_TYPE_NONE;
987 goto precision;
988 }
989
990 /* we finished early by reading the precision */
991 if (spec->type == FORMAT_TYPE_PRECISION) {
992 if (spec->precision < 0)
993 spec->precision = 0;
994
995 spec->type = FORMAT_TYPE_NONE;
996 goto qualifier;
997 }
998
999 /* By default */
1000 spec->type = FORMAT_TYPE_NONE;
1001
1002 for (; *fmt ; ++fmt) {
1003 if (*fmt == '%')
1004 break;
1005 }
1006
1007 /* Return the current non-format string */
1008 if (fmt != start || !*fmt)
1009 return fmt - start;
1010
1011 /* Process flags */
1012 spec->flags = 0;
1013
1014 while (1) { /* this also skips first '%' */
1015 bool found = true;
1016
1017 ++fmt;
1018
1019 switch (*fmt) {
1020 case '-': spec->flags |= LEFT; break;
1021 case '+': spec->flags |= PLUS; break;
1022 case ' ': spec->flags |= SPACE; break;
1023 case '#': spec->flags |= SPECIAL; break;
1024 case '0': spec->flags |= ZEROPAD; break;
1025 default: found = false;
1026 }
1027
1028 if (!found)
1029 break;
1030 }
1031
1032 /* get field width */
1033 spec->field_width = -1;
1034
1035 if (isdigit(*fmt))
1036 spec->field_width = skip_atoi(&fmt);
1037 else if (*fmt == '*') {
1038 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001039 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001040 return ++fmt - start;
1041 }
1042
1043precision:
1044 /* get the precision */
1045 spec->precision = -1;
1046 if (*fmt == '.') {
1047 ++fmt;
1048 if (isdigit(*fmt)) {
1049 spec->precision = skip_atoi(&fmt);
1050 if (spec->precision < 0)
1051 spec->precision = 0;
1052 } else if (*fmt == '*') {
1053 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001054 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001055 return ++fmt - start;
1056 }
1057 }
1058
1059qualifier:
1060 /* get the conversion qualifier */
1061 spec->qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001062 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1063 _tolower(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001064 spec->qualifier = *fmt++;
1065 if (unlikely(spec->qualifier == *fmt)) {
1066 if (spec->qualifier == 'l') {
1067 spec->qualifier = 'L';
1068 ++fmt;
1069 } else if (spec->qualifier == 'h') {
1070 spec->qualifier = 'H';
1071 ++fmt;
1072 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001073 }
1074 }
1075
1076 /* default base */
1077 spec->base = 10;
1078 switch (*fmt) {
1079 case 'c':
1080 spec->type = FORMAT_TYPE_CHAR;
1081 return ++fmt - start;
1082
1083 case 's':
1084 spec->type = FORMAT_TYPE_STR;
1085 return ++fmt - start;
1086
1087 case 'p':
1088 spec->type = FORMAT_TYPE_PTR;
1089 return fmt - start;
1090 /* skip alnum */
1091
1092 case 'n':
1093 spec->type = FORMAT_TYPE_NRCHARS;
1094 return ++fmt - start;
1095
1096 case '%':
1097 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1098 return ++fmt - start;
1099
1100 /* integer number formats - set up the flags and "break" */
1101 case 'o':
1102 spec->base = 8;
1103 break;
1104
1105 case 'x':
1106 spec->flags |= SMALL;
1107
1108 case 'X':
1109 spec->base = 16;
1110 break;
1111
1112 case 'd':
1113 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001114 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001115 case 'u':
1116 break;
1117
1118 default:
1119 spec->type = FORMAT_TYPE_INVALID;
1120 return fmt - start;
1121 }
1122
1123 if (spec->qualifier == 'L')
1124 spec->type = FORMAT_TYPE_LONG_LONG;
1125 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001126 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001127 spec->type = FORMAT_TYPE_LONG;
1128 else
1129 spec->type = FORMAT_TYPE_ULONG;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001130 } else if (_tolower(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001131 spec->type = FORMAT_TYPE_SIZE_T;
1132 } else if (spec->qualifier == 't') {
1133 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001134 } else if (spec->qualifier == 'H') {
1135 if (spec->flags & SIGN)
1136 spec->type = FORMAT_TYPE_BYTE;
1137 else
1138 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001139 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001140 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001141 spec->type = FORMAT_TYPE_SHORT;
1142 else
1143 spec->type = FORMAT_TYPE_USHORT;
1144 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001145 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001146 spec->type = FORMAT_TYPE_INT;
1147 else
1148 spec->type = FORMAT_TYPE_UINT;
1149 }
1150
1151 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001152}
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/**
1155 * vsnprintf - Format a string and place it in a buffer
1156 * @buf: The buffer to place the result into
1157 * @size: The size of the buffer, including the trailing null space
1158 * @fmt: The format string to use
1159 * @args: Arguments for the format string
1160 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001161 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001162 * %pS output the name of a text symbol with offset
1163 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001164 * %pF output the name of a function pointer with its offset
1165 * %pf output the name of a function pointer without its offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001166 * %pB output the name of a backtrace symbol with its offset
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001167 * %pR output the address range in a struct resource with decoded flags
1168 * %pr output the address range in a struct resource with raw flags
1169 * %pM output a 6-byte MAC address with colons
1170 * %pm output a 6-byte MAC address without colons
1171 * %pI4 print an IPv4 address without leading zeros
1172 * %pi4 print an IPv4 address with leading zeros
1173 * %pI6 print an IPv6 address with colons
1174 * %pi6 print an IPv6 address without colons
Jan Engelhardtf996f202011-07-14 18:48:56 +02001175 * %pI6c print an IPv6 address as specified by RFC 5952
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001176 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1177 * case.
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001178 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001179 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 * The return value is the number of characters which would
1181 * be generated for the given input, excluding the trailing
1182 * '\0', as per ISO C99. If you want to have the exact
1183 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001184 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 * return is greater than or equal to @size, the resulting
1186 * string is truncated.
1187 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001188 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
1190int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001193 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001194 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001196 /* Reject out-of-range values early. Large positive sizes are
1197 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001198 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001202 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001204 /* Make sure end is always >= buf */
1205 if (end < buf) {
1206 end = ((void *)-1);
1207 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001210 while (*fmt) {
1211 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001212 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001213
1214 fmt += read;
1215
1216 switch (spec.type) {
1217 case FORMAT_TYPE_NONE: {
1218 int copy = read;
1219 if (str < end) {
1220 if (copy > end - str)
1221 copy = end - str;
1222 memcpy(str, old_fmt, copy);
1223 }
1224 str += read;
1225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
Vegard Nossumed681a92009-03-14 12:08:50 +01001228 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001229 spec.field_width = va_arg(args, int);
1230 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001232 case FORMAT_TYPE_PRECISION:
1233 spec.precision = va_arg(args, int);
1234 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
André Goddard Rosad4be1512009-12-14 18:00:59 -08001236 case FORMAT_TYPE_CHAR: {
1237 char c;
1238
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001239 if (!(spec.flags & LEFT)) {
1240 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001241 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 *str = ' ';
1243 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001246 }
1247 c = (unsigned char) va_arg(args, int);
1248 if (str < end)
1249 *str = c;
1250 ++str;
1251 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001252 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001253 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001255 }
1256 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001259 case FORMAT_TYPE_STR:
1260 str = string(str, end, va_arg(args, char *), spec);
1261 break;
1262
1263 case FORMAT_TYPE_PTR:
1264 str = pointer(fmt+1, str, end, va_arg(args, void *),
1265 spec);
1266 while (isalnum(*fmt))
1267 fmt++;
1268 break;
1269
1270 case FORMAT_TYPE_PERCENT_CHAR:
1271 if (str < end)
1272 *str = '%';
1273 ++str;
1274 break;
1275
1276 case FORMAT_TYPE_INVALID:
1277 if (str < end)
1278 *str = '%';
1279 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001280 break;
1281
1282 case FORMAT_TYPE_NRCHARS: {
Joe Perchesef0658f2010-03-06 17:10:14 -08001283 u8 qualifier = spec.qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001284
1285 if (qualifier == 'l') {
1286 long *ip = va_arg(args, long *);
1287 *ip = (str - buf);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001288 } else if (_tolower(qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001289 size_t *ip = va_arg(args, size_t *);
1290 *ip = (str - buf);
1291 } else {
1292 int *ip = va_arg(args, int *);
1293 *ip = (str - buf);
1294 }
1295 break;
1296 }
1297
1298 default:
1299 switch (spec.type) {
1300 case FORMAT_TYPE_LONG_LONG:
1301 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001303 case FORMAT_TYPE_ULONG:
1304 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001306 case FORMAT_TYPE_LONG:
1307 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001309 case FORMAT_TYPE_SIZE_T:
1310 num = va_arg(args, size_t);
1311 break;
1312 case FORMAT_TYPE_PTRDIFF:
1313 num = va_arg(args, ptrdiff_t);
1314 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001315 case FORMAT_TYPE_UBYTE:
1316 num = (unsigned char) va_arg(args, int);
1317 break;
1318 case FORMAT_TYPE_BYTE:
1319 num = (signed char) va_arg(args, int);
1320 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001321 case FORMAT_TYPE_USHORT:
1322 num = (unsigned short) va_arg(args, int);
1323 break;
1324 case FORMAT_TYPE_SHORT:
1325 num = (short) va_arg(args, int);
1326 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001327 case FORMAT_TYPE_INT:
1328 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001331 num = va_arg(args, unsigned int);
1332 }
1333
1334 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001337
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001338 if (size > 0) {
1339 if (str < end)
1340 *str = '\0';
1341 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001342 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001343 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001344
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001345 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349EXPORT_SYMBOL(vsnprintf);
1350
1351/**
1352 * vscnprintf - Format a string and place it in a buffer
1353 * @buf: The buffer to place the result into
1354 * @size: The size of the buffer, including the trailing null space
1355 * @fmt: The format string to use
1356 * @args: Arguments for the format string
1357 *
1358 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08001359 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 * returns 0.
1361 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001362 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001363 *
1364 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 */
1366int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1367{
1368 int i;
1369
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001370 i = vsnprintf(buf, size, fmt, args);
1371
Anton Arapovb921c692011-01-12 16:59:49 -08001372 if (likely(i < size))
1373 return i;
1374 if (size != 0)
1375 return size - 1;
1376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378EXPORT_SYMBOL(vscnprintf);
1379
1380/**
1381 * snprintf - Format a string and place it in a buffer
1382 * @buf: The buffer to place the result into
1383 * @size: The size of the buffer, including the trailing null space
1384 * @fmt: The format string to use
1385 * @...: Arguments for the format string
1386 *
1387 * The return value is the number of characters which would be
1388 * generated for the given input, excluding the trailing null,
1389 * as per ISO C99. If the return is greater than or equal to
1390 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001391 *
1392 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001394int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 va_list args;
1397 int i;
1398
1399 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001400 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return i;
1404}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405EXPORT_SYMBOL(snprintf);
1406
1407/**
1408 * scnprintf - 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 written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07001415 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 */
1417
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001418int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 va_list args;
1421 int i;
1422
1423 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08001424 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001426
Anton Arapovb921c692011-01-12 16:59:49 -08001427 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429EXPORT_SYMBOL(scnprintf);
1430
1431/**
1432 * vsprintf - Format a string and place it in a buffer
1433 * @buf: The buffer to place the result into
1434 * @fmt: The format string to use
1435 * @args: Arguments for the format string
1436 *
1437 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001438 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 * buffer overflows.
1440 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001441 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001442 *
1443 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 */
1445int vsprintf(char *buf, const char *fmt, va_list args)
1446{
1447 return vsnprintf(buf, INT_MAX, fmt, args);
1448}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449EXPORT_SYMBOL(vsprintf);
1450
1451/**
1452 * sprintf - Format a string and place it in a buffer
1453 * @buf: The buffer to place the result into
1454 * @fmt: The format string to use
1455 * @...: Arguments for the format string
1456 *
1457 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001458 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001460 *
1461 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001463int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 va_list args;
1466 int i;
1467
1468 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001469 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return i;
1473}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474EXPORT_SYMBOL(sprintf);
1475
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001476#ifdef CONFIG_BINARY_PRINTF
1477/*
1478 * bprintf service:
1479 * vbin_printf() - VA arguments to binary data
1480 * bstr_printf() - Binary data to text string
1481 */
1482
1483/**
1484 * vbin_printf - Parse a format string and place args' binary value in a buffer
1485 * @bin_buf: The buffer to place args' binary value
1486 * @size: The size of the buffer(by words(32bits), not characters)
1487 * @fmt: The format string to use
1488 * @args: Arguments for the format string
1489 *
1490 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1491 * is skiped.
1492 *
1493 * The return value is the number of words(32bits) which would be generated for
1494 * the given input.
1495 *
1496 * NOTE:
1497 * If the return value is greater than @size, the resulting bin_buf is NOT
1498 * valid for bstr_printf().
1499 */
1500int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1501{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001502 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001503 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001504
1505 str = (char *)bin_buf;
1506 end = (char *)(bin_buf + size);
1507
1508#define save_arg(type) \
1509do { \
1510 if (sizeof(type) == 8) { \
1511 unsigned long long value; \
1512 str = PTR_ALIGN(str, sizeof(u32)); \
1513 value = va_arg(args, unsigned long long); \
1514 if (str + sizeof(type) <= end) { \
1515 *(u32 *)str = *(u32 *)&value; \
1516 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1517 } \
1518 } else { \
1519 unsigned long value; \
1520 str = PTR_ALIGN(str, sizeof(type)); \
1521 value = va_arg(args, int); \
1522 if (str + sizeof(type) <= end) \
1523 *(typeof(type) *)str = (type)value; \
1524 } \
1525 str += sizeof(type); \
1526} while (0)
1527
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001528 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08001529 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001530
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001531 fmt += read;
1532
1533 switch (spec.type) {
1534 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08001535 case FORMAT_TYPE_INVALID:
1536 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001537 break;
1538
Vegard Nossumed681a92009-03-14 12:08:50 +01001539 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001540 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001541 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001542 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001543
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001544 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001545 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001546 break;
1547
1548 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001549 const char *save_str = va_arg(args, char *);
1550 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08001551
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001552 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1553 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08001554 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08001555 len = strlen(save_str) + 1;
1556 if (str + len < end)
1557 memcpy(str, save_str, len);
1558 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001559 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001560 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001561
1562 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001563 save_arg(void *);
1564 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001565 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001566 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001567 break;
1568
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001569 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001570 /* skip %n 's argument */
Joe Perchesef0658f2010-03-06 17:10:14 -08001571 u8 qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001572 void *skip_arg;
1573 if (qualifier == 'l')
1574 skip_arg = va_arg(args, long *);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001575 else if (_tolower(qualifier) == 'z')
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001576 skip_arg = va_arg(args, size_t *);
1577 else
1578 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001579 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001580 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001581
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001582 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001583 switch (spec.type) {
1584
1585 case FORMAT_TYPE_LONG_LONG:
1586 save_arg(long long);
1587 break;
1588 case FORMAT_TYPE_ULONG:
1589 case FORMAT_TYPE_LONG:
1590 save_arg(unsigned long);
1591 break;
1592 case FORMAT_TYPE_SIZE_T:
1593 save_arg(size_t);
1594 break;
1595 case FORMAT_TYPE_PTRDIFF:
1596 save_arg(ptrdiff_t);
1597 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001598 case FORMAT_TYPE_UBYTE:
1599 case FORMAT_TYPE_BYTE:
1600 save_arg(char);
1601 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001602 case FORMAT_TYPE_USHORT:
1603 case FORMAT_TYPE_SHORT:
1604 save_arg(short);
1605 break;
1606 default:
1607 save_arg(int);
1608 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001609 }
1610 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001611
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001612 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001613#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001614}
1615EXPORT_SYMBOL_GPL(vbin_printf);
1616
1617/**
1618 * bstr_printf - Format a string from binary arguments and place it in a buffer
1619 * @buf: The buffer to place the result into
1620 * @size: The size of the buffer, including the trailing null space
1621 * @fmt: The format string to use
1622 * @bin_buf: Binary arguments for the format string
1623 *
1624 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1625 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1626 * a binary buffer that generated by vbin_printf.
1627 *
1628 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001629 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001630 *
1631 * The return value is the number of characters which would
1632 * be generated for the given input, excluding the trailing
1633 * '\0', as per ISO C99. If you want to have the exact
1634 * number of characters written into @buf as return value
1635 * (not including the trailing '\0'), use vscnprintf(). If the
1636 * return is greater than or equal to @size, the resulting
1637 * string is truncated.
1638 */
1639int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1640{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001641 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08001642 char *str, *end;
1643 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001644
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001645 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001646 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001647
1648 str = buf;
1649 end = buf + size;
1650
1651#define get_arg(type) \
1652({ \
1653 typeof(type) value; \
1654 if (sizeof(type) == 8) { \
1655 args = PTR_ALIGN(args, sizeof(u32)); \
1656 *(u32 *)&value = *(u32 *)args; \
1657 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1658 } else { \
1659 args = PTR_ALIGN(args, sizeof(type)); \
1660 value = *(typeof(type) *)args; \
1661 } \
1662 args += sizeof(type); \
1663 value; \
1664})
1665
1666 /* Make sure end is always >= buf */
1667 if (end < buf) {
1668 end = ((void *)-1);
1669 size = end - buf;
1670 }
1671
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001672 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001673 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001674 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001675
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001676 fmt += read;
1677
1678 switch (spec.type) {
1679 case FORMAT_TYPE_NONE: {
1680 int copy = read;
1681 if (str < end) {
1682 if (copy > end - str)
1683 copy = end - str;
1684 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001685 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001686 str += read;
1687 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001688 }
1689
Vegard Nossumed681a92009-03-14 12:08:50 +01001690 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001691 spec.field_width = get_arg(int);
1692 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001693
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001694 case FORMAT_TYPE_PRECISION:
1695 spec.precision = get_arg(int);
1696 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001697
André Goddard Rosad4be1512009-12-14 18:00:59 -08001698 case FORMAT_TYPE_CHAR: {
1699 char c;
1700
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001701 if (!(spec.flags & LEFT)) {
1702 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001703 if (str < end)
1704 *str = ' ';
1705 ++str;
1706 }
1707 }
1708 c = (unsigned char) get_arg(char);
1709 if (str < end)
1710 *str = c;
1711 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001712 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001713 if (str < end)
1714 *str = ' ';
1715 ++str;
1716 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001717 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001718 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001719
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001720 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001721 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001722 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001723 str = string(str, end, (char *)str_arg, spec);
1724 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001725 }
1726
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001727 case FORMAT_TYPE_PTR:
1728 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1729 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001730 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001731 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001732
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001733 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001734 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001735 if (str < end)
1736 *str = '%';
1737 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001738 break;
1739
1740 case FORMAT_TYPE_NRCHARS:
1741 /* skip */
1742 break;
1743
André Goddard Rosad4be1512009-12-14 18:00:59 -08001744 default: {
1745 unsigned long long num;
1746
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001747 switch (spec.type) {
1748
1749 case FORMAT_TYPE_LONG_LONG:
1750 num = get_arg(long long);
1751 break;
1752 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001753 case FORMAT_TYPE_LONG:
1754 num = get_arg(unsigned long);
1755 break;
1756 case FORMAT_TYPE_SIZE_T:
1757 num = get_arg(size_t);
1758 break;
1759 case FORMAT_TYPE_PTRDIFF:
1760 num = get_arg(ptrdiff_t);
1761 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001762 case FORMAT_TYPE_UBYTE:
1763 num = get_arg(unsigned char);
1764 break;
1765 case FORMAT_TYPE_BYTE:
1766 num = get_arg(signed char);
1767 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001768 case FORMAT_TYPE_USHORT:
1769 num = get_arg(unsigned short);
1770 break;
1771 case FORMAT_TYPE_SHORT:
1772 num = get_arg(short);
1773 break;
1774 case FORMAT_TYPE_UINT:
1775 num = get_arg(unsigned int);
1776 break;
1777 default:
1778 num = get_arg(int);
1779 }
1780
1781 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08001782 } /* default: */
1783 } /* switch(spec.type) */
1784 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001785
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001786 if (size > 0) {
1787 if (str < end)
1788 *str = '\0';
1789 else
1790 end[-1] = '\0';
1791 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001792
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001793#undef get_arg
1794
1795 /* the trailing null byte doesn't count towards the total */
1796 return str - buf;
1797}
1798EXPORT_SYMBOL_GPL(bstr_printf);
1799
1800/**
1801 * bprintf - Parse a format string and place args' binary value in a buffer
1802 * @bin_buf: The buffer to place args' binary value
1803 * @size: The size of the buffer(by words(32bits), not characters)
1804 * @fmt: The format string to use
1805 * @...: Arguments for the format string
1806 *
1807 * The function returns the number of words(u32) written
1808 * into @bin_buf.
1809 */
1810int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1811{
1812 va_list args;
1813 int ret;
1814
1815 va_start(args, fmt);
1816 ret = vbin_printf(bin_buf, size, fmt, args);
1817 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001818
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001819 return ret;
1820}
1821EXPORT_SYMBOL_GPL(bprintf);
1822
1823#endif /* CONFIG_BINARY_PRINTF */
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825/**
1826 * vsscanf - Unformat a buffer into a list of arguments
1827 * @buf: input buffer
1828 * @fmt: format of buffer
1829 * @args: arguments
1830 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001831int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
1833 const char *str = buf;
1834 char *next;
1835 char digit;
1836 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08001837 u8 qualifier;
1838 u8 base;
1839 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001840 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001842 while (*fmt && *str) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /* skip any white space in format */
1844 /* white space in format matchs any amount of
1845 * white space, including none, in the input.
1846 */
1847 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08001848 fmt = skip_spaces(++fmt);
1849 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851
1852 /* anything that is not a conversion must match exactly */
1853 if (*fmt != '%' && *fmt) {
1854 if (*fmt++ != *str++)
1855 break;
1856 continue;
1857 }
1858
1859 if (!*fmt)
1860 break;
1861 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /* skip this conversion.
1864 * advance both strings to next white space
1865 */
1866 if (*fmt == '*') {
Andy Spencer8fccae22009-10-01 15:44:27 -07001867 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 fmt++;
1869 while (!isspace(*str) && *str)
1870 str++;
1871 continue;
1872 }
1873
1874 /* get field width */
1875 field_width = -1;
1876 if (isdigit(*fmt))
1877 field_width = skip_atoi(&fmt);
1878
1879 /* get conversion qualifier */
1880 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001881 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1882 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 qualifier = *fmt++;
1884 if (unlikely(qualifier == *fmt)) {
1885 if (qualifier == 'h') {
1886 qualifier = 'H';
1887 fmt++;
1888 } else if (qualifier == 'l') {
1889 qualifier = 'L';
1890 fmt++;
1891 }
1892 }
1893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 if (!*fmt || !*str)
1896 break;
1897
André Goddard Rosad4be1512009-12-14 18:00:59 -08001898 base = 10;
1899 is_sign = 0;
1900
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001901 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 case 'c':
1903 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001904 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (field_width == -1)
1906 field_width = 1;
1907 do {
1908 *s++ = *str++;
1909 } while (--field_width > 0 && *str);
1910 num++;
1911 }
1912 continue;
1913 case 's':
1914 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001915 char *s = (char *)va_arg(args, char *);
1916 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07001917 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08001919 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001922 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 *s = '\0';
1925 num++;
1926 }
1927 continue;
1928 case 'n':
1929 /* return number of characters read so far */
1930 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001931 int *i = (int *)va_arg(args, int*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 *i = str - buf;
1933 }
1934 continue;
1935 case 'o':
1936 base = 8;
1937 break;
1938 case 'x':
1939 case 'X':
1940 base = 16;
1941 break;
1942 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001943 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 case 'd':
1945 is_sign = 1;
1946 case 'u':
1947 break;
1948 case '%':
1949 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001950 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return num;
1952 continue;
1953 default:
1954 /* invalid format; stop here */
1955 return num;
1956 }
1957
1958 /* have some sort of integer conversion.
1959 * first, skip white space in buffer.
1960 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08001961 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 digit = *str;
1964 if (is_sign && digit == '-')
1965 digit = *(str + 1);
1966
1967 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001968 || (base == 16 && !isxdigit(digit))
1969 || (base == 10 && !isdigit(digit))
1970 || (base == 8 && (!isdigit(digit) || digit > '7'))
1971 || (base == 0 && !isdigit(digit)))
1972 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001974 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 case 'H': /* that's 'hh' in format */
1976 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001977 signed char *s = (signed char *)va_arg(args, signed char *);
1978 *s = (signed char)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001980 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
1981 *s = (unsigned char)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983 break;
1984 case 'h':
1985 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001986 short *s = (short *)va_arg(args, short *);
1987 *s = (short)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001989 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
1990 *s = (unsigned short)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
1992 break;
1993 case 'l':
1994 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001995 long *l = (long *)va_arg(args, long *);
1996 *l = simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001998 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
1999 *l = simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001 break;
2002 case 'L':
2003 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002004 long long *l = (long long *)va_arg(args, long long *);
2005 *l = simple_strtoll(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002007 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2008 *l = simple_strtoull(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010 break;
2011 case 'Z':
2012 case 'z':
2013 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002014 size_t *s = (size_t *)va_arg(args, size_t *);
2015 *s = (size_t)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017 break;
2018 default:
2019 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002020 int *i = (int *)va_arg(args, int *);
2021 *i = (int)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002023 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2024 *i = (unsigned int)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026 break;
2027 }
2028 num++;
2029
2030 if (!next)
2031 break;
2032 str = next;
2033 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002034
2035 /*
2036 * Now we've come all the way through so either the input string or the
2037 * format ended. In the former case, there can be a %n at the current
2038 * position in the format that needs to be filled.
2039 */
2040 if (*fmt == '%' && *(fmt + 1) == 'n') {
2041 int *p = (int *)va_arg(args, int *);
2042 *p = str - buf;
2043 }
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return num;
2046}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047EXPORT_SYMBOL(vsscanf);
2048
2049/**
2050 * sscanf - Unformat a buffer into a list of arguments
2051 * @buf: input buffer
2052 * @fmt: formatting of buffer
2053 * @...: resulting arguments
2054 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002055int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 va_list args;
2058 int i;
2059
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002060 va_start(args, fmt);
2061 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return i;
2065}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066EXPORT_SYMBOL(sscanf);