Coverage Report

Created: 2026-06-02 06:39

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