1
/* vsprintf with automatic memory allocation.
2
Copyright (C) 1999, 2002-2005 Free Software Foundation, Inc.
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2, or (at your option)
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License along
15
with this program; if not, write to the Free Software Foundation,
16
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
/* Tell glibc's <stdio.h> to provide a prototype for snprintf().
19
This must come before <config.h> because <config.h> may include
20
<features.h>, and once <features.h> has been included, it's too late. */
22
# define _GNU_SOURCE 1
34
# include "vasnwprintf.h"
36
# include "vasnprintf.h"
39
#include <stdio.h> /* snprintf(), sprintf() */
40
#include <stdlib.h> /* abort(), malloc(), realloc(), free() */
41
#include <string.h> /* memcpy(), strlen() */
42
#include <errno.h> /* errno */
43
#include <limits.h> /* CHAR_BIT, INT_MAX */
44
#include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
46
# include "wprintf-parse.h"
48
# include "printf-parse.h"
51
/* Checked size_t computations. */
54
/* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
56
# define EOVERFLOW E2BIG
61
# define local_wcslen wcslen
63
/* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
64
a dependency towards this library, here is a local substitute.
65
Define this substitute only once, even if this file is included
66
twice in the same compilation unit. */
67
# ifndef local_wcslen_defined
68
# define local_wcslen_defined 1
70
local_wcslen (const wchar_t *s)
74
for (ptr = s; *ptr != (wchar_t) 0; ptr++)
83
# define VASNPRINTF vasnwprintf
84
# define CHAR_T wchar_t
85
# define DIRECTIVE wchar_t_directive
86
# define DIRECTIVES wchar_t_directives
87
# define PRINTF_PARSE wprintf_parse
88
# define USE_SNPRINTF 1
89
# if HAVE_DECL__SNWPRINTF
90
/* On Windows, the function swprintf() has a different signature than
91
on Unix; we use the _snwprintf() function instead. */
92
# define SNPRINTF _snwprintf
95
# define SNPRINTF swprintf
98
# define VASNPRINTF vasnprintf
100
# define DIRECTIVE char_directive
101
# define DIRECTIVES char_directives
102
# define PRINTF_PARSE printf_parse
103
# define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
104
# if HAVE_DECL__SNPRINTF
106
# define SNPRINTF _snprintf
109
# define SNPRINTF snprintf
114
VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
119
if (PRINTF_PARSE (format, &d, &a) < 0)
130
if (printf_fetchargs (args, &a) < 0)
138
size_t buf_neededlength;
140
CHAR_T *buf_malloced;
144
/* Output string accumulator. */
149
/* Allocate a small buffer that will hold a directive passed to
150
sprintf or snprintf. */
152
xsum4 (7, d.max_width_length, d.max_precision_length, 6);
154
if (buf_neededlength < 4000 / sizeof (CHAR_T))
156
buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
162
size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
163
if (size_overflow_p (buf_memsize))
164
goto out_of_memory_1;
165
buf = (CHAR_T *) malloc (buf_memsize);
167
goto out_of_memory_1;
171
if (resultbuf != NULL)
174
allocated = *lengthp;
183
result is either == resultbuf or == NULL or malloc-allocated.
184
If length > 0, then result != NULL. */
186
/* Ensures that allocated >= needed. Aborts through a jump to
187
out_of_memory if needed is SIZE_MAX or otherwise too big. */
188
#define ENSURE_ALLOCATION(needed) \
189
if ((needed) > allocated) \
191
size_t memory_size; \
194
allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
195
if ((needed) > allocated) \
196
allocated = (needed); \
197
memory_size = xtimes (allocated, sizeof (CHAR_T)); \
198
if (size_overflow_p (memory_size)) \
199
goto out_of_memory; \
200
if (result == resultbuf || result == NULL) \
201
memory = (CHAR_T *) malloc (memory_size); \
203
memory = (CHAR_T *) realloc (result, memory_size); \
204
if (memory == NULL) \
205
goto out_of_memory; \
206
if (result == resultbuf && length > 0) \
207
memcpy (memory, result, length * sizeof (CHAR_T)); \
211
for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
213
if (cp != dp->dir_start)
215
size_t n = dp->dir_start - cp;
216
size_t augmented_length = xsum (length, n);
218
ENSURE_ALLOCATION (augmented_length);
219
memcpy (result + length, cp, n * sizeof (CHAR_T));
220
length = augmented_length;
225
/* Execute a single directive. */
226
if (dp->conversion == '%')
228
size_t augmented_length;
230
if (!(dp->arg_index == ARG_NONE))
232
augmented_length = xsum (length, 1);
233
ENSURE_ALLOCATION (augmented_length);
234
result[length] = '%';
235
length = augmented_length;
239
if (!(dp->arg_index != ARG_NONE))
242
if (dp->conversion == 'n')
244
switch (a.arg[dp->arg_index].type)
246
case TYPE_COUNT_SCHAR_POINTER:
247
*a.arg[dp->arg_index].a.a_count_schar_pointer = length;
249
case TYPE_COUNT_SHORT_POINTER:
250
*a.arg[dp->arg_index].a.a_count_short_pointer = length;
252
case TYPE_COUNT_INT_POINTER:
253
*a.arg[dp->arg_index].a.a_count_int_pointer = length;
255
case TYPE_COUNT_LONGINT_POINTER:
256
*a.arg[dp->arg_index].a.a_count_longint_pointer = length;
258
#ifdef HAVE_LONG_LONG
259
case TYPE_COUNT_LONGLONGINT_POINTER:
260
*a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
269
arg_type type = a.arg[dp->arg_index].type;
271
unsigned int prefix_count;
278
/* Allocate a temporary buffer of sufficient size for calling
285
if (dp->width_start != dp->width_end)
287
if (dp->width_arg_index != ARG_NONE)
291
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
293
arg = a.arg[dp->width_arg_index].a.a_int;
294
width = (arg < 0 ? (unsigned int) (-arg) : arg);
298
const CHAR_T *digitp = dp->width_start;
301
width = xsum (xtimes (width, 10), *digitp++ - '0');
302
while (digitp != dp->width_end);
307
if (dp->precision_start != dp->precision_end)
309
if (dp->precision_arg_index != ARG_NONE)
313
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
315
arg = a.arg[dp->precision_arg_index].a.a_int;
316
precision = (arg < 0 ? 0 : arg);
320
const CHAR_T *digitp = dp->precision_start + 1;
323
while (digitp != dp->precision_end)
324
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
328
switch (dp->conversion)
331
case 'd': case 'i': case 'u':
332
# ifdef HAVE_LONG_LONG
333
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
335
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
336
* 0.30103 /* binary -> decimal */
337
* 2 /* estimate for FLAG_GROUP */
339
+ 1 /* turn floor into ceil */
340
+ 1; /* account for leading sign */
343
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
345
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
346
* 0.30103 /* binary -> decimal */
347
* 2 /* estimate for FLAG_GROUP */
349
+ 1 /* turn floor into ceil */
350
+ 1; /* account for leading sign */
353
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
354
* 0.30103 /* binary -> decimal */
355
* 2 /* estimate for FLAG_GROUP */
357
+ 1 /* turn floor into ceil */
358
+ 1; /* account for leading sign */
362
# ifdef HAVE_LONG_LONG
363
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
365
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
366
* 0.333334 /* binary -> octal */
368
+ 1 /* turn floor into ceil */
369
+ 1; /* account for leading sign */
372
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
374
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
375
* 0.333334 /* binary -> octal */
377
+ 1 /* turn floor into ceil */
378
+ 1; /* account for leading sign */
381
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
382
* 0.333334 /* binary -> octal */
384
+ 1 /* turn floor into ceil */
385
+ 1; /* account for leading sign */
389
# ifdef HAVE_LONG_LONG
390
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
392
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
393
* 0.25 /* binary -> hexadecimal */
395
+ 1 /* turn floor into ceil */
396
+ 2; /* account for leading sign or alternate form */
399
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
401
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
402
* 0.25 /* binary -> hexadecimal */
404
+ 1 /* turn floor into ceil */
405
+ 2; /* account for leading sign or alternate form */
408
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
409
* 0.25 /* binary -> hexadecimal */
411
+ 1 /* turn floor into ceil */
412
+ 2; /* account for leading sign or alternate form */
416
# ifdef HAVE_LONG_DOUBLE
417
if (type == TYPE_LONGDOUBLE)
419
(unsigned int) (LDBL_MAX_EXP
420
* 0.30103 /* binary -> decimal */
421
* 2 /* estimate for FLAG_GROUP */
423
+ 1 /* turn floor into ceil */
424
+ 10; /* sign, decimal point etc. */
428
(unsigned int) (DBL_MAX_EXP
429
* 0.30103 /* binary -> decimal */
430
* 2 /* estimate for FLAG_GROUP */
432
+ 1 /* turn floor into ceil */
433
+ 10; /* sign, decimal point etc. */
434
tmp_length = xsum (tmp_length, precision);
437
case 'e': case 'E': case 'g': case 'G':
440
12; /* sign, decimal point, exponent etc. */
441
tmp_length = xsum (tmp_length, precision);
445
# if defined HAVE_WINT_T && !WIDE_CHAR_VERSION
446
if (type == TYPE_WIDE_CHAR)
447
tmp_length = MB_CUR_MAX;
455
if (type == TYPE_WIDE_STRING)
458
local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
460
# if !WIDE_CHAR_VERSION
461
tmp_length = xtimes (tmp_length, MB_CUR_MAX);
466
tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
471
(unsigned int) (sizeof (void *) * CHAR_BIT
472
* 0.25 /* binary -> hexadecimal */
474
+ 1 /* turn floor into ceil */
475
+ 2; /* account for leading 0x */
482
if (tmp_length < width)
485
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
488
if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
492
size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
494
if (size_overflow_p (tmp_memsize))
495
/* Overflow, would lead to out of memory. */
497
tmp = (CHAR_T *) malloc (tmp_memsize);
504
/* Construct the format string for calling snprintf or
508
if (dp->flags & FLAG_GROUP)
510
if (dp->flags & FLAG_LEFT)
512
if (dp->flags & FLAG_SHOWSIGN)
514
if (dp->flags & FLAG_SPACE)
516
if (dp->flags & FLAG_ALT)
518
if (dp->flags & FLAG_ZERO)
520
if (dp->width_start != dp->width_end)
522
size_t n = dp->width_end - dp->width_start;
523
memcpy (p, dp->width_start, n * sizeof (CHAR_T));
526
if (dp->precision_start != dp->precision_end)
528
size_t n = dp->precision_end - dp->precision_start;
529
memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
535
#ifdef HAVE_LONG_LONG
536
case TYPE_LONGLONGINT:
537
case TYPE_ULONGLONGINT:
547
case TYPE_WIDE_STRING:
551
#ifdef HAVE_LONG_DOUBLE
552
case TYPE_LONGDOUBLE:
568
/* Construct the arguments for calling snprintf or sprintf. */
570
if (dp->width_arg_index != ARG_NONE)
572
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
574
prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
576
if (dp->precision_arg_index != ARG_NONE)
578
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
580
prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
584
/* Prepare checking whether snprintf returns the count
586
ENSURE_ALLOCATION (xsum (length, 1));
587
result[length] = '\0';
596
maxlen = allocated - length;
601
# define SNPRINTF_BUF(arg) \
602
switch (prefix_count) \
605
retcount = SNPRINTF (result + length, maxlen, buf, \
609
retcount = SNPRINTF (result + length, maxlen, buf, \
610
prefixes[0], arg, &count); \
613
retcount = SNPRINTF (result + length, maxlen, buf, \
614
prefixes[0], prefixes[1], arg, \
621
# define SNPRINTF_BUF(arg) \
622
switch (prefix_count) \
625
count = sprintf (tmp, buf, arg); \
628
count = sprintf (tmp, buf, prefixes[0], arg); \
631
count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
643
int arg = a.arg[dp->arg_index].a.a_schar;
649
unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
655
int arg = a.arg[dp->arg_index].a.a_short;
661
unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
667
int arg = a.arg[dp->arg_index].a.a_int;
673
unsigned int arg = a.arg[dp->arg_index].a.a_uint;
679
long int arg = a.arg[dp->arg_index].a.a_longint;
685
unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
689
#ifdef HAVE_LONG_LONG
690
case TYPE_LONGLONGINT:
692
long long int arg = a.arg[dp->arg_index].a.a_longlongint;
696
case TYPE_ULONGLONGINT:
698
unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
705
double arg = a.arg[dp->arg_index].a.a_double;
709
#ifdef HAVE_LONG_DOUBLE
710
case TYPE_LONGDOUBLE:
712
long double arg = a.arg[dp->arg_index].a.a_longdouble;
719
int arg = a.arg[dp->arg_index].a.a_char;
726
wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
733
const char *arg = a.arg[dp->arg_index].a.a_string;
738
case TYPE_WIDE_STRING:
740
const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
747
void *arg = a.arg[dp->arg_index].a.a_pointer;
756
/* Portability: Not all implementations of snprintf()
757
are ISO C 99 compliant. Determine the number of
758
bytes that snprintf() has produced or would have
762
/* Verify that snprintf() has NUL-terminated its
764
if (count < maxlen && result[length + count] != '\0')
766
/* Portability hack. */
767
if (retcount > count)
772
/* snprintf() doesn't understand the '%n'
776
/* Don't use the '%n' directive; instead, look
777
at the snprintf() return value. */
783
/* Look at the snprintf() return value. */
786
/* HP-UX 10.20 snprintf() is doubly deficient:
787
It doesn't understand the '%n' directive,
788
*and* it returns -1 (rather than the length
789
that would have been required) when the
790
buffer is too small. */
792
xsum (xtimes (allocated, 2), 12);
793
ENSURE_ALLOCATION (bigger_need);
802
/* Attempt to handle failure. */
805
if (!(result == resultbuf || result == NULL))
807
if (buf_malloced != NULL)
815
if (count >= tmp_length)
816
/* tmp_length was incorrectly calculated - fix the
821
/* Make room for the result. */
824
/* Need at least count bytes. But allocate
825
proportionally, to avoid looping eternally if
826
snprintf() reports a too small count. */
828
xmax (xsum (length, count), xtimes (allocated, 2));
830
ENSURE_ALLOCATION (n);
837
/* The snprintf() result did fit. */
839
/* Append the sprintf() result. */
840
memcpy (result + length, tmp, count * sizeof (CHAR_T));
852
/* Add the final NUL. */
853
ENSURE_ALLOCATION (xsum (length, 1));
854
result[length] = '\0';
856
if (result != resultbuf && length + 1 < allocated)
858
/* Shrink the allocated memory if possible. */
861
memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
866
if (buf_malloced != NULL)
870
if (length > INT_MAX)
871
goto length_overflow;
875
/* We could produce such a big string, but its length doesn't fit into
876
an 'int'. POSIX says that snprintf() fails with errno = EOVERFLOW in
878
if (result != resultbuf)
884
if (!(result == resultbuf || result == NULL))
886
if (buf_malloced != NULL)