1 #include "stdio_impl.h"
13 /* Some useful macros */
15 #define MAX(a,b) ((a)>(b) ? (a) : (b))
16 #define MIN(a,b) ((a)<(b) ? (a) : (b))
18 /* Convenient bit representation for modifier flags, which all fall
19 * within 31 codepoints of the space character. */
21 #define ALT_FORM (1U<<'#'-' ')
22 #define ZERO_PAD (1U<<'0'-' ')
23 #define LEFT_ADJ (1U<<'-'-' ')
24 #define PAD_POS (1U<<' '-' ')
25 #define MARK_POS (1U<<'+'-' ')
26 #define GROUPED (1U<<'\''-' ')
28 #define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
30 /* State machine to accept length modifiers + conversion specifiers.
31 * Result is 0 on failure, or an argument type to pop on success. */
34 BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE,
37 PTR, INT, UINT, ULLONG,
39 SHORT, USHORT, CHAR, UCHAR,
40 LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
46 #define S(x) [(x)-'A']
48 static const unsigned char states[]['z'-'A'+1] = {
50 S('d') = INT, S('i') = INT,
51 S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
52 S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
53 S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
54 S('c') = CHAR, S('C') = INT,
55 S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
57 S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
58 S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE,
59 }, { /* 1: l-prefixed */
60 S('d') = LONG, S('i') = LONG,
61 S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
62 S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
63 S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
64 S('c') = INT, S('s') = PTR, S('n') = PTR,
66 }, { /* 2: ll-prefixed */
67 S('d') = LLONG, S('i') = LLONG,
68 S('o') = ULLONG, S('u') = ULLONG,
69 S('x') = ULLONG, S('X') = ULLONG,
71 }, { /* 3: h-prefixed */
72 S('d') = SHORT, S('i') = SHORT,
73 S('o') = USHORT, S('u') = USHORT,
74 S('x') = USHORT, S('X') = USHORT,
77 }, { /* 4: hh-prefixed */
78 S('d') = CHAR, S('i') = CHAR,
79 S('o') = UCHAR, S('u') = UCHAR,
80 S('x') = UCHAR, S('X') = UCHAR,
82 }, { /* 5: L-prefixed */
83 S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL,
84 S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL,
86 }, { /* 6: z- or t-prefixed (assumed to be same size) */
87 S('d') = PDIFF, S('i') = PDIFF,
88 S('o') = SIZET, S('u') = SIZET,
89 S('x') = SIZET, S('X') = SIZET,
91 }, { /* 7: j-prefixed */
92 S('d') = IMAX, S('i') = IMAX,
93 S('o') = UMAX, S('u') = UMAX,
94 S('x') = UMAX, S('X') = UMAX,
99 #define OOB(x) ((unsigned)(x)-'A' > 'z'-'A')
108 static void pop_arg(union arg *arg, int type, va_list *ap)
111 case PTR: arg->p = va_arg(*ap, void *);
112 break; case INT: arg->i = va_arg(*ap, int);
113 break; case UINT: arg->i = va_arg(*ap, unsigned int);
114 break; case LONG: arg->i = va_arg(*ap, long);
115 break; case ULONG: arg->i = va_arg(*ap, unsigned long);
116 break; case ULLONG: arg->i = va_arg(*ap, unsigned long long);
117 break; case SHORT: arg->i = (short)va_arg(*ap, int);
118 break; case USHORT: arg->i = (unsigned short)va_arg(*ap, int);
119 break; case CHAR: arg->i = (signed char)va_arg(*ap, int);
120 break; case UCHAR: arg->i = (unsigned char)va_arg(*ap, int);
121 break; case LLONG: arg->i = va_arg(*ap, long long);
122 break; case SIZET: arg->i = va_arg(*ap, size_t);
123 break; case IMAX: arg->i = va_arg(*ap, intmax_t);
124 break; case UMAX: arg->i = va_arg(*ap, uintmax_t);
125 break; case PDIFF: arg->i = va_arg(*ap, ptrdiff_t);
126 break; case UIPTR: arg->i = (uintptr_t)va_arg(*ap, void *);
127 break; case DBL: arg->f = va_arg(*ap, double);
128 break; case LDBL: arg->f = va_arg(*ap, long double);
132 static void out(FILE *f, const char *s, size_t l)
134 if (!(f->flags & F_ERR)) __fwritex((void *)s, l, f);
137 static void pad(FILE *f, char c, int w, int l, int fl)
140 if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return;
142 memset(pad, c, l>sizeof pad ? sizeof pad : l);
143 for (; l >= sizeof pad; l -= sizeof pad)
144 out(f, pad, sizeof pad);
148 static const char xdigits[16] = {
152 static char *fmt_x(uintmax_t x, char *s, int lower)
154 for (; x; x>>=4) *--s = xdigits[(x&15)]|lower;
158 static char *fmt_o(uintmax_t x, char *s)
160 for (; x; x>>=3) *--s = '0' + (x&7);
164 static char *fmt_u(uintmax_t x, char *s)
167 for ( ; x>ULONG_MAX; x/=10) *--s = '0' + x%10;
168 for (y=x; y; y/=10) *--s = '0' + y%10;
172 /* Do not override this check. The floating point printing code below
173 * depends on the float.h constants being right. If they are wrong, it
174 * may overflow the stack. */
175 #if LDBL_MANT_DIG == 53
176 typedef char compiler_defines_long_double_incorrectly[9-(int)sizeof(long double)];
179 static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
181 uint32_t big[(LDBL_MANT_DIG+28)/29 + 1 // mantissa expansion
182 + (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9]; // exponent expansion
183 uint32_t *a, *d, *r, *z;
184 int e2=0, e, i, j, l;
185 char buf[9+LDBL_MANT_DIG/4], *s;
186 const char *prefix="-0X+0X 0X-0x+0x 0x";
188 char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr;
193 } else if (fl & MARK_POS) {
195 } else if (fl & PAD_POS) {
197 } else prefix++, pl=0;
200 char *s = (t&32)?"inf":"INF";
201 if (y!=y) s=(t&32)?"nan":"NAN";
202 pad(f, ' ', w, 3+pl, fl&~ZERO_PAD);
205 pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);
209 y = frexpl(y, &e2) * 2;
213 long double round = 8.0;
216 if (t&32) prefix += 9;
219 if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
220 else re=LDBL_MANT_DIG/4-1-p;
223 while (re--) round*=16;
235 estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
236 if (estr==ebuf) *--estr='0';
237 *--estr = (e2<0 ? '-' : '+');
238 *--estr = t+('p'-'a');
243 *s++=xdigits[x]|(t&32);
245 if (s-buf==1 && (y||p>0||(fl&ALT_FORM))) *s++='.';
248 if (p > INT_MAX-2-(ebuf-estr)-pl)
250 if (p && s-buf-2 < p)
251 l = (p+2) + (ebuf-estr);
253 l = (s-buf) + (ebuf-estr);
255 pad(f, ' ', w, pl+l, fl);
257 pad(f, '0', w, pl+l, fl^ZERO_PAD);
259 pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
260 out(f, estr, ebuf-estr);
261 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
266 if (y) y *= 0x1p28, e2-=28;
269 else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
273 y = 1000000000*(y-*z++);
279 for (d=z-1; d>=a; d--) {
280 uint64_t x = ((uint64_t)*d<<sh)+carry;
282 carry = x / 1000000000;
284 if (carry) *--a = carry;
285 while (z>a && !z[-1]) z--;
289 uint32_t carry=0, *b;
290 int sh=MIN(9,-e2), need=1+(p+LDBL_MANT_DIG/3U+8)/9;
291 for (d=a; d<z; d++) {
292 uint32_t rm = *d & (1<<sh)-1;
293 *d = (*d>>sh) + carry;
294 carry = (1000000000>>sh) * rm;
297 if (carry) *z++ = carry;
298 /* Avoid (slow!) computation past requested precision */
299 b = (t|32)=='f' ? r : a;
300 if (z-b > need) z = b+need;
304 if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
307 /* Perform rounding: j is precision after the radix (possibly neg) */
308 j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
311 /* We avoid C's broken division of negative numbers */
312 d = r + 1 + ((j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP);
315 for (i=10, j++; j<9; i*=10, j++);
317 /* Are there any significant digits past j? */
319 long double round = 2/LDBL_EPSILON;
321 if ((*d/i & 1) || (i==1000000000 && d>a && (d[-1]&1)))
323 if (x<i/2) small=0x0.8p0;
324 else if (x==i/2 && d+1==z) small=0x1.0p0;
326 if (pl && *prefix=='-') round*=-1, small*=-1;
328 /* Decide whether to round by probing round+small */
329 if (round+small != round) {
331 while (*d > 999999999) {
336 for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
341 for (; z>a && !z[-1]; z--);
352 if (!(fl&ALT_FORM)) {
353 /* Count trailing zeros in last place */
354 if (z>a && z[-1]) for (i=10, j=0; z[-1]%i==0; i*=10, j++);
357 p = MIN(p,MAX(0,9*(z-r-1)-j));
359 p = MIN(p,MAX(0,9*(z-r-1)+e-j));
362 if (p > INT_MAX-1-(p || (fl&ALT_FORM)))
364 l = 1 + p + (p || (fl&ALT_FORM));
366 if (e > INT_MAX-l) return -1;
369 estr=fmt_u(e<0 ? -e : e, ebuf);
370 while(ebuf-estr<2) *--estr='0';
371 *--estr = (e<0 ? '-' : '+');
373 if (ebuf-estr > INT_MAX-l) return -1;
377 if (l > INT_MAX-pl) return -1;
378 pad(f, ' ', w, pl+l, fl);
380 pad(f, '0', w, pl+l, fl^ZERO_PAD);
384 for (d=a; d<=r; d++) {
385 char *s = fmt_u(*d, buf+9);
386 if (d!=a) while (s>buf) *--s='0';
387 else if (s==buf+9) *--s='0';
390 if (p || (fl&ALT_FORM)) out(f, ".", 1);
391 for (; d<z && p>0; d++, p-=9) {
392 char *s = fmt_u(*d, buf+9);
393 while (s>buf) *--s='0';
396 pad(f, '0', p+9, 9, 0);
399 for (d=a; d<z && p>=0; d++) {
400 char *s = fmt_u(*d, buf+9);
401 if (s==buf+9) *--s='0';
402 if (d!=a) while (s>buf) *--s='0';
405 if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
407 out(f, s, MIN(buf+9-s, p));
410 pad(f, '0', p+18, 18, 0);
411 out(f, estr, ebuf-estr);
414 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
419 static int getint(char **s) {
421 for (i=0; isdigit(**s); (*s)++) {
422 if (i > INT_MAX/10U || **s-'0' > INT_MAX-10*i) i = -1;
423 else i = 10*i + (**s-'0');
428 static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
430 char *a, *z, *s=(char *)fmt;
438 char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
445 /* This error is only specified for snprintf, but since it's
446 * unspecified for other forms, do the same. Stop immediately
447 * on overflow; otherwise %n could produce wrong results. */
448 if (l > INT_MAX - cnt) goto overflow;
450 /* Update output count, end loop when fmt is exhausted */
454 /* Handle literal text and %% format specifiers */
455 for (a=s; *s && *s!='%'; s++);
456 for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2);
457 if (z-a > INT_MAX-cnt) goto overflow;
462 if (isdigit(s[1]) && s[2]=='$') {
471 /* Read modifier flags */
472 for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
475 /* Read field width */
477 if (isdigit(s[1]) && s[2]=='$') {
479 nl_type[s[1]-'0'] = INT;
480 w = nl_arg[s[1]-'0'].i;
483 w = f ? va_arg(*ap, int) : 0;
486 if (w<0) fl|=LEFT_ADJ, w=-w;
487 } else if ((w=getint(&s))<0) goto overflow;
490 if (*s=='.' && s[1]=='*') {
491 if (isdigit(s[2]) && s[3]=='$') {
492 nl_type[s[2]-'0'] = INT;
493 p = nl_arg[s[2]-'0'].i;
496 p = f ? va_arg(*ap, int) : 0;
500 } else if (*s=='.') {
509 /* Format specifier state machine */
512 if (OOB(*s)) goto inval;
514 st=states[st]S(*s++);
518 /* Check validity of argument type (nl/normal) */
520 if (argpos>=0) goto inval;
522 if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
523 else if (f) pop_arg(&arg, st, ap);
529 z = buf + sizeof(buf);
534 /* Transform ls,lc -> S,C */
535 if (ps && (t&15)==3) t&=~32;
537 /* - and 0 flags are mutually exclusive */
538 if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
543 case BARE: *(int *)arg.p = cnt; break;
544 case LPRE: *(long *)arg.p = cnt; break;
545 case LLPRE: *(long long *)arg.p = cnt; break;
546 case HPRE: *(unsigned short *)arg.p = cnt; break;
547 case HHPRE: *(unsigned char *)arg.p = cnt; break;
548 case ZTPRE: *(size_t *)arg.p = cnt; break;
549 case JPRE: *(uintmax_t *)arg.p = cnt; break;
553 p = MAX(p, 2*sizeof(void*));
557 a = fmt_x(arg.i, z, t&32);
558 if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2;
562 if ((fl&ALT_FORM) && p<z-a+1) p=z-a+1;
566 if (arg.i>INTMAX_MAX) {
568 } else if (fl & MARK_POS) {
570 } else if (fl & PAD_POS) {
576 if (xp && p<0) goto overflow;
577 if (xp) fl &= ~ZERO_PAD;
582 p = MAX(p, z-a + !arg.i);
589 if (1) a = strerror(errno); else
591 a = arg.p ? arg.p : "(null)";
592 z = a + strnlen(a, p<0 ? INT_MAX : p);
593 if (p<0 && *z) goto overflow;
604 for (i=l=0; i<p && *ws && (l=wctomb(mb, *ws++))>=0 && l<=p-i; i+=l);
606 if (i > INT_MAX) goto overflow;
608 pad(f, ' ', w, p, fl);
610 for (i=0; i<0U+p && *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
612 pad(f, ' ', w, p, fl^LEFT_ADJ);
615 case 'e': case 'f': case 'g': case 'a':
616 case 'E': case 'F': case 'G': case 'A':
617 if (xp && p<0) goto overflow;
618 l = fmt_fp(f, arg.f, w, p, fl, t);
619 if (l<0) goto overflow;
623 if (p < z-a) p = z-a;
624 if (p > INT_MAX-pl) goto overflow;
625 if (w < pl+p) w = pl+p;
626 if (w > INT_MAX-cnt) goto overflow;
628 pad(f, ' ', w, pl+p, fl);
630 pad(f, '0', w, pl+p, fl^ZERO_PAD);
631 pad(f, '0', p, z-a, 0);
633 pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
641 for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
642 pop_arg(nl_arg+i, nl_type[i], ap);
643 for (; i<=NL_ARGMAX && !nl_type[i]; i++);
644 if (i<=NL_ARGMAX) goto inval;
655 int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
658 int nl_type[NL_ARGMAX+1] = {0};
659 union arg nl_arg[NL_ARGMAX+1];
660 unsigned char internal_buf[80], *saved_buf = 0;
664 /* the copy allows passing va_list* even if va_list is an array */
666 if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) {
672 olderr = f->flags & F_ERR;
673 if (f->mode < 1) f->flags &= ~F_ERR;
676 f->wpos = f->wbase = f->buf = internal_buf;
677 f->buf_size = sizeof internal_buf;
678 f->wend = internal_buf + sizeof internal_buf;
680 ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
683 if (!f->wpos) ret = -1;
686 f->wpos = f->wbase = f->wend = 0;
688 if (f->flags & F_ERR) ret = -1;