blob: 10303d9a2d53d1e2be256cc26ce88ca990cdc930 [file] [log] [blame]
Elliott Hughes506c6de2016-01-15 16:30:18 -08001/* $OpenBSD: vfprintf.c,v 1.71 2016/01/04 15:47:47 schwarze Exp $ */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -070034#define CHAR_TYPE char
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036#include <sys/mman.h>
Elliott Hughesc8f2c522017-10-31 13:07:51 -070037#include <sys/types.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038
39#include <errno.h>
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -070040#include <float.h>
Elliott Hughes05493712014-04-17 17:30:03 -070041#include <langinfo.h>
42#include <limits.h>
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -070043#include <locale.h>
44#include <math.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045#include <stdarg.h>
46#include <stddef.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047#include <stdint.h>
Elliott Hughesc8f2c522017-10-31 13:07:51 -070048#include <stdio.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049#include <stdlib.h>
50#include <string.h>
Elliott Hughes05493712014-04-17 17:30:03 -070051#include <unistd.h>
52#include <wchar.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054#include "fvwrite.h"
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -070055#include "gdtoa.h"
Elliott Hughesc8f2c522017-10-31 13:07:51 -070056#include "local.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +040058union arg {
Elliott Hughesc8f2c522017-10-31 13:07:51 -070059 int intarg;
60 unsigned int uintarg;
61 long longarg;
62 unsigned long ulongarg;
63 long long longlongarg;
64 unsigned long long ulonglongarg;
65 ptrdiff_t ptrdiffarg;
66 size_t sizearg;
67 ssize_t ssizearg;
68 intmax_t intmaxarg;
69 uintmax_t uintmaxarg;
70 void* pvoidarg;
71 char* pchararg;
72 signed char* pschararg;
73 short* pshortarg;
74 int* pintarg;
75 long* plongarg;
76 long long* plonglongarg;
77 ptrdiff_t* pptrdiffarg;
78 ssize_t* pssizearg;
79 intmax_t* pintmaxarg;
80 double doublearg;
81 long double longdoublearg;
82 wint_t wintarg;
83 wchar_t* pwchararg;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +040084};
85
Elliott Hughes618303c2017-11-02 16:58:44 -070086static int __find_arguments(const CHAR_TYPE* fmt0, va_list ap, union arg** argtable, size_t* argtablesiz);
Elliott Hughesc8f2c522017-10-31 13:07:51 -070087static int __grow_type_table(unsigned char** typetable, int* tablesize);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088
89/*
90 * Flush out all the vectors defined by the given uio,
91 * then reset it so that it can be reused.
92 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070093static int __sprint(FILE* fp, struct __suio* uio) {
94 int err;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095
Elliott Hughesc8f2c522017-10-31 13:07:51 -070096 if (uio->uio_resid == 0) {
97 uio->uio_iovcnt = 0;
98 return (0);
99 }
100 err = __sfvwrite(fp, uio);
101 uio->uio_resid = 0;
102 uio->uio_iovcnt = 0;
103 return (err);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104}
105
106/*
107 * Helper function for `fprintf to unbuffered unix file': creates a
108 * temporary buffer. We only work on write-only files; this avoids
109 * worries about ungetc buffers and so forth.
110 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700111static int __sbprintf(FILE* fp, const char* fmt, va_list ap) {
112 int ret;
113 FILE fake;
114 struct __sfileext fakeext;
115 unsigned char buf[BUFSIZ];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700117 _FILEEXT_SETUP(&fake, &fakeext);
118 /* copy the important variables */
119 fake._flags = fp->_flags & ~__SNBF;
120 fake._file = fp->_file;
121 fake._cookie = fp->_cookie;
122 fake._write = fp->_write;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700124 /* set up the buffer */
125 fake._bf._base = fake._p = buf;
126 fake._bf._size = fake._w = sizeof(buf);
127 fake._lbfsize = 0; /* not actually used, but Just In Case */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700129 /* do the work, then copy any error status */
130 ret = __vfprintf(&fake, fmt, ap);
131 if (ret >= 0 && __sflush(&fake)) ret = EOF;
132 if (fake._flags & __SERR) fp->_flags |= __SERR;
133 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134}
135
Elliott Hughes05493712014-04-17 17:30:03 -0700136/*
137 * Convert a wide character string argument for the %ls format to a multibyte
138 * string representation. If not -1, prec specifies the maximum number of
139 * bytes to output, and also means that we can't assume that the wide char
140 * string is null-terminated.
141 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700142static char* __wcsconv(wchar_t* wcsarg, int prec) {
143 mbstate_t mbs;
144 char buf[MB_LEN_MAX];
145 wchar_t* p;
146 char* convbuf;
147 size_t clen, nbytes;
Elliott Hughes05493712014-04-17 17:30:03 -0700148
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700149 /* Allocate space for the maximum number of bytes we could output. */
150 if (prec < 0) {
151 memset(&mbs, 0, sizeof(mbs));
152 p = wcsarg;
153 nbytes = wcsrtombs(NULL, (const wchar_t**)&p, 0, &mbs);
154 if (nbytes == (size_t)-1) return (NULL);
155 } else {
156 /*
157 * Optimisation: if the output precision is small enough,
158 * just allocate enough memory for the maximum instead of
159 * scanning the string.
160 */
161 if (prec < 128)
162 nbytes = prec;
163 else {
164 nbytes = 0;
165 p = wcsarg;
166 memset(&mbs, 0, sizeof(mbs));
167 for (;;) {
168 clen = wcrtomb(buf, *p++, &mbs);
169 if (clen == 0 || clen == (size_t)-1 || nbytes + clen > (size_t)prec) break;
170 nbytes += clen;
171 }
172 if (clen == (size_t)-1) return (NULL);
173 }
174 }
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700175 if ((convbuf = static_cast<char*>(malloc(nbytes + 1))) == NULL) return NULL;
Elliott Hughes05493712014-04-17 17:30:03 -0700176
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700177 /* Fill the output buffer. */
178 p = wcsarg;
179 memset(&mbs, 0, sizeof(mbs));
180 if ((nbytes = wcsrtombs(convbuf, (const wchar_t**)&p, nbytes, &mbs)) == (size_t)-1) {
181 free(convbuf);
182 return (NULL);
183 }
184 convbuf[nbytes] = '\0';
185 return (convbuf);
Elliott Hughes05493712014-04-17 17:30:03 -0700186}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800187
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700188#define DEFPREC 6
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700190#define to_digit(c) ((c) - '0')
191#define is_digit(c) ((unsigned)to_digit(c) <= 9)
192#define to_char(n) ((CHAR_TYPE)((n) + '0'))
193
194template <typename CharT>
195static int exponent(CharT* p0, int exp, int fmtch) {
196 CharT* p = p0;
197 *p++ = fmtch;
198 if (exp < 0) {
199 exp = -exp;
200 *p++ = '-';
201 } else {
202 *p++ = '+';
203 }
204
205 CharT expbuf[MAXEXPDIG];
206 CharT* t = expbuf + MAXEXPDIG;
207 if (exp > 9) {
208 do {
209 *--t = to_char(exp % 10);
210 } while ((exp /= 10) > 9);
211 *--t = to_char(exp);
212 for (; t < expbuf + MAXEXPDIG; *p++ = *t++) /* nothing */;
213 } else {
214 /*
215 * Exponents for decimal floating point conversions
216 * (%[eEgG]) must be at least two characters long,
217 * whereas exponents for hexadecimal conversions can
218 * be only one character long.
219 */
220 if (fmtch == 'e' || fmtch == 'E') *p++ = '0';
221 *p++ = to_char(exp);
222 }
223 return (p - p0);
224}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225
Elliott Hughes05493712014-04-17 17:30:03 -0700226/*
227 * The size of the buffer we use as scratch space for integer
228 * conversions, among other things. Technically, we would need the
229 * most space for base 10 conversions with thousands' grouping
230 * characters between each pair of digits. 100 bytes is a
231 * conservative overestimate even for a 128-bit uintmax_t.
232 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700233#define BUF 100
Elliott Hughes05493712014-04-17 17:30:03 -0700234
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700235#define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
Elliott Hughes05493712014-04-17 17:30:03 -0700236
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238 * Flags used during conversion.
239 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700240#define ALT 0x0001 /* alternate form */
241#define LADJUST 0x0004 /* left adjustment */
242#define LONGDBL 0x0008 /* long double */
243#define LONGINT 0x0010 /* long integer */
244#define LLONGINT 0x0020 /* long long integer */
245#define SHORTINT 0x0040 /* short integer */
246#define ZEROPAD 0x0080 /* zero (as opposed to blank) pad */
247#define FPT 0x0100 /* Floating point number */
248#define PTRINT 0x0200 /* (unsigned) ptrdiff_t */
249#define SIZEINT 0x0400 /* (signed) size_t */
250#define CHARINT 0x0800 /* 8 bit integer */
251#define MAXINT 0x1000 /* largest integer size (intmax_t) */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800252
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700253int __vfprintf(FILE* fp, const char* fmt0, __va_list ap) {
254 char* fmt; /* format string */
255 int ch; /* character from fmt */
256 int n, n2; /* handy integers (short term usage) */
257 char* cp; /* handy char pointer (short term usage) */
258 struct __siov* iovp; /* for PRINT macro */
259 int flags; /* flags as above */
260 int ret; /* return value accumulator */
261 int width; /* width from format (%8d), or 0 */
262 int prec; /* precision from format; <0 for N/A */
263 char sign; /* sign prefix (' ', '+', '-', or \0) */
264 wchar_t wc;
265 mbstate_t ps;
266 /*
267 * We can decompose the printed representation of floating
268 * point numbers into several parts, some of which may be empty:
269 *
270 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
271 * A B ---C--- D E F
272 *
273 * A: 'sign' holds this value if present; '\0' otherwise
274 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
275 * C: cp points to the string MMMNNN. Leading and trailing
276 * zeros are not in the string and must be added.
277 * D: expchar holds this character; '\0' if no exponent, e.g. %f
278 * F: at least two digits for decimal, at least one digit for hex
279 */
280 char* decimal_point = NULL;
281 int signflag; /* true if float is negative */
282 union { /* floating point arguments %[aAeEfFgG] */
283 double dbl;
284 long double ldbl;
285 } fparg;
286 int expt; /* integer value of exponent */
287 char expchar; /* exponent character: [eEpP\0] */
288 char* dtoaend; /* pointer to end of converted digits */
289 int expsize; /* character count for expstr */
290 int lead; /* sig figs before decimal or group sep */
291 int ndig; /* actual number of digits returned by dtoa */
292 char expstr[MAXEXPDIG + 2]; /* buffer for exponent string: e+ZZZ */
293 char* dtoaresult = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800294
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700295 uintmax_t _umax; /* integer arguments %[diouxX] */
296 enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */
297 int dprec; /* a copy of prec if %[diouxX], 0 otherwise */
298 int realsz; /* field size expanded by dprec */
299 int size; /* size of converted field or string */
300 const char* xdigs; /* digits for %[xX] conversion */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800301#define NIOV 8
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700302 struct __suio uio; /* output information: summary */
303 struct __siov iov[NIOV]; /* ... and individual io vectors */
304 char buf[BUF]; /* buffer with space for digits of uintmax_t */
305 char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
306 union arg* argtable; /* args, built due to positional arg */
307 union arg statargtable[STATIC_ARG_TBL_SIZE];
308 size_t argtablesiz;
309 int nextarg; /* 1-based argument index */
310 va_list orgap; /* original argument pointer */
311 char* convbuf; /* buffer for wide to multi-byte conversion */
Elliott Hughes05493712014-04-17 17:30:03 -0700312
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700313 /*
314 * Choose PADSIZE to trade efficiency vs. size. If larger printf
315 * fields occur frequently, increase PADSIZE and make the initialisers
316 * below longer.
317 */
318#define PADSIZE 16 /* pad chunk size */
319 static char blanks[PADSIZE] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
320 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
321 static char zeroes[PADSIZE] = { '0', '0', '0', '0', '0', '0', '0', '0',
322 '0', '0', '0', '0', '0', '0', '0', '0' };
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800323
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700324 static const char xdigs_lower[] = "0123456789abcdef";
325 static const char xdigs_upper[] = "0123456789ABCDEF";
Elliott Hughes05493712014-04-17 17:30:03 -0700326
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700327 /*
328 * BEWARE, these `goto error' on error, and PAD uses `n'.
329 */
330#define PRINT(ptr, len) \
331 do { \
332 iovp->iov_base = (ptr); \
333 iovp->iov_len = (len); \
334 uio.uio_resid += (len); \
335 iovp++; \
336 if (++uio.uio_iovcnt >= NIOV) { \
337 if (__sprint(fp, &uio)) goto error; \
338 iovp = iov; \
339 } \
340 } while (0)
341#define PAD(howmany, with) \
342 do { \
343 if ((n = (howmany)) > 0) { \
344 while (n > PADSIZE) { \
345 PRINT(with, PADSIZE); \
346 n -= PADSIZE; \
347 } \
348 PRINT(with, n); \
349 } \
350 } while (0)
351#define PRINTANDPAD(p, ep, len, with) \
352 do { \
353 n2 = (ep) - (p); \
354 if (n2 > (len)) n2 = (len); \
355 if (n2 > 0) PRINT((p), n2); \
356 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
357 } while (0)
358#define FLUSH() \
359 do { \
360 if (uio.uio_resid && __sprint(fp, &uio)) goto error; \
361 uio.uio_iovcnt = 0; \
362 iovp = iov; \
363 } while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800364
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700365 /*
366 * To extend shorts properly, we need both signed and unsigned
367 * argument extraction methods.
368 */
369#define SARG() \
370 ((intmax_t)(flags & MAXINT \
371 ? GETARG(intmax_t) \
372 : flags & LLONGINT \
373 ? GETARG(long long) \
374 : flags & LONGINT \
375 ? GETARG(long) \
376 : flags & PTRINT \
377 ? GETARG(ptrdiff_t) \
378 : flags & SIZEINT \
379 ? GETARG(ssize_t) \
380 : flags & SHORTINT \
381 ? (short)GETARG(int) \
382 : flags & CHARINT ? (signed char)GETARG(int) \
383 : GETARG(int)))
384#define UARG() \
385 ((uintmax_t)(flags & MAXINT \
386 ? GETARG(uintmax_t) \
387 : flags & LLONGINT \
388 ? GETARG(unsigned long long) \
389 : flags & LONGINT \
390 ? GETARG(unsigned long) \
391 : flags & PTRINT ? (uintptr_t)GETARG(ptrdiff_t) : /* XXX */ \
392 flags & SIZEINT \
393 ? GETARG(size_t) \
394 : flags & SHORTINT \
395 ? (unsigned short)GETARG(int) \
396 : flags & CHARINT ? (unsigned char)GETARG(int) \
397 : GETARG(unsigned int)))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700399 /*
400 * Append a digit to a value and check for overflow.
401 */
402#define APPEND_DIGIT(val, dig) \
403 do { \
404 if ((val) > INT_MAX / 10) goto overflow; \
405 (val) *= 10; \
406 if ((val) > INT_MAX - to_digit((dig))) goto overflow; \
407 (val) += to_digit((dig)); \
408 } while (0)
Elliott Hughes05493712014-04-17 17:30:03 -0700409
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700410 /*
411 * Get * arguments, including the form *nn$. Preserve the nextarg
412 * that the argument can be gotten once the type is determined.
413 */
414#define GETASTER(val) \
415 n2 = 0; \
416 cp = fmt; \
417 while (is_digit(*cp)) { \
418 APPEND_DIGIT(n2, *cp); \
419 cp++; \
420 } \
421 if (*cp == '$') { \
422 int hold = nextarg; \
423 if (argtable == NULL) { \
424 argtable = statargtable; \
425 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) { \
426 ret = -1; \
427 goto error; \
428 } \
429 } \
430 nextarg = n2; \
431 val = GETARG(int); \
432 nextarg = hold; \
433 fmt = ++cp; \
434 } else { \
435 val = GETARG(int); \
436 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800437
438/*
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700439 * Get the argument indexed by nextarg. If the argument table is
440 * built, use it to get the argument. If its not, get the next
441 * argument (and arguments must be gotten sequentially).
442 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800443#define GETARG(type) \
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700444 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : (nextarg++, va_arg(ap, type)))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800445
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700446 _SET_ORIENTATION(fp, -1);
447 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
448 if (cantwrite(fp)) {
449 errno = EBADF;
450 return (EOF);
451 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800452
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700453 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
454 if ((fp->_flags & (__SNBF | __SWR | __SRW)) == (__SNBF | __SWR) && fp->_file >= 0)
455 return (__sbprintf(fp, fmt0, ap));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800456
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700457 fmt = (char*)fmt0;
458 argtable = NULL;
459 nextarg = 1;
460 va_copy(orgap, ap);
461 uio.uio_iov = iovp = iov;
462 uio.uio_resid = 0;
463 uio.uio_iovcnt = 0;
464 ret = 0;
465 convbuf = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800466
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700467 memset(&ps, 0, sizeof(ps));
468 /*
469 * Scan the format for conversions (`%' character).
470 */
471 for (;;) {
472 cp = fmt;
473 while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
474 fmt += n;
475 if (wc == '%') {
476 fmt--;
477 break;
478 }
479 }
480 if (n < 0) {
481 ret = -1;
482 goto error;
483 }
484 if (fmt != cp) {
485 ptrdiff_t m = fmt - cp;
486 if (m < 0 || m > INT_MAX - ret) goto overflow;
487 PRINT(cp, m);
488 ret += m;
489 }
490 if (n == 0) goto done;
491 fmt++; /* skip over '%' */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800492
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700493 flags = 0;
494 dprec = 0;
495 width = 0;
496 prec = -1;
497 sign = '\0';
498 ox[1] = '\0';
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800499
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700500 rflag:
501 ch = *fmt++;
502 reswitch:
503 switch (ch) {
504 case ' ':
505 /*
506 * ``If the space and + flags both appear, the space
507 * flag will be ignored.''
508 * -- ANSI X3J11
509 */
510 if (!sign) sign = ' ';
511 goto rflag;
512 case '#':
513 flags |= ALT;
514 goto rflag;
515 case '\'':
516 /* grouping not implemented */
517 goto rflag;
518 case '*':
519 /*
520 * ``A negative field width argument is taken as a
521 * - flag followed by a positive field width.''
522 * -- ANSI X3J11
523 * They don't exclude field widths read from args.
524 */
525 GETASTER(width);
526 if (width >= 0) goto rflag;
527 if (width == INT_MIN) goto overflow;
528 width = -width;
529 /* FALLTHROUGH */
530 case '-':
531 flags |= LADJUST;
532 goto rflag;
533 case '+':
534 sign = '+';
535 goto rflag;
536 case '.':
537 if ((ch = *fmt++) == '*') {
538 GETASTER(n);
539 prec = n < 0 ? -1 : n;
540 goto rflag;
541 }
542 n = 0;
543 while (is_digit(ch)) {
544 APPEND_DIGIT(n, ch);
545 ch = *fmt++;
546 }
547 if (ch == '$') {
548 nextarg = n;
549 if (argtable == NULL) {
550 argtable = statargtable;
551 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
552 ret = -1;
553 goto error;
554 }
555 }
556 goto rflag;
557 }
558 prec = n;
559 goto reswitch;
560 case '0':
561 /*
562 * ``Note that 0 is taken as a flag, not as the
563 * beginning of a field width.''
564 * -- ANSI X3J11
565 */
566 flags |= ZEROPAD;
567 goto rflag;
568 case '1':
569 case '2':
570 case '3':
571 case '4':
572 case '5':
573 case '6':
574 case '7':
575 case '8':
576 case '9':
577 n = 0;
578 do {
579 APPEND_DIGIT(n, ch);
580 ch = *fmt++;
581 } while (is_digit(ch));
582 if (ch == '$') {
583 nextarg = n;
584 if (argtable == NULL) {
585 argtable = statargtable;
586 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
587 ret = -1;
588 goto error;
589 }
590 }
591 goto rflag;
592 }
593 width = n;
594 goto reswitch;
595 case 'L':
596 flags |= LONGDBL;
597 goto rflag;
598 case 'h':
599 if (*fmt == 'h') {
600 fmt++;
601 flags |= CHARINT;
602 } else {
603 flags |= SHORTINT;
604 }
605 goto rflag;
606 case 'j':
607 flags |= MAXINT;
608 goto rflag;
609 case 'l':
610 if (*fmt == 'l') {
611 fmt++;
612 flags |= LLONGINT;
613 } else {
614 flags |= LONGINT;
615 }
616 goto rflag;
617 case 'q':
618 flags |= LLONGINT;
619 goto rflag;
620 case 't':
621 flags |= PTRINT;
622 goto rflag;
623 case 'z':
624 flags |= SIZEINT;
625 goto rflag;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700626 case 'C':
627 flags |= LONGINT;
628 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700629 case 'c':
630 if (flags & LONGINT) {
631 mbstate_t mbs;
632 size_t mbseqlen;
Elliott Hughes05493712014-04-17 17:30:03 -0700633
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700634 memset(&mbs, 0, sizeof(mbs));
635 mbseqlen = wcrtomb(buf, (wchar_t)GETARG(wint_t), &mbs);
636 if (mbseqlen == (size_t)-1) {
637 ret = -1;
638 goto error;
639 }
640 cp = buf;
641 size = (int)mbseqlen;
642 } else {
643 *(cp = buf) = GETARG(int);
644 size = 1;
645 }
646 sign = '\0';
647 break;
648 case 'D':
649 flags |= LONGINT;
650 /*FALLTHROUGH*/
651 case 'd':
652 case 'i':
653 _umax = SARG();
654 if ((intmax_t)_umax < 0) {
655 _umax = -_umax;
656 sign = '-';
657 }
658 base = DEC;
659 goto number;
660 case 'a':
661 case 'A':
662 if (ch == 'a') {
663 ox[1] = 'x';
664 xdigs = xdigs_lower;
665 expchar = 'p';
666 } else {
667 ox[1] = 'X';
668 xdigs = xdigs_upper;
669 expchar = 'P';
670 }
671 if (prec >= 0) prec++;
672 if (dtoaresult) __freedtoa(dtoaresult);
673 if (flags & LONGDBL) {
674 fparg.ldbl = GETARG(long double);
675 dtoaresult = cp = __hldtoa(fparg.ldbl, xdigs, prec, &expt, &signflag, &dtoaend);
676 if (dtoaresult == NULL) {
677 errno = ENOMEM;
678 goto error;
679 }
680 } else {
681 fparg.dbl = GETARG(double);
682 dtoaresult = cp = __hdtoa(fparg.dbl, xdigs, prec, &expt, &signflag, &dtoaend);
683 if (dtoaresult == NULL) {
684 errno = ENOMEM;
685 goto error;
686 }
687 }
688 if (prec < 0) prec = dtoaend - cp;
689 if (expt == INT_MAX) ox[1] = '\0';
690 goto fp_common;
691 case 'e':
692 case 'E':
693 expchar = ch;
694 if (prec < 0) /* account for digit before decpt */
695 prec = DEFPREC + 1;
696 else
697 prec++;
698 goto fp_begin;
699 case 'f':
700 case 'F':
701 expchar = '\0';
702 goto fp_begin;
703 case 'g':
704 case 'G':
705 expchar = ch - ('g' - 'e');
706 if (prec == 0) prec = 1;
707 fp_begin:
708 if (prec < 0) prec = DEFPREC;
709 if (dtoaresult) __freedtoa(dtoaresult);
710 if (flags & LONGDBL) {
711 fparg.ldbl = GETARG(long double);
712 dtoaresult = cp = __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
713 if (dtoaresult == NULL) {
714 errno = ENOMEM;
715 goto error;
716 }
717 } else {
718 fparg.dbl = GETARG(double);
719 dtoaresult = cp = __dtoa(fparg.dbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
720 if (dtoaresult == NULL) {
721 errno = ENOMEM;
722 goto error;
723 }
724 if (expt == 9999) expt = INT_MAX;
725 }
726 fp_common:
727 if (signflag) sign = '-';
728 if (expt == INT_MAX) { /* inf or nan */
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700729 if (*cp == 'N') {
730 cp = const_cast<char*>((ch >= 'a') ? "nan" : "NAN");
731 } else {
732 cp = const_cast<char*>((ch >= 'a') ? "inf" : "INF");
733 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700734 size = 3;
735 flags &= ~ZEROPAD;
736 break;
737 }
738 flags |= FPT;
739 ndig = dtoaend - cp;
740 if (ch == 'g' || ch == 'G') {
741 if (expt > -4 && expt <= prec) {
742 /* Make %[gG] smell like %[fF] */
743 expchar = '\0';
744 if (flags & ALT)
745 prec -= expt;
746 else
747 prec = ndig - expt;
748 if (prec < 0) prec = 0;
749 } else {
750 /*
751 * Make %[gG] smell like %[eE], but
752 * trim trailing zeroes if no # flag.
753 */
754 if (!(flags & ALT)) prec = ndig;
755 }
756 }
757 if (expchar) {
758 expsize = exponent(expstr, expt - 1, expchar);
759 size = expsize + prec;
760 if (prec > 1 || flags & ALT) ++size;
761 } else {
762 /* space for digits before decimal point */
763 if (expt > 0)
764 size = expt;
765 else /* "0" */
766 size = 1;
767 /* space for decimal pt and following digits */
768 if (prec || flags & ALT) size += prec + 1;
769 lead = expt;
770 }
771 break;
Elliott Hughese2341d02014-05-02 18:16:32 -0700772#ifndef NO_PRINTF_PERCENT_N
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700773 case 'n':
774 if (flags & LLONGINT)
775 *GETARG(long long*) = ret;
776 else if (flags & LONGINT)
777 *GETARG(long*) = ret;
778 else if (flags & SHORTINT)
779 *GETARG(short*) = ret;
780 else if (flags & CHARINT)
781 *GETARG(signed char*) = ret;
782 else if (flags & PTRINT)
783 *GETARG(ptrdiff_t*) = ret;
784 else if (flags & SIZEINT)
785 *GETARG(ssize_t*) = ret;
786 else if (flags & MAXINT)
787 *GETARG(intmax_t*) = ret;
788 else
789 *GETARG(int*) = ret;
790 continue; /* no output */
791#endif /* NO_PRINTF_PERCENT_N */
792 case 'O':
793 flags |= LONGINT;
794 /*FALLTHROUGH*/
795 case 'o':
796 _umax = UARG();
797 base = OCT;
798 goto nosign;
799 case 'p':
800 /*
801 * ``The argument shall be a pointer to void. The
802 * value of the pointer is converted to a sequence
803 * of printable characters, in an implementation-
804 * defined manner.''
805 * -- ANSI X3J11
806 */
807 _umax = (u_long)GETARG(void*);
808 base = HEX;
809 xdigs = xdigs_lower;
810 ox[1] = 'x';
811 goto nosign;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700812 case 'S':
813 flags |= LONGINT;
814 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700815 case 's':
816 if (flags & LONGINT) {
817 wchar_t* wcp;
Elliott Hughes05493712014-04-17 17:30:03 -0700818
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700819 free(convbuf);
820 convbuf = NULL;
821 if ((wcp = GETARG(wchar_t*)) == NULL) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700822 cp = const_cast<char*>("(null)");
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700823 } else {
824 convbuf = __wcsconv(wcp, prec);
825 if (convbuf == NULL) {
826 ret = -1;
827 goto error;
828 }
829 cp = convbuf;
830 }
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700831 } else if ((cp = GETARG(char*)) == NULL) {
832 cp = const_cast<char*>("(null)");
833 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700834 if (prec >= 0) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700835 size = strnlen(cp, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700836 } else {
837 size_t len;
Elliott Hughes05493712014-04-17 17:30:03 -0700838
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700839 if ((len = strlen(cp)) > INT_MAX) goto overflow;
840 size = (int)len;
841 }
842 sign = '\0';
843 break;
844 case 'U':
845 flags |= LONGINT;
846 /*FALLTHROUGH*/
847 case 'u':
848 _umax = UARG();
849 base = DEC;
850 goto nosign;
851 case 'X':
852 xdigs = xdigs_upper;
853 goto hex;
854 case 'x':
855 xdigs = xdigs_lower;
856 hex:
857 _umax = UARG();
858 base = HEX;
859 /* leading 0x/X only if non-zero */
860 if (flags & ALT && _umax != 0) ox[1] = ch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800861
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700862 /* unsigned conversions */
863 nosign:
864 sign = '\0';
865 /*
866 * ``... diouXx conversions ... if a precision is
867 * specified, the 0 flag will be ignored.''
868 * -- ANSI X3J11
869 */
870 number:
871 if ((dprec = prec) >= 0) flags &= ~ZEROPAD;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800872
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700873 /*
874 * ``The result of converting a zero value with an
875 * explicit precision of zero is no characters.''
876 * -- ANSI X3J11
877 */
878 cp = buf + BUF;
879 if (_umax != 0 || prec != 0) {
880 /*
881 * Unsigned mod is hard, and unsigned mod
882 * by a constant is easier than that by
883 * a variable; hence this switch.
884 */
885 switch (base) {
886 case OCT:
887 do {
888 *--cp = to_char(_umax & 7);
889 _umax >>= 3;
890 } while (_umax);
891 /* handle octal leading 0 */
892 if (flags & ALT && *cp != '0') *--cp = '0';
893 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800894
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700895 case DEC:
896 /* many numbers are 1 digit */
897 while (_umax >= 10) {
898 *--cp = to_char(_umax % 10);
899 _umax /= 10;
900 }
901 *--cp = to_char(_umax);
902 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800903
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700904 case HEX:
905 do {
906 *--cp = xdigs[_umax & 15];
907 _umax >>= 4;
908 } while (_umax);
909 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800910
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700911 default:
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700912 abort();
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700913 }
914 }
915 size = buf + BUF - cp;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700916 if (size > BUF) abort(); /* should never happen */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700917 break;
918 default: /* "%?" prints ?, unless ? is NUL */
919 if (ch == '\0') goto done;
920 /* pretend it was %c with argument ch */
921 cp = buf;
922 *cp = ch;
923 size = 1;
924 sign = '\0';
925 break;
926 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800927
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700928 /*
929 * All reasonable formats wind up here. At this point, `cp'
930 * points to a string which (if not flags&LADJUST) should be
931 * padded out to `width' places. If flags&ZEROPAD, it should
932 * first be prefixed by any sign or other prefix; otherwise,
933 * it should be blank padded before the prefix is emitted.
934 * After any left-hand padding and prefixing, emit zeroes
935 * required by a decimal %[diouxX] precision, then print the
936 * string proper, then emit zeroes required by any leftover
937 * floating precision; finally, if LADJUST, pad with blanks.
938 *
939 * Compute actual size, so we know how much to pad.
940 * size excludes decimal prec; realsz includes it.
941 */
942 realsz = dprec > size ? dprec : size;
943 if (sign) realsz++;
944 if (ox[1]) realsz += 2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800945
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700946 /* right-adjusting blank padding */
947 if ((flags & (LADJUST | ZEROPAD)) == 0) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800948
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700949 /* prefix */
950 if (sign) PRINT(&sign, 1);
951 if (ox[1]) { /* ox[1] is either x, X, or \0 */
952 ox[0] = '0';
953 PRINT(ox, 2);
954 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800955
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700956 /* right-adjusting zero padding */
957 if ((flags & (LADJUST | ZEROPAD)) == ZEROPAD) PAD(width - realsz, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800958
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700959 /* leading zeroes from decimal precision */
960 PAD(dprec - size, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800961
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700962 /* the string or number proper */
963 if ((flags & FPT) == 0) {
964 PRINT(cp, size);
965 } else { /* glue together f_p fragments */
966 if (decimal_point == NULL) decimal_point = nl_langinfo(RADIXCHAR);
967 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
968 if (expt <= 0) {
969 PRINT(zeroes, 1);
970 if (prec || flags & ALT) PRINT(decimal_point, 1);
971 PAD(-expt, zeroes);
972 /* already handled initial 0's */
973 prec += expt;
974 } else {
975 PRINTANDPAD(cp, dtoaend, lead, zeroes);
976 cp += lead;
977 if (prec || flags & ALT) PRINT(decimal_point, 1);
978 }
979 PRINTANDPAD(cp, dtoaend, prec, zeroes);
980 } else { /* %[eE] or sufficiently long %[gG] */
981 if (prec > 1 || flags & ALT) {
982 buf[0] = *cp++;
983 buf[1] = *decimal_point;
984 PRINT(buf, 2);
985 PRINT(cp, ndig - 1);
986 PAD(prec - ndig, zeroes);
987 } else { /* XeYYY */
988 PRINT(cp, 1);
989 }
990 PRINT(expstr, expsize);
991 }
992 }
993 /* left-adjusting padding (always blank) */
994 if (flags & LADJUST) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800995
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700996 /* finally, adjust ret */
997 if (width < realsz) width = realsz;
998 if (width > INT_MAX - ret) goto overflow;
999 ret += width;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001000
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001001 FLUSH(); /* copy out the I/O vectors */
1002 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001003done:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001004 FLUSH();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001005error:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001006 va_end(orgap);
1007 if (__sferror(fp)) ret = -1;
1008 goto finish;
Elliott Hughes05493712014-04-17 17:30:03 -07001009
1010overflow:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001011 errno = ENOMEM;
1012 ret = -1;
Elliott Hughes05493712014-04-17 17:30:03 -07001013
1014finish:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001015 free(convbuf);
1016 if (dtoaresult) __freedtoa(dtoaresult);
1017 if (argtable != NULL && argtable != statargtable) {
1018 munmap(argtable, argtablesiz);
1019 argtable = NULL;
1020 }
1021 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001022}
1023
1024/*
1025 * Type ids for argument type table.
1026 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001027#define T_UNUSED 0
1028#define T_SHORT 1
1029#define T_U_SHORT 2
1030#define TP_SHORT 3
1031#define T_INT 4
1032#define T_U_INT 5
1033#define TP_INT 6
1034#define T_LONG 7
1035#define T_U_LONG 8
1036#define TP_LONG 9
1037#define T_LLONG 10
1038#define T_U_LLONG 11
1039#define TP_LLONG 12
1040#define T_DOUBLE 13
1041#define T_LONG_DOUBLE 14
1042#define TP_CHAR 15
1043#define TP_VOID 16
1044#define T_PTRINT 17
1045#define TP_PTRINT 18
1046#define T_SIZEINT 19
1047#define T_SSIZEINT 20
1048#define TP_SSIZEINT 21
1049#define T_MAXINT 22
1050#define T_MAXUINT 23
1051#define TP_MAXINT 24
1052#define T_CHAR 25
1053#define T_U_CHAR 26
1054#define T_WINT 27
1055#define TP_WCHAR 28
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001056
1057/*
1058 * Find all arguments when a positional parameter is encountered. Returns a
1059 * table, indexed by argument number, of pointers to each arguments. The
1060 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1061 * It will be replaced with a mmap-ed one if it overflows (malloc cannot be
1062 * used since we are attempting to make snprintf thread safe, and alloca is
1063 * problematic since we have nested functions..)
1064 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001065static int __find_arguments(const char* fmt0, va_list ap, union arg** argtable,
1066 size_t* argtablesiz) {
1067 char* fmt; /* format string */
1068 int ch; /* character from fmt */
1069 int n, n2; /* handy integer (short term usage) */
1070 char* cp; /* handy char pointer (short term usage) */
1071 int flags; /* flags as above */
1072 unsigned char* typetable; /* table of types */
1073 unsigned char stattypetable[STATIC_ARG_TBL_SIZE];
1074 int tablesize; /* current size of type table */
1075 int tablemax; /* largest used index in table */
1076 int nextarg; /* 1-based argument index */
1077 int ret = 0; /* return value */
1078 wchar_t wc;
1079 mbstate_t ps;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001080
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001081 /*
1082 * Add an argument type to the table, expanding if necessary.
1083 */
1084#define ADDTYPE(type) \
1085 ((nextarg >= tablesize) ? __grow_type_table(&typetable, &tablesize) : 0, \
1086 (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = type)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001087
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001088#define ADDSARG() \
1089 ((flags & MAXINT) \
1090 ? ADDTYPE(T_MAXINT) \
1091 : ((flags & PTRINT) ? ADDTYPE(T_PTRINT) \
1092 : ((flags & SIZEINT) \
1093 ? ADDTYPE(T_SSIZEINT) \
1094 : ((flags & LLONGINT) \
1095 ? ADDTYPE(T_LLONG) \
1096 : ((flags & LONGINT) \
1097 ? ADDTYPE(T_LONG) \
1098 : ((flags & SHORTINT) \
1099 ? ADDTYPE(T_SHORT) \
1100 : ((flags & CHARINT) ? ADDTYPE(T_CHAR) \
1101 : ADDTYPE(T_INT))))))))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001102
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001103#define ADDUARG() \
1104 ((flags & MAXINT) \
1105 ? ADDTYPE(T_MAXUINT) \
1106 : ((flags & PTRINT) \
1107 ? ADDTYPE(T_PTRINT) \
1108 : ((flags & SIZEINT) \
1109 ? ADDTYPE(T_SIZEINT) \
1110 : ((flags & LLONGINT) \
1111 ? ADDTYPE(T_U_LLONG) \
1112 : ((flags & LONGINT) \
1113 ? ADDTYPE(T_U_LONG) \
1114 : ((flags & SHORTINT) \
1115 ? ADDTYPE(T_U_SHORT) \
1116 : ((flags & CHARINT) ? ADDTYPE(T_U_CHAR) \
1117 : ADDTYPE(T_U_INT))))))))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001118
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001119 /*
1120 * Add * arguments to the type array.
1121 */
1122#define ADDASTER() \
1123 n2 = 0; \
1124 cp = fmt; \
1125 while (is_digit(*cp)) { \
1126 APPEND_DIGIT(n2, *cp); \
1127 cp++; \
1128 } \
1129 if (*cp == '$') { \
1130 int hold = nextarg; \
1131 nextarg = n2; \
1132 ADDTYPE(T_INT); \
1133 nextarg = hold; \
1134 fmt = ++cp; \
1135 } else { \
1136 ADDTYPE(T_INT); \
1137 }
1138 fmt = (char*)fmt0;
1139 typetable = stattypetable;
1140 tablesize = STATIC_ARG_TBL_SIZE;
1141 tablemax = 0;
1142 nextarg = 1;
1143 memset(typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1144 memset(&ps, 0, sizeof(ps));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001145
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001146 /*
1147 * Scan the format for conversions (`%' character).
1148 */
1149 for (;;) {
1150 cp = fmt;
1151 while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
1152 fmt += n;
1153 if (wc == '%') {
1154 fmt--;
1155 break;
1156 }
1157 }
1158 if (n < 0) return (-1);
1159 if (n == 0) goto done;
1160 fmt++; /* skip over '%' */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001161
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001162 flags = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001163
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001164 rflag:
1165 ch = *fmt++;
1166 reswitch:
1167 switch (ch) {
1168 case ' ':
1169 case '#':
1170 case '\'':
1171 goto rflag;
1172 case '*':
1173 ADDASTER();
1174 goto rflag;
1175 case '-':
1176 case '+':
1177 goto rflag;
1178 case '.':
1179 if ((ch = *fmt++) == '*') {
1180 ADDASTER();
1181 goto rflag;
1182 }
1183 while (is_digit(ch)) {
1184 ch = *fmt++;
1185 }
1186 goto reswitch;
1187 case '0':
1188 goto rflag;
1189 case '1':
1190 case '2':
1191 case '3':
1192 case '4':
1193 case '5':
1194 case '6':
1195 case '7':
1196 case '8':
1197 case '9':
1198 n = 0;
1199 do {
1200 APPEND_DIGIT(n, ch);
1201 ch = *fmt++;
1202 } while (is_digit(ch));
1203 if (ch == '$') {
1204 nextarg = n;
1205 goto rflag;
1206 }
1207 goto reswitch;
1208 case 'L':
1209 flags |= LONGDBL;
1210 goto rflag;
1211 case 'h':
1212 if (*fmt == 'h') {
1213 fmt++;
1214 flags |= CHARINT;
1215 } else {
1216 flags |= SHORTINT;
1217 }
1218 goto rflag;
1219 case 'j':
1220 flags |= MAXINT;
1221 goto rflag;
1222 case 'l':
1223 if (*fmt == 'l') {
1224 fmt++;
1225 flags |= LLONGINT;
1226 } else {
1227 flags |= LONGINT;
1228 }
1229 goto rflag;
1230 case 'q':
1231 flags |= LLONGINT;
1232 goto rflag;
1233 case 't':
1234 flags |= PTRINT;
1235 goto rflag;
1236 case 'z':
1237 flags |= SIZEINT;
1238 goto rflag;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001239 case 'C':
1240 flags |= LONGINT;
1241 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001242 case 'c':
1243 if (flags & LONGINT)
1244 ADDTYPE(T_WINT);
1245 else
1246 ADDTYPE(T_INT);
1247 break;
1248 case 'D':
1249 flags |= LONGINT;
1250 /*FALLTHROUGH*/
1251 case 'd':
1252 case 'i':
1253 ADDSARG();
1254 break;
1255 case 'a':
1256 case 'A':
1257 case 'e':
1258 case 'E':
1259 case 'f':
1260 case 'F':
1261 case 'g':
1262 case 'G':
1263 if (flags & LONGDBL)
1264 ADDTYPE(T_LONG_DOUBLE);
1265 else
1266 ADDTYPE(T_DOUBLE);
1267 break;
Elliott Hughese2341d02014-05-02 18:16:32 -07001268#ifndef NO_PRINTF_PERCENT_N
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001269 case 'n':
1270 if (flags & LLONGINT)
1271 ADDTYPE(TP_LLONG);
1272 else if (flags & LONGINT)
1273 ADDTYPE(TP_LONG);
1274 else if (flags & SHORTINT)
1275 ADDTYPE(TP_SHORT);
1276 else if (flags & PTRINT)
1277 ADDTYPE(TP_PTRINT);
1278 else if (flags & SIZEINT)
1279 ADDTYPE(TP_SSIZEINT);
1280 else if (flags & MAXINT)
1281 ADDTYPE(TP_MAXINT);
1282 else
1283 ADDTYPE(TP_INT);
1284 continue; /* no output */
1285#endif /* NO_PRINTF_PERCENT_N */
1286 case 'O':
1287 flags |= LONGINT;
1288 /*FALLTHROUGH*/
1289 case 'o':
1290 ADDUARG();
1291 break;
1292 case 'p':
1293 ADDTYPE(TP_VOID);
1294 break;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001295 case 'S':
1296 flags |= LONGINT;
1297 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001298 case 's':
1299 if (flags & LONGINT)
1300 ADDTYPE(TP_WCHAR);
1301 else
1302 ADDTYPE(TP_CHAR);
1303 break;
1304 case 'U':
1305 flags |= LONGINT;
1306 /*FALLTHROUGH*/
1307 case 'u':
1308 case 'X':
1309 case 'x':
1310 ADDUARG();
1311 break;
1312 default: /* "%?" prints ?, unless ? is NUL */
1313 if (ch == '\0') goto done;
1314 break;
1315 }
1316 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001317done:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001318 /*
1319 * Build the argument table.
1320 */
1321 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1322 *argtablesiz = sizeof(union arg) * (tablemax + 1);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001323 *argtable = static_cast<arg*>(mmap(NULL, *argtablesiz,
1324 PROT_WRITE | PROT_READ,
1325 MAP_ANON | MAP_PRIVATE, -1, 0));
1326 if (*argtable == MAP_FAILED) return -1;
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001327 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001328
1329#if 0
1330 /* XXX is this required? */
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001331 (*argtable)[0].intarg = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001332#endif
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001333 for (n = 1; n <= tablemax; n++) {
1334 switch (typetable[n]) {
1335 case T_UNUSED:
1336 case T_CHAR:
1337 case T_U_CHAR:
1338 case T_SHORT:
1339 case T_U_SHORT:
1340 case T_INT:
1341 (*argtable)[n].intarg = va_arg(ap, int);
1342 break;
1343 case TP_SHORT:
1344 (*argtable)[n].pshortarg = va_arg(ap, short*);
1345 break;
1346 case T_U_INT:
1347 (*argtable)[n].uintarg = va_arg(ap, unsigned int);
1348 break;
1349 case TP_INT:
1350 (*argtable)[n].pintarg = va_arg(ap, int*);
1351 break;
1352 case T_LONG:
1353 (*argtable)[n].longarg = va_arg(ap, long);
1354 break;
1355 case T_U_LONG:
1356 (*argtable)[n].ulongarg = va_arg(ap, unsigned long);
1357 break;
1358 case TP_LONG:
1359 (*argtable)[n].plongarg = va_arg(ap, long*);
1360 break;
1361 case T_LLONG:
1362 (*argtable)[n].longlongarg = va_arg(ap, long long);
1363 break;
1364 case T_U_LLONG:
1365 (*argtable)[n].ulonglongarg = va_arg(ap, unsigned long long);
1366 break;
1367 case TP_LLONG:
1368 (*argtable)[n].plonglongarg = va_arg(ap, long long*);
1369 break;
1370 case T_DOUBLE:
1371 (*argtable)[n].doublearg = va_arg(ap, double);
1372 break;
1373 case T_LONG_DOUBLE:
1374 (*argtable)[n].longdoublearg = va_arg(ap, long double);
1375 break;
1376 case TP_CHAR:
1377 (*argtable)[n].pchararg = va_arg(ap, char*);
1378 break;
1379 case TP_VOID:
1380 (*argtable)[n].pvoidarg = va_arg(ap, void*);
1381 break;
1382 case T_PTRINT:
1383 (*argtable)[n].ptrdiffarg = va_arg(ap, ptrdiff_t);
1384 break;
1385 case TP_PTRINT:
1386 (*argtable)[n].pptrdiffarg = va_arg(ap, ptrdiff_t*);
1387 break;
1388 case T_SIZEINT:
1389 (*argtable)[n].sizearg = va_arg(ap, size_t);
1390 break;
1391 case T_SSIZEINT:
1392 (*argtable)[n].ssizearg = va_arg(ap, ssize_t);
1393 break;
1394 case TP_SSIZEINT:
1395 (*argtable)[n].pssizearg = va_arg(ap, ssize_t*);
1396 break;
1397 case T_MAXINT:
1398 (*argtable)[n].intmaxarg = va_arg(ap, intmax_t);
1399 break;
1400 case T_MAXUINT:
1401 (*argtable)[n].uintmaxarg = va_arg(ap, uintmax_t);
1402 break;
1403 case TP_MAXINT:
1404 (*argtable)[n].pintmaxarg = va_arg(ap, intmax_t*);
1405 break;
1406 case T_WINT:
1407 (*argtable)[n].wintarg = va_arg(ap, wint_t);
1408 break;
1409 case TP_WCHAR:
1410 (*argtable)[n].pwchararg = va_arg(ap, wchar_t*);
1411 break;
1412 }
1413 }
1414 goto finish;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001415
Elliott Hughes05493712014-04-17 17:30:03 -07001416overflow:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001417 errno = ENOMEM;
1418 ret = -1;
Elliott Hughes05493712014-04-17 17:30:03 -07001419
1420finish:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001421 if (typetable != NULL && typetable != stattypetable) {
1422 munmap(typetable, *argtablesiz);
1423 typetable = NULL;
1424 }
1425 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001426}
1427
1428/*
1429 * Increase the size of the type table.
1430 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001431static int __grow_type_table(unsigned char** typetable, int* tablesize) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001432 unsigned char* old_table = *typetable;
1433 int new_size = *tablesize * 2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001434
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001435 if (new_size < getpagesize()) new_size = getpagesize();
Elliott Hughes05493712014-04-17 17:30:03 -07001436
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001437 if (*tablesize == STATIC_ARG_TBL_SIZE) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001438 *typetable = static_cast<unsigned char*>(mmap(NULL, new_size,
1439 PROT_WRITE | PROT_READ,
1440 MAP_ANON | MAP_PRIVATE, -1, 0));
1441 if (*typetable == MAP_FAILED) return -1;
1442 bcopy(old_table, *typetable, *tablesize);
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001443 } else {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001444 unsigned char* new_table = static_cast<unsigned char*>(mmap(NULL, new_size,
1445 PROT_WRITE | PROT_READ,
1446 MAP_ANON | MAP_PRIVATE, -1, 0));
1447 if (new_table == MAP_FAILED) return -1;
1448 memmove(new_table, *typetable, *tablesize);
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001449 munmap(*typetable, *tablesize);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001450 *typetable = new_table;
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001451 }
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001452 memset(*typetable + *tablesize, T_UNUSED, (new_size - *tablesize));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001453
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001454 *tablesize = new_size;
1455 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001456}