xref: /PHP-8.4/main/spprintf.c (revision 8d5c3e67)
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 'S': {
375 					zend_string *str = va_arg(ap, zend_string*);
376 					s_len = ZSTR_LEN(str);
377 					s = ZSTR_VAL(str);
378 					if (adjust_precision && (size_t)precision < s_len) {
379 						s_len = precision;
380 					}
381 					break;
382 				}
383 				case 'u':
384 					switch(modifier) {
385 						default:
386 							i_num = (int64_t) va_arg(ap, unsigned int);
387 							break;
388 						case LM_LONG_DOUBLE:
389 							goto fmt_error;
390 						case LM_LONG:
391 							i_num = (int64_t) va_arg(ap, unsigned long int);
392 							break;
393 						case LM_SIZE_T:
394 							i_num = (int64_t) va_arg(ap, size_t);
395 							break;
396 #if SIZEOF_LONG_LONG
397 						case LM_LONG_LONG:
398 							i_num = (int64_t) va_arg(ap, unsigned long long int);
399 							break;
400 #endif
401 #if SIZEOF_INTMAX_T
402 						case LM_INTMAX_T:
403 							i_num = (int64_t) va_arg(ap, uintmax_t);
404 							break;
405 #endif
406 #if SIZEOF_PTRDIFF_T
407 						case LM_PTRDIFF_T:
408 							i_num = (int64_t) va_arg(ap, ptrdiff_t);
409 							break;
410 #endif
411 					}
412 					/*
413 					 * The rest also applies to other integer formats, so fall
414 					 * into that case.
415 					 */
416 					ZEND_FALLTHROUGH;
417 				case 'd':
418 				case 'i':
419 					/*
420 					 * Get the arg if we haven't already.
421 					 */
422 					if ((*fmt) != 'u') {
423 						switch(modifier) {
424 							default:
425 								i_num = (int64_t) va_arg(ap, int);
426 								break;
427 							case LM_LONG_DOUBLE:
428 								goto fmt_error;
429 							case LM_LONG:
430 								i_num = (int64_t) va_arg(ap, long int);
431 								break;
432 							case LM_SIZE_T:
433 #if SIZEOF_SSIZE_T
434 								i_num = (int64_t) va_arg(ap, ssize_t);
435 #else
436 								i_num = (int64_t) va_arg(ap, size_t);
437 #endif
438 								break;
439 #if SIZEOF_LONG_LONG
440 							case LM_LONG_LONG:
441 								i_num = (int64_t) va_arg(ap, long long int);
442 								break;
443 #endif
444 #if SIZEOF_INTMAX_T
445 							case LM_INTMAX_T:
446 								i_num = (int64_t) va_arg(ap, intmax_t);
447 								break;
448 #endif
449 #if SIZEOF_PTRDIFF_T
450 							case LM_PTRDIFF_T:
451 								i_num = (int64_t) va_arg(ap, ptrdiff_t);
452 								break;
453 #endif
454 						}
455 					}
456 					s = ap_php_conv_10(i_num, (*fmt) == 'u', &is_negative,
457 								&num_buf[NUM_BUF_SIZE], &s_len);
458 					FIX_PRECISION(adjust_precision, precision, s, s_len);
459 
460 					if (*fmt != 'u') {
461 						if (is_negative)
462 							prefix_char = '-';
463 						else if (print_sign)
464 							prefix_char = '+';
465 						else if (print_blank)
466 							prefix_char = ' ';
467 					}
468 					break;
469 
470 
471 				case 'o':
472 					switch(modifier) {
473 						default:
474 							ui_num = (uint64_t) va_arg(ap, unsigned int);
475 							break;
476 						case LM_LONG_DOUBLE:
477 							goto fmt_error;
478 						case LM_LONG:
479 							ui_num = (uint64_t) va_arg(ap, unsigned long int);
480 							break;
481 						case LM_SIZE_T:
482 							ui_num = (uint64_t) va_arg(ap, size_t);
483 							break;
484 #if SIZEOF_LONG_LONG
485 						case LM_LONG_LONG:
486 							ui_num = (uint64_t) va_arg(ap, unsigned long long int);
487 							break;
488 #endif
489 #if SIZEOF_INTMAX_T
490 						case LM_INTMAX_T:
491 							ui_num = (uint64_t) va_arg(ap, uintmax_t);
492 							break;
493 #endif
494 #if SIZEOF_PTRDIFF_T
495 						case LM_PTRDIFF_T:
496 							ui_num = (uint64_t) va_arg(ap, ptrdiff_t);
497 							break;
498 #endif
499 					}
500 					s = ap_php_conv_p2(ui_num, 3, *fmt,
501 								&num_buf[NUM_BUF_SIZE], &s_len);
502 					FIX_PRECISION(adjust_precision, precision, s, s_len);
503 					if (alternate_form && *s != '0') {
504 						*--s = '0';
505 						s_len++;
506 					}
507 					break;
508 
509 
510 				case 'x':
511 				case 'X':
512 					switch(modifier) {
513 						default:
514 							ui_num = (uint64_t) va_arg(ap, unsigned int);
515 							break;
516 						case LM_LONG_DOUBLE:
517 							goto fmt_error;
518 						case LM_LONG:
519 							ui_num = (uint64_t) va_arg(ap, unsigned long int);
520 							break;
521 						case LM_SIZE_T:
522 							ui_num = (uint64_t) va_arg(ap, size_t);
523 							break;
524 #if SIZEOF_LONG_LONG
525 						case LM_LONG_LONG:
526 							ui_num = (uint64_t) va_arg(ap, unsigned long long int);
527 							break;
528 #endif
529 #if SIZEOF_INTMAX_T
530 						case LM_INTMAX_T:
531 							ui_num = (uint64_t) va_arg(ap, uintmax_t);
532 							break;
533 #endif
534 #if SIZEOF_PTRDIFF_T
535 						case LM_PTRDIFF_T:
536 							ui_num = (uint64_t) va_arg(ap, ptrdiff_t);
537 							break;
538 #endif
539 					}
540 					s = ap_php_conv_p2(ui_num, 4, *fmt,
541 								&num_buf[NUM_BUF_SIZE], &s_len);
542 					FIX_PRECISION(adjust_precision, precision, s, s_len);
543 					if (alternate_form && ui_num != 0) {
544 						*--s = *fmt;	/* 'x' or 'X' */
545 						*--s = '0';
546 						s_len += 2;
547 					}
548 					break;
549 
550 
551 				case 's':
552 					s = va_arg(ap, char *);
553 					if (s != NULL) {
554 						if (!adjust_precision) {
555 							s_len = strlen(s);
556 						} else {
557 							s_len = zend_strnlen(s, precision);
558 						}
559 					} else {
560 						s = S_NULL;
561 						s_len = S_NULL_LEN;
562 					}
563 					pad_char = ' ';
564 					break;
565 
566 
567 				case 'f':
568 				case 'F':
569 				case 'e':
570 				case 'E':
571 					switch(modifier) {
572 						case LM_LONG_DOUBLE:
573 							fp_num = (double) va_arg(ap, long double);
574 							break;
575 						case LM_STD:
576 							fp_num = va_arg(ap, double);
577 							break;
578 						default:
579 							goto fmt_error;
580 					}
581 
582 					if (zend_isnan(fp_num)) {
583 						s = "nan";
584 						s_len = 3;
585 					} else if (zend_isinf(fp_num)) {
586 						s = "inf";
587 						s_len = 3;
588 					} else {
589 #ifdef ZTS
590 						localeconv_r(&lconv);
591 #else
592 						if (!lconv) {
593 							lconv = localeconv();
594 						}
595 #endif
596 						s = php_conv_fp((*fmt == 'f')?'F':*fmt, fp_num, alternate_form,
597 						 (adjust_precision == false) ? FLOAT_DIGITS : precision,
598 						 (*fmt == 'f')?LCONV_DECIMAL_POINT:'.',
599 									&is_negative, &num_buf[1], &s_len);
600 						if (is_negative)
601 							prefix_char = '-';
602 						else if (print_sign)
603 							prefix_char = '+';
604 						else if (print_blank)
605 							prefix_char = ' ';
606 					}
607 					break;
608 
609 
610 				case 'g':
611 				case 'k':
612 				case 'G':
613 				case 'H':
614 					switch(modifier) {
615 						case LM_LONG_DOUBLE:
616 							fp_num = (double) va_arg(ap, long double);
617 							break;
618 						case LM_STD:
619 							fp_num = va_arg(ap, double);
620 							break;
621 						default:
622 							goto fmt_error;
623 					}
624 
625 					if (zend_isnan(fp_num)) {
626  						s = "NAN";
627  						s_len = 3;
628  						break;
629  					} else if (zend_isinf(fp_num)) {
630  						if (fp_num > 0) {
631  							s = "INF";
632  							s_len = 3;
633  						} else {
634  							s = "-INF";
635  							s_len = 4;
636  						}
637  						break;
638  					}
639 
640 					if (adjust_precision == false)
641 						precision = FLOAT_DIGITS;
642 					else if (precision == 0)
643 						precision = 1;
644 					/*
645 					 * * We use &num_buf[ 1 ], so that we have room for the sign
646 					 */
647 #ifdef ZTS
648 					localeconv_r(&lconv);
649 #else
650 					if (!lconv) {
651 						lconv = localeconv();
652 					}
653 #endif
654 					s = zend_gcvt(fp_num, precision, (*fmt=='H' || *fmt == 'k') ? '.' : LCONV_DECIMAL_POINT, (*fmt == 'G' || *fmt == 'H')?'E':'e', &num_buf[1]);
655 					if (*s == '-')
656 						prefix_char = *s++;
657 					else if (print_sign)
658 						prefix_char = '+';
659 					else if (print_blank)
660 						prefix_char = ' ';
661 
662 					s_len = strlen(s);
663 
664 					if (alternate_form && (strchr(s, '.')) == NULL)
665 						s[s_len++] = '.';
666 					break;
667 
668 
669 				case 'c':
670 					char_buf[0] = (char) (va_arg(ap, int));
671 					s = &char_buf[0];
672 					s_len = 1;
673 					pad_char = ' ';
674 					break;
675 
676 
677 				case '%':
678 					char_buf[0] = '%';
679 					s = &char_buf[0];
680 					s_len = 1;
681 					pad_char = ' ';
682 					break;
683 
684 
685 				case 'n':
686 					*(va_arg(ap, int *)) = is_char? (int)((smart_string *)xbuf)->len : (int)ZSTR_LEN(((smart_str *)xbuf)->s);
687 					goto skip_output;
688 
689 					/*
690 					 * Always extract the argument as a "char *" pointer. We
691 					 * should be using "void *" but there are still machines
692 					 * that don't understand it.
693 					 * If the pointer size is equal to the size of an unsigned
694 					 * integer we convert the pointer to a hex number, otherwise
695 					 * we print "%p" to indicate that we don't handle "%p".
696 					 */
697 				case 'p':
698 					if (sizeof(char *) <= sizeof(uint64_t)) {
699 						ui_num = (uint64_t)((size_t) va_arg(ap, char *));
700 						s = ap_php_conv_p2(ui_num, 4, 'x',
701 								&num_buf[NUM_BUF_SIZE], &s_len);
702 						if (ui_num != 0) {
703 							*--s = 'x';
704 							*--s = '0';
705 							s_len += 2;
706 						}
707 					} else {
708 						s = "%p";
709 						s_len = 2;
710 					}
711 					pad_char = ' ';
712 					break;
713 
714 
715 				case NUL:
716 					/*
717 					 * The last character of the format string was %.
718 					 * We ignore it.
719 					 */
720 					continue;
721 
722 
723 fmt_error:
724 				php_error(E_ERROR, "Illegal length modifier specified '%c' in s[np]printf call", *fmt);
725 					/*
726 					 * The default case is for unrecognized %'s.
727 					 * We print %<char> to help the user identify what
728 					 * option is not understood.
729 					 * This is also useful in case the user wants to pass
730 					 * the output of format_converter to another function
731 					 * that understands some other %<char> (like syslog).
732 					 * Note that we can't point s inside fmt because the
733 					 * unknown <char> could be preceded by width etc.
734 					 */
735 					ZEND_FALLTHROUGH;
736 				default:
737 					char_buf[0] = '%';
738 					char_buf[1] = *fmt;
739 					s = char_buf;
740 					s_len = 2;
741 					pad_char = ' ';
742 					break;
743 			}
744 
745 			if (prefix_char != NUL) {
746 				*--s = prefix_char;
747 				s_len++;
748 			}
749 			if (adjust_width && adjust == RIGHT && (size_t)min_width > s_len) {
750 				if (pad_char == '0' && prefix_char != NUL) {
751 					INS_CHAR(xbuf, *s, is_char);
752 					s++;
753 					s_len--;
754 					min_width--;
755 				}
756 				PAD_CHAR(xbuf, pad_char, min_width - s_len, is_char);
757 			}
758 			/*
759 			 * Print the string s.
760 			 */
761 			INS_STRING(xbuf, s, s_len, is_char);
762 
763 			if (adjust_width && adjust == LEFT && (size_t)min_width > s_len) {
764 				PAD_CHAR(xbuf, pad_char, min_width - s_len, is_char);
765 			}
766 
767 			zend_tmp_string_release(tmp_str);
768 		}
769 skip_output:
770 		fmt++;
771 	}
772 	return;
773 }
774 /* }}} */
775 
php_printf_to_smart_string(smart_string * buf,const char * format,va_list ap)776 PHPAPI void php_printf_to_smart_string(smart_string *buf, const char *format, va_list ap) /* {{{ */
777 {
778 	xbuf_format_converter(buf, 1, format, ap);
779 }
780 /* }}} */
781 
php_printf_to_smart_str(smart_str * buf,const char * format,va_list ap)782 PHPAPI void php_printf_to_smart_str(smart_str *buf, const char *format, va_list ap) /* {{{ */
783 {
784 	xbuf_format_converter(buf, 0, format, ap);
785 }
786 /* }}} */
787