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