Skip to content

Commit 11f0e1d

Browse files
committed
Move encoding fetching out of php_mb_convert_encoding()
1 parent 857fe61 commit 11f0e1d

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

ext/mbstring/mbstring.c

+15-23
Original file line numberDiff line numberDiff line change
@@ -4217,19 +4217,11 @@ static inline int php_mb_check_encoding_impl(mbfl_buffer_converter *convd, const
42174217
}
42184218

42194219

4220-
MBSTRING_API int php_mb_check_encoding(const char *input, size_t length, const char *enc)
4220+
MBSTRING_API int php_mb_check_encoding(
4221+
const char *input, size_t length, const mbfl_encoding *encoding)
42214222
{
4222-
const mbfl_encoding *encoding = MBSTRG(current_internal_encoding);
42234223
mbfl_buffer_converter *convd;
42244224

4225-
if (enc != NULL) {
4226-
encoding = mbfl_name2encoding(enc);
4227-
if (!encoding || encoding == &mbfl_encoding_pass) {
4228-
php_error_docref(NULL, E_WARNING, "Invalid encoding \"%s\"", enc);
4229-
return 0;
4230-
}
4231-
}
4232-
42334225
convd = php_mb_init_convd(encoding);
42344226
if (convd == NULL) {
42354227
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
42454237
}
42464238

42474239

4248-
MBSTRING_API int php_mb_check_encoding_recursive(HashTable *vars, const zend_string *enc)
4240+
static int php_mb_check_encoding_recursive(HashTable *vars, const mbfl_encoding *encoding)
42494241
{
4250-
const mbfl_encoding *encoding = MBSTRG(current_internal_encoding);
42514242
mbfl_buffer_converter *convd;
42524243
zend_long idx;
42534244
zend_string *key;
@@ -4256,14 +4247,6 @@ MBSTRING_API int php_mb_check_encoding_recursive(HashTable *vars, const zend_str
42564247

42574248
(void)(idx);
42584249

4259-
if (enc != NULL) {
4260-
encoding = mbfl_name2encoding(ZSTR_VAL(enc));
4261-
if (!encoding || encoding == &mbfl_encoding_pass) {
4262-
php_error_docref(NULL, E_WARNING, "Invalid encoding \"%s\"", ZSTR_VAL(enc));
4263-
return 0;
4264-
}
4265-
}
4266-
42674250
convd = php_mb_init_convd(encoding);
42684251
if (convd == NULL) {
42694252
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
42924275
}
42934276
break;
42944277
case IS_ARRAY:
4295-
if (!php_mb_check_encoding_recursive(Z_ARRVAL_P(entry), enc)) {
4278+
if (!php_mb_check_encoding_recursive(Z_ARRVAL_P(entry), encoding)) {
42964279
valid = 0;
42974280
break;
42984281
}
@@ -4321,20 +4304,29 @@ PHP_FUNCTION(mb_check_encoding)
43214304
{
43224305
zend_string *input_str = NULL, *enc = NULL;
43234306
HashTable *input_ht = NULL;
4307+
const mbfl_encoding *encoding = MBSTRG(current_internal_encoding);
43244308

43254309
ZEND_PARSE_PARAMETERS_START(0, 2)
43264310
Z_PARAM_OPTIONAL
43274311
Z_PARAM_STR_OR_ARRAY_HT(input_str, input_ht)
43284312
Z_PARAM_STR(enc)
43294313
ZEND_PARSE_PARAMETERS_END();
43304314

4315+
if (enc != NULL) {
4316+
encoding = mbfl_name2encoding(ZSTR_VAL(enc));
4317+
if (!encoding || encoding == &mbfl_encoding_pass) {
4318+
php_error_docref(NULL, E_WARNING, "Invalid encoding \"%s\"", ZSTR_VAL(enc));
4319+
RETURN_FALSE;
4320+
}
4321+
}
4322+
43314323
if (input_ht) {
4332-
if (!php_mb_check_encoding_recursive(input_ht, enc)) {
4324+
if (!php_mb_check_encoding_recursive(input_ht, encoding)) {
43334325
RETURN_FALSE;
43344326
}
43354327
RETURN_TRUE;
43364328
} else if (input_str) {
4337-
if (!php_mb_check_encoding(ZSTR_VAL(input_str), ZSTR_LEN(input_str), enc ? ZSTR_VAL(enc): NULL)) {
4329+
if (!php_mb_check_encoding(ZSTR_VAL(input_str), ZSTR_LEN(input_str), encoding)) {
43384330
RETURN_FALSE;
43394331
}
43404332
RETURN_TRUE;

ext/mbstring/mbstring.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ MBSTRING_API size_t php_mb_mbchar_bytes_ex(const char *s, const mbfl_encoding *e
125125
MBSTRING_API size_t php_mb_mbchar_bytes(const char *s);
126126

127127
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);
128-
MBSTRING_API int php_mb_check_encoding(const char *input, size_t length, const char *enc);
128+
MBSTRING_API int php_mb_check_encoding(const char *input, size_t length, const mbfl_encoding *encoding);
129129

130130
ZEND_BEGIN_MODULE_GLOBALS(mbstring)
131131
char *internal_encoding_name;

ext/mbstring/php_mbregex.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,13 @@ const char *php_mb_regex_get_mbctype(void)
438438
}
439439
/* }}} */
440440

441+
/* {{{ php_mb_regex_get_mbctype_encoding */
442+
const mbfl_encoding *php_mb_regex_get_mbctype_encoding(void)
443+
{
444+
return mbfl_name2encoding(_php_mb_regex_mbctype2name(MBREX(current_mbctype)));
445+
}
446+
/* }}} */
447+
441448
/* {{{ php_mb_regex_get_default_mbctype */
442449
const char *php_mb_regex_get_default_mbctype(void)
443450
{
@@ -457,7 +464,7 @@ static php_mb_regex_t *php_mbregex_compile_pattern(const char *pattern, size_t p
457464
OnigUChar err_str[ONIG_MAX_ERROR_MESSAGE_LEN];
458465
OnigEncoding enc = MBREX(current_mbctype);
459466

460-
if (!php_mb_check_encoding(pattern, patlen, _php_mb_regex_mbctype2name(enc))) {
467+
if (!php_mb_check_encoding(pattern, patlen, php_mb_regex_get_mbctype_encoding())) {
461468
php_error_docref(NULL, E_WARNING,
462469
"Pattern is not valid under %s encoding", _php_mb_regex_mbctype2name(enc));
463470
return NULL;
@@ -912,7 +919,7 @@ static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
912919
if (!php_mb_check_encoding(
913920
string,
914921
string_len,
915-
php_mb_regex_get_mbctype()
922+
php_mb_regex_get_mbctype_encoding()
916923
)) {
917924
RETURN_FALSE;
918925
}
@@ -1053,7 +1060,7 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp
10531060
if (!php_mb_check_encoding(
10541061
string,
10551062
string_len,
1056-
php_mb_regex_get_mbctype()
1063+
php_mb_regex_get_mbctype_encoding()
10571064
)) {
10581065
RETURN_NULL();
10591066
}
@@ -1260,7 +1267,7 @@ PHP_FUNCTION(mb_split)
12601267
count--;
12611268
}
12621269

1263-
if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype())) {
1270+
if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype_encoding())) {
12641271
RETURN_FALSE;
12651272
}
12661273

@@ -1354,7 +1361,7 @@ PHP_FUNCTION(mb_ereg_match)
13541361
}
13551362
}
13561363

1357-
if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype())) {
1364+
if (!php_mb_check_encoding(string, string_len, php_mb_regex_get_mbctype_encoding())) {
13581365
RETURN_FALSE;
13591366
}
13601367

@@ -1566,7 +1573,7 @@ PHP_FUNCTION(mb_ereg_search_init)
15661573
if (php_mb_check_encoding(
15671574
ZSTR_VAL(arg_str),
15681575
ZSTR_LEN(arg_str),
1569-
php_mb_regex_get_mbctype()
1576+
php_mb_regex_get_mbctype_encoding()
15701577
)) {
15711578
MBREX(search_pos) = 0;
15721579
RETVAL_TRUE;

0 commit comments

Comments
 (0)