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