Subversion Repositories Kolibri OS

Rev

Rev 5270 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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.  
  26. #include <linux/math64.h>
  27. #include <linux/ioport.h>
  28. #include <linux/export.h>
  29.  
  30. #include <asm/div64.h>
  31. #include <asm/page.h>           /* for PAGE_SIZE */
  32.  
  33. #define ZERO_SIZE_PTR ((void *)16)
  34.  
  35. #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
  36.                                 (unsigned long)ZERO_SIZE_PTR)
  37.  
  38. #ifndef dereference_function_descriptor
  39. #define dereference_function_descriptor(p) (p)
  40. #endif
  41.  
  42. #define KSTRTOX_OVERFLOW        (1U << 31)
  43.  
  44. const char hex_asc[] = "0123456789abcdef";
  45. const char hex_asc_upper[] = "0123456789ABCDEF";
  46.  
  47. /* Works only for digits and letters, but small and fast */
  48. #define TOLOWER(x) ((x) | 0x20)
  49.  
  50.  
  51. char *skip_spaces(const char *str)
  52. {
  53.         while (isspace(*str))
  54.                 ++str;
  55.         return (char *)str;
  56. }
  57. EXPORT_SYMBOL(skip_spaces);
  58.  
  59. const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
  60. {
  61.     if (*base == 0) {
  62.         if (s[0] == '0') {
  63.             if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
  64.                 *base = 16;
  65.             else
  66.                 *base = 8;
  67.         } else
  68.             *base = 10;
  69.     }
  70.     if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
  71.         s += 2;
  72.     return s;
  73. }
  74.  
  75. /*
  76.  * Convert non-negative integer string representation in explicitly given radix
  77.  * to an integer.
  78.  * Return number of characters consumed maybe or-ed with overflow bit.
  79.  * If overflow occurs, result integer (incorrect) is still returned.
  80.  *
  81.  * Don't you dare use this function.
  82.  */
  83. unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
  84. {
  85.     unsigned long long res;
  86.     unsigned int rv;
  87.     int overflow;
  88.  
  89.     res = 0;
  90.     rv = 0;
  91.     overflow = 0;
  92.     while (*s) {
  93.         unsigned int val;
  94.  
  95.         if ('0' <= *s && *s <= '9')
  96.             val = *s - '0';
  97.         else if ('a' <= _tolower(*s) && _tolower(*s) <= 'f')
  98.             val = _tolower(*s) - 'a' + 10;
  99.         else
  100.             break;
  101.  
  102.         if (val >= base)
  103.             break;
  104.         /*
  105.          * Check for overflow only if we are within range of
  106.          * it in the max base we support (16)
  107.          */
  108.         if (unlikely(res & (~0ull << 60))) {
  109.             if (res > div_u64(ULLONG_MAX - val, base))
  110.                 overflow = 1;
  111.         }
  112.         res = res * base + val;
  113.         rv++;
  114.         s++;
  115.     }
  116.     *p = res;
  117.     if (overflow)
  118.         rv |= KSTRTOX_OVERFLOW;
  119.     return rv;
  120. }
  121.  
  122.  
  123. /**
  124.  * simple_strtoull - convert a string to an unsigned long long
  125.  * @cp: The start of the string
  126.  * @endp: A pointer to the end of the parsed string will be placed here
  127.  * @base: The number base to use
  128.  *
  129.  * This function is obsolete. Please use kstrtoull instead.
  130.  */
  131. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  132. {
  133.         unsigned long long result;
  134.         unsigned int rv;
  135.  
  136.         cp = _parse_integer_fixup_radix(cp, &base);
  137.         rv = _parse_integer(cp, base, &result);
  138.         /* FIXME */
  139.         cp += (rv & ~KSTRTOX_OVERFLOW);
  140.  
  141.         if (endp)
  142.                 *endp = (char *)cp;
  143.  
  144.         return result;
  145. }
  146. EXPORT_SYMBOL(simple_strtoull);
  147.  
  148. /**
  149.  * simple_strtoul - convert a string to an unsigned long
  150.  * @cp: The start of the string
  151.  * @endp: A pointer to the end of the parsed string will be placed here
  152.  * @base: The number base to use
  153.  *
  154.  * This function is obsolete. Please use kstrtoul instead.
  155.  */
  156. unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
  157. {
  158.         return simple_strtoull(cp, endp, base);
  159. }
  160. EXPORT_SYMBOL(simple_strtoul);
  161.  
  162. /**
  163.  * simple_strtol - convert a string to a signed long
  164.  * @cp: The start of the string
  165.  * @endp: A pointer to the end of the parsed string will be placed here
  166.  * @base: The number base to use
  167.  *
  168.  * This function is obsolete. Please use kstrtol instead.
  169.  */
  170. long simple_strtol(const char *cp, char **endp, unsigned int base)
  171. {
  172.         if (*cp == '-')
  173.                 return -simple_strtoul(cp + 1, endp, base);
  174.  
  175.         return simple_strtoul(cp, endp, base);
  176. }
  177. EXPORT_SYMBOL(simple_strtol);
  178.  
  179. /**
  180.  * simple_strtoll - convert a string to a signed long long
  181.  * @cp: The start of the string
  182.  * @endp: A pointer to the end of the parsed string will be placed here
  183.  * @base: The number base to use
  184.  *
  185.  * This function is obsolete. Please use kstrtoll instead.
  186.  */
  187. long long simple_strtoll(const char *cp, char **endp, unsigned int base)
  188. {
  189.         if (*cp == '-')
  190.                 return -simple_strtoull(cp + 1, endp, base);
  191.  
  192.         return simple_strtoull(cp, endp, base);
  193. }
  194. EXPORT_SYMBOL(simple_strtoll);
  195.  
  196. static noinline_for_stack
  197. int skip_atoi(const char **s)
  198. {
  199.         int i = 0;
  200.  
  201.         do {
  202.                 i = i*10 + *((*s)++) - '0';
  203.         } while (isdigit(**s));
  204.  
  205.         return i;
  206. }
  207.  
  208. /*
  209.  * Decimal conversion is by far the most typical, and is used for
  210.  * /proc and /sys data. This directly impacts e.g. top performance
  211.  * with many processes running. We optimize it for speed by emitting
  212.  * two characters at a time, using a 200 byte lookup table. This
  213.  * roughly halves the number of multiplications compared to computing
  214.  * the digits one at a time. Implementation strongly inspired by the
  215.  * previous version, which in turn used ideas described at
  216.  * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
  217.  * from the author, Douglas W. Jones).
  218.  *
  219.  * It turns out there is precisely one 26 bit fixed-point
  220.  * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
  221.  * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
  222.  * range happens to be somewhat larger (x <= 1073741898), but that's
  223.  * irrelevant for our purpose.
  224.  *
  225.  * For dividing a number in the range [10^4, 10^6-1] by 100, we still
  226.  * need a 32x32->64 bit multiply, so we simply use the same constant.
  227.  *
  228.  * For dividing a number in the range [100, 10^4-1] by 100, there are
  229.  * several options. The simplest is (x * 0x147b) >> 19, which is valid
  230.  * for all x <= 43698.
  231.  */
  232.  
  233. static const u16 decpair[100] = {
  234. #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
  235.         _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
  236.         _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
  237.         _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
  238.         _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
  239.         _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
  240.         _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
  241.         _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
  242.         _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
  243.         _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
  244.         _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
  245. #undef _
  246. };
  247.  
  248. /*
  249.  * This will print a single '0' even if r == 0, since we would
  250.  * immediately jump to out_r where two 0s would be written but only
  251.  * one of them accounted for in buf. This is needed by ip4_string
  252.  * below. All other callers pass a non-zero value of r.
  253. */
  254. static noinline_for_stack
  255. char *put_dec_trunc8(char *buf, unsigned r)
  256. {
  257.         unsigned q;
  258.  
  259.         /* 1 <= r < 10^8 */
  260.         if (r < 100)
  261.                 goto out_r;
  262.  
  263.         /* 100 <= r < 10^8 */
  264.         q = (r * (u64)0x28f5c29) >> 32;
  265.         *((u16 *)buf) = decpair[r - 100*q];
  266.         buf += 2;
  267.  
  268.         /* 1 <= q < 10^6 */
  269.         if (q < 100)
  270.                 goto out_q;
  271.  
  272.         /*  100 <= q < 10^6 */
  273.         r = (q * (u64)0x28f5c29) >> 32;
  274.         *((u16 *)buf) = decpair[q - 100*r];
  275.         buf += 2;
  276.  
  277.         /* 1 <= r < 10^4 */
  278.         if (r < 100)
  279.                 goto out_r;
  280.  
  281.         /* 100 <= r < 10^4 */
  282.         q = (r * 0x147b) >> 19;
  283.         *((u16 *)buf) = decpair[r - 100*q];
  284.         buf += 2;
  285. out_q:
  286.         /* 1 <= q < 100 */
  287.         r = q;
  288. out_r:
  289.         /* 1 <= r < 100 */
  290.         *((u16 *)buf) = decpair[r];
  291.         buf += r < 10 ? 1 : 2;
  292.         return buf;
  293. }
  294.  
  295. #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
  296. static noinline_for_stack
  297. char *put_dec_full8(char *buf, unsigned r)
  298. {
  299.         unsigned q;
  300.  
  301.         /* 0 <= r < 10^8 */
  302.         q = (r * (u64)0x28f5c29) >> 32;
  303.         *((u16 *)buf) = decpair[r - 100*q];
  304.         buf += 2;
  305.  
  306.         /* 0 <= q < 10^6 */
  307.         r = (q * (u64)0x28f5c29) >> 32;
  308.         *((u16 *)buf) = decpair[q - 100*r];
  309.         buf += 2;
  310.  
  311.         /* 0 <= r < 10^4 */
  312.         q = (r * 0x147b) >> 19;
  313.         *((u16 *)buf) = decpair[r - 100*q];
  314.         buf += 2;
  315.  
  316.         /* 0 <= q < 100 */
  317.         *((u16 *)buf) = decpair[q];
  318.         buf += 2;
  319.         return buf;
  320. }
  321.  
  322. static noinline_for_stack
  323. char *put_dec(char *buf, unsigned long long n)
  324. {
  325.                 if (n >= 100*1000*1000)
  326.                 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
  327.         /* 1 <= n <= 1.6e11 */
  328.         if (n >= 100*1000*1000)
  329.                 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
  330.         /* 1 <= n < 1e8 */
  331.         return put_dec_trunc8(buf, n);
  332. }
  333.  
  334. #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
  335.  
  336. static void
  337. put_dec_full4(char *buf, unsigned r)
  338. {
  339.         unsigned q;
  340.  
  341.         /* 0 <= r < 10^4 */
  342.         q = (r * 0x147b) >> 19;
  343.         *((u16 *)buf) = decpair[r - 100*q];
  344.         buf += 2;
  345.         /* 0 <= q < 100 */
  346.         *((u16 *)buf) = decpair[q];
  347. }
  348.  
  349. /*
  350.  * Call put_dec_full4 on x % 10000, return x / 10000.
  351.  * The approximation x/10000 == (x * 0x346DC5D7) >> 43
  352.  * holds for all x < 1,128,869,999.  The largest value this
  353.  * helper will ever be asked to convert is 1,125,520,955.
  354.  * (second call in the put_dec code, assuming n is all-ones).
  355.  */
  356. static noinline_for_stack
  357. unsigned put_dec_helper4(char *buf, unsigned x)
  358. {
  359.         uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
  360.  
  361.         put_dec_full4(buf, x - q * 10000);
  362.         return q;
  363. }
  364.  
  365. /* Based on code by Douglas W. Jones found at
  366.  * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
  367.  * (with permission from the author).
  368.  * Performs no 64-bit division and hence should be fast on 32-bit machines.
  369.  */
  370. static
  371. char *put_dec(char *buf, unsigned long long n)
  372. {
  373.         uint32_t d3, d2, d1, q, h;
  374.  
  375.         if (n < 100*1000*1000)
  376.                 return put_dec_trunc8(buf, n);
  377.  
  378.         d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
  379.         h   = (n >> 32);
  380.         d2  = (h      ) & 0xffff;
  381.         d3  = (h >> 16); /* implicit "& 0xffff" */
  382.  
  383.         /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
  384.              = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
  385.         q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
  386.         q = put_dec_helper4(buf, q);
  387.  
  388.         q += 7671 * d3 + 9496 * d2 + 6 * d1;
  389.         q = put_dec_helper4(buf+4, q);
  390.  
  391.         q += 4749 * d3 + 42 * d2;
  392.         q = put_dec_helper4(buf+8, q);
  393.  
  394.         q += 281 * d3;
  395.         buf += 12;
  396.         if (q)
  397.                 buf = put_dec_trunc8(buf, q);
  398.         else while (buf[-1] == '0')
  399.                 --buf;
  400.  
  401.         return buf;
  402. }
  403.  
  404. #endif
  405.  
  406. /*
  407.  * Convert passed number to decimal string.
  408.  * Returns the length of string.  On buffer overflow, returns 0.
  409.  *
  410.  * If speed is not important, use snprintf(). It's easy to read the code.
  411.  */
  412. int num_to_str(char *buf, int size, unsigned long long num)
  413. {
  414.         /* put_dec requires 2-byte alignment of the buffer. */
  415.         char tmp[sizeof(num) * 3] __aligned(2);
  416.         int idx, len;
  417.  
  418.         /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
  419.         if (num <= 9) {
  420.                 tmp[0] = '0' + num;
  421.                 len = 1;
  422.         } else {
  423.                 len = put_dec(tmp, num) - tmp;
  424.         }
  425.  
  426.         if (len > size)
  427.                 return 0;
  428.         for (idx = 0; idx < len; ++idx)
  429.                 buf[idx] = tmp[len - idx - 1];
  430.         return len;
  431. }
  432.  
  433. #define SIGN    1               /* unsigned/signed, must be 1 */
  434. #define LEFT    2               /* left justified */
  435. #define PLUS    4               /* show plus */
  436. #define SPACE   8               /* space if plus */
  437. #define ZEROPAD 16              /* pad with zero, must be 16 == '0' - ' ' */
  438. #define SMALL   32              /* use lowercase in hex (must be 32 == 0x20) */
  439. #define SPECIAL 64              /* prefix hex with "0x", octal with "0" */
  440.  
  441. enum format_type {
  442.         FORMAT_TYPE_NONE, /* Just a string part */
  443.         FORMAT_TYPE_WIDTH,
  444.         FORMAT_TYPE_PRECISION,
  445.         FORMAT_TYPE_CHAR,
  446.         FORMAT_TYPE_STR,
  447.         FORMAT_TYPE_PTR,
  448.         FORMAT_TYPE_PERCENT_CHAR,
  449.         FORMAT_TYPE_INVALID,
  450.         FORMAT_TYPE_LONG_LONG,
  451.         FORMAT_TYPE_ULONG,
  452.         FORMAT_TYPE_LONG,
  453.         FORMAT_TYPE_UBYTE,
  454.         FORMAT_TYPE_BYTE,
  455.         FORMAT_TYPE_USHORT,
  456.         FORMAT_TYPE_SHORT,
  457.         FORMAT_TYPE_UINT,
  458.         FORMAT_TYPE_INT,
  459.         FORMAT_TYPE_SIZE_T,
  460.         FORMAT_TYPE_PTRDIFF
  461. };
  462.  
  463. struct printf_spec {
  464.         u8      type;           /* format_type enum */
  465.         u8      flags;          /* flags to number() */
  466.         u8      base;           /* number base, 8, 10 or 16 only */
  467.         u8      qualifier;      /* number qualifier, one of 'hHlLtzZ' */
  468.         s16     field_width;    /* width of output field */
  469.         s16     precision;      /* # of digits/chars */
  470. };
  471.  
  472. static noinline_for_stack
  473. char *number(char *buf, char *end, unsigned long long num,
  474.              struct printf_spec spec)
  475. {
  476.         /* put_dec requires 2-byte alignment of the buffer. */
  477.         char tmp[3 * sizeof(num)] __aligned(2);
  478.         char sign;
  479.         char locase;
  480.         int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
  481.         int i;
  482.         bool is_zero = num == 0LL;
  483.  
  484.         /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  485.          * produces same digits or (maybe lowercased) letters */
  486.         locase = (spec.flags & SMALL);
  487.         if (spec.flags & LEFT)
  488.                 spec.flags &= ~ZEROPAD;
  489.         sign = 0;
  490.         if (spec.flags & SIGN) {
  491.                 if ((signed long long)num < 0) {
  492.                         sign = '-';
  493.                         num = -(signed long long)num;
  494.                         spec.field_width--;
  495.                 } else if (spec.flags & PLUS) {
  496.                         sign = '+';
  497.                         spec.field_width--;
  498.                 } else if (spec.flags & SPACE) {
  499.                         sign = ' ';
  500.                         spec.field_width--;
  501.                 }
  502.         }
  503.         if (need_pfx) {
  504.                 if (spec.base == 16)
  505.                         spec.field_width -= 2;
  506.                 else if (!is_zero)
  507.                         spec.field_width--;
  508.         }
  509.  
  510.         /* generate full string in tmp[], in reverse order */
  511.         i = 0;
  512.         if (num < spec.base)
  513.                 tmp[i++] = hex_asc_upper[num] | locase;
  514.         else if (spec.base != 10) { /* 8 or 16 */
  515.                 int mask = spec.base - 1;
  516.                 int shift = 3;
  517.  
  518.                 if (spec.base == 16)
  519.                         shift = 4;
  520.                 do {
  521.                         tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
  522.                         num >>= shift;
  523.                 } while (num);
  524.         } else { /* base 10 */
  525.                 i = put_dec(tmp, num) - tmp;
  526.         }
  527.  
  528.         /* printing 100 using %2d gives "100", not "00" */
  529.         if (i > spec.precision)
  530.                 spec.precision = i;
  531.         /* leading space padding */
  532.         spec.field_width -= spec.precision;
  533.         if (!(spec.flags & (ZEROPAD | LEFT))) {
  534.                 while (--spec.field_width >= 0) {
  535.                         if (buf < end)
  536.                                 *buf = ' ';
  537.                         ++buf;
  538.                 }
  539.         }
  540.         /* sign */
  541.         if (sign) {
  542.                 if (buf < end)
  543.                         *buf = sign;
  544.                 ++buf;
  545.         }
  546.         /* "0x" / "0" prefix */
  547.         if (need_pfx) {
  548.                 if (spec.base == 16 || !is_zero) {
  549.                 if (buf < end)
  550.                         *buf = '0';
  551.                 ++buf;
  552.                 }
  553.                 if (spec.base == 16) {
  554.                         if (buf < end)
  555.                                 *buf = ('X' | locase);
  556.                         ++buf;
  557.                 }
  558.         }
  559.         /* zero or space padding */
  560.         if (!(spec.flags & LEFT)) {
  561.                 char c = ' ' + (spec.flags & ZEROPAD);
  562.                 BUILD_BUG_ON(' ' + ZEROPAD != '0');
  563.                 while (--spec.field_width >= 0) {
  564.                         if (buf < end)
  565.                                 *buf = c;
  566.                         ++buf;
  567.                 }
  568.         }
  569.         /* hmm even more zero padding? */
  570.         while (i <= --spec.precision) {
  571.                 if (buf < end)
  572.                         *buf = '0';
  573.                 ++buf;
  574.         }
  575.         /* actual digits of result */
  576.         while (--i >= 0) {
  577.                 if (buf < end)
  578.                         *buf = tmp[i];
  579.                 ++buf;
  580.         }
  581.         /* trailing space padding */
  582.         while (--spec.field_width >= 0) {
  583.                 if (buf < end)
  584.                         *buf = ' ';
  585.                 ++buf;
  586.         }
  587.  
  588.         return buf;
  589. }
  590.  
  591. static noinline_for_stack
  592. char *string(char *buf, char *end, const char *s, struct printf_spec spec)
  593. {
  594.         int len, i;
  595.  
  596.         if ((unsigned long)s < PAGE_SIZE)
  597.                 s = "(null)";
  598.  
  599.         len = strnlen(s, spec.precision);
  600.  
  601.         if (!(spec.flags & LEFT)) {
  602.                 while (len < spec.field_width--) {
  603.                         if (buf < end)
  604.                                 *buf = ' ';
  605.                         ++buf;
  606.                 }
  607.         }
  608.         for (i = 0; i < len; ++i) {
  609.                 if (buf < end)
  610.                         *buf = *s;
  611.                 ++buf; ++s;
  612.         }
  613.         while (len < spec.field_width--) {
  614.                 if (buf < end)
  615.                         *buf = ' ';
  616.                 ++buf;
  617.         }
  618.  
  619.         return buf;
  620. }
  621.  
  622. static noinline_for_stack
  623. char *symbol_string(char *buf, char *end, void *ptr,
  624.                     struct printf_spec spec, const char *fmt)
  625. {
  626.         unsigned long value;
  627. #ifdef CONFIG_KALLSYMS
  628.         char sym[KSYM_SYMBOL_LEN];
  629. #endif
  630.  
  631.         if (fmt[1] == 'R')
  632.                 ptr = __builtin_extract_return_addr(ptr);
  633.         value = (unsigned long)ptr;
  634.  
  635. #ifdef CONFIG_KALLSYMS
  636.         if (*fmt == 'B')
  637.                 sprint_backtrace(sym, value);
  638.         else if (*fmt != 'f' && *fmt != 's')
  639.                 sprint_symbol(sym, value);
  640.         else
  641.                 sprint_symbol_no_offset(sym, value);
  642.  
  643.         return string(buf, end, sym, spec);
  644. #else
  645.         spec.field_width = 2 * sizeof(void *);
  646.         spec.flags |= SPECIAL | SMALL | ZEROPAD;
  647.         spec.base = 16;
  648.  
  649.         return number(buf, end, value, spec);
  650. #endif
  651. }
  652.  
  653. static noinline_for_stack
  654. char *resource_string(char *buf, char *end, struct resource *res,
  655.                       struct printf_spec spec, const char *fmt)
  656. {
  657. #ifndef IO_RSRC_PRINTK_SIZE
  658. #define IO_RSRC_PRINTK_SIZE     6
  659. #endif
  660.  
  661. #ifndef MEM_RSRC_PRINTK_SIZE
  662. #define MEM_RSRC_PRINTK_SIZE    10
  663. #endif
  664.         static const struct printf_spec io_spec = {
  665.                 .base = 16,
  666.                 .field_width = IO_RSRC_PRINTK_SIZE,
  667.                 .precision = -1,
  668.                 .flags = SPECIAL | SMALL | ZEROPAD,
  669.         };
  670.         static const struct printf_spec mem_spec = {
  671.                 .base = 16,
  672.                 .field_width = MEM_RSRC_PRINTK_SIZE,
  673.                 .precision = -1,
  674.                 .flags = SPECIAL | SMALL | ZEROPAD,
  675.         };
  676.         static const struct printf_spec bus_spec = {
  677.                 .base = 16,
  678.                 .field_width = 2,
  679.                 .precision = -1,
  680.                 .flags = SMALL | ZEROPAD,
  681.         };
  682.         static const struct printf_spec dec_spec = {
  683.                 .base = 10,
  684.                 .precision = -1,
  685.                 .flags = 0,
  686.         };
  687.         static const struct printf_spec str_spec = {
  688.                 .field_width = -1,
  689.                 .precision = 10,
  690.                 .flags = LEFT,
  691.         };
  692.         static const struct printf_spec flag_spec = {
  693.                 .base = 16,
  694.                 .precision = -1,
  695.                 .flags = SPECIAL | SMALL,
  696.         };
  697.  
  698.         /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
  699.          * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
  700. #define RSRC_BUF_SIZE           ((2 * sizeof(resource_size_t)) + 4)
  701. #define FLAG_BUF_SIZE           (2 * sizeof(res->flags))
  702. #define DECODED_BUF_SIZE        sizeof("[mem - 64bit pref window disabled]")
  703. #define RAW_BUF_SIZE            sizeof("[mem - flags 0x]")
  704.         char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
  705.                      2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
  706.  
  707.         char *p = sym, *pend = sym + sizeof(sym);
  708.         int decode = (fmt[0] == 'R') ? 1 : 0;
  709.         const struct printf_spec *specp;
  710.  
  711.         *p++ = '[';
  712.         if (res->flags & IORESOURCE_IO) {
  713.                 p = string(p, pend, "io  ", str_spec);
  714.                 specp = &io_spec;
  715.         } else if (res->flags & IORESOURCE_MEM) {
  716.                 p = string(p, pend, "mem ", str_spec);
  717.                 specp = &mem_spec;
  718.         } else if (res->flags & IORESOURCE_IRQ) {
  719.                 p = string(p, pend, "irq ", str_spec);
  720.                 specp = &dec_spec;
  721.         } else if (res->flags & IORESOURCE_DMA) {
  722.                 p = string(p, pend, "dma ", str_spec);
  723.                 specp = &dec_spec;
  724.         } else if (res->flags & IORESOURCE_BUS) {
  725.                 p = string(p, pend, "bus ", str_spec);
  726.                 specp = &bus_spec;
  727.         } else {
  728.                 p = string(p, pend, "??? ", str_spec);
  729.                 specp = &mem_spec;
  730.                 decode = 0;
  731.         }
  732.         if (decode && res->flags & IORESOURCE_UNSET) {
  733.                 p = string(p, pend, "size ", str_spec);
  734.                 p = number(p, pend, resource_size(res), *specp);
  735.         } else {
  736.         p = number(p, pend, res->start, *specp);
  737.         if (res->start != res->end) {
  738.                 *p++ = '-';
  739.                 p = number(p, pend, res->end, *specp);
  740.                 }
  741.         }
  742.         if (decode) {
  743.                 if (res->flags & IORESOURCE_MEM_64)
  744.                         p = string(p, pend, " 64bit", str_spec);
  745.                 if (res->flags & IORESOURCE_PREFETCH)
  746.                         p = string(p, pend, " pref", str_spec);
  747.                 if (res->flags & IORESOURCE_WINDOW)
  748.                         p = string(p, pend, " window", str_spec);
  749.                 if (res->flags & IORESOURCE_DISABLED)
  750.                         p = string(p, pend, " disabled", str_spec);
  751.         } else {
  752.                 p = string(p, pend, " flags ", str_spec);
  753.                 p = number(p, pend, res->flags, flag_spec);
  754.         }
  755.         *p++ = ']';
  756.         *p = '\0';
  757.  
  758.         return string(buf, end, sym, spec);
  759. }
  760.  
  761. static noinline_for_stack
  762. char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
  763.                  const char *fmt)
  764. {
  765.         int i, len = 1;         /* if we pass '%ph[CDN]', field width remains
  766.                                    negative value, fallback to the default */
  767.         char separator;
  768.  
  769.         if (spec.field_width == 0)
  770.                 /* nothing to print */
  771.                 return buf;
  772.  
  773.         if (ZERO_OR_NULL_PTR(addr))
  774.                 /* NULL pointer */
  775.                 return string(buf, end, NULL, spec);
  776.  
  777.         switch (fmt[1]) {
  778.         case 'C':
  779.                 separator = ':';
  780.                 break;
  781.         case 'D':
  782.                 separator = '-';
  783.                 break;
  784.         case 'N':
  785.                 separator = 0;
  786.                 break;
  787.         default:
  788.                 separator = ' ';
  789.                 break;
  790.         }
  791.  
  792.         if (spec.field_width > 0)
  793.                 len = min_t(int, spec.field_width, 64);
  794.  
  795.         for (i = 0; i < len; ++i) {
  796.                 if (buf < end)
  797.                         *buf = hex_asc_hi(addr[i]);
  798.                 ++buf;
  799.                 if (buf < end)
  800.                         *buf = hex_asc_lo(addr[i]);
  801.                 ++buf;
  802.  
  803.                 if (separator && i != len - 1) {
  804.                         if (buf < end)
  805.                                 *buf = separator;
  806.                         ++buf;
  807.                 }
  808.         }
  809.  
  810.         return buf;
  811. }
  812.  
  813. static noinline_for_stack
  814. char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
  815.                     struct printf_spec spec, const char *fmt)
  816. {
  817.         const int CHUNKSZ = 32;
  818.         int nr_bits = max_t(int, spec.field_width, 0);
  819.         int i, chunksz;
  820.         bool first = true;
  821.  
  822.         /* reused to print numbers */
  823.         spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
  824.  
  825.         chunksz = nr_bits & (CHUNKSZ - 1);
  826.         if (chunksz == 0)
  827.                 chunksz = CHUNKSZ;
  828.  
  829.         i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
  830.         for (; i >= 0; i -= CHUNKSZ) {
  831.                 u32 chunkmask, val;
  832.                 int word, bit;
  833.  
  834.                 chunkmask = ((1ULL << chunksz) - 1);
  835.                 word = i / BITS_PER_LONG;
  836.                 bit = i % BITS_PER_LONG;
  837.                 val = (bitmap[word] >> bit) & chunkmask;
  838.  
  839.                 if (!first) {
  840.                         if (buf < end)
  841.                                 *buf = ',';
  842.                         buf++;
  843.                 }
  844.                 first = false;
  845.  
  846.                 spec.field_width = DIV_ROUND_UP(chunksz, 4);
  847.                 buf = number(buf, end, val, spec);
  848.  
  849.                 chunksz = CHUNKSZ;
  850.         }
  851.         return buf;
  852. }
  853.  
  854. static noinline_for_stack
  855. char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
  856.                          struct printf_spec spec, const char *fmt)
  857. {
  858.         int nr_bits = max_t(int, spec.field_width, 0);
  859.         /* current bit is 'cur', most recently seen range is [rbot, rtop] */
  860.         int cur, rbot, rtop;
  861.         bool first = true;
  862.  
  863.         /* reused to print numbers */
  864.         spec = (struct printf_spec){ .base = 10 };
  865.  
  866.         rbot = cur = find_first_bit(bitmap, nr_bits);
  867.         while (cur < nr_bits) {
  868.                 rtop = cur;
  869.                 cur = find_next_bit(bitmap, nr_bits, cur + 1);
  870.                 if (cur < nr_bits && cur <= rtop + 1)
  871.                         continue;
  872.  
  873.                 if (!first) {
  874.                         if (buf < end)
  875.                                 *buf = ',';
  876.                         buf++;
  877.                 }
  878.                 first = false;
  879.  
  880.                 buf = number(buf, end, rbot, spec);
  881.                 if (rbot < rtop) {
  882.                         if (buf < end)
  883.                                 *buf = '-';
  884.                         buf++;
  885.  
  886.                         buf = number(buf, end, rtop, spec);
  887.         }
  888.  
  889.                 rbot = cur;
  890.         }
  891.         return buf;
  892. }
  893.  
  894. static noinline_for_stack
  895. char *mac_address_string(char *buf, char *end, u8 *addr,
  896.                          struct printf_spec spec, const char *fmt)
  897. {
  898.         char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
  899.         char *p = mac_addr;
  900.         int i;
  901.         char separator;
  902.         bool reversed = false;
  903.  
  904.         switch (fmt[1]) {
  905.         case 'F':
  906.                 separator = '-';
  907.                 break;
  908.  
  909.         case 'R':
  910.                 reversed = true;
  911.                 /* fall through */
  912.  
  913.         default:
  914.                 separator = ':';
  915.                 break;
  916.         }
  917.  
  918.         for (i = 0; i < 6; i++) {
  919.                 if (reversed)
  920.                         p = hex_byte_pack(p, addr[5 - i]);
  921.                 else
  922.                         p = hex_byte_pack(p, addr[i]);
  923.  
  924.                 if (fmt[0] == 'M' && i != 5)
  925.                         *p++ = separator;
  926.         }
  927.         *p = '\0';
  928.  
  929.         return string(buf, end, mac_addr, spec);
  930. }
  931.  
  932. static noinline_for_stack
  933. char *ip4_string(char *p, const u8 *addr, const char *fmt)
  934. {
  935.         int i;
  936.         bool leading_zeros = (fmt[0] == 'i');
  937.         int index;
  938.         int step;
  939.  
  940.         switch (fmt[2]) {
  941.         case 'h':
  942. #ifdef __BIG_ENDIAN
  943.                 index = 0;
  944.                 step = 1;
  945. #else
  946.                 index = 3;
  947.                 step = -1;
  948. #endif
  949.                 break;
  950.         case 'l':
  951.                 index = 3;
  952.                 step = -1;
  953.                 break;
  954.         case 'n':
  955.         case 'b':
  956.         default:
  957.                 index = 0;
  958.                 step = 1;
  959.                 break;
  960.         }
  961.         for (i = 0; i < 4; i++) {
  962.                 char temp[4] __aligned(2);      /* hold each IP quad in reverse order */
  963.                 int digits = put_dec_trunc8(temp, addr[index]) - temp;
  964.                 if (leading_zeros) {
  965.                         if (digits < 3)
  966.                                 *p++ = '0';
  967.                         if (digits < 2)
  968.                                 *p++ = '0';
  969.                 }
  970.                 /* reverse the digits in the quad */
  971.                 while (digits--)
  972.                         *p++ = temp[digits];
  973.                 if (i < 3)
  974.                         *p++ = '.';
  975.                 index += step;
  976.         }
  977.         *p = '\0';
  978.  
  979.         return p;
  980. }
  981.  
  982.  
  983. static noinline_for_stack
  984. char *ip4_addr_string(char *buf, char *end, const u8 *addr,
  985.                       struct printf_spec spec, const char *fmt)
  986. {
  987.         char ip4_addr[sizeof("255.255.255.255")];
  988.  
  989.         ip4_string(ip4_addr, addr, fmt);
  990.  
  991.         return string(buf, end, ip4_addr, spec);
  992. }
  993.  
  994.  
  995. int kptr_restrict = 1;
  996.  
  997. /*
  998.  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
  999.  * by an extra set of alphanumeric characters that are extended format
  1000.  * specifiers.
  1001.  *
  1002.  * Right now we handle:
  1003.  *
  1004.  * - 'F' For symbolic function descriptor pointers with offset
  1005.  * - 'f' For simple symbolic function names without offset
  1006.  * - 'S' For symbolic direct pointers with offset
  1007.  * - 's' For symbolic direct pointers without offset
  1008.  * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
  1009.  * - 'B' For backtraced symbolic direct pointers with offset
  1010.  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
  1011.  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
  1012.  * - 'b[l]' For a bitmap, the number of bits is determined by the field
  1013.  *       width which must be explicitly specified either as part of the
  1014.  *       format string '%32b[l]' or through '%*b[l]', [l] selects
  1015.  *       range-list format instead of hex format
  1016.  * - 'M' For a 6-byte MAC address, it prints the address in the
  1017.  *       usual colon-separated hex notation
  1018.  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
  1019.  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
  1020.  *       with a dash-separated hex notation
  1021.  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
  1022.  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
  1023.  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
  1024.  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
  1025.  *       [S][pfs]
  1026.  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
  1027.  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
  1028.  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
  1029.  *       IPv6 omits the colons (01020304...0f)
  1030.  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
  1031.  *       [S][pfs]
  1032.  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
  1033.  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
  1034.  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
  1035.  * - 'I[6S]c' for IPv6 addresses printed as specified by
  1036.  *       http://tools.ietf.org/html/rfc5952
  1037.  * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
  1038.  *                of the following flags (see string_escape_mem() for the
  1039.  *                details):
  1040.  *                  a - ESCAPE_ANY
  1041.  *                  c - ESCAPE_SPECIAL
  1042.  *                  h - ESCAPE_HEX
  1043.  *                  n - ESCAPE_NULL
  1044.  *                  o - ESCAPE_OCTAL
  1045.  *                  p - ESCAPE_NP
  1046.  *                  s - ESCAPE_SPACE
  1047.  *                By default ESCAPE_ANY_NP is used.
  1048.  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
  1049.  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  1050.  *       Options for %pU are:
  1051.  *         b big endian lower case hex (default)
  1052.  *         B big endian UPPER case hex
  1053.  *         l little endian lower case hex
  1054.  *         L little endian UPPER case hex
  1055.  *           big endian output byte order is:
  1056.  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
  1057.  *           little endian output byte order is:
  1058.  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
  1059.  * - 'V' For a struct va_format which contains a format string * and va_list *,
  1060.  *       call vsnprintf(->format, *->va_list).
  1061.  *       Implements a "recursive vsnprintf".
  1062.  *       Do not use this feature without some mechanism to verify the
  1063.  *       correctness of the format string and va_list arguments.
  1064.  * - 'K' For a kernel pointer that should be hidden from unprivileged users
  1065.  * - 'NF' For a netdev_features_t
  1066.  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
  1067.  *            a certain separator (' ' by default):
  1068.  *              C colon
  1069.  *              D dash
  1070.  *              N no separator
  1071.  *            The maximum supported length is 64 bytes of the input. Consider
  1072.  *            to use print_hex_dump() for the larger input.
  1073.  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
  1074.  *           (default assumed to be phys_addr_t, passed by reference)
  1075.  * - 'd[234]' For a dentry name (optionally 2-4 last components)
  1076.  * - 'D[234]' Same as 'd' but for a struct file
  1077.  * - 'C' For a clock, it prints the name (Common Clock Framework) or address
  1078.  *       (legacy clock framework) of the clock
  1079.  * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
  1080.  *        (legacy clock framework) of the clock
  1081.  * - 'Cr' For a clock, it prints the current rate of the clock
  1082.  *
  1083.  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  1084.  * function pointers are really function descriptors, which contain a
  1085.  * pointer to the real address.
  1086.  */
  1087. static noinline_for_stack
  1088. char *pointer(const char *fmt, char *buf, char *end, void *ptr,
  1089.               struct printf_spec spec)
  1090. {
  1091.         int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
  1092.  
  1093.         if (!ptr && *fmt != 'K') {
  1094.                 /*
  1095.                  * Print (null) with the same width as a pointer so it makes
  1096.                  * tabular output look nice.
  1097.                  */
  1098.                 if (spec.field_width == -1)
  1099.                         spec.field_width = default_width;
  1100.                 return string(buf, end, "(null)", spec);
  1101.         }
  1102.  
  1103.         switch (*fmt) {
  1104.         case 'F':
  1105.         case 'f':
  1106.                 ptr = dereference_function_descriptor(ptr);
  1107.                 /* Fallthrough */
  1108.         case 'S':
  1109.         case 's':
  1110.         case 'B':
  1111.                 return symbol_string(buf, end, ptr, spec, fmt);
  1112.         case 'R':
  1113.         case 'r':
  1114.                 return resource_string(buf, end, ptr, spec, fmt);
  1115.         case 'h':
  1116.                 return hex_string(buf, end, ptr, spec, fmt);
  1117.         case 'M':                       /* Colon separated: 00:01:02:03:04:05 */
  1118.         case 'm':                       /* Contiguous: 000102030405 */
  1119.                                         /* [mM]F (FDDI) */
  1120.                                         /* [mM]R (Reverse order; Bluetooth) */
  1121.                 return mac_address_string(buf, end, ptr, spec, fmt);
  1122.         case 'I':                       /* Formatted IP supported
  1123.                                          * 4:   1.2.3.4
  1124.                                          * 6:   0001:0203:...:0708
  1125.                                          * 6c:  1::708 or 1::1.2.3.4
  1126.                                          */
  1127.         case 'i':                       /* Contiguous:
  1128.                                          * 4:   001.002.003.004
  1129.                                          * 6:   000102...0f
  1130.                                          */
  1131.                 switch (fmt[1]) {
  1132.                 case '4':
  1133.                         return ip4_addr_string(buf, end, ptr, spec, fmt);
  1134.                 }
  1135.                 break;
  1136.         case 'V':
  1137.                 {
  1138.                         va_list va;
  1139.  
  1140.                         va_copy(va, *((struct va_format *)ptr)->va);
  1141.                         buf += vsnprintf(buf, end > buf ? end - buf : 0,
  1142.                                          ((struct va_format *)ptr)->fmt, va);
  1143.                         va_end(va);
  1144.                         return buf;
  1145.                 }
  1146.  
  1147.         }
  1148.         spec.flags |= SMALL;
  1149.         if (spec.field_width == -1) {
  1150.                 spec.field_width = default_width;
  1151.                 spec.flags |= ZEROPAD;
  1152.         }
  1153.         spec.base = 16;
  1154.  
  1155.         return number(buf, end, (unsigned long) ptr, spec);
  1156. }
  1157.  
  1158. /*
  1159.  * Helper function to decode printf style format.
  1160.  * Each call decode a token from the format and return the
  1161.  * number of characters read (or likely the delta where it wants
  1162.  * to go on the next call).
  1163.  * The decoded token is returned through the parameters
  1164.  *
  1165.  * 'h', 'l', or 'L' for integer fields
  1166.  * 'z' support added 23/7/1999 S.H.
  1167.  * 'z' changed to 'Z' --davidm 1/25/99
  1168.  * 't' added for ptrdiff_t
  1169.  *
  1170.  * @fmt: the format string
  1171.  * @type of the token returned
  1172.  * @flags: various flags such as +, -, # tokens..
  1173.  * @field_width: overwritten width
  1174.  * @base: base of the number (octal, hex, ...)
  1175.  * @precision: precision of a number
  1176.  * @qualifier: qualifier of a number (long, size_t, ...)
  1177.  */
  1178. static noinline_for_stack
  1179. int format_decode(const char *fmt, struct printf_spec *spec)
  1180. {
  1181.         const char *start = fmt;
  1182.  
  1183.         /* we finished early by reading the field width */
  1184.         if (spec->type == FORMAT_TYPE_WIDTH) {
  1185.                 if (spec->field_width < 0) {
  1186.                         spec->field_width = -spec->field_width;
  1187.                         spec->flags |= LEFT;
  1188.                 }
  1189.                 spec->type = FORMAT_TYPE_NONE;
  1190.                 goto precision;
  1191.         }
  1192.  
  1193.         /* we finished early by reading the precision */
  1194.         if (spec->type == FORMAT_TYPE_PRECISION) {
  1195.                 if (spec->precision < 0)
  1196.                         spec->precision = 0;
  1197.  
  1198.                 spec->type = FORMAT_TYPE_NONE;
  1199.                 goto qualifier;
  1200.         }
  1201.  
  1202.         /* By default */
  1203.         spec->type = FORMAT_TYPE_NONE;
  1204.  
  1205.         for (; *fmt ; ++fmt) {
  1206.                 if (*fmt == '%')
  1207.                         break;
  1208.         }
  1209.  
  1210.         /* Return the current non-format string */
  1211.         if (fmt != start || !*fmt)
  1212.                 return fmt - start;
  1213.  
  1214.         /* Process flags */
  1215.         spec->flags = 0;
  1216.  
  1217.         while (1) { /* this also skips first '%' */
  1218.                 bool found = true;
  1219.  
  1220.                 ++fmt;
  1221.  
  1222.                 switch (*fmt) {
  1223.                 case '-': spec->flags |= LEFT;    break;
  1224.                 case '+': spec->flags |= PLUS;    break;
  1225.                 case ' ': spec->flags |= SPACE;   break;
  1226.                 case '#': spec->flags |= SPECIAL; break;
  1227.                 case '0': spec->flags |= ZEROPAD; break;
  1228.                 default:  found = false;
  1229.                 }
  1230.  
  1231.                 if (!found)
  1232.                         break;
  1233.         }
  1234.  
  1235.         /* get field width */
  1236.         spec->field_width = -1;
  1237.  
  1238.         if (isdigit(*fmt))
  1239.                 spec->field_width = skip_atoi(&fmt);
  1240.         else if (*fmt == '*') {
  1241.                 /* it's the next argument */
  1242.                 spec->type = FORMAT_TYPE_WIDTH;
  1243.                 return ++fmt - start;
  1244.         }
  1245.  
  1246. precision:
  1247.         /* get the precision */
  1248.         spec->precision = -1;
  1249.         if (*fmt == '.') {
  1250.                 ++fmt;
  1251.                 if (isdigit(*fmt)) {
  1252.                         spec->precision = skip_atoi(&fmt);
  1253.                         if (spec->precision < 0)
  1254.                                 spec->precision = 0;
  1255.                 } else if (*fmt == '*') {
  1256.                         /* it's the next argument */
  1257.                         spec->type = FORMAT_TYPE_PRECISION;
  1258.                         return ++fmt - start;
  1259.                 }
  1260.         }
  1261.  
  1262. qualifier:
  1263.         /* get the conversion qualifier */
  1264.         spec->qualifier = -1;
  1265.         if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
  1266.             _tolower(*fmt) == 'z' || *fmt == 't') {
  1267.                 spec->qualifier = *fmt++;
  1268.                 if (unlikely(spec->qualifier == *fmt)) {
  1269.                         if (spec->qualifier == 'l') {
  1270.                                 spec->qualifier = 'L';
  1271.                                 ++fmt;
  1272.                         } else if (spec->qualifier == 'h') {
  1273.                                 spec->qualifier = 'H';
  1274.                                 ++fmt;
  1275.                         }
  1276.                 }
  1277.         }
  1278.  
  1279.         /* default base */
  1280.         spec->base = 10;
  1281.         switch (*fmt) {
  1282.         case 'c':
  1283.                 spec->type = FORMAT_TYPE_CHAR;
  1284.                 return ++fmt - start;
  1285.  
  1286.         case 's':
  1287.                 spec->type = FORMAT_TYPE_STR;
  1288.                 return ++fmt - start;
  1289.  
  1290.         case 'p':
  1291.                 spec->type = FORMAT_TYPE_PTR;
  1292.                 return ++fmt - start;
  1293.  
  1294.         case '%':
  1295.                 spec->type = FORMAT_TYPE_PERCENT_CHAR;
  1296.                 return ++fmt - start;
  1297.  
  1298.         /* integer number formats - set up the flags and "break" */
  1299.         case 'o':
  1300.                 spec->base = 8;
  1301.                 break;
  1302.  
  1303.         case 'x':
  1304.                 spec->flags |= SMALL;
  1305.  
  1306.         case 'X':
  1307.                 spec->base = 16;
  1308.                 break;
  1309.  
  1310.         case 'd':
  1311.         case 'i':
  1312.                 spec->flags |= SIGN;
  1313.         case 'u':
  1314.                 break;
  1315.  
  1316.         case 'n':
  1317.                 /*
  1318.                  * Since %n poses a greater security risk than utility, treat
  1319.                  * it as an invalid format specifier. Warn about its use so
  1320.                  * that new instances don't get added.
  1321.                  */
  1322. //       WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt);
  1323.                 /* Fall-through */
  1324.  
  1325.         default:
  1326.                 spec->type = FORMAT_TYPE_INVALID;
  1327.                 return fmt - start;
  1328.         }
  1329.  
  1330.         if (spec->qualifier == 'L')
  1331.                 spec->type = FORMAT_TYPE_LONG_LONG;
  1332.         else if (spec->qualifier == 'l') {
  1333.                 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
  1334.                 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
  1335.         } else if (_tolower(spec->qualifier) == 'z') {
  1336.                 spec->type = FORMAT_TYPE_SIZE_T;
  1337.         } else if (spec->qualifier == 't') {
  1338.                 spec->type = FORMAT_TYPE_PTRDIFF;
  1339.         } else if (spec->qualifier == 'H') {
  1340.                 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
  1341.                 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
  1342.         } else if (spec->qualifier == 'h') {
  1343.                 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
  1344.                 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
  1345.         } else {
  1346.                 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
  1347.                 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
  1348.         }
  1349.  
  1350.         return ++fmt - start;
  1351. }
  1352.  
  1353. /**
  1354.  * vsnprintf - Format a string and place it in a buffer
  1355.  * @buf: The buffer to place the result into
  1356.  * @size: The size of the buffer, including the trailing null space
  1357.  * @fmt: The format string to use
  1358.  * @args: Arguments for the format string
  1359.  *
  1360.  * This function follows C99 vsnprintf, but has some extensions:
  1361.  * %pS output the name of a text symbol with offset
  1362.  * %ps output the name of a text symbol without offset
  1363.  * %pF output the name of a function pointer with its offset
  1364.  * %pf output the name of a function pointer without its offset
  1365.  * %pB output the name of a backtrace symbol with its offset
  1366.  * %pR output the address range in a struct resource with decoded flags
  1367.  * %pr output the address range in a struct resource with raw flags
  1368.  * %pb output the bitmap with field width as the number of bits
  1369.  * %pbl output the bitmap as range list with field width as the number of bits
  1370.  * %pM output a 6-byte MAC address with colons
  1371.  * %pMR output a 6-byte MAC address with colons in reversed order
  1372.  * %pMF output a 6-byte MAC address with dashes
  1373.  * %pm output a 6-byte MAC address without colons
  1374.  * %pmR output a 6-byte MAC address without colons in reversed order
  1375.  * %pI4 print an IPv4 address without leading zeros
  1376.  * %pi4 print an IPv4 address with leading zeros
  1377.  * %pI6 print an IPv6 address with colons
  1378.  * %pi6 print an IPv6 address without colons
  1379.  * %pI6c print an IPv6 address as specified by RFC 5952
  1380.  * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
  1381.  * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
  1382.  * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
  1383.  *   case.
  1384.  * %*pE[achnops] print an escaped buffer
  1385.  * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
  1386.  *           bytes of the input)
  1387.  * %pC output the name (Common Clock Framework) or address (legacy clock
  1388.  *     framework) of a clock
  1389.  * %pCn output the name (Common Clock Framework) or address (legacy clock
  1390.  *      framework) of a clock
  1391.  * %pCr output the current rate of a clock
  1392.  * %n is ignored
  1393.  *
  1394.  * ** Please update Documentation/printk-formats.txt when making changes **
  1395.  *
  1396.  * The return value is the number of characters which would
  1397.  * be generated for the given input, excluding the trailing
  1398.  * '\0', as per ISO C99. If you want to have the exact
  1399.  * number of characters written into @buf as return value
  1400.  * (not including the trailing '\0'), use vscnprintf(). If the
  1401.  * return is greater than or equal to @size, the resulting
  1402.  * string is truncated.
  1403.  *
  1404.  * If you're not already dealing with a va_list consider using snprintf().
  1405.  */
  1406. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1407. {
  1408.         unsigned long long num;
  1409.         char *str, *end;
  1410.         struct printf_spec spec = {0};
  1411.  
  1412.         /* Reject out-of-range values early.  Large positive sizes are
  1413.            used for unknown buffer sizes. */
  1414.         if (WARN_ON_ONCE(size > INT_MAX))
  1415.                 return 0;
  1416.  
  1417.         str = buf;
  1418.         end = buf + size;
  1419.  
  1420.         /* Make sure end is always >= buf */
  1421.         if (end < buf) {
  1422.                 end = ((void *)-1);
  1423.                 size = end - buf;
  1424.         }
  1425.  
  1426.         while (*fmt) {
  1427.                 const char *old_fmt = fmt;
  1428.                 int read = format_decode(fmt, &spec);
  1429.  
  1430.                 fmt += read;
  1431.  
  1432.                 switch (spec.type) {
  1433.                 case FORMAT_TYPE_NONE: {
  1434.                         int copy = read;
  1435.                         if (str < end) {
  1436.                                 if (copy > end - str)
  1437.                                         copy = end - str;
  1438.                                 memcpy(str, old_fmt, copy);
  1439.                         }
  1440.                         str += read;
  1441.                         break;
  1442.                 }
  1443.  
  1444.                 case FORMAT_TYPE_WIDTH:
  1445.                         spec.field_width = va_arg(args, int);
  1446.                         break;
  1447.  
  1448.                 case FORMAT_TYPE_PRECISION:
  1449.                         spec.precision = va_arg(args, int);
  1450.                         break;
  1451.  
  1452.                 case FORMAT_TYPE_CHAR: {
  1453.                         char c;
  1454.  
  1455.                         if (!(spec.flags & LEFT)) {
  1456.                                 while (--spec.field_width > 0) {
  1457.                                         if (str < end)
  1458.                                                 *str = ' ';
  1459.                                         ++str;
  1460.  
  1461.                                 }
  1462.                         }
  1463.                         c = (unsigned char) va_arg(args, int);
  1464.                         if (str < end)
  1465.                                 *str = c;
  1466.                         ++str;
  1467.                         while (--spec.field_width > 0) {
  1468.                                 if (str < end)
  1469.                                         *str = ' ';
  1470.                                 ++str;
  1471.                         }
  1472.                         break;
  1473.                 }
  1474.  
  1475.                 case FORMAT_TYPE_STR:
  1476.                         str = string(str, end, va_arg(args, char *), spec);
  1477.                         break;
  1478.  
  1479.                 case FORMAT_TYPE_PTR:
  1480.                         str = pointer(fmt, str, end, va_arg(args, void *),
  1481.                                       spec);
  1482.                         while (isalnum(*fmt))
  1483.                                 fmt++;
  1484.                         break;
  1485.  
  1486.                 case FORMAT_TYPE_PERCENT_CHAR:
  1487.                         if (str < end)
  1488.                                 *str = '%';
  1489.                         ++str;
  1490.                         break;
  1491.  
  1492.                 case FORMAT_TYPE_INVALID:
  1493.                         if (str < end)
  1494.                                 *str = '%';
  1495.                         ++str;
  1496.                         break;
  1497.  
  1498.                 default:
  1499.                         switch (spec.type) {
  1500.                         case FORMAT_TYPE_LONG_LONG:
  1501.                                 num = va_arg(args, long long);
  1502.                                 break;
  1503.                         case FORMAT_TYPE_ULONG:
  1504.                                 num = va_arg(args, unsigned long);
  1505.                                 break;
  1506.                         case FORMAT_TYPE_LONG:
  1507.                                 num = va_arg(args, long);
  1508.                                 break;
  1509.                         case FORMAT_TYPE_SIZE_T:
  1510.                                 if (spec.flags & SIGN)
  1511.                                         num = va_arg(args, ssize_t);
  1512.                                 else
  1513.                                 num = va_arg(args, size_t);
  1514.                                 break;
  1515.                         case FORMAT_TYPE_PTRDIFF:
  1516.                                 num = va_arg(args, ptrdiff_t);
  1517.                                 break;
  1518.                         case FORMAT_TYPE_UBYTE:
  1519.                                 num = (unsigned char) va_arg(args, int);
  1520.                                 break;
  1521.                         case FORMAT_TYPE_BYTE:
  1522.                                 num = (signed char) va_arg(args, int);
  1523.                                 break;
  1524.                         case FORMAT_TYPE_USHORT:
  1525.                                 num = (unsigned short) va_arg(args, int);
  1526.                                 break;
  1527.                         case FORMAT_TYPE_SHORT:
  1528.                                 num = (short) va_arg(args, int);
  1529.                                 break;
  1530.                         case FORMAT_TYPE_INT:
  1531.                                 num = (int) va_arg(args, int);
  1532.                                 break;
  1533.                         default:
  1534.                                 num = va_arg(args, unsigned int);
  1535.                         }
  1536.  
  1537.                         str = number(str, end, num, spec);
  1538.                 }
  1539.         }
  1540.  
  1541.         if (size > 0) {
  1542.                 if (str < end)
  1543.                         *str = '\0';
  1544.                 else
  1545.                         end[-1] = '\0';
  1546.         }
  1547.  
  1548.         /* the trailing null byte doesn't count towards the total */
  1549.         return str-buf;
  1550.  
  1551. }
  1552. EXPORT_SYMBOL(vsnprintf);
  1553.  
  1554. /**
  1555.  * vscnprintf - Format a string and place it in a buffer
  1556.  * @buf: The buffer to place the result into
  1557.  * @size: The size of the buffer, including the trailing null space
  1558.  * @fmt: The format string to use
  1559.  * @args: Arguments for the format string
  1560.  *
  1561.  * The return value is the number of characters which have been written into
  1562.  * the @buf not including the trailing '\0'. If @size is == 0 the function
  1563.  * returns 0.
  1564.  *
  1565.  * If you're not already dealing with a va_list consider using scnprintf().
  1566.  *
  1567.  * See the vsnprintf() documentation for format string extensions over C99.
  1568.  */
  1569. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1570. {
  1571.         int i;
  1572.  
  1573.         i = vsnprintf(buf, size, fmt, args);
  1574.  
  1575.         if (likely(i < size))
  1576.                 return i;
  1577.         if (size != 0)
  1578.                 return size - 1;
  1579.         return 0;
  1580. }
  1581. EXPORT_SYMBOL(vscnprintf);
  1582.  
  1583. /**
  1584.  * snprintf - Format a string and place it in a buffer
  1585.  * @buf: The buffer to place the result into
  1586.  * @size: The size of the buffer, including the trailing null space
  1587.  * @fmt: The format string to use
  1588.  * @...: Arguments for the format string
  1589.  *
  1590.  * The return value is the number of characters which would be
  1591.  * generated for the given input, excluding the trailing null,
  1592.  * as per ISO C99.  If the return is greater than or equal to
  1593.  * @size, the resulting string is truncated.
  1594.  *
  1595.  * See the vsnprintf() documentation for format string extensions over C99.
  1596.  */
  1597. int snprintf(char *buf, size_t size, const char *fmt, ...)
  1598. {
  1599.         va_list args;
  1600.         int i;
  1601.  
  1602.         va_start(args, fmt);
  1603.         i = vsnprintf(buf, size, fmt, args);
  1604.         va_end(args);
  1605.  
  1606.         return i;
  1607. }
  1608. EXPORT_SYMBOL(snprintf);
  1609.  
  1610. /**
  1611.  * scnprintf - Format a string and place it in a buffer
  1612.  * @buf: The buffer to place the result into
  1613.  * @size: The size of the buffer, including the trailing null space
  1614.  * @fmt: The format string to use
  1615.  * @...: Arguments for the format string
  1616.  *
  1617.  * The return value is the number of characters written into @buf not including
  1618.  * the trailing '\0'. If @size is == 0 the function returns 0.
  1619.  */
  1620.  
  1621. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  1622. {
  1623.         va_list args;
  1624.         int i;
  1625.  
  1626.         va_start(args, fmt);
  1627.         i = vscnprintf(buf, size, fmt, args);
  1628.         va_end(args);
  1629.  
  1630.         return i;
  1631. }
  1632. EXPORT_SYMBOL(scnprintf);
  1633.  
  1634. /**
  1635.  * vsprintf - Format a string and place it in a buffer
  1636.  * @buf: The buffer to place the result into
  1637.  * @fmt: The format string to use
  1638.  * @args: Arguments for the format string
  1639.  *
  1640.  * The function returns the number of characters written
  1641.  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  1642.  * buffer overflows.
  1643.  *
  1644.  * If you're not already dealing with a va_list consider using sprintf().
  1645.  *
  1646.  * See the vsnprintf() documentation for format string extensions over C99.
  1647.  */
  1648. int vsprintf(char *buf, const char *fmt, va_list args)
  1649. {
  1650.         return vsnprintf(buf, INT_MAX, fmt, args);
  1651. }
  1652. EXPORT_SYMBOL(vsprintf);
  1653.  
  1654. /**
  1655.  * sprintf - Format a string and place it in a buffer
  1656.  * @buf: The buffer to place the result into
  1657.  * @fmt: The format string to use
  1658.  * @...: Arguments for the format string
  1659.  *
  1660.  * The function returns the number of characters written
  1661.  * into @buf. Use snprintf() or scnprintf() in order to avoid
  1662.  * buffer overflows.
  1663.  *
  1664.  * See the vsnprintf() documentation for format string extensions over C99.
  1665.  */
  1666. int sprintf(char *buf, const char *fmt, ...)
  1667. {
  1668.         va_list args;
  1669.         int i;
  1670.  
  1671.         va_start(args, fmt);
  1672.         i = vsnprintf(buf, INT_MAX, fmt, args);
  1673.         va_end(args);
  1674.  
  1675.         return i;
  1676. }
  1677. EXPORT_SYMBOL(sprintf);
  1678. /**
  1679.  * vsscanf - Unformat a buffer into a list of arguments
  1680.  * @buf:        input buffer
  1681.  * @fmt:        format of buffer
  1682.  * @args:       arguments
  1683.  */
  1684. int vsscanf(const char *buf, const char *fmt, va_list args)
  1685. {
  1686.         const char *str = buf;
  1687.         char *next;
  1688.         char digit;
  1689.         int num = 0;
  1690.         u8 qualifier;
  1691.         unsigned int base;
  1692.         union {
  1693.                 long long s;
  1694.                 unsigned long long u;
  1695.         } val;
  1696.         s16 field_width;
  1697.         bool is_sign;
  1698.  
  1699.         while (*fmt) {
  1700.                 /* skip any white space in format */
  1701.                 /* white space in format matchs any amount of
  1702.                  * white space, including none, in the input.
  1703.                  */
  1704.                 if (isspace(*fmt)) {
  1705.                         fmt = skip_spaces(++fmt);
  1706.                         str = skip_spaces(str);
  1707.                 }
  1708.  
  1709.                 /* anything that is not a conversion must match exactly */
  1710.                 if (*fmt != '%' && *fmt) {
  1711.                         if (*fmt++ != *str++)
  1712.                                 break;
  1713.                         continue;
  1714.                 }
  1715.  
  1716.                 if (!*fmt)
  1717.                         break;
  1718.                 ++fmt;
  1719.  
  1720.                 /* skip this conversion.
  1721.                  * advance both strings to next white space
  1722.                  */
  1723.                 if (*fmt == '*') {
  1724.                         if (!*str)
  1725.                                 break;
  1726.                         while (!isspace(*fmt) && *fmt != '%' && *fmt)
  1727.                                 fmt++;
  1728.                         while (!isspace(*str) && *str)
  1729.                                 str++;
  1730.                         continue;
  1731.                 }
  1732.  
  1733.                 /* get field width */
  1734.                 field_width = -1;
  1735.                 if (isdigit(*fmt)) {
  1736.                         field_width = skip_atoi(&fmt);
  1737.                         if (field_width <= 0)
  1738.                                 break;
  1739.                 }
  1740.  
  1741.                 /* get conversion qualifier */
  1742.                 qualifier = -1;
  1743.                 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
  1744.                     _tolower(*fmt) == 'z') {
  1745.                         qualifier = *fmt++;
  1746.                         if (unlikely(qualifier == *fmt)) {
  1747.                                 if (qualifier == 'h') {
  1748.                                         qualifier = 'H';
  1749.                                         fmt++;
  1750.                                 } else if (qualifier == 'l') {
  1751.                                         qualifier = 'L';
  1752.                                         fmt++;
  1753.                                 }
  1754.                         }
  1755.                 }
  1756.  
  1757.                 if (!*fmt)
  1758.                         break;
  1759.  
  1760.                 if (*fmt == 'n') {
  1761.                         /* return number of characters read so far */
  1762.                         *va_arg(args, int *) = str - buf;
  1763.                         ++fmt;
  1764.                         continue;
  1765.                 }
  1766.  
  1767.                 if (!*str)
  1768.                         break;
  1769.  
  1770.                 base = 10;
  1771.                 is_sign = false;
  1772.  
  1773.                 switch (*fmt++) {
  1774.                 case 'c':
  1775.                 {
  1776.                         char *s = (char *)va_arg(args, char*);
  1777.                         if (field_width == -1)
  1778.                                 field_width = 1;
  1779.                         do {
  1780.                                 *s++ = *str++;
  1781.                         } while (--field_width > 0 && *str);
  1782.                         num++;
  1783.                 }
  1784.                 continue;
  1785.                 case 's':
  1786.                 {
  1787.                         char *s = (char *)va_arg(args, char *);
  1788.                         if (field_width == -1)
  1789.                                 field_width = SHRT_MAX;
  1790.                         /* first, skip leading white space in buffer */
  1791.                         str = skip_spaces(str);
  1792.  
  1793.                         /* now copy until next white space */
  1794.                         while (*str && !isspace(*str) && field_width--)
  1795.                                 *s++ = *str++;
  1796.                         *s = '\0';
  1797.                         num++;
  1798.                 }
  1799.                 continue;
  1800.                 case 'o':
  1801.                         base = 8;
  1802.                         break;
  1803.                 case 'x':
  1804.                 case 'X':
  1805.                         base = 16;
  1806.                         break;
  1807.                 case 'i':
  1808.                         base = 0;
  1809.                 case 'd':
  1810.                         is_sign = true;
  1811.                 case 'u':
  1812.                         break;
  1813.                 case '%':
  1814.                         /* looking for '%' in str */
  1815.                         if (*str++ != '%')
  1816.                                 return num;
  1817.                         continue;
  1818.                 default:
  1819.                         /* invalid format; stop here */
  1820.                         return num;
  1821.                 }
  1822.  
  1823.                 /* have some sort of integer conversion.
  1824.                  * first, skip white space in buffer.
  1825.                  */
  1826.                 str = skip_spaces(str);
  1827.  
  1828.                 digit = *str;
  1829.                 if (is_sign && digit == '-')
  1830.                         digit = *(str + 1);
  1831.  
  1832.                 if (!digit
  1833.                     || (base == 16 && !isxdigit(digit))
  1834.                     || (base == 10 && !isdigit(digit))
  1835.                     || (base == 8 && (!isdigit(digit) || digit > '7'))
  1836.                     || (base == 0 && !isdigit(digit)))
  1837.                         break;
  1838.  
  1839.                 if (is_sign)
  1840.                         val.s = qualifier != 'L' ?
  1841.                                 simple_strtol(str, &next, base) :
  1842.                                 simple_strtoll(str, &next, base);
  1843.                 else
  1844.                         val.u = qualifier != 'L' ?
  1845.                                 simple_strtoul(str, &next, base) :
  1846.                                 simple_strtoull(str, &next, base);
  1847.  
  1848.                 if (field_width > 0 && next - str > field_width) {
  1849.                         if (base == 0)
  1850.                                 _parse_integer_fixup_radix(str, &base);
  1851.                         while (next - str > field_width) {
  1852.                                 if (is_sign)
  1853.                                         val.s = div_s64(val.s, base);
  1854.                                 else
  1855.                                         val.u = div_u64(val.u, base);
  1856.                                 --next;
  1857.                         }
  1858.                 }
  1859.  
  1860.                 switch (qualifier) {
  1861.                 case 'H':       /* that's 'hh' in format */
  1862.                         if (is_sign)
  1863.                                 *va_arg(args, signed char *) = val.s;
  1864.                         else
  1865.                                 *va_arg(args, unsigned char *) = val.u;
  1866.                         break;
  1867.                 case 'h':
  1868.                         if (is_sign)
  1869.                                 *va_arg(args, short *) = val.s;
  1870.                         else
  1871.                                 *va_arg(args, unsigned short *) = val.u;
  1872.                         break;
  1873.                 case 'l':
  1874.                         if (is_sign)
  1875.                                 *va_arg(args, long *) = val.s;
  1876.                         else
  1877.                                 *va_arg(args, unsigned long *) = val.u;
  1878.                         break;
  1879.                 case 'L':
  1880.                         if (is_sign)
  1881.                                 *va_arg(args, long long *) = val.s;
  1882.                         else
  1883.                                 *va_arg(args, unsigned long long *) = val.u;
  1884.                         break;
  1885.                 case 'Z':
  1886.                 case 'z':
  1887.                         *va_arg(args, size_t *) = val.u;
  1888.                         break;
  1889.                 default:
  1890.                         if (is_sign)
  1891.                                 *va_arg(args, int *) = val.s;
  1892.                         else
  1893.                                 *va_arg(args, unsigned int *) = val.u;
  1894.                         break;
  1895.                 }
  1896.                 num++;
  1897.  
  1898.                 if (!next)
  1899.                         break;
  1900.                 str = next;
  1901.         }
  1902.  
  1903.         return num;
  1904. }
  1905. EXPORT_SYMBOL(vsscanf);
  1906.  
  1907. /**
  1908.  * sscanf - Unformat a buffer into a list of arguments
  1909.  * @buf:        input buffer
  1910.  * @fmt:        formatting of buffer
  1911.  * @...:        resulting arguments
  1912.  */
  1913. int sscanf(const char *buf, const char *fmt, ...)
  1914. {
  1915.         va_list args;
  1916.         int i;
  1917.  
  1918.         va_start(args, fmt);
  1919.         i = vsscanf(buf, fmt, args);
  1920.         va_end(args);
  1921.  
  1922.         return i;
  1923. }
  1924. EXPORT_SYMBOL(sscanf);
  1925.