Coverage Report

Created: 2025-12-14 06:06

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
0
#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
337k
  const __m128i blconv_offset = _mm_set1_epi8((signed char)(SCHAR_MIN - start)); \
93
337k
  const __m128i blconv_threshold = _mm_set1_epi8(SCHAR_MIN + (end - start) + 1);
94
95
465k
#define BLOCKCONV_STRIDE sizeof(__m128i)
96
97
#define BLOCKCONV_INIT_DELTA(delta) \
98
35.4k
  const __m128i blconv_delta = _mm_set1_epi8(delta);
99
100
#define BLOCKCONV_LOAD(input) \
101
42.6k
  __m128i blconv_operand = _mm_loadu_si128((__m128i*)(input)); \
102
42.6k
  __m128i blconv_mask = _mm_cmplt_epi8(_mm_add_epi8(blconv_operand, blconv_offset), blconv_threshold);
103
104
36.2k
#define BLOCKCONV_FOUND() _mm_movemask_epi8(blconv_mask)
105
106
#define BLOCKCONV_STORE(dest) \
107
41.7k
  __m128i blconv_add = _mm_and_si128(blconv_mask, blconv_delta); \
108
41.7k
  __m128i blconv_result = _mm_add_epi8(blconv_operand, blconv_add); \
109
41.7k
  _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
383
  ZVAL_UNDEF(dst);                                    \
234
383
  if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), dst, ctype) == FAILURE) {         \
235
383
    zend_error(E_WARNING,                               \
236
383
      "Object of class %s could not be converted to %s", ZSTR_VAL(Z_OBJCE_P(op)->name),\
237
383
    zend_get_type_by_const(ctype));                           \
238
383
  }                                            \
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
0
{
294
0
  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
0
    case IS_STRING:
303
0
      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
0
        ZVAL_LONG(holder, 0);
305
0
      }
306
0
      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
0
    case IS_LONG:
318
0
    case IS_DOUBLE:
319
0
    default:
320
0
      return op;
321
0
  }
322
0
}
323
/* }}} */
324
325
static zend_never_inline zend_result ZEND_FASTCALL _zendi_try_convert_scalar_to_number(zval *op, zval *holder) /* {{{ */
326
0
{
327
0
  switch (Z_TYPE_P(op)) {
328
0
    case IS_NULL:
329
0
    case IS_FALSE:
330
0
      ZVAL_LONG(holder, 0);
331
0
      return SUCCESS;
332
0
    case IS_TRUE:
333
0
      ZVAL_LONG(holder, 1);
334
0
      return SUCCESS;
335
0
    case IS_STRING:
336
0
    {
337
0
      bool trailing_data = false;
338
      /* For BC reasons we allow errors so that we can warn on leading numeric string */
339
0
      if (0 == (Z_TYPE_INFO_P(holder) = is_numeric_string_ex(Z_STRVAL_P(op), Z_STRLEN_P(op),
340
0
          &Z_LVAL_P(holder), &Z_DVAL_P(holder),  /* allow errors */ true, NULL, &trailing_data))) {
341
        /* Will lead to invalid OP type error */
342
0
        return FAILURE;
343
0
      }
344
0
      if (UNEXPECTED(trailing_data)) {
345
0
        zend_error(E_WARNING, "A non-numeric value encountered");
346
0
        if (UNEXPECTED(EG(exception))) {
347
0
          return FAILURE;
348
0
        }
349
0
      }
350
0
      return SUCCESS;
351
0
    }
352
0
    case IS_OBJECT:
353
0
      if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), holder, _IS_NUMBER) == FAILURE
354
0
          || EG(exception)) {
355
0
        return FAILURE;
356
0
      }
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
0
    case IS_ARRAY:
361
0
      return FAILURE;
362
0
    EMPTY_SWITCH_DEFAULT_CASE()
363
0
  }
364
0
}
365
/* }}} */
366
367
static zend_always_inline zend_result zendi_try_convert_scalar_to_number(zval *op, zval *holder) /* {{{ */
368
0
{
369
0
  if (Z_TYPE_P(op) == IS_LONG || Z_TYPE_P(op) == IS_DOUBLE) {
370
0
    ZVAL_COPY_VALUE(holder, op);
371
0
    return SUCCESS;
372
0
  } else {
373
0
    return _zendi_try_convert_scalar_to_number(op, holder);
374
0
  }
375
0
}
376
/* }}} */
377
378
static zend_never_inline zend_long ZEND_FASTCALL zendi_try_get_long(const zval *op, bool *failed) /* {{{ */
379
0
{
380
0
  *failed = false;
381
0
try_again:
382
0
  switch (Z_TYPE_P(op)) {
383
0
    case IS_NULL:
384
0
    case IS_FALSE:
385
0
      return 0;
386
0
    case IS_TRUE:
387
0
      return 1;
388
0
    case IS_DOUBLE: {
389
0
      double dval = Z_DVAL_P(op);
390
0
      zend_long lval = zend_dval_to_lval_safe(dval);
391
0
      if (UNEXPECTED(EG(exception))) {
392
0
        *failed = true;
393
0
      }
394
0
      return lval;
395
0
    }
396
0
    case IS_STRING:
397
0
      {
398
0
        uint8_t type;
399
0
        zend_long lval;
400
0
        double dval;
401
0
        bool trailing_data = false;
402
0
        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
0
        type = is_numeric_string_ex(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval,
406
0
          /* allow errors */ true, NULL, &trailing_data);
407
0
        if (type == 0) {
408
0
          *failed = true;
409
0
          return 0;
410
0
        }
411
0
        if (UNEXPECTED(trailing_data)) {
412
0
          if (type != IS_LONG) {
413
0
            op_str = zend_string_copy(Z_STR_P(op));
414
0
          }
415
0
          zend_error(E_WARNING, "A non-numeric value encountered");
416
0
          if (UNEXPECTED(EG(exception))) {
417
0
            *failed = true;
418
0
            zend_tmp_string_release(op_str);
419
0
            return 0;
420
0
          }
421
0
        }
422
0
        if (EXPECTED(type == IS_LONG)) {
423
0
          return lval;
424
0
        } 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
0
          if (op_str == NULL) {
431
            /* zend_dval_to_lval_cap() can emit a warning so always do the copy here */
432
0
            op_str = zend_string_copy(Z_STR_P(op));
433
0
          }
434
0
          lval = zend_dval_to_lval_cap(dval, op_str);
435
0
          if (!zend_is_long_compatible(dval, lval)) {
436
0
            zend_incompatible_string_to_long_error(op_str);
437
0
            if (UNEXPECTED(EG(exception))) {
438
0
              *failed = true;
439
0
            }
440
0
          }
441
0
          zend_string_release(op_str);
442
0
          return lval;
443
0
        }
444
0
      }
445
0
    case IS_OBJECT:
446
0
      {
447
0
        zval dst;
448
0
        if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &dst, IS_LONG) == FAILURE
449
0
            || EG(exception)) {
450
0
          *failed = true;
451
0
          return 0;
452
0
        }
453
0
        ZEND_ASSERT(Z_TYPE(dst) == IS_LONG);
454
0
        return Z_LVAL(dst);
455
0
      }
456
0
    case IS_RESOURCE:
457
0
    case IS_ARRAY:
458
0
      *failed = true;
459
0
      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
0
    EMPTY_SWITCH_DEFAULT_CASE()
469
0
  }
470
0
}
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
0
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \
484
0
    && 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
0
  if (UNEXPECTED(Z_TYPE_P(op2) == IS_OBJECT) \
492
0
    && UNEXPECTED(Z_OBJ_HANDLER_P(op2, do_operation)) \
493
0
    && 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
0
  ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode) \
499
0
  else \
500
0
  ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode)
501
502
#define ZEND_TRY_UNARY_OBJECT_OPERATION(opcode) \
503
0
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \
504
0
    && UNEXPECTED(Z_OBJ_HANDLER_P(op1, do_operation)) \
505
0
    && 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
0
  do {                               \
511
0
    if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {           \
512
0
      bool failed;                      \
513
0
      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
0
      ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode);       \
521
0
      op1_lval = zendi_try_get_long(op1, &failed);        \
522
0
      if (UNEXPECTED(failed)) {                 \
523
0
        zend_binop_error(sigil, op1, op2);            \
524
0
        if (result != op1) {                 \
525
0
          ZVAL_UNDEF(result);                 \
526
0
        }                           \
527
0
        return FAILURE;                     \
528
0
      }                             \
529
0
    } else {                           \
530
0
      op1_lval = Z_LVAL_P(op1);                  \
531
0
    }                               \
532
0
  } while (0);                            \
533
0
  do {                               \
534
0
    if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {           \
535
0
      bool failed;                      \
536
0
      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
0
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode);       \
544
0
      op2_lval = zendi_try_get_long(op2, &failed);        \
545
0
      if (UNEXPECTED(failed)) {                 \
546
0
        zend_binop_error(sigil, op1, op2);            \
547
0
        if (result != op1) {                 \
548
0
          ZVAL_UNDEF(result);                 \
549
0
        }                           \
550
0
        return FAILURE;                     \
551
0
      }                             \
552
0
    } else {                           \
553
0
      op2_lval = Z_LVAL_P(op2);                  \
554
0
    }                               \
555
0
  } while (0);
556
557
ZEND_API void ZEND_FASTCALL convert_to_long(zval *op) /* {{{ */
558
0
{
559
0
  zend_long tmp;
560
561
0
try_again:
562
0
  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
0
    case IS_LONG:
576
0
      break;
577
0
    case IS_DOUBLE: {
578
      /* NAN might emit a warning */
579
0
      zend_long new_value = zend_dval_to_lval(Z_DVAL_P(op));
580
0
      zval_ptr_dtor(op);
581
0
      ZVAL_LONG(op, new_value);
582
0
      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
0
    EMPTY_SWITCH_DEFAULT_CASE()
614
0
  }
615
0
}
616
/* }}} */
617
618
ZEND_API void ZEND_FASTCALL convert_to_double(zval *op) /* {{{ */
619
0
{
620
0
  double tmp;
621
622
0
try_again:
623
0
  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
0
    case IS_LONG:
638
0
      ZVAL_DOUBLE(op, (double) Z_LVAL_P(op));
639
0
      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
0
    EMPTY_SWITCH_DEFAULT_CASE()
673
0
  }
674
0
}
675
/* }}} */
676
677
ZEND_API void ZEND_FASTCALL convert_to_null(zval *op) /* {{{ */
678
0
{
679
0
  if (UNEXPECTED(Z_TYPE_P(op) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op)))) {
680
0
    zend_nan_coerced_to_type_warning(IS_NULL);
681
0
  }
682
0
  zval_ptr_dtor(op);
683
0
  ZVAL_NULL(op);
684
0
}
685
/* }}} */
686
687
ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op) /* {{{ */
688
0
{
689
0
  bool tmp;
690
691
0
try_again:
692
0
  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
0
    case IS_DOUBLE: {
710
      /* We compute the new value before emitting the warning as the zval may change */
711
0
      bool new_value = Z_DVAL_P(op) ? true : false;
712
0
      if (UNEXPECTED(zend_isnan(Z_DVAL_P(op)))) {
713
0
        zend_nan_coerced_to_type_warning(_IS_BOOL);
714
0
        zval_ptr_dtor(op);
715
0
      }
716
0
      ZVAL_BOOL(op, new_value);
717
0
      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
0
    EMPTY_SWITCH_DEFAULT_CASE()
755
0
  }
756
0
}
757
/* }}} */
758
759
ZEND_API void ZEND_FASTCALL _convert_to_string(zval *op) /* {{{ */
760
780k
{
761
780k
try_again:
762
780k
  switch (Z_TYPE_P(op)) {
763
0
    case IS_UNDEF:
764
0
    case IS_NULL:
765
0
    case IS_FALSE: {
766
0
      ZVAL_EMPTY_STRING(op);
767
0
      break;
768
0
    }
769
0
    case IS_TRUE:
770
0
      ZVAL_CHAR(op, '1');
771
0
      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
0
    }
780
780k
    case IS_LONG:
781
780k
      ZVAL_STR(op, zend_long_to_str(Z_LVAL_P(op)));
782
780k
      break;
783
0
    case IS_DOUBLE: {
784
      /* Casting NAN will cause a warning */
785
0
      zend_string *new_value = zend_double_to_str(Z_DVAL_P(op));
786
0
      zval_ptr_dtor(op);
787
0
      ZVAL_NEW_STR(op, new_value);
788
0
      break;
789
0
    }
790
0
    case IS_ARRAY:
791
0
      zend_error(E_WARNING, "Array to string conversion");
792
0
      zval_ptr_dtor(op);
793
0
      ZVAL_INTERNED_STR(op, ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED));
794
0
      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
780k
    EMPTY_SWITCH_DEFAULT_CASE()
813
780k
  }
814
780k
}
815
/* }}} */
816
817
ZEND_API bool ZEND_FASTCALL _try_convert_to_string(zval *op) /* {{{ */
818
0
{
819
0
  zend_string *str;
820
821
0
  ZEND_ASSERT(Z_TYPE_P(op) != IS_STRING);
822
0
  str = zval_try_get_string_func(op);
823
0
  if (UNEXPECTED(!str)) {
824
0
    return 0;
825
0
  }
826
0
  zval_ptr_dtor(op);
827
0
  ZVAL_STR(op, str);
828
0
  return 1;
829
0
}
830
/* }}} */
831
832
static void convert_scalar_to_array(zval *op) /* {{{ */
833
0
{
834
0
  if (UNEXPECTED(Z_TYPE_P(op) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op)))) {
835
0
    zend_nan_coerced_to_type_warning(IS_ARRAY);
836
0
  }
837
0
  HashTable *ht = zend_new_array(1);
838
0
  zend_hash_index_add_new(ht, 0, op);
839
0
  ZVAL_ARR(op, ht);
840
0
}
841
/* }}} */
842
843
ZEND_API void ZEND_FASTCALL convert_to_array(zval *op) /* {{{ */
844
0
{
845
0
try_again:
846
0
  switch (Z_TYPE_P(op)) {
847
0
    case IS_ARRAY:
848
0
      break;
849
/* OBJECTS_OPTIMIZE */
850
0
    case IS_OBJECT:
851
0
      if (Z_OBJCE_P(op) == zend_ce_closure) {
852
0
        convert_scalar_to_array(op);
853
0
      } else if (ZEND_STD_BUILD_OBJECT_PROPERTIES_ARRAY_COMPATIBLE(op)) {
854
        /* Optimized version without rebuilding properties HashTable */
855
0
        HashTable *ht = zend_std_build_object_properties_array(Z_OBJ_P(op));
856
0
        OBJ_RELEASE(Z_OBJ_P(op));
857
0
        ZVAL_ARR(op, ht);
858
0
      } else {
859
0
        HashTable *obj_ht = zend_get_properties_for(op, ZEND_PROP_PURPOSE_ARRAY_CAST);
860
0
        if (obj_ht) {
861
0
          HashTable *new_obj_ht = zend_proptable_to_symtable(obj_ht,
862
0
            (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
0
          zval_ptr_dtor(op);
866
0
          ZVAL_ARR(op, new_obj_ht);
867
0
          zend_release_properties(obj_ht);
868
0
        } else {
869
0
          zval_ptr_dtor(op);
870
          /*ZVAL_EMPTY_ARRAY(op);*/
871
0
          array_init(op);
872
0
        }
873
0
      }
874
0
      break;
875
0
    case IS_NULL:
876
      /*ZVAL_EMPTY_ARRAY(op);*/
877
0
      array_init(op);
878
0
      break;
879
0
    case IS_REFERENCE:
880
0
      zend_unwrap_reference(op);
881
0
      goto try_again;
882
0
    default:
883
0
      convert_scalar_to_array(op);
884
0
      break;
885
0
  }
886
0
}
887
/* }}} */
888
889
ZEND_API void ZEND_FASTCALL convert_to_object(zval *op) /* {{{ */
890
0
{
891
0
try_again:
892
0
  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
0
    case IS_DOUBLE:
920
0
      if (UNEXPECTED(zend_isnan(Z_DVAL_P(op)))) {
921
0
        zend_nan_coerced_to_type_warning(IS_OBJECT);
922
0
      }
923
0
      ZEND_FALLTHROUGH;
924
0
    default: {
925
0
      zval tmp;
926
0
      ZVAL_COPY_VALUE(&tmp, op);
927
0
      object_init(op);
928
0
      zend_hash_add_new(Z_OBJPROP_P(op), ZSTR_KNOWN(ZEND_STR_SCALAR), &tmp);
929
0
      break;
930
0
    }
931
0
  }
932
0
}
933
/* }}} */
934
935
ZEND_API void ZEND_COLD zend_incompatible_double_to_long_error(double d)
936
0
{
937
0
  zend_error_unchecked(E_DEPRECATED, "Implicit conversion from float %.*H to int loses precision", -1, d);
938
0
}
939
ZEND_API void ZEND_COLD zend_incompatible_string_to_long_error(const zend_string *s)
940
0
{
941
0
  zend_error(E_DEPRECATED, "Implicit conversion from float-string \"%s\" to int loses precision", ZSTR_VAL(s));
942
0
}
943
ZEND_API void ZEND_COLD zend_oob_double_to_long_error(double d)
944
8.15k
{
945
8.15k
  zend_error_unchecked(E_WARNING, "The float %.*H is not representable as an int, cast occurred", -1, d);
946
8.15k
}
947
ZEND_API void ZEND_COLD zend_oob_string_to_long_error(const zend_string *s)
948
2.56k
{
949
2.56k
  zend_error_unchecked(E_WARNING, "The float-string \"%s\" is not representable as an int, cast occurred", ZSTR_VAL(s));
950
2.56k
}
951
952
ZEND_API void ZEND_COLD zend_nan_coerced_to_type_warning(uint8_t type)
953
0
{
954
0
  zend_error(E_WARNING, "unexpected NAN value was coerced to %s", zend_get_type_by_const(type));
955
0
}
956
957
ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(const zval *op, bool is_strict) /* {{{ */
958
15.4k
{
959
15.4k
try_again:
960
15.4k
  switch (Z_TYPE_P(op)) {
961
0
    case IS_UNDEF:
962
0
    case IS_NULL:
963
69
    case IS_FALSE:
964
69
      return 0;
965
2
    case IS_TRUE:
966
2
      return 1;
967
0
    case IS_RESOURCE:
968
0
      return Z_RES_HANDLE_P(op);
969
0
    case IS_LONG:
970
0
      return Z_LVAL_P(op);
971
4.89k
    case IS_DOUBLE: {
972
4.89k
      double dval = Z_DVAL_P(op);
973
4.89k
      zend_long lval = zend_dval_to_lval(dval);
974
4.89k
      if (UNEXPECTED(is_strict)) {
975
0
        if (!zend_is_long_compatible(dval, lval)) {
976
0
          zend_incompatible_double_to_long_error(dval);
977
0
        }
978
0
      }
979
4.89k
      return lval;
980
0
    }
981
10.4k
    case IS_STRING:
982
10.4k
      {
983
10.4k
        uint8_t type;
984
10.4k
        zend_long lval;
985
10.4k
        double dval;
986
10.4k
        if (0 == (type = is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval, true))) {
987
441
          return 0;
988
10.0k
        } else if (EXPECTED(type == IS_LONG)) {
989
7.36k
          return lval;
990
7.36k
        } 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
2.67k
          lval = zend_dval_to_lval_cap(dval, Z_STR_P(op));
998
2.67k
          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
2.67k
          return lval;
1004
2.67k
        }
1005
10.4k
      }
1006
3
    case IS_ARRAY:
1007
3
      return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1 : 0;
1008
2
    case IS_OBJECT:
1009
2
      {
1010
2
        zval dst;
1011
2
        convert_object_to_type(op, &dst, IS_LONG);
1012
2
        if (Z_TYPE(dst) == IS_LONG) {
1013
0
          return Z_LVAL(dst);
1014
2
        } else {
1015
2
          return 1;
1016
2
        }
1017
2
      }
1018
0
    case IS_REFERENCE:
1019
0
      op = Z_REFVAL_P(op);
1020
0
      goto try_again;
1021
15.4k
    EMPTY_SWITCH_DEFAULT_CASE()
1022
15.4k
  }
1023
0
  return 0;
1024
15.4k
}
1025
/* }}} */
1026
1027
ZEND_API double ZEND_FASTCALL zval_get_double_func(const zval *op) /* {{{ */
1028
8.02k
{
1029
15.9k
try_again:
1030
15.9k
  switch (Z_TYPE_P(op)) {
1031
0
    case IS_NULL:
1032
21
    case IS_FALSE:
1033
21
      return 0.0;
1034
10
    case IS_TRUE:
1035
10
      return 1.0;
1036
0
    case IS_RESOURCE:
1037
0
      return (double) Z_RES_HANDLE_P(op);
1038
330
    case IS_LONG:
1039
330
      return (double) Z_LVAL_P(op);
1040
3.11k
    case IS_DOUBLE:
1041
3.11k
      return Z_DVAL_P(op);
1042
637
    case IS_STRING:
1043
637
      return zend_strtod(Z_STRVAL_P(op), NULL);
1044
3.53k
    case IS_ARRAY:
1045
3.53k
      return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1.0 : 0.0;
1046
381
    case IS_OBJECT:
1047
381
      {
1048
381
        zval dst;
1049
381
        convert_object_to_type(op, &dst, IS_DOUBLE);
1050
1051
381
        if (Z_TYPE(dst) == IS_DOUBLE) {
1052
0
          return Z_DVAL(dst);
1053
381
        } else {
1054
381
          return 1.0;
1055
381
        }
1056
381
      }
1057
7.97k
    case IS_REFERENCE:
1058
7.97k
      op = Z_REFVAL_P(op);
1059
7.97k
      goto try_again;
1060
15.9k
    EMPTY_SWITCH_DEFAULT_CASE()
1061
15.9k
  }
1062
0
  return 0.0;
1063
15.9k
}
1064
/* }}} */
1065
1066
static zend_always_inline zend_string* __zval_get_string_func(zval *op, bool try) /* {{{ */
1067
371
{
1068
371
try_again:
1069
371
  switch (Z_TYPE_P(op)) {
1070
0
    case IS_UNDEF:
1071
1
    case IS_NULL:
1072
1
    case IS_FALSE:
1073
1
      return ZSTR_EMPTY_ALLOC();
1074
1
    case IS_TRUE:
1075
1
      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
94
    case IS_LONG:
1079
94
      return zend_long_to_str(Z_LVAL_P(op));
1080
275
    case IS_DOUBLE:
1081
275
      return zend_double_to_str(Z_DVAL_P(op));
1082
0
    case IS_ARRAY:
1083
0
      zend_error(E_WARNING, "Array to string conversion");
1084
0
      return (try && UNEXPECTED(EG(exception))) ?
1085
0
        NULL : ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
1086
0
    case IS_OBJECT: {
1087
0
      zval tmp;
1088
0
      if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &tmp, IS_STRING) == SUCCESS) {
1089
0
        return Z_STR(tmp);
1090
0
      }
1091
0
      if (!EG(exception)) {
1092
0
        zend_throw_error(NULL, "Object of class %s could not be converted to string", ZSTR_VAL(Z_OBJCE_P(op)->name));
1093
0
      }
1094
0
      return try ? NULL : ZSTR_EMPTY_ALLOC();
1095
0
    }
1096
0
    case IS_REFERENCE:
1097
0
      op = Z_REFVAL_P(op);
1098
0
      goto try_again;
1099
0
    case IS_STRING:
1100
0
      return zend_string_copy(Z_STR_P(op));
1101
371
    EMPTY_SWITCH_DEFAULT_CASE()
1102
371
  }
1103
0
  return NULL;
1104
371
}
1105
/* }}} */
1106
1107
ZEND_API zend_string* ZEND_FASTCALL zval_get_string_func(zval *op) /* {{{ */
1108
371
{
1109
371
  return __zval_get_string_func(op, false);
1110
371
}
1111
/* }}} */
1112
1113
ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_func(zval *op) /* {{{ */
1114
0
{
1115
0
  return __zval_get_string_func(op, true);
1116
0
}
1117
/* }}} */
1118
1119
0
static ZEND_COLD zend_never_inline void ZEND_FASTCALL zend_binop_error(const char *operator, zval *op1, zval *op2) /* {{{ */ {
1120
0
  if (EG(exception)) {
1121
0
    return;
1122
0
  }
1123
1124
0
  zend_type_error("Unsupported operand types: %s %s %s",
1125
0
    zend_zval_type_name(op1), operator, zend_zval_type_name(op2));
1126
0
}
1127
/* }}} */
1128
1129
static zend_never_inline void ZEND_FASTCALL add_function_array(zval *result, zval *op1, zval *op2) /* {{{ */
1130
0
{
1131
0
  if (result == op1 && Z_ARR_P(op1) == Z_ARR_P(op2)) {
1132
    /* $a += $a */
1133
0
    return;
1134
0
  }
1135
0
  if (result != op1) {
1136
0
    ZVAL_ARR(result, zend_array_dup(Z_ARR_P(op1)));
1137
0
  } else {
1138
0
    SEPARATE_ARRAY(result);
1139
0
  }
1140
0
  zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0);
1141
0
}
1142
/* }}} */
1143
1144
static zend_always_inline zend_result add_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1145
0
{
1146
0
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1147
1148
0
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1149
0
    fast_long_add_function(result, op1, op2);
1150
0
    return SUCCESS;
1151
0
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1152
0
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) + Z_DVAL_P(op2));
1153
0
    return SUCCESS;
1154
0
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1155
0
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) + Z_DVAL_P(op2));
1156
0
    return SUCCESS;
1157
0
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1158
0
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) + ((double)Z_LVAL_P(op2)));
1159
0
    return SUCCESS;
1160
0
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_ARRAY, IS_ARRAY))) {
1161
0
    add_function_array(result, op1, op2);
1162
0
    return SUCCESS;
1163
0
  } else {
1164
0
    return FAILURE;
1165
0
  }
1166
0
} /* }}} */
1167
1168
static zend_never_inline zend_result ZEND_FASTCALL add_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1169
0
{
1170
0
  ZVAL_DEREF(op1);
1171
0
  ZVAL_DEREF(op2);
1172
0
  if (add_function_fast(result, op1, op2) == SUCCESS) {
1173
0
    return SUCCESS;
1174
0
  }
1175
1176
0
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_ADD);
1177
1178
0
  zval op1_copy, op2_copy;
1179
0
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1180
0
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1181
0
    zend_binop_error("+", op1, op2);
1182
0
    if (result != op1) {
1183
0
      ZVAL_UNDEF(result);
1184
0
    }
1185
0
    return FAILURE;
1186
0
  }
1187
1188
0
  if (result == op1) {
1189
0
    zval_ptr_dtor(result);
1190
0
  }
1191
1192
0
  if (add_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1193
0
    return SUCCESS;
1194
0
  }
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
0
{
1202
0
  if (add_function_fast(result, op1, op2) == SUCCESS) {
1203
0
    return SUCCESS;
1204
0
  } else {
1205
0
    return add_function_slow(result, op1, op2);
1206
0
  }
1207
0
}
1208
/* }}} */
1209
1210
static zend_always_inline zend_result sub_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1211
0
{
1212
0
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1213
1214
0
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1215
0
    fast_long_sub_function(result, op1, op2);
1216
0
    return SUCCESS;
1217
0
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1218
0
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) - Z_DVAL_P(op2));
1219
0
    return SUCCESS;
1220
0
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1221
0
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) - Z_DVAL_P(op2));
1222
0
    return SUCCESS;
1223
0
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1224
0
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) - ((double)Z_LVAL_P(op2)));
1225
0
    return SUCCESS;
1226
0
  } else {
1227
0
    return FAILURE;
1228
0
  }
1229
0
}
1230
/* }}} */
1231
1232
static zend_never_inline zend_result ZEND_FASTCALL sub_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1233
0
{
1234
0
  ZVAL_DEREF(op1);
1235
0
  ZVAL_DEREF(op2);
1236
0
  if (sub_function_fast(result, op1, op2) == SUCCESS) {
1237
0
    return SUCCESS;
1238
0
  }
1239
1240
0
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SUB);
1241
1242
0
  zval op1_copy, op2_copy;
1243
0
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1244
0
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1245
0
    zend_binop_error("-", op1, op2);
1246
0
    if (result != op1) {
1247
0
      ZVAL_UNDEF(result);
1248
0
    }
1249
0
    return FAILURE;
1250
0
  }
1251
1252
0
  if (result == op1) {
1253
0
    zval_ptr_dtor(result);
1254
0
  }
1255
1256
0
  if (sub_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1257
0
    return SUCCESS;
1258
0
  }
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
0
{
1267
0
  if (sub_function_fast(result, op1, op2) == SUCCESS) {
1268
0
    return SUCCESS;
1269
0
  } else {
1270
0
    return sub_function_slow(result, op1, op2);
1271
0
  }
1272
0
}
1273
/* }}} */
1274
1275
static zend_always_inline zend_result mul_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
1276
0
{
1277
0
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1278
1279
0
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1280
0
    zend_long overflow;
1281
0
    ZEND_SIGNED_MULTIPLY_LONG(
1282
0
      Z_LVAL_P(op1), Z_LVAL_P(op2),
1283
0
      Z_LVAL_P(result), Z_DVAL_P(result), overflow);
1284
0
    Z_TYPE_INFO_P(result) = overflow ? IS_DOUBLE : IS_LONG;
1285
0
    return SUCCESS;
1286
0
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1287
0
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) * Z_DVAL_P(op2));
1288
0
    return SUCCESS;
1289
0
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1290
0
    ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) * Z_DVAL_P(op2));
1291
0
    return SUCCESS;
1292
0
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1293
0
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) * ((double)Z_LVAL_P(op2)));
1294
0
    return SUCCESS;
1295
0
  } else {
1296
0
    return FAILURE;
1297
0
  }
1298
0
}
1299
/* }}} */
1300
1301
static zend_never_inline zend_result ZEND_FASTCALL mul_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
1302
0
{
1303
0
  ZVAL_DEREF(op1);
1304
0
  ZVAL_DEREF(op2);
1305
0
  if (mul_function_fast(result, op1, op2) == SUCCESS) {
1306
0
    return SUCCESS;
1307
0
  }
1308
1309
0
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_MUL);
1310
1311
0
  zval op1_copy, op2_copy;
1312
0
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1313
0
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1314
0
    zend_binop_error("*", op1, op2);
1315
0
    if (result != op1) {
1316
0
      ZVAL_UNDEF(result);
1317
0
    }
1318
0
    return FAILURE;
1319
0
  }
1320
1321
0
  if (result == op1) {
1322
0
    zval_ptr_dtor(result);
1323
0
  }
1324
1325
0
  if (mul_function_fast(result, &op1_copy, &op2_copy) == SUCCESS) {
1326
0
    return SUCCESS;
1327
0
  }
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
0
{
1336
0
  if (mul_function_fast(result, op1, op2) == SUCCESS) {
1337
0
    return SUCCESS;
1338
0
  } else {
1339
0
    return mul_function_slow(result, op1, op2);
1340
0
  }
1341
0
}
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
0
{
1351
0
  if (UNEXPECTED(base == 0.0 && exponent < 0.0)) {
1352
0
    zend_power_base_0_exponent_lt_0_error();
1353
0
  }
1354
1355
0
  return pow(base, exponent);
1356
0
}
1357
1358
static zend_result ZEND_FASTCALL pow_function_base(zval *result, zval *op1, zval *op2) /* {{{ */
1359
0
{
1360
0
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1361
1362
0
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1363
0
    if (Z_LVAL_P(op2) >= 0) {
1364
0
      zend_long l1 = 1, l2 = Z_LVAL_P(op1), i = Z_LVAL_P(op2);
1365
1366
0
      if (i == 0) {
1367
0
        ZVAL_LONG(result, 1L);
1368
0
        return SUCCESS;
1369
0
      } else if (l2 == 0) {
1370
0
        ZVAL_LONG(result, 0);
1371
0
        return SUCCESS;
1372
0
      }
1373
1374
0
      while (i >= 1) {
1375
0
        zend_long overflow;
1376
0
        double dval = 0.0;
1377
1378
0
        if (i % 2) {
1379
0
          --i;
1380
0
          ZEND_SIGNED_MULTIPLY_LONG(l1, l2, l1, dval, overflow);
1381
0
          if (overflow) {
1382
0
            ZVAL_DOUBLE(result, dval * safe_pow(l2, i));
1383
0
            return SUCCESS;
1384
0
          }
1385
0
        } else {
1386
0
          i /= 2;
1387
0
          ZEND_SIGNED_MULTIPLY_LONG(l2, l2, l2, dval, overflow);
1388
0
          if (overflow) {
1389
0
            ZVAL_DOUBLE(result, (double)l1 * safe_pow(dval, i));
1390
0
            return SUCCESS;
1391
0
          }
1392
0
        }
1393
0
      }
1394
      /* i == 0 */
1395
0
      ZVAL_LONG(result, l1);
1396
0
    } else {
1397
0
      ZVAL_DOUBLE(result, safe_pow((double)Z_LVAL_P(op1), (double)Z_LVAL_P(op2)));
1398
0
    }
1399
0
    return SUCCESS;
1400
0
  } 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
0
  } 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
0
  } 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
0
  } else {
1410
0
    return FAILURE;
1411
0
  }
1412
0
}
1413
/* }}} */
1414
1415
ZEND_API zend_result ZEND_FASTCALL pow_function(zval *result, zval *op1, zval *op2) /* {{{ */
1416
0
{
1417
0
  ZVAL_DEREF(op1);
1418
0
  ZVAL_DEREF(op2);
1419
0
  if (pow_function_base(result, op1, op2) == SUCCESS) {
1420
0
    return SUCCESS;
1421
0
  }
1422
1423
0
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_POW);
1424
1425
0
  zval op1_copy, op2_copy;
1426
0
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1427
0
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1428
0
    zend_binop_error("**", op1, op2);
1429
0
    if (result != op1) {
1430
0
      ZVAL_UNDEF(result);
1431
0
    }
1432
0
    return FAILURE;
1433
0
  }
1434
1435
0
  if (result == op1) {
1436
0
    zval_ptr_dtor(result);
1437
0
  }
1438
1439
0
  if (pow_function_base(result, &op1_copy, &op2_copy) == SUCCESS) {
1440
0
    return SUCCESS;
1441
0
  }
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
0
{
1456
0
  uint8_t type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
1457
1458
0
  if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
1459
0
    if (Z_LVAL_P(op2) == 0) {
1460
0
      return DIV_BY_ZERO;
1461
0
    } 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
0
    if (Z_LVAL_P(op1) % Z_LVAL_P(op2) == 0) { /* integer */
1467
0
      ZVAL_LONG(result, Z_LVAL_P(op1) / Z_LVAL_P(op2));
1468
0
    } else {
1469
0
      ZVAL_DOUBLE(result, ((double) Z_LVAL_P(op1)) / Z_LVAL_P(op2));
1470
0
    }
1471
0
    return DIV_SUCCESS;
1472
0
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
1473
0
    if (Z_DVAL_P(op2) == 0) {
1474
0
      return DIV_BY_ZERO;
1475
0
    }
1476
0
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) / Z_DVAL_P(op2));
1477
0
    return DIV_SUCCESS;
1478
0
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
1479
0
    if (Z_LVAL_P(op2) == 0) {
1480
0
      return DIV_BY_ZERO;
1481
0
    }
1482
0
    ZVAL_DOUBLE(result, Z_DVAL_P(op1) / (double)Z_LVAL_P(op2));
1483
0
    return DIV_SUCCESS;
1484
0
  } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
1485
0
    if (Z_DVAL_P(op2) == 0) {
1486
0
      return DIV_BY_ZERO;
1487
0
    }
1488
0
    ZVAL_DOUBLE(result, (double)Z_LVAL_P(op1) / Z_DVAL_P(op2));
1489
0
    return DIV_SUCCESS;
1490
0
  } else {
1491
0
    return DIV_TYPES_NOT_HANDLED;
1492
0
  }
1493
0
}
1494
/* }}} */
1495
1496
ZEND_API zend_result ZEND_FASTCALL div_function(zval *result, zval *op1, zval *op2) /* {{{ */
1497
0
{
1498
0
  ZVAL_DEREF(op1);
1499
0
  ZVAL_DEREF(op2);
1500
1501
0
  zend_div_status retval = div_function_base(result, op1, op2);
1502
0
  if (EXPECTED(retval == DIV_SUCCESS)) {
1503
0
    return SUCCESS;
1504
0
  }
1505
1506
0
  if (UNEXPECTED(retval == DIV_BY_ZERO)) {
1507
0
    goto div_by_zero;
1508
0
  }
1509
1510
0
  ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_DIV);
1511
1512
0
  zval result_copy, op1_copy, op2_copy;
1513
0
  if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
1514
0
      || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {
1515
0
    zend_binop_error("/", op1, op2);
1516
0
    if (result != op1) {
1517
0
      ZVAL_UNDEF(result);
1518
0
    }
1519
0
    return FAILURE;
1520
0
  }
1521
1522
0
  retval = div_function_base(&result_copy, &op1_copy, &op2_copy);
1523
0
  if (retval == DIV_SUCCESS) {
1524
0
    if (result == op1) {
1525
0
      zval_ptr_dtor(result);
1526
0
    }
1527
0
    ZVAL_COPY_VALUE(result, &result_copy);
1528
0
    return SUCCESS;
1529
0
  }
1530
1531
0
div_by_zero:
1532
0
  ZEND_ASSERT(retval == DIV_BY_ZERO && "DIV_TYPES_NOT_HANDLED should not occur here");
1533
0
  if (result != op1) {
1534
0
    ZVAL_UNDEF(result);
1535
0
  }
1536
0
  zend_throw_error(zend_ce_division_by_zero_error, "Division by zero");
1537
0
  return FAILURE;
1538
0
}
1539
/* }}} */
1540
1541
ZEND_API zend_result ZEND_FASTCALL mod_function(zval *result, zval *op1, zval *op2) /* {{{ */
1542
0
{
1543
0
  zend_long op1_lval, op2_lval;
1544
1545
0
  convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_MOD, "%");
1546
1547
0
  if (op2_lval == 0) {
1548
    /* modulus by zero */
1549
0
    if (EG(current_execute_data) && !CG(in_compilation)) {
1550
0
      zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
1551
0
    } else {
1552
0
      zend_error_noreturn(E_ERROR, "Modulo by zero");
1553
0
    }
1554
0
    if (op1 != result) {
1555
0
      ZVAL_UNDEF(result);
1556
0
    }
1557
0
    return FAILURE;
1558
0
  }
1559
1560
0
  if (op1 == result) {
1561
0
    zval_ptr_dtor(result);
1562
0
  }
1563
1564
0
  if (op2_lval == -1) {
1565
    /* Prevent overflow error/crash if op1==LONG_MIN */
1566
0
    ZVAL_LONG(result, 0);
1567
0
    return SUCCESS;
1568
0
  }
1569
1570
0
  ZVAL_LONG(result, op1_lval % op2_lval);
1571
0
  return SUCCESS;
1572
0
}
1573
/* }}} */
1574
1575
ZEND_API zend_result ZEND_FASTCALL boolean_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */
1576
0
{
1577
0
  int op1_val, op2_val;
1578
1579
0
  do {
1580
0
    if (Z_TYPE_P(op1) == IS_FALSE) {
1581
0
      op1_val = 0;
1582
0
    } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
1583
0
      op1_val = 1;
1584
0
    } else {
1585
0
      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
0
      ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BOOL_XOR);
1596
0
      op1_val = zend_is_true(op1);
1597
0
    }
1598
0
  } while (0);
1599
0
  do {
1600
0
    if (Z_TYPE_P(op2) == IS_FALSE) {
1601
0
      op2_val = 0;
1602
0
    } else if (EXPECTED(Z_TYPE_P(op2) == IS_TRUE)) {
1603
0
      op2_val = 1;
1604
0
    } else {
1605
0
      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
0
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BOOL_XOR);
1616
0
      op2_val = zend_is_true(op2);
1617
0
    }
1618
0
  } while (0);
1619
1620
0
  ZVAL_BOOL(result, op1_val ^ op2_val);
1621
0
  return SUCCESS;
1622
0
}
1623
/* }}} */
1624
1625
ZEND_API zend_result ZEND_FASTCALL boolean_not_function(zval *result, zval *op1) /* {{{ */
1626
0
{
1627
0
  if (Z_TYPE_P(op1) < IS_TRUE) {
1628
0
    ZVAL_TRUE(result);
1629
0
  } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
1630
0
    ZVAL_FALSE(result);
1631
0
  } else {
1632
0
    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
0
    ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BOOL_NOT);
1643
1644
0
    ZVAL_BOOL(result, !zend_is_true(op1));
1645
0
  }
1646
0
  return SUCCESS;
1647
0
}
1648
/* }}} */
1649
1650
ZEND_API zend_result ZEND_FASTCALL bitwise_not_function(zval *result, zval *op1) /* {{{ */
1651
0
{
1652
0
try_again:
1653
0
  switch (Z_TYPE_P(op1)) {
1654
0
    case IS_LONG:
1655
0
      ZVAL_LONG(result, ~Z_LVAL_P(op1));
1656
0
      return SUCCESS;
1657
0
    case IS_DOUBLE: {
1658
0
      zend_long lval = zend_dval_to_lval_safe(Z_DVAL_P(op1));
1659
0
      if (EG(exception)) {
1660
0
        if (result != op1) {
1661
0
          ZVAL_UNDEF(result);
1662
0
        }
1663
0
        return FAILURE;
1664
0
      }
1665
0
      ZVAL_LONG(result, ~lval);
1666
0
      return SUCCESS;
1667
0
    }
1668
0
    case IS_STRING: {
1669
0
      size_t i;
1670
1671
0
      if (Z_STRLEN_P(op1) == 1) {
1672
0
        zend_uchar not = (zend_uchar) ~*Z_STRVAL_P(op1);
1673
0
        ZVAL_CHAR(result, not);
1674
0
      } else {
1675
0
        ZVAL_NEW_STR(result, zend_string_alloc(Z_STRLEN_P(op1), 0));
1676
0
        for (i = 0; i < Z_STRLEN_P(op1); i++) {
1677
0
          Z_STRVAL_P(result)[i] = ~Z_STRVAL_P(op1)[i];
1678
0
        }
1679
0
        Z_STRVAL_P(result)[i] = 0;
1680
0
      }
1681
0
      return SUCCESS;
1682
0
    }
1683
0
    case IS_REFERENCE:
1684
0
      op1 = Z_REFVAL_P(op1);
1685
0
      goto try_again;
1686
0
    default:
1687
0
      ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BW_NOT);
1688
1689
0
      if (result != op1) {
1690
0
        ZVAL_UNDEF(result);
1691
0
      }
1692
0
      zend_type_error("Cannot perform bitwise not on %s", zend_zval_value_name(op1));
1693
0
      return FAILURE;
1694
0
  }
1695
0
}
1696
/* }}} */
1697
1698
ZEND_API zend_result ZEND_FASTCALL bitwise_or_function(zval *result, zval *op1, zval *op2) /* {{{ */
1699
0
{
1700
0
  zend_long op1_lval, op2_lval;
1701
1702
0
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1703
0
    ZVAL_LONG(result, Z_LVAL_P(op1) | Z_LVAL_P(op2));
1704
0
    return SUCCESS;
1705
0
  }
1706
1707
0
  ZVAL_DEREF(op1);
1708
0
  ZVAL_DEREF(op2);
1709
1710
0
  if (Z_TYPE_P(op1) == IS_STRING && EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
1711
0
    zval *longer, *shorter;
1712
0
    zend_string *str;
1713
0
    size_t i;
1714
1715
0
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1716
0
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1717
0
        zend_uchar or = (zend_uchar) (*Z_STRVAL_P(op1) | *Z_STRVAL_P(op2));
1718
0
        if (result==op1) {
1719
0
          zval_ptr_dtor_str(result);
1720
0
        }
1721
0
        ZVAL_CHAR(result, or);
1722
0
        return SUCCESS;
1723
0
      }
1724
0
      longer = op1;
1725
0
      shorter = op2;
1726
0
    } else {
1727
0
      longer = op2;
1728
0
      shorter = op1;
1729
0
    }
1730
1731
0
    str = zend_string_alloc(Z_STRLEN_P(longer), 0);
1732
0
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1733
0
      ZSTR_VAL(str)[i] = Z_STRVAL_P(longer)[i] | Z_STRVAL_P(shorter)[i];
1734
0
    }
1735
0
    memcpy(ZSTR_VAL(str) + i, Z_STRVAL_P(longer) + i, Z_STRLEN_P(longer) - i + 1);
1736
0
    if (result==op1) {
1737
0
      zval_ptr_dtor_str(result);
1738
0
    }
1739
0
    ZVAL_NEW_STR(result, str);
1740
0
    return SUCCESS;
1741
0
  }
1742
1743
0
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1744
0
    bool failed;
1745
0
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_OR);
1746
0
    op1_lval = zendi_try_get_long(op1, &failed);
1747
0
    if (UNEXPECTED(failed)) {
1748
0
      zend_binop_error("|", op1, op2);
1749
0
      if (result != op1) {
1750
0
        ZVAL_UNDEF(result);
1751
0
      }
1752
0
      return FAILURE;
1753
0
    }
1754
0
  } else {
1755
0
    op1_lval = Z_LVAL_P(op1);
1756
0
  }
1757
0
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1758
0
    bool failed;
1759
0
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_OR);
1760
0
    op2_lval = zendi_try_get_long(op2, &failed);
1761
0
    if (UNEXPECTED(failed)) {
1762
0
      zend_binop_error("|", op1, op2);
1763
0
      if (result != op1) {
1764
0
        ZVAL_UNDEF(result);
1765
0
      }
1766
0
      return FAILURE;
1767
0
    }
1768
0
  } else {
1769
0
    op2_lval = Z_LVAL_P(op2);
1770
0
  }
1771
1772
0
  if (op1 == result) {
1773
0
    zval_ptr_dtor(result);
1774
0
  }
1775
0
  ZVAL_LONG(result, op1_lval | op2_lval);
1776
0
  return SUCCESS;
1777
0
}
1778
/* }}} */
1779
1780
ZEND_API zend_result ZEND_FASTCALL bitwise_and_function(zval *result, zval *op1, zval *op2) /* {{{ */
1781
0
{
1782
0
  zend_long op1_lval, op2_lval;
1783
1784
0
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1785
0
    ZVAL_LONG(result, Z_LVAL_P(op1) & Z_LVAL_P(op2));
1786
0
    return SUCCESS;
1787
0
  }
1788
1789
0
  ZVAL_DEREF(op1);
1790
0
  ZVAL_DEREF(op2);
1791
1792
0
  if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
1793
0
    zval *longer, *shorter;
1794
0
    zend_string *str;
1795
0
    size_t i;
1796
1797
0
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1798
0
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1799
0
        zend_uchar and = (zend_uchar) (*Z_STRVAL_P(op1) & *Z_STRVAL_P(op2));
1800
0
        if (result==op1) {
1801
0
          zval_ptr_dtor_str(result);
1802
0
        }
1803
0
        ZVAL_CHAR(result, and);
1804
0
        return SUCCESS;
1805
0
      }
1806
0
      longer = op1;
1807
0
      shorter = op2;
1808
0
    } else {
1809
0
      longer = op2;
1810
0
      shorter = op1;
1811
0
    }
1812
1813
0
    str = zend_string_alloc(Z_STRLEN_P(shorter), 0);
1814
0
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1815
0
      ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] & Z_STRVAL_P(longer)[i];
1816
0
    }
1817
0
    ZSTR_VAL(str)[i] = 0;
1818
0
    if (result==op1) {
1819
0
      zval_ptr_dtor_str(result);
1820
0
    }
1821
0
    ZVAL_NEW_STR(result, str);
1822
0
    return SUCCESS;
1823
0
  }
1824
1825
0
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1826
0
    bool failed;
1827
0
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_AND);
1828
0
    op1_lval = zendi_try_get_long(op1, &failed);
1829
0
    if (UNEXPECTED(failed)) {
1830
0
      zend_binop_error("&", op1, op2);
1831
0
      if (result != op1) {
1832
0
        ZVAL_UNDEF(result);
1833
0
      }
1834
0
      return FAILURE;
1835
0
    }
1836
0
  } else {
1837
0
    op1_lval = Z_LVAL_P(op1);
1838
0
  }
1839
0
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1840
0
    bool failed;
1841
0
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_AND);
1842
0
    op2_lval = zendi_try_get_long(op2, &failed);
1843
0
    if (UNEXPECTED(failed)) {
1844
0
      zend_binop_error("&", op1, op2);
1845
0
      if (result != op1) {
1846
0
        ZVAL_UNDEF(result);
1847
0
      }
1848
0
      return FAILURE;
1849
0
    }
1850
0
  } else {
1851
0
    op2_lval = Z_LVAL_P(op2);
1852
0
  }
1853
1854
0
  if (op1 == result) {
1855
0
    zval_ptr_dtor(result);
1856
0
  }
1857
0
  ZVAL_LONG(result, op1_lval & op2_lval);
1858
0
  return SUCCESS;
1859
0
}
1860
/* }}} */
1861
1862
ZEND_API zend_result ZEND_FASTCALL bitwise_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */
1863
0
{
1864
0
  zend_long op1_lval, op2_lval;
1865
1866
0
  if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
1867
0
    ZVAL_LONG(result, Z_LVAL_P(op1) ^ Z_LVAL_P(op2));
1868
0
    return SUCCESS;
1869
0
  }
1870
1871
0
  ZVAL_DEREF(op1);
1872
0
  ZVAL_DEREF(op2);
1873
1874
0
  if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
1875
0
    zval *longer, *shorter;
1876
0
    zend_string *str;
1877
0
    size_t i;
1878
1879
0
    if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
1880
0
      if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
1881
0
        zend_uchar xor = (zend_uchar) (*Z_STRVAL_P(op1) ^ *Z_STRVAL_P(op2));
1882
0
        if (result==op1) {
1883
0
          zval_ptr_dtor_str(result);
1884
0
        }
1885
0
        ZVAL_CHAR(result, xor);
1886
0
        return SUCCESS;
1887
0
      }
1888
0
      longer = op1;
1889
0
      shorter = op2;
1890
0
    } else {
1891
0
      longer = op2;
1892
0
      shorter = op1;
1893
0
    }
1894
1895
0
    str = zend_string_alloc(Z_STRLEN_P(shorter), 0);
1896
0
    for (i = 0; i < Z_STRLEN_P(shorter); i++) {
1897
0
      ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] ^ Z_STRVAL_P(longer)[i];
1898
0
    }
1899
0
    ZSTR_VAL(str)[i] = 0;
1900
0
    if (result==op1) {
1901
0
      zval_ptr_dtor_str(result);
1902
0
    }
1903
0
    ZVAL_NEW_STR(result, str);
1904
0
    return SUCCESS;
1905
0
  }
1906
1907
0
  if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
1908
0
    bool failed;
1909
0
    ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_XOR);
1910
0
    op1_lval = zendi_try_get_long(op1, &failed);
1911
0
    if (UNEXPECTED(failed)) {
1912
0
      zend_binop_error("^", op1, op2);
1913
0
      if (result != op1) {
1914
0
        ZVAL_UNDEF(result);
1915
0
      }
1916
0
      return FAILURE;
1917
0
    }
1918
0
  } else {
1919
0
    op1_lval = Z_LVAL_P(op1);
1920
0
  }
1921
0
  if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
1922
0
    bool failed;
1923
0
    ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_XOR);
1924
0
    op2_lval = zendi_try_get_long(op2, &failed);
1925
0
    if (UNEXPECTED(failed)) {
1926
0
      zend_binop_error("^", op1, op2);
1927
0
      if (result != op1) {
1928
0
        ZVAL_UNDEF(result);
1929
0
      }
1930
0
      return FAILURE;
1931
0
    }
1932
0
  } else {
1933
0
    op2_lval = Z_LVAL_P(op2);
1934
0
  }
1935
1936
0
  if (op1 == result) {
1937
0
    zval_ptr_dtor(result);
1938
0
  }
1939
0
  ZVAL_LONG(result, op1_lval ^ op2_lval);
1940
0
  return SUCCESS;
1941
0
}
1942
/* }}} */
1943
1944
ZEND_API zend_result ZEND_FASTCALL shift_left_function(zval *result, zval *op1, zval *op2) /* {{{ */
1945
0
{
1946
0
  zend_long op1_lval, op2_lval;
1947
1948
0
  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
0
  if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
1952
0
    if (EXPECTED(op2_lval > 0)) {
1953
0
      if (op1 == result) {
1954
0
        zval_ptr_dtor(result);
1955
0
      }
1956
0
      ZVAL_LONG(result, 0);
1957
0
      return SUCCESS;
1958
0
    } else {
1959
0
      if (EG(current_execute_data) && !CG(in_compilation)) {
1960
0
        zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number");
1961
0
      } else {
1962
0
        zend_error_noreturn(E_ERROR, "Bit shift by negative number");
1963
0
      }
1964
0
      if (op1 != result) {
1965
0
        ZVAL_UNDEF(result);
1966
0
      }
1967
0
      return FAILURE;
1968
0
    }
1969
0
  }
1970
1971
0
  if (op1 == result) {
1972
0
    zval_ptr_dtor(result);
1973
0
  }
1974
1975
  /* Perform shift on unsigned numbers to get well-defined wrap behavior. */
1976
0
  ZVAL_LONG(result, (zend_long) ((zend_ulong) op1_lval << op2_lval));
1977
0
  return SUCCESS;
1978
0
}
1979
/* }}} */
1980
1981
ZEND_API zend_result ZEND_FASTCALL shift_right_function(zval *result, zval *op1, zval *op2) /* {{{ */
1982
0
{
1983
0
  zend_long op1_lval, op2_lval;
1984
1985
0
  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
0
  if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
1989
0
    if (EXPECTED(op2_lval > 0)) {
1990
0
      if (op1 == result) {
1991
0
        zval_ptr_dtor(result);
1992
0
      }
1993
0
      ZVAL_LONG(result, (op1_lval < 0) ? -1 : 0);
1994
0
      return SUCCESS;
1995
0
    } else {
1996
0
      if (EG(current_execute_data) && !CG(in_compilation)) {
1997
0
        zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number");
1998
0
      } else {
1999
0
        zend_error_noreturn(E_ERROR, "Bit shift by negative number");
2000
0
      }
2001
0
      if (op1 != result) {
2002
0
        ZVAL_UNDEF(result);
2003
0
      }
2004
0
      return FAILURE;
2005
0
    }
2006
0
  }
2007
2008
0
  if (op1 == result) {
2009
0
    zval_ptr_dtor(result);
2010
0
  }
2011
2012
0
  ZVAL_LONG(result, op1_lval >> op2_lval);
2013
0
  return SUCCESS;
2014
0
}
2015
/* }}} */
2016
2017
ZEND_API zend_result ZEND_FASTCALL concat_function(zval *result, zval *op1, zval *op2) /* {{{ */
2018
0
{
2019
0
  zval *orig_op1 = op1;
2020
0
  zend_string *op1_string, *op2_string;
2021
0
  bool free_op1_string = false;
2022
0
  bool free_op2_string = false;
2023
2024
0
  do {
2025
0
    if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) {
2026
0
      op1_string = Z_STR_P(op1);
2027
0
    } else {
2028
0
      if (Z_ISREF_P(op1)) {
2029
0
        op1 = Z_REFVAL_P(op1);
2030
0
        if (Z_TYPE_P(op1) == IS_STRING) {
2031
0
          op1_string = Z_STR_P(op1);
2032
0
          break;
2033
0
        }
2034
0
      }
2035
0
      ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_CONCAT);
2036
0
      op1_string = zval_try_get_string_func(op1);
2037
0
      if (UNEXPECTED(!op1_string)) {
2038
0
        if (orig_op1 != result) {
2039
0
          ZVAL_UNDEF(result);
2040
0
        }
2041
0
        return FAILURE;
2042
0
      }
2043
0
      free_op1_string = true;
2044
0
      if (result == op1) {
2045
0
        if (UNEXPECTED(op1 == op2)) {
2046
0
          op2_string = op1_string;
2047
0
          goto has_op2_string;
2048
0
        }
2049
0
      }
2050
0
    }
2051
0
  } while (0);
2052
0
  do {
2053
0
    if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2054
0
      op2_string = Z_STR_P(op2);
2055
0
    } else {
2056
0
      if (Z_ISREF_P(op2)) {
2057
0
        op2 = Z_REFVAL_P(op2);
2058
0
        if (Z_TYPE_P(op2) == IS_STRING) {
2059
0
          op2_string = Z_STR_P(op2);
2060
0
          break;
2061
0
        }
2062
0
      }
2063
      /* hold an additional reference because a userland function could free this */
2064
0
      if (!free_op1_string) {
2065
0
        op1_string = zend_string_copy(op1_string);
2066
0
        free_op1_string = true;
2067
0
      }
2068
0
      ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_CONCAT);
2069
0
      op2_string = zval_try_get_string_func(op2);
2070
0
      if (UNEXPECTED(!op2_string)) {
2071
0
        zend_string_release_ex(op1_string, false);
2072
0
        if (orig_op1 != result) {
2073
0
          ZVAL_UNDEF(result);
2074
0
        }
2075
0
        return FAILURE;
2076
0
      }
2077
0
      free_op2_string = true;
2078
0
    }
2079
0
  } while (0);
2080
2081
0
has_op2_string:;
2082
0
  if (UNEXPECTED(ZSTR_LEN(op1_string) == 0)) {
2083
0
    if (EXPECTED(result != op2 || Z_TYPE_P(result) != IS_STRING)) {
2084
0
      if (result == orig_op1) {
2085
0
        i_zval_ptr_dtor(result);
2086
0
      }
2087
0
      if (free_op2_string) {
2088
        /* transfer ownership of op2_string */
2089
0
        ZVAL_STR(result, op2_string);
2090
0
        free_op2_string = false;
2091
0
      } else {
2092
0
        ZVAL_STR_COPY(result, op2_string);
2093
0
      }
2094
0
    }
2095
0
  } else if (UNEXPECTED(ZSTR_LEN(op2_string) == 0)) {
2096
0
    if (EXPECTED(result != op1 || Z_TYPE_P(result) != IS_STRING)) {
2097
0
      if (result == orig_op1) {
2098
0
        i_zval_ptr_dtor(result);
2099
0
      }
2100
0
      if (free_op1_string) {
2101
        /* transfer ownership of op1_string */
2102
0
        ZVAL_STR(result, op1_string);
2103
0
        free_op1_string = false;
2104
0
      } else {
2105
0
        ZVAL_STR_COPY(result, op1_string);
2106
0
      }
2107
0
    }
2108
0
  } else {
2109
0
    size_t op1_len = ZSTR_LEN(op1_string);
2110
0
    size_t op2_len = ZSTR_LEN(op2_string);
2111
0
    size_t result_len = op1_len + op2_len;
2112
0
    zend_string *result_str;
2113
0
    uint32_t flags = ZSTR_GET_COPYABLE_CONCAT_PROPERTIES_BOTH(op1_string, op2_string);
2114
2115
0
    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
0
    if (result == op1) {
2126
      /* Destroy the old result first to drop the refcount, such that $x .= ...; may happen in-place. */
2127
0
      if (free_op1_string) {
2128
        /* op1_string will be used as the result, so we should not free it */
2129
0
        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
0
        ZVAL_NULL(result);
2133
0
        free_op1_string = false;
2134
0
      }
2135
      /* special case, perform operations on result */
2136
0
      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
0
      if (op1_string == op2_string) {
2139
0
        if (free_op2_string) {
2140
0
          zend_string_release_ex(op2_string, false);
2141
0
          free_op2_string = false;
2142
0
        }
2143
0
        op2_string = result_str;
2144
0
      }
2145
0
    } else {
2146
0
      result_str = zend_string_alloc(result_len, 0);
2147
0
      memcpy(ZSTR_VAL(result_str), ZSTR_VAL(op1_string), op1_len);
2148
0
      if (result == orig_op1) {
2149
0
        i_zval_ptr_dtor(result);
2150
0
      }
2151
0
    }
2152
0
    GC_ADD_FLAGS(result_str, flags);
2153
2154
0
    ZVAL_NEW_STR(result, result_str);
2155
0
    memcpy(ZSTR_VAL(result_str) + op1_len, ZSTR_VAL(op2_string), op2_len);
2156
0
    ZSTR_VAL(result_str)[result_len] = '\0';
2157
0
  }
2158
2159
0
  if (free_op1_string) zend_string_release_ex(op1_string, false);
2160
0
  if (free_op2_string) zend_string_release_ex(op2_string, false);
2161
2162
0
  return SUCCESS;
2163
0
}
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
0
{
2187
0
  if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) &&
2188
0
      EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
2189
0
    if (Z_STR_P(op1) == Z_STR_P(op2)) {
2190
0
      return 0;
2191
0
    } else {
2192
0
      return zend_binary_strcmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
2193
0
    }
2194
0
  } else {
2195
0
    zend_string *tmp_str1, *tmp_str2;
2196
0
    zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2197
0
    zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2198
0
    int ret = zend_binary_strcmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2));
2199
2200
0
    zend_tmp_string_release(tmp_str1);
2201
0
    zend_tmp_string_release(tmp_str2);
2202
0
    return ret;
2203
0
  }
2204
0
}
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
0
{
2231
0
  zend_string *tmp_str1, *tmp_str2;
2232
0
  zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
2233
0
  zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
2234
0
  int ret = strcoll(ZSTR_VAL(str1), ZSTR_VAL(str2));
2235
2236
0
  zend_tmp_string_release(tmp_str1);
2237
0
  zend_tmp_string_release(tmp_str2);
2238
0
  return ret;
2239
0
}
2240
/* }}} */
2241
2242
ZEND_API int ZEND_FASTCALL numeric_compare_function(zval *op1, zval *op2) /* {{{ */
2243
0
{
2244
0
  double d1, d2;
2245
2246
0
  d1 = zval_get_double(op1);
2247
0
  d2 = zval_get_double(op2);
2248
2249
0
  return ZEND_THREEWAY_COMPARE(d1, d2);
2250
0
}
2251
/* }}} */
2252
2253
ZEND_API zend_result ZEND_FASTCALL compare_function(zval *result, zval *op1, zval *op2) /* {{{ */
2254
0
{
2255
0
  ZVAL_LONG(result, zend_compare(op1, op2));
2256
0
  return SUCCESS;
2257
0
}
2258
/* }}} */
2259
2260
static int compare_long_to_string(zend_long lval, zend_string *str) /* {{{ */
2261
0
{
2262
0
  zend_long str_lval;
2263
0
  double str_dval;
2264
0
  uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &str_lval, &str_dval, 0);
2265
2266
0
  if (type == IS_LONG) {
2267
0
    return lval > str_lval ? 1 : lval < str_lval ? -1 : 0;
2268
0
  }
2269
2270
0
  if (type == IS_DOUBLE) {
2271
0
    return ZEND_THREEWAY_COMPARE((double) lval, str_dval);
2272
0
  }
2273
2274
0
  zend_string *lval_as_str = zend_long_to_str(lval);
2275
0
  int cmp_result = zend_binary_strcmp(
2276
0
    ZSTR_VAL(lval_as_str), ZSTR_LEN(lval_as_str), ZSTR_VAL(str), ZSTR_LEN(str));
2277
0
  zend_string_release(lval_as_str);
2278
0
  return ZEND_NORMALIZE_BOOL(cmp_result);
2279
0
}
2280
/* }}} */
2281
2282
static int compare_double_to_string(double dval, zend_string *str) /* {{{ */
2283
0
{
2284
0
  zend_long str_lval;
2285
0
  double str_dval;
2286
0
  uint8_t type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &str_lval, &str_dval, 0);
2287
2288
0
  ZEND_ASSERT(!zend_isnan(dval));
2289
2290
0
  if (type == IS_LONG) {
2291
0
    return ZEND_THREEWAY_COMPARE(dval, (double) str_lval);
2292
0
  }
2293
2294
0
  if (type == IS_DOUBLE) {
2295
0
    return ZEND_THREEWAY_COMPARE(dval, str_dval);
2296
0
  }
2297
2298
0
  zend_string *dval_as_str = zend_double_to_str(dval);
2299
0
  int cmp_result = zend_binary_strcmp(
2300
0
    ZSTR_VAL(dval_as_str), ZSTR_LEN(dval_as_str), ZSTR_VAL(str), ZSTR_LEN(str));
2301
0
  zend_string_release(dval_as_str);
2302
0
  return ZEND_NORMALIZE_BOOL(cmp_result);
2303
0
}
2304
/* }}} */
2305
2306
ZEND_API int ZEND_FASTCALL zend_compare(zval *op1, zval *op2) /* {{{ */
2307
0
{
2308
0
  bool converted = false;
2309
0
  zval op1_copy, op2_copy;
2310
2311
0
  while (1) {
2312
0
    switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
2313
0
      case TYPE_PAIR(IS_LONG, IS_LONG):
2314
0
        return Z_LVAL_P(op1)>Z_LVAL_P(op2)?1:(Z_LVAL_P(op1)<Z_LVAL_P(op2)?-1:0);
2315
2316
0
      case TYPE_PAIR(IS_DOUBLE, IS_LONG):
2317
0
        return ZEND_THREEWAY_COMPARE(Z_DVAL_P(op1), (double)Z_LVAL_P(op2));
2318
2319
0
      case TYPE_PAIR(IS_LONG, IS_DOUBLE):
2320
0
        return ZEND_THREEWAY_COMPARE((double)Z_LVAL_P(op1), Z_DVAL_P(op2));
2321
2322
0
      case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
2323
0
        return ZEND_THREEWAY_COMPARE(Z_DVAL_P(op1), Z_DVAL_P(op2));
2324
2325
0
      case TYPE_PAIR(IS_ARRAY, IS_ARRAY):
2326
0
        return zend_compare_arrays(op1, op2);
2327
2328
0
      case TYPE_PAIR(IS_NULL, IS_NULL):
2329
0
      case TYPE_PAIR(IS_NULL, IS_FALSE):
2330
0
      case TYPE_PAIR(IS_FALSE, IS_NULL):
2331
0
      case TYPE_PAIR(IS_FALSE, IS_FALSE):
2332
0
      case TYPE_PAIR(IS_TRUE, IS_TRUE):
2333
0
        return 0;
2334
2335
0
      case TYPE_PAIR(IS_NULL, IS_TRUE):
2336
0
        return -1;
2337
2338
0
      case TYPE_PAIR(IS_TRUE, IS_NULL):
2339
0
        return 1;
2340
2341
0
      case TYPE_PAIR(IS_STRING, IS_STRING):
2342
0
        if (Z_STR_P(op1) == Z_STR_P(op2)) {
2343
0
          return 0;
2344
0
        }
2345
0
        return zendi_smart_strcmp(Z_STR_P(op1), Z_STR_P(op2));
2346
2347
0
      case TYPE_PAIR(IS_NULL, IS_STRING):
2348
0
        return Z_STRLEN_P(op2) == 0 ? 0 : -1;
2349
2350
0
      case TYPE_PAIR(IS_STRING, IS_NULL):
2351
0
        return Z_STRLEN_P(op1) == 0 ? 0 : 1;
2352
2353
0
      case TYPE_PAIR(IS_LONG, IS_STRING):
2354
0
        return compare_long_to_string(Z_LVAL_P(op1), Z_STR_P(op2));
2355
2356
0
      case TYPE_PAIR(IS_STRING, IS_LONG):
2357
0
        return -compare_long_to_string(Z_LVAL_P(op2), Z_STR_P(op1));
2358
2359
0
      case TYPE_PAIR(IS_DOUBLE, IS_STRING):
2360
0
        if (zend_isnan(Z_DVAL_P(op1))) {
2361
0
          return 1;
2362
0
        }
2363
2364
0
        return compare_double_to_string(Z_DVAL_P(op1), Z_STR_P(op2));
2365
2366
0
      case TYPE_PAIR(IS_STRING, IS_DOUBLE):
2367
0
        if (zend_isnan(Z_DVAL_P(op2))) {
2368
0
          return 1;
2369
0
        }
2370
2371
0
        return -compare_double_to_string(Z_DVAL_P(op2), Z_STR_P(op1));
2372
2373
0
      case TYPE_PAIR(IS_OBJECT, IS_NULL):
2374
0
        return 1;
2375
2376
0
      case TYPE_PAIR(IS_NULL, IS_OBJECT):
2377
0
        return -1;
2378
2379
0
      default:
2380
0
        if (Z_ISREF_P(op1)) {
2381
0
          op1 = Z_REFVAL_P(op1);
2382
0
          continue;
2383
0
        } else if (Z_ISREF_P(op2)) {
2384
0
          op2 = Z_REFVAL_P(op2);
2385
0
          continue;
2386
0
        }
2387
2388
0
        if (Z_TYPE_P(op1) == IS_OBJECT
2389
0
         || Z_TYPE_P(op2) == IS_OBJECT) {
2390
0
          zval *object, *other;
2391
0
          if (Z_TYPE_P(op1) == IS_OBJECT) {
2392
0
            object = op1;
2393
0
            other = op2;
2394
0
          } else {
2395
0
            object = op2;
2396
0
            other = op1;
2397
0
          }
2398
0
          if (EXPECTED(Z_TYPE_P(other) == IS_OBJECT)) {
2399
0
            if (Z_OBJ_P(object) == Z_OBJ_P(other)) {
2400
0
              return 0;
2401
0
            }
2402
0
          } else if (Z_TYPE_P(other) == IS_TRUE || Z_TYPE_P(other) == IS_FALSE) {
2403
0
            zval casted;
2404
0
            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
0
            int ret = object == op1 ? zend_compare(&casted, other) : zend_compare(other, &casted);
2408
0
            ZEND_ASSERT(!Z_REFCOUNTED_P(&casted));
2409
0
            return ret;
2410
0
          }
2411
0
          return Z_OBJ_HANDLER_P(object, compare)(op1, op2);
2412
0
        }
2413
2414
0
        if (!converted) {
2415
          /* Handle NAN */
2416
0
          if (UNEXPECTED(
2417
0
            (Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))
2418
0
            || (Z_TYPE_P(op2) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op2)))
2419
0
          )) {
2420
            // TODO: NAN should always be uncomparable
2421
            /* NAN used be cast to TRUE so handle this manually for the time being */
2422
0
            if (Z_TYPE_P(op1) < IS_TRUE) {
2423
0
              return -1;
2424
0
            } else if (Z_TYPE_P(op1) == IS_TRUE || Z_TYPE_P(op2) == IS_TRUE) {
2425
0
              return 0;
2426
0
            } else if (Z_TYPE_P(op2) < IS_TRUE) {
2427
0
              return 1;
2428
0
            } else if (Z_TYPE_P(op1) != IS_DOUBLE) {
2429
0
              op1 = _zendi_convert_scalar_to_number_silent(op1, &op1_copy);
2430
0
              converted = true;
2431
0
            } else if (Z_TYPE_P(op2) != IS_DOUBLE) {
2432
0
              op2 = _zendi_convert_scalar_to_number_silent(op2, &op2_copy);
2433
0
              converted = true;
2434
0
            }
2435
0
          } else if (Z_TYPE_P(op1) < IS_TRUE) {
2436
0
            return zend_is_true(op2) ? -1 : 0;
2437
0
          } else if (Z_TYPE_P(op1) == IS_TRUE) {
2438
0
            return zend_is_true(op2) ? 0 : 1;
2439
0
          } else if (Z_TYPE_P(op2) < IS_TRUE) {
2440
0
            return zend_is_true(op1) ? 1 : 0;
2441
0
          } else if (Z_TYPE_P(op2) == IS_TRUE) {
2442
0
            return zend_is_true(op1) ? 0 : -1;
2443
0
          } else {
2444
0
            op1 = _zendi_convert_scalar_to_number_silent(op1, &op1_copy);
2445
0
            op2 = _zendi_convert_scalar_to_number_silent(op2, &op2_copy);
2446
0
            if (EG(exception)) {
2447
0
              return 1; /* to stop comparison of arrays */
2448
0
            }
2449
0
            converted = true;
2450
0
          }
2451
0
        } else if (Z_TYPE_P(op1)==IS_ARRAY) {
2452
0
          return 1;
2453
0
        } else if (Z_TYPE_P(op2)==IS_ARRAY) {
2454
0
          return -1;
2455
0
        } else {
2456
0
          ZEND_UNREACHABLE();
2457
0
          zend_throw_error(NULL, "Unsupported operand types");
2458
0
          return 1;
2459
0
        }
2460
0
    }
2461
0
  }
2462
0
}
2463
/* }}} */
2464
2465
/* return int to be compatible with compare_func_t */
2466
static int hash_zval_identical_function(zval *z1, zval *z2) /* {{{ */
2467
0
{
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
0
  ZVAL_DEREF(z1);
2474
0
  ZVAL_DEREF(z2);
2475
0
  return fast_is_not_identical_function(z1, z2);
2476
0
}
2477
/* }}} */
2478
2479
ZEND_API bool ZEND_FASTCALL zend_is_identical(const zval *op1, const zval *op2) /* {{{ */
2480
0
{
2481
0
  if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
2482
0
    return 0;
2483
0
  }
2484
0
  switch (Z_TYPE_P(op1)) {
2485
0
    case IS_NULL:
2486
0
    case IS_FALSE:
2487
0
    case IS_TRUE:
2488
0
      return 1;
2489
0
    case IS_LONG:
2490
0
      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
0
    case IS_DOUBLE:
2494
0
      return (Z_DVAL_P(op1) == Z_DVAL_P(op2));
2495
0
    case IS_STRING:
2496
0
      return zend_string_equals(Z_STR_P(op1), Z_STR_P(op2));
2497
0
    case IS_ARRAY:
2498
0
      return (Z_ARRVAL_P(op1) == Z_ARRVAL_P(op2) ||
2499
0
        zend_hash_compare(Z_ARRVAL_P(op1), Z_ARRVAL_P(op2), (compare_func_t) hash_zval_identical_function, 1) == 0);
2500
0
    case IS_OBJECT:
2501
0
      return (Z_OBJ_P(op1) == Z_OBJ_P(op2));
2502
0
    default:
2503
0
      return 0;
2504
0
  }
2505
0
}
2506
/* }}} */
2507
2508
ZEND_API zend_result ZEND_FASTCALL is_identical_function(zval *result, zval *op1, zval *op2) /* {{{ */
2509
0
{
2510
0
  ZVAL_BOOL(result, zend_is_identical(op1, op2));
2511
0
  return SUCCESS;
2512
0
}
2513
/* }}} */
2514
2515
ZEND_API zend_result ZEND_FASTCALL is_not_identical_function(zval *result, zval *op1, zval *op2) /* {{{ */
2516
0
{
2517
0
  ZVAL_BOOL(result, !zend_is_identical(op1, op2));
2518
0
  return SUCCESS;
2519
0
}
2520
/* }}} */
2521
2522
ZEND_API zend_result ZEND_FASTCALL is_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2523
0
{
2524
0
  ZVAL_BOOL(result, zend_compare(op1, op2) == 0);
2525
0
  return SUCCESS;
2526
0
}
2527
/* }}} */
2528
2529
ZEND_API zend_result ZEND_FASTCALL is_not_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2530
0
{
2531
0
  ZVAL_BOOL(result, (zend_compare(op1, op2) != 0));
2532
0
  return SUCCESS;
2533
0
}
2534
/* }}} */
2535
2536
ZEND_API zend_result ZEND_FASTCALL is_smaller_function(zval *result, zval *op1, zval *op2) /* {{{ */
2537
0
{
2538
0
  ZVAL_BOOL(result, (zend_compare(op1, op2) < 0));
2539
0
  return SUCCESS;
2540
0
}
2541
/* }}} */
2542
2543
ZEND_API zend_result ZEND_FASTCALL is_smaller_or_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
2544
0
{
2545
0
  ZVAL_BOOL(result, (zend_compare(op1, op2) <= 0));
2546
0
  return SUCCESS;
2547
0
}
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
96
{
2552
96
  uint32_t i;
2553
96
  ZEND_ASSERT(interface_ce->ce_flags & ZEND_ACC_INTERFACE);
2554
2555
96
  if (class_ce->num_interfaces) {
2556
92
    ZEND_ASSERT(class_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
2557
356
    for (i = 0; i < class_ce->num_interfaces; i++) {
2558
276
      if (class_ce->interfaces[i] == interface_ce) {
2559
12
        return 1;
2560
12
      }
2561
276
    }
2562
92
  }
2563
84
  return 0;
2564
96
}
2565
/* }}} */
2566
2567
ZEND_API bool ZEND_FASTCALL instanceof_function_slow(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */
2568
434k
{
2569
434k
  ZEND_ASSERT(instance_ce != ce && "Should have been checked already");
2570
434k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
2571
92.9k
    uint32_t i;
2572
2573
92.9k
    if (instance_ce->num_interfaces) {
2574
92.8k
      ZEND_ASSERT(instance_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
2575
185k
      for (i = 0; i < instance_ce->num_interfaces; i++) {
2576
185k
        if (instance_ce->interfaces[i] == ce) {
2577
92.8k
          return 1;
2578
92.8k
        }
2579
185k
      }
2580
92.8k
    }
2581
6
    return 0;
2582
342k
  } else {
2583
342k
    while (1) {
2584
342k
      instance_ce = instance_ce->parent;
2585
342k
      if (instance_ce == ce) {
2586
256
        return 1;
2587
256
      }
2588
342k
      if (instance_ce == NULL) {
2589
341k
        return 0;
2590
341k
      }
2591
342k
    }
2592
342k
  }
2593
434k
}
2594
/* }}} */
2595
2596
0
#define LOWER_CASE 1
2597
0
#define UPPER_CASE 2
2598
0
#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
0
{
2615
0
  int carry=0;
2616
0
  size_t pos=Z_STRLEN_P(str)-1;
2617
0
  char *s;
2618
0
  zend_string *t;
2619
0
  int last=0; /* Shut up the compiler warning */
2620
0
  int ch;
2621
2622
0
  zend_string *zstr = Z_STR_P(str);
2623
0
  zend_string_addref(zstr);
2624
0
  zend_error(E_DEPRECATED, "Increment on non-numeric string is deprecated, use str_increment() instead");
2625
0
  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
0
  zval_ptr_dtor(str);
2631
0
  ZVAL_STR(str, zstr);
2632
2633
0
  if (UNEXPECTED(Z_STRLEN_P(str) == 0)) {
2634
0
    zval_ptr_dtor(str);
2635
0
    ZVAL_CHAR(str, '1');
2636
0
    return true;
2637
0
  }
2638
2639
0
  if (!Z_REFCOUNTED_P(str)) {
2640
0
    Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
2641
0
    Z_TYPE_INFO_P(str) = IS_STRING_EX;
2642
0
  } else if (Z_REFCOUNT_P(str) > 1) {
2643
    /* Only release string after allocation succeeded. */
2644
0
    zend_string *orig_str = Z_STR_P(str);
2645
0
    Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
2646
0
    GC_DELREF(orig_str);
2647
0
  } else {
2648
0
    zend_string_forget_hash_val(Z_STR_P(str));
2649
0
  }
2650
0
  s = Z_STRVAL_P(str);
2651
2652
0
  do {
2653
0
    ch = s[pos];
2654
0
    if (ch >= 'a' && ch <= 'z') {
2655
0
      if (ch == 'z') {
2656
0
        s[pos] = 'a';
2657
0
        carry=1;
2658
0
      } else {
2659
0
        s[pos]++;
2660
0
        carry=0;
2661
0
      }
2662
0
      last=LOWER_CASE;
2663
0
    } else if (ch >= 'A' && ch <= 'Z') {
2664
0
      if (ch == 'Z') {
2665
0
        s[pos] = 'A';
2666
0
        carry=1;
2667
0
      } else {
2668
0
        s[pos]++;
2669
0
        carry=0;
2670
0
      }
2671
0
      last=UPPER_CASE;
2672
0
    } else if (ch >= '0' && ch <= '9') {
2673
0
      if (ch == '9') {
2674
0
        s[pos] = '0';
2675
0
        carry=1;
2676
0
      } else {
2677
0
        s[pos]++;
2678
0
        carry=0;
2679
0
      }
2680
0
      last = NUMERIC;
2681
0
    } else {
2682
0
      carry=0;
2683
0
      break;
2684
0
    }
2685
0
    if (carry == 0) {
2686
0
      break;
2687
0
    }
2688
0
  } while (pos-- > 0);
2689
2690
0
  if (carry) {
2691
0
    t = zend_string_alloc(Z_STRLEN_P(str)+1, 0);
2692
0
    memcpy(ZSTR_VAL(t) + 1, Z_STRVAL_P(str), Z_STRLEN_P(str));
2693
0
    ZSTR_VAL(t)[Z_STRLEN_P(str) + 1] = '\0';
2694
0
    switch (last) {
2695
0
      case NUMERIC:
2696
0
        ZSTR_VAL(t)[0] = '1';
2697
0
        break;
2698
0
      case UPPER_CASE:
2699
0
        ZSTR_VAL(t)[0] = 'A';
2700
0
        break;
2701
0
      case LOWER_CASE:
2702
0
        ZSTR_VAL(t)[0] = 'a';
2703
0
        break;
2704
0
    }
2705
0
    zend_string_free(Z_STR_P(str));
2706
0
    ZVAL_NEW_STR(str, t);
2707
0
  }
2708
0
  return true;
2709
0
}
2710
/* }}} */
2711
2712
ZEND_API zend_result ZEND_FASTCALL increment_function(zval *op1) /* {{{ */
2713
0
{
2714
0
try_again:
2715
0
  switch (Z_TYPE_P(op1)) {
2716
0
    case IS_LONG:
2717
0
      fast_long_increment_function(op1);
2718
0
      break;
2719
0
    case IS_DOUBLE:
2720
0
      Z_DVAL_P(op1) = Z_DVAL_P(op1) + 1;
2721
0
      break;
2722
0
    case IS_NULL:
2723
0
      ZVAL_LONG(op1, 1);
2724
0
      break;
2725
0
    case IS_STRING: {
2726
0
        zend_long lval;
2727
0
        double dval;
2728
2729
0
        switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) {
2730
0
          case IS_LONG:
2731
0
            zval_ptr_dtor_str(op1);
2732
0
            if (lval == ZEND_LONG_MAX) {
2733
              /* switch to double */
2734
0
              double d = (double)lval;
2735
0
              ZVAL_DOUBLE(op1, d+1);
2736
0
            } else {
2737
0
              ZVAL_LONG(op1, lval+1);
2738
0
            }
2739
0
            break;
2740
0
          case IS_DOUBLE:
2741
0
            zval_ptr_dtor_str(op1);
2742
0
            ZVAL_DOUBLE(op1, dval+1);
2743
0
            break;
2744
0
          default:
2745
            /* Perl style string increment */
2746
0
            increment_string(op1);
2747
0
            if (EG(exception)) {
2748
0
              return FAILURE;
2749
0
            }
2750
0
            break;
2751
0
        }
2752
0
      }
2753
0
      break;
2754
0
    case IS_FALSE:
2755
0
    case IS_TRUE: {
2756
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2757
0
      zval copy;
2758
0
      ZVAL_COPY_VALUE(&copy, op1);
2759
0
      zend_error(E_WARNING, "Increment on type bool has no effect, this will change in the next major version of PHP");
2760
0
      zval_ptr_dtor(op1);
2761
0
      ZVAL_COPY_VALUE(op1, &copy);
2762
0
      if (EG(exception)) {
2763
0
        return FAILURE;
2764
0
      }
2765
0
      break;
2766
0
    }
2767
0
    case IS_REFERENCE:
2768
0
      op1 = Z_REFVAL_P(op1);
2769
0
      goto try_again;
2770
0
    case IS_OBJECT: {
2771
0
      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
0
      zval tmp;
2779
0
      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
0
      ZEND_FALLTHROUGH;
2786
0
    }
2787
0
    case IS_RESOURCE:
2788
0
    case IS_ARRAY:
2789
0
      zend_type_error("Cannot increment %s", zend_zval_value_name(op1));
2790
0
      return FAILURE;
2791
0
    EMPTY_SWITCH_DEFAULT_CASE()
2792
0
  }
2793
0
  return SUCCESS;
2794
0
}
2795
/* }}} */
2796
2797
ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op1) /* {{{ */
2798
0
{
2799
0
  zend_long lval;
2800
0
  double dval;
2801
2802
0
try_again:
2803
0
  switch (Z_TYPE_P(op1)) {
2804
0
    case IS_LONG:
2805
0
      fast_long_decrement_function(op1);
2806
0
      break;
2807
0
    case IS_DOUBLE:
2808
0
      Z_DVAL_P(op1) = Z_DVAL_P(op1) - 1;
2809
0
      break;
2810
0
    case IS_STRING:   /* Like perl we only support string increment */
2811
0
      if (Z_STRLEN_P(op1) == 0) { /* consider as 0 */
2812
0
        zend_error(E_DEPRECATED, "Decrement on empty string is deprecated as non-numeric");
2813
0
        if (EG(exception)) {
2814
0
          return FAILURE;
2815
0
        }
2816
        /* A userland error handler can change the type from string to something else */
2817
0
        zval_ptr_dtor(op1);
2818
0
        ZVAL_LONG(op1, -1);
2819
0
        break;
2820
0
      }
2821
0
      switch (is_numeric_str_function(Z_STR_P(op1), &lval, &dval)) {
2822
0
        case IS_LONG:
2823
0
          zval_ptr_dtor_str(op1);
2824
0
          if (lval == ZEND_LONG_MIN) {
2825
0
            double d = (double)lval;
2826
0
            ZVAL_DOUBLE(op1, d-1);
2827
0
          } else {
2828
0
            ZVAL_LONG(op1, lval-1);
2829
0
          }
2830
0
          break;
2831
0
        case IS_DOUBLE:
2832
0
          zval_ptr_dtor_str(op1);
2833
0
          ZVAL_DOUBLE(op1, dval - 1);
2834
0
          break;
2835
0
        default: {
2836
          /* Error handler can unset the variable */
2837
0
          zend_string *zstr = Z_STR_P(op1);
2838
0
          zend_string_addref(zstr);
2839
0
          zend_error(E_DEPRECATED, "Decrement on non-numeric string has no effect and is deprecated");
2840
0
          if (EG(exception)) {
2841
0
            zend_string_release(zstr);
2842
0
            return FAILURE;
2843
0
          }
2844
0
          zval_ptr_dtor(op1);
2845
0
          ZVAL_STR(op1, zstr);
2846
0
        }
2847
0
      }
2848
0
      break;
2849
0
    case IS_NULL: {
2850
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2851
0
      zval copy;
2852
0
      ZVAL_COPY_VALUE(&copy, op1);
2853
0
      zend_error(E_WARNING, "Decrement on type null has no effect, this will change in the next major version of PHP");
2854
0
      zval_ptr_dtor(op1);
2855
0
      ZVAL_COPY_VALUE(op1, &copy);
2856
0
      if (EG(exception)) {
2857
0
        return FAILURE;
2858
0
      }
2859
0
      break;
2860
0
    }
2861
0
    case IS_FALSE:
2862
0
    case IS_TRUE: {
2863
      /* Error handler can undef/change type of op1, save it and reset it in case those cases */
2864
0
      zval copy;
2865
0
      ZVAL_COPY_VALUE(&copy, op1);
2866
0
      zend_error(E_WARNING, "Decrement on type bool has no effect, this will change in the next major version of PHP");
2867
0
      zval_ptr_dtor(op1);
2868
0
      ZVAL_COPY_VALUE(op1, &copy);
2869
0
      if (EG(exception)) {
2870
0
        return FAILURE;
2871
0
      }
2872
0
      break;
2873
0
    }
2874
0
    case IS_REFERENCE:
2875
0
      op1 = Z_REFVAL_P(op1);
2876
0
      goto try_again;
2877
0
    case IS_OBJECT: {
2878
0
      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
0
      zval tmp;
2886
0
      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
0
      ZEND_FALLTHROUGH;
2893
0
    }
2894
0
    case IS_RESOURCE:
2895
0
    case IS_ARRAY:
2896
0
      zend_type_error("Cannot decrement %s", zend_zval_value_name(op1));
2897
0
      return FAILURE;
2898
0
    EMPTY_SWITCH_DEFAULT_CASE()
2899
0
  }
2900
2901
0
  return SUCCESS;
2902
0
}
2903
/* }}} */
2904
2905
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op) /* {{{ */
2906
0
{
2907
0
  return i_zend_is_true(op);
2908
0
}
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
35.5k
static zend_always_inline void zend_str_tolower_impl(char *dest, const char *str, size_t length) /* {{{ */ {
3013
35.5k
  unsigned char *p = (unsigned char*)str;
3014
35.5k
  unsigned char *q = (unsigned char*)dest;
3015
35.5k
  unsigned char *end = p + length;
3016
35.5k
#ifdef HAVE_BLOCKCONV
3017
35.5k
  if (length >= BLOCKCONV_STRIDE) {
3018
35
    BLOCKCONV_INIT_RANGE('A', 'Z');
3019
35
    BLOCKCONV_INIT_DELTA('a' - 'A');
3020
6.39k
    do {
3021
6.39k
      BLOCKCONV_LOAD(p);
3022
6.39k
      BLOCKCONV_STORE(q);
3023
6.39k
      p += BLOCKCONV_STRIDE;
3024
6.39k
      q += BLOCKCONV_STRIDE;
3025
6.39k
    } while (p + BLOCKCONV_STRIDE <= end);
3026
35
  }
3027
35.5k
#endif
3028
38.3k
  while (p < end) {
3029
2.79k
    *q++ = zend_tolower_ascii(*p++);
3030
2.79k
  }
3031
35.5k
}
3032
/* }}} */
3033
3034
0
static zend_always_inline void zend_str_toupper_impl(char *dest, const char *str, size_t length) /* {{{ */ {
3035
0
  unsigned char *p = (unsigned char*)str;
3036
0
  unsigned char *q = (unsigned char*)dest;
3037
0
  unsigned char *end = p + length;
3038
0
#ifdef HAVE_BLOCKCONV
3039
0
  if (length >= BLOCKCONV_STRIDE) {
3040
0
    BLOCKCONV_INIT_RANGE('a', 'z');
3041
0
    BLOCKCONV_INIT_DELTA('A' - 'a');
3042
0
    do {
3043
0
      BLOCKCONV_LOAD(p);
3044
0
      BLOCKCONV_STORE(q);
3045
0
      p += BLOCKCONV_STRIDE;
3046
0
      q += BLOCKCONV_STRIDE;
3047
0
    } while (p + BLOCKCONV_STRIDE <= end);
3048
0
  }
3049
0
#endif
3050
0
  while (p < end) {
3051
0
    *q++ = zend_toupper_ascii(*p++);
3052
0
  }
3053
0
}
3054
/* }}} */
3055
3056
ZEND_API char* ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length) /* {{{ */
3057
163
{
3058
163
  zend_str_tolower_impl(dest, source, length);
3059
163
  dest[length] = '\0';
3060
163
  return dest;
3061
163
}
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
120
{
3074
120
  return zend_str_tolower_copy((char *)emalloc(length+1), source, length);
3075
120
}
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
0
{
3086
0
  zend_str_tolower_impl(str, (const char*)str, length);
3087
0
}
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
0
{
3099
0
  const unsigned char *p = (const unsigned char*)source;
3100
0
  const unsigned char *end = p + length;
3101
3102
0
  while (p < end) {
3103
0
    if (*p != zend_tolower_ascii(*p)) {
3104
0
      char *res = (char*)emalloc(length + 1);
3105
0
      unsigned char *r;
3106
3107
0
      if (p != (const unsigned char*)source) {
3108
0
        memcpy(res, source, p - (const unsigned char*)source);
3109
0
      }
3110
0
      r = (unsigned char*)p + (res - source);
3111
0
      zend_str_tolower_impl((char *)r, (const char*)p, end - p);
3112
0
      res[length] = '\0';
3113
0
      return res;
3114
0
    }
3115
0
    p++;
3116
0
  }
3117
0
  return NULL;
3118
0
}
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
337k
{
3147
337k
  size_t length = ZSTR_LEN(str);
3148
337k
  unsigned char *p = (unsigned char *) ZSTR_VAL(str);
3149
337k
  unsigned char *end = p + length;
3150
3151
337k
#ifdef HAVE_BLOCKCONV
3152
337k
  BLOCKCONV_INIT_RANGE('A', 'Z');
3153
338k
  while (p + BLOCKCONV_STRIDE <= end) {
3154
36.2k
    BLOCKCONV_LOAD(p);
3155
36.2k
    if (BLOCKCONV_FOUND()) {
3156
35.3k
      zend_string *res = zend_string_alloc(length, persistent);
3157
35.3k
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char *) ZSTR_VAL(str));
3158
35.3k
      unsigned char *q = (unsigned char*) ZSTR_VAL(res) + (p - (unsigned char*) ZSTR_VAL(str));
3159
3160
      /* Lowercase the chunk we already compared. */
3161
35.3k
      BLOCKCONV_INIT_DELTA('a' - 'A');
3162
35.3k
      BLOCKCONV_STORE(q);
3163
3164
      /* Lowercase the rest of the string. */
3165
35.3k
      p += BLOCKCONV_STRIDE;
3166
35.3k
      q += BLOCKCONV_STRIDE;
3167
35.3k
      zend_str_tolower_impl((char *) q, (const char *) p, end - p);
3168
35.3k
      ZSTR_VAL(res)[length] = '\0';
3169
35.3k
      return res;
3170
35.3k
    }
3171
863
    p += BLOCKCONV_STRIDE;
3172
863
  }
3173
302k
#endif
3174
3175
363k
  while (p < end) {
3176
353k
    if (*p != zend_tolower_ascii(*p)) {
3177
291k
      zend_string *res = zend_string_alloc(length, persistent);
3178
291k
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str));
3179
3180
291k
      unsigned char *q = (unsigned char*) ZSTR_VAL(res) + (p - (unsigned char*) ZSTR_VAL(str));
3181
3.38M
      while (p < end) {
3182
3.09M
        *q++ = zend_tolower_ascii(*p++);
3183
3.09M
      }
3184
291k
      ZSTR_VAL(res)[length] = '\0';
3185
291k
      return res;
3186
291k
    }
3187
61.2k
    p++;
3188
61.2k
  }
3189
3190
10.5k
  return zend_string_copy(str);
3191
302k
}
3192
/* }}} */
3193
3194
ZEND_API zend_string* ZEND_FASTCALL zend_string_toupper_ex(zend_string *str, bool persistent) /* {{{ */
3195
0
{
3196
0
  size_t length = ZSTR_LEN(str);
3197
0
  unsigned char *p = (unsigned char *) ZSTR_VAL(str);
3198
0
  unsigned char *end = p + length;
3199
3200
0
#ifdef HAVE_BLOCKCONV
3201
0
  BLOCKCONV_INIT_RANGE('a', 'z');
3202
0
  while (p + BLOCKCONV_STRIDE <= end) {
3203
0
    BLOCKCONV_LOAD(p);
3204
0
    if (BLOCKCONV_FOUND()) {
3205
0
      zend_string *res = zend_string_alloc(length, persistent);
3206
0
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char *) ZSTR_VAL(str));
3207
0
      unsigned char *q = (unsigned char *) ZSTR_VAL(res) + (p - (unsigned char *) ZSTR_VAL(str));
3208
3209
      /* Uppercase the chunk we already compared. */
3210
0
      BLOCKCONV_INIT_DELTA('A' - 'a');
3211
0
      BLOCKCONV_STORE(q);
3212
3213
      /* Uppercase the rest of the string. */
3214
0
      p += BLOCKCONV_STRIDE;
3215
0
      q += BLOCKCONV_STRIDE;
3216
0
      zend_str_toupper_impl((char *) q, (const char *) p, end - p);
3217
0
      ZSTR_VAL(res)[length] = '\0';
3218
0
      return res;
3219
0
    }
3220
0
    p += BLOCKCONV_STRIDE;
3221
0
  }
3222
0
#endif
3223
3224
0
  while (p < end) {
3225
0
    if (*p != zend_toupper_ascii(*p)) {
3226
0
      zend_string *res = zend_string_alloc(length, persistent);
3227
0
      memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str));
3228
3229
0
      unsigned char *q = (unsigned char *) ZSTR_VAL(res) + (p - (unsigned char *) ZSTR_VAL(str));
3230
0
      while (p < end) {
3231
0
        *q++ = zend_toupper_ascii(*p++);
3232
0
      }
3233
0
      ZSTR_VAL(res)[length] = '\0';
3234
0
      return res;
3235
0
    }
3236
0
    p++;
3237
0
  }
3238
3239
0
  return zend_string_copy(str);
3240
0
}
3241
/* }}} */
3242
3243
ZEND_API int ZEND_FASTCALL zend_binary_strcmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
3244
0
{
3245
0
  int retval;
3246
3247
0
  if (s1 == s2) {
3248
0
    return 0;
3249
0
  }
3250
0
  retval = memcmp(s1, s2, MIN(len1, len2));
3251
0
  if (!retval) {
3252
0
    return ZEND_THREEWAY_COMPARE(len1, len2);
3253
0
  } else {
3254
0
    return retval;
3255
0
  }
3256
0
}
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
0
{
3261
0
  int retval;
3262
3263
0
  if (s1 == s2) {
3264
0
    return 0;
3265
0
  }
3266
0
  retval = memcmp(s1, s2, MIN(length, MIN(len1, len2)));
3267
0
  if (!retval) {
3268
0
    return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2));
3269
0
  } else {
3270
0
    return retval;
3271
0
  }
3272
0
}
3273
/* }}} */
3274
3275
ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
3276
158
{
3277
158
  size_t len;
3278
158
  int c1, c2;
3279
3280
158
  if (s1 == s2) {
3281
18
    return 0;
3282
18
  }
3283
3284
140
  len = MIN(len1, len2);
3285
524
  while (len--) {
3286
484
    c1 = zend_tolower_ascii(*(unsigned char *)s1++);
3287
484
    c2 = zend_tolower_ascii(*(unsigned char *)s2++);
3288
484
    if (c1 != c2) {
3289
100
      return c1 - c2;
3290
100
    }
3291
484
  }
3292
3293
40
  return ZEND_THREEWAY_COMPARE(len1, len2);
3294
140
}
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
0
{
3299
0
  size_t len;
3300
0
  int c1, c2;
3301
3302
0
  if (s1 == s2) {
3303
0
    return 0;
3304
0
  }
3305
0
  len = MIN(length, MIN(len1, len2));
3306
0
  while (len--) {
3307
0
    c1 = zend_tolower_ascii(*(unsigned char *)s1++);
3308
0
    c2 = zend_tolower_ascii(*(unsigned char *)s2++);
3309
0
    if (c1 != c2) {
3310
0
      return c1 - c2;
3311
0
    }
3312
0
  }
3313
3314
0
  return ZEND_THREEWAY_COMPARE(MIN(length, len1), MIN(length, len2));
3315
0
}
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
0
{
3375
0
  uint8_t ret1, ret2;
3376
0
  int oflow1, oflow2;
3377
0
  zend_long lval1 = 0, lval2 = 0;
3378
0
  double dval1 = 0.0, dval2 = 0.0;
3379
3380
0
  if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, false, &oflow1, NULL)) &&
3381
0
    (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
0
    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
0
    if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) {
3394
0
      if (ret1 != IS_DOUBLE) {
3395
0
        if (oflow2) {
3396
          /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
3397
0
          return 0;
3398
0
        }
3399
0
        dval1 = (double) lval1;
3400
0
      } else if (ret2 != IS_DOUBLE) {
3401
0
        if (oflow1) {
3402
0
          return 0;
3403
0
        }
3404
0
        dval2 = (double) lval2;
3405
0
      } 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
0
        goto string_cmp;
3409
0
      }
3410
0
      return dval1 == dval2;
3411
0
    } else { /* they both have to be long's */
3412
0
      return lval1 == lval2;
3413
0
    }
3414
0
  } else {
3415
0
string_cmp:
3416
0
    return zend_string_equal_content(s1, s2);
3417
0
  }
3418
0
}
3419
/* }}} */
3420
3421
ZEND_API int ZEND_FASTCALL zendi_smart_strcmp(zend_string *s1, zend_string *s2) /* {{{ */
3422
0
{
3423
0
  uint8_t ret1, ret2;
3424
0
  int oflow1, oflow2;
3425
0
  zend_long lval1 = 0, lval2 = 0;
3426
0
  double dval1 = 0.0, dval2 = 0.0;
3427
3428
0
  if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, false, &oflow1, NULL)) &&
3429
0
    (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
0
    if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0.) {
3436
0
#endif
3437
      /* both values are integers overflowed to the same side, and the
3438
       * double comparison may have resulted in crucial accuracy lost */
3439
0
      goto string_cmp;
3440
0
    }
3441
0
    if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) {
3442
0
      if (ret1 != IS_DOUBLE) {
3443
0
        if (oflow2) {
3444
          /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
3445
0
          return -1 * oflow2;
3446
0
        }
3447
0
        dval1 = (double) lval1;
3448
0
      } else if (ret2 != IS_DOUBLE) {
3449
0
        if (oflow1) {
3450
0
          return oflow1;
3451
0
        }
3452
0
        dval2 = (double) lval2;
3453
0
      } 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
0
      dval1 = dval1 - dval2;
3459
0
      return ZEND_NORMALIZE_BOOL(dval1);
3460
0
    } else { /* they both have to be long's */
3461
0
      return lval1 > lval2 ? 1 : (lval1 < lval2 ? -1 : 0);
3462
0
    }
3463
0
  } else {
3464
0
    int strcmp_ret;
3465
0
string_cmp:
3466
0
    strcmp_ret = zend_binary_strcmp(s1->val, s1->len, s2->val, s2->len);
3467
0
    return ZEND_NORMALIZE_BOOL(strcmp_ret);
3468
0
  }
3469
0
}
3470
/* }}} */
3471
3472
static int hash_zval_compare_function(zval *z1, zval *z2) /* {{{ */
3473
0
{
3474
0
  return zend_compare(z1, z2);
3475
0
}
3476
/* }}} */
3477
3478
ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable *ht2) /* {{{ */
3479
0
{
3480
0
  if (ht1 == ht2) {
3481
0
    return 0;
3482
0
  }
3483
3484
0
  GC_TRY_ADDREF(ht1);
3485
0
  GC_TRY_ADDREF(ht2);
3486
3487
0
  int ret = zend_hash_compare(ht1, ht2, (compare_func_t) hash_zval_compare_function, 0);
3488
3489
0
  GC_TRY_DTOR_NO_REF(ht1);
3490
0
  GC_TRY_DTOR_NO_REF(ht2);
3491
3492
0
  return ret;
3493
0
}
3494
/* }}} */
3495
3496
ZEND_API int ZEND_FASTCALL zend_compare_arrays(zval *a1, zval *a2) /* {{{ */
3497
0
{
3498
0
  return zend_compare_symbol_tables(Z_ARRVAL_P(a1), Z_ARRVAL_P(a2));
3499
0
}
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
780k
{
3518
780k
  if ((zend_ulong)num <= 9) {
3519
728k
    return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num);
3520
728k
  } else {
3521
52.0k
    char buf[MAX_LENGTH_OF_LONG + 1];
3522
52.0k
    char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
3523
52.0k
    zend_string *str =  zend_string_init(res, buf + sizeof(buf) - 1 - res, 0);
3524
52.0k
    GC_ADD_FLAGS(str, IS_STR_VALID_UTF8);
3525
52.0k
    return str;
3526
52.0k
  }
3527
780k
}
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
275
{
3602
275
  char buf[ZEND_DOUBLE_MAX_LENGTH];
3603
  /* Model snprintf precision behavior. */
3604
275
  int precision = (int) EG(precision);
3605
275
  zend_gcvt(num, precision ? precision : 1, '.', 'E', buf);
3606
275
  zend_string *str =  zend_string_init(buf, strlen(buf), 0);
3607
275
  if (UNEXPECTED(zend_isnan(num))) {
3608
0
    zend_nan_coerced_to_type_warning(IS_STRING);
3609
0
  }
3610
275
  GC_ADD_FLAGS(str, IS_STR_VALID_UTF8);
3611
275
  return str;
3612
275
}
3613
3614
ZEND_API uint8_t ZEND_FASTCALL is_numeric_str_function(const zend_string *str, zend_long *lval, double *dval) /* {{{ */
3615
0
{
3616
0
  return is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), lval, dval, false);
3617
0
}
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
10.4k
{
3623
10.4k
  const char *ptr;
3624
10.4k
  int digits = 0, dp_or_e = 0;
3625
10.4k
  double local_dval = 0.0;
3626
10.4k
  uint8_t type;
3627
10.4k
  zend_ulong tmp_lval = 0;
3628
10.4k
  int neg = 0;
3629
3630
10.4k
  if (!length) {
3631
1
    return 0;
3632
1
  }
3633
3634
10.4k
  if (oflow_info != NULL) {
3635
0
    *oflow_info = 0;
3636
0
  }
3637
10.4k
  if (trailing_data != NULL) {
3638
0
    *trailing_data = false;
3639
0
  }
3640
3641
  /* Skip any whitespace
3642
   * This is much faster than the isspace() function */
3643
12.2k
  while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\v' || *str == '\f') {
3644
1.85k
    str++;
3645
1.85k
    length--;
3646
1.85k
  }
3647
10.4k
  ptr = str;
3648
3649
10.4k
  if (*ptr == '-') {
3650
9.80k
    neg = 1;
3651
9.80k
    ptr++;
3652
9.80k
  } else if (*ptr == '+') {
3653
90
    ptr++;
3654
90
  }
3655
3656
10.4k
  if (ZEND_IS_DIGIT(*ptr)) {
3657
    /* Skip any leading 0s */
3658
59.0k
    while (*ptr == '0') {
3659
49.0k
      ptr++;
3660
49.0k
    }
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
75.5k
    for (type = IS_LONG; !(digits >= MAX_LENGTH_OF_LONG && (dval || allow_errors)); digits++, ptr++) {
3666
75.5k
check_digits:
3667
75.5k
      if (ZEND_IS_DIGIT(*ptr)) {
3668
65.5k
        tmp_lval = tmp_lval * 10 + (*ptr) - '0';
3669
65.5k
        continue;
3670
65.5k
      } else if (*ptr == '.' && dp_or_e < 1) {
3671
17
        goto process_double;
3672
9.91k
      } else if ((*ptr == 'e' || *ptr == 'E') && dp_or_e < 2) {
3673
5.61k
        const char *e = ptr + 1;
3674
3675
5.61k
        if (*e == '-' || *e == '+') {
3676
17
          ptr = e++;
3677
17
        }
3678
5.61k
        if (ZEND_IS_DIGIT(*e)) {
3679
1.31k
          goto process_double;
3680
1.31k
        }
3681
5.61k
      }
3682
3683
8.60k
      break;
3684
75.5k
    }
3685
3686
8.61k
    if (digits >= MAX_LENGTH_OF_LONG) {
3687
10
      if (oflow_info != NULL) {
3688
0
        *oflow_info = *str == '-' ? -1 : 1;
3689
0
      }
3690
10
      dp_or_e = -1;
3691
10
      goto process_double;
3692
10
    }
3693
8.61k
  } else if (*ptr == '.' && ZEND_IS_DIGIT(ptr[1])) {
3694
1.42k
process_double:
3695
1.42k
    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
1.42k
    if (dval) {
3700
1.42k
      local_dval = zend_strtod(str, &ptr);
3701
1.42k
    } else if (!allow_errors && dp_or_e != -1) {
3702
0
      dp_or_e = (*ptr++ == '.') ? 1 : 2;
3703
0
      goto check_digits;
3704
0
    }
3705
1.42k
  } else {
3706
403
    return 0;
3707
403
  }
3708
3709
10.0k
  if (ptr != str + length) {
3710
6.58k
    const char *endptr = ptr;
3711
8.16k
    while (*endptr == ' ' || *endptr == '\t' || *endptr == '\n' || *endptr == '\r' || *endptr == '\v' || *endptr == '\f') {
3712
1.57k
      endptr++;
3713
1.57k
      length--;
3714
1.57k
    }
3715
6.58k
    if (ptr != str + length) {
3716
6.56k
      if (!allow_errors) {
3717
0
        return 0;
3718
0
      }
3719
6.56k
      if (trailing_data != NULL) {
3720
0
        *trailing_data = true;
3721
0
      }
3722
6.56k
    }
3723
6.58k
  }
3724
3725
10.0k
  if (type == IS_LONG) {
3726
8.60k
    if (digits == MAX_LENGTH_OF_LONG - 1) {
3727
2.13k
      int cmp = strcmp(&ptr[-digits], long_min_digits);
3728
3729
2.13k
      if (!(cmp < 0 || (cmp == 0 && *str == '-'))) {
3730
1.24k
        if (dval) {
3731
1.24k
          *dval = zend_strtod(str, NULL);
3732
1.24k
        }
3733
1.24k
        if (oflow_info != NULL) {
3734
0
          *oflow_info = *str == '-' ? -1 : 1;
3735
0
        }
3736
3737
1.24k
        return IS_DOUBLE;
3738
1.24k
      }
3739
2.13k
    }
3740
3741
7.36k
    if (lval) {
3742
7.36k
      if (neg) {
3743
6.94k
        tmp_lval = -tmp_lval;
3744
6.94k
      }
3745
7.36k
      *lval = (zend_long) tmp_lval;
3746
7.36k
    }
3747
3748
7.36k
    return IS_LONG;
3749
8.60k
  } else {
3750
1.42k
    if (dval) {
3751
1.42k
      *dval = local_dval;
3752
1.42k
    }
3753
3754
1.42k
    return IS_DOUBLE;
3755
1.42k
  }
3756
10.0k
}
3757
/* }}} */
3758
3759
/*
3760
 * String matching - Sunday algorithm
3761
 * http://www.iti.fh-flensburg.de/lang/algorithmen/pattern/sundayen.htm
3762
 */
3763
0
static zend_always_inline void zend_memnstr_ex_pre(unsigned int td[], const char *needle, size_t needle_len, int reverse) /* {{{ */ {
3764
0
  int i;
3765
3766
0
  for (i = 0; i < 256; i++) {
3767
0
    td[i] = needle_len + 1;
3768
0
  }
3769
3770
0
  if (reverse) {
3771
0
    for (i = needle_len - 1; i >= 0; i--) {
3772
0
      td[(unsigned char)needle[i]] = i + 1;
3773
0
    }
3774
0
  } else {
3775
0
    size_t i;
3776
3777
0
    for (i = 0; i < needle_len; i++) {
3778
0
      td[(unsigned char)needle[i]] = (int)needle_len - i;
3779
0
    }
3780
0
  }
3781
0
}
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
0
{
3786
0
  unsigned int td[256];
3787
0
  size_t i;
3788
0
  const char *p;
3789
3790
0
  if (needle_len == 0 || (end - haystack) < needle_len) {
3791
0
    return NULL;
3792
0
  }
3793
3794
0
  zend_memnstr_ex_pre(td, needle, needle_len, 0);
3795
3796
0
  p = haystack;
3797
0
  end -= needle_len;
3798
3799
0
  while (p <= end) {
3800
0
    for (i = 0; i < needle_len; i++) {
3801
0
      if (needle[i] != p[i]) {
3802
0
        break;
3803
0
      }
3804
0
    }
3805
0
    if (i == needle_len) {
3806
0
      return p;
3807
0
    }
3808
0
    if (UNEXPECTED(p == end)) {
3809
0
      return NULL;
3810
0
    }
3811
0
    p += td[(unsigned char)(p[needle_len])];
3812
0
  }
3813
3814
0
  return NULL;
3815
0
}
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.93k
{
3872
7.93k
  double  two_pow_64 = pow(2., 64.),
3873
7.93k
      dmod;
3874
3875
7.93k
  dmod = fmod(d, two_pow_64);
3876
7.93k
  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
331
    dmod += two_pow_64;
3880
331
  }
3881
7.93k
  return (zend_long)(zend_ulong)dmod;
3882
7.93k
}
3883
/* }}} */
3884
#endif