1 /* vsprintf with automatic memory allocation.
2 This file is intended to provide exactly the same functionality
3 as the version in gnulib, but without the need for the xsize module.
5 Copyright (C) 1999, 2002-2007 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
22 This must come before <config.h> because <config.h> may include
23 <features.h>, and once <features.h> has been included, it's too late. */
25 # 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 <stdint.h> /* SIZE_MAX */
43 #include <string.h> /* memcpy(), strlen() */
44 #include <errno.h> /* errno */
45 #include <limits.h> /* CHAR_BIT, INT_MAX */
46 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
48 # include "wprintf-parse.h"
50 # include "printf-parse.h"
55 # define local_wcslen wcslen
57 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
58 a dependency towards this library, here is a local substitute.
59 Define this substitute only once, even if this file is included
60 twice in the same compilation unit. */
61 # ifndef local_wcslen_defined
62 # define local_wcslen_defined 1
64 local_wcslen (const wchar_t *s)
68 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
77 # define VASNPRINTF vasnwprintf
78 # define CHAR_T wchar_t
79 # define DIRECTIVE wchar_t_directive
80 # define DIRECTIVES wchar_t_directives
81 # define PRINTF_PARSE wprintf_parse
82 # define USE_SNPRINTF 1
83 # if HAVE_DECL__SNWPRINTF
84 /* On Windows, the function swprintf() has a different signature than
85 on Unix; we use the _snwprintf() function instead. */
86 # define SNPRINTF _snwprintf
89 # define SNPRINTF swprintf
92 # define VASNPRINTF vasnprintf
94 # define DIRECTIVE char_directive
95 # define DIRECTIVES char_directives
96 # define PRINTF_PARSE printf_parse
97 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
98 # if HAVE_DECL__SNPRINTF
100 # define SNPRINTF _snprintf
103 # define SNPRINTF snprintf
108 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
113 if (PRINTF_PARSE (format, &d, &a) < 0)
124 if (printf_fetchargs (args, &a) < 0)
132 size_t buf_neededlength;
134 CHAR_T *buf_malloced;
138 /* Output string accumulator. */
143 /* Allocate a small buffer that will hold a directive passed to
144 sprintf or snprintf. */
145 buf_neededlength = 7 + d.max_width_length + d.max_precision_length + 6;
147 if (buf_neededlength < 4000 / sizeof (CHAR_T))
149 buf = alloca (buf_neededlength * sizeof (CHAR_T));
155 if (SIZE_MAX / sizeof (CHAR_T) < buf_neededlength)
156 goto out_of_memory_1;
157 buf = (CHAR_T *) malloc (buf_neededlength * sizeof (CHAR_T));
159 goto out_of_memory_1;
163 if (resultbuf != NULL)
166 allocated = *lengthp;
175 result is either == resultbuf or == NULL or malloc-allocated.
176 If length > 0, then result != NULL. */
178 /* Ensures that allocated >= length + extra. Aborts through a jump to
179 out_of_memory if size is too big. */
180 #define ENSURE_ALLOCATION(extra) \
182 size_t needed = length + (extra); \
183 if (needed < length) \
184 goto out_of_memory; \
185 if (needed > allocated) \
187 size_t memory_size; \
190 allocated = (allocated > 0 ? 2 * allocated : 12); \
191 if (needed > allocated) \
192 allocated = needed; \
193 if (SIZE_MAX / sizeof (CHAR_T) < allocated) \
194 goto out_of_memory; \
195 memory_size = allocated * sizeof (CHAR_T); \
196 if (result == resultbuf || result == NULL) \
197 memory = (CHAR_T *) malloc (memory_size); \
199 memory = (CHAR_T *) realloc (result, memory_size); \
200 if (memory == NULL) \
201 goto out_of_memory; \
202 if (result == resultbuf && length > 0) \
203 memcpy (memory, result, length * sizeof (CHAR_T)); \
208 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
210 if (cp != dp->dir_start)
212 size_t n = dp->dir_start - cp;
214 ENSURE_ALLOCATION (n);
215 memcpy (result + length, cp, n * sizeof (CHAR_T));
221 /* Execute a single directive. */
222 if (dp->conversion == '%')
224 if (!(dp->arg_index == ARG_NONE))
226 ENSURE_ALLOCATION (1);
227 result[length] = '%';
232 if (!(dp->arg_index != ARG_NONE))
235 if (dp->conversion == 'n')
237 switch (a.arg[dp->arg_index].type)
239 case TYPE_COUNT_SCHAR_POINTER:
240 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
242 case TYPE_COUNT_SHORT_POINTER:
243 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
245 case TYPE_COUNT_INT_POINTER:
246 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
248 case TYPE_COUNT_LONGINT_POINTER:
249 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
251 #if HAVE_LONG_LONG_INT
252 case TYPE_COUNT_LONGLONGINT_POINTER:
253 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
262 arg_type type = a.arg[dp->arg_index].type;
264 unsigned int prefix_count;
271 /* Allocate a temporary buffer of sufficient size for calling
278 if (dp->width_start != dp->width_end)
280 if (dp->width_arg_index != ARG_NONE)
284 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
286 arg = a.arg[dp->width_arg_index].a.a_int;
287 width = (arg < 0 ? (unsigned int) (-arg) : arg);
291 const CHAR_T *digitp = dp->width_start;
295 size_t w_tmp = width * 10 + (*digitp++ - '0');
296 if (SIZE_MAX / 10 < width || w_tmp < width)
300 while (digitp != dp->width_end);
305 if (dp->precision_start != dp->precision_end)
307 if (dp->precision_arg_index != ARG_NONE)
311 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
313 arg = a.arg[dp->precision_arg_index].a.a_int;
314 precision = (arg < 0 ? 0 : arg);
318 const CHAR_T *digitp = dp->precision_start + 1;
321 while (digitp != dp->precision_end)
323 size_t p1 = 10 * precision + (*digitp++ - '0');
324 precision = ((SIZE_MAX / 10 < precision
331 switch (dp->conversion)
334 case 'd': case 'i': case 'u':
335 # if HAVE_LONG_LONG_INT
336 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
338 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
339 * 0.30103 /* binary -> decimal */
341 + 1; /* turn floor into ceil */
344 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
346 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
347 * 0.30103 /* binary -> decimal */
349 + 1; /* turn floor into ceil */
352 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
353 * 0.30103 /* binary -> decimal */
355 + 1; /* turn floor into ceil */
356 if (tmp_length < precision)
357 tmp_length = precision;
358 /* Multiply by 2, as an estimate for FLAG_GROUP. */
359 /* Add 1, to account for a leading sign. */
360 tmp_length = (tmp_length < SIZE_MAX / 2
366 # if HAVE_LONG_LONG_INT
367 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
369 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
370 * 0.333334 /* binary -> octal */
372 + 1; /* turn floor into ceil */
375 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
377 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
378 * 0.333334 /* binary -> octal */
380 + 1; /* turn floor into ceil */
383 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
384 * 0.333334 /* binary -> octal */
386 + 1; /* turn floor into ceil */
387 if (tmp_length < precision)
388 tmp_length = precision;
389 /* Add 1, to account for a leading sign. */
390 tmp_length += (tmp_length < SIZE_MAX);
394 # if HAVE_LONG_LONG_INT
395 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
397 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
398 * 0.25 /* binary -> hexadecimal */
400 + 1; /* turn floor into ceil */
403 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
405 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
406 * 0.25 /* binary -> hexadecimal */
408 + 1; /* turn floor into ceil */
411 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
412 * 0.25 /* binary -> hexadecimal */
414 + 1; /* turn floor into ceil */
415 if (tmp_length < precision)
416 tmp_length = precision;
417 /* Add 2, to account for a leading sign or alternate form. */
424 # if HAVE_LONG_DOUBLE
425 if (type == TYPE_LONGDOUBLE)
427 (unsigned int) (LDBL_MAX_EXP
428 * 0.30103 /* binary -> decimal */
429 * 2 /* estimate for FLAG_GROUP */
431 + 1 /* turn floor into ceil */
432 + 10; /* sign, decimal point etc. */
436 (unsigned int) (DBL_MAX_EXP
437 * 0.30103 /* binary -> decimal */
438 * 2 /* estimate for FLAG_GROUP */
440 + 1 /* turn floor into ceil */
441 + 10; /* sign, decimal point etc. */
442 tmp_length += precision;
443 if (tmp_length < precision)
447 case 'e': case 'E': case 'g': case 'G':
449 12; /* sign, decimal point, exponent etc. */
450 tmp_length += precision;
451 if (tmp_length < precision)
456 # if HAVE_LONG_DOUBLE
457 if (type == TYPE_LONGDOUBLE)
459 (unsigned int) (LDBL_DIG
460 * 0.831 /* decimal -> hexadecimal */
462 + 1; /* turn floor into ceil */
466 (unsigned int) (DBL_DIG
467 * 0.831 /* decimal -> hexadecimal */
469 + 1; /* turn floor into ceil */
470 if (tmp_length < precision)
471 tmp_length = precision;
472 /* Account for sign, decimal point etc. */
479 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
480 if (type == TYPE_WIDE_CHAR)
481 tmp_length = MB_CUR_MAX;
489 if (type == TYPE_WIDE_STRING)
492 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
494 # if !WIDE_CHAR_VERSION
495 if (SIZE_MAX / MB_CUR_MAX < tmp_length)
497 tmp_length *= MB_CUR_MAX;
502 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
507 (unsigned int) (sizeof (void *) * CHAR_BIT
508 * 0.25 /* binary -> hexadecimal */
510 + 1 /* turn floor into ceil */
511 + 2; /* account for leading 0x */
518 if (tmp_length < width)
521 tmp_length++; /* account for trailing NUL */
526 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
530 if (SIZE_MAX / sizeof (CHAR_T) < tmp_length)
531 /* Overflow, would lead to out of memory. */
533 tmp = (CHAR_T *) malloc (tmp_length * sizeof (CHAR_T));
540 /* Construct the format string for calling snprintf or
544 if (dp->flags & FLAG_GROUP)
546 if (dp->flags & FLAG_LEFT)
548 if (dp->flags & FLAG_SHOWSIGN)
550 if (dp->flags & FLAG_SPACE)
552 if (dp->flags & FLAG_ALT)
554 if (dp->flags & FLAG_ZERO)
556 if (dp->width_start != dp->width_end)
558 size_t n = dp->width_end - dp->width_start;
559 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
562 if (dp->precision_start != dp->precision_end)
564 size_t n = dp->precision_end - dp->precision_start;
565 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
571 #if HAVE_LONG_LONG_INT
572 case TYPE_LONGLONGINT:
573 case TYPE_ULONGLONGINT:
583 case TYPE_WIDE_STRING:
588 case TYPE_LONGDOUBLE:
604 /* Construct the arguments for calling snprintf or sprintf. */
606 if (dp->width_arg_index != ARG_NONE)
608 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
610 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
612 if (dp->precision_arg_index != ARG_NONE)
614 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
616 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
620 /* Prepare checking whether snprintf returns the count
622 ENSURE_ALLOCATION (1);
623 result[length] = '\0';
632 maxlen = allocated - length;
637 # define SNPRINTF_BUF(arg) \
638 switch (prefix_count) \
641 retcount = SNPRINTF (result + length, maxlen, buf, \
645 retcount = SNPRINTF (result + length, maxlen, buf, \
646 prefixes[0], arg, &count); \
649 retcount = SNPRINTF (result + length, maxlen, buf, \
650 prefixes[0], prefixes[1], arg, \
657 # define SNPRINTF_BUF(arg) \
658 switch (prefix_count) \
661 count = sprintf (tmp, buf, arg); \
664 count = sprintf (tmp, buf, prefixes[0], arg); \
667 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
679 int arg = a.arg[dp->arg_index].a.a_schar;
685 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
691 int arg = a.arg[dp->arg_index].a.a_short;
697 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
703 int arg = a.arg[dp->arg_index].a.a_int;
709 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
715 long int arg = a.arg[dp->arg_index].a.a_longint;
721 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
725 #if HAVE_LONG_LONG_INT
726 case TYPE_LONGLONGINT:
728 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
732 case TYPE_ULONGLONGINT:
734 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
741 double arg = a.arg[dp->arg_index].a.a_double;
746 case TYPE_LONGDOUBLE:
748 long double arg = a.arg[dp->arg_index].a.a_longdouble;
755 int arg = a.arg[dp->arg_index].a.a_char;
762 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
769 const char *arg = a.arg[dp->arg_index].a.a_string;
774 case TYPE_WIDE_STRING:
776 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
783 void *arg = a.arg[dp->arg_index].a.a_pointer;
792 /* Portability: Not all implementations of snprintf()
793 are ISO C 99 compliant. Determine the number of
794 bytes that snprintf() has produced or would have
798 /* Verify that snprintf() has NUL-terminated its
800 if (count < maxlen && result[length + count] != '\0')
802 /* Portability hack. */
803 if (retcount > count)
808 /* snprintf() doesn't understand the '%n'
812 /* Don't use the '%n' directive; instead, look
813 at the snprintf() return value. */
819 /* Look at the snprintf() return value. */
822 /* HP-UX 10.20 snprintf() is doubly deficient:
823 It doesn't understand the '%n' directive,
824 *and* it returns -1 (rather than the length
825 that would have been required) when the
826 buffer is too small. */
828 (allocated > 12 ? allocated : 12);
829 ENSURE_ALLOCATION (bigger_need);
838 /* Attempt to handle failure. */
841 if (!(result == resultbuf || result == NULL))
843 if (buf_malloced != NULL)
851 if (count >= tmp_length)
852 /* tmp_length was incorrectly calculated - fix the
857 /* Make room for the result. */
860 /* Need at least count bytes. But allocate
861 proportionally, to avoid looping eternally if
862 snprintf() reports a too small count. */
863 ENSURE_ALLOCATION (count < allocated
864 ? allocated : count);
871 /* The snprintf() result did fit. */
873 /* Append the sprintf() result. */
874 memcpy (result + length, tmp, count * sizeof (CHAR_T));
886 /* Add the final NUL. */
887 ENSURE_ALLOCATION (1);
888 result[length] = '\0';
890 if (result != resultbuf && length + 1 < allocated)
892 /* Shrink the allocated memory if possible. */
895 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
900 if (buf_malloced != NULL)
904 if (length > INT_MAX)
905 goto length_overflow;
909 /* We could produce such a big string, but its length doesn't fit into
910 an 'int'. POSIX says that snprintf() fails with errno = EOVERFLOW in
912 if (result != resultbuf)
918 if (!(result == resultbuf || result == NULL))
920 if (buf_malloced != NULL)