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