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