/src/php-src/main/spprintf.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Author: Marcus Boerger <helly@php.net> | |
12 | | +----------------------------------------------------------------------+ |
13 | | */ |
14 | | |
15 | | /* This is the spprintf implementation. |
16 | | * It has emerged from apache snprintf. See original header: |
17 | | */ |
18 | | |
19 | | /* ==================================================================== |
20 | | * Copyright (c) 1995-1998 The Apache Group. All rights reserved. |
21 | | * |
22 | | * Redistribution and use in source and binary forms, with or without |
23 | | * modification, are permitted provided that the following conditions |
24 | | * are met: |
25 | | * |
26 | | * 1. Redistributions of source code must retain the above copyright |
27 | | * notice, this list of conditions and the following disclaimer. |
28 | | * |
29 | | * 2. Redistributions in binary form must reproduce the above copyright |
30 | | * notice, this list of conditions and the following disclaimer in |
31 | | * the documentation and/or other materials provided with the |
32 | | * distribution. |
33 | | * |
34 | | * 3. All advertising materials mentioning features or use of this |
35 | | * software must display the following acknowledgment: |
36 | | * "This product includes software developed by the Apache Group |
37 | | * for use in the Apache HTTP server project (http://www.apache.org/)." |
38 | | * |
39 | | * 4. The names "Apache Server" and "Apache Group" must not be used to |
40 | | * endorse or promote products derived from this software without |
41 | | * prior written permission. |
42 | | * |
43 | | * 5. Redistributions of any form whatsoever must retain the following |
44 | | * acknowledgment: |
45 | | * "This product includes software developed by the Apache Group |
46 | | * for use in the Apache HTTP server project (http://www.apache.org/)." |
47 | | * |
48 | | * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY |
49 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
50 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
51 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR |
52 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
53 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
54 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
55 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
56 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
57 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
58 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
59 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
60 | | * ==================================================================== |
61 | | * |
62 | | * This software consists of voluntary contributions made by many |
63 | | * individuals on behalf of the Apache Group and was originally based |
64 | | * on public domain software written at the National Center for |
65 | | * Supercomputing Applications, University of Illinois, Urbana-Champaign. |
66 | | * For more information on the Apache Group and the Apache HTTP server |
67 | | * project, please see <http://www.apache.org/>. |
68 | | * |
69 | | * This code is based on, and used with the permission of, the |
70 | | * SIO stdio-replacement strx_* functions by Panos Tsirigotis |
71 | | * <panos@alumni.cs.colorado.edu> for xinetd. |
72 | | */ |
73 | | #ifndef _GNU_SOURCE |
74 | | # define _GNU_SOURCE |
75 | | #endif |
76 | | #include "php.h" |
77 | | |
78 | | #include <stddef.h> |
79 | | #include <stdio.h> |
80 | | #include <ctype.h> |
81 | | #include <sys/types.h> |
82 | | #include <stdarg.h> |
83 | | #include <string.h> |
84 | | #include <stdlib.h> |
85 | | #include <math.h> |
86 | | #include <inttypes.h> |
87 | | |
88 | | #include <locale.h> |
89 | | #ifdef ZTS |
90 | | #include "ext/standard/php_string.h" |
91 | | #define LCONV_DECIMAL_POINT localeconv_decimal_point() |
92 | | #else |
93 | 1.78M | #define LCONV_DECIMAL_POINT (*lconv->decimal_point) |
94 | | #endif |
95 | | |
96 | | #include "snprintf.h" |
97 | | |
98 | 54.6M | #define NUL '\0' |
99 | | #define INT_NULL ((int *)0) |
100 | | |
101 | 0 | #define S_NULL "(null)" |
102 | 0 | #define S_NULL_LEN 6 |
103 | | |
104 | 0 | #define FLOAT_DIGITS 6 |
105 | | #define EXPONENT_LENGTH 10 |
106 | | |
107 | | #include "zend_smart_str.h" |
108 | | #include "zend_smart_string.h" |
109 | | |
110 | | /* {{{ macros */ |
111 | | |
112 | 146M | #define INS_CHAR(xbuf, ch, is_char) do { \ |
113 | 146M | if ((is_char)) { \ |
114 | 35.8M | smart_string_appendc((smart_string *)(xbuf), (ch)); \ |
115 | 110M | } else { \ |
116 | 110M | smart_str_appendc((smart_str *)(xbuf), (ch)); \ |
117 | 110M | } \ |
118 | 146M | } while (0); |
119 | | |
120 | 27.3M | #define INS_STRING(xbuf, str, len, is_char) do { \ |
121 | 27.3M | if ((is_char)) { \ |
122 | 12.3M | smart_string_appendl((smart_string *)(xbuf), (str), (len)); \ |
123 | 14.9M | } else { \ |
124 | 14.9M | smart_str_appendl((smart_str *)(xbuf), (str), (len)); \ |
125 | 14.9M | } \ |
126 | 27.3M | } while (0); |
127 | | |
128 | 4.85M | #define PAD_CHAR(xbuf, ch, count, is_char) do { \ |
129 | 4.85M | if ((is_char)) { \ |
130 | 4.83M | smart_string_alloc(((smart_string *)(xbuf)), (count), 0); \ |
131 | 4.83M | memset(((smart_string *)(xbuf))->c + ((smart_string *)(xbuf))->len, (ch), (count)); \ |
132 | 4.83M | ((smart_string *)(xbuf))->len += (count); \ |
133 | 4.83M | } else { \ |
134 | 22.1k | smart_str_alloc(((smart_str *)(xbuf)), (count), 0); \ |
135 | 22.1k | memset(ZSTR_VAL(((smart_str *)(xbuf))->s) + ZSTR_LEN(((smart_str *)(xbuf))->s), (ch), (count)); \ |
136 | 22.1k | ZSTR_LEN(((smart_str *)(xbuf))->s) += (count); \ |
137 | 22.1k | } \ |
138 | 4.85M | } while (0); |
139 | | |
140 | | /* |
141 | | * NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions |
142 | | * which can be at most max length of double |
143 | | */ |
144 | 5.99M | #define NUM_BUF_SIZE ZEND_DOUBLE_MAX_LENGTH |
145 | | |
146 | 67.3k | #define NUM(c) (c - '0') |
147 | | |
148 | 54.2k | #define STR_TO_DEC(str, num) do { \ |
149 | 54.2k | num = NUM(*(str)++); \ |
150 | 54.2k | while (isdigit((unsigned char)*(str))) {\ |
151 | 13.1k | num *= 10; \ |
152 | 13.1k | num += NUM(*(str)++); \ |
153 | 13.1k | if (num >= INT_MAX / 10) { \ |
154 | 0 | while (isdigit((unsigned char)*(str)++)); \ |
155 | 0 | break; \ |
156 | 0 | } \ |
157 | 13.1k | } \ |
158 | 54.2k | } while (0) |
159 | | |
160 | | /* |
161 | | * This macro does zero padding so that the precision |
162 | | * requirement is satisfied. The padding is done by |
163 | | * adding '0's to the left of the string that is going |
164 | | * to be printed. |
165 | | */ |
166 | 5.99M | #define FIX_PRECISION(adjust, precision, s, s_len) do { \ |
167 | 5.99M | if (adjust) \ |
168 | 5.99M | while (s_len < (size_t)precision) { \ |
169 | 0 | *--s = '0'; \ |
170 | 0 | s_len++; \ |
171 | 0 | } \ |
172 | 5.99M | } while (0) |
173 | | |
174 | | /* }}} */ |
175 | | |
176 | | /* |
177 | | * Do format conversion placing the output in buffer |
178 | | */ |
179 | | static void xbuf_format_converter(void *xbuf, bool is_char, const char *fmt, va_list ap) /* {{{ */ |
180 | 12.3M | { |
181 | 12.3M | char *s = NULL; |
182 | 12.3M | size_t s_len; |
183 | | |
184 | 12.3M | int min_width = 0; |
185 | 12.3M | int precision = 0; |
186 | 12.3M | enum { |
187 | 12.3M | LEFT, RIGHT |
188 | 12.3M | } adjust; |
189 | 12.3M | char pad_char; |
190 | 12.3M | char prefix_char; |
191 | | |
192 | 12.3M | double fp_num; |
193 | 12.3M | int64_t i_num = (int64_t) 0; |
194 | 12.3M | uint64_t ui_num = (uint64_t) 0; |
195 | | |
196 | 12.3M | char num_buf[NUM_BUF_SIZE]; |
197 | 12.3M | char char_buf[2]; /* for printing %% and %<unknown> */ |
198 | | |
199 | 12.3M | #ifndef ZTS |
200 | 12.3M | struct lconv *lconv = NULL; |
201 | 12.3M | #endif |
202 | | |
203 | | /* |
204 | | * Flag variables |
205 | | */ |
206 | 12.3M | length_modifier_e modifier; |
207 | 12.3M | bool alternate_form; |
208 | 12.3M | bool print_sign; |
209 | 12.3M | bool print_blank; |
210 | 12.3M | bool adjust_precision; |
211 | 12.3M | bool adjust_width; |
212 | 12.3M | bool is_negative; |
213 | | |
214 | 186M | while (*fmt) { |
215 | 173M | if (*fmt != '%') { |
216 | 146M | INS_CHAR(xbuf, *fmt, is_char); |
217 | 146M | } else { |
218 | | /* |
219 | | * Default variable settings |
220 | | */ |
221 | 27.3M | zend_string *tmp_str = NULL; |
222 | 27.3M | adjust = RIGHT; |
223 | 27.3M | alternate_form = print_sign = print_blank = false; |
224 | 27.3M | pad_char = ' '; |
225 | 27.3M | prefix_char = NUL; |
226 | | |
227 | 27.3M | fmt++; |
228 | | |
229 | | /* |
230 | | * Try to avoid checking for flags, width or precision |
231 | | */ |
232 | 27.3M | if (isascii((unsigned char)*fmt) && !islower((unsigned char)*fmt)) { |
233 | | /* |
234 | | * Recognize flags: -, #, BLANK, + |
235 | | */ |
236 | 13.5M | for (;; fmt++) { |
237 | 13.5M | if (*fmt == '-') |
238 | 0 | adjust = LEFT; |
239 | 13.5M | else if (*fmt == '+') |
240 | 0 | print_sign = true; |
241 | 13.5M | else if (*fmt == '#') |
242 | 0 | alternate_form = true; |
243 | 13.5M | else if (*fmt == ' ') |
244 | 0 | print_blank = true; |
245 | 13.5M | else if (*fmt == '0') |
246 | 40.8k | pad_char = '0'; |
247 | 13.5M | else |
248 | 13.5M | break; |
249 | 13.5M | } |
250 | | |
251 | | /* |
252 | | * Check if a width was specified |
253 | | */ |
254 | 13.5M | if (isdigit((unsigned char)*fmt)) { |
255 | 53.7k | STR_TO_DEC(fmt, min_width); |
256 | 53.7k | adjust_width = true; |
257 | 13.4M | } else if (*fmt == '*') { |
258 | 4.90M | min_width = va_arg(ap, int); |
259 | 4.90M | fmt++; |
260 | 4.90M | adjust_width = true; |
261 | 4.90M | if (min_width < 0) { |
262 | 0 | adjust = LEFT; |
263 | 0 | min_width = -min_width; |
264 | 0 | } |
265 | 4.90M | } else |
266 | 8.54M | adjust_width = false; |
267 | | |
268 | | /* |
269 | | * Check if a precision was specified |
270 | | */ |
271 | 13.5M | if (*fmt == '.') { |
272 | 1.80M | adjust_precision = true; |
273 | 1.80M | fmt++; |
274 | 1.80M | if (isdigit((unsigned char)*fmt)) { |
275 | 462 | STR_TO_DEC(fmt, precision); |
276 | 1.80M | } else if (*fmt == '*') { |
277 | 1.80M | precision = va_arg(ap, int); |
278 | 1.80M | fmt++; |
279 | 1.80M | if (precision < -1) |
280 | 0 | precision = -1; |
281 | 1.80M | } else |
282 | 0 | precision = 0; |
283 | 1.80M | } else |
284 | 11.6M | adjust_precision = false; |
285 | 13.5M | } else |
286 | 13.8M | adjust_precision = adjust_width = false; |
287 | | |
288 | | /* |
289 | | * Modifier check |
290 | | */ |
291 | 27.3M | switch (*fmt) { |
292 | 0 | case 'L': |
293 | 0 | fmt++; |
294 | 0 | modifier = LM_LONG_DOUBLE; |
295 | 0 | break; |
296 | 2.81M | case 'l': |
297 | 2.81M | fmt++; |
298 | 2.81M | #if SIZEOF_LONG_LONG |
299 | 2.81M | if (*fmt == 'l') { |
300 | 0 | fmt++; |
301 | 0 | modifier = LM_LONG_LONG; |
302 | 0 | } else |
303 | 2.81M | #endif |
304 | 2.81M | modifier = LM_LONG; |
305 | 2.81M | break; |
306 | 391k | case 'z': |
307 | 391k | fmt++; |
308 | 391k | modifier = LM_SIZE_T; |
309 | 391k | break; |
310 | 0 | case 'j': |
311 | 0 | fmt++; |
312 | 0 | modifier = LM_INTMAX_T; |
313 | 0 | break; |
314 | 0 | case 't': |
315 | 0 | fmt++; |
316 | 0 | modifier = LM_PTRDIFF_T; |
317 | 0 | break; |
318 | 121k | case 'p': |
319 | 121k | { |
320 | 121k | char __next = *(fmt+1); |
321 | 121k | if ('d' == __next || 'u' == __next || 'x' == __next || 'o' == __next) { |
322 | 0 | zend_error_noreturn(E_CORE_ERROR, |
323 | 0 | "printf \"p\" modifier is no longer supported, use ZEND_LONG_FMT"); |
324 | 0 | } |
325 | 121k | modifier = LM_STD; |
326 | 121k | break; |
327 | 0 | } |
328 | 0 | case 'h': |
329 | 0 | fmt++; |
330 | 0 | if (*fmt == 'h') { |
331 | 0 | fmt++; |
332 | 0 | } |
333 | | /* these are promoted to int, so no break */ |
334 | 0 | ZEND_FALLTHROUGH; |
335 | 23.9M | default: |
336 | 23.9M | modifier = LM_STD; |
337 | 23.9M | break; |
338 | 27.3M | } |
339 | | |
340 | | /* |
341 | | * Argument extraction and printing. |
342 | | * First we determine the argument type. |
343 | | * Then, we convert the argument to a string. |
344 | | * On exit from the switch, s points to the string that |
345 | | * must be printed, s_len has the length of the string |
346 | | * The precision requirements, if any, are reflected in s_len. |
347 | | * |
348 | | * NOTE: pad_char may be set to '0' because of the 0 flag. |
349 | | * It is reset to ' ' by non-numeric formats |
350 | | */ |
351 | 27.3M | switch (*fmt) { |
352 | 0 | case 'Z': { |
353 | 0 | zval *zvp = va_arg(ap, zval*); |
354 | 0 | zend_string *str = zval_get_tmp_string(zvp, &tmp_str); |
355 | 0 | s_len = ZSTR_LEN(str); |
356 | 0 | s = ZSTR_VAL(str); |
357 | 0 | if (adjust_precision && (size_t)precision < s_len) { |
358 | 0 | s_len = precision; |
359 | 0 | } |
360 | 0 | break; |
361 | 0 | } |
362 | 6.74M | case 'S': { |
363 | 6.86M | format_zend_string:; |
364 | 6.86M | zend_string *str = va_arg(ap, zend_string*); |
365 | 6.86M | s_len = ZSTR_LEN(str); |
366 | 6.86M | s = ZSTR_VAL(str); |
367 | 6.86M | if (adjust_precision && (size_t)precision < s_len) { |
368 | 0 | s_len = precision; |
369 | 0 | } |
370 | 6.86M | break; |
371 | 6.74M | } |
372 | 2.31M | case 'u': |
373 | 2.31M | switch(modifier) { |
374 | 2.31M | default: |
375 | 2.31M | i_num = (int64_t) va_arg(ap, unsigned int); |
376 | 2.31M | break; |
377 | 0 | case LM_LONG_DOUBLE: |
378 | 0 | goto fmt_error; |
379 | 0 | case LM_LONG: |
380 | 0 | i_num = (int64_t) va_arg(ap, unsigned long int); |
381 | 0 | break; |
382 | 263 | case LM_SIZE_T: |
383 | 263 | i_num = (int64_t) va_arg(ap, size_t); |
384 | 263 | break; |
385 | 0 | #if SIZEOF_LONG_LONG |
386 | 0 | case LM_LONG_LONG: |
387 | 0 | i_num = (int64_t) va_arg(ap, unsigned long long int); |
388 | 0 | break; |
389 | 0 | #endif |
390 | 0 | case LM_INTMAX_T: |
391 | 0 | i_num = (int64_t) va_arg(ap, uintmax_t); |
392 | 0 | break; |
393 | 0 | case LM_PTRDIFF_T: |
394 | 0 | i_num = (int64_t) va_arg(ap, ptrdiff_t); |
395 | 0 | break; |
396 | 2.31M | } |
397 | | /* |
398 | | * The rest also applies to other integer formats, so fall |
399 | | * into that case. |
400 | | */ |
401 | 2.31M | ZEND_FALLTHROUGH; |
402 | 5.64M | case 'd': |
403 | 5.64M | case 'i': |
404 | | /* |
405 | | * Get the arg if we haven't already. |
406 | | */ |
407 | 5.64M | if ((*fmt) != 'u') { |
408 | 3.33M | switch(modifier) { |
409 | 130k | default: |
410 | 130k | i_num = (int64_t) va_arg(ap, int); |
411 | 130k | break; |
412 | 0 | case LM_LONG_DOUBLE: |
413 | 0 | goto fmt_error; |
414 | 2.81M | case LM_LONG: |
415 | 2.81M | i_num = (int64_t) va_arg(ap, long int); |
416 | 2.81M | break; |
417 | 384k | case LM_SIZE_T: |
418 | 384k | i_num = (int64_t) va_arg(ap, ssize_t); |
419 | 384k | break; |
420 | 0 | #if SIZEOF_LONG_LONG |
421 | 0 | case LM_LONG_LONG: |
422 | 0 | i_num = (int64_t) va_arg(ap, long long int); |
423 | 0 | break; |
424 | 0 | #endif |
425 | 0 | case LM_INTMAX_T: |
426 | 0 | i_num = (int64_t) va_arg(ap, intmax_t); |
427 | 0 | break; |
428 | 0 | case LM_PTRDIFF_T: |
429 | 0 | i_num = (int64_t) va_arg(ap, ptrdiff_t); |
430 | 0 | break; |
431 | 3.33M | } |
432 | 3.33M | } |
433 | 5.64M | s = ap_php_conv_10(i_num, (*fmt) == 'u', &is_negative, |
434 | 5.64M | &num_buf[NUM_BUF_SIZE], &s_len); |
435 | 5.64M | FIX_PRECISION(adjust_precision, precision, s, s_len); |
436 | | |
437 | 5.64M | if (*fmt != 'u') { |
438 | 3.33M | if (is_negative) |
439 | 101k | prefix_char = '-'; |
440 | 3.23M | else if (print_sign) |
441 | 0 | prefix_char = '+'; |
442 | 3.23M | else if (print_blank) |
443 | 0 | prefix_char = ' '; |
444 | 3.33M | } |
445 | 5.64M | break; |
446 | | |
447 | | |
448 | 0 | case 'o': |
449 | 0 | switch(modifier) { |
450 | 0 | default: |
451 | 0 | ui_num = (uint64_t) va_arg(ap, unsigned int); |
452 | 0 | break; |
453 | 0 | case LM_LONG_DOUBLE: |
454 | 0 | goto fmt_error; |
455 | 0 | case LM_LONG: |
456 | 0 | ui_num = (uint64_t) va_arg(ap, unsigned long int); |
457 | 0 | break; |
458 | 0 | case LM_SIZE_T: |
459 | 0 | ui_num = (uint64_t) va_arg(ap, size_t); |
460 | 0 | break; |
461 | 0 | #if SIZEOF_LONG_LONG |
462 | 0 | case LM_LONG_LONG: |
463 | 0 | ui_num = (uint64_t) va_arg(ap, unsigned long long int); |
464 | 0 | break; |
465 | 0 | #endif |
466 | 0 | case LM_INTMAX_T: |
467 | 0 | ui_num = (uint64_t) va_arg(ap, uintmax_t); |
468 | 0 | break; |
469 | 0 | case LM_PTRDIFF_T: |
470 | 0 | ui_num = (uint64_t) va_arg(ap, ptrdiff_t); |
471 | 0 | break; |
472 | 0 | } |
473 | 0 | s = ap_php_conv_p2(ui_num, 3, *fmt, |
474 | 0 | &num_buf[NUM_BUF_SIZE], &s_len); |
475 | 0 | FIX_PRECISION(adjust_precision, precision, s, s_len); |
476 | 0 | if (alternate_form && *s != '0') { |
477 | 0 | *--s = '0'; |
478 | 0 | s_len++; |
479 | 0 | } |
480 | 0 | break; |
481 | | |
482 | | |
483 | 308k | case 'x': |
484 | 348k | case 'X': |
485 | 348k | switch(modifier) { |
486 | 342k | default: |
487 | 342k | ui_num = (uint64_t) va_arg(ap, unsigned int); |
488 | 342k | break; |
489 | 0 | case LM_LONG_DOUBLE: |
490 | 0 | goto fmt_error; |
491 | 0 | case LM_LONG: |
492 | 0 | ui_num = (uint64_t) va_arg(ap, unsigned long int); |
493 | 0 | break; |
494 | 6.09k | case LM_SIZE_T: |
495 | 6.09k | ui_num = (uint64_t) va_arg(ap, size_t); |
496 | 6.09k | break; |
497 | 0 | #if SIZEOF_LONG_LONG |
498 | 0 | case LM_LONG_LONG: |
499 | 0 | ui_num = (uint64_t) va_arg(ap, unsigned long long int); |
500 | 0 | break; |
501 | 0 | #endif |
502 | 0 | case LM_INTMAX_T: |
503 | 0 | ui_num = (uint64_t) va_arg(ap, uintmax_t); |
504 | 0 | break; |
505 | 0 | case LM_PTRDIFF_T: |
506 | 0 | ui_num = (uint64_t) va_arg(ap, ptrdiff_t); |
507 | 0 | break; |
508 | 348k | } |
509 | 348k | s = ap_php_conv_p2(ui_num, 4, *fmt, |
510 | 348k | &num_buf[NUM_BUF_SIZE], &s_len); |
511 | 348k | FIX_PRECISION(adjust_precision, precision, s, s_len); |
512 | 348k | if (alternate_form && ui_num != 0) { |
513 | 0 | *--s = *fmt; /* 'x' or 'X' */ |
514 | 0 | *--s = '0'; |
515 | 0 | s_len += 2; |
516 | 0 | } |
517 | 348k | break; |
518 | | |
519 | | |
520 | 7.32M | case 's': |
521 | 7.32M | s = va_arg(ap, char *); |
522 | 7.32M | if (s != NULL) { |
523 | 7.32M | if (!adjust_precision) { |
524 | 7.32M | s_len = strlen(s); |
525 | 7.32M | } else { |
526 | 19 | s_len = zend_strnlen(s, precision); |
527 | 19 | } |
528 | 7.32M | } else { |
529 | 0 | s = S_NULL; |
530 | 0 | s_len = S_NULL_LEN; |
531 | 0 | } |
532 | 7.32M | pad_char = ' '; |
533 | 7.32M | break; |
534 | | |
535 | | |
536 | 10 | case 'f': |
537 | 462 | case 'F': |
538 | 462 | case 'e': |
539 | 462 | case 'E': |
540 | 462 | switch(modifier) { |
541 | 0 | case LM_LONG_DOUBLE: |
542 | 0 | fp_num = (double) va_arg(ap, long double); |
543 | 0 | break; |
544 | 462 | case LM_STD: |
545 | 462 | fp_num = va_arg(ap, double); |
546 | 462 | break; |
547 | 0 | default: |
548 | 0 | goto fmt_error; |
549 | 462 | } |
550 | | |
551 | 462 | if (zend_isnan(fp_num)) { |
552 | 4 | s = "nan"; |
553 | 4 | s_len = 3; |
554 | 458 | } else if (zend_isinf(fp_num)) { |
555 | 12 | s = "inf"; |
556 | 12 | s_len = 3; |
557 | 446 | } else { |
558 | 446 | #ifndef ZTS |
559 | 446 | if (!lconv) { |
560 | 438 | lconv = localeconv(); |
561 | 438 | } |
562 | 446 | #endif |
563 | 446 | s = php_conv_fp((*fmt == 'f')?'F':*fmt, fp_num, alternate_form, |
564 | 446 | (adjust_precision == false) ? FLOAT_DIGITS : precision, |
565 | 446 | (*fmt == 'f')?LCONV_DECIMAL_POINT:'.', |
566 | 446 | &is_negative, &num_buf[1], &s_len); |
567 | 446 | if (is_negative) |
568 | 51 | prefix_char = '-'; |
569 | 395 | else if (print_sign) |
570 | 0 | prefix_char = '+'; |
571 | 395 | else if (print_blank) |
572 | 0 | prefix_char = ' '; |
573 | 446 | } |
574 | 462 | break; |
575 | | |
576 | | |
577 | 0 | case 'g': |
578 | 0 | case 'k': |
579 | 0 | case 'G': |
580 | 1.80M | case 'H': |
581 | 1.80M | switch(modifier) { |
582 | 0 | case LM_LONG_DOUBLE: |
583 | 0 | fp_num = (double) va_arg(ap, long double); |
584 | 0 | break; |
585 | 1.80M | case LM_STD: |
586 | 1.80M | fp_num = va_arg(ap, double); |
587 | 1.80M | break; |
588 | 0 | default: |
589 | 0 | goto fmt_error; |
590 | 1.80M | } |
591 | | |
592 | 1.80M | if (zend_isnan(fp_num)) { |
593 | 19.3k | s = "NAN"; |
594 | 19.3k | s_len = 3; |
595 | 19.3k | break; |
596 | 1.78M | } else if (zend_isinf(fp_num)) { |
597 | 2.80k | if (fp_num > 0) { |
598 | 2.22k | s = "INF"; |
599 | 2.22k | s_len = 3; |
600 | 2.22k | } else { |
601 | 581 | s = "-INF"; |
602 | 581 | s_len = 4; |
603 | 581 | } |
604 | 2.80k | break; |
605 | 2.80k | } |
606 | | |
607 | 1.78M | if (adjust_precision == false) |
608 | 0 | precision = FLOAT_DIGITS; |
609 | 1.78M | else if (precision == 0) |
610 | 0 | precision = 1; |
611 | | /* |
612 | | * * We use &num_buf[ 1 ], so that we have room for the sign |
613 | | */ |
614 | 1.78M | #ifndef ZTS |
615 | 1.78M | if (!lconv) { |
616 | 1.78M | lconv = localeconv(); |
617 | 1.78M | } |
618 | 1.78M | #endif |
619 | 1.78M | s = zend_gcvt(fp_num, precision, (*fmt=='H' || *fmt == 'k') ? '.' : LCONV_DECIMAL_POINT, (*fmt == 'G' || *fmt == 'H')?'E':'e', &num_buf[1]); |
620 | 1.78M | if (*s == '-') |
621 | 64.3k | prefix_char = *s++; |
622 | 1.71M | else if (print_sign) |
623 | 0 | prefix_char = '+'; |
624 | 1.71M | else if (print_blank) |
625 | 0 | prefix_char = ' '; |
626 | | |
627 | 1.78M | s_len = strlen(s); |
628 | | |
629 | 1.78M | if (alternate_form && (strchr(s, '.')) == NULL) |
630 | 0 | s[s_len++] = '.'; |
631 | 1.78M | break; |
632 | | |
633 | | |
634 | 5.32M | case 'c': |
635 | 5.32M | char_buf[0] = (char) (va_arg(ap, int)); |
636 | 5.32M | s = &char_buf[0]; |
637 | 5.32M | s_len = 1; |
638 | 5.32M | pad_char = ' '; |
639 | 5.32M | break; |
640 | | |
641 | | |
642 | 0 | case '%': |
643 | 0 | char_buf[0] = '%'; |
644 | 0 | s = &char_buf[0]; |
645 | 0 | s_len = 1; |
646 | 0 | pad_char = ' '; |
647 | 0 | break; |
648 | | |
649 | | |
650 | 0 | case 'n': |
651 | 0 | *(va_arg(ap, int *)) = is_char? (int)((smart_string *)xbuf)->len : (int)ZSTR_LEN(((smart_str *)xbuf)->s); |
652 | 0 | goto skip_output; |
653 | | |
654 | | /* |
655 | | * Always extract the argument as a "char *" pointer. We |
656 | | * should be using "void *" but there are still machines |
657 | | * that don't understand it. |
658 | | * If the pointer size is equal to the size of an unsigned |
659 | | * integer we convert the pointer to a hex number, otherwise |
660 | | * we print "%p" to indicate that we don't handle "%p". |
661 | | */ |
662 | 121k | case 'p': |
663 | | /* %p[alnum]+ extensions */ |
664 | 121k | switch (*(fmt+1)) { |
665 | 121k | case 'S': |
666 | | /* zend_string* */ |
667 | 121k | fmt++; |
668 | 121k | goto format_zend_string; |
669 | 0 | case 'p': |
670 | | /* pointer */ |
671 | 0 | fmt++; |
672 | 0 | break; |
673 | 0 | default: |
674 | 0 | if (isalnum(*(fmt+1))) { |
675 | 0 | zend_error_noreturn(E_CORE_ERROR, |
676 | 0 | "Invalid printf specifier \"p%c\"", *(fmt+1)); |
677 | 0 | } |
678 | 0 | break; |
679 | 121k | } |
680 | | /* Normal %p */ |
681 | 0 | if (sizeof(char *) <= sizeof(uint64_t)) { |
682 | 0 | ui_num = (uint64_t)((size_t) va_arg(ap, char *)); |
683 | 0 | s = ap_php_conv_p2(ui_num, 4, 'x', |
684 | 0 | &num_buf[NUM_BUF_SIZE], &s_len); |
685 | 0 | if (ui_num != 0) { |
686 | 0 | *--s = 'x'; |
687 | 0 | *--s = '0'; |
688 | 0 | s_len += 2; |
689 | 0 | } |
690 | 0 | } else { |
691 | 0 | s = "%p"; |
692 | 0 | s_len = 2; |
693 | 0 | } |
694 | 0 | pad_char = ' '; |
695 | 0 | break; |
696 | | |
697 | | |
698 | 0 | case NUL: |
699 | | /* |
700 | | * The last character of the format string was %. |
701 | | * We ignore it. |
702 | | */ |
703 | 0 | continue; |
704 | | |
705 | | |
706 | 0 | fmt_error: |
707 | 0 | php_error(E_ERROR, "Illegal length modifier specified '%c' in s[np]printf call", *fmt); |
708 | | /* |
709 | | * The default case is for unrecognized %'s. |
710 | | * We print %<char> to help the user identify what |
711 | | * option is not understood. |
712 | | * This is also useful in case the user wants to pass |
713 | | * the output of format_converter to another function |
714 | | * that understands some other %<char> (like syslog). |
715 | | * Note that we can't point s inside fmt because the |
716 | | * unknown <char> could be preceded by width etc. |
717 | | */ |
718 | 0 | ZEND_FALLTHROUGH; |
719 | 0 | default: |
720 | 0 | char_buf[0] = '%'; |
721 | 0 | char_buf[1] = *fmt; |
722 | 0 | s = char_buf; |
723 | 0 | s_len = 2; |
724 | 0 | pad_char = ' '; |
725 | 0 | break; |
726 | 27.3M | } |
727 | | |
728 | 27.3M | if (prefix_char != NUL) { |
729 | 165k | *--s = prefix_char; |
730 | 165k | s_len++; |
731 | 165k | } |
732 | 27.3M | if (adjust_width && adjust == RIGHT && (size_t)min_width > s_len) { |
733 | 4.85M | if (pad_char == '0' && prefix_char != NUL) { |
734 | 0 | INS_CHAR(xbuf, *s, is_char); |
735 | 0 | s++; |
736 | 0 | s_len--; |
737 | 0 | min_width--; |
738 | 0 | } |
739 | 4.85M | PAD_CHAR(xbuf, pad_char, min_width - s_len, is_char); |
740 | 4.85M | } |
741 | | /* |
742 | | * Print the string s. |
743 | | */ |
744 | 27.3M | INS_STRING(xbuf, s, s_len, is_char); |
745 | | |
746 | 27.3M | if (adjust_width && adjust == LEFT && (size_t)min_width > s_len) { |
747 | 0 | PAD_CHAR(xbuf, pad_char, min_width - s_len, is_char); |
748 | 0 | } |
749 | | |
750 | 27.3M | zend_tmp_string_release(tmp_str); |
751 | 27.3M | } |
752 | 173M | skip_output: |
753 | 173M | fmt++; |
754 | 173M | } |
755 | 12.3M | return; |
756 | 12.3M | } |
757 | | /* }}} */ |
758 | | |
759 | | PHPAPI void php_printf_to_smart_string(smart_string *buf, const char *format, va_list ap) /* {{{ */ |
760 | 7.66M | { |
761 | 7.66M | xbuf_format_converter(buf, 1, format, ap); |
762 | 7.66M | } |
763 | | /* }}} */ |
764 | | |
765 | | PHPAPI void php_printf_to_smart_str(smart_str *buf, const char *format, va_list ap) /* {{{ */ |
766 | 4.72M | { |
767 | 4.72M | xbuf_format_converter(buf, 0, format, ap); |
768 | 4.72M | } |
769 | | /* }}} */ |