aboutsummaryrefslogtreecommitdiffstats
path: root/lib/vsprintf.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/vsprintf.c')
-rw-r--r--lib/vsprintf.c119
1 files changed, 80 insertions, 39 deletions
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 3b8aeec4e327..0d461c7c14db 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -381,8 +381,8 @@ static noinline char *put_dec(char *buf, unsigned long long num)
381#define PLUS 4 /* show plus */ 381#define PLUS 4 /* show plus */
382#define SPACE 8 /* space if plus */ 382#define SPACE 8 /* space if plus */
383#define LEFT 16 /* left justified */ 383#define LEFT 16 /* left justified */
384#define SMALL 32 /* Must be 32 == 0x20 */ 384#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
385#define SPECIAL 64 /* 0x */ 385#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
386 386
387enum format_type { 387enum format_type {
388 FORMAT_TYPE_NONE, /* Just a string part */ 388 FORMAT_TYPE_NONE, /* Just a string part */
@@ -408,12 +408,12 @@ enum format_type {
408}; 408};
409 409
410struct printf_spec { 410struct printf_spec {
411 enum format_type type; 411 u16 type;
412 int flags; /* flags to number() */ 412 s16 field_width; /* width of output field */
413 int field_width; /* width of output field */ 413 u8 flags; /* flags to number() */
414 int base; 414 u8 base;
415 int precision; /* # of digits/chars */ 415 s8 precision; /* # of digits/chars */
416 int qualifier; 416 u8 qualifier;
417}; 417};
418 418
419static char *number(char *buf, char *end, unsigned long long num, 419static char *number(char *buf, char *end, unsigned long long num,
@@ -597,22 +597,29 @@ static char *resource_string(char *buf, char *end, struct resource *res,
597#ifndef MEM_RSRC_PRINTK_SIZE 597#ifndef MEM_RSRC_PRINTK_SIZE
598#define MEM_RSRC_PRINTK_SIZE 10 598#define MEM_RSRC_PRINTK_SIZE 10
599#endif 599#endif
600 struct printf_spec hex_spec = { 600 static const struct printf_spec io_spec = {
601 .base = 16, 601 .base = 16,
602 .field_width = IO_RSRC_PRINTK_SIZE,
602 .precision = -1, 603 .precision = -1,
603 .flags = SPECIAL | SMALL | ZEROPAD, 604 .flags = SPECIAL | SMALL | ZEROPAD,
604 }; 605 };
605 struct printf_spec dec_spec = { 606 static const struct printf_spec mem_spec = {
607 .base = 16,
608 .field_width = MEM_RSRC_PRINTK_SIZE,
609 .precision = -1,
610 .flags = SPECIAL | SMALL | ZEROPAD,
611 };
612 static const struct printf_spec dec_spec = {
606 .base = 10, 613 .base = 10,
607 .precision = -1, 614 .precision = -1,
608 .flags = 0, 615 .flags = 0,
609 }; 616 };
610 struct printf_spec str_spec = { 617 static const struct printf_spec str_spec = {
611 .field_width = -1, 618 .field_width = -1,
612 .precision = 10, 619 .precision = 10,
613 .flags = LEFT, 620 .flags = LEFT,
614 }; 621 };
615 struct printf_spec flag_spec = { 622 static const struct printf_spec flag_spec = {
616 .base = 16, 623 .base = 16,
617 .precision = -1, 624 .precision = -1,
618 .flags = SPECIAL | SMALL, 625 .flags = SPECIAL | SMALL,
@@ -628,35 +635,31 @@ static char *resource_string(char *buf, char *end, struct resource *res,
628 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 635 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
629 636
630 char *p = sym, *pend = sym + sizeof(sym); 637 char *p = sym, *pend = sym + sizeof(sym);
631 int size = -1, addr = 0;
632 int decode = (fmt[0] == 'R') ? 1 : 0; 638 int decode = (fmt[0] == 'R') ? 1 : 0;
633 639 const struct printf_spec *specp;
634 if (res->flags & IORESOURCE_IO) {
635 size = IO_RSRC_PRINTK_SIZE;
636 addr = 1;
637 } else if (res->flags & IORESOURCE_MEM) {
638 size = MEM_RSRC_PRINTK_SIZE;
639 addr = 1;
640 }
641 640
642 *p++ = '['; 641 *p++ = '[';
643 if (res->flags & IORESOURCE_IO) 642 if (res->flags & IORESOURCE_IO) {
644 p = string(p, pend, "io ", str_spec); 643 p = string(p, pend, "io ", str_spec);
645 else if (res->flags & IORESOURCE_MEM) 644 specp = &io_spec;
645 } else if (res->flags & IORESOURCE_MEM) {
646 p = string(p, pend, "mem ", str_spec); 646 p = string(p, pend, "mem ", str_spec);
647 else if (res->flags & IORESOURCE_IRQ) 647 specp = &mem_spec;
648 } else if (res->flags & IORESOURCE_IRQ) {
648 p = string(p, pend, "irq ", str_spec); 649 p = string(p, pend, "irq ", str_spec);
649 else if (res->flags & IORESOURCE_DMA) 650 specp = &dec_spec;
651 } else if (res->flags & IORESOURCE_DMA) {
650 p = string(p, pend, "dma ", str_spec); 652 p = string(p, pend, "dma ", str_spec);
651 else { 653 specp = &dec_spec;
654 } else {
652 p = string(p, pend, "??? ", str_spec); 655 p = string(p, pend, "??? ", str_spec);
656 specp = &mem_spec;
653 decode = 0; 657 decode = 0;
654 } 658 }
655 hex_spec.field_width = size; 659 p = number(p, pend, res->start, *specp);
656 p = number(p, pend, res->start, addr ? hex_spec : dec_spec);
657 if (res->start != res->end) { 660 if (res->start != res->end) {
658 *p++ = '-'; 661 *p++ = '-';
659 p = number(p, pend, res->end, addr ? hex_spec : dec_spec); 662 p = number(p, pend, res->end, *specp);
660 } 663 }
661 if (decode) { 664 if (decode) {
662 if (res->flags & IORESOURCE_MEM_64) 665 if (res->flags & IORESOURCE_MEM_64)
@@ -681,24 +684,55 @@ static char *mac_address_string(char *buf, char *end, u8 *addr,
681 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 684 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
682 char *p = mac_addr; 685 char *p = mac_addr;
683 int i; 686 int i;
687 char separator;
688
689 if (fmt[1] == 'F') { /* FDDI canonical format */
690 separator = '-';
691 } else {
692 separator = ':';
693 }
684 694
685 for (i = 0; i < 6; i++) { 695 for (i = 0; i < 6; i++) {
686 p = pack_hex_byte(p, addr[i]); 696 p = pack_hex_byte(p, addr[i]);
687 if (fmt[0] == 'M' && i != 5) 697 if (fmt[0] == 'M' && i != 5)
688 *p++ = ':'; 698 *p++ = separator;
689 } 699 }
690 *p = '\0'; 700 *p = '\0';
691 701
692 return string(buf, end, mac_addr, spec); 702 return string(buf, end, mac_addr, spec);
693} 703}
694 704
695static char *ip4_string(char *p, const u8 *addr, bool leading_zeros) 705static char *ip4_string(char *p, const u8 *addr, const char *fmt)
696{ 706{
697 int i; 707 int i;
698 708 bool leading_zeros = (fmt[0] == 'i');
709 int index;
710 int step;
711
712 switch (fmt[2]) {
713 case 'h':
714#ifdef __BIG_ENDIAN
715 index = 0;
716 step = 1;
717#else
718 index = 3;
719 step = -1;
720#endif
721 break;
722 case 'l':
723 index = 3;
724 step = -1;
725 break;
726 case 'n':
727 case 'b':
728 default:
729 index = 0;
730 step = 1;
731 break;
732 }
699 for (i = 0; i < 4; i++) { 733 for (i = 0; i < 4; i++) {
700 char temp[3]; /* hold each IP quad in reverse order */ 734 char temp[3]; /* hold each IP quad in reverse order */
701 int digits = put_dec_trunc(temp, addr[i]) - temp; 735 int digits = put_dec_trunc(temp, addr[index]) - temp;
702 if (leading_zeros) { 736 if (leading_zeros) {
703 if (digits < 3) 737 if (digits < 3)
704 *p++ = '0'; 738 *p++ = '0';
@@ -710,6 +744,7 @@ static char *ip4_string(char *p, const u8 *addr, bool leading_zeros)
710 *p++ = temp[digits]; 744 *p++ = temp[digits];
711 if (i < 3) 745 if (i < 3)
712 *p++ = '.'; 746 *p++ = '.';
747 index += step;
713 } 748 }
714 *p = '\0'; 749 *p = '\0';
715 750
@@ -789,7 +824,7 @@ static char *ip6_compressed_string(char *p, const char *addr)
789 if (useIPv4) { 824 if (useIPv4) {
790 if (needcolon) 825 if (needcolon)
791 *p++ = ':'; 826 *p++ = ':';
792 p = ip4_string(p, &in6.s6_addr[12], false); 827 p = ip4_string(p, &in6.s6_addr[12], "I4");
793 } 828 }
794 *p = '\0'; 829 *p = '\0';
795 830
@@ -829,7 +864,7 @@ static char *ip4_addr_string(char *buf, char *end, const u8 *addr,
829{ 864{
830 char ip4_addr[sizeof("255.255.255.255")]; 865 char ip4_addr[sizeof("255.255.255.255")];
831 866
832 ip4_string(ip4_addr, addr, fmt[0] == 'i'); 867 ip4_string(ip4_addr, addr, fmt);
833 868
834 return string(buf, end, ip4_addr, spec); 869 return string(buf, end, ip4_addr, spec);
835} 870}
@@ -896,12 +931,15 @@ static char *uuid_string(char *buf, char *end, const u8 *addr,
896 * - 'M' For a 6-byte MAC address, it prints the address in the 931 * - 'M' For a 6-byte MAC address, it prints the address in the
897 * usual colon-separated hex notation 932 * usual colon-separated hex notation
898 * - 'm' For a 6-byte MAC address, it prints the hex address without colons 933 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
934 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
935 * with a dash-separated hex notation
899 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 936 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
900 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 937 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
901 * IPv6 uses colon separated network-order 16 bit hex with leading 0's 938 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
902 * - 'i' [46] for 'raw' IPv4/IPv6 addresses 939 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
903 * IPv6 omits the colons (01020304...0f) 940 * IPv6 omits the colons (01020304...0f)
904 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 941 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
942 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
905 * - 'I6c' for IPv6 addresses printed as specified by 943 * - 'I6c' for IPv6 addresses printed as specified by
906 * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 944 * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00
907 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 945 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
@@ -939,6 +977,7 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
939 return resource_string(buf, end, ptr, spec, fmt); 977 return resource_string(buf, end, ptr, spec, fmt);
940 case 'M': /* Colon separated: 00:01:02:03:04:05 */ 978 case 'M': /* Colon separated: 00:01:02:03:04:05 */
941 case 'm': /* Contiguous: 000102030405 */ 979 case 'm': /* Contiguous: 000102030405 */
980 /* [mM]F (FDDI, bit reversed) */
942 return mac_address_string(buf, end, ptr, spec, fmt); 981 return mac_address_string(buf, end, ptr, spec, fmt);
943 case 'I': /* Formatted IP supported 982 case 'I': /* Formatted IP supported
944 * 4: 1.2.3.4 983 * 4: 1.2.3.4
@@ -1297,7 +1336,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1297 break; 1336 break;
1298 1337
1299 case FORMAT_TYPE_NRCHARS: { 1338 case FORMAT_TYPE_NRCHARS: {
1300 int qualifier = spec.qualifier; 1339 u8 qualifier = spec.qualifier;
1301 1340
1302 if (qualifier == 'l') { 1341 if (qualifier == 'l') {
1303 long *ip = va_arg(args, long *); 1342 long *ip = va_arg(args, long *);
@@ -1583,7 +1622,7 @@ do { \
1583 1622
1584 case FORMAT_TYPE_NRCHARS: { 1623 case FORMAT_TYPE_NRCHARS: {
1585 /* skip %n 's argument */ 1624 /* skip %n 's argument */
1586 int qualifier = spec.qualifier; 1625 u8 qualifier = spec.qualifier;
1587 void *skip_arg; 1626 void *skip_arg;
1588 if (qualifier == 'l') 1627 if (qualifier == 'l')
1589 skip_arg = va_arg(args, long *); 1628 skip_arg = va_arg(args, long *);
@@ -1849,7 +1888,9 @@ int vsscanf(const char *buf, const char *fmt, va_list args)
1849 char *next; 1888 char *next;
1850 char digit; 1889 char digit;
1851 int num = 0; 1890 int num = 0;
1852 int qualifier, base, field_width; 1891 u8 qualifier;
1892 u8 base;
1893 s16 field_width;
1853 bool is_sign; 1894 bool is_sign;
1854 1895
1855 while (*fmt && *str) { 1896 while (*fmt && *str) {
@@ -1927,7 +1968,7 @@ int vsscanf(const char *buf, const char *fmt, va_list args)
1927 { 1968 {
1928 char *s = (char *)va_arg(args, char *); 1969 char *s = (char *)va_arg(args, char *);
1929 if (field_width == -1) 1970 if (field_width == -1)
1930 field_width = INT_MAX; 1971 field_width = SHORT_MAX;
1931 /* first, skip leading white space in buffer */ 1972 /* first, skip leading white space in buffer */
1932 str = skip_spaces(str); 1973 str = skip_spaces(str);
1933 1974