1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2003 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
21 This must come before <config.h> because <config.h> may include
22 <features.h>, and once <features.h> has been included, it's too late. */
24 # define _GNU_SOURCE 1
32 #include "gst-printf.h"
35 #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 */
43 #include "printf-parse.h"
44 #include "printf-extension.h"
48 # define local_wcslen wcslen
50 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
51 a dependency towards this library, here is a local substitute.
52 Define this substitute only once, even if this file is included
53 twice in the same compilation unit. */
54 # ifndef local_wcslen_defined
55 # define local_wcslen_defined 1
57 local_wcslen (const wchar_t * s)
61 for (ptr = s; *ptr != (wchar_t) 0; ptr++);
68 /* For those losing systems which don't have 'alloca' we have to add
69 some additional code emulating it. */
70 #if defined (alloca) || defined (GLIB_HAVE_ALLOCA_H)
71 # define freea(p) /* nothing */
73 # define alloca(n) malloc (n)
74 # define freea(p) free (p)
77 #ifndef HAVE_LONG_LONG_FORMAT
79 print_long_long (char *buf,
83 unsigned long flags, char conversion, unsigned long long number)
90 static const char *upper = "0123456789ABCDEFX";
91 static const char *lower = "0123456789abcdefx";
98 if (p - buf == len - 1) \
108 switch (conversion) {
132 negative = (long long) number < 0;
134 number = -((long long) number);
139 pointer = bufferend = &buffer[sizeof (buffer) - 1];
141 for (i = 1; i < (int) sizeof (buffer); i++) {
142 *pointer-- = digits[number % base];
149 width -= (bufferend - pointer) - 1;
151 /* Adjust precision */
152 if (precision != -1) {
153 precision -= (bufferend - pointer) - 1;
159 /* Adjust width further */
160 if (negative || (flags & FLAG_SHOWSIGN) || (flags & FLAG_SPACE))
162 if (flags & FLAG_ALT) {
175 /* Output prefixes spaces if needed */
176 if (!((flags & FLAG_LEFT) || ((flags & FLAG_ZERO) && (precision == -1)))) {
177 count = (precision == -1) ? 0 : precision;
178 while (width-- > count)
182 /* width has been adjusted for signs and alternatives */
185 } else if (flags & FLAG_SHOWSIGN) {
187 } else if (flags & FLAG_SPACE) {
191 if (flags & FLAG_ALT) {
205 /* Output prefixed zero padding if needed */
206 if (flags & FLAG_ZERO) {
209 while (precision-- > 0) {
215 /* Output the number itself */
216 while (*(++pointer)) {
220 /* Output trailing spaces if needed */
221 if (flags & FLAG_LEFT) {
233 printf_postprocess_args (char_directives * directives, arguments * arguments)
237 for (i = 0; i < directives->count; ++i) {
241 dp = &directives->dir[i];
243 /* %% has no arguments, for example */
244 if (dp->arg_index < 0)
247 a = &arguments->arg[dp->arg_index];
249 if (a->type == TYPE_POINTER_EXT) {
253 fmt[1] = POINTER_EXT_SIGNIFIER_CHAR;
254 fmt[2] = dp->ptr_ext_char;
258 __gst_printf_pointer_extension_serialize (fmt, a->a.a_pointer);
264 vasnprintf (char *resultbuf, size_t * lengthp, const char *format, va_list args)
269 if (printf_parse (format, &d, &a) < 0) {
276 while (a.count--) { \
277 if (a.arg[a.count].ext_string) \
278 free (a.arg[a.count].ext_string); \
283 if (printf_fetchargs (args, &a) < 0) {
289 /* collect TYPE_POINTER_EXT argument strings */
290 printf_postprocess_args (&d, &a);
294 (char *) alloca (7 + d.max_width_length + d.max_precision_length + 6);
298 /* Output string accumulator. */
303 if (resultbuf != NULL) {
305 allocated = *lengthp;
312 result is either == resultbuf or == NULL or malloc-allocated.
313 If length > 0, then result != NULL. */
315 #define ENSURE_ALLOCATION(needed) \
316 if ((needed) > allocated) \
320 allocated = (allocated > 0 ? 2 * allocated : 12); \
321 if ((needed) > allocated) \
322 allocated = (needed); \
323 if (result == resultbuf || result == NULL) \
324 memory = (char *) malloc (allocated); \
326 memory = (char *) realloc (result, allocated); \
328 if (memory == NULL) \
330 if (!(result == resultbuf || result == NULL)) \
337 if (result == resultbuf && length > 0) \
338 memcpy (memory, result, length); \
342 for (cp = format, i = 0, dp = &d.dir[0];; cp = dp->dir_end, i++, dp++) {
343 if (cp != dp->dir_start) {
344 size_t n = dp->dir_start - cp;
346 ENSURE_ALLOCATION (length + n);
347 memcpy (result + length, cp, n);
353 /* Execute a single directive. */
354 if (dp->conversion == '%') {
355 if (!(dp->arg_index < 0))
357 ENSURE_ALLOCATION (length + 1);
358 result[length] = '%';
361 if (!(dp->arg_index >= 0))
364 if (dp->conversion == 'n') {
365 switch (a.arg[dp->arg_index].type) {
366 case TYPE_COUNT_SCHAR_POINTER:
367 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
369 case TYPE_COUNT_SHORT_POINTER:
370 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
372 case TYPE_COUNT_INT_POINTER:
373 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
375 case TYPE_COUNT_LONGINT_POINTER:
376 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
378 #ifdef HAVE_LONG_LONG
379 case TYPE_COUNT_LONGLONGINT_POINTER:
380 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
387 arg_type type = a.arg[dp->arg_index].type;
389 unsigned int prefix_count;
391 #ifndef HAVE_SNPRINTF
392 unsigned int tmp_length;
396 /* Allocate a temporary buffer of sufficient size for calling
400 unsigned int precision;
403 if (dp->width_start != dp->width_end) {
404 if (dp->width_arg_index >= 0) {
407 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
409 arg = a.arg[dp->width_arg_index].a.a_int;
410 width = (arg < 0 ? -arg : arg);
412 const char *digitp = dp->width_start;
415 width = width * 10 + (*digitp++ - '0');
416 while (digitp != dp->width_end);
421 if (dp->precision_start != dp->precision_end) {
422 if (dp->precision_arg_index >= 0) {
425 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
427 arg = a.arg[dp->precision_arg_index].a.a_int;
428 precision = (arg < 0 ? 0 : arg);
430 const char *digitp = dp->precision_start + 1;
433 while (digitp != dp->precision_end)
434 precision = precision * 10 + (*digitp++ - '0');
438 switch (dp->conversion) {
442 # ifdef HAVE_LONG_LONG
443 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
444 tmp_length = (unsigned int) (sizeof (unsigned long long) * CHAR_BIT * 0.30103 /* binary -> decimal */
445 * 2 /* estimate for FLAG_GROUP */
447 + 1 /* turn floor into ceil */
448 + 1; /* account for leading sign */
451 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
452 tmp_length = (unsigned int) (sizeof (unsigned long) * CHAR_BIT * 0.30103 /* binary -> decimal */
453 * 2 /* estimate for FLAG_GROUP */
455 + 1 /* turn floor into ceil */
456 + 1; /* account for leading sign */
458 tmp_length = (unsigned int) (sizeof (unsigned int) * CHAR_BIT * 0.30103 /* binary -> decimal */
459 * 2 /* estimate for FLAG_GROUP */
461 + 1 /* turn floor into ceil */
462 + 1; /* account for leading sign */
466 # ifdef HAVE_LONG_LONG
467 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
468 tmp_length = (unsigned int) (sizeof (unsigned long long) * CHAR_BIT * 0.333334 /* binary -> octal */
470 + 1 /* turn floor into ceil */
471 + 1; /* account for leading sign */
474 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
475 tmp_length = (unsigned int) (sizeof (unsigned long) * CHAR_BIT * 0.333334 /* binary -> octal */
477 + 1 /* turn floor into ceil */
478 + 1; /* account for leading sign */
480 tmp_length = (unsigned int) (sizeof (unsigned int) * CHAR_BIT * 0.333334 /* binary -> octal */
482 + 1 /* turn floor into ceil */
483 + 1; /* account for leading sign */
488 # ifdef HAVE_LONG_LONG
489 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
490 tmp_length = (unsigned int) (sizeof (unsigned long long) * CHAR_BIT * 0.25 /* binary -> hexadecimal */
492 + 1 /* turn floor into ceil */
493 + 2; /* account for leading sign or alternate form */
496 # ifdef HAVE_INT64_AND_I64
497 if (type == TYPE_INT64 || type == TYPE_UINT64)
498 tmp_length = (unsigned int) (sizeof (unsigned __int64) * CHAR_BIT * 0.25 /* binary -> hexadecimal */
500 + 1 /* turn floor into ceil */
501 + 2; /* account for leading sign or alternate form */
504 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
505 tmp_length = (unsigned int) (sizeof (unsigned long) * CHAR_BIT * 0.25 /* binary -> hexadecimal */
507 + 1 /* turn floor into ceil */
508 + 2; /* account for leading sign or alternate form */
510 tmp_length = (unsigned int) (sizeof (unsigned int) * CHAR_BIT * 0.25 /* binary -> hexadecimal */
512 + 1 /* turn floor into ceil */
513 + 2; /* account for leading sign or alternate form */
518 # ifdef HAVE_LONG_DOUBLE
519 if (type == TYPE_LONGDOUBLE)
520 tmp_length = (unsigned int) (LDBL_MAX_EXP * 0.30103 /* binary -> decimal */
521 * 2 /* estimate for FLAG_GROUP */
523 + 1 /* turn floor into ceil */
524 + precision + 10; /* sign, decimal point etc. */
527 tmp_length = (unsigned int) (DBL_MAX_EXP * 0.30103 /* binary -> decimal */
528 * 2 /* estimate for FLAG_GROUP */
530 + 1 /* turn floor into ceil */
531 + precision + 10; /* sign, decimal point etc. */
540 tmp_length = precision + 12; /* sign, decimal point, exponent etc. */
545 if (type == TYPE_WIDE_CHAR)
546 tmp_length = MB_CUR_MAX;
554 if (type == TYPE_WIDE_STRING)
555 tmp_length = (a.arg[dp->arg_index].a.a_wide_string == NULL ? 6 /* wcslen(L"(null)") */
556 : local_wcslen (a.arg[dp->arg_index].a.a_wide_string))
560 tmp_length = a.arg[dp->arg_index].a.a_string == NULL ? 6 /* strlen("(null)") */
561 : strlen (a.arg[dp->arg_index].a.a_string);
565 tmp_length = (unsigned int) (sizeof (void *) * CHAR_BIT * 0.25 /* binary -> hexadecimal */
567 + 1 /* turn floor into ceil */
568 + 2; /* account for leading 0x */
570 /* make sure we always have enough space for a plain %p, so + */
571 if (dp->flags & FLAG_PTR_EXT && a.arg[dp->arg_index].ext_string)
572 tmp_length += strlen (a.arg[dp->arg_index].ext_string);
579 if (tmp_length < width)
582 tmp_length++; /* account for trailing NUL */
585 if (tmp_length <= sizeof (tmpbuf))
588 tmp = (char *) malloc (tmp_length);
591 if (!(result == resultbuf || result == NULL))
601 /* Construct the format string for calling snprintf or
605 if (dp->flags & FLAG_GROUP)
607 if (dp->flags & FLAG_LEFT)
609 if (dp->flags & FLAG_SHOWSIGN)
611 if (dp->flags & FLAG_SPACE)
613 if (dp->flags & FLAG_ALT)
615 if (dp->flags & FLAG_ZERO)
617 if (dp->width_start != dp->width_end) {
618 size_t n = dp->width_end - dp->width_start;
619 memcpy (p, dp->width_start, n);
622 if (dp->precision_start != dp->precision_end) {
623 size_t n = dp->precision_end - dp->precision_start;
624 memcpy (p, dp->precision_start, n);
629 #ifdef HAVE_INT64_AND_I64
637 #ifdef HAVE_LONG_LONG
638 case TYPE_LONGLONGINT:
639 case TYPE_ULONGLONGINT:
640 #ifdef HAVE_INT64_AND_I64 /* The system (sn)printf uses %I64. Also assume
641 * that long long == __int64.
658 case TYPE_WIDE_STRING:
662 #ifdef HAVE_LONG_DOUBLE
663 case TYPE_LONGDOUBLE:
679 /* Construct the arguments for calling snprintf or sprintf. */
681 if (dp->width_arg_index >= 0) {
682 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
684 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
686 if (dp->precision_arg_index >= 0) {
687 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
689 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
692 /* Prepare checking whether snprintf returns the count
694 ENSURE_ALLOCATION (length + 1);
695 result[length] = '\0';
705 maxlen = allocated - length;
711 #define SNPRINTF_BUF(arg) \
712 switch (prefix_count) \
715 retcount = snprintf (result + length, maxlen, buf, \
719 retcount = snprintf (result + length, maxlen, buf, \
720 prefixes[0], arg, &count); \
723 retcount = snprintf (result + length, maxlen, buf, \
724 prefixes[0], prefixes[1], arg, \
731 #define SNPRINTF_BUF(arg) \
732 switch (prefix_count) \
735 count = sprintf (tmp, buf, arg); \
738 count = sprintf (tmp, buf, prefixes[0], arg); \
741 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
752 int arg = a.arg[dp->arg_index].a.a_schar;
758 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
764 int arg = a.arg[dp->arg_index].a.a_short;
770 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
776 int arg = a.arg[dp->arg_index].a.a_int;
782 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
788 long int arg = a.arg[dp->arg_index].a.a_longint;
794 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
798 #ifdef HAVE_INT64_AND_I64
801 __int64 arg = a.arg[dp->arg_index].a.a_int64;
807 unsigned __int64 arg = a.arg[dp->arg_index].a.a_uint64;
812 #ifdef HAVE_LONG_LONG
813 #ifndef HAVE_LONG_LONG_FORMAT
814 case TYPE_LONGLONGINT:
815 case TYPE_ULONGLONGINT:
817 unsigned long long int arg =
818 a.arg[dp->arg_index].a.a_ulonglongint;
823 if (dp->width_start != dp->width_end) {
824 if (dp->width_arg_index >= 0) {
827 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
829 arg = a.arg[dp->width_arg_index].a.a_int;
830 width = (arg < 0 ? -arg : arg);
832 const char *digitp = dp->width_start;
835 width = width * 10 + (*digitp++ - '0');
836 while (digitp != dp->width_end);
841 if (dp->precision_start != dp->precision_end) {
842 if (dp->precision_arg_index >= 0) {
845 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
847 arg = a.arg[dp->precision_arg_index].a.a_int;
848 precision = (arg < 0 ? 0 : arg);
850 const char *digitp = dp->precision_start + 1;
854 precision = precision * 10 + (*digitp++ - '0');
855 while (digitp != dp->precision_end);
859 count = print_long_long (result + length, maxlen,
860 width, precision, dp->flags, dp->conversion, arg);
862 count = print_long_long (tmp, tmp_length,
863 width, precision, dp->flags, dp->conversion, arg);
868 case TYPE_LONGLONGINT:
870 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
874 case TYPE_ULONGLONGINT:
876 unsigned long long int arg =
877 a.arg[dp->arg_index].a.a_ulonglongint;
885 double arg = a.arg[dp->arg_index].a.a_double;
889 #ifdef HAVE_LONG_DOUBLE
890 case TYPE_LONGDOUBLE:
892 long double arg = a.arg[dp->arg_index].a.a_longdouble;
899 int arg = a.arg[dp->arg_index].a.a_char;
906 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
913 const char *arg = a.arg[dp->arg_index].a.a_string == NULL
914 ? "(null)" : a.arg[dp->arg_index].a.a_string;
919 case TYPE_WIDE_STRING:
922 a.arg[dp->arg_index].a.a_wide_string ==
923 NULL ? L"(null)" : a.arg[dp->arg_index].a.a_wide_string;
930 void *arg = a.arg[dp->arg_index].a.a_pointer;
934 case TYPE_POINTER_EXT:
936 void *arg = a.arg[dp->arg_index].a.a_pointer;
938 if (a.arg[dp->arg_index].ext_string != NULL) {
939 arg = a.arg[dp->arg_index].ext_string;
951 /* Portability: Not all implementations of snprintf()
952 are ISO C 99 compliant. Determine the number of
953 bytes that snprintf() has produced or would have
956 /* Verify that snprintf() has NUL-terminated its
958 if (count < maxlen && result[length + count] != '\0')
960 /* Portability hack. */
961 if (retcount > count)
964 /* snprintf() doesn't understand the '%n'
967 /* Don't use the '%n' directive; instead, look
968 at the snprintf() return value. */
976 /* Attempt to handle failure. */
978 if (!(result == resultbuf || result == NULL))
985 #ifndef HAVE_SNPRINTF
986 if (count >= tmp_length)
987 /* tmp_length was incorrectly calculated - fix the
992 /* Make room for the result. */
993 if (count >= maxlen) {
994 /* Need at least count bytes. But allocate
995 proportionally, to avoid looping eternally if
996 snprintf() reports a too small count. */
997 size_t n = length + count;
999 if (n < 2 * allocated)
1002 ENSURE_ALLOCATION (n);
1003 #ifdef HAVE_SNPRINTF
1007 #ifdef HAVE_SNPRINTF
1008 /* The snprintf() result did fit. */
1010 /* Append the sprintf() result. */
1011 memcpy (result + length, tmp, count);
1023 /* Add the final NUL. */
1024 ENSURE_ALLOCATION (length + 1);
1025 result[length] = '\0';
1027 if (result != resultbuf && length + 1 < allocated) {
1028 /* Shrink the allocated memory if possible. */
1031 memory = (char *) realloc (result, length + 1);