Skip to content

Commit 1686c3f

Browse files
committed
Improve ascii check
1 parent f7b053f commit 1686c3f

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

win32/codepage.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ PW32CP wchar_t *php_win32_cp_conv_ascii_to_w(const char* in, size_t in_len, size
105105
{/*{{{*/
106106
wchar_t *ret, *ret_idx;
107107
const char *idx = in, *end;
108+
char ch_err = 0;
108109

109110
assert(in && in_len ? in[in_len] == '\0' : 1);
110111

@@ -123,29 +124,33 @@ PW32CP wchar_t *php_win32_cp_conv_ascii_to_w(const char* in, size_t in_len, size
123124

124125
/* Process unaligned chunk. */
125126
while (idx < aidx) {
126-
if (!__isascii(*idx) && '\0' != *idx) {
127-
ASCII_FAIL_RETURN()
128-
}
127+
ch_err |= *idx;
129128
idx++;
130129
}
130+
if (ch_err & 0x80) {
131+
ASCII_FAIL_RETURN()
132+
}
131133

132134
/* Process aligned chunk. */
135+
__m128i vec_err = _mm_setzero_si128();
133136
while (end - idx > 15) {
134137
const __m128i block = _mm_load_si128((__m128i *)idx);
135-
if (_mm_movemask_epi8(block)) {
136-
ASCII_FAIL_RETURN()
137-
}
138+
vec_err = _mm_or_si128(vec_err, block);
138139
idx += 16;
139140
}
141+
if (_mm_movemask_epi8(vec_err)) {
142+
ASCII_FAIL_RETURN()
143+
}
140144
}
141145

142146
/* Process the trailing part, or otherwise process string < 16 bytes. */
143147
while (idx < end) {
144-
if (!__isascii(*idx) && '\0' != *idx) {
145-
ASCII_FAIL_RETURN()
146-
}
148+
ch_err |= *idx;
147149
idx++;
148150
}
151+
if (ch_err & 0x80) {
152+
ASCII_FAIL_RETURN()
153+
}
149154

150155
ret = malloc((in_len+1)*sizeof(wchar_t));
151156
if (!ret) {
@@ -156,9 +161,6 @@ PW32CP wchar_t *php_win32_cp_conv_ascii_to_w(const char* in, size_t in_len, size
156161
ret_idx = ret;
157162
idx = in;
158163

159-
/* Check and conversion could be merged. This however would
160-
be more expencive, if a non ASCII string was passed.
161-
TODO check whether the impact is acceptable. */
162164
if (in_len > 15) {
163165
const char *aidx = (const char *)ZEND_SLIDE_TO_ALIGNED16(in);
164166

0 commit comments

Comments
 (0)