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.

1169 lines
27 KiB

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