3 /* vsprintf with automatic memory allocation.
4 Copyright (C) 1999, 2002-2003 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published
8 by the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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
37 # include "vasnwprintf.h"
39 # include "vasnprintf.h"
42 #include <stdio.h> /* snprintf(), sprintf() */
43 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
44 #include <string.h> /* memcpy(), strlen() */
45 #include <errno.h> /* errno */
46 #include <limits.h> /* CHAR_BIT */
47 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
49 # include "wprintf-parse.h"
51 # include "printf-parse.h"
54 /* Checked size_t computations. */
59 # define local_wcslen wcslen
61 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
62 a dependency towards this library, here is a local substitute.
63 Define this substitute only once, even if this file is included
64 twice in the same compilation unit. */
65 # ifndef local_wcslen_defined
66 # define local_wcslen_defined 1
68 local_wcslen (const wchar_t *s
)
72 for (ptr
= s
; *ptr
!= (wchar_t) 0; ptr
++)
81 # define VASNPRINTF vasnwprintf
82 # define CHAR_T wchar_t
83 # define DIRECTIVE wchar_t_directive
84 # define DIRECTIVES wchar_t_directives
85 # define PRINTF_PARSE wprintf_parse
86 # define USE_SNPRINTF 1
87 # if HAVE_DECL__SNWPRINTF
88 /* On Windows, the function swprintf() has a different signature than
89 on Unix; we use the _snwprintf() function instead. */
90 # define SNPRINTF _snwprintf
93 # define SNPRINTF swprintf
96 # define VASNPRINTF vasnprintf
98 # define DIRECTIVE char_directive
99 # define DIRECTIVES char_directives
100 # define PRINTF_PARSE printf_parse
101 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
102 # if HAVE_DECL__SNPRINTF
104 # define SNPRINTF _snprintf
107 # define SNPRINTF snprintf
112 VASNPRINTF (CHAR_T
*resultbuf
, size_t *lengthp
, const CHAR_T
*format
, va_list args
)
117 if (PRINTF_PARSE (format
, &d
, &a
) < 0)
128 if (printf_fetchargs (args
, &a
) < 0)
136 size_t buf_neededlength
;
138 CHAR_T
*buf_malloced
;
142 /* Output string accumulator. */
147 /* Allocate a small buffer that will hold a directive passed to
148 sprintf or snprintf. */
150 xsum4 (7, d
.max_width_length
, d
.max_precision_length
, 6);
152 if (buf_neededlength
< 4000 / sizeof (CHAR_T
))
154 buf
= (CHAR_T
*) alloca (buf_neededlength
* sizeof (CHAR_T
));
160 size_t buf_memsize
= xtimes (buf_neededlength
, sizeof (CHAR_T
));
161 if (size_overflow_p (buf_memsize
))
162 goto out_of_memory_1
;
163 buf
= (CHAR_T
*) malloc (buf_memsize
);
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 >= needed. Aborts through a jump to
185 out_of_memory if needed is SIZE_MAX or otherwise too big. */
186 #define ENSURE_ALLOCATION(needed) \
187 if ((needed) > allocated) \
189 size_t memory_size; \
192 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
193 if ((needed) > allocated) \
194 allocated = (needed); \
195 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
196 if (size_overflow_p (memory_size)) \
197 goto out_of_memory; \
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)); \
209 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
211 if (cp
!= dp
->dir_start
)
213 size_t n
= dp
->dir_start
- cp
;
214 size_t augmented_length
= xsum (length
, n
);
216 ENSURE_ALLOCATION (augmented_length
);
217 memcpy (result
+ length
, cp
, n
* sizeof (CHAR_T
));
218 length
= augmented_length
;
223 /* Execute a single directive. */
224 if (dp
->conversion
== '%')
226 size_t augmented_length
;
228 if (!(dp
->arg_index
== ARG_NONE
))
230 augmented_length
= xsum (length
, 1);
231 ENSURE_ALLOCATION (augmented_length
);
232 result
[length
] = '%';
233 length
= augmented_length
;
237 if (!(dp
->arg_index
!= ARG_NONE
))
240 if (dp
->conversion
== 'n')
242 switch (a
.arg
[dp
->arg_index
].type
)
244 case TYPE_COUNT_SCHAR_POINTER
:
245 *a
.arg
[dp
->arg_index
].a
.a_count_schar_pointer
= length
;
247 case TYPE_COUNT_SHORT_POINTER
:
248 *a
.arg
[dp
->arg_index
].a
.a_count_short_pointer
= length
;
250 case TYPE_COUNT_INT_POINTER
:
251 *a
.arg
[dp
->arg_index
].a
.a_count_int_pointer
= length
;
253 case TYPE_COUNT_LONGINT_POINTER
:
254 *a
.arg
[dp
->arg_index
].a
.a_count_longint_pointer
= length
;
256 #ifdef HAVE_LONG_LONG
257 case TYPE_COUNT_LONGLONGINT_POINTER
:
258 *a
.arg
[dp
->arg_index
].a
.a_count_longlongint_pointer
= length
;
267 arg_type type
= a
.arg
[dp
->arg_index
].type
;
269 unsigned int prefix_count
;
276 /* Allocate a temporary buffer of sufficient size for calling
283 if (dp
->width_start
!= dp
->width_end
)
285 if (dp
->width_arg_index
!= ARG_NONE
)
289 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
291 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
292 width
= (arg
< 0 ? (unsigned int) (-arg
) : arg
);
296 const CHAR_T
*digitp
= dp
->width_start
;
299 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
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;
322 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
323 while (digitp
!= dp
->precision_end
);
327 switch (dp
->conversion
)
330 case 'd': case 'i': case 'u':
331 # ifdef HAVE_LONG_LONG
332 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
334 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
335 * 0.30103 /* binary -> decimal */
336 * 2 /* estimate for FLAG_GROUP */
338 + 1 /* turn floor into ceil */
339 + 1; /* account for leading sign */
342 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
344 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
345 * 0.30103 /* binary -> decimal */
346 * 2 /* estimate for FLAG_GROUP */
348 + 1 /* turn floor into ceil */
349 + 1; /* account for leading sign */
352 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
353 * 0.30103 /* binary -> decimal */
354 * 2 /* estimate for FLAG_GROUP */
356 + 1 /* turn floor into ceil */
357 + 1; /* account for leading sign */
361 # ifdef HAVE_LONG_LONG
362 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
364 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
365 * 0.333334 /* binary -> octal */
367 + 1 /* turn floor into ceil */
368 + 1; /* account for leading sign */
371 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
373 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
374 * 0.333334 /* binary -> octal */
376 + 1 /* turn floor into ceil */
377 + 1; /* account for leading sign */
380 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
381 * 0.333334 /* binary -> octal */
383 + 1 /* turn floor into ceil */
384 + 1; /* account for leading sign */
388 # ifdef HAVE_LONG_LONG
389 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
391 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
392 * 0.25 /* binary -> hexadecimal */
394 + 1 /* turn floor into ceil */
395 + 2; /* account for leading sign or alternate form */
398 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
400 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
401 * 0.25 /* binary -> hexadecimal */
403 + 1 /* turn floor into ceil */
404 + 2; /* account for leading sign or alternate form */
407 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
408 * 0.25 /* binary -> hexadecimal */
410 + 1 /* turn floor into ceil */
411 + 2; /* account for leading sign or alternate form */
415 # ifdef HAVE_LONG_DOUBLE
416 if (type
== TYPE_LONGDOUBLE
)
418 (unsigned int) (LDBL_MAX_EXP
419 * 0.30103 /* binary -> decimal */
420 * 2 /* estimate for FLAG_GROUP */
422 + 1 /* turn floor into ceil */
423 + 10; /* sign, decimal point etc. */
427 (unsigned int) (DBL_MAX_EXP
428 * 0.30103 /* binary -> decimal */
429 * 2 /* estimate for FLAG_GROUP */
431 + 1 /* turn floor into ceil */
432 + 10; /* sign, decimal point etc. */
433 tmp_length
= xsum (tmp_length
, precision
);
436 case 'e': case 'E': case 'g': case 'G':
439 12; /* sign, decimal point, exponent etc. */
440 tmp_length
= xsum (tmp_length
, precision
);
444 # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION
445 if (type
== TYPE_WIDE_CHAR
)
446 tmp_length
= MB_CUR_MAX
;
454 if (type
== TYPE_WIDE_STRING
)
457 local_wcslen (a
.arg
[dp
->arg_index
].a
.a_wide_string
);
459 # if !WIDE_CHAR_VERSION
460 tmp_length
= xtimes (tmp_length
, MB_CUR_MAX
);
465 tmp_length
= strlen (a
.arg
[dp
->arg_index
].a
.a_string
);
470 (unsigned int) (sizeof (void *) * CHAR_BIT
471 * 0.25 /* binary -> hexadecimal */
473 + 1 /* turn floor into ceil */
474 + 2; /* account for leading 0x */
481 if (tmp_length
< width
)
484 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
487 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (CHAR_T
))
491 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (CHAR_T
));
493 if (size_overflow_p (tmp_memsize
))
494 /* Overflow, would lead to out of memory. */
496 tmp
= (CHAR_T
*) malloc (tmp_memsize
);
503 /* Construct the format string for calling snprintf or
507 if (dp
->flags
& FLAG_GROUP
)
509 if (dp
->flags
& FLAG_LEFT
)
511 if (dp
->flags
& FLAG_SHOWSIGN
)
513 if (dp
->flags
& FLAG_SPACE
)
515 if (dp
->flags
& FLAG_ALT
)
517 if (dp
->flags
& FLAG_ZERO
)
519 if (dp
->width_start
!= dp
->width_end
)
521 size_t n
= dp
->width_end
- dp
->width_start
;
522 memcpy (p
, dp
->width_start
, n
* sizeof (CHAR_T
));
525 if (dp
->precision_start
!= dp
->precision_end
)
527 size_t n
= dp
->precision_end
- dp
->precision_start
;
528 memcpy (p
, dp
->precision_start
, n
* sizeof (CHAR_T
));
534 #ifdef HAVE_LONG_LONG
535 case TYPE_LONGLONGINT
:
536 case TYPE_ULONGLONGINT
:
546 case TYPE_WIDE_STRING
:
550 #ifdef HAVE_LONG_DOUBLE
551 case TYPE_LONGDOUBLE
:
567 /* Construct the arguments for calling snprintf or sprintf. */
569 if (dp
->width_arg_index
!= ARG_NONE
)
571 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
573 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
575 if (dp
->precision_arg_index
!= ARG_NONE
)
577 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
579 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
583 /* Prepare checking whether snprintf returns the count
585 ENSURE_ALLOCATION (xsum (length
, 1));
586 result
[length
] = '\0';
595 maxlen
= allocated
- length
;
600 # define SNPRINTF_BUF(arg) \
601 switch (prefix_count) \
604 retcount = SNPRINTF (result + length, maxlen, buf, \
608 retcount = SNPRINTF (result + length, maxlen, buf, \
609 prefixes[0], arg, &count); \
612 retcount = SNPRINTF (result + length, maxlen, buf, \
613 prefixes[0], prefixes[1], arg, \
620 # define SNPRINTF_BUF(arg) \
621 switch (prefix_count) \
624 count = sprintf (tmp, buf, arg); \
627 count = sprintf (tmp, buf, prefixes[0], arg); \
630 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
642 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
648 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
654 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
660 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
666 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
672 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
678 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
684 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
688 #ifdef HAVE_LONG_LONG
689 case TYPE_LONGLONGINT
:
691 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
695 case TYPE_ULONGLONGINT
:
697 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
704 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
708 #ifdef HAVE_LONG_DOUBLE
709 case TYPE_LONGDOUBLE
:
711 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
718 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
725 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
732 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
737 case TYPE_WIDE_STRING
:
739 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
746 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
755 /* Portability: Not all implementations of snprintf()
756 are ISO C 99 compliant. Determine the number of
757 bytes that snprintf() has produced or would have
761 /* Verify that snprintf() has NUL-terminated its
763 if (count
< maxlen
&& result
[length
+ count
] != '\0')
765 /* Portability hack. */
766 if (retcount
> count
)
771 /* snprintf() doesn't understand the '%n'
775 /* Don't use the '%n' directive; instead, look
776 at the snprintf() return value. */
782 /* Look at the snprintf() return value. */
785 /* HP-UX 10.20 snprintf() is doubly deficient:
786 It doesn't understand the '%n' directive,
787 *and* it returns -1 (rather than the length
788 that would have been required) when the
789 buffer is too small. */
791 xsum (xtimes (allocated
, 2), 12);
792 ENSURE_ALLOCATION (bigger_need
);
801 /* Attempt to handle failure. */
804 if (!(result
== resultbuf
|| result
== NULL
))
806 if (buf_malloced
!= NULL
)
814 if (count
>= tmp_length
)
815 /* tmp_length was incorrectly calculated - fix the
820 /* Make room for the result. */
823 /* Need at least count bytes. But allocate
824 proportionally, to avoid looping eternally if
825 snprintf() reports a too small count. */
827 xmax (xsum (length
, count
), xtimes (allocated
, 2));
829 ENSURE_ALLOCATION (n
);
836 /* The snprintf() result did fit. */
838 /* Append the sprintf() result. */
839 memcpy (result
+ length
, tmp
, count
* sizeof (CHAR_T
));
851 /* Add the final NUL. */
852 ENSURE_ALLOCATION (xsum (length
, 1));
853 result
[length
] = '\0';
855 if (result
!= resultbuf
&& length
+ 1 < allocated
)
857 /* Shrink the allocated memory if possible. */
860 memory
= (CHAR_T
*) realloc (result
, (length
+ 1) * sizeof (CHAR_T
));
865 if (buf_malloced
!= NULL
)
872 if (!(result
== resultbuf
|| result
== NULL
))
874 if (buf_malloced
!= NULL
)