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