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