You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1355 lines
32 KiB

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