Return error code when an object is not found.
[gnutls.git] / gl / printf-parse.c
blobff24ba47e24e2245369bf49df04793605de4b3b3
1 /* Formatted output to strings.
2 Copyright (C) 1999-2000, 2002-2003, 2006-2011 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 General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
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 General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
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.
23 Depends on CHAR_T.
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.
27 Depends on CHAR_T.
28 STATIC Set to 'static' to declare the function static.
29 ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions. */
31 #ifndef PRINTF_PARSE
32 # include <config.h>
33 #endif
35 /* Specification. */
36 #ifndef PRINTF_PARSE
37 # include "printf-parse.h"
38 #endif
40 /* Default parameters. */
41 #ifndef PRINTF_PARSE
42 # define PRINTF_PARSE printf_parse
43 # define CHAR_T char
44 # define DIRECTIVE char_directive
45 # define DIRECTIVES char_directives
46 #endif
48 /* Get size_t, NULL. */
49 #include <stddef.h>
51 /* Get intmax_t. */
52 #if defined IN_LIBINTL || defined IN_LIBASPRINTF
53 # if HAVE_STDINT_H_WITH_UINTMAX
54 # include <stdint.h>
55 # endif
56 # if HAVE_INTTYPES_H_WITH_UINTMAX
57 # include <inttypes.h>
58 # endif
59 #else
60 # include <stdint.h>
61 #endif
63 /* malloc(), realloc(), free(). */
64 #include <stdlib.h>
66 /* memcpy(). */
67 #include <string.h>
69 /* errno. */
70 #include <errno.h>
72 /* Checked size_t computations. */
73 #include "xsize.h"
75 #if CHAR_T_ONLY_ASCII
76 /* c_isascii(). */
77 # include "c-ctype.h"
78 #endif
80 #ifdef STATIC
81 STATIC
82 #endif
83 int
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;
93 d->count = 0;
94 d_allocated = N_DIRECT_ALLOC_DIRECTIVES;
95 d->dir = d->direct_alloc_dir;
97 a->count = 0;
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; \
107 argument *memory; \
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)); \
124 a->arg = memory; \
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. */ \
132 goto error; \
135 while (*cp != '\0')
137 CHAR_T c = *cp++;
138 if (c == '%')
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;
145 dp->flags = 0;
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')
157 const CHAR_T *np;
159 for (np = cp; *np >= '0' && *np <= '9'; np++)
161 if (*np == '$')
163 size_t n = 0;
165 for (np = cp; *np >= '0' && *np <= '9'; np++)
166 n = xsum (xtimes (n, 10), *np - '0');
167 if (n == 0)
168 /* Positional argument 0. */
169 goto error;
170 if (size_overflow_p (n))
171 /* n too large, would lead to out of memory later. */
172 goto error;
173 arg_index = n - 1;
174 cp = np + 1;
178 /* Read the flags. */
179 for (;;)
181 if (*cp == '\'')
183 dp->flags |= FLAG_GROUP;
184 cp++;
186 else if (*cp == '-')
188 dp->flags |= FLAG_LEFT;
189 cp++;
191 else if (*cp == '+')
193 dp->flags |= FLAG_SHOWSIGN;
194 cp++;
196 else if (*cp == ' ')
198 dp->flags |= FLAG_SPACE;
199 cp++;
201 else if (*cp == '#')
203 dp->flags |= FLAG_ALT;
204 cp++;
206 else if (*cp == '0')
208 dp->flags |= FLAG_ZERO;
209 cp++;
211 #if __GLIBC__ >= 2 && !defined __UCLIBC__
212 else if (*cp == 'I')
214 dp->flags |= FLAG_LOCALIZED;
215 cp++;
217 #endif
218 else
219 break;
222 /* Parse the field width. */
223 if (*cp == '*')
225 dp->width_start = cp;
226 cp++;
227 dp->width_end = cp;
228 if (max_width_length < 1)
229 max_width_length = 1;
231 /* Test for positional argument. */
232 if (*cp >= '0' && *cp <= '9')
234 const CHAR_T *np;
236 for (np = cp; *np >= '0' && *np <= '9'; np++)
238 if (*np == '$')
240 size_t n = 0;
242 for (np = cp; *np >= '0' && *np <= '9'; np++)
243 n = xsum (xtimes (n, 10), *np - '0');
244 if (n == 0)
245 /* Positional argument 0. */
246 goto error;
247 if (size_overflow_p (n))
248 /* n too large, would lead to out of memory later. */
249 goto error;
250 dp->width_arg_index = n - 1;
251 cp = np + 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. */
259 goto error;
261 REGISTER_ARG (dp->width_arg_index, TYPE_INT);
263 else if (*cp >= '0' && *cp <= '9')
265 size_t width_length;
267 dp->width_start = cp;
268 for (; *cp >= '0' && *cp <= '9'; cp++)
270 dp->width_end = 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. */
277 if (*cp == '.')
279 cp++;
280 if (*cp == '*')
282 dp->precision_start = cp - 1;
283 cp++;
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')
291 const CHAR_T *np;
293 for (np = cp; *np >= '0' && *np <= '9'; np++)
295 if (*np == '$')
297 size_t n = 0;
299 for (np = cp; *np >= '0' && *np <= '9'; np++)
300 n = xsum (xtimes (n, 10), *np - '0');
301 if (n == 0)
302 /* Positional argument 0. */
303 goto error;
304 if (size_overflow_p (n))
305 /* n too large, would lead to out of memory
306 later. */
307 goto error;
308 dp->precision_arg_index = n - 1;
309 cp = np + 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. */
317 goto error;
319 REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
321 else
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;
336 arg_type type;
338 /* Parse argument type/size specifiers. */
340 int flags = 0;
342 for (;;)
344 if (*cp == 'h')
346 flags |= (1 << (flags & 1));
347 cp++;
349 else if (*cp == 'L')
351 flags |= 4;
352 cp++;
354 else if (*cp == 'l')
356 flags += 8;
357 cp++;
359 else if (*cp == 'j')
361 if (sizeof (intmax_t) > sizeof (long))
363 /* intmax_t = long long */
364 flags += 16;
366 else if (sizeof (intmax_t) > sizeof (int))
368 /* intmax_t = long */
369 flags += 8;
371 cp++;
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 */
381 flags += 16;
383 else if (sizeof (size_t) > sizeof (int))
385 /* size_t = long */
386 flags += 8;
388 cp++;
390 else if (*cp == 't')
392 if (sizeof (ptrdiff_t) > sizeof (long))
394 /* ptrdiff_t = long long */
395 flags += 16;
397 else if (sizeof (ptrdiff_t) > sizeof (int))
399 /* ptrdiff_t = long */
400 flags += 8;
402 cp++;
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. */
408 else if (*cp == 'q')
410 if (64 / 8 > sizeof (long))
412 /* int64_t = long long */
413 flags += 16;
415 else
417 /* int64_t = long */
418 flags += 8;
420 cp++;
422 #endif
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 */
432 flags += 16;
434 else
436 /* __int64 = long */
437 flags += 8;
439 cp += 3;
441 #endif
442 else
443 break;
446 /* Read the conversion character. */
447 c = *cp++;
448 switch (c)
450 case 'd': case 'i':
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;
455 else
456 #endif
457 /* If 'long long' exists and is the same as 'long', we parse
458 "lld" into TYPE_LONGINT. */
459 if (flags >= 8)
460 type = TYPE_LONGINT;
461 else if (flags & 2)
462 type = TYPE_SCHAR;
463 else if (flags & 1)
464 type = TYPE_SHORT;
465 else
466 type = TYPE_INT;
467 break;
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;
473 else
474 #endif
475 /* If 'unsigned long long' exists and is the same as
476 'unsigned long', we parse "llu" into TYPE_ULONGINT. */
477 if (flags >= 8)
478 type = TYPE_ULONGINT;
479 else if (flags & 2)
480 type = TYPE_UCHAR;
481 else if (flags & 1)
482 type = TYPE_USHORT;
483 else
484 type = TYPE_UINT;
485 break;
486 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
487 case 'a': case 'A':
488 if (flags >= 16 || (flags & 4))
489 type = TYPE_LONGDOUBLE;
490 else
491 type = TYPE_DOUBLE;
492 break;
493 case 'c':
494 if (flags >= 8)
495 #if HAVE_WINT_T
496 type = TYPE_WIDE_CHAR;
497 #else
498 goto error;
499 #endif
500 else
501 type = TYPE_CHAR;
502 break;
503 #if HAVE_WINT_T
504 case 'C':
505 type = TYPE_WIDE_CHAR;
506 c = 'c';
507 break;
508 #endif
509 case 's':
510 if (flags >= 8)
511 #if HAVE_WCHAR_T
512 type = TYPE_WIDE_STRING;
513 #else
514 goto error;
515 #endif
516 else
517 type = TYPE_STRING;
518 break;
519 #if HAVE_WCHAR_T
520 case 'S':
521 type = TYPE_WIDE_STRING;
522 c = 's';
523 break;
524 #endif
525 case 'p':
526 type = TYPE_POINTER;
527 break;
528 case 'n':
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;
533 else
534 #endif
535 /* If 'long long' exists and is the same as 'long', we parse
536 "lln" into TYPE_COUNT_LONGINT_POINTER. */
537 if (flags >= 8)
538 type = TYPE_COUNT_LONGINT_POINTER;
539 else if (flags & 2)
540 type = TYPE_COUNT_SCHAR_POINTER;
541 else if (flags & 1)
542 type = TYPE_COUNT_SHORT_POINTER;
543 else
544 type = TYPE_COUNT_INT_POINTER;
545 break;
546 #if ENABLE_UNISTDIO
547 /* The unistdio extensions. */
548 case 'U':
549 if (flags >= 16)
550 type = TYPE_U32_STRING;
551 else if (flags >= 8)
552 type = TYPE_U16_STRING;
553 else
554 type = TYPE_U8_STRING;
555 break;
556 #endif
557 case '%':
558 type = TYPE_NONE;
559 break;
560 default:
561 /* Unknown conversion character. */
562 goto error;
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. */
574 goto error;
576 REGISTER_ARG (dp->arg_index, type);
578 dp->conversion = c;
579 dp->dir_end = cp;
582 d->count++;
583 if (d->count >= d_allocated)
585 size_t memory_size;
586 DIRECTIVE *memory;
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. */
592 goto out_of_memory;
593 memory = (DIRECTIVE *) (d->dir != d->direct_alloc_dir
594 ? realloc (d->dir, memory_size)
595 : malloc (memory_size));
596 if (memory == NULL)
597 /* Out of memory. */
598 goto out_of_memory;
599 if (d->dir == d->direct_alloc_dir)
600 memcpy (memory, d->dir, d->count * sizeof (DIRECTIVE));
601 d->dir = memory;
604 #if CHAR_T_ONLY_ASCII
605 else if (!c_isascii (c))
607 /* Non-ASCII character. Not supported. */
608 goto error;
610 #endif
612 d->dir[d->count].dir_start = cp;
614 d->max_width_length = max_width_length;
615 d->max_precision_length = max_precision_length;
616 return 0;
618 error:
619 if (a->arg != a->direct_alloc_arg)
620 free (a->arg);
621 if (d->dir != d->direct_alloc_dir)
622 free (d->dir);
623 errno = EINVAL;
624 return -1;
626 out_of_memory:
627 if (a->arg != a->direct_alloc_arg)
628 free (a->arg);
629 if (d->dir != d->direct_alloc_dir)
630 free (d->dir);
631 errno = ENOMEM;
632 return -1;
635 #undef PRINTF_PARSE
636 #undef DIRECTIVES
637 #undef DIRECTIVE
638 #undef CHAR_T_ONLY_ASCII
639 #undef CHAR_T