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