PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - main - snprintf.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 346 517 66.9 %
Date: 2015-07-31 Functions: 13 15 86.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2015 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:                                                              |
      16             :   +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #define _GNU_SOURCE
      22             : #include "php.h"
      23             : 
      24             : #include <zend_strtod.h>
      25             : 
      26             : #include <stddef.h>
      27             : #include <stdio.h>
      28             : #include <ctype.h>
      29             : #include <sys/types.h>
      30             : #include <stdarg.h>
      31             : #include <string.h>
      32             : #include <stdlib.h>
      33             : #include <math.h>
      34             : 
      35             : #ifdef HAVE_INTTYPES_H
      36             : #include <inttypes.h>
      37             : #endif
      38             : 
      39             : #ifdef HAVE_LOCALE_H
      40             : #include <locale.h>
      41             : #ifdef ZTS
      42             : #include "ext/standard/php_string.h"
      43             : #define LCONV_DECIMAL_POINT (*lconv.decimal_point)
      44             : #else
      45             : #define LCONV_DECIMAL_POINT (*lconv->decimal_point)
      46             : #endif
      47             : #else
      48             : #define LCONV_DECIMAL_POINT '.'
      49             : #endif
      50             : 
      51             : /*
      52             :  * Copyright (c) 2002, 2006 Todd C. Miller <Todd.Miller@courtesan.com>
      53             :  *
      54             :  * Permission to use, copy, modify, and distribute this software for any
      55             :  * purpose with or without fee is hereby granted, provided that the above
      56             :  * copyright notice and this permission notice appear in all copies.
      57             :  *
      58             :  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      59             :  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
      60             :  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
      61             :  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
      62             :  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
      63             :  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      64             :  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      65             :  *
      66             :  * Sponsored in part by the Defense Advanced Research Projects
      67             :  * Agency (DARPA) and Air Force Research Laboratory, Air Force
      68             :  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
      69             :  */
      70             : 
      71      105437 : static char * __cvt(double value, int ndigit, int *decpt, int *sign, int fmode, int pad) /* {{{ */
      72             : {
      73      105437 :         register char *s = NULL;
      74             :         char *p, *rve, c;
      75             :         size_t siz;
      76             : 
      77      105437 :         if (ndigit < 0) {
      78           0 :                 siz = -ndigit + 1;
      79             :         } else {
      80      105437 :                 siz = ndigit + 1;
      81             :         }
      82             : 
      83             :         /* __dtoa() doesn't allocate space for 0 so we do it by hand */
      84      105437 :         if (value == 0.0) {
      85         508 :                 *decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */
      86         508 :                 *sign = 0;
      87         508 :                 if ((rve = s = (char *)malloc(ndigit?siz:2)) == NULL) {
      88           0 :                         return(NULL);
      89             :                 }
      90         508 :                 *rve++ = '0';
      91         508 :                 *rve = '\0';
      92         508 :                 if (!ndigit) {
      93           8 :                         return(s);
      94             :                 }
      95             :         } else {
      96      104929 :                 p = zend_dtoa(value, fmode + 2, ndigit, decpt, sign, &rve);
      97      104929 :                 if (*decpt == 9999) {
      98             :                         /* Infinity or Nan, convert to inf or nan like printf */
      99           0 :                         *decpt = 0;
     100           0 :                         c = *p;
     101           0 :                         zend_freedtoa(p);
     102           0 :                         return strdup((c == 'I' ? "INF" : "NAN"));
     103             :                 }
     104             :                 /* Make a local copy and adjust rve to be in terms of s */
     105      104929 :                 if (pad && fmode) {
     106      104177 :                         siz += *decpt;
     107             :                 }
     108      104929 :                 if ((s = (char *)malloc(siz+1)) == NULL) {
     109           0 :                         zend_freedtoa(p);
     110           0 :                         return(NULL);
     111             :                 }
     112      104929 :                 (void) strlcpy(s, p, siz);
     113      104929 :                 rve = s + (rve - p);
     114      104929 :                 zend_freedtoa(p);
     115             :         }
     116             : 
     117             :         /* Add trailing zeros */
     118      105429 :         if (pad) {
     119      105429 :                 siz -= rve - s;
     120      441915 :                 while (--siz) {
     121      231057 :                         *rve++ = '0';
     122             :                 }
     123      105429 :                 *rve = '\0';
     124             :         }
     125             : 
     126      105429 :         return(s);
     127             : }
     128             : /* }}} */
     129             : 
     130         983 : static inline char *php_ecvt(double value, int ndigit, int *decpt, int *sign) /* {{{ */
     131             : {
     132         983 :         return(__cvt(value, ndigit, decpt, sign, 0, 1));
     133             : }
     134             : /* }}} */
     135             : 
     136      104454 : static inline char *php_fcvt(double value, int ndigit, int *decpt, int *sign) /* {{{ */
     137             : {
     138      104454 :     return(__cvt(value, ndigit, decpt, sign, 1, 1));
     139             : }
     140             : /* }}} */
     141             : 
     142       14401 : PHPAPI char *php_gcvt(double value, int ndigit, char dec_point, char exponent, char *buf) /* {{{ */
     143             : {
     144             :         char *digits, *dst, *src;
     145             :         int i, decpt, sign;
     146             : 
     147       14401 :         digits = zend_dtoa(value, 2, ndigit, &decpt, &sign, NULL);
     148       14401 :         if (decpt == 9999) {
     149             :                 /*
     150             :                  * Infinity or NaN, convert to inf or nan with sign.
     151             :                  * We assume the buffer is at least ndigit long.
     152             :                  */
     153          12 :                 snprintf(buf, ndigit + 1, "%s%s", (sign && *digits == 'I') ? "-" : "", *digits == 'I' ? "INF" : "NAN");
     154          12 :                 zend_freedtoa(digits);
     155          12 :                 return (buf);
     156             :         }
     157             : 
     158       14389 :         dst = buf;
     159       14389 :         if (sign) {
     160        3261 :                 *dst++ = '-';
     161             :         }
     162             : 
     163       16773 :         if ((decpt >= 0 && decpt > ndigit) || decpt < -3) { /* use E-style */
     164             :                 /* exponential format (e.g. 1.2345e+13) */
     165        2384 :                 if (--decpt < 0) {
     166        1214 :                         sign = 1;
     167        1214 :                         decpt = -decpt;
     168             :                 } else {
     169        1170 :                         sign = 0;
     170             :                 }
     171        2384 :                 src = digits;
     172        2384 :                 *dst++ = *src++;
     173        2384 :                 *dst++ = dec_point;
     174        2384 :                 if (*src == '\0') {
     175         148 :                         *dst++ = '0';
     176             :                 } else {
     177             :                         do {
     178      308977 :                                 *dst++ = *src++;
     179      308977 :                         } while (*src != '\0');
     180             :                 }
     181        2384 :                 *dst++ = exponent;
     182        2384 :                 if (sign) {
     183        1214 :                         *dst++ = '-';
     184             :                 } else {
     185        1170 :                         *dst++ = '+';
     186             :                 }
     187        2384 :                 if (decpt < 10) {
     188         429 :                         *dst++ = '0' + decpt;
     189         429 :                         *dst = '\0';
     190             :                 } else {
     191             :                         /* XXX - optimize */
     192        4537 :                         for (sign = decpt, i = 0; (sign /= 10) != 0; i++)
     193        2582 :                                 continue;
     194        1955 :                         dst[i + 1] = '\0';
     195        8447 :                         while (decpt != 0) {
     196        4537 :                                 dst[i--] = '0' + decpt % 10;
     197        4537 :                                 decpt /= 10;
     198             :                         }
     199             :                 }
     200       12005 :         } else if (decpt < 0) {
     201             :                 /* standard format 0. */
     202         249 :                 *dst++ = '0';   /* zero before decimal point */
     203         249 :                 *dst++ = dec_point;
     204             :                 do {
     205         435 :                         *dst++ = '0';
     206         435 :                 } while (++decpt < 0);
     207         249 :                 src = digits;
     208        2094 :                 while (*src != '\0') {
     209        1596 :                         *dst++ = *src++;
     210             :                 }
     211         249 :                 *dst = '\0';
     212             :         } else {
     213             :                 /* standard format */
     214       42176 :                 for (i = 0, src = digits; i < decpt; i++) {
     215       30420 :                         if (*src != '\0') {
     216       26417 :                                 *dst++ = *src++;
     217             :                         } else {
     218        4003 :                                 *dst++ = '0';
     219             :                         }
     220             :                 }
     221       11756 :                 if (*src != '\0') {
     222        8148 :                         if (src == digits) {
     223         943 :                                 *dst++ = '0';   /* zero before decimal point */
     224             :                         }
     225        8148 :                         *dst++ = dec_point;
     226       35542 :                         for (i = decpt; digits[i] != '\0'; i++) {
     227       27394 :                 *dst++ = digits[i];
     228             :             }
     229             :         }
     230       11756 :         *dst = '\0';
     231             :     }
     232       14389 :     zend_freedtoa(digits);
     233       14389 :     return (buf);
     234             : }
     235             : /* }}} */
     236             : 
     237             : /* {{{ Apache license */
     238             : /* ====================================================================
     239             :  * Copyright (c) 1995-1998 The Apache Group.  All rights reserved.
     240             :  *
     241             :  * Redistribution and use in source and binary forms, with or without
     242             :  * modification, are permitted provided that the following conditions
     243             :  * are met:
     244             :  *
     245             :  * 1. Redistributions of source code must retain the above copyright
     246             :  *    notice, this list of conditions and the following disclaimer.
     247             :  *
     248             :  * 2. Redistributions in binary form must reproduce the above copyright
     249             :  *    notice, this list of conditions and the following disclaimer in
     250             :  *    the documentation and/or other materials provided with the
     251             :  *    distribution.
     252             :  *
     253             :  * 3. All advertising materials mentioning features or use of this
     254             :  *    software must display the following acknowledgment:
     255             :  *    "This product includes software developed by the Apache Group
     256             :  *    for use in the Apache HTTP server project (http://www.apache.org/)."
     257             :  *
     258             :  * 4. The names "Apache Server" and "Apache Group" must not be used to
     259             :  *    endorse or promote products derived from this software without
     260             :  *    prior written permission.
     261             :  *
     262             :  * 5. Redistributions of any form whatsoever must retain the following
     263             :  *    acknowledgment:
     264             :  *    "This product includes software developed by the Apache Group
     265             :  *    for use in the Apache HTTP server project (http://www.apache.org/)."
     266             :  *
     267             :  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
     268             :  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     269             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     270             :  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
     271             :  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     272             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     273             :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     274             :  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     275             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     276             :  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     277             :  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
     278             :  * OF THE POSSIBILITY OF SUCH DAMAGE.
     279             :  * ====================================================================
     280             :  *
     281             :  * This software consists of voluntary contributions made by many
     282             :  * individuals on behalf of the Apache Group and was originally based
     283             :  * on public domain software written at the National Center for
     284             :  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
     285             :  * For more information on the Apache Group and the Apache HTTP server
     286             :  * project, please see <http://www.apache.org/>.
     287             :  *
     288             :  * This code is based on, and used with the permission of, the
     289             :  * SIO stdio-replacement strx_* functions by Panos Tsirigotis
     290             :  * <panos@alumni.cs.colorado.edu> for xinetd.
     291             :  */
     292             : /* }}} */
     293             : 
     294             : #define FALSE                   0
     295             : #define TRUE                    1
     296             : #define NUL                     '\0'
     297             : #define INT_NULL                ((int *)0)
     298             : 
     299             : #define S_NULL                  "(null)"
     300             : #define S_NULL_LEN              6
     301             : 
     302             : #define FLOAT_DIGITS            6
     303             : #define EXPONENT_LENGTH         10
     304             : 
     305             : 
     306             : /*
     307             :  * Convert num to its decimal format.
     308             :  * Return value:
     309             :  *   - a pointer to a string containing the number (no sign)
     310             :  *   - len contains the length of the string
     311             :  *   - is_negative is set to TRUE or FALSE depending on the sign
     312             :  *     of the number (always set to FALSE if is_unsigned is TRUE)
     313             :  *
     314             :  * The caller provides a buffer for the string: that is the buf_end argument
     315             :  * which is a pointer to the END of the buffer + 1 (i.e. if the buffer
     316             :  * is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
     317             :  */
     318             : /* char * ap_php_conv_10() {{{ */
     319      932410 : PHPAPI char * ap_php_conv_10(register wide_int num, register bool_int is_unsigned,
     320             :            register bool_int * is_negative, char *buf_end, register size_t *len)
     321             : {
     322      932410 :         register char *p = buf_end;
     323             :         register u_wide_int magnitude;
     324             : 
     325      932410 :         if (is_unsigned) {
     326        3277 :                 magnitude = (u_wide_int) num;
     327        3277 :                 *is_negative = FALSE;
     328             :         } else {
     329      929133 :                 *is_negative = (num < 0);
     330             : 
     331             :                 /*
     332             :                  * On a 2's complement machine, negating the most negative integer
     333             :                  * results in a number that cannot be represented as a signed integer.
     334             :                  * Here is what we do to obtain the number's magnitude:
     335             :                  *      a. add 1 to the number
     336             :                  *      b. negate it (becomes positive)
     337             :                  *      c. convert it to unsigned
     338             :                  *      d. add 1
     339             :                  */
     340      929133 :                 if (*is_negative) {
     341        3473 :                         wide_int t = num + 1;
     342        3473 :                         magnitude = ((u_wide_int) - t) + 1;
     343             :                 } else {
     344      925660 :                         magnitude = (u_wide_int) num;
     345             :                 }
     346             :         }
     347             : 
     348             :         /*
     349             :          * We use a do-while loop so that we write at least 1 digit
     350             :          */
     351             :         do {
     352     2171737 :                 register u_wide_int new_magnitude = magnitude / 10;
     353             : 
     354     2171737 :                 *--p = (char)(magnitude - new_magnitude * 10 + '0');
     355     2171737 :                 magnitude = new_magnitude;
     356             :         }
     357     2171737 :         while (magnitude);
     358             : 
     359      932410 :         *len = buf_end - p;
     360      932410 :         return (p);
     361             : }
     362             : /* }}} */
     363             : 
     364             : /* If you change this value then also change bug24640.phpt.
     365             :  * Also NDIG must be reasonable smaller than NUM_BUF_SIZE.
     366             :  */
     367             : #define NDIG    320
     368             : 
     369             : 
     370             : /*
     371             :  * Convert a floating point number to a string formats 'f', 'e' or 'E'.
     372             :  * The result is placed in buf, and len denotes the length of the string
     373             :  * The sign is returned in the is_negative argument (and is not placed
     374             :  * in buf).
     375             :  */
     376             : /* PHPAPI char * php_conv_fp() {{{ */
     377      105437 : PHPAPI char * php_conv_fp(register char format, register double num,
     378             :                  boolean_e add_dp, int precision, char dec_point, bool_int * is_negative, char *buf, size_t *len)
     379             : {
     380      105437 :         register char *s = buf;
     381             :         register char *p, *p_orig;
     382             :         int decimal_point;
     383             : 
     384      105437 :         if (precision >= NDIG - 1) {
     385           2 :                 precision = NDIG - 2;
     386             :         }
     387             : 
     388      105437 :         if (format == 'F') {
     389      104454 :                 p_orig = p = php_fcvt(num, precision, &decimal_point, is_negative);
     390             :         } else {                                                /* either e or E format */
     391         983 :                 p_orig = p = php_ecvt(num, precision + 1, &decimal_point, is_negative);
     392             :         }
     393             : 
     394             :         /*
     395             :          * Check for Infinity and NaN
     396             :          */
     397      105437 :         if (isalpha((int)*p)) {
     398           0 :                 *len = strlen(p);
     399           0 :                 memcpy(buf, p, *len + 1);
     400           0 :                 *is_negative = FALSE;
     401           0 :                 free(p_orig);
     402           0 :                 return (buf);
     403             :         }
     404      105437 :         if (format == 'F') {
     405      104454 :                 if (decimal_point <= 0) {
     406      100496 :                         if (num != 0 || precision > 0) {
     407      100488 :                                 *s++ = '0';
     408      100488 :                                 if (precision > 0) {
     409      100488 :                                         *s++ = dec_point;
     410      212391 :                                         while (decimal_point++ < 0) {
     411       11415 :                                                 *s++ = '0';
     412             :                                         }
     413           0 :                                 } else if (add_dp) {
     414           0 :                                         *s++ = dec_point;
     415             :                                 }
     416             :                         }
     417             :                 } else {
     418        3958 :                         int addz = decimal_point >= NDIG ? decimal_point - NDIG + 1 : 0;
     419        3958 :                         decimal_point -= addz;
     420       25366 :                         while (decimal_point-- > 0) {
     421       17450 :                                 *s++ = *p++;
     422             :                         }
     423        7916 :                         while (addz-- > 0) {
     424           0 :                                 *s++ = '0';
     425             :                         }
     426        3958 :                         if (precision > 0 || add_dp) {
     427        2626 :                                 *s++ = dec_point;
     428             :                         }
     429             :                 }
     430             :         } else {
     431         983 :                 *s++ = *p++;
     432         983 :                 if (precision > 0 || add_dp) {
     433         982 :                         *s++ = '.';
     434             :                 }
     435             :         }
     436             : 
     437             :         /*
     438             :          * copy the rest of p, the NUL is NOT copied
     439             :          */
     440     1023954 :         while (*p) {
     441      813080 :                 *s++ = *p++;
     442             :         }
     443             : 
     444      105437 :         if (format != 'F') {
     445             :                 char temp[EXPONENT_LENGTH];             /* for exponent conversion */
     446             :                 size_t t_len;
     447             :                 bool_int exponent_is_negative;
     448             : 
     449         983 :                 *s++ = format;                  /* either e or E */
     450         983 :                 decimal_point--;
     451         983 :                 if (decimal_point != 0) {
     452         449 :                         p = ap_php_conv_10((wide_int) decimal_point, FALSE, &exponent_is_negative, &temp[EXPONENT_LENGTH], &t_len);
     453         449 :                         *s++ = exponent_is_negative ? '-' : '+';
     454             : 
     455             :                         /*
     456             :                          * Make sure the exponent has at least 2 digits
     457             :                          */
     458        1397 :                         while (t_len--) {
     459         499 :                                 *s++ = *p++;
     460             :                         }
     461             :                 } else {
     462         534 :                         *s++ = '+';
     463         534 :                         *s++ = '0';
     464             :                 }
     465             :         }
     466      105437 :         *len = s - buf;
     467      105437 :         free(p_orig);
     468      105437 :         return (buf);
     469             : }
     470             : /* }}} */
     471             : 
     472             : /*
     473             :  * Convert num to a base X number where X is a power of 2. nbits determines X.
     474             :  * For example, if nbits is 3, we do base 8 conversion
     475             :  * Return value:
     476             :  *      a pointer to a string containing the number
     477             :  *
     478             :  * The caller provides a buffer for the string: that is the buf_end argument
     479             :  * which is a pointer to the END of the buffer + 1 (i.e. if the buffer
     480             :  * is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
     481             :  */
     482       68978 : PHPAPI char * ap_php_conv_p2(register u_wide_int num, register int nbits, char format, char *buf_end, register size_t *len) /* {{{ */
     483             : {
     484       68978 :         register int mask = (1 << nbits) - 1;
     485       68978 :         register char *p = buf_end;
     486             :         static char low_digits[] = "0123456789abcdef";
     487             :         static char upper_digits[] = "0123456789ABCDEF";
     488       68978 :         register char *digits = (format == 'X') ? upper_digits : low_digits;
     489             : 
     490             :         do {
     491      487084 :                 *--p = digits[num & mask];
     492      487084 :                 num >>= nbits;
     493             :         }
     494      487084 :         while (num);
     495             : 
     496       68978 :         *len = buf_end - p;
     497       68978 :         return (p);
     498             : }
     499             : /* }}} */
     500             : 
     501             : /*
     502             :  * NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions
     503             :  *
     504             :  * XXX: this is a magic number; do not decrease it
     505             :  * Emax = 1023
     506             :  * NDIG = 320
     507             :  * NUM_BUF_SIZE >= strlen("-") + Emax + strlrn(".") + NDIG + strlen("E+1023") + 1;
     508             :  */
     509             : #define NUM_BUF_SIZE            2048
     510             : 
     511             : 
     512             : /*
     513             :  * Descriptor for buffer area
     514             :  */
     515             : struct buf_area {
     516             :         char *buf_end;
     517             :         char *nextb;                            /* pointer to next byte to read/write   */
     518             : };
     519             : 
     520             : typedef struct buf_area buffy;
     521             : 
     522             : /*
     523             :  * The INS_CHAR macro inserts a character in the buffer and writes
     524             :  * the buffer back to disk if necessary
     525             :  * It uses the char pointers sp and bep:
     526             :  *      sp points to the next available character in the buffer
     527             :  *      bep points to the end-of-buffer+1
     528             :  * While using this macro, note that the nextb pointer is NOT updated.
     529             :  *
     530             :  * NOTE: Evaluation of the c argument should not have any side-effects
     531             :  */
     532             : #define INS_CHAR(c, sp, bep, cc) \
     533             :         {                            \
     534             :                 if (sp < bep)            \
     535             :                 {                        \
     536             :                         *sp++ = c;           \
     537             :                 }                        \
     538             :                 cc++;                    \
     539             :         }
     540             : 
     541             : #define NUM( c )                        ( c - '0' )
     542             : 
     543             : #define STR_TO_DEC( str, num )          \
     544             :     num = NUM( *str++ ) ;               \
     545             :     while ( isdigit((int)*str ) )               \
     546             :     {                                   \
     547             :         num *= 10 ;                     \
     548             :         num += NUM( *str++ ) ;          \
     549             :     }
     550             : 
     551             : /*
     552             :  * This macro does zero padding so that the precision
     553             :  * requirement is satisfied. The padding is done by
     554             :  * adding '0's to the left of the string that is going
     555             :  * to be printed.
     556             :  */
     557             : #define FIX_PRECISION( adjust, precision, s, s_len )    \
     558             :     if ( adjust )                                       \
     559             :         while ( s_len < precision )                  \
     560             :         {                                               \
     561             :             *--s = '0' ;                                \
     562             :             s_len++ ;                                   \
     563             :         }
     564             : 
     565             : /*
     566             :  * Macro that does padding. The padding is done by printing
     567             :  * the character ch.
     568             :  */
     569             : #define PAD( width, len, ch )   do              \
     570             :         {                                       \
     571             :             INS_CHAR( ch, sp, bep, cc ) ;       \
     572             :             width-- ;                           \
     573             :         }                                       \
     574             :         while ( width > len )
     575             : 
     576             : /*
     577             :  * Prefix the character ch to the string str
     578             :  * Increase length
     579             :  * Set the has_prefix flag
     580             :  */
     581             : #define PREFIX( str, length, ch )        *--str = ch ; length++ ; has_prefix = YES
     582             : 
     583             : 
     584             : /*
     585             :  * Do format conversion placing the output in buffer
     586             :  */
     587     1380168 : static int format_converter(register buffy * odp, const char *fmt, va_list ap) /* {{{ */
     588             : {
     589             :         char *sp;
     590             :         char *bep;
     591     1380168 :         int cc = 0;
     592             :         size_t i;
     593             : 
     594     1380168 :         char *s = NULL;
     595             :         size_t s_len;
     596             :         int free_zcopy;
     597             :         zval *zvp, zcopy;
     598             : 
     599     1380168 :         int min_width = 0;
     600     1380168 :         int precision = 0;
     601             :         enum {
     602             :                 LEFT, RIGHT
     603             :         } adjust;
     604             :         char pad_char;
     605             :         char prefix_char;
     606             : 
     607             :         double fp_num;
     608     1380168 :         wide_int i_num = (wide_int) 0;
     609             :         u_wide_int ui_num;
     610             : 
     611             :         char num_buf[NUM_BUF_SIZE];
     612             :         char char_buf[2];                       /* for printing %% and %<unknown> */
     613             : 
     614             : #ifdef HAVE_LOCALE_H
     615             : #ifdef ZTS
     616             :         struct lconv lconv;
     617             : #else
     618     1380168 :         struct lconv *lconv = NULL;
     619             : #endif
     620             : #endif
     621             : 
     622             :         /*
     623             :          * Flag variables
     624             :          */
     625             :         length_modifier_e modifier;
     626             :         boolean_e alternate_form;
     627             :         boolean_e print_sign;
     628             :         boolean_e print_blank;
     629             :         boolean_e adjust_precision;
     630             :         boolean_e adjust_width;
     631             :         bool_int is_negative;
     632             : 
     633     1380168 :         sp = odp->nextb;
     634     1380168 :         bep = odp->buf_end;
     635             : 
     636     8469362 :         while (*fmt) {
     637     5709026 :                 if (*fmt != '%') {
     638     4160478 :                         INS_CHAR(*fmt, sp, bep, cc);
     639             :                 } else {
     640             :                         /*
     641             :                          * Default variable settings
     642             :                          */
     643     1548548 :                         adjust = RIGHT;
     644     1548548 :                         alternate_form = print_sign = print_blank = NO;
     645     1548548 :                         pad_char = ' ';
     646     1548548 :                         prefix_char = NUL;
     647     1548548 :                         free_zcopy = 0;
     648             : 
     649     1548548 :                         fmt++;
     650             : 
     651             :                         /*
     652             :                          * Try to avoid checking for flags, width or precision
     653             :                          */
     654     1689173 :                         if (isascii((int)*fmt) && !islower((int)*fmt)) {
     655             :                                 /*
     656             :                                  * Recognize flags: -, #, BLANK, +
     657             :                                  */
     658       39419 :                                 for (;; fmt++) {
     659      180044 :                                         if (*fmt == '-')
     660           1 :                                                 adjust = LEFT;
     661      180043 :                                         else if (*fmt == '+')
     662           5 :                                                 print_sign = YES;
     663      180038 :                                         else if (*fmt == '#')
     664           0 :                                                 alternate_form = YES;
     665      180038 :                                         else if (*fmt == ' ')
     666           0 :                                                 print_blank = YES;
     667      180038 :                                         else if (*fmt == '0')
     668       39413 :                                                 pad_char = '0';
     669             :                                         else
     670      140625 :                                                 break;
     671       39419 :                                 }
     672             : 
     673             :                                 /*
     674             :                                  * Check if a width was specified
     675             :                                  */
     676      140625 :                                 if (isdigit((int)*fmt)) {
     677       39311 :                                         STR_TO_DEC(fmt, min_width);
     678       39311 :                                         adjust_width = YES;
     679      101314 :                                 } else if (*fmt == '*') {
     680           0 :                                         min_width = va_arg(ap, int);
     681           0 :                                         fmt++;
     682           0 :                                         adjust_width = YES;
     683           0 :                                         if (min_width < 0) {
     684           0 :                                                 adjust = LEFT;
     685           0 :                                                 min_width = -min_width;
     686             :                                         }
     687             :                                 } else
     688      101314 :                                         adjust_width = NO;
     689             : 
     690             :                                 /*
     691             :                                  * Check if a precision was specified
     692             :                                  */
     693      140625 :                                 if (*fmt == '.') {
     694      101310 :                                         adjust_precision = YES;
     695      101310 :                                         fmt++;
     696      101310 :                                         if (isdigit((int)*fmt)) {
     697      100241 :                                                 STR_TO_DEC(fmt, precision);
     698        1069 :                                         } else if (*fmt == '*') {
     699        1069 :                                                 precision = va_arg(ap, int);
     700        1069 :                                                 fmt++;
     701        1069 :                                                 if (precision < 0)
     702           0 :                                                         precision = 0;
     703             :                                         } else
     704           0 :                                                 precision = 0;
     705             : 
     706      101310 :                                         if (precision > FORMAT_CONV_MAX_PRECISION) {
     707           0 :                                                 precision = FORMAT_CONV_MAX_PRECISION;
     708             :                                         }
     709             :                                 } else
     710       39315 :                                         adjust_precision = NO;
     711             :                         } else
     712     1407923 :                                 adjust_precision = adjust_width = NO;
     713             : 
     714             :                         /*
     715             :                          * Modifier check
     716             :                          */
     717     1548548 :                         switch (*fmt) {
     718             :                                 case 'L':
     719           0 :                                         fmt++;
     720           0 :                                         modifier = LM_LONG_DOUBLE;
     721           0 :                                         break;
     722             :                                 case 'I':
     723           2 :                                         fmt++;
     724             : #if SIZEOF_LONG_LONG
     725           4 :                                         if (*fmt == '6' && *(fmt+1) == '4') {
     726           2 :                                                 fmt += 2;
     727           2 :                                                 modifier = LM_LONG_LONG;
     728             :                                         } else
     729             : #endif
     730           0 :                                                 if (*fmt == '3' && *(fmt+1) == '2') {
     731           0 :                                                         fmt += 2;
     732           0 :                                                         modifier = LM_LONG;
     733             :                                                 } else {
     734             : #ifdef _WIN64
     735             :                                                         modifier = LM_LONG_LONG;
     736             : #else
     737           0 :                                                         modifier = LM_LONG;
     738             : #endif
     739             :                                                 }
     740           2 :                                         break;
     741             :                                 case 'l':
     742      119129 :                                         fmt++;
     743             : #if SIZEOF_LONG_LONG
     744      119129 :                                         if (*fmt == 'l') {
     745        5776 :                                                 fmt++;
     746        5776 :                                                 modifier = LM_LONG_LONG;
     747             :                                         } else
     748             : #endif
     749      113353 :                                                 modifier = LM_LONG;
     750      119129 :                                         break;
     751             :                                 case 'z':
     752          13 :                                         fmt++;
     753          13 :                                         modifier = LM_SIZE_T;
     754          13 :                                         break;
     755             :                                 case 'j':
     756           0 :                                         fmt++;
     757             : #if SIZEOF_INTMAX_T
     758           0 :                                         modifier = LM_INTMAX_T;
     759             : #else
     760             :                                         modifier = LM_SIZE_T;
     761             : #endif
     762           0 :                                         break;
     763             :                                 case 't':
     764           0 :                                         fmt++;
     765             : #if SIZEOF_PTRDIFF_T
     766           0 :                                         modifier = LM_PTRDIFF_T;
     767             : #else
     768             :                                         modifier = LM_SIZE_T;
     769             : #endif
     770           0 :                                         break;
     771             :                                 case 'p':
     772          11 :                                         fmt++;
     773          11 :                                         modifier = LM_PHP_INT_T;
     774          11 :                                         break;
     775             :                                 case 'h':
     776           0 :                                         fmt++;
     777           0 :                                         if (*fmt == 'h') {
     778           0 :                                                 fmt++;
     779             :                                         }
     780             :                                         /* these are promoted to int, so no break */
     781             :                                 default:
     782     1429393 :                                         modifier = LM_STD;
     783             :                                         break;
     784             :                         }
     785             : 
     786             :                         /*
     787             :                          * Argument extraction and printing.
     788             :                          * First we determine the argument type.
     789             :                          * Then, we convert the argument to a string.
     790             :                          * On exit from the switch, s points to the string that
     791             :                          * must be printed, s_len has the length of the string
     792             :                          * The precision requirements, if any, are reflected in s_len.
     793             :                          *
     794             :                          * NOTE: pad_char may be set to '0' because of the 0 flag.
     795             :                          *   It is reset to ' ' by non-numeric formats
     796             :                          */
     797     1548548 :                         switch (*fmt) {
     798             :                                 case 'Z': {
     799           0 :                                                                     zvp = (zval*) va_arg(ap, zval*);
     800           0 :                                         free_zcopy = zend_make_printable_zval(zvp, &zcopy);
     801           0 :                                         if (free_zcopy) {
     802           0 :                                                 zvp = &zcopy;
     803             :                                         }
     804           0 :                                         s_len = Z_STRLEN_P(zvp);
     805           0 :                                         s = Z_STRVAL_P(zvp);
     806           0 :                                         if (adjust_precision && precision < s_len) {
     807           0 :                                                 s_len = precision;
     808             :                                         }
     809           0 :                                         break;
     810             :                                 }
     811             :                                 case 'u':
     812        2478 :                                         switch(modifier) {
     813             :                                                 default:
     814         799 :                                                         i_num = (wide_int) va_arg(ap, unsigned int);
     815         799 :                                                         break;
     816             :                                                 case LM_LONG_DOUBLE:
     817           0 :                                                         goto fmt_error;
     818             :                                                 case LM_LONG:
     819        1666 :                                                         i_num = (wide_int) va_arg(ap, unsigned long int);
     820        1666 :                                                         break;
     821             :                                                 case LM_SIZE_T:
     822          13 :                                                         i_num = (wide_int) va_arg(ap, size_t);
     823          13 :                                                         break;
     824             : #if SIZEOF_LONG_LONG
     825             :                                                 case LM_LONG_LONG:
     826           0 :                                                         i_num = (wide_int) va_arg(ap, u_wide_int);
     827           0 :                                                         break;
     828             : #endif
     829             : #if SIZEOF_INTMAX_T
     830             :                                                 case LM_INTMAX_T:
     831           0 :                                                         i_num = (wide_int) va_arg(ap, uintmax_t);
     832           0 :                                                         break;
     833             : #endif
     834             : #if SIZEOF_PTRDIFF_T
     835             :                                                 case LM_PTRDIFF_T:
     836           0 :                                                         i_num = (wide_int) va_arg(ap, ptrdiff_t);
     837           0 :                                                         break;
     838             : #endif
     839             :                                                 case LM_PHP_INT_T:
     840           0 :                                                         i_num = (wide_int) va_arg(ap, zend_ulong);
     841             :                                                         break;
     842             :                                         }
     843             :                                         /*
     844             :                                          * The rest also applies to other integer formats, so fall
     845             :                                          * into that case.
     846             :                                          */
     847             :                                 case 'd':
     848             :                                 case 'i':
     849             :                                         /*
     850             :                                          * Get the arg if we haven't already.
     851             :                                          */
     852      682217 :                                         if ((*fmt) != 'u') {
     853      679739 :                                                 switch(modifier) {
     854             :                                                         default:
     855      562290 :                                                                 i_num = (wide_int) va_arg(ap, int);
     856      562290 :                                                                 break;
     857             :                                                         case LM_LONG_DOUBLE:
     858           0 :                                                                 goto fmt_error;
     859             :                                                         case LM_LONG:
     860      111660 :                                                                 i_num = (wide_int) va_arg(ap, long int);
     861      111660 :                                                                 break;
     862             :                                                         case LM_SIZE_T:
     863             : #if SIZEOF_SSIZE_T
     864           0 :                                                                 i_num = (wide_int) va_arg(ap, ssize_t);
     865             : #else
     866             :                                                                 i_num = (wide_int) va_arg(ap, size_t);
     867             : #endif
     868           0 :                                                                 break;
     869             : #if SIZEOF_LONG_LONG
     870             :                                                         case LM_LONG_LONG:
     871        5778 :                                                                 i_num = (wide_int) va_arg(ap, wide_int);
     872        5778 :                                                                 break;
     873             : #endif
     874             : #if SIZEOF_INTMAX_T
     875             :                                                         case LM_INTMAX_T:
     876           0 :                                                                 i_num = (wide_int) va_arg(ap, intmax_t);
     877           0 :                                                                 break;
     878             : #endif
     879             : #if SIZEOF_PTRDIFF_T
     880             :                                                         case LM_PTRDIFF_T:
     881           0 :                                                                 i_num = (wide_int) va_arg(ap, ptrdiff_t);
     882           0 :                                                                 break;
     883             : #endif
     884             :                                                         case LM_PHP_INT_T:
     885          11 :                                                                 i_num = (wide_int) va_arg(ap, zend_long);
     886             :                                                                 break;
     887             :                                                 }
     888             :                                         }
     889      682217 :                                         s = ap_php_conv_10(i_num, (*fmt) == 'u', &is_negative,
     890             :                                                                 &num_buf[NUM_BUF_SIZE], &s_len);
     891      682217 :                                         FIX_PRECISION(adjust_precision, precision, s, s_len);
     892             : 
     893      682217 :                                         if (*fmt != 'u') {
     894      679739 :                                                 if (is_negative) {
     895         535 :                                                         prefix_char = '-';
     896      679204 :                                                 } else if (print_sign) {
     897           4 :                                                         prefix_char = '+';
     898      679200 :                                                 } else if (print_blank) {
     899           0 :                                                         prefix_char = ' ';
     900             :                                                 }
     901             :                                         }
     902      682217 :                                         break;
     903             : 
     904             : 
     905             :                                 case 'o':
     906           0 :                                         switch(modifier) {
     907             :                                                 default:
     908           0 :                                                         ui_num = (u_wide_int) va_arg(ap, unsigned int);
     909           0 :                                                         break;
     910             :                                                 case LM_LONG_DOUBLE:
     911           0 :                                                         goto fmt_error;
     912             :                                                 case LM_LONG:
     913           0 :                                                         ui_num = (u_wide_int) va_arg(ap, unsigned long int);
     914           0 :                                                         break;
     915             :                                                 case LM_SIZE_T:
     916           0 :                                                         ui_num = (u_wide_int) va_arg(ap, size_t);
     917           0 :                                                         break;
     918             : #if SIZEOF_LONG_LONG
     919             :                                                 case LM_LONG_LONG:
     920           0 :                                                         ui_num = (u_wide_int) va_arg(ap, u_wide_int);
     921           0 :                                                         break;
     922             : #endif
     923             : #if SIZEOF_INTMAX_T
     924             :                                                 case LM_INTMAX_T:
     925           0 :                                                         ui_num = (u_wide_int) va_arg(ap, uintmax_t);
     926           0 :                                                         break;
     927             : #endif
     928             : #if SIZEOF_PTRDIFF_T
     929             :                                                 case LM_PTRDIFF_T:
     930           0 :                                                         ui_num = (u_wide_int) va_arg(ap, ptrdiff_t);
     931           0 :                                                         break;
     932             : #endif
     933             :                                                 case LM_PHP_INT_T:
     934           0 :                                                         ui_num = (u_wide_int) va_arg(ap, zend_ulong);
     935             :                                                         break;
     936             :                                         }
     937           0 :                                         s = ap_php_conv_p2(ui_num, 3, *fmt, &num_buf[NUM_BUF_SIZE], &s_len);
     938           0 :                                         FIX_PRECISION(adjust_precision, precision, s, s_len);
     939           0 :                                         if (alternate_form && *s != '0') {
     940           0 :                                                 *--s = '0';
     941           0 :                                                 s_len++;
     942             :                                         }
     943           0 :                                         break;
     944             : 
     945             : 
     946             :                                 case 'x':
     947             :                                 case 'X':
     948         199 :                                         switch(modifier) {
     949             :                                                 default:
     950         172 :                                                         ui_num = (u_wide_int) va_arg(ap, unsigned int);
     951         172 :                                                         break;
     952             :                                                 case LM_LONG_DOUBLE:
     953           0 :                                                         goto fmt_error;
     954             :                                                 case LM_LONG:
     955          27 :                                                         ui_num = (u_wide_int) va_arg(ap, unsigned long int);
     956          27 :                                                         break;
     957             :                                                 case LM_SIZE_T:
     958           0 :                                                         ui_num = (u_wide_int) va_arg(ap, size_t);
     959           0 :                                                         break;
     960             : #if SIZEOF_LONG_LONG
     961             :                                                 case LM_LONG_LONG:
     962           0 :                                                         ui_num = (u_wide_int) va_arg(ap, u_wide_int);
     963           0 :                                                         break;
     964             : #endif
     965             : #if SIZEOF_INTMAX_T
     966             :                                                 case LM_INTMAX_T:
     967           0 :                                                         ui_num = (u_wide_int) va_arg(ap, uintmax_t);
     968           0 :                                                         break;
     969             : #endif
     970             : #if SIZEOF_PTRDIFF_T
     971             :                                                 case LM_PTRDIFF_T:
     972           0 :                                                         ui_num = (u_wide_int) va_arg(ap, ptrdiff_t);
     973           0 :                                                         break;
     974             : #endif
     975             :                                                 case LM_PHP_INT_T:
     976           0 :                                                         ui_num = (u_wide_int) va_arg(ap, zend_ulong);
     977             :                                                         break;
     978             :                                         }
     979         199 :                                         s = ap_php_conv_p2(ui_num, 4, *fmt, &num_buf[NUM_BUF_SIZE], &s_len);
     980         199 :                                         FIX_PRECISION(adjust_precision, precision, s, s_len);
     981         199 :                                         if (alternate_form && i_num != 0) {
     982           0 :                                                 *--s = *fmt;    /* 'x' or 'X' */
     983           0 :                                                 *--s = '0';
     984           0 :                                                 s_len += 2;
     985             :                                         }
     986         199 :                                         break;
     987             : 
     988             : 
     989             :                                 case 's':
     990             :                                 case 'v':
     991      763704 :                                         s = va_arg(ap, char *);
     992      763704 :                                         if (s != NULL) {
     993      763704 :                                                 s_len = strlen(s);
     994      763704 :                                                 if (adjust_precision && precision < s_len) {
     995          13 :                                                         s_len = precision;
     996             :                                                 }
     997             :                                         } else {
     998           0 :                                                 s = S_NULL;
     999           0 :                                                 s_len = S_NULL_LEN;
    1000             :                                         }
    1001      763704 :                                         pad_char = ' ';
    1002      763704 :                                         break;
    1003             : 
    1004             : 
    1005             :                                 case 'f':
    1006             :                                 case 'F':
    1007             :                                 case 'e':
    1008             :                                 case 'E':
    1009      100109 :                                         switch(modifier) {
    1010             :                                                 case LM_LONG_DOUBLE:
    1011           0 :                                                         fp_num = (double) va_arg(ap, long double);
    1012           0 :                                                         break;
    1013             :                                                 case LM_STD:
    1014      100109 :                                                         fp_num = va_arg(ap, double);
    1015      100109 :                                                         break;
    1016             :                                                 default:
    1017           0 :                                                         goto fmt_error;
    1018             :                                         }
    1019             : 
    1020      100109 :                                         if (zend_isnan(fp_num)) {
    1021           0 :                                                 s = "NAN";
    1022           0 :                                                 s_len = 3;
    1023      100109 :                                         } else if (zend_isinf(fp_num)) {
    1024           0 :                                                 s = "INF";
    1025           0 :                                                 s_len = 3;
    1026             :                                         } else {
    1027             : #ifdef HAVE_LOCALE_H
    1028             : #ifdef ZTS
    1029             :                                                 localeconv_r(&lconv);
    1030             : #else
    1031      100109 :                                                 if (!lconv) {
    1032      100109 :                                                         lconv = localeconv();
    1033             :                                                 }
    1034             : #endif
    1035             : #endif
    1036      100121 :                                                 s = php_conv_fp((*fmt == 'f')?'F':*fmt, fp_num, alternate_form,
    1037             :                                                  (adjust_precision == NO) ? FLOAT_DIGITS : precision,
    1038      100121 :                                                  (*fmt == 'f')?LCONV_DECIMAL_POINT:'.',
    1039             :                                                                         &is_negative, &num_buf[1], &s_len);
    1040      100109 :                                                 if (is_negative)
    1041           0 :                                                         prefix_char = '-';
    1042      100109 :                                                 else if (print_sign)
    1043           0 :                                                         prefix_char = '+';
    1044      100109 :                                                 else if (print_blank)
    1045           0 :                                                         prefix_char = ' ';
    1046             :                                         }
    1047      100109 :                                         break;
    1048             : 
    1049             : 
    1050             :                                 case 'g':
    1051             :                                 case 'k':
    1052             :                                 case 'G':
    1053             :                                 case 'H':
    1054           3 :                                         switch(modifier) {
    1055             :                                                 case LM_LONG_DOUBLE:
    1056           0 :                                                         fp_num = (double) va_arg(ap, long double);
    1057           0 :                                                         break;
    1058             :                                                 case LM_STD:
    1059           3 :                                                         fp_num = va_arg(ap, double);
    1060           3 :                                                         break;
    1061             :                                                 default:
    1062           0 :                                                         goto fmt_error;
    1063             :                                         }
    1064             : 
    1065           3 :                                         if (zend_isnan(fp_num)) {
    1066           0 :                                                 s = "NAN";
    1067           0 :                                                 s_len = 3;
    1068           0 :                                                 break;
    1069           3 :                                         } else if (zend_isinf(fp_num)) {
    1070           0 :                                                 if (fp_num > 0) {
    1071           0 :                                                         s = "INF";
    1072           0 :                                                         s_len = 3;
    1073             :                                                 } else {
    1074           0 :                                                         s = "-INF";
    1075           0 :                                                         s_len = 4;
    1076             :                                                 }
    1077           0 :                                                 break;
    1078             :                                         }
    1079             : 
    1080           3 :                                         if (adjust_precision == NO) {
    1081           0 :                                                 precision = FLOAT_DIGITS;
    1082           3 :                                         } else if (precision == 0) {
    1083           0 :                                                 precision = 1;
    1084             :                                         }
    1085             :                                         /*
    1086             :                                          * * We use &num_buf[ 1 ], so that we have room for the sign
    1087             :                                          */
    1088             : #ifdef HAVE_LOCALE_H
    1089             : #ifdef ZTS
    1090             :                                         localeconv_r(&lconv);
    1091             : #else
    1092           3 :                                         if (!lconv) {
    1093           3 :                                                 lconv = localeconv();
    1094             :                                         }
    1095             : #endif
    1096             : #endif
    1097           3 :                                         s = php_gcvt(fp_num, precision, (*fmt=='H' || *fmt == 'k') ? '.' : LCONV_DECIMAL_POINT, (*fmt == 'G' || *fmt == 'H')?'E':'e', &num_buf[1]);
    1098           3 :                                         if (*s == '-') {
    1099           1 :                                                 prefix_char = *s++;
    1100           2 :                                         } else if (print_sign) {
    1101           0 :                                                 prefix_char = '+';
    1102           2 :                                         } else if (print_blank) {
    1103           0 :                                                 prefix_char = ' ';
    1104             :                                         }
    1105             : 
    1106           3 :                                         s_len = strlen(s);
    1107             : 
    1108           3 :                                         if (alternate_form && (strchr(s, '.')) == NULL) {
    1109           0 :                                                 s[s_len++] = '.';
    1110             :                                         }
    1111           3 :                                         break;
    1112             : 
    1113             : 
    1114             :                                 case 'c':
    1115        2315 :                                         char_buf[0] = (char) (va_arg(ap, int));
    1116        2315 :                                         s = &char_buf[0];
    1117        2315 :                                         s_len = 1;
    1118        2315 :                                         pad_char = ' ';
    1119        2315 :                                         break;
    1120             : 
    1121             : 
    1122             :                                 case '%':
    1123           1 :                                         char_buf[0] = '%';
    1124           1 :                                         s = &char_buf[0];
    1125           1 :                                         s_len = 1;
    1126           1 :                                         pad_char = ' ';
    1127           1 :                                         break;
    1128             : 
    1129             : 
    1130             :                                 case 'n':
    1131           0 :                                         *(va_arg(ap, int *)) = cc;
    1132           0 :                                         goto skip_output;
    1133             : 
    1134             :                                         /*
    1135             :                                          * Always extract the argument as a "char *" pointer. We
    1136             :                                          * should be using "void *" but there are still machines
    1137             :                                          * that don't understand it.
    1138             :                                          * If the pointer size is equal to the size of an unsigned
    1139             :                                          * integer we convert the pointer to a hex number, otherwise
    1140             :                                          * we print "%p" to indicate that we don't handle "%p".
    1141             :                                          */
    1142             :                                 case 'p':
    1143             :                                         if (sizeof(char *) <= sizeof(u_wide_int)) {
    1144           0 :                                                 ui_num = (u_wide_int)((size_t) va_arg(ap, char *));
    1145           0 :                                                 s = ap_php_conv_p2(ui_num, 4, 'x',
    1146             :                                                                 &num_buf[NUM_BUF_SIZE], &s_len);
    1147           0 :                                                 if (ui_num != 0) {
    1148           0 :                                                         *--s = 'x';
    1149           0 :                                                         *--s = '0';
    1150           0 :                                                         s_len += 2;
    1151             :                                                 }
    1152             :                                         } else {
    1153             :                                                 s = "%p";
    1154             :                                                 s_len = 2;
    1155             :                                         }
    1156           0 :                                         pad_char = ' ';
    1157           0 :                                         break;
    1158             : 
    1159             : 
    1160             :                                 case NUL:
    1161             :                                         /*
    1162             :                                          * The last character of the format string was %.
    1163             :                                          * We ignore it.
    1164             :                                          */
    1165           0 :                                         continue;
    1166             : 
    1167             : 
    1168             : fmt_error:
    1169           0 :                                 php_error(E_ERROR, "Illegal length modifier specified '%c' in s[np]printf call", *fmt);
    1170             :                                         /*
    1171             :                                          * The default case is for unrecognized %'s.
    1172             :                                          * We print %<char> to help the user identify what
    1173             :                                          * option is not understood.
    1174             :                                          * This is also useful in case the user wants to pass
    1175             :                                          * the output of format_converter to another function
    1176             :                                          * that understands some other %<char> (like syslog).
    1177             :                                          * Note that we can't point s inside fmt because the
    1178             :                                          * unknown <char> could be preceded by width etc.
    1179             :                                          */
    1180             :                                 default:
    1181           0 :                                         char_buf[0] = '%';
    1182           0 :                                         char_buf[1] = *fmt;
    1183           0 :                                         s = char_buf;
    1184           0 :                                         s_len = 2;
    1185           0 :                                         pad_char = ' ';
    1186             :                                         break;
    1187             :                         }
    1188             : 
    1189     1548548 :                         if (prefix_char != NUL) {
    1190         540 :                                 *--s = prefix_char;
    1191         540 :                                 s_len++;
    1192             :                         }
    1193     1548548 :                         if (adjust_width && adjust == RIGHT && min_width > s_len) {
    1194       19554 :                                 if (pad_char == '0' && prefix_char != NUL) {
    1195           5 :                                         INS_CHAR(*s, sp, bep, cc)
    1196           5 :                                                 s++;
    1197           5 :                                         s_len--;
    1198           5 :                                         min_width--;
    1199             :                                 }
    1200       23547 :                                 PAD(min_width, s_len, pad_char);
    1201             :                         }
    1202             :                         /*
    1203             :                          * Print the string s.
    1204             :                          */
    1205     9410883 :                         for (i = s_len; i != 0; i--) {
    1206     7862335 :                                 INS_CHAR(*s, sp, bep, cc);
    1207     7862335 :                                 s++;
    1208             :                         }
    1209             : 
    1210     1548548 :                         if (adjust_width && adjust == LEFT && min_width > s_len)
    1211           0 :                                 PAD(min_width, s_len, pad_char);
    1212     1548548 :                         if (free_zcopy) {
    1213             :                                 zval_dtor(&zcopy);
    1214             :                         }
    1215             :                 }
    1216             : skip_output:
    1217     5709026 :                 fmt++;
    1218             :         }
    1219     1380168 :         odp->nextb = sp;
    1220     1380168 :         return (cc);
    1221             : }
    1222             : /* }}} */
    1223             : 
    1224             : /*
    1225             :  * This is the general purpose conversion function.
    1226             :  */
    1227     1380168 : static void strx_printv(int *ccp, char *buf, size_t len, const char *format, va_list ap) /* {{{ */
    1228             : {
    1229             :         buffy od;
    1230             :         int cc;
    1231             : 
    1232             :         /*
    1233             :          * First initialize the descriptor
    1234             :          * Notice that if no length is given, we initialize buf_end to the
    1235             :          * highest possible address.
    1236             :          */
    1237     1380168 :         if (len == 0) {
    1238           0 :                 od.buf_end = (char *) ~0;
    1239           0 :                 od.nextb   = (char *) ~0;
    1240             :         } else {
    1241     1380168 :                 od.buf_end = &buf[len-1];
    1242     1380168 :                 od.nextb   = buf;
    1243             :         }
    1244             : 
    1245             :         /*
    1246             :          * Do the conversion
    1247             :          */
    1248     1380168 :         cc = format_converter(&od, format, ap);
    1249     1380168 :         if (len != 0 && od.nextb <= od.buf_end) {
    1250     1380168 :                 *(od.nextb) = '\0';
    1251             :         }
    1252     1380168 :         if (ccp) {
    1253     1380168 :                 *ccp = cc;
    1254             :         }
    1255     1380168 : }
    1256             : /* }}} */
    1257             : 
    1258     1224068 : PHPAPI int ap_php_slprintf(char *buf, size_t len, const char *format,...) /* {{{ */
    1259             : {
    1260             :         int cc;
    1261             :         va_list ap;
    1262             : 
    1263     1224068 :         va_start(ap, format);
    1264     1224068 :         strx_printv(&cc, buf, len, format, ap);
    1265     1224068 :         va_end(ap);
    1266     1224068 :         if (cc >= len) {
    1267           0 :                 cc = (int)len -1;
    1268           0 :                 buf[cc] = '\0';
    1269             :         }
    1270     1224068 :         return cc;
    1271             : }
    1272             : /* }}} */
    1273             : 
    1274          13 : PHPAPI int ap_php_vslprintf(char *buf, size_t len, const char *format, va_list ap) /* {{{ */
    1275             : {
    1276             :         int cc;
    1277             : 
    1278          13 :         strx_printv(&cc, buf, len, format, ap);
    1279          13 :         if (cc >= len) {
    1280           0 :                 cc = (int)len -1;
    1281           0 :                 buf[cc] = '\0';
    1282             :         }
    1283          13 :         return cc;
    1284             : }
    1285             : /* }}} */
    1286             : 
    1287      156061 : PHPAPI int ap_php_snprintf(char *buf, size_t len, const char *format,...) /* {{{ */
    1288             : {
    1289             :         int cc;
    1290             :         va_list ap;
    1291             : 
    1292      156061 :         va_start(ap, format);
    1293      156061 :         strx_printv(&cc, buf, len, format, ap);
    1294      156061 :         va_end(ap);
    1295      156061 :         return (cc);
    1296             : }
    1297             : /* }}} */
    1298             : 
    1299          26 : PHPAPI int ap_php_vsnprintf(char *buf, size_t len, const char *format, va_list ap) /* {{{ */
    1300             : {
    1301             :         int cc;
    1302             : 
    1303          26 :         strx_printv(&cc, buf, len, format, ap);
    1304          26 :         return (cc);
    1305             : }
    1306             : /* }}} */
    1307             : 
    1308           0 : PHPAPI int ap_php_vasprintf(char **buf, const char *format, va_list ap) /* {{{ */
    1309             : {
    1310             :         va_list ap2;
    1311             :         int cc;
    1312             : 
    1313           0 :         va_copy(ap2, ap);
    1314           0 :         cc = ap_php_vsnprintf(NULL, 0, format, ap2);
    1315           0 :         va_end(ap2);
    1316             : 
    1317           0 :         *buf = NULL;
    1318             : 
    1319           0 :         if (cc >= 0) {
    1320           0 :                 if ((*buf = malloc(++cc)) != NULL) {
    1321           0 :                         if ((cc = ap_php_vsnprintf(*buf, cc, format, ap)) < 0) {
    1322           0 :                                 free(*buf);
    1323           0 :                                 *buf = NULL;
    1324             :                         }
    1325             :                 }
    1326             :         }
    1327             : 
    1328           0 :         return cc;
    1329             : }
    1330             : /* }}} */
    1331             : 
    1332           0 : PHPAPI int ap_php_asprintf(char **buf, const char *format, ...) /* {{{ */
    1333             : {
    1334             :         int cc;
    1335             :         va_list ap;
    1336             : 
    1337           0 :         va_start(ap, format);
    1338           0 :         cc = vasprintf(buf, format, ap);
    1339           0 :         va_end(ap);
    1340           0 :         return cc;
    1341             : }
    1342             : /* }}} */
    1343             : 
    1344             : /*
    1345             :  * Local variables:
    1346             :  * tab-width: 4
    1347             :  * c-basic-offset: 4
    1348             :  * End:
    1349             :  * vim600: sw=4 ts=4 fdm=marker
    1350             :  * vim<600: sw=4 ts=4
    1351             :  */

Generated by: LCOV version 1.10

Generated at Fri, 31 Jul 2015 08:59:03 +0000 (3 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.