diff options
author | Paul Mundt <lethal@linux-sh.org> | 2011-01-13 01:06:28 -0500 |
---|---|---|
committer | Paul Mundt <lethal@linux-sh.org> | 2011-01-13 01:06:28 -0500 |
commit | f43dc23d5ea91fca257be02138a255f02d98e806 (patch) | |
tree | b29722f6e965316e90ac97abf79923ced250dc21 /lib/vsprintf.c | |
parent | f8e53553f452dcbf67cb89c8cba63a1cd6eb4cc0 (diff) | |
parent | 4162cf64973df51fc885825bc9ca4d055891c49f (diff) |
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6 into common/serial-rework
Conflicts:
arch/sh/kernel/cpu/sh2/setup-sh7619.c
arch/sh/kernel/cpu/sh2a/setup-mxg.c
arch/sh/kernel/cpu/sh2a/setup-sh7201.c
arch/sh/kernel/cpu/sh2a/setup-sh7203.c
arch/sh/kernel/cpu/sh2a/setup-sh7206.c
arch/sh/kernel/cpu/sh3/setup-sh7705.c
arch/sh/kernel/cpu/sh3/setup-sh770x.c
arch/sh/kernel/cpu/sh3/setup-sh7710.c
arch/sh/kernel/cpu/sh3/setup-sh7720.c
arch/sh/kernel/cpu/sh4/setup-sh4-202.c
arch/sh/kernel/cpu/sh4/setup-sh7750.c
arch/sh/kernel/cpu/sh4/setup-sh7760.c
arch/sh/kernel/cpu/sh4a/setup-sh7343.c
arch/sh/kernel/cpu/sh4a/setup-sh7366.c
arch/sh/kernel/cpu/sh4a/setup-sh7722.c
arch/sh/kernel/cpu/sh4a/setup-sh7723.c
arch/sh/kernel/cpu/sh4a/setup-sh7724.c
arch/sh/kernel/cpu/sh4a/setup-sh7763.c
arch/sh/kernel/cpu/sh4a/setup-sh7770.c
arch/sh/kernel/cpu/sh4a/setup-sh7780.c
arch/sh/kernel/cpu/sh4a/setup-sh7785.c
arch/sh/kernel/cpu/sh4a/setup-sh7786.c
arch/sh/kernel/cpu/sh4a/setup-shx3.c
arch/sh/kernel/cpu/sh5/setup-sh5.c
drivers/serial/sh-sci.c
drivers/serial/sh-sci.h
include/linux/serial_sci.h
Diffstat (limited to 'lib/vsprintf.c')
-rw-r--r-- | lib/vsprintf.c | 871 |
1 files changed, 580 insertions, 291 deletions
diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 756ccafa9cec..c150d3dafff4 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c | |||
@@ -9,7 +9,7 @@ | |||
9 | * Wirzenius wrote this portably, Torvalds fucked it up :-) | 9 | * Wirzenius wrote this portably, Torvalds fucked it up :-) |
10 | */ | 10 | */ |
11 | 11 | ||
12 | /* | 12 | /* |
13 | * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> | 13 | * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> |
14 | * - changed to provide snprintf and vsnprintf functions | 14 | * - changed to provide snprintf and vsnprintf functions |
15 | * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> | 15 | * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> |
@@ -25,6 +25,7 @@ | |||
25 | #include <linux/kallsyms.h> | 25 | #include <linux/kallsyms.h> |
26 | #include <linux/uaccess.h> | 26 | #include <linux/uaccess.h> |
27 | #include <linux/ioport.h> | 27 | #include <linux/ioport.h> |
28 | #include <net/addrconf.h> | ||
28 | 29 | ||
29 | #include <asm/page.h> /* for PAGE_SIZE */ | 30 | #include <asm/page.h> /* for PAGE_SIZE */ |
30 | #include <asm/div64.h> | 31 | #include <asm/div64.h> |
@@ -46,14 +47,14 @@ static unsigned int simple_guess_base(const char *cp) | |||
46 | } | 47 | } |
47 | 48 | ||
48 | /** | 49 | /** |
49 | * simple_strtoul - convert a string to an unsigned long | 50 | * simple_strtoull - convert a string to an unsigned long long |
50 | * @cp: The start of the string | 51 | * @cp: The start of the string |
51 | * @endp: A pointer to the end of the parsed string will be placed here | 52 | * @endp: A pointer to the end of the parsed string will be placed here |
52 | * @base: The number base to use | 53 | * @base: The number base to use |
53 | */ | 54 | */ |
54 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) | 55 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
55 | { | 56 | { |
56 | unsigned long result = 0; | 57 | unsigned long long result = 0; |
57 | 58 | ||
58 | if (!base) | 59 | if (!base) |
59 | base = simple_guess_base(cp); | 60 | base = simple_guess_base(cp); |
@@ -70,58 +71,39 @@ unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) | |||
70 | result = result * base + value; | 71 | result = result * base + value; |
71 | cp++; | 72 | cp++; |
72 | } | 73 | } |
73 | |||
74 | if (endp) | 74 | if (endp) |
75 | *endp = (char *)cp; | 75 | *endp = (char *)cp; |
76 | |||
76 | return result; | 77 | return result; |
77 | } | 78 | } |
78 | EXPORT_SYMBOL(simple_strtoul); | 79 | EXPORT_SYMBOL(simple_strtoull); |
79 | 80 | ||
80 | /** | 81 | /** |
81 | * simple_strtol - convert a string to a signed long | 82 | * simple_strtoul - convert a string to an unsigned long |
82 | * @cp: The start of the string | 83 | * @cp: The start of the string |
83 | * @endp: A pointer to the end of the parsed string will be placed here | 84 | * @endp: A pointer to the end of the parsed string will be placed here |
84 | * @base: The number base to use | 85 | * @base: The number base to use |
85 | */ | 86 | */ |
86 | long simple_strtol(const char *cp, char **endp, unsigned int base) | 87 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
87 | { | 88 | { |
88 | if(*cp == '-') | 89 | return simple_strtoull(cp, endp, base); |
89 | return -simple_strtoul(cp + 1, endp, base); | ||
90 | return simple_strtoul(cp, endp, base); | ||
91 | } | 90 | } |
92 | EXPORT_SYMBOL(simple_strtol); | 91 | EXPORT_SYMBOL(simple_strtoul); |
93 | 92 | ||
94 | /** | 93 | /** |
95 | * simple_strtoull - convert a string to an unsigned long long | 94 | * simple_strtol - convert a string to a signed long |
96 | * @cp: The start of the string | 95 | * @cp: The start of the string |
97 | * @endp: A pointer to the end of the parsed string will be placed here | 96 | * @endp: A pointer to the end of the parsed string will be placed here |
98 | * @base: The number base to use | 97 | * @base: The number base to use |
99 | */ | 98 | */ |
100 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) | 99 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
101 | { | 100 | { |
102 | unsigned long long result = 0; | 101 | if (*cp == '-') |
103 | 102 | return -simple_strtoul(cp + 1, endp, base); | |
104 | if (!base) | ||
105 | base = simple_guess_base(cp); | ||
106 | |||
107 | if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') | ||
108 | cp += 2; | ||
109 | |||
110 | while (isxdigit(*cp)) { | ||
111 | unsigned int value; | ||
112 | |||
113 | value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; | ||
114 | if (value >= base) | ||
115 | break; | ||
116 | result = result * base + value; | ||
117 | cp++; | ||
118 | } | ||
119 | 103 | ||
120 | if (endp) | 104 | return simple_strtoul(cp, endp, base); |
121 | *endp = (char *)cp; | ||
122 | return result; | ||
123 | } | 105 | } |
124 | EXPORT_SYMBOL(simple_strtoull); | 106 | EXPORT_SYMBOL(simple_strtol); |
125 | 107 | ||
126 | /** | 108 | /** |
127 | * simple_strtoll - convert a string to a signed long long | 109 | * simple_strtoll - convert a string to a signed long long |
@@ -131,10 +113,12 @@ EXPORT_SYMBOL(simple_strtoull); | |||
131 | */ | 113 | */ |
132 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) | 114 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
133 | { | 115 | { |
134 | if(*cp=='-') | 116 | if (*cp == '-') |
135 | return -simple_strtoull(cp + 1, endp, base); | 117 | return -simple_strtoull(cp + 1, endp, base); |
118 | |||
136 | return simple_strtoull(cp, endp, base); | 119 | return simple_strtoull(cp, endp, base); |
137 | } | 120 | } |
121 | EXPORT_SYMBOL(simple_strtoll); | ||
138 | 122 | ||
139 | /** | 123 | /** |
140 | * strict_strtoul - convert a string to an unsigned long strictly | 124 | * strict_strtoul - convert a string to an unsigned long strictly |
@@ -162,18 +146,16 @@ int strict_strtoul(const char *cp, unsigned int base, unsigned long *res) | |||
162 | { | 146 | { |
163 | char *tail; | 147 | char *tail; |
164 | unsigned long val; | 148 | unsigned long val; |
165 | size_t len; | ||
166 | 149 | ||
167 | *res = 0; | 150 | *res = 0; |
168 | len = strlen(cp); | 151 | if (!*cp) |
169 | if (len == 0) | ||
170 | return -EINVAL; | 152 | return -EINVAL; |
171 | 153 | ||
172 | val = simple_strtoul(cp, &tail, base); | 154 | val = simple_strtoul(cp, &tail, base); |
173 | if (tail == cp) | 155 | if (tail == cp) |
174 | return -EINVAL; | 156 | return -EINVAL; |
175 | if ((*tail == '\0') || | 157 | |
176 | ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { | 158 | if ((tail[0] == '\0') || (tail[0] == '\n' && tail[1] == '\0')) { |
177 | *res = val; | 159 | *res = val; |
178 | return 0; | 160 | return 0; |
179 | } | 161 | } |
@@ -235,18 +217,15 @@ int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res) | |||
235 | { | 217 | { |
236 | char *tail; | 218 | char *tail; |
237 | unsigned long long val; | 219 | unsigned long long val; |
238 | size_t len; | ||
239 | 220 | ||
240 | *res = 0; | 221 | *res = 0; |
241 | len = strlen(cp); | 222 | if (!*cp) |
242 | if (len == 0) | ||
243 | return -EINVAL; | 223 | return -EINVAL; |
244 | 224 | ||
245 | val = simple_strtoull(cp, &tail, base); | 225 | val = simple_strtoull(cp, &tail, base); |
246 | if (tail == cp) | 226 | if (tail == cp) |
247 | return -EINVAL; | 227 | return -EINVAL; |
248 | if ((*tail == '\0') || | 228 | if ((tail[0] == '\0') || (tail[0] == '\n' && tail[1] == '\0')) { |
249 | ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { | ||
250 | *res = val; | 229 | *res = val; |
251 | return 0; | 230 | return 0; |
252 | } | 231 | } |
@@ -282,12 +261,14 @@ int strict_strtoll(const char *cp, unsigned int base, long long *res) | |||
282 | } | 261 | } |
283 | EXPORT_SYMBOL(strict_strtoll); | 262 | EXPORT_SYMBOL(strict_strtoll); |
284 | 263 | ||
285 | static int skip_atoi(const char **s) | 264 | static noinline_for_stack |
265 | int skip_atoi(const char **s) | ||
286 | { | 266 | { |
287 | int i=0; | 267 | int i = 0; |
288 | 268 | ||
289 | while (isdigit(**s)) | 269 | while (isdigit(**s)) |
290 | i = i*10 + *((*s)++) - '0'; | 270 | i = i*10 + *((*s)++) - '0'; |
271 | |||
291 | return i; | 272 | return i; |
292 | } | 273 | } |
293 | 274 | ||
@@ -301,7 +282,8 @@ static int skip_atoi(const char **s) | |||
301 | /* Formats correctly any integer in [0,99999]. | 282 | /* Formats correctly any integer in [0,99999]. |
302 | * Outputs from one to five digits depending on input. | 283 | * Outputs from one to five digits depending on input. |
303 | * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ | 284 | * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ |
304 | static char* put_dec_trunc(char *buf, unsigned q) | 285 | static noinline_for_stack |
286 | char *put_dec_trunc(char *buf, unsigned q) | ||
305 | { | 287 | { |
306 | unsigned d3, d2, d1, d0; | 288 | unsigned d3, d2, d1, d0; |
307 | d1 = (q>>4) & 0xf; | 289 | d1 = (q>>4) & 0xf; |
@@ -330,14 +312,16 @@ static char* put_dec_trunc(char *buf, unsigned q) | |||
330 | d3 = d3 - 10*q; | 312 | d3 = d3 - 10*q; |
331 | *buf++ = d3 + '0'; /* next digit */ | 313 | *buf++ = d3 + '0'; /* next digit */ |
332 | if (q != 0) | 314 | if (q != 0) |
333 | *buf++ = q + '0'; /* most sign. digit */ | 315 | *buf++ = q + '0'; /* most sign. digit */ |
334 | } | 316 | } |
335 | } | 317 | } |
336 | } | 318 | } |
319 | |||
337 | return buf; | 320 | return buf; |
338 | } | 321 | } |
339 | /* Same with if's removed. Always emits five digits */ | 322 | /* Same with if's removed. Always emits five digits */ |
340 | static char* put_dec_full(char *buf, unsigned q) | 323 | static noinline_for_stack |
324 | char *put_dec_full(char *buf, unsigned q) | ||
341 | { | 325 | { |
342 | /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ | 326 | /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ |
343 | /* but anyway, gcc produces better code with full-sized ints */ | 327 | /* but anyway, gcc produces better code with full-sized ints */ |
@@ -346,14 +330,15 @@ static char* put_dec_full(char *buf, unsigned q) | |||
346 | d2 = (q>>8) & 0xf; | 330 | d2 = (q>>8) & 0xf; |
347 | d3 = (q>>12); | 331 | d3 = (q>>12); |
348 | 332 | ||
349 | /* Possible ways to approx. divide by 10 */ | 333 | /* |
350 | /* gcc -O2 replaces multiply with shifts and adds */ | 334 | * Possible ways to approx. divide by 10 |
351 | // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) | 335 | * gcc -O2 replaces multiply with shifts and adds |
352 | // (x * 0x67) >> 10: 1100111 | 336 | * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) |
353 | // (x * 0x34) >> 9: 110100 - same | 337 | * (x * 0x67) >> 10: 1100111 |
354 | // (x * 0x1a) >> 8: 11010 - same | 338 | * (x * 0x34) >> 9: 110100 - same |
355 | // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) | 339 | * (x * 0x1a) >> 8: 11010 - same |
356 | 340 | * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) | |
341 | */ | ||
357 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); | 342 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
358 | q = (d0 * 0xcd) >> 11; | 343 | q = (d0 * 0xcd) >> 11; |
359 | d0 = d0 - 10*q; | 344 | d0 = d0 - 10*q; |
@@ -374,10 +359,12 @@ static char* put_dec_full(char *buf, unsigned q) | |||
374 | d3 = d3 - 10*q; | 359 | d3 = d3 - 10*q; |
375 | *buf++ = d3 + '0'; | 360 | *buf++ = d3 + '0'; |
376 | *buf++ = q + '0'; | 361 | *buf++ = q + '0'; |
362 | |||
377 | return buf; | 363 | return buf; |
378 | } | 364 | } |
379 | /* No inlining helps gcc to use registers better */ | 365 | /* No inlining helps gcc to use registers better */ |
380 | static noinline char* put_dec(char *buf, unsigned long long num) | 366 | static noinline_for_stack |
367 | char *put_dec(char *buf, unsigned long long num) | ||
381 | { | 368 | { |
382 | while (1) { | 369 | while (1) { |
383 | unsigned rem; | 370 | unsigned rem; |
@@ -393,8 +380,8 @@ static noinline char* put_dec(char *buf, unsigned long long num) | |||
393 | #define PLUS 4 /* show plus */ | 380 | #define PLUS 4 /* show plus */ |
394 | #define SPACE 8 /* space if plus */ | 381 | #define SPACE 8 /* space if plus */ |
395 | #define LEFT 16 /* left justified */ | 382 | #define LEFT 16 /* left justified */ |
396 | #define SMALL 32 /* Must be 32 == 0x20 */ | 383 | #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ |
397 | #define SPECIAL 64 /* 0x */ | 384 | #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ |
398 | 385 | ||
399 | enum format_type { | 386 | enum format_type { |
400 | FORMAT_TYPE_NONE, /* Just a string part */ | 387 | FORMAT_TYPE_NONE, /* Just a string part */ |
@@ -420,16 +407,17 @@ enum format_type { | |||
420 | }; | 407 | }; |
421 | 408 | ||
422 | struct printf_spec { | 409 | struct printf_spec { |
423 | enum format_type type; | 410 | u8 type; /* format_type enum */ |
424 | int flags; /* flags to number() */ | 411 | u8 flags; /* flags to number() */ |
425 | int field_width; /* width of output field */ | 412 | u8 base; /* number base, 8, 10 or 16 only */ |
426 | int base; | 413 | u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ |
427 | int precision; /* # of digits/chars */ | 414 | s16 field_width; /* width of output field */ |
428 | int qualifier; | 415 | s16 precision; /* # of digits/chars */ |
429 | }; | 416 | }; |
430 | 417 | ||
431 | static char *number(char *buf, char *end, unsigned long long num, | 418 | static noinline_for_stack |
432 | struct printf_spec spec) | 419 | char *number(char *buf, char *end, unsigned long long num, |
420 | struct printf_spec spec) | ||
433 | { | 421 | { |
434 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ | 422 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ |
435 | static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ | 423 | static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ |
@@ -447,9 +435,9 @@ static char *number(char *buf, char *end, unsigned long long num, | |||
447 | spec.flags &= ~ZEROPAD; | 435 | spec.flags &= ~ZEROPAD; |
448 | sign = 0; | 436 | sign = 0; |
449 | if (spec.flags & SIGN) { | 437 | if (spec.flags & SIGN) { |
450 | if ((signed long long) num < 0) { | 438 | if ((signed long long)num < 0) { |
451 | sign = '-'; | 439 | sign = '-'; |
452 | num = - (signed long long) num; | 440 | num = -(signed long long)num; |
453 | spec.field_width--; | 441 | spec.field_width--; |
454 | } else if (spec.flags & PLUS) { | 442 | } else if (spec.flags & PLUS) { |
455 | sign = '+'; | 443 | sign = '+'; |
@@ -477,7 +465,9 @@ static char *number(char *buf, char *end, unsigned long long num, | |||
477 | else if (spec.base != 10) { /* 8 or 16 */ | 465 | else if (spec.base != 10) { /* 8 or 16 */ |
478 | int mask = spec.base - 1; | 466 | int mask = spec.base - 1; |
479 | int shift = 3; | 467 | int shift = 3; |
480 | if (spec.base == 16) shift = 4; | 468 | |
469 | if (spec.base == 16) | ||
470 | shift = 4; | ||
481 | do { | 471 | do { |
482 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); | 472 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); |
483 | num >>= shift; | 473 | num >>= shift; |
@@ -492,7 +482,7 @@ static char *number(char *buf, char *end, unsigned long long num, | |||
492 | /* leading space padding */ | 482 | /* leading space padding */ |
493 | spec.field_width -= spec.precision; | 483 | spec.field_width -= spec.precision; |
494 | if (!(spec.flags & (ZEROPAD+LEFT))) { | 484 | if (!(spec.flags & (ZEROPAD+LEFT))) { |
495 | while(--spec.field_width >= 0) { | 485 | while (--spec.field_width >= 0) { |
496 | if (buf < end) | 486 | if (buf < end) |
497 | *buf = ' '; | 487 | *buf = ' '; |
498 | ++buf; | 488 | ++buf; |
@@ -542,15 +532,17 @@ static char *number(char *buf, char *end, unsigned long long num, | |||
542 | *buf = ' '; | 532 | *buf = ' '; |
543 | ++buf; | 533 | ++buf; |
544 | } | 534 | } |
535 | |||
545 | return buf; | 536 | return buf; |
546 | } | 537 | } |
547 | 538 | ||
548 | static char *string(char *buf, char *end, char *s, struct printf_spec spec) | 539 | static noinline_for_stack |
540 | char *string(char *buf, char *end, const char *s, struct printf_spec spec) | ||
549 | { | 541 | { |
550 | int len, i; | 542 | int len, i; |
551 | 543 | ||
552 | if ((unsigned long)s < PAGE_SIZE) | 544 | if ((unsigned long)s < PAGE_SIZE) |
553 | s = "<NULL>"; | 545 | s = "(null)"; |
554 | 546 | ||
555 | len = strnlen(s, spec.precision); | 547 | len = strnlen(s, spec.precision); |
556 | 548 | ||
@@ -571,123 +563,379 @@ static char *string(char *buf, char *end, char *s, struct printf_spec spec) | |||
571 | *buf = ' '; | 563 | *buf = ' '; |
572 | ++buf; | 564 | ++buf; |
573 | } | 565 | } |
566 | |||
574 | return buf; | 567 | return buf; |
575 | } | 568 | } |
576 | 569 | ||
577 | static char *symbol_string(char *buf, char *end, void *ptr, | 570 | static noinline_for_stack |
578 | struct printf_spec spec, char ext) | 571 | char *symbol_string(char *buf, char *end, void *ptr, |
572 | struct printf_spec spec, char ext) | ||
579 | { | 573 | { |
580 | unsigned long value = (unsigned long) ptr; | 574 | unsigned long value = (unsigned long) ptr; |
581 | #ifdef CONFIG_KALLSYMS | 575 | #ifdef CONFIG_KALLSYMS |
582 | char sym[KSYM_SYMBOL_LEN]; | 576 | char sym[KSYM_SYMBOL_LEN]; |
583 | if (ext != 'f') | 577 | if (ext != 'f' && ext != 's') |
584 | sprint_symbol(sym, value); | 578 | sprint_symbol(sym, value); |
585 | else | 579 | else |
586 | kallsyms_lookup(value, NULL, NULL, NULL, sym); | 580 | kallsyms_lookup(value, NULL, NULL, NULL, sym); |
581 | |||
587 | return string(buf, end, sym, spec); | 582 | return string(buf, end, sym, spec); |
588 | #else | 583 | #else |
589 | spec.field_width = 2*sizeof(void *); | 584 | spec.field_width = 2 * sizeof(void *); |
590 | spec.flags |= SPECIAL | SMALL | ZEROPAD; | 585 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
591 | spec.base = 16; | 586 | spec.base = 16; |
587 | |||
592 | return number(buf, end, value, spec); | 588 | return number(buf, end, value, spec); |
593 | #endif | 589 | #endif |
594 | } | 590 | } |
595 | 591 | ||
596 | static char *resource_string(char *buf, char *end, struct resource *res, | 592 | static noinline_for_stack |
597 | struct printf_spec spec) | 593 | char *resource_string(char *buf, char *end, struct resource *res, |
594 | struct printf_spec spec, const char *fmt) | ||
598 | { | 595 | { |
599 | #ifndef IO_RSRC_PRINTK_SIZE | 596 | #ifndef IO_RSRC_PRINTK_SIZE |
600 | #define IO_RSRC_PRINTK_SIZE 4 | 597 | #define IO_RSRC_PRINTK_SIZE 6 |
601 | #endif | 598 | #endif |
602 | 599 | ||
603 | #ifndef MEM_RSRC_PRINTK_SIZE | 600 | #ifndef MEM_RSRC_PRINTK_SIZE |
604 | #define MEM_RSRC_PRINTK_SIZE 8 | 601 | #define MEM_RSRC_PRINTK_SIZE 10 |
605 | #endif | 602 | #endif |
606 | struct printf_spec num_spec = { | 603 | static const struct printf_spec io_spec = { |
607 | .base = 16, | 604 | .base = 16, |
605 | .field_width = IO_RSRC_PRINTK_SIZE, | ||
608 | .precision = -1, | 606 | .precision = -1, |
609 | .flags = SPECIAL | SMALL | ZEROPAD, | 607 | .flags = SPECIAL | SMALL | ZEROPAD, |
610 | }; | 608 | }; |
611 | /* room for the actual numbers, the two "0x", -, [, ] and the final zero */ | 609 | static const struct printf_spec mem_spec = { |
612 | char sym[4*sizeof(resource_size_t) + 8]; | 610 | .base = 16, |
613 | char *p = sym, *pend = sym + sizeof(sym); | 611 | .field_width = MEM_RSRC_PRINTK_SIZE, |
614 | int size = -1; | 612 | .precision = -1, |
613 | .flags = SPECIAL | SMALL | ZEROPAD, | ||
614 | }; | ||
615 | static const struct printf_spec bus_spec = { | ||
616 | .base = 16, | ||
617 | .field_width = 2, | ||
618 | .precision = -1, | ||
619 | .flags = SMALL | ZEROPAD, | ||
620 | }; | ||
621 | static const struct printf_spec dec_spec = { | ||
622 | .base = 10, | ||
623 | .precision = -1, | ||
624 | .flags = 0, | ||
625 | }; | ||
626 | static const struct printf_spec str_spec = { | ||
627 | .field_width = -1, | ||
628 | .precision = 10, | ||
629 | .flags = LEFT, | ||
630 | }; | ||
631 | static const struct printf_spec flag_spec = { | ||
632 | .base = 16, | ||
633 | .precision = -1, | ||
634 | .flags = SPECIAL | SMALL, | ||
635 | }; | ||
615 | 636 | ||
616 | if (res->flags & IORESOURCE_IO) | 637 | /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) |
617 | size = IO_RSRC_PRINTK_SIZE; | 638 | * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ |
618 | else if (res->flags & IORESOURCE_MEM) | 639 | #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) |
619 | size = MEM_RSRC_PRINTK_SIZE; | 640 | #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) |
641 | #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") | ||
642 | #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") | ||
643 | char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, | ||
644 | 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; | ||
645 | |||
646 | char *p = sym, *pend = sym + sizeof(sym); | ||
647 | int decode = (fmt[0] == 'R') ? 1 : 0; | ||
648 | const struct printf_spec *specp; | ||
620 | 649 | ||
621 | *p++ = '['; | 650 | *p++ = '['; |
622 | num_spec.field_width = size; | 651 | if (res->flags & IORESOURCE_IO) { |
623 | p = number(p, pend, res->start, num_spec); | 652 | p = string(p, pend, "io ", str_spec); |
624 | *p++ = '-'; | 653 | specp = &io_spec; |
625 | p = number(p, pend, res->end, num_spec); | 654 | } else if (res->flags & IORESOURCE_MEM) { |
655 | p = string(p, pend, "mem ", str_spec); | ||
656 | specp = &mem_spec; | ||
657 | } else if (res->flags & IORESOURCE_IRQ) { | ||
658 | p = string(p, pend, "irq ", str_spec); | ||
659 | specp = &dec_spec; | ||
660 | } else if (res->flags & IORESOURCE_DMA) { | ||
661 | p = string(p, pend, "dma ", str_spec); | ||
662 | specp = &dec_spec; | ||
663 | } else if (res->flags & IORESOURCE_BUS) { | ||
664 | p = string(p, pend, "bus ", str_spec); | ||
665 | specp = &bus_spec; | ||
666 | } else { | ||
667 | p = string(p, pend, "??? ", str_spec); | ||
668 | specp = &mem_spec; | ||
669 | decode = 0; | ||
670 | } | ||
671 | p = number(p, pend, res->start, *specp); | ||
672 | if (res->start != res->end) { | ||
673 | *p++ = '-'; | ||
674 | p = number(p, pend, res->end, *specp); | ||
675 | } | ||
676 | if (decode) { | ||
677 | if (res->flags & IORESOURCE_MEM_64) | ||
678 | p = string(p, pend, " 64bit", str_spec); | ||
679 | if (res->flags & IORESOURCE_PREFETCH) | ||
680 | p = string(p, pend, " pref", str_spec); | ||
681 | if (res->flags & IORESOURCE_WINDOW) | ||
682 | p = string(p, pend, " window", str_spec); | ||
683 | if (res->flags & IORESOURCE_DISABLED) | ||
684 | p = string(p, pend, " disabled", str_spec); | ||
685 | } else { | ||
686 | p = string(p, pend, " flags ", str_spec); | ||
687 | p = number(p, pend, res->flags, flag_spec); | ||
688 | } | ||
626 | *p++ = ']'; | 689 | *p++ = ']'; |
627 | *p = 0; | 690 | *p = '\0'; |
628 | 691 | ||
629 | return string(buf, end, sym, spec); | 692 | return string(buf, end, sym, spec); |
630 | } | 693 | } |
631 | 694 | ||
632 | static char *mac_address_string(char *buf, char *end, u8 *addr, | 695 | static noinline_for_stack |
633 | struct printf_spec spec) | 696 | char *mac_address_string(char *buf, char *end, u8 *addr, |
697 | struct printf_spec spec, const char *fmt) | ||
634 | { | 698 | { |
635 | char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */ | 699 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
636 | char *p = mac_addr; | 700 | char *p = mac_addr; |
637 | int i; | 701 | int i; |
702 | char separator; | ||
703 | |||
704 | if (fmt[1] == 'F') { /* FDDI canonical format */ | ||
705 | separator = '-'; | ||
706 | } else { | ||
707 | separator = ':'; | ||
708 | } | ||
638 | 709 | ||
639 | for (i = 0; i < 6; i++) { | 710 | for (i = 0; i < 6; i++) { |
640 | p = pack_hex_byte(p, addr[i]); | 711 | p = pack_hex_byte(p, addr[i]); |
641 | if (!(spec.flags & SPECIAL) && i != 5) | 712 | if (fmt[0] == 'M' && i != 5) |
642 | *p++ = ':'; | 713 | *p++ = separator; |
643 | } | 714 | } |
644 | *p = '\0'; | 715 | *p = '\0'; |
645 | spec.flags &= ~SPECIAL; | ||
646 | 716 | ||
647 | return string(buf, end, mac_addr, spec); | 717 | return string(buf, end, mac_addr, spec); |
648 | } | 718 | } |
649 | 719 | ||
650 | static char *ip6_addr_string(char *buf, char *end, u8 *addr, | 720 | static noinline_for_stack |
651 | struct printf_spec spec) | 721 | char *ip4_string(char *p, const u8 *addr, const char *fmt) |
652 | { | 722 | { |
653 | char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */ | ||
654 | char *p = ip6_addr; | ||
655 | int i; | 723 | int i; |
724 | bool leading_zeros = (fmt[0] == 'i'); | ||
725 | int index; | ||
726 | int step; | ||
727 | |||
728 | switch (fmt[2]) { | ||
729 | case 'h': | ||
730 | #ifdef __BIG_ENDIAN | ||
731 | index = 0; | ||
732 | step = 1; | ||
733 | #else | ||
734 | index = 3; | ||
735 | step = -1; | ||
736 | #endif | ||
737 | break; | ||
738 | case 'l': | ||
739 | index = 3; | ||
740 | step = -1; | ||
741 | break; | ||
742 | case 'n': | ||
743 | case 'b': | ||
744 | default: | ||
745 | index = 0; | ||
746 | step = 1; | ||
747 | break; | ||
748 | } | ||
749 | for (i = 0; i < 4; i++) { | ||
750 | char temp[3]; /* hold each IP quad in reverse order */ | ||
751 | int digits = put_dec_trunc(temp, addr[index]) - temp; | ||
752 | if (leading_zeros) { | ||
753 | if (digits < 3) | ||
754 | *p++ = '0'; | ||
755 | if (digits < 2) | ||
756 | *p++ = '0'; | ||
757 | } | ||
758 | /* reverse the digits in the quad */ | ||
759 | while (digits--) | ||
760 | *p++ = temp[digits]; | ||
761 | if (i < 3) | ||
762 | *p++ = '.'; | ||
763 | index += step; | ||
764 | } | ||
765 | *p = '\0'; | ||
656 | 766 | ||
657 | for (i = 0; i < 8; i++) { | 767 | return p; |
658 | p = pack_hex_byte(p, addr[2 * i]); | 768 | } |
659 | p = pack_hex_byte(p, addr[2 * i + 1]); | 769 | |
660 | if (!(spec.flags & SPECIAL) && i != 7) | 770 | static noinline_for_stack |
771 | char *ip6_compressed_string(char *p, const char *addr) | ||
772 | { | ||
773 | int i, j, range; | ||
774 | unsigned char zerolength[8]; | ||
775 | int longest = 1; | ||
776 | int colonpos = -1; | ||
777 | u16 word; | ||
778 | u8 hi, lo; | ||
779 | bool needcolon = false; | ||
780 | bool useIPv4; | ||
781 | struct in6_addr in6; | ||
782 | |||
783 | memcpy(&in6, addr, sizeof(struct in6_addr)); | ||
784 | |||
785 | useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); | ||
786 | |||
787 | memset(zerolength, 0, sizeof(zerolength)); | ||
788 | |||
789 | if (useIPv4) | ||
790 | range = 6; | ||
791 | else | ||
792 | range = 8; | ||
793 | |||
794 | /* find position of longest 0 run */ | ||
795 | for (i = 0; i < range; i++) { | ||
796 | for (j = i; j < range; j++) { | ||
797 | if (in6.s6_addr16[j] != 0) | ||
798 | break; | ||
799 | zerolength[i]++; | ||
800 | } | ||
801 | } | ||
802 | for (i = 0; i < range; i++) { | ||
803 | if (zerolength[i] > longest) { | ||
804 | longest = zerolength[i]; | ||
805 | colonpos = i; | ||
806 | } | ||
807 | } | ||
808 | |||
809 | /* emit address */ | ||
810 | for (i = 0; i < range; i++) { | ||
811 | if (i == colonpos) { | ||
812 | if (needcolon || i == 0) | ||
813 | *p++ = ':'; | ||
661 | *p++ = ':'; | 814 | *p++ = ':'; |
815 | needcolon = false; | ||
816 | i += longest - 1; | ||
817 | continue; | ||
818 | } | ||
819 | if (needcolon) { | ||
820 | *p++ = ':'; | ||
821 | needcolon = false; | ||
822 | } | ||
823 | /* hex u16 without leading 0s */ | ||
824 | word = ntohs(in6.s6_addr16[i]); | ||
825 | hi = word >> 8; | ||
826 | lo = word & 0xff; | ||
827 | if (hi) { | ||
828 | if (hi > 0x0f) | ||
829 | p = pack_hex_byte(p, hi); | ||
830 | else | ||
831 | *p++ = hex_asc_lo(hi); | ||
832 | p = pack_hex_byte(p, lo); | ||
833 | } | ||
834 | else if (lo > 0x0f) | ||
835 | p = pack_hex_byte(p, lo); | ||
836 | else | ||
837 | *p++ = hex_asc_lo(lo); | ||
838 | needcolon = true; | ||
839 | } | ||
840 | |||
841 | if (useIPv4) { | ||
842 | if (needcolon) | ||
843 | *p++ = ':'; | ||
844 | p = ip4_string(p, &in6.s6_addr[12], "I4"); | ||
662 | } | 845 | } |
663 | *p = '\0'; | 846 | *p = '\0'; |
664 | spec.flags &= ~SPECIAL; | ||
665 | 847 | ||
666 | return string(buf, end, ip6_addr, spec); | 848 | return p; |
667 | } | 849 | } |
668 | 850 | ||
669 | static char *ip4_addr_string(char *buf, char *end, u8 *addr, | 851 | static noinline_for_stack |
670 | struct printf_spec spec) | 852 | char *ip6_string(char *p, const char *addr, const char *fmt) |
671 | { | 853 | { |
672 | char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */ | 854 | int i; |
673 | char temp[3]; /* hold each IP quad in reverse order */ | ||
674 | char *p = ip4_addr; | ||
675 | int i, digits; | ||
676 | 855 | ||
677 | for (i = 0; i < 4; i++) { | 856 | for (i = 0; i < 8; i++) { |
678 | digits = put_dec_trunc(temp, addr[i]) - temp; | 857 | p = pack_hex_byte(p, *addr++); |
679 | /* reverse the digits in the quad */ | 858 | p = pack_hex_byte(p, *addr++); |
680 | while (digits--) | 859 | if (fmt[0] == 'I' && i != 7) |
681 | *p++ = temp[digits]; | 860 | *p++ = ':'; |
682 | if (i != 3) | ||
683 | *p++ = '.'; | ||
684 | } | 861 | } |
685 | *p = '\0'; | 862 | *p = '\0'; |
686 | spec.flags &= ~SPECIAL; | 863 | |
864 | return p; | ||
865 | } | ||
866 | |||
867 | static noinline_for_stack | ||
868 | char *ip6_addr_string(char *buf, char *end, const u8 *addr, | ||
869 | struct printf_spec spec, const char *fmt) | ||
870 | { | ||
871 | char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; | ||
872 | |||
873 | if (fmt[0] == 'I' && fmt[2] == 'c') | ||
874 | ip6_compressed_string(ip6_addr, addr); | ||
875 | else | ||
876 | ip6_string(ip6_addr, addr, fmt); | ||
877 | |||
878 | return string(buf, end, ip6_addr, spec); | ||
879 | } | ||
880 | |||
881 | static noinline_for_stack | ||
882 | char *ip4_addr_string(char *buf, char *end, const u8 *addr, | ||
883 | struct printf_spec spec, const char *fmt) | ||
884 | { | ||
885 | char ip4_addr[sizeof("255.255.255.255")]; | ||
886 | |||
887 | ip4_string(ip4_addr, addr, fmt); | ||
687 | 888 | ||
688 | return string(buf, end, ip4_addr, spec); | 889 | return string(buf, end, ip4_addr, spec); |
689 | } | 890 | } |
690 | 891 | ||
892 | static noinline_for_stack | ||
893 | char *uuid_string(char *buf, char *end, const u8 *addr, | ||
894 | struct printf_spec spec, const char *fmt) | ||
895 | { | ||
896 | char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; | ||
897 | char *p = uuid; | ||
898 | int i; | ||
899 | static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; | ||
900 | static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; | ||
901 | const u8 *index = be; | ||
902 | bool uc = false; | ||
903 | |||
904 | switch (*(++fmt)) { | ||
905 | case 'L': | ||
906 | uc = true; /* fall-through */ | ||
907 | case 'l': | ||
908 | index = le; | ||
909 | break; | ||
910 | case 'B': | ||
911 | uc = true; | ||
912 | break; | ||
913 | } | ||
914 | |||
915 | for (i = 0; i < 16; i++) { | ||
916 | p = pack_hex_byte(p, addr[index[i]]); | ||
917 | switch (i) { | ||
918 | case 3: | ||
919 | case 5: | ||
920 | case 7: | ||
921 | case 9: | ||
922 | *p++ = '-'; | ||
923 | break; | ||
924 | } | ||
925 | } | ||
926 | |||
927 | *p = 0; | ||
928 | |||
929 | if (uc) { | ||
930 | p = uuid; | ||
931 | do { | ||
932 | *p = toupper(*p); | ||
933 | } while (*(++p)); | ||
934 | } | ||
935 | |||
936 | return string(buf, end, uuid, spec); | ||
937 | } | ||
938 | |||
691 | /* | 939 | /* |
692 | * Show a '%p' thing. A kernel extension is that the '%p' is followed | 940 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
693 | * by an extra set of alphanumeric characters that are extended format | 941 | * by an extra set of alphanumeric characters that are extended format |
@@ -697,25 +945,58 @@ static char *ip4_addr_string(char *buf, char *end, u8 *addr, | |||
697 | * | 945 | * |
698 | * - 'F' For symbolic function descriptor pointers with offset | 946 | * - 'F' For symbolic function descriptor pointers with offset |
699 | * - 'f' For simple symbolic function names without offset | 947 | * - 'f' For simple symbolic function names without offset |
700 | * - 'S' For symbolic direct pointers | 948 | * - 'S' For symbolic direct pointers with offset |
701 | * - 'R' For a struct resource pointer, it prints the range of | 949 | * - 's' For symbolic direct pointers without offset |
702 | * addresses (not the name nor the flags) | 950 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
951 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] | ||
703 | * - 'M' For a 6-byte MAC address, it prints the address in the | 952 | * - 'M' For a 6-byte MAC address, it prints the address in the |
704 | * usual colon-separated hex notation | 953 | * usual colon-separated hex notation |
705 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated | 954 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
706 | * decimal for v4 and colon separated network-order 16 bit hex for v6) | 955 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
707 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is | 956 | * with a dash-separated hex notation |
708 | * currently the same | 957 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
958 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) | ||
959 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's | ||
960 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses | ||
961 | * IPv6 omits the colons (01020304...0f) | ||
962 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) | ||
963 | * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order | ||
964 | * - 'I6c' for IPv6 addresses printed as specified by | ||
965 | * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 | ||
966 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form | ||
967 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
968 | * Options for %pU are: | ||
969 | * b big endian lower case hex (default) | ||
970 | * B big endian UPPER case hex | ||
971 | * l little endian lower case hex | ||
972 | * L little endian UPPER case hex | ||
973 | * big endian output byte order is: | ||
974 | * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] | ||
975 | * little endian output byte order is: | ||
976 | * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] | ||
977 | * - 'V' For a struct va_format which contains a format string * and va_list *, | ||
978 | * call vsnprintf(->format, *->va_list). | ||
979 | * Implements a "recursive vsnprintf". | ||
980 | * Do not use this feature without some mechanism to verify the | ||
981 | * correctness of the format string and va_list arguments. | ||
709 | * | 982 | * |
710 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 | 983 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
711 | * function pointers are really function descriptors, which contain a | 984 | * function pointers are really function descriptors, which contain a |
712 | * pointer to the real address. | 985 | * pointer to the real address. |
713 | */ | 986 | */ |
714 | static char *pointer(const char *fmt, char *buf, char *end, void *ptr, | 987 | static noinline_for_stack |
715 | struct printf_spec spec) | 988 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
989 | struct printf_spec spec) | ||
716 | { | 990 | { |
717 | if (!ptr) | 991 | if (!ptr) { |
992 | /* | ||
993 | * Print (null) with the same width as a pointer so it makes | ||
994 | * tabular output look nice. | ||
995 | */ | ||
996 | if (spec.field_width == -1) | ||
997 | spec.field_width = 2 * sizeof(void *); | ||
718 | return string(buf, end, "(null)", spec); | 998 | return string(buf, end, "(null)", spec); |
999 | } | ||
719 | 1000 | ||
720 | switch (*fmt) { | 1001 | switch (*fmt) { |
721 | case 'F': | 1002 | case 'F': |
@@ -723,28 +1004,41 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr, | |||
723 | ptr = dereference_function_descriptor(ptr); | 1004 | ptr = dereference_function_descriptor(ptr); |
724 | /* Fallthrough */ | 1005 | /* Fallthrough */ |
725 | case 'S': | 1006 | case 'S': |
1007 | case 's': | ||
726 | return symbol_string(buf, end, ptr, spec, *fmt); | 1008 | return symbol_string(buf, end, ptr, spec, *fmt); |
727 | case 'R': | 1009 | case 'R': |
728 | return resource_string(buf, end, ptr, spec); | 1010 | case 'r': |
729 | case 'm': | 1011 | return resource_string(buf, end, ptr, spec, fmt); |
730 | spec.flags |= SPECIAL; | 1012 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
731 | /* Fallthrough */ | 1013 | case 'm': /* Contiguous: 000102030405 */ |
732 | case 'M': | 1014 | /* [mM]F (FDDI, bit reversed) */ |
733 | return mac_address_string(buf, end, ptr, spec); | 1015 | return mac_address_string(buf, end, ptr, spec, fmt); |
734 | case 'i': | 1016 | case 'I': /* Formatted IP supported |
735 | spec.flags |= SPECIAL; | 1017 | * 4: 1.2.3.4 |
736 | /* Fallthrough */ | 1018 | * 6: 0001:0203:...:0708 |
737 | case 'I': | 1019 | * 6c: 1::708 or 1::1.2.3.4 |
738 | if (fmt[1] == '6') | 1020 | */ |
739 | return ip6_addr_string(buf, end, ptr, spec); | 1021 | case 'i': /* Contiguous: |
740 | if (fmt[1] == '4') | 1022 | * 4: 001.002.003.004 |
741 | return ip4_addr_string(buf, end, ptr, spec); | 1023 | * 6: 000102...0f |
742 | spec.flags &= ~SPECIAL; | 1024 | */ |
1025 | switch (fmt[1]) { | ||
1026 | case '6': | ||
1027 | return ip6_addr_string(buf, end, ptr, spec, fmt); | ||
1028 | case '4': | ||
1029 | return ip4_addr_string(buf, end, ptr, spec, fmt); | ||
1030 | } | ||
743 | break; | 1031 | break; |
1032 | case 'U': | ||
1033 | return uuid_string(buf, end, ptr, spec, fmt); | ||
1034 | case 'V': | ||
1035 | return buf + vsnprintf(buf, end - buf, | ||
1036 | ((struct va_format *)ptr)->fmt, | ||
1037 | *(((struct va_format *)ptr)->va)); | ||
744 | } | 1038 | } |
745 | spec.flags |= SMALL; | 1039 | spec.flags |= SMALL; |
746 | if (spec.field_width == -1) { | 1040 | if (spec.field_width == -1) { |
747 | spec.field_width = 2*sizeof(void *); | 1041 | spec.field_width = 2 * sizeof(void *); |
748 | spec.flags |= ZEROPAD; | 1042 | spec.flags |= ZEROPAD; |
749 | } | 1043 | } |
750 | spec.base = 16; | 1044 | spec.base = 16; |
@@ -772,7 +1066,8 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr, | |||
772 | * @precision: precision of a number | 1066 | * @precision: precision of a number |
773 | * @qualifier: qualifier of a number (long, size_t, ...) | 1067 | * @qualifier: qualifier of a number (long, size_t, ...) |
774 | */ | 1068 | */ |
775 | static int format_decode(const char *fmt, struct printf_spec *spec) | 1069 | static noinline_for_stack |
1070 | int format_decode(const char *fmt, struct printf_spec *spec) | ||
776 | { | 1071 | { |
777 | const char *start = fmt; | 1072 | const char *start = fmt; |
778 | 1073 | ||
@@ -858,8 +1153,8 @@ precision: | |||
858 | qualifier: | 1153 | qualifier: |
859 | /* get the conversion qualifier */ | 1154 | /* get the conversion qualifier */ |
860 | spec->qualifier = -1; | 1155 | spec->qualifier = -1; |
861 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || | 1156 | if (*fmt == 'h' || TOLOWER(*fmt) == 'l' || |
862 | *fmt == 'Z' || *fmt == 'z' || *fmt == 't') { | 1157 | TOLOWER(*fmt) == 'z' || *fmt == 't') { |
863 | spec->qualifier = *fmt++; | 1158 | spec->qualifier = *fmt++; |
864 | if (unlikely(spec->qualifier == *fmt)) { | 1159 | if (unlikely(spec->qualifier == *fmt)) { |
865 | if (spec->qualifier == 'l') { | 1160 | if (spec->qualifier == 'l') { |
@@ -926,7 +1221,7 @@ qualifier: | |||
926 | spec->type = FORMAT_TYPE_LONG; | 1221 | spec->type = FORMAT_TYPE_LONG; |
927 | else | 1222 | else |
928 | spec->type = FORMAT_TYPE_ULONG; | 1223 | spec->type = FORMAT_TYPE_ULONG; |
929 | } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') { | 1224 | } else if (TOLOWER(spec->qualifier) == 'z') { |
930 | spec->type = FORMAT_TYPE_SIZE_T; | 1225 | spec->type = FORMAT_TYPE_SIZE_T; |
931 | } else if (spec->qualifier == 't') { | 1226 | } else if (spec->qualifier == 't') { |
932 | spec->type = FORMAT_TYPE_PTRDIFF; | 1227 | spec->type = FORMAT_TYPE_PTRDIFF; |
@@ -958,10 +1253,23 @@ qualifier: | |||
958 | * @args: Arguments for the format string | 1253 | * @args: Arguments for the format string |
959 | * | 1254 | * |
960 | * This function follows C99 vsnprintf, but has some extensions: | 1255 | * This function follows C99 vsnprintf, but has some extensions: |
961 | * %pS output the name of a text symbol | 1256 | * %pS output the name of a text symbol with offset |
1257 | * %ps output the name of a text symbol without offset | ||
962 | * %pF output the name of a function pointer with its offset | 1258 | * %pF output the name of a function pointer with its offset |
963 | * %pf output the name of a function pointer without its offset | 1259 | * %pf output the name of a function pointer without its offset |
964 | * %pR output the address range in a struct resource | 1260 | * %pR output the address range in a struct resource with decoded flags |
1261 | * %pr output the address range in a struct resource with raw flags | ||
1262 | * %pM output a 6-byte MAC address with colons | ||
1263 | * %pm output a 6-byte MAC address without colons | ||
1264 | * %pI4 print an IPv4 address without leading zeros | ||
1265 | * %pi4 print an IPv4 address with leading zeros | ||
1266 | * %pI6 print an IPv6 address with colons | ||
1267 | * %pi6 print an IPv6 address without colons | ||
1268 | * %pI6c print an IPv6 address as specified by | ||
1269 | * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 | ||
1270 | * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper | ||
1271 | * case. | ||
1272 | * %n is ignored | ||
965 | * | 1273 | * |
966 | * The return value is the number of characters which would | 1274 | * The return value is the number of characters which would |
967 | * be generated for the given input, excluding the trailing | 1275 | * be generated for the given input, excluding the trailing |
@@ -977,19 +1285,13 @@ qualifier: | |||
977 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | 1285 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
978 | { | 1286 | { |
979 | unsigned long long num; | 1287 | unsigned long long num; |
980 | char *str, *end, c; | 1288 | char *str, *end; |
981 | int read; | ||
982 | struct printf_spec spec = {0}; | 1289 | struct printf_spec spec = {0}; |
983 | 1290 | ||
984 | /* Reject out-of-range values early. Large positive sizes are | 1291 | /* Reject out-of-range values early. Large positive sizes are |
985 | used for unknown buffer sizes. */ | 1292 | used for unknown buffer sizes. */ |
986 | if (unlikely((int) size < 0)) { | 1293 | if (WARN_ON_ONCE((int) size < 0)) |
987 | /* There can be only one.. */ | ||
988 | static char warn = 1; | ||
989 | WARN_ON(warn); | ||
990 | warn = 0; | ||
991 | return 0; | 1294 | return 0; |
992 | } | ||
993 | 1295 | ||
994 | str = buf; | 1296 | str = buf; |
995 | end = buf + size; | 1297 | end = buf + size; |
@@ -1002,8 +1304,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | |||
1002 | 1304 | ||
1003 | while (*fmt) { | 1305 | while (*fmt) { |
1004 | const char *old_fmt = fmt; | 1306 | const char *old_fmt = fmt; |
1005 | 1307 | int read = format_decode(fmt, &spec); | |
1006 | read = format_decode(fmt, &spec); | ||
1007 | 1308 | ||
1008 | fmt += read; | 1309 | fmt += read; |
1009 | 1310 | ||
@@ -1027,7 +1328,9 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | |||
1027 | spec.precision = va_arg(args, int); | 1328 | spec.precision = va_arg(args, int); |
1028 | break; | 1329 | break; |
1029 | 1330 | ||
1030 | case FORMAT_TYPE_CHAR: | 1331 | case FORMAT_TYPE_CHAR: { |
1332 | char c; | ||
1333 | |||
1031 | if (!(spec.flags & LEFT)) { | 1334 | if (!(spec.flags & LEFT)) { |
1032 | while (--spec.field_width > 0) { | 1335 | while (--spec.field_width > 0) { |
1033 | if (str < end) | 1336 | if (str < end) |
@@ -1046,6 +1349,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | |||
1046 | ++str; | 1349 | ++str; |
1047 | } | 1350 | } |
1048 | break; | 1351 | break; |
1352 | } | ||
1049 | 1353 | ||
1050 | case FORMAT_TYPE_STR: | 1354 | case FORMAT_TYPE_STR: |
1051 | str = string(str, end, va_arg(args, char *), spec); | 1355 | str = string(str, end, va_arg(args, char *), spec); |
@@ -1071,13 +1375,12 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | |||
1071 | break; | 1375 | break; |
1072 | 1376 | ||
1073 | case FORMAT_TYPE_NRCHARS: { | 1377 | case FORMAT_TYPE_NRCHARS: { |
1074 | int qualifier = spec.qualifier; | 1378 | u8 qualifier = spec.qualifier; |
1075 | 1379 | ||
1076 | if (qualifier == 'l') { | 1380 | if (qualifier == 'l') { |
1077 | long *ip = va_arg(args, long *); | 1381 | long *ip = va_arg(args, long *); |
1078 | *ip = (str - buf); | 1382 | *ip = (str - buf); |
1079 | } else if (qualifier == 'Z' || | 1383 | } else if (TOLOWER(qualifier) == 'z') { |
1080 | qualifier == 'z') { | ||
1081 | size_t *ip = va_arg(args, size_t *); | 1384 | size_t *ip = va_arg(args, size_t *); |
1082 | *ip = (str - buf); | 1385 | *ip = (str - buf); |
1083 | } else { | 1386 | } else { |
@@ -1160,7 +1463,8 @@ int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) | |||
1160 | { | 1463 | { |
1161 | int i; | 1464 | int i; |
1162 | 1465 | ||
1163 | i=vsnprintf(buf,size,fmt,args); | 1466 | i = vsnprintf(buf, size, fmt, args); |
1467 | |||
1164 | return (i >= size) ? (size - 1) : i; | 1468 | return (i >= size) ? (size - 1) : i; |
1165 | } | 1469 | } |
1166 | EXPORT_SYMBOL(vscnprintf); | 1470 | EXPORT_SYMBOL(vscnprintf); |
@@ -1179,14 +1483,15 @@ EXPORT_SYMBOL(vscnprintf); | |||
1179 | * | 1483 | * |
1180 | * See the vsnprintf() documentation for format string extensions over C99. | 1484 | * See the vsnprintf() documentation for format string extensions over C99. |
1181 | */ | 1485 | */ |
1182 | int snprintf(char * buf, size_t size, const char *fmt, ...) | 1486 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
1183 | { | 1487 | { |
1184 | va_list args; | 1488 | va_list args; |
1185 | int i; | 1489 | int i; |
1186 | 1490 | ||
1187 | va_start(args, fmt); | 1491 | va_start(args, fmt); |
1188 | i=vsnprintf(buf,size,fmt,args); | 1492 | i = vsnprintf(buf, size, fmt, args); |
1189 | va_end(args); | 1493 | va_end(args); |
1494 | |||
1190 | return i; | 1495 | return i; |
1191 | } | 1496 | } |
1192 | EXPORT_SYMBOL(snprintf); | 1497 | EXPORT_SYMBOL(snprintf); |
@@ -1199,10 +1504,10 @@ EXPORT_SYMBOL(snprintf); | |||
1199 | * @...: Arguments for the format string | 1504 | * @...: Arguments for the format string |
1200 | * | 1505 | * |
1201 | * The return value is the number of characters written into @buf not including | 1506 | * The return value is the number of characters written into @buf not including |
1202 | * the trailing '\0'. If @size is <= 0 the function returns 0. | 1507 | * the trailing '\0'. If @size is == 0 the function returns 0. |
1203 | */ | 1508 | */ |
1204 | 1509 | ||
1205 | int scnprintf(char * buf, size_t size, const char *fmt, ...) | 1510 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
1206 | { | 1511 | { |
1207 | va_list args; | 1512 | va_list args; |
1208 | int i; | 1513 | int i; |
@@ -1210,7 +1515,12 @@ int scnprintf(char * buf, size_t size, const char *fmt, ...) | |||
1210 | va_start(args, fmt); | 1515 | va_start(args, fmt); |
1211 | i = vsnprintf(buf, size, fmt, args); | 1516 | i = vsnprintf(buf, size, fmt, args); |
1212 | va_end(args); | 1517 | va_end(args); |
1213 | return (i >= size) ? (size - 1) : i; | 1518 | |
1519 | if (likely(i < size)) | ||
1520 | return i; | ||
1521 | if (size != 0) | ||
1522 | return size - 1; | ||
1523 | return 0; | ||
1214 | } | 1524 | } |
1215 | EXPORT_SYMBOL(scnprintf); | 1525 | EXPORT_SYMBOL(scnprintf); |
1216 | 1526 | ||
@@ -1247,14 +1557,15 @@ EXPORT_SYMBOL(vsprintf); | |||
1247 | * | 1557 | * |
1248 | * See the vsnprintf() documentation for format string extensions over C99. | 1558 | * See the vsnprintf() documentation for format string extensions over C99. |
1249 | */ | 1559 | */ |
1250 | int sprintf(char * buf, const char *fmt, ...) | 1560 | int sprintf(char *buf, const char *fmt, ...) |
1251 | { | 1561 | { |
1252 | va_list args; | 1562 | va_list args; |
1253 | int i; | 1563 | int i; |
1254 | 1564 | ||
1255 | va_start(args, fmt); | 1565 | va_start(args, fmt); |
1256 | i=vsnprintf(buf, INT_MAX, fmt, args); | 1566 | i = vsnprintf(buf, INT_MAX, fmt, args); |
1257 | va_end(args); | 1567 | va_end(args); |
1568 | |||
1258 | return i; | 1569 | return i; |
1259 | } | 1570 | } |
1260 | EXPORT_SYMBOL(sprintf); | 1571 | EXPORT_SYMBOL(sprintf); |
@@ -1287,7 +1598,6 @@ int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) | |||
1287 | { | 1598 | { |
1288 | struct printf_spec spec = {0}; | 1599 | struct printf_spec spec = {0}; |
1289 | char *str, *end; | 1600 | char *str, *end; |
1290 | int read; | ||
1291 | 1601 | ||
1292 | str = (char *)bin_buf; | 1602 | str = (char *)bin_buf; |
1293 | end = (char *)(bin_buf + size); | 1603 | end = (char *)(bin_buf + size); |
@@ -1312,14 +1622,15 @@ do { \ | |||
1312 | str += sizeof(type); \ | 1622 | str += sizeof(type); \ |
1313 | } while (0) | 1623 | } while (0) |
1314 | 1624 | ||
1315 | |||
1316 | while (*fmt) { | 1625 | while (*fmt) { |
1317 | read = format_decode(fmt, &spec); | 1626 | int read = format_decode(fmt, &spec); |
1318 | 1627 | ||
1319 | fmt += read; | 1628 | fmt += read; |
1320 | 1629 | ||
1321 | switch (spec.type) { | 1630 | switch (spec.type) { |
1322 | case FORMAT_TYPE_NONE: | 1631 | case FORMAT_TYPE_NONE: |
1632 | case FORMAT_TYPE_INVALID: | ||
1633 | case FORMAT_TYPE_PERCENT_CHAR: | ||
1323 | break; | 1634 | break; |
1324 | 1635 | ||
1325 | case FORMAT_TYPE_WIDTH: | 1636 | case FORMAT_TYPE_WIDTH: |
@@ -1334,13 +1645,14 @@ do { \ | |||
1334 | case FORMAT_TYPE_STR: { | 1645 | case FORMAT_TYPE_STR: { |
1335 | const char *save_str = va_arg(args, char *); | 1646 | const char *save_str = va_arg(args, char *); |
1336 | size_t len; | 1647 | size_t len; |
1648 | |||
1337 | if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE | 1649 | if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE |
1338 | || (unsigned long)save_str < PAGE_SIZE) | 1650 | || (unsigned long)save_str < PAGE_SIZE) |
1339 | save_str = "<NULL>"; | 1651 | save_str = "(null)"; |
1340 | len = strlen(save_str); | 1652 | len = strlen(save_str) + 1; |
1341 | if (str + len + 1 < end) | 1653 | if (str + len < end) |
1342 | memcpy(str, save_str, len + 1); | 1654 | memcpy(str, save_str, len); |
1343 | str += len + 1; | 1655 | str += len; |
1344 | break; | 1656 | break; |
1345 | } | 1657 | } |
1346 | 1658 | ||
@@ -1351,19 +1663,13 @@ do { \ | |||
1351 | fmt++; | 1663 | fmt++; |
1352 | break; | 1664 | break; |
1353 | 1665 | ||
1354 | case FORMAT_TYPE_PERCENT_CHAR: | ||
1355 | break; | ||
1356 | |||
1357 | case FORMAT_TYPE_INVALID: | ||
1358 | break; | ||
1359 | |||
1360 | case FORMAT_TYPE_NRCHARS: { | 1666 | case FORMAT_TYPE_NRCHARS: { |
1361 | /* skip %n 's argument */ | 1667 | /* skip %n 's argument */ |
1362 | int qualifier = spec.qualifier; | 1668 | u8 qualifier = spec.qualifier; |
1363 | void *skip_arg; | 1669 | void *skip_arg; |
1364 | if (qualifier == 'l') | 1670 | if (qualifier == 'l') |
1365 | skip_arg = va_arg(args, long *); | 1671 | skip_arg = va_arg(args, long *); |
1366 | else if (qualifier == 'Z' || qualifier == 'z') | 1672 | else if (TOLOWER(qualifier) == 'z') |
1367 | skip_arg = va_arg(args, size_t *); | 1673 | skip_arg = va_arg(args, size_t *); |
1368 | else | 1674 | else |
1369 | skip_arg = va_arg(args, int *); | 1675 | skip_arg = va_arg(args, int *); |
@@ -1399,8 +1705,8 @@ do { \ | |||
1399 | } | 1705 | } |
1400 | } | 1706 | } |
1401 | } | 1707 | } |
1402 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; | ||
1403 | 1708 | ||
1709 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; | ||
1404 | #undef save_arg | 1710 | #undef save_arg |
1405 | } | 1711 | } |
1406 | EXPORT_SYMBOL_GPL(vbin_printf); | 1712 | EXPORT_SYMBOL_GPL(vbin_printf); |
@@ -1417,11 +1723,7 @@ EXPORT_SYMBOL_GPL(vbin_printf); | |||
1417 | * a binary buffer that generated by vbin_printf. | 1723 | * a binary buffer that generated by vbin_printf. |
1418 | * | 1724 | * |
1419 | * The format follows C99 vsnprintf, but has some extensions: | 1725 | * The format follows C99 vsnprintf, but has some extensions: |
1420 | * %pS output the name of a text symbol | 1726 | * see vsnprintf comment for details. |
1421 | * %pF output the name of a function pointer with its offset | ||
1422 | * %pf output the name of a function pointer without its offset | ||
1423 | * %pR output the address range in a struct resource | ||
1424 | * %n is ignored | ||
1425 | * | 1727 | * |
1426 | * The return value is the number of characters which would | 1728 | * The return value is the number of characters which would |
1427 | * be generated for the given input, excluding the trailing | 1729 | * be generated for the given input, excluding the trailing |
@@ -1433,19 +1735,12 @@ EXPORT_SYMBOL_GPL(vbin_printf); | |||
1433 | */ | 1735 | */ |
1434 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | 1736 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) |
1435 | { | 1737 | { |
1436 | unsigned long long num; | ||
1437 | char *str, *end, c; | ||
1438 | const char *args = (const char *)bin_buf; | ||
1439 | |||
1440 | struct printf_spec spec = {0}; | 1738 | struct printf_spec spec = {0}; |
1739 | char *str, *end; | ||
1740 | const char *args = (const char *)bin_buf; | ||
1441 | 1741 | ||
1442 | if (unlikely((int) size < 0)) { | 1742 | if (WARN_ON_ONCE((int) size < 0)) |
1443 | /* There can be only one.. */ | ||
1444 | static char warn = 1; | ||
1445 | WARN_ON(warn); | ||
1446 | warn = 0; | ||
1447 | return 0; | 1743 | return 0; |
1448 | } | ||
1449 | 1744 | ||
1450 | str = buf; | 1745 | str = buf; |
1451 | end = buf + size; | 1746 | end = buf + size; |
@@ -1472,10 +1767,8 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |||
1472 | } | 1767 | } |
1473 | 1768 | ||
1474 | while (*fmt) { | 1769 | while (*fmt) { |
1475 | int read; | ||
1476 | const char *old_fmt = fmt; | 1770 | const char *old_fmt = fmt; |
1477 | 1771 | int read = format_decode(fmt, &spec); | |
1478 | read = format_decode(fmt, &spec); | ||
1479 | 1772 | ||
1480 | fmt += read; | 1773 | fmt += read; |
1481 | 1774 | ||
@@ -1499,7 +1792,9 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |||
1499 | spec.precision = get_arg(int); | 1792 | spec.precision = get_arg(int); |
1500 | break; | 1793 | break; |
1501 | 1794 | ||
1502 | case FORMAT_TYPE_CHAR: | 1795 | case FORMAT_TYPE_CHAR: { |
1796 | char c; | ||
1797 | |||
1503 | if (!(spec.flags & LEFT)) { | 1798 | if (!(spec.flags & LEFT)) { |
1504 | while (--spec.field_width > 0) { | 1799 | while (--spec.field_width > 0) { |
1505 | if (str < end) | 1800 | if (str < end) |
@@ -1517,11 +1812,11 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |||
1517 | ++str; | 1812 | ++str; |
1518 | } | 1813 | } |
1519 | break; | 1814 | break; |
1815 | } | ||
1520 | 1816 | ||
1521 | case FORMAT_TYPE_STR: { | 1817 | case FORMAT_TYPE_STR: { |
1522 | const char *str_arg = args; | 1818 | const char *str_arg = args; |
1523 | size_t len = strlen(str_arg); | 1819 | args += strlen(str_arg) + 1; |
1524 | args += len + 1; | ||
1525 | str = string(str, end, (char *)str_arg, spec); | 1820 | str = string(str, end, (char *)str_arg, spec); |
1526 | break; | 1821 | break; |
1527 | } | 1822 | } |
@@ -1533,11 +1828,6 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |||
1533 | break; | 1828 | break; |
1534 | 1829 | ||
1535 | case FORMAT_TYPE_PERCENT_CHAR: | 1830 | case FORMAT_TYPE_PERCENT_CHAR: |
1536 | if (str < end) | ||
1537 | *str = '%'; | ||
1538 | ++str; | ||
1539 | break; | ||
1540 | |||
1541 | case FORMAT_TYPE_INVALID: | 1831 | case FORMAT_TYPE_INVALID: |
1542 | if (str < end) | 1832 | if (str < end) |
1543 | *str = '%'; | 1833 | *str = '%'; |
@@ -1548,15 +1838,15 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |||
1548 | /* skip */ | 1838 | /* skip */ |
1549 | break; | 1839 | break; |
1550 | 1840 | ||
1551 | default: | 1841 | default: { |
1842 | unsigned long long num; | ||
1843 | |||
1552 | switch (spec.type) { | 1844 | switch (spec.type) { |
1553 | 1845 | ||
1554 | case FORMAT_TYPE_LONG_LONG: | 1846 | case FORMAT_TYPE_LONG_LONG: |
1555 | num = get_arg(long long); | 1847 | num = get_arg(long long); |
1556 | break; | 1848 | break; |
1557 | case FORMAT_TYPE_ULONG: | 1849 | case FORMAT_TYPE_ULONG: |
1558 | num = get_arg(unsigned long); | ||
1559 | break; | ||
1560 | case FORMAT_TYPE_LONG: | 1850 | case FORMAT_TYPE_LONG: |
1561 | num = get_arg(unsigned long); | 1851 | num = get_arg(unsigned long); |
1562 | break; | 1852 | break; |
@@ -1586,8 +1876,9 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |||
1586 | } | 1876 | } |
1587 | 1877 | ||
1588 | str = number(str, end, num, spec); | 1878 | str = number(str, end, num, spec); |
1589 | } | 1879 | } /* default: */ |
1590 | } | 1880 | } /* switch(spec.type) */ |
1881 | } /* while(*fmt) */ | ||
1591 | 1882 | ||
1592 | if (size > 0) { | 1883 | if (size > 0) { |
1593 | if (str < end) | 1884 | if (str < end) |
@@ -1621,6 +1912,7 @@ int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) | |||
1621 | va_start(args, fmt); | 1912 | va_start(args, fmt); |
1622 | ret = vbin_printf(bin_buf, size, fmt, args); | 1913 | ret = vbin_printf(bin_buf, size, fmt, args); |
1623 | va_end(args); | 1914 | va_end(args); |
1915 | |||
1624 | return ret; | 1916 | return ret; |
1625 | } | 1917 | } |
1626 | EXPORT_SYMBOL_GPL(bprintf); | 1918 | EXPORT_SYMBOL_GPL(bprintf); |
@@ -1633,27 +1925,25 @@ EXPORT_SYMBOL_GPL(bprintf); | |||
1633 | * @fmt: format of buffer | 1925 | * @fmt: format of buffer |
1634 | * @args: arguments | 1926 | * @args: arguments |
1635 | */ | 1927 | */ |
1636 | int vsscanf(const char * buf, const char * fmt, va_list args) | 1928 | int vsscanf(const char *buf, const char *fmt, va_list args) |
1637 | { | 1929 | { |
1638 | const char *str = buf; | 1930 | const char *str = buf; |
1639 | char *next; | 1931 | char *next; |
1640 | char digit; | 1932 | char digit; |
1641 | int num = 0; | 1933 | int num = 0; |
1642 | int qualifier; | 1934 | u8 qualifier; |
1643 | int base; | 1935 | u8 base; |
1644 | int field_width; | 1936 | s16 field_width; |
1645 | int is_sign = 0; | 1937 | bool is_sign; |
1646 | 1938 | ||
1647 | while(*fmt && *str) { | 1939 | while (*fmt && *str) { |
1648 | /* skip any white space in format */ | 1940 | /* skip any white space in format */ |
1649 | /* white space in format matchs any amount of | 1941 | /* white space in format matchs any amount of |
1650 | * white space, including none, in the input. | 1942 | * white space, including none, in the input. |
1651 | */ | 1943 | */ |
1652 | if (isspace(*fmt)) { | 1944 | if (isspace(*fmt)) { |
1653 | while (isspace(*fmt)) | 1945 | fmt = skip_spaces(++fmt); |
1654 | ++fmt; | 1946 | str = skip_spaces(str); |
1655 | while (isspace(*str)) | ||
1656 | ++str; | ||
1657 | } | 1947 | } |
1658 | 1948 | ||
1659 | /* anything that is not a conversion must match exactly */ | 1949 | /* anything that is not a conversion must match exactly */ |
@@ -1666,12 +1956,12 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1666 | if (!*fmt) | 1956 | if (!*fmt) |
1667 | break; | 1957 | break; |
1668 | ++fmt; | 1958 | ++fmt; |
1669 | 1959 | ||
1670 | /* skip this conversion. | 1960 | /* skip this conversion. |
1671 | * advance both strings to next white space | 1961 | * advance both strings to next white space |
1672 | */ | 1962 | */ |
1673 | if (*fmt == '*') { | 1963 | if (*fmt == '*') { |
1674 | while (!isspace(*fmt) && *fmt) | 1964 | while (!isspace(*fmt) && *fmt != '%' && *fmt) |
1675 | fmt++; | 1965 | fmt++; |
1676 | while (!isspace(*str) && *str) | 1966 | while (!isspace(*str) && *str) |
1677 | str++; | 1967 | str++; |
@@ -1685,8 +1975,8 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1685 | 1975 | ||
1686 | /* get conversion qualifier */ | 1976 | /* get conversion qualifier */ |
1687 | qualifier = -1; | 1977 | qualifier = -1; |
1688 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || | 1978 | if (*fmt == 'h' || TOLOWER(*fmt) == 'l' || |
1689 | *fmt == 'Z' || *fmt == 'z') { | 1979 | TOLOWER(*fmt) == 'z') { |
1690 | qualifier = *fmt++; | 1980 | qualifier = *fmt++; |
1691 | if (unlikely(qualifier == *fmt)) { | 1981 | if (unlikely(qualifier == *fmt)) { |
1692 | if (qualifier == 'h') { | 1982 | if (qualifier == 'h') { |
@@ -1698,16 +1988,17 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1698 | } | 1988 | } |
1699 | } | 1989 | } |
1700 | } | 1990 | } |
1701 | base = 10; | ||
1702 | is_sign = 0; | ||
1703 | 1991 | ||
1704 | if (!*fmt || !*str) | 1992 | if (!*fmt || !*str) |
1705 | break; | 1993 | break; |
1706 | 1994 | ||
1707 | switch(*fmt++) { | 1995 | base = 10; |
1996 | is_sign = 0; | ||
1997 | |||
1998 | switch (*fmt++) { | ||
1708 | case 'c': | 1999 | case 'c': |
1709 | { | 2000 | { |
1710 | char *s = (char *) va_arg(args,char*); | 2001 | char *s = (char *)va_arg(args, char*); |
1711 | if (field_width == -1) | 2002 | if (field_width == -1) |
1712 | field_width = 1; | 2003 | field_width = 1; |
1713 | do { | 2004 | do { |
@@ -1718,17 +2009,15 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1718 | continue; | 2009 | continue; |
1719 | case 's': | 2010 | case 's': |
1720 | { | 2011 | { |
1721 | char *s = (char *) va_arg(args, char *); | 2012 | char *s = (char *)va_arg(args, char *); |
1722 | if(field_width == -1) | 2013 | if (field_width == -1) |
1723 | field_width = INT_MAX; | 2014 | field_width = SHRT_MAX; |
1724 | /* first, skip leading white space in buffer */ | 2015 | /* first, skip leading white space in buffer */ |
1725 | while (isspace(*str)) | 2016 | str = skip_spaces(str); |
1726 | str++; | ||
1727 | 2017 | ||
1728 | /* now copy until next white space */ | 2018 | /* now copy until next white space */ |
1729 | while (*str && !isspace(*str) && field_width--) { | 2019 | while (*str && !isspace(*str) && field_width--) |
1730 | *s++ = *str++; | 2020 | *s++ = *str++; |
1731 | } | ||
1732 | *s = '\0'; | 2021 | *s = '\0'; |
1733 | num++; | 2022 | num++; |
1734 | } | 2023 | } |
@@ -1736,7 +2025,7 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1736 | case 'n': | 2025 | case 'n': |
1737 | /* return number of characters read so far */ | 2026 | /* return number of characters read so far */ |
1738 | { | 2027 | { |
1739 | int *i = (int *)va_arg(args,int*); | 2028 | int *i = (int *)va_arg(args, int*); |
1740 | *i = str - buf; | 2029 | *i = str - buf; |
1741 | } | 2030 | } |
1742 | continue; | 2031 | continue; |
@@ -1748,14 +2037,14 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1748 | base = 16; | 2037 | base = 16; |
1749 | break; | 2038 | break; |
1750 | case 'i': | 2039 | case 'i': |
1751 | base = 0; | 2040 | base = 0; |
1752 | case 'd': | 2041 | case 'd': |
1753 | is_sign = 1; | 2042 | is_sign = 1; |
1754 | case 'u': | 2043 | case 'u': |
1755 | break; | 2044 | break; |
1756 | case '%': | 2045 | case '%': |
1757 | /* looking for '%' in str */ | 2046 | /* looking for '%' in str */ |
1758 | if (*str++ != '%') | 2047 | if (*str++ != '%') |
1759 | return num; | 2048 | return num; |
1760 | continue; | 2049 | continue; |
1761 | default: | 2050 | default: |
@@ -1766,71 +2055,70 @@ int vsscanf(const char * buf, const char * fmt, va_list args) | |||
1766 | /* have some sort of integer conversion. | 2055 | /* have some sort of integer conversion. |
1767 | * first, skip white space in buffer. | 2056 | * first, skip white space in buffer. |
1768 | */ | 2057 | */ |
1769 | while (isspace(*str)) | 2058 | str = skip_spaces(str); |
1770 | str++; | ||
1771 | 2059 | ||
1772 | digit = *str; | 2060 | digit = *str; |
1773 | if (is_sign && digit == '-') | 2061 | if (is_sign && digit == '-') |
1774 | digit = *(str + 1); | 2062 | digit = *(str + 1); |
1775 | 2063 | ||
1776 | if (!digit | 2064 | if (!digit |
1777 | || (base == 16 && !isxdigit(digit)) | 2065 | || (base == 16 && !isxdigit(digit)) |
1778 | || (base == 10 && !isdigit(digit)) | 2066 | || (base == 10 && !isdigit(digit)) |
1779 | || (base == 8 && (!isdigit(digit) || digit > '7')) | 2067 | || (base == 8 && (!isdigit(digit) || digit > '7')) |
1780 | || (base == 0 && !isdigit(digit))) | 2068 | || (base == 0 && !isdigit(digit))) |
1781 | break; | 2069 | break; |
1782 | 2070 | ||
1783 | switch(qualifier) { | 2071 | switch (qualifier) { |
1784 | case 'H': /* that's 'hh' in format */ | 2072 | case 'H': /* that's 'hh' in format */ |
1785 | if (is_sign) { | 2073 | if (is_sign) { |
1786 | signed char *s = (signed char *) va_arg(args,signed char *); | 2074 | signed char *s = (signed char *)va_arg(args, signed char *); |
1787 | *s = (signed char) simple_strtol(str,&next,base); | 2075 | *s = (signed char)simple_strtol(str, &next, base); |
1788 | } else { | 2076 | } else { |
1789 | unsigned char *s = (unsigned char *) va_arg(args, unsigned char *); | 2077 | unsigned char *s = (unsigned char *)va_arg(args, unsigned char *); |
1790 | *s = (unsigned char) simple_strtoul(str, &next, base); | 2078 | *s = (unsigned char)simple_strtoul(str, &next, base); |
1791 | } | 2079 | } |
1792 | break; | 2080 | break; |
1793 | case 'h': | 2081 | case 'h': |
1794 | if (is_sign) { | 2082 | if (is_sign) { |
1795 | short *s = (short *) va_arg(args,short *); | 2083 | short *s = (short *)va_arg(args, short *); |
1796 | *s = (short) simple_strtol(str,&next,base); | 2084 | *s = (short)simple_strtol(str, &next, base); |
1797 | } else { | 2085 | } else { |
1798 | unsigned short *s = (unsigned short *) va_arg(args, unsigned short *); | 2086 | unsigned short *s = (unsigned short *)va_arg(args, unsigned short *); |
1799 | *s = (unsigned short) simple_strtoul(str, &next, base); | 2087 | *s = (unsigned short)simple_strtoul(str, &next, base); |
1800 | } | 2088 | } |
1801 | break; | 2089 | break; |
1802 | case 'l': | 2090 | case 'l': |
1803 | if (is_sign) { | 2091 | if (is_sign) { |
1804 | long *l = (long *) va_arg(args,long *); | 2092 | long *l = (long *)va_arg(args, long *); |
1805 | *l = simple_strtol(str,&next,base); | 2093 | *l = simple_strtol(str, &next, base); |
1806 | } else { | 2094 | } else { |
1807 | unsigned long *l = (unsigned long*) va_arg(args,unsigned long*); | 2095 | unsigned long *l = (unsigned long *)va_arg(args, unsigned long *); |
1808 | *l = simple_strtoul(str,&next,base); | 2096 | *l = simple_strtoul(str, &next, base); |
1809 | } | 2097 | } |
1810 | break; | 2098 | break; |
1811 | case 'L': | 2099 | case 'L': |
1812 | if (is_sign) { | 2100 | if (is_sign) { |
1813 | long long *l = (long long*) va_arg(args,long long *); | 2101 | long long *l = (long long *)va_arg(args, long long *); |
1814 | *l = simple_strtoll(str,&next,base); | 2102 | *l = simple_strtoll(str, &next, base); |
1815 | } else { | 2103 | } else { |
1816 | unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*); | 2104 | unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *); |
1817 | *l = simple_strtoull(str,&next,base); | 2105 | *l = simple_strtoull(str, &next, base); |
1818 | } | 2106 | } |
1819 | break; | 2107 | break; |
1820 | case 'Z': | 2108 | case 'Z': |
1821 | case 'z': | 2109 | case 'z': |
1822 | { | 2110 | { |
1823 | size_t *s = (size_t*) va_arg(args,size_t*); | 2111 | size_t *s = (size_t *)va_arg(args, size_t *); |
1824 | *s = (size_t) simple_strtoul(str,&next,base); | 2112 | *s = (size_t)simple_strtoul(str, &next, base); |
1825 | } | 2113 | } |
1826 | break; | 2114 | break; |
1827 | default: | 2115 | default: |
1828 | if (is_sign) { | 2116 | if (is_sign) { |
1829 | int *i = (int *) va_arg(args, int*); | 2117 | int *i = (int *)va_arg(args, int *); |
1830 | *i = (int) simple_strtol(str,&next,base); | 2118 | *i = (int)simple_strtol(str, &next, base); |
1831 | } else { | 2119 | } else { |
1832 | unsigned int *i = (unsigned int*) va_arg(args, unsigned int*); | 2120 | unsigned int *i = (unsigned int *)va_arg(args, unsigned int*); |
1833 | *i = (unsigned int) simple_strtoul(str,&next,base); | 2121 | *i = (unsigned int)simple_strtoul(str, &next, base); |
1834 | } | 2122 | } |
1835 | break; | 2123 | break; |
1836 | } | 2124 | } |
@@ -1861,14 +2149,15 @@ EXPORT_SYMBOL(vsscanf); | |||
1861 | * @fmt: formatting of buffer | 2149 | * @fmt: formatting of buffer |
1862 | * @...: resulting arguments | 2150 | * @...: resulting arguments |
1863 | */ | 2151 | */ |
1864 | int sscanf(const char * buf, const char * fmt, ...) | 2152 | int sscanf(const char *buf, const char *fmt, ...) |
1865 | { | 2153 | { |
1866 | va_list args; | 2154 | va_list args; |
1867 | int i; | 2155 | int i; |
1868 | 2156 | ||
1869 | va_start(args,fmt); | 2157 | va_start(args, fmt); |
1870 | i = vsscanf(buf,fmt,args); | 2158 | i = vsscanf(buf, fmt, args); |
1871 | va_end(args); | 2159 | va_end(args); |
2160 | |||
1872 | return i; | 2161 | return i; |
1873 | } | 2162 | } |
1874 | EXPORT_SYMBOL(sscanf); | 2163 | EXPORT_SYMBOL(sscanf); |