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> | |
8bc3bcc9 | 20 | #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ |
1da177e4 LT |
21 | #include <linux/types.h> |
22 | #include <linux/string.h> | |
23 | #include <linux/ctype.h> | |
24 | #include <linux/kernel.h> | |
0fe1ef24 | 25 | #include <linux/kallsyms.h> |
53809751 | 26 | #include <linux/math64.h> |
0fe1ef24 | 27 | #include <linux/uaccess.h> |
332d2e78 | 28 | #include <linux/ioport.h> |
7135a8a1 | 29 | #include <linux/cred.h> |
8a27f7c9 | 30 | #include <net/addrconf.h> |
1da177e4 | 31 | |
4e57b681 | 32 | #include <asm/page.h> /* for PAGE_SIZE */ |
deac93df | 33 | #include <asm/sections.h> /* for dereference_function_descriptor() */ |
1da177e4 | 34 | |
1dff46d6 | 35 | #include "kstrtox.h" |
aa46a63e | 36 | |
1da177e4 | 37 | /** |
922ac25c | 38 | * simple_strtoull - convert a string to an unsigned long long |
1da177e4 LT |
39 | * @cp: The start of the string |
40 | * @endp: A pointer to the end of the parsed string will be placed here | |
41 | * @base: The number base to use | |
462e4711 EZ |
42 | * |
43 | * This function is obsolete. Please use kstrtoull instead. | |
1da177e4 | 44 | */ |
922ac25c | 45 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
1da177e4 | 46 | { |
1dff46d6 AD |
47 | unsigned long long result; |
48 | unsigned int rv; | |
aa46a63e | 49 | |
1dff46d6 AD |
50 | cp = _parse_integer_fixup_radix(cp, &base); |
51 | rv = _parse_integer(cp, base, &result); | |
52 | /* FIXME */ | |
53 | cp += (rv & ~KSTRTOX_OVERFLOW); | |
aa46a63e | 54 | |
1da177e4 LT |
55 | if (endp) |
56 | *endp = (char *)cp; | |
7b9186f5 | 57 | |
1da177e4 LT |
58 | return result; |
59 | } | |
922ac25c | 60 | EXPORT_SYMBOL(simple_strtoull); |
1da177e4 LT |
61 | |
62 | /** | |
922ac25c | 63 | * simple_strtoul - convert a string to an unsigned long |
1da177e4 LT |
64 | * @cp: The start of the string |
65 | * @endp: A pointer to the end of the parsed string will be placed here | |
66 | * @base: The number base to use | |
462e4711 EZ |
67 | * |
68 | * This function is obsolete. Please use kstrtoul instead. | |
1da177e4 | 69 | */ |
922ac25c | 70 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
1da177e4 | 71 | { |
922ac25c | 72 | return simple_strtoull(cp, endp, base); |
1da177e4 | 73 | } |
922ac25c | 74 | EXPORT_SYMBOL(simple_strtoul); |
1da177e4 LT |
75 | |
76 | /** | |
922ac25c | 77 | * simple_strtol - convert a string to a signed long |
1da177e4 LT |
78 | * @cp: The start of the string |
79 | * @endp: A pointer to the end of the parsed string will be placed here | |
80 | * @base: The number base to use | |
462e4711 EZ |
81 | * |
82 | * This function is obsolete. Please use kstrtol instead. | |
1da177e4 | 83 | */ |
922ac25c | 84 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
1da177e4 | 85 | { |
922ac25c AGR |
86 | if (*cp == '-') |
87 | return -simple_strtoul(cp + 1, endp, base); | |
7b9186f5 | 88 | |
922ac25c | 89 | return simple_strtoul(cp, endp, base); |
1da177e4 | 90 | } |
922ac25c | 91 | EXPORT_SYMBOL(simple_strtol); |
1da177e4 LT |
92 | |
93 | /** | |
94 | * simple_strtoll - convert a string to a signed long long | |
95 | * @cp: The start of the string | |
96 | * @endp: A pointer to the end of the parsed string will be placed here | |
97 | * @base: The number base to use | |
462e4711 EZ |
98 | * |
99 | * This function is obsolete. Please use kstrtoll instead. | |
1da177e4 | 100 | */ |
22d27051 | 101 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
1da177e4 | 102 | { |
7b9186f5 | 103 | if (*cp == '-') |
22d27051 | 104 | return -simple_strtoull(cp + 1, endp, base); |
7b9186f5 | 105 | |
22d27051 | 106 | return simple_strtoull(cp, endp, base); |
1da177e4 | 107 | } |
98d5ce0d | 108 | EXPORT_SYMBOL(simple_strtoll); |
1da177e4 | 109 | |
cf3b429b JP |
110 | static noinline_for_stack |
111 | int skip_atoi(const char **s) | |
1da177e4 | 112 | { |
7b9186f5 | 113 | int i = 0; |
1da177e4 LT |
114 | |
115 | while (isdigit(**s)) | |
116 | i = i*10 + *((*s)++) - '0'; | |
7b9186f5 | 117 | |
1da177e4 LT |
118 | return i; |
119 | } | |
120 | ||
4277eedd DV |
121 | /* Decimal conversion is by far the most typical, and is used |
122 | * for /proc and /sys data. This directly impacts e.g. top performance | |
123 | * with many processes running. We optimize it for speed | |
133fd9f5 DV |
124 | * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html> |
125 | * (with permission from the author, Douglas W. Jones). | |
126 | */ | |
4277eedd | 127 | |
133fd9f5 DV |
128 | #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 |
129 | /* Formats correctly any integer in [0, 999999999] */ | |
cf3b429b | 130 | static noinline_for_stack |
133fd9f5 | 131 | char *put_dec_full9(char *buf, unsigned q) |
4277eedd | 132 | { |
133fd9f5 | 133 | unsigned r; |
7b9186f5 | 134 | |
133fd9f5 DV |
135 | /* |
136 | * Possible ways to approx. divide by 10 | |
137 | * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit) | |
138 | * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul) | |
139 | * (x * 0x6667) >> 18 x < 43699 | |
140 | * (x * 0x3334) >> 17 x < 16389 | |
141 | * (x * 0x199a) >> 16 x < 16389 | |
142 | * (x * 0x0ccd) >> 15 x < 16389 | |
143 | * (x * 0x0667) >> 14 x < 2739 | |
144 | * (x * 0x0334) >> 13 x < 1029 | |
145 | * (x * 0x019a) >> 12 x < 1029 | |
146 | * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386) | |
147 | * (x * 0x0067) >> 10 x < 179 | |
148 | * (x * 0x0034) >> 9 x < 69 same | |
149 | * (x * 0x001a) >> 8 x < 69 same | |
150 | * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386) | |
151 | * (x * 0x0007) >> 6 x < 19 | |
152 | * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html> | |
153 | */ | |
154 | r = (q * (uint64_t)0x1999999a) >> 32; | |
155 | *buf++ = (q - 10 * r) + '0'; /* 1 */ | |
156 | q = (r * (uint64_t)0x1999999a) >> 32; | |
157 | *buf++ = (r - 10 * q) + '0'; /* 2 */ | |
158 | r = (q * (uint64_t)0x1999999a) >> 32; | |
159 | *buf++ = (q - 10 * r) + '0'; /* 3 */ | |
160 | q = (r * (uint64_t)0x1999999a) >> 32; | |
161 | *buf++ = (r - 10 * q) + '0'; /* 4 */ | |
162 | r = (q * (uint64_t)0x1999999a) >> 32; | |
163 | *buf++ = (q - 10 * r) + '0'; /* 5 */ | |
164 | /* Now value is under 10000, can avoid 64-bit multiply */ | |
165 | q = (r * 0x199a) >> 16; | |
166 | *buf++ = (r - 10 * q) + '0'; /* 6 */ | |
167 | r = (q * 0xcd) >> 11; | |
168 | *buf++ = (q - 10 * r) + '0'; /* 7 */ | |
169 | q = (r * 0xcd) >> 11; | |
170 | *buf++ = (r - 10 * q) + '0'; /* 8 */ | |
171 | *buf++ = q + '0'; /* 9 */ | |
4277eedd DV |
172 | return buf; |
173 | } | |
133fd9f5 DV |
174 | #endif |
175 | ||
176 | /* Similar to above but do not pad with zeros. | |
177 | * Code can be easily arranged to print 9 digits too, but our callers | |
178 | * always call put_dec_full9() instead when the number has 9 decimal digits. | |
179 | */ | |
cf3b429b | 180 | static noinline_for_stack |
133fd9f5 | 181 | char *put_dec_trunc8(char *buf, unsigned r) |
4277eedd | 182 | { |
133fd9f5 DV |
183 | unsigned q; |
184 | ||
185 | /* Copy of previous function's body with added early returns */ | |
cb239d0a GS |
186 | while (r >= 10000) { |
187 | q = r + '0'; | |
188 | r = (r * (uint64_t)0x1999999a) >> 32; | |
189 | *buf++ = q - 10*r; | |
190 | } | |
191 | ||
f4000516 GS |
192 | q = (r * 0x199a) >> 16; /* r <= 9999 */ |
193 | *buf++ = (r - 10 * q) + '0'; | |
133fd9f5 DV |
194 | if (q == 0) |
195 | return buf; | |
f4000516 GS |
196 | r = (q * 0xcd) >> 11; /* q <= 999 */ |
197 | *buf++ = (q - 10 * r) + '0'; | |
133fd9f5 DV |
198 | if (r == 0) |
199 | return buf; | |
f4000516 GS |
200 | q = (r * 0xcd) >> 11; /* r <= 99 */ |
201 | *buf++ = (r - 10 * q) + '0'; | |
133fd9f5 DV |
202 | if (q == 0) |
203 | return buf; | |
f4000516 | 204 | *buf++ = q + '0'; /* q <= 9 */ |
133fd9f5 DV |
205 | return buf; |
206 | } | |
4277eedd | 207 | |
133fd9f5 DV |
208 | /* There are two algorithms to print larger numbers. |
209 | * One is generic: divide by 1000000000 and repeatedly print | |
210 | * groups of (up to) 9 digits. It's conceptually simple, | |
211 | * but requires a (unsigned long long) / 1000000000 division. | |
212 | * | |
213 | * Second algorithm splits 64-bit unsigned long long into 16-bit chunks, | |
214 | * manipulates them cleverly and generates groups of 4 decimal digits. | |
215 | * It so happens that it does NOT require long long division. | |
216 | * | |
217 | * If long is > 32 bits, division of 64-bit values is relatively easy, | |
218 | * and we will use the first algorithm. | |
219 | * If long long is > 64 bits (strange architecture with VERY large long long), | |
220 | * second algorithm can't be used, and we again use the first one. | |
221 | * | |
222 | * Else (if long is 32 bits and long long is 64 bits) we use second one. | |
223 | */ | |
7b9186f5 | 224 | |
133fd9f5 DV |
225 | #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 |
226 | ||
227 | /* First algorithm: generic */ | |
228 | ||
229 | static | |
230 | char *put_dec(char *buf, unsigned long long n) | |
231 | { | |
232 | if (n >= 100*1000*1000) { | |
233 | while (n >= 1000*1000*1000) | |
234 | buf = put_dec_full9(buf, do_div(n, 1000*1000*1000)); | |
235 | if (n >= 100*1000*1000) | |
236 | return put_dec_full9(buf, n); | |
237 | } | |
238 | return put_dec_trunc8(buf, n); | |
4277eedd | 239 | } |
133fd9f5 DV |
240 | |
241 | #else | |
242 | ||
243 | /* Second algorithm: valid only for 64-bit long longs */ | |
244 | ||
e49317d4 | 245 | /* See comment in put_dec_full9 for choice of constants */ |
cf3b429b | 246 | static noinline_for_stack |
2359172a | 247 | void put_dec_full4(char *buf, unsigned q) |
4277eedd | 248 | { |
133fd9f5 | 249 | unsigned r; |
e49317d4 | 250 | r = (q * 0xccd) >> 15; |
2359172a | 251 | buf[0] = (q - 10 * r) + '0'; |
e49317d4 | 252 | q = (r * 0xcd) >> 11; |
2359172a | 253 | buf[1] = (r - 10 * q) + '0'; |
133fd9f5 | 254 | r = (q * 0xcd) >> 11; |
2359172a GS |
255 | buf[2] = (q - 10 * r) + '0'; |
256 | buf[3] = r + '0'; | |
257 | } | |
258 | ||
259 | /* | |
260 | * Call put_dec_full4 on x % 10000, return x / 10000. | |
261 | * The approximation x/10000 == (x * 0x346DC5D7) >> 43 | |
262 | * holds for all x < 1,128,869,999. The largest value this | |
263 | * helper will ever be asked to convert is 1,125,520,955. | |
264 | * (d1 in the put_dec code, assuming n is all-ones). | |
265 | */ | |
266 | static | |
267 | unsigned put_dec_helper4(char *buf, unsigned x) | |
268 | { | |
269 | uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; | |
270 | ||
271 | put_dec_full4(buf, x - q * 10000); | |
272 | return q; | |
4277eedd DV |
273 | } |
274 | ||
133fd9f5 DV |
275 | /* Based on code by Douglas W. Jones found at |
276 | * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> | |
277 | * (with permission from the author). | |
278 | * Performs no 64-bit division and hence should be fast on 32-bit machines. | |
279 | */ | |
280 | static | |
281 | char *put_dec(char *buf, unsigned long long n) | |
282 | { | |
283 | uint32_t d3, d2, d1, q, h; | |
284 | ||
285 | if (n < 100*1000*1000) | |
286 | return put_dec_trunc8(buf, n); | |
287 | ||
288 | d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ | |
289 | h = (n >> 32); | |
290 | d2 = (h ) & 0xffff; | |
291 | d3 = (h >> 16); /* implicit "& 0xffff" */ | |
292 | ||
293 | q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); | |
2359172a GS |
294 | q = put_dec_helper4(buf, q); |
295 | ||
296 | q += 7671 * d3 + 9496 * d2 + 6 * d1; | |
297 | q = put_dec_helper4(buf+4, q); | |
298 | ||
299 | q += 4749 * d3 + 42 * d2; | |
300 | q = put_dec_helper4(buf+8, q); | |
133fd9f5 | 301 | |
2359172a GS |
302 | q += 281 * d3; |
303 | buf += 12; | |
304 | if (q) | |
305 | buf = put_dec_trunc8(buf, q); | |
306 | else while (buf[-1] == '0') | |
133fd9f5 DV |
307 | --buf; |
308 | ||
309 | return buf; | |
310 | } | |
311 | ||
312 | #endif | |
313 | ||
1ac101a5 KH |
314 | /* |
315 | * Convert passed number to decimal string. | |
316 | * Returns the length of string. On buffer overflow, returns 0. | |
317 | * | |
318 | * If speed is not important, use snprintf(). It's easy to read the code. | |
319 | */ | |
320 | int num_to_str(char *buf, int size, unsigned long long num) | |
321 | { | |
133fd9f5 | 322 | char tmp[sizeof(num) * 3]; |
1ac101a5 KH |
323 | int idx, len; |
324 | ||
133fd9f5 DV |
325 | /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ |
326 | if (num <= 9) { | |
327 | tmp[0] = '0' + num; | |
328 | len = 1; | |
329 | } else { | |
330 | len = put_dec(tmp, num) - tmp; | |
331 | } | |
1ac101a5 KH |
332 | |
333 | if (len > size) | |
334 | return 0; | |
335 | for (idx = 0; idx < len; ++idx) | |
336 | buf[idx] = tmp[len - idx - 1]; | |
133fd9f5 | 337 | return len; |
1ac101a5 KH |
338 | } |
339 | ||
1da177e4 LT |
340 | #define ZEROPAD 1 /* pad with zero */ |
341 | #define SIGN 2 /* unsigned/signed long */ | |
342 | #define PLUS 4 /* show plus */ | |
343 | #define SPACE 8 /* space if plus */ | |
344 | #define LEFT 16 /* left justified */ | |
b89dc5d6 BH |
345 | #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ |
346 | #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ | |
1da177e4 | 347 | |
fef20d9c FW |
348 | enum format_type { |
349 | FORMAT_TYPE_NONE, /* Just a string part */ | |
ed681a91 | 350 | FORMAT_TYPE_WIDTH, |
fef20d9c FW |
351 | FORMAT_TYPE_PRECISION, |
352 | FORMAT_TYPE_CHAR, | |
353 | FORMAT_TYPE_STR, | |
354 | FORMAT_TYPE_PTR, | |
355 | FORMAT_TYPE_PERCENT_CHAR, | |
356 | FORMAT_TYPE_INVALID, | |
357 | FORMAT_TYPE_LONG_LONG, | |
358 | FORMAT_TYPE_ULONG, | |
359 | FORMAT_TYPE_LONG, | |
a4e94ef0 Z |
360 | FORMAT_TYPE_UBYTE, |
361 | FORMAT_TYPE_BYTE, | |
fef20d9c FW |
362 | FORMAT_TYPE_USHORT, |
363 | FORMAT_TYPE_SHORT, | |
364 | FORMAT_TYPE_UINT, | |
365 | FORMAT_TYPE_INT, | |
366 | FORMAT_TYPE_NRCHARS, | |
367 | FORMAT_TYPE_SIZE_T, | |
368 | FORMAT_TYPE_PTRDIFF | |
369 | }; | |
370 | ||
371 | struct printf_spec { | |
4e310fda | 372 | u8 type; /* format_type enum */ |
ef0658f3 | 373 | u8 flags; /* flags to number() */ |
4e310fda JP |
374 | u8 base; /* number base, 8, 10 or 16 only */ |
375 | u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ | |
376 | s16 field_width; /* width of output field */ | |
377 | s16 precision; /* # of digits/chars */ | |
fef20d9c FW |
378 | }; |
379 | ||
cf3b429b JP |
380 | static noinline_for_stack |
381 | char *number(char *buf, char *end, unsigned long long num, | |
382 | struct printf_spec spec) | |
1da177e4 | 383 | { |
9b706aee DV |
384 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ |
385 | static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ | |
386 | ||
387 | char tmp[66]; | |
388 | char sign; | |
389 | char locase; | |
fef20d9c | 390 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
1da177e4 | 391 | int i; |
7c203422 | 392 | bool is_zero = num == 0LL; |
1da177e4 | 393 | |
9b706aee DV |
394 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
395 | * produces same digits or (maybe lowercased) letters */ | |
fef20d9c FW |
396 | locase = (spec.flags & SMALL); |
397 | if (spec.flags & LEFT) | |
398 | spec.flags &= ~ZEROPAD; | |
1da177e4 | 399 | sign = 0; |
fef20d9c | 400 | if (spec.flags & SIGN) { |
7b9186f5 | 401 | if ((signed long long)num < 0) { |
1da177e4 | 402 | sign = '-'; |
7b9186f5 | 403 | num = -(signed long long)num; |
fef20d9c FW |
404 | spec.field_width--; |
405 | } else if (spec.flags & PLUS) { | |
1da177e4 | 406 | sign = '+'; |
fef20d9c FW |
407 | spec.field_width--; |
408 | } else if (spec.flags & SPACE) { | |
1da177e4 | 409 | sign = ' '; |
fef20d9c | 410 | spec.field_width--; |
1da177e4 LT |
411 | } |
412 | } | |
b39a7340 | 413 | if (need_pfx) { |
fef20d9c | 414 | if (spec.base == 16) |
7c203422 PC |
415 | spec.field_width -= 2; |
416 | else if (!is_zero) | |
fef20d9c | 417 | spec.field_width--; |
1da177e4 | 418 | } |
b39a7340 DV |
419 | |
420 | /* generate full string in tmp[], in reverse order */ | |
1da177e4 | 421 | i = 0; |
133fd9f5 DV |
422 | if (num < spec.base) |
423 | tmp[i++] = digits[num] | locase; | |
4277eedd DV |
424 | /* Generic code, for any base: |
425 | else do { | |
9b706aee | 426 | tmp[i++] = (digits[do_div(num,base)] | locase); |
4277eedd DV |
427 | } while (num != 0); |
428 | */ | |
fef20d9c FW |
429 | else if (spec.base != 10) { /* 8 or 16 */ |
430 | int mask = spec.base - 1; | |
b39a7340 | 431 | int shift = 3; |
7b9186f5 AGR |
432 | |
433 | if (spec.base == 16) | |
434 | shift = 4; | |
b39a7340 | 435 | do { |
9b706aee | 436 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); |
b39a7340 DV |
437 | num >>= shift; |
438 | } while (num); | |
4277eedd DV |
439 | } else { /* base 10 */ |
440 | i = put_dec(tmp, num) - tmp; | |
441 | } | |
b39a7340 DV |
442 | |
443 | /* printing 100 using %2d gives "100", not "00" */ | |
fef20d9c FW |
444 | if (i > spec.precision) |
445 | spec.precision = i; | |
b39a7340 | 446 | /* leading space padding */ |
fef20d9c FW |
447 | spec.field_width -= spec.precision; |
448 | if (!(spec.flags & (ZEROPAD+LEFT))) { | |
7b9186f5 | 449 | while (--spec.field_width >= 0) { |
f796937a | 450 | if (buf < end) |
1da177e4 LT |
451 | *buf = ' '; |
452 | ++buf; | |
453 | } | |
454 | } | |
b39a7340 | 455 | /* sign */ |
1da177e4 | 456 | if (sign) { |
f796937a | 457 | if (buf < end) |
1da177e4 LT |
458 | *buf = sign; |
459 | ++buf; | |
460 | } | |
b39a7340 DV |
461 | /* "0x" / "0" prefix */ |
462 | if (need_pfx) { | |
7c203422 PC |
463 | if (spec.base == 16 || !is_zero) { |
464 | if (buf < end) | |
465 | *buf = '0'; | |
466 | ++buf; | |
467 | } | |
fef20d9c | 468 | if (spec.base == 16) { |
f796937a | 469 | if (buf < end) |
9b706aee | 470 | *buf = ('X' | locase); |
1da177e4 LT |
471 | ++buf; |
472 | } | |
473 | } | |
b39a7340 | 474 | /* zero or space padding */ |
fef20d9c FW |
475 | if (!(spec.flags & LEFT)) { |
476 | char c = (spec.flags & ZEROPAD) ? '0' : ' '; | |
477 | while (--spec.field_width >= 0) { | |
f796937a | 478 | if (buf < end) |
1da177e4 LT |
479 | *buf = c; |
480 | ++buf; | |
481 | } | |
482 | } | |
b39a7340 | 483 | /* hmm even more zero padding? */ |
fef20d9c | 484 | while (i <= --spec.precision) { |
f796937a | 485 | if (buf < end) |
1da177e4 LT |
486 | *buf = '0'; |
487 | ++buf; | |
488 | } | |
b39a7340 DV |
489 | /* actual digits of result */ |
490 | while (--i >= 0) { | |
f796937a | 491 | if (buf < end) |
1da177e4 LT |
492 | *buf = tmp[i]; |
493 | ++buf; | |
494 | } | |
b39a7340 | 495 | /* trailing space padding */ |
fef20d9c | 496 | while (--spec.field_width >= 0) { |
f796937a | 497 | if (buf < end) |
1da177e4 LT |
498 | *buf = ' '; |
499 | ++buf; | |
500 | } | |
7b9186f5 | 501 | |
1da177e4 LT |
502 | return buf; |
503 | } | |
504 | ||
cf3b429b JP |
505 | static noinline_for_stack |
506 | char *string(char *buf, char *end, const char *s, struct printf_spec spec) | |
0f9bfa56 LT |
507 | { |
508 | int len, i; | |
509 | ||
510 | if ((unsigned long)s < PAGE_SIZE) | |
0f4f81dc | 511 | s = "(null)"; |
0f9bfa56 | 512 | |
fef20d9c | 513 | len = strnlen(s, spec.precision); |
0f9bfa56 | 514 | |
fef20d9c FW |
515 | if (!(spec.flags & LEFT)) { |
516 | while (len < spec.field_width--) { | |
0f9bfa56 LT |
517 | if (buf < end) |
518 | *buf = ' '; | |
519 | ++buf; | |
520 | } | |
521 | } | |
522 | for (i = 0; i < len; ++i) { | |
523 | if (buf < end) | |
524 | *buf = *s; | |
525 | ++buf; ++s; | |
526 | } | |
fef20d9c | 527 | while (len < spec.field_width--) { |
0f9bfa56 LT |
528 | if (buf < end) |
529 | *buf = ' '; | |
530 | ++buf; | |
531 | } | |
7b9186f5 | 532 | |
0f9bfa56 LT |
533 | return buf; |
534 | } | |
535 | ||
cf3b429b JP |
536 | static noinline_for_stack |
537 | char *symbol_string(char *buf, char *end, void *ptr, | |
b0d33c2b | 538 | struct printf_spec spec, const char *fmt) |
0fe1ef24 | 539 | { |
b0d33c2b | 540 | unsigned long value; |
0fe1ef24 LT |
541 | #ifdef CONFIG_KALLSYMS |
542 | char sym[KSYM_SYMBOL_LEN]; | |
b0d33c2b JP |
543 | #endif |
544 | ||
545 | if (fmt[1] == 'R') | |
546 | ptr = __builtin_extract_return_addr(ptr); | |
547 | value = (unsigned long)ptr; | |
548 | ||
549 | #ifdef CONFIG_KALLSYMS | |
550 | if (*fmt == 'B') | |
0f77a8d3 | 551 | sprint_backtrace(sym, value); |
b0d33c2b | 552 | else if (*fmt != 'f' && *fmt != 's') |
0c8b946e FW |
553 | sprint_symbol(sym, value); |
554 | else | |
4796dd20 | 555 | sprint_symbol_no_offset(sym, value); |
7b9186f5 | 556 | |
fef20d9c | 557 | return string(buf, end, sym, spec); |
0fe1ef24 | 558 | #else |
7b9186f5 | 559 | spec.field_width = 2 * sizeof(void *); |
fef20d9c FW |
560 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
561 | spec.base = 16; | |
7b9186f5 | 562 | |
fef20d9c | 563 | return number(buf, end, value, spec); |
0fe1ef24 LT |
564 | #endif |
565 | } | |
566 | ||
cf3b429b JP |
567 | static noinline_for_stack |
568 | char *resource_string(char *buf, char *end, struct resource *res, | |
569 | struct printf_spec spec, const char *fmt) | |
332d2e78 LT |
570 | { |
571 | #ifndef IO_RSRC_PRINTK_SIZE | |
28405372 | 572 | #define IO_RSRC_PRINTK_SIZE 6 |
332d2e78 LT |
573 | #endif |
574 | ||
575 | #ifndef MEM_RSRC_PRINTK_SIZE | |
28405372 | 576 | #define MEM_RSRC_PRINTK_SIZE 10 |
332d2e78 | 577 | #endif |
4da0b66c | 578 | static const struct printf_spec io_spec = { |
fef20d9c | 579 | .base = 16, |
4da0b66c | 580 | .field_width = IO_RSRC_PRINTK_SIZE, |
fef20d9c FW |
581 | .precision = -1, |
582 | .flags = SPECIAL | SMALL | ZEROPAD, | |
583 | }; | |
4da0b66c BH |
584 | static const struct printf_spec mem_spec = { |
585 | .base = 16, | |
586 | .field_width = MEM_RSRC_PRINTK_SIZE, | |
587 | .precision = -1, | |
588 | .flags = SPECIAL | SMALL | ZEROPAD, | |
589 | }; | |
0f4050c7 BH |
590 | static const struct printf_spec bus_spec = { |
591 | .base = 16, | |
592 | .field_width = 2, | |
593 | .precision = -1, | |
594 | .flags = SMALL | ZEROPAD, | |
595 | }; | |
4da0b66c | 596 | static const struct printf_spec dec_spec = { |
c91d3376 BH |
597 | .base = 10, |
598 | .precision = -1, | |
599 | .flags = 0, | |
600 | }; | |
4da0b66c | 601 | static const struct printf_spec str_spec = { |
fd95541e BH |
602 | .field_width = -1, |
603 | .precision = 10, | |
604 | .flags = LEFT, | |
605 | }; | |
4da0b66c | 606 | static const struct printf_spec flag_spec = { |
fd95541e BH |
607 | .base = 16, |
608 | .precision = -1, | |
609 | .flags = SPECIAL | SMALL, | |
610 | }; | |
c7dabef8 BH |
611 | |
612 | /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) | |
613 | * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ | |
614 | #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) | |
615 | #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) | |
9d7cca04 | 616 | #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") |
c7dabef8 BH |
617 | #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") |
618 | char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, | |
619 | 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; | |
620 | ||
332d2e78 | 621 | char *p = sym, *pend = sym + sizeof(sym); |
c7dabef8 | 622 | int decode = (fmt[0] == 'R') ? 1 : 0; |
4da0b66c | 623 | const struct printf_spec *specp; |
332d2e78 LT |
624 | |
625 | *p++ = '['; | |
4da0b66c | 626 | if (res->flags & IORESOURCE_IO) { |
c7dabef8 | 627 | p = string(p, pend, "io ", str_spec); |
4da0b66c BH |
628 | specp = &io_spec; |
629 | } else if (res->flags & IORESOURCE_MEM) { | |
c7dabef8 | 630 | p = string(p, pend, "mem ", str_spec); |
4da0b66c BH |
631 | specp = &mem_spec; |
632 | } else if (res->flags & IORESOURCE_IRQ) { | |
c7dabef8 | 633 | p = string(p, pend, "irq ", str_spec); |
4da0b66c BH |
634 | specp = &dec_spec; |
635 | } else if (res->flags & IORESOURCE_DMA) { | |
c7dabef8 | 636 | p = string(p, pend, "dma ", str_spec); |
4da0b66c | 637 | specp = &dec_spec; |
0f4050c7 BH |
638 | } else if (res->flags & IORESOURCE_BUS) { |
639 | p = string(p, pend, "bus ", str_spec); | |
640 | specp = &bus_spec; | |
4da0b66c | 641 | } else { |
c7dabef8 | 642 | p = string(p, pend, "??? ", str_spec); |
4da0b66c | 643 | specp = &mem_spec; |
c7dabef8 | 644 | decode = 0; |
fd95541e | 645 | } |
4da0b66c | 646 | p = number(p, pend, res->start, *specp); |
c91d3376 BH |
647 | if (res->start != res->end) { |
648 | *p++ = '-'; | |
4da0b66c | 649 | p = number(p, pend, res->end, *specp); |
c91d3376 | 650 | } |
c7dabef8 | 651 | if (decode) { |
fd95541e BH |
652 | if (res->flags & IORESOURCE_MEM_64) |
653 | p = string(p, pend, " 64bit", str_spec); | |
654 | if (res->flags & IORESOURCE_PREFETCH) | |
655 | p = string(p, pend, " pref", str_spec); | |
9d7cca04 BH |
656 | if (res->flags & IORESOURCE_WINDOW) |
657 | p = string(p, pend, " window", str_spec); | |
fd95541e BH |
658 | if (res->flags & IORESOURCE_DISABLED) |
659 | p = string(p, pend, " disabled", str_spec); | |
c7dabef8 BH |
660 | } else { |
661 | p = string(p, pend, " flags ", str_spec); | |
662 | p = number(p, pend, res->flags, flag_spec); | |
fd95541e | 663 | } |
332d2e78 | 664 | *p++ = ']'; |
c7dabef8 | 665 | *p = '\0'; |
332d2e78 | 666 | |
fef20d9c | 667 | return string(buf, end, sym, spec); |
332d2e78 LT |
668 | } |
669 | ||
31550a16 AS |
670 | static noinline_for_stack |
671 | char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, | |
672 | const char *fmt) | |
673 | { | |
674 | int i, len = 1; /* if we pass '%ph[CDN]', field witdh remains | |
675 | negative value, fallback to the default */ | |
676 | char separator; | |
677 | ||
678 | if (spec.field_width == 0) | |
679 | /* nothing to print */ | |
680 | return buf; | |
681 | ||
682 | if (ZERO_OR_NULL_PTR(addr)) | |
683 | /* NULL pointer */ | |
684 | return string(buf, end, NULL, spec); | |
685 | ||
686 | switch (fmt[1]) { | |
687 | case 'C': | |
688 | separator = ':'; | |
689 | break; | |
690 | case 'D': | |
691 | separator = '-'; | |
692 | break; | |
693 | case 'N': | |
694 | separator = 0; | |
695 | break; | |
696 | default: | |
697 | separator = ' '; | |
698 | break; | |
699 | } | |
700 | ||
701 | if (spec.field_width > 0) | |
702 | len = min_t(int, spec.field_width, 64); | |
703 | ||
704 | for (i = 0; i < len && buf < end - 1; i++) { | |
705 | buf = hex_byte_pack(buf, addr[i]); | |
706 | ||
707 | if (buf < end && separator && i != len - 1) | |
708 | *buf++ = separator; | |
709 | } | |
710 | ||
711 | return buf; | |
712 | } | |
713 | ||
cf3b429b JP |
714 | static noinline_for_stack |
715 | char *mac_address_string(char *buf, char *end, u8 *addr, | |
716 | struct printf_spec spec, const char *fmt) | |
dd45c9cf | 717 | { |
8a27f7c9 | 718 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
dd45c9cf HH |
719 | char *p = mac_addr; |
720 | int i; | |
bc7259a2 | 721 | char separator; |
76597ff9 | 722 | bool reversed = false; |
bc7259a2 | 723 | |
76597ff9 AE |
724 | switch (fmt[1]) { |
725 | case 'F': | |
bc7259a2 | 726 | separator = '-'; |
76597ff9 AE |
727 | break; |
728 | ||
729 | case 'R': | |
730 | reversed = true; | |
731 | /* fall through */ | |
732 | ||
733 | default: | |
bc7259a2 | 734 | separator = ':'; |
76597ff9 | 735 | break; |
bc7259a2 | 736 | } |
dd45c9cf HH |
737 | |
738 | for (i = 0; i < 6; i++) { | |
76597ff9 AE |
739 | if (reversed) |
740 | p = hex_byte_pack(p, addr[5 - i]); | |
741 | else | |
742 | p = hex_byte_pack(p, addr[i]); | |
743 | ||
8a27f7c9 | 744 | if (fmt[0] == 'M' && i != 5) |
bc7259a2 | 745 | *p++ = separator; |
dd45c9cf HH |
746 | } |
747 | *p = '\0'; | |
748 | ||
fef20d9c | 749 | return string(buf, end, mac_addr, spec); |
dd45c9cf HH |
750 | } |
751 | ||
cf3b429b JP |
752 | static noinline_for_stack |
753 | char *ip4_string(char *p, const u8 *addr, const char *fmt) | |
8a27f7c9 JP |
754 | { |
755 | int i; | |
0159f24e JP |
756 | bool leading_zeros = (fmt[0] == 'i'); |
757 | int index; | |
758 | int step; | |
759 | ||
760 | switch (fmt[2]) { | |
761 | case 'h': | |
762 | #ifdef __BIG_ENDIAN | |
763 | index = 0; | |
764 | step = 1; | |
765 | #else | |
766 | index = 3; | |
767 | step = -1; | |
768 | #endif | |
769 | break; | |
770 | case 'l': | |
771 | index = 3; | |
772 | step = -1; | |
773 | break; | |
774 | case 'n': | |
775 | case 'b': | |
776 | default: | |
777 | index = 0; | |
778 | step = 1; | |
779 | break; | |
780 | } | |
8a27f7c9 JP |
781 | for (i = 0; i < 4; i++) { |
782 | char temp[3]; /* hold each IP quad in reverse order */ | |
133fd9f5 | 783 | int digits = put_dec_trunc8(temp, addr[index]) - temp; |
8a27f7c9 JP |
784 | if (leading_zeros) { |
785 | if (digits < 3) | |
786 | *p++ = '0'; | |
787 | if (digits < 2) | |
788 | *p++ = '0'; | |
789 | } | |
790 | /* reverse the digits in the quad */ | |
791 | while (digits--) | |
792 | *p++ = temp[digits]; | |
793 | if (i < 3) | |
794 | *p++ = '.'; | |
0159f24e | 795 | index += step; |
8a27f7c9 | 796 | } |
8a27f7c9 | 797 | *p = '\0'; |
7b9186f5 | 798 | |
8a27f7c9 JP |
799 | return p; |
800 | } | |
801 | ||
cf3b429b JP |
802 | static noinline_for_stack |
803 | char *ip6_compressed_string(char *p, const char *addr) | |
689afa7d | 804 | { |
7b9186f5 | 805 | int i, j, range; |
8a27f7c9 JP |
806 | unsigned char zerolength[8]; |
807 | int longest = 1; | |
808 | int colonpos = -1; | |
809 | u16 word; | |
7b9186f5 | 810 | u8 hi, lo; |
8a27f7c9 | 811 | bool needcolon = false; |
eb78cd26 JP |
812 | bool useIPv4; |
813 | struct in6_addr in6; | |
814 | ||
815 | memcpy(&in6, addr, sizeof(struct in6_addr)); | |
816 | ||
817 | useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); | |
8a27f7c9 JP |
818 | |
819 | memset(zerolength, 0, sizeof(zerolength)); | |
820 | ||
821 | if (useIPv4) | |
822 | range = 6; | |
823 | else | |
824 | range = 8; | |
825 | ||
826 | /* find position of longest 0 run */ | |
827 | for (i = 0; i < range; i++) { | |
828 | for (j = i; j < range; j++) { | |
eb78cd26 | 829 | if (in6.s6_addr16[j] != 0) |
8a27f7c9 JP |
830 | break; |
831 | zerolength[i]++; | |
832 | } | |
833 | } | |
834 | for (i = 0; i < range; i++) { | |
835 | if (zerolength[i] > longest) { | |
836 | longest = zerolength[i]; | |
837 | colonpos = i; | |
838 | } | |
839 | } | |
29cf519e JP |
840 | if (longest == 1) /* don't compress a single 0 */ |
841 | colonpos = -1; | |
689afa7d | 842 | |
8a27f7c9 JP |
843 | /* emit address */ |
844 | for (i = 0; i < range; i++) { | |
845 | if (i == colonpos) { | |
846 | if (needcolon || i == 0) | |
847 | *p++ = ':'; | |
848 | *p++ = ':'; | |
849 | needcolon = false; | |
850 | i += longest - 1; | |
851 | continue; | |
852 | } | |
853 | if (needcolon) { | |
854 | *p++ = ':'; | |
855 | needcolon = false; | |
856 | } | |
857 | /* hex u16 without leading 0s */ | |
eb78cd26 | 858 | word = ntohs(in6.s6_addr16[i]); |
8a27f7c9 JP |
859 | hi = word >> 8; |
860 | lo = word & 0xff; | |
861 | if (hi) { | |
862 | if (hi > 0x0f) | |
55036ba7 | 863 | p = hex_byte_pack(p, hi); |
8a27f7c9 JP |
864 | else |
865 | *p++ = hex_asc_lo(hi); | |
55036ba7 | 866 | p = hex_byte_pack(p, lo); |
8a27f7c9 | 867 | } |
b5ff992b | 868 | else if (lo > 0x0f) |
55036ba7 | 869 | p = hex_byte_pack(p, lo); |
8a27f7c9 JP |
870 | else |
871 | *p++ = hex_asc_lo(lo); | |
872 | needcolon = true; | |
873 | } | |
874 | ||
875 | if (useIPv4) { | |
876 | if (needcolon) | |
877 | *p++ = ':'; | |
0159f24e | 878 | p = ip4_string(p, &in6.s6_addr[12], "I4"); |
8a27f7c9 | 879 | } |
8a27f7c9 | 880 | *p = '\0'; |
7b9186f5 | 881 | |
8a27f7c9 JP |
882 | return p; |
883 | } | |
884 | ||
cf3b429b JP |
885 | static noinline_for_stack |
886 | char *ip6_string(char *p, const char *addr, const char *fmt) | |
8a27f7c9 JP |
887 | { |
888 | int i; | |
7b9186f5 | 889 | |
689afa7d | 890 | for (i = 0; i < 8; i++) { |
55036ba7 AS |
891 | p = hex_byte_pack(p, *addr++); |
892 | p = hex_byte_pack(p, *addr++); | |
8a27f7c9 | 893 | if (fmt[0] == 'I' && i != 7) |
689afa7d HH |
894 | *p++ = ':'; |
895 | } | |
896 | *p = '\0'; | |
7b9186f5 | 897 | |
8a27f7c9 JP |
898 | return p; |
899 | } | |
900 | ||
cf3b429b JP |
901 | static noinline_for_stack |
902 | char *ip6_addr_string(char *buf, char *end, const u8 *addr, | |
903 | struct printf_spec spec, const char *fmt) | |
8a27f7c9 JP |
904 | { |
905 | char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; | |
906 | ||
907 | if (fmt[0] == 'I' && fmt[2] == 'c') | |
eb78cd26 | 908 | ip6_compressed_string(ip6_addr, addr); |
8a27f7c9 | 909 | else |
eb78cd26 | 910 | ip6_string(ip6_addr, addr, fmt); |
689afa7d | 911 | |
fef20d9c | 912 | return string(buf, end, ip6_addr, spec); |
689afa7d HH |
913 | } |
914 | ||
cf3b429b JP |
915 | static noinline_for_stack |
916 | char *ip4_addr_string(char *buf, char *end, const u8 *addr, | |
917 | struct printf_spec spec, const char *fmt) | |
4aa99606 | 918 | { |
8a27f7c9 | 919 | char ip4_addr[sizeof("255.255.255.255")]; |
4aa99606 | 920 | |
0159f24e | 921 | ip4_string(ip4_addr, addr, fmt); |
4aa99606 | 922 | |
fef20d9c | 923 | return string(buf, end, ip4_addr, spec); |
4aa99606 HH |
924 | } |
925 | ||
cf3b429b JP |
926 | static noinline_for_stack |
927 | char *uuid_string(char *buf, char *end, const u8 *addr, | |
928 | struct printf_spec spec, const char *fmt) | |
9ac6e44e JP |
929 | { |
930 | char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; | |
931 | char *p = uuid; | |
932 | int i; | |
933 | static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; | |
934 | static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; | |
935 | const u8 *index = be; | |
936 | bool uc = false; | |
937 | ||
938 | switch (*(++fmt)) { | |
939 | case 'L': | |
940 | uc = true; /* fall-through */ | |
941 | case 'l': | |
942 | index = le; | |
943 | break; | |
944 | case 'B': | |
945 | uc = true; | |
946 | break; | |
947 | } | |
948 | ||
949 | for (i = 0; i < 16; i++) { | |
55036ba7 | 950 | p = hex_byte_pack(p, addr[index[i]]); |
9ac6e44e JP |
951 | switch (i) { |
952 | case 3: | |
953 | case 5: | |
954 | case 7: | |
955 | case 9: | |
956 | *p++ = '-'; | |
957 | break; | |
958 | } | |
959 | } | |
960 | ||
961 | *p = 0; | |
962 | ||
963 | if (uc) { | |
964 | p = uuid; | |
965 | do { | |
966 | *p = toupper(*p); | |
967 | } while (*(++p)); | |
968 | } | |
969 | ||
970 | return string(buf, end, uuid, spec); | |
971 | } | |
972 | ||
c8f44aff MM |
973 | static |
974 | char *netdev_feature_string(char *buf, char *end, const u8 *addr, | |
975 | struct printf_spec spec) | |
976 | { | |
977 | spec.flags |= SPECIAL | SMALL | ZEROPAD; | |
978 | if (spec.field_width == -1) | |
979 | spec.field_width = 2 + 2 * sizeof(netdev_features_t); | |
980 | spec.base = 16; | |
981 | ||
982 | return number(buf, end, *(const netdev_features_t *)addr, spec); | |
983 | } | |
984 | ||
411f05f1 | 985 | int kptr_restrict __read_mostly; |
455cd5ab | 986 | |
4d8a743c LT |
987 | /* |
988 | * Show a '%p' thing. A kernel extension is that the '%p' is followed | |
989 | * by an extra set of alphanumeric characters that are extended format | |
990 | * specifiers. | |
991 | * | |
332d2e78 LT |
992 | * Right now we handle: |
993 | * | |
0c8b946e FW |
994 | * - 'F' For symbolic function descriptor pointers with offset |
995 | * - 'f' For simple symbolic function names without offset | |
0efb4d20 SR |
996 | * - 'S' For symbolic direct pointers with offset |
997 | * - 's' For symbolic direct pointers without offset | |
b0d33c2b | 998 | * - '[FfSs]R' as above with __builtin_extract_return_addr() translation |
0f77a8d3 | 999 | * - 'B' For backtraced symbolic direct pointers with offset |
c7dabef8 BH |
1000 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
1001 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] | |
dd45c9cf HH |
1002 | * - 'M' For a 6-byte MAC address, it prints the address in the |
1003 | * usual colon-separated hex notation | |
8a27f7c9 | 1004 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
bc7259a2 | 1005 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
c8e00060 | 1006 | * with a dash-separated hex notation |
7c59154e | 1007 | * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) |
8a27f7c9 JP |
1008 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
1009 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) | |
1010 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's | |
1011 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses | |
1012 | * IPv6 omits the colons (01020304...0f) | |
1013 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) | |
0159f24e | 1014 | * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order |
8a27f7c9 | 1015 | * - 'I6c' for IPv6 addresses printed as specified by |
29cf519e | 1016 | * http://tools.ietf.org/html/rfc5952 |
9ac6e44e JP |
1017 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form |
1018 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | |
1019 | * Options for %pU are: | |
1020 | * b big endian lower case hex (default) | |
1021 | * B big endian UPPER case hex | |
1022 | * l little endian lower case hex | |
1023 | * L little endian UPPER case hex | |
1024 | * big endian output byte order is: | |
1025 | * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] | |
1026 | * little endian output byte order is: | |
1027 | * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] | |
7db6f5fb JP |
1028 | * - 'V' For a struct va_format which contains a format string * and va_list *, |
1029 | * call vsnprintf(->format, *->va_list). | |
1030 | * Implements a "recursive vsnprintf". | |
1031 | * Do not use this feature without some mechanism to verify the | |
1032 | * correctness of the format string and va_list arguments. | |
455cd5ab | 1033 | * - 'K' For a kernel pointer that should be hidden from unprivileged users |
c8f44aff | 1034 | * - 'NF' For a netdev_features_t |
31550a16 AS |
1035 | * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with |
1036 | * a certain separator (' ' by default): | |
1037 | * C colon | |
1038 | * D dash | |
1039 | * N no separator | |
1040 | * The maximum supported length is 64 bytes of the input. Consider | |
1041 | * to use print_hex_dump() for the larger input. | |
7d799210 | 1042 | * - 'a' For a phys_addr_t type and its derivative types (passed by reference) |
9ac6e44e | 1043 | * |
332d2e78 LT |
1044 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
1045 | * function pointers are really function descriptors, which contain a | |
1046 | * pointer to the real address. | |
4d8a743c | 1047 | */ |
cf3b429b JP |
1048 | static noinline_for_stack |
1049 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, | |
1050 | struct printf_spec spec) | |
78a8bf69 | 1051 | { |
725fe002 GL |
1052 | int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0); |
1053 | ||
9f36e2c4 | 1054 | if (!ptr && *fmt != 'K') { |
5e057981 JP |
1055 | /* |
1056 | * Print (null) with the same width as a pointer so it makes | |
1057 | * tabular output look nice. | |
1058 | */ | |
1059 | if (spec.field_width == -1) | |
725fe002 | 1060 | spec.field_width = default_width; |
fef20d9c | 1061 | return string(buf, end, "(null)", spec); |
5e057981 | 1062 | } |
d97106ab | 1063 | |
0fe1ef24 LT |
1064 | switch (*fmt) { |
1065 | case 'F': | |
0c8b946e | 1066 | case 'f': |
0fe1ef24 LT |
1067 | ptr = dereference_function_descriptor(ptr); |
1068 | /* Fallthrough */ | |
1069 | case 'S': | |
9ac6e44e | 1070 | case 's': |
0f77a8d3 | 1071 | case 'B': |
b0d33c2b | 1072 | return symbol_string(buf, end, ptr, spec, fmt); |
332d2e78 | 1073 | case 'R': |
c7dabef8 | 1074 | case 'r': |
fd95541e | 1075 | return resource_string(buf, end, ptr, spec, fmt); |
31550a16 AS |
1076 | case 'h': |
1077 | return hex_string(buf, end, ptr, spec, fmt); | |
8a27f7c9 JP |
1078 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
1079 | case 'm': /* Contiguous: 000102030405 */ | |
76597ff9 AE |
1080 | /* [mM]F (FDDI) */ |
1081 | /* [mM]R (Reverse order; Bluetooth) */ | |
8a27f7c9 JP |
1082 | return mac_address_string(buf, end, ptr, spec, fmt); |
1083 | case 'I': /* Formatted IP supported | |
1084 | * 4: 1.2.3.4 | |
1085 | * 6: 0001:0203:...:0708 | |
1086 | * 6c: 1::708 or 1::1.2.3.4 | |
1087 | */ | |
1088 | case 'i': /* Contiguous: | |
1089 | * 4: 001.002.003.004 | |
1090 | * 6: 000102...0f | |
1091 | */ | |
1092 | switch (fmt[1]) { | |
1093 | case '6': | |
1094 | return ip6_addr_string(buf, end, ptr, spec, fmt); | |
1095 | case '4': | |
1096 | return ip4_addr_string(buf, end, ptr, spec, fmt); | |
1097 | } | |
fef20d9c | 1098 | break; |
9ac6e44e JP |
1099 | case 'U': |
1100 | return uuid_string(buf, end, ptr, spec, fmt); | |
7db6f5fb | 1101 | case 'V': |
5756b76e JB |
1102 | { |
1103 | va_list va; | |
1104 | ||
1105 | va_copy(va, *((struct va_format *)ptr)->va); | |
1106 | buf += vsnprintf(buf, end > buf ? end - buf : 0, | |
1107 | ((struct va_format *)ptr)->fmt, va); | |
1108 | va_end(va); | |
1109 | return buf; | |
1110 | } | |
455cd5ab DR |
1111 | case 'K': |
1112 | /* | |
1113 | * %pK cannot be used in IRQ context because its test | |
1114 | * for CAP_SYSLOG would be meaningless. | |
1115 | */ | |
3715c530 DR |
1116 | if (kptr_restrict && (in_irq() || in_serving_softirq() || |
1117 | in_nmi())) { | |
455cd5ab | 1118 | if (spec.field_width == -1) |
725fe002 | 1119 | spec.field_width = default_width; |
455cd5ab | 1120 | return string(buf, end, "pK-error", spec); |
455cd5ab | 1121 | } |
7135a8a1 RM |
1122 | |
1123 | switch (kptr_restrict) { | |
1124 | case 0: | |
1125 | /* Always print %pK values */ | |
1126 | break; | |
1127 | case 1: { | |
1128 | /* | |
1129 | * Only print the real pointer value if the current | |
1130 | * process has CAP_SYSLOG and is running with the | |
1131 | * same credentials it started with. This is because | |
1132 | * access to files is checked at open() time, but %pK | |
1133 | * checks permission at read() time. We don't want to | |
1134 | * leak pointer values if a binary opens a file using | |
1135 | * %pK and then elevates privileges before reading it. | |
1136 | */ | |
1137 | const struct cred *cred = current_cred(); | |
1138 | ||
1139 | if (!has_capability_noaudit(current, CAP_SYSLOG) || | |
1140 | !uid_eq(cred->euid, cred->uid) || | |
1141 | !gid_eq(cred->egid, cred->gid)) | |
1142 | ptr = NULL; | |
1143 | break; | |
1144 | } | |
1145 | case 2: | |
1146 | default: | |
1147 | /* Always print 0's for %pK */ | |
26297607 | 1148 | ptr = NULL; |
7135a8a1 RM |
1149 | break; |
1150 | } | |
26297607 | 1151 | break; |
7135a8a1 | 1152 | |
c8f44aff MM |
1153 | case 'N': |
1154 | switch (fmt[1]) { | |
1155 | case 'F': | |
1156 | return netdev_feature_string(buf, end, ptr, spec); | |
1157 | } | |
1158 | break; | |
7d799210 SM |
1159 | case 'a': |
1160 | spec.flags |= SPECIAL | SMALL | ZEROPAD; | |
1161 | spec.field_width = sizeof(phys_addr_t) * 2 + 2; | |
1162 | spec.base = 16; | |
1163 | return number(buf, end, | |
1164 | (unsigned long long) *((phys_addr_t *)ptr), spec); | |
fef20d9c FW |
1165 | } |
1166 | spec.flags |= SMALL; | |
1167 | if (spec.field_width == -1) { | |
725fe002 | 1168 | spec.field_width = default_width; |
fef20d9c FW |
1169 | spec.flags |= ZEROPAD; |
1170 | } | |
1171 | spec.base = 16; | |
1172 | ||
1173 | return number(buf, end, (unsigned long) ptr, spec); | |
1174 | } | |
1175 | ||
1176 | /* | |
1177 | * Helper function to decode printf style format. | |
1178 | * Each call decode a token from the format and return the | |
1179 | * number of characters read (or likely the delta where it wants | |
1180 | * to go on the next call). | |
1181 | * The decoded token is returned through the parameters | |
1182 | * | |
1183 | * 'h', 'l', or 'L' for integer fields | |
1184 | * 'z' support added 23/7/1999 S.H. | |
1185 | * 'z' changed to 'Z' --davidm 1/25/99 | |
1186 | * 't' added for ptrdiff_t | |
1187 | * | |
1188 | * @fmt: the format string | |
1189 | * @type of the token returned | |
1190 | * @flags: various flags such as +, -, # tokens.. | |
1191 | * @field_width: overwritten width | |
1192 | * @base: base of the number (octal, hex, ...) | |
1193 | * @precision: precision of a number | |
1194 | * @qualifier: qualifier of a number (long, size_t, ...) | |
1195 | */ | |
cf3b429b JP |
1196 | static noinline_for_stack |
1197 | int format_decode(const char *fmt, struct printf_spec *spec) | |
fef20d9c FW |
1198 | { |
1199 | const char *start = fmt; | |
fef20d9c FW |
1200 | |
1201 | /* we finished early by reading the field width */ | |
ed681a91 | 1202 | if (spec->type == FORMAT_TYPE_WIDTH) { |
fef20d9c FW |
1203 | if (spec->field_width < 0) { |
1204 | spec->field_width = -spec->field_width; | |
1205 | spec->flags |= LEFT; | |
1206 | } | |
1207 | spec->type = FORMAT_TYPE_NONE; | |
1208 | goto precision; | |
1209 | } | |
1210 | ||
1211 | /* we finished early by reading the precision */ | |
1212 | if (spec->type == FORMAT_TYPE_PRECISION) { | |
1213 | if (spec->precision < 0) | |
1214 | spec->precision = 0; | |
1215 | ||
1216 | spec->type = FORMAT_TYPE_NONE; | |
1217 | goto qualifier; | |
1218 | } | |
1219 | ||
1220 | /* By default */ | |
1221 | spec->type = FORMAT_TYPE_NONE; | |
1222 | ||
1223 | for (; *fmt ; ++fmt) { | |
1224 | if (*fmt == '%') | |
1225 | break; | |
1226 | } | |
1227 | ||
1228 | /* Return the current non-format string */ | |
1229 | if (fmt != start || !*fmt) | |
1230 | return fmt - start; | |
1231 | ||
1232 | /* Process flags */ | |
1233 | spec->flags = 0; | |
1234 | ||
1235 | while (1) { /* this also skips first '%' */ | |
1236 | bool found = true; | |
1237 | ||
1238 | ++fmt; | |
1239 | ||
1240 | switch (*fmt) { | |
1241 | case '-': spec->flags |= LEFT; break; | |
1242 | case '+': spec->flags |= PLUS; break; | |
1243 | case ' ': spec->flags |= SPACE; break; | |
1244 | case '#': spec->flags |= SPECIAL; break; | |
1245 | case '0': spec->flags |= ZEROPAD; break; | |
1246 | default: found = false; | |
1247 | } | |
1248 | ||
1249 | if (!found) | |
1250 | break; | |
1251 | } | |
1252 | ||
1253 | /* get field width */ | |
1254 | spec->field_width = -1; | |
1255 | ||
1256 | if (isdigit(*fmt)) | |
1257 | spec->field_width = skip_atoi(&fmt); | |
1258 | else if (*fmt == '*') { | |
1259 | /* it's the next argument */ | |
ed681a91 | 1260 | spec->type = FORMAT_TYPE_WIDTH; |
fef20d9c FW |
1261 | return ++fmt - start; |
1262 | } | |
1263 | ||
1264 | precision: | |
1265 | /* get the precision */ | |
1266 | spec->precision = -1; | |
1267 | if (*fmt == '.') { | |
1268 | ++fmt; | |
1269 | if (isdigit(*fmt)) { | |
1270 | spec->precision = skip_atoi(&fmt); | |
1271 | if (spec->precision < 0) | |
1272 | spec->precision = 0; | |
1273 | } else if (*fmt == '*') { | |
1274 | /* it's the next argument */ | |
adf26f84 | 1275 | spec->type = FORMAT_TYPE_PRECISION; |
fef20d9c FW |
1276 | return ++fmt - start; |
1277 | } | |
1278 | } | |
1279 | ||
1280 | qualifier: | |
1281 | /* get the conversion qualifier */ | |
1282 | spec->qualifier = -1; | |
75fb8f26 AS |
1283 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
1284 | _tolower(*fmt) == 'z' || *fmt == 't') { | |
a4e94ef0 Z |
1285 | spec->qualifier = *fmt++; |
1286 | if (unlikely(spec->qualifier == *fmt)) { | |
1287 | if (spec->qualifier == 'l') { | |
1288 | spec->qualifier = 'L'; | |
1289 | ++fmt; | |
1290 | } else if (spec->qualifier == 'h') { | |
1291 | spec->qualifier = 'H'; | |
1292 | ++fmt; | |
1293 | } | |
fef20d9c FW |
1294 | } |
1295 | } | |
1296 | ||
1297 | /* default base */ | |
1298 | spec->base = 10; | |
1299 | switch (*fmt) { | |
1300 | case 'c': | |
1301 | spec->type = FORMAT_TYPE_CHAR; | |
1302 | return ++fmt - start; | |
1303 | ||
1304 | case 's': | |
1305 | spec->type = FORMAT_TYPE_STR; | |
1306 | return ++fmt - start; | |
1307 | ||
1308 | case 'p': | |
1309 | spec->type = FORMAT_TYPE_PTR; | |
1310 | return fmt - start; | |
1311 | /* skip alnum */ | |
1312 | ||
1313 | case 'n': | |
1314 | spec->type = FORMAT_TYPE_NRCHARS; | |
1315 | return ++fmt - start; | |
1316 | ||
1317 | case '%': | |
1318 | spec->type = FORMAT_TYPE_PERCENT_CHAR; | |
1319 | return ++fmt - start; | |
1320 | ||
1321 | /* integer number formats - set up the flags and "break" */ | |
1322 | case 'o': | |
1323 | spec->base = 8; | |
1324 | break; | |
1325 | ||
1326 | case 'x': | |
1327 | spec->flags |= SMALL; | |
1328 | ||
1329 | case 'X': | |
1330 | spec->base = 16; | |
1331 | break; | |
1332 | ||
1333 | case 'd': | |
1334 | case 'i': | |
39e874f8 | 1335 | spec->flags |= SIGN; |
fef20d9c | 1336 | case 'u': |
4aa99606 | 1337 | break; |
fef20d9c FW |
1338 | |
1339 | default: | |
1340 | spec->type = FORMAT_TYPE_INVALID; | |
1341 | return fmt - start; | |
0fe1ef24 | 1342 | } |
fef20d9c FW |
1343 | |
1344 | if (spec->qualifier == 'L') | |
1345 | spec->type = FORMAT_TYPE_LONG_LONG; | |
1346 | else if (spec->qualifier == 'l') { | |
39e874f8 | 1347 | if (spec->flags & SIGN) |
fef20d9c FW |
1348 | spec->type = FORMAT_TYPE_LONG; |
1349 | else | |
1350 | spec->type = FORMAT_TYPE_ULONG; | |
75fb8f26 | 1351 | } else if (_tolower(spec->qualifier) == 'z') { |
fef20d9c FW |
1352 | spec->type = FORMAT_TYPE_SIZE_T; |
1353 | } else if (spec->qualifier == 't') { | |
1354 | spec->type = FORMAT_TYPE_PTRDIFF; | |
a4e94ef0 Z |
1355 | } else if (spec->qualifier == 'H') { |
1356 | if (spec->flags & SIGN) | |
1357 | spec->type = FORMAT_TYPE_BYTE; | |
1358 | else | |
1359 | spec->type = FORMAT_TYPE_UBYTE; | |
fef20d9c | 1360 | } else if (spec->qualifier == 'h') { |
39e874f8 | 1361 | if (spec->flags & SIGN) |
fef20d9c FW |
1362 | spec->type = FORMAT_TYPE_SHORT; |
1363 | else | |
1364 | spec->type = FORMAT_TYPE_USHORT; | |
1365 | } else { | |
39e874f8 | 1366 | if (spec->flags & SIGN) |
fef20d9c FW |
1367 | spec->type = FORMAT_TYPE_INT; |
1368 | else | |
1369 | spec->type = FORMAT_TYPE_UINT; | |
78a8bf69 | 1370 | } |
fef20d9c FW |
1371 | |
1372 | return ++fmt - start; | |
78a8bf69 LT |
1373 | } |
1374 | ||
1da177e4 LT |
1375 | /** |
1376 | * vsnprintf - Format a string and place it in a buffer | |
1377 | * @buf: The buffer to place the result into | |
1378 | * @size: The size of the buffer, including the trailing null space | |
1379 | * @fmt: The format string to use | |
1380 | * @args: Arguments for the format string | |
1381 | * | |
20036fdc | 1382 | * This function follows C99 vsnprintf, but has some extensions: |
91adcd2c SR |
1383 | * %pS output the name of a text symbol with offset |
1384 | * %ps output the name of a text symbol without offset | |
0c8b946e FW |
1385 | * %pF output the name of a function pointer with its offset |
1386 | * %pf output the name of a function pointer without its offset | |
0f77a8d3 | 1387 | * %pB output the name of a backtrace symbol with its offset |
8a79503a UKK |
1388 | * %pR output the address range in a struct resource with decoded flags |
1389 | * %pr output the address range in a struct resource with raw flags | |
1390 | * %pM output a 6-byte MAC address with colons | |
7c59154e AS |
1391 | * %pMR output a 6-byte MAC address with colons in reversed order |
1392 | * %pMF output a 6-byte MAC address with dashes | |
8a79503a | 1393 | * %pm output a 6-byte MAC address without colons |
7c59154e | 1394 | * %pmR output a 6-byte MAC address without colons in reversed order |
8a79503a UKK |
1395 | * %pI4 print an IPv4 address without leading zeros |
1396 | * %pi4 print an IPv4 address with leading zeros | |
1397 | * %pI6 print an IPv6 address with colons | |
1398 | * %pi6 print an IPv6 address without colons | |
f996f208 | 1399 | * %pI6c print an IPv6 address as specified by RFC 5952 |
8a79503a UKK |
1400 | * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper |
1401 | * case. | |
31550a16 AS |
1402 | * %*ph[CDN] a variable-length hex string with a separator (supports up to 64 |
1403 | * bytes of the input) | |
0efb4d20 | 1404 | * %n is ignored |
20036fdc | 1405 | * |
80f548e0 AM |
1406 | * ** Please update Documentation/printk-formats.txt when making changes ** |
1407 | * | |
1da177e4 LT |
1408 | * The return value is the number of characters which would |
1409 | * be generated for the given input, excluding the trailing | |
1410 | * '\0', as per ISO C99. If you want to have the exact | |
1411 | * number of characters written into @buf as return value | |
72fd4a35 | 1412 | * (not including the trailing '\0'), use vscnprintf(). If the |
1da177e4 LT |
1413 | * return is greater than or equal to @size, the resulting |
1414 | * string is truncated. | |
1415 | * | |
ba1835eb | 1416 | * If you're not already dealing with a va_list consider using snprintf(). |
1da177e4 LT |
1417 | */ |
1418 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | |
1419 | { | |
1da177e4 | 1420 | unsigned long long num; |
d4be151b | 1421 | char *str, *end; |
fef20d9c | 1422 | struct printf_spec spec = {0}; |
1da177e4 | 1423 | |
f796937a JF |
1424 | /* Reject out-of-range values early. Large positive sizes are |
1425 | used for unknown buffer sizes. */ | |
2f30b1f9 | 1426 | if (WARN_ON_ONCE((int) size < 0)) |
1da177e4 | 1427 | return 0; |
1da177e4 LT |
1428 | |
1429 | str = buf; | |
f796937a | 1430 | end = buf + size; |
1da177e4 | 1431 | |
f796937a JF |
1432 | /* Make sure end is always >= buf */ |
1433 | if (end < buf) { | |
1434 | end = ((void *)-1); | |
1435 | size = end - buf; | |
1da177e4 LT |
1436 | } |
1437 | ||
fef20d9c FW |
1438 | while (*fmt) { |
1439 | const char *old_fmt = fmt; | |
d4be151b | 1440 | int read = format_decode(fmt, &spec); |
1da177e4 | 1441 | |
fef20d9c | 1442 | fmt += read; |
1da177e4 | 1443 | |
fef20d9c FW |
1444 | switch (spec.type) { |
1445 | case FORMAT_TYPE_NONE: { | |
1446 | int copy = read; | |
1447 | if (str < end) { | |
1448 | if (copy > end - str) | |
1449 | copy = end - str; | |
1450 | memcpy(str, old_fmt, copy); | |
1da177e4 | 1451 | } |
fef20d9c FW |
1452 | str += read; |
1453 | break; | |
1da177e4 LT |
1454 | } |
1455 | ||
ed681a91 | 1456 | case FORMAT_TYPE_WIDTH: |
fef20d9c FW |
1457 | spec.field_width = va_arg(args, int); |
1458 | break; | |
1da177e4 | 1459 | |
fef20d9c FW |
1460 | case FORMAT_TYPE_PRECISION: |
1461 | spec.precision = va_arg(args, int); | |
1462 | break; | |
1da177e4 | 1463 | |
d4be151b AGR |
1464 | case FORMAT_TYPE_CHAR: { |
1465 | char c; | |
1466 | ||
fef20d9c FW |
1467 | if (!(spec.flags & LEFT)) { |
1468 | while (--spec.field_width > 0) { | |
f796937a | 1469 | if (str < end) |
1da177e4 LT |
1470 | *str = ' '; |
1471 | ++str; | |
1da177e4 | 1472 | |
fef20d9c FW |
1473 | } |
1474 | } | |
1475 | c = (unsigned char) va_arg(args, int); | |
1476 | if (str < end) | |
1477 | *str = c; | |
1478 | ++str; | |
1479 | while (--spec.field_width > 0) { | |
f796937a | 1480 | if (str < end) |
fef20d9c | 1481 | *str = ' '; |
1da177e4 | 1482 | ++str; |
fef20d9c FW |
1483 | } |
1484 | break; | |
d4be151b | 1485 | } |
1da177e4 | 1486 | |
fef20d9c FW |
1487 | case FORMAT_TYPE_STR: |
1488 | str = string(str, end, va_arg(args, char *), spec); | |
1489 | break; | |
1da177e4 | 1490 | |
fef20d9c FW |
1491 | case FORMAT_TYPE_PTR: |
1492 | str = pointer(fmt+1, str, end, va_arg(args, void *), | |
1493 | spec); | |
1494 | while (isalnum(*fmt)) | |
1495 | fmt++; | |
1496 | break; | |
1da177e4 | 1497 | |
fef20d9c FW |
1498 | case FORMAT_TYPE_PERCENT_CHAR: |
1499 | if (str < end) | |
1500 | *str = '%'; | |
1501 | ++str; | |
1502 | break; | |
1da177e4 | 1503 | |
fef20d9c FW |
1504 | case FORMAT_TYPE_INVALID: |
1505 | if (str < end) | |
1506 | *str = '%'; | |
1507 | ++str; | |
fef20d9c FW |
1508 | break; |
1509 | ||
1510 | case FORMAT_TYPE_NRCHARS: { | |
ef0658f3 | 1511 | u8 qualifier = spec.qualifier; |
fef20d9c FW |
1512 | |
1513 | if (qualifier == 'l') { | |
1514 | long *ip = va_arg(args, long *); | |
1515 | *ip = (str - buf); | |
75fb8f26 | 1516 | } else if (_tolower(qualifier) == 'z') { |
fef20d9c FW |
1517 | size_t *ip = va_arg(args, size_t *); |
1518 | *ip = (str - buf); | |
1519 | } else { | |
1520 | int *ip = va_arg(args, int *); | |
1521 | *ip = (str - buf); | |
1522 | } | |
1523 | break; | |
1da177e4 | 1524 | } |
fef20d9c FW |
1525 | |
1526 | default: | |
1527 | switch (spec.type) { | |
1528 | case FORMAT_TYPE_LONG_LONG: | |
1529 | num = va_arg(args, long long); | |
1530 | break; | |
1531 | case FORMAT_TYPE_ULONG: | |
1532 | num = va_arg(args, unsigned long); | |
1533 | break; | |
1534 | case FORMAT_TYPE_LONG: | |
1535 | num = va_arg(args, long); | |
1536 | break; | |
1537 | case FORMAT_TYPE_SIZE_T: | |
ef124960 JG |
1538 | if (spec.flags & SIGN) |
1539 | num = va_arg(args, ssize_t); | |
1540 | else | |
1541 | num = va_arg(args, size_t); | |
fef20d9c FW |
1542 | break; |
1543 | case FORMAT_TYPE_PTRDIFF: | |
1544 | num = va_arg(args, ptrdiff_t); | |
1545 | break; | |
a4e94ef0 Z |
1546 | case FORMAT_TYPE_UBYTE: |
1547 | num = (unsigned char) va_arg(args, int); | |
1548 | break; | |
1549 | case FORMAT_TYPE_BYTE: | |
1550 | num = (signed char) va_arg(args, int); | |
1551 | break; | |
fef20d9c FW |
1552 | case FORMAT_TYPE_USHORT: |
1553 | num = (unsigned short) va_arg(args, int); | |
1554 | break; | |
1555 | case FORMAT_TYPE_SHORT: | |
1556 | num = (short) va_arg(args, int); | |
1557 | break; | |
39e874f8 FW |
1558 | case FORMAT_TYPE_INT: |
1559 | num = (int) va_arg(args, int); | |
fef20d9c FW |
1560 | break; |
1561 | default: | |
1562 | num = va_arg(args, unsigned int); | |
1563 | } | |
1564 | ||
1565 | str = number(str, end, num, spec); | |
1da177e4 | 1566 | } |
1da177e4 | 1567 | } |
fef20d9c | 1568 | |
f796937a JF |
1569 | if (size > 0) { |
1570 | if (str < end) | |
1571 | *str = '\0'; | |
1572 | else | |
0a6047ee | 1573 | end[-1] = '\0'; |
f796937a | 1574 | } |
fef20d9c | 1575 | |
f796937a | 1576 | /* the trailing null byte doesn't count towards the total */ |
1da177e4 | 1577 | return str-buf; |
fef20d9c | 1578 | |
1da177e4 | 1579 | } |
1da177e4 LT |
1580 | EXPORT_SYMBOL(vsnprintf); |
1581 | ||
1582 | /** | |
1583 | * vscnprintf - Format a string and place it in a buffer | |
1584 | * @buf: The buffer to place the result into | |
1585 | * @size: The size of the buffer, including the trailing null space | |
1586 | * @fmt: The format string to use | |
1587 | * @args: Arguments for the format string | |
1588 | * | |
1589 | * The return value is the number of characters which have been written into | |
b921c69f | 1590 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
1da177e4 LT |
1591 | * returns 0. |
1592 | * | |
ba1835eb | 1593 | * If you're not already dealing with a va_list consider using scnprintf(). |
20036fdc AK |
1594 | * |
1595 | * See the vsnprintf() documentation for format string extensions over C99. | |
1da177e4 LT |
1596 | */ |
1597 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) | |
1598 | { | |
1599 | int i; | |
1600 | ||
7b9186f5 AGR |
1601 | i = vsnprintf(buf, size, fmt, args); |
1602 | ||
b921c69f AA |
1603 | if (likely(i < size)) |
1604 | return i; | |
1605 | if (size != 0) | |
1606 | return size - 1; | |
1607 | return 0; | |
1da177e4 | 1608 | } |
1da177e4 LT |
1609 | EXPORT_SYMBOL(vscnprintf); |
1610 | ||
1611 | /** | |
1612 | * snprintf - Format a string and place it in a buffer | |
1613 | * @buf: The buffer to place the result into | |
1614 | * @size: The size of the buffer, including the trailing null space | |
1615 | * @fmt: The format string to use | |
1616 | * @...: Arguments for the format string | |
1617 | * | |
1618 | * The return value is the number of characters which would be | |
1619 | * generated for the given input, excluding the trailing null, | |
1620 | * as per ISO C99. If the return is greater than or equal to | |
1621 | * @size, the resulting string is truncated. | |
20036fdc AK |
1622 | * |
1623 | * See the vsnprintf() documentation for format string extensions over C99. | |
1da177e4 | 1624 | */ |
7b9186f5 | 1625 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
1da177e4 LT |
1626 | { |
1627 | va_list args; | |
1628 | int i; | |
1629 | ||
1630 | va_start(args, fmt); | |
7b9186f5 | 1631 | i = vsnprintf(buf, size, fmt, args); |
1da177e4 | 1632 | va_end(args); |
7b9186f5 | 1633 | |
1da177e4 LT |
1634 | return i; |
1635 | } | |
1da177e4 LT |
1636 | EXPORT_SYMBOL(snprintf); |
1637 | ||
1638 | /** | |
1639 | * scnprintf - Format a string and place it in a buffer | |
1640 | * @buf: The buffer to place the result into | |
1641 | * @size: The size of the buffer, including the trailing null space | |
1642 | * @fmt: The format string to use | |
1643 | * @...: Arguments for the format string | |
1644 | * | |
1645 | * The return value is the number of characters written into @buf not including | |
b903c0b8 | 1646 | * the trailing '\0'. If @size is == 0 the function returns 0. |
1da177e4 LT |
1647 | */ |
1648 | ||
7b9186f5 | 1649 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
1da177e4 LT |
1650 | { |
1651 | va_list args; | |
1652 | int i; | |
1653 | ||
1654 | va_start(args, fmt); | |
b921c69f | 1655 | i = vscnprintf(buf, size, fmt, args); |
1da177e4 | 1656 | va_end(args); |
7b9186f5 | 1657 | |
b921c69f | 1658 | return i; |
1da177e4 LT |
1659 | } |
1660 | EXPORT_SYMBOL(scnprintf); | |
1661 | ||
1662 | /** | |
1663 | * vsprintf - Format a string and place it in a buffer | |
1664 | * @buf: The buffer to place the result into | |
1665 | * @fmt: The format string to use | |
1666 | * @args: Arguments for the format string | |
1667 | * | |
1668 | * The function returns the number of characters written | |
72fd4a35 | 1669 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
1da177e4 LT |
1670 | * buffer overflows. |
1671 | * | |
ba1835eb | 1672 | * If you're not already dealing with a va_list consider using sprintf(). |
20036fdc AK |
1673 | * |
1674 | * See the vsnprintf() documentation for format string extensions over C99. | |
1da177e4 LT |
1675 | */ |
1676 | int vsprintf(char *buf, const char *fmt, va_list args) | |
1677 | { | |
1678 | return vsnprintf(buf, INT_MAX, fmt, args); | |
1679 | } | |
1da177e4 LT |
1680 | EXPORT_SYMBOL(vsprintf); |
1681 | ||
1682 | /** | |
1683 | * sprintf - Format a string and place it in a buffer | |
1684 | * @buf: The buffer to place the result into | |
1685 | * @fmt: The format string to use | |
1686 | * @...: Arguments for the format string | |
1687 | * | |
1688 | * The function returns the number of characters written | |
72fd4a35 | 1689 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
1da177e4 | 1690 | * buffer overflows. |
20036fdc AK |
1691 | * |
1692 | * See the vsnprintf() documentation for format string extensions over C99. | |
1da177e4 | 1693 | */ |
7b9186f5 | 1694 | int sprintf(char *buf, const char *fmt, ...) |
1da177e4 LT |
1695 | { |
1696 | va_list args; | |
1697 | int i; | |
1698 | ||
1699 | va_start(args, fmt); | |
7b9186f5 | 1700 | i = vsnprintf(buf, INT_MAX, fmt, args); |
1da177e4 | 1701 | va_end(args); |
7b9186f5 | 1702 | |
1da177e4 LT |
1703 | return i; |
1704 | } | |
1da177e4 LT |
1705 | EXPORT_SYMBOL(sprintf); |
1706 | ||
4370aa4a LJ |
1707 | #ifdef CONFIG_BINARY_PRINTF |
1708 | /* | |
1709 | * bprintf service: | |
1710 | * vbin_printf() - VA arguments to binary data | |
1711 | * bstr_printf() - Binary data to text string | |
1712 | */ | |
1713 | ||
1714 | /** | |
1715 | * vbin_printf - Parse a format string and place args' binary value in a buffer | |
1716 | * @bin_buf: The buffer to place args' binary value | |
1717 | * @size: The size of the buffer(by words(32bits), not characters) | |
1718 | * @fmt: The format string to use | |
1719 | * @args: Arguments for the format string | |
1720 | * | |
1721 | * The format follows C99 vsnprintf, except %n is ignored, and its argument | |
1722 | * is skiped. | |
1723 | * | |
1724 | * The return value is the number of words(32bits) which would be generated for | |
1725 | * the given input. | |
1726 | * | |
1727 | * NOTE: | |
1728 | * If the return value is greater than @size, the resulting bin_buf is NOT | |
1729 | * valid for bstr_printf(). | |
1730 | */ | |
1731 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) | |
1732 | { | |
fef20d9c | 1733 | struct printf_spec spec = {0}; |
4370aa4a | 1734 | char *str, *end; |
4370aa4a LJ |
1735 | |
1736 | str = (char *)bin_buf; | |
1737 | end = (char *)(bin_buf + size); | |
1738 | ||
1739 | #define save_arg(type) \ | |
1740 | do { \ | |
1741 | if (sizeof(type) == 8) { \ | |
1742 | unsigned long long value; \ | |
1743 | str = PTR_ALIGN(str, sizeof(u32)); \ | |
1744 | value = va_arg(args, unsigned long long); \ | |
1745 | if (str + sizeof(type) <= end) { \ | |
1746 | *(u32 *)str = *(u32 *)&value; \ | |
1747 | *(u32 *)(str + 4) = *((u32 *)&value + 1); \ | |
1748 | } \ | |
1749 | } else { \ | |
1750 | unsigned long value; \ | |
1751 | str = PTR_ALIGN(str, sizeof(type)); \ | |
1752 | value = va_arg(args, int); \ | |
1753 | if (str + sizeof(type) <= end) \ | |
1754 | *(typeof(type) *)str = (type)value; \ | |
1755 | } \ | |
1756 | str += sizeof(type); \ | |
1757 | } while (0) | |
1758 | ||
fef20d9c | 1759 | while (*fmt) { |
d4be151b | 1760 | int read = format_decode(fmt, &spec); |
4370aa4a | 1761 | |
fef20d9c | 1762 | fmt += read; |
4370aa4a | 1763 | |
fef20d9c FW |
1764 | switch (spec.type) { |
1765 | case FORMAT_TYPE_NONE: | |
d4be151b AGR |
1766 | case FORMAT_TYPE_INVALID: |
1767 | case FORMAT_TYPE_PERCENT_CHAR: | |
fef20d9c FW |
1768 | break; |
1769 | ||
ed681a91 | 1770 | case FORMAT_TYPE_WIDTH: |
fef20d9c FW |
1771 | case FORMAT_TYPE_PRECISION: |
1772 | save_arg(int); | |
1773 | break; | |
1774 | ||
1775 | case FORMAT_TYPE_CHAR: | |
4370aa4a | 1776 | save_arg(char); |
fef20d9c FW |
1777 | break; |
1778 | ||
1779 | case FORMAT_TYPE_STR: { | |
4370aa4a LJ |
1780 | const char *save_str = va_arg(args, char *); |
1781 | size_t len; | |
6c356634 | 1782 | |
4370aa4a LJ |
1783 | if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE |
1784 | || (unsigned long)save_str < PAGE_SIZE) | |
0f4f81dc | 1785 | save_str = "(null)"; |
6c356634 AGR |
1786 | len = strlen(save_str) + 1; |
1787 | if (str + len < end) | |
1788 | memcpy(str, save_str, len); | |
1789 | str += len; | |
fef20d9c | 1790 | break; |
4370aa4a | 1791 | } |
fef20d9c FW |
1792 | |
1793 | case FORMAT_TYPE_PTR: | |
4370aa4a LJ |
1794 | save_arg(void *); |
1795 | /* skip all alphanumeric pointer suffixes */ | |
fef20d9c | 1796 | while (isalnum(*fmt)) |
4370aa4a | 1797 | fmt++; |
fef20d9c FW |
1798 | break; |
1799 | ||
fef20d9c | 1800 | case FORMAT_TYPE_NRCHARS: { |
4370aa4a | 1801 | /* skip %n 's argument */ |
ef0658f3 | 1802 | u8 qualifier = spec.qualifier; |
4370aa4a LJ |
1803 | void *skip_arg; |
1804 | if (qualifier == 'l') | |
1805 | skip_arg = va_arg(args, long *); | |
75fb8f26 | 1806 | else if (_tolower(qualifier) == 'z') |
4370aa4a LJ |
1807 | skip_arg = va_arg(args, size_t *); |
1808 | else | |
1809 | skip_arg = va_arg(args, int *); | |
fef20d9c | 1810 | break; |
4370aa4a | 1811 | } |
fef20d9c FW |
1812 | |
1813 | default: | |
1814 | switch (spec.type) { | |
1815 | ||
1816 | case FORMAT_TYPE_LONG_LONG: | |
4370aa4a | 1817 | save_arg(long long); |
fef20d9c FW |
1818 | break; |
1819 | case FORMAT_TYPE_ULONG: | |
1820 | case FORMAT_TYPE_LONG: | |
4370aa4a | 1821 | save_arg(unsigned long); |
fef20d9c FW |
1822 | break; |
1823 | case FORMAT_TYPE_SIZE_T: | |
4370aa4a | 1824 | save_arg(size_t); |
fef20d9c FW |
1825 | break; |
1826 | case FORMAT_TYPE_PTRDIFF: | |
4370aa4a | 1827 | save_arg(ptrdiff_t); |
fef20d9c | 1828 | break; |
a4e94ef0 Z |
1829 | case FORMAT_TYPE_UBYTE: |
1830 | case FORMAT_TYPE_BYTE: | |
1831 | save_arg(char); | |
1832 | break; | |
fef20d9c FW |
1833 | case FORMAT_TYPE_USHORT: |
1834 | case FORMAT_TYPE_SHORT: | |
4370aa4a | 1835 | save_arg(short); |
fef20d9c FW |
1836 | break; |
1837 | default: | |
4370aa4a | 1838 | save_arg(int); |
fef20d9c | 1839 | } |
4370aa4a LJ |
1840 | } |
1841 | } | |
fef20d9c | 1842 | |
7b9186f5 | 1843 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; |
fef20d9c | 1844 | #undef save_arg |
4370aa4a LJ |
1845 | } |
1846 | EXPORT_SYMBOL_GPL(vbin_printf); | |
1847 | ||
1848 | /** | |
1849 | * bstr_printf - Format a string from binary arguments and place it in a buffer | |
1850 | * @buf: The buffer to place the result into | |
1851 | * @size: The size of the buffer, including the trailing null space | |
1852 | * @fmt: The format string to use | |
1853 | * @bin_buf: Binary arguments for the format string | |
1854 | * | |
1855 | * This function like C99 vsnprintf, but the difference is that vsnprintf gets | |
1856 | * arguments from stack, and bstr_printf gets arguments from @bin_buf which is | |
1857 | * a binary buffer that generated by vbin_printf. | |
1858 | * | |
1859 | * The format follows C99 vsnprintf, but has some extensions: | |
0efb4d20 | 1860 | * see vsnprintf comment for details. |
4370aa4a LJ |
1861 | * |
1862 | * The return value is the number of characters which would | |
1863 | * be generated for the given input, excluding the trailing | |
1864 | * '\0', as per ISO C99. If you want to have the exact | |
1865 | * number of characters written into @buf as return value | |
1866 | * (not including the trailing '\0'), use vscnprintf(). If the | |
1867 | * return is greater than or equal to @size, the resulting | |
1868 | * string is truncated. | |
1869 | */ | |
1870 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |
1871 | { | |
fef20d9c | 1872 | struct printf_spec spec = {0}; |
d4be151b AGR |
1873 | char *str, *end; |
1874 | const char *args = (const char *)bin_buf; | |
4370aa4a | 1875 | |
2f30b1f9 | 1876 | if (WARN_ON_ONCE((int) size < 0)) |
4370aa4a | 1877 | return 0; |
4370aa4a LJ |
1878 | |
1879 | str = buf; | |
1880 | end = buf + size; | |
1881 | ||
1882 | #define get_arg(type) \ | |
1883 | ({ \ | |
1884 | typeof(type) value; \ | |
1885 | if (sizeof(type) == 8) { \ | |
1886 | args = PTR_ALIGN(args, sizeof(u32)); \ | |
1887 | *(u32 *)&value = *(u32 *)args; \ | |
1888 | *((u32 *)&value + 1) = *(u32 *)(args + 4); \ | |
1889 | } else { \ | |
1890 | args = PTR_ALIGN(args, sizeof(type)); \ | |
1891 | value = *(typeof(type) *)args; \ | |
1892 | } \ | |
1893 | args += sizeof(type); \ | |
1894 | value; \ | |
1895 | }) | |
1896 | ||
1897 | /* Make sure end is always >= buf */ | |
1898 | if (end < buf) { | |
1899 | end = ((void *)-1); | |
1900 | size = end - buf; | |
1901 | } | |
1902 | ||
fef20d9c | 1903 | while (*fmt) { |
fef20d9c | 1904 | const char *old_fmt = fmt; |
d4be151b | 1905 | int read = format_decode(fmt, &spec); |
4370aa4a | 1906 | |
fef20d9c | 1907 | fmt += read; |
4370aa4a | 1908 | |
fef20d9c FW |
1909 | switch (spec.type) { |
1910 | case FORMAT_TYPE_NONE: { | |
1911 | int copy = read; | |
1912 | if (str < end) { | |
1913 | if (copy > end - str) | |
1914 | copy = end - str; | |
1915 | memcpy(str, old_fmt, copy); | |
4370aa4a | 1916 | } |
fef20d9c FW |
1917 | str += read; |
1918 | break; | |
4370aa4a LJ |
1919 | } |
1920 | ||
ed681a91 | 1921 | case FORMAT_TYPE_WIDTH: |
fef20d9c FW |
1922 | spec.field_width = get_arg(int); |
1923 | break; | |
4370aa4a | 1924 | |
fef20d9c FW |
1925 | case FORMAT_TYPE_PRECISION: |
1926 | spec.precision = get_arg(int); | |
1927 | break; | |
4370aa4a | 1928 | |
d4be151b AGR |
1929 | case FORMAT_TYPE_CHAR: { |
1930 | char c; | |
1931 | ||
fef20d9c FW |
1932 | if (!(spec.flags & LEFT)) { |
1933 | while (--spec.field_width > 0) { | |
4370aa4a LJ |
1934 | if (str < end) |
1935 | *str = ' '; | |
1936 | ++str; | |
1937 | } | |
1938 | } | |
1939 | c = (unsigned char) get_arg(char); | |
1940 | if (str < end) | |
1941 | *str = c; | |
1942 | ++str; | |
fef20d9c | 1943 | while (--spec.field_width > 0) { |
4370aa4a LJ |
1944 | if (str < end) |
1945 | *str = ' '; | |
1946 | ++str; | |
1947 | } | |
fef20d9c | 1948 | break; |
d4be151b | 1949 | } |
4370aa4a | 1950 | |
fef20d9c | 1951 | case FORMAT_TYPE_STR: { |
4370aa4a | 1952 | const char *str_arg = args; |
d4be151b | 1953 | args += strlen(str_arg) + 1; |
fef20d9c FW |
1954 | str = string(str, end, (char *)str_arg, spec); |
1955 | break; | |
4370aa4a LJ |
1956 | } |
1957 | ||
fef20d9c FW |
1958 | case FORMAT_TYPE_PTR: |
1959 | str = pointer(fmt+1, str, end, get_arg(void *), spec); | |
1960 | while (isalnum(*fmt)) | |
4370aa4a | 1961 | fmt++; |
fef20d9c | 1962 | break; |
4370aa4a | 1963 | |
fef20d9c | 1964 | case FORMAT_TYPE_PERCENT_CHAR: |
fef20d9c | 1965 | case FORMAT_TYPE_INVALID: |
4370aa4a LJ |
1966 | if (str < end) |
1967 | *str = '%'; | |
1968 | ++str; | |
fef20d9c FW |
1969 | break; |
1970 | ||
1971 | case FORMAT_TYPE_NRCHARS: | |
1972 | /* skip */ | |
1973 | break; | |
1974 | ||
d4be151b AGR |
1975 | default: { |
1976 | unsigned long long num; | |
1977 | ||
fef20d9c FW |
1978 | switch (spec.type) { |
1979 | ||
1980 | case FORMAT_TYPE_LONG_LONG: | |
1981 | num = get_arg(long long); | |
1982 | break; | |
1983 | case FORMAT_TYPE_ULONG: | |
fef20d9c FW |
1984 | case FORMAT_TYPE_LONG: |
1985 | num = get_arg(unsigned long); | |
1986 | break; | |
1987 | case FORMAT_TYPE_SIZE_T: | |
1988 | num = get_arg(size_t); | |
1989 | break; | |
1990 | case FORMAT_TYPE_PTRDIFF: | |
1991 | num = get_arg(ptrdiff_t); | |
1992 | break; | |
a4e94ef0 Z |
1993 | case FORMAT_TYPE_UBYTE: |
1994 | num = get_arg(unsigned char); | |
1995 | break; | |
1996 | case FORMAT_TYPE_BYTE: | |
1997 | num = get_arg(signed char); | |
1998 | break; | |
fef20d9c FW |
1999 | case FORMAT_TYPE_USHORT: |
2000 | num = get_arg(unsigned short); | |
2001 | break; | |
2002 | case FORMAT_TYPE_SHORT: | |
2003 | num = get_arg(short); | |
2004 | break; | |
2005 | case FORMAT_TYPE_UINT: | |
2006 | num = get_arg(unsigned int); | |
2007 | break; | |
2008 | default: | |
2009 | num = get_arg(int); | |
2010 | } | |
2011 | ||
2012 | str = number(str, end, num, spec); | |
d4be151b AGR |
2013 | } /* default: */ |
2014 | } /* switch(spec.type) */ | |
2015 | } /* while(*fmt) */ | |
fef20d9c | 2016 | |
4370aa4a LJ |
2017 | if (size > 0) { |
2018 | if (str < end) | |
2019 | *str = '\0'; | |
2020 | else | |
2021 | end[-1] = '\0'; | |
2022 | } | |
fef20d9c | 2023 | |
4370aa4a LJ |
2024 | #undef get_arg |
2025 | ||
2026 | /* the trailing null byte doesn't count towards the total */ | |
2027 | return str - buf; | |
2028 | } | |
2029 | EXPORT_SYMBOL_GPL(bstr_printf); | |
2030 | ||
2031 | /** | |
2032 | * bprintf - Parse a format string and place args' binary value in a buffer | |
2033 | * @bin_buf: The buffer to place args' binary value | |
2034 | * @size: The size of the buffer(by words(32bits), not characters) | |
2035 | * @fmt: The format string to use | |
2036 | * @...: Arguments for the format string | |
2037 | * | |
2038 | * The function returns the number of words(u32) written | |
2039 | * into @bin_buf. | |
2040 | */ | |
2041 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) | |
2042 | { | |
2043 | va_list args; | |
2044 | int ret; | |
2045 | ||
2046 | va_start(args, fmt); | |
2047 | ret = vbin_printf(bin_buf, size, fmt, args); | |
2048 | va_end(args); | |
7b9186f5 | 2049 | |
4370aa4a LJ |
2050 | return ret; |
2051 | } | |
2052 | EXPORT_SYMBOL_GPL(bprintf); | |
2053 | ||
2054 | #endif /* CONFIG_BINARY_PRINTF */ | |
2055 | ||
1da177e4 LT |
2056 | /** |
2057 | * vsscanf - Unformat a buffer into a list of arguments | |
2058 | * @buf: input buffer | |
2059 | * @fmt: format of buffer | |
2060 | * @args: arguments | |
2061 | */ | |
7b9186f5 | 2062 | int vsscanf(const char *buf, const char *fmt, va_list args) |
1da177e4 LT |
2063 | { |
2064 | const char *str = buf; | |
2065 | char *next; | |
2066 | char digit; | |
2067 | int num = 0; | |
ef0658f3 | 2068 | u8 qualifier; |
53809751 JB |
2069 | unsigned int base; |
2070 | union { | |
2071 | long long s; | |
2072 | unsigned long long u; | |
2073 | } val; | |
ef0658f3 | 2074 | s16 field_width; |
d4be151b | 2075 | bool is_sign; |
1da177e4 | 2076 | |
da99075c | 2077 | while (*fmt) { |
1da177e4 LT |
2078 | /* skip any white space in format */ |
2079 | /* white space in format matchs any amount of | |
2080 | * white space, including none, in the input. | |
2081 | */ | |
2082 | if (isspace(*fmt)) { | |
e7d2860b AGR |
2083 | fmt = skip_spaces(++fmt); |
2084 | str = skip_spaces(str); | |
1da177e4 LT |
2085 | } |
2086 | ||
2087 | /* anything that is not a conversion must match exactly */ | |
2088 | if (*fmt != '%' && *fmt) { | |
2089 | if (*fmt++ != *str++) | |
2090 | break; | |
2091 | continue; | |
2092 | } | |
2093 | ||
2094 | if (!*fmt) | |
2095 | break; | |
2096 | ++fmt; | |
7b9186f5 | 2097 | |
1da177e4 LT |
2098 | /* skip this conversion. |
2099 | * advance both strings to next white space | |
2100 | */ | |
2101 | if (*fmt == '*') { | |
da99075c JB |
2102 | if (!*str) |
2103 | break; | |
8fccae2c | 2104 | while (!isspace(*fmt) && *fmt != '%' && *fmt) |
1da177e4 LT |
2105 | fmt++; |
2106 | while (!isspace(*str) && *str) | |
2107 | str++; | |
2108 | continue; | |
2109 | } | |
2110 | ||
2111 | /* get field width */ | |
2112 | field_width = -1; | |
53809751 | 2113 | if (isdigit(*fmt)) { |
1da177e4 | 2114 | field_width = skip_atoi(&fmt); |
53809751 JB |
2115 | if (field_width <= 0) |
2116 | break; | |
2117 | } | |
1da177e4 LT |
2118 | |
2119 | /* get conversion qualifier */ | |
2120 | qualifier = -1; | |
75fb8f26 AS |
2121 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
2122 | _tolower(*fmt) == 'z') { | |
1da177e4 LT |
2123 | qualifier = *fmt++; |
2124 | if (unlikely(qualifier == *fmt)) { | |
2125 | if (qualifier == 'h') { | |
2126 | qualifier = 'H'; | |
2127 | fmt++; | |
2128 | } else if (qualifier == 'l') { | |
2129 | qualifier = 'L'; | |
2130 | fmt++; | |
2131 | } | |
2132 | } | |
2133 | } | |
1da177e4 | 2134 | |
da99075c JB |
2135 | if (!*fmt) |
2136 | break; | |
2137 | ||
2138 | if (*fmt == 'n') { | |
2139 | /* return number of characters read so far */ | |
2140 | *va_arg(args, int *) = str - buf; | |
2141 | ++fmt; | |
2142 | continue; | |
2143 | } | |
2144 | ||
2145 | if (!*str) | |
1da177e4 LT |
2146 | break; |
2147 | ||
d4be151b AGR |
2148 | base = 10; |
2149 | is_sign = 0; | |
2150 | ||
7b9186f5 | 2151 | switch (*fmt++) { |
1da177e4 LT |
2152 | case 'c': |
2153 | { | |
7b9186f5 | 2154 | char *s = (char *)va_arg(args, char*); |
1da177e4 LT |
2155 | if (field_width == -1) |
2156 | field_width = 1; | |
2157 | do { | |
2158 | *s++ = *str++; | |
2159 | } while (--field_width > 0 && *str); | |
2160 | num++; | |
2161 | } | |
2162 | continue; | |
2163 | case 's': | |
2164 | { | |
7b9186f5 AGR |
2165 | char *s = (char *)va_arg(args, char *); |
2166 | if (field_width == -1) | |
4be929be | 2167 | field_width = SHRT_MAX; |
1da177e4 | 2168 | /* first, skip leading white space in buffer */ |
e7d2860b | 2169 | str = skip_spaces(str); |
1da177e4 LT |
2170 | |
2171 | /* now copy until next white space */ | |
7b9186f5 | 2172 | while (*str && !isspace(*str) && field_width--) |
1da177e4 | 2173 | *s++ = *str++; |
1da177e4 LT |
2174 | *s = '\0'; |
2175 | num++; | |
2176 | } | |
2177 | continue; | |
1da177e4 LT |
2178 | case 'o': |
2179 | base = 8; | |
2180 | break; | |
2181 | case 'x': | |
2182 | case 'X': | |
2183 | base = 16; | |
2184 | break; | |
2185 | case 'i': | |
7b9186f5 | 2186 | base = 0; |
1da177e4 LT |
2187 | case 'd': |
2188 | is_sign = 1; | |
2189 | case 'u': | |
2190 | break; | |
2191 | case '%': | |
2192 | /* looking for '%' in str */ | |
7b9186f5 | 2193 | if (*str++ != '%') |
1da177e4 LT |
2194 | return num; |
2195 | continue; | |
2196 | default: | |
2197 | /* invalid format; stop here */ | |
2198 | return num; | |
2199 | } | |
2200 | ||
2201 | /* have some sort of integer conversion. | |
2202 | * first, skip white space in buffer. | |
2203 | */ | |
e7d2860b | 2204 | str = skip_spaces(str); |
1da177e4 LT |
2205 | |
2206 | digit = *str; | |
2207 | if (is_sign && digit == '-') | |
2208 | digit = *(str + 1); | |
2209 | ||
2210 | if (!digit | |
7b9186f5 AGR |
2211 | || (base == 16 && !isxdigit(digit)) |
2212 | || (base == 10 && !isdigit(digit)) | |
2213 | || (base == 8 && (!isdigit(digit) || digit > '7')) | |
2214 | || (base == 0 && !isdigit(digit))) | |
2215 | break; | |
1da177e4 | 2216 | |
53809751 JB |
2217 | if (is_sign) |
2218 | val.s = qualifier != 'L' ? | |
2219 | simple_strtol(str, &next, base) : | |
2220 | simple_strtoll(str, &next, base); | |
2221 | else | |
2222 | val.u = qualifier != 'L' ? | |
2223 | simple_strtoul(str, &next, base) : | |
2224 | simple_strtoull(str, &next, base); | |
2225 | ||
2226 | if (field_width > 0 && next - str > field_width) { | |
2227 | if (base == 0) | |
2228 | _parse_integer_fixup_radix(str, &base); | |
2229 | while (next - str > field_width) { | |
2230 | if (is_sign) | |
2231 | val.s = div_s64(val.s, base); | |
2232 | else | |
2233 | val.u = div_u64(val.u, base); | |
2234 | --next; | |
2235 | } | |
2236 | } | |
2237 | ||
7b9186f5 | 2238 | switch (qualifier) { |
1da177e4 | 2239 | case 'H': /* that's 'hh' in format */ |
53809751 JB |
2240 | if (is_sign) |
2241 | *va_arg(args, signed char *) = val.s; | |
2242 | else | |
2243 | *va_arg(args, unsigned char *) = val.u; | |
1da177e4 LT |
2244 | break; |
2245 | case 'h': | |
53809751 JB |
2246 | if (is_sign) |
2247 | *va_arg(args, short *) = val.s; | |
2248 | else | |
2249 | *va_arg(args, unsigned short *) = val.u; | |
1da177e4 LT |
2250 | break; |
2251 | case 'l': | |
53809751 JB |
2252 | if (is_sign) |
2253 | *va_arg(args, long *) = val.s; | |
2254 | else | |
2255 | *va_arg(args, unsigned long *) = val.u; | |
1da177e4 LT |
2256 | break; |
2257 | case 'L': | |
53809751 JB |
2258 | if (is_sign) |
2259 | *va_arg(args, long long *) = val.s; | |
2260 | else | |
2261 | *va_arg(args, unsigned long long *) = val.u; | |
1da177e4 LT |
2262 | break; |
2263 | case 'Z': | |
2264 | case 'z': | |
53809751 JB |
2265 | *va_arg(args, size_t *) = val.u; |
2266 | break; | |
1da177e4 | 2267 | default: |
53809751 JB |
2268 | if (is_sign) |
2269 | *va_arg(args, int *) = val.s; | |
2270 | else | |
2271 | *va_arg(args, unsigned int *) = val.u; | |
1da177e4 LT |
2272 | break; |
2273 | } | |
2274 | num++; | |
2275 | ||
2276 | if (!next) | |
2277 | break; | |
2278 | str = next; | |
2279 | } | |
c6b40d16 | 2280 | |
1da177e4 LT |
2281 | return num; |
2282 | } | |
1da177e4 LT |
2283 | EXPORT_SYMBOL(vsscanf); |
2284 | ||
2285 | /** | |
2286 | * sscanf - Unformat a buffer into a list of arguments | |
2287 | * @buf: input buffer | |
2288 | * @fmt: formatting of buffer | |
2289 | * @...: resulting arguments | |
2290 | */ | |
7b9186f5 | 2291 | int sscanf(const char *buf, const char *fmt, ...) |
1da177e4 LT |
2292 | { |
2293 | va_list args; | |
2294 | int i; | |
2295 | ||
7b9186f5 AGR |
2296 | va_start(args, fmt); |
2297 | i = vsscanf(buf, fmt, args); | |
1da177e4 | 2298 | va_end(args); |
7b9186f5 | 2299 | |
1da177e4 LT |
2300 | return i; |
2301 | } | |
1da177e4 | 2302 | EXPORT_SYMBOL(sscanf); |