1 | /* Copyright (C) 1991-2023 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the License, or (at your option) any later version. |
8 | |
9 | The GNU C Library 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 GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <https://www.gnu.org/licenses/>. */ |
17 | |
18 | #include <array_length.h> |
19 | #include <assert.h> |
20 | #include <ctype.h> |
21 | #include <limits.h> |
22 | #include <printf.h> |
23 | #include <stdarg.h> |
24 | #include <stdint.h> |
25 | #include <stdlib.h> |
26 | #include <string.h> |
27 | #include <errno.h> |
28 | #include <wchar.h> |
29 | #include <libc-lock.h> |
30 | #include <sys/param.h> |
31 | #include <_itoa.h> |
32 | #include <locale/localeinfo.h> |
33 | #include <grouping_iterator.h> |
34 | #include <stdio.h> |
35 | #include <scratch_buffer.h> |
36 | #include <intprops.h> |
37 | #include <printf_buffer.h> |
38 | #include <printf_buffer_to_file.h> |
39 | |
40 | /* This code is shared between the standard stdio implementation found |
41 | in GNU C library and the libio implementation originally found in |
42 | GNU libg++. |
43 | |
44 | Beside this it is also shared between the normal and wide character |
45 | implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */ |
46 | |
47 | #include <libioP.h> |
48 | |
49 | #ifdef COMPILE_WPRINTF |
50 | #include <wctype.h> |
51 | #endif |
52 | |
53 | #define ARGCHECK(S, Format) \ |
54 | do \ |
55 | { \ |
56 | /* Check file argument for consistence. */ \ |
57 | CHECK_FILE (S, -1); \ |
58 | if (S->_flags & _IO_NO_WRITES) \ |
59 | { \ |
60 | S->_flags |= _IO_ERR_SEEN; \ |
61 | __set_errno (EBADF); \ |
62 | return -1; \ |
63 | } \ |
64 | if (Format == NULL) \ |
65 | { \ |
66 | __set_errno (EINVAL); \ |
67 | return -1; \ |
68 | } \ |
69 | } while (0) |
70 | #define UNBUFFERED_P(S) ((S)->_flags & _IO_UNBUFFERED) |
71 | |
72 | #if __HAVE_FLOAT128_UNLIKE_LDBL |
73 | # define PARSE_FLOAT_VA_ARG_EXTENDED(INFO) \ |
74 | do \ |
75 | { \ |
76 | if (is_long_double \ |
77 | && (mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) \ |
78 | { \ |
79 | INFO.is_binary128 = 1; \ |
80 | the_arg.pa_float128 = va_arg (ap, _Float128); \ |
81 | } \ |
82 | else \ |
83 | { \ |
84 | PARSE_FLOAT_VA_ARG (INFO); \ |
85 | } \ |
86 | } \ |
87 | while (0) |
88 | #else |
89 | # define PARSE_FLOAT_VA_ARG_EXTENDED(INFO) \ |
90 | PARSE_FLOAT_VA_ARG (INFO); |
91 | #endif |
92 | |
93 | #define PARSE_FLOAT_VA_ARG(INFO) \ |
94 | do \ |
95 | { \ |
96 | INFO.is_binary128 = 0; \ |
97 | if (is_long_double) \ |
98 | the_arg.pa_long_double = va_arg (ap, long double); \ |
99 | else \ |
100 | the_arg.pa_double = va_arg (ap, double); \ |
101 | } \ |
102 | while (0) |
103 | |
104 | #if __HAVE_FLOAT128_UNLIKE_LDBL |
105 | # define SETUP_FLOAT128_INFO(INFO) \ |
106 | do \ |
107 | { \ |
108 | if ((mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) \ |
109 | INFO.is_binary128 = is_long_double; \ |
110 | else \ |
111 | INFO.is_binary128 = 0; \ |
112 | } \ |
113 | while (0) |
114 | #else |
115 | # define SETUP_FLOAT128_INFO(INFO) \ |
116 | do \ |
117 | { \ |
118 | INFO.is_binary128 = 0; \ |
119 | } \ |
120 | while (0) |
121 | #endif |
122 | |
123 | #ifndef COMPILE_WPRINTF |
124 | # include "printf_buffer-char.h" |
125 | # define vfprintf __vfprintf_internal |
126 | # define OTHER_CHAR_T wchar_t |
127 | # define UCHAR_T unsigned char |
128 | # define INT_T int |
129 | typedef const char *THOUSANDS_SEP_T; |
130 | # define L_(Str) Str |
131 | # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10) |
132 | # define STR_LEN(Str) strlen (Str) |
133 | |
134 | # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\ |
135 | return -1 |
136 | # define CONVERT_FROM_OTHER_STRING __wcsrtombs |
137 | #else |
138 | # include "printf_buffer-wchar_t.h" |
139 | # define vfprintf __vfwprintf_internal |
140 | # define OTHER_CHAR_T char |
141 | /* This is a hack!!! There should be a type uwchar_t. */ |
142 | # define UCHAR_T unsigned int /* uwchar_t */ |
143 | # define INT_T wint_t |
144 | typedef wchar_t THOUSANDS_SEP_T; |
145 | # define L_(Str) L##Str |
146 | # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10) |
147 | # define STR_LEN(Str) __wcslen (Str) |
148 | |
149 | # include <_itowa.h> |
150 | |
151 | # define ORIENT if (_IO_fwide (s, 1) != 1) return -1 |
152 | # define CONVERT_FROM_OTHER_STRING __mbsrtowcs |
153 | |
154 | # undef _itoa |
155 | # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case) |
156 | # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case) |
157 | # undef EOF |
158 | # define EOF WEOF |
159 | #endif |
160 | |
161 | /* Include the shared code for parsing the format string. */ |
162 | #include "printf-parse.h" |
163 | |
164 | |
165 | /* Write the string SRC to S. If PREC is non-negative, write at most |
166 | PREC bytes. If LEFT is true, perform left justification. */ |
167 | static void |
168 | outstring_converted_wide_string (struct Xprintf_buffer *target, |
169 | const OTHER_CHAR_T *src, int prec, |
170 | int width, bool left) |
171 | { |
172 | /* Use a small buffer to combine processing of multiple characters. |
173 | CONVERT_FROM_OTHER_STRING expects the buffer size in (wide) |
174 | characters, and buf_length counts that. */ |
175 | enum { buf_length = 256 / sizeof (CHAR_T) }; |
176 | CHAR_T buf[buf_length]; |
177 | _Static_assert (sizeof (buf) > MB_LEN_MAX, |
178 | "buffer is large enough for a single multi-byte character" ); |
179 | |
180 | /* Add the initial padding if needed. */ |
181 | if (width > 0 && !left) |
182 | { |
183 | /* Make a first pass to find the output width, so that we can |
184 | add the required padding. */ |
185 | mbstate_t mbstate = { 0 }; |
186 | const OTHER_CHAR_T *src_copy = src; |
187 | size_t total_written; |
188 | if (prec < 0) |
189 | total_written = CONVERT_FROM_OTHER_STRING |
190 | (NULL, &src_copy, 0, &mbstate); |
191 | else |
192 | { |
193 | /* The source might not be null-terminated. Enforce the |
194 | limit manually, based on the output length. */ |
195 | total_written = 0; |
196 | size_t limit = prec; |
197 | while (limit > 0 && src_copy != NULL) |
198 | { |
199 | size_t write_limit = buf_length; |
200 | if (write_limit > limit) |
201 | write_limit = limit; |
202 | size_t written = CONVERT_FROM_OTHER_STRING |
203 | (buf, &src_copy, write_limit, &mbstate); |
204 | if (written == (size_t) -1) |
205 | { |
206 | Xprintf_buffer_mark_failed (target); |
207 | return; |
208 | } |
209 | if (written == 0) |
210 | break; |
211 | total_written += written; |
212 | limit -= written; |
213 | } |
214 | } |
215 | |
216 | /* Output initial padding. */ |
217 | Xprintf_buffer_pad (target, L_(' '), width - total_written); |
218 | if (Xprintf_buffer_has_failed (target)) |
219 | return; |
220 | } |
221 | |
222 | /* Convert the input string, piece by piece. */ |
223 | size_t total_written = 0; |
224 | { |
225 | mbstate_t mbstate = { 0 }; |
226 | /* If prec is negative, remaining is not decremented, otherwise, |
227 | it serves as the write limit. */ |
228 | size_t remaining = -1; |
229 | if (prec >= 0) |
230 | remaining = prec; |
231 | while (remaining > 0 && src != NULL) |
232 | { |
233 | size_t write_limit = buf_length; |
234 | if (remaining < write_limit) |
235 | write_limit = remaining; |
236 | size_t written = CONVERT_FROM_OTHER_STRING |
237 | (buf, &src, write_limit, &mbstate); |
238 | if (written == (size_t) -1) |
239 | { |
240 | Xprintf_buffer_mark_failed (target); |
241 | return; |
242 | } |
243 | if (written == 0) |
244 | break; |
245 | Xprintf_buffer_write (target, buf, written); |
246 | total_written += written; |
247 | if (prec >= 0) |
248 | remaining -= written; |
249 | } |
250 | } |
251 | |
252 | /* Add final padding. */ |
253 | if (width > 0 && left) |
254 | Xprintf_buffer_pad (target, L_(' '), width - total_written); |
255 | } |
256 | |
257 | /* Calls __printf_fp or __printf_fphex based on the value of the |
258 | format specifier INFO->spec. */ |
259 | static inline void |
260 | __printf_fp_spec (struct Xprintf_buffer *target, |
261 | const struct printf_info *info, const void *const *args) |
262 | { |
263 | if (info->spec == 'a' || info->spec == 'A') |
264 | Xprintf (fphex_l_buffer) (target, _NL_CURRENT_LOCALE, info, args); |
265 | else |
266 | Xprintf (fp_l_buffer) (target, _NL_CURRENT_LOCALE, info, args); |
267 | } |
268 | |
269 | /* For handling long_double and longlong we use the same flag. If |
270 | `long' and `long long' are effectively the same type define it to |
271 | zero. */ |
272 | #if LONG_MAX == LONG_LONG_MAX |
273 | # define is_longlong 0 |
274 | #else |
275 | # define is_longlong is_long_double |
276 | #endif |
277 | |
278 | /* If `long' and `int' is effectively the same type we don't have to |
279 | handle `long separately. */ |
280 | #if INT_MAX == LONG_MAX |
281 | # define is_long_num 0 |
282 | #else |
283 | # define is_long_num is_long |
284 | #endif |
285 | |
286 | |
287 | /* Global constants. */ |
288 | static const CHAR_T null[] = L_("(null)" ); |
289 | |
290 | /* Size of the work_buffer variable (in characters, not bytes. */ |
291 | enum { WORK_BUFFER_SIZE = 1000 / sizeof (CHAR_T) }; |
292 | |
293 | /* This table maps a character into a number representing a class. In |
294 | each step there is a destination label for each class. */ |
295 | static const uint8_t jump_table[] = |
296 | { |
297 | /* ' ' */ 1, 0, 0, /* '#' */ 4, |
298 | 0, /* '%' */ 14, 0, /* '\''*/ 6, |
299 | 0, 0, /* '*' */ 7, /* '+' */ 2, |
300 | 0, /* '-' */ 3, /* '.' */ 9, 0, |
301 | /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8, |
302 | /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8, |
303 | /* '8' */ 8, /* '9' */ 8, 0, 0, |
304 | 0, 0, 0, 0, |
305 | 0, /* 'A' */ 26, /* 'B' */ 30, /* 'C' */ 25, |
306 | 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19, |
307 | 0, /* 'I' */ 29, 0, 0, |
308 | /* 'L' */ 12, 0, 0, 0, |
309 | 0, 0, 0, /* 'S' */ 21, |
310 | 0, 0, 0, 0, |
311 | /* 'X' */ 18, 0, /* 'Z' */ 13, 0, |
312 | 0, 0, 0, 0, |
313 | 0, /* 'a' */ 26, /* 'b' */ 30, /* 'c' */ 20, |
314 | /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19, |
315 | /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0, |
316 | /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17, |
317 | /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21, |
318 | /* 't' */ 27, /* 'u' */ 16, 0, /* 'w' */ 31, |
319 | /* 'x' */ 18, 0, /* 'z' */ 13 |
320 | }; |
321 | |
322 | #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z')) |
323 | #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')]) |
324 | #define LABEL(Name) do_##Name |
325 | #ifdef SHARED |
326 | /* 'int' is enough and it saves some space on 64 bit systems. */ |
327 | # define JUMP_TABLE_TYPE const int |
328 | # define JUMP_TABLE_BASE_LABEL do_form_unknown |
329 | # define REF(Name) &&do_##Name - &&JUMP_TABLE_BASE_LABEL |
330 | # define JUMP(ChExpr, table) \ |
331 | do \ |
332 | { \ |
333 | int offset; \ |
334 | void *ptr; \ |
335 | spec = (ChExpr); \ |
336 | offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \ |
337 | : table[CHAR_CLASS (spec)]; \ |
338 | ptr = &&JUMP_TABLE_BASE_LABEL + offset; \ |
339 | goto *ptr; \ |
340 | } \ |
341 | while (0) |
342 | #else |
343 | # define JUMP_TABLE_TYPE const void *const |
344 | # define REF(Name) &&do_##Name |
345 | # define JUMP(ChExpr, table) \ |
346 | do \ |
347 | { \ |
348 | const void *ptr; \ |
349 | spec = (ChExpr); \ |
350 | ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \ |
351 | : table[CHAR_CLASS (spec)]; \ |
352 | goto *ptr; \ |
353 | } \ |
354 | while (0) |
355 | #endif |
356 | |
357 | #define STEP0_3_TABLE \ |
358 | /* Step 0: at the beginning. */ \ |
359 | static JUMP_TABLE_TYPE step0_jumps[32] = \ |
360 | { \ |
361 | REF (form_unknown), \ |
362 | REF (flag_space), /* for ' ' */ \ |
363 | REF (flag_plus), /* for '+' */ \ |
364 | REF (flag_minus), /* for '-' */ \ |
365 | REF (flag_hash), /* for '<hash>' */ \ |
366 | REF (flag_zero), /* for '0' */ \ |
367 | REF (flag_quote), /* for '\'' */ \ |
368 | REF (width_asterics), /* for '*' */ \ |
369 | REF (width), /* for '1'...'9' */ \ |
370 | REF (precision), /* for '.' */ \ |
371 | REF (mod_half), /* for 'h' */ \ |
372 | REF (mod_long), /* for 'l' */ \ |
373 | REF (mod_longlong), /* for 'L', 'q' */ \ |
374 | REF (mod_size_t), /* for 'z', 'Z' */ \ |
375 | REF (form_percent), /* for '%' */ \ |
376 | REF (form_integer), /* for 'd', 'i' */ \ |
377 | REF (form_unsigned), /* for 'u' */ \ |
378 | REF (form_octal), /* for 'o' */ \ |
379 | REF (form_hexa), /* for 'X', 'x' */ \ |
380 | REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \ |
381 | REF (form_character), /* for 'c' */ \ |
382 | REF (form_string), /* for 's', 'S' */ \ |
383 | REF (form_pointer), /* for 'p' */ \ |
384 | REF (form_number), /* for 'n' */ \ |
385 | REF (form_strerror), /* for 'm' */ \ |
386 | REF (form_wcharacter), /* for 'C' */ \ |
387 | REF (form_floathex), /* for 'A', 'a' */ \ |
388 | REF (mod_ptrdiff_t), /* for 't' */ \ |
389 | REF (mod_intmax_t), /* for 'j' */ \ |
390 | REF (flag_i18n), /* for 'I' */ \ |
391 | REF (form_binary), /* for 'B', 'b' */ \ |
392 | REF (mod_bitwidth), /* for 'w' */ \ |
393 | }; \ |
394 | /* Step 1: after processing width. */ \ |
395 | static JUMP_TABLE_TYPE step1_jumps[32] = \ |
396 | { \ |
397 | REF (form_unknown), \ |
398 | REF (form_unknown), /* for ' ' */ \ |
399 | REF (form_unknown), /* for '+' */ \ |
400 | REF (form_unknown), /* for '-' */ \ |
401 | REF (form_unknown), /* for '<hash>' */ \ |
402 | REF (form_unknown), /* for '0' */ \ |
403 | REF (form_unknown), /* for '\'' */ \ |
404 | REF (form_unknown), /* for '*' */ \ |
405 | REF (form_unknown), /* for '1'...'9' */ \ |
406 | REF (precision), /* for '.' */ \ |
407 | REF (mod_half), /* for 'h' */ \ |
408 | REF (mod_long), /* for 'l' */ \ |
409 | REF (mod_longlong), /* for 'L', 'q' */ \ |
410 | REF (mod_size_t), /* for 'z', 'Z' */ \ |
411 | REF (form_percent), /* for '%' */ \ |
412 | REF (form_integer), /* for 'd', 'i' */ \ |
413 | REF (form_unsigned), /* for 'u' */ \ |
414 | REF (form_octal), /* for 'o' */ \ |
415 | REF (form_hexa), /* for 'X', 'x' */ \ |
416 | REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \ |
417 | REF (form_character), /* for 'c' */ \ |
418 | REF (form_string), /* for 's', 'S' */ \ |
419 | REF (form_pointer), /* for 'p' */ \ |
420 | REF (form_number), /* for 'n' */ \ |
421 | REF (form_strerror), /* for 'm' */ \ |
422 | REF (form_wcharacter), /* for 'C' */ \ |
423 | REF (form_floathex), /* for 'A', 'a' */ \ |
424 | REF (mod_ptrdiff_t), /* for 't' */ \ |
425 | REF (mod_intmax_t), /* for 'j' */ \ |
426 | REF (form_unknown), /* for 'I' */ \ |
427 | REF (form_binary), /* for 'B', 'b' */ \ |
428 | REF (mod_bitwidth), /* for 'w' */ \ |
429 | }; \ |
430 | /* Step 2: after processing precision. */ \ |
431 | static JUMP_TABLE_TYPE step2_jumps[32] = \ |
432 | { \ |
433 | REF (form_unknown), \ |
434 | REF (form_unknown), /* for ' ' */ \ |
435 | REF (form_unknown), /* for '+' */ \ |
436 | REF (form_unknown), /* for '-' */ \ |
437 | REF (form_unknown), /* for '<hash>' */ \ |
438 | REF (form_unknown), /* for '0' */ \ |
439 | REF (form_unknown), /* for '\'' */ \ |
440 | REF (form_unknown), /* for '*' */ \ |
441 | REF (form_unknown), /* for '1'...'9' */ \ |
442 | REF (form_unknown), /* for '.' */ \ |
443 | REF (mod_half), /* for 'h' */ \ |
444 | REF (mod_long), /* for 'l' */ \ |
445 | REF (mod_longlong), /* for 'L', 'q' */ \ |
446 | REF (mod_size_t), /* for 'z', 'Z' */ \ |
447 | REF (form_percent), /* for '%' */ \ |
448 | REF (form_integer), /* for 'd', 'i' */ \ |
449 | REF (form_unsigned), /* for 'u' */ \ |
450 | REF (form_octal), /* for 'o' */ \ |
451 | REF (form_hexa), /* for 'X', 'x' */ \ |
452 | REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \ |
453 | REF (form_character), /* for 'c' */ \ |
454 | REF (form_string), /* for 's', 'S' */ \ |
455 | REF (form_pointer), /* for 'p' */ \ |
456 | REF (form_number), /* for 'n' */ \ |
457 | REF (form_strerror), /* for 'm' */ \ |
458 | REF (form_wcharacter), /* for 'C' */ \ |
459 | REF (form_floathex), /* for 'A', 'a' */ \ |
460 | REF (mod_ptrdiff_t), /* for 't' */ \ |
461 | REF (mod_intmax_t), /* for 'j' */ \ |
462 | REF (form_unknown), /* for 'I' */ \ |
463 | REF (form_binary), /* for 'B', 'b' */ \ |
464 | REF (mod_bitwidth), /* for 'w' */ \ |
465 | }; \ |
466 | /* Step 3a: after processing first 'h' modifier. */ \ |
467 | static JUMP_TABLE_TYPE step3a_jumps[32] = \ |
468 | { \ |
469 | REF (form_unknown), \ |
470 | REF (form_unknown), /* for ' ' */ \ |
471 | REF (form_unknown), /* for '+' */ \ |
472 | REF (form_unknown), /* for '-' */ \ |
473 | REF (form_unknown), /* for '<hash>' */ \ |
474 | REF (form_unknown), /* for '0' */ \ |
475 | REF (form_unknown), /* for '\'' */ \ |
476 | REF (form_unknown), /* for '*' */ \ |
477 | REF (form_unknown), /* for '1'...'9' */ \ |
478 | REF (form_unknown), /* for '.' */ \ |
479 | REF (mod_halfhalf), /* for 'h' */ \ |
480 | REF (form_unknown), /* for 'l' */ \ |
481 | REF (form_unknown), /* for 'L', 'q' */ \ |
482 | REF (form_unknown), /* for 'z', 'Z' */ \ |
483 | REF (form_percent), /* for '%' */ \ |
484 | REF (form_integer), /* for 'd', 'i' */ \ |
485 | REF (form_unsigned), /* for 'u' */ \ |
486 | REF (form_octal), /* for 'o' */ \ |
487 | REF (form_hexa), /* for 'X', 'x' */ \ |
488 | REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \ |
489 | REF (form_unknown), /* for 'c' */ \ |
490 | REF (form_unknown), /* for 's', 'S' */ \ |
491 | REF (form_unknown), /* for 'p' */ \ |
492 | REF (form_number), /* for 'n' */ \ |
493 | REF (form_unknown), /* for 'm' */ \ |
494 | REF (form_unknown), /* for 'C' */ \ |
495 | REF (form_unknown), /* for 'A', 'a' */ \ |
496 | REF (form_unknown), /* for 't' */ \ |
497 | REF (form_unknown), /* for 'j' */ \ |
498 | REF (form_unknown), /* for 'I' */ \ |
499 | REF (form_binary), /* for 'B', 'b' */ \ |
500 | REF (form_unknown), /* for 'w' */ \ |
501 | }; \ |
502 | /* Step 3b: after processing first 'l' modifier. */ \ |
503 | static JUMP_TABLE_TYPE step3b_jumps[32] = \ |
504 | { \ |
505 | REF (form_unknown), \ |
506 | REF (form_unknown), /* for ' ' */ \ |
507 | REF (form_unknown), /* for '+' */ \ |
508 | REF (form_unknown), /* for '-' */ \ |
509 | REF (form_unknown), /* for '<hash>' */ \ |
510 | REF (form_unknown), /* for '0' */ \ |
511 | REF (form_unknown), /* for '\'' */ \ |
512 | REF (form_unknown), /* for '*' */ \ |
513 | REF (form_unknown), /* for '1'...'9' */ \ |
514 | REF (form_unknown), /* for '.' */ \ |
515 | REF (form_unknown), /* for 'h' */ \ |
516 | REF (mod_longlong), /* for 'l' */ \ |
517 | REF (form_unknown), /* for 'L', 'q' */ \ |
518 | REF (form_unknown), /* for 'z', 'Z' */ \ |
519 | REF (form_percent), /* for '%' */ \ |
520 | REF (form_integer), /* for 'd', 'i' */ \ |
521 | REF (form_unsigned), /* for 'u' */ \ |
522 | REF (form_octal), /* for 'o' */ \ |
523 | REF (form_hexa), /* for 'X', 'x' */ \ |
524 | REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \ |
525 | REF (form_character), /* for 'c' */ \ |
526 | REF (form_string), /* for 's', 'S' */ \ |
527 | REF (form_pointer), /* for 'p' */ \ |
528 | REF (form_number), /* for 'n' */ \ |
529 | REF (form_strerror), /* for 'm' */ \ |
530 | REF (form_wcharacter), /* for 'C' */ \ |
531 | REF (form_floathex), /* for 'A', 'a' */ \ |
532 | REF (form_unknown), /* for 't' */ \ |
533 | REF (form_unknown), /* for 'j' */ \ |
534 | REF (form_unknown), /* for 'I' */ \ |
535 | REF (form_binary), /* for 'B', 'b' */ \ |
536 | REF (form_unknown), /* for 'w' */ \ |
537 | } |
538 | |
539 | #define STEP4_TABLE \ |
540 | /* Step 4: processing format specifier. */ \ |
541 | static JUMP_TABLE_TYPE step4_jumps[32] = \ |
542 | { \ |
543 | REF (form_unknown), \ |
544 | REF (form_unknown), /* for ' ' */ \ |
545 | REF (form_unknown), /* for '+' */ \ |
546 | REF (form_unknown), /* for '-' */ \ |
547 | REF (form_unknown), /* for '<hash>' */ \ |
548 | REF (form_unknown), /* for '0' */ \ |
549 | REF (form_unknown), /* for '\'' */ \ |
550 | REF (form_unknown), /* for '*' */ \ |
551 | REF (form_unknown), /* for '1'...'9' */ \ |
552 | REF (form_unknown), /* for '.' */ \ |
553 | REF (form_unknown), /* for 'h' */ \ |
554 | REF (form_unknown), /* for 'l' */ \ |
555 | REF (form_unknown), /* for 'L', 'q' */ \ |
556 | REF (form_unknown), /* for 'z', 'Z' */ \ |
557 | REF (form_percent), /* for '%' */ \ |
558 | REF (form_integer), /* for 'd', 'i' */ \ |
559 | REF (form_unsigned), /* for 'u' */ \ |
560 | REF (form_octal), /* for 'o' */ \ |
561 | REF (form_hexa), /* for 'X', 'x' */ \ |
562 | REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \ |
563 | REF (form_character), /* for 'c' */ \ |
564 | REF (form_string), /* for 's', 'S' */ \ |
565 | REF (form_pointer), /* for 'p' */ \ |
566 | REF (form_number), /* for 'n' */ \ |
567 | REF (form_strerror), /* for 'm' */ \ |
568 | REF (form_wcharacter), /* for 'C' */ \ |
569 | REF (form_floathex), /* for 'A', 'a' */ \ |
570 | REF (form_unknown), /* for 't' */ \ |
571 | REF (form_unknown), /* for 'j' */ \ |
572 | REF (form_unknown), /* for 'I' */ \ |
573 | REF (form_binary), /* for 'B', 'b' */ \ |
574 | REF (form_unknown), /* for 'w' */ \ |
575 | } |
576 | |
577 | /* Handle positional format specifiers. */ |
578 | static void printf_positional (struct Xprintf_buffer *buf, |
579 | const CHAR_T *format, int readonly_format, |
580 | va_list ap, va_list *ap_savep, |
581 | int nspecs_done, const UCHAR_T *lead_str_end, |
582 | CHAR_T *work_buffer, int save_errno, |
583 | const char *grouping, |
584 | THOUSANDS_SEP_T thousands_sep, |
585 | unsigned int mode_flags); |
586 | |
587 | /* Handle unknown format specifier. */ |
588 | static void printf_unknown (struct Xprintf_buffer *, |
589 | const struct printf_info *) __THROW; |
590 | |
591 | static void group_number (struct Xprintf_buffer *buf, |
592 | struct grouping_iterator *iter, |
593 | CHAR_T *from, CHAR_T *to, |
594 | THOUSANDS_SEP_T thousands_sep, bool i18n); |
595 | |
596 | /* The buffer-based function itself. */ |
597 | void |
598 | Xprintf_buffer (struct Xprintf_buffer *buf, const CHAR_T *format, |
599 | va_list ap, unsigned int mode_flags) |
600 | { |
601 | /* The character used as thousands separator. */ |
602 | THOUSANDS_SEP_T thousands_sep = 0; |
603 | |
604 | /* The string describing the size of groups of digits. */ |
605 | const char *grouping; |
606 | |
607 | /* Current character in format string. */ |
608 | const UCHAR_T *f; |
609 | |
610 | /* End of leading constant string. */ |
611 | const UCHAR_T *lead_str_end; |
612 | |
613 | /* Points to next format specifier. */ |
614 | const UCHAR_T *end_of_spec; |
615 | |
616 | /* Buffer intermediate results. */ |
617 | CHAR_T work_buffer[WORK_BUFFER_SIZE]; |
618 | CHAR_T *workend; |
619 | |
620 | /* We have to save the original argument pointer. */ |
621 | va_list ap_save; |
622 | |
623 | /* Count number of specifiers we already processed. */ |
624 | int nspecs_done; |
625 | |
626 | /* For the %m format we may need the current `errno' value. */ |
627 | int save_errno = errno; |
628 | |
629 | /* 1 if format is in read-only memory, -1 if it is in writable memory, |
630 | 0 if unknown. */ |
631 | int readonly_format = 0; |
632 | |
633 | /* Initialize local variables. */ |
634 | grouping = (const char *) -1; |
635 | #ifdef __va_copy |
636 | /* This macro will be available soon in gcc's <stdarg.h>. We need it |
637 | since on some systems `va_list' is not an integral type. */ |
638 | __va_copy (ap_save, ap); |
639 | #else |
640 | ap_save = ap; |
641 | #endif |
642 | nspecs_done = 0; |
643 | |
644 | #ifdef COMPILE_WPRINTF |
645 | /* Find the first format specifier. */ |
646 | f = lead_str_end = __find_specwc ((const UCHAR_T *) format); |
647 | #else |
648 | /* Find the first format specifier. */ |
649 | f = lead_str_end = __find_specmb ((const UCHAR_T *) format); |
650 | #endif |
651 | |
652 | /* Write the literal text before the first format. */ |
653 | Xprintf_buffer_write (buf, format, |
654 | lead_str_end - (const UCHAR_T *) format); |
655 | if (Xprintf_buffer_has_failed (buf)) |
656 | return; |
657 | |
658 | /* If we only have to print a simple string, return now. */ |
659 | if (*f == L_('\0')) |
660 | return; |
661 | |
662 | /* Use the slow path in case any printf handler is registered. */ |
663 | if (__glibc_unlikely (__printf_function_table != NULL |
664 | || __printf_modifier_table != NULL |
665 | || __printf_va_arg_table != NULL)) |
666 | goto do_positional; |
667 | |
668 | /* Process whole format string. */ |
669 | do |
670 | { |
671 | STEP0_3_TABLE; |
672 | STEP4_TABLE; |
673 | |
674 | int is_negative; /* Flag for negative number. */ |
675 | union |
676 | { |
677 | unsigned long long int longlong; |
678 | unsigned long int word; |
679 | } number; |
680 | int base; |
681 | union printf_arg the_arg; |
682 | CHAR_T *string; /* Pointer to argument string. */ |
683 | int alt = 0; /* Alternate format. */ |
684 | int space = 0; /* Use space prefix if no sign is needed. */ |
685 | int left = 0; /* Left-justify output. */ |
686 | int showsign = 0; /* Always begin with plus or minus sign. */ |
687 | int group = 0; /* Print numbers according grouping rules. */ |
688 | /* Argument is long double/long long int. Only used if |
689 | double/long double or long int/long long int are distinct. */ |
690 | int is_long_double __attribute__ ((unused)) = 0; |
691 | int is_short = 0; /* Argument is short int. */ |
692 | int is_long = 0; /* Argument is long int. */ |
693 | int is_char = 0; /* Argument is promoted (unsigned) char. */ |
694 | int width = 0; /* Width of output; 0 means none specified. */ |
695 | int prec = -1; /* Precision of output; -1 means none specified. */ |
696 | /* This flag is set by the 'I' modifier and selects the use of the |
697 | `outdigits' as determined by the current locale. */ |
698 | int use_outdigits = 0; |
699 | UCHAR_T pad = L_(' ');/* Padding character. */ |
700 | CHAR_T spec; |
701 | |
702 | workend = work_buffer + WORK_BUFFER_SIZE; |
703 | |
704 | /* Get current character in format string. */ |
705 | JUMP (*++f, step0_jumps); |
706 | |
707 | /* ' ' flag. */ |
708 | LABEL (flag_space): |
709 | space = 1; |
710 | JUMP (*++f, step0_jumps); |
711 | |
712 | /* '+' flag. */ |
713 | LABEL (flag_plus): |
714 | showsign = 1; |
715 | JUMP (*++f, step0_jumps); |
716 | |
717 | /* The '-' flag. */ |
718 | LABEL (flag_minus): |
719 | left = 1; |
720 | pad = L_(' '); |
721 | JUMP (*++f, step0_jumps); |
722 | |
723 | /* The '#' flag. */ |
724 | LABEL (flag_hash): |
725 | alt = 1; |
726 | JUMP (*++f, step0_jumps); |
727 | |
728 | /* The '0' flag. */ |
729 | LABEL (flag_zero): |
730 | if (!left) |
731 | pad = L_('0'); |
732 | JUMP (*++f, step0_jumps); |
733 | |
734 | /* The '\'' flag. */ |
735 | LABEL (flag_quote): |
736 | group = 1; |
737 | |
738 | if (grouping == (const char *) -1) |
739 | { |
740 | #ifdef COMPILE_WPRINTF |
741 | thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC, |
742 | _NL_NUMERIC_THOUSANDS_SEP_WC); |
743 | #else |
744 | thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP); |
745 | #endif |
746 | |
747 | grouping = _NL_CURRENT (LC_NUMERIC, GROUPING); |
748 | if (*grouping == '\0' || *grouping == CHAR_MAX |
749 | #ifdef COMPILE_WPRINTF |
750 | || thousands_sep == L'\0' |
751 | #else |
752 | || *thousands_sep == '\0' |
753 | #endif |
754 | ) |
755 | grouping = NULL; |
756 | } |
757 | JUMP (*++f, step0_jumps); |
758 | |
759 | LABEL (flag_i18n): |
760 | use_outdigits = 1; |
761 | JUMP (*++f, step0_jumps); |
762 | |
763 | /* Get width from argument. */ |
764 | LABEL (width_asterics): |
765 | { |
766 | const UCHAR_T *tmp; /* Temporary value. */ |
767 | |
768 | tmp = ++f; |
769 | if (ISDIGIT (*tmp)) |
770 | { |
771 | int pos = read_int (&tmp); |
772 | |
773 | if (pos == -1) |
774 | { |
775 | __set_errno (EOVERFLOW); |
776 | Xprintf_buffer_mark_failed (buf); |
777 | goto all_done; |
778 | } |
779 | |
780 | if (pos && *tmp == L_('$')) |
781 | /* The width comes from a positional parameter. */ |
782 | goto do_positional; |
783 | } |
784 | width = va_arg (ap, int); |
785 | |
786 | /* Negative width means left justified. */ |
787 | if (width < 0) |
788 | { |
789 | width = -width; |
790 | pad = L_(' '); |
791 | left = 1; |
792 | } |
793 | } |
794 | JUMP (*f, step1_jumps); |
795 | |
796 | /* Given width in format string. */ |
797 | LABEL (width): |
798 | width = read_int (&f); |
799 | |
800 | if (__glibc_unlikely (width == -1)) |
801 | { |
802 | __set_errno (EOVERFLOW); |
803 | Xprintf_buffer_mark_failed (buf); |
804 | goto all_done; |
805 | } |
806 | |
807 | if (*f == L_('$')) |
808 | /* Oh, oh. The argument comes from a positional parameter. */ |
809 | goto do_positional; |
810 | JUMP (*f, step1_jumps); |
811 | |
812 | LABEL (precision): |
813 | ++f; |
814 | if (*f == L_('*')) |
815 | { |
816 | const UCHAR_T *tmp; /* Temporary value. */ |
817 | |
818 | tmp = ++f; |
819 | if (ISDIGIT (*tmp)) |
820 | { |
821 | int pos = read_int (&tmp); |
822 | |
823 | if (pos == -1) |
824 | { |
825 | __set_errno (EOVERFLOW); |
826 | Xprintf_buffer_mark_failed (buf); |
827 | goto all_done; |
828 | } |
829 | |
830 | if (pos && *tmp == L_('$')) |
831 | /* The precision comes from a positional parameter. */ |
832 | goto do_positional; |
833 | } |
834 | prec = va_arg (ap, int); |
835 | |
836 | /* If the precision is negative the precision is omitted. */ |
837 | if (prec < 0) |
838 | prec = -1; |
839 | } |
840 | else if (ISDIGIT (*f)) |
841 | { |
842 | prec = read_int (&f); |
843 | |
844 | /* The precision was specified in this case as an extremely |
845 | large positive value. */ |
846 | if (prec == -1) |
847 | { |
848 | __set_errno (EOVERFLOW); |
849 | Xprintf_buffer_mark_failed (buf); |
850 | goto all_done; |
851 | } |
852 | } |
853 | else |
854 | prec = 0; |
855 | JUMP (*f, step2_jumps); |
856 | |
857 | /* Process 'h' modifier. There might another 'h' following. */ |
858 | LABEL (mod_half): |
859 | is_short = 1; |
860 | JUMP (*++f, step3a_jumps); |
861 | |
862 | /* Process 'hh' modifier. */ |
863 | LABEL (mod_halfhalf): |
864 | is_short = 0; |
865 | is_char = 1; |
866 | JUMP (*++f, step4_jumps); |
867 | |
868 | /* Process 'l' modifier. There might another 'l' following. */ |
869 | LABEL (mod_long): |
870 | is_long = 1; |
871 | JUMP (*++f, step3b_jumps); |
872 | |
873 | /* Process 'L', 'q', or 'll' modifier. No other modifier is |
874 | allowed to follow. */ |
875 | LABEL (mod_longlong): |
876 | is_long_double = 1; |
877 | is_long = 1; |
878 | JUMP (*++f, step4_jumps); |
879 | |
880 | LABEL (mod_size_t): |
881 | is_long_double = sizeof (size_t) > sizeof (unsigned long int); |
882 | is_long = sizeof (size_t) > sizeof (unsigned int); |
883 | JUMP (*++f, step4_jumps); |
884 | |
885 | LABEL (mod_ptrdiff_t): |
886 | is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int); |
887 | is_long = sizeof (ptrdiff_t) > sizeof (unsigned int); |
888 | JUMP (*++f, step4_jumps); |
889 | |
890 | LABEL (mod_intmax_t): |
891 | is_long_double = sizeof (intmax_t) > sizeof (unsigned long int); |
892 | is_long = sizeof (intmax_t) > sizeof (unsigned int); |
893 | JUMP (*++f, step4_jumps); |
894 | |
895 | /* Process 'wN' or 'wfN' modifier. */ |
896 | LABEL (mod_bitwidth): |
897 | ++f; |
898 | bool is_fast = false; |
899 | if (*f == L_('f')) |
900 | { |
901 | ++f; |
902 | is_fast = true; |
903 | } |
904 | int bitwidth = 0; |
905 | if (ISDIGIT (*f)) |
906 | bitwidth = read_int (&f); |
907 | if (is_fast) |
908 | switch (bitwidth) |
909 | { |
910 | case 8: |
911 | bitwidth = INT_FAST8_WIDTH; |
912 | break; |
913 | case 16: |
914 | bitwidth = INT_FAST16_WIDTH; |
915 | break; |
916 | case 32: |
917 | bitwidth = INT_FAST32_WIDTH; |
918 | break; |
919 | case 64: |
920 | bitwidth = INT_FAST64_WIDTH; |
921 | break; |
922 | } |
923 | switch (bitwidth) |
924 | { |
925 | case 8: |
926 | is_char = 1; |
927 | break; |
928 | case 16: |
929 | is_short = 1; |
930 | break; |
931 | case 32: |
932 | break; |
933 | case 64: |
934 | is_long_double = 1; |
935 | is_long = 1; |
936 | break; |
937 | default: |
938 | /* ISO C requires this error to be detected. */ |
939 | __set_errno (EINVAL); |
940 | Xprintf_buffer_mark_failed (buf); |
941 | goto all_done; |
942 | } |
943 | JUMP (*f, step4_jumps); |
944 | |
945 | /* Process current format. */ |
946 | while (1) |
947 | { |
948 | #define process_arg_int() va_arg (ap, int) |
949 | #define process_arg_long_int() va_arg (ap, long int) |
950 | #define process_arg_long_long_int() va_arg (ap, long long int) |
951 | #define process_arg_pointer() va_arg (ap, void *) |
952 | #define process_arg_string() va_arg (ap, const char *) |
953 | #define process_arg_unsigned_int() va_arg (ap, unsigned int) |
954 | #define process_arg_unsigned_long_int() va_arg (ap, unsigned long int) |
955 | #define process_arg_unsigned_long_long_int() va_arg (ap, unsigned long long int) |
956 | #define process_arg_wchar_t() va_arg (ap, wchar_t) |
957 | #define process_arg_wstring() va_arg (ap, const wchar_t *) |
958 | #include "vfprintf-process-arg.c" |
959 | #undef process_arg_int |
960 | #undef process_arg_long_int |
961 | #undef process_arg_long_long_int |
962 | #undef process_arg_pointer |
963 | #undef process_arg_string |
964 | #undef process_arg_unsigned_int |
965 | #undef process_arg_unsigned_long_int |
966 | #undef process_arg_unsigned_long_long_int |
967 | #undef process_arg_wchar_t |
968 | #undef process_arg_wstring |
969 | |
970 | LABEL (form_float): |
971 | LABEL (form_floathex): |
972 | { |
973 | if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) |
974 | is_long_double = 0; |
975 | |
976 | struct printf_info info = |
977 | { |
978 | .prec = prec, |
979 | .width = width, |
980 | .spec = spec, |
981 | .is_long_double = is_long_double, |
982 | .is_short = is_short, |
983 | .is_long = is_long, |
984 | .alt = alt, |
985 | .space = space, |
986 | .left = left, |
987 | .showsign = showsign, |
988 | .group = group, |
989 | .pad = pad, |
990 | .extra = 0, |
991 | .i18n = use_outdigits, |
992 | .wide = sizeof (CHAR_T) != 1, |
993 | .is_binary128 = 0 |
994 | }; |
995 | |
996 | PARSE_FLOAT_VA_ARG_EXTENDED (info); |
997 | const void *ptr = &the_arg; |
998 | |
999 | __printf_fp_spec (buf, &info, &ptr); |
1000 | } |
1001 | break; |
1002 | |
1003 | LABEL (form_unknown): |
1004 | if (spec == L_('\0')) |
1005 | { |
1006 | /* The format string ended before the specifier is complete. */ |
1007 | __set_errno (EINVAL); |
1008 | Xprintf_buffer_mark_failed (buf); |
1009 | goto all_done; |
1010 | } |
1011 | |
1012 | /* If we are in the fast loop force entering the complicated |
1013 | one. */ |
1014 | goto do_positional; |
1015 | } |
1016 | |
1017 | /* The format is correctly handled. */ |
1018 | ++nspecs_done; |
1019 | |
1020 | /* Look for next format specifier. */ |
1021 | #ifdef COMPILE_WPRINTF |
1022 | f = __find_specwc ((end_of_spec = ++f)); |
1023 | #else |
1024 | f = __find_specmb ((end_of_spec = ++f)); |
1025 | #endif |
1026 | |
1027 | /* Write the following constant string. */ |
1028 | Xprintf_buffer_write (buf, (const CHAR_T *) end_of_spec, |
1029 | f - end_of_spec); |
1030 | } |
1031 | while (*f != L_('\0') && !Xprintf_buffer_has_failed (buf)); |
1032 | |
1033 | all_done: |
1034 | /* printf_positional performs cleanup under its all_done label, so |
1035 | vfprintf-process-arg.c uses it for this function and |
1036 | printf_positional below. */ |
1037 | return; |
1038 | |
1039 | /* Hand off processing for positional parameters. */ |
1040 | do_positional: |
1041 | printf_positional (buf, format, readonly_format, ap, &ap_save, |
1042 | nspecs_done, lead_str_end, work_buffer, |
1043 | save_errno, grouping, thousands_sep, mode_flags); |
1044 | } |
1045 | |
1046 | static void |
1047 | printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format, |
1048 | int readonly_format, |
1049 | va_list ap, va_list *ap_savep, int nspecs_done, |
1050 | const UCHAR_T *lead_str_end, |
1051 | CHAR_T *work_buffer, int save_errno, |
1052 | const char *grouping, THOUSANDS_SEP_T thousands_sep, |
1053 | unsigned int mode_flags) |
1054 | { |
1055 | /* For positional argument handling. */ |
1056 | struct scratch_buffer specsbuf; |
1057 | scratch_buffer_init (&specsbuf); |
1058 | struct printf_spec *specs = specsbuf.data; |
1059 | size_t specs_limit = specsbuf.length / sizeof (specs[0]); |
1060 | |
1061 | /* Used as a backing store for args_value, args_size, args_type |
1062 | below. */ |
1063 | struct scratch_buffer argsbuf; |
1064 | scratch_buffer_init (&argsbuf); |
1065 | |
1066 | /* Array with information about the needed arguments. This has to |
1067 | be dynamically extensible. */ |
1068 | size_t nspecs = 0; |
1069 | |
1070 | /* The number of arguments the format string requests. This will |
1071 | determine the size of the array needed to store the argument |
1072 | attributes. */ |
1073 | size_t nargs = 0; |
1074 | |
1075 | /* Positional parameters refer to arguments directly. This could |
1076 | also determine the maximum number of arguments. Track the |
1077 | maximum number. */ |
1078 | size_t max_ref_arg = 0; |
1079 | |
1080 | /* Just a counter. */ |
1081 | size_t cnt; |
1082 | |
1083 | if (grouping == (const char *) -1) |
1084 | { |
1085 | #ifdef COMPILE_WPRINTF |
1086 | thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC, |
1087 | _NL_NUMERIC_THOUSANDS_SEP_WC); |
1088 | #else |
1089 | thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP); |
1090 | #endif |
1091 | |
1092 | grouping = _NL_CURRENT (LC_NUMERIC, GROUPING); |
1093 | if (*grouping == '\0' || *grouping == CHAR_MAX) |
1094 | grouping = NULL; |
1095 | } |
1096 | |
1097 | for (const UCHAR_T *f = lead_str_end; *f != L_('\0'); |
1098 | f = specs[nspecs++].next_fmt) |
1099 | { |
1100 | if (nspecs == specs_limit) |
1101 | { |
1102 | if (!scratch_buffer_grow_preserve (&specsbuf)) |
1103 | { |
1104 | Xprintf_buffer_mark_failed (buf); |
1105 | goto all_done; |
1106 | } |
1107 | specs = specsbuf.data; |
1108 | specs_limit = specsbuf.length / sizeof (specs[0]); |
1109 | } |
1110 | |
1111 | /* Parse the format specifier. */ |
1112 | bool failed; |
1113 | #ifdef COMPILE_WPRINTF |
1114 | nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg, |
1115 | &failed); |
1116 | #else |
1117 | nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg, |
1118 | &failed); |
1119 | #endif |
1120 | if (failed) |
1121 | { |
1122 | Xprintf_buffer_mark_failed (buf); |
1123 | goto all_done; |
1124 | } |
1125 | } |
1126 | |
1127 | /* Determine the number of arguments the format string consumes. */ |
1128 | nargs = MAX (nargs, max_ref_arg); |
1129 | |
1130 | union printf_arg *args_value; |
1131 | int *args_size; |
1132 | int *args_type; |
1133 | { |
1134 | /* Calculate total size needed to represent a single argument |
1135 | across all three argument-related arrays. */ |
1136 | size_t bytes_per_arg |
1137 | = sizeof (*args_value) + sizeof (*args_size) + sizeof (*args_type); |
1138 | if (!scratch_buffer_set_array_size (&argsbuf, nargs, bytes_per_arg)) |
1139 | { |
1140 | Xprintf_buffer_mark_failed (buf); |
1141 | goto all_done; |
1142 | } |
1143 | args_value = argsbuf.data; |
1144 | /* Set up the remaining two arrays to each point past the end of |
1145 | the prior array, since space for all three has been allocated |
1146 | now. */ |
1147 | args_size = &args_value[nargs].pa_int; |
1148 | args_type = &args_size[nargs]; |
1149 | memset (args_type, (mode_flags & PRINTF_FORTIFY) != 0 ? '\xff' : '\0', |
1150 | nargs * sizeof (*args_type)); |
1151 | } |
1152 | |
1153 | /* XXX Could do sanity check here: If any element in ARGS_TYPE is |
1154 | still zero after this loop, format is invalid. For now we |
1155 | simply use 0 as the value. */ |
1156 | |
1157 | /* Fill in the types of all the arguments. */ |
1158 | for (cnt = 0; cnt < nspecs; ++cnt) |
1159 | { |
1160 | /* If the width is determined by an argument this is an int. */ |
1161 | if (specs[cnt].width_arg != -1) |
1162 | args_type[specs[cnt].width_arg] = PA_INT; |
1163 | |
1164 | /* If the precision is determined by an argument this is an int. */ |
1165 | if (specs[cnt].prec_arg != -1) |
1166 | args_type[specs[cnt].prec_arg] = PA_INT; |
1167 | |
1168 | switch (specs[cnt].ndata_args) |
1169 | { |
1170 | case 0: /* No arguments. */ |
1171 | break; |
1172 | case 1: /* One argument; we already have the |
1173 | type and size. */ |
1174 | args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type; |
1175 | args_size[specs[cnt].data_arg] = specs[cnt].size; |
1176 | break; |
1177 | default: |
1178 | /* We have more than one argument for this format spec. |
1179 | We must call the arginfo function again to determine |
1180 | all the types. */ |
1181 | (void) (*__printf_arginfo_table[specs[cnt].info.spec]) |
1182 | (&specs[cnt].info, |
1183 | specs[cnt].ndata_args, &args_type[specs[cnt].data_arg], |
1184 | &args_size[specs[cnt].data_arg]); |
1185 | break; |
1186 | } |
1187 | } |
1188 | |
1189 | /* Now we know all the types and the order. Fill in the argument |
1190 | values. */ |
1191 | for (cnt = 0; cnt < nargs; ++cnt) |
1192 | switch (args_type[cnt]) |
1193 | { |
1194 | #define T(tag, mem, type) \ |
1195 | case tag: \ |
1196 | args_value[cnt].mem = va_arg (*ap_savep, type); \ |
1197 | break |
1198 | |
1199 | T (PA_WCHAR, pa_wchar, wint_t); |
1200 | case PA_CHAR: /* Promoted. */ |
1201 | case PA_INT|PA_FLAG_SHORT: /* Promoted. */ |
1202 | #if LONG_MAX == INT_MAX |
1203 | case PA_INT|PA_FLAG_LONG: |
1204 | #endif |
1205 | T (PA_INT, pa_int, int); |
1206 | #if LONG_MAX == LONG_LONG_MAX |
1207 | case PA_INT|PA_FLAG_LONG: |
1208 | #endif |
1209 | T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int); |
1210 | #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX |
1211 | # error "he?" |
1212 | #endif |
1213 | case PA_FLOAT: /* Promoted. */ |
1214 | T (PA_DOUBLE, pa_double, double); |
1215 | case PA_DOUBLE|PA_FLAG_LONG_DOUBLE: |
1216 | if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) |
1217 | { |
1218 | args_value[cnt].pa_double = va_arg (*ap_savep, double); |
1219 | args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE; |
1220 | } |
1221 | #if __HAVE_FLOAT128_UNLIKE_LDBL |
1222 | else if ((mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) |
1223 | args_value[cnt].pa_float128 = va_arg (*ap_savep, _Float128); |
1224 | #endif |
1225 | else |
1226 | args_value[cnt].pa_long_double = va_arg (*ap_savep, long double); |
1227 | break; |
1228 | case PA_STRING: /* All pointers are the same */ |
1229 | case PA_WSTRING: /* All pointers are the same */ |
1230 | T (PA_POINTER, pa_pointer, void *); |
1231 | #undef T |
1232 | default: |
1233 | if ((args_type[cnt] & PA_FLAG_PTR) != 0) |
1234 | args_value[cnt].pa_pointer = va_arg (*ap_savep, void *); |
1235 | else if (__glibc_unlikely (__printf_va_arg_table != NULL) |
1236 | && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL) |
1237 | { |
1238 | args_value[cnt].pa_user = alloca (args_size[cnt]); |
1239 | (*__printf_va_arg_table[args_type[cnt] - PA_LAST]) |
1240 | (args_value[cnt].pa_user, ap_savep); |
1241 | } |
1242 | else |
1243 | memset (&args_value[cnt], 0, sizeof (args_value[cnt])); |
1244 | break; |
1245 | case -1: |
1246 | /* Error case. Not all parameters appear in N$ format |
1247 | strings. We have no way to determine their type. */ |
1248 | assert ((mode_flags & PRINTF_FORTIFY) != 0); |
1249 | __libc_fatal ("*** invalid %N$ use detected ***\n" ); |
1250 | } |
1251 | |
1252 | /* Now walk through all format specifiers and process them. */ |
1253 | for (; (size_t) nspecs_done < nspecs && !Xprintf_buffer_has_failed (buf); |
1254 | ++nspecs_done) |
1255 | { |
1256 | STEP4_TABLE; |
1257 | |
1258 | int is_negative; |
1259 | union |
1260 | { |
1261 | unsigned long long int longlong; |
1262 | unsigned long int word; |
1263 | } number; |
1264 | int base; |
1265 | CHAR_T *string; /* Pointer to argument string. */ |
1266 | |
1267 | /* Fill variables from values in struct. */ |
1268 | int alt = specs[nspecs_done].info.alt; |
1269 | int space = specs[nspecs_done].info.space; |
1270 | int left = specs[nspecs_done].info.left; |
1271 | int showsign = specs[nspecs_done].info.showsign; |
1272 | int group = specs[nspecs_done].info.group; |
1273 | int is_long_double __attribute__ ((unused)) |
1274 | = specs[nspecs_done].info.is_long_double; |
1275 | int is_short = specs[nspecs_done].info.is_short; |
1276 | int is_char = specs[nspecs_done].info.is_char; |
1277 | int is_long = specs[nspecs_done].info.is_long; |
1278 | int width = specs[nspecs_done].info.width; |
1279 | int prec = specs[nspecs_done].info.prec; |
1280 | int use_outdigits = specs[nspecs_done].info.i18n; |
1281 | char pad = specs[nspecs_done].info.pad; |
1282 | CHAR_T spec = specs[nspecs_done].info.spec; |
1283 | |
1284 | CHAR_T *workend = work_buffer + WORK_BUFFER_SIZE; |
1285 | |
1286 | /* Fill in last information. */ |
1287 | if (specs[nspecs_done].width_arg != -1) |
1288 | { |
1289 | /* Extract the field width from an argument. */ |
1290 | specs[nspecs_done].info.width = |
1291 | args_value[specs[nspecs_done].width_arg].pa_int; |
1292 | |
1293 | if (specs[nspecs_done].info.width < 0) |
1294 | /* If the width value is negative left justification is |
1295 | selected and the value is taken as being positive. */ |
1296 | { |
1297 | specs[nspecs_done].info.width *= -1; |
1298 | left = specs[nspecs_done].info.left = 1; |
1299 | } |
1300 | width = specs[nspecs_done].info.width; |
1301 | } |
1302 | |
1303 | if (specs[nspecs_done].prec_arg != -1) |
1304 | { |
1305 | /* Extract the precision from an argument. */ |
1306 | specs[nspecs_done].info.prec = |
1307 | args_value[specs[nspecs_done].prec_arg].pa_int; |
1308 | |
1309 | if (specs[nspecs_done].info.prec < 0) |
1310 | /* If the precision is negative the precision is |
1311 | omitted. */ |
1312 | specs[nspecs_done].info.prec = -1; |
1313 | |
1314 | prec = specs[nspecs_done].info.prec; |
1315 | } |
1316 | |
1317 | /* Process format specifiers. */ |
1318 | do |
1319 | { |
1320 | if (spec <= UCHAR_MAX |
1321 | && __printf_function_table != NULL |
1322 | && __printf_function_table[(size_t) spec] != NULL) |
1323 | { |
1324 | int function_done |
1325 | = Xprintf (function_invoke) (buf, |
1326 | __printf_function_table[(size_t) spec], |
1327 | &args_value[specs[nspecs_done] |
1328 | .data_arg], |
1329 | specs[nspecs_done].ndata_args, |
1330 | &specs[nspecs_done].info); |
1331 | if (function_done != -2) |
1332 | { |
1333 | /* If an error occurred we don't have information |
1334 | about # of chars. */ |
1335 | if (function_done < 0) |
1336 | { |
1337 | /* Function has set errno. */ |
1338 | Xprintf_buffer_mark_failed (buf); |
1339 | goto all_done; |
1340 | } |
1341 | break; |
1342 | } |
1343 | } |
1344 | |
1345 | JUMP (spec, step4_jumps); |
1346 | |
1347 | #define process_arg_data args_value[specs[nspecs_done].data_arg] |
1348 | #define process_arg_int() process_arg_data.pa_int |
1349 | #define process_arg_long_int() process_arg_data.pa_long_int |
1350 | #define process_arg_long_long_int() process_arg_data.pa_long_long_int |
1351 | #define process_arg_pointer() process_arg_data.pa_pointer |
1352 | #define process_arg_string() process_arg_data.pa_string |
1353 | #define process_arg_unsigned_int() process_arg_data.pa_u_int |
1354 | #define process_arg_unsigned_long_int() process_arg_data.pa_u_long_int |
1355 | #define process_arg_unsigned_long_long_int() process_arg_data.pa_u_long_long_int |
1356 | #define process_arg_wchar_t() process_arg_data.pa_wchar |
1357 | #define process_arg_wstring() process_arg_data.pa_wstring |
1358 | #include "vfprintf-process-arg.c" |
1359 | #undef process_arg_data |
1360 | #undef process_arg_int |
1361 | #undef process_arg_long_int |
1362 | #undef process_arg_long_long_int |
1363 | #undef process_arg_pointer |
1364 | #undef process_arg_string |
1365 | #undef process_arg_unsigned_int |
1366 | #undef process_arg_unsigned_long_int |
1367 | #undef process_arg_unsigned_long_long_int |
1368 | #undef process_arg_wchar_t |
1369 | #undef process_arg_wstring |
1370 | |
1371 | LABEL (form_float): |
1372 | LABEL (form_floathex): |
1373 | { |
1374 | const void *ptr |
1375 | = (const void *) &args_value[specs[nspecs_done].data_arg]; |
1376 | if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) |
1377 | { |
1378 | specs[nspecs_done].data_arg_type = PA_DOUBLE; |
1379 | specs[nspecs_done].info.is_long_double = 0; |
1380 | } |
1381 | SETUP_FLOAT128_INFO (specs[nspecs_done].info); |
1382 | |
1383 | __printf_fp_spec (buf, &specs[nspecs_done].info, &ptr); |
1384 | } |
1385 | break; |
1386 | |
1387 | LABEL (form_unknown): |
1388 | { |
1389 | printf_unknown (buf, &specs[nspecs_done].info); |
1390 | } |
1391 | break; |
1392 | } |
1393 | while (Xprintf_buffer_has_failed (buf)); |
1394 | |
1395 | /* Write the following constant string. */ |
1396 | Xprintf_buffer_write (buf, |
1397 | (const CHAR_T *) specs[nspecs_done].end_of_fmt, |
1398 | (specs[nspecs_done].next_fmt |
1399 | - specs[nspecs_done].end_of_fmt)); |
1400 | } |
1401 | all_done: |
1402 | scratch_buffer_free (&argsbuf); |
1403 | scratch_buffer_free (&specsbuf); |
1404 | } |
1405 | |
1406 | /* Handle an unknown format specifier. This prints out a canonicalized |
1407 | representation of the format spec itself. */ |
1408 | static void |
1409 | printf_unknown (struct Xprintf_buffer *buf, const struct printf_info *info) |
1410 | { |
1411 | CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3]; |
1412 | CHAR_T *const workend |
1413 | = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)]; |
1414 | CHAR_T *w; |
1415 | |
1416 | Xprintf_buffer_putc (buf, L_('%')); |
1417 | |
1418 | if (info->alt) |
1419 | Xprintf_buffer_putc (buf, L_('#')); |
1420 | if (info->group) |
1421 | Xprintf_buffer_putc (buf, L_('\'')); |
1422 | if (info->showsign) |
1423 | Xprintf_buffer_putc (buf, L_('+')); |
1424 | else if (info->space) |
1425 | Xprintf_buffer_putc (buf, L_(' ')); |
1426 | if (info->left) |
1427 | Xprintf_buffer_putc (buf, L_('-')); |
1428 | if (info->pad == L_('0')) |
1429 | Xprintf_buffer_putc (buf, L_('0')); |
1430 | if (info->i18n) |
1431 | Xprintf_buffer_putc (buf, L_('I')); |
1432 | |
1433 | if (info->width != 0) |
1434 | { |
1435 | w = _itoa_word (info->width, workend, 10, 0); |
1436 | while (w < workend) |
1437 | Xprintf_buffer_putc (buf, *w++); |
1438 | } |
1439 | |
1440 | if (info->prec != -1) |
1441 | { |
1442 | Xprintf_buffer_putc (buf, L_('.')); |
1443 | w = _itoa_word (info->prec, workend, 10, 0); |
1444 | while (w < workend) |
1445 | Xprintf_buffer_putc (buf, *w++); |
1446 | } |
1447 | |
1448 | if (info->spec != L_('\0')) |
1449 | Xprintf_buffer_putc (buf, info->spec); |
1450 | } |
1451 | |
1452 | static void |
1453 | group_number (struct Xprintf_buffer *buf, |
1454 | struct grouping_iterator *iter, |
1455 | CHAR_T *from, CHAR_T *to, THOUSANDS_SEP_T thousands_sep, |
1456 | bool i18n) |
1457 | { |
1458 | if (!i18n) |
1459 | for (CHAR_T *cp = from; cp != to; ++cp) |
1460 | { |
1461 | if (__grouping_iterator_next (iter)) |
1462 | { |
1463 | #ifdef COMPILE_WPRINTF |
1464 | __wprintf_buffer_putc (buf, thousands_sep); |
1465 | #else |
1466 | __printf_buffer_puts (buf, thousands_sep); |
1467 | #endif |
1468 | } |
1469 | Xprintf_buffer_putc (buf, *cp); |
1470 | } |
1471 | else |
1472 | { |
1473 | /* Apply digit translation and grouping. */ |
1474 | for (CHAR_T *cp = from; cp != to; ++cp) |
1475 | { |
1476 | if (__grouping_iterator_next (iter)) |
1477 | { |
1478 | #ifdef COMPILE_WPRINTF |
1479 | __wprintf_buffer_putc (buf, thousands_sep); |
1480 | #else |
1481 | __printf_buffer_puts (buf, thousands_sep); |
1482 | #endif |
1483 | } |
1484 | int digit = *cp - '0'; |
1485 | #ifdef COMPILE_WPRINTF |
1486 | __wprintf_buffer_putc |
1487 | (buf, _NL_CURRENT_WORD (LC_CTYPE, |
1488 | _NL_CTYPE_OUTDIGIT0_WC + digit)); |
1489 | #else |
1490 | __printf_buffer_puts |
1491 | (buf, _NL_CURRENT (LC_CTYPE, _NL_CTYPE_OUTDIGIT0_MB + digit)); |
1492 | #endif |
1493 | } |
1494 | } |
1495 | } |
1496 | |
1497 | |
1498 | /* The FILE-based function. */ |
1499 | int |
1500 | vfprintf (FILE *s, const CHAR_T *format, va_list ap, unsigned int mode_flags) |
1501 | { |
1502 | /* Orient the stream. */ |
1503 | #ifdef ORIENT |
1504 | ORIENT; |
1505 | #endif |
1506 | |
1507 | /* Sanity check of arguments. */ |
1508 | ARGCHECK (s, format); |
1509 | |
1510 | #ifdef ORIENT |
1511 | /* Check for correct orientation. */ |
1512 | if (_IO_vtable_offset (s) == 0 |
1513 | && _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1) |
1514 | != (sizeof (CHAR_T) == 1 ? -1 : 1)) |
1515 | /* The stream is already oriented otherwise. */ |
1516 | return EOF; |
1517 | #endif |
1518 | |
1519 | if (!_IO_need_lock (s)) |
1520 | { |
1521 | struct Xprintf (buffer_to_file) wrap; |
1522 | Xprintf (buffer_to_file_init) (&wrap, s); |
1523 | Xprintf_buffer (&wrap.base, format, ap, mode_flags); |
1524 | return Xprintf (buffer_to_file_done) (&wrap); |
1525 | } |
1526 | |
1527 | int done; |
1528 | |
1529 | /* Lock stream. */ |
1530 | _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s); |
1531 | _IO_flockfile (s); |
1532 | |
1533 | /* Set up the wrapping buffer. */ |
1534 | struct Xprintf (buffer_to_file) wrap; |
1535 | Xprintf (buffer_to_file_init) (&wrap, s); |
1536 | |
1537 | /* Perform the printing operation on the buffer. */ |
1538 | Xprintf_buffer (&wrap.base, format, ap, mode_flags); |
1539 | done = Xprintf (buffer_to_file_done) (&wrap); |
1540 | |
1541 | /* Unlock the stream. */ |
1542 | _IO_funlockfile (s); |
1543 | _IO_cleanup_region_end (0); |
1544 | |
1545 | return done; |
1546 | } |
1547 | |