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