From 04c8ce259fb422c7bcdac3d72bacbf0ee910d772 Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Mon, 30 Dec 2013 10:50:46 +0800 Subject: [PATCH 1/8] Fixed bug #66298 (ext/opcache/Optimizer/zend_optimizer.c has dos-style ^M as lineend) Use git blame -w to ingore the space changes --- NEWS | 2 + ext/opcache/Optimizer/zend_optimizer.c | 702 +++++++++--------- ext/opcache/Optimizer/zend_optimizer.h | 98 +-- .../Optimizer/zend_optimizer_internal.h | 172 ++--- 4 files changed, 488 insertions(+), 486 deletions(-) diff --git a/NEWS b/NEWS index aed16cce65..615f539e66 100644 --- a/NEWS +++ b/NEWS @@ -42,6 +42,8 @@ PHP NEWS . Fixed bug #65486 (mysqli_poll() is broken on win x64). (Anatol) - OPCache: + . Fixed bug #66298 (ext/opcache/Optimizer/zend_optimizer.c has dos-style + ^M as lineend). (Laruence) . Fixed reavlidate_path=1 behavior to avoid caching of symlinks values. (Dmitry) . Fixed Issue #140: "opcache.enable_file_override" doesn't respect diff --git a/ext/opcache/Optimizer/zend_optimizer.c b/ext/opcache/Optimizer/zend_optimizer.c index c932530128..2c96c3b0ce 100644 --- a/ext/opcache/Optimizer/zend_optimizer.c +++ b/ext/opcache/Optimizer/zend_optimizer.c @@ -1,351 +1,351 @@ -/* - +----------------------------------------------------------------------+ - | Zend OPcache | - +----------------------------------------------------------------------+ - | Copyright (c) 1998-2013 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Authors: Andi Gutmans | - | Zeev Suraski | - | Stanislav Malyshev | - | Dmitry Stogov | - +----------------------------------------------------------------------+ -*/ - -#include "php.h" -#include "Optimizer/zend_optimizer.h" -#include "Optimizer/zend_optimizer_internal.h" -#include "zend_API.h" -#include "zend_constants.h" -#include "zend_execute.h" - -#define OPTIMIZATION_LEVEL \ - ZCG(accel_directives).optimization_level - -#if ZEND_EXTENSION_API_NO >= PHP_5_5_X_API_NO -static int zend_optimizer_lookup_cv(zend_op_array *op_array, char* name, int name_len) -{ - int i = 0; - ulong hash_value = zend_inline_hash_func(name, name_len+1); - - while (i < op_array->last_var) { - if (op_array->vars[i].name == name || - (op_array->vars[i].hash_value == hash_value && - op_array->vars[i].name_len == name_len && - memcmp(op_array->vars[i].name, name, name_len) == 0)) { - return i; - } - i++; - } - i = op_array->last_var; - op_array->last_var++; - op_array->vars = erealloc(op_array->vars, op_array->last_var * sizeof(zend_compiled_variable)); - if (IS_INTERNED(name)) { - op_array->vars[i].name = name; - } else { - op_array->vars[i].name = estrndup(name, name_len); - } - op_array->vars[i].name_len = name_len; - op_array->vars[i].hash_value = hash_value; - return i; -} -#endif - -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO -int zend_optimizer_add_literal(zend_op_array *op_array, const zval *zv TSRMLS_DC) -{ - int i = op_array->last_literal; - op_array->last_literal++; - if (i >= CG(context).literals_size) { - CG(context).literals_size += 16; /* FIXME */ - op_array->literals = (zend_literal*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zend_literal)); - } - op_array->literals[i].constant = *zv; - op_array->literals[i].hash_value = 0; - op_array->literals[i].cache_slot = -1; - Z_SET_REFCOUNT(op_array->literals[i].constant, 2); - Z_SET_ISREF(op_array->literals[i].constant); - return i; -} - -# define LITERAL_LONG(op, val) do { \ - zval _c; \ - ZVAL_LONG(&_c, val); \ - op.constant = zend_optimizer_add_literal(op_array, &_c TSRMLS_CC); \ - } while (0) - -# define LITERAL_BOOL(op, val) do { \ - zval _c; \ - ZVAL_BOOL(&_c, val); \ - op.constant = zend_optimizer_add_literal(op_array, &_c TSRMLS_CC); \ - } while (0) - -# define literal_dtor(zv) do { \ - zval_dtor(zv); \ - Z_TYPE_P(zv) = IS_NULL; \ - } while (0) - -#define COPY_NODE(target, src) do { \ - target ## _type = src ## _type; \ - target = src; \ - } while (0) - -#else - -# define LITERAL_LONG(op, val) ZVAL_LONG(&op.u.constant, val) - -# define LITERAL_BOOL(op, val) ZVAL_BOOL(&op.u.constant, val) - -# define literal_dtor(zv) zval_dtor(zv) - -#define COPY_NODE(target, src) do { \ - target = src; \ - } while (0) - -#endif - -static void update_op1_const(zend_op_array *op_array, - zend_op *opline, - zval *val TSRMLS_DC) -{ - if (opline->opcode == ZEND_FREE) { - MAKE_NOP(opline); - zval_dtor(val); - } else { - ZEND_OP1_TYPE(opline) = IS_CONST; -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO - if (Z_TYPE_P(val) == IS_STRING) { - switch (opline->opcode) { - case ZEND_INIT_STATIC_METHOD_CALL: - case ZEND_CATCH: - case ZEND_FETCH_CONSTANT: - opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); - Z_HASH_P(&ZEND_OP1_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)) + 1); - op_array->literals[opline->op1.constant].cache_slot = op_array->last_cache_slot++; - zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); - zend_optimizer_add_literal(op_array, val TSRMLS_CC); - op_array->literals[opline->op1.constant+1].hash_value = zend_hash_func(Z_STRVAL(op_array->literals[opline->op1.constant+1].constant), Z_STRLEN(op_array->literals[opline->op1.constant+1].constant) + 1); - break; - case ZEND_DO_FCALL: - zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); - opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); - Z_HASH_P(&ZEND_OP1_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)) + 1); - op_array->literals[opline->op1.constant].cache_slot = op_array->last_cache_slot++; - break; - default: - opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); - Z_HASH_P(&ZEND_OP1_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)) + 1); - break; - } - } else { - opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); - } -#else - ZEND_OP1_LITERAL(opline) = *val; -#endif - } -} - -static void update_op2_const(zend_op_array *op_array, - zend_op *opline, - zval *val TSRMLS_DC) -{ - ZEND_OP2_TYPE(opline) = IS_CONST; -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO - opline->op2.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); - if (Z_TYPE_P(val) == IS_STRING) { - Z_HASH_P(&ZEND_OP2_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP2_LITERAL(opline)), Z_STRLEN(ZEND_OP2_LITERAL(opline)) + 1); - switch (opline->opcode) { - case ZEND_FETCH_R: - case ZEND_FETCH_W: - case ZEND_FETCH_RW: - case ZEND_FETCH_IS: - case ZEND_FETCH_UNSET: - case ZEND_FETCH_FUNC_ARG: - case ZEND_FETCH_CLASS: - case ZEND_INIT_FCALL_BY_NAME: - /*case ZEND_INIT_NS_FCALL_BY_NAME:*/ - case ZEND_UNSET_VAR: - case ZEND_ISSET_ISEMPTY_VAR: - case ZEND_ADD_INTERFACE: - case ZEND_ADD_TRAIT: - op_array->literals[opline->op2.constant].cache_slot = op_array->last_cache_slot++; - zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); - zend_optimizer_add_literal(op_array, val TSRMLS_CC); - op_array->literals[opline->op2.constant+1].hash_value = zend_hash_func(Z_STRVAL(op_array->literals[opline->op2.constant+1].constant), Z_STRLEN(op_array->literals[opline->op2.constant+1].constant) + 1); - break; - case ZEND_INIT_METHOD_CALL: - case ZEND_INIT_STATIC_METHOD_CALL: - zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); - zend_optimizer_add_literal(op_array, val TSRMLS_CC); - op_array->literals[opline->op2.constant+1].hash_value = zend_hash_func(Z_STRVAL(op_array->literals[opline->op2.constant+1].constant), Z_STRLEN(op_array->literals[opline->op2.constant+1].constant) + 1); - /* break missing intentionally */ - /*case ZEND_FETCH_CONSTANT:*/ - case ZEND_ASSIGN_OBJ: - case ZEND_FETCH_OBJ_R: - case ZEND_FETCH_OBJ_W: - case ZEND_FETCH_OBJ_RW: - case ZEND_FETCH_OBJ_IS: - case ZEND_FETCH_OBJ_UNSET: - case ZEND_FETCH_OBJ_FUNC_ARG: - case ZEND_UNSET_OBJ: - case ZEND_PRE_INC_OBJ: - case ZEND_PRE_DEC_OBJ: - case ZEND_POST_INC_OBJ: - case ZEND_POST_DEC_OBJ: - case ZEND_ISSET_ISEMPTY_PROP_OBJ: - op_array->literals[opline->op2.constant].cache_slot = op_array->last_cache_slot; - op_array->last_cache_slot += 2; - break; - case ZEND_ASSIGN_ADD: - case ZEND_ASSIGN_SUB: - case ZEND_ASSIGN_MUL: - case ZEND_ASSIGN_DIV: - case ZEND_ASSIGN_MOD: - case ZEND_ASSIGN_SL: - case ZEND_ASSIGN_SR: - case ZEND_ASSIGN_CONCAT: - case ZEND_ASSIGN_BW_OR: - case ZEND_ASSIGN_BW_AND: - case ZEND_ASSIGN_BW_XOR: - if (opline->extended_value == ZEND_ASSIGN_OBJ) { - op_array->literals[opline->op2.constant].cache_slot = op_array->last_cache_slot; - op_array->last_cache_slot += 2; - } - break; -#if ZEND_EXTENSION_API_NO >= PHP_5_4_X_API_NO - case ZEND_OP_DATA: - if ((opline-1)->opcode == ZEND_ASSIGN_DIM || - ((opline-1)->extended_value == ZEND_ASSIGN_DIM && - ((opline-1)->opcode == ZEND_ASSIGN_ADD || - (opline-1)->opcode == ZEND_ASSIGN_SUB || - (opline-1)->opcode == ZEND_ASSIGN_MUL || - (opline-1)->opcode == ZEND_ASSIGN_DIV || - (opline-1)->opcode == ZEND_ASSIGN_MOD || - (opline-1)->opcode == ZEND_ASSIGN_SL || - (opline-1)->opcode == ZEND_ASSIGN_SR || - (opline-1)->opcode == ZEND_ASSIGN_CONCAT || - (opline-1)->opcode == ZEND_ASSIGN_BW_OR || - (opline-1)->opcode == ZEND_ASSIGN_BW_AND || - (opline-1)->opcode == ZEND_ASSIGN_BW_XOR))) { - goto check_numeric; - } - break; - case ZEND_ISSET_ISEMPTY_DIM_OBJ: - case ZEND_ADD_ARRAY_ELEMENT: - case ZEND_INIT_ARRAY: - case ZEND_UNSET_DIM: - case ZEND_FETCH_DIM_R: - case ZEND_FETCH_DIM_W: - case ZEND_FETCH_DIM_RW: - case ZEND_FETCH_DIM_IS: - case ZEND_FETCH_DIM_FUNC_ARG: - case ZEND_FETCH_DIM_UNSET: - case ZEND_FETCH_DIM_TMP_VAR: -check_numeric: - { - ulong index; - int numeric = 0; - - ZEND_HANDLE_NUMERIC_EX(Z_STRVAL_P(val), Z_STRLEN_P(val)+1, index, numeric = 1); - if (numeric) { - zval_dtor(val); - ZVAL_LONG(val, index); - op_array->literals[opline->op2.constant].constant = *val; - } - } - break; -#endif - default: - break; - } - } -#else - ZEND_OP2_LITERAL(opline) = *val; -#endif -} - -static void replace_tmp_by_const(zend_op_array *op_array, - zend_op *opline, - zend_uint var, - zval *val - TSRMLS_DC) -{ - zend_op *end = op_array->opcodes + op_array->last; - - while (opline < end) { - if (ZEND_OP1_TYPE(opline) == IS_TMP_VAR && - ZEND_OP1(opline).var == var) { - - update_op1_const(op_array, opline, val TSRMLS_CC); - /* TMP_VAR my be used only once */ - break; - } - - if (ZEND_OP2_TYPE(opline) == IS_TMP_VAR && - ZEND_OP2(opline).var == var) { - - update_op2_const(op_array, opline, val TSRMLS_CC); - /* TMP_VAR my be used only once */ - break; - } - opline++; - } -} - -#include "Optimizer/nop_removal.c" -#include "Optimizer/block_pass.c" -#include "Optimizer/optimize_temp_vars_5.c" - -void zend_optimizer(zend_op_array *op_array TSRMLS_DC) -{ - if (op_array->type == ZEND_EVAL_CODE || - (op_array->fn_flags & ZEND_ACC_INTERACTIVE)) { - return; - } - - /* pass 1 - * - substitute persistent constants (true, false, null, etc) - * - perform compile-time evaluation of constant binary and unary operations - * - optimize series of ADD_STRING and/or ADD_CHAR - * - convert CAST(IS_BOOL,x) into BOOL(x) - * - convert INTI_FCALL_BY_NAME + DO_FCALL_BY_NAME into DO_FCALL - */ -#include "Optimizer/pass1_5.c" - - /* pass 2: - * - convert non-numeric constants to numeric constants in numeric operators - * - optimize constant conditional JMPs - * - optimize static BRKs and CONTs - */ -#include "Optimizer/pass2.c" - - /* pass 3: - * - optimize $i = $i+expr to $i+=expr - * - optimize series of JMPs - * - change $i++ to ++$i where possible - */ -#include "Optimizer/pass3.c" - - /* pass 5: - * - CFG optimization - */ -#include "Optimizer/pass5.c" - - /* pass 9: - * - Optimize temp variables usage - */ -#include "Optimizer/pass9.c" - - /* pass 10: - * - remove NOPs - */ -#include "Optimizer/pass10.c" -} +/* + +----------------------------------------------------------------------+ + | Zend OPcache | + +----------------------------------------------------------------------+ + | Copyright (c) 1998-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Andi Gutmans | + | Zeev Suraski | + | Stanislav Malyshev | + | Dmitry Stogov | + +----------------------------------------------------------------------+ +*/ + +#include "php.h" +#include "Optimizer/zend_optimizer.h" +#include "Optimizer/zend_optimizer_internal.h" +#include "zend_API.h" +#include "zend_constants.h" +#include "zend_execute.h" + +#define OPTIMIZATION_LEVEL \ + ZCG(accel_directives).optimization_level + +#if ZEND_EXTENSION_API_NO >= PHP_5_5_X_API_NO +static int zend_optimizer_lookup_cv(zend_op_array *op_array, char* name, int name_len) +{ + int i = 0; + ulong hash_value = zend_inline_hash_func(name, name_len+1); + + while (i < op_array->last_var) { + if (op_array->vars[i].name == name || + (op_array->vars[i].hash_value == hash_value && + op_array->vars[i].name_len == name_len && + memcmp(op_array->vars[i].name, name, name_len) == 0)) { + return i; + } + i++; + } + i = op_array->last_var; + op_array->last_var++; + op_array->vars = erealloc(op_array->vars, op_array->last_var * sizeof(zend_compiled_variable)); + if (IS_INTERNED(name)) { + op_array->vars[i].name = name; + } else { + op_array->vars[i].name = estrndup(name, name_len); + } + op_array->vars[i].name_len = name_len; + op_array->vars[i].hash_value = hash_value; + return i; +} +#endif + +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO +int zend_optimizer_add_literal(zend_op_array *op_array, const zval *zv TSRMLS_DC) +{ + int i = op_array->last_literal; + op_array->last_literal++; + if (i >= CG(context).literals_size) { + CG(context).literals_size += 16; /* FIXME */ + op_array->literals = (zend_literal*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zend_literal)); + } + op_array->literals[i].constant = *zv; + op_array->literals[i].hash_value = 0; + op_array->literals[i].cache_slot = -1; + Z_SET_REFCOUNT(op_array->literals[i].constant, 2); + Z_SET_ISREF(op_array->literals[i].constant); + return i; +} + +# define LITERAL_LONG(op, val) do { \ + zval _c; \ + ZVAL_LONG(&_c, val); \ + op.constant = zend_optimizer_add_literal(op_array, &_c TSRMLS_CC); \ + } while (0) + +# define LITERAL_BOOL(op, val) do { \ + zval _c; \ + ZVAL_BOOL(&_c, val); \ + op.constant = zend_optimizer_add_literal(op_array, &_c TSRMLS_CC); \ + } while (0) + +# define literal_dtor(zv) do { \ + zval_dtor(zv); \ + Z_TYPE_P(zv) = IS_NULL; \ + } while (0) + +#define COPY_NODE(target, src) do { \ + target ## _type = src ## _type; \ + target = src; \ + } while (0) + +#else + +# define LITERAL_LONG(op, val) ZVAL_LONG(&op.u.constant, val) + +# define LITERAL_BOOL(op, val) ZVAL_BOOL(&op.u.constant, val) + +# define literal_dtor(zv) zval_dtor(zv) + +#define COPY_NODE(target, src) do { \ + target = src; \ + } while (0) + +#endif + +static void update_op1_const(zend_op_array *op_array, + zend_op *opline, + zval *val TSRMLS_DC) +{ + if (opline->opcode == ZEND_FREE) { + MAKE_NOP(opline); + zval_dtor(val); + } else { + ZEND_OP1_TYPE(opline) = IS_CONST; +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO + if (Z_TYPE_P(val) == IS_STRING) { + switch (opline->opcode) { + case ZEND_INIT_STATIC_METHOD_CALL: + case ZEND_CATCH: + case ZEND_FETCH_CONSTANT: + opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); + Z_HASH_P(&ZEND_OP1_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)) + 1); + op_array->literals[opline->op1.constant].cache_slot = op_array->last_cache_slot++; + zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); + zend_optimizer_add_literal(op_array, val TSRMLS_CC); + op_array->literals[opline->op1.constant+1].hash_value = zend_hash_func(Z_STRVAL(op_array->literals[opline->op1.constant+1].constant), Z_STRLEN(op_array->literals[opline->op1.constant+1].constant) + 1); + break; + case ZEND_DO_FCALL: + zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); + opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); + Z_HASH_P(&ZEND_OP1_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)) + 1); + op_array->literals[opline->op1.constant].cache_slot = op_array->last_cache_slot++; + break; + default: + opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); + Z_HASH_P(&ZEND_OP1_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)) + 1); + break; + } + } else { + opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); + } +#else + ZEND_OP1_LITERAL(opline) = *val; +#endif + } +} + +static void update_op2_const(zend_op_array *op_array, + zend_op *opline, + zval *val TSRMLS_DC) +{ + ZEND_OP2_TYPE(opline) = IS_CONST; +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO + opline->op2.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); + if (Z_TYPE_P(val) == IS_STRING) { + Z_HASH_P(&ZEND_OP2_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP2_LITERAL(opline)), Z_STRLEN(ZEND_OP2_LITERAL(opline)) + 1); + switch (opline->opcode) { + case ZEND_FETCH_R: + case ZEND_FETCH_W: + case ZEND_FETCH_RW: + case ZEND_FETCH_IS: + case ZEND_FETCH_UNSET: + case ZEND_FETCH_FUNC_ARG: + case ZEND_FETCH_CLASS: + case ZEND_INIT_FCALL_BY_NAME: + /*case ZEND_INIT_NS_FCALL_BY_NAME:*/ + case ZEND_UNSET_VAR: + case ZEND_ISSET_ISEMPTY_VAR: + case ZEND_ADD_INTERFACE: + case ZEND_ADD_TRAIT: + op_array->literals[opline->op2.constant].cache_slot = op_array->last_cache_slot++; + zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); + zend_optimizer_add_literal(op_array, val TSRMLS_CC); + op_array->literals[opline->op2.constant+1].hash_value = zend_hash_func(Z_STRVAL(op_array->literals[opline->op2.constant+1].constant), Z_STRLEN(op_array->literals[opline->op2.constant+1].constant) + 1); + break; + case ZEND_INIT_METHOD_CALL: + case ZEND_INIT_STATIC_METHOD_CALL: + zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); + zend_optimizer_add_literal(op_array, val TSRMLS_CC); + op_array->literals[opline->op2.constant+1].hash_value = zend_hash_func(Z_STRVAL(op_array->literals[opline->op2.constant+1].constant), Z_STRLEN(op_array->literals[opline->op2.constant+1].constant) + 1); + /* break missing intentionally */ + /*case ZEND_FETCH_CONSTANT:*/ + case ZEND_ASSIGN_OBJ: + case ZEND_FETCH_OBJ_R: + case ZEND_FETCH_OBJ_W: + case ZEND_FETCH_OBJ_RW: + case ZEND_FETCH_OBJ_IS: + case ZEND_FETCH_OBJ_UNSET: + case ZEND_FETCH_OBJ_FUNC_ARG: + case ZEND_UNSET_OBJ: + case ZEND_PRE_INC_OBJ: + case ZEND_PRE_DEC_OBJ: + case ZEND_POST_INC_OBJ: + case ZEND_POST_DEC_OBJ: + case ZEND_ISSET_ISEMPTY_PROP_OBJ: + op_array->literals[opline->op2.constant].cache_slot = op_array->last_cache_slot; + op_array->last_cache_slot += 2; + break; + case ZEND_ASSIGN_ADD: + case ZEND_ASSIGN_SUB: + case ZEND_ASSIGN_MUL: + case ZEND_ASSIGN_DIV: + case ZEND_ASSIGN_MOD: + case ZEND_ASSIGN_SL: + case ZEND_ASSIGN_SR: + case ZEND_ASSIGN_CONCAT: + case ZEND_ASSIGN_BW_OR: + case ZEND_ASSIGN_BW_AND: + case ZEND_ASSIGN_BW_XOR: + if (opline->extended_value == ZEND_ASSIGN_OBJ) { + op_array->literals[opline->op2.constant].cache_slot = op_array->last_cache_slot; + op_array->last_cache_slot += 2; + } + break; +#if ZEND_EXTENSION_API_NO >= PHP_5_4_X_API_NO + case ZEND_OP_DATA: + if ((opline-1)->opcode == ZEND_ASSIGN_DIM || + ((opline-1)->extended_value == ZEND_ASSIGN_DIM && + ((opline-1)->opcode == ZEND_ASSIGN_ADD || + (opline-1)->opcode == ZEND_ASSIGN_SUB || + (opline-1)->opcode == ZEND_ASSIGN_MUL || + (opline-1)->opcode == ZEND_ASSIGN_DIV || + (opline-1)->opcode == ZEND_ASSIGN_MOD || + (opline-1)->opcode == ZEND_ASSIGN_SL || + (opline-1)->opcode == ZEND_ASSIGN_SR || + (opline-1)->opcode == ZEND_ASSIGN_CONCAT || + (opline-1)->opcode == ZEND_ASSIGN_BW_OR || + (opline-1)->opcode == ZEND_ASSIGN_BW_AND || + (opline-1)->opcode == ZEND_ASSIGN_BW_XOR))) { + goto check_numeric; + } + break; + case ZEND_ISSET_ISEMPTY_DIM_OBJ: + case ZEND_ADD_ARRAY_ELEMENT: + case ZEND_INIT_ARRAY: + case ZEND_UNSET_DIM: + case ZEND_FETCH_DIM_R: + case ZEND_FETCH_DIM_W: + case ZEND_FETCH_DIM_RW: + case ZEND_FETCH_DIM_IS: + case ZEND_FETCH_DIM_FUNC_ARG: + case ZEND_FETCH_DIM_UNSET: + case ZEND_FETCH_DIM_TMP_VAR: +check_numeric: + { + ulong index; + int numeric = 0; + + ZEND_HANDLE_NUMERIC_EX(Z_STRVAL_P(val), Z_STRLEN_P(val)+1, index, numeric = 1); + if (numeric) { + zval_dtor(val); + ZVAL_LONG(val, index); + op_array->literals[opline->op2.constant].constant = *val; + } + } + break; +#endif + default: + break; + } + } +#else + ZEND_OP2_LITERAL(opline) = *val; +#endif +} + +static void replace_tmp_by_const(zend_op_array *op_array, + zend_op *opline, + zend_uint var, + zval *val + TSRMLS_DC) +{ + zend_op *end = op_array->opcodes + op_array->last; + + while (opline < end) { + if (ZEND_OP1_TYPE(opline) == IS_TMP_VAR && + ZEND_OP1(opline).var == var) { + + update_op1_const(op_array, opline, val TSRMLS_CC); + /* TMP_VAR my be used only once */ + break; + } + + if (ZEND_OP2_TYPE(opline) == IS_TMP_VAR && + ZEND_OP2(opline).var == var) { + + update_op2_const(op_array, opline, val TSRMLS_CC); + /* TMP_VAR my be used only once */ + break; + } + opline++; + } +} + +#include "Optimizer/nop_removal.c" +#include "Optimizer/block_pass.c" +#include "Optimizer/optimize_temp_vars_5.c" + +void zend_optimizer(zend_op_array *op_array TSRMLS_DC) +{ + if (op_array->type == ZEND_EVAL_CODE || + (op_array->fn_flags & ZEND_ACC_INTERACTIVE)) { + return; + } + + /* pass 1 + * - substitute persistent constants (true, false, null, etc) + * - perform compile-time evaluation of constant binary and unary operations + * - optimize series of ADD_STRING and/or ADD_CHAR + * - convert CAST(IS_BOOL,x) into BOOL(x) + * - convert INTI_FCALL_BY_NAME + DO_FCALL_BY_NAME into DO_FCALL + */ +#include "Optimizer/pass1_5.c" + + /* pass 2: + * - convert non-numeric constants to numeric constants in numeric operators + * - optimize constant conditional JMPs + * - optimize static BRKs and CONTs + */ +#include "Optimizer/pass2.c" + + /* pass 3: + * - optimize $i = $i+expr to $i+=expr + * - optimize series of JMPs + * - change $i++ to ++$i where possible + */ +#include "Optimizer/pass3.c" + + /* pass 5: + * - CFG optimization + */ +#include "Optimizer/pass5.c" + + /* pass 9: + * - Optimize temp variables usage + */ +#include "Optimizer/pass9.c" + + /* pass 10: + * - remove NOPs + */ +#include "Optimizer/pass10.c" +} diff --git a/ext/opcache/Optimizer/zend_optimizer.h b/ext/opcache/Optimizer/zend_optimizer.h index 98275a20aa..7f2b4f21fd 100644 --- a/ext/opcache/Optimizer/zend_optimizer.h +++ b/ext/opcache/Optimizer/zend_optimizer.h @@ -1,49 +1,49 @@ -/* - +----------------------------------------------------------------------+ - | Zend OPcache | - +----------------------------------------------------------------------+ - | Copyright (c) 1998-2013 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Authors: Andi Gutmans | - | Zeev Suraski | - | Stanislav Malyshev | - | Dmitry Stogov | - +----------------------------------------------------------------------+ -*/ - -#ifndef ZEND_OPTIMIZER_H -#define ZEND_OPTIMIZER_H - -#include "zend.h" -#include "zend_compile.h" - -#define ZEND_OPTIMIZER_PASS_1 (1<<0) /* CSE, STRING construction */ -#define ZEND_OPTIMIZER_PASS_2 (1<<1) /* Constant conversion and jumps */ -#define ZEND_OPTIMIZER_PASS_3 (1<<2) /* ++, +=, series of jumps */ -#define ZEND_OPTIMIZER_PASS_4 (1<<3) -#define ZEND_OPTIMIZER_PASS_5 (1<<4) /* CFG based optimization */ -#define ZEND_OPTIMIZER_PASS_6 (1<<5) -#define ZEND_OPTIMIZER_PASS_7 (1<<6) -#define ZEND_OPTIMIZER_PASS_8 (1<<7) -#define ZEND_OPTIMIZER_PASS_9 (1<<8) /* TMP VAR usage */ -#define ZEND_OPTIMIZER_PASS_10 (1<<9) /* NOP removal */ -#define ZEND_OPTIMIZER_PASS_11 (1<<10) -#define ZEND_OPTIMIZER_PASS_12 (1<<11) -#define ZEND_OPTIMIZER_PASS_13 (1<<12) -#define ZEND_OPTIMIZER_PASS_14 (1<<13) - -#define ZEND_OPTIMIZER_ALL_PASSES 0xFFFFFFFF - -#define DEFAULT_OPTIMIZATION_LEVEL "0xFFFFFFFF" - -void zend_optimizer(zend_op_array *op_array TSRMLS_DC); - -#endif +/* + +----------------------------------------------------------------------+ + | Zend OPcache | + +----------------------------------------------------------------------+ + | Copyright (c) 1998-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Andi Gutmans | + | Zeev Suraski | + | Stanislav Malyshev | + | Dmitry Stogov | + +----------------------------------------------------------------------+ +*/ + +#ifndef ZEND_OPTIMIZER_H +#define ZEND_OPTIMIZER_H + +#include "zend.h" +#include "zend_compile.h" + +#define ZEND_OPTIMIZER_PASS_1 (1<<0) /* CSE, STRING construction */ +#define ZEND_OPTIMIZER_PASS_2 (1<<1) /* Constant conversion and jumps */ +#define ZEND_OPTIMIZER_PASS_3 (1<<2) /* ++, +=, series of jumps */ +#define ZEND_OPTIMIZER_PASS_4 (1<<3) +#define ZEND_OPTIMIZER_PASS_5 (1<<4) /* CFG based optimization */ +#define ZEND_OPTIMIZER_PASS_6 (1<<5) +#define ZEND_OPTIMIZER_PASS_7 (1<<6) +#define ZEND_OPTIMIZER_PASS_8 (1<<7) +#define ZEND_OPTIMIZER_PASS_9 (1<<8) /* TMP VAR usage */ +#define ZEND_OPTIMIZER_PASS_10 (1<<9) /* NOP removal */ +#define ZEND_OPTIMIZER_PASS_11 (1<<10) +#define ZEND_OPTIMIZER_PASS_12 (1<<11) +#define ZEND_OPTIMIZER_PASS_13 (1<<12) +#define ZEND_OPTIMIZER_PASS_14 (1<<13) + +#define ZEND_OPTIMIZER_ALL_PASSES 0xFFFFFFFF + +#define DEFAULT_OPTIMIZATION_LEVEL "0xFFFFFFFF" + +void zend_optimizer(zend_op_array *op_array TSRMLS_DC); + +#endif diff --git a/ext/opcache/Optimizer/zend_optimizer_internal.h b/ext/opcache/Optimizer/zend_optimizer_internal.h index 616bdf74f6..657b480316 100644 --- a/ext/opcache/Optimizer/zend_optimizer_internal.h +++ b/ext/opcache/Optimizer/zend_optimizer_internal.h @@ -1,86 +1,86 @@ -/* - +----------------------------------------------------------------------+ - | Zend OPcache | - +----------------------------------------------------------------------+ - | Copyright (c) 1998-2013 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Authors: Andi Gutmans | - | Zeev Suraski | - | Stanislav Malyshev | - | Dmitry Stogov | - +----------------------------------------------------------------------+ -*/ - -#ifndef ZEND_OPTIMIZER_INTERNAL_H -#define ZEND_OPTIMIZER_INTERNAL_H - -#include "ZendAccelerator.h" - -#if ZEND_EXTENSION_API_NO > PHP_5_4_X_API_NO -# define VAR_NUM(v) ((zend_uint)(EX_TMP_VAR_NUM(0, 0) - EX_TMP_VAR(0, v))) -# define NUM_VAR(v) ((zend_uint)(zend_uintptr_t)EX_TMP_VAR_NUM(0, v)) -#elif ZEND_EXTENSION_API_NO > PHP_5_2_X_API_NO -# define VAR_NUM(v) ((v)/ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable))) -# define NUM_VAR(v) ((v)*ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable))) -#else -# define VAR_NUM(v) ((v)/(sizeof(temp_variable))) -# define NUM_VAR(v) ((v)*(sizeof(temp_variable))) -#endif - -#define INV_COND(op) ((op) == ZEND_JMPZ ? ZEND_JMPNZ : ZEND_JMPZ) -#define INV_EX_COND(op) ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ : ZEND_JMPZ) -#define INV_COND_EX(op) ((op) == ZEND_JMPZ ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX) -#define INV_EX_COND_EX(op) ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX) - -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO -# define MAKE_NOP(opline) { opline->opcode = ZEND_NOP; memset(&opline->result,0,sizeof(opline->result)); memset(&opline->op1,0,sizeof(opline->op1)); memset(&opline->op2,0,sizeof(opline->op2)); opline->result_type=opline->op1_type=opline->op2_type=IS_UNUSED; opline->handler = zend_opcode_handlers[ZEND_NOP]; } -# define RESULT_USED(op) (((op->result_type & IS_VAR) && !(op->result_type & EXT_TYPE_UNUSED)) || op->result_type == IS_TMP_VAR) -# define RESULT_UNUSED(op) ((op->result_type & EXT_TYPE_UNUSED) != 0) -# define SAME_VAR(op1, op2) ((((op1 ## _type & IS_VAR) && (op2 ## _type & IS_VAR)) || (op1 ## _type == IS_TMP_VAR && op2 ## _type == IS_TMP_VAR)) && op1.var == op2.var) -#else -# define MAKE_NOP(opline) { opline->opcode = ZEND_NOP; memset(&opline->result,0,sizeof(znode)); memset(&opline->op1,0,sizeof(znode)); memset(&opline->op2,0,sizeof(znode)); opline->result.op_type=opline->op1.op_type=opline->op2.op_type=IS_UNUSED; opline->handler = zend_opcode_handlers[ZEND_NOP]; } -# define RESULT_USED(op) ((op->result.op_type == IS_VAR && (op->result.u.EA.type & EXT_TYPE_UNUSED) == 0) || (op->result.op_type == IS_TMP_VAR)) -# define RESULT_UNUSED(op) ((op->result.op_type == IS_VAR) && (op->result.u.EA.type == EXT_TYPE_UNUSED)) -# define SAME_VAR(op1, op2) (((op1.op_type == IS_VAR && op2.op_type == IS_VAR) || (op1.op_type == IS_TMP_VAR && op2.op_type == IS_TMP_VAR)) && op1.u.var == op2.u.var) -#endif - -typedef struct _zend_code_block zend_code_block; -typedef struct _zend_block_source zend_block_source; - -struct _zend_code_block { - int access; - zend_op *start_opline; - int start_opline_no; - int len; - zend_code_block *op1_to; - zend_code_block *op2_to; - zend_code_block *ext_to; - zend_code_block *follow_to; - zend_code_block *next; - zend_block_source *sources; - zend_bool protected; /* don't merge this block with others */ -}; - -typedef struct _zend_cfg { - zend_code_block *blocks; - zend_code_block **try; - zend_code_block **catch; - zend_code_block **loop_start; - zend_code_block **loop_cont; - zend_code_block **loop_brk; -} zend_cfg; - -struct _zend_block_source { - zend_code_block *from; - zend_block_source *next; -}; - -#endif +/* + +----------------------------------------------------------------------+ + | Zend OPcache | + +----------------------------------------------------------------------+ + | Copyright (c) 1998-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Andi Gutmans | + | Zeev Suraski | + | Stanislav Malyshev | + | Dmitry Stogov | + +----------------------------------------------------------------------+ +*/ + +#ifndef ZEND_OPTIMIZER_INTERNAL_H +#define ZEND_OPTIMIZER_INTERNAL_H + +#include "ZendAccelerator.h" + +#if ZEND_EXTENSION_API_NO > PHP_5_4_X_API_NO +# define VAR_NUM(v) ((zend_uint)(EX_TMP_VAR_NUM(0, 0) - EX_TMP_VAR(0, v))) +# define NUM_VAR(v) ((zend_uint)(zend_uintptr_t)EX_TMP_VAR_NUM(0, v)) +#elif ZEND_EXTENSION_API_NO > PHP_5_2_X_API_NO +# define VAR_NUM(v) ((v)/ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable))) +# define NUM_VAR(v) ((v)*ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable))) +#else +# define VAR_NUM(v) ((v)/(sizeof(temp_variable))) +# define NUM_VAR(v) ((v)*(sizeof(temp_variable))) +#endif + +#define INV_COND(op) ((op) == ZEND_JMPZ ? ZEND_JMPNZ : ZEND_JMPZ) +#define INV_EX_COND(op) ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ : ZEND_JMPZ) +#define INV_COND_EX(op) ((op) == ZEND_JMPZ ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX) +#define INV_EX_COND_EX(op) ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX) + +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO +# define MAKE_NOP(opline) { opline->opcode = ZEND_NOP; memset(&opline->result,0,sizeof(opline->result)); memset(&opline->op1,0,sizeof(opline->op1)); memset(&opline->op2,0,sizeof(opline->op2)); opline->result_type=opline->op1_type=opline->op2_type=IS_UNUSED; opline->handler = zend_opcode_handlers[ZEND_NOP]; } +# define RESULT_USED(op) (((op->result_type & IS_VAR) && !(op->result_type & EXT_TYPE_UNUSED)) || op->result_type == IS_TMP_VAR) +# define RESULT_UNUSED(op) ((op->result_type & EXT_TYPE_UNUSED) != 0) +# define SAME_VAR(op1, op2) ((((op1 ## _type & IS_VAR) && (op2 ## _type & IS_VAR)) || (op1 ## _type == IS_TMP_VAR && op2 ## _type == IS_TMP_VAR)) && op1.var == op2.var) +#else +# define MAKE_NOP(opline) { opline->opcode = ZEND_NOP; memset(&opline->result,0,sizeof(znode)); memset(&opline->op1,0,sizeof(znode)); memset(&opline->op2,0,sizeof(znode)); opline->result.op_type=opline->op1.op_type=opline->op2.op_type=IS_UNUSED; opline->handler = zend_opcode_handlers[ZEND_NOP]; } +# define RESULT_USED(op) ((op->result.op_type == IS_VAR && (op->result.u.EA.type & EXT_TYPE_UNUSED) == 0) || (op->result.op_type == IS_TMP_VAR)) +# define RESULT_UNUSED(op) ((op->result.op_type == IS_VAR) && (op->result.u.EA.type == EXT_TYPE_UNUSED)) +# define SAME_VAR(op1, op2) (((op1.op_type == IS_VAR && op2.op_type == IS_VAR) || (op1.op_type == IS_TMP_VAR && op2.op_type == IS_TMP_VAR)) && op1.u.var == op2.u.var) +#endif + +typedef struct _zend_code_block zend_code_block; +typedef struct _zend_block_source zend_block_source; + +struct _zend_code_block { + int access; + zend_op *start_opline; + int start_opline_no; + int len; + zend_code_block *op1_to; + zend_code_block *op2_to; + zend_code_block *ext_to; + zend_code_block *follow_to; + zend_code_block *next; + zend_block_source *sources; + zend_bool protected; /* don't merge this block with others */ +}; + +typedef struct _zend_cfg { + zend_code_block *blocks; + zend_code_block **try; + zend_code_block **catch; + zend_code_block **loop_start; + zend_code_block **loop_cont; + zend_code_block **loop_brk; +} zend_cfg; + +struct _zend_block_source { + zend_code_block *from; + zend_block_source *next; +}; + +#endif From a88543a106fa2e43b3153d11987b577ec28f78c0 Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Mon, 30 Dec 2013 10:51:47 +0800 Subject: [PATCH 2/8] Fixed bug #66298 (ext/opcache/Optimizer/zend_optimizer.c has dos-style ^M as lineend) --- ext/opcache/Optimizer/zend_optimizer.c | 1172 ++++++++--------- ext/opcache/Optimizer/zend_optimizer.h | 94 +- .../Optimizer/zend_optimizer_internal.h | 172 +-- 3 files changed, 719 insertions(+), 719 deletions(-) diff --git a/ext/opcache/Optimizer/zend_optimizer.c b/ext/opcache/Optimizer/zend_optimizer.c index 0426f63514..763bf91db6 100644 --- a/ext/opcache/Optimizer/zend_optimizer.c +++ b/ext/opcache/Optimizer/zend_optimizer.c @@ -1,586 +1,586 @@ -/* - +----------------------------------------------------------------------+ - | Zend OPcache | - +----------------------------------------------------------------------+ - | Copyright (c) 1998-2013 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Authors: Andi Gutmans | - | Zeev Suraski | - | Stanislav Malyshev | - | Dmitry Stogov | - +----------------------------------------------------------------------+ -*/ - -#include "php.h" -#include "Optimizer/zend_optimizer.h" -#include "Optimizer/zend_optimizer_internal.h" -#include "zend_API.h" -#include "zend_constants.h" -#include "zend_execute.h" -#include "zend_vm.h" - -#define OPTIMIZATION_LEVEL \ - ZCG(accel_directives).optimization_level - -static void zend_optimizer_zval_dtor_wrapper(zval *zvalue) -{ - zval_dtor(zvalue); -} - -static void zend_optimizer_collect_constant(HashTable **constants, zval *name, zval* value) -{ - zval val; - - if (!*constants) { - *constants = emalloc(sizeof(HashTable)); - zend_hash_init(*constants, 16, NULL, (void (*)(void *))zend_optimizer_zval_dtor_wrapper, 0); - } - val = *value; - zval_copy_ctor(&val); - zend_hash_add(*constants, Z_STRVAL_P(name), Z_STRLEN_P(name)+1, (void**)&val, sizeof(zval), NULL); -} - -static int zend_optimizer_get_collected_constant(HashTable *constants, zval *name, zval* value) -{ - zval *val; - - if (zend_hash_find(constants, Z_STRVAL_P(name), Z_STRLEN_P(name)+1, (void**)&val) == SUCCESS) { - *value = *val; - zval_copy_ctor(value); - return 1; - } - return 0; -} - -#if ZEND_EXTENSION_API_NO >= PHP_5_5_X_API_NO -static int zend_optimizer_lookup_cv(zend_op_array *op_array, char* name, int name_len) -{ - int i = 0; - ulong hash_value = zend_inline_hash_func(name, name_len+1); - - while (i < op_array->last_var) { - if (op_array->vars[i].name == name || - (op_array->vars[i].hash_value == hash_value && - op_array->vars[i].name_len == name_len && - memcmp(op_array->vars[i].name, name, name_len) == 0)) { - return i; - } - i++; - } - i = op_array->last_var; - op_array->last_var++; - op_array->vars = erealloc(op_array->vars, op_array->last_var * sizeof(zend_compiled_variable)); - if (IS_INTERNED(name)) { - op_array->vars[i].name = name; - } else { - op_array->vars[i].name = estrndup(name, name_len); - } - op_array->vars[i].name_len = name_len; - op_array->vars[i].hash_value = hash_value; - return i; -} -#endif - -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO -int zend_optimizer_add_literal(zend_op_array *op_array, const zval *zv TSRMLS_DC) -{ - int i = op_array->last_literal; - op_array->last_literal++; - op_array->literals = (zend_literal*)erealloc(op_array->literals, op_array->last_literal * sizeof(zend_literal)); - op_array->literals[i].constant = *zv; - op_array->literals[i].hash_value = 0; - op_array->literals[i].cache_slot = -1; - Z_SET_REFCOUNT(op_array->literals[i].constant, 2); - Z_SET_ISREF(op_array->literals[i].constant); - return i; -} - -# define LITERAL_LONG(op, val) do { \ - zval _c; \ - ZVAL_LONG(&_c, val); \ - op.constant = zend_optimizer_add_literal(op_array, &_c TSRMLS_CC); \ - } while (0) - -# define LITERAL_BOOL(op, val) do { \ - zval _c; \ - ZVAL_BOOL(&_c, val); \ - op.constant = zend_optimizer_add_literal(op_array, &_c TSRMLS_CC); \ - } while (0) - -# define literal_dtor(zv) do { \ - zval_dtor(zv); \ - Z_TYPE_P(zv) = IS_NULL; \ - } while (0) - -#define COPY_NODE(target, src) do { \ - target ## _type = src ## _type; \ - target = src; \ - } while (0) - -#else - -# define LITERAL_LONG(op, val) ZVAL_LONG(&op.u.constant, val) - -# define LITERAL_BOOL(op, val) ZVAL_BOOL(&op.u.constant, val) - -# define literal_dtor(zv) zval_dtor(zv) - -#define COPY_NODE(target, src) do { \ - target = src; \ - } while (0) - -#endif - -static void update_op1_const(zend_op_array *op_array, - zend_op *opline, - zval *val TSRMLS_DC) -{ - if (opline->opcode == ZEND_FREE) { - MAKE_NOP(opline); - zval_dtor(val); - } else { - ZEND_OP1_TYPE(opline) = IS_CONST; -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO - if (Z_TYPE_P(val) == IS_STRING) { - switch (opline->opcode) { - case ZEND_INIT_STATIC_METHOD_CALL: - case ZEND_CATCH: - case ZEND_FETCH_CONSTANT: - opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); - Z_HASH_P(&ZEND_OP1_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)) + 1); - op_array->literals[opline->op1.constant].cache_slot = op_array->last_cache_slot++; - zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); - zend_optimizer_add_literal(op_array, val TSRMLS_CC); - op_array->literals[opline->op1.constant+1].hash_value = zend_hash_func(Z_STRVAL(op_array->literals[opline->op1.constant+1].constant), Z_STRLEN(op_array->literals[opline->op1.constant+1].constant) + 1); - break; - case ZEND_DO_FCALL: - zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); - opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); - Z_HASH_P(&ZEND_OP1_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)) + 1); - op_array->literals[opline->op1.constant].cache_slot = op_array->last_cache_slot++; - break; - default: - opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); - Z_HASH_P(&ZEND_OP1_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)) + 1); - break; - } - } else { - opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); - } -#else - ZEND_OP1_LITERAL(opline) = *val; -#endif - } -} - -static void update_op2_const(zend_op_array *op_array, - zend_op *opline, - zval *val TSRMLS_DC) -{ - ZEND_OP2_TYPE(opline) = IS_CONST; -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO - opline->op2.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); - if (Z_TYPE_P(val) == IS_STRING) { - Z_HASH_P(&ZEND_OP2_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP2_LITERAL(opline)), Z_STRLEN(ZEND_OP2_LITERAL(opline)) + 1); - switch (opline->opcode) { - case ZEND_FETCH_R: - case ZEND_FETCH_W: - case ZEND_FETCH_RW: - case ZEND_FETCH_IS: - case ZEND_FETCH_UNSET: - case ZEND_FETCH_FUNC_ARG: - case ZEND_FETCH_CLASS: - case ZEND_INIT_FCALL_BY_NAME: - /*case ZEND_INIT_NS_FCALL_BY_NAME:*/ - case ZEND_UNSET_VAR: - case ZEND_ISSET_ISEMPTY_VAR: - case ZEND_ADD_INTERFACE: - case ZEND_ADD_TRAIT: - op_array->literals[opline->op2.constant].cache_slot = op_array->last_cache_slot++; - zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); - zend_optimizer_add_literal(op_array, val TSRMLS_CC); - op_array->literals[opline->op2.constant+1].hash_value = zend_hash_func(Z_STRVAL(op_array->literals[opline->op2.constant+1].constant), Z_STRLEN(op_array->literals[opline->op2.constant+1].constant) + 1); - break; - case ZEND_INIT_METHOD_CALL: - case ZEND_INIT_STATIC_METHOD_CALL: - zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); - zend_optimizer_add_literal(op_array, val TSRMLS_CC); - op_array->literals[opline->op2.constant+1].hash_value = zend_hash_func(Z_STRVAL(op_array->literals[opline->op2.constant+1].constant), Z_STRLEN(op_array->literals[opline->op2.constant+1].constant) + 1); - /* break missing intentionally */ - /*case ZEND_FETCH_CONSTANT:*/ - case ZEND_ASSIGN_OBJ: - case ZEND_FETCH_OBJ_R: - case ZEND_FETCH_OBJ_W: - case ZEND_FETCH_OBJ_RW: - case ZEND_FETCH_OBJ_IS: - case ZEND_FETCH_OBJ_UNSET: - case ZEND_FETCH_OBJ_FUNC_ARG: - case ZEND_UNSET_OBJ: - case ZEND_PRE_INC_OBJ: - case ZEND_PRE_DEC_OBJ: - case ZEND_POST_INC_OBJ: - case ZEND_POST_DEC_OBJ: - case ZEND_ISSET_ISEMPTY_PROP_OBJ: - op_array->literals[opline->op2.constant].cache_slot = op_array->last_cache_slot; - op_array->last_cache_slot += 2; - break; - case ZEND_ASSIGN_ADD: - case ZEND_ASSIGN_SUB: - case ZEND_ASSIGN_MUL: - case ZEND_ASSIGN_DIV: - case ZEND_ASSIGN_MOD: - case ZEND_ASSIGN_SL: - case ZEND_ASSIGN_SR: - case ZEND_ASSIGN_CONCAT: - case ZEND_ASSIGN_BW_OR: - case ZEND_ASSIGN_BW_AND: - case ZEND_ASSIGN_BW_XOR: - if (opline->extended_value == ZEND_ASSIGN_OBJ) { - op_array->literals[opline->op2.constant].cache_slot = op_array->last_cache_slot; - op_array->last_cache_slot += 2; - } - break; -#if ZEND_EXTENSION_API_NO >= PHP_5_4_X_API_NO - case ZEND_OP_DATA: - if ((opline-1)->opcode == ZEND_ASSIGN_DIM || - ((opline-1)->extended_value == ZEND_ASSIGN_DIM && - ((opline-1)->opcode == ZEND_ASSIGN_ADD || - (opline-1)->opcode == ZEND_ASSIGN_SUB || - (opline-1)->opcode == ZEND_ASSIGN_MUL || - (opline-1)->opcode == ZEND_ASSIGN_DIV || - (opline-1)->opcode == ZEND_ASSIGN_MOD || - (opline-1)->opcode == ZEND_ASSIGN_SL || - (opline-1)->opcode == ZEND_ASSIGN_SR || - (opline-1)->opcode == ZEND_ASSIGN_CONCAT || - (opline-1)->opcode == ZEND_ASSIGN_BW_OR || - (opline-1)->opcode == ZEND_ASSIGN_BW_AND || - (opline-1)->opcode == ZEND_ASSIGN_BW_XOR))) { - goto check_numeric; - } - break; - case ZEND_ISSET_ISEMPTY_DIM_OBJ: - case ZEND_ADD_ARRAY_ELEMENT: - case ZEND_INIT_ARRAY: - case ZEND_UNSET_DIM: - case ZEND_FETCH_DIM_R: - case ZEND_FETCH_DIM_W: - case ZEND_FETCH_DIM_RW: - case ZEND_FETCH_DIM_IS: - case ZEND_FETCH_DIM_FUNC_ARG: - case ZEND_FETCH_DIM_UNSET: - case ZEND_FETCH_DIM_TMP_VAR: -check_numeric: - { - ulong index; - int numeric = 0; - - ZEND_HANDLE_NUMERIC_EX(Z_STRVAL_P(val), Z_STRLEN_P(val)+1, index, numeric = 1); - if (numeric) { - zval_dtor(val); - ZVAL_LONG(val, index); - op_array->literals[opline->op2.constant].constant = *val; - } - } - break; -#endif - default: - break; - } - } -#else - ZEND_OP2_LITERAL(opline) = *val; -#endif -} - -static int replace_var_by_const(zend_op_array *op_array, - zend_op *opline, - zend_uint var, - zval *val TSRMLS_DC) -{ - zend_op *end = op_array->opcodes + op_array->last; - - while (opline < end) { - if (ZEND_OP1_TYPE(opline) == IS_VAR && - ZEND_OP1(opline).var == var) { - switch (opline->opcode) { - case ZEND_FETCH_DIM_W: - case ZEND_FETCH_DIM_RW: - case ZEND_FETCH_DIM_FUNC_ARG: - case ZEND_FETCH_DIM_UNSET: - case ZEND_ASSIGN_DIM: -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO - case ZEND_SEPARATE: -#endif - return 0; - case ZEND_SEND_VAR_NO_REF: - if (opline->extended_value & ZEND_ARG_COMPILE_TIME_BOUND) { - if (opline->extended_value & ZEND_ARG_SEND_BY_REF) { - return 0; - } - opline->extended_value = ZEND_DO_FCALL; - } else { - opline->extended_value = ZEND_DO_FCALL_BY_NAME; - } - opline->opcode = ZEND_SEND_VAL; - break; - default: - break; - } - update_op1_const(op_array, opline, val TSRMLS_CC); - break; - } - - if (ZEND_OP2_TYPE(opline) == IS_VAR && - ZEND_OP2(opline).var == var) { - switch (opline->opcode) { - case ZEND_ASSIGN_REF: - return 0; - default: - break; - } - update_op2_const(op_array, opline, val TSRMLS_CC); - break; - } - opline++; - } - - return 1; -} - -static void replace_tmp_by_const(zend_op_array *op_array, - zend_op *opline, - zend_uint var, - zval *val - TSRMLS_DC) -{ - zend_op *end = op_array->opcodes + op_array->last; - - while (opline < end) { - if (ZEND_OP1_TYPE(opline) == IS_TMP_VAR && - ZEND_OP1(opline).var == var) { - - update_op1_const(op_array, opline, val TSRMLS_CC); - /* TMP_VAR my be used only once */ - break; - } - - if (ZEND_OP2_TYPE(opline) == IS_TMP_VAR && - ZEND_OP2(opline).var == var) { - - update_op2_const(op_array, opline, val TSRMLS_CC); - /* TMP_VAR my be used only once */ - break; - } - opline++; - } -} - -#include "Optimizer/nop_removal.c" -#include "Optimizer/block_pass.c" -#include "Optimizer/optimize_temp_vars_5.c" -#include "Optimizer/compact_literals.c" -#include "Optimizer/optimize_func_calls.c" - -static void zend_optimize(zend_op_array *op_array, - zend_persistent_script *script, - HashTable **constants TSRMLS_DC) -{ - if (op_array->type == ZEND_EVAL_CODE || - (op_array->fn_flags & ZEND_ACC_INTERACTIVE)) { - return; - } - - /* pass 1 - * - substitute persistent constants (true, false, null, etc) - * - perform compile-time evaluation of constant binary and unary operations - * - optimize series of ADD_STRING and/or ADD_CHAR - * - convert CAST(IS_BOOL,x) into BOOL(x) - */ -#include "Optimizer/pass1_5.c" - - /* pass 2: - * - convert non-numeric constants to numeric constants in numeric operators - * - optimize constant conditional JMPs - * - optimize static BRKs and CONTs - * - pre-evaluate constant function calls - */ -#include "Optimizer/pass2.c" - - /* pass 3: - * - optimize $i = $i+expr to $i+=expr - * - optimize series of JMPs - * - change $i++ to ++$i where possible - */ -#include "Optimizer/pass3.c" - -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO - /* pass 4: - * - INIT_FCALL_BY_NAME -> DO_FCALL - */ - if (ZEND_OPTIMIZER_PASS_4 & OPTIMIZATION_LEVEL) { - optimize_func_calls(op_array, script TSRMLS_CC); - } -#endif - - /* pass 5: - * - CFG optimization - */ -#include "Optimizer/pass5.c" - - /* pass 9: - * - Optimize temp variables usage - */ -#include "Optimizer/pass9.c" - - /* pass 10: - * - remove NOPs - */ -#include "Optimizer/pass10.c" - -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO - /* pass 11: - * - Compact literals table - */ - if (ZEND_OPTIMIZER_PASS_11 & OPTIMIZATION_LEVEL) { - optimizer_compact_literals(op_array TSRMLS_CC); - } -#endif -} - -static void zend_accel_optimize(zend_op_array *op_array, - zend_persistent_script *script, - HashTable **constants TSRMLS_DC) -{ - zend_op *opline, *end; - - /* Revert pass_two() */ - opline = op_array->opcodes; - end = opline + op_array->last; - while (opline < end) { -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO - if (opline->op1_type == IS_CONST) { - opline->op1.constant = opline->op1.literal - op_array->literals; - } - if (opline->op2_type == IS_CONST) { - opline->op2.constant = opline->op2.literal - op_array->literals; - } -#endif - switch (opline->opcode) { - case ZEND_JMP: -#if ZEND_EXTENSION_API_NO > PHP_5_2_X_API_NO - case ZEND_GOTO: -#endif -#if ZEND_EXTENSION_API_NO > PHP_5_4_X_API_NO - case ZEND_FAST_CALL: -#endif - ZEND_OP1(opline).opline_num = ZEND_OP1(opline).jmp_addr - op_array->opcodes; - break; - case ZEND_JMPZ: - case ZEND_JMPNZ: - case ZEND_JMPZ_EX: - case ZEND_JMPNZ_EX: -#if ZEND_EXTENSION_API_NO > PHP_5_2_X_API_NO - case ZEND_JMP_SET: -#endif -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO - case ZEND_JMP_SET_VAR: -#endif - ZEND_OP2(opline).opline_num = ZEND_OP2(opline).jmp_addr - op_array->opcodes; - break; - } - opline++; - } - - /* Do actual optimizations */ - zend_optimize(op_array, script, constants TSRMLS_CC); - - /* Redo pass_two() */ - opline = op_array->opcodes; - end = opline + op_array->last; - while (opline < end) { -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO - if (opline->op1_type == IS_CONST) { - opline->op1.zv = &op_array->literals[opline->op1.constant].constant; - } - if (opline->op2_type == IS_CONST) { - opline->op2.zv = &op_array->literals[opline->op2.constant].constant; - } -#endif - switch (opline->opcode) { - case ZEND_JMP: -#if ZEND_EXTENSION_API_NO > PHP_5_2_X_API_NO - case ZEND_GOTO: -#endif -#if ZEND_EXTENSION_API_NO > PHP_5_4_X_API_NO - case ZEND_FAST_CALL: -#endif - ZEND_OP1(opline).jmp_addr = &op_array->opcodes[ZEND_OP1(opline).opline_num]; - break; - case ZEND_JMPZ: - case ZEND_JMPNZ: - case ZEND_JMPZ_EX: - case ZEND_JMPNZ_EX: -#if ZEND_EXTENSION_API_NO > PHP_5_2_X_API_NO - case ZEND_JMP_SET: -#endif -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO - case ZEND_JMP_SET_VAR: -#endif - ZEND_OP2(opline).jmp_addr = &op_array->opcodes[ZEND_OP2(opline).opline_num]; - break; - } - ZEND_VM_SET_OPCODE_HANDLER(opline); - opline++; - } -} - -int zend_accel_script_optimize(zend_persistent_script *script TSRMLS_DC) -{ - Bucket *p, *q; - HashTable *constants = NULL; - - zend_accel_optimize(&script->main_op_array, script, &constants TSRMLS_CC); - - p = script->function_table.pListHead; - while (p) { - zend_op_array *op_array = (zend_op_array*)p->pData; - zend_accel_optimize(op_array, script, &constants TSRMLS_CC); - p = p->pListNext; - } - - p = script->class_table.pListHead; - while (p) { - zend_class_entry *ce = (zend_class_entry*)p->pDataPtr; - q = ce->function_table.pListHead; - while (q) { - zend_op_array *op_array = (zend_op_array*)q->pData; - if (op_array->scope == ce) { - zend_accel_optimize(op_array, script, &constants TSRMLS_CC); - } else if (op_array->type == ZEND_USER_FUNCTION) { - zend_op_array *orig_op_array; - if (zend_hash_find(&op_array->scope->function_table, q->arKey, q->nKeyLength, (void**)&orig_op_array) == SUCCESS) { - HashTable *ht = op_array->static_variables; - *op_array = *orig_op_array; - op_array->static_variables = ht; - } - } - q = q->pListNext; - } - p = p->pListNext; - } - - if (constants) { - zend_hash_destroy(constants); - efree(constants); - } - - return 1; -} +/* + +----------------------------------------------------------------------+ + | Zend OPcache | + +----------------------------------------------------------------------+ + | Copyright (c) 1998-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Andi Gutmans | + | Zeev Suraski | + | Stanislav Malyshev | + | Dmitry Stogov | + +----------------------------------------------------------------------+ +*/ + +#include "php.h" +#include "Optimizer/zend_optimizer.h" +#include "Optimizer/zend_optimizer_internal.h" +#include "zend_API.h" +#include "zend_constants.h" +#include "zend_execute.h" +#include "zend_vm.h" + +#define OPTIMIZATION_LEVEL \ + ZCG(accel_directives).optimization_level + +static void zend_optimizer_zval_dtor_wrapper(zval *zvalue) +{ + zval_dtor(zvalue); +} + +static void zend_optimizer_collect_constant(HashTable **constants, zval *name, zval* value) +{ + zval val; + + if (!*constants) { + *constants = emalloc(sizeof(HashTable)); + zend_hash_init(*constants, 16, NULL, (void (*)(void *))zend_optimizer_zval_dtor_wrapper, 0); + } + val = *value; + zval_copy_ctor(&val); + zend_hash_add(*constants, Z_STRVAL_P(name), Z_STRLEN_P(name)+1, (void**)&val, sizeof(zval), NULL); +} + +static int zend_optimizer_get_collected_constant(HashTable *constants, zval *name, zval* value) +{ + zval *val; + + if (zend_hash_find(constants, Z_STRVAL_P(name), Z_STRLEN_P(name)+1, (void**)&val) == SUCCESS) { + *value = *val; + zval_copy_ctor(value); + return 1; + } + return 0; +} + +#if ZEND_EXTENSION_API_NO >= PHP_5_5_X_API_NO +static int zend_optimizer_lookup_cv(zend_op_array *op_array, char* name, int name_len) +{ + int i = 0; + ulong hash_value = zend_inline_hash_func(name, name_len+1); + + while (i < op_array->last_var) { + if (op_array->vars[i].name == name || + (op_array->vars[i].hash_value == hash_value && + op_array->vars[i].name_len == name_len && + memcmp(op_array->vars[i].name, name, name_len) == 0)) { + return i; + } + i++; + } + i = op_array->last_var; + op_array->last_var++; + op_array->vars = erealloc(op_array->vars, op_array->last_var * sizeof(zend_compiled_variable)); + if (IS_INTERNED(name)) { + op_array->vars[i].name = name; + } else { + op_array->vars[i].name = estrndup(name, name_len); + } + op_array->vars[i].name_len = name_len; + op_array->vars[i].hash_value = hash_value; + return i; +} +#endif + +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO +int zend_optimizer_add_literal(zend_op_array *op_array, const zval *zv TSRMLS_DC) +{ + int i = op_array->last_literal; + op_array->last_literal++; + op_array->literals = (zend_literal*)erealloc(op_array->literals, op_array->last_literal * sizeof(zend_literal)); + op_array->literals[i].constant = *zv; + op_array->literals[i].hash_value = 0; + op_array->literals[i].cache_slot = -1; + Z_SET_REFCOUNT(op_array->literals[i].constant, 2); + Z_SET_ISREF(op_array->literals[i].constant); + return i; +} + +# define LITERAL_LONG(op, val) do { \ + zval _c; \ + ZVAL_LONG(&_c, val); \ + op.constant = zend_optimizer_add_literal(op_array, &_c TSRMLS_CC); \ + } while (0) + +# define LITERAL_BOOL(op, val) do { \ + zval _c; \ + ZVAL_BOOL(&_c, val); \ + op.constant = zend_optimizer_add_literal(op_array, &_c TSRMLS_CC); \ + } while (0) + +# define literal_dtor(zv) do { \ + zval_dtor(zv); \ + Z_TYPE_P(zv) = IS_NULL; \ + } while (0) + +#define COPY_NODE(target, src) do { \ + target ## _type = src ## _type; \ + target = src; \ + } while (0) + +#else + +# define LITERAL_LONG(op, val) ZVAL_LONG(&op.u.constant, val) + +# define LITERAL_BOOL(op, val) ZVAL_BOOL(&op.u.constant, val) + +# define literal_dtor(zv) zval_dtor(zv) + +#define COPY_NODE(target, src) do { \ + target = src; \ + } while (0) + +#endif + +static void update_op1_const(zend_op_array *op_array, + zend_op *opline, + zval *val TSRMLS_DC) +{ + if (opline->opcode == ZEND_FREE) { + MAKE_NOP(opline); + zval_dtor(val); + } else { + ZEND_OP1_TYPE(opline) = IS_CONST; +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO + if (Z_TYPE_P(val) == IS_STRING) { + switch (opline->opcode) { + case ZEND_INIT_STATIC_METHOD_CALL: + case ZEND_CATCH: + case ZEND_FETCH_CONSTANT: + opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); + Z_HASH_P(&ZEND_OP1_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)) + 1); + op_array->literals[opline->op1.constant].cache_slot = op_array->last_cache_slot++; + zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); + zend_optimizer_add_literal(op_array, val TSRMLS_CC); + op_array->literals[opline->op1.constant+1].hash_value = zend_hash_func(Z_STRVAL(op_array->literals[opline->op1.constant+1].constant), Z_STRLEN(op_array->literals[opline->op1.constant+1].constant) + 1); + break; + case ZEND_DO_FCALL: + zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); + opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); + Z_HASH_P(&ZEND_OP1_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)) + 1); + op_array->literals[opline->op1.constant].cache_slot = op_array->last_cache_slot++; + break; + default: + opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); + Z_HASH_P(&ZEND_OP1_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)) + 1); + break; + } + } else { + opline->op1.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); + } +#else + ZEND_OP1_LITERAL(opline) = *val; +#endif + } +} + +static void update_op2_const(zend_op_array *op_array, + zend_op *opline, + zval *val TSRMLS_DC) +{ + ZEND_OP2_TYPE(opline) = IS_CONST; +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO + opline->op2.constant = zend_optimizer_add_literal(op_array, val TSRMLS_CC); + if (Z_TYPE_P(val) == IS_STRING) { + Z_HASH_P(&ZEND_OP2_LITERAL(opline)) = zend_hash_func(Z_STRVAL(ZEND_OP2_LITERAL(opline)), Z_STRLEN(ZEND_OP2_LITERAL(opline)) + 1); + switch (opline->opcode) { + case ZEND_FETCH_R: + case ZEND_FETCH_W: + case ZEND_FETCH_RW: + case ZEND_FETCH_IS: + case ZEND_FETCH_UNSET: + case ZEND_FETCH_FUNC_ARG: + case ZEND_FETCH_CLASS: + case ZEND_INIT_FCALL_BY_NAME: + /*case ZEND_INIT_NS_FCALL_BY_NAME:*/ + case ZEND_UNSET_VAR: + case ZEND_ISSET_ISEMPTY_VAR: + case ZEND_ADD_INTERFACE: + case ZEND_ADD_TRAIT: + op_array->literals[opline->op2.constant].cache_slot = op_array->last_cache_slot++; + zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); + zend_optimizer_add_literal(op_array, val TSRMLS_CC); + op_array->literals[opline->op2.constant+1].hash_value = zend_hash_func(Z_STRVAL(op_array->literals[opline->op2.constant+1].constant), Z_STRLEN(op_array->literals[opline->op2.constant+1].constant) + 1); + break; + case ZEND_INIT_METHOD_CALL: + case ZEND_INIT_STATIC_METHOD_CALL: + zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); + zend_optimizer_add_literal(op_array, val TSRMLS_CC); + op_array->literals[opline->op2.constant+1].hash_value = zend_hash_func(Z_STRVAL(op_array->literals[opline->op2.constant+1].constant), Z_STRLEN(op_array->literals[opline->op2.constant+1].constant) + 1); + /* break missing intentionally */ + /*case ZEND_FETCH_CONSTANT:*/ + case ZEND_ASSIGN_OBJ: + case ZEND_FETCH_OBJ_R: + case ZEND_FETCH_OBJ_W: + case ZEND_FETCH_OBJ_RW: + case ZEND_FETCH_OBJ_IS: + case ZEND_FETCH_OBJ_UNSET: + case ZEND_FETCH_OBJ_FUNC_ARG: + case ZEND_UNSET_OBJ: + case ZEND_PRE_INC_OBJ: + case ZEND_PRE_DEC_OBJ: + case ZEND_POST_INC_OBJ: + case ZEND_POST_DEC_OBJ: + case ZEND_ISSET_ISEMPTY_PROP_OBJ: + op_array->literals[opline->op2.constant].cache_slot = op_array->last_cache_slot; + op_array->last_cache_slot += 2; + break; + case ZEND_ASSIGN_ADD: + case ZEND_ASSIGN_SUB: + case ZEND_ASSIGN_MUL: + case ZEND_ASSIGN_DIV: + case ZEND_ASSIGN_MOD: + case ZEND_ASSIGN_SL: + case ZEND_ASSIGN_SR: + case ZEND_ASSIGN_CONCAT: + case ZEND_ASSIGN_BW_OR: + case ZEND_ASSIGN_BW_AND: + case ZEND_ASSIGN_BW_XOR: + if (opline->extended_value == ZEND_ASSIGN_OBJ) { + op_array->literals[opline->op2.constant].cache_slot = op_array->last_cache_slot; + op_array->last_cache_slot += 2; + } + break; +#if ZEND_EXTENSION_API_NO >= PHP_5_4_X_API_NO + case ZEND_OP_DATA: + if ((opline-1)->opcode == ZEND_ASSIGN_DIM || + ((opline-1)->extended_value == ZEND_ASSIGN_DIM && + ((opline-1)->opcode == ZEND_ASSIGN_ADD || + (opline-1)->opcode == ZEND_ASSIGN_SUB || + (opline-1)->opcode == ZEND_ASSIGN_MUL || + (opline-1)->opcode == ZEND_ASSIGN_DIV || + (opline-1)->opcode == ZEND_ASSIGN_MOD || + (opline-1)->opcode == ZEND_ASSIGN_SL || + (opline-1)->opcode == ZEND_ASSIGN_SR || + (opline-1)->opcode == ZEND_ASSIGN_CONCAT || + (opline-1)->opcode == ZEND_ASSIGN_BW_OR || + (opline-1)->opcode == ZEND_ASSIGN_BW_AND || + (opline-1)->opcode == ZEND_ASSIGN_BW_XOR))) { + goto check_numeric; + } + break; + case ZEND_ISSET_ISEMPTY_DIM_OBJ: + case ZEND_ADD_ARRAY_ELEMENT: + case ZEND_INIT_ARRAY: + case ZEND_UNSET_DIM: + case ZEND_FETCH_DIM_R: + case ZEND_FETCH_DIM_W: + case ZEND_FETCH_DIM_RW: + case ZEND_FETCH_DIM_IS: + case ZEND_FETCH_DIM_FUNC_ARG: + case ZEND_FETCH_DIM_UNSET: + case ZEND_FETCH_DIM_TMP_VAR: +check_numeric: + { + ulong index; + int numeric = 0; + + ZEND_HANDLE_NUMERIC_EX(Z_STRVAL_P(val), Z_STRLEN_P(val)+1, index, numeric = 1); + if (numeric) { + zval_dtor(val); + ZVAL_LONG(val, index); + op_array->literals[opline->op2.constant].constant = *val; + } + } + break; +#endif + default: + break; + } + } +#else + ZEND_OP2_LITERAL(opline) = *val; +#endif +} + +static int replace_var_by_const(zend_op_array *op_array, + zend_op *opline, + zend_uint var, + zval *val TSRMLS_DC) +{ + zend_op *end = op_array->opcodes + op_array->last; + + while (opline < end) { + if (ZEND_OP1_TYPE(opline) == IS_VAR && + ZEND_OP1(opline).var == var) { + switch (opline->opcode) { + case ZEND_FETCH_DIM_W: + case ZEND_FETCH_DIM_RW: + case ZEND_FETCH_DIM_FUNC_ARG: + case ZEND_FETCH_DIM_UNSET: + case ZEND_ASSIGN_DIM: +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO + case ZEND_SEPARATE: +#endif + return 0; + case ZEND_SEND_VAR_NO_REF: + if (opline->extended_value & ZEND_ARG_COMPILE_TIME_BOUND) { + if (opline->extended_value & ZEND_ARG_SEND_BY_REF) { + return 0; + } + opline->extended_value = ZEND_DO_FCALL; + } else { + opline->extended_value = ZEND_DO_FCALL_BY_NAME; + } + opline->opcode = ZEND_SEND_VAL; + break; + default: + break; + } + update_op1_const(op_array, opline, val TSRMLS_CC); + break; + } + + if (ZEND_OP2_TYPE(opline) == IS_VAR && + ZEND_OP2(opline).var == var) { + switch (opline->opcode) { + case ZEND_ASSIGN_REF: + return 0; + default: + break; + } + update_op2_const(op_array, opline, val TSRMLS_CC); + break; + } + opline++; + } + + return 1; +} + +static void replace_tmp_by_const(zend_op_array *op_array, + zend_op *opline, + zend_uint var, + zval *val + TSRMLS_DC) +{ + zend_op *end = op_array->opcodes + op_array->last; + + while (opline < end) { + if (ZEND_OP1_TYPE(opline) == IS_TMP_VAR && + ZEND_OP1(opline).var == var) { + + update_op1_const(op_array, opline, val TSRMLS_CC); + /* TMP_VAR my be used only once */ + break; + } + + if (ZEND_OP2_TYPE(opline) == IS_TMP_VAR && + ZEND_OP2(opline).var == var) { + + update_op2_const(op_array, opline, val TSRMLS_CC); + /* TMP_VAR my be used only once */ + break; + } + opline++; + } +} + +#include "Optimizer/nop_removal.c" +#include "Optimizer/block_pass.c" +#include "Optimizer/optimize_temp_vars_5.c" +#include "Optimizer/compact_literals.c" +#include "Optimizer/optimize_func_calls.c" + +static void zend_optimize(zend_op_array *op_array, + zend_persistent_script *script, + HashTable **constants TSRMLS_DC) +{ + if (op_array->type == ZEND_EVAL_CODE || + (op_array->fn_flags & ZEND_ACC_INTERACTIVE)) { + return; + } + + /* pass 1 + * - substitute persistent constants (true, false, null, etc) + * - perform compile-time evaluation of constant binary and unary operations + * - optimize series of ADD_STRING and/or ADD_CHAR + * - convert CAST(IS_BOOL,x) into BOOL(x) + */ +#include "Optimizer/pass1_5.c" + + /* pass 2: + * - convert non-numeric constants to numeric constants in numeric operators + * - optimize constant conditional JMPs + * - optimize static BRKs and CONTs + * - pre-evaluate constant function calls + */ +#include "Optimizer/pass2.c" + + /* pass 3: + * - optimize $i = $i+expr to $i+=expr + * - optimize series of JMPs + * - change $i++ to ++$i where possible + */ +#include "Optimizer/pass3.c" + +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO + /* pass 4: + * - INIT_FCALL_BY_NAME -> DO_FCALL + */ + if (ZEND_OPTIMIZER_PASS_4 & OPTIMIZATION_LEVEL) { + optimize_func_calls(op_array, script TSRMLS_CC); + } +#endif + + /* pass 5: + * - CFG optimization + */ +#include "Optimizer/pass5.c" + + /* pass 9: + * - Optimize temp variables usage + */ +#include "Optimizer/pass9.c" + + /* pass 10: + * - remove NOPs + */ +#include "Optimizer/pass10.c" + +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO + /* pass 11: + * - Compact literals table + */ + if (ZEND_OPTIMIZER_PASS_11 & OPTIMIZATION_LEVEL) { + optimizer_compact_literals(op_array TSRMLS_CC); + } +#endif +} + +static void zend_accel_optimize(zend_op_array *op_array, + zend_persistent_script *script, + HashTable **constants TSRMLS_DC) +{ + zend_op *opline, *end; + + /* Revert pass_two() */ + opline = op_array->opcodes; + end = opline + op_array->last; + while (opline < end) { +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO + if (opline->op1_type == IS_CONST) { + opline->op1.constant = opline->op1.literal - op_array->literals; + } + if (opline->op2_type == IS_CONST) { + opline->op2.constant = opline->op2.literal - op_array->literals; + } +#endif + switch (opline->opcode) { + case ZEND_JMP: +#if ZEND_EXTENSION_API_NO > PHP_5_2_X_API_NO + case ZEND_GOTO: +#endif +#if ZEND_EXTENSION_API_NO > PHP_5_4_X_API_NO + case ZEND_FAST_CALL: +#endif + ZEND_OP1(opline).opline_num = ZEND_OP1(opline).jmp_addr - op_array->opcodes; + break; + case ZEND_JMPZ: + case ZEND_JMPNZ: + case ZEND_JMPZ_EX: + case ZEND_JMPNZ_EX: +#if ZEND_EXTENSION_API_NO > PHP_5_2_X_API_NO + case ZEND_JMP_SET: +#endif +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO + case ZEND_JMP_SET_VAR: +#endif + ZEND_OP2(opline).opline_num = ZEND_OP2(opline).jmp_addr - op_array->opcodes; + break; + } + opline++; + } + + /* Do actual optimizations */ + zend_optimize(op_array, script, constants TSRMLS_CC); + + /* Redo pass_two() */ + opline = op_array->opcodes; + end = opline + op_array->last; + while (opline < end) { +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO + if (opline->op1_type == IS_CONST) { + opline->op1.zv = &op_array->literals[opline->op1.constant].constant; + } + if (opline->op2_type == IS_CONST) { + opline->op2.zv = &op_array->literals[opline->op2.constant].constant; + } +#endif + switch (opline->opcode) { + case ZEND_JMP: +#if ZEND_EXTENSION_API_NO > PHP_5_2_X_API_NO + case ZEND_GOTO: +#endif +#if ZEND_EXTENSION_API_NO > PHP_5_4_X_API_NO + case ZEND_FAST_CALL: +#endif + ZEND_OP1(opline).jmp_addr = &op_array->opcodes[ZEND_OP1(opline).opline_num]; + break; + case ZEND_JMPZ: + case ZEND_JMPNZ: + case ZEND_JMPZ_EX: + case ZEND_JMPNZ_EX: +#if ZEND_EXTENSION_API_NO > PHP_5_2_X_API_NO + case ZEND_JMP_SET: +#endif +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO + case ZEND_JMP_SET_VAR: +#endif + ZEND_OP2(opline).jmp_addr = &op_array->opcodes[ZEND_OP2(opline).opline_num]; + break; + } + ZEND_VM_SET_OPCODE_HANDLER(opline); + opline++; + } +} + +int zend_accel_script_optimize(zend_persistent_script *script TSRMLS_DC) +{ + Bucket *p, *q; + HashTable *constants = NULL; + + zend_accel_optimize(&script->main_op_array, script, &constants TSRMLS_CC); + + p = script->function_table.pListHead; + while (p) { + zend_op_array *op_array = (zend_op_array*)p->pData; + zend_accel_optimize(op_array, script, &constants TSRMLS_CC); + p = p->pListNext; + } + + p = script->class_table.pListHead; + while (p) { + zend_class_entry *ce = (zend_class_entry*)p->pDataPtr; + q = ce->function_table.pListHead; + while (q) { + zend_op_array *op_array = (zend_op_array*)q->pData; + if (op_array->scope == ce) { + zend_accel_optimize(op_array, script, &constants TSRMLS_CC); + } else if (op_array->type == ZEND_USER_FUNCTION) { + zend_op_array *orig_op_array; + if (zend_hash_find(&op_array->scope->function_table, q->arKey, q->nKeyLength, (void**)&orig_op_array) == SUCCESS) { + HashTable *ht = op_array->static_variables; + *op_array = *orig_op_array; + op_array->static_variables = ht; + } + } + q = q->pListNext; + } + p = p->pListNext; + } + + if (constants) { + zend_hash_destroy(constants); + efree(constants); + } + + return 1; +} diff --git a/ext/opcache/Optimizer/zend_optimizer.h b/ext/opcache/Optimizer/zend_optimizer.h index 5a3d715ac9..f324d9ffa1 100644 --- a/ext/opcache/Optimizer/zend_optimizer.h +++ b/ext/opcache/Optimizer/zend_optimizer.h @@ -1,47 +1,47 @@ -/* - +----------------------------------------------------------------------+ - | Zend OPcache | - +----------------------------------------------------------------------+ - | Copyright (c) 1998-2013 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Authors: Andi Gutmans | - | Zeev Suraski | - | Stanislav Malyshev | - | Dmitry Stogov | - +----------------------------------------------------------------------+ -*/ - -#ifndef ZEND_OPTIMIZER_H -#define ZEND_OPTIMIZER_H - -#include "zend.h" -#include "zend_compile.h" - -#define ZEND_OPTIMIZER_PASS_1 (1<<0) /* CSE, STRING construction */ -#define ZEND_OPTIMIZER_PASS_2 (1<<1) /* Constant conversion and jumps */ -#define ZEND_OPTIMIZER_PASS_3 (1<<2) /* ++, +=, series of jumps */ -#define ZEND_OPTIMIZER_PASS_4 (1<<3) /* INIT_FCALL_BY_NAME -> DO_FCALL */ -#define ZEND_OPTIMIZER_PASS_5 (1<<4) /* CFG based optimization */ -#define ZEND_OPTIMIZER_PASS_6 (1<<5) -#define ZEND_OPTIMIZER_PASS_7 (1<<6) -#define ZEND_OPTIMIZER_PASS_8 (1<<7) -#define ZEND_OPTIMIZER_PASS_9 (1<<8) /* TMP VAR usage */ -#define ZEND_OPTIMIZER_PASS_10 (1<<9) /* NOP removal */ -#define ZEND_OPTIMIZER_PASS_11 (1<<10) /* Merge equal constants */ -#define ZEND_OPTIMIZER_PASS_12 (1<<11) -#define ZEND_OPTIMIZER_PASS_13 (1<<12) -#define ZEND_OPTIMIZER_PASS_14 (1<<13) - -#define ZEND_OPTIMIZER_ALL_PASSES 0xFFFFFFFF - -#define DEFAULT_OPTIMIZATION_LEVEL "0xFFFFFFFF" - -#endif +/* + +----------------------------------------------------------------------+ + | Zend OPcache | + +----------------------------------------------------------------------+ + | Copyright (c) 1998-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Andi Gutmans | + | Zeev Suraski | + | Stanislav Malyshev | + | Dmitry Stogov | + +----------------------------------------------------------------------+ +*/ + +#ifndef ZEND_OPTIMIZER_H +#define ZEND_OPTIMIZER_H + +#include "zend.h" +#include "zend_compile.h" + +#define ZEND_OPTIMIZER_PASS_1 (1<<0) /* CSE, STRING construction */ +#define ZEND_OPTIMIZER_PASS_2 (1<<1) /* Constant conversion and jumps */ +#define ZEND_OPTIMIZER_PASS_3 (1<<2) /* ++, +=, series of jumps */ +#define ZEND_OPTIMIZER_PASS_4 (1<<3) /* INIT_FCALL_BY_NAME -> DO_FCALL */ +#define ZEND_OPTIMIZER_PASS_5 (1<<4) /* CFG based optimization */ +#define ZEND_OPTIMIZER_PASS_6 (1<<5) +#define ZEND_OPTIMIZER_PASS_7 (1<<6) +#define ZEND_OPTIMIZER_PASS_8 (1<<7) +#define ZEND_OPTIMIZER_PASS_9 (1<<8) /* TMP VAR usage */ +#define ZEND_OPTIMIZER_PASS_10 (1<<9) /* NOP removal */ +#define ZEND_OPTIMIZER_PASS_11 (1<<10) /* Merge equal constants */ +#define ZEND_OPTIMIZER_PASS_12 (1<<11) +#define ZEND_OPTIMIZER_PASS_13 (1<<12) +#define ZEND_OPTIMIZER_PASS_14 (1<<13) + +#define ZEND_OPTIMIZER_ALL_PASSES 0xFFFFFFFF + +#define DEFAULT_OPTIMIZATION_LEVEL "0xFFFFFFFF" + +#endif diff --git a/ext/opcache/Optimizer/zend_optimizer_internal.h b/ext/opcache/Optimizer/zend_optimizer_internal.h index 616bdf74f6..657b480316 100644 --- a/ext/opcache/Optimizer/zend_optimizer_internal.h +++ b/ext/opcache/Optimizer/zend_optimizer_internal.h @@ -1,86 +1,86 @@ -/* - +----------------------------------------------------------------------+ - | Zend OPcache | - +----------------------------------------------------------------------+ - | Copyright (c) 1998-2013 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Authors: Andi Gutmans | - | Zeev Suraski | - | Stanislav Malyshev | - | Dmitry Stogov | - +----------------------------------------------------------------------+ -*/ - -#ifndef ZEND_OPTIMIZER_INTERNAL_H -#define ZEND_OPTIMIZER_INTERNAL_H - -#include "ZendAccelerator.h" - -#if ZEND_EXTENSION_API_NO > PHP_5_4_X_API_NO -# define VAR_NUM(v) ((zend_uint)(EX_TMP_VAR_NUM(0, 0) - EX_TMP_VAR(0, v))) -# define NUM_VAR(v) ((zend_uint)(zend_uintptr_t)EX_TMP_VAR_NUM(0, v)) -#elif ZEND_EXTENSION_API_NO > PHP_5_2_X_API_NO -# define VAR_NUM(v) ((v)/ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable))) -# define NUM_VAR(v) ((v)*ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable))) -#else -# define VAR_NUM(v) ((v)/(sizeof(temp_variable))) -# define NUM_VAR(v) ((v)*(sizeof(temp_variable))) -#endif - -#define INV_COND(op) ((op) == ZEND_JMPZ ? ZEND_JMPNZ : ZEND_JMPZ) -#define INV_EX_COND(op) ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ : ZEND_JMPZ) -#define INV_COND_EX(op) ((op) == ZEND_JMPZ ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX) -#define INV_EX_COND_EX(op) ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX) - -#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO -# define MAKE_NOP(opline) { opline->opcode = ZEND_NOP; memset(&opline->result,0,sizeof(opline->result)); memset(&opline->op1,0,sizeof(opline->op1)); memset(&opline->op2,0,sizeof(opline->op2)); opline->result_type=opline->op1_type=opline->op2_type=IS_UNUSED; opline->handler = zend_opcode_handlers[ZEND_NOP]; } -# define RESULT_USED(op) (((op->result_type & IS_VAR) && !(op->result_type & EXT_TYPE_UNUSED)) || op->result_type == IS_TMP_VAR) -# define RESULT_UNUSED(op) ((op->result_type & EXT_TYPE_UNUSED) != 0) -# define SAME_VAR(op1, op2) ((((op1 ## _type & IS_VAR) && (op2 ## _type & IS_VAR)) || (op1 ## _type == IS_TMP_VAR && op2 ## _type == IS_TMP_VAR)) && op1.var == op2.var) -#else -# define MAKE_NOP(opline) { opline->opcode = ZEND_NOP; memset(&opline->result,0,sizeof(znode)); memset(&opline->op1,0,sizeof(znode)); memset(&opline->op2,0,sizeof(znode)); opline->result.op_type=opline->op1.op_type=opline->op2.op_type=IS_UNUSED; opline->handler = zend_opcode_handlers[ZEND_NOP]; } -# define RESULT_USED(op) ((op->result.op_type == IS_VAR && (op->result.u.EA.type & EXT_TYPE_UNUSED) == 0) || (op->result.op_type == IS_TMP_VAR)) -# define RESULT_UNUSED(op) ((op->result.op_type == IS_VAR) && (op->result.u.EA.type == EXT_TYPE_UNUSED)) -# define SAME_VAR(op1, op2) (((op1.op_type == IS_VAR && op2.op_type == IS_VAR) || (op1.op_type == IS_TMP_VAR && op2.op_type == IS_TMP_VAR)) && op1.u.var == op2.u.var) -#endif - -typedef struct _zend_code_block zend_code_block; -typedef struct _zend_block_source zend_block_source; - -struct _zend_code_block { - int access; - zend_op *start_opline; - int start_opline_no; - int len; - zend_code_block *op1_to; - zend_code_block *op2_to; - zend_code_block *ext_to; - zend_code_block *follow_to; - zend_code_block *next; - zend_block_source *sources; - zend_bool protected; /* don't merge this block with others */ -}; - -typedef struct _zend_cfg { - zend_code_block *blocks; - zend_code_block **try; - zend_code_block **catch; - zend_code_block **loop_start; - zend_code_block **loop_cont; - zend_code_block **loop_brk; -} zend_cfg; - -struct _zend_block_source { - zend_code_block *from; - zend_block_source *next; -}; - -#endif +/* + +----------------------------------------------------------------------+ + | Zend OPcache | + +----------------------------------------------------------------------+ + | Copyright (c) 1998-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Andi Gutmans | + | Zeev Suraski | + | Stanislav Malyshev | + | Dmitry Stogov | + +----------------------------------------------------------------------+ +*/ + +#ifndef ZEND_OPTIMIZER_INTERNAL_H +#define ZEND_OPTIMIZER_INTERNAL_H + +#include "ZendAccelerator.h" + +#if ZEND_EXTENSION_API_NO > PHP_5_4_X_API_NO +# define VAR_NUM(v) ((zend_uint)(EX_TMP_VAR_NUM(0, 0) - EX_TMP_VAR(0, v))) +# define NUM_VAR(v) ((zend_uint)(zend_uintptr_t)EX_TMP_VAR_NUM(0, v)) +#elif ZEND_EXTENSION_API_NO > PHP_5_2_X_API_NO +# define VAR_NUM(v) ((v)/ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable))) +# define NUM_VAR(v) ((v)*ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable))) +#else +# define VAR_NUM(v) ((v)/(sizeof(temp_variable))) +# define NUM_VAR(v) ((v)*(sizeof(temp_variable))) +#endif + +#define INV_COND(op) ((op) == ZEND_JMPZ ? ZEND_JMPNZ : ZEND_JMPZ) +#define INV_EX_COND(op) ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ : ZEND_JMPZ) +#define INV_COND_EX(op) ((op) == ZEND_JMPZ ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX) +#define INV_EX_COND_EX(op) ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX) + +#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO +# define MAKE_NOP(opline) { opline->opcode = ZEND_NOP; memset(&opline->result,0,sizeof(opline->result)); memset(&opline->op1,0,sizeof(opline->op1)); memset(&opline->op2,0,sizeof(opline->op2)); opline->result_type=opline->op1_type=opline->op2_type=IS_UNUSED; opline->handler = zend_opcode_handlers[ZEND_NOP]; } +# define RESULT_USED(op) (((op->result_type & IS_VAR) && !(op->result_type & EXT_TYPE_UNUSED)) || op->result_type == IS_TMP_VAR) +# define RESULT_UNUSED(op) ((op->result_type & EXT_TYPE_UNUSED) != 0) +# define SAME_VAR(op1, op2) ((((op1 ## _type & IS_VAR) && (op2 ## _type & IS_VAR)) || (op1 ## _type == IS_TMP_VAR && op2 ## _type == IS_TMP_VAR)) && op1.var == op2.var) +#else +# define MAKE_NOP(opline) { opline->opcode = ZEND_NOP; memset(&opline->result,0,sizeof(znode)); memset(&opline->op1,0,sizeof(znode)); memset(&opline->op2,0,sizeof(znode)); opline->result.op_type=opline->op1.op_type=opline->op2.op_type=IS_UNUSED; opline->handler = zend_opcode_handlers[ZEND_NOP]; } +# define RESULT_USED(op) ((op->result.op_type == IS_VAR && (op->result.u.EA.type & EXT_TYPE_UNUSED) == 0) || (op->result.op_type == IS_TMP_VAR)) +# define RESULT_UNUSED(op) ((op->result.op_type == IS_VAR) && (op->result.u.EA.type == EXT_TYPE_UNUSED)) +# define SAME_VAR(op1, op2) (((op1.op_type == IS_VAR && op2.op_type == IS_VAR) || (op1.op_type == IS_TMP_VAR && op2.op_type == IS_TMP_VAR)) && op1.u.var == op2.u.var) +#endif + +typedef struct _zend_code_block zend_code_block; +typedef struct _zend_block_source zend_block_source; + +struct _zend_code_block { + int access; + zend_op *start_opline; + int start_opline_no; + int len; + zend_code_block *op1_to; + zend_code_block *op2_to; + zend_code_block *ext_to; + zend_code_block *follow_to; + zend_code_block *next; + zend_block_source *sources; + zend_bool protected; /* don't merge this block with others */ +}; + +typedef struct _zend_cfg { + zend_code_block *blocks; + zend_code_block **try; + zend_code_block **catch; + zend_code_block **loop_start; + zend_code_block **loop_cont; + zend_code_block **loop_brk; +} zend_cfg; + +struct _zend_block_source { + zend_code_block *from; + zend_block_source *next; +}; + +#endif From 6699b1f8ead84c5ea96df4b4eae5f9b6ed96bc5d Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Mon, 30 Dec 2013 10:58:01 +0800 Subject: [PATCH 3/8] Fix dos style eol in phpts --- ext/opcache/tests/issue0115.phpt | 96 ++++++++++++++++---------------- ext/opcache/tests/issue0140.phpt | 86 ++++++++++++++-------------- ext/opcache/tests/issue0149.phpt | 70 +++++++++++------------ 3 files changed, 126 insertions(+), 126 deletions(-) diff --git a/ext/opcache/tests/issue0115.phpt b/ext/opcache/tests/issue0115.phpt index a1e469ff2f..0dfdd9f0eb 100644 --- a/ext/opcache/tests/issue0115.phpt +++ b/ext/opcache/tests/issue0115.phpt @@ -1,48 +1,48 @@ ---TEST-- -ISSUE #115 (path issue when using phar) ---INI-- -opcache.enable=1 -opcache.enable_cli=1 -phar.readonly=0 ---SKIPIF-- - - - ---FILE-- -'; -$p = new Phar(__DIR__ . '/issue0115_1.phar.php', 0, 'this'); -$p['index.php'] = 'setStub($stub); -unset($p); -$p = new Phar(__DIR__ . '/issue0115_2.phar.php', 0, 'this'); -$p['index.php'] = 'setStub($stub); -unset($p); - -include "php_cli_server.inc"; -php_cli_server_start('-d opcache.enable=1 -d opcache.enable_cli=1'); -echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/issue0115_1.phar.php'); -echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/issue0115_2.phar.php'); -?> ---CLEAN-- - ---EXPECT-- -Hello from Index 1. -Hello World 1! -Hello from Index 2. -Hello World 2! +--TEST-- +ISSUE #115 (path issue when using phar) +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +phar.readonly=0 +--SKIPIF-- + + + +--FILE-- +'; +$p = new Phar(__DIR__ . '/issue0115_1.phar.php', 0, 'this'); +$p['index.php'] = 'setStub($stub); +unset($p); +$p = new Phar(__DIR__ . '/issue0115_2.phar.php', 0, 'this'); +$p['index.php'] = 'setStub($stub); +unset($p); + +include "php_cli_server.inc"; +php_cli_server_start('-d opcache.enable=1 -d opcache.enable_cli=1'); +echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/issue0115_1.phar.php'); +echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/issue0115_2.phar.php'); +?> +--CLEAN-- + +--EXPECT-- +Hello from Index 1. +Hello World 1! +Hello from Index 2. +Hello World 2! diff --git a/ext/opcache/tests/issue0140.phpt b/ext/opcache/tests/issue0140.phpt index 7c0d6b92b3..98e0e45cc2 100644 --- a/ext/opcache/tests/issue0140.phpt +++ b/ext/opcache/tests/issue0140.phpt @@ -1,43 +1,43 @@ ---TEST-- -Issue #140: "opcache.enable_file_override" doesn't respect "opcache.revalidate_freq" ---INI-- -opcache.enable=1 -opcache.enable_cli=1 -opcache.revalidate_freq=0 -opcache.file_update_protection=0 ---SKIPIF-- - - ---FILE-- - ---EXPECTF-- -bool(true) -1 -int(%d) -bool(true) -2 -int(%d) -bool(false) -bool(false) -bool(false) +--TEST-- +Issue #140: "opcache.enable_file_override" doesn't respect "opcache.revalidate_freq" +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.revalidate_freq=0 +opcache.file_update_protection=0 +--SKIPIF-- + + +--FILE-- + +--EXPECTF-- +bool(true) +1 +int(%d) +bool(true) +2 +int(%d) +bool(false) +bool(false) +bool(false) diff --git a/ext/opcache/tests/issue0149.phpt b/ext/opcache/tests/issue0149.phpt index 7044d39388..8c7f1bb7e0 100644 --- a/ext/opcache/tests/issue0149.phpt +++ b/ext/opcache/tests/issue0149.phpt @@ -1,35 +1,35 @@ ---TEST-- -ISSUE #149 (Phar mount points not working this OPcache enabled) ---INI-- -opcache.enable=1 -opcache.enable_cli=1 -phar.readonly=0 ---SKIPIF-- - - - ---FILE-- -"; -$p = new Phar(__DIR__ . '/issue0149.phar.php', 0, 'this'); -$p['index.php'] = ""; # A Phar must have at least one file, hence this dummy -$p->setStub($stub); -unset($p); - -include "php_cli_server.inc"; -php_cli_server_start('-d opcache.enable=1 -d opcache.enable_cli=1'); -echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/issue0149.phar.php'); -echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/issue0149.phar.php'); -echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/issue0149.phar.php'); -?> ---CLEAN-- - ---EXPECT-- -OK -OK -OK +--TEST-- +ISSUE #149 (Phar mount points not working this OPcache enabled) +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +phar.readonly=0 +--SKIPIF-- + + + +--FILE-- +"; +$p = new Phar(__DIR__ . '/issue0149.phar.php', 0, 'this'); +$p['index.php'] = ""; # A Phar must have at least one file, hence this dummy +$p->setStub($stub); +unset($p); + +include "php_cli_server.inc"; +php_cli_server_start('-d opcache.enable=1 -d opcache.enable_cli=1'); +echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/issue0149.phar.php'); +echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/issue0149.phar.php'); +echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/issue0149.phar.php'); +?> +--CLEAN-- + +--EXPECT-- +OK +OK +OK From 31d4d11432983b441832863983ae6e9d1dd614ba Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Mon, 30 Dec 2013 10:58:40 +0800 Subject: [PATCH 4/8] Fix one more test in dos style eol --- ext/opcache/tests/bug66338.phpt | 84 ++++++++++++++++----------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/ext/opcache/tests/bug66338.phpt b/ext/opcache/tests/bug66338.phpt index cf0018df94..ed7f8ab475 100644 --- a/ext/opcache/tests/bug66338.phpt +++ b/ext/opcache/tests/bug66338.phpt @@ -1,42 +1,42 @@ ---TEST-- -Bug #66338 (Optimization binding of class constants is not safely opcacheable) ---INI-- -opcache.enable=0 ---SKIPIF-- - ---FILE-- - ---EXPECT-- -The President of the USA is Barack Hussein Obama II -The Prime Minister of the UK is David William Donald Cameron +--TEST-- +Bug #66338 (Optimization binding of class constants is not safely opcacheable) +--INI-- +opcache.enable=0 +--SKIPIF-- + +--FILE-- + +--EXPECT-- +The President of the USA is Barack Hussein Obama II +The Prime Minister of the UK is David William Donald Cameron From ed5be4f7ad9255f13f0e1263e0b88181dc3c31d8 Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Mon, 30 Dec 2013 11:07:25 +0800 Subject: [PATCH 5/8] Fix test fail --- ext/opcache/tests/blacklist.phpt | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/opcache/tests/blacklist.phpt b/ext/opcache/tests/blacklist.phpt index 57e4c306dd..18c205cacf 100644 --- a/ext/opcache/tests/blacklist.phpt +++ b/ext/opcache/tests/blacklist.phpt @@ -4,6 +4,7 @@ Blacklist (with glob, quote and comments) opcache.enable=1 opcache.enable_cli=1 opcache.blacklist_filename={PWD}/opcache-*.blacklist +opcache.file_update_protection=0 --SKIPIF-- --FILE-- From 0a950a05005f94e56bd2a42b99a0961db86952db Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Mon, 30 Dec 2013 07:35:30 +0100 Subject: [PATCH 6/8] Sync with pecl/zip 1.12.4dev - update bunled libzip to 0.11.2 - expose zip_file_set_external_attributes + zip_file_get_external_attributes with new methods: ZipArchive::setExternalAttributesName ZipArchive::setExternalAttributesIndex ZipArchive::getExternalAttributesName ZipArchive::getExternalAttributesIndex --- ext/zip/config.m4 | 13 +- ext/zip/config.w32 | 4 +- ext/zip/lib/config.h | 6 +- ext/zip/lib/php_zip_config.w32.h | 3 + ext/zip/lib/zip.h | 80 +++++---- ext/zip/lib/zip_add.c | 2 +- ext/zip/lib/zip_add_entry.c | 3 +- ext/zip/lib/zip_close.c | 35 ++-- ext/zip/lib/zip_dir_add.c | 19 ++- ext/zip/lib/zip_dirent.c | 18 +-- ext/zip/lib/zip_discard.c | 1 + ext/zip/lib/zip_extra_field.c | 2 +- ext/zip/lib/zip_extra_field_api.c | 10 ++ ext/zip/lib/zip_fclose.c | 1 + ext/zip/lib/zip_file_get_offset.c | 2 +- ext/zip/lib/zip_get_archive_flag.c | 2 +- ext/zip/lib/zip_get_num_files.c | 2 +- ext/zip/lib/zip_open.c | 7 +- ext/zip/lib/zip_set_archive_flag.c | 2 +- ext/zip/lib/zip_set_file_compression.c | 2 +- ext/zip/lib/zip_set_name.c | 4 +- ext/zip/lib/zip_source_crc.c | 4 +- ext/zip/lib/zip_source_deflate.c | 12 +- ext/zip/lib/zip_source_filep.c | 9 +- ext/zip/lib/zip_source_pkware.c | 2 +- ext/zip/lib/zip_source_zip_new.c | 12 +- ext/zip/lib/zip_stat_index.c | 2 +- ext/zip/lib/zip_string.c | 2 +- ext/zip/lib/zipint.h | 36 +++-- ext/zip/php_zip.c | 214 ++++++++++++++++++++++++- ext/zip/php_zip.h | 2 +- 31 files changed, 401 insertions(+), 112 deletions(-) diff --git a/ext/zip/config.m4 b/ext/zip/config.m4 index 56ae6a5b29..a21ad2d3e1 100644 --- a/ext/zip/config.m4 +++ b/ext/zip/config.m4 @@ -63,9 +63,14 @@ if test "$PHP_ZIP" != "no"; then AC_MSG_RESULT(from option: found in $PHP_LIBZIP) elif test -x "$PKG_CONFIG" && $PKG_CONFIG --exists libzip; then - LIBZIP_CFLAGS=`$PKG_CONFIG libzip --cflags` - LIBZIP_LIBDIR=`$PKG_CONFIG libzip --variable=libdir` - AC_MSG_RESULT(from pkgconfig: found in $LIBZIP_LIBDIR) + if $PKG_CONFIG libzip --atleast-version 0.11; then + LIBZIP_CFLAGS=`$PKG_CONFIG libzip --cflags` + LIBZIP_LIBDIR=`$PKG_CONFIG libzip --variable=libdir` + LIBZIP_VERSON=`$PKG_CONFIG libzip --modversion` + AC_MSG_RESULT(from pkgconfig: version $LIBZIP_VERSON found in $LIBZIP_LIBDIR) + else + AC_MSG_ERROR(system libzip must be upgraded to version >= 0.11) + fi else for i in /usr/local /usr; do @@ -107,6 +112,7 @@ if test "$PHP_ZIP" != "no"; then lib/zip_fclose.c lib/zip_fdopen.c lib/zip_file_add.c lib/zip_file_error_clear.c lib/zip_file_error_get.c\ lib/zip_file_get_comment.c lib/zip_file_get_offset.c lib/zip_file_rename.c lib/zip_file_replace.c\ lib/zip_file_set_comment.c lib/zip_file_strerror.c lib/zip_filerange_crc.c lib/zip_fopen.c\ + lib/zip_file_get_external_attributes.c lib/zip_file_set_external_attributes.c \ lib/zip_fopen_encrypted.c lib/zip_fopen_index.c lib/zip_fopen_index_encrypted.c lib/zip_fread.c\ lib/zip_get_archive_comment.c lib/zip_get_archive_flag.c lib/zip_get_compression_implementation.c\ lib/zip_get_encryption_implementation.c lib/zip_get_file_comment.c lib/zip_get_name.c lib/zip_get_num_entries.c \ @@ -122,6 +128,7 @@ if test "$PHP_ZIP" != "no"; then AC_DEFINE(HAVE_ZIP,1,[ ]) PHP_NEW_EXTENSION(zip, php_zip.c zip_stream.c $PHP_ZIP_SOURCES, $ext_shared) PHP_ADD_BUILD_DIR($ext_builddir/lib, 1) + PHP_ADD_INCLUDE([$ext_srcdir/lib]) PHP_SUBST(ZIP_SHARED_LIBADD) fi diff --git a/ext/zip/config.w32 b/ext/zip/config.w32 index 5b9f09575a..28de344785 100644 --- a/ext/zip/config.w32 +++ b/ext/zip/config.w32 @@ -5,6 +5,7 @@ ARG_ENABLE("zip", "ZIP support", "yes"); if (PHP_ZIP != "no") { if (CHECK_HEADER_ADD_INCLUDE("zlib.h", "CFLAGS_ZIP", "..\\zlib;" + PHP_ZIP) && + CHECK_HEADER_ADD_INCLUDE("zipconf.h", "CFLAGS_ZIP", configure_module_dirname + "\\lib;" + PHP_ZIP) && (((PHP_ZLIB=="no") && (CHECK_LIB("zlib_a.lib", "zip", PHP_ZIP) || CHECK_LIB("zlib.lib", "zip", PHP_ZIP))) || (PHP_ZLIB_SHARED && CHECK_LIB("zlib.lib", "zip", PHP_ZIP)) || (PHP_ZLIB == "yes" && (!PHP_ZLIB_SHARED))) ) { @@ -26,7 +27,8 @@ if (PHP_ZIP != "no") { zip_source_function.c zip_source_layered.c zip_source_open.c zip_source_pkware.c zip_source_pop.c\ zip_source_read.c zip_source_stat.c zip_source_window.c zip_source_zip.c zip_source_zip_new.c\ zip_stat.c zip_stat_index.c zip_stat_init.c zip_strerror.c zip_string.c zip_unchange.c zip_unchange_all.c\ - zip_unchange_archive.c zip_unchange_data.c zip_utf-8.c mkstemp.c", "zip"); + zip_unchange_archive.c zip_unchange_data.c zip_utf-8.c mkstemp.c \ + zip_file_set_external_attributes.c zip_file_get_external_attributes.c", "zip"); AC_DEFINE('HAVE_ZIP', 1); ADD_FLAG("CFLAGS_ZIP", "/D _WIN32"); diff --git a/ext/zip/lib/config.h b/ext/zip/lib/config.h index f9132ba439..ee77c66d43 100644 --- a/ext/zip/lib/config.h +++ b/ext/zip/lib/config.h @@ -21,5 +21,9 @@ #else /* Building in PHP tree */ -#include "php_config.h" +# ifdef _WIN32 +# include "config.w32.h" +# else +# include "php_config.h" +# endif #endif diff --git a/ext/zip/lib/php_zip_config.w32.h b/ext/zip/lib/php_zip_config.w32.h index 8956839c62..1fec26317b 100644 --- a/ext/zip/lib/php_zip_config.w32.h +++ b/ext/zip/lib/php_zip_config.w32.h @@ -54,4 +54,7 @@ typedef long long ssize_t; # endif #endif +# undef strcasecmp +# define strcasecmp _strcmpi + #endif /* HAD_CONFIG_H */ diff --git a/ext/zip/lib/zip.h b/ext/zip/lib/zip.h index 1fb1dbf5e0..2665291abc 100644 --- a/ext/zip/lib/zip.h +++ b/ext/zip/lib/zip.h @@ -20,7 +20,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -35,8 +35,9 @@ */ -#include "main/php.h" +#include "main/php.h" + #ifdef PHP_WIN32 #ifdef PHP_ZIP_EXPORTS # define ZIP_EXTERN __declspec(dllexport) _stdcall @@ -48,23 +49,14 @@ #else # define ZIP_EXTERN #endif + -#ifndef ZIP_EXTERN -#ifdef _WIN32 -#define ZIP_EXTERN __declspec(dllimport) -#elif defined(__GNUC__) && __GNUC__ >= 4 -#define ZIP_EXTERN __attribute__ ((visibility ("default"))) -#else -#define ZIP_EXTERN -#endif +#ifdef __cplusplus +extern "C" { #endif - - -BEGIN_EXTERN_C() - -#include "zipconf.h" +#include #include #include @@ -77,6 +69,7 @@ BEGIN_EXTERN_C() #define ZIP_CHECKCONS 4 #define ZIP_TRUNCATE 8 + /* flags for zip_name_locate, zip_fopen, zip_stat, ... */ #define ZIP_FL_NOCASE 1u /* ignore case on name lookup */ @@ -190,6 +183,29 @@ BEGIN_EXTERN_C() #endif #define ZIP_EM_UNKNOWN 0xffff /* unknown algorithm */ +#define ZIP_OPSYS_DOS 0x00u +#define ZIP_OPSYS_AMIGA 0x01u +#define ZIP_OPSYS_OPENVMS 0x02u +#define ZIP_OPSYS_UNIX 0x03u +#define ZIP_OPSYS_VM_CMS 0x04u +#define ZIP_OPSYS_ATARI_ST 0x05u +#define ZIP_OPSYS_OS_2 0x06u +#define ZIP_OPSYS_MACINTOSH 0x07u +#define ZIP_OPSYS_Z_SYSTEM 0x08u +#define ZIP_OPSYS_CPM 0x09u +#define ZIP_OPSYS_WINDOWS_NTFS 0x0au +#define ZIP_OPSYS_MVS 0x0bu +#define ZIP_OPSYS_VSE 0x0cu +#define ZIP_OPSYS_ACORN_RISC 0x0du +#define ZIP_OPSYS_VFAT 0x0eu +#define ZIP_OPSYS_ALTERNATE_MVS 0x0fu +#define ZIP_OPSYS_BEOS 0x10u +#define ZIP_OPSYS_TANDEM 0x11u +#define ZIP_OPSYS_OS_400 0x12u +#define ZIP_OPSYS_OS_X 0x13u + +#define ZIP_OPSYS_DEFAULT ZIP_OPSYS_UNIX + enum zip_source_cmd { @@ -248,21 +264,32 @@ ZIP_EXTERN int zip_set_file_comment(struct zip *, zip_uint64_t, const char *, in #endif ZIP_EXTERN int zip_archive_set_tempdir(struct zip *, const char *); -ZIP_EXTERN zip_int64_t zip_file_add(struct zip *, const char *, struct zip_source *, zip_flags_t); -ZIP_EXTERN zip_int64_t zip_dir_add(struct zip *, const char *, zip_flags_t); ZIP_EXTERN int zip_close(struct zip *); -ZIP_EXTERN void zip_discard(struct zip *); ZIP_EXTERN int zip_delete(struct zip *, zip_uint64_t); -ZIP_EXTERN int zip_file_extra_field_delete(struct zip *, zip_uint64_t, zip_uint16_t, zip_flags_t); -ZIP_EXTERN int zip_file_extra_field_delete_by_id(struct zip *, zip_uint64_t, zip_uint16_t, zip_uint16_t, zip_flags_t); +ZIP_EXTERN zip_int64_t zip_dir_add(struct zip *, const char *, zip_flags_t); +ZIP_EXTERN void zip_discard(struct zip *); ZIP_EXTERN void zip_error_clear(struct zip *); ZIP_EXTERN void zip_error_get(struct zip *, int *, int *); ZIP_EXTERN int zip_error_get_sys_type(int); ZIP_EXTERN int zip_error_to_str(char *, zip_uint64_t, int, int); ZIP_EXTERN int zip_fclose(struct zip_file *); ZIP_EXTERN struct zip *zip_fdopen(int, int, int *); +ZIP_EXTERN zip_int64_t zip_file_add(struct zip *, const char *, struct zip_source *, zip_flags_t); ZIP_EXTERN void zip_file_error_clear(struct zip_file *); ZIP_EXTERN void zip_file_error_get(struct zip_file *, int *, int *); +ZIP_EXTERN int zip_file_extra_field_delete(struct zip *, zip_uint64_t, zip_uint16_t, zip_flags_t); +ZIP_EXTERN int zip_file_extra_field_delete_by_id(struct zip *, zip_uint64_t, zip_uint16_t, zip_uint16_t, zip_flags_t); +ZIP_EXTERN int zip_file_extra_field_set(struct zip *, zip_uint64_t, zip_uint16_t, zip_uint16_t, const zip_uint8_t *, zip_uint16_t, zip_flags_t); +ZIP_EXTERN zip_int16_t zip_file_extra_fields_count(struct zip *, zip_uint64_t, zip_flags_t); +ZIP_EXTERN zip_int16_t zip_file_extra_fields_count_by_id(struct zip *, zip_uint64_t, zip_uint16_t, zip_flags_t); +ZIP_EXTERN const zip_uint8_t *zip_file_extra_field_get(struct zip *, zip_uint64_t, zip_uint16_t, zip_uint16_t *, zip_uint16_t *, zip_flags_t); +ZIP_EXTERN const zip_uint8_t *zip_file_extra_field_get_by_id(struct zip *, zip_uint64_t, zip_uint16_t, zip_uint16_t, zip_uint16_t *, zip_flags_t); +ZIP_EXTERN const char *zip_file_get_comment(struct zip *, zip_uint64_t, zip_uint32_t *, zip_flags_t); +ZIP_EXTERN int zip_file_get_external_attributes(struct zip *, zip_uint64_t, zip_flags_t, zip_uint8_t *, zip_uint32_t *); +ZIP_EXTERN int zip_file_rename(struct zip *, zip_uint64_t, const char *, zip_flags_t); +ZIP_EXTERN int zip_file_replace(struct zip *, zip_uint64_t, struct zip_source *, zip_flags_t); +ZIP_EXTERN int zip_file_set_comment(struct zip *, zip_uint64_t, const char *, zip_uint16_t, zip_flags_t); +ZIP_EXTERN int zip_file_set_external_attributes(struct zip *, zip_uint64_t, zip_flags_t, zip_uint8_t, zip_uint32_t); ZIP_EXTERN const char *zip_file_strerror(struct zip_file *); ZIP_EXTERN struct zip_file *zip_fopen(struct zip *, const char *, zip_flags_t); ZIP_EXTERN struct zip_file *zip_fopen_encrypted(struct zip *, const char *, zip_flags_t, const char *); @@ -271,23 +298,14 @@ ZIP_EXTERN struct zip_file *zip_fopen_index_encrypted(struct zip *, zip_uint64_t ZIP_EXTERN zip_int64_t zip_fread(struct zip_file *, void *, zip_uint64_t); ZIP_EXTERN const char *zip_get_archive_comment(struct zip *, int *, zip_flags_t); ZIP_EXTERN int zip_get_archive_flag(struct zip *, zip_flags_t, zip_flags_t); -ZIP_EXTERN const char *zip_file_get_comment(struct zip *, zip_uint64_t, zip_uint32_t *, zip_flags_t); -ZIP_EXTERN const zip_uint8_t *zip_file_extra_field_get(struct zip *, zip_uint64_t, zip_uint16_t, zip_uint16_t *, zip_uint16_t *, zip_flags_t); -ZIP_EXTERN const zip_uint8_t *zip_file_extra_field_get_by_id(struct zip *, zip_uint64_t, zip_uint16_t, zip_uint16_t, zip_uint16_t *, zip_flags_t); -ZIP_EXTERN zip_int16_t zip_file_extra_fields_count(struct zip *, zip_uint64_t, zip_flags_t); -ZIP_EXTERN zip_int16_t zip_file_extra_fields_count_by_id(struct zip *, zip_uint64_t, zip_uint16_t, zip_flags_t); ZIP_EXTERN const char *zip_get_name(struct zip *, zip_uint64_t, zip_flags_t); ZIP_EXTERN zip_int64_t zip_get_num_entries(struct zip *, zip_flags_t); ZIP_EXTERN zip_int64_t zip_name_locate(struct zip *, const char *, zip_flags_t); ZIP_EXTERN struct zip *zip_open(const char *, int, int *); -ZIP_EXTERN int zip_file_rename(struct zip *, zip_uint64_t, const char *, zip_flags_t); -ZIP_EXTERN int zip_file_replace(struct zip *, zip_uint64_t, struct zip_source *, zip_flags_t); ZIP_EXTERN int zip_set_archive_comment(struct zip *, const char *, zip_uint16_t); ZIP_EXTERN int zip_set_archive_flag(struct zip *, zip_flags_t, int); ZIP_EXTERN int zip_set_default_password(struct zip *, const char *); -ZIP_EXTERN int zip_file_set_comment(struct zip *, zip_uint64_t, const char *, zip_uint16_t, zip_flags_t); ZIP_EXTERN int zip_set_file_compression(struct zip *, zip_uint64_t, zip_int32_t, zip_uint32_t); -ZIP_EXTERN int zip_file_extra_field_set(struct zip *, zip_uint64_t, zip_uint16_t, zip_uint16_t, const zip_uint8_t *, zip_uint16_t, zip_flags_t); ZIP_EXTERN struct zip_source *zip_source_buffer(struct zip *, const void *, zip_uint64_t, int); ZIP_EXTERN struct zip_source *zip_source_file(struct zip *, const char *, zip_uint64_t, zip_int64_t); ZIP_EXTERN struct zip_source *zip_source_filep(struct zip *, FILE *, zip_uint64_t, zip_int64_t); @@ -302,6 +320,8 @@ ZIP_EXTERN int zip_unchange(struct zip *, zip_uint64_t); ZIP_EXTERN int zip_unchange_all(struct zip *); ZIP_EXTERN int zip_unchange_archive(struct zip *); +#ifdef __cplusplus +} +#endif -END_EXTERN_C(); #endif /* _HAD_ZIP_H */ diff --git a/ext/zip/lib/zip_add.c b/ext/zip/lib/zip_add.c index 4bce3fd4af..5b907824b3 100644 --- a/ext/zip/lib/zip_add.c +++ b/ext/zip/lib/zip_add.c @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE diff --git a/ext/zip/lib/zip_add_entry.c b/ext/zip/lib/zip_add_entry.c index 3a7e2ccbe9..5cbc9574ad 100644 --- a/ext/zip/lib/zip_add_entry.c +++ b/ext/zip/lib/zip_add_entry.c @@ -49,7 +49,8 @@ _zip_add_entry(struct zip *za) if (za->nentry+1 >= za->nentry_alloc) { struct zip_entry *rentries; zip_uint64_t nalloc = za->nentry_alloc + 16; - rentries = (struct zip_entry *)realloc(za->entry, sizeof(struct zip_entry) * nalloc); + /* TODO check for overflow */ + rentries = (struct zip_entry *)realloc(za->entry, sizeof(struct zip_entry) * (size_t)nalloc); if (!rentries) { _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); return -1; diff --git a/ext/zip/lib/zip_close.c b/ext/zip/lib/zip_close.c index c9c7e58088..e8dd9d600d 100644 --- a/ext/zip/lib/zip_close.c +++ b/ext/zip/lib/zip_close.c @@ -1,6 +1,6 @@ /* zip_close.c -- close zip archive and update changes - Copyright (C) 1999-2011 Dieter Baron and Thomas Klausner + Copyright (C) 1999-2013 Dieter Baron and Thomas Klausner This file is part of libzip, a library to manipulate ZIP archives. The authors can be contacted at @@ -17,7 +17,7 @@ 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. - + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -47,7 +47,7 @@ #endif #include #include -#ifdef PHP_WIN32 +#ifdef _WIN32 #include #include #endif @@ -73,7 +73,7 @@ zip_close(struct zip *za) int error; char *temp; FILE *out; -#ifndef PHP_WIN32 +#ifndef _WIN32 mode_t mask; #endif struct zip_filelist *filelist; @@ -110,18 +110,18 @@ zip_close(struct zip *za) return -1; } - if ((filelist=(struct zip_filelist *)malloc(sizeof(filelist[0])*survivors)) == NULL) + if ((filelist=(struct zip_filelist *)malloc(sizeof(filelist[0])*(size_t)survivors)) == NULL) return -1; /* archive comment is special for torrentzip */ if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) { - /* XXX: use internal function when zip_set_archive_comment clears TORRENT flag */ + /* TODO: use internal function when zip_set_archive_comment clears TORRENT flag */ if (zip_set_archive_comment(za, TORRENT_SIG "XXXXXXXX", TORRENT_SIG_LEN + TORRENT_CRC_LEN) < 0) { free(filelist); return -1; } } - /* XXX: if no longer torrentzip and archive comment not changed by user, delete it */ + /* TODO: if no longer torrentzip and archive comment not changed by user, delete it */ /* create list of files with index into original archive */ @@ -153,7 +153,7 @@ zip_close(struct zip *za) if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) - qsort(filelist, survivors, sizeof(filelist[0]), + qsort(filelist, (size_t)survivors, sizeof(filelist[0]), _zip_torrentzip_cmp); new_torrentzip = (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 1 @@ -189,7 +189,7 @@ zip_close(struct zip *za) _zip_dirent_torrent_normalize(entry->changes); - de->offset = (zip_uint64_t)ftello(out); /* XXX: check for errors */ + de->offset = (zip_uint64_t)ftello(out); /* TODO: check for errors */ if (new_data) { struct zip_source *zs; @@ -225,7 +225,7 @@ zip_close(struct zip *za) error = 1; break; } - if ((fseek(za->zp, (off_t)offset, SEEK_SET) < 0)) { + if ((fseeko(za->zp, (off_t)offset, SEEK_SET) < 0)) { _zip_error_set(&za->error, ZIP_ER_SEEK, errno); error = 1; break; @@ -273,7 +273,7 @@ zip_close(struct zip *za) } return -1; } -#ifndef PHP_WIN32 +#ifndef _WIN32 mask = umask(0); umask(mask); chmod(za->zn, 0666&~mask); @@ -367,7 +367,7 @@ add_data(struct zip *za, struct zip_source *src, struct zip_dirent *de, FILE *ft return -1; } - /* XXX: deflate 0-byte files for torrentzip? */ + /* TODO: deflate 0-byte files for torrentzip? */ if (de->comp_method != ZIP_CM_STORE && ((st.valid & ZIP_STAT_SIZE) == 0 || st.size != 0)) { if ((comp_impl=_zip_get_compression_implementation(de->comp_method)) == NULL) { _zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0); @@ -398,7 +398,7 @@ add_data(struct zip *za, struct zip_source *src, struct zip_dirent *de, FILE *ft while (s2 != src) { if ((s2=zip_source_pop(s2)) == NULL) { - /* XXX: set erorr */ + /* TODO: set erorr */ ret = -1; break; } @@ -461,7 +461,7 @@ copy_data(FILE *fs, zip_uint64_t len, FILE *ft, struct zip_error *error) return 0; while (len > 0) { - nn = len > sizeof(buf) ? sizeof(buf) : len; + nn = len > sizeof(buf) ? sizeof(buf) : len > SIZE_MAX ? SIZE_MAX : (size_t)len; if ((n=fread(buf, 1, nn, fs)) == 0) { if (ferror(fs)) { _zip_error_set(error, ZIP_ER_READ, errno); @@ -477,7 +477,7 @@ copy_data(FILE *fs, zip_uint64_t len, FILE *ft, struct zip_error *error) _zip_error_set(error, ZIP_ER_WRITE, errno); return -1; } - + len -= n; } @@ -615,7 +615,7 @@ _zip_create_temp_output(struct zip *za, FILE **outp) free(temp); return NULL; } - + if ((tfp=fdopen(tfd, "r+b")) == NULL) { _zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno); close(tfd); @@ -623,7 +623,8 @@ _zip_create_temp_output(struct zip *za, FILE **outp) free(temp); return NULL; } -#ifdef PHP_WIN32 + +#ifdef _WIN32 /* According to Pierre Joye, Windows in some environments per default creates text files, so force binary mode. diff --git a/ext/zip/lib/zip_dir_add.c b/ext/zip/lib/zip_dir_add.c index 0a74bd6087..1a662f4aaf 100644 --- a/ext/zip/lib/zip_dir_add.c +++ b/ext/zip/lib/zip_dir_add.c @@ -1,6 +1,6 @@ /* zip_dir_add.c -- add directory - Copyright (C) 1999-2012 Dieter Baron and Thomas Klausner + Copyright (C) 1999-2013 Dieter Baron and Thomas Klausner This file is part of libzip, a library to manipulate ZIP archives. The authors can be contacted at @@ -46,7 +46,7 @@ ZIP_EXTERN zip_int64_t zip_dir_add(struct zip *za, const char *name, zip_flags_t flags) { size_t len; - zip_int64_t ret; + zip_int64_t idx; char *s; struct zip_source *source; @@ -78,11 +78,18 @@ zip_dir_add(struct zip *za, const char *name, zip_flags_t flags) return -1; } - ret = _zip_file_replace(za, ZIP_UINT64_MAX, s ? s : name, source, flags); + idx = _zip_file_replace(za, ZIP_UINT64_MAX, s ? s : name, source, flags); free(s); - if (ret < 0) - zip_source_free(source); - return ret; + if (idx < 0) + zip_source_free(source); + else { + if (zip_file_set_external_attributes(za, (zip_uint64_t)idx, 0, ZIP_OPSYS_DEFAULT, ZIP_EXT_ATTRIB_DEFAULT_DIR) < 0) { + zip_delete(za, (zip_uint64_t)idx); + return -1; + } + } + + return idx; } diff --git a/ext/zip/lib/zip_dirent.c b/ext/zip/lib/zip_dirent.c index f4f2deb6fc..5c5792c3e9 100644 --- a/ext/zip/lib/zip_dirent.c +++ b/ext/zip/lib/zip_dirent.c @@ -81,7 +81,7 @@ _zip_cdir_grow(struct zip_cdir *cd, zip_uint64_t nentry, struct zip_error *error return 0; if ((entry=((struct zip_entry *) - realloc(cd->entry, sizeof(*(cd->entry))*nentry))) == NULL) { + realloc(cd->entry, sizeof(*(cd->entry))*(size_t)nentry))) == NULL) { _zip_error_set(error, ZIP_ER_MEMORY, 0); return -1; } @@ -110,7 +110,7 @@ _zip_cdir_new(zip_uint64_t nentry, struct zip_error *error) if (nentry == 0) cd->entry = NULL; - else if ((cd->entry=(struct zip_entry *)malloc(sizeof(*(cd->entry))*nentry)) == NULL) { + else if ((cd->entry=(struct zip_entry *)malloc(sizeof(*(cd->entry))*(size_t)nentry)) == NULL) { _zip_error_set(error, ZIP_ER_MEMORY, 0); free(cd); return NULL; @@ -260,7 +260,7 @@ _zip_dirent_init(struct zip_dirent *de) de->local_extra_fields_read = 0; de->cloned = 0; - de->version_madeby = 20; + de->version_madeby = 20 | (ZIP_OPSYS_DEFAULT << 8); de->version_needed = 20; /* 2.0 */ de->bitflags = 0; de->comp_method = ZIP_CM_DEFAULT; @@ -273,7 +273,7 @@ _zip_dirent_init(struct zip_dirent *de) de->comment = NULL; de->disk_number = 0; de->int_attrib = 0; - de->ext_attrib = 0; + de->ext_attrib = ZIP_EXT_ATTRIB_DEFAULT; de->offset = 0; } @@ -320,7 +320,7 @@ _zip_dirent_new(void) Returns 0 if successful. On error, error is filled in and -1 is returned. - XXX: leftp and file position undefined on error. + TODO: leftp and file position undefined on error. */ int @@ -460,7 +460,7 @@ _zip_dirent_read(struct zip_dirent *zde, FILE *fp, if (zde->uncomp_size == ZIP_UINT32_MAX || zde->comp_size == ZIP_UINT32_MAX || zde->offset == ZIP_UINT32_MAX) { zip_uint16_t got_len, needed_len; const zip_uint8_t *ef = _zip_ef_get_by_id(zde->extra_fields, &got_len, ZIP_EF_ZIP64, 0, local ? ZIP_EF_LOCAL : ZIP_EF_CENTRAL, error); - /* XXX: if got_len == 0 && !ZIP64_EOCD: no error, 0xffffffff is valid value */ + /* TODO: if got_len == 0 && !ZIP64_EOCD: no error, 0xffffffff is valid value */ if (ef == NULL) return -1; @@ -676,8 +676,8 @@ _zip_dirent_write(struct zip_dirent *de, FILE *fp, zip_flags_t flags, struct zip ef_zip64_p = ef_zip64; if (flags & ZIP_FL_LOCAL) { if ((flags & ZIP_FL_FORCE_ZIP64) || de->comp_size > ZIP_UINT32_MAX || de->uncomp_size > ZIP_UINT32_MAX) { - _zip_poke8(de->comp_size, &ef_zip64_p); _zip_poke8(de->uncomp_size, &ef_zip64_p); + _zip_poke8(de->comp_size, &ef_zip64_p); } } else { @@ -707,7 +707,7 @@ _zip_dirent_write(struct zip_dirent *de, FILE *fp, zip_flags_t flags, struct zip _zip_write2(is_really_zip64 ? 45 : de->version_madeby, fp); _zip_write2(is_really_zip64 ? 45 : de->version_needed, fp); _zip_write2(de->bitflags&0xfff9, fp); /* clear compression method specific flags */ - _zip_write2((zip_uint16_t)de->comp_method, fp); /* XXX: can it be ZIP_CM_DEFAULT? */ + _zip_write2((zip_uint16_t)de->comp_method, fp); /* TODO: can it be ZIP_CM_DEFAULT? */ _zip_u2d_time(de->last_mod, &dostime, &dosdate); _zip_write2(dostime, fp); @@ -796,7 +796,7 @@ _zip_ef_utf8(zip_uint16_t id, struct zip_string *str, struct zip_error *error) raw = _zip_string_get(str, &len, ZIP_FL_ENC_RAW, NULL); if (len+5 > ZIP_UINT16_MAX) { - /* XXX: error */ + /* TODO: error */ } if ((data=(zip_uint8_t *)malloc(len+5)) == NULL) { diff --git a/ext/zip/lib/zip_discard.c b/ext/zip/lib/zip_discard.c index 37ba8c2ea6..040b029411 100644 --- a/ext/zip/lib/zip_discard.c +++ b/ext/zip/lib/zip_discard.c @@ -74,6 +74,7 @@ zip_discard(struct zip *za) } } + _zip_error_fini(&za->error); free(za->file); free(za); diff --git a/ext/zip/lib/zip_extra_field.c b/ext/zip/lib/zip_extra_field.c index 41fd2b1d90..8f1ccaa235 100644 --- a/ext/zip/lib/zip_extra_field.c +++ b/ext/zip/lib/zip_extra_field.c @@ -78,7 +78,7 @@ _zip_ef_delete_by_id(struct zip_extra_field *ef, zip_uint16_t id, zip_uint16_t i head = ef; prev = NULL; for (; ef; ef=(prev ? prev->next : head)) { - if ((ef->flags & flags & ZIP_EF_BOTH) && ef->id == id) { + if ((ef->flags & flags & ZIP_EF_BOTH) && ((ef->id == id) || (id == ZIP_EXTRA_FIELD_ALL))) { if (id_idx == ZIP_EXTRA_FIELD_ALL || i == id_idx) { ef->flags &= ~(flags & ZIP_EF_BOTH); if ((ef->flags & ZIP_EF_BOTH) == 0) { diff --git a/ext/zip/lib/zip_extra_field_api.c b/ext/zip/lib/zip_extra_field_api.c index 02ed4555e0..3e936d2664 100644 --- a/ext/zip/lib/zip_extra_field_api.c +++ b/ext/zip/lib/zip_extra_field_api.c @@ -46,6 +46,11 @@ zip_file_extra_field_delete(struct zip *za, zip_uint64_t idx, zip_uint16_t ef_id _zip_error_set(&za->error, ZIP_ER_INVAL, 0); return -1; } + + if (((flags & ZIP_EF_BOTH) == ZIP_EF_BOTH) && (ef_idx != ZIP_EXTRA_FIELD_ALL)) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; + } if (_zip_get_dirent(za, idx, 0, NULL) == NULL) return -1; @@ -75,6 +80,11 @@ zip_file_extra_field_delete_by_id(struct zip *za, zip_uint64_t idx, zip_uint16_t _zip_error_set(&za->error, ZIP_ER_INVAL, 0); return -1; } + + if (((flags & ZIP_EF_BOTH) == ZIP_EF_BOTH) && (ef_idx != ZIP_EXTRA_FIELD_ALL)) { + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); + return -1; + } if (_zip_get_dirent(za, idx, 0, NULL) == NULL) return -1; diff --git a/ext/zip/lib/zip_fclose.c b/ext/zip/lib/zip_fclose.c index 093c30eada..c05ec3dd0f 100644 --- a/ext/zip/lib/zip_fclose.c +++ b/ext/zip/lib/zip_fclose.c @@ -62,6 +62,7 @@ zip_fclose(struct zip_file *zf) if (zf->error.zip_err) ret = zf->error.zip_err; + _zip_error_fini(&zf->error); free(zf); return ret; } diff --git a/ext/zip/lib/zip_file_get_offset.c b/ext/zip/lib/zip_file_get_offset.c index 65a011fc07..e8c199fb5b 100644 --- a/ext/zip/lib/zip_file_get_offset.c +++ b/ext/zip/lib/zip_file_get_offset.c @@ -63,7 +63,7 @@ _zip_file_get_offset(const struct zip *za, zip_uint64_t idx, struct zip_error *e return 0; } - /* XXX: cache? */ + /* TODO: cache? */ if ((size=_zip_dirent_size(za->zp, ZIP_EF_LOCAL, error)) < 0) return 0; diff --git a/ext/zip/lib/zip_get_archive_flag.c b/ext/zip/lib/zip_get_archive_flag.c index c3be5c14fe..7be5a3f2be 100644 --- a/ext/zip/lib/zip_get_archive_flag.c +++ b/ext/zip/lib/zip_get_archive_flag.c @@ -38,7 +38,7 @@ ZIP_EXTERN int -zip_get_archive_flag(struct zip *za, unsigned int flag, zip_flags_t flags) +zip_get_archive_flag(struct zip *za, zip_flags_t flag, zip_flags_t flags) { unsigned int fl; diff --git a/ext/zip/lib/zip_get_num_files.c b/ext/zip/lib/zip_get_num_files.c index 29b06dc819..d75941d5e1 100644 --- a/ext/zip/lib/zip_get_num_files.c +++ b/ext/zip/lib/zip_get_num_files.c @@ -44,6 +44,6 @@ zip_get_num_files(struct zip *za) if (za == NULL) return -1; - /* XXX: check for overflow */ + /* TODO: check for overflow */ return (int)za->nentry; } diff --git a/ext/zip/lib/zip_open.c b/ext/zip/lib/zip_open.c index e6a30d5ae7..d91fe469db 100644 --- a/ext/zip/lib/zip_open.c +++ b/ext/zip/lib/zip_open.c @@ -364,7 +364,7 @@ _zip_checkcons(FILE *fp, struct zip_cdir *cd, struct zip_error *error) /* _zip_check_torrentzip: - check wether ZA has a valid TORRENTZIP comment, i.e. is torrentzipped */ + check whether ZA has a valid TORRENTZIP comment, i.e. is torrentzipped */ static void _zip_check_torrentzip(struct zip *za, const struct zip_cdir *cdir) @@ -498,6 +498,11 @@ _zip_find_central_dir(FILE *fp, unsigned int flags, int *zep, off_t len) zip_int64_t best; struct zip_error zerr; + if (len < (off_t)EOCDLEN) { + set_error(zep, NULL, ZIP_ER_NOZIP); + return NULL; + } + i = fseeko(fp, -(len < CDBUFSIZE ? len : CDBUFSIZE), SEEK_END); if (i == -1 && errno != EFBIG) { /* seek before start of file on my machine */ diff --git a/ext/zip/lib/zip_set_archive_flag.c b/ext/zip/lib/zip_set_archive_flag.c index b6cdab120d..68f99b8568 100644 --- a/ext/zip/lib/zip_set_archive_flag.c +++ b/ext/zip/lib/zip_set_archive_flag.c @@ -38,7 +38,7 @@ ZIP_EXTERN int -zip_set_archive_flag(struct zip *za, unsigned int flag, int value) +zip_set_archive_flag(struct zip *za, zip_flags_t flag, int value) { unsigned int new_flags; diff --git a/ext/zip/lib/zip_set_file_compression.c b/ext/zip/lib/zip_set_file_compression.c index ea8517d70e..647f69a68f 100644 --- a/ext/zip/lib/zip_set_file_compression.c +++ b/ext/zip/lib/zip_set_file_compression.c @@ -63,7 +63,7 @@ zip_set_file_compression(struct zip *za, zip_uint64_t idx, old_method = (e->orig == NULL ? ZIP_CM_DEFAULT : e->orig->comp_method); - /* XXX: revisit this when flags are supported, since they may require a recompression */ + /* TODO: revisit this when flags are supported, since they may require a recompression */ if (method == old_method) { if (e->changes) { diff --git a/ext/zip/lib/zip_set_name.c b/ext/zip/lib/zip_set_name.c index 60c9e7d5bf..02fa1272d6 100644 --- a/ext/zip/lib/zip_set_name.c +++ b/ext/zip/lib/zip_set_name.c @@ -59,7 +59,7 @@ _zip_set_name(struct zip *za, zip_uint64_t idx, const char *name, zip_flags_t fl } if (name && strlen(name) > 0) { - /* XXX: check for string too long */ + /* TODO: check for string too long */ if ((str=_zip_string_new((const zip_uint8_t *)name, (zip_uint16_t)strlen(name), flags, &za->error)) == NULL) return -1; if ((flags & ZIP_FL_ENCODING_ALL) == ZIP_FL_ENC_GUESS && _zip_guess_encoding(str, ZIP_ENCODING_UNKNOWN) == ZIP_ENCODING_UTF8_GUESSED) @@ -68,7 +68,7 @@ _zip_set_name(struct zip *za, zip_uint64_t idx, const char *name, zip_flags_t fl else str = NULL; - /* XXX: encoding flags needed for CP437? */ + /* TODO: encoding flags needed for CP437? */ if ((i=_zip_name_locate(za, name, 0, NULL)) >= 0 && (zip_uint64_t)i != idx) { _zip_string_free(str); _zip_error_set(&za->error, ZIP_ER_EXISTS, 0); diff --git a/ext/zip/lib/zip_source_crc.c b/ext/zip/lib/zip_source_crc.c index 99bc965228..7d6df6a385 100644 --- a/ext/zip/lib/zip_source_crc.c +++ b/ext/zip/lib/zip_source_crc.c @@ -125,7 +125,7 @@ crc_read(struct zip_source *src, void *_ctx, void *data, } else { ctx->size += (zip_uint64_t)n; - ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data, (uInt)n); /* XXX: check for overflow, use multiple crc calls if needed */ + ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data, (uInt)n); /* TODO: check for overflow, use multiple crc calls if needed */ } return n; @@ -139,7 +139,7 @@ crc_read(struct zip_source *src, void *_ctx, void *data, st = (struct zip_stat *)data; if (ctx->eof) { - /* XXX: Set comp_size, comp_method, encryption_method? + /* TODO: Set comp_size, comp_method, encryption_method? After all, this only works for uncompressed data. */ st->size = ctx->size; st->crc = ctx->crc; diff --git a/ext/zip/lib/zip_source_deflate.c b/ext/zip/lib/zip_source_deflate.c index 879953144c..57444d9677 100644 --- a/ext/zip/lib/zip_source_deflate.c +++ b/ext/zip/lib/zip_source_deflate.c @@ -113,7 +113,7 @@ compress_read(struct zip_source *src, struct deflate *ctx, return 0; ctx->zstr.next_out = (Bytef *)data; - ctx->zstr.avail_out = (uInt)len; /* XXX: check for overflow */ + ctx->zstr.avail_out = (uInt)len; /* TODO: check for overflow */ end = 0; while (!end) { @@ -144,7 +144,7 @@ compress_read(struct zip_source *src, struct deflate *ctx, else if (n == 0) { ctx->eof = 1; ctx->size = ctx->zstr.total_in; - /* XXX: check against stat of src? */ + /* TODO: check against stat of src? */ } else { ctx->zstr.next_in = (Bytef *)ctx->buffer; @@ -187,7 +187,7 @@ decompress_read(struct zip_source *src, struct deflate *ctx, return 0; ctx->zstr.next_out = (Bytef *)data; - ctx->zstr.avail_out = (uInt)len; /* XXX: check for overflow */ + ctx->zstr.avail_out = (uInt)len; /* TODO: check for overflow */ end = 0; while (!end && ctx->zstr.avail_out) { @@ -332,7 +332,7 @@ deflate_decompress(struct zip_source *src, void *ud, void *data, ctx->zstr.zfree = Z_NULL; ctx->zstr.opaque = NULL; ctx->zstr.next_in = (Bytef *)ctx->buffer; - ctx->zstr.avail_in = (uInt)n /* XXX: check for overflow */; + ctx->zstr.avail_in = (uInt)n /* TODO: check for overflow */; /* negative value to tell zlib that there is no header */ if ((ret=inflateInit2(&ctx->zstr, -MAX_WBITS)) != Z_OK) { @@ -370,7 +370,7 @@ deflate_decompress(struct zip_source *src, void *ud, void *data, return sizeof(int)*2; case ZIP_SOURCE_FREE: - /* XXX: inflateEnd if close was not called */ + /* TODO: inflateEnd if close was not called */ free(ctx); return 0; @@ -387,6 +387,6 @@ deflate_decompress(struct zip_source *src, void *ud, void *data, static void deflate_free(struct deflate *ctx) { - /* XXX: deflateEnd if close was not called */ + /* TODO: deflateEnd if close was not called */ free(ctx); } diff --git a/ext/zip/lib/zip_source_filep.c b/ext/zip/lib/zip_source_filep.c index 0bd2d6846e..fcebe73bc6 100644 --- a/ext/zip/lib/zip_source_filep.c +++ b/ext/zip/lib/zip_source_filep.c @@ -125,7 +125,8 @@ read_file(void *state, void *data, zip_uint64_t len, enum zip_source_cmd cmd) { struct read_file *z; char *buf; - size_t i, n; + zip_uint64_t n; + size_t i; z = (struct read_file *)state; buf = (char *)data; @@ -151,11 +152,13 @@ read_file(void *state, void *data, zip_uint64_t len, enum zip_source_cmd cmd) return 0; case ZIP_SOURCE_READ: - /* XXX: return INVAL if len > size_t max */ if (z->remain != -1) n = len > (zip_uint64_t)z->remain ? (zip_uint64_t)z->remain : len; else n = len; + + if (n > SIZE_MAX) + n = SIZE_MAX; if (!z->closep) { /* we might share this file with others, so let's be safe */ @@ -166,7 +169,7 @@ read_file(void *state, void *data, zip_uint64_t len, enum zip_source_cmd cmd) } } - if ((i=fread(buf, 1, n, z->f)) == 0) { + if ((i=fread(buf, 1, (size_t)n, z->f)) == 0) { if (ferror(z->f)) { z->e[0] = ZIP_ER_READ; z->e[1] = errno; diff --git a/ext/zip/lib/zip_source_pkware.c b/ext/zip/lib/zip_source_pkware.c index ec53dfeef0..f6cbf1d2d5 100644 --- a/ext/zip/lib/zip_source_pkware.c +++ b/ext/zip/lib/zip_source_pkware.c @@ -204,7 +204,7 @@ pkware_decrypt(struct zip_source *src, void *ud, void *data, st->encryption_method = ZIP_EM_NONE; st->valid |= ZIP_STAT_ENCRYPTION_METHOD; - /* XXX: deduce HEADERLEN from size for uncompressed */ + /* TODO: deduce HEADERLEN from size for uncompressed */ if (st->valid & ZIP_STAT_COMP_SIZE) st->comp_size -= HEADERLEN; } diff --git a/ext/zip/lib/zip_source_zip_new.c b/ext/zip/lib/zip_source_zip_new.c index 8e48f6f3fa..353489a653 100644 --- a/ext/zip/lib/zip_source_zip_new.c +++ b/ext/zip/lib/zip_source_zip_new.c @@ -121,12 +121,12 @@ _zip_source_zip_new(struct zip *za, struct zip *srcza, zip_uint64_t srcidx, zip_ st2.mtime = st.mtime; st2.valid = ZIP_STAT_SIZE|ZIP_STAT_COMP_SIZE|ZIP_STAT_COMP_METHOD|ZIP_STAT_MTIME; - /* XXX: check for overflow of st2.size */ + /* TODO: check for overflow of st2.size */ if ((src=_zip_source_file_or_p(za, NULL, srcza->zp, offset+start, (zip_int64_t)st2.size, 0, &st2)) == NULL) return NULL; } else { - /* XXX: check for overflow of st.comp_size */ + /* TODO: check for overflow of st.comp_size */ if ((src=_zip_source_file_or_p(za, NULL, srcza->zp, offset, (zip_int64_t)st.comp_size, 0, &st)) == NULL) return NULL; } @@ -134,7 +134,7 @@ _zip_source_zip_new(struct zip *za, struct zip *srcza, zip_uint64_t srcidx, zip_ if (enc_impl) { if ((s2=enc_impl(za, src, st.encryption_method, 0, password)) == NULL) { zip_source_free(src); - /* XXX: set error (how?) */ + /* TODO: set error (how?) */ return NULL; } src = s2; @@ -142,7 +142,7 @@ _zip_source_zip_new(struct zip *za, struct zip *srcza, zip_uint64_t srcidx, zip_ if (comp_impl) { if ((s2=comp_impl(za, src, st.comp_method, 0)) == NULL) { zip_source_free(src); - /* XXX: set error (how?) */ + /* TODO: set error (how?) */ return NULL; } src = s2; @@ -152,7 +152,7 @@ _zip_source_zip_new(struct zip *za, struct zip *srcza, zip_uint64_t srcidx, zip_ /* when reading the whole file, check for crc errors */ if ((s2=zip_source_crc(za, src, 1)) == NULL) { zip_source_free(src); - /* XXX: set error (how?) */ + /* TODO: set error (how?) */ return NULL; } src = s2; @@ -161,7 +161,7 @@ _zip_source_zip_new(struct zip *za, struct zip *srcza, zip_uint64_t srcidx, zip_ if (start+len > 0 && (comp_impl || enc_impl)) { if ((s2=zip_source_window(za, src, start, len ? len : st.size-start)) == NULL) { zip_source_free(src); - /* XXX: set error (how?) (why?) */ + /* TODO: set error (how?) (why?) */ return NULL; } src = s2; diff --git a/ext/zip/lib/zip_stat_index.c b/ext/zip/lib/zip_stat_index.c index f4ce72aaa8..2505c80138 100644 --- a/ext/zip/lib/zip_stat_index.c +++ b/ext/zip/lib/zip_stat_index.c @@ -68,7 +68,7 @@ zip_stat_index(struct zip *za, zip_uint64_t index, zip_flags_t flags, st->comp_method = (zip_uint16_t)de->comp_method; if (de->bitflags & ZIP_GPBF_ENCRYPTED) { if (de->bitflags & ZIP_GPBF_STRONG_ENCRYPTION) { - /* XXX */ + /* TODO */ st->encryption_method = ZIP_EM_UNKNOWN; } else diff --git a/ext/zip/lib/zip_string.c b/ext/zip/lib/zip_string.c index a2d5eb5d3f..4e8709b5f7 100644 --- a/ext/zip/lib/zip_string.c +++ b/ext/zip/lib/zip_string.c @@ -64,7 +64,7 @@ _zip_string_equal(const struct zip_string *a, const struct zip_string *b) if (a->length != b->length) return 0; - /* XXX: encoding */ + /* TODO: encoding */ return (memcmp(a->raw, b->raw, a->length) == 0); } diff --git a/ext/zip/lib/zipint.h b/ext/zip/lib/zipint.h index e8765c5b66..372923d418 100644 --- a/ext/zip/lib/zipint.h +++ b/ext/zip/lib/zipint.h @@ -56,19 +56,14 @@ # include "config.h" #endif -#if defined(HAVE_MOVEFILEEXA) && defined(_WIN32) +#ifdef HAVE_MOVEFILEEXA #include -#define _zip_rename(s, t) \ - (!MoveFileExA((s), (t), MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING)) +#define _zip_rename(s, t) (!MoveFileExA((s), (t), MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING)) #else #define _zip_rename rename #endif #ifdef _WIN32 -#undef strcasecmp -# define strcasecmp _strcmpi -#endif - #if defined(HAVE__CLOSE) #define close _close #endif @@ -86,12 +81,13 @@ #if defined(HAVE__OPEN) #define open(a, b, c) _open((a), (b)) #endif -#if defined(HAVE__SNPRINTF) +#if defined(HAVE__SNPRINTF) && !defined(PHP_WIN32) #define snprintf _snprintf #endif -#if defined(HAVE__STRDUP) && !defined(strdup) +#if defined(HAVE__STRDUP) && !defined(HAVE_STRDUP) #define strdup _strdup #endif +#endif #ifndef HAVE_FSEEKO #define fseeko(s, o, w) (fseek((s), (long int)(o), (w))) @@ -122,6 +118,18 @@ int _zip_mkstemp(char *); #error unsupported size of off_t #endif +#ifndef SIZE_MAX +#if SIZEOF_SIZE_T == 8 +#define SIZE_MAX ZIP_INT64_MAX +#elif SIZEOF_SIZE_T == 4 +#define SIZE_MAX ZIP_INT32_MAX +#elif SIZEOF_SIZE_T == 2 +#define SIZE_MAX ZIP_INT16_MAX +#else +#error unsupported size of size_t +#endif +#endif + #define CENTRAL_MAGIC "PK\1\2" #define LOCAL_MAGIC "PK\3\4" #define EOCD_MAGIC "PK\5\6" @@ -151,7 +159,12 @@ int _zip_mkstemp(char *); #define ZIP_EF_ZIP64 0x0001 #define ZIP_EF_IS_INTERNAL(id) ((id) == ZIP_EF_UTF_8_COMMENT || (id) == ZIP_EF_UTF_8_NAME || (id) == ZIP_EF_ZIP64) - + +/* according to unzip-6.0's zipinfo.c, this corresponds to a regular file with rw permissions for everyone */ +#define ZIP_EXT_ATTRIB_DEFAULT (0100666<<16) +/* according to unzip-6.0's zipinfo.c, this corresponds to a directory with rwx permissions for everyone */ +#define ZIP_EXT_ATTRIB_DEFAULT_DIR (0040777<<16) + /* This section contains API that won't materialize like this. It's placed in the internal section, pending cleanup. */ @@ -296,12 +309,13 @@ struct zip_file { #define ZIP_DIRENT_FILENAME 0x0002u #define ZIP_DIRENT_COMMENT 0x0004u #define ZIP_DIRENT_EXTRA_FIELD 0x0008u +#define ZIP_DIRENT_ATTRIBUTES 0x0010u #define ZIP_DIRENT_ALL 0xffffu struct zip_dirent { zip_uint32_t changed; int local_extra_fields_read; /* whether we already read in local header extra fields */ - int cloned; /* wether this instance is cloned, and thus shares non-changed strings */ + int cloned; /* whether this instance is cloned, and thus shares non-changed strings */ zip_uint16_t version_madeby; /* (c) version of creator */ zip_uint16_t version_needed; /* (cl) version needed to extract */ diff --git a/ext/zip/php_zip.c b/ext/zip/php_zip.c index 5e7d99b755..ccfbb73a29 100644 --- a/ext/zip/php_zip.c +++ b/ext/zip/php_zip.c @@ -336,7 +336,7 @@ static int php_zip_add_file(struct zip *za, const char *filename, size_t filenam if (!zs) { return -1; } - if (zip_file_add(za, entry_name, zs, ZIP_FL_OVERWRITE) < 0) { + if (zip_file_add(za, entry_name, zs, ZIP_FL_OVERWRITE) < 0) { zip_source_free(zs); return -1; } else { @@ -1541,6 +1541,7 @@ static ZIPARCHIVE_METHOD(open) /* we already have an opened zip, free it */ if (zip_close(ze_obj->za) != 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty string as source"); + efree(resolved_path); RETURN_FALSE; } ze_obj->za = NULL; @@ -1552,6 +1553,7 @@ static ZIPARCHIVE_METHOD(open) intern = zip_open(resolved_path, flags, &err); if (!intern || err) { + efree(resolved_path); RETURN_LONG((long)err); } ze_obj->filename = resolved_path; @@ -2156,6 +2158,156 @@ static ZIPARCHIVE_METHOD(setCommentIndex) } /* }}} */ +/* those constants/functions are only available in libzip since 0.11.2 */ +#ifdef ZIP_OPSYS_DEFAULT + +/* {{{ proto bool ZipArchive::setExternalAttributesName(string name, int opsys, int attr [, int flags]) +Set external attributes for file in zip, using its name */ +static ZIPARCHIVE_METHOD(setExternalAttributesName) +{ + struct zip *intern; + zval *this = getThis(); + int name_len; + char *name; + long flags=0, opsys, attr; + zip_int64_t idx; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll|l", + &name, &name_len, &opsys, &attr, &flags) == FAILURE) { + return; + } + + if (name_len < 1) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string as entry name"); + } + + idx = zip_name_locate(intern, name, 0); + if (idx < 0) { + RETURN_FALSE; + } + if (zip_file_set_external_attributes(intern, idx, (zip_flags_t)flags, + (zip_uint8_t)(opsys&0xff), (zip_uint32_t)attr) < 0) { + RETURN_FALSE; + } + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto bool ZipArchive::setExternalAttributesIndex(int index, int opsys, int attr [, int flags]) +Set external attributes for file in zip, using its index */ +static ZIPARCHIVE_METHOD(setExternalAttributesIndex) +{ + struct zip *intern; + zval *this = getThis(); + long index, flags=0, opsys, attr; + struct zip_stat sb; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll|l", + &index, &opsys, &attr, &flags) == FAILURE) { + return; + } + + PHP_ZIP_STAT_INDEX(intern, index, 0, sb); + if (zip_file_set_external_attributes(intern, (zip_uint64_t)index, + (zip_flags_t)flags, (zip_uint8_t)(opsys&0xff), (zip_uint32_t)attr) < 0) { + RETURN_FALSE; + } + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto bool ZipArchive::getExternalAttributesName(string name, int &opsys, int &attr [, int flags]) +Get external attributes for file in zip, using its name */ +static ZIPARCHIVE_METHOD(getExternalAttributesName) +{ + struct zip *intern; + zval *this = getThis(), *z_opsys, *z_attr; + int name_len; + char *name; + long flags=0; + zip_uint8_t opsys; + zip_uint32_t attr; + zip_int64_t idx; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szz|l", + &name, &name_len, &z_opsys, &z_attr, &flags) == FAILURE) { + return; + } + + if (name_len < 1) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string as entry name"); + } + + idx = zip_name_locate(intern, name, 0); + if (idx < 0) { + RETURN_FALSE; + } + if (zip_file_get_external_attributes(intern, idx, + (zip_flags_t)flags, &opsys, &attr) < 0) { + RETURN_FALSE; + } + zval_dtor(z_opsys); + ZVAL_LONG(z_opsys, opsys); + zval_dtor(z_attr); + ZVAL_LONG(z_attr, attr); + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto bool ZipArchive::getExternalAttributesIndex(int index, int &opsys, int &attr [, int flags]) +Get external attributes for file in zip, using its index */ +static ZIPARCHIVE_METHOD(getExternalAttributesIndex) +{ + struct zip *intern; + zval *this = getThis(), *z_opsys, *z_attr; + long index, flags=0; + zip_uint8_t opsys; + zip_uint32_t attr; + struct zip_stat sb; + + if (!this) { + RETURN_FALSE; + } + + ZIP_FROM_OBJECT(intern, this); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lzz|l", + &index, &z_opsys, &z_attr, &flags) == FAILURE) { + return; + } + + PHP_ZIP_STAT_INDEX(intern, index, 0, sb); + if (zip_file_get_external_attributes(intern, (zip_uint64_t)index, + (zip_flags_t)flags, &opsys, &attr) < 0) { + RETURN_FALSE; + } + zval_dtor(z_opsys); + ZVAL_LONG(z_opsys, opsys); + zval_dtor(z_attr); + ZVAL_LONG(z_attr, attr); + RETURN_TRUE; +} +/* }}} */ +#endif /* ifdef ZIP_OPSYS_DEFAULT */ + /* {{{ proto string ZipArchive::getCommentName(string name[, int flags]) Returns the comment of an entry using its name */ static ZIPARCHIVE_METHOD(getCommentName) @@ -2784,6 +2936,36 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_ziparchive_getstream, 0, 0, 1) ZEND_ARG_INFO(0, entryname) ZEND_END_ARG_INFO() + +#ifdef ZIP_OPSYS_DEFAULT +ZEND_BEGIN_ARG_INFO_EX(arginfo_ziparchive_setextattrname, 0, 0, 3) + ZEND_ARG_INFO(0, name) + ZEND_ARG_INFO(0, opsys) + ZEND_ARG_INFO(0, attr) + ZEND_ARG_INFO(0, flags) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_ziparchive_setextattrindex, 0, 0, 3) + ZEND_ARG_INFO(0, index) + ZEND_ARG_INFO(0, opsys) + ZEND_ARG_INFO(0, attr) + ZEND_ARG_INFO(0, flags) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_ziparchive_getextattrname, 0, 0, 3) + ZEND_ARG_INFO(0, name) + ZEND_ARG_INFO(1, opsys) + ZEND_ARG_INFO(1, attr) + ZEND_ARG_INFO(0, flags) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_ziparchive_getextattrindex, 0, 0, 3) + ZEND_ARG_INFO(0, index) + ZEND_ARG_INFO(1, opsys) + ZEND_ARG_INFO(1, attr) + ZEND_ARG_INFO(0, flags) +ZEND_END_ARG_INFO() +#endif /* ifdef ZIP_OPSYS_DEFAULT */ /* }}} */ /* {{{ ze_zip_object_class_functions */ @@ -2819,6 +3001,10 @@ static const zend_function_entry zip_class_functions[] = { ZIPARCHIVE_ME(getFromName, arginfo_ziparchive_getfromname, ZEND_ACC_PUBLIC) ZIPARCHIVE_ME(getFromIndex, arginfo_ziparchive_getfromindex, ZEND_ACC_PUBLIC) ZIPARCHIVE_ME(getStream, arginfo_ziparchive_getstream, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(setExternalAttributesName, arginfo_ziparchive_setextattrname, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(setExternalAttributesIndex, arginfo_ziparchive_setextattrindex, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(getExternalAttributesName, arginfo_ziparchive_getextattrname, ZEND_ACC_PUBLIC) + ZIPARCHIVE_ME(getExternalAttributesIndex, arginfo_ziparchive_getextattrindex, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */ @@ -2902,8 +3088,32 @@ static PHP_MINIT_FUNCTION(zip) REGISTER_ZIP_CLASS_CONST_LONG("ER_REMOVE", ZIP_ER_REMOVE); /* S Can't remove file */ REGISTER_ZIP_CLASS_CONST_LONG("ER_DELETED", ZIP_ER_DELETED); /* N Entry has been deleted */ +#ifdef ZIP_OPSYS_DEFAULT + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_DOS", ZIP_OPSYS_DOS); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_AMIGA", ZIP_OPSYS_AMIGA); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_OPENVMS", ZIP_OPSYS_OPENVMS); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_UNIX", ZIP_OPSYS_UNIX); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_VM_CMS", ZIP_OPSYS_VM_CMS); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_ATARI_ST", ZIP_OPSYS_ATARI_ST); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_OS_2", ZIP_OPSYS_OS_2); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_MACINTOSH", ZIP_OPSYS_MACINTOSH); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_Z_SYSTEM", ZIP_OPSYS_Z_SYSTEM); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_WINDOWS_NTFS", ZIP_OPSYS_WINDOWS_NTFS); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_MVS", ZIP_OPSYS_MVS); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_VSE", ZIP_OPSYS_VSE); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_ACORN_RISC", ZIP_OPSYS_ACORN_RISC); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_VFAT", ZIP_OPSYS_VFAT); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_ALTERNATE_MVS", ZIP_OPSYS_ALTERNATE_MVS); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_BEOS", ZIP_OPSYS_BEOS); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_TANDEM", ZIP_OPSYS_TANDEM); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_OS_400", ZIP_OPSYS_OS_400); + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_OS_X", ZIP_OPSYS_OS_X); + + REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_DEFAULT", ZIP_OPSYS_DEFAULT); +#endif /* ifdef ZIP_OPSYS_DEFAULT */ + php_register_url_stream_wrapper("zip", &php_stream_zip_wrapper TSRMLS_CC); -#endif +#endif /* ifdef PHP_ZIP_USE_OO */ le_zip_dir = zend_register_list_destructors_ex(php_zip_free_dir, NULL, le_zip_dir_name, module_number); le_zip_entry = zend_register_list_destructors_ex(php_zip_free_entry, NULL, le_zip_entry_name, module_number); diff --git a/ext/zip/php_zip.h b/ext/zip/php_zip.h index c8da941b7a..8cceff7136 100644 --- a/ext/zip/php_zip.h +++ b/ext/zip/php_zip.h @@ -38,7 +38,7 @@ extern zend_module_entry zip_module_entry; #define ZIP_OVERWRITE ZIP_TRUNCATE #endif -#define PHP_ZIP_VERSION "1.12.3" +#define PHP_ZIP_VERSION "1.12.4-dev" #if ((PHP_MAJOR_VERSION >= 5 && PHP_MINOR_VERSION >= 2) || PHP_MAJOR_VERSION >= 6) # define PHP_ZIP_USE_OO 1 From 41cdb132c846ad412e0b414185a0ad9b6fc176e2 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Mon, 30 Dec 2013 07:45:09 +0100 Subject: [PATCH 7/8] NEWS --- NEWS | 7 ++++++- UPGRADING | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 384faecfba..a16ae7ba6f 100644 --- a/NEWS +++ b/NEWS @@ -95,9 +95,14 @@ PHP NEWS (Mike) - Zip: - . update libzip to version 1.11.1. + . update libzip to version 1.11.2. PHP don't use any ilibzip private symbol anymore. (Pierre, Remi) . new method ZipArchive::setPassword($password). (Pierre) . add --with-libzip option to build with system libzip. (Remi) + . new methods: + ZipArchive::setExternalAttributesName($name, $opsys, $attr [, $flags]) + ZipArchive::setExternalAttributesIndex($idx, $opsys, $attr [, $flags]) + ZipArchive::getExternalAttributesName($name, &$opsys, &$attr [, $flags]) + ZipArchive::getExternalAttributesIndex($idx, &$opsys, &$attr [, $flags]) <<< NOTE: Insert NEWS from last stable release here prior to actual release! >>> diff --git a/UPGRADING b/UPGRADING index 660d3b9bd2..f2be0a3ea8 100755 --- a/UPGRADING +++ b/UPGRADING @@ -162,4 +162,5 @@ PHP X.Y UPGRADE NOTES overridden by setting a Connection header via the header context option. - Zip: - New --with-libzip option allow to use system libzip. Version > 0.11 required. + New --with-libzip option allow to use system libzip. Version > 0.11 required, + Version >= 0.11.2 recommended for all features. From 3a492cf6869ebc2ee330662b7e11cedf53566769 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Mon, 30 Dec 2013 08:47:16 +0100 Subject: [PATCH 8/8] fix compat for PHP ABI changes --- ext/zip/php_zip.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/ext/zip/php_zip.c b/ext/zip/php_zip.c index ccfbb73a29..d2d36965f6 100644 --- a/ext/zip/php_zip.c +++ b/ext/zip/php_zip.c @@ -112,6 +112,7 @@ static int le_zip_entry; # define add_ascii_assoc_long add_assoc_long #endif + /* Flatten a path by making a relative path (to .)*/ static char * php_zip_make_relative_path(char *path, int path_len) /* {{{ */ { @@ -150,6 +151,15 @@ static char * php_zip_make_relative_path(char *path, int path_len) /* {{{ */ /* }}} */ #ifdef PHP_ZIP_USE_OO + +#if PHP_VERSION_ID < 50600 +# define CWD_STATE_ALLOC(l) malloc(l) +# define CWD_STATE_FREE(s) free(s) +#else +# define CWD_STATE_ALLOC(l) emalloc(l) +# define CWD_STATE_FREE(s) efree(s) +#endif + /* {{{ php_zip_extract_file */ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int file_len TSRMLS_DC) { @@ -170,7 +180,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil size_t path_cleaned_len; cwd_state new_state; - new_state.cwd = (char*)emalloc(1); + new_state.cwd = CWD_STATE_ALLOC(1); new_state.cwd[0] = '\0'; new_state.cwd_length = 0; @@ -207,7 +217,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil if (ZIP_OPENBASEDIR_CHECKPATH(file_dirname_fullpath)) { efree(file_dirname_fullpath); efree(file_basename); - efree(new_state.cwd); + CWD_STATE_FREE(new_state.cwd); return 0; } } @@ -231,7 +241,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil efree(file_dirname_fullpath); if (!is_dir_only) { efree(file_basename); - efree(new_state.cwd); + CWD_STATE_FREE(new_state.cwd); } return 0; } @@ -240,7 +250,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil /* it is a standalone directory, job done */ if (is_dir_only) { efree(file_dirname_fullpath); - efree(new_state.cwd); + CWD_STATE_FREE(new_state.cwd); return 1; } @@ -248,13 +258,13 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil if (!len) { efree(file_dirname_fullpath); efree(file_basename); - efree(new_state.cwd); + CWD_STATE_FREE(new_state.cwd); return 0; } else if (len > MAXPATHLEN) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Full extraction path exceed MAXPATHLEN (%i)", MAXPATHLEN); efree(file_dirname_fullpath); efree(file_basename); - efree(new_state.cwd); + CWD_STATE_FREE(new_state.cwd); return 0; } @@ -266,7 +276,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil efree(fullpath); efree(file_dirname_fullpath); efree(file_basename); - efree(new_state.cwd); + CWD_STATE_FREE(new_state.cwd); return 0; } @@ -301,7 +311,7 @@ done: efree(fullpath); efree(file_basename); efree(file_dirname_fullpath); - efree(new_state.cwd); + CWD_STATE_FREE(new_state.cwd); if (n<0) { return 0; @@ -1010,7 +1020,11 @@ static int php_zip_has_property(zval *object, zval *member, int type KEY_ARG_DC Z_SET_REFCOUNT_P(tmp, 1); Z_UNSET_ISREF_P(tmp); if (type == 1) { +#if PHP_VERSION_ID >= 50699 + retval = zend_is_true(tmp TSRMLS_CC); +#else retval = zend_is_true(tmp); +#endif } else if (type == 0) { retval = (Z_TYPE_P(tmp) != IS_NULL); }