1 #include "stdio_impl.h"
14 /* Some useful macros */
16 #define MAX(a,b) ((a)>(b) ? (a) : (b))
17 #define MIN(a,b) ((a)<(b) ? (a) : (b))
19 /* Convenient bit representation for modifier flags, which all fall
20 * within 31 codepoints of the space character. */
22 #define ALT_FORM (1U<<'#'-' ')
23 #define ZERO_PAD (1U<<'0'-' ')
24 #define LEFT_ADJ (1U<<'-'-' ')
25 #define PAD_POS (1U<<' '-' ')
26 #define MARK_POS (1U<<'+'-' ')
27 #define GROUPED (1U<<'\''-' ')
29 #define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
31 /* State machine to accept length modifiers + conversion specifiers.
32 * Result is 0 on failure, or an argument type to pop on success. */
35 BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE,
38 PTR, INT, UINT, ULLONG,
40 SHORT, USHORT, CHAR, UCHAR,
41 LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
47 #define S(x) [(x)-'A']
49 static const unsigned char states[]['z'-'A'+1] = {
51 S('d') = INT, S('i') = INT,
52 S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
53 S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
54 S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
55 S('c') = CHAR, S('C') = INT,
56 S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
58 S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
59 S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE,
60 }, { /* 1: l-prefixed */
61 S('d') = LONG, S('i') = LONG,
62 S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
63 S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
64 S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
65 S('c') = INT, S('s') = PTR, S('n') = PTR,
67 }, { /* 2: ll-prefixed */
68 S('d') = LLONG, S('i') = LLONG,
69 S('o') = ULLONG, S('u') = ULLONG,
70 S('x') = ULLONG, S('X') = ULLONG,
72 }, { /* 3: h-prefixed */
73 S('d') = SHORT, S('i') = SHORT,
74 S('o') = USHORT, S('u') = USHORT,
75 S('x') = USHORT, S('X') = USHORT,
78 }, { /* 4: hh-prefixed */
79 S('d') = CHAR, S('i') = CHAR,
80 S('o') = UCHAR, S('u') = UCHAR,
81 S('x') = UCHAR, S('X') = UCHAR,
83 }, { /* 5: L-prefixed */
84 S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL,
85 S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL,
87 }, { /* 6: z- or t-prefixed (assumed to be same size) */
88 S('d') = PDIFF, S('i') = PDIFF,
89 S('o') = SIZET, S('u') = SIZET,
90 S('x') = SIZET, S('X') = SIZET,
92 }, { /* 7: j-prefixed */
93 S('d') = IMAX, S('i') = IMAX,
94 S('o') = UMAX, S('u') = UMAX,
95 S('x') = UMAX, S('X') = UMAX,
100 #define OOB(x) ((unsigned)(x)-'A' > 'z'-'A')
109 static void pop_arg(union arg *arg, int type, va_list *ap)
112 case PTR: arg->p = va_arg(*ap, void *);
113 break; case INT: arg->i = va_arg(*ap, int);
114 break; case UINT: arg->i = va_arg(*ap, unsigned int);
115 break; case LONG: arg->i = va_arg(*ap, long);
116 break; case ULONG: arg->i = va_arg(*ap, unsigned long);
117 break; case ULLONG: arg->i = va_arg(*ap, unsigned long long);
118 break; case SHORT: arg->i = (short)va_arg(*ap, int);
119 break; case USHORT: arg->i = (unsigned short)va_arg(*ap, int);
120 break; case CHAR: arg->i = (signed char)va_arg(*ap, int);
121 break; case UCHAR: arg->i = (unsigned char)va_arg(*ap, int);
122 break; case LLONG: arg->i = va_arg(*ap, long long);
123 break; case SIZET: arg->i = va_arg(*ap, size_t);
124 break; case IMAX: arg->i = va_arg(*ap, intmax_t);
125 break; case UMAX: arg->i = va_arg(*ap, uintmax_t);
126 break; case PDIFF: arg->i = va_arg(*ap, ptrdiff_t);
127 break; case UIPTR: arg->i = (uintptr_t)va_arg(*ap, void *);
128 break; case DBL: arg->f = va_arg(*ap, double);
129 break; case LDBL: arg->f = va_arg(*ap, long double);
133 static void out(FILE *f, const char *s, size_t l)
135 if (!(f->flags & F_ERR)) __fwritex((void *)s, l, f);
138 static void pad(FILE *f, char c, int w, int l, int fl)
141 if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return;
143 memset(pad, c, l>sizeof pad ? sizeof pad : l);
144 for (; l >= sizeof pad; l -= sizeof pad)
145 out(f, pad, sizeof pad);
149 static const char xdigits[16] = {
153 static char *fmt_x(uintmax_t x, char *s, int lower)
155 for (; x; x>>=4) *--s = xdigits[(x&15)]|lower;
159 static char *fmt_o(uintmax_t x, char *s)
161 for (; x; x>>=3) *--s = '0' + (x&7);
165 static char *fmt_u(uintmax_t x, char *s)
168 for ( ; x>ULONG_MAX; x/=10) *--s = '0' + x%10;
169 for (y=x; y; y/=10) *--s = '0' + y%10;
173 /* Do not override this check. The floating point printing code below
174 * depends on the float.h constants being right. If they are wrong, it
175 * may overflow the stack. */
176 #if LDBL_MANT_DIG == 53
177 typedef char compiler_defines_long_double_incorrectly[9-(int)sizeof(long double)];
180 static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
182 uint32_t big[(LDBL_MANT_DIG+28)/29 + 1 // mantissa expansion
183 + (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9]; // exponent expansion
184 uint32_t *a, *d, *r, *z;
185 int e2=0, e, i, j, l;
186 char buf[9+LDBL_MANT_DIG/4], *s;
187 const char *prefix="-0X+0X 0X-0x+0x 0x";
189 char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr;
194 } else if (fl & MARK_POS) {
196 } else if (fl & PAD_POS) {
198 } else prefix++, pl=0;
201 char *s = (t&32)?"inf":"INF";
202 if (y!=y) s=(t&32)?"nan":"NAN";
203 pad(f, ' ', w, 3+pl, fl&~ZERO_PAD);
206 pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);
210 y = frexpl(y, &e2) * 2;
214 long double round = 8.0;
217 if (t&32) prefix += 9;
220 if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
221 else re=LDBL_MANT_DIG/4-1-p;
224 round *= 1<<(LDBL_MANT_DIG%4);
225 while (re--) round*=16;
237 estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
238 if (estr==ebuf) *--estr='0';
239 *--estr = (e2<0 ? '-' : '+');
240 *--estr = t+('p'-'a');
245 *s++=xdigits[x]|(t&32);
247 if (s-buf==1 && (y||p>0||(fl&ALT_FORM))) *s++='.';
250 if (p > INT_MAX-2-(ebuf-estr)-pl)
252 if (p && s-buf-2 < p)
253 l = (p+2) + (ebuf-estr);
255 l = (s-buf) + (ebuf-estr);
257 pad(f, ' ', w, pl+l, fl);
259 pad(f, '0', w, pl+l, fl^ZERO_PAD);
261 pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
262 out(f, estr, ebuf-estr);
263 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
268 if (y) y *= 0x1p28, e2-=28;
271 else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
275 y = 1000000000*(y-*z++);
281 for (d=z-1; d>=a; d--) {
282 uint64_t x = ((uint64_t)*d<<sh)+carry;
284 carry = x / 1000000000;
286 if (carry) *--a = carry;
287 while (z>a && !z[-1]) z--;
291 uint32_t carry=0, *b;
292 int sh=MIN(9,-e2), need=1+(p+LDBL_MANT_DIG/3U+8)/9;
293 for (d=a; d<z; d++) {
294 uint32_t rm = *d & (1<<sh)-1;
295 *d = (*d>>sh) + carry;
296 carry = (1000000000>>sh) * rm;
299 if (carry) *z++ = carry;
300 /* Avoid (slow!) computation past requested precision */
301 b = (t|32)=='f' ? r : a;
302 if (z-b > need) z = b+need;
306 if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
309 /* Perform rounding: j is precision after the radix (possibly neg) */
310 j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
313 /* We avoid C's broken division of negative numbers */
314 d = r + 1 + ((j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP);
317 for (i=10, j++; j<9; i*=10, j++);
319 /* Are there any significant digits past j? */
321 long double round = 2/LDBL_EPSILON;
323 if ((*d/i & 1) || (i==1000000000 && d>a && (d[-1]&1)))
325 if (x<i/2) small=0x0.8p0;
326 else if (x==i/2 && d+1==z) small=0x1.0p0;
328 if (pl && *prefix=='-') round*=-1, small*=-1;
330 /* Decide whether to round by probing round+small */
331 if (round+small != round) {
333 while (*d > 999999999) {
338 for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
343 for (; z>a && !z[-1]; z--);
354 if (!(fl&ALT_FORM)) {
355 /* Count trailing zeros in last place */
356 if (z>a && z[-1]) for (i=10, j=0; z[-1]%i==0; i*=10, j++);
359 p = MIN(p,MAX(0,9*(z-r-1)-j));
361 p = MIN(p,MAX(0,9*(z-r-1)+e-j));
364 if (p > INT_MAX-1-(p || (fl&ALT_FORM)))
366 l = 1 + p + (p || (fl&ALT_FORM));
368 if (e > INT_MAX-l) return -1;
371 estr=fmt_u(e<0 ? -e : e, ebuf);
372 while(ebuf-estr<2) *--estr='0';
373 *--estr = (e<0 ? '-' : '+');
375 if (ebuf-estr > INT_MAX-l) return -1;
379 if (l > INT_MAX-pl) return -1;
380 pad(f, ' ', w, pl+l, fl);
382 pad(f, '0', w, pl+l, fl^ZERO_PAD);
386 for (d=a; d<=r; d++) {
387 char *s = fmt_u(*d, buf+9);
388 if (d!=a) while (s>buf) *--s='0';
389 else if (s==buf+9) *--s='0';
392 if (p || (fl&ALT_FORM)) out(f, ".", 1);
393 for (; d<z && p>0; d++, p-=9) {
394 char *s = fmt_u(*d, buf+9);
395 while (s>buf) *--s='0';
398 pad(f, '0', p+9, 9, 0);
401 for (d=a; d<z && p>=0; d++) {
402 char *s = fmt_u(*d, buf+9);
403 if (s==buf+9) *--s='0';
404 if (d!=a) while (s>buf) *--s='0';
407 if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
409 out(f, s, MIN(buf+9-s, p));
412 pad(f, '0', p+18, 18, 0);
413 out(f, estr, ebuf-estr);
416 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
421 static int getint(char **s) {
423 for (i=0; isdigit(**s); (*s)++) {
424 if (i > INT_MAX/10U || **s-'0' > INT_MAX-10*i) i = -1;
425 else i = 10*i + (**s-'0');
430 static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
432 char *a, *z, *s=(char *)fmt;
440 char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
447 /* This error is only specified for snprintf, but since it's
448 * unspecified for other forms, do the same. Stop immediately
449 * on overflow; otherwise %n could produce wrong results. */
450 if (l > INT_MAX - cnt) goto overflow;
452 /* Update output count, end loop when fmt is exhausted */
456 /* Handle literal text and %% format specifiers */
457 for (a=s; *s && *s!='%'; s++);
458 for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2);
459 if (z-a > INT_MAX-cnt) goto overflow;
464 if (isdigit(s[1]) && s[2]=='$') {
473 /* Read modifier flags */
474 for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
477 /* Read field width */
479 if (isdigit(s[1]) && s[2]=='$') {
481 nl_type[s[1]-'0'] = INT;
482 w = nl_arg[s[1]-'0'].i;
485 w = f ? va_arg(*ap, int) : 0;
488 if (w<0) fl|=LEFT_ADJ, w=-w;
489 } else if ((w=getint(&s))<0) goto overflow;
492 if (*s=='.' && s[1]=='*') {
493 if (isdigit(s[2]) && s[3]=='$') {
494 nl_type[s[2]-'0'] = INT;
495 p = nl_arg[s[2]-'0'].i;
498 p = f ? va_arg(*ap, int) : 0;
502 } else if (*s=='.') {
511 /* Format specifier state machine */
514 if (OOB(*s)) goto inval;
516 st=states[st]S(*s++);
520 /* Check validity of argument type (nl/normal) */
522 if (argpos>=0) goto inval;
524 if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
525 else if (f) pop_arg(&arg, st, ap);
531 z = buf + sizeof(buf);
536 /* Transform ls,lc -> S,C */
537 if (ps && (t&15)==3) t&=~32;
539 /* - and 0 flags are mutually exclusive */
540 if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
545 case BARE: *(int *)arg.p = cnt; break;
546 case LPRE: *(long *)arg.p = cnt; break;
547 case LLPRE: *(long long *)arg.p = cnt; break;
548 case HPRE: *(unsigned short *)arg.p = cnt; break;
549 case HHPRE: *(unsigned char *)arg.p = cnt; break;
550 case ZTPRE: *(size_t *)arg.p = cnt; break;
551 case JPRE: *(uintmax_t *)arg.p = cnt; break;
555 p = MAX(p, 2*sizeof(void*));
559 a = fmt_x(arg.i, z, t&32);
560 if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2;
564 if ((fl&ALT_FORM) && p<z-a+1) p=z-a+1;
568 if (arg.i>INTMAX_MAX) {
570 } else if (fl & MARK_POS) {
572 } else if (fl & PAD_POS) {
578 if (xp && p<0) goto overflow;
579 if (xp) fl &= ~ZERO_PAD;
584 p = MAX(p, z-a + !arg.i);
591 if (1) a = strerror(errno); else
593 a = arg.p ? arg.p : "(null)";
594 z = a + strnlen(a, p<0 ? INT_MAX : p);
595 if (p<0 && *z) goto overflow;
606 for (i=l=0; i<p && *ws && (l=wctomb(mb, *ws++))>=0 && l<=p-i; i+=l);
608 if (i > INT_MAX) goto overflow;
610 pad(f, ' ', w, p, fl);
612 for (i=0; i<0U+p && *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
614 pad(f, ' ', w, p, fl^LEFT_ADJ);
617 case 'e': case 'f': case 'g': case 'a':
618 case 'E': case 'F': case 'G': case 'A':
619 if (xp && p<0) goto overflow;
620 l = fmt_fp(f, arg.f, w, p, fl, t);
621 if (l<0) goto overflow;
625 if (p < z-a) p = z-a;
626 if (p > INT_MAX-pl) goto overflow;
627 if (w < pl+p) w = pl+p;
628 if (w > INT_MAX-cnt) goto overflow;
630 pad(f, ' ', w, pl+p, fl);
632 pad(f, '0', w, pl+p, fl^ZERO_PAD);
633 pad(f, '0', p, z-a, 0);
635 pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
643 for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
644 pop_arg(nl_arg+i, nl_type[i], ap);
645 for (; i<=NL_ARGMAX && !nl_type[i]; i++);
646 if (i<=NL_ARGMAX) goto inval;
657 int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
660 int nl_type[NL_ARGMAX+1] = {0};
661 union arg nl_arg[NL_ARGMAX+1];
662 unsigned char internal_buf[80], *saved_buf = 0;
666 /* the copy allows passing va_list* even if va_list is an array */
668 if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) {
674 olderr = f->flags & F_ERR;
675 if (f->mode < 1) f->flags &= ~F_ERR;
678 f->buf = internal_buf;
679 f->buf_size = sizeof internal_buf;
680 f->wpos = f->wbase = f->wend = 0;
682 if (!f->wend && __towrite(f)) ret = -1;
683 else ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
686 if (!f->wpos) ret = -1;
689 f->wpos = f->wbase = f->wend = 0;
691 if (f->flags & F_ERR) ret = -1;