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
39 # include "vasnwprintf.h"
41 # include "vasnprintf.h"
44 #include <stdio.h> /* snprintf(), sprintf() */
45 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
46 #include <string.h> /* memcpy(), strlen() */
47 #include <errno.h> /* errno */
48 #include <limits.h> /* CHAR_BIT, INT_MAX */
49 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
51 # include "wprintf-parse.h"
53 # include "printf-parse.h"
56 /* Checked size_t computations. */
59 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
61 # define EOVERFLOW E2BIG
66 # define local_wcslen wcslen
68 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
69 a dependency towards this library, here is a local substitute.
70 Define this substitute only once, even if this file is included
71 twice in the same compilation unit. */
72 # ifndef local_wcslen_defined
73 # define local_wcslen_defined 1
75 local_wcslen (const wchar_t *s
)
79 for (ptr
= s
; *ptr
!= (wchar_t) 0; ptr
++)
88 # define VASNPRINTF vasnwprintf
89 # define CHAR_T wchar_t
90 # define DIRECTIVE wchar_t_directive
91 # define DIRECTIVES wchar_t_directives
92 # define PRINTF_PARSE wprintf_parse
93 # define USE_SNPRINTF 1
94 # if HAVE_DECL__SNWPRINTF
95 /* On Windows, the function swprintf() has a different signature than
96 on Unix; we use the _snwprintf() function instead. */
97 # define SNPRINTF _snwprintf
100 # define SNPRINTF swprintf
103 # define VASNPRINTF vasnprintf
105 # define DIRECTIVE char_directive
106 # define DIRECTIVES char_directives
107 # define PRINTF_PARSE printf_parse
108 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
109 # if HAVE_DECL__SNPRINTF
111 # define SNPRINTF _snprintf
114 # define SNPRINTF snprintf
119 VASNPRINTF (CHAR_T
*resultbuf
, size_t *lengthp
, const CHAR_T
*format
, va_list args
)
124 if (PRINTF_PARSE (format
, &d
, &a
) < 0)
135 if (printf_fetchargs (args
, &a
) < 0)
143 size_t buf_neededlength
;
145 CHAR_T
*buf_malloced
;
149 /* Output string accumulator. */
154 /* Allocate a small buffer that will hold a directive passed to
155 sprintf or snprintf. */
157 xsum4 (7, d
.max_width_length
, d
.max_precision_length
, 6);
159 if (buf_neededlength
< 4000 / sizeof (CHAR_T
))
161 buf
= (CHAR_T
*) alloca (buf_neededlength
* sizeof (CHAR_T
));
167 size_t buf_memsize
= xtimes (buf_neededlength
, sizeof (CHAR_T
));
168 if (size_overflow_p (buf_memsize
))
169 goto out_of_memory_1
;
170 buf
= (CHAR_T
*) malloc (buf_memsize
);
172 goto out_of_memory_1
;
176 if (resultbuf
!= NULL
)
179 allocated
= *lengthp
;
188 result is either == resultbuf or == NULL or malloc-allocated.
189 If length > 0, then result != NULL. */
191 /* Ensures that allocated >= needed. Aborts through a jump to
192 out_of_memory if needed is SIZE_MAX or otherwise too big. */
193 #define ENSURE_ALLOCATION(needed) \
194 if ((needed) > allocated) \
196 size_t memory_size; \
199 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
200 if ((needed) > allocated) \
201 allocated = (needed); \
202 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
203 if (size_overflow_p (memory_size)) \
204 goto out_of_memory; \
205 if (result == resultbuf || result == NULL) \
206 memory = (CHAR_T *) malloc (memory_size); \
208 memory = (CHAR_T *) realloc (result, memory_size); \
209 if (memory == NULL) \
210 goto out_of_memory; \
211 if (result == resultbuf && length > 0) \
212 memcpy (memory, result, length * sizeof (CHAR_T)); \
216 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
218 if (cp
!= dp
->dir_start
)
220 size_t n
= dp
->dir_start
- cp
;
221 size_t augmented_length
= xsum (length
, n
);
223 ENSURE_ALLOCATION (augmented_length
);
224 memcpy (result
+ length
, cp
, n
* sizeof (CHAR_T
));
225 length
= augmented_length
;
230 /* Execute a single directive. */
231 if (dp
->conversion
== '%')
233 size_t augmented_length
;
235 if (!(dp
->arg_index
== ARG_NONE
))
237 augmented_length
= xsum (length
, 1);
238 ENSURE_ALLOCATION (augmented_length
);
239 result
[length
] = '%';
240 length
= augmented_length
;
244 if (!(dp
->arg_index
!= ARG_NONE
))
247 if (dp
->conversion
== 'n')
249 switch (a
.arg
[dp
->arg_index
].type
)
251 case TYPE_COUNT_SCHAR_POINTER
:
252 *a
.arg
[dp
->arg_index
].a
.a_count_schar_pointer
= length
;
254 case TYPE_COUNT_SHORT_POINTER
:
255 *a
.arg
[dp
->arg_index
].a
.a_count_short_pointer
= length
;
257 case TYPE_COUNT_INT_POINTER
:
258 *a
.arg
[dp
->arg_index
].a
.a_count_int_pointer
= length
;
260 case TYPE_COUNT_LONGINT_POINTER
:
261 *a
.arg
[dp
->arg_index
].a
.a_count_longint_pointer
= length
;
263 #ifdef HAVE_LONG_LONG
264 case TYPE_COUNT_LONGLONGINT_POINTER
:
265 *a
.arg
[dp
->arg_index
].a
.a_count_longlongint_pointer
= length
;
274 arg_type type
= a
.arg
[dp
->arg_index
].type
;
276 unsigned int prefix_count
;
283 /* Allocate a temporary buffer of sufficient size for calling
290 if (dp
->width_start
!= dp
->width_end
)
292 if (dp
->width_arg_index
!= ARG_NONE
)
296 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
298 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
299 width
= (arg
< 0 ? (unsigned int) (-arg
) : arg
);
303 const CHAR_T
*digitp
= dp
->width_start
;
306 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
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
)
329 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
333 switch (dp
->conversion
)
336 case 'd': case 'i': case 'u':
337 # ifdef HAVE_LONG_LONG
338 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
340 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
341 * 0.30103 /* binary -> decimal */
342 * 2 /* estimate for FLAG_GROUP */
344 + 1 /* turn floor into ceil */
345 + 1; /* account for leading sign */
348 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
350 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
351 * 0.30103 /* binary -> decimal */
352 * 2 /* estimate for FLAG_GROUP */
354 + 1 /* turn floor into ceil */
355 + 1; /* account for leading sign */
358 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
359 * 0.30103 /* binary -> decimal */
360 * 2 /* estimate for FLAG_GROUP */
362 + 1 /* turn floor into ceil */
363 + 1; /* account for leading sign */
367 # ifdef HAVE_LONG_LONG
368 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
370 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
371 * 0.333334 /* binary -> octal */
373 + 1 /* turn floor into ceil */
374 + 1; /* account for leading sign */
377 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
379 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
380 * 0.333334 /* binary -> octal */
382 + 1 /* turn floor into ceil */
383 + 1; /* account for leading sign */
386 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
387 * 0.333334 /* binary -> octal */
389 + 1 /* turn floor into ceil */
390 + 1; /* account for leading sign */
394 # ifdef HAVE_LONG_LONG
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 */
401 + 2; /* account for leading sign or alternate form */
404 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
406 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
407 * 0.25 /* binary -> hexadecimal */
409 + 1 /* turn floor into ceil */
410 + 2; /* account for leading sign or alternate form */
413 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
414 * 0.25 /* binary -> hexadecimal */
416 + 1 /* turn floor into ceil */
417 + 2; /* account for leading sign or alternate form */
421 # ifdef HAVE_LONG_DOUBLE
422 if (type
== TYPE_LONGDOUBLE
)
424 (unsigned int) (LDBL_MAX_EXP
425 * 0.30103 /* binary -> decimal */
426 * 2 /* estimate for FLAG_GROUP */
428 + 1 /* turn floor into ceil */
429 + 10; /* sign, decimal point etc. */
433 (unsigned int) (DBL_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. */
439 tmp_length
= xsum (tmp_length
, precision
);
442 case 'e': case 'E': case 'g': case 'G':
445 12; /* sign, decimal point, exponent etc. */
446 tmp_length
= xsum (tmp_length
, precision
);
450 # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION
451 if (type
== TYPE_WIDE_CHAR
)
452 tmp_length
= MB_CUR_MAX
;
460 if (type
== TYPE_WIDE_STRING
)
463 local_wcslen (a
.arg
[dp
->arg_index
].a
.a_wide_string
);
465 # if !WIDE_CHAR_VERSION
466 tmp_length
= xtimes (tmp_length
, MB_CUR_MAX
);
471 tmp_length
= strlen (a
.arg
[dp
->arg_index
].a
.a_string
);
476 (unsigned int) (sizeof (void *) * CHAR_BIT
477 * 0.25 /* binary -> hexadecimal */
479 + 1 /* turn floor into ceil */
480 + 2; /* account for leading 0x */
487 if (tmp_length
< width
)
490 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
493 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (CHAR_T
))
497 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (CHAR_T
));
499 if (size_overflow_p (tmp_memsize
))
500 /* Overflow, would lead to out of memory. */
502 tmp
= (CHAR_T
*) malloc (tmp_memsize
);
509 /* Construct the format string for calling snprintf or
513 if (dp
->flags
& FLAG_GROUP
)
515 if (dp
->flags
& FLAG_LEFT
)
517 if (dp
->flags
& FLAG_SHOWSIGN
)
519 if (dp
->flags
& FLAG_SPACE
)
521 if (dp
->flags
& FLAG_ALT
)
523 if (dp
->flags
& FLAG_ZERO
)
525 if (dp
->width_start
!= dp
->width_end
)
527 size_t n
= dp
->width_end
- dp
->width_start
;
528 memcpy (p
, dp
->width_start
, n
* sizeof (CHAR_T
));
531 if (dp
->precision_start
!= dp
->precision_end
)
533 size_t n
= dp
->precision_end
- dp
->precision_start
;
534 memcpy (p
, dp
->precision_start
, n
* sizeof (CHAR_T
));
540 #ifdef HAVE_LONG_LONG
541 case TYPE_LONGLONGINT
:
542 case TYPE_ULONGLONGINT
:
552 case TYPE_WIDE_STRING
:
556 #ifdef HAVE_LONG_DOUBLE
557 case TYPE_LONGDOUBLE
:
573 /* Construct the arguments for calling snprintf or sprintf. */
575 if (dp
->width_arg_index
!= ARG_NONE
)
577 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
579 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
581 if (dp
->precision_arg_index
!= ARG_NONE
)
583 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
585 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
589 /* Prepare checking whether snprintf returns the count
591 ENSURE_ALLOCATION (xsum (length
, 1));
592 result
[length
] = '\0';
601 maxlen
= allocated
- length
;
606 # define SNPRINTF_BUF(arg) \
607 switch (prefix_count) \
610 retcount = SNPRINTF (result + length, maxlen, buf, \
614 retcount = SNPRINTF (result + length, maxlen, buf, \
615 prefixes[0], arg, &count); \
618 retcount = SNPRINTF (result + length, maxlen, buf, \
619 prefixes[0], prefixes[1], arg, \
626 # define SNPRINTF_BUF(arg) \
627 switch (prefix_count) \
630 count = sprintf (tmp, buf, arg); \
633 count = sprintf (tmp, buf, prefixes[0], arg); \
636 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
648 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
654 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
660 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
666 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
672 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
678 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
684 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
690 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
694 #ifdef HAVE_LONG_LONG
695 case TYPE_LONGLONGINT
:
697 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
701 case TYPE_ULONGLONGINT
:
703 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
710 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
714 #ifdef HAVE_LONG_DOUBLE
715 case TYPE_LONGDOUBLE
:
717 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
724 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
731 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
738 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
743 case TYPE_WIDE_STRING
:
745 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
752 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
761 /* Portability: Not all implementations of snprintf()
762 are ISO C 99 compliant. Determine the number of
763 bytes that snprintf() has produced or would have
767 /* Verify that snprintf() has NUL-terminated its
769 if (count
< maxlen
&& result
[length
+ count
] != '\0')
771 /* Portability hack. */
772 if (retcount
> count
)
777 /* snprintf() doesn't understand the '%n'
781 /* Don't use the '%n' directive; instead, look
782 at the snprintf() return value. */
788 /* Look at the snprintf() return value. */
791 /* HP-UX 10.20 snprintf() is doubly deficient:
792 It doesn't understand the '%n' directive,
793 *and* it returns -1 (rather than the length
794 that would have been required) when the
795 buffer is too small. */
797 xsum (xtimes (allocated
, 2), 12);
798 ENSURE_ALLOCATION (bigger_need
);
807 /* Attempt to handle failure. */
810 if (!(result
== resultbuf
|| result
== NULL
))
812 if (buf_malloced
!= NULL
)
820 if (count
>= tmp_length
)
821 /* tmp_length was incorrectly calculated - fix the
826 /* Make room for the result. */
829 /* Need at least count bytes. But allocate
830 proportionally, to avoid looping eternally if
831 snprintf() reports a too small count. */
833 xmax (xsum (length
, count
), xtimes (allocated
, 2));
835 ENSURE_ALLOCATION (n
);
842 /* The snprintf() result did fit. */
844 /* Append the sprintf() result. */
845 memcpy (result
+ length
, tmp
, count
* sizeof (CHAR_T
));
857 /* Add the final NUL. */
858 ENSURE_ALLOCATION (xsum (length
, 1));
859 result
[length
] = '\0';
861 if (result
!= resultbuf
&& length
+ 1 < allocated
)
863 /* Shrink the allocated memory if possible. */
866 memory
= (CHAR_T
*) realloc (result
, (length
+ 1) * sizeof (CHAR_T
));
871 if (buf_malloced
!= NULL
)
875 if (length
> INT_MAX
)
876 goto length_overflow
;
880 /* We could produce such a big string, but its length doesn't fit into
881 an 'int'. POSIX says that snprintf() fails with errno = EOVERFLOW in
883 if (result
!= resultbuf
)
889 if (!(result
== resultbuf
|| result
== NULL
))
891 if (buf_malloced
!= NULL
)