2 * Taken from Linux kernel's linux/lib/vsprintf.c
3 * and somewhat simplified.
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
19 #ifndef HAVE_FPUTS_UNLOCKED
20 # define fputs_unlocked fputs
23 #define noinline_for_stack /*nothing*/
24 #define likely(expr) (expr)
25 #define unlikely(expr) (expr)
27 #define do_div(n, d) ({ __typeof(num) t = n % d; n /= d; t; })
30 #define isdigit(a) ((unsigned char)((a) - '0') <= 9)
33 int skip_atoi(const char **s)
39 i = i*10 + *p++ - '0';
45 /* Decimal conversion is by far the most typical, and is used
46 * for /proc and /sys data. This directly impacts e.g. top performance
47 * with many processes running. We optimize it for speed
48 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
49 * (with permission from the author, Douglas W. Jones).
52 #if LONG_MAX != 0x7fffffffUL || LLONG_MAX != 0x7fffffffffffffffULL
53 /* Formats correctly any integer in [0, 999999999] */
54 static noinline_for_stack
55 char *put_dec_full9(char *buf, unsigned q)
59 /* Possible ways to approx. divide by 10
60 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
61 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
62 * (x * 0x6667) >> 18 x < 43699
63 * (x * 0x3334) >> 17 x < 16389
64 * (x * 0x199a) >> 16 x < 16389
65 * (x * 0x0ccd) >> 15 x < 16389
66 * (x * 0x0667) >> 14 x < 2739
67 * (x * 0x0334) >> 13 x < 1029
68 * (x * 0x019a) >> 12 x < 1029
69 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
70 * (x * 0x0067) >> 10 x < 179
71 * (x * 0x0034) >> 9 x < 69 same
72 * (x * 0x001a) >> 8 x < 69 same
73 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
74 * (x * 0x0007) >> 6 x < 19
75 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
77 r = (q * (uint64_t)0x1999999a) >> 32;
78 *buf++ = (q - 10 * r) + '0'; /* 1 */
79 q = (r * (uint64_t)0x1999999a) >> 32;
80 *buf++ = (r - 10 * q) + '0'; /* 2 */
81 r = (q * (uint64_t)0x1999999a) >> 32;
82 *buf++ = (q - 10 * r) + '0'; /* 3 */
83 q = (r * (uint64_t)0x1999999a) >> 32;
84 *buf++ = (r - 10 * q) + '0'; /* 4 */
85 r = (q * (uint64_t)0x1999999a) >> 32;
86 *buf++ = (q - 10 * r) + '0'; /* 5 */
87 /* Now value is under 10000, can avoid 64-bit multiply */
88 q = (r * 0x199a) >> 16;
89 *buf++ = (r - 10 * q) + '0'; /* 6 */
91 *buf++ = (q - 10 * r) + '0'; /* 7 */
93 *buf++ = (r - 10 * q) + '0'; /* 8 */
94 *buf++ = q + '0'; /* 9 */
99 /* Similar to above but do not pad with zeros.
100 * Code can be easily arranged to print 9 digits too, but our callers
101 * always call put_dec_full9() instead when the number has 9 decimal digits.
103 static noinline_for_stack
104 char *put_dec_trunc8(char *buf, unsigned r)
108 /* Copy of previous function's body with added early returns */
109 q = (r * (uint64_t)0x1999999a) >> 32;
110 *buf++ = (r - 10 * q) + '0'; /* 2 */
111 if (q == 0) return buf;
112 r = (q * (uint64_t)0x1999999a) >> 32;
113 *buf++ = (q - 10 * r) + '0'; /* 3 */
114 if (r == 0) return buf;
115 q = (r * (uint64_t)0x1999999a) >> 32;
116 *buf++ = (r - 10 * q) + '0'; /* 4 */
117 if (q == 0) return buf;
118 r = (q * (uint64_t)0x1999999a) >> 32;
119 *buf++ = (q - 10 * r) + '0'; /* 5 */
120 if (r == 0) return buf;
121 q = (r * 0x199a) >> 16;
122 *buf++ = (r - 10 * q) + '0'; /* 6 */
123 if (q == 0) return buf;
124 r = (q * 0xcd) >> 11;
125 *buf++ = (q - 10 * r) + '0'; /* 7 */
126 if (r == 0) return buf;
127 q = (r * 0xcd) >> 11;
128 *buf++ = (r - 10 * q) + '0'; /* 8 */
129 if (q == 0) return buf;
130 *buf++ = q + '0'; /* 9 */
134 /* There are two algorithms to print larger numbers.
135 * One is generic: divide by 1000000000 and repeatedly print
136 * groups of (up to) 9 digits. It's conceptually simple,
137 * but requires a (unsigned long long) / 1000000000 division.
139 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
140 * manipulates them cleverly and generates groups of 4 decimal digits.
141 * It so happens that it does NOT require long long division.
143 * If long is > 32 bits, division of 64-bit values is relatively easy,
144 * and we will use the first algorithm.
145 * If long long is > 64 bits (strange architecture with VERY large long long),
146 * second algorithm can't be used, and we again use the first one.
148 * Else (if long is 32 bits and long long is 64 bits) we use second one.
151 #if LONG_MAX != 0x7fffffffUL || LLONG_MAX != 0x7fffffffffffffffULL
153 /* First algorithm: generic */
156 char *put_dec(char *buf, unsigned long long n)
158 if (n >= 100*1000*1000) {
159 while (n >= 1000*1000*1000)
160 buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
161 if (n >= 100*1000*1000)
162 return put_dec_full9(buf, n);
164 return put_dec_trunc8(buf, n);
169 /* Second algorithm: valid only for 64-bit long longs */
171 static noinline_for_stack
172 char *put_dec_full4(char *buf, unsigned q)
175 r = (q * 0xcccd) >> 19;
176 *buf++ = (q - 10 * r) + '0';
177 q = (r * 0x199a) >> 16;
178 *buf++ = (r - 10 * q) + '0';
179 r = (q * 0xcd) >> 11;
180 *buf++ = (q - 10 * r) + '0';
185 /* Based on code by Douglas W. Jones found at
186 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
187 * (with permission from the author).
188 * Performs no 64-bit division and hence should be fast on 32-bit machines.
191 char *put_dec(char *buf, unsigned long long n)
193 uint32_t d3, d2, d1, q, h;
195 if (n < 100*1000*1000)
196 return put_dec_trunc8(buf, n);
198 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
201 d3 = (h >> 16); /* implicit "& 0xffff" */
203 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
205 buf = put_dec_full4(buf, q % 10000);
208 d1 = q + 7671 * d3 + 9496 * d2 + 6 * d1;
209 buf = put_dec_full4(buf, d1 % 10000);
212 d2 = q + 4749 * d3 + 42 * d2;
213 buf = put_dec_full4(buf, d2 % 10000);
219 buf = put_dec_full4(buf, d3 % 10000);
223 buf = put_dec_full4(buf, q);
225 while (buf[-1] == '0')
234 * For strace, the following formats are not supported:
235 * %h[h]u, %zu, %tu - use [unsigned] int/long/long long fmt instead
236 * %8.4u - no precision field for integers allowed (ok for strings)
237 * %+d, % d - no forced sign or force "space positive" sign
238 * %-07u - use %-7u instead
242 #define ZEROPAD 1 /* pad with zero */
243 #define SIGN 2 /* unsigned/signed long */
244 //#define PLUS 4 /* show plus */
245 //#define SPACE 8 /* space if plus */
246 #define LEFT 16 /* left justified */
247 //#deefine SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
248 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
251 FORMAT_TYPE_NONE, /* Just a string part */
253 FORMAT_TYPE_PRECISION,
257 FORMAT_TYPE_PERCENT_CHAR,
259 FORMAT_TYPE_LONG_LONG,
267 uint8_t type; /* format_type enum */
268 uint8_t flags; /* flags to number() */
269 uint8_t base; /* number base, 8, 10 or 16 only */
270 uint8_t qualifier; /* number qualifier, one of 'hHlLtzZ' */
271 int field_width; /* width of output field */
272 int precision; /* # of digits/chars */
275 static noinline_for_stack
276 char *number(char *buf, char *end, unsigned long long num,
277 struct printf_spec spec)
279 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
280 static const char digits[16] = "0123456789abcdef"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
282 char tmp[sizeof(long long)*3 + 4];
284 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
287 /* We may overflow the buf. Crudely check for it */
288 i = sizeof(long long)*3 + 4;
289 if (i < spec.field_width)
290 i = spec.field_width;
291 if ((end - buf) <= i)
294 //we don't use formats like "%-07u"
295 // if (spec.flags & LEFT)
296 // spec.flags &= ~ZEROPAD;
298 if (spec.flags & SIGN) {
299 if ((signed long long)num < 0) {
301 num = -(signed long long)num;
303 // } else if (spec.flags & PLUS) {
305 // spec.field_width--;
306 // } else if (spec.flags & SPACE) {
308 // spec.field_width--;
317 /* generate full string in tmp[], in reverse order */
320 tmp[i++] = digits[num];
321 /* Generic code, for any base:
323 tmp[i++] = (digits[do_div(num,base)]);
326 else if (spec.base != 10) { /* 8 or 16 */
327 int mask = spec.base - 1;
333 tmp[i++] = digits[((unsigned char)num) & mask];
336 } else { /* base 10 */
337 i = put_dec(tmp, num) - tmp;
340 //spec.precision is assumed 0 ("not specified")
341 // /* printing 100 using %2d gives "100", not "00" */
342 // if (i > spec.precision)
343 // spec.precision = i;
344 // /* leading space padding */
345 // spec.field_width -= spec.precision;
346 spec.field_width -= i;
347 if (!(spec.flags & (ZEROPAD+LEFT))) {
348 while (--spec.field_width >= 0) {
360 /* "0x" / "0" prefix */
365 if (spec.base == 16) {
371 /* zero or space padding */
372 if (!(spec.flags & LEFT)) {
373 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
374 while (--spec.field_width >= 0) {
380 // /* hmm even more zero padding? */
381 // while (i <= --spec.precision) {
386 /* actual digits of result */
392 /* trailing space padding */
393 while (--spec.field_width >= 0) {
402 static noinline_for_stack
403 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
410 len = strnlen(s, spec.precision);
412 /* We may overflow the buf. Crudely check for it */
414 if (i < spec.field_width)
415 i = spec.field_width;
416 if ((end - buf) <= i)
419 if (!(spec.flags & LEFT)) {
420 while (len < spec.field_width--) {
426 for (i = 0; i < len; ++i) {
431 while (len < spec.field_width--) {
440 static noinline_for_stack
441 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
442 struct printf_spec spec)
444 // spec.flags |= SMALL;
445 if (spec.field_width == -1) {
446 spec.field_width = 2 * sizeof(void *);
447 spec.flags |= ZEROPAD;
451 return number(buf, end, (unsigned long) ptr, spec);
455 * Helper function to decode printf style format.
456 * Each call decode a token from the format and return the
457 * number of characters read (or likely the delta where it wants
458 * to go on the next call).
459 * The decoded token is returned through the parameters
461 * 'h', 'l', or 'L' for integer fields
462 * 'z' support added 23/7/1999 S.H.
463 * 'z' changed to 'Z' --davidm 1/25/99
464 * 't' added for ptrdiff_t
466 * @fmt: the format string
467 * @type of the token returned
468 * @flags: various flags such as +, -, # tokens..
469 * @field_width: overwritten width
470 * @base: base of the number (octal, hex, ...)
471 * @precision: precision of a number
472 * @qualifier: qualifier of a number (long, size_t, ...)
474 static noinline_for_stack
475 int format_decode(const char *fmt, struct printf_spec *spec)
477 const char *start = fmt;
479 /* we finished early by reading the field width */
480 if (spec->type == FORMAT_TYPE_WIDTH) {
481 if (spec->field_width < 0) {
482 spec->field_width = -spec->field_width;
485 spec->type = FORMAT_TYPE_NONE;
489 /* we finished early by reading the precision */
490 if (spec->type == FORMAT_TYPE_PRECISION) {
491 if (spec->precision < 0)
494 spec->type = FORMAT_TYPE_NONE;
499 spec->type = FORMAT_TYPE_NONE;
509 /* Return the current non-format string */
516 while (1) { /* this also skips first '%' */
522 case '-': spec->flags |= LEFT; break;
523 // case '+': spec->flags |= PLUS; break;
524 // case ' ': spec->flags |= SPACE; break;
525 case '#': spec->flags |= SPECIAL; break;
526 case '0': spec->flags |= ZEROPAD; break;
527 default: found = false;
534 /* get field width */
535 spec->field_width = -1;
538 spec->field_width = skip_atoi(&fmt);
539 else if (*fmt == '*') {
540 /* it's the next argument */
541 spec->type = FORMAT_TYPE_WIDTH;
542 return ++fmt - start;
546 /* get the precision */
547 spec->precision = -1;
551 spec->precision = skip_atoi(&fmt);
552 // if (spec->precision < 0)
553 // spec->precision = 0;
554 } else if (*fmt == '*') {
555 /* it's the next argument */
556 spec->type = FORMAT_TYPE_PRECISION;
557 return ++fmt - start;
562 /* get the conversion qualifier */
563 spec->qualifier = -1;
565 spec->qualifier = *fmt++;
566 if (unlikely(spec->qualifier == *fmt)) {
567 spec->qualifier = 'L';
576 spec->type = FORMAT_TYPE_CHAR;
577 return ++fmt - start;
580 spec->type = FORMAT_TYPE_STR;
581 return ++fmt - start;
584 spec->type = FORMAT_TYPE_PTR;
585 return ++fmt - start;
588 spec->type = FORMAT_TYPE_PERCENT_CHAR;
589 return ++fmt - start;
591 /* integer number formats - set up the flags and "break" */
597 // spec->flags |= SMALL;
610 spec->type = FORMAT_TYPE_INVALID;
614 if (spec->qualifier == 'L')
615 spec->type = FORMAT_TYPE_LONG_LONG;
616 else if (spec->qualifier == 'l') {
617 if (spec->flags & SIGN)
618 spec->type = FORMAT_TYPE_LONG;
620 spec->type = FORMAT_TYPE_ULONG;
622 if (spec->flags & SIGN)
623 spec->type = FORMAT_TYPE_INT;
625 spec->type = FORMAT_TYPE_UINT;
628 return ++fmt - start;
632 * vsnprintf - Format a string and place it in a buffer
633 * @buf: The buffer to place the result into
634 * @size: The size of the buffer, including the trailing null space
635 * @fmt: The format string to use
636 * @args: Arguments for the format string
638 * The return value is the number of characters which would
639 * be generated for the given input, excluding the trailing
640 * '\0', as per ISO C99. If you want to have the exact
641 * number of characters written into @buf as return value
642 * (not including the trailing '\0'), use vscnprintf(). If the
643 * return is greater than or equal to @size, the resulting
644 * string is truncated.
646 * If you're not already dealing with a va_list consider using snprintf().
649 int kernel_vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
651 unsigned long long num;
653 struct printf_spec spec = {0};
659 const char *old_fmt = fmt;
660 int read = format_decode(fmt, &spec);
665 case FORMAT_TYPE_NONE: {
668 if (copy > end - str)
670 memcpy(str, old_fmt, copy);
676 case FORMAT_TYPE_WIDTH:
677 spec.field_width = va_arg(args, int);
680 case FORMAT_TYPE_PRECISION:
681 spec.precision = va_arg(args, int);
684 case FORMAT_TYPE_CHAR: {
687 if (!(spec.flags & LEFT)) {
688 while (--spec.field_width > 0) {
695 c = (unsigned char) va_arg(args, int);
699 while (--spec.field_width > 0) {
707 case FORMAT_TYPE_STR:
708 str = string(str, end, va_arg(args, char *), spec);
711 case FORMAT_TYPE_PTR:
712 str = pointer(fmt+1, str, end, va_arg(args, void *),
714 // while (isalnum(*fmt))
718 case FORMAT_TYPE_PERCENT_CHAR:
724 case FORMAT_TYPE_INVALID:
732 case FORMAT_TYPE_LONG_LONG:
733 num = va_arg(args, long long);
735 case FORMAT_TYPE_ULONG:
736 num = va_arg(args, unsigned long);
738 case FORMAT_TYPE_LONG:
739 num = va_arg(args, long);
741 case FORMAT_TYPE_INT:
742 num = (int) va_arg(args, int);
745 num = va_arg(args, unsigned int);
748 str = number(str, end, num, spec);
759 /* the trailing null byte doesn't count towards the total */
764 int strace_vfprintf(FILE *fp, const char *fmt, va_list args)
766 static char *buf = NULL;
767 static unsigned buflen = 0;
773 unsigned len = kernel_vsnprintf(buf, buflen, fmt, a1);
779 buf = xmalloc(buflen);
780 /*len =*/ kernel_vsnprintf(buf, buflen, fmt, args);
783 r = fputs_unlocked(buf, fp);
788 #endif /* USE_CUSTOM_PRINTF */