ndk: Add printf() et al to Android support library.
This adds a musl-based printf() implementation to the Android support
library. This adds support for exotic formatters like %a (hexadecimal
floats), required by the libc++ unit test suite, as found by Nico
Weber (thakis@chromium.org)
+ Add missing implementation of frexp()/frexpf()/frexpl()
(required by vfprintf.c)
+ Minimize the differences between our vfwprintf.c implementation
and the original musl one, by moving the output wrapper structure
to src/stdio/stdio_impl.[hc].
BUG=36496
Change-Id: I1b8e166646f5680434e3899bbecdf9492141e4ab
diff --git a/sources/android/support/Android.mk b/sources/android/support/Android.mk
index 6b1208d..9435722 100644
--- a/sources/android/support/Android.mk
+++ b/sources/android/support/Android.mk
@@ -11,6 +11,8 @@
src/locale/localeconv.c \
src/locale/newlocale.c \
src/locale/uselocale.c \
+ src/stdio/stdio_impl.c \
+ src/stdio/vfprintf.c \
src/stdio/vfwprintf.c \
src/musl-multibyte/btowc.c \
src/musl-multibyte/internal.c \
@@ -97,6 +99,14 @@
src/musl-locale/wcsxfrm_l.c \
src/musl-locale/wctrans_l.c \
src/musl-locale/wctype_l.c \
+ src/musl-math/frexp.c \
+ src/musl-math/frexpf.c \
+ src/musl-math/frexpl.c \
+ src/musl-stdio/printf.c \
+ src/musl-stdio/snprintf.c \
+ src/musl-stdio/sprintf.c \
+ src/musl-stdio/vprintf.c \
+ src/musl-stdio/vsprintf.c \
src/musl-stdio/swprintf.c \
src/musl-stdio/vwprintf.c \
src/musl-stdio/wprintf.c \
diff --git a/sources/android/support/src/musl-math/frexp.c b/sources/android/support/src/musl-math/frexp.c
new file mode 100644
index 0000000..27b6266
--- /dev/null
+++ b/sources/android/support/src/musl-math/frexp.c
@@ -0,0 +1,23 @@
+#include <math.h>
+#include <stdint.h>
+
+double frexp(double x, int *e)
+{
+ union { double d; uint64_t i; } y = { x };
+ int ee = y.i>>52 & 0x7ff;
+
+ if (!ee) {
+ if (x) {
+ x = frexp(x*0x1p64, e);
+ *e -= 64;
+ } else *e = 0;
+ return x;
+ } else if (ee == 0x7ff) {
+ return x;
+ }
+
+ *e = ee - 0x3fe;
+ y.i &= 0x800fffffffffffffull;
+ y.i |= 0x3fe0000000000000ull;
+ return y.d;
+}
diff --git a/sources/android/support/src/musl-math/frexpf.c b/sources/android/support/src/musl-math/frexpf.c
new file mode 100644
index 0000000..0787097
--- /dev/null
+++ b/sources/android/support/src/musl-math/frexpf.c
@@ -0,0 +1,23 @@
+#include <math.h>
+#include <stdint.h>
+
+float frexpf(float x, int *e)
+{
+ union { float f; uint32_t i; } y = { x };
+ int ee = y.i>>23 & 0xff;
+
+ if (!ee) {
+ if (x) {
+ x = frexpf(x*0x1p64, e);
+ *e -= 64;
+ } else *e = 0;
+ return x;
+ } else if (ee == 0xff) {
+ return x;
+ }
+
+ *e = ee - 0x7e;
+ y.i &= 0x807ffffful;
+ y.i |= 0x3f000000ul;
+ return y.f;
+}
diff --git a/sources/android/support/src/musl-math/frexpl.c b/sources/android/support/src/musl-math/frexpl.c
new file mode 100644
index 0000000..f9d90a6
--- /dev/null
+++ b/sources/android/support/src/musl-math/frexpl.c
@@ -0,0 +1,37 @@
+#include <math.h>
+#include <stdint.h>
+#include <float.h>
+
+#if LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
+
+/* This version is for 80-bit little endian long double */
+
+long double frexpl(long double x, int *e)
+{
+ union { long double ld; uint16_t hw[5]; } y = { x };
+ int ee = y.hw[4]&0x7fff;
+
+ if (!ee) {
+ if (x) {
+ x = frexpl(x*0x1p64, e);
+ *e -= 64;
+ } else *e = 0;
+ return x;
+ } else if (ee == 0x7fff) {
+ return x;
+ }
+
+ *e = ee - 0x3ffe;
+ y.hw[4] &= 0x8000;
+ y.hw[4] |= 0x3ffe;
+ return y.ld;
+}
+
+#else
+
+long double frexpl(long double x, int *e)
+{
+ return frexp(x, e);
+}
+
+#endif
diff --git a/sources/android/support/src/musl-stdio/printf.c b/sources/android/support/src/musl-stdio/printf.c
new file mode 100644
index 0000000..cebfe40
--- /dev/null
+++ b/sources/android/support/src/musl-stdio/printf.c
@@ -0,0 +1,12 @@
+#include <stdio.h>
+#include <stdarg.h>
+
+int printf(const char *restrict fmt, ...)
+{
+ int ret;
+ va_list ap;
+ va_start(ap, fmt);
+ ret = vfprintf(stdout, fmt, ap);
+ va_end(ap);
+ return ret;
+}
diff --git a/sources/android/support/src/musl-stdio/snprintf.c b/sources/android/support/src/musl-stdio/snprintf.c
new file mode 100644
index 0000000..771503b
--- /dev/null
+++ b/sources/android/support/src/musl-stdio/snprintf.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+#include <stdarg.h>
+
+int snprintf(char *restrict s, size_t n, const char *restrict fmt, ...)
+{
+ int ret;
+ va_list ap;
+ va_start(ap, fmt);
+ ret = vsnprintf(s, n, fmt, ap);
+ va_end(ap);
+ return ret;
+}
+
diff --git a/sources/android/support/src/musl-stdio/sprintf.c b/sources/android/support/src/musl-stdio/sprintf.c
new file mode 100644
index 0000000..9dff524
--- /dev/null
+++ b/sources/android/support/src/musl-stdio/sprintf.c
@@ -0,0 +1,12 @@
+#include <stdio.h>
+#include <stdarg.h>
+
+int sprintf(char *restrict s, const char *restrict fmt, ...)
+{
+ int ret;
+ va_list ap;
+ va_start(ap, fmt);
+ ret = vsprintf(s, fmt, ap);
+ va_end(ap);
+ return ret;
+}
diff --git a/sources/android/support/src/musl-stdio/vprintf.c b/sources/android/support/src/musl-stdio/vprintf.c
new file mode 100644
index 0000000..30d2bff
--- /dev/null
+++ b/sources/android/support/src/musl-stdio/vprintf.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int vprintf(const char *restrict fmt, va_list ap)
+{
+ return vfprintf(stdout, fmt, ap);
+}
diff --git a/sources/android/support/src/musl-stdio/vsprintf.c b/sources/android/support/src/musl-stdio/vsprintf.c
new file mode 100644
index 0000000..c57349d
--- /dev/null
+++ b/sources/android/support/src/musl-stdio/vsprintf.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+#include <limits.h>
+
+int vsprintf(char *restrict s, const char *restrict fmt, va_list ap)
+{
+ return vsnprintf(s, INT_MAX, fmt, ap);
+}
diff --git a/sources/android/support/src/stdio/stdio_impl.c b/sources/android/support/src/stdio/stdio_impl.c
new file mode 100644
index 0000000..e3c442d
--- /dev/null
+++ b/sources/android/support/src/stdio/stdio_impl.c
@@ -0,0 +1,129 @@
+#include <stdio.h>
+#include <wchar.h>
+#include <string.h>
+
+#define _STDIO_IMPL_NO_REDIRECT_MACROS
+#include "stdio_impl.h"
+
+void fake_file_init_file(FakeFILE* file, FILE* f) {
+ memset(file, 0, sizeof(*file));
+ file->file = f;
+}
+
+void fake_file_init_buffer(FakeFILE* file, char* buffer, size_t buffer_size) {
+ memset(file, 0, sizeof(*file));
+ file->buffer = buffer;
+ file->buffer_pos = 0;
+ file->buffer_size = buffer_size;
+}
+
+void fake_file_init_wbuffer(FakeFILE* file,
+ wchar_t* buffer,
+ size_t buffer_size) {
+ fake_file_init_buffer(file, (char*)buffer, buffer_size * sizeof(wchar_t));
+}
+
+void fake_file_out(FakeFILE* file, const char* text, size_t length) {
+ if (length == 0) {
+ // Happens pretty often, so bail immediately.
+ return;
+ }
+ if (file->file != NULL) {
+ fwrite(text, 1, length, file->file);
+ } else {
+ // Write into a bounded buffer.
+ size_t avail = file->buffer_size - file->buffer_pos;
+ if (length > avail)
+ length = avail;
+ memcpy((char*)(file->buffer + file->buffer_pos),
+ (const char*)text,
+ length);
+ file->buffer_pos += length;
+ }
+}
+
+void fake_file_outw(FakeFILE* file, const wchar_t* text, size_t length) {
+ if (length == 0)
+ return;
+ if (file->file != NULL) {
+ // Write into a file the UTF-8 encoded version.
+ // Original source calls fputwc() in a loop, which is slightly inefficient
+ // for large strings.
+ // TODO(digit): Support locale-specific encoding?
+ size_t mb_len = wcstombs(NULL, text, length);
+ char* mb_buffer = malloc(mb_len);
+ wcstombs(mb_buffer, text, length);
+ fwrite(mb_buffer, 1, mb_len, file->file);
+ free(mb_buffer);
+ } else {
+ // Write into a bounded buffer. This assumes the buffer really
+ // holds wchar_t items.
+ size_t avail = (file->buffer_size - file->buffer_pos) / sizeof(wchar_t);
+ if (length > avail)
+ length = avail;
+ memcpy((char*)(file->buffer + file->buffer_pos),
+ (const char*)text,
+ length * sizeof(wchar_t));
+ file->buffer_pos += length * sizeof(wchar_t);
+ }
+}
+
+int fake_feof(FakeFILE* file) {
+ if (file->file != NULL)
+ return feof(file->file);
+ else
+ return (file->buffer_pos >= file->buffer_size);
+}
+
+int fake_ferror(FakeFILE* file) {
+ if (file->file != NULL)
+ return ferror(file->file);
+
+ return 0;
+}
+
+int fake_fprintf(FakeFILE* file, const char* format, ...) {
+ va_list args;
+ va_start(args, format);
+ if (file->file)
+ return vfprintf(file->file, format, args);
+ else {
+ // TODO(digit): Make this faster.
+ // First, generate formatted byte output.
+ int mb_len = vsnprintf(NULL, 0, format, args);
+ char* mb_buffer = malloc(mb_len + 1);
+ vsnprintf(mb_buffer, mb_len + 1, format, args);
+ // Then convert to wchar_t buffer.
+ size_t wide_len = mbstowcs(NULL, mb_buffer, mb_len);
+ wchar_t* wide_buffer = malloc((wide_len + 1) * sizeof(wchar_t));
+ mbstowcs(wide_buffer, mb_buffer, mb_len);
+ // Add to buffer.
+ fake_file_outw(file, wide_buffer, wide_len);
+ // finished
+ free(wide_buffer);
+ free(mb_buffer);
+
+ return wide_len;
+ }
+ va_end(args);
+}
+
+void fake_fputc(char ch, FakeFILE* file) {
+ if (file->file)
+ fputc(ch, file->file);
+ else {
+ if (file->buffer_pos < file->buffer_size)
+ file->buffer[file->buffer_pos++] = ch;
+ }
+}
+
+void fake_fputwc(wchar_t wc, FakeFILE* file) {
+ if (file->file)
+ fputwc(wc, file->file);
+ else {
+ if (file->buffer_pos + sizeof(wchar_t) - 1U < file->buffer_size) {
+ *(wchar_t*)(&file->buffer[file->buffer_pos]) = wc;
+ file->buffer_pos += sizeof(wchar_t);
+ }
+ }
+}
diff --git a/sources/android/support/src/stdio/stdio_impl.h b/sources/android/support/src/stdio/stdio_impl.h
new file mode 100644
index 0000000..51ce59c
--- /dev/null
+++ b/sources/android/support/src/stdio/stdio_impl.h
@@ -0,0 +1,63 @@
+// The Musl sources for snprintf(), sscanf() assume they can use a specially
+// crafted FILE object to represent the output/input buffers. However, this
+// doesn't work when using FILE handle from Bionic.
+//
+// This header is used to 'cheat' by redefining FILE and a few other macro
+// redefinitions for functions used in the sources in this directory.
+
+#ifndef STDIO_IMPL_H
+#define STDIO_IMPL_H
+
+#define __HIDDEN__ __attribute__((__visibility__("hidden")))
+
+// A structure that wraps either a real FILE* handle, or an input/output
+// buffer.
+typedef struct {
+ FILE* file;
+ unsigned char* buffer;
+ size_t buffer_size;
+ size_t buffer_pos;
+} FakeFILE;
+
+// Initialize FakeFILE wrapper |file| to use a FILE* handle |f|
+void fake_file_init_file(FakeFILE* file, FILE* f) __HIDDEN__;
+
+// Initialize FakeFILE wrapper |file| to use a |buffer| of |buffer_size| chars.
+void fake_file_init_buffer(FakeFILE* file, char* buffer, size_t buffer_size)
+ __HIDDEN__;
+
+// Initialize FakeFILE wrapper |file| to use a wchar_t |buffer| of
+// |buffer_size| wide-chars.
+void fake_file_init_wbuffer(FakeFILE* file, wchar_t* buffer, size_t buffer_size)
+ __HIDDEN__;
+
+// Replacement for out() in vfprintf.c
+void fake_file_out(FakeFILE* file, const char* s, size_t l) __HIDDEN__;
+
+// Replacement for out() in fvwprintf.c
+void fake_file_outw(FakeFILE* file, const wchar_t* s, size_t l) __HIDDEN__;
+
+// Fake replacement for stdio functions of similar names.
+int fake_feof(FakeFILE* file) __HIDDEN__;
+int fake_ferror(FakeFILE* file) __HIDDEN__;
+int fake_fprintf(FakeFILE* file, const char* fmt, ...) __HIDDEN__;
+void fake_fputc(char ch, FakeFILE* file) __HIDDEN__;
+void fake_fputwc(wchar_t wc, FakeFILE* file) __HIDDEN__;
+
+#ifndef _STDIO_IMPL_NO_REDIRECT_MACROS
+
+// Macro redirection - ugly but necessary to minimize changes to the sources.
+#define FILE FakeFILE
+
+#undef feof
+#define feof fake_feof
+
+#undef ferror
+#define ferror fake_ferror
+#define fprintf fake_fprintf
+#define fputc fake_fputc
+#define fputwc fake_fputwc
+
+#endif /* _STDIO_IMPL_NO_REDIRECT_MACROS */
+
+#endif /* STDIO_IMPL_H */
diff --git a/sources/android/support/src/stdio/vfprintf.c b/sources/android/support/src/stdio/vfprintf.c
new file mode 100644
index 0000000..5789acf
--- /dev/null
+++ b/sources/android/support/src/stdio/vfprintf.c
@@ -0,0 +1,780 @@
+/*
+ Copyright (C) 2005-2012 Rich Felker
+
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+ Modified in 2013 for the Android Open Source Project.
+ */
+#include <errno.h>
+#include <ctype.h>
+#include <limits.h>
+#include <string.h>
+#include <stdarg.h>
+#include <wchar.h>
+#include <inttypes.h>
+#include <math.h>
+#include <float.h>
+
+#include "stdio_impl.h"
+
+/* Some useful macros */
+
+#define MAX(a,b) ((a)>(b) ? (a) : (b))
+#define MIN(a,b) ((a)<(b) ? (a) : (b))
+#define CONCAT2(x,y) x ## y
+#define CONCAT(x,y) CONCAT2(x,y)
+
+/* Convenient bit representation for modifier flags, which all fall
+ * within 31 codepoints of the space character. */
+
+#define ALT_FORM (1U<<'#'-' ')
+#define ZERO_PAD (1U<<'0'-' ')
+#define LEFT_ADJ (1U<<'-'-' ')
+#define PAD_POS (1U<<' '-' ')
+#define MARK_POS (1U<<'+'-' ')
+#define GROUPED (1U<<'\''-' ')
+
+#define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
+
+#if UINT_MAX == ULONG_MAX
+#define LONG_IS_INT
+#endif
+
+#if SIZE_MAX != ULONG_MAX || UINTMAX_MAX != ULLONG_MAX
+#define ODD_TYPES
+#endif
+
+/* State machine to accept length modifiers + conversion specifiers.
+ * Result is 0 on failure, or an argument type to pop on success. */
+
+enum {
+ BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE,
+ ZTPRE, JPRE,
+ STOP,
+ PTR, INT, UINT, ULLONG,
+#ifndef LONG_IS_INT
+ LONG, ULONG,
+#else
+#define LONG INT
+#define ULONG UINT
+#endif
+ SHORT, USHORT, CHAR, UCHAR,
+#ifdef ODD_TYPES
+ LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
+#else
+#define LLONG ULLONG
+#define SIZET ULONG
+#define IMAX LLONG
+#define UMAX ULLONG
+#define PDIFF LONG
+#define UIPTR ULONG
+#endif
+ DBL, LDBL,
+ NOARG,
+ MAXSTATE
+};
+
+#define S(x) [(x)-'A']
+
+static const unsigned char states[]['z'-'A'+1] = {
+ { /* 0: bare types */
+ S('d') = INT, S('i') = INT,
+ S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
+ S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
+ S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
+ S('c') = CHAR, S('C') = INT,
+ S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
+ S('m') = NOARG,
+ S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
+ S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE,
+ }, { /* 1: l-prefixed */
+ S('d') = LONG, S('i') = LONG,
+ S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
+ S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
+ S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
+ S('c') = INT, S('s') = PTR, S('n') = PTR,
+ S('l') = LLPRE,
+ }, { /* 2: ll-prefixed */
+ S('d') = LLONG, S('i') = LLONG,
+ S('o') = ULLONG, S('u') = ULLONG,
+ S('x') = ULLONG, S('X') = ULLONG,
+ S('n') = PTR,
+ }, { /* 3: h-prefixed */
+ S('d') = SHORT, S('i') = SHORT,
+ S('o') = USHORT, S('u') = USHORT,
+ S('x') = USHORT, S('X') = USHORT,
+ S('n') = PTR,
+ S('h') = HHPRE,
+ }, { /* 4: hh-prefixed */
+ S('d') = CHAR, S('i') = CHAR,
+ S('o') = UCHAR, S('u') = UCHAR,
+ S('x') = UCHAR, S('X') = UCHAR,
+ S('n') = PTR,
+ }, { /* 5: L-prefixed */
+ S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL,
+ S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL,
+ S('n') = PTR,
+ }, { /* 6: z- or t-prefixed (assumed to be same size) */
+ S('d') = PDIFF, S('i') = PDIFF,
+ S('o') = SIZET, S('u') = SIZET,
+ S('x') = SIZET, S('X') = SIZET,
+ S('n') = PTR,
+ }, { /* 7: j-prefixed */
+ S('d') = IMAX, S('i') = IMAX,
+ S('o') = UMAX, S('u') = UMAX,
+ S('x') = UMAX, S('X') = UMAX,
+ S('n') = PTR,
+ }
+};
+
+#define OOB(x) ((unsigned)(x)-'A' > 'z'-'A')
+
+union arg
+{
+ uintmax_t i;
+ long double f;
+ void *p;
+};
+
+static void pop_arg(union arg *arg, int type, va_list *ap)
+{
+ /* Give the compiler a hint for optimizing the switch. */
+ if ((unsigned)type > MAXSTATE) return;
+ switch (type) {
+ case PTR: arg->p = va_arg(*ap, void *);
+ break; case INT: arg->i = va_arg(*ap, int);
+ break; case UINT: arg->i = va_arg(*ap, unsigned int);
+#ifndef LONG_IS_INT
+ break; case LONG: arg->i = va_arg(*ap, long);
+ break; case ULONG: arg->i = va_arg(*ap, unsigned long);
+#endif
+ break; case ULLONG: arg->i = va_arg(*ap, unsigned long long);
+ break; case SHORT: arg->i = (short)va_arg(*ap, int);
+ break; case USHORT: arg->i = (unsigned short)va_arg(*ap, int);
+ break; case CHAR: arg->i = (signed char)va_arg(*ap, int);
+ break; case UCHAR: arg->i = (unsigned char)va_arg(*ap, int);
+#ifdef ODD_TYPES
+ break; case LLONG: arg->i = va_arg(*ap, long long);
+ break; case SIZET: arg->i = va_arg(*ap, size_t);
+ break; case IMAX: arg->i = va_arg(*ap, intmax_t);
+ break; case UMAX: arg->i = va_arg(*ap, uintmax_t);
+ break; case PDIFF: arg->i = va_arg(*ap, ptrdiff_t);
+ break; case UIPTR: arg->i = (uintptr_t)va_arg(*ap, void *);
+#endif
+ break; case DBL: arg->f = va_arg(*ap, double);
+ break; case LDBL: arg->f = va_arg(*ap, long double);
+ }
+}
+
+static void out(FILE *f, const char *s, size_t l)
+{
+#if defined(__ANDROID__)
+ fake_file_out(f, s, l);
+#else
+ __fwritex((void *)s, l, f);
+#endif
+}
+
+static void pad(FILE *f, char c, int w, int l, int fl)
+{
+ char pad[256];
+ if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return;
+ l = w - l;
+ memset(pad, c, l>sizeof pad ? sizeof pad : l);
+ for (; l >= sizeof pad; l -= sizeof pad)
+ out(f, pad, sizeof pad);
+ out(f, pad, l);
+}
+
+static const char xdigits[16] = {
+ "0123456789ABCDEF"
+};
+
+static char *fmt_x(uintmax_t x, char *s, int lower)
+{
+ for (; x; x>>=4) *--s = xdigits[(x&15)]|lower;
+ return s;
+}
+
+static char *fmt_o(uintmax_t x, char *s)
+{
+ for (; x; x>>=3) *--s = '0' + (x&7);
+ return s;
+}
+
+static char *fmt_u(uintmax_t x, char *s)
+{
+ unsigned long y;
+ for ( ; x>ULONG_MAX; x/=10) *--s = '0' + x%10;
+ for (y=x; y; y/=10) *--s = '0' + y%10;
+ return s;
+}
+
+static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
+{
+ uint32_t big[(LDBL_MAX_EXP+LDBL_MANT_DIG)/9+1];
+ uint32_t *a, *d, *r, *z;
+ int e2=0, e, i, j, l;
+ char buf[9+LDBL_MANT_DIG/4], *s;
+ const char *prefix="-0X+0X 0X-0x+0x 0x";
+ int pl;
+ char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr;
+
+ pl=1;
+ if (signbit(y)) {
+ y=-y;
+ } else if (fl & MARK_POS) {
+ prefix+=3;
+ } else if (fl & PAD_POS) {
+ prefix+=6;
+ } else prefix++, pl=0;
+
+ if (!isfinite(y)) {
+ char *s = (t&32)?"inf":"INF";
+ if (y!=y) s=(t&32)?"nan":"NAN", pl=0;
+ pad(f, ' ', w, 3+pl, fl&~ZERO_PAD);
+ out(f, prefix, pl);
+ out(f, s, 3);
+ pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);
+ return MAX(w, 3+pl);
+ }
+
+ y = frexpl(y, &e2) * 2;
+ if (y) e2--;
+
+ if ((t|32)=='a') {
+ long double round = 8.0;
+ int re;
+
+ if (t&32) prefix += 9;
+ pl += 2;
+
+ if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
+ else re=LDBL_MANT_DIG/4-1-p;
+
+ if (re) {
+ while (re--) round*=16;
+ if (*prefix=='-') {
+ y=-y;
+ y-=round;
+ y+=round;
+ y=-y;
+ } else {
+ y+=round;
+ y-=round;
+ }
+ }
+
+ estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
+ if (estr==ebuf) *--estr='0';
+ *--estr = (e2<0 ? '-' : '+');
+ *--estr = t+('p'-'a');
+
+ s=buf;
+ do {
+ int x=y;
+ *s++=xdigits[x]|(t&32);
+ y=16*(y-x);
+ if (s-buf==1 && (y||p>0||(fl&ALT_FORM))) *s++='.';
+ } while (y);
+
+ if (p && s-buf-2 < p)
+ l = (p+2) + (ebuf-estr);
+ else
+ l = (s-buf) + (ebuf-estr);
+
+ pad(f, ' ', w, pl+l, fl);
+ out(f, prefix, pl);
+ pad(f, '0', w, pl+l, fl^ZERO_PAD);
+ out(f, buf, s-buf);
+ pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
+ out(f, estr, ebuf-estr);
+ pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
+ return MAX(w, pl+l);
+ }
+ if (p<0) p=6;
+
+ if (y) y *= 0x1p28, e2-=28;
+
+ if (e2<0) a=r=z=big;
+ else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
+
+ do {
+ *z = y;
+ y = 1000000000*(y-*z++);
+ } while (y);
+
+ while (e2>0) {
+ uint32_t carry=0;
+ int sh=MIN(29,e2);
+ for (d=z-1; d>=a; d--) {
+ uint64_t x = ((uint64_t)*d<<sh)+carry;
+ *d = x % 1000000000;
+ carry = x / 1000000000;
+ }
+ if (!z[-1] && z>a) z--;
+ if (carry) *--a = carry;
+ e2-=sh;
+ }
+ while (e2<0) {
+ uint32_t carry=0, *b;
+ int sh=MIN(9,-e2);
+ for (d=a; d<z; d++) {
+ uint32_t rm = *d & (1<<sh)-1;
+ *d = (*d>>sh) + carry;
+ carry = (1000000000>>sh) * rm;
+ }
+ if (!*a) a++;
+ if (carry) *z++ = carry;
+ /* Avoid (slow!) computation past requested precision */
+ b = (t|32)=='f' ? r : a;
+ if (z-b > 2+p/9) z = b+2+p/9;
+ e2+=sh;
+ }
+
+ if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
+ else e=0;
+
+ /* Perform rounding: j is precision after the radix (possibly neg) */
+ j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
+ if (j < 9*(z-r-1)) {
+ uint32_t x;
+ /* We avoid C's broken division of negative numbers */
+ d = r + 1 + ((j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP);
+ j += 9*LDBL_MAX_EXP;
+ j %= 9;
+ for (i=10, j++; j<9; i*=10, j++);
+ x = *d % i;
+ /* Are there any significant digits past j? */
+ if (x || d+1!=z) {
+ long double round = CONCAT(0x1p,LDBL_MANT_DIG);
+ long double small;
+ if (*d/i & 1) round += 2;
+ if (x<i/2) small=0x0.8p0;
+ else if (x==i/2 && d+1==z) small=0x1.0p0;
+ else small=0x1.8p0;
+ if (pl && *prefix=='-') round*=-1, small*=-1;
+ *d -= x;
+ /* Decide whether to round by probing round+small */
+ if (round+small != round) {
+ *d = *d + i;
+ while (*d > 999999999) {
+ *d--=0;
+ (*d)++;
+ }
+ if (d<a) a=d;
+ for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
+ }
+ }
+ if (z>d+1) z=d+1;
+ for (; !z[-1] && z>a; z--);
+ }
+
+ if ((t|32)=='g') {
+ if (!p) p++;
+ if (p>e && e>=-4) {
+ t--;
+ p-=e+1;
+ } else {
+ t-=2;
+ p--;
+ }
+ if (!(fl&ALT_FORM)) {
+ /* Count trailing zeros in last place */
+ if (z>a && z[-1]) for (i=10, j=0; z[-1]%i==0; i*=10, j++);
+ else j=9;
+ if ((t|32)=='f')
+ p = MIN(p,MAX(0,9*(z-r-1)-j));
+ else
+ p = MIN(p,MAX(0,9*(z-r-1)+e-j));
+ }
+ }
+ l = 1 + p + (p || (fl&ALT_FORM));
+ if ((t|32)=='f') {
+ if (e>0) l+=e;
+ } else {
+ estr=fmt_u(e<0 ? -e : e, ebuf);
+ while(ebuf-estr<2) *--estr='0';
+ *--estr = (e<0 ? '-' : '+');
+ *--estr = t;
+ l += ebuf-estr;
+ }
+
+ pad(f, ' ', w, pl+l, fl);
+ out(f, prefix, pl);
+ pad(f, '0', w, pl+l, fl^ZERO_PAD);
+
+ if ((t|32)=='f') {
+ if (a>r) a=r;
+ for (d=a; d<=r; d++) {
+ char *s = fmt_u(*d, buf+9);
+ if (d!=a) while (s>buf) *--s='0';
+ else if (s==buf+9) *--s='0';
+ out(f, s, buf+9-s);
+ }
+ if (p || (fl&ALT_FORM)) out(f, ".", 1);
+ for (; d<z && p>0; d++, p-=9) {
+ char *s = fmt_u(*d, buf+9);
+ while (s>buf) *--s='0';
+ out(f, s, MIN(9,p));
+ }
+ pad(f, '0', p+9, 9, 0);
+ } else {
+ if (z<=a) z=a+1;
+ for (d=a; d<z && p>=0; d++) {
+ char *s = fmt_u(*d, buf+9);
+ if (s==buf+9) *--s='0';
+ if (d!=a) while (s>buf) *--s='0';
+ else {
+ out(f, s++, 1);
+ if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
+ }
+ out(f, s, MIN(buf+9-s, p));
+ p -= buf+9-s;
+ }
+ pad(f, '0', p+18, 18, 0);
+ out(f, estr, ebuf-estr);
+ }
+
+ pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
+
+ return MAX(w, pl+l);
+}
+
+static int getint(char **s) {
+ int i;
+ for (i=0; isdigit(**s); (*s)++)
+ i = 10*i + (**s-'0');
+ return i;
+}
+
+static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
+{
+ char *a, *z, *s=(char *)fmt;
+ unsigned l10n=0, fl;
+ int w, p;
+ union arg arg;
+ int argpos;
+ unsigned st, ps;
+ int cnt=0, l=0;
+ int i;
+ char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
+ const char *prefix;
+ int t, pl;
+ wchar_t wc[2], *ws;
+ char mb[4];
+
+ for (;;) {
+ /* Update output count, end loop when fmt is exhausted */
+ if (cnt >= 0) {
+ if (l > INT_MAX - cnt) {
+ errno = EOVERFLOW;
+ cnt = -1;
+ } else cnt += l;
+ }
+ if (!*s) break;
+
+ /* Handle literal text and %% format specifiers */
+ for (a=s; *s && *s!='%'; s++);
+ for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2);
+ l = z-a;
+ if (f) out(f, a, l);
+ if (l) continue;
+
+ if (isdigit(s[1]) && s[2]=='$') {
+ l10n=1;
+ argpos = s[1]-'0';
+ s+=3;
+ } else {
+ argpos = -1;
+ s++;
+ }
+
+ /* Read modifier flags */
+ for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
+ fl |= 1U<<*s-' ';
+
+ /* Read field width */
+ if (*s=='*') {
+ if (isdigit(s[1]) && s[2]=='$') {
+ l10n=1;
+ nl_type[s[1]-'0'] = INT;
+ w = nl_arg[s[1]-'0'].i;
+ s+=3;
+ } else if (!l10n) {
+ w = f ? va_arg(*ap, int) : 0;
+ s++;
+ } else return -1;
+ if (w<0) fl|=LEFT_ADJ, w=-w;
+ } else if ((w=getint(&s))<0) return -1;
+
+ /* Read precision */
+ if (*s=='.' && s[1]=='*') {
+ if (isdigit(s[2]) && s[3]=='$') {
+ nl_type[s[2]-'0'] = INT;
+ p = nl_arg[s[2]-'0'].i;
+ s+=4;
+ } else if (!l10n) {
+ p = f ? va_arg(*ap, int) : 0;
+ s+=2;
+ } else return -1;
+ } else if (*s=='.') {
+ s++;
+ p = getint(&s);
+ } else p = -1;
+
+ /* Format specifier state machine */
+ st=0;
+ do {
+ if (OOB(*s)) return -1;
+ ps=st;
+ st=states[st]S(*s++);
+ } while (st-1<STOP);
+ if (!st) return -1;
+
+ /* Check validity of argument type (nl/normal) */
+ if (st==NOARG) {
+ if (argpos>=0) return -1;
+ else if (!f) continue;
+ } else {
+ if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
+ else if (f) pop_arg(&arg, st, ap);
+ else return 0;
+ }
+
+ if (!f) continue;
+
+ z = buf + sizeof(buf);
+ prefix = "-+ 0X0x";
+ pl = 0;
+ t = s[-1];
+
+ /* Transform ls,lc -> S,C */
+ if (ps && (t&15)==3) t&=~32;
+
+ /* - and 0 flags are mutually exclusive */
+ if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
+
+ switch(t) {
+ case 'n':
+#ifndef __ANDROID__ /* Disabled on Android for security reasons. */
+ switch(ps) {
+ case BARE: *(int *)arg.p = cnt; break;
+ case LPRE: *(long *)arg.p = cnt; break;
+ case LLPRE: *(long long *)arg.p = cnt; break;
+ case HPRE: *(unsigned short *)arg.p = cnt; break;
+ case HHPRE: *(unsigned char *)arg.p = cnt; break;
+ case ZTPRE: *(size_t *)arg.p = cnt; break;
+ case JPRE: *(uintmax_t *)arg.p = cnt; break;
+ }
+#endif /* !__ANDROID__ */
+ continue;
+ case 'p':
+ p = MAX(p, 2*sizeof(void*));
+ t = 'x';
+ fl |= ALT_FORM;
+ case 'x': case 'X':
+ a = fmt_x(arg.i, z, t&32);
+ if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2;
+ if (0) {
+ case 'o':
+ a = fmt_o(arg.i, z);
+ if ((fl&ALT_FORM) && arg.i) prefix+=5, pl=1;
+ } if (0) {
+ case 'd': case 'i':
+ pl=1;
+ if (arg.i>INTMAX_MAX) {
+ arg.i=-arg.i;
+ } else if (fl & MARK_POS) {
+ prefix++;
+ } else if (fl & PAD_POS) {
+ prefix+=2;
+ } else pl=0;
+ case 'u':
+ a = fmt_u(arg.i, z);
+ }
+ if (p>=0) fl &= ~ZERO_PAD;
+ if (!arg.i && !p) {
+ a=z;
+ break;
+ }
+ p = MAX(p, z-a + !arg.i);
+ break;
+ case 'c':
+ *(a=z-(p=1))=arg.i;
+ fl &= ~ZERO_PAD;
+ break;
+ case 'm':
+ if (1) a = strerror(errno); else
+ case 's':
+ a = arg.p ? arg.p : "(null)";
+#if defined(__ANDROID__)
+ /* On Android, memchr() will return NULL for
+ * out-of-bound requests, e.g. if |p == -1|. */
+ if (p >= 0) {
+ z = memchr(a, 0, p);
+ if (!z) z=a+p;
+ else p=z-a;
+ } else {
+ p=strlen(a);
+ z=a+p;
+ }
+#else /* !__ANDROID__ */
+ if (!z) z=a+p;
+ else p=z-a;
+#endif /* !__ANDROID__ */
+ fl &= ~ZERO_PAD;
+ break;
+ case 'C':
+ wc[0] = arg.i;
+ wc[1] = 0;
+ arg.p = wc;
+ p = -1;
+ case 'S':
+ ws = arg.p;
+ for (i=l=0; i<0U+p && *ws && (l=wctomb(mb, *ws++))>=0 && l<=0U+p-i; i+=l);
+ if (l<0) return -1;
+ p = i;
+ pad(f, ' ', w, p, fl);
+ ws = arg.p;
+ for (i=0; i<0U+p && *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
+ out(f, mb, l);
+ pad(f, ' ', w, p, fl^LEFT_ADJ);
+ l = w>p ? w : p;
+ continue;
+ case 'e': case 'f': case 'g': case 'a':
+ case 'E': case 'F': case 'G': case 'A':
+ l = fmt_fp(f, arg.f, w, p, fl, t);
+ continue;
+ }
+
+ if (p < z-a) p = z-a;
+ if (w < pl+p) w = pl+p;
+
+ pad(f, ' ', w, pl+p, fl);
+ out(f, prefix, pl);
+ pad(f, '0', w, pl+p, fl^ZERO_PAD);
+ pad(f, '0', p, z-a, 0);
+ out(f, a, z-a);
+ pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
+
+ l = w;
+ }
+
+ if (f) return cnt;
+ if (!l10n) return 0;
+
+ for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
+ pop_arg(nl_arg+i, nl_type[i], ap);
+ for (; i<=NL_ARGMAX && !nl_type[i]; i++);
+ if (i<=NL_ARGMAX) return -1;
+ return 1;
+}
+
+#ifdef __ANDROID__
+#undef FILE /* no longer needed */
+
+int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
+{
+ va_list ap2;
+ int nl_type[NL_ARGMAX+1] = {0};
+ union arg nl_arg[NL_ARGMAX+1];
+ int ret;
+ FakeFILE out[1];
+ fake_file_init_file(out, f);
+
+ va_copy(ap2, ap);
+ ret = printf_core(0, fmt, &ap2, nl_arg, nl_type);
+ va_end(ap2);
+ if (ret < 0)
+ return -1;
+
+ va_copy(ap2, ap);
+ ret = printf_core(out, fmt, &ap2, nl_arg, nl_type);
+ va_end(ap2);
+ return ret;
+}
+
+int vsnprintf(char *restrict s, size_t n, const char *restrict fmt, va_list ap)
+{
+ va_list ap2;
+ int nl_type[NL_ARGMAX+1] = {0};
+ union arg nl_arg[NL_ARGMAX+1];
+ int r;
+ char b;
+ FakeFILE out[1];
+
+ if (n-1 > INT_MAX-1) {
+ if (n) {
+ errno = EOVERFLOW;
+ return -1;
+ }
+ s = &b;
+ n = 1;
+ }
+
+ /* Ensure pointers don't wrap if "infinite" n is passed in */
+ if (n > (char *)0+SIZE_MAX-s-1) n = (char *)0+SIZE_MAX-s-1;
+ fake_file_init_buffer(out, s, n);
+
+ va_copy(ap2, ap);
+ r = printf_core(out, fmt, &ap2, nl_arg, nl_type);
+ va_end(ap2);
+
+ if (r < n)
+ s[r] = '\0';
+ else
+ s[n - 1] = '\0';
+
+ return r;
+}
+
+#else /* !__ANDROID__ */
+int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
+{
+ va_list ap2;
+ int nl_type[NL_ARGMAX+1] = {0};
+ union arg nl_arg[NL_ARGMAX+1];
+ unsigned char internal_buf[80], *saved_buf = 0;
+ int ret;
+
+ va_copy(ap2, ap);
+ if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) return -1;
+
+ FLOCK(f);
+ if (!f->buf_size) {
+ saved_buf = f->buf;
+ f->wpos = f->wbase = f->buf = internal_buf;
+ f->buf_size = sizeof internal_buf;
+ f->wend = internal_buf + sizeof internal_buf;
+ }
+ ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
+ if (saved_buf) {
+ f->write(f, 0, 0);
+ if (!f->wpos) ret = -1;
+ f->buf = saved_buf;
+ f->buf_size = 0;
+ f->wpos = f->wbase = f->wend = 0;
+ }
+ FUNLOCK(f);
+ va_end(ap2);
+ return ret;
+}
+#endif /* !__ANDROID__ */
diff --git a/sources/android/support/src/stdio/vfwprintf.c b/sources/android/support/src/stdio/vfwprintf.c
index 56e5c48..4bf79d6 100644
--- a/sources/android/support/src/stdio/vfwprintf.c
+++ b/sources/android/support/src/stdio/vfwprintf.c
@@ -23,7 +23,6 @@
Modified in 2013 for the Android Open Source Project.
*/
-//#include "stdio_impl.h"
#include <errno.h>
#include <ctype.h>
#include <limits.h>
@@ -32,95 +31,7 @@
#include <wchar.h>
#include <inttypes.h>
-// A wrapper around either a FILE* or a string buffer, used to output
-// the result of formatted expansion.
-typedef struct {
- FILE* file;
- wchar_t* buffer;
- size_t buffer_pos;
- size_t buffer_size;
-} Out;
-
-void out_init_file(Out* out, FILE* f) {
- memset(out, 0, sizeof(*out));
- out->file = f;
-}
-
-void out_init_buffer(Out* out, wchar_t* buffer, size_t buffer_size) {
- memset(out, 0, sizeof(*out));
- out->buffer = buffer;
- out->buffer_pos = 0;
- out->buffer_size = buffer_size;
-}
-
-void out_write(Out* out, const wchar_t* text, size_t length) {
- if (out->file != NULL) {
- // Write into a file the UTF-8 encoded version.
- // TODO(digit): Support locale-specific encoding.
- size_t mb_len = wcstombs(NULL, text, length);
- char* mb_buffer = malloc(mb_len);
- wcstombs(mb_buffer, text, length);
- fwrite(mb_buffer, 1, mb_len, out->file);
- free(mb_buffer);
- } else {
- // Write into a bounded buffer.
- size_t avail = out->buffer_size - out->buffer_pos;
- if (length > avail)
- length = avail;
- memcpy((char*)(out->buffer + out->buffer_pos),
- (const char*)text,
- length * sizeof(wchar_t));
- out->buffer_pos += length;
- }
-}
-
-void out_putwc(Out* out, wchar_t wc) {
- if (out->file)
- fputwc(wc, out->file);
- else {
- if (out->buffer_pos < out->buffer_size)
- out->buffer[out->buffer_pos++] = wc;
- }
-}
-
-int out_printf(Out* out, const char* format, ...) {
- va_list args;
- va_start(args, format);
- if (out->file)
- return vfprintf(out->file, format, args);
- else {
- // TODO(digit): Make this faster.
- // First, generate formatted byte output.
- int mb_len = vsnprintf(NULL, 0, format, args);
- char* mb_buffer = malloc(mb_len + 1);
- vsnprintf(mb_buffer, mb_len + 1, format, args);
- // Then convert to wchar_t buffer.
- size_t wide_len = mbstowcs(NULL, mb_buffer, mb_len);
- wchar_t* wide_buffer = malloc((wide_len + 1) * sizeof(wchar_t));
- mbstowcs(wide_buffer, mb_buffer, mb_len);
- // Add to buffer.
- out_write(out, wide_buffer, wide_len);
- // finished
- free(wide_buffer);
- free(mb_buffer);
-
- return wide_len;
- }
- va_end(args);
-}
-int out_error(Out* out) {
- if (out->file != NULL)
- return ferror(out->file);
-
- return 0;
-}
-
-int out_overflow(Out* out) {
- if (out->file != NULL)
- return feof(out->file);
- else
- return (out->buffer_pos >= out->buffer_size);
-}
+#include "stdio_impl.h"
/* Convenient bit representation for modifier flags, which all fall
* within 31 codepoints of the space character. */
@@ -262,6 +173,15 @@
}
}
+static void out(FILE *f, const wchar_t *s, size_t l)
+{
+#if defined(__ANDROID__)
+ fake_file_outw(f, s, l);
+#else
+ while (l--) fputwc(*s++, f);
+#endif
+}
+
static int getint(wchar_t **s) {
int i;
for (i=0; iswdigit(**s); (*s)++)
@@ -275,7 +195,7 @@
['p'-'a']='j'
};
-static int wprintf_core(Out *out, const wchar_t *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
+static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
{
wchar_t *a, *z, *s=(wchar_t *)fmt, *s0;
unsigned l10n=0, litpct, fl;
@@ -294,7 +214,7 @@
/* Update output count, end loop when fmt is exhausted */
if (cnt >= 0) {
if (l > INT_MAX - cnt) {
- if (!out_error(out)) errno = EOVERFLOW;
+ if (!ferror(f)) errno = EOVERFLOW;
cnt = -1;
} else cnt += l;
}
@@ -306,7 +226,7 @@
z = s+litpct;
s += 2*litpct;
l = z-a;
- if (out) out_write(out, a, l);
+ if (f) out(f, a, l);
if (l) continue;
if (iswdigit(s[1]) && s[2]=='$') {
@@ -330,7 +250,7 @@
w = nl_arg[s[1]-'0'].i;
s+=3;
} else if (!l10n) {
- w = out ? va_arg(*ap, int) : 0;
+ w = f ? va_arg(*ap, int) : 0;
s++;
} else return -1;
if (w<0) fl|=LEFT_ADJ, w=-w;
@@ -343,7 +263,7 @@
p = nl_arg[s[2]-'0'].i;
s+=4;
} else if (!l10n) {
- p = out ? va_arg(*ap, int) : 0;
+ p = f ? va_arg(*ap, int) : 0;
s+=2;
} else return -1;
} else if (*s=='.') {
@@ -364,19 +284,20 @@
/* Check validity of argument type (nl/normal) */
if (st==NOARG) {
if (argpos>=0) return -1;
- else if (!out) continue;
+ else if (!f) continue;
} else {
if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
- else if (out) pop_arg(&arg, st, ap);
+ else if (f) pop_arg(&arg, st, ap);
else return 0;
}
- if (!out) continue;
+ if (!f) continue;
t = s[-1];
if (ps && (t&15)==3) t&=~32;
switch (t) {
case 'n':
+#ifndef __ANDROID__ /* Disabled on Android for security reasons. */
switch(ps) {
case BARE: *(int *)arg.p = cnt; break;
case LPRE: *(long *)arg.p = cnt; break;
@@ -386,13 +307,14 @@
case ZTPRE: *(size_t *)arg.p = cnt; break;
case JPRE: *(uintmax_t *)arg.p = cnt; break;
}
+#endif /* !__ANDROID__ */
continue;
case 'c':
- out_putwc(out, btowc(arg.i));
+ fputwc(btowc(arg.i), f);
l = 1;
continue;
case 'C':
- out_putwc(out, arg.i);
+ fputwc(arg.i, f);
l = 1;
continue;
case 'S':
@@ -401,9 +323,9 @@
if (!z) z=a+p;
else p=z-a;
if (w<p) w=p;
- if (!(fl&LEFT_ADJ)) out_printf(out, "%.*s", w-p, "");
- out_write(out, a, p);
- if ((fl&LEFT_ADJ)) out_printf(out, "%.*s", w-p, "");
+ if (!(fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
+ out(f, a, p);
+ if ((fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
l=w;
continue;
case 's':
@@ -413,14 +335,14 @@
if (i<0) return -1;
p=l;
if (w<p) w=p;
- if (!(fl&LEFT_ADJ)) out_printf(out, "%.*s", w-p, "");
+ if (!(fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
bs = arg.p;
while (l--) {
i=mbtowc(&wc, bs, MB_LEN_MAX);
bs+=i;
- out_putwc(out, wc);
+ fputwc(wc, f);
}
- if ((fl&LEFT_ADJ)) out_printf(out, "%.*s", w-p, "");
+ if ((fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
l=w;
continue;
}
@@ -435,15 +357,15 @@
switch (t|32) {
case 'a': case 'e': case 'f': case 'g':
- l = out_printf(out, charfmt, w, p, arg.f);
+ l = fprintf(f, charfmt, w, p, arg.f);
break;
case 'd': case 'i': case 'o': case 'u': case 'x': case 'p':
- l = out_printf(out, charfmt, w, p, arg.i);
+ l = fprintf(f, charfmt, w, p, arg.i);
break;
}
}
- if (out) return cnt;
+ if (f) return cnt;
if (!l10n) return 0;
for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
@@ -453,14 +375,16 @@
return 1;
}
+#ifdef __ANDROID__
+#undef FILE /* no longer needed */
int vfwprintf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
{
va_list ap2;
int nl_type[NL_ARGMAX] = {0};
union arg nl_arg[NL_ARGMAX];
int ret;
- Out out[1];
- out_init_file(out, f);
+ FakeFILE out[1];
+ fake_file_init_file(out, f);
va_copy(ap2, ap);
// Check for error in format string before writing anything to file.
if (wprintf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) {
@@ -478,11 +402,29 @@
int nl_type[NL_ARGMAX] = {0};
union arg nl_arg[NL_ARGMAX];
int ret;
- Out out[1];
- out_init_buffer(out, s, l);
+ FakeFILE out[1];
+ fake_file_init_wbuffer(out, s, l);
va_copy(ap2, ap);
ret = wprintf_core(out, fmt, &ap2, nl_arg, nl_type);
va_end(ap2);
- if (out_overflow(out)) return -1;
+ if (fake_feof(out)) return -1;
return ret;
}
+#else /* !__ANDROID__ */
+int vfwprintf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
+{
+ va_list ap2;
+ int nl_type[NL_ARGMAX] = {0};
+ union arg nl_arg[NL_ARGMAX];
+ int ret;
+
+ va_copy(ap2, ap);
+ if (wprintf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) return -1;
+
+ FLOCK(f);
+ ret = wprintf_core(f, fmt, &ap2, nl_arg, nl_type);
+ FUNLOCK(f);
+ va_end(ap2);
+ return ret;
+}
+#endif /* !__ANDROID__ */
diff --git a/sources/android/support/tests/stdio_unittest.cc b/sources/android/support/tests/stdio_unittest.cc
index 08ace9a..fca503d 100644
--- a/sources/android/support/tests/stdio_unittest.cc
+++ b/sources/android/support/tests/stdio_unittest.cc
@@ -16,6 +16,9 @@
EXPECT_EQ(L'\0', char_buff[11]);
EXPECT_EQ(12, snprintf(char_buff, 1, "%s", kString));
EXPECT_EQ(L'\0', char_buff[0]);
+
+ EXPECT_EQ(20, snprintf(char_buff, char_buff_len, "%a", 3.1415926535));
+ EXPECT_STREQ("0x1.921fb54411744p+1", char_buff);
}
TEST(stdio,swprintf) {