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 :-)
16 #ifdef USE_CUSTOM_PRINTF
18 #define noinline_for_stack /*nothing*/
19 #define likely(expr) (expr)
20 #define unlikely(expr) (expr)
22 #define do_div(n, d) ({ __typeof(num) t = n % d; n /= d; t; })
25 #define isdigit(a) ((unsigned char)((a) - '0') <= 9)
28 int skip_atoi(const char **s)
34 i = i*10 + *p++ - '0';
40 /* Decimal conversion is by far the most typical, and is used
41 * for /proc and /sys data. This directly impacts e.g. top performance
42 * with many processes running. We optimize it for speed
43 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
44 * (with permission from the author, Douglas W. Jones).
47 #if LONG_MAX != 0x7fffffffUL || LLONG_MAX != 0x7fffffffffffffffULL
48 /* Formats correctly any integer in [0, 999999999] */
49 static noinline_for_stack
50 char *put_dec_full9(char *buf, unsigned q)
54 /* Possible ways to approx. divide by 10
55 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
56 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
57 * (x * 0x6667) >> 18 x < 43699
58 * (x * 0x3334) >> 17 x < 16389
59 * (x * 0x199a) >> 16 x < 16389
60 * (x * 0x0ccd) >> 15 x < 16389
61 * (x * 0x0667) >> 14 x < 2739
62 * (x * 0x0334) >> 13 x < 1029
63 * (x * 0x019a) >> 12 x < 1029
64 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
65 * (x * 0x0067) >> 10 x < 179
66 * (x * 0x0034) >> 9 x < 69 same
67 * (x * 0x001a) >> 8 x < 69 same
68 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
69 * (x * 0x0007) >> 6 x < 19
70 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
72 r = (q * (uint64_t)0x1999999a) >> 32;
73 *buf++ = (q - 10 * r) + '0'; /* 1 */
74 q = (r * (uint64_t)0x1999999a) >> 32;
75 *buf++ = (r - 10 * q) + '0'; /* 2 */
76 r = (q * (uint64_t)0x1999999a) >> 32;
77 *buf++ = (q - 10 * r) + '0'; /* 3 */
78 q = (r * (uint64_t)0x1999999a) >> 32;
79 *buf++ = (r - 10 * q) + '0'; /* 4 */
80 r = (q * (uint64_t)0x1999999a) >> 32;
81 *buf++ = (q - 10 * r) + '0'; /* 5 */
82 /* Now value is under 10000, can avoid 64-bit multiply */
83 q = (r * 0x199a) >> 16;
84 *buf++ = (r - 10 * q) + '0'; /* 6 */
86 *buf++ = (q - 10 * r) + '0'; /* 7 */
88 *buf++ = (r - 10 * q) + '0'; /* 8 */
89 *buf++ = q + '0'; /* 9 */
94 /* Similar to above but do not pad with zeros.
95 * Code can be easily arranged to print 9 digits too, but our callers
96 * always call put_dec_full9() instead when the number has 9 decimal digits.
98 static noinline_for_stack
99 char *put_dec_trunc8(char *buf, unsigned r)
103 /* Copy of previous function's body with added early returns */
104 q = (r * (uint64_t)0x1999999a) >> 32;
105 *buf++ = (r - 10 * q) + '0'; /* 2 */
106 if (q == 0) return buf;
107 r = (q * (uint64_t)0x1999999a) >> 32;
108 *buf++ = (q - 10 * r) + '0'; /* 3 */
109 if (r == 0) return buf;
110 q = (r * (uint64_t)0x1999999a) >> 32;
111 *buf++ = (r - 10 * q) + '0'; /* 4 */
112 if (q == 0) return buf;
113 r = (q * (uint64_t)0x1999999a) >> 32;
114 *buf++ = (q - 10 * r) + '0'; /* 5 */
115 if (r == 0) return buf;
116 q = (r * 0x199a) >> 16;
117 *buf++ = (r - 10 * q) + '0'; /* 6 */
118 if (q == 0) return buf;
119 r = (q * 0xcd) >> 11;
120 *buf++ = (q - 10 * r) + '0'; /* 7 */
121 if (r == 0) return buf;
122 q = (r * 0xcd) >> 11;
123 *buf++ = (r - 10 * q) + '0'; /* 8 */
124 if (q == 0) return buf;
125 *buf++ = q + '0'; /* 9 */
129 /* There are two algorithms to print larger numbers.
130 * One is generic: divide by 1000000000 and repeatedly print
131 * groups of (up to) 9 digits. It's conceptually simple,
132 * but requires a (unsigned long long) / 1000000000 division.
134 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
135 * manipulates them cleverly and generates groups of 4 decimal digits.
136 * It so happens that it does NOT require long long division.
138 * If long is > 32 bits, division of 64-bit values is relatively easy,
139 * and we will use the first algorithm.
140 * If long long is > 64 bits (strange architecture with VERY large long long),
141 * second algorithm can't be used, and we again use the first one.
143 * Else (if long is 32 bits and long long is 64 bits) we use second one.
146 #if LONG_MAX != 0x7fffffffUL || LLONG_MAX != 0x7fffffffffffffffULL
148 /* First algorithm: generic */
151 char *put_dec(char *buf, unsigned long long n)
153 if (n >= 100*1000*1000) {
154 while (n >= 1000*1000*1000)
155 buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
156 if (n >= 100*1000*1000)
157 return put_dec_full9(buf, n);
159 return put_dec_trunc8(buf, n);
164 /* Second algorithm: valid only for 64-bit long longs */
166 static noinline_for_stack
167 char *put_dec_full4(char *buf, unsigned q)
170 r = (q * 0xcccd) >> 19;
171 *buf++ = (q - 10 * r) + '0';
172 q = (r * 0x199a) >> 16;
173 *buf++ = (r - 10 * q) + '0';
174 r = (q * 0xcd) >> 11;
175 *buf++ = (q - 10 * r) + '0';
180 /* Based on code by Douglas W. Jones found at
181 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
182 * (with permission from the author).
183 * Performs no 64-bit division and hence should be fast on 32-bit machines.
186 char *put_dec(char *buf, unsigned long long n)
188 uint32_t d3, d2, d1, q, h;
190 if (n < 100*1000*1000)
191 return put_dec_trunc8(buf, n);
193 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
196 d3 = (h >> 16); /* implicit "& 0xffff" */
198 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
200 buf = put_dec_full4(buf, q % 10000);
203 d1 = q + 7671 * d3 + 9496 * d2 + 6 * d1;
204 buf = put_dec_full4(buf, d1 % 10000);
207 d2 = q + 4749 * d3 + 42 * d2;
208 buf = put_dec_full4(buf, d2 % 10000);
214 buf = put_dec_full4(buf, d3 % 10000);
218 buf = put_dec_full4(buf, q);
220 while (buf[-1] == '0')
229 * For strace, the following formats are not supported:
230 * %h[h]u, %zu, %tu - use [unsigned] int/long/long long fmt instead
231 * %8.4u - no precision field for integers allowed (ok for strings)
232 * %+d, % d - no forced sign or force "space positive" sign
233 * %-07u - use %-7u instead
237 #define ZEROPAD 1 /* pad with zero */
238 #define SIGN 2 /* unsigned/signed long */
239 //#define PLUS 4 /* show plus */
240 //#define SPACE 8 /* space if plus */
241 #define LEFT 16 /* left justified */
242 //#deefine SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
243 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
246 FORMAT_TYPE_NONE, /* Just a string part */
248 FORMAT_TYPE_PRECISION,
252 FORMAT_TYPE_PERCENT_CHAR,
254 FORMAT_TYPE_LONG_LONG,
262 uint8_t type; /* format_type enum */
263 uint8_t flags; /* flags to number() */
264 uint8_t base; /* number base, 8, 10 or 16 only */
265 uint8_t qualifier; /* number qualifier, one of 'hHlLtzZ' */
266 int field_width; /* width of output field */
267 int precision; /* # of digits/chars */
270 static noinline_for_stack
271 char *number(char *buf, char *end, unsigned long long num,
272 struct printf_spec spec)
274 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
275 static const char digits[16] = "0123456789abcdef"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
277 char tmp[sizeof(long long)*3 + 4];
279 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
282 /* We may overflow the buf. Crudely check for it */
283 i = sizeof(long long)*3 + 4;
284 if (i < spec.field_width)
285 i = spec.field_width;
286 if ((end - buf) <= i)
289 //we don't use formats like "%-07u"
290 // if (spec.flags & LEFT)
291 // spec.flags &= ~ZEROPAD;
293 if (spec.flags & SIGN) {
294 if ((signed long long)num < 0) {
296 num = -(signed long long)num;
298 // } else if (spec.flags & PLUS) {
300 // spec.field_width--;
301 // } else if (spec.flags & SPACE) {
303 // spec.field_width--;
312 /* generate full string in tmp[], in reverse order */
315 tmp[i++] = digits[num];
316 /* Generic code, for any base:
318 tmp[i++] = (digits[do_div(num,base)]);
321 else if (spec.base != 10) { /* 8 or 16 */
322 int mask = spec.base - 1;
328 tmp[i++] = digits[((unsigned char)num) & mask];
331 } else { /* base 10 */
332 i = put_dec(tmp, num) - tmp;
335 //spec.precision is assumed 0 ("not specified")
336 // /* printing 100 using %2d gives "100", not "00" */
337 // if (i > spec.precision)
338 // spec.precision = i;
339 // /* leading space padding */
340 // spec.field_width -= spec.precision;
341 spec.field_width -= i;
342 if (!(spec.flags & (ZEROPAD+LEFT))) {
343 while (--spec.field_width >= 0) {
355 /* "0x" / "0" prefix */
360 if (spec.base == 16) {
366 /* zero or space padding */
367 if (!(spec.flags & LEFT)) {
368 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
369 while (--spec.field_width >= 0) {
375 // /* hmm even more zero padding? */
376 // while (i <= --spec.precision) {
381 /* actual digits of result */
387 /* trailing space padding */
388 while (--spec.field_width >= 0) {
397 static noinline_for_stack
398 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
405 len = strnlen(s, spec.precision);
407 /* We may overflow the buf. Crudely check for it */
409 if (i < spec.field_width)
410 i = spec.field_width;
411 if ((end - buf) <= i)
414 if (!(spec.flags & LEFT)) {
415 while (len < spec.field_width--) {
421 for (i = 0; i < len; ++i) {
426 while (len < spec.field_width--) {
435 static noinline_for_stack
436 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
437 struct printf_spec spec)
439 // spec.flags |= SMALL;
440 if (spec.field_width == -1) {
441 spec.field_width = 2 * sizeof(void *);
442 spec.flags |= ZEROPAD;
446 return number(buf, end, (unsigned long) ptr, spec);
450 * Helper function to decode printf style format.
451 * Each call decode a token from the format and return the
452 * number of characters read (or likely the delta where it wants
453 * to go on the next call).
454 * The decoded token is returned through the parameters
456 * 'h', 'l', or 'L' for integer fields
457 * 'z' support added 23/7/1999 S.H.
458 * 'z' changed to 'Z' --davidm 1/25/99
459 * 't' added for ptrdiff_t
461 * @fmt: the format string
462 * @type of the token returned
463 * @flags: various flags such as +, -, # tokens..
464 * @field_width: overwritten width
465 * @base: base of the number (octal, hex, ...)
466 * @precision: precision of a number
467 * @qualifier: qualifier of a number (long, size_t, ...)
469 static noinline_for_stack
470 int format_decode(const char *fmt, struct printf_spec *spec)
472 const char *start = fmt;
474 /* we finished early by reading the field width */
475 if (spec->type == FORMAT_TYPE_WIDTH) {
476 if (spec->field_width < 0) {
477 spec->field_width = -spec->field_width;
480 spec->type = FORMAT_TYPE_NONE;
484 /* we finished early by reading the precision */
485 if (spec->type == FORMAT_TYPE_PRECISION) {
486 if (spec->precision < 0)
489 spec->type = FORMAT_TYPE_NONE;
494 spec->type = FORMAT_TYPE_NONE;
504 /* Return the current non-format string */
511 while (1) { /* this also skips first '%' */
517 case '-': spec->flags |= LEFT; break;
518 // case '+': spec->flags |= PLUS; break;
519 // case ' ': spec->flags |= SPACE; break;
520 case '#': spec->flags |= SPECIAL; break;
521 case '0': spec->flags |= ZEROPAD; break;
522 default: found = false;
529 /* get field width */
530 spec->field_width = -1;
533 spec->field_width = skip_atoi(&fmt);
534 else if (*fmt == '*') {
535 /* it's the next argument */
536 spec->type = FORMAT_TYPE_WIDTH;
537 return ++fmt - start;
541 /* get the precision */
542 spec->precision = -1;
546 spec->precision = skip_atoi(&fmt);
547 // if (spec->precision < 0)
548 // spec->precision = 0;
549 } else if (*fmt == '*') {
550 /* it's the next argument */
551 spec->type = FORMAT_TYPE_PRECISION;
552 return ++fmt - start;
557 /* get the conversion qualifier */
558 spec->qualifier = -1;
560 spec->qualifier = *fmt++;
561 if (unlikely(spec->qualifier == *fmt)) {
562 spec->qualifier = 'L';
571 spec->type = FORMAT_TYPE_CHAR;
572 return ++fmt - start;
575 spec->type = FORMAT_TYPE_STR;
576 return ++fmt - start;
579 spec->type = FORMAT_TYPE_PTR;
580 return ++fmt - start;
583 spec->type = FORMAT_TYPE_PERCENT_CHAR;
584 return ++fmt - start;
586 /* integer number formats - set up the flags and "break" */
592 // spec->flags |= SMALL;
605 spec->type = FORMAT_TYPE_INVALID;
609 if (spec->qualifier == 'L')
610 spec->type = FORMAT_TYPE_LONG_LONG;
611 else if (spec->qualifier == 'l') {
612 if (spec->flags & SIGN)
613 spec->type = FORMAT_TYPE_LONG;
615 spec->type = FORMAT_TYPE_ULONG;
617 if (spec->flags & SIGN)
618 spec->type = FORMAT_TYPE_INT;
620 spec->type = FORMAT_TYPE_UINT;
623 return ++fmt - start;
627 * vsnprintf - Format a string and place it in a buffer
628 * @buf: The buffer to place the result into
629 * @size: The size of the buffer, including the trailing null space
630 * @fmt: The format string to use
631 * @args: Arguments for the format string
633 * The return value is the number of characters which would
634 * be generated for the given input, excluding the trailing
635 * '\0', as per ISO C99. If you want to have the exact
636 * number of characters written into @buf as return value
637 * (not including the trailing '\0'), use vscnprintf(). If the
638 * return is greater than or equal to @size, the resulting
639 * string is truncated.
641 * If you're not already dealing with a va_list consider using snprintf().
644 int kernel_vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
646 unsigned long long num;
648 struct printf_spec spec = {0};
654 const char *old_fmt = fmt;
655 int read = format_decode(fmt, &spec);
660 case FORMAT_TYPE_NONE: {
663 if (copy > end - str)
665 memcpy(str, old_fmt, copy);
671 case FORMAT_TYPE_WIDTH:
672 spec.field_width = va_arg(args, int);
675 case FORMAT_TYPE_PRECISION:
676 spec.precision = va_arg(args, int);
679 case FORMAT_TYPE_CHAR: {
682 if (!(spec.flags & LEFT)) {
683 while (--spec.field_width > 0) {
690 c = (unsigned char) va_arg(args, int);
694 while (--spec.field_width > 0) {
702 case FORMAT_TYPE_STR:
703 str = string(str, end, va_arg(args, char *), spec);
706 case FORMAT_TYPE_PTR:
707 str = pointer(fmt+1, str, end, va_arg(args, void *),
709 // while (isalnum(*fmt))
713 case FORMAT_TYPE_PERCENT_CHAR:
719 case FORMAT_TYPE_INVALID:
727 case FORMAT_TYPE_LONG_LONG:
728 num = va_arg(args, long long);
730 case FORMAT_TYPE_ULONG:
731 num = va_arg(args, unsigned long);
733 case FORMAT_TYPE_LONG:
734 num = va_arg(args, long);
736 case FORMAT_TYPE_INT:
737 num = (int) va_arg(args, int);
740 num = va_arg(args, unsigned int);
743 str = number(str, end, num, spec);
754 /* the trailing null byte doesn't count towards the total */
759 int strace_vfprintf(FILE *fp, const char *fmt, va_list args)
762 static unsigned buflen;
768 unsigned len = kernel_vsnprintf(buf, buflen, fmt, a1);
774 buf = malloc(buflen);
775 /*len =*/ kernel_vsnprintf(buf, buflen, fmt, args);
778 r = fputs_unlocked(buf, fp);
783 #endif /* USE_CUSTOM_PRINTF */