1 /* Formatted output to strings.
2 Copyright (C) 1999-2000, 2002-2003, 2006-2008, 2011, 2015 Free
3 Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* This file can be parametrized with the following macros:
19 CHAR_T The element type of the format string.
20 CHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
21 in the format string are ASCII.
22 DIRECTIVE Structure denoting a format directive.
24 DIRECTIVES Structure denoting the set of format directives of a
25 format string. Depends on CHAR_T.
26 PRINTF_PARSE Function that parses a format string.
28 STATIC Set to 'static' to declare the function static.
29 ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions. */
37 # include "printf-parse.h"
40 /* Default parameters. */
42 # define PRINTF_PARSE printf_parse
44 # define DIRECTIVE char_directive
45 # define DIRECTIVES char_directives
48 /* Get size_t, NULL. */
52 #if defined IN_LIBINTL || defined IN_LIBASPRINTF
53 # if HAVE_STDINT_H_WITH_UINTMAX
56 # if HAVE_INTTYPES_H_WITH_UINTMAX
57 # include <inttypes.h>
63 /* malloc(), realloc(), free(). */
72 /* Checked size_t computations. */
84 PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
86 const CHAR_T *cp = format; /* pointer into format */
87 size_t arg_posn = 0; /* number of regular arguments consumed */
88 size_t d_allocated; /* allocated elements of d->dir */
89 size_t a_allocated; /* allocated elements of a->arg */
90 size_t max_width_length = 0;
91 size_t max_precision_length = 0;
94 d_allocated = N_DIRECT_ALLOC_DIRECTIVES;
95 d->dir = d->direct_alloc_dir;
98 a_allocated = N_DIRECT_ALLOC_ARGUMENTS;
99 a->arg = a->direct_alloc_arg;
101 #define REGISTER_ARG(_index_,_type_) \
103 size_t n = (_index_); \
104 if (n >= a_allocated) \
106 size_t memory_size; \
109 a_allocated = xtimes (a_allocated, 2); \
110 if (a_allocated <= n) \
111 a_allocated = xsum (n, 1); \
112 memory_size = xtimes (a_allocated, sizeof (argument)); \
113 if (size_overflow_p (memory_size)) \
114 /* Overflow, would lead to out of memory. */ \
115 goto out_of_memory; \
116 memory = (argument *) (a->arg != a->direct_alloc_arg \
117 ? realloc (a->arg, memory_size) \
118 : malloc (memory_size)); \
119 if (memory == NULL) \
120 /* Out of memory. */ \
121 goto out_of_memory; \
122 if (a->arg == a->direct_alloc_arg) \
123 memcpy (memory, a->arg, a->count * sizeof (argument)); \
126 while (a->count <= n) \
127 a->arg[a->count++].type = TYPE_NONE; \
128 if (a->arg[n].type == TYPE_NONE) \
129 a->arg[n].type = (_type_); \
130 else if (a->arg[n].type != (_type_)) \
131 /* Ambiguous type for positional argument. */ \
140 size_t arg_index = ARG_NONE;
141 DIRECTIVE *dp = &d->dir[d->count]; /* pointer to next directive */
143 /* Initialize the next directive. */
144 dp->dir_start = cp - 1;
146 dp->width_start = NULL;
147 dp->width_end = NULL;
148 dp->width_arg_index = ARG_NONE;
149 dp->precision_start = NULL;
150 dp->precision_end = NULL;
151 dp->precision_arg_index = ARG_NONE;
152 dp->arg_index = ARG_NONE;
154 /* Test for positional argument. */
155 if (*cp >= '0' && *cp <= '9')
159 for (np = cp; *np >= '0' && *np <= '9'; np++)
165 for (np = cp; *np >= '0' && *np <= '9'; np++)
166 n = xsum (xtimes (n, 10), *np - '0');
168 /* Positional argument 0. */
170 if (size_overflow_p (n))
171 /* n too large, would lead to out of memory later. */
178 /* Read the flags. */
183 dp->flags |= FLAG_GROUP;
188 dp->flags |= FLAG_LEFT;
193 dp->flags |= FLAG_SHOWSIGN;
198 dp->flags |= FLAG_SPACE;
203 dp->flags |= FLAG_ALT;
208 dp->flags |= FLAG_ZERO;
211 #if __GLIBC__ >= 2 && !defined __UCLIBC__
214 dp->flags |= FLAG_LOCALIZED;
222 /* Parse the field width. */
225 dp->width_start = cp;
228 if (max_width_length < 1)
229 max_width_length = 1;
231 /* Test for positional argument. */
232 if (*cp >= '0' && *cp <= '9')
236 for (np = cp; *np >= '0' && *np <= '9'; np++)
242 for (np = cp; *np >= '0' && *np <= '9'; np++)
243 n = xsum (xtimes (n, 10), *np - '0');
245 /* Positional argument 0. */
247 if (size_overflow_p (n))
248 /* n too large, would lead to out of memory later. */
250 dp->width_arg_index = n - 1;
254 if (dp->width_arg_index == ARG_NONE)
256 dp->width_arg_index = arg_posn++;
257 if (dp->width_arg_index == ARG_NONE)
258 /* arg_posn wrapped around. */
261 REGISTER_ARG (dp->width_arg_index, TYPE_INT);
263 else if (*cp >= '0' && *cp <= '9')
267 dp->width_start = cp;
268 for (; *cp >= '0' && *cp <= '9'; cp++)
271 width_length = dp->width_end - dp->width_start;
272 if (max_width_length < width_length)
273 max_width_length = width_length;
276 /* Parse the precision. */
282 dp->precision_start = cp - 1;
284 dp->precision_end = cp;
285 if (max_precision_length < 2)
286 max_precision_length = 2;
288 /* Test for positional argument. */
289 if (*cp >= '0' && *cp <= '9')
293 for (np = cp; *np >= '0' && *np <= '9'; np++)
299 for (np = cp; *np >= '0' && *np <= '9'; np++)
300 n = xsum (xtimes (n, 10), *np - '0');
302 /* Positional argument 0. */
304 if (size_overflow_p (n))
305 /* n too large, would lead to out of memory
308 dp->precision_arg_index = n - 1;
312 if (dp->precision_arg_index == ARG_NONE)
314 dp->precision_arg_index = arg_posn++;
315 if (dp->precision_arg_index == ARG_NONE)
316 /* arg_posn wrapped around. */
319 REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
323 size_t precision_length;
325 dp->precision_start = cp - 1;
326 for (; *cp >= '0' && *cp <= '9'; cp++)
328 dp->precision_end = cp;
329 precision_length = dp->precision_end - dp->precision_start;
330 if (max_precision_length < precision_length)
331 max_precision_length = precision_length;
338 /* Parse argument type/size specifiers. */
346 flags |= (1 << (flags & 1));
361 if (sizeof (intmax_t) > sizeof (long))
363 /* intmax_t = long long */
366 else if (sizeof (intmax_t) > sizeof (int))
368 /* intmax_t = long */
373 else if (*cp == 'z' || *cp == 'Z')
375 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
376 because the warning facility in gcc-2.95.2 understands
377 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
378 if (sizeof (size_t) > sizeof (long))
380 /* size_t = long long */
383 else if (sizeof (size_t) > sizeof (int))
392 if (sizeof (ptrdiff_t) > sizeof (long))
394 /* ptrdiff_t = long long */
397 else if (sizeof (ptrdiff_t) > sizeof (int))
399 /* ptrdiff_t = long */
404 #if defined __APPLE__ && defined __MACH__
405 /* On MacOS X 10.3, PRIdMAX is defined as "qd".
406 We cannot change it to "lld" because PRIdMAX must also
407 be understood by the system's printf routines. */
410 if (64 / 8 > sizeof (long))
412 /* int64_t = long long */
423 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
424 /* On native Win32, PRIdMAX is defined as "I64d".
425 We cannot change it to "lld" because PRIdMAX must also
426 be understood by the system's printf routines. */
427 else if (*cp == 'I' && cp[1] == '6' && cp[2] == '4')
429 if (64 / 8 > sizeof (long))
431 /* __int64 = long long */
446 /* Read the conversion character. */
451 #if HAVE_LONG_LONG_INT
452 /* If 'long long' exists and is larger than 'long': */
453 if (flags >= 16 || (flags & 4))
454 type = TYPE_LONGLONGINT;
457 /* If 'long long' exists and is the same as 'long', we parse
458 "lld" into TYPE_LONGINT. */
468 case 'o': case 'u': case 'x': case 'X':
469 #if HAVE_LONG_LONG_INT
470 /* If 'long long' exists and is larger than 'long': */
471 if (flags >= 16 || (flags & 4))
472 type = TYPE_ULONGLONGINT;
475 /* If 'unsigned long long' exists and is the same as
476 'unsigned long', we parse "llu" into TYPE_ULONGINT. */
478 type = TYPE_ULONGINT;
486 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
488 if (flags >= 16 || (flags & 4))
489 type = TYPE_LONGDOUBLE;
496 type = TYPE_WIDE_CHAR;
505 type = TYPE_WIDE_CHAR;
512 type = TYPE_WIDE_STRING;
521 type = TYPE_WIDE_STRING;
529 #if HAVE_LONG_LONG_INT
530 /* If 'long long' exists and is larger than 'long': */
531 if (flags >= 16 || (flags & 4))
532 type = TYPE_COUNT_LONGLONGINT_POINTER;
535 /* If 'long long' exists and is the same as 'long', we parse
536 "lln" into TYPE_COUNT_LONGINT_POINTER. */
538 type = TYPE_COUNT_LONGINT_POINTER;
540 type = TYPE_COUNT_SCHAR_POINTER;
542 type = TYPE_COUNT_SHORT_POINTER;
544 type = TYPE_COUNT_INT_POINTER;
547 /* The unistdio extensions. */
550 type = TYPE_U32_STRING;
552 type = TYPE_U16_STRING;
554 type = TYPE_U8_STRING;
561 /* Unknown conversion character. */
566 if (type != TYPE_NONE)
568 dp->arg_index = arg_index;
569 if (dp->arg_index == ARG_NONE)
571 dp->arg_index = arg_posn++;
572 if (dp->arg_index == ARG_NONE)
573 /* arg_posn wrapped around. */
576 REGISTER_ARG (dp->arg_index, type);
583 if (d->count >= d_allocated)
588 d_allocated = xtimes (d_allocated, 2);
589 memory_size = xtimes (d_allocated, sizeof (DIRECTIVE));
590 if (size_overflow_p (memory_size))
591 /* Overflow, would lead to out of memory. */
593 memory = (DIRECTIVE *) (d->dir != d->direct_alloc_dir
594 ? realloc (d->dir, memory_size)
595 : malloc (memory_size));
599 if (d->dir == d->direct_alloc_dir)
600 memcpy (memory, d->dir, d->count * sizeof (DIRECTIVE));
604 #if CHAR_T_ONLY_ASCII
605 else if (!c_isascii (c))
607 /* Non-ASCII character. Not supported. */
612 d->dir[d->count].dir_start = cp;
614 d->max_width_length = max_width_length;
615 d->max_precision_length = max_precision_length;
619 if (a->arg != a->direct_alloc_arg)
621 if (d->dir != d->direct_alloc_dir)
627 if (a->arg != a->direct_alloc_arg)
629 if (d->dir != d->direct_alloc_dir)
638 #undef CHAR_T_ONLY_ASCII