Skip to content
Snippets Groups Projects
vsprintf.c 56 KiB
Newer Older
  • Learn to ignore specific revisions
  • 			have_p = true;
    			break;
    		case 'h':
    		case 'l':
    		case 'n':
    		case 'b':
    			fmt4[2] = *fmt;
    			break;
    		}
    	}
    
    	p = ip4_string(ip4_addr, addr, fmt4);
    	if (have_p) {
    		*p++ = ':';
    		p = number(p, pend, ntohs(sa->sin_port), spec);
    	}
    	*p = '\0';
    
    	return string(buf, end, ip4_addr, spec);
    }
    
    
    static noinline_for_stack
    char *uuid_string(char *buf, char *end, const u8 *addr,
    		  struct printf_spec spec, const char *fmt)
    
    {
    	char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
    	char *p = uuid;
    	int i;
    	static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    	static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
    	const u8 *index = be;
    	bool uc = false;
    
    	switch (*(++fmt)) {
    	case 'L':
    		uc = true;		/* fall-through */
    	case 'l':
    		index = le;
    		break;
    	case 'B':
    		uc = true;
    		break;
    	}
    
    	for (i = 0; i < 16; i++) {
    
    		p = hex_byte_pack(p, addr[index[i]]);
    
    		switch (i) {
    		case 3:
    		case 5:
    		case 7:
    		case 9:
    			*p++ = '-';
    			break;
    		}
    	}
    
    	*p = 0;
    
    	if (uc) {
    		p = uuid;
    		do {
    			*p = toupper(*p);
    		} while (*(++p));
    	}
    
    	return string(buf, end, uuid, spec);
    }
    
    
    static
    char *netdev_feature_string(char *buf, char *end, const u8 *addr,
    		      struct printf_spec spec)
    {
    	spec.flags |= SPECIAL | SMALL | ZEROPAD;
    	if (spec.field_width == -1)
    		spec.field_width = 2 + 2 * sizeof(netdev_features_t);
    	spec.base = 16;
    
    	return number(buf, end, *(const netdev_features_t *)addr, spec);
    }
    
    
    int kptr_restrict __read_mostly;
    
    /*
     * Show a '%p' thing.  A kernel extension is that the '%p' is followed
     * by an extra set of alphanumeric characters that are extended format
     * specifiers.
     *
    
     * Right now we handle:
     *
    
     * - 'F' For symbolic function descriptor pointers with offset
     * - 'f' For simple symbolic function names without offset
    
     * - 'S' For symbolic direct pointers with offset
     * - 's' For symbolic direct pointers without offset
    
     * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
    
     * - 'B' For backtraced symbolic direct pointers with offset
    
     * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
     * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
    
     * - 'M' For a 6-byte MAC address, it prints the address in the
     *       usual colon-separated hex notation
    
     * - 'm' For a 6-byte MAC address, it prints the hex address without colons
    
     * - 'MF' For a 6-byte MAC FDDI address, it prints the address
    
     *       with a dash-separated hex notation
    
     * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
    
     * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
     *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
     *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
    
     *       [S][pfs]
     *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
     *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
    
     * - 'i' [46] for 'raw' IPv4/IPv6 addresses
     *       IPv6 omits the colons (01020304...0f)
     *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
    
     *       [S][pfs]
     *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
     *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
     * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
     * - 'I[6S]c' for IPv6 addresses printed as specified by
    
     *       http://tools.ietf.org/html/rfc5952
    
     * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
     *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
     *       Options for %pU are:
     *         b big endian lower case hex (default)
     *         B big endian UPPER case hex
     *         l little endian lower case hex
     *         L little endian UPPER case hex
     *           big endian output byte order is:
     *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
     *           little endian output byte order is:
     *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
    
     * - 'V' For a struct va_format which contains a format string * and va_list *,
     *       call vsnprintf(->format, *->va_list).
     *       Implements a "recursive vsnprintf".
     *       Do not use this feature without some mechanism to verify the
     *       correctness of the format string and va_list arguments.
    
     * - 'K' For a kernel pointer that should be hidden from unprivileged users
    
     * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
     *            a certain separator (' ' by default):
     *              C colon
     *              D dash
     *              N no separator
     *            The maximum supported length is 64 bytes of the input. Consider
     *            to use print_hex_dump() for the larger input.
    
     * - 'a' For a phys_addr_t type and its derivative types (passed by reference)
    
     * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
     * function pointers are really function descriptors, which contain a
     * pointer to the real address.
    
    static noinline_for_stack
    char *pointer(const char *fmt, char *buf, char *end, void *ptr,
    	      struct printf_spec spec)
    
    	int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
    
    
    	if (!ptr && *fmt != 'K') {
    
    		/*
    		 * Print (null) with the same width as a pointer so it makes
    		 * tabular output look nice.
    		 */
    		if (spec.field_width == -1)
    
    			spec.field_width = default_width;
    
    		return string(buf, end, "(null)", spec);
    
    		ptr = dereference_function_descriptor(ptr);
    		/* Fallthrough */
    	case 'S':
    
    		return symbol_string(buf, end, ptr, spec, fmt);
    
    		return resource_string(buf, end, ptr, spec, fmt);
    
    	case 'h':
    		return hex_string(buf, end, ptr, spec, fmt);
    
    	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
    	case 'm':			/* Contiguous: 000102030405 */
    
    					/* [mM]F (FDDI) */
    					/* [mM]R (Reverse order; Bluetooth) */
    
    		return mac_address_string(buf, end, ptr, spec, fmt);
    	case 'I':			/* Formatted IP supported
    					 * 4:	1.2.3.4
    					 * 6:	0001:0203:...:0708
    					 * 6c:	1::708 or 1::1.2.3.4
    					 */
    	case 'i':			/* Contiguous:
    					 * 4:	001.002.003.004
    					 * 6:   000102...0f
    					 */
    		switch (fmt[1]) {
    		case '6':
    			return ip6_addr_string(buf, end, ptr, spec, fmt);
    		case '4':
    			return ip4_addr_string(buf, end, ptr, spec, fmt);
    
    		case 'S': {
    			const union {
    				struct sockaddr		raw;
    				struct sockaddr_in	v4;
    				struct sockaddr_in6	v6;
    			} *sa = ptr;
    
    			switch (sa->raw.sa_family) {
    			case AF_INET:
    				return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
    			case AF_INET6:
    				return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
    			default:
    				return string(buf, end, "(invalid address)", spec);
    			}}
    
    	case 'U':
    		return uuid_string(buf, end, ptr, spec, fmt);
    
    		{
    			va_list va;
    
    			va_copy(va, *((struct va_format *)ptr)->va);
    			buf += vsnprintf(buf, end > buf ? end - buf : 0,
    					 ((struct va_format *)ptr)->fmt, va);
    			va_end(va);
    			return buf;
    		}
    
    	case 'K':
    		/*
    		 * %pK cannot be used in IRQ context because its test
    		 * for CAP_SYSLOG would be meaningless.
    		 */
    
    		if (kptr_restrict && (in_irq() || in_serving_softirq() ||
    				      in_nmi())) {
    
    				spec.field_width = default_width;
    
    			return string(buf, end, "pK-error", spec);
    		}
    
    		if (!((kptr_restrict == 0) ||
    		      (kptr_restrict == 1 &&
    		       has_capability_noaudit(current, CAP_SYSLOG))))
    			ptr = NULL;
    		break;
    
    	case 'N':
    		switch (fmt[1]) {
    		case 'F':
    			return netdev_feature_string(buf, end, ptr, spec);
    		}
    		break;
    
    	case 'a':
    		spec.flags |= SPECIAL | SMALL | ZEROPAD;
    		spec.field_width = sizeof(phys_addr_t) * 2 + 2;
    		spec.base = 16;
    		return number(buf, end,
    			      (unsigned long long) *((phys_addr_t *)ptr), spec);
    
    	}
    	spec.flags |= SMALL;
    	if (spec.field_width == -1) {
    
    		spec.field_width = default_width;
    
    		spec.flags |= ZEROPAD;
    	}
    	spec.base = 16;
    
    	return number(buf, end, (unsigned long) ptr, spec);
    }
    
    /*
     * Helper function to decode printf style format.
     * Each call decode a token from the format and return the
     * number of characters read (or likely the delta where it wants
     * to go on the next call).
     * The decoded token is returned through the parameters
     *
     * 'h', 'l', or 'L' for integer fields
     * 'z' support added 23/7/1999 S.H.
     * 'z' changed to 'Z' --davidm 1/25/99
     * 't' added for ptrdiff_t
     *
     * @fmt: the format string
     * @type of the token returned
     * @flags: various flags such as +, -, # tokens..
     * @field_width: overwritten width
     * @base: base of the number (octal, hex, ...)
     * @precision: precision of a number
     * @qualifier: qualifier of a number (long, size_t, ...)
     */
    
    static noinline_for_stack
    int format_decode(const char *fmt, struct printf_spec *spec)
    
    {
    	const char *start = fmt;
    
    	/* we finished early by reading the field width */
    
    	if (spec->type == FORMAT_TYPE_WIDTH) {
    
    		if (spec->field_width < 0) {
    			spec->field_width = -spec->field_width;
    			spec->flags |= LEFT;
    		}
    		spec->type = FORMAT_TYPE_NONE;
    		goto precision;
    	}
    
    	/* we finished early by reading the precision */
    	if (spec->type == FORMAT_TYPE_PRECISION) {
    		if (spec->precision < 0)
    			spec->precision = 0;
    
    		spec->type = FORMAT_TYPE_NONE;
    		goto qualifier;
    	}
    
    	/* By default */
    	spec->type = FORMAT_TYPE_NONE;
    
    	for (; *fmt ; ++fmt) {
    		if (*fmt == '%')
    			break;
    	}
    
    	/* Return the current non-format string */
    	if (fmt != start || !*fmt)
    		return fmt - start;
    
    	/* Process flags */
    	spec->flags = 0;
    
    	while (1) { /* this also skips first '%' */
    		bool found = true;
    
    		++fmt;
    
    		switch (*fmt) {
    		case '-': spec->flags |= LEFT;    break;
    		case '+': spec->flags |= PLUS;    break;
    		case ' ': spec->flags |= SPACE;   break;
    		case '#': spec->flags |= SPECIAL; break;
    		case '0': spec->flags |= ZEROPAD; break;
    		default:  found = false;
    		}
    
    		if (!found)
    			break;
    	}
    
    	/* get field width */
    	spec->field_width = -1;
    
    	if (isdigit(*fmt))
    		spec->field_width = skip_atoi(&fmt);
    	else if (*fmt == '*') {
    		/* it's the next argument */
    
    		spec->type = FORMAT_TYPE_WIDTH;
    
    		return ++fmt - start;
    	}
    
    precision:
    	/* get the precision */
    	spec->precision = -1;
    	if (*fmt == '.') {
    		++fmt;
    		if (isdigit(*fmt)) {
    			spec->precision = skip_atoi(&fmt);
    			if (spec->precision < 0)
    				spec->precision = 0;
    		} else if (*fmt == '*') {
    			/* it's the next argument */
    
    			return ++fmt - start;
    		}
    	}
    
    qualifier:
    	/* get the conversion qualifier */
    	spec->qualifier = -1;
    
    	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
    	    _tolower(*fmt) == 'z' || *fmt == 't') {
    
    		spec->qualifier = *fmt++;
    		if (unlikely(spec->qualifier == *fmt)) {
    			if (spec->qualifier == 'l') {
    				spec->qualifier = 'L';
    				++fmt;
    			} else if (spec->qualifier == 'h') {
    				spec->qualifier = 'H';
    				++fmt;
    			}
    
    		}
    	}
    
    	/* default base */
    	spec->base = 10;
    	switch (*fmt) {
    	case 'c':
    		spec->type = FORMAT_TYPE_CHAR;
    		return ++fmt - start;
    
    	case 's':
    		spec->type = FORMAT_TYPE_STR;
    		return ++fmt - start;
    
    	case 'p':
    		spec->type = FORMAT_TYPE_PTR;
    		return fmt - start;
    		/* skip alnum */
    
    	case 'n':
    		spec->type = FORMAT_TYPE_NRCHARS;
    		return ++fmt - start;
    
    	case '%':
    		spec->type = FORMAT_TYPE_PERCENT_CHAR;
    		return ++fmt - start;
    
    	/* integer number formats - set up the flags and "break" */
    	case 'o':
    		spec->base = 8;
    		break;
    
    	case 'x':
    		spec->flags |= SMALL;
    
    	case 'X':
    		spec->base = 16;
    		break;
    
    	case 'd':
    	case 'i':
    
    
    	default:
    		spec->type = FORMAT_TYPE_INVALID;
    		return fmt - start;
    
    
    	if (spec->qualifier == 'L')
    		spec->type = FORMAT_TYPE_LONG_LONG;
    	else if (spec->qualifier == 'l') {
    
    		if (spec->flags & SIGN)
    
    			spec->type = FORMAT_TYPE_LONG;
    		else
    			spec->type = FORMAT_TYPE_ULONG;
    
    	} else if (_tolower(spec->qualifier) == 'z') {
    
    		spec->type = FORMAT_TYPE_SIZE_T;
    	} else if (spec->qualifier == 't') {
    		spec->type = FORMAT_TYPE_PTRDIFF;
    
    	} else if (spec->qualifier == 'H') {
    		if (spec->flags & SIGN)
    			spec->type = FORMAT_TYPE_BYTE;
    		else
    			spec->type = FORMAT_TYPE_UBYTE;
    
    	} else if (spec->qualifier == 'h') {
    
    		if (spec->flags & SIGN)
    
    			spec->type = FORMAT_TYPE_SHORT;
    		else
    			spec->type = FORMAT_TYPE_USHORT;
    	} else {
    
    		if (spec->flags & SIGN)
    
    			spec->type = FORMAT_TYPE_INT;
    		else
    			spec->type = FORMAT_TYPE_UINT;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /**
     * vsnprintf - Format a string and place it in a buffer
     * @buf: The buffer to place the result into
     * @size: The size of the buffer, including the trailing null space
     * @fmt: The format string to use
     * @args: Arguments for the format string
     *
    
     * This function follows C99 vsnprintf, but has some extensions:
    
     * %pS output the name of a text symbol with offset
     * %ps output the name of a text symbol without offset
    
     * %pF output the name of a function pointer with its offset
     * %pf output the name of a function pointer without its offset
    
     * %pB output the name of a backtrace symbol with its offset
    
     * %pR output the address range in a struct resource with decoded flags
     * %pr output the address range in a struct resource with raw flags
     * %pM output a 6-byte MAC address with colons
    
     * %pMR output a 6-byte MAC address with colons in reversed order
     * %pMF output a 6-byte MAC address with dashes
    
     * %pm output a 6-byte MAC address without colons
    
     * %pmR output a 6-byte MAC address without colons in reversed order
    
     * %pI4 print an IPv4 address without leading zeros
     * %pi4 print an IPv4 address with leading zeros
     * %pI6 print an IPv6 address with colons
     * %pi6 print an IPv6 address without colons
    
     * %pI6c print an IPv6 address as specified by RFC 5952
    
     * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
     * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
    
     * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
     *   case.
    
     * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
     *           bytes of the input)
    
     * ** Please update Documentation/printk-formats.txt when making changes **
     *
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * The return value is the number of characters which would
     * be generated for the given input, excluding the trailing
     * '\0', as per ISO C99. If you want to have the exact
     * number of characters written into @buf as return value
    
     * (not including the trailing '\0'), use vscnprintf(). If the
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * return is greater than or equal to @size, the resulting
     * string is truncated.
     *
    
     * If you're not already dealing with a va_list consider using snprintf().
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
    {
    	unsigned long long num;
    
    	struct printf_spec spec = {0};
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	/* Reject out-of-range values early.  Large positive sizes are
    	   used for unknown buffer sizes. */
    
    	if (WARN_ON_ONCE((int) size < 0))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return 0;
    
    	str = buf;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	/* Make sure end is always >= buf */
    	if (end < buf) {
    		end = ((void *)-1);
    		size = end - buf;
    
    	while (*fmt) {
    		const char *old_fmt = fmt;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		switch (spec.type) {
    		case FORMAT_TYPE_NONE: {
    			int copy = read;
    			if (str < end) {
    				if (copy > end - str)
    					copy = end - str;
    				memcpy(str, old_fmt, copy);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    
    			spec.field_width = va_arg(args, int);
    			break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		case FORMAT_TYPE_PRECISION:
    			spec.precision = va_arg(args, int);
    			break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    			if (!(spec.flags & LEFT)) {
    				while (--spec.field_width > 0) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    						*str = ' ';
    					++str;
    
    
    				}
    			}
    			c = (unsigned char) va_arg(args, int);
    			if (str < end)
    				*str = c;
    			++str;
    			while (--spec.field_width > 0) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				++str;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		case FORMAT_TYPE_STR:
    			str = string(str, end, va_arg(args, char *), spec);
    			break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		case FORMAT_TYPE_PTR:
    			str = pointer(fmt+1, str, end, va_arg(args, void *),
    				      spec);
    			while (isalnum(*fmt))
    				fmt++;
    			break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		case FORMAT_TYPE_PERCENT_CHAR:
    			if (str < end)
    				*str = '%';
    			++str;
    			break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		case FORMAT_TYPE_INVALID:
    			if (str < end)
    				*str = '%';
    			++str;
    			break;
    
    		case FORMAT_TYPE_NRCHARS: {
    
    			u8 qualifier = spec.qualifier;
    
    
    			if (qualifier == 'l') {
    				long *ip = va_arg(args, long *);
    				*ip = (str - buf);
    
    			} else if (_tolower(qualifier) == 'z') {
    
    				size_t *ip = va_arg(args, size_t *);
    				*ip = (str - buf);
    			} else {
    				int *ip = va_arg(args, int *);
    				*ip = (str - buf);
    			}
    			break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    
    
    		default:
    			switch (spec.type) {
    			case FORMAT_TYPE_LONG_LONG:
    				num = va_arg(args, long long);
    				break;
    			case FORMAT_TYPE_ULONG:
    				num = va_arg(args, unsigned long);
    				break;
    			case FORMAT_TYPE_LONG:
    				num = va_arg(args, long);
    				break;
    			case FORMAT_TYPE_SIZE_T:
    
    				if (spec.flags & SIGN)
    					num = va_arg(args, ssize_t);
    				else
    					num = va_arg(args, size_t);
    
    				break;
    			case FORMAT_TYPE_PTRDIFF:
    				num = va_arg(args, ptrdiff_t);
    				break;
    
    			case FORMAT_TYPE_UBYTE:
    				num = (unsigned char) va_arg(args, int);
    				break;
    			case FORMAT_TYPE_BYTE:
    				num = (signed char) va_arg(args, int);
    				break;
    
    			case FORMAT_TYPE_USHORT:
    				num = (unsigned short) va_arg(args, int);
    				break;
    			case FORMAT_TYPE_SHORT:
    				num = (short) va_arg(args, int);
    				break;
    
    			case FORMAT_TYPE_INT:
    				num = (int) va_arg(args, int);
    
    				break;
    			default:
    				num = va_arg(args, unsigned int);
    			}
    
    			str = number(str, end, num, spec);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    	}
    
    			end[-1] = '\0';
    
    	/* the trailing null byte doesn't count towards the total */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return str-buf;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    EXPORT_SYMBOL(vsnprintf);
    
    /**
     * vscnprintf - Format a string and place it in a buffer
     * @buf: The buffer to place the result into
     * @size: The size of the buffer, including the trailing null space
     * @fmt: The format string to use
     * @args: Arguments for the format string
     *
     * The return value is the number of characters which have been written into
    
     * the @buf not including the trailing '\0'. If @size is == 0 the function
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * returns 0.
     *
    
     * If you're not already dealing with a va_list consider using scnprintf().
    
     *
     * See the vsnprintf() documentation for format string extensions over C99.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
    {
    	int i;
    
    
    	i = vsnprintf(buf, size, fmt, args);
    
    
    	if (likely(i < size))
    		return i;
    	if (size != 0)
    		return size - 1;
    	return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    EXPORT_SYMBOL(vscnprintf);
    
    /**
     * snprintf - Format a string and place it in a buffer
     * @buf: The buffer to place the result into
     * @size: The size of the buffer, including the trailing null space
     * @fmt: The format string to use
     * @...: Arguments for the format string
     *
     * The return value is the number of characters which would be
     * generated for the given input, excluding the trailing null,
     * as per ISO C99.  If the return is greater than or equal to
     * @size, the resulting string is truncated.
    
     *
     * See the vsnprintf() documentation for format string extensions over C99.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    
    int snprintf(char *buf, size_t size, const char *fmt, ...)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	va_list args;
    	int i;
    
    	va_start(args, fmt);
    
    	i = vsnprintf(buf, size, fmt, args);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	va_end(args);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return i;
    }
    EXPORT_SYMBOL(snprintf);
    
    /**
     * scnprintf - Format a string and place it in a buffer
     * @buf: The buffer to place the result into
     * @size: The size of the buffer, including the trailing null space
     * @fmt: The format string to use
     * @...: Arguments for the format string
     *
     * The return value is the number of characters written into @buf not including
    
     * the trailing '\0'. If @size is == 0 the function returns 0.
    
    int scnprintf(char *buf, size_t size, const char *fmt, ...)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	va_list args;
    	int i;
    
    	va_start(args, fmt);
    
    	i = vscnprintf(buf, size, fmt, args);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	va_end(args);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    EXPORT_SYMBOL(scnprintf);
    
    /**
     * vsprintf - Format a string and place it in a buffer
     * @buf: The buffer to place the result into
     * @fmt: The format string to use
     * @args: Arguments for the format string
     *
     * The function returns the number of characters written
    
     * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * buffer overflows.
     *
    
     * If you're not already dealing with a va_list consider using sprintf().
    
     *
     * See the vsnprintf() documentation for format string extensions over C99.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    int vsprintf(char *buf, const char *fmt, va_list args)
    {
    	return vsnprintf(buf, INT_MAX, fmt, args);
    }
    EXPORT_SYMBOL(vsprintf);
    
    /**
     * sprintf - Format a string and place it in a buffer
     * @buf: The buffer to place the result into
     * @fmt: The format string to use
     * @...: Arguments for the format string
     *
     * The function returns the number of characters written
    
     * into @buf. Use snprintf() or scnprintf() in order to avoid
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * buffer overflows.
    
     *
     * See the vsnprintf() documentation for format string extensions over C99.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    
    int sprintf(char *buf, const char *fmt, ...)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	va_list args;
    	int i;
    
    	va_start(args, fmt);
    
    	i = vsnprintf(buf, INT_MAX, fmt, args);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	va_end(args);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return i;
    }
    EXPORT_SYMBOL(sprintf);
    
    
    #ifdef CONFIG_BINARY_PRINTF
    /*
     * bprintf service:
     * vbin_printf() - VA arguments to binary data
     * bstr_printf() - Binary data to text string
     */
    
    /**
     * vbin_printf - Parse a format string and place args' binary value in a buffer
     * @bin_buf: The buffer to place args' binary value
     * @size: The size of the buffer(by words(32bits), not characters)
     * @fmt: The format string to use
     * @args: Arguments for the format string
     *
     * The format follows C99 vsnprintf, except %n is ignored, and its argument
     * is skiped.
     *
     * The return value is the number of words(32bits) which would be generated for
     * the given input.
     *
     * NOTE:
     * If the return value is greater than @size, the resulting bin_buf is NOT
     * valid for bstr_printf().
     */
    int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
    {
    
    	struct printf_spec spec = {0};
    
    	char *str, *end;
    
    	str = (char *)bin_buf;
    	end = (char *)(bin_buf + size);
    
    #define save_arg(type)							\
    do {									\
    	if (sizeof(type) == 8) {					\
    		unsigned long long value;				\
    		str = PTR_ALIGN(str, sizeof(u32));			\
    		value = va_arg(args, unsigned long long);		\
    		if (str + sizeof(type) <= end) {			\
    			*(u32 *)str = *(u32 *)&value;			\
    			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
    		}							\
    	} else {							\
    		unsigned long value;					\
    		str = PTR_ALIGN(str, sizeof(type));			\
    		value = va_arg(args, int);				\
    		if (str + sizeof(type) <= end)				\
    			*(typeof(type) *)str = (type)value;		\
    	}								\
    	str += sizeof(type);						\
    } while (0)
    
    
    		switch (spec.type) {
    		case FORMAT_TYPE_NONE:
    
    		case FORMAT_TYPE_INVALID:
    		case FORMAT_TYPE_PERCENT_CHAR:
    
    		case FORMAT_TYPE_PRECISION:
    			save_arg(int);
    			break;
    
    		case FORMAT_TYPE_CHAR:
    
    			save_arg(char);
    
    			const char *save_str = va_arg(args, char *);
    			size_t len;
    
    			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
    					|| (unsigned long)save_str < PAGE_SIZE)
    
    				save_str = "(null)";
    
    			len = strlen(save_str) + 1;
    			if (str + len < end)
    				memcpy(str, save_str, len);
    			str += len;
    
    			save_arg(void *);
    			/* skip all alphanumeric pointer suffixes */
    
    				fmt++;
    
    			/* skip %n 's argument */
    
    			u8 qualifier = spec.qualifier;
    
    			void *skip_arg;
    			if (qualifier == 'l')
    				skip_arg = va_arg(args, long *);
    
    			else if (_tolower(qualifier) == 'z')
    
    				skip_arg = va_arg(args, size_t *);
    			else
    				skip_arg = va_arg(args, int *);
    
    
    		default:
    			switch (spec.type) {
    
    			case FORMAT_TYPE_LONG_LONG:
    
    				save_arg(long long);
    
    				break;
    			case FORMAT_TYPE_ULONG:
    			case FORMAT_TYPE_LONG:
    
    				save_arg(unsigned long);
    
    				save_arg(size_t);
    
    				save_arg(ptrdiff_t);
    
    			case FORMAT_TYPE_UBYTE:
    			case FORMAT_TYPE_BYTE:
    				save_arg(char);
    				break;
    
    			case FORMAT_TYPE_USHORT:
    			case FORMAT_TYPE_SHORT:
    
    				save_arg(short);
    
    				save_arg(int);
    
    	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
    
    }
    EXPORT_SYMBOL_GPL(vbin_printf);
    
    /**
     * bstr_printf - Format a string from binary arguments and place it in a buffer
     * @buf: The buffer to place the result into
     * @size: The size of the buffer, including the trailing null space
     * @fmt: The format string to use
     * @bin_buf: Binary arguments for the format string
     *
     * This function like C99 vsnprintf, but the difference is that vsnprintf gets
     * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
     * a binary buffer that generated by vbin_printf.
     *
     * The format follows C99 vsnprintf, but has some extensions:
    
     *  see vsnprintf comment for details.
    
     *
     * The return value is the number of characters which would
     * be generated for the given input, excluding the trailing
     * '\0', as per ISO C99. If you want to have the exact
     * number of characters written into @buf as return value
     * (not including the trailing '\0'), use vscnprintf(). If the
     * return is greater than or equal to @size, the resulting
     * string is truncated.
     */
    int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
    {
    
    	struct printf_spec spec = {0};
    
    	char *str, *end;
    	const char *args = (const char *)bin_buf;
    
    	if (WARN_ON_ONCE((int) size < 0))
    
    		return 0;
    
    	str = buf;
    	end = buf + size;
    
    #define get_arg(type)							\
    ({									\
    	typeof(type) value;						\
    	if (sizeof(type) == 8) {					\
    		args = PTR_ALIGN(args, sizeof(u32));			\
    		*(u32 *)&value = *(u32 *)args;				\
    		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
    	} else {							\
    		args = PTR_ALIGN(args, sizeof(type));			\
    		value = *(typeof(type) *)args;				\
    	}								\
    	args += sizeof(type);						\
    	value;								\
    })
    
    	/* Make sure end is always >= buf */
    	if (end < buf) {
    		end = ((void *)-1);
    		size = end - buf;
    	}
    
    
    	while (*fmt) {
    		const char *old_fmt = fmt;