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