net: fix harmonize_features() vs NETIF_F_HIGHDMA
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / lib / vsprintf.c
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
12 /*
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>
20 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/ctype.h>
24 #include <linux/kernel.h>
25 #include <linux/kallsyms.h>
26 #include <linux/math64.h>
27 #include <linux/uaccess.h>
28 #include <linux/ioport.h>
29 #include <linux/cred.h>
30 #include <net/addrconf.h>
31
32 #include <asm/page.h> /* for PAGE_SIZE */
33 #include <asm/sections.h> /* for dereference_function_descriptor() */
34
35 #include "kstrtox.h"
36
37 /**
38 * simple_strtoull - convert a string to an unsigned long long
39 * @cp: The start of the string
40 * @endp: A pointer to the end of the parsed string will be placed here
41 * @base: The number base to use
42 *
43 * This function is obsolete. Please use kstrtoull instead.
44 */
45 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
46 {
47 unsigned long long result;
48 unsigned int rv;
49
50 cp = _parse_integer_fixup_radix(cp, &base);
51 rv = _parse_integer(cp, base, &result);
52 /* FIXME */
53 cp += (rv & ~KSTRTOX_OVERFLOW);
54
55 if (endp)
56 *endp = (char *)cp;
57
58 return result;
59 }
60 EXPORT_SYMBOL(simple_strtoull);
61
62 /**
63 * simple_strtoul - convert a string to an unsigned long
64 * @cp: The start of the string
65 * @endp: A pointer to the end of the parsed string will be placed here
66 * @base: The number base to use
67 *
68 * This function is obsolete. Please use kstrtoul instead.
69 */
70 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
71 {
72 return simple_strtoull(cp, endp, base);
73 }
74 EXPORT_SYMBOL(simple_strtoul);
75
76 /**
77 * simple_strtol - convert a string to a signed long
78 * @cp: The start of the string
79 * @endp: A pointer to the end of the parsed string will be placed here
80 * @base: The number base to use
81 *
82 * This function is obsolete. Please use kstrtol instead.
83 */
84 long simple_strtol(const char *cp, char **endp, unsigned int base)
85 {
86 if (*cp == '-')
87 return -simple_strtoul(cp + 1, endp, base);
88
89 return simple_strtoul(cp, endp, base);
90 }
91 EXPORT_SYMBOL(simple_strtol);
92
93 /**
94 * simple_strtoll - convert a string to a signed long long
95 * @cp: The start of the string
96 * @endp: A pointer to the end of the parsed string will be placed here
97 * @base: The number base to use
98 *
99 * This function is obsolete. Please use kstrtoll instead.
100 */
101 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
102 {
103 if (*cp == '-')
104 return -simple_strtoull(cp + 1, endp, base);
105
106 return simple_strtoull(cp, endp, base);
107 }
108 EXPORT_SYMBOL(simple_strtoll);
109
110 static noinline_for_stack
111 int skip_atoi(const char **s)
112 {
113 int i = 0;
114
115 while (isdigit(**s))
116 i = i*10 + *((*s)++) - '0';
117
118 return i;
119 }
120
121 /* Decimal conversion is by far the most typical, and is used
122 * for /proc and /sys data. This directly impacts e.g. top performance
123 * with many processes running. We optimize it for speed
124 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
125 * (with permission from the author, Douglas W. Jones).
126 */
127
128 #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
129 /* Formats correctly any integer in [0, 999999999] */
130 static noinline_for_stack
131 char *put_dec_full9(char *buf, unsigned q)
132 {
133 unsigned r;
134
135 /*
136 * Possible ways to approx. divide by 10
137 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
138 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
139 * (x * 0x6667) >> 18 x < 43699
140 * (x * 0x3334) >> 17 x < 16389
141 * (x * 0x199a) >> 16 x < 16389
142 * (x * 0x0ccd) >> 15 x < 16389
143 * (x * 0x0667) >> 14 x < 2739
144 * (x * 0x0334) >> 13 x < 1029
145 * (x * 0x019a) >> 12 x < 1029
146 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
147 * (x * 0x0067) >> 10 x < 179
148 * (x * 0x0034) >> 9 x < 69 same
149 * (x * 0x001a) >> 8 x < 69 same
150 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
151 * (x * 0x0007) >> 6 x < 19
152 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
153 */
154 r = (q * (uint64_t)0x1999999a) >> 32;
155 *buf++ = (q - 10 * r) + '0'; /* 1 */
156 q = (r * (uint64_t)0x1999999a) >> 32;
157 *buf++ = (r - 10 * q) + '0'; /* 2 */
158 r = (q * (uint64_t)0x1999999a) >> 32;
159 *buf++ = (q - 10 * r) + '0'; /* 3 */
160 q = (r * (uint64_t)0x1999999a) >> 32;
161 *buf++ = (r - 10 * q) + '0'; /* 4 */
162 r = (q * (uint64_t)0x1999999a) >> 32;
163 *buf++ = (q - 10 * r) + '0'; /* 5 */
164 /* Now value is under 10000, can avoid 64-bit multiply */
165 q = (r * 0x199a) >> 16;
166 *buf++ = (r - 10 * q) + '0'; /* 6 */
167 r = (q * 0xcd) >> 11;
168 *buf++ = (q - 10 * r) + '0'; /* 7 */
169 q = (r * 0xcd) >> 11;
170 *buf++ = (r - 10 * q) + '0'; /* 8 */
171 *buf++ = q + '0'; /* 9 */
172 return buf;
173 }
174 #endif
175
176 /* Similar to above but do not pad with zeros.
177 * Code can be easily arranged to print 9 digits too, but our callers
178 * always call put_dec_full9() instead when the number has 9 decimal digits.
179 */
180 static noinline_for_stack
181 char *put_dec_trunc8(char *buf, unsigned r)
182 {
183 unsigned q;
184
185 /* Copy of previous function's body with added early returns */
186 while (r >= 10000) {
187 q = r + '0';
188 r = (r * (uint64_t)0x1999999a) >> 32;
189 *buf++ = q - 10*r;
190 }
191
192 q = (r * 0x199a) >> 16; /* r <= 9999 */
193 *buf++ = (r - 10 * q) + '0';
194 if (q == 0)
195 return buf;
196 r = (q * 0xcd) >> 11; /* q <= 999 */
197 *buf++ = (q - 10 * r) + '0';
198 if (r == 0)
199 return buf;
200 q = (r * 0xcd) >> 11; /* r <= 99 */
201 *buf++ = (r - 10 * q) + '0';
202 if (q == 0)
203 return buf;
204 *buf++ = q + '0'; /* q <= 9 */
205 return buf;
206 }
207
208 /* There are two algorithms to print larger numbers.
209 * One is generic: divide by 1000000000 and repeatedly print
210 * groups of (up to) 9 digits. It's conceptually simple,
211 * but requires a (unsigned long long) / 1000000000 division.
212 *
213 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
214 * manipulates them cleverly and generates groups of 4 decimal digits.
215 * It so happens that it does NOT require long long division.
216 *
217 * If long is > 32 bits, division of 64-bit values is relatively easy,
218 * and we will use the first algorithm.
219 * If long long is > 64 bits (strange architecture with VERY large long long),
220 * second algorithm can't be used, and we again use the first one.
221 *
222 * Else (if long is 32 bits and long long is 64 bits) we use second one.
223 */
224
225 #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
226
227 /* First algorithm: generic */
228
229 static
230 char *put_dec(char *buf, unsigned long long n)
231 {
232 if (n >= 100*1000*1000) {
233 while (n >= 1000*1000*1000)
234 buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
235 if (n >= 100*1000*1000)
236 return put_dec_full9(buf, n);
237 }
238 return put_dec_trunc8(buf, n);
239 }
240
241 #else
242
243 /* Second algorithm: valid only for 64-bit long longs */
244
245 /* See comment in put_dec_full9 for choice of constants */
246 static noinline_for_stack
247 void put_dec_full4(char *buf, unsigned q)
248 {
249 unsigned r;
250 r = (q * 0xccd) >> 15;
251 buf[0] = (q - 10 * r) + '0';
252 q = (r * 0xcd) >> 11;
253 buf[1] = (r - 10 * q) + '0';
254 r = (q * 0xcd) >> 11;
255 buf[2] = (q - 10 * r) + '0';
256 buf[3] = r + '0';
257 }
258
259 /*
260 * Call put_dec_full4 on x % 10000, return x / 10000.
261 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
262 * holds for all x < 1,128,869,999. The largest value this
263 * helper will ever be asked to convert is 1,125,520,955.
264 * (d1 in the put_dec code, assuming n is all-ones).
265 */
266 static
267 unsigned put_dec_helper4(char *buf, unsigned x)
268 {
269 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
270
271 put_dec_full4(buf, x - q * 10000);
272 return q;
273 }
274
275 /* Based on code by Douglas W. Jones found at
276 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
277 * (with permission from the author).
278 * Performs no 64-bit division and hence should be fast on 32-bit machines.
279 */
280 static
281 char *put_dec(char *buf, unsigned long long n)
282 {
283 uint32_t d3, d2, d1, q, h;
284
285 if (n < 100*1000*1000)
286 return put_dec_trunc8(buf, n);
287
288 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
289 h = (n >> 32);
290 d2 = (h ) & 0xffff;
291 d3 = (h >> 16); /* implicit "& 0xffff" */
292
293 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
294 q = put_dec_helper4(buf, q);
295
296 q += 7671 * d3 + 9496 * d2 + 6 * d1;
297 q = put_dec_helper4(buf+4, q);
298
299 q += 4749 * d3 + 42 * d2;
300 q = put_dec_helper4(buf+8, q);
301
302 q += 281 * d3;
303 buf += 12;
304 if (q)
305 buf = put_dec_trunc8(buf, q);
306 else while (buf[-1] == '0')
307 --buf;
308
309 return buf;
310 }
311
312 #endif
313
314 /*
315 * Convert passed number to decimal string.
316 * Returns the length of string. On buffer overflow, returns 0.
317 *
318 * If speed is not important, use snprintf(). It's easy to read the code.
319 */
320 int num_to_str(char *buf, int size, unsigned long long num)
321 {
322 char tmp[sizeof(num) * 3];
323 int idx, len;
324
325 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
326 if (num <= 9) {
327 tmp[0] = '0' + num;
328 len = 1;
329 } else {
330 len = put_dec(tmp, num) - tmp;
331 }
332
333 if (len > size)
334 return 0;
335 for (idx = 0; idx < len; ++idx)
336 buf[idx] = tmp[len - idx - 1];
337 return len;
338 }
339
340 #define ZEROPAD 1 /* pad with zero */
341 #define SIGN 2 /* unsigned/signed long */
342 #define PLUS 4 /* show plus */
343 #define SPACE 8 /* space if plus */
344 #define LEFT 16 /* left justified */
345 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
346 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
347
348 enum format_type {
349 FORMAT_TYPE_NONE, /* Just a string part */
350 FORMAT_TYPE_WIDTH,
351 FORMAT_TYPE_PRECISION,
352 FORMAT_TYPE_CHAR,
353 FORMAT_TYPE_STR,
354 FORMAT_TYPE_PTR,
355 FORMAT_TYPE_PERCENT_CHAR,
356 FORMAT_TYPE_INVALID,
357 FORMAT_TYPE_LONG_LONG,
358 FORMAT_TYPE_ULONG,
359 FORMAT_TYPE_LONG,
360 FORMAT_TYPE_UBYTE,
361 FORMAT_TYPE_BYTE,
362 FORMAT_TYPE_USHORT,
363 FORMAT_TYPE_SHORT,
364 FORMAT_TYPE_UINT,
365 FORMAT_TYPE_INT,
366 FORMAT_TYPE_NRCHARS,
367 FORMAT_TYPE_SIZE_T,
368 FORMAT_TYPE_PTRDIFF
369 };
370
371 struct printf_spec {
372 u8 type; /* format_type enum */
373 u8 flags; /* flags to number() */
374 u8 base; /* number base, 8, 10 or 16 only */
375 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
376 s16 field_width; /* width of output field */
377 s16 precision; /* # of digits/chars */
378 };
379
380 int kptr_restrict __read_mostly = 4;
381
382 /*
383 * Always cleanse %p and %pK specifiers
384 */
385 static inline int kptr_restrict_always_cleanse_pointers(void)
386 {
387 return kptr_restrict >= 3;
388 }
389
390 /*
391 * Always cleanse physical addresses (%pa* specifiers)
392 */
393 static inline int kptr_restrict_cleanse_addresses(void)
394 {
395 return kptr_restrict >= 4;
396 }
397
398 /*
399 * Always cleanse resource addresses (%p[rR] specifiers)
400 */
401 static inline int kptr_restrict_cleanse_resources(void)
402 {
403 return kptr_restrict >= 4;
404 }
405
406 static noinline_for_stack
407 char *number(char *buf, char *end, unsigned long long num,
408 struct printf_spec spec)
409 {
410 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
411 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
412
413 char tmp[66];
414 char sign;
415 char locase;
416 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
417 int i;
418 bool is_zero = num == 0LL;
419
420 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
421 * produces same digits or (maybe lowercased) letters */
422 locase = (spec.flags & SMALL);
423 if (spec.flags & LEFT)
424 spec.flags &= ~ZEROPAD;
425 sign = 0;
426 if (spec.flags & SIGN) {
427 if ((signed long long)num < 0) {
428 sign = '-';
429 num = -(signed long long)num;
430 spec.field_width--;
431 } else if (spec.flags & PLUS) {
432 sign = '+';
433 spec.field_width--;
434 } else if (spec.flags & SPACE) {
435 sign = ' ';
436 spec.field_width--;
437 }
438 }
439 if (need_pfx) {
440 if (spec.base == 16)
441 spec.field_width -= 2;
442 else if (!is_zero)
443 spec.field_width--;
444 }
445
446 /* generate full string in tmp[], in reverse order */
447 i = 0;
448 if (num < spec.base)
449 tmp[i++] = digits[num] | locase;
450 /* Generic code, for any base:
451 else do {
452 tmp[i++] = (digits[do_div(num,base)] | locase);
453 } while (num != 0);
454 */
455 else if (spec.base != 10) { /* 8 or 16 */
456 int mask = spec.base - 1;
457 int shift = 3;
458
459 if (spec.base == 16)
460 shift = 4;
461 do {
462 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
463 num >>= shift;
464 } while (num);
465 } else { /* base 10 */
466 i = put_dec(tmp, num) - tmp;
467 }
468
469 /* printing 100 using %2d gives "100", not "00" */
470 if (i > spec.precision)
471 spec.precision = i;
472 /* leading space padding */
473 spec.field_width -= spec.precision;
474 if (!(spec.flags & (ZEROPAD+LEFT))) {
475 while (--spec.field_width >= 0) {
476 if (buf < end)
477 *buf = ' ';
478 ++buf;
479 }
480 }
481 /* sign */
482 if (sign) {
483 if (buf < end)
484 *buf = sign;
485 ++buf;
486 }
487 /* "0x" / "0" prefix */
488 if (need_pfx) {
489 if (spec.base == 16 || !is_zero) {
490 if (buf < end)
491 *buf = '0';
492 ++buf;
493 }
494 if (spec.base == 16) {
495 if (buf < end)
496 *buf = ('X' | locase);
497 ++buf;
498 }
499 }
500 /* zero or space padding */
501 if (!(spec.flags & LEFT)) {
502 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
503 while (--spec.field_width >= 0) {
504 if (buf < end)
505 *buf = c;
506 ++buf;
507 }
508 }
509 /* hmm even more zero padding? */
510 while (i <= --spec.precision) {
511 if (buf < end)
512 *buf = '0';
513 ++buf;
514 }
515 /* actual digits of result */
516 while (--i >= 0) {
517 if (buf < end)
518 *buf = tmp[i];
519 ++buf;
520 }
521 /* trailing space padding */
522 while (--spec.field_width >= 0) {
523 if (buf < end)
524 *buf = ' ';
525 ++buf;
526 }
527
528 return buf;
529 }
530
531 static noinline_for_stack
532 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
533 {
534 int len, i;
535
536 if ((unsigned long)s < PAGE_SIZE)
537 s = "(null)";
538
539 len = strnlen(s, spec.precision);
540
541 if (!(spec.flags & LEFT)) {
542 while (len < spec.field_width--) {
543 if (buf < end)
544 *buf = ' ';
545 ++buf;
546 }
547 }
548 for (i = 0; i < len; ++i) {
549 if (buf < end)
550 *buf = *s;
551 ++buf; ++s;
552 }
553 while (len < spec.field_width--) {
554 if (buf < end)
555 *buf = ' ';
556 ++buf;
557 }
558
559 return buf;
560 }
561
562 static noinline_for_stack
563 char *symbol_string(char *buf, char *end, void *ptr,
564 struct printf_spec spec, const char *fmt)
565 {
566 unsigned long value;
567 #ifdef CONFIG_KALLSYMS
568 char sym[KSYM_SYMBOL_LEN];
569 #endif
570
571 if (fmt[1] == 'R')
572 ptr = __builtin_extract_return_addr(ptr);
573 value = (unsigned long)ptr;
574
575 #ifdef CONFIG_KALLSYMS
576 if (*fmt == 'B')
577 sprint_backtrace(sym, value);
578 else if (*fmt != 'f' && *fmt != 's')
579 sprint_symbol(sym, value);
580 else
581 sprint_symbol_no_offset(sym, value);
582
583 return string(buf, end, sym, spec);
584 #else
585 spec.field_width = 2 * sizeof(void *);
586 spec.flags |= SPECIAL | SMALL | ZEROPAD;
587 spec.base = 16;
588
589 return number(buf, end, value, spec);
590 #endif
591 }
592
593 static noinline_for_stack
594 char *resource_string(char *buf, char *end, struct resource *res,
595 struct printf_spec spec, const char *fmt)
596 {
597 #ifndef IO_RSRC_PRINTK_SIZE
598 #define IO_RSRC_PRINTK_SIZE 6
599 #endif
600
601 #ifndef MEM_RSRC_PRINTK_SIZE
602 #define MEM_RSRC_PRINTK_SIZE 10
603 #endif
604 static const struct printf_spec io_spec = {
605 .base = 16,
606 .field_width = IO_RSRC_PRINTK_SIZE,
607 .precision = -1,
608 .flags = SPECIAL | SMALL | ZEROPAD,
609 };
610 static const struct printf_spec mem_spec = {
611 .base = 16,
612 .field_width = MEM_RSRC_PRINTK_SIZE,
613 .precision = -1,
614 .flags = SPECIAL | SMALL | ZEROPAD,
615 };
616 static const struct printf_spec bus_spec = {
617 .base = 16,
618 .field_width = 2,
619 .precision = -1,
620 .flags = SMALL | ZEROPAD,
621 };
622 static const struct printf_spec dec_spec = {
623 .base = 10,
624 .precision = -1,
625 .flags = 0,
626 };
627 static const struct printf_spec str_spec = {
628 .field_width = -1,
629 .precision = 10,
630 .flags = LEFT,
631 };
632 static const struct printf_spec flag_spec = {
633 .base = 16,
634 .precision = -1,
635 .flags = SPECIAL | SMALL,
636 };
637
638 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
639 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
640 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
641 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
642 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
643 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
644 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
645 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
646
647 char *p = sym, *pend = sym + sizeof(sym);
648 int decode = (fmt[0] == 'R') ? 1 : 0;
649 int cleanse = kptr_restrict_cleanse_resources();
650 const struct printf_spec *specp;
651
652 *p++ = '[';
653 if (res->flags & IORESOURCE_IO) {
654 p = string(p, pend, "io ", str_spec);
655 specp = &io_spec;
656 } else if (res->flags & IORESOURCE_MEM) {
657 p = string(p, pend, "mem ", str_spec);
658 specp = &mem_spec;
659 } else if (res->flags & IORESOURCE_IRQ) {
660 p = string(p, pend, "irq ", str_spec);
661 specp = &dec_spec;
662 } else if (res->flags & IORESOURCE_DMA) {
663 p = string(p, pend, "dma ", str_spec);
664 specp = &dec_spec;
665 } else if (res->flags & IORESOURCE_BUS) {
666 p = string(p, pend, "bus ", str_spec);
667 specp = &bus_spec;
668 } else {
669 p = string(p, pend, "??? ", str_spec);
670 specp = &mem_spec;
671 decode = 0;
672 }
673 p = number(p, pend, cleanse ? 0UL : res->start, *specp);
674 if (res->start != res->end) {
675 *p++ = '-';
676 p = number(p, pend,
677 cleanse ? res->end - res->start : res->end, *specp);
678 }
679 if (decode) {
680 if (res->flags & IORESOURCE_MEM_64)
681 p = string(p, pend, " 64bit", str_spec);
682 if (res->flags & IORESOURCE_PREFETCH)
683 p = string(p, pend, " pref", str_spec);
684 if (res->flags & IORESOURCE_WINDOW)
685 p = string(p, pend, " window", str_spec);
686 if (res->flags & IORESOURCE_DISABLED)
687 p = string(p, pend, " disabled", str_spec);
688 } else {
689 p = string(p, pend, " flags ", str_spec);
690 p = number(p, pend, res->flags, flag_spec);
691 }
692 *p++ = ']';
693 *p = '\0';
694
695 return string(buf, end, sym, spec);
696
697 }
698
699 static noinline_for_stack
700 char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
701 const char *fmt)
702 {
703 int i, len = 1; /* if we pass '%ph[CDN]', field witdh remains
704 negative value, fallback to the default */
705 char separator;
706
707 if (spec.field_width == 0)
708 /* nothing to print */
709 return buf;
710
711 if (ZERO_OR_NULL_PTR(addr))
712 /* NULL pointer */
713 return string(buf, end, NULL, spec);
714
715 switch (fmt[1]) {
716 case 'C':
717 separator = ':';
718 break;
719 case 'D':
720 separator = '-';
721 break;
722 case 'N':
723 separator = 0;
724 break;
725 default:
726 separator = ' ';
727 break;
728 }
729
730 if (spec.field_width > 0)
731 len = min_t(int, spec.field_width, 64);
732
733 for (i = 0; i < len && buf < end - 1; i++) {
734 buf = hex_byte_pack(buf, addr[i]);
735
736 if (buf < end && separator && i != len - 1)
737 *buf++ = separator;
738 }
739
740 return buf;
741 }
742
743 static noinline_for_stack
744 char *mac_address_string(char *buf, char *end, u8 *addr,
745 struct printf_spec spec, const char *fmt)
746 {
747 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
748 char *p = mac_addr;
749 int i;
750 char separator;
751 bool reversed = false;
752
753 switch (fmt[1]) {
754 case 'F':
755 separator = '-';
756 break;
757
758 case 'R':
759 reversed = true;
760 /* fall through */
761
762 default:
763 separator = ':';
764 break;
765 }
766
767 for (i = 0; i < 6; i++) {
768 if (reversed)
769 p = hex_byte_pack(p, addr[5 - i]);
770 else
771 p = hex_byte_pack(p, addr[i]);
772
773 if (fmt[0] == 'M' && i != 5)
774 *p++ = separator;
775 }
776 *p = '\0';
777
778 return string(buf, end, mac_addr, spec);
779 }
780
781 static noinline_for_stack
782 char *ip4_string(char *p, const u8 *addr, const char *fmt)
783 {
784 int i;
785 bool leading_zeros = (fmt[0] == 'i');
786 int index;
787 int step;
788
789 switch (fmt[2]) {
790 case 'h':
791 #ifdef __BIG_ENDIAN
792 index = 0;
793 step = 1;
794 #else
795 index = 3;
796 step = -1;
797 #endif
798 break;
799 case 'l':
800 index = 3;
801 step = -1;
802 break;
803 case 'n':
804 case 'b':
805 default:
806 index = 0;
807 step = 1;
808 break;
809 }
810 for (i = 0; i < 4; i++) {
811 char temp[3]; /* hold each IP quad in reverse order */
812 int digits = put_dec_trunc8(temp, addr[index]) - temp;
813 if (leading_zeros) {
814 if (digits < 3)
815 *p++ = '0';
816 if (digits < 2)
817 *p++ = '0';
818 }
819 /* reverse the digits in the quad */
820 while (digits--)
821 *p++ = temp[digits];
822 if (i < 3)
823 *p++ = '.';
824 index += step;
825 }
826 *p = '\0';
827
828 return p;
829 }
830
831 static noinline_for_stack
832 char *ip6_compressed_string(char *p, const char *addr)
833 {
834 int i, j, range;
835 unsigned char zerolength[8];
836 int longest = 1;
837 int colonpos = -1;
838 u16 word;
839 u8 hi, lo;
840 bool needcolon = false;
841 bool useIPv4;
842 struct in6_addr in6;
843
844 memcpy(&in6, addr, sizeof(struct in6_addr));
845
846 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
847
848 memset(zerolength, 0, sizeof(zerolength));
849
850 if (useIPv4)
851 range = 6;
852 else
853 range = 8;
854
855 /* find position of longest 0 run */
856 for (i = 0; i < range; i++) {
857 for (j = i; j < range; j++) {
858 if (in6.s6_addr16[j] != 0)
859 break;
860 zerolength[i]++;
861 }
862 }
863 for (i = 0; i < range; i++) {
864 if (zerolength[i] > longest) {
865 longest = zerolength[i];
866 colonpos = i;
867 }
868 }
869 if (longest == 1) /* don't compress a single 0 */
870 colonpos = -1;
871
872 /* emit address */
873 for (i = 0; i < range; i++) {
874 if (i == colonpos) {
875 if (needcolon || i == 0)
876 *p++ = ':';
877 *p++ = ':';
878 needcolon = false;
879 i += longest - 1;
880 continue;
881 }
882 if (needcolon) {
883 *p++ = ':';
884 needcolon = false;
885 }
886 /* hex u16 without leading 0s */
887 word = ntohs(in6.s6_addr16[i]);
888 hi = word >> 8;
889 lo = word & 0xff;
890 if (hi) {
891 if (hi > 0x0f)
892 p = hex_byte_pack(p, hi);
893 else
894 *p++ = hex_asc_lo(hi);
895 p = hex_byte_pack(p, lo);
896 }
897 else if (lo > 0x0f)
898 p = hex_byte_pack(p, lo);
899 else
900 *p++ = hex_asc_lo(lo);
901 needcolon = true;
902 }
903
904 if (useIPv4) {
905 if (needcolon)
906 *p++ = ':';
907 p = ip4_string(p, &in6.s6_addr[12], "I4");
908 }
909 *p = '\0';
910
911 return p;
912 }
913
914 static noinline_for_stack
915 char *ip6_string(char *p, const char *addr, const char *fmt)
916 {
917 int i;
918
919 for (i = 0; i < 8; i++) {
920 p = hex_byte_pack(p, *addr++);
921 p = hex_byte_pack(p, *addr++);
922 if (fmt[0] == 'I' && i != 7)
923 *p++ = ':';
924 }
925 *p = '\0';
926
927 return p;
928 }
929
930 static noinline_for_stack
931 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
932 struct printf_spec spec, const char *fmt)
933 {
934 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
935
936 if (fmt[0] == 'I' && fmt[2] == 'c')
937 ip6_compressed_string(ip6_addr, addr);
938 else
939 ip6_string(ip6_addr, addr, fmt);
940
941 return string(buf, end, ip6_addr, spec);
942 }
943
944 static noinline_for_stack
945 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
946 struct printf_spec spec, const char *fmt)
947 {
948 char ip4_addr[sizeof("255.255.255.255")];
949
950 ip4_string(ip4_addr, addr, fmt);
951
952 return string(buf, end, ip4_addr, spec);
953 }
954
955 static noinline_for_stack
956 char *uuid_string(char *buf, char *end, const u8 *addr,
957 struct printf_spec spec, const char *fmt)
958 {
959 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
960 char *p = uuid;
961 int i;
962 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
963 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
964 const u8 *index = be;
965 bool uc = false;
966
967 switch (*(++fmt)) {
968 case 'L':
969 uc = true; /* fall-through */
970 case 'l':
971 index = le;
972 break;
973 case 'B':
974 uc = true;
975 break;
976 }
977
978 for (i = 0; i < 16; i++) {
979 p = hex_byte_pack(p, addr[index[i]]);
980 switch (i) {
981 case 3:
982 case 5:
983 case 7:
984 case 9:
985 *p++ = '-';
986 break;
987 }
988 }
989
990 *p = 0;
991
992 if (uc) {
993 p = uuid;
994 do {
995 *p = toupper(*p);
996 } while (*(++p));
997 }
998
999 return string(buf, end, uuid, spec);
1000 }
1001
1002 static
1003 char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1004 struct printf_spec spec)
1005 {
1006 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1007 if (spec.field_width == -1)
1008 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1009 spec.base = 16;
1010
1011 return number(buf, end, *(const netdev_features_t *)addr, spec);
1012 }
1013
1014 /*
1015 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1016 * by an extra set of alphanumeric characters that are extended format
1017 * specifiers.
1018 *
1019 * Right now we handle:
1020 *
1021 * - 'F' For symbolic function descriptor pointers with offset
1022 * - 'f' For simple symbolic function names without offset
1023 * - 'S' For symbolic direct pointers with offset
1024 * - 's' For symbolic direct pointers without offset
1025 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
1026 * - 'B' For backtraced symbolic direct pointers with offset
1027 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1028 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1029 * - 'M' For a 6-byte MAC address, it prints the address in the
1030 * usual colon-separated hex notation
1031 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1032 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1033 * with a dash-separated hex notation
1034 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
1035 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1036 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1037 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1038 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1039 * IPv6 omits the colons (01020304...0f)
1040 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1041 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
1042 * - 'I6c' for IPv6 addresses printed as specified by
1043 * http://tools.ietf.org/html/rfc5952
1044 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1045 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1046 * Options for %pU are:
1047 * b big endian lower case hex (default)
1048 * B big endian UPPER case hex
1049 * l little endian lower case hex
1050 * L little endian UPPER case hex
1051 * big endian output byte order is:
1052 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1053 * little endian output byte order is:
1054 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
1055 * - 'V' For a struct va_format which contains a format string * and va_list *,
1056 * call vsnprintf(->format, *->va_list).
1057 * Implements a "recursive vsnprintf".
1058 * Do not use this feature without some mechanism to verify the
1059 * correctness of the format string and va_list arguments.
1060 * - 'K' For a kernel pointer that should be hidden from unprivileged users
1061 * - 'P' For a kernel pointer that should be shown to all users
1062 * - 'NF' For a netdev_features_t
1063 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1064 * a certain separator (' ' by default):
1065 * C colon
1066 * D dash
1067 * N no separator
1068 * The maximum supported length is 64 bytes of the input. Consider
1069 * to use print_hex_dump() for the larger input.
1070 * - 'a' For a phys_addr_t type and its derivative types (passed by reference)
1071 *
1072 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1073 * function pointers are really function descriptors, which contain a
1074 * pointer to the real address.
1075 *
1076 * Note: That for kptr_restrict set to 3, %p and %pK have the same
1077 * meaning.
1078 *
1079 * Note: That for kptr_restrict set to 4, %pa will null out the physical
1080 * address.
1081 *
1082 * Note: That for kptr_restrict set to 4, %p[rR] will null out the memory
1083 * address.
1084 */
1085 static noinline_for_stack
1086 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1087 struct printf_spec spec)
1088 {
1089 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1090
1091 if (!ptr && *fmt != 'K' && !kptr_restrict_always_cleanse_pointers()) {
1092 /*
1093 * Print (null) with the same width as a pointer so it makes
1094 * tabular output look nice.
1095 */
1096 if (spec.field_width == -1)
1097 spec.field_width = default_width;
1098 return string(buf, end, "(null)", spec);
1099 }
1100
1101 switch (*fmt) {
1102 case 'F':
1103 case 'f':
1104 ptr = dereference_function_descriptor(ptr);
1105 /* Fallthrough */
1106 case 'S':
1107 case 's':
1108 case 'B':
1109 return symbol_string(buf, end, ptr, spec, fmt);
1110 case 'R':
1111 case 'r':
1112 return resource_string(buf, end, ptr, spec, fmt);
1113 case 'h':
1114 return hex_string(buf, end, ptr, spec, fmt);
1115 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1116 case 'm': /* Contiguous: 000102030405 */
1117 /* [mM]F (FDDI) */
1118 /* [mM]R (Reverse order; Bluetooth) */
1119 return mac_address_string(buf, end, ptr, spec, fmt);
1120 case 'I': /* Formatted IP supported
1121 * 4: 1.2.3.4
1122 * 6: 0001:0203:...:0708
1123 * 6c: 1::708 or 1::1.2.3.4
1124 */
1125 case 'i': /* Contiguous:
1126 * 4: 001.002.003.004
1127 * 6: 000102...0f
1128 */
1129 switch (fmt[1]) {
1130 case '6':
1131 return ip6_addr_string(buf, end, ptr, spec, fmt);
1132 case '4':
1133 return ip4_addr_string(buf, end, ptr, spec, fmt);
1134 }
1135 break;
1136 case 'U':
1137 return uuid_string(buf, end, ptr, spec, fmt);
1138 case 'V':
1139 {
1140 va_list va;
1141
1142 va_copy(va, *((struct va_format *)ptr)->va);
1143 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1144 ((struct va_format *)ptr)->fmt, va);
1145 va_end(va);
1146 return buf;
1147 }
1148 case 'N':
1149 switch (fmt[1]) {
1150 case 'F':
1151 return netdev_feature_string(buf, end, ptr, spec);
1152 }
1153 break;
1154 case 'a':
1155 {
1156 unsigned long long addr;
1157 if (fmt[1] != 'P' && kptr_restrict_cleanse_addresses())
1158 addr = 0;
1159 else
1160 addr = *((phys_addr_t *)ptr);
1161 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1162 spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1163 spec.base = 16;
1164 return number(buf, end, addr, spec);
1165 }
1166 case 'P':
1167 /*
1168 * an explicitly whitelisted kernel pointer should never be
1169 * cleansed
1170 */
1171 break;
1172 default:
1173 /*
1174 * plain %p, no extension, check if we should always cleanse and
1175 * treat like %pK.
1176 */
1177 if (!kptr_restrict_always_cleanse_pointers()) {
1178 break;
1179 }
1180 /* fallthrough */
1181 case 'K':
1182 switch (kptr_restrict) {
1183 case 0:
1184 /* Always print %p values */
1185 break;
1186 case 1: {
1187 const struct cred *cred;
1188
1189 /*
1190 * kptr_restrict==1 cannot be used in IRQ context
1191 * because its test for CAP_SYSLOG would be meaningless.
1192 */
1193 if (in_irq() || in_serving_softirq() || in_nmi()) {
1194 if (spec.field_width == -1)
1195 spec.field_width = default_width;
1196 return string(buf, end, "pK-error", spec);
1197 }
1198
1199 /*
1200 * Only print the real pointer value if the current
1201 * process has CAP_SYSLOG and is running with the
1202 * same credentials it started with. This is because
1203 * access to files is checked at open() time, but %p
1204 * checks permission at read() time. We don't want to
1205 * leak pointer values if a binary opens a file using
1206 * %pK and then elevates privileges before reading it.
1207 */
1208 cred = current_cred();
1209 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1210 !uid_eq(cred->euid, cred->uid) ||
1211 !gid_eq(cred->egid, cred->gid))
1212 ptr = NULL;
1213 break;
1214 }
1215 case 2: /* restrict only %pK */
1216 case 3: /* restrict all non-extensioned %p and %pK */
1217 case 4: /* restrict all non-extensioned %p, %pK, %pa*, %p[rR] */
1218 default:
1219 ptr = NULL;
1220 break;
1221 }
1222 break;
1223 }
1224 spec.flags |= SMALL;
1225 if (spec.field_width == -1) {
1226 spec.field_width = default_width;
1227 spec.flags |= ZEROPAD;
1228 }
1229 spec.base = 16;
1230
1231 return number(buf, end, (unsigned long long) ptr, spec);
1232 }
1233
1234 /*
1235 * Helper function to decode printf style format.
1236 * Each call decode a token from the format and return the
1237 * number of characters read (or likely the delta where it wants
1238 * to go on the next call).
1239 * The decoded token is returned through the parameters
1240 *
1241 * 'h', 'l', or 'L' for integer fields
1242 * 'z' support added 23/7/1999 S.H.
1243 * 'z' changed to 'Z' --davidm 1/25/99
1244 * 't' added for ptrdiff_t
1245 *
1246 * @fmt: the format string
1247 * @type of the token returned
1248 * @flags: various flags such as +, -, # tokens..
1249 * @field_width: overwritten width
1250 * @base: base of the number (octal, hex, ...)
1251 * @precision: precision of a number
1252 * @qualifier: qualifier of a number (long, size_t, ...)
1253 */
1254 static noinline_for_stack
1255 int format_decode(const char *fmt, struct printf_spec *spec)
1256 {
1257 const char *start = fmt;
1258
1259 /* we finished early by reading the field width */
1260 if (spec->type == FORMAT_TYPE_WIDTH) {
1261 if (spec->field_width < 0) {
1262 spec->field_width = -spec->field_width;
1263 spec->flags |= LEFT;
1264 }
1265 spec->type = FORMAT_TYPE_NONE;
1266 goto precision;
1267 }
1268
1269 /* we finished early by reading the precision */
1270 if (spec->type == FORMAT_TYPE_PRECISION) {
1271 if (spec->precision < 0)
1272 spec->precision = 0;
1273
1274 spec->type = FORMAT_TYPE_NONE;
1275 goto qualifier;
1276 }
1277
1278 /* By default */
1279 spec->type = FORMAT_TYPE_NONE;
1280
1281 for (; *fmt ; ++fmt) {
1282 if (*fmt == '%')
1283 break;
1284 }
1285
1286 /* Return the current non-format string */
1287 if (fmt != start || !*fmt)
1288 return fmt - start;
1289
1290 /* Process flags */
1291 spec->flags = 0;
1292
1293 while (1) { /* this also skips first '%' */
1294 bool found = true;
1295
1296 ++fmt;
1297
1298 switch (*fmt) {
1299 case '-': spec->flags |= LEFT; break;
1300 case '+': spec->flags |= PLUS; break;
1301 case ' ': spec->flags |= SPACE; break;
1302 case '#': spec->flags |= SPECIAL; break;
1303 case '0': spec->flags |= ZEROPAD; break;
1304 default: found = false;
1305 }
1306
1307 if (!found)
1308 break;
1309 }
1310
1311 /* get field width */
1312 spec->field_width = -1;
1313
1314 if (isdigit(*fmt))
1315 spec->field_width = skip_atoi(&fmt);
1316 else if (*fmt == '*') {
1317 /* it's the next argument */
1318 spec->type = FORMAT_TYPE_WIDTH;
1319 return ++fmt - start;
1320 }
1321
1322 precision:
1323 /* get the precision */
1324 spec->precision = -1;
1325 if (*fmt == '.') {
1326 ++fmt;
1327 if (isdigit(*fmt)) {
1328 spec->precision = skip_atoi(&fmt);
1329 if (spec->precision < 0)
1330 spec->precision = 0;
1331 } else if (*fmt == '*') {
1332 /* it's the next argument */
1333 spec->type = FORMAT_TYPE_PRECISION;
1334 return ++fmt - start;
1335 }
1336 }
1337
1338 qualifier:
1339 /* get the conversion qualifier */
1340 spec->qualifier = -1;
1341 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1342 _tolower(*fmt) == 'z' || *fmt == 't') {
1343 spec->qualifier = *fmt++;
1344 if (unlikely(spec->qualifier == *fmt)) {
1345 if (spec->qualifier == 'l') {
1346 spec->qualifier = 'L';
1347 ++fmt;
1348 } else if (spec->qualifier == 'h') {
1349 spec->qualifier = 'H';
1350 ++fmt;
1351 }
1352 }
1353 }
1354
1355 /* default base */
1356 spec->base = 10;
1357 switch (*fmt) {
1358 case 'c':
1359 spec->type = FORMAT_TYPE_CHAR;
1360 return ++fmt - start;
1361
1362 case 's':
1363 spec->type = FORMAT_TYPE_STR;
1364 return ++fmt - start;
1365
1366 case 'p':
1367 spec->type = FORMAT_TYPE_PTR;
1368 return fmt - start;
1369 /* skip alnum */
1370
1371 case 'n':
1372 spec->type = FORMAT_TYPE_NRCHARS;
1373 return ++fmt - start;
1374
1375 case '%':
1376 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1377 return ++fmt - start;
1378
1379 /* integer number formats - set up the flags and "break" */
1380 case 'o':
1381 spec->base = 8;
1382 break;
1383
1384 case 'x':
1385 spec->flags |= SMALL;
1386
1387 case 'X':
1388 spec->base = 16;
1389 break;
1390
1391 case 'd':
1392 case 'i':
1393 spec->flags |= SIGN;
1394 case 'u':
1395 break;
1396
1397 default:
1398 spec->type = FORMAT_TYPE_INVALID;
1399 return fmt - start;
1400 }
1401
1402 if (spec->qualifier == 'L')
1403 spec->type = FORMAT_TYPE_LONG_LONG;
1404 else if (spec->qualifier == 'l') {
1405 if (spec->flags & SIGN)
1406 spec->type = FORMAT_TYPE_LONG;
1407 else
1408 spec->type = FORMAT_TYPE_ULONG;
1409 } else if (_tolower(spec->qualifier) == 'z') {
1410 spec->type = FORMAT_TYPE_SIZE_T;
1411 } else if (spec->qualifier == 't') {
1412 spec->type = FORMAT_TYPE_PTRDIFF;
1413 } else if (spec->qualifier == 'H') {
1414 if (spec->flags & SIGN)
1415 spec->type = FORMAT_TYPE_BYTE;
1416 else
1417 spec->type = FORMAT_TYPE_UBYTE;
1418 } else if (spec->qualifier == 'h') {
1419 if (spec->flags & SIGN)
1420 spec->type = FORMAT_TYPE_SHORT;
1421 else
1422 spec->type = FORMAT_TYPE_USHORT;
1423 } else {
1424 if (spec->flags & SIGN)
1425 spec->type = FORMAT_TYPE_INT;
1426 else
1427 spec->type = FORMAT_TYPE_UINT;
1428 }
1429
1430 return ++fmt - start;
1431 }
1432
1433 /**
1434 * vsnprintf - Format a string and place it in a buffer
1435 * @buf: The buffer to place the result into
1436 * @size: The size of the buffer, including the trailing null space
1437 * @fmt: The format string to use
1438 * @args: Arguments for the format string
1439 *
1440 * This function follows C99 vsnprintf, but has some extensions:
1441 * %pS output the name of a text symbol with offset
1442 * %ps output the name of a text symbol without offset
1443 * %pF output the name of a function pointer with its offset
1444 * %pf output the name of a function pointer without its offset
1445 * %pB output the name of a backtrace symbol with its offset
1446 * %pR output the address range in a struct resource with decoded flags
1447 * %pr output the address range in a struct resource with raw flags
1448 * %pM output a 6-byte MAC address with colons
1449 * %pMR output a 6-byte MAC address with colons in reversed order
1450 * %pMF output a 6-byte MAC address with dashes
1451 * %pm output a 6-byte MAC address without colons
1452 * %pmR output a 6-byte MAC address without colons in reversed order
1453 * %pI4 print an IPv4 address without leading zeros
1454 * %pi4 print an IPv4 address with leading zeros
1455 * %pI6 print an IPv6 address with colons
1456 * %pi6 print an IPv6 address without colons
1457 * %pI6c print an IPv6 address as specified by RFC 5952
1458 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1459 * case.
1460 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1461 * bytes of the input)
1462 * %n is ignored
1463 *
1464 * ** Please update Documentation/printk-formats.txt when making changes **
1465 *
1466 * The return value is the number of characters which would
1467 * be generated for the given input, excluding the trailing
1468 * '\0', as per ISO C99. If you want to have the exact
1469 * number of characters written into @buf as return value
1470 * (not including the trailing '\0'), use vscnprintf(). If the
1471 * return is greater than or equal to @size, the resulting
1472 * string is truncated.
1473 *
1474 * If you're not already dealing with a va_list consider using snprintf().
1475 */
1476 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1477 {
1478 unsigned long long num;
1479 char *str, *end;
1480 struct printf_spec spec = {0};
1481
1482 /* Reject out-of-range values early. Large positive sizes are
1483 used for unknown buffer sizes. */
1484 if (WARN_ON_ONCE((int) size < 0))
1485 return 0;
1486
1487 str = buf;
1488 end = buf + size;
1489
1490 /* Make sure end is always >= buf */
1491 if (end < buf) {
1492 end = ((void *)-1);
1493 size = end - buf;
1494 }
1495
1496 while (*fmt) {
1497 const char *old_fmt = fmt;
1498 int read = format_decode(fmt, &spec);
1499
1500 fmt += read;
1501
1502 switch (spec.type) {
1503 case FORMAT_TYPE_NONE: {
1504 int copy = read;
1505 if (str < end) {
1506 if (copy > end - str)
1507 copy = end - str;
1508 memcpy(str, old_fmt, copy);
1509 }
1510 str += read;
1511 break;
1512 }
1513
1514 case FORMAT_TYPE_WIDTH:
1515 spec.field_width = va_arg(args, int);
1516 break;
1517
1518 case FORMAT_TYPE_PRECISION:
1519 spec.precision = va_arg(args, int);
1520 break;
1521
1522 case FORMAT_TYPE_CHAR: {
1523 char c;
1524
1525 if (!(spec.flags & LEFT)) {
1526 while (--spec.field_width > 0) {
1527 if (str < end)
1528 *str = ' ';
1529 ++str;
1530
1531 }
1532 }
1533 c = (unsigned char) va_arg(args, int);
1534 if (str < end)
1535 *str = c;
1536 ++str;
1537 while (--spec.field_width > 0) {
1538 if (str < end)
1539 *str = ' ';
1540 ++str;
1541 }
1542 break;
1543 }
1544
1545 case FORMAT_TYPE_STR:
1546 str = string(str, end, va_arg(args, char *), spec);
1547 break;
1548
1549 case FORMAT_TYPE_PTR:
1550 str = pointer(fmt+1, str, end, va_arg(args, void *),
1551 spec);
1552 while (isalnum(*fmt))
1553 fmt++;
1554 break;
1555
1556 case FORMAT_TYPE_PERCENT_CHAR:
1557 if (str < end)
1558 *str = '%';
1559 ++str;
1560 break;
1561
1562 case FORMAT_TYPE_INVALID:
1563 if (str < end)
1564 *str = '%';
1565 ++str;
1566 break;
1567
1568 case FORMAT_TYPE_NRCHARS: {
1569 u8 qualifier = spec.qualifier;
1570
1571 if (qualifier == 'l') {
1572 long *ip = va_arg(args, long *);
1573 *ip = (str - buf);
1574 } else if (_tolower(qualifier) == 'z') {
1575 size_t *ip = va_arg(args, size_t *);
1576 *ip = (str - buf);
1577 } else {
1578 int *ip = va_arg(args, int *);
1579 *ip = (str - buf);
1580 }
1581 break;
1582 }
1583
1584 default:
1585 switch (spec.type) {
1586 case FORMAT_TYPE_LONG_LONG:
1587 num = va_arg(args, long long);
1588 break;
1589 case FORMAT_TYPE_ULONG:
1590 num = va_arg(args, unsigned long);
1591 break;
1592 case FORMAT_TYPE_LONG:
1593 num = va_arg(args, long);
1594 break;
1595 case FORMAT_TYPE_SIZE_T:
1596 if (spec.flags & SIGN)
1597 num = va_arg(args, ssize_t);
1598 else
1599 num = va_arg(args, size_t);
1600 break;
1601 case FORMAT_TYPE_PTRDIFF:
1602 num = va_arg(args, ptrdiff_t);
1603 break;
1604 case FORMAT_TYPE_UBYTE:
1605 num = (unsigned char) va_arg(args, int);
1606 break;
1607 case FORMAT_TYPE_BYTE:
1608 num = (signed char) va_arg(args, int);
1609 break;
1610 case FORMAT_TYPE_USHORT:
1611 num = (unsigned short) va_arg(args, int);
1612 break;
1613 case FORMAT_TYPE_SHORT:
1614 num = (short) va_arg(args, int);
1615 break;
1616 case FORMAT_TYPE_INT:
1617 num = (int) va_arg(args, int);
1618 break;
1619 default:
1620 num = va_arg(args, unsigned int);
1621 }
1622
1623 str = number(str, end, num, spec);
1624 }
1625 }
1626
1627 if (size > 0) {
1628 if (str < end)
1629 *str = '\0';
1630 else
1631 end[-1] = '\0';
1632 }
1633
1634 /* the trailing null byte doesn't count towards the total */
1635 return str-buf;
1636
1637 }
1638 EXPORT_SYMBOL(vsnprintf);
1639
1640 /**
1641 * vscnprintf - Format a string and place it in a buffer
1642 * @buf: The buffer to place the result into
1643 * @size: The size of the buffer, including the trailing null space
1644 * @fmt: The format string to use
1645 * @args: Arguments for the format string
1646 *
1647 * The return value is the number of characters which have been written into
1648 * the @buf not including the trailing '\0'. If @size is == 0 the function
1649 * returns 0.
1650 *
1651 * If you're not already dealing with a va_list consider using scnprintf().
1652 *
1653 * See the vsnprintf() documentation for format string extensions over C99.
1654 */
1655 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1656 {
1657 int i;
1658
1659 i = vsnprintf(buf, size, fmt, args);
1660
1661 if (likely(i < size))
1662 return i;
1663 if (size != 0)
1664 return size - 1;
1665 return 0;
1666 }
1667 EXPORT_SYMBOL(vscnprintf);
1668
1669 /**
1670 * snprintf - Format a string and place it in a buffer
1671 * @buf: The buffer to place the result into
1672 * @size: The size of the buffer, including the trailing null space
1673 * @fmt: The format string to use
1674 * @...: Arguments for the format string
1675 *
1676 * The return value is the number of characters which would be
1677 * generated for the given input, excluding the trailing null,
1678 * as per ISO C99. If the return is greater than or equal to
1679 * @size, the resulting string is truncated.
1680 *
1681 * See the vsnprintf() documentation for format string extensions over C99.
1682 */
1683 int snprintf(char *buf, size_t size, const char *fmt, ...)
1684 {
1685 va_list args;
1686 int i;
1687
1688 va_start(args, fmt);
1689 i = vsnprintf(buf, size, fmt, args);
1690 va_end(args);
1691
1692 return i;
1693 }
1694 EXPORT_SYMBOL(snprintf);
1695
1696 /**
1697 * scnprintf - Format a string and place it in a buffer
1698 * @buf: The buffer to place the result into
1699 * @size: The size of the buffer, including the trailing null space
1700 * @fmt: The format string to use
1701 * @...: Arguments for the format string
1702 *
1703 * The return value is the number of characters written into @buf not including
1704 * the trailing '\0'. If @size is == 0 the function returns 0.
1705 */
1706
1707 int scnprintf(char *buf, size_t size, const char *fmt, ...)
1708 {
1709 va_list args;
1710 int i;
1711
1712 va_start(args, fmt);
1713 i = vscnprintf(buf, size, fmt, args);
1714 va_end(args);
1715
1716 return i;
1717 }
1718 EXPORT_SYMBOL(scnprintf);
1719
1720 /**
1721 * vsprintf - Format a string and place it in a buffer
1722 * @buf: The buffer to place the result into
1723 * @fmt: The format string to use
1724 * @args: Arguments for the format string
1725 *
1726 * The function returns the number of characters written
1727 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1728 * buffer overflows.
1729 *
1730 * If you're not already dealing with a va_list consider using sprintf().
1731 *
1732 * See the vsnprintf() documentation for format string extensions over C99.
1733 */
1734 int vsprintf(char *buf, const char *fmt, va_list args)
1735 {
1736 return vsnprintf(buf, INT_MAX, fmt, args);
1737 }
1738 EXPORT_SYMBOL(vsprintf);
1739
1740 /**
1741 * sprintf - Format a string and place it in a buffer
1742 * @buf: The buffer to place the result into
1743 * @fmt: The format string to use
1744 * @...: Arguments for the format string
1745 *
1746 * The function returns the number of characters written
1747 * into @buf. Use snprintf() or scnprintf() in order to avoid
1748 * buffer overflows.
1749 *
1750 * See the vsnprintf() documentation for format string extensions over C99.
1751 */
1752 int sprintf(char *buf, const char *fmt, ...)
1753 {
1754 va_list args;
1755 int i;
1756
1757 va_start(args, fmt);
1758 i = vsnprintf(buf, INT_MAX, fmt, args);
1759 va_end(args);
1760
1761 return i;
1762 }
1763 EXPORT_SYMBOL(sprintf);
1764
1765 #ifdef CONFIG_BINARY_PRINTF
1766 /*
1767 * bprintf service:
1768 * vbin_printf() - VA arguments to binary data
1769 * bstr_printf() - Binary data to text string
1770 */
1771
1772 /**
1773 * vbin_printf - Parse a format string and place args' binary value in a buffer
1774 * @bin_buf: The buffer to place args' binary value
1775 * @size: The size of the buffer(by words(32bits), not characters)
1776 * @fmt: The format string to use
1777 * @args: Arguments for the format string
1778 *
1779 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1780 * is skiped.
1781 *
1782 * The return value is the number of words(32bits) which would be generated for
1783 * the given input.
1784 *
1785 * NOTE:
1786 * If the return value is greater than @size, the resulting bin_buf is NOT
1787 * valid for bstr_printf().
1788 */
1789 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1790 {
1791 struct printf_spec spec = {0};
1792 char *str, *end;
1793
1794 str = (char *)bin_buf;
1795 end = (char *)(bin_buf + size);
1796
1797 #define save_arg(type) \
1798 do { \
1799 if (sizeof(type) == 8) { \
1800 unsigned long long value; \
1801 str = PTR_ALIGN(str, sizeof(u32)); \
1802 value = va_arg(args, unsigned long long); \
1803 if (str + sizeof(type) <= end) { \
1804 *(u32 *)str = *(u32 *)&value; \
1805 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1806 } \
1807 } else { \
1808 unsigned long value; \
1809 str = PTR_ALIGN(str, sizeof(type)); \
1810 value = va_arg(args, int); \
1811 if (str + sizeof(type) <= end) \
1812 *(typeof(type) *)str = (type)value; \
1813 } \
1814 str += sizeof(type); \
1815 } while (0)
1816
1817 while (*fmt) {
1818 int read = format_decode(fmt, &spec);
1819
1820 fmt += read;
1821
1822 switch (spec.type) {
1823 case FORMAT_TYPE_NONE:
1824 case FORMAT_TYPE_INVALID:
1825 case FORMAT_TYPE_PERCENT_CHAR:
1826 break;
1827
1828 case FORMAT_TYPE_WIDTH:
1829 case FORMAT_TYPE_PRECISION:
1830 save_arg(int);
1831 break;
1832
1833 case FORMAT_TYPE_CHAR:
1834 save_arg(char);
1835 break;
1836
1837 case FORMAT_TYPE_STR: {
1838 const char *save_str = va_arg(args, char *);
1839 size_t len;
1840
1841 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1842 || (unsigned long)save_str < PAGE_SIZE)
1843 save_str = "(null)";
1844 len = strlen(save_str) + 1;
1845 if (str + len < end)
1846 memcpy(str, save_str, len);
1847 str += len;
1848 break;
1849 }
1850
1851 case FORMAT_TYPE_PTR:
1852 save_arg(void *);
1853 /* skip all alphanumeric pointer suffixes */
1854 while (isalnum(*fmt))
1855 fmt++;
1856 break;
1857
1858 case FORMAT_TYPE_NRCHARS: {
1859 /* skip %n 's argument */
1860 u8 qualifier = spec.qualifier;
1861 void *skip_arg;
1862 if (qualifier == 'l')
1863 skip_arg = va_arg(args, long *);
1864 else if (_tolower(qualifier) == 'z')
1865 skip_arg = va_arg(args, size_t *);
1866 else
1867 skip_arg = va_arg(args, int *);
1868 break;
1869 }
1870
1871 default:
1872 switch (spec.type) {
1873
1874 case FORMAT_TYPE_LONG_LONG:
1875 save_arg(long long);
1876 break;
1877 case FORMAT_TYPE_ULONG:
1878 case FORMAT_TYPE_LONG:
1879 save_arg(unsigned long);
1880 break;
1881 case FORMAT_TYPE_SIZE_T:
1882 save_arg(size_t);
1883 break;
1884 case FORMAT_TYPE_PTRDIFF:
1885 save_arg(ptrdiff_t);
1886 break;
1887 case FORMAT_TYPE_UBYTE:
1888 case FORMAT_TYPE_BYTE:
1889 save_arg(char);
1890 break;
1891 case FORMAT_TYPE_USHORT:
1892 case FORMAT_TYPE_SHORT:
1893 save_arg(short);
1894 break;
1895 default:
1896 save_arg(int);
1897 }
1898 }
1899 }
1900
1901 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1902 #undef save_arg
1903 }
1904 EXPORT_SYMBOL_GPL(vbin_printf);
1905
1906 /**
1907 * bstr_printf - Format a string from binary arguments and place it in a buffer
1908 * @buf: The buffer to place the result into
1909 * @size: The size of the buffer, including the trailing null space
1910 * @fmt: The format string to use
1911 * @bin_buf: Binary arguments for the format string
1912 *
1913 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1914 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1915 * a binary buffer that generated by vbin_printf.
1916 *
1917 * The format follows C99 vsnprintf, but has some extensions:
1918 * see vsnprintf comment for details.
1919 *
1920 * The return value is the number of characters which would
1921 * be generated for the given input, excluding the trailing
1922 * '\0', as per ISO C99. If you want to have the exact
1923 * number of characters written into @buf as return value
1924 * (not including the trailing '\0'), use vscnprintf(). If the
1925 * return is greater than or equal to @size, the resulting
1926 * string is truncated.
1927 */
1928 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1929 {
1930 struct printf_spec spec = {0};
1931 char *str, *end;
1932 const char *args = (const char *)bin_buf;
1933
1934 if (WARN_ON_ONCE((int) size < 0))
1935 return 0;
1936
1937 str = buf;
1938 end = buf + size;
1939
1940 #define get_arg(type) \
1941 ({ \
1942 typeof(type) value; \
1943 if (sizeof(type) == 8) { \
1944 args = PTR_ALIGN(args, sizeof(u32)); \
1945 *(u32 *)&value = *(u32 *)args; \
1946 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1947 } else { \
1948 args = PTR_ALIGN(args, sizeof(type)); \
1949 value = *(typeof(type) *)args; \
1950 } \
1951 args += sizeof(type); \
1952 value; \
1953 })
1954
1955 /* Make sure end is always >= buf */
1956 if (end < buf) {
1957 end = ((void *)-1);
1958 size = end - buf;
1959 }
1960
1961 while (*fmt) {
1962 const char *old_fmt = fmt;
1963 int read = format_decode(fmt, &spec);
1964
1965 fmt += read;
1966
1967 switch (spec.type) {
1968 case FORMAT_TYPE_NONE: {
1969 int copy = read;
1970 if (str < end) {
1971 if (copy > end - str)
1972 copy = end - str;
1973 memcpy(str, old_fmt, copy);
1974 }
1975 str += read;
1976 break;
1977 }
1978
1979 case FORMAT_TYPE_WIDTH:
1980 spec.field_width = get_arg(int);
1981 break;
1982
1983 case FORMAT_TYPE_PRECISION:
1984 spec.precision = get_arg(int);
1985 break;
1986
1987 case FORMAT_TYPE_CHAR: {
1988 char c;
1989
1990 if (!(spec.flags & LEFT)) {
1991 while (--spec.field_width > 0) {
1992 if (str < end)
1993 *str = ' ';
1994 ++str;
1995 }
1996 }
1997 c = (unsigned char) get_arg(char);
1998 if (str < end)
1999 *str = c;
2000 ++str;
2001 while (--spec.field_width > 0) {
2002 if (str < end)
2003 *str = ' ';
2004 ++str;
2005 }
2006 break;
2007 }
2008
2009 case FORMAT_TYPE_STR: {
2010 const char *str_arg = args;
2011 args += strlen(str_arg) + 1;
2012 str = string(str, end, (char *)str_arg, spec);
2013 break;
2014 }
2015
2016 case FORMAT_TYPE_PTR:
2017 str = pointer(fmt+1, str, end, get_arg(void *), spec);
2018 while (isalnum(*fmt))
2019 fmt++;
2020 break;
2021
2022 case FORMAT_TYPE_PERCENT_CHAR:
2023 case FORMAT_TYPE_INVALID:
2024 if (str < end)
2025 *str = '%';
2026 ++str;
2027 break;
2028
2029 case FORMAT_TYPE_NRCHARS:
2030 /* skip */
2031 break;
2032
2033 default: {
2034 unsigned long long num;
2035
2036 switch (spec.type) {
2037
2038 case FORMAT_TYPE_LONG_LONG:
2039 num = get_arg(long long);
2040 break;
2041 case FORMAT_TYPE_ULONG:
2042 case FORMAT_TYPE_LONG:
2043 num = get_arg(unsigned long);
2044 break;
2045 case FORMAT_TYPE_SIZE_T:
2046 num = get_arg(size_t);
2047 break;
2048 case FORMAT_TYPE_PTRDIFF:
2049 num = get_arg(ptrdiff_t);
2050 break;
2051 case FORMAT_TYPE_UBYTE:
2052 num = get_arg(unsigned char);
2053 break;
2054 case FORMAT_TYPE_BYTE:
2055 num = get_arg(signed char);
2056 break;
2057 case FORMAT_TYPE_USHORT:
2058 num = get_arg(unsigned short);
2059 break;
2060 case FORMAT_TYPE_SHORT:
2061 num = get_arg(short);
2062 break;
2063 case FORMAT_TYPE_UINT:
2064 num = get_arg(unsigned int);
2065 break;
2066 default:
2067 num = get_arg(int);
2068 }
2069
2070 str = number(str, end, num, spec);
2071 } /* default: */
2072 } /* switch(spec.type) */
2073 } /* while(*fmt) */
2074
2075 if (size > 0) {
2076 if (str < end)
2077 *str = '\0';
2078 else
2079 end[-1] = '\0';
2080 }
2081
2082 #undef get_arg
2083
2084 /* the trailing null byte doesn't count towards the total */
2085 return str - buf;
2086 }
2087 EXPORT_SYMBOL_GPL(bstr_printf);
2088
2089 /**
2090 * bprintf - Parse a format string and place args' binary value in a buffer
2091 * @bin_buf: The buffer to place args' binary value
2092 * @size: The size of the buffer(by words(32bits), not characters)
2093 * @fmt: The format string to use
2094 * @...: Arguments for the format string
2095 *
2096 * The function returns the number of words(u32) written
2097 * into @bin_buf.
2098 */
2099 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2100 {
2101 va_list args;
2102 int ret;
2103
2104 va_start(args, fmt);
2105 ret = vbin_printf(bin_buf, size, fmt, args);
2106 va_end(args);
2107
2108 return ret;
2109 }
2110 EXPORT_SYMBOL_GPL(bprintf);
2111
2112 #endif /* CONFIG_BINARY_PRINTF */
2113
2114 /**
2115 * vsscanf - Unformat a buffer into a list of arguments
2116 * @buf: input buffer
2117 * @fmt: format of buffer
2118 * @args: arguments
2119 */
2120 int vsscanf(const char *buf, const char *fmt, va_list args)
2121 {
2122 const char *str = buf;
2123 char *next;
2124 char digit;
2125 int num = 0;
2126 u8 qualifier;
2127 unsigned int base;
2128 union {
2129 long long s;
2130 unsigned long long u;
2131 } val;
2132 s16 field_width;
2133 bool is_sign;
2134
2135 while (*fmt) {
2136 /* skip any white space in format */
2137 /* white space in format matchs any amount of
2138 * white space, including none, in the input.
2139 */
2140 if (isspace(*fmt)) {
2141 fmt = skip_spaces(++fmt);
2142 str = skip_spaces(str);
2143 }
2144
2145 /* anything that is not a conversion must match exactly */
2146 if (*fmt != '%' && *fmt) {
2147 if (*fmt++ != *str++)
2148 break;
2149 continue;
2150 }
2151
2152 if (!*fmt)
2153 break;
2154 ++fmt;
2155
2156 /* skip this conversion.
2157 * advance both strings to next white space
2158 */
2159 if (*fmt == '*') {
2160 if (!*str)
2161 break;
2162 while (!isspace(*fmt) && *fmt != '%' && *fmt)
2163 fmt++;
2164 while (!isspace(*str) && *str)
2165 str++;
2166 continue;
2167 }
2168
2169 /* get field width */
2170 field_width = -1;
2171 if (isdigit(*fmt)) {
2172 field_width = skip_atoi(&fmt);
2173 if (field_width <= 0)
2174 break;
2175 }
2176
2177 /* get conversion qualifier */
2178 qualifier = -1;
2179 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2180 _tolower(*fmt) == 'z') {
2181 qualifier = *fmt++;
2182 if (unlikely(qualifier == *fmt)) {
2183 if (qualifier == 'h') {
2184 qualifier = 'H';
2185 fmt++;
2186 } else if (qualifier == 'l') {
2187 qualifier = 'L';
2188 fmt++;
2189 }
2190 }
2191 }
2192
2193 if (!*fmt)
2194 break;
2195
2196 if (*fmt == 'n') {
2197 /* return number of characters read so far */
2198 *va_arg(args, int *) = str - buf;
2199 ++fmt;
2200 continue;
2201 }
2202
2203 if (!*str)
2204 break;
2205
2206 base = 10;
2207 is_sign = 0;
2208
2209 switch (*fmt++) {
2210 case 'c':
2211 {
2212 char *s = (char *)va_arg(args, char*);
2213 if (field_width == -1)
2214 field_width = 1;
2215 do {
2216 *s++ = *str++;
2217 } while (--field_width > 0 && *str);
2218 num++;
2219 }
2220 continue;
2221 case 's':
2222 {
2223 char *s = (char *)va_arg(args, char *);
2224 if (field_width == -1)
2225 field_width = SHRT_MAX;
2226 /* first, skip leading white space in buffer */
2227 str = skip_spaces(str);
2228
2229 /* now copy until next white space */
2230 while (*str && !isspace(*str) && field_width--)
2231 *s++ = *str++;
2232 *s = '\0';
2233 num++;
2234 }
2235 continue;
2236 case 'o':
2237 base = 8;
2238 break;
2239 case 'x':
2240 case 'X':
2241 base = 16;
2242 break;
2243 case 'i':
2244 base = 0;
2245 case 'd':
2246 is_sign = 1;
2247 case 'u':
2248 break;
2249 case '%':
2250 /* looking for '%' in str */
2251 if (*str++ != '%')
2252 return num;
2253 continue;
2254 default:
2255 /* invalid format; stop here */
2256 return num;
2257 }
2258
2259 /* have some sort of integer conversion.
2260 * first, skip white space in buffer.
2261 */
2262 str = skip_spaces(str);
2263
2264 digit = *str;
2265 if (is_sign && digit == '-')
2266 digit = *(str + 1);
2267
2268 if (!digit
2269 || (base == 16 && !isxdigit(digit))
2270 || (base == 10 && !isdigit(digit))
2271 || (base == 8 && (!isdigit(digit) || digit > '7'))
2272 || (base == 0 && !isdigit(digit)))
2273 break;
2274
2275 if (is_sign)
2276 val.s = qualifier != 'L' ?
2277 simple_strtol(str, &next, base) :
2278 simple_strtoll(str, &next, base);
2279 else
2280 val.u = qualifier != 'L' ?
2281 simple_strtoul(str, &next, base) :
2282 simple_strtoull(str, &next, base);
2283
2284 if (field_width > 0 && next - str > field_width) {
2285 if (base == 0)
2286 _parse_integer_fixup_radix(str, &base);
2287 while (next - str > field_width) {
2288 if (is_sign)
2289 val.s = div_s64(val.s, base);
2290 else
2291 val.u = div_u64(val.u, base);
2292 --next;
2293 }
2294 }
2295
2296 switch (qualifier) {
2297 case 'H': /* that's 'hh' in format */
2298 if (is_sign)
2299 *va_arg(args, signed char *) = val.s;
2300 else
2301 *va_arg(args, unsigned char *) = val.u;
2302 break;
2303 case 'h':
2304 if (is_sign)
2305 *va_arg(args, short *) = val.s;
2306 else
2307 *va_arg(args, unsigned short *) = val.u;
2308 break;
2309 case 'l':
2310 if (is_sign)
2311 *va_arg(args, long *) = val.s;
2312 else
2313 *va_arg(args, unsigned long *) = val.u;
2314 break;
2315 case 'L':
2316 if (is_sign)
2317 *va_arg(args, long long *) = val.s;
2318 else
2319 *va_arg(args, unsigned long long *) = val.u;
2320 break;
2321 case 'Z':
2322 case 'z':
2323 *va_arg(args, size_t *) = val.u;
2324 break;
2325 default:
2326 if (is_sign)
2327 *va_arg(args, int *) = val.s;
2328 else
2329 *va_arg(args, unsigned int *) = val.u;
2330 break;
2331 }
2332 num++;
2333
2334 if (!next)
2335 break;
2336 str = next;
2337 }
2338
2339 return num;
2340 }
2341 EXPORT_SYMBOL(vsscanf);
2342
2343 /**
2344 * sscanf - Unformat a buffer into a list of arguments
2345 * @buf: input buffer
2346 * @fmt: formatting of buffer
2347 * @...: resulting arguments
2348 */
2349 int sscanf(const char *buf, const char *fmt, ...)
2350 {
2351 va_list args;
2352 int i;
2353
2354 va_start(args, fmt);
2355 i = vsscanf(buf, fmt, args);
2356 va_end(args);
2357
2358 return i;
2359 }
2360 EXPORT_SYMBOL(sscanf);