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 Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser 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
34 # include "vasnwprintf.h"
36 # include "vasnprintf.h"
39 #include <stdio.h> /* snprintf(), sprintf() */
40 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
41 #include <string.h> /* memcpy(), strlen() */
42 #include <errno.h> /* errno */
43 #include <limits.h> /* CHAR_BIT */
44 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
46 # include "wprintf-parse.h"
48 # include "printf-parse.h"
51 /* Checked size_t computations. */
56 # define local_wcslen wcslen
58 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
59 a dependency towards this library, here is a local substitute.
60 Define this substitute only once, even if this file is included
61 twice in the same compilation unit. */
62 # ifndef local_wcslen_defined
63 # define local_wcslen_defined 1
65 local_wcslen (const wchar_t *s
)
69 for (ptr
= s
; *ptr
!= (wchar_t) 0; ptr
++)
78 # define VASNPRINTF vasnwprintf
79 # define CHAR_T wchar_t
80 # define DIRECTIVE wchar_t_directive
81 # define DIRECTIVES wchar_t_directives
82 # define PRINTF_PARSE wprintf_parse
83 # define USE_SNPRINTF 1
84 # if HAVE_DECL__SNWPRINTF
85 /* On Windows, the function swprintf() has a different signature than
86 on Unix; we use the _snwprintf() function instead. */
87 # define SNPRINTF _snwprintf
90 # define SNPRINTF swprintf
93 # define VASNPRINTF vasnprintf
95 # define DIRECTIVE char_directive
96 # define DIRECTIVES char_directives
97 # define PRINTF_PARSE printf_parse
98 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
99 # if HAVE_DECL__SNPRINTF
101 # define SNPRINTF _snprintf
104 # define SNPRINTF snprintf
109 VASNPRINTF (CHAR_T
*resultbuf
, size_t *lengthp
, const CHAR_T
*format
, va_list args
)
114 if (PRINTF_PARSE (format
, &d
, &a
) < 0)
125 if (printf_fetchargs (args
, &a
) < 0)
133 size_t buf_neededlength
;
135 CHAR_T
*buf_malloced
;
139 /* Output string accumulator. */
144 /* Allocate a small buffer that will hold a directive passed to
145 sprintf or snprintf. */
147 xsum4 (7, d
.max_width_length
, d
.max_precision_length
, 6);
149 if (buf_neededlength
< 4000 / sizeof (CHAR_T
))
151 buf
= (CHAR_T
*) alloca (buf_neededlength
* sizeof (CHAR_T
));
157 size_t buf_memsize
= xtimes (buf_neededlength
, sizeof (CHAR_T
));
158 if (size_overflow_p (buf_memsize
))
159 goto out_of_memory_1
;
160 buf
= (CHAR_T
*) malloc (buf_memsize
);
162 goto out_of_memory_1
;
166 if (resultbuf
!= NULL
)
169 allocated
= *lengthp
;
178 result is either == resultbuf or == NULL or malloc-allocated.
179 If length > 0, then result != NULL. */
181 /* Ensures that allocated >= needed. Aborts through a jump to
182 out_of_memory if needed is SIZE_MAX or otherwise too big. */
183 #define ENSURE_ALLOCATION(needed) \
184 if ((needed) > allocated) \
186 size_t memory_size; \
189 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
190 if ((needed) > allocated) \
191 allocated = (needed); \
192 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
193 if (size_overflow_p (memory_size)) \
194 goto out_of_memory; \
195 if (result == resultbuf || result == NULL) \
196 memory = (CHAR_T *) malloc (memory_size); \
198 memory = (CHAR_T *) realloc (result, memory_size); \
199 if (memory == NULL) \
200 goto out_of_memory; \
201 if (result == resultbuf && length > 0) \
202 memcpy (memory, result, length * sizeof (CHAR_T)); \
206 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
208 if (cp
!= dp
->dir_start
)
210 size_t n
= dp
->dir_start
- cp
;
211 size_t augmented_length
= xsum (length
, n
);
213 ENSURE_ALLOCATION (augmented_length
);
214 memcpy (result
+ length
, cp
, n
* sizeof (CHAR_T
));
215 length
= augmented_length
;
220 /* Execute a single directive. */
221 if (dp
->conversion
== '%')
223 size_t augmented_length
;
225 if (!(dp
->arg_index
== ARG_NONE
))
227 augmented_length
= xsum (length
, 1);
228 ENSURE_ALLOCATION (augmented_length
);
229 result
[length
] = '%';
230 length
= augmented_length
;
234 if (!(dp
->arg_index
!= ARG_NONE
))
237 if (dp
->conversion
== 'n')
239 switch (a
.arg
[dp
->arg_index
].type
)
241 case TYPE_COUNT_SCHAR_POINTER
:
242 *a
.arg
[dp
->arg_index
].a
.a_count_schar_pointer
= length
;
244 case TYPE_COUNT_SHORT_POINTER
:
245 *a
.arg
[dp
->arg_index
].a
.a_count_short_pointer
= length
;
247 case TYPE_COUNT_INT_POINTER
:
248 *a
.arg
[dp
->arg_index
].a
.a_count_int_pointer
= length
;
250 case TYPE_COUNT_LONGINT_POINTER
:
251 *a
.arg
[dp
->arg_index
].a
.a_count_longint_pointer
= length
;
253 #ifdef HAVE_LONG_LONG
254 case TYPE_COUNT_LONGLONGINT_POINTER
:
255 *a
.arg
[dp
->arg_index
].a
.a_count_longlongint_pointer
= length
;
264 arg_type type
= a
.arg
[dp
->arg_index
].type
;
266 unsigned int prefix_count
;
273 /* Allocate a temporary buffer of sufficient size for calling
280 if (dp
->width_start
!= dp
->width_end
)
282 if (dp
->width_arg_index
!= ARG_NONE
)
286 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
288 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
289 width
= (arg
< 0 ? (unsigned int) (-arg
) : arg
);
293 const CHAR_T
*digitp
= dp
->width_start
;
296 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
297 while (digitp
!= dp
->width_end
);
302 if (dp
->precision_start
!= dp
->precision_end
)
304 if (dp
->precision_arg_index
!= ARG_NONE
)
308 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
310 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
311 precision
= (arg
< 0 ? 0 : arg
);
315 const CHAR_T
*digitp
= dp
->precision_start
+ 1;
318 while (digitp
!= dp
->precision_end
)
319 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
323 switch (dp
->conversion
)
326 case 'd': case 'i': case 'u':
327 # ifdef HAVE_LONG_LONG
328 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
330 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
331 * 0.30103 /* binary -> decimal */
333 + 1; /* turn floor into ceil */
336 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
338 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
339 * 0.30103 /* binary -> decimal */
341 + 1; /* turn floor into ceil */
344 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
345 * 0.30103 /* binary -> decimal */
347 + 1; /* turn floor into ceil */
348 if (tmp_length
< precision
)
349 tmp_length
= precision
;
350 /* Multiply by 2, as an estimate for FLAG_GROUP. */
351 tmp_length
= xsum (tmp_length
, tmp_length
);
352 /* Add 1, to account for a leading sign. */
353 tmp_length
= xsum (tmp_length
, 1);
357 # ifdef HAVE_LONG_LONG
358 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
360 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
361 * 0.333334 /* binary -> octal */
363 + 1; /* turn floor into ceil */
366 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
368 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
369 * 0.333334 /* binary -> octal */
371 + 1; /* turn floor into ceil */
374 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
375 * 0.333334 /* binary -> octal */
377 + 1; /* turn floor into ceil */
378 if (tmp_length
< precision
)
379 tmp_length
= precision
;
380 /* Add 1, to account for a leading sign. */
381 tmp_length
= xsum (tmp_length
, 1);
385 # ifdef HAVE_LONG_LONG
386 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
388 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
389 * 0.25 /* binary -> hexadecimal */
391 + 1; /* turn floor into ceil */
394 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
396 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
397 * 0.25 /* binary -> hexadecimal */
399 + 1; /* turn floor into ceil */
402 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
403 * 0.25 /* binary -> hexadecimal */
405 + 1; /* turn floor into ceil */
406 if (tmp_length
< precision
)
407 tmp_length
= precision
;
408 /* Add 2, to account for a leading sign or alternate form. */
409 tmp_length
= xsum (tmp_length
, 2);
413 # ifdef HAVE_LONG_DOUBLE
414 if (type
== TYPE_LONGDOUBLE
)
416 (unsigned int) (LDBL_MAX_EXP
417 * 0.30103 /* binary -> decimal */
418 * 2 /* estimate for FLAG_GROUP */
420 + 1 /* turn floor into ceil */
421 + 10; /* sign, decimal point etc. */
425 (unsigned int) (DBL_MAX_EXP
426 * 0.30103 /* binary -> decimal */
427 * 2 /* estimate for FLAG_GROUP */
429 + 1 /* turn floor into ceil */
430 + 10; /* sign, decimal point etc. */
431 tmp_length
= xsum (tmp_length
, precision
);
434 case 'e': case 'E': case 'g': case 'G':
437 12; /* sign, decimal point, exponent etc. */
438 tmp_length
= xsum (tmp_length
, precision
);
442 # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION
443 if (type
== TYPE_WIDE_CHAR
)
444 tmp_length
= MB_CUR_MAX
;
452 if (type
== TYPE_WIDE_STRING
)
455 local_wcslen (a
.arg
[dp
->arg_index
].a
.a_wide_string
);
457 # if !WIDE_CHAR_VERSION
458 tmp_length
= xtimes (tmp_length
, MB_CUR_MAX
);
463 tmp_length
= strlen (a
.arg
[dp
->arg_index
].a
.a_string
);
468 (unsigned int) (sizeof (void *) * CHAR_BIT
469 * 0.25 /* binary -> hexadecimal */
471 + 1 /* turn floor into ceil */
472 + 2; /* account for leading 0x */
479 if (tmp_length
< width
)
482 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
485 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (CHAR_T
))
489 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (CHAR_T
));
491 if (size_overflow_p (tmp_memsize
))
492 /* Overflow, would lead to out of memory. */
494 tmp
= (CHAR_T
*) malloc (tmp_memsize
);
501 /* Construct the format string for calling snprintf or
505 if (dp
->flags
& FLAG_GROUP
)
507 if (dp
->flags
& FLAG_LEFT
)
509 if (dp
->flags
& FLAG_SHOWSIGN
)
511 if (dp
->flags
& FLAG_SPACE
)
513 if (dp
->flags
& FLAG_ALT
)
515 if (dp
->flags
& FLAG_ZERO
)
517 if (dp
->width_start
!= dp
->width_end
)
519 size_t n
= dp
->width_end
- dp
->width_start
;
520 memcpy (p
, dp
->width_start
, n
* sizeof (CHAR_T
));
523 if (dp
->precision_start
!= dp
->precision_end
)
525 size_t n
= dp
->precision_end
- dp
->precision_start
;
526 memcpy (p
, dp
->precision_start
, n
* sizeof (CHAR_T
));
532 #ifdef HAVE_LONG_LONG
533 case TYPE_LONGLONGINT
:
534 case TYPE_ULONGLONGINT
:
544 case TYPE_WIDE_STRING
:
548 #ifdef HAVE_LONG_DOUBLE
549 case TYPE_LONGDOUBLE
:
565 /* Construct the arguments for calling snprintf or sprintf. */
567 if (dp
->width_arg_index
!= ARG_NONE
)
569 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
571 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
573 if (dp
->precision_arg_index
!= ARG_NONE
)
575 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
577 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
581 /* Prepare checking whether snprintf returns the count
583 ENSURE_ALLOCATION (xsum (length
, 1));
584 result
[length
] = '\0';
593 maxlen
= allocated
- length
;
598 # define SNPRINTF_BUF(arg) \
599 switch (prefix_count) \
602 retcount = SNPRINTF (result + length, maxlen, buf, \
606 retcount = SNPRINTF (result + length, maxlen, buf, \
607 prefixes[0], arg, &count); \
610 retcount = SNPRINTF (result + length, maxlen, buf, \
611 prefixes[0], prefixes[1], arg, \
618 # define SNPRINTF_BUF(arg) \
619 switch (prefix_count) \
622 count = sprintf (tmp, buf, arg); \
625 count = sprintf (tmp, buf, prefixes[0], arg); \
628 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
640 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
646 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
652 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
658 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
664 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
670 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
676 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
682 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
686 #ifdef HAVE_LONG_LONG
687 case TYPE_LONGLONGINT
:
689 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
693 case TYPE_ULONGLONGINT
:
695 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
702 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
706 #ifdef HAVE_LONG_DOUBLE
707 case TYPE_LONGDOUBLE
:
709 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
716 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
723 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
730 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
735 case TYPE_WIDE_STRING
:
737 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
744 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
753 /* Portability: Not all implementations of snprintf()
754 are ISO C 99 compliant. Determine the number of
755 bytes that snprintf() has produced or would have
759 /* Verify that snprintf() has NUL-terminated its
761 if (count
< maxlen
&& result
[length
+ count
] != '\0')
763 /* Portability hack. */
764 if (retcount
> count
)
769 /* snprintf() doesn't understand the '%n'
773 /* Don't use the '%n' directive; instead, look
774 at the snprintf() return value. */
780 /* Look at the snprintf() return value. */
783 /* HP-UX 10.20 snprintf() is doubly deficient:
784 It doesn't understand the '%n' directive,
785 *and* it returns -1 (rather than the length
786 that would have been required) when the
787 buffer is too small. */
789 xsum (xtimes (allocated
, 2), 12);
790 ENSURE_ALLOCATION (bigger_need
);
799 /* Attempt to handle failure. */
802 if (!(result
== resultbuf
|| result
== NULL
))
804 if (buf_malloced
!= NULL
)
812 if (count
>= tmp_length
)
813 /* tmp_length was incorrectly calculated - fix the
818 /* Make room for the result. */
821 /* Need at least count bytes. But allocate
822 proportionally, to avoid looping eternally if
823 snprintf() reports a too small count. */
825 xmax (xsum (length
, count
), xtimes (allocated
, 2));
827 ENSURE_ALLOCATION (n
);
834 /* The snprintf() result did fit. */
836 /* Append the sprintf() result. */
837 memcpy (result
+ length
, tmp
, count
* sizeof (CHAR_T
));
849 /* Add the final NUL. */
850 ENSURE_ALLOCATION (xsum (length
, 1));
851 result
[length
] = '\0';
853 if (result
!= resultbuf
&& length
+ 1 < allocated
)
855 /* Shrink the allocated memory if possible. */
858 memory
= (CHAR_T
*) realloc (result
, (length
+ 1) * sizeof (CHAR_T
));
863 if (buf_malloced
!= NULL
)
867 /* Note that we can produce a big string of a length > INT_MAX. POSIX
868 says that snprintf() fails with errno = EOVERFLOW in this case, but
869 that's only because snprintf() returns an 'int'. This function does
870 not have this limitation. */
874 if (!(result
== resultbuf
|| result
== NULL
))
876 if (buf_malloced
!= NULL
)