Skip to content

Commit 15e32fd

Browse files
committed
generate utf-8 string directly instead of using php_mb_convert_encoding
1 parent b8468ad commit 15e32fd

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

ext/mbstring/mbstring.c

+51-14
Original file line numberDiff line numberDiff line change
@@ -4815,28 +4815,65 @@ static inline char* php_mb_chr(zend_long cp, const char* enc, size_t *output_len
48154815
}
48164816
}
48174817

4818-
if (php_mb_is_no_encoding_unicode(no_enc)) {
48194818

4820-
if (0 > cp || 0x10ffff < cp) {
4819+
if (php_mb_is_no_encoding_utf8(no_enc)) {
48214820

4822-
if (php_mb_is_no_encoding_unicode(MBSTRG(current_internal_encoding)->no_encoding)) {
4821+
if (0 > cp || cp > 0x10ffff || (cp > 0xd7ff && 0xe000 > cp)) {
4822+
if (php_mb_is_no_encoding_utf8(MBSTRG(current_internal_encoding)->no_encoding)) {
48234823
cp = MBSTRG(current_filter_illegal_substchar);
4824+
} else if (php_mb_is_no_encoding_unicode(MBSTRG(current_internal_encoding)->no_encoding)) {
4825+
if (0xd800 > MBSTRG(current_filter_illegal_substchar) || MBSTRG(current_filter_illegal_substchar) > 0xdfff) {
4826+
cp = MBSTRG(current_filter_illegal_substchar);
4827+
} else {
4828+
cp = 0x3f;
4829+
}
48244830
} else {
48254831
cp = 0x3f;
48264832
}
4833+
}
48274834

4828-
} else if (php_mb_is_no_encoding_utf8(no_enc)) {
4835+
if (cp < 0x80) {
4836+
ret_len = 1;
4837+
ret = (char *) safe_emalloc(ret_len, 1, 1);
4838+
ret[0] = cp;
4839+
ret[1] = 0;
4840+
} else if (cp < 0x800) {
4841+
ret_len = 2;
4842+
ret = (char *) safe_emalloc(ret_len, 1, 1);
4843+
ret[0] = 0xc0 | (cp >> 6);
4844+
ret[1] = 0x80 | (cp & 0x3f);
4845+
ret[2] = 0;
4846+
} else if (cp < 0x10000) {
4847+
ret_len = 3;
4848+
ret = (char *) safe_emalloc(ret_len, 1, 1);
4849+
ret[0] = 0xe0 | (cp >> 12);
4850+
ret[1] = 0x80 | ((cp >> 6) & 0x3f);
4851+
ret[2] = 0x80 | (cp & 0x3f);
4852+
ret[3] = 0;
4853+
} else {
4854+
ret_len = 4;
4855+
ret = (char *) safe_emalloc(ret_len, 1, 1);
4856+
ret[0] = 0xf0 | (cp >> 18);
4857+
ret[1] = 0x80 | ((cp >> 12) & 0x3f);
4858+
ret[2] = 0x80 | ((cp >> 6) & 0x3f);
4859+
ret[3] = 0x80 | (cp & 0x3f);
4860+
ret[4] = 0;
4861+
}
48294862

4830-
if (cp > 0xd7ff && 0xe000 > cp) {
4831-
if (php_mb_is_no_encoding_unicode(MBSTRG(current_internal_encoding)->no_encoding)) {
4832-
if (0xd800 > MBSTRG(current_filter_illegal_substchar) || MBSTRG(current_filter_illegal_substchar) > 0xdfff) {
4833-
cp = MBSTRG(current_filter_illegal_substchar);
4834-
} else {
4835-
cp = 0x3f;
4836-
}
4837-
} else {
4838-
cp = 0x3f;
4839-
}
4863+
if (output_len) {
4864+
*output_len = ret_len;
4865+
}
4866+
4867+
return ret;
4868+
4869+
} else if (php_mb_is_no_encoding_unicode(no_enc)) {
4870+
4871+
if (0 > cp || 0x10ffff < cp) {
4872+
4873+
if (php_mb_is_no_encoding_unicode(MBSTRG(current_internal_encoding)->no_encoding)) {
4874+
cp = MBSTRG(current_filter_illegal_substchar);
4875+
} else {
4876+
cp = 0x3f;
48404877
}
48414878

48424879
}

0 commit comments

Comments
 (0)