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 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 */
339 + 1; /* turn floor into ceil */
342 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
344 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
345 * 0.30103 /* binary -> decimal */
347 + 1; /* turn floor into ceil */
350 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
351 * 0.30103 /* binary -> decimal */
353 + 1; /* turn floor into ceil */
354 if (tmp_length < precision)
355 tmp_length = precision;
356 /* Multiply by 2, as an estimate for FLAG_GROUP. */
357 tmp_length = xsum (tmp_length, tmp_length);
358 /* Add 1, to account for a leading sign. */
359 tmp_length = xsum (tmp_length, 1);
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 */
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 */
380 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
381 * 0.333334 /* binary -> octal */
383 + 1; /* turn floor into ceil */
384 if (tmp_length < precision)
385 tmp_length = precision;
386 /* Add 1, to account for a leading sign. */
387 tmp_length = xsum (tmp_length, 1);
391 # ifdef HAVE_LONG_LONG
392 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
394 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
395 * 0.25 /* binary -> hexadecimal */
397 + 1; /* turn floor into ceil */
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 */
408 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
409 * 0.25 /* binary -> hexadecimal */
411 + 1; /* turn floor into ceil */
412 if (tmp_length < precision)
413 tmp_length = precision;
414 /* Add 2, to account for a leading sign or alternate form. */
415 tmp_length = xsum (tmp_length, 2);
419 # ifdef HAVE_LONG_DOUBLE
420 if (type == TYPE_LONGDOUBLE)
422 (unsigned int) (LDBL_MAX_EXP
423 * 0.30103 /* binary -> decimal */
424 * 2 /* estimate for FLAG_GROUP */
426 + 1 /* turn floor into ceil */
427 + 10; /* sign, decimal point etc. */
431 (unsigned int) (DBL_MAX_EXP
432 * 0.30103 /* binary -> decimal */
433 * 2 /* estimate for FLAG_GROUP */
435 + 1 /* turn floor into ceil */
436 + 10; /* sign, decimal point etc. */
437 tmp_length = xsum (tmp_length, precision);
440 case 'e': case 'E': case 'g': case 'G':
443 12; /* sign, decimal point, exponent etc. */
444 tmp_length = xsum (tmp_length, precision);
448 # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION
449 if (type == TYPE_WIDE_CHAR)
450 tmp_length = MB_CUR_MAX;
458 if (type == TYPE_WIDE_STRING)
461 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
463 # if !WIDE_CHAR_VERSION
464 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
469 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
474 (unsigned int) (sizeof (void *) * CHAR_BIT
475 * 0.25 /* binary -> hexadecimal */
477 + 1 /* turn floor into ceil */
478 + 2; /* account for leading 0x */
485 if (tmp_length < width)
488 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
491 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
495 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
497 if (size_overflow_p (tmp_memsize))
498 /* Overflow, would lead to out of memory. */
500 tmp = (CHAR_T *) malloc (tmp_memsize);
507 /* Construct the format string for calling snprintf or
511 if (dp->flags & FLAG_GROUP)
513 if (dp->flags & FLAG_LEFT)
515 if (dp->flags & FLAG_SHOWSIGN)
517 if (dp->flags & FLAG_SPACE)
519 if (dp->flags & FLAG_ALT)
521 if (dp->flags & FLAG_ZERO)
523 if (dp->width_start != dp->width_end)
525 size_t n = dp->width_end - dp->width_start;
526 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
529 if (dp->precision_start != dp->precision_end)
531 size_t n = dp->precision_end - dp->precision_start;
532 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
538 #ifdef HAVE_LONG_LONG
539 case TYPE_LONGLONGINT:
540 case TYPE_ULONGLONGINT:
550 case TYPE_WIDE_STRING:
554 #ifdef HAVE_LONG_DOUBLE
555 case TYPE_LONGDOUBLE:
571 /* Construct the arguments for calling snprintf or sprintf. */
573 if (dp->width_arg_index != ARG_NONE)
575 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
577 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
579 if (dp->precision_arg_index != ARG_NONE)
581 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
583 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
587 /* Prepare checking whether snprintf returns the count
589 ENSURE_ALLOCATION (xsum (length, 1));
590 result[length] = '\0';
599 maxlen = allocated - length;
604 # define SNPRINTF_BUF(arg) \
605 switch (prefix_count) \
608 retcount = SNPRINTF (result + length, maxlen, buf, \
612 retcount = SNPRINTF (result + length, maxlen, buf, \
613 prefixes[0], arg, &count); \
616 retcount = SNPRINTF (result + length, maxlen, buf, \
617 prefixes[0], prefixes[1], arg, \
624 # define SNPRINTF_BUF(arg) \
625 switch (prefix_count) \
628 count = sprintf (tmp, buf, arg); \
631 count = sprintf (tmp, buf, prefixes[0], arg); \
634 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
646 int arg = a.arg[dp->arg_index].a.a_schar;
652 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
658 int arg = a.arg[dp->arg_index].a.a_short;
664 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
670 int arg = a.arg[dp->arg_index].a.a_int;
676 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
682 long int arg = a.arg[dp->arg_index].a.a_longint;
688 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
692 #ifdef HAVE_LONG_LONG
693 case TYPE_LONGLONGINT:
695 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
699 case TYPE_ULONGLONGINT:
701 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
708 double arg = a.arg[dp->arg_index].a.a_double;
712 #ifdef HAVE_LONG_DOUBLE
713 case TYPE_LONGDOUBLE:
715 long double arg = a.arg[dp->arg_index].a.a_longdouble;
722 int arg = a.arg[dp->arg_index].a.a_char;
729 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
736 const char *arg = a.arg[dp->arg_index].a.a_string;
741 case TYPE_WIDE_STRING:
743 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
750 void *arg = a.arg[dp->arg_index].a.a_pointer;
759 /* Portability: Not all implementations of snprintf()
760 are ISO C 99 compliant. Determine the number of
761 bytes that snprintf() has produced or would have
765 /* Verify that snprintf() has NUL-terminated its
767 if (count < maxlen && result[length + count] != '\0')
769 /* Portability hack. */
770 if (retcount > count)
775 /* snprintf() doesn't understand the '%n'
779 /* Don't use the '%n' directive; instead, look
780 at the snprintf() return value. */
786 /* Look at the snprintf() return value. */
789 /* HP-UX 10.20 snprintf() is doubly deficient:
790 It doesn't understand the '%n' directive,
791 *and* it returns -1 (rather than the length
792 that would have been required) when the
793 buffer is too small. */
795 xsum (xtimes (allocated, 2), 12);
796 ENSURE_ALLOCATION (bigger_need);
805 /* Attempt to handle failure. */
808 if (!(result == resultbuf || result == NULL))
810 if (buf_malloced != NULL)
818 if (count >= tmp_length)
819 /* tmp_length was incorrectly calculated - fix the
824 /* Make room for the result. */
827 /* Need at least count bytes. But allocate
828 proportionally, to avoid looping eternally if
829 snprintf() reports a too small count. */
831 xmax (xsum (length, count), xtimes (allocated, 2));
833 ENSURE_ALLOCATION (n);
840 /* The snprintf() result did fit. */
842 /* Append the sprintf() result. */
843 memcpy (result + length, tmp, count * sizeof (CHAR_T));
855 /* Add the final NUL. */
856 ENSURE_ALLOCATION (xsum (length, 1));
857 result[length] = '\0';
859 if (result != resultbuf && length + 1 < allocated)
861 /* Shrink the allocated memory if possible. */
864 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
869 if (buf_malloced != NULL)
873 if (length > INT_MAX)
874 goto length_overflow;
878 /* We could produce such a big string, but its length doesn't fit into
879 an 'int'. POSIX says that snprintf() fails with errno = EOVERFLOW in
881 if (result != resultbuf)
887 if (!(result == resultbuf || result == NULL))
889 if (buf_malloced != NULL)