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