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