phpdbg_out.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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. | Authors: Felipe Pena <felipe@php.net> |
  16. | Authors: Joe Watkins <joe.watkins@live.co.uk> |
  17. | Authors: Bob Weinand <bwoebi@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include "zend.h"
  21. #include "php.h"
  22. #include "spprintf.h"
  23. #include "phpdbg.h"
  24. #include "phpdbg_io.h"
  25. #include "phpdbg_eol.h"
  26. #include "ext/standard/html.h"
  27. #ifdef _WIN32
  28. # include "win32/time.h"
  29. #endif
  30. ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
  31. /* copied from php-src/main/snprintf.c and slightly modified */
  32. /*
  33. * NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions
  34. *
  35. * XXX: this is a magic number; do not decrease it
  36. * Emax = 1023
  37. * NDIG = 320
  38. * NUM_BUF_SIZE >= strlen("-") + Emax + strlrn(".") + NDIG + strlen("E+1023") + 1;
  39. */
  40. #define NUM_BUF_SIZE 2048
  41. /*
  42. * Descriptor for buffer area
  43. */
  44. struct buf_area {
  45. char *buf_end;
  46. char *nextb; /* pointer to next byte to read/write */
  47. };
  48. typedef struct buf_area buffy;
  49. /*
  50. * The INS_CHAR macro inserts a character in the buffer and writes
  51. * the buffer back to disk if necessary
  52. * It uses the char pointers sp and bep:
  53. * sp points to the next available character in the buffer
  54. * bep points to the end-of-buffer+1
  55. * While using this macro, note that the nextb pointer is NOT updated.
  56. *
  57. * NOTE: Evaluation of the c argument should not have any side-effects
  58. */
  59. #define INS_CHAR(c, sp, bep, cc) \
  60. { \
  61. if (sp < bep) \
  62. { \
  63. *sp++ = c; \
  64. } \
  65. cc++; \
  66. }
  67. #define NUM( c ) ( c - '0' )
  68. #define STR_TO_DEC( str, num ) \
  69. num = NUM( *str++ ) ; \
  70. while ( isdigit((int)*str ) ) \
  71. { \
  72. num *= 10 ; \
  73. num += NUM( *str++ ) ; \
  74. }
  75. /*
  76. * This macro does zero padding so that the precision
  77. * requirement is satisfied. The padding is done by
  78. * adding '0's to the left of the string that is going
  79. * to be printed.
  80. */
  81. #define FIX_PRECISION( adjust, precision, s, s_len ) \
  82. if ( adjust ) \
  83. while ( s_len < precision ) \
  84. { \
  85. *--s = '0' ; \
  86. s_len++ ; \
  87. }
  88. /*
  89. * Macro that does padding. The padding is done by printing
  90. * the character ch.
  91. */
  92. #define PAD( width, len, ch ) do \
  93. { \
  94. INS_CHAR( ch, sp, bep, cc ) ; \
  95. width-- ; \
  96. } \
  97. while ( width > len )
  98. /*
  99. * Prefix the character ch to the string str
  100. * Increase length
  101. * Set the has_prefix flag
  102. */
  103. #define PREFIX( str, length, ch ) *--str = ch ; length++ ; has_prefix = YES
  104. #ifdef HAVE_LOCALE_H
  105. #include <locale.h>
  106. #define LCONV_DECIMAL_POINT (*lconv->decimal_point)
  107. #else
  108. #define LCONV_DECIMAL_POINT '.'
  109. #endif
  110. #define NUL '\0'
  111. #define S_NULL "(null)"
  112. #define S_NULL_LEN 6
  113. #define FLOAT_DIGITS 6
  114. /*
  115. * Do format conversion placing the output in buffer
  116. */
  117. static int format_converter(register buffy *odp, const char *fmt, zend_bool escape_xml, va_list ap) {
  118. char *sp;
  119. char *bep;
  120. int cc = 0;
  121. int i;
  122. char *s = NULL, *free_s = NULL;
  123. size_t s_len;
  124. zend_bool free_zcopy;
  125. zval *zvp, zcopy;
  126. int min_width = 0;
  127. int precision = 0;
  128. enum {
  129. LEFT, RIGHT
  130. } adjust;
  131. char pad_char;
  132. char prefix_char;
  133. double fp_num;
  134. wide_int i_num = (wide_int) 0;
  135. u_wide_int ui_num;
  136. char num_buf[NUM_BUF_SIZE];
  137. char char_buf[2]; /* for printing %% and %<unknown> */
  138. #ifdef HAVE_LOCALE_H
  139. struct lconv *lconv = NULL;
  140. #endif
  141. /*
  142. * Flag variables
  143. */
  144. length_modifier_e modifier;
  145. boolean_e alternate_form;
  146. boolean_e print_sign;
  147. boolean_e print_blank;
  148. boolean_e adjust_precision;
  149. boolean_e adjust_width;
  150. bool_int is_negative;
  151. sp = odp->nextb;
  152. bep = odp->buf_end;
  153. while (*fmt) {
  154. if (*fmt != '%') {
  155. INS_CHAR(*fmt, sp, bep, cc);
  156. } else {
  157. /*
  158. * Default variable settings
  159. */
  160. adjust = RIGHT;
  161. alternate_form = print_sign = print_blank = NO;
  162. pad_char = ' ';
  163. prefix_char = NUL;
  164. free_zcopy = 0;
  165. fmt++;
  166. /*
  167. * Try to avoid checking for flags, width or precision
  168. */
  169. if (isascii((int)*fmt) && !islower((int)*fmt)) {
  170. /*
  171. * Recognize flags: -, #, BLANK, +
  172. */
  173. for (;; fmt++) {
  174. if (*fmt == '-')
  175. adjust = LEFT;
  176. else if (*fmt == '+')
  177. print_sign = YES;
  178. else if (*fmt == '#')
  179. alternate_form = YES;
  180. else if (*fmt == ' ')
  181. print_blank = YES;
  182. else if (*fmt == '0')
  183. pad_char = '0';
  184. else
  185. break;
  186. }
  187. /*
  188. * Check if a width was specified
  189. */
  190. if (isdigit((int)*fmt)) {
  191. STR_TO_DEC(fmt, min_width);
  192. adjust_width = YES;
  193. } else if (*fmt == '*') {
  194. min_width = va_arg(ap, int);
  195. fmt++;
  196. adjust_width = YES;
  197. if (min_width < 0) {
  198. adjust = LEFT;
  199. min_width = -min_width;
  200. }
  201. } else
  202. adjust_width = NO;
  203. /*
  204. * Check if a precision was specified
  205. */
  206. if (*fmt == '.') {
  207. adjust_precision = YES;
  208. fmt++;
  209. if (isdigit((int)*fmt)) {
  210. STR_TO_DEC(fmt, precision);
  211. } else if (*fmt == '*') {
  212. precision = va_arg(ap, int);
  213. fmt++;
  214. if (precision < 0)
  215. precision = 0;
  216. } else
  217. precision = 0;
  218. if (precision > FORMAT_CONV_MAX_PRECISION && *fmt != 's' && *fmt != 'v' && *fmt != 'b') {
  219. precision = FORMAT_CONV_MAX_PRECISION;
  220. }
  221. } else
  222. adjust_precision = NO;
  223. } else
  224. adjust_precision = adjust_width = NO;
  225. /*
  226. * Modifier check
  227. */
  228. switch (*fmt) {
  229. case 'L':
  230. fmt++;
  231. modifier = LM_LONG_DOUBLE;
  232. break;
  233. case 'I':
  234. fmt++;
  235. #if SIZEOF_LONG_LONG
  236. if (*fmt == '6' && *(fmt+1) == '4') {
  237. fmt += 2;
  238. modifier = LM_LONG_LONG;
  239. } else
  240. #endif
  241. if (*fmt == '3' && *(fmt+1) == '2') {
  242. fmt += 2;
  243. modifier = LM_LONG;
  244. } else {
  245. #ifdef _WIN64
  246. modifier = LM_LONG_LONG;
  247. #else
  248. modifier = LM_LONG;
  249. #endif
  250. }
  251. break;
  252. case 'l':
  253. fmt++;
  254. #if SIZEOF_LONG_LONG
  255. if (*fmt == 'l') {
  256. fmt++;
  257. modifier = LM_LONG_LONG;
  258. } else
  259. #endif
  260. modifier = LM_LONG;
  261. break;
  262. case 'z':
  263. fmt++;
  264. modifier = LM_SIZE_T;
  265. break;
  266. case 'j':
  267. fmt++;
  268. #if SIZEOF_INTMAX_T
  269. modifier = LM_INTMAX_T;
  270. #else
  271. modifier = LM_SIZE_T;
  272. #endif
  273. break;
  274. case 't':
  275. fmt++;
  276. #if SIZEOF_PTRDIFF_T
  277. modifier = LM_PTRDIFF_T;
  278. #else
  279. modifier = LM_SIZE_T;
  280. #endif
  281. break;
  282. case 'h':
  283. fmt++;
  284. if (*fmt == 'h') {
  285. fmt++;
  286. }
  287. /* these are promoted to int, so no break */
  288. default:
  289. modifier = LM_STD;
  290. break;
  291. }
  292. /*
  293. * Argument extraction and printing.
  294. * First we determine the argument type.
  295. * Then, we convert the argument to a string.
  296. * On exit from the switch, s points to the string that
  297. * must be printed, s_len has the length of the string
  298. * The precision requirements, if any, are reflected in s_len.
  299. *
  300. * NOTE: pad_char may be set to '0' because of the 0 flag.
  301. * It is reset to ' ' by non-numeric formats
  302. */
  303. switch (*fmt) {
  304. case 'Z':
  305. zvp = (zval *) va_arg(ap, zval *);
  306. free_zcopy = zend_make_printable_zval(zvp, &zcopy);
  307. if (free_zcopy) {
  308. zvp = &zcopy;
  309. }
  310. s_len = Z_STRLEN_P(zvp);
  311. s = Z_STRVAL_P(zvp);
  312. if (adjust_precision && precision < s_len) {
  313. s_len = precision;
  314. }
  315. break;
  316. case 'u':
  317. switch(modifier) {
  318. default:
  319. i_num = (wide_int) va_arg(ap, unsigned int);
  320. break;
  321. case LM_LONG_DOUBLE:
  322. goto fmt_error;
  323. case LM_LONG:
  324. i_num = (wide_int) va_arg(ap, unsigned long int);
  325. break;
  326. case LM_SIZE_T:
  327. i_num = (wide_int) va_arg(ap, size_t);
  328. break;
  329. #if SIZEOF_LONG_LONG
  330. case LM_LONG_LONG:
  331. i_num = (wide_int) va_arg(ap, u_wide_int);
  332. break;
  333. #endif
  334. #if SIZEOF_INTMAX_T
  335. case LM_INTMAX_T:
  336. i_num = (wide_int) va_arg(ap, uintmax_t);
  337. break;
  338. #endif
  339. #if SIZEOF_PTRDIFF_T
  340. case LM_PTRDIFF_T:
  341. i_num = (wide_int) va_arg(ap, ptrdiff_t);
  342. break;
  343. #endif
  344. }
  345. /*
  346. * The rest also applies to other integer formats, so fall
  347. * into that case.
  348. */
  349. case 'd':
  350. case 'i':
  351. /*
  352. * Get the arg if we haven't already.
  353. */
  354. if ((*fmt) != 'u') {
  355. switch(modifier) {
  356. default:
  357. i_num = (wide_int) va_arg(ap, int);
  358. break;
  359. case LM_LONG_DOUBLE:
  360. goto fmt_error;
  361. case LM_LONG:
  362. i_num = (wide_int) va_arg(ap, long int);
  363. break;
  364. case LM_SIZE_T:
  365. #if SIZEOF_SSIZE_T
  366. i_num = (wide_int) va_arg(ap, ssize_t);
  367. #else
  368. i_num = (wide_int) va_arg(ap, size_t);
  369. #endif
  370. break;
  371. #if SIZEOF_LONG_LONG
  372. case LM_LONG_LONG:
  373. i_num = (wide_int) va_arg(ap, wide_int);
  374. break;
  375. #endif
  376. #if SIZEOF_INTMAX_T
  377. case LM_INTMAX_T:
  378. i_num = (wide_int) va_arg(ap, intmax_t);
  379. break;
  380. #endif
  381. #if SIZEOF_PTRDIFF_T
  382. case LM_PTRDIFF_T:
  383. i_num = (wide_int) va_arg(ap, ptrdiff_t);
  384. break;
  385. #endif
  386. }
  387. }
  388. s = ap_php_conv_10(i_num, (*fmt) == 'u', &is_negative,
  389. &num_buf[NUM_BUF_SIZE], &s_len);
  390. FIX_PRECISION(adjust_precision, precision, s, s_len);
  391. if (*fmt != 'u') {
  392. if (is_negative) {
  393. prefix_char = '-';
  394. } else if (print_sign) {
  395. prefix_char = '+';
  396. } else if (print_blank) {
  397. prefix_char = ' ';
  398. }
  399. }
  400. break;
  401. case 'o':
  402. switch(modifier) {
  403. default:
  404. ui_num = (u_wide_int) va_arg(ap, unsigned int);
  405. break;
  406. case LM_LONG_DOUBLE:
  407. goto fmt_error;
  408. case LM_LONG:
  409. ui_num = (u_wide_int) va_arg(ap, unsigned long int);
  410. break;
  411. case LM_SIZE_T:
  412. ui_num = (u_wide_int) va_arg(ap, size_t);
  413. break;
  414. #if SIZEOF_LONG_LONG
  415. case LM_LONG_LONG:
  416. ui_num = (u_wide_int) va_arg(ap, u_wide_int);
  417. break;
  418. #endif
  419. #if SIZEOF_INTMAX_T
  420. case LM_INTMAX_T:
  421. ui_num = (u_wide_int) va_arg(ap, uintmax_t);
  422. break;
  423. #endif
  424. #if SIZEOF_PTRDIFF_T
  425. case LM_PTRDIFF_T:
  426. ui_num = (u_wide_int) va_arg(ap, ptrdiff_t);
  427. break;
  428. #endif
  429. }
  430. s = ap_php_conv_p2(ui_num, 3, *fmt, &num_buf[NUM_BUF_SIZE], &s_len);
  431. FIX_PRECISION(adjust_precision, precision, s, s_len);
  432. if (alternate_form && *s != '0') {
  433. *--s = '0';
  434. s_len++;
  435. }
  436. break;
  437. case 'x':
  438. case 'X':
  439. switch(modifier) {
  440. default:
  441. ui_num = (u_wide_int) va_arg(ap, unsigned int);
  442. break;
  443. case LM_LONG_DOUBLE:
  444. goto fmt_error;
  445. case LM_LONG:
  446. ui_num = (u_wide_int) va_arg(ap, unsigned long int);
  447. break;
  448. case LM_SIZE_T:
  449. ui_num = (u_wide_int) va_arg(ap, size_t);
  450. break;
  451. #if SIZEOF_LONG_LONG
  452. case LM_LONG_LONG:
  453. ui_num = (u_wide_int) va_arg(ap, u_wide_int);
  454. break;
  455. #endif
  456. #if SIZEOF_INTMAX_T
  457. case LM_INTMAX_T:
  458. ui_num = (u_wide_int) va_arg(ap, uintmax_t);
  459. break;
  460. #endif
  461. #if SIZEOF_PTRDIFF_T
  462. case LM_PTRDIFF_T:
  463. ui_num = (u_wide_int) va_arg(ap, ptrdiff_t);
  464. break;
  465. #endif
  466. }
  467. s = ap_php_conv_p2(ui_num, 4, *fmt, &num_buf[NUM_BUF_SIZE], &s_len);
  468. FIX_PRECISION(adjust_precision, precision, s, s_len);
  469. if (alternate_form && i_num != 0) {
  470. *--s = *fmt; /* 'x' or 'X' */
  471. *--s = '0';
  472. s_len += 2;
  473. }
  474. break;
  475. case 's':
  476. case 'v':
  477. s = va_arg(ap, char *);
  478. if (s != NULL) {
  479. if (adjust_precision) {
  480. s_len = precision;
  481. } else {
  482. s_len = strlen(s);
  483. }
  484. if (escape_xml) {
  485. /* added: support for xml escaping */
  486. int old_slen = s_len, i = 0;
  487. char *old_s = s, *s_ptr;
  488. free_s = s_ptr = s = emalloc(old_slen * 6 + 1);
  489. do {
  490. if (old_s[i] == '&' || old_s[i] == '"' || old_s[i] == '<') {
  491. *s_ptr++ = '&';
  492. switch (old_s[i]) {
  493. case '"':
  494. s_len += 5;
  495. *s_ptr++ = 'q';
  496. *s_ptr++ = 'u';
  497. *s_ptr++ = 'o';
  498. *s_ptr++ = 't';
  499. break;
  500. case '<':
  501. s_len += 3;
  502. *s_ptr++ = 'l';
  503. *s_ptr++ = 't';
  504. break;
  505. case '&':
  506. s_len += 4;
  507. *s_ptr++ = 'a';
  508. *s_ptr++ = 'm';
  509. *s_ptr++ = 'p';
  510. break;
  511. }
  512. *s_ptr++ = ';';
  513. } else {
  514. *s_ptr++ = old_s[i];
  515. }
  516. } while (i++ < old_slen);
  517. }
  518. } else {
  519. s = S_NULL;
  520. s_len = S_NULL_LEN;
  521. }
  522. pad_char = ' ';
  523. break;
  524. case 'b':
  525. if (escape_xml) {
  526. s = PHPDBG_G(err_buf).xml;
  527. } else {
  528. s = PHPDBG_G(err_buf).msg;
  529. }
  530. if (s != NULL) {
  531. if (escape_xml) {
  532. s_len = PHPDBG_G(err_buf).xmllen;
  533. } else {
  534. s_len = PHPDBG_G(err_buf).msglen;
  535. }
  536. if (adjust_precision && precision != s_len) {
  537. s_len = precision;
  538. }
  539. } else {
  540. s = "";
  541. s_len = 0;
  542. }
  543. pad_char = ' ';
  544. break;
  545. case 'r':
  546. if (PHPDBG_G(req_id)) {
  547. s_len = spprintf(&s, 0, "req=\"" ZEND_ULONG_FMT "\"", PHPDBG_G(req_id));
  548. free_s = s;
  549. } else {
  550. s = "";
  551. s_len = 0;
  552. }
  553. break;
  554. case 'f':
  555. case 'F':
  556. case 'e':
  557. case 'E':
  558. switch(modifier) {
  559. case LM_LONG_DOUBLE:
  560. fp_num = (double) va_arg(ap, long double);
  561. break;
  562. case LM_STD:
  563. fp_num = va_arg(ap, double);
  564. break;
  565. default:
  566. goto fmt_error;
  567. }
  568. if (zend_isnan(fp_num)) {
  569. s = "NAN";
  570. s_len = 3;
  571. } else if (zend_isinf(fp_num)) {
  572. s = "INF";
  573. s_len = 3;
  574. } else {
  575. #ifdef HAVE_LOCALE_H
  576. if (!lconv) {
  577. lconv = localeconv();
  578. }
  579. #endif
  580. s = php_conv_fp((*fmt == 'f')?'F':*fmt, fp_num, alternate_form,
  581. (adjust_precision == NO) ? FLOAT_DIGITS : precision,
  582. (*fmt == 'f')?LCONV_DECIMAL_POINT:'.',
  583. &is_negative, &num_buf[1], &s_len);
  584. if (is_negative)
  585. prefix_char = '-';
  586. else if (print_sign)
  587. prefix_char = '+';
  588. else if (print_blank)
  589. prefix_char = ' ';
  590. }
  591. break;
  592. case 'g':
  593. case 'k':
  594. case 'G':
  595. case 'H':
  596. switch(modifier) {
  597. case LM_LONG_DOUBLE:
  598. fp_num = (double) va_arg(ap, long double);
  599. break;
  600. case LM_STD:
  601. fp_num = va_arg(ap, double);
  602. break;
  603. default:
  604. goto fmt_error;
  605. }
  606. if (zend_isnan(fp_num)) {
  607. s = "NAN";
  608. s_len = 3;
  609. break;
  610. } else if (zend_isinf(fp_num)) {
  611. if (fp_num > 0) {
  612. s = "INF";
  613. s_len = 3;
  614. } else {
  615. s = "-INF";
  616. s_len = 4;
  617. }
  618. break;
  619. }
  620. if (adjust_precision == NO) {
  621. precision = FLOAT_DIGITS;
  622. } else if (precision == 0) {
  623. precision = 1;
  624. }
  625. /*
  626. * * We use &num_buf[ 1 ], so that we have room for the sign
  627. */
  628. #ifdef HAVE_LOCALE_H
  629. if (!lconv) {
  630. lconv = localeconv();
  631. }
  632. #endif
  633. s = php_gcvt(fp_num, precision, (*fmt=='H' || *fmt == 'k') ? '.' : LCONV_DECIMAL_POINT, (*fmt == 'G' || *fmt == 'H')?'E':'e', &num_buf[1]);
  634. if (*s == '-') {
  635. prefix_char = *s++;
  636. } else if (print_sign) {
  637. prefix_char = '+';
  638. } else if (print_blank) {
  639. prefix_char = ' ';
  640. }
  641. s_len = strlen(s);
  642. if (alternate_form && (strchr(s, '.')) == NULL) {
  643. s[s_len++] = '.';
  644. }
  645. break;
  646. case 'c':
  647. char_buf[0] = (char) (va_arg(ap, int));
  648. s = &char_buf[0];
  649. s_len = 1;
  650. pad_char = ' ';
  651. break;
  652. case '%':
  653. char_buf[0] = '%';
  654. s = &char_buf[0];
  655. s_len = 1;
  656. pad_char = ' ';
  657. break;
  658. case 'n':
  659. *(va_arg(ap, int *)) = cc;
  660. goto skip_output;
  661. /*
  662. * Always extract the argument as a "char *" pointer. We
  663. * should be using "void *" but there are still machines
  664. * that don't understand it.
  665. * If the pointer size is equal to the size of an unsigned
  666. * integer we convert the pointer to a hex number, otherwise
  667. * we print "%p" to indicate that we don't handle "%p".
  668. */
  669. case 'p':
  670. if (sizeof(char *) <= sizeof(u_wide_int)) {
  671. ui_num = (u_wide_int)((size_t) va_arg(ap, char *));
  672. s = ap_php_conv_p2(ui_num, 4, 'x',
  673. &num_buf[NUM_BUF_SIZE], &s_len);
  674. if (ui_num != 0) {
  675. *--s = 'x';
  676. *--s = '0';
  677. s_len += 2;
  678. }
  679. } else {
  680. s = "%p";
  681. s_len = 2;
  682. }
  683. pad_char = ' ';
  684. break;
  685. case NUL:
  686. /*
  687. * The last character of the format string was %.
  688. * We ignore it.
  689. */
  690. continue;
  691. fmt_error:
  692. php_error(E_ERROR, "Illegal length modifier specified '%c' in s[np]printf call", *fmt);
  693. /*
  694. * The default case is for unrecognized %'s.
  695. * We print %<char> to help the user identify what
  696. * option is not understood.
  697. * This is also useful in case the user wants to pass
  698. * the output of format_converter to another function
  699. * that understands some other %<char> (like syslog).
  700. * Note that we can't point s inside fmt because the
  701. * unknown <char> could be preceded by width etc.
  702. */
  703. default:
  704. char_buf[0] = '%';
  705. char_buf[1] = *fmt;
  706. s = char_buf;
  707. s_len = 2;
  708. pad_char = ' ';
  709. break;
  710. }
  711. if (prefix_char != NUL) {
  712. *--s = prefix_char;
  713. s_len++;
  714. }
  715. if (adjust_width && adjust == RIGHT && min_width > s_len) {
  716. if (pad_char == '0' && prefix_char != NUL) {
  717. INS_CHAR(*s, sp, bep, cc)
  718. s++;
  719. s_len--;
  720. min_width--;
  721. }
  722. PAD(min_width, s_len, pad_char);
  723. }
  724. /*
  725. * Print the string s.
  726. */
  727. for (i = s_len; i != 0; i--) {
  728. INS_CHAR(*s, sp, bep, cc);
  729. s++;
  730. }
  731. if (adjust_width && adjust == LEFT && min_width > s_len)
  732. PAD(min_width, s_len, pad_char);
  733. if (free_zcopy) {
  734. zval_ptr_dtor_str(&zcopy);
  735. }
  736. }
  737. skip_output:
  738. if (free_s) {
  739. efree(free_s);
  740. free_s = NULL;
  741. }
  742. fmt++;
  743. }
  744. odp->nextb = sp;
  745. return (cc);
  746. }
  747. static void strx_printv(int *ccp, char *buf, size_t len, const char *format, zend_bool escape_xml, va_list ap) {
  748. buffy od;
  749. int cc;
  750. /*
  751. * First initialize the descriptor
  752. * Notice that if no length is given, we initialize buf_end to the
  753. * highest possible address.
  754. */
  755. if (len == 0) {
  756. od.buf_end = (char *) ~0;
  757. od.nextb = (char *) ~0;
  758. } else {
  759. od.buf_end = &buf[len-1];
  760. od.nextb = buf;
  761. }
  762. /*
  763. * Do the conversion
  764. */
  765. cc = format_converter(&od, format, escape_xml, ap);
  766. if (len != 0 && od.nextb <= od.buf_end) {
  767. *(od.nextb) = '\0';
  768. }
  769. if (ccp) {
  770. *ccp = cc;
  771. }
  772. }
  773. static int phpdbg_xml_vsnprintf(char *buf, size_t len, const char *format, zend_bool escape_xml, va_list ap) {
  774. int cc;
  775. strx_printv(&cc, buf, len, format, escape_xml, ap);
  776. return (cc);
  777. }
  778. PHPDBG_API int phpdbg_xml_vasprintf(char **buf, const char *format, zend_bool escape_xml, va_list ap) {
  779. va_list ap2;
  780. int cc;
  781. va_copy(ap2, ap);
  782. cc = phpdbg_xml_vsnprintf(NULL, 0, format, escape_xml, ap2);
  783. va_end(ap2);
  784. *buf = NULL;
  785. if (cc >= 0) {
  786. *buf = emalloc(++cc);
  787. if ((cc = phpdbg_xml_vsnprintf(*buf, cc, format, escape_xml, ap)) < 0) {
  788. efree(*buf);
  789. *buf = NULL;
  790. }
  791. }
  792. return cc;
  793. }
  794. /* copy end */
  795. PHPDBG_API int _phpdbg_xml_asprintf(char **buf, const char *format, zend_bool escape_xml, ...) {
  796. int ret;
  797. va_list va;
  798. va_start(va, escape_xml);
  799. ret = phpdbg_xml_vasprintf(buf, format, escape_xml, va);
  800. va_end(va);
  801. return ret;
  802. }
  803. PHPDBG_API int _phpdbg_asprintf(char **buf, const char *format, ...) {
  804. int ret;
  805. va_list va;
  806. va_start(va, format);
  807. ret = phpdbg_xml_vasprintf(buf, format, 0, va);
  808. va_end(va);
  809. return ret;
  810. }
  811. static int phpdbg_encode_xml(char **buf, char *msg, int msglen, int from, char *to) {
  812. int i;
  813. int tolen = to ? strlen(to) : 5;
  814. char *tmp = *buf = emalloc(msglen * tolen);
  815. for (i = 0; i++ < msglen; msg++) {
  816. if (*msg == '&') {
  817. memcpy(tmp, ZEND_STRL("&amp;"));
  818. tmp += sizeof("&amp;") - 1;
  819. } else if (*msg == '<') {
  820. memcpy(tmp, ZEND_STRL("&lt;"));
  821. tmp += sizeof("&lt;") - 1;
  822. } else if (((int) *msg) == from) {
  823. memcpy(tmp, to, tolen);
  824. tmp += tolen;
  825. } else {
  826. *tmp++ = *msg;
  827. }
  828. }
  829. {
  830. int len = tmp - *buf;
  831. *buf = erealloc(*buf, len + 1);
  832. return len;
  833. }
  834. }
  835. static void phpdbg_encode_ctrl_chars(char **buf, int *buflen) {
  836. char *tmp, *tmpptr;
  837. int len;
  838. int i;
  839. tmp = tmpptr = emalloc(*buflen * 5);
  840. for (i = 0; i < *buflen; i++) {
  841. if ((*buf)[i] < 0x20) {
  842. *tmpptr++ = '&';
  843. *tmpptr++ = '#';
  844. if ((unsigned int) ((*buf)[i]) > 9) {
  845. *tmpptr++ = ((*buf)[i] / 10) + '0';
  846. }
  847. *tmpptr++ = ((*buf)[i] % 10) + '0';
  848. *tmpptr++ = ';';
  849. } else {
  850. *tmpptr++ = (*buf)[i];
  851. }
  852. }
  853. len = tmpptr - tmp;
  854. efree(*buf);
  855. *buf = erealloc(tmp, len + 1);
  856. *buflen = len;
  857. }
  858. static int phpdbg_process_print(int fd, int type, const char *tag, const char *msg, int msglen, const char *xml, int xmllen) {
  859. char *msgout = NULL, *buf;
  860. int msgoutlen, xmloutlen, buflen;
  861. const char *severity;
  862. if ((PHPDBG_G(flags) & PHPDBG_WRITE_XML) && PHPDBG_G(in_script_xml) && PHPDBG_G(in_script_xml) != type) {
  863. phpdbg_mixed_write(fd, ZEND_STRL("</stream>"));
  864. PHPDBG_G(in_script_xml) = 0;
  865. }
  866. switch (type) {
  867. case P_ERROR:
  868. severity = "error";
  869. if (!PHPDBG_G(last_was_newline)) {
  870. if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) {
  871. phpdbg_mixed_write(fd, ZEND_STRL("<phpdbg>\n" "</phpdbg>"));
  872. } else {
  873. phpdbg_mixed_write(fd, ZEND_STRL("\n"));
  874. }
  875. PHPDBG_G(last_was_newline) = 1;
  876. }
  877. if (PHPDBG_G(flags) & PHPDBG_IS_COLOURED) {
  878. msgoutlen = phpdbg_asprintf(&msgout, "\033[%sm[%.*s]\033[0m\n", PHPDBG_G(colors)[PHPDBG_COLOR_ERROR]->code, msglen, msg);
  879. } else {
  880. msgoutlen = phpdbg_asprintf(&msgout, "[%.*s]\n", msglen, msg);
  881. }
  882. break;
  883. case P_NOTICE:
  884. severity = "notice";
  885. if (!PHPDBG_G(last_was_newline)) {
  886. if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) {
  887. phpdbg_mixed_write(fd, ZEND_STRL("<phpdbg>\n" "</phpdbg>"));
  888. } else {
  889. phpdbg_mixed_write(fd, ZEND_STRL("\n"));
  890. }
  891. PHPDBG_G(last_was_newline) = 1;
  892. }
  893. if (PHPDBG_G(flags) & PHPDBG_IS_COLOURED) {
  894. msgoutlen = phpdbg_asprintf(&msgout, "\033[%sm[%.*s]\033[0m\n", PHPDBG_G(colors)[PHPDBG_COLOR_NOTICE]->code, msglen, msg);
  895. } else {
  896. msgoutlen = phpdbg_asprintf(&msgout, "[%.*s]\n", msglen, msg);
  897. }
  898. break;
  899. case P_WRITELN:
  900. severity = "normal";
  901. if (msg) {
  902. msgoutlen = phpdbg_asprintf(&msgout, "%.*s\n", msglen, msg);
  903. } else {
  904. msgoutlen = 1;
  905. msgout = estrdup("\n");
  906. }
  907. PHPDBG_G(last_was_newline) = 1;
  908. break;
  909. case P_WRITE:
  910. severity = "normal";
  911. if (msg) {
  912. msgout = estrndup(msg, msglen);
  913. msgoutlen = msglen;
  914. PHPDBG_G(last_was_newline) = msg[msglen - 1] == '\n';
  915. } else {
  916. msgoutlen = 0;
  917. msgout = estrdup("");
  918. }
  919. break;
  920. case P_STDOUT:
  921. case P_STDERR:
  922. if (msg) {
  923. PHPDBG_G(last_was_newline) = msg[msglen - 1] == '\n';
  924. if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) {
  925. zend_string *encoded;
  926. if (PHPDBG_G(in_script_xml) != type) {
  927. char *stream_buf;
  928. int stream_buflen = phpdbg_asprintf(&stream_buf, "<stream type=\"%s\">", type == P_STDERR ? "stderr" : "stdout");
  929. phpdbg_mixed_write(fd, stream_buf, stream_buflen);
  930. efree(stream_buf);
  931. PHPDBG_G(in_script_xml) = type;
  932. }
  933. encoded = php_escape_html_entities((unsigned char *) msg, msglen, 0, ENT_NOQUOTES, PG(internal_encoding) && PG(internal_encoding)[0] ? PG(internal_encoding) : (SG(default_charset) ? SG(default_charset) : "UTF-8"));
  934. buflen = ZSTR_LEN(encoded);
  935. memcpy(buf = emalloc(buflen + 1), ZSTR_VAL(encoded), buflen);
  936. phpdbg_encode_ctrl_chars(&buf, &buflen);
  937. phpdbg_mixed_write(fd, buf, buflen);
  938. efree(buf);
  939. } else {
  940. phpdbg_mixed_write(fd, msg, msglen);
  941. }
  942. return msglen;
  943. }
  944. break;
  945. /* no formatting on logging output */
  946. case P_LOG:
  947. severity = "log";
  948. if (msg) {
  949. struct timeval tp;
  950. if (gettimeofday(&tp, NULL) == SUCCESS) {
  951. msgoutlen = phpdbg_asprintf(&msgout, "[%ld %.8F]: %.*s\n", tp.tv_sec, tp.tv_usec / 1000000., msglen, msg);
  952. } else {
  953. msgoutlen = FAILURE;
  954. }
  955. }
  956. break;
  957. }
  958. if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) {
  959. char *xmlout;
  960. if (PHPDBG_G(req_id)) {
  961. char *xmlbuf = NULL;
  962. xmllen = phpdbg_asprintf(&xmlbuf, "req=\"" ZEND_ULONG_FMT "\" %.*s", PHPDBG_G(req_id), xmllen, xml);
  963. xml = xmlbuf;
  964. }
  965. if (msgout) {
  966. buflen = phpdbg_encode_xml(&buf, msgout, msgoutlen, '"', "&quot;");
  967. xmloutlen = phpdbg_asprintf(&xmlout, "<%s severity=\"%s\" %.*s msgout=\"%.*s\" />", tag, severity, xmllen, xml, buflen, buf);
  968. efree(buf);
  969. } else {
  970. xmloutlen = phpdbg_asprintf(&xmlout, "<%s severity=\"%s\" %.*s msgout=\"\" />", tag, severity, xmllen, xml);
  971. }
  972. phpdbg_encode_ctrl_chars(&xmlout, &xmloutlen);
  973. phpdbg_eol_convert(&xmlout, &xmloutlen);
  974. phpdbg_mixed_write(fd, xmlout, xmloutlen);
  975. efree(xmlout);
  976. } else if (msgout) {
  977. phpdbg_eol_convert(&msgout, &msgoutlen);
  978. phpdbg_mixed_write(fd, msgout, msgoutlen);
  979. }
  980. if (PHPDBG_G(req_id) && (PHPDBG_G(flags) & PHPDBG_WRITE_XML)) {
  981. efree((char *) xml);
  982. }
  983. if (msgout) {
  984. efree(msgout);
  985. }
  986. return msgout ? msgoutlen : xmloutlen;
  987. } /* }}} */
  988. PHPDBG_API int phpdbg_vprint(int type, int fd, const char *tag, const char *xmlfmt, const char *strfmt, va_list args) {
  989. char *msg = NULL, *xml = NULL;
  990. int msglen = 0, xmllen = 0;
  991. int len;
  992. va_list argcpy;
  993. if (strfmt != NULL && strlen(strfmt) > 0L) {
  994. va_copy(argcpy, args);
  995. msglen = phpdbg_xml_vasprintf(&msg, strfmt, 0, argcpy);
  996. va_end(argcpy);
  997. }
  998. if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) {
  999. if (xmlfmt != NULL && strlen(xmlfmt) > 0L) {
  1000. va_copy(argcpy, args);
  1001. xmllen = phpdbg_xml_vasprintf(&xml, xmlfmt, 1, argcpy);
  1002. va_end(argcpy);
  1003. } else {
  1004. xml = estrdup("");
  1005. }
  1006. }
  1007. if (PHPDBG_G(err_buf).active && type != P_STDOUT && type != P_STDERR) {
  1008. phpdbg_free_err_buf();
  1009. PHPDBG_G(err_buf).type = type;
  1010. PHPDBG_G(err_buf).fd = fd;
  1011. PHPDBG_G(err_buf).tag = estrdup(tag);
  1012. PHPDBG_G(err_buf).msg = msg;
  1013. PHPDBG_G(err_buf).msglen = msglen;
  1014. if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) {
  1015. PHPDBG_G(err_buf).xml = xml;
  1016. PHPDBG_G(err_buf).xmllen = xmllen;
  1017. }
  1018. return msglen;
  1019. }
  1020. len = phpdbg_process_print(fd, type, tag, msg, msglen, xml, xmllen);
  1021. if (msg) {
  1022. efree(msg);
  1023. }
  1024. if (xml) {
  1025. efree(xml);
  1026. }
  1027. return len;
  1028. }
  1029. PHPDBG_API void phpdbg_free_err_buf(void) {
  1030. if (PHPDBG_G(err_buf).type == 0) {
  1031. return;
  1032. }
  1033. PHPDBG_G(err_buf).type = 0;
  1034. efree(PHPDBG_G(err_buf).tag);
  1035. efree(PHPDBG_G(err_buf).msg);
  1036. if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) {
  1037. efree(PHPDBG_G(err_buf).xml);
  1038. }
  1039. }
  1040. PHPDBG_API void phpdbg_activate_err_buf(zend_bool active) {
  1041. PHPDBG_G(err_buf).active = active;
  1042. }
  1043. PHPDBG_API int phpdbg_output_err_buf(const char *tag, const char *xmlfmt, const char *strfmt, ...) {
  1044. int len;
  1045. va_list args;
  1046. int errbuf_active = PHPDBG_G(err_buf).active;
  1047. if (PHPDBG_G(flags) & PHPDBG_DISCARD_OUTPUT) {
  1048. return 0;
  1049. }
  1050. PHPDBG_G(err_buf).active = 0;
  1051. va_start(args, strfmt);
  1052. len = phpdbg_vprint(PHPDBG_G(err_buf).type, PHPDBG_G(err_buf).fd, tag ? tag : PHPDBG_G(err_buf).tag, xmlfmt, strfmt, args);
  1053. va_end(args);
  1054. PHPDBG_G(err_buf).active = errbuf_active;
  1055. phpdbg_free_err_buf();
  1056. return len;
  1057. }
  1058. PHPDBG_API int phpdbg_print(int type, int fd, const char *tag, const char *xmlfmt, const char *strfmt, ...) {
  1059. va_list args;
  1060. int len;
  1061. if (PHPDBG_G(flags) & PHPDBG_DISCARD_OUTPUT) {
  1062. return 0;
  1063. }
  1064. va_start(args, strfmt);
  1065. len = phpdbg_vprint(type, fd, tag, xmlfmt, strfmt, args);
  1066. va_end(args);
  1067. return len;
  1068. }
  1069. PHPDBG_API int phpdbg_xml_internal(int fd, const char *fmt, ...) {
  1070. int len = 0;
  1071. if (PHPDBG_G(flags) & PHPDBG_DISCARD_OUTPUT) {
  1072. return 0;
  1073. }
  1074. if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) {
  1075. va_list args;
  1076. char *buffer;
  1077. int buflen;
  1078. va_start(args, fmt);
  1079. buflen = phpdbg_xml_vasprintf(&buffer, fmt, 1, args);
  1080. va_end(args);
  1081. phpdbg_encode_ctrl_chars(&buffer, &buflen);
  1082. if (PHPDBG_G(in_script_xml)) {
  1083. phpdbg_mixed_write(fd, ZEND_STRL("</stream>"));
  1084. PHPDBG_G(in_script_xml) = 0;
  1085. }
  1086. len = phpdbg_mixed_write(fd, buffer, buflen);
  1087. efree(buffer);
  1088. }
  1089. return len;
  1090. }
  1091. PHPDBG_API int phpdbg_log_internal(int fd, const char *fmt, ...) {
  1092. va_list args;
  1093. char *buffer;
  1094. int buflen;
  1095. int len = 0;
  1096. va_start(args, fmt);
  1097. buflen = phpdbg_xml_vasprintf(&buffer, fmt, 0, args);
  1098. va_end(args);
  1099. len = phpdbg_mixed_write(fd, buffer, buflen);
  1100. efree(buffer);
  1101. return len;
  1102. }
  1103. PHPDBG_API int phpdbg_out_internal(int fd, const char *fmt, ...) {
  1104. va_list args;
  1105. char *buffer;
  1106. int buflen;
  1107. int len = 0;
  1108. if (PHPDBG_G(flags) & PHPDBG_DISCARD_OUTPUT) {
  1109. return 0;
  1110. }
  1111. va_start(args, fmt);
  1112. buflen = phpdbg_xml_vasprintf(&buffer, fmt, 0, args);
  1113. va_end(args);
  1114. if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) {
  1115. char *msg;
  1116. int msglen;
  1117. msglen = phpdbg_encode_xml(&msg, buffer, buflen, 256, NULL);
  1118. phpdbg_encode_ctrl_chars(&msg, &msglen);
  1119. phpdbg_eol_convert(&msg, &msglen);
  1120. if (PHPDBG_G(in_script_xml)) {
  1121. phpdbg_mixed_write(fd, ZEND_STRL("</stream>"));
  1122. PHPDBG_G(in_script_xml) = 0;
  1123. }
  1124. phpdbg_mixed_write(fd, ZEND_STRL("<phpdbg>"));
  1125. len = phpdbg_mixed_write(fd, msg, msglen);
  1126. phpdbg_mixed_write(fd, ZEND_STRL("</phpdbg>"));
  1127. } else {
  1128. phpdbg_eol_convert(&buffer, &buflen);
  1129. len = phpdbg_mixed_write(fd, buffer, buflen);
  1130. }
  1131. efree(buffer);
  1132. return len;
  1133. }
  1134. PHPDBG_API int phpdbg_rlog_internal(int fd, const char *fmt, ...) { /* {{{ */
  1135. int rc = 0;
  1136. va_list args;
  1137. struct timeval tp;
  1138. va_start(args, fmt);
  1139. if (gettimeofday(&tp, NULL) == SUCCESS) {
  1140. char friendly[100];
  1141. char *format = NULL, *buffer = NULL, *outbuf = NULL;
  1142. const time_t tt = tp.tv_sec;
  1143. #ifdef PHP_WIN32
  1144. strftime(friendly, 100, "%a %b %d %H.%%04d %Y", localtime(&tt));
  1145. #else
  1146. strftime(friendly, 100, "%a %b %d %T.%%04d %Y", localtime(&tt));
  1147. #endif
  1148. phpdbg_asprintf(&buffer, friendly, tp.tv_usec/1000);
  1149. phpdbg_asprintf(&format, "[%s]: %s\n", buffer, fmt);
  1150. rc = phpdbg_xml_vasprintf(&outbuf, format, 0, args);
  1151. if (outbuf) {
  1152. rc = phpdbg_mixed_write(fd, outbuf, rc);
  1153. efree(outbuf);
  1154. }
  1155. efree(format);
  1156. efree(buffer);
  1157. }
  1158. va_end(args);
  1159. return rc;
  1160. } /* }}} */