xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Internal header for parsing printf format strings. |
| 2 | Copyright (C) 1995-2016 Free Software Foundation, Inc. |
| 3 | This file is part of th GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library 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 GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <printf.h> |
| 20 | #include <stdint.h> |
| 21 | #include <stddef.h> |
| 22 | #include <string.h> |
| 23 | #include <wchar.h> |
| 24 | |
| 25 | |
| 26 | struct printf_spec |
| 27 | { |
| 28 | /* Information parsed from the format spec. */ |
| 29 | struct printf_info info; |
| 30 | |
| 31 | /* Pointers into the format string for the end of this format |
| 32 | spec and the next (or to the end of the string if no more). */ |
| 33 | const UCHAR_T *end_of_fmt, *next_fmt; |
| 34 | |
| 35 | /* Position of arguments for precision and width, or -1 if `info' has |
| 36 | the constant value. */ |
| 37 | int prec_arg, width_arg; |
| 38 | |
| 39 | int data_arg; /* Position of data argument. */ |
| 40 | int data_arg_type; /* Type of first argument. */ |
| 41 | /* Number of arguments consumed by this format specifier. */ |
| 42 | size_t ndata_args; |
| 43 | /* Size of the parameter for PA_USER type. */ |
| 44 | int size; |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | /* The various kinds off arguments that can be passed to printf. */ |
| 49 | union printf_arg |
| 50 | { |
| 51 | wchar_t pa_wchar; |
| 52 | int pa_int; |
| 53 | long int pa_long_int; |
| 54 | long long int pa_long_long_int; |
| 55 | unsigned int pa_u_int; |
| 56 | unsigned long int pa_u_long_int; |
| 57 | unsigned long long int pa_u_long_long_int; |
| 58 | double pa_double; |
| 59 | long double pa_long_double; |
| 60 | const char *pa_string; |
| 61 | const wchar_t *pa_wstring; |
| 62 | void *pa_pointer; |
| 63 | void *pa_user; |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | #ifndef DONT_NEED_READ_INT |
| 68 | /* Read a simple integer from a string and update the string pointer. |
| 69 | It is assumed that the first character is a digit. */ |
| 70 | static int |
| 71 | read_int (const UCHAR_T * *pstr) |
| 72 | { |
| 73 | int retval = **pstr - L_('0'); |
| 74 | |
| 75 | while (ISDIGIT (*++(*pstr))) |
| 76 | if (retval >= 0) |
| 77 | { |
| 78 | if (INT_MAX / 10 < retval) |
| 79 | retval = -1; |
| 80 | else |
| 81 | { |
| 82 | int digit = **pstr - L_('0'); |
| 83 | |
| 84 | retval *= 10; |
| 85 | if (INT_MAX - digit < retval) |
| 86 | retval = -1; |
| 87 | else |
| 88 | retval += digit; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return retval; |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | |
| 97 | /* These are defined in reg-printf.c. */ |
| 98 | extern printf_arginfo_size_function **__printf_arginfo_table attribute_hidden; |
| 99 | extern printf_function **__printf_function_table attribute_hidden; |
| 100 | extern printf_va_arg_function **__printf_va_arg_table attribute_hidden; |
| 101 | |
| 102 | |
| 103 | /* Find the next spec in FORMAT, or the end of the string. Returns |
| 104 | a pointer into FORMAT, to a '%' or a '\0'. */ |
| 105 | __extern_always_inline const unsigned char * |
| 106 | __find_specmb (const unsigned char *format) |
| 107 | { |
| 108 | return (const unsigned char *) __strchrnul ((const char *) format, '%'); |
| 109 | } |
| 110 | |
| 111 | __extern_always_inline const unsigned int * |
| 112 | __find_specwc (const unsigned int *format) |
| 113 | { |
| 114 | return (const unsigned int *) __wcschrnul ((const wchar_t *) format, L'%'); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC |
| 119 | with the parsed details. POSN is the number of arguments already |
| 120 | consumed. At most MAXTYPES - POSN types are filled in TYPES. Return |
| 121 | the number of args consumed by this spec; *MAX_REF_ARG is updated so it |
| 122 | remains the highest argument index used. */ |
| 123 | extern size_t __parse_one_specmb (const unsigned char *format, size_t posn, |
| 124 | struct printf_spec *spec, |
| 125 | size_t *max_ref_arg) attribute_hidden; |
| 126 | |
| 127 | extern size_t __parse_one_specwc (const unsigned int *format, size_t posn, |
| 128 | struct printf_spec *spec, |
| 129 | size_t *max_ref_arg) attribute_hidden; |
| 130 | |
| 131 | |
| 132 | |
| 133 | /* This variable is defined in reg-modifier.c. */ |
| 134 | struct printf_modifier_record; |
| 135 | extern struct printf_modifier_record **__printf_modifier_table |
| 136 | attribute_hidden; |
| 137 | |
| 138 | /* Handle registered modifiers. */ |
| 139 | extern int __handle_registered_modifier_mb (const unsigned char **format, |
| 140 | struct printf_info *info) |
| 141 | attribute_hidden; |
| 142 | extern int __handle_registered_modifier_wc (const unsigned int **format, |
| 143 | struct printf_info *info) |
| 144 | attribute_hidden; |