Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 12 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 20 | #include <linux/clk-provider.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 21 | #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/types.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/ctype.h> |
| 25 | #include <linux/kernel.h> |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 26 | #include <linux/kallsyms.h> |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 27 | #include <linux/math64.h> |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 28 | #include <linux/uaccess.h> |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 29 | #include <linux/ioport.h> |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 30 | #include <linux/dcache.h> |
Ryan Mallon | 312b4e2 | 2013-11-12 15:08:51 -0800 | [diff] [blame] | 31 | #include <linux/cred.h> |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 32 | #include <net/addrconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 34 | #include <asm/page.h> /* for PAGE_SIZE */ |
James Bottomley | deac93d | 2008-09-03 20:43:36 -0500 | [diff] [blame] | 35 | #include <asm/sections.h> /* for dereference_function_descriptor() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 37 | #include <linux/string_helpers.h> |
Alexey Dobriyan | 1dff46d | 2011-10-31 17:12:28 -0700 | [diff] [blame] | 38 | #include "kstrtox.h" |
Harvey Harrison | aa46a63 | 2008-10-16 13:40:34 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | * simple_strtoull - convert a string to an unsigned long long |
| 42 | * @cp: The start of the string |
| 43 | * @endp: A pointer to the end of the parsed string will be placed here |
| 44 | * @base: The number base to use |
Eldad Zack | 462e471 | 2012-12-17 16:03:05 -0800 | [diff] [blame] | 45 | * |
| 46 | * This function is obsolete. Please use kstrtoull instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | */ |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 48 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Alexey Dobriyan | 1dff46d | 2011-10-31 17:12:28 -0700 | [diff] [blame] | 50 | unsigned long long result; |
| 51 | unsigned int rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Alexey Dobriyan | 1dff46d | 2011-10-31 17:12:28 -0700 | [diff] [blame] | 53 | cp = _parse_integer_fixup_radix(cp, &base); |
| 54 | rv = _parse_integer(cp, base, &result); |
| 55 | /* FIXME */ |
| 56 | cp += (rv & ~KSTRTOX_OVERFLOW); |
Harvey Harrison | aa46a63 | 2008-10-16 13:40:34 -0700 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | if (endp) |
| 59 | *endp = (char *)cp; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | return result; |
| 62 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | EXPORT_SYMBOL(simple_strtoull); |
| 64 | |
| 65 | /** |
André Goddard Rosa | 922ac25 | 2009-12-14 18:01:01 -0800 | [diff] [blame] | 66 | * simple_strtoul - convert a string to an unsigned long |
| 67 | * @cp: The start of the string |
| 68 | * @endp: A pointer to the end of the parsed string will be placed here |
| 69 | * @base: The number base to use |
Eldad Zack | 462e471 | 2012-12-17 16:03:05 -0800 | [diff] [blame] | 70 | * |
| 71 | * This function is obsolete. Please use kstrtoul instead. |
André Goddard Rosa | 922ac25 | 2009-12-14 18:01:01 -0800 | [diff] [blame] | 72 | */ |
| 73 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
| 74 | { |
| 75 | return simple_strtoull(cp, endp, base); |
| 76 | } |
| 77 | EXPORT_SYMBOL(simple_strtoul); |
| 78 | |
| 79 | /** |
| 80 | * simple_strtol - convert a string to a signed long |
| 81 | * @cp: The start of the string |
| 82 | * @endp: A pointer to the end of the parsed string will be placed here |
| 83 | * @base: The number base to use |
Eldad Zack | 462e471 | 2012-12-17 16:03:05 -0800 | [diff] [blame] | 84 | * |
| 85 | * This function is obsolete. Please use kstrtol instead. |
André Goddard Rosa | 922ac25 | 2009-12-14 18:01:01 -0800 | [diff] [blame] | 86 | */ |
| 87 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
| 88 | { |
| 89 | if (*cp == '-') |
| 90 | return -simple_strtoul(cp + 1, endp, base); |
| 91 | |
| 92 | return simple_strtoul(cp, endp, base); |
| 93 | } |
| 94 | EXPORT_SYMBOL(simple_strtol); |
| 95 | |
| 96 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | * simple_strtoll - convert a string to a signed long long |
| 98 | * @cp: The start of the string |
| 99 | * @endp: A pointer to the end of the parsed string will be placed here |
| 100 | * @base: The number base to use |
Eldad Zack | 462e471 | 2012-12-17 16:03:05 -0800 | [diff] [blame] | 101 | * |
| 102 | * This function is obsolete. Please use kstrtoll instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | */ |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 104 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 106 | if (*cp == '-') |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 107 | return -simple_strtoull(cp + 1, endp, base); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 108 | |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 109 | return simple_strtoull(cp, endp, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
Hans Verkuil | 98d5ce0 | 2010-04-23 13:18:04 -0400 | [diff] [blame] | 111 | EXPORT_SYMBOL(simple_strtoll); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 113 | static noinline_for_stack |
| 114 | int skip_atoi(const char **s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 116 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Rasmus Villemoes | 43e5b66 | 2015-02-12 15:01:42 -0800 | [diff] [blame] | 118 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | i = i*10 + *((*s)++) - '0'; |
Rasmus Villemoes | 43e5b66 | 2015-02-12 15:01:42 -0800 | [diff] [blame] | 120 | } while (isdigit(**s)); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | return i; |
| 123 | } |
| 124 | |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 125 | /* Decimal conversion is by far the most typical, and is used |
| 126 | * for /proc and /sys data. This directly impacts e.g. top performance |
| 127 | * with many processes running. We optimize it for speed |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 128 | * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html> |
| 129 | * (with permission from the author, Douglas W. Jones). |
| 130 | */ |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 131 | |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 132 | #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 |
| 133 | /* Formats correctly any integer in [0, 999999999] */ |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 134 | static noinline_for_stack |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 135 | char *put_dec_full9(char *buf, unsigned q) |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 136 | { |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 137 | unsigned r; |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 138 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 139 | /* |
| 140 | * Possible ways to approx. divide by 10 |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 141 | * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit) |
| 142 | * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul) |
| 143 | * (x * 0x6667) >> 18 x < 43699 |
| 144 | * (x * 0x3334) >> 17 x < 16389 |
| 145 | * (x * 0x199a) >> 16 x < 16389 |
| 146 | * (x * 0x0ccd) >> 15 x < 16389 |
| 147 | * (x * 0x0667) >> 14 x < 2739 |
| 148 | * (x * 0x0334) >> 13 x < 1029 |
| 149 | * (x * 0x019a) >> 12 x < 1029 |
| 150 | * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386) |
| 151 | * (x * 0x0067) >> 10 x < 179 |
| 152 | * (x * 0x0034) >> 9 x < 69 same |
| 153 | * (x * 0x001a) >> 8 x < 69 same |
| 154 | * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386) |
| 155 | * (x * 0x0007) >> 6 x < 19 |
| 156 | * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html> |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 157 | */ |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 158 | r = (q * (uint64_t)0x1999999a) >> 32; |
| 159 | *buf++ = (q - 10 * r) + '0'; /* 1 */ |
| 160 | q = (r * (uint64_t)0x1999999a) >> 32; |
| 161 | *buf++ = (r - 10 * q) + '0'; /* 2 */ |
| 162 | r = (q * (uint64_t)0x1999999a) >> 32; |
| 163 | *buf++ = (q - 10 * r) + '0'; /* 3 */ |
| 164 | q = (r * (uint64_t)0x1999999a) >> 32; |
| 165 | *buf++ = (r - 10 * q) + '0'; /* 4 */ |
| 166 | r = (q * (uint64_t)0x1999999a) >> 32; |
| 167 | *buf++ = (q - 10 * r) + '0'; /* 5 */ |
| 168 | /* Now value is under 10000, can avoid 64-bit multiply */ |
| 169 | q = (r * 0x199a) >> 16; |
| 170 | *buf++ = (r - 10 * q) + '0'; /* 6 */ |
| 171 | r = (q * 0xcd) >> 11; |
| 172 | *buf++ = (q - 10 * r) + '0'; /* 7 */ |
| 173 | q = (r * 0xcd) >> 11; |
| 174 | *buf++ = (r - 10 * q) + '0'; /* 8 */ |
| 175 | *buf++ = q + '0'; /* 9 */ |
| 176 | return buf; |
| 177 | } |
| 178 | #endif |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 179 | |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 180 | /* Similar to above but do not pad with zeros. |
| 181 | * Code can be easily arranged to print 9 digits too, but our callers |
| 182 | * always call put_dec_full9() instead when the number has 9 decimal digits. |
| 183 | */ |
| 184 | static noinline_for_stack |
| 185 | char *put_dec_trunc8(char *buf, unsigned r) |
| 186 | { |
| 187 | unsigned q; |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 188 | |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 189 | /* Copy of previous function's body with added early returns */ |
George Spelvin | cb239d0 | 2012-10-04 17:12:30 -0700 | [diff] [blame] | 190 | while (r >= 10000) { |
| 191 | q = r + '0'; |
| 192 | r = (r * (uint64_t)0x1999999a) >> 32; |
| 193 | *buf++ = q - 10*r; |
| 194 | } |
| 195 | |
George Spelvin | f400051 | 2012-10-04 17:12:32 -0700 | [diff] [blame] | 196 | q = (r * 0x199a) >> 16; /* r <= 9999 */ |
| 197 | *buf++ = (r - 10 * q) + '0'; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 198 | if (q == 0) |
| 199 | return buf; |
George Spelvin | f400051 | 2012-10-04 17:12:32 -0700 | [diff] [blame] | 200 | r = (q * 0xcd) >> 11; /* q <= 999 */ |
| 201 | *buf++ = (q - 10 * r) + '0'; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 202 | if (r == 0) |
| 203 | return buf; |
George Spelvin | f400051 | 2012-10-04 17:12:32 -0700 | [diff] [blame] | 204 | q = (r * 0xcd) >> 11; /* r <= 99 */ |
| 205 | *buf++ = (r - 10 * q) + '0'; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 206 | if (q == 0) |
| 207 | return buf; |
George Spelvin | f400051 | 2012-10-04 17:12:32 -0700 | [diff] [blame] | 208 | *buf++ = q + '0'; /* q <= 9 */ |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 209 | return buf; |
| 210 | } |
| 211 | |
| 212 | /* There are two algorithms to print larger numbers. |
| 213 | * One is generic: divide by 1000000000 and repeatedly print |
| 214 | * groups of (up to) 9 digits. It's conceptually simple, |
| 215 | * but requires a (unsigned long long) / 1000000000 division. |
| 216 | * |
| 217 | * Second algorithm splits 64-bit unsigned long long into 16-bit chunks, |
| 218 | * manipulates them cleverly and generates groups of 4 decimal digits. |
| 219 | * It so happens that it does NOT require long long division. |
| 220 | * |
| 221 | * If long is > 32 bits, division of 64-bit values is relatively easy, |
| 222 | * and we will use the first algorithm. |
| 223 | * If long long is > 64 bits (strange architecture with VERY large long long), |
| 224 | * second algorithm can't be used, and we again use the first one. |
| 225 | * |
| 226 | * Else (if long is 32 bits and long long is 64 bits) we use second one. |
| 227 | */ |
| 228 | |
| 229 | #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 |
| 230 | |
| 231 | /* First algorithm: generic */ |
| 232 | |
| 233 | static |
| 234 | char *put_dec(char *buf, unsigned long long n) |
| 235 | { |
| 236 | if (n >= 100*1000*1000) { |
| 237 | while (n >= 1000*1000*1000) |
| 238 | buf = put_dec_full9(buf, do_div(n, 1000*1000*1000)); |
| 239 | if (n >= 100*1000*1000) |
| 240 | return put_dec_full9(buf, n); |
| 241 | } |
| 242 | return put_dec_trunc8(buf, n); |
| 243 | } |
| 244 | |
| 245 | #else |
| 246 | |
| 247 | /* Second algorithm: valid only for 64-bit long longs */ |
| 248 | |
George Spelvin | e49317d | 2012-10-04 17:12:27 -0700 | [diff] [blame] | 249 | /* See comment in put_dec_full9 for choice of constants */ |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 250 | static noinline_for_stack |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 251 | void put_dec_full4(char *buf, unsigned q) |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 252 | { |
| 253 | unsigned r; |
George Spelvin | e49317d | 2012-10-04 17:12:27 -0700 | [diff] [blame] | 254 | r = (q * 0xccd) >> 15; |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 255 | buf[0] = (q - 10 * r) + '0'; |
George Spelvin | e49317d | 2012-10-04 17:12:27 -0700 | [diff] [blame] | 256 | q = (r * 0xcd) >> 11; |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 257 | buf[1] = (r - 10 * q) + '0'; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 258 | r = (q * 0xcd) >> 11; |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 259 | buf[2] = (q - 10 * r) + '0'; |
| 260 | buf[3] = r + '0'; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Call put_dec_full4 on x % 10000, return x / 10000. |
| 265 | * The approximation x/10000 == (x * 0x346DC5D7) >> 43 |
| 266 | * holds for all x < 1,128,869,999. The largest value this |
| 267 | * helper will ever be asked to convert is 1,125,520,955. |
| 268 | * (d1 in the put_dec code, assuming n is all-ones). |
| 269 | */ |
| 270 | static |
| 271 | unsigned put_dec_helper4(char *buf, unsigned x) |
| 272 | { |
| 273 | uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; |
| 274 | |
| 275 | put_dec_full4(buf, x - q * 10000); |
| 276 | return q; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* Based on code by Douglas W. Jones found at |
| 280 | * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> |
| 281 | * (with permission from the author). |
| 282 | * Performs no 64-bit division and hence should be fast on 32-bit machines. |
| 283 | */ |
| 284 | static |
| 285 | char *put_dec(char *buf, unsigned long long n) |
| 286 | { |
| 287 | uint32_t d3, d2, d1, q, h; |
| 288 | |
| 289 | if (n < 100*1000*1000) |
| 290 | return put_dec_trunc8(buf, n); |
| 291 | |
| 292 | d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ |
| 293 | h = (n >> 32); |
| 294 | d2 = (h ) & 0xffff; |
| 295 | d3 = (h >> 16); /* implicit "& 0xffff" */ |
| 296 | |
| 297 | q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 298 | q = put_dec_helper4(buf, q); |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 299 | |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 300 | q += 7671 * d3 + 9496 * d2 + 6 * d1; |
| 301 | q = put_dec_helper4(buf+4, q); |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 302 | |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 303 | q += 4749 * d3 + 42 * d2; |
| 304 | q = put_dec_helper4(buf+8, q); |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 305 | |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 306 | q += 281 * d3; |
| 307 | buf += 12; |
| 308 | if (q) |
| 309 | buf = put_dec_trunc8(buf, q); |
| 310 | else while (buf[-1] == '0') |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 311 | --buf; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 312 | |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 313 | return buf; |
| 314 | } |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 315 | |
| 316 | #endif |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 317 | |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 318 | /* |
| 319 | * Convert passed number to decimal string. |
| 320 | * Returns the length of string. On buffer overflow, returns 0. |
| 321 | * |
| 322 | * If speed is not important, use snprintf(). It's easy to read the code. |
| 323 | */ |
| 324 | int num_to_str(char *buf, int size, unsigned long long num) |
| 325 | { |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 326 | char tmp[sizeof(num) * 3]; |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 327 | int idx, len; |
| 328 | |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 329 | /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ |
| 330 | if (num <= 9) { |
| 331 | tmp[0] = '0' + num; |
| 332 | len = 1; |
| 333 | } else { |
| 334 | len = put_dec(tmp, num) - tmp; |
| 335 | } |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 336 | |
| 337 | if (len > size) |
| 338 | return 0; |
| 339 | for (idx = 0; idx < len; ++idx) |
| 340 | buf[idx] = tmp[len - idx - 1]; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 341 | return len; |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 344 | #define SIGN 1 /* unsigned/signed, must be 1 */ |
Rasmus Villemoes | d1c1b12 | 2015-04-15 16:17:11 -0700 | [diff] [blame] | 345 | #define LEFT 2 /* left justified */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | #define PLUS 4 /* show plus */ |
| 347 | #define SPACE 8 /* space if plus */ |
Rasmus Villemoes | d1c1b12 | 2015-04-15 16:17:11 -0700 | [diff] [blame] | 348 | #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ |
Bjorn Helgaas | b89dc5d | 2010-03-05 10:47:31 -0700 | [diff] [blame] | 349 | #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ |
| 350 | #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 352 | enum format_type { |
| 353 | FORMAT_TYPE_NONE, /* Just a string part */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 354 | FORMAT_TYPE_WIDTH, |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 355 | FORMAT_TYPE_PRECISION, |
| 356 | FORMAT_TYPE_CHAR, |
| 357 | FORMAT_TYPE_STR, |
| 358 | FORMAT_TYPE_PTR, |
| 359 | FORMAT_TYPE_PERCENT_CHAR, |
| 360 | FORMAT_TYPE_INVALID, |
| 361 | FORMAT_TYPE_LONG_LONG, |
| 362 | FORMAT_TYPE_ULONG, |
| 363 | FORMAT_TYPE_LONG, |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 364 | FORMAT_TYPE_UBYTE, |
| 365 | FORMAT_TYPE_BYTE, |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 366 | FORMAT_TYPE_USHORT, |
| 367 | FORMAT_TYPE_SHORT, |
| 368 | FORMAT_TYPE_UINT, |
| 369 | FORMAT_TYPE_INT, |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 370 | FORMAT_TYPE_SIZE_T, |
| 371 | FORMAT_TYPE_PTRDIFF |
| 372 | }; |
| 373 | |
| 374 | struct printf_spec { |
Joe Perches | 4e310fd | 2010-04-14 09:27:40 -0700 | [diff] [blame] | 375 | u8 type; /* format_type enum */ |
Joe Perches | ef0658f | 2010-03-06 17:10:14 -0800 | [diff] [blame] | 376 | u8 flags; /* flags to number() */ |
Joe Perches | 4e310fd | 2010-04-14 09:27:40 -0700 | [diff] [blame] | 377 | u8 base; /* number base, 8, 10 or 16 only */ |
| 378 | u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ |
| 379 | s16 field_width; /* width of output field */ |
| 380 | s16 precision; /* # of digits/chars */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 381 | }; |
| 382 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 383 | static noinline_for_stack |
| 384 | char *number(char *buf, char *end, unsigned long long num, |
| 385 | struct printf_spec spec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
Rasmus Villemoes | e26c12c | 2015-04-15 16:17:05 -0700 | [diff] [blame] | 387 | char tmp[3 * sizeof(num)]; |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 388 | char sign; |
| 389 | char locase; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 390 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | int i; |
Pierre Carrier | 7c20342 | 2012-05-29 15:07:35 -0700 | [diff] [blame] | 392 | bool is_zero = num == 0LL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 394 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
| 395 | * produces same digits or (maybe lowercased) letters */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 396 | locase = (spec.flags & SMALL); |
| 397 | if (spec.flags & LEFT) |
| 398 | spec.flags &= ~ZEROPAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | sign = 0; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 400 | if (spec.flags & SIGN) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 401 | if ((signed long long)num < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | sign = '-'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 403 | num = -(signed long long)num; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 404 | spec.field_width--; |
| 405 | } else if (spec.flags & PLUS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | sign = '+'; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 407 | spec.field_width--; |
| 408 | } else if (spec.flags & SPACE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | sign = ' '; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 410 | spec.field_width--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 413 | if (need_pfx) { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 414 | if (spec.base == 16) |
Pierre Carrier | 7c20342 | 2012-05-29 15:07:35 -0700 | [diff] [blame] | 415 | spec.field_width -= 2; |
| 416 | else if (!is_zero) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 417 | spec.field_width--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 419 | |
| 420 | /* generate full string in tmp[], in reverse order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | i = 0; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 422 | if (num < spec.base) |
Rasmus Villemoes | 3ea8d44 | 2015-04-15 16:17:08 -0700 | [diff] [blame] | 423 | tmp[i++] = hex_asc_upper[num] | locase; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 424 | else if (spec.base != 10) { /* 8 or 16 */ |
| 425 | int mask = spec.base - 1; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 426 | int shift = 3; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 427 | |
| 428 | if (spec.base == 16) |
| 429 | shift = 4; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 430 | do { |
Rasmus Villemoes | 3ea8d44 | 2015-04-15 16:17:08 -0700 | [diff] [blame] | 431 | tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 432 | num >>= shift; |
| 433 | } while (num); |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 434 | } else { /* base 10 */ |
| 435 | i = put_dec(tmp, num) - tmp; |
| 436 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 437 | |
| 438 | /* printing 100 using %2d gives "100", not "00" */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 439 | if (i > spec.precision) |
| 440 | spec.precision = i; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 441 | /* leading space padding */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 442 | spec.field_width -= spec.precision; |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 443 | if (!(spec.flags & (ZEROPAD | LEFT))) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 444 | while (--spec.field_width >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 445 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | *buf = ' '; |
| 447 | ++buf; |
| 448 | } |
| 449 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 450 | /* sign */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | if (sign) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 452 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | *buf = sign; |
| 454 | ++buf; |
| 455 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 456 | /* "0x" / "0" prefix */ |
| 457 | if (need_pfx) { |
Pierre Carrier | 7c20342 | 2012-05-29 15:07:35 -0700 | [diff] [blame] | 458 | if (spec.base == 16 || !is_zero) { |
| 459 | if (buf < end) |
| 460 | *buf = '0'; |
| 461 | ++buf; |
| 462 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 463 | if (spec.base == 16) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 464 | if (buf < end) |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 465 | *buf = ('X' | locase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | ++buf; |
| 467 | } |
| 468 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 469 | /* zero or space padding */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 470 | if (!(spec.flags & LEFT)) { |
Rasmus Villemoes | d1c1b12 | 2015-04-15 16:17:11 -0700 | [diff] [blame] | 471 | char c = ' ' + (spec.flags & ZEROPAD); |
| 472 | BUILD_BUG_ON(' ' + ZEROPAD != '0'); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 473 | while (--spec.field_width >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 474 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | *buf = c; |
| 476 | ++buf; |
| 477 | } |
| 478 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 479 | /* hmm even more zero padding? */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 480 | while (i <= --spec.precision) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 481 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | *buf = '0'; |
| 483 | ++buf; |
| 484 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 485 | /* actual digits of result */ |
| 486 | while (--i >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 487 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | *buf = tmp[i]; |
| 489 | ++buf; |
| 490 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 491 | /* trailing space padding */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 492 | while (--spec.field_width >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 493 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | *buf = ' '; |
| 495 | ++buf; |
| 496 | } |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | return buf; |
| 499 | } |
| 500 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 501 | static noinline_for_stack |
| 502 | char *string(char *buf, char *end, const char *s, struct printf_spec spec) |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 503 | { |
| 504 | int len, i; |
| 505 | |
| 506 | if ((unsigned long)s < PAGE_SIZE) |
André Goddard Rosa | 0f4f81d | 2009-12-14 18:00:55 -0800 | [diff] [blame] | 507 | s = "(null)"; |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 508 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 509 | len = strnlen(s, spec.precision); |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 510 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 511 | if (!(spec.flags & LEFT)) { |
| 512 | while (len < spec.field_width--) { |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 513 | if (buf < end) |
| 514 | *buf = ' '; |
| 515 | ++buf; |
| 516 | } |
| 517 | } |
| 518 | for (i = 0; i < len; ++i) { |
| 519 | if (buf < end) |
| 520 | *buf = *s; |
| 521 | ++buf; ++s; |
| 522 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 523 | while (len < spec.field_width--) { |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 524 | if (buf < end) |
| 525 | *buf = ' '; |
| 526 | ++buf; |
| 527 | } |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 528 | |
Linus Torvalds | 0f9bfa5 | 2008-07-06 16:06:25 -0700 | [diff] [blame] | 529 | return buf; |
| 530 | } |
| 531 | |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 532 | static void widen(char *buf, char *end, unsigned len, unsigned spaces) |
| 533 | { |
| 534 | size_t size; |
| 535 | if (buf >= end) /* nowhere to put anything */ |
| 536 | return; |
| 537 | size = end - buf; |
| 538 | if (size <= spaces) { |
| 539 | memset(buf, ' ', size); |
| 540 | return; |
| 541 | } |
| 542 | if (len) { |
| 543 | if (len > size - spaces) |
| 544 | len = size - spaces; |
| 545 | memmove(buf + spaces, buf, len); |
| 546 | } |
| 547 | memset(buf, ' ', spaces); |
| 548 | } |
| 549 | |
| 550 | static noinline_for_stack |
| 551 | char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, |
| 552 | const char *fmt) |
| 553 | { |
| 554 | const char *array[4], *s; |
| 555 | const struct dentry *p; |
| 556 | int depth; |
| 557 | int i, n; |
| 558 | |
| 559 | switch (fmt[1]) { |
| 560 | case '2': case '3': case '4': |
| 561 | depth = fmt[1] - '0'; |
| 562 | break; |
| 563 | default: |
| 564 | depth = 1; |
| 565 | } |
| 566 | |
| 567 | rcu_read_lock(); |
| 568 | for (i = 0; i < depth; i++, d = p) { |
| 569 | p = ACCESS_ONCE(d->d_parent); |
| 570 | array[i] = ACCESS_ONCE(d->d_name.name); |
| 571 | if (p == d) { |
| 572 | if (i) |
| 573 | array[i] = ""; |
| 574 | i++; |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | s = array[--i]; |
| 579 | for (n = 0; n != spec.precision; n++, buf++) { |
| 580 | char c = *s++; |
| 581 | if (!c) { |
| 582 | if (!i) |
| 583 | break; |
| 584 | c = '/'; |
| 585 | s = array[--i]; |
| 586 | } |
| 587 | if (buf < end) |
| 588 | *buf = c; |
| 589 | } |
| 590 | rcu_read_unlock(); |
| 591 | if (n < spec.field_width) { |
| 592 | /* we want to pad the sucker */ |
| 593 | unsigned spaces = spec.field_width - n; |
| 594 | if (!(spec.flags & LEFT)) { |
| 595 | widen(buf - n, end, n, spaces); |
| 596 | return buf + spaces; |
| 597 | } |
| 598 | while (spaces--) { |
| 599 | if (buf < end) |
| 600 | *buf = ' '; |
| 601 | ++buf; |
| 602 | } |
| 603 | } |
| 604 | return buf; |
| 605 | } |
| 606 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 607 | static noinline_for_stack |
| 608 | char *symbol_string(char *buf, char *end, void *ptr, |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 609 | struct printf_spec spec, const char *fmt) |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 610 | { |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 611 | unsigned long value; |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 612 | #ifdef CONFIG_KALLSYMS |
| 613 | char sym[KSYM_SYMBOL_LEN]; |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 614 | #endif |
| 615 | |
| 616 | if (fmt[1] == 'R') |
| 617 | ptr = __builtin_extract_return_addr(ptr); |
| 618 | value = (unsigned long)ptr; |
| 619 | |
| 620 | #ifdef CONFIG_KALLSYMS |
| 621 | if (*fmt == 'B') |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 622 | sprint_backtrace(sym, value); |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 623 | else if (*fmt != 'f' && *fmt != 's') |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 624 | sprint_symbol(sym, value); |
| 625 | else |
Stephen Boyd | 4796dd2 | 2012-05-29 15:07:33 -0700 | [diff] [blame] | 626 | sprint_symbol_no_offset(sym, value); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 627 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 628 | return string(buf, end, sym, spec); |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 629 | #else |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 630 | spec.field_width = 2 * sizeof(void *); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 631 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
| 632 | spec.base = 16; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 633 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 634 | return number(buf, end, value, spec); |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 635 | #endif |
| 636 | } |
| 637 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 638 | static noinline_for_stack |
| 639 | char *resource_string(char *buf, char *end, struct resource *res, |
| 640 | struct printf_spec spec, const char *fmt) |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 641 | { |
| 642 | #ifndef IO_RSRC_PRINTK_SIZE |
Bjorn Helgaas | 2840537 | 2009-10-06 15:33:29 -0600 | [diff] [blame] | 643 | #define IO_RSRC_PRINTK_SIZE 6 |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 644 | #endif |
| 645 | |
| 646 | #ifndef MEM_RSRC_PRINTK_SIZE |
Bjorn Helgaas | 2840537 | 2009-10-06 15:33:29 -0600 | [diff] [blame] | 647 | #define MEM_RSRC_PRINTK_SIZE 10 |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 648 | #endif |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 649 | static const struct printf_spec io_spec = { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 650 | .base = 16, |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 651 | .field_width = IO_RSRC_PRINTK_SIZE, |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 652 | .precision = -1, |
| 653 | .flags = SPECIAL | SMALL | ZEROPAD, |
| 654 | }; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 655 | static const struct printf_spec mem_spec = { |
| 656 | .base = 16, |
| 657 | .field_width = MEM_RSRC_PRINTK_SIZE, |
| 658 | .precision = -1, |
| 659 | .flags = SPECIAL | SMALL | ZEROPAD, |
| 660 | }; |
Bjorn Helgaas | 0f4050c | 2010-03-05 10:47:42 -0700 | [diff] [blame] | 661 | static const struct printf_spec bus_spec = { |
| 662 | .base = 16, |
| 663 | .field_width = 2, |
| 664 | .precision = -1, |
| 665 | .flags = SMALL | ZEROPAD, |
| 666 | }; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 667 | static const struct printf_spec dec_spec = { |
Bjorn Helgaas | c91d337 | 2009-10-06 15:33:34 -0600 | [diff] [blame] | 668 | .base = 10, |
| 669 | .precision = -1, |
| 670 | .flags = 0, |
| 671 | }; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 672 | static const struct printf_spec str_spec = { |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 673 | .field_width = -1, |
| 674 | .precision = 10, |
| 675 | .flags = LEFT, |
| 676 | }; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 677 | static const struct printf_spec flag_spec = { |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 678 | .base = 16, |
| 679 | .precision = -1, |
| 680 | .flags = SPECIAL | SMALL, |
| 681 | }; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 682 | |
| 683 | /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) |
| 684 | * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ |
| 685 | #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) |
| 686 | #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) |
Bjorn Helgaas | 9d7cca0 | 2010-03-05 10:47:47 -0700 | [diff] [blame] | 687 | #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 688 | #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") |
| 689 | char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, |
| 690 | 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; |
| 691 | |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 692 | char *p = sym, *pend = sym + sizeof(sym); |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 693 | int decode = (fmt[0] == 'R') ? 1 : 0; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 694 | const struct printf_spec *specp; |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 695 | |
| 696 | *p++ = '['; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 697 | if (res->flags & IORESOURCE_IO) { |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 698 | p = string(p, pend, "io ", str_spec); |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 699 | specp = &io_spec; |
| 700 | } else if (res->flags & IORESOURCE_MEM) { |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 701 | p = string(p, pend, "mem ", str_spec); |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 702 | specp = &mem_spec; |
| 703 | } else if (res->flags & IORESOURCE_IRQ) { |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 704 | p = string(p, pend, "irq ", str_spec); |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 705 | specp = &dec_spec; |
| 706 | } else if (res->flags & IORESOURCE_DMA) { |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 707 | p = string(p, pend, "dma ", str_spec); |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 708 | specp = &dec_spec; |
Bjorn Helgaas | 0f4050c | 2010-03-05 10:47:42 -0700 | [diff] [blame] | 709 | } else if (res->flags & IORESOURCE_BUS) { |
| 710 | p = string(p, pend, "bus ", str_spec); |
| 711 | specp = &bus_spec; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 712 | } else { |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 713 | p = string(p, pend, "??? ", str_spec); |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 714 | specp = &mem_spec; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 715 | decode = 0; |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 716 | } |
Bjorn Helgaas | d19cb80 | 2014-02-26 11:25:56 -0700 | [diff] [blame] | 717 | if (decode && res->flags & IORESOURCE_UNSET) { |
| 718 | p = string(p, pend, "size ", str_spec); |
| 719 | p = number(p, pend, resource_size(res), *specp); |
| 720 | } else { |
| 721 | p = number(p, pend, res->start, *specp); |
| 722 | if (res->start != res->end) { |
| 723 | *p++ = '-'; |
| 724 | p = number(p, pend, res->end, *specp); |
| 725 | } |
Bjorn Helgaas | c91d337 | 2009-10-06 15:33:34 -0600 | [diff] [blame] | 726 | } |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 727 | if (decode) { |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 728 | if (res->flags & IORESOURCE_MEM_64) |
| 729 | p = string(p, pend, " 64bit", str_spec); |
| 730 | if (res->flags & IORESOURCE_PREFETCH) |
| 731 | p = string(p, pend, " pref", str_spec); |
Bjorn Helgaas | 9d7cca0 | 2010-03-05 10:47:47 -0700 | [diff] [blame] | 732 | if (res->flags & IORESOURCE_WINDOW) |
| 733 | p = string(p, pend, " window", str_spec); |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 734 | if (res->flags & IORESOURCE_DISABLED) |
| 735 | p = string(p, pend, " disabled", str_spec); |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 736 | } else { |
| 737 | p = string(p, pend, " flags ", str_spec); |
| 738 | p = number(p, pend, res->flags, flag_spec); |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 739 | } |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 740 | *p++ = ']'; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 741 | *p = '\0'; |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 742 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 743 | return string(buf, end, sym, spec); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 744 | } |
| 745 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 746 | static noinline_for_stack |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 747 | char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, |
| 748 | const char *fmt) |
| 749 | { |
Steven Rostedt | 360603a | 2013-05-28 15:47:39 -0400 | [diff] [blame] | 750 | int i, len = 1; /* if we pass '%ph[CDN]', field width remains |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 751 | negative value, fallback to the default */ |
| 752 | char separator; |
| 753 | |
| 754 | if (spec.field_width == 0) |
| 755 | /* nothing to print */ |
| 756 | return buf; |
| 757 | |
| 758 | if (ZERO_OR_NULL_PTR(addr)) |
| 759 | /* NULL pointer */ |
| 760 | return string(buf, end, NULL, spec); |
| 761 | |
| 762 | switch (fmt[1]) { |
| 763 | case 'C': |
| 764 | separator = ':'; |
| 765 | break; |
| 766 | case 'D': |
| 767 | separator = '-'; |
| 768 | break; |
| 769 | case 'N': |
| 770 | separator = 0; |
| 771 | break; |
| 772 | default: |
| 773 | separator = ' '; |
| 774 | break; |
| 775 | } |
| 776 | |
| 777 | if (spec.field_width > 0) |
| 778 | len = min_t(int, spec.field_width, 64); |
| 779 | |
Rasmus Villemoes | 9c98f23 | 2015-04-15 16:17:23 -0700 | [diff] [blame^] | 780 | for (i = 0; i < len; ++i) { |
| 781 | if (buf < end) |
| 782 | *buf = hex_asc_hi(addr[i]); |
| 783 | ++buf; |
| 784 | if (buf < end) |
| 785 | *buf = hex_asc_lo(addr[i]); |
| 786 | ++buf; |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 787 | |
Rasmus Villemoes | 9c98f23 | 2015-04-15 16:17:23 -0700 | [diff] [blame^] | 788 | if (separator && i != len - 1) { |
| 789 | if (buf < end) |
| 790 | *buf = separator; |
| 791 | ++buf; |
| 792 | } |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | return buf; |
| 796 | } |
| 797 | |
| 798 | static noinline_for_stack |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 799 | char *bitmap_string(char *buf, char *end, unsigned long *bitmap, |
| 800 | struct printf_spec spec, const char *fmt) |
| 801 | { |
| 802 | const int CHUNKSZ = 32; |
| 803 | int nr_bits = max_t(int, spec.field_width, 0); |
| 804 | int i, chunksz; |
| 805 | bool first = true; |
| 806 | |
| 807 | /* reused to print numbers */ |
| 808 | spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; |
| 809 | |
| 810 | chunksz = nr_bits & (CHUNKSZ - 1); |
| 811 | if (chunksz == 0) |
| 812 | chunksz = CHUNKSZ; |
| 813 | |
| 814 | i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; |
| 815 | for (; i >= 0; i -= CHUNKSZ) { |
| 816 | u32 chunkmask, val; |
| 817 | int word, bit; |
| 818 | |
| 819 | chunkmask = ((1ULL << chunksz) - 1); |
| 820 | word = i / BITS_PER_LONG; |
| 821 | bit = i % BITS_PER_LONG; |
| 822 | val = (bitmap[word] >> bit) & chunkmask; |
| 823 | |
| 824 | if (!first) { |
| 825 | if (buf < end) |
| 826 | *buf = ','; |
| 827 | buf++; |
| 828 | } |
| 829 | first = false; |
| 830 | |
| 831 | spec.field_width = DIV_ROUND_UP(chunksz, 4); |
| 832 | buf = number(buf, end, val, spec); |
| 833 | |
| 834 | chunksz = CHUNKSZ; |
| 835 | } |
| 836 | return buf; |
| 837 | } |
| 838 | |
| 839 | static noinline_for_stack |
| 840 | char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, |
| 841 | struct printf_spec spec, const char *fmt) |
| 842 | { |
| 843 | int nr_bits = max_t(int, spec.field_width, 0); |
| 844 | /* current bit is 'cur', most recently seen range is [rbot, rtop] */ |
| 845 | int cur, rbot, rtop; |
| 846 | bool first = true; |
| 847 | |
| 848 | /* reused to print numbers */ |
| 849 | spec = (struct printf_spec){ .base = 10 }; |
| 850 | |
| 851 | rbot = cur = find_first_bit(bitmap, nr_bits); |
| 852 | while (cur < nr_bits) { |
| 853 | rtop = cur; |
| 854 | cur = find_next_bit(bitmap, nr_bits, cur + 1); |
| 855 | if (cur < nr_bits && cur <= rtop + 1) |
| 856 | continue; |
| 857 | |
| 858 | if (!first) { |
| 859 | if (buf < end) |
| 860 | *buf = ','; |
| 861 | buf++; |
| 862 | } |
| 863 | first = false; |
| 864 | |
| 865 | buf = number(buf, end, rbot, spec); |
| 866 | if (rbot < rtop) { |
| 867 | if (buf < end) |
| 868 | *buf = '-'; |
| 869 | buf++; |
| 870 | |
| 871 | buf = number(buf, end, rtop, spec); |
| 872 | } |
| 873 | |
| 874 | rbot = cur; |
| 875 | } |
| 876 | return buf; |
| 877 | } |
| 878 | |
| 879 | static noinline_for_stack |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 880 | char *mac_address_string(char *buf, char *end, u8 *addr, |
| 881 | struct printf_spec spec, const char *fmt) |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 882 | { |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 883 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 884 | char *p = mac_addr; |
| 885 | int i; |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 886 | char separator; |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 887 | bool reversed = false; |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 888 | |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 889 | switch (fmt[1]) { |
| 890 | case 'F': |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 891 | separator = '-'; |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 892 | break; |
| 893 | |
| 894 | case 'R': |
| 895 | reversed = true; |
| 896 | /* fall through */ |
| 897 | |
| 898 | default: |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 899 | separator = ':'; |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 900 | break; |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 901 | } |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 902 | |
| 903 | for (i = 0; i < 6; i++) { |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 904 | if (reversed) |
| 905 | p = hex_byte_pack(p, addr[5 - i]); |
| 906 | else |
| 907 | p = hex_byte_pack(p, addr[i]); |
| 908 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 909 | if (fmt[0] == 'M' && i != 5) |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 910 | *p++ = separator; |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 911 | } |
| 912 | *p = '\0'; |
| 913 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 914 | return string(buf, end, mac_addr, spec); |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 917 | static noinline_for_stack |
| 918 | char *ip4_string(char *p, const u8 *addr, const char *fmt) |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 919 | { |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 920 | int i; |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 921 | bool leading_zeros = (fmt[0] == 'i'); |
| 922 | int index; |
| 923 | int step; |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 924 | |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 925 | switch (fmt[2]) { |
| 926 | case 'h': |
| 927 | #ifdef __BIG_ENDIAN |
| 928 | index = 0; |
| 929 | step = 1; |
| 930 | #else |
| 931 | index = 3; |
| 932 | step = -1; |
| 933 | #endif |
| 934 | break; |
| 935 | case 'l': |
| 936 | index = 3; |
| 937 | step = -1; |
| 938 | break; |
| 939 | case 'n': |
| 940 | case 'b': |
| 941 | default: |
| 942 | index = 0; |
| 943 | step = 1; |
| 944 | break; |
| 945 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 946 | for (i = 0; i < 4; i++) { |
| 947 | char temp[3]; /* hold each IP quad in reverse order */ |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 948 | int digits = put_dec_trunc8(temp, addr[index]) - temp; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 949 | if (leading_zeros) { |
| 950 | if (digits < 3) |
| 951 | *p++ = '0'; |
| 952 | if (digits < 2) |
| 953 | *p++ = '0'; |
| 954 | } |
| 955 | /* reverse the digits in the quad */ |
| 956 | while (digits--) |
| 957 | *p++ = temp[digits]; |
| 958 | if (i < 3) |
| 959 | *p++ = '.'; |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 960 | index += step; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 961 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 962 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 963 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 964 | return p; |
| 965 | } |
| 966 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 967 | static noinline_for_stack |
| 968 | char *ip6_compressed_string(char *p, const char *addr) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 969 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 970 | int i, j, range; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 971 | unsigned char zerolength[8]; |
| 972 | int longest = 1; |
| 973 | int colonpos = -1; |
| 974 | u16 word; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 975 | u8 hi, lo; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 976 | bool needcolon = false; |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 977 | bool useIPv4; |
| 978 | struct in6_addr in6; |
| 979 | |
| 980 | memcpy(&in6, addr, sizeof(struct in6_addr)); |
| 981 | |
| 982 | useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 983 | |
| 984 | memset(zerolength, 0, sizeof(zerolength)); |
| 985 | |
| 986 | if (useIPv4) |
| 987 | range = 6; |
| 988 | else |
| 989 | range = 8; |
| 990 | |
| 991 | /* find position of longest 0 run */ |
| 992 | for (i = 0; i < range; i++) { |
| 993 | for (j = i; j < range; j++) { |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 994 | if (in6.s6_addr16[j] != 0) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 995 | break; |
| 996 | zerolength[i]++; |
| 997 | } |
| 998 | } |
| 999 | for (i = 0; i < range; i++) { |
| 1000 | if (zerolength[i] > longest) { |
| 1001 | longest = zerolength[i]; |
| 1002 | colonpos = i; |
| 1003 | } |
| 1004 | } |
Joe Perches | 29cf519 | 2011-06-09 11:23:37 -0700 | [diff] [blame] | 1005 | if (longest == 1) /* don't compress a single 0 */ |
| 1006 | colonpos = -1; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1007 | |
| 1008 | /* emit address */ |
| 1009 | for (i = 0; i < range; i++) { |
| 1010 | if (i == colonpos) { |
| 1011 | if (needcolon || i == 0) |
| 1012 | *p++ = ':'; |
| 1013 | *p++ = ':'; |
| 1014 | needcolon = false; |
| 1015 | i += longest - 1; |
| 1016 | continue; |
| 1017 | } |
| 1018 | if (needcolon) { |
| 1019 | *p++ = ':'; |
| 1020 | needcolon = false; |
| 1021 | } |
| 1022 | /* hex u16 without leading 0s */ |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 1023 | word = ntohs(in6.s6_addr16[i]); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1024 | hi = word >> 8; |
| 1025 | lo = word & 0xff; |
| 1026 | if (hi) { |
| 1027 | if (hi > 0x0f) |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 1028 | p = hex_byte_pack(p, hi); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1029 | else |
| 1030 | *p++ = hex_asc_lo(hi); |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 1031 | p = hex_byte_pack(p, lo); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1032 | } |
André Goddard Rosa | b5ff992 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1033 | else if (lo > 0x0f) |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 1034 | p = hex_byte_pack(p, lo); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1035 | else |
| 1036 | *p++ = hex_asc_lo(lo); |
| 1037 | needcolon = true; |
| 1038 | } |
| 1039 | |
| 1040 | if (useIPv4) { |
| 1041 | if (needcolon) |
| 1042 | *p++ = ':'; |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 1043 | p = ip4_string(p, &in6.s6_addr[12], "I4"); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1044 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1045 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1046 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1047 | return p; |
| 1048 | } |
| 1049 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1050 | static noinline_for_stack |
| 1051 | char *ip6_string(char *p, const char *addr, const char *fmt) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1052 | { |
| 1053 | int i; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1054 | |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1055 | for (i = 0; i < 8; i++) { |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 1056 | p = hex_byte_pack(p, *addr++); |
| 1057 | p = hex_byte_pack(p, *addr++); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1058 | if (fmt[0] == 'I' && i != 7) |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1059 | *p++ = ':'; |
| 1060 | } |
| 1061 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1062 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1063 | return p; |
| 1064 | } |
| 1065 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1066 | static noinline_for_stack |
| 1067 | char *ip6_addr_string(char *buf, char *end, const u8 *addr, |
| 1068 | struct printf_spec spec, const char *fmt) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1069 | { |
| 1070 | char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; |
| 1071 | |
| 1072 | if (fmt[0] == 'I' && fmt[2] == 'c') |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 1073 | ip6_compressed_string(ip6_addr, addr); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1074 | else |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 1075 | ip6_string(ip6_addr, addr, fmt); |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1076 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1077 | return string(buf, end, ip6_addr, spec); |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1080 | static noinline_for_stack |
| 1081 | char *ip4_addr_string(char *buf, char *end, const u8 *addr, |
| 1082 | struct printf_spec spec, const char *fmt) |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1083 | { |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1084 | char ip4_addr[sizeof("255.255.255.255")]; |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1085 | |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 1086 | ip4_string(ip4_addr, addr, fmt); |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1087 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1088 | return string(buf, end, ip4_addr, spec); |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1091 | static noinline_for_stack |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 1092 | char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, |
| 1093 | struct printf_spec spec, const char *fmt) |
| 1094 | { |
| 1095 | bool have_p = false, have_s = false, have_f = false, have_c = false; |
| 1096 | char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + |
| 1097 | sizeof(":12345") + sizeof("/123456789") + |
| 1098 | sizeof("%1234567890")]; |
| 1099 | char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); |
| 1100 | const u8 *addr = (const u8 *) &sa->sin6_addr; |
| 1101 | char fmt6[2] = { fmt[0], '6' }; |
| 1102 | u8 off = 0; |
| 1103 | |
| 1104 | fmt++; |
| 1105 | while (isalpha(*++fmt)) { |
| 1106 | switch (*fmt) { |
| 1107 | case 'p': |
| 1108 | have_p = true; |
| 1109 | break; |
| 1110 | case 'f': |
| 1111 | have_f = true; |
| 1112 | break; |
| 1113 | case 's': |
| 1114 | have_s = true; |
| 1115 | break; |
| 1116 | case 'c': |
| 1117 | have_c = true; |
| 1118 | break; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | if (have_p || have_s || have_f) { |
| 1123 | *p = '['; |
| 1124 | off = 1; |
| 1125 | } |
| 1126 | |
| 1127 | if (fmt6[0] == 'I' && have_c) |
| 1128 | p = ip6_compressed_string(ip6_addr + off, addr); |
| 1129 | else |
| 1130 | p = ip6_string(ip6_addr + off, addr, fmt6); |
| 1131 | |
| 1132 | if (have_p || have_s || have_f) |
| 1133 | *p++ = ']'; |
| 1134 | |
| 1135 | if (have_p) { |
| 1136 | *p++ = ':'; |
| 1137 | p = number(p, pend, ntohs(sa->sin6_port), spec); |
| 1138 | } |
| 1139 | if (have_f) { |
| 1140 | *p++ = '/'; |
| 1141 | p = number(p, pend, ntohl(sa->sin6_flowinfo & |
| 1142 | IPV6_FLOWINFO_MASK), spec); |
| 1143 | } |
| 1144 | if (have_s) { |
| 1145 | *p++ = '%'; |
| 1146 | p = number(p, pend, sa->sin6_scope_id, spec); |
| 1147 | } |
| 1148 | *p = '\0'; |
| 1149 | |
| 1150 | return string(buf, end, ip6_addr, spec); |
| 1151 | } |
| 1152 | |
| 1153 | static noinline_for_stack |
| 1154 | char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, |
| 1155 | struct printf_spec spec, const char *fmt) |
| 1156 | { |
| 1157 | bool have_p = false; |
| 1158 | char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; |
| 1159 | char *pend = ip4_addr + sizeof(ip4_addr); |
| 1160 | const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; |
| 1161 | char fmt4[3] = { fmt[0], '4', 0 }; |
| 1162 | |
| 1163 | fmt++; |
| 1164 | while (isalpha(*++fmt)) { |
| 1165 | switch (*fmt) { |
| 1166 | case 'p': |
| 1167 | have_p = true; |
| 1168 | break; |
| 1169 | case 'h': |
| 1170 | case 'l': |
| 1171 | case 'n': |
| 1172 | case 'b': |
| 1173 | fmt4[2] = *fmt; |
| 1174 | break; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | p = ip4_string(ip4_addr, addr, fmt4); |
| 1179 | if (have_p) { |
| 1180 | *p++ = ':'; |
| 1181 | p = number(p, pend, ntohs(sa->sin_port), spec); |
| 1182 | } |
| 1183 | *p = '\0'; |
| 1184 | |
| 1185 | return string(buf, end, ip4_addr, spec); |
| 1186 | } |
| 1187 | |
| 1188 | static noinline_for_stack |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 1189 | char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, |
| 1190 | const char *fmt) |
| 1191 | { |
| 1192 | bool found = true; |
| 1193 | int count = 1; |
| 1194 | unsigned int flags = 0; |
| 1195 | int len; |
| 1196 | |
| 1197 | if (spec.field_width == 0) |
| 1198 | return buf; /* nothing to print */ |
| 1199 | |
| 1200 | if (ZERO_OR_NULL_PTR(addr)) |
| 1201 | return string(buf, end, NULL, spec); /* NULL pointer */ |
| 1202 | |
| 1203 | |
| 1204 | do { |
| 1205 | switch (fmt[count++]) { |
| 1206 | case 'a': |
| 1207 | flags |= ESCAPE_ANY; |
| 1208 | break; |
| 1209 | case 'c': |
| 1210 | flags |= ESCAPE_SPECIAL; |
| 1211 | break; |
| 1212 | case 'h': |
| 1213 | flags |= ESCAPE_HEX; |
| 1214 | break; |
| 1215 | case 'n': |
| 1216 | flags |= ESCAPE_NULL; |
| 1217 | break; |
| 1218 | case 'o': |
| 1219 | flags |= ESCAPE_OCTAL; |
| 1220 | break; |
| 1221 | case 'p': |
| 1222 | flags |= ESCAPE_NP; |
| 1223 | break; |
| 1224 | case 's': |
| 1225 | flags |= ESCAPE_SPACE; |
| 1226 | break; |
| 1227 | default: |
| 1228 | found = false; |
| 1229 | break; |
| 1230 | } |
| 1231 | } while (found); |
| 1232 | |
| 1233 | if (!flags) |
| 1234 | flags = ESCAPE_ANY_NP; |
| 1235 | |
| 1236 | len = spec.field_width < 0 ? 1 : spec.field_width; |
| 1237 | |
| 1238 | /* Ignore the error. We print as many characters as we can */ |
| 1239 | string_escape_mem(addr, len, &buf, end - buf, flags, NULL); |
| 1240 | |
| 1241 | return buf; |
| 1242 | } |
| 1243 | |
| 1244 | static noinline_for_stack |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1245 | char *uuid_string(char *buf, char *end, const u8 *addr, |
| 1246 | struct printf_spec spec, const char *fmt) |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1247 | { |
| 1248 | char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; |
| 1249 | char *p = uuid; |
| 1250 | int i; |
| 1251 | static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; |
| 1252 | static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; |
| 1253 | const u8 *index = be; |
| 1254 | bool uc = false; |
| 1255 | |
| 1256 | switch (*(++fmt)) { |
| 1257 | case 'L': |
| 1258 | uc = true; /* fall-through */ |
| 1259 | case 'l': |
| 1260 | index = le; |
| 1261 | break; |
| 1262 | case 'B': |
| 1263 | uc = true; |
| 1264 | break; |
| 1265 | } |
| 1266 | |
| 1267 | for (i = 0; i < 16; i++) { |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 1268 | p = hex_byte_pack(p, addr[index[i]]); |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1269 | switch (i) { |
| 1270 | case 3: |
| 1271 | case 5: |
| 1272 | case 7: |
| 1273 | case 9: |
| 1274 | *p++ = '-'; |
| 1275 | break; |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | *p = 0; |
| 1280 | |
| 1281 | if (uc) { |
| 1282 | p = uuid; |
| 1283 | do { |
| 1284 | *p = toupper(*p); |
| 1285 | } while (*(++p)); |
| 1286 | } |
| 1287 | |
| 1288 | return string(buf, end, uuid, spec); |
| 1289 | } |
| 1290 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1291 | static |
| 1292 | char *netdev_feature_string(char *buf, char *end, const u8 *addr, |
| 1293 | struct printf_spec spec) |
| 1294 | { |
| 1295 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
| 1296 | if (spec.field_width == -1) |
| 1297 | spec.field_width = 2 + 2 * sizeof(netdev_features_t); |
| 1298 | spec.base = 16; |
| 1299 | |
| 1300 | return number(buf, end, *(const netdev_features_t *)addr, spec); |
| 1301 | } |
| 1302 | |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1303 | static noinline_for_stack |
| 1304 | char *address_val(char *buf, char *end, const void *addr, |
| 1305 | struct printf_spec spec, const char *fmt) |
| 1306 | { |
| 1307 | unsigned long long num; |
| 1308 | |
| 1309 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
| 1310 | spec.base = 16; |
| 1311 | |
| 1312 | switch (fmt[1]) { |
| 1313 | case 'd': |
| 1314 | num = *(const dma_addr_t *)addr; |
| 1315 | spec.field_width = sizeof(dma_addr_t) * 2 + 2; |
| 1316 | break; |
| 1317 | case 'p': |
| 1318 | default: |
| 1319 | num = *(const phys_addr_t *)addr; |
| 1320 | spec.field_width = sizeof(phys_addr_t) * 2 + 2; |
| 1321 | break; |
| 1322 | } |
| 1323 | |
| 1324 | return number(buf, end, num, spec); |
| 1325 | } |
| 1326 | |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 1327 | static noinline_for_stack |
| 1328 | char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, |
| 1329 | const char *fmt) |
| 1330 | { |
| 1331 | if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk) |
| 1332 | return string(buf, end, NULL, spec); |
| 1333 | |
| 1334 | switch (fmt[1]) { |
| 1335 | case 'r': |
| 1336 | return number(buf, end, clk_get_rate(clk), spec); |
| 1337 | |
| 1338 | case 'n': |
| 1339 | default: |
| 1340 | #ifdef CONFIG_COMMON_CLK |
| 1341 | return string(buf, end, __clk_get_name(clk), spec); |
| 1342 | #else |
| 1343 | spec.base = 16; |
| 1344 | spec.field_width = sizeof(unsigned long) * 2 + 2; |
| 1345 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
| 1346 | return number(buf, end, (unsigned long)clk, spec); |
| 1347 | #endif |
| 1348 | } |
| 1349 | } |
| 1350 | |
Ingo Molnar | 411f05f | 2011-05-12 23:00:28 +0200 | [diff] [blame] | 1351 | int kptr_restrict __read_mostly; |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 1352 | |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 1353 | /* |
| 1354 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
| 1355 | * by an extra set of alphanumeric characters that are extended format |
| 1356 | * specifiers. |
| 1357 | * |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1358 | * Right now we handle: |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1359 | * |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 1360 | * - 'F' For symbolic function descriptor pointers with offset |
| 1361 | * - 'f' For simple symbolic function names without offset |
Steven Rostedt | 0efb4d2 | 2009-09-17 09:27:29 -0400 | [diff] [blame] | 1362 | * - 'S' For symbolic direct pointers with offset |
| 1363 | * - 's' For symbolic direct pointers without offset |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 1364 | * - '[FfSs]R' as above with __builtin_extract_return_addr() translation |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 1365 | * - 'B' For backtraced symbolic direct pointers with offset |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 1366 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
| 1367 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 1368 | * - 'b[l]' For a bitmap, the number of bits is determined by the field |
| 1369 | * width which must be explicitly specified either as part of the |
| 1370 | * format string '%32b[l]' or through '%*b[l]', [l] selects |
| 1371 | * range-list format instead of hex format |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 1372 | * - 'M' For a 6-byte MAC address, it prints the address in the |
| 1373 | * usual colon-separated hex notation |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1374 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 1375 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
Joe Perches | c8e0006 | 2010-01-11 00:44:14 -0800 | [diff] [blame] | 1376 | * with a dash-separated hex notation |
Andy Shevchenko | 7c59154 | 2012-10-04 17:12:33 -0700 | [diff] [blame] | 1377 | * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1378 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
| 1379 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) |
| 1380 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 1381 | * [S][pfs] |
| 1382 | * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to |
| 1383 | * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1384 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
| 1385 | * IPv6 omits the colons (01020304...0f) |
| 1386 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 1387 | * [S][pfs] |
| 1388 | * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to |
| 1389 | * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] |
| 1390 | * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order |
| 1391 | * - 'I[6S]c' for IPv6 addresses printed as specified by |
Joe Perches | 29cf519 | 2011-06-09 11:23:37 -0700 | [diff] [blame] | 1392 | * http://tools.ietf.org/html/rfc5952 |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 1393 | * - 'E[achnops]' For an escaped buffer, where rules are defined by combination |
| 1394 | * of the following flags (see string_escape_mem() for the |
| 1395 | * details): |
| 1396 | * a - ESCAPE_ANY |
| 1397 | * c - ESCAPE_SPECIAL |
| 1398 | * h - ESCAPE_HEX |
| 1399 | * n - ESCAPE_NULL |
| 1400 | * o - ESCAPE_OCTAL |
| 1401 | * p - ESCAPE_NP |
| 1402 | * s - ESCAPE_SPACE |
| 1403 | * By default ESCAPE_ANY_NP is used. |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1404 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form |
| 1405 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 1406 | * Options for %pU are: |
| 1407 | * b big endian lower case hex (default) |
| 1408 | * B big endian UPPER case hex |
| 1409 | * l little endian lower case hex |
| 1410 | * L little endian UPPER case hex |
| 1411 | * big endian output byte order is: |
| 1412 | * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] |
| 1413 | * little endian output byte order is: |
| 1414 | * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] |
Joe Perches | 7db6f5f | 2010-06-27 01:02:33 +0000 | [diff] [blame] | 1415 | * - 'V' For a struct va_format which contains a format string * and va_list *, |
| 1416 | * call vsnprintf(->format, *->va_list). |
| 1417 | * Implements a "recursive vsnprintf". |
| 1418 | * Do not use this feature without some mechanism to verify the |
| 1419 | * correctness of the format string and va_list arguments. |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 1420 | * - 'K' For a kernel pointer that should be hidden from unprivileged users |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1421 | * - 'NF' For a netdev_features_t |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 1422 | * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with |
| 1423 | * a certain separator (' ' by default): |
| 1424 | * C colon |
| 1425 | * D dash |
| 1426 | * N no separator |
| 1427 | * The maximum supported length is 64 bytes of the input. Consider |
| 1428 | * to use print_hex_dump() for the larger input. |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1429 | * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives |
| 1430 | * (default assumed to be phys_addr_t, passed by reference) |
Olof Johansson | c0d92a5 | 2013-11-12 15:09:50 -0800 | [diff] [blame] | 1431 | * - 'd[234]' For a dentry name (optionally 2-4 last components) |
| 1432 | * - 'D[234]' Same as 'd' but for a struct file |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 1433 | * - 'C' For a clock, it prints the name (Common Clock Framework) or address |
| 1434 | * (legacy clock framework) of the clock |
| 1435 | * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address |
| 1436 | * (legacy clock framework) of the clock |
| 1437 | * - 'Cr' For a clock, it prints the current rate of the clock |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1438 | * |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1439 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
| 1440 | * function pointers are really function descriptors, which contain a |
| 1441 | * pointer to the real address. |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 1442 | */ |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1443 | static noinline_for_stack |
| 1444 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
| 1445 | struct printf_spec spec) |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 1446 | { |
Grant Likely | 725fe00 | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 1447 | int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0); |
| 1448 | |
Kees Cook | 9f36e2c | 2011-03-22 16:34:22 -0700 | [diff] [blame] | 1449 | if (!ptr && *fmt != 'K') { |
Joe Perches | 5e05798 | 2010-10-26 14:22:50 -0700 | [diff] [blame] | 1450 | /* |
| 1451 | * Print (null) with the same width as a pointer so it makes |
| 1452 | * tabular output look nice. |
| 1453 | */ |
| 1454 | if (spec.field_width == -1) |
Grant Likely | 725fe00 | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 1455 | spec.field_width = default_width; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1456 | return string(buf, end, "(null)", spec); |
Joe Perches | 5e05798 | 2010-10-26 14:22:50 -0700 | [diff] [blame] | 1457 | } |
Linus Torvalds | d97106a | 2009-01-03 11:46:17 -0800 | [diff] [blame] | 1458 | |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1459 | switch (*fmt) { |
| 1460 | case 'F': |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 1461 | case 'f': |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1462 | ptr = dereference_function_descriptor(ptr); |
| 1463 | /* Fallthrough */ |
| 1464 | case 'S': |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1465 | case 's': |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 1466 | case 'B': |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 1467 | return symbol_string(buf, end, ptr, spec, fmt); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1468 | case 'R': |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 1469 | case 'r': |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 1470 | return resource_string(buf, end, ptr, spec, fmt); |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 1471 | case 'h': |
| 1472 | return hex_string(buf, end, ptr, spec, fmt); |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 1473 | case 'b': |
| 1474 | switch (fmt[1]) { |
| 1475 | case 'l': |
| 1476 | return bitmap_list_string(buf, end, ptr, spec, fmt); |
| 1477 | default: |
| 1478 | return bitmap_string(buf, end, ptr, spec, fmt); |
| 1479 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1480 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
| 1481 | case 'm': /* Contiguous: 000102030405 */ |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 1482 | /* [mM]F (FDDI) */ |
| 1483 | /* [mM]R (Reverse order; Bluetooth) */ |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1484 | return mac_address_string(buf, end, ptr, spec, fmt); |
| 1485 | case 'I': /* Formatted IP supported |
| 1486 | * 4: 1.2.3.4 |
| 1487 | * 6: 0001:0203:...:0708 |
| 1488 | * 6c: 1::708 or 1::1.2.3.4 |
| 1489 | */ |
| 1490 | case 'i': /* Contiguous: |
| 1491 | * 4: 001.002.003.004 |
| 1492 | * 6: 000102...0f |
| 1493 | */ |
| 1494 | switch (fmt[1]) { |
| 1495 | case '6': |
| 1496 | return ip6_addr_string(buf, end, ptr, spec, fmt); |
| 1497 | case '4': |
| 1498 | return ip4_addr_string(buf, end, ptr, spec, fmt); |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 1499 | case 'S': { |
| 1500 | const union { |
| 1501 | struct sockaddr raw; |
| 1502 | struct sockaddr_in v4; |
| 1503 | struct sockaddr_in6 v6; |
| 1504 | } *sa = ptr; |
| 1505 | |
| 1506 | switch (sa->raw.sa_family) { |
| 1507 | case AF_INET: |
| 1508 | return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); |
| 1509 | case AF_INET6: |
| 1510 | return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); |
| 1511 | default: |
| 1512 | return string(buf, end, "(invalid address)", spec); |
| 1513 | }} |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1514 | } |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1515 | break; |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 1516 | case 'E': |
| 1517 | return escaped_string(buf, end, ptr, spec, fmt); |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1518 | case 'U': |
| 1519 | return uuid_string(buf, end, ptr, spec, fmt); |
Joe Perches | 7db6f5f | 2010-06-27 01:02:33 +0000 | [diff] [blame] | 1520 | case 'V': |
Jan Beulich | 5756b76 | 2012-03-05 16:49:24 +0000 | [diff] [blame] | 1521 | { |
| 1522 | va_list va; |
| 1523 | |
| 1524 | va_copy(va, *((struct va_format *)ptr)->va); |
| 1525 | buf += vsnprintf(buf, end > buf ? end - buf : 0, |
| 1526 | ((struct va_format *)ptr)->fmt, va); |
| 1527 | va_end(va); |
| 1528 | return buf; |
| 1529 | } |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 1530 | case 'K': |
| 1531 | /* |
| 1532 | * %pK cannot be used in IRQ context because its test |
| 1533 | * for CAP_SYSLOG would be meaningless. |
| 1534 | */ |
Dan Rosenberg | 3715c530 | 2012-07-30 14:40:26 -0700 | [diff] [blame] | 1535 | if (kptr_restrict && (in_irq() || in_serving_softirq() || |
| 1536 | in_nmi())) { |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 1537 | if (spec.field_width == -1) |
Grant Likely | 725fe00 | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 1538 | spec.field_width = default_width; |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 1539 | return string(buf, end, "pK-error", spec); |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 1540 | } |
Ryan Mallon | 312b4e2 | 2013-11-12 15:08:51 -0800 | [diff] [blame] | 1541 | |
| 1542 | switch (kptr_restrict) { |
| 1543 | case 0: |
| 1544 | /* Always print %pK values */ |
| 1545 | break; |
| 1546 | case 1: { |
| 1547 | /* |
| 1548 | * Only print the real pointer value if the current |
| 1549 | * process has CAP_SYSLOG and is running with the |
| 1550 | * same credentials it started with. This is because |
| 1551 | * access to files is checked at open() time, but %pK |
| 1552 | * checks permission at read() time. We don't want to |
| 1553 | * leak pointer values if a binary opens a file using |
| 1554 | * %pK and then elevates privileges before reading it. |
| 1555 | */ |
| 1556 | const struct cred *cred = current_cred(); |
| 1557 | |
| 1558 | if (!has_capability_noaudit(current, CAP_SYSLOG) || |
| 1559 | !uid_eq(cred->euid, cred->uid) || |
| 1560 | !gid_eq(cred->egid, cred->gid)) |
| 1561 | ptr = NULL; |
| 1562 | break; |
| 1563 | } |
| 1564 | case 2: |
| 1565 | default: |
| 1566 | /* Always print 0's for %pK */ |
Joe Perches | 2629760 | 2011-03-22 16:34:19 -0700 | [diff] [blame] | 1567 | ptr = NULL; |
Ryan Mallon | 312b4e2 | 2013-11-12 15:08:51 -0800 | [diff] [blame] | 1568 | break; |
| 1569 | } |
Joe Perches | 2629760 | 2011-03-22 16:34:19 -0700 | [diff] [blame] | 1570 | break; |
Ryan Mallon | 312b4e2 | 2013-11-12 15:08:51 -0800 | [diff] [blame] | 1571 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1572 | case 'N': |
| 1573 | switch (fmt[1]) { |
| 1574 | case 'F': |
| 1575 | return netdev_feature_string(buf, end, ptr, spec); |
| 1576 | } |
| 1577 | break; |
Stepan Moskovchenko | 7d79921 | 2013-02-21 16:43:09 -0800 | [diff] [blame] | 1578 | case 'a': |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1579 | return address_val(buf, end, ptr, spec, fmt); |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 1580 | case 'd': |
| 1581 | return dentry_name(buf, end, ptr, spec, fmt); |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 1582 | case 'C': |
| 1583 | return clock(buf, end, ptr, spec, fmt); |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 1584 | case 'D': |
| 1585 | return dentry_name(buf, end, |
| 1586 | ((const struct file *)ptr)->f_path.dentry, |
| 1587 | spec, fmt); |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1588 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1589 | spec.flags |= SMALL; |
| 1590 | if (spec.field_width == -1) { |
Grant Likely | 725fe00 | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 1591 | spec.field_width = default_width; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1592 | spec.flags |= ZEROPAD; |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 1593 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1594 | spec.base = 16; |
| 1595 | |
| 1596 | return number(buf, end, (unsigned long) ptr, spec); |
| 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * Helper function to decode printf style format. |
| 1601 | * Each call decode a token from the format and return the |
| 1602 | * number of characters read (or likely the delta where it wants |
| 1603 | * to go on the next call). |
| 1604 | * The decoded token is returned through the parameters |
| 1605 | * |
| 1606 | * 'h', 'l', or 'L' for integer fields |
| 1607 | * 'z' support added 23/7/1999 S.H. |
| 1608 | * 'z' changed to 'Z' --davidm 1/25/99 |
| 1609 | * 't' added for ptrdiff_t |
| 1610 | * |
| 1611 | * @fmt: the format string |
| 1612 | * @type of the token returned |
| 1613 | * @flags: various flags such as +, -, # tokens.. |
| 1614 | * @field_width: overwritten width |
| 1615 | * @base: base of the number (octal, hex, ...) |
| 1616 | * @precision: precision of a number |
| 1617 | * @qualifier: qualifier of a number (long, size_t, ...) |
| 1618 | */ |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1619 | static noinline_for_stack |
| 1620 | int format_decode(const char *fmt, struct printf_spec *spec) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1621 | { |
| 1622 | const char *start = fmt; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1623 | |
| 1624 | /* we finished early by reading the field width */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1625 | if (spec->type == FORMAT_TYPE_WIDTH) { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1626 | if (spec->field_width < 0) { |
| 1627 | spec->field_width = -spec->field_width; |
| 1628 | spec->flags |= LEFT; |
| 1629 | } |
| 1630 | spec->type = FORMAT_TYPE_NONE; |
| 1631 | goto precision; |
| 1632 | } |
| 1633 | |
| 1634 | /* we finished early by reading the precision */ |
| 1635 | if (spec->type == FORMAT_TYPE_PRECISION) { |
| 1636 | if (spec->precision < 0) |
| 1637 | spec->precision = 0; |
| 1638 | |
| 1639 | spec->type = FORMAT_TYPE_NONE; |
| 1640 | goto qualifier; |
| 1641 | } |
| 1642 | |
| 1643 | /* By default */ |
| 1644 | spec->type = FORMAT_TYPE_NONE; |
| 1645 | |
| 1646 | for (; *fmt ; ++fmt) { |
| 1647 | if (*fmt == '%') |
| 1648 | break; |
| 1649 | } |
| 1650 | |
| 1651 | /* Return the current non-format string */ |
| 1652 | if (fmt != start || !*fmt) |
| 1653 | return fmt - start; |
| 1654 | |
| 1655 | /* Process flags */ |
| 1656 | spec->flags = 0; |
| 1657 | |
| 1658 | while (1) { /* this also skips first '%' */ |
| 1659 | bool found = true; |
| 1660 | |
| 1661 | ++fmt; |
| 1662 | |
| 1663 | switch (*fmt) { |
| 1664 | case '-': spec->flags |= LEFT; break; |
| 1665 | case '+': spec->flags |= PLUS; break; |
| 1666 | case ' ': spec->flags |= SPACE; break; |
| 1667 | case '#': spec->flags |= SPECIAL; break; |
| 1668 | case '0': spec->flags |= ZEROPAD; break; |
| 1669 | default: found = false; |
| 1670 | } |
| 1671 | |
| 1672 | if (!found) |
| 1673 | break; |
| 1674 | } |
| 1675 | |
| 1676 | /* get field width */ |
| 1677 | spec->field_width = -1; |
| 1678 | |
| 1679 | if (isdigit(*fmt)) |
| 1680 | spec->field_width = skip_atoi(&fmt); |
| 1681 | else if (*fmt == '*') { |
| 1682 | /* it's the next argument */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1683 | spec->type = FORMAT_TYPE_WIDTH; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1684 | return ++fmt - start; |
| 1685 | } |
| 1686 | |
| 1687 | precision: |
| 1688 | /* get the precision */ |
| 1689 | spec->precision = -1; |
| 1690 | if (*fmt == '.') { |
| 1691 | ++fmt; |
| 1692 | if (isdigit(*fmt)) { |
| 1693 | spec->precision = skip_atoi(&fmt); |
| 1694 | if (spec->precision < 0) |
| 1695 | spec->precision = 0; |
| 1696 | } else if (*fmt == '*') { |
| 1697 | /* it's the next argument */ |
Vegard Nossum | adf26f8 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1698 | spec->type = FORMAT_TYPE_PRECISION; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1699 | return ++fmt - start; |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | qualifier: |
| 1704 | /* get the conversion qualifier */ |
| 1705 | spec->qualifier = -1; |
Andy Shevchenko | 75fb8f2 | 2011-07-25 17:13:20 -0700 | [diff] [blame] | 1706 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
| 1707 | _tolower(*fmt) == 'z' || *fmt == 't') { |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1708 | spec->qualifier = *fmt++; |
| 1709 | if (unlikely(spec->qualifier == *fmt)) { |
| 1710 | if (spec->qualifier == 'l') { |
| 1711 | spec->qualifier = 'L'; |
| 1712 | ++fmt; |
| 1713 | } else if (spec->qualifier == 'h') { |
| 1714 | spec->qualifier = 'H'; |
| 1715 | ++fmt; |
| 1716 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | /* default base */ |
| 1721 | spec->base = 10; |
| 1722 | switch (*fmt) { |
| 1723 | case 'c': |
| 1724 | spec->type = FORMAT_TYPE_CHAR; |
| 1725 | return ++fmt - start; |
| 1726 | |
| 1727 | case 's': |
| 1728 | spec->type = FORMAT_TYPE_STR; |
| 1729 | return ++fmt - start; |
| 1730 | |
| 1731 | case 'p': |
| 1732 | spec->type = FORMAT_TYPE_PTR; |
Rasmus Villemoes | ffbfed0 | 2015-02-12 15:01:37 -0800 | [diff] [blame] | 1733 | return ++fmt - start; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1734 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1735 | case '%': |
| 1736 | spec->type = FORMAT_TYPE_PERCENT_CHAR; |
| 1737 | return ++fmt - start; |
| 1738 | |
| 1739 | /* integer number formats - set up the flags and "break" */ |
| 1740 | case 'o': |
| 1741 | spec->base = 8; |
| 1742 | break; |
| 1743 | |
| 1744 | case 'x': |
| 1745 | spec->flags |= SMALL; |
| 1746 | |
| 1747 | case 'X': |
| 1748 | spec->base = 16; |
| 1749 | break; |
| 1750 | |
| 1751 | case 'd': |
| 1752 | case 'i': |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1753 | spec->flags |= SIGN; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1754 | case 'u': |
| 1755 | break; |
| 1756 | |
Ryan Mallon | 708d96fd | 2014-04-03 14:48:37 -0700 | [diff] [blame] | 1757 | case 'n': |
| 1758 | /* |
| 1759 | * Since %n poses a greater security risk than utility, treat |
| 1760 | * it as an invalid format specifier. Warn about its use so |
| 1761 | * that new instances don't get added. |
| 1762 | */ |
| 1763 | WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt); |
| 1764 | /* Fall-through */ |
| 1765 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1766 | default: |
| 1767 | spec->type = FORMAT_TYPE_INVALID; |
| 1768 | return fmt - start; |
| 1769 | } |
| 1770 | |
| 1771 | if (spec->qualifier == 'L') |
| 1772 | spec->type = FORMAT_TYPE_LONG_LONG; |
| 1773 | else if (spec->qualifier == 'l') { |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 1774 | BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); |
| 1775 | spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); |
Andy Shevchenko | 75fb8f2 | 2011-07-25 17:13:20 -0700 | [diff] [blame] | 1776 | } else if (_tolower(spec->qualifier) == 'z') { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1777 | spec->type = FORMAT_TYPE_SIZE_T; |
| 1778 | } else if (spec->qualifier == 't') { |
| 1779 | spec->type = FORMAT_TYPE_PTRDIFF; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1780 | } else if (spec->qualifier == 'H') { |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 1781 | BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); |
| 1782 | spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1783 | } else if (spec->qualifier == 'h') { |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 1784 | BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); |
| 1785 | spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1786 | } else { |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 1787 | BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); |
| 1788 | spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | return ++fmt - start; |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 1792 | } |
| 1793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | /** |
| 1795 | * vsnprintf - Format a string and place it in a buffer |
| 1796 | * @buf: The buffer to place the result into |
| 1797 | * @size: The size of the buffer, including the trailing null space |
| 1798 | * @fmt: The format string to use |
| 1799 | * @args: Arguments for the format string |
| 1800 | * |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1801 | * This function follows C99 vsnprintf, but has some extensions: |
Steven Rostedt | 91adcd2 | 2009-09-16 20:03:06 -0400 | [diff] [blame] | 1802 | * %pS output the name of a text symbol with offset |
| 1803 | * %ps output the name of a text symbol without offset |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 1804 | * %pF output the name of a function pointer with its offset |
| 1805 | * %pf output the name of a function pointer without its offset |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 1806 | * %pB output the name of a backtrace symbol with its offset |
Uwe Kleine-König | 8a79503 | 2009-12-17 15:27:12 -0800 | [diff] [blame] | 1807 | * %pR output the address range in a struct resource with decoded flags |
| 1808 | * %pr output the address range in a struct resource with raw flags |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 1809 | * %pb output the bitmap with field width as the number of bits |
| 1810 | * %pbl output the bitmap as range list with field width as the number of bits |
Uwe Kleine-König | 8a79503 | 2009-12-17 15:27:12 -0800 | [diff] [blame] | 1811 | * %pM output a 6-byte MAC address with colons |
Andy Shevchenko | 7c59154 | 2012-10-04 17:12:33 -0700 | [diff] [blame] | 1812 | * %pMR output a 6-byte MAC address with colons in reversed order |
| 1813 | * %pMF output a 6-byte MAC address with dashes |
Uwe Kleine-König | 8a79503 | 2009-12-17 15:27:12 -0800 | [diff] [blame] | 1814 | * %pm output a 6-byte MAC address without colons |
Andy Shevchenko | 7c59154 | 2012-10-04 17:12:33 -0700 | [diff] [blame] | 1815 | * %pmR output a 6-byte MAC address without colons in reversed order |
Uwe Kleine-König | 8a79503 | 2009-12-17 15:27:12 -0800 | [diff] [blame] | 1816 | * %pI4 print an IPv4 address without leading zeros |
| 1817 | * %pi4 print an IPv4 address with leading zeros |
| 1818 | * %pI6 print an IPv6 address with colons |
| 1819 | * %pi6 print an IPv6 address without colons |
Jan Engelhardt | f996f20 | 2011-07-14 18:48:56 +0200 | [diff] [blame] | 1820 | * %pI6c print an IPv6 address as specified by RFC 5952 |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 1821 | * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address |
| 1822 | * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address |
Uwe Kleine-König | 8a79503 | 2009-12-17 15:27:12 -0800 | [diff] [blame] | 1823 | * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper |
| 1824 | * case. |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 1825 | * %*pE[achnops] print an escaped buffer |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 1826 | * %*ph[CDN] a variable-length hex string with a separator (supports up to 64 |
| 1827 | * bytes of the input) |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 1828 | * %pC output the name (Common Clock Framework) or address (legacy clock |
| 1829 | * framework) of a clock |
| 1830 | * %pCn output the name (Common Clock Framework) or address (legacy clock |
| 1831 | * framework) of a clock |
| 1832 | * %pCr output the current rate of a clock |
Steven Rostedt | 0efb4d2 | 2009-09-17 09:27:29 -0400 | [diff] [blame] | 1833 | * %n is ignored |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 1834 | * |
Andrew Morton | 80f548e | 2012-07-30 14:40:25 -0700 | [diff] [blame] | 1835 | * ** Please update Documentation/printk-formats.txt when making changes ** |
| 1836 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | * The return value is the number of characters which would |
| 1838 | * be generated for the given input, excluding the trailing |
| 1839 | * '\0', as per ISO C99. If you want to have the exact |
| 1840 | * number of characters written into @buf as return value |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 1841 | * (not including the trailing '\0'), use vscnprintf(). If the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | * return is greater than or equal to @size, the resulting |
| 1843 | * string is truncated. |
| 1844 | * |
Uwe Kleine-König | ba1835e | 2011-04-06 07:49:04 -0700 | [diff] [blame] | 1845 | * If you're not already dealing with a va_list consider using snprintf(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | */ |
| 1847 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 1848 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | unsigned long long num; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1850 | char *str, *end; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1851 | struct printf_spec spec = {0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1853 | /* Reject out-of-range values early. Large positive sizes are |
| 1854 | used for unknown buffer sizes. */ |
Rasmus Villemoes | 2aa2f9e | 2015-02-12 15:01:39 -0800 | [diff] [blame] | 1855 | if (WARN_ON_ONCE(size > INT_MAX)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | |
| 1858 | str = buf; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1859 | end = buf + size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1861 | /* Make sure end is always >= buf */ |
| 1862 | if (end < buf) { |
| 1863 | end = ((void *)-1); |
| 1864 | size = end - buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | } |
| 1866 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1867 | while (*fmt) { |
| 1868 | const char *old_fmt = fmt; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1869 | int read = format_decode(fmt, &spec); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1870 | |
| 1871 | fmt += read; |
| 1872 | |
| 1873 | switch (spec.type) { |
| 1874 | case FORMAT_TYPE_NONE: { |
| 1875 | int copy = read; |
| 1876 | if (str < end) { |
| 1877 | if (copy > end - str) |
| 1878 | copy = end - str; |
| 1879 | memcpy(str, old_fmt, copy); |
| 1880 | } |
| 1881 | str += read; |
| 1882 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1885 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1886 | spec.field_width = va_arg(args, int); |
| 1887 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1889 | case FORMAT_TYPE_PRECISION: |
| 1890 | spec.precision = va_arg(args, int); |
| 1891 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1893 | case FORMAT_TYPE_CHAR: { |
| 1894 | char c; |
| 1895 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1896 | if (!(spec.flags & LEFT)) { |
| 1897 | while (--spec.field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1898 | if (str < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | *str = ' '; |
| 1900 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1903 | } |
| 1904 | c = (unsigned char) va_arg(args, int); |
| 1905 | if (str < end) |
| 1906 | *str = c; |
| 1907 | ++str; |
| 1908 | while (--spec.field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1909 | if (str < end) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1910 | *str = ' '; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1912 | } |
| 1913 | break; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1914 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1916 | case FORMAT_TYPE_STR: |
| 1917 | str = string(str, end, va_arg(args, char *), spec); |
| 1918 | break; |
| 1919 | |
| 1920 | case FORMAT_TYPE_PTR: |
Rasmus Villemoes | ffbfed0 | 2015-02-12 15:01:37 -0800 | [diff] [blame] | 1921 | str = pointer(fmt, str, end, va_arg(args, void *), |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1922 | spec); |
| 1923 | while (isalnum(*fmt)) |
| 1924 | fmt++; |
| 1925 | break; |
| 1926 | |
| 1927 | case FORMAT_TYPE_PERCENT_CHAR: |
| 1928 | if (str < end) |
| 1929 | *str = '%'; |
| 1930 | ++str; |
| 1931 | break; |
| 1932 | |
| 1933 | case FORMAT_TYPE_INVALID: |
| 1934 | if (str < end) |
| 1935 | *str = '%'; |
| 1936 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1937 | break; |
| 1938 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1939 | default: |
| 1940 | switch (spec.type) { |
| 1941 | case FORMAT_TYPE_LONG_LONG: |
| 1942 | num = va_arg(args, long long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1944 | case FORMAT_TYPE_ULONG: |
| 1945 | num = va_arg(args, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1947 | case FORMAT_TYPE_LONG: |
| 1948 | num = va_arg(args, long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1950 | case FORMAT_TYPE_SIZE_T: |
Jason Gunthorpe | ef12496 | 2012-12-17 15:59:58 -0800 | [diff] [blame] | 1951 | if (spec.flags & SIGN) |
| 1952 | num = va_arg(args, ssize_t); |
| 1953 | else |
| 1954 | num = va_arg(args, size_t); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1955 | break; |
| 1956 | case FORMAT_TYPE_PTRDIFF: |
| 1957 | num = va_arg(args, ptrdiff_t); |
| 1958 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1959 | case FORMAT_TYPE_UBYTE: |
| 1960 | num = (unsigned char) va_arg(args, int); |
| 1961 | break; |
| 1962 | case FORMAT_TYPE_BYTE: |
| 1963 | num = (signed char) va_arg(args, int); |
| 1964 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1965 | case FORMAT_TYPE_USHORT: |
| 1966 | num = (unsigned short) va_arg(args, int); |
| 1967 | break; |
| 1968 | case FORMAT_TYPE_SHORT: |
| 1969 | num = (short) va_arg(args, int); |
| 1970 | break; |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 1971 | case FORMAT_TYPE_INT: |
| 1972 | num = (int) va_arg(args, int); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1973 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | default: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1975 | num = va_arg(args, unsigned int); |
| 1976 | } |
| 1977 | |
| 1978 | str = number(str, end, num, spec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1981 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1982 | if (size > 0) { |
| 1983 | if (str < end) |
| 1984 | *str = '\0'; |
| 1985 | else |
Linus Torvalds | 0a6047e | 2006-06-28 17:09:34 -0700 | [diff] [blame] | 1986 | end[-1] = '\0'; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1987 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1988 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 1989 | /* the trailing null byte doesn't count towards the total */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | return str-buf; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1991 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | EXPORT_SYMBOL(vsnprintf); |
| 1994 | |
| 1995 | /** |
| 1996 | * vscnprintf - Format a string and place it in a buffer |
| 1997 | * @buf: The buffer to place the result into |
| 1998 | * @size: The size of the buffer, including the trailing null space |
| 1999 | * @fmt: The format string to use |
| 2000 | * @args: Arguments for the format string |
| 2001 | * |
| 2002 | * The return value is the number of characters which have been written into |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 2003 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | * returns 0. |
| 2005 | * |
Uwe Kleine-König | ba1835e | 2011-04-06 07:49:04 -0700 | [diff] [blame] | 2006 | * If you're not already dealing with a va_list consider using scnprintf(). |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2007 | * |
| 2008 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | */ |
| 2010 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 2011 | { |
| 2012 | int i; |
| 2013 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2014 | i = vsnprintf(buf, size, fmt, args); |
| 2015 | |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 2016 | if (likely(i < size)) |
| 2017 | return i; |
| 2018 | if (size != 0) |
| 2019 | return size - 1; |
| 2020 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | EXPORT_SYMBOL(vscnprintf); |
| 2023 | |
| 2024 | /** |
| 2025 | * snprintf - Format a string and place it in a buffer |
| 2026 | * @buf: The buffer to place the result into |
| 2027 | * @size: The size of the buffer, including the trailing null space |
| 2028 | * @fmt: The format string to use |
| 2029 | * @...: Arguments for the format string |
| 2030 | * |
| 2031 | * The return value is the number of characters which would be |
| 2032 | * generated for the given input, excluding the trailing null, |
| 2033 | * as per ISO C99. If the return is greater than or equal to |
| 2034 | * @size, the resulting string is truncated. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2035 | * |
| 2036 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2038 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | { |
| 2040 | va_list args; |
| 2041 | int i; |
| 2042 | |
| 2043 | va_start(args, fmt); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2044 | i = vsnprintf(buf, size, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | return i; |
| 2048 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | EXPORT_SYMBOL(snprintf); |
| 2050 | |
| 2051 | /** |
| 2052 | * scnprintf - Format a string and place it in a buffer |
| 2053 | * @buf: The buffer to place the result into |
| 2054 | * @size: The size of the buffer, including the trailing null space |
| 2055 | * @fmt: The format string to use |
| 2056 | * @...: Arguments for the format string |
| 2057 | * |
| 2058 | * The return value is the number of characters written into @buf not including |
Changli Gao | b903c0b | 2010-10-26 14:22:50 -0700 | [diff] [blame] | 2059 | * the trailing '\0'. If @size is == 0 the function returns 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | */ |
| 2061 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2062 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | { |
| 2064 | va_list args; |
| 2065 | int i; |
| 2066 | |
| 2067 | va_start(args, fmt); |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 2068 | i = vscnprintf(buf, size, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2070 | |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 2071 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | } |
| 2073 | EXPORT_SYMBOL(scnprintf); |
| 2074 | |
| 2075 | /** |
| 2076 | * vsprintf - Format a string and place it in a buffer |
| 2077 | * @buf: The buffer to place the result into |
| 2078 | * @fmt: The format string to use |
| 2079 | * @args: Arguments for the format string |
| 2080 | * |
| 2081 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 2082 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | * buffer overflows. |
| 2084 | * |
Uwe Kleine-König | ba1835e | 2011-04-06 07:49:04 -0700 | [diff] [blame] | 2085 | * If you're not already dealing with a va_list consider using sprintf(). |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2086 | * |
| 2087 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | */ |
| 2089 | int vsprintf(char *buf, const char *fmt, va_list args) |
| 2090 | { |
| 2091 | return vsnprintf(buf, INT_MAX, fmt, args); |
| 2092 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | EXPORT_SYMBOL(vsprintf); |
| 2094 | |
| 2095 | /** |
| 2096 | * sprintf - Format a string and place it in a buffer |
| 2097 | * @buf: The buffer to place the result into |
| 2098 | * @fmt: The format string to use |
| 2099 | * @...: Arguments for the format string |
| 2100 | * |
| 2101 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 2102 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | * buffer overflows. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2104 | * |
| 2105 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2107 | int sprintf(char *buf, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | { |
| 2109 | va_list args; |
| 2110 | int i; |
| 2111 | |
| 2112 | va_start(args, fmt); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2113 | i = vsnprintf(buf, INT_MAX, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | return i; |
| 2117 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | EXPORT_SYMBOL(sprintf); |
| 2119 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2120 | #ifdef CONFIG_BINARY_PRINTF |
| 2121 | /* |
| 2122 | * bprintf service: |
| 2123 | * vbin_printf() - VA arguments to binary data |
| 2124 | * bstr_printf() - Binary data to text string |
| 2125 | */ |
| 2126 | |
| 2127 | /** |
| 2128 | * vbin_printf - Parse a format string and place args' binary value in a buffer |
| 2129 | * @bin_buf: The buffer to place args' binary value |
| 2130 | * @size: The size of the buffer(by words(32bits), not characters) |
| 2131 | * @fmt: The format string to use |
| 2132 | * @args: Arguments for the format string |
| 2133 | * |
| 2134 | * The format follows C99 vsnprintf, except %n is ignored, and its argument |
Masanari Iida | da3dae5 | 2014-09-09 01:27:23 +0900 | [diff] [blame] | 2135 | * is skipped. |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2136 | * |
| 2137 | * The return value is the number of words(32bits) which would be generated for |
| 2138 | * the given input. |
| 2139 | * |
| 2140 | * NOTE: |
| 2141 | * If the return value is greater than @size, the resulting bin_buf is NOT |
| 2142 | * valid for bstr_printf(). |
| 2143 | */ |
| 2144 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) |
| 2145 | { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2146 | struct printf_spec spec = {0}; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2147 | char *str, *end; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2148 | |
| 2149 | str = (char *)bin_buf; |
| 2150 | end = (char *)(bin_buf + size); |
| 2151 | |
| 2152 | #define save_arg(type) \ |
| 2153 | do { \ |
| 2154 | if (sizeof(type) == 8) { \ |
| 2155 | unsigned long long value; \ |
| 2156 | str = PTR_ALIGN(str, sizeof(u32)); \ |
| 2157 | value = va_arg(args, unsigned long long); \ |
| 2158 | if (str + sizeof(type) <= end) { \ |
| 2159 | *(u32 *)str = *(u32 *)&value; \ |
| 2160 | *(u32 *)(str + 4) = *((u32 *)&value + 1); \ |
| 2161 | } \ |
| 2162 | } else { \ |
| 2163 | unsigned long value; \ |
| 2164 | str = PTR_ALIGN(str, sizeof(type)); \ |
| 2165 | value = va_arg(args, int); \ |
| 2166 | if (str + sizeof(type) <= end) \ |
| 2167 | *(typeof(type) *)str = (type)value; \ |
| 2168 | } \ |
| 2169 | str += sizeof(type); \ |
| 2170 | } while (0) |
| 2171 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2172 | while (*fmt) { |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2173 | int read = format_decode(fmt, &spec); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2174 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2175 | fmt += read; |
| 2176 | |
| 2177 | switch (spec.type) { |
| 2178 | case FORMAT_TYPE_NONE: |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2179 | case FORMAT_TYPE_INVALID: |
| 2180 | case FORMAT_TYPE_PERCENT_CHAR: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2181 | break; |
| 2182 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 2183 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2184 | case FORMAT_TYPE_PRECISION: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2185 | save_arg(int); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2186 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2187 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2188 | case FORMAT_TYPE_CHAR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2189 | save_arg(char); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2190 | break; |
| 2191 | |
| 2192 | case FORMAT_TYPE_STR: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2193 | const char *save_str = va_arg(args, char *); |
| 2194 | size_t len; |
André Goddard Rosa | 6c35663 | 2009-12-14 18:00:56 -0800 | [diff] [blame] | 2195 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2196 | if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE |
| 2197 | || (unsigned long)save_str < PAGE_SIZE) |
André Goddard Rosa | 0f4f81d | 2009-12-14 18:00:55 -0800 | [diff] [blame] | 2198 | save_str = "(null)"; |
André Goddard Rosa | 6c35663 | 2009-12-14 18:00:56 -0800 | [diff] [blame] | 2199 | len = strlen(save_str) + 1; |
| 2200 | if (str + len < end) |
| 2201 | memcpy(str, save_str, len); |
| 2202 | str += len; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2203 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2204 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2205 | |
| 2206 | case FORMAT_TYPE_PTR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2207 | save_arg(void *); |
| 2208 | /* skip all alphanumeric pointer suffixes */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2209 | while (isalnum(*fmt)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2210 | fmt++; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2211 | break; |
| 2212 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2213 | default: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2214 | switch (spec.type) { |
| 2215 | |
| 2216 | case FORMAT_TYPE_LONG_LONG: |
| 2217 | save_arg(long long); |
| 2218 | break; |
| 2219 | case FORMAT_TYPE_ULONG: |
| 2220 | case FORMAT_TYPE_LONG: |
| 2221 | save_arg(unsigned long); |
| 2222 | break; |
| 2223 | case FORMAT_TYPE_SIZE_T: |
| 2224 | save_arg(size_t); |
| 2225 | break; |
| 2226 | case FORMAT_TYPE_PTRDIFF: |
| 2227 | save_arg(ptrdiff_t); |
| 2228 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 2229 | case FORMAT_TYPE_UBYTE: |
| 2230 | case FORMAT_TYPE_BYTE: |
| 2231 | save_arg(char); |
| 2232 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2233 | case FORMAT_TYPE_USHORT: |
| 2234 | case FORMAT_TYPE_SHORT: |
| 2235 | save_arg(short); |
| 2236 | break; |
| 2237 | default: |
| 2238 | save_arg(int); |
| 2239 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2240 | } |
| 2241 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2242 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2243 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2244 | #undef save_arg |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2245 | } |
| 2246 | EXPORT_SYMBOL_GPL(vbin_printf); |
| 2247 | |
| 2248 | /** |
| 2249 | * bstr_printf - Format a string from binary arguments and place it in a buffer |
| 2250 | * @buf: The buffer to place the result into |
| 2251 | * @size: The size of the buffer, including the trailing null space |
| 2252 | * @fmt: The format string to use |
| 2253 | * @bin_buf: Binary arguments for the format string |
| 2254 | * |
| 2255 | * This function like C99 vsnprintf, but the difference is that vsnprintf gets |
| 2256 | * arguments from stack, and bstr_printf gets arguments from @bin_buf which is |
| 2257 | * a binary buffer that generated by vbin_printf. |
| 2258 | * |
| 2259 | * The format follows C99 vsnprintf, but has some extensions: |
Steven Rostedt | 0efb4d2 | 2009-09-17 09:27:29 -0400 | [diff] [blame] | 2260 | * see vsnprintf comment for details. |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2261 | * |
| 2262 | * The return value is the number of characters which would |
| 2263 | * be generated for the given input, excluding the trailing |
| 2264 | * '\0', as per ISO C99. If you want to have the exact |
| 2265 | * number of characters written into @buf as return value |
| 2266 | * (not including the trailing '\0'), use vscnprintf(). If the |
| 2267 | * return is greater than or equal to @size, the resulting |
| 2268 | * string is truncated. |
| 2269 | */ |
| 2270 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) |
| 2271 | { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2272 | struct printf_spec spec = {0}; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2273 | char *str, *end; |
| 2274 | const char *args = (const char *)bin_buf; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2275 | |
Marcin Slusarz | 2f30b1f9 | 2009-09-21 17:04:29 -0700 | [diff] [blame] | 2276 | if (WARN_ON_ONCE((int) size < 0)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2277 | return 0; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2278 | |
| 2279 | str = buf; |
| 2280 | end = buf + size; |
| 2281 | |
| 2282 | #define get_arg(type) \ |
| 2283 | ({ \ |
| 2284 | typeof(type) value; \ |
| 2285 | if (sizeof(type) == 8) { \ |
| 2286 | args = PTR_ALIGN(args, sizeof(u32)); \ |
| 2287 | *(u32 *)&value = *(u32 *)args; \ |
| 2288 | *((u32 *)&value + 1) = *(u32 *)(args + 4); \ |
| 2289 | } else { \ |
| 2290 | args = PTR_ALIGN(args, sizeof(type)); \ |
| 2291 | value = *(typeof(type) *)args; \ |
| 2292 | } \ |
| 2293 | args += sizeof(type); \ |
| 2294 | value; \ |
| 2295 | }) |
| 2296 | |
| 2297 | /* Make sure end is always >= buf */ |
| 2298 | if (end < buf) { |
| 2299 | end = ((void *)-1); |
| 2300 | size = end - buf; |
| 2301 | } |
| 2302 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2303 | while (*fmt) { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2304 | const char *old_fmt = fmt; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2305 | int read = format_decode(fmt, &spec); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2306 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2307 | fmt += read; |
| 2308 | |
| 2309 | switch (spec.type) { |
| 2310 | case FORMAT_TYPE_NONE: { |
| 2311 | int copy = read; |
| 2312 | if (str < end) { |
| 2313 | if (copy > end - str) |
| 2314 | copy = end - str; |
| 2315 | memcpy(str, old_fmt, copy); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2316 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2317 | str += read; |
| 2318 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2319 | } |
| 2320 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 2321 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2322 | spec.field_width = get_arg(int); |
| 2323 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2324 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2325 | case FORMAT_TYPE_PRECISION: |
| 2326 | spec.precision = get_arg(int); |
| 2327 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2328 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2329 | case FORMAT_TYPE_CHAR: { |
| 2330 | char c; |
| 2331 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2332 | if (!(spec.flags & LEFT)) { |
| 2333 | while (--spec.field_width > 0) { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2334 | if (str < end) |
| 2335 | *str = ' '; |
| 2336 | ++str; |
| 2337 | } |
| 2338 | } |
| 2339 | c = (unsigned char) get_arg(char); |
| 2340 | if (str < end) |
| 2341 | *str = c; |
| 2342 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2343 | while (--spec.field_width > 0) { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2344 | if (str < end) |
| 2345 | *str = ' '; |
| 2346 | ++str; |
| 2347 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2348 | break; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2349 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2350 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2351 | case FORMAT_TYPE_STR: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2352 | const char *str_arg = args; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2353 | args += strlen(str_arg) + 1; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2354 | str = string(str, end, (char *)str_arg, spec); |
| 2355 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2356 | } |
| 2357 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2358 | case FORMAT_TYPE_PTR: |
Rasmus Villemoes | ffbfed0 | 2015-02-12 15:01:37 -0800 | [diff] [blame] | 2359 | str = pointer(fmt, str, end, get_arg(void *), spec); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2360 | while (isalnum(*fmt)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2361 | fmt++; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2362 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2363 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2364 | case FORMAT_TYPE_PERCENT_CHAR: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2365 | case FORMAT_TYPE_INVALID: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2366 | if (str < end) |
| 2367 | *str = '%'; |
| 2368 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2369 | break; |
| 2370 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2371 | default: { |
| 2372 | unsigned long long num; |
| 2373 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2374 | switch (spec.type) { |
| 2375 | |
| 2376 | case FORMAT_TYPE_LONG_LONG: |
| 2377 | num = get_arg(long long); |
| 2378 | break; |
| 2379 | case FORMAT_TYPE_ULONG: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2380 | case FORMAT_TYPE_LONG: |
| 2381 | num = get_arg(unsigned long); |
| 2382 | break; |
| 2383 | case FORMAT_TYPE_SIZE_T: |
| 2384 | num = get_arg(size_t); |
| 2385 | break; |
| 2386 | case FORMAT_TYPE_PTRDIFF: |
| 2387 | num = get_arg(ptrdiff_t); |
| 2388 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 2389 | case FORMAT_TYPE_UBYTE: |
| 2390 | num = get_arg(unsigned char); |
| 2391 | break; |
| 2392 | case FORMAT_TYPE_BYTE: |
| 2393 | num = get_arg(signed char); |
| 2394 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2395 | case FORMAT_TYPE_USHORT: |
| 2396 | num = get_arg(unsigned short); |
| 2397 | break; |
| 2398 | case FORMAT_TYPE_SHORT: |
| 2399 | num = get_arg(short); |
| 2400 | break; |
| 2401 | case FORMAT_TYPE_UINT: |
| 2402 | num = get_arg(unsigned int); |
| 2403 | break; |
| 2404 | default: |
| 2405 | num = get_arg(int); |
| 2406 | } |
| 2407 | |
| 2408 | str = number(str, end, num, spec); |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2409 | } /* default: */ |
| 2410 | } /* switch(spec.type) */ |
| 2411 | } /* while(*fmt) */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2412 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2413 | if (size > 0) { |
| 2414 | if (str < end) |
| 2415 | *str = '\0'; |
| 2416 | else |
| 2417 | end[-1] = '\0'; |
| 2418 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2419 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2420 | #undef get_arg |
| 2421 | |
| 2422 | /* the trailing null byte doesn't count towards the total */ |
| 2423 | return str - buf; |
| 2424 | } |
| 2425 | EXPORT_SYMBOL_GPL(bstr_printf); |
| 2426 | |
| 2427 | /** |
| 2428 | * bprintf - Parse a format string and place args' binary value in a buffer |
| 2429 | * @bin_buf: The buffer to place args' binary value |
| 2430 | * @size: The size of the buffer(by words(32bits), not characters) |
| 2431 | * @fmt: The format string to use |
| 2432 | * @...: Arguments for the format string |
| 2433 | * |
| 2434 | * The function returns the number of words(u32) written |
| 2435 | * into @bin_buf. |
| 2436 | */ |
| 2437 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) |
| 2438 | { |
| 2439 | va_list args; |
| 2440 | int ret; |
| 2441 | |
| 2442 | va_start(args, fmt); |
| 2443 | ret = vbin_printf(bin_buf, size, fmt, args); |
| 2444 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2445 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2446 | return ret; |
| 2447 | } |
| 2448 | EXPORT_SYMBOL_GPL(bprintf); |
| 2449 | |
| 2450 | #endif /* CONFIG_BINARY_PRINTF */ |
| 2451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | /** |
| 2453 | * vsscanf - Unformat a buffer into a list of arguments |
| 2454 | * @buf: input buffer |
| 2455 | * @fmt: format of buffer |
| 2456 | * @args: arguments |
| 2457 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2458 | int vsscanf(const char *buf, const char *fmt, va_list args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | { |
| 2460 | const char *str = buf; |
| 2461 | char *next; |
| 2462 | char digit; |
| 2463 | int num = 0; |
Joe Perches | ef0658f | 2010-03-06 17:10:14 -0800 | [diff] [blame] | 2464 | u8 qualifier; |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2465 | unsigned int base; |
| 2466 | union { |
| 2467 | long long s; |
| 2468 | unsigned long long u; |
| 2469 | } val; |
Joe Perches | ef0658f | 2010-03-06 17:10:14 -0800 | [diff] [blame] | 2470 | s16 field_width; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2471 | bool is_sign; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | |
Jan Beulich | da99075 | 2012-10-04 17:13:24 -0700 | [diff] [blame] | 2473 | while (*fmt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | /* skip any white space in format */ |
| 2475 | /* white space in format matchs any amount of |
| 2476 | * white space, including none, in the input. |
| 2477 | */ |
| 2478 | if (isspace(*fmt)) { |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 2479 | fmt = skip_spaces(++fmt); |
| 2480 | str = skip_spaces(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | /* anything that is not a conversion must match exactly */ |
| 2484 | if (*fmt != '%' && *fmt) { |
| 2485 | if (*fmt++ != *str++) |
| 2486 | break; |
| 2487 | continue; |
| 2488 | } |
| 2489 | |
| 2490 | if (!*fmt) |
| 2491 | break; |
| 2492 | ++fmt; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | /* skip this conversion. |
| 2495 | * advance both strings to next white space |
| 2496 | */ |
| 2497 | if (*fmt == '*') { |
Jan Beulich | da99075 | 2012-10-04 17:13:24 -0700 | [diff] [blame] | 2498 | if (!*str) |
| 2499 | break; |
Andy Spencer | 8fccae2 | 2009-10-01 15:44:27 -0700 | [diff] [blame] | 2500 | while (!isspace(*fmt) && *fmt != '%' && *fmt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | fmt++; |
| 2502 | while (!isspace(*str) && *str) |
| 2503 | str++; |
| 2504 | continue; |
| 2505 | } |
| 2506 | |
| 2507 | /* get field width */ |
| 2508 | field_width = -1; |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2509 | if (isdigit(*fmt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | field_width = skip_atoi(&fmt); |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2511 | if (field_width <= 0) |
| 2512 | break; |
| 2513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | |
| 2515 | /* get conversion qualifier */ |
| 2516 | qualifier = -1; |
Andy Shevchenko | 75fb8f2 | 2011-07-25 17:13:20 -0700 | [diff] [blame] | 2517 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
| 2518 | _tolower(*fmt) == 'z') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | qualifier = *fmt++; |
| 2520 | if (unlikely(qualifier == *fmt)) { |
| 2521 | if (qualifier == 'h') { |
| 2522 | qualifier = 'H'; |
| 2523 | fmt++; |
| 2524 | } else if (qualifier == 'l') { |
| 2525 | qualifier = 'L'; |
| 2526 | fmt++; |
| 2527 | } |
| 2528 | } |
| 2529 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | |
Jan Beulich | da99075 | 2012-10-04 17:13:24 -0700 | [diff] [blame] | 2531 | if (!*fmt) |
| 2532 | break; |
| 2533 | |
| 2534 | if (*fmt == 'n') { |
| 2535 | /* return number of characters read so far */ |
| 2536 | *va_arg(args, int *) = str - buf; |
| 2537 | ++fmt; |
| 2538 | continue; |
| 2539 | } |
| 2540 | |
| 2541 | if (!*str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | break; |
| 2543 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2544 | base = 10; |
Fabian Frederick | 3f623eb | 2014-06-04 16:11:52 -0700 | [diff] [blame] | 2545 | is_sign = false; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2546 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2547 | switch (*fmt++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | case 'c': |
| 2549 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2550 | char *s = (char *)va_arg(args, char*); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | if (field_width == -1) |
| 2552 | field_width = 1; |
| 2553 | do { |
| 2554 | *s++ = *str++; |
| 2555 | } while (--field_width > 0 && *str); |
| 2556 | num++; |
| 2557 | } |
| 2558 | continue; |
| 2559 | case 's': |
| 2560 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2561 | char *s = (char *)va_arg(args, char *); |
| 2562 | if (field_width == -1) |
Alexey Dobriyan | 4be929b | 2010-05-24 14:33:03 -0700 | [diff] [blame] | 2563 | field_width = SHRT_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | /* first, skip leading white space in buffer */ |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 2565 | str = skip_spaces(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | |
| 2567 | /* now copy until next white space */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2568 | while (*str && !isspace(*str) && field_width--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | *s++ = *str++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | *s = '\0'; |
| 2571 | num++; |
| 2572 | } |
| 2573 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | case 'o': |
| 2575 | base = 8; |
| 2576 | break; |
| 2577 | case 'x': |
| 2578 | case 'X': |
| 2579 | base = 16; |
| 2580 | break; |
| 2581 | case 'i': |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2582 | base = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | case 'd': |
Fabian Frederick | 3f623eb | 2014-06-04 16:11:52 -0700 | [diff] [blame] | 2584 | is_sign = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | case 'u': |
| 2586 | break; |
| 2587 | case '%': |
| 2588 | /* looking for '%' in str */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2589 | if (*str++ != '%') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | return num; |
| 2591 | continue; |
| 2592 | default: |
| 2593 | /* invalid format; stop here */ |
| 2594 | return num; |
| 2595 | } |
| 2596 | |
| 2597 | /* have some sort of integer conversion. |
| 2598 | * first, skip white space in buffer. |
| 2599 | */ |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 2600 | str = skip_spaces(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | |
| 2602 | digit = *str; |
| 2603 | if (is_sign && digit == '-') |
| 2604 | digit = *(str + 1); |
| 2605 | |
| 2606 | if (!digit |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2607 | || (base == 16 && !isxdigit(digit)) |
| 2608 | || (base == 10 && !isdigit(digit)) |
| 2609 | || (base == 8 && (!isdigit(digit) || digit > '7')) |
| 2610 | || (base == 0 && !isdigit(digit))) |
| 2611 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2613 | if (is_sign) |
| 2614 | val.s = qualifier != 'L' ? |
| 2615 | simple_strtol(str, &next, base) : |
| 2616 | simple_strtoll(str, &next, base); |
| 2617 | else |
| 2618 | val.u = qualifier != 'L' ? |
| 2619 | simple_strtoul(str, &next, base) : |
| 2620 | simple_strtoull(str, &next, base); |
| 2621 | |
| 2622 | if (field_width > 0 && next - str > field_width) { |
| 2623 | if (base == 0) |
| 2624 | _parse_integer_fixup_radix(str, &base); |
| 2625 | while (next - str > field_width) { |
| 2626 | if (is_sign) |
| 2627 | val.s = div_s64(val.s, base); |
| 2628 | else |
| 2629 | val.u = div_u64(val.u, base); |
| 2630 | --next; |
| 2631 | } |
| 2632 | } |
| 2633 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2634 | switch (qualifier) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | case 'H': /* that's 'hh' in format */ |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2636 | if (is_sign) |
| 2637 | *va_arg(args, signed char *) = val.s; |
| 2638 | else |
| 2639 | *va_arg(args, unsigned char *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | break; |
| 2641 | case 'h': |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2642 | if (is_sign) |
| 2643 | *va_arg(args, short *) = val.s; |
| 2644 | else |
| 2645 | *va_arg(args, unsigned short *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | break; |
| 2647 | case 'l': |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2648 | if (is_sign) |
| 2649 | *va_arg(args, long *) = val.s; |
| 2650 | else |
| 2651 | *va_arg(args, unsigned long *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | break; |
| 2653 | case 'L': |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2654 | if (is_sign) |
| 2655 | *va_arg(args, long long *) = val.s; |
| 2656 | else |
| 2657 | *va_arg(args, unsigned long long *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | break; |
| 2659 | case 'Z': |
| 2660 | case 'z': |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2661 | *va_arg(args, size_t *) = val.u; |
| 2662 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | default: |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2664 | if (is_sign) |
| 2665 | *va_arg(args, int *) = val.s; |
| 2666 | else |
| 2667 | *va_arg(args, unsigned int *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | break; |
| 2669 | } |
| 2670 | num++; |
| 2671 | |
| 2672 | if (!next) |
| 2673 | break; |
| 2674 | str = next; |
| 2675 | } |
Johannes Berg | c6b40d1 | 2007-05-08 00:27:20 -0700 | [diff] [blame] | 2676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | return num; |
| 2678 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | EXPORT_SYMBOL(vsscanf); |
| 2680 | |
| 2681 | /** |
| 2682 | * sscanf - Unformat a buffer into a list of arguments |
| 2683 | * @buf: input buffer |
| 2684 | * @fmt: formatting of buffer |
| 2685 | * @...: resulting arguments |
| 2686 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2687 | int sscanf(const char *buf, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | { |
| 2689 | va_list args; |
| 2690 | int i; |
| 2691 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2692 | va_start(args, fmt); |
| 2693 | i = vsscanf(buf, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | return i; |
| 2697 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | EXPORT_SYMBOL(sscanf); |