formatted_print.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Stig S�ther Bakken <ssb@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include <math.h> /* modf() */
  17. #include "php.h"
  18. #include "ext/standard/head.h"
  19. #include "php_string.h"
  20. #include "zend_execute.h"
  21. #include <stdio.h>
  22. #include <locale.h>
  23. #ifdef ZTS
  24. #include "ext/standard/php_string.h"
  25. #define LCONV_DECIMAL_POINT (*lconv.decimal_point)
  26. #else
  27. #define LCONV_DECIMAL_POINT (*lconv->decimal_point)
  28. #endif
  29. #define ALIGN_LEFT 0
  30. #define ALIGN_RIGHT 1
  31. #define ADJ_WIDTH 1
  32. #define ADJ_PRECISION 2
  33. #define NUM_BUF_SIZE 500
  34. #define FLOAT_PRECISION 6
  35. #define MAX_FLOAT_PRECISION 53
  36. #if 0
  37. /* trick to control varargs functions through cpp */
  38. # define PRINTF_DEBUG(arg) php_printf arg
  39. #else
  40. # define PRINTF_DEBUG(arg)
  41. #endif
  42. static const char hexchars[] = "0123456789abcdef";
  43. static const char HEXCHARS[] = "0123456789ABCDEF";
  44. /* php_spintf_appendchar() {{{ */
  45. inline static void
  46. php_sprintf_appendchar(zend_string **buffer, size_t *pos, char add)
  47. {
  48. if ((*pos + 1) >= ZSTR_LEN(*buffer)) {
  49. PRINTF_DEBUG(("%s(): ereallocing buffer to %d bytes\n", get_active_function_name(), ZSTR_LEN(*buffer)));
  50. *buffer = zend_string_extend(*buffer, ZSTR_LEN(*buffer) << 1, 0);
  51. }
  52. PRINTF_DEBUG(("sprintf: appending '%c', pos=\n", add, *pos));
  53. ZSTR_VAL(*buffer)[(*pos)++] = add;
  54. }
  55. /* }}} */
  56. /* php_spintf_appendchar() {{{ */
  57. inline static void
  58. php_sprintf_appendchars(zend_string **buffer, size_t *pos, char *add, size_t len)
  59. {
  60. if ((*pos + len) >= ZSTR_LEN(*buffer)) {
  61. size_t nlen = ZSTR_LEN(*buffer);
  62. PRINTF_DEBUG(("%s(): ereallocing buffer to %d bytes\n", get_active_function_name(), ZSTR_LEN(*buffer)));
  63. do {
  64. nlen = nlen << 1;
  65. } while ((*pos + len) >= nlen);
  66. *buffer = zend_string_extend(*buffer, nlen, 0);
  67. }
  68. PRINTF_DEBUG(("sprintf: appending \"%s\", pos=\n", add, *pos));
  69. memcpy(ZSTR_VAL(*buffer) + (*pos), add, len);
  70. *pos += len;
  71. }
  72. /* }}} */
  73. /* php_spintf_appendstring() {{{ */
  74. inline static void
  75. php_sprintf_appendstring(zend_string **buffer, size_t *pos, char *add,
  76. size_t min_width, size_t max_width, char padding,
  77. size_t alignment, size_t len, bool neg, int expprec, int always_sign)
  78. {
  79. size_t npad;
  80. size_t req_size;
  81. size_t copy_len;
  82. size_t m_width;
  83. copy_len = (expprec ? MIN(max_width, len) : len);
  84. npad = (min_width < copy_len) ? 0 : min_width - copy_len;
  85. PRINTF_DEBUG(("sprintf: appendstring(%x, %d, %d, \"%s\", %d, '%c', %d)\n",
  86. *buffer, *pos, ZSTR_LEN(*buffer), add, min_width, padding, alignment));
  87. m_width = MAX(min_width, copy_len);
  88. if(m_width > INT_MAX - *pos - 1) {
  89. zend_error_noreturn(E_ERROR, "Field width %zd is too long", m_width);
  90. }
  91. req_size = *pos + m_width + 1;
  92. if (req_size > ZSTR_LEN(*buffer)) {
  93. size_t size = ZSTR_LEN(*buffer);
  94. while (req_size > size) {
  95. if (size > ZEND_SIZE_MAX/2) {
  96. zend_error_noreturn(E_ERROR, "Field width %zd is too long", req_size);
  97. }
  98. size <<= 1;
  99. }
  100. PRINTF_DEBUG(("sprintf ereallocing buffer to %d bytes\n", size));
  101. *buffer = zend_string_extend(*buffer, size, 0);
  102. }
  103. if (alignment == ALIGN_RIGHT) {
  104. if ((neg || always_sign) && padding=='0') {
  105. ZSTR_VAL(*buffer)[(*pos)++] = (neg) ? '-' : '+';
  106. add++;
  107. len--;
  108. copy_len--;
  109. }
  110. while (npad-- > 0) {
  111. ZSTR_VAL(*buffer)[(*pos)++] = padding;
  112. }
  113. }
  114. PRINTF_DEBUG(("sprintf: appending \"%s\"\n", add));
  115. memcpy(&ZSTR_VAL(*buffer)[*pos], add, copy_len + 1);
  116. *pos += copy_len;
  117. if (alignment == ALIGN_LEFT) {
  118. while (npad--) {
  119. ZSTR_VAL(*buffer)[(*pos)++] = padding;
  120. }
  121. }
  122. }
  123. /* }}} */
  124. /* php_spintf_appendint() {{{ */
  125. inline static void
  126. php_sprintf_appendint(zend_string **buffer, size_t *pos, zend_long number,
  127. size_t width, char padding, size_t alignment,
  128. int always_sign)
  129. {
  130. char numbuf[NUM_BUF_SIZE];
  131. zend_ulong magn, nmagn;
  132. unsigned int i = NUM_BUF_SIZE - 1, neg = 0;
  133. PRINTF_DEBUG(("sprintf: appendint(%x, %x, %x, %d, %d, '%c', %d)\n",
  134. *buffer, pos, &ZSTR_LEN(*buffer), number, width, padding, alignment));
  135. if (number < 0) {
  136. neg = 1;
  137. magn = ((zend_ulong) -(number + 1)) + 1;
  138. } else {
  139. magn = (zend_ulong) number;
  140. }
  141. /* Can't right-pad 0's on integers */
  142. if(alignment==0 && padding=='0') padding=' ';
  143. numbuf[i] = '\0';
  144. do {
  145. nmagn = magn / 10;
  146. numbuf[--i] = (unsigned char)(magn - (nmagn * 10)) + '0';
  147. magn = nmagn;
  148. }
  149. while (magn > 0 && i > 1);
  150. if (neg) {
  151. numbuf[--i] = '-';
  152. } else if (always_sign) {
  153. numbuf[--i] = '+';
  154. }
  155. PRINTF_DEBUG(("sprintf: appending %d as \"%s\", i=%d\n",
  156. number, &numbuf[i], i));
  157. php_sprintf_appendstring(buffer, pos, &numbuf[i], width, 0,
  158. padding, alignment, (NUM_BUF_SIZE - 1) - i,
  159. neg, 0, always_sign);
  160. }
  161. /* }}} */
  162. /* php_spintf_appenduint() {{{ */
  163. inline static void
  164. php_sprintf_appenduint(zend_string **buffer, size_t *pos,
  165. zend_ulong number,
  166. size_t width, char padding, size_t alignment)
  167. {
  168. char numbuf[NUM_BUF_SIZE];
  169. zend_ulong magn, nmagn;
  170. unsigned int i = NUM_BUF_SIZE - 1;
  171. PRINTF_DEBUG(("sprintf: appenduint(%x, %x, %x, %d, %d, '%c', %d)\n",
  172. *buffer, pos, &ZSTR_LEN(*buffer), number, width, padding, alignment));
  173. magn = (zend_ulong) number;
  174. /* Can't right-pad 0's on integers */
  175. if (alignment == 0 && padding == '0') padding = ' ';
  176. numbuf[i] = '\0';
  177. do {
  178. nmagn = magn / 10;
  179. numbuf[--i] = (unsigned char)(magn - (nmagn * 10)) + '0';
  180. magn = nmagn;
  181. } while (magn > 0 && i > 0);
  182. PRINTF_DEBUG(("sprintf: appending %d as \"%s\", i=%d\n", number, &numbuf[i], i));
  183. php_sprintf_appendstring(buffer, pos, &numbuf[i], width, 0,
  184. padding, alignment, (NUM_BUF_SIZE - 1) - i, /* neg */ false, 0, 0);
  185. }
  186. /* }}} */
  187. /* php_spintf_appenddouble() {{{ */
  188. inline static void
  189. php_sprintf_appenddouble(zend_string **buffer, size_t *pos,
  190. double number,
  191. size_t width, char padding,
  192. size_t alignment, int precision,
  193. int adjust, char fmt,
  194. int always_sign
  195. )
  196. {
  197. char num_buf[NUM_BUF_SIZE];
  198. char *s = NULL;
  199. size_t s_len = 0;
  200. bool is_negative = false;
  201. #ifdef ZTS
  202. struct lconv lconv;
  203. #else
  204. struct lconv *lconv;
  205. #endif
  206. PRINTF_DEBUG(("sprintf: appenddouble(%x, %x, %x, %f, %d, '%c', %d, %c)\n",
  207. *buffer, pos, &ZSTR_LEN(*buffer), number, width, padding, alignment, fmt));
  208. if ((adjust & ADJ_PRECISION) == 0) {
  209. precision = FLOAT_PRECISION;
  210. } else if (precision > MAX_FLOAT_PRECISION) {
  211. php_error_docref(NULL, E_NOTICE, "Requested precision of %d digits was truncated to PHP maximum of %d digits", precision, MAX_FLOAT_PRECISION);
  212. precision = MAX_FLOAT_PRECISION;
  213. }
  214. if (zend_isnan(number)) {
  215. is_negative = (number<0);
  216. php_sprintf_appendstring(buffer, pos, "NaN", 3, 0, padding,
  217. alignment, 3, is_negative, 0, always_sign);
  218. return;
  219. }
  220. if (zend_isinf(number)) {
  221. is_negative = (number<0);
  222. php_sprintf_appendstring(buffer, pos, "INF", 3, 0, padding,
  223. alignment, 3, is_negative, 0, always_sign);
  224. return;
  225. }
  226. switch (fmt) {
  227. case 'e':
  228. case 'E':
  229. case 'f':
  230. case 'F':
  231. #ifdef ZTS
  232. localeconv_r(&lconv);
  233. #else
  234. lconv = localeconv();
  235. #endif
  236. s = php_conv_fp((fmt == 'f')?'F':fmt, number, 0, precision,
  237. (fmt == 'f')?LCONV_DECIMAL_POINT:'.',
  238. &is_negative, &num_buf[1], &s_len);
  239. if (is_negative) {
  240. num_buf[0] = '-';
  241. s = num_buf;
  242. s_len++;
  243. } else if (always_sign) {
  244. num_buf[0] = '+';
  245. s = num_buf;
  246. s_len++;
  247. }
  248. break;
  249. case 'g':
  250. case 'G':
  251. case 'h':
  252. case 'H':
  253. {
  254. if (precision == 0)
  255. precision = 1;
  256. char decimal_point = '.';
  257. if (fmt == 'g' || fmt == 'G') {
  258. #ifdef ZTS
  259. localeconv_r(&lconv);
  260. #else
  261. lconv = localeconv();
  262. #endif
  263. decimal_point = LCONV_DECIMAL_POINT;
  264. }
  265. char exp_char = fmt == 'G' || fmt == 'H' ? 'E' : 'e';
  266. /* We use &num_buf[ 1 ], so that we have room for the sign. */
  267. s = zend_gcvt(number, precision, decimal_point, exp_char, &num_buf[1]);
  268. is_negative = 0;
  269. if (*s == '-') {
  270. is_negative = 1;
  271. s = &num_buf[1];
  272. } else if (always_sign) {
  273. num_buf[0] = '+';
  274. s = num_buf;
  275. }
  276. s_len = strlen(s);
  277. break;
  278. }
  279. }
  280. php_sprintf_appendstring(buffer, pos, s, width, 0, padding,
  281. alignment, s_len, is_negative, 0, always_sign);
  282. }
  283. /* }}} */
  284. /* php_spintf_appendd2n() {{{ */
  285. inline static void
  286. php_sprintf_append2n(zend_string **buffer, size_t *pos, zend_long number,
  287. size_t width, char padding, size_t alignment, int n,
  288. const char *chartable, int expprec)
  289. {
  290. char numbuf[NUM_BUF_SIZE];
  291. zend_ulong num;
  292. zend_ulong i = NUM_BUF_SIZE - 1;
  293. int andbits = (1 << n) - 1;
  294. PRINTF_DEBUG(("sprintf: append2n(%x, %x, %x, %d, %d, '%c', %d, %d, %x)\n",
  295. *buffer, pos, &ZSTR_LEN(*buffer), number, width, padding, alignment, n,
  296. chartable));
  297. PRINTF_DEBUG(("sprintf: append2n 2^%d andbits=%x\n", n, andbits));
  298. num = (zend_ulong) number;
  299. numbuf[i] = '\0';
  300. do {
  301. numbuf[--i] = chartable[(num & andbits)];
  302. num >>= n;
  303. }
  304. while (num > 0);
  305. php_sprintf_appendstring(buffer, pos, &numbuf[i], width, 0,
  306. padding, alignment, (NUM_BUF_SIZE - 1) - i,
  307. /* neg */ false, expprec, 0);
  308. }
  309. /* }}} */
  310. /* php_spintf_getnumber() {{{ */
  311. inline static int
  312. php_sprintf_getnumber(char **buffer, size_t *len)
  313. {
  314. char *endptr;
  315. zend_long num = ZEND_STRTOL(*buffer, &endptr, 10);
  316. size_t i;
  317. if (endptr != NULL) {
  318. i = (endptr - *buffer);
  319. *len -= i;
  320. *buffer = endptr;
  321. }
  322. PRINTF_DEBUG(("sprintf_getnumber: number was %d bytes long\n", i));
  323. if (num >= INT_MAX || num < 0) {
  324. return -1;
  325. } else {
  326. return (int) num;
  327. }
  328. }
  329. /* }}} */
  330. #define ARG_NUM_NEXT -1
  331. #define ARG_NUM_INVALID -2
  332. int php_sprintf_get_argnum(char **format, size_t *format_len) {
  333. char *temppos = *format;
  334. while (isdigit((int) *temppos)) temppos++;
  335. if (*temppos != '$') {
  336. return ARG_NUM_NEXT;
  337. }
  338. int argnum = php_sprintf_getnumber(format, format_len);
  339. if (argnum <= 0) {
  340. zend_value_error("Argument number specifier must be greater than zero and less than %d", INT_MAX);
  341. return ARG_NUM_INVALID;
  342. }
  343. (*format)++; /* skip the '$' */
  344. (*format_len)--;
  345. return argnum - 1;
  346. }
  347. /* php_formatted_print() {{{
  348. * New sprintf implementation for PHP.
  349. *
  350. * Modifiers:
  351. *
  352. * " " pad integers with spaces
  353. * "-" left adjusted field
  354. * n field size
  355. * "."n precision (floats only)
  356. * "+" Always place a sign (+ or -) in front of a number
  357. *
  358. * Type specifiers:
  359. *
  360. * "%" literal "%", modifiers are ignored.
  361. * "b" integer argument is printed as binary
  362. * "c" integer argument is printed as a single character
  363. * "d" argument is an integer
  364. * "f" the argument is a float
  365. * "o" integer argument is printed as octal
  366. * "s" argument is a string
  367. * "x" integer argument is printed as lowercase hexadecimal
  368. * "X" integer argument is printed as uppercase hexadecimal
  369. *
  370. * nb_additional_parameters is used for throwing errors:
  371. * - -1: ValueError is thrown (for vsprintf where args originates from an array)
  372. * - 0 or more: ArgumentCountError is thrown
  373. */
  374. static zend_string *
  375. php_formatted_print(char *format, size_t format_len, zval *args, int argc, int nb_additional_parameters)
  376. {
  377. size_t size = 240, outpos = 0;
  378. int alignment, currarg, adjusting, argnum, width, precision;
  379. char *temppos, padding;
  380. zend_string *result;
  381. int always_sign;
  382. int max_missing_argnum = -1;
  383. result = zend_string_alloc(size, 0);
  384. currarg = 0;
  385. argnum = 0;
  386. while (format_len) {
  387. int expprec;
  388. zval *tmp;
  389. temppos = memchr(format, '%', format_len);
  390. if (!temppos) {
  391. php_sprintf_appendchars(&result, &outpos, format, format_len);
  392. break;
  393. } else if (temppos != format) {
  394. php_sprintf_appendchars(&result, &outpos, format, temppos - format);
  395. format_len -= temppos - format;
  396. format = temppos;
  397. }
  398. format++; /* skip the '%' */
  399. format_len--;
  400. if (*format == '%') {
  401. php_sprintf_appendchar(&result, &outpos, '%');
  402. format++;
  403. format_len--;
  404. } else {
  405. /* starting a new format specifier, reset variables */
  406. alignment = ALIGN_RIGHT;
  407. adjusting = 0;
  408. padding = ' ';
  409. always_sign = 0;
  410. expprec = 0;
  411. PRINTF_DEBUG(("sprintf: first looking at '%c', inpos=%d\n",
  412. *format, format - Z_STRVAL_P(z_format)));
  413. if (isalpha((int)*format)) {
  414. width = precision = 0;
  415. argnum = ARG_NUM_NEXT;
  416. } else {
  417. /* first look for argnum */
  418. argnum = php_sprintf_get_argnum(&format, &format_len);
  419. if (argnum == ARG_NUM_INVALID) {
  420. goto fail;
  421. }
  422. /* after argnum comes modifiers */
  423. PRINTF_DEBUG(("sprintf: looking for modifiers\n"
  424. "sprintf: now looking at '%c', inpos=%d\n",
  425. *format, format - Z_STRVAL_P(z_format)));
  426. for (;; format++, format_len--) {
  427. if (*format == ' ' || *format == '0') {
  428. padding = *format;
  429. } else if (*format == '-') {
  430. alignment = ALIGN_LEFT;
  431. /* space padding, the default */
  432. } else if (*format == '+') {
  433. always_sign = 1;
  434. } else if (*format == '\'') {
  435. if (format_len > 1) {
  436. format++;
  437. format_len--;
  438. padding = *format;
  439. } else {
  440. zend_value_error("Missing padding character");
  441. goto fail;
  442. }
  443. } else {
  444. PRINTF_DEBUG(("sprintf: end of modifiers\n"));
  445. break;
  446. }
  447. }
  448. PRINTF_DEBUG(("sprintf: padding='%c'\n", padding));
  449. PRINTF_DEBUG(("sprintf: alignment=%s\n",
  450. (alignment == ALIGN_LEFT) ? "left" : "right"));
  451. /* after modifiers comes width */
  452. if (*format == '*') {
  453. format++;
  454. format_len--;
  455. int width_argnum = php_sprintf_get_argnum(&format, &format_len);
  456. if (width_argnum == ARG_NUM_INVALID) {
  457. goto fail;
  458. }
  459. if (width_argnum == ARG_NUM_NEXT) {
  460. width_argnum = currarg++;
  461. }
  462. if (width_argnum >= argc) {
  463. max_missing_argnum = MAX(max_missing_argnum, width_argnum);
  464. continue;
  465. }
  466. tmp = &args[width_argnum];
  467. ZVAL_DEREF(tmp);
  468. if (Z_TYPE_P(tmp) != IS_LONG) {
  469. zend_value_error("Width must be an integer");
  470. goto fail;
  471. }
  472. if (Z_LVAL_P(tmp) < 0 || Z_LVAL_P(tmp) > INT_MAX) {
  473. zend_value_error("Width must be greater than zero and less than %d", INT_MAX);
  474. goto fail;
  475. }
  476. width = Z_LVAL_P(tmp);
  477. adjusting |= ADJ_WIDTH;
  478. } else if (isdigit((int)*format)) {
  479. PRINTF_DEBUG(("sprintf: getting width\n"));
  480. if ((width = php_sprintf_getnumber(&format, &format_len)) < 0) {
  481. zend_value_error("Width must be greater than zero and less than %d", INT_MAX);
  482. goto fail;
  483. }
  484. adjusting |= ADJ_WIDTH;
  485. } else {
  486. width = 0;
  487. }
  488. PRINTF_DEBUG(("sprintf: width=%d\n", width));
  489. /* after width and argnum comes precision */
  490. if (*format == '.') {
  491. format++;
  492. format_len--;
  493. PRINTF_DEBUG(("sprintf: getting precision\n"));
  494. if (*format == '*') {
  495. format++;
  496. format_len--;
  497. int prec_argnum = php_sprintf_get_argnum(&format, &format_len);
  498. if (prec_argnum == ARG_NUM_INVALID) {
  499. goto fail;
  500. }
  501. if (prec_argnum == ARG_NUM_NEXT) {
  502. prec_argnum = currarg++;
  503. }
  504. if (prec_argnum >= argc) {
  505. max_missing_argnum = MAX(max_missing_argnum, prec_argnum);
  506. continue;
  507. }
  508. tmp = &args[prec_argnum];
  509. ZVAL_DEREF(tmp);
  510. if (Z_TYPE_P(tmp) != IS_LONG) {
  511. zend_value_error("Precision must be an integer");
  512. goto fail;
  513. }
  514. if (Z_LVAL_P(tmp) < -1 || Z_LVAL_P(tmp) > INT_MAX) {
  515. zend_value_error("Precision must be between -1 and %d", INT_MAX);
  516. goto fail;
  517. }
  518. precision = Z_LVAL_P(tmp);
  519. adjusting |= ADJ_PRECISION;
  520. expprec = 1;
  521. } else if (isdigit((int)*format)) {
  522. if ((precision = php_sprintf_getnumber(&format, &format_len)) < 0) {
  523. zend_value_error("Precision must be greater than zero and less than %d", INT_MAX);
  524. goto fail;
  525. }
  526. adjusting |= ADJ_PRECISION;
  527. expprec = 1;
  528. } else {
  529. precision = 0;
  530. }
  531. } else {
  532. precision = 0;
  533. }
  534. PRINTF_DEBUG(("sprintf: precision=%d\n", precision));
  535. }
  536. if (*format == 'l') {
  537. format++;
  538. format_len--;
  539. }
  540. PRINTF_DEBUG(("sprintf: format character='%c'\n", *format));
  541. if (argnum == ARG_NUM_NEXT) {
  542. argnum = currarg++;
  543. }
  544. if (argnum >= argc) {
  545. max_missing_argnum = MAX(max_missing_argnum, argnum);
  546. continue;
  547. }
  548. if (expprec && precision == -1
  549. && *format != 'g' && *format != 'G' && *format != 'h' && *format != 'H') {
  550. zend_value_error("Precision -1 is only supported for %%g, %%G, %%h and %%H");
  551. goto fail;
  552. }
  553. /* now we expect to find a type specifier */
  554. tmp = &args[argnum];
  555. switch (*format) {
  556. case 's': {
  557. zend_string *t;
  558. zend_string *str = zval_get_tmp_string(tmp, &t);
  559. php_sprintf_appendstring(&result, &outpos,
  560. ZSTR_VAL(str),
  561. width, precision, padding,
  562. alignment,
  563. ZSTR_LEN(str),
  564. /* neg */ false, expprec, 0);
  565. zend_tmp_string_release(t);
  566. break;
  567. }
  568. case 'd':
  569. php_sprintf_appendint(&result, &outpos,
  570. zval_get_long(tmp),
  571. width, padding, alignment,
  572. always_sign);
  573. break;
  574. case 'u':
  575. php_sprintf_appenduint(&result, &outpos,
  576. zval_get_long(tmp),
  577. width, padding, alignment);
  578. break;
  579. case 'e':
  580. case 'E':
  581. case 'f':
  582. case 'F':
  583. case 'g':
  584. case 'G':
  585. case 'h':
  586. case 'H':
  587. php_sprintf_appenddouble(&result, &outpos,
  588. zval_get_double(tmp),
  589. width, padding, alignment,
  590. precision, adjusting,
  591. *format, always_sign
  592. );
  593. break;
  594. case 'c':
  595. php_sprintf_appendchar(&result, &outpos,
  596. (char) zval_get_long(tmp));
  597. break;
  598. case 'o':
  599. php_sprintf_append2n(&result, &outpos,
  600. zval_get_long(tmp),
  601. width, padding, alignment, 3,
  602. hexchars, expprec);
  603. break;
  604. case 'x':
  605. php_sprintf_append2n(&result, &outpos,
  606. zval_get_long(tmp),
  607. width, padding, alignment, 4,
  608. hexchars, expprec);
  609. break;
  610. case 'X':
  611. php_sprintf_append2n(&result, &outpos,
  612. zval_get_long(tmp),
  613. width, padding, alignment, 4,
  614. HEXCHARS, expprec);
  615. break;
  616. case 'b':
  617. php_sprintf_append2n(&result, &outpos,
  618. zval_get_long(tmp),
  619. width, padding, alignment, 1,
  620. hexchars, expprec);
  621. break;
  622. case '%':
  623. php_sprintf_appendchar(&result, &outpos, '%');
  624. break;
  625. case '\0':
  626. if (!format_len) {
  627. zend_value_error("Missing format specifier at end of string");
  628. goto fail;
  629. }
  630. ZEND_FALLTHROUGH;
  631. default:
  632. zend_value_error("Unknown format specifier \"%c\"", *format);
  633. goto fail;
  634. }
  635. format++;
  636. format_len--;
  637. }
  638. }
  639. if (max_missing_argnum >= 0) {
  640. if (nb_additional_parameters == -1) {
  641. zend_value_error("The arguments array must contain %d items, %d given", max_missing_argnum + 1, argc);
  642. } else {
  643. zend_argument_count_error("%d arguments are required, %d given", max_missing_argnum + nb_additional_parameters + 1, argc + nb_additional_parameters);
  644. }
  645. goto fail;
  646. }
  647. /* possibly, we have to make sure we have room for the terminating null? */
  648. ZSTR_VAL(result)[outpos]=0;
  649. ZSTR_LEN(result) = outpos;
  650. return result;
  651. fail:
  652. zend_string_efree(result);
  653. return NULL;
  654. }
  655. /* }}} */
  656. /* php_formatted_print_get_array() {{{ */
  657. static zval *php_formatted_print_get_array(zend_array *array, int *argc)
  658. {
  659. zval *args, *zv;
  660. int n;
  661. n = zend_hash_num_elements(array);
  662. args = (zval *)safe_emalloc(n, sizeof(zval), 0);
  663. n = 0;
  664. ZEND_HASH_FOREACH_VAL(array, zv) {
  665. ZVAL_COPY_VALUE(&args[n], zv);
  666. n++;
  667. } ZEND_HASH_FOREACH_END();
  668. *argc = n;
  669. return args;
  670. }
  671. /* }}} */
  672. /* {{{ Return a formatted string */
  673. PHP_FUNCTION(sprintf)
  674. {
  675. zend_string *result;
  676. char *format;
  677. size_t format_len;
  678. zval *args;
  679. int argc;
  680. ZEND_PARSE_PARAMETERS_START(1, -1)
  681. Z_PARAM_STRING(format, format_len)
  682. Z_PARAM_VARIADIC('*', args, argc)
  683. ZEND_PARSE_PARAMETERS_END();
  684. result = php_formatted_print(format, format_len, args, argc, 1);
  685. if (result == NULL) {
  686. RETURN_THROWS();
  687. }
  688. RETVAL_STR(result);
  689. }
  690. /* }}} */
  691. /* {{{ Return a formatted string */
  692. PHP_FUNCTION(vsprintf)
  693. {
  694. zend_string *result;
  695. char *format;
  696. size_t format_len;
  697. zval *args;
  698. zend_array *array;
  699. int argc;
  700. ZEND_PARSE_PARAMETERS_START(2, 2)
  701. Z_PARAM_STRING(format, format_len)
  702. Z_PARAM_ARRAY_HT(array)
  703. ZEND_PARSE_PARAMETERS_END();
  704. args = php_formatted_print_get_array(array, &argc);
  705. result = php_formatted_print(format, format_len, args, argc, -1);
  706. efree(args);
  707. if (result == NULL) {
  708. RETURN_THROWS();
  709. }
  710. RETVAL_STR(result);
  711. }
  712. /* }}} */
  713. /* {{{ Output a formatted string */
  714. PHP_FUNCTION(printf)
  715. {
  716. zend_string *result;
  717. size_t rlen;
  718. char *format;
  719. size_t format_len;
  720. zval *args;
  721. int argc;
  722. ZEND_PARSE_PARAMETERS_START(1, -1)
  723. Z_PARAM_STRING(format, format_len)
  724. Z_PARAM_VARIADIC('*', args, argc)
  725. ZEND_PARSE_PARAMETERS_END();
  726. result = php_formatted_print(format, format_len, args, argc, 1);
  727. if (result == NULL) {
  728. RETURN_THROWS();
  729. }
  730. rlen = PHPWRITE(ZSTR_VAL(result), ZSTR_LEN(result));
  731. zend_string_efree(result);
  732. RETURN_LONG(rlen);
  733. }
  734. /* }}} */
  735. /* {{{ Output a formatted string */
  736. PHP_FUNCTION(vprintf)
  737. {
  738. zend_string *result;
  739. size_t rlen;
  740. char *format;
  741. size_t format_len;
  742. zval *args;
  743. zend_array *array;
  744. int argc;
  745. ZEND_PARSE_PARAMETERS_START(2, 2)
  746. Z_PARAM_STRING(format, format_len)
  747. Z_PARAM_ARRAY_HT(array)
  748. ZEND_PARSE_PARAMETERS_END();
  749. args = php_formatted_print_get_array(array, &argc);
  750. result = php_formatted_print(format, format_len, args, argc, -1);
  751. efree(args);
  752. if (result == NULL) {
  753. RETURN_THROWS();
  754. }
  755. rlen = PHPWRITE(ZSTR_VAL(result), ZSTR_LEN(result));
  756. zend_string_efree(result);
  757. RETURN_LONG(rlen);
  758. }
  759. /* }}} */
  760. /* {{{ Output a formatted string into a stream */
  761. PHP_FUNCTION(fprintf)
  762. {
  763. php_stream *stream;
  764. char *format;
  765. size_t format_len;
  766. zval *arg1, *args;
  767. int argc;
  768. zend_string *result;
  769. ZEND_PARSE_PARAMETERS_START(2, -1)
  770. Z_PARAM_RESOURCE(arg1)
  771. Z_PARAM_STRING(format, format_len)
  772. Z_PARAM_VARIADIC('*', args, argc)
  773. ZEND_PARSE_PARAMETERS_END();
  774. php_stream_from_zval(stream, arg1);
  775. result = php_formatted_print(format, format_len, args, argc, 2);
  776. if (result == NULL) {
  777. RETURN_THROWS();
  778. }
  779. php_stream_write(stream, ZSTR_VAL(result), ZSTR_LEN(result));
  780. RETVAL_LONG(ZSTR_LEN(result));
  781. zend_string_efree(result);
  782. }
  783. /* }}} */
  784. /* {{{ Output a formatted string into a stream */
  785. PHP_FUNCTION(vfprintf)
  786. {
  787. php_stream *stream;
  788. char *format;
  789. size_t format_len;
  790. zval *arg1, *args;
  791. zend_array *array;
  792. int argc;
  793. zend_string *result;
  794. ZEND_PARSE_PARAMETERS_START(3, 3)
  795. Z_PARAM_RESOURCE(arg1)
  796. Z_PARAM_STRING(format, format_len)
  797. Z_PARAM_ARRAY_HT(array)
  798. ZEND_PARSE_PARAMETERS_END();
  799. php_stream_from_zval(stream, arg1);
  800. args = php_formatted_print_get_array(array, &argc);
  801. result = php_formatted_print(format, format_len, args, argc, -1);
  802. efree(args);
  803. if (result == NULL) {
  804. RETURN_THROWS();
  805. }
  806. php_stream_write(stream, ZSTR_VAL(result), ZSTR_LEN(result));
  807. RETVAL_LONG(ZSTR_LEN(result));
  808. zend_string_efree(result);
  809. }
  810. /* }}} */