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