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