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