Skip to content

Commit

Permalink
Merged pull request #449
Browse files Browse the repository at this point in the history
  • Loading branch information
derickr committed Feb 15, 2019
2 parents 53d9655 + 4ea954f commit f0073b2
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 53 deletions.
3 changes: 3 additions & 0 deletions run-xdebug-tests.php
Expand Up @@ -142,6 +142,9 @@
} }
} }
$environment['TEST_PHP_EXECUTABLE'] = $php; $environment['TEST_PHP_EXECUTABLE'] = $php;
} else {
$php = trim(`which php`);
$environment['TEST_PHP_EXECUTABLE'] = $php;
} }


if (getenv('TEST_PHP_CGI_EXECUTABLE')) { if (getenv('TEST_PHP_CGI_EXECUTABLE')) {
Expand Down
217 changes: 203 additions & 14 deletions xdebug_compat.c
Expand Up @@ -74,34 +74,223 @@ char *xdebug_str_to_str(char *haystack, size_t length, const char *needle, size_
return retval; return retval;
} }


char *xdebug_base64_encode(unsigned char *data, int data_len, int *new_len) /* {{{ base64 tables */
static const char base64_table[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0'
};

static const char base64_pad = '=';

static const short base64_reverse_table[256] = {
-2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
-2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
-2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
};
/* }}} */

static unsigned char *xdebug_base64_encode_impl(const unsigned char *in, size_t inl, unsigned char *out) /* {{{ */
{ {
zend_string *new_str;
char *retval;


new_str = php_base64_encode(data, data_len); while (inl > 2) { /* keep going until we have less than 24 bits */
*new_len = new_str->len; *out++ = base64_table[in[0] >> 2];
*out++ = base64_table[((in[0] & 0x03) << 4) + (in[1] >> 4)];
*out++ = base64_table[((in[1] & 0x0f) << 2) + (in[2] >> 6)];
*out++ = base64_table[in[2] & 0x3f];


retval = estrndup(new_str->val, new_str->len); in += 3;
inl -= 3; /* we just handle 3 octets of data */
}


zend_string_release(new_str); /* now deal with the tail end of things */
if (inl != 0) {
*out++ = base64_table[in[0] >> 2];
if (inl > 1) {
*out++ = base64_table[((in[0] & 0x03) << 4) + (in[1] >> 4)];
*out++ = base64_table[(in[1] & 0x0f) << 2];
*out++ = base64_pad;
} else {
*out++ = base64_table[(in[0] & 0x03) << 4];
*out++ = base64_pad;
*out++ = base64_pad;
}
}

*out = '\0';

return out;
}
/* }}} */

static int xdebug_base64_decode_impl(const unsigned char *in, size_t inl, unsigned char *out, size_t *outl, zend_bool strict) /* {{{ */
{
int ch;
size_t i = 0, padding = 0, j = *outl;

/* run through the whole string, converting as we go */
while (inl-- > 0) {
ch = *in++;
if (ch == base64_pad) {
padding++;
continue;
}

ch = base64_reverse_table[ch];
if (!strict) {
/* skip unknown characters and whitespace */
if (ch < 0) {
continue;
}
} else {
/* skip whitespace */
if (ch == -1) {
continue;
}
/* fail on bad characters or if any data follows padding */
if (ch == -2 || padding) {
goto fail;
}
}

switch (i % 4) {
case 0:
out[j] = ch << 2;
break;
case 1:
out[j++] |= ch >> 4;
out[j] = (ch & 0x0f) << 4;
break;
case 2:
out[j++] |= ch >>2;
out[j] = (ch & 0x03) << 6;
break;
case 3:
out[j++] |= ch;
break;
}
i++;
}

/* fail if the input is truncated (only one char in last group) */
if (strict && i % 4 == 1) {
goto fail;
}

/* fail if the padding length is wrong (not VV==, VVV=), but accept zero padding
* RFC 4648: "In some circumstances, the use of padding [--] is not required" */
if (strict && padding && (padding > 2 || (i + padding) % 4 != 0)) {
goto fail;
}

*outl = j;
out[j] = '\0';

return 1;

fail:
return 0;
}
/* }}} */

unsigned char *xdebug_base64_encode(unsigned char *data, size_t data_len, size_t *new_len)
{
unsigned char *retval = xdmalloc((((data_len + 2) / 3) + 1) * (4 * sizeof(char)));
unsigned char *end;

end = xdebug_base64_encode_impl(data, data_len, retval);
*new_len = end - retval;


return retval; return retval;
} }


unsigned char *xdebug_base64_decode(unsigned char *data, int data_len, int *new_len) unsigned char *xdebug_base64_decode(unsigned char *data, size_t data_len, size_t *new_len)
{ {
unsigned char *retval = xdmalloc(data_len + 1);

xdebug_base64_decode_impl(data, data_len, retval, new_len, 0);

return retval;
}

zend_string *xdebug_addslashes(zend_string *str)
{
/* maximum string length, worst case situation */
char *target;
const char *source, *end;
size_t offset;
zend_string *new_str; zend_string *new_str;
char *retval;


new_str = php_base64_decode(data, data_len); if (!str) {
*new_len = new_str->len; return ZSTR_EMPTY_ALLOC();
}


retval = estrndup(new_str->val, new_str->len); source = ZSTR_VAL(str);
end = source + ZSTR_LEN(str);

while (source < end) {
switch (*source) {
case '\0':
case '\'':
case '\"':
case '\\':
goto do_escape;
default:
source++;
break;
}
}


zend_string_release(new_str); return zend_string_copy(str);

do_escape:
offset = source - (char *)ZSTR_VAL(str);
new_str = zend_string_safe_alloc(2, ZSTR_LEN(str) - offset, offset, 0);
memcpy(ZSTR_VAL(new_str), ZSTR_VAL(str), offset);
target = ZSTR_VAL(new_str) + offset;

while (source < end) {
switch (*source) {
case '\0':
*target++ = '\\';
*target++ = '0';
break;
case '\'':
case '\"':
case '\\':
*target++ = '\\';
/* break is missing *intentionally* */
default:
*target++ = *source;
break;
}
source++;
}

*target = '\0';

if (ZSTR_LEN(new_str) - (target - ZSTR_VAL(new_str)) > 16) {
new_str = zend_string_truncate(new_str, target - ZSTR_VAL(new_str), 0);
} else {
ZSTR_LEN(new_str) = target - ZSTR_VAL(new_str);
}


return (unsigned char*) retval; return new_str;
} }


void xdebug_stripcslashes(char *str, int *len) void xdebug_stripcslashes(char *str, int *len)
Expand Down
8 changes: 6 additions & 2 deletions xdebug_compat.h
Expand Up @@ -29,9 +29,13 @@
zval *xdebug_zval_ptr(int op_type, const znode_op *node, zend_execute_data *zdata TSRMLS_DC); zval *xdebug_zval_ptr(int op_type, const znode_op *node, zend_execute_data *zdata TSRMLS_DC);


char *xdebug_str_to_str(char *haystack, size_t length, const char *needle, size_t needle_len, const char *str, size_t str_len, size_t *new_len); char *xdebug_str_to_str(char *haystack, size_t length, const char *needle, size_t needle_len, const char *str, size_t str_len, size_t *new_len);
char *xdebug_base64_encode(unsigned char *data, int data_len, int *new_len);
unsigned char *xdebug_base64_decode(unsigned char *data, int data_len, int *new_len); unsigned char *xdebug_base64_encode(unsigned char *data, size_t data_len, size_t *new_len);
unsigned char *xdebug_base64_decode(unsigned char *data, size_t data_len, size_t *new_len);

zend_string *xdebug_addslashes(zend_string *str);
void xdebug_stripcslashes(char *string, int *new_len); void xdebug_stripcslashes(char *string, int *new_len);

zend_class_entry *xdebug_fetch_class(char *classname, int classname_len, int flags TSRMLS_DC); zend_class_entry *xdebug_fetch_class(char *classname, int classname_len, int flags TSRMLS_DC);
int xdebug_get_constant(xdebug_str *val, zval *const_val TSRMLS_DC); int xdebug_get_constant(xdebug_str *val, zval *const_val TSRMLS_DC);
void xdebug_setcookie(const char *name, int name_len, char *value, int value_len, time_t expires, const char *path, int path_len, const char *domain, int domain_len, int secure, int url_encode, int httponly TSRMLS_DC); void xdebug_setcookie(const char *name, int name_len, char *value, int value_len, time_t expires, const char *path, int path_len, const char *domain, int domain_len, int secure, int url_encode, int httponly TSRMLS_DC);
Expand Down
12 changes: 6 additions & 6 deletions xdebug_handler_dbgp.c
Expand Up @@ -764,7 +764,7 @@ DBGP_FUNC(breakpoint_set)
xdebug_brk_info *brk_info; xdebug_brk_info *brk_info;
char *tmp_name; char *tmp_name;
int brk_id = 0; int brk_id = 0;
int new_length = 0; size_t new_length = 0;
function_stack_entry *fse; function_stack_entry *fse;
XDEBUG_STR_SWITCH_DECL; XDEBUG_STR_SWITCH_DECL;


Expand Down Expand Up @@ -952,7 +952,7 @@ DBGP_FUNC(eval)
char *eval_string; char *eval_string;
xdebug_xml_node *ret_xml; xdebug_xml_node *ret_xml;
zval ret_zval; zval ret_zval;
int new_length; size_t new_length = 0;
int res; int res;
xdebug_var_export_options *options; xdebug_var_export_options *options;


Expand All @@ -973,7 +973,7 @@ DBGP_FUNC(eval)


res = xdebug_do_eval(eval_string, &ret_zval TSRMLS_CC); res = xdebug_do_eval(eval_string, &ret_zval TSRMLS_CC);


efree(eval_string); xdfree(eval_string);


/* Handle result */ /* Handle result */
if (res == FAILURE) { if (res == FAILURE) {
Expand Down Expand Up @@ -1426,7 +1426,7 @@ static void set_vars_from_EG(TSRMLS_D)
DBGP_FUNC(property_set) DBGP_FUNC(property_set)
{ {
unsigned char *new_value; unsigned char *new_value;
int new_length; size_t new_length = 0;
int depth = 0; int depth = 0;
int context_nr = 0; int context_nr = 0;
int res; int res;
Expand Down Expand Up @@ -1487,7 +1487,7 @@ DBGP_FUNC(property_set)


/* Handle result */ /* Handle result */
if (Z_TYPE(symbol) == IS_UNDEF) { if (Z_TYPE(symbol) == IS_UNDEF) {
efree(new_value); xdfree(new_value);
RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_PROPERTY_NON_EXISTENT); RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_PROPERTY_NON_EXISTENT);
} else { } else {
// TODO Doesn't make sense anymore in this form // TODO Doesn't make sense anymore in this form
Expand Down Expand Up @@ -1538,7 +1538,7 @@ DBGP_FUNC(property_set)


/* Free data */ /* Free data */
xdfree(eval_string); xdfree(eval_string);
efree(new_value); xdfree(new_value);


/* Handle result */ /* Handle result */
if (res == FAILURE) { if (res == FAILURE) {
Expand Down

0 comments on commit f0073b2

Please sign in to comment.