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: 2014-12-13 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-2014 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      105476 : static char * __cvt(double value, int ndigit, int *decpt, int *sign, int fmode, int pad) /* {{{ */
      72             : {
      73      105476 :         register char *s = NULL;
      74             :         char *p, *rve, c;
      75             :         size_t siz;
      76             : 
      77      105476 :         if (ndigit < 0) {
      78           0 :                 siz = -ndigit + 1;
      79             :         } else {
      80      105476 :                 siz = ndigit + 1;
      81             :         }
      82             : 
      83             :         /* __dtoa() doesn't allocate space for 0 so we do it by hand */
      84      105476 :         if (value == 0.0) {
      85         504 :                 *decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */
      86         504 :                 *sign = 0;
      87         504 :                 if ((rve = s = (char *)malloc(ndigit?siz:2)) == NULL) {
      88           0 :                         return(NULL);
      89             :                 }
      90         504 :                 *rve++ = '0';
      91         504 :                 *rve = '\0';
      92         504 :                 if (!ndigit) {
      93           6 :                         return(s);
      94             :                 }
      95             :         } else {
      96      104972 :                 p = zend_dtoa(value, fmode + 2, ndigit, decpt, sign, &rve);
      97      104972 :                 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      104972 :                 if (pad && fmode) {
     106      104220 :                         siz += *decpt;
     107             :                 }
     108      104972 :                 if ((s = (char *)malloc(siz+1)) == NULL) {
     109           0 :                         zend_freedtoa(p);
     110           0 :                         return(NULL);
     111             :                 }
     112      104972 :                 (void) strlcpy(s, p, siz);
     113      104972 :                 rve = s + (rve - p);
     114      104972 :                 zend_freedtoa(p);
     115             :         }
     116             : 
     117             :         /* Add trailing zeros */
     118      105470 :         if (pad) {
     119      105470 :                 siz -= rve - s;
     120      442980 :                 while (--siz) {
     121      232040 :                         *rve++ = '0';
     122             :                 }
     123      105470 :                 *rve = '\0';
     124             :         }
     125             : 
     126      105470 :         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      104493 : static inline char *php_fcvt(double value, int ndigit, int *decpt, int *sign) /* {{{ */
     137             : {
     138      104493 :     return(__cvt(value, ndigit, decpt, sign, 1, 1));
     139             : }
     140             : /* }}} */
     141             : 
     142       15893 : 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       15893 :         digits = zend_dtoa(value, 2, ndigit, &decpt, &sign, NULL);
     148       15893 :         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       15881 :         dst = buf;
     159       15881 :         if (sign) {
     160        3277 :                 *dst++ = '-';
     161             :         }
     162             : 
     163       18294 :         if ((decpt >= 0 && decpt > ndigit) || decpt < -3) { /* use E-style */
     164             :                 /* exponential format (e.g. 1.2345e+13) */
     165        2413 :                 if (--decpt < 0) {
     166        1256 :                         sign = 1;
     167        1256 :                         decpt = -decpt;
     168             :                 } else {
     169        1157 :                         sign = 0;
     170             :                 }
     171        2413 :                 src = digits;
     172        2413 :                 *dst++ = *src++;
     173        2413 :                 *dst++ = dec_point;
     174        2413 :                 if (*src == '\0') {
     175         146 :                         *dst++ = '0';
     176             :                 } else {
     177             :                         do {
     178      309253 :                                 *dst++ = *src++;
     179      309253 :                         } while (*src != '\0');
     180             :                 }
     181        2413 :                 *dst++ = exponent;
     182        2413 :                 if (sign) {
     183        1256 :                         *dst++ = '-';
     184             :                 } else {
     185        1157 :                         *dst++ = '+';
     186             :                 }
     187        2413 :                 if (decpt < 10) {
     188         472 :                         *dst++ = '0' + decpt;
     189         472 :                         *dst = '\0';
     190             :                 } else {
     191             :                         /* XXX - optimize */
     192        4508 :                         for (sign = decpt, i = 0; (sign /= 10) != 0; i++)
     193        2567 :                                 continue;
     194        1941 :                         dst[i + 1] = '\0';
     195        8390 :                         while (decpt != 0) {
     196        4508 :                                 dst[i--] = '0' + decpt % 10;
     197        4508 :                                 decpt /= 10;
     198             :                         }
     199             :                 }
     200       13468 :         } 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       45538 :                 for (i = 0, src = digits; i < decpt; i++) {
     215       32319 :                         if (*src != '\0') {
     216       28154 :                                 *dst++ = *src++;
     217             :                         } else {
     218        4165 :                                 *dst++ = '0';
     219             :                         }
     220             :                 }
     221       13219 :                 if (*src != '\0') {
     222        9552 :                         if (src == digits) {
     223        1691 :                                 *dst++ = '0';   /* zero before decimal point */
     224             :                         }
     225        9552 :                         *dst++ = dec_point;
     226       52771 :                         for (i = decpt; digits[i] != '\0'; i++) {
     227       43219 :                 *dst++ = digits[i];
     228             :             }
     229             :         }
     230       13219 :         *dst = '\0';
     231             :     }
     232       15881 :     zend_freedtoa(digits);
     233       15881 :     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      938281 : 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      938281 :         register char *p = buf_end;
     323             :         register u_wide_int magnitude;
     324             : 
     325      938281 :         if (is_unsigned) {
     326        3290 :                 magnitude = (u_wide_int) num;
     327        3290 :                 *is_negative = FALSE;
     328             :         } else {
     329      934991 :                 *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      934991 :                 if (*is_negative) {
     341        3511 :                         wide_int t = num + 1;
     342        3511 :                         magnitude = ((u_wide_int) - t) + 1;
     343             :                 } else {
     344      931480 :                         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     2183930 :                 register u_wide_int new_magnitude = magnitude / 10;
     353             : 
     354     2183930 :                 *--p = (char)(magnitude - new_magnitude * 10 + '0');
     355     2183930 :                 magnitude = new_magnitude;
     356             :         }
     357     2183930 :         while (magnitude);
     358             : 
     359      938281 :         *len = buf_end - p;
     360      938281 :         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      105476 : 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      105476 :         register char *s = buf;
     381             :         register char *p, *p_orig;
     382             :         int decimal_point;
     383             : 
     384      105476 :         if (precision >= NDIG - 1) {
     385           2 :                 precision = NDIG - 2;
     386             :         }
     387             : 
     388      105476 :         if (format == 'F') {
     389      104493 :                 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      105476 :         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      105476 :         if (format == 'F') {
     405      104493 :                 if (decimal_point <= 0) {
     406      100466 :                         if (num != 0 || precision > 0) {
     407      100460 :                                 *s++ = '0';
     408      100460 :                                 if (precision > 0) {
     409      100460 :                                         *s++ = dec_point;
     410      212591 :                                         while (decimal_point++ < 0) {
     411       11671 :                                                 *s++ = '0';
     412             :                                         }
     413           0 :                                 } else if (add_dp) {
     414           0 :                                         *s++ = dec_point;
     415             :                                 }
     416             :                         }
     417             :                 } else {
     418        4027 :                         int addz = decimal_point >= NDIG ? decimal_point - NDIG + 1 : 0;
     419        4027 :                         decimal_point -= addz;
     420       25651 :                         while (decimal_point-- > 0) {
     421       17597 :                                 *s++ = *p++;
     422             :                         }
     423        8054 :                         while (addz-- > 0) {
     424           0 :                                 *s++ = '0';
     425             :                         }
     426        4027 :                         if (precision > 0 || add_dp) {
     427        2693 :                                 *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     1024088 :         while (*p) {
     441      813136 :                 *s++ = *p++;
     442             :         }
     443             : 
     444      105476 :         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      105476 :         *len = s - buf;
     467      105476 :         free(p_orig);
     468      105476 :         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       68204 : 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       68204 :         register int mask = (1 << nbits) - 1;
     485       68204 :         register char *p = buf_end;
     486             :         static char low_digits[] = "0123456789abcdef";
     487             :         static char upper_digits[] = "0123456789ABCDEF";
     488       68204 :         register char *digits = (format == 'X') ? upper_digits : low_digits;
     489             : 
     490             :         do {
     491      502541 :                 *--p = digits[num & mask];
     492      502541 :                 num >>= nbits;
     493             :         }
     494      502541 :         while (num);
     495             : 
     496       68204 :         *len = buf_end - p;
     497       68204 :         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     1355290 : static int format_converter(register buffy * odp, const char *fmt, va_list ap) /* {{{ */
     588             : {
     589             :         char *sp;
     590             :         char *bep;
     591     1355290 :         int cc = 0;
     592             :         size_t i;
     593             : 
     594     1355290 :         char *s = NULL;
     595             :         size_t s_len;
     596             :         int free_zcopy;
     597             :         zval *zvp, zcopy;
     598             : 
     599     1355290 :         int min_width = 0;
     600     1355290 :         int precision = 0;
     601             :         enum {
     602             :                 LEFT, RIGHT
     603             :         } adjust;
     604             :         char pad_char;
     605             :         char prefix_char;
     606             : 
     607             :         double fp_num;
     608     1355290 :         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     1355290 :         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     1355290 :         sp = odp->nextb;
     634     1355290 :         bep = odp->buf_end;
     635             : 
     636     8199159 :         while (*fmt) {
     637     5488579 :                 if (*fmt != '%') {
     638     3993529 :                         INS_CHAR(*fmt, sp, bep, cc);
     639             :                 } else {
     640             :                         /*
     641             :                          * Default variable settings
     642             :                          */
     643     1495050 :                         adjust = RIGHT;
     644     1495050 :                         alternate_form = print_sign = print_blank = NO;
     645     1495050 :                         pad_char = ' ';
     646     1495050 :                         prefix_char = NUL;
     647     1495050 :                         free_zcopy = 0;
     648             : 
     649     1495050 :                         fmt++;
     650             : 
     651             :                         /*
     652             :                          * Try to avoid checking for flags, width or precision
     653             :                          */
     654     1639334 :                         if (isascii((int)*fmt) && !islower((int)*fmt)) {
     655             :                                 /*
     656             :                                  * Recognize flags: -, #, BLANK, +
     657             :                                  */
     658       43061 :                                 for (;; fmt++) {
     659      187345 :                                         if (*fmt == '-')
     660           1 :                                                 adjust = LEFT;
     661      187344 :                                         else if (*fmt == '+')
     662           5 :                                                 print_sign = YES;
     663      187339 :                                         else if (*fmt == '#')
     664           0 :                                                 alternate_form = YES;
     665      187339 :                                         else if (*fmt == ' ')
     666           0 :                                                 print_blank = YES;
     667      187339 :                                         else if (*fmt == '0')
     668       43055 :                                                 pad_char = '0';
     669             :                                         else
     670      144284 :                                                 break;
     671       43061 :                                 }
     672             : 
     673             :                                 /*
     674             :                                  * Check if a width was specified
     675             :                                  */
     676      144284 :                                 if (isdigit((int)*fmt)) {
     677       43091 :                                         STR_TO_DEC(fmt, min_width);
     678       43091 :                                         adjust_width = YES;
     679      101193 :                                 } 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      101193 :                                         adjust_width = NO;
     689             : 
     690             :                                 /*
     691             :                                  * Check if a precision was specified
     692             :                                  */
     693      144284 :                                 if (*fmt == '.') {
     694      101190 :                                         adjust_precision = YES;
     695      101190 :                                         fmt++;
     696      101190 :                                         if (isdigit((int)*fmt)) {
     697      100121 :                                                 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      101190 :                                         if (precision > FORMAT_CONV_MAX_PRECISION) {
     707           0 :                                                 precision = FORMAT_CONV_MAX_PRECISION;
     708             :                                         }
     709             :                                 } else
     710       43094 :                                         adjust_precision = NO;
     711             :                         } else
     712     1350766 :                                 adjust_precision = adjust_width = NO;
     713             : 
     714             :                         /*
     715             :                          * Modifier check
     716             :                          */
     717     1495050 :                         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      120057 :                                         fmt++;
     743             : #if SIZEOF_LONG_LONG
     744      120057 :                                         if (*fmt == 'l') {
     745        6244 :                                                 fmt++;
     746        6244 :                                                 modifier = LM_LONG_LONG;
     747             :                                         } else
     748             : #endif
     749      113813 :                                                 modifier = LM_LONG;
     750      120057 :                                         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     1374967 :                                         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     1495050 :                         switch (*fmt) {
     798             :                                 case 'Z': {
     799             :                                     TSRMLS_FETCH();
     800           0 :                                     zvp = (zval*) va_arg(ap, zval*);
     801           0 :                                         free_zcopy = zend_make_printable_zval(zvp, &zcopy TSRMLS_CC);
     802           0 :                                         if (free_zcopy) {
     803           0 :                                                 zvp = &zcopy;
     804             :                                         }
     805           0 :                                         s_len = Z_STRLEN_P(zvp);
     806           0 :                                         s = Z_STRVAL_P(zvp);
     807           0 :                                         if (adjust_precision && precision < s_len) {
     808           0 :                                                 s_len = precision;
     809             :                                         }       
     810           0 :                                         break;
     811             :                                 }
     812             :                                 case 'u':
     813        2474 :                                         switch(modifier) {
     814             :                                                 default:
     815         796 :                                                         i_num = (wide_int) va_arg(ap, unsigned int);
     816         796 :                                                         break;
     817             :                                                 case LM_LONG_DOUBLE:
     818           0 :                                                         goto fmt_error;
     819             :                                                 case LM_LONG:
     820        1665 :                                                         i_num = (wide_int) va_arg(ap, unsigned long int);
     821        1665 :                                                         break;
     822             :                                                 case LM_SIZE_T:
     823          13 :                                                         i_num = (wide_int) va_arg(ap, size_t);
     824          13 :                                                         break;
     825             : #if SIZEOF_LONG_LONG
     826             :                                                 case LM_LONG_LONG:
     827           0 :                                                         i_num = (wide_int) va_arg(ap, u_wide_int);
     828           0 :                                                         break;
     829             : #endif
     830             : #if SIZEOF_INTMAX_T
     831             :                                                 case LM_INTMAX_T:
     832           0 :                                                         i_num = (wide_int) va_arg(ap, uintmax_t);
     833           0 :                                                         break;
     834             : #endif
     835             : #if SIZEOF_PTRDIFF_T
     836             :                                                 case LM_PTRDIFF_T:
     837           0 :                                                         i_num = (wide_int) va_arg(ap, ptrdiff_t);
     838           0 :                                                         break;
     839             : #endif
     840             :                                                 case LM_PHP_INT_T:
     841           0 :                                                         i_num = (wide_int) va_arg(ap, zend_ulong);
     842             :                                                         break;
     843             :                                         }
     844             :                                         /*
     845             :                                          * The rest also applies to other integer formats, so fall
     846             :                                          * into that case.
     847             :                                          */
     848             :                                 case 'd':
     849             :                                 case 'i':
     850             :                                         /*
     851             :                                          * Get the arg if we haven't already.
     852             :                                          */
     853      686187 :                                         if ((*fmt) != 'u') {
     854      683713 :                                                 switch(modifier) {
     855             :                                                         default:
     856      565332 :                                                                 i_num = (wide_int) va_arg(ap, int);
     857      565332 :                                                                 break;
     858             :                                                         case LM_LONG_DOUBLE:
     859           0 :                                                                 goto fmt_error;
     860             :                                                         case LM_LONG:
     861      112124 :                                                                 i_num = (wide_int) va_arg(ap, long int);
     862      112124 :                                                                 break;
     863             :                                                         case LM_SIZE_T:
     864             : #if SIZEOF_SSIZE_T
     865           0 :                                                                 i_num = (wide_int) va_arg(ap, ssize_t);
     866             : #else
     867             :                                                                 i_num = (wide_int) va_arg(ap, size_t);
     868             : #endif
     869           0 :                                                                 break;
     870             : #if SIZEOF_LONG_LONG
     871             :                                                         case LM_LONG_LONG:
     872        6246 :                                                                 i_num = (wide_int) va_arg(ap, wide_int);
     873        6246 :                                                                 break;
     874             : #endif
     875             : #if SIZEOF_INTMAX_T
     876             :                                                         case LM_INTMAX_T:
     877           0 :                                                                 i_num = (wide_int) va_arg(ap, intmax_t);
     878           0 :                                                                 break;
     879             : #endif
     880             : #if SIZEOF_PTRDIFF_T
     881             :                                                         case LM_PTRDIFF_T:
     882           0 :                                                                 i_num = (wide_int) va_arg(ap, ptrdiff_t);
     883           0 :                                                                 break;
     884             : #endif
     885             :                                                         case LM_PHP_INT_T:
     886          11 :                                                                 i_num = (wide_int) va_arg(ap, zend_long);
     887             :                                                                 break;
     888             :                                                 }
     889             :                                         }
     890      686187 :                                         s = ap_php_conv_10(i_num, (*fmt) == 'u', &is_negative,
     891             :                                                                 &num_buf[NUM_BUF_SIZE], &s_len);
     892      686187 :                                         FIX_PRECISION(adjust_precision, precision, s, s_len);
     893             : 
     894      686187 :                                         if (*fmt != 'u') {
     895      683713 :                                                 if (is_negative) {
     896         535 :                                                         prefix_char = '-';
     897      683178 :                                                 } else if (print_sign) {
     898           4 :                                                         prefix_char = '+';
     899      683174 :                                                 } else if (print_blank) {
     900           0 :                                                         prefix_char = ' ';
     901             :                                                 }
     902             :                                         }
     903      686187 :                                         break;
     904             : 
     905             : 
     906             :                                 case 'o':
     907           0 :                                         switch(modifier) {
     908             :                                                 default:
     909           0 :                                                         ui_num = (u_wide_int) va_arg(ap, unsigned int);
     910           0 :                                                         break;
     911             :                                                 case LM_LONG_DOUBLE:
     912           0 :                                                         goto fmt_error;
     913             :                                                 case LM_LONG:
     914           0 :                                                         ui_num = (u_wide_int) va_arg(ap, unsigned long int);
     915           0 :                                                         break;
     916             :                                                 case LM_SIZE_T:
     917           0 :                                                         ui_num = (u_wide_int) va_arg(ap, size_t);
     918           0 :                                                         break;
     919             : #if SIZEOF_LONG_LONG
     920             :                                                 case LM_LONG_LONG:
     921           0 :                                                         ui_num = (u_wide_int) va_arg(ap, u_wide_int);
     922           0 :                                                         break;
     923             : #endif
     924             : #if SIZEOF_INTMAX_T
     925             :                                                 case LM_INTMAX_T:
     926           0 :                                                         ui_num = (u_wide_int) va_arg(ap, uintmax_t);
     927           0 :                                                         break;
     928             : #endif
     929             : #if SIZEOF_PTRDIFF_T
     930             :                                                 case LM_PTRDIFF_T:
     931           0 :                                                         ui_num = (u_wide_int) va_arg(ap, ptrdiff_t);
     932           0 :                                                         break;
     933             : #endif
     934             :                                                 case LM_PHP_INT_T:
     935           0 :                                                         ui_num = (u_wide_int) va_arg(ap, zend_ulong);
     936             :                                                         break;
     937             :                                         }
     938           0 :                                         s = ap_php_conv_p2(ui_num, 3, *fmt, &num_buf[NUM_BUF_SIZE], &s_len);
     939           0 :                                         FIX_PRECISION(adjust_precision, precision, s, s_len);
     940           0 :                                         if (alternate_form && *s != '0') {
     941           0 :                                                 *--s = '0';
     942           0 :                                                 s_len++;
     943             :                                         }
     944           0 :                                         break;
     945             : 
     946             : 
     947             :                                 case 'x':
     948             :                                 case 'X':
     949          54 :                                         switch(modifier) {
     950             :                                                 default:
     951          30 :                                                         ui_num = (u_wide_int) va_arg(ap, unsigned int);
     952          30 :                                                         break;
     953             :                                                 case LM_LONG_DOUBLE:
     954           0 :                                                         goto fmt_error;
     955             :                                                 case LM_LONG:
     956          24 :                                                         ui_num = (u_wide_int) va_arg(ap, unsigned long int);
     957          24 :                                                         break;
     958             :                                                 case LM_SIZE_T:
     959           0 :                                                         ui_num = (u_wide_int) va_arg(ap, size_t);
     960           0 :                                                         break;
     961             : #if SIZEOF_LONG_LONG
     962             :                                                 case LM_LONG_LONG:
     963           0 :                                                         ui_num = (u_wide_int) va_arg(ap, u_wide_int);
     964           0 :                                                         break;
     965             : #endif
     966             : #if SIZEOF_INTMAX_T
     967             :                                                 case LM_INTMAX_T:
     968           0 :                                                         ui_num = (u_wide_int) va_arg(ap, uintmax_t);
     969           0 :                                                         break;
     970             : #endif
     971             : #if SIZEOF_PTRDIFF_T
     972             :                                                 case LM_PTRDIFF_T:
     973           0 :                                                         ui_num = (u_wide_int) va_arg(ap, ptrdiff_t);
     974           0 :                                                         break;
     975             : #endif
     976             :                                                 case LM_PHP_INT_T:
     977           0 :                                                         ui_num = (u_wide_int) va_arg(ap, zend_ulong);
     978             :                                                         break;
     979             :                                         }
     980          54 :                                         s = ap_php_conv_p2(ui_num, 4, *fmt, &num_buf[NUM_BUF_SIZE], &s_len);
     981          54 :                                         FIX_PRECISION(adjust_precision, precision, s, s_len);
     982          54 :                                         if (alternate_form && i_num != 0) {
     983           0 :                                                 *--s = *fmt;    /* 'x' or 'X' */
     984           0 :                                                 *--s = '0';
     985           0 :                                                 s_len += 2;
     986             :                                         }
     987          54 :                                         break;
     988             : 
     989             : 
     990             :                                 case 's':
     991             :                                 case 'v':
     992      705664 :                                         s = va_arg(ap, char *);
     993      705664 :                                         if (s != NULL) {
     994      705664 :                                                 s_len = strlen(s);
     995      705664 :                                                 if (adjust_precision && precision < s_len) {
     996          13 :                                                         s_len = precision;
     997             :                                                 }
     998             :                                         } else {
     999           0 :                                                 s = S_NULL;
    1000           0 :                                                 s_len = S_NULL_LEN;
    1001             :                                         }
    1002      705664 :                                         pad_char = ' ';
    1003      705664 :                                         break;
    1004             : 
    1005             : 
    1006             :                                 case 'f':
    1007             :                                 case 'F':
    1008             :                                 case 'e':
    1009             :                                 case 'E':
    1010      100130 :                                         switch(modifier) {
    1011             :                                                 case LM_LONG_DOUBLE:
    1012           0 :                                                         fp_num = (double) va_arg(ap, long double);
    1013           0 :                                                         break;
    1014             :                                                 case LM_STD:
    1015      100130 :                                                         fp_num = va_arg(ap, double);
    1016      100130 :                                                         break;
    1017             :                                                 default:
    1018           0 :                                                         goto fmt_error;
    1019             :                                         }
    1020             : 
    1021      100130 :                                         if (zend_isnan(fp_num)) {
    1022           0 :                                                 s = "NAN";
    1023           0 :                                                 s_len = 3;
    1024      100130 :                                         } else if (zend_isinf(fp_num)) {
    1025           0 :                                                 s = "INF";
    1026           0 :                                                 s_len = 3;
    1027             :                                         } else {
    1028             : #ifdef HAVE_LOCALE_H
    1029             : #ifdef ZTS
    1030             :                                                 localeconv_r(&lconv);
    1031             : #else
    1032      100130 :                                                 if (!lconv) {
    1033      100130 :                                                         lconv = localeconv();
    1034             :                                                 }
    1035             : #endif
    1036             : #endif
    1037      100142 :                                                 s = php_conv_fp((*fmt == 'f')?'F':*fmt, fp_num, alternate_form,
    1038             :                                                  (adjust_precision == NO) ? FLOAT_DIGITS : precision,
    1039      100142 :                                                  (*fmt == 'f')?LCONV_DECIMAL_POINT:'.',
    1040             :                                                                         &is_negative, &num_buf[1], &s_len);
    1041      100130 :                                                 if (is_negative)
    1042           0 :                                                         prefix_char = '-';
    1043      100130 :                                                 else if (print_sign)
    1044           0 :                                                         prefix_char = '+';
    1045      100130 :                                                 else if (print_blank)
    1046           0 :                                                         prefix_char = ' ';
    1047             :                                         }
    1048      100130 :                                         break;
    1049             : 
    1050             : 
    1051             :                                 case 'g':
    1052             :                                 case 'k':
    1053             :                                 case 'G':
    1054             :                                 case 'H':
    1055           3 :                                         switch(modifier) {
    1056             :                                                 case LM_LONG_DOUBLE:
    1057           0 :                                                         fp_num = (double) va_arg(ap, long double);
    1058           0 :                                                         break;
    1059             :                                                 case LM_STD:
    1060           3 :                                                         fp_num = va_arg(ap, double);
    1061           3 :                                                         break;
    1062             :                                                 default:
    1063           0 :                                                         goto fmt_error;
    1064             :                                         }
    1065             : 
    1066           3 :                                         if (zend_isnan(fp_num)) {
    1067           0 :                                                 s = "NAN";
    1068           0 :                                                 s_len = 3;
    1069           0 :                                                 break;
    1070           3 :                                         } else if (zend_isinf(fp_num)) {
    1071           0 :                                                 if (fp_num > 0) {
    1072           0 :                                                         s = "INF";
    1073           0 :                                                         s_len = 3;
    1074             :                                                 } else {
    1075           0 :                                                         s = "-INF";
    1076           0 :                                                         s_len = 4;
    1077             :                                                 }
    1078           0 :                                                 break;
    1079             :                                         }
    1080             : 
    1081           3 :                                         if (adjust_precision == NO) {
    1082           0 :                                                 precision = FLOAT_DIGITS;
    1083           3 :                                         } else if (precision == 0) {
    1084           0 :                                                 precision = 1;
    1085             :                                         }
    1086             :                                         /*
    1087             :                                          * * We use &num_buf[ 1 ], so that we have room for the sign
    1088             :                                          */
    1089             : #ifdef HAVE_LOCALE_H
    1090             : #ifdef ZTS
    1091             :                                         localeconv_r(&lconv);
    1092             : #else
    1093           3 :                                         if (!lconv) {
    1094           3 :                                                 lconv = localeconv();
    1095             :                                         }
    1096             : #endif
    1097             : #endif
    1098           3 :                                         s = php_gcvt(fp_num, precision, (*fmt=='H' || *fmt == 'k') ? '.' : LCONV_DECIMAL_POINT, (*fmt == 'G' || *fmt == 'H')?'E':'e', &num_buf[1]);
    1099           3 :                                         if (*s == '-') {
    1100           1 :                                                 prefix_char = *s++;
    1101           2 :                                         } else if (print_sign) {
    1102           0 :                                                 prefix_char = '+';
    1103           2 :                                         } else if (print_blank) {
    1104           0 :                                                 prefix_char = ' ';
    1105             :                                         }
    1106             : 
    1107           3 :                                         s_len = strlen(s);
    1108             : 
    1109           3 :                                         if (alternate_form && (strchr(s, '.')) == NULL) {
    1110           0 :                                                 s[s_len++] = '.';
    1111             :                                         }
    1112           3 :                                         break;
    1113             : 
    1114             : 
    1115             :                                 case 'c':
    1116        3011 :                                         char_buf[0] = (char) (va_arg(ap, int));
    1117        3011 :                                         s = &char_buf[0];
    1118        3011 :                                         s_len = 1;
    1119        3011 :                                         pad_char = ' ';
    1120        3011 :                                         break;
    1121             : 
    1122             : 
    1123             :                                 case '%':
    1124           1 :                                         char_buf[0] = '%';
    1125           1 :                                         s = &char_buf[0];
    1126           1 :                                         s_len = 1;
    1127           1 :                                         pad_char = ' ';
    1128           1 :                                         break;
    1129             : 
    1130             : 
    1131             :                                 case 'n':
    1132           0 :                                         *(va_arg(ap, int *)) = cc;
    1133           0 :                                         goto skip_output;
    1134             : 
    1135             :                                         /*
    1136             :                                          * Always extract the argument as a "char *" pointer. We
    1137             :                                          * should be using "void *" but there are still machines
    1138             :                                          * that don't understand it.
    1139             :                                          * If the pointer size is equal to the size of an unsigned
    1140             :                                          * integer we convert the pointer to a hex number, otherwise
    1141             :                                          * we print "%p" to indicate that we don't handle "%p".
    1142             :                                          */
    1143             :                                 case 'p':
    1144             :                                         if (sizeof(char *) <= sizeof(u_wide_int)) {
    1145           0 :                                                 ui_num = (u_wide_int)((size_t) va_arg(ap, char *));
    1146           0 :                                                 s = ap_php_conv_p2(ui_num, 4, 'x',
    1147             :                                                                 &num_buf[NUM_BUF_SIZE], &s_len);
    1148           0 :                                                 if (ui_num != 0) {
    1149           0 :                                                         *--s = 'x';
    1150           0 :                                                         *--s = '0';
    1151           0 :                                                         s_len += 2;
    1152             :                                                 }
    1153             :                                         } else {
    1154             :                                                 s = "%p";
    1155             :                                                 s_len = 2;
    1156             :                                         }
    1157           0 :                                         pad_char = ' ';
    1158           0 :                                         break;
    1159             : 
    1160             : 
    1161             :                                 case NUL:
    1162             :                                         /*
    1163             :                                          * The last character of the format string was %.
    1164             :                                          * We ignore it.
    1165             :                                          */
    1166           0 :                                         continue;
    1167             : 
    1168             : 
    1169             : fmt_error:
    1170           0 :                                 php_error(E_ERROR, "Illegal length modifier specified '%c' in s[np]printf call", *fmt);
    1171             :                                         /*
    1172             :                                          * The default case is for unrecognized %'s.
    1173             :                                          * We print %<char> to help the user identify what
    1174             :                                          * option is not understood.
    1175             :                                          * This is also useful in case the user wants to pass
    1176             :                                          * the output of format_converter to another function
    1177             :                                          * that understands some other %<char> (like syslog).
    1178             :                                          * Note that we can't point s inside fmt because the
    1179             :                                          * unknown <char> could be preceded by width etc.
    1180             :                                          */
    1181             :                                 default:
    1182           0 :                                         char_buf[0] = '%';
    1183           0 :                                         char_buf[1] = *fmt;
    1184           0 :                                         s = char_buf;
    1185           0 :                                         s_len = 2;
    1186           0 :                                         pad_char = ' ';
    1187             :                                         break;
    1188             :                         }
    1189             : 
    1190     1495050 :                         if (prefix_char != NUL) {
    1191         540 :                                 *--s = prefix_char;
    1192         540 :                                 s_len++;
    1193             :                         }
    1194     1495050 :                         if (adjust_width && adjust == RIGHT && min_width > s_len) {
    1195       22153 :                                 if (pad_char == '0' && prefix_char != NUL) {
    1196           5 :                                         INS_CHAR(*s, sp, bep, cc)
    1197           5 :                                                 s++;
    1198           5 :                                         s_len--;
    1199           5 :                                         min_width--;
    1200             :                                 }
    1201       26130 :                                 PAD(min_width, s_len, pad_char);
    1202             :                         }
    1203             :                         /*
    1204             :                          * Print the string s.
    1205             :                          */
    1206     9113115 :                         for (i = s_len; i != 0; i--) {
    1207     7618065 :                                 INS_CHAR(*s, sp, bep, cc);
    1208     7618065 :                                 s++;
    1209             :                         }
    1210             : 
    1211     1495050 :                         if (adjust_width && adjust == LEFT && min_width > s_len)
    1212           0 :                                 PAD(min_width, s_len, pad_char);
    1213     1495050 :                         if (free_zcopy) {
    1214             :                                 zval_dtor(&zcopy);
    1215             :                         }
    1216             :                 }
    1217             : skip_output:
    1218     5488579 :                 fmt++;
    1219             :         }
    1220     1355290 :         odp->nextb = sp;
    1221     1355290 :         return (cc);
    1222             : }
    1223             : /* }}} */
    1224             : 
    1225             : /*
    1226             :  * This is the general purpose conversion function.
    1227             :  */
    1228     1355290 : static void strx_printv(int *ccp, char *buf, size_t len, const char *format, va_list ap) /* {{{ */
    1229             : {
    1230             :         buffy od;
    1231             :         int cc;
    1232             : 
    1233             :         /*
    1234             :          * First initialize the descriptor
    1235             :          * Notice that if no length is given, we initialize buf_end to the
    1236             :          * highest possible address.
    1237             :          */
    1238     1355290 :         if (len == 0) {
    1239           0 :                 od.buf_end = (char *) ~0;
    1240           0 :                 od.nextb   = (char *) ~0;
    1241             :         } else {
    1242     1355290 :                 od.buf_end = &buf[len-1];
    1243     1355290 :                 od.nextb   = buf;
    1244             :         }
    1245             : 
    1246             :         /*
    1247             :          * Do the conversion
    1248             :          */
    1249     1355290 :         cc = format_converter(&od, format, ap);
    1250     1355290 :         if (len != 0 && od.nextb <= od.buf_end) {
    1251     1355290 :                 *(od.nextb) = '\0';
    1252             :         }
    1253     1355290 :         if (ccp) {
    1254     1355290 :                 *ccp = cc;
    1255             :         }
    1256     1355290 : }
    1257             : /* }}} */
    1258             : 
    1259     1214385 : PHPAPI int ap_php_slprintf(char *buf, size_t len, const char *format,...) /* {{{ */
    1260             : {
    1261             :         int cc;
    1262             :         va_list ap;
    1263             : 
    1264     1214385 :         va_start(ap, format);
    1265     1214385 :         strx_printv(&cc, buf, len, format, ap);
    1266     1214385 :         va_end(ap);
    1267     1214385 :         if (cc >= len) {
    1268           0 :                 cc = (int)len -1;
    1269           0 :                 buf[cc] = '\0';
    1270             :         }
    1271     1214385 :         return cc;
    1272             : }
    1273             : /* }}} */
    1274             : 
    1275          15 : PHPAPI int ap_php_vslprintf(char *buf, size_t len, const char *format, va_list ap) /* {{{ */
    1276             : {
    1277             :         int cc;
    1278             : 
    1279          15 :         strx_printv(&cc, buf, len, format, ap);
    1280          15 :         if (cc >= len) {
    1281           0 :                 cc = (int)len -1;
    1282           0 :                 buf[cc] = '\0';
    1283             :         }
    1284          15 :         return cc;
    1285             : }
    1286             : /* }}} */
    1287             : 
    1288      140864 : PHPAPI int ap_php_snprintf(char *buf, size_t len, const char *format,...) /* {{{ */
    1289             : {
    1290             :         int cc;
    1291             :         va_list ap;
    1292             : 
    1293      140864 :         va_start(ap, format);
    1294      140864 :         strx_printv(&cc, buf, len, format, ap);
    1295      140864 :         va_end(ap);
    1296      140864 :         return (cc);
    1297             : }
    1298             : /* }}} */
    1299             : 
    1300          26 : PHPAPI int ap_php_vsnprintf(char *buf, size_t len, const char *format, va_list ap) /* {{{ */
    1301             : {
    1302             :         int cc;
    1303             : 
    1304          26 :         strx_printv(&cc, buf, len, format, ap);
    1305          26 :         return (cc);
    1306             : }
    1307             : /* }}} */
    1308             : 
    1309           0 : PHPAPI int ap_php_vasprintf(char **buf, const char *format, va_list ap) /* {{{ */
    1310             : {
    1311             :         va_list ap2;
    1312             :         int cc;
    1313             : 
    1314           0 :         va_copy(ap2, ap);
    1315           0 :         cc = ap_php_vsnprintf(NULL, 0, format, ap2);
    1316           0 :         va_end(ap2);
    1317             : 
    1318           0 :         *buf = NULL;
    1319             : 
    1320           0 :         if (cc >= 0) {
    1321           0 :                 if ((*buf = malloc(++cc)) != NULL) {
    1322           0 :                         if ((cc = ap_php_vsnprintf(*buf, cc, format, ap)) < 0) {
    1323           0 :                                 free(*buf);
    1324           0 :                                 *buf = NULL;
    1325             :                         }
    1326             :                 }
    1327             :         }
    1328             : 
    1329           0 :         return cc;
    1330             : }
    1331             : /* }}} */
    1332             : 
    1333           0 : PHPAPI int ap_php_asprintf(char **buf, const char *format, ...) /* {{{ */
    1334             : {
    1335             :         int cc;
    1336             :         va_list ap;
    1337             : 
    1338           0 :         va_start(ap, format);
    1339           0 :         cc = vasprintf(buf, format, ap);
    1340           0 :         va_end(ap);
    1341           0 :         return cc;
    1342             : }
    1343             : /* }}} */
    1344             : 
    1345             : /*
    1346             :  * Local variables:
    1347             :  * tab-width: 4
    1348             :  * c-basic-offset: 4
    1349             :  * End:
    1350             :  * vim600: sw=4 ts=4 fdm=marker
    1351             :  * vim<600: sw=4 ts=4
    1352             :  */

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:27 +0000 (7 days ago)

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