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