blob: 6d1682894a89a3bac6b963e64a91b285f2c2b158 [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 Hughesc8f2c522017-10-31 13:07:51 -070086static int __find_arguments(const char* fmt0, va_list ap, union arg** argtable, size_t* argtablesiz);
87static 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 int ret;
Kenny Rootf5823402011-02-12 07:13:44 -0800255
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700256 FLOCKFILE(fp);
257 ret = __vfprintf(fp, fmt0, ap);
258 FUNLOCKFILE(fp);
259 return (ret);
Kenny Rootf5823402011-02-12 07:13:44 -0800260}
Elliott Hughes506c6de2016-01-15 16:30:18 -0800261DEF_STRONG(vfprintf);
Kenny Rootf5823402011-02-12 07:13:44 -0800262
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700263int __vfprintf(FILE* fp, const char* fmt0, __va_list ap) {
264 char* fmt; /* format string */
265 int ch; /* character from fmt */
266 int n, n2; /* handy integers (short term usage) */
267 char* cp; /* handy char pointer (short term usage) */
268 struct __siov* iovp; /* for PRINT macro */
269 int flags; /* flags as above */
270 int ret; /* return value accumulator */
271 int width; /* width from format (%8d), or 0 */
272 int prec; /* precision from format; <0 for N/A */
273 char sign; /* sign prefix (' ', '+', '-', or \0) */
274 wchar_t wc;
275 mbstate_t ps;
276 /*
277 * We can decompose the printed representation of floating
278 * point numbers into several parts, some of which may be empty:
279 *
280 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
281 * A B ---C--- D E F
282 *
283 * A: 'sign' holds this value if present; '\0' otherwise
284 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
285 * C: cp points to the string MMMNNN. Leading and trailing
286 * zeros are not in the string and must be added.
287 * D: expchar holds this character; '\0' if no exponent, e.g. %f
288 * F: at least two digits for decimal, at least one digit for hex
289 */
290 char* decimal_point = NULL;
291 int signflag; /* true if float is negative */
292 union { /* floating point arguments %[aAeEfFgG] */
293 double dbl;
294 long double ldbl;
295 } fparg;
296 int expt; /* integer value of exponent */
297 char expchar; /* exponent character: [eEpP\0] */
298 char* dtoaend; /* pointer to end of converted digits */
299 int expsize; /* character count for expstr */
300 int lead; /* sig figs before decimal or group sep */
301 int ndig; /* actual number of digits returned by dtoa */
302 char expstr[MAXEXPDIG + 2]; /* buffer for exponent string: e+ZZZ */
303 char* dtoaresult = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800304
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700305 uintmax_t _umax; /* integer arguments %[diouxX] */
306 enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */
307 int dprec; /* a copy of prec if %[diouxX], 0 otherwise */
308 int realsz; /* field size expanded by dprec */
309 int size; /* size of converted field or string */
310 const char* xdigs; /* digits for %[xX] conversion */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800311#define NIOV 8
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700312 struct __suio uio; /* output information: summary */
313 struct __siov iov[NIOV]; /* ... and individual io vectors */
314 char buf[BUF]; /* buffer with space for digits of uintmax_t */
315 char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
316 union arg* argtable; /* args, built due to positional arg */
317 union arg statargtable[STATIC_ARG_TBL_SIZE];
318 size_t argtablesiz;
319 int nextarg; /* 1-based argument index */
320 va_list orgap; /* original argument pointer */
321 char* convbuf; /* buffer for wide to multi-byte conversion */
Elliott Hughes05493712014-04-17 17:30:03 -0700322
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700323 /*
324 * Choose PADSIZE to trade efficiency vs. size. If larger printf
325 * fields occur frequently, increase PADSIZE and make the initialisers
326 * below longer.
327 */
328#define PADSIZE 16 /* pad chunk size */
329 static char blanks[PADSIZE] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
330 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
331 static char zeroes[PADSIZE] = { '0', '0', '0', '0', '0', '0', '0', '0',
332 '0', '0', '0', '0', '0', '0', '0', '0' };
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800333
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700334 static const char xdigs_lower[] = "0123456789abcdef";
335 static const char xdigs_upper[] = "0123456789ABCDEF";
Elliott Hughes05493712014-04-17 17:30:03 -0700336
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700337 /*
338 * BEWARE, these `goto error' on error, and PAD uses `n'.
339 */
340#define PRINT(ptr, len) \
341 do { \
342 iovp->iov_base = (ptr); \
343 iovp->iov_len = (len); \
344 uio.uio_resid += (len); \
345 iovp++; \
346 if (++uio.uio_iovcnt >= NIOV) { \
347 if (__sprint(fp, &uio)) goto error; \
348 iovp = iov; \
349 } \
350 } while (0)
351#define PAD(howmany, with) \
352 do { \
353 if ((n = (howmany)) > 0) { \
354 while (n > PADSIZE) { \
355 PRINT(with, PADSIZE); \
356 n -= PADSIZE; \
357 } \
358 PRINT(with, n); \
359 } \
360 } while (0)
361#define PRINTANDPAD(p, ep, len, with) \
362 do { \
363 n2 = (ep) - (p); \
364 if (n2 > (len)) n2 = (len); \
365 if (n2 > 0) PRINT((p), n2); \
366 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
367 } while (0)
368#define FLUSH() \
369 do { \
370 if (uio.uio_resid && __sprint(fp, &uio)) goto error; \
371 uio.uio_iovcnt = 0; \
372 iovp = iov; \
373 } while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800374
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700375 /*
376 * To extend shorts properly, we need both signed and unsigned
377 * argument extraction methods.
378 */
379#define SARG() \
380 ((intmax_t)(flags & MAXINT \
381 ? GETARG(intmax_t) \
382 : flags & LLONGINT \
383 ? GETARG(long long) \
384 : flags & LONGINT \
385 ? GETARG(long) \
386 : flags & PTRINT \
387 ? GETARG(ptrdiff_t) \
388 : flags & SIZEINT \
389 ? GETARG(ssize_t) \
390 : flags & SHORTINT \
391 ? (short)GETARG(int) \
392 : flags & CHARINT ? (signed char)GETARG(int) \
393 : GETARG(int)))
394#define UARG() \
395 ((uintmax_t)(flags & MAXINT \
396 ? GETARG(uintmax_t) \
397 : flags & LLONGINT \
398 ? GETARG(unsigned long long) \
399 : flags & LONGINT \
400 ? GETARG(unsigned long) \
401 : flags & PTRINT ? (uintptr_t)GETARG(ptrdiff_t) : /* XXX */ \
402 flags & SIZEINT \
403 ? GETARG(size_t) \
404 : flags & SHORTINT \
405 ? (unsigned short)GETARG(int) \
406 : flags & CHARINT ? (unsigned char)GETARG(int) \
407 : GETARG(unsigned int)))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800408
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700409 /*
410 * Append a digit to a value and check for overflow.
411 */
412#define APPEND_DIGIT(val, dig) \
413 do { \
414 if ((val) > INT_MAX / 10) goto overflow; \
415 (val) *= 10; \
416 if ((val) > INT_MAX - to_digit((dig))) goto overflow; \
417 (val) += to_digit((dig)); \
418 } while (0)
Elliott Hughes05493712014-04-17 17:30:03 -0700419
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700420 /*
421 * Get * arguments, including the form *nn$. Preserve the nextarg
422 * that the argument can be gotten once the type is determined.
423 */
424#define GETASTER(val) \
425 n2 = 0; \
426 cp = fmt; \
427 while (is_digit(*cp)) { \
428 APPEND_DIGIT(n2, *cp); \
429 cp++; \
430 } \
431 if (*cp == '$') { \
432 int hold = nextarg; \
433 if (argtable == NULL) { \
434 argtable = statargtable; \
435 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) { \
436 ret = -1; \
437 goto error; \
438 } \
439 } \
440 nextarg = n2; \
441 val = GETARG(int); \
442 nextarg = hold; \
443 fmt = ++cp; \
444 } else { \
445 val = GETARG(int); \
446 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800447
448/*
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700449 * Get the argument indexed by nextarg. If the argument table is
450 * built, use it to get the argument. If its not, get the next
451 * argument (and arguments must be gotten sequentially).
452 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800453#define GETARG(type) \
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700454 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : (nextarg++, va_arg(ap, type)))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800455
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700456 _SET_ORIENTATION(fp, -1);
457 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
458 if (cantwrite(fp)) {
459 errno = EBADF;
460 return (EOF);
461 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800462
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700463 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
464 if ((fp->_flags & (__SNBF | __SWR | __SRW)) == (__SNBF | __SWR) && fp->_file >= 0)
465 return (__sbprintf(fp, fmt0, ap));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800466
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700467 fmt = (char*)fmt0;
468 argtable = NULL;
469 nextarg = 1;
470 va_copy(orgap, ap);
471 uio.uio_iov = iovp = iov;
472 uio.uio_resid = 0;
473 uio.uio_iovcnt = 0;
474 ret = 0;
475 convbuf = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800476
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700477 memset(&ps, 0, sizeof(ps));
478 /*
479 * Scan the format for conversions (`%' character).
480 */
481 for (;;) {
482 cp = fmt;
483 while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
484 fmt += n;
485 if (wc == '%') {
486 fmt--;
487 break;
488 }
489 }
490 if (n < 0) {
491 ret = -1;
492 goto error;
493 }
494 if (fmt != cp) {
495 ptrdiff_t m = fmt - cp;
496 if (m < 0 || m > INT_MAX - ret) goto overflow;
497 PRINT(cp, m);
498 ret += m;
499 }
500 if (n == 0) goto done;
501 fmt++; /* skip over '%' */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800502
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700503 flags = 0;
504 dprec = 0;
505 width = 0;
506 prec = -1;
507 sign = '\0';
508 ox[1] = '\0';
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800509
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700510 rflag:
511 ch = *fmt++;
512 reswitch:
513 switch (ch) {
514 case ' ':
515 /*
516 * ``If the space and + flags both appear, the space
517 * flag will be ignored.''
518 * -- ANSI X3J11
519 */
520 if (!sign) sign = ' ';
521 goto rflag;
522 case '#':
523 flags |= ALT;
524 goto rflag;
525 case '\'':
526 /* grouping not implemented */
527 goto rflag;
528 case '*':
529 /*
530 * ``A negative field width argument is taken as a
531 * - flag followed by a positive field width.''
532 * -- ANSI X3J11
533 * They don't exclude field widths read from args.
534 */
535 GETASTER(width);
536 if (width >= 0) goto rflag;
537 if (width == INT_MIN) goto overflow;
538 width = -width;
539 /* FALLTHROUGH */
540 case '-':
541 flags |= LADJUST;
542 goto rflag;
543 case '+':
544 sign = '+';
545 goto rflag;
546 case '.':
547 if ((ch = *fmt++) == '*') {
548 GETASTER(n);
549 prec = n < 0 ? -1 : n;
550 goto rflag;
551 }
552 n = 0;
553 while (is_digit(ch)) {
554 APPEND_DIGIT(n, ch);
555 ch = *fmt++;
556 }
557 if (ch == '$') {
558 nextarg = n;
559 if (argtable == NULL) {
560 argtable = statargtable;
561 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
562 ret = -1;
563 goto error;
564 }
565 }
566 goto rflag;
567 }
568 prec = n;
569 goto reswitch;
570 case '0':
571 /*
572 * ``Note that 0 is taken as a flag, not as the
573 * beginning of a field width.''
574 * -- ANSI X3J11
575 */
576 flags |= ZEROPAD;
577 goto rflag;
578 case '1':
579 case '2':
580 case '3':
581 case '4':
582 case '5':
583 case '6':
584 case '7':
585 case '8':
586 case '9':
587 n = 0;
588 do {
589 APPEND_DIGIT(n, ch);
590 ch = *fmt++;
591 } while (is_digit(ch));
592 if (ch == '$') {
593 nextarg = n;
594 if (argtable == NULL) {
595 argtable = statargtable;
596 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
597 ret = -1;
598 goto error;
599 }
600 }
601 goto rflag;
602 }
603 width = n;
604 goto reswitch;
605 case 'L':
606 flags |= LONGDBL;
607 goto rflag;
608 case 'h':
609 if (*fmt == 'h') {
610 fmt++;
611 flags |= CHARINT;
612 } else {
613 flags |= SHORTINT;
614 }
615 goto rflag;
616 case 'j':
617 flags |= MAXINT;
618 goto rflag;
619 case 'l':
620 if (*fmt == 'l') {
621 fmt++;
622 flags |= LLONGINT;
623 } else {
624 flags |= LONGINT;
625 }
626 goto rflag;
627 case 'q':
628 flags |= LLONGINT;
629 goto rflag;
630 case 't':
631 flags |= PTRINT;
632 goto rflag;
633 case 'z':
634 flags |= SIZEINT;
635 goto rflag;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700636 case 'C':
637 flags |= LONGINT;
638 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700639 case 'c':
640 if (flags & LONGINT) {
641 mbstate_t mbs;
642 size_t mbseqlen;
Elliott Hughes05493712014-04-17 17:30:03 -0700643
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700644 memset(&mbs, 0, sizeof(mbs));
645 mbseqlen = wcrtomb(buf, (wchar_t)GETARG(wint_t), &mbs);
646 if (mbseqlen == (size_t)-1) {
647 ret = -1;
648 goto error;
649 }
650 cp = buf;
651 size = (int)mbseqlen;
652 } else {
653 *(cp = buf) = GETARG(int);
654 size = 1;
655 }
656 sign = '\0';
657 break;
658 case 'D':
659 flags |= LONGINT;
660 /*FALLTHROUGH*/
661 case 'd':
662 case 'i':
663 _umax = SARG();
664 if ((intmax_t)_umax < 0) {
665 _umax = -_umax;
666 sign = '-';
667 }
668 base = DEC;
669 goto number;
670 case 'a':
671 case 'A':
672 if (ch == 'a') {
673 ox[1] = 'x';
674 xdigs = xdigs_lower;
675 expchar = 'p';
676 } else {
677 ox[1] = 'X';
678 xdigs = xdigs_upper;
679 expchar = 'P';
680 }
681 if (prec >= 0) prec++;
682 if (dtoaresult) __freedtoa(dtoaresult);
683 if (flags & LONGDBL) {
684 fparg.ldbl = GETARG(long double);
685 dtoaresult = cp = __hldtoa(fparg.ldbl, xdigs, prec, &expt, &signflag, &dtoaend);
686 if (dtoaresult == NULL) {
687 errno = ENOMEM;
688 goto error;
689 }
690 } else {
691 fparg.dbl = GETARG(double);
692 dtoaresult = cp = __hdtoa(fparg.dbl, xdigs, prec, &expt, &signflag, &dtoaend);
693 if (dtoaresult == NULL) {
694 errno = ENOMEM;
695 goto error;
696 }
697 }
698 if (prec < 0) prec = dtoaend - cp;
699 if (expt == INT_MAX) ox[1] = '\0';
700 goto fp_common;
701 case 'e':
702 case 'E':
703 expchar = ch;
704 if (prec < 0) /* account for digit before decpt */
705 prec = DEFPREC + 1;
706 else
707 prec++;
708 goto fp_begin;
709 case 'f':
710 case 'F':
711 expchar = '\0';
712 goto fp_begin;
713 case 'g':
714 case 'G':
715 expchar = ch - ('g' - 'e');
716 if (prec == 0) prec = 1;
717 fp_begin:
718 if (prec < 0) prec = DEFPREC;
719 if (dtoaresult) __freedtoa(dtoaresult);
720 if (flags & LONGDBL) {
721 fparg.ldbl = GETARG(long double);
722 dtoaresult = cp = __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
723 if (dtoaresult == NULL) {
724 errno = ENOMEM;
725 goto error;
726 }
727 } else {
728 fparg.dbl = GETARG(double);
729 dtoaresult = cp = __dtoa(fparg.dbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
730 if (dtoaresult == NULL) {
731 errno = ENOMEM;
732 goto error;
733 }
734 if (expt == 9999) expt = INT_MAX;
735 }
736 fp_common:
737 if (signflag) sign = '-';
738 if (expt == INT_MAX) { /* inf or nan */
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700739 if (*cp == 'N') {
740 cp = const_cast<char*>((ch >= 'a') ? "nan" : "NAN");
741 } else {
742 cp = const_cast<char*>((ch >= 'a') ? "inf" : "INF");
743 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700744 size = 3;
745 flags &= ~ZEROPAD;
746 break;
747 }
748 flags |= FPT;
749 ndig = dtoaend - cp;
750 if (ch == 'g' || ch == 'G') {
751 if (expt > -4 && expt <= prec) {
752 /* Make %[gG] smell like %[fF] */
753 expchar = '\0';
754 if (flags & ALT)
755 prec -= expt;
756 else
757 prec = ndig - expt;
758 if (prec < 0) prec = 0;
759 } else {
760 /*
761 * Make %[gG] smell like %[eE], but
762 * trim trailing zeroes if no # flag.
763 */
764 if (!(flags & ALT)) prec = ndig;
765 }
766 }
767 if (expchar) {
768 expsize = exponent(expstr, expt - 1, expchar);
769 size = expsize + prec;
770 if (prec > 1 || flags & ALT) ++size;
771 } else {
772 /* space for digits before decimal point */
773 if (expt > 0)
774 size = expt;
775 else /* "0" */
776 size = 1;
777 /* space for decimal pt and following digits */
778 if (prec || flags & ALT) size += prec + 1;
779 lead = expt;
780 }
781 break;
Elliott Hughese2341d02014-05-02 18:16:32 -0700782#ifndef NO_PRINTF_PERCENT_N
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700783 case 'n':
784 if (flags & LLONGINT)
785 *GETARG(long long*) = ret;
786 else if (flags & LONGINT)
787 *GETARG(long*) = ret;
788 else if (flags & SHORTINT)
789 *GETARG(short*) = ret;
790 else if (flags & CHARINT)
791 *GETARG(signed char*) = ret;
792 else if (flags & PTRINT)
793 *GETARG(ptrdiff_t*) = ret;
794 else if (flags & SIZEINT)
795 *GETARG(ssize_t*) = ret;
796 else if (flags & MAXINT)
797 *GETARG(intmax_t*) = ret;
798 else
799 *GETARG(int*) = ret;
800 continue; /* no output */
801#endif /* NO_PRINTF_PERCENT_N */
802 case 'O':
803 flags |= LONGINT;
804 /*FALLTHROUGH*/
805 case 'o':
806 _umax = UARG();
807 base = OCT;
808 goto nosign;
809 case 'p':
810 /*
811 * ``The argument shall be a pointer to void. The
812 * value of the pointer is converted to a sequence
813 * of printable characters, in an implementation-
814 * defined manner.''
815 * -- ANSI X3J11
816 */
817 _umax = (u_long)GETARG(void*);
818 base = HEX;
819 xdigs = xdigs_lower;
820 ox[1] = 'x';
821 goto nosign;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700822 case 'S':
823 flags |= LONGINT;
824 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700825 case 's':
826 if (flags & LONGINT) {
827 wchar_t* wcp;
Elliott Hughes05493712014-04-17 17:30:03 -0700828
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700829 free(convbuf);
830 convbuf = NULL;
831 if ((wcp = GETARG(wchar_t*)) == NULL) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700832 cp = const_cast<char*>("(null)");
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700833 } else {
834 convbuf = __wcsconv(wcp, prec);
835 if (convbuf == NULL) {
836 ret = -1;
837 goto error;
838 }
839 cp = convbuf;
840 }
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700841 } else if ((cp = GETARG(char*)) == NULL) {
842 cp = const_cast<char*>("(null)");
843 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700844 if (prec >= 0) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700845 size = strnlen(cp, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700846 } else {
847 size_t len;
Elliott Hughes05493712014-04-17 17:30:03 -0700848
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700849 if ((len = strlen(cp)) > INT_MAX) goto overflow;
850 size = (int)len;
851 }
852 sign = '\0';
853 break;
854 case 'U':
855 flags |= LONGINT;
856 /*FALLTHROUGH*/
857 case 'u':
858 _umax = UARG();
859 base = DEC;
860 goto nosign;
861 case 'X':
862 xdigs = xdigs_upper;
863 goto hex;
864 case 'x':
865 xdigs = xdigs_lower;
866 hex:
867 _umax = UARG();
868 base = HEX;
869 /* leading 0x/X only if non-zero */
870 if (flags & ALT && _umax != 0) ox[1] = ch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800871
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700872 /* unsigned conversions */
873 nosign:
874 sign = '\0';
875 /*
876 * ``... diouXx conversions ... if a precision is
877 * specified, the 0 flag will be ignored.''
878 * -- ANSI X3J11
879 */
880 number:
881 if ((dprec = prec) >= 0) flags &= ~ZEROPAD;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800882
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700883 /*
884 * ``The result of converting a zero value with an
885 * explicit precision of zero is no characters.''
886 * -- ANSI X3J11
887 */
888 cp = buf + BUF;
889 if (_umax != 0 || prec != 0) {
890 /*
891 * Unsigned mod is hard, and unsigned mod
892 * by a constant is easier than that by
893 * a variable; hence this switch.
894 */
895 switch (base) {
896 case OCT:
897 do {
898 *--cp = to_char(_umax & 7);
899 _umax >>= 3;
900 } while (_umax);
901 /* handle octal leading 0 */
902 if (flags & ALT && *cp != '0') *--cp = '0';
903 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800904
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700905 case DEC:
906 /* many numbers are 1 digit */
907 while (_umax >= 10) {
908 *--cp = to_char(_umax % 10);
909 _umax /= 10;
910 }
911 *--cp = to_char(_umax);
912 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800913
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700914 case HEX:
915 do {
916 *--cp = xdigs[_umax & 15];
917 _umax >>= 4;
918 } while (_umax);
919 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800920
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700921 default:
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700922 abort();
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700923 }
924 }
925 size = buf + BUF - cp;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700926 if (size > BUF) abort(); /* should never happen */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700927 break;
928 default: /* "%?" prints ?, unless ? is NUL */
929 if (ch == '\0') goto done;
930 /* pretend it was %c with argument ch */
931 cp = buf;
932 *cp = ch;
933 size = 1;
934 sign = '\0';
935 break;
936 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800937
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700938 /*
939 * All reasonable formats wind up here. At this point, `cp'
940 * points to a string which (if not flags&LADJUST) should be
941 * padded out to `width' places. If flags&ZEROPAD, it should
942 * first be prefixed by any sign or other prefix; otherwise,
943 * it should be blank padded before the prefix is emitted.
944 * After any left-hand padding and prefixing, emit zeroes
945 * required by a decimal %[diouxX] precision, then print the
946 * string proper, then emit zeroes required by any leftover
947 * floating precision; finally, if LADJUST, pad with blanks.
948 *
949 * Compute actual size, so we know how much to pad.
950 * size excludes decimal prec; realsz includes it.
951 */
952 realsz = dprec > size ? dprec : size;
953 if (sign) realsz++;
954 if (ox[1]) realsz += 2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800955
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700956 /* right-adjusting blank padding */
957 if ((flags & (LADJUST | ZEROPAD)) == 0) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800958
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700959 /* prefix */
960 if (sign) PRINT(&sign, 1);
961 if (ox[1]) { /* ox[1] is either x, X, or \0 */
962 ox[0] = '0';
963 PRINT(ox, 2);
964 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800965
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700966 /* right-adjusting zero padding */
967 if ((flags & (LADJUST | ZEROPAD)) == ZEROPAD) PAD(width - realsz, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800968
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700969 /* leading zeroes from decimal precision */
970 PAD(dprec - size, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800971
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700972 /* the string or number proper */
973 if ((flags & FPT) == 0) {
974 PRINT(cp, size);
975 } else { /* glue together f_p fragments */
976 if (decimal_point == NULL) decimal_point = nl_langinfo(RADIXCHAR);
977 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
978 if (expt <= 0) {
979 PRINT(zeroes, 1);
980 if (prec || flags & ALT) PRINT(decimal_point, 1);
981 PAD(-expt, zeroes);
982 /* already handled initial 0's */
983 prec += expt;
984 } else {
985 PRINTANDPAD(cp, dtoaend, lead, zeroes);
986 cp += lead;
987 if (prec || flags & ALT) PRINT(decimal_point, 1);
988 }
989 PRINTANDPAD(cp, dtoaend, prec, zeroes);
990 } else { /* %[eE] or sufficiently long %[gG] */
991 if (prec > 1 || flags & ALT) {
992 buf[0] = *cp++;
993 buf[1] = *decimal_point;
994 PRINT(buf, 2);
995 PRINT(cp, ndig - 1);
996 PAD(prec - ndig, zeroes);
997 } else { /* XeYYY */
998 PRINT(cp, 1);
999 }
1000 PRINT(expstr, expsize);
1001 }
1002 }
1003 /* left-adjusting padding (always blank) */
1004 if (flags & LADJUST) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001005
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001006 /* finally, adjust ret */
1007 if (width < realsz) width = realsz;
1008 if (width > INT_MAX - ret) goto overflow;
1009 ret += width;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001010
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001011 FLUSH(); /* copy out the I/O vectors */
1012 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001013done:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001014 FLUSH();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001015error:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001016 va_end(orgap);
1017 if (__sferror(fp)) ret = -1;
1018 goto finish;
Elliott Hughes05493712014-04-17 17:30:03 -07001019
1020overflow:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001021 errno = ENOMEM;
1022 ret = -1;
Elliott Hughes05493712014-04-17 17:30:03 -07001023
1024finish:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001025 free(convbuf);
1026 if (dtoaresult) __freedtoa(dtoaresult);
1027 if (argtable != NULL && argtable != statargtable) {
1028 munmap(argtable, argtablesiz);
1029 argtable = NULL;
1030 }
1031 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001032}
1033
1034/*
1035 * Type ids for argument type table.
1036 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001037#define T_UNUSED 0
1038#define T_SHORT 1
1039#define T_U_SHORT 2
1040#define TP_SHORT 3
1041#define T_INT 4
1042#define T_U_INT 5
1043#define TP_INT 6
1044#define T_LONG 7
1045#define T_U_LONG 8
1046#define TP_LONG 9
1047#define T_LLONG 10
1048#define T_U_LLONG 11
1049#define TP_LLONG 12
1050#define T_DOUBLE 13
1051#define T_LONG_DOUBLE 14
1052#define TP_CHAR 15
1053#define TP_VOID 16
1054#define T_PTRINT 17
1055#define TP_PTRINT 18
1056#define T_SIZEINT 19
1057#define T_SSIZEINT 20
1058#define TP_SSIZEINT 21
1059#define T_MAXINT 22
1060#define T_MAXUINT 23
1061#define TP_MAXINT 24
1062#define T_CHAR 25
1063#define T_U_CHAR 26
1064#define T_WINT 27
1065#define TP_WCHAR 28
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001066
1067/*
1068 * Find all arguments when a positional parameter is encountered. Returns a
1069 * table, indexed by argument number, of pointers to each arguments. The
1070 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1071 * It will be replaced with a mmap-ed one if it overflows (malloc cannot be
1072 * used since we are attempting to make snprintf thread safe, and alloca is
1073 * problematic since we have nested functions..)
1074 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001075static int __find_arguments(const char* fmt0, va_list ap, union arg** argtable,
1076 size_t* argtablesiz) {
1077 char* fmt; /* format string */
1078 int ch; /* character from fmt */
1079 int n, n2; /* handy integer (short term usage) */
1080 char* cp; /* handy char pointer (short term usage) */
1081 int flags; /* flags as above */
1082 unsigned char* typetable; /* table of types */
1083 unsigned char stattypetable[STATIC_ARG_TBL_SIZE];
1084 int tablesize; /* current size of type table */
1085 int tablemax; /* largest used index in table */
1086 int nextarg; /* 1-based argument index */
1087 int ret = 0; /* return value */
1088 wchar_t wc;
1089 mbstate_t ps;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001090
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001091 /*
1092 * Add an argument type to the table, expanding if necessary.
1093 */
1094#define ADDTYPE(type) \
1095 ((nextarg >= tablesize) ? __grow_type_table(&typetable, &tablesize) : 0, \
1096 (nextarg > tablemax) ? tablemax = nextarg : 0, typetable[nextarg++] = type)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001097
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001098#define ADDSARG() \
1099 ((flags & MAXINT) \
1100 ? ADDTYPE(T_MAXINT) \
1101 : ((flags & PTRINT) ? ADDTYPE(T_PTRINT) \
1102 : ((flags & SIZEINT) \
1103 ? ADDTYPE(T_SSIZEINT) \
1104 : ((flags & LLONGINT) \
1105 ? ADDTYPE(T_LLONG) \
1106 : ((flags & LONGINT) \
1107 ? ADDTYPE(T_LONG) \
1108 : ((flags & SHORTINT) \
1109 ? ADDTYPE(T_SHORT) \
1110 : ((flags & CHARINT) ? ADDTYPE(T_CHAR) \
1111 : ADDTYPE(T_INT))))))))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001112
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001113#define ADDUARG() \
1114 ((flags & MAXINT) \
1115 ? ADDTYPE(T_MAXUINT) \
1116 : ((flags & PTRINT) \
1117 ? ADDTYPE(T_PTRINT) \
1118 : ((flags & SIZEINT) \
1119 ? ADDTYPE(T_SIZEINT) \
1120 : ((flags & LLONGINT) \
1121 ? ADDTYPE(T_U_LLONG) \
1122 : ((flags & LONGINT) \
1123 ? ADDTYPE(T_U_LONG) \
1124 : ((flags & SHORTINT) \
1125 ? ADDTYPE(T_U_SHORT) \
1126 : ((flags & CHARINT) ? ADDTYPE(T_U_CHAR) \
1127 : ADDTYPE(T_U_INT))))))))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001128
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001129 /*
1130 * Add * arguments to the type array.
1131 */
1132#define ADDASTER() \
1133 n2 = 0; \
1134 cp = fmt; \
1135 while (is_digit(*cp)) { \
1136 APPEND_DIGIT(n2, *cp); \
1137 cp++; \
1138 } \
1139 if (*cp == '$') { \
1140 int hold = nextarg; \
1141 nextarg = n2; \
1142 ADDTYPE(T_INT); \
1143 nextarg = hold; \
1144 fmt = ++cp; \
1145 } else { \
1146 ADDTYPE(T_INT); \
1147 }
1148 fmt = (char*)fmt0;
1149 typetable = stattypetable;
1150 tablesize = STATIC_ARG_TBL_SIZE;
1151 tablemax = 0;
1152 nextarg = 1;
1153 memset(typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1154 memset(&ps, 0, sizeof(ps));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001155
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001156 /*
1157 * Scan the format for conversions (`%' character).
1158 */
1159 for (;;) {
1160 cp = fmt;
1161 while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
1162 fmt += n;
1163 if (wc == '%') {
1164 fmt--;
1165 break;
1166 }
1167 }
1168 if (n < 0) return (-1);
1169 if (n == 0) goto done;
1170 fmt++; /* skip over '%' */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001171
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001172 flags = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001173
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001174 rflag:
1175 ch = *fmt++;
1176 reswitch:
1177 switch (ch) {
1178 case ' ':
1179 case '#':
1180 case '\'':
1181 goto rflag;
1182 case '*':
1183 ADDASTER();
1184 goto rflag;
1185 case '-':
1186 case '+':
1187 goto rflag;
1188 case '.':
1189 if ((ch = *fmt++) == '*') {
1190 ADDASTER();
1191 goto rflag;
1192 }
1193 while (is_digit(ch)) {
1194 ch = *fmt++;
1195 }
1196 goto reswitch;
1197 case '0':
1198 goto rflag;
1199 case '1':
1200 case '2':
1201 case '3':
1202 case '4':
1203 case '5':
1204 case '6':
1205 case '7':
1206 case '8':
1207 case '9':
1208 n = 0;
1209 do {
1210 APPEND_DIGIT(n, ch);
1211 ch = *fmt++;
1212 } while (is_digit(ch));
1213 if (ch == '$') {
1214 nextarg = n;
1215 goto rflag;
1216 }
1217 goto reswitch;
1218 case 'L':
1219 flags |= LONGDBL;
1220 goto rflag;
1221 case 'h':
1222 if (*fmt == 'h') {
1223 fmt++;
1224 flags |= CHARINT;
1225 } else {
1226 flags |= SHORTINT;
1227 }
1228 goto rflag;
1229 case 'j':
1230 flags |= MAXINT;
1231 goto rflag;
1232 case 'l':
1233 if (*fmt == 'l') {
1234 fmt++;
1235 flags |= LLONGINT;
1236 } else {
1237 flags |= LONGINT;
1238 }
1239 goto rflag;
1240 case 'q':
1241 flags |= LLONGINT;
1242 goto rflag;
1243 case 't':
1244 flags |= PTRINT;
1245 goto rflag;
1246 case 'z':
1247 flags |= SIZEINT;
1248 goto rflag;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001249 case 'C':
1250 flags |= LONGINT;
1251 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001252 case 'c':
1253 if (flags & LONGINT)
1254 ADDTYPE(T_WINT);
1255 else
1256 ADDTYPE(T_INT);
1257 break;
1258 case 'D':
1259 flags |= LONGINT;
1260 /*FALLTHROUGH*/
1261 case 'd':
1262 case 'i':
1263 ADDSARG();
1264 break;
1265 case 'a':
1266 case 'A':
1267 case 'e':
1268 case 'E':
1269 case 'f':
1270 case 'F':
1271 case 'g':
1272 case 'G':
1273 if (flags & LONGDBL)
1274 ADDTYPE(T_LONG_DOUBLE);
1275 else
1276 ADDTYPE(T_DOUBLE);
1277 break;
Elliott Hughese2341d02014-05-02 18:16:32 -07001278#ifndef NO_PRINTF_PERCENT_N
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001279 case 'n':
1280 if (flags & LLONGINT)
1281 ADDTYPE(TP_LLONG);
1282 else if (flags & LONGINT)
1283 ADDTYPE(TP_LONG);
1284 else if (flags & SHORTINT)
1285 ADDTYPE(TP_SHORT);
1286 else if (flags & PTRINT)
1287 ADDTYPE(TP_PTRINT);
1288 else if (flags & SIZEINT)
1289 ADDTYPE(TP_SSIZEINT);
1290 else if (flags & MAXINT)
1291 ADDTYPE(TP_MAXINT);
1292 else
1293 ADDTYPE(TP_INT);
1294 continue; /* no output */
1295#endif /* NO_PRINTF_PERCENT_N */
1296 case 'O':
1297 flags |= LONGINT;
1298 /*FALLTHROUGH*/
1299 case 'o':
1300 ADDUARG();
1301 break;
1302 case 'p':
1303 ADDTYPE(TP_VOID);
1304 break;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001305 case 'S':
1306 flags |= LONGINT;
1307 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001308 case 's':
1309 if (flags & LONGINT)
1310 ADDTYPE(TP_WCHAR);
1311 else
1312 ADDTYPE(TP_CHAR);
1313 break;
1314 case 'U':
1315 flags |= LONGINT;
1316 /*FALLTHROUGH*/
1317 case 'u':
1318 case 'X':
1319 case 'x':
1320 ADDUARG();
1321 break;
1322 default: /* "%?" prints ?, unless ? is NUL */
1323 if (ch == '\0') goto done;
1324 break;
1325 }
1326 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001327done:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001328 /*
1329 * Build the argument table.
1330 */
1331 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1332 *argtablesiz = sizeof(union arg) * (tablemax + 1);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001333 *argtable = static_cast<arg*>(mmap(NULL, *argtablesiz,
1334 PROT_WRITE | PROT_READ,
1335 MAP_ANON | MAP_PRIVATE, -1, 0));
1336 if (*argtable == MAP_FAILED) return -1;
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001337 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001338
1339#if 0
1340 /* XXX is this required? */
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001341 (*argtable)[0].intarg = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001342#endif
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001343 for (n = 1; n <= tablemax; n++) {
1344 switch (typetable[n]) {
1345 case T_UNUSED:
1346 case T_CHAR:
1347 case T_U_CHAR:
1348 case T_SHORT:
1349 case T_U_SHORT:
1350 case T_INT:
1351 (*argtable)[n].intarg = va_arg(ap, int);
1352 break;
1353 case TP_SHORT:
1354 (*argtable)[n].pshortarg = va_arg(ap, short*);
1355 break;
1356 case T_U_INT:
1357 (*argtable)[n].uintarg = va_arg(ap, unsigned int);
1358 break;
1359 case TP_INT:
1360 (*argtable)[n].pintarg = va_arg(ap, int*);
1361 break;
1362 case T_LONG:
1363 (*argtable)[n].longarg = va_arg(ap, long);
1364 break;
1365 case T_U_LONG:
1366 (*argtable)[n].ulongarg = va_arg(ap, unsigned long);
1367 break;
1368 case TP_LONG:
1369 (*argtable)[n].plongarg = va_arg(ap, long*);
1370 break;
1371 case T_LLONG:
1372 (*argtable)[n].longlongarg = va_arg(ap, long long);
1373 break;
1374 case T_U_LLONG:
1375 (*argtable)[n].ulonglongarg = va_arg(ap, unsigned long long);
1376 break;
1377 case TP_LLONG:
1378 (*argtable)[n].plonglongarg = va_arg(ap, long long*);
1379 break;
1380 case T_DOUBLE:
1381 (*argtable)[n].doublearg = va_arg(ap, double);
1382 break;
1383 case T_LONG_DOUBLE:
1384 (*argtable)[n].longdoublearg = va_arg(ap, long double);
1385 break;
1386 case TP_CHAR:
1387 (*argtable)[n].pchararg = va_arg(ap, char*);
1388 break;
1389 case TP_VOID:
1390 (*argtable)[n].pvoidarg = va_arg(ap, void*);
1391 break;
1392 case T_PTRINT:
1393 (*argtable)[n].ptrdiffarg = va_arg(ap, ptrdiff_t);
1394 break;
1395 case TP_PTRINT:
1396 (*argtable)[n].pptrdiffarg = va_arg(ap, ptrdiff_t*);
1397 break;
1398 case T_SIZEINT:
1399 (*argtable)[n].sizearg = va_arg(ap, size_t);
1400 break;
1401 case T_SSIZEINT:
1402 (*argtable)[n].ssizearg = va_arg(ap, ssize_t);
1403 break;
1404 case TP_SSIZEINT:
1405 (*argtable)[n].pssizearg = va_arg(ap, ssize_t*);
1406 break;
1407 case T_MAXINT:
1408 (*argtable)[n].intmaxarg = va_arg(ap, intmax_t);
1409 break;
1410 case T_MAXUINT:
1411 (*argtable)[n].uintmaxarg = va_arg(ap, uintmax_t);
1412 break;
1413 case TP_MAXINT:
1414 (*argtable)[n].pintmaxarg = va_arg(ap, intmax_t*);
1415 break;
1416 case T_WINT:
1417 (*argtable)[n].wintarg = va_arg(ap, wint_t);
1418 break;
1419 case TP_WCHAR:
1420 (*argtable)[n].pwchararg = va_arg(ap, wchar_t*);
1421 break;
1422 }
1423 }
1424 goto finish;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001425
Elliott Hughes05493712014-04-17 17:30:03 -07001426overflow:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001427 errno = ENOMEM;
1428 ret = -1;
Elliott Hughes05493712014-04-17 17:30:03 -07001429
1430finish:
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001431 if (typetable != NULL && typetable != stattypetable) {
1432 munmap(typetable, *argtablesiz);
1433 typetable = NULL;
1434 }
1435 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001436}
1437
1438/*
1439 * Increase the size of the type table.
1440 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001441static int __grow_type_table(unsigned char** typetable, int* tablesize) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001442 unsigned char* old_table = *typetable;
1443 int new_size = *tablesize * 2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001444
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001445 if (new_size < getpagesize()) new_size = getpagesize();
Elliott Hughes05493712014-04-17 17:30:03 -07001446
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001447 if (*tablesize == STATIC_ARG_TBL_SIZE) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001448 *typetable = static_cast<unsigned char*>(mmap(NULL, new_size,
1449 PROT_WRITE | PROT_READ,
1450 MAP_ANON | MAP_PRIVATE, -1, 0));
1451 if (*typetable == MAP_FAILED) return -1;
1452 bcopy(old_table, *typetable, *tablesize);
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001453 } else {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001454 unsigned char* new_table = static_cast<unsigned char*>(mmap(NULL, new_size,
1455 PROT_WRITE | PROT_READ,
1456 MAP_ANON | MAP_PRIVATE, -1, 0));
1457 if (new_table == MAP_FAILED) return -1;
1458 memmove(new_table, *typetable, *tablesize);
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001459 munmap(*typetable, *tablesize);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001460 *typetable = new_table;
Elliott Hughesc8f2c522017-10-31 13:07:51 -07001461 }
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001462 memset(*typetable + *tablesize, T_UNUSED, (new_size - *tablesize));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001463
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -07001464 *tablesize = new_size;
1465 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001466}