1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2006 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"
52 # define SIZE_MAX ((size_t) -1)
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. */
152 buf_neededlength = 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 if (SIZE_MAX / sizeof (CHAR_T) < buf_neededlength)
163 goto out_of_memory_1;
164 buf = (CHAR_T *) malloc (buf_neededlength * sizeof (CHAR_T));
166 goto out_of_memory_1;
170 if (resultbuf != NULL)
173 allocated = *lengthp;
182 result is either == resultbuf or == NULL or malloc-allocated.
183 If length > 0, then result != NULL. */
185 /* Ensures that allocated >= length + extra. Aborts through a jump to
186 out_of_memory if size is too big. */
187 #define ENSURE_ALLOCATION(extra) \
189 size_t needed = length + (extra); \
190 if (needed < length) \
191 goto out_of_memory; \
192 if (needed > allocated) \
194 size_t memory_size; \
197 allocated = (allocated > 0 ? 2 * allocated : 12); \
198 if (needed > allocated) \
199 allocated = needed; \
200 if (SIZE_MAX / sizeof (CHAR_T) < allocated) \
201 goto out_of_memory; \
202 memory_size = allocated * sizeof (CHAR_T); \
203 if (result == resultbuf || result == NULL) \
204 memory = (CHAR_T *) malloc (memory_size); \
206 memory = (CHAR_T *) realloc (result, memory_size); \
207 if (memory == NULL) \
208 goto out_of_memory; \
209 if (result == resultbuf && length > 0) \
210 memcpy (memory, result, length * sizeof (CHAR_T)); \
215 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
217 if (cp != dp->dir_start)
219 size_t n = dp->dir_start - cp;
221 ENSURE_ALLOCATION (n);
222 memcpy (result + length, cp, n * sizeof (CHAR_T));
228 /* Execute a single directive. */
229 if (dp->conversion == '%')
231 if (!(dp->arg_index == ARG_NONE))
233 ENSURE_ALLOCATION (1);
234 result[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;
302 size_t w_tmp = width * 10 + (*digitp++ - '0');
303 if (SIZE_MAX / 10 < width || w_tmp < width)
307 while (digitp != dp->width_end);
312 if (dp->precision_start != dp->precision_end)
314 if (dp->precision_arg_index != ARG_NONE)
318 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
320 arg = a.arg[dp->precision_arg_index].a.a_int;
321 precision = (arg < 0 ? 0 : arg);
325 const CHAR_T *digitp = dp->precision_start + 1;
328 while (digitp != dp->precision_end)
330 size_t p1 = 10 * precision + (*digitp++ - '0');
331 precision = ((SIZE_MAX / 10 < precision
338 switch (dp->conversion)
341 case 'd': case 'i': case 'u':
342 # ifdef HAVE_LONG_LONG
343 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
345 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
346 * 0.30103 /* binary -> decimal */
348 + 1; /* turn floor into ceil */
351 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
353 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
354 * 0.30103 /* binary -> decimal */
356 + 1; /* turn floor into ceil */
359 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
360 * 0.30103 /* binary -> decimal */
362 + 1; /* turn floor into ceil */
363 if (tmp_length < precision)
364 tmp_length = precision;
365 /* Multiply by 2, as an estimate for FLAG_GROUP. */
366 /* Add 1, to account for a leading sign. */
367 tmp_length = (tmp_length < SIZE_MAX / 2
373 # ifdef HAVE_LONG_LONG
374 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
376 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
377 * 0.333334 /* binary -> octal */
379 + 1; /* turn floor into ceil */
382 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
384 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
385 * 0.333334 /* binary -> octal */
387 + 1; /* turn floor into ceil */
390 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
391 * 0.333334 /* binary -> octal */
393 + 1; /* turn floor into ceil */
394 if (tmp_length < precision)
395 tmp_length = precision;
396 /* Add 1, to account for a leading sign. */
397 tmp_length += (tmp_length < SIZE_MAX);
401 # ifdef HAVE_LONG_LONG
402 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
404 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
405 * 0.25 /* binary -> hexadecimal */
407 + 1; /* turn floor into ceil */
410 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
412 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
413 * 0.25 /* binary -> hexadecimal */
415 + 1; /* turn floor into ceil */
418 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
419 * 0.25 /* binary -> hexadecimal */
421 + 1; /* turn floor into ceil */
422 if (tmp_length < precision)
423 tmp_length = precision;
424 /* Add 2, to account for a leading sign or alternate form. */
425 if (tmp_length <= SIZE_MAX / 2)
430 # ifdef HAVE_LONG_DOUBLE
431 if (type == TYPE_LONGDOUBLE)
433 (unsigned int) (LDBL_MAX_EXP
434 * 0.30103 /* binary -> decimal */
435 * 2 /* estimate for FLAG_GROUP */
437 + 1 /* turn floor into ceil */
438 + 10; /* sign, decimal point etc. */
442 (unsigned int) (DBL_MAX_EXP
443 * 0.30103 /* binary -> decimal */
444 * 2 /* estimate for FLAG_GROUP */
446 + 1 /* turn floor into ceil */
447 + 10; /* sign, decimal point etc. */
448 tmp_length += precision;
449 if (tmp_length < precision)
453 case 'e': case 'E': case 'g': case 'G':
456 12; /* sign, decimal point, exponent etc. */
457 tmp_length += precision;
458 if (tmp_length < precision)
463 # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION
464 if (type == TYPE_WIDE_CHAR)
465 tmp_length = MB_CUR_MAX;
473 if (type == TYPE_WIDE_STRING)
476 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
478 # if !WIDE_CHAR_VERSION
479 if (SIZE_MAX / MB_CUR_MAX < tmp_length)
481 tmp_length *= MB_CUR_MAX;
486 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
491 (unsigned int) (sizeof (void *) * CHAR_BIT
492 * 0.25 /* binary -> hexadecimal */
494 + 1 /* turn floor into ceil */
495 + 2; /* account for leading 0x */
502 if (tmp_length < width)
505 tmp_length++; /* account for trailing NUL */
510 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
514 if (SIZE_MAX / sizeof (CHAR_T) < tmp_length)
515 /* Overflow, would lead to out of memory. */
517 tmp = (CHAR_T *) malloc (tmp_length * sizeof (CHAR_T));
524 /* Construct the format string for calling snprintf or
528 if (dp->flags & FLAG_GROUP)
530 if (dp->flags & FLAG_LEFT)
532 if (dp->flags & FLAG_SHOWSIGN)
534 if (dp->flags & FLAG_SPACE)
536 if (dp->flags & FLAG_ALT)
538 if (dp->flags & FLAG_ZERO)
540 if (dp->width_start != dp->width_end)
542 size_t n = dp->width_end - dp->width_start;
543 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
546 if (dp->precision_start != dp->precision_end)
548 size_t n = dp->precision_end - dp->precision_start;
549 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
555 #ifdef HAVE_LONG_LONG
556 case TYPE_LONGLONGINT:
557 case TYPE_ULONGLONGINT:
567 case TYPE_WIDE_STRING:
571 #ifdef HAVE_LONG_DOUBLE
572 case TYPE_LONGDOUBLE:
588 /* Construct the arguments for calling snprintf or sprintf. */
590 if (dp->width_arg_index != ARG_NONE)
592 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
594 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
596 if (dp->precision_arg_index != ARG_NONE)
598 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
600 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
604 /* Prepare checking whether snprintf returns the count
606 ENSURE_ALLOCATION (1);
607 result[length] = '\0';
616 maxlen = allocated - length;
621 # define SNPRINTF_BUF(arg) \
622 switch (prefix_count) \
625 retcount = SNPRINTF (result + length, maxlen, buf, \
629 retcount = SNPRINTF (result + length, maxlen, buf, \
630 prefixes[0], arg, &count); \
633 retcount = SNPRINTF (result + length, maxlen, buf, \
634 prefixes[0], prefixes[1], arg, \
641 # define SNPRINTF_BUF(arg) \
642 switch (prefix_count) \
645 count = sprintf (tmp, buf, arg); \
648 count = sprintf (tmp, buf, prefixes[0], arg); \
651 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
663 int arg = a.arg[dp->arg_index].a.a_schar;
669 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
675 int arg = a.arg[dp->arg_index].a.a_short;
681 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
687 int arg = a.arg[dp->arg_index].a.a_int;
693 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
699 long int arg = a.arg[dp->arg_index].a.a_longint;
705 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
709 #ifdef HAVE_LONG_LONG
710 case TYPE_LONGLONGINT:
712 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
716 case TYPE_ULONGLONGINT:
718 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
725 double arg = a.arg[dp->arg_index].a.a_double;
729 #ifdef HAVE_LONG_DOUBLE
730 case TYPE_LONGDOUBLE:
732 long double arg = a.arg[dp->arg_index].a.a_longdouble;
739 int arg = a.arg[dp->arg_index].a.a_char;
746 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
753 const char *arg = a.arg[dp->arg_index].a.a_string;
758 case TYPE_WIDE_STRING:
760 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
767 void *arg = a.arg[dp->arg_index].a.a_pointer;
776 /* Portability: Not all implementations of snprintf()
777 are ISO C 99 compliant. Determine the number of
778 bytes that snprintf() has produced or would have
782 /* Verify that snprintf() has NUL-terminated its
784 if (count < maxlen && result[length + count] != '\0')
786 /* Portability hack. */
787 if (retcount > count)
792 /* snprintf() doesn't understand the '%n'
796 /* Don't use the '%n' directive; instead, look
797 at the snprintf() return value. */
803 /* Look at the snprintf() return value. */
806 /* HP-UX 10.20 snprintf() is doubly deficient:
807 It doesn't understand the '%n' directive,
808 *and* it returns -1 (rather than the length
809 that would have been required) when the
810 buffer is too small. */
812 (allocated > 12 ? allocated : 12);
813 ENSURE_ALLOCATION (bigger_need);
822 /* Attempt to handle failure. */
825 if (!(result == resultbuf || result == NULL))
827 if (buf_malloced != NULL)
835 if (count >= tmp_length)
836 /* tmp_length was incorrectly calculated - fix the
841 /* Make room for the result. */
844 /* Need at least count bytes. But allocate
845 proportionally, to avoid looping eternally if
846 snprintf() reports a too small count. */
847 ENSURE_ALLOCATION (count < allocated
848 ? allocated : count);
855 /* The snprintf() result did fit. */
857 /* Append the sprintf() result. */
858 memcpy (result + length, tmp, count * sizeof (CHAR_T));
870 /* Add the final NUL. */
871 ENSURE_ALLOCATION (1);
872 result[length] = '\0';
874 if (result != resultbuf && length + 1 < allocated)
876 /* Shrink the allocated memory if possible. */
879 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
884 if (buf_malloced != NULL)
888 if (length > INT_MAX)
889 goto length_overflow;
893 /* We could produce such a big string, but its length doesn't fit into
894 an 'int'. POSIX says that snprintf() fails with errno = EOVERFLOW in
896 if (result != resultbuf)
902 if (!(result == resultbuf || result == NULL))
904 if (buf_malloced != NULL)