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