Skip to content
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

Request #65081 - implemeting mb_scrub #1099

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
53 changes: 53 additions & 0 deletions ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_check_encoding, 0, 0, 0)
ZEND_ARG_INFO(0, encoding)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_scrub, 0, 0, 1)
ZEND_ARG_INFO(0, str)
ZEND_ARG_INFO(0, encoding)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_mb_regex_encoding, 0, 0, 0)
ZEND_ARG_INFO(0, encoding)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -562,6 +567,7 @@ const zend_function_entry mbstring_functions[] = {
PHP_FE(mb_send_mail, arginfo_mb_send_mail)
PHP_FE(mb_get_info, arginfo_mb_get_info)
PHP_FE(mb_check_encoding, arginfo_mb_check_encoding)
PHP_FE(mb_scrub, arginfo_mb_scrub)
#if HAVE_MBREGEX
PHP_MBREGEX_FUNCTION_ENTRIES
#endif
Expand Down Expand Up @@ -4570,6 +4576,53 @@ PHP_FUNCTION(mb_check_encoding)
}
/* }}} */

static inline char* php_mb_scrub(const char* str, size_t str_len, const char* enc)
{
size_t ret_len;

return php_mb_convert_encoding(str, str_len, enc, enc, &ret_len);
}

/* {{{ proto bool mb_scrub([string str[, string encoding]]) */
PHP_FUNCTION(mb_scrub)
{
char* str;
size_t str_len;
char *enc = NULL;
size_t enc_len;

char *ret;

#ifndef FAST_ZPP
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &str, &str_len, &enc, &enc_len) == FAILURE) {
return;
}
#else
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STRING(str, str_len)
Z_PARAM_OPTIONAL
Z_PARAM_STRING(enc, enc_len)
ZEND_PARSE_PARAMETERS_END();
#endif

if (enc == NULL) {
enc = (char *) MBSTRG(current_internal_encoding)->name;
} else if (!mbfl_is_support_encoding(enc)) {
php_error_docref(NULL, E_WARNING, "Unknown encoding \"%s\"", enc);
RETURN_FALSE;
}

ret = php_mb_scrub(str, str_len, enc);

if (ret == NULL) {
RETURN_FALSE;
}

RETVAL_STRING(ret);
efree(ret);
}
/* }}} */

/* {{{ php_mb_populate_current_detect_order_list */
static void php_mb_populate_current_detect_order_list(void)
{
Expand Down
1 change: 1 addition & 0 deletions ext/mbstring/mbstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ PHP_FUNCTION(mb_decode_numericentity);
PHP_FUNCTION(mb_send_mail);
PHP_FUNCTION(mb_get_info);
PHP_FUNCTION(mb_check_encoding);
PHP_FUNCTION(mb_scrub);

MBSTRING_API char *php_mb_safe_strrchr_ex(const char *s, unsigned int c,
size_t nbytes, const mbfl_encoding *enc);
Expand Down
14 changes: 14 additions & 0 deletions ext/mbstring/tests/mb_scrub.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
mb_scrub()
--SKIPIF--
<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
--FILE--
<?php
var_dump(
"?" === mb_scrub("\x80"),
"?" === mb_scrub("\x80", 'UTF-8')
);
?>
--EXPECT--
bool(true)
bool(true)