diff options
-rw-r--r-- | include/linux/kernel.h | 17 | ||||
-rw-r--r-- | include/linux/string.h | 7 | ||||
-rw-r--r-- | lib/Kconfig | 3 | ||||
-rw-r--r-- | lib/vsprintf.c | 1006 |
4 files changed, 806 insertions, 227 deletions
diff --git a/include/linux/kernel.h b/include/linux/kernel.h index d4614a8a034b..7aef15c4645e 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h | |||
@@ -242,6 +242,19 @@ extern struct ratelimit_state printk_ratelimit_state; | |||
242 | extern int printk_ratelimit(void); | 242 | extern int printk_ratelimit(void); |
243 | extern bool printk_timed_ratelimit(unsigned long *caller_jiffies, | 243 | extern bool printk_timed_ratelimit(unsigned long *caller_jiffies, |
244 | unsigned int interval_msec); | 244 | unsigned int interval_msec); |
245 | |||
246 | /* | ||
247 | * Print a one-time message (analogous to WARN_ONCE() et al): | ||
248 | */ | ||
249 | #define printk_once(x...) ({ \ | ||
250 | static int __print_once = 1; \ | ||
251 | \ | ||
252 | if (__print_once) { \ | ||
253 | __print_once = 0; \ | ||
254 | printk(x); \ | ||
255 | } \ | ||
256 | }) | ||
257 | |||
245 | #else | 258 | #else |
246 | static inline int vprintk(const char *s, va_list args) | 259 | static inline int vprintk(const char *s, va_list args) |
247 | __attribute__ ((format (printf, 1, 0))); | 260 | __attribute__ ((format (printf, 1, 0))); |
@@ -253,6 +266,10 @@ static inline int printk_ratelimit(void) { return 0; } | |||
253 | static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \ | 266 | static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \ |
254 | unsigned int interval_msec) \ | 267 | unsigned int interval_msec) \ |
255 | { return false; } | 268 | { return false; } |
269 | |||
270 | /* No effect, but we still get type checking even in the !PRINTK case: */ | ||
271 | #define printk_once(x...) printk(x) | ||
272 | |||
256 | #endif | 273 | #endif |
257 | 274 | ||
258 | extern int printk_needs_cpu(int cpu); | 275 | extern int printk_needs_cpu(int cpu); |
diff --git a/include/linux/string.h b/include/linux/string.h index d18fc198aa2f..27ac31784ad2 100644 --- a/include/linux/string.h +++ b/include/linux/string.h | |||
@@ -10,6 +10,7 @@ | |||
10 | #include <linux/compiler.h> /* for inline */ | 10 | #include <linux/compiler.h> /* for inline */ |
11 | #include <linux/types.h> /* for size_t */ | 11 | #include <linux/types.h> /* for size_t */ |
12 | #include <linux/stddef.h> /* for NULL */ | 12 | #include <linux/stddef.h> /* for NULL */ |
13 | #include <stdarg.h> | ||
13 | 14 | ||
14 | extern char *strndup_user(const char __user *, long); | 15 | extern char *strndup_user(const char __user *, long); |
15 | 16 | ||
@@ -111,6 +112,12 @@ extern void argv_free(char **argv); | |||
111 | 112 | ||
112 | extern bool sysfs_streq(const char *s1, const char *s2); | 113 | extern bool sysfs_streq(const char *s1, const char *s2); |
113 | 114 | ||
115 | #ifdef CONFIG_BINARY_PRINTF | ||
116 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args); | ||
117 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf); | ||
118 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4); | ||
119 | #endif | ||
120 | |||
114 | extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, | 121 | extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, |
115 | const void *from, size_t available); | 122 | const void *from, size_t available); |
116 | 123 | ||
diff --git a/lib/Kconfig b/lib/Kconfig index 03c2c24b9083..97d62cf091a7 100644 --- a/lib/Kconfig +++ b/lib/Kconfig | |||
@@ -2,6 +2,9 @@ | |||
2 | # Library configuration | 2 | # Library configuration |
3 | # | 3 | # |
4 | 4 | ||
5 | config BINARY_PRINTF | ||
6 | def_bool n | ||
7 | |||
5 | menu "Library routines" | 8 | menu "Library routines" |
6 | 9 | ||
7 | config BITREVERSE | 10 | config BITREVERSE |
diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 0fbd0121d91d..25f01578c856 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c | |||
@@ -396,7 +396,38 @@ static noinline char* put_dec(char *buf, unsigned long long num) | |||
396 | #define SMALL 32 /* Must be 32 == 0x20 */ | 396 | #define SMALL 32 /* Must be 32 == 0x20 */ |
397 | #define SPECIAL 64 /* 0x */ | 397 | #define SPECIAL 64 /* 0x */ |
398 | 398 | ||
399 | static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type) | 399 | enum format_type { |
400 | FORMAT_TYPE_NONE, /* Just a string part */ | ||
401 | FORMAT_TYPE_WITDH, | ||
402 | FORMAT_TYPE_PRECISION, | ||
403 | FORMAT_TYPE_CHAR, | ||
404 | FORMAT_TYPE_STR, | ||
405 | FORMAT_TYPE_PTR, | ||
406 | FORMAT_TYPE_PERCENT_CHAR, | ||
407 | FORMAT_TYPE_INVALID, | ||
408 | FORMAT_TYPE_LONG_LONG, | ||
409 | FORMAT_TYPE_ULONG, | ||
410 | FORMAT_TYPE_LONG, | ||
411 | FORMAT_TYPE_USHORT, | ||
412 | FORMAT_TYPE_SHORT, | ||
413 | FORMAT_TYPE_UINT, | ||
414 | FORMAT_TYPE_INT, | ||
415 | FORMAT_TYPE_NRCHARS, | ||
416 | FORMAT_TYPE_SIZE_T, | ||
417 | FORMAT_TYPE_PTRDIFF | ||
418 | }; | ||
419 | |||
420 | struct printf_spec { | ||
421 | enum format_type type; | ||
422 | int flags; /* flags to number() */ | ||
423 | int field_width; /* width of output field */ | ||
424 | int base; | ||
425 | int precision; /* # of digits/chars */ | ||
426 | int qualifier; | ||
427 | }; | ||
428 | |||
429 | static char *number(char *buf, char *end, unsigned long long num, | ||
430 | struct printf_spec spec) | ||
400 | { | 431 | { |
401 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ | 432 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ |
402 | static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ | 433 | static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ |
@@ -404,32 +435,32 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int | |||
404 | char tmp[66]; | 435 | char tmp[66]; |
405 | char sign; | 436 | char sign; |
406 | char locase; | 437 | char locase; |
407 | int need_pfx = ((type & SPECIAL) && base != 10); | 438 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
408 | int i; | 439 | int i; |
409 | 440 | ||
410 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' | 441 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
411 | * produces same digits or (maybe lowercased) letters */ | 442 | * produces same digits or (maybe lowercased) letters */ |
412 | locase = (type & SMALL); | 443 | locase = (spec.flags & SMALL); |
413 | if (type & LEFT) | 444 | if (spec.flags & LEFT) |
414 | type &= ~ZEROPAD; | 445 | spec.flags &= ~ZEROPAD; |
415 | sign = 0; | 446 | sign = 0; |
416 | if (type & SIGN) { | 447 | if (spec.flags & SIGN) { |
417 | if ((signed long long) num < 0) { | 448 | if ((signed long long) num < 0) { |
418 | sign = '-'; | 449 | sign = '-'; |
419 | num = - (signed long long) num; | 450 | num = - (signed long long) num; |
420 | size--; | 451 | spec.field_width--; |
421 | } else if (type & PLUS) { | 452 | } else if (spec.flags & PLUS) { |
422 | sign = '+'; | 453 | sign = '+'; |
423 | size--; | 454 | spec.field_width--; |
424 | } else if (type & SPACE) { | 455 | } else if (spec.flags & SPACE) { |
425 | sign = ' '; | 456 | sign = ' '; |
426 | size--; | 457 | spec.field_width--; |
427 | } | 458 | } |
428 | } | 459 | } |
429 | if (need_pfx) { | 460 | if (need_pfx) { |
430 | size--; | 461 | spec.field_width--; |
431 | if (base == 16) | 462 | if (spec.base == 16) |
432 | size--; | 463 | spec.field_width--; |
433 | } | 464 | } |
434 | 465 | ||
435 | /* generate full string in tmp[], in reverse order */ | 466 | /* generate full string in tmp[], in reverse order */ |
@@ -441,10 +472,10 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int | |||
441 | tmp[i++] = (digits[do_div(num,base)] | locase); | 472 | tmp[i++] = (digits[do_div(num,base)] | locase); |
442 | } while (num != 0); | 473 | } while (num != 0); |
443 | */ | 474 | */ |
444 | else if (base != 10) { /* 8 or 16 */ | 475 | else if (spec.base != 10) { /* 8 or 16 */ |
445 | int mask = base - 1; | 476 | int mask = spec.base - 1; |
446 | int shift = 3; | 477 | int shift = 3; |
447 | if (base == 16) shift = 4; | 478 | if (spec.base == 16) shift = 4; |
448 | do { | 479 | do { |
449 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); | 480 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); |
450 | num >>= shift; | 481 | num >>= shift; |
@@ -454,12 +485,12 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int | |||
454 | } | 485 | } |
455 | 486 | ||
456 | /* printing 100 using %2d gives "100", not "00" */ | 487 | /* printing 100 using %2d gives "100", not "00" */ |
457 | if (i > precision) | 488 | if (i > spec.precision) |
458 | precision = i; | 489 | spec.precision = i; |
459 | /* leading space padding */ | 490 | /* leading space padding */ |
460 | size -= precision; | 491 | spec.field_width -= spec.precision; |
461 | if (!(type & (ZEROPAD+LEFT))) { | 492 | if (!(spec.flags & (ZEROPAD+LEFT))) { |
462 | while(--size >= 0) { | 493 | while(--spec.field_width >= 0) { |
463 | if (buf < end) | 494 | if (buf < end) |
464 | *buf = ' '; | 495 | *buf = ' '; |
465 | ++buf; | 496 | ++buf; |
@@ -476,23 +507,23 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int | |||
476 | if (buf < end) | 507 | if (buf < end) |
477 | *buf = '0'; | 508 | *buf = '0'; |
478 | ++buf; | 509 | ++buf; |
479 | if (base == 16) { | 510 | if (spec.base == 16) { |
480 | if (buf < end) | 511 | if (buf < end) |
481 | *buf = ('X' | locase); | 512 | *buf = ('X' | locase); |
482 | ++buf; | 513 | ++buf; |
483 | } | 514 | } |
484 | } | 515 | } |
485 | /* zero or space padding */ | 516 | /* zero or space padding */ |
486 | if (!(type & LEFT)) { | 517 | if (!(spec.flags & LEFT)) { |
487 | char c = (type & ZEROPAD) ? '0' : ' '; | 518 | char c = (spec.flags & ZEROPAD) ? '0' : ' '; |
488 | while (--size >= 0) { | 519 | while (--spec.field_width >= 0) { |
489 | if (buf < end) | 520 | if (buf < end) |
490 | *buf = c; | 521 | *buf = c; |
491 | ++buf; | 522 | ++buf; |
492 | } | 523 | } |
493 | } | 524 | } |
494 | /* hmm even more zero padding? */ | 525 | /* hmm even more zero padding? */ |
495 | while (i <= --precision) { | 526 | while (i <= --spec.precision) { |
496 | if (buf < end) | 527 | if (buf < end) |
497 | *buf = '0'; | 528 | *buf = '0'; |
498 | ++buf; | 529 | ++buf; |
@@ -504,7 +535,7 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int | |||
504 | ++buf; | 535 | ++buf; |
505 | } | 536 | } |
506 | /* trailing space padding */ | 537 | /* trailing space padding */ |
507 | while (--size >= 0) { | 538 | while (--spec.field_width >= 0) { |
508 | if (buf < end) | 539 | if (buf < end) |
509 | *buf = ' '; | 540 | *buf = ' '; |
510 | ++buf; | 541 | ++buf; |
@@ -512,17 +543,17 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int | |||
512 | return buf; | 543 | return buf; |
513 | } | 544 | } |
514 | 545 | ||
515 | static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags) | 546 | static char *string(char *buf, char *end, char *s, struct printf_spec spec) |
516 | { | 547 | { |
517 | int len, i; | 548 | int len, i; |
518 | 549 | ||
519 | if ((unsigned long)s < PAGE_SIZE) | 550 | if ((unsigned long)s < PAGE_SIZE) |
520 | s = "<NULL>"; | 551 | s = "<NULL>"; |
521 | 552 | ||
522 | len = strnlen(s, precision); | 553 | len = strnlen(s, spec.precision); |
523 | 554 | ||
524 | if (!(flags & LEFT)) { | 555 | if (!(spec.flags & LEFT)) { |
525 | while (len < field_width--) { | 556 | while (len < spec.field_width--) { |
526 | if (buf < end) | 557 | if (buf < end) |
527 | *buf = ' '; | 558 | *buf = ' '; |
528 | ++buf; | 559 | ++buf; |
@@ -533,7 +564,7 @@ static char *string(char *buf, char *end, char *s, int field_width, int precisio | |||
533 | *buf = *s; | 564 | *buf = *s; |
534 | ++buf; ++s; | 565 | ++buf; ++s; |
535 | } | 566 | } |
536 | while (len < field_width--) { | 567 | while (len < spec.field_width--) { |
537 | if (buf < end) | 568 | if (buf < end) |
538 | *buf = ' '; | 569 | *buf = ' '; |
539 | ++buf; | 570 | ++buf; |
@@ -541,21 +572,24 @@ static char *string(char *buf, char *end, char *s, int field_width, int precisio | |||
541 | return buf; | 572 | return buf; |
542 | } | 573 | } |
543 | 574 | ||
544 | static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags) | 575 | static char *symbol_string(char *buf, char *end, void *ptr, |
576 | struct printf_spec spec) | ||
545 | { | 577 | { |
546 | unsigned long value = (unsigned long) ptr; | 578 | unsigned long value = (unsigned long) ptr; |
547 | #ifdef CONFIG_KALLSYMS | 579 | #ifdef CONFIG_KALLSYMS |
548 | char sym[KSYM_SYMBOL_LEN]; | 580 | char sym[KSYM_SYMBOL_LEN]; |
549 | sprint_symbol(sym, value); | 581 | sprint_symbol(sym, value); |
550 | return string(buf, end, sym, field_width, precision, flags); | 582 | return string(buf, end, sym, spec); |
551 | #else | 583 | #else |
552 | field_width = 2*sizeof(void *); | 584 | spec.field_width = 2*sizeof(void *); |
553 | flags |= SPECIAL | SMALL | ZEROPAD; | 585 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
554 | return number(buf, end, value, 16, field_width, precision, flags); | 586 | spec.base = 16; |
587 | return number(buf, end, value, spec); | ||
555 | #endif | 588 | #endif |
556 | } | 589 | } |
557 | 590 | ||
558 | static char *resource_string(char *buf, char *end, struct resource *res, int field_width, int precision, int flags) | 591 | static char *resource_string(char *buf, char *end, struct resource *res, |
592 | struct printf_spec spec) | ||
559 | { | 593 | { |
560 | #ifndef IO_RSRC_PRINTK_SIZE | 594 | #ifndef IO_RSRC_PRINTK_SIZE |
561 | #define IO_RSRC_PRINTK_SIZE 4 | 595 | #define IO_RSRC_PRINTK_SIZE 4 |
@@ -564,7 +598,11 @@ static char *resource_string(char *buf, char *end, struct resource *res, int fie | |||
564 | #ifndef MEM_RSRC_PRINTK_SIZE | 598 | #ifndef MEM_RSRC_PRINTK_SIZE |
565 | #define MEM_RSRC_PRINTK_SIZE 8 | 599 | #define MEM_RSRC_PRINTK_SIZE 8 |
566 | #endif | 600 | #endif |
567 | 601 | struct printf_spec num_spec = { | |
602 | .base = 16, | ||
603 | .precision = -1, | ||
604 | .flags = SPECIAL | SMALL | ZEROPAD, | ||
605 | }; | ||
568 | /* room for the actual numbers, the two "0x", -, [, ] and the final zero */ | 606 | /* room for the actual numbers, the two "0x", -, [, ] and the final zero */ |
569 | char sym[4*sizeof(resource_size_t) + 8]; | 607 | char sym[4*sizeof(resource_size_t) + 8]; |
570 | char *p = sym, *pend = sym + sizeof(sym); | 608 | char *p = sym, *pend = sym + sizeof(sym); |
@@ -576,17 +614,18 @@ static char *resource_string(char *buf, char *end, struct resource *res, int fie | |||
576 | size = MEM_RSRC_PRINTK_SIZE; | 614 | size = MEM_RSRC_PRINTK_SIZE; |
577 | 615 | ||
578 | *p++ = '['; | 616 | *p++ = '['; |
579 | p = number(p, pend, res->start, 16, size, -1, SPECIAL | SMALL | ZEROPAD); | 617 | num_spec.field_width = size; |
618 | p = number(p, pend, res->start, num_spec); | ||
580 | *p++ = '-'; | 619 | *p++ = '-'; |
581 | p = number(p, pend, res->end, 16, size, -1, SPECIAL | SMALL | ZEROPAD); | 620 | p = number(p, pend, res->end, num_spec); |
582 | *p++ = ']'; | 621 | *p++ = ']'; |
583 | *p = 0; | 622 | *p = 0; |
584 | 623 | ||
585 | return string(buf, end, sym, field_width, precision, flags); | 624 | return string(buf, end, sym, spec); |
586 | } | 625 | } |
587 | 626 | ||
588 | static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width, | 627 | static char *mac_address_string(char *buf, char *end, u8 *addr, |
589 | int precision, int flags) | 628 | struct printf_spec spec) |
590 | { | 629 | { |
591 | char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */ | 630 | char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */ |
592 | char *p = mac_addr; | 631 | char *p = mac_addr; |
@@ -594,16 +633,17 @@ static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width, | |||
594 | 633 | ||
595 | for (i = 0; i < 6; i++) { | 634 | for (i = 0; i < 6; i++) { |
596 | p = pack_hex_byte(p, addr[i]); | 635 | p = pack_hex_byte(p, addr[i]); |
597 | if (!(flags & SPECIAL) && i != 5) | 636 | if (!(spec.flags & SPECIAL) && i != 5) |
598 | *p++ = ':'; | 637 | *p++ = ':'; |
599 | } | 638 | } |
600 | *p = '\0'; | 639 | *p = '\0'; |
640 | spec.flags &= ~SPECIAL; | ||
601 | 641 | ||
602 | return string(buf, end, mac_addr, field_width, precision, flags & ~SPECIAL); | 642 | return string(buf, end, mac_addr, spec); |
603 | } | 643 | } |
604 | 644 | ||
605 | static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width, | 645 | static char *ip6_addr_string(char *buf, char *end, u8 *addr, |
606 | int precision, int flags) | 646 | struct printf_spec spec) |
607 | { | 647 | { |
608 | char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */ | 648 | char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */ |
609 | char *p = ip6_addr; | 649 | char *p = ip6_addr; |
@@ -612,16 +652,17 @@ static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width, | |||
612 | for (i = 0; i < 8; i++) { | 652 | for (i = 0; i < 8; i++) { |
613 | p = pack_hex_byte(p, addr[2 * i]); | 653 | p = pack_hex_byte(p, addr[2 * i]); |
614 | p = pack_hex_byte(p, addr[2 * i + 1]); | 654 | p = pack_hex_byte(p, addr[2 * i + 1]); |
615 | if (!(flags & SPECIAL) && i != 7) | 655 | if (!(spec.flags & SPECIAL) && i != 7) |
616 | *p++ = ':'; | 656 | *p++ = ':'; |
617 | } | 657 | } |
618 | *p = '\0'; | 658 | *p = '\0'; |
659 | spec.flags &= ~SPECIAL; | ||
619 | 660 | ||
620 | return string(buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL); | 661 | return string(buf, end, ip6_addr, spec); |
621 | } | 662 | } |
622 | 663 | ||
623 | static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width, | 664 | static char *ip4_addr_string(char *buf, char *end, u8 *addr, |
624 | int precision, int flags) | 665 | struct printf_spec spec) |
625 | { | 666 | { |
626 | char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */ | 667 | char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */ |
627 | char temp[3]; /* hold each IP quad in reverse order */ | 668 | char temp[3]; /* hold each IP quad in reverse order */ |
@@ -637,8 +678,9 @@ static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width, | |||
637 | *p++ = '.'; | 678 | *p++ = '.'; |
638 | } | 679 | } |
639 | *p = '\0'; | 680 | *p = '\0'; |
681 | spec.flags &= ~SPECIAL; | ||
640 | 682 | ||
641 | return string(buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL); | 683 | return string(buf, end, ip4_addr, spec); |
642 | } | 684 | } |
643 | 685 | ||
644 | /* | 686 | /* |
@@ -663,41 +705,234 @@ static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width, | |||
663 | * function pointers are really function descriptors, which contain a | 705 | * function pointers are really function descriptors, which contain a |
664 | * pointer to the real address. | 706 | * pointer to the real address. |
665 | */ | 707 | */ |
666 | static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags) | 708 | static char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
709 | struct printf_spec spec) | ||
667 | { | 710 | { |
668 | if (!ptr) | 711 | if (!ptr) |
669 | return string(buf, end, "(null)", field_width, precision, flags); | 712 | return string(buf, end, "(null)", spec); |
670 | 713 | ||
671 | switch (*fmt) { | 714 | switch (*fmt) { |
672 | case 'F': | 715 | case 'F': |
673 | ptr = dereference_function_descriptor(ptr); | 716 | ptr = dereference_function_descriptor(ptr); |
674 | /* Fallthrough */ | 717 | /* Fallthrough */ |
675 | case 'S': | 718 | case 'S': |
676 | return symbol_string(buf, end, ptr, field_width, precision, flags); | 719 | return symbol_string(buf, end, ptr, spec); |
677 | case 'R': | 720 | case 'R': |
678 | return resource_string(buf, end, ptr, field_width, precision, flags); | 721 | return resource_string(buf, end, ptr, spec); |
679 | case 'm': | 722 | case 'm': |
680 | flags |= SPECIAL; | 723 | spec.flags |= SPECIAL; |
681 | /* Fallthrough */ | 724 | /* Fallthrough */ |
682 | case 'M': | 725 | case 'M': |
683 | return mac_address_string(buf, end, ptr, field_width, precision, flags); | 726 | return mac_address_string(buf, end, ptr, spec); |
684 | case 'i': | 727 | case 'i': |
685 | flags |= SPECIAL; | 728 | spec.flags |= SPECIAL; |
686 | /* Fallthrough */ | 729 | /* Fallthrough */ |
687 | case 'I': | 730 | case 'I': |
688 | if (fmt[1] == '6') | 731 | if (fmt[1] == '6') |
689 | return ip6_addr_string(buf, end, ptr, field_width, precision, flags); | 732 | return ip6_addr_string(buf, end, ptr, spec); |
690 | if (fmt[1] == '4') | 733 | if (fmt[1] == '4') |
691 | return ip4_addr_string(buf, end, ptr, field_width, precision, flags); | 734 | return ip4_addr_string(buf, end, ptr, spec); |
692 | flags &= ~SPECIAL; | 735 | spec.flags &= ~SPECIAL; |
736 | break; | ||
737 | } | ||
738 | spec.flags |= SMALL; | ||
739 | if (spec.field_width == -1) { | ||
740 | spec.field_width = 2*sizeof(void *); | ||
741 | spec.flags |= ZEROPAD; | ||
742 | } | ||
743 | spec.base = 16; | ||
744 | |||
745 | return number(buf, end, (unsigned long) ptr, spec); | ||
746 | } | ||
747 | |||
748 | /* | ||
749 | * Helper function to decode printf style format. | ||
750 | * Each call decode a token from the format and return the | ||
751 | * number of characters read (or likely the delta where it wants | ||
752 | * to go on the next call). | ||
753 | * The decoded token is returned through the parameters | ||
754 | * | ||
755 | * 'h', 'l', or 'L' for integer fields | ||
756 | * 'z' support added 23/7/1999 S.H. | ||
757 | * 'z' changed to 'Z' --davidm 1/25/99 | ||
758 | * 't' added for ptrdiff_t | ||
759 | * | ||
760 | * @fmt: the format string | ||
761 | * @type of the token returned | ||
762 | * @flags: various flags such as +, -, # tokens.. | ||
763 | * @field_width: overwritten width | ||
764 | * @base: base of the number (octal, hex, ...) | ||
765 | * @precision: precision of a number | ||
766 | * @qualifier: qualifier of a number (long, size_t, ...) | ||
767 | */ | ||
768 | static int format_decode(const char *fmt, struct printf_spec *spec) | ||
769 | { | ||
770 | const char *start = fmt; | ||
771 | bool sign = false; | ||
772 | |||
773 | /* we finished early by reading the field width */ | ||
774 | if (spec->type == FORMAT_TYPE_WITDH) { | ||
775 | if (spec->field_width < 0) { | ||
776 | spec->field_width = -spec->field_width; | ||
777 | spec->flags |= LEFT; | ||
778 | } | ||
779 | spec->type = FORMAT_TYPE_NONE; | ||
780 | goto precision; | ||
781 | } | ||
782 | |||
783 | /* we finished early by reading the precision */ | ||
784 | if (spec->type == FORMAT_TYPE_PRECISION) { | ||
785 | if (spec->precision < 0) | ||
786 | spec->precision = 0; | ||
787 | |||
788 | spec->type = FORMAT_TYPE_NONE; | ||
789 | goto qualifier; | ||
790 | } | ||
791 | |||
792 | /* By default */ | ||
793 | spec->type = FORMAT_TYPE_NONE; | ||
794 | |||
795 | for (; *fmt ; ++fmt) { | ||
796 | if (*fmt == '%') | ||
797 | break; | ||
798 | } | ||
799 | |||
800 | /* Return the current non-format string */ | ||
801 | if (fmt != start || !*fmt) | ||
802 | return fmt - start; | ||
803 | |||
804 | /* Process flags */ | ||
805 | spec->flags = 0; | ||
806 | |||
807 | while (1) { /* this also skips first '%' */ | ||
808 | bool found = true; | ||
809 | |||
810 | ++fmt; | ||
811 | |||
812 | switch (*fmt) { | ||
813 | case '-': spec->flags |= LEFT; break; | ||
814 | case '+': spec->flags |= PLUS; break; | ||
815 | case ' ': spec->flags |= SPACE; break; | ||
816 | case '#': spec->flags |= SPECIAL; break; | ||
817 | case '0': spec->flags |= ZEROPAD; break; | ||
818 | default: found = false; | ||
819 | } | ||
820 | |||
821 | if (!found) | ||
822 | break; | ||
823 | } | ||
824 | |||
825 | /* get field width */ | ||
826 | spec->field_width = -1; | ||
827 | |||
828 | if (isdigit(*fmt)) | ||
829 | spec->field_width = skip_atoi(&fmt); | ||
830 | else if (*fmt == '*') { | ||
831 | /* it's the next argument */ | ||
832 | spec->type = FORMAT_TYPE_WITDH; | ||
833 | return ++fmt - start; | ||
834 | } | ||
835 | |||
836 | precision: | ||
837 | /* get the precision */ | ||
838 | spec->precision = -1; | ||
839 | if (*fmt == '.') { | ||
840 | ++fmt; | ||
841 | if (isdigit(*fmt)) { | ||
842 | spec->precision = skip_atoi(&fmt); | ||
843 | if (spec->precision < 0) | ||
844 | spec->precision = 0; | ||
845 | } else if (*fmt == '*') { | ||
846 | /* it's the next argument */ | ||
847 | spec->type = FORMAT_TYPE_WITDH; | ||
848 | return ++fmt - start; | ||
849 | } | ||
850 | } | ||
851 | |||
852 | qualifier: | ||
853 | /* get the conversion qualifier */ | ||
854 | spec->qualifier = -1; | ||
855 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || | ||
856 | *fmt == 'Z' || *fmt == 'z' || *fmt == 't') { | ||
857 | spec->qualifier = *fmt; | ||
858 | ++fmt; | ||
859 | if (spec->qualifier == 'l' && *fmt == 'l') { | ||
860 | spec->qualifier = 'L'; | ||
861 | ++fmt; | ||
862 | } | ||
863 | } | ||
864 | |||
865 | /* default base */ | ||
866 | spec->base = 10; | ||
867 | switch (*fmt) { | ||
868 | case 'c': | ||
869 | spec->type = FORMAT_TYPE_CHAR; | ||
870 | return ++fmt - start; | ||
871 | |||
872 | case 's': | ||
873 | spec->type = FORMAT_TYPE_STR; | ||
874 | return ++fmt - start; | ||
875 | |||
876 | case 'p': | ||
877 | spec->type = FORMAT_TYPE_PTR; | ||
878 | return fmt - start; | ||
879 | /* skip alnum */ | ||
880 | |||
881 | case 'n': | ||
882 | spec->type = FORMAT_TYPE_NRCHARS; | ||
883 | return ++fmt - start; | ||
884 | |||
885 | case '%': | ||
886 | spec->type = FORMAT_TYPE_PERCENT_CHAR; | ||
887 | return ++fmt - start; | ||
888 | |||
889 | /* integer number formats - set up the flags and "break" */ | ||
890 | case 'o': | ||
891 | spec->base = 8; | ||
892 | break; | ||
893 | |||
894 | case 'x': | ||
895 | spec->flags |= SMALL; | ||
896 | |||
897 | case 'X': | ||
898 | spec->base = 16; | ||
899 | break; | ||
900 | |||
901 | case 'd': | ||
902 | case 'i': | ||
903 | sign = true; | ||
904 | case 'u': | ||
693 | break; | 905 | break; |
906 | |||
907 | default: | ||
908 | spec->type = FORMAT_TYPE_INVALID; | ||
909 | return fmt - start; | ||
694 | } | 910 | } |
695 | flags |= SMALL; | 911 | |
696 | if (field_width == -1) { | 912 | if (spec->qualifier == 'L') |
697 | field_width = 2*sizeof(void *); | 913 | spec->type = FORMAT_TYPE_LONG_LONG; |
698 | flags |= ZEROPAD; | 914 | else if (spec->qualifier == 'l') { |
915 | if (sign) | ||
916 | spec->type = FORMAT_TYPE_LONG; | ||
917 | else | ||
918 | spec->type = FORMAT_TYPE_ULONG; | ||
919 | } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') { | ||
920 | spec->type = FORMAT_TYPE_SIZE_T; | ||
921 | } else if (spec->qualifier == 't') { | ||
922 | spec->type = FORMAT_TYPE_PTRDIFF; | ||
923 | } else if (spec->qualifier == 'h') { | ||
924 | if (sign) | ||
925 | spec->type = FORMAT_TYPE_SHORT; | ||
926 | else | ||
927 | spec->type = FORMAT_TYPE_USHORT; | ||
928 | } else { | ||
929 | if (sign) | ||
930 | spec->type = FORMAT_TYPE_INT; | ||
931 | else | ||
932 | spec->type = FORMAT_TYPE_UINT; | ||
699 | } | 933 | } |
700 | return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags); | 934 | |
935 | return ++fmt - start; | ||
701 | } | 936 | } |
702 | 937 | ||
703 | /** | 938 | /** |
@@ -726,18 +961,9 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field | |||
726 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | 961 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
727 | { | 962 | { |
728 | unsigned long long num; | 963 | unsigned long long num; |
729 | int base; | ||
730 | char *str, *end, c; | 964 | char *str, *end, c; |
731 | 965 | int read; | |
732 | int flags; /* flags to number() */ | 966 | struct printf_spec spec = {0}; |
733 | |||
734 | int field_width; /* width of output field */ | ||
735 | int precision; /* min. # of digits for integers; max | ||
736 | number of chars for from string */ | ||
737 | int qualifier; /* 'h', 'l', or 'L' for integer fields */ | ||
738 | /* 'z' support added 23/7/1999 S.H. */ | ||
739 | /* 'z' changed to 'Z' --davidm 1/25/99 */ | ||
740 | /* 't' added for ptrdiff_t */ | ||
741 | 967 | ||
742 | /* Reject out-of-range values early. Large positive sizes are | 968 | /* Reject out-of-range values early. Large positive sizes are |
743 | used for unknown buffer sizes. */ | 969 | used for unknown buffer sizes. */ |
@@ -758,184 +984,144 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | |||
758 | size = end - buf; | 984 | size = end - buf; |
759 | } | 985 | } |
760 | 986 | ||
761 | for (; *fmt ; ++fmt) { | 987 | while (*fmt) { |
762 | if (*fmt != '%') { | 988 | const char *old_fmt = fmt; |
763 | if (str < end) | ||
764 | *str = *fmt; | ||
765 | ++str; | ||
766 | continue; | ||
767 | } | ||
768 | 989 | ||
769 | /* process flags */ | 990 | read = format_decode(fmt, &spec); |
770 | flags = 0; | ||
771 | repeat: | ||
772 | ++fmt; /* this also skips first '%' */ | ||
773 | switch (*fmt) { | ||
774 | case '-': flags |= LEFT; goto repeat; | ||
775 | case '+': flags |= PLUS; goto repeat; | ||
776 | case ' ': flags |= SPACE; goto repeat; | ||
777 | case '#': flags |= SPECIAL; goto repeat; | ||
778 | case '0': flags |= ZEROPAD; goto repeat; | ||
779 | } | ||
780 | 991 | ||
781 | /* get field width */ | 992 | fmt += read; |
782 | field_width = -1; | ||
783 | if (isdigit(*fmt)) | ||
784 | field_width = skip_atoi(&fmt); | ||
785 | else if (*fmt == '*') { | ||
786 | ++fmt; | ||
787 | /* it's the next argument */ | ||
788 | field_width = va_arg(args, int); | ||
789 | if (field_width < 0) { | ||
790 | field_width = -field_width; | ||
791 | flags |= LEFT; | ||
792 | } | ||
793 | } | ||
794 | 993 | ||
795 | /* get the precision */ | 994 | switch (spec.type) { |
796 | precision = -1; | 995 | case FORMAT_TYPE_NONE: { |
797 | if (*fmt == '.') { | 996 | int copy = read; |
798 | ++fmt; | 997 | if (str < end) { |
799 | if (isdigit(*fmt)) | 998 | if (copy > end - str) |
800 | precision = skip_atoi(&fmt); | 999 | copy = end - str; |
801 | else if (*fmt == '*') { | 1000 | memcpy(str, old_fmt, copy); |
802 | ++fmt; | ||
803 | /* it's the next argument */ | ||
804 | precision = va_arg(args, int); | ||
805 | } | 1001 | } |
806 | if (precision < 0) | 1002 | str += read; |
807 | precision = 0; | 1003 | break; |
808 | } | 1004 | } |
809 | 1005 | ||
810 | /* get the conversion qualifier */ | 1006 | case FORMAT_TYPE_WITDH: |
811 | qualifier = -1; | 1007 | spec.field_width = va_arg(args, int); |
812 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || | 1008 | break; |
813 | *fmt =='Z' || *fmt == 'z' || *fmt == 't') { | ||
814 | qualifier = *fmt; | ||
815 | ++fmt; | ||
816 | if (qualifier == 'l' && *fmt == 'l') { | ||
817 | qualifier = 'L'; | ||
818 | ++fmt; | ||
819 | } | ||
820 | } | ||
821 | 1009 | ||
822 | /* default base */ | 1010 | case FORMAT_TYPE_PRECISION: |
823 | base = 10; | 1011 | spec.precision = va_arg(args, int); |
1012 | break; | ||
824 | 1013 | ||
825 | switch (*fmt) { | 1014 | case FORMAT_TYPE_CHAR: |
826 | case 'c': | 1015 | if (!(spec.flags & LEFT)) { |
827 | if (!(flags & LEFT)) { | 1016 | while (--spec.field_width > 0) { |
828 | while (--field_width > 0) { | ||
829 | if (str < end) | ||
830 | *str = ' '; | ||
831 | ++str; | ||
832 | } | ||
833 | } | ||
834 | c = (unsigned char) va_arg(args, int); | ||
835 | if (str < end) | ||
836 | *str = c; | ||
837 | ++str; | ||
838 | while (--field_width > 0) { | ||
839 | if (str < end) | 1017 | if (str < end) |
840 | *str = ' '; | 1018 | *str = ' '; |
841 | ++str; | 1019 | ++str; |
842 | } | ||
843 | continue; | ||
844 | |||
845 | case 's': | ||
846 | str = string(str, end, va_arg(args, char *), field_width, precision, flags); | ||
847 | continue; | ||
848 | |||
849 | case 'p': | ||
850 | str = pointer(fmt+1, str, end, | ||
851 | va_arg(args, void *), | ||
852 | field_width, precision, flags); | ||
853 | /* Skip all alphanumeric pointer suffixes */ | ||
854 | while (isalnum(fmt[1])) | ||
855 | fmt++; | ||
856 | continue; | ||
857 | |||
858 | case 'n': | ||
859 | /* FIXME: | ||
860 | * What does C99 say about the overflow case here? */ | ||
861 | if (qualifier == 'l') { | ||
862 | long * ip = va_arg(args, long *); | ||
863 | *ip = (str - buf); | ||
864 | } else if (qualifier == 'Z' || qualifier == 'z') { | ||
865 | size_t * ip = va_arg(args, size_t *); | ||
866 | *ip = (str - buf); | ||
867 | } else { | ||
868 | int * ip = va_arg(args, int *); | ||
869 | *ip = (str - buf); | ||
870 | } | ||
871 | continue; | ||
872 | 1020 | ||
873 | case '%': | 1021 | } |
1022 | } | ||
1023 | c = (unsigned char) va_arg(args, int); | ||
1024 | if (str < end) | ||
1025 | *str = c; | ||
1026 | ++str; | ||
1027 | while (--spec.field_width > 0) { | ||
874 | if (str < end) | 1028 | if (str < end) |
875 | *str = '%'; | 1029 | *str = ' '; |
876 | ++str; | 1030 | ++str; |
877 | continue; | 1031 | } |
1032 | break; | ||
878 | 1033 | ||
879 | /* integer number formats - set up the flags and "break" */ | 1034 | case FORMAT_TYPE_STR: |
880 | case 'o': | 1035 | str = string(str, end, va_arg(args, char *), spec); |
881 | base = 8; | 1036 | break; |
882 | break; | ||
883 | 1037 | ||
884 | case 'x': | 1038 | case FORMAT_TYPE_PTR: |
885 | flags |= SMALL; | 1039 | str = pointer(fmt+1, str, end, va_arg(args, void *), |
886 | case 'X': | 1040 | spec); |
887 | base = 16; | 1041 | while (isalnum(*fmt)) |
888 | break; | 1042 | fmt++; |
1043 | break; | ||
889 | 1044 | ||
890 | case 'd': | 1045 | case FORMAT_TYPE_PERCENT_CHAR: |
891 | case 'i': | 1046 | if (str < end) |
892 | flags |= SIGN; | 1047 | *str = '%'; |
893 | case 'u': | 1048 | ++str; |
894 | break; | 1049 | break; |
895 | 1050 | ||
896 | default: | 1051 | case FORMAT_TYPE_INVALID: |
1052 | if (str < end) | ||
1053 | *str = '%'; | ||
1054 | ++str; | ||
1055 | if (*fmt) { | ||
897 | if (str < end) | 1056 | if (str < end) |
898 | *str = '%'; | 1057 | *str = *fmt; |
899 | ++str; | 1058 | ++str; |
900 | if (*fmt) { | 1059 | } else { |
901 | if (str < end) | 1060 | --fmt; |
902 | *str = *fmt; | 1061 | } |
903 | ++str; | 1062 | break; |
904 | } else { | 1063 | |
905 | --fmt; | 1064 | case FORMAT_TYPE_NRCHARS: { |
906 | } | 1065 | int qualifier = spec.qualifier; |
907 | continue; | 1066 | |
1067 | if (qualifier == 'l') { | ||
1068 | long *ip = va_arg(args, long *); | ||
1069 | *ip = (str - buf); | ||
1070 | } else if (qualifier == 'Z' || | ||
1071 | qualifier == 'z') { | ||
1072 | size_t *ip = va_arg(args, size_t *); | ||
1073 | *ip = (str - buf); | ||
1074 | } else { | ||
1075 | int *ip = va_arg(args, int *); | ||
1076 | *ip = (str - buf); | ||
1077 | } | ||
1078 | break; | ||
908 | } | 1079 | } |
909 | if (qualifier == 'L') | 1080 | |
910 | num = va_arg(args, long long); | 1081 | default: |
911 | else if (qualifier == 'l') { | 1082 | switch (spec.type) { |
912 | num = va_arg(args, unsigned long); | 1083 | case FORMAT_TYPE_LONG_LONG: |
913 | if (flags & SIGN) | 1084 | num = va_arg(args, long long); |
914 | num = (signed long) num; | 1085 | break; |
915 | } else if (qualifier == 'Z' || qualifier == 'z') { | 1086 | case FORMAT_TYPE_ULONG: |
916 | num = va_arg(args, size_t); | 1087 | num = va_arg(args, unsigned long); |
917 | } else if (qualifier == 't') { | 1088 | break; |
918 | num = va_arg(args, ptrdiff_t); | 1089 | case FORMAT_TYPE_LONG: |
919 | } else if (qualifier == 'h') { | 1090 | num = va_arg(args, long); |
920 | num = (unsigned short) va_arg(args, int); | 1091 | break; |
921 | if (flags & SIGN) | 1092 | case FORMAT_TYPE_SIZE_T: |
922 | num = (signed short) num; | 1093 | num = va_arg(args, size_t); |
923 | } else { | 1094 | break; |
924 | num = va_arg(args, unsigned int); | 1095 | case FORMAT_TYPE_PTRDIFF: |
925 | if (flags & SIGN) | 1096 | num = va_arg(args, ptrdiff_t); |
926 | num = (signed int) num; | 1097 | break; |
1098 | case FORMAT_TYPE_USHORT: | ||
1099 | num = (unsigned short) va_arg(args, int); | ||
1100 | break; | ||
1101 | case FORMAT_TYPE_SHORT: | ||
1102 | num = (short) va_arg(args, int); | ||
1103 | break; | ||
1104 | case FORMAT_TYPE_UINT: | ||
1105 | num = va_arg(args, unsigned int); | ||
1106 | break; | ||
1107 | default: | ||
1108 | num = va_arg(args, unsigned int); | ||
1109 | } | ||
1110 | |||
1111 | str = number(str, end, num, spec); | ||
927 | } | 1112 | } |
928 | str = number(str, end, num, base, | ||
929 | field_width, precision, flags); | ||
930 | } | 1113 | } |
1114 | |||
931 | if (size > 0) { | 1115 | if (size > 0) { |
932 | if (str < end) | 1116 | if (str < end) |
933 | *str = '\0'; | 1117 | *str = '\0'; |
934 | else | 1118 | else |
935 | end[-1] = '\0'; | 1119 | end[-1] = '\0'; |
936 | } | 1120 | } |
1121 | |||
937 | /* the trailing null byte doesn't count towards the total */ | 1122 | /* the trailing null byte doesn't count towards the total */ |
938 | return str-buf; | 1123 | return str-buf; |
1124 | |||
939 | } | 1125 | } |
940 | EXPORT_SYMBOL(vsnprintf); | 1126 | EXPORT_SYMBOL(vsnprintf); |
941 | 1127 | ||
@@ -1058,6 +1244,372 @@ int sprintf(char * buf, const char *fmt, ...) | |||
1058 | } | 1244 | } |
1059 | EXPORT_SYMBOL(sprintf); | 1245 | EXPORT_SYMBOL(sprintf); |
1060 | 1246 | ||
1247 | #ifdef CONFIG_BINARY_PRINTF | ||
1248 | /* | ||
1249 | * bprintf service: | ||
1250 | * vbin_printf() - VA arguments to binary data | ||
1251 | * bstr_printf() - Binary data to text string | ||
1252 | */ | ||
1253 | |||
1254 | /** | ||
1255 | * vbin_printf - Parse a format string and place args' binary value in a buffer | ||
1256 | * @bin_buf: The buffer to place args' binary value | ||
1257 | * @size: The size of the buffer(by words(32bits), not characters) | ||
1258 | * @fmt: The format string to use | ||
1259 | * @args: Arguments for the format string | ||
1260 | * | ||
1261 | * The format follows C99 vsnprintf, except %n is ignored, and its argument | ||
1262 | * is skiped. | ||
1263 | * | ||
1264 | * The return value is the number of words(32bits) which would be generated for | ||
1265 | * the given input. | ||
1266 | * | ||
1267 | * NOTE: | ||
1268 | * If the return value is greater than @size, the resulting bin_buf is NOT | ||
1269 | * valid for bstr_printf(). | ||
1270 | */ | ||
1271 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) | ||
1272 | { | ||
1273 | struct printf_spec spec = {0}; | ||
1274 | char *str, *end; | ||
1275 | int read; | ||
1276 | |||
1277 | str = (char *)bin_buf; | ||
1278 | end = (char *)(bin_buf + size); | ||
1279 | |||
1280 | #define save_arg(type) \ | ||
1281 | do { \ | ||
1282 | if (sizeof(type) == 8) { \ | ||
1283 | unsigned long long value; \ | ||
1284 | str = PTR_ALIGN(str, sizeof(u32)); \ | ||
1285 | value = va_arg(args, unsigned long long); \ | ||
1286 | if (str + sizeof(type) <= end) { \ | ||
1287 | *(u32 *)str = *(u32 *)&value; \ | ||
1288 | *(u32 *)(str + 4) = *((u32 *)&value + 1); \ | ||
1289 | } \ | ||
1290 | } else { \ | ||
1291 | unsigned long value; \ | ||
1292 | str = PTR_ALIGN(str, sizeof(type)); \ | ||
1293 | value = va_arg(args, int); \ | ||
1294 | if (str + sizeof(type) <= end) \ | ||
1295 | *(typeof(type) *)str = (type)value; \ | ||
1296 | } \ | ||
1297 | str += sizeof(type); \ | ||
1298 | } while (0) | ||
1299 | |||
1300 | |||
1301 | while (*fmt) { | ||
1302 | read = format_decode(fmt, &spec); | ||
1303 | |||
1304 | fmt += read; | ||
1305 | |||
1306 | switch (spec.type) { | ||
1307 | case FORMAT_TYPE_NONE: | ||
1308 | break; | ||
1309 | |||
1310 | case FORMAT_TYPE_WITDH: | ||
1311 | case FORMAT_TYPE_PRECISION: | ||
1312 | save_arg(int); | ||
1313 | break; | ||
1314 | |||
1315 | case FORMAT_TYPE_CHAR: | ||
1316 | save_arg(char); | ||
1317 | break; | ||
1318 | |||
1319 | case FORMAT_TYPE_STR: { | ||
1320 | const char *save_str = va_arg(args, char *); | ||
1321 | size_t len; | ||
1322 | if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE | ||
1323 | || (unsigned long)save_str < PAGE_SIZE) | ||
1324 | save_str = "<NULL>"; | ||
1325 | len = strlen(save_str); | ||
1326 | if (str + len + 1 < end) | ||
1327 | memcpy(str, save_str, len + 1); | ||
1328 | str += len + 1; | ||
1329 | break; | ||
1330 | } | ||
1331 | |||
1332 | case FORMAT_TYPE_PTR: | ||
1333 | save_arg(void *); | ||
1334 | /* skip all alphanumeric pointer suffixes */ | ||
1335 | while (isalnum(*fmt)) | ||
1336 | fmt++; | ||
1337 | break; | ||
1338 | |||
1339 | case FORMAT_TYPE_PERCENT_CHAR: | ||
1340 | break; | ||
1341 | |||
1342 | case FORMAT_TYPE_INVALID: | ||
1343 | if (!*fmt) | ||
1344 | --fmt; | ||
1345 | break; | ||
1346 | |||
1347 | case FORMAT_TYPE_NRCHARS: { | ||
1348 | /* skip %n 's argument */ | ||
1349 | int qualifier = spec.qualifier; | ||
1350 | void *skip_arg; | ||
1351 | if (qualifier == 'l') | ||
1352 | skip_arg = va_arg(args, long *); | ||
1353 | else if (qualifier == 'Z' || qualifier == 'z') | ||
1354 | skip_arg = va_arg(args, size_t *); | ||
1355 | else | ||
1356 | skip_arg = va_arg(args, int *); | ||
1357 | break; | ||
1358 | } | ||
1359 | |||
1360 | default: | ||
1361 | switch (spec.type) { | ||
1362 | |||
1363 | case FORMAT_TYPE_LONG_LONG: | ||
1364 | save_arg(long long); | ||
1365 | break; | ||
1366 | case FORMAT_TYPE_ULONG: | ||
1367 | case FORMAT_TYPE_LONG: | ||
1368 | save_arg(unsigned long); | ||
1369 | break; | ||
1370 | case FORMAT_TYPE_SIZE_T: | ||
1371 | save_arg(size_t); | ||
1372 | break; | ||
1373 | case FORMAT_TYPE_PTRDIFF: | ||
1374 | save_arg(ptrdiff_t); | ||
1375 | break; | ||
1376 | case FORMAT_TYPE_USHORT: | ||
1377 | case FORMAT_TYPE_SHORT: | ||
1378 | save_arg(short); | ||
1379 | break; | ||
1380 | default: | ||
1381 | save_arg(int); | ||
1382 | } | ||
1383 | } | ||
1384 | } | ||
1385 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; | ||
1386 | |||
1387 | #undef save_arg | ||
1388 | } | ||
1389 | EXPORT_SYMBOL_GPL(vbin_printf); | ||
1390 | |||
1391 | /** | ||
1392 | * bstr_printf - Format a string from binary arguments and place it in a buffer | ||
1393 | * @buf: The buffer to place the result into | ||
1394 | * @size: The size of the buffer, including the trailing null space | ||
1395 | * @fmt: The format string to use | ||
1396 | * @bin_buf: Binary arguments for the format string | ||
1397 | * | ||
1398 | * This function like C99 vsnprintf, but the difference is that vsnprintf gets | ||
1399 | * arguments from stack, and bstr_printf gets arguments from @bin_buf which is | ||
1400 | * a binary buffer that generated by vbin_printf. | ||
1401 | * | ||
1402 | * The format follows C99 vsnprintf, but has some extensions: | ||
1403 | * %pS output the name of a text symbol | ||
1404 | * %pF output the name of a function pointer | ||
1405 | * %pR output the address range in a struct resource | ||
1406 | * %n is ignored | ||
1407 | * | ||
1408 | * The return value is the number of characters which would | ||
1409 | * be generated for the given input, excluding the trailing | ||
1410 | * '\0', as per ISO C99. If you want to have the exact | ||
1411 | * number of characters written into @buf as return value | ||
1412 | * (not including the trailing '\0'), use vscnprintf(). If the | ||
1413 | * return is greater than or equal to @size, the resulting | ||
1414 | * string is truncated. | ||
1415 | */ | ||
1416 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | ||
1417 | { | ||
1418 | unsigned long long num; | ||
1419 | char *str, *end, c; | ||
1420 | const char *args = (const char *)bin_buf; | ||
1421 | |||
1422 | struct printf_spec spec = {0}; | ||
1423 | |||
1424 | if (unlikely((int) size < 0)) { | ||
1425 | /* There can be only one.. */ | ||
1426 | static char warn = 1; | ||
1427 | WARN_ON(warn); | ||
1428 | warn = 0; | ||
1429 | return 0; | ||
1430 | } | ||
1431 | |||
1432 | str = buf; | ||
1433 | end = buf + size; | ||
1434 | |||
1435 | #define get_arg(type) \ | ||
1436 | ({ \ | ||
1437 | typeof(type) value; \ | ||
1438 | if (sizeof(type) == 8) { \ | ||
1439 | args = PTR_ALIGN(args, sizeof(u32)); \ | ||
1440 | *(u32 *)&value = *(u32 *)args; \ | ||
1441 | *((u32 *)&value + 1) = *(u32 *)(args + 4); \ | ||
1442 | } else { \ | ||
1443 | args = PTR_ALIGN(args, sizeof(type)); \ | ||
1444 | value = *(typeof(type) *)args; \ | ||
1445 | } \ | ||
1446 | args += sizeof(type); \ | ||
1447 | value; \ | ||
1448 | }) | ||
1449 | |||
1450 | /* Make sure end is always >= buf */ | ||
1451 | if (end < buf) { | ||
1452 | end = ((void *)-1); | ||
1453 | size = end - buf; | ||
1454 | } | ||
1455 | |||
1456 | while (*fmt) { | ||
1457 | int read; | ||
1458 | const char *old_fmt = fmt; | ||
1459 | |||
1460 | read = format_decode(fmt, &spec); | ||
1461 | |||
1462 | fmt += read; | ||
1463 | |||
1464 | switch (spec.type) { | ||
1465 | case FORMAT_TYPE_NONE: { | ||
1466 | int copy = read; | ||
1467 | if (str < end) { | ||
1468 | if (copy > end - str) | ||
1469 | copy = end - str; | ||
1470 | memcpy(str, old_fmt, copy); | ||
1471 | } | ||
1472 | str += read; | ||
1473 | break; | ||
1474 | } | ||
1475 | |||
1476 | case FORMAT_TYPE_WITDH: | ||
1477 | spec.field_width = get_arg(int); | ||
1478 | break; | ||
1479 | |||
1480 | case FORMAT_TYPE_PRECISION: | ||
1481 | spec.precision = get_arg(int); | ||
1482 | break; | ||
1483 | |||
1484 | case FORMAT_TYPE_CHAR: | ||
1485 | if (!(spec.flags & LEFT)) { | ||
1486 | while (--spec.field_width > 0) { | ||
1487 | if (str < end) | ||
1488 | *str = ' '; | ||
1489 | ++str; | ||
1490 | } | ||
1491 | } | ||
1492 | c = (unsigned char) get_arg(char); | ||
1493 | if (str < end) | ||
1494 | *str = c; | ||
1495 | ++str; | ||
1496 | while (--spec.field_width > 0) { | ||
1497 | if (str < end) | ||
1498 | *str = ' '; | ||
1499 | ++str; | ||
1500 | } | ||
1501 | break; | ||
1502 | |||
1503 | case FORMAT_TYPE_STR: { | ||
1504 | const char *str_arg = args; | ||
1505 | size_t len = strlen(str_arg); | ||
1506 | args += len + 1; | ||
1507 | str = string(str, end, (char *)str_arg, spec); | ||
1508 | break; | ||
1509 | } | ||
1510 | |||
1511 | case FORMAT_TYPE_PTR: | ||
1512 | str = pointer(fmt+1, str, end, get_arg(void *), spec); | ||
1513 | while (isalnum(*fmt)) | ||
1514 | fmt++; | ||
1515 | break; | ||
1516 | |||
1517 | case FORMAT_TYPE_PERCENT_CHAR: | ||
1518 | if (str < end) | ||
1519 | *str = '%'; | ||
1520 | ++str; | ||
1521 | break; | ||
1522 | |||
1523 | case FORMAT_TYPE_INVALID: | ||
1524 | if (str < end) | ||
1525 | *str = '%'; | ||
1526 | ++str; | ||
1527 | if (*fmt) { | ||
1528 | if (str < end) | ||
1529 | *str = *fmt; | ||
1530 | ++str; | ||
1531 | } else { | ||
1532 | --fmt; | ||
1533 | } | ||
1534 | break; | ||
1535 | |||
1536 | case FORMAT_TYPE_NRCHARS: | ||
1537 | /* skip */ | ||
1538 | break; | ||
1539 | |||
1540 | default: | ||
1541 | switch (spec.type) { | ||
1542 | |||
1543 | case FORMAT_TYPE_LONG_LONG: | ||
1544 | num = get_arg(long long); | ||
1545 | break; | ||
1546 | case FORMAT_TYPE_ULONG: | ||
1547 | num = get_arg(unsigned long); | ||
1548 | break; | ||
1549 | case FORMAT_TYPE_LONG: | ||
1550 | num = get_arg(unsigned long); | ||
1551 | break; | ||
1552 | case FORMAT_TYPE_SIZE_T: | ||
1553 | num = get_arg(size_t); | ||
1554 | break; | ||
1555 | case FORMAT_TYPE_PTRDIFF: | ||
1556 | num = get_arg(ptrdiff_t); | ||
1557 | break; | ||
1558 | case FORMAT_TYPE_USHORT: | ||
1559 | num = get_arg(unsigned short); | ||
1560 | break; | ||
1561 | case FORMAT_TYPE_SHORT: | ||
1562 | num = get_arg(short); | ||
1563 | break; | ||
1564 | case FORMAT_TYPE_UINT: | ||
1565 | num = get_arg(unsigned int); | ||
1566 | break; | ||
1567 | default: | ||
1568 | num = get_arg(int); | ||
1569 | } | ||
1570 | |||
1571 | str = number(str, end, num, spec); | ||
1572 | } | ||
1573 | } | ||
1574 | |||
1575 | if (size > 0) { | ||
1576 | if (str < end) | ||
1577 | *str = '\0'; | ||
1578 | else | ||
1579 | end[-1] = '\0'; | ||
1580 | } | ||
1581 | |||
1582 | #undef get_arg | ||
1583 | |||
1584 | /* the trailing null byte doesn't count towards the total */ | ||
1585 | return str - buf; | ||
1586 | } | ||
1587 | EXPORT_SYMBOL_GPL(bstr_printf); | ||
1588 | |||
1589 | /** | ||
1590 | * bprintf - Parse a format string and place args' binary value in a buffer | ||
1591 | * @bin_buf: The buffer to place args' binary value | ||
1592 | * @size: The size of the buffer(by words(32bits), not characters) | ||
1593 | * @fmt: The format string to use | ||
1594 | * @...: Arguments for the format string | ||
1595 | * | ||
1596 | * The function returns the number of words(u32) written | ||
1597 | * into @bin_buf. | ||
1598 | */ | ||
1599 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) | ||
1600 | { | ||
1601 | va_list args; | ||
1602 | int ret; | ||
1603 | |||
1604 | va_start(args, fmt); | ||
1605 | ret = vbin_printf(bin_buf, size, fmt, args); | ||
1606 | va_end(args); | ||
1607 | return ret; | ||
1608 | } | ||
1609 | EXPORT_SYMBOL_GPL(bprintf); | ||
1610 | |||
1611 | #endif /* CONFIG_BINARY_PRINTF */ | ||
1612 | |||
1061 | /** | 1613 | /** |
1062 | * vsscanf - Unformat a buffer into a list of arguments | 1614 | * vsscanf - Unformat a buffer into a list of arguments |
1063 | * @buf: input buffer | 1615 | * @buf: input buffer |