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. */
418 if (tmp_length
<= SIZE_MAX
/ 2)
423 # if HAVE_LONG_DOUBLE
424 if (type
== TYPE_LONGDOUBLE
)
426 (unsigned int) (LDBL_MAX_EXP
427 * 0.30103 /* binary -> decimal */
428 * 2 /* estimate for FLAG_GROUP */
430 + 1 /* turn floor into ceil */
431 + 10; /* sign, decimal point etc. */
435 (unsigned int) (DBL_MAX_EXP
436 * 0.30103 /* binary -> decimal */
437 * 2 /* estimate for FLAG_GROUP */
439 + 1 /* turn floor into ceil */
440 + 10; /* sign, decimal point etc. */
441 tmp_length
+= precision
;
442 if (tmp_length
< precision
)
446 case 'e': case 'E': case 'g': case 'G':
448 12; /* sign, decimal point, exponent etc. */
449 tmp_length
+= precision
;
450 if (tmp_length
< precision
)
455 # if HAVE_LONG_DOUBLE
456 if (type
== TYPE_LONGDOUBLE
)
458 (unsigned int) (LDBL_DIG
459 * 0.831 /* decimal -> hexadecimal */
461 + 1; /* turn floor into ceil */
465 (unsigned int) (DBL_DIG
466 * 0.831 /* decimal -> hexadecimal */
468 + 1; /* turn floor into ceil */
469 if (tmp_length
< precision
)
470 tmp_length
= precision
;
471 /* Account for sign, decimal point etc. */
478 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
479 if (type
== TYPE_WIDE_CHAR
)
480 tmp_length
= MB_CUR_MAX
;
488 if (type
== TYPE_WIDE_STRING
)
491 local_wcslen (a
.arg
[dp
->arg_index
].a
.a_wide_string
);
493 # if !WIDE_CHAR_VERSION
494 if (SIZE_MAX
/ MB_CUR_MAX
< tmp_length
)
496 tmp_length
*= MB_CUR_MAX
;
501 tmp_length
= strlen (a
.arg
[dp
->arg_index
].a
.a_string
);
506 (unsigned int) (sizeof (void *) * CHAR_BIT
507 * 0.25 /* binary -> hexadecimal */
509 + 1 /* turn floor into ceil */
510 + 2; /* account for leading 0x */
517 if (tmp_length
< width
)
520 tmp_length
++; /* account for trailing NUL */
525 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (CHAR_T
))
529 if (SIZE_MAX
/ sizeof (CHAR_T
) < tmp_length
)
530 /* Overflow, would lead to out of memory. */
532 tmp
= (CHAR_T
*) malloc (tmp_length
* sizeof (CHAR_T
));
539 /* Construct the format string for calling snprintf or
543 if (dp
->flags
& FLAG_GROUP
)
545 if (dp
->flags
& FLAG_LEFT
)
547 if (dp
->flags
& FLAG_SHOWSIGN
)
549 if (dp
->flags
& FLAG_SPACE
)
551 if (dp
->flags
& FLAG_ALT
)
553 if (dp
->flags
& FLAG_ZERO
)
555 if (dp
->width_start
!= dp
->width_end
)
557 size_t n
= dp
->width_end
- dp
->width_start
;
558 memcpy (p
, dp
->width_start
, n
* sizeof (CHAR_T
));
561 if (dp
->precision_start
!= dp
->precision_end
)
563 size_t n
= dp
->precision_end
- dp
->precision_start
;
564 memcpy (p
, dp
->precision_start
, n
* sizeof (CHAR_T
));
570 #if HAVE_LONG_LONG_INT
571 case TYPE_LONGLONGINT
:
572 case TYPE_ULONGLONGINT
:
582 case TYPE_WIDE_STRING
:
587 case TYPE_LONGDOUBLE
:
603 /* Construct the arguments for calling snprintf or sprintf. */
605 if (dp
->width_arg_index
!= ARG_NONE
)
607 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
609 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
611 if (dp
->precision_arg_index
!= ARG_NONE
)
613 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
615 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
619 /* Prepare checking whether snprintf returns the count
621 ENSURE_ALLOCATION (1);
622 result
[length
] = '\0';
631 maxlen
= allocated
- length
;
636 # define SNPRINTF_BUF(arg) \
637 switch (prefix_count) \
640 retcount = SNPRINTF (result + length, maxlen, buf, \
644 retcount = SNPRINTF (result + length, maxlen, buf, \
645 prefixes[0], arg, &count); \
648 retcount = SNPRINTF (result + length, maxlen, buf, \
649 prefixes[0], prefixes[1], arg, \
656 # define SNPRINTF_BUF(arg) \
657 switch (prefix_count) \
660 count = sprintf (tmp, buf, arg); \
663 count = sprintf (tmp, buf, prefixes[0], arg); \
666 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
678 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
684 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
690 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
696 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
702 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
708 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
714 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
720 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
724 #if HAVE_LONG_LONG_INT
725 case TYPE_LONGLONGINT
:
727 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
731 case TYPE_ULONGLONGINT
:
733 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
740 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
745 case TYPE_LONGDOUBLE
:
747 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
754 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
761 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
768 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
773 case TYPE_WIDE_STRING
:
775 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
782 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
791 /* Portability: Not all implementations of snprintf()
792 are ISO C 99 compliant. Determine the number of
793 bytes that snprintf() has produced or would have
797 /* Verify that snprintf() has NUL-terminated its
799 if (count
< maxlen
&& result
[length
+ count
] != '\0')
801 /* Portability hack. */
802 if (retcount
> count
)
807 /* snprintf() doesn't understand the '%n'
811 /* Don't use the '%n' directive; instead, look
812 at the snprintf() return value. */
818 /* Look at the snprintf() return value. */
821 /* HP-UX 10.20 snprintf() is doubly deficient:
822 It doesn't understand the '%n' directive,
823 *and* it returns -1 (rather than the length
824 that would have been required) when the
825 buffer is too small. */
827 (allocated
> 12 ? allocated
: 12);
828 ENSURE_ALLOCATION (bigger_need
);
837 /* Attempt to handle failure. */
840 if (!(result
== resultbuf
|| result
== NULL
))
842 if (buf_malloced
!= NULL
)
850 if (count
>= tmp_length
)
851 /* tmp_length was incorrectly calculated - fix the
856 /* Make room for the result. */
859 /* Need at least count bytes. But allocate
860 proportionally, to avoid looping eternally if
861 snprintf() reports a too small count. */
862 ENSURE_ALLOCATION (count
< allocated
863 ? allocated
: count
);
870 /* The snprintf() result did fit. */
872 /* Append the sprintf() result. */
873 memcpy (result
+ length
, tmp
, count
* sizeof (CHAR_T
));
885 /* Add the final NUL. */
886 ENSURE_ALLOCATION (1);
887 result
[length
] = '\0';
889 if (result
!= resultbuf
&& length
+ 1 < allocated
)
891 /* Shrink the allocated memory if possible. */
894 memory
= (CHAR_T
*) realloc (result
, (length
+ 1) * sizeof (CHAR_T
));
899 if (buf_malloced
!= NULL
)
903 if (length
> INT_MAX
)
904 goto length_overflow
;
908 /* We could produce such a big string, but its length doesn't fit into
909 an 'int'. POSIX says that snprintf() fails with errno = EOVERFLOW in
911 if (result
!= resultbuf
)
917 if (!(result
== resultbuf
|| result
== NULL
))
919 if (buf_malloced
!= NULL
)