Subversion Repositories Kolibri OS

Rev

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