1 /* Formatted output to strings.
2 Copyright (C) 1999-2000, 2002-2003, 2006-2016 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 <http://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"
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>
61 # if !defined (_MSC_VER) || (_MSC_VER >= 1600)
64 typedef signed __int64
intmax_t;
68 /* malloc(), realloc(), free(). */
86 PRINTF_PARSE (const CHAR_T
*format
, DIRECTIVES
*d
, arguments
*a
)
88 const CHAR_T
*cp
= format
; /* pointer into format */
89 size_t arg_posn
= 0; /* number of regular arguments consumed */
90 size_t d_allocated
; /* allocated elements of d->dir */
91 size_t a_allocated
; /* allocated elements of a->arg */
92 size_t max_width_length
= 0;
93 size_t max_precision_length
= 0;
96 d_allocated
= N_DIRECT_ALLOC_DIRECTIVES
;
97 d
->dir
= d
->direct_alloc_dir
;
100 a_allocated
= N_DIRECT_ALLOC_ARGUMENTS
;
101 a
->arg
= a
->direct_alloc_arg
;
103 #define REGISTER_ARG(_index_,_type_) \
105 size_t n = (_index_); \
106 if (n >= a_allocated) \
108 size_t memory_size; \
111 a_allocated = xtimes (a_allocated, 2); \
112 if (a_allocated <= n) \
113 a_allocated = xsum (n, 1); \
114 memory_size = xtimes (a_allocated, sizeof (argument)); \
115 if (size_overflow_p (memory_size)) \
116 /* Overflow, would lead to out of memory. */ \
117 goto out_of_memory; \
118 memory = (argument *) (a->arg != a->direct_alloc_arg \
119 ? realloc (a->arg, memory_size) \
120 : malloc (memory_size)); \
121 if (memory == NULL) \
122 /* Out of memory. */ \
123 goto out_of_memory; \
124 if (a->arg == a->direct_alloc_arg) \
125 memcpy (memory, a->arg, a->count * sizeof (argument)); \
128 while (a->count <= n) \
129 a->arg[a->count++].type = TYPE_NONE; \
130 if (a->arg[n].type == TYPE_NONE) \
131 a->arg[n].type = (_type_); \
132 else if (a->arg[n].type != (_type_)) \
133 /* Ambiguous type for positional argument. */ \
142 size_t arg_index
= ARG_NONE
;
143 DIRECTIVE
*dp
= &d
->dir
[d
->count
]; /* pointer to next directive */
145 /* Initialize the next directive. */
146 dp
->dir_start
= cp
- 1;
148 dp
->width_start
= NULL
;
149 dp
->width_end
= NULL
;
150 dp
->width_arg_index
= ARG_NONE
;
151 dp
->precision_start
= NULL
;
152 dp
->precision_end
= NULL
;
153 dp
->precision_arg_index
= ARG_NONE
;
154 dp
->arg_index
= ARG_NONE
;
156 /* Test for positional argument. */
157 if (*cp
>= '0' && *cp
<= '9')
161 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
167 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
168 n
= xsum (xtimes (n
, 10), *np
- '0');
170 /* Positional argument 0. */
172 if (size_overflow_p (n
))
173 /* n too large, would lead to out of memory later. */
180 /* Read the flags. */
185 dp
->flags
|= FLAG_GROUP
;
190 dp
->flags
|= FLAG_LEFT
;
195 dp
->flags
|= FLAG_SHOWSIGN
;
200 dp
->flags
|= FLAG_SPACE
;
205 dp
->flags
|= FLAG_ALT
;
210 dp
->flags
|= FLAG_ZERO
;
213 #if __GLIBC__ >= 2 && !defined __UCLIBC__
216 dp
->flags
|= FLAG_LOCALIZED
;
224 /* Parse the field width. */
227 dp
->width_start
= cp
;
230 if (max_width_length
< 1)
231 max_width_length
= 1;
233 /* Test for positional argument. */
234 if (*cp
>= '0' && *cp
<= '9')
238 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
244 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
245 n
= xsum (xtimes (n
, 10), *np
- '0');
247 /* Positional argument 0. */
249 if (size_overflow_p (n
))
250 /* n too large, would lead to out of memory later. */
252 dp
->width_arg_index
= n
- 1;
256 if (dp
->width_arg_index
== ARG_NONE
)
258 dp
->width_arg_index
= arg_posn
++;
259 if (dp
->width_arg_index
== ARG_NONE
)
260 /* arg_posn wrapped around. */
263 REGISTER_ARG (dp
->width_arg_index
, TYPE_INT
);
265 else if (*cp
>= '0' && *cp
<= '9')
269 dp
->width_start
= cp
;
270 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
273 width_length
= dp
->width_end
- dp
->width_start
;
274 if (max_width_length
< width_length
)
275 max_width_length
= width_length
;
278 /* Parse the precision. */
284 dp
->precision_start
= cp
- 1;
286 dp
->precision_end
= cp
;
287 if (max_precision_length
< 2)
288 max_precision_length
= 2;
290 /* Test for positional argument. */
291 if (*cp
>= '0' && *cp
<= '9')
295 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
301 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
302 n
= xsum (xtimes (n
, 10), *np
- '0');
304 /* Positional argument 0. */
306 if (size_overflow_p (n
))
307 /* n too large, would lead to out of memory
310 dp
->precision_arg_index
= n
- 1;
314 if (dp
->precision_arg_index
== ARG_NONE
)
316 dp
->precision_arg_index
= arg_posn
++;
317 if (dp
->precision_arg_index
== ARG_NONE
)
318 /* arg_posn wrapped around. */
321 REGISTER_ARG (dp
->precision_arg_index
, TYPE_INT
);
325 size_t precision_length
;
327 dp
->precision_start
= cp
- 1;
328 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
330 dp
->precision_end
= cp
;
331 precision_length
= dp
->precision_end
- dp
->precision_start
;
332 if (max_precision_length
< precision_length
)
333 max_precision_length
= precision_length
;
340 /* Parse argument type/size specifiers. */
348 flags
|= (1 << (flags
& 1));
363 if (sizeof (intmax_t) > sizeof (long))
365 /* intmax_t = long long */
368 else if (sizeof (intmax_t) > sizeof (int))
370 /* intmax_t = long */
375 else if (*cp
== 'z' || *cp
== 'Z')
377 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
378 because the warning facility in gcc-2.95.2 understands
379 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
380 if (sizeof (size_t) > sizeof (long))
382 /* size_t = long long */
385 else if (sizeof (size_t) > sizeof (int))
394 if (sizeof (ptrdiff_t) > sizeof (long))
396 /* ptrdiff_t = long long */
399 else if (sizeof (ptrdiff_t) > sizeof (int))
401 /* ptrdiff_t = long */
406 #if defined __APPLE__ && defined __MACH__
407 /* On Mac OS X 10.3, PRIdMAX is defined as "qd".
408 We cannot change it to "lld" because PRIdMAX must also
409 be understood by the system's printf routines. */
412 if (64 / 8 > sizeof (long))
414 /* int64_t = long long */
425 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
426 /* On native Windows, PRIdMAX is defined as "I64d".
427 We cannot change it to "lld" because PRIdMAX must also
428 be understood by the system's printf routines. */
429 else if (*cp
== 'I' && cp
[1] == '6' && cp
[2] == '4')
431 if (64 / 8 > sizeof (long))
433 /* __int64 = long long */
448 /* Read the conversion character. */
454 /* If 'long long' exists and is larger than 'long': */
455 if (flags
>= 16 || (flags
& 4))
456 type
= TYPE_LONGLONGINT
;
459 /* If 'long long' exists and is the same as 'long', we parse
460 "lld" into TYPE_LONGINT. */
470 case 'o': case 'u': case 'x': case 'X':
472 /* If 'long long' exists and is larger than 'long': */
473 if (flags
>= 16 || (flags
& 4))
474 type
= TYPE_ULONGLONGINT
;
477 /* If 'unsigned long long' exists and is the same as
478 'unsigned long', we parse "llu" into TYPE_ULONGINT. */
480 type
= TYPE_ULONGINT
;
488 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
490 if (flags
>= 16 || (flags
& 4))
491 type
= TYPE_LONGDOUBLE
;
498 type
= TYPE_WIDE_CHAR
;
507 type
= TYPE_WIDE_CHAR
;
514 type
= TYPE_WIDE_STRING
;
523 type
= TYPE_WIDE_STRING
;
532 /* If 'long long' exists and is larger than 'long': */
533 if (flags
>= 16 || (flags
& 4))
534 type
= TYPE_COUNT_LONGLONGINT_POINTER
;
537 /* If 'long long' exists and is the same as 'long', we parse
538 "lln" into TYPE_COUNT_LONGINT_POINTER. */
540 type
= TYPE_COUNT_LONGINT_POINTER
;
542 type
= TYPE_COUNT_SCHAR_POINTER
;
544 type
= TYPE_COUNT_SHORT_POINTER
;
546 type
= TYPE_COUNT_INT_POINTER
;
549 /* The unistdio extensions. */
552 type
= TYPE_U32_STRING
;
554 type
= TYPE_U16_STRING
;
556 type
= TYPE_U8_STRING
;
563 /* Unknown conversion character. */
568 if (type
!= TYPE_NONE
)
570 dp
->arg_index
= arg_index
;
571 if (dp
->arg_index
== ARG_NONE
)
573 dp
->arg_index
= arg_posn
++;
574 if (dp
->arg_index
== ARG_NONE
)
575 /* arg_posn wrapped around. */
578 REGISTER_ARG (dp
->arg_index
, type
);
585 if (d
->count
>= d_allocated
)
590 d_allocated
= xtimes (d_allocated
, 2);
591 memory_size
= xtimes (d_allocated
, sizeof (DIRECTIVE
));
592 if (size_overflow_p (memory_size
))
593 /* Overflow, would lead to out of memory. */
595 memory
= (DIRECTIVE
*) (d
->dir
!= d
->direct_alloc_dir
596 ? realloc (d
->dir
, memory_size
)
597 : malloc (memory_size
));
601 if (d
->dir
== d
->direct_alloc_dir
)
602 memcpy (memory
, d
->dir
, d
->count
* sizeof (DIRECTIVE
));
606 #if CHAR_T_ONLY_ASCII
607 else if (!c_isascii (c
))
609 /* Non-ASCII character. Not supported. */
614 d
->dir
[d
->count
].dir_start
= cp
;
616 d
->max_width_length
= max_width_length
;
617 d
->max_precision_length
= max_precision_length
;
621 if (a
->arg
!= a
->direct_alloc_arg
)
623 if (d
->dir
!= d
->direct_alloc_dir
)
629 if (a
->arg
!= a
->direct_alloc_arg
)
631 if (d
->dir
!= d
->direct_alloc_dir
)
640 #undef CHAR_T_ONLY_ASCII