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