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
zend_mempcpy(void * dest,const void * src,size_t n)277 static zend_always_inline void *zend_mempcpy(void *dest, const void *src, size_t n)
278 {
279 #if defined(HAVE_MEMPCPY)
280 return mempcpy(dest, src, n);
281 #else
282 return (char *)memcpy(dest, src, n) + n;
283 #endif
284 }
285
286 ZEND_API zend_result ZEND_FASTCALL increment_function(zval *op1);
287 ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op2);
288
289 ZEND_API void ZEND_FASTCALL convert_scalar_to_number(zval *op);
290 ZEND_API void ZEND_FASTCALL _convert_to_string(zval *op);
291 ZEND_API void ZEND_FASTCALL convert_to_long(zval *op);
292 ZEND_API void ZEND_FASTCALL convert_to_double(zval *op);
293 ZEND_API void ZEND_FASTCALL convert_to_null(zval *op);
294 ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op);
295 ZEND_API void ZEND_FASTCALL convert_to_array(zval *op);
296 ZEND_API void ZEND_FASTCALL convert_to_object(zval *op);
297
298 ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(const zval *op, bool is_strict);
299 ZEND_API zend_long ZEND_FASTCALL zval_try_get_long(const zval *op, bool *failed);
300 ZEND_API double ZEND_FASTCALL zval_get_double_func(const zval *op);
301 ZEND_API zend_string* ZEND_FASTCALL zval_get_string_func(zval *op);
302 ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_func(zval *op);
303
zval_get_long(const zval * op)304 static zend_always_inline zend_long zval_get_long(const zval *op) {
305 return EXPECTED(Z_TYPE_P(op) == IS_LONG) ? Z_LVAL_P(op) : zval_get_long_func(op, false);
306 }
zval_get_long_ex(const zval * op,bool is_strict)307 static zend_always_inline zend_long zval_get_long_ex(const zval *op, bool is_strict) {
308 return EXPECTED(Z_TYPE_P(op) == IS_LONG) ? Z_LVAL_P(op) : zval_get_long_func(op, is_strict);
309 }
zval_get_double(const zval * op)310 static zend_always_inline double zval_get_double(const zval *op) {
311 return EXPECTED(Z_TYPE_P(op) == IS_DOUBLE) ? Z_DVAL_P(op) : zval_get_double_func(op);
312 }
zval_get_string(zval * op)313 static zend_always_inline zend_string *zval_get_string(zval *op) {
314 return EXPECTED(Z_TYPE_P(op) == IS_STRING) ? zend_string_copy(Z_STR_P(op)) : zval_get_string_func(op);
315 }
316
zval_get_tmp_string(zval * op,zend_string ** tmp)317 static zend_always_inline zend_string *zval_get_tmp_string(zval *op, zend_string **tmp) {
318 if (EXPECTED(Z_TYPE_P(op) == IS_STRING)) {
319 *tmp = NULL;
320 return Z_STR_P(op);
321 } else {
322 return *tmp = zval_get_string_func(op);
323 }
324 }
zend_tmp_string_release(zend_string * tmp)325 static zend_always_inline void zend_tmp_string_release(zend_string *tmp) {
326 if (UNEXPECTED(tmp)) {
327 zend_string_release_ex(tmp, 0);
328 }
329 }
330
331 /* Like zval_get_string, but returns NULL if the conversion fails with an exception. */
zval_try_get_string(zval * op)332 static zend_always_inline zend_string *zval_try_get_string(zval *op) {
333 if (EXPECTED(Z_TYPE_P(op) == IS_STRING)) {
334 zend_string *ret = zend_string_copy(Z_STR_P(op));
335 ZEND_ASSUME(ret != NULL);
336 return ret;
337 } else {
338 return zval_try_get_string_func(op);
339 }
340 }
341
342 /* 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)343 static zend_always_inline zend_string *zval_try_get_tmp_string(zval *op, zend_string **tmp) {
344 if (EXPECTED(Z_TYPE_P(op) == IS_STRING)) {
345 zend_string *ret = Z_STR_P(op);
346 *tmp = NULL;
347 ZEND_ASSUME(ret != NULL);
348 return ret;
349 } else {
350 return *tmp = zval_try_get_string_func(op);
351 }
352 }
353
354 /* Like convert_to_string(), but returns whether the conversion succeeded and does not modify the
355 * zval in-place if it fails. */
356 ZEND_API bool ZEND_FASTCALL _try_convert_to_string(zval *op);
try_convert_to_string(zval * op)357 static zend_always_inline bool try_convert_to_string(zval *op) {
358 if (Z_TYPE_P(op) == IS_STRING) {
359 return 1;
360 }
361 return _try_convert_to_string(op);
362 }
363
364 /* Compatibility macros for 7.2 and below */
365 #define _zval_get_long(op) zval_get_long(op)
366 #define _zval_get_double(op) zval_get_double(op)
367 #define _zval_get_string(op) zval_get_string(op)
368 #define _zval_get_long_func(op) zval_get_long_func(op)
369 #define _zval_get_double_func(op) zval_get_double_func(op)
370 #define _zval_get_string_func(op) zval_get_string_func(op)
371
372 #define convert_to_string(op) if (Z_TYPE_P(op) != IS_STRING) { _convert_to_string((op)); }
373
374
375 ZEND_API int ZEND_FASTCALL zend_is_true(const zval *op);
376 ZEND_API bool ZEND_FASTCALL zend_object_is_true(const zval *op);
377
378 #define zval_is_true(op) \
379 zend_is_true(op)
380
i_zend_is_true(const zval * op)381 static zend_always_inline bool i_zend_is_true(const zval *op)
382 {
383 bool result = 0;
384
385 again:
386 switch (Z_TYPE_P(op)) {
387 case IS_TRUE:
388 result = 1;
389 break;
390 case IS_LONG:
391 if (Z_LVAL_P(op)) {
392 result = 1;
393 }
394 break;
395 case IS_DOUBLE:
396 if (Z_DVAL_P(op)) {
397 result = 1;
398 }
399 break;
400 case IS_STRING:
401 if (Z_STRLEN_P(op) > 1 || (Z_STRLEN_P(op) && Z_STRVAL_P(op)[0] != '0')) {
402 result = 1;
403 }
404 break;
405 case IS_ARRAY:
406 if (zend_hash_num_elements(Z_ARRVAL_P(op))) {
407 result = 1;
408 }
409 break;
410 case IS_OBJECT:
411 if (EXPECTED(Z_OBJ_HT_P(op)->cast_object == zend_std_cast_object_tostring)) {
412 result = 1;
413 } else {
414 result = zend_object_is_true(op);
415 }
416 break;
417 case IS_RESOURCE:
418 if (EXPECTED(Z_RES_HANDLE_P(op))) {
419 result = 1;
420 }
421 break;
422 case IS_REFERENCE:
423 op = Z_REFVAL_P(op);
424 goto again;
425 break;
426 default:
427 break;
428 }
429 return result;
430 }
431
432 /* Indicate that two values cannot be compared. This value should be returned for both orderings
433 * of the operands. This implies that all of ==, <, <= and >, >= will return false, because we
434 * canonicalize >/>= to </<= with swapped operands. */
435 // TODO: Use a different value to allow an actual distinction here.
436 #define ZEND_UNCOMPARABLE 1
437
438 ZEND_API int ZEND_FASTCALL zend_compare(zval *op1, zval *op2);
439
440 ZEND_API zend_result ZEND_FASTCALL compare_function(zval *result, zval *op1, zval *op2);
441
442 ZEND_API int ZEND_FASTCALL numeric_compare_function(zval *op1, zval *op2);
443 ZEND_API int ZEND_FASTCALL string_compare_function_ex(zval *op1, zval *op2, bool case_insensitive);
444 ZEND_API int ZEND_FASTCALL string_compare_function(zval *op1, zval *op2);
445 ZEND_API int ZEND_FASTCALL string_case_compare_function(zval *op1, zval *op2);
446 ZEND_API int ZEND_FASTCALL string_locale_compare_function(zval *op1, zval *op2);
447
448 ZEND_API extern const unsigned char zend_tolower_map[256];
449 ZEND_API extern const unsigned char zend_toupper_map[256];
450
451 #define zend_tolower_ascii(c) (zend_tolower_map[(unsigned char)(c)])
452 #define zend_toupper_ascii(c) (zend_toupper_map[(unsigned char)(c)])
453
454 ZEND_API void ZEND_FASTCALL zend_str_tolower(char *str, size_t length);
455 ZEND_API void ZEND_FASTCALL zend_str_toupper(char *str, size_t length);
456 ZEND_API char* ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length);
457 ZEND_API char* ZEND_FASTCALL zend_str_toupper_copy(char *dest, const char *source, size_t length);
458 ZEND_API char* ZEND_FASTCALL zend_str_tolower_dup(const char *source, size_t length);
459 ZEND_API char* ZEND_FASTCALL zend_str_toupper_dup(const char *source, size_t length);
460 ZEND_API char* ZEND_FASTCALL zend_str_tolower_dup_ex(const char *source, size_t length);
461 ZEND_API char* ZEND_FASTCALL zend_str_toupper_dup_ex(const char *source, size_t length);
462 ZEND_API zend_string* ZEND_FASTCALL zend_string_tolower_ex(zend_string *str, bool persistent);
463 ZEND_API zend_string* ZEND_FASTCALL zend_string_toupper_ex(zend_string *str, bool persistent);
464
zend_string_tolower(zend_string * str)465 static zend_always_inline zend_string* zend_string_tolower(zend_string *str) {
466 return zend_string_tolower_ex(str, false);
467 }
zend_string_toupper(zend_string * str)468 static zend_always_inline zend_string* zend_string_toupper(zend_string *str) {
469 return zend_string_toupper_ex(str, false);
470 }
471
472 ZEND_API int ZEND_FASTCALL zend_binary_zval_strcmp(zval *s1, zval *s2);
473 ZEND_API int ZEND_FASTCALL zend_binary_zval_strncmp(zval *s1, zval *s2, zval *s3);
474 ZEND_API int ZEND_FASTCALL zend_binary_strcmp(const char *s1, size_t len1, const char *s2, size_t len2);
475 ZEND_API int ZEND_FASTCALL zend_binary_strncmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length);
476 ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp(const char *s1, size_t len1, const char *s2, size_t len2);
477 ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length);
478 ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2);
479 ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2, size_t length);
480
481 ZEND_API bool ZEND_FASTCALL zendi_smart_streq(zend_string *s1, zend_string *s2);
482 ZEND_API int ZEND_FASTCALL zendi_smart_strcmp(zend_string *s1, zend_string *s2);
483 ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable *ht2);
484 ZEND_API int ZEND_FASTCALL zend_compare_arrays(zval *a1, zval *a2);
485 ZEND_API int ZEND_FASTCALL zend_compare_objects(zval *o1, zval *o2);
486
487 /** Deprecatd in favor of ZEND_STRTOL() */
488 ZEND_ATTRIBUTE_DEPRECATED ZEND_API int ZEND_FASTCALL zend_atoi(const char *str, size_t str_len);
489
490 /** Deprecatd in favor of ZEND_STRTOL() */
491 ZEND_ATTRIBUTE_DEPRECATED ZEND_API zend_long ZEND_FASTCALL zend_atol(const char *str, size_t str_len);
492
493 #define convert_to_null_ex(zv) convert_to_null(zv)
494 #define convert_to_boolean_ex(zv) convert_to_boolean(zv)
495 #define convert_to_long_ex(zv) convert_to_long(zv)
496 #define convert_to_double_ex(zv) convert_to_double(zv)
497 #define convert_to_string_ex(zv) convert_to_string(zv)
498 #define convert_to_array_ex(zv) convert_to_array(zv)
499 #define convert_to_object_ex(zv) convert_to_object(zv)
500 #define convert_scalar_to_number_ex(zv) convert_scalar_to_number(zv)
501
502 ZEND_API void zend_update_current_locale(void);
503
504 ZEND_API void zend_reset_lc_ctype_locale(void);
505
506 /* The offset in bytes between the value and type fields of a zval */
507 #define ZVAL_OFFSETOF_TYPE \
508 (offsetof(zval, u1.type_info) - offsetof(zval, value))
509
510 #if defined(HAVE_ASM_GOTO) && !__has_feature(memory_sanitizer)
511 # define ZEND_USE_ASM_ARITHMETIC 1
512 #else
513 # define ZEND_USE_ASM_ARITHMETIC 0
514 #endif
515
fast_long_increment_function(zval * op1)516 static zend_always_inline void fast_long_increment_function(zval *op1)
517 {
518 #if ZEND_USE_ASM_ARITHMETIC && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__)
519 __asm__ goto(
520 "addl $1,(%0)\n\t"
521 "jo %l1\n"
522 :
523 : "r"(&op1->value)
524 : "cc", "memory"
525 : overflow);
526 return;
527 overflow: ZEND_ATTRIBUTE_COLD_LABEL
528 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
529 #elif ZEND_USE_ASM_ARITHMETIC && defined(__x86_64__)
530 __asm__ goto(
531 "addq $1,(%0)\n\t"
532 "jo %l1\n"
533 :
534 : "r"(&op1->value)
535 : "cc", "memory"
536 : overflow);
537 return;
538 overflow: ZEND_ATTRIBUTE_COLD_LABEL
539 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
540 #elif ZEND_USE_ASM_ARITHMETIC && defined(__aarch64__)
541 __asm__ goto (
542 "ldr x5, [%0]\n\t"
543 "adds x5, x5, 1\n\t"
544 "bvs %l1\n"
545 "str x5, [%0]"
546 :
547 : "r"(&op1->value)
548 : "x5", "cc", "memory"
549 : overflow);
550 return;
551 overflow: ZEND_ATTRIBUTE_COLD_LABEL
552 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
553 #elif PHP_HAVE_BUILTIN_SADDL_OVERFLOW && SIZEOF_LONG == SIZEOF_ZEND_LONG
554 long lresult;
555 if (UNEXPECTED(__builtin_saddl_overflow(Z_LVAL_P(op1), 1, &lresult))) {
556 /* switch to double */
557 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
558 } else {
559 Z_LVAL_P(op1) = lresult;
560 }
561 #elif PHP_HAVE_BUILTIN_SADDLL_OVERFLOW && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG
562 long long llresult;
563 if (UNEXPECTED(__builtin_saddll_overflow(Z_LVAL_P(op1), 1, &llresult))) {
564 /* switch to double */
565 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
566 } else {
567 Z_LVAL_P(op1) = llresult;
568 }
569 #else
570 if (UNEXPECTED(Z_LVAL_P(op1) == ZEND_LONG_MAX)) {
571 /* switch to double */
572 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
573 } else {
574 Z_LVAL_P(op1)++;
575 }
576 #endif
577 }
578
fast_long_decrement_function(zval * op1)579 static zend_always_inline void fast_long_decrement_function(zval *op1)
580 {
581 #if ZEND_USE_ASM_ARITHMETIC && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__)
582 __asm__ goto(
583 "subl $1,(%0)\n\t"
584 "jo %l1\n"
585 :
586 : "r"(&op1->value)
587 : "cc", "memory"
588 : overflow);
589 return;
590 overflow: ZEND_ATTRIBUTE_COLD_LABEL
591 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
592 #elif ZEND_USE_ASM_ARITHMETIC && defined(__x86_64__)
593 __asm__ goto(
594 "subq $1,(%0)\n\t"
595 "jo %l1\n"
596 :
597 : "r"(&op1->value)
598 : "cc", "memory"
599 : overflow);
600 return;
601 overflow: ZEND_ATTRIBUTE_COLD_LABEL
602 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
603 #elif ZEND_USE_ASM_ARITHMETIC && defined(__aarch64__)
604 __asm__ goto (
605 "ldr x5, [%0]\n\t"
606 "subs x5 ,x5, 1\n\t"
607 "bvs %l1\n"
608 "str x5, [%0]"
609 :
610 : "r"(&op1->value)
611 : "x5", "cc", "memory"
612 : overflow);
613 return;
614 overflow: ZEND_ATTRIBUTE_COLD_LABEL
615 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
616 #elif PHP_HAVE_BUILTIN_SSUBL_OVERFLOW && SIZEOF_LONG == SIZEOF_ZEND_LONG
617 long lresult;
618 if (UNEXPECTED(__builtin_ssubl_overflow(Z_LVAL_P(op1), 1, &lresult))) {
619 /* switch to double */
620 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
621 } else {
622 Z_LVAL_P(op1) = lresult;
623 }
624 #elif PHP_HAVE_BUILTIN_SSUBLL_OVERFLOW && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG
625 long long llresult;
626 if (UNEXPECTED(__builtin_ssubll_overflow(Z_LVAL_P(op1), 1, &llresult))) {
627 /* switch to double */
628 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
629 } else {
630 Z_LVAL_P(op1) = llresult;
631 }
632 #else
633 if (UNEXPECTED(Z_LVAL_P(op1) == ZEND_LONG_MIN)) {
634 /* switch to double */
635 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
636 } else {
637 Z_LVAL_P(op1)--;
638 }
639 #endif
640 }
641
fast_long_add_function(zval * result,zval * op1,zval * op2)642 static zend_always_inline void fast_long_add_function(zval *result, zval *op1, zval *op2)
643 {
644 #if ZEND_USE_ASM_ARITHMETIC && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__)
645 __asm__ goto(
646 "movl (%1), %%eax\n\t"
647 "addl (%2), %%eax\n\t"
648 "jo %l5\n\t"
649 "movl %%eax, (%0)\n\t"
650 "movl %3, %c4(%0)\n"
651 :
652 : "r"(&result->value),
653 "r"(&op1->value),
654 "r"(&op2->value),
655 "n"(IS_LONG),
656 "n"(ZVAL_OFFSETOF_TYPE)
657 : "eax","cc", "memory"
658 : overflow);
659 return;
660 overflow: ZEND_ATTRIBUTE_COLD_LABEL
661 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
662 #elif ZEND_USE_ASM_ARITHMETIC && defined(__x86_64__)
663 __asm__ goto(
664 "movq (%1), %%rax\n\t"
665 "addq (%2), %%rax\n\t"
666 "jo %l5\n\t"
667 "movq %%rax, (%0)\n\t"
668 "movl %3, %c4(%0)\n"
669 :
670 : "r"(&result->value),
671 "r"(&op1->value),
672 "r"(&op2->value),
673 "n"(IS_LONG),
674 "n"(ZVAL_OFFSETOF_TYPE)
675 : "rax","cc", "memory"
676 : overflow);
677 return;
678 overflow: ZEND_ATTRIBUTE_COLD_LABEL
679 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
680 #elif ZEND_USE_ASM_ARITHMETIC && defined(__aarch64__)
681 __asm__ goto(
682 "ldr x5, [%1]\n\t"
683 "ldr x6, [%2]\n\t"
684 "adds x5, x5, x6\n\t"
685 "bvs %l5\n\t"
686 "mov w6, %3\n\t"
687 "str x5, [%0]\n\t"
688 "str w6, [%0, %c4]\n"
689 :
690 : "r"(&result->value),
691 "r"(&op1->value),
692 "r"(&op2->value),
693 "n"(IS_LONG),
694 "n"(ZVAL_OFFSETOF_TYPE)
695 : "x5", "x6", "cc", "memory"
696 : overflow);
697 return;
698 overflow: ZEND_ATTRIBUTE_COLD_LABEL
699 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
700 #elif PHP_HAVE_BUILTIN_SADDL_OVERFLOW && SIZEOF_LONG == SIZEOF_ZEND_LONG
701 long lresult;
702 if (UNEXPECTED(__builtin_saddl_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &lresult))) {
703 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
704 } else {
705 ZVAL_LONG(result, lresult);
706 }
707 #elif PHP_HAVE_BUILTIN_SADDLL_OVERFLOW && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG
708 long long llresult;
709 if (UNEXPECTED(__builtin_saddll_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &llresult))) {
710 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
711 } else {
712 ZVAL_LONG(result, llresult);
713 }
714 #else
715 /*
716 * 'result' may alias with op1 or op2, so we need to
717 * ensure that 'result' is not updated until after we
718 * have read the values of op1 and op2.
719 */
720
721 if (UNEXPECTED((Z_LVAL_P(op1) & LONG_SIGN_MASK) == (Z_LVAL_P(op2) & LONG_SIGN_MASK)
722 && (Z_LVAL_P(op1) & LONG_SIGN_MASK) != ((Z_LVAL_P(op1) + Z_LVAL_P(op2)) & LONG_SIGN_MASK))) {
723 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
724 } else {
725 ZVAL_LONG(result, Z_LVAL_P(op1) + Z_LVAL_P(op2));
726 }
727 #endif
728 }
729
fast_long_sub_function(zval * result,zval * op1,zval * op2)730 static zend_always_inline void fast_long_sub_function(zval *result, zval *op1, zval *op2)
731 {
732 #if ZEND_USE_ASM_ARITHMETIC && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__)
733 __asm__ goto(
734 "movl (%1), %%eax\n\t"
735 "subl (%2), %%eax\n\t"
736 "jo %l5\n\t"
737 "movl %%eax, (%0)\n\t"
738 "movl %3, %c4(%0)\n"
739 :
740 : "r"(&result->value),
741 "r"(&op1->value),
742 "r"(&op2->value),
743 "n"(IS_LONG),
744 "n"(ZVAL_OFFSETOF_TYPE)
745 : "eax","cc", "memory"
746 : overflow);
747 return;
748 overflow: ZEND_ATTRIBUTE_COLD_LABEL
749 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
750 #elif ZEND_USE_ASM_ARITHMETIC && defined(__x86_64__)
751 __asm__ goto(
752 "movq (%1), %%rax\n\t"
753 "subq (%2), %%rax\n\t"
754 "jo %l5\n\t"
755 "movq %%rax, (%0)\n\t"
756 "movl %3, %c4(%0)\n"
757 :
758 : "r"(&result->value),
759 "r"(&op1->value),
760 "r"(&op2->value),
761 "n"(IS_LONG),
762 "n"(ZVAL_OFFSETOF_TYPE)
763 : "rax","cc", "memory"
764 : overflow);
765 return;
766 overflow: ZEND_ATTRIBUTE_COLD_LABEL
767 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
768 #elif ZEND_USE_ASM_ARITHMETIC && defined(__aarch64__)
769 __asm__ goto(
770 "ldr x5, [%1]\n\t"
771 "ldr x6, [%2]\n\t"
772 "subs x5, x5, x6\n\t"
773 "bvs %l5\n\t"
774 "mov w6, %3\n\t"
775 "str x5, [%0]\n\t"
776 "str w6, [%0, %c4]\n"
777 :
778 : "r"(&result->value),
779 "r"(&op1->value),
780 "r"(&op2->value),
781 "n"(IS_LONG),
782 "n"(ZVAL_OFFSETOF_TYPE)
783 : "x5", "x6", "cc", "memory"
784 : overflow);
785 return;
786 overflow: ZEND_ATTRIBUTE_COLD_LABEL
787 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
788 #elif PHP_HAVE_BUILTIN_SSUBL_OVERFLOW && SIZEOF_LONG == SIZEOF_ZEND_LONG
789 long lresult;
790 if (UNEXPECTED(__builtin_ssubl_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &lresult))) {
791 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
792 } else {
793 ZVAL_LONG(result, lresult);
794 }
795 #elif PHP_HAVE_BUILTIN_SSUBLL_OVERFLOW && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG
796 long long llresult;
797 if (UNEXPECTED(__builtin_ssubll_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &llresult))) {
798 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
799 } else {
800 ZVAL_LONG(result, llresult);
801 }
802 #else
803 ZVAL_LONG(result, Z_LVAL_P(op1) - Z_LVAL_P(op2));
804
805 if (UNEXPECTED((Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(op2) & LONG_SIGN_MASK)
806 && (Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(result) & LONG_SIGN_MASK))) {
807 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
808 }
809 #endif
810 }
811
zend_fast_equal_strings(zend_string * s1,zend_string * s2)812 static zend_always_inline bool zend_fast_equal_strings(zend_string *s1, zend_string *s2)
813 {
814 if (s1 == s2) {
815 return 1;
816 } else if (ZSTR_VAL(s1)[0] > '9' || ZSTR_VAL(s2)[0] > '9') {
817 return zend_string_equal_content(s1, s2);
818 } else {
819 return zendi_smart_streq(s1, s2);
820 }
821 }
822
fast_equal_check_function(zval * op1,zval * op2)823 static zend_always_inline bool fast_equal_check_function(zval *op1, zval *op2)
824 {
825 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
826 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
827 return Z_LVAL_P(op1) == Z_LVAL_P(op2);
828 } else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
829 return ((double)Z_LVAL_P(op1)) == Z_DVAL_P(op2);
830 }
831 } else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
832 if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
833 return Z_DVAL_P(op1) == Z_DVAL_P(op2);
834 } else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
835 return Z_DVAL_P(op1) == ((double)Z_LVAL_P(op2));
836 }
837 } else if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) {
838 if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
839 return zend_fast_equal_strings(Z_STR_P(op1), Z_STR_P(op2));
840 }
841 }
842 return zend_compare(op1, op2) == 0;
843 }
844
fast_equal_check_long(zval * op1,zval * op2)845 static zend_always_inline bool fast_equal_check_long(zval *op1, zval *op2)
846 {
847 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
848 return Z_LVAL_P(op1) == Z_LVAL_P(op2);
849 }
850 return zend_compare(op1, op2) == 0;
851 }
852
fast_equal_check_string(zval * op1,zval * op2)853 static zend_always_inline bool fast_equal_check_string(zval *op1, zval *op2)
854 {
855 if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
856 return zend_fast_equal_strings(Z_STR_P(op1), Z_STR_P(op2));
857 }
858 return zend_compare(op1, op2) == 0;
859 }
860
fast_is_identical_function(zval * op1,zval * op2)861 static zend_always_inline bool fast_is_identical_function(zval *op1, zval *op2)
862 {
863 if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
864 return 0;
865 } else if (Z_TYPE_P(op1) <= IS_TRUE) {
866 return 1;
867 }
868 return zend_is_identical(op1, op2);
869 }
870
fast_is_not_identical_function(zval * op1,zval * op2)871 static zend_always_inline bool fast_is_not_identical_function(zval *op1, zval *op2)
872 {
873 if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
874 return 1;
875 } else if (Z_TYPE_P(op1) <= IS_TRUE) {
876 return 0;
877 }
878 return !zend_is_identical(op1, op2);
879 }
880
881 /* buf points to the END of the buffer */
zend_print_ulong_to_buf(char * buf,zend_ulong num)882 static zend_always_inline char *zend_print_ulong_to_buf(char *buf, zend_ulong num) {
883 *buf = '\0';
884 do {
885 *--buf = (char) (num % 10) + '0';
886 num /= 10;
887 } while (num > 0);
888 return buf;
889 }
890
891 /* buf points to the END of the buffer */
zend_print_long_to_buf(char * buf,zend_long num)892 static zend_always_inline char *zend_print_long_to_buf(char *buf, zend_long num) {
893 if (num < 0) {
894 char *result = zend_print_ulong_to_buf(buf, ~((zend_ulong) num) + 1);
895 *--result = '-';
896 return result;
897 } else {
898 return zend_print_ulong_to_buf(buf, num);
899 }
900 }
901
902 ZEND_API zend_string* ZEND_FASTCALL zend_long_to_str(zend_long num);
903 ZEND_API zend_string* ZEND_FASTCALL zend_ulong_to_str(zend_ulong num);
904 ZEND_API zend_string* ZEND_FASTCALL zend_u64_to_str(uint64_t num);
905 ZEND_API zend_string* ZEND_FASTCALL zend_i64_to_str(int64_t num);
906 ZEND_API zend_string* ZEND_FASTCALL zend_double_to_str(double num);
907
zend_unwrap_reference(zval * op)908 static zend_always_inline void zend_unwrap_reference(zval *op) /* {{{ */
909 {
910 if (Z_REFCOUNT_P(op) == 1) {
911 ZVAL_UNREF(op);
912 } else {
913 Z_DELREF_P(op);
914 ZVAL_COPY(op, Z_REFVAL_P(op));
915 }
916 }
917 /* }}} */
918
zend_strnieq(const char * ptr1,const char * ptr2,size_t num)919 static zend_always_inline bool zend_strnieq(const char *ptr1, const char *ptr2, size_t num)
920 {
921 const char *end = ptr1 + num;
922 while (ptr1 < end) {
923 if (zend_tolower_ascii(*ptr1++) != zend_tolower_ascii(*ptr2++)) {
924 return 0;
925 }
926 }
927 return 1;
928 }
929
930 static zend_always_inline const char *
zend_memnistr(const char * haystack,const char * needle,size_t needle_len,const char * end)931 zend_memnistr(const char *haystack, const char *needle, size_t needle_len, const char *end)
932 {
933 ZEND_ASSERT(end >= haystack);
934
935 if (UNEXPECTED(needle_len == 0)) {
936 return haystack;
937 }
938
939 if (UNEXPECTED(needle_len > (size_t)(end - haystack))) {
940 return NULL;
941 }
942
943 const char first_lower = zend_tolower_ascii(*needle);
944 const char first_upper = zend_toupper_ascii(*needle);
945 const char *p_lower = (const char *)memchr(haystack, first_lower, end - haystack);
946 const char *p_upper = NULL;
947 if (first_lower != first_upper) {
948 // If the needle length is 1 we don't need to look beyond p_lower as it is a guaranteed match
949 size_t upper_search_length = needle_len == 1 && p_lower != NULL ? p_lower - haystack : end - haystack;
950 p_upper = (const char *)memchr(haystack, first_upper, upper_search_length);
951 }
952 const char *p = !p_upper || (p_lower && p_lower < p_upper) ? p_lower : p_upper;
953
954 if (needle_len == 1) {
955 return p;
956 }
957
958 const char needle_end_lower = zend_tolower_ascii(needle[needle_len - 1]);
959 const char needle_end_upper = zend_toupper_ascii(needle[needle_len - 1]);
960 end -= needle_len;
961
962 while (p && p <= end) {
963 if (needle_end_lower == p[needle_len - 1] || needle_end_upper == p[needle_len - 1]) {
964 if (zend_strnieq(needle + 1, p + 1, needle_len - 2)) {
965 return p;
966 }
967 }
968 if (p_lower == p) {
969 p_lower = (const char *)memchr(p_lower + 1, first_lower, end - p_lower);
970 }
971 if (p_upper == p) {
972 p_upper = (const char *)memchr(p_upper + 1, first_upper, end - p_upper);
973 }
974 p = !p_upper || (p_lower && p_lower < p_upper) ? p_lower : p_upper;
975 }
976
977 return NULL;
978 }
979
980
981 END_EXTERN_C()
982
983 #endif
984