Coverage Report

Created: 2025-12-14 06:09

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