Coverage Report

Created: 2026-06-02 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_operators.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright © Zend Technologies Ltd., a subsidiary company of          |
6
   |     Perforce Software, Inc., and Contributors.                       |
7
   +----------------------------------------------------------------------+
8
   | This source file is subject to the Modified BSD License that is      |
9
   | bundled with this package in the file LICENSE, and is available      |
10
   | through the World Wide Web at <https://www.php.net/license/>.        |
11
   |                                                                      |
12
   | SPDX-License-Identifier: BSD-3-Clause                                |
13
   +----------------------------------------------------------------------+
14
   | Authors: Andi Gutmans <andi@php.net>                                 |
15
   |          Zeev Suraski <zeev@php.net>                                 |
16
   |          Dmitry Stogov <dmitry@php.net>                              |
17
   +----------------------------------------------------------------------+
18
*/
19
20
#include <ctype.h>
21
22
#include "zend.h"
23
#include "zend_operators.h"
24
#include "zend_variables.h"
25
#include "zend_globals.h"
26
#include "zend_list.h"
27
#include "zend_API.h"
28
#include "zend_strtod.h"
29
#include "zend_exceptions.h"
30
#include "zend_closures.h"
31
32
#include <locale.h>
33
#ifdef HAVE_LANGINFO_H
34
# include <langinfo.h>
35
#endif
36
37
#ifdef ZEND_INTRIN_AVX2_NATIVE
38
#include <immintrin.h>
39
#endif
40
#ifdef __SSE2__
41
#include <emmintrin.h>
42
#endif
43
#if defined(__aarch64__) || defined(_M_ARM64)
44
#include <arm_neon.h>
45
#endif
46
47
#if defined(ZEND_WIN32) && !defined(ZTS) && defined(_MSC_VER)
48
/* This performance improvement of tolower() on Windows gives 10-18% on bench.php */
49
#define ZEND_USE_TOLOWER_L 1
50
#endif
51
52
#ifdef ZEND_USE_TOLOWER_L
53
static _locale_t current_locale = NULL;
54
/* this is true global! may lead to strange effects on ZTS, but so may setlocale() */
55
#define zend_tolower(c) _tolower_l(c, current_locale)
56
#else
57
0
#define zend_tolower(c) tolower(c)
58
#endif
59
60
390k
#define TYPE_PAIR(t1,t2) (((t1) << 4) | (t2))
61
62
#ifdef ZEND_INTRIN_AVX2_NATIVE
63
#define HAVE_BLOCKCONV
64
65
#define BLOCKCONV_INIT_RANGE(start, end) \
66
  const __m256i blconv_offset = _mm256_set1_epi8((signed char)(SCHAR_MIN - start)); \
67
  const __m256i blconv_threshold = _mm256_set1_epi8(SCHAR_MIN + (end - start) + 1);
68
69
#define BLOCKCONV_STRIDE sizeof(__m256i)
70
71
#define BLOCKCONV_INIT_DELTA(delta) \
72
  const __m256i blconv_delta = _mm256_set1_epi8(delta);
73
74
#define BLOCKCONV_LOAD(input) \
75
  __m256i blconv_operand = _mm256_loadu_si256((__m256i*)(input)); \
76
  __m256i blconv_mask = _mm256_cmpgt_epi8(blconv_threshold, _mm256_add_epi8(blconv_operand, blconv_offset));
77
78
#define BLOCKCONV_FOUND() _mm256_movemask_epi8(blconv_mask)
79
80
#define BLOCKCONV_STORE(dest) \
81
  __m256i blconv_add = _mm256_and_si256(blconv_mask, blconv_delta); \
82
  __m256i blconv_result = _mm256_add_epi8(blconv_operand, blconv_add); \
83
  _mm256_storeu_si256((__m256i*)(dest), blconv_result);
84
85
#elif __SSE2__
86
#define HAVE_BLOCKCONV
87
88
/* Common code for SSE2 accelerated character case conversion */
89
90
#define BLOCKCONV_INIT_RANGE(start, end) \
91
200k
  const __m128i blconv_offset = _mm_set1_epi8((signed char)(SCHAR_MIN - start)); \
92
200k
  const __m128i blconv_threshold = _mm_set1_epi8(SCHAR_MIN + (end - start) + 1);
93
94
275k
#define BLOCKCONV_STRIDE sizeof(__m128i)
95
96
#define BLOCKCONV_INIT_DELTA(delta) \
97
8.86k
  const __m128i blconv_delta = _mm_set1_epi8(delta);
98
99
#define BLOCKCONV_LOAD(input) \
100
28.1k
  __m128i blconv_operand = _mm_loadu_si128((__m128i*)(input)); \
101
28.1k
  __m128i blconv_mask = _mm_cmplt_epi8(_mm_add_epi8(blconv_operand, blconv_offset), blconv_threshold);
102
103
21.5k
#define BLOCKCONV_FOUND() _mm_movemask_epi8(blconv_mask)
104
105
#define BLOCKCONV_STORE(dest) \
106
13.7k
  __m128i blconv_add = _mm_and_si128(blconv_mask, blconv_delta); \
107
13.7k
  __m128i blconv_result = _mm_add_epi8(blconv_operand, blconv_add); \
108
13.7k
  _mm_storeu_si128((__m128i *)(dest), blconv_result);
109
110
#elif defined(__aarch64__) || defined(_M_ARM64)
111
#define HAVE_BLOCKCONV
112
113
#define BLOCKCONV_INIT_RANGE(start, end) \
114
  const int8x16_t blconv_offset = vdupq_n_s8((signed char)(SCHAR_MIN - start)); \
115
  const int8x16_t blconv_threshold = vdupq_n_s8(SCHAR_MIN + (end - start) + 1);
116
117
#define BLOCKCONV_STRIDE sizeof(int8x16_t)
118
119
#define BLOCKCONV_INIT_DELTA(delta) \
120
  const int8x16_t blconv_delta = vdupq_n_s8(delta);
121
122
#define BLOCKCONV_LOAD(input) \
123
  int8x16_t blconv_operand = vld1q_s8((const int8_t*)(input)); \
124
  uint8x16_t blconv_mask = vcltq_s8(vreinterpretq_s8_u8(vaddq_u8(vreinterpretq_u8_s8(blconv_operand), vreinterpretq_u8_s8(blconv_offset))), blconv_threshold);
125
126
#define BLOCKCONV_FOUND() vmaxvq_u8(blconv_mask)
127
128
#define BLOCKCONV_STORE(dest) \
129
  int8x16_t blconv_add = vandq_s8(vreinterpretq_s8_u8(blconv_mask), blconv_delta); \
130
  int8x16_t blconv_result = vaddq_s8(blconv_operand, blconv_add); \
131
  vst1q_s8((int8_t *)(dest), blconv_result);
132
133
#endif /* defined(__aarch64__) || defined(_M_ARM64) */
134
135
ZEND_API const unsigned char zend_tolower_map[256] = {
136
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
137
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
138
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
139
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
140
0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
141
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x5b,0x5c,0x5d,0x5e,0x5f,
142
0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
143
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,
144
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
145
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
146
0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
147
0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
148
0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
149
0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
150
0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
151
0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
152
};
153
154
ZEND_API const unsigned char zend_toupper_map[256] = {
155
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
156
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
157
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
158
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
159
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
160
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
161
0x60,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
162
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x7b,0x7c,0x7d,0x7e,0x7f,
163
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
164
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
165
0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
166
0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
167
0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
168
0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
169
0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
170
0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
171
};
172
173
174
/**
175
 * Functions using locale lowercase:
176
    zend_binary_strncasecmp_l
177
    zend_binary_strcasecmp_l
178
 * Functions using ascii lowercase:
179
    string_compare_function_ex
180
    string_case_compare_function
181
      zend_str_tolower_copy
182
    zend_str_tolower_dup
183
    zend_str_tolower
184
    zend_binary_strcasecmp
185
    zend_binary_strncasecmp
186
 */
187
188
static zend_long ZEND_FASTCALL zend_atol_internal(const char *str, size_t str_len) /* {{{ */
189
0
{
190
0
  if (!str_len) {
191
0
    str_len = strlen(str);
192
0
  }
193
194
  /* Perform following multiplications on unsigned to avoid overflow UB.
195
   * For now overflow is silently ignored -- not clear what else can be
196
   * done here, especially as the final result of this function may be
197
   * used in an unsigned context (e.g. "memory_limit=3G", which overflows
198
   * zend_long on 32-bit, but not size_t). */
199
0
  zend_ulong retval = (zend_ulong) ZEND_STRTOL(str, NULL, 0);
200
0
  if (str_len>0) {
201
0
    switch (str[str_len-1]) {
202
0
      case 'g':
203
0
      case 'G':
204
0
        retval *= 1024;
205
0
        ZEND_FALLTHROUGH;
206
0
      case 'm':
207
0
      case 'M':
208
0
        retval *= 1024;
209
0
        ZEND_FALLTHROUGH;
210
0
      case 'k':
211
0
      case 'K':
212
0
        retval *= 1024;
213
0
        break;
214
0
    }
215
0
  }
216
0
  return (zend_long) retval;
217
0
}
218
/* }}} */
219
220
ZEND_API zend_long ZEND_FASTCALL zend_atol(const char *str, size_t str_len)
221
0
{
222
0
  return zend_atol_internal(str, str_len);
223
0
}
224
225
ZEND_API int ZEND_FASTCALL zend_atoi(const char *str, size_t str_len)
226
0
{
227
0
  return (int) zend_atol_internal(str, str_len);
228
0
}
229
230
/* {{{ convert_object_to_type: dst will be either ctype or UNDEF */
231
#define convert_object_to_type(op, dst, ctype)                  \
232
15
  ZVAL_UNDEF(dst);                                    \
233
15
  if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), dst, ctype) == FAILURE) {         \
234
15
    zend_error(E_WARNING,                               \
235
15
      "Object of class %s could not be converted to %s", ZSTR_VAL(Z_OBJCE_P(op)->name),\
236
15
    zend_get_type_by_const(ctype));                           \
237
15
  }                                            \
238
239
/* }}} */
240
241
ZEND_API void ZEND_FASTCALL convert_scalar_to_number(zval *op) /* {{{ */
242
0
{
243
0
try_again:
244
0
  switch (Z_TYPE_P(op)) {
245
0
    case IS_REFERENCE:
246
0
      zend_unwrap_reference(op);
247
0
      goto try_again;
248
0
    case IS_STRING:
249
0
      {
250
0
        zend_string *str;
251
252
0
        str = Z_STR_P(op);
253
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) {
254
0
          ZVAL_LONG(op, 0);
255
0
        }
256
0
        zend_string_release_ex(str, 0);
257
0
        break;
258
0
      }
259
0
    case IS_NULL:
260
0
    case IS_FALSE:
261
0
      ZVAL_LONG(op, 0);
262
0
      break;
263
0
    case IS_TRUE:
264
0
      ZVAL_LONG(op, 1);
265
0
      break;
266
0
    case IS_RESOURCE:
267
0
      {
268
0
        zend_long l = Z_RES_HANDLE_P(op);
269
0
        zval_ptr_dtor(op);
270
0
        ZVAL_LONG(op, l);
271
0
      }
272
0
      break;
273
0
    case IS_OBJECT:
274
0
      {
275
0
        zval dst;
276
277
0
        convert_object_to_type(op, &dst, _IS_NUMBER);
278
0
        zval_ptr_dtor(op);
279
280
0
        if (Z_TYPE(dst) == IS_LONG || Z_TYPE(dst) == IS_DOUBLE) {
281
0
          ZVAL_COPY_VALUE(op, &dst);
282
0
        } else {
283
0
          ZVAL_LONG(op, 1);
284
0
        }
285
0
      }
286
0
      break;
287
0
  }
288
0
}
289
/* }}} */
290
291
static zend_never_inline zval* ZEND_FASTCALL _zendi_convert_scalar_to_number_silent(zval *op, zval *holder) /* {{{ */
292
4.44k
{
293
4.44k
  switch (Z_TYPE_P(op)) {
294
0
    case IS_NULL:
295
0
    case IS_FALSE:
296
0
      ZVAL_LONG(holder, 0);
297
0
      return holder;
298
0
    case IS_TRUE:
299
0
      ZVAL_LONG(holder, 1);
300
0
      return holder;
301
497
    case IS_STRING:
302
497
      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) {
303
293
        ZVAL_LONG(holder, 0);
304
293
      }
305
497
      return holder;
306
0
    case IS_RESOURCE:
307
0
      ZVAL_LONG(holder, Z_RES_HANDLE_P(op));
308
0
      return holder;
309
0
    case IS_OBJECT:
310
0
      convert_object_to_type(op, holder, _IS_NUMBER);
311
0
      if (UNEXPECTED(EG(exception)) ||
312
0
          UNEXPECTED(Z_TYPE_P(holder) != IS_LONG && Z_TYPE_P(holder) != IS_DOUBLE)) {
313
0
        ZVAL_LONG(holder, 1);
314
0
      }
315
0
      return holder;
316
1.68k
    case IS_LONG:
317
1.69k
    case IS_DOUBLE:
318
3.94k
    default:
319
3.94k
      return op;
320
4.44k
  }
321
4.44k
}
322
/* }}} */
323
324
static zend_never_inline zend_result ZEND_FASTCALL _zendi_try_convert_scalar_to_number(zval *op, zval *holder) /* {{{ */
325
60.2k
{
326
60.2k
  switch (Z_TYPE_P(op)) {
327
32.9k
    case IS_NULL:
328
40.4k
    case IS_FALSE:
329
40.4k
      ZVAL_LONG(holder, 0);
330
40.4k
      return SUCCESS;
331
3.84k
    case IS_TRUE:
332
3.84k
      ZVAL_LONG(holder, 1);
333
3.84k
      return SUCCESS;
334
15.7k
    case IS_STRING:
335
15.7k
    {
336
15.7k
      bool trailing_data = false;
337
      /* For BC reasons we allow errors so that we can warn on leading numeric string */
338
15.7k
      if (0 == (Z_TYPE_INFO_P(holder) = is_numeric_string_ex(Z_STRVAL_P(op), Z_STRLEN_P(op),
339
15.7k
          &Z_LVAL_P(holder), &Z_DVAL_P(holder),  /* allow errors */ true, NULL, &trailing_data))) {
340
        /* Will lead to invalid OP type error */
341
264
        return FAILURE;
342
264
      }
343
15.5k
      if (UNEXPECTED(trailing_data)) {
344
8.21k
        zend_error(E_WARNING, "A non-numeric value encountered");
345
8.21k
        if (UNEXPECTED(EG(exception))) {
346
0
          return FAILURE;
347
0
        }
348
8.21k
      }
349
15.5k
      return SUCCESS;
350
15.5k
    }
351
73
    case IS_OBJECT:
352
73
      if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), holder, _IS_NUMBER) == FAILURE
353
73
          || EG(exception)) {
354
73
        return FAILURE;
355
73
      }
356
0
      ZEND_ASSERT(Z_TYPE_P(holder) == IS_LONG || Z_TYPE_P(holder) == IS_DOUBLE);
357
0
      return SUCCESS;
358
0
    case IS_RESOURCE:
359
117
    case IS_ARRAY:
360
117
      return FAILURE;
361
0
    default: ZEND_UNREACHABLE();
362
60.2k
  }
363
60.2k
}
364
/* }}} */
365
366
static zend_always_inline zend_result zendi_try_convert_scalar_to_number(zval *op, zval *holder) /* {{{ */
367
101k
{
368
101k
  if (Z_TYPE_P(op) == IS_LONG || Z_TYPE_P(op) == IS_DOUBLE) {
369
41.4k
    ZVAL_COPY_VALUE(holder, op);
370
41.4k
    return SUCCESS;
371
60.2k
  } else {
372
60.2k
    return _zendi_try_convert_scalar_to_number(op, holder);
373
60.2k
  }
374
101k
}
375
/* }}} */
376
377
static zend_never_inline zend_long ZEND_FASTCALL zendi_try_get_long(const zval *op, bool *failed) /* {{{ */
378
29.3k
{
379
29.3k
  *failed = false;
380
29.3k
try_again:
381
29.3k
  switch (Z_TYPE_P(op)) {
382
4.79k
    case IS_NULL:
383
5.70k
    case IS_FALSE:
384
5.70k
      return 0;
385
680
    case IS_TRUE:
386
680
      return 1;
387
17.4k
    case IS_DOUBLE: {
388
17.4k
      double dval = Z_DVAL_P(op);
389
17.4k
      zend_long lval = zend_dval_to_lval_safe(dval);
390
17.4k
      if (UNEXPECTED(EG(exception))) {
391
0
        *failed = true;
392
0
      }
393
17.4k
      return lval;
394
4.79k
    }
395
5.47k
    case IS_STRING:
396
5.47k
      {
397
5.47k
        uint8_t type;
398
5.47k
        zend_long lval;
399
5.47k
        double dval;
400
5.47k
        bool trailing_data = false;
401
5.47k
        zend_string *op_str = NULL; /* protect against error handlers */
402
403
        /* For BC reasons we allow errors so that we can warn on leading numeric string */
404
5.47k
        type = is_numeric_string_ex(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval,
405
5.47k
          /* allow errors */ true, NULL, &trailing_data);
406
5.47k
        if (type == 0) {
407
829
          *failed = true;
408
829
          return 0;
409
829
        }
410
4.64k
        if (UNEXPECTED(trailing_data)) {
411
2.28k
          if (type != IS_LONG) {
412
627
            op_str = zend_string_copy(Z_STR_P(op));
413
627
          }
414
2.28k
          zend_error(E_WARNING, "A non-numeric value encountered");
415
2.28k
          if (UNEXPECTED(EG(exception))) {
416
0
            *failed = true;
417
0
            zend_tmp_string_release(op_str);
418
0
            return 0;
419
0
          }
420
2.28k
        }
421
4.64k
        if (EXPECTED(type == IS_LONG)) {
422
3.20k
          return lval;
423
3.20k
        } else {
424
          /* Previously we used strtol here, not is_numeric_string,
425
           * and strtol gives you LONG_MAX/_MIN on overflow.
426
           * We use use saturating conversion to emulate strtol()'s
427
           * behaviour.
428
           */
429
1.44k
          lval = zend_dval_to_lval_cap(dval);
430
1.44k
          if (!zend_is_long_compatible(dval, lval)) {
431
1.29k
            zend_incompatible_string_to_long_error(op_str ? op_str : Z_STR_P(op));
432
1.29k
            if (UNEXPECTED(EG(exception))) {
433
0
              *failed = true;
434
0
            }
435
1.29k
          }
436
1.44k
          zend_tmp_string_release(op_str);
437
1.44k
          return lval;
438
1.44k
        }
439
4.64k
      }
440
48
    case IS_OBJECT:
441
48
      {
442
48
        zval dst;
443
48
        if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &dst, IS_LONG) == FAILURE
444
48
            || EG(exception)) {
445
48
          *failed = true;
446
48
          return 0;
447
48
        }
448
0
        ZEND_ASSERT(Z_TYPE(dst) == IS_LONG);
449
0
        return Z_LVAL(dst);
450
0
      }
451
0
    case IS_UNDEF:
452
0
    case IS_RESOURCE:
453
18
    case IS_ARRAY:
454
18
      *failed = true;
455
18
      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
0
    default: ZEND_UNREACHABLE();
465
29.3k
  }
466
29.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
125k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \
480
125k
    && 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
162k
  if (UNEXPECTED(Z_TYPE_P(op2) == IS_OBJECT) \
488
162k
    && UNEXPECTED(Z_OBJ_HANDLER_P(op2, do_operation)) \
489
162k
    && 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
108k
  ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode) \
495
108k
  else \
496
108k
  ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode)
497
498
#define ZEND_TRY_UNARY_OBJECT_OPERATION(opcode) \
499
357
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \
500
357
    && UNEXPECTED(Z_OBJ_HANDLER_P(op1, do_operation)) \
501
357
    && 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
11.4k
  do {                               \
507
11.4k
    if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {           \
508
7.00k
      bool failed;                      \
509
7.00k
      if (Z_ISREF_P(op1)) {                   \
510
3
        op1 = Z_REFVAL_P(op1);                  \
511
3
        if (Z_TYPE_P(op1) == IS_LONG) {             \
512
0
          op1_lval = Z_LVAL_P(op1);              \
513
0
          break;                        \
514
0
        }                            \
515
3
      }                              \
516
7.00k
      ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode);       \
517
7.00k
      op1_lval = zendi_try_get_long(op1, &failed);        \
518
7.00k
      if (UNEXPECTED(failed)) {                 \
519
288
        zend_binop_error(sigil, op1, op2);            \
520
288
        if (result != op1) {                 \
521
243
          ZVAL_UNDEF(result);                 \
522
243
        }                            \
523
288
        return FAILURE;                     \
524
288
      }                              \
525
7.00k
    } else {                           \
526
4.39k
      op1_lval = Z_LVAL_P(op1);                  \
527
4.39k
    }                                \
528
11.4k
  } while (0);                            \
529
11.4k
  do {                               \
530
11.1k
    if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {           \
531
3.66k
      bool failed;                      \
532
3.66k
      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
3.66k
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode);       \
540
3.66k
      op2_lval = zendi_try_get_long(op2, &failed);        \
541
3.66k
      if (UNEXPECTED(failed)) {                 \
542
305
        zend_binop_error(sigil, op1, op2);            \
543
305
        if (result != op1) {                 \
544
299
          ZVAL_UNDEF(result);                 \
545
299
        }                            \
546
305
        return FAILURE;                     \
547
305
      }                              \
548
7.44k
    } else {                           \
549
7.44k
      op2_lval = Z_LVAL_P(op2);                  \
550
7.44k
    }                                \
551
11.1k
  } while (0);
552
553
ZEND_API void ZEND_FASTCALL convert_to_long(zval *op) /* {{{ */
554
24
{
555
24
  zend_long tmp;
556
557
24
try_again:
558
24
  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
15
    case IS_LONG:
572
15
      break;
573
6
    case IS_DOUBLE: {
574
      /* NAN might emit a warning */
575
6
      zend_long new_value = zend_dval_to_lval(Z_DVAL_P(op));
576
6
      zval_ptr_dtor(op);
577
6
      ZVAL_LONG(op, new_value);
578
6
      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
0
    default: ZEND_UNREACHABLE();
610
24
  }
611
24
}
612
/* }}} */
613
614
ZEND_API void ZEND_FASTCALL convert_to_double(zval *op) /* {{{ */
615
10
{
616
10
  double tmp;
617
618
10
try_again:
619
10
  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
10
    case IS_LONG:
634
10
      ZVAL_DOUBLE(op, (double) Z_LVAL_P(op));
635
10
      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
0
    default: ZEND_UNREACHABLE();
669
10
  }
670
10
}
671
/* }}} */
672
673
ZEND_API void ZEND_FASTCALL convert_to_null(zval *op) /* {{{ */
674
12
{
675
12
  if (UNEXPECTED(Z_TYPE_P(op) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op)))) {
676
9
    zend_nan_coerced_to_type_warning(IS_NULL);
677
9
  }
678
12
  zval_ptr_dtor(op);
679
12
  ZVAL_NULL(op);
680
12
}
681
/* }}} */
682
683
ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op) /* {{{ */
684
9
{
685
9
  bool tmp;
686
687
9
try_again:
688
9
  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
6
    case IS_DOUBLE: {
706
      /* We compute the new value before emitting the warning as the zval may change */
707
6
      bool new_value = Z_DVAL_P(op) ? true : false;
708
6
      if (UNEXPECTED(zend_isnan(Z_DVAL_P(op)))) {
709
6
        zend_nan_coerced_to_type_warning(_IS_BOOL);
710
6
        zval_ptr_dtor(op);
711
6
      }
712
6
      ZVAL_BOOL(op, new_value);
713
6
      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
0
    default: ZEND_UNREACHABLE();
751
9
  }
752
9
}
753
/* }}} */
754
755
ZEND_API void ZEND_FASTCALL _convert_to_string(zval *op) /* {{{ */
756
5.89k
{
757
5.89k
try_again:
758
5.89k
  switch (Z_TYPE_P(op)) {
759
0
    case IS_UNDEF:
760
2.03k
    case IS_NULL:
761
3.42k
    case IS_FALSE: {
762
3.42k
      ZVAL_EMPTY_STRING(op);
763
3.42k
      break;
764
2.03k
    }
765
1.22k
    case IS_TRUE:
766
1.22k
      ZVAL_CHAR(op, '1');
767
1.22k
      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
2.03k
    }
776
1.13k
    case IS_LONG:
777
1.13k
      ZVAL_STR(op, zend_long_to_str(Z_LVAL_P(op)));
778
1.13k
      break;
779
109
    case IS_DOUBLE: {
780
      /* Casting NAN will cause a warning */
781
109
      zend_string *new_value = zend_double_to_str(Z_DVAL_P(op));
782
109
      zval_ptr_dtor(op);
783
109
      ZVAL_NEW_STR(op, new_value);
784
109
      break;
785
2.03k
    }
786
2
    case IS_ARRAY:
787
2
      zend_error(E_WARNING, "Array to string conversion");
788
2
      zval_ptr_dtor(op);
789
2
      ZVAL_INTERNED_STR(op, ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED));
790
2
      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
0
    default: ZEND_UNREACHABLE();
809
5.89k
  }
810
5.89k
}
811
/* }}} */
812
813
ZEND_API bool ZEND_FASTCALL _try_convert_to_string(zval *op) /* {{{ */
814
15
{
815
15
  zend_string *str;
816
817
15
  ZEND_ASSERT(Z_TYPE_P(op) != IS_STRING);
818
15
  str = zval_try_get_string_func(op);
819
15
  if (UNEXPECTED(!str)) {
820
12
    return 0;
821
12
  }
822
3
  zval_ptr_dtor(op);
823
3
  ZVAL_STR(op, str);
824
3
  return 1;
825
15
}
826
/* }}} */
827
828
static void convert_scalar_to_array(zval *op) /* {{{ */
829
17
{
830
17
  if (UNEXPECTED(Z_TYPE_P(op) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op)))) {
831
3
    zend_nan_coerced_to_type_warning(IS_ARRAY);
832
3
  }
833
17
  HashTable *ht = zend_new_array(1);
834
17
  zend_hash_index_add_new(ht, 0, op);
835
17
  ZVAL_ARR(op, ht);
836
17
}
837
/* }}} */
838
839
ZEND_API void ZEND_FASTCALL convert_to_array(zval *op) /* {{{ */
840
87
{
841
87
try_again:
842
87
  switch (Z_TYPE_P(op)) {
843
24
    case IS_ARRAY:
844
24
      break;
845
/* OBJECTS_OPTIMIZE */
846
27
    case IS_OBJECT:
847
27
      if (Z_OBJCE_P(op) == zend_ce_closure) {
848
3
        convert_scalar_to_array(op);
849
24
      } else if (ZEND_STD_BUILD_OBJECT_PROPERTIES_ARRAY_COMPATIBLE(op)) {
850
        /* Optimized version without rebuilding properties HashTable */
851
12
        HashTable *ht = zend_std_build_object_properties_array(Z_OBJ_P(op));
852
12
        OBJ_RELEASE(Z_OBJ_P(op));
853
12
        ZVAL_ARR(op, ht);
854
12
      } else {
855
12
        HashTable *obj_ht = zend_get_properties_for(op, ZEND_PROP_PURPOSE_ARRAY_CAST);
856
12
        if (obj_ht) {
857
12
          HashTable *new_obj_ht = zend_proptable_to_symtable(obj_ht,
858
12
            (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
12
          zval_ptr_dtor(op);
862
12
          ZVAL_ARR(op, new_obj_ht);
863
12
          zend_release_properties(obj_ht);
864
12
        } else {
865
0
          zval_ptr_dtor(op);
866
          /*ZVAL_EMPTY_ARRAY(op);*/
867
0
          array_init(op);
868
0
        }
869
12
      }
870
27
      break;
871
22
    case IS_NULL:
872
      /*ZVAL_EMPTY_ARRAY(op);*/
873
22
      array_init(op);
874
22
      break;
875
0
    case IS_REFERENCE:
876
0
      zend_unwrap_reference(op);
877
0
      goto try_again;
878
14
    default:
879
14
      convert_scalar_to_array(op);
880
14
      break;
881
87
  }
882
87
}
883
/* }}} */
884
885
ZEND_API void ZEND_FASTCALL convert_to_object(zval *op) /* {{{ */
886
6
{
887
6
try_again:
888
6
  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
3
    case IS_DOUBLE:
916
3
      if (UNEXPECTED(zend_isnan(Z_DVAL_P(op)))) {
917
3
        zend_nan_coerced_to_type_warning(IS_OBJECT);
918
3
      }
919
3
      ZEND_FALLTHROUGH;
920
3
    default: {
921
3
      zval tmp;
922
3
      ZVAL_COPY_VALUE(&tmp, op);
923
3
      object_init(op);
924
3
      zend_hash_add_new(Z_OBJPROP_P(op), ZSTR_KNOWN(ZEND_STR_SCALAR), &tmp);
925
3
      break;
926
3
    }
927
6
  }
928
6
}
929
/* }}} */
930
931
ZEND_API void ZEND_COLD zend_incompatible_double_to_long_error(double d)
932
11.3k
{
933
11.3k
  zend_error_unchecked(E_DEPRECATED, "Implicit conversion from float %.*H to int loses precision", -1, d);
934
11.3k
}
935
ZEND_API void ZEND_COLD zend_incompatible_string_to_long_error(const zend_string *s)
936
1.32k
{
937
1.32k
  zend_error(E_DEPRECATED, "Implicit conversion from float-string \"%s\" to int loses precision", ZSTR_VAL(s));
938
1.32k
}
939
ZEND_API void ZEND_COLD zend_oob_double_to_long_error(double d)
940
6.63k
{
941
6.63k
  zend_error_unchecked(E_WARNING, "The float %.*H is not representable as an int, cast occurred", -1, d);
942
6.63k
}
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
150
{
950
150
  zend_error(E_WARNING, "unexpected NAN value was coerced to %s", zend_get_type_by_const(type));
951
150
}
952
953
ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(const zval *op, bool is_strict) /* {{{ */
954
2.14k
{
955
2.15k
try_again:
956
2.15k
  switch (Z_TYPE_P(op)) {
957
51
    case IS_UNDEF:
958
162
    case IS_NULL:
959
846
    case IS_FALSE:
960
846
      return 0;
961
543
    case IS_TRUE:
962
543
      return 1;
963
0
    case IS_RESOURCE:
964
0
      return Z_RES_HANDLE_P(op);
965
6
    case IS_LONG:
966
6
      return Z_LVAL_P(op);
967
354
    case IS_DOUBLE: {
968
354
      double dval = Z_DVAL_P(op);
969
354
      zend_long lval = zend_dval_to_lval(dval);
970
354
      if (UNEXPECTED(is_strict)) {
971
51
        if (!zend_is_long_compatible(dval, lval)) {
972
51
          zend_incompatible_double_to_long_error(dval);
973
51
        }
974
51
      }
975
354
      return lval;
976
162
    }
977
388
    case IS_STRING:
978
388
      {
979
388
        uint8_t type;
980
388
        zend_long lval;
981
388
        double dval;
982
388
        if (0 == (type = is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval, true))) {
983
29
          return 0;
984
359
        } else if (EXPECTED(type == IS_LONG)) {
985
156
          return lval;
986
203
        } 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
203
          lval = zend_dval_to_lval_cap(dval);
994
203
          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
203
          return lval;
1000
203
        }
1001
388
      }
1002
0
    case IS_ARRAY:
1003
0
      return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1 : 0;
1004
12
    case IS_OBJECT:
1005
12
      {
1006
12
        zval dst;
1007
12
        convert_object_to_type(op, &dst, IS_LONG);
1008
12
        if (Z_TYPE(dst) == IS_LONG) {
1009
0
          return Z_LVAL(dst);
1010
12
        } else {
1011
12
          return 1;
1012
12
        }
1013
12
      }
1014
6
    case IS_REFERENCE:
1015
6
      op = Z_REFVAL_P(op);
1016
6
      goto try_again;
1017
0
    default: ZEND_UNREACHABLE();
1018
2.15k
  }
1019
0
  return 0;
1020
2.15k
}
1021
/* }}} */
1022
1023
ZEND_API double ZEND_FASTCALL zval_get_double_func(const zval *op) /* {{{ */
1024
6.26k
{
1025
6.26k
try_again:
1026
6.26k
  switch (Z_TYPE_P(op)) {
1027
48
    case IS_NULL:
1028
438
    case IS_FALSE:
1029
438
      return 0.0;
1030
0
    case IS_TRUE:
1031
0
      return 1.0;
1032
0
    case IS_RESOURCE:
1033
0
      return (double) Z_RES_HANDLE_P(op);
1034
1.00k
    case IS_LONG:
1035
1.00k
      return (double) Z_LVAL_P(op);
1036
0
    case IS_DOUBLE:
1037
0
      return Z_DVAL_P(op);
1038
4.21k
    case IS_STRING:
1039
4.21k
      return zend_strtod(Z_STRVAL_P(op), NULL);
1040
612
    case IS_ARRAY:
1041
612
      return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1.0 : 0.0;
1042
3
    case IS_OBJECT:
1043
3
      {
1044
3
        zval dst;
1045
3
        convert_object_to_type(op, &dst, IS_DOUBLE);
1046
1047
3
        if (Z_TYPE(dst) == IS_DOUBLE) {
1048
0
          return Z_DVAL(dst);
1049
3
        } else {
1050
3
          return 1.0;
1051
3
        }
1052
3
      }
1053
0
    case IS_REFERENCE:
1054
0
      op = Z_REFVAL_P(op);
1055
0
      goto try_again;
1056
0
    default: ZEND_UNREACHABLE();
1057
6.26k
  }
1058
0
  return 0.0;
1059
6.26k
}
1060
/* }}} */
1061
1062
static zend_always_inline zend_string* __zval_get_string_func(const zval *op, bool try) /* {{{ */
1063
348k
{
1064
351k
try_again:
1065
351k
  switch (Z_TYPE_P(op)) {
1066
184k
    case IS_UNDEF:
1067
276k
    case IS_NULL:
1068
291k
    case IS_FALSE:
1069
291k
      return ZSTR_EMPTY_ALLOC();
1070
9.27k
    case IS_TRUE:
1071
9.27k
      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
36.7k
    case IS_LONG:
1075
36.7k
      return zend_long_to_str(Z_LVAL_P(op));
1076
7.42k
    case IS_DOUBLE:
1077
7.42k
      return zend_double_to_str(Z_DVAL_P(op));
1078
1.45k
    case IS_ARRAY:
1079
1.45k
      zend_error(E_WARNING, "Array to string conversion");
1080
1.45k
      return (try && UNEXPECTED(EG(exception))) ?
1081
1.45k
        NULL : ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
1082
2.12k
    case IS_OBJECT: {
1083
2.12k
      zval tmp;
1084
2.12k
      if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &tmp, IS_STRING) == SUCCESS) {
1085
1.77k
        return Z_STR(tmp);
1086
1.77k
      }
1087
357
      if (!EG(exception)) {
1088
48
        zend_throw_error(NULL, "Object of class %s could not be converted to string", ZSTR_VAL(Z_OBJCE_P(op)->name));
1089
48
      }
1090
357
      return try ? NULL : ZSTR_EMPTY_ALLOC();
1091
2.12k
    }
1092
2.23k
    case IS_REFERENCE:
1093
2.23k
      op = Z_REFVAL_P(op);
1094
2.23k
      goto try_again;
1095
294
    case IS_STRING:
1096
294
      return zend_string_copy(Z_STR_P(op));
1097
0
    default: ZEND_UNREACHABLE();
1098
351k
  }
1099
0
  return NULL;
1100
351k
}
1101
/* }}} */
1102
1103
ZEND_API zend_string* ZEND_FASTCALL zval_get_string_func(const zval *op) /* {{{ */
1104
241k
{
1105
241k
  return __zval_get_string_func(op, false);
1106
241k
}
1107
/* }}} */
1108
1109
ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_func(const zval *op) /* {{{ */
1110
107k
{
1111
107k
  return __zval_get_string_func(op, true);
1112
107k
}
1113
/* }}} */
1114
1115
1.34k
static ZEND_COLD zend_never_inline void ZEND_FASTCALL zend_binop_error(const char *operator, const zval *op1, const zval *op2) /* {{{ */ {
1116
1.34k
  if (EG(exception)) {
1117
0
    return;
1118
0
  }
1119
1120
1.34k
  zend_type_error("Unsupported operand types: %s %s %s",
1121
1.34k
    zend_zval_type_name(op1), operator, zend_zval_type_name(op2));
1122
1.34k
}
1123
/* }}} */
1124
1125
static zend_never_inline void ZEND_FASTCALL add_function_array(zval *result, const zval *op1, const zval *op2) /* {{{ */
1126
666
{
1127
666
  if (result == op1 && Z_ARR_P(op1) == Z_ARR_P(op2)) {
1128
    /* $a += $a */
1129
33
    return;
1130
33
  }
1131
633
  if (result != op1) {
1132
613
    ZVAL_ARR(result, zend_array_dup(Z_ARR_P(op1)));
1133
613
  } else {
1134
20
    SEPARATE_ARRAY(result);
1135
20
  }
1136
633
  zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0);
1137
633
}
1138
/* }}} */
1139
1140
static zend_always_inline zend_result add_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1141
85.2k
{
1142
85.2k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1143
1144
85.2k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1145
24.6k
    fast_long_add_function(result, op1, op2);
1146
24.6k
    return SUCCESS;
1147
60.5k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1148
1.88k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) + Z_DVAL_P(op2));
1149
1.88k
    return SUCCESS;
1150
58.7k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1151
2.70k
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) + Z_DVAL_P(op2));
1152
2.70k
    return SUCCESS;
1153
56.0k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1154
3.00k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) + ((double)Z_LVAL_P(op2)));
1155
3.00k
    return SUCCESS;
1156
52.9k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_ARRAY, IS_ARRAY))) {
1157
666
    add_function_array(result, op1, op2);
1158
666
    return SUCCESS;
1159
52.3k
  } else {
1160
52.3k
    return FAILURE;
1161
52.3k
  }
1162
85.2k
} /* }}} */
1163
1164
static zend_never_inline zend_result ZEND_FASTCALL add_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1165
27.5k
{
1166
27.5k
  ZVAL_DEREF(op1);
1167
27.5k
  ZVAL_DEREF(op2);
1168
27.5k
  if (add_function_fast(result, op1, op2) == SUCCESS) {
1169
2.76k
    return SUCCESS;
1170
2.76k
  }
1171
1172
27.5k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_ADD);
1173
1174
24.7k
  zval op1_copy, op2_copy;
1175
24.7k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1176
24.6k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1177
123
    zend_binop_error("+", op1, op2);
1178
123
    if (result != op1) {
1179
84
      ZVAL_UNDEF(result);
1180
84
    }
1181
123
    return FAILURE;
1182
123
  }
1183
1184
24.6k
  if (result == op1) {
1185
4.56k
    zval_ptr_dtor(result);
1186
4.56k
  }
1187
1188
24.6k
  if (add_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1189
24.6k
    return SUCCESS;
1190
24.6k
  }
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
33.0k
{
1198
33.0k
  if (add_function_fast(result, op1, op2) == SUCCESS) {
1199
5.48k
    return SUCCESS;
1200
27.5k
  } else {
1201
27.5k
    return add_function_slow(result, op1, op2);
1202
27.5k
  }
1203
33.0k
}
1204
/* }}} */
1205
1206
static zend_always_inline zend_result sub_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1207
15.7k
{
1208
15.7k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1209
1210
15.7k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1211
5.57k
    fast_long_sub_function(result, op1, op2);
1212
5.57k
    return SUCCESS;
1213
10.1k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1214
231
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) - Z_DVAL_P(op2));
1215
231
    return SUCCESS;
1216
9.92k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1217
240
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) - Z_DVAL_P(op2));
1218
240
    return SUCCESS;
1219
9.68k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1220
668
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) - ((double)Z_LVAL_P(op2)));
1221
668
    return SUCCESS;
1222
9.01k
  } else {
1223
9.01k
    return FAILURE;
1224
9.01k
  }
1225
15.7k
}
1226
/* }}} */
1227
1228
static zend_never_inline zend_result ZEND_FASTCALL sub_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1229
4.67k
{
1230
4.67k
  ZVAL_DEREF(op1);
1231
4.67k
  ZVAL_DEREF(op2);
1232
4.67k
  if (sub_function_fast(result, op1, op2) == SUCCESS) {
1233
335
    return SUCCESS;
1234
335
  }
1235
1236
4.67k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SUB);
1237
1238
4.34k
  zval op1_copy, op2_copy;
1239
4.34k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1240
4.25k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1241
121
    zend_binop_error("-", op1, op2);
1242
121
    if (result != op1) {
1243
94
      ZVAL_UNDEF(result);
1244
94
    }
1245
121
    return FAILURE;
1246
121
  }
1247
1248
4.22k
  if (result == op1) {
1249
508
    zval_ptr_dtor(result);
1250
508
  }
1251
1252
4.22k
  if (sub_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1253
4.22k
    return SUCCESS;
1254
4.22k
  }
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
6.83k
{
1263
6.83k
  if (sub_function_fast(result, op1, op2) == SUCCESS) {
1264
2.16k
    return SUCCESS;
1265
4.67k
  } else {
1266
4.67k
    return sub_function_slow(result, op1, op2);
1267
4.67k
  }
1268
6.83k
}
1269
/* }}} */
1270
1271
static zend_always_inline zend_result mul_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1272
71.0k
{
1273
71.0k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1274
1275
71.0k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1276
26.8k
    zend_long overflow;
1277
26.8k
    ZEND_SIGNED_MULTIPLY_LONG(
1278
26.8k
      Z_LVAL_P(op1), Z_LVAL_P(op2),
1279
26.8k
      Z_LVAL_P(result), Z_DVAL_P(result), overflow);
1280
26.8k
    Z_TYPE_INFO_P(result) = overflow ? IS_DOUBLE : IS_LONG;
1281
26.8k
    return SUCCESS;
1282
44.2k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1283
520
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) * Z_DVAL_P(op2));
1284
520
    return SUCCESS;
1285
43.7k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1286
367
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) * Z_DVAL_P(op2));
1287
367
    return SUCCESS;
1288
43.3k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1289
2.01k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) * ((double)Z_LVAL_P(op2)));
1290
2.01k
    return SUCCESS;
1291
41.3k
  } else {
1292
41.3k
    return FAILURE;
1293
41.3k
  }
1294
71.0k
}
1295
/* }}} */
1296
1297
static zend_never_inline zend_result ZEND_FASTCALL mul_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1298
20.8k
{
1299
20.8k
  ZVAL_DEREF(op1);
1300
20.8k
  ZVAL_DEREF(op2);
1301
20.8k
  if (mul_function_fast(result, op1, op2) == SUCCESS) {
1302
313
    return SUCCESS;
1303
313
  }
1304
1305
20.8k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_MUL);
1306
1307
20.5k
  zval op1_copy, op2_copy;
1308
20.5k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1309
20.3k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1310
129
    zend_binop_error("*", op1, op2);
1311
129
    if (result != op1) {
1312
90
      ZVAL_UNDEF(result);
1313
90
    }
1314
129
    return FAILURE;
1315
129
  }
1316
1317
20.3k
  if (result == op1) {
1318
2.83k
    zval_ptr_dtor(result);
1319
2.83k
  }
1320
1321
20.3k
  if (mul_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1322
20.3k
    return SUCCESS;
1323
20.3k
  }
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
29.9k
{
1332
29.9k
  if (mul_function_fast(result, op1, op2) == SUCCESS) {
1333
9.08k
    return SUCCESS;
1334
20.8k
  } else {
1335
20.8k
    return mul_function_slow(result, op1, op2);
1336
20.8k
  }
1337
29.9k
}
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
32
{
1347
32
  if (UNEXPECTED(base == 0.0 && exponent < 0.0)) {
1348
0
    zend_power_base_0_exponent_lt_0_error();
1349
0
  }
1350
1351
32
  return pow(base, exponent);
1352
32
}
1353
1354
static zend_result ZEND_FASTCALL pow_function_base(zval *result, zval *op1, zval *op2) /* {{{ */
1355
151
{
1356
151
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1357
1358
151
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1359
76
    if (Z_LVAL_P(op2) >= 0) {
1360
56
      zend_long l1 = 1, l2 = Z_LVAL_P(op1), i = Z_LVAL_P(op2);
1361
1362
56
      if (i == 0) {
1363
12
        ZVAL_LONG(result, 1L);
1364
12
        return SUCCESS;
1365
44
      } else if (l2 == 0) {
1366
6
        ZVAL_LONG(result, 0);
1367
6
        return SUCCESS;
1368
6
      }
1369
1370
222
      while (i >= 1) {
1371
196
        zend_long overflow;
1372
196
        double dval = 0.0;
1373
1374
196
        if (i % 2) {
1375
74
          --i;
1376
74
          ZEND_SIGNED_MULTIPLY_LONG(l1, l2, l1, dval, overflow);
1377
74
          if (overflow) {
1378
3
            ZVAL_DOUBLE(result, dval * safe_pow(l2, i));
1379
3
            return SUCCESS;
1380
3
          }
1381
122
        } else {
1382
122
          i /= 2;
1383
122
          ZEND_SIGNED_MULTIPLY_LONG(l2, l2, l2, dval, overflow);
1384
122
          if (overflow) {
1385
9
            ZVAL_DOUBLE(result, (double)l1 * safe_pow(dval, i));
1386
9
            return SUCCESS;
1387
9
          }
1388
122
        }
1389
196
      }
1390
      /* i == 0 */
1391
26
      ZVAL_LONG(result, l1);
1392
26
    } else {
1393
20
      ZVAL_DOUBLE(result, safe_pow((double)Z_LVAL_P(op1), (double)Z_LVAL_P(op2)));
1394
20
    }
1395
46
    return SUCCESS;
1396
76
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1397
0
    ZVAL_DOUBLE(result, safe_pow(Z_DVAL_P(op1), Z_DVAL_P(op2)));
1398
0
    return SUCCESS;
1399
75
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1400
0
    ZVAL_DOUBLE(result, safe_pow((double)Z_LVAL_P(op1), Z_DVAL_P(op2)));
1401
0
    return SUCCESS;
1402
75
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1403
0
    ZVAL_DOUBLE(result, safe_pow(Z_DVAL_P(op1), (double)Z_LVAL_P(op2)));
1404
0
    return SUCCESS;
1405
75
  } else {
1406
75
    return FAILURE;
1407
75
  }
1408
151
}
1409
/* }}} */
1410
1411
ZEND_API zend_result ZEND_FASTCALL pow_function(zval *result, zval *op1, zval *op2) /* {{{ */
1412
103
{
1413
103
  ZVAL_DEREF(op1);
1414
103
  ZVAL_DEREF(op2);
1415
103
  if (pow_function_base(result, op1, op2) == SUCCESS) {
1416
28
    return SUCCESS;
1417
28
  }
1418
1419
103
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_POW);
1420
1421
75
  zval op1_copy, op2_copy;
1422
75
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1423
48
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1424
27
    zend_binop_error("**", op1, op2);
1425
27
    if (result != op1) {
1426
6
      ZVAL_UNDEF(result);
1427
6
    }
1428
27
    return FAILURE;
1429
27
  }
1430
1431
48
  if (result == op1) {
1432
21
    zval_ptr_dtor(result);
1433
21
  }
1434
1435
48
  if (pow_function_base(result, &op1_copy, &op2_copy) == SUCCESS) {
1436
48
    return SUCCESS;
1437
48
  }
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
5.23k
{
1452
5.23k
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1453
1454
5.23k
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1455
1.93k
    if (Z_LVAL_P(op2) == 0) {
1456
66
      return DIV_BY_ZERO;
1457
1.86k
    } else if (Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == ZEND_LONG_MIN) {
1458
      /* Prevent overflow error/crash */
1459
0
      ZVAL_DOUBLE(result, (double) ZEND_LONG_MIN / -1);
1460
0
      return DIV_SUCCESS;
1461
0
    }
1462
1.86k
    if (Z_LVAL_P(op1) % Z_LVAL_P(op2) == 0) { /* integer */
1463
793
      ZVAL_LONG(result, Z_LVAL_P(op1) / Z_LVAL_P(op2));
1464
1.07k
    } else {
1465
1.07k
      ZVAL_DOUBLE(result, ((double) Z_LVAL_P(op1)) / Z_LVAL_P(op2));
1466
1.07k
    }
1467
1.86k
    return DIV_SUCCESS;
1468
3.30k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1469
214
    if (Z_DVAL_P(op2) == 0) {
1470
3
      return DIV_BY_ZERO;
1471
3
    }
1472
211
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) / Z_DVAL_P(op2));
1473
211
    return DIV_SUCCESS;
1474
3.08k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1475
1.62k
    if (Z_LVAL_P(op2) == 0) {
1476
6
      return DIV_BY_ZERO;
1477
6
    }
1478
1.61k
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) / (double)Z_LVAL_P(op2));
1479
1.61k
    return DIV_SUCCESS;
1480
1.62k
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1481
121
    if (Z_DVAL_P(op2) == 0) {
1482
6
      return DIV_BY_ZERO;
1483
6
    }
1484
115
    ZVAL_DOUBLE(result, (double)Z_LVAL_P(op1) / Z_DVAL_P(op2));
1485
115
    return DIV_SUCCESS;
1486
1.34k
  } else {
1487
1.34k
    return DIV_TYPES_NOT_HANDLED;
1488
1.34k
  }
1489
5.23k
}
1490
/* }}} */
1491
1492
ZEND_API zend_result ZEND_FASTCALL div_function(zval *result, zval *op1, zval *op2) /* {{{ */
1493
3.94k
{
1494
3.94k
  ZVAL_DEREF(op1);
1495
3.94k
  ZVAL_DEREF(op2);
1496
1497
3.94k
  zend_div_status retval = div_function_base(result, op1, op2);
1498
3.94k
  if (EXPECTED(retval == DIV_SUCCESS)) {
1499
2.56k
    return SUCCESS;
1500
2.56k
  }
1501
1502
1.38k
  if (UNEXPECTED(retval == DIV_BY_ZERO)) {
1503
36
    goto div_by_zero;
1504
36
  }
1505
1506
1.38k
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_DIV);
1507
1508
1.34k
  zval result_copy, op1_copy, op2_copy;
1509
1.34k
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1510
1.31k
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1511
54
    zend_binop_error("/", op1, op2);
1512
54
    if (result != op1) {
1513
27
      ZVAL_UNDEF(result);
1514
27
    }
1515
54
    return FAILURE;
1516
54
  }
1517
1518
1.29k
  retval = div_function_base(&result_copy, &op1_copy, &op2_copy);
1519
1.29k
  if (retval == DIV_SUCCESS) {
1520
1.24k
    if (result == op1) {
1521
145
      zval_ptr_dtor(result);
1522
145
    }
1523
1.24k
    ZVAL_COPY_VALUE(result, &result_copy);
1524
1.24k
    return SUCCESS;
1525
1.24k
  }
1526
1527
81
div_by_zero:
1528
81
  ZEND_ASSERT(retval == DIV_BY_ZERO && "DIV_TYPES_NOT_HANDLED should not occur here");
1529
81
  if (result != op1) {
1530
75
    ZVAL_UNDEF(result);
1531
75
  }
1532
81
  zend_throw_error(zend_ce_division_by_zero_error, "Division by zero");
1533
81
  return FAILURE;
1534
81
}
1535
/* }}} */
1536
1537
ZEND_API zend_result ZEND_FASTCALL mod_function(zval *result, zval *op1, zval *op2) /* {{{ */
1538
6.91k
{
1539
6.91k
  zend_long op1_lval, op2_lval;
1540
1541
6.91k
  convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_MOD, "%");
1542
1543
6.84k
  if (op2_lval == 0) {
1544
    /* modulus by zero */
1545
78
    if (EG(current_execute_data) && !CG(in_compilation)) {
1546
78
      zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
1547
78
    } else {
1548
0
      zend_error_noreturn(E_ERROR, "Modulo by zero");
1549
0
    }
1550
78
    if (op1 != result) {
1551
48
      ZVAL_UNDEF(result);
1552
48
    }
1553
78
    return FAILURE;
1554
78
  }
1555
1556
6.77k
  if (op1 == result) {
1557
66
    zval_ptr_dtor(result);
1558
66
  }
1559
1560
6.77k
  if (op2_lval == -1) {
1561
    /* Prevent overflow error/crash if op1==LONG_MIN */
1562
9
    ZVAL_LONG(result, 0);
1563
9
    return SUCCESS;
1564
9
  }
1565
1566
6.76k
  ZVAL_LONG(result, op1_lval % op2_lval);
1567
6.76k
  return SUCCESS;
1568
6.77k
}
1569
/* }}} */
1570
1571
ZEND_API zend_result ZEND_FASTCALL boolean_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */
1572
5.31k
{
1573
5.31k
  int op1_val, op2_val;
1574
1575
5.31k
  do {
1576
5.31k
    if (Z_TYPE_P(op1) == IS_FALSE) {
1577
110
      op1_val = 0;
1578
5.20k
    } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
1579
174
      op1_val = 1;
1580
5.03k
    } else {
1581
5.03k
      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
5.03k
      ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BOOL_XOR);
1592
5.03k
      op1_val = zend_is_true(op1);
1593
5.03k
    }
1594
5.31k
  } while (0);
1595
5.31k
  do {
1596
5.31k
    if (Z_TYPE_P(op2) == IS_FALSE) {
1597
142
      op2_val = 0;
1598
5.17k
    } else if (EXPECTED(Z_TYPE_P(op2) == IS_TRUE)) {
1599
49
      op2_val = 1;
1600
5.12k
    } else {
1601
5.12k
      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
5.12k
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BOOL_XOR);
1612
5.12k
      op2_val = zend_is_true(op2);
1613
5.12k
    }
1614
5.31k
  } while (0);
1615
1616
5.31k
  ZVAL_BOOL(result, op1_val ^ op2_val);
1617
5.31k
  return SUCCESS;
1618
5.31k
}
1619
/* }}} */
1620
1621
ZEND_API zend_result ZEND_FASTCALL boolean_not_function(zval *result, zval *op1) /* {{{ */
1622
330
{
1623
330
  if (Z_TYPE_P(op1) < IS_TRUE) {
1624
30
    ZVAL_TRUE(result);
1625
300
  } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
1626
12
    ZVAL_FALSE(result);
1627
288
  } else {
1628
288
    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
288
    ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BOOL_NOT);
1639
1640
288
    ZVAL_BOOL(result, !zend_is_true(op1));
1641
288
  }
1642
330
  return SUCCESS;
1643
330
}
1644
/* }}} */
1645
1646
ZEND_API zend_result ZEND_FASTCALL bitwise_not_function(zval *result, zval *op1) /* {{{ */
1647
3.24k
{
1648
3.26k
try_again:
1649
3.26k
  switch (Z_TYPE_P(op1)) {
1650
418
    case IS_LONG:
1651
418
      ZVAL_LONG(result, ~Z_LVAL_P(op1));
1652
418
      return SUCCESS;
1653
685
    case IS_DOUBLE: {
1654
685
      zend_long lval = zend_dval_to_lval_safe(Z_DVAL_P(op1));
1655
685
      if (EG(exception)) {
1656
0
        if (result != op1) {
1657
0
          ZVAL_UNDEF(result);
1658
0
        }
1659
0
        return FAILURE;
1660
0
      }
1661
685
      ZVAL_LONG(result, ~lval);
1662
685
      return SUCCESS;
1663
685
    }
1664
2.07k
    case IS_STRING: {
1665
2.07k
      size_t i;
1666
1667
2.07k
      if (Z_STRLEN_P(op1) == 1) {
1668
42
        zend_uchar not = (zend_uchar) ~*Z_STRVAL_P(op1);
1669
42
        ZVAL_CHAR(result, not);
1670
2.03k
      } else {
1671
2.03k
        ZVAL_NEW_STR(result, zend_string_alloc(Z_STRLEN_P(op1), 0));
1672
11.5M
        for (i = 0; i < Z_STRLEN_P(op1); i++) {
1673
11.5M
          Z_STRVAL_P(result)[i] = ~Z_STRVAL_P(op1)[i];
1674
11.5M
        }
1675
2.03k
        Z_STRVAL_P(result)[i] = 0;
1676
2.03k
      }
1677
2.07k
      return SUCCESS;
1678
685
    }
1679
18
    case IS_REFERENCE:
1680
18
      op1 = Z_REFVAL_P(op1);
1681
18
      goto try_again;
1682
69
    default:
1683
69
      ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BW_NOT);
1684
1685
69
      if (result != op1) {
1686
69
        ZVAL_UNDEF(result);
1687
69
      }
1688
69
      zend_type_error("Cannot perform bitwise not on %s", zend_zval_value_name(op1));
1689
69
      return FAILURE;
1690
3.26k
  }
1691
3.26k
}
1692
/* }}} */
1693
1694
ZEND_API zend_result ZEND_FASTCALL bitwise_or_function(zval *result, zval *op1, zval *op2) /* {{{ */
1695
6.61k
{
1696
6.61k
  zend_long op1_lval, op2_lval;
1697
1698
6.61k
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1699
383
    ZVAL_LONG(result, Z_LVAL_P(op1) | Z_LVAL_P(op2));
1700
383
    return SUCCESS;
1701
383
  }
1702
1703
6.23k
  ZVAL_DEREF(op1);
1704
6.23k
  ZVAL_DEREF(op2);
1705
1706
6.23k
  if (Z_TYPE_P(op1) == IS_STRING && EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
1707
5.58k
    zval *longer, *shorter;
1708
5.58k
    zend_string *str;
1709
5.58k
    size_t i;
1710
1711
5.58k
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1712
3.71k
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1713
3
        zend_uchar or = (zend_uchar) (*Z_STRVAL_P(op1) | *Z_STRVAL_P(op2));
1714
3
        if (result==op1) {
1715
3
          zval_ptr_dtor_str(result);
1716
3
        }
1717
3
        ZVAL_CHAR(result, or);
1718
3
        return SUCCESS;
1719
3
      }
1720
3.71k
      longer = op1;
1721
3.71k
      shorter = op2;
1722
3.71k
    } else {
1723
1.86k
      longer = op2;
1724
1.86k
      shorter = op1;
1725
1.86k
    }
1726
1727
5.57k
    str = zend_string_alloc(Z_STRLEN_P(longer), 0);
1728
2.03M
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1729
2.02M
      ZSTR_VAL(str)[i] = Z_STRVAL_P(longer)[i] | Z_STRVAL_P(shorter)[i];
1730
2.02M
    }
1731
5.57k
    memcpy(ZSTR_VAL(str) + i, Z_STRVAL_P(longer) + i, Z_STRLEN_P(longer) - i + 1);
1732
5.57k
    if (result==op1) {
1733
6
      zval_ptr_dtor_str(result);
1734
6
    }
1735
5.57k
    ZVAL_NEW_STR(result, str);
1736
5.57k
    return SUCCESS;
1737
5.58k
  }
1738
1739
654
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1740
453
    bool failed;
1741
453
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_OR);
1742
453
    op1_lval = zendi_try_get_long(op1, &failed);
1743
453
    if (UNEXPECTED(failed)) {
1744
173
      zend_binop_error("|", op1, op2);
1745
173
      if (result != op1) {
1746
161
        ZVAL_UNDEF(result);
1747
161
      }
1748
173
      return FAILURE;
1749
173
    }
1750
453
  } else {
1751
201
    op1_lval = Z_LVAL_P(op1);
1752
201
  }
1753
481
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1754
315
    bool failed;
1755
315
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_OR);
1756
315
    op2_lval = zendi_try_get_long(op2, &failed);
1757
315
    if (UNEXPECTED(failed)) {
1758
18
      zend_binop_error("|", op1, op2);
1759
18
      if (result != op1) {
1760
12
        ZVAL_UNDEF(result);
1761
12
      }
1762
18
      return FAILURE;
1763
18
    }
1764
315
  } else {
1765
166
    op2_lval = Z_LVAL_P(op2);
1766
166
  }
1767
1768
463
  if (op1 == result) {
1769
38
    zval_ptr_dtor(result);
1770
38
  }
1771
463
  ZVAL_LONG(result, op1_lval | op2_lval);
1772
463
  return SUCCESS;
1773
481
}
1774
/* }}} */
1775
1776
ZEND_API zend_result ZEND_FASTCALL bitwise_and_function(zval *result, zval *op1, zval *op2) /* {{{ */
1777
144k
{
1778
144k
  zend_long op1_lval, op2_lval;
1779
1780
144k
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1781
3.55k
    ZVAL_LONG(result, Z_LVAL_P(op1) & Z_LVAL_P(op2));
1782
3.55k
    return SUCCESS;
1783
3.55k
  }
1784
1785
140k
  ZVAL_DEREF(op1);
1786
140k
  ZVAL_DEREF(op2);
1787
1788
140k
  if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
1789
131k
    zval *longer, *shorter;
1790
131k
    zend_string *str;
1791
131k
    size_t i;
1792
1793
131k
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1794
30.8k
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1795
15
        zend_uchar and = (zend_uchar) (*Z_STRVAL_P(op1) & *Z_STRVAL_P(op2));
1796
15
        if (result==op1) {
1797
9
          zval_ptr_dtor_str(result);
1798
9
        }
1799
15
        ZVAL_CHAR(result, and);
1800
15
        return SUCCESS;
1801
15
      }
1802
30.8k
      longer = op1;
1803
30.8k
      shorter = op2;
1804
100k
    } else {
1805
100k
      longer = op2;
1806
100k
      shorter = op1;
1807
100k
    }
1808
1809
131k
    str = zend_string_alloc(Z_STRLEN_P(shorter), 0);
1810
1.53G
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1811
1.53G
      ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] & Z_STRVAL_P(longer)[i];
1812
1.53G
    }
1813
131k
    ZSTR_VAL(str)[i] = 0;
1814
131k
    if (result==op1) {
1815
93.7k
      zval_ptr_dtor_str(result);
1816
93.7k
    }
1817
131k
    ZVAL_NEW_STR(result, str);
1818
131k
    return SUCCESS;
1819
131k
  }
1820
1821
9.40k
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1822
4.01k
    bool failed;
1823
4.01k
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_AND);
1824
4.01k
    op1_lval = zendi_try_get_long(op1, &failed);
1825
4.01k
    if (UNEXPECTED(failed)) {
1826
39
      zend_binop_error("&", op1, op2);
1827
39
      if (result != op1) {
1828
24
        ZVAL_UNDEF(result);
1829
24
      }
1830
39
      return FAILURE;
1831
39
    }
1832
5.39k
  } else {
1833
5.39k
    op1_lval = Z_LVAL_P(op1);
1834
5.39k
  }
1835
9.37k
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1836
7.99k
    bool failed;
1837
7.99k
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_AND);
1838
7.99k
    op2_lval = zendi_try_get_long(op2, &failed);
1839
7.99k
    if (UNEXPECTED(failed)) {
1840
30
      zend_binop_error("&", op1, op2);
1841
30
      if (result != op1) {
1842
18
        ZVAL_UNDEF(result);
1843
18
      }
1844
30
      return FAILURE;
1845
30
    }
1846
7.99k
  } else {
1847
1.37k
    op2_lval = Z_LVAL_P(op2);
1848
1.37k
  }
1849
1850
9.34k
  if (op1 == result) {
1851
7.53k
    zval_ptr_dtor(result);
1852
7.53k
  }
1853
9.34k
  ZVAL_LONG(result, op1_lval & op2_lval);
1854
9.34k
  return SUCCESS;
1855
9.37k
}
1856
/* }}} */
1857
1858
ZEND_API zend_result ZEND_FASTCALL bitwise_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */
1859
31.4k
{
1860
31.4k
  zend_long op1_lval, op2_lval;
1861
1862
31.4k
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1863
257
    ZVAL_LONG(result, Z_LVAL_P(op1) ^ Z_LVAL_P(op2));
1864
257
    return SUCCESS;
1865
257
  }
1866
1867
31.2k
  ZVAL_DEREF(op1);
1868
31.2k
  ZVAL_DEREF(op2);
1869
1870
31.2k
  if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
1871
25.3k
    zval *longer, *shorter;
1872
25.3k
    zend_string *str;
1873
25.3k
    size_t i;
1874
1875
25.3k
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1876
18.4k
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1877
6
        zend_uchar xor = (zend_uchar) (*Z_STRVAL_P(op1) ^ *Z_STRVAL_P(op2));
1878
6
        if (result==op1) {
1879
6
          zval_ptr_dtor_str(result);
1880
6
        }
1881
6
        ZVAL_CHAR(result, xor);
1882
6
        return SUCCESS;
1883
6
      }
1884
18.4k
      longer = op1;
1885
18.4k
      shorter = op2;
1886
18.4k
    } else {
1887
6.96k
      longer = op2;
1888
6.96k
      shorter = op1;
1889
6.96k
    }
1890
1891
25.3k
    str = zend_string_alloc(Z_STRLEN_P(shorter), 0);
1892
107M
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1893
107M
      ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] ^ Z_STRVAL_P(longer)[i];
1894
107M
    }
1895
25.3k
    ZSTR_VAL(str)[i] = 0;
1896
25.3k
    if (result==op1) {
1897
6
      zval_ptr_dtor_str(result);
1898
6
    }
1899
25.3k
    ZVAL_NEW_STR(result, str);
1900
25.3k
    return SUCCESS;
1901
25.3k
  }
1902
1903
5.82k
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1904
583
    bool failed;
1905
583
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_XOR);
1906
583
    op1_lval = zendi_try_get_long(op1, &failed);
1907
583
    if (UNEXPECTED(failed)) {
1908
27
      zend_binop_error("^", op1, op2);
1909
27
      if (result != op1) {
1910
15
        ZVAL_UNDEF(result);
1911
15
      }
1912
27
      return FAILURE;
1913
27
    }
1914
5.24k
  } else {
1915
5.24k
    op1_lval = Z_LVAL_P(op1);
1916
5.24k
  }
1917
5.79k
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1918
5.33k
    bool failed;
1919
5.33k
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_XOR);
1920
5.33k
    op2_lval = zendi_try_get_long(op2, &failed);
1921
5.33k
    if (UNEXPECTED(failed)) {
1922
15
      zend_binop_error("^", op1, op2);
1923
15
      if (result != op1) {
1924
12
        ZVAL_UNDEF(result);
1925
12
      }
1926
15
      return FAILURE;
1927
15
    }
1928
5.33k
  } else {
1929
467
    op2_lval = Z_LVAL_P(op2);
1930
467
  }
1931
1932
5.78k
  if (op1 == result) {
1933
24
    zval_ptr_dtor(result);
1934
24
  }
1935
5.78k
  ZVAL_LONG(result, op1_lval ^ op2_lval);
1936
5.78k
  return SUCCESS;
1937
5.79k
}
1938
/* }}} */
1939
1940
ZEND_API zend_result ZEND_FASTCALL shift_left_function(zval *result, zval *op1, zval *op2) /* {{{ */
1941
2.38k
{
1942
2.38k
  zend_long op1_lval, op2_lval;
1943
1944
2.38k
  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
1.89k
  if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
1948
114
    if (EXPECTED(op2_lval > 0)) {
1949
90
      if (op1 == result) {
1950
3
        zval_ptr_dtor(result);
1951
3
      }
1952
90
      ZVAL_LONG(result, 0);
1953
90
      return SUCCESS;
1954
90
    } else {
1955
24
      if (EG(current_execute_data) && !CG(in_compilation)) {
1956
24
        zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number");
1957
24
      } else {
1958
0
        zend_error_noreturn(E_ERROR, "Bit shift by negative number");
1959
0
      }
1960
24
      if (op1 != result) {
1961
15
        ZVAL_UNDEF(result);
1962
15
      }
1963
24
      return FAILURE;
1964
24
    }
1965
114
  }
1966
1967
1.78k
  if (op1 == result) {
1968
42
    zval_ptr_dtor(result);
1969
42
  }
1970
1971
  /* Perform shift on unsigned numbers to get well-defined wrap behavior. */
1972
1.78k
  ZVAL_LONG(result, (zend_long) ((zend_ulong) op1_lval << op2_lval));
1973
1.78k
  return SUCCESS;
1974
1.89k
}
1975
/* }}} */
1976
1977
ZEND_API zend_result ZEND_FASTCALL shift_right_function(zval *result, zval *op1, zval *op2) /* {{{ */
1978
2.10k
{
1979
2.10k
  zend_long op1_lval, op2_lval;
1980
1981
2.10k
  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
2.06k
  if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
1985
79
    if (EXPECTED(op2_lval > 0)) {
1986
19
      if (op1 == result) {
1987
3
        zval_ptr_dtor(result);
1988
3
      }
1989
19
      ZVAL_LONG(result, (op1_lval < 0) ? -1 : 0);
1990
19
      return SUCCESS;
1991
60
    } else {
1992
60
      if (EG(current_execute_data) && !CG(in_compilation)) {
1993
60
        zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number");
1994
60
      } else {
1995
0
        zend_error_noreturn(E_ERROR, "Bit shift by negative number");
1996
0
      }
1997
60
      if (op1 != result) {
1998
48
        ZVAL_UNDEF(result);
1999
48
      }
2000
60
      return FAILURE;
2001
60
    }
2002
79
  }
2003
2004
1.98k
  if (op1 == result) {
2005
1.53k
    zval_ptr_dtor(result);
2006
1.53k
  }
2007
2008
1.98k
  ZVAL_LONG(result, op1_lval >> op2_lval);
2009
1.98k
  return SUCCESS;
2010
2.06k
}
2011
/* }}} */
2012
2013
ZEND_API zend_result ZEND_FASTCALL concat_function(zval *result, zval *op1, zval *op2) /* {{{ */
2014
595k
{
2015
595k
  zval *orig_op1 = op1;
2016
595k
  zend_string *op1_string, *op2_string;
2017
595k
  bool free_op1_string = false;
2018
595k
  bool free_op2_string = false;
2019
2020
595k
  do {
2021
595k
    if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) {
2022
538k
      op1_string = Z_STR_P(op1);
2023
538k
    } else {
2024
57.7k
      if (Z_ISREF_P(op1)) {
2025
508
        op1 = Z_REFVAL_P(op1);
2026
508
        if (Z_TYPE_P(op1) == IS_STRING) {
2027
143
          op1_string = Z_STR_P(op1);
2028
143
          break;
2029
143
        }
2030
508
      }
2031
57.7k
      ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_CONCAT);
2032
57.5k
      op1_string = zval_try_get_string_func(op1);
2033
57.5k
      if (UNEXPECTED(!op1_string)) {
2034
36
        if (orig_op1 != result) {
2035
18
          ZVAL_UNDEF(result);
2036
18
        }
2037
36
        return FAILURE;
2038
36
      }
2039
57.5k
      free_op1_string = true;
2040
57.5k
      if (result == op1) {
2041
45.7k
        if (UNEXPECTED(op1 == op2)) {
2042
6
          op2_string = op1_string;
2043
6
          goto has_op2_string;
2044
6
        }
2045
45.7k
      }
2046
57.5k
    }
2047
595k
  } while (0);
2048
595k
  do {
2049
595k
    if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2050
564k
      op2_string = Z_STR_P(op2);
2051
564k
    } else {
2052
31.4k
      if (Z_ISREF_P(op2)) {
2053
21
        op2 = Z_REFVAL_P(op2);
2054
21
        if (Z_TYPE_P(op2) == IS_STRING) {
2055
18
          op2_string = Z_STR_P(op2);
2056
18
          break;
2057
18
        }
2058
21
      }
2059
      /* hold an additional reference because a userland function could free this */
2060
31.3k
      if (!free_op1_string) {
2061
20.7k
        op1_string = zend_string_copy(op1_string);
2062
20.7k
        free_op1_string = true;
2063
20.7k
      }
2064
31.3k
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_CONCAT);
2065
31.3k
      op2_string = zval_try_get_string_func(op2);
2066
31.3k
      if (UNEXPECTED(!op2_string)) {
2067
36
        zend_string_release_ex(op1_string, false);
2068
36
        if (orig_op1 != result) {
2069
15
          ZVAL_UNDEF(result);
2070
15
        }
2071
36
        return FAILURE;
2072
36
      }
2073
31.3k
      free_op2_string = true;
2074
31.3k
    }
2075
595k
  } while (0);
2076
2077
595k
has_op2_string:;
2078
595k
  if (UNEXPECTED(ZSTR_LEN(op1_string) == 0)) {
2079
55.4k
    if (EXPECTED(result != op2 || Z_TYPE_P(result) != IS_STRING)) {
2080
55.4k
      if (result == orig_op1) {
2081
50.5k
        i_zval_ptr_dtor(result);
2082
50.5k
      }
2083
55.4k
      if (free_op2_string) {
2084
        /* transfer ownership of op2_string */
2085
11.3k
        ZVAL_STR(result, op2_string);
2086
11.3k
        free_op2_string = false;
2087
44.1k
      } else {
2088
44.1k
        ZVAL_STR_COPY(result, op2_string);
2089
44.1k
      }
2090
55.4k
    }
2091
540k
  } else if (UNEXPECTED(ZSTR_LEN(op2_string) == 0)) {
2092
12.0k
    if (EXPECTED(result != op1 || Z_TYPE_P(result) != IS_STRING)) {
2093
4.67k
      if (result == orig_op1) {
2094
495
        i_zval_ptr_dtor(result);
2095
495
      }
2096
4.67k
      if (free_op1_string) {
2097
        /* transfer ownership of op1_string */
2098
4.67k
        ZVAL_STR(result, op1_string);
2099
4.67k
        free_op1_string = false;
2100
4.67k
      } else {
2101
6
        ZVAL_STR_COPY(result, op1_string);
2102
6
      }
2103
4.67k
    }
2104
528k
  } else {
2105
528k
    size_t op1_len = ZSTR_LEN(op1_string);
2106
528k
    size_t op2_len = ZSTR_LEN(op2_string);
2107
528k
    size_t result_len = op1_len + op2_len;
2108
528k
    zend_string *result_str;
2109
528k
    uint32_t flags = ZSTR_GET_COPYABLE_CONCAT_PROPERTIES_BOTH(op1_string, op2_string);
2110
2111
528k
    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
528k
    if (result == op1) {
2122
      /* Destroy the old result first to drop the refcount, such that $x .= ...; may happen in-place. */
2123
519k
      if (free_op1_string) {
2124
        /* op1_string will be used as the result, so we should not free it */
2125
9.84k
        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
9.84k
        ZVAL_NULL(result);
2129
9.84k
        free_op1_string = false;
2130
9.84k
      }
2131
      /* special case, perform operations on result */
2132
519k
      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
519k
      if (op1_string == op2_string) {
2135
17.2k
        if (free_op2_string) {
2136
452
          zend_string_release_ex(op2_string, false);
2137
452
          free_op2_string = false;
2138
452
        }
2139
17.2k
        op2_string = result_str;
2140
17.2k
      }
2141
519k
    } else {
2142
9.06k
      result_str = zend_string_alloc(result_len, 0);
2143
9.06k
      memcpy(ZSTR_VAL(result_str), ZSTR_VAL(op1_string), op1_len);
2144
9.06k
      if (result == orig_op1) {
2145
0
        i_zval_ptr_dtor(result);
2146
0
      }
2147
9.06k
    }
2148
528k
    GC_ADD_FLAGS(result_str, flags);
2149
2150
528k
    ZVAL_NEW_STR(result, result_str);
2151
528k
    memcpy(ZSTR_VAL(result_str) + op1_len, ZSTR_VAL(op2_string), op2_len);
2152
528k
    ZSTR_VAL(result_str)[result_len] = '\0';
2153
528k
  }
2154
2155
595k
  if (free_op1_string) zend_string_release_ex(op1_string, false);
2156
595k
  if (free_op2_string) zend_string_release_ex(op2_string, false);
2157
2158
595k
  return SUCCESS;
2159
595k
}
2160
/* }}} */
2161
2162
ZEND_API int ZEND_FASTCALL string_compare_function_ex(const zval *op1, const 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(const zval *op1, const zval *op2) /* {{{ */
2182
38.0k
{
2183
38.0k
  if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) &&
2184
37.3k
      EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2185
37.2k
    if (Z_STR_P(op1) == Z_STR_P(op2)) {
2186
4.15k
      return 0;
2187
33.0k
    } else {
2188
33.0k
      return zend_binary_strcmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
2189
33.0k
    }
2190
37.2k
  } else {
2191
762
    zend_string *tmp_str1, *tmp_str2;
2192
762
    zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2193
762
    zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2194
762
    int ret = zend_binary_strcmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2));
2195
2196
762
    zend_tmp_string_release(tmp_str1);
2197
762
    zend_tmp_string_release(tmp_str2);
2198
762
    return ret;
2199
762
  }
2200
38.0k
}
2201
/* }}} */
2202
2203
ZEND_API int ZEND_FASTCALL string_case_compare_function(const zval *op1, const zval *op2) /* {{{ */
2204
0
{
2205
0
  if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) &&
2206
0
      EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2207
0
    if (Z_STR_P(op1) == Z_STR_P(op2)) {
2208
0
      return 0;
2209
0
    } else {
2210
0
      return zend_binary_strcasecmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
2211
0
    }
2212
0
  } else {
2213
0
    zend_string *tmp_str1, *tmp_str2;
2214
0
    zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2215
0
    zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2216
0
    int ret = zend_binary_strcasecmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2));
2217
2218
0
    zend_tmp_string_release(tmp_str1);
2219
0
    zend_tmp_string_release(tmp_str2);
2220
0
    return ret;
2221
0
  }
2222
0
}
2223
/* }}} */
2224
2225
ZEND_API int ZEND_FASTCALL string_locale_compare_function(const zval *op1, const zval *op2) /* {{{ */
2226
525
{
2227
525
  zend_string *tmp_str1, *tmp_str2;
2228
525
  zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2229
525
  zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2230
525
  int ret = strcoll(ZSTR_VAL(str1), ZSTR_VAL(str2));
2231
2232
525
  zend_tmp_string_release(tmp_str1);
2233
525
  zend_tmp_string_release(tmp_str2);
2234
525
  return ret;
2235
525
}
2236
/* }}} */
2237
2238
ZEND_API int ZEND_FASTCALL numeric_compare_function(const zval *op1, const zval *op2) /* {{{ */
2239
2.28k
{
2240
2.28k
  double d1, d2;
2241
2242
2.28k
  d1 = zval_get_double(op1);
2243
2.28k
  d2 = zval_get_double(op2);
2244
2245
2.28k
  return ZEND_THREEWAY_COMPARE(d1, d2);
2246
2.28k
}
2247
/* }}} */
2248
2249
ZEND_API zend_result ZEND_FASTCALL compare_function(zval *result, zval *op1, zval *op2) /* {{{ */
2250
116
{
2251
116
  ZVAL_LONG(result, zend_compare(op1, op2));
2252
116
  return SUCCESS;
2253
116
}
2254
/* }}} */
2255
2256
static int compare_long_to_string(zend_long lval, const zend_string *str) /* {{{ */
2257
4.00k
{
2258
4.00k
  zend_long str_lval;
2259
4.00k
  double str_dval;
2260
4.00k
  uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &str_lval, &str_dval, 0);
2261
2262
4.00k
  if (type == IS_LONG) {
2263
2.38k
    return lval > str_lval ? 1 : lval < str_lval ? -1 : 0;
2264
2.38k
  }
2265
2266
1.61k
  if (type == IS_DOUBLE) {
2267
148
    return ZEND_THREEWAY_COMPARE((double) lval, str_dval);
2268
148
  }
2269
2270
1.46k
  zend_string *lval_as_str = zend_long_to_str(lval);
2271
1.46k
  int cmp_result = zend_binary_strcmp(
2272
1.46k
    ZSTR_VAL(lval_as_str), ZSTR_LEN(lval_as_str), ZSTR_VAL(str), ZSTR_LEN(str));
2273
1.46k
  zend_string_release(lval_as_str);
2274
1.46k
  return ZEND_NORMALIZE_BOOL(cmp_result);
2275
1.61k
}
2276
/* }}} */
2277
2278
static int compare_double_to_string(double dval, const zend_string *str) /* {{{ */
2279
189
{
2280
189
  zend_long str_lval;
2281
189
  double str_dval;
2282
189
  uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &str_lval, &str_dval, 0);
2283
2284
189
  ZEND_ASSERT(!zend_isnan(dval));
2285
2286
189
  if (type == IS_LONG) {
2287
48
    return ZEND_THREEWAY_COMPARE(dval, (double) str_lval);
2288
48
  }
2289
2290
141
  if (type == IS_DOUBLE) {
2291
62
    return ZEND_THREEWAY_COMPARE(dval, str_dval);
2292
62
  }
2293
2294
79
  zend_string *dval_as_str = zend_double_to_str(dval);
2295
79
  int cmp_result = zend_binary_strcmp(
2296
79
    ZSTR_VAL(dval_as_str), ZSTR_LEN(dval_as_str), ZSTR_VAL(str), ZSTR_LEN(str));
2297
79
  zend_string_release(dval_as_str);
2298
79
  return ZEND_NORMALIZE_BOOL(cmp_result);
2299
141
}
2300
/* }}} */
2301
2302
ZEND_API int ZEND_FASTCALL zend_compare(zval *op1, zval *op2) /* {{{ */
2303
147k
{
2304
147k
  bool converted = false;
2305
147k
  zval op1_copy, op2_copy;
2306
2307
151k
  while (1) {
2308
151k
    switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
2309
2.22k
      case TYPE_PAIR(IS_LONG, IS_LONG):
2310
2.22k
        return Z_LVAL_P(op1)>Z_LVAL_P(op2)?1:(Z_LVAL_P(op1)<Z_LVAL_P(op2)?-1:0);
2311
2312
35
      case TYPE_PAIR(IS_DOUBLE, IS_LONG):
2313
35
        return ZEND_THREEWAY_COMPARE(Z_DVAL_P(op1), (double)Z_LVAL_P(op2));
2314
2315
206
      case TYPE_PAIR(IS_LONG, IS_DOUBLE):
2316
206
        return ZEND_THREEWAY_COMPARE((double)Z_LVAL_P(op1), Z_DVAL_P(op2));
2317
2318
43
      case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
2319
43
        return ZEND_THREEWAY_COMPARE(Z_DVAL_P(op1), Z_DVAL_P(op2));
2320
2321
2.56k
      case TYPE_PAIR(IS_ARRAY, IS_ARRAY):
2322
2.56k
        return zend_compare_arrays(op1, op2);
2323
2324
2.53k
      case TYPE_PAIR(IS_NULL, IS_NULL):
2325
3.13k
      case TYPE_PAIR(IS_NULL, IS_FALSE):
2326
8.73k
      case TYPE_PAIR(IS_FALSE, IS_NULL):
2327
9.17k
      case TYPE_PAIR(IS_FALSE, IS_FALSE):
2328
9.52k
      case TYPE_PAIR(IS_TRUE, IS_TRUE):
2329
9.52k
        return 0;
2330
2331
494
      case TYPE_PAIR(IS_NULL, IS_TRUE):
2332
494
        return -1;
2333
2334
244
      case TYPE_PAIR(IS_TRUE, IS_NULL):
2335
244
        return 1;
2336
2337
5.27k
      case TYPE_PAIR(IS_STRING, IS_STRING):
2338
5.27k
        if (Z_STR_P(op1) == Z_STR_P(op2)) {
2339
305
          return 0;
2340
305
        }
2341
4.97k
        return zendi_smart_strcmp(Z_STR_P(op1), Z_STR_P(op2));
2342
2343
6.37k
      case TYPE_PAIR(IS_NULL, IS_STRING):
2344
6.37k
        return Z_STRLEN_P(op2) == 0 ? 0 : -1;
2345
2346
6.46k
      case TYPE_PAIR(IS_STRING, IS_NULL):
2347
6.46k
        return Z_STRLEN_P(op1) == 0 ? 0 : 1;
2348
2349
1.46k
      case TYPE_PAIR(IS_LONG, IS_STRING):
2350
1.46k
        return compare_long_to_string(Z_LVAL_P(op1), Z_STR_P(op2));
2351
2352
2.53k
      case TYPE_PAIR(IS_STRING, IS_LONG):
2353
2.53k
        return -compare_long_to_string(Z_LVAL_P(op2), Z_STR_P(op1));
2354
2355
235
      case TYPE_PAIR(IS_DOUBLE, IS_STRING):
2356
235
        if (zend_isnan(Z_DVAL_P(op1))) {
2357
48
          return 1;
2358
48
        }
2359
2360
187
        return compare_double_to_string(Z_DVAL_P(op1), Z_STR_P(op2));
2361
2362
20
      case TYPE_PAIR(IS_STRING, IS_DOUBLE):
2363
20
        if (zend_isnan(Z_DVAL_P(op2))) {
2364
18
          return 1;
2365
18
        }
2366
2367
2
        return -compare_double_to_string(Z_DVAL_P(op2), Z_STR_P(op1));
2368
2369
90
      case TYPE_PAIR(IS_OBJECT, IS_NULL):
2370
90
        return 1;
2371
2372
238
      case TYPE_PAIR(IS_NULL, IS_OBJECT):
2373
238
        return -1;
2374
2375
113k
      default:
2376
113k
        if (Z_ISREF_P(op1)) {
2377
892
          op1 = Z_REFVAL_P(op1);
2378
892
          continue;
2379
112k
        } else if (Z_ISREF_P(op2)) {
2380
664
          op2 = Z_REFVAL_P(op2);
2381
664
          continue;
2382
664
        }
2383
2384
112k
        if (Z_TYPE_P(op1) == IS_OBJECT
2385
110k
         || Z_TYPE_P(op2) == IS_OBJECT) {
2386
1.19k
          zval *object, *other;
2387
1.19k
          if (Z_TYPE_P(op1) == IS_OBJECT) {
2388
1.04k
            object = op1;
2389
1.04k
            other = op2;
2390
1.04k
          } else {
2391
144
            object = op2;
2392
144
            other = op1;
2393
144
          }
2394
1.19k
          if (EXPECTED(Z_TYPE_P(other) == IS_OBJECT)) {
2395
602
            if (Z_OBJ_P(object) == Z_OBJ_P(other)) {
2396
90
              return 0;
2397
90
            }
2398
602
          } else if (Z_TYPE_P(other) == IS_TRUE || Z_TYPE_P(other) == IS_FALSE) {
2399
237
            zval casted;
2400
237
            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
237
            int ret = object == op1 ? zend_compare(&casted, other) : zend_compare(other, &casted);
2404
237
            ZEND_ASSERT(!Z_REFCOUNTED_P(&casted));
2405
237
            return ret;
2406
237
          }
2407
866
          return Z_OBJ_HANDLER_P(object, compare)(op1, op2);
2408
1.19k
        }
2409
2410
110k
        if (!converted) {
2411
          /* Handle NAN */
2412
108k
          if (UNEXPECTED(
2413
108k
            (Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))
2414
108k
            || (Z_TYPE_P(op2) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op2)))
2415
108k
          )) {
2416
            // TODO: NAN should always be uncomparable
2417
            /* NAN used be cast to TRUE so handle this manually for the time being */
2418
267
            if (Z_TYPE_P(op1) < IS_TRUE) {
2419
36
              return -1;
2420
231
            } else if (Z_TYPE_P(op1) == IS_TRUE || Z_TYPE_P(op2) == IS_TRUE) {
2421
63
              return 0;
2422
168
            } else if (Z_TYPE_P(op2) < IS_TRUE) {
2423
108
              return 1;
2424
108
            } else if (Z_TYPE_P(op1) != IS_DOUBLE) {
2425
18
              op1 = _zendi_convert_scalar_to_number_silent(op1, &op1_copy);
2426
18
              converted = true;
2427
42
            } else if (Z_TYPE_P(op2) != IS_DOUBLE) {
2428
42
              op2 = _zendi_convert_scalar_to_number_silent(op2, &op2_copy);
2429
42
              converted = true;
2430
42
            }
2431
108k
          } else if (Z_TYPE_P(op1) < IS_TRUE) {
2432
98.1k
            return zend_is_true(op2) ? -1 : 0;
2433
98.1k
          } else if (Z_TYPE_P(op1) == IS_TRUE) {
2434
731
            return zend_is_true(op2) ? 0 : 1;
2435
9.39k
          } else if (Z_TYPE_P(op2) < IS_TRUE) {
2436
6.56k
            return zend_is_true(op1) ? 1 : 0;
2437
6.56k
          } else if (Z_TYPE_P(op2) == IS_TRUE) {
2438
640
            return zend_is_true(op1) ? 0 : -1;
2439
2.19k
          } else {
2440
2.19k
            op1 = _zendi_convert_scalar_to_number_silent(op1, &op1_copy);
2441
2.19k
            op2 = _zendi_convert_scalar_to_number_silent(op2, &op2_copy);
2442
2.19k
            if (EG(exception)) {
2443
0
              return 1; /* to stop comparison of arrays */
2444
0
            }
2445
2.19k
            converted = true;
2446
2.19k
          }
2447
108k
        } else if (Z_TYPE_P(op1)==IS_ARRAY) {
2448
1.61k
          return 1;
2449
1.61k
        } else if (Z_TYPE_P(op2)==IS_ARRAY) {
2450
637
          return -1;
2451
637
        } else {
2452
0
          ZEND_UNREACHABLE();
2453
0
          zend_throw_error(NULL, "Unsupported operand types");
2454
0
          return 1;
2455
0
        }
2456
151k
    }
2457
151k
  }
2458
147k
}
2459
/* }}} */
2460
2461
/* return int to be compatible with compare_func_t */
2462
static int hash_zval_identical_function(const zval *z1, const zval *z2) /* {{{ */
2463
60
{
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
60
  ZVAL_DEREF(z1);
2470
60
  ZVAL_DEREF(z2);
2471
60
  return fast_is_not_identical_function(z1, z2);
2472
60
}
2473
/* }}} */
2474
2475
ZEND_API bool ZEND_FASTCALL zend_is_identical(const zval *op1, const zval *op2) /* {{{ */
2476
24.4k
{
2477
24.4k
  if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
2478
422
    return 0;
2479
422
  }
2480
24.0k
  switch (Z_TYPE_P(op1)) {
2481
90
    case IS_NULL:
2482
92
    case IS_FALSE:
2483
122
    case IS_TRUE:
2484
122
      return 1;
2485
6.06k
    case IS_LONG:
2486
6.06k
      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
443
    case IS_DOUBLE:
2490
443
      return (Z_DVAL_P(op1) == Z_DVAL_P(op2));
2491
16.9k
    case IS_STRING:
2492
16.9k
      return zend_string_equals(Z_STR_P(op1), Z_STR_P(op2));
2493
107
    case IS_ARRAY:
2494
107
      return (Z_ARRVAL_P(op1) == Z_ARRVAL_P(op2) ||
2495
81
        zend_hash_compare(Z_ARRVAL_P(op1), Z_ARRVAL_P(op2), (compare_func_t) hash_zval_identical_function, 1) == 0);
2496
299
    case IS_OBJECT:
2497
299
      return (Z_OBJ_P(op1) == Z_OBJ_P(op2));
2498
0
    default:
2499
0
      return 0;
2500
24.0k
  }
2501
24.0k
}
2502
/* }}} */
2503
2504
ZEND_API zend_result ZEND_FASTCALL is_identical_function(zval *result, const zval *op1, const zval *op2) /* {{{ */
2505
586
{
2506
586
  ZVAL_BOOL(result, zend_is_identical(op1, op2));
2507
586
  return SUCCESS;
2508
586
}
2509
/* }}} */
2510
2511
ZEND_API zend_result ZEND_FASTCALL is_not_identical_function(zval *result, const zval *op1, const zval *op2) /* {{{ */
2512
20
{
2513
20
  ZVAL_BOOL(result, !zend_is_identical(op1, op2));
2514
20
  return SUCCESS;
2515
20
}
2516
/* }}} */
2517
2518
ZEND_API zend_result ZEND_FASTCALL is_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2519
144
{
2520
144
  ZVAL_BOOL(result, zend_compare(op1, op2) == 0);
2521
144
  return SUCCESS;
2522
144
}
2523
/* }}} */
2524
2525
ZEND_API zend_result ZEND_FASTCALL is_not_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2526
243
{
2527
243
  ZVAL_BOOL(result, (zend_compare(op1, op2) != 0));
2528
243
  return SUCCESS;
2529
243
}
2530
/* }}} */
2531
2532
ZEND_API zend_result ZEND_FASTCALL is_smaller_function(zval *result, zval *op1, zval *op2) /* {{{ */
2533
1.69k
{
2534
1.69k
  ZVAL_BOOL(result, (zend_compare(op1, op2) < 0));
2535
1.69k
  return SUCCESS;
2536
1.69k
}
2537
/* }}} */
2538
2539
ZEND_API zend_result ZEND_FASTCALL is_smaller_or_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2540
320
{
2541
320
  ZVAL_BOOL(result, (zend_compare(op1, op2) <= 0));
2542
320
  return SUCCESS;
2543
320
}
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
1.69k
{
2548
1.69k
  uint32_t i;
2549
1.69k
  ZEND_ASSERT(interface_ce->ce_flags & ZEND_ACC_INTERFACE);
2550
2551
1.69k
  if (class_ce->num_interfaces) {
2552
1.67k
    ZEND_ASSERT(class_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
2553
3.92k
    for (i = 0; i < class_ce->num_interfaces; i++) {
2554
2.88k
      if (class_ce->interfaces[i] == interface_ce) {
2555
634
        return 1;
2556
634
      }
2557
2.88k
    }
2558
1.67k
  }
2559
1.05k
  return 0;
2560
1.69k
}
2561
/* }}} */
2562
2563
ZEND_API bool ZEND_FASTCALL instanceof_function_slow(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */
2564
12.1M
{
2565
12.1M
  ZEND_ASSERT(instance_ce != ce && "Should have been checked already");
2566
12.1M
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
2567
349k
    uint32_t i;
2568
2569
349k
    if (instance_ce->num_interfaces) {
2570
348k
      ZEND_ASSERT(instance_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
2571
691k
      for (i = 0; i < instance_ce->num_interfaces; i++) {
2572
691k
        if (instance_ce->interfaces[i] == ce) {
2573
348k
          return 1;
2574
348k
        }
2575
691k
      }
2576
348k
    }
2577
899
    return 0;
2578
11.8M
  } else {
2579
34.6M
    while (1) {
2580
34.6M
      instance_ce = instance_ce->parent;
2581
34.6M
      if (instance_ce == ce) {
2582
722k
        return 1;
2583
722k
      }
2584
33.8M
      if (instance_ce == NULL) {
2585
11.0M
        return 0;
2586
11.0M
      }
2587
33.8M
    }
2588
11.8M
  }
2589
12.1M
}
2590
/* }}} */
2591
2592
189
#define LOWER_CASE 1
2593
84
#define UPPER_CASE 2
2594
315
#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
519
{
2611
519
  int carry=0;
2612
519
  size_t pos=Z_STRLEN_P(str)-1;
2613
519
  char *s;
2614
519
  zend_string *t;
2615
519
  int last=0; /* Shut up the compiler warning */
2616
519
  int ch;
2617
2618
519
  zend_string *zstr = Z_STR_P(str);
2619
519
  zend_string_addref(zstr);
2620
519
  zend_error(E_DEPRECATED, "Increment on non-numeric string is deprecated, use str_increment() instead");
2621
519
  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
519
  zval_ptr_dtor(str);
2627
519
  ZVAL_STR(str, zstr);
2628
2629
519
  if (UNEXPECTED(Z_STRLEN_P(str) == 0)) {
2630
12
    zval_ptr_dtor(str);
2631
12
    ZVAL_CHAR(str, '1');
2632
12
    return true;
2633
12
  }
2634
2635
507
  if (!Z_REFCOUNTED_P(str)) {
2636
249
    Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
2637
249
    Z_TYPE_INFO_P(str) = IS_STRING_EX;
2638
258
  } else if (Z_REFCOUNT_P(str) > 1) {
2639
    /* Only release string after allocation succeeded. */
2640
48
    zend_string *orig_str = Z_STR_P(str);
2641
48
    Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
2642
48
    GC_DELREF(orig_str);
2643
210
  } else {
2644
210
    zend_string_forget_hash_val(Z_STR_P(str));
2645
210
  }
2646
507
  s = Z_STRVAL_P(str);
2647
2648
618
  do {
2649
618
    ch = s[pos];
2650
618
    if (ch >= 'a' && ch <= 'z') {
2651
180
      if (ch == 'z') {
2652
33
        s[pos] = 'a';
2653
33
        carry=1;
2654
147
      } else {
2655
147
        s[pos]++;
2656
147
        carry=0;
2657
147
      }
2658
180
      last=LOWER_CASE;
2659
438
    } else if (ch >= 'A' && ch <= 'Z') {
2660
75
      if (ch == 'Z') {
2661
42
        s[pos] = 'A';
2662
42
        carry=1;
2663
42
      } else {
2664
33
        s[pos]++;
2665
33
        carry=0;
2666
33
      }
2667
75
      last=UPPER_CASE;
2668
363
    } else if (ch >= '0' && ch <= '9') {
2669
303
      if (ch == '9') {
2670
66
        s[pos] = '0';
2671
66
        carry=1;
2672
237
      } else {
2673
237
        s[pos]++;
2674
237
        carry=0;
2675
237
      }
2676
303
      last = NUMERIC;
2677
303
    } else {
2678
60
      carry=0;
2679
60
      break;
2680
60
    }
2681
558
    if (carry == 0) {
2682
417
      break;
2683
417
    }
2684
558
  } while (pos-- > 0);
2685
2686
507
  if (carry) {
2687
30
    t = zend_string_alloc(Z_STRLEN_P(str)+1, 0);
2688
30
    memcpy(ZSTR_VAL(t) + 1, Z_STRVAL_P(str), Z_STRLEN_P(str));
2689
30
    ZSTR_VAL(t)[Z_STRLEN_P(str) + 1] = '\0';
2690
30
    switch (last) {
2691
12
      case NUMERIC:
2692
12
        ZSTR_VAL(t)[0] = '1';
2693
12
        break;
2694
9
      case UPPER_CASE:
2695
9
        ZSTR_VAL(t)[0] = 'A';
2696
9
        break;
2697
9
      case LOWER_CASE:
2698
9
        ZSTR_VAL(t)[0] = 'a';
2699
9
        break;
2700
30
    }
2701
30
    zend_string_free(Z_STR_P(str));
2702
30
    ZVAL_NEW_STR(str, t);
2703
30
  }
2704
507
  return true;
2705
507
}
2706
/* }}} */
2707
2708
ZEND_API zend_result ZEND_FASTCALL increment_function(zval *op1) /* {{{ */
2709
5.71k
{
2710
5.71k
try_again:
2711
5.71k
  switch (Z_TYPE_P(op1)) {
2712
1.94k
    case IS_LONG:
2713
1.94k
      fast_long_increment_function(op1);
2714
1.94k
      break;
2715
903
    case IS_DOUBLE:
2716
903
      Z_DVAL_P(op1) = Z_DVAL_P(op1) + 1;
2717
903
      break;
2718
1.48k
    case IS_NULL:
2719
1.48k
      ZVAL_LONG(op1, 1);
2720
1.48k
      break;
2721
720
    case IS_STRING: {
2722
720
        zend_long lval;
2723
720
        double dval;
2724
2725
720
        switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) {
2726
170
          case IS_LONG:
2727
170
            zval_ptr_dtor_str(op1);
2728
170
            if (lval == ZEND_LONG_MAX) {
2729
              /* switch to double */
2730
3
              double d = (double)lval;
2731
3
              ZVAL_DOUBLE(op1, d+1);
2732
167
            } else {
2733
167
              ZVAL_LONG(op1, lval+1);
2734
167
            }
2735
170
            break;
2736
31
          case IS_DOUBLE:
2737
31
            zval_ptr_dtor_str(op1);
2738
31
            ZVAL_DOUBLE(op1, dval+1);
2739
31
            break;
2740
519
          default:
2741
            /* Perl style string increment */
2742
519
            increment_string(op1);
2743
519
            if (EG(exception)) {
2744
0
              return FAILURE;
2745
0
            }
2746
519
            break;
2747
720
        }
2748
720
      }
2749
720
      break;
2750
720
    case IS_FALSE:
2751
620
    case IS_TRUE: {
2752
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2753
620
      zval copy;
2754
620
      ZVAL_COPY_VALUE(&copy, op1);
2755
620
      zend_error(E_WARNING, "Increment on type bool has no effect, this will change in the next major version of PHP");
2756
620
      zval_ptr_dtor(op1);
2757
620
      ZVAL_COPY_VALUE(op1, &copy);
2758
620
      if (EG(exception)) {
2759
0
        return FAILURE;
2760
0
      }
2761
620
      break;
2762
620
    }
2763
620
    case IS_REFERENCE:
2764
0
      op1 = Z_REFVAL_P(op1);
2765
0
      goto try_again;
2766
48
    case IS_OBJECT: {
2767
48
      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
48
      zval tmp;
2775
48
      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
48
      ZEND_FALLTHROUGH;
2782
48
    }
2783
48
    case IS_RESOURCE:
2784
48
    case IS_ARRAY:
2785
48
      zend_type_error("Cannot increment %s", zend_zval_value_name(op1));
2786
48
      return FAILURE;
2787
0
    default: ZEND_UNREACHABLE();
2788
5.71k
  }
2789
5.67k
  return SUCCESS;
2790
5.71k
}
2791
/* }}} */
2792
2793
ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op1) /* {{{ */
2794
8.96k
{
2795
8.96k
  zend_long lval;
2796
8.96k
  double dval;
2797
2798
8.96k
try_again:
2799
8.96k
  switch (Z_TYPE_P(op1)) {
2800
497
    case IS_LONG:
2801
497
      fast_long_decrement_function(op1);
2802
497
      break;
2803
3.58k
    case IS_DOUBLE:
2804
3.58k
      Z_DVAL_P(op1) = Z_DVAL_P(op1) - 1;
2805
3.58k
      break;
2806
1.94k
    case IS_STRING:   /* Like perl we only support string increment */
2807
1.94k
      if (Z_STRLEN_P(op1) == 0) { /* consider as 0 */
2808
6
        zend_error(E_DEPRECATED, "Decrement on empty string is deprecated as non-numeric");
2809
6
        if (EG(exception)) {
2810
0
          return FAILURE;
2811
0
        }
2812
        /* A userland error handler can change the type from string to something else */
2813
6
        zval_ptr_dtor(op1);
2814
6
        ZVAL_LONG(op1, -1);
2815
6
        break;
2816
6
      }
2817
1.93k
      switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) {
2818
884
        case IS_LONG:
2819
884
          zval_ptr_dtor_str(op1);
2820
884
          if (lval == ZEND_LONG_MIN) {
2821
3
            double d = (double)lval;
2822
3
            ZVAL_DOUBLE(op1, d-1);
2823
881
          } else {
2824
881
            ZVAL_LONG(op1, lval-1);
2825
881
          }
2826
884
          break;
2827
123
        case IS_DOUBLE:
2828
123
          zval_ptr_dtor_str(op1);
2829
123
          ZVAL_DOUBLE(op1, dval - 1);
2830
123
          break;
2831
932
        default: {
2832
          /* Error handler can unset the variable */
2833
932
          zend_string *zstr = Z_STR_P(op1);
2834
932
          zend_string_addref(zstr);
2835
932
          zend_error(E_DEPRECATED, "Decrement on non-numeric string has no effect and is deprecated");
2836
932
          if (EG(exception)) {
2837
0
            zend_string_release(zstr);
2838
0
            return FAILURE;
2839
0
          }
2840
932
          zval_ptr_dtor(op1);
2841
932
          ZVAL_STR(op1, zstr);
2842
932
        }
2843
1.93k
      }
2844
1.93k
      break;
2845
1.93k
    case IS_NULL: {
2846
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2847
1.83k
      zval copy;
2848
1.83k
      ZVAL_COPY_VALUE(&copy, op1);
2849
1.83k
      zend_error(E_WARNING, "Decrement on type null has no effect, this will change in the next major version of PHP");
2850
1.83k
      zval_ptr_dtor(op1);
2851
1.83k
      ZVAL_COPY_VALUE(op1, &copy);
2852
1.83k
      if (EG(exception)) {
2853
0
        return FAILURE;
2854
0
      }
2855
1.83k
      break;
2856
1.83k
    }
2857
1.83k
    case IS_FALSE:
2858
1.06k
    case IS_TRUE: {
2859
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2860
1.06k
      zval copy;
2861
1.06k
      ZVAL_COPY_VALUE(&copy, op1);
2862
1.06k
      zend_error(E_WARNING, "Decrement on type bool has no effect, this will change in the next major version of PHP");
2863
1.06k
      zval_ptr_dtor(op1);
2864
1.06k
      ZVAL_COPY_VALUE(op1, &copy);
2865
1.06k
      if (EG(exception)) {
2866
0
        return FAILURE;
2867
0
      }
2868
1.06k
      break;
2869
1.06k
    }
2870
1.06k
    case IS_REFERENCE:
2871
0
      op1 = Z_REFVAL_P(op1);
2872
0
      goto try_again;
2873
45
    case IS_OBJECT: {
2874
45
      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
45
      zval tmp;
2882
45
      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
45
      ZEND_FALLTHROUGH;
2889
45
    }
2890
45
    case IS_RESOURCE:
2891
45
    case IS_ARRAY:
2892
45
      zend_type_error("Cannot decrement %s", zend_zval_value_name(op1));
2893
45
      return FAILURE;
2894
0
    default: ZEND_UNREACHABLE();
2895
8.96k
  }
2896
2897
8.92k
  return SUCCESS;
2898
8.96k
}
2899
/* }}} */
2900
2901
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op) /* {{{ */
2902
126k
{
2903
126k
  return i_zend_is_true(op);
2904
126k
}
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
2
{
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
2
  if (MB_CUR_MAX > 1) {
2942
2
#ifdef HAVE_NL_LANGINFO
2943
2
    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
2
    CG(variable_width_locale) = 1;
2967
2
    CG(ascii_compatible_locale) = 0;
2968
2969
2
    if (charmap) {
2970
2
      size_t len = strlen(charmap);
2971
2
      static const char *ascii_compatible_charmaps[] = {
2972
2
        "utf-8",
2973
2
        "utf8",
2974
        // TODO: EUC-* are also ASCII compatible ???
2975
2
        NULL
2976
2
      };
2977
2
      const char **p;
2978
      /* Check if current locale is ASCII compatible */
2979
2
      for (p = ascii_compatible_charmaps; *p; p++) {
2980
2
        if (zend_binary_strcasecmp(charmap, len, *p, strlen(*p)) == 0) {
2981
2
          CG(ascii_compatible_locale) = 1;
2982
2
          break;
2983
2
        }
2984
2
      }
2985
2
    }
2986
2987
2
  } 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
2
}
2997
/* }}} */
2998
2999
ZEND_API void zend_reset_lc_ctype_locale(void)
3000
2
{
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
2
  if (!setlocale(LC_CTYPE, "C.UTF-8")) {
3004
0
    setlocale(LC_CTYPE, "C");
3005
0
  }
3006
2
}
3007
3008
13.5k
static zend_always_inline void zend_str_tolower_impl(char *dest, const char *str, size_t length) /* {{{ */ {
3009
13.5k
  unsigned char *p = (unsigned char*)str;
3010
13.5k
  unsigned char *q = (unsigned char*)dest;
3011
13.5k
  unsigned char *end = p + length;
3012
13.5k
#ifdef HAVE_BLOCKCONV
3013
13.5k
  if (length >= BLOCKCONV_STRIDE) {
3014
1.63k
    BLOCKCONV_INIT_RANGE('A', 'Z');
3015
1.63k
    BLOCKCONV_INIT_DELTA('a' - 'A');
3016
6.52k
    do {
3017
6.52k
      BLOCKCONV_LOAD(p);
3018
6.52k
      BLOCKCONV_STORE(q);
3019
6.52k
      p += BLOCKCONV_STRIDE;
3020
6.52k
      q += BLOCKCONV_STRIDE;
3021
6.52k
    } while (p + BLOCKCONV_STRIDE <= end);
3022
1.63k
  }
3023
13.5k
#endif
3024
82.3k
  while (p < end) {
3025
68.7k
    *q++ = zend_tolower_ascii(*p++);
3026
68.7k
  }
3027
13.5k
}
3028
/* }}} */
3029
3030
15
static zend_always_inline void zend_str_toupper_impl(char *dest, const char *str, size_t length) /* {{{ */ {
3031
15
  unsigned char *p = (unsigned char*)str;
3032
15
  unsigned char *q = (unsigned char*)dest;
3033
15
  unsigned char *end = p + length;
3034
15
#ifdef HAVE_BLOCKCONV
3035
15
  if (length >= BLOCKCONV_STRIDE) {
3036
9
    BLOCKCONV_INIT_RANGE('a', 'z');
3037
9
    BLOCKCONV_INIT_DELTA('A' - 'a');
3038
42
    do {
3039
42
      BLOCKCONV_LOAD(p);
3040
42
      BLOCKCONV_STORE(q);
3041
42
      p += BLOCKCONV_STRIDE;
3042
42
      q += BLOCKCONV_STRIDE;
3043
42
    } while (p + BLOCKCONV_STRIDE <= end);
3044
9
  }
3045
15
#endif
3046
81
  while (p < end) {
3047
66
    *q++ = zend_toupper_ascii(*p++);
3048
66
  }
3049
15
}
3050
/* }}} */
3051
3052
ZEND_API char* ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length) /* {{{ */
3053
5.70k
{
3054
5.70k
  zend_str_tolower_impl(dest, source, length);
3055
5.70k
  dest[length] = '\0';
3056
5.70k
  return dest;
3057
5.70k
}
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
201
{
3070
201
  return zend_str_tolower_copy((char *)emalloc(length+1), source, length);
3071
201
}
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
602
{
3082
602
  zend_str_tolower_impl(str, (const char*)str, length);
3083
602
}
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
51
{
3095
51
  const unsigned char *p = (const unsigned char*)source;
3096
51
  const unsigned char *end = p + length;
3097
3098
2.38k
  while (p < end) {
3099
2.38k
    if (*p != zend_tolower_ascii(*p)) {
3100
48
      char *res = (char*)emalloc(length + 1);
3101
48
      unsigned char *r;
3102
3103
48
      if (p != (const unsigned char*)source) {
3104
48
        memcpy(res, source, p - (const unsigned char*)source);
3105
48
      }
3106
48
      r = (unsigned char*)p + (res - source);
3107
48
      zend_str_tolower_impl((char *)r, (const char*)p, end - p);
3108
48
      res[length] = '\0';
3109
48
      return res;
3110
48
    }
3111
2.33k
    p++;
3112
2.33k
  }
3113
3
  return NULL;
3114
51
}
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
198k
{
3143
198k
  size_t length = ZSTR_LEN(str);
3144
198k
  unsigned char *p = (unsigned char *) ZSTR_VAL(str);
3145
198k
  unsigned char *end = p + length;
3146
3147
198k
#ifdef HAVE_BLOCKCONV
3148
198k
  BLOCKCONV_INIT_RANGE('A', 'Z');
3149
212k
  while (p + BLOCKCONV_STRIDE <= end) {
3150
21.5k
    BLOCKCONV_LOAD(p);
3151
21.5k
    if (BLOCKCONV_FOUND()) {
3152
7.21k
      zend_string *res = zend_string_alloc(length, persistent);
3153
7.21k
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char *) ZSTR_VAL(str));
3154
7.21k
      unsigned char *q = (unsigned char*) ZSTR_VAL(res) + (p - (unsigned char*) ZSTR_VAL(str));
3155
3156
      /* Lowercase the chunk we already compared. */
3157
7.21k
      BLOCKCONV_INIT_DELTA('a' - 'A');
3158
7.21k
      BLOCKCONV_STORE(q);
3159
3160
      /* Lowercase the rest of the string. */
3161
7.21k
      p += BLOCKCONV_STRIDE;
3162
7.21k
      q += BLOCKCONV_STRIDE;
3163
7.21k
      zend_str_tolower_impl((char *) q, (const char *) p, end - p);
3164
7.21k
      ZSTR_VAL(res)[length] = '\0';
3165
7.21k
      return res;
3166
7.21k
    }
3167
14.3k
    p += BLOCKCONV_STRIDE;
3168
14.3k
  }
3169
190k
#endif
3170
3171
925k
  while (p < end) {
3172
823k
    if (*p != zend_tolower_ascii(*p)) {
3173
88.4k
      zend_string *res = zend_string_alloc(length, persistent);
3174
88.4k
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str));
3175
3176
88.4k
      unsigned char *q = (unsigned char*) ZSTR_VAL(res) + (p - (unsigned char*) ZSTR_VAL(str));
3177
627k
      while (p < end) {
3178
539k
        *q++ = zend_tolower_ascii(*p++);
3179
539k
      }
3180
88.4k
      ZSTR_VAL(res)[length] = '\0';
3181
88.4k
      return res;
3182
88.4k
    }
3183
735k
    p++;
3184
735k
  }
3185
3186
102k
  return zend_string_copy(str);
3187
190k
}
3188
/* }}} */
3189
3190
ZEND_API zend_string* ZEND_FASTCALL zend_string_toupper_ex(zend_string *str, bool persistent) /* {{{ */
3191
873
{
3192
873
  size_t length = ZSTR_LEN(str);
3193
873
  unsigned char *p = (unsigned char *) ZSTR_VAL(str);
3194
873
  unsigned char *end = p + length;
3195
3196
873
#ifdef HAVE_BLOCKCONV
3197
873
  BLOCKCONV_INIT_RANGE('a', 'z');
3198
876
  while (p + BLOCKCONV_STRIDE <= end) {
3199
18
    BLOCKCONV_LOAD(p);
3200
18
    if (BLOCKCONV_FOUND()) {
3201
15
      zend_string *res = zend_string_alloc(length, persistent);
3202
15
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char *) ZSTR_VAL(str));
3203
15
      unsigned char *q = (unsigned char *) ZSTR_VAL(res) + (p - (unsigned char *) ZSTR_VAL(str));
3204
3205
      /* Uppercase the chunk we already compared. */
3206
15
      BLOCKCONV_INIT_DELTA('A' - 'a');
3207
15
      BLOCKCONV_STORE(q);
3208
3209
      /* Uppercase the rest of the string. */
3210
15
      p += BLOCKCONV_STRIDE;
3211
15
      q += BLOCKCONV_STRIDE;
3212
15
      zend_str_toupper_impl((char *) q, (const char *) p, end - p);
3213
15
      ZSTR_VAL(res)[length] = '\0';
3214
15
      return res;
3215
15
    }
3216
3
    p += BLOCKCONV_STRIDE;
3217
3
  }
3218
858
#endif
3219
3220
885
  while (p < end) {
3221
777
    if (*p != zend_toupper_ascii(*p)) {
3222
750
      zend_string *res = zend_string_alloc(length, persistent);
3223
750
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str));
3224
3225
750
      unsigned char *q = (unsigned char *) ZSTR_VAL(res) + (p - (unsigned char *) ZSTR_VAL(str));
3226
6.42k
      while (p < end) {
3227
5.67k
        *q++ = zend_toupper_ascii(*p++);
3228
5.67k
      }
3229
750
      ZSTR_VAL(res)[length] = '\0';
3230
750
      return res;
3231
750
    }
3232
27
    p++;
3233
27
  }
3234
3235
108
  return zend_string_copy(str);
3236
858
}
3237
/* }}} */
3238
3239
ZEND_API int ZEND_FASTCALL zend_binary_strcmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
3240
40.2k
{
3241
40.2k
  int retval;
3242
3243
40.2k
  if (s1 == s2) {
3244
9
    return 0;
3245
9
  }
3246
40.2k
  retval = memcmp(s1, s2, MIN(len1, len2));
3247
40.2k
  if (!retval) {
3248
4.95k
    return ZEND_THREEWAY_COMPARE(len1, len2);
3249
35.3k
  } else {
3250
35.3k
    return retval;
3251
35.3k
  }
3252
40.2k
}
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
27
{
3257
27
  int retval;
3258
3259
27
  if (s1 == s2) {
3260
6
    return 0;
3261
6
  }
3262
21
  retval = memcmp(s1, s2, MIN(length, MIN(len1, len2)));
3263
21
  if (!retval) {
3264
15
    return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2));
3265
15
  } else {
3266
6
    return retval;
3267
6
  }
3268
21
}
3269
/* }}} */
3270
3271
ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
3272
76.3k
{
3273
76.3k
  size_t len;
3274
76.3k
  int c1, c2;
3275
3276
76.3k
  if (s1 == s2) {
3277
2.42k
    return 0;
3278
2.42k
  }
3279
3280
73.8k
  len = MIN(len1, len2);
3281
175k
  while (len--) {
3282
156k
    c1 = zend_tolower_ascii(*(unsigned char *)s1++);
3283
156k
    c2 = zend_tolower_ascii(*(unsigned char *)s2++);
3284
156k
    if (c1 != c2) {
3285
55.6k
      return c1 - c2;
3286
55.6k
    }
3287
156k
  }
3288
3289
18.2k
  return ZEND_THREEWAY_COMPARE(len1, len2);
3290
73.8k
}
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
21
{
3295
21
  size_t len;
3296
21
  int c1, c2;
3297
3298
21
  if (s1 == s2) {
3299
3
    return 0;
3300
3
  }
3301
18
  len = MIN(length, MIN(len1, len2));
3302
66
  while (len--) {
3303
51
    c1 = zend_tolower_ascii(*(unsigned char *)s1++);
3304
51
    c2 = zend_tolower_ascii(*(unsigned char *)s2++);
3305
51
    if (c1 != c2) {
3306
3
      return c1 - c2;
3307
3
    }
3308
51
  }
3309
3310
15
  return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2));
3311
18
}
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((unsigned char)*(s1++));
3326
0
    c2 = zend_tolower((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((unsigned char)*(s1++));
3347
0
    c2 = zend_tolower((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(const zval *s1, const 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(const zval *s1, const zval *s2, const 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(const zend_string *s1, const zend_string *s2) /* {{{ */
3370
816
{
3371
816
  uint8_t ret1, ret2;
3372
816
  int oflow1, oflow2;
3373
816
  zend_long lval1 = 0, lval2 = 0;
3374
816
  double dval1 = 0.0, dval2 = 0.0;
3375
3376
816
  if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, false, &oflow1, NULL)) &&
3377
532
    (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
443
    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
443
    if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) {
3390
158
      if (ret1 != IS_DOUBLE) {
3391
69
        if (oflow2) {
3392
          /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
3393
57
          return 0;
3394
57
        }
3395
12
        dval1 = (double) lval1;
3396
89
      } else if (ret2 != IS_DOUBLE) {
3397
28
        if (oflow1) {
3398
1
          return 0;
3399
1
        }
3400
27
        dval2 = (double) lval2;
3401
61
      } 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
70
      return dval1 == dval2;
3407
285
    } else { /* they both have to be long's */
3408
285
      return lval1 == lval2;
3409
285
    }
3410
443
  } else {
3411
403
string_cmp:
3412
403
    return zend_string_equal_content(s1, s2);
3413
373
  }
3414
816
}
3415
/* }}} */
3416
3417
ZEND_API int ZEND_FASTCALL zendi_smart_strcmp(const zend_string *s1, const zend_string *s2) /* {{{ */
3418
4.97k
{
3419
4.97k
  uint8_t ret1, ret2;
3420
4.97k
  int oflow1, oflow2;
3421
4.97k
  zend_long lval1 = 0, lval2 = 0;
3422
4.97k
  double dval1 = 0.0, dval2 = 0.0;
3423
3424
4.97k
  if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, false, &oflow1, NULL)) &&
3425
133
    (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
102
    if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0.) {
3432
14
#endif
3433
      /* both values are integers overflowed to the same side, and the
3434
       * double comparison may have resulted in crucial accuracy lost */
3435
14
      goto string_cmp;
3436
14
    }
3437
88
    if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) {
3438
28
      if (ret1 != IS_DOUBLE) {
3439
14
        if (oflow2) {
3440
          /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
3441
14
          return -1 * oflow2;
3442
14
        }
3443
0
        dval1 = (double) lval1;
3444
14
      } else if (ret2 != IS_DOUBLE) {
3445
2
        if (oflow1) {
3446
2
          return oflow1;
3447
2
        }
3448
0
        dval2 = (double) lval2;
3449
12
      } 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
0
        goto string_cmp;
3453
0
      }
3454
12
      dval1 = dval1 - dval2;
3455
12
      return ZEND_NORMALIZE_BOOL(dval1);
3456
60
    } else { /* they both have to be long's */
3457
60
      return lval1 > lval2 ? 1 : (lval1 < lval2 ? -1 : 0);
3458
60
    }
3459
4.86k
  } else {
3460
4.86k
    int strcmp_ret;
3461
4.88k
string_cmp:
3462
4.88k
    strcmp_ret = zend_binary_strcmp(s1->val, s1->len, s2->val, s2->len);
3463
4.88k
    return ZEND_NORMALIZE_BOOL(strcmp_ret);
3464
4.86k
  }
3465
4.97k
}
3466
/* }}} */
3467
3468
static int hash_zval_compare_function(zval *z1, zval *z2) /* {{{ */
3469
135
{
3470
135
  return zend_compare(z1, z2);
3471
135
}
3472
/* }}} */
3473
3474
ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable *ht2) /* {{{ */
3475
2.59k
{
3476
2.59k
  if (ht1 == ht2) {
3477
1.02k
    return 0;
3478
1.02k
  }
3479
3480
1.56k
  GC_TRY_ADDREF(ht1);
3481
1.56k
  GC_TRY_ADDREF(ht2);
3482
3483
1.56k
  int ret = zend_hash_compare(ht1, ht2, (compare_func_t) hash_zval_compare_function, 0);
3484
3485
1.56k
  GC_TRY_DTOR_NO_REF(ht1);
3486
1.56k
  GC_TRY_DTOR_NO_REF(ht2);
3487
3488
1.56k
  return ret;
3489
2.59k
}
3490
/* }}} */
3491
3492
ZEND_API int ZEND_FASTCALL zend_compare_arrays(const zval *a1, const zval *a2) /* {{{ */
3493
2.56k
{
3494
2.56k
  return zend_compare_symbol_tables(Z_ARRVAL_P(a1), Z_ARRVAL_P(a2));
3495
2.56k
}
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
39.6k
{
3514
39.6k
  if ((zend_ulong)num <= 9) {
3515
20.5k
    return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num);
3516
20.5k
  } else {
3517
19.1k
    char buf[MAX_LENGTH_OF_LONG + 1];
3518
19.1k
    char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
3519
19.1k
    zend_string *str =  zend_string_init(res, buf + sizeof(buf) - 1 - res, 0);
3520
19.1k
    GC_ADD_FLAGS(str, IS_STR_VALID_UTF8);
3521
19.1k
    return str;
3522
19.1k
  }
3523
39.6k
}
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
7.60k
{
3598
7.60k
  char buf[ZEND_DOUBLE_MAX_LENGTH];
3599
  /* Model snprintf precision behavior. */
3600
7.60k
  int precision = (int) EG(precision);
3601
7.60k
  zend_gcvt(num, precision ? precision : 1, '.', 'E', buf);
3602
7.60k
  zend_string *str =  zend_string_init(buf, strlen(buf), 0);
3603
7.60k
  if (UNEXPECTED(zend_isnan(num))) {
3604
42
    zend_nan_coerced_to_type_warning(IS_STRING);
3605
42
  }
3606
7.60k
  GC_ADD_FLAGS(str, IS_STR_VALID_UTF8);
3607
7.60k
  return str;
3608
7.60k
}
3609
3610
ZEND_API uint8_t ZEND_FASTCALL is_numeric_str_function(const zend_string *str, zend_long *lval, double *dval) /* {{{ */
3611
3.93k
{
3612
3.93k
  return is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), lval, dval, false);
3613
3.93k
}
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
46.9k
{
3619
46.9k
  const char *ptr;
3620
46.9k
  int digits = 0, dp_or_e = 0;
3621
46.9k
  double local_dval = 0.0;
3622
46.9k
  uint8_t type;
3623
46.9k
  zend_ulong tmp_lval = 0;
3624
46.9k
  int neg = 0;
3625
3626
46.9k
  if (!length) {
3627
1.13k
    return 0;
3628
1.13k
  }
3629
3630
45.7k
  if (oflow_info != NULL) {
3631
5.92k
    *oflow_info = 0;
3632
5.92k
  }
3633
45.7k
  if (trailing_data != NULL) {
3634
20.9k
    *trailing_data = false;
3635
20.9k
  }
3636
3637
  /* Skip any whitespace
3638
   * This is much faster than the isspace() function */
3639
52.3k
  while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\v' || *str == '\f') {
3640
6.52k
    str++;
3641
6.52k
    length--;
3642
6.52k
  }
3643
45.7k
  ptr = str;
3644
3645
45.7k
  if (*ptr == '-') {
3646
9.80k
    neg = 1;
3647
9.80k
    ptr++;
3648
35.9k
  } else if (*ptr == '+') {
3649
14
    ptr++;
3650
14
  }
3651
3652
45.7k
  if (ZEND_IS_DIGIT(*ptr)) {
3653
    /* Skip any leading 0s */
3654
151M
    while (*ptr == '0') {
3655
151M
      ptr++;
3656
151M
    }
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
312k
    for (type = IS_LONG; !(digits >= MAX_LENGTH_OF_LONG && (dval || allow_errors)); digits++, ptr++) {
3662
310k
check_digits:
3663
310k
      if (ZEND_IS_DIGIT(*ptr)) {
3664
277k
        tmp_lval = tmp_lval * 10 + (*ptr) - '0';
3665
277k
        continue;
3666
277k
      } else if (*ptr == '.' && dp_or_e < 1) {
3667
6.84k
        goto process_double;
3668
27.0k
      } else if ((*ptr == 'e' || *ptr == 'E') && dp_or_e < 2) {
3669
304
        const char *e = ptr + 1;
3670
3671
304
        if (*e == '-' || *e == '+') {
3672
13
          ptr = e++;
3673
13
        }
3674
304
        if (ZEND_IS_DIGIT(*e)) {
3675
258
          goto process_double;
3676
258
        }
3677
304
      }
3678
3679
26.7k
      break;
3680
310k
    }
3681
3682
29.5k
    if (digits >= MAX_LENGTH_OF_LONG) {
3683
4.31k
      if (oflow_info != NULL) {
3684
320
        *oflow_info = *str == '-' ? -1 : 1;
3685
320
      }
3686
4.31k
      dp_or_e = -1;
3687
4.31k
      goto process_double;
3688
4.31k
    }
3689
29.5k
  } else if (*ptr == '.' && ZEND_IS_DIGIT(ptr[1])) {
3690
11.4k
process_double:
3691
11.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
11.4k
    if (dval) {
3696
8.97k
      local_dval = zend_strtod(str, &ptr);
3697
8.97k
    } else if (!allow_errors && dp_or_e != -1) {
3698
845
      dp_or_e = (*ptr++ == '.') ? 1 : 2;
3699
845
      goto check_digits;
3700
845
    }
3701
11.4k
  } else {
3702
9.97k
    return 0;
3703
9.97k
  }
3704
3705
35.8k
  if (ptr != str + length) {
3706
17.9k
    const char *endptr = ptr;
3707
24.5k
    while (*endptr == ' ' || *endptr == '\t' || *endptr == '\n' || *endptr == '\r' || *endptr == '\v' || *endptr == '\f') {
3708
6.57k
      endptr++;
3709
6.57k
      length--;
3710
6.57k
    }
3711
17.9k
    if (ptr != str + length) {
3712
17.5k
      if (!allow_errors) {
3713
6.82k
        return 0;
3714
6.82k
      }
3715
10.6k
      if (trailing_data != NULL) {
3716
10.6k
        *trailing_data = true;
3717
10.6k
      }
3718
10.6k
    }
3719
17.9k
  }
3720
3721
28.9k
  if (type == IS_LONG) {
3722
19.6k
    if (digits == MAX_LENGTH_OF_LONG - 1) {
3723
438
      int cmp = strcmp(&ptr[-digits], long_min_digits);
3724
3725
438
      if (!(cmp < 0 || (cmp == 0 && *str == '-'))) {
3726
37
        if (dval) {
3727
37
          *dval = zend_strtod(str, NULL);
3728
37
        }
3729
37
        if (oflow_info != NULL) {
3730
26
          *oflow_info = *str == '-' ? -1 : 1;
3731
26
        }
3732
3733
37
        return IS_DOUBLE;
3734
37
      }
3735
438
    }
3736
3737
19.5k
    if (lval) {
3738
18.6k
      if (neg) {
3739
5.57k
        tmp_lval = -tmp_lval;
3740
5.57k
      }
3741
18.6k
      *lval = (zend_long) tmp_lval;
3742
18.6k
    }
3743
3744
19.5k
    return IS_LONG;
3745
19.6k
  } else {
3746
9.36k
    if (dval) {
3747
8.30k
      *dval = local_dval;
3748
8.30k
    }
3749
3750
9.36k
    return IS_DOUBLE;
3751
9.36k
  }
3752
28.9k
}
3753
/* }}} */
3754
3755
/*
3756
 * String matching - Sunday algorithm
3757
 * http://www.iti.fh-flensburg.de/lang/algorithmen/pattern/sundayen.htm
3758
 */
3759
61
static zend_always_inline void zend_memnstr_ex_pre(unsigned int td[], const char *needle, size_t needle_len, int reverse) /* {{{ */ {
3760
61
  int i;
3761
3762
15.6k
  for (i = 0; i < 256; i++) {
3763
15.6k
    td[i] = needle_len + 1;
3764
15.6k
  }
3765
3766
61
  if (reverse) {
3767
0
    for (i = needle_len - 1; i >= 0; i--) {
3768
0
      td[(unsigned char)needle[i]] = i + 1;
3769
0
    }
3770
61
  } else {
3771
61
    size_t i;
3772
3773
5.63k
    for (i = 0; i < needle_len; i++) {
3774
5.57k
      td[(unsigned char)needle[i]] = (int)needle_len - i;
3775
5.57k
    }
3776
61
  }
3777
61
}
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
61
{
3782
61
  unsigned int td[256];
3783
61
  size_t i;
3784
61
  const char *p;
3785
3786
61
  if (needle_len == 0 || (end - haystack) < needle_len) {
3787
0
    return NULL;
3788
0
  }
3789
3790
61
  zend_memnstr_ex_pre(td, needle, needle_len, 0);
3791
3792
61
  p = haystack;
3793
61
  end -= needle_len;
3794
3795
2.27k
  while (p <= end) {
3796
2.36k
    for (i = 0; i < needle_len; i++) {
3797
2.36k
      if (needle[i] != p[i]) {
3798
2.21k
        break;
3799
2.21k
      }
3800
2.36k
    }
3801
2.21k
    if (i == needle_len) {
3802
0
      return p;
3803
0
    }
3804
2.21k
    if (UNEXPECTED(p == end)) {
3805
3
      return NULL;
3806
3
    }
3807
2.21k
    p += td[(unsigned char)(p[needle_len])];
3808
2.21k
  }
3809
3810
58
  return NULL;
3811
61
}
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
8.37k
{
3868
8.37k
  double  two_pow_64 = pow(2., 64.),
3869
8.37k
      dmod;
3870
3871
8.37k
  dmod = fmod(d, two_pow_64);
3872
8.37k
  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
1.95k
    dmod += two_pow_64;
3876
1.95k
  }
3877
8.37k
  return (zend_long)(zend_ulong)dmod;
3878
8.37k
}
3879
/* }}} */
3880
#endif