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