blob: 736974576e2d685a2bd68d08b7658bbcee5fd7f8 [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
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700115 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
116 * (with permission from the author, Douglas W. Jones).
117 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700118
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700119#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
120/* Formats correctly any integer in [0, 999999999] */
Joe Perchescf3b4292010-05-24 14:33:16 -0700121static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700122char *put_dec_full9(char *buf, unsigned q)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700123{
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700124 unsigned r;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700125
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800126 /*
127 * Possible ways to approx. divide by 10
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700128 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
129 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
130 * (x * 0x6667) >> 18 x < 43699
131 * (x * 0x3334) >> 17 x < 16389
132 * (x * 0x199a) >> 16 x < 16389
133 * (x * 0x0ccd) >> 15 x < 16389
134 * (x * 0x0667) >> 14 x < 2739
135 * (x * 0x0334) >> 13 x < 1029
136 * (x * 0x019a) >> 12 x < 1029
137 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
138 * (x * 0x0067) >> 10 x < 179
139 * (x * 0x0034) >> 9 x < 69 same
140 * (x * 0x001a) >> 8 x < 69 same
141 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
142 * (x * 0x0007) >> 6 x < 19
143 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800144 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700145 r = (q * (uint64_t)0x1999999a) >> 32;
146 *buf++ = (q - 10 * r) + '0'; /* 1 */
147 q = (r * (uint64_t)0x1999999a) >> 32;
148 *buf++ = (r - 10 * q) + '0'; /* 2 */
149 r = (q * (uint64_t)0x1999999a) >> 32;
150 *buf++ = (q - 10 * r) + '0'; /* 3 */
151 q = (r * (uint64_t)0x1999999a) >> 32;
152 *buf++ = (r - 10 * q) + '0'; /* 4 */
153 r = (q * (uint64_t)0x1999999a) >> 32;
154 *buf++ = (q - 10 * r) + '0'; /* 5 */
155 /* Now value is under 10000, can avoid 64-bit multiply */
156 q = (r * 0x199a) >> 16;
157 *buf++ = (r - 10 * q) + '0'; /* 6 */
158 r = (q * 0xcd) >> 11;
159 *buf++ = (q - 10 * r) + '0'; /* 7 */
160 q = (r * 0xcd) >> 11;
161 *buf++ = (r - 10 * q) + '0'; /* 8 */
162 *buf++ = q + '0'; /* 9 */
163 return buf;
164}
165#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700166
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700167/* Similar to above but do not pad with zeros.
168 * Code can be easily arranged to print 9 digits too, but our callers
169 * always call put_dec_full9() instead when the number has 9 decimal digits.
170 */
171static noinline_for_stack
172char *put_dec_trunc8(char *buf, unsigned r)
173{
174 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700175
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700176 /* Copy of previous function's body with added early returns */
177 q = (r * (uint64_t)0x1999999a) >> 32;
178 *buf++ = (r - 10 * q) + '0'; /* 2 */
179 if (q == 0)
180 return buf;
181 r = (q * (uint64_t)0x1999999a) >> 32;
182 *buf++ = (q - 10 * r) + '0'; /* 3 */
183 if (r == 0)
184 return buf;
185 q = (r * (uint64_t)0x1999999a) >> 32;
186 *buf++ = (r - 10 * q) + '0'; /* 4 */
187 if (q == 0)
188 return buf;
189 r = (q * (uint64_t)0x1999999a) >> 32;
190 *buf++ = (q - 10 * r) + '0'; /* 5 */
191 if (r == 0)
192 return buf;
193 q = (r * 0x199a) >> 16;
194 *buf++ = (r - 10 * q) + '0'; /* 6 */
195 if (q == 0)
196 return buf;
197 r = (q * 0xcd) >> 11;
198 *buf++ = (q - 10 * r) + '0'; /* 7 */
199 if (r == 0)
200 return buf;
201 q = (r * 0xcd) >> 11;
202 *buf++ = (r - 10 * q) + '0'; /* 8 */
203 if (q == 0)
204 return buf;
205 *buf++ = q + '0'; /* 9 */
206 return buf;
207}
208
209/* There are two algorithms to print larger numbers.
210 * One is generic: divide by 1000000000 and repeatedly print
211 * groups of (up to) 9 digits. It's conceptually simple,
212 * but requires a (unsigned long long) / 1000000000 division.
213 *
214 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
215 * manipulates them cleverly and generates groups of 4 decimal digits.
216 * It so happens that it does NOT require long long division.
217 *
218 * If long is > 32 bits, division of 64-bit values is relatively easy,
219 * and we will use the first algorithm.
220 * If long long is > 64 bits (strange architecture with VERY large long long),
221 * second algorithm can't be used, and we again use the first one.
222 *
223 * Else (if long is 32 bits and long long is 64 bits) we use second one.
224 */
225
226#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
227
228/* First algorithm: generic */
229
230static
231char *put_dec(char *buf, unsigned long long n)
232{
233 if (n >= 100*1000*1000) {
234 while (n >= 1000*1000*1000)
235 buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
236 if (n >= 100*1000*1000)
237 return put_dec_full9(buf, n);
238 }
239 return put_dec_trunc8(buf, n);
240}
241
242#else
243
244/* Second algorithm: valid only for 64-bit long longs */
245
246static noinline_for_stack
247char *put_dec_full4(char *buf, unsigned q)
248{
249 unsigned r;
250 r = (q * 0xcccd) >> 19;
251 *buf++ = (q - 10 * r) + '0';
252 q = (r * 0x199a) >> 16;
253 *buf++ = (r - 10 * q) + '0';
254 r = (q * 0xcd) >> 11;
255 *buf++ = (q - 10 * r) + '0';
256 *buf++ = r + '0';
257 return buf;
258}
259
260/* Based on code by Douglas W. Jones found at
261 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
262 * (with permission from the author).
263 * Performs no 64-bit division and hence should be fast on 32-bit machines.
264 */
265static
266char *put_dec(char *buf, unsigned long long n)
267{
268 uint32_t d3, d2, d1, q, h;
269
270 if (n < 100*1000*1000)
271 return put_dec_trunc8(buf, n);
272
273 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
274 h = (n >> 32);
275 d2 = (h ) & 0xffff;
276 d3 = (h >> 16); /* implicit "& 0xffff" */
277
278 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
279
280 buf = put_dec_full4(buf, q % 10000);
281 q = q / 10000;
282
283 d1 = q + 7671 * d3 + 9496 * d2 + 6 * d1;
284 buf = put_dec_full4(buf, d1 % 10000);
285 q = d1 / 10000;
286
287 d2 = q + 4749 * d3 + 42 * d2;
288 buf = put_dec_full4(buf, d2 % 10000);
289 q = d2 / 10000;
290
291 d3 = q + 281 * d3;
292 if (!d3)
293 goto done;
294 buf = put_dec_full4(buf, d3 % 10000);
295 q = d3 / 10000;
296 if (!q)
297 goto done;
298 buf = put_dec_full4(buf, q);
299 done:
300 while (buf[-1] == '0')
301 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800302
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700303 return buf;
304}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700305
306#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700307
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700308/*
309 * Convert passed number to decimal string.
310 * Returns the length of string. On buffer overflow, returns 0.
311 *
312 * If speed is not important, use snprintf(). It's easy to read the code.
313 */
314int num_to_str(char *buf, int size, unsigned long long num)
315{
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700316 char tmp[sizeof(num) * 3];
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700317 int idx, len;
318
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700319 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
320 if (num <= 9) {
321 tmp[0] = '0' + num;
322 len = 1;
323 } else {
324 len = put_dec(tmp, num) - tmp;
325 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700326
327 if (len > size)
328 return 0;
329 for (idx = 0; idx < len; ++idx)
330 buf[idx] = tmp[len - idx - 1];
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700331 return len;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#define ZEROPAD 1 /* pad with zero */
335#define SIGN 2 /* unsigned/signed long */
336#define PLUS 4 /* show plus */
337#define SPACE 8 /* space if plus */
338#define LEFT 16 /* left justified */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700339#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
340#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100342enum format_type {
343 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100344 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100345 FORMAT_TYPE_PRECISION,
346 FORMAT_TYPE_CHAR,
347 FORMAT_TYPE_STR,
348 FORMAT_TYPE_PTR,
349 FORMAT_TYPE_PERCENT_CHAR,
350 FORMAT_TYPE_INVALID,
351 FORMAT_TYPE_LONG_LONG,
352 FORMAT_TYPE_ULONG,
353 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800354 FORMAT_TYPE_UBYTE,
355 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100356 FORMAT_TYPE_USHORT,
357 FORMAT_TYPE_SHORT,
358 FORMAT_TYPE_UINT,
359 FORMAT_TYPE_INT,
360 FORMAT_TYPE_NRCHARS,
361 FORMAT_TYPE_SIZE_T,
362 FORMAT_TYPE_PTRDIFF
363};
364
365struct printf_spec {
Joe Perches4e310fd2010-04-14 09:27:40 -0700366 u8 type; /* format_type enum */
Joe Perchesef0658f2010-03-06 17:10:14 -0800367 u8 flags; /* flags to number() */
Joe Perches4e310fd2010-04-14 09:27:40 -0700368 u8 base; /* number base, 8, 10 or 16 only */
369 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
370 s16 field_width; /* width of output field */
371 s16 precision; /* # of digits/chars */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100372};
373
Joe Perchescf3b4292010-05-24 14:33:16 -0700374static noinline_for_stack
375char *number(char *buf, char *end, unsigned long long num,
376 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100378 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
379 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
380
381 char tmp[66];
382 char sign;
383 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100384 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700386 bool is_zero = num == 0LL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100388 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
389 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100390 locase = (spec.flags & SMALL);
391 if (spec.flags & LEFT)
392 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100394 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800395 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800397 num = -(signed long long)num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100398 spec.field_width--;
399 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100401 spec.field_width--;
402 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100404 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700407 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100408 if (spec.base == 16)
Pierre Carrier7c203422012-05-29 15:07:35 -0700409 spec.field_width -= 2;
410 else if (!is_zero)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100411 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700413
414 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700416 if (num < spec.base)
417 tmp[i++] = digits[num] | locase;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700418 /* Generic code, for any base:
419 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100420 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700421 } while (num != 0);
422 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100423 else if (spec.base != 10) { /* 8 or 16 */
424 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700425 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800426
427 if (spec.base == 16)
428 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700429 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100430 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700431 num >>= shift;
432 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700433 } else { /* base 10 */
434 i = put_dec(tmp, num) - tmp;
435 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700436
437 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100438 if (i > spec.precision)
439 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700440 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100441 spec.field_width -= spec.precision;
442 if (!(spec.flags & (ZEROPAD+LEFT))) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800443 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700444 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 *buf = ' ';
446 ++buf;
447 }
448 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700449 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700451 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *buf = sign;
453 ++buf;
454 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700455 /* "0x" / "0" prefix */
456 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700457 if (spec.base == 16 || !is_zero) {
458 if (buf < end)
459 *buf = '0';
460 ++buf;
461 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100462 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700463 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100464 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 ++buf;
466 }
467 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700468 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100469 if (!(spec.flags & LEFT)) {
470 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
471 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700472 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 *buf = c;
474 ++buf;
475 }
476 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700477 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100478 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700479 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 *buf = '0';
481 ++buf;
482 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700483 /* actual digits of result */
484 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700485 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 *buf = tmp[i];
487 ++buf;
488 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700489 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100490 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700491 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 *buf = ' ';
493 ++buf;
494 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return buf;
497}
498
Joe Perchescf3b4292010-05-24 14:33:16 -0700499static noinline_for_stack
500char *string(char *buf, char *end, const char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700501{
502 int len, i;
503
504 if ((unsigned long)s < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -0800505 s = "(null)";
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700506
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100507 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700508
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100509 if (!(spec.flags & LEFT)) {
510 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700511 if (buf < end)
512 *buf = ' ';
513 ++buf;
514 }
515 }
516 for (i = 0; i < len; ++i) {
517 if (buf < end)
518 *buf = *s;
519 ++buf; ++s;
520 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100521 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700522 if (buf < end)
523 *buf = ' ';
524 ++buf;
525 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800526
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700527 return buf;
528}
529
Joe Perchescf3b4292010-05-24 14:33:16 -0700530static noinline_for_stack
531char *symbol_string(char *buf, char *end, void *ptr,
532 struct printf_spec spec, char ext)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700533{
534 unsigned long value = (unsigned long) ptr;
535#ifdef CONFIG_KALLSYMS
536 char sym[KSYM_SYMBOL_LEN];
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900537 if (ext == 'B')
538 sprint_backtrace(sym, value);
539 else if (ext != 'f' && ext != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200540 sprint_symbol(sym, value);
541 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700542 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800543
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100544 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700545#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800546 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100547 spec.flags |= SPECIAL | SMALL | ZEROPAD;
548 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800549
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100550 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700551#endif
552}
553
Joe Perchescf3b4292010-05-24 14:33:16 -0700554static noinline_for_stack
555char *resource_string(char *buf, char *end, struct resource *res,
556 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100557{
558#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600559#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100560#endif
561
562#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600563#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100564#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700565 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100566 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700567 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100568 .precision = -1,
569 .flags = SPECIAL | SMALL | ZEROPAD,
570 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700571 static const struct printf_spec mem_spec = {
572 .base = 16,
573 .field_width = MEM_RSRC_PRINTK_SIZE,
574 .precision = -1,
575 .flags = SPECIAL | SMALL | ZEROPAD,
576 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700577 static const struct printf_spec bus_spec = {
578 .base = 16,
579 .field_width = 2,
580 .precision = -1,
581 .flags = SMALL | ZEROPAD,
582 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700583 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600584 .base = 10,
585 .precision = -1,
586 .flags = 0,
587 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700588 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600589 .field_width = -1,
590 .precision = 10,
591 .flags = LEFT,
592 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700593 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600594 .base = 16,
595 .precision = -1,
596 .flags = SPECIAL | SMALL,
597 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600598
599 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
600 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
601#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
602#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700603#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600604#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
605 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
606 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
607
Linus Torvalds332d2e72008-10-20 15:07:34 +1100608 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600609 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700610 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100611
612 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700613 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600614 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700615 specp = &io_spec;
616 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600617 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700618 specp = &mem_spec;
619 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600620 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700621 specp = &dec_spec;
622 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600623 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700624 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700625 } else if (res->flags & IORESOURCE_BUS) {
626 p = string(p, pend, "bus ", str_spec);
627 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700628 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600629 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700630 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600631 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600632 }
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700633 p = number(p, pend, res->start, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600634 if (res->start != res->end) {
635 *p++ = '-';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700636 p = number(p, pend, res->end, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600637 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600638 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600639 if (res->flags & IORESOURCE_MEM_64)
640 p = string(p, pend, " 64bit", str_spec);
641 if (res->flags & IORESOURCE_PREFETCH)
642 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700643 if (res->flags & IORESOURCE_WINDOW)
644 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600645 if (res->flags & IORESOURCE_DISABLED)
646 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600647 } else {
648 p = string(p, pend, " flags ", str_spec);
649 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600650 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100651 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600652 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100653
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100654 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100655}
656
Joe Perchescf3b4292010-05-24 14:33:16 -0700657static noinline_for_stack
658char *mac_address_string(char *buf, char *end, u8 *addr,
659 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700660{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000661 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700662 char *p = mac_addr;
663 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000664 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700665 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000666
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700667 switch (fmt[1]) {
668 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000669 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700670 break;
671
672 case 'R':
673 reversed = true;
674 /* fall through */
675
676 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000677 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700678 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000679 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700680
681 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700682 if (reversed)
683 p = hex_byte_pack(p, addr[5 - i]);
684 else
685 p = hex_byte_pack(p, addr[i]);
686
Joe Perches8a27f7c2009-08-17 12:29:44 +0000687 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000688 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700689 }
690 *p = '\0';
691
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100692 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700693}
694
Joe Perchescf3b4292010-05-24 14:33:16 -0700695static noinline_for_stack
696char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700697{
Harvey Harrison689afa72008-10-28 16:04:44 -0700698 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800699 bool leading_zeros = (fmt[0] == 'i');
700 int index;
701 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700702
Joe Perches0159f242010-01-13 20:23:30 -0800703 switch (fmt[2]) {
704 case 'h':
705#ifdef __BIG_ENDIAN
706 index = 0;
707 step = 1;
708#else
709 index = 3;
710 step = -1;
711#endif
712 break;
713 case 'l':
714 index = 3;
715 step = -1;
716 break;
717 case 'n':
718 case 'b':
719 default:
720 index = 0;
721 step = 1;
722 break;
723 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000724 for (i = 0; i < 4; i++) {
725 char temp[3]; /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700726 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000727 if (leading_zeros) {
728 if (digits < 3)
729 *p++ = '0';
730 if (digits < 2)
731 *p++ = '0';
732 }
733 /* reverse the digits in the quad */
734 while (digits--)
735 *p++ = temp[digits];
736 if (i < 3)
737 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -0800738 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000739 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000740 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800741
Joe Perches8a27f7c2009-08-17 12:29:44 +0000742 return p;
743}
744
Joe Perchescf3b4292010-05-24 14:33:16 -0700745static noinline_for_stack
746char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000747{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800748 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000749 unsigned char zerolength[8];
750 int longest = 1;
751 int colonpos = -1;
752 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800753 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000754 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000755 bool useIPv4;
756 struct in6_addr in6;
757
758 memcpy(&in6, addr, sizeof(struct in6_addr));
759
760 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000761
762 memset(zerolength, 0, sizeof(zerolength));
763
764 if (useIPv4)
765 range = 6;
766 else
767 range = 8;
768
769 /* find position of longest 0 run */
770 for (i = 0; i < range; i++) {
771 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000772 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000773 break;
774 zerolength[i]++;
775 }
776 }
777 for (i = 0; i < range; i++) {
778 if (zerolength[i] > longest) {
779 longest = zerolength[i];
780 colonpos = i;
781 }
782 }
Joe Perches29cf5192011-06-09 11:23:37 -0700783 if (longest == 1) /* don't compress a single 0 */
784 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000785
786 /* emit address */
787 for (i = 0; i < range; i++) {
788 if (i == colonpos) {
789 if (needcolon || i == 0)
790 *p++ = ':';
791 *p++ = ':';
792 needcolon = false;
793 i += longest - 1;
794 continue;
795 }
796 if (needcolon) {
797 *p++ = ':';
798 needcolon = false;
799 }
800 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +0000801 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000802 hi = word >> 8;
803 lo = word & 0xff;
804 if (hi) {
805 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700806 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000807 else
808 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700809 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000810 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -0800811 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700812 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000813 else
814 *p++ = hex_asc_lo(lo);
815 needcolon = true;
816 }
817
818 if (useIPv4) {
819 if (needcolon)
820 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -0800821 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +0000822 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000823 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800824
Joe Perches8a27f7c2009-08-17 12:29:44 +0000825 return p;
826}
827
Joe Perchescf3b4292010-05-24 14:33:16 -0700828static noinline_for_stack
829char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000830{
831 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800832
Harvey Harrison689afa72008-10-28 16:04:44 -0700833 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700834 p = hex_byte_pack(p, *addr++);
835 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000836 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700837 *p++ = ':';
838 }
839 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800840
Joe Perches8a27f7c2009-08-17 12:29:44 +0000841 return p;
842}
843
Joe Perchescf3b4292010-05-24 14:33:16 -0700844static noinline_for_stack
845char *ip6_addr_string(char *buf, char *end, const u8 *addr,
846 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000847{
848 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
849
850 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +0000851 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000852 else
Joe Percheseb78cd22009-09-18 13:04:06 +0000853 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -0700854
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100855 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700856}
857
Joe Perchescf3b4292010-05-24 14:33:16 -0700858static noinline_for_stack
859char *ip4_addr_string(char *buf, char *end, const u8 *addr,
860 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700861{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000862 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700863
Joe Perches0159f242010-01-13 20:23:30 -0800864 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700865
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100866 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700867}
868
Joe Perchescf3b4292010-05-24 14:33:16 -0700869static noinline_for_stack
870char *uuid_string(char *buf, char *end, const u8 *addr,
871 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -0800872{
873 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
874 char *p = uuid;
875 int i;
876 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
877 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
878 const u8 *index = be;
879 bool uc = false;
880
881 switch (*(++fmt)) {
882 case 'L':
883 uc = true; /* fall-through */
884 case 'l':
885 index = le;
886 break;
887 case 'B':
888 uc = true;
889 break;
890 }
891
892 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700893 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -0800894 switch (i) {
895 case 3:
896 case 5:
897 case 7:
898 case 9:
899 *p++ = '-';
900 break;
901 }
902 }
903
904 *p = 0;
905
906 if (uc) {
907 p = uuid;
908 do {
909 *p = toupper(*p);
910 } while (*(++p));
911 }
912
913 return string(buf, end, uuid, spec);
914}
915
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000916static
917char *netdev_feature_string(char *buf, char *end, const u8 *addr,
918 struct printf_spec spec)
919{
920 spec.flags |= SPECIAL | SMALL | ZEROPAD;
921 if (spec.field_width == -1)
922 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
923 spec.base = 16;
924
925 return number(buf, end, *(const netdev_features_t *)addr, spec);
926}
927
Ingo Molnar411f05f2011-05-12 23:00:28 +0200928int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800929
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700930/*
931 * Show a '%p' thing. A kernel extension is that the '%p' is followed
932 * by an extra set of alphanumeric characters that are extended format
933 * specifiers.
934 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100935 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700936 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200937 * - 'F' For symbolic function descriptor pointers with offset
938 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -0400939 * - 'S' For symbolic direct pointers with offset
940 * - 's' For symbolic direct pointers without offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900941 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600942 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
943 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700944 * - 'M' For a 6-byte MAC address, it prints the address in the
945 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000946 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +0000947 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -0800948 * with a dash-separated hex notation
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700949 * - '[mM]R For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000950 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
951 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
952 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
953 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
954 * IPv6 omits the colons (01020304...0f)
955 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Joe Perches0159f242010-01-13 20:23:30 -0800956 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
Joe Perches8a27f7c2009-08-17 12:29:44 +0000957 * - 'I6c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -0700958 * http://tools.ietf.org/html/rfc5952
Joe Perches9ac6e442009-12-14 18:01:09 -0800959 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
960 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
961 * Options for %pU are:
962 * b big endian lower case hex (default)
963 * B big endian UPPER case hex
964 * l little endian lower case hex
965 * L little endian UPPER case hex
966 * big endian output byte order is:
967 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
968 * little endian output byte order is:
969 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +0000970 * - 'V' For a struct va_format which contains a format string * and va_list *,
971 * call vsnprintf(->format, *->va_list).
972 * Implements a "recursive vsnprintf".
973 * Do not use this feature without some mechanism to verify the
974 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800975 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000976 * - 'NF' For a netdev_features_t
Joe Perches9ac6e442009-12-14 18:01:09 -0800977 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100978 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
979 * function pointers are really function descriptors, which contain a
980 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700981 */
Joe Perchescf3b4292010-05-24 14:33:16 -0700982static noinline_for_stack
983char *pointer(const char *fmt, char *buf, char *end, void *ptr,
984 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700985{
Grant Likely725fe002012-05-31 16:26:08 -0700986 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
987
Kees Cook9f36e2c2011-03-22 16:34:22 -0700988 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -0700989 /*
990 * Print (null) with the same width as a pointer so it makes
991 * tabular output look nice.
992 */
993 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -0700994 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100995 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -0700996 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800997
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700998 switch (*fmt) {
999 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001000 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001001 ptr = dereference_function_descriptor(ptr);
1002 /* Fallthrough */
1003 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001004 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001005 case 'B':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001006 return symbol_string(buf, end, ptr, spec, *fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001007 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001008 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001009 return resource_string(buf, end, ptr, spec, fmt);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001010 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1011 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001012 /* [mM]F (FDDI) */
1013 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001014 return mac_address_string(buf, end, ptr, spec, fmt);
1015 case 'I': /* Formatted IP supported
1016 * 4: 1.2.3.4
1017 * 6: 0001:0203:...:0708
1018 * 6c: 1::708 or 1::1.2.3.4
1019 */
1020 case 'i': /* Contiguous:
1021 * 4: 001.002.003.004
1022 * 6: 000102...0f
1023 */
1024 switch (fmt[1]) {
1025 case '6':
1026 return ip6_addr_string(buf, end, ptr, spec, fmt);
1027 case '4':
1028 return ip4_addr_string(buf, end, ptr, spec, fmt);
1029 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001030 break;
Joe Perches9ac6e442009-12-14 18:01:09 -08001031 case 'U':
1032 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001033 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001034 {
1035 va_list va;
1036
1037 va_copy(va, *((struct va_format *)ptr)->va);
1038 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1039 ((struct va_format *)ptr)->fmt, va);
1040 va_end(va);
1041 return buf;
1042 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001043 case 'K':
1044 /*
1045 * %pK cannot be used in IRQ context because its test
1046 * for CAP_SYSLOG would be meaningless.
1047 */
1048 if (in_irq() || in_serving_softirq() || in_nmi()) {
1049 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001050 spec.field_width = default_width;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001051 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001052 }
Joe Perches26297602011-03-22 16:34:19 -07001053 if (!((kptr_restrict == 0) ||
1054 (kptr_restrict == 1 &&
1055 has_capability_noaudit(current, CAP_SYSLOG))))
1056 ptr = NULL;
1057 break;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001058 case 'N':
1059 switch (fmt[1]) {
1060 case 'F':
1061 return netdev_feature_string(buf, end, ptr, spec);
1062 }
1063 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001064 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001065 spec.flags |= SMALL;
1066 if (spec.field_width == -1) {
Grant Likely725fe002012-05-31 16:26:08 -07001067 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001068 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001069 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001070 spec.base = 16;
1071
1072 return number(buf, end, (unsigned long) ptr, spec);
1073}
1074
1075/*
1076 * Helper function to decode printf style format.
1077 * Each call decode a token from the format and return the
1078 * number of characters read (or likely the delta where it wants
1079 * to go on the next call).
1080 * The decoded token is returned through the parameters
1081 *
1082 * 'h', 'l', or 'L' for integer fields
1083 * 'z' support added 23/7/1999 S.H.
1084 * 'z' changed to 'Z' --davidm 1/25/99
1085 * 't' added for ptrdiff_t
1086 *
1087 * @fmt: the format string
1088 * @type of the token returned
1089 * @flags: various flags such as +, -, # tokens..
1090 * @field_width: overwritten width
1091 * @base: base of the number (octal, hex, ...)
1092 * @precision: precision of a number
1093 * @qualifier: qualifier of a number (long, size_t, ...)
1094 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001095static noinline_for_stack
1096int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001097{
1098 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001099
1100 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001101 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001102 if (spec->field_width < 0) {
1103 spec->field_width = -spec->field_width;
1104 spec->flags |= LEFT;
1105 }
1106 spec->type = FORMAT_TYPE_NONE;
1107 goto precision;
1108 }
1109
1110 /* we finished early by reading the precision */
1111 if (spec->type == FORMAT_TYPE_PRECISION) {
1112 if (spec->precision < 0)
1113 spec->precision = 0;
1114
1115 spec->type = FORMAT_TYPE_NONE;
1116 goto qualifier;
1117 }
1118
1119 /* By default */
1120 spec->type = FORMAT_TYPE_NONE;
1121
1122 for (; *fmt ; ++fmt) {
1123 if (*fmt == '%')
1124 break;
1125 }
1126
1127 /* Return the current non-format string */
1128 if (fmt != start || !*fmt)
1129 return fmt - start;
1130
1131 /* Process flags */
1132 spec->flags = 0;
1133
1134 while (1) { /* this also skips first '%' */
1135 bool found = true;
1136
1137 ++fmt;
1138
1139 switch (*fmt) {
1140 case '-': spec->flags |= LEFT; break;
1141 case '+': spec->flags |= PLUS; break;
1142 case ' ': spec->flags |= SPACE; break;
1143 case '#': spec->flags |= SPECIAL; break;
1144 case '0': spec->flags |= ZEROPAD; break;
1145 default: found = false;
1146 }
1147
1148 if (!found)
1149 break;
1150 }
1151
1152 /* get field width */
1153 spec->field_width = -1;
1154
1155 if (isdigit(*fmt))
1156 spec->field_width = skip_atoi(&fmt);
1157 else if (*fmt == '*') {
1158 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001159 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001160 return ++fmt - start;
1161 }
1162
1163precision:
1164 /* get the precision */
1165 spec->precision = -1;
1166 if (*fmt == '.') {
1167 ++fmt;
1168 if (isdigit(*fmt)) {
1169 spec->precision = skip_atoi(&fmt);
1170 if (spec->precision < 0)
1171 spec->precision = 0;
1172 } else if (*fmt == '*') {
1173 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001174 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001175 return ++fmt - start;
1176 }
1177 }
1178
1179qualifier:
1180 /* get the conversion qualifier */
1181 spec->qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001182 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1183 _tolower(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001184 spec->qualifier = *fmt++;
1185 if (unlikely(spec->qualifier == *fmt)) {
1186 if (spec->qualifier == 'l') {
1187 spec->qualifier = 'L';
1188 ++fmt;
1189 } else if (spec->qualifier == 'h') {
1190 spec->qualifier = 'H';
1191 ++fmt;
1192 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001193 }
1194 }
1195
1196 /* default base */
1197 spec->base = 10;
1198 switch (*fmt) {
1199 case 'c':
1200 spec->type = FORMAT_TYPE_CHAR;
1201 return ++fmt - start;
1202
1203 case 's':
1204 spec->type = FORMAT_TYPE_STR;
1205 return ++fmt - start;
1206
1207 case 'p':
1208 spec->type = FORMAT_TYPE_PTR;
1209 return fmt - start;
1210 /* skip alnum */
1211
1212 case 'n':
1213 spec->type = FORMAT_TYPE_NRCHARS;
1214 return ++fmt - start;
1215
1216 case '%':
1217 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1218 return ++fmt - start;
1219
1220 /* integer number formats - set up the flags and "break" */
1221 case 'o':
1222 spec->base = 8;
1223 break;
1224
1225 case 'x':
1226 spec->flags |= SMALL;
1227
1228 case 'X':
1229 spec->base = 16;
1230 break;
1231
1232 case 'd':
1233 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001234 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001235 case 'u':
1236 break;
1237
1238 default:
1239 spec->type = FORMAT_TYPE_INVALID;
1240 return fmt - start;
1241 }
1242
1243 if (spec->qualifier == 'L')
1244 spec->type = FORMAT_TYPE_LONG_LONG;
1245 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001246 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001247 spec->type = FORMAT_TYPE_LONG;
1248 else
1249 spec->type = FORMAT_TYPE_ULONG;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001250 } else if (_tolower(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001251 spec->type = FORMAT_TYPE_SIZE_T;
1252 } else if (spec->qualifier == 't') {
1253 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001254 } else if (spec->qualifier == 'H') {
1255 if (spec->flags & SIGN)
1256 spec->type = FORMAT_TYPE_BYTE;
1257 else
1258 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001259 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001260 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001261 spec->type = FORMAT_TYPE_SHORT;
1262 else
1263 spec->type = FORMAT_TYPE_USHORT;
1264 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001265 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001266 spec->type = FORMAT_TYPE_INT;
1267 else
1268 spec->type = FORMAT_TYPE_UINT;
1269 }
1270
1271 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001272}
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274/**
1275 * vsnprintf - Format a string and place it in a buffer
1276 * @buf: The buffer to place the result into
1277 * @size: The size of the buffer, including the trailing null space
1278 * @fmt: The format string to use
1279 * @args: Arguments for the format string
1280 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001281 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001282 * %pS output the name of a text symbol with offset
1283 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001284 * %pF output the name of a function pointer with its offset
1285 * %pf output the name of a function pointer without its offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001286 * %pB output the name of a backtrace symbol with its offset
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001287 * %pR output the address range in a struct resource with decoded flags
1288 * %pr output the address range in a struct resource with raw flags
1289 * %pM output a 6-byte MAC address with colons
1290 * %pm output a 6-byte MAC address without colons
1291 * %pI4 print an IPv4 address without leading zeros
1292 * %pi4 print an IPv4 address with leading zeros
1293 * %pI6 print an IPv6 address with colons
1294 * %pi6 print an IPv6 address without colons
Jan Engelhardtf996f202011-07-14 18:48:56 +02001295 * %pI6c print an IPv6 address as specified by RFC 5952
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001296 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1297 * case.
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001298 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001299 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 * The return value is the number of characters which would
1301 * be generated for the given input, excluding the trailing
1302 * '\0', as per ISO C99. If you want to have the exact
1303 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001304 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 * return is greater than or equal to @size, the resulting
1306 * string is truncated.
1307 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001308 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
1310int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001313 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001314 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001316 /* Reject out-of-range values early. Large positive sizes are
1317 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001318 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001322 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001324 /* Make sure end is always >= buf */
1325 if (end < buf) {
1326 end = ((void *)-1);
1327 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001330 while (*fmt) {
1331 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001332 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001333
1334 fmt += read;
1335
1336 switch (spec.type) {
1337 case FORMAT_TYPE_NONE: {
1338 int copy = read;
1339 if (str < end) {
1340 if (copy > end - str)
1341 copy = end - str;
1342 memcpy(str, old_fmt, copy);
1343 }
1344 str += read;
1345 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347
Vegard Nossumed681a92009-03-14 12:08:50 +01001348 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001349 spec.field_width = va_arg(args, int);
1350 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001352 case FORMAT_TYPE_PRECISION:
1353 spec.precision = va_arg(args, int);
1354 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
André Goddard Rosad4be1512009-12-14 18:00:59 -08001356 case FORMAT_TYPE_CHAR: {
1357 char c;
1358
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001359 if (!(spec.flags & LEFT)) {
1360 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001361 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 *str = ' ';
1363 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001366 }
1367 c = (unsigned char) va_arg(args, int);
1368 if (str < end)
1369 *str = c;
1370 ++str;
1371 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001372 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001373 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001375 }
1376 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001379 case FORMAT_TYPE_STR:
1380 str = string(str, end, va_arg(args, char *), spec);
1381 break;
1382
1383 case FORMAT_TYPE_PTR:
1384 str = pointer(fmt+1, str, end, va_arg(args, void *),
1385 spec);
1386 while (isalnum(*fmt))
1387 fmt++;
1388 break;
1389
1390 case FORMAT_TYPE_PERCENT_CHAR:
1391 if (str < end)
1392 *str = '%';
1393 ++str;
1394 break;
1395
1396 case FORMAT_TYPE_INVALID:
1397 if (str < end)
1398 *str = '%';
1399 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001400 break;
1401
1402 case FORMAT_TYPE_NRCHARS: {
Joe Perchesef0658f2010-03-06 17:10:14 -08001403 u8 qualifier = spec.qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001404
1405 if (qualifier == 'l') {
1406 long *ip = va_arg(args, long *);
1407 *ip = (str - buf);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001408 } else if (_tolower(qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001409 size_t *ip = va_arg(args, size_t *);
1410 *ip = (str - buf);
1411 } else {
1412 int *ip = va_arg(args, int *);
1413 *ip = (str - buf);
1414 }
1415 break;
1416 }
1417
1418 default:
1419 switch (spec.type) {
1420 case FORMAT_TYPE_LONG_LONG:
1421 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001423 case FORMAT_TYPE_ULONG:
1424 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001426 case FORMAT_TYPE_LONG:
1427 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001429 case FORMAT_TYPE_SIZE_T:
1430 num = va_arg(args, size_t);
1431 break;
1432 case FORMAT_TYPE_PTRDIFF:
1433 num = va_arg(args, ptrdiff_t);
1434 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001435 case FORMAT_TYPE_UBYTE:
1436 num = (unsigned char) va_arg(args, int);
1437 break;
1438 case FORMAT_TYPE_BYTE:
1439 num = (signed char) va_arg(args, int);
1440 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001441 case FORMAT_TYPE_USHORT:
1442 num = (unsigned short) va_arg(args, int);
1443 break;
1444 case FORMAT_TYPE_SHORT:
1445 num = (short) va_arg(args, int);
1446 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001447 case FORMAT_TYPE_INT:
1448 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001449 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001451 num = va_arg(args, unsigned int);
1452 }
1453
1454 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001457
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001458 if (size > 0) {
1459 if (str < end)
1460 *str = '\0';
1461 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001462 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001463 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001464
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001465 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469EXPORT_SYMBOL(vsnprintf);
1470
1471/**
1472 * vscnprintf - Format a string and place it in a buffer
1473 * @buf: The buffer to place the result into
1474 * @size: The size of the buffer, including the trailing null space
1475 * @fmt: The format string to use
1476 * @args: Arguments for the format string
1477 *
1478 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08001479 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 * returns 0.
1481 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001482 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001483 *
1484 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 */
1486int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1487{
1488 int i;
1489
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001490 i = vsnprintf(buf, size, fmt, args);
1491
Anton Arapovb921c692011-01-12 16:59:49 -08001492 if (likely(i < size))
1493 return i;
1494 if (size != 0)
1495 return size - 1;
1496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498EXPORT_SYMBOL(vscnprintf);
1499
1500/**
1501 * snprintf - Format a string and place it in a buffer
1502 * @buf: The buffer to place the result into
1503 * @size: The size of the buffer, including the trailing null space
1504 * @fmt: The format string to use
1505 * @...: Arguments for the format string
1506 *
1507 * The return value is the number of characters which would be
1508 * generated for the given input, excluding the trailing null,
1509 * as per ISO C99. If the return is greater than or equal to
1510 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001511 *
1512 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001514int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 va_list args;
1517 int i;
1518
1519 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001520 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return i;
1524}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525EXPORT_SYMBOL(snprintf);
1526
1527/**
1528 * scnprintf - Format a string and place it in a buffer
1529 * @buf: The buffer to place the result into
1530 * @size: The size of the buffer, including the trailing null space
1531 * @fmt: The format string to use
1532 * @...: Arguments for the format string
1533 *
1534 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07001535 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 */
1537
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001538int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 va_list args;
1541 int i;
1542
1543 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08001544 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001546
Anton Arapovb921c692011-01-12 16:59:49 -08001547 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549EXPORT_SYMBOL(scnprintf);
1550
1551/**
1552 * vsprintf - Format a string and place it in a buffer
1553 * @buf: The buffer to place the result into
1554 * @fmt: The format string to use
1555 * @args: Arguments for the format string
1556 *
1557 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001558 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 * buffer overflows.
1560 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001561 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001562 *
1563 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 */
1565int vsprintf(char *buf, const char *fmt, va_list args)
1566{
1567 return vsnprintf(buf, INT_MAX, fmt, args);
1568}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569EXPORT_SYMBOL(vsprintf);
1570
1571/**
1572 * sprintf - Format a string and place it in a buffer
1573 * @buf: The buffer to place the result into
1574 * @fmt: The format string to use
1575 * @...: Arguments for the format string
1576 *
1577 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001578 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001580 *
1581 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001583int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 va_list args;
1586 int i;
1587
1588 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001589 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return i;
1593}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594EXPORT_SYMBOL(sprintf);
1595
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001596#ifdef CONFIG_BINARY_PRINTF
1597/*
1598 * bprintf service:
1599 * vbin_printf() - VA arguments to binary data
1600 * bstr_printf() - Binary data to text string
1601 */
1602
1603/**
1604 * vbin_printf - Parse a format string and place args' binary value in a buffer
1605 * @bin_buf: The buffer to place args' binary value
1606 * @size: The size of the buffer(by words(32bits), not characters)
1607 * @fmt: The format string to use
1608 * @args: Arguments for the format string
1609 *
1610 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1611 * is skiped.
1612 *
1613 * The return value is the number of words(32bits) which would be generated for
1614 * the given input.
1615 *
1616 * NOTE:
1617 * If the return value is greater than @size, the resulting bin_buf is NOT
1618 * valid for bstr_printf().
1619 */
1620int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1621{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001622 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001623 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001624
1625 str = (char *)bin_buf;
1626 end = (char *)(bin_buf + size);
1627
1628#define save_arg(type) \
1629do { \
1630 if (sizeof(type) == 8) { \
1631 unsigned long long value; \
1632 str = PTR_ALIGN(str, sizeof(u32)); \
1633 value = va_arg(args, unsigned long long); \
1634 if (str + sizeof(type) <= end) { \
1635 *(u32 *)str = *(u32 *)&value; \
1636 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1637 } \
1638 } else { \
1639 unsigned long value; \
1640 str = PTR_ALIGN(str, sizeof(type)); \
1641 value = va_arg(args, int); \
1642 if (str + sizeof(type) <= end) \
1643 *(typeof(type) *)str = (type)value; \
1644 } \
1645 str += sizeof(type); \
1646} while (0)
1647
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001648 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08001649 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001650
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001651 fmt += read;
1652
1653 switch (spec.type) {
1654 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08001655 case FORMAT_TYPE_INVALID:
1656 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001657 break;
1658
Vegard Nossumed681a92009-03-14 12:08:50 +01001659 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001660 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001661 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001662 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001663
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001664 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001665 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001666 break;
1667
1668 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001669 const char *save_str = va_arg(args, char *);
1670 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08001671
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001672 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1673 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08001674 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08001675 len = strlen(save_str) + 1;
1676 if (str + len < end)
1677 memcpy(str, save_str, len);
1678 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001679 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001680 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001681
1682 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001683 save_arg(void *);
1684 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001685 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001686 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001687 break;
1688
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001689 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001690 /* skip %n 's argument */
Joe Perchesef0658f2010-03-06 17:10:14 -08001691 u8 qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001692 void *skip_arg;
1693 if (qualifier == 'l')
1694 skip_arg = va_arg(args, long *);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001695 else if (_tolower(qualifier) == 'z')
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001696 skip_arg = va_arg(args, size_t *);
1697 else
1698 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001699 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001700 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001701
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001702 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001703 switch (spec.type) {
1704
1705 case FORMAT_TYPE_LONG_LONG:
1706 save_arg(long long);
1707 break;
1708 case FORMAT_TYPE_ULONG:
1709 case FORMAT_TYPE_LONG:
1710 save_arg(unsigned long);
1711 break;
1712 case FORMAT_TYPE_SIZE_T:
1713 save_arg(size_t);
1714 break;
1715 case FORMAT_TYPE_PTRDIFF:
1716 save_arg(ptrdiff_t);
1717 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001718 case FORMAT_TYPE_UBYTE:
1719 case FORMAT_TYPE_BYTE:
1720 save_arg(char);
1721 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001722 case FORMAT_TYPE_USHORT:
1723 case FORMAT_TYPE_SHORT:
1724 save_arg(short);
1725 break;
1726 default:
1727 save_arg(int);
1728 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001729 }
1730 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001731
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001732 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001733#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001734}
1735EXPORT_SYMBOL_GPL(vbin_printf);
1736
1737/**
1738 * bstr_printf - Format a string from binary arguments and place it in a buffer
1739 * @buf: The buffer to place the result into
1740 * @size: The size of the buffer, including the trailing null space
1741 * @fmt: The format string to use
1742 * @bin_buf: Binary arguments for the format string
1743 *
1744 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1745 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1746 * a binary buffer that generated by vbin_printf.
1747 *
1748 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001749 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001750 *
1751 * The return value is the number of characters which would
1752 * be generated for the given input, excluding the trailing
1753 * '\0', as per ISO C99. If you want to have the exact
1754 * number of characters written into @buf as return value
1755 * (not including the trailing '\0'), use vscnprintf(). If the
1756 * return is greater than or equal to @size, the resulting
1757 * string is truncated.
1758 */
1759int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1760{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001761 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08001762 char *str, *end;
1763 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001764
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001765 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001766 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001767
1768 str = buf;
1769 end = buf + size;
1770
1771#define get_arg(type) \
1772({ \
1773 typeof(type) value; \
1774 if (sizeof(type) == 8) { \
1775 args = PTR_ALIGN(args, sizeof(u32)); \
1776 *(u32 *)&value = *(u32 *)args; \
1777 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1778 } else { \
1779 args = PTR_ALIGN(args, sizeof(type)); \
1780 value = *(typeof(type) *)args; \
1781 } \
1782 args += sizeof(type); \
1783 value; \
1784})
1785
1786 /* Make sure end is always >= buf */
1787 if (end < buf) {
1788 end = ((void *)-1);
1789 size = end - buf;
1790 }
1791
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001792 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001793 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001794 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001795
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001796 fmt += read;
1797
1798 switch (spec.type) {
1799 case FORMAT_TYPE_NONE: {
1800 int copy = read;
1801 if (str < end) {
1802 if (copy > end - str)
1803 copy = end - str;
1804 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001805 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001806 str += read;
1807 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001808 }
1809
Vegard Nossumed681a92009-03-14 12:08:50 +01001810 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001811 spec.field_width = get_arg(int);
1812 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001813
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001814 case FORMAT_TYPE_PRECISION:
1815 spec.precision = get_arg(int);
1816 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001817
André Goddard Rosad4be1512009-12-14 18:00:59 -08001818 case FORMAT_TYPE_CHAR: {
1819 char c;
1820
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001821 if (!(spec.flags & LEFT)) {
1822 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001823 if (str < end)
1824 *str = ' ';
1825 ++str;
1826 }
1827 }
1828 c = (unsigned char) get_arg(char);
1829 if (str < end)
1830 *str = c;
1831 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001832 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001833 if (str < end)
1834 *str = ' ';
1835 ++str;
1836 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001837 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001838 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001839
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001840 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001841 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001842 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001843 str = string(str, end, (char *)str_arg, spec);
1844 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001845 }
1846
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001847 case FORMAT_TYPE_PTR:
1848 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1849 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001850 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001851 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001852
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001853 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001854 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001855 if (str < end)
1856 *str = '%';
1857 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001858 break;
1859
1860 case FORMAT_TYPE_NRCHARS:
1861 /* skip */
1862 break;
1863
André Goddard Rosad4be1512009-12-14 18:00:59 -08001864 default: {
1865 unsigned long long num;
1866
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001867 switch (spec.type) {
1868
1869 case FORMAT_TYPE_LONG_LONG:
1870 num = get_arg(long long);
1871 break;
1872 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001873 case FORMAT_TYPE_LONG:
1874 num = get_arg(unsigned long);
1875 break;
1876 case FORMAT_TYPE_SIZE_T:
1877 num = get_arg(size_t);
1878 break;
1879 case FORMAT_TYPE_PTRDIFF:
1880 num = get_arg(ptrdiff_t);
1881 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001882 case FORMAT_TYPE_UBYTE:
1883 num = get_arg(unsigned char);
1884 break;
1885 case FORMAT_TYPE_BYTE:
1886 num = get_arg(signed char);
1887 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001888 case FORMAT_TYPE_USHORT:
1889 num = get_arg(unsigned short);
1890 break;
1891 case FORMAT_TYPE_SHORT:
1892 num = get_arg(short);
1893 break;
1894 case FORMAT_TYPE_UINT:
1895 num = get_arg(unsigned int);
1896 break;
1897 default:
1898 num = get_arg(int);
1899 }
1900
1901 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08001902 } /* default: */
1903 } /* switch(spec.type) */
1904 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001905
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001906 if (size > 0) {
1907 if (str < end)
1908 *str = '\0';
1909 else
1910 end[-1] = '\0';
1911 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001912
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001913#undef get_arg
1914
1915 /* the trailing null byte doesn't count towards the total */
1916 return str - buf;
1917}
1918EXPORT_SYMBOL_GPL(bstr_printf);
1919
1920/**
1921 * bprintf - Parse a format string and place args' binary value in a buffer
1922 * @bin_buf: The buffer to place args' binary value
1923 * @size: The size of the buffer(by words(32bits), not characters)
1924 * @fmt: The format string to use
1925 * @...: Arguments for the format string
1926 *
1927 * The function returns the number of words(u32) written
1928 * into @bin_buf.
1929 */
1930int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1931{
1932 va_list args;
1933 int ret;
1934
1935 va_start(args, fmt);
1936 ret = vbin_printf(bin_buf, size, fmt, args);
1937 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001938
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001939 return ret;
1940}
1941EXPORT_SYMBOL_GPL(bprintf);
1942
1943#endif /* CONFIG_BINARY_PRINTF */
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945/**
1946 * vsscanf - Unformat a buffer into a list of arguments
1947 * @buf: input buffer
1948 * @fmt: format of buffer
1949 * @args: arguments
1950 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001951int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
1953 const char *str = buf;
1954 char *next;
1955 char digit;
1956 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08001957 u8 qualifier;
1958 u8 base;
1959 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001960 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001962 while (*fmt && *str) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /* skip any white space in format */
1964 /* white space in format matchs any amount of
1965 * white space, including none, in the input.
1966 */
1967 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08001968 fmt = skip_spaces(++fmt);
1969 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971
1972 /* anything that is not a conversion must match exactly */
1973 if (*fmt != '%' && *fmt) {
1974 if (*fmt++ != *str++)
1975 break;
1976 continue;
1977 }
1978
1979 if (!*fmt)
1980 break;
1981 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 /* skip this conversion.
1984 * advance both strings to next white space
1985 */
1986 if (*fmt == '*') {
Andy Spencer8fccae22009-10-01 15:44:27 -07001987 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 fmt++;
1989 while (!isspace(*str) && *str)
1990 str++;
1991 continue;
1992 }
1993
1994 /* get field width */
1995 field_width = -1;
1996 if (isdigit(*fmt))
1997 field_width = skip_atoi(&fmt);
1998
1999 /* get conversion qualifier */
2000 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002001 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2002 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 qualifier = *fmt++;
2004 if (unlikely(qualifier == *fmt)) {
2005 if (qualifier == 'h') {
2006 qualifier = 'H';
2007 fmt++;
2008 } else if (qualifier == 'l') {
2009 qualifier = 'L';
2010 fmt++;
2011 }
2012 }
2013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 if (!*fmt || !*str)
2016 break;
2017
André Goddard Rosad4be1512009-12-14 18:00:59 -08002018 base = 10;
2019 is_sign = 0;
2020
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002021 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 case 'c':
2023 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002024 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (field_width == -1)
2026 field_width = 1;
2027 do {
2028 *s++ = *str++;
2029 } while (--field_width > 0 && *str);
2030 num++;
2031 }
2032 continue;
2033 case 's':
2034 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002035 char *s = (char *)va_arg(args, char *);
2036 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002037 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002039 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002042 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 *s = '\0';
2045 num++;
2046 }
2047 continue;
2048 case 'n':
2049 /* return number of characters read so far */
2050 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002051 int *i = (int *)va_arg(args, int*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 *i = str - buf;
2053 }
2054 continue;
2055 case 'o':
2056 base = 8;
2057 break;
2058 case 'x':
2059 case 'X':
2060 base = 16;
2061 break;
2062 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002063 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 case 'd':
2065 is_sign = 1;
2066 case 'u':
2067 break;
2068 case '%':
2069 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002070 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return num;
2072 continue;
2073 default:
2074 /* invalid format; stop here */
2075 return num;
2076 }
2077
2078 /* have some sort of integer conversion.
2079 * first, skip white space in buffer.
2080 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002081 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 digit = *str;
2084 if (is_sign && digit == '-')
2085 digit = *(str + 1);
2086
2087 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002088 || (base == 16 && !isxdigit(digit))
2089 || (base == 10 && !isdigit(digit))
2090 || (base == 8 && (!isdigit(digit) || digit > '7'))
2091 || (base == 0 && !isdigit(digit)))
2092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002094 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 case 'H': /* that's 'hh' in format */
2096 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002097 signed char *s = (signed char *)va_arg(args, signed char *);
2098 *s = (signed char)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002100 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
2101 *s = (unsigned char)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103 break;
2104 case 'h':
2105 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002106 short *s = (short *)va_arg(args, short *);
2107 *s = (short)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002109 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
2110 *s = (unsigned short)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112 break;
2113 case 'l':
2114 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002115 long *l = (long *)va_arg(args, long *);
2116 *l = simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002118 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
2119 *l = simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
2121 break;
2122 case 'L':
2123 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002124 long long *l = (long long *)va_arg(args, long long *);
2125 *l = simple_strtoll(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002127 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2128 *l = simple_strtoull(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
2130 break;
2131 case 'Z':
2132 case 'z':
2133 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002134 size_t *s = (size_t *)va_arg(args, size_t *);
2135 *s = (size_t)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
2137 break;
2138 default:
2139 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002140 int *i = (int *)va_arg(args, int *);
2141 *i = (int)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002143 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2144 *i = (unsigned int)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 }
2146 break;
2147 }
2148 num++;
2149
2150 if (!next)
2151 break;
2152 str = next;
2153 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002154
2155 /*
2156 * Now we've come all the way through so either the input string or the
2157 * format ended. In the former case, there can be a %n at the current
2158 * position in the format that needs to be filled.
2159 */
2160 if (*fmt == '%' && *(fmt + 1) == 'n') {
2161 int *p = (int *)va_arg(args, int *);
2162 *p = str - buf;
2163 }
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return num;
2166}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167EXPORT_SYMBOL(vsscanf);
2168
2169/**
2170 * sscanf - Unformat a buffer into a list of arguments
2171 * @buf: input buffer
2172 * @fmt: formatting of buffer
2173 * @...: resulting arguments
2174 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002175int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
2177 va_list args;
2178 int i;
2179
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002180 va_start(args, fmt);
2181 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return i;
2185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186EXPORT_SYMBOL(sscanf);