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 :-)
14 #ifdef USE_CUSTOM_PRINTF
19 #define noinline_for_stack /*nothing*/
20 #define likely(expr) (expr)
21 #define unlikely(expr) (expr)
23 #define do_div(n, d) ({ __typeof(num) t = n % d; n /= d; t; })
26 #define isdigit(a) ((unsigned char)((a) - '0') <= 9)
29 int skip_atoi(const char **s)
35 i = i*10 + *p++ - '0';
41 /* Decimal conversion is by far the most typical, and is used
42 * for /proc and /sys data. This directly impacts e.g. top performance
43 * with many processes running. We optimize it for speed
44 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
45 * (with permission from the author, Douglas W. Jones).
48 #if LONG_MAX != 0x7fffffffUL || LLONG_MAX != 0x7fffffffffffffffULL
49 /* Formats correctly any integer in [0, 999999999] */
50 static noinline_for_stack
51 char *put_dec_full9(char *buf, unsigned q)
55 /* Possible ways to approx. divide by 10
56 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
57 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
58 * (x * 0x6667) >> 18 x < 43699
59 * (x * 0x3334) >> 17 x < 16389
60 * (x * 0x199a) >> 16 x < 16389
61 * (x * 0x0ccd) >> 15 x < 16389
62 * (x * 0x0667) >> 14 x < 2739
63 * (x * 0x0334) >> 13 x < 1029
64 * (x * 0x019a) >> 12 x < 1029
65 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
66 * (x * 0x0067) >> 10 x < 179
67 * (x * 0x0034) >> 9 x < 69 same
68 * (x * 0x001a) >> 8 x < 69 same
69 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
70 * (x * 0x0007) >> 6 x < 19
71 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
73 r = (q * (uint64_t)0x1999999a) >> 32;
74 *buf++ = (q - 10 * r) + '0'; /* 1 */
75 q = (r * (uint64_t)0x1999999a) >> 32;
76 *buf++ = (r - 10 * q) + '0'; /* 2 */
77 r = (q * (uint64_t)0x1999999a) >> 32;
78 *buf++ = (q - 10 * r) + '0'; /* 3 */
79 q = (r * (uint64_t)0x1999999a) >> 32;
80 *buf++ = (r - 10 * q) + '0'; /* 4 */
81 r = (q * (uint64_t)0x1999999a) >> 32;
82 *buf++ = (q - 10 * r) + '0'; /* 5 */
83 /* Now value is under 10000, can avoid 64-bit multiply */
84 q = (r * 0x199a) >> 16;
85 *buf++ = (r - 10 * q) + '0'; /* 6 */
87 *buf++ = (q - 10 * r) + '0'; /* 7 */
89 *buf++ = (r - 10 * q) + '0'; /* 8 */
90 *buf++ = q + '0'; /* 9 */
95 /* Similar to above but do not pad with zeros.
96 * Code can be easily arranged to print 9 digits too, but our callers
97 * always call put_dec_full9() instead when the number has 9 decimal digits.
99 static noinline_for_stack
100 char *put_dec_trunc8(char *buf, unsigned r)
104 /* Copy of previous function's body with added early returns */
105 q = (r * (uint64_t)0x1999999a) >> 32;
106 *buf++ = (r - 10 * q) + '0'; /* 2 */
107 if (q == 0) return buf;
108 r = (q * (uint64_t)0x1999999a) >> 32;
109 *buf++ = (q - 10 * r) + '0'; /* 3 */
110 if (r == 0) return buf;
111 q = (r * (uint64_t)0x1999999a) >> 32;
112 *buf++ = (r - 10 * q) + '0'; /* 4 */
113 if (q == 0) return buf;
114 r = (q * (uint64_t)0x1999999a) >> 32;
115 *buf++ = (q - 10 * r) + '0'; /* 5 */
116 if (r == 0) return buf;
117 q = (r * 0x199a) >> 16;
118 *buf++ = (r - 10 * q) + '0'; /* 6 */
119 if (q == 0) return buf;
120 r = (q * 0xcd) >> 11;
121 *buf++ = (q - 10 * r) + '0'; /* 7 */
122 if (r == 0) return buf;
123 q = (r * 0xcd) >> 11;
124 *buf++ = (r - 10 * q) + '0'; /* 8 */
125 if (q == 0) return buf;
126 *buf++ = q + '0'; /* 9 */
130 /* There are two algorithms to print larger numbers.
131 * One is generic: divide by 1000000000 and repeatedly print
132 * groups of (up to) 9 digits. It's conceptually simple,
133 * but requires a (unsigned long long) / 1000000000 division.
135 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
136 * manipulates them cleverly and generates groups of 4 decimal digits.
137 * It so happens that it does NOT require long long division.
139 * If long is > 32 bits, division of 64-bit values is relatively easy,
140 * and we will use the first algorithm.
141 * If long long is > 64 bits (strange architecture with VERY large long long),
142 * second algorithm can't be used, and we again use the first one.
144 * Else (if long is 32 bits and long long is 64 bits) we use second one.
147 #if LONG_MAX != 0x7fffffffUL || LLONG_MAX != 0x7fffffffffffffffULL
149 /* First algorithm: generic */
152 char *put_dec(char *buf, unsigned long long n)
154 if (n >= 100*1000*1000) {
155 while (n >= 1000*1000*1000)
156 buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
157 if (n >= 100*1000*1000)
158 return put_dec_full9(buf, n);
160 return put_dec_trunc8(buf, n);
165 /* Second algorithm: valid only for 64-bit long longs */
167 static noinline_for_stack
168 char *put_dec_full4(char *buf, unsigned q)
171 r = (q * 0xcccd) >> 19;
172 *buf++ = (q - 10 * r) + '0';
173 q = (r * 0x199a) >> 16;
174 *buf++ = (r - 10 * q) + '0';
175 r = (q * 0xcd) >> 11;
176 *buf++ = (q - 10 * r) + '0';
181 /* Based on code by Douglas W. Jones found at
182 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
183 * (with permission from the author).
184 * Performs no 64-bit division and hence should be fast on 32-bit machines.
187 char *put_dec(char *buf, unsigned long long n)
189 uint32_t d3, d2, d1, q, h;
191 if (n < 100*1000*1000)
192 return put_dec_trunc8(buf, n);
194 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
197 d3 = (h >> 16); /* implicit "& 0xffff" */
199 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
201 buf = put_dec_full4(buf, q % 10000);
204 d1 = q + 7671 * d3 + 9496 * d2 + 6 * d1;
205 buf = put_dec_full4(buf, d1 % 10000);
208 d2 = q + 4749 * d3 + 42 * d2;
209 buf = put_dec_full4(buf, d2 % 10000);
215 buf = put_dec_full4(buf, d3 % 10000);
219 buf = put_dec_full4(buf, q);
221 while (buf[-1] == '0')
230 * For strace, the following formats are not supported:
231 * %h[h]u, %zu, %tu - use [unsigned] int/long/long long fmt instead
232 * %8.4u - no precision field for integers allowed (ok for strings)
233 * %+d, % d - no forced sign or force "space positive" sign
234 * %-07u - use %-7u instead
238 #define ZEROPAD 1 /* pad with zero */
239 #define SIGN 2 /* unsigned/signed long */
240 //#define PLUS 4 /* show plus */
241 //#define SPACE 8 /* space if plus */
242 #define LEFT 16 /* left justified */
243 //#deefine SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
244 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
247 FORMAT_TYPE_NONE, /* Just a string part */
249 FORMAT_TYPE_PRECISION,
253 FORMAT_TYPE_PERCENT_CHAR,
255 FORMAT_TYPE_LONG_LONG,
263 uint8_t type; /* format_type enum */
264 uint8_t flags; /* flags to number() */
265 uint8_t base; /* number base, 8, 10 or 16 only */
266 uint8_t qualifier; /* number qualifier, one of 'hHlLtzZ' */
267 int field_width; /* width of output field */
268 int precision; /* # of digits/chars */
271 static noinline_for_stack
272 char *number(char *buf, char *end, unsigned long long num,
273 struct printf_spec spec)
275 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
276 static const char digits[16] = "0123456789abcdef"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
278 char tmp[sizeof(long long)*3 + 4];
280 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
283 /* We may overflow the buf. Crudely check for it */
284 i = sizeof(long long)*3 + 4;
285 if (i < spec.field_width)
286 i = spec.field_width;
287 if ((end - buf) <= i)
290 //we don't use formats like "%-07u"
291 // if (spec.flags & LEFT)
292 // spec.flags &= ~ZEROPAD;
294 if (spec.flags & SIGN) {
295 if ((signed long long)num < 0) {
297 num = -(signed long long)num;
299 // } else if (spec.flags & PLUS) {
301 // spec.field_width--;
302 // } else if (spec.flags & SPACE) {
304 // spec.field_width--;
313 /* generate full string in tmp[], in reverse order */
316 tmp[i++] = digits[num];
317 /* Generic code, for any base:
319 tmp[i++] = (digits[do_div(num,base)]);
322 else if (spec.base != 10) { /* 8 or 16 */
323 int mask = spec.base - 1;
329 tmp[i++] = digits[((unsigned char)num) & mask];
332 } else { /* base 10 */
333 i = put_dec(tmp, num) - tmp;
336 //spec.precision is assumed 0 ("not specified")
337 // /* printing 100 using %2d gives "100", not "00" */
338 // if (i > spec.precision)
339 // spec.precision = i;
340 // /* leading space padding */
341 // spec.field_width -= spec.precision;
342 spec.field_width -= i;
343 if (!(spec.flags & (ZEROPAD+LEFT))) {
344 while (--spec.field_width >= 0) {
356 /* "0x" / "0" prefix */
361 if (spec.base == 16) {
367 /* zero or space padding */
368 if (!(spec.flags & LEFT)) {
369 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
370 while (--spec.field_width >= 0) {
376 // /* hmm even more zero padding? */
377 // while (i <= --spec.precision) {
382 /* actual digits of result */
388 /* trailing space padding */
389 while (--spec.field_width >= 0) {
398 static noinline_for_stack
399 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
406 len = strnlen(s, spec.precision);
408 /* We may overflow the buf. Crudely check for it */
410 if (i < spec.field_width)
411 i = spec.field_width;
412 if ((end - buf) <= i)
415 if (!(spec.flags & LEFT)) {
416 while (len < spec.field_width--) {
422 for (i = 0; i < len; ++i) {
427 while (len < spec.field_width--) {
436 static noinline_for_stack
437 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
438 struct printf_spec spec)
440 // spec.flags |= SMALL;
441 if (spec.field_width == -1) {
442 spec.field_width = 2 * sizeof(void *);
443 spec.flags |= ZEROPAD;
447 return number(buf, end, (unsigned long) ptr, spec);
451 * Helper function to decode printf style format.
452 * Each call decode a token from the format and return the
453 * number of characters read (or likely the delta where it wants
454 * to go on the next call).
455 * The decoded token is returned through the parameters
457 * 'h', 'l', or 'L' for integer fields
458 * 'z' support added 23/7/1999 S.H.
459 * 'z' changed to 'Z' --davidm 1/25/99
460 * 't' added for ptrdiff_t
462 * @fmt: the format string
463 * @type of the token returned
464 * @flags: various flags such as +, -, # tokens..
465 * @field_width: overwritten width
466 * @base: base of the number (octal, hex, ...)
467 * @precision: precision of a number
468 * @qualifier: qualifier of a number (long, size_t, ...)
470 static noinline_for_stack
471 int format_decode(const char *fmt, struct printf_spec *spec)
473 const char *start = fmt;
475 /* we finished early by reading the field width */
476 if (spec->type == FORMAT_TYPE_WIDTH) {
477 if (spec->field_width < 0) {
478 spec->field_width = -spec->field_width;
481 spec->type = FORMAT_TYPE_NONE;
485 /* we finished early by reading the precision */
486 if (spec->type == FORMAT_TYPE_PRECISION) {
487 if (spec->precision < 0)
490 spec->type = FORMAT_TYPE_NONE;
495 spec->type = FORMAT_TYPE_NONE;
505 /* Return the current non-format string */
512 while (1) { /* this also skips first '%' */
518 case '-': spec->flags |= LEFT; break;
519 // case '+': spec->flags |= PLUS; break;
520 // case ' ': spec->flags |= SPACE; break;
521 case '#': spec->flags |= SPECIAL; break;
522 case '0': spec->flags |= ZEROPAD; break;
523 default: found = false;
530 /* get field width */
531 spec->field_width = -1;
534 spec->field_width = skip_atoi(&fmt);
535 else if (*fmt == '*') {
536 /* it's the next argument */
537 spec->type = FORMAT_TYPE_WIDTH;
538 return ++fmt - start;
542 /* get the precision */
543 spec->precision = -1;
547 spec->precision = skip_atoi(&fmt);
548 // if (spec->precision < 0)
549 // spec->precision = 0;
550 } else if (*fmt == '*') {
551 /* it's the next argument */
552 spec->type = FORMAT_TYPE_PRECISION;
553 return ++fmt - start;
558 /* get the conversion qualifier */
559 spec->qualifier = -1;
561 spec->qualifier = *fmt++;
562 if (unlikely(spec->qualifier == *fmt)) {
563 spec->qualifier = 'L';
572 spec->type = FORMAT_TYPE_CHAR;
573 return ++fmt - start;
576 spec->type = FORMAT_TYPE_STR;
577 return ++fmt - start;
580 spec->type = FORMAT_TYPE_PTR;
581 return ++fmt - start;
584 spec->type = FORMAT_TYPE_PERCENT_CHAR;
585 return ++fmt - start;
587 /* integer number formats - set up the flags and "break" */
593 // spec->flags |= SMALL;
606 spec->type = FORMAT_TYPE_INVALID;
610 if (spec->qualifier == 'L')
611 spec->type = FORMAT_TYPE_LONG_LONG;
612 else if (spec->qualifier == 'l') {
613 if (spec->flags & SIGN)
614 spec->type = FORMAT_TYPE_LONG;
616 spec->type = FORMAT_TYPE_ULONG;
618 if (spec->flags & SIGN)
619 spec->type = FORMAT_TYPE_INT;
621 spec->type = FORMAT_TYPE_UINT;
624 return ++fmt - start;
628 * vsnprintf - Format a string and place it in a buffer
629 * @buf: The buffer to place the result into
630 * @size: The size of the buffer, including the trailing null space
631 * @fmt: The format string to use
632 * @args: Arguments for the format string
634 * The return value is the number of characters which would
635 * be generated for the given input, excluding the trailing
636 * '\0', as per ISO C99. If you want to have the exact
637 * number of characters written into @buf as return value
638 * (not including the trailing '\0'), use vscnprintf(). If the
639 * return is greater than or equal to @size, the resulting
640 * string is truncated.
642 * If you're not already dealing with a va_list consider using snprintf().
645 int kernel_vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
647 unsigned long long num;
649 struct printf_spec spec = {0};
655 const char *old_fmt = fmt;
656 int read = format_decode(fmt, &spec);
661 case FORMAT_TYPE_NONE: {
664 if (copy > end - str)
666 memcpy(str, old_fmt, copy);
672 case FORMAT_TYPE_WIDTH:
673 spec.field_width = va_arg(args, int);
676 case FORMAT_TYPE_PRECISION:
677 spec.precision = va_arg(args, int);
680 case FORMAT_TYPE_CHAR: {
683 if (!(spec.flags & LEFT)) {
684 while (--spec.field_width > 0) {
691 c = (unsigned char) va_arg(args, int);
695 while (--spec.field_width > 0) {
703 case FORMAT_TYPE_STR:
704 str = string(str, end, va_arg(args, char *), spec);
707 case FORMAT_TYPE_PTR:
708 str = pointer(fmt+1, str, end, va_arg(args, void *),
710 // while (isalnum(*fmt))
714 case FORMAT_TYPE_PERCENT_CHAR:
720 case FORMAT_TYPE_INVALID:
728 case FORMAT_TYPE_LONG_LONG:
729 num = va_arg(args, long long);
731 case FORMAT_TYPE_ULONG:
732 num = va_arg(args, unsigned long);
734 case FORMAT_TYPE_LONG:
735 num = va_arg(args, long);
737 case FORMAT_TYPE_INT:
738 num = (int) va_arg(args, int);
741 num = va_arg(args, unsigned int);
744 str = number(str, end, num, spec);
755 /* the trailing null byte doesn't count towards the total */
760 int strace_vfprintf(FILE *fp, const char *fmt, va_list args)
762 static char *buf = NULL;
763 static unsigned buflen = 0;
769 unsigned len = kernel_vsnprintf(buf, buflen, fmt, a1);
775 buf = malloc(buflen);
778 /*len =*/ kernel_vsnprintf(buf, buflen, fmt, args);
781 r = fputs_unlocked(buf, fp);
786 #endif /* USE_CUSTOM_PRINTF */