xref: /PHP-8.3/Zend/zend_operators.h (revision a2068ef4)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    |          Dmitry Stogov <dmitry@php.net>                              |
18    +----------------------------------------------------------------------+
19 */
20 
21 #ifndef ZEND_OPERATORS_H
22 #define ZEND_OPERATORS_H
23 
24 #include <errno.h>
25 #include <math.h>
26 #include <assert.h>
27 #include <stddef.h>
28 #include <stdint.h>
29 
30 #ifdef HAVE_IEEEFP_H
31 #include <ieeefp.h>
32 #endif
33 
34 #include "zend_portability.h"
35 #include "zend_strtod.h"
36 #include "zend_multiply.h"
37 #include "zend_object_handlers.h"
38 
39 #define LONG_SIGN_MASK ZEND_LONG_MIN
40 
41 BEGIN_EXTERN_C()
42 ZEND_API zend_result ZEND_FASTCALL add_function(zval *result, zval *op1, zval *op2);
43 ZEND_API zend_result ZEND_FASTCALL sub_function(zval *result, zval *op1, zval *op2);
44 ZEND_API zend_result ZEND_FASTCALL mul_function(zval *result, zval *op1, zval *op2);
45 ZEND_API zend_result ZEND_FASTCALL pow_function(zval *result, zval *op1, zval *op2);
46 ZEND_API zend_result ZEND_FASTCALL div_function(zval *result, zval *op1, zval *op2);
47 ZEND_API zend_result ZEND_FASTCALL mod_function(zval *result, zval *op1, zval *op2);
48 ZEND_API zend_result ZEND_FASTCALL boolean_xor_function(zval *result, zval *op1, zval *op2);
49 ZEND_API zend_result ZEND_FASTCALL boolean_not_function(zval *result, zval *op1);
50 ZEND_API zend_result ZEND_FASTCALL bitwise_not_function(zval *result, zval *op1);
51 ZEND_API zend_result ZEND_FASTCALL bitwise_or_function(zval *result, zval *op1, zval *op2);
52 ZEND_API zend_result ZEND_FASTCALL bitwise_and_function(zval *result, zval *op1, zval *op2);
53 ZEND_API zend_result ZEND_FASTCALL bitwise_xor_function(zval *result, zval *op1, zval *op2);
54 ZEND_API zend_result ZEND_FASTCALL shift_left_function(zval *result, zval *op1, zval *op2);
55 ZEND_API zend_result ZEND_FASTCALL shift_right_function(zval *result, zval *op1, zval *op2);
56 ZEND_API zend_result ZEND_FASTCALL concat_function(zval *result, zval *op1, zval *op2);
57 
58 ZEND_API bool ZEND_FASTCALL zend_is_identical(const zval *op1, const zval *op2);
59 
60 ZEND_API zend_result ZEND_FASTCALL is_equal_function(zval *result, zval *op1, zval *op2);
61 ZEND_API zend_result ZEND_FASTCALL is_identical_function(zval *result, zval *op1, zval *op2);
62 ZEND_API zend_result ZEND_FASTCALL is_not_identical_function(zval *result, zval *op1, zval *op2);
63 ZEND_API zend_result ZEND_FASTCALL is_not_equal_function(zval *result, zval *op1, zval *op2);
64 ZEND_API zend_result ZEND_FASTCALL is_smaller_function(zval *result, zval *op1, zval *op2);
65 ZEND_API zend_result ZEND_FASTCALL is_smaller_or_equal_function(zval *result, zval *op1, zval *op2);
66 
67 ZEND_API bool ZEND_FASTCALL zend_class_implements_interface(const zend_class_entry *class_ce, const zend_class_entry *interface_ce);
68 ZEND_API bool ZEND_FASTCALL instanceof_function_slow(const zend_class_entry *instance_ce, const zend_class_entry *ce);
69 
instanceof_function(const zend_class_entry * instance_ce,const zend_class_entry * ce)70 static zend_always_inline bool instanceof_function(
71 		const zend_class_entry *instance_ce, const zend_class_entry *ce) {
72 	return instance_ce == ce || instanceof_function_slow(instance_ce, ce);
73 }
74 
75 ZEND_API bool zend_string_only_has_ascii_alphanumeric(const zend_string *str);
76 
77 /**
78  * Checks whether the string "str" with length "length" is numeric. The value
79  * of allow_errors determines whether it's required to be entirely numeric, or
80  * just its prefix. Leading whitespace is allowed.
81  *
82  * The function returns 0 if the string did not contain a valid number; IS_LONG
83  * if it contained a number that fits within the range of a long; or IS_DOUBLE
84  * if the number was out of long range or contained a decimal point/exponent.
85  * The number's value is returned into the respective pointer, *lval or *dval,
86  * if that pointer is not NULL.
87  *
88  * This variant also gives information if a string that represents an integer
89  * could not be represented as such due to overflow. It writes 1 to oflow_info
90  * if the integer is larger than ZEND_LONG_MAX and -1 if it's smaller than ZEND_LONG_MIN.
91  */
92 ZEND_API uint8_t ZEND_FASTCALL _is_numeric_string_ex(const char *str, size_t length, zend_long *lval,
93 	double *dval, bool allow_errors, int *oflow_info, bool *trailing_data);
94 
95 ZEND_API const char* ZEND_FASTCALL zend_memnstr_ex(const char *haystack, const char *needle, size_t needle_len, const char *end);
96 ZEND_API const char* ZEND_FASTCALL zend_memnrstr_ex(const char *haystack, const char *needle, size_t needle_len, const char *end);
97 
98 #if SIZEOF_ZEND_LONG == 4
99 #	define ZEND_DOUBLE_FITS_LONG(d) (!((d) > (double)ZEND_LONG_MAX || (d) < (double)ZEND_LONG_MIN))
100 #else
101 	/* >= as (double)ZEND_LONG_MAX is outside signed range */
102 #	define ZEND_DOUBLE_FITS_LONG(d) (!((d) >= (double)ZEND_LONG_MAX || (d) < (double)ZEND_LONG_MIN))
103 #endif
104 
105 ZEND_API zend_long ZEND_FASTCALL zend_dval_to_lval_slow(double d);
106 
zend_dval_to_lval(double d)107 static zend_always_inline zend_long zend_dval_to_lval(double d)
108 {
109 	if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) {
110 		return 0;
111 	} else if (!ZEND_DOUBLE_FITS_LONG(d)) {
112 		return zend_dval_to_lval_slow(d);
113 	}
114 	return (zend_long)d;
115 }
116 
117 /* Used to convert a string float to integer during an (int) cast */
zend_dval_to_lval_cap(double d)118 static zend_always_inline zend_long zend_dval_to_lval_cap(double d)
119 {
120 	if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) {
121 		return 0;
122 	} else if (!ZEND_DOUBLE_FITS_LONG(d)) {
123 		return (d > 0 ? ZEND_LONG_MAX : ZEND_LONG_MIN);
124 	}
125 	return (zend_long)d;
126 }
127 /* }}} */
128 
zend_is_long_compatible(double d,zend_long l)129 static zend_always_inline bool zend_is_long_compatible(double d, zend_long l) {
130 	return (double)l == d;
131 }
132 
133 ZEND_API void zend_incompatible_double_to_long_error(double d);
134 ZEND_API void zend_incompatible_string_to_long_error(const zend_string *s);
135 
zend_dval_to_lval_safe(double d)136 static zend_always_inline zend_long zend_dval_to_lval_safe(double d)
137 {
138 	zend_long l = zend_dval_to_lval(d);
139 	if (!zend_is_long_compatible(d, l)) {
140 		zend_incompatible_double_to_long_error(d);
141 	}
142 	return l;
143 }
144 
145 #define ZEND_IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
146 #define ZEND_IS_XDIGIT(c) (((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
147 
is_numeric_string_ex(const char * str,size_t length,zend_long * lval,double * dval,bool allow_errors,int * oflow_info,bool * trailing_data)148 static zend_always_inline uint8_t is_numeric_string_ex(const char *str, size_t length, zend_long *lval,
149 	double *dval, bool allow_errors, int *oflow_info, bool *trailing_data)
150 {
151 	if (*str > '9') {
152 		return 0;
153 	}
154 	return _is_numeric_string_ex(str, length, lval, dval, allow_errors, oflow_info, trailing_data);
155 }
156 
is_numeric_string(const char * str,size_t length,zend_long * lval,double * dval,bool allow_errors)157 static zend_always_inline uint8_t is_numeric_string(const char *str, size_t length, zend_long *lval, double *dval, bool allow_errors) {
158     return is_numeric_string_ex(str, length, lval, dval, allow_errors, NULL, NULL);
159 }
160 
161 ZEND_API uint8_t ZEND_FASTCALL is_numeric_str_function(const zend_string *str, zend_long *lval, double *dval);
162 
163 static zend_always_inline const char *
zend_memnstr(const char * haystack,const char * needle,size_t needle_len,const char * end)164 zend_memnstr(const char *haystack, const char *needle, size_t needle_len, const char *end)
165 {
166 	const char *p = haystack;
167 	size_t off_s;
168 
169 	ZEND_ASSERT(end >= p);
170 
171 	if (needle_len == 1) {
172 		return (const char *)memchr(p, *needle, (end-p));
173 	} else if (UNEXPECTED(needle_len == 0)) {
174 		return p;
175 	}
176 
177 	off_s = (size_t)(end - p);
178 
179 	if (needle_len > off_s) {
180 		return NULL;
181 	}
182 
183 	if (EXPECTED(off_s < 1024 || needle_len < 9)) {	/* glibc memchr is faster when needle is too short */
184 		const char ne = needle[needle_len-1];
185 		end -= needle_len;
186 
187 		while (p <= end) {
188 			if ((p = (const char *)memchr(p, *needle, (end-p+1)))) {
189 				if (ne == p[needle_len-1] && !memcmp(needle+1, p+1, needle_len-2)) {
190 					return p;
191 				}
192 			} else {
193 				return NULL;
194 			}
195 			p++;
196 		}
197 
198 		return NULL;
199 	} else {
200 		return zend_memnstr_ex(haystack, needle, needle_len, end);
201 	}
202 }
203 
zend_memrchr(const void * s,int c,size_t n)204 static zend_always_inline const void *zend_memrchr(const void *s, int c, size_t n)
205 {
206 #if defined(HAVE_MEMRCHR) && !defined(i386)
207 	/* On x86 memrchr() doesn't use SSE/AVX, so inlined version is faster */
208 	return (const void*)memrchr(s, c, n);
209 #else
210 	const unsigned char *e;
211 	if (0 == n) {
212 		return NULL;
213 	}
214 
215 	for (e = (const unsigned char *)s + n - 1; e >= (const unsigned char *)s; e--) {
216 		if (*e == (unsigned char)c) {
217 			return (const void *)e;
218 		}
219 	}
220 	return NULL;
221 #endif
222 }
223 
224 
225 static zend_always_inline const char *
zend_memnrstr(const char * haystack,const char * needle,size_t needle_len,const char * end)226 zend_memnrstr(const char *haystack, const char *needle, size_t needle_len, const char *end)
227 {
228     const char *p = end;
229     ptrdiff_t off_p;
230     size_t off_s;
231 
232 	if (needle_len == 0) {
233 		return p;
234 	}
235 
236     if (needle_len == 1) {
237         return (const char *)zend_memrchr(haystack, *needle, (p - haystack));
238     }
239 
240     off_p = end - haystack;
241     off_s = (off_p > 0) ? (size_t)off_p : 0;
242 
243     if (needle_len > off_s) {
244         return NULL;
245     }
246 
247 	if (EXPECTED(off_s < 1024 || needle_len < 3)) {
248 		const char ne = needle[needle_len-1];
249 		p -= needle_len;
250 
251 		do {
252 			p = (const char *)zend_memrchr(haystack, *needle, (p - haystack) + 1);
253 			if (!p) {
254 				return NULL;
255 			}
256 			if (ne == p[needle_len-1] && !memcmp(needle + 1, p + 1, needle_len - 2)) {
257 				return p;
258 			}
259 		} while (p-- >= haystack);
260 
261 		return NULL;
262 	} else {
263 		return zend_memnrstr_ex(haystack, needle, needle_len, end);
264 	}
265 }
266 
zend_strnlen(const char * s,size_t maxlen)267 static zend_always_inline size_t zend_strnlen(const char* s, size_t maxlen)
268 {
269 #if defined(HAVE_STRNLEN)
270 	return strnlen(s, maxlen);
271 #else
272 	const char *p = (const char *)memchr(s, '\0', maxlen);
273 	return p ? p-s : maxlen;
274 #endif
275 }
276 
277 ZEND_API zend_result ZEND_FASTCALL increment_function(zval *op1);
278 ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op2);
279 
280 ZEND_API void ZEND_FASTCALL convert_scalar_to_number(zval *op);
281 ZEND_API void ZEND_FASTCALL _convert_to_string(zval *op);
282 ZEND_API void ZEND_FASTCALL convert_to_long(zval *op);
283 ZEND_API void ZEND_FASTCALL convert_to_double(zval *op);
284 ZEND_API void ZEND_FASTCALL convert_to_null(zval *op);
285 ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op);
286 ZEND_API void ZEND_FASTCALL convert_to_array(zval *op);
287 ZEND_API void ZEND_FASTCALL convert_to_object(zval *op);
288 
289 ZEND_API zend_long    ZEND_FASTCALL zval_get_long_func(const zval *op, bool is_strict);
290 ZEND_API zend_long    ZEND_FASTCALL zval_try_get_long(const zval *op, bool *failed);
291 ZEND_API double       ZEND_FASTCALL zval_get_double_func(const zval *op);
292 ZEND_API zend_string* ZEND_FASTCALL zval_get_string_func(zval *op);
293 ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_func(zval *op);
294 
zval_get_long(const zval * op)295 static zend_always_inline zend_long zval_get_long(const zval *op) {
296 	return EXPECTED(Z_TYPE_P(op) == IS_LONG) ? Z_LVAL_P(op) : zval_get_long_func(op, false);
297 }
zval_get_long_ex(const zval * op,bool is_strict)298 static zend_always_inline zend_long zval_get_long_ex(const zval *op, bool is_strict) {
299 	return EXPECTED(Z_TYPE_P(op) == IS_LONG) ? Z_LVAL_P(op) : zval_get_long_func(op, is_strict);
300 }
zval_get_double(const zval * op)301 static zend_always_inline double zval_get_double(const zval *op) {
302 	return EXPECTED(Z_TYPE_P(op) == IS_DOUBLE) ? Z_DVAL_P(op) : zval_get_double_func(op);
303 }
zval_get_string(zval * op)304 static zend_always_inline zend_string *zval_get_string(zval *op) {
305 	return EXPECTED(Z_TYPE_P(op) == IS_STRING) ? zend_string_copy(Z_STR_P(op)) : zval_get_string_func(op);
306 }
307 
zval_get_tmp_string(zval * op,zend_string ** tmp)308 static zend_always_inline zend_string *zval_get_tmp_string(zval *op, zend_string **tmp) {
309 	if (EXPECTED(Z_TYPE_P(op) == IS_STRING)) {
310 		*tmp = NULL;
311 		return Z_STR_P(op);
312 	} else {
313 		return *tmp = zval_get_string_func(op);
314 	}
315 }
zend_tmp_string_release(zend_string * tmp)316 static zend_always_inline void zend_tmp_string_release(zend_string *tmp) {
317 	if (UNEXPECTED(tmp)) {
318 		zend_string_release_ex(tmp, 0);
319 	}
320 }
321 
322 /* Like zval_get_string, but returns NULL if the conversion fails with an exception. */
zval_try_get_string(zval * op)323 static zend_always_inline zend_string *zval_try_get_string(zval *op) {
324 	if (EXPECTED(Z_TYPE_P(op) == IS_STRING)) {
325 		zend_string *ret = zend_string_copy(Z_STR_P(op));
326 		ZEND_ASSUME(ret != NULL);
327 		return ret;
328 	} else {
329 		return zval_try_get_string_func(op);
330 	}
331 }
332 
333 /* Like zval_get_tmp_string, but returns NULL if the conversion fails with an exception. */
zval_try_get_tmp_string(zval * op,zend_string ** tmp)334 static zend_always_inline zend_string *zval_try_get_tmp_string(zval *op, zend_string **tmp) {
335 	if (EXPECTED(Z_TYPE_P(op) == IS_STRING)) {
336 		zend_string *ret = Z_STR_P(op);
337 		*tmp = NULL;
338 		ZEND_ASSUME(ret != NULL);
339 		return ret;
340 	} else {
341 		return *tmp = zval_try_get_string_func(op);
342 	}
343 }
344 
345 /* Like convert_to_string(), but returns whether the conversion succeeded and does not modify the
346  * zval in-place if it fails. */
347 ZEND_API bool ZEND_FASTCALL _try_convert_to_string(zval *op);
try_convert_to_string(zval * op)348 static zend_always_inline bool try_convert_to_string(zval *op) {
349 	if (Z_TYPE_P(op) == IS_STRING) {
350 		return 1;
351 	}
352 	return _try_convert_to_string(op);
353 }
354 
355 /* Compatibility macros for 7.2 and below */
356 #define _zval_get_long(op) zval_get_long(op)
357 #define _zval_get_double(op) zval_get_double(op)
358 #define _zval_get_string(op) zval_get_string(op)
359 #define _zval_get_long_func(op) zval_get_long_func(op)
360 #define _zval_get_double_func(op) zval_get_double_func(op)
361 #define _zval_get_string_func(op) zval_get_string_func(op)
362 
363 #define convert_to_string(op) if (Z_TYPE_P(op) != IS_STRING) { _convert_to_string((op)); }
364 
365 
366 ZEND_API int ZEND_FASTCALL zend_is_true(const zval *op);
367 ZEND_API bool ZEND_FASTCALL zend_object_is_true(const zval *op);
368 
369 #define zval_is_true(op) \
370 	zend_is_true(op)
371 
i_zend_is_true(const zval * op)372 static zend_always_inline bool i_zend_is_true(const zval *op)
373 {
374 	bool result = 0;
375 
376 again:
377 	switch (Z_TYPE_P(op)) {
378 		case IS_TRUE:
379 			result = 1;
380 			break;
381 		case IS_LONG:
382 			if (Z_LVAL_P(op)) {
383 				result = 1;
384 			}
385 			break;
386 		case IS_DOUBLE:
387 			if (Z_DVAL_P(op)) {
388 				result = 1;
389 			}
390 			break;
391 		case IS_STRING:
392 			if (Z_STRLEN_P(op) > 1 || (Z_STRLEN_P(op) && Z_STRVAL_P(op)[0] != '0')) {
393 				result = 1;
394 			}
395 			break;
396 		case IS_ARRAY:
397 			if (zend_hash_num_elements(Z_ARRVAL_P(op))) {
398 				result = 1;
399 			}
400 			break;
401 		case IS_OBJECT:
402 			if (EXPECTED(Z_OBJ_HT_P(op)->cast_object == zend_std_cast_object_tostring)) {
403 				result = 1;
404 			} else {
405 				result = zend_object_is_true(op);
406 			}
407 			break;
408 		case IS_RESOURCE:
409 			if (EXPECTED(Z_RES_HANDLE_P(op))) {
410 				result = 1;
411 			}
412 			break;
413 		case IS_REFERENCE:
414 			op = Z_REFVAL_P(op);
415 			goto again;
416 			break;
417 		default:
418 			break;
419 	}
420 	return result;
421 }
422 
423 /* Indicate that two values cannot be compared. This value should be returned for both orderings
424  * of the operands. This implies that all of ==, <, <= and >, >= will return false, because we
425  * canonicalize >/>= to </<= with swapped operands. */
426 // TODO: Use a different value to allow an actual distinction here.
427 #define ZEND_UNCOMPARABLE 1
428 
429 ZEND_API int ZEND_FASTCALL zend_compare(zval *op1, zval *op2);
430 
431 ZEND_API zend_result ZEND_FASTCALL compare_function(zval *result, zval *op1, zval *op2);
432 
433 ZEND_API int ZEND_FASTCALL numeric_compare_function(zval *op1, zval *op2);
434 ZEND_API int ZEND_FASTCALL string_compare_function_ex(zval *op1, zval *op2, bool case_insensitive);
435 ZEND_API int ZEND_FASTCALL string_compare_function(zval *op1, zval *op2);
436 ZEND_API int ZEND_FASTCALL string_case_compare_function(zval *op1, zval *op2);
437 ZEND_API int ZEND_FASTCALL string_locale_compare_function(zval *op1, zval *op2);
438 
439 ZEND_API extern const unsigned char zend_tolower_map[256];
440 ZEND_API extern const unsigned char zend_toupper_map[256];
441 
442 #define zend_tolower_ascii(c) (zend_tolower_map[(unsigned char)(c)])
443 #define zend_toupper_ascii(c) (zend_toupper_map[(unsigned char)(c)])
444 
445 ZEND_API void         ZEND_FASTCALL zend_str_tolower(char *str, size_t length);
446 ZEND_API void         ZEND_FASTCALL zend_str_toupper(char *str, size_t length);
447 ZEND_API char*        ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length);
448 ZEND_API char*        ZEND_FASTCALL zend_str_toupper_copy(char *dest, const char *source, size_t length);
449 ZEND_API char*        ZEND_FASTCALL zend_str_tolower_dup(const char *source, size_t length);
450 ZEND_API char*        ZEND_FASTCALL zend_str_toupper_dup(const char *source, size_t length);
451 ZEND_API char*        ZEND_FASTCALL zend_str_tolower_dup_ex(const char *source, size_t length);
452 ZEND_API char*        ZEND_FASTCALL zend_str_toupper_dup_ex(const char *source, size_t length);
453 ZEND_API zend_string* ZEND_FASTCALL zend_string_tolower_ex(zend_string *str, bool persistent);
454 ZEND_API zend_string* ZEND_FASTCALL zend_string_toupper_ex(zend_string *str, bool persistent);
455 
zend_string_tolower(zend_string * str)456 static zend_always_inline zend_string* zend_string_tolower(zend_string *str) {
457 	return zend_string_tolower_ex(str, false);
458 }
zend_string_toupper(zend_string * str)459 static zend_always_inline zend_string* zend_string_toupper(zend_string *str) {
460 	return zend_string_toupper_ex(str, false);
461 }
462 
463 ZEND_API int ZEND_FASTCALL zend_binary_zval_strcmp(zval *s1, zval *s2);
464 ZEND_API int ZEND_FASTCALL zend_binary_zval_strncmp(zval *s1, zval *s2, zval *s3);
465 ZEND_API int ZEND_FASTCALL zend_binary_strcmp(const char *s1, size_t len1, const char *s2, size_t len2);
466 ZEND_API int ZEND_FASTCALL zend_binary_strncmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length);
467 ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp(const char *s1, size_t len1, const char *s2, size_t len2);
468 ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length);
469 ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2);
470 ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2, size_t length);
471 
472 ZEND_API bool ZEND_FASTCALL zendi_smart_streq(zend_string *s1, zend_string *s2);
473 ZEND_API int ZEND_FASTCALL zendi_smart_strcmp(zend_string *s1, zend_string *s2);
474 ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable *ht2);
475 ZEND_API int ZEND_FASTCALL zend_compare_arrays(zval *a1, zval *a2);
476 ZEND_API int ZEND_FASTCALL zend_compare_objects(zval *o1, zval *o2);
477 
478 /** Deprecatd in favor of ZEND_STRTOL() */
479 ZEND_ATTRIBUTE_DEPRECATED ZEND_API int ZEND_FASTCALL zend_atoi(const char *str, size_t str_len);
480 
481 /** Deprecatd in favor of ZEND_STRTOL() */
482 ZEND_ATTRIBUTE_DEPRECATED ZEND_API zend_long ZEND_FASTCALL zend_atol(const char *str, size_t str_len);
483 
484 #define convert_to_null_ex(zv) convert_to_null(zv)
485 #define convert_to_boolean_ex(zv) convert_to_boolean(zv)
486 #define convert_to_long_ex(zv) convert_to_long(zv)
487 #define convert_to_double_ex(zv) convert_to_double(zv)
488 #define convert_to_string_ex(zv) convert_to_string(zv)
489 #define convert_to_array_ex(zv) convert_to_array(zv)
490 #define convert_to_object_ex(zv) convert_to_object(zv)
491 #define convert_scalar_to_number_ex(zv) convert_scalar_to_number(zv)
492 
493 ZEND_API void zend_update_current_locale(void);
494 
495 ZEND_API void zend_reset_lc_ctype_locale(void);
496 
497 /* The offset in bytes between the value and type fields of a zval */
498 #define ZVAL_OFFSETOF_TYPE	\
499 	(offsetof(zval, u1.type_info) - offsetof(zval, value))
500 
501 #if defined(HAVE_ASM_GOTO) && !__has_feature(memory_sanitizer)
502 # define ZEND_USE_ASM_ARITHMETIC 1
503 #else
504 # define ZEND_USE_ASM_ARITHMETIC 0
505 #endif
506 
fast_long_increment_function(zval * op1)507 static zend_always_inline void fast_long_increment_function(zval *op1)
508 {
509 #if ZEND_USE_ASM_ARITHMETIC && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__)
510 	__asm__ goto(
511 		"addl $1,(%0)\n\t"
512 		"jo  %l1\n"
513 		:
514 		: "r"(&op1->value)
515 		: "cc", "memory"
516 		: overflow);
517 	return;
518 overflow: ZEND_ATTRIBUTE_COLD_LABEL
519 	ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
520 #elif ZEND_USE_ASM_ARITHMETIC && defined(__x86_64__)
521 	__asm__ goto(
522 		"addq $1,(%0)\n\t"
523 		"jo  %l1\n"
524 		:
525 		: "r"(&op1->value)
526 		: "cc", "memory"
527 		: overflow);
528 	return;
529 overflow: ZEND_ATTRIBUTE_COLD_LABEL
530 	ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
531 #elif ZEND_USE_ASM_ARITHMETIC && defined(__aarch64__)
532 	__asm__ goto (
533 		"ldr x5, [%0]\n\t"
534 		"adds x5, x5, 1\n\t"
535 		"bvs %l1\n"
536 		"str x5, [%0]"
537 		:
538 		: "r"(&op1->value)
539 		: "x5", "cc", "memory"
540 		: overflow);
541 	return;
542 overflow: ZEND_ATTRIBUTE_COLD_LABEL
543 	ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
544 #elif PHP_HAVE_BUILTIN_SADDL_OVERFLOW && SIZEOF_LONG == SIZEOF_ZEND_LONG
545 	long lresult;
546 	if (UNEXPECTED(__builtin_saddl_overflow(Z_LVAL_P(op1), 1, &lresult))) {
547 		/* switch to double */
548 		ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
549 	} else {
550 		Z_LVAL_P(op1) = lresult;
551 	}
552 #elif PHP_HAVE_BUILTIN_SADDLL_OVERFLOW && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG
553 	long long llresult;
554 	if (UNEXPECTED(__builtin_saddll_overflow(Z_LVAL_P(op1), 1, &llresult))) {
555 		/* switch to double */
556 		ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
557 	} else {
558 		Z_LVAL_P(op1) = llresult;
559 	}
560 #else
561 	if (UNEXPECTED(Z_LVAL_P(op1) == ZEND_LONG_MAX)) {
562 		/* switch to double */
563 		ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
564 	} else {
565 		Z_LVAL_P(op1)++;
566 	}
567 #endif
568 }
569 
fast_long_decrement_function(zval * op1)570 static zend_always_inline void fast_long_decrement_function(zval *op1)
571 {
572 #if ZEND_USE_ASM_ARITHMETIC && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__)
573 	__asm__ goto(
574 		"subl $1,(%0)\n\t"
575 		"jo  %l1\n"
576 		:
577 		: "r"(&op1->value)
578 		: "cc", "memory"
579 		: overflow);
580 	return;
581 overflow: ZEND_ATTRIBUTE_COLD_LABEL
582 	ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
583 #elif ZEND_USE_ASM_ARITHMETIC && defined(__x86_64__)
584 	__asm__ goto(
585 		"subq $1,(%0)\n\t"
586 		"jo  %l1\n"
587 		:
588 		: "r"(&op1->value)
589 		: "cc", "memory"
590 		: overflow);
591 	return;
592 overflow: ZEND_ATTRIBUTE_COLD_LABEL
593 	ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
594 #elif ZEND_USE_ASM_ARITHMETIC && defined(__aarch64__)
595 	__asm__ goto (
596 		"ldr x5, [%0]\n\t"
597 		"subs x5 ,x5, 1\n\t"
598 		"bvs %l1\n"
599 		"str x5, [%0]"
600 		:
601 		: "r"(&op1->value)
602 		: "x5", "cc", "memory"
603 		: overflow);
604 	return;
605 overflow: ZEND_ATTRIBUTE_COLD_LABEL
606 	ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
607 #elif PHP_HAVE_BUILTIN_SSUBL_OVERFLOW && SIZEOF_LONG == SIZEOF_ZEND_LONG
608 	long lresult;
609 	if (UNEXPECTED(__builtin_ssubl_overflow(Z_LVAL_P(op1), 1, &lresult))) {
610 		/* switch to double */
611 		ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
612 	} else {
613 		Z_LVAL_P(op1) = lresult;
614 	}
615 #elif PHP_HAVE_BUILTIN_SSUBLL_OVERFLOW && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG
616 	long long llresult;
617 	if (UNEXPECTED(__builtin_ssubll_overflow(Z_LVAL_P(op1), 1, &llresult))) {
618 		/* switch to double */
619 		ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
620 	} else {
621 		Z_LVAL_P(op1) = llresult;
622 	}
623 #else
624 	if (UNEXPECTED(Z_LVAL_P(op1) == ZEND_LONG_MIN)) {
625 		/* switch to double */
626 		ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
627 	} else {
628 		Z_LVAL_P(op1)--;
629 	}
630 #endif
631 }
632 
fast_long_add_function(zval * result,zval * op1,zval * op2)633 static zend_always_inline void fast_long_add_function(zval *result, zval *op1, zval *op2)
634 {
635 #if ZEND_USE_ASM_ARITHMETIC && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__)
636 	__asm__ goto(
637 		"movl	(%1), %%eax\n\t"
638 		"addl   (%2), %%eax\n\t"
639 		"jo     %l5\n\t"
640 		"movl   %%eax, (%0)\n\t"
641 		"movl   %3, %c4(%0)\n"
642 		:
643 		: "r"(&result->value),
644 		  "r"(&op1->value),
645 		  "r"(&op2->value),
646 		  "n"(IS_LONG),
647 		  "n"(ZVAL_OFFSETOF_TYPE)
648 		: "eax","cc", "memory"
649 		: overflow);
650 	return;
651 overflow: ZEND_ATTRIBUTE_COLD_LABEL
652 	ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
653 #elif ZEND_USE_ASM_ARITHMETIC && defined(__x86_64__)
654 	__asm__ goto(
655 		"movq	(%1), %%rax\n\t"
656 		"addq   (%2), %%rax\n\t"
657 		"jo     %l5\n\t"
658 		"movq   %%rax, (%0)\n\t"
659 		"movl   %3, %c4(%0)\n"
660 		:
661 		: "r"(&result->value),
662 		  "r"(&op1->value),
663 		  "r"(&op2->value),
664 		  "n"(IS_LONG),
665 		  "n"(ZVAL_OFFSETOF_TYPE)
666 		: "rax","cc", "memory"
667 		: overflow);
668 	return;
669 overflow: ZEND_ATTRIBUTE_COLD_LABEL
670 	ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
671 #elif ZEND_USE_ASM_ARITHMETIC && defined(__aarch64__)
672 	__asm__ goto(
673 		"ldr    x5, [%1]\n\t"
674 		"ldr    x6, [%2]\n\t"
675 		"adds	x5, x5, x6\n\t"
676 		"bvs	%l5\n\t"
677 		"mov	w6, %3\n\t"
678 		"str	x5, [%0]\n\t"
679 		"str	w6, [%0, %c4]\n"
680 		:
681 		: "r"(&result->value),
682 		  "r"(&op1->value),
683 		  "r"(&op2->value),
684 		  "n"(IS_LONG),
685 		  "n"(ZVAL_OFFSETOF_TYPE)
686 		: "x5", "x6", "cc", "memory"
687 		: overflow);
688 	return;
689 overflow: ZEND_ATTRIBUTE_COLD_LABEL
690 	ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
691 #elif PHP_HAVE_BUILTIN_SADDL_OVERFLOW && SIZEOF_LONG == SIZEOF_ZEND_LONG
692 	long lresult;
693 	if (UNEXPECTED(__builtin_saddl_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &lresult))) {
694 		ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
695 	} else {
696 		ZVAL_LONG(result, lresult);
697 	}
698 #elif PHP_HAVE_BUILTIN_SADDLL_OVERFLOW && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG
699 	long long llresult;
700 	if (UNEXPECTED(__builtin_saddll_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &llresult))) {
701 		ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
702 	} else {
703 		ZVAL_LONG(result, llresult);
704 	}
705 #else
706 	/*
707 	 * 'result' may alias with op1 or op2, so we need to
708 	 * ensure that 'result' is not updated until after we
709 	 * have read the values of op1 and op2.
710 	 */
711 
712 	if (UNEXPECTED((Z_LVAL_P(op1) & LONG_SIGN_MASK) == (Z_LVAL_P(op2) & LONG_SIGN_MASK)
713 		&& (Z_LVAL_P(op1) & LONG_SIGN_MASK) != ((Z_LVAL_P(op1) + Z_LVAL_P(op2)) & LONG_SIGN_MASK))) {
714 		ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
715 	} else {
716 		ZVAL_LONG(result, Z_LVAL_P(op1) + Z_LVAL_P(op2));
717 	}
718 #endif
719 }
720 
fast_long_sub_function(zval * result,zval * op1,zval * op2)721 static zend_always_inline void fast_long_sub_function(zval *result, zval *op1, zval *op2)
722 {
723 #if ZEND_USE_ASM_ARITHMETIC && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__)
724 	__asm__ goto(
725 		"movl	(%1), %%eax\n\t"
726 		"subl   (%2), %%eax\n\t"
727 		"jo     %l5\n\t"
728 		"movl   %%eax, (%0)\n\t"
729 		"movl   %3, %c4(%0)\n"
730 		:
731 		: "r"(&result->value),
732 		  "r"(&op1->value),
733 		  "r"(&op2->value),
734 		  "n"(IS_LONG),
735 		  "n"(ZVAL_OFFSETOF_TYPE)
736 		: "eax","cc", "memory"
737 		: overflow);
738 	return;
739 overflow: ZEND_ATTRIBUTE_COLD_LABEL
740 	ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
741 #elif ZEND_USE_ASM_ARITHMETIC && defined(__x86_64__)
742 	__asm__ goto(
743 		"movq	(%1), %%rax\n\t"
744 		"subq   (%2), %%rax\n\t"
745 		"jo     %l5\n\t"
746 		"movq   %%rax, (%0)\n\t"
747 		"movl   %3, %c4(%0)\n"
748 		:
749 		: "r"(&result->value),
750 		  "r"(&op1->value),
751 		  "r"(&op2->value),
752 		  "n"(IS_LONG),
753 		  "n"(ZVAL_OFFSETOF_TYPE)
754 		: "rax","cc", "memory"
755 		: overflow);
756 	return;
757 overflow: ZEND_ATTRIBUTE_COLD_LABEL
758 	ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
759 #elif ZEND_USE_ASM_ARITHMETIC && defined(__aarch64__)
760 	__asm__ goto(
761 		"ldr    x5, [%1]\n\t"
762 		"ldr    x6, [%2]\n\t"
763 		"subs	x5, x5, x6\n\t"
764 		"bvs	%l5\n\t"
765 		"mov	w6, %3\n\t"
766 		"str	x5, [%0]\n\t"
767 		"str	w6, [%0, %c4]\n"
768 		:
769 		: "r"(&result->value),
770 		  "r"(&op1->value),
771 		  "r"(&op2->value),
772 		  "n"(IS_LONG),
773 		  "n"(ZVAL_OFFSETOF_TYPE)
774 		: "x5", "x6", "cc", "memory"
775 		: overflow);
776 	return;
777 overflow: ZEND_ATTRIBUTE_COLD_LABEL
778 	ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
779 #elif PHP_HAVE_BUILTIN_SSUBL_OVERFLOW && SIZEOF_LONG == SIZEOF_ZEND_LONG
780 	long lresult;
781 	if (UNEXPECTED(__builtin_ssubl_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &lresult))) {
782 		ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
783 	} else {
784 		ZVAL_LONG(result, lresult);
785 	}
786 #elif PHP_HAVE_BUILTIN_SSUBLL_OVERFLOW && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG
787 	long long llresult;
788 	if (UNEXPECTED(__builtin_ssubll_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &llresult))) {
789 		ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
790 	} else {
791 		ZVAL_LONG(result, llresult);
792 	}
793 #else
794 	ZVAL_LONG(result, Z_LVAL_P(op1) - Z_LVAL_P(op2));
795 
796 	if (UNEXPECTED((Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(op2) & LONG_SIGN_MASK)
797 		&& (Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(result) & LONG_SIGN_MASK))) {
798 		ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
799 	}
800 #endif
801 }
802 
zend_fast_equal_strings(zend_string * s1,zend_string * s2)803 static zend_always_inline bool zend_fast_equal_strings(zend_string *s1, zend_string *s2)
804 {
805 	if (s1 == s2) {
806 		return 1;
807 	} else if (ZSTR_VAL(s1)[0] > '9' || ZSTR_VAL(s2)[0] > '9') {
808 		return zend_string_equal_content(s1, s2);
809 	} else {
810 		return zendi_smart_streq(s1, s2);
811 	}
812 }
813 
fast_equal_check_function(zval * op1,zval * op2)814 static zend_always_inline bool fast_equal_check_function(zval *op1, zval *op2)
815 {
816 	if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
817 		if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
818 			return Z_LVAL_P(op1) == Z_LVAL_P(op2);
819 		} else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
820 			return ((double)Z_LVAL_P(op1)) == Z_DVAL_P(op2);
821 		}
822 	} else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
823 		if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
824 			return Z_DVAL_P(op1) == Z_DVAL_P(op2);
825 		} else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
826 			return Z_DVAL_P(op1) == ((double)Z_LVAL_P(op2));
827 		}
828 	} else if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) {
829 		if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
830 			return zend_fast_equal_strings(Z_STR_P(op1), Z_STR_P(op2));
831 		}
832 	}
833 	return zend_compare(op1, op2) == 0;
834 }
835 
fast_equal_check_long(zval * op1,zval * op2)836 static zend_always_inline bool fast_equal_check_long(zval *op1, zval *op2)
837 {
838 	if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
839 		return Z_LVAL_P(op1) == Z_LVAL_P(op2);
840 	}
841 	return zend_compare(op1, op2) == 0;
842 }
843 
fast_equal_check_string(zval * op1,zval * op2)844 static zend_always_inline bool fast_equal_check_string(zval *op1, zval *op2)
845 {
846 	if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
847 		return zend_fast_equal_strings(Z_STR_P(op1), Z_STR_P(op2));
848 	}
849 	return zend_compare(op1, op2) == 0;
850 }
851 
fast_is_identical_function(zval * op1,zval * op2)852 static zend_always_inline bool fast_is_identical_function(zval *op1, zval *op2)
853 {
854 	if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
855 		return 0;
856 	} else if (Z_TYPE_P(op1) <= IS_TRUE) {
857 		return 1;
858 	}
859 	return zend_is_identical(op1, op2);
860 }
861 
fast_is_not_identical_function(zval * op1,zval * op2)862 static zend_always_inline bool fast_is_not_identical_function(zval *op1, zval *op2)
863 {
864 	if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
865 		return 1;
866 	} else if (Z_TYPE_P(op1) <= IS_TRUE) {
867 		return 0;
868 	}
869 	return !zend_is_identical(op1, op2);
870 }
871 
872 /* buf points to the END of the buffer */
zend_print_ulong_to_buf(char * buf,zend_ulong num)873 static zend_always_inline char *zend_print_ulong_to_buf(char *buf, zend_ulong num) {
874 	*buf = '\0';
875 	do {
876 		*--buf = (char) (num % 10) + '0';
877 		num /= 10;
878 	} while (num > 0);
879 	return buf;
880 }
881 
882 /* buf points to the END of the buffer */
zend_print_long_to_buf(char * buf,zend_long num)883 static zend_always_inline char *zend_print_long_to_buf(char *buf, zend_long num) {
884 	if (num < 0) {
885 	    char *result = zend_print_ulong_to_buf(buf, ~((zend_ulong) num) + 1);
886 	    *--result = '-';
887 		return result;
888 	} else {
889 	    return zend_print_ulong_to_buf(buf, num);
890 	}
891 }
892 
893 ZEND_API zend_string* ZEND_FASTCALL zend_long_to_str(zend_long num);
894 ZEND_API zend_string* ZEND_FASTCALL zend_ulong_to_str(zend_ulong num);
895 ZEND_API zend_string* ZEND_FASTCALL zend_u64_to_str(uint64_t num);
896 ZEND_API zend_string* ZEND_FASTCALL zend_i64_to_str(int64_t num);
897 ZEND_API zend_string* ZEND_FASTCALL zend_double_to_str(double num);
898 
zend_unwrap_reference(zval * op)899 static zend_always_inline void zend_unwrap_reference(zval *op) /* {{{ */
900 {
901 	if (Z_REFCOUNT_P(op) == 1) {
902 		ZVAL_UNREF(op);
903 	} else {
904 		Z_DELREF_P(op);
905 		ZVAL_COPY(op, Z_REFVAL_P(op));
906 	}
907 }
908 /* }}} */
909 
zend_strnieq(const char * ptr1,const char * ptr2,size_t num)910 static zend_always_inline bool zend_strnieq(const char *ptr1, const char *ptr2, size_t num)
911 {
912 	const char *end = ptr1 + num;
913 	while (ptr1 < end) {
914 		if (zend_tolower_ascii(*ptr1++) != zend_tolower_ascii(*ptr2++)) {
915 			return 0;
916 		}
917 	}
918 	return 1;
919 }
920 
921 static zend_always_inline const char *
zend_memnistr(const char * haystack,const char * needle,size_t needle_len,const char * end)922 zend_memnistr(const char *haystack, const char *needle, size_t needle_len, const char *end)
923 {
924 	ZEND_ASSERT(end >= haystack);
925 
926 	if (UNEXPECTED(needle_len == 0)) {
927 		return haystack;
928 	}
929 
930 	if (UNEXPECTED(needle_len > (size_t)(end - haystack))) {
931 		return NULL;
932 	}
933 
934 	const char first_lower = zend_tolower_ascii(*needle);
935 	const char first_upper = zend_toupper_ascii(*needle);
936 	const char *p_lower = (const char *)memchr(haystack, first_lower, end - haystack);
937 	const char *p_upper = NULL;
938 	if (first_lower != first_upper) {
939 		// If the needle length is 1 we don't need to look beyond p_lower as it is a guaranteed match
940 		size_t upper_search_length = needle_len == 1 && p_lower != NULL ? p_lower - haystack : end - haystack;
941 		p_upper = (const char *)memchr(haystack, first_upper, upper_search_length);
942 	}
943 	const char *p = !p_upper || (p_lower && p_lower < p_upper) ? p_lower : p_upper;
944 
945 	if (needle_len == 1) {
946 		return p;
947 	}
948 
949 	const char needle_end_lower = zend_tolower_ascii(needle[needle_len - 1]);
950 	const char needle_end_upper = zend_toupper_ascii(needle[needle_len - 1]);
951 	end -= needle_len;
952 
953 	while (p && p <= end) {
954 		if (needle_end_lower == p[needle_len - 1] || needle_end_upper == p[needle_len - 1]) {
955 			if (zend_strnieq(needle + 1, p + 1, needle_len - 2)) {
956 				return p;
957 			}
958 		}
959 		if (p_lower == p) {
960 			p_lower = (const char *)memchr(p_lower + 1, first_lower, end - p_lower);
961 		}
962 		if (p_upper == p) {
963 			p_upper = (const char *)memchr(p_upper + 1, first_upper, end - p_upper);
964 		}
965 		p = !p_upper || (p_lower && p_lower < p_upper) ? p_lower : p_upper;
966 	}
967 
968 	return NULL;
969 }
970 
971 
972 END_EXTERN_C()
973 
974 #endif
975