1 : /*
2 : +----------------------------------------------------------------------+
3 : | Zend Engine |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1998-2009 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@zend.com> |
16 : | Zeev Suraski <zeev@zend.com> |
17 : +----------------------------------------------------------------------+
18 : */
19 :
20 : /* $Id: zend_operators.h 280482 2009-05-14 01:28:15Z mattwil $ */
21 :
22 : #ifndef ZEND_OPERATORS_H
23 : #define ZEND_OPERATORS_H
24 :
25 : #include <errno.h>
26 : #include <math.h>
27 : #include <assert.h>
28 :
29 : #ifdef HAVE_IEEEFP_H
30 : #include <ieeefp.h>
31 : #endif
32 :
33 : #include "zend_strtod.h"
34 :
35 : #if 0&&HAVE_BCMATH
36 : #include "ext/bcmath/libbcmath/src/bcmath.h"
37 : #endif
38 :
39 : BEGIN_EXTERN_C()
40 : ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
41 : ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
42 : ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
43 : ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
44 : ZEND_API int mod_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
45 : ZEND_API int boolean_xor_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
46 : ZEND_API int boolean_not_function(zval *result, zval *op1 TSRMLS_DC);
47 : ZEND_API int bitwise_not_function(zval *result, zval *op1 TSRMLS_DC);
48 : ZEND_API int bitwise_or_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
49 : ZEND_API int bitwise_and_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
50 : ZEND_API int bitwise_xor_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
51 : ZEND_API int shift_left_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
52 : ZEND_API int shift_right_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
53 : ZEND_API int concat_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
54 :
55 : ZEND_API int is_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
56 : ZEND_API int is_identical_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
57 : ZEND_API int is_not_identical_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
58 : ZEND_API int is_not_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
59 : ZEND_API int is_smaller_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
60 : ZEND_API int is_smaller_or_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
61 :
62 : ZEND_API zend_bool instanceof_function_ex(zend_class_entry *instance_ce, zend_class_entry *ce, zend_bool interfaces_only TSRMLS_DC);
63 : ZEND_API zend_bool instanceof_function(zend_class_entry *instance_ce, zend_class_entry *ce TSRMLS_DC);
64 : END_EXTERN_C()
65 :
66 : #define ZEND_IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
67 : #define ZEND_IS_XDIGIT(c) (((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
68 :
69 : /**
70 : * Checks whether the string "str" with length "length" is numeric. The value
71 : * of allow_errors determines whether it's required to be entirely numeric, or
72 : * just its prefix. Leading whitespace is allowed.
73 : *
74 : * The function returns 0 if the string did not contain a valid number; IS_LONG
75 : * if it contained a number that fits within the range of a long; or IS_DOUBLE
76 : * if the number was out of long range or contained a decimal point/exponent.
77 : * The number's value is returned into the respective pointer, *lval or *dval,
78 : * if that pointer is not NULL.
79 : */
80 :
81 : static inline zend_uchar is_numeric_string(const char *str, int length, long *lval, double *dval, int allow_errors)
82 1215349 : {
83 : const char *ptr;
84 1215349 : int base = 10, digits = 0, dp_or_e = 0;
85 : double local_dval;
86 : zend_uchar type;
87 :
88 1215349 : if (!length) {
89 788 : return 0;
90 : }
91 :
92 : /* Skip any whitespace
93 : * This is much faster than the isspace() function */
94 2430908 : while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\v' || *str == '\f') {
95 1786 : str++;
96 1786 : length--;
97 : }
98 1214561 : ptr = str;
99 :
100 1214561 : if (*ptr == '-' || *ptr == '+') {
101 24878 : ptr++;
102 : }
103 :
104 1727116 : if (ZEND_IS_DIGIT(*ptr)) {
105 : /* Handle hex numbers
106 : * str is used instead of ptr to disallow signs and keep old behavior */
107 714853 : if (length > 2 && *str == '0' && (str[1] == 'x' || str[1] == 'X')) {
108 221 : base = 16;
109 221 : ptr += 2;
110 : }
111 :
112 : /* Skip any leading 0s */
113 1630212 : while (*ptr == '0') {
114 200506 : ptr++;
115 : }
116 :
117 : /* Count the number of digits. If a decimal point/exponent is found,
118 : * it's a double. Otherwise, if there's a dval or no need to check for
119 : * a full match, stop when there are too many digits for a long */
120 5242198 : for (type = IS_LONG; !(digits >= MAX_LENGTH_OF_LONG && (dval || allow_errors == 1)); digits++, ptr++) {
121 5242186 : check_digits:
122 5242186 : if (ZEND_IS_DIGIT(*ptr) || (base == 16 && ZEND_IS_XDIGIT(*ptr))) {
123 : continue;
124 714841 : } else if (base == 10) {
125 714620 : if (*ptr == '.' && dp_or_e < 1) {
126 : goto process_double;
127 512588 : } else if ((*ptr == 'e' || *ptr == 'E') && dp_or_e < 2) {
128 256 : const char *e = ptr + 1;
129 :
130 256 : if (*e == '-' || *e == '+') {
131 8 : ptr = e++;
132 : }
133 256 : if (ZEND_IS_DIGIT(*e)) {
134 252 : goto process_double;
135 : }
136 : }
137 : }
138 :
139 512557 : break;
140 : }
141 :
142 512581 : if (base == 10) {
143 512360 : if (digits >= MAX_LENGTH_OF_LONG) {
144 26 : dp_or_e = -1;
145 26 : goto process_double;
146 : }
147 221 : } else if (!(digits < SIZEOF_LONG * 2 || (digits == SIZEOF_LONG * 2 && ptr[-digits] <= '7'))) {
148 0 : if (dval) {
149 0 : local_dval = zend_hex_strtod(str, (char **)&ptr);
150 : }
151 0 : type = IS_DOUBLE;
152 : }
153 702258 : } else if (*ptr == '.' && ZEND_IS_DIGIT(ptr[1])) {
154 202562 : process_double:
155 202562 : type = IS_DOUBLE;
156 :
157 : /* If there's a dval, do the conversion; else continue checking
158 : * the digits if we need to check for a full match */
159 202562 : if (dval) {
160 202548 : local_dval = zend_strtod(str, (char **)&ptr);
161 14 : } else if (allow_errors != 1 && dp_or_e != -1) {
162 12 : dp_or_e = (*ptr++ == '.') ? 1 : 2;
163 12 : goto check_digits;
164 : }
165 : } else {
166 499456 : return 0;
167 : }
168 :
169 715105 : if (ptr != str + length) {
170 1360 : if (!allow_errors) {
171 591 : return 0;
172 : }
173 769 : if (allow_errors == -1) {
174 3 : zend_error(E_NOTICE, "A non well formed numeric value encountered");
175 : }
176 : }
177 :
178 714514 : if (type == IS_LONG) {
179 512389 : if (digits == MAX_LENGTH_OF_LONG - 1) {
180 399980 : int cmp = strcmp(&ptr[-digits], long_min_digits);
181 :
182 399980 : if (!(cmp < 0 || (cmp == 0 && *str == '-'))) {
183 28 : if (dval) {
184 28 : *dval = zend_strtod(str, NULL);
185 : }
186 :
187 28 : return IS_DOUBLE;
188 : }
189 : }
190 :
191 512361 : if (lval) {
192 512325 : *lval = strtol(str, NULL, base);
193 : }
194 :
195 512361 : return IS_LONG;
196 : } else {
197 202125 : if (dval) {
198 202114 : *dval = local_dval;
199 : }
200 :
201 202125 : return IS_DOUBLE;
202 : }
203 : }
204 :
205 : static inline char *
206 : zend_memnstr(char *haystack, char *needle, int needle_len, char *end)
207 1791749 : {
208 1791749 : char *p = haystack;
209 1791749 : char ne = needle[needle_len-1];
210 :
211 1791749 : if(needle_len > end-haystack) {
212 77420 : return NULL;
213 : }
214 1714329 : end -= needle_len;
215 :
216 5735809 : while (p <= end) {
217 3975219 : if ((p = (char *)memchr(p, *needle, (end-p+1))) && ne == p[needle_len-1]) {
218 1167292 : if (!memcmp(needle, p, needle_len-1)) {
219 816983 : return p;
220 : }
221 : }
222 :
223 3158236 : if (p == NULL) {
224 851085 : return NULL;
225 : }
226 :
227 2307151 : p++;
228 : }
229 :
230 46261 : return NULL;
231 : }
232 :
233 : static inline void *zend_memrchr(const void *s, int c, size_t n)
234 1731 : {
235 : register unsigned char *e;
236 :
237 1731 : if (n <= 0) {
238 369 : return NULL;
239 : }
240 :
241 14567 : for (e = (unsigned char *)s + n - 1; e >= (unsigned char *)s; e--) {
242 13769 : if (*e == (unsigned char)c) {
243 564 : return (void *)e;
244 : }
245 : }
246 :
247 798 : return NULL;
248 : }
249 :
250 : BEGIN_EXTERN_C()
251 : ZEND_API int increment_function(zval *op1);
252 : ZEND_API int decrement_function(zval *op2);
253 :
254 : ZEND_API void convert_scalar_to_number(zval *op TSRMLS_DC);
255 : ZEND_API void _convert_to_string(zval *op ZEND_FILE_LINE_DC);
256 : ZEND_API void convert_to_long(zval *op);
257 : ZEND_API void convert_to_double(zval *op);
258 : ZEND_API void convert_to_long_base(zval *op, int base);
259 : ZEND_API void convert_to_null(zval *op);
260 : ZEND_API void convert_to_boolean(zval *op);
261 : ZEND_API void convert_to_array(zval *op);
262 : ZEND_API void convert_to_object(zval *op);
263 : ZEND_API void multi_convert_to_long_ex(int argc, ...);
264 : ZEND_API void multi_convert_to_double_ex(int argc, ...);
265 : ZEND_API void multi_convert_to_string_ex(int argc, ...);
266 : ZEND_API int add_char_to_string(zval *result, zval *op1, zval *op2);
267 : ZEND_API int add_string_to_string(zval *result, zval *op1, zval *op2);
268 : #define convert_to_string(op) if ((op)->type != IS_STRING) { _convert_to_string((op) ZEND_FILE_LINE_CC); }
269 :
270 : ZEND_API double zend_string_to_double(const char *number, zend_uint length);
271 :
272 : ZEND_API int zval_is_true(zval *op);
273 : ZEND_API int compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
274 : ZEND_API int numeric_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
275 : ZEND_API int string_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
276 : #if HAVE_STRCOLL
277 : ZEND_API int string_locale_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
278 : #endif
279 :
280 : ZEND_API void zend_str_tolower(char *str, unsigned int length);
281 : ZEND_API char *zend_str_tolower_copy(char *dest, const char *source, unsigned int length);
282 : END_EXTERN_C()
283 :
284 : static inline char *
285 : zend_str_tolower_dup(const char *source, unsigned int length)
286 43349134 : {
287 43349134 : return zend_str_tolower_copy((char *)emalloc(length+1), source, length);
288 : }
289 :
290 : BEGIN_EXTERN_C()
291 : ZEND_API int zend_binary_zval_strcmp(zval *s1, zval *s2);
292 : ZEND_API int zend_binary_zval_strncmp(zval *s1, zval *s2, zval *s3);
293 : ZEND_API int zend_binary_zval_strcasecmp(zval *s1, zval *s2);
294 : ZEND_API int zend_binary_zval_strncasecmp(zval *s1, zval *s2, zval *s3);
295 : ZEND_API int zend_binary_strcmp(char *s1, uint len1, char *s2, uint len2);
296 : ZEND_API int zend_binary_strncmp(char *s1, uint len1, char *s2, uint len2, uint length);
297 : ZEND_API int zend_binary_strcasecmp(char *s1, uint len1, char *s2, uint len2);
298 : ZEND_API int zend_binary_strncasecmp(char *s1, uint len1, char *s2, uint len2, uint length);
299 :
300 : ZEND_API void zendi_smart_strcmp(zval *result, zval *s1, zval *s2);
301 : ZEND_API void zend_compare_symbol_tables(zval *result, HashTable *ht1, HashTable *ht2 TSRMLS_DC);
302 : ZEND_API void zend_compare_arrays(zval *result, zval *a1, zval *a2 TSRMLS_DC);
303 : ZEND_API void zend_compare_objects(zval *result, zval *o1, zval *o2 TSRMLS_DC);
304 :
305 : ZEND_API int zend_atoi(const char *str, int str_len);
306 :
307 : ZEND_API void zend_locale_sprintf_double(zval *op ZEND_FILE_LINE_DC);
308 : END_EXTERN_C()
309 : #define convert_to_ex_master(ppzv, lower_type, upper_type) \
310 : if ((*ppzv)->type!=IS_##upper_type) { \
311 : SEPARATE_ZVAL_IF_NOT_REF(ppzv); \
312 : convert_to_##lower_type(*ppzv); \
313 : }
314 :
315 : #define convert_to_explicit_type(pzv, type) \
316 : do { \
317 : switch (type) { \
318 : case IS_NULL: \
319 : convert_to_null(pzv); \
320 : break; \
321 : case IS_LONG: \
322 : convert_to_long(pzv); \
323 : break; \
324 : case IS_DOUBLE: \
325 : convert_to_double(pzv); \
326 : break; \
327 : case IS_BOOL: \
328 : convert_to_boolean(pzv); \
329 : break; \
330 : case IS_ARRAY: \
331 : convert_to_array(pzv); \
332 : break; \
333 : case IS_OBJECT: \
334 : convert_to_object(pzv); \
335 : break; \
336 : case IS_STRING: \
337 : convert_to_string(pzv); \
338 : break; \
339 : default: \
340 : assert(0); \
341 : break; \
342 : } \
343 : } while (0); \
344 :
345 : #define convert_to_explicit_type_ex(ppzv, str_type) \
346 : if (Z_TYPE_PP(ppzv) != str_type) { \
347 : SEPARATE_ZVAL_IF_NOT_REF(ppzv); \
348 : convert_to_explicit_type(*ppzv, str_type); \
349 : }
350 :
351 : #define convert_to_boolean_ex(ppzv) convert_to_ex_master(ppzv, boolean, BOOL)
352 : #define convert_to_long_ex(ppzv) convert_to_ex_master(ppzv, long, LONG)
353 : #define convert_to_double_ex(ppzv) convert_to_ex_master(ppzv, double, DOUBLE)
354 : #define convert_to_string_ex(ppzv) convert_to_ex_master(ppzv, string, STRING)
355 : #define convert_to_array_ex(ppzv) convert_to_ex_master(ppzv, array, ARRAY)
356 : #define convert_to_object_ex(ppzv) convert_to_ex_master(ppzv, object, OBJECT)
357 : #define convert_to_null_ex(ppzv) convert_to_ex_master(ppzv, null, NULL)
358 :
359 : #define convert_scalar_to_number_ex(ppzv) \
360 : if (Z_TYPE_PP(ppzv)!=IS_LONG && Z_TYPE_PP(ppzv)!=IS_DOUBLE) { \
361 : if (!(*ppzv)->is_ref) { \
362 : SEPARATE_ZVAL(ppzv); \
363 : } \
364 : convert_scalar_to_number(*ppzv TSRMLS_CC); \
365 : }
366 :
367 :
368 : #define Z_LVAL(zval) (zval).value.lval
369 : #define Z_BVAL(zval) ((zend_bool)(zval).value.lval)
370 : #define Z_DVAL(zval) (zval).value.dval
371 : #define Z_STRVAL(zval) (zval).value.str.val
372 : #define Z_STRLEN(zval) (zval).value.str.len
373 : #define Z_ARRVAL(zval) (zval).value.ht
374 : #define Z_OBJVAL(zval) (zval).value.obj
375 : #define Z_OBJ_HANDLE(zval) Z_OBJVAL(zval).handle
376 : #define Z_OBJ_HT(zval) Z_OBJVAL(zval).handlers
377 : #define Z_OBJCE(zval) zend_get_class_entry(&(zval) TSRMLS_CC)
378 : #define Z_OBJPROP(zval) Z_OBJ_HT((zval))->get_properties(&(zval) TSRMLS_CC)
379 : #define Z_OBJ_HANDLER(zval, hf) Z_OBJ_HT((zval))->hf
380 : #define Z_RESVAL(zval) (zval).value.lval
381 :
382 : #define Z_LVAL_P(zval_p) Z_LVAL(*zval_p)
383 : #define Z_BVAL_P(zval_p) Z_BVAL(*zval_p)
384 : #define Z_DVAL_P(zval_p) Z_DVAL(*zval_p)
385 : #define Z_STRVAL_P(zval_p) Z_STRVAL(*zval_p)
386 : #define Z_STRLEN_P(zval_p) Z_STRLEN(*zval_p)
387 : #define Z_ARRVAL_P(zval_p) Z_ARRVAL(*zval_p)
388 : #define Z_OBJPROP_P(zval_p) Z_OBJPROP(*zval_p)
389 : #define Z_OBJCE_P(zval_p) Z_OBJCE(*zval_p)
390 : #define Z_RESVAL_P(zval_p) Z_RESVAL(*zval_p)
391 : #define Z_OBJVAL_P(zval_p) Z_OBJVAL(*zval_p)
392 : #define Z_OBJ_HANDLE_P(zval_p) Z_OBJ_HANDLE(*zval_p)
393 : #define Z_OBJ_HT_P(zval_p) Z_OBJ_HT(*zval_p)
394 : #define Z_OBJ_HANDLER_P(zval_p, h) Z_OBJ_HANDLER(*zval_p, h)
395 :
396 : #define Z_LVAL_PP(zval_pp) Z_LVAL(**zval_pp)
397 : #define Z_BVAL_PP(zval_pp) Z_BVAL(**zval_pp)
398 : #define Z_DVAL_PP(zval_pp) Z_DVAL(**zval_pp)
399 : #define Z_STRVAL_PP(zval_pp) Z_STRVAL(**zval_pp)
400 : #define Z_STRLEN_PP(zval_pp) Z_STRLEN(**zval_pp)
401 : #define Z_ARRVAL_PP(zval_pp) Z_ARRVAL(**zval_pp)
402 : #define Z_OBJPROP_PP(zval_pp) Z_OBJPROP(**zval_pp)
403 : #define Z_OBJCE_PP(zval_pp) Z_OBJCE(**zval_pp)
404 : #define Z_RESVAL_PP(zval_pp) Z_RESVAL(**zval_pp)
405 : #define Z_OBJVAL_PP(zval_pp) Z_OBJVAL(**zval_pp)
406 : #define Z_OBJ_HANDLE_PP(zval_p) Z_OBJ_HANDLE(**zval_p)
407 : #define Z_OBJ_HT_PP(zval_p) Z_OBJ_HT(**zval_p)
408 : #define Z_OBJ_HANDLER_PP(zval_p, h) Z_OBJ_HANDLER(**zval_p, h)
409 :
410 : #define Z_TYPE(zval) (zval).type
411 : #define Z_TYPE_P(zval_p) Z_TYPE(*zval_p)
412 : #define Z_TYPE_PP(zval_pp) Z_TYPE(**zval_pp)
413 :
414 : #if HAVE_SETLOCALE && defined(ZEND_WIN32) && !defined(ZTS) && defined(_MSC_VER) && (_MSC_VER >= 1400)
415 : /* This is performance improvement of tolower() on Windows and VC2005
416 : * GIves 10-18% on bench.php
417 : */
418 : #define ZEND_USE_TOLOWER_L 1
419 : #endif
420 :
421 : #ifdef ZEND_USE_TOLOWER_L
422 : ZEND_API void zend_update_current_locale(void);
423 : #else
424 : #define zend_update_current_locale()
425 : #endif
426 :
427 : #endif
428 :
429 : /*
430 : * Local variables:
431 : * tab-width: 4
432 : * c-basic-offset: 4
433 : * indent-tabs-mode: t
434 : * End:
435 : */
|