/src/php-src/Zend/zend_operators.c
Line | Count | Source |
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 | | #include <ctype.h> |
22 | | |
23 | | #include "zend.h" |
24 | | #include "zend_operators.h" |
25 | | #include "zend_variables.h" |
26 | | #include "zend_globals.h" |
27 | | #include "zend_list.h" |
28 | | #include "zend_API.h" |
29 | | #include "zend_strtod.h" |
30 | | #include "zend_exceptions.h" |
31 | | #include "zend_closures.h" |
32 | | |
33 | | #include <locale.h> |
34 | | #ifdef HAVE_LANGINFO_H |
35 | | # include <langinfo.h> |
36 | | #endif |
37 | | |
38 | | #ifdef ZEND_INTRIN_AVX2_NATIVE |
39 | | #include <immintrin.h> |
40 | | #endif |
41 | | #ifdef __SSE2__ |
42 | | #include <emmintrin.h> |
43 | | #endif |
44 | | #if defined(__aarch64__) || defined(_M_ARM64) |
45 | | #include <arm_neon.h> |
46 | | #endif |
47 | | |
48 | | #if defined(ZEND_WIN32) && !defined(ZTS) && defined(_MSC_VER) |
49 | | /* This performance improvement of tolower() on Windows gives 10-18% on bench.php */ |
50 | | #define ZEND_USE_TOLOWER_L 1 |
51 | | #endif |
52 | | |
53 | | #ifdef ZEND_USE_TOLOWER_L |
54 | | static _locale_t current_locale = NULL; |
55 | | /* this is true global! may lead to strange effects on ZTS, but so may setlocale() */ |
56 | | #define zend_tolower(c) _tolower_l(c, current_locale) |
57 | | #else |
58 | 0 | #define zend_tolower(c) tolower(c) |
59 | | #endif |
60 | | |
61 | 1.17M | #define TYPE_PAIR(t1,t2) (((t1) << 4) | (t2)) |
62 | | |
63 | | #ifdef ZEND_INTRIN_AVX2_NATIVE |
64 | | #define HAVE_BLOCKCONV |
65 | | |
66 | | #define BLOCKCONV_INIT_RANGE(start, end) \ |
67 | | const __m256i blconv_offset = _mm256_set1_epi8((signed char)(SCHAR_MIN - start)); \ |
68 | | const __m256i blconv_threshold = _mm256_set1_epi8(SCHAR_MIN + (end - start) + 1); |
69 | | |
70 | | #define BLOCKCONV_STRIDE sizeof(__m256i) |
71 | | |
72 | | #define BLOCKCONV_INIT_DELTA(delta) \ |
73 | | const __m256i blconv_delta = _mm256_set1_epi8(delta); |
74 | | |
75 | | #define BLOCKCONV_LOAD(input) \ |
76 | | __m256i blconv_operand = _mm256_loadu_si256((__m256i*)(input)); \ |
77 | | __m256i blconv_mask = _mm256_cmpgt_epi8(blconv_threshold, _mm256_add_epi8(blconv_operand, blconv_offset)); |
78 | | |
79 | | #define BLOCKCONV_FOUND() _mm256_movemask_epi8(blconv_mask) |
80 | | |
81 | | #define BLOCKCONV_STORE(dest) \ |
82 | | __m256i blconv_add = _mm256_and_si256(blconv_mask, blconv_delta); \ |
83 | | __m256i blconv_result = _mm256_add_epi8(blconv_operand, blconv_add); \ |
84 | | _mm256_storeu_si256((__m256i*)(dest), blconv_result); |
85 | | |
86 | | #elif __SSE2__ |
87 | | #define HAVE_BLOCKCONV |
88 | | |
89 | | /* Common code for SSE2 accelerated character case conversion */ |
90 | | |
91 | | #define BLOCKCONV_INIT_RANGE(start, end) \ |
92 | 6.00M | const __m128i blconv_offset = _mm_set1_epi8((signed char)(SCHAR_MIN - start)); \ |
93 | 6.00M | const __m128i blconv_threshold = _mm_set1_epi8(SCHAR_MIN + (end - start) + 1); |
94 | | |
95 | 48.7M | #define BLOCKCONV_STRIDE sizeof(__m128i) |
96 | | |
97 | | #define BLOCKCONV_INIT_DELTA(delta) \ |
98 | 3.05M | const __m128i blconv_delta = _mm_set1_epi8(delta); |
99 | | |
100 | | #define BLOCKCONV_LOAD(input) \ |
101 | 15.1M | __m128i blconv_operand = _mm_loadu_si128((__m128i*)(input)); \ |
102 | 15.1M | __m128i blconv_mask = _mm_cmplt_epi8(_mm_add_epi8(blconv_operand, blconv_offset), blconv_threshold); |
103 | | |
104 | 6.58M | #define BLOCKCONV_FOUND() _mm_movemask_epi8(blconv_mask) |
105 | | |
106 | | #define BLOCKCONV_STORE(dest) \ |
107 | 9.97M | __m128i blconv_add = _mm_and_si128(blconv_mask, blconv_delta); \ |
108 | 9.97M | __m128i blconv_result = _mm_add_epi8(blconv_operand, blconv_add); \ |
109 | 9.97M | _mm_storeu_si128((__m128i *)(dest), blconv_result); |
110 | | |
111 | | #elif defined(__aarch64__) || defined(_M_ARM64) |
112 | | #define HAVE_BLOCKCONV |
113 | | |
114 | | #define BLOCKCONV_INIT_RANGE(start, end) \ |
115 | | const int8x16_t blconv_offset = vdupq_n_s8((signed char)(SCHAR_MIN - start)); \ |
116 | | const int8x16_t blconv_threshold = vdupq_n_s8(SCHAR_MIN + (end - start) + 1); |
117 | | |
118 | | #define BLOCKCONV_STRIDE sizeof(int8x16_t) |
119 | | |
120 | | #define BLOCKCONV_INIT_DELTA(delta) \ |
121 | | const int8x16_t blconv_delta = vdupq_n_s8(delta); |
122 | | |
123 | | #define BLOCKCONV_LOAD(input) \ |
124 | | int8x16_t blconv_operand = vld1q_s8((const int8_t*)(input)); \ |
125 | | uint8x16_t blconv_mask = vcltq_s8(vreinterpretq_s8_u8(vaddq_u8(vreinterpretq_u8_s8(blconv_operand), vreinterpretq_u8_s8(blconv_offset))), blconv_threshold); |
126 | | |
127 | | #define BLOCKCONV_FOUND() vmaxvq_u8(blconv_mask) |
128 | | |
129 | | #define BLOCKCONV_STORE(dest) \ |
130 | | int8x16_t blconv_add = vandq_s8(vreinterpretq_s8_u8(blconv_mask), blconv_delta); \ |
131 | | int8x16_t blconv_result = vaddq_s8(blconv_operand, blconv_add); \ |
132 | | vst1q_s8((int8_t *)(dest), blconv_result); |
133 | | |
134 | | #endif /* defined(__aarch64__) || defined(_M_ARM64) */ |
135 | | |
136 | | ZEND_API const unsigned char zend_tolower_map[256] = { |
137 | | 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, |
138 | | 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f, |
139 | | 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f, |
140 | | 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f, |
141 | | 0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f, |
142 | | 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x5b,0x5c,0x5d,0x5e,0x5f, |
143 | | 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f, |
144 | | 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f, |
145 | | 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f, |
146 | | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f, |
147 | | 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf, |
148 | | 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf, |
149 | | 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf, |
150 | | 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf, |
151 | | 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef, |
152 | | 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff |
153 | | }; |
154 | | |
155 | | ZEND_API const unsigned char zend_toupper_map[256] = { |
156 | | 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, |
157 | | 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f, |
158 | | 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f, |
159 | | 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f, |
160 | | 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f, |
161 | | 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f, |
162 | | 0x60,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f, |
163 | | 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x7b,0x7c,0x7d,0x7e,0x7f, |
164 | | 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f, |
165 | | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f, |
166 | | 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf, |
167 | | 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf, |
168 | | 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf, |
169 | | 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf, |
170 | | 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef, |
171 | | 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff |
172 | | }; |
173 | | |
174 | | |
175 | | /** |
176 | | * Functions using locale lowercase: |
177 | | zend_binary_strncasecmp_l |
178 | | zend_binary_strcasecmp_l |
179 | | * Functions using ascii lowercase: |
180 | | string_compare_function_ex |
181 | | string_case_compare_function |
182 | | zend_str_tolower_copy |
183 | | zend_str_tolower_dup |
184 | | zend_str_tolower |
185 | | zend_binary_strcasecmp |
186 | | zend_binary_strncasecmp |
187 | | */ |
188 | | |
189 | | static zend_long ZEND_FASTCALL zend_atol_internal(const char *str, size_t str_len) /* {{{ */ |
190 | 0 | { |
191 | 0 | if (!str_len) { |
192 | 0 | str_len = strlen(str); |
193 | 0 | } |
194 | | |
195 | | /* Perform following multiplications on unsigned to avoid overflow UB. |
196 | | * For now overflow is silently ignored -- not clear what else can be |
197 | | * done here, especially as the final result of this function may be |
198 | | * used in an unsigned context (e.g. "memory_limit=3G", which overflows |
199 | | * zend_long on 32-bit, but not size_t). */ |
200 | 0 | zend_ulong retval = (zend_ulong) ZEND_STRTOL(str, NULL, 0); |
201 | 0 | if (str_len>0) { |
202 | 0 | switch (str[str_len-1]) { |
203 | 0 | case 'g': |
204 | 0 | case 'G': |
205 | 0 | retval *= 1024; |
206 | 0 | ZEND_FALLTHROUGH; |
207 | 0 | case 'm': |
208 | 0 | case 'M': |
209 | 0 | retval *= 1024; |
210 | 0 | ZEND_FALLTHROUGH; |
211 | 0 | case 'k': |
212 | 0 | case 'K': |
213 | 0 | retval *= 1024; |
214 | 0 | break; |
215 | 0 | } |
216 | 0 | } |
217 | 0 | return (zend_long) retval; |
218 | 0 | } |
219 | | /* }}} */ |
220 | | |
221 | | ZEND_API zend_long ZEND_FASTCALL zend_atol(const char *str, size_t str_len) |
222 | 0 | { |
223 | 0 | return zend_atol_internal(str, str_len); |
224 | 0 | } |
225 | | |
226 | | ZEND_API int ZEND_FASTCALL zend_atoi(const char *str, size_t str_len) |
227 | 0 | { |
228 | 0 | return (int) zend_atol_internal(str, str_len); |
229 | 0 | } |
230 | | |
231 | | /* {{{ convert_object_to_type: dst will be either ctype or UNDEF */ |
232 | | #define convert_object_to_type(op, dst, ctype) \ |
233 | 163 | ZVAL_UNDEF(dst); \ |
234 | 163 | if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), dst, ctype) == FAILURE) { \ |
235 | 163 | zend_error(E_WARNING, \ |
236 | 163 | "Object of class %s could not be converted to %s", ZSTR_VAL(Z_OBJCE_P(op)->name),\ |
237 | 163 | zend_get_type_by_const(ctype)); \ |
238 | 163 | } \ |
239 | | |
240 | | /* }}} */ |
241 | | |
242 | | ZEND_API void ZEND_FASTCALL convert_scalar_to_number(zval *op) /* {{{ */ |
243 | 0 | { |
244 | 0 | try_again: |
245 | 0 | switch (Z_TYPE_P(op)) { |
246 | 0 | case IS_REFERENCE: |
247 | 0 | zend_unwrap_reference(op); |
248 | 0 | goto try_again; |
249 | 0 | case IS_STRING: |
250 | 0 | { |
251 | 0 | zend_string *str; |
252 | |
|
253 | 0 | str = Z_STR_P(op); |
254 | 0 | if ((Z_TYPE_INFO_P(op)=is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &Z_LVAL_P(op), &Z_DVAL_P(op), 1)) == 0) { |
255 | 0 | ZVAL_LONG(op, 0); |
256 | 0 | } |
257 | 0 | zend_string_release_ex(str, 0); |
258 | 0 | break; |
259 | 0 | } |
260 | 0 | case IS_NULL: |
261 | 0 | case IS_FALSE: |
262 | 0 | ZVAL_LONG(op, 0); |
263 | 0 | break; |
264 | 0 | case IS_TRUE: |
265 | 0 | ZVAL_LONG(op, 1); |
266 | 0 | break; |
267 | 0 | case IS_RESOURCE: |
268 | 0 | { |
269 | 0 | zend_long l = Z_RES_HANDLE_P(op); |
270 | 0 | zval_ptr_dtor(op); |
271 | 0 | ZVAL_LONG(op, l); |
272 | 0 | } |
273 | 0 | break; |
274 | 0 | case IS_OBJECT: |
275 | 0 | { |
276 | 0 | zval dst; |
277 | |
|
278 | 0 | convert_object_to_type(op, &dst, _IS_NUMBER); |
279 | 0 | zval_ptr_dtor(op); |
280 | |
|
281 | 0 | if (Z_TYPE(dst) == IS_LONG || Z_TYPE(dst) == IS_DOUBLE) { |
282 | 0 | ZVAL_COPY_VALUE(op, &dst); |
283 | 0 | } else { |
284 | 0 | ZVAL_LONG(op, 1); |
285 | 0 | } |
286 | 0 | } |
287 | 0 | break; |
288 | 0 | } |
289 | 0 | } |
290 | | /* }}} */ |
291 | | |
292 | | static zend_never_inline zval* ZEND_FASTCALL _zendi_convert_scalar_to_number_silent(zval *op, zval *holder) /* {{{ */ |
293 | 12.3k | { |
294 | 12.3k | switch (Z_TYPE_P(op)) { |
295 | 0 | case IS_NULL: |
296 | 0 | case IS_FALSE: |
297 | 0 | ZVAL_LONG(holder, 0); |
298 | 0 | return holder; |
299 | 0 | case IS_TRUE: |
300 | 0 | ZVAL_LONG(holder, 1); |
301 | 0 | return holder; |
302 | 3.07k | case IS_STRING: |
303 | 3.07k | if ((Z_TYPE_INFO_P(holder) = is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &Z_LVAL_P(holder), &Z_DVAL_P(holder), 1)) == 0) { |
304 | 1.10k | ZVAL_LONG(holder, 0); |
305 | 1.10k | } |
306 | 3.07k | return holder; |
307 | 0 | case IS_RESOURCE: |
308 | 0 | ZVAL_LONG(holder, Z_RES_HANDLE_P(op)); |
309 | 0 | return holder; |
310 | 0 | case IS_OBJECT: |
311 | 0 | convert_object_to_type(op, holder, _IS_NUMBER); |
312 | 0 | if (UNEXPECTED(EG(exception)) || |
313 | 0 | UNEXPECTED(Z_TYPE_P(holder) != IS_LONG && Z_TYPE_P(holder) != IS_DOUBLE)) { |
314 | 0 | ZVAL_LONG(holder, 1); |
315 | 0 | } |
316 | 0 | return holder; |
317 | 3.03k | case IS_LONG: |
318 | 3.06k | case IS_DOUBLE: |
319 | 9.28k | default: |
320 | 9.28k | return op; |
321 | 12.3k | } |
322 | 12.3k | } |
323 | | /* }}} */ |
324 | | |
325 | | static zend_never_inline zend_result ZEND_FASTCALL _zendi_try_convert_scalar_to_number(zval *op, zval *holder) /* {{{ */ |
326 | 119k | { |
327 | 119k | switch (Z_TYPE_P(op)) { |
328 | 53.3k | case IS_NULL: |
329 | 66.4k | case IS_FALSE: |
330 | 66.4k | ZVAL_LONG(holder, 0); |
331 | 66.4k | return SUCCESS; |
332 | 6.14k | case IS_TRUE: |
333 | 6.14k | ZVAL_LONG(holder, 1); |
334 | 6.14k | return SUCCESS; |
335 | 46.5k | case IS_STRING: |
336 | 46.5k | { |
337 | 46.5k | bool trailing_data = false; |
338 | | /* For BC reasons we allow errors so that we can warn on leading numeric string */ |
339 | 46.5k | if (0 == (Z_TYPE_INFO_P(holder) = is_numeric_string_ex(Z_STRVAL_P(op), Z_STRLEN_P(op), |
340 | 46.5k | &Z_LVAL_P(holder), &Z_DVAL_P(holder), /* allow errors */ true, NULL, &trailing_data))) { |
341 | | /* Will lead to invalid OP type error */ |
342 | 376 | return FAILURE; |
343 | 376 | } |
344 | 46.1k | if (UNEXPECTED(trailing_data)) { |
345 | 15.8k | zend_error(E_WARNING, "A non-numeric value encountered"); |
346 | 15.8k | if (UNEXPECTED(EG(exception))) { |
347 | 0 | return FAILURE; |
348 | 0 | } |
349 | 15.8k | } |
350 | 46.1k | return SUCCESS; |
351 | 46.1k | } |
352 | 105 | case IS_OBJECT: |
353 | 105 | if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), holder, _IS_NUMBER) == FAILURE |
354 | 105 | || EG(exception)) { |
355 | 105 | return FAILURE; |
356 | 105 | } |
357 | 0 | ZEND_ASSERT(Z_TYPE_P(holder) == IS_LONG || Z_TYPE_P(holder) == IS_DOUBLE); |
358 | 0 | return SUCCESS; |
359 | 0 | case IS_RESOURCE: |
360 | 162 | case IS_ARRAY: |
361 | 162 | return FAILURE; |
362 | 119k | EMPTY_SWITCH_DEFAULT_CASE() |
363 | 119k | } |
364 | 119k | } |
365 | | /* }}} */ |
366 | | |
367 | | static zend_always_inline zend_result zendi_try_convert_scalar_to_number(zval *op, zval *holder) /* {{{ */ |
368 | 209k | { |
369 | 209k | if (Z_TYPE_P(op) == IS_LONG || Z_TYPE_P(op) == IS_DOUBLE) { |
370 | 90.1k | ZVAL_COPY_VALUE(holder, op); |
371 | 90.1k | return SUCCESS; |
372 | 119k | } else { |
373 | 119k | return _zendi_try_convert_scalar_to_number(op, holder); |
374 | 119k | } |
375 | 209k | } |
376 | | /* }}} */ |
377 | | |
378 | | static zend_never_inline zend_long ZEND_FASTCALL zendi_try_get_long(const zval *op, bool *failed) /* {{{ */ |
379 | 82.3k | { |
380 | 82.3k | *failed = false; |
381 | 82.3k | try_again: |
382 | 82.3k | switch (Z_TYPE_P(op)) { |
383 | 9.35k | case IS_NULL: |
384 | 14.2k | case IS_FALSE: |
385 | 14.2k | return 0; |
386 | 3.23k | case IS_TRUE: |
387 | 3.23k | return 1; |
388 | 58.4k | case IS_DOUBLE: { |
389 | 58.4k | double dval = Z_DVAL_P(op); |
390 | 58.4k | zend_long lval = zend_dval_to_lval_safe(dval); |
391 | 58.4k | if (UNEXPECTED(EG(exception))) { |
392 | 0 | *failed = true; |
393 | 0 | } |
394 | 58.4k | return lval; |
395 | 9.35k | } |
396 | 6.28k | case IS_STRING: |
397 | 6.28k | { |
398 | 6.28k | uint8_t type; |
399 | 6.28k | zend_long lval; |
400 | 6.28k | double dval; |
401 | 6.28k | bool trailing_data = false; |
402 | 6.28k | zend_string *op_str = NULL; /* protect against error handlers */ |
403 | | |
404 | | /* For BC reasons we allow errors so that we can warn on leading numeric string */ |
405 | 6.28k | type = is_numeric_string_ex(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval, |
406 | 6.28k | /* allow errors */ true, NULL, &trailing_data); |
407 | 6.28k | if (type == 0) { |
408 | 729 | *failed = true; |
409 | 729 | return 0; |
410 | 729 | } |
411 | 5.55k | if (UNEXPECTED(trailing_data)) { |
412 | 2.26k | if (type != IS_LONG) { |
413 | 714 | op_str = zend_string_copy(Z_STR_P(op)); |
414 | 714 | } |
415 | 2.26k | zend_error(E_WARNING, "A non-numeric value encountered"); |
416 | 2.26k | if (UNEXPECTED(EG(exception))) { |
417 | 0 | *failed = true; |
418 | 0 | zend_tmp_string_release(op_str); |
419 | 0 | return 0; |
420 | 0 | } |
421 | 2.26k | } |
422 | 5.55k | if (EXPECTED(type == IS_LONG)) { |
423 | 3.89k | return lval; |
424 | 3.89k | } else { |
425 | | /* Previously we used strtol here, not is_numeric_string, |
426 | | * and strtol gives you LONG_MAX/_MIN on overflow. |
427 | | * We use use saturating conversion to emulate strtol()'s |
428 | | * behaviour. |
429 | | */ |
430 | 1.66k | lval = zend_dval_to_lval_cap(dval); |
431 | 1.66k | if (!zend_is_long_compatible(dval, lval)) { |
432 | 1.35k | zend_incompatible_string_to_long_error(op_str ? op_str : Z_STR_P(op)); |
433 | 1.35k | if (UNEXPECTED(EG(exception))) { |
434 | 0 | *failed = true; |
435 | 0 | } |
436 | 1.35k | } |
437 | 1.66k | zend_tmp_string_release(op_str); |
438 | 1.66k | return lval; |
439 | 1.66k | } |
440 | 5.55k | } |
441 | 94 | case IS_OBJECT: |
442 | 94 | { |
443 | 94 | zval dst; |
444 | 94 | if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &dst, IS_LONG) == FAILURE |
445 | 94 | || EG(exception)) { |
446 | 94 | *failed = true; |
447 | 94 | return 0; |
448 | 94 | } |
449 | 0 | ZEND_ASSERT(Z_TYPE(dst) == IS_LONG); |
450 | 0 | return Z_LVAL(dst); |
451 | 0 | } |
452 | 0 | case IS_RESOURCE: |
453 | 24 | case IS_ARRAY: |
454 | 24 | *failed = true; |
455 | 24 | return 0; |
456 | 0 | case IS_REFERENCE: |
457 | 0 | op = Z_REFVAL_P(op); |
458 | 0 | if (Z_TYPE_P(op) == IS_LONG) { |
459 | 0 | return Z_LVAL_P(op); |
460 | 0 | } else { |
461 | 0 | goto try_again; |
462 | 0 | } |
463 | 0 | break; |
464 | 82.3k | EMPTY_SWITCH_DEFAULT_CASE() |
465 | 82.3k | } |
466 | 82.3k | } |
467 | | /* }}} */ |
468 | | |
469 | | ZEND_API zend_long ZEND_FASTCALL zval_try_get_long(const zval *op, bool *failed) |
470 | 0 | { |
471 | 0 | if (EXPECTED(Z_TYPE_P(op) == IS_LONG)) { |
472 | 0 | *failed = false; |
473 | 0 | return Z_LVAL_P(op); |
474 | 0 | } |
475 | 0 | return zendi_try_get_long(op, failed); |
476 | 0 | } |
477 | | |
478 | | #define ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode) \ |
479 | 258k | if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \ |
480 | 258k | && UNEXPECTED(Z_OBJ_HANDLER_P(op1, do_operation))) { \ |
481 | 0 | if (EXPECTED(SUCCESS == Z_OBJ_HANDLER_P(op1, do_operation)(opcode, result, op1, op2))) { \ |
482 | 0 | return SUCCESS; \ |
483 | 0 | } \ |
484 | 0 | } |
485 | | |
486 | | #define ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode) \ |
487 | 338k | if (UNEXPECTED(Z_TYPE_P(op2) == IS_OBJECT) \ |
488 | 338k | && UNEXPECTED(Z_OBJ_HANDLER_P(op2, do_operation)) \ |
489 | 338k | && EXPECTED(SUCCESS == Z_OBJ_HANDLER_P(op2, do_operation)(opcode, result, op1, op2))) { \ |
490 | 0 | return SUCCESS; \ |
491 | 0 | } |
492 | | |
493 | | #define ZEND_TRY_BINARY_OBJECT_OPERATION(opcode) \ |
494 | 218k | ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode) \ |
495 | 218k | else \ |
496 | 218k | ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode) |
497 | | |
498 | | #define ZEND_TRY_UNARY_OBJECT_OPERATION(opcode) \ |
499 | 102k | if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \ |
500 | 102k | && UNEXPECTED(Z_OBJ_HANDLER_P(op1, do_operation)) \ |
501 | 102k | && EXPECTED(SUCCESS == Z_OBJ_HANDLER_P(op1, do_operation)(opcode, result, op1, NULL))) { \ |
502 | 0 | return SUCCESS; \ |
503 | 0 | } |
504 | | |
505 | | #define convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, opcode, sigil) \ |
506 | 29.6k | do { \ |
507 | 29.6k | if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) { \ |
508 | 20.3k | bool failed; \ |
509 | 20.3k | if (Z_ISREF_P(op1)) { \ |
510 | 3 | op1 = Z_REFVAL_P(op1); \ |
511 | 3 | if (Z_TYPE_P(op1) == IS_LONG) { \ |
512 | 3 | op1_lval = Z_LVAL_P(op1); \ |
513 | 3 | break; \ |
514 | 3 | } \ |
515 | 3 | } \ |
516 | 20.3k | ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode); \ |
517 | 20.3k | op1_lval = zendi_try_get_long(op1, &failed); \ |
518 | 20.3k | if (UNEXPECTED(failed)) { \ |
519 | 174 | zend_binop_error(sigil, op1, op2); \ |
520 | 174 | if (result != op1) { \ |
521 | 105 | ZVAL_UNDEF(result); \ |
522 | 105 | } \ |
523 | 174 | return FAILURE; \ |
524 | 174 | } \ |
525 | 20.3k | } else { \ |
526 | 9.31k | op1_lval = Z_LVAL_P(op1); \ |
527 | 9.31k | } \ |
528 | 29.6k | } while (0); \ |
529 | 29.6k | do { \ |
530 | 29.4k | if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) { \ |
531 | 7.31k | bool failed; \ |
532 | 7.31k | if (Z_ISREF_P(op2)) { \ |
533 | 0 | op2 = Z_REFVAL_P(op2); \ |
534 | 0 | if (Z_TYPE_P(op2) == IS_LONG) { \ |
535 | 0 | op2_lval = Z_LVAL_P(op2); \ |
536 | 0 | break; \ |
537 | 0 | } \ |
538 | 0 | } \ |
539 | 7.31k | ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode); \ |
540 | 7.31k | op2_lval = zendi_try_get_long(op2, &failed); \ |
541 | 7.31k | if (UNEXPECTED(failed)) { \ |
542 | 335 | zend_binop_error(sigil, op1, op2); \ |
543 | 335 | if (result != op1) { \ |
544 | 317 | ZVAL_UNDEF(result); \ |
545 | 317 | } \ |
546 | 335 | return FAILURE; \ |
547 | 335 | } \ |
548 | 22.1k | } else { \ |
549 | 22.1k | op2_lval = Z_LVAL_P(op2); \ |
550 | 22.1k | } \ |
551 | 29.4k | } while (0); |
552 | | |
553 | | ZEND_API void ZEND_FASTCALL convert_to_long(zval *op) /* {{{ */ |
554 | 34 | { |
555 | 34 | zend_long tmp; |
556 | | |
557 | 34 | try_again: |
558 | 34 | switch (Z_TYPE_P(op)) { |
559 | 0 | case IS_NULL: |
560 | 3 | case IS_FALSE: |
561 | 3 | ZVAL_LONG(op, 0); |
562 | 3 | break; |
563 | 0 | case IS_TRUE: |
564 | 0 | ZVAL_LONG(op, 1); |
565 | 0 | break; |
566 | 0 | case IS_RESOURCE: |
567 | 0 | tmp = Z_RES_HANDLE_P(op); |
568 | 0 | zval_ptr_dtor(op); |
569 | 0 | ZVAL_LONG(op, tmp); |
570 | 0 | break; |
571 | 26 | case IS_LONG: |
572 | 26 | break; |
573 | 5 | case IS_DOUBLE: { |
574 | | /* NAN might emit a warning */ |
575 | 5 | zend_long new_value = zend_dval_to_lval(Z_DVAL_P(op)); |
576 | 5 | zval_ptr_dtor(op); |
577 | 5 | ZVAL_LONG(op, new_value); |
578 | 5 | break; |
579 | 0 | } |
580 | 0 | case IS_STRING: |
581 | 0 | { |
582 | 0 | zend_string *str = Z_STR_P(op); |
583 | 0 | ZVAL_LONG(op, zval_get_long(op)); |
584 | 0 | zend_string_release_ex(str, 0); |
585 | 0 | } |
586 | 0 | break; |
587 | 0 | case IS_ARRAY: |
588 | 0 | tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0); |
589 | 0 | zval_ptr_dtor(op); |
590 | 0 | ZVAL_LONG(op, tmp); |
591 | 0 | break; |
592 | 0 | case IS_OBJECT: |
593 | 0 | { |
594 | 0 | zval dst; |
595 | |
|
596 | 0 | convert_object_to_type(op, &dst, IS_LONG); |
597 | 0 | zval_ptr_dtor(op); |
598 | |
|
599 | 0 | if (Z_TYPE(dst) == IS_LONG) { |
600 | 0 | ZVAL_LONG(op, Z_LVAL(dst)); |
601 | 0 | } else { |
602 | 0 | ZVAL_LONG(op, 1); |
603 | 0 | } |
604 | 0 | return; |
605 | 0 | } |
606 | 0 | case IS_REFERENCE: |
607 | 0 | zend_unwrap_reference(op); |
608 | 0 | goto try_again; |
609 | 34 | EMPTY_SWITCH_DEFAULT_CASE() |
610 | 34 | } |
611 | 34 | } |
612 | | /* }}} */ |
613 | | |
614 | | ZEND_API void ZEND_FASTCALL convert_to_double(zval *op) /* {{{ */ |
615 | 673 | { |
616 | 673 | double tmp; |
617 | | |
618 | 673 | try_again: |
619 | 673 | switch (Z_TYPE_P(op)) { |
620 | 0 | case IS_NULL: |
621 | 0 | case IS_FALSE: |
622 | 0 | ZVAL_DOUBLE(op, 0.0); |
623 | 0 | break; |
624 | 0 | case IS_TRUE: |
625 | 0 | ZVAL_DOUBLE(op, 1.0); |
626 | 0 | break; |
627 | 0 | case IS_RESOURCE: { |
628 | 0 | double d = (double) Z_RES_HANDLE_P(op); |
629 | 0 | zval_ptr_dtor(op); |
630 | 0 | ZVAL_DOUBLE(op, d); |
631 | 0 | } |
632 | 0 | break; |
633 | 673 | case IS_LONG: |
634 | 673 | ZVAL_DOUBLE(op, (double) Z_LVAL_P(op)); |
635 | 673 | break; |
636 | 0 | case IS_DOUBLE: |
637 | 0 | break; |
638 | 0 | case IS_STRING: |
639 | 0 | { |
640 | 0 | zend_string *str = Z_STR_P(op); |
641 | |
|
642 | 0 | ZVAL_DOUBLE(op, zend_strtod(ZSTR_VAL(str), NULL)); |
643 | 0 | zend_string_release_ex(str, 0); |
644 | 0 | } |
645 | 0 | break; |
646 | 0 | case IS_ARRAY: |
647 | 0 | tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0); |
648 | 0 | zval_ptr_dtor(op); |
649 | 0 | ZVAL_DOUBLE(op, tmp); |
650 | 0 | break; |
651 | 0 | case IS_OBJECT: |
652 | 0 | { |
653 | 0 | zval dst; |
654 | |
|
655 | 0 | convert_object_to_type(op, &dst, IS_DOUBLE); |
656 | 0 | zval_ptr_dtor(op); |
657 | |
|
658 | 0 | if (Z_TYPE(dst) == IS_DOUBLE) { |
659 | 0 | ZVAL_DOUBLE(op, Z_DVAL(dst)); |
660 | 0 | } else { |
661 | 0 | ZVAL_DOUBLE(op, 1.0); |
662 | 0 | } |
663 | 0 | break; |
664 | 0 | } |
665 | 0 | case IS_REFERENCE: |
666 | 0 | zend_unwrap_reference(op); |
667 | 0 | goto try_again; |
668 | 673 | EMPTY_SWITCH_DEFAULT_CASE() |
669 | 673 | } |
670 | 673 | } |
671 | | /* }}} */ |
672 | | |
673 | | ZEND_API void ZEND_FASTCALL convert_to_null(zval *op) /* {{{ */ |
674 | 10 | { |
675 | 10 | if (UNEXPECTED(Z_TYPE_P(op) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op)))) { |
676 | 7 | zend_nan_coerced_to_type_warning(IS_NULL); |
677 | 7 | } |
678 | 10 | zval_ptr_dtor(op); |
679 | 10 | ZVAL_NULL(op); |
680 | 10 | } |
681 | | /* }}} */ |
682 | | |
683 | | ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op) /* {{{ */ |
684 | 8 | { |
685 | 8 | bool tmp; |
686 | | |
687 | 8 | try_again: |
688 | 8 | switch (Z_TYPE_P(op)) { |
689 | 0 | case IS_FALSE: |
690 | 0 | case IS_TRUE: |
691 | 0 | break; |
692 | 3 | case IS_NULL: |
693 | 3 | ZVAL_FALSE(op); |
694 | 3 | break; |
695 | 0 | case IS_RESOURCE: { |
696 | 0 | zend_long l = (Z_RES_HANDLE_P(op) ? 1 : 0); |
697 | |
|
698 | 0 | zval_ptr_dtor(op); |
699 | 0 | ZVAL_BOOL(op, l); |
700 | 0 | } |
701 | 0 | break; |
702 | 0 | case IS_LONG: |
703 | 0 | ZVAL_BOOL(op, Z_LVAL_P(op) ? 1 : 0); |
704 | 0 | break; |
705 | 5 | case IS_DOUBLE: { |
706 | | /* We compute the new value before emitting the warning as the zval may change */ |
707 | 5 | bool new_value = Z_DVAL_P(op) ? true : false; |
708 | 5 | if (UNEXPECTED(zend_isnan(Z_DVAL_P(op)))) { |
709 | 5 | zend_nan_coerced_to_type_warning(_IS_BOOL); |
710 | 5 | zval_ptr_dtor(op); |
711 | 5 | } |
712 | 5 | ZVAL_BOOL(op, new_value); |
713 | 5 | break; |
714 | 0 | } |
715 | 0 | case IS_STRING: |
716 | 0 | { |
717 | 0 | zend_string *str = Z_STR_P(op); |
718 | |
|
719 | 0 | if (ZSTR_LEN(str) == 0 |
720 | 0 | || (ZSTR_LEN(str) == 1 && ZSTR_VAL(str)[0] == '0')) { |
721 | 0 | ZVAL_FALSE(op); |
722 | 0 | } else { |
723 | 0 | ZVAL_TRUE(op); |
724 | 0 | } |
725 | 0 | zend_string_release_ex(str, 0); |
726 | 0 | } |
727 | 0 | break; |
728 | 0 | case IS_ARRAY: |
729 | 0 | tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0); |
730 | 0 | zval_ptr_dtor(op); |
731 | 0 | ZVAL_BOOL(op, tmp); |
732 | 0 | break; |
733 | 0 | case IS_OBJECT: |
734 | 0 | { |
735 | 0 | zval dst; |
736 | |
|
737 | 0 | convert_object_to_type(op, &dst, _IS_BOOL); |
738 | 0 | zval_ptr_dtor(op); |
739 | |
|
740 | 0 | if (Z_TYPE_INFO(dst) == IS_FALSE || Z_TYPE_INFO(dst) == IS_TRUE) { |
741 | 0 | Z_TYPE_INFO_P(op) = Z_TYPE_INFO(dst); |
742 | 0 | } else { |
743 | 0 | ZVAL_TRUE(op); |
744 | 0 | } |
745 | 0 | break; |
746 | 0 | } |
747 | 0 | case IS_REFERENCE: |
748 | 0 | zend_unwrap_reference(op); |
749 | 0 | goto try_again; |
750 | 8 | EMPTY_SWITCH_DEFAULT_CASE() |
751 | 8 | } |
752 | 8 | } |
753 | | /* }}} */ |
754 | | |
755 | | ZEND_API void ZEND_FASTCALL _convert_to_string(zval *op) /* {{{ */ |
756 | 720k | { |
757 | 720k | try_again: |
758 | 720k | switch (Z_TYPE_P(op)) { |
759 | 0 | case IS_UNDEF: |
760 | 4.84k | case IS_NULL: |
761 | 6.90k | case IS_FALSE: { |
762 | 6.90k | ZVAL_EMPTY_STRING(op); |
763 | 6.90k | break; |
764 | 4.84k | } |
765 | 2.55k | case IS_TRUE: |
766 | 2.55k | ZVAL_CHAR(op, '1'); |
767 | 2.55k | break; |
768 | 0 | case IS_STRING: |
769 | 0 | break; |
770 | 0 | case IS_RESOURCE: { |
771 | 0 | zend_string *str = zend_strpprintf(0, "Resource id #" ZEND_LONG_FMT, (zend_long)Z_RES_HANDLE_P(op)); |
772 | 0 | zval_ptr_dtor(op); |
773 | 0 | ZVAL_NEW_STR(op, str); |
774 | 0 | break; |
775 | 4.84k | } |
776 | 709k | case IS_LONG: |
777 | 709k | ZVAL_STR(op, zend_long_to_str(Z_LVAL_P(op))); |
778 | 709k | break; |
779 | 1.45k | case IS_DOUBLE: { |
780 | | /* Casting NAN will cause a warning */ |
781 | 1.45k | zend_string *new_value = zend_double_to_str(Z_DVAL_P(op)); |
782 | 1.45k | zval_ptr_dtor(op); |
783 | 1.45k | ZVAL_NEW_STR(op, new_value); |
784 | 1.45k | break; |
785 | 4.84k | } |
786 | 115 | case IS_ARRAY: |
787 | 115 | zend_error(E_WARNING, "Array to string conversion"); |
788 | 115 | zval_ptr_dtor(op); |
789 | 115 | ZVAL_INTERNED_STR(op, ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED)); |
790 | 115 | break; |
791 | 0 | case IS_OBJECT: { |
792 | 0 | zval tmp; |
793 | 0 | if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &tmp, IS_STRING) == SUCCESS) { |
794 | 0 | zval_ptr_dtor(op); |
795 | 0 | ZVAL_COPY_VALUE(op, &tmp); |
796 | 0 | return; |
797 | 0 | } |
798 | 0 | if (!EG(exception)) { |
799 | 0 | zend_throw_error(NULL, "Object of class %s could not be converted to string", ZSTR_VAL(Z_OBJCE_P(op)->name)); |
800 | 0 | } |
801 | 0 | zval_ptr_dtor(op); |
802 | 0 | ZVAL_EMPTY_STRING(op); |
803 | 0 | break; |
804 | 0 | } |
805 | 0 | case IS_REFERENCE: |
806 | 0 | zend_unwrap_reference(op); |
807 | 0 | goto try_again; |
808 | 720k | EMPTY_SWITCH_DEFAULT_CASE() |
809 | 720k | } |
810 | 720k | } |
811 | | /* }}} */ |
812 | | |
813 | | ZEND_API bool ZEND_FASTCALL _try_convert_to_string(zval *op) /* {{{ */ |
814 | 19 | { |
815 | 19 | zend_string *str; |
816 | | |
817 | 19 | ZEND_ASSERT(Z_TYPE_P(op) != IS_STRING); |
818 | 19 | str = zval_try_get_string_func(op); |
819 | 19 | if (UNEXPECTED(!str)) { |
820 | 17 | return 0; |
821 | 17 | } |
822 | 2 | zval_ptr_dtor(op); |
823 | 2 | ZVAL_STR(op, str); |
824 | 2 | return 1; |
825 | 19 | } |
826 | | /* }}} */ |
827 | | |
828 | | static void convert_scalar_to_array(zval *op) /* {{{ */ |
829 | 83 | { |
830 | 83 | if (UNEXPECTED(Z_TYPE_P(op) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op)))) { |
831 | 5 | zend_nan_coerced_to_type_warning(IS_ARRAY); |
832 | 5 | } |
833 | 83 | HashTable *ht = zend_new_array(1); |
834 | 83 | zend_hash_index_add_new(ht, 0, op); |
835 | 83 | ZVAL_ARR(op, ht); |
836 | 83 | } |
837 | | /* }}} */ |
838 | | |
839 | | ZEND_API void ZEND_FASTCALL convert_to_array(zval *op) /* {{{ */ |
840 | 256 | { |
841 | 256 | try_again: |
842 | 256 | switch (Z_TYPE_P(op)) { |
843 | 79 | case IS_ARRAY: |
844 | 79 | break; |
845 | | /* OBJECTS_OPTIMIZE */ |
846 | 57 | case IS_OBJECT: |
847 | 57 | if (Z_OBJCE_P(op) == zend_ce_closure) { |
848 | 5 | convert_scalar_to_array(op); |
849 | 52 | } else if (ZEND_STD_BUILD_OBJECT_PROPERTIES_ARRAY_COMPATIBLE(op)) { |
850 | | /* Optimized version without rebuilding properties HashTable */ |
851 | 28 | HashTable *ht = zend_std_build_object_properties_array(Z_OBJ_P(op)); |
852 | 28 | OBJ_RELEASE(Z_OBJ_P(op)); |
853 | 28 | ZVAL_ARR(op, ht); |
854 | 28 | } else { |
855 | 24 | HashTable *obj_ht = zend_get_properties_for(op, ZEND_PROP_PURPOSE_ARRAY_CAST); |
856 | 24 | if (obj_ht) { |
857 | 24 | HashTable *new_obj_ht = zend_proptable_to_symtable(obj_ht, |
858 | 24 | (Z_OBJCE_P(op)->default_properties_count || |
859 | 0 | Z_OBJ_P(op)->handlers != &std_object_handlers || |
860 | 0 | GC_IS_RECURSIVE(obj_ht))); |
861 | 24 | zval_ptr_dtor(op); |
862 | 24 | ZVAL_ARR(op, new_obj_ht); |
863 | 24 | zend_release_properties(obj_ht); |
864 | 24 | } else { |
865 | 0 | zval_ptr_dtor(op); |
866 | | /*ZVAL_EMPTY_ARRAY(op);*/ |
867 | 0 | array_init(op); |
868 | 0 | } |
869 | 24 | } |
870 | 57 | break; |
871 | 42 | case IS_NULL: |
872 | | /*ZVAL_EMPTY_ARRAY(op);*/ |
873 | 42 | array_init(op); |
874 | 42 | break; |
875 | 0 | case IS_REFERENCE: |
876 | 0 | zend_unwrap_reference(op); |
877 | 0 | goto try_again; |
878 | 78 | default: |
879 | 78 | convert_scalar_to_array(op); |
880 | 78 | break; |
881 | 256 | } |
882 | 256 | } |
883 | | /* }}} */ |
884 | | |
885 | | ZEND_API void ZEND_FASTCALL convert_to_object(zval *op) /* {{{ */ |
886 | 8 | { |
887 | 8 | try_again: |
888 | 8 | switch (Z_TYPE_P(op)) { |
889 | 3 | case IS_ARRAY: |
890 | 3 | { |
891 | 3 | HashTable *ht = zend_symtable_to_proptable(Z_ARR_P(op)); |
892 | 3 | zend_object *obj; |
893 | | |
894 | 3 | if (GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) { |
895 | | /* TODO: try not to duplicate immutable arrays as well ??? */ |
896 | 0 | ht = zend_array_dup(ht); |
897 | 3 | } else if (ht != Z_ARR_P(op)) { |
898 | 3 | zval_ptr_dtor(op); |
899 | 3 | } else { |
900 | 0 | GC_DELREF(ht); |
901 | 0 | } |
902 | 3 | obj = zend_objects_new(zend_standard_class_def); |
903 | 3 | obj->properties = ht; |
904 | 3 | ZVAL_OBJ(op, obj); |
905 | 3 | break; |
906 | 0 | } |
907 | 0 | case IS_OBJECT: |
908 | 0 | break; |
909 | 0 | case IS_NULL: |
910 | 0 | object_init(op); |
911 | 0 | break; |
912 | 0 | case IS_REFERENCE: |
913 | 0 | zend_unwrap_reference(op); |
914 | 0 | goto try_again; |
915 | 5 | case IS_DOUBLE: |
916 | 5 | if (UNEXPECTED(zend_isnan(Z_DVAL_P(op)))) { |
917 | 5 | zend_nan_coerced_to_type_warning(IS_OBJECT); |
918 | 5 | } |
919 | 5 | ZEND_FALLTHROUGH; |
920 | 5 | default: { |
921 | 5 | zval tmp; |
922 | 5 | ZVAL_COPY_VALUE(&tmp, op); |
923 | 5 | object_init(op); |
924 | 5 | zend_hash_add_new(Z_OBJPROP_P(op), ZSTR_KNOWN(ZEND_STR_SCALAR), &tmp); |
925 | 5 | break; |
926 | 5 | } |
927 | 8 | } |
928 | 8 | } |
929 | | /* }}} */ |
930 | | |
931 | | ZEND_API void ZEND_COLD zend_incompatible_double_to_long_error(double d) |
932 | 39.0k | { |
933 | 39.0k | zend_error_unchecked(E_DEPRECATED, "Implicit conversion from float %.*H to int loses precision", -1, d); |
934 | 39.0k | } |
935 | | ZEND_API void ZEND_COLD zend_incompatible_string_to_long_error(const zend_string *s) |
936 | 1.39k | { |
937 | 1.39k | zend_error(E_DEPRECATED, "Implicit conversion from float-string \"%s\" to int loses precision", ZSTR_VAL(s)); |
938 | 1.39k | } |
939 | | ZEND_API void ZEND_COLD zend_oob_double_to_long_error(double d) |
940 | 45.1k | { |
941 | 45.1k | zend_error_unchecked(E_WARNING, "The float %.*H is not representable as an int, cast occurred", -1, d); |
942 | 45.1k | } |
943 | | ZEND_API void ZEND_COLD zend_oob_string_to_long_error(const zend_string *s) |
944 | 0 | { |
945 | 0 | zend_error_unchecked(E_WARNING, "The float-string \"%s\" is not representable as an int, cast occurred", ZSTR_VAL(s)); |
946 | 0 | } |
947 | | |
948 | | ZEND_API void ZEND_COLD zend_nan_coerced_to_type_warning(uint8_t type) |
949 | 269 | { |
950 | 269 | zend_error(E_WARNING, "unexpected NAN value was coerced to %s", zend_get_type_by_const(type)); |
951 | 269 | } |
952 | | |
953 | | ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(const zval *op, bool is_strict) /* {{{ */ |
954 | 42.1k | { |
955 | 42.1k | try_again: |
956 | 42.1k | switch (Z_TYPE_P(op)) { |
957 | 81 | case IS_UNDEF: |
958 | 518 | case IS_NULL: |
959 | 1.80k | case IS_FALSE: |
960 | 1.80k | return 0; |
961 | 975 | case IS_TRUE: |
962 | 975 | return 1; |
963 | 0 | case IS_RESOURCE: |
964 | 0 | return Z_RES_HANDLE_P(op); |
965 | 14 | case IS_LONG: |
966 | 14 | return Z_LVAL_P(op); |
967 | 25.8k | case IS_DOUBLE: { |
968 | 25.8k | double dval = Z_DVAL_P(op); |
969 | 25.8k | zend_long lval = zend_dval_to_lval(dval); |
970 | 25.8k | if (UNEXPECTED(is_strict)) { |
971 | 103 | if (!zend_is_long_compatible(dval, lval)) { |
972 | 103 | zend_incompatible_double_to_long_error(dval); |
973 | 103 | } |
974 | 103 | } |
975 | 25.8k | return lval; |
976 | 518 | } |
977 | 13.3k | case IS_STRING: |
978 | 13.3k | { |
979 | 13.3k | uint8_t type; |
980 | 13.3k | zend_long lval; |
981 | 13.3k | double dval; |
982 | 13.3k | if (0 == (type = is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval, true))) { |
983 | 1.09k | return 0; |
984 | 12.2k | } else if (EXPECTED(type == IS_LONG)) { |
985 | 8.01k | return lval; |
986 | 8.01k | } else { |
987 | | /* Previously we used strtol here, not is_numeric_string, |
988 | | * and strtol gives you LONG_MAX/_MIN on overflow. |
989 | | * We use saturating conversion to emulate strtol()'s |
990 | | * behaviour. |
991 | | */ |
992 | | /* Most usages are expected to not be (int) casts */ |
993 | 4.25k | lval = zend_dval_to_lval_cap(dval); |
994 | 4.25k | if (UNEXPECTED(is_strict)) { |
995 | 0 | if (!zend_is_long_compatible(dval, lval)) { |
996 | 0 | zend_incompatible_string_to_long_error(Z_STR_P(op)); |
997 | 0 | } |
998 | 0 | } |
999 | 4.25k | return lval; |
1000 | 4.25k | } |
1001 | 13.3k | } |
1002 | 154 | case IS_ARRAY: |
1003 | 154 | return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1 : 0; |
1004 | 24 | case IS_OBJECT: |
1005 | 24 | { |
1006 | 24 | zval dst; |
1007 | 24 | convert_object_to_type(op, &dst, IS_LONG); |
1008 | 24 | if (Z_TYPE(dst) == IS_LONG) { |
1009 | 0 | return Z_LVAL(dst); |
1010 | 24 | } else { |
1011 | 24 | return 1; |
1012 | 24 | } |
1013 | 24 | } |
1014 | 14 | case IS_REFERENCE: |
1015 | 14 | op = Z_REFVAL_P(op); |
1016 | 14 | goto try_again; |
1017 | 42.1k | EMPTY_SWITCH_DEFAULT_CASE() |
1018 | 42.1k | } |
1019 | 0 | return 0; |
1020 | 42.1k | } |
1021 | | /* }}} */ |
1022 | | |
1023 | | ZEND_API double ZEND_FASTCALL zval_get_double_func(const zval *op) /* {{{ */ |
1024 | 50.3k | { |
1025 | 55.1k | try_again: |
1026 | 55.1k | switch (Z_TYPE_P(op)) { |
1027 | 354 | case IS_NULL: |
1028 | 1.63k | case IS_FALSE: |
1029 | 1.63k | return 0.0; |
1030 | 412 | case IS_TRUE: |
1031 | 412 | return 1.0; |
1032 | 0 | case IS_RESOURCE: |
1033 | 0 | return (double) Z_RES_HANDLE_P(op); |
1034 | 35.5k | case IS_LONG: |
1035 | 35.5k | return (double) Z_LVAL_P(op); |
1036 | 2.90k | case IS_DOUBLE: |
1037 | 2.90k | return Z_DVAL_P(op); |
1038 | 3.02k | case IS_STRING: |
1039 | 3.02k | return zend_strtod(Z_STRVAL_P(op), NULL); |
1040 | 6.68k | case IS_ARRAY: |
1041 | 6.68k | return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1.0 : 0.0; |
1042 | 139 | case IS_OBJECT: |
1043 | 139 | { |
1044 | 139 | zval dst; |
1045 | 139 | convert_object_to_type(op, &dst, IS_DOUBLE); |
1046 | | |
1047 | 139 | if (Z_TYPE(dst) == IS_DOUBLE) { |
1048 | 0 | return Z_DVAL(dst); |
1049 | 139 | } else { |
1050 | 139 | return 1.0; |
1051 | 139 | } |
1052 | 139 | } |
1053 | 4.87k | case IS_REFERENCE: |
1054 | 4.87k | op = Z_REFVAL_P(op); |
1055 | 4.87k | goto try_again; |
1056 | 55.1k | EMPTY_SWITCH_DEFAULT_CASE() |
1057 | 55.1k | } |
1058 | 0 | return 0.0; |
1059 | 55.1k | } |
1060 | | /* }}} */ |
1061 | | |
1062 | | static zend_always_inline zend_string* __zval_get_string_func(zval *op, bool try) /* {{{ */ |
1063 | 1.00M | { |
1064 | 1.00M | try_again: |
1065 | 1.00M | switch (Z_TYPE_P(op)) { |
1066 | 433k | case IS_UNDEF: |
1067 | 617k | case IS_NULL: |
1068 | 648k | case IS_FALSE: |
1069 | 648k | return ZSTR_EMPTY_ALLOC(); |
1070 | 13.5k | case IS_TRUE: |
1071 | 13.5k | return ZSTR_CHAR('1'); |
1072 | 0 | case IS_RESOURCE: |
1073 | 0 | return zend_strpprintf(0, "Resource id #" ZEND_LONG_FMT, (zend_long)Z_RES_HANDLE_P(op)); |
1074 | 177k | case IS_LONG: |
1075 | 177k | return zend_long_to_str(Z_LVAL_P(op)); |
1076 | 150k | case IS_DOUBLE: |
1077 | 150k | return zend_double_to_str(Z_DVAL_P(op)); |
1078 | 2.96k | case IS_ARRAY: |
1079 | 2.96k | zend_error(E_WARNING, "Array to string conversion"); |
1080 | 2.96k | return (try && UNEXPECTED(EG(exception))) ? |
1081 | 2.96k | NULL : ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED); |
1082 | 7.16k | case IS_OBJECT: { |
1083 | 7.16k | zval tmp; |
1084 | 7.16k | if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &tmp, IS_STRING) == SUCCESS) { |
1085 | 3.97k | return Z_STR(tmp); |
1086 | 3.97k | } |
1087 | 3.19k | if (!EG(exception)) { |
1088 | 854 | zend_throw_error(NULL, "Object of class %s could not be converted to string", ZSTR_VAL(Z_OBJCE_P(op)->name)); |
1089 | 854 | } |
1090 | 3.19k | return try ? NULL : ZSTR_EMPTY_ALLOC(); |
1091 | 7.16k | } |
1092 | 5.33k | case IS_REFERENCE: |
1093 | 5.33k | op = Z_REFVAL_P(op); |
1094 | 5.33k | goto try_again; |
1095 | 525 | case IS_STRING: |
1096 | 525 | return zend_string_copy(Z_STR_P(op)); |
1097 | 1.00M | EMPTY_SWITCH_DEFAULT_CASE() |
1098 | 1.00M | } |
1099 | 0 | return NULL; |
1100 | 1.00M | } |
1101 | | /* }}} */ |
1102 | | |
1103 | | ZEND_API zend_string* ZEND_FASTCALL zval_get_string_func(zval *op) /* {{{ */ |
1104 | 779k | { |
1105 | 779k | return __zval_get_string_func(op, false); |
1106 | 779k | } |
1107 | | /* }}} */ |
1108 | | |
1109 | | ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_func(zval *op) /* {{{ */ |
1110 | 220k | { |
1111 | 220k | return __zval_get_string_func(op, true); |
1112 | 220k | } |
1113 | | /* }}} */ |
1114 | | |
1115 | 1.49k | static ZEND_COLD zend_never_inline void ZEND_FASTCALL zend_binop_error(const char *operator, zval *op1, zval *op2) /* {{{ */ { |
1116 | 1.49k | if (EG(exception)) { |
1117 | 0 | return; |
1118 | 0 | } |
1119 | | |
1120 | 1.49k | zend_type_error("Unsupported operand types: %s %s %s", |
1121 | 1.49k | zend_zval_type_name(op1), operator, zend_zval_type_name(op2)); |
1122 | 1.49k | } |
1123 | | /* }}} */ |
1124 | | |
1125 | | static zend_never_inline void ZEND_FASTCALL add_function_array(zval *result, zval *op1, zval *op2) /* {{{ */ |
1126 | 4.65k | { |
1127 | 4.65k | if (result == op1 && Z_ARR_P(op1) == Z_ARR_P(op2)) { |
1128 | | /* $a += $a */ |
1129 | 182 | return; |
1130 | 182 | } |
1131 | 4.46k | if (result != op1) { |
1132 | 4.44k | ZVAL_ARR(result, zend_array_dup(Z_ARR_P(op1))); |
1133 | 4.44k | } else { |
1134 | 25 | SEPARATE_ARRAY(result); |
1135 | 25 | } |
1136 | 4.46k | zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0); |
1137 | 4.46k | } |
1138 | | /* }}} */ |
1139 | | |
1140 | | static zend_always_inline zend_result add_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */ |
1141 | 125k | { |
1142 | 125k | uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2)); |
1143 | | |
1144 | 125k | if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) { |
1145 | 37.0k | fast_long_add_function(result, op1, op2); |
1146 | 37.0k | return SUCCESS; |
1147 | 88.2k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) { |
1148 | 5.28k | ZVAL_DOUBLE(result, Z_DVAL_P(op1) + Z_DVAL_P(op2)); |
1149 | 5.28k | return SUCCESS; |
1150 | 82.9k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) { |
1151 | 3.62k | ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) + Z_DVAL_P(op2)); |
1152 | 3.62k | return SUCCESS; |
1153 | 79.3k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) { |
1154 | 4.35k | ZVAL_DOUBLE(result, Z_DVAL_P(op1) + ((double)Z_LVAL_P(op2))); |
1155 | 4.35k | return SUCCESS; |
1156 | 75.0k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_ARRAY, IS_ARRAY))) { |
1157 | 4.65k | add_function_array(result, op1, op2); |
1158 | 4.65k | return SUCCESS; |
1159 | 70.3k | } else { |
1160 | 70.3k | return FAILURE; |
1161 | 70.3k | } |
1162 | 125k | } /* }}} */ |
1163 | | |
1164 | | static zend_never_inline zend_result ZEND_FASTCALL add_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */ |
1165 | 37.6k | { |
1166 | 37.6k | ZVAL_DEREF(op1); |
1167 | 37.6k | ZVAL_DEREF(op2); |
1168 | 37.6k | if (add_function_fast(result, op1, op2) == SUCCESS) { |
1169 | 4.94k | return SUCCESS; |
1170 | 4.94k | } |
1171 | | |
1172 | 37.6k | ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_ADD); |
1173 | | |
1174 | 32.7k | zval op1_copy, op2_copy; |
1175 | 32.7k | if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE) |
1176 | 32.5k | || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) { |
1177 | 169 | zend_binop_error("+", op1, op2); |
1178 | 169 | if (result != op1) { |
1179 | 125 | ZVAL_UNDEF(result); |
1180 | 125 | } |
1181 | 169 | return FAILURE; |
1182 | 169 | } |
1183 | | |
1184 | 32.5k | if (result == op1) { |
1185 | 5.66k | zval_ptr_dtor(result); |
1186 | 5.66k | } |
1187 | | |
1188 | 32.5k | if (add_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) { |
1189 | 32.5k | return SUCCESS; |
1190 | 32.5k | } |
1191 | | |
1192 | 0 | ZEND_ASSERT(0 && "Operation must succeed"); |
1193 | 0 | return FAILURE; |
1194 | 0 | } /* }}} */ |
1195 | | |
1196 | | ZEND_API zend_result ZEND_FASTCALL add_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
1197 | 55.0k | { |
1198 | 55.0k | if (add_function_fast(result, op1, op2) == SUCCESS) { |
1199 | 17.4k | return SUCCESS; |
1200 | 37.6k | } else { |
1201 | 37.6k | return add_function_slow(result, op1, op2); |
1202 | 37.6k | } |
1203 | 55.0k | } |
1204 | | /* }}} */ |
1205 | | |
1206 | | static zend_always_inline zend_result sub_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */ |
1207 | 44.3k | { |
1208 | 44.3k | uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2)); |
1209 | | |
1210 | 44.3k | if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) { |
1211 | 13.2k | fast_long_sub_function(result, op1, op2); |
1212 | 13.2k | return SUCCESS; |
1213 | 31.0k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) { |
1214 | 2.04k | ZVAL_DOUBLE(result, Z_DVAL_P(op1) - Z_DVAL_P(op2)); |
1215 | 2.04k | return SUCCESS; |
1216 | 29.0k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) { |
1217 | 2.96k | ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) - Z_DVAL_P(op2)); |
1218 | 2.96k | return SUCCESS; |
1219 | 26.0k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) { |
1220 | 2.58k | ZVAL_DOUBLE(result, Z_DVAL_P(op1) - ((double)Z_LVAL_P(op2))); |
1221 | 2.58k | return SUCCESS; |
1222 | 23.5k | } else { |
1223 | 23.5k | return FAILURE; |
1224 | 23.5k | } |
1225 | 44.3k | } |
1226 | | /* }}} */ |
1227 | | |
1228 | | static zend_never_inline zend_result ZEND_FASTCALL sub_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */ |
1229 | 12.4k | { |
1230 | 12.4k | ZVAL_DEREF(op1); |
1231 | 12.4k | ZVAL_DEREF(op2); |
1232 | 12.4k | if (sub_function_fast(result, op1, op2) == SUCCESS) { |
1233 | 1.31k | return SUCCESS; |
1234 | 1.31k | } |
1235 | | |
1236 | 12.4k | ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SUB); |
1237 | | |
1238 | 11.0k | zval op1_copy, op2_copy; |
1239 | 11.0k | if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE) |
1240 | 10.9k | || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) { |
1241 | 147 | zend_binop_error("-", op1, op2); |
1242 | 147 | if (result != op1) { |
1243 | 108 | ZVAL_UNDEF(result); |
1244 | 108 | } |
1245 | 147 | return FAILURE; |
1246 | 147 | } |
1247 | | |
1248 | 10.9k | if (result == op1) { |
1249 | 872 | zval_ptr_dtor(result); |
1250 | 872 | } |
1251 | | |
1252 | 10.9k | if (sub_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) { |
1253 | 10.9k | return SUCCESS; |
1254 | 10.9k | } |
1255 | | |
1256 | 0 | ZEND_ASSERT(0 && "Operation must succeed"); |
1257 | 0 | return FAILURE; |
1258 | 0 | } |
1259 | | /* }}} */ |
1260 | | |
1261 | | ZEND_API zend_result ZEND_FASTCALL sub_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
1262 | 21.0k | { |
1263 | 21.0k | if (sub_function_fast(result, op1, op2) == SUCCESS) { |
1264 | 8.59k | return SUCCESS; |
1265 | 12.4k | } else { |
1266 | 12.4k | return sub_function_slow(result, op1, op2); |
1267 | 12.4k | } |
1268 | 21.0k | } |
1269 | | /* }}} */ |
1270 | | |
1271 | | static zend_always_inline zend_result mul_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */ |
1272 | 209k | { |
1273 | 209k | uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2)); |
1274 | | |
1275 | 209k | if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) { |
1276 | 84.0k | zend_long overflow; |
1277 | 84.0k | ZEND_SIGNED_MULTIPLY_LONG( |
1278 | 84.0k | Z_LVAL_P(op1), Z_LVAL_P(op2), |
1279 | 84.0k | Z_LVAL_P(result), Z_DVAL_P(result), overflow); |
1280 | 84.0k | Z_TYPE_INFO_P(result) = overflow ? IS_DOUBLE : IS_LONG; |
1281 | 84.0k | return SUCCESS; |
1282 | 125k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) { |
1283 | 1.60k | ZVAL_DOUBLE(result, Z_DVAL_P(op1) * Z_DVAL_P(op2)); |
1284 | 1.60k | return SUCCESS; |
1285 | 124k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) { |
1286 | 1.09k | ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) * Z_DVAL_P(op2)); |
1287 | 1.09k | return SUCCESS; |
1288 | 122k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) { |
1289 | 7.83k | ZVAL_DOUBLE(result, Z_DVAL_P(op1) * ((double)Z_LVAL_P(op2))); |
1290 | 7.83k | return SUCCESS; |
1291 | 115k | } else { |
1292 | 115k | return FAILURE; |
1293 | 115k | } |
1294 | 209k | } |
1295 | | /* }}} */ |
1296 | | |
1297 | | static zend_never_inline zend_result ZEND_FASTCALL mul_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */ |
1298 | 57.9k | { |
1299 | 57.9k | ZVAL_DEREF(op1); |
1300 | 57.9k | ZVAL_DEREF(op2); |
1301 | 57.9k | if (mul_function_fast(result, op1, op2) == SUCCESS) { |
1302 | 715 | return SUCCESS; |
1303 | 715 | } |
1304 | | |
1305 | 57.9k | ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_MUL); |
1306 | | |
1307 | 57.2k | zval op1_copy, op2_copy; |
1308 | 57.2k | if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE) |
1309 | 57.0k | || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) { |
1310 | 175 | zend_binop_error("*", op1, op2); |
1311 | 175 | if (result != op1) { |
1312 | 133 | ZVAL_UNDEF(result); |
1313 | 133 | } |
1314 | 175 | return FAILURE; |
1315 | 175 | } |
1316 | | |
1317 | 57.0k | if (result == op1) { |
1318 | 3.81k | zval_ptr_dtor(result); |
1319 | 3.81k | } |
1320 | | |
1321 | 57.0k | if (mul_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) { |
1322 | 57.0k | return SUCCESS; |
1323 | 57.0k | } |
1324 | | |
1325 | 0 | ZEND_ASSERT(0 && "Operation must succeed"); |
1326 | 0 | return FAILURE; |
1327 | 0 | } |
1328 | | /* }}} */ |
1329 | | |
1330 | | ZEND_API zend_result ZEND_FASTCALL mul_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
1331 | 94.7k | { |
1332 | 94.7k | if (mul_function_fast(result, op1, op2) == SUCCESS) { |
1333 | 36.8k | return SUCCESS; |
1334 | 57.9k | } else { |
1335 | 57.9k | return mul_function_slow(result, op1, op2); |
1336 | 57.9k | } |
1337 | 94.7k | } |
1338 | | /* }}} */ |
1339 | | |
1340 | | static void ZEND_COLD zend_power_base_0_exponent_lt_0_error(void) |
1341 | 0 | { |
1342 | 0 | zend_error(E_DEPRECATED, "Power of base 0 and negative exponent is deprecated"); |
1343 | 0 | } |
1344 | | |
1345 | | static double safe_pow(double base, double exponent) |
1346 | 4.52k | { |
1347 | 4.52k | if (UNEXPECTED(base == 0.0 && exponent < 0.0)) { |
1348 | 0 | zend_power_base_0_exponent_lt_0_error(); |
1349 | 0 | } |
1350 | | |
1351 | 4.52k | return pow(base, exponent); |
1352 | 4.52k | } |
1353 | | |
1354 | | static zend_result ZEND_FASTCALL pow_function_base(zval *result, zval *op1, zval *op2) /* {{{ */ |
1355 | 12.8k | { |
1356 | 12.8k | uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2)); |
1357 | | |
1358 | 12.8k | if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) { |
1359 | 9.51k | if (Z_LVAL_P(op2) >= 0) { |
1360 | 8.51k | zend_long l1 = 1, l2 = Z_LVAL_P(op1), i = Z_LVAL_P(op2); |
1361 | | |
1362 | 8.51k | if (i == 0) { |
1363 | 2.71k | ZVAL_LONG(result, 1L); |
1364 | 2.71k | return SUCCESS; |
1365 | 5.79k | } else if (l2 == 0) { |
1366 | 2.45k | ZVAL_LONG(result, 0); |
1367 | 2.45k | return SUCCESS; |
1368 | 2.45k | } |
1369 | | |
1370 | 19.0k | while (i >= 1) { |
1371 | 17.2k | zend_long overflow; |
1372 | 17.2k | double dval = 0.0; |
1373 | | |
1374 | 17.2k | if (i % 2) { |
1375 | 6.86k | --i; |
1376 | 6.86k | ZEND_SIGNED_MULTIPLY_LONG(l1, l2, l1, dval, overflow); |
1377 | 6.86k | if (overflow) { |
1378 | 1.08k | ZVAL_DOUBLE(result, dval * safe_pow(l2, i)); |
1379 | 1.08k | return SUCCESS; |
1380 | 1.08k | } |
1381 | 10.3k | } else { |
1382 | 10.3k | i /= 2; |
1383 | 10.3k | ZEND_SIGNED_MULTIPLY_LONG(l2, l2, l2, dval, overflow); |
1384 | 10.3k | if (overflow) { |
1385 | 420 | ZVAL_DOUBLE(result, (double)l1 * safe_pow(dval, i)); |
1386 | 420 | return SUCCESS; |
1387 | 420 | } |
1388 | 10.3k | } |
1389 | 17.2k | } |
1390 | | /* i == 0 */ |
1391 | 1.83k | ZVAL_LONG(result, l1); |
1392 | 1.83k | } else { |
1393 | 1.00k | ZVAL_DOUBLE(result, safe_pow((double)Z_LVAL_P(op1), (double)Z_LVAL_P(op2))); |
1394 | 1.00k | } |
1395 | 2.83k | return SUCCESS; |
1396 | 9.51k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) { |
1397 | 563 | ZVAL_DOUBLE(result, safe_pow(Z_DVAL_P(op1), Z_DVAL_P(op2))); |
1398 | 563 | return SUCCESS; |
1399 | 2.73k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) { |
1400 | 647 | ZVAL_DOUBLE(result, safe_pow((double)Z_LVAL_P(op1), Z_DVAL_P(op2))); |
1401 | 647 | return SUCCESS; |
1402 | 2.08k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) { |
1403 | 800 | ZVAL_DOUBLE(result, safe_pow(Z_DVAL_P(op1), (double)Z_LVAL_P(op2))); |
1404 | 800 | return SUCCESS; |
1405 | 1.28k | } else { |
1406 | 1.28k | return FAILURE; |
1407 | 1.28k | } |
1408 | 12.8k | } |
1409 | | /* }}} */ |
1410 | | |
1411 | | ZEND_API zend_result ZEND_FASTCALL pow_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
1412 | 11.5k | { |
1413 | 11.5k | ZVAL_DEREF(op1); |
1414 | 11.5k | ZVAL_DEREF(op2); |
1415 | 11.5k | if (pow_function_base(result, op1, op2) == SUCCESS) { |
1416 | 10.2k | return SUCCESS; |
1417 | 10.2k | } |
1418 | | |
1419 | 11.5k | ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_POW); |
1420 | | |
1421 | 1.28k | zval op1_copy, op2_copy; |
1422 | 1.28k | if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE) |
1423 | 1.23k | || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) { |
1424 | 55 | zend_binop_error("**", op1, op2); |
1425 | 55 | if (result != op1) { |
1426 | 20 | ZVAL_UNDEF(result); |
1427 | 20 | } |
1428 | 55 | return FAILURE; |
1429 | 55 | } |
1430 | | |
1431 | 1.23k | if (result == op1) { |
1432 | 23 | zval_ptr_dtor(result); |
1433 | 23 | } |
1434 | | |
1435 | 1.23k | if (pow_function_base(result, &op1_copy, &op2_copy) == SUCCESS) { |
1436 | 1.23k | return SUCCESS; |
1437 | 1.23k | } |
1438 | | |
1439 | 0 | ZEND_ASSERT(0 && "Operation must succeed"); |
1440 | 0 | return FAILURE; |
1441 | 0 | } |
1442 | | /* }}} */ |
1443 | | |
1444 | | typedef enum { |
1445 | | DIV_SUCCESS, |
1446 | | DIV_BY_ZERO, |
1447 | | DIV_TYPES_NOT_HANDLED |
1448 | | } zend_div_status; |
1449 | | |
1450 | | static zend_div_status ZEND_FASTCALL div_function_base(zval *result, const zval *op1, const zval *op2) /* {{{ */ |
1451 | 16.2k | { |
1452 | 16.2k | uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2)); |
1453 | | |
1454 | 16.2k | if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) { |
1455 | 7.83k | if (Z_LVAL_P(op2) == 0) { |
1456 | 60 | return DIV_BY_ZERO; |
1457 | 7.77k | } else if (Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == ZEND_LONG_MIN) { |
1458 | | /* Prevent overflow error/crash */ |
1459 | 80 | ZVAL_DOUBLE(result, (double) ZEND_LONG_MIN / -1); |
1460 | 80 | return DIV_SUCCESS; |
1461 | 80 | } |
1462 | 7.69k | if (Z_LVAL_P(op1) % Z_LVAL_P(op2) == 0) { /* integer */ |
1463 | 2.15k | ZVAL_LONG(result, Z_LVAL_P(op1) / Z_LVAL_P(op2)); |
1464 | 5.53k | } else { |
1465 | 5.53k | ZVAL_DOUBLE(result, ((double) Z_LVAL_P(op1)) / Z_LVAL_P(op2)); |
1466 | 5.53k | } |
1467 | 7.69k | return DIV_SUCCESS; |
1468 | 8.41k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) { |
1469 | 838 | if (Z_DVAL_P(op2) == 0) { |
1470 | 7 | return DIV_BY_ZERO; |
1471 | 7 | } |
1472 | 831 | ZVAL_DOUBLE(result, Z_DVAL_P(op1) / Z_DVAL_P(op2)); |
1473 | 831 | return DIV_SUCCESS; |
1474 | 7.57k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) { |
1475 | 1.81k | if (Z_LVAL_P(op2) == 0) { |
1476 | 10 | return DIV_BY_ZERO; |
1477 | 10 | } |
1478 | 1.80k | ZVAL_DOUBLE(result, Z_DVAL_P(op1) / (double)Z_LVAL_P(op2)); |
1479 | 1.80k | return DIV_SUCCESS; |
1480 | 5.75k | } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) { |
1481 | 3.04k | if (Z_DVAL_P(op2) == 0) { |
1482 | 10 | return DIV_BY_ZERO; |
1483 | 10 | } |
1484 | 3.03k | ZVAL_DOUBLE(result, (double)Z_LVAL_P(op1) / Z_DVAL_P(op2)); |
1485 | 3.03k | return DIV_SUCCESS; |
1486 | 3.04k | } else { |
1487 | 2.71k | return DIV_TYPES_NOT_HANDLED; |
1488 | 2.71k | } |
1489 | 16.2k | } |
1490 | | /* }}} */ |
1491 | | |
1492 | | ZEND_API zend_result ZEND_FASTCALL div_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
1493 | 13.6k | { |
1494 | 13.6k | ZVAL_DEREF(op1); |
1495 | 13.6k | ZVAL_DEREF(op2); |
1496 | | |
1497 | 13.6k | zend_div_status retval = div_function_base(result, op1, op2); |
1498 | 13.6k | if (EXPECTED(retval == DIV_SUCCESS)) { |
1499 | 10.8k | return SUCCESS; |
1500 | 10.8k | } |
1501 | | |
1502 | 2.75k | if (UNEXPECTED(retval == DIV_BY_ZERO)) { |
1503 | 42 | goto div_by_zero; |
1504 | 42 | } |
1505 | | |
1506 | 2.75k | ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_DIV); |
1507 | | |
1508 | 2.71k | zval result_copy, op1_copy, op2_copy; |
1509 | 2.71k | if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE) |
1510 | 2.63k | || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) { |
1511 | 97 | zend_binop_error("/", op1, op2); |
1512 | 97 | if (result != op1) { |
1513 | 58 | ZVAL_UNDEF(result); |
1514 | 58 | } |
1515 | 97 | return FAILURE; |
1516 | 97 | } |
1517 | | |
1518 | 2.61k | retval = div_function_base(&result_copy, &op1_copy, &op2_copy); |
1519 | 2.61k | if (retval == DIV_SUCCESS) { |
1520 | 2.57k | if (result == op1) { |
1521 | 295 | zval_ptr_dtor(result); |
1522 | 295 | } |
1523 | 2.57k | ZVAL_COPY_VALUE(result, &result_copy); |
1524 | 2.57k | return SUCCESS; |
1525 | 2.57k | } |
1526 | | |
1527 | 87 | div_by_zero: |
1528 | 87 | ZEND_ASSERT(retval == DIV_BY_ZERO && "DIV_TYPES_NOT_HANDLED should not occur here"); |
1529 | 87 | if (result != op1) { |
1530 | 67 | ZVAL_UNDEF(result); |
1531 | 67 | } |
1532 | 87 | zend_throw_error(zend_ce_division_by_zero_error, "Division by zero"); |
1533 | 87 | return FAILURE; |
1534 | 87 | } |
1535 | | /* }}} */ |
1536 | | |
1537 | | ZEND_API zend_result ZEND_FASTCALL mod_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
1538 | 21.6k | { |
1539 | 21.6k | zend_long op1_lval, op2_lval; |
1540 | | |
1541 | 21.6k | convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_MOD, "%"); |
1542 | | |
1543 | 21.5k | if (op2_lval == 0) { |
1544 | | /* modulus by zero */ |
1545 | 113 | if (EG(current_execute_data) && !CG(in_compilation)) { |
1546 | 113 | zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero"); |
1547 | 113 | } else { |
1548 | 0 | zend_error_noreturn(E_ERROR, "Modulo by zero"); |
1549 | 0 | } |
1550 | 113 | if (op1 != result) { |
1551 | 65 | ZVAL_UNDEF(result); |
1552 | 65 | } |
1553 | 113 | return FAILURE; |
1554 | 113 | } |
1555 | | |
1556 | 21.4k | if (op1 == result) { |
1557 | 611 | zval_ptr_dtor(result); |
1558 | 611 | } |
1559 | | |
1560 | 21.4k | if (op2_lval == -1) { |
1561 | | /* Prevent overflow error/crash if op1==LONG_MIN */ |
1562 | 390 | ZVAL_LONG(result, 0); |
1563 | 390 | return SUCCESS; |
1564 | 390 | } |
1565 | | |
1566 | 21.0k | ZVAL_LONG(result, op1_lval % op2_lval); |
1567 | 21.0k | return SUCCESS; |
1568 | 21.4k | } |
1569 | | /* }}} */ |
1570 | | |
1571 | | ZEND_API zend_result ZEND_FASTCALL boolean_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
1572 | 7.71k | { |
1573 | 7.71k | int op1_val, op2_val; |
1574 | | |
1575 | 7.71k | do { |
1576 | 7.71k | if (Z_TYPE_P(op1) == IS_FALSE) { |
1577 | 639 | op1_val = 0; |
1578 | 7.07k | } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) { |
1579 | 491 | op1_val = 1; |
1580 | 6.58k | } else { |
1581 | 6.58k | if (Z_ISREF_P(op1)) { |
1582 | 0 | op1 = Z_REFVAL_P(op1); |
1583 | 0 | if (Z_TYPE_P(op1) == IS_FALSE) { |
1584 | 0 | op1_val = 0; |
1585 | 0 | break; |
1586 | 0 | } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) { |
1587 | 0 | op1_val = 1; |
1588 | 0 | break; |
1589 | 0 | } |
1590 | 0 | } |
1591 | 6.58k | ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BOOL_XOR); |
1592 | 6.58k | op1_val = zend_is_true(op1); |
1593 | 6.58k | } |
1594 | 7.71k | } while (0); |
1595 | 7.71k | do { |
1596 | 7.71k | if (Z_TYPE_P(op2) == IS_FALSE) { |
1597 | 308 | op2_val = 0; |
1598 | 7.40k | } else if (EXPECTED(Z_TYPE_P(op2) == IS_TRUE)) { |
1599 | 467 | op2_val = 1; |
1600 | 6.93k | } else { |
1601 | 6.93k | if (Z_ISREF_P(op2)) { |
1602 | 0 | op2 = Z_REFVAL_P(op2); |
1603 | 0 | if (Z_TYPE_P(op2) == IS_FALSE) { |
1604 | 0 | op2_val = 0; |
1605 | 0 | break; |
1606 | 0 | } else if (EXPECTED(Z_TYPE_P(op2) == IS_TRUE)) { |
1607 | 0 | op2_val = 1; |
1608 | 0 | break; |
1609 | 0 | } |
1610 | 0 | } |
1611 | 6.93k | ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BOOL_XOR); |
1612 | 6.93k | op2_val = zend_is_true(op2); |
1613 | 6.93k | } |
1614 | 7.71k | } while (0); |
1615 | | |
1616 | 7.71k | ZVAL_BOOL(result, op1_val ^ op2_val); |
1617 | 7.71k | return SUCCESS; |
1618 | 7.71k | } |
1619 | | /* }}} */ |
1620 | | |
1621 | | ZEND_API zend_result ZEND_FASTCALL boolean_not_function(zval *result, zval *op1) /* {{{ */ |
1622 | 114k | { |
1623 | 114k | if (Z_TYPE_P(op1) < IS_TRUE) { |
1624 | 11.1k | ZVAL_TRUE(result); |
1625 | 102k | } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) { |
1626 | 619 | ZVAL_FALSE(result); |
1627 | 102k | } else { |
1628 | 102k | if (Z_ISREF_P(op1)) { |
1629 | 0 | op1 = Z_REFVAL_P(op1); |
1630 | 0 | if (Z_TYPE_P(op1) < IS_TRUE) { |
1631 | 0 | ZVAL_TRUE(result); |
1632 | 0 | return SUCCESS; |
1633 | 0 | } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) { |
1634 | 0 | ZVAL_FALSE(result); |
1635 | 0 | return SUCCESS; |
1636 | 0 | } |
1637 | 0 | } |
1638 | 102k | ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BOOL_NOT); |
1639 | | |
1640 | 102k | ZVAL_BOOL(result, !zend_is_true(op1)); |
1641 | 102k | } |
1642 | 114k | return SUCCESS; |
1643 | 114k | } |
1644 | | /* }}} */ |
1645 | | |
1646 | | ZEND_API zend_result ZEND_FASTCALL bitwise_not_function(zval *result, zval *op1) /* {{{ */ |
1647 | 9.15k | { |
1648 | 9.16k | try_again: |
1649 | 9.16k | switch (Z_TYPE_P(op1)) { |
1650 | 2.04k | case IS_LONG: |
1651 | 2.04k | ZVAL_LONG(result, ~Z_LVAL_P(op1)); |
1652 | 2.04k | return SUCCESS; |
1653 | 1.44k | case IS_DOUBLE: { |
1654 | 1.44k | zend_long lval = zend_dval_to_lval_safe(Z_DVAL_P(op1)); |
1655 | 1.44k | if (EG(exception)) { |
1656 | 0 | if (result != op1) { |
1657 | 0 | ZVAL_UNDEF(result); |
1658 | 0 | } |
1659 | 0 | return FAILURE; |
1660 | 0 | } |
1661 | 1.44k | ZVAL_LONG(result, ~lval); |
1662 | 1.44k | return SUCCESS; |
1663 | 1.44k | } |
1664 | 5.60k | case IS_STRING: { |
1665 | 5.60k | size_t i; |
1666 | | |
1667 | 5.60k | if (Z_STRLEN_P(op1) == 1) { |
1668 | 138 | zend_uchar not = (zend_uchar) ~*Z_STRVAL_P(op1); |
1669 | 138 | ZVAL_CHAR(result, not); |
1670 | 5.46k | } else { |
1671 | 5.46k | ZVAL_NEW_STR(result, zend_string_alloc(Z_STRLEN_P(op1), 0)); |
1672 | 139M | for (i = 0; i < Z_STRLEN_P(op1); i++) { |
1673 | 139M | Z_STRVAL_P(result)[i] = ~Z_STRVAL_P(op1)[i]; |
1674 | 139M | } |
1675 | 5.46k | Z_STRVAL_P(result)[i] = 0; |
1676 | 5.46k | } |
1677 | 5.60k | return SUCCESS; |
1678 | 1.44k | } |
1679 | 19 | case IS_REFERENCE: |
1680 | 19 | op1 = Z_REFVAL_P(op1); |
1681 | 19 | goto try_again; |
1682 | 65 | default: |
1683 | 65 | ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BW_NOT); |
1684 | | |
1685 | 65 | if (result != op1) { |
1686 | 65 | ZVAL_UNDEF(result); |
1687 | 65 | } |
1688 | 65 | zend_type_error("Cannot perform bitwise not on %s", zend_zval_value_name(op1)); |
1689 | 65 | return FAILURE; |
1690 | 9.16k | } |
1691 | 9.16k | } |
1692 | | /* }}} */ |
1693 | | |
1694 | | ZEND_API zend_result ZEND_FASTCALL bitwise_or_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
1695 | 20.5k | { |
1696 | 20.5k | zend_long op1_lval, op2_lval; |
1697 | | |
1698 | 20.5k | if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) { |
1699 | 886 | ZVAL_LONG(result, Z_LVAL_P(op1) | Z_LVAL_P(op2)); |
1700 | 886 | return SUCCESS; |
1701 | 886 | } |
1702 | | |
1703 | 19.6k | ZVAL_DEREF(op1); |
1704 | 19.6k | ZVAL_DEREF(op2); |
1705 | | |
1706 | 19.6k | if (Z_TYPE_P(op1) == IS_STRING && EXPECTED(Z_TYPE_P(op2) == IS_STRING)) { |
1707 | 15.4k | zval *longer, *shorter; |
1708 | 15.4k | zend_string *str; |
1709 | 15.4k | size_t i; |
1710 | | |
1711 | 15.4k | if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) { |
1712 | 10.3k | if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) { |
1713 | 267 | zend_uchar or = (zend_uchar) (*Z_STRVAL_P(op1) | *Z_STRVAL_P(op2)); |
1714 | 267 | if (result==op1) { |
1715 | 5 | zval_ptr_dtor_str(result); |
1716 | 5 | } |
1717 | 267 | ZVAL_CHAR(result, or); |
1718 | 267 | return SUCCESS; |
1719 | 267 | } |
1720 | 10.0k | longer = op1; |
1721 | 10.0k | shorter = op2; |
1722 | 10.0k | } else { |
1723 | 5.11k | longer = op2; |
1724 | 5.11k | shorter = op1; |
1725 | 5.11k | } |
1726 | | |
1727 | 15.1k | str = zend_string_alloc(Z_STRLEN_P(longer), 0); |
1728 | 4.44M | for (i = 0; i < Z_STRLEN_P(shorter); i++) { |
1729 | 4.42M | ZSTR_VAL(str)[i] = Z_STRVAL_P(longer)[i] | Z_STRVAL_P(shorter)[i]; |
1730 | 4.42M | } |
1731 | 15.1k | memcpy(ZSTR_VAL(str) + i, Z_STRVAL_P(longer) + i, Z_STRLEN_P(longer) - i + 1); |
1732 | 15.1k | if (result==op1) { |
1733 | 19 | zval_ptr_dtor_str(result); |
1734 | 19 | } |
1735 | 15.1k | ZVAL_NEW_STR(result, str); |
1736 | 15.1k | return SUCCESS; |
1737 | 15.4k | } |
1738 | | |
1739 | 4.21k | if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) { |
1740 | 2.37k | bool failed; |
1741 | 2.37k | ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_OR); |
1742 | 2.37k | op1_lval = zendi_try_get_long(op1, &failed); |
1743 | 2.37k | if (UNEXPECTED(failed)) { |
1744 | 183 | zend_binop_error("|", op1, op2); |
1745 | 183 | if (result != op1) { |
1746 | 165 | ZVAL_UNDEF(result); |
1747 | 165 | } |
1748 | 183 | return FAILURE; |
1749 | 183 | } |
1750 | 2.37k | } else { |
1751 | 1.84k | op1_lval = Z_LVAL_P(op1); |
1752 | 1.84k | } |
1753 | 4.03k | if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) { |
1754 | 2.09k | bool failed; |
1755 | 2.09k | ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_OR); |
1756 | 2.09k | op2_lval = zendi_try_get_long(op2, &failed); |
1757 | 2.09k | if (UNEXPECTED(failed)) { |
1758 | 29 | zend_binop_error("|", op1, op2); |
1759 | 29 | if (result != op1) { |
1760 | 22 | ZVAL_UNDEF(result); |
1761 | 22 | } |
1762 | 29 | return FAILURE; |
1763 | 29 | } |
1764 | 2.09k | } else { |
1765 | 1.93k | op2_lval = Z_LVAL_P(op2); |
1766 | 1.93k | } |
1767 | | |
1768 | 4.00k | if (op1 == result) { |
1769 | 61 | zval_ptr_dtor(result); |
1770 | 61 | } |
1771 | 4.00k | ZVAL_LONG(result, op1_lval | op2_lval); |
1772 | 4.00k | return SUCCESS; |
1773 | 4.03k | } |
1774 | | /* }}} */ |
1775 | | |
1776 | | ZEND_API zend_result ZEND_FASTCALL bitwise_and_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
1777 | 321k | { |
1778 | 321k | zend_long op1_lval, op2_lval; |
1779 | | |
1780 | 321k | if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) { |
1781 | 13.9k | ZVAL_LONG(result, Z_LVAL_P(op1) & Z_LVAL_P(op2)); |
1782 | 13.9k | return SUCCESS; |
1783 | 13.9k | } |
1784 | | |
1785 | 307k | ZVAL_DEREF(op1); |
1786 | 307k | ZVAL_DEREF(op2); |
1787 | | |
1788 | 307k | if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) { |
1789 | 289k | zval *longer, *shorter; |
1790 | 289k | zend_string *str; |
1791 | 289k | size_t i; |
1792 | | |
1793 | 289k | if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) { |
1794 | 77.7k | if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) { |
1795 | 59 | zend_uchar and = (zend_uchar) (*Z_STRVAL_P(op1) & *Z_STRVAL_P(op2)); |
1796 | 59 | if (result==op1) { |
1797 | 29 | zval_ptr_dtor_str(result); |
1798 | 29 | } |
1799 | 59 | ZVAL_CHAR(result, and); |
1800 | 59 | return SUCCESS; |
1801 | 59 | } |
1802 | 77.7k | longer = op1; |
1803 | 77.7k | shorter = op2; |
1804 | 211k | } else { |
1805 | 211k | longer = op2; |
1806 | 211k | shorter = op1; |
1807 | 211k | } |
1808 | | |
1809 | 288k | str = zend_string_alloc(Z_STRLEN_P(shorter), 0); |
1810 | 2.04G | for (i = 0; i < Z_STRLEN_P(shorter); i++) { |
1811 | 2.04G | ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] & Z_STRVAL_P(longer)[i]; |
1812 | 2.04G | } |
1813 | 288k | ZSTR_VAL(str)[i] = 0; |
1814 | 288k | if (result==op1) { |
1815 | 190k | zval_ptr_dtor_str(result); |
1816 | 190k | } |
1817 | 288k | ZVAL_NEW_STR(result, str); |
1818 | 288k | return SUCCESS; |
1819 | 289k | } |
1820 | | |
1821 | 18.3k | if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) { |
1822 | 9.66k | bool failed; |
1823 | 9.66k | ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_AND); |
1824 | 9.66k | op1_lval = zendi_try_get_long(op1, &failed); |
1825 | 9.66k | if (UNEXPECTED(failed)) { |
1826 | 35 | zend_binop_error("&", op1, op2); |
1827 | 35 | if (result != op1) { |
1828 | 17 | ZVAL_UNDEF(result); |
1829 | 17 | } |
1830 | 35 | return FAILURE; |
1831 | 35 | } |
1832 | 9.66k | } else { |
1833 | 8.71k | op1_lval = Z_LVAL_P(op1); |
1834 | 8.71k | } |
1835 | 18.3k | if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) { |
1836 | 14.5k | bool failed; |
1837 | 14.5k | ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_AND); |
1838 | 14.5k | op2_lval = zendi_try_get_long(op2, &failed); |
1839 | 14.5k | if (UNEXPECTED(failed)) { |
1840 | 33 | zend_binop_error("&", op1, op2); |
1841 | 33 | if (result != op1) { |
1842 | 14 | ZVAL_UNDEF(result); |
1843 | 14 | } |
1844 | 33 | return FAILURE; |
1845 | 33 | } |
1846 | 14.5k | } else { |
1847 | 3.76k | op2_lval = Z_LVAL_P(op2); |
1848 | 3.76k | } |
1849 | | |
1850 | 18.3k | if (op1 == result) { |
1851 | 11.3k | zval_ptr_dtor(result); |
1852 | 11.3k | } |
1853 | 18.3k | ZVAL_LONG(result, op1_lval & op2_lval); |
1854 | 18.3k | return SUCCESS; |
1855 | 18.3k | } |
1856 | | /* }}} */ |
1857 | | |
1858 | | ZEND_API zend_result ZEND_FASTCALL bitwise_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
1859 | 91.5k | { |
1860 | 91.5k | zend_long op1_lval, op2_lval; |
1861 | | |
1862 | 91.5k | if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) { |
1863 | 925 | ZVAL_LONG(result, Z_LVAL_P(op1) ^ Z_LVAL_P(op2)); |
1864 | 925 | return SUCCESS; |
1865 | 925 | } |
1866 | | |
1867 | 90.6k | ZVAL_DEREF(op1); |
1868 | 90.6k | ZVAL_DEREF(op2); |
1869 | | |
1870 | 90.6k | if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) { |
1871 | 65.2k | zval *longer, *shorter; |
1872 | 65.2k | zend_string *str; |
1873 | 65.2k | size_t i; |
1874 | | |
1875 | 65.2k | if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) { |
1876 | 54.1k | if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) { |
1877 | 77 | zend_uchar xor = (zend_uchar) (*Z_STRVAL_P(op1) ^ *Z_STRVAL_P(op2)); |
1878 | 77 | if (result==op1) { |
1879 | 5 | zval_ptr_dtor_str(result); |
1880 | 5 | } |
1881 | 77 | ZVAL_CHAR(result, xor); |
1882 | 77 | return SUCCESS; |
1883 | 77 | } |
1884 | 54.0k | longer = op1; |
1885 | 54.0k | shorter = op2; |
1886 | 54.0k | } else { |
1887 | 11.0k | longer = op2; |
1888 | 11.0k | shorter = op1; |
1889 | 11.0k | } |
1890 | | |
1891 | 65.1k | str = zend_string_alloc(Z_STRLEN_P(shorter), 0); |
1892 | 265M | for (i = 0; i < Z_STRLEN_P(shorter); i++) { |
1893 | 265M | ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] ^ Z_STRVAL_P(longer)[i]; |
1894 | 265M | } |
1895 | 65.1k | ZSTR_VAL(str)[i] = 0; |
1896 | 65.1k | if (result==op1) { |
1897 | 7 | zval_ptr_dtor_str(result); |
1898 | 7 | } |
1899 | 65.1k | ZVAL_NEW_STR(result, str); |
1900 | 65.1k | return SUCCESS; |
1901 | 65.2k | } |
1902 | | |
1903 | 25.4k | if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) { |
1904 | 1.53k | bool failed; |
1905 | 1.53k | ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_XOR); |
1906 | 1.53k | op1_lval = zendi_try_get_long(op1, &failed); |
1907 | 1.53k | if (UNEXPECTED(failed)) { |
1908 | 37 | zend_binop_error("^", op1, op2); |
1909 | 37 | if (result != op1) { |
1910 | 22 | ZVAL_UNDEF(result); |
1911 | 22 | } |
1912 | 37 | return FAILURE; |
1913 | 37 | } |
1914 | 23.9k | } else { |
1915 | 23.9k | op1_lval = Z_LVAL_P(op1); |
1916 | 23.9k | } |
1917 | 25.4k | if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) { |
1918 | 24.4k | bool failed; |
1919 | 24.4k | ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_XOR); |
1920 | 24.4k | op2_lval = zendi_try_get_long(op2, &failed); |
1921 | 24.4k | if (UNEXPECTED(failed)) { |
1922 | 21 | zend_binop_error("^", op1, op2); |
1923 | 21 | if (result != op1) { |
1924 | 14 | ZVAL_UNDEF(result); |
1925 | 14 | } |
1926 | 21 | return FAILURE; |
1927 | 21 | } |
1928 | 24.4k | } else { |
1929 | 954 | op2_lval = Z_LVAL_P(op2); |
1930 | 954 | } |
1931 | | |
1932 | 25.4k | if (op1 == result) { |
1933 | 34 | zval_ptr_dtor(result); |
1934 | 34 | } |
1935 | 25.4k | ZVAL_LONG(result, op1_lval ^ op2_lval); |
1936 | 25.4k | return SUCCESS; |
1937 | 25.4k | } |
1938 | | /* }}} */ |
1939 | | |
1940 | | ZEND_API zend_result ZEND_FASTCALL shift_left_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
1941 | 3.69k | { |
1942 | 3.69k | zend_long op1_lval, op2_lval; |
1943 | | |
1944 | 3.69k | convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_SL, "<<"); |
1945 | | |
1946 | | /* prevent wrapping quirkiness on some processors where << 64 + x == << x */ |
1947 | 3.35k | if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) { |
1948 | 318 | if (EXPECTED(op2_lval > 0)) { |
1949 | 285 | if (op1 == result) { |
1950 | 7 | zval_ptr_dtor(result); |
1951 | 7 | } |
1952 | 285 | ZVAL_LONG(result, 0); |
1953 | 285 | return SUCCESS; |
1954 | 285 | } else { |
1955 | 33 | if (EG(current_execute_data) && !CG(in_compilation)) { |
1956 | 33 | zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number"); |
1957 | 33 | } else { |
1958 | 0 | zend_error_noreturn(E_ERROR, "Bit shift by negative number"); |
1959 | 0 | } |
1960 | 33 | if (op1 != result) { |
1961 | 18 | ZVAL_UNDEF(result); |
1962 | 18 | } |
1963 | 33 | return FAILURE; |
1964 | 33 | } |
1965 | 318 | } |
1966 | | |
1967 | 3.04k | if (op1 == result) { |
1968 | 52 | zval_ptr_dtor(result); |
1969 | 52 | } |
1970 | | |
1971 | | /* Perform shift on unsigned numbers to get well-defined wrap behavior. */ |
1972 | 3.04k | ZVAL_LONG(result, (zend_long) ((zend_ulong) op1_lval << op2_lval)); |
1973 | 3.04k | return SUCCESS; |
1974 | 3.35k | } |
1975 | | /* }}} */ |
1976 | | |
1977 | | ZEND_API zend_result ZEND_FASTCALL shift_right_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
1978 | 4.26k | { |
1979 | 4.26k | zend_long op1_lval, op2_lval; |
1980 | | |
1981 | 4.26k | convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_SR, ">>"); |
1982 | | |
1983 | | /* prevent wrapping quirkiness on some processors where >> 64 + x == >> x */ |
1984 | 4.21k | if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) { |
1985 | 963 | if (EXPECTED(op2_lval > 0)) { |
1986 | 859 | if (op1 == result) { |
1987 | 5 | zval_ptr_dtor(result); |
1988 | 5 | } |
1989 | 859 | ZVAL_LONG(result, (op1_lval < 0) ? -1 : 0); |
1990 | 859 | return SUCCESS; |
1991 | 859 | } else { |
1992 | 104 | if (EG(current_execute_data) && !CG(in_compilation)) { |
1993 | 104 | zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number"); |
1994 | 104 | } else { |
1995 | 0 | zend_error_noreturn(E_ERROR, "Bit shift by negative number"); |
1996 | 0 | } |
1997 | 104 | if (op1 != result) { |
1998 | 91 | ZVAL_UNDEF(result); |
1999 | 91 | } |
2000 | 104 | return FAILURE; |
2001 | 104 | } |
2002 | 963 | } |
2003 | | |
2004 | 3.25k | if (op1 == result) { |
2005 | 2.14k | zval_ptr_dtor(result); |
2006 | 2.14k | } |
2007 | | |
2008 | 3.25k | ZVAL_LONG(result, op1_lval >> op2_lval); |
2009 | 3.25k | return SUCCESS; |
2010 | 4.21k | } |
2011 | | /* }}} */ |
2012 | | |
2013 | | ZEND_API zend_result ZEND_FASTCALL concat_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
2014 | 1.25M | { |
2015 | 1.25M | zval *orig_op1 = op1; |
2016 | 1.25M | zend_string *op1_string, *op2_string; |
2017 | 1.25M | bool free_op1_string = false; |
2018 | 1.25M | bool free_op2_string = false; |
2019 | | |
2020 | 1.25M | do { |
2021 | 1.25M | if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) { |
2022 | 1.13M | op1_string = Z_STR_P(op1); |
2023 | 1.13M | } else { |
2024 | 113k | if (Z_ISREF_P(op1)) { |
2025 | 1.56k | op1 = Z_REFVAL_P(op1); |
2026 | 1.56k | if (Z_TYPE_P(op1) == IS_STRING) { |
2027 | 439 | op1_string = Z_STR_P(op1); |
2028 | 439 | break; |
2029 | 439 | } |
2030 | 1.56k | } |
2031 | 113k | ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_CONCAT); |
2032 | 113k | op1_string = zval_try_get_string_func(op1); |
2033 | 113k | if (UNEXPECTED(!op1_string)) { |
2034 | 59 | if (orig_op1 != result) { |
2035 | 27 | ZVAL_UNDEF(result); |
2036 | 27 | } |
2037 | 59 | return FAILURE; |
2038 | 59 | } |
2039 | 113k | free_op1_string = true; |
2040 | 113k | if (result == op1) { |
2041 | 89.2k | if (UNEXPECTED(op1 == op2)) { |
2042 | 108 | op2_string = op1_string; |
2043 | 108 | goto has_op2_string; |
2044 | 108 | } |
2045 | 89.2k | } |
2046 | 113k | } |
2047 | 1.25M | } while (0); |
2048 | 1.25M | do { |
2049 | 1.25M | if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) { |
2050 | 1.18M | op2_string = Z_STR_P(op2); |
2051 | 1.18M | } else { |
2052 | 65.5k | if (Z_ISREF_P(op2)) { |
2053 | 101 | op2 = Z_REFVAL_P(op2); |
2054 | 101 | if (Z_TYPE_P(op2) == IS_STRING) { |
2055 | 67 | op2_string = Z_STR_P(op2); |
2056 | 67 | break; |
2057 | 67 | } |
2058 | 101 | } |
2059 | | /* hold an additional reference because a userland function could free this */ |
2060 | 65.4k | if (!free_op1_string) { |
2061 | 43.2k | op1_string = zend_string_copy(op1_string); |
2062 | 43.2k | free_op1_string = true; |
2063 | 43.2k | } |
2064 | 65.4k | ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_CONCAT); |
2065 | 65.4k | op2_string = zval_try_get_string_func(op2); |
2066 | 65.4k | if (UNEXPECTED(!op2_string)) { |
2067 | 53 | zend_string_release_ex(op1_string, false); |
2068 | 53 | if (orig_op1 != result) { |
2069 | 27 | ZVAL_UNDEF(result); |
2070 | 27 | } |
2071 | 53 | return FAILURE; |
2072 | 53 | } |
2073 | 65.3k | free_op2_string = true; |
2074 | 65.3k | } |
2075 | 1.25M | } while (0); |
2076 | | |
2077 | 1.25M | has_op2_string:; |
2078 | 1.25M | if (UNEXPECTED(ZSTR_LEN(op1_string) == 0)) { |
2079 | 116k | if (EXPECTED(result != op2 || Z_TYPE_P(result) != IS_STRING)) { |
2080 | 116k | if (result == orig_op1) { |
2081 | 104k | i_zval_ptr_dtor(result); |
2082 | 104k | } |
2083 | 116k | if (free_op2_string) { |
2084 | | /* transfer ownership of op2_string */ |
2085 | 26.7k | ZVAL_STR(result, op2_string); |
2086 | 26.7k | free_op2_string = false; |
2087 | 89.7k | } else { |
2088 | 89.7k | ZVAL_STR_COPY(result, op2_string); |
2089 | 89.7k | } |
2090 | 116k | } |
2091 | 1.13M | } else if (UNEXPECTED(ZSTR_LEN(op2_string) == 0)) { |
2092 | 23.9k | if (EXPECTED(result != op1 || Z_TYPE_P(result) != IS_STRING)) { |
2093 | 6.64k | if (result == orig_op1) { |
2094 | 1.04k | i_zval_ptr_dtor(result); |
2095 | 1.04k | } |
2096 | 6.64k | if (free_op1_string) { |
2097 | | /* transfer ownership of op1_string */ |
2098 | 6.44k | ZVAL_STR(result, op1_string); |
2099 | 6.44k | free_op1_string = false; |
2100 | 6.44k | } else { |
2101 | 202 | ZVAL_STR_COPY(result, op1_string); |
2102 | 202 | } |
2103 | 6.64k | } |
2104 | 1.11M | } else { |
2105 | 1.11M | size_t op1_len = ZSTR_LEN(op1_string); |
2106 | 1.11M | size_t op2_len = ZSTR_LEN(op2_string); |
2107 | 1.11M | size_t result_len = op1_len + op2_len; |
2108 | 1.11M | zend_string *result_str; |
2109 | 1.11M | uint32_t flags = ZSTR_GET_COPYABLE_CONCAT_PROPERTIES_BOTH(op1_string, op2_string); |
2110 | | |
2111 | 1.11M | if (UNEXPECTED(op1_len > ZSTR_MAX_LEN - op2_len)) { |
2112 | 0 | if (free_op1_string) zend_string_release_ex(op1_string, false); |
2113 | 0 | if (free_op2_string) zend_string_release_ex(op2_string, false); |
2114 | 0 | zend_throw_error(NULL, "String size overflow"); |
2115 | 0 | if (orig_op1 != result) { |
2116 | 0 | ZVAL_UNDEF(result); |
2117 | 0 | } |
2118 | 0 | return FAILURE; |
2119 | 0 | } |
2120 | | |
2121 | 1.11M | if (result == op1) { |
2122 | | /* Destroy the old result first to drop the refcount, such that $x .= ...; may happen in-place. */ |
2123 | 1.09M | if (free_op1_string) { |
2124 | | /* op1_string will be used as the result, so we should not free it */ |
2125 | 18.5k | i_zval_ptr_dtor(result); |
2126 | | /* Set it to NULL in case that the extension will throw an out-of-memory error. |
2127 | | * Otherwise the shutdown sequence will try to free this again. */ |
2128 | 18.5k | ZVAL_NULL(result); |
2129 | 18.5k | free_op1_string = false; |
2130 | 18.5k | } |
2131 | | /* special case, perform operations on result */ |
2132 | 1.09M | result_str = zend_string_extend(op1_string, result_len, 0); |
2133 | | /* account for the case where result_str == op1_string == op2_string and the realloc is done */ |
2134 | 1.09M | if (op1_string == op2_string) { |
2135 | 31.5k | if (free_op2_string) { |
2136 | 1.72k | zend_string_release_ex(op2_string, false); |
2137 | 1.72k | free_op2_string = false; |
2138 | 1.72k | } |
2139 | 31.5k | op2_string = result_str; |
2140 | 31.5k | } |
2141 | 1.09M | } else { |
2142 | 20.6k | result_str = zend_string_alloc(result_len, 0); |
2143 | 20.6k | memcpy(ZSTR_VAL(result_str), ZSTR_VAL(op1_string), op1_len); |
2144 | 20.6k | if (result == orig_op1) { |
2145 | 0 | i_zval_ptr_dtor(result); |
2146 | 0 | } |
2147 | 20.6k | } |
2148 | 1.11M | GC_ADD_FLAGS(result_str, flags); |
2149 | | |
2150 | 1.11M | ZVAL_NEW_STR(result, result_str); |
2151 | 1.11M | memcpy(ZSTR_VAL(result_str) + op1_len, ZSTR_VAL(op2_string), op2_len); |
2152 | 1.11M | ZSTR_VAL(result_str)[result_len] = '\0'; |
2153 | 1.11M | } |
2154 | | |
2155 | 1.25M | if (free_op1_string) zend_string_release_ex(op1_string, false); |
2156 | 1.25M | if (free_op2_string) zend_string_release_ex(op2_string, false); |
2157 | | |
2158 | 1.25M | return SUCCESS; |
2159 | 1.25M | } |
2160 | | /* }}} */ |
2161 | | |
2162 | | ZEND_API int ZEND_FASTCALL string_compare_function_ex(zval *op1, zval *op2, bool case_insensitive) /* {{{ */ |
2163 | 0 | { |
2164 | 0 | zend_string *tmp_str1, *tmp_str2; |
2165 | 0 | zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1); |
2166 | 0 | zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2); |
2167 | 0 | int ret; |
2168 | |
|
2169 | 0 | if (case_insensitive) { |
2170 | 0 | ret = zend_binary_strcasecmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2)); |
2171 | 0 | } else { |
2172 | 0 | ret = zend_binary_strcmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2)); |
2173 | 0 | } |
2174 | |
|
2175 | 0 | zend_tmp_string_release(tmp_str1); |
2176 | 0 | zend_tmp_string_release(tmp_str2); |
2177 | 0 | return ret; |
2178 | 0 | } |
2179 | | /* }}} */ |
2180 | | |
2181 | | ZEND_API int ZEND_FASTCALL string_compare_function(zval *op1, zval *op2) /* {{{ */ |
2182 | 102k | { |
2183 | 102k | if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) && |
2184 | 47.8k | EXPECTED(Z_TYPE_P(op2) == IS_STRING)) { |
2185 | 46.0k | if (Z_STR_P(op1) == Z_STR_P(op2)) { |
2186 | 5.99k | return 0; |
2187 | 40.1k | } else { |
2188 | 40.1k | return zend_binary_strcmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2)); |
2189 | 40.1k | } |
2190 | 56.6k | } else { |
2191 | 56.6k | zend_string *tmp_str1, *tmp_str2; |
2192 | 56.6k | zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1); |
2193 | 56.6k | zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2); |
2194 | 56.6k | int ret = zend_binary_strcmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2)); |
2195 | | |
2196 | 56.6k | zend_tmp_string_release(tmp_str1); |
2197 | 56.6k | zend_tmp_string_release(tmp_str2); |
2198 | 56.6k | return ret; |
2199 | 56.6k | } |
2200 | 102k | } |
2201 | | /* }}} */ |
2202 | | |
2203 | | ZEND_API int ZEND_FASTCALL string_case_compare_function(zval *op1, zval *op2) /* {{{ */ |
2204 | 27 | { |
2205 | 27 | if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) && |
2206 | 26 | EXPECTED(Z_TYPE_P(op2) == IS_STRING)) { |
2207 | 18 | if (Z_STR_P(op1) == Z_STR_P(op2)) { |
2208 | 0 | return 0; |
2209 | 18 | } else { |
2210 | 18 | return zend_binary_strcasecmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2)); |
2211 | 18 | } |
2212 | 18 | } else { |
2213 | 9 | zend_string *tmp_str1, *tmp_str2; |
2214 | 9 | zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1); |
2215 | 9 | zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2); |
2216 | 9 | int ret = zend_binary_strcasecmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2)); |
2217 | | |
2218 | 9 | zend_tmp_string_release(tmp_str1); |
2219 | 9 | zend_tmp_string_release(tmp_str2); |
2220 | 9 | return ret; |
2221 | 9 | } |
2222 | 27 | } |
2223 | | /* }}} */ |
2224 | | |
2225 | | ZEND_API int ZEND_FASTCALL string_locale_compare_function(zval *op1, zval *op2) /* {{{ */ |
2226 | 1.71k | { |
2227 | 1.71k | zend_string *tmp_str1, *tmp_str2; |
2228 | 1.71k | zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1); |
2229 | 1.71k | zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2); |
2230 | 1.71k | int ret = strcoll(ZSTR_VAL(str1), ZSTR_VAL(str2)); |
2231 | | |
2232 | 1.71k | zend_tmp_string_release(tmp_str1); |
2233 | 1.71k | zend_tmp_string_release(tmp_str2); |
2234 | 1.71k | return ret; |
2235 | 1.71k | } |
2236 | | /* }}} */ |
2237 | | |
2238 | | ZEND_API int ZEND_FASTCALL numeric_compare_function(zval *op1, zval *op2) /* {{{ */ |
2239 | 8.35k | { |
2240 | 8.35k | double d1, d2; |
2241 | | |
2242 | 8.35k | d1 = zval_get_double(op1); |
2243 | 8.35k | d2 = zval_get_double(op2); |
2244 | | |
2245 | 8.35k | return ZEND_THREEWAY_COMPARE(d1, d2); |
2246 | 8.35k | } |
2247 | | /* }}} */ |
2248 | | |
2249 | | ZEND_API zend_result ZEND_FASTCALL compare_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
2250 | 304 | { |
2251 | 304 | ZVAL_LONG(result, zend_compare(op1, op2)); |
2252 | 304 | return SUCCESS; |
2253 | 304 | } |
2254 | | /* }}} */ |
2255 | | |
2256 | | static int compare_long_to_string(zend_long lval, zend_string *str) /* {{{ */ |
2257 | 16.9k | { |
2258 | 16.9k | zend_long str_lval; |
2259 | 16.9k | double str_dval; |
2260 | 16.9k | uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &str_lval, &str_dval, 0); |
2261 | | |
2262 | 16.9k | if (type == IS_LONG) { |
2263 | 4.16k | return lval > str_lval ? 1 : lval < str_lval ? -1 : 0; |
2264 | 4.16k | } |
2265 | | |
2266 | 12.8k | if (type == IS_DOUBLE) { |
2267 | 686 | return ZEND_THREEWAY_COMPARE((double) lval, str_dval); |
2268 | 686 | } |
2269 | | |
2270 | 12.1k | zend_string *lval_as_str = zend_long_to_str(lval); |
2271 | 12.1k | int cmp_result = zend_binary_strcmp( |
2272 | 12.1k | ZSTR_VAL(lval_as_str), ZSTR_LEN(lval_as_str), ZSTR_VAL(str), ZSTR_LEN(str)); |
2273 | 12.1k | zend_string_release(lval_as_str); |
2274 | 12.1k | return ZEND_NORMALIZE_BOOL(cmp_result); |
2275 | 12.8k | } |
2276 | | /* }}} */ |
2277 | | |
2278 | | static int compare_double_to_string(double dval, zend_string *str) /* {{{ */ |
2279 | 3.81k | { |
2280 | 3.81k | zend_long str_lval; |
2281 | 3.81k | double str_dval; |
2282 | 3.81k | uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &str_lval, &str_dval, 0); |
2283 | | |
2284 | 3.81k | ZEND_ASSERT(!zend_isnan(dval)); |
2285 | | |
2286 | 3.81k | if (type == IS_LONG) { |
2287 | 992 | return ZEND_THREEWAY_COMPARE(dval, (double) str_lval); |
2288 | 992 | } |
2289 | | |
2290 | 2.82k | if (type == IS_DOUBLE) { |
2291 | 858 | return ZEND_THREEWAY_COMPARE(dval, str_dval); |
2292 | 858 | } |
2293 | | |
2294 | 1.96k | zend_string *dval_as_str = zend_double_to_str(dval); |
2295 | 1.96k | int cmp_result = zend_binary_strcmp( |
2296 | 1.96k | ZSTR_VAL(dval_as_str), ZSTR_LEN(dval_as_str), ZSTR_VAL(str), ZSTR_LEN(str)); |
2297 | 1.96k | zend_string_release(dval_as_str); |
2298 | 1.96k | return ZEND_NORMALIZE_BOOL(cmp_result); |
2299 | 2.82k | } |
2300 | | /* }}} */ |
2301 | | |
2302 | | ZEND_API int ZEND_FASTCALL zend_compare(zval *op1, zval *op2) /* {{{ */ |
2303 | 497k | { |
2304 | 497k | bool converted = false; |
2305 | 497k | zval op1_copy, op2_copy; |
2306 | | |
2307 | 508k | while (1) { |
2308 | 508k | switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) { |
2309 | 116k | case TYPE_PAIR(IS_LONG, IS_LONG): |
2310 | 116k | return Z_LVAL_P(op1)>Z_LVAL_P(op2)?1:(Z_LVAL_P(op1)<Z_LVAL_P(op2)?-1:0); |
2311 | | |
2312 | 3.23k | case TYPE_PAIR(IS_DOUBLE, IS_LONG): |
2313 | 3.23k | return ZEND_THREEWAY_COMPARE(Z_DVAL_P(op1), (double)Z_LVAL_P(op2)); |
2314 | | |
2315 | 1.96k | case TYPE_PAIR(IS_LONG, IS_DOUBLE): |
2316 | 1.96k | return ZEND_THREEWAY_COMPARE((double)Z_LVAL_P(op1), Z_DVAL_P(op2)); |
2317 | | |
2318 | 677 | case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE): |
2319 | 677 | return ZEND_THREEWAY_COMPARE(Z_DVAL_P(op1), Z_DVAL_P(op2)); |
2320 | | |
2321 | 5.47k | case TYPE_PAIR(IS_ARRAY, IS_ARRAY): |
2322 | 5.47k | return zend_compare_arrays(op1, op2); |
2323 | | |
2324 | 3.68k | case TYPE_PAIR(IS_NULL, IS_NULL): |
2325 | 5.07k | case TYPE_PAIR(IS_NULL, IS_FALSE): |
2326 | 19.9k | case TYPE_PAIR(IS_FALSE, IS_NULL): |
2327 | 20.7k | case TYPE_PAIR(IS_FALSE, IS_FALSE): |
2328 | 21.2k | case TYPE_PAIR(IS_TRUE, IS_TRUE): |
2329 | 21.2k | return 0; |
2330 | | |
2331 | 2.05k | case TYPE_PAIR(IS_NULL, IS_TRUE): |
2332 | 2.05k | return -1; |
2333 | | |
2334 | 348 | case TYPE_PAIR(IS_TRUE, IS_NULL): |
2335 | 348 | return 1; |
2336 | | |
2337 | 10.5k | case TYPE_PAIR(IS_STRING, IS_STRING): |
2338 | 10.5k | if (Z_STR_P(op1) == Z_STR_P(op2)) { |
2339 | 2.15k | return 0; |
2340 | 2.15k | } |
2341 | 8.38k | return zendi_smart_strcmp(Z_STR_P(op1), Z_STR_P(op2)); |
2342 | | |
2343 | 8.08k | case TYPE_PAIR(IS_NULL, IS_STRING): |
2344 | 8.08k | return Z_STRLEN_P(op2) == 0 ? 0 : -1; |
2345 | | |
2346 | 17.2k | case TYPE_PAIR(IS_STRING, IS_NULL): |
2347 | 17.2k | return Z_STRLEN_P(op1) == 0 ? 0 : 1; |
2348 | | |
2349 | 12.1k | case TYPE_PAIR(IS_LONG, IS_STRING): |
2350 | 12.1k | return compare_long_to_string(Z_LVAL_P(op1), Z_STR_P(op2)); |
2351 | | |
2352 | 4.82k | case TYPE_PAIR(IS_STRING, IS_LONG): |
2353 | 4.82k | return -compare_long_to_string(Z_LVAL_P(op2), Z_STR_P(op1)); |
2354 | | |
2355 | 1.97k | case TYPE_PAIR(IS_DOUBLE, IS_STRING): |
2356 | 1.97k | if (zend_isnan(Z_DVAL_P(op1))) { |
2357 | 72 | return 1; |
2358 | 72 | } |
2359 | | |
2360 | 1.90k | return compare_double_to_string(Z_DVAL_P(op1), Z_STR_P(op2)); |
2361 | | |
2362 | 2.00k | case TYPE_PAIR(IS_STRING, IS_DOUBLE): |
2363 | 2.00k | if (zend_isnan(Z_DVAL_P(op2))) { |
2364 | 95 | return 1; |
2365 | 95 | } |
2366 | | |
2367 | 1.90k | return -compare_double_to_string(Z_DVAL_P(op2), Z_STR_P(op1)); |
2368 | | |
2369 | 162 | case TYPE_PAIR(IS_OBJECT, IS_NULL): |
2370 | 162 | return 1; |
2371 | | |
2372 | 492 | case TYPE_PAIR(IS_NULL, IS_OBJECT): |
2373 | 492 | return -1; |
2374 | | |
2375 | 299k | default: |
2376 | 299k | if (Z_ISREF_P(op1)) { |
2377 | 2.82k | op1 = Z_REFVAL_P(op1); |
2378 | 2.82k | continue; |
2379 | 296k | } else if (Z_ISREF_P(op2)) { |
2380 | 1.52k | op2 = Z_REFVAL_P(op2); |
2381 | 1.52k | continue; |
2382 | 1.52k | } |
2383 | | |
2384 | 295k | if (Z_TYPE_P(op1) == IS_OBJECT |
2385 | 293k | || Z_TYPE_P(op2) == IS_OBJECT) { |
2386 | 2.12k | zval *object, *other; |
2387 | 2.12k | if (Z_TYPE_P(op1) == IS_OBJECT) { |
2388 | 1.89k | object = op1; |
2389 | 1.89k | other = op2; |
2390 | 1.89k | } else { |
2391 | 234 | object = op2; |
2392 | 234 | other = op1; |
2393 | 234 | } |
2394 | 2.12k | if (EXPECTED(Z_TYPE_P(other) == IS_OBJECT)) { |
2395 | 999 | if (Z_OBJ_P(object) == Z_OBJ_P(other)) { |
2396 | 147 | return 0; |
2397 | 147 | } |
2398 | 1.12k | } else if (Z_TYPE_P(other) == IS_TRUE || Z_TYPE_P(other) == IS_FALSE) { |
2399 | 351 | zval casted; |
2400 | 351 | if (Z_OBJ_HANDLER_P(object, cast_object)(Z_OBJ_P(object), &casted, _IS_BOOL) == FAILURE) { |
2401 | 0 | return object == op1 ? 1 : -1; |
2402 | 0 | } |
2403 | 351 | int ret = object == op1 ? zend_compare(&casted, other) : zend_compare(other, &casted); |
2404 | 351 | ZEND_ASSERT(!Z_REFCOUNTED_P(&casted)); |
2405 | 351 | return ret; |
2406 | 351 | } |
2407 | 1.62k | return Z_OBJ_HANDLER_P(object, compare)(op1, op2); |
2408 | 2.12k | } |
2409 | | |
2410 | 292k | if (!converted) { |
2411 | | /* Handle NAN */ |
2412 | 286k | if (UNEXPECTED( |
2413 | 286k | (Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1))) |
2414 | 286k | || (Z_TYPE_P(op2) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op2))) |
2415 | 286k | )) { |
2416 | | // TODO: NAN should always be uncomparable |
2417 | | /* NAN used be cast to TRUE so handle this manually for the time being */ |
2418 | 390 | if (Z_TYPE_P(op1) < IS_TRUE) { |
2419 | 54 | return -1; |
2420 | 336 | } else if (Z_TYPE_P(op1) == IS_TRUE || Z_TYPE_P(op2) == IS_TRUE) { |
2421 | 95 | return 0; |
2422 | 241 | } else if (Z_TYPE_P(op2) < IS_TRUE) { |
2423 | 153 | return 1; |
2424 | 153 | } else if (Z_TYPE_P(op1) != IS_DOUBLE) { |
2425 | 26 | op1 = _zendi_convert_scalar_to_number_silent(op1, &op1_copy); |
2426 | 26 | converted = true; |
2427 | 62 | } else if (Z_TYPE_P(op2) != IS_DOUBLE) { |
2428 | 62 | op2 = _zendi_convert_scalar_to_number_silent(op2, &op2_copy); |
2429 | 62 | converted = true; |
2430 | 62 | } |
2431 | 286k | } else if (Z_TYPE_P(op1) < IS_TRUE) { |
2432 | 209k | return zend_is_true(op2) ? -1 : 0; |
2433 | 209k | } else if (Z_TYPE_P(op1) == IS_TRUE) { |
2434 | 790 | return zend_is_true(op2) ? 0 : 1; |
2435 | 76.4k | } else if (Z_TYPE_P(op2) < IS_TRUE) { |
2436 | 17.9k | return zend_is_true(op1) ? 1 : 0; |
2437 | 58.4k | } else if (Z_TYPE_P(op2) == IS_TRUE) { |
2438 | 52.3k | return zend_is_true(op1) ? 0 : -1; |
2439 | 52.3k | } else { |
2440 | 6.13k | op1 = _zendi_convert_scalar_to_number_silent(op1, &op1_copy); |
2441 | 6.13k | op2 = _zendi_convert_scalar_to_number_silent(op2, &op2_copy); |
2442 | 6.13k | if (EG(exception)) { |
2443 | 0 | return 1; /* to stop comparison of arrays */ |
2444 | 0 | } |
2445 | 6.13k | converted = true; |
2446 | 6.13k | } |
2447 | 286k | } else if (Z_TYPE_P(op1)==IS_ARRAY) { |
2448 | 4.15k | return 1; |
2449 | 4.15k | } else if (Z_TYPE_P(op2)==IS_ARRAY) { |
2450 | 2.06k | return -1; |
2451 | 2.06k | } else { |
2452 | 0 | ZEND_UNREACHABLE(); |
2453 | 0 | zend_throw_error(NULL, "Unsupported operand types"); |
2454 | 0 | return 1; |
2455 | 0 | } |
2456 | 508k | } |
2457 | 508k | } |
2458 | 497k | } |
2459 | | /* }}} */ |
2460 | | |
2461 | | /* return int to be compatible with compare_func_t */ |
2462 | | static int hash_zval_identical_function(zval *z1, zval *z2) /* {{{ */ |
2463 | 663 | { |
2464 | | /* is_identical_function() returns 1 in case of identity and 0 in case |
2465 | | * of a difference; |
2466 | | * whereas this comparison function is expected to return 0 on identity, |
2467 | | * and non zero otherwise. |
2468 | | */ |
2469 | 663 | ZVAL_DEREF(z1); |
2470 | 663 | ZVAL_DEREF(z2); |
2471 | 663 | return fast_is_not_identical_function(z1, z2); |
2472 | 663 | } |
2473 | | /* }}} */ |
2474 | | |
2475 | | ZEND_API bool ZEND_FASTCALL zend_is_identical(const zval *op1, const zval *op2) /* {{{ */ |
2476 | 109k | { |
2477 | 109k | if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) { |
2478 | 15.2k | return 0; |
2479 | 15.2k | } |
2480 | 93.8k | switch (Z_TYPE_P(op1)) { |
2481 | 159 | case IS_NULL: |
2482 | 526 | case IS_FALSE: |
2483 | 51.3k | case IS_TRUE: |
2484 | 51.3k | return 1; |
2485 | 9.04k | case IS_LONG: |
2486 | 9.04k | return (Z_LVAL_P(op1) == Z_LVAL_P(op2)); |
2487 | 0 | case IS_RESOURCE: |
2488 | 0 | return (Z_RES_P(op1) == Z_RES_P(op2)); |
2489 | 692 | case IS_DOUBLE: |
2490 | 692 | return (Z_DVAL_P(op1) == Z_DVAL_P(op2)); |
2491 | 30.6k | case IS_STRING: |
2492 | 30.6k | return zend_string_equals(Z_STR_P(op1), Z_STR_P(op2)); |
2493 | 1.64k | case IS_ARRAY: |
2494 | 1.64k | return (Z_ARRVAL_P(op1) == Z_ARRVAL_P(op2) || |
2495 | 1.45k | zend_hash_compare(Z_ARRVAL_P(op1), Z_ARRVAL_P(op2), (compare_func_t) hash_zval_identical_function, 1) == 0); |
2496 | 460 | case IS_OBJECT: |
2497 | 460 | return (Z_OBJ_P(op1) == Z_OBJ_P(op2)); |
2498 | 0 | default: |
2499 | 0 | return 0; |
2500 | 93.8k | } |
2501 | 93.8k | } |
2502 | | /* }}} */ |
2503 | | |
2504 | | ZEND_API zend_result ZEND_FASTCALL is_identical_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
2505 | 24.2k | { |
2506 | 24.2k | ZVAL_BOOL(result, zend_is_identical(op1, op2)); |
2507 | 24.2k | return SUCCESS; |
2508 | 24.2k | } |
2509 | | /* }}} */ |
2510 | | |
2511 | | ZEND_API zend_result ZEND_FASTCALL is_not_identical_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
2512 | 44.0k | { |
2513 | 44.0k | ZVAL_BOOL(result, !zend_is_identical(op1, op2)); |
2514 | 44.0k | return SUCCESS; |
2515 | 44.0k | } |
2516 | | /* }}} */ |
2517 | | |
2518 | | ZEND_API zend_result ZEND_FASTCALL is_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
2519 | 16.0k | { |
2520 | 16.0k | ZVAL_BOOL(result, zend_compare(op1, op2) == 0); |
2521 | 16.0k | return SUCCESS; |
2522 | 16.0k | } |
2523 | | /* }}} */ |
2524 | | |
2525 | | ZEND_API zend_result ZEND_FASTCALL is_not_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
2526 | 1.40k | { |
2527 | 1.40k | ZVAL_BOOL(result, (zend_compare(op1, op2) != 0)); |
2528 | 1.40k | return SUCCESS; |
2529 | 1.40k | } |
2530 | | /* }}} */ |
2531 | | |
2532 | | ZEND_API zend_result ZEND_FASTCALL is_smaller_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
2533 | 84.1k | { |
2534 | 84.1k | ZVAL_BOOL(result, (zend_compare(op1, op2) < 0)); |
2535 | 84.1k | return SUCCESS; |
2536 | 84.1k | } |
2537 | | /* }}} */ |
2538 | | |
2539 | | ZEND_API zend_result ZEND_FASTCALL is_smaller_or_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */ |
2540 | 37.3k | { |
2541 | 37.3k | ZVAL_BOOL(result, (zend_compare(op1, op2) <= 0)); |
2542 | 37.3k | return SUCCESS; |
2543 | 37.3k | } |
2544 | | /* }}} */ |
2545 | | |
2546 | | ZEND_API bool ZEND_FASTCALL zend_class_implements_interface(const zend_class_entry *class_ce, const zend_class_entry *interface_ce) /* {{{ */ |
2547 | 3.73k | { |
2548 | 3.73k | uint32_t i; |
2549 | 3.73k | ZEND_ASSERT(interface_ce->ce_flags & ZEND_ACC_INTERFACE); |
2550 | | |
2551 | 3.73k | if (class_ce->num_interfaces) { |
2552 | 3.67k | ZEND_ASSERT(class_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES); |
2553 | 10.3k | for (i = 0; i < class_ce->num_interfaces; i++) { |
2554 | 8.00k | if (class_ce->interfaces[i] == interface_ce) { |
2555 | 1.34k | return 1; |
2556 | 1.34k | } |
2557 | 8.00k | } |
2558 | 3.67k | } |
2559 | 2.38k | return 0; |
2560 | 3.73k | } |
2561 | | /* }}} */ |
2562 | | |
2563 | | ZEND_API bool ZEND_FASTCALL instanceof_function_slow(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */ |
2564 | 16.6M | { |
2565 | 16.6M | ZEND_ASSERT(instance_ce != ce && "Should have been checked already"); |
2566 | 16.6M | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
2567 | 1.01M | uint32_t i; |
2568 | | |
2569 | 1.01M | if (instance_ce->num_interfaces) { |
2570 | 1.01M | ZEND_ASSERT(instance_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES); |
2571 | 2.01M | for (i = 0; i < instance_ce->num_interfaces; i++) { |
2572 | 2.01M | if (instance_ce->interfaces[i] == ce) { |
2573 | 1.01M | return 1; |
2574 | 1.01M | } |
2575 | 2.01M | } |
2576 | 1.01M | } |
2577 | 1.63k | return 0; |
2578 | 15.6M | } else { |
2579 | 43.8M | while (1) { |
2580 | 43.8M | instance_ce = instance_ce->parent; |
2581 | 43.8M | if (instance_ce == ce) { |
2582 | 1.61M | return 1; |
2583 | 1.61M | } |
2584 | 42.2M | if (instance_ce == NULL) { |
2585 | 14.0M | return 0; |
2586 | 14.0M | } |
2587 | 42.2M | } |
2588 | 15.6M | } |
2589 | 16.6M | } |
2590 | | /* }}} */ |
2591 | | |
2592 | 316 | #define LOWER_CASE 1 |
2593 | 140 | #define UPPER_CASE 2 |
2594 | 1.01k | #define NUMERIC 3 |
2595 | | |
2596 | | ZEND_API bool zend_string_only_has_ascii_alphanumeric(const zend_string *str) |
2597 | 0 | { |
2598 | 0 | const char *p = ZSTR_VAL(str); |
2599 | 0 | const char *e = ZSTR_VAL(str) + ZSTR_LEN(str); |
2600 | 0 | while (p < e) { |
2601 | 0 | char c = *p++; |
2602 | 0 | if (UNEXPECTED( c < '0' || c > 'z' || (c < 'a' && c > 'Z') || (c < 'A' && c > '9') ) ) { |
2603 | 0 | return false; |
2604 | 0 | } |
2605 | 0 | } |
2606 | 0 | return true; |
2607 | 0 | } |
2608 | | |
2609 | | static bool ZEND_FASTCALL increment_string(zval *str) /* {{{ */ |
2610 | 1.36k | { |
2611 | 1.36k | int carry=0; |
2612 | 1.36k | size_t pos=Z_STRLEN_P(str)-1; |
2613 | 1.36k | char *s; |
2614 | 1.36k | zend_string *t; |
2615 | 1.36k | int last=0; /* Shut up the compiler warning */ |
2616 | 1.36k | int ch; |
2617 | | |
2618 | 1.36k | zend_string *zstr = Z_STR_P(str); |
2619 | 1.36k | zend_string_addref(zstr); |
2620 | 1.36k | zend_error(E_DEPRECATED, "Increment on non-numeric string is deprecated, use str_increment() instead"); |
2621 | 1.36k | if (EG(exception)) { |
2622 | 0 | zend_string_release(zstr); |
2623 | 0 | return false; |
2624 | 0 | } |
2625 | | /* A userland error handler can change the type from string to something else */ |
2626 | 1.36k | zval_ptr_dtor(str); |
2627 | 1.36k | ZVAL_STR(str, zstr); |
2628 | | |
2629 | 1.36k | if (UNEXPECTED(Z_STRLEN_P(str) == 0)) { |
2630 | 20 | zval_ptr_dtor(str); |
2631 | 20 | ZVAL_CHAR(str, '1'); |
2632 | 20 | return true; |
2633 | 20 | } |
2634 | | |
2635 | 1.34k | if (!Z_REFCOUNTED_P(str)) { |
2636 | 406 | Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0); |
2637 | 406 | Z_TYPE_INFO_P(str) = IS_STRING_EX; |
2638 | 938 | } else if (Z_REFCOUNT_P(str) > 1) { |
2639 | | /* Only release string after allocation succeeded. */ |
2640 | 298 | zend_string *orig_str = Z_STR_P(str); |
2641 | 298 | Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0); |
2642 | 298 | GC_DELREF(orig_str); |
2643 | 640 | } else { |
2644 | 640 | zend_string_forget_hash_val(Z_STR_P(str)); |
2645 | 640 | } |
2646 | 1.34k | s = Z_STRVAL_P(str); |
2647 | | |
2648 | 1.54k | do { |
2649 | 1.54k | ch = s[pos]; |
2650 | 1.54k | if (ch >= 'a' && ch <= 'z') { |
2651 | 301 | if (ch == 'z') { |
2652 | 57 | s[pos] = 'a'; |
2653 | 57 | carry=1; |
2654 | 244 | } else { |
2655 | 244 | s[pos]++; |
2656 | 244 | carry=0; |
2657 | 244 | } |
2658 | 301 | last=LOWER_CASE; |
2659 | 1.24k | } else if (ch >= 'A' && ch <= 'Z') { |
2660 | 125 | if (ch == 'Z') { |
2661 | 73 | s[pos] = 'A'; |
2662 | 73 | carry=1; |
2663 | 73 | } else { |
2664 | 52 | s[pos]++; |
2665 | 52 | carry=0; |
2666 | 52 | } |
2667 | 125 | last=UPPER_CASE; |
2668 | 1.11k | } else if (ch >= '0' && ch <= '9') { |
2669 | 997 | if (ch == '9') { |
2670 | 120 | s[pos] = '0'; |
2671 | 120 | carry=1; |
2672 | 877 | } else { |
2673 | 877 | s[pos]++; |
2674 | 877 | carry=0; |
2675 | 877 | } |
2676 | 997 | last = NUMERIC; |
2677 | 997 | } else { |
2678 | 119 | carry=0; |
2679 | 119 | break; |
2680 | 119 | } |
2681 | 1.42k | if (carry == 0) { |
2682 | 1.17k | break; |
2683 | 1.17k | } |
2684 | 1.42k | } while (pos-- > 0); |
2685 | | |
2686 | 1.34k | if (carry) { |
2687 | 52 | t = zend_string_alloc(Z_STRLEN_P(str)+1, 0); |
2688 | 52 | memcpy(ZSTR_VAL(t) + 1, Z_STRVAL_P(str), Z_STRLEN_P(str)); |
2689 | 52 | ZSTR_VAL(t)[Z_STRLEN_P(str) + 1] = '\0'; |
2690 | 52 | switch (last) { |
2691 | 22 | case NUMERIC: |
2692 | 22 | ZSTR_VAL(t)[0] = '1'; |
2693 | 22 | break; |
2694 | 15 | case UPPER_CASE: |
2695 | 15 | ZSTR_VAL(t)[0] = 'A'; |
2696 | 15 | break; |
2697 | 15 | case LOWER_CASE: |
2698 | 15 | ZSTR_VAL(t)[0] = 'a'; |
2699 | 15 | break; |
2700 | 52 | } |
2701 | 52 | zend_string_free(Z_STR_P(str)); |
2702 | 52 | ZVAL_NEW_STR(str, t); |
2703 | 52 | } |
2704 | 1.34k | return true; |
2705 | 1.34k | } |
2706 | | /* }}} */ |
2707 | | |
2708 | | ZEND_API zend_result ZEND_FASTCALL increment_function(zval *op1) /* {{{ */ |
2709 | 9.38k | { |
2710 | 9.38k | try_again: |
2711 | 9.38k | switch (Z_TYPE_P(op1)) { |
2712 | 3.17k | case IS_LONG: |
2713 | 3.17k | fast_long_increment_function(op1); |
2714 | 3.17k | break; |
2715 | 2.06k | case IS_DOUBLE: |
2716 | 2.06k | Z_DVAL_P(op1) = Z_DVAL_P(op1) + 1; |
2717 | 2.06k | break; |
2718 | 2.17k | case IS_NULL: |
2719 | 2.17k | ZVAL_LONG(op1, 1); |
2720 | 2.17k | break; |
2721 | 1.68k | case IS_STRING: { |
2722 | 1.68k | zend_long lval; |
2723 | 1.68k | double dval; |
2724 | | |
2725 | 1.68k | switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) { |
2726 | 272 | case IS_LONG: |
2727 | 272 | zval_ptr_dtor_str(op1); |
2728 | 272 | if (lval == ZEND_LONG_MAX) { |
2729 | | /* switch to double */ |
2730 | 4 | double d = (double)lval; |
2731 | 4 | ZVAL_DOUBLE(op1, d+1); |
2732 | 268 | } else { |
2733 | 268 | ZVAL_LONG(op1, lval+1); |
2734 | 268 | } |
2735 | 272 | break; |
2736 | 51 | case IS_DOUBLE: |
2737 | 51 | zval_ptr_dtor_str(op1); |
2738 | 51 | ZVAL_DOUBLE(op1, dval+1); |
2739 | 51 | break; |
2740 | 1.36k | default: |
2741 | | /* Perl style string increment */ |
2742 | 1.36k | increment_string(op1); |
2743 | 1.36k | if (EG(exception)) { |
2744 | 0 | return FAILURE; |
2745 | 0 | } |
2746 | 1.36k | break; |
2747 | 1.68k | } |
2748 | 1.68k | } |
2749 | 1.68k | break; |
2750 | 1.68k | case IS_FALSE: |
2751 | 236 | case IS_TRUE: { |
2752 | | /* Error handler can undef/change type of op1, save it and reset it in case those cases */ |
2753 | 236 | zval copy; |
2754 | 236 | ZVAL_COPY_VALUE(©, op1); |
2755 | 236 | zend_error(E_WARNING, "Increment on type bool has no effect, this will change in the next major version of PHP"); |
2756 | 236 | zval_ptr_dtor(op1); |
2757 | 236 | ZVAL_COPY_VALUE(op1, ©); |
2758 | 236 | if (EG(exception)) { |
2759 | 0 | return FAILURE; |
2760 | 0 | } |
2761 | 236 | break; |
2762 | 236 | } |
2763 | 236 | case IS_REFERENCE: |
2764 | 0 | op1 = Z_REFVAL_P(op1); |
2765 | 0 | goto try_again; |
2766 | 55 | case IS_OBJECT: { |
2767 | 55 | if (Z_OBJ_HANDLER_P(op1, do_operation)) { |
2768 | 0 | zval op2; |
2769 | 0 | ZVAL_LONG(&op2, 1); |
2770 | 0 | if (Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_ADD, op1, op1, &op2) == SUCCESS) { |
2771 | 0 | return SUCCESS; |
2772 | 0 | } |
2773 | 0 | } |
2774 | 55 | zval tmp; |
2775 | 55 | if (Z_OBJ_HT_P(op1)->cast_object(Z_OBJ_P(op1), &tmp, _IS_NUMBER) == SUCCESS) { |
2776 | 0 | ZEND_ASSERT(Z_TYPE(tmp) == IS_LONG || Z_TYPE(tmp) == IS_DOUBLE); |
2777 | 0 | zval_ptr_dtor(op1); |
2778 | 0 | ZVAL_COPY_VALUE(op1, &tmp); |
2779 | 0 | goto try_again; |
2780 | 0 | } |
2781 | 55 | ZEND_FALLTHROUGH; |
2782 | 55 | } |
2783 | 55 | case IS_RESOURCE: |
2784 | 55 | case IS_ARRAY: |
2785 | 55 | zend_type_error("Cannot increment %s", zend_zval_value_name(op1)); |
2786 | 55 | return FAILURE; |
2787 | 9.38k | EMPTY_SWITCH_DEFAULT_CASE() |
2788 | 9.38k | } |
2789 | 9.32k | return SUCCESS; |
2790 | 9.38k | } |
2791 | | /* }}} */ |
2792 | | |
2793 | | ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op1) /* {{{ */ |
2794 | 14.3k | { |
2795 | 14.3k | zend_long lval; |
2796 | 14.3k | double dval; |
2797 | | |
2798 | 14.3k | try_again: |
2799 | 14.3k | switch (Z_TYPE_P(op1)) { |
2800 | 2.37k | case IS_LONG: |
2801 | 2.37k | fast_long_decrement_function(op1); |
2802 | 2.37k | break; |
2803 | 5.75k | case IS_DOUBLE: |
2804 | 5.75k | Z_DVAL_P(op1) = Z_DVAL_P(op1) - 1; |
2805 | 5.75k | break; |
2806 | 2.02k | case IS_STRING: /* Like perl we only support string increment */ |
2807 | 2.02k | if (Z_STRLEN_P(op1) == 0) { /* consider as 0 */ |
2808 | 12 | zend_error(E_DEPRECATED, "Decrement on empty string is deprecated as non-numeric"); |
2809 | 12 | if (EG(exception)) { |
2810 | 0 | return FAILURE; |
2811 | 0 | } |
2812 | | /* A userland error handler can change the type from string to something else */ |
2813 | 12 | zval_ptr_dtor(op1); |
2814 | 12 | ZVAL_LONG(op1, -1); |
2815 | 12 | break; |
2816 | 12 | } |
2817 | 2.00k | switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) { |
2818 | 1.29k | case IS_LONG: |
2819 | 1.29k | zval_ptr_dtor_str(op1); |
2820 | 1.29k | if (lval == ZEND_LONG_MIN) { |
2821 | 9 | double d = (double)lval; |
2822 | 9 | ZVAL_DOUBLE(op1, d-1); |
2823 | 1.28k | } else { |
2824 | 1.28k | ZVAL_LONG(op1, lval-1); |
2825 | 1.28k | } |
2826 | 1.29k | break; |
2827 | 158 | case IS_DOUBLE: |
2828 | 158 | zval_ptr_dtor_str(op1); |
2829 | 158 | ZVAL_DOUBLE(op1, dval - 1); |
2830 | 158 | break; |
2831 | 553 | default: { |
2832 | | /* Error handler can unset the variable */ |
2833 | 553 | zend_string *zstr = Z_STR_P(op1); |
2834 | 553 | zend_string_addref(zstr); |
2835 | 553 | zend_error(E_DEPRECATED, "Decrement on non-numeric string has no effect and is deprecated"); |
2836 | 553 | if (EG(exception)) { |
2837 | 0 | zend_string_release(zstr); |
2838 | 0 | return FAILURE; |
2839 | 0 | } |
2840 | 553 | zval_ptr_dtor(op1); |
2841 | 553 | ZVAL_STR(op1, zstr); |
2842 | 553 | } |
2843 | 2.00k | } |
2844 | 2.00k | break; |
2845 | 3.34k | case IS_NULL: { |
2846 | | /* Error handler can undef/change type of op1, save it and reset it in case those cases */ |
2847 | 3.34k | zval copy; |
2848 | 3.34k | ZVAL_COPY_VALUE(©, op1); |
2849 | 3.34k | zend_error(E_WARNING, "Decrement on type null has no effect, this will change in the next major version of PHP"); |
2850 | 3.34k | zval_ptr_dtor(op1); |
2851 | 3.34k | ZVAL_COPY_VALUE(op1, ©); |
2852 | 3.34k | if (EG(exception)) { |
2853 | 0 | return FAILURE; |
2854 | 0 | } |
2855 | 3.34k | break; |
2856 | 3.34k | } |
2857 | 3.34k | case IS_FALSE: |
2858 | 828 | case IS_TRUE: { |
2859 | | /* Error handler can undef/change type of op1, save it and reset it in case those cases */ |
2860 | 828 | zval copy; |
2861 | 828 | ZVAL_COPY_VALUE(©, op1); |
2862 | 828 | zend_error(E_WARNING, "Decrement on type bool has no effect, this will change in the next major version of PHP"); |
2863 | 828 | zval_ptr_dtor(op1); |
2864 | 828 | ZVAL_COPY_VALUE(op1, ©); |
2865 | 828 | if (EG(exception)) { |
2866 | 0 | return FAILURE; |
2867 | 0 | } |
2868 | 828 | break; |
2869 | 828 | } |
2870 | 828 | case IS_REFERENCE: |
2871 | 0 | op1 = Z_REFVAL_P(op1); |
2872 | 0 | goto try_again; |
2873 | 48 | case IS_OBJECT: { |
2874 | 48 | if (Z_OBJ_HANDLER_P(op1, do_operation)) { |
2875 | 0 | zval op2; |
2876 | 0 | ZVAL_LONG(&op2, 1); |
2877 | 0 | if (Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_SUB, op1, op1, &op2) == SUCCESS) { |
2878 | 0 | return SUCCESS; |
2879 | 0 | } |
2880 | 0 | } |
2881 | 48 | zval tmp; |
2882 | 48 | if (Z_OBJ_HT_P(op1)->cast_object(Z_OBJ_P(op1), &tmp, _IS_NUMBER) == SUCCESS) { |
2883 | 0 | ZEND_ASSERT(Z_TYPE(tmp) == IS_LONG || Z_TYPE(tmp) == IS_DOUBLE); |
2884 | 0 | zval_ptr_dtor(op1); |
2885 | 0 | ZVAL_COPY_VALUE(op1, &tmp); |
2886 | 0 | goto try_again; |
2887 | 0 | } |
2888 | 48 | ZEND_FALLTHROUGH; |
2889 | 48 | } |
2890 | 48 | case IS_RESOURCE: |
2891 | 50 | case IS_ARRAY: |
2892 | 50 | zend_type_error("Cannot decrement %s", zend_zval_value_name(op1)); |
2893 | 50 | return FAILURE; |
2894 | 14.3k | EMPTY_SWITCH_DEFAULT_CASE() |
2895 | 14.3k | } |
2896 | | |
2897 | 14.3k | return SUCCESS; |
2898 | 14.3k | } |
2899 | | /* }}} */ |
2900 | | |
2901 | | ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op) /* {{{ */ |
2902 | 418k | { |
2903 | 418k | return i_zend_is_true(op); |
2904 | 418k | } |
2905 | | /* }}} */ |
2906 | | |
2907 | | ZEND_API bool ZEND_FASTCALL zend_object_is_true(const zval *op) /* {{{ */ |
2908 | 0 | { |
2909 | 0 | zend_object *zobj = Z_OBJ_P(op); |
2910 | 0 | zval tmp; |
2911 | 0 | if (zobj->handlers->cast_object(zobj, &tmp, _IS_BOOL) == SUCCESS) { |
2912 | 0 | return Z_TYPE(tmp) == IS_TRUE; |
2913 | 0 | } |
2914 | 0 | zend_error(E_RECOVERABLE_ERROR, "Object of class %s could not be converted to bool", ZSTR_VAL(zobj->ce->name)); |
2915 | 0 | return false; |
2916 | 0 | } |
2917 | | /* }}} */ |
2918 | | |
2919 | | ZEND_API void zend_update_current_locale(void) /* {{{ */ |
2920 | 16 | { |
2921 | | #ifdef ZEND_USE_TOLOWER_L |
2922 | | # if defined(ZEND_WIN32) && defined(_MSC_VER) |
2923 | | current_locale = _get_current_locale(); |
2924 | | # else |
2925 | | current_locale = uselocale(0); |
2926 | | # endif |
2927 | | #endif |
2928 | | #if defined(ZEND_WIN32) && defined(_MSC_VER) |
2929 | | if (MB_CUR_MAX > 1) { |
2930 | | unsigned int cp = ___lc_codepage_func(); |
2931 | | CG(variable_width_locale) = 1; |
2932 | | // TODO: EUC-* are also ASCII compatible ??? |
2933 | | CG(ascii_compatible_locale) = |
2934 | | cp == 65001; /* UTF-8 */ |
2935 | | } else { |
2936 | | CG(variable_width_locale) = 0; |
2937 | | CG(ascii_compatible_locale) = 1; |
2938 | | } |
2939 | | #elif defined(MB_CUR_MAX) |
2940 | | /* Check if current locale uses variable width encoding */ |
2941 | 16 | if (MB_CUR_MAX > 1) { |
2942 | 16 | #ifdef HAVE_NL_LANGINFO |
2943 | 16 | const char *charmap = nl_langinfo(CODESET); |
2944 | | #else |
2945 | | char buf[16]; |
2946 | | const char *charmap = NULL; |
2947 | | const char *locale = setlocale(LC_CTYPE, NULL); |
2948 | | |
2949 | | if (locale) { |
2950 | | const char *dot = strchr(locale, '.'); |
2951 | | const char *modifier; |
2952 | | |
2953 | | if (dot) { |
2954 | | dot++; |
2955 | | modifier = strchr(dot, '@'); |
2956 | | if (!modifier) { |
2957 | | charmap = dot; |
2958 | | } else if (modifier - dot < sizeof(buf)) { |
2959 | | memcpy(buf, dot, modifier - dot); |
2960 | | buf[modifier - dot] = '\0'; |
2961 | | charmap = buf; |
2962 | | } |
2963 | | } |
2964 | | } |
2965 | | #endif |
2966 | 16 | CG(variable_width_locale) = 1; |
2967 | 16 | CG(ascii_compatible_locale) = 0; |
2968 | | |
2969 | 16 | if (charmap) { |
2970 | 16 | size_t len = strlen(charmap); |
2971 | 16 | static const char *ascii_compatible_charmaps[] = { |
2972 | 16 | "utf-8", |
2973 | 16 | "utf8", |
2974 | | // TODO: EUC-* are also ASCII compatible ??? |
2975 | 16 | NULL |
2976 | 16 | }; |
2977 | 16 | const char **p; |
2978 | | /* Check if current locale is ASCII compatible */ |
2979 | 16 | for (p = ascii_compatible_charmaps; *p; p++) { |
2980 | 16 | if (zend_binary_strcasecmp(charmap, len, *p, strlen(*p)) == 0) { |
2981 | 16 | CG(ascii_compatible_locale) = 1; |
2982 | 16 | break; |
2983 | 16 | } |
2984 | 16 | } |
2985 | 16 | } |
2986 | | |
2987 | 16 | } else { |
2988 | 0 | CG(variable_width_locale) = 0; |
2989 | 0 | CG(ascii_compatible_locale) = 1; |
2990 | 0 | } |
2991 | | #else |
2992 | | /* We can't determine current charset. Assume the worst case */ |
2993 | | CG(variable_width_locale) = 1; |
2994 | | CG(ascii_compatible_locale) = 0; |
2995 | | #endif |
2996 | 16 | } |
2997 | | /* }}} */ |
2998 | | |
2999 | | ZEND_API void zend_reset_lc_ctype_locale(void) |
3000 | 16 | { |
3001 | | /* Use the C.UTF-8 locale so that readline can process UTF-8 input, while not interfering |
3002 | | * with single-byte locale-dependent functions used by PHP. */ |
3003 | 16 | if (!setlocale(LC_CTYPE, "C.UTF-8")) { |
3004 | 0 | setlocale(LC_CTYPE, "C"); |
3005 | 0 | } |
3006 | 16 | } |
3007 | | |
3008 | 5.58M | static zend_always_inline void zend_str_tolower_impl(char *dest, const char *str, size_t length) /* {{{ */ { |
3009 | 5.58M | unsigned char *p = (unsigned char*)str; |
3010 | 5.58M | unsigned char *q = (unsigned char*)dest; |
3011 | 5.58M | unsigned char *end = p + length; |
3012 | 5.58M | #ifdef HAVE_BLOCKCONV |
3013 | 5.58M | if (length >= BLOCKCONV_STRIDE) { |
3014 | 1.60M | BLOCKCONV_INIT_RANGE('A', 'Z'); |
3015 | 1.60M | BLOCKCONV_INIT_DELTA('a' - 'A'); |
3016 | 8.53M | do { |
3017 | 8.53M | BLOCKCONV_LOAD(p); |
3018 | 8.53M | BLOCKCONV_STORE(q); |
3019 | 8.53M | p += BLOCKCONV_STRIDE; |
3020 | 8.53M | q += BLOCKCONV_STRIDE; |
3021 | 8.53M | } while (p + BLOCKCONV_STRIDE <= end); |
3022 | 1.60M | } |
3023 | 5.58M | #endif |
3024 | 47.2M | while (p < end) { |
3025 | 41.6M | *q++ = zend_tolower_ascii(*p++); |
3026 | 41.6M | } |
3027 | 5.58M | } |
3028 | | /* }}} */ |
3029 | | |
3030 | 16 | static zend_always_inline void zend_str_toupper_impl(char *dest, const char *str, size_t length) /* {{{ */ { |
3031 | 16 | unsigned char *p = (unsigned char*)str; |
3032 | 16 | unsigned char *q = (unsigned char*)dest; |
3033 | 16 | unsigned char *end = p + length; |
3034 | 16 | #ifdef HAVE_BLOCKCONV |
3035 | 16 | if (length >= BLOCKCONV_STRIDE) { |
3036 | 8 | BLOCKCONV_INIT_RANGE('a', 'z'); |
3037 | 8 | BLOCKCONV_INIT_DELTA('A' - 'a'); |
3038 | 16 | do { |
3039 | 16 | BLOCKCONV_LOAD(p); |
3040 | 16 | BLOCKCONV_STORE(q); |
3041 | 16 | p += BLOCKCONV_STRIDE; |
3042 | 16 | q += BLOCKCONV_STRIDE; |
3043 | 16 | } while (p + BLOCKCONV_STRIDE <= end); |
3044 | 8 | } |
3045 | 16 | #endif |
3046 | 72 | while (p < end) { |
3047 | 56 | *q++ = zend_toupper_ascii(*p++); |
3048 | 56 | } |
3049 | 16 | } |
3050 | | /* }}} */ |
3051 | | |
3052 | | ZEND_API char* ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length) /* {{{ */ |
3053 | 1.26M | { |
3054 | 1.26M | zend_str_tolower_impl(dest, source, length); |
3055 | 1.26M | dest[length] = '\0'; |
3056 | 1.26M | return dest; |
3057 | 1.26M | } |
3058 | | /* }}} */ |
3059 | | |
3060 | | ZEND_API char* ZEND_FASTCALL zend_str_toupper_copy(char *dest, const char *source, size_t length) /* {{{ */ |
3061 | 0 | { |
3062 | 0 | zend_str_toupper_impl(dest, source, length); |
3063 | 0 | dest[length] = '\0'; |
3064 | 0 | return dest; |
3065 | 0 | } |
3066 | | /* }}} */ |
3067 | | |
3068 | | ZEND_API char* ZEND_FASTCALL zend_str_tolower_dup(const char *source, size_t length) /* {{{ */ |
3069 | 1.09k | { |
3070 | 1.09k | return zend_str_tolower_copy((char *)emalloc(length+1), source, length); |
3071 | 1.09k | } |
3072 | | /* }}} */ |
3073 | | |
3074 | | ZEND_API char* ZEND_FASTCALL zend_str_toupper_dup(const char *source, size_t length) /* {{{ */ |
3075 | 0 | { |
3076 | 0 | return zend_str_toupper_copy((char *)emalloc(length+1), source, length); |
3077 | 0 | } |
3078 | | /* }}} */ |
3079 | | |
3080 | | ZEND_API void ZEND_FASTCALL zend_str_tolower(char *str, size_t length) /* {{{ */ |
3081 | 2.87M | { |
3082 | 2.87M | zend_str_tolower_impl(str, (const char*)str, length); |
3083 | 2.87M | } |
3084 | | /* }}} */ |
3085 | | |
3086 | | ZEND_API void ZEND_FASTCALL zend_str_toupper(char *str, size_t length) /* {{{ */ |
3087 | 0 | { |
3088 | 0 | zend_str_toupper_impl(str, (const char*)str, length); |
3089 | 0 | } |
3090 | | /* }}} */ |
3091 | | |
3092 | | |
3093 | | ZEND_API char* ZEND_FASTCALL zend_str_tolower_dup_ex(const char *source, size_t length) /* {{{ */ |
3094 | 34 | { |
3095 | 34 | const unsigned char *p = (const unsigned char*)source; |
3096 | 34 | const unsigned char *end = p + length; |
3097 | | |
3098 | 685 | while (p < end) { |
3099 | 684 | if (*p != zend_tolower_ascii(*p)) { |
3100 | 33 | char *res = (char*)emalloc(length + 1); |
3101 | 33 | unsigned char *r; |
3102 | | |
3103 | 33 | if (p != (const unsigned char*)source) { |
3104 | 33 | memcpy(res, source, p - (const unsigned char*)source); |
3105 | 33 | } |
3106 | 33 | r = (unsigned char*)p + (res - source); |
3107 | 33 | zend_str_tolower_impl((char *)r, (const char*)p, end - p); |
3108 | 33 | res[length] = '\0'; |
3109 | 33 | return res; |
3110 | 33 | } |
3111 | 651 | p++; |
3112 | 651 | } |
3113 | 1 | return NULL; |
3114 | 34 | } |
3115 | | /* }}} */ |
3116 | | |
3117 | | ZEND_API char* ZEND_FASTCALL zend_str_toupper_dup_ex(const char *source, size_t length) /* {{{ */ |
3118 | 0 | { |
3119 | 0 | const unsigned char *p = (const unsigned char*)source; |
3120 | 0 | const unsigned char *end = p + length; |
3121 | |
|
3122 | 0 | while (p < end) { |
3123 | 0 | if (*p != zend_toupper_ascii(*p)) { |
3124 | 0 | char *res = (char*)emalloc(length + 1); |
3125 | 0 | unsigned char *r; |
3126 | |
|
3127 | 0 | if (p != (const unsigned char*)source) { |
3128 | 0 | memcpy(res, source, p - (const unsigned char*)source); |
3129 | 0 | } |
3130 | 0 | r = (unsigned char*)p + (res - source); |
3131 | 0 | zend_str_toupper_impl((char *)r, (const char*)p, end - p); |
3132 | 0 | res[length] = '\0'; |
3133 | 0 | return res; |
3134 | 0 | } |
3135 | 0 | p++; |
3136 | 0 | } |
3137 | 0 | return NULL; |
3138 | 0 | } |
3139 | | /* }}} */ |
3140 | | |
3141 | | ZEND_API zend_string* ZEND_FASTCALL zend_string_tolower_ex(zend_string *str, bool persistent) /* {{{ */ |
3142 | 4.39M | { |
3143 | 4.39M | size_t length = ZSTR_LEN(str); |
3144 | 4.39M | unsigned char *p = (unsigned char *) ZSTR_VAL(str); |
3145 | 4.39M | unsigned char *end = p + length; |
3146 | | |
3147 | 4.39M | #ifdef HAVE_BLOCKCONV |
3148 | 4.39M | BLOCKCONV_INIT_RANGE('A', 'Z'); |
3149 | 9.53M | while (p + BLOCKCONV_STRIDE <= end) { |
3150 | 6.58M | BLOCKCONV_LOAD(p); |
3151 | 6.58M | if (BLOCKCONV_FOUND()) { |
3152 | 1.44M | zend_string *res = zend_string_alloc(length, persistent); |
3153 | 1.44M | memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char *) ZSTR_VAL(str)); |
3154 | 1.44M | unsigned char *q = (unsigned char*) ZSTR_VAL(res) + (p - (unsigned char*) ZSTR_VAL(str)); |
3155 | | |
3156 | | /* Lowercase the chunk we already compared. */ |
3157 | 1.44M | BLOCKCONV_INIT_DELTA('a' - 'A'); |
3158 | 1.44M | BLOCKCONV_STORE(q); |
3159 | | |
3160 | | /* Lowercase the rest of the string. */ |
3161 | 1.44M | p += BLOCKCONV_STRIDE; |
3162 | 1.44M | q += BLOCKCONV_STRIDE; |
3163 | 1.44M | zend_str_tolower_impl((char *) q, (const char *) p, end - p); |
3164 | 1.44M | ZSTR_VAL(res)[length] = '\0'; |
3165 | 1.44M | return res; |
3166 | 1.44M | } |
3167 | 5.13M | p += BLOCKCONV_STRIDE; |
3168 | 5.13M | } |
3169 | 2.94M | #endif |
3170 | | |
3171 | 14.6M | while (p < end) { |
3172 | 13.4M | if (*p != zend_tolower_ascii(*p)) { |
3173 | 1.78M | zend_string *res = zend_string_alloc(length, persistent); |
3174 | 1.78M | memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str)); |
3175 | | |
3176 | 1.78M | unsigned char *q = (unsigned char*) ZSTR_VAL(res) + (p - (unsigned char*) ZSTR_VAL(str)); |
3177 | 16.7M | while (p < end) { |
3178 | 14.9M | *q++ = zend_tolower_ascii(*p++); |
3179 | 14.9M | } |
3180 | 1.78M | ZSTR_VAL(res)[length] = '\0'; |
3181 | 1.78M | return res; |
3182 | 1.78M | } |
3183 | 11.6M | p++; |
3184 | 11.6M | } |
3185 | | |
3186 | 1.16M | return zend_string_copy(str); |
3187 | 2.94M | } |
3188 | | /* }}} */ |
3189 | | |
3190 | | ZEND_API zend_string* ZEND_FASTCALL zend_string_toupper_ex(zend_string *str, bool persistent) /* {{{ */ |
3191 | 1.01k | { |
3192 | 1.01k | size_t length = ZSTR_LEN(str); |
3193 | 1.01k | unsigned char *p = (unsigned char *) ZSTR_VAL(str); |
3194 | 1.01k | unsigned char *end = p + length; |
3195 | | |
3196 | 1.01k | #ifdef HAVE_BLOCKCONV |
3197 | 1.01k | BLOCKCONV_INIT_RANGE('a', 'z'); |
3198 | 1.01k | while (p + BLOCKCONV_STRIDE <= end) { |
3199 | 19 | BLOCKCONV_LOAD(p); |
3200 | 19 | if (BLOCKCONV_FOUND()) { |
3201 | 16 | zend_string *res = zend_string_alloc(length, persistent); |
3202 | 16 | memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char *) ZSTR_VAL(str)); |
3203 | 16 | unsigned char *q = (unsigned char *) ZSTR_VAL(res) + (p - (unsigned char *) ZSTR_VAL(str)); |
3204 | | |
3205 | | /* Uppercase the chunk we already compared. */ |
3206 | 16 | BLOCKCONV_INIT_DELTA('A' - 'a'); |
3207 | 16 | BLOCKCONV_STORE(q); |
3208 | | |
3209 | | /* Uppercase the rest of the string. */ |
3210 | 16 | p += BLOCKCONV_STRIDE; |
3211 | 16 | q += BLOCKCONV_STRIDE; |
3212 | 16 | zend_str_toupper_impl((char *) q, (const char *) p, end - p); |
3213 | 16 | ZSTR_VAL(res)[length] = '\0'; |
3214 | 16 | return res; |
3215 | 16 | } |
3216 | 3 | p += BLOCKCONV_STRIDE; |
3217 | 3 | } |
3218 | 994 | #endif |
3219 | | |
3220 | 1.02k | while (p < end) { |
3221 | 892 | if (*p != zend_toupper_ascii(*p)) { |
3222 | 863 | zend_string *res = zend_string_alloc(length, persistent); |
3223 | 863 | memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str)); |
3224 | | |
3225 | 863 | unsigned char *q = (unsigned char *) ZSTR_VAL(res) + (p - (unsigned char *) ZSTR_VAL(str)); |
3226 | 7.16k | while (p < end) { |
3227 | 6.30k | *q++ = zend_toupper_ascii(*p++); |
3228 | 6.30k | } |
3229 | 863 | ZSTR_VAL(res)[length] = '\0'; |
3230 | 863 | return res; |
3231 | 863 | } |
3232 | 29 | p++; |
3233 | 29 | } |
3234 | | |
3235 | 131 | return zend_string_copy(str); |
3236 | 994 | } |
3237 | | /* }}} */ |
3238 | | |
3239 | | ZEND_API int ZEND_FASTCALL zend_binary_strcmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */ |
3240 | 117k | { |
3241 | 117k | int retval; |
3242 | | |
3243 | 117k | if (s1 == s2) { |
3244 | 16.2k | return 0; |
3245 | 16.2k | } |
3246 | 101k | retval = memcmp(s1, s2, MIN(len1, len2)); |
3247 | 101k | if (!retval) { |
3248 | 16.8k | return ZEND_THREEWAY_COMPARE(len1, len2); |
3249 | 84.3k | } else { |
3250 | 84.3k | return retval; |
3251 | 84.3k | } |
3252 | 101k | } |
3253 | | /* }}} */ |
3254 | | |
3255 | | ZEND_API int ZEND_FASTCALL zend_binary_strncmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */ |
3256 | 37 | { |
3257 | 37 | int retval; |
3258 | | |
3259 | 37 | if (s1 == s2) { |
3260 | 10 | return 0; |
3261 | 10 | } |
3262 | 27 | retval = memcmp(s1, s2, MIN(length, MIN(len1, len2))); |
3263 | 27 | if (!retval) { |
3264 | 21 | return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2)); |
3265 | 21 | } else { |
3266 | 6 | return retval; |
3267 | 6 | } |
3268 | 27 | } |
3269 | | /* }}} */ |
3270 | | |
3271 | | ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */ |
3272 | 814k | { |
3273 | 814k | size_t len; |
3274 | 814k | int c1, c2; |
3275 | | |
3276 | 814k | if (s1 == s2) { |
3277 | 18.6k | return 0; |
3278 | 18.6k | } |
3279 | | |
3280 | 795k | len = MIN(len1, len2); |
3281 | 2.05M | while (len--) { |
3282 | 1.92M | c1 = zend_tolower_ascii(*(unsigned char *)s1++); |
3283 | 1.92M | c2 = zend_tolower_ascii(*(unsigned char *)s2++); |
3284 | 1.92M | if (c1 != c2) { |
3285 | 669k | return c1 - c2; |
3286 | 669k | } |
3287 | 1.92M | } |
3288 | | |
3289 | 126k | return ZEND_THREEWAY_COMPARE(len1, len2); |
3290 | 795k | } |
3291 | | /* }}} */ |
3292 | | |
3293 | | ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */ |
3294 | 45 | { |
3295 | 45 | size_t len; |
3296 | 45 | int c1, c2; |
3297 | | |
3298 | 45 | if (s1 == s2) { |
3299 | 5 | return 0; |
3300 | 5 | } |
3301 | 40 | len = MIN(length, MIN(len1, len2)); |
3302 | 126 | while (len--) { |
3303 | 97 | c1 = zend_tolower_ascii(*(unsigned char *)s1++); |
3304 | 97 | c2 = zend_tolower_ascii(*(unsigned char *)s2++); |
3305 | 97 | if (c1 != c2) { |
3306 | 11 | return c1 - c2; |
3307 | 11 | } |
3308 | 97 | } |
3309 | | |
3310 | 29 | return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2)); |
3311 | 40 | } |
3312 | | /* }}} */ |
3313 | | |
3314 | | ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */ |
3315 | 0 | { |
3316 | 0 | size_t len; |
3317 | 0 | int c1, c2; |
3318 | |
|
3319 | 0 | if (s1 == s2) { |
3320 | 0 | return 0; |
3321 | 0 | } |
3322 | | |
3323 | 0 | len = MIN(len1, len2); |
3324 | 0 | while (len--) { |
3325 | 0 | c1 = zend_tolower((int)*(unsigned char *)s1++); |
3326 | 0 | c2 = zend_tolower((int)*(unsigned char *)s2++); |
3327 | 0 | if (c1 != c2) { |
3328 | 0 | return c1 - c2; |
3329 | 0 | } |
3330 | 0 | } |
3331 | | |
3332 | 0 | return ZEND_THREEWAY_COMPARE(len1, len2); |
3333 | 0 | } |
3334 | | /* }}} */ |
3335 | | |
3336 | | ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */ |
3337 | 0 | { |
3338 | 0 | size_t len; |
3339 | 0 | int c1, c2; |
3340 | |
|
3341 | 0 | if (s1 == s2) { |
3342 | 0 | return 0; |
3343 | 0 | } |
3344 | 0 | len = MIN(length, MIN(len1, len2)); |
3345 | 0 | while (len--) { |
3346 | 0 | c1 = zend_tolower((int)*(unsigned char *)s1++); |
3347 | 0 | c2 = zend_tolower((int)*(unsigned char *)s2++); |
3348 | 0 | if (c1 != c2) { |
3349 | 0 | return c1 - c2; |
3350 | 0 | } |
3351 | 0 | } |
3352 | | |
3353 | 0 | return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2)); |
3354 | 0 | } |
3355 | | /* }}} */ |
3356 | | |
3357 | | ZEND_API int ZEND_FASTCALL zend_binary_zval_strcmp(zval *s1, zval *s2) /* {{{ */ |
3358 | 0 | { |
3359 | 0 | return zend_binary_strcmp(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2)); |
3360 | 0 | } |
3361 | | /* }}} */ |
3362 | | |
3363 | | ZEND_API int ZEND_FASTCALL zend_binary_zval_strncmp(zval *s1, zval *s2, zval *s3) /* {{{ */ |
3364 | 0 | { |
3365 | 0 | return zend_binary_strncmp(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2), Z_LVAL_P(s3)); |
3366 | 0 | } |
3367 | | /* }}} */ |
3368 | | |
3369 | | ZEND_API bool ZEND_FASTCALL zendi_smart_streq(zend_string *s1, zend_string *s2) /* {{{ */ |
3370 | 903 | { |
3371 | 903 | uint8_t ret1, ret2; |
3372 | 903 | int oflow1, oflow2; |
3373 | 903 | zend_long lval1 = 0, lval2 = 0; |
3374 | 903 | double dval1 = 0.0, dval2 = 0.0; |
3375 | | |
3376 | 903 | if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, false, &oflow1, NULL)) && |
3377 | 588 | (ret2 = is_numeric_string_ex(s2->val, s2->len, &lval2, &dval2, false, &oflow2, NULL))) { |
3378 | | #if ZEND_ULONG_MAX == 0xFFFFFFFF |
3379 | | if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0. && |
3380 | | ((oflow1 == 1 && dval1 > 9007199254740991. /*0x1FFFFFFFFFFFFF*/) |
3381 | | || (oflow1 == -1 && dval1 < -9007199254740991.))) { |
3382 | | #else |
3383 | 518 | if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0.) { |
3384 | 0 | #endif |
3385 | | /* both values are integers overflown to the same side, and the |
3386 | | * double comparison may have resulted in crucial accuracy lost */ |
3387 | 0 | goto string_cmp; |
3388 | 0 | } |
3389 | 518 | if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) { |
3390 | 224 | if (ret1 != IS_DOUBLE) { |
3391 | 108 | if (oflow2) { |
3392 | | /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */ |
3393 | 94 | return 0; |
3394 | 94 | } |
3395 | 14 | dval1 = (double) lval1; |
3396 | 116 | } else if (ret2 != IS_DOUBLE) { |
3397 | 44 | if (oflow1) { |
3398 | 1 | return 0; |
3399 | 1 | } |
3400 | 43 | dval2 = (double) lval2; |
3401 | 72 | } else if (dval1 == dval2 && !zend_finite(dval1)) { |
3402 | | /* Both values overflowed and have the same sign, |
3403 | | * so a numeric comparison would be inaccurate */ |
3404 | 30 | goto string_cmp; |
3405 | 30 | } |
3406 | 99 | return dval1 == dval2; |
3407 | 294 | } else { /* they both have to be long's */ |
3408 | 294 | return lval1 == lval2; |
3409 | 294 | } |
3410 | 518 | } else { |
3411 | 415 | string_cmp: |
3412 | 415 | return zend_string_equal_content(s1, s2); |
3413 | 385 | } |
3414 | 903 | } |
3415 | | /* }}} */ |
3416 | | |
3417 | | ZEND_API int ZEND_FASTCALL zendi_smart_strcmp(zend_string *s1, zend_string *s2) /* {{{ */ |
3418 | 8.38k | { |
3419 | 8.38k | uint8_t ret1, ret2; |
3420 | 8.38k | int oflow1, oflow2; |
3421 | 8.38k | zend_long lval1 = 0, lval2 = 0; |
3422 | 8.38k | double dval1 = 0.0, dval2 = 0.0; |
3423 | | |
3424 | 8.38k | if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, false, &oflow1, NULL)) && |
3425 | 2.77k | (ret2 = is_numeric_string_ex(s2->val, s2->len, &lval2, &dval2, false, &oflow2, NULL))) { |
3426 | | #if ZEND_ULONG_MAX == 0xFFFFFFFF |
3427 | | if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0. && |
3428 | | ((oflow1 == 1 && dval1 > 9007199254740991. /*0x1FFFFFFFFFFFFF*/) |
3429 | | || (oflow1 == -1 && dval1 < -9007199254740991.))) { |
3430 | | #else |
3431 | 1.94k | if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0.) { |
3432 | 30 | #endif |
3433 | | /* both values are integers overflowed to the same side, and the |
3434 | | * double comparison may have resulted in crucial accuracy lost */ |
3435 | 30 | goto string_cmp; |
3436 | 30 | } |
3437 | 1.91k | if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) { |
3438 | 1.09k | if (ret1 != IS_DOUBLE) { |
3439 | 217 | if (oflow2) { |
3440 | | /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */ |
3441 | 134 | return -1 * oflow2; |
3442 | 134 | } |
3443 | 83 | dval1 = (double) lval1; |
3444 | 878 | } else if (ret2 != IS_DOUBLE) { |
3445 | 368 | if (oflow1) { |
3446 | 15 | return oflow1; |
3447 | 15 | } |
3448 | 353 | dval2 = (double) lval2; |
3449 | 510 | } else if (dval1 == dval2 && !zend_finite(dval1)) { |
3450 | | /* Both values overflowed and have the same sign, |
3451 | | * so a numeric comparison would be inaccurate */ |
3452 | 35 | goto string_cmp; |
3453 | 35 | } |
3454 | 911 | dval1 = dval1 - dval2; |
3455 | 911 | return ZEND_NORMALIZE_BOOL(dval1); |
3456 | 1.09k | } else { /* they both have to be long's */ |
3457 | 817 | return lval1 > lval2 ? 1 : (lval1 < lval2 ? -1 : 0); |
3458 | 817 | } |
3459 | 6.44k | } else { |
3460 | 6.44k | int strcmp_ret; |
3461 | 6.50k | string_cmp: |
3462 | 6.50k | strcmp_ret = zend_binary_strcmp(s1->val, s1->len, s2->val, s2->len); |
3463 | 6.50k | return ZEND_NORMALIZE_BOOL(strcmp_ret); |
3464 | 6.44k | } |
3465 | 8.38k | } |
3466 | | /* }}} */ |
3467 | | |
3468 | | static int hash_zval_compare_function(zval *z1, zval *z2) /* {{{ */ |
3469 | 452 | { |
3470 | 452 | return zend_compare(z1, z2); |
3471 | 452 | } |
3472 | | /* }}} */ |
3473 | | |
3474 | | ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable *ht2) /* {{{ */ |
3475 | 5.51k | { |
3476 | 5.51k | if (ht1 == ht2) { |
3477 | 1.82k | return 0; |
3478 | 1.82k | } |
3479 | | |
3480 | 3.69k | GC_TRY_ADDREF(ht1); |
3481 | 3.69k | GC_TRY_ADDREF(ht2); |
3482 | | |
3483 | 3.69k | int ret = zend_hash_compare(ht1, ht2, (compare_func_t) hash_zval_compare_function, 0); |
3484 | | |
3485 | 3.69k | GC_TRY_DTOR_NO_REF(ht1); |
3486 | 3.69k | GC_TRY_DTOR_NO_REF(ht2); |
3487 | | |
3488 | 3.69k | return ret; |
3489 | 5.51k | } |
3490 | | /* }}} */ |
3491 | | |
3492 | | ZEND_API int ZEND_FASTCALL zend_compare_arrays(zval *a1, zval *a2) /* {{{ */ |
3493 | 5.47k | { |
3494 | 5.47k | return zend_compare_symbol_tables(Z_ARRVAL_P(a1), Z_ARRVAL_P(a2)); |
3495 | 5.47k | } |
3496 | | /* }}} */ |
3497 | | |
3498 | | ZEND_API int ZEND_FASTCALL zend_compare_objects(zval *o1, zval *o2) /* {{{ */ |
3499 | 0 | { |
3500 | 0 | if (Z_OBJ_P(o1) == Z_OBJ_P(o2)) { |
3501 | 0 | return 0; |
3502 | 0 | } |
3503 | | |
3504 | 0 | if (Z_OBJ_HT_P(o1)->compare == NULL) { |
3505 | 0 | return 1; |
3506 | 0 | } else { |
3507 | 0 | return Z_OBJ_HT_P(o1)->compare(o1, o2); |
3508 | 0 | } |
3509 | 0 | } |
3510 | | /* }}} */ |
3511 | | |
3512 | | ZEND_API zend_string* ZEND_FASTCALL zend_long_to_str(zend_long num) /* {{{ */ |
3513 | 899k | { |
3514 | 899k | if ((zend_ulong)num <= 9) { |
3515 | 787k | return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num); |
3516 | 787k | } else { |
3517 | 111k | char buf[MAX_LENGTH_OF_LONG + 1]; |
3518 | 111k | char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, num); |
3519 | 111k | zend_string *str = zend_string_init(res, buf + sizeof(buf) - 1 - res, 0); |
3520 | 111k | GC_ADD_FLAGS(str, IS_STR_VALID_UTF8); |
3521 | 111k | return str; |
3522 | 111k | } |
3523 | 899k | } |
3524 | | /* }}} */ |
3525 | | |
3526 | | ZEND_API zend_string* ZEND_FASTCALL zend_ulong_to_str(zend_ulong num) |
3527 | 0 | { |
3528 | 0 | if (num <= 9) { |
3529 | 0 | return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num); |
3530 | 0 | } else { |
3531 | 0 | char buf[MAX_LENGTH_OF_LONG + 1]; |
3532 | 0 | char *res = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num); |
3533 | 0 | zend_string *str = zend_string_init(res, buf + sizeof(buf) - 1 - res, 0); |
3534 | 0 | GC_ADD_FLAGS(str, IS_STR_VALID_UTF8); |
3535 | 0 | return str; |
3536 | 0 | } |
3537 | 0 | } |
3538 | | |
3539 | | /* buf points to the END of the buffer */ |
3540 | 0 | static zend_always_inline char *zend_print_u64_to_buf(char *buf, uint64_t num64) { |
3541 | 0 | #if SIZEOF_ZEND_LONG == 8 |
3542 | 0 | return zend_print_ulong_to_buf(buf, num64); |
3543 | | #else |
3544 | | *buf = '\0'; |
3545 | | while (num64 > ZEND_ULONG_MAX) { |
3546 | | *--buf = (char) (num64 % 10) + '0'; |
3547 | | num64 /= 10; |
3548 | | } |
3549 | | |
3550 | | zend_ulong num = (zend_ulong) num64; |
3551 | | do { |
3552 | | *--buf = (char) (num % 10) + '0'; |
3553 | | num /= 10; |
3554 | | } while (num > 0); |
3555 | | return buf; |
3556 | | #endif |
3557 | 0 | } |
3558 | | |
3559 | | /* buf points to the END of the buffer */ |
3560 | 0 | static zend_always_inline char *zend_print_i64_to_buf(char *buf, int64_t num) { |
3561 | 0 | if (num < 0) { |
3562 | 0 | char *result = zend_print_u64_to_buf(buf, ~((uint64_t) num) + 1); |
3563 | 0 | *--result = '-'; |
3564 | 0 | return result; |
3565 | 0 | } else { |
3566 | 0 | return zend_print_u64_to_buf(buf, num); |
3567 | 0 | } |
3568 | 0 | } |
3569 | | |
3570 | | ZEND_API zend_string* ZEND_FASTCALL zend_u64_to_str(uint64_t num) |
3571 | 0 | { |
3572 | 0 | if (num <= 9) { |
3573 | 0 | return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num); |
3574 | 0 | } else { |
3575 | 0 | char buf[20 + 1]; |
3576 | 0 | char *res = zend_print_u64_to_buf(buf + sizeof(buf) - 1, num); |
3577 | 0 | zend_string *str = zend_string_init(res, buf + sizeof(buf) - 1 - res, 0); |
3578 | 0 | GC_ADD_FLAGS(str, IS_STR_VALID_UTF8); |
3579 | 0 | return str; |
3580 | 0 | } |
3581 | 0 | } |
3582 | | |
3583 | | ZEND_API zend_string* ZEND_FASTCALL zend_i64_to_str(int64_t num) |
3584 | 0 | { |
3585 | 0 | if ((uint64_t)num <= 9) { |
3586 | 0 | return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num); |
3587 | 0 | } else { |
3588 | 0 | char buf[20 + 1]; |
3589 | 0 | char *res = zend_print_i64_to_buf(buf + sizeof(buf) - 1, num); |
3590 | 0 | zend_string *str = zend_string_init(res, buf + sizeof(buf) - 1 - res, 0); |
3591 | 0 | GC_ADD_FLAGS(str, IS_STR_VALID_UTF8); |
3592 | 0 | return str; |
3593 | 0 | } |
3594 | 0 | } |
3595 | | |
3596 | | ZEND_API zend_string* ZEND_FASTCALL zend_double_to_str(double num) |
3597 | 153k | { |
3598 | 153k | char buf[ZEND_DOUBLE_MAX_LENGTH]; |
3599 | | /* Model snprintf precision behavior. */ |
3600 | 153k | int precision = (int) EG(precision); |
3601 | 153k | zend_gcvt(num, precision ? precision : 1, '.', 'E', buf); |
3602 | 153k | zend_string *str = zend_string_init(buf, strlen(buf), 0); |
3603 | 153k | if (UNEXPECTED(zend_isnan(num))) { |
3604 | 144 | zend_nan_coerced_to_type_warning(IS_STRING); |
3605 | 144 | } |
3606 | 153k | GC_ADD_FLAGS(str, IS_STR_VALID_UTF8); |
3607 | 153k | return str; |
3608 | 153k | } |
3609 | | |
3610 | | ZEND_API uint8_t ZEND_FASTCALL is_numeric_str_function(const zend_string *str, zend_long *lval, double *dval) /* {{{ */ |
3611 | 13.8k | { |
3612 | 13.8k | return is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), lval, dval, false); |
3613 | 13.8k | } |
3614 | | /* }}} */ |
3615 | | |
3616 | | ZEND_API uint8_t ZEND_FASTCALL _is_numeric_string_ex(const char *str, size_t length, zend_long *lval, |
3617 | | double *dval, bool allow_errors, int *oflow_info, bool *trailing_data) /* {{{ */ |
3618 | 178k | { |
3619 | 178k | const char *ptr; |
3620 | 178k | int digits = 0, dp_or_e = 0; |
3621 | 178k | double local_dval = 0.0; |
3622 | 178k | uint8_t type; |
3623 | 178k | zend_ulong tmp_lval = 0; |
3624 | 178k | int neg = 0; |
3625 | | |
3626 | 178k | if (!length) { |
3627 | 5.78k | return 0; |
3628 | 5.78k | } |
3629 | | |
3630 | 173k | if (oflow_info != NULL) { |
3631 | 11.4k | *oflow_info = 0; |
3632 | 11.4k | } |
3633 | 173k | if (trailing_data != NULL) { |
3634 | 52.4k | *trailing_data = false; |
3635 | 52.4k | } |
3636 | | |
3637 | | /* Skip any whitespace |
3638 | | * This is much faster than the isspace() function */ |
3639 | 200k | while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\v' || *str == '\f') { |
3640 | 27.2k | str++; |
3641 | 27.2k | length--; |
3642 | 27.2k | } |
3643 | 173k | ptr = str; |
3644 | | |
3645 | 173k | if (*ptr == '-') { |
3646 | 66.5k | neg = 1; |
3647 | 66.5k | ptr++; |
3648 | 106k | } else if (*ptr == '+') { |
3649 | 1.34k | ptr++; |
3650 | 1.34k | } |
3651 | | |
3652 | 173k | if (ZEND_IS_DIGIT(*ptr)) { |
3653 | | /* Skip any leading 0s */ |
3654 | 255k | while (*ptr == '0') { |
3655 | 114k | ptr++; |
3656 | 114k | } |
3657 | | |
3658 | | /* Count the number of digits. If a decimal point/exponent is found, |
3659 | | * it's a double. Otherwise, if there's a dval or no need to check for |
3660 | | * a full match, stop when there are too many digits for a long */ |
3661 | 1.44M | for (type = IS_LONG; !(digits >= MAX_LENGTH_OF_LONG && (dval || allow_errors)); digits++, ptr++) { |
3662 | 1.45M | check_digits: |
3663 | 1.45M | if (ZEND_IS_DIGIT(*ptr)) { |
3664 | 1.30M | tmp_lval = tmp_lval * 10 + (*ptr) - '0'; |
3665 | 1.30M | continue; |
3666 | 1.30M | } else if (*ptr == '.' && dp_or_e < 1) { |
3667 | 12.8k | goto process_double; |
3668 | 132k | } else if ((*ptr == 'e' || *ptr == 'E') && dp_or_e < 2) { |
3669 | 9.00k | const char *e = ptr + 1; |
3670 | | |
3671 | 9.00k | if (*e == '-' || *e == '+') { |
3672 | 4.97k | ptr = e++; |
3673 | 4.97k | } |
3674 | 9.00k | if (ZEND_IS_DIGIT(*e)) { |
3675 | 5.58k | goto process_double; |
3676 | 5.58k | } |
3677 | 9.00k | } |
3678 | | |
3679 | 126k | break; |
3680 | 1.45M | } |
3681 | | |
3682 | 133k | if (digits >= MAX_LENGTH_OF_LONG) { |
3683 | 9.72k | if (oflow_info != NULL) { |
3684 | 769 | *oflow_info = *str == '-' ? -1 : 1; |
3685 | 769 | } |
3686 | 9.72k | dp_or_e = -1; |
3687 | 9.72k | goto process_double; |
3688 | 9.72k | } |
3689 | 133k | } else if (*ptr == '.' && ZEND_IS_DIGIT(ptr[1])) { |
3690 | 36.4k | process_double: |
3691 | 36.4k | type = IS_DOUBLE; |
3692 | | |
3693 | | /* If there's a dval, do the conversion; else continue checking |
3694 | | * the digits if we need to check for a full match */ |
3695 | 36.4k | if (dval) { |
3696 | 21.9k | local_dval = zend_strtod(str, &ptr); |
3697 | 21.9k | } else if (!allow_errors && dp_or_e != -1) { |
3698 | 10.6k | dp_or_e = (*ptr++ == '.') ? 1 : 2; |
3699 | 10.6k | goto check_digits; |
3700 | 10.6k | } |
3701 | 36.4k | } else { |
3702 | 23.8k | return 0; |
3703 | 23.8k | } |
3704 | | |
3705 | 149k | if (ptr != str + length) { |
3706 | 52.9k | const char *endptr = ptr; |
3707 | 96.9k | while (*endptr == ' ' || *endptr == '\t' || *endptr == '\n' || *endptr == '\r' || *endptr == '\v' || *endptr == '\f') { |
3708 | 44.0k | endptr++; |
3709 | 44.0k | length--; |
3710 | 44.0k | } |
3711 | 52.9k | if (ptr != str + length) { |
3712 | 51.1k | if (!allow_errors) { |
3713 | 26.0k | return 0; |
3714 | 26.0k | } |
3715 | 25.0k | if (trailing_data != NULL) { |
3716 | 18.3k | *trailing_data = true; |
3717 | 18.3k | } |
3718 | 25.0k | } |
3719 | 52.9k | } |
3720 | | |
3721 | 123k | if (type == IS_LONG) { |
3722 | 94.0k | if (digits == MAX_LENGTH_OF_LONG - 1) { |
3723 | 34.3k | int cmp = strcmp(&ptr[-digits], long_min_digits); |
3724 | | |
3725 | 34.3k | if (!(cmp < 0 || (cmp == 0 && *str == '-'))) { |
3726 | 3.43k | if (dval) { |
3727 | 2.83k | *dval = zend_strtod(str, NULL); |
3728 | 2.83k | } |
3729 | 3.43k | if (oflow_info != NULL) { |
3730 | 175 | *oflow_info = *str == '-' ? -1 : 1; |
3731 | 175 | } |
3732 | | |
3733 | 3.43k | return IS_DOUBLE; |
3734 | 3.43k | } |
3735 | 34.3k | } |
3736 | | |
3737 | 90.6k | if (lval) { |
3738 | 62.6k | if (neg) { |
3739 | 29.2k | tmp_lval = -tmp_lval; |
3740 | 29.2k | } |
3741 | 62.6k | *lval = (zend_long) tmp_lval; |
3742 | 62.6k | } |
3743 | | |
3744 | 90.6k | return IS_LONG; |
3745 | 94.0k | } else { |
3746 | 29.0k | if (dval) { |
3747 | 20.8k | *dval = local_dval; |
3748 | 20.8k | } |
3749 | | |
3750 | 29.0k | return IS_DOUBLE; |
3751 | 29.0k | } |
3752 | 123k | } |
3753 | | /* }}} */ |
3754 | | |
3755 | | /* |
3756 | | * String matching - Sunday algorithm |
3757 | | * http://www.iti.fh-flensburg.de/lang/algorithmen/pattern/sundayen.htm |
3758 | | */ |
3759 | 79 | static zend_always_inline void zend_memnstr_ex_pre(unsigned int td[], const char *needle, size_t needle_len, int reverse) /* {{{ */ { |
3760 | 79 | int i; |
3761 | | |
3762 | 20.3k | for (i = 0; i < 256; i++) { |
3763 | 20.2k | td[i] = needle_len + 1; |
3764 | 20.2k | } |
3765 | | |
3766 | 79 | if (reverse) { |
3767 | 0 | for (i = needle_len - 1; i >= 0; i--) { |
3768 | 0 | td[(unsigned char)needle[i]] = i + 1; |
3769 | 0 | } |
3770 | 79 | } else { |
3771 | 79 | size_t i; |
3772 | | |
3773 | 8.05k | for (i = 0; i < needle_len; i++) { |
3774 | 7.97k | td[(unsigned char)needle[i]] = (int)needle_len - i; |
3775 | 7.97k | } |
3776 | 79 | } |
3777 | 79 | } |
3778 | | /* }}} */ |
3779 | | |
3780 | | ZEND_API const char* ZEND_FASTCALL zend_memnstr_ex(const char *haystack, const char *needle, size_t needle_len, const char *end) /* {{{ */ |
3781 | 79 | { |
3782 | 79 | unsigned int td[256]; |
3783 | 79 | size_t i; |
3784 | 79 | const char *p; |
3785 | | |
3786 | 79 | if (needle_len == 0 || (end - haystack) < needle_len) { |
3787 | 0 | return NULL; |
3788 | 0 | } |
3789 | | |
3790 | 79 | zend_memnstr_ex_pre(td, needle, needle_len, 0); |
3791 | | |
3792 | 79 | p = haystack; |
3793 | 79 | end -= needle_len; |
3794 | | |
3795 | 2.48k | while (p <= end) { |
3796 | 2.54k | for (i = 0; i < needle_len; i++) { |
3797 | 2.54k | if (needle[i] != p[i]) { |
3798 | 2.40k | break; |
3799 | 2.40k | } |
3800 | 2.54k | } |
3801 | 2.40k | if (i == needle_len) { |
3802 | 0 | return p; |
3803 | 0 | } |
3804 | 2.40k | if (UNEXPECTED(p == end)) { |
3805 | 3 | return NULL; |
3806 | 3 | } |
3807 | 2.40k | p += td[(unsigned char)(p[needle_len])]; |
3808 | 2.40k | } |
3809 | | |
3810 | 76 | return NULL; |
3811 | 79 | } |
3812 | | /* }}} */ |
3813 | | |
3814 | | ZEND_API const char* ZEND_FASTCALL zend_memnrstr_ex(const char *haystack, const char *needle, size_t needle_len, const char *end) /* {{{ */ |
3815 | 0 | { |
3816 | 0 | unsigned int td[256]; |
3817 | 0 | size_t i; |
3818 | 0 | const char *p; |
3819 | |
|
3820 | 0 | if (needle_len == 0 || (end - haystack) < needle_len) { |
3821 | 0 | return NULL; |
3822 | 0 | } |
3823 | | |
3824 | 0 | zend_memnstr_ex_pre(td, needle, needle_len, 1); |
3825 | |
|
3826 | 0 | p = end; |
3827 | 0 | p -= needle_len; |
3828 | |
|
3829 | 0 | while (p >= haystack) { |
3830 | 0 | for (i = 0; i < needle_len; i++) { |
3831 | 0 | if (needle[i] != p[i]) { |
3832 | 0 | break; |
3833 | 0 | } |
3834 | 0 | } |
3835 | |
|
3836 | 0 | if (i == needle_len) { |
3837 | 0 | return (const char *)p; |
3838 | 0 | } |
3839 | | |
3840 | 0 | if (UNEXPECTED(p == haystack)) { |
3841 | 0 | return NULL; |
3842 | 0 | } |
3843 | | |
3844 | 0 | p -= td[(unsigned char)(p[-1])]; |
3845 | 0 | } |
3846 | | |
3847 | 0 | return NULL; |
3848 | 0 | } |
3849 | | /* }}} */ |
3850 | | |
3851 | | #if SIZEOF_ZEND_LONG == 4 |
3852 | | ZEND_API zend_long ZEND_FASTCALL zend_dval_to_lval_slow(double d) /* {{{ */ |
3853 | | { |
3854 | | double two_pow_32 = pow(2., 32.), |
3855 | | dmod; |
3856 | | |
3857 | | dmod = fmod(d, two_pow_32); |
3858 | | if (dmod < 0) { |
3859 | | /* we're going to make this number positive; call ceil() |
3860 | | * to simulate rounding towards 0 of the negative number */ |
3861 | | dmod = ceil(dmod) + two_pow_32; |
3862 | | } |
3863 | | return (zend_long)(zend_ulong)dmod; |
3864 | | } |
3865 | | #else |
3866 | | ZEND_API zend_long ZEND_FASTCALL zend_dval_to_lval_slow(double d) |
3867 | 45.9k | { |
3868 | 45.9k | double two_pow_64 = pow(2., 64.), |
3869 | 45.9k | dmod; |
3870 | | |
3871 | 45.9k | dmod = fmod(d, two_pow_64); |
3872 | 45.9k | if (dmod < 0) { |
3873 | | /* no need to call ceil; original double must have had no |
3874 | | * fractional part, hence dmod does not have one either */ |
3875 | 4.39k | dmod += two_pow_64; |
3876 | 4.39k | } |
3877 | 45.9k | return (zend_long)(zend_ulong)dmod; |
3878 | 45.9k | } |
3879 | | /* }}} */ |
3880 | | #endif |