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