diff options
Diffstat (limited to 'Zend/zend_operators.c')
| -rw-r--r-- | Zend/zend_operators.c | 2155 |
1 files changed, 1320 insertions, 835 deletions
diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c index 242611643f..01732510a2 100644 --- a/Zend/zend_operators.c +++ b/Zend/zend_operators.c @@ -84,9 +84,9 @@ ZEND_API int zend_atoi(const char *str, int str_len) /* {{{ */ int retval; if (!str_len) { - str_len = strlen(str); + str_len = (int)strlen(str); } - retval = strtol(str, NULL, 0); + retval = ZEND_STRTOL(str, NULL, 0); if (str_len>0) { switch (str[str_len-1]) { case 'g': @@ -107,14 +107,14 @@ ZEND_API int zend_atoi(const char *str, int str_len) /* {{{ */ } /* }}} */ -ZEND_API long zend_atol(const char *str, int str_len) /* {{{ */ +ZEND_API zend_long zend_atol(const char *str, int str_len) /* {{{ */ { - long retval; + zend_long retval; if (!str_len) { - str_len = strlen(str); + str_len = (int)strlen(str); } - retval = strtol(str, NULL, 0); + retval = ZEND_STRTOL(str, NULL, 0); if (str_len>0) { switch (str[str_len-1]) { case 'g': @@ -135,79 +135,46 @@ ZEND_API long zend_atol(const char *str, int str_len) /* {{{ */ } /* }}} */ -ZEND_API double zend_string_to_double(const char *number, zend_uint length) /* {{{ */ -{ - double divisor = 10.0; - double result = 0.0; - double exponent; - const char *end = number+length; - const char *digit = number; - - if (!length) { - return result; - } - - while (digit < end) { - if ((*digit <= '9' && *digit >= '0')) { - result *= 10; - result += *digit - '0'; - } else if (*digit == '.') { - digit++; - break; - } else if (toupper(*digit) == 'E') { - exponent = (double) atoi(digit+1); - result *= pow(10.0, exponent); - return result; - } else { - return result; - } - digit++; - } - - while (digit < end) { - if ((*digit <= '9' && *digit >= '0')) { - result += (*digit - '0') / divisor; - divisor *= 10; - } else if (toupper(*digit) == 'E') { - exponent = (double) atoi(digit+1); - result *= pow(10.0, exponent); - return result; - } else { - return result; - } - digit++; - } - return result; -} -/* }}} */ - -ZEND_API void convert_scalar_to_number(zval *op TSRMLS_DC) /* {{{ */ +ZEND_API void convert_scalar_to_number(zval *op) /* {{{ */ { +try_again: switch (Z_TYPE_P(op)) { + case IS_REFERENCE: + if (Z_REFCOUNT_P(op) == 1) { + ZVAL_UNREF(op); + } else { + Z_DELREF_P(op); + ZVAL_COPY_VALUE(op, Z_REFVAL_P(op)); + } + goto try_again; case IS_STRING: { - char *strval; + zend_string *str; - strval = Z_STRVAL_P(op); - if ((Z_TYPE_P(op)=is_numeric_string(strval, Z_STRLEN_P(op), &Z_LVAL_P(op), &Z_DVAL_P(op), 1)) == 0) { + str = Z_STR_P(op); + if ((Z_TYPE_INFO_P(op)=is_numeric_string(str->val, str->len, &Z_LVAL_P(op), &Z_DVAL_P(op), 1)) == 0) { ZVAL_LONG(op, 0); } - str_efree(strval); + zend_string_release(str); break; } - case IS_BOOL: - Z_TYPE_P(op) = IS_LONG; + case IS_NULL: + case IS_FALSE: + ZVAL_LONG(op, 0); + break; + case IS_TRUE: + ZVAL_LONG(op, 1); break; case IS_RESOURCE: - zend_list_delete(Z_LVAL_P(op)); - Z_TYPE_P(op) = IS_LONG; + { + zend_long l = Z_RES_HANDLE_P(op); + zval_ptr_dtor(op); + ZVAL_LONG(op, l); + } break; case IS_OBJECT: convert_to_long_base(op, 10); break; - case IS_NULL: - ZVAL_LONG(op, 0); - break; } } /* }}} */ @@ -216,30 +183,33 @@ ZEND_API void convert_scalar_to_number(zval *op TSRMLS_DC) /* {{{ */ #define zendi_convert_scalar_to_number(op, holder, result) \ if (op==result) { \ if (Z_TYPE_P(op) != IS_LONG) { \ - convert_scalar_to_number(op TSRMLS_CC); \ + convert_scalar_to_number(op); \ } \ } else { \ switch (Z_TYPE_P(op)) { \ case IS_STRING: \ { \ - if ((Z_TYPE(holder)=is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &Z_LVAL(holder), &Z_DVAL(holder), 1)) == 0) { \ + if ((Z_TYPE_INFO(holder)=is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &Z_LVAL(holder), &Z_DVAL(holder), 1)) == 0) { \ ZVAL_LONG(&(holder), 0); \ } \ (op) = &(holder); \ break; \ } \ - case IS_BOOL: \ - case IS_RESOURCE: \ - ZVAL_LONG(&(holder), Z_LVAL_P(op)); \ - (op) = &(holder); \ - break; \ case IS_NULL: \ + case IS_FALSE: \ ZVAL_LONG(&(holder), 0); \ (op) = &(holder); \ break; \ + case IS_TRUE: \ + ZVAL_LONG(&(holder), 1); \ + (op) = &(holder); \ + break; \ + case IS_RESOURCE: \ + ZVAL_LONG(&(holder), Z_RES_HANDLE_P(op)); \ + (op) = &(holder); \ + break; \ case IS_OBJECT: \ - (holder) = (*(op)); \ - zval_copy_ctor(&(holder)); \ + ZVAL_DUP(&(holder), op); \ convert_to_long_base(&(holder), 10); \ if (Z_TYPE(holder) == IS_LONG) { \ (op) = &(holder); \ @@ -250,114 +220,58 @@ ZEND_API void convert_scalar_to_number(zval *op TSRMLS_DC) /* {{{ */ /* }}} */ -/* {{{ zendi_convert_to_long */ -#define zendi_convert_to_long(op, holder, result) \ - if (op == result) { \ - convert_to_long(op); \ - } else if (Z_TYPE_P(op) != IS_LONG) { \ - switch (Z_TYPE_P(op)) { \ - case IS_NULL: \ - Z_LVAL(holder) = 0; \ - break; \ - case IS_DOUBLE: \ - Z_LVAL(holder) = zend_dval_to_lval(Z_DVAL_P(op)); \ - break; \ - case IS_STRING: \ - Z_LVAL(holder) = strtol(Z_STRVAL_P(op), NULL, 10); \ - break; \ - case IS_ARRAY: \ - Z_LVAL(holder) = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0); \ - break; \ - case IS_OBJECT: \ - (holder) = (*(op)); \ - zval_copy_ctor(&(holder)); \ - convert_to_long_base(&(holder), 10); \ - break; \ - case IS_BOOL: \ - case IS_RESOURCE: \ - Z_LVAL(holder) = Z_LVAL_P(op); \ - break; \ - default: \ - zend_error(E_WARNING, "Cannot convert to ordinal value"); \ - Z_LVAL(holder) = 0; \ - break; \ - } \ - Z_TYPE(holder) = IS_LONG; \ - (op) = &(holder); \ - } - -/* }}} */ - -/* {{{ zendi_convert_to_boolean */ -#define zendi_convert_to_boolean(op, holder, result) \ - if (op==result) { \ - convert_to_boolean(op); \ - } else if (Z_TYPE_P(op) != IS_BOOL) { \ - switch (Z_TYPE_P(op)) { \ - case IS_NULL: \ - Z_LVAL(holder) = 0; \ - break; \ - case IS_RESOURCE: \ - case IS_LONG: \ - Z_LVAL(holder) = (Z_LVAL_P(op) ? 1 : 0); \ - break; \ - case IS_DOUBLE: \ - Z_LVAL(holder) = (Z_DVAL_P(op) ? 1 : 0); \ - break; \ - case IS_STRING: \ - if (Z_STRLEN_P(op) == 0 \ - || (Z_STRLEN_P(op)==1 && Z_STRVAL_P(op)[0]=='0')) { \ - Z_LVAL(holder) = 0; \ - } else { \ - Z_LVAL(holder) = 1; \ - } \ - break; \ - case IS_ARRAY: \ - Z_LVAL(holder) = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0); \ - break; \ - case IS_OBJECT: \ - (holder) = (*(op)); \ - zval_copy_ctor(&(holder)); \ - convert_to_boolean(&(holder)); \ - break; \ - default: \ - Z_LVAL(holder) = 0; \ - break; \ - } \ - Z_TYPE(holder) = IS_BOOL; \ - (op) = &(holder); \ - } - -/* }}} */ - /* {{{ convert_object_to_type */ -#define convert_object_to_type(op, ctype, conv_func) \ +#define convert_object_to_type(op, dst, ctype, conv_func) \ + ZVAL_UNDEF(dst); \ if (Z_OBJ_HT_P(op)->cast_object) { \ - zval dst; \ - if (Z_OBJ_HT_P(op)->cast_object(op, &dst, ctype TSRMLS_CC) == FAILURE) { \ + if (Z_OBJ_HT_P(op)->cast_object(op, dst, ctype) == FAILURE) { \ zend_error(E_RECOVERABLE_ERROR, \ - "Object of class %s could not be converted to %s", Z_OBJCE_P(op)->name, \ + "Object of class %s could not be converted to %s", Z_OBJCE_P(op)->name->val,\ zend_get_type_by_const(ctype)); \ - } else { \ - zval_dtor(op); \ - Z_TYPE_P(op) = ctype; \ - op->value = dst.value; \ - } \ - } else { \ - if (Z_OBJ_HT_P(op)->get) { \ - zval *newop = Z_OBJ_HT_P(op)->get(op TSRMLS_CC); \ - if (Z_TYPE_P(newop) != IS_OBJECT) { \ - /* for safety - avoid loop */ \ - zval_dtor(op); \ - *op = *newop; \ - FREE_ZVAL(newop); \ - conv_func(op); \ - } \ + } \ + } else if (Z_OBJ_HT_P(op)->get) { \ + zval *newop = Z_OBJ_HT_P(op)->get(op, dst); \ + if (Z_TYPE_P(newop) != IS_OBJECT) { \ + /* for safety - avoid loop */ \ + ZVAL_COPY_VALUE(dst, newop); \ + conv_func(dst); \ } \ } /* }}} */ +#define convert_op1_op2_long(op1, op1_lval, op2, op2_lval, op, op_func) \ + do { \ + if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) { \ + if (Z_ISREF_P(op1)) { \ + op1 = Z_REFVAL_P(op1); \ + if (Z_TYPE_P(op1) == IS_LONG) { \ + op1_lval = Z_LVAL_P(op1); \ + break; \ + } \ + } \ + ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(op, op_func); \ + op1_lval = _zval_get_long_func(op1); \ + } else { \ + op1_lval = Z_LVAL_P(op1); \ + } \ + } while (0); \ + do { \ + if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) { \ + if (Z_ISREF_P(op2)) { \ + op2 = Z_REFVAL_P(op2); \ + if (Z_TYPE_P(op2) == IS_LONG) { \ + op2_lval = Z_LVAL_P(op2); \ + break; \ + } \ + } \ + ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(op); \ + op2_lval = _zval_get_long_func(op2); \ + } else { \ + op2_lval = Z_LVAL_P(op2); \ + } \ + } while (0); + ZEND_API void convert_to_long(zval *op) /* {{{ */ { if (Z_TYPE_P(op) != IS_LONG) { @@ -368,61 +282,60 @@ ZEND_API void convert_to_long(zval *op) /* {{{ */ ZEND_API void convert_to_long_base(zval *op, int base) /* {{{ */ { - long tmp; + zend_long tmp; switch (Z_TYPE_P(op)) { case IS_NULL: - Z_LVAL_P(op) = 0; + case IS_FALSE: + ZVAL_LONG(op, 0); + break; + case IS_TRUE: + ZVAL_LONG(op, 1); break; case IS_RESOURCE: { - TSRMLS_FETCH(); - - zend_list_delete(Z_LVAL_P(op)); + zend_long l = Z_RES_HANDLE_P(op); + zval_ptr_dtor(op); + ZVAL_LONG(op, l); } /* break missing intentionally */ - case IS_BOOL: + Z_TYPE_INFO_P(op) = IS_LONG; + break; case IS_LONG: break; case IS_DOUBLE: - Z_LVAL_P(op) = zend_dval_to_lval(Z_DVAL_P(op)); + ZVAL_LONG(op, zend_dval_to_lval(Z_DVAL_P(op))); break; case IS_STRING: { - char *strval = Z_STRVAL_P(op); + zend_string *str = Z_STR_P(op); - Z_LVAL_P(op) = strtol(strval, NULL, base); - str_efree(strval); + ZVAL_LONG(op, ZEND_STRTOL(str->val, NULL, base)); + zend_string_release(str); } break; case IS_ARRAY: tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0); zval_dtor(op); - Z_LVAL_P(op) = tmp; + ZVAL_LONG(op, tmp); break; case IS_OBJECT: { - int retval = 1; - TSRMLS_FETCH(); + zval dst; + + convert_object_to_type(op, &dst, IS_LONG, convert_to_long); + zval_dtor(op); - convert_object_to_type(op, IS_LONG, convert_to_long); + if (Z_TYPE(dst) == IS_LONG) { + ZVAL_COPY_VALUE(op, &dst); + } else { + zend_error(E_NOTICE, "Object of class %s could not be converted to int", Z_OBJCE_P(op)->name->val); - if (Z_TYPE_P(op) == IS_LONG) { - return; + ZVAL_LONG(op, 1); } - zend_error(E_NOTICE, "Object of class %s could not be converted to int", Z_OBJCE_P(op)->name); - - zval_dtor(op); - ZVAL_LONG(op, retval); return; } - default: - zend_error(E_WARNING, "Cannot convert to ordinal value"); - zval_dtor(op); - Z_LVAL_P(op) = 0; - break; + EMPTY_SWITCH_DEFAULT_CASE() } - - Z_TYPE_P(op) = IS_LONG; } /* }}} */ @@ -432,56 +345,54 @@ ZEND_API void convert_to_double(zval *op) /* {{{ */ switch (Z_TYPE_P(op)) { case IS_NULL: - Z_DVAL_P(op) = 0.0; + case IS_FALSE: + ZVAL_DOUBLE(op, 0.0); + break; + case IS_TRUE: + ZVAL_DOUBLE(op, 1.0); break; case IS_RESOURCE: { - TSRMLS_FETCH(); - - zend_list_delete(Z_LVAL_P(op)); + double d = (double) Z_RES_HANDLE_P(op); + zval_ptr_dtor(op); + ZVAL_DOUBLE(op, d); } - /* break missing intentionally */ - case IS_BOOL: + break; case IS_LONG: - Z_DVAL_P(op) = (double) Z_LVAL_P(op); + ZVAL_DOUBLE(op, (double) Z_LVAL_P(op)); break; case IS_DOUBLE: break; case IS_STRING: { - char *strval = Z_STRVAL_P(op); + zend_string *str = Z_STR_P(op); - Z_DVAL_P(op) = zend_strtod(strval, NULL); - str_efree(strval); + ZVAL_DOUBLE(op, zend_strtod(str->val, NULL)); + zend_string_release(str); } break; case IS_ARRAY: tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0); zval_dtor(op); - Z_DVAL_P(op) = tmp; + ZVAL_DOUBLE(op, tmp); break; case IS_OBJECT: { - double retval = 1.0; - TSRMLS_FETCH(); + zval dst; - convert_object_to_type(op, IS_DOUBLE, convert_to_double); + convert_object_to_type(op, &dst, IS_DOUBLE, convert_to_double); + zval_dtor(op); - if (Z_TYPE_P(op) == IS_DOUBLE) { - return; - } - zend_error(E_NOTICE, "Object of class %s could not be converted to double", Z_OBJCE_P(op)->name); + if (Z_TYPE(dst) == IS_DOUBLE) { + ZVAL_COPY_VALUE(op, &dst); + } else { + zend_error(E_NOTICE, "Object of class %s could not be converted to float", Z_OBJCE_P(op)->name->val); - zval_dtor(op); - ZVAL_DOUBLE(op, retval); + ZVAL_DOUBLE(op, 1.0); + } break; } - default: - zend_error(E_WARNING, "Cannot convert to real value (type=%d)", Z_TYPE_P(op)); - zval_dtor(op); - Z_DVAL_P(op) = 0; - break; + EMPTY_SWITCH_DEFAULT_CASE() } - Z_TYPE_P(op) = IS_DOUBLE; } /* }}} */ @@ -489,22 +400,19 @@ ZEND_API void convert_to_null(zval *op) /* {{{ */ { if (Z_TYPE_P(op) == IS_OBJECT) { if (Z_OBJ_HT_P(op)->cast_object) { - zval *org; - TSRMLS_FETCH(); + zval org; - ALLOC_ZVAL(org); - *org = *op; - if (Z_OBJ_HT_P(op)->cast_object(org, op, IS_NULL TSRMLS_CC) == SUCCESS) { - zval_dtor(org); + ZVAL_COPY_VALUE(&org, op); + if (Z_OBJ_HT_P(op)->cast_object(&org, op, IS_NULL) == SUCCESS) { + zval_dtor(&org); return; } - *op = *org; - FREE_ZVAL(org); + ZVAL_COPY_VALUE(op, &org); } } zval_dtor(op); - Z_TYPE_P(op) = IS_NULL; + ZVAL_NULL(op); } /* }}} */ @@ -513,225 +421,180 @@ ZEND_API void convert_to_boolean(zval *op) /* {{{ */ int tmp; switch (Z_TYPE_P(op)) { - case IS_BOOL: + case IS_FALSE: + case IS_TRUE: break; case IS_NULL: - Z_LVAL_P(op) = 0; + ZVAL_FALSE(op); break; case IS_RESOURCE: { - TSRMLS_FETCH(); + zend_long l = (Z_RES_HANDLE_P(op) ? 1 : 0); - zend_list_delete(Z_LVAL_P(op)); + zval_ptr_dtor(op); + ZVAL_BOOL(op, l); } - /* break missing intentionally */ + break; case IS_LONG: - Z_LVAL_P(op) = (Z_LVAL_P(op) ? 1 : 0); + ZVAL_BOOL(op, Z_LVAL_P(op) ? 1 : 0); break; case IS_DOUBLE: - Z_LVAL_P(op) = (Z_DVAL_P(op) ? 1 : 0); + ZVAL_BOOL(op, Z_DVAL_P(op) ? 1 : 0); break; case IS_STRING: { - char *strval = Z_STRVAL_P(op); + zend_string *str = Z_STR_P(op); - if (Z_STRLEN_P(op) == 0 - || (Z_STRLEN_P(op)==1 && Z_STRVAL_P(op)[0]=='0')) { - Z_LVAL_P(op) = 0; + if (str->len == 0 + || (str->len == 1 && str->val[0] == '0')) { + ZVAL_FALSE(op); } else { - Z_LVAL_P(op) = 1; + ZVAL_TRUE(op); } - str_efree(strval); + zend_string_release(str); } break; case IS_ARRAY: tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0); zval_dtor(op); - Z_LVAL_P(op) = tmp; + ZVAL_BOOL(op, tmp); break; case IS_OBJECT: { - zend_bool retval = 1; - TSRMLS_FETCH(); + zval dst; - convert_object_to_type(op, IS_BOOL, convert_to_boolean); + convert_object_to_type(op, &dst, _IS_BOOL, convert_to_boolean); + zval_dtor(op); - if (Z_TYPE_P(op) == IS_BOOL) { - return; + if (Z_TYPE(dst) == IS_FALSE || Z_TYPE(dst) == IS_TRUE) { + ZVAL_COPY_VALUE(op, &dst); + } else { + ZVAL_TRUE(op); } - - zval_dtor(op); - ZVAL_BOOL(op, retval); break; } - default: - zval_dtor(op); - Z_LVAL_P(op) = 0; - break; + EMPTY_SWITCH_DEFAULT_CASE() } - Z_TYPE_P(op) = IS_BOOL; } /* }}} */ ZEND_API void _convert_to_cstring(zval *op ZEND_FILE_LINE_DC) /* {{{ */ { - double dval; - switch (Z_TYPE_P(op)) { - case IS_DOUBLE: { - TSRMLS_FETCH(); - dval = Z_DVAL_P(op); - Z_STRLEN_P(op) = zend_spprintf(&Z_STRVAL_P(op), 0, "%.*H", (int) EG(precision), dval); - /* %H already handles removing trailing zeros from the fractional part, yay */ - break; - } - default: - _convert_to_string(op ZEND_FILE_LINE_CC); - } - Z_TYPE_P(op) = IS_STRING; + _convert_to_string(op ZEND_FILE_LINE_CC); } /* }}} */ ZEND_API void _convert_to_string(zval *op ZEND_FILE_LINE_DC) /* {{{ */ { - long lval; - double dval; - switch (Z_TYPE_P(op)) { + case IS_UNDEF: case IS_NULL: - Z_STRVAL_P(op) = STR_EMPTY_ALLOC(); - Z_STRLEN_P(op) = 0; + case IS_FALSE: { + ZVAL_EMPTY_STRING(op); break; - case IS_STRING: + } + case IS_TRUE: + ZVAL_NEW_STR(op, zend_string_init("1", 1, 0)); break; - case IS_BOOL: - if (Z_LVAL_P(op)) { - Z_STRVAL_P(op) = estrndup_rel("1", 1); - Z_STRLEN_P(op) = 1; - } else { - Z_STRVAL_P(op) = STR_EMPTY_ALLOC(); - Z_STRLEN_P(op) = 0; - } + case IS_STRING: break; case IS_RESOURCE: { - long tmp = Z_LVAL_P(op); - TSRMLS_FETCH(); - - zend_list_delete(Z_LVAL_P(op)); - Z_STRLEN_P(op) = zend_spprintf(&Z_STRVAL_P(op), 0, "Resource id #%ld", tmp); + char buf[sizeof("Resource id #") + MAX_LENGTH_OF_LONG]; + int len = snprintf(buf, sizeof(buf), "Resource id #" ZEND_LONG_FMT, (zend_long)Z_RES_HANDLE_P(op)); + ZVAL_NEW_STR(op, zend_string_init(buf, len, 0)); break; } - case IS_LONG: - lval = Z_LVAL_P(op); - - Z_STRLEN_P(op) = zend_spprintf(&Z_STRVAL_P(op), 0, "%ld", lval); + case IS_LONG: { + ZVAL_NEW_STR(op, zend_long_to_str(Z_LVAL_P(op))); break; + } case IS_DOUBLE: { - TSRMLS_FETCH(); - dval = Z_DVAL_P(op); - Z_STRLEN_P(op) = zend_spprintf(&Z_STRVAL_P(op), 0, "%.*G", (int) EG(precision), dval); + zend_string *str; + double dval = Z_DVAL_P(op); + + str = zend_strpprintf(0, "%.*G", (int) EG(precision), dval); /* %G already handles removing trailing zeros from the fractional part, yay */ + ZVAL_NEW_STR(op, str); break; } case IS_ARRAY: zend_error(E_NOTICE, "Array to string conversion"); zval_dtor(op); - Z_STRVAL_P(op) = estrndup_rel("Array", sizeof("Array")-1); - Z_STRLEN_P(op) = sizeof("Array")-1; + ZVAL_NEW_STR(op, zend_string_init("Array", sizeof("Array")-1, 0)); break; case IS_OBJECT: { - TSRMLS_FETCH(); + zval dst; - convert_object_to_type(op, IS_STRING, convert_to_string); + convert_object_to_type(op, &dst, IS_STRING, convert_to_string); - if (Z_TYPE_P(op) == IS_STRING) { - return; + if (Z_TYPE(dst) == IS_STRING) { + zval_dtor(op); + ZVAL_COPY_VALUE(op, &dst); + } else { + zend_error(E_NOTICE, "Object of class %s to string conversion", Z_OBJCE_P(op)->name->val); + zval_dtor(op); + ZVAL_NEW_STR(op, zend_string_init("Object", sizeof("Object")-1, 0)); } - - zend_error(E_NOTICE, "Object of class %s to string conversion", Z_OBJCE_P(op)->name); - zval_dtor(op); - Z_STRVAL_P(op) = estrndup_rel("Object", sizeof("Object")-1); - Z_STRLEN_P(op) = sizeof("Object")-1; break; } - default: - zval_dtor(op); - ZVAL_BOOL(op, 0); - break; + EMPTY_SWITCH_DEFAULT_CASE() } - Z_TYPE_P(op) = IS_STRING; } /* }}} */ -static void convert_scalar_to_array(zval *op, int type TSRMLS_DC) /* {{{ */ +static void convert_scalar_to_array(zval *op) /* {{{ */ { - zval *entry; + zval entry; - ALLOC_ZVAL(entry); - *entry = *op; - INIT_PZVAL(entry); + ZVAL_COPY_VALUE(&entry, op); - switch (type) { - case IS_ARRAY: - ALLOC_HASHTABLE(Z_ARRVAL_P(op)); - zend_hash_init(Z_ARRVAL_P(op), 0, NULL, ZVAL_PTR_DTOR, 0); - zend_hash_index_update(Z_ARRVAL_P(op), 0, (void *) &entry, sizeof(zval *), NULL); - Z_TYPE_P(op) = IS_ARRAY; - break; - case IS_OBJECT: - object_init(op); - zend_hash_update(Z_OBJPROP_P(op), "scalar", sizeof("scalar"), (void *) &entry, sizeof(zval *), NULL); - break; - } + ZVAL_NEW_ARR(op); + zend_hash_init(Z_ARRVAL_P(op), 8, NULL, ZVAL_PTR_DTOR, 0); + zend_hash_index_add_new(Z_ARRVAL_P(op), 0, &entry); } /* }}} */ ZEND_API void convert_to_array(zval *op) /* {{{ */ { - TSRMLS_FETCH(); switch (Z_TYPE_P(op)) { case IS_ARRAY: break; /* OBJECTS_OPTIMIZE */ case IS_OBJECT: - { - zval *tmp; - HashTable *ht; - - ALLOC_HASHTABLE(ht); - zend_hash_init(ht, 0, NULL, ZVAL_PTR_DTOR, 0); - if (Z_OBJCE_P(op) == zend_ce_closure) { - convert_scalar_to_array(op, IS_ARRAY TSRMLS_CC); - if (Z_TYPE_P(op) == IS_ARRAY) { - zend_hash_destroy(ht); - FREE_HASHTABLE(ht); - return; - } - } else if (Z_OBJ_HT_P(op)->get_properties) { - HashTable *obj_ht = Z_OBJ_HT_P(op)->get_properties(op TSRMLS_CC); + if (Z_OBJCE_P(op) == zend_ce_closure) { + convert_scalar_to_array(op); + } else { + if (Z_OBJ_HT_P(op)->get_properties) { + HashTable *obj_ht = Z_OBJ_HT_P(op)->get_properties(op); if (obj_ht) { - zend_hash_copy(ht, obj_ht, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *)); + zval arr; + ZVAL_NEW_ARR(&arr); + zend_array_dup(Z_ARRVAL(arr), obj_ht); + zval_dtor(op); + ZVAL_COPY_VALUE(op, &arr); + return; } } else { - convert_object_to_type(op, IS_ARRAY, convert_to_array); + zval dst; + convert_object_to_type(op, &dst, IS_ARRAY, convert_to_array); - if (Z_TYPE_P(op) == IS_ARRAY) { - zend_hash_destroy(ht); - FREE_HASHTABLE(ht); + if (Z_TYPE(dst) == IS_ARRAY) { + zval_dtor(op); + ZVAL_COPY_VALUE(op, &dst); return; } } + zval_dtor(op); - Z_TYPE_P(op) = IS_ARRAY; - Z_ARRVAL_P(op) = ht; + array_init(op); } break; case IS_NULL: - ALLOC_HASHTABLE(Z_ARRVAL_P(op)); - zend_hash_init(Z_ARRVAL_P(op), 0, NULL, ZVAL_PTR_DTOR, 0); - Z_TYPE_P(op) = IS_ARRAY; + ZVAL_NEW_ARR(op); + zend_hash_init(Z_ARRVAL_P(op), 8, NULL, ZVAL_PTR_DTOR, 0); break; default: - convert_scalar_to_array(op, IS_ARRAY TSRMLS_CC); + convert_scalar_to_array(op); break; } } @@ -739,12 +602,18 @@ ZEND_API void convert_to_array(zval *op) /* {{{ */ ZEND_API void convert_to_object(zval *op) /* {{{ */ { - TSRMLS_FETCH(); switch (Z_TYPE_P(op)) { case IS_ARRAY: { - object_and_properties_init(op, zend_standard_class_def, Z_ARRVAL_P(op)); + HashTable *properties = emalloc(sizeof(HashTable)); + zend_array *arr = Z_ARR_P(op); + + memcpy(properties, Z_ARRVAL_P(op), sizeof(HashTable)); + object_and_properties_init(op, zend_standard_class_def, properties); + if (--GC_REFCOUNT(arr) == 0) { + efree_size(arr, sizeof(zend_array)); + } break; } case IS_OBJECT: @@ -752,22 +621,26 @@ ZEND_API void convert_to_object(zval *op) /* {{{ */ case IS_NULL: object_init(op); break; - default: - convert_scalar_to_array(op, IS_OBJECT TSRMLS_CC); + default: { + zval tmp; + ZVAL_COPY_VALUE(&tmp, op); + object_init(op); + zend_hash_str_add_new(Z_OBJPROP_P(op), "scalar", sizeof("scalar")-1, &tmp); break; + } } } /* }}} */ ZEND_API void multi_convert_to_long_ex(int argc, ...) /* {{{ */ { - zval **arg; + zval *arg; va_list ap; va_start(ap, argc); while (argc--) { - arg = va_arg(ap, zval **); + arg = va_arg(ap, zval *); convert_to_long_ex(arg); } @@ -777,13 +650,13 @@ ZEND_API void multi_convert_to_long_ex(int argc, ...) /* {{{ */ ZEND_API void multi_convert_to_double_ex(int argc, ...) /* {{{ */ { - zval **arg; + zval *arg; va_list ap; va_start(ap, argc); while (argc--) { - arg = va_arg(ap, zval **); + arg = va_arg(ap, zval *); convert_to_double_ex(arg); } @@ -793,13 +666,13 @@ ZEND_API void multi_convert_to_double_ex(int argc, ...) /* {{{ */ ZEND_API void multi_convert_to_string_ex(int argc, ...) /* {{{ */ { - zval **arg; + zval *arg; va_list ap; va_start(ap, argc); while (argc--) { - arg = va_arg(ap, zval **); + arg = va_arg(ap, zval *); convert_to_string_ex(arg); } @@ -807,7 +680,142 @@ ZEND_API void multi_convert_to_string_ex(int argc, ...) /* {{{ */ } /* }}} */ -ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API zend_long _zval_get_long_func(zval *op) /* {{{ */ +{ +try_again: + switch (Z_TYPE_P(op)) { + case IS_NULL: + case IS_FALSE: + return 0; + case IS_TRUE: + return 1; + case IS_RESOURCE: + return Z_RES_HANDLE_P(op); + case IS_LONG: + return Z_LVAL_P(op); + case IS_DOUBLE: + return zend_dval_to_lval(Z_DVAL_P(op)); + case IS_STRING: + return ZEND_STRTOL(Z_STRVAL_P(op), NULL, 10); + case IS_ARRAY: + return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1 : 0; + case IS_OBJECT: + { + zval dst; + convert_object_to_type(op, &dst, IS_LONG, convert_to_long); + if (Z_TYPE(dst) == IS_LONG) { + return Z_LVAL(dst); + } else { + zend_error(E_NOTICE, "Object of class %s could not be converted to int", Z_OBJCE_P(op)->name->val); + return 1; + } + } + case IS_REFERENCE: + op = Z_REFVAL_P(op); + goto try_again; + EMPTY_SWITCH_DEFAULT_CASE() + } + return 0; +} +/* }}} */ + +ZEND_API double _zval_get_double_func(zval *op) /* {{{ */ +{ +try_again: + switch (Z_TYPE_P(op)) { + case IS_NULL: + case IS_FALSE: + return 0.0; + case IS_TRUE: + return 1.0; + case IS_RESOURCE: + return (double) Z_RES_HANDLE_P(op); + case IS_LONG: + return (double) Z_LVAL_P(op); + case IS_DOUBLE: + return Z_DVAL_P(op); + case IS_STRING: + return zend_strtod(Z_STRVAL_P(op), NULL); + case IS_ARRAY: + return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1.0 : 0.0; + case IS_OBJECT: + { + zval dst; + convert_object_to_type(op, &dst, IS_DOUBLE, convert_to_double); + + if (Z_TYPE(dst) == IS_DOUBLE) { + return Z_DVAL(dst); + } else { + zend_error(E_NOTICE, "Object of class %s could not be converted to double", Z_OBJCE_P(op)->name->val); + + return 1.0; + } + } + case IS_REFERENCE: + op = Z_REFVAL_P(op); + goto try_again; + EMPTY_SWITCH_DEFAULT_CASE() + } + return 0.0; +} +/* }}} */ + +ZEND_API zend_string *_zval_get_string_func(zval *op) /* {{{ */ +{ +try_again: + switch (Z_TYPE_P(op)) { + case IS_UNDEF: + case IS_NULL: + case IS_FALSE: + return STR_EMPTY_ALLOC(); + case IS_TRUE: + return zend_string_init("1", 1, 0); + case IS_RESOURCE: { + char buf[sizeof("Resource id #") + MAX_LENGTH_OF_LONG]; + int len; + + len = snprintf(buf, sizeof(buf), "Resource id #" ZEND_LONG_FMT, (zend_long)Z_RES_HANDLE_P(op)); + return zend_string_init(buf, len, 0); + } + case IS_LONG: { + return zend_long_to_str(Z_LVAL_P(op)); + } + case IS_DOUBLE: { + return zend_strpprintf(0, "%.*G", (int) EG(precision), Z_DVAL_P(op)); + } + case IS_ARRAY: + zend_error(E_NOTICE, "Array to string conversion"); + return zend_string_init("Array", sizeof("Array")-1, 0); + case IS_OBJECT: { + zval tmp; + if (Z_OBJ_HT_P(op)->cast_object) { + if (Z_OBJ_HT_P(op)->cast_object(op, &tmp, IS_STRING) == SUCCESS) { + return Z_STR(tmp); + } + } else if (Z_OBJ_HT_P(op)->get) { + zval *z = Z_OBJ_HT_P(op)->get(op, &tmp); + if (Z_TYPE_P(z) != IS_OBJECT) { + zend_string *str = zval_get_string(z); + zval_ptr_dtor(z); + return str; + } + zval_ptr_dtor(z); + } + zend_error(EG(exception) ? E_ERROR : E_RECOVERABLE_ERROR, "Object of class %s could not be converted to string", Z_OBJCE_P(op)->name->val); + return STR_EMPTY_ALLOC(); + } + case IS_REFERENCE: + op = Z_REFVAL_P(op); + goto try_again; + case IS_STRING: + return zend_string_copy(Z_STR_P(op)); + EMPTY_SWITCH_DEFAULT_CASE() + } + return NULL; +} +/* }}} */ + +ZEND_API int add_function(zval *result, zval *op1, zval *op2) /* {{{ */ { zval op1_copy, op2_copy; int converted = 0; @@ -815,7 +823,7 @@ ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * while (1) { switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) { case TYPE_PAIR(IS_LONG, IS_LONG): { - long lval = Z_LVAL_P(op1) + Z_LVAL_P(op2); + zend_long lval = Z_LVAL_P(op1) + Z_LVAL_P(op2); /* check for overflow by comparing sign bits */ if ((Z_LVAL_P(op1) & LONG_SIGN_MASK) == (Z_LVAL_P(op2) & LONG_SIGN_MASK) @@ -840,24 +848,24 @@ ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * ZVAL_DOUBLE(result, Z_DVAL_P(op1) + Z_DVAL_P(op2)); return SUCCESS; - case TYPE_PAIR(IS_ARRAY, IS_ARRAY): { - zval *tmp; - + case TYPE_PAIR(IS_ARRAY, IS_ARRAY): if ((result == op1) && (result == op2)) { /* $a += $a */ return SUCCESS; } if (result != op1) { - *result = *op1; - zval_copy_ctor(result); + ZVAL_DUP(result, op1); } - zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), (void (*)(void *pData)) zval_add_ref, (void *) &tmp, sizeof(zval *), 0); + zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0); return SUCCESS; - } default: - if (!converted) { - ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_ADD); + if (Z_ISREF_P(op1)) { + op1 = Z_REFVAL_P(op1); + } else if (Z_ISREF_P(op2)) { + op2 = Z_REFVAL_P(op2); + } else if (!converted) { + ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_ADD, add_function); zendi_convert_scalar_to_number(op1, op1_copy, result); zendi_convert_scalar_to_number(op2, op2_copy, result); @@ -871,7 +879,7 @@ ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * } /* }}} */ -ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int sub_function(zval *result, zval *op1, zval *op2) /* {{{ */ { zval op1_copy, op2_copy; int converted = 0; @@ -879,7 +887,7 @@ ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * while (1) { switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) { case TYPE_PAIR(IS_LONG, IS_LONG): { - long lval = Z_LVAL_P(op1) - Z_LVAL_P(op2); + zend_long lval = Z_LVAL_P(op1) - Z_LVAL_P(op2); /* check for overflow by comparing sign bits */ if ((Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(op2) & LONG_SIGN_MASK) @@ -905,8 +913,12 @@ ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * return SUCCESS; default: - if (!converted) { - ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SUB); + if (Z_ISREF_P(op1)) { + op1 = Z_REFVAL_P(op1); + } else if (Z_ISREF_P(op2)) { + op2 = Z_REFVAL_P(op2); + } else if (!converted) { + ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SUB, sub_function); zendi_convert_scalar_to_number(op1, op1_copy, result); zendi_convert_scalar_to_number(op2, op2_copy, result); @@ -920,7 +932,7 @@ ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * } /* }}} */ -ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int mul_function(zval *result, zval *op1, zval *op2) /* {{{ */ { zval op1_copy, op2_copy; int converted = 0; @@ -928,10 +940,10 @@ ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * while (1) { switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) { case TYPE_PAIR(IS_LONG, IS_LONG): { - long overflow; + zend_long overflow; ZEND_SIGNED_MULTIPLY_LONG(Z_LVAL_P(op1),Z_LVAL_P(op2), Z_LVAL_P(result),Z_DVAL_P(result),overflow); - Z_TYPE_P(result) = overflow ? IS_DOUBLE : IS_LONG; + Z_TYPE_INFO_P(result) = overflow ? IS_DOUBLE : IS_LONG; return SUCCESS; } @@ -948,8 +960,12 @@ ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * return SUCCESS; default: - if (!converted) { - ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_MUL); + if (Z_ISREF_P(op1)) { + op1 = Z_REFVAL_P(op1); + } else if (Z_ISREF_P(op2)) { + op2 = Z_REFVAL_P(op2); + } else if (!converted) { + ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_MUL, mul_function); zendi_convert_scalar_to_number(op1, op1_copy, result); zendi_convert_scalar_to_number(op2, op2_copy, result); @@ -963,7 +979,7 @@ ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * } /* }}} */ -ZEND_API int pow_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int pow_function(zval *result, zval *op1, zval *op2) /* {{{ */ { zval op1_copy, op2_copy; int converted = 0; @@ -972,7 +988,7 @@ ZEND_API int pow_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) { case TYPE_PAIR(IS_LONG, IS_LONG): if (Z_LVAL_P(op2) >= 0) { - long l1 = 1, l2 = Z_LVAL_P(op1), i = Z_LVAL_P(op2); + zend_long l1 = 1, l2 = Z_LVAL_P(op1), i = Z_LVAL_P(op2); if (i == 0) { ZVAL_LONG(result, 1L); @@ -983,7 +999,7 @@ ZEND_API int pow_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * } while (i >= 1) { - long overflow; + zend_long overflow; double dval = 0.0; if (i % 2) { @@ -1022,8 +1038,12 @@ ZEND_API int pow_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * return SUCCESS; default: - if (!converted) { - ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_POW); + if (Z_ISREF_P(op1)) { + op1 = Z_REFVAL_P(op1); + } else if (Z_ISREF_P(op2)) { + op2 = Z_REFVAL_P(op2); + } else if (!converted) { + ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_POW, pow_function); if (Z_TYPE_P(op1) == IS_ARRAY) { ZVAL_LONG(result, 0); @@ -1047,7 +1067,7 @@ ZEND_API int pow_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * } /* }}} */ -ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int div_function(zval *result, zval *op1, zval *op2) /* {{{ */ { zval op1_copy, op2_copy; int converted = 0; @@ -1057,11 +1077,11 @@ ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * case TYPE_PAIR(IS_LONG, IS_LONG): if (Z_LVAL_P(op2) == 0) { zend_error(E_WARNING, "Division by zero"); - ZVAL_BOOL(result, 0); + ZVAL_FALSE(result); return FAILURE; /* division by zero */ - } else if (Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == LONG_MIN) { + } else if (Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == ZEND_LONG_MIN) { /* Prevent overflow error/crash */ - ZVAL_DOUBLE(result, (double) LONG_MIN / -1); + ZVAL_DOUBLE(result, (double) ZEND_LONG_MIN / -1); return SUCCESS; } if (Z_LVAL_P(op1) % Z_LVAL_P(op2) == 0) { /* integer */ @@ -1074,7 +1094,7 @@ ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * case TYPE_PAIR(IS_DOUBLE, IS_LONG): if (Z_LVAL_P(op2) == 0) { zend_error(E_WARNING, "Division by zero"); - ZVAL_BOOL(result, 0); + ZVAL_FALSE(result); return FAILURE; /* division by zero */ } ZVAL_DOUBLE(result, Z_DVAL_P(op1) / (double)Z_LVAL_P(op2)); @@ -1083,7 +1103,7 @@ ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * case TYPE_PAIR(IS_LONG, IS_DOUBLE): if (Z_DVAL_P(op2) == 0) { zend_error(E_WARNING, "Division by zero"); - ZVAL_BOOL(result, 0); + ZVAL_FALSE(result); return FAILURE; /* division by zero */ } ZVAL_DOUBLE(result, (double)Z_LVAL_P(op1) / Z_DVAL_P(op2)); @@ -1092,15 +1112,19 @@ ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE): if (Z_DVAL_P(op2) == 0) { zend_error(E_WARNING, "Division by zero"); - ZVAL_BOOL(result, 0); + ZVAL_FALSE(result); return FAILURE; /* division by zero */ } ZVAL_DOUBLE(result, Z_DVAL_P(op1) / Z_DVAL_P(op2)); return SUCCESS; default: - if (!converted) { - ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_DIV); + if (Z_ISREF_P(op1)) { + op1 = Z_REFVAL_P(op1); + } else if (Z_ISREF_P(op2)) { + op2 = Z_REFVAL_P(op2); + } else if (!converted) { + ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_DIV, div_function); zendi_convert_scalar_to_number(op1, op1_copy, result); zendi_convert_scalar_to_number(op2, op2_copy, result); @@ -1114,76 +1138,111 @@ ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * } /* }}} */ -ZEND_API int mod_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int mod_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - zval op1_copy, op2_copy; - long op1_lval; + zend_long op1_lval, op2_lval; - if (Z_TYPE_P(op1) != IS_LONG || Z_TYPE_P(op2) != IS_LONG) { - ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_MOD); + convert_op1_op2_long(op1, op1_lval, op2, op2_lval, ZEND_MOD, mod_function); - zendi_convert_to_long(op1, op1_copy, result); - op1_lval = Z_LVAL_P(op1); - zendi_convert_to_long(op2, op2_copy, result); - } else { - op1_lval = Z_LVAL_P(op1); + if (op1 == result) { + zval_dtor(result); } - if (Z_LVAL_P(op2) == 0) { + if (op2_lval == 0) { zend_error(E_WARNING, "Division by zero"); - ZVAL_BOOL(result, 0); + ZVAL_FALSE(result); return FAILURE; /* modulus by zero */ } - if (Z_LVAL_P(op2) == -1) { + if (op2_lval == -1) { /* Prevent overflow error/crash if op1==LONG_MIN */ ZVAL_LONG(result, 0); return SUCCESS; } - ZVAL_LONG(result, op1_lval % Z_LVAL_P(op2)); + ZVAL_LONG(result, op1_lval % op2_lval); return SUCCESS; } /* }}} */ -ZEND_API int boolean_xor_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int boolean_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - zval op1_copy, op2_copy; - long op1_lval; + int op1_val, op2_val; - if (Z_TYPE_P(op1) != IS_BOOL || Z_TYPE_P(op2) != IS_BOOL) { - ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_BOOL_XOR); - - zendi_convert_to_boolean(op1, op1_copy, result); - op1_lval = Z_LVAL_P(op1); - zendi_convert_to_boolean(op2, op2_copy, result); - } else { - op1_lval = Z_LVAL_P(op1); - } + do { + if (Z_TYPE_P(op1) == IS_FALSE) { + op1_val = 0; + } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) { + op1_val = 1; + } else { + if (Z_ISREF_P(op1)) { + op1 = Z_REFVAL_P(op1); + if (Z_TYPE_P(op1) == IS_FALSE) { + op1_val = 0; + break; + } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) { + op1_val = 1; + break; + } + } + ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BOOL_XOR, boolean_xor_function); + op1_val = zval_is_true(op1); + } + } while (0); + do { + if (Z_TYPE_P(op2) == IS_FALSE) { + op2_val = 0; + } else if (EXPECTED(Z_TYPE_P(op2) == IS_TRUE)) { + op2_val = 1; + } else { + if (Z_ISREF_P(op2)) { + op2 = Z_REFVAL_P(op2); + if (Z_TYPE_P(op2) == IS_FALSE) { + op2_val = 0; + break; + } else if (EXPECTED(Z_TYPE_P(op2) == IS_TRUE)) { + op2_val = 1; + break; + } + } + ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BOOL_XOR); + op2_val = zval_is_true(op2); + } + } while (0); - ZVAL_BOOL(result, op1_lval ^ Z_LVAL_P(op2)); + ZVAL_BOOL(result, op1_val ^ op2_val); return SUCCESS; } /* }}} */ -ZEND_API int boolean_not_function(zval *result, zval *op1 TSRMLS_DC) /* {{{ */ +ZEND_API int boolean_not_function(zval *result, zval *op1) /* {{{ */ { - zval op1_copy; - - if (Z_TYPE_P(op1) != IS_BOOL) { + if (Z_TYPE_P(op1) < IS_TRUE) { + ZVAL_TRUE(result); + } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) { + ZVAL_FALSE(result); + } else { + if (Z_ISREF_P(op1)) { + op1 = Z_REFVAL_P(op1); + if (Z_TYPE_P(op1) < IS_TRUE) { + ZVAL_TRUE(result); + return SUCCESS; + } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) { + ZVAL_FALSE(result); + return SUCCESS; + } + } ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BOOL_NOT); - zendi_convert_to_boolean(op1, op1_copy, result); + ZVAL_BOOL(result, !zval_is_true(op1)); } - - ZVAL_BOOL(result, !Z_LVAL_P(op1)); return SUCCESS; } /* }}} */ -ZEND_API int bitwise_not_function(zval *result, zval *op1 TSRMLS_DC) /* {{{ */ +ZEND_API int bitwise_not_function(zval *result, zval *op1) /* {{{ */ { - +try_again: switch (Z_TYPE_P(op1)) { case IS_LONG: ZVAL_LONG(result, ~Z_LVAL_P(op1)); @@ -1192,17 +1251,18 @@ ZEND_API int bitwise_not_function(zval *result, zval *op1 TSRMLS_DC) /* {{{ */ ZVAL_LONG(result, ~zend_dval_to_lval(Z_DVAL_P(op1))); return SUCCESS; case IS_STRING: { - int i; - zval op1_copy = *op1; - - Z_TYPE_P(result) = IS_STRING; - Z_STRVAL_P(result) = estrndup(Z_STRVAL(op1_copy), Z_STRLEN(op1_copy)); - Z_STRLEN_P(result) = Z_STRLEN(op1_copy); - for (i = 0; i < Z_STRLEN(op1_copy); i++) { - Z_STRVAL_P(result)[i] = ~Z_STRVAL(op1_copy)[i]; + size_t i; + + ZVAL_NEW_STR(result, zend_string_alloc(Z_STRLEN_P(op1), 0)); + for (i = 0; i < Z_STRLEN_P(op1); i++) { + Z_STRVAL_P(result)[i] = ~Z_STRVAL_P(op1)[i]; } + Z_STRVAL_P(result)[i] = 0; return SUCCESS; } + case IS_REFERENCE: + op1 = Z_REFVAL_P(op1); + goto try_again; default: ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BW_NOT); @@ -1212,15 +1272,22 @@ ZEND_API int bitwise_not_function(zval *result, zval *op1 TSRMLS_DC) /* {{{ */ } /* }}} */ -ZEND_API int bitwise_or_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int bitwise_or_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - zval op1_copy, op2_copy; - long op1_lval; + zend_long op1_lval, op2_lval; + + if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) { + ZVAL_LONG(result, Z_LVAL_P(op1) | Z_LVAL_P(op2)); + return SUCCESS; + } + + ZVAL_DEREF(op1); + ZVAL_DEREF(op2); if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) { zval *longer, *shorter; - char *result_str; - int i, result_len; + zend_string *str; + size_t i; if (Z_STRLEN_P(op1) >= Z_STRLEN_P(op2)) { longer = op1; @@ -1230,44 +1297,55 @@ ZEND_API int bitwise_or_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) / shorter = op1; } - Z_TYPE_P(result) = IS_STRING; - result_len = Z_STRLEN_P(longer); - result_str = estrndup(Z_STRVAL_P(longer), Z_STRLEN_P(longer)); + str = zend_string_alloc(Z_STRLEN_P(longer), 0); for (i = 0; i < Z_STRLEN_P(shorter); i++) { - result_str[i] |= Z_STRVAL_P(shorter)[i]; + str->val[i] = Z_STRVAL_P(longer)[i] | Z_STRVAL_P(shorter)[i]; } + memcpy(str->val + i, Z_STRVAL_P(longer) + i, Z_STRLEN_P(longer) - i + 1); if (result==op1) { - str_efree(Z_STRVAL_P(result)); + zend_string_release(Z_STR_P(result)); } - Z_STRVAL_P(result) = result_str; - Z_STRLEN_P(result) = result_len; + ZVAL_NEW_STR(result, str); return SUCCESS; } - if (Z_TYPE_P(op1) != IS_LONG || Z_TYPE_P(op2) != IS_LONG) { - ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_BW_OR); - - zendi_convert_to_long(op1, op1_copy, result); - op1_lval = Z_LVAL_P(op1); - zendi_convert_to_long(op2, op2_copy, result); + if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) { + ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_OR, bitwise_or_function); + op1_lval = _zval_get_long_func(op1); } else { op1_lval = Z_LVAL_P(op1); } + if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) { + ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_OR); + op2_lval = _zval_get_long_func(op2); + } else { + op2_lval = Z_LVAL_P(op2); + } - ZVAL_LONG(result, op1_lval | Z_LVAL_P(op2)); + if (op1 == result) { + zval_dtor(result); + } + ZVAL_LONG(result, op1_lval | op2_lval); return SUCCESS; } /* }}} */ -ZEND_API int bitwise_and_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int bitwise_and_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - zval op1_copy, op2_copy; - long op1_lval; + zend_long op1_lval, op2_lval; + + if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) { + ZVAL_LONG(result, Z_LVAL_P(op1) & Z_LVAL_P(op2)); + return SUCCESS; + } + + ZVAL_DEREF(op1); + ZVAL_DEREF(op2); if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) { zval *longer, *shorter; - char *result_str; - int i, result_len; + zend_string *str; + size_t i; if (Z_STRLEN_P(op1) >= Z_STRLEN_P(op2)) { longer = op1; @@ -1277,44 +1355,55 @@ ZEND_API int bitwise_and_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) shorter = op1; } - Z_TYPE_P(result) = IS_STRING; - result_len = Z_STRLEN_P(shorter); - result_str = estrndup(Z_STRVAL_P(shorter), Z_STRLEN_P(shorter)); + str = zend_string_alloc(Z_STRLEN_P(shorter), 0); for (i = 0; i < Z_STRLEN_P(shorter); i++) { - result_str[i] &= Z_STRVAL_P(longer)[i]; + str->val[i] = Z_STRVAL_P(shorter)[i] & Z_STRVAL_P(longer)[i]; } + str->val[i] = 0; if (result==op1) { - str_efree(Z_STRVAL_P(result)); + zend_string_release(Z_STR_P(result)); } - Z_STRVAL_P(result) = result_str; - Z_STRLEN_P(result) = result_len; + ZVAL_NEW_STR(result, str); return SUCCESS; } - if (Z_TYPE_P(op1) != IS_LONG || Z_TYPE_P(op2) != IS_LONG) { - ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_BW_AND); - - zendi_convert_to_long(op1, op1_copy, result); - op1_lval = Z_LVAL_P(op1); - zendi_convert_to_long(op2, op2_copy, result); + if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) { + ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_AND, bitwise_or_function); + op1_lval = _zval_get_long_func(op1); } else { op1_lval = Z_LVAL_P(op1); } + if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) { + ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_AND); + op2_lval = _zval_get_long_func(op2); + } else { + op2_lval = Z_LVAL_P(op2); + } - ZVAL_LONG(result, op1_lval & Z_LVAL_P(op2)); + if (op1 == result) { + zval_dtor(result); + } + ZVAL_LONG(result, op1_lval & op2_lval); return SUCCESS; } /* }}} */ -ZEND_API int bitwise_xor_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int bitwise_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - zval op1_copy, op2_copy; - long op1_lval; + zend_long op1_lval, op2_lval; + + if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) { + ZVAL_LONG(result, Z_LVAL_P(op1) ^ Z_LVAL_P(op2)); + return SUCCESS; + } + + ZVAL_DEREF(op1); + ZVAL_DEREF(op2); if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) { zval *longer, *shorter; - char *result_str; - int i, result_len; + zend_string *str; + size_t i; if (Z_STRLEN_P(op1) >= Z_STRLEN_P(op2)) { longer = op1; @@ -1324,71 +1413,89 @@ ZEND_API int bitwise_xor_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) shorter = op1; } - Z_TYPE_P(result) = IS_STRING; - result_len = Z_STRLEN_P(shorter); - result_str = estrndup(Z_STRVAL_P(shorter), Z_STRLEN_P(shorter)); + str = zend_string_alloc(Z_STRLEN_P(shorter), 0); for (i = 0; i < Z_STRLEN_P(shorter); i++) { - result_str[i] ^= Z_STRVAL_P(longer)[i]; + str->val[i] = Z_STRVAL_P(shorter)[i] ^ Z_STRVAL_P(longer)[i]; } + str->val[i] = 0; if (result==op1) { - str_efree(Z_STRVAL_P(result)); + zend_string_release(Z_STR_P(result)); } - Z_STRVAL_P(result) = result_str; - Z_STRLEN_P(result) = result_len; + ZVAL_NEW_STR(result, str); return SUCCESS; } - if (Z_TYPE_P(op1) != IS_LONG || Z_TYPE_P(op2) != IS_LONG) { - ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_BW_XOR); - - zendi_convert_to_long(op1, op1_copy, result); - op1_lval = Z_LVAL_P(op1); - zendi_convert_to_long(op2, op2_copy, result); + if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) { + ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_XOR, bitwise_or_function); + op1_lval = _zval_get_long_func(op1); } else { op1_lval = Z_LVAL_P(op1); } + if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) { + ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_XOR); + op2_lval = _zval_get_long_func(op2); + } else { + op2_lval = Z_LVAL_P(op2); + } - ZVAL_LONG(result, op1_lval ^ Z_LVAL_P(op2)); + if (op1 == result) { + zval_dtor(result); + } + ZVAL_LONG(result, op1_lval ^ op2_lval); return SUCCESS; } /* }}} */ -ZEND_API int shift_left_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int shift_left_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - zval op1_copy, op2_copy; - long op1_lval; + zend_long op1_lval, op2_lval; - if (Z_TYPE_P(op1) != IS_LONG || Z_TYPE_P(op2) != IS_LONG) { - ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SL); + convert_op1_op2_long(op1, op1_lval, op2, op2_lval, ZEND_SL, shift_left_function); - zendi_convert_to_long(op1, op1_copy, result); - op1_lval = Z_LVAL_P(op1); - zendi_convert_to_long(op2, op2_copy, result); - } else { - op1_lval = Z_LVAL_P(op1); + if (op1 == result) { + zval_dtor(result); + } + + /* prevent wrapping quirkiness on some processors where << 64 + x == << x */ + if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) { + if (EXPECTED(op2_lval > 0)) { + ZVAL_LONG(result, 0); + return SUCCESS; + } else { + zend_error(E_WARNING, "Bit shift by negative number"); + ZVAL_FALSE(result); + return FAILURE; + } } - ZVAL_LONG(result, op1_lval << Z_LVAL_P(op2)); + ZVAL_LONG(result, op1_lval << op2_lval); return SUCCESS; } /* }}} */ -ZEND_API int shift_right_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int shift_right_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - zval op1_copy, op2_copy; - long op1_lval; + zend_long op1_lval, op2_lval; - if (Z_TYPE_P(op1) != IS_LONG || Z_TYPE_P(op2) != IS_LONG) { - ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SR); + convert_op1_op2_long(op1, op1_lval, op2, op2_lval, ZEND_SR, shift_right_function); - zendi_convert_to_long(op1, op1_copy, result); - op1_lval = Z_LVAL_P(op1); - zendi_convert_to_long(op2, op2_copy, result); - } else { - op1_lval = Z_LVAL_P(op1); + if (op1 == result) { + zval_dtor(result); + } + + /* prevent wrapping quirkiness on some processors where >> 64 + x == >> x */ + if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) { + if (EXPECTED(op2_lval > 0)) { + ZVAL_LONG(result, (op1_lval < 0) ? -1 : 0); + return SUCCESS; + } else { + zend_error(E_WARNING, "Bit shift by negative number"); + ZVAL_FALSE(result); + return FAILURE; + } } - ZVAL_LONG(result, op1_lval >> Z_LVAL_P(op2)); + ZVAL_LONG(result, op1_lval >> op2_lval); return SUCCESS; } /* }}} */ @@ -1396,12 +1503,12 @@ ZEND_API int shift_right_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* must support result==op1 */ ZEND_API int add_char_to_string(zval *result, const zval *op1, const zval *op2) /* {{{ */ { - int length = Z_STRLEN_P(op1) + 1; - char *buf = str_erealloc(Z_STRVAL_P(op1), length + 1); + size_t length = Z_STRLEN_P(op1) + 1; + zend_string *buf = zend_string_realloc(Z_STR_P(op1), length, 0); - buf[length - 1] = (char) Z_LVAL_P(op2); - buf[length] = 0; - ZVAL_STRINGL(result, buf, length, 0); + buf->val[length - 1] = (char) Z_LVAL_P(op2); + buf->val[length] = 0; + ZVAL_NEW_STR(result, buf); return SUCCESS; } /* }}} */ @@ -1409,194 +1516,202 @@ ZEND_API int add_char_to_string(zval *result, const zval *op1, const zval *op2) /* must support result==op1 */ ZEND_API int add_string_to_string(zval *result, const zval *op1, const zval *op2) /* {{{ */ { - int length = Z_STRLEN_P(op1) + Z_STRLEN_P(op2); - char *buf = str_erealloc(Z_STRVAL_P(op1), length + 1); + size_t op1_len = Z_STRLEN_P(op1); + size_t length = op1_len + Z_STRLEN_P(op2); + zend_string *buf = zend_string_realloc(Z_STR_P(op1), length, 0); - memcpy(buf + Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2)); - buf[length] = 0; - ZVAL_STRINGL(result, buf, length, 0); + memcpy(buf->val + op1_len, Z_STRVAL_P(op2), Z_STRLEN_P(op2)); + buf->val[length] = 0; + ZVAL_NEW_STR(result, buf); return SUCCESS; } /* }}} */ -ZEND_API int concat_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int concat_function(zval *result, zval *op1, zval *op2) /* {{{ */ { zval op1_copy, op2_copy; int use_copy1 = 0, use_copy2 = 0; - if (Z_TYPE_P(op1) != IS_STRING || Z_TYPE_P(op2) != IS_STRING) { - ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_CONCAT); - - if (Z_TYPE_P(op1) != IS_STRING) { - zend_make_printable_zval(op1, &op1_copy, &use_copy1); + do { + if (UNEXPECTED(Z_TYPE_P(op1) != IS_STRING)) { + if (Z_ISREF_P(op1)) { + op1 = Z_REFVAL_P(op1); + if (Z_TYPE_P(op1) == IS_STRING) break; + } + ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_CONCAT, concat_function); + use_copy1 = zend_make_printable_zval(op1, &op1_copy); + if (use_copy1) { + /* We have created a converted copy of op1. Therefore, op1 won't become the result so + * we have to free it. + */ + if (result == op1) { + zval_dtor(op1); + if (UNEXPECTED(op1 == op2)) { + op2 = &op1_copy; + } + } + op1 = &op1_copy; + } } - if (Z_TYPE_P(op2) != IS_STRING) { - zend_make_printable_zval(op2, &op2_copy, &use_copy2); + } while (0); + do { + if (UNEXPECTED(Z_TYPE_P(op2) != IS_STRING)) { + if (Z_ISREF_P(op2)) { + op2 = Z_REFVAL_P(op2); + if (Z_TYPE_P(op2) == IS_STRING) break; + } + ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_CONCAT); + use_copy2 = zend_make_printable_zval(op2, &op2_copy); + if (use_copy2) { + op2 = &op2_copy; + } } - } + } while (0); - if (use_copy1) { - /* We have created a converted copy of op1. Therefore, op1 won't become the result so - * we have to free it. - */ - if (result == op1) { - zval_dtor(op1); - } - op1 = &op1_copy; - } - if (use_copy2) { - op2 = &op2_copy; - } - if (result==op1 && !IS_INTERNED(Z_STRVAL_P(op1))) { /* special case, perform operations on result */ - uint res_len = Z_STRLEN_P(op1) + Z_STRLEN_P(op2); + { + size_t op1_len = Z_STRLEN_P(op1); + size_t op2_len = Z_STRLEN_P(op2); + size_t result_len = op1_len + op2_len; + zend_string *result_str; - if (Z_STRLEN_P(result) < 0 || (int) (Z_STRLEN_P(op1) + Z_STRLEN_P(op2)) < 0) { - efree(Z_STRVAL_P(result)); - ZVAL_EMPTY_STRING(result); - zend_error(E_ERROR, "String size overflow"); + if (op1_len > SIZE_MAX - op2_len) { + zend_error_noreturn(E_ERROR, "String size overflow"); } - Z_STRVAL_P(result) = erealloc(Z_STRVAL_P(result), res_len+1); + if (result == op1 && Z_REFCOUNTED_P(result)) { + /* special case, perform operations on result */ + result_str = zend_string_realloc(Z_STR_P(result), result_len, 0); + } else { + result_str = zend_string_alloc(result_len, 0); + memcpy(result_str->val, Z_STRVAL_P(op1), op1_len); + } - memcpy(Z_STRVAL_P(result)+Z_STRLEN_P(result), Z_STRVAL_P(op2), Z_STRLEN_P(op2)); - Z_STRVAL_P(result)[res_len]=0; - Z_STRLEN_P(result) = res_len; - } else { - int length = Z_STRLEN_P(op1) + Z_STRLEN_P(op2); - char *buf = (char *) emalloc(length + 1); + /* This has to happen first to account for the cases where result == op1 == op2 and + * the realloc is done. In this case this line will also update Z_STRVAL_P(op2) to + * point to the new string. The first op2_len bytes of result will still be the same. */ + ZVAL_NEW_STR(result, result_str); - memcpy(buf, Z_STRVAL_P(op1), Z_STRLEN_P(op1)); - memcpy(buf + Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2)); - buf[length] = 0; - ZVAL_STRINGL(result, buf, length, 0); + memcpy(result_str->val + op1_len, Z_STRVAL_P(op2), op2_len); + result_str->val[result_len] = '\0'; } - if (use_copy1) { + + if (UNEXPECTED(use_copy1)) { zval_dtor(op1); } - if (use_copy2) { + if (UNEXPECTED(use_copy2)) { zval_dtor(op2); } return SUCCESS; } /* }}} */ -ZEND_API int string_compare_function_ex(zval *result, zval *op1, zval *op2, zend_bool case_insensitive TSRMLS_DC) /* {{{ */ +ZEND_API int string_compare_function_ex(zval *result, zval *op1, zval *op2, zend_bool case_insensitive) /* {{{ */ { - zval op1_copy, op2_copy; - int use_copy1 = 0, use_copy2 = 0; - - if (Z_TYPE_P(op1) != IS_STRING) { - zend_make_printable_zval(op1, &op1_copy, &use_copy1); - } - if (Z_TYPE_P(op2) != IS_STRING) { - zend_make_printable_zval(op2, &op2_copy, &use_copy2); - } - - if (use_copy1) { - op1 = &op1_copy; - } - if (use_copy2) { - op2 = &op2_copy; - } + zend_string *str1 = zval_get_string(op1); + zend_string *str2 = zval_get_string(op2); if (case_insensitive) { - ZVAL_LONG(result, zend_binary_zval_strcasecmp(op1, op2)); + ZVAL_LONG(result, zend_binary_strcasecmp_l(str1->val, str1->len, str2->val, str1->len)); } else { - ZVAL_LONG(result, zend_binary_zval_strcmp(op1, op2)); + ZVAL_LONG(result, zend_binary_strcmp(str1->val, str1->len, str2->val, str2->len)); } - if (use_copy1) { - zval_dtor(op1); - } - if (use_copy2) { - zval_dtor(op2); - } + zend_string_release(str1); + zend_string_release(str2); return SUCCESS; } /* }}} */ -ZEND_API int string_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int string_compare_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - return string_compare_function_ex(result, op1, op2, 0 TSRMLS_CC); + if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) && + EXPECTED(Z_TYPE_P(op2) == IS_STRING)) { + if (Z_STR_P(op1) == Z_STR_P(op2)) { + ZVAL_LONG(result, 0); + } else { + ZVAL_LONG(result, zend_binary_strcmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2))); + } + } else { + zend_string *str1 = zval_get_string(op1); + zend_string *str2 = zval_get_string(op2); + + ZVAL_LONG(result, zend_binary_strcmp(str1->val, str1->len, str2->val, str2->len)); + + zend_string_release(str1); + zend_string_release(str2); + } + return SUCCESS; } /* }}} */ -ZEND_API int string_case_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int string_case_compare_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - return string_compare_function_ex(result, op1, op2, 1 TSRMLS_CC); + if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) && + EXPECTED(Z_TYPE_P(op2) == IS_STRING)) { + if (Z_STR_P(op1) == Z_STR_P(op2)) { + ZVAL_LONG(result, 0); + } else { + ZVAL_LONG(result, zend_binary_strcasecmp_l(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2))); + } + } else { + zend_string *str1 = zval_get_string(op1); + zend_string *str2 = zval_get_string(op2); + + ZVAL_LONG(result, zend_binary_strcasecmp_l(str1->val, str1->len, str2->val, str1->len)); + + zend_string_release(str1); + zend_string_release(str2); + } + return SUCCESS; } /* }}} */ #if HAVE_STRCOLL -ZEND_API int string_locale_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int string_locale_compare_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - zval op1_copy, op2_copy; - int use_copy1 = 0, use_copy2 = 0; - - if (Z_TYPE_P(op1) != IS_STRING) { - zend_make_printable_zval(op1, &op1_copy, &use_copy1); - } - if (Z_TYPE_P(op2) != IS_STRING) { - zend_make_printable_zval(op2, &op2_copy, &use_copy2); - } - - if (use_copy1) { - op1 = &op1_copy; - } - if (use_copy2) { - op2 = &op2_copy; - } + zend_string *str1 = zval_get_string(op1); + zend_string *str2 = zval_get_string(op2); - ZVAL_LONG(result, strcoll(Z_STRVAL_P(op1), Z_STRVAL_P(op2))); + ZVAL_LONG(result, strcoll(str1->val, str2->val)); - if (use_copy1) { - zval_dtor(op1); - } - if (use_copy2) { - zval_dtor(op2); - } + zend_string_release(str1); + zend_string_release(str2); return SUCCESS; } /* }}} */ #endif -ZEND_API int numeric_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int numeric_compare_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - zval op1_copy, op2_copy; - - op1_copy = *op1; - zval_copy_ctor(&op1_copy); + double d1, d2; - op2_copy = *op2; - zval_copy_ctor(&op2_copy); + d1 = zval_get_double(op1); + d2 = zval_get_double(op2); - convert_to_double(&op1_copy); - convert_to_double(&op2_copy); - - ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL(op1_copy)-Z_DVAL(op2_copy))); + ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(d1 - d2)); return SUCCESS; } /* }}} */ -static inline void zend_free_obj_get_result(zval *op TSRMLS_DC) /* {{{ */ +static inline void zend_free_obj_get_result(zval *op) /* {{{ */ { - if (Z_REFCOUNT_P(op) == 0) { - GC_REMOVE_ZVAL_FROM_BUFFER(op); - zval_dtor(op); - FREE_ZVAL(op); - } else { - zval_ptr_dtor(&op); + if (Z_REFCOUNTED_P(op)) { + if (Z_REFCOUNT_P(op) == 0) { + zval_dtor(op); + } else { + zval_ptr_dtor(op); + } } } /* }}} */ -ZEND_API int compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int compare_function(zval *result, zval *op1, zval *op2) /* {{{ */ { int ret; int converted = 0; zval op1_copy, op2_copy; - zval *op_free; + zval *op_free, tmp_free; while (1) { switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) { @@ -1624,35 +1739,39 @@ ZEND_API int compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* { return SUCCESS; case TYPE_PAIR(IS_ARRAY, IS_ARRAY): - zend_compare_arrays(result, op1, op2 TSRMLS_CC); + ZVAL_LONG(result, zend_compare_arrays(op1, op2)); return SUCCESS; case TYPE_PAIR(IS_NULL, IS_NULL): + case TYPE_PAIR(IS_NULL, IS_FALSE): + case TYPE_PAIR(IS_FALSE, IS_NULL): + case TYPE_PAIR(IS_FALSE, IS_FALSE): + case TYPE_PAIR(IS_TRUE, IS_TRUE): ZVAL_LONG(result, 0); return SUCCESS; - case TYPE_PAIR(IS_NULL, IS_BOOL): - ZVAL_LONG(result, Z_LVAL_P(op2) ? -1 : 0); - return SUCCESS; - - case TYPE_PAIR(IS_BOOL, IS_NULL): - ZVAL_LONG(result, Z_LVAL_P(op1) ? 1 : 0); + case TYPE_PAIR(IS_NULL, IS_TRUE): + ZVAL_LONG(result, -1); return SUCCESS; - case TYPE_PAIR(IS_BOOL, IS_BOOL): - ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_LVAL_P(op1) - Z_LVAL_P(op2))); + case TYPE_PAIR(IS_TRUE, IS_NULL): + ZVAL_LONG(result, 1); return SUCCESS; case TYPE_PAIR(IS_STRING, IS_STRING): - zendi_smart_strcmp(result, op1, op2); + if (Z_STR_P(op1) == Z_STR_P(op2)) { + ZVAL_LONG(result, 0); + return SUCCESS; + } + ZVAL_LONG(result, zendi_smart_strcmp(op1, op2)); return SUCCESS; case TYPE_PAIR(IS_NULL, IS_STRING): - ZVAL_LONG(result, zend_binary_strcmp("", 0, Z_STRVAL_P(op2), Z_STRLEN_P(op2))); + ZVAL_LONG(result, Z_STRLEN_P(op2) == 0 ? 0 : -1); return SUCCESS; case TYPE_PAIR(IS_STRING, IS_NULL): - ZVAL_LONG(result, zend_binary_strcmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), "", 0)); + ZVAL_LONG(result, Z_STRLEN_P(op1) == 0 ? 0 : 1); return SUCCESS; case TYPE_PAIR(IS_OBJECT, IS_NULL): @@ -1664,56 +1783,66 @@ ZEND_API int compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* { return SUCCESS; default: + if (Z_ISREF_P(op1)) { + op1 = Z_REFVAL_P(op1); + continue; + } else if (Z_ISREF_P(op2)) { + op2 = Z_REFVAL_P(op2); + continue; + } + if (Z_TYPE_P(op1) == IS_OBJECT && Z_OBJ_HANDLER_P(op1, compare)) { - return Z_OBJ_HANDLER_P(op1, compare)(result, op1, op2 TSRMLS_CC); + return Z_OBJ_HANDLER_P(op1, compare)(result, op1, op2); } else if (Z_TYPE_P(op2) == IS_OBJECT && Z_OBJ_HANDLER_P(op2, compare)) { - return Z_OBJ_HANDLER_P(op2, compare)(result, op1, op2 TSRMLS_CC); + return Z_OBJ_HANDLER_P(op2, compare)(result, op1, op2); } if (Z_TYPE_P(op1) == IS_OBJECT && Z_TYPE_P(op2) == IS_OBJECT) { - if (Z_OBJ_HANDLE_P(op1) == Z_OBJ_HANDLE_P(op2)) { + if (Z_OBJ_P(op1) == Z_OBJ_P(op2)) { /* object handles are identical, apparently this is the same object */ ZVAL_LONG(result, 0); return SUCCESS; } if (Z_OBJ_HANDLER_P(op1, compare_objects) == Z_OBJ_HANDLER_P(op2, compare_objects)) { - ZVAL_LONG(result, Z_OBJ_HANDLER_P(op1, compare_objects)(op1, op2 TSRMLS_CC)); + ZVAL_LONG(result, Z_OBJ_HANDLER_P(op1, compare_objects)(op1, op2)); return SUCCESS; } } if (Z_TYPE_P(op1) == IS_OBJECT) { if (Z_OBJ_HT_P(op1)->get) { - op_free = Z_OBJ_HT_P(op1)->get(op1 TSRMLS_CC); - ret = compare_function(result, op_free, op2 TSRMLS_CC); - zend_free_obj_get_result(op_free TSRMLS_CC); + zval rv; + op_free = Z_OBJ_HT_P(op1)->get(op1, &rv); + ret = compare_function(result, op_free, op2); + zend_free_obj_get_result(op_free); return ret; } else if (Z_TYPE_P(op2) != IS_OBJECT && Z_OBJ_HT_P(op1)->cast_object) { - ALLOC_INIT_ZVAL(op_free); - if (Z_OBJ_HT_P(op1)->cast_object(op1, op_free, Z_TYPE_P(op2) TSRMLS_CC) == FAILURE) { + ZVAL_UNDEF(&tmp_free); + if (Z_OBJ_HT_P(op1)->cast_object(op1, &tmp_free, ((Z_TYPE_P(op2) == IS_FALSE || Z_TYPE_P(op2) == IS_TRUE) ? _IS_BOOL : Z_TYPE_P(op2))) == FAILURE) { ZVAL_LONG(result, 1); - zend_free_obj_get_result(op_free TSRMLS_CC); + zend_free_obj_get_result(&tmp_free); return SUCCESS; } - ret = compare_function(result, op_free, op2 TSRMLS_CC); - zend_free_obj_get_result(op_free TSRMLS_CC); + ret = compare_function(result, &tmp_free, op2); + zend_free_obj_get_result(&tmp_free); return ret; } } if (Z_TYPE_P(op2) == IS_OBJECT) { if (Z_OBJ_HT_P(op2)->get) { - op_free = Z_OBJ_HT_P(op2)->get(op2 TSRMLS_CC); - ret = compare_function(result, op1, op_free TSRMLS_CC); - zend_free_obj_get_result(op_free TSRMLS_CC); + zval rv; + op_free = Z_OBJ_HT_P(op2)->get(op2, &rv); + ret = compare_function(result, op1, op_free); + zend_free_obj_get_result(op_free); return ret; } else if (Z_TYPE_P(op1) != IS_OBJECT && Z_OBJ_HT_P(op2)->cast_object) { - ALLOC_INIT_ZVAL(op_free); - if (Z_OBJ_HT_P(op2)->cast_object(op2, op_free, Z_TYPE_P(op1) TSRMLS_CC) == FAILURE) { + ZVAL_UNDEF(&tmp_free); + if (Z_OBJ_HT_P(op2)->cast_object(op2, &tmp_free, ((Z_TYPE_P(op1) == IS_FALSE || Z_TYPE_P(op1) == IS_TRUE) ? _IS_BOOL : Z_TYPE_P(op1))) == FAILURE) { ZVAL_LONG(result, -1); - zend_free_obj_get_result(op_free TSRMLS_CC); + zend_free_obj_get_result(&tmp_free); return SUCCESS; } - ret = compare_function(result, op1, op_free TSRMLS_CC); - zend_free_obj_get_result(op_free TSRMLS_CC); + ret = compare_function(result, op1, &tmp_free); + zend_free_obj_get_result(&tmp_free); return ret; } else if (Z_TYPE_P(op1) == IS_OBJECT) { ZVAL_LONG(result, 1); @@ -1721,21 +1850,17 @@ ZEND_API int compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* { } } if (!converted) { - if (Z_TYPE_P(op1) == IS_NULL) { - zendi_convert_to_boolean(op2, op2_copy, result); - ZVAL_LONG(result, Z_LVAL_P(op2) ? -1 : 0); + if (Z_TYPE_P(op1) == IS_NULL || Z_TYPE_P(op1) == IS_FALSE) { + ZVAL_LONG(result, zval_is_true(op2) ? -1 : 0); return SUCCESS; - } else if (Z_TYPE_P(op2) == IS_NULL) { - zendi_convert_to_boolean(op1, op1_copy, result); - ZVAL_LONG(result, Z_LVAL_P(op1) ? 1 : 0); + } else if (Z_TYPE_P(op2) == IS_NULL || Z_TYPE_P(op2) == IS_FALSE) { + ZVAL_LONG(result, zval_is_true(op1) ? 1 : 0); return SUCCESS; - } else if (Z_TYPE_P(op1) == IS_BOOL) { - zendi_convert_to_boolean(op2, op2_copy, result); - ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_LVAL_P(op1) - Z_LVAL_P(op2))); + } else if (Z_TYPE_P(op1) == IS_TRUE) { + ZVAL_LONG(result, zval_is_true(op2) ? 0 : 1); return SUCCESS; - } else if (Z_TYPE_P(op2) == IS_BOOL) { - zendi_convert_to_boolean(op1, op1_copy, result); - ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_LVAL_P(op1) - Z_LVAL_P(op2))); + } else if (Z_TYPE_P(op2) == IS_TRUE) { + ZVAL_LONG(result, zval_is_true(op1) ? 0 : -1); return SUCCESS; } else { zendi_convert_scalar_to_number(op1, op1_copy, result); @@ -1763,78 +1888,85 @@ ZEND_API int compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* { } /* }}} */ -static int hash_zval_identical_function(const zval **z1, const zval **z2) /* {{{ */ +static int hash_zval_identical_function(zval *z1, zval *z2) /* {{{ */ { zval result; - TSRMLS_FETCH(); /* is_identical_function() returns 1 in case of identity and 0 in case * of a difference; * whereas this comparison function is expected to return 0 on identity, * and non zero otherwise. */ - if (is_identical_function(&result, (zval *) *z1, (zval *) *z2 TSRMLS_CC)==FAILURE) { + ZVAL_DEREF(z1); + ZVAL_DEREF(z2); + if (is_identical_function(&result, z1, z2)==FAILURE) { return 1; } - return !Z_LVAL(result); + return Z_TYPE(result) != IS_TRUE; } /* }}} */ -ZEND_API int is_identical_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int is_identical_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - Z_TYPE_P(result) = IS_BOOL; if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) { - Z_LVAL_P(result) = 0; + ZVAL_FALSE(result); return SUCCESS; } switch (Z_TYPE_P(op1)) { case IS_NULL: - Z_LVAL_P(result) = 1; + case IS_FALSE: + case IS_TRUE: + ZVAL_TRUE(result); break; - case IS_BOOL: case IS_LONG: + ZVAL_BOOL(result, Z_LVAL_P(op1) == Z_LVAL_P(op2)); + break; case IS_RESOURCE: - Z_LVAL_P(result) = (Z_LVAL_P(op1) == Z_LVAL_P(op2)); + ZVAL_BOOL(result, Z_RES_P(op1) == Z_RES_P(op2)); break; case IS_DOUBLE: - Z_LVAL_P(result) = (Z_DVAL_P(op1) == Z_DVAL_P(op2)); + ZVAL_BOOL(result, Z_DVAL_P(op1) == Z_DVAL_P(op2)); break; case IS_STRING: - Z_LVAL_P(result) = ((Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) - && (!memcmp(Z_STRVAL_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op1)))); + if (Z_STR_P(op1) == Z_STR_P(op2)) { + ZVAL_TRUE(result); + } else { + ZVAL_BOOL(result, (Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) + && (!memcmp(Z_STRVAL_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op1)))); + } break; case IS_ARRAY: - Z_LVAL_P(result) = (Z_ARRVAL_P(op1) == Z_ARRVAL_P(op2) || - zend_hash_compare(Z_ARRVAL_P(op1), Z_ARRVAL_P(op2), (compare_func_t) hash_zval_identical_function, 1 TSRMLS_CC)==0); + ZVAL_BOOL(result, Z_ARRVAL_P(op1) == Z_ARRVAL_P(op2) || + zend_hash_compare(Z_ARRVAL_P(op1), Z_ARRVAL_P(op2), (compare_func_t) hash_zval_identical_function, 1)==0); break; case IS_OBJECT: if (Z_OBJ_HT_P(op1) == Z_OBJ_HT_P(op2)) { - Z_LVAL_P(result) = (Z_OBJ_HANDLE_P(op1) == Z_OBJ_HANDLE_P(op2)); + ZVAL_BOOL(result, Z_OBJ_P(op1) == Z_OBJ_P(op2)); } else { - Z_LVAL_P(result) = 0; + ZVAL_FALSE(result); } break; default: - Z_LVAL_P(result) = 0; + ZVAL_FALSE(result); return FAILURE; } return SUCCESS; } /* }}} */ -ZEND_API int is_not_identical_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int is_not_identical_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - if (is_identical_function(result, op1, op2 TSRMLS_CC) == FAILURE) { + if (is_identical_function(result, op1, op2) == FAILURE) { return FAILURE; } - Z_LVAL_P(result) = !Z_LVAL_P(result); + ZVAL_BOOL(result, Z_TYPE_P(result) != IS_TRUE); return SUCCESS; } /* }}} */ -ZEND_API int is_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int is_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - if (compare_function(result, op1, op2 TSRMLS_CC) == FAILURE) { + if (compare_function(result, op1, op2) == FAILURE) { return FAILURE; } ZVAL_BOOL(result, (Z_LVAL_P(result) == 0)); @@ -1842,9 +1974,9 @@ ZEND_API int is_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* } /* }}} */ -ZEND_API int is_not_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int is_not_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - if (compare_function(result, op1, op2 TSRMLS_CC) == FAILURE) { + if (compare_function(result, op1, op2) == FAILURE) { return FAILURE; } ZVAL_BOOL(result, (Z_LVAL_P(result) != 0)); @@ -1852,9 +1984,9 @@ ZEND_API int is_not_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) } /* }}} */ -ZEND_API int is_smaller_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int is_smaller_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - if (compare_function(result, op1, op2 TSRMLS_CC) == FAILURE) { + if (compare_function(result, op1, op2) == FAILURE) { return FAILURE; } ZVAL_BOOL(result, (Z_LVAL_P(result) < 0)); @@ -1862,9 +1994,9 @@ ZEND_API int is_smaller_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) / } /* }}} */ -ZEND_API int is_smaller_or_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +ZEND_API int is_smaller_or_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */ { - if (compare_function(result, op1, op2 TSRMLS_CC) == FAILURE) { + if (compare_function(result, op1, op2) == FAILURE) { return FAILURE; } ZVAL_BOOL(result, (Z_LVAL_P(result) <= 0)); @@ -1872,31 +2004,69 @@ ZEND_API int is_smaller_or_equal_function(zval *result, zval *op1, zval *op2 TSR } /* }}} */ -ZEND_API zend_bool instanceof_function_ex(const zend_class_entry *instance_ce, const zend_class_entry *ce, zend_bool interfaces_only TSRMLS_DC) /* {{{ */ +static zend_bool instanceof_interface_only(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */ { - zend_uint i; + uint32_t i; - for (i=0; i<instance_ce->num_interfaces; i++) { - if (instanceof_function(instance_ce->interfaces[i], ce TSRMLS_CC)) { + for (i = 0; i < instance_ce->num_interfaces; i++) { + if (instanceof_interface_only(instance_ce->interfaces[i], ce)) { return 1; } } - if (!interfaces_only) { - while (instance_ce) { - if (instance_ce == ce) { + return 0; +} +/* }}} */ + +static zend_always_inline zend_bool instanceof_class(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */ +{ + while (instance_ce) { + if (instance_ce == ce) { + return 1; + } + instance_ce = instance_ce->parent; + } + return 0; +} +/* }}} */ + +static zend_bool instanceof_interface(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */ +{ + uint32_t i; + + for (i = 0; i < instance_ce->num_interfaces; i++) { + if (instanceof_interface(instance_ce->interfaces[i], ce)) { + return 1; + } + } + return instanceof_class(instance_ce, ce); +} +/* }}} */ + +ZEND_API zend_bool instanceof_function_ex(const zend_class_entry *instance_ce, const zend_class_entry *ce, zend_bool interfaces_only) /* {{{ */ +{ + if (ce->ce_flags & ZEND_ACC_INTERFACE) { + if (!interfaces_only) { + if (instanceof_interface_only(instance_ce, ce)) { return 1; } - instance_ce = instance_ce->parent; + } else { + return instanceof_interface(instance_ce, ce); } } - + if (!interfaces_only) { + return instanceof_class(instance_ce, ce); + } return 0; } /* }}} */ -ZEND_API zend_bool instanceof_function(const zend_class_entry *instance_ce, const zend_class_entry *ce TSRMLS_DC) /* {{{ */ +ZEND_API zend_bool instanceof_function(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */ { - return instanceof_function_ex(instance_ce, ce, 0 TSRMLS_CC); + if (ce->ce_flags & ZEND_ACC_INTERFACE) { + return instanceof_interface(instance_ce, ce); + } else { + return instanceof_class(instance_ce, ce); + } } /* }}} */ @@ -1907,24 +2077,31 @@ ZEND_API zend_bool instanceof_function(const zend_class_entry *instance_ce, cons static void increment_string(zval *str) /* {{{ */ { int carry=0; - int pos=Z_STRLEN_P(str)-1; - char *s=Z_STRVAL_P(str); - char *t; + size_t pos=Z_STRLEN_P(str)-1; + char *s; + zend_string *t; int last=0; /* Shut up the compiler warning */ int ch; if (Z_STRLEN_P(str) == 0) { - str_efree(Z_STRVAL_P(str)); - Z_STRVAL_P(str) = estrndup("1", sizeof("1")-1); - Z_STRLEN_P(str) = 1; + zend_string_release(Z_STR_P(str)); + Z_STR_P(str) = zend_string_init("1", sizeof("1")-1, 0); + Z_TYPE_INFO_P(str) = IS_STRING_EX; return; } - if (IS_INTERNED(s)) { - Z_STRVAL_P(str) = s = estrndup(s, Z_STRLEN_P(str)); + if (!Z_REFCOUNTED_P(str)) { + Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0); + Z_TYPE_INFO_P(str) = IS_STRING_EX; + } else if (Z_REFCOUNT_P(str) > 1) { + Z_DELREF_P(str); + Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0); + } else { + zend_string_forget_hash_val(Z_STR_P(str)); } + s = Z_STRVAL_P(str); - while (pos >= 0) { + do { ch = s[pos]; if (ch >= 'a' && ch <= 'z') { if (ch == 'z') { @@ -1960,36 +2137,35 @@ static void increment_string(zval *str) /* {{{ */ if (carry == 0) { break; } - pos--; - } + } while (pos-- > 0); if (carry) { - t = (char *) emalloc(Z_STRLEN_P(str)+1+1); - memcpy(t+1, Z_STRVAL_P(str), Z_STRLEN_P(str)); - Z_STRLEN_P(str)++; - t[Z_STRLEN_P(str)] = '\0'; + t = zend_string_alloc(Z_STRLEN_P(str)+1, 0); + memcpy(t->val + 1, Z_STRVAL_P(str), Z_STRLEN_P(str)); + t->val[Z_STRLEN_P(str) + 1] = '\0'; switch (last) { case NUMERIC: - t[0] = '1'; + t->val[0] = '1'; break; case UPPER_CASE: - t[0] = 'A'; + t->val[0] = 'A'; break; case LOWER_CASE: - t[0] = 'a'; + t->val[0] = 'a'; break; } - str_efree(Z_STRVAL_P(str)); - Z_STRVAL_P(str) = t; + zend_string_free(Z_STR_P(str)); + ZVAL_NEW_STR(str, t); } } /* }}} */ ZEND_API int increment_function(zval *op1) /* {{{ */ { +try_again: switch (Z_TYPE_P(op1)) { case IS_LONG: - if (Z_LVAL_P(op1) == LONG_MAX) { + if (Z_LVAL_P(op1) == ZEND_LONG_MAX) { /* switch to double */ double d = (double)Z_LVAL_P(op1); ZVAL_DOUBLE(op1, d+1); @@ -2004,13 +2180,13 @@ ZEND_API int increment_function(zval *op1) /* {{{ */ ZVAL_LONG(op1, 1); break; case IS_STRING: { - long lval; + zend_long lval; double dval; switch (is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), &lval, &dval, 0)) { case IS_LONG: - str_efree(Z_STRVAL_P(op1)); - if (lval == LONG_MAX) { + zend_string_release(Z_STR_P(op1)); + if (lval == ZEND_LONG_MAX) { /* switch to double */ double d = (double)lval; ZVAL_DOUBLE(op1, d+1); @@ -2019,7 +2195,7 @@ ZEND_API int increment_function(zval *op1) /* {{{ */ } break; case IS_DOUBLE: - str_efree(Z_STRVAL_P(op1)); + zend_string_release(Z_STR_P(op1)); ZVAL_DOUBLE(op1, dval+1); break; default: @@ -2030,19 +2206,31 @@ ZEND_API int increment_function(zval *op1) /* {{{ */ } break; case IS_OBJECT: - if (Z_OBJ_HANDLER_P(op1, do_operation)) { - zval *op2; + if (Z_OBJ_HANDLER_P(op1, get) + && Z_OBJ_HANDLER_P(op1, set)) { + /* proxy object */ + zval rv; + zval *val; + + val = Z_OBJ_HANDLER_P(op1, get)(op1, &rv); + Z_ADDREF_P(val); + fast_increment_function(val); + Z_OBJ_HANDLER_P(op1, set)(op1, val); + zval_ptr_dtor(val); + } else if (Z_OBJ_HANDLER_P(op1, do_operation)) { + zval op2; int res; - TSRMLS_FETCH(); - MAKE_STD_ZVAL(op2); - ZVAL_LONG(op2, 1); - res = Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_ADD, op1, op1, op2 TSRMLS_CC); + ZVAL_LONG(&op2, 1); + res = Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_ADD, op1, op1, &op2); zval_ptr_dtor(&op2); return res; } return FAILURE; + case IS_REFERENCE: + op1 = Z_REFVAL_P(op1); + goto try_again; default: return FAILURE; } @@ -2052,12 +2240,13 @@ ZEND_API int increment_function(zval *op1) /* {{{ */ ZEND_API int decrement_function(zval *op1) /* {{{ */ { - long lval; + zend_long lval; double dval; +try_again: switch (Z_TYPE_P(op1)) { case IS_LONG: - if (Z_LVAL_P(op1) == LONG_MIN) { + if (Z_LVAL_P(op1) == ZEND_LONG_MIN) { double d = (double)Z_LVAL_P(op1); ZVAL_DOUBLE(op1, d-1); } else { @@ -2069,14 +2258,14 @@ ZEND_API int decrement_function(zval *op1) /* {{{ */ break; case IS_STRING: /* Like perl we only support string increment */ if (Z_STRLEN_P(op1) == 0) { /* consider as 0 */ - str_efree(Z_STRVAL_P(op1)); + zend_string_release(Z_STR_P(op1)); ZVAL_LONG(op1, -1); break; } switch (is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), &lval, &dval, 0)) { case IS_LONG: - str_efree(Z_STRVAL_P(op1)); - if (lval == LONG_MIN) { + zend_string_release(Z_STR_P(op1)); + if (lval == ZEND_LONG_MIN) { double d = (double)lval; ZVAL_DOUBLE(op1, d-1); } else { @@ -2084,25 +2273,37 @@ ZEND_API int decrement_function(zval *op1) /* {{{ */ } break; case IS_DOUBLE: - str_efree(Z_STRVAL_P(op1)); + zend_string_release(Z_STR_P(op1)); ZVAL_DOUBLE(op1, dval - 1); break; } break; case IS_OBJECT: - if (Z_OBJ_HANDLER_P(op1, do_operation)) { - zval *op2; + if (Z_OBJ_HANDLER_P(op1, get) + && Z_OBJ_HANDLER_P(op1, set)) { + /* proxy object */ + zval rv; + zval *val; + + val = Z_OBJ_HANDLER_P(op1, get)(op1, &rv); + Z_ADDREF_P(val); + fast_decrement_function(val); + Z_OBJ_HANDLER_P(op1, set)(op1, val); + zval_ptr_dtor(val); + } else if (Z_OBJ_HANDLER_P(op1, do_operation)) { + zval op2; int res; - TSRMLS_FETCH(); - MAKE_STD_ZVAL(op2); - ZVAL_LONG(op2, 1); - res = Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_SUB, op1, op1, op2 TSRMLS_CC); + ZVAL_LONG(&op2, 1); + res = Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_SUB, op1, op1, &op2); zval_ptr_dtor(&op2); return res; } return FAILURE; + case IS_REFERENCE: + op1 = Z_REFVAL_P(op1); + goto try_again; default: return FAILURE; } @@ -2111,10 +2312,33 @@ ZEND_API int decrement_function(zval *op1) /* {{{ */ } /* }}} */ -ZEND_API int zval_is_true(zval *op) /* {{{ */ +ZEND_API int zend_is_true(zval *op) /* {{{ */ { - convert_to_boolean(op); - return (Z_LVAL_P(op) ? 1 : 0); + return i_zend_is_true(op); +} +/* }}} */ + +ZEND_API int zend_object_is_true(zval *op) /* {{{ */ +{ + if (Z_OBJ_HT_P(op)->cast_object) { + zval tmp; + if (Z_OBJ_HT_P(op)->cast_object(op, &tmp, _IS_BOOL) == SUCCESS) { + return Z_TYPE(tmp) == IS_TRUE; + } + zend_error(E_RECOVERABLE_ERROR, "Object of class %s could not be converted to boolean", Z_OBJ_P(op)->ce->name->val); + } else if (Z_OBJ_HT_P(op)->get) { + int result; + zval rv; + zval *tmp = Z_OBJ_HT_P(op)->get(op, &rv); + + if (Z_TYPE_P(tmp) != IS_OBJECT) { + /* for safety - avoid loop */ + result = i_zend_is_true(tmp); + zval_ptr_dtor(tmp); + return result; + } + } + return 1; } /* }}} */ @@ -2126,7 +2350,7 @@ ZEND_API void zend_update_current_locale(void) /* {{{ */ /* }}} */ #endif -ZEND_API char *zend_str_tolower_copy(char *dest, const char *source, unsigned int length) /* {{{ */ +ZEND_API char *zend_str_tolower_copy(char *dest, const char *source, size_t length) /* {{{ */ { register unsigned char *str = (unsigned char*)source; register unsigned char *result = (unsigned char*)dest; @@ -2141,13 +2365,13 @@ ZEND_API char *zend_str_tolower_copy(char *dest, const char *source, unsigned in } /* }}} */ -ZEND_API char *zend_str_tolower_dup(const char *source, unsigned int length) /* {{{ */ +ZEND_API char *zend_str_tolower_dup(const char *source, size_t length) /* {{{ */ { return zend_str_tolower_copy((char *)emalloc(length+1), source, length); } /* }}} */ -ZEND_API void zend_str_tolower(char *str, unsigned int length) /* {{{ */ +ZEND_API void zend_str_tolower(char *str, size_t length) /* {{{ */ { register unsigned char *p = (unsigned char*)str; register unsigned char *end = p + length; @@ -2159,7 +2383,35 @@ ZEND_API void zend_str_tolower(char *str, unsigned int length) /* {{{ */ } /* }}} */ -ZEND_API int zend_binary_strcmp(const char *s1, uint len1, const char *s2, uint len2) /* {{{ */ +ZEND_API zend_string *zend_string_tolower(zend_string *str) /* {{{ */ +{ + register unsigned char *p = (unsigned char*)str->val; + register unsigned char *end = p + str->len; + + while (p < end) { + if (*p != zend_tolower_ascii(*p)) { + zend_string *res = zend_string_alloc(str->len, 0); + register unsigned char *r; + + if (p != (unsigned char*)str->val) { + memcpy(res->val, str->val, p - (unsigned char*)str->val); + } + r = p + (res->val - str->val); + while (p < end) { + *r = zend_tolower_ascii(*p); + p++; + r++; + } + *r = '\0'; + return res; + } + p++; + } + return zend_string_copy(str); +} +/* }}} */ + +ZEND_API int zend_binary_strcmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */ { int retval; @@ -2168,14 +2420,14 @@ ZEND_API int zend_binary_strcmp(const char *s1, uint len1, const char *s2, uint } retval = memcmp(s1, s2, MIN(len1, len2)); if (!retval) { - return (len1 - len2); + return (int)(len1 - len2); } else { return retval; } } /* }}} */ -ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint len2, uint length) /* {{{ */ +ZEND_API int zend_binary_strncmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */ { int retval; @@ -2184,16 +2436,16 @@ ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint } retval = memcmp(s1, s2, MIN(length, MIN(len1, len2))); if (!retval) { - return (MIN(length, len1) - MIN(length, len2)); + return (int)(MIN(length, len1) - MIN(length, len2)); } else { return retval; } } /* }}} */ -ZEND_API int zend_binary_strcasecmp(const char *s1, uint len1, const char *s2, uint len2) /* {{{ */ +ZEND_API int zend_binary_strcasecmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */ { - int len; + size_t len; int c1, c2; if (s1 == s2) { @@ -2209,13 +2461,13 @@ ZEND_API int zend_binary_strcasecmp(const char *s1, uint len1, const char *s2, u } } - return len1 - len2; + return (int)(len1 - len2); } /* }}} */ -ZEND_API int zend_binary_strncasecmp(const char *s1, uint len1, const char *s2, uint len2, uint length) /* {{{ */ +ZEND_API int zend_binary_strncasecmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */ { - int len; + size_t len; int c1, c2; if (s1 == s2) { @@ -2230,13 +2482,13 @@ ZEND_API int zend_binary_strncasecmp(const char *s1, uint len1, const char *s2, } } - return MIN(length, len1) - MIN(length, len2); + return (int)(MIN(length, len1) - MIN(length, len2)); } /* }}} */ -ZEND_API int zend_binary_strcasecmp_l(const char *s1, uint len1, const char *s2, uint len2) /* {{{ */ +ZEND_API int zend_binary_strcasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */ { - int len; + size_t len; int c1, c2; if (s1 == s2) { @@ -2252,13 +2504,13 @@ ZEND_API int zend_binary_strcasecmp_l(const char *s1, uint len1, const char *s2, } } - return len1 - len2; + return (int)(len1 - len2); } /* }}} */ -ZEND_API int zend_binary_strncasecmp_l(const char *s1, uint len1, const char *s2, uint len2, uint length) /* {{{ */ +ZEND_API int zend_binary_strncasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */ { - int len; + size_t len; int c1, c2; if (s1 == s2) { @@ -2273,7 +2525,7 @@ ZEND_API int zend_binary_strncasecmp_l(const char *s1, uint len1, const char *s2 } } - return MIN(length, len1) - MIN(length, len2); + return (int)(MIN(length, len1) - MIN(length, len2)); } /* }}} */ @@ -2301,16 +2553,16 @@ ZEND_API int zend_binary_zval_strncasecmp(zval *s1, zval *s2, zval *s3) /* {{{ * } /* }}} */ -ZEND_API void zendi_smart_strcmp(zval *result, zval *s1, zval *s2) /* {{{ */ +ZEND_API zend_long zendi_smart_strcmp(zval *s1, zval *s2) /* {{{ */ { int ret1, ret2; int oflow1, oflow2; - long lval1 = 0, lval2 = 0; + zend_long lval1 = 0, lval2 = 0; double dval1 = 0.0, dval2 = 0.0; - if ((ret1=is_numeric_string_ex(Z_STRVAL_P(s1), Z_STRLEN_P(s1), &lval1, &dval1, 0, &oflow1)) && - (ret2=is_numeric_string_ex(Z_STRVAL_P(s2), Z_STRLEN_P(s2), &lval2, &dval2, 0, &oflow2))) { -#if ULONG_MAX == 0xFFFFFFFF + if ((ret1 = is_numeric_string_ex(Z_STRVAL_P(s1), Z_STRLEN_P(s1), &lval1, &dval1, 0, &oflow1)) && + (ret2 = is_numeric_string_ex(Z_STRVAL_P(s2), Z_STRLEN_P(s2), &lval2, &dval2, 0, &oflow2))) { +#if ZEND_ULONG_MAX == 0xFFFFFFFF if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0. && ((oflow1 == 1 && dval1 > 9007199254740991. /*0x1FFFFFFFFFFFFF*/) || (oflow1 == -1 && dval1 < -9007199254740991.))) { @@ -2321,18 +2573,16 @@ ZEND_API void zendi_smart_strcmp(zval *result, zval *s1, zval *s2) /* {{{ */ * double comparison may have resulted in crucial accuracy lost */ goto string_cmp; } - if ((ret1==IS_DOUBLE) || (ret2==IS_DOUBLE)) { - if (ret1!=IS_DOUBLE) { + if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) { + if (ret1 != IS_DOUBLE) { if (oflow2) { /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */ - ZVAL_LONG(result, -1 * oflow2); - return; + return -1 * oflow2; } dval1 = (double) lval1; - } else if (ret2!=IS_DOUBLE) { + } else if (ret2 != IS_DOUBLE) { if (oflow1) { - ZVAL_LONG(result, oflow1); - return; + return oflow1; } dval2 = (double) lval2; } else if (dval1 == dval2 && !zend_finite(dval1)) { @@ -2340,70 +2590,305 @@ ZEND_API void zendi_smart_strcmp(zval *result, zval *s1, zval *s2) /* {{{ */ * so a numeric comparison would be inaccurate */ goto string_cmp; } - Z_DVAL_P(result) = dval1 - dval2; - ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL_P(result))); + dval1 = dval1 - dval2; + return ZEND_NORMALIZE_BOOL(dval1); } else { /* they both have to be long's */ - ZVAL_LONG(result, lval1 > lval2 ? 1 : (lval1 < lval2 ? -1 : 0)); + return lval1 > lval2 ? 1 : (lval1 < lval2 ? -1 : 0); } } else { + int strcmp_ret; string_cmp: - Z_LVAL_P(result) = zend_binary_zval_strcmp(s1, s2); - ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_LVAL_P(result))); + strcmp_ret = zend_binary_strcmp(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2)); + return ZEND_NORMALIZE_BOOL(strcmp_ret); } } /* }}} */ -static int hash_zval_compare_function(const zval **z1, const zval **z2 TSRMLS_DC) /* {{{ */ +static int hash_zval_compare_function(zval *z1, zval *z2) /* {{{ */ { zval result; - if (compare_function(&result, (zval *) *z1, (zval *) *z2 TSRMLS_CC)==FAILURE) { + if (compare_function(&result, z1, z2)==FAILURE) { return 1; } return Z_LVAL(result); } /* }}} */ -ZEND_API int zend_compare_symbol_tables_i(HashTable *ht1, HashTable *ht2 TSRMLS_DC) /* {{{ */ +ZEND_API int zend_compare_symbol_tables(HashTable *ht1, HashTable *ht2) /* {{{ */ { - return ht1 == ht2 ? 0 : zend_hash_compare(ht1, ht2, (compare_func_t) hash_zval_compare_function, 0 TSRMLS_CC); + return ht1 == ht2 ? 0 : zend_hash_compare(ht1, ht2, (compare_func_t) hash_zval_compare_function, 0); } /* }}} */ -ZEND_API void zend_compare_symbol_tables(zval *result, HashTable *ht1, HashTable *ht2 TSRMLS_DC) /* {{{ */ +ZEND_API int zend_compare_arrays(zval *a1, zval *a2) /* {{{ */ { - ZVAL_LONG(result, ht1 == ht2 ? 0 : zend_hash_compare(ht1, ht2, (compare_func_t) hash_zval_compare_function, 0 TSRMLS_CC)); + return zend_compare_symbol_tables(Z_ARRVAL_P(a1), Z_ARRVAL_P(a2)); } /* }}} */ -ZEND_API void zend_compare_arrays(zval *result, zval *a1, zval *a2 TSRMLS_DC) /* {{{ */ +ZEND_API int zend_compare_objects(zval *o1, zval *o2) /* {{{ */ { - zend_compare_symbol_tables(result, Z_ARRVAL_P(a1), Z_ARRVAL_P(a2) TSRMLS_CC); + if (Z_OBJ_P(o1) == Z_OBJ_P(o2)) { + return 0; + } + + if (Z_OBJ_HT_P(o1)->compare_objects == NULL) { + return 1; + } else { + return Z_OBJ_HT_P(o1)->compare_objects(o1, o2); + } } /* }}} */ -ZEND_API void zend_compare_objects(zval *result, zval *o1, zval *o2 TSRMLS_DC) /* {{{ */ +ZEND_API void zend_locale_sprintf_double(zval *op ZEND_FILE_LINE_DC) /* {{{ */ { - Z_TYPE_P(result) = IS_LONG; + zend_string *str; - if (Z_OBJ_HANDLE_P(o1) == Z_OBJ_HANDLE_P(o2)) { - Z_LVAL_P(result) = 0; - return; + str = zend_strpprintf(0, "%.*G", (int) EG(precision), (double)Z_DVAL_P(op)); + ZVAL_NEW_STR(op, str); +} +/* }}} */ + +ZEND_API zend_string *zend_long_to_str(zend_long num) /* {{{ */ +{ + char buf[MAX_LENGTH_OF_LONG + 1]; + char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, num); + return zend_string_init(res, buf + sizeof(buf) - 1 - res, 0); +} +/* }}} */ + +ZEND_API zend_uchar is_numeric_str_function(const zend_string *str, zend_long *lval, double *dval) /* {{{ */ { + return is_numeric_string_ex(str->val, str->len, lval, dval, -1, NULL); +} +/* }}} */ + +ZEND_API zend_uchar _is_numeric_string_ex(const char *str, size_t length, zend_long *lval, double *dval, int allow_errors, int *oflow_info) /* {{{ */ +{ + const char *ptr; + int base = 10, digits = 0, dp_or_e = 0; + double local_dval = 0.0; + zend_uchar type; + + if (!length) { + return 0; } - if (Z_OBJ_HT_P(o1)->compare_objects == NULL) { - Z_LVAL_P(result) = 1; + if (oflow_info != NULL) { + *oflow_info = 0; + } + + /* Skip any whitespace + * This is much faster than the isspace() function */ + while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\v' || *str == '\f') { + str++; + length--; + } + ptr = str; + + if (*ptr == '-' || *ptr == '+') { + ptr++; + } + + if (ZEND_IS_DIGIT(*ptr)) { + /* Handle hex numbers + * str is used instead of ptr to disallow signs and keep old behavior */ + if (length > 2 && *str == '0' && (str[1] == 'x' || str[1] == 'X')) { + base = 16; + ptr += 2; + } + + /* Skip any leading 0s */ + while (*ptr == '0') { + ptr++; + } + + /* Count the number of digits. If a decimal point/exponent is found, + * it's a double. Otherwise, if there's a dval or no need to check for + * a full match, stop when there are too many digits for a long */ + for (type = IS_LONG; !(digits >= MAX_LENGTH_OF_LONG && (dval || allow_errors == 1)); digits++, ptr++) { +check_digits: + if (ZEND_IS_DIGIT(*ptr) || (base == 16 && ZEND_IS_XDIGIT(*ptr))) { + continue; + } else if (base == 10) { + if (*ptr == '.' && dp_or_e < 1) { + goto process_double; + } else if ((*ptr == 'e' || *ptr == 'E') && dp_or_e < 2) { + const char *e = ptr + 1; + + if (*e == '-' || *e == '+') { + ptr = e++; + } + if (ZEND_IS_DIGIT(*e)) { + goto process_double; + } + } + } + + break; + } + + if (base == 10) { + if (digits >= MAX_LENGTH_OF_LONG) { + if (oflow_info != NULL) { + *oflow_info = *str == '-' ? -1 : 1; + } + dp_or_e = -1; + goto process_double; + } + } else if (!(digits < SIZEOF_ZEND_LONG * 2 || (digits == SIZEOF_ZEND_LONG * 2 && ptr[-digits] <= '7'))) { + if (dval) { + local_dval = zend_hex_strtod(str, &ptr); + } + if (oflow_info != NULL) { + *oflow_info = 1; + } + type = IS_DOUBLE; + } + } else if (*ptr == '.' && ZEND_IS_DIGIT(ptr[1])) { +process_double: + type = IS_DOUBLE; + + /* If there's a dval, do the conversion; else continue checking + * the digits if we need to check for a full match */ + if (dval) { + local_dval = zend_strtod(str, &ptr); + } else if (allow_errors != 1 && dp_or_e != -1) { + dp_or_e = (*ptr++ == '.') ? 1 : 2; + goto check_digits; + } + } else { + return 0; + } + + if (ptr != str + length) { + if (!allow_errors) { + return 0; + } + if (allow_errors == -1) { + zend_error(E_NOTICE, "A non well formed numeric value encountered"); + } + } + + if (type == IS_LONG) { + if (digits == MAX_LENGTH_OF_LONG - 1) { + int cmp = strcmp(&ptr[-digits], long_min_digits); + + if (!(cmp < 0 || (cmp == 0 && *str == '-'))) { + if (dval) { + *dval = zend_strtod(str, NULL); + } + if (oflow_info != NULL) { + *oflow_info = *str == '-' ? -1 : 1; + } + + return IS_DOUBLE; + } + } + + if (lval) { + *lval = ZEND_STRTOL(str, NULL, base); + } + + return IS_LONG; } else { - Z_LVAL_P(result) = Z_OBJ_HT_P(o1)->compare_objects(o1, o2 TSRMLS_CC); + if (dval) { + *dval = local_dval; + } + + return IS_DOUBLE; } } /* }}} */ -ZEND_API void zend_locale_sprintf_double(zval *op ZEND_FILE_LINE_DC) /* {{{ */ +/* + * String matching - Sunday algorithm + * http://www.iti.fh-flensburg.de/lang/algorithmen/pattern/sundayen.htm + */ +static zend_always_inline void zend_memnstr_ex_pre(unsigned int td[], const char *needle, size_t needle_len, int reverse) /* {{{ */ { + int i; + + for (i = 0; i < 256; i++) { + td[i] = needle_len + 1; + } + + if (reverse) { + for (i = needle_len - 1; i >= 0; i--) { + td[(unsigned char)needle[i]] = i + 1; + } + } else { + for (i = 0; i < needle_len; i++) { + td[(unsigned char)needle[i]] = (int)needle_len - i; + } + } +} +/* }}} */ + +ZEND_API const char* zend_memnstr_ex(const char *haystack, const char *needle, size_t needle_len, char *end) /* {{{ */ { - TSRMLS_FETCH(); + unsigned int td[256]; + register size_t i; + register const char *p; + + if (needle_len == 0 || (end - haystack) == 0) { + return NULL; + } + + zend_memnstr_ex_pre(td, needle, needle_len, 0); + + p = haystack; + end -= needle_len; + + while (p <= end) { + for (i = 0; i < needle_len; i++) { + if (needle[i] != p[i]) { + break; + } + } + if (i == needle_len) { + return p; + } + p += td[(unsigned char)(p[needle_len])]; + } + + return NULL; +} +/* }}} */ + +ZEND_API const char* zend_memnrstr_ex(const char *haystack, const char *needle, size_t needle_len, char *end) /* {{{ */ +{ + unsigned int td[256]; + register size_t i; + register const char *p; + + if (needle_len == 0 || (end - haystack) == 0) { + return NULL; + } + + zend_memnstr_ex_pre(td, needle, needle_len, 1); + + p = end; + p -= needle_len; + + while (p >= haystack) { + for (i = 0; i < needle_len; i++) { + if (needle[i] != p[i]) { + break; + } + } + + if (i == needle_len) { + return (const char *)p; + } + + if (UNEXPECTED(p == haystack)) { + return NULL; + } + + p -= td[(unsigned char)(p[-1])]; + } - Z_STRLEN_P(op) = zend_spprintf(&Z_STRVAL_P(op), 0, "%.*G", (int) EG(precision), (double)Z_DVAL_P(op)); + return NULL; } /* }}} */ |
