Coverage Report

Created: 2026-06-02 06:36

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
0
#define LCONV_DECIMAL_POINT (*lconv->decimal_point)
94
#endif
95
96
#include "snprintf.h"
97
98
4
#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
16
#define INS_CHAR(xbuf, ch, is_char) do { \
113
16
  if ((is_char)) { \
114
16
    smart_string_appendc((smart_string *)(xbuf), (ch)); \
115
16
  } else { \
116
0
    smart_str_appendc((smart_str *)(xbuf), (ch)); \
117
0
  } \
118
16
} while (0);
119
120
2
#define INS_STRING(xbuf, str, len, is_char) do { \
121
2
  if ((is_char)) { \
122
2
    smart_string_appendl((smart_string *)(xbuf), (str), (len)); \
123
2
  } else { \
124
0
    smart_str_appendl((smart_str *)(xbuf), (str), (len)); \
125
0
  } \
126
2
} while (0);
127
128
0
#define PAD_CHAR(xbuf, ch, count, is_char) do { \
129
0
  if ((is_char)) { \
130
0
    smart_string_alloc(((smart_string *)(xbuf)), (count), 0); \
131
0
    memset(((smart_string *)(xbuf))->c + ((smart_string *)(xbuf))->len, (ch), (count)); \
132
0
    ((smart_string *)(xbuf))->len += (count); \
133
0
  } else { \
134
0
    smart_str_alloc(((smart_str *)(xbuf)), (count), 0); \
135
0
    memset(ZSTR_VAL(((smart_str *)(xbuf))->s) + ZSTR_LEN(((smart_str *)(xbuf))->s), (ch), (count)); \
136
0
    ZSTR_LEN(((smart_str *)(xbuf))->s) += (count); \
137
0
  } \
138
0
} 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
0
#define NUM_BUF_SIZE ZEND_DOUBLE_MAX_LENGTH
145
146
0
#define NUM(c) (c - '0')
147
148
0
#define STR_TO_DEC(str, num) do {     \
149
0
  num = NUM(*(str)++);                   \
150
0
  while (isdigit((unsigned char)*(str))) {\
151
0
    num *= 10;                        \
152
0
    num += NUM(*(str)++);               \
153
0
    if (num >= INT_MAX / 10) {     \
154
0
      while (isdigit((unsigned char)*(str)++));  \
155
0
      break;              \
156
0
    }                 \
157
0
    }                   \
158
0
} 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
0
#define FIX_PRECISION(adjust, precision, s, s_len) do { \
167
0
    if (adjust)                               \
168
0
    while (s_len < (size_t)precision) {       \
169
0
      *--s = '0';                               \
170
0
      s_len++;                                  \
171
0
    }                        \
172
0
} 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
{
181
2
  char *s = NULL;
182
2
  size_t s_len;
183
184
2
  int min_width = 0;
185
2
  int precision = 0;
186
2
  enum {
187
2
    LEFT, RIGHT
188
2
  } adjust;
189
2
  char pad_char;
190
2
  char prefix_char;
191
192
2
  double fp_num;
193
2
  int64_t i_num = (int64_t) 0;
194
2
  uint64_t ui_num = (uint64_t) 0;
195
196
2
  char num_buf[NUM_BUF_SIZE];
197
2
  char char_buf[2];     /* for printing %% and %<unknown> */
198
199
#ifdef ZTS
200
  struct lconv lconv;
201
#else
202
2
  struct lconv *lconv = NULL;
203
2
#endif
204
205
  /*
206
   * Flag variables
207
   */
208
2
  length_modifier_e modifier;
209
2
  bool alternate_form;
210
2
  bool print_sign;
211
2
  bool print_blank;
212
2
  bool adjust_precision;
213
2
  bool adjust_width;
214
2
  bool is_negative;
215
216
20
  while (*fmt) {
217
18
    if (*fmt != '%') {
218
16
      INS_CHAR(xbuf, *fmt, is_char);
219
16
    } else {
220
      /*
221
       * Default variable settings
222
       */
223
2
      zend_string *tmp_str = NULL;
224
2
      adjust = RIGHT;
225
2
      alternate_form = print_sign = print_blank = false;
226
2
      pad_char = ' ';
227
2
      prefix_char = NUL;
228
229
2
      fmt++;
230
231
      /*
232
       * Try to avoid checking for flags, width or precision
233
       */
234
2
      if (isascii((unsigned char)*fmt) && !islower((unsigned char)*fmt)) {
235
        /*
236
         * Recognize flags: -, #, BLANK, +
237
         */
238
0
        for (;; fmt++) {
239
0
          if (*fmt == '-')
240
0
            adjust = LEFT;
241
0
          else if (*fmt == '+')
242
0
            print_sign = true;
243
0
          else if (*fmt == '#')
244
0
            alternate_form = true;
245
0
          else if (*fmt == ' ')
246
0
            print_blank = true;
247
0
          else if (*fmt == '0')
248
0
            pad_char = '0';
249
0
          else
250
0
            break;
251
0
        }
252
253
        /*
254
         * Check if a width was specified
255
         */
256
0
        if (isdigit((unsigned char)*fmt)) {
257
0
          STR_TO_DEC(fmt, min_width);
258
0
          adjust_width = true;
259
0
        } else if (*fmt == '*') {
260
0
          min_width = va_arg(ap, int);
261
0
          fmt++;
262
0
          adjust_width = true;
263
0
          if (min_width < 0) {
264
0
            adjust = LEFT;
265
0
            min_width = -min_width;
266
0
          }
267
0
        } else
268
0
          adjust_width = false;
269
270
        /*
271
         * Check if a precision was specified
272
         */
273
0
        if (*fmt == '.') {
274
0
          adjust_precision = true;
275
0
          fmt++;
276
0
          if (isdigit((unsigned char)*fmt)) {
277
0
            STR_TO_DEC(fmt, precision);
278
0
          } else if (*fmt == '*') {
279
0
            precision = va_arg(ap, int);
280
0
            fmt++;
281
0
            if (precision < -1)
282
0
              precision = -1;
283
0
          } else
284
0
            precision = 0;
285
0
        } else
286
0
          adjust_precision = false;
287
0
      } else
288
2
        adjust_precision = adjust_width = false;
289
290
      /*
291
       * Modifier check
292
       */
293
2
      switch (*fmt) {
294
0
        case 'L':
295
0
          fmt++;
296
0
          modifier = LM_LONG_DOUBLE;
297
0
          break;
298
0
        case 'l':
299
0
          fmt++;
300
0
#if SIZEOF_LONG_LONG
301
0
          if (*fmt == 'l') {
302
0
            fmt++;
303
0
            modifier = LM_LONG_LONG;
304
0
          } else
305
0
#endif
306
0
            modifier = LM_LONG;
307
0
          break;
308
0
        case 'z':
309
0
          fmt++;
310
0
          modifier = LM_SIZE_T;
311
0
          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
        default:
338
2
          modifier = LM_STD;
339
2
          break;
340
2
      }
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
      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
0
        case 'S': {
365
0
          zend_string *str = va_arg(ap, zend_string*);
366
0
          s_len = ZSTR_LEN(str);
367
0
          s = ZSTR_VAL(str);
368
0
          if (adjust_precision && (size_t)precision < s_len) {
369
0
            s_len = precision;
370
0
          }
371
0
          break;
372
0
        }
373
0
        case 'u':
374
0
          switch(modifier) {
375
0
            default:
376
0
              i_num = (int64_t) va_arg(ap, unsigned int);
377
0
              break;
378
0
            case LM_LONG_DOUBLE:
379
0
              goto fmt_error;
380
0
            case LM_LONG:
381
0
              i_num = (int64_t) va_arg(ap, unsigned long int);
382
0
              break;
383
0
            case LM_SIZE_T:
384
0
              i_num = (int64_t) va_arg(ap, size_t);
385
0
              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
0
          }
398
          /*
399
           * The rest also applies to other integer formats, so fall
400
           * into that case.
401
           */
402
0
          ZEND_FALLTHROUGH;
403
0
        case 'd':
404
0
        case 'i':
405
          /*
406
           * Get the arg if we haven't already.
407
           */
408
0
          if ((*fmt) != 'u') {
409
0
            switch(modifier) {
410
0
              default:
411
0
                i_num = (int64_t) va_arg(ap, int);
412
0
                break;
413
0
              case LM_LONG_DOUBLE:
414
0
                goto fmt_error;
415
0
              case LM_LONG:
416
0
                i_num = (int64_t) va_arg(ap, long int);
417
0
                break;
418
0
              case LM_SIZE_T:
419
0
                i_num = (int64_t) va_arg(ap, ssize_t);
420
0
                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
0
            }
433
0
          }
434
0
          s = ap_php_conv_10(i_num, (*fmt) == 'u', &is_negative,
435
0
                &num_buf[NUM_BUF_SIZE], &s_len);
436
0
          FIX_PRECISION(adjust_precision, precision, s, s_len);
437
438
0
          if (*fmt != 'u') {
439
0
            if (is_negative)
440
0
              prefix_char = '-';
441
0
            else if (print_sign)
442
0
              prefix_char = '+';
443
0
            else if (print_blank)
444
0
              prefix_char = ' ';
445
0
          }
446
0
          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
0
        case 'x':
485
0
        case 'X':
486
0
          switch(modifier) {
487
0
            default:
488
0
              ui_num = (uint64_t) va_arg(ap, unsigned int);
489
0
              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
0
            case LM_SIZE_T:
496
0
              ui_num = (uint64_t) va_arg(ap, size_t);
497
0
              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
0
          }
510
0
          s = ap_php_conv_p2(ui_num, 4, *fmt,
511
0
                &num_buf[NUM_BUF_SIZE], &s_len);
512
0
          FIX_PRECISION(adjust_precision, precision, s, s_len);
513
0
          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
0
          break;
519
520
521
2
        case 's':
522
2
          s = va_arg(ap, char *);
523
2
          if (s != NULL) {
524
2
            if (!adjust_precision) {
525
2
              s_len = strlen(s);
526
2
            } else {
527
0
              s_len = zend_strnlen(s, precision);
528
0
            }
529
2
          } else {
530
0
            s = S_NULL;
531
0
            s_len = S_NULL_LEN;
532
0
          }
533
2
          pad_char = ' ';
534
2
          break;
535
536
537
0
        case 'f':
538
0
        case 'F':
539
0
        case 'e':
540
0
        case 'E':
541
0
          switch(modifier) {
542
0
            case LM_LONG_DOUBLE:
543
0
              fp_num = (double) va_arg(ap, long double);
544
0
              break;
545
0
            case LM_STD:
546
0
              fp_num = va_arg(ap, double);
547
0
              break;
548
0
            default:
549
0
              goto fmt_error;
550
0
          }
551
552
0
          if (zend_isnan(fp_num)) {
553
0
            s = "nan";
554
0
            s_len = 3;
555
0
          } else if (zend_isinf(fp_num)) {
556
0
            s = "inf";
557
0
            s_len = 3;
558
0
          } else {
559
#ifdef ZTS
560
            localeconv_r(&lconv);
561
#else
562
0
            if (!lconv) {
563
0
              lconv = localeconv();
564
0
            }
565
0
#endif
566
0
            s = php_conv_fp((*fmt == 'f')?'F':*fmt, fp_num, alternate_form,
567
0
             (adjust_precision == false) ? FLOAT_DIGITS : precision,
568
0
             (*fmt == 'f')?LCONV_DECIMAL_POINT:'.',
569
0
                  &is_negative, &num_buf[1], &s_len);
570
0
            if (is_negative)
571
0
              prefix_char = '-';
572
0
            else if (print_sign)
573
0
              prefix_char = '+';
574
0
            else if (print_blank)
575
0
              prefix_char = ' ';
576
0
          }
577
0
          break;
578
579
580
0
        case 'g':
581
0
        case 'k':
582
0
        case 'G':
583
0
        case 'H':
584
0
          switch(modifier) {
585
0
            case LM_LONG_DOUBLE:
586
0
              fp_num = (double) va_arg(ap, long double);
587
0
              break;
588
0
            case LM_STD:
589
0
              fp_num = va_arg(ap, double);
590
0
              break;
591
0
            default:
592
0
              goto fmt_error;
593
0
          }
594
595
0
          if (zend_isnan(fp_num)) {
596
0
            s = "NAN";
597
0
            s_len = 3;
598
0
            break;
599
0
          } else if (zend_isinf(fp_num)) {
600
0
            if (fp_num > 0) {
601
0
              s = "INF";
602
0
              s_len = 3;
603
0
            } else {
604
0
              s = "-INF";
605
0
              s_len = 4;
606
0
            }
607
0
            break;
608
0
          }
609
610
0
          if (adjust_precision == false)
611
0
            precision = FLOAT_DIGITS;
612
0
          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
0
          if (!lconv) {
621
0
            lconv = localeconv();
622
0
          }
623
0
#endif
624
0
          s = zend_gcvt(fp_num, precision, (*fmt=='H' || *fmt == 'k') ? '.' : LCONV_DECIMAL_POINT, (*fmt == 'G' || *fmt == 'H')?'E':'e', &num_buf[1]);
625
0
          if (*s == '-')
626
0
            prefix_char = *s++;
627
0
          else if (print_sign)
628
0
            prefix_char = '+';
629
0
          else if (print_blank)
630
0
            prefix_char = ' ';
631
632
0
          s_len = strlen(s);
633
634
0
          if (alternate_form && (strchr(s, '.')) == NULL)
635
0
            s[s_len++] = '.';
636
0
          break;
637
638
639
0
        case 'c':
640
0
          char_buf[0] = (char) (va_arg(ap, int));
641
0
          s = &char_buf[0];
642
0
          s_len = 1;
643
0
          pad_char = ' ';
644
0
          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
      }
714
715
2
      if (prefix_char != NUL) {
716
0
        *--s = prefix_char;
717
0
        s_len++;
718
0
      }
719
2
      if (adjust_width && adjust == RIGHT && (size_t)min_width > s_len) {
720
0
        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
0
        PAD_CHAR(xbuf, pad_char, min_width - s_len, is_char);
727
0
      }
728
      /*
729
       * Print the string s.
730
       */
731
2
      INS_STRING(xbuf, s, s_len, is_char);
732
733
2
      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
      zend_tmp_string_release(tmp_str);
738
2
    }
739
18
skip_output:
740
18
    fmt++;
741
18
  }
742
2
  return;
743
2
}
744
/* }}} */
745
746
PHPAPI void php_printf_to_smart_string(smart_string *buf, const char *format, va_list ap) /* {{{ */
747
2
{
748
2
  xbuf_format_converter(buf, 1, format, ap);
749
2
}
750
/* }}} */
751
752
PHPAPI void php_printf_to_smart_str(smart_str *buf, const char *format, va_list ap) /* {{{ */
753
0
{
754
0
  xbuf_format_converter(buf, 0, format, ap);
755
0
}
756
/* }}} */