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