]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - lib/vsprintf.c
lib/vsprintf.c: eliminate potential race in string()
[mirror_ubuntu-zesty-kernel.git] / lib / vsprintf.c
CommitLineData
1da177e4
LT
1/*
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
7b9186f5 12/*
1da177e4
LT
13 * 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>
0d1d7a55 20#include <linux/clk.h>
900cca29 21#include <linux/clk-provider.h>
8bc3bcc9 22#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
1da177e4
LT
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/ctype.h>
26#include <linux/kernel.h>
0fe1ef24 27#include <linux/kallsyms.h>
53809751 28#include <linux/math64.h>
0fe1ef24 29#include <linux/uaccess.h>
332d2e78 30#include <linux/ioport.h>
4b6ccca7 31#include <linux/dcache.h>
312b4e22 32#include <linux/cred.h>
8a27f7c9 33#include <net/addrconf.h>
1031bc58
DM
34#ifdef CONFIG_BLOCK
35#include <linux/blkdev.h>
36#endif
1da177e4 37
4e57b681 38#include <asm/page.h> /* for PAGE_SIZE */
deac93df 39#include <asm/sections.h> /* for dereference_function_descriptor() */
7c43d9a3 40#include <asm/byteorder.h> /* cpu_to_le16 */
1da177e4 41
71dca95d 42#include <linux/string_helpers.h>
1dff46d6 43#include "kstrtox.h"
aa46a63e 44
1da177e4 45/**
922ac25c 46 * simple_strtoull - convert a string to an unsigned long long
1da177e4
LT
47 * @cp: The start of the string
48 * @endp: A pointer to the end of the parsed string will be placed here
49 * @base: The number base to use
462e4711
EZ
50 *
51 * This function is obsolete. Please use kstrtoull instead.
1da177e4 52 */
922ac25c 53unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
1da177e4 54{
1dff46d6
AD
55 unsigned long long result;
56 unsigned int rv;
aa46a63e 57
1dff46d6
AD
58 cp = _parse_integer_fixup_radix(cp, &base);
59 rv = _parse_integer(cp, base, &result);
60 /* FIXME */
61 cp += (rv & ~KSTRTOX_OVERFLOW);
aa46a63e 62
1da177e4
LT
63 if (endp)
64 *endp = (char *)cp;
7b9186f5 65
1da177e4
LT
66 return result;
67}
922ac25c 68EXPORT_SYMBOL(simple_strtoull);
1da177e4
LT
69
70/**
922ac25c 71 * simple_strtoul - convert a string to an unsigned long
1da177e4
LT
72 * @cp: The start of the string
73 * @endp: A pointer to the end of the parsed string will be placed here
74 * @base: The number base to use
462e4711
EZ
75 *
76 * This function is obsolete. Please use kstrtoul instead.
1da177e4 77 */
922ac25c 78unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
1da177e4 79{
922ac25c 80 return simple_strtoull(cp, endp, base);
1da177e4 81}
922ac25c 82EXPORT_SYMBOL(simple_strtoul);
1da177e4
LT
83
84/**
922ac25c 85 * simple_strtol - convert a string to a signed long
1da177e4
LT
86 * @cp: The start of the string
87 * @endp: A pointer to the end of the parsed string will be placed here
88 * @base: The number base to use
462e4711
EZ
89 *
90 * This function is obsolete. Please use kstrtol instead.
1da177e4 91 */
922ac25c 92long simple_strtol(const char *cp, char **endp, unsigned int base)
1da177e4 93{
922ac25c
AGR
94 if (*cp == '-')
95 return -simple_strtoul(cp + 1, endp, base);
7b9186f5 96
922ac25c 97 return simple_strtoul(cp, endp, base);
1da177e4 98}
922ac25c 99EXPORT_SYMBOL(simple_strtol);
1da177e4
LT
100
101/**
102 * simple_strtoll - convert a string to a signed long long
103 * @cp: The start of the string
104 * @endp: A pointer to the end of the parsed string will be placed here
105 * @base: The number base to use
462e4711
EZ
106 *
107 * This function is obsolete. Please use kstrtoll instead.
1da177e4 108 */
22d27051 109long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1da177e4 110{
7b9186f5 111 if (*cp == '-')
22d27051 112 return -simple_strtoull(cp + 1, endp, base);
7b9186f5 113
22d27051 114 return simple_strtoull(cp, endp, base);
1da177e4 115}
98d5ce0d 116EXPORT_SYMBOL(simple_strtoll);
1da177e4 117
cf3b429b
JP
118static noinline_for_stack
119int skip_atoi(const char **s)
1da177e4 120{
7b9186f5 121 int i = 0;
1da177e4 122
43e5b666 123 do {
1da177e4 124 i = i*10 + *((*s)++) - '0';
43e5b666 125 } while (isdigit(**s));
7b9186f5 126
1da177e4
LT
127 return i;
128}
129
7c43d9a3
RV
130/*
131 * Decimal conversion is by far the most typical, and is used for
132 * /proc and /sys data. This directly impacts e.g. top performance
133 * with many processes running. We optimize it for speed by emitting
134 * two characters at a time, using a 200 byte lookup table. This
135 * roughly halves the number of multiplications compared to computing
136 * the digits one at a time. Implementation strongly inspired by the
137 * previous version, which in turn used ideas described at
138 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
139 * from the author, Douglas W. Jones).
140 *
141 * It turns out there is precisely one 26 bit fixed-point
142 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
143 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
144 * range happens to be somewhat larger (x <= 1073741898), but that's
145 * irrelevant for our purpose.
146 *
147 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
148 * need a 32x32->64 bit multiply, so we simply use the same constant.
149 *
150 * For dividing a number in the range [100, 10^4-1] by 100, there are
151 * several options. The simplest is (x * 0x147b) >> 19, which is valid
152 * for all x <= 43698.
133fd9f5 153 */
4277eedd 154
7c43d9a3
RV
155static const u16 decpair[100] = {
156#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
157 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
158 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
159 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
160 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
161 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
162 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
163 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
164 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
165 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
166 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
167#undef _
168};
169
170/*
171 * This will print a single '0' even if r == 0, since we would
675cf53c
RV
172 * immediately jump to out_r where two 0s would be written but only
173 * one of them accounted for in buf. This is needed by ip4_string
174 * below. All other callers pass a non-zero value of r.
7c43d9a3 175*/
cf3b429b 176static noinline_for_stack
7c43d9a3 177char *put_dec_trunc8(char *buf, unsigned r)
4277eedd 178{
7c43d9a3
RV
179 unsigned q;
180
181 /* 1 <= r < 10^8 */
182 if (r < 100)
183 goto out_r;
184
185 /* 100 <= r < 10^8 */
186 q = (r * (u64)0x28f5c29) >> 32;
187 *((u16 *)buf) = decpair[r - 100*q];
188 buf += 2;
189
190 /* 1 <= q < 10^6 */
191 if (q < 100)
192 goto out_q;
193
194 /* 100 <= q < 10^6 */
195 r = (q * (u64)0x28f5c29) >> 32;
196 *((u16 *)buf) = decpair[q - 100*r];
197 buf += 2;
198
199 /* 1 <= r < 10^4 */
200 if (r < 100)
201 goto out_r;
202
203 /* 100 <= r < 10^4 */
204 q = (r * 0x147b) >> 19;
205 *((u16 *)buf) = decpair[r - 100*q];
206 buf += 2;
207out_q:
208 /* 1 <= q < 100 */
209 r = q;
210out_r:
211 /* 1 <= r < 100 */
212 *((u16 *)buf) = decpair[r];
675cf53c 213 buf += r < 10 ? 1 : 2;
4277eedd
DV
214 return buf;
215}
133fd9f5 216
7c43d9a3 217#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
cf3b429b 218static noinline_for_stack
7c43d9a3 219char *put_dec_full8(char *buf, unsigned r)
4277eedd 220{
133fd9f5
DV
221 unsigned q;
222
7c43d9a3
RV
223 /* 0 <= r < 10^8 */
224 q = (r * (u64)0x28f5c29) >> 32;
225 *((u16 *)buf) = decpair[r - 100*q];
226 buf += 2;
4277eedd 227
7c43d9a3
RV
228 /* 0 <= q < 10^6 */
229 r = (q * (u64)0x28f5c29) >> 32;
230 *((u16 *)buf) = decpair[q - 100*r];
231 buf += 2;
7b9186f5 232
7c43d9a3
RV
233 /* 0 <= r < 10^4 */
234 q = (r * 0x147b) >> 19;
235 *((u16 *)buf) = decpair[r - 100*q];
236 buf += 2;
133fd9f5 237
7c43d9a3
RV
238 /* 0 <= q < 100 */
239 *((u16 *)buf) = decpair[q];
240 buf += 2;
241 return buf;
242}
133fd9f5 243
7c43d9a3 244static noinline_for_stack
133fd9f5
DV
245char *put_dec(char *buf, unsigned long long n)
246{
7c43d9a3
RV
247 if (n >= 100*1000*1000)
248 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
249 /* 1 <= n <= 1.6e11 */
250 if (n >= 100*1000*1000)
251 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
252 /* 1 <= n < 1e8 */
133fd9f5 253 return put_dec_trunc8(buf, n);
4277eedd 254}
133fd9f5 255
7c43d9a3 256#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
133fd9f5 257
7c43d9a3
RV
258static void
259put_dec_full4(char *buf, unsigned r)
4277eedd 260{
7c43d9a3
RV
261 unsigned q;
262
263 /* 0 <= r < 10^4 */
264 q = (r * 0x147b) >> 19;
265 *((u16 *)buf) = decpair[r - 100*q];
266 buf += 2;
267 /* 0 <= q < 100 */
268 *((u16 *)buf) = decpair[q];
2359172a
GS
269}
270
271/*
272 * Call put_dec_full4 on x % 10000, return x / 10000.
273 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
274 * holds for all x < 1,128,869,999. The largest value this
275 * helper will ever be asked to convert is 1,125,520,955.
7c43d9a3 276 * (second call in the put_dec code, assuming n is all-ones).
2359172a 277 */
7c43d9a3 278static noinline_for_stack
2359172a
GS
279unsigned put_dec_helper4(char *buf, unsigned x)
280{
281 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
282
283 put_dec_full4(buf, x - q * 10000);
284 return q;
4277eedd
DV
285}
286
133fd9f5
DV
287/* Based on code by Douglas W. Jones found at
288 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
289 * (with permission from the author).
290 * Performs no 64-bit division and hence should be fast on 32-bit machines.
291 */
292static
293char *put_dec(char *buf, unsigned long long n)
294{
295 uint32_t d3, d2, d1, q, h;
296
297 if (n < 100*1000*1000)
298 return put_dec_trunc8(buf, n);
299
300 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
301 h = (n >> 32);
302 d2 = (h ) & 0xffff;
303 d3 = (h >> 16); /* implicit "& 0xffff" */
304
7c43d9a3
RV
305 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
306 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
133fd9f5 307 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
2359172a
GS
308 q = put_dec_helper4(buf, q);
309
310 q += 7671 * d3 + 9496 * d2 + 6 * d1;
311 q = put_dec_helper4(buf+4, q);
312
313 q += 4749 * d3 + 42 * d2;
314 q = put_dec_helper4(buf+8, q);
133fd9f5 315
2359172a
GS
316 q += 281 * d3;
317 buf += 12;
318 if (q)
319 buf = put_dec_trunc8(buf, q);
320 else while (buf[-1] == '0')
133fd9f5
DV
321 --buf;
322
323 return buf;
324}
325
326#endif
327
1ac101a5
KH
328/*
329 * Convert passed number to decimal string.
330 * Returns the length of string. On buffer overflow, returns 0.
331 *
332 * If speed is not important, use snprintf(). It's easy to read the code.
333 */
334int num_to_str(char *buf, int size, unsigned long long num)
335{
7c43d9a3
RV
336 /* put_dec requires 2-byte alignment of the buffer. */
337 char tmp[sizeof(num) * 3] __aligned(2);
1ac101a5
KH
338 int idx, len;
339
133fd9f5
DV
340 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
341 if (num <= 9) {
342 tmp[0] = '0' + num;
343 len = 1;
344 } else {
345 len = put_dec(tmp, num) - tmp;
346 }
1ac101a5
KH
347
348 if (len > size)
349 return 0;
350 for (idx = 0; idx < len; ++idx)
351 buf[idx] = tmp[len - idx - 1];
133fd9f5 352 return len;
1ac101a5
KH
353}
354
51be17df 355#define SIGN 1 /* unsigned/signed, must be 1 */
d1c1b121 356#define LEFT 2 /* left justified */
1da177e4
LT
357#define PLUS 4 /* show plus */
358#define SPACE 8 /* space if plus */
d1c1b121 359#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
b89dc5d6
BH
360#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
361#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
1da177e4 362
fef20d9c
FW
363enum format_type {
364 FORMAT_TYPE_NONE, /* Just a string part */
ed681a91 365 FORMAT_TYPE_WIDTH,
fef20d9c
FW
366 FORMAT_TYPE_PRECISION,
367 FORMAT_TYPE_CHAR,
368 FORMAT_TYPE_STR,
369 FORMAT_TYPE_PTR,
370 FORMAT_TYPE_PERCENT_CHAR,
371 FORMAT_TYPE_INVALID,
372 FORMAT_TYPE_LONG_LONG,
373 FORMAT_TYPE_ULONG,
374 FORMAT_TYPE_LONG,
a4e94ef0
Z
375 FORMAT_TYPE_UBYTE,
376 FORMAT_TYPE_BYTE,
fef20d9c
FW
377 FORMAT_TYPE_USHORT,
378 FORMAT_TYPE_SHORT,
379 FORMAT_TYPE_UINT,
380 FORMAT_TYPE_INT,
fef20d9c
FW
381 FORMAT_TYPE_SIZE_T,
382 FORMAT_TYPE_PTRDIFF
383};
384
385struct printf_spec {
4e310fda 386 u8 type; /* format_type enum */
ef0658f3 387 u8 flags; /* flags to number() */
4e310fda
JP
388 u8 base; /* number base, 8, 10 or 16 only */
389 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
390 s16 field_width; /* width of output field */
391 s16 precision; /* # of digits/chars */
fef20d9c
FW
392};
393
cf3b429b
JP
394static noinline_for_stack
395char *number(char *buf, char *end, unsigned long long num,
396 struct printf_spec spec)
1da177e4 397{
7c43d9a3
RV
398 /* put_dec requires 2-byte alignment of the buffer. */
399 char tmp[3 * sizeof(num)] __aligned(2);
9b706aee
DV
400 char sign;
401 char locase;
fef20d9c 402 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
1da177e4 403 int i;
7c203422 404 bool is_zero = num == 0LL;
1da177e4 405
9b706aee
DV
406 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
407 * produces same digits or (maybe lowercased) letters */
fef20d9c
FW
408 locase = (spec.flags & SMALL);
409 if (spec.flags & LEFT)
410 spec.flags &= ~ZEROPAD;
1da177e4 411 sign = 0;
fef20d9c 412 if (spec.flags & SIGN) {
7b9186f5 413 if ((signed long long)num < 0) {
1da177e4 414 sign = '-';
7b9186f5 415 num = -(signed long long)num;
fef20d9c
FW
416 spec.field_width--;
417 } else if (spec.flags & PLUS) {
1da177e4 418 sign = '+';
fef20d9c
FW
419 spec.field_width--;
420 } else if (spec.flags & SPACE) {
1da177e4 421 sign = ' ';
fef20d9c 422 spec.field_width--;
1da177e4
LT
423 }
424 }
b39a7340 425 if (need_pfx) {
fef20d9c 426 if (spec.base == 16)
7c203422
PC
427 spec.field_width -= 2;
428 else if (!is_zero)
fef20d9c 429 spec.field_width--;
1da177e4 430 }
b39a7340
DV
431
432 /* generate full string in tmp[], in reverse order */
1da177e4 433 i = 0;
133fd9f5 434 if (num < spec.base)
3ea8d440 435 tmp[i++] = hex_asc_upper[num] | locase;
fef20d9c
FW
436 else if (spec.base != 10) { /* 8 or 16 */
437 int mask = spec.base - 1;
b39a7340 438 int shift = 3;
7b9186f5
AGR
439
440 if (spec.base == 16)
441 shift = 4;
b39a7340 442 do {
3ea8d440 443 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
b39a7340
DV
444 num >>= shift;
445 } while (num);
4277eedd
DV
446 } else { /* base 10 */
447 i = put_dec(tmp, num) - tmp;
448 }
b39a7340
DV
449
450 /* printing 100 using %2d gives "100", not "00" */
fef20d9c
FW
451 if (i > spec.precision)
452 spec.precision = i;
b39a7340 453 /* leading space padding */
fef20d9c 454 spec.field_width -= spec.precision;
51be17df 455 if (!(spec.flags & (ZEROPAD | LEFT))) {
7b9186f5 456 while (--spec.field_width >= 0) {
f796937a 457 if (buf < end)
1da177e4
LT
458 *buf = ' ';
459 ++buf;
460 }
461 }
b39a7340 462 /* sign */
1da177e4 463 if (sign) {
f796937a 464 if (buf < end)
1da177e4
LT
465 *buf = sign;
466 ++buf;
467 }
b39a7340
DV
468 /* "0x" / "0" prefix */
469 if (need_pfx) {
7c203422
PC
470 if (spec.base == 16 || !is_zero) {
471 if (buf < end)
472 *buf = '0';
473 ++buf;
474 }
fef20d9c 475 if (spec.base == 16) {
f796937a 476 if (buf < end)
9b706aee 477 *buf = ('X' | locase);
1da177e4
LT
478 ++buf;
479 }
480 }
b39a7340 481 /* zero or space padding */
fef20d9c 482 if (!(spec.flags & LEFT)) {
d1c1b121
RV
483 char c = ' ' + (spec.flags & ZEROPAD);
484 BUILD_BUG_ON(' ' + ZEROPAD != '0');
fef20d9c 485 while (--spec.field_width >= 0) {
f796937a 486 if (buf < end)
1da177e4
LT
487 *buf = c;
488 ++buf;
489 }
490 }
b39a7340 491 /* hmm even more zero padding? */
fef20d9c 492 while (i <= --spec.precision) {
f796937a 493 if (buf < end)
1da177e4
LT
494 *buf = '0';
495 ++buf;
496 }
b39a7340
DV
497 /* actual digits of result */
498 while (--i >= 0) {
f796937a 499 if (buf < end)
1da177e4
LT
500 *buf = tmp[i];
501 ++buf;
502 }
b39a7340 503 /* trailing space padding */
fef20d9c 504 while (--spec.field_width >= 0) {
f796937a 505 if (buf < end)
1da177e4
LT
506 *buf = ' ';
507 ++buf;
508 }
7b9186f5 509
1da177e4
LT
510 return buf;
511}
512
cfccde04 513static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
4b6ccca7
AV
514{
515 size_t size;
516 if (buf >= end) /* nowhere to put anything */
517 return;
518 size = end - buf;
519 if (size <= spaces) {
520 memset(buf, ' ', size);
521 return;
522 }
523 if (len) {
524 if (len > size - spaces)
525 len = size - spaces;
526 memmove(buf + spaces, buf, len);
527 }
528 memset(buf, ' ', spaces);
529}
530
cfccde04
RV
531/*
532 * Handle field width padding for a string.
533 * @buf: current buffer position
534 * @n: length of string
535 * @end: end of output buffer
536 * @spec: for field width and flags
537 * Returns: new buffer position after padding.
538 */
539static noinline_for_stack
540char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
541{
542 unsigned spaces;
543
544 if (likely(n >= spec.field_width))
545 return buf;
546 /* we want to pad the sucker */
547 spaces = spec.field_width - n;
548 if (!(spec.flags & LEFT)) {
549 move_right(buf - n, end, n, spaces);
550 return buf + spaces;
551 }
552 while (spaces--) {
553 if (buf < end)
554 *buf = ' ';
555 ++buf;
556 }
557 return buf;
558}
559
95508cfa
RV
560static noinline_for_stack
561char *string(char *buf, char *end, const char *s, struct printf_spec spec)
562{
34fc8b90
RV
563 int len = 0;
564 size_t lim = spec.precision;
95508cfa
RV
565
566 if ((unsigned long)s < PAGE_SIZE)
567 s = "(null)";
568
34fc8b90
RV
569 while (lim--) {
570 char c = *s++;
571 if (!c)
572 break;
95508cfa 573 if (buf < end)
34fc8b90 574 *buf = c;
95508cfa 575 ++buf;
34fc8b90 576 ++len;
95508cfa 577 }
34fc8b90 578 return widen_string(buf, len, end, spec);
95508cfa
RV
579}
580
4b6ccca7
AV
581static noinline_for_stack
582char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
583 const char *fmt)
584{
585 const char *array[4], *s;
586 const struct dentry *p;
587 int depth;
588 int i, n;
589
590 switch (fmt[1]) {
591 case '2': case '3': case '4':
592 depth = fmt[1] - '0';
593 break;
594 default:
595 depth = 1;
596 }
597
598 rcu_read_lock();
599 for (i = 0; i < depth; i++, d = p) {
600 p = ACCESS_ONCE(d->d_parent);
601 array[i] = ACCESS_ONCE(d->d_name.name);
602 if (p == d) {
603 if (i)
604 array[i] = "";
605 i++;
606 break;
607 }
608 }
609 s = array[--i];
610 for (n = 0; n != spec.precision; n++, buf++) {
611 char c = *s++;
612 if (!c) {
613 if (!i)
614 break;
615 c = '/';
616 s = array[--i];
617 }
618 if (buf < end)
619 *buf = c;
620 }
621 rcu_read_unlock();
cfccde04 622 return widen_string(buf, n, end, spec);
4b6ccca7
AV
623}
624
1031bc58
DM
625#ifdef CONFIG_BLOCK
626static noinline_for_stack
627char *bdev_name(char *buf, char *end, struct block_device *bdev,
628 struct printf_spec spec, const char *fmt)
629{
630 struct gendisk *hd = bdev->bd_disk;
631
632 buf = string(buf, end, hd->disk_name, spec);
633 if (bdev->bd_part->partno) {
634 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
635 if (buf < end)
636 *buf = 'p';
637 buf++;
638 }
639 buf = number(buf, end, bdev->bd_part->partno, spec);
640 }
641 return buf;
642}
643#endif
644
cf3b429b
JP
645static noinline_for_stack
646char *symbol_string(char *buf, char *end, void *ptr,
b0d33c2b 647 struct printf_spec spec, const char *fmt)
0fe1ef24 648{
b0d33c2b 649 unsigned long value;
0fe1ef24
LT
650#ifdef CONFIG_KALLSYMS
651 char sym[KSYM_SYMBOL_LEN];
b0d33c2b
JP
652#endif
653
654 if (fmt[1] == 'R')
655 ptr = __builtin_extract_return_addr(ptr);
656 value = (unsigned long)ptr;
657
658#ifdef CONFIG_KALLSYMS
659 if (*fmt == 'B')
0f77a8d3 660 sprint_backtrace(sym, value);
b0d33c2b 661 else if (*fmt != 'f' && *fmt != 's')
0c8b946e
FW
662 sprint_symbol(sym, value);
663 else
4796dd20 664 sprint_symbol_no_offset(sym, value);
7b9186f5 665
fef20d9c 666 return string(buf, end, sym, spec);
0fe1ef24 667#else
7b9186f5 668 spec.field_width = 2 * sizeof(void *);
fef20d9c
FW
669 spec.flags |= SPECIAL | SMALL | ZEROPAD;
670 spec.base = 16;
7b9186f5 671
fef20d9c 672 return number(buf, end, value, spec);
0fe1ef24
LT
673#endif
674}
675
cf3b429b
JP
676static noinline_for_stack
677char *resource_string(char *buf, char *end, struct resource *res,
678 struct printf_spec spec, const char *fmt)
332d2e78
LT
679{
680#ifndef IO_RSRC_PRINTK_SIZE
28405372 681#define IO_RSRC_PRINTK_SIZE 6
332d2e78
LT
682#endif
683
684#ifndef MEM_RSRC_PRINTK_SIZE
28405372 685#define MEM_RSRC_PRINTK_SIZE 10
332d2e78 686#endif
4da0b66c 687 static const struct printf_spec io_spec = {
fef20d9c 688 .base = 16,
4da0b66c 689 .field_width = IO_RSRC_PRINTK_SIZE,
fef20d9c
FW
690 .precision = -1,
691 .flags = SPECIAL | SMALL | ZEROPAD,
692 };
4da0b66c
BH
693 static const struct printf_spec mem_spec = {
694 .base = 16,
695 .field_width = MEM_RSRC_PRINTK_SIZE,
696 .precision = -1,
697 .flags = SPECIAL | SMALL | ZEROPAD,
698 };
0f4050c7
BH
699 static const struct printf_spec bus_spec = {
700 .base = 16,
701 .field_width = 2,
702 .precision = -1,
703 .flags = SMALL | ZEROPAD,
704 };
4da0b66c 705 static const struct printf_spec dec_spec = {
c91d3376
BH
706 .base = 10,
707 .precision = -1,
708 .flags = 0,
709 };
4da0b66c 710 static const struct printf_spec str_spec = {
fd95541e
BH
711 .field_width = -1,
712 .precision = 10,
713 .flags = LEFT,
714 };
4da0b66c 715 static const struct printf_spec flag_spec = {
fd95541e
BH
716 .base = 16,
717 .precision = -1,
718 .flags = SPECIAL | SMALL,
719 };
c7dabef8
BH
720
721 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
722 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
723#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
724#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
9d7cca04 725#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
c7dabef8
BH
726#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
727 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
728 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
729
332d2e78 730 char *p = sym, *pend = sym + sizeof(sym);
c7dabef8 731 int decode = (fmt[0] == 'R') ? 1 : 0;
4da0b66c 732 const struct printf_spec *specp;
332d2e78
LT
733
734 *p++ = '[';
4da0b66c 735 if (res->flags & IORESOURCE_IO) {
c7dabef8 736 p = string(p, pend, "io ", str_spec);
4da0b66c
BH
737 specp = &io_spec;
738 } else if (res->flags & IORESOURCE_MEM) {
c7dabef8 739 p = string(p, pend, "mem ", str_spec);
4da0b66c
BH
740 specp = &mem_spec;
741 } else if (res->flags & IORESOURCE_IRQ) {
c7dabef8 742 p = string(p, pend, "irq ", str_spec);
4da0b66c
BH
743 specp = &dec_spec;
744 } else if (res->flags & IORESOURCE_DMA) {
c7dabef8 745 p = string(p, pend, "dma ", str_spec);
4da0b66c 746 specp = &dec_spec;
0f4050c7
BH
747 } else if (res->flags & IORESOURCE_BUS) {
748 p = string(p, pend, "bus ", str_spec);
749 specp = &bus_spec;
4da0b66c 750 } else {
c7dabef8 751 p = string(p, pend, "??? ", str_spec);
4da0b66c 752 specp = &mem_spec;
c7dabef8 753 decode = 0;
fd95541e 754 }
d19cb803
BH
755 if (decode && res->flags & IORESOURCE_UNSET) {
756 p = string(p, pend, "size ", str_spec);
757 p = number(p, pend, resource_size(res), *specp);
758 } else {
759 p = number(p, pend, res->start, *specp);
760 if (res->start != res->end) {
761 *p++ = '-';
762 p = number(p, pend, res->end, *specp);
763 }
c91d3376 764 }
c7dabef8 765 if (decode) {
fd95541e
BH
766 if (res->flags & IORESOURCE_MEM_64)
767 p = string(p, pend, " 64bit", str_spec);
768 if (res->flags & IORESOURCE_PREFETCH)
769 p = string(p, pend, " pref", str_spec);
9d7cca04
BH
770 if (res->flags & IORESOURCE_WINDOW)
771 p = string(p, pend, " window", str_spec);
fd95541e
BH
772 if (res->flags & IORESOURCE_DISABLED)
773 p = string(p, pend, " disabled", str_spec);
c7dabef8
BH
774 } else {
775 p = string(p, pend, " flags ", str_spec);
776 p = number(p, pend, res->flags, flag_spec);
fd95541e 777 }
332d2e78 778 *p++ = ']';
c7dabef8 779 *p = '\0';
332d2e78 780
fef20d9c 781 return string(buf, end, sym, spec);
332d2e78
LT
782}
783
31550a16
AS
784static noinline_for_stack
785char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
786 const char *fmt)
787{
360603a1 788 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
31550a16
AS
789 negative value, fallback to the default */
790 char separator;
791
792 if (spec.field_width == 0)
793 /* nothing to print */
794 return buf;
795
796 if (ZERO_OR_NULL_PTR(addr))
797 /* NULL pointer */
798 return string(buf, end, NULL, spec);
799
800 switch (fmt[1]) {
801 case 'C':
802 separator = ':';
803 break;
804 case 'D':
805 separator = '-';
806 break;
807 case 'N':
808 separator = 0;
809 break;
810 default:
811 separator = ' ';
812 break;
813 }
814
815 if (spec.field_width > 0)
816 len = min_t(int, spec.field_width, 64);
817
9c98f235
RV
818 for (i = 0; i < len; ++i) {
819 if (buf < end)
820 *buf = hex_asc_hi(addr[i]);
821 ++buf;
822 if (buf < end)
823 *buf = hex_asc_lo(addr[i]);
824 ++buf;
31550a16 825
9c98f235
RV
826 if (separator && i != len - 1) {
827 if (buf < end)
828 *buf = separator;
829 ++buf;
830 }
31550a16
AS
831 }
832
833 return buf;
834}
835
dbc760bc
TH
836static noinline_for_stack
837char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
838 struct printf_spec spec, const char *fmt)
839{
840 const int CHUNKSZ = 32;
841 int nr_bits = max_t(int, spec.field_width, 0);
842 int i, chunksz;
843 bool first = true;
844
845 /* reused to print numbers */
846 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
847
848 chunksz = nr_bits & (CHUNKSZ - 1);
849 if (chunksz == 0)
850 chunksz = CHUNKSZ;
851
852 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
853 for (; i >= 0; i -= CHUNKSZ) {
854 u32 chunkmask, val;
855 int word, bit;
856
857 chunkmask = ((1ULL << chunksz) - 1);
858 word = i / BITS_PER_LONG;
859 bit = i % BITS_PER_LONG;
860 val = (bitmap[word] >> bit) & chunkmask;
861
862 if (!first) {
863 if (buf < end)
864 *buf = ',';
865 buf++;
866 }
867 first = false;
868
869 spec.field_width = DIV_ROUND_UP(chunksz, 4);
870 buf = number(buf, end, val, spec);
871
872 chunksz = CHUNKSZ;
873 }
874 return buf;
875}
876
877static noinline_for_stack
878char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
879 struct printf_spec spec, const char *fmt)
880{
881 int nr_bits = max_t(int, spec.field_width, 0);
882 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
883 int cur, rbot, rtop;
884 bool first = true;
885
886 /* reused to print numbers */
887 spec = (struct printf_spec){ .base = 10 };
888
889 rbot = cur = find_first_bit(bitmap, nr_bits);
890 while (cur < nr_bits) {
891 rtop = cur;
892 cur = find_next_bit(bitmap, nr_bits, cur + 1);
893 if (cur < nr_bits && cur <= rtop + 1)
894 continue;
895
896 if (!first) {
897 if (buf < end)
898 *buf = ',';
899 buf++;
900 }
901 first = false;
902
903 buf = number(buf, end, rbot, spec);
904 if (rbot < rtop) {
905 if (buf < end)
906 *buf = '-';
907 buf++;
908
909 buf = number(buf, end, rtop, spec);
910 }
911
912 rbot = cur;
913 }
914 return buf;
915}
916
cf3b429b
JP
917static noinline_for_stack
918char *mac_address_string(char *buf, char *end, u8 *addr,
919 struct printf_spec spec, const char *fmt)
dd45c9cf 920{
8a27f7c9 921 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
dd45c9cf
HH
922 char *p = mac_addr;
923 int i;
bc7259a2 924 char separator;
76597ff9 925 bool reversed = false;
bc7259a2 926
76597ff9
AE
927 switch (fmt[1]) {
928 case 'F':
bc7259a2 929 separator = '-';
76597ff9
AE
930 break;
931
932 case 'R':
933 reversed = true;
934 /* fall through */
935
936 default:
bc7259a2 937 separator = ':';
76597ff9 938 break;
bc7259a2 939 }
dd45c9cf
HH
940
941 for (i = 0; i < 6; i++) {
76597ff9
AE
942 if (reversed)
943 p = hex_byte_pack(p, addr[5 - i]);
944 else
945 p = hex_byte_pack(p, addr[i]);
946
8a27f7c9 947 if (fmt[0] == 'M' && i != 5)
bc7259a2 948 *p++ = separator;
dd45c9cf
HH
949 }
950 *p = '\0';
951
fef20d9c 952 return string(buf, end, mac_addr, spec);
dd45c9cf
HH
953}
954
cf3b429b
JP
955static noinline_for_stack
956char *ip4_string(char *p, const u8 *addr, const char *fmt)
8a27f7c9
JP
957{
958 int i;
0159f24e
JP
959 bool leading_zeros = (fmt[0] == 'i');
960 int index;
961 int step;
962
963 switch (fmt[2]) {
964 case 'h':
965#ifdef __BIG_ENDIAN
966 index = 0;
967 step = 1;
968#else
969 index = 3;
970 step = -1;
971#endif
972 break;
973 case 'l':
974 index = 3;
975 step = -1;
976 break;
977 case 'n':
978 case 'b':
979 default:
980 index = 0;
981 step = 1;
982 break;
983 }
8a27f7c9 984 for (i = 0; i < 4; i++) {
7c43d9a3 985 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
133fd9f5 986 int digits = put_dec_trunc8(temp, addr[index]) - temp;
8a27f7c9
JP
987 if (leading_zeros) {
988 if (digits < 3)
989 *p++ = '0';
990 if (digits < 2)
991 *p++ = '0';
992 }
993 /* reverse the digits in the quad */
994 while (digits--)
995 *p++ = temp[digits];
996 if (i < 3)
997 *p++ = '.';
0159f24e 998 index += step;
8a27f7c9 999 }
8a27f7c9 1000 *p = '\0';
7b9186f5 1001
8a27f7c9
JP
1002 return p;
1003}
1004
cf3b429b
JP
1005static noinline_for_stack
1006char *ip6_compressed_string(char *p, const char *addr)
689afa7d 1007{
7b9186f5 1008 int i, j, range;
8a27f7c9
JP
1009 unsigned char zerolength[8];
1010 int longest = 1;
1011 int colonpos = -1;
1012 u16 word;
7b9186f5 1013 u8 hi, lo;
8a27f7c9 1014 bool needcolon = false;
eb78cd26
JP
1015 bool useIPv4;
1016 struct in6_addr in6;
1017
1018 memcpy(&in6, addr, sizeof(struct in6_addr));
1019
1020 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
8a27f7c9
JP
1021
1022 memset(zerolength, 0, sizeof(zerolength));
1023
1024 if (useIPv4)
1025 range = 6;
1026 else
1027 range = 8;
1028
1029 /* find position of longest 0 run */
1030 for (i = 0; i < range; i++) {
1031 for (j = i; j < range; j++) {
eb78cd26 1032 if (in6.s6_addr16[j] != 0)
8a27f7c9
JP
1033 break;
1034 zerolength[i]++;
1035 }
1036 }
1037 for (i = 0; i < range; i++) {
1038 if (zerolength[i] > longest) {
1039 longest = zerolength[i];
1040 colonpos = i;
1041 }
1042 }
29cf519e
JP
1043 if (longest == 1) /* don't compress a single 0 */
1044 colonpos = -1;
689afa7d 1045
8a27f7c9
JP
1046 /* emit address */
1047 for (i = 0; i < range; i++) {
1048 if (i == colonpos) {
1049 if (needcolon || i == 0)
1050 *p++ = ':';
1051 *p++ = ':';
1052 needcolon = false;
1053 i += longest - 1;
1054 continue;
1055 }
1056 if (needcolon) {
1057 *p++ = ':';
1058 needcolon = false;
1059 }
1060 /* hex u16 without leading 0s */
eb78cd26 1061 word = ntohs(in6.s6_addr16[i]);
8a27f7c9
JP
1062 hi = word >> 8;
1063 lo = word & 0xff;
1064 if (hi) {
1065 if (hi > 0x0f)
55036ba7 1066 p = hex_byte_pack(p, hi);
8a27f7c9
JP
1067 else
1068 *p++ = hex_asc_lo(hi);
55036ba7 1069 p = hex_byte_pack(p, lo);
8a27f7c9 1070 }
b5ff992b 1071 else if (lo > 0x0f)
55036ba7 1072 p = hex_byte_pack(p, lo);
8a27f7c9
JP
1073 else
1074 *p++ = hex_asc_lo(lo);
1075 needcolon = true;
1076 }
1077
1078 if (useIPv4) {
1079 if (needcolon)
1080 *p++ = ':';
0159f24e 1081 p = ip4_string(p, &in6.s6_addr[12], "I4");
8a27f7c9 1082 }
8a27f7c9 1083 *p = '\0';
7b9186f5 1084
8a27f7c9
JP
1085 return p;
1086}
1087
cf3b429b
JP
1088static noinline_for_stack
1089char *ip6_string(char *p, const char *addr, const char *fmt)
8a27f7c9
JP
1090{
1091 int i;
7b9186f5 1092
689afa7d 1093 for (i = 0; i < 8; i++) {
55036ba7
AS
1094 p = hex_byte_pack(p, *addr++);
1095 p = hex_byte_pack(p, *addr++);
8a27f7c9 1096 if (fmt[0] == 'I' && i != 7)
689afa7d
HH
1097 *p++ = ':';
1098 }
1099 *p = '\0';
7b9186f5 1100
8a27f7c9
JP
1101 return p;
1102}
1103
cf3b429b
JP
1104static noinline_for_stack
1105char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1106 struct printf_spec spec, const char *fmt)
8a27f7c9
JP
1107{
1108 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1109
1110 if (fmt[0] == 'I' && fmt[2] == 'c')
eb78cd26 1111 ip6_compressed_string(ip6_addr, addr);
8a27f7c9 1112 else
eb78cd26 1113 ip6_string(ip6_addr, addr, fmt);
689afa7d 1114
fef20d9c 1115 return string(buf, end, ip6_addr, spec);
689afa7d
HH
1116}
1117
cf3b429b
JP
1118static noinline_for_stack
1119char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1120 struct printf_spec spec, const char *fmt)
4aa99606 1121{
8a27f7c9 1122 char ip4_addr[sizeof("255.255.255.255")];
4aa99606 1123
0159f24e 1124 ip4_string(ip4_addr, addr, fmt);
4aa99606 1125
fef20d9c 1126 return string(buf, end, ip4_addr, spec);
4aa99606
HH
1127}
1128
10679643
DB
1129static noinline_for_stack
1130char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1131 struct printf_spec spec, const char *fmt)
1132{
1133 bool have_p = false, have_s = false, have_f = false, have_c = false;
1134 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1135 sizeof(":12345") + sizeof("/123456789") +
1136 sizeof("%1234567890")];
1137 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1138 const u8 *addr = (const u8 *) &sa->sin6_addr;
1139 char fmt6[2] = { fmt[0], '6' };
1140 u8 off = 0;
1141
1142 fmt++;
1143 while (isalpha(*++fmt)) {
1144 switch (*fmt) {
1145 case 'p':
1146 have_p = true;
1147 break;
1148 case 'f':
1149 have_f = true;
1150 break;
1151 case 's':
1152 have_s = true;
1153 break;
1154 case 'c':
1155 have_c = true;
1156 break;
1157 }
1158 }
1159
1160 if (have_p || have_s || have_f) {
1161 *p = '[';
1162 off = 1;
1163 }
1164
1165 if (fmt6[0] == 'I' && have_c)
1166 p = ip6_compressed_string(ip6_addr + off, addr);
1167 else
1168 p = ip6_string(ip6_addr + off, addr, fmt6);
1169
1170 if (have_p || have_s || have_f)
1171 *p++ = ']';
1172
1173 if (have_p) {
1174 *p++ = ':';
1175 p = number(p, pend, ntohs(sa->sin6_port), spec);
1176 }
1177 if (have_f) {
1178 *p++ = '/';
1179 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1180 IPV6_FLOWINFO_MASK), spec);
1181 }
1182 if (have_s) {
1183 *p++ = '%';
1184 p = number(p, pend, sa->sin6_scope_id, spec);
1185 }
1186 *p = '\0';
1187
1188 return string(buf, end, ip6_addr, spec);
1189}
1190
1191static noinline_for_stack
1192char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1193 struct printf_spec spec, const char *fmt)
1194{
1195 bool have_p = false;
1196 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1197 char *pend = ip4_addr + sizeof(ip4_addr);
1198 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1199 char fmt4[3] = { fmt[0], '4', 0 };
1200
1201 fmt++;
1202 while (isalpha(*++fmt)) {
1203 switch (*fmt) {
1204 case 'p':
1205 have_p = true;
1206 break;
1207 case 'h':
1208 case 'l':
1209 case 'n':
1210 case 'b':
1211 fmt4[2] = *fmt;
1212 break;
1213 }
1214 }
1215
1216 p = ip4_string(ip4_addr, addr, fmt4);
1217 if (have_p) {
1218 *p++ = ':';
1219 p = number(p, pend, ntohs(sa->sin_port), spec);
1220 }
1221 *p = '\0';
1222
1223 return string(buf, end, ip4_addr, spec);
1224}
1225
71dca95d
AS
1226static noinline_for_stack
1227char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1228 const char *fmt)
1229{
1230 bool found = true;
1231 int count = 1;
1232 unsigned int flags = 0;
1233 int len;
1234
1235 if (spec.field_width == 0)
1236 return buf; /* nothing to print */
1237
1238 if (ZERO_OR_NULL_PTR(addr))
1239 return string(buf, end, NULL, spec); /* NULL pointer */
1240
1241
1242 do {
1243 switch (fmt[count++]) {
1244 case 'a':
1245 flags |= ESCAPE_ANY;
1246 break;
1247 case 'c':
1248 flags |= ESCAPE_SPECIAL;
1249 break;
1250 case 'h':
1251 flags |= ESCAPE_HEX;
1252 break;
1253 case 'n':
1254 flags |= ESCAPE_NULL;
1255 break;
1256 case 'o':
1257 flags |= ESCAPE_OCTAL;
1258 break;
1259 case 'p':
1260 flags |= ESCAPE_NP;
1261 break;
1262 case 's':
1263 flags |= ESCAPE_SPACE;
1264 break;
1265 default:
1266 found = false;
1267 break;
1268 }
1269 } while (found);
1270
1271 if (!flags)
1272 flags = ESCAPE_ANY_NP;
1273
1274 len = spec.field_width < 0 ? 1 : spec.field_width;
1275
41416f23
RV
1276 /*
1277 * string_escape_mem() writes as many characters as it can to
1278 * the given buffer, and returns the total size of the output
1279 * had the buffer been big enough.
1280 */
1281 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
71dca95d
AS
1282
1283 return buf;
1284}
1285
cf3b429b
JP
1286static noinline_for_stack
1287char *uuid_string(char *buf, char *end, const u8 *addr,
1288 struct printf_spec spec, const char *fmt)
9ac6e44e
JP
1289{
1290 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
1291 char *p = uuid;
1292 int i;
1293 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
1294 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
1295 const u8 *index = be;
1296 bool uc = false;
1297
1298 switch (*(++fmt)) {
1299 case 'L':
1300 uc = true; /* fall-through */
1301 case 'l':
1302 index = le;
1303 break;
1304 case 'B':
1305 uc = true;
1306 break;
1307 }
1308
1309 for (i = 0; i < 16; i++) {
55036ba7 1310 p = hex_byte_pack(p, addr[index[i]]);
9ac6e44e
JP
1311 switch (i) {
1312 case 3:
1313 case 5:
1314 case 7:
1315 case 9:
1316 *p++ = '-';
1317 break;
1318 }
1319 }
1320
1321 *p = 0;
1322
1323 if (uc) {
1324 p = uuid;
1325 do {
1326 *p = toupper(*p);
1327 } while (*(++p));
1328 }
1329
1330 return string(buf, end, uuid, spec);
1331}
1332
c8f44aff
MM
1333static
1334char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1335 struct printf_spec spec)
1336{
1337 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1338 if (spec.field_width == -1)
1339 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1340 spec.base = 16;
1341
1342 return number(buf, end, *(const netdev_features_t *)addr, spec);
1343}
1344
aaf07621
JP
1345static noinline_for_stack
1346char *address_val(char *buf, char *end, const void *addr,
1347 struct printf_spec spec, const char *fmt)
1348{
1349 unsigned long long num;
1350
1351 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1352 spec.base = 16;
1353
1354 switch (fmt[1]) {
1355 case 'd':
1356 num = *(const dma_addr_t *)addr;
1357 spec.field_width = sizeof(dma_addr_t) * 2 + 2;
1358 break;
1359 case 'p':
1360 default:
1361 num = *(const phys_addr_t *)addr;
1362 spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1363 break;
1364 }
1365
1366 return number(buf, end, num, spec);
1367}
1368
900cca29
GU
1369static noinline_for_stack
1370char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1371 const char *fmt)
1372{
1373 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1374 return string(buf, end, NULL, spec);
1375
1376 switch (fmt[1]) {
1377 case 'r':
1378 return number(buf, end, clk_get_rate(clk), spec);
1379
1380 case 'n':
1381 default:
1382#ifdef CONFIG_COMMON_CLK
1383 return string(buf, end, __clk_get_name(clk), spec);
1384#else
1385 spec.base = 16;
1386 spec.field_width = sizeof(unsigned long) * 2 + 2;
1387 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1388 return number(buf, end, (unsigned long)clk, spec);
1389#endif
1390 }
1391}
1392
411f05f1 1393int kptr_restrict __read_mostly;
455cd5ab 1394
4d8a743c
LT
1395/*
1396 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1397 * by an extra set of alphanumeric characters that are extended format
1398 * specifiers.
1399 *
332d2e78
LT
1400 * Right now we handle:
1401 *
0c8b946e
FW
1402 * - 'F' For symbolic function descriptor pointers with offset
1403 * - 'f' For simple symbolic function names without offset
0efb4d20
SR
1404 * - 'S' For symbolic direct pointers with offset
1405 * - 's' For symbolic direct pointers without offset
b0d33c2b 1406 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
0f77a8d3 1407 * - 'B' For backtraced symbolic direct pointers with offset
c7dabef8
BH
1408 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1409 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
dbc760bc
TH
1410 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1411 * width which must be explicitly specified either as part of the
1412 * format string '%32b[l]' or through '%*b[l]', [l] selects
1413 * range-list format instead of hex format
dd45c9cf
HH
1414 * - 'M' For a 6-byte MAC address, it prints the address in the
1415 * usual colon-separated hex notation
8a27f7c9 1416 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
bc7259a2 1417 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
c8e00060 1418 * with a dash-separated hex notation
7c59154e 1419 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
8a27f7c9
JP
1420 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1421 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1422 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
10679643
DB
1423 * [S][pfs]
1424 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1425 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
8a27f7c9
JP
1426 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1427 * IPv6 omits the colons (01020304...0f)
1428 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
10679643
DB
1429 * [S][pfs]
1430 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1431 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1432 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1433 * - 'I[6S]c' for IPv6 addresses printed as specified by
29cf519e 1434 * http://tools.ietf.org/html/rfc5952
71dca95d
AS
1435 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1436 * of the following flags (see string_escape_mem() for the
1437 * details):
1438 * a - ESCAPE_ANY
1439 * c - ESCAPE_SPECIAL
1440 * h - ESCAPE_HEX
1441 * n - ESCAPE_NULL
1442 * o - ESCAPE_OCTAL
1443 * p - ESCAPE_NP
1444 * s - ESCAPE_SPACE
1445 * By default ESCAPE_ANY_NP is used.
9ac6e44e
JP
1446 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1447 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1448 * Options for %pU are:
1449 * b big endian lower case hex (default)
1450 * B big endian UPPER case hex
1451 * l little endian lower case hex
1452 * L little endian UPPER case hex
1453 * big endian output byte order is:
1454 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1455 * little endian output byte order is:
1456 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
7db6f5fb
JP
1457 * - 'V' For a struct va_format which contains a format string * and va_list *,
1458 * call vsnprintf(->format, *->va_list).
1459 * Implements a "recursive vsnprintf".
1460 * Do not use this feature without some mechanism to verify the
1461 * correctness of the format string and va_list arguments.
455cd5ab 1462 * - 'K' For a kernel pointer that should be hidden from unprivileged users
c8f44aff 1463 * - 'NF' For a netdev_features_t
31550a16
AS
1464 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1465 * a certain separator (' ' by default):
1466 * C colon
1467 * D dash
1468 * N no separator
1469 * The maximum supported length is 64 bytes of the input. Consider
1470 * to use print_hex_dump() for the larger input.
aaf07621
JP
1471 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1472 * (default assumed to be phys_addr_t, passed by reference)
c0d92a57
OJ
1473 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1474 * - 'D[234]' Same as 'd' but for a struct file
1031bc58 1475 * - 'g' For block_device name (gendisk + partition number)
900cca29
GU
1476 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1477 * (legacy clock framework) of the clock
1478 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1479 * (legacy clock framework) of the clock
1480 * - 'Cr' For a clock, it prints the current rate of the clock
5e4ee7b1
MK
1481 *
1482 * ** Please update also Documentation/printk-formats.txt when making changes **
9ac6e44e 1483 *
332d2e78
LT
1484 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1485 * function pointers are really function descriptors, which contain a
1486 * pointer to the real address.
4d8a743c 1487 */
cf3b429b
JP
1488static noinline_for_stack
1489char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1490 struct printf_spec spec)
78a8bf69 1491{
80c9eb46 1492 const int default_width = 2 * sizeof(void *);
725fe002 1493
9f36e2c4 1494 if (!ptr && *fmt != 'K') {
5e057981
JP
1495 /*
1496 * Print (null) with the same width as a pointer so it makes
1497 * tabular output look nice.
1498 */
1499 if (spec.field_width == -1)
725fe002 1500 spec.field_width = default_width;
fef20d9c 1501 return string(buf, end, "(null)", spec);
5e057981 1502 }
d97106ab 1503
0fe1ef24
LT
1504 switch (*fmt) {
1505 case 'F':
0c8b946e 1506 case 'f':
0fe1ef24
LT
1507 ptr = dereference_function_descriptor(ptr);
1508 /* Fallthrough */
1509 case 'S':
9ac6e44e 1510 case 's':
0f77a8d3 1511 case 'B':
b0d33c2b 1512 return symbol_string(buf, end, ptr, spec, fmt);
332d2e78 1513 case 'R':
c7dabef8 1514 case 'r':
fd95541e 1515 return resource_string(buf, end, ptr, spec, fmt);
31550a16
AS
1516 case 'h':
1517 return hex_string(buf, end, ptr, spec, fmt);
dbc760bc
TH
1518 case 'b':
1519 switch (fmt[1]) {
1520 case 'l':
1521 return bitmap_list_string(buf, end, ptr, spec, fmt);
1522 default:
1523 return bitmap_string(buf, end, ptr, spec, fmt);
1524 }
8a27f7c9
JP
1525 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1526 case 'm': /* Contiguous: 000102030405 */
76597ff9
AE
1527 /* [mM]F (FDDI) */
1528 /* [mM]R (Reverse order; Bluetooth) */
8a27f7c9
JP
1529 return mac_address_string(buf, end, ptr, spec, fmt);
1530 case 'I': /* Formatted IP supported
1531 * 4: 1.2.3.4
1532 * 6: 0001:0203:...:0708
1533 * 6c: 1::708 or 1::1.2.3.4
1534 */
1535 case 'i': /* Contiguous:
1536 * 4: 001.002.003.004
1537 * 6: 000102...0f
1538 */
1539 switch (fmt[1]) {
1540 case '6':
1541 return ip6_addr_string(buf, end, ptr, spec, fmt);
1542 case '4':
1543 return ip4_addr_string(buf, end, ptr, spec, fmt);
10679643
DB
1544 case 'S': {
1545 const union {
1546 struct sockaddr raw;
1547 struct sockaddr_in v4;
1548 struct sockaddr_in6 v6;
1549 } *sa = ptr;
1550
1551 switch (sa->raw.sa_family) {
1552 case AF_INET:
1553 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1554 case AF_INET6:
1555 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1556 default:
1557 return string(buf, end, "(invalid address)", spec);
1558 }}
8a27f7c9 1559 }
fef20d9c 1560 break;
71dca95d
AS
1561 case 'E':
1562 return escaped_string(buf, end, ptr, spec, fmt);
9ac6e44e
JP
1563 case 'U':
1564 return uuid_string(buf, end, ptr, spec, fmt);
7db6f5fb 1565 case 'V':
5756b76e
JB
1566 {
1567 va_list va;
1568
1569 va_copy(va, *((struct va_format *)ptr)->va);
1570 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1571 ((struct va_format *)ptr)->fmt, va);
1572 va_end(va);
1573 return buf;
1574 }
455cd5ab
DR
1575 case 'K':
1576 /*
1577 * %pK cannot be used in IRQ context because its test
1578 * for CAP_SYSLOG would be meaningless.
1579 */
3715c530
DR
1580 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1581 in_nmi())) {
455cd5ab 1582 if (spec.field_width == -1)
725fe002 1583 spec.field_width = default_width;
455cd5ab 1584 return string(buf, end, "pK-error", spec);
455cd5ab 1585 }
312b4e22
RM
1586
1587 switch (kptr_restrict) {
1588 case 0:
1589 /* Always print %pK values */
1590 break;
1591 case 1: {
1592 /*
1593 * Only print the real pointer value if the current
1594 * process has CAP_SYSLOG and is running with the
1595 * same credentials it started with. This is because
1596 * access to files is checked at open() time, but %pK
1597 * checks permission at read() time. We don't want to
1598 * leak pointer values if a binary opens a file using
1599 * %pK and then elevates privileges before reading it.
1600 */
1601 const struct cred *cred = current_cred();
1602
1603 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1604 !uid_eq(cred->euid, cred->uid) ||
1605 !gid_eq(cred->egid, cred->gid))
1606 ptr = NULL;
1607 break;
1608 }
1609 case 2:
1610 default:
1611 /* Always print 0's for %pK */
26297607 1612 ptr = NULL;
312b4e22
RM
1613 break;
1614 }
26297607 1615 break;
312b4e22 1616
c8f44aff
MM
1617 case 'N':
1618 switch (fmt[1]) {
1619 case 'F':
1620 return netdev_feature_string(buf, end, ptr, spec);
1621 }
1622 break;
7d799210 1623 case 'a':
aaf07621 1624 return address_val(buf, end, ptr, spec, fmt);
4b6ccca7
AV
1625 case 'd':
1626 return dentry_name(buf, end, ptr, spec, fmt);
900cca29
GU
1627 case 'C':
1628 return clock(buf, end, ptr, spec, fmt);
4b6ccca7
AV
1629 case 'D':
1630 return dentry_name(buf, end,
1631 ((const struct file *)ptr)->f_path.dentry,
1632 spec, fmt);
1031bc58
DM
1633#ifdef CONFIG_BLOCK
1634 case 'g':
1635 return bdev_name(buf, end, ptr, spec, fmt);
1636#endif
1637
fef20d9c
FW
1638 }
1639 spec.flags |= SMALL;
1640 if (spec.field_width == -1) {
725fe002 1641 spec.field_width = default_width;
fef20d9c
FW
1642 spec.flags |= ZEROPAD;
1643 }
1644 spec.base = 16;
1645
1646 return number(buf, end, (unsigned long) ptr, spec);
1647}
1648
1649/*
1650 * Helper function to decode printf style format.
1651 * Each call decode a token from the format and return the
1652 * number of characters read (or likely the delta where it wants
1653 * to go on the next call).
1654 * The decoded token is returned through the parameters
1655 *
1656 * 'h', 'l', or 'L' for integer fields
1657 * 'z' support added 23/7/1999 S.H.
1658 * 'z' changed to 'Z' --davidm 1/25/99
1659 * 't' added for ptrdiff_t
1660 *
1661 * @fmt: the format string
1662 * @type of the token returned
1663 * @flags: various flags such as +, -, # tokens..
1664 * @field_width: overwritten width
1665 * @base: base of the number (octal, hex, ...)
1666 * @precision: precision of a number
1667 * @qualifier: qualifier of a number (long, size_t, ...)
1668 */
cf3b429b
JP
1669static noinline_for_stack
1670int format_decode(const char *fmt, struct printf_spec *spec)
fef20d9c
FW
1671{
1672 const char *start = fmt;
fef20d9c
FW
1673
1674 /* we finished early by reading the field width */
ed681a91 1675 if (spec->type == FORMAT_TYPE_WIDTH) {
fef20d9c
FW
1676 if (spec->field_width < 0) {
1677 spec->field_width = -spec->field_width;
1678 spec->flags |= LEFT;
1679 }
1680 spec->type = FORMAT_TYPE_NONE;
1681 goto precision;
1682 }
1683
1684 /* we finished early by reading the precision */
1685 if (spec->type == FORMAT_TYPE_PRECISION) {
1686 if (spec->precision < 0)
1687 spec->precision = 0;
1688
1689 spec->type = FORMAT_TYPE_NONE;
1690 goto qualifier;
1691 }
1692
1693 /* By default */
1694 spec->type = FORMAT_TYPE_NONE;
1695
1696 for (; *fmt ; ++fmt) {
1697 if (*fmt == '%')
1698 break;
1699 }
1700
1701 /* Return the current non-format string */
1702 if (fmt != start || !*fmt)
1703 return fmt - start;
1704
1705 /* Process flags */
1706 spec->flags = 0;
1707
1708 while (1) { /* this also skips first '%' */
1709 bool found = true;
1710
1711 ++fmt;
1712
1713 switch (*fmt) {
1714 case '-': spec->flags |= LEFT; break;
1715 case '+': spec->flags |= PLUS; break;
1716 case ' ': spec->flags |= SPACE; break;
1717 case '#': spec->flags |= SPECIAL; break;
1718 case '0': spec->flags |= ZEROPAD; break;
1719 default: found = false;
1720 }
1721
1722 if (!found)
1723 break;
1724 }
1725
1726 /* get field width */
1727 spec->field_width = -1;
1728
1729 if (isdigit(*fmt))
1730 spec->field_width = skip_atoi(&fmt);
1731 else if (*fmt == '*') {
1732 /* it's the next argument */
ed681a91 1733 spec->type = FORMAT_TYPE_WIDTH;
fef20d9c
FW
1734 return ++fmt - start;
1735 }
1736
1737precision:
1738 /* get the precision */
1739 spec->precision = -1;
1740 if (*fmt == '.') {
1741 ++fmt;
1742 if (isdigit(*fmt)) {
1743 spec->precision = skip_atoi(&fmt);
1744 if (spec->precision < 0)
1745 spec->precision = 0;
1746 } else if (*fmt == '*') {
1747 /* it's the next argument */
adf26f84 1748 spec->type = FORMAT_TYPE_PRECISION;
fef20d9c
FW
1749 return ++fmt - start;
1750 }
1751 }
1752
1753qualifier:
1754 /* get the conversion qualifier */
1755 spec->qualifier = -1;
75fb8f26
AS
1756 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1757 _tolower(*fmt) == 'z' || *fmt == 't') {
a4e94ef0
Z
1758 spec->qualifier = *fmt++;
1759 if (unlikely(spec->qualifier == *fmt)) {
1760 if (spec->qualifier == 'l') {
1761 spec->qualifier = 'L';
1762 ++fmt;
1763 } else if (spec->qualifier == 'h') {
1764 spec->qualifier = 'H';
1765 ++fmt;
1766 }
fef20d9c
FW
1767 }
1768 }
1769
1770 /* default base */
1771 spec->base = 10;
1772 switch (*fmt) {
1773 case 'c':
1774 spec->type = FORMAT_TYPE_CHAR;
1775 return ++fmt - start;
1776
1777 case 's':
1778 spec->type = FORMAT_TYPE_STR;
1779 return ++fmt - start;
1780
1781 case 'p':
1782 spec->type = FORMAT_TYPE_PTR;
ffbfed03 1783 return ++fmt - start;
fef20d9c 1784
fef20d9c
FW
1785 case '%':
1786 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1787 return ++fmt - start;
1788
1789 /* integer number formats - set up the flags and "break" */
1790 case 'o':
1791 spec->base = 8;
1792 break;
1793
1794 case 'x':
1795 spec->flags |= SMALL;
1796
1797 case 'X':
1798 spec->base = 16;
1799 break;
1800
1801 case 'd':
1802 case 'i':
39e874f8 1803 spec->flags |= SIGN;
fef20d9c 1804 case 'u':
4aa99606 1805 break;
fef20d9c 1806
708d96fd
RM
1807 case 'n':
1808 /*
b006f19b
RV
1809 * Since %n poses a greater security risk than
1810 * utility, treat it as any other invalid or
1811 * unsupported format specifier.
708d96fd 1812 */
708d96fd
RM
1813 /* Fall-through */
1814
fef20d9c 1815 default:
b006f19b 1816 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
fef20d9c
FW
1817 spec->type = FORMAT_TYPE_INVALID;
1818 return fmt - start;
0fe1ef24 1819 }
fef20d9c
FW
1820
1821 if (spec->qualifier == 'L')
1822 spec->type = FORMAT_TYPE_LONG_LONG;
1823 else if (spec->qualifier == 'l') {
51be17df
RV
1824 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
1825 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
75fb8f26 1826 } else if (_tolower(spec->qualifier) == 'z') {
fef20d9c
FW
1827 spec->type = FORMAT_TYPE_SIZE_T;
1828 } else if (spec->qualifier == 't') {
1829 spec->type = FORMAT_TYPE_PTRDIFF;
a4e94ef0 1830 } else if (spec->qualifier == 'H') {
51be17df
RV
1831 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
1832 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
fef20d9c 1833 } else if (spec->qualifier == 'h') {
51be17df
RV
1834 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
1835 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
fef20d9c 1836 } else {
51be17df
RV
1837 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
1838 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
78a8bf69 1839 }
fef20d9c
FW
1840
1841 return ++fmt - start;
78a8bf69
LT
1842}
1843
1da177e4
LT
1844/**
1845 * vsnprintf - Format a string and place it in a buffer
1846 * @buf: The buffer to place the result into
1847 * @size: The size of the buffer, including the trailing null space
1848 * @fmt: The format string to use
1849 * @args: Arguments for the format string
1850 *
d7ec9a05
RV
1851 * This function generally follows C99 vsnprintf, but has some
1852 * extensions and a few limitations:
1853 *
1854 * %n is unsupported
5e4ee7b1
MK
1855 * %p* is handled by pointer()
1856 *
1857 * See pointer() or Documentation/printk-formats.txt for more
1858 * extensive description.
20036fdc 1859 *
5e4ee7b1 1860 * ** Please update the documentation in both places when making changes **
80f548e0 1861 *
1da177e4
LT
1862 * The return value is the number of characters which would
1863 * be generated for the given input, excluding the trailing
1864 * '\0', as per ISO C99. If you want to have the exact
1865 * number of characters written into @buf as return value
72fd4a35 1866 * (not including the trailing '\0'), use vscnprintf(). If the
1da177e4
LT
1867 * return is greater than or equal to @size, the resulting
1868 * string is truncated.
1869 *
ba1835eb 1870 * If you're not already dealing with a va_list consider using snprintf().
1da177e4
LT
1871 */
1872int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1873{
1da177e4 1874 unsigned long long num;
d4be151b 1875 char *str, *end;
fef20d9c 1876 struct printf_spec spec = {0};
1da177e4 1877
f796937a
JF
1878 /* Reject out-of-range values early. Large positive sizes are
1879 used for unknown buffer sizes. */
2aa2f9e2 1880 if (WARN_ON_ONCE(size > INT_MAX))
1da177e4 1881 return 0;
1da177e4
LT
1882
1883 str = buf;
f796937a 1884 end = buf + size;
1da177e4 1885
f796937a
JF
1886 /* Make sure end is always >= buf */
1887 if (end < buf) {
1888 end = ((void *)-1);
1889 size = end - buf;
1da177e4
LT
1890 }
1891
fef20d9c
FW
1892 while (*fmt) {
1893 const char *old_fmt = fmt;
d4be151b 1894 int read = format_decode(fmt, &spec);
1da177e4 1895
fef20d9c 1896 fmt += read;
1da177e4 1897
fef20d9c
FW
1898 switch (spec.type) {
1899 case FORMAT_TYPE_NONE: {
1900 int copy = read;
1901 if (str < end) {
1902 if (copy > end - str)
1903 copy = end - str;
1904 memcpy(str, old_fmt, copy);
1da177e4 1905 }
fef20d9c
FW
1906 str += read;
1907 break;
1da177e4
LT
1908 }
1909
ed681a91 1910 case FORMAT_TYPE_WIDTH:
fef20d9c
FW
1911 spec.field_width = va_arg(args, int);
1912 break;
1da177e4 1913
fef20d9c
FW
1914 case FORMAT_TYPE_PRECISION:
1915 spec.precision = va_arg(args, int);
1916 break;
1da177e4 1917
d4be151b
AGR
1918 case FORMAT_TYPE_CHAR: {
1919 char c;
1920
fef20d9c
FW
1921 if (!(spec.flags & LEFT)) {
1922 while (--spec.field_width > 0) {
f796937a 1923 if (str < end)
1da177e4
LT
1924 *str = ' ';
1925 ++str;
1da177e4 1926
fef20d9c
FW
1927 }
1928 }
1929 c = (unsigned char) va_arg(args, int);
1930 if (str < end)
1931 *str = c;
1932 ++str;
1933 while (--spec.field_width > 0) {
f796937a 1934 if (str < end)
fef20d9c 1935 *str = ' ';
1da177e4 1936 ++str;
fef20d9c
FW
1937 }
1938 break;
d4be151b 1939 }
1da177e4 1940
fef20d9c
FW
1941 case FORMAT_TYPE_STR:
1942 str = string(str, end, va_arg(args, char *), spec);
1943 break;
1da177e4 1944
fef20d9c 1945 case FORMAT_TYPE_PTR:
ffbfed03 1946 str = pointer(fmt, str, end, va_arg(args, void *),
fef20d9c
FW
1947 spec);
1948 while (isalnum(*fmt))
1949 fmt++;
1950 break;
1da177e4 1951
fef20d9c
FW
1952 case FORMAT_TYPE_PERCENT_CHAR:
1953 if (str < end)
1954 *str = '%';
1955 ++str;
1956 break;
1da177e4 1957
fef20d9c 1958 case FORMAT_TYPE_INVALID:
b006f19b
RV
1959 /*
1960 * Presumably the arguments passed gcc's type
1961 * checking, but there is no safe or sane way
1962 * for us to continue parsing the format and
1963 * fetching from the va_list; the remaining
1964 * specifiers and arguments would be out of
1965 * sync.
1966 */
1967 goto out;
fef20d9c 1968
fef20d9c
FW
1969 default:
1970 switch (spec.type) {
1971 case FORMAT_TYPE_LONG_LONG:
1972 num = va_arg(args, long long);
1973 break;
1974 case FORMAT_TYPE_ULONG:
1975 num = va_arg(args, unsigned long);
1976 break;
1977 case FORMAT_TYPE_LONG:
1978 num = va_arg(args, long);
1979 break;
1980 case FORMAT_TYPE_SIZE_T:
ef124960
JG
1981 if (spec.flags & SIGN)
1982 num = va_arg(args, ssize_t);
1983 else
1984 num = va_arg(args, size_t);
fef20d9c
FW
1985 break;
1986 case FORMAT_TYPE_PTRDIFF:
1987 num = va_arg(args, ptrdiff_t);
1988 break;
a4e94ef0
Z
1989 case FORMAT_TYPE_UBYTE:
1990 num = (unsigned char) va_arg(args, int);
1991 break;
1992 case FORMAT_TYPE_BYTE:
1993 num = (signed char) va_arg(args, int);
1994 break;
fef20d9c
FW
1995 case FORMAT_TYPE_USHORT:
1996 num = (unsigned short) va_arg(args, int);
1997 break;
1998 case FORMAT_TYPE_SHORT:
1999 num = (short) va_arg(args, int);
2000 break;
39e874f8
FW
2001 case FORMAT_TYPE_INT:
2002 num = (int) va_arg(args, int);
fef20d9c
FW
2003 break;
2004 default:
2005 num = va_arg(args, unsigned int);
2006 }
2007
2008 str = number(str, end, num, spec);
1da177e4 2009 }
1da177e4 2010 }
fef20d9c 2011
b006f19b 2012out:
f796937a
JF
2013 if (size > 0) {
2014 if (str < end)
2015 *str = '\0';
2016 else
0a6047ee 2017 end[-1] = '\0';
f796937a 2018 }
fef20d9c 2019
f796937a 2020 /* the trailing null byte doesn't count towards the total */
1da177e4 2021 return str-buf;
fef20d9c 2022
1da177e4 2023}
1da177e4
LT
2024EXPORT_SYMBOL(vsnprintf);
2025
2026/**
2027 * vscnprintf - Format a string and place it in a buffer
2028 * @buf: The buffer to place the result into
2029 * @size: The size of the buffer, including the trailing null space
2030 * @fmt: The format string to use
2031 * @args: Arguments for the format string
2032 *
2033 * The return value is the number of characters which have been written into
b921c69f 2034 * the @buf not including the trailing '\0'. If @size is == 0 the function
1da177e4
LT
2035 * returns 0.
2036 *
ba1835eb 2037 * If you're not already dealing with a va_list consider using scnprintf().
20036fdc
AK
2038 *
2039 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
2040 */
2041int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2042{
2043 int i;
2044
7b9186f5
AGR
2045 i = vsnprintf(buf, size, fmt, args);
2046
b921c69f
AA
2047 if (likely(i < size))
2048 return i;
2049 if (size != 0)
2050 return size - 1;
2051 return 0;
1da177e4 2052}
1da177e4
LT
2053EXPORT_SYMBOL(vscnprintf);
2054
2055/**
2056 * snprintf - Format a string and place it in a buffer
2057 * @buf: The buffer to place the result into
2058 * @size: The size of the buffer, including the trailing null space
2059 * @fmt: The format string to use
2060 * @...: Arguments for the format string
2061 *
2062 * The return value is the number of characters which would be
2063 * generated for the given input, excluding the trailing null,
2064 * as per ISO C99. If the return is greater than or equal to
2065 * @size, the resulting string is truncated.
20036fdc
AK
2066 *
2067 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4 2068 */
7b9186f5 2069int snprintf(char *buf, size_t size, const char *fmt, ...)
1da177e4
LT
2070{
2071 va_list args;
2072 int i;
2073
2074 va_start(args, fmt);
7b9186f5 2075 i = vsnprintf(buf, size, fmt, args);
1da177e4 2076 va_end(args);
7b9186f5 2077
1da177e4
LT
2078 return i;
2079}
1da177e4
LT
2080EXPORT_SYMBOL(snprintf);
2081
2082/**
2083 * scnprintf - Format a string and place it in a buffer
2084 * @buf: The buffer to place the result into
2085 * @size: The size of the buffer, including the trailing null space
2086 * @fmt: The format string to use
2087 * @...: Arguments for the format string
2088 *
2089 * The return value is the number of characters written into @buf not including
b903c0b8 2090 * the trailing '\0'. If @size is == 0 the function returns 0.
1da177e4
LT
2091 */
2092
7b9186f5 2093int scnprintf(char *buf, size_t size, const char *fmt, ...)
1da177e4
LT
2094{
2095 va_list args;
2096 int i;
2097
2098 va_start(args, fmt);
b921c69f 2099 i = vscnprintf(buf, size, fmt, args);
1da177e4 2100 va_end(args);
7b9186f5 2101
b921c69f 2102 return i;
1da177e4
LT
2103}
2104EXPORT_SYMBOL(scnprintf);
2105
2106/**
2107 * vsprintf - Format a string and place it in a buffer
2108 * @buf: The buffer to place the result into
2109 * @fmt: The format string to use
2110 * @args: Arguments for the format string
2111 *
2112 * The function returns the number of characters written
72fd4a35 2113 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1da177e4
LT
2114 * buffer overflows.
2115 *
ba1835eb 2116 * If you're not already dealing with a va_list consider using sprintf().
20036fdc
AK
2117 *
2118 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
2119 */
2120int vsprintf(char *buf, const char *fmt, va_list args)
2121{
2122 return vsnprintf(buf, INT_MAX, fmt, args);
2123}
1da177e4
LT
2124EXPORT_SYMBOL(vsprintf);
2125
2126/**
2127 * sprintf - Format a string and place it in a buffer
2128 * @buf: The buffer to place the result into
2129 * @fmt: The format string to use
2130 * @...: Arguments for the format string
2131 *
2132 * The function returns the number of characters written
72fd4a35 2133 * into @buf. Use snprintf() or scnprintf() in order to avoid
1da177e4 2134 * buffer overflows.
20036fdc
AK
2135 *
2136 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4 2137 */
7b9186f5 2138int sprintf(char *buf, const char *fmt, ...)
1da177e4
LT
2139{
2140 va_list args;
2141 int i;
2142
2143 va_start(args, fmt);
7b9186f5 2144 i = vsnprintf(buf, INT_MAX, fmt, args);
1da177e4 2145 va_end(args);
7b9186f5 2146
1da177e4
LT
2147 return i;
2148}
1da177e4
LT
2149EXPORT_SYMBOL(sprintf);
2150
4370aa4a
LJ
2151#ifdef CONFIG_BINARY_PRINTF
2152/*
2153 * bprintf service:
2154 * vbin_printf() - VA arguments to binary data
2155 * bstr_printf() - Binary data to text string
2156 */
2157
2158/**
2159 * vbin_printf - Parse a format string and place args' binary value in a buffer
2160 * @bin_buf: The buffer to place args' binary value
2161 * @size: The size of the buffer(by words(32bits), not characters)
2162 * @fmt: The format string to use
2163 * @args: Arguments for the format string
2164 *
2165 * The format follows C99 vsnprintf, except %n is ignored, and its argument
da3dae54 2166 * is skipped.
4370aa4a
LJ
2167 *
2168 * The return value is the number of words(32bits) which would be generated for
2169 * the given input.
2170 *
2171 * NOTE:
2172 * If the return value is greater than @size, the resulting bin_buf is NOT
2173 * valid for bstr_printf().
2174 */
2175int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2176{
fef20d9c 2177 struct printf_spec spec = {0};
4370aa4a 2178 char *str, *end;
4370aa4a
LJ
2179
2180 str = (char *)bin_buf;
2181 end = (char *)(bin_buf + size);
2182
2183#define save_arg(type) \
2184do { \
2185 if (sizeof(type) == 8) { \
2186 unsigned long long value; \
2187 str = PTR_ALIGN(str, sizeof(u32)); \
2188 value = va_arg(args, unsigned long long); \
2189 if (str + sizeof(type) <= end) { \
2190 *(u32 *)str = *(u32 *)&value; \
2191 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2192 } \
2193 } else { \
2194 unsigned long value; \
2195 str = PTR_ALIGN(str, sizeof(type)); \
2196 value = va_arg(args, int); \
2197 if (str + sizeof(type) <= end) \
2198 *(typeof(type) *)str = (type)value; \
2199 } \
2200 str += sizeof(type); \
2201} while (0)
2202
fef20d9c 2203 while (*fmt) {
d4be151b 2204 int read = format_decode(fmt, &spec);
4370aa4a 2205
fef20d9c 2206 fmt += read;
4370aa4a 2207
fef20d9c
FW
2208 switch (spec.type) {
2209 case FORMAT_TYPE_NONE:
d4be151b 2210 case FORMAT_TYPE_PERCENT_CHAR:
fef20d9c 2211 break;
b006f19b
RV
2212 case FORMAT_TYPE_INVALID:
2213 goto out;
fef20d9c 2214
ed681a91 2215 case FORMAT_TYPE_WIDTH:
fef20d9c
FW
2216 case FORMAT_TYPE_PRECISION:
2217 save_arg(int);
2218 break;
2219
2220 case FORMAT_TYPE_CHAR:
4370aa4a 2221 save_arg(char);
fef20d9c
FW
2222 break;
2223
2224 case FORMAT_TYPE_STR: {
4370aa4a
LJ
2225 const char *save_str = va_arg(args, char *);
2226 size_t len;
6c356634 2227
4370aa4a
LJ
2228 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2229 || (unsigned long)save_str < PAGE_SIZE)
0f4f81dc 2230 save_str = "(null)";
6c356634
AGR
2231 len = strlen(save_str) + 1;
2232 if (str + len < end)
2233 memcpy(str, save_str, len);
2234 str += len;
fef20d9c 2235 break;
4370aa4a 2236 }
fef20d9c
FW
2237
2238 case FORMAT_TYPE_PTR:
4370aa4a
LJ
2239 save_arg(void *);
2240 /* skip all alphanumeric pointer suffixes */
fef20d9c 2241 while (isalnum(*fmt))
4370aa4a 2242 fmt++;
fef20d9c
FW
2243 break;
2244
fef20d9c
FW
2245 default:
2246 switch (spec.type) {
2247
2248 case FORMAT_TYPE_LONG_LONG:
4370aa4a 2249 save_arg(long long);
fef20d9c
FW
2250 break;
2251 case FORMAT_TYPE_ULONG:
2252 case FORMAT_TYPE_LONG:
4370aa4a 2253 save_arg(unsigned long);
fef20d9c
FW
2254 break;
2255 case FORMAT_TYPE_SIZE_T:
4370aa4a 2256 save_arg(size_t);
fef20d9c
FW
2257 break;
2258 case FORMAT_TYPE_PTRDIFF:
4370aa4a 2259 save_arg(ptrdiff_t);
fef20d9c 2260 break;
a4e94ef0
Z
2261 case FORMAT_TYPE_UBYTE:
2262 case FORMAT_TYPE_BYTE:
2263 save_arg(char);
2264 break;
fef20d9c
FW
2265 case FORMAT_TYPE_USHORT:
2266 case FORMAT_TYPE_SHORT:
4370aa4a 2267 save_arg(short);
fef20d9c
FW
2268 break;
2269 default:
4370aa4a 2270 save_arg(int);
fef20d9c 2271 }
4370aa4a
LJ
2272 }
2273 }
fef20d9c 2274
b006f19b 2275out:
7b9186f5 2276 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
fef20d9c 2277#undef save_arg
4370aa4a
LJ
2278}
2279EXPORT_SYMBOL_GPL(vbin_printf);
2280
2281/**
2282 * bstr_printf - Format a string from binary arguments and place it in a buffer
2283 * @buf: The buffer to place the result into
2284 * @size: The size of the buffer, including the trailing null space
2285 * @fmt: The format string to use
2286 * @bin_buf: Binary arguments for the format string
2287 *
2288 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2289 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2290 * a binary buffer that generated by vbin_printf.
2291 *
2292 * The format follows C99 vsnprintf, but has some extensions:
0efb4d20 2293 * see vsnprintf comment for details.
4370aa4a
LJ
2294 *
2295 * The return value is the number of characters which would
2296 * be generated for the given input, excluding the trailing
2297 * '\0', as per ISO C99. If you want to have the exact
2298 * number of characters written into @buf as return value
2299 * (not including the trailing '\0'), use vscnprintf(). If the
2300 * return is greater than or equal to @size, the resulting
2301 * string is truncated.
2302 */
2303int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2304{
fef20d9c 2305 struct printf_spec spec = {0};
d4be151b
AGR
2306 char *str, *end;
2307 const char *args = (const char *)bin_buf;
4370aa4a 2308
762abb51 2309 if (WARN_ON_ONCE(size > INT_MAX))
4370aa4a 2310 return 0;
4370aa4a
LJ
2311
2312 str = buf;
2313 end = buf + size;
2314
2315#define get_arg(type) \
2316({ \
2317 typeof(type) value; \
2318 if (sizeof(type) == 8) { \
2319 args = PTR_ALIGN(args, sizeof(u32)); \
2320 *(u32 *)&value = *(u32 *)args; \
2321 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2322 } else { \
2323 args = PTR_ALIGN(args, sizeof(type)); \
2324 value = *(typeof(type) *)args; \
2325 } \
2326 args += sizeof(type); \
2327 value; \
2328})
2329
2330 /* Make sure end is always >= buf */
2331 if (end < buf) {
2332 end = ((void *)-1);
2333 size = end - buf;
2334 }
2335
fef20d9c 2336 while (*fmt) {
fef20d9c 2337 const char *old_fmt = fmt;
d4be151b 2338 int read = format_decode(fmt, &spec);
4370aa4a 2339
fef20d9c 2340 fmt += read;
4370aa4a 2341
fef20d9c
FW
2342 switch (spec.type) {
2343 case FORMAT_TYPE_NONE: {
2344 int copy = read;
2345 if (str < end) {
2346 if (copy > end - str)
2347 copy = end - str;
2348 memcpy(str, old_fmt, copy);
4370aa4a 2349 }
fef20d9c
FW
2350 str += read;
2351 break;
4370aa4a
LJ
2352 }
2353
ed681a91 2354 case FORMAT_TYPE_WIDTH:
fef20d9c
FW
2355 spec.field_width = get_arg(int);
2356 break;
4370aa4a 2357
fef20d9c
FW
2358 case FORMAT_TYPE_PRECISION:
2359 spec.precision = get_arg(int);
2360 break;
4370aa4a 2361
d4be151b
AGR
2362 case FORMAT_TYPE_CHAR: {
2363 char c;
2364
fef20d9c
FW
2365 if (!(spec.flags & LEFT)) {
2366 while (--spec.field_width > 0) {
4370aa4a
LJ
2367 if (str < end)
2368 *str = ' ';
2369 ++str;
2370 }
2371 }
2372 c = (unsigned char) get_arg(char);
2373 if (str < end)
2374 *str = c;
2375 ++str;
fef20d9c 2376 while (--spec.field_width > 0) {
4370aa4a
LJ
2377 if (str < end)
2378 *str = ' ';
2379 ++str;
2380 }
fef20d9c 2381 break;
d4be151b 2382 }
4370aa4a 2383
fef20d9c 2384 case FORMAT_TYPE_STR: {
4370aa4a 2385 const char *str_arg = args;
d4be151b 2386 args += strlen(str_arg) + 1;
fef20d9c
FW
2387 str = string(str, end, (char *)str_arg, spec);
2388 break;
4370aa4a
LJ
2389 }
2390
fef20d9c 2391 case FORMAT_TYPE_PTR:
ffbfed03 2392 str = pointer(fmt, str, end, get_arg(void *), spec);
fef20d9c 2393 while (isalnum(*fmt))
4370aa4a 2394 fmt++;
fef20d9c 2395 break;
4370aa4a 2396
fef20d9c 2397 case FORMAT_TYPE_PERCENT_CHAR:
4370aa4a
LJ
2398 if (str < end)
2399 *str = '%';
2400 ++str;
fef20d9c
FW
2401 break;
2402
b006f19b
RV
2403 case FORMAT_TYPE_INVALID:
2404 goto out;
2405
d4be151b
AGR
2406 default: {
2407 unsigned long long num;
2408
fef20d9c
FW
2409 switch (spec.type) {
2410
2411 case FORMAT_TYPE_LONG_LONG:
2412 num = get_arg(long long);
2413 break;
2414 case FORMAT_TYPE_ULONG:
fef20d9c
FW
2415 case FORMAT_TYPE_LONG:
2416 num = get_arg(unsigned long);
2417 break;
2418 case FORMAT_TYPE_SIZE_T:
2419 num = get_arg(size_t);
2420 break;
2421 case FORMAT_TYPE_PTRDIFF:
2422 num = get_arg(ptrdiff_t);
2423 break;
a4e94ef0
Z
2424 case FORMAT_TYPE_UBYTE:
2425 num = get_arg(unsigned char);
2426 break;
2427 case FORMAT_TYPE_BYTE:
2428 num = get_arg(signed char);
2429 break;
fef20d9c
FW
2430 case FORMAT_TYPE_USHORT:
2431 num = get_arg(unsigned short);
2432 break;
2433 case FORMAT_TYPE_SHORT:
2434 num = get_arg(short);
2435 break;
2436 case FORMAT_TYPE_UINT:
2437 num = get_arg(unsigned int);
2438 break;
2439 default:
2440 num = get_arg(int);
2441 }
2442
2443 str = number(str, end, num, spec);
d4be151b
AGR
2444 } /* default: */
2445 } /* switch(spec.type) */
2446 } /* while(*fmt) */
fef20d9c 2447
b006f19b 2448out:
4370aa4a
LJ
2449 if (size > 0) {
2450 if (str < end)
2451 *str = '\0';
2452 else
2453 end[-1] = '\0';
2454 }
fef20d9c 2455
4370aa4a
LJ
2456#undef get_arg
2457
2458 /* the trailing null byte doesn't count towards the total */
2459 return str - buf;
2460}
2461EXPORT_SYMBOL_GPL(bstr_printf);
2462
2463/**
2464 * bprintf - Parse a format string and place args' binary value in a buffer
2465 * @bin_buf: The buffer to place args' binary value
2466 * @size: The size of the buffer(by words(32bits), not characters)
2467 * @fmt: The format string to use
2468 * @...: Arguments for the format string
2469 *
2470 * The function returns the number of words(u32) written
2471 * into @bin_buf.
2472 */
2473int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2474{
2475 va_list args;
2476 int ret;
2477
2478 va_start(args, fmt);
2479 ret = vbin_printf(bin_buf, size, fmt, args);
2480 va_end(args);
7b9186f5 2481
4370aa4a
LJ
2482 return ret;
2483}
2484EXPORT_SYMBOL_GPL(bprintf);
2485
2486#endif /* CONFIG_BINARY_PRINTF */
2487
1da177e4
LT
2488/**
2489 * vsscanf - Unformat a buffer into a list of arguments
2490 * @buf: input buffer
2491 * @fmt: format of buffer
2492 * @args: arguments
2493 */
7b9186f5 2494int vsscanf(const char *buf, const char *fmt, va_list args)
1da177e4
LT
2495{
2496 const char *str = buf;
2497 char *next;
2498 char digit;
2499 int num = 0;
ef0658f3 2500 u8 qualifier;
53809751
JB
2501 unsigned int base;
2502 union {
2503 long long s;
2504 unsigned long long u;
2505 } val;
ef0658f3 2506 s16 field_width;
d4be151b 2507 bool is_sign;
1da177e4 2508
da99075c 2509 while (*fmt) {
1da177e4
LT
2510 /* skip any white space in format */
2511 /* white space in format matchs any amount of
2512 * white space, including none, in the input.
2513 */
2514 if (isspace(*fmt)) {
e7d2860b
AGR
2515 fmt = skip_spaces(++fmt);
2516 str = skip_spaces(str);
1da177e4
LT
2517 }
2518
2519 /* anything that is not a conversion must match exactly */
2520 if (*fmt != '%' && *fmt) {
2521 if (*fmt++ != *str++)
2522 break;
2523 continue;
2524 }
2525
2526 if (!*fmt)
2527 break;
2528 ++fmt;
7b9186f5 2529
1da177e4
LT
2530 /* skip this conversion.
2531 * advance both strings to next white space
2532 */
2533 if (*fmt == '*') {
da99075c
JB
2534 if (!*str)
2535 break;
8fccae2c 2536 while (!isspace(*fmt) && *fmt != '%' && *fmt)
1da177e4
LT
2537 fmt++;
2538 while (!isspace(*str) && *str)
2539 str++;
2540 continue;
2541 }
2542
2543 /* get field width */
2544 field_width = -1;
53809751 2545 if (isdigit(*fmt)) {
1da177e4 2546 field_width = skip_atoi(&fmt);
53809751
JB
2547 if (field_width <= 0)
2548 break;
2549 }
1da177e4
LT
2550
2551 /* get conversion qualifier */
2552 qualifier = -1;
75fb8f26
AS
2553 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2554 _tolower(*fmt) == 'z') {
1da177e4
LT
2555 qualifier = *fmt++;
2556 if (unlikely(qualifier == *fmt)) {
2557 if (qualifier == 'h') {
2558 qualifier = 'H';
2559 fmt++;
2560 } else if (qualifier == 'l') {
2561 qualifier = 'L';
2562 fmt++;
2563 }
2564 }
2565 }
1da177e4 2566
da99075c
JB
2567 if (!*fmt)
2568 break;
2569
2570 if (*fmt == 'n') {
2571 /* return number of characters read so far */
2572 *va_arg(args, int *) = str - buf;
2573 ++fmt;
2574 continue;
2575 }
2576
2577 if (!*str)
1da177e4
LT
2578 break;
2579
d4be151b 2580 base = 10;
3f623eba 2581 is_sign = false;
d4be151b 2582
7b9186f5 2583 switch (*fmt++) {
1da177e4
LT
2584 case 'c':
2585 {
7b9186f5 2586 char *s = (char *)va_arg(args, char*);
1da177e4
LT
2587 if (field_width == -1)
2588 field_width = 1;
2589 do {
2590 *s++ = *str++;
2591 } while (--field_width > 0 && *str);
2592 num++;
2593 }
2594 continue;
2595 case 's':
2596 {
7b9186f5
AGR
2597 char *s = (char *)va_arg(args, char *);
2598 if (field_width == -1)
4be929be 2599 field_width = SHRT_MAX;
1da177e4 2600 /* first, skip leading white space in buffer */
e7d2860b 2601 str = skip_spaces(str);
1da177e4
LT
2602
2603 /* now copy until next white space */
7b9186f5 2604 while (*str && !isspace(*str) && field_width--)
1da177e4 2605 *s++ = *str++;
1da177e4
LT
2606 *s = '\0';
2607 num++;
2608 }
2609 continue;
1da177e4
LT
2610 case 'o':
2611 base = 8;
2612 break;
2613 case 'x':
2614 case 'X':
2615 base = 16;
2616 break;
2617 case 'i':
7b9186f5 2618 base = 0;
1da177e4 2619 case 'd':
3f623eba 2620 is_sign = true;
1da177e4
LT
2621 case 'u':
2622 break;
2623 case '%':
2624 /* looking for '%' in str */
7b9186f5 2625 if (*str++ != '%')
1da177e4
LT
2626 return num;
2627 continue;
2628 default:
2629 /* invalid format; stop here */
2630 return num;
2631 }
2632
2633 /* have some sort of integer conversion.
2634 * first, skip white space in buffer.
2635 */
e7d2860b 2636 str = skip_spaces(str);
1da177e4
LT
2637
2638 digit = *str;
2639 if (is_sign && digit == '-')
2640 digit = *(str + 1);
2641
2642 if (!digit
7b9186f5
AGR
2643 || (base == 16 && !isxdigit(digit))
2644 || (base == 10 && !isdigit(digit))
2645 || (base == 8 && (!isdigit(digit) || digit > '7'))
2646 || (base == 0 && !isdigit(digit)))
2647 break;
1da177e4 2648
53809751
JB
2649 if (is_sign)
2650 val.s = qualifier != 'L' ?
2651 simple_strtol(str, &next, base) :
2652 simple_strtoll(str, &next, base);
2653 else
2654 val.u = qualifier != 'L' ?
2655 simple_strtoul(str, &next, base) :
2656 simple_strtoull(str, &next, base);
2657
2658 if (field_width > 0 && next - str > field_width) {
2659 if (base == 0)
2660 _parse_integer_fixup_radix(str, &base);
2661 while (next - str > field_width) {
2662 if (is_sign)
2663 val.s = div_s64(val.s, base);
2664 else
2665 val.u = div_u64(val.u, base);
2666 --next;
2667 }
2668 }
2669
7b9186f5 2670 switch (qualifier) {
1da177e4 2671 case 'H': /* that's 'hh' in format */
53809751
JB
2672 if (is_sign)
2673 *va_arg(args, signed char *) = val.s;
2674 else
2675 *va_arg(args, unsigned char *) = val.u;
1da177e4
LT
2676 break;
2677 case 'h':
53809751
JB
2678 if (is_sign)
2679 *va_arg(args, short *) = val.s;
2680 else
2681 *va_arg(args, unsigned short *) = val.u;
1da177e4
LT
2682 break;
2683 case 'l':
53809751
JB
2684 if (is_sign)
2685 *va_arg(args, long *) = val.s;
2686 else
2687 *va_arg(args, unsigned long *) = val.u;
1da177e4
LT
2688 break;
2689 case 'L':
53809751
JB
2690 if (is_sign)
2691 *va_arg(args, long long *) = val.s;
2692 else
2693 *va_arg(args, unsigned long long *) = val.u;
1da177e4
LT
2694 break;
2695 case 'Z':
2696 case 'z':
53809751
JB
2697 *va_arg(args, size_t *) = val.u;
2698 break;
1da177e4 2699 default:
53809751
JB
2700 if (is_sign)
2701 *va_arg(args, int *) = val.s;
2702 else
2703 *va_arg(args, unsigned int *) = val.u;
1da177e4
LT
2704 break;
2705 }
2706 num++;
2707
2708 if (!next)
2709 break;
2710 str = next;
2711 }
c6b40d16 2712
1da177e4
LT
2713 return num;
2714}
1da177e4
LT
2715EXPORT_SYMBOL(vsscanf);
2716
2717/**
2718 * sscanf - Unformat a buffer into a list of arguments
2719 * @buf: input buffer
2720 * @fmt: formatting of buffer
2721 * @...: resulting arguments
2722 */
7b9186f5 2723int sscanf(const char *buf, const char *fmt, ...)
1da177e4
LT
2724{
2725 va_list args;
2726 int i;
2727
7b9186f5
AGR
2728 va_start(args, fmt);
2729 i = vsscanf(buf, fmt, args);
1da177e4 2730 va_end(args);
7b9186f5 2731
1da177e4
LT
2732 return i;
2733}
1da177e4 2734EXPORT_SYMBOL(sscanf);