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 it
5 under the terms of the GNU Library General Public License as published
6 by 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
20 This must come before <config.h> because <config.h> may include
21 <features.h>, and once <features.h> has been included, it's too late. */
23 # define _GNU_SOURCE 1
35 # include "vasnwprintf.h"
37 # include "vasnprintf.h"
40 #include <stdio.h> /* snprintf(), sprintf() */
41 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
42 #include <string.h> /* memcpy(), strlen() */
43 #include <errno.h> /* errno */
44 #include <limits.h> /* CHAR_BIT, INT_MAX */
45 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
47 # include "wprintf-parse.h"
49 # include "printf-parse.h"
52 /* Checked size_t computations. */
55 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
57 # define EOVERFLOW E2BIG
62 # define local_wcslen wcslen
64 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
65 a dependency towards this library, here is a local substitute.
66 Define this substitute only once, even if this file is included
67 twice in the same compilation unit. */
68 # ifndef local_wcslen_defined
69 # define local_wcslen_defined 1
71 local_wcslen (const wchar_t *s)
75 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
84 # define VASNPRINTF vasnwprintf
85 # define CHAR_T wchar_t
86 # define DIRECTIVE wchar_t_directive
87 # define DIRECTIVES wchar_t_directives
88 # define PRINTF_PARSE wprintf_parse
89 # define USE_SNPRINTF 1
90 # if HAVE_DECL__SNWPRINTF
91 /* On Windows, the function swprintf() has a different signature than
92 on Unix; we use the _snwprintf() function instead. */
93 # define SNPRINTF _snwprintf
96 # define SNPRINTF swprintf
99 # define VASNPRINTF vasnprintf
101 # define DIRECTIVE char_directive
102 # define DIRECTIVES char_directives
103 # define PRINTF_PARSE printf_parse
104 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
105 # if HAVE_DECL__SNPRINTF
107 # define SNPRINTF _snprintf
110 # define SNPRINTF snprintf
115 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
120 if (PRINTF_PARSE (format, &d, &a) < 0)
131 if (printf_fetchargs (args, &a) < 0)
139 size_t buf_neededlength;
141 CHAR_T *buf_malloced;
145 /* Output string accumulator. */
150 /* Allocate a small buffer that will hold a directive passed to
151 sprintf or snprintf. */
153 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
155 if (buf_neededlength < 4000 / sizeof (CHAR_T))
157 buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
163 size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
164 if (size_overflow_p (buf_memsize))
165 goto out_of_memory_1;
166 buf = (CHAR_T *) malloc (buf_memsize);
168 goto out_of_memory_1;
172 if (resultbuf != NULL)
175 allocated = *lengthp;
184 result is either == resultbuf or == NULL or malloc-allocated.
185 If length > 0, then result != NULL. */
187 /* Ensures that allocated >= needed. Aborts through a jump to
188 out_of_memory if needed is SIZE_MAX or otherwise too big. */
189 #define ENSURE_ALLOCATION(needed) \
190 if ((needed) > allocated) \
192 size_t memory_size; \
195 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
196 if ((needed) > allocated) \
197 allocated = (needed); \
198 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
199 if (size_overflow_p (memory_size)) \
200 goto out_of_memory; \
201 if (result == resultbuf || result == NULL) \
202 memory = (CHAR_T *) malloc (memory_size); \
204 memory = (CHAR_T *) realloc (result, memory_size); \
205 if (memory == NULL) \
206 goto out_of_memory; \
207 if (result == resultbuf && length > 0) \
208 memcpy (memory, result, length * sizeof (CHAR_T)); \
212 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
214 if (cp != dp->dir_start)
216 size_t n = dp->dir_start - cp;
217 size_t augmented_length = xsum (length, n);
219 ENSURE_ALLOCATION (augmented_length);
220 memcpy (result + length, cp, n * sizeof (CHAR_T));
221 length = augmented_length;
226 /* Execute a single directive. */
227 if (dp->conversion == '%')
229 size_t augmented_length;
231 if (!(dp->arg_index == ARG_NONE))
233 augmented_length = xsum (length, 1);
234 ENSURE_ALLOCATION (augmented_length);
235 result[length] = '%';
236 length = augmented_length;
240 if (!(dp->arg_index != ARG_NONE))
243 if (dp->conversion == 'n')
245 switch (a.arg[dp->arg_index].type)
247 case TYPE_COUNT_SCHAR_POINTER:
248 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
250 case TYPE_COUNT_SHORT_POINTER:
251 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
253 case TYPE_COUNT_INT_POINTER:
254 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
256 case TYPE_COUNT_LONGINT_POINTER:
257 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
259 #ifdef HAVE_LONG_LONG
260 case TYPE_COUNT_LONGLONGINT_POINTER:
261 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
270 arg_type type = a.arg[dp->arg_index].type;
272 unsigned int prefix_count;
279 /* Allocate a temporary buffer of sufficient size for calling
286 if (dp->width_start != dp->width_end)
288 if (dp->width_arg_index != ARG_NONE)
292 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
294 arg = a.arg[dp->width_arg_index].a.a_int;
295 width = (arg < 0 ? (unsigned int) (-arg) : arg);
299 const CHAR_T *digitp = dp->width_start;
302 width = xsum (xtimes (width, 10), *digitp++ - '0');
303 while (digitp != dp->width_end);
308 if (dp->precision_start != dp->precision_end)
310 if (dp->precision_arg_index != ARG_NONE)
314 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
316 arg = a.arg[dp->precision_arg_index].a.a_int;
317 precision = (arg < 0 ? 0 : arg);
321 const CHAR_T *digitp = dp->precision_start + 1;
324 while (digitp != dp->precision_end)
325 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
329 switch (dp->conversion)
332 case 'd': case 'i': case 'u':
333 # ifdef HAVE_LONG_LONG
334 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
336 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
337 * 0.30103 /* binary -> decimal */
338 * 2 /* estimate for FLAG_GROUP */
340 + 1 /* turn floor into ceil */
341 + 1; /* account for leading sign */
344 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
346 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
347 * 0.30103 /* binary -> decimal */
348 * 2 /* estimate for FLAG_GROUP */
350 + 1 /* turn floor into ceil */
351 + 1; /* account for leading sign */
354 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
355 * 0.30103 /* binary -> decimal */
356 * 2 /* estimate for FLAG_GROUP */
358 + 1 /* turn floor into ceil */
359 + 1; /* account for leading sign */
363 # ifdef HAVE_LONG_LONG
364 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
366 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
367 * 0.333334 /* binary -> octal */
369 + 1 /* turn floor into ceil */
370 + 1; /* account for leading sign */
373 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
375 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
376 * 0.333334 /* binary -> octal */
378 + 1 /* turn floor into ceil */
379 + 1; /* account for leading sign */
382 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
383 * 0.333334 /* binary -> octal */
385 + 1 /* turn floor into ceil */
386 + 1; /* account for leading sign */
390 # ifdef HAVE_LONG_LONG
391 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
393 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
394 * 0.25 /* binary -> hexadecimal */
396 + 1 /* turn floor into ceil */
397 + 2; /* account for leading sign or alternate form */
400 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
402 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
403 * 0.25 /* binary -> hexadecimal */
405 + 1 /* turn floor into ceil */
406 + 2; /* account for leading sign or alternate form */
409 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
410 * 0.25 /* binary -> hexadecimal */
412 + 1 /* turn floor into ceil */
413 + 2; /* account for leading sign or alternate form */
417 # ifdef HAVE_LONG_DOUBLE
418 if (type == TYPE_LONGDOUBLE)
420 (unsigned int) (LDBL_MAX_EXP
421 * 0.30103 /* binary -> decimal */
422 * 2 /* estimate for FLAG_GROUP */
424 + 1 /* turn floor into ceil */
425 + 10; /* sign, decimal point etc. */
429 (unsigned int) (DBL_MAX_EXP
430 * 0.30103 /* binary -> decimal */
431 * 2 /* estimate for FLAG_GROUP */
433 + 1 /* turn floor into ceil */
434 + 10; /* sign, decimal point etc. */
435 tmp_length = xsum (tmp_length, precision);
438 case 'e': case 'E': case 'g': case 'G':
441 12; /* sign, decimal point, exponent etc. */
442 tmp_length = xsum (tmp_length, precision);
446 # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION
447 if (type == TYPE_WIDE_CHAR)
448 tmp_length = MB_CUR_MAX;
456 if (type == TYPE_WIDE_STRING)
459 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
461 # if !WIDE_CHAR_VERSION
462 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
467 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
472 (unsigned int) (sizeof (void *) * CHAR_BIT
473 * 0.25 /* binary -> hexadecimal */
475 + 1 /* turn floor into ceil */
476 + 2; /* account for leading 0x */
483 if (tmp_length < width)
486 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
489 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
493 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
495 if (size_overflow_p (tmp_memsize))
496 /* Overflow, would lead to out of memory. */
498 tmp = (CHAR_T *) malloc (tmp_memsize);
505 /* Construct the format string for calling snprintf or
509 if (dp->flags & FLAG_GROUP)
511 if (dp->flags & FLAG_LEFT)
513 if (dp->flags & FLAG_SHOWSIGN)
515 if (dp->flags & FLAG_SPACE)
517 if (dp->flags & FLAG_ALT)
519 if (dp->flags & FLAG_ZERO)
521 if (dp->width_start != dp->width_end)
523 size_t n = dp->width_end - dp->width_start;
524 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
527 if (dp->precision_start != dp->precision_end)
529 size_t n = dp->precision_end - dp->precision_start;
530 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
536 #ifdef HAVE_LONG_LONG
537 case TYPE_LONGLONGINT:
538 case TYPE_ULONGLONGINT:
548 case TYPE_WIDE_STRING:
552 #ifdef HAVE_LONG_DOUBLE
553 case TYPE_LONGDOUBLE:
569 /* Construct the arguments for calling snprintf or sprintf. */
571 if (dp->width_arg_index != ARG_NONE)
573 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
575 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
577 if (dp->precision_arg_index != ARG_NONE)
579 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
581 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
585 /* Prepare checking whether snprintf returns the count
587 ENSURE_ALLOCATION (xsum (length, 1));
588 result[length] = '\0';
597 maxlen = allocated - length;
602 # define SNPRINTF_BUF(arg) \
603 switch (prefix_count) \
606 retcount = SNPRINTF (result + length, maxlen, buf, \
610 retcount = SNPRINTF (result + length, maxlen, buf, \
611 prefixes[0], arg, &count); \
614 retcount = SNPRINTF (result + length, maxlen, buf, \
615 prefixes[0], prefixes[1], arg, \
622 # define SNPRINTF_BUF(arg) \
623 switch (prefix_count) \
626 count = sprintf (tmp, buf, arg); \
629 count = sprintf (tmp, buf, prefixes[0], arg); \
632 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
644 int arg = a.arg[dp->arg_index].a.a_schar;
650 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
656 int arg = a.arg[dp->arg_index].a.a_short;
662 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
668 int arg = a.arg[dp->arg_index].a.a_int;
674 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
680 long int arg = a.arg[dp->arg_index].a.a_longint;
686 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
690 #ifdef HAVE_LONG_LONG
691 case TYPE_LONGLONGINT:
693 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
697 case TYPE_ULONGLONGINT:
699 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
706 double arg = a.arg[dp->arg_index].a.a_double;
710 #ifdef HAVE_LONG_DOUBLE
711 case TYPE_LONGDOUBLE:
713 long double arg = a.arg[dp->arg_index].a.a_longdouble;
720 int arg = a.arg[dp->arg_index].a.a_char;
727 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
734 const char *arg = a.arg[dp->arg_index].a.a_string;
739 case TYPE_WIDE_STRING:
741 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
748 void *arg = a.arg[dp->arg_index].a.a_pointer;
757 /* Portability: Not all implementations of snprintf()
758 are ISO C 99 compliant. Determine the number of
759 bytes that snprintf() has produced or would have
763 /* Verify that snprintf() has NUL-terminated its
765 if (count < maxlen && result[length + count] != '\0')
767 /* Portability hack. */
768 if (retcount > count)
773 /* snprintf() doesn't understand the '%n'
777 /* Don't use the '%n' directive; instead, look
778 at the snprintf() return value. */
784 /* Look at the snprintf() return value. */
787 /* HP-UX 10.20 snprintf() is doubly deficient:
788 It doesn't understand the '%n' directive,
789 *and* it returns -1 (rather than the length
790 that would have been required) when the
791 buffer is too small. */
793 xsum (xtimes (allocated, 2), 12);
794 ENSURE_ALLOCATION (bigger_need);
803 /* Attempt to handle failure. */
806 if (!(result == resultbuf || result == NULL))
808 if (buf_malloced != NULL)
816 if (count >= tmp_length)
817 /* tmp_length was incorrectly calculated - fix the
822 /* Make room for the result. */
825 /* Need at least count bytes. But allocate
826 proportionally, to avoid looping eternally if
827 snprintf() reports a too small count. */
829 xmax (xsum (length, count), xtimes (allocated, 2));
831 ENSURE_ALLOCATION (n);
838 /* The snprintf() result did fit. */
840 /* Append the sprintf() result. */
841 memcpy (result + length, tmp, count * sizeof (CHAR_T));
853 /* Add the final NUL. */
854 ENSURE_ALLOCATION (xsum (length, 1));
855 result[length] = '\0';
857 if (result != resultbuf && length + 1 < allocated)
859 /* Shrink the allocated memory if possible. */
862 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
867 if (buf_malloced != NULL)
871 if (length > INT_MAX)
872 goto length_overflow;
876 /* We could produce such a big string, but its length doesn't fit into
877 an 'int'. POSIX says that snprintf() fails with errno = EOVERFLOW in
879 if (result != resultbuf)
885 if (!(result == resultbuf || result == NULL))
887 if (buf_malloced != NULL)