/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 (*lconv.decimal_point) |
92 | | #else |
93 | 413k | #define LCONV_DECIMAL_POINT (*lconv->decimal_point) |
94 | | #endif |
95 | | |
96 | | #include "snprintf.h" |
97 | | |
98 | 68.1M | #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 | 278M | #define INS_CHAR(xbuf, ch, is_char) do { \ |
113 | 278M | if ((is_char)) { \ |
114 | 132M | smart_string_appendc((smart_string *)(xbuf), (ch)); \ |
115 | 145M | } else { \ |
116 | 145M | smart_str_appendc((smart_str *)(xbuf), (ch)); \ |
117 | 145M | } \ |
118 | 278M | } while (0); |
119 | | |
120 | 34.0M | #define INS_STRING(xbuf, str, len, is_char) do { \ |
121 | 34.0M | if ((is_char)) { \ |
122 | 20.1M | smart_string_appendl((smart_string *)(xbuf), (str), (len)); \ |
123 | 20.1M | } else { \ |
124 | 13.8M | smart_str_appendl((smart_str *)(xbuf), (str), (len)); \ |
125 | 13.8M | } \ |
126 | 34.0M | } while (0); |
127 | | |
128 | 7.56M | #define PAD_CHAR(xbuf, ch, count, is_char) do { \ |
129 | 7.56M | if ((is_char)) { \ |
130 | 7.54M | smart_string_alloc(((smart_string *)(xbuf)), (count), 0); \ |
131 | 7.54M | memset(((smart_string *)(xbuf))->c + ((smart_string *)(xbuf))->len, (ch), (count)); \ |
132 | 7.54M | ((smart_string *)(xbuf))->len += (count); \ |
133 | 7.54M | } else { \ |
134 | 20.8k | smart_str_alloc(((smart_str *)(xbuf)), (count), 0); \ |
135 | 20.8k | memset(ZSTR_VAL(((smart_str *)(xbuf))->s) + ZSTR_LEN(((smart_str *)(xbuf))->s), (ch), (count)); \ |
136 | 20.8k | ZSTR_LEN(((smart_str *)(xbuf))->s) += (count); \ |
137 | 20.8k | } \ |
138 | 7.56M | } 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 | 7.35M | #define NUM_BUF_SIZE ZEND_DOUBLE_MAX_LENGTH |
145 | | |
146 | 71.7k | #define NUM(c) (c - '0') |
147 | | |
148 | 56.9k | #define STR_TO_DEC(str, num) do { \ |
149 | 56.9k | num = NUM(*(str)++); \ |
150 | 56.9k | while (isdigit((unsigned char)*(str))) {\ |
151 | 14.7k | num *= 10; \ |
152 | 14.7k | num += NUM(*(str)++); \ |
153 | 14.7k | if (num >= INT_MAX / 10) { \ |
154 | 0 | while (isdigit((unsigned char)*(str)++)); \ |
155 | 0 | break; \ |
156 | 0 | } \ |
157 | 14.7k | } \ |
158 | 56.9k | } 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 | 7.35M | #define FIX_PRECISION(adjust, precision, s, s_len) do { \ |
167 | 7.35M | if (adjust) \ |
168 | 7.35M | while (s_len < (size_t)precision) { \ |
169 | 0 | *--s = '0'; \ |
170 | 0 | s_len++; \ |
171 | 0 | } \ |
172 | 7.35M | } 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 | 23.8M | { |
181 | 23.8M | char *s = NULL; |
182 | 23.8M | size_t s_len; |
183 | | |
184 | 23.8M | int min_width = 0; |
185 | 23.8M | int precision = 0; |
186 | 23.8M | enum { |
187 | 23.8M | LEFT, RIGHT |
188 | 23.8M | } adjust; |
189 | 23.8M | char pad_char; |
190 | 23.8M | char prefix_char; |
191 | | |
192 | 23.8M | double fp_num; |
193 | 23.8M | int64_t i_num = (int64_t) 0; |
194 | 23.8M | uint64_t ui_num = (uint64_t) 0; |
195 | | |
196 | 23.8M | char num_buf[NUM_BUF_SIZE]; |
197 | 23.8M | char char_buf[2]; /* for printing %% and %<unknown> */ |
198 | | |
199 | | #ifdef ZTS |
200 | | struct lconv lconv; |
201 | | #else |
202 | 23.8M | struct lconv *lconv = NULL; |
203 | 23.8M | #endif |
204 | | |
205 | | /* |
206 | | * Flag variables |
207 | | */ |
208 | 23.8M | length_modifier_e modifier; |
209 | 23.8M | bool alternate_form; |
210 | 23.8M | bool print_sign; |
211 | 23.8M | bool print_blank; |
212 | 23.8M | bool adjust_precision; |
213 | 23.8M | bool adjust_width; |
214 | 23.8M | bool is_negative; |
215 | | |
216 | 335M | while (*fmt) { |
217 | 312M | if (*fmt != '%') { |
218 | 278M | INS_CHAR(xbuf, *fmt, is_char); |
219 | 278M | } else { |
220 | | /* |
221 | | * Default variable settings |
222 | | */ |
223 | 34.0M | zend_string *tmp_str = NULL; |
224 | 34.0M | adjust = RIGHT; |
225 | 34.0M | alternate_form = print_sign = print_blank = false; |
226 | 34.0M | pad_char = ' '; |
227 | 34.0M | prefix_char = NUL; |
228 | | |
229 | 34.0M | fmt++; |
230 | | |
231 | | /* |
232 | | * Try to avoid checking for flags, width or precision |
233 | | */ |
234 | 34.0M | if (isascii((unsigned char)*fmt) && !islower((unsigned char)*fmt)) { |
235 | | /* |
236 | | * Recognize flags: -, #, BLANK, + |
237 | | */ |
238 | 14.2M | for (;; fmt++) { |
239 | 14.2M | if (*fmt == '-') |
240 | 0 | adjust = LEFT; |
241 | 14.2M | else if (*fmt == '+') |
242 | 0 | print_sign = true; |
243 | 14.2M | else if (*fmt == '#') |
244 | 0 | alternate_form = true; |
245 | 14.2M | else if (*fmt == ' ') |
246 | 0 | print_blank = true; |
247 | 14.2M | else if (*fmt == '0') |
248 | 42.5k | pad_char = '0'; |
249 | 14.2M | else |
250 | 14.2M | break; |
251 | 14.2M | } |
252 | | |
253 | | /* |
254 | | * Check if a width was specified |
255 | | */ |
256 | 14.2M | if (isdigit((unsigned char)*fmt)) { |
257 | 56.3k | STR_TO_DEC(fmt, min_width); |
258 | 56.3k | adjust_width = true; |
259 | 14.1M | } else if (*fmt == '*') { |
260 | 7.61M | min_width = va_arg(ap, int); |
261 | 7.61M | fmt++; |
262 | 7.61M | adjust_width = true; |
263 | 7.61M | if (min_width < 0) { |
264 | 0 | adjust = LEFT; |
265 | 0 | min_width = -min_width; |
266 | 0 | } |
267 | 7.61M | } else |
268 | 6.57M | adjust_width = false; |
269 | | |
270 | | /* |
271 | | * Check if a precision was specified |
272 | | */ |
273 | 14.2M | if (*fmt == '.') { |
274 | 447k | adjust_precision = true; |
275 | 447k | fmt++; |
276 | 447k | if (isdigit((unsigned char)*fmt)) { |
277 | 604 | STR_TO_DEC(fmt, precision); |
278 | 446k | } else if (*fmt == '*') { |
279 | 446k | precision = va_arg(ap, int); |
280 | 446k | fmt++; |
281 | 446k | if (precision < -1) |
282 | 0 | precision = -1; |
283 | 446k | } else |
284 | 0 | precision = 0; |
285 | 447k | } else |
286 | 13.7M | adjust_precision = false; |
287 | 14.2M | } else |
288 | 19.8M | adjust_precision = adjust_width = false; |
289 | | |
290 | | /* |
291 | | * Modifier check |
292 | | */ |
293 | 34.0M | switch (*fmt) { |
294 | 0 | case 'L': |
295 | 0 | fmt++; |
296 | 0 | modifier = LM_LONG_DOUBLE; |
297 | 0 | break; |
298 | 4.56M | case 'l': |
299 | 4.56M | fmt++; |
300 | 4.56M | #if SIZEOF_LONG_LONG |
301 | 4.56M | if (*fmt == 'l') { |
302 | 0 | fmt++; |
303 | 0 | modifier = LM_LONG_LONG; |
304 | 0 | } else |
305 | 4.56M | #endif |
306 | 4.56M | modifier = LM_LONG; |
307 | 4.56M | break; |
308 | 508k | case 'z': |
309 | 508k | fmt++; |
310 | 508k | modifier = LM_SIZE_T; |
311 | 508k | break; |
312 | 0 | case 'j': |
313 | 0 | fmt++; |
314 | 0 | modifier = LM_INTMAX_T; |
315 | 0 | break; |
316 | 0 | case 't': |
317 | 0 | fmt++; |
318 | 0 | modifier = LM_PTRDIFF_T; |
319 | 0 | break; |
320 | 151k | case 'p': |
321 | 151k | { |
322 | 151k | char __next = *(fmt+1); |
323 | 151k | if ('d' == __next || 'u' == __next || 'x' == __next || 'o' == __next) { |
324 | 0 | zend_error_noreturn(E_CORE_ERROR, |
325 | 0 | "printf \"p\" modifier is no longer supported, use ZEND_LONG_FMT"); |
326 | 0 | } |
327 | 151k | modifier = LM_STD; |
328 | 151k | break; |
329 | 151k | } |
330 | 0 | case 'h': |
331 | 0 | fmt++; |
332 | 0 | if (*fmt == 'h') { |
333 | 0 | fmt++; |
334 | 0 | } |
335 | | /* these are promoted to int, so no break */ |
336 | 0 | ZEND_FALLTHROUGH; |
337 | 28.8M | default: |
338 | 28.8M | modifier = LM_STD; |
339 | 28.8M | break; |
340 | 34.0M | } |
341 | | |
342 | | /* |
343 | | * Argument extraction and printing. |
344 | | * First we determine the argument type. |
345 | | * Then, we convert the argument to a string. |
346 | | * On exit from the switch, s points to the string that |
347 | | * must be printed, s_len has the length of the string |
348 | | * The precision requirements, if any, are reflected in s_len. |
349 | | * |
350 | | * NOTE: pad_char may be set to '0' because of the 0 flag. |
351 | | * It is reset to ' ' by non-numeric formats |
352 | | */ |
353 | 34.0M | switch (*fmt) { |
354 | 0 | case 'Z': { |
355 | 0 | zval *zvp = va_arg(ap, zval*); |
356 | 0 | zend_string *str = zval_get_tmp_string(zvp, &tmp_str); |
357 | 0 | s_len = ZSTR_LEN(str); |
358 | 0 | s = ZSTR_VAL(str); |
359 | 0 | if (adjust_precision && (size_t)precision < s_len) { |
360 | 0 | s_len = precision; |
361 | 0 | } |
362 | 0 | break; |
363 | 0 | } |
364 | 6.13M | case 'S': { |
365 | 6.28M | format_zend_string:; |
366 | 6.28M | zend_string *str = va_arg(ap, zend_string*); |
367 | 6.28M | s_len = ZSTR_LEN(str); |
368 | 6.28M | s = ZSTR_VAL(str); |
369 | 6.28M | if (adjust_precision && (size_t)precision < s_len) { |
370 | 0 | s_len = precision; |
371 | 0 | } |
372 | 6.28M | break; |
373 | 6.13M | } |
374 | 1.56M | case 'u': |
375 | 1.56M | switch(modifier) { |
376 | 1.56M | default: |
377 | 1.56M | i_num = (int64_t) va_arg(ap, unsigned int); |
378 | 1.56M | break; |
379 | 0 | case LM_LONG_DOUBLE: |
380 | 0 | goto fmt_error; |
381 | 29 | case LM_LONG: |
382 | 29 | i_num = (int64_t) va_arg(ap, unsigned long int); |
383 | 29 | break; |
384 | 3.02k | case LM_SIZE_T: |
385 | 3.02k | i_num = (int64_t) va_arg(ap, size_t); |
386 | 3.02k | break; |
387 | 0 | #if SIZEOF_LONG_LONG |
388 | 0 | case LM_LONG_LONG: |
389 | 0 | i_num = (int64_t) va_arg(ap, unsigned long long int); |
390 | 0 | break; |
391 | 0 | #endif |
392 | 0 | case LM_INTMAX_T: |
393 | 0 | i_num = (int64_t) va_arg(ap, uintmax_t); |
394 | 0 | break; |
395 | 0 | case LM_PTRDIFF_T: |
396 | 0 | i_num = (int64_t) va_arg(ap, ptrdiff_t); |
397 | 0 | break; |
398 | 1.56M | } |
399 | | /* |
400 | | * The rest also applies to other integer formats, so fall |
401 | | * into that case. |
402 | | */ |
403 | 1.56M | ZEND_FALLTHROUGH; |
404 | 7.14M | case 'd': |
405 | 7.14M | case 'i': |
406 | | /* |
407 | | * Get the arg if we haven't already. |
408 | | */ |
409 | 7.14M | if ((*fmt) != 'u') { |
410 | 5.57M | switch(modifier) { |
411 | 512k | default: |
412 | 512k | i_num = (int64_t) va_arg(ap, int); |
413 | 512k | break; |
414 | 0 | case LM_LONG_DOUBLE: |
415 | 0 | goto fmt_error; |
416 | 4.56M | case LM_LONG: |
417 | 4.56M | i_num = (int64_t) va_arg(ap, long int); |
418 | 4.56M | break; |
419 | 498k | case LM_SIZE_T: |
420 | 498k | i_num = (int64_t) va_arg(ap, ssize_t); |
421 | 498k | break; |
422 | 0 | #if SIZEOF_LONG_LONG |
423 | 0 | case LM_LONG_LONG: |
424 | 0 | i_num = (int64_t) va_arg(ap, long long int); |
425 | 0 | break; |
426 | 0 | #endif |
427 | 0 | case LM_INTMAX_T: |
428 | 0 | i_num = (int64_t) va_arg(ap, intmax_t); |
429 | 0 | break; |
430 | 0 | case LM_PTRDIFF_T: |
431 | 0 | i_num = (int64_t) va_arg(ap, ptrdiff_t); |
432 | 0 | break; |
433 | 5.57M | } |
434 | 5.57M | } |
435 | 7.14M | s = ap_php_conv_10(i_num, (*fmt) == 'u', &is_negative, |
436 | 7.14M | &num_buf[NUM_BUF_SIZE], &s_len); |
437 | 7.14M | FIX_PRECISION(adjust_precision, precision, s, s_len); |
438 | | |
439 | 7.14M | if (*fmt != 'u') { |
440 | 5.57M | if (is_negative) |
441 | 73.0k | prefix_char = '-'; |
442 | 5.50M | else if (print_sign) |
443 | 0 | prefix_char = '+'; |
444 | 5.50M | else if (print_blank) |
445 | 0 | prefix_char = ' '; |
446 | 5.57M | } |
447 | 7.14M | break; |
448 | | |
449 | | |
450 | 0 | case 'o': |
451 | 0 | switch(modifier) { |
452 | 0 | default: |
453 | 0 | ui_num = (uint64_t) va_arg(ap, unsigned int); |
454 | 0 | break; |
455 | 0 | case LM_LONG_DOUBLE: |
456 | 0 | goto fmt_error; |
457 | 0 | case LM_LONG: |
458 | 0 | ui_num = (uint64_t) va_arg(ap, unsigned long int); |
459 | 0 | break; |
460 | 0 | case LM_SIZE_T: |
461 | 0 | ui_num = (uint64_t) va_arg(ap, size_t); |
462 | 0 | break; |
463 | 0 | #if SIZEOF_LONG_LONG |
464 | 0 | case LM_LONG_LONG: |
465 | 0 | ui_num = (uint64_t) va_arg(ap, unsigned long long int); |
466 | 0 | break; |
467 | 0 | #endif |
468 | 0 | case LM_INTMAX_T: |
469 | 0 | ui_num = (uint64_t) va_arg(ap, uintmax_t); |
470 | 0 | break; |
471 | 0 | case LM_PTRDIFF_T: |
472 | 0 | ui_num = (uint64_t) va_arg(ap, ptrdiff_t); |
473 | 0 | break; |
474 | 0 | } |
475 | 0 | s = ap_php_conv_p2(ui_num, 3, *fmt, |
476 | 0 | &num_buf[NUM_BUF_SIZE], &s_len); |
477 | 0 | FIX_PRECISION(adjust_precision, precision, s, s_len); |
478 | 0 | if (alternate_form && *s != '0') { |
479 | 0 | *--s = '0'; |
480 | 0 | s_len++; |
481 | 0 | } |
482 | 0 | break; |
483 | | |
484 | | |
485 | 169k | case 'x': |
486 | 211k | case 'X': |
487 | 211k | switch(modifier) { |
488 | 205k | default: |
489 | 205k | ui_num = (uint64_t) va_arg(ap, unsigned int); |
490 | 205k | break; |
491 | 0 | case LM_LONG_DOUBLE: |
492 | 0 | goto fmt_error; |
493 | 0 | case LM_LONG: |
494 | 0 | ui_num = (uint64_t) va_arg(ap, unsigned long int); |
495 | 0 | break; |
496 | 6.41k | case LM_SIZE_T: |
497 | 6.41k | ui_num = (uint64_t) va_arg(ap, size_t); |
498 | 6.41k | break; |
499 | 0 | #if SIZEOF_LONG_LONG |
500 | 0 | case LM_LONG_LONG: |
501 | 0 | ui_num = (uint64_t) va_arg(ap, unsigned long long int); |
502 | 0 | break; |
503 | 0 | #endif |
504 | 0 | case LM_INTMAX_T: |
505 | 0 | ui_num = (uint64_t) va_arg(ap, uintmax_t); |
506 | 0 | break; |
507 | 0 | case LM_PTRDIFF_T: |
508 | 0 | ui_num = (uint64_t) va_arg(ap, ptrdiff_t); |
509 | 0 | break; |
510 | 211k | } |
511 | 211k | s = ap_php_conv_p2(ui_num, 4, *fmt, |
512 | 211k | &num_buf[NUM_BUF_SIZE], &s_len); |
513 | 211k | FIX_PRECISION(adjust_precision, precision, s, s_len); |
514 | 211k | if (alternate_form && ui_num != 0) { |
515 | 0 | *--s = *fmt; /* 'x' or 'X' */ |
516 | 0 | *--s = '0'; |
517 | 0 | s_len += 2; |
518 | 0 | } |
519 | 211k | break; |
520 | | |
521 | | |
522 | 11.7M | case 's': |
523 | 11.7M | s = va_arg(ap, char *); |
524 | 11.7M | if (s != NULL) { |
525 | 11.7M | if (!adjust_precision) { |
526 | 11.7M | s_len = strlen(s); |
527 | 11.7M | } else { |
528 | 280 | s_len = zend_strnlen(s, precision); |
529 | 280 | } |
530 | 11.7M | } else { |
531 | 0 | s = S_NULL; |
532 | 0 | s_len = S_NULL_LEN; |
533 | 0 | } |
534 | 11.7M | pad_char = ' '; |
535 | 11.7M | break; |
536 | | |
537 | | |
538 | 85 | case 'f': |
539 | 604 | case 'F': |
540 | 604 | case 'e': |
541 | 604 | case 'E': |
542 | 604 | switch(modifier) { |
543 | 0 | case LM_LONG_DOUBLE: |
544 | 0 | fp_num = (double) va_arg(ap, long double); |
545 | 0 | break; |
546 | 604 | case LM_STD: |
547 | 604 | fp_num = va_arg(ap, double); |
548 | 604 | break; |
549 | 0 | default: |
550 | 0 | goto fmt_error; |
551 | 604 | } |
552 | | |
553 | 604 | if (zend_isnan(fp_num)) { |
554 | 4 | s = "nan"; |
555 | 4 | s_len = 3; |
556 | 600 | } else if (zend_isinf(fp_num)) { |
557 | 24 | s = "inf"; |
558 | 24 | s_len = 3; |
559 | 576 | } else { |
560 | | #ifdef ZTS |
561 | | localeconv_r(&lconv); |
562 | | #else |
563 | 576 | if (!lconv) { |
564 | 508 | lconv = localeconv(); |
565 | 508 | } |
566 | 576 | #endif |
567 | 576 | s = php_conv_fp((*fmt == 'f')?'F':*fmt, fp_num, alternate_form, |
568 | 576 | (adjust_precision == false) ? FLOAT_DIGITS : precision, |
569 | 576 | (*fmt == 'f')?LCONV_DECIMAL_POINT:'.', |
570 | 576 | &is_negative, &num_buf[1], &s_len); |
571 | 576 | if (is_negative) |
572 | 55 | prefix_char = '-'; |
573 | 521 | else if (print_sign) |
574 | 0 | prefix_char = '+'; |
575 | 521 | else if (print_blank) |
576 | 0 | prefix_char = ' '; |
577 | 576 | } |
578 | 604 | break; |
579 | | |
580 | | |
581 | 0 | case 'g': |
582 | 0 | case 'k': |
583 | 0 | case 'G': |
584 | 446k | case 'H': |
585 | 446k | switch(modifier) { |
586 | 0 | case LM_LONG_DOUBLE: |
587 | 0 | fp_num = (double) va_arg(ap, long double); |
588 | 0 | break; |
589 | 446k | case LM_STD: |
590 | 446k | fp_num = va_arg(ap, double); |
591 | 446k | break; |
592 | 0 | default: |
593 | 0 | goto fmt_error; |
594 | 446k | } |
595 | | |
596 | 446k | if (zend_isnan(fp_num)) { |
597 | 27.2k | s = "NAN"; |
598 | 27.2k | s_len = 3; |
599 | 27.2k | break; |
600 | 419k | } else if (zend_isinf(fp_num)) { |
601 | 5.28k | if (fp_num > 0) { |
602 | 4.85k | s = "INF"; |
603 | 4.85k | s_len = 3; |
604 | 4.85k | } else { |
605 | 428 | s = "-INF"; |
606 | 428 | s_len = 4; |
607 | 428 | } |
608 | 5.28k | break; |
609 | 5.28k | } |
610 | | |
611 | 413k | if (adjust_precision == false) |
612 | 0 | precision = FLOAT_DIGITS; |
613 | 413k | else if (precision == 0) |
614 | 0 | precision = 1; |
615 | | /* |
616 | | * * We use &num_buf[ 1 ], so that we have room for the sign |
617 | | */ |
618 | | #ifdef ZTS |
619 | | localeconv_r(&lconv); |
620 | | #else |
621 | 413k | if (!lconv) { |
622 | 413k | lconv = localeconv(); |
623 | 413k | } |
624 | 413k | #endif |
625 | 413k | s = zend_gcvt(fp_num, precision, (*fmt=='H' || *fmt == 'k') ? '.' : LCONV_DECIMAL_POINT, (*fmt == 'G' || *fmt == 'H')?'E':'e', &num_buf[1]); |
626 | 413k | if (*s == '-') |
627 | 88.8k | prefix_char = *s++; |
628 | 324k | else if (print_sign) |
629 | 0 | prefix_char = '+'; |
630 | 324k | else if (print_blank) |
631 | 0 | prefix_char = ' '; |
632 | | |
633 | 413k | s_len = strlen(s); |
634 | | |
635 | 413k | if (alternate_form && (strchr(s, '.')) == NULL) |
636 | 0 | s[s_len++] = '.'; |
637 | 413k | break; |
638 | | |
639 | | |
640 | 8.16M | case 'c': |
641 | 8.16M | char_buf[0] = (char) (va_arg(ap, int)); |
642 | 8.16M | s = &char_buf[0]; |
643 | 8.16M | s_len = 1; |
644 | 8.16M | pad_char = ' '; |
645 | 8.16M | break; |
646 | | |
647 | | |
648 | 0 | case '%': |
649 | 0 | char_buf[0] = '%'; |
650 | 0 | s = &char_buf[0]; |
651 | 0 | s_len = 1; |
652 | 0 | pad_char = ' '; |
653 | 0 | break; |
654 | | |
655 | | |
656 | 0 | case 'n': |
657 | 0 | *(va_arg(ap, int *)) = is_char? (int)((smart_string *)xbuf)->len : (int)ZSTR_LEN(((smart_str *)xbuf)->s); |
658 | 0 | goto skip_output; |
659 | | |
660 | | /* |
661 | | * Always extract the argument as a "char *" pointer. We |
662 | | * should be using "void *" but there are still machines |
663 | | * that don't understand it. |
664 | | * If the pointer size is equal to the size of an unsigned |
665 | | * integer we convert the pointer to a hex number, otherwise |
666 | | * we print "%p" to indicate that we don't handle "%p". |
667 | | */ |
668 | 151k | case 'p': |
669 | | /* %p[alnum]+ extensions */ |
670 | 151k | switch (*(fmt+1)) { |
671 | 151k | case 'S': |
672 | | /* zend_string* */ |
673 | 151k | fmt++; |
674 | 151k | goto format_zend_string; |
675 | 0 | case 'p': |
676 | | /* pointer */ |
677 | 0 | fmt++; |
678 | 0 | break; |
679 | 0 | default: |
680 | 0 | if (isalnum(*(fmt+1))) { |
681 | 0 | zend_error_noreturn(E_CORE_ERROR, |
682 | 0 | "Invalid printf specifier \"p%c\"", *(fmt+1)); |
683 | 0 | } |
684 | 0 | break; |
685 | 151k | } |
686 | | /* Normal %p */ |
687 | 0 | if (sizeof(char *) <= sizeof(uint64_t)) { |
688 | 0 | ui_num = (uint64_t)((size_t) va_arg(ap, char *)); |
689 | 0 | s = ap_php_conv_p2(ui_num, 4, 'x', |
690 | 0 | &num_buf[NUM_BUF_SIZE], &s_len); |
691 | 0 | if (ui_num != 0) { |
692 | 0 | *--s = 'x'; |
693 | 0 | *--s = '0'; |
694 | 0 | s_len += 2; |
695 | 0 | } |
696 | 0 | } else { |
697 | 0 | s = "%p"; |
698 | 0 | s_len = 2; |
699 | 0 | } |
700 | 0 | pad_char = ' '; |
701 | 0 | break; |
702 | | |
703 | | |
704 | 0 | case NUL: |
705 | | /* |
706 | | * The last character of the format string was %. |
707 | | * We ignore it. |
708 | | */ |
709 | 0 | continue; |
710 | | |
711 | | |
712 | 0 | fmt_error: |
713 | 0 | php_error(E_ERROR, "Illegal length modifier specified '%c' in s[np]printf call", *fmt); |
714 | | /* |
715 | | * The default case is for unrecognized %'s. |
716 | | * We print %<char> to help the user identify what |
717 | | * option is not understood. |
718 | | * This is also useful in case the user wants to pass |
719 | | * the output of format_converter to another function |
720 | | * that understands some other %<char> (like syslog). |
721 | | * Note that we can't point s inside fmt because the |
722 | | * unknown <char> could be preceded by width etc. |
723 | | */ |
724 | 0 | ZEND_FALLTHROUGH; |
725 | 0 | default: |
726 | 0 | char_buf[0] = '%'; |
727 | 0 | char_buf[1] = *fmt; |
728 | 0 | s = char_buf; |
729 | 0 | s_len = 2; |
730 | 0 | pad_char = ' '; |
731 | 0 | break; |
732 | 34.0M | } |
733 | | |
734 | 34.0M | if (prefix_char != NUL) { |
735 | 161k | *--s = prefix_char; |
736 | 161k | s_len++; |
737 | 161k | } |
738 | 34.0M | if (adjust_width && adjust == RIGHT && (size_t)min_width > s_len) { |
739 | 7.56M | if (pad_char == '0' && prefix_char != NUL) { |
740 | 0 | INS_CHAR(xbuf, *s, is_char); |
741 | 0 | s++; |
742 | 0 | s_len--; |
743 | 0 | min_width--; |
744 | 0 | } |
745 | 7.56M | PAD_CHAR(xbuf, pad_char, min_width - s_len, is_char); |
746 | 7.56M | } |
747 | | /* |
748 | | * Print the string s. |
749 | | */ |
750 | 34.0M | INS_STRING(xbuf, s, s_len, is_char); |
751 | | |
752 | 34.0M | if (adjust_width && adjust == LEFT && (size_t)min_width > s_len) { |
753 | 0 | PAD_CHAR(xbuf, pad_char, min_width - s_len, is_char); |
754 | 0 | } |
755 | | |
756 | 34.0M | zend_tmp_string_release(tmp_str); |
757 | 34.0M | } |
758 | 312M | skip_output: |
759 | 312M | fmt++; |
760 | 312M | } |
761 | 23.8M | return; |
762 | 23.8M | } |
763 | | /* }}} */ |
764 | | |
765 | | PHPAPI void php_printf_to_smart_string(smart_string *buf, const char *format, va_list ap) /* {{{ */ |
766 | 17.9M | { |
767 | 17.9M | xbuf_format_converter(buf, 1, format, ap); |
768 | 17.9M | } |
769 | | /* }}} */ |
770 | | |
771 | | PHPAPI void php_printf_to_smart_str(smart_str *buf, const char *format, va_list ap) /* {{{ */ |
772 | 5.86M | { |
773 | 5.86M | xbuf_format_converter(buf, 0, format, ap); |
774 | 5.86M | } |
775 | | /* }}} */ |