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
32 # include "vasnwprintf.h"
34 # include "vasnprintf.h"
37 #include <stdio.h> /* snprintf(), sprintf() */
38 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
39 #include <string.h> /* memcpy(), strlen() */
40 #include <errno.h> /* errno */
41 #include <limits.h> /* CHAR_BIT */
42 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
44 # include "wprintf-parse.h"
46 # include "printf-parse.h"
49 /* Checked size_t computations. */
54 # define local_wcslen wcslen
56 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
57 a dependency towards this library, here is a local substitute.
58 Define this substitute only once, even if this file is included
59 twice in the same compilation unit. */
60 # ifndef local_wcslen_defined
61 # define local_wcslen_defined 1
63 local_wcslen (const wchar_t *s
)
67 for (ptr
= s
; *ptr
!= (wchar_t) 0; ptr
++)
76 # define VASNPRINTF vasnwprintf
77 # define CHAR_T wchar_t
78 # define DIRECTIVE wchar_t_directive
79 # define DIRECTIVES wchar_t_directives
80 # define PRINTF_PARSE wprintf_parse
81 # define USE_SNPRINTF 1
82 # if HAVE_DECL__SNWPRINTF
83 /* On Windows, the function swprintf() has a different signature than
84 on Unix; we use the _snwprintf() function instead. */
85 # define SNPRINTF _snwprintf
88 # define SNPRINTF swprintf
91 # define VASNPRINTF vasnprintf
93 # define DIRECTIVE char_directive
94 # define DIRECTIVES char_directives
95 # define PRINTF_PARSE printf_parse
96 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
97 # if HAVE_DECL__SNPRINTF
99 # define SNPRINTF _snprintf
102 # define SNPRINTF snprintf
107 VASNPRINTF (CHAR_T
*resultbuf
, size_t *lengthp
, const CHAR_T
*format
, va_list args
)
112 if (PRINTF_PARSE (format
, &d
, &a
) < 0)
123 if (printf_fetchargs (args
, &a
) < 0)
131 size_t buf_neededlength
;
133 CHAR_T
*buf_malloced
;
137 /* Output string accumulator. */
142 /* Allocate a small buffer that will hold a directive passed to
143 sprintf or snprintf. */
145 xsum4 (7, d
.max_width_length
, d
.max_precision_length
, 6);
147 if (buf_neededlength
< 4000 / sizeof (CHAR_T
))
149 buf
= (CHAR_T
*) alloca (buf_neededlength
* sizeof (CHAR_T
));
155 size_t buf_memsize
= xtimes (buf_neededlength
, sizeof (CHAR_T
));
156 if (size_overflow_p (buf_memsize
))
157 goto out_of_memory_1
;
158 buf
= (CHAR_T
*) malloc (buf_memsize
);
160 goto out_of_memory_1
;
164 if (resultbuf
!= NULL
)
167 allocated
= *lengthp
;
176 result is either == resultbuf or == NULL or malloc-allocated.
177 If length > 0, then result != NULL. */
179 /* Ensures that allocated >= needed. Aborts through a jump to
180 out_of_memory if needed is SIZE_MAX or otherwise too big. */
181 #define ENSURE_ALLOCATION(needed) \
182 if ((needed) > allocated) \
184 size_t memory_size; \
187 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
188 if ((needed) > allocated) \
189 allocated = (needed); \
190 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
191 if (size_overflow_p (memory_size)) \
192 goto out_of_memory; \
193 if (result == resultbuf || result == NULL) \
194 memory = (CHAR_T *) malloc (memory_size); \
196 memory = (CHAR_T *) realloc (result, memory_size); \
197 if (memory == NULL) \
198 goto out_of_memory; \
199 if (result == resultbuf && length > 0) \
200 memcpy (memory, result, length * sizeof (CHAR_T)); \
204 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
206 if (cp
!= dp
->dir_start
)
208 size_t n
= dp
->dir_start
- cp
;
209 size_t augmented_length
= xsum (length
, n
);
211 ENSURE_ALLOCATION (augmented_length
);
212 memcpy (result
+ length
, cp
, n
* sizeof (CHAR_T
));
213 length
= augmented_length
;
218 /* Execute a single directive. */
219 if (dp
->conversion
== '%')
221 size_t augmented_length
;
223 if (!(dp
->arg_index
== ARG_NONE
))
225 augmented_length
= xsum (length
, 1);
226 ENSURE_ALLOCATION (augmented_length
);
227 result
[length
] = '%';
228 length
= augmented_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 #ifdef 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
;
294 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
295 while (digitp
!= dp
->width_end
);
300 if (dp
->precision_start
!= dp
->precision_end
)
302 if (dp
->precision_arg_index
!= ARG_NONE
)
306 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
308 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
309 precision
= (arg
< 0 ? 0 : arg
);
313 const CHAR_T
*digitp
= dp
->precision_start
+ 1;
316 while (digitp
!= dp
->precision_end
)
317 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
321 switch (dp
->conversion
)
324 case 'd': case 'i': case 'u':
325 # ifdef HAVE_LONG_LONG_INT
326 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
328 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
329 * 0.30103 /* binary -> decimal */
331 + 1; /* turn floor into ceil */
334 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
336 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
337 * 0.30103 /* binary -> decimal */
339 + 1; /* turn floor into ceil */
342 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
343 * 0.30103 /* binary -> decimal */
345 + 1; /* turn floor into ceil */
346 if (tmp_length
< precision
)
347 tmp_length
= precision
;
348 /* Multiply by 2, as an estimate for FLAG_GROUP. */
349 tmp_length
= xsum (tmp_length
, tmp_length
);
350 /* Add 1, to account for a leading sign. */
351 tmp_length
= xsum (tmp_length
, 1);
355 # ifdef HAVE_LONG_LONG_INT
356 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
358 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
359 * 0.333334 /* binary -> octal */
361 + 1; /* turn floor into ceil */
364 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
366 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
367 * 0.333334 /* binary -> octal */
369 + 1; /* turn floor into ceil */
372 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
373 * 0.333334 /* binary -> octal */
375 + 1; /* turn floor into ceil */
376 if (tmp_length
< precision
)
377 tmp_length
= precision
;
378 /* Add 1, to account for a leading sign. */
379 tmp_length
= xsum (tmp_length
, 1);
383 # ifdef HAVE_LONG_LONG_INT
384 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
386 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
387 * 0.25 /* binary -> hexadecimal */
389 + 1; /* turn floor into ceil */
392 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
394 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
395 * 0.25 /* binary -> hexadecimal */
397 + 1; /* turn floor into ceil */
400 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
401 * 0.25 /* binary -> hexadecimal */
403 + 1; /* turn floor into ceil */
404 if (tmp_length
< precision
)
405 tmp_length
= precision
;
406 /* Add 2, to account for a leading sign or alternate form. */
407 tmp_length
= xsum (tmp_length
, 2);
411 # ifdef HAVE_LONG_DOUBLE
412 if (type
== TYPE_LONGDOUBLE
)
414 (unsigned int) (LDBL_MAX_EXP
415 * 0.30103 /* binary -> decimal */
416 * 2 /* estimate for FLAG_GROUP */
418 + 1 /* turn floor into ceil */
419 + 10; /* sign, decimal point etc. */
423 (unsigned int) (DBL_MAX_EXP
424 * 0.30103 /* binary -> decimal */
425 * 2 /* estimate for FLAG_GROUP */
427 + 1 /* turn floor into ceil */
428 + 10; /* sign, decimal point etc. */
429 tmp_length
= xsum (tmp_length
, precision
);
432 case 'e': case 'E': case 'g': case 'G':
435 12; /* sign, decimal point, exponent etc. */
436 tmp_length
= xsum (tmp_length
, precision
);
440 # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION
441 if (type
== TYPE_WIDE_CHAR
)
442 tmp_length
= MB_CUR_MAX
;
450 if (type
== TYPE_WIDE_STRING
)
453 local_wcslen (a
.arg
[dp
->arg_index
].a
.a_wide_string
);
455 # if !WIDE_CHAR_VERSION
456 tmp_length
= xtimes (tmp_length
, MB_CUR_MAX
);
461 tmp_length
= strlen (a
.arg
[dp
->arg_index
].a
.a_string
);
466 (unsigned int) (sizeof (void *) * CHAR_BIT
467 * 0.25 /* binary -> hexadecimal */
469 + 1 /* turn floor into ceil */
470 + 2; /* account for leading 0x */
477 if (tmp_length
< width
)
480 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
483 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (CHAR_T
))
487 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (CHAR_T
));
489 if (size_overflow_p (tmp_memsize
))
490 /* Overflow, would lead to out of memory. */
492 tmp
= (CHAR_T
*) malloc (tmp_memsize
);
499 /* Construct the format string for calling snprintf or
503 if (dp
->flags
& FLAG_GROUP
)
505 if (dp
->flags
& FLAG_LEFT
)
507 if (dp
->flags
& FLAG_SHOWSIGN
)
509 if (dp
->flags
& FLAG_SPACE
)
511 if (dp
->flags
& FLAG_ALT
)
513 if (dp
->flags
& FLAG_ZERO
)
515 if (dp
->width_start
!= dp
->width_end
)
517 size_t n
= dp
->width_end
- dp
->width_start
;
518 memcpy (p
, dp
->width_start
, n
* sizeof (CHAR_T
));
521 if (dp
->precision_start
!= dp
->precision_end
)
523 size_t n
= dp
->precision_end
- dp
->precision_start
;
524 memcpy (p
, dp
->precision_start
, n
* sizeof (CHAR_T
));
530 #ifdef HAVE_LONG_LONG_INT
531 case TYPE_LONGLONGINT
:
532 case TYPE_ULONGLONGINT
:
542 case TYPE_WIDE_STRING
:
546 #ifdef HAVE_LONG_DOUBLE
547 case TYPE_LONGDOUBLE
:
563 /* Construct the arguments for calling snprintf or sprintf. */
565 if (dp
->width_arg_index
!= ARG_NONE
)
567 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
569 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
571 if (dp
->precision_arg_index
!= ARG_NONE
)
573 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
575 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
579 /* Prepare checking whether snprintf returns the count
581 ENSURE_ALLOCATION (xsum (length
, 1));
582 result
[length
] = '\0';
591 maxlen
= allocated
- length
;
596 # define SNPRINTF_BUF(arg) \
597 switch (prefix_count) \
600 retcount = SNPRINTF (result + length, maxlen, buf, \
604 retcount = SNPRINTF (result + length, maxlen, buf, \
605 prefixes[0], arg, &count); \
608 retcount = SNPRINTF (result + length, maxlen, buf, \
609 prefixes[0], prefixes[1], arg, \
616 # define SNPRINTF_BUF(arg) \
617 switch (prefix_count) \
620 count = sprintf (tmp, buf, arg); \
623 count = sprintf (tmp, buf, prefixes[0], arg); \
626 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
638 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
644 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
650 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
656 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
662 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
668 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
674 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
680 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
684 #ifdef HAVE_LONG_LONG_INT
685 case TYPE_LONGLONGINT
:
687 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
691 case TYPE_ULONGLONGINT
:
693 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
700 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
704 #ifdef HAVE_LONG_DOUBLE
705 case TYPE_LONGDOUBLE
:
707 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
714 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
721 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
728 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
733 case TYPE_WIDE_STRING
:
735 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
742 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
751 /* Portability: Not all implementations of snprintf()
752 are ISO C 99 compliant. Determine the number of
753 bytes that snprintf() has produced or would have
757 /* Verify that snprintf() has NUL-terminated its
759 if (count
< maxlen
&& result
[length
+ count
] != '\0')
761 /* Portability hack. */
762 if (retcount
> count
)
767 /* snprintf() doesn't understand the '%n'
771 /* Don't use the '%n' directive; instead, look
772 at the snprintf() return value. */
778 /* Look at the snprintf() return value. */
781 /* HP-UX 10.20 snprintf() is doubly deficient:
782 It doesn't understand the '%n' directive,
783 *and* it returns -1 (rather than the length
784 that would have been required) when the
785 buffer is too small. */
787 xsum (xtimes (allocated
, 2), 12);
788 ENSURE_ALLOCATION (bigger_need
);
797 /* Attempt to handle failure. */
800 if (!(result
== resultbuf
|| result
== NULL
))
802 if (buf_malloced
!= NULL
)
810 if (count
>= tmp_length
)
811 /* tmp_length was incorrectly calculated - fix the
816 /* Make room for the result. */
819 /* Need at least count bytes. But allocate
820 proportionally, to avoid looping eternally if
821 snprintf() reports a too small count. */
823 xmax (xsum (length
, count
), xtimes (allocated
, 2));
825 ENSURE_ALLOCATION (n
);
832 /* The snprintf() result did fit. */
834 /* Append the sprintf() result. */
835 memcpy (result
+ length
, tmp
, count
* sizeof (CHAR_T
));
847 /* Add the final NUL. */
848 ENSURE_ALLOCATION (xsum (length
, 1));
849 result
[length
] = '\0';
851 if (result
!= resultbuf
&& length
+ 1 < allocated
)
853 /* Shrink the allocated memory if possible. */
856 memory
= (CHAR_T
*) realloc (result
, (length
+ 1) * sizeof (CHAR_T
));
861 if (buf_malloced
!= NULL
)
865 /* Note that we can produce a big string of a length > INT_MAX. POSIX
866 says that snprintf() fails with errno = EOVERFLOW in this case, but
867 that's only because snprintf() returns an 'int'. This function does
868 not have this limitation. */
872 if (!(result
== resultbuf
|| result
== NULL
))
874 if (buf_malloced
!= NULL
)