1 /* Formatted output to strings.
2 Copyright (C) 1999-2000, 2002-2003, 2006-2019 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, see <https://www.gnu.org/licenses/>. */
17 /* This file can be parametrized with the following macros:
18 CHAR_T The element type of the format string.
19 CHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
20 in the format string are ASCII.
21 DIRECTIVE Structure denoting a format directive.
23 DIRECTIVES Structure denoting the set of format directives of a
24 format string. Depends on CHAR_T.
25 PRINTF_PARSE Function that parses a format string.
27 STATIC Set to 'static' to declare the function static.
28 ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions. */
38 # include "printf-parse.h"
41 /* Default parameters. */
43 # define PRINTF_PARSE printf_parse
45 # define DIRECTIVE char_directive
46 # define DIRECTIVES char_directives
49 /* Get size_t, NULL. */
53 #if defined IN_LIBINTL || defined IN_LIBASPRINTF
54 # if HAVE_STDINT_H_WITH_UINTMAX
57 # if HAVE_INTTYPES_H_WITH_UINTMAX
58 # include <inttypes.h>
64 /* malloc(), realloc(), free(). */
73 /* Checked size_t computations. */
85 PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
87 const CHAR_T *cp = format; /* pointer into format */
88 size_t arg_posn = 0; /* number of regular arguments consumed */
89 size_t d_allocated; /* allocated elements of d->dir */
90 size_t a_allocated; /* allocated elements of a->arg */
91 size_t max_width_length = 0;
92 size_t max_precision_length = 0;
95 d_allocated = N_DIRECT_ALLOC_DIRECTIVES;
96 d->dir = d->direct_alloc_dir;
99 a_allocated = N_DIRECT_ALLOC_ARGUMENTS;
100 a->arg = a->direct_alloc_arg;
102 #define REGISTER_ARG(_index_,_type_) \
104 size_t n = (_index_); \
105 if (n >= a_allocated) \
107 size_t memory_size; \
110 a_allocated = xtimes (a_allocated, 2); \
111 if (a_allocated <= n) \
112 a_allocated = xsum (n, 1); \
113 memory_size = xtimes (a_allocated, sizeof (argument)); \
114 if (size_overflow_p (memory_size)) \
115 /* Overflow, would lead to out of memory. */ \
116 goto out_of_memory; \
117 memory = (argument *) (a->arg != a->direct_alloc_arg \
118 ? realloc (a->arg, memory_size) \
119 : malloc (memory_size)); \
120 if (memory == NULL) \
121 /* Out of memory. */ \
122 goto out_of_memory; \
123 if (a->arg == a->direct_alloc_arg) \
124 memcpy (memory, a->arg, a->count * sizeof (argument)); \
127 while (a->count <= n) \
128 a->arg[a->count++].type = TYPE_NONE; \
129 if (a->arg[n].type == TYPE_NONE) \
130 a->arg[n].type = (_type_); \
131 else if (a->arg[n].type != (_type_)) \
132 /* Ambiguous type for positional argument. */ \
141 size_t arg_index = ARG_NONE;
142 DIRECTIVE *dp = &d->dir[d->count]; /* pointer to next directive */
144 /* Initialize the next directive. */
145 dp->dir_start = cp - 1;
147 dp->width_start = NULL;
148 dp->width_end = NULL;
149 dp->width_arg_index = ARG_NONE;
150 dp->precision_start = NULL;
151 dp->precision_end = NULL;
152 dp->precision_arg_index = ARG_NONE;
153 dp->arg_index = ARG_NONE;
155 /* Test for positional argument. */
156 if (*cp >= '0' && *cp <= '9')
160 for (np = cp; *np >= '0' && *np <= '9'; np++)
166 for (np = cp; *np >= '0' && *np <= '9'; np++)
167 n = xsum (xtimes (n, 10), *np - '0');
169 /* Positional argument 0. */
171 if (size_overflow_p (n))
172 /* n too large, would lead to out of memory later. */
179 /* Read the flags. */
184 dp->flags |= FLAG_GROUP;
189 dp->flags |= FLAG_LEFT;
194 dp->flags |= FLAG_SHOWSIGN;
199 dp->flags |= FLAG_SPACE;
204 dp->flags |= FLAG_ALT;
209 dp->flags |= FLAG_ZERO;
212 #if __GLIBC__ >= 2 && !defined __UCLIBC__
215 dp->flags |= FLAG_LOCALIZED;
223 /* Parse the field width. */
226 dp->width_start = cp;
229 if (max_width_length < 1)
230 max_width_length = 1;
232 /* Test for positional argument. */
233 if (*cp >= '0' && *cp <= '9')
237 for (np = cp; *np >= '0' && *np <= '9'; np++)
243 for (np = cp; *np >= '0' && *np <= '9'; np++)
244 n = xsum (xtimes (n, 10), *np - '0');
246 /* Positional argument 0. */
248 if (size_overflow_p (n))
249 /* n too large, would lead to out of memory later. */
251 dp->width_arg_index = n - 1;
255 if (dp->width_arg_index == ARG_NONE)
257 dp->width_arg_index = arg_posn++;
258 if (dp->width_arg_index == ARG_NONE)
259 /* arg_posn wrapped around. */
262 REGISTER_ARG (dp->width_arg_index, TYPE_INT);
264 else if (*cp >= '0' && *cp <= '9')
268 dp->width_start = cp;
269 for (; *cp >= '0' && *cp <= '9'; cp++)
272 width_length = dp->width_end - dp->width_start;
273 if (max_width_length < width_length)
274 max_width_length = width_length;
277 /* Parse the precision. */
283 dp->precision_start = cp - 1;
285 dp->precision_end = cp;
286 if (max_precision_length < 2)
287 max_precision_length = 2;
289 /* Test for positional argument. */
290 if (*cp >= '0' && *cp <= '9')
294 for (np = cp; *np >= '0' && *np <= '9'; np++)
300 for (np = cp; *np >= '0' && *np <= '9'; np++)
301 n = xsum (xtimes (n, 10), *np - '0');
303 /* Positional argument 0. */
305 if (size_overflow_p (n))
306 /* n too large, would lead to out of memory
309 dp->precision_arg_index = n - 1;
313 if (dp->precision_arg_index == ARG_NONE)
315 dp->precision_arg_index = arg_posn++;
316 if (dp->precision_arg_index == ARG_NONE)
317 /* arg_posn wrapped around. */
320 REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
324 size_t precision_length;
326 dp->precision_start = cp - 1;
327 for (; *cp >= '0' && *cp <= '9'; cp++)
329 dp->precision_end = cp;
330 precision_length = dp->precision_end - dp->precision_start;
331 if (max_precision_length < precision_length)
332 max_precision_length = precision_length;
339 /* Parse argument type/size specifiers. */
347 flags |= (1 << (flags & 1));
362 if (sizeof (intmax_t) > sizeof (long))
364 /* intmax_t = long long */
367 else if (sizeof (intmax_t) > sizeof (int))
369 /* intmax_t = long */
374 else if (*cp == 'z' || *cp == 'Z')
376 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
377 because the warning facility in gcc-2.95.2 understands
378 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
379 if (sizeof (size_t) > sizeof (long))
381 /* size_t = long long */
384 else if (sizeof (size_t) > sizeof (int))
393 if (sizeof (ptrdiff_t) > sizeof (long))
395 /* ptrdiff_t = long long */
398 else if (sizeof (ptrdiff_t) > sizeof (int))
400 /* ptrdiff_t = long */
405 #if defined __APPLE__ && defined __MACH__
406 /* On Mac OS X 10.3, PRIdMAX is defined as "qd".
407 We cannot change it to "lld" because PRIdMAX must also
408 be understood by the system's printf routines. */
411 if (64 / 8 > sizeof (long))
413 /* int64_t = long long */
424 #if defined _WIN32 && ! defined __CYGWIN__
425 /* On native Windows, PRIdMAX is defined as "I64d".
426 We cannot change it to "lld" because PRIdMAX must also
427 be understood by the system's printf routines. */
428 else if (*cp == 'I' && cp[1] == '6' && cp[2] == '4')
430 if (64 / 8 > sizeof (long))
432 /* __int64 = long long */
447 /* Read the conversion character. */
453 /* If 'long long' exists and is larger than 'long': */
454 if (flags >= 16 || (flags & 4))
455 type = TYPE_LONGLONGINT;
458 /* If 'long long' exists and is the same as 'long', we parse
459 "lld" into TYPE_LONGINT. */
469 case 'o': case 'u': case 'x': case 'X':
471 /* If 'long long' exists and is larger than 'long': */
472 if (flags >= 16 || (flags & 4))
473 type = TYPE_ULONGLONGINT;
476 /* If 'unsigned long long' exists and is the same as
477 'unsigned long', we parse "llu" into TYPE_ULONGINT. */
479 type = TYPE_ULONGINT;
487 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
489 if (flags >= 16 || (flags & 4))
490 type = TYPE_LONGDOUBLE;
497 type = TYPE_WIDE_CHAR;
506 type = TYPE_WIDE_CHAR;
513 type = TYPE_WIDE_STRING;
522 type = TYPE_WIDE_STRING;
531 /* If 'long long' exists and is larger than 'long': */
532 if (flags >= 16 || (flags & 4))
533 type = TYPE_COUNT_LONGLONGINT_POINTER;
536 /* If 'long long' exists and is the same as 'long', we parse
537 "lln" into TYPE_COUNT_LONGINT_POINTER. */
539 type = TYPE_COUNT_LONGINT_POINTER;
541 type = TYPE_COUNT_SCHAR_POINTER;
543 type = TYPE_COUNT_SHORT_POINTER;
545 type = TYPE_COUNT_INT_POINTER;
548 /* The unistdio extensions. */
551 type = TYPE_U32_STRING;
553 type = TYPE_U16_STRING;
555 type = TYPE_U8_STRING;
562 /* Unknown conversion character. */
567 if (type != TYPE_NONE)
569 dp->arg_index = arg_index;
570 if (dp->arg_index == ARG_NONE)
572 dp->arg_index = arg_posn++;
573 if (dp->arg_index == ARG_NONE)
574 /* arg_posn wrapped around. */
577 REGISTER_ARG (dp->arg_index, type);
584 if (d->count >= d_allocated)
589 d_allocated = xtimes (d_allocated, 2);
590 memory_size = xtimes (d_allocated, sizeof (DIRECTIVE));
591 if (size_overflow_p (memory_size))
592 /* Overflow, would lead to out of memory. */
594 memory = (DIRECTIVE *) (d->dir != d->direct_alloc_dir
595 ? realloc (d->dir, memory_size)
596 : malloc (memory_size));
600 if (d->dir == d->direct_alloc_dir)
601 memcpy (memory, d->dir, d->count * sizeof (DIRECTIVE));
605 #if CHAR_T_ONLY_ASCII
606 else if (!c_isascii (c))
608 /* Non-ASCII character. Not supported. */
613 d->dir[d->count].dir_start = cp;
615 d->max_width_length = max_width_length;
616 d->max_precision_length = max_precision_length;
620 if (a->arg != a->direct_alloc_arg)
622 if (d->dir != d->direct_alloc_dir)
628 if (a->arg != a->direct_alloc_arg)
630 if (d->dir != d->direct_alloc_dir)
639 #undef CHAR_T_ONLY_ASCII