Skip to content

Optimize php_filter_encode_html() #18795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions ext/filter/sanitizing_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,27 @@ static void php_filter_encode_html(zval *value, const unsigned char *chars)
size_t len = Z_STRLEN_P(value);
unsigned char *s = (unsigned char *)Z_STRVAL_P(value);
unsigned char *e = s + len;
unsigned char *last_output = s;

if (Z_STRLEN_P(value) == 0) {
return;
}

while (s < e) {
if (chars[*s]) {
smart_str_appendl(&str, (const char *) last_output, s - last_output);
smart_str_appendl(&str, "&#", 2);
smart_str_append_unsigned(&str, (zend_ulong)*s);
smart_str_appendc(&str, ';');
} else {
/* XXX: this needs to be optimized to work with blocks of 'safe' chars */
smart_str_appendc(&str, *s);
last_output = s + 1;
}
s++;
}

smart_str_appendl(&str, (const char *) last_output, s - last_output);

zval_ptr_dtor(value);
ZVAL_STR(value, smart_str_extract(&str));
ZVAL_NEW_STR(value, smart_str_extract(&str));
}

static const unsigned char hexchars[] = "0123456789ABCDEF";
Expand Down