1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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, INT_MAX */
44 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
46 # include "wprintf-parse.h"
48 # include "printf-parse.h"
52 # define SIZE_MAX ((size_t) -1)
57 # define local_wcslen wcslen
59 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
60 a dependency towards this library, here is a local substitute.
61 Define this substitute only once, even if this file is included
62 twice in the same compilation unit. */
63 # ifndef local_wcslen_defined
64 # define local_wcslen_defined 1
66 local_wcslen (const wchar_t *s
)
70 for (ptr
= s
; *ptr
!= (wchar_t) 0; ptr
++)
79 # define VASNPRINTF vasnwprintf
80 # define CHAR_T wchar_t
81 # define DIRECTIVE wchar_t_directive
82 # define DIRECTIVES wchar_t_directives
83 # define PRINTF_PARSE wprintf_parse
84 # define USE_SNPRINTF 1
85 # if HAVE_DECL__SNWPRINTF
86 /* On Windows, the function swprintf() has a different signature than
87 on Unix; we use the _snwprintf() function instead. */
88 # define SNPRINTF _snwprintf
91 # define SNPRINTF swprintf
94 # define VASNPRINTF vasnprintf
96 # define DIRECTIVE char_directive
97 # define DIRECTIVES char_directives
98 # define PRINTF_PARSE printf_parse
99 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
100 # if HAVE_DECL__SNPRINTF
102 # define SNPRINTF _snprintf
105 # define SNPRINTF snprintf
110 VASNPRINTF (CHAR_T
*resultbuf
, size_t *lengthp
, const CHAR_T
*format
, va_list args
)
115 if (PRINTF_PARSE (format
, &d
, &a
) < 0)
126 if (printf_fetchargs (args
, &a
) < 0)
134 size_t buf_neededlength
;
136 CHAR_T
*buf_malloced
;
140 /* Output string accumulator. */
145 /* Allocate a small buffer that will hold a directive passed to
146 sprintf or snprintf. */
147 buf_neededlength
= 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 if (SIZE_MAX
/ sizeof (CHAR_T
) < buf_neededlength
)
158 goto out_of_memory_1
;
159 buf
= (CHAR_T
*) malloc (buf_neededlength
* sizeof (CHAR_T
));
161 goto out_of_memory_1
;
165 if (resultbuf
!= NULL
)
168 allocated
= *lengthp
;
177 result is either == resultbuf or == NULL or malloc-allocated.
178 If length > 0, then result != NULL. */
180 /* Ensures that allocated >= length + extra. Aborts through a jump to
181 out_of_memory if size is too big. */
182 #define ENSURE_ALLOCATION(extra) \
184 size_t needed = length + (extra); \
185 if (needed < length) \
186 goto out_of_memory; \
187 if (needed > allocated) \
189 size_t memory_size; \
192 allocated = (allocated > 0 ? 2 * allocated : 12); \
193 if (needed > allocated) \
194 allocated = needed; \
195 if (SIZE_MAX / sizeof (CHAR_T) < allocated) \
196 goto out_of_memory; \
197 memory_size = allocated * sizeof (CHAR_T); \
198 if (result == resultbuf || result == NULL) \
199 memory = (CHAR_T *) malloc (memory_size); \
201 memory = (CHAR_T *) realloc (result, memory_size); \
202 if (memory == NULL) \
203 goto out_of_memory; \
204 if (result == resultbuf && length > 0) \
205 memcpy (memory, result, length * sizeof (CHAR_T)); \
210 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
212 if (cp
!= dp
->dir_start
)
214 size_t n
= dp
->dir_start
- cp
;
216 ENSURE_ALLOCATION (n
);
217 memcpy (result
+ length
, cp
, n
* sizeof (CHAR_T
));
223 /* Execute a single directive. */
224 if (dp
->conversion
== '%')
226 if (!(dp
->arg_index
== ARG_NONE
))
228 ENSURE_ALLOCATION (1);
229 result
[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
;
297 if (SIZE_MAX
/ 10 <= width
)
299 width
= width
* 10 + (*digitp
++ - '0');
301 while (digitp
!= dp
->width_end
);
306 if (dp
->precision_start
!= dp
->precision_end
)
308 if (dp
->precision_arg_index
!= ARG_NONE
)
312 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
314 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
315 precision
= (arg
< 0 ? 0 : arg
);
319 const CHAR_T
*digitp
= dp
->precision_start
+ 1;
322 while (digitp
!= dp
->precision_end
)
324 size_t p1
= 10 * precision
+ (*digitp
++ - '0');
325 precision
= ((SIZE_MAX
/ 10 < precision
332 switch (dp
->conversion
)
335 case 'd': case 'i': case 'u':
336 # ifdef HAVE_LONG_LONG
337 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
339 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
340 * 0.30103 /* binary -> decimal */
341 * 2 /* estimate for FLAG_GROUP */
343 + 1 /* turn floor into ceil */
344 + 1; /* account for leading sign */
347 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
349 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
350 * 0.30103 /* binary -> decimal */
351 * 2 /* estimate for FLAG_GROUP */
353 + 1 /* turn floor into ceil */
354 + 1; /* account for leading sign */
357 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
358 * 0.30103 /* binary -> decimal */
359 * 2 /* estimate for FLAG_GROUP */
361 + 1 /* turn floor into ceil */
362 + 1; /* account for leading sign */
366 # ifdef HAVE_LONG_LONG
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 */
373 + 1; /* account for leading sign */
376 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
378 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
379 * 0.333334 /* binary -> octal */
381 + 1 /* turn floor into ceil */
382 + 1; /* account for leading sign */
385 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
386 * 0.333334 /* binary -> octal */
388 + 1 /* turn floor into ceil */
389 + 1; /* account for leading sign */
393 # ifdef HAVE_LONG_LONG
394 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
396 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
397 * 0.25 /* binary -> hexadecimal */
399 + 1 /* turn floor into ceil */
400 + 2; /* account for leading sign or alternate form */
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 */
409 + 2; /* account for leading sign or alternate form */
412 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
413 * 0.25 /* binary -> hexadecimal */
415 + 1 /* turn floor into ceil */
416 + 2; /* account for leading sign or alternate form */
420 # ifdef HAVE_LONG_DOUBLE
421 if (type
== TYPE_LONGDOUBLE
)
423 (unsigned int) (LDBL_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. */
432 (unsigned int) (DBL_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. */
438 tmp_length
+= precision
;
439 if (tmp_length
< precision
)
443 case 'e': case 'E': case 'g': case 'G':
446 12; /* sign, decimal point, exponent etc. */
447 tmp_length
+= precision
;
448 if (tmp_length
< precision
)
453 # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION
454 if (type
== TYPE_WIDE_CHAR
)
455 tmp_length
= MB_CUR_MAX
;
463 if (type
== TYPE_WIDE_STRING
)
466 local_wcslen (a
.arg
[dp
->arg_index
].a
.a_wide_string
);
468 # if !WIDE_CHAR_VERSION
469 if (SIZE_MAX
/ MB_CUR_MAX
< tmp_length
)
471 tmp_length
*= MB_CUR_MAX
;
476 tmp_length
= strlen (a
.arg
[dp
->arg_index
].a
.a_string
);
481 (unsigned int) (sizeof (void *) * CHAR_BIT
482 * 0.25 /* binary -> hexadecimal */
484 + 1 /* turn floor into ceil */
485 + 2; /* account for leading 0x */
492 if (tmp_length
< width
)
495 tmp_length
++; /* account for trailing NUL */
500 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (CHAR_T
))
504 if (SIZE_MAX
/ sizeof (CHAR_T
) < tmp_length
)
505 /* Overflow, would lead to out of memory. */
507 tmp
= (CHAR_T
*) malloc (tmp_length
* sizeof (CHAR_T
));
514 /* Construct the format string for calling snprintf or
518 if (dp
->flags
& FLAG_GROUP
)
520 if (dp
->flags
& FLAG_LEFT
)
522 if (dp
->flags
& FLAG_SHOWSIGN
)
524 if (dp
->flags
& FLAG_SPACE
)
526 if (dp
->flags
& FLAG_ALT
)
528 if (dp
->flags
& FLAG_ZERO
)
530 if (dp
->width_start
!= dp
->width_end
)
532 size_t n
= dp
->width_end
- dp
->width_start
;
533 memcpy (p
, dp
->width_start
, n
* sizeof (CHAR_T
));
536 if (dp
->precision_start
!= dp
->precision_end
)
538 size_t n
= dp
->precision_end
- dp
->precision_start
;
539 memcpy (p
, dp
->precision_start
, n
* sizeof (CHAR_T
));
545 #ifdef HAVE_LONG_LONG
546 case TYPE_LONGLONGINT
:
547 case TYPE_ULONGLONGINT
:
557 case TYPE_WIDE_STRING
:
561 #ifdef HAVE_LONG_DOUBLE
562 case TYPE_LONGDOUBLE
:
578 /* Construct the arguments for calling snprintf or sprintf. */
580 if (dp
->width_arg_index
!= ARG_NONE
)
582 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
584 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
586 if (dp
->precision_arg_index
!= ARG_NONE
)
588 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
590 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
594 /* Prepare checking whether snprintf returns the count
596 ENSURE_ALLOCATION (1);
597 result
[length
] = '\0';
606 maxlen
= allocated
- length
;
611 # define SNPRINTF_BUF(arg) \
612 switch (prefix_count) \
615 retcount = SNPRINTF (result + length, maxlen, buf, \
619 retcount = SNPRINTF (result + length, maxlen, buf, \
620 prefixes[0], arg, &count); \
623 retcount = SNPRINTF (result + length, maxlen, buf, \
624 prefixes[0], prefixes[1], arg, \
631 # define SNPRINTF_BUF(arg) \
632 switch (prefix_count) \
635 count = sprintf (tmp, buf, arg); \
638 count = sprintf (tmp, buf, prefixes[0], arg); \
641 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
653 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
659 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
665 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
671 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
677 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
683 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
689 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
695 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
699 #ifdef HAVE_LONG_LONG
700 case TYPE_LONGLONGINT
:
702 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
706 case TYPE_ULONGLONGINT
:
708 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
715 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
719 #ifdef HAVE_LONG_DOUBLE
720 case TYPE_LONGDOUBLE
:
722 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
729 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
736 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
743 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
748 case TYPE_WIDE_STRING
:
750 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
757 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
766 /* Portability: Not all implementations of snprintf()
767 are ISO C 99 compliant. Determine the number of
768 bytes that snprintf() has produced or would have
772 /* Verify that snprintf() has NUL-terminated its
774 if (count
< maxlen
&& result
[length
+ count
] != '\0')
776 /* Portability hack. */
777 if (retcount
> count
)
782 /* snprintf() doesn't understand the '%n'
786 /* Don't use the '%n' directive; instead, look
787 at the snprintf() return value. */
793 /* Look at the snprintf() return value. */
796 /* HP-UX 10.20 snprintf() is doubly deficient:
797 It doesn't understand the '%n' directive,
798 *and* it returns -1 (rather than the length
799 that would have been required) when the
800 buffer is too small. */
802 (allocated
> 12 ? allocated
: 12);
803 ENSURE_ALLOCATION (bigger_need
);
812 /* Attempt to handle failure. */
815 if (!(result
== resultbuf
|| result
== NULL
))
817 if (buf_malloced
!= NULL
)
825 if (count
>= tmp_length
)
826 /* tmp_length was incorrectly calculated - fix the
831 /* Make room for the result. */
834 /* Need at least count bytes. But allocate
835 proportionally, to avoid looping eternally if
836 snprintf() reports a too small count. */
837 ENSURE_ALLOCATION (count
< allocated
838 ? allocated
: count
);
845 /* The snprintf() result did fit. */
847 /* Append the sprintf() result. */
848 memcpy (result
+ length
, tmp
, count
* sizeof (CHAR_T
));
860 /* Add the final NUL. */
861 ENSURE_ALLOCATION (1);
862 result
[length
] = '\0';
864 if (result
!= resultbuf
&& length
+ 1 < allocated
)
866 /* Shrink the allocated memory if possible. */
869 memory
= (CHAR_T
*) realloc (result
, (length
+ 1) * sizeof (CHAR_T
));
874 if (buf_malloced
!= NULL
)
878 if (length
> INT_MAX
)
879 goto length_overflow
;
883 /* We could produce such a big string, but its length doesn't fit into
884 an 'int'. POSIX says that snprintf() fails with errno = EOVERFLOW in
886 if (result
!= resultbuf
)
892 if (!(result
== resultbuf
|| result
== NULL
))
894 if (buf_malloced
!= NULL
)