4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/ctype.h>
24 #include <linux/kernel.h>
25 #include <linux/kallsyms.h>
26 #include <linux/uaccess.h>
27 #include <linux/ioport.h>
29 #include <asm/page.h> /* for PAGE_SIZE */
30 #include <asm/div64.h>
31 #include <asm/sections.h> /* for dereference_function_descriptor() */
33 /* Works only for digits and letters, but small and fast */
34 #define TOLOWER(x) ((x) | 0x20)
36 static unsigned int simple_guess_base(const char *cp)
39 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
49 * simple_strtoul - convert a string to an unsigned long
50 * @cp: The start of the string
51 * @endp: A pointer to the end of the parsed string will be placed here
52 * @base: The number base to use
54 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
56 unsigned long result = 0;
59 base = simple_guess_base(cp);
61 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
64 while (isxdigit(*cp)) {
67 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
70 result = result * base + value;
78 EXPORT_SYMBOL(simple_strtoul);
81 * simple_strtol - convert a string to a signed long
82 * @cp: The start of the string
83 * @endp: A pointer to the end of the parsed string will be placed here
84 * @base: The number base to use
86 long simple_strtol(const char *cp, char **endp, unsigned int base)
89 return -simple_strtoul(cp + 1, endp, base);
90 return simple_strtoul(cp, endp, base);
92 EXPORT_SYMBOL(simple_strtol);
95 * simple_strtoull - convert a string to an unsigned long long
96 * @cp: The start of the string
97 * @endp: A pointer to the end of the parsed string will be placed here
98 * @base: The number base to use
100 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
102 unsigned long long result = 0;
105 base = simple_guess_base(cp);
107 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
110 while (isxdigit(*cp)) {
113 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
116 result = result * base + value;
124 EXPORT_SYMBOL(simple_strtoull);
127 * simple_strtoll - convert a string to a signed long long
128 * @cp: The start of the string
129 * @endp: A pointer to the end of the parsed string will be placed here
130 * @base: The number base to use
132 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
135 return -simple_strtoull(cp + 1, endp, base);
136 return simple_strtoull(cp, endp, base);
140 * strict_strtoul - convert a string to an unsigned long strictly
141 * @cp: The string to be converted
142 * @base: The number base to use
143 * @res: The converted result value
145 * strict_strtoul converts a string to an unsigned long only if the
146 * string is really an unsigned long string, any string containing
147 * any invalid char at the tail will be rejected and -EINVAL is returned,
148 * only a newline char at the tail is acceptible because people generally
149 * change a module parameter in the following way:
151 * echo 1024 > /sys/module/e1000/parameters/copybreak
153 * echo will append a newline to the tail.
155 * It returns 0 if conversion is successful and *res is set to the converted
156 * value, otherwise it returns -EINVAL and *res is set to 0.
158 * simple_strtoul just ignores the successive invalid characters and
159 * return the converted value of prefix part of the string.
161 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
172 val = simple_strtoul(cp, &tail, base);
175 if ((*tail == '\0') ||
176 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
183 EXPORT_SYMBOL(strict_strtoul);
186 * strict_strtol - convert a string to a long strictly
187 * @cp: The string to be converted
188 * @base: The number base to use
189 * @res: The converted result value
191 * strict_strtol is similiar to strict_strtoul, but it allows the first
192 * character of a string is '-'.
194 * It returns 0 if conversion is successful and *res is set to the converted
195 * value, otherwise it returns -EINVAL and *res is set to 0.
197 int strict_strtol(const char *cp, unsigned int base, long *res)
201 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
205 ret = strict_strtoul(cp, base, (unsigned long *)res);
210 EXPORT_SYMBOL(strict_strtol);
213 * strict_strtoull - convert a string to an unsigned long long strictly
214 * @cp: The string to be converted
215 * @base: The number base to use
216 * @res: The converted result value
218 * strict_strtoull converts a string to an unsigned long long only if the
219 * string is really an unsigned long long string, any string containing
220 * any invalid char at the tail will be rejected and -EINVAL is returned,
221 * only a newline char at the tail is acceptible because people generally
222 * change a module parameter in the following way:
224 * echo 1024 > /sys/module/e1000/parameters/copybreak
226 * echo will append a newline to the tail of the string.
228 * It returns 0 if conversion is successful and *res is set to the converted
229 * value, otherwise it returns -EINVAL and *res is set to 0.
231 * simple_strtoull just ignores the successive invalid characters and
232 * return the converted value of prefix part of the string.
234 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
237 unsigned long long val;
245 val = simple_strtoull(cp, &tail, base);
248 if ((*tail == '\0') ||
249 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
256 EXPORT_SYMBOL(strict_strtoull);
259 * strict_strtoll - convert a string to a long long strictly
260 * @cp: The string to be converted
261 * @base: The number base to use
262 * @res: The converted result value
264 * strict_strtoll is similiar to strict_strtoull, but it allows the first
265 * character of a string is '-'.
267 * It returns 0 if conversion is successful and *res is set to the converted
268 * value, otherwise it returns -EINVAL and *res is set to 0.
270 int strict_strtoll(const char *cp, unsigned int base, long long *res)
274 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
278 ret = strict_strtoull(cp, base, (unsigned long long *)res);
283 EXPORT_SYMBOL(strict_strtoll);
285 static int skip_atoi(const char **s)
290 i = i*10 + *((*s)++) - '0';
294 /* Decimal conversion is by far the most typical, and is used
295 * for /proc and /sys data. This directly impacts e.g. top performance
296 * with many processes running. We optimize it for speed
298 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
299 * (with permission from the author, Douglas W. Jones). */
301 /* Formats correctly any integer in [0,99999].
302 * Outputs from one to five digits depending on input.
303 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
304 static char* put_dec_trunc(char *buf, unsigned q)
306 unsigned d3, d2, d1, d0;
311 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
312 q = (d0 * 0xcd) >> 11;
314 *buf++ = d0 + '0'; /* least significant digit */
315 d1 = q + 9*d3 + 5*d2 + d1;
317 q = (d1 * 0xcd) >> 11;
319 *buf++ = d1 + '0'; /* next digit */
322 if ((d2 != 0) || (d3 != 0)) {
325 *buf++ = d2 + '0'; /* next digit */
329 q = (d3 * 0xcd) >> 11;
331 *buf++ = d3 + '0'; /* next digit */
333 *buf++ = q + '0'; /* most sign. digit */
339 /* Same with if's removed. Always emits five digits */
340 static char* put_dec_full(char *buf, unsigned q)
342 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
343 /* but anyway, gcc produces better code with full-sized ints */
344 unsigned d3, d2, d1, d0;
349 /* Possible ways to approx. divide by 10 */
350 /* gcc -O2 replaces multiply with shifts and adds */
351 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
352 // (x * 0x67) >> 10: 1100111
353 // (x * 0x34) >> 9: 110100 - same
354 // (x * 0x1a) >> 8: 11010 - same
355 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
357 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
358 q = (d0 * 0xcd) >> 11;
361 d1 = q + 9*d3 + 5*d2 + d1;
362 q = (d1 * 0xcd) >> 11;
372 q = (d3 * 0xcd) >> 11; /* - shorter code */
373 /* q = (d3 * 0x67) >> 10; - would also work */
379 /* No inlining helps gcc to use registers better */
380 static noinline char* put_dec(char *buf, unsigned long long num)
385 return put_dec_trunc(buf, num);
386 rem = do_div(num, 100000);
387 buf = put_dec_full(buf, rem);
391 #define ZEROPAD 1 /* pad with zero */
392 #define SIGN 2 /* unsigned/signed long */
393 #define PLUS 4 /* show plus */
394 #define SPACE 8 /* space if plus */
395 #define LEFT 16 /* left justified */
396 #define SMALL 32 /* Must be 32 == 0x20 */
397 #define SPECIAL 64 /* 0x */
399 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
401 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
402 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
407 int need_pfx = ((type & SPECIAL) && base != 10);
410 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
411 * produces same digits or (maybe lowercased) letters */
412 locase = (type & SMALL);
417 if ((signed long long) num < 0) {
419 num = - (signed long long) num;
421 } else if (type & PLUS) {
424 } else if (type & SPACE) {
435 /* generate full string in tmp[], in reverse order */
439 /* Generic code, for any base:
441 tmp[i++] = (digits[do_div(num,base)] | locase);
444 else if (base != 10) { /* 8 or 16 */
447 if (base == 16) shift = 4;
449 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
452 } else { /* base 10 */
453 i = put_dec(tmp, num) - tmp;
456 /* printing 100 using %2d gives "100", not "00" */
459 /* leading space padding */
461 if (!(type & (ZEROPAD+LEFT))) {
474 /* "0x" / "0" prefix */
481 *buf = ('X' | locase);
485 /* zero or space padding */
486 if (!(type & LEFT)) {
487 char c = (type & ZEROPAD) ? '0' : ' ';
488 while (--size >= 0) {
494 /* hmm even more zero padding? */
495 while (i <= --precision) {
500 /* actual digits of result */
506 /* trailing space padding */
507 while (--size >= 0) {
515 static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
519 if ((unsigned long)s < PAGE_SIZE)
522 len = strnlen(s, precision);
524 if (!(flags & LEFT)) {
525 while (len < field_width--) {
531 for (i = 0; i < len; ++i) {
536 while (len < field_width--) {
544 static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
546 unsigned long value = (unsigned long) ptr;
547 #ifdef CONFIG_KALLSYMS
548 char sym[KSYM_SYMBOL_LEN];
549 sprint_symbol(sym, value);
550 return string(buf, end, sym, field_width, precision, flags);
552 field_width = 2*sizeof(void *);
553 flags |= SPECIAL | SMALL | ZEROPAD;
554 return number(buf, end, value, 16, field_width, precision, flags);
558 static char *resource_string(char *buf, char *end, struct resource *res, int field_width, int precision, int flags)
560 #ifndef IO_RSRC_PRINTK_SIZE
561 #define IO_RSRC_PRINTK_SIZE 4
564 #ifndef MEM_RSRC_PRINTK_SIZE
565 #define MEM_RSRC_PRINTK_SIZE 8
568 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
569 char sym[4*sizeof(resource_size_t) + 8];
570 char *p = sym, *pend = sym + sizeof(sym);
573 if (res->flags & IORESOURCE_IO)
574 size = IO_RSRC_PRINTK_SIZE;
575 else if (res->flags & IORESOURCE_MEM)
576 size = MEM_RSRC_PRINTK_SIZE;
579 p = number(p, pend, res->start, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
581 p = number(p, pend, res->end, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
585 return string(buf, end, sym, field_width, precision, flags);
588 static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
589 int precision, int flags)
591 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
595 for (i = 0; i < 6; i++) {
596 p = pack_hex_byte(p, addr[i]);
597 if (!(flags & SPECIAL) && i != 5)
602 return string(buf, end, mac_addr, field_width, precision, flags & ~SPECIAL);
605 static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
606 int precision, int flags)
608 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
612 for (i = 0; i < 8; i++) {
613 p = pack_hex_byte(p, addr[2 * i]);
614 p = pack_hex_byte(p, addr[2 * i + 1]);
615 if (!(flags & SPECIAL) && i != 7)
620 return string(buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL);
623 static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
624 int precision, int flags)
626 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 */
631 for (i = 0; i < 4; i++) {
632 digits = put_dec_trunc(temp, addr[i]) - temp;
633 /* reverse the digits in the quad */
641 return string(buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL);
645 * Show a '%p' thing. A kernel extension is that the '%p' is followed
646 * by an extra set of alphanumeric characters that are extended format
649 * Right now we handle:
651 * - 'F' For symbolic function descriptor pointers
652 * - 'S' For symbolic direct pointers
653 * - 'R' For a struct resource pointer, it prints the range of
654 * addresses (not the name nor the flags)
655 * - 'M' For a 6-byte MAC address, it prints the address in the
656 * usual colon-separated hex notation
657 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
658 * decimal for v4 and colon separated network-order 16 bit hex for v6)
659 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
662 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
663 * function pointers are really function descriptors, which contain a
664 * pointer to the real address.
666 static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
669 return string(buf, end, "(null)", field_width, precision, flags);
673 ptr = dereference_function_descriptor(ptr);
676 return symbol_string(buf, end, ptr, field_width, precision, flags);
678 return resource_string(buf, end, ptr, field_width, precision, flags);
683 return mac_address_string(buf, end, ptr, field_width, precision, flags);
689 return ip6_addr_string(buf, end, ptr, field_width, precision, flags);
691 return ip4_addr_string(buf, end, ptr, field_width, precision, flags);
696 if (field_width == -1) {
697 field_width = 2*sizeof(void *);
700 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
704 * vsnprintf - Format a string and place it in a buffer
705 * @buf: The buffer to place the result into
706 * @size: The size of the buffer, including the trailing null space
707 * @fmt: The format string to use
708 * @args: Arguments for the format string
710 * This function follows C99 vsnprintf, but has some extensions:
711 * %pS output the name of a text symbol
712 * %pF output the name of a function pointer
713 * %pR output the address range in a struct resource
715 * The return value is the number of characters which would
716 * be generated for the given input, excluding the trailing
717 * '\0', as per ISO C99. If you want to have the exact
718 * number of characters written into @buf as return value
719 * (not including the trailing '\0'), use vscnprintf(). If the
720 * return is greater than or equal to @size, the resulting
721 * string is truncated.
723 * Call this function if you are already dealing with a va_list.
724 * You probably want snprintf() instead.
726 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
728 unsigned long long num;
732 int flags; /* flags to number() */
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 */
742 /* Reject out-of-range values early. Large positive sizes are
743 used for unknown buffer sizes. */
744 if (unlikely((int) size < 0)) {
745 /* There can be only one.. */
746 static char warn = 1;
755 /* Make sure end is always >= buf */
761 for (; *fmt ; ++fmt) {
772 ++fmt; /* this also skips first '%' */
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;
781 /* get field width */
784 field_width = skip_atoi(&fmt);
785 else if (*fmt == '*') {
787 /* it's the next argument */
788 field_width = va_arg(args, int);
789 if (field_width < 0) {
790 field_width = -field_width;
795 /* get the precision */
800 precision = skip_atoi(&fmt);
801 else if (*fmt == '*') {
803 /* it's the next argument */
804 precision = va_arg(args, int);
810 /* get the conversion qualifier */
812 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
813 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
816 if (qualifier == 'l' && *fmt == 'l') {
827 if (!(flags & LEFT)) {
828 while (--field_width > 0) {
834 c = (unsigned char) va_arg(args, int);
838 while (--field_width > 0) {
846 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
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]))
860 * What does C99 say about the overflow case here? */
861 if (qualifier == 'l') {
862 long * ip = va_arg(args, long *);
864 } else if (qualifier == 'Z' || qualifier == 'z') {
865 size_t * ip = va_arg(args, size_t *);
868 int * ip = va_arg(args, int *);
879 /* integer number formats - set up the flags and "break" */
909 if (qualifier == 'L')
910 num = va_arg(args, long long);
911 else if (qualifier == 'l') {
912 num = va_arg(args, unsigned long);
914 num = (signed long) num;
915 } else if (qualifier == 'Z' || qualifier == 'z') {
916 num = va_arg(args, size_t);
917 } else if (qualifier == 't') {
918 num = va_arg(args, ptrdiff_t);
919 } else if (qualifier == 'h') {
920 num = (unsigned short) va_arg(args, int);
922 num = (signed short) num;
924 num = va_arg(args, unsigned int);
926 num = (signed int) num;
928 str = number(str, end, num, base,
929 field_width, precision, flags);
937 /* the trailing null byte doesn't count towards the total */
940 EXPORT_SYMBOL(vsnprintf);
943 * vscnprintf - Format a string and place it in a buffer
944 * @buf: The buffer to place the result into
945 * @size: The size of the buffer, including the trailing null space
946 * @fmt: The format string to use
947 * @args: Arguments for the format string
949 * The return value is the number of characters which have been written into
950 * the @buf not including the trailing '\0'. If @size is <= 0 the function
953 * Call this function if you are already dealing with a va_list.
954 * You probably want scnprintf() instead.
956 * See the vsnprintf() documentation for format string extensions over C99.
958 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
962 i=vsnprintf(buf,size,fmt,args);
963 return (i >= size) ? (size - 1) : i;
965 EXPORT_SYMBOL(vscnprintf);
968 * snprintf - Format a string and place it in a buffer
969 * @buf: The buffer to place the result into
970 * @size: The size of the buffer, including the trailing null space
971 * @fmt: The format string to use
972 * @...: Arguments for the format string
974 * The return value is the number of characters which would be
975 * generated for the given input, excluding the trailing null,
976 * as per ISO C99. If the return is greater than or equal to
977 * @size, the resulting string is truncated.
979 * See the vsnprintf() documentation for format string extensions over C99.
981 int snprintf(char * buf, size_t size, const char *fmt, ...)
987 i=vsnprintf(buf,size,fmt,args);
991 EXPORT_SYMBOL(snprintf);
994 * scnprintf - Format a string and place it in a buffer
995 * @buf: The buffer to place the result into
996 * @size: The size of the buffer, including the trailing null space
997 * @fmt: The format string to use
998 * @...: Arguments for the format string
1000 * The return value is the number of characters written into @buf not including
1001 * the trailing '\0'. If @size is <= 0 the function returns 0.
1004 int scnprintf(char * buf, size_t size, const char *fmt, ...)
1009 va_start(args, fmt);
1010 i = vsnprintf(buf, size, fmt, args);
1012 return (i >= size) ? (size - 1) : i;
1014 EXPORT_SYMBOL(scnprintf);
1017 * vsprintf - Format a string and place it in a buffer
1018 * @buf: The buffer to place the result into
1019 * @fmt: The format string to use
1020 * @args: Arguments for the format string
1022 * The function returns the number of characters written
1023 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1026 * Call this function if you are already dealing with a va_list.
1027 * You probably want sprintf() instead.
1029 * See the vsnprintf() documentation for format string extensions over C99.
1031 int vsprintf(char *buf, const char *fmt, va_list args)
1033 return vsnprintf(buf, INT_MAX, fmt, args);
1035 EXPORT_SYMBOL(vsprintf);
1038 * sprintf - Format a string and place it in a buffer
1039 * @buf: The buffer to place the result into
1040 * @fmt: The format string to use
1041 * @...: Arguments for the format string
1043 * The function returns the number of characters written
1044 * into @buf. Use snprintf() or scnprintf() in order to avoid
1047 * See the vsnprintf() documentation for format string extensions over C99.
1049 int sprintf(char * buf, const char *fmt, ...)
1054 va_start(args, fmt);
1055 i=vsnprintf(buf, INT_MAX, fmt, args);
1059 EXPORT_SYMBOL(sprintf);
1061 #ifdef CONFIG_BINARY_PRINTF
1064 * vbin_printf() - VA arguments to binary data
1065 * bstr_printf() - Binary data to text string
1069 * vbin_printf - Parse a format string and place args' binary value in a buffer
1070 * @bin_buf: The buffer to place args' binary value
1071 * @size: The size of the buffer(by words(32bits), not characters)
1072 * @fmt: The format string to use
1073 * @args: Arguments for the format string
1075 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1078 * The return value is the number of words(32bits) which would be generated for
1082 * If the return value is greater than @size, the resulting bin_buf is NOT
1083 * valid for bstr_printf().
1085 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1090 str = (char *)bin_buf;
1091 end = (char *)(bin_buf + size);
1093 #define save_arg(type) \
1095 if (sizeof(type) == 8) { \
1096 unsigned long long value; \
1097 str = PTR_ALIGN(str, sizeof(u32)); \
1098 value = va_arg(args, unsigned long long); \
1099 if (str + sizeof(type) <= end) { \
1100 *(u32 *)str = *(u32 *)&value; \
1101 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1104 unsigned long value; \
1105 str = PTR_ALIGN(str, sizeof(type)); \
1106 value = va_arg(args, int); \
1107 if (str + sizeof(type) <= end) \
1108 *(typeof(type) *)str = (type)value; \
1110 str += sizeof(type); \
1113 for (; *fmt ; ++fmt) {
1119 ++fmt; /* this also skips first '%' */
1120 if (*fmt == '-' || *fmt == '+' || *fmt == ' '
1121 || *fmt == '#' || *fmt == '0')
1124 /* parse field width */
1127 else if (*fmt == '*') {
1129 /* it's the next argument */
1133 /* parse the precision */
1138 else if (*fmt == '*') {
1140 /* it's the next argument */
1145 /* parse the conversion qualifier */
1147 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1148 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
1151 if (qualifier == 'l' && *fmt == 'l') {
1157 /* parse format type */
1163 /* save the string argument */
1164 const char *save_str = va_arg(args, char *);
1166 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1167 || (unsigned long)save_str < PAGE_SIZE)
1168 save_str = "<NULL>";
1169 len = strlen(save_str);
1170 if (str + len + 1 < end)
1171 memcpy(str, save_str, len + 1);
1177 /* skip all alphanumeric pointer suffixes */
1178 while (isalnum(fmt[1]))
1182 /* skip %n 's argument */
1184 if (qualifier == 'l')
1185 skip_arg = va_arg(args, long *);
1186 else if (qualifier == 'Z' || qualifier == 'z')
1187 skip_arg = va_arg(args, size_t *);
1189 skip_arg = va_arg(args, int *);
1198 /* save arg for case: 'o', 'x', 'X', 'd', 'i', 'u' */
1199 if (qualifier == 'L')
1200 save_arg(long long);
1201 else if (qualifier == 'l')
1202 save_arg(unsigned long);
1203 else if (qualifier == 'Z' || qualifier == 'z')
1205 else if (qualifier == 't')
1206 save_arg(ptrdiff_t);
1207 else if (qualifier == 'h')
1220 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1222 EXPORT_SYMBOL_GPL(vbin_printf);
1225 * bstr_printf - Format a string from binary arguments and place it in a buffer
1226 * @buf: The buffer to place the result into
1227 * @size: The size of the buffer, including the trailing null space
1228 * @fmt: The format string to use
1229 * @bin_buf: Binary arguments for the format string
1231 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1232 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1233 * a binary buffer that generated by vbin_printf.
1235 * The format follows C99 vsnprintf, but has some extensions:
1236 * %pS output the name of a text symbol
1237 * %pF output the name of a function pointer
1238 * %pR output the address range in a struct resource
1241 * The return value is the number of characters which would
1242 * be generated for the given input, excluding the trailing
1243 * '\0', as per ISO C99. If you want to have the exact
1244 * number of characters written into @buf as return value
1245 * (not including the trailing '\0'), use vscnprintf(). If the
1246 * return is greater than or equal to @size, the resulting
1247 * string is truncated.
1249 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1251 unsigned long long num;
1254 const char *args = (const char *)bin_buf;
1261 if (unlikely((int) size < 0)) {
1262 /* There can be only one.. */
1263 static char warn = 1;
1272 #define get_arg(type) \
1274 typeof(type) value; \
1275 if (sizeof(type) == 8) { \
1276 args = PTR_ALIGN(args, sizeof(u32)); \
1277 *(u32 *)&value = *(u32 *)args; \
1278 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1280 args = PTR_ALIGN(args, sizeof(type)); \
1281 value = *(typeof(type) *)args; \
1283 args += sizeof(type); \
1287 /* Make sure end is always >= buf */
1293 for (; *fmt ; ++fmt) {
1304 ++fmt; /* this also skips first '%' */
1323 /* get field width */
1326 field_width = skip_atoi(&fmt);
1327 else if (*fmt == '*') {
1329 /* it's the next argument */
1330 field_width = get_arg(int);
1331 if (field_width < 0) {
1332 field_width = -field_width;
1337 /* get the precision */
1342 precision = skip_atoi(&fmt);
1343 else if (*fmt == '*') {
1345 /* it's the next argument */
1346 precision = get_arg(int);
1352 /* get the conversion qualifier */
1354 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1355 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
1358 if (qualifier == 'l' && *fmt == 'l') {
1369 if (!(flags & LEFT)) {
1370 while (--field_width > 0) {
1376 c = (unsigned char) get_arg(char);
1380 while (--field_width > 0) {
1388 const char *str_arg = args;
1389 size_t len = strlen(str_arg);
1391 str = string(str, end, (char *)str_arg, field_width,
1397 str = pointer(fmt+1, str, end, get_arg(void *),
1398 field_width, precision, flags);
1399 /* Skip all alphanumeric pointer suffixes */
1400 while (isalnum(fmt[1]))
1414 /* integer number formats - set up the flags and "break" */
1444 if (qualifier == 'L')
1445 num = get_arg(long long);
1446 else if (qualifier == 'l') {
1447 num = get_arg(unsigned long);
1449 num = (signed long) num;
1450 } else if (qualifier == 'Z' || qualifier == 'z') {
1451 num = get_arg(size_t);
1452 } else if (qualifier == 't') {
1453 num = get_arg(ptrdiff_t);
1454 } else if (qualifier == 'h') {
1455 num = (unsigned short) get_arg(short);
1457 num = (signed short) num;
1459 num = get_arg(unsigned int);
1461 num = (signed int) num;
1463 str = number(str, end, num, base,
1464 field_width, precision, flags);
1474 /* the trailing null byte doesn't count towards the total */
1477 EXPORT_SYMBOL_GPL(bstr_printf);
1480 * bprintf - Parse a format string and place args' binary value in a buffer
1481 * @bin_buf: The buffer to place args' binary value
1482 * @size: The size of the buffer(by words(32bits), not characters)
1483 * @fmt: The format string to use
1484 * @...: Arguments for the format string
1486 * The function returns the number of words(u32) written
1489 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1494 va_start(args, fmt);
1495 ret = vbin_printf(bin_buf, size, fmt, args);
1499 EXPORT_SYMBOL_GPL(bprintf);
1501 #endif /* CONFIG_BINARY_PRINTF */
1504 * vsscanf - Unformat a buffer into a list of arguments
1505 * @buf: input buffer
1506 * @fmt: format of buffer
1509 int vsscanf(const char * buf, const char * fmt, va_list args)
1511 const char *str = buf;
1520 while(*fmt && *str) {
1521 /* skip any white space in format */
1522 /* white space in format matchs any amount of
1523 * white space, including none, in the input.
1525 if (isspace(*fmt)) {
1526 while (isspace(*fmt))
1528 while (isspace(*str))
1532 /* anything that is not a conversion must match exactly */
1533 if (*fmt != '%' && *fmt) {
1534 if (*fmt++ != *str++)
1543 /* skip this conversion.
1544 * advance both strings to next white space
1547 while (!isspace(*fmt) && *fmt)
1549 while (!isspace(*str) && *str)
1554 /* get field width */
1557 field_width = skip_atoi(&fmt);
1559 /* get conversion qualifier */
1561 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1562 *fmt == 'Z' || *fmt == 'z') {
1564 if (unlikely(qualifier == *fmt)) {
1565 if (qualifier == 'h') {
1568 } else if (qualifier == 'l') {
1583 char *s = (char *) va_arg(args,char*);
1584 if (field_width == -1)
1588 } while (--field_width > 0 && *str);
1594 char *s = (char *) va_arg(args, char *);
1595 if(field_width == -1)
1596 field_width = INT_MAX;
1597 /* first, skip leading white space in buffer */
1598 while (isspace(*str))
1601 /* now copy until next white space */
1602 while (*str && !isspace(*str) && field_width--) {
1610 /* return number of characters read so far */
1612 int *i = (int *)va_arg(args,int*);
1630 /* looking for '%' in str */
1635 /* invalid format; stop here */
1639 /* have some sort of integer conversion.
1640 * first, skip white space in buffer.
1642 while (isspace(*str))
1646 if (is_sign && digit == '-')
1650 || (base == 16 && !isxdigit(digit))
1651 || (base == 10 && !isdigit(digit))
1652 || (base == 8 && (!isdigit(digit) || digit > '7'))
1653 || (base == 0 && !isdigit(digit)))
1657 case 'H': /* that's 'hh' in format */
1659 signed char *s = (signed char *) va_arg(args,signed char *);
1660 *s = (signed char) simple_strtol(str,&next,base);
1662 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1663 *s = (unsigned char) simple_strtoul(str, &next, base);
1668 short *s = (short *) va_arg(args,short *);
1669 *s = (short) simple_strtol(str,&next,base);
1671 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1672 *s = (unsigned short) simple_strtoul(str, &next, base);
1677 long *l = (long *) va_arg(args,long *);
1678 *l = simple_strtol(str,&next,base);
1680 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1681 *l = simple_strtoul(str,&next,base);
1686 long long *l = (long long*) va_arg(args,long long *);
1687 *l = simple_strtoll(str,&next,base);
1689 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1690 *l = simple_strtoull(str,&next,base);
1696 size_t *s = (size_t*) va_arg(args,size_t*);
1697 *s = (size_t) simple_strtoul(str,&next,base);
1702 int *i = (int *) va_arg(args, int*);
1703 *i = (int) simple_strtol(str,&next,base);
1705 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1706 *i = (unsigned int) simple_strtoul(str,&next,base);
1718 * Now we've come all the way through so either the input string or the
1719 * format ended. In the former case, there can be a %n at the current
1720 * position in the format that needs to be filled.
1722 if (*fmt == '%' && *(fmt + 1) == 'n') {
1723 int *p = (int *)va_arg(args, int *);
1729 EXPORT_SYMBOL(vsscanf);
1732 * sscanf - Unformat a buffer into a list of arguments
1733 * @buf: input buffer
1734 * @fmt: formatting of buffer
1735 * @...: resulting arguments
1737 int sscanf(const char * buf, const char * fmt, ...)
1743 i = vsscanf(buf,fmt,args);
1747 EXPORT_SYMBOL(sscanf);