Rev 5056 | Rev 6082 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1869 | serge | 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 | |||
12 | /* |
||
13 | * Fri Jul 13 2001 Crutcher Dunnavant |
||
14 | * - changed to provide snprintf and vsnprintf functions |
||
15 | * So Feb 1 16:51:32 CET 2004 Juergen Quade |
||
16 | * - scnprintf and vscnprintf |
||
17 | */ |
||
18 | |||
19 | #include |
||
5056 | serge | 20 | #include |
1869 | serge | 21 | #include |
22 | #include |
||
23 | #include |
||
24 | #include |
||
5270 | serge | 25 | |
1869 | serge | 26 | #include |
3031 | serge | 27 | #include |
1869 | serge | 28 | |
29 | #include |
||
5270 | serge | 30 | #include |
1869 | serge | 31 | |
5056 | serge | 32 | |
33 | static inline u64 div_u64(u64 dividend, u32 divisor) |
||
34 | { |
||
35 | u32 remainder; |
||
36 | return div_u64_rem(dividend, divisor, &remainder); |
||
37 | } |
||
38 | |||
39 | static inline s64 div_s64(s64 dividend, s32 divisor) |
||
40 | { |
||
41 | s32 remainder; |
||
42 | return div_s64_rem(dividend, divisor, &remainder); |
||
43 | } |
||
44 | |||
1869 | serge | 45 | |
5056 | serge | 46 | #define ZERO_SIZE_PTR ((void *)16) |
47 | |||
48 | #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \ |
||
49 | (unsigned long)ZERO_SIZE_PTR) |
||
50 | |||
1869 | serge | 51 | #ifndef dereference_function_descriptor |
52 | #define dereference_function_descriptor(p) (p) |
||
53 | #endif |
||
54 | |||
5056 | serge | 55 | #define KSTRTOX_OVERFLOW (1U << 31) |
56 | |||
1869 | serge | 57 | const char hex_asc[] = "0123456789abcdef"; |
58 | |||
59 | /* Works only for digits and letters, but small and fast */ |
||
60 | #define TOLOWER(x) ((x) | 0x20) |
||
61 | |||
62 | |||
5056 | serge | 63 | char *skip_spaces(const char *str) |
64 | { |
||
65 | while (isspace(*str)) |
||
66 | ++str; |
||
67 | return (char *)str; |
||
68 | } |
||
69 | EXPORT_SYMBOL(skip_spaces); |
||
70 | |||
71 | const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) |
||
72 | { |
||
73 | if (*base == 0) { |
||
74 | if (s[0] == '0') { |
||
75 | if (_tolower(s[1]) == 'x' && isxdigit(s[2])) |
||
76 | *base = 16; |
||
77 | else |
||
78 | *base = 8; |
||
79 | } else |
||
80 | *base = 10; |
||
81 | } |
||
82 | if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x') |
||
83 | s += 2; |
||
84 | return s; |
||
85 | } |
||
86 | |||
87 | /* |
||
88 | * Convert non-negative integer string representation in explicitly given radix |
||
89 | * to an integer. |
||
90 | * Return number of characters consumed maybe or-ed with overflow bit. |
||
91 | * If overflow occurs, result integer (incorrect) is still returned. |
||
92 | * |
||
93 | * Don't you dare use this function. |
||
94 | */ |
||
95 | unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p) |
||
96 | { |
||
97 | unsigned long long res; |
||
98 | unsigned int rv; |
||
99 | int overflow; |
||
100 | |||
101 | res = 0; |
||
102 | rv = 0; |
||
103 | overflow = 0; |
||
104 | while (*s) { |
||
105 | unsigned int val; |
||
106 | |||
107 | if ('0' <= *s && *s <= '9') |
||
108 | val = *s - '0'; |
||
109 | else if ('a' <= _tolower(*s) && _tolower(*s) <= 'f') |
||
110 | val = _tolower(*s) - 'a' + 10; |
||
111 | else |
||
112 | break; |
||
113 | |||
114 | if (val >= base) |
||
115 | break; |
||
116 | /* |
||
117 | * Check for overflow only if we are within range of |
||
118 | * it in the max base we support (16) |
||
119 | */ |
||
120 | if (unlikely(res & (~0ull << 60))) { |
||
121 | if (res > div_u64(ULLONG_MAX - val, base)) |
||
122 | overflow = 1; |
||
123 | } |
||
124 | res = res * base + val; |
||
125 | rv++; |
||
126 | s++; |
||
127 | } |
||
128 | *p = res; |
||
129 | if (overflow) |
||
130 | rv |= KSTRTOX_OVERFLOW; |
||
131 | return rv; |
||
132 | } |
||
133 | |||
134 | |||
1869 | serge | 135 | /** |
136 | * simple_strtoull - convert a string to an unsigned long long |
||
137 | * @cp: The start of the string |
||
138 | * @endp: A pointer to the end of the parsed string will be placed here |
||
139 | * @base: The number base to use |
||
5056 | serge | 140 | * |
141 | * This function is obsolete. Please use kstrtoull instead. |
||
1869 | serge | 142 | */ |
143 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
||
144 | { |
||
5056 | serge | 145 | unsigned long long result; |
146 | unsigned int rv; |
||
1869 | serge | 147 | |
5056 | serge | 148 | cp = _parse_integer_fixup_radix(cp, &base); |
149 | rv = _parse_integer(cp, base, &result); |
||
150 | /* FIXME */ |
||
151 | cp += (rv & ~KSTRTOX_OVERFLOW); |
||
1869 | serge | 152 | |
153 | if (endp) |
||
154 | *endp = (char *)cp; |
||
155 | |||
156 | return result; |
||
157 | } |
||
158 | EXPORT_SYMBOL(simple_strtoull); |
||
159 | |||
160 | /** |
||
161 | * simple_strtoul - convert a string to an unsigned long |
||
162 | * @cp: The start of the string |
||
163 | * @endp: A pointer to the end of the parsed string will be placed here |
||
164 | * @base: The number base to use |
||
5056 | serge | 165 | * |
166 | * This function is obsolete. Please use kstrtoul instead. |
||
1869 | serge | 167 | */ |
168 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
||
169 | { |
||
170 | return simple_strtoull(cp, endp, base); |
||
171 | } |
||
172 | EXPORT_SYMBOL(simple_strtoul); |
||
173 | |||
174 | /** |
||
175 | * simple_strtol - convert a string to a signed long |
||
176 | * @cp: The start of the string |
||
177 | * @endp: A pointer to the end of the parsed string will be placed here |
||
178 | * @base: The number base to use |
||
5056 | serge | 179 | * |
180 | * This function is obsolete. Please use kstrtol instead. |
||
1869 | serge | 181 | */ |
182 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
||
183 | { |
||
184 | if (*cp == '-') |
||
185 | return -simple_strtoul(cp + 1, endp, base); |
||
186 | |||
187 | return simple_strtoul(cp, endp, base); |
||
188 | } |
||
189 | EXPORT_SYMBOL(simple_strtol); |
||
190 | |||
191 | /** |
||
192 | * simple_strtoll - convert a string to a signed long long |
||
193 | * @cp: The start of the string |
||
194 | * @endp: A pointer to the end of the parsed string will be placed here |
||
195 | * @base: The number base to use |
||
5056 | serge | 196 | * |
197 | * This function is obsolete. Please use kstrtoll instead. |
||
1869 | serge | 198 | */ |
199 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
||
200 | { |
||
201 | if (*cp == '-') |
||
202 | return -simple_strtoull(cp + 1, endp, base); |
||
203 | |||
204 | return simple_strtoull(cp, endp, base); |
||
205 | } |
||
206 | EXPORT_SYMBOL(simple_strtoll); |
||
207 | |||
5056 | serge | 208 | static noinline_for_stack |
209 | int skip_atoi(const char **s) |
||
1869 | serge | 210 | { |
5056 | serge | 211 | int i = 0; |
1869 | serge | 212 | |
5056 | serge | 213 | while (isdigit(**s)) |
214 | i = i*10 + *((*s)++) - '0'; |
||
1869 | serge | 215 | |
5056 | serge | 216 | return i; |
217 | } |
||
1869 | serge | 218 | |
5056 | serge | 219 | /* Decimal conversion is by far the most typical, and is used |
220 | * for /proc and /sys data. This directly impacts e.g. top performance |
||
221 | * with many processes running. We optimize it for speed |
||
222 | * using ideas described at |
||
223 | * (with permission from the author, Douglas W. Jones). |
||
224 | */ |
||
1869 | serge | 225 | |
5056 | serge | 226 | #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 |
227 | /* Formats correctly any integer in [0, 999999999] */ |
||
228 | static noinline_for_stack |
||
229 | char *put_dec_full9(char *buf, unsigned q) |
||
230 | { |
||
231 | unsigned r; |
||
232 | |||
233 | /* |
||
234 | * Possible ways to approx. divide by 10 |
||
235 | * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit) |
||
236 | * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul) |
||
237 | * (x * 0x6667) >> 18 x < 43699 |
||
238 | * (x * 0x3334) >> 17 x < 16389 |
||
239 | * (x * 0x199a) >> 16 x < 16389 |
||
240 | * (x * 0x0ccd) >> 15 x < 16389 |
||
241 | * (x * 0x0667) >> 14 x < 2739 |
||
242 | * (x * 0x0334) >> 13 x < 1029 |
||
243 | * (x * 0x019a) >> 12 x < 1029 |
||
244 | * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386) |
||
245 | * (x * 0x0067) >> 10 x < 179 |
||
246 | * (x * 0x0034) >> 9 x < 69 same |
||
247 | * (x * 0x001a) >> 8 x < 69 same |
||
248 | * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386) |
||
249 | * (x * 0x0007) >> 6 x < 19 |
||
250 | * See |
||
251 | */ |
||
252 | r = (q * (uint64_t)0x1999999a) >> 32; |
||
253 | *buf++ = (q - 10 * r) + '0'; /* 1 */ |
||
254 | q = (r * (uint64_t)0x1999999a) >> 32; |
||
255 | *buf++ = (r - 10 * q) + '0'; /* 2 */ |
||
256 | r = (q * (uint64_t)0x1999999a) >> 32; |
||
257 | *buf++ = (q - 10 * r) + '0'; /* 3 */ |
||
258 | q = (r * (uint64_t)0x1999999a) >> 32; |
||
259 | *buf++ = (r - 10 * q) + '0'; /* 4 */ |
||
260 | r = (q * (uint64_t)0x1999999a) >> 32; |
||
261 | *buf++ = (q - 10 * r) + '0'; /* 5 */ |
||
262 | /* Now value is under 10000, can avoid 64-bit multiply */ |
||
263 | q = (r * 0x199a) >> 16; |
||
264 | *buf++ = (r - 10 * q) + '0'; /* 6 */ |
||
265 | r = (q * 0xcd) >> 11; |
||
266 | *buf++ = (q - 10 * r) + '0'; /* 7 */ |
||
267 | q = (r * 0xcd) >> 11; |
||
268 | *buf++ = (r - 10 * q) + '0'; /* 8 */ |
||
269 | *buf++ = q + '0'; /* 9 */ |
||
270 | return buf; |
||
1869 | serge | 271 | } |
5056 | serge | 272 | #endif |
1869 | serge | 273 | |
5056 | serge | 274 | /* Similar to above but do not pad with zeros. |
275 | * Code can be easily arranged to print 9 digits too, but our callers |
||
276 | * always call put_dec_full9() instead when the number has 9 decimal digits. |
||
1869 | serge | 277 | */ |
5056 | serge | 278 | static noinline_for_stack |
279 | char *put_dec_trunc8(char *buf, unsigned r) |
||
1869 | serge | 280 | { |
5056 | serge | 281 | unsigned q; |
282 | |||
283 | /* Copy of previous function's body with added early returns */ |
||
284 | while (r >= 10000) { |
||
285 | q = r + '0'; |
||
286 | r = (r * (uint64_t)0x1999999a) >> 32; |
||
287 | *buf++ = q - 10*r; |
||
1869 | serge | 288 | } |
289 | |||
5056 | serge | 290 | q = (r * 0x199a) >> 16; /* r <= 9999 */ |
291 | *buf++ = (r - 10 * q) + '0'; |
||
292 | if (q == 0) |
||
293 | return buf; |
||
294 | r = (q * 0xcd) >> 11; /* q <= 999 */ |
||
295 | *buf++ = (q - 10 * r) + '0'; |
||
296 | if (r == 0) |
||
297 | return buf; |
||
298 | q = (r * 0xcd) >> 11; /* r <= 99 */ |
||
299 | *buf++ = (r - 10 * q) + '0'; |
||
300 | if (q == 0) |
||
301 | return buf; |
||
302 | *buf++ = q + '0'; /* q <= 9 */ |
||
303 | return buf; |
||
1869 | serge | 304 | } |
305 | |||
5056 | serge | 306 | /* There are two algorithms to print larger numbers. |
307 | * One is generic: divide by 1000000000 and repeatedly print |
||
308 | * groups of (up to) 9 digits. It's conceptually simple, |
||
309 | * but requires a (unsigned long long) / 1000000000 division. |
||
1869 | serge | 310 | * |
5056 | serge | 311 | * Second algorithm splits 64-bit unsigned long long into 16-bit chunks, |
312 | * manipulates them cleverly and generates groups of 4 decimal digits. |
||
313 | * It so happens that it does NOT require long long division. |
||
1869 | serge | 314 | * |
5056 | serge | 315 | * If long is > 32 bits, division of 64-bit values is relatively easy, |
316 | * and we will use the first algorithm. |
||
317 | * If long long is > 64 bits (strange architecture with VERY large long long), |
||
318 | * second algorithm can't be used, and we again use the first one. |
||
1869 | serge | 319 | * |
5056 | serge | 320 | * Else (if long is 32 bits and long long is 64 bits) we use second one. |
1869 | serge | 321 | */ |
322 | |||
5056 | serge | 323 | #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 |
1869 | serge | 324 | |
5056 | serge | 325 | /* First algorithm: generic */ |
326 | |||
327 | static |
||
328 | char *put_dec(char *buf, unsigned long long n) |
||
329 | { |
||
330 | if (n >= 100*1000*1000) { |
||
331 | while (n >= 1000*1000*1000) |
||
332 | buf = put_dec_full9(buf, do_div(n, 1000*1000*1000)); |
||
333 | if (n >= 100*1000*1000) |
||
334 | return put_dec_full9(buf, n); |
||
1869 | serge | 335 | } |
5056 | serge | 336 | return put_dec_trunc8(buf, n); |
337 | } |
||
1869 | serge | 338 | |
5056 | serge | 339 | #else |
340 | |||
341 | /* Second algorithm: valid only for 64-bit long longs */ |
||
342 | |||
343 | /* See comment in put_dec_full9 for choice of constants */ |
||
344 | static noinline_for_stack |
||
345 | void put_dec_full4(char *buf, unsigned q) |
||
346 | { |
||
347 | unsigned r; |
||
348 | r = (q * 0xccd) >> 15; |
||
349 | buf[0] = (q - 10 * r) + '0'; |
||
350 | q = (r * 0xcd) >> 11; |
||
351 | buf[1] = (r - 10 * q) + '0'; |
||
352 | r = (q * 0xcd) >> 11; |
||
353 | buf[2] = (q - 10 * r) + '0'; |
||
354 | buf[3] = r + '0'; |
||
1869 | serge | 355 | } |
356 | |||
5056 | serge | 357 | /* |
358 | * Call put_dec_full4 on x % 10000, return x / 10000. |
||
359 | * The approximation x/10000 == (x * 0x346DC5D7) >> 43 |
||
360 | * holds for all x < 1,128,869,999. The largest value this |
||
361 | * helper will ever be asked to convert is 1,125,520,955. |
||
362 | * (d1 in the put_dec code, assuming n is all-ones). |
||
1869 | serge | 363 | */ |
5056 | serge | 364 | static |
365 | unsigned put_dec_helper4(char *buf, unsigned x) |
||
1869 | serge | 366 | { |
5056 | serge | 367 | uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; |
1869 | serge | 368 | |
5056 | serge | 369 | put_dec_full4(buf, x - q * 10000); |
370 | return q; |
||
1869 | serge | 371 | } |
372 | |||
5056 | serge | 373 | /* Based on code by Douglas W. Jones found at |
374 | * |
||
375 | * (with permission from the author). |
||
376 | * Performs no 64-bit division and hence should be fast on 32-bit machines. |
||
377 | */ |
||
378 | static |
||
379 | char *put_dec(char *buf, unsigned long long n) |
||
1869 | serge | 380 | { |
5056 | serge | 381 | uint32_t d3, d2, d1, q, h; |
1869 | serge | 382 | |
5056 | serge | 383 | if (n < 100*1000*1000) |
384 | return put_dec_trunc8(buf, n); |
||
1869 | serge | 385 | |
5056 | serge | 386 | d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ |
387 | h = (n >> 32); |
||
388 | d2 = (h ) & 0xffff; |
||
389 | d3 = (h >> 16); /* implicit "& 0xffff" */ |
||
1869 | serge | 390 | |
5056 | serge | 391 | q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); |
392 | q = put_dec_helper4(buf, q); |
||
1869 | serge | 393 | |
5056 | serge | 394 | q += 7671 * d3 + 9496 * d2 + 6 * d1; |
395 | q = put_dec_helper4(buf+4, q); |
||
1869 | serge | 396 | |
5056 | serge | 397 | q += 4749 * d3 + 42 * d2; |
398 | q = put_dec_helper4(buf+8, q); |
||
1869 | serge | 399 | |
5056 | serge | 400 | q += 281 * d3; |
401 | buf += 12; |
||
402 | if (q) |
||
403 | buf = put_dec_trunc8(buf, q); |
||
404 | else while (buf[-1] == '0') |
||
405 | --buf; |
||
1869 | serge | 406 | |
407 | return buf; |
||
408 | } |
||
409 | |||
5056 | serge | 410 | #endif |
1869 | serge | 411 | |
5056 | serge | 412 | /* |
413 | * Convert passed number to decimal string. |
||
414 | * Returns the length of string. On buffer overflow, returns 0. |
||
415 | * |
||
416 | * If speed is not important, use snprintf(). It's easy to read the code. |
||
417 | */ |
||
418 | int num_to_str(char *buf, int size, unsigned long long num) |
||
419 | { |
||
420 | char tmp[sizeof(num) * 3]; |
||
421 | int idx, len; |
||
1869 | serge | 422 | |
5056 | serge | 423 | /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ |
424 | if (num <= 9) { |
||
425 | tmp[0] = '0' + num; |
||
426 | len = 1; |
||
427 | } else { |
||
428 | len = put_dec(tmp, num) - tmp; |
||
429 | } |
||
1869 | serge | 430 | |
5056 | serge | 431 | if (len > size) |
432 | return 0; |
||
433 | for (idx = 0; idx < len; ++idx) |
||
434 | buf[idx] = tmp[len - idx - 1]; |
||
435 | return len; |
||
1869 | serge | 436 | } |
437 | |||
438 | #define ZEROPAD 1 /* pad with zero */ |
||
439 | #define SIGN 2 /* unsigned/signed long */ |
||
440 | #define PLUS 4 /* show plus */ |
||
441 | #define SPACE 8 /* space if plus */ |
||
442 | #define LEFT 16 /* left justified */ |
||
443 | #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ |
||
444 | #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ |
||
445 | |||
446 | enum format_type { |
||
447 | FORMAT_TYPE_NONE, /* Just a string part */ |
||
448 | FORMAT_TYPE_WIDTH, |
||
449 | FORMAT_TYPE_PRECISION, |
||
450 | FORMAT_TYPE_CHAR, |
||
451 | FORMAT_TYPE_STR, |
||
452 | FORMAT_TYPE_PTR, |
||
453 | FORMAT_TYPE_PERCENT_CHAR, |
||
454 | FORMAT_TYPE_INVALID, |
||
455 | FORMAT_TYPE_LONG_LONG, |
||
456 | FORMAT_TYPE_ULONG, |
||
457 | FORMAT_TYPE_LONG, |
||
458 | FORMAT_TYPE_UBYTE, |
||
459 | FORMAT_TYPE_BYTE, |
||
460 | FORMAT_TYPE_USHORT, |
||
461 | FORMAT_TYPE_SHORT, |
||
462 | FORMAT_TYPE_UINT, |
||
463 | FORMAT_TYPE_INT, |
||
464 | FORMAT_TYPE_SIZE_T, |
||
465 | FORMAT_TYPE_PTRDIFF |
||
466 | }; |
||
467 | |||
468 | struct printf_spec { |
||
469 | u8 type; /* format_type enum */ |
||
470 | u8 flags; /* flags to number() */ |
||
471 | u8 base; /* number base, 8, 10 or 16 only */ |
||
472 | u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ |
||
473 | s16 field_width; /* width of output field */ |
||
474 | s16 precision; /* # of digits/chars */ |
||
475 | }; |
||
476 | |||
477 | static noinline_for_stack |
||
478 | char *number(char *buf, char *end, unsigned long long num, |
||
479 | struct printf_spec spec) |
||
480 | { |
||
481 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ |
||
482 | static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ |
||
483 | |||
484 | char tmp[66]; |
||
485 | char sign; |
||
486 | char locase; |
||
487 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
||
488 | int i; |
||
5056 | serge | 489 | bool is_zero = num == 0LL; |
1869 | serge | 490 | |
491 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
||
492 | * produces same digits or (maybe lowercased) letters */ |
||
493 | locase = (spec.flags & SMALL); |
||
494 | if (spec.flags & LEFT) |
||
495 | spec.flags &= ~ZEROPAD; |
||
496 | sign = 0; |
||
497 | if (spec.flags & SIGN) { |
||
498 | if ((signed long long)num < 0) { |
||
499 | sign = '-'; |
||
500 | num = -(signed long long)num; |
||
501 | spec.field_width--; |
||
502 | } else if (spec.flags & PLUS) { |
||
503 | sign = '+'; |
||
504 | spec.field_width--; |
||
505 | } else if (spec.flags & SPACE) { |
||
506 | sign = ' '; |
||
507 | spec.field_width--; |
||
508 | } |
||
509 | } |
||
510 | if (need_pfx) { |
||
511 | if (spec.base == 16) |
||
5056 | serge | 512 | spec.field_width -= 2; |
513 | else if (!is_zero) |
||
1869 | serge | 514 | spec.field_width--; |
515 | } |
||
516 | |||
517 | /* generate full string in tmp[], in reverse order */ |
||
518 | i = 0; |
||
5056 | serge | 519 | if (num < spec.base) |
520 | tmp[i++] = digits[num] | locase; |
||
1869 | serge | 521 | /* Generic code, for any base: |
522 | else do { |
||
523 | tmp[i++] = (digits[do_div(num,base)] | locase); |
||
524 | } while (num != 0); |
||
525 | */ |
||
526 | else if (spec.base != 10) { /* 8 or 16 */ |
||
527 | int mask = spec.base - 1; |
||
528 | int shift = 3; |
||
529 | |||
530 | if (spec.base == 16) |
||
531 | shift = 4; |
||
532 | do { |
||
533 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); |
||
534 | num >>= shift; |
||
535 | } while (num); |
||
536 | } else { /* base 10 */ |
||
537 | i = put_dec(tmp, num) - tmp; |
||
538 | } |
||
539 | |||
540 | /* printing 100 using %2d gives "100", not "00" */ |
||
541 | if (i > spec.precision) |
||
542 | spec.precision = i; |
||
543 | /* leading space padding */ |
||
544 | spec.field_width -= spec.precision; |
||
545 | if (!(spec.flags & (ZEROPAD+LEFT))) { |
||
546 | while (--spec.field_width >= 0) { |
||
547 | if (buf < end) |
||
548 | *buf = ' '; |
||
549 | ++buf; |
||
550 | } |
||
551 | } |
||
552 | /* sign */ |
||
553 | if (sign) { |
||
554 | if (buf < end) |
||
555 | *buf = sign; |
||
556 | ++buf; |
||
557 | } |
||
558 | /* "0x" / "0" prefix */ |
||
559 | if (need_pfx) { |
||
5056 | serge | 560 | if (spec.base == 16 || !is_zero) { |
1869 | serge | 561 | if (buf < end) |
562 | *buf = '0'; |
||
563 | ++buf; |
||
5056 | serge | 564 | } |
1869 | serge | 565 | if (spec.base == 16) { |
566 | if (buf < end) |
||
567 | *buf = ('X' | locase); |
||
568 | ++buf; |
||
569 | } |
||
570 | } |
||
571 | /* zero or space padding */ |
||
572 | if (!(spec.flags & LEFT)) { |
||
573 | char c = (spec.flags & ZEROPAD) ? '0' : ' '; |
||
574 | while (--spec.field_width >= 0) { |
||
575 | if (buf < end) |
||
576 | *buf = c; |
||
577 | ++buf; |
||
578 | } |
||
579 | } |
||
580 | /* hmm even more zero padding? */ |
||
581 | while (i <= --spec.precision) { |
||
582 | if (buf < end) |
||
583 | *buf = '0'; |
||
584 | ++buf; |
||
585 | } |
||
586 | /* actual digits of result */ |
||
587 | while (--i >= 0) { |
||
588 | if (buf < end) |
||
589 | *buf = tmp[i]; |
||
590 | ++buf; |
||
591 | } |
||
592 | /* trailing space padding */ |
||
593 | while (--spec.field_width >= 0) { |
||
594 | if (buf < end) |
||
595 | *buf = ' '; |
||
596 | ++buf; |
||
597 | } |
||
598 | |||
599 | return buf; |
||
600 | } |
||
601 | |||
602 | static noinline_for_stack |
||
603 | char *string(char *buf, char *end, const char *s, struct printf_spec spec) |
||
604 | { |
||
605 | int len, i; |
||
606 | |||
5056 | serge | 607 | if ((unsigned long)s < PAGE_SIZE) |
1869 | serge | 608 | s = "(null)"; |
609 | |||
610 | len = strnlen(s, spec.precision); |
||
611 | |||
612 | if (!(spec.flags & LEFT)) { |
||
613 | while (len < spec.field_width--) { |
||
614 | if (buf < end) |
||
615 | *buf = ' '; |
||
616 | ++buf; |
||
617 | } |
||
618 | } |
||
619 | for (i = 0; i < len; ++i) { |
||
620 | if (buf < end) |
||
621 | *buf = *s; |
||
622 | ++buf; ++s; |
||
623 | } |
||
624 | while (len < spec.field_width--) { |
||
625 | if (buf < end) |
||
626 | *buf = ' '; |
||
627 | ++buf; |
||
628 | } |
||
629 | |||
630 | return buf; |
||
631 | } |
||
632 | |||
633 | static noinline_for_stack |
||
634 | char *symbol_string(char *buf, char *end, void *ptr, |
||
5056 | serge | 635 | struct printf_spec spec, const char *fmt) |
1869 | serge | 636 | { |
5056 | serge | 637 | unsigned long value; |
1869 | serge | 638 | #ifdef CONFIG_KALLSYMS |
639 | char sym[KSYM_SYMBOL_LEN]; |
||
5056 | serge | 640 | #endif |
641 | |||
642 | if (fmt[1] == 'R') |
||
643 | ptr = __builtin_extract_return_addr(ptr); |
||
644 | value = (unsigned long)ptr; |
||
645 | |||
646 | #ifdef CONFIG_KALLSYMS |
||
647 | if (*fmt == 'B') |
||
648 | sprint_backtrace(sym, value); |
||
649 | else if (*fmt != 'f' && *fmt != 's') |
||
1869 | serge | 650 | sprint_symbol(sym, value); |
651 | else |
||
5056 | serge | 652 | sprint_symbol_no_offset(sym, value); |
1869 | serge | 653 | |
654 | return string(buf, end, sym, spec); |
||
655 | #else |
||
656 | spec.field_width = 2 * sizeof(void *); |
||
657 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
||
658 | spec.base = 16; |
||
659 | |||
660 | return number(buf, end, value, spec); |
||
661 | #endif |
||
662 | } |
||
663 | |||
664 | static noinline_for_stack |
||
665 | char *resource_string(char *buf, char *end, struct resource *res, |
||
666 | struct printf_spec spec, const char *fmt) |
||
667 | { |
||
668 | #ifndef IO_RSRC_PRINTK_SIZE |
||
669 | #define IO_RSRC_PRINTK_SIZE 6 |
||
670 | #endif |
||
671 | |||
672 | #ifndef MEM_RSRC_PRINTK_SIZE |
||
673 | #define MEM_RSRC_PRINTK_SIZE 10 |
||
674 | #endif |
||
675 | static const struct printf_spec io_spec = { |
||
676 | .base = 16, |
||
677 | .field_width = IO_RSRC_PRINTK_SIZE, |
||
678 | .precision = -1, |
||
679 | .flags = SPECIAL | SMALL | ZEROPAD, |
||
680 | }; |
||
681 | static const struct printf_spec mem_spec = { |
||
682 | .base = 16, |
||
683 | .field_width = MEM_RSRC_PRINTK_SIZE, |
||
684 | .precision = -1, |
||
685 | .flags = SPECIAL | SMALL | ZEROPAD, |
||
686 | }; |
||
687 | static const struct printf_spec bus_spec = { |
||
688 | .base = 16, |
||
689 | .field_width = 2, |
||
690 | .precision = -1, |
||
691 | .flags = SMALL | ZEROPAD, |
||
692 | }; |
||
693 | static const struct printf_spec dec_spec = { |
||
694 | .base = 10, |
||
695 | .precision = -1, |
||
696 | .flags = 0, |
||
697 | }; |
||
698 | static const struct printf_spec str_spec = { |
||
699 | .field_width = -1, |
||
700 | .precision = 10, |
||
701 | .flags = LEFT, |
||
702 | }; |
||
703 | static const struct printf_spec flag_spec = { |
||
704 | .base = 16, |
||
705 | .precision = -1, |
||
706 | .flags = SPECIAL | SMALL, |
||
707 | }; |
||
708 | |||
709 | /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) |
||
710 | * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ |
||
711 | #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) |
||
712 | #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) |
||
713 | #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") |
||
714 | #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") |
||
1871 | clevermous | 715 | #undef max |
716 | #define max(a,b) ((a) > (b) ? (a) : (b)) |
||
1869 | serge | 717 | char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, |
718 | 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; |
||
719 | |||
720 | char *p = sym, *pend = sym + sizeof(sym); |
||
721 | int decode = (fmt[0] == 'R') ? 1 : 0; |
||
722 | const struct printf_spec *specp; |
||
723 | |||
724 | *p++ = '['; |
||
725 | if (res->flags & IORESOURCE_IO) { |
||
726 | p = string(p, pend, "io ", str_spec); |
||
727 | specp = &io_spec; |
||
728 | } else if (res->flags & IORESOURCE_MEM) { |
||
729 | p = string(p, pend, "mem ", str_spec); |
||
730 | specp = &mem_spec; |
||
731 | } else if (res->flags & IORESOURCE_IRQ) { |
||
732 | p = string(p, pend, "irq ", str_spec); |
||
733 | specp = &dec_spec; |
||
734 | } else if (res->flags & IORESOURCE_DMA) { |
||
735 | p = string(p, pend, "dma ", str_spec); |
||
736 | specp = &dec_spec; |
||
737 | } else if (res->flags & IORESOURCE_BUS) { |
||
738 | p = string(p, pend, "bus ", str_spec); |
||
739 | specp = &bus_spec; |
||
740 | } else { |
||
741 | p = string(p, pend, "??? ", str_spec); |
||
742 | specp = &mem_spec; |
||
743 | decode = 0; |
||
744 | } |
||
745 | p = number(p, pend, res->start, *specp); |
||
746 | if (res->start != res->end) { |
||
747 | *p++ = '-'; |
||
748 | p = number(p, pend, res->end, *specp); |
||
749 | } |
||
750 | if (decode) { |
||
751 | if (res->flags & IORESOURCE_MEM_64) |
||
752 | p = string(p, pend, " 64bit", str_spec); |
||
753 | if (res->flags & IORESOURCE_PREFETCH) |
||
754 | p = string(p, pend, " pref", str_spec); |
||
755 | if (res->flags & IORESOURCE_WINDOW) |
||
756 | p = string(p, pend, " window", str_spec); |
||
757 | if (res->flags & IORESOURCE_DISABLED) |
||
758 | p = string(p, pend, " disabled", str_spec); |
||
759 | } else { |
||
760 | p = string(p, pend, " flags ", str_spec); |
||
761 | p = number(p, pend, res->flags, flag_spec); |
||
762 | } |
||
763 | *p++ = ']'; |
||
764 | *p = '\0'; |
||
765 | |||
766 | return string(buf, end, sym, spec); |
||
767 | } |
||
768 | |||
769 | static noinline_for_stack |
||
5056 | serge | 770 | char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, |
771 | const char *fmt) |
||
772 | { |
||
773 | int i, len = 1; /* if we pass '%ph[CDN]', field width remains |
||
774 | negative value, fallback to the default */ |
||
775 | char separator; |
||
776 | |||
777 | if (spec.field_width == 0) |
||
778 | /* nothing to print */ |
||
779 | return buf; |
||
780 | |||
781 | if (ZERO_OR_NULL_PTR(addr)) |
||
782 | /* NULL pointer */ |
||
783 | return string(buf, end, NULL, spec); |
||
784 | |||
785 | switch (fmt[1]) { |
||
786 | case 'C': |
||
787 | separator = ':'; |
||
788 | break; |
||
789 | case 'D': |
||
790 | separator = '-'; |
||
791 | break; |
||
792 | case 'N': |
||
793 | separator = 0; |
||
794 | break; |
||
795 | default: |
||
796 | separator = ' '; |
||
797 | break; |
||
798 | } |
||
799 | |||
800 | if (spec.field_width > 0) |
||
801 | len = min_t(int, spec.field_width, 64); |
||
802 | |||
803 | for (i = 0; i < len && buf < end - 1; i++) { |
||
804 | buf = hex_byte_pack(buf, addr[i]); |
||
805 | |||
806 | if (buf < end && separator && i != len - 1) |
||
807 | *buf++ = separator; |
||
808 | } |
||
809 | |||
810 | return buf; |
||
811 | } |
||
812 | |||
813 | static noinline_for_stack |
||
1869 | serge | 814 | char *mac_address_string(char *buf, char *end, u8 *addr, |
815 | struct printf_spec spec, const char *fmt) |
||
816 | { |
||
817 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
||
818 | char *p = mac_addr; |
||
819 | int i; |
||
820 | char separator; |
||
5056 | serge | 821 | bool reversed = false; |
1869 | serge | 822 | |
5056 | serge | 823 | switch (fmt[1]) { |
824 | case 'F': |
||
1869 | serge | 825 | separator = '-'; |
5056 | serge | 826 | break; |
827 | |||
828 | case 'R': |
||
829 | reversed = true; |
||
830 | /* fall through */ |
||
831 | |||
832 | default: |
||
1869 | serge | 833 | separator = ':'; |
5056 | serge | 834 | break; |
1869 | serge | 835 | } |
836 | |||
837 | for (i = 0; i < 6; i++) { |
||
5056 | serge | 838 | if (reversed) |
839 | p = hex_byte_pack(p, addr[5 - i]); |
||
840 | else |
||
841 | p = hex_byte_pack(p, addr[i]); |
||
842 | |||
1869 | serge | 843 | if (fmt[0] == 'M' && i != 5) |
844 | *p++ = separator; |
||
845 | } |
||
846 | *p = '\0'; |
||
847 | |||
848 | return string(buf, end, mac_addr, spec); |
||
849 | } |
||
850 | |||
851 | static noinline_for_stack |
||
852 | char *ip4_string(char *p, const u8 *addr, const char *fmt) |
||
853 | { |
||
854 | int i; |
||
855 | bool leading_zeros = (fmt[0] == 'i'); |
||
856 | int index; |
||
857 | int step; |
||
858 | |||
859 | switch (fmt[2]) { |
||
860 | case 'h': |
||
861 | #ifdef __BIG_ENDIAN |
||
862 | index = 0; |
||
863 | step = 1; |
||
864 | #else |
||
865 | index = 3; |
||
866 | step = -1; |
||
867 | #endif |
||
868 | break; |
||
869 | case 'l': |
||
870 | index = 3; |
||
871 | step = -1; |
||
872 | break; |
||
873 | case 'n': |
||
874 | case 'b': |
||
875 | default: |
||
876 | index = 0; |
||
877 | step = 1; |
||
878 | break; |
||
879 | } |
||
880 | for (i = 0; i < 4; i++) { |
||
881 | char temp[3]; /* hold each IP quad in reverse order */ |
||
5056 | serge | 882 | int digits = put_dec_trunc8(temp, addr[index]) - temp; |
1869 | serge | 883 | if (leading_zeros) { |
884 | if (digits < 3) |
||
885 | *p++ = '0'; |
||
886 | if (digits < 2) |
||
887 | *p++ = '0'; |
||
888 | } |
||
889 | /* reverse the digits in the quad */ |
||
890 | while (digits--) |
||
891 | *p++ = temp[digits]; |
||
892 | if (i < 3) |
||
893 | *p++ = '.'; |
||
894 | index += step; |
||
895 | } |
||
896 | *p = '\0'; |
||
897 | |||
898 | return p; |
||
899 | } |
||
900 | |||
901 | |||
902 | static noinline_for_stack |
||
903 | char *ip4_addr_string(char *buf, char *end, const u8 *addr, |
||
904 | struct printf_spec spec, const char *fmt) |
||
905 | { |
||
906 | char ip4_addr[sizeof("255.255.255.255")]; |
||
907 | |||
908 | ip4_string(ip4_addr, addr, fmt); |
||
909 | |||
910 | return string(buf, end, ip4_addr, spec); |
||
911 | } |
||
912 | |||
913 | |||
914 | int kptr_restrict = 1; |
||
915 | |||
916 | /* |
||
917 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
||
918 | * by an extra set of alphanumeric characters that are extended format |
||
919 | * specifiers. |
||
920 | * |
||
921 | * Right now we handle: |
||
922 | * |
||
923 | * - 'F' For symbolic function descriptor pointers with offset |
||
924 | * - 'f' For simple symbolic function names without offset |
||
925 | * - 'S' For symbolic direct pointers with offset |
||
926 | * - 's' For symbolic direct pointers without offset |
||
5056 | serge | 927 | * - '[FfSs]R' as above with __builtin_extract_return_addr() translation |
928 | * - 'B' For backtraced symbolic direct pointers with offset |
||
1869 | serge | 929 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
930 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] |
||
931 | * - 'M' For a 6-byte MAC address, it prints the address in the |
||
932 | * usual colon-separated hex notation |
||
933 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
||
934 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
||
935 | * with a dash-separated hex notation |
||
5056 | serge | 936 | * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) |
1869 | serge | 937 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
938 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) |
||
939 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's |
||
5056 | serge | 940 | * [S][pfs] |
941 | * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to |
||
942 | * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] |
||
1869 | serge | 943 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
944 | * IPv6 omits the colons (01020304...0f) |
||
945 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) |
||
5056 | serge | 946 | * [S][pfs] |
947 | * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to |
||
948 | * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] |
||
949 | * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order |
||
950 | * - 'I[6S]c' for IPv6 addresses printed as specified by |
||
951 | * http://tools.ietf.org/html/rfc5952 |
||
1869 | serge | 952 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form |
953 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
||
954 | * Options for %pU are: |
||
955 | * b big endian lower case hex (default) |
||
956 | * B big endian UPPER case hex |
||
957 | * l little endian lower case hex |
||
958 | * L little endian UPPER case hex |
||
959 | * big endian output byte order is: |
||
960 | * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] |
||
961 | * little endian output byte order is: |
||
962 | * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] |
||
963 | * - 'V' For a struct va_format which contains a format string * and va_list *, |
||
964 | * call vsnprintf(->format, *->va_list). |
||
965 | * Implements a "recursive vsnprintf". |
||
966 | * Do not use this feature without some mechanism to verify the |
||
967 | * correctness of the format string and va_list arguments. |
||
968 | * - 'K' For a kernel pointer that should be hidden from unprivileged users |
||
5056 | serge | 969 | * - 'NF' For a netdev_features_t |
970 | * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with |
||
971 | * a certain separator (' ' by default): |
||
972 | * C colon |
||
973 | * D dash |
||
974 | * N no separator |
||
975 | * The maximum supported length is 64 bytes of the input. Consider |
||
976 | * to use print_hex_dump() for the larger input. |
||
977 | * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives |
||
978 | * (default assumed to be phys_addr_t, passed by reference) |
||
979 | * - 'd[234]' For a dentry name (optionally 2-4 last components) |
||
980 | * - 'D[234]' Same as 'd' but for a struct file |
||
1869 | serge | 981 | * |
982 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
||
983 | * function pointers are really function descriptors, which contain a |
||
984 | * pointer to the real address. |
||
985 | */ |
||
986 | static noinline_for_stack |
||
987 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
||
988 | struct printf_spec spec) |
||
989 | { |
||
5056 | serge | 990 | int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0); |
991 | |||
992 | if (!ptr && *fmt != 'K') { |
||
1869 | serge | 993 | /* |
994 | * Print (null) with the same width as a pointer so it makes |
||
995 | * tabular output look nice. |
||
996 | */ |
||
997 | if (spec.field_width == -1) |
||
5056 | serge | 998 | spec.field_width = default_width; |
1869 | serge | 999 | return string(buf, end, "(null)", spec); |
1000 | } |
||
1001 | |||
1002 | switch (*fmt) { |
||
1003 | case 'F': |
||
1004 | case 'f': |
||
1005 | ptr = dereference_function_descriptor(ptr); |
||
1006 | /* Fallthrough */ |
||
1007 | case 'S': |
||
1008 | case 's': |
||
5056 | serge | 1009 | case 'B': |
1010 | return symbol_string(buf, end, ptr, spec, fmt); |
||
1869 | serge | 1011 | case 'R': |
1012 | case 'r': |
||
1013 | return resource_string(buf, end, ptr, spec, fmt); |
||
5056 | serge | 1014 | case 'h': |
1015 | return hex_string(buf, end, ptr, spec, fmt); |
||
1869 | serge | 1016 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
1017 | case 'm': /* Contiguous: 000102030405 */ |
||
5056 | serge | 1018 | /* [mM]F (FDDI) */ |
1019 | /* [mM]R (Reverse order; Bluetooth) */ |
||
1869 | serge | 1020 | return mac_address_string(buf, end, ptr, spec, fmt); |
1021 | case 'I': /* Formatted IP supported |
||
1022 | * 4: 1.2.3.4 |
||
1023 | * 6: 0001:0203:...:0708 |
||
1024 | * 6c: 1::708 or 1::1.2.3.4 |
||
1025 | */ |
||
1026 | case 'i': /* Contiguous: |
||
1027 | * 4: 001.002.003.004 |
||
1028 | * 6: 000102...0f |
||
1029 | */ |
||
1030 | switch (fmt[1]) { |
||
1031 | case '4': |
||
1032 | return ip4_addr_string(buf, end, ptr, spec, fmt); |
||
1033 | } |
||
1034 | break; |
||
1035 | case 'V': |
||
5056 | serge | 1036 | { |
1037 | va_list va; |
||
1038 | |||
1039 | va_copy(va, *((struct va_format *)ptr)->va); |
||
1040 | buf += vsnprintf(buf, end > buf ? end - buf : 0, |
||
1041 | ((struct va_format *)ptr)->fmt, va); |
||
1042 | va_end(va); |
||
1043 | return buf; |
||
1044 | } |
||
1045 | |||
1869 | serge | 1046 | } |
1047 | spec.flags |= SMALL; |
||
1048 | if (spec.field_width == -1) { |
||
5056 | serge | 1049 | spec.field_width = default_width; |
1869 | serge | 1050 | spec.flags |= ZEROPAD; |
1051 | } |
||
1052 | spec.base = 16; |
||
1053 | |||
1054 | return number(buf, end, (unsigned long) ptr, spec); |
||
1055 | } |
||
1056 | |||
1057 | /* |
||
1058 | * Helper function to decode printf style format. |
||
1059 | * Each call decode a token from the format and return the |
||
1060 | * number of characters read (or likely the delta where it wants |
||
1061 | * to go on the next call). |
||
1062 | * The decoded token is returned through the parameters |
||
1063 | * |
||
1064 | * 'h', 'l', or 'L' for integer fields |
||
1065 | * 'z' support added 23/7/1999 S.H. |
||
1066 | * 'z' changed to 'Z' --davidm 1/25/99 |
||
1067 | * 't' added for ptrdiff_t |
||
1068 | * |
||
1069 | * @fmt: the format string |
||
1070 | * @type of the token returned |
||
1071 | * @flags: various flags such as +, -, # tokens.. |
||
1072 | * @field_width: overwritten width |
||
1073 | * @base: base of the number (octal, hex, ...) |
||
1074 | * @precision: precision of a number |
||
1075 | * @qualifier: qualifier of a number (long, size_t, ...) |
||
1076 | */ |
||
1077 | static noinline_for_stack |
||
1078 | int format_decode(const char *fmt, struct printf_spec *spec) |
||
1079 | { |
||
1080 | const char *start = fmt; |
||
1081 | |||
1082 | /* we finished early by reading the field width */ |
||
1083 | if (spec->type == FORMAT_TYPE_WIDTH) { |
||
1084 | if (spec->field_width < 0) { |
||
1085 | spec->field_width = -spec->field_width; |
||
1086 | spec->flags |= LEFT; |
||
1087 | } |
||
1088 | spec->type = FORMAT_TYPE_NONE; |
||
1089 | goto precision; |
||
1090 | } |
||
1091 | |||
1092 | /* we finished early by reading the precision */ |
||
1093 | if (spec->type == FORMAT_TYPE_PRECISION) { |
||
1094 | if (spec->precision < 0) |
||
1095 | spec->precision = 0; |
||
1096 | |||
1097 | spec->type = FORMAT_TYPE_NONE; |
||
1098 | goto qualifier; |
||
1099 | } |
||
1100 | |||
1101 | /* By default */ |
||
1102 | spec->type = FORMAT_TYPE_NONE; |
||
1103 | |||
1104 | for (; *fmt ; ++fmt) { |
||
1105 | if (*fmt == '%') |
||
1106 | break; |
||
1107 | } |
||
1108 | |||
1109 | /* Return the current non-format string */ |
||
1110 | if (fmt != start || !*fmt) |
||
1111 | return fmt - start; |
||
1112 | |||
1113 | /* Process flags */ |
||
1114 | spec->flags = 0; |
||
1115 | |||
1116 | while (1) { /* this also skips first '%' */ |
||
1117 | bool found = true; |
||
1118 | |||
1119 | ++fmt; |
||
1120 | |||
1121 | switch (*fmt) { |
||
1122 | case '-': spec->flags |= LEFT; break; |
||
1123 | case '+': spec->flags |= PLUS; break; |
||
1124 | case ' ': spec->flags |= SPACE; break; |
||
1125 | case '#': spec->flags |= SPECIAL; break; |
||
1126 | case '0': spec->flags |= ZEROPAD; break; |
||
1127 | default: found = false; |
||
1128 | } |
||
1129 | |||
1130 | if (!found) |
||
1131 | break; |
||
1132 | } |
||
1133 | |||
1134 | /* get field width */ |
||
1135 | spec->field_width = -1; |
||
1136 | |||
1137 | if (isdigit(*fmt)) |
||
1138 | spec->field_width = skip_atoi(&fmt); |
||
1139 | else if (*fmt == '*') { |
||
1140 | /* it's the next argument */ |
||
1141 | spec->type = FORMAT_TYPE_WIDTH; |
||
1142 | return ++fmt - start; |
||
1143 | } |
||
1144 | |||
1145 | precision: |
||
1146 | /* get the precision */ |
||
1147 | spec->precision = -1; |
||
1148 | if (*fmt == '.') { |
||
1149 | ++fmt; |
||
1150 | if (isdigit(*fmt)) { |
||
1151 | spec->precision = skip_atoi(&fmt); |
||
1152 | if (spec->precision < 0) |
||
1153 | spec->precision = 0; |
||
1154 | } else if (*fmt == '*') { |
||
1155 | /* it's the next argument */ |
||
1156 | spec->type = FORMAT_TYPE_PRECISION; |
||
1157 | return ++fmt - start; |
||
1158 | } |
||
1159 | } |
||
1160 | |||
1161 | qualifier: |
||
1162 | /* get the conversion qualifier */ |
||
1163 | spec->qualifier = -1; |
||
5056 | serge | 1164 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
1165 | _tolower(*fmt) == 'z' || *fmt == 't') { |
||
1869 | serge | 1166 | spec->qualifier = *fmt++; |
1167 | if (unlikely(spec->qualifier == *fmt)) { |
||
1168 | if (spec->qualifier == 'l') { |
||
1169 | spec->qualifier = 'L'; |
||
1170 | ++fmt; |
||
1171 | } else if (spec->qualifier == 'h') { |
||
1172 | spec->qualifier = 'H'; |
||
1173 | ++fmt; |
||
1174 | } |
||
1175 | } |
||
1176 | } |
||
1177 | |||
1178 | /* default base */ |
||
1179 | spec->base = 10; |
||
1180 | switch (*fmt) { |
||
1181 | case 'c': |
||
1182 | spec->type = FORMAT_TYPE_CHAR; |
||
1183 | return ++fmt - start; |
||
1184 | |||
1185 | case 's': |
||
1186 | spec->type = FORMAT_TYPE_STR; |
||
1187 | return ++fmt - start; |
||
1188 | |||
1189 | case 'p': |
||
1190 | spec->type = FORMAT_TYPE_PTR; |
||
1191 | return fmt - start; |
||
1192 | /* skip alnum */ |
||
1193 | |||
1194 | case '%': |
||
1195 | spec->type = FORMAT_TYPE_PERCENT_CHAR; |
||
1196 | return ++fmt - start; |
||
1197 | |||
1198 | /* integer number formats - set up the flags and "break" */ |
||
1199 | case 'o': |
||
1200 | spec->base = 8; |
||
1201 | break; |
||
1202 | |||
1203 | case 'x': |
||
1204 | spec->flags |= SMALL; |
||
1205 | |||
1206 | case 'X': |
||
1207 | spec->base = 16; |
||
1208 | break; |
||
1209 | |||
1210 | case 'd': |
||
1211 | case 'i': |
||
1212 | spec->flags |= SIGN; |
||
1213 | case 'u': |
||
1214 | break; |
||
1215 | |||
5056 | serge | 1216 | case 'n': |
1217 | /* |
||
1218 | * Since %n poses a greater security risk than utility, treat |
||
1219 | * it as an invalid format specifier. Warn about its use so |
||
1220 | * that new instances don't get added. |
||
1221 | */ |
||
1222 | // WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt); |
||
1223 | /* Fall-through */ |
||
1224 | |||
1869 | serge | 1225 | default: |
1226 | spec->type = FORMAT_TYPE_INVALID; |
||
1227 | return fmt - start; |
||
1228 | } |
||
1229 | |||
1230 | if (spec->qualifier == 'L') |
||
1231 | spec->type = FORMAT_TYPE_LONG_LONG; |
||
1232 | else if (spec->qualifier == 'l') { |
||
1233 | if (spec->flags & SIGN) |
||
1234 | spec->type = FORMAT_TYPE_LONG; |
||
1235 | else |
||
1236 | spec->type = FORMAT_TYPE_ULONG; |
||
5056 | serge | 1237 | } else if (_tolower(spec->qualifier) == 'z') { |
1869 | serge | 1238 | spec->type = FORMAT_TYPE_SIZE_T; |
1239 | } else if (spec->qualifier == 't') { |
||
1240 | spec->type = FORMAT_TYPE_PTRDIFF; |
||
1241 | } else if (spec->qualifier == 'H') { |
||
1242 | if (spec->flags & SIGN) |
||
1243 | spec->type = FORMAT_TYPE_BYTE; |
||
1244 | else |
||
1245 | spec->type = FORMAT_TYPE_UBYTE; |
||
1246 | } else if (spec->qualifier == 'h') { |
||
1247 | if (spec->flags & SIGN) |
||
1248 | spec->type = FORMAT_TYPE_SHORT; |
||
1249 | else |
||
1250 | spec->type = FORMAT_TYPE_USHORT; |
||
1251 | } else { |
||
1252 | if (spec->flags & SIGN) |
||
1253 | spec->type = FORMAT_TYPE_INT; |
||
1254 | else |
||
1255 | spec->type = FORMAT_TYPE_UINT; |
||
1256 | } |
||
1257 | |||
1258 | return ++fmt - start; |
||
1259 | } |
||
1260 | |||
1261 | /** |
||
1262 | * vsnprintf - Format a string and place it in a buffer |
||
1263 | * @buf: The buffer to place the result into |
||
1264 | * @size: The size of the buffer, including the trailing null space |
||
1265 | * @fmt: The format string to use |
||
1266 | * @args: Arguments for the format string |
||
1267 | * |
||
1268 | * This function follows C99 vsnprintf, but has some extensions: |
||
1269 | * %pS output the name of a text symbol with offset |
||
1270 | * %ps output the name of a text symbol without offset |
||
1271 | * %pF output the name of a function pointer with its offset |
||
1272 | * %pf output the name of a function pointer without its offset |
||
5056 | serge | 1273 | * %pB output the name of a backtrace symbol with its offset |
1869 | serge | 1274 | * %pR output the address range in a struct resource with decoded flags |
1275 | * %pr output the address range in a struct resource with raw flags |
||
1276 | * %pM output a 6-byte MAC address with colons |
||
5056 | serge | 1277 | * %pMR output a 6-byte MAC address with colons in reversed order |
1278 | * %pMF output a 6-byte MAC address with dashes |
||
1869 | serge | 1279 | * %pm output a 6-byte MAC address without colons |
5056 | serge | 1280 | * %pmR output a 6-byte MAC address without colons in reversed order |
1869 | serge | 1281 | * %pI4 print an IPv4 address without leading zeros |
1282 | * %pi4 print an IPv4 address with leading zeros |
||
1283 | * %pI6 print an IPv6 address with colons |
||
1284 | * %pi6 print an IPv6 address without colons |
||
5056 | serge | 1285 | * %pI6c print an IPv6 address as specified by RFC 5952 |
1286 | * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address |
||
1287 | * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address |
||
1869 | serge | 1288 | * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper |
1289 | * case. |
||
5270 | serge | 1290 | * %*pE[achnops] print an escaped buffer |
5056 | serge | 1291 | * %*ph[CDN] a variable-length hex string with a separator (supports up to 64 |
1292 | * bytes of the input) |
||
1869 | serge | 1293 | * %n is ignored |
1294 | * |
||
5056 | serge | 1295 | * ** Please update Documentation/printk-formats.txt when making changes ** |
1296 | * |
||
1869 | serge | 1297 | * The return value is the number of characters which would |
1298 | * be generated for the given input, excluding the trailing |
||
1299 | * '\0', as per ISO C99. If you want to have the exact |
||
1300 | * number of characters written into @buf as return value |
||
1301 | * (not including the trailing '\0'), use vscnprintf(). If the |
||
1302 | * return is greater than or equal to @size, the resulting |
||
1303 | * string is truncated. |
||
1304 | * |
||
5056 | serge | 1305 | * If you're not already dealing with a va_list consider using snprintf(). |
1869 | serge | 1306 | */ |
1307 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
||
1308 | { |
||
1309 | unsigned long long num; |
||
1310 | char *str, *end; |
||
1311 | struct printf_spec spec = {0}; |
||
1312 | |||
1313 | /* Reject out-of-range values early. Large positive sizes are |
||
1314 | used for unknown buffer sizes. */ |
||
5056 | serge | 1315 | if ((int) size < 0) |
1869 | serge | 1316 | return 0; |
1317 | |||
1318 | str = buf; |
||
1319 | end = buf + size; |
||
1320 | |||
1321 | /* Make sure end is always >= buf */ |
||
1322 | if (end < buf) { |
||
1323 | end = ((void *)-1); |
||
1324 | size = end - buf; |
||
1325 | } |
||
1326 | |||
1327 | while (*fmt) { |
||
1328 | const char *old_fmt = fmt; |
||
1329 | int read = format_decode(fmt, &spec); |
||
1330 | |||
1331 | fmt += read; |
||
1332 | |||
1333 | switch (spec.type) { |
||
1334 | case FORMAT_TYPE_NONE: { |
||
1335 | int copy = read; |
||
1336 | if (str < end) { |
||
1337 | if (copy > end - str) |
||
1338 | copy = end - str; |
||
1339 | memcpy(str, old_fmt, copy); |
||
1340 | } |
||
1341 | str += read; |
||
1342 | break; |
||
1343 | } |
||
1344 | |||
1345 | case FORMAT_TYPE_WIDTH: |
||
1346 | spec.field_width = va_arg(args, int); |
||
1347 | break; |
||
1348 | |||
1349 | case FORMAT_TYPE_PRECISION: |
||
1350 | spec.precision = va_arg(args, int); |
||
1351 | break; |
||
1352 | |||
1353 | case FORMAT_TYPE_CHAR: { |
||
1354 | char c; |
||
1355 | |||
1356 | if (!(spec.flags & LEFT)) { |
||
1357 | while (--spec.field_width > 0) { |
||
1358 | if (str < end) |
||
1359 | *str = ' '; |
||
1360 | ++str; |
||
1361 | |||
1362 | } |
||
1363 | } |
||
1364 | c = (unsigned char) va_arg(args, int); |
||
1365 | if (str < end) |
||
1366 | *str = c; |
||
1367 | ++str; |
||
1368 | while (--spec.field_width > 0) { |
||
1369 | if (str < end) |
||
1370 | *str = ' '; |
||
1371 | ++str; |
||
1372 | } |
||
1373 | break; |
||
1374 | } |
||
1375 | |||
1376 | case FORMAT_TYPE_STR: |
||
1377 | str = string(str, end, va_arg(args, char *), spec); |
||
1378 | break; |
||
1379 | |||
1380 | case FORMAT_TYPE_PTR: |
||
1381 | str = pointer(fmt+1, str, end, va_arg(args, void *), |
||
1382 | spec); |
||
1383 | while (isalnum(*fmt)) |
||
1384 | fmt++; |
||
1385 | break; |
||
1386 | |||
1387 | case FORMAT_TYPE_PERCENT_CHAR: |
||
1388 | if (str < end) |
||
1389 | *str = '%'; |
||
1390 | ++str; |
||
1391 | break; |
||
1392 | |||
1393 | case FORMAT_TYPE_INVALID: |
||
1394 | if (str < end) |
||
1395 | *str = '%'; |
||
1396 | ++str; |
||
1397 | break; |
||
1398 | |||
1399 | default: |
||
1400 | switch (spec.type) { |
||
1401 | case FORMAT_TYPE_LONG_LONG: |
||
1402 | num = va_arg(args, long long); |
||
1403 | break; |
||
1404 | case FORMAT_TYPE_ULONG: |
||
1405 | num = va_arg(args, unsigned long); |
||
1406 | break; |
||
1407 | case FORMAT_TYPE_LONG: |
||
1408 | num = va_arg(args, long); |
||
1409 | break; |
||
1410 | case FORMAT_TYPE_SIZE_T: |
||
5056 | serge | 1411 | if (spec.flags & SIGN) |
1412 | num = va_arg(args, ssize_t); |
||
1413 | else |
||
1869 | serge | 1414 | num = va_arg(args, size_t); |
1415 | break; |
||
1416 | case FORMAT_TYPE_PTRDIFF: |
||
1417 | num = va_arg(args, ptrdiff_t); |
||
1418 | break; |
||
1419 | case FORMAT_TYPE_UBYTE: |
||
1420 | num = (unsigned char) va_arg(args, int); |
||
1421 | break; |
||
1422 | case FORMAT_TYPE_BYTE: |
||
1423 | num = (signed char) va_arg(args, int); |
||
1424 | break; |
||
1425 | case FORMAT_TYPE_USHORT: |
||
1426 | num = (unsigned short) va_arg(args, int); |
||
1427 | break; |
||
1428 | case FORMAT_TYPE_SHORT: |
||
1429 | num = (short) va_arg(args, int); |
||
1430 | break; |
||
1431 | case FORMAT_TYPE_INT: |
||
1432 | num = (int) va_arg(args, int); |
||
1433 | break; |
||
1434 | default: |
||
1435 | num = va_arg(args, unsigned int); |
||
1436 | } |
||
1437 | |||
1438 | str = number(str, end, num, spec); |
||
1439 | } |
||
1440 | } |
||
1441 | |||
1442 | if (size > 0) { |
||
1443 | if (str < end) |
||
1444 | *str = '\0'; |
||
1445 | else |
||
1446 | end[-1] = '\0'; |
||
1447 | } |
||
1448 | |||
1449 | /* the trailing null byte doesn't count towards the total */ |
||
1450 | return str-buf; |
||
1451 | |||
1452 | } |
||
1453 | EXPORT_SYMBOL(vsnprintf); |
||
1454 | |||
5056 | serge | 1455 | /** |
1456 | * vscnprintf - Format a string and place it in a buffer |
||
1457 | * @buf: The buffer to place the result into |
||
1458 | * @size: The size of the buffer, including the trailing null space |
||
1459 | * @fmt: The format string to use |
||
1460 | * @args: Arguments for the format string |
||
1461 | * |
||
1462 | * The return value is the number of characters which have been written into |
||
1463 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
||
1464 | * returns 0. |
||
1465 | * |
||
1466 | * If you're not already dealing with a va_list consider using scnprintf(). |
||
1467 | * |
||
1468 | * See the vsnprintf() documentation for format string extensions over C99. |
||
1469 | */ |
||
1470 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) |
||
1869 | serge | 1471 | { |
5056 | serge | 1472 | int i; |
1473 | |||
1474 | i = vsnprintf(buf, size, fmt, args); |
||
1475 | |||
1476 | if (likely(i < size)) |
||
1477 | return i; |
||
1478 | if (size != 0) |
||
1479 | return size - 1; |
||
1480 | return 0; |
||
1869 | serge | 1481 | } |
5056 | serge | 1482 | EXPORT_SYMBOL(vscnprintf); |
1869 | serge | 1483 | |
1484 | /** |
||
1485 | * snprintf - Format a string and place it in a buffer |
||
1486 | * @buf: The buffer to place the result into |
||
1487 | * @size: The size of the buffer, including the trailing null space |
||
1488 | * @fmt: The format string to use |
||
1489 | * @...: Arguments for the format string |
||
1490 | * |
||
1491 | * The return value is the number of characters which would be |
||
1492 | * generated for the given input, excluding the trailing null, |
||
1493 | * as per ISO C99. If the return is greater than or equal to |
||
1494 | * @size, the resulting string is truncated. |
||
1495 | * |
||
1496 | * See the vsnprintf() documentation for format string extensions over C99. |
||
1497 | */ |
||
1498 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
||
1499 | { |
||
1500 | va_list args; |
||
1501 | int i; |
||
1502 | |||
1503 | va_start(args, fmt); |
||
1504 | i = vsnprintf(buf, size, fmt, args); |
||
1505 | va_end(args); |
||
1506 | |||
1507 | return i; |
||
1508 | } |
||
1509 | EXPORT_SYMBOL(snprintf); |
||
1510 | |||
5056 | serge | 1511 | /** |
1512 | * scnprintf - Format a string and place it in a buffer |
||
1513 | * @buf: The buffer to place the result into |
||
1514 | * @size: The size of the buffer, including the trailing null space |
||
1515 | * @fmt: The format string to use |
||
1516 | * @...: Arguments for the format string |
||
1517 | * |
||
1518 | * The return value is the number of characters written into @buf not including |
||
1519 | * the trailing '\0'. If @size is == 0 the function returns 0. |
||
1520 | */ |
||
1869 | serge | 1521 | |
5056 | serge | 1522 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
1523 | { |
||
1524 | va_list args; |
||
1525 | int i; |
||
1526 | |||
1527 | va_start(args, fmt); |
||
1528 | i = vscnprintf(buf, size, fmt, args); |
||
1529 | va_end(args); |
||
1530 | |||
1531 | return i; |
||
1532 | } |
||
1533 | EXPORT_SYMBOL(scnprintf); |
||
1534 | |||
1869 | serge | 1535 | /** |
5056 | serge | 1536 | * vsprintf - Format a string and place it in a buffer |
1537 | * @buf: The buffer to place the result into |
||
1538 | * @fmt: The format string to use |
||
1539 | * @args: Arguments for the format string |
||
1540 | * |
||
1541 | * The function returns the number of characters written |
||
1542 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
||
1543 | * buffer overflows. |
||
1544 | * |
||
1545 | * If you're not already dealing with a va_list consider using sprintf(). |
||
1546 | * |
||
1547 | * See the vsnprintf() documentation for format string extensions over C99. |
||
1548 | */ |
||
1549 | int vsprintf(char *buf, const char *fmt, va_list args) |
||
1550 | { |
||
1551 | return vsnprintf(buf, INT_MAX, fmt, args); |
||
1552 | } |
||
1553 | EXPORT_SYMBOL(vsprintf); |
||
1554 | |||
1555 | /** |
||
1869 | serge | 1556 | * sprintf - Format a string and place it in a buffer |
1557 | * @buf: The buffer to place the result into |
||
1558 | * @fmt: The format string to use |
||
1559 | * @...: Arguments for the format string |
||
1560 | * |
||
1561 | * The function returns the number of characters written |
||
1562 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
||
1563 | * buffer overflows. |
||
1564 | * |
||
1565 | * See the vsnprintf() documentation for format string extensions over C99. |
||
1566 | */ |
||
1567 | int sprintf(char *buf, const char *fmt, ...) |
||
1568 | { |
||
1569 | va_list args; |
||
1570 | int i; |
||
1571 | |||
1572 | va_start(args, fmt); |
||
1573 | i = vsnprintf(buf, INT_MAX, fmt, args); |
||
1574 | va_end(args); |
||
1575 | |||
1576 | return i; |
||
1577 | } |
||
5056 | serge | 1578 | EXPORT_SYMBOL(sprintf); |
1579 | /** |
||
1580 | * vsscanf - Unformat a buffer into a list of arguments |
||
1581 | * @buf: input buffer |
||
1582 | * @fmt: format of buffer |
||
1583 | * @args: arguments |
||
1584 | */ |
||
1585 | int vsscanf(const char *buf, const char *fmt, va_list args) |
||
1586 | { |
||
1587 | const char *str = buf; |
||
1588 | char *next; |
||
1589 | char digit; |
||
1590 | int num = 0; |
||
1591 | u8 qualifier; |
||
1592 | unsigned int base; |
||
1593 | union { |
||
1594 | long long s; |
||
1595 | unsigned long long u; |
||
1596 | } val; |
||
1597 | s16 field_width; |
||
1598 | bool is_sign; |
||
1869 | serge | 1599 | |
5056 | serge | 1600 | while (*fmt) { |
1601 | /* skip any white space in format */ |
||
1602 | /* white space in format matchs any amount of |
||
1603 | * white space, including none, in the input. |
||
1604 | */ |
||
1605 | if (isspace(*fmt)) { |
||
1606 | fmt = skip_spaces(++fmt); |
||
1607 | str = skip_spaces(str); |
||
1608 | } |
||
1609 | |||
1610 | /* anything that is not a conversion must match exactly */ |
||
1611 | if (*fmt != '%' && *fmt) { |
||
1612 | if (*fmt++ != *str++) |
||
1613 | break; |
||
1614 | continue; |
||
1615 | } |
||
1616 | |||
1617 | if (!*fmt) |
||
1618 | break; |
||
1619 | ++fmt; |
||
1620 | |||
1621 | /* skip this conversion. |
||
1622 | * advance both strings to next white space |
||
1623 | */ |
||
1624 | if (*fmt == '*') { |
||
1625 | if (!*str) |
||
1626 | break; |
||
1627 | while (!isspace(*fmt) && *fmt != '%' && *fmt) |
||
1628 | fmt++; |
||
1629 | while (!isspace(*str) && *str) |
||
1630 | str++; |
||
1631 | continue; |
||
1632 | } |
||
1633 | |||
1634 | /* get field width */ |
||
1635 | field_width = -1; |
||
1636 | if (isdigit(*fmt)) { |
||
1637 | field_width = skip_atoi(&fmt); |
||
1638 | if (field_width <= 0) |
||
1639 | break; |
||
1640 | } |
||
1641 | |||
1642 | /* get conversion qualifier */ |
||
1643 | qualifier = -1; |
||
1644 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
||
1645 | _tolower(*fmt) == 'z') { |
||
1646 | qualifier = *fmt++; |
||
1647 | if (unlikely(qualifier == *fmt)) { |
||
1648 | if (qualifier == 'h') { |
||
1649 | qualifier = 'H'; |
||
1650 | fmt++; |
||
1651 | } else if (qualifier == 'l') { |
||
1652 | qualifier = 'L'; |
||
1653 | fmt++; |
||
1654 | } |
||
1655 | } |
||
1656 | } |
||
1657 | |||
1658 | if (!*fmt) |
||
1659 | break; |
||
1660 | |||
1661 | if (*fmt == 'n') { |
||
1662 | /* return number of characters read so far */ |
||
1663 | *va_arg(args, int *) = str - buf; |
||
1664 | ++fmt; |
||
1665 | continue; |
||
1666 | } |
||
1667 | |||
1668 | if (!*str) |
||
1669 | break; |
||
1670 | |||
1671 | base = 10; |
||
1672 | is_sign = false; |
||
1673 | |||
1674 | switch (*fmt++) { |
||
1675 | case 'c': |
||
1676 | { |
||
1677 | char *s = (char *)va_arg(args, char*); |
||
1678 | if (field_width == -1) |
||
1679 | field_width = 1; |
||
1680 | do { |
||
1681 | *s++ = *str++; |
||
1682 | } while (--field_width > 0 && *str); |
||
1683 | num++; |
||
1684 | } |
||
1685 | continue; |
||
1686 | case 's': |
||
1687 | { |
||
1688 | char *s = (char *)va_arg(args, char *); |
||
1689 | if (field_width == -1) |
||
1690 | field_width = SHRT_MAX; |
||
1691 | /* first, skip leading white space in buffer */ |
||
1692 | str = skip_spaces(str); |
||
1693 | |||
1694 | /* now copy until next white space */ |
||
1695 | while (*str && !isspace(*str) && field_width--) |
||
1696 | *s++ = *str++; |
||
1697 | *s = '\0'; |
||
1698 | num++; |
||
1699 | } |
||
1700 | continue; |
||
1701 | case 'o': |
||
1702 | base = 8; |
||
1703 | break; |
||
1704 | case 'x': |
||
1705 | case 'X': |
||
1706 | base = 16; |
||
1707 | break; |
||
1708 | case 'i': |
||
1709 | base = 0; |
||
1710 | case 'd': |
||
1711 | is_sign = true; |
||
1712 | case 'u': |
||
1713 | break; |
||
1714 | case '%': |
||
1715 | /* looking for '%' in str */ |
||
1716 | if (*str++ != '%') |
||
1717 | return num; |
||
1718 | continue; |
||
1719 | default: |
||
1720 | /* invalid format; stop here */ |
||
1721 | return num; |
||
1722 | } |
||
1723 | |||
1724 | /* have some sort of integer conversion. |
||
1725 | * first, skip white space in buffer. |
||
1726 | */ |
||
1727 | str = skip_spaces(str); |
||
1728 | |||
1729 | digit = *str; |
||
1730 | if (is_sign && digit == '-') |
||
1731 | digit = *(str + 1); |
||
1732 | |||
1733 | if (!digit |
||
1734 | || (base == 16 && !isxdigit(digit)) |
||
1735 | || (base == 10 && !isdigit(digit)) |
||
1736 | || (base == 8 && (!isdigit(digit) || digit > '7')) |
||
1737 | || (base == 0 && !isdigit(digit))) |
||
1738 | break; |
||
1739 | |||
1740 | if (is_sign) |
||
1741 | val.s = qualifier != 'L' ? |
||
1742 | simple_strtol(str, &next, base) : |
||
1743 | simple_strtoll(str, &next, base); |
||
1744 | else |
||
1745 | val.u = qualifier != 'L' ? |
||
1746 | simple_strtoul(str, &next, base) : |
||
1747 | simple_strtoull(str, &next, base); |
||
1748 | |||
1749 | if (field_width > 0 && next - str > field_width) { |
||
1750 | if (base == 0) |
||
1751 | _parse_integer_fixup_radix(str, &base); |
||
1752 | while (next - str > field_width) { |
||
1753 | if (is_sign) |
||
1754 | val.s = div_s64(val.s, base); |
||
1755 | else |
||
1756 | val.u = div_u64(val.u, base); |
||
1757 | --next; |
||
1758 | } |
||
1759 | } |
||
1760 | |||
1761 | switch (qualifier) { |
||
1762 | case 'H': /* that's 'hh' in format */ |
||
1763 | if (is_sign) |
||
1764 | *va_arg(args, signed char *) = val.s; |
||
1765 | else |
||
1766 | *va_arg(args, unsigned char *) = val.u; |
||
1767 | break; |
||
1768 | case 'h': |
||
1769 | if (is_sign) |
||
1770 | *va_arg(args, short *) = val.s; |
||
1771 | else |
||
1772 | *va_arg(args, unsigned short *) = val.u; |
||
1773 | break; |
||
1774 | case 'l': |
||
1775 | if (is_sign) |
||
1776 | *va_arg(args, long *) = val.s; |
||
1777 | else |
||
1778 | *va_arg(args, unsigned long *) = val.u; |
||
1779 | break; |
||
1780 | case 'L': |
||
1781 | if (is_sign) |
||
1782 | *va_arg(args, long long *) = val.s; |
||
1783 | else |
||
1784 | *va_arg(args, unsigned long long *) = val.u; |
||
1785 | break; |
||
1786 | case 'Z': |
||
1787 | case 'z': |
||
1788 | *va_arg(args, size_t *) = val.u; |
||
1789 | break; |
||
1790 | default: |
||
1791 | if (is_sign) |
||
1792 | *va_arg(args, int *) = val.s; |
||
1793 | else |
||
1794 | *va_arg(args, unsigned int *) = val.u; |
||
1795 | break; |
||
1796 | } |
||
1797 | num++; |
||
1798 | |||
1799 | if (!next) |
||
1800 | break; |
||
1801 | str = next; |
||
1802 | } |
||
1803 | |||
1804 | return num; |
||
1805 | } |
||
1806 | EXPORT_SYMBOL(vsscanf); |
||
1807 | |||
1808 | /** |
||
1809 | * sscanf - Unformat a buffer into a list of arguments |
||
1810 | * @buf: input buffer |
||
1811 | * @fmt: formatting of buffer |
||
1812 | * @...: resulting arguments |
||
1813 | */ |
||
1814 | int sscanf(const char *buf, const char *fmt, ...) |
||
1815 | { |
||
1816 | va_list args; |
||
1817 | int i; |
||
1818 | |||
1819 | va_start(args, fmt); |
||
1820 | i = vsscanf(buf, fmt, args); |
||
1821 | va_end(args); |
||
1822 | |||
1823 | return i; |
||
1824 | } |
||
1825 | EXPORT_SYMBOL(sscanf);=>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>=>>>>>>>>>=>>>=>=>=>=>>>>>>>>>>>>>>>>>><>=>=>=>=>><>=> |