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
33 # include "vasnwprintf.h"
35 # include "vasnprintf.h"
38 #include <stdio.h> /* snprintf(), sprintf() */
39 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
40 #include <string.h> /* memcpy(), strlen() */
41 #include <errno.h> /* errno */
42 #include <limits.h> /* CHAR_BIT, INT_MAX */
43 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
45 # include "wprintf-parse.h"
47 # include "printf-parse.h"
51 # define SIZE_MAX ((size_t) -1)
54 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
56 # define EOVERFLOW E2BIG
61 # define local_wcslen wcslen
63 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
64 a dependency towards this library, here is a local substitute.
65 Define this substitute only once, even if this file is included
66 twice in the same compilation unit. */
67 # ifndef local_wcslen_defined
68 # define local_wcslen_defined 1
70 local_wcslen (const wchar_t *s
)
74 for (ptr
= s
; *ptr
!= (wchar_t) 0; ptr
++)
83 # define VASNPRINTF vasnwprintf
84 # define CHAR_T wchar_t
85 # define DIRECTIVE wchar_t_directive
86 # define DIRECTIVES wchar_t_directives
87 # define PRINTF_PARSE wprintf_parse
88 # define USE_SNPRINTF 1
89 # if HAVE_DECL__SNWPRINTF
90 /* On Windows, the function swprintf() has a different signature than
91 on Unix; we use the _snwprintf() function instead. */
92 # define SNPRINTF _snwprintf
95 # define SNPRINTF swprintf
98 # define VASNPRINTF vasnprintf
100 # define DIRECTIVE char_directive
101 # define DIRECTIVES char_directives
102 # define PRINTF_PARSE printf_parse
103 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
104 # if HAVE_DECL__SNPRINTF
106 # define SNPRINTF _snprintf
109 # define SNPRINTF snprintf
114 VASNPRINTF (CHAR_T
*resultbuf
, size_t *lengthp
, const CHAR_T
*format
, va_list args
)
119 if (PRINTF_PARSE (format
, &d
, &a
) < 0)
130 if (printf_fetchargs (args
, &a
) < 0)
138 size_t buf_neededlength
;
140 CHAR_T
*buf_malloced
;
144 /* Output string accumulator. */
149 /* Allocate a small buffer that will hold a directive passed to
150 sprintf or snprintf. */
151 buf_neededlength
= 7 + d
.max_width_length
+ d
.max_precision_length
+ 6;
153 if (buf_neededlength
< 4000 / sizeof (CHAR_T
))
155 buf
= (CHAR_T
*) alloca (buf_neededlength
* sizeof (CHAR_T
));
161 if (SIZE_MAX
/ sizeof (CHAR_T
) < buf_neededlength
)
162 goto out_of_memory_1
;
163 buf
= (CHAR_T
*) malloc (buf_neededlength
* sizeof (CHAR_T
));
165 goto out_of_memory_1
;
169 if (resultbuf
!= NULL
)
172 allocated
= *lengthp
;
181 result is either == resultbuf or == NULL or malloc-allocated.
182 If length > 0, then result != NULL. */
184 /* Ensures that allocated >= length + extra. Aborts through a jump to
185 out_of_memory if size is too big. */
186 #define ENSURE_ALLOCATION(extra) \
188 size_t needed = length + (extra); \
189 if (needed < length) \
190 goto out_of_memory; \
191 if (needed > allocated) \
193 size_t memory_size; \
196 allocated = (allocated > 0 ? 2 * allocated : 12); \
197 if (needed > allocated) \
198 allocated = needed; \
199 if (SIZE_MAX / sizeof (CHAR_T) < allocated) \
200 goto out_of_memory; \
201 memory_size = allocated * sizeof (CHAR_T); \
202 if (result == resultbuf || result == NULL) \
203 memory = (CHAR_T *) malloc (memory_size); \
205 memory = (CHAR_T *) realloc (result, memory_size); \
206 if (memory == NULL) \
207 goto out_of_memory; \
208 if (result == resultbuf && length > 0) \
209 memcpy (memory, result, length * sizeof (CHAR_T)); \
214 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
216 if (cp
!= dp
->dir_start
)
218 size_t n
= dp
->dir_start
- cp
;
220 ENSURE_ALLOCATION (n
);
221 memcpy (result
+ length
, cp
, n
* sizeof (CHAR_T
));
227 /* Execute a single directive. */
228 if (dp
->conversion
== '%')
230 if (!(dp
->arg_index
== ARG_NONE
))
232 ENSURE_ALLOCATION (1);
233 result
[length
] = '%';
238 if (!(dp
->arg_index
!= ARG_NONE
))
241 if (dp
->conversion
== 'n')
243 switch (a
.arg
[dp
->arg_index
].type
)
245 case TYPE_COUNT_SCHAR_POINTER
:
246 *a
.arg
[dp
->arg_index
].a
.a_count_schar_pointer
= length
;
248 case TYPE_COUNT_SHORT_POINTER
:
249 *a
.arg
[dp
->arg_index
].a
.a_count_short_pointer
= length
;
251 case TYPE_COUNT_INT_POINTER
:
252 *a
.arg
[dp
->arg_index
].a
.a_count_int_pointer
= length
;
254 case TYPE_COUNT_LONGINT_POINTER
:
255 *a
.arg
[dp
->arg_index
].a
.a_count_longint_pointer
= length
;
257 #ifdef HAVE_LONG_LONG
258 case TYPE_COUNT_LONGLONGINT_POINTER
:
259 *a
.arg
[dp
->arg_index
].a
.a_count_longlongint_pointer
= length
;
268 arg_type type
= a
.arg
[dp
->arg_index
].type
;
270 unsigned int prefix_count
;
277 /* Allocate a temporary buffer of sufficient size for calling
284 if (dp
->width_start
!= dp
->width_end
)
286 if (dp
->width_arg_index
!= ARG_NONE
)
290 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
292 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
293 width
= (arg
< 0 ? (unsigned int) (-arg
) : arg
);
297 const CHAR_T
*digitp
= dp
->width_start
;
301 size_t w_tmp
= width
* 10 + (*digitp
++ - '0');
302 if (SIZE_MAX
/ 10 < width
|| w_tmp
< width
)
306 while (digitp
!= dp
->width_end
);
311 if (dp
->precision_start
!= dp
->precision_end
)
313 if (dp
->precision_arg_index
!= ARG_NONE
)
317 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
319 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
320 precision
= (arg
< 0 ? 0 : arg
);
324 const CHAR_T
*digitp
= dp
->precision_start
+ 1;
327 while (digitp
!= dp
->precision_end
)
329 size_t p1
= 10 * precision
+ (*digitp
++ - '0');
330 precision
= ((SIZE_MAX
/ 10 < precision
337 switch (dp
->conversion
)
340 case 'd': case 'i': case 'u':
341 # ifdef HAVE_LONG_LONG
342 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
344 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
345 * 0.30103 /* binary -> decimal */
347 + 1; /* turn floor into ceil */
350 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
352 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
353 * 0.30103 /* binary -> decimal */
355 + 1; /* turn floor into ceil */
358 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
359 * 0.30103 /* binary -> decimal */
361 + 1; /* turn floor into ceil */
362 if (tmp_length
< precision
)
363 tmp_length
= precision
;
364 /* Multiply by 2, as an estimate for FLAG_GROUP. */
365 /* Add 1, to account for a leading sign. */
366 tmp_length
= (tmp_length
< SIZE_MAX
/ 2
372 # ifdef HAVE_LONG_LONG
373 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
375 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
376 * 0.333334 /* binary -> octal */
378 + 1; /* turn floor into ceil */
381 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
383 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
384 * 0.333334 /* binary -> octal */
386 + 1; /* turn floor into ceil */
389 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
390 * 0.333334 /* binary -> octal */
392 + 1; /* turn floor into ceil */
393 if (tmp_length
< precision
)
394 tmp_length
= precision
;
395 /* Add 1, to account for a leading sign. */
396 tmp_length
+= (tmp_length
< SIZE_MAX
);
400 # ifdef HAVE_LONG_LONG
401 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
403 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
404 * 0.25 /* binary -> hexadecimal */
406 + 1; /* turn floor into ceil */
409 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
411 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
412 * 0.25 /* binary -> hexadecimal */
414 + 1; /* turn floor into ceil */
417 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
418 * 0.25 /* binary -> hexadecimal */
420 + 1; /* turn floor into ceil */
421 if (tmp_length
< precision
)
422 tmp_length
= precision
;
423 /* Add 2, to account for a leading sign or alternate form. */
424 if (tmp_length
<= SIZE_MAX
/ 2)
429 # ifdef HAVE_LONG_DOUBLE
430 if (type
== TYPE_LONGDOUBLE
)
432 (unsigned int) (LDBL_MAX_EXP
433 * 0.30103 /* binary -> decimal */
434 * 2 /* estimate for FLAG_GROUP */
436 + 1 /* turn floor into ceil */
437 + 10; /* sign, decimal point etc. */
441 (unsigned int) (DBL_MAX_EXP
442 * 0.30103 /* binary -> decimal */
443 * 2 /* estimate for FLAG_GROUP */
445 + 1 /* turn floor into ceil */
446 + 10; /* sign, decimal point etc. */
447 tmp_length
+= precision
;
448 if (tmp_length
< precision
)
452 case 'e': case 'E': case 'g': case 'G':
455 12; /* sign, decimal point, exponent etc. */
456 tmp_length
+= precision
;
457 if (tmp_length
< precision
)
462 # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION
463 if (type
== TYPE_WIDE_CHAR
)
464 tmp_length
= MB_CUR_MAX
;
472 if (type
== TYPE_WIDE_STRING
)
475 local_wcslen (a
.arg
[dp
->arg_index
].a
.a_wide_string
);
477 # if !WIDE_CHAR_VERSION
478 if (SIZE_MAX
/ MB_CUR_MAX
< tmp_length
)
480 tmp_length
*= MB_CUR_MAX
;
485 tmp_length
= strlen (a
.arg
[dp
->arg_index
].a
.a_string
);
490 (unsigned int) (sizeof (void *) * CHAR_BIT
491 * 0.25 /* binary -> hexadecimal */
493 + 1 /* turn floor into ceil */
494 + 2; /* account for leading 0x */
501 if (tmp_length
< width
)
504 tmp_length
++; /* account for trailing NUL */
509 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (CHAR_T
))
513 if (SIZE_MAX
/ sizeof (CHAR_T
) < tmp_length
)
514 /* Overflow, would lead to out of memory. */
516 tmp
= (CHAR_T
*) malloc (tmp_length
* sizeof (CHAR_T
));
523 /* Construct the format string for calling snprintf or
527 if (dp
->flags
& FLAG_GROUP
)
529 if (dp
->flags
& FLAG_LEFT
)
531 if (dp
->flags
& FLAG_SHOWSIGN
)
533 if (dp
->flags
& FLAG_SPACE
)
535 if (dp
->flags
& FLAG_ALT
)
537 if (dp
->flags
& FLAG_ZERO
)
539 if (dp
->width_start
!= dp
->width_end
)
541 size_t n
= dp
->width_end
- dp
->width_start
;
542 memcpy (p
, dp
->width_start
, n
* sizeof (CHAR_T
));
545 if (dp
->precision_start
!= dp
->precision_end
)
547 size_t n
= dp
->precision_end
- dp
->precision_start
;
548 memcpy (p
, dp
->precision_start
, n
* sizeof (CHAR_T
));
554 #ifdef HAVE_LONG_LONG
555 case TYPE_LONGLONGINT
:
556 case TYPE_ULONGLONGINT
:
566 case TYPE_WIDE_STRING
:
570 #ifdef HAVE_LONG_DOUBLE
571 case TYPE_LONGDOUBLE
:
587 /* Construct the arguments for calling snprintf or sprintf. */
589 if (dp
->width_arg_index
!= ARG_NONE
)
591 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
593 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
595 if (dp
->precision_arg_index
!= ARG_NONE
)
597 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
599 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
603 /* Prepare checking whether snprintf returns the count
605 ENSURE_ALLOCATION (1);
606 result
[length
] = '\0';
615 maxlen
= allocated
- length
;
620 # define SNPRINTF_BUF(arg) \
621 switch (prefix_count) \
624 retcount = SNPRINTF (result + length, maxlen, buf, \
628 retcount = SNPRINTF (result + length, maxlen, buf, \
629 prefixes[0], arg, &count); \
632 retcount = SNPRINTF (result + length, maxlen, buf, \
633 prefixes[0], prefixes[1], arg, \
640 # define SNPRINTF_BUF(arg) \
641 switch (prefix_count) \
644 count = sprintf (tmp, buf, arg); \
647 count = sprintf (tmp, buf, prefixes[0], arg); \
650 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
662 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
668 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
674 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
680 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
686 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
692 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
698 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
704 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
708 #ifdef HAVE_LONG_LONG
709 case TYPE_LONGLONGINT
:
711 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
715 case TYPE_ULONGLONGINT
:
717 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
724 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
728 #ifdef HAVE_LONG_DOUBLE
729 case TYPE_LONGDOUBLE
:
731 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
738 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
745 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
752 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
757 case TYPE_WIDE_STRING
:
759 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
766 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
775 /* Portability: Not all implementations of snprintf()
776 are ISO C 99 compliant. Determine the number of
777 bytes that snprintf() has produced or would have
781 /* Verify that snprintf() has NUL-terminated its
783 if (count
< maxlen
&& result
[length
+ count
] != '\0')
785 /* Portability hack. */
786 if (retcount
> count
)
791 /* snprintf() doesn't understand the '%n'
795 /* Don't use the '%n' directive; instead, look
796 at the snprintf() return value. */
802 /* Look at the snprintf() return value. */
805 /* HP-UX 10.20 snprintf() is doubly deficient:
806 It doesn't understand the '%n' directive,
807 *and* it returns -1 (rather than the length
808 that would have been required) when the
809 buffer is too small. */
811 (allocated
> 12 ? allocated
: 12);
812 ENSURE_ALLOCATION (bigger_need
);
821 /* Attempt to handle failure. */
824 if (!(result
== resultbuf
|| result
== NULL
))
826 if (buf_malloced
!= NULL
)
834 if (count
>= tmp_length
)
835 /* tmp_length was incorrectly calculated - fix the
840 /* Make room for the result. */
843 /* Need at least count bytes. But allocate
844 proportionally, to avoid looping eternally if
845 snprintf() reports a too small count. */
846 ENSURE_ALLOCATION (count
< allocated
847 ? allocated
: count
);
854 /* The snprintf() result did fit. */
856 /* Append the sprintf() result. */
857 memcpy (result
+ length
, tmp
, count
* sizeof (CHAR_T
));
869 /* Add the final NUL. */
870 ENSURE_ALLOCATION (1);
871 result
[length
] = '\0';
873 if (result
!= resultbuf
&& length
+ 1 < allocated
)
875 /* Shrink the allocated memory if possible. */
878 memory
= (CHAR_T
*) realloc (result
, (length
+ 1) * sizeof (CHAR_T
));
883 if (buf_malloced
!= NULL
)
887 if (length
> INT_MAX
)
888 goto length_overflow
;
892 /* We could produce such a big string, but its length doesn't fit into
893 an 'int'. POSIX says that snprintf() fails with errno = EOVERFLOW in
895 if (result
!= resultbuf
)
901 if (!(result
== resultbuf
|| result
== NULL
))
903 if (buf_malloced
!= NULL
)