1 /* Formatted output to strings.
2 Copyright (C) 1999-2000, 2002-2003, 2006-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file 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
15 along 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. */
36 # include "printf-parse.h"
39 /* Default parameters. */
41 # define PRINTF_PARSE printf_parse
43 # define DIRECTIVE char_directive
44 # define DIRECTIVES char_directives
47 /* Get size_t, NULL. */
53 /* malloc(), realloc(), free(). */
62 /* Checked size_t computations. */
74 PRINTF_PARSE (const CHAR_T
*format
, DIRECTIVES
*d
, arguments
*a
)
76 const CHAR_T
*cp
= format
; /* pointer into format */
77 size_t arg_posn
= 0; /* number of regular arguments consumed */
78 size_t d_allocated
; /* allocated elements of d->dir */
79 size_t a_allocated
; /* allocated elements of a->arg */
80 size_t max_width_length
= 0;
81 size_t max_precision_length
= 0;
84 d_allocated
= N_DIRECT_ALLOC_DIRECTIVES
;
85 d
->dir
= d
->direct_alloc_dir
;
88 a_allocated
= N_DIRECT_ALLOC_ARGUMENTS
;
89 a
->arg
= a
->direct_alloc_arg
;
91 #define REGISTER_ARG(_index_,_type_) \
93 size_t n = (_index_); \
94 if (n >= a_allocated) \
99 a_allocated = xtimes (a_allocated, 2); \
100 if (a_allocated <= n) \
101 a_allocated = xsum (n, 1); \
102 memory_size = xtimes (a_allocated, sizeof (argument)); \
103 if (size_overflow_p (memory_size)) \
104 /* Overflow, would lead to out of memory. */ \
105 goto out_of_memory; \
106 memory = (argument *) (a->arg != a->direct_alloc_arg \
107 ? realloc (a->arg, memory_size) \
108 : malloc (memory_size)); \
109 if (memory == NULL) \
110 /* Out of memory. */ \
111 goto out_of_memory; \
112 if (a->arg == a->direct_alloc_arg) \
113 memcpy (memory, a->arg, a->count * sizeof (argument)); \
116 while (a->count <= n) \
117 a->arg[a->count++].type = TYPE_NONE; \
118 if (a->arg[n].type == TYPE_NONE) \
119 a->arg[n].type = (_type_); \
120 else if (a->arg[n].type != (_type_)) \
121 /* Ambiguous type for positional argument. */ \
130 size_t arg_index
= ARG_NONE
;
131 DIRECTIVE
*dp
= &d
->dir
[d
->count
]; /* pointer to next directive */
133 /* Initialize the next directive. */
134 dp
->dir_start
= cp
- 1;
136 dp
->width_start
= NULL
;
137 dp
->width_end
= NULL
;
138 dp
->width_arg_index
= ARG_NONE
;
139 dp
->precision_start
= NULL
;
140 dp
->precision_end
= NULL
;
141 dp
->precision_arg_index
= ARG_NONE
;
142 dp
->arg_index
= ARG_NONE
;
144 /* Test for positional argument. */
145 if (*cp
>= '0' && *cp
<= '9')
149 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
155 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
156 n
= xsum (xtimes (n
, 10), *np
- '0');
158 /* Positional argument 0. */
160 if (size_overflow_p (n
))
161 /* n too large, would lead to out of memory later. */
168 /* Read the flags. */
173 dp
->flags
|= FLAG_GROUP
;
178 dp
->flags
|= FLAG_LEFT
;
183 dp
->flags
|= FLAG_SHOWSIGN
;
188 dp
->flags
|= FLAG_SPACE
;
193 dp
->flags
|= FLAG_ALT
;
198 dp
->flags
|= FLAG_ZERO
;
201 #if __GLIBC__ >= 2 && !defined __UCLIBC__
204 dp
->flags
|= FLAG_LOCALIZED
;
212 /* Parse the field width. */
215 dp
->width_start
= cp
;
218 if (max_width_length
< 1)
219 max_width_length
= 1;
221 /* Test for positional argument. */
222 if (*cp
>= '0' && *cp
<= '9')
226 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
232 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
233 n
= xsum (xtimes (n
, 10), *np
- '0');
235 /* Positional argument 0. */
237 if (size_overflow_p (n
))
238 /* n too large, would lead to out of memory later. */
240 dp
->width_arg_index
= n
- 1;
244 if (dp
->width_arg_index
== ARG_NONE
)
246 dp
->width_arg_index
= arg_posn
++;
247 if (dp
->width_arg_index
== ARG_NONE
)
248 /* arg_posn wrapped around. */
251 REGISTER_ARG (dp
->width_arg_index
, TYPE_INT
);
253 else if (*cp
>= '0' && *cp
<= '9')
257 dp
->width_start
= cp
;
258 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
261 width_length
= dp
->width_end
- dp
->width_start
;
262 if (max_width_length
< width_length
)
263 max_width_length
= width_length
;
266 /* Parse the precision. */
272 dp
->precision_start
= cp
- 1;
274 dp
->precision_end
= cp
;
275 if (max_precision_length
< 2)
276 max_precision_length
= 2;
278 /* Test for positional argument. */
279 if (*cp
>= '0' && *cp
<= '9')
283 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
289 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
290 n
= xsum (xtimes (n
, 10), *np
- '0');
292 /* Positional argument 0. */
294 if (size_overflow_p (n
))
295 /* n too large, would lead to out of memory
298 dp
->precision_arg_index
= n
- 1;
302 if (dp
->precision_arg_index
== ARG_NONE
)
304 dp
->precision_arg_index
= arg_posn
++;
305 if (dp
->precision_arg_index
== ARG_NONE
)
306 /* arg_posn wrapped around. */
309 REGISTER_ARG (dp
->precision_arg_index
, TYPE_INT
);
313 size_t precision_length
;
315 dp
->precision_start
= cp
- 1;
316 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
318 dp
->precision_end
= cp
;
319 precision_length
= dp
->precision_end
- dp
->precision_start
;
320 if (max_precision_length
< precision_length
)
321 max_precision_length
= precision_length
;
328 /* Parse argument type/size specifiers. */
329 /* Relevant for the conversion characters d, i. */
330 arg_type signed_type
= TYPE_INT
;
331 /* Relevant for the conversion characters b, o, u, x, X. */
332 arg_type unsigned_type
= TYPE_UINT
;
333 /* Relevant for the conversion characters n. */
334 arg_type pointer_type
= TYPE_COUNT_INT_POINTER
;
335 /* Relevant for the conversion characters a, A, e, E, f, F, g, G. */
336 arg_type floatingpoint_type
= TYPE_DOUBLE
;
342 signed_type
= TYPE_SCHAR
;
343 unsigned_type
= TYPE_UCHAR
;
344 pointer_type
= TYPE_COUNT_SCHAR_POINTER
;
349 signed_type
= TYPE_SHORT
;
350 unsigned_type
= TYPE_USHORT
;
351 pointer_type
= TYPE_COUNT_SHORT_POINTER
;
359 signed_type
= TYPE_LONGLONGINT
;
360 unsigned_type
= TYPE_ULONGLONGINT
;
361 pointer_type
= TYPE_COUNT_LONGLONGINT_POINTER
;
362 /* For backward compatibility only. */
363 floatingpoint_type
= TYPE_LONGDOUBLE
;
368 signed_type
= TYPE_LONGINT
;
369 unsigned_type
= TYPE_ULONGINT
;
370 pointer_type
= TYPE_COUNT_LONGINT_POINTER
;
376 if (sizeof (intmax_t) > sizeof (long))
378 /* intmax_t = long long */
379 signed_type
= TYPE_LONGLONGINT
;
380 unsigned_type
= TYPE_ULONGLONGINT
;
381 pointer_type
= TYPE_COUNT_LONGLONGINT_POINTER
;
382 /* For backward compatibility only. */
383 floatingpoint_type
= TYPE_LONGDOUBLE
;
385 else if (sizeof (intmax_t) > sizeof (int))
387 /* intmax_t = long */
388 signed_type
= TYPE_LONGINT
;
389 unsigned_type
= TYPE_ULONGINT
;
390 pointer_type
= TYPE_COUNT_LONGINT_POINTER
;
394 else if (*cp
== 'z' || *cp
== 'Z')
396 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
397 because the warning facility in gcc-2.95.2 understands
398 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
399 if (sizeof (size_t) > sizeof (long))
401 /* size_t = unsigned long long */
402 signed_type
= TYPE_LONGLONGINT
;
403 unsigned_type
= TYPE_ULONGLONGINT
;
404 pointer_type
= TYPE_COUNT_LONGLONGINT_POINTER
;
405 /* For backward compatibility only. */
406 floatingpoint_type
= TYPE_LONGDOUBLE
;
408 else if (sizeof (size_t) > sizeof (int))
410 /* size_t = unsigned long */
411 signed_type
= TYPE_LONGINT
;
412 unsigned_type
= TYPE_ULONGINT
;
413 pointer_type
= TYPE_COUNT_LONGINT_POINTER
;
419 if (sizeof (ptrdiff_t) > sizeof (long))
421 /* ptrdiff_t = long long */
422 signed_type
= TYPE_LONGLONGINT
;
423 unsigned_type
= TYPE_ULONGLONGINT
;
424 pointer_type
= TYPE_COUNT_LONGLONGINT_POINTER
;
425 /* For backward compatibility only. */
426 floatingpoint_type
= TYPE_LONGDOUBLE
;
428 else if (sizeof (ptrdiff_t) > sizeof (int))
430 /* ptrdiff_t = long */
431 signed_type
= TYPE_LONGINT
;
432 unsigned_type
= TYPE_ULONGINT
;
433 pointer_type
= TYPE_COUNT_LONGINT_POINTER
;
439 /* wN and wfN are standardized in ISO C 23. */
444 signed_type
= TYPE_INT_FAST8_T
;
445 unsigned_type
= TYPE_UINT_FAST8_T
;
446 pointer_type
= TYPE_COUNT_INT_FAST8_T_POINTER
;
449 else if (cp
[2] == '1' && cp
[3] == '6')
451 signed_type
= TYPE_INT_FAST16_T
;
452 unsigned_type
= TYPE_UINT_FAST16_T
;
453 pointer_type
= TYPE_COUNT_INT_FAST16_T_POINTER
;
456 else if (cp
[2] == '3' && cp
[3] == '2')
458 signed_type
= TYPE_INT_FAST32_T
;
459 unsigned_type
= TYPE_UINT_FAST32_T
;
460 pointer_type
= TYPE_COUNT_INT_FAST32_T_POINTER
;
463 else if (cp
[2] == '6' && cp
[3] == '4')
465 signed_type
= TYPE_INT_FAST64_T
;
466 unsigned_type
= TYPE_UINT_FAST64_T
;
467 pointer_type
= TYPE_COUNT_INT_FAST64_T_POINTER
;
475 signed_type
= TYPE_INT8_T
;
476 unsigned_type
= TYPE_UINT8_T
;
477 pointer_type
= TYPE_COUNT_INT8_T_POINTER
;
480 else if (cp
[1] == '1' && cp
[2] == '6')
482 signed_type
= TYPE_INT16_T
;
483 unsigned_type
= TYPE_UINT16_T
;
484 pointer_type
= TYPE_COUNT_INT16_T_POINTER
;
487 else if (cp
[1] == '3' && cp
[2] == '2')
489 signed_type
= TYPE_INT32_T
;
490 unsigned_type
= TYPE_UINT32_T
;
491 pointer_type
= TYPE_COUNT_INT32_T_POINTER
;
494 else if (cp
[1] == '6' && cp
[2] == '4')
496 signed_type
= TYPE_INT64_T
;
497 unsigned_type
= TYPE_UINT64_T
;
498 pointer_type
= TYPE_COUNT_INT64_T_POINTER
;
505 signed_type
= TYPE_LONGLONGINT
;
506 unsigned_type
= TYPE_ULONGLONGINT
;
507 pointer_type
= TYPE_COUNT_LONGLONGINT_POINTER
;
508 floatingpoint_type
= TYPE_LONGDOUBLE
;
511 #if defined __APPLE__ && defined __MACH__
512 /* On Mac OS X 10.3, PRIdMAX is defined as "qd".
513 We cannot change it to "lld" because PRIdMAX must also
514 be understood by the system's printf routines. */
517 if (64 / 8 > sizeof (long))
519 /* int64_t = long long */
520 signed_type
= TYPE_LONGLONGINT
;
521 unsigned_type
= TYPE_ULONGLONGINT
;
522 pointer_type
= TYPE_COUNT_LONGLONGINT_POINTER
;
523 /* For backward compatibility only. */
524 floatingpoint_type
= TYPE_LONGDOUBLE
;
529 signed_type
= TYPE_LONGINT
;
530 unsigned_type
= TYPE_ULONGINT
;
531 pointer_type
= TYPE_COUNT_LONGINT_POINTER
;
536 #if defined _WIN32 && ! defined __CYGWIN__
537 /* On native Windows, PRIdMAX is defined as "I64d".
538 We cannot change it to "lld" because PRIdMAX must also
539 be understood by the system's printf routines. */
540 else if (*cp
== 'I' && cp
[1] == '6' && cp
[2] == '4')
542 if (64 / 8 > sizeof (long))
544 /* __int64_t = long long */
545 signed_type
= TYPE_LONGLONGINT
;
546 unsigned_type
= TYPE_ULONGLONGINT
;
547 pointer_type
= TYPE_COUNT_LONGLONGINT_POINTER
;
548 /* For backward compatibility only. */
549 floatingpoint_type
= TYPE_LONGDOUBLE
;
553 /* __int64_t = long */
554 signed_type
= TYPE_LONGINT
;
555 unsigned_type
= TYPE_ULONGINT
;
556 pointer_type
= TYPE_COUNT_LONGINT_POINTER
;
563 /* Read the conversion character. */
570 case 'b': case 'o': case 'u': case 'x': case 'X':
571 #if SUPPORT_GNU_PRINTF_DIRECTIVES \
572 || (__GLIBC__ + (__GLIBC_MINOR__ >= 35) > 2)
575 type
= unsigned_type
;
577 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
579 type
= floatingpoint_type
;
582 if (signed_type
== TYPE_LONGINT
583 /* For backward compatibility only. */
584 || signed_type
== TYPE_LONGLONGINT
)
586 type
= TYPE_WIDE_CHAR
;
595 type
= TYPE_WIDE_CHAR
;
600 if (signed_type
== TYPE_LONGINT
601 /* For backward compatibility only. */
602 || signed_type
== TYPE_LONGLONGINT
)
604 type
= TYPE_WIDE_STRING
;
613 type
= TYPE_WIDE_STRING
;
620 #if NEED_PRINTF_WITH_N_DIRECTIVE
626 /* The unistdio extensions. */
628 if (signed_type
== TYPE_LONGLONGINT
)
629 type
= TYPE_U32_STRING
;
630 else if (signed_type
== TYPE_LONGINT
)
631 type
= TYPE_U16_STRING
;
633 type
= TYPE_U8_STRING
;
640 /* Unknown conversion character. */
644 if (type
!= TYPE_NONE
)
646 dp
->arg_index
= arg_index
;
647 if (dp
->arg_index
== ARG_NONE
)
649 dp
->arg_index
= arg_posn
++;
650 if (dp
->arg_index
== ARG_NONE
)
651 /* arg_posn wrapped around. */
654 REGISTER_ARG (dp
->arg_index
, type
);
661 if (d
->count
>= d_allocated
)
666 d_allocated
= xtimes (d_allocated
, 2);
667 memory_size
= xtimes (d_allocated
, sizeof (DIRECTIVE
));
668 if (size_overflow_p (memory_size
))
669 /* Overflow, would lead to out of memory. */
671 memory
= (DIRECTIVE
*) (d
->dir
!= d
->direct_alloc_dir
672 ? realloc (d
->dir
, memory_size
)
673 : malloc (memory_size
));
677 if (d
->dir
== d
->direct_alloc_dir
)
678 memcpy (memory
, d
->dir
, d
->count
* sizeof (DIRECTIVE
));
682 #if CHAR_T_ONLY_ASCII
683 else if (!c_isascii (c
))
685 /* Non-ASCII character. Not supported. */
690 d
->dir
[d
->count
].dir_start
= cp
;
692 d
->max_width_length
= max_width_length
;
693 d
->max_precision_length
= max_precision_length
;
697 if (a
->arg
!= a
->direct_alloc_arg
)
699 if (d
->dir
!= d
->direct_alloc_dir
)
705 if (a
->arg
!= a
->direct_alloc_arg
)
707 if (d
->dir
!= d
->direct_alloc_dir
)
716 #undef CHAR_T_ONLY_ASCII