]> granicus.if.org Git - php/commitdiff
Move encoding fetching out of php_mb_convert_encoding()
authorNikita Popov <nikita.ppv@gmail.com>
Tue, 31 Mar 2020 19:47:55 +0000 (21:47 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Tue, 31 Mar 2020 19:47:55 +0000 (21:47 +0200)
ext/mbstring/mbstring.c
ext/mbstring/mbstring.h
ext/mbstring/php_mbregex.c

index 4f5662f30afd11309e447b0749936cbd946fb68a..6b37881589b782f0b873baf35b0afbf1ce21f6d3 100644 (file)
@@ -4217,19 +4217,11 @@ static inline int php_mb_check_encoding_impl(mbfl_buffer_converter *convd, const
 }
 
 
-MBSTRING_API int php_mb_check_encoding(const char *input, size_t length, const char *enc)
+MBSTRING_API int php_mb_check_encoding(
+               const char *input, size_t length, const mbfl_encoding *encoding)
 {
-       const mbfl_encoding *encoding = MBSTRG(current_internal_encoding);
        mbfl_buffer_converter *convd;
 
-       if (enc != NULL) {
-               encoding = mbfl_name2encoding(enc);
-               if (!encoding || encoding == &mbfl_encoding_pass) {
-                       php_error_docref(NULL, E_WARNING, "Invalid encoding \"%s\"", enc);
-                       return 0;
-               }
-       }
-
        convd = php_mb_init_convd(encoding);
        if (convd == NULL) {
                php_error_docref(NULL, E_WARNING, "Unable to create converter");
@@ -4245,9 +4237,8 @@ MBSTRING_API int php_mb_check_encoding(const char *input, size_t length, const c
 }
 
 
-MBSTRING_API int php_mb_check_encoding_recursive(HashTable *vars, const zend_string *enc)
+static int php_mb_check_encoding_recursive(HashTable *vars, const mbfl_encoding *encoding)
 {
-       const mbfl_encoding *encoding = MBSTRG(current_internal_encoding);
        mbfl_buffer_converter *convd;
        zend_long idx;
        zend_string *key;
@@ -4256,14 +4247,6 @@ MBSTRING_API int php_mb_check_encoding_recursive(HashTable *vars, const zend_str
 
        (void)(idx);
 
-       if (enc != NULL) {
-               encoding = mbfl_name2encoding(ZSTR_VAL(enc));
-               if (!encoding || encoding == &mbfl_encoding_pass) {
-                       php_error_docref(NULL, E_WARNING, "Invalid encoding \"%s\"", ZSTR_VAL(enc));
-                       return 0;
-               }
-       }
-
        convd = php_mb_init_convd(encoding);
        if (convd == NULL) {
                php_error_docref(NULL, E_WARNING, "Unable to create converter");
@@ -4292,7 +4275,7 @@ MBSTRING_API int php_mb_check_encoding_recursive(HashTable *vars, const zend_str
                                }
                                break;
                        case IS_ARRAY:
-                               if (!php_mb_check_encoding_recursive(Z_ARRVAL_P(entry), enc)) {
+                               if (!php_mb_check_encoding_recursive(Z_ARRVAL_P(entry), encoding)) {
                                        valid = 0;
                                        break;
                                }
@@ -4321,6 +4304,7 @@ PHP_FUNCTION(mb_check_encoding)
 {
        zend_string *input_str = NULL, *enc = NULL;
        HashTable *input_ht = NULL;
+       const mbfl_encoding *encoding = MBSTRG(current_internal_encoding);
 
        ZEND_PARSE_PARAMETERS_START(0, 2)
                Z_PARAM_OPTIONAL
@@ -4328,13 +4312,21 @@ PHP_FUNCTION(mb_check_encoding)
                Z_PARAM_STR(enc)
        ZEND_PARSE_PARAMETERS_END();
 
+       if (enc != NULL) {
+               encoding = mbfl_name2encoding(ZSTR_VAL(enc));
+               if (!encoding || encoding == &mbfl_encoding_pass) {
+                       php_error_docref(NULL, E_WARNING, "Invalid encoding \"%s\"", ZSTR_VAL(enc));
+                       RETURN_FALSE;
+               }
+       }
+
        if (input_ht) {
-               if (!php_mb_check_encoding_recursive(input_ht, enc)) {
+               if (!php_mb_check_encoding_recursive(input_ht, encoding)) {
                        RETURN_FALSE;
                }
                RETURN_TRUE;
        } else if (input_str) {
-               if (!php_mb_check_encoding(ZSTR_VAL(input_str), ZSTR_LEN(input_str), enc ? ZSTR_VAL(enc): NULL)) {
+               if (!php_mb_check_encoding(ZSTR_VAL(input_str), ZSTR_LEN(input_str), encoding)) {
                        RETURN_FALSE;
                }
                RETURN_TRUE;
index 0e16bf7631733c9d9aaa8ff1a4644bb047987bc0..290bdf04d76d8aa133b0f90d33dde3d448d711fe 100644 (file)
@@ -125,7 +125,7 @@ MBSTRING_API size_t php_mb_mbchar_bytes_ex(const char *s, const mbfl_encoding *e
 MBSTRING_API size_t php_mb_mbchar_bytes(const char *s);
 
 MBSTRING_API size_t php_mb_stripos(int mode, const char *old_haystack, size_t old_haystack_len, const char *old_needle, size_t old_needle_len, zend_long offset, const mbfl_encoding *encoding);
-MBSTRING_API int php_mb_check_encoding(const char *input, size_t length, const char *enc);
+MBSTRING_API int php_mb_check_encoding(const char *input, size_t length, const mbfl_encoding *encoding);
 
 ZEND_BEGIN_MODULE_GLOBALS(mbstring)
        char *internal_encoding_name;
index 2f50b5f618391e3ac31c05ebcbac312aba04c0ce..c4e066fccc31ac0f0c5bdaacf48cbb5d82b3e597 100644 (file)
@@ -438,6 +438,13 @@ const char *php_mb_regex_get_mbctype(void)
 }
 /* }}} */
 
+/* {{{ php_mb_regex_get_mbctype_encoding */
+const mbfl_encoding *php_mb_regex_get_mbctype_encoding(void)
+{
+       return mbfl_name2encoding(_php_mb_regex_mbctype2name(MBREX(current_mbctype)));
+}
+/* }}} */
+
 /* {{{ php_mb_regex_get_default_mbctype */
 const char *php_mb_regex_get_default_mbctype(void)
 {
@@ -457,7 +464,7 @@ static php_mb_regex_t *php_mbregex_compile_pattern(const char *pattern, size_t p
        OnigUChar err_str[ONIG_MAX_ERROR_MESSAGE_LEN];
        OnigEncoding enc = MBREX(current_mbctype);
 
-       if (!php_mb_check_encoding(pattern, patlen, _php_mb_regex_mbctype2name(enc))) {
+       if (!php_mb_check_encoding(pattern, patlen, php_mb_regex_get_mbctype_encoding())) {
                php_error_docref(NULL, E_WARNING,
                        "Pattern is not valid under %s encoding", _php_mb_regex_mbctype2name(enc));
                return NULL;
@@ -912,7 +919,7 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
        if (!php_mb_check_encoding(
                string,
                string_len,
-               php_mb_regex_get_mbctype()
+               php_mb_regex_get_mbctype_encoding()
        )) {
                RETURN_FALSE;
        }
@@ -1053,7 +1060,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
                if (!php_mb_check_encoding(
                string,
                string_len,
-               php_mb_regex_get_mbctype()
+               php_mb_regex_get_mbctype_encoding()
                )) {
                        RETURN_NULL();
                }
@@ -1260,7 +1267,7 @@ PHP_FUNCTION(mb_split)
                count--;
        }
 
-       if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype())) {
+       if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype_encoding())) {
                RETURN_FALSE;
        }
 
@@ -1354,7 +1361,7 @@ PHP_FUNCTION(mb_ereg_match)
                }
        }
 
-       if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype())) {
+       if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype_encoding())) {
                RETURN_FALSE;
        }
 
@@ -1566,7 +1573,7 @@ PHP_FUNCTION(mb_ereg_search_init)
        if (php_mb_check_encoding(
        ZSTR_VAL(arg_str),
        ZSTR_LEN(arg_str),
-       php_mb_regex_get_mbctype()
+       php_mb_regex_get_mbctype_encoding()
        )) {
                MBREX(search_pos) = 0;
                RETVAL_TRUE;