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