| 1 | #include "stdio_impl.h" |
| 2 | #include <errno.h> |
| 3 | #include <ctype.h> |
| 4 | #include <limits.h> |
| 5 | #include <string.h> |
| 6 | #include <stdarg.h> |
| 7 | #include <stddef.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <wchar.h> |
| 10 | #include <inttypes.h> |
| 11 | #ifdef __wasilibc_unmodified_upstream // Changes to optimize printf/scanf when long double isn't needed |
| 12 | #else |
| 13 | #include "printscan.h" |
| 14 | #endif |
| 15 | |
| 16 | /* Convenient bit representation for modifier flags, which all fall |
| 17 | * within 31 codepoints of the space character. */ |
| 18 | |
| 19 | #define ALT_FORM (1U<<'#'-' ') |
| 20 | #define ZERO_PAD (1U<<'0'-' ') |
| 21 | #define LEFT_ADJ (1U<<'-'-' ') |
| 22 | #define PAD_POS (1U<<' '-' ') |
| 23 | #define MARK_POS (1U<<'+'-' ') |
| 24 | #define GROUPED (1U<<'\''-' ') |
| 25 | |
| 26 | #define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED) |
| 27 | |
| 28 | /* State machine to accept length modifiers + conversion specifiers. |
| 29 | * Result is 0 on failure, or an argument type to pop on success. */ |
| 30 | |
| 31 | enum { |
| 32 | 	BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE, |
| 33 | 	ZTPRE, JPRE, |
| 34 | 	STOP, |
| 35 | 	PTR, INT, UINT, ULLONG, |
| 36 | 	LONG, ULONG, |
| 37 | 	SHORT, USHORT, CHAR, UCHAR, |
| 38 | 	LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR, |
| 39 | 	DBL, LDBL, |
| 40 | 	NOARG, |
| 41 | 	MAXSTATE |
| 42 | }; |
| 43 | |
| 44 | #define S(x) [(x)-'A'] |
| 45 | |
| 46 | static const unsigned char states[]['z'-'A'+1] = { |
| 47 | 	{ /* 0: bare types */ |
| 48 | 		S('d') = INT, S('i') = INT, |
| 49 | 		S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT, |
| 50 | 		S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL, |
| 51 | 		S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL, |
| 52 | 		S('c') = INT, S('C') = UINT, |
| 53 | 		S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR, |
| 54 | 		S('m') = NOARG, |
| 55 | 		S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE, |
| 56 | 		S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE, |
| 57 | 	}, { /* 1: l-prefixed */ |
| 58 | 		S('d') = LONG, S('i') = LONG, |
| 59 | 		S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG, |
| 60 | 		S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL, |
| 61 | 		S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL, |
| 62 | 		S('c') = UINT, S('s') = PTR, S('n') = PTR, |
| 63 | 		S('l') = LLPRE, |
| 64 | 	}, { /* 2: ll-prefixed */ |
| 65 | 		S('d') = LLONG, S('i') = LLONG, |
| 66 | 		S('o') = ULLONG, S('u') = ULLONG, |
| 67 | 		S('x') = ULLONG, S('X') = ULLONG, |
| 68 | 		S('n') = PTR, |
| 69 | 	}, { /* 3: h-prefixed */ |
| 70 | 		S('d') = SHORT, S('i') = SHORT, |
| 71 | 		S('o') = USHORT, S('u') = USHORT, |
| 72 | 		S('x') = USHORT, S('X') = USHORT, |
| 73 | 		S('n') = PTR, |
| 74 | 		S('h') = HHPRE, |
| 75 | 	}, { /* 4: hh-prefixed */ |
| 76 | 		S('d') = CHAR, S('i') = CHAR, |
| 77 | 		S('o') = UCHAR, S('u') = UCHAR, |
| 78 | 		S('x') = UCHAR, S('X') = UCHAR, |
| 79 | 		S('n') = PTR, |
| 80 | 	}, { /* 5: L-prefixed */ |
| 81 | 		S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL, |
| 82 | 		S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL, |
| 83 | 		S('n') = PTR, |
| 84 | 	}, { /* 6: z- or t-prefixed (assumed to be same size) */ |
| 85 | 		S('d') = PDIFF, S('i') = PDIFF, |
| 86 | 		S('o') = SIZET, S('u') = SIZET, |
| 87 | 		S('x') = SIZET, S('X') = SIZET, |
| 88 | 		S('n') = PTR, |
| 89 | 	}, { /* 7: j-prefixed */ |
| 90 | 		S('d') = IMAX, S('i') = IMAX, |
| 91 | 		S('o') = UMAX, S('u') = UMAX, |
| 92 | 		S('x') = UMAX, S('X') = UMAX, |
| 93 | 		S('n') = PTR, |
| 94 | 	} |
| 95 | }; |
| 96 | |
| 97 | #define OOB(x) ((unsigned)(x)-'A' > 'z'-'A') |
| 98 | |
| 99 | union arg |
| 100 | { |
| 101 | 	uintmax_t i; |
| 102 | #if !defined(__wasilibc_printscan_no_floating_point) |
| 103 | #if defined(__wasilibc_printscan_no_long_double) |
| 104 | 	long_double f; |
| 105 | #else |
| 106 | 	long double f; |
| 107 | #endif |
| 108 | #endif |
| 109 | 	void *p; |
| 110 | }; |
| 111 | |
| 112 | static void pop_arg(union arg *arg, int type, va_list *ap) |
| 113 | { |
| 114 | 	switch (type) { |
| 115 | 	 case PTR:	arg->p = va_arg(*ap, void *); |
| 116 | 	break; case INT:	arg->i = va_arg(*ap, int); |
| 117 | 	break; case UINT:	arg->i = va_arg(*ap, unsigned int); |
| 118 | 	break; case LONG:	arg->i = va_arg(*ap, long); |
| 119 | 	break; case ULONG:	arg->i = va_arg(*ap, unsigned long); |
| 120 | 	break; case ULLONG:	arg->i = va_arg(*ap, unsigned long long); |
| 121 | 	break; case SHORT:	arg->i = (short)va_arg(*ap, int); |
| 122 | 	break; case USHORT:	arg->i = (unsigned short)va_arg(*ap, int); |
| 123 | 	break; case CHAR:	arg->i = (signed char)va_arg(*ap, int); |
| 124 | 	break; case UCHAR:	arg->i = (unsigned char)va_arg(*ap, int); |
| 125 | 	break; case LLONG:	arg->i = va_arg(*ap, long long); |
| 126 | 	break; case SIZET:	arg->i = va_arg(*ap, size_t); |
| 127 | 	break; case IMAX:	arg->i = va_arg(*ap, intmax_t); |
| 128 | 	break; case UMAX:	arg->i = va_arg(*ap, uintmax_t); |
| 129 | 	break; case PDIFF:	arg->i = va_arg(*ap, ptrdiff_t); |
| 130 | 	break; case UIPTR:	arg->i = (uintptr_t)va_arg(*ap, void *); |
| 131 | #if defined(__wasilibc_printscan_no_floating_point) |
| 132 | 	break; case DBL: |
| 133 | 	break; case LDBL:	floating_point_not_supported(); |
| 134 | #else |
| 135 | 	break; case DBL:	arg->f = va_arg(*ap, double); |
| 136 | #if defined(__wasilibc_printscan_no_long_double) |
| 137 | 	break; case LDBL:	long_double_not_supported(); |
| 138 | #else |
| 139 | 	break; case LDBL:	arg->f = va_arg(*ap, long double); |
| 140 | #endif |
| 141 | #endif |
| 142 | 	} |
| 143 | } |
| 144 | |
| 145 | static void out(FILE *f, const wchar_t *s, size_t l) |
| 146 | { |
| 147 | 	while (l-- && !ferror(f)) fputwc(*s++, f); |
| 148 | } |
| 149 | |
| 150 | static void pad(FILE *f, int n, int fl) |
| 151 | { |
| 152 | 	if ((fl & LEFT_ADJ) || !n || ferror(f)) return; |
| 153 | 	fprintf(f, "%*s", n, ""); |
| 154 | } |
| 155 | |
| 156 | static int getint(wchar_t **s) { |
| 157 | 	int i; |
| 158 | 	for (i=0; iswdigit(**s); (*s)++) { |
| 159 | 		if (i > INT_MAX/10U || **s-'0' > INT_MAX-10*i) i = -1; |
| 160 | 		else i = 10*i + (**s-'0'); |
| 161 | 	} |
| 162 | 	return i; |
| 163 | } |
| 164 | |
| 165 | static const char sizeprefix['y'-'a'] = { |
| 166 | ['a'-'a']='L', ['e'-'a']='L', ['f'-'a']='L', ['g'-'a']='L', |
| 167 | ['d'-'a']='j', ['i'-'a']='j', ['o'-'a']='j', ['u'-'a']='j', ['x'-'a']='j', |
| 168 | ['p'-'a']='j' |
| 169 | }; |
| 170 | |
| 171 | static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_arg, int *nl_type) |
| 172 | { |
| 173 | 	wchar_t *a, *z, *s=(wchar_t *)fmt; |
| 174 | 	unsigned l10n=0, fl; |
| 175 | 	int w, p, xp; |
| 176 | 	union arg arg; |
| 177 | 	int argpos; |
| 178 | 	unsigned st, ps; |
| 179 | 	int cnt=0, l=0; |
| 180 | 	int i; |
| 181 | 	int t; |
| 182 | 	char *bs; |
| 183 | 	char charfmt[16]; |
| 184 | 	wchar_t wc; |
| 185 | |
| 186 | 	for (;;) { |
| 187 | 		/* This error is only specified for snprintf, but since it's |
| 188 | 		 * unspecified for other forms, do the same. Stop immediately |
| 189 | 		 * on overflow; otherwise %n could produce wrong results. */ |
| 190 | 		if (l > INT_MAX - cnt) goto overflow; |
| 191 | |
| 192 | 		/* Update output count, end loop when fmt is exhausted */ |
| 193 | 		cnt += l; |
| 194 | 		if (!*s) break; |
| 195 | |
| 196 | 		/* Handle literal text and %% format specifiers */ |
| 197 | 		for (a=s; *s && *s!='%'; s++); |
| 198 | 		for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2); |
| 199 | 		if (z-a > INT_MAX-cnt) goto overflow; |
| 200 | 		l = z-a; |
| 201 | 		if (f) out(f, a, l); |
| 202 | 		if (l) continue; |
| 203 | |
| 204 | 		if (iswdigit(s[1]) && s[2]=='$') { |
| 205 | 			l10n=1; |
| 206 | 			argpos = s[1]-'0'; |
| 207 | 			s+=3; |
| 208 | 		} else { |
| 209 | 			argpos = -1; |
| 210 | 			s++; |
| 211 | 		} |
| 212 | |
| 213 | 		/* Read modifier flags */ |
| 214 | 		for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++) |
| 215 | 			fl |= 1U<<*s-' '; |
| 216 | |
| 217 | 		/* Read field width */ |
| 218 | 		if (*s=='*') { |
| 219 | 			if (iswdigit(s[1]) && s[2]=='$') { |
| 220 | 				l10n=1; |
| 221 | 				nl_type[s[1]-'0'] = INT; |
| 222 | 				w = nl_arg[s[1]-'0'].i; |
| 223 | 				s+=3; |
| 224 | 			} else if (!l10n) { |
| 225 | 				w = f ? va_arg(*ap, int) : 0; |
| 226 | 				s++; |
| 227 | 			} else goto inval; |
| 228 | 			if (w<0) fl|=LEFT_ADJ, w=-w; |
| 229 | 		} else if ((w=getint(&s))<0) goto overflow; |
| 230 | |
| 231 | 		/* Read precision */ |
| 232 | 		if (*s=='.' && s[1]=='*') { |
| 233 | 			if (isdigit(s[2]) && s[3]=='$') { |
| 234 | 				nl_type[s[2]-'0'] = INT; |
| 235 | 				p = nl_arg[s[2]-'0'].i; |
| 236 | 				s+=4; |
| 237 | 			} else if (!l10n) { |
| 238 | 				p = f ? va_arg(*ap, int) : 0; |
| 239 | 				s+=2; |
| 240 | 			} else goto inval; |
| 241 | 			xp = (p>=0); |
| 242 | 		} else if (*s=='.') { |
| 243 | 			s++; |
| 244 | 			p = getint(&s); |
| 245 | 			xp = 1; |
| 246 | 		} else { |
| 247 | 			p = -1; |
| 248 | 			xp = 0; |
| 249 | 		} |
| 250 | |
| 251 | 		/* Format specifier state machine */ |
| 252 | 		st=0; |
| 253 | 		do { |
| 254 | 			if (OOB(*s)) goto inval; |
| 255 | 			ps=st; |
| 256 | 			st=states[st]S(*s++); |
| 257 | 		} while (st-1<STOP); |
| 258 | 		if (!st) goto inval; |
| 259 | |
| 260 | 		/* Check validity of argument type (nl/normal) */ |
| 261 | 		if (st==NOARG) { |
| 262 | 			if (argpos>=0) goto inval; |
| 263 | 		} else { |
| 264 | 			if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos]; |
| 265 | 			else if (f) pop_arg(&arg, st, ap); |
| 266 | 			else return 0; |
| 267 | 		} |
| 268 | |
| 269 | 		if (!f) continue; |
| 270 | |
| 271 | 		/* Do not process any new directives once in error state. */ |
| 272 | 		if (ferror(f)) return -1; |
| 273 | |
| 274 | 		t = s[-1]; |
| 275 | 		if (ps && (t&15)==3) t&=~32; |
| 276 | |
| 277 | 		switch (t) { |
| 278 | 		case 'n': |
| 279 | 			switch(ps) { |
| 280 | 			case BARE: *(int *)arg.p = cnt; break; |
| 281 | 			case LPRE: *(long *)arg.p = cnt; break; |
| 282 | 			case LLPRE: *(long long *)arg.p = cnt; break; |
| 283 | 			case HPRE: *(unsigned short *)arg.p = cnt; break; |
| 284 | 			case HHPRE: *(unsigned char *)arg.p = cnt; break; |
| 285 | 			case ZTPRE: *(size_t *)arg.p = cnt; break; |
| 286 | 			case JPRE: *(uintmax_t *)arg.p = cnt; break; |
| 287 | 			} |
| 288 | 			continue; |
| 289 | 		case 'c': |
| 290 | 		case 'C': |
| 291 | 			if (w<1) w=1; |
| 292 | 			pad(f, w-1, fl); |
| 293 | 			out(f, &(wchar_t){t=='C' ? arg.i : btowc(arg.i)}, 1); |
| 294 | 			pad(f, w-1, fl^LEFT_ADJ); |
| 295 | 			l = w; |
| 296 | 			continue; |
| 297 | 		case 'S': |
| 298 | 			a = arg.p; |
| 299 | 			z = a + wcsnlen(a, p<0 ? INT_MAX : p); |
| 300 | 			if (p<0 && *z) goto overflow; |
| 301 | 			p = z-a; |
| 302 | 			if (w<p) w=p; |
| 303 | 			pad(f, w-p, fl); |
| 304 | 			out(f, a, p); |
| 305 | 			pad(f, w-p, fl^LEFT_ADJ); |
| 306 | 			l=w; |
| 307 | 			continue; |
| 308 | 		case 'm': |
| 309 | 			arg.p = strerror(errno); |
| 310 | 		case 's': |
| 311 | 			if (!arg.p) arg.p = "(null)"; |
| 312 | 			bs = arg.p; |
| 313 | 			for (i=l=0; l<(p<0?INT_MAX:p) && (i=mbtowc(&wc, bs, MB_LEN_MAX))>0; bs+=i, l++); |
| 314 | 			if (i<0) return -1; |
| 315 | 			if (p<0 && *bs) goto overflow; |
| 316 | 			p=l; |
| 317 | 			if (w<p) w=p; |
| 318 | 			pad(f, w-p, fl); |
| 319 | 			bs = arg.p; |
| 320 | 			while (l--) { |
| 321 | 				i=mbtowc(&wc, bs, MB_LEN_MAX); |
| 322 | 				bs+=i; |
| 323 | 				out(f, &wc, 1); |
| 324 | 			} |
| 325 | 			pad(f, w-p, fl^LEFT_ADJ); |
| 326 | 			l=w; |
| 327 | 			continue; |
| 328 | 		} |
| 329 | |
| 330 | 		if (xp && p<0) goto overflow; |
| 331 | #if defined(__wasilibc_printscan_no_long_double) |
| 332 | 		// Omit the 'L' modifier for floating-point cases. |
| 333 | 		switch (t|32) { |
| 334 | 		case 'a': case 'e': case 'f': case 'g': |
| 335 | 			snprintf(charfmt, sizeof charfmt, "%%%s%s%s%s%s*.*%c", |
| 336 | 				"#"+!(fl & ALT_FORM), |
| 337 | 				"+"+!(fl & MARK_POS), |
| 338 | 				"-"+!(fl & LEFT_ADJ), |
| 339 | 				" "+!(fl & PAD_POS), |
| 340 | 				"0"+!(fl & ZERO_PAD), |
| 341 | 				t); |
| 342 | |
| 343 | 			l = fprintf(f, charfmt, w, p, arg.f); |
| 344 | 			break; |
| 345 | 		case 'd': case 'i': case 'o': case 'u': case 'x': case 'p': |
| 346 | 			snprintf(charfmt, sizeof charfmt, "%%%s%s%s%s%s*.*%c%c", |
| 347 | 				"#"+!(fl & ALT_FORM), |
| 348 | 				"+"+!(fl & MARK_POS), |
| 349 | 				"-"+!(fl & LEFT_ADJ), |
| 350 | 				" "+!(fl & PAD_POS), |
| 351 | 				"0"+!(fl & ZERO_PAD), |
| 352 | 				sizeprefix[(t|32)-'a'], t); |
| 353 | |
| 354 | 			l = fprintf(f, charfmt, w, p, arg.i); |
| 355 | 			break; |
| 356 | 		} |
| 357 | #else |
| 358 | 		snprintf(charfmt, sizeof charfmt, "%%%s%s%s%s%s*.*%c%c", |
| 359 | 			"#"+!(fl & ALT_FORM), |
| 360 | 			"+"+!(fl & MARK_POS), |
| 361 | 			"-"+!(fl & LEFT_ADJ), |
| 362 | 			" "+!(fl & PAD_POS), |
| 363 | 			"0"+!(fl & ZERO_PAD), |
| 364 | 			sizeprefix[(t|32)-'a'], t); |
| 365 | |
| 366 | 		switch (t|32) { |
| 367 | #if !defined(__wasilibc_printscan_no_floating_point) |
| 368 | 		case 'a': case 'e': case 'f': case 'g': |
| 369 | 			l = fprintf(f, charfmt, w, p, arg.f); |
| 370 | 			break; |
| 371 | #endif |
| 372 | 		case 'd': case 'i': case 'o': case 'u': case 'x': case 'p': |
| 373 | 			l = fprintf(f, charfmt, w, p, arg.i); |
| 374 | 			break; |
| 375 | 		} |
| 376 | #endif |
| 377 | 	} |
| 378 | |
| 379 | 	if (f) return cnt; |
| 380 | 	if (!l10n) return 0; |
| 381 | |
| 382 | 	for (i=1; i<=NL_ARGMAX && nl_type[i]; i++) |
| 383 | 		pop_arg(nl_arg+i, nl_type[i], ap); |
| 384 | 	for (; i<=NL_ARGMAX && !nl_type[i]; i++); |
| 385 | 	if (i<=NL_ARGMAX) return -1; |
| 386 | 	return 1; |
| 387 | |
| 388 | inval: |
| 389 | 	errno = EINVAL; |
| 390 | 	return -1; |
| 391 | overflow: |
| 392 | 	errno = EOVERFLOW; |
| 393 | 	return -1; |
| 394 | } |
| 395 | |
| 396 | int vfwprintf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap) |
| 397 | { |
| 398 | 	va_list ap2; |
| 399 | 	int nl_type[NL_ARGMAX+1] = {0}; |
| 400 | 	union arg nl_arg[NL_ARGMAX+1]; |
| 401 | 	int olderr; |
| 402 | 	int ret; |
| 403 | |
| 404 | 	/* the copy allows passing va_list* even if va_list is an array */ |
| 405 | 	va_copy(ap2, ap); |
| 406 | 	if (wprintf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) { |
| 407 | 		va_end(ap2); |
| 408 | 		return -1; |
| 409 | 	} |
| 410 | |
| 411 | 	FLOCK(f); |
| 412 | 	fwide(f, 1); |
| 413 | 	olderr = f->flags & F_ERR; |
| 414 | 	f->flags &= ~F_ERR; |
| 415 | 	ret = wprintf_core(f, fmt, &ap2, nl_arg, nl_type); |
| 416 | 	if (ferror(f)) ret = -1; |
| 417 | 	f->flags |= olderr; |
| 418 | 	FUNLOCK(f); |
| 419 | 	va_end(ap2); |
| 420 | 	return ret; |
| 421 | } |