2003-02-19 16:40:19 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 17:03:12 +08:00
|
|
|
| Copyright (c) The PHP Group |
|
2003-02-19 16:40:19 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2003-02-19 16:40:19 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2021-05-06 18:16:35 +08:00
|
|
|
| https://www.php.net/license/3_01.txt |
|
2003-02-19 16:40:19 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2021-11-24 21:13:34 +08:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
# define _GNU_SOURCE
|
|
|
|
#endif
|
2006-12-06 17:52:51 +08:00
|
|
|
#include "php.h"
|
|
|
|
|
|
|
|
#include <zend_strtod.h>
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include <locale.h>
|
2014-12-06 18:59:43 +08:00
|
|
|
#ifdef ZTS
|
2014-12-06 19:09:35 +08:00
|
|
|
#include "ext/standard/php_string.h"
|
2014-12-06 18:59:43 +08:00
|
|
|
#define LCONV_DECIMAL_POINT (*lconv.decimal_point)
|
|
|
|
#else
|
2006-12-19 21:13:29 +08:00
|
|
|
#define LCONV_DECIMAL_POINT (*lconv->decimal_point)
|
2014-12-06 18:59:43 +08:00
|
|
|
#endif
|
2006-12-06 17:52:51 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2002, 2006 Todd C. Miller <Todd.Miller@courtesan.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* Sponsored in part by the Defense Advanced Research Projects
|
|
|
|
* Agency (DARPA) and Air Force Research Laboratory, Air Force
|
|
|
|
* Materiel Command, USAF, under agreement number F39502-99-1-0512.
|
|
|
|
*/
|
|
|
|
|
2021-07-15 01:59:51 +08:00
|
|
|
static char * __cvt(double value, int ndigit, int *decpt, bool *sign, int fmode, int pad) /* {{{ */
|
2006-12-06 17:52:51 +08:00
|
|
|
{
|
2021-07-15 06:43:22 +08:00
|
|
|
char *s = NULL;
|
2006-12-06 17:52:51 +08:00
|
|
|
char *p, *rve, c;
|
|
|
|
size_t siz;
|
|
|
|
|
|
|
|
if (ndigit < 0) {
|
|
|
|
siz = -ndigit + 1;
|
|
|
|
} else {
|
|
|
|
siz = ndigit + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* __dtoa() doesn't allocate space for 0 so we do it by hand */
|
|
|
|
if (value == 0.0) {
|
|
|
|
*decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */
|
|
|
|
*sign = 0;
|
2007-02-26 20:05:52 +08:00
|
|
|
if ((rve = s = (char *)malloc(ndigit?siz:2)) == NULL) {
|
2006-12-06 17:52:51 +08:00
|
|
|
return(NULL);
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
2006-12-06 17:52:51 +08:00
|
|
|
*rve++ = '0';
|
|
|
|
*rve = '\0';
|
|
|
|
if (!ndigit) {
|
|
|
|
return(s);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p = zend_dtoa(value, fmode + 2, ndigit, decpt, sign, &rve);
|
|
|
|
if (*decpt == 9999) {
|
|
|
|
/* Infinity or Nan, convert to inf or nan like printf */
|
|
|
|
*decpt = 0;
|
|
|
|
c = *p;
|
|
|
|
zend_freedtoa(p);
|
2012-07-15 02:15:11 +08:00
|
|
|
return strdup((c == 'I' ? "INF" : "NAN"));
|
2006-12-06 17:52:51 +08:00
|
|
|
}
|
|
|
|
/* Make a local copy and adjust rve to be in terms of s */
|
2007-02-26 20:05:52 +08:00
|
|
|
if (pad && fmode) {
|
2006-12-06 17:52:51 +08:00
|
|
|
siz += *decpt;
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
2006-12-06 17:52:51 +08:00
|
|
|
if ((s = (char *)malloc(siz+1)) == NULL) {
|
|
|
|
zend_freedtoa(p);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
(void) strlcpy(s, p, siz);
|
|
|
|
rve = s + (rve - p);
|
|
|
|
zend_freedtoa(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add trailing zeros */
|
|
|
|
if (pad) {
|
|
|
|
siz -= rve - s;
|
2007-02-26 20:05:52 +08:00
|
|
|
while (--siz) {
|
2006-12-06 17:52:51 +08:00
|
|
|
*rve++ = '0';
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
2006-12-06 17:52:51 +08:00
|
|
|
*rve = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return(s);
|
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
/* }}} */
|
2006-12-06 17:52:51 +08:00
|
|
|
|
2021-07-15 01:59:51 +08:00
|
|
|
static inline char *php_ecvt(double value, int ndigit, int *decpt, bool *sign) /* {{{ */
|
2006-12-06 17:52:51 +08:00
|
|
|
{
|
|
|
|
return(__cvt(value, ndigit, decpt, sign, 0, 1));
|
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
/* }}} */
|
2006-12-06 17:52:51 +08:00
|
|
|
|
2021-07-15 01:59:51 +08:00
|
|
|
static inline char *php_fcvt(double value, int ndigit, int *decpt, bool *sign) /* {{{ */
|
2006-12-06 17:52:51 +08:00
|
|
|
{
|
2021-06-29 16:04:10 +08:00
|
|
|
return(__cvt(value, ndigit, decpt, sign, 1, 1));
|
2006-12-06 17:52:51 +08:00
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
/* }}} */
|
2006-12-06 17:52:51 +08:00
|
|
|
|
2007-02-26 20:05:52 +08:00
|
|
|
/* {{{ Apache license */
|
1999-04-08 05:05:13 +08:00
|
|
|
/* ====================================================================
|
|
|
|
* Copyright (c) 1995-1998 The Apache Group. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2008-02-07 20:47:44 +08:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
1999-04-08 05:05:13 +08:00
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. All advertising materials mentioning features or use of this
|
|
|
|
* software must display the following acknowledgment:
|
|
|
|
* "This product includes software developed by the Apache Group
|
|
|
|
* for use in the Apache HTTP server project (http://www.apache.org/)."
|
|
|
|
*
|
|
|
|
* 4. The names "Apache Server" and "Apache Group" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission.
|
|
|
|
*
|
|
|
|
* 5. Redistributions of any form whatsoever must retain the following
|
|
|
|
* acknowledgment:
|
|
|
|
* "This product includes software developed by the Apache Group
|
|
|
|
* for use in the Apache HTTP server project (http://www.apache.org/)."
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
|
|
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many
|
|
|
|
* individuals on behalf of the Apache Group and was originally based
|
|
|
|
* on public domain software written at the National Center for
|
|
|
|
* Supercomputing Applications, University of Illinois, Urbana-Champaign.
|
|
|
|
* For more information on the Apache Group and the Apache HTTP server
|
|
|
|
* project, please see <http://www.apache.org/>.
|
|
|
|
*
|
|
|
|
* This code is based on, and used with the permission of, the
|
|
|
|
* SIO stdio-replacement strx_* functions by Panos Tsirigotis
|
|
|
|
* <panos@alumni.cs.colorado.edu> for xinetd.
|
|
|
|
*/
|
2007-02-26 20:05:52 +08:00
|
|
|
/* }}} */
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2002-04-10 09:07:49 +08:00
|
|
|
#define NUL '\0'
|
|
|
|
#define INT_NULL ((int *)0)
|
|
|
|
|
|
|
|
#define S_NULL "(null)"
|
|
|
|
#define S_NULL_LEN 6
|
|
|
|
|
|
|
|
#define FLOAT_DIGITS 6
|
|
|
|
#define EXPONENT_LENGTH 10
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert num to its decimal format.
|
|
|
|
* Return value:
|
|
|
|
* - a pointer to a string containing the number (no sign)
|
|
|
|
* - len contains the length of the string
|
2021-07-15 01:59:51 +08:00
|
|
|
* - is_negative is set to true or false depending on the sign
|
|
|
|
* of the number (always set to false if is_unsigned is true)
|
2002-04-10 09:07:49 +08:00
|
|
|
*
|
|
|
|
* The caller provides a buffer for the string: that is the buf_end argument
|
|
|
|
* which is a pointer to the END of the buffer + 1 (i.e. if the buffer
|
|
|
|
* is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
|
|
|
|
*/
|
2007-02-26 20:05:52 +08:00
|
|
|
/* char * ap_php_conv_10() {{{ */
|
2021-07-15 06:43:22 +08:00
|
|
|
PHPAPI char * ap_php_conv_10(int64_t num, bool is_unsigned,
|
|
|
|
bool * is_negative, char *buf_end, size_t *len)
|
2002-04-10 09:07:49 +08:00
|
|
|
{
|
2021-07-15 06:43:22 +08:00
|
|
|
char *p = buf_end;
|
|
|
|
uint64_t magnitude;
|
2002-04-10 09:07:49 +08:00
|
|
|
|
|
|
|
if (is_unsigned) {
|
2021-07-15 06:33:35 +08:00
|
|
|
magnitude = (uint64_t) num;
|
2021-07-15 01:59:51 +08:00
|
|
|
*is_negative = false;
|
2002-04-10 09:07:49 +08:00
|
|
|
} else {
|
|
|
|
*is_negative = (num < 0);
|
|
|
|
|
|
|
|
/*
|
2008-02-07 20:47:44 +08:00
|
|
|
* On a 2's complement machine, negating the most negative integer
|
2002-04-10 09:07:49 +08:00
|
|
|
* results in a number that cannot be represented as a signed integer.
|
|
|
|
* Here is what we do to obtain the number's magnitude:
|
|
|
|
* a. add 1 to the number
|
|
|
|
* b. negate it (becomes positive)
|
|
|
|
* c. convert it to unsigned
|
|
|
|
* d. add 1
|
|
|
|
*/
|
|
|
|
if (*is_negative) {
|
2021-07-15 06:41:31 +08:00
|
|
|
int64_t t = num + 1;
|
2021-07-15 06:33:35 +08:00
|
|
|
magnitude = ((uint64_t) - t) + 1;
|
2007-02-26 20:05:52 +08:00
|
|
|
} else {
|
2021-07-15 06:33:35 +08:00
|
|
|
magnitude = (uint64_t) num;
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
2002-04-10 09:07:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-02-07 20:47:44 +08:00
|
|
|
* We use a do-while loop so that we write at least 1 digit
|
2002-04-10 09:07:49 +08:00
|
|
|
*/
|
|
|
|
do {
|
2021-07-15 06:43:22 +08:00
|
|
|
uint64_t new_magnitude = magnitude / 10;
|
2002-04-10 09:07:49 +08:00
|
|
|
|
2002-04-10 21:02:53 +08:00
|
|
|
*--p = (char)(magnitude - new_magnitude * 10 + '0');
|
2002-04-10 09:07:49 +08:00
|
|
|
magnitude = new_magnitude;
|
|
|
|
}
|
|
|
|
while (magnitude);
|
|
|
|
|
|
|
|
*len = buf_end - p;
|
|
|
|
return (p);
|
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
/* }}} */
|
2002-04-10 09:07:49 +08:00
|
|
|
|
2003-07-18 05:26:25 +08:00
|
|
|
/* If you change this value then also change bug24640.phpt.
|
2005-12-26 22:46:49 +08:00
|
|
|
* Also NDIG must be reasonable smaller than NUM_BUF_SIZE.
|
2003-07-18 05:26:25 +08:00
|
|
|
*/
|
2005-12-26 22:46:49 +08:00
|
|
|
#define NDIG 320
|
2002-04-10 09:07:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a floating point number to a string formats 'f', 'e' or 'E'.
|
|
|
|
* The result is placed in buf, and len denotes the length of the string
|
|
|
|
* The sign is returned in the is_negative argument (and is not placed
|
|
|
|
* in buf).
|
|
|
|
*/
|
2007-02-26 20:05:52 +08:00
|
|
|
/* PHPAPI char * php_conv_fp() {{{ */
|
2021-07-15 06:43:22 +08:00
|
|
|
PHPAPI char * php_conv_fp(char format, double num,
|
2021-07-15 02:09:39 +08:00
|
|
|
bool add_dp, int precision, char dec_point, bool * is_negative, char *buf, size_t *len)
|
2002-04-10 09:07:49 +08:00
|
|
|
{
|
2021-07-15 06:43:22 +08:00
|
|
|
char *s = buf;
|
|
|
|
char *p, *p_orig;
|
2002-04-10 09:07:49 +08:00
|
|
|
int decimal_point;
|
2006-12-06 17:52:51 +08:00
|
|
|
|
|
|
|
if (precision >= NDIG - 1) {
|
|
|
|
precision = NDIG - 2;
|
|
|
|
}
|
2002-04-10 09:07:49 +08:00
|
|
|
|
2007-02-26 20:05:52 +08:00
|
|
|
if (format == 'F') {
|
2006-12-19 19:54:38 +08:00
|
|
|
p_orig = p = php_fcvt(num, precision, &decimal_point, is_negative);
|
2007-02-26 20:05:52 +08:00
|
|
|
} else { /* either e or E format */
|
2006-12-19 19:54:38 +08:00
|
|
|
p_orig = p = php_ecvt(num, precision + 1, &decimal_point, is_negative);
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
2002-04-10 09:07:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for Infinity and NaN
|
|
|
|
*/
|
|
|
|
if (isalpha((int)*p)) {
|
2002-05-07 13:26:30 +08:00
|
|
|
*len = strlen(p);
|
|
|
|
memcpy(buf, p, *len + 1);
|
2021-07-15 01:59:51 +08:00
|
|
|
*is_negative = false;
|
2006-12-06 17:52:51 +08:00
|
|
|
free(p_orig);
|
2002-04-10 09:07:49 +08:00
|
|
|
return (buf);
|
|
|
|
}
|
2006-12-18 17:26:54 +08:00
|
|
|
if (format == 'F') {
|
2002-04-10 09:07:49 +08:00
|
|
|
if (decimal_point <= 0) {
|
2006-12-06 17:52:51 +08:00
|
|
|
if (num != 0 || precision > 0) {
|
|
|
|
*s++ = '0';
|
|
|
|
if (precision > 0) {
|
2006-12-18 17:26:54 +08:00
|
|
|
*s++ = dec_point;
|
2007-02-26 20:05:52 +08:00
|
|
|
while (decimal_point++ < 0) {
|
2006-12-06 17:52:51 +08:00
|
|
|
*s++ = '0';
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
2006-12-06 17:52:51 +08:00
|
|
|
} else if (add_dp) {
|
2006-12-18 17:26:54 +08:00
|
|
|
*s++ = dec_point;
|
2006-12-06 17:52:51 +08:00
|
|
|
}
|
2002-04-10 09:07:49 +08:00
|
|
|
}
|
|
|
|
} else {
|
2005-12-26 21:41:58 +08:00
|
|
|
int addz = decimal_point >= NDIG ? decimal_point - NDIG + 1 : 0;
|
|
|
|
decimal_point -= addz;
|
2002-04-10 09:07:49 +08:00
|
|
|
while (decimal_point-- > 0) {
|
|
|
|
*s++ = *p++;
|
|
|
|
}
|
2005-12-26 21:41:58 +08:00
|
|
|
while (addz-- > 0) {
|
|
|
|
*s++ = '0';
|
|
|
|
}
|
2002-04-10 09:07:49 +08:00
|
|
|
if (precision > 0 || add_dp) {
|
2006-12-18 17:26:54 +08:00
|
|
|
*s++ = dec_point;
|
2002-04-10 09:07:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*s++ = *p++;
|
2007-02-26 20:05:52 +08:00
|
|
|
if (precision > 0 || add_dp) {
|
2002-04-10 09:07:49 +08:00
|
|
|
*s++ = '.';
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
2002-04-10 09:07:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy the rest of p, the NUL is NOT copied
|
|
|
|
*/
|
2007-02-26 20:05:52 +08:00
|
|
|
while (*p) {
|
2002-04-10 09:07:49 +08:00
|
|
|
*s++ = *p++;
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
2002-04-10 09:07:49 +08:00
|
|
|
|
2006-12-18 17:26:54 +08:00
|
|
|
if (format != 'F') {
|
2002-04-10 09:07:49 +08:00
|
|
|
char temp[EXPONENT_LENGTH]; /* for exponent conversion */
|
2014-08-26 02:22:49 +08:00
|
|
|
size_t t_len;
|
2021-07-15 01:59:51 +08:00
|
|
|
bool exponent_is_negative;
|
2002-04-10 09:07:49 +08:00
|
|
|
|
|
|
|
*s++ = format; /* either e or E */
|
|
|
|
decimal_point--;
|
|
|
|
if (decimal_point != 0) {
|
2021-07-15 06:41:31 +08:00
|
|
|
p = ap_php_conv_10((int64_t) decimal_point, false, &exponent_is_negative, &temp[EXPONENT_LENGTH], &t_len);
|
2002-04-10 09:07:49 +08:00
|
|
|
*s++ = exponent_is_negative ? '-' : '+';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the exponent has at least 2 digits
|
|
|
|
*/
|
2007-02-26 20:05:52 +08:00
|
|
|
while (t_len--) {
|
2002-04-10 09:07:49 +08:00
|
|
|
*s++ = *p++;
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
2002-04-10 09:07:49 +08:00
|
|
|
} else {
|
|
|
|
*s++ = '+';
|
|
|
|
*s++ = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*len = s - buf;
|
2006-12-06 17:52:51 +08:00
|
|
|
free(p_orig);
|
2002-04-10 09:07:49 +08:00
|
|
|
return (buf);
|
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
/* }}} */
|
2002-04-10 09:07:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert num to a base X number where X is a power of 2. nbits determines X.
|
|
|
|
* For example, if nbits is 3, we do base 8 conversion
|
|
|
|
* Return value:
|
|
|
|
* a pointer to a string containing the number
|
|
|
|
*
|
|
|
|
* The caller provides a buffer for the string: that is the buf_end argument
|
|
|
|
* which is a pointer to the END of the buffer + 1 (i.e. if the buffer
|
|
|
|
* is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
|
|
|
|
*/
|
2021-07-15 06:43:22 +08:00
|
|
|
PHPAPI char * ap_php_conv_p2(uint64_t num, int nbits, char format, char *buf_end, size_t *len) /* {{{ */
|
2002-04-10 09:07:49 +08:00
|
|
|
{
|
2021-07-15 06:43:22 +08:00
|
|
|
int mask = (1 << nbits) - 1;
|
|
|
|
char *p = buf_end;
|
2017-12-15 03:14:36 +08:00
|
|
|
static const char low_digits[] = "0123456789abcdef";
|
|
|
|
static const char upper_digits[] = "0123456789ABCDEF";
|
2021-07-15 06:43:22 +08:00
|
|
|
const char *digits = (format == 'X') ? upper_digits : low_digits;
|
2002-04-10 09:07:49 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
*--p = digits[num & mask];
|
|
|
|
num >>= nbits;
|
|
|
|
}
|
|
|
|
while (num);
|
|
|
|
|
|
|
|
*len = buf_end - p;
|
|
|
|
return (p);
|
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
/* }}} */
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions
|
|
|
|
*
|
|
|
|
* XXX: this is a magic number; do not decrease it
|
2011-07-11 17:47:59 +08:00
|
|
|
* Emax = 1023
|
|
|
|
* NDIG = 320
|
2021-03-31 11:49:41 +08:00
|
|
|
* NUM_BUF_SIZE >= strlen("-") + Emax + strlen(".") + NDIG + strlen("E+1023") + 1;
|
1999-04-08 05:05:13 +08:00
|
|
|
*/
|
2011-07-11 17:47:59 +08:00
|
|
|
#define NUM_BUF_SIZE 2048
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Descriptor for buffer area
|
|
|
|
*/
|
|
|
|
struct buf_area {
|
|
|
|
char *buf_end;
|
|
|
|
char *nextb; /* pointer to next byte to read/write */
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct buf_area buffy;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The INS_CHAR macro inserts a character in the buffer and writes
|
|
|
|
* the buffer back to disk if necessary
|
|
|
|
* It uses the char pointers sp and bep:
|
|
|
|
* sp points to the next available character in the buffer
|
|
|
|
* bep points to the end-of-buffer+1
|
|
|
|
* While using this macro, note that the nextb pointer is NOT updated.
|
|
|
|
*
|
2023-04-28 17:05:32 +08:00
|
|
|
* NOTE: Evaluation of the c argument should not have any side effects
|
1999-04-08 05:05:13 +08:00
|
|
|
*/
|
2002-08-21 09:11:50 +08:00
|
|
|
#define INS_CHAR(c, sp, bep, cc) \
|
|
|
|
{ \
|
|
|
|
if (sp < bep) \
|
|
|
|
{ \
|
|
|
|
*sp++ = c; \
|
|
|
|
} \
|
|
|
|
cc++; \
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
#define NUM( c ) ( c - '0' )
|
|
|
|
|
|
|
|
#define STR_TO_DEC( str, num ) \
|
|
|
|
num = NUM( *str++ ) ; \
|
|
|
|
while ( isdigit((int)*str ) ) \
|
|
|
|
{ \
|
|
|
|
num *= 10 ; \
|
|
|
|
num += NUM( *str++ ) ; \
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This macro does zero padding so that the precision
|
|
|
|
* requirement is satisfied. The padding is done by
|
|
|
|
* adding '0's to the left of the string that is going
|
|
|
|
* to be printed.
|
|
|
|
*/
|
|
|
|
#define FIX_PRECISION( adjust, precision, s, s_len ) \
|
2021-06-29 16:04:10 +08:00
|
|
|
if ( adjust ) \
|
2016-06-22 01:12:29 +08:00
|
|
|
while ( s_len < (size_t)precision ) \
|
|
|
|
{ \
|
|
|
|
*--s = '0' ; \
|
|
|
|
s_len++ ; \
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Macro that does padding. The padding is done by printing
|
|
|
|
* the character ch.
|
|
|
|
*/
|
|
|
|
#define PAD( width, len, ch ) do \
|
|
|
|
{ \
|
|
|
|
INS_CHAR( ch, sp, bep, cc ) ; \
|
|
|
|
width-- ; \
|
|
|
|
} \
|
2019-06-12 19:02:56 +08:00
|
|
|
while ( (size_t)width > len )
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do format conversion placing the output in buffer
|
|
|
|
*/
|
2021-07-15 06:43:22 +08:00
|
|
|
static size_t format_converter(buffy * odp, const char *fmt, va_list ap) /* {{{ */
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
2008-02-08 02:41:35 +08:00
|
|
|
char *sp;
|
|
|
|
char *bep;
|
2021-07-15 02:49:23 +08:00
|
|
|
size_t cc = 0;
|
2014-08-26 02:22:49 +08:00
|
|
|
size_t i;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2008-02-08 02:41:35 +08:00
|
|
|
char *s = NULL;
|
2014-08-26 02:22:49 +08:00
|
|
|
size_t s_len;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2008-02-08 02:41:35 +08:00
|
|
|
int min_width = 0;
|
1999-04-08 05:05:13 +08:00
|
|
|
int precision = 0;
|
|
|
|
enum {
|
|
|
|
LEFT, RIGHT
|
|
|
|
} adjust;
|
|
|
|
char pad_char;
|
|
|
|
char prefix_char;
|
|
|
|
|
|
|
|
double fp_num;
|
2021-07-15 06:41:31 +08:00
|
|
|
int64_t i_num = (int64_t) 0;
|
2021-07-15 06:33:35 +08:00
|
|
|
uint64_t ui_num;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
char num_buf[NUM_BUF_SIZE];
|
|
|
|
char char_buf[2]; /* for printing %% and %<unknown> */
|
|
|
|
|
2014-12-06 18:59:43 +08:00
|
|
|
#ifdef ZTS
|
|
|
|
struct lconv lconv;
|
|
|
|
#else
|
2006-12-19 19:54:38 +08:00
|
|
|
struct lconv *lconv = NULL;
|
2006-12-19 21:13:29 +08:00
|
|
|
#endif
|
2006-12-19 19:54:38 +08:00
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
/*
|
|
|
|
* Flag variables
|
|
|
|
*/
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
length_modifier_e modifier;
|
2021-07-15 02:09:39 +08:00
|
|
|
bool alternate_form;
|
|
|
|
bool print_sign;
|
|
|
|
bool print_blank;
|
|
|
|
bool adjust_precision;
|
|
|
|
bool adjust_width;
|
2021-07-15 01:59:51 +08:00
|
|
|
bool is_negative;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
sp = odp->nextb;
|
|
|
|
bep = odp->buf_end;
|
|
|
|
|
|
|
|
while (*fmt) {
|
|
|
|
if (*fmt != '%') {
|
|
|
|
INS_CHAR(*fmt, sp, bep, cc);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Default variable settings
|
|
|
|
*/
|
2021-06-10 16:12:29 +08:00
|
|
|
zend_string *tmp_str = NULL;
|
1999-04-08 05:05:13 +08:00
|
|
|
adjust = RIGHT;
|
2021-07-15 02:09:39 +08:00
|
|
|
alternate_form = print_sign = print_blank = false;
|
1999-04-08 05:05:13 +08:00
|
|
|
pad_char = ' ';
|
|
|
|
prefix_char = NUL;
|
|
|
|
|
|
|
|
fmt++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to avoid checking for flags, width or precision
|
|
|
|
*/
|
|
|
|
if (isascii((int)*fmt) && !islower((int)*fmt)) {
|
|
|
|
/*
|
|
|
|
* Recognize flags: -, #, BLANK, +
|
|
|
|
*/
|
|
|
|
for (;; fmt++) {
|
|
|
|
if (*fmt == '-')
|
|
|
|
adjust = LEFT;
|
|
|
|
else if (*fmt == '+')
|
2021-07-15 02:09:39 +08:00
|
|
|
print_sign = true;
|
1999-04-08 05:05:13 +08:00
|
|
|
else if (*fmt == '#')
|
2021-07-15 02:09:39 +08:00
|
|
|
alternate_form = true;
|
1999-04-08 05:05:13 +08:00
|
|
|
else if (*fmt == ' ')
|
2021-07-15 02:09:39 +08:00
|
|
|
print_blank = true;
|
1999-04-08 05:05:13 +08:00
|
|
|
else if (*fmt == '0')
|
|
|
|
pad_char = '0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if a width was specified
|
|
|
|
*/
|
|
|
|
if (isdigit((int)*fmt)) {
|
|
|
|
STR_TO_DEC(fmt, min_width);
|
2021-07-15 02:09:39 +08:00
|
|
|
adjust_width = true;
|
1999-04-08 05:05:13 +08:00
|
|
|
} else if (*fmt == '*') {
|
|
|
|
min_width = va_arg(ap, int);
|
|
|
|
fmt++;
|
2021-07-15 02:09:39 +08:00
|
|
|
adjust_width = true;
|
1999-04-08 05:05:13 +08:00
|
|
|
if (min_width < 0) {
|
|
|
|
adjust = LEFT;
|
|
|
|
min_width = -min_width;
|
|
|
|
}
|
|
|
|
} else
|
2021-07-15 02:09:39 +08:00
|
|
|
adjust_width = false;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if a precision was specified
|
|
|
|
*/
|
|
|
|
if (*fmt == '.') {
|
2021-07-15 02:09:39 +08:00
|
|
|
adjust_precision = true;
|
1999-04-08 05:05:13 +08:00
|
|
|
fmt++;
|
|
|
|
if (isdigit((int)*fmt)) {
|
|
|
|
STR_TO_DEC(fmt, precision);
|
|
|
|
} else if (*fmt == '*') {
|
|
|
|
precision = va_arg(ap, int);
|
|
|
|
fmt++;
|
Bring minimum precision inline with spprintf
The precision "minimum" for spprintf was changed in
3f23e6bca90545a180ac4dbc80712065b1281d74 with the cryptic comment "Enable 0
mode for echo/print". Since then the behaviour of spprintf and snprintf has not
been the same. This results in some APIs handling precision differently than
others, which then resulted in the following Xdebug issue:
https://bugs.xdebug.org/view.php?id=2151
The "manpage" for snprinf says about precision:
An optional precision, in the form of a period ('.') followed by an
optional decimal digit string. Instead of a decimal digit string one
may write "*" or "*m$" (for some decimal integer m) to specify that the
precision is given in the next argument, or in the m-th argument, re‐
spectively, which must be of type int. If the precision is given as
just '.', the precision is taken to be zero. A negative precision is
taken as if the precision were omitted.
However, the snprintf implementation never supported this "negative precision",
which is what PHP's default setting is in PG(precision). However, in
3f23e6bca90545a180ac4dbc80712065b1281d74 spprintf was made to support this.
Although this techinically can break BC, there is clearly a bug here, and I
could not see any failing tests locally.
2023-01-31 02:57:12 +08:00
|
|
|
if (precision < -1)
|
|
|
|
precision = -1;
|
1999-04-08 05:05:13 +08:00
|
|
|
} else
|
|
|
|
precision = 0;
|
|
|
|
} else
|
2021-07-15 02:09:39 +08:00
|
|
|
adjust_precision = false;
|
1999-04-08 05:05:13 +08:00
|
|
|
} else
|
2021-07-15 02:09:39 +08:00
|
|
|
adjust_precision = adjust_width = false;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Modifier check
|
|
|
|
*/
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
switch (*fmt) {
|
2003-09-14 17:50:36 +08:00
|
|
|
case 'L':
|
|
|
|
fmt++;
|
|
|
|
modifier = LM_LONG_DOUBLE;
|
|
|
|
break;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
case 'l':
|
|
|
|
fmt++;
|
|
|
|
#if SIZEOF_LONG_LONG
|
|
|
|
if (*fmt == 'l') {
|
|
|
|
fmt++;
|
|
|
|
modifier = LM_LONG_LONG;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
modifier = LM_LONG;
|
|
|
|
break;
|
|
|
|
case 'z':
|
2003-09-14 17:50:36 +08:00
|
|
|
fmt++;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
modifier = LM_SIZE_T;
|
|
|
|
break;
|
|
|
|
case 'j':
|
2003-09-14 17:50:36 +08:00
|
|
|
fmt++;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
#if SIZEOF_INTMAX_T
|
|
|
|
modifier = LM_INTMAX_T;
|
|
|
|
#else
|
|
|
|
modifier = LM_SIZE_T;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 't':
|
2003-09-14 17:50:36 +08:00
|
|
|
fmt++;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
#if SIZEOF_PTRDIFF_T
|
|
|
|
modifier = LM_PTRDIFF_T;
|
|
|
|
#else
|
|
|
|
modifier = LM_SIZE_T;
|
|
|
|
#endif
|
|
|
|
break;
|
2014-08-16 17:16:11 +08:00
|
|
|
case 'p':
|
2021-06-16 21:14:35 +08:00
|
|
|
{
|
|
|
|
char __next = *(fmt+1);
|
|
|
|
if ('d' == __next || 'u' == __next || 'x' == __next || 'o' == __next) {
|
|
|
|
zend_error_noreturn(E_CORE_ERROR,
|
|
|
|
"printf \"p\" modifier is no longer supported, use ZEND_LONG_FMT");
|
|
|
|
}
|
|
|
|
modifier = LM_STD;
|
2014-08-16 17:16:11 +08:00
|
|
|
break;
|
2021-06-16 21:14:35 +08:00
|
|
|
}
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
case 'h':
|
|
|
|
fmt++;
|
|
|
|
if (*fmt == 'h') {
|
|
|
|
fmt++;
|
|
|
|
}
|
|
|
|
/* these are promoted to int, so no break */
|
2020-09-30 06:25:49 +08:00
|
|
|
ZEND_FALLTHROUGH;
|
2008-02-07 20:47:44 +08:00
|
|
|
default:
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
modifier = LM_STD;
|
|
|
|
break;
|
2003-09-14 00:49:24 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Argument extraction and printing.
|
|
|
|
* First we determine the argument type.
|
|
|
|
* Then, we convert the argument to a string.
|
|
|
|
* On exit from the switch, s points to the string that
|
|
|
|
* must be printed, s_len has the length of the string
|
|
|
|
* The precision requirements, if any, are reflected in s_len.
|
|
|
|
*
|
|
|
|
* NOTE: pad_char may be set to '0' because of the 0 flag.
|
|
|
|
* It is reset to ' ' by non-numeric formats
|
|
|
|
*/
|
|
|
|
switch (*fmt) {
|
2014-08-11 13:09:46 +08:00
|
|
|
case 'Z': {
|
2021-06-10 16:12:29 +08:00
|
|
|
zval *zvp = va_arg(ap, zval*);
|
|
|
|
zend_string *str = zval_get_tmp_string(zvp, &tmp_str);
|
|
|
|
s_len = ZSTR_LEN(str);
|
|
|
|
s = ZSTR_VAL(str);
|
2016-06-22 01:12:29 +08:00
|
|
|
if (adjust_precision && (size_t)precision < s_len) {
|
2008-02-08 02:41:35 +08:00
|
|
|
s_len = precision;
|
2015-01-03 17:22:58 +08:00
|
|
|
}
|
2008-02-08 02:41:35 +08:00
|
|
|
break;
|
2014-08-11 13:09:46 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
case 'u':
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
switch(modifier) {
|
|
|
|
default:
|
2021-07-15 06:41:31 +08:00
|
|
|
i_num = (int64_t) va_arg(ap, unsigned int);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
2003-09-14 17:50:36 +08:00
|
|
|
case LM_LONG_DOUBLE:
|
|
|
|
goto fmt_error;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
case LM_LONG:
|
2021-07-15 06:41:31 +08:00
|
|
|
i_num = (int64_t) va_arg(ap, unsigned long int);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
case LM_SIZE_T:
|
2021-07-15 06:41:31 +08:00
|
|
|
i_num = (int64_t) va_arg(ap, size_t);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#if SIZEOF_LONG_LONG
|
|
|
|
case LM_LONG_LONG:
|
2021-07-15 06:41:31 +08:00
|
|
|
i_num = (int64_t) va_arg(ap, unsigned long long int);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_INTMAX_T
|
|
|
|
case LM_INTMAX_T:
|
2021-07-15 06:41:31 +08:00
|
|
|
i_num = (int64_t) va_arg(ap, uintmax_t);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_PTRDIFF_T
|
|
|
|
case LM_PTRDIFF_T:
|
2021-07-15 06:41:31 +08:00
|
|
|
i_num = (int64_t) va_arg(ap, ptrdiff_t);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
/*
|
|
|
|
* The rest also applies to other integer formats, so fall
|
|
|
|
* into that case.
|
|
|
|
*/
|
2020-09-30 06:25:49 +08:00
|
|
|
ZEND_FALLTHROUGH;
|
1999-04-08 05:05:13 +08:00
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
/*
|
|
|
|
* Get the arg if we haven't already.
|
|
|
|
*/
|
|
|
|
if ((*fmt) != 'u') {
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
switch(modifier) {
|
|
|
|
default:
|
2021-07-15 06:41:31 +08:00
|
|
|
i_num = (int64_t) va_arg(ap, int);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
2003-09-14 17:50:36 +08:00
|
|
|
case LM_LONG_DOUBLE:
|
|
|
|
goto fmt_error;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
case LM_LONG:
|
2021-07-15 06:41:31 +08:00
|
|
|
i_num = (int64_t) va_arg(ap, long int);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
case LM_SIZE_T:
|
|
|
|
#if SIZEOF_SSIZE_T
|
2021-07-15 06:41:31 +08:00
|
|
|
i_num = (int64_t) va_arg(ap, ssize_t);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
#else
|
2021-07-15 06:41:31 +08:00
|
|
|
i_num = (int64_t) va_arg(ap, size_t);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
#if SIZEOF_LONG_LONG
|
|
|
|
case LM_LONG_LONG:
|
2021-07-15 06:41:31 +08:00
|
|
|
i_num = (int64_t) va_arg(ap, long long int);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_INTMAX_T
|
|
|
|
case LM_INTMAX_T:
|
2021-07-15 06:41:31 +08:00
|
|
|
i_num = (int64_t) va_arg(ap, intmax_t);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_PTRDIFF_T
|
|
|
|
case LM_PTRDIFF_T:
|
2021-07-15 06:41:31 +08:00
|
|
|
i_num = (int64_t) va_arg(ap, ptrdiff_t);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2002-04-10 09:07:49 +08:00
|
|
|
s = ap_php_conv_10(i_num, (*fmt) == 'u', &is_negative,
|
1999-04-08 05:05:13 +08:00
|
|
|
&num_buf[NUM_BUF_SIZE], &s_len);
|
|
|
|
FIX_PRECISION(adjust_precision, precision, s, s_len);
|
|
|
|
|
|
|
|
if (*fmt != 'u') {
|
2007-02-26 20:05:52 +08:00
|
|
|
if (is_negative) {
|
1999-04-08 05:05:13 +08:00
|
|
|
prefix_char = '-';
|
2007-02-26 20:05:52 +08:00
|
|
|
} else if (print_sign) {
|
1999-04-08 05:05:13 +08:00
|
|
|
prefix_char = '+';
|
2007-02-26 20:05:52 +08:00
|
|
|
} else if (print_blank) {
|
1999-04-08 05:05:13 +08:00
|
|
|
prefix_char = ' ';
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'o':
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
switch(modifier) {
|
|
|
|
default:
|
2021-07-15 06:33:35 +08:00
|
|
|
ui_num = (uint64_t) va_arg(ap, unsigned int);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
2003-09-14 17:50:36 +08:00
|
|
|
case LM_LONG_DOUBLE:
|
|
|
|
goto fmt_error;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
case LM_LONG:
|
2021-07-15 06:33:35 +08:00
|
|
|
ui_num = (uint64_t) va_arg(ap, unsigned long int);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
case LM_SIZE_T:
|
2021-07-15 06:33:35 +08:00
|
|
|
ui_num = (uint64_t) va_arg(ap, size_t);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#if SIZEOF_LONG_LONG
|
|
|
|
case LM_LONG_LONG:
|
2021-07-15 06:33:35 +08:00
|
|
|
ui_num = (uint64_t) va_arg(ap, unsigned long long int);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_INTMAX_T
|
|
|
|
case LM_INTMAX_T:
|
2021-07-15 06:33:35 +08:00
|
|
|
ui_num = (uint64_t) va_arg(ap, uintmax_t);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_PTRDIFF_T
|
|
|
|
case LM_PTRDIFF_T:
|
2021-07-15 06:33:35 +08:00
|
|
|
ui_num = (uint64_t) va_arg(ap, ptrdiff_t);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
s = ap_php_conv_p2(ui_num, 3, *fmt, &num_buf[NUM_BUF_SIZE], &s_len);
|
1999-04-08 05:05:13 +08:00
|
|
|
FIX_PRECISION(adjust_precision, precision, s, s_len);
|
|
|
|
if (alternate_form && *s != '0') {
|
|
|
|
*--s = '0';
|
|
|
|
s_len++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
switch(modifier) {
|
|
|
|
default:
|
2021-07-15 06:33:35 +08:00
|
|
|
ui_num = (uint64_t) va_arg(ap, unsigned int);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
2003-09-14 17:50:36 +08:00
|
|
|
case LM_LONG_DOUBLE:
|
|
|
|
goto fmt_error;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
case LM_LONG:
|
2021-07-15 06:33:35 +08:00
|
|
|
ui_num = (uint64_t) va_arg(ap, unsigned long int);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
case LM_SIZE_T:
|
2021-07-15 06:33:35 +08:00
|
|
|
ui_num = (uint64_t) va_arg(ap, size_t);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#if SIZEOF_LONG_LONG
|
|
|
|
case LM_LONG_LONG:
|
2021-07-15 06:33:35 +08:00
|
|
|
ui_num = (uint64_t) va_arg(ap, unsigned long long int);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_INTMAX_T
|
|
|
|
case LM_INTMAX_T:
|
2021-07-15 06:33:35 +08:00
|
|
|
ui_num = (uint64_t) va_arg(ap, uintmax_t);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_PTRDIFF_T
|
|
|
|
case LM_PTRDIFF_T:
|
2021-07-15 06:33:35 +08:00
|
|
|
ui_num = (uint64_t) va_arg(ap, ptrdiff_t);
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
s = ap_php_conv_p2(ui_num, 4, *fmt, &num_buf[NUM_BUF_SIZE], &s_len);
|
1999-04-08 05:05:13 +08:00
|
|
|
FIX_PRECISION(adjust_precision, precision, s, s_len);
|
|
|
|
if (alternate_form && i_num != 0) {
|
|
|
|
*--s = *fmt; /* 'x' or 'X' */
|
|
|
|
*--s = '0';
|
|
|
|
s_len += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
s = va_arg(ap, char *);
|
|
|
|
if (s != NULL) {
|
|
|
|
s_len = strlen(s);
|
2016-06-22 01:12:29 +08:00
|
|
|
if (adjust_precision && (size_t)precision < s_len) {
|
1999-04-08 05:05:13 +08:00
|
|
|
s_len = precision;
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
} else {
|
|
|
|
s = S_NULL;
|
|
|
|
s_len = S_NULL_LEN;
|
|
|
|
}
|
|
|
|
pad_char = ' ';
|
|
|
|
break;
|
|
|
|
|
2008-02-07 20:47:44 +08:00
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
case 'f':
|
2006-12-19 19:54:38 +08:00
|
|
|
case 'F':
|
1999-04-08 05:05:13 +08:00
|
|
|
case 'e':
|
|
|
|
case 'E':
|
2003-09-14 17:50:36 +08:00
|
|
|
switch(modifier) {
|
|
|
|
case LM_LONG_DOUBLE:
|
|
|
|
fp_num = (double) va_arg(ap, long double);
|
|
|
|
break;
|
|
|
|
case LM_STD:
|
|
|
|
fp_num = va_arg(ap, double);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fmt_error;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2001-05-02 00:29:52 +08:00
|
|
|
if (zend_isnan(fp_num)) {
|
2006-12-19 21:15:39 +08:00
|
|
|
s = "NAN";
|
2001-05-02 00:29:52 +08:00
|
|
|
s_len = 3;
|
|
|
|
} else if (zend_isinf(fp_num)) {
|
2006-12-19 21:15:39 +08:00
|
|
|
s = "INF";
|
2001-05-02 00:29:52 +08:00
|
|
|
s_len = 3;
|
|
|
|
} else {
|
2014-12-06 18:59:43 +08:00
|
|
|
#ifdef ZTS
|
|
|
|
localeconv_r(&lconv);
|
|
|
|
#else
|
2006-12-19 19:54:38 +08:00
|
|
|
if (!lconv) {
|
|
|
|
lconv = localeconv();
|
|
|
|
}
|
2006-12-19 21:13:29 +08:00
|
|
|
#endif
|
2006-12-19 19:54:38 +08:00
|
|
|
s = php_conv_fp((*fmt == 'f')?'F':*fmt, fp_num, alternate_form,
|
2021-07-15 02:09:39 +08:00
|
|
|
(adjust_precision == false) ? FLOAT_DIGITS : precision,
|
2006-12-19 21:13:29 +08:00
|
|
|
(*fmt == 'f')?LCONV_DECIMAL_POINT:'.',
|
2001-05-02 00:29:52 +08:00
|
|
|
&is_negative, &num_buf[1], &s_len);
|
|
|
|
if (is_negative)
|
|
|
|
prefix_char = '-';
|
|
|
|
else if (print_sign)
|
|
|
|
prefix_char = '+';
|
|
|
|
else if (print_blank)
|
|
|
|
prefix_char = ' ';
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'g':
|
2007-10-01 23:22:41 +08:00
|
|
|
case 'k':
|
1999-04-08 05:05:13 +08:00
|
|
|
case 'G':
|
2007-06-19 20:20:50 +08:00
|
|
|
case 'H':
|
2003-09-14 17:50:36 +08:00
|
|
|
switch(modifier) {
|
|
|
|
case LM_LONG_DOUBLE:
|
|
|
|
fp_num = (double) va_arg(ap, long double);
|
|
|
|
break;
|
|
|
|
case LM_STD:
|
|
|
|
fp_num = va_arg(ap, double);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fmt_error;
|
|
|
|
}
|
2003-09-30 07:44:07 +08:00
|
|
|
|
|
|
|
if (zend_isnan(fp_num)) {
|
2008-02-08 02:41:35 +08:00
|
|
|
s = "NAN";
|
|
|
|
s_len = 3;
|
|
|
|
break;
|
|
|
|
} else if (zend_isinf(fp_num)) {
|
|
|
|
if (fp_num > 0) {
|
|
|
|
s = "INF";
|
|
|
|
s_len = 3;
|
|
|
|
} else {
|
|
|
|
s = "-INF";
|
|
|
|
s_len = 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2003-09-30 07:44:07 +08:00
|
|
|
|
2021-07-15 02:09:39 +08:00
|
|
|
if (adjust_precision == false) {
|
1999-04-08 05:05:13 +08:00
|
|
|
precision = FLOAT_DIGITS;
|
2007-02-26 20:05:52 +08:00
|
|
|
} else if (precision == 0) {
|
1999-04-08 05:05:13 +08:00
|
|
|
precision = 1;
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
/*
|
|
|
|
* * We use &num_buf[ 1 ], so that we have room for the sign
|
|
|
|
*/
|
2014-12-06 18:59:43 +08:00
|
|
|
#ifdef ZTS
|
|
|
|
localeconv_r(&lconv);
|
|
|
|
#else
|
2006-12-19 19:54:38 +08:00
|
|
|
if (!lconv) {
|
|
|
|
lconv = localeconv();
|
|
|
|
}
|
2014-12-06 18:59:43 +08:00
|
|
|
#endif
|
2021-08-02 20:40:01 +08:00
|
|
|
s = zend_gcvt(fp_num, precision, (*fmt=='H' || *fmt == 'k') ? '.' : LCONV_DECIMAL_POINT, (*fmt == 'G' || *fmt == 'H')?'E':'e', &num_buf[1]);
|
2007-02-26 20:05:52 +08:00
|
|
|
if (*s == '-') {
|
1999-04-08 05:05:13 +08:00
|
|
|
prefix_char = *s++;
|
2007-02-26 20:05:52 +08:00
|
|
|
} else if (print_sign) {
|
1999-04-08 05:05:13 +08:00
|
|
|
prefix_char = '+';
|
2007-02-26 20:05:52 +08:00
|
|
|
} else if (print_blank) {
|
1999-04-08 05:05:13 +08:00
|
|
|
prefix_char = ' ';
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
s_len = strlen(s);
|
|
|
|
|
2011-08-09 10:42:25 +08:00
|
|
|
if (alternate_form && (strchr(s, '.')) == NULL) {
|
1999-04-08 05:05:13 +08:00
|
|
|
s[s_len++] = '.';
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
char_buf[0] = (char) (va_arg(ap, int));
|
|
|
|
s = &char_buf[0];
|
|
|
|
s_len = 1;
|
|
|
|
pad_char = ' ';
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case '%':
|
|
|
|
char_buf[0] = '%';
|
|
|
|
s = &char_buf[0];
|
|
|
|
s_len = 1;
|
|
|
|
pad_char = ' ';
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
*(va_arg(ap, int *)) = cc;
|
2006-01-25 04:59:46 +08:00
|
|
|
goto skip_output;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
/*
|
2008-02-07 20:47:44 +08:00
|
|
|
* Always extract the argument as a "char *" pointer. We
|
|
|
|
* should be using "void *" but there are still machines
|
1999-04-08 05:05:13 +08:00
|
|
|
* that don't understand it.
|
|
|
|
* If the pointer size is equal to the size of an unsigned
|
2008-02-07 20:47:44 +08:00
|
|
|
* integer we convert the pointer to a hex number, otherwise
|
1999-04-08 05:05:13 +08:00
|
|
|
* we print "%p" to indicate that we don't handle "%p".
|
|
|
|
*/
|
|
|
|
case 'p':
|
2021-07-15 06:33:35 +08:00
|
|
|
if (sizeof(char *) <= sizeof(uint64_t)) {
|
|
|
|
ui_num = (uint64_t)((size_t) va_arg(ap, char *));
|
2008-02-07 20:47:44 +08:00
|
|
|
s = ap_php_conv_p2(ui_num, 4, 'x',
|
2003-09-14 00:49:24 +08:00
|
|
|
&num_buf[NUM_BUF_SIZE], &s_len);
|
2004-04-16 07:08:22 +08:00
|
|
|
if (ui_num != 0) {
|
2003-09-14 00:49:24 +08:00
|
|
|
*--s = 'x';
|
|
|
|
*--s = '0';
|
|
|
|
s_len += 2;
|
|
|
|
}
|
|
|
|
} else {
|
1999-04-08 05:05:13 +08:00
|
|
|
s = "%p";
|
|
|
|
s_len = 2;
|
|
|
|
}
|
|
|
|
pad_char = ' ';
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case NUL:
|
|
|
|
/*
|
|
|
|
* The last character of the format string was %.
|
|
|
|
* We ignore it.
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
2003-09-14 17:50:36 +08:00
|
|
|
fmt_error:
|
|
|
|
php_error(E_ERROR, "Illegal length modifier specified '%c' in s[np]printf call", *fmt);
|
1999-04-08 05:05:13 +08:00
|
|
|
/*
|
|
|
|
* The default case is for unrecognized %'s.
|
|
|
|
* We print %<char> to help the user identify what
|
|
|
|
* option is not understood.
|
|
|
|
* This is also useful in case the user wants to pass
|
|
|
|
* the output of format_converter to another function
|
|
|
|
* that understands some other %<char> (like syslog).
|
|
|
|
* Note that we can't point s inside fmt because the
|
|
|
|
* unknown <char> could be preceded by width etc.
|
|
|
|
*/
|
2020-09-30 06:25:49 +08:00
|
|
|
ZEND_FALLTHROUGH;
|
1999-04-08 05:05:13 +08:00
|
|
|
default:
|
|
|
|
char_buf[0] = '%';
|
|
|
|
char_buf[1] = *fmt;
|
|
|
|
s = char_buf;
|
|
|
|
s_len = 2;
|
|
|
|
pad_char = ' ';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefix_char != NUL) {
|
|
|
|
*--s = prefix_char;
|
|
|
|
s_len++;
|
|
|
|
}
|
2016-06-22 01:12:29 +08:00
|
|
|
if (adjust_width && adjust == RIGHT && (size_t)min_width > s_len) {
|
1999-04-08 05:05:13 +08:00
|
|
|
if (pad_char == '0' && prefix_char != NUL) {
|
|
|
|
INS_CHAR(*s, sp, bep, cc)
|
|
|
|
s++;
|
|
|
|
s_len--;
|
|
|
|
min_width--;
|
|
|
|
}
|
2019-06-12 19:02:56 +08:00
|
|
|
PAD(min_width, s_len, pad_char);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
/*
|
2008-02-07 20:47:44 +08:00
|
|
|
* Print the string s.
|
1999-04-08 05:05:13 +08:00
|
|
|
*/
|
|
|
|
for (i = s_len; i != 0; i--) {
|
|
|
|
INS_CHAR(*s, sp, bep, cc);
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
2016-06-22 01:12:29 +08:00
|
|
|
if (adjust_width && adjust == LEFT && (size_t)min_width > s_len)
|
2019-06-12 19:02:56 +08:00
|
|
|
PAD(min_width, s_len, pad_char);
|
2021-06-10 16:12:29 +08:00
|
|
|
zend_tmp_string_release(tmp_str);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
2006-01-25 04:59:46 +08:00
|
|
|
skip_output:
|
1999-04-08 05:05:13 +08:00
|
|
|
fmt++;
|
|
|
|
}
|
|
|
|
odp->nextb = sp;
|
|
|
|
return (cc);
|
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
/* }}} */
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the general purpose conversion function.
|
|
|
|
*/
|
2021-07-15 02:49:23 +08:00
|
|
|
static size_t strx_printv(char *buf, size_t len, const char *format, va_list ap) /* {{{ */
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
|
|
|
buffy od;
|
2021-07-15 02:49:23 +08:00
|
|
|
size_t cc;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* First initialize the descriptor
|
|
|
|
* Notice that if no length is given, we initialize buf_end to the
|
|
|
|
* highest possible address.
|
|
|
|
*/
|
2002-08-21 09:11:50 +08:00
|
|
|
if (len == 0) {
|
|
|
|
od.buf_end = (char *) ~0;
|
|
|
|
od.nextb = (char *) ~0;
|
|
|
|
} else {
|
|
|
|
od.buf_end = &buf[len-1];
|
|
|
|
od.nextb = buf;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do the conversion
|
|
|
|
*/
|
|
|
|
cc = format_converter(&od, format, ap);
|
2007-02-26 20:05:52 +08:00
|
|
|
if (len != 0 && od.nextb <= od.buf_end) {
|
1999-04-08 05:05:13 +08:00
|
|
|
*(od.nextb) = '\0';
|
2007-02-26 20:05:52 +08:00
|
|
|
}
|
2021-07-15 02:49:23 +08:00
|
|
|
return cc;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
/* }}} */
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2007-02-26 20:05:52 +08:00
|
|
|
PHPAPI int ap_php_slprintf(char *buf, size_t len, const char *format,...) /* {{{ */
|
2007-02-25 02:20:46 +08:00
|
|
|
{
|
2021-07-15 02:49:23 +08:00
|
|
|
size_t cc;
|
2007-02-25 02:20:46 +08:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2021-07-15 02:49:23 +08:00
|
|
|
cc = strx_printv(buf, len, format, ap);
|
2007-02-25 02:20:46 +08:00
|
|
|
va_end(ap);
|
2021-07-15 02:49:23 +08:00
|
|
|
if (cc >= len) {
|
|
|
|
cc = len -1;
|
2007-02-25 02:20:46 +08:00
|
|
|
buf[cc] = '\0';
|
|
|
|
}
|
2021-07-15 02:49:23 +08:00
|
|
|
return (int) cc;
|
2007-02-25 02:20:46 +08:00
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
/* }}} */
|
2007-02-25 02:20:46 +08:00
|
|
|
|
2007-02-26 20:05:52 +08:00
|
|
|
PHPAPI int ap_php_vslprintf(char *buf, size_t len, const char *format, va_list ap) /* {{{ */
|
2007-02-25 02:20:46 +08:00
|
|
|
{
|
2021-07-15 02:49:23 +08:00
|
|
|
size_t cc = strx_printv(buf, len, format, ap);
|
|
|
|
if (cc >= len) {
|
|
|
|
cc = len -1;
|
2007-02-25 02:20:46 +08:00
|
|
|
buf[cc] = '\0';
|
|
|
|
}
|
2021-07-15 02:49:23 +08:00
|
|
|
return (int) cc;
|
2007-02-25 02:20:46 +08:00
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
/* }}} */
|
2007-02-25 02:20:46 +08:00
|
|
|
|
2007-02-26 20:05:52 +08:00
|
|
|
PHPAPI int ap_php_snprintf(char *buf, size_t len, const char *format,...) /* {{{ */
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
2021-07-15 02:49:23 +08:00
|
|
|
size_t cc;
|
1999-04-08 05:05:13 +08:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2021-07-15 02:49:23 +08:00
|
|
|
cc = strx_printv(buf, len, format, ap);
|
1999-04-08 05:05:13 +08:00
|
|
|
va_end(ap);
|
2021-07-15 02:49:23 +08:00
|
|
|
return (int) cc;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
/* }}} */
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2007-02-26 20:05:52 +08:00
|
|
|
PHPAPI int ap_php_vsnprintf(char *buf, size_t len, const char *format, va_list ap) /* {{{ */
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
2021-07-15 02:49:23 +08:00
|
|
|
size_t cc = strx_printv(buf, len, format, ap);
|
|
|
|
return (int) cc;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
2007-02-26 20:05:52 +08:00
|
|
|
/* }}} */
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2008-11-22 06:05:03 +08:00
|
|
|
PHPAPI int ap_php_vasprintf(char **buf, const char *format, va_list ap) /* {{{ */
|
|
|
|
{
|
|
|
|
va_list ap2;
|
|
|
|
int cc;
|
|
|
|
|
|
|
|
va_copy(ap2, ap);
|
|
|
|
cc = ap_php_vsnprintf(NULL, 0, format, ap2);
|
|
|
|
va_end(ap2);
|
|
|
|
|
|
|
|
*buf = NULL;
|
|
|
|
|
|
|
|
if (cc >= 0) {
|
2008-11-28 03:45:27 +08:00
|
|
|
if ((*buf = malloc(++cc)) != NULL) {
|
2008-11-22 06:05:03 +08:00
|
|
|
if ((cc = ap_php_vsnprintf(*buf, cc, format, ap)) < 0) {
|
2008-11-28 03:45:27 +08:00
|
|
|
free(*buf);
|
2008-11-22 06:05:03 +08:00
|
|
|
*buf = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cc;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2008-11-28 03:45:27 +08:00
|
|
|
PHPAPI int ap_php_asprintf(char **buf, const char *format, ...) /* {{{ */
|
|
|
|
{
|
|
|
|
int cc;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
cc = vasprintf(buf, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return cc;
|
|
|
|
}
|
|
|
|
/* }}} */
|