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