Rework to support 7.0 and 7.1

This commit is contained in:
Gustavo Lopes
2017-07-20 04:43:00 +01:00
parent cc7edac87b
commit 83756881c2
15 changed files with 704 additions and 165 deletions

View File

@@ -1,13 +1,19 @@
language: c
dist: trusty
addons:
apt:
packages:
- valgrind
env:
#- PHP_VERSION=5.2.17 ZTS=yes MIRROR=http://museum.php.net/php5/
- PHP_VERSION=5.3.29 ZTS=yes
- PHP_VERSION=5.4.45 ZTS=yes
- PHP_VERSION=5.5.37 ZTS=yes
- PHP_VERSION=5.6.30 ZTS=yes
- PHP_VERSION=5.6.30 ZTS=yes RUN_TESTS_FLAGS=-m
- PHP_VERSION=5.6.30 ZTS=no
- PHP_VERSION=7.0.21 ZTS=yes COVERAGE=yes
- PHP_VERSION=7.1.7 ZTS=yes RUN_TESTS_FLAGS=-m
cache:
directories:
@@ -18,7 +24,10 @@ before_install:
- maybe_install_php $PHP_VERSION $ZTS
install:
- build $PHP_VERSION $ZTS
- build $PHP_VERSION $ZTS "$COVERAGE"
script:
- run_tests $PHP_VERSION $ZTS
after_success:
- 'test "$COVERAGE" != "yes" || bash <(curl -s https://codecov.io/bash)'

View File

@@ -46,6 +46,8 @@
#ifndef PHP_RAR_H
#define PHP_RAR_H
#include <php.h>
extern zend_module_entry rar_module_entry;
#define phpext_rar_ptr &rar_module_entry
@@ -81,13 +83,70 @@ enum HOST_SYSTEM {
/* maximum comment size if 64KB */
#define RAR_MAX_COMMENT_SIZE 65536
/* PHP 7+ abstraction */
#if PHP_MAJOR_VERSION >= 7
typedef zend_object* rar_obj_ref;
#define rar_zval_add_ref(ppzv) zval_add_ref(*ppzv)
#define ZVAL_ALLOC_DUP(dst, src) \
do { \
dst = (zval*) emalloc(sizeof(zval)); \
ZVAL_DUP(dst, src); \
} while (0)
#define RAR_RETURN_STRINGL(s, l, duplicate) \
do { \
RETVAL_STRINGL(s, l); \
if (duplicate == 0) { \
efree(s); \
} \
return; \
} while (0)
#define RAR_ZVAL_STRING(z, s, duplicate) \
do { \
ZVAL_STRING(z, s); \
if (duplicate == 0) { \
efree(s); \
} \
} while (0)
typedef size_t zpp_s_size_t;
#define MAKE_STD_ZVAL(zv_p) \
do { \
(zv_p) = emalloc(sizeof(zval)); \
ZVAL_NULL(zv_p); \
} while (0)
#define INIT_ZVAL(zv) ZVAL_UNDEF(&zv)
#define ZEND_ACC_FINAL_CLASS ZEND_ACC_FINAL
#else /* PHP 5.x */
typedef zend_object_handle rar_obj_ref;
#define rar_zval_add_ref zval_add_ref
#define ZVAL_ALLOC_DUP(dst, src) \
do { \
zval *z_src = src; \
dst = z_src; \
zval_add_ref(&dst); \
SEPARATE_ZVAL(&dst); \
} while (0)
#define RAR_ZVAL_STRING ZVAL_STRING
#define RAR_RETURN_STRINGL(s, l, duplicate) RETURN_STRINGL(s, l, duplicate)
typedef int zpp_s_size_t;
#define zend_hash_str_del zend_hash_del
#endif
typedef struct _rar_cb_user_data {
char *password; /* can be NULL */
zval *callable; /* can be NULL */
} rar_cb_user_data;
typedef struct rar {
zend_object_handle id;
rar_obj_ref obj_ref;
struct _rar_entries *entries;
struct RAROpenArchiveDataEx *list_open_data;
struct RAROpenArchiveDataEx *extract_open_data;
@@ -99,7 +158,7 @@ typedef struct rar {
} rar_file_t;
/* Misc */
#ifdef ZTS
#if defined(ZTS) && PHP_MAJOR_VERSION < 7
# define RAR_TSRMLS_TC , void ***
#else
# define RAR_TSRMLS_TC
@@ -134,7 +193,7 @@ typedef struct _rar_contents_cache {
int misses;
/* args: cache key, cache key size, cached object) */
void (*put)(const char *, uint, zval * RAR_TSRMLS_TC);
zval *(*get)(const char *, uint RAR_TSRMLS_TC);
zval *(*get)(const char *, uint, zval * RAR_TSRMLS_TC);
} rar_contents_cache;
/* Module globals, currently used for dir wrappers cache */
@@ -169,6 +228,15 @@ ZEND_EXTERN_MODULE_GLOBALS(rar);
#endif
/* Other compatibility quirks */
/* PHP 5.3 doesn't have ZVAL_COPY_VALUE */
#if !defined(ZEND_COPY_VALUE) && PHP_MAJOR_VERSION == 5
#define ZVAL_COPY_VALUE(z, v) \
do { \
(z)->value = (v)->value; \
Z_TYPE_P(z) = Z_TYPE_P(v); \
} while (0)
#endif
#if !defined(HAVE_STRNLEN) || !HAVE_STRNLEN
size_t _rar_strnlen(const char *s, size_t maxlen);
# define strnlen _rar_strnlen

77
rar.c
View File

@@ -160,8 +160,14 @@ void _rar_destroy_userdata(rar_cb_user_data *udata) /* {{{ */
efree(udata->password);
}
if (udata->callable != NULL)
if (udata->callable != NULL) {
#if PHP_MAJOR_VERSION < 7
zval_ptr_dtor(&udata->callable);
#else
zval_ptr_dtor(udata->callable);
efree(udata->callable);
#endif
}
udata->password = NULL;
udata->callable = NULL;
@@ -404,7 +410,7 @@ PHP_FUNCTION(rar_wrapper_cache_stats) /* {{{ */
len = spprintf(&result, 0, "%u/%u (hits/misses)",
RAR_G(contents_cache).hits, RAR_G(contents_cache).misses);
RETURN_STRINGL(result, len, 0);
RAR_RETURN_STRINGL(result, len, 0);
}
/* }}} */
/* }}} */
@@ -441,27 +447,50 @@ static int _rar_unrar_volume_user_callback(char* dst_buffer,
zend_fcall_info_cache *cache
TSRMLS_DC) /* {{{ */
{
#if PHP_MAJOR_VERSION < 7
zval *failed_vol,
*retval_ptr = NULL,
**params;
#else
zval failed_vol,
retval,
*params,
*const retval_ptr = &retval;
#endif
int ret = -1;
#if PHP_MAJOR_VERSION < 7
MAKE_STD_ZVAL(failed_vol);
ZVAL_STRING(failed_vol, dst_buffer, 1);
RAR_ZVAL_STRING(failed_vol, dst_buffer, 1);
params = &failed_vol;
fci->retval_ptr_ptr = &retval_ptr;
fci->params = &params;
#else
ZVAL_STRING(&failed_vol, dst_buffer);
ZVAL_NULL(&retval);
params = &failed_vol;
fci->retval = &retval;
fci->params = params;
#endif
fci->param_count = 1;
#if PHP_MAJOR_VERSION < 7
if (zend_call_function(fci, cache TSRMLS_CC) != SUCCESS ||
fci->retval_ptr_ptr == NULL ||
*fci->retval_ptr_ptr == NULL) {
#else
if (zend_call_function(fci, cache TSRMLS_CC) != SUCCESS || EG(exception)) {
#endif
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Failure to call volume find callback");
goto cleanup;
}
#if PHP_MAJOR_VERSION < 7
assert(*fci->retval_ptr_ptr == retval_ptr);
#else
assert(fci->retval == &retval);
#endif
if (Z_TYPE_P(retval_ptr) == IS_NULL) {
/* let return -1 */
}
@@ -500,9 +529,15 @@ static int _rar_unrar_volume_user_callback(char* dst_buffer,
}
cleanup:
#if PHP_MAJOR_VERSION < 7
zval_ptr_dtor(&failed_vol);
if (retval_ptr != NULL)
if (retval_ptr != NULL) {
zval_ptr_dtor(&retval_ptr);
}
#else
zval_ptr_dtor(&failed_vol);
zval_ptr_dtor(&retval);
#endif
return ret;
}
/* }}} */
@@ -616,12 +651,15 @@ static zend_function_entry rar_functions[] = {
/* {{{ Globals' related activities */
ZEND_DECLARE_MODULE_GLOBALS(rar);
#if PHP_MAJOR_VERSION < 7
static int _rar_array_apply_remove_first(void *pDest TSRMLS_DC)
#else
static int _rar_array_apply_remove_first(zval *pDest TSRMLS_DC)
#endif
{
return (ZEND_HASH_APPLY_STOP | ZEND_HASH_APPLY_REMOVE);
}
/* caller should increment zval refcount before calling this */
static void _rar_contents_cache_put(const char *key,
uint key_len,
zval *zv TSRMLS_DC)
@@ -634,21 +672,38 @@ static void _rar_contents_cache_put(const char *key,
zend_hash_apply(cc->data, _rar_array_apply_remove_first TSRMLS_CC);
assert(zend_hash_num_elements(cc->data) == cur_size - 1);
}
zval_add_ref(&zv);
rar_zval_add_ref(&zv);
#if PHP_MAJOR_VERSION < 7
assert(Z_REFCOUNT_P(zv) > 1);
SEPARATE_ZVAL(&zv); /* ensure we store a heap allocated copy */
zend_hash_update(cc->data, key, key_len, &zv, sizeof(zv), NULL);
#else
zend_hash_str_update(cc->data, key, key_len, zv);
#endif
}
static zval *_rar_contents_cache_get(const char *key,
uint key_len TSRMLS_DC)
uint key_len,
zval *rv TSRMLS_DC)
{
rar_contents_cache *cc = &RAR_G(contents_cache);
zval **element = NULL;
zend_hash_find(cc->data, key, key_len, (void **) &element);
zval *element = NULL;
#if PHP_MAJOR_VERSION < 7
zval **element_p = NULL;
zend_hash_find(cc->data, key, key_len, (void **) &element_p);
if (element_p) {
element = *element_p;
}
#else
element = zend_hash_str_find(cc->data, key, key_len);
#endif
if (element != NULL) {
cc->hits++;
zval_add_ref(element);
return *element;
INIT_ZVAL(*rv);
ZVAL_COPY_VALUE(rv, element);
zval_copy_ctor(rv);
return rv;
}
else {
cc->misses++;

View File

@@ -70,7 +70,7 @@ void _rar_handle_ext_error(const char *format TSRMLS_DC, ...) /* {{{ */
va_list arg;
char *message;
#ifdef ZTS
#if defined(ZTS) && PHP_MAJOR_VERSION < 7
va_start(arg, TSRMLS_C);
#else
va_start(arg, format);
@@ -91,9 +91,13 @@ int _rar_using_exceptions(TSRMLS_D)
zval *pval;
pval = zend_read_static_property(rarexception_ce_ptr, "usingExceptions",
sizeof("usingExceptions") -1, (zend_bool) 1 TSRMLS_CC);
#if PHP_MAJOR_VERSION < 7
assert(Z_TYPE_P(pval) == IS_BOOL);
return Z_BVAL_P(pval);
#else
assert(Z_TYPE_P(pval) == IS_TRUE || Z_TYPE_P(pval) == IS_FALSE);
return Z_TYPE_P(pval) == IS_TRUE;
#endif
}
/* returns a string or NULL if not an error */
@@ -182,7 +186,11 @@ PHP_METHOD(rarexception, setUsingExceptions)
Return whether exceptions are being used */
PHP_METHOD(rarexception, isUsingExceptions)
{
#if PHP_MAJOR_VERSION < 7
zval **pval;
#else
zval *pval;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE ) {
return;
@@ -192,15 +200,25 @@ PHP_METHOD(rarexception, isUsingExceptions)
#if PHP_VERSION_ID < 50399
pval = zend_std_get_static_property(rarexception_ce_ptr, "usingExceptions",
sizeof("usingExceptions") -1, (zend_bool) 0 TSRMLS_CC);
#else
#elif PHP_MAJOR_VERSION < 7
pval = zend_std_get_static_property(rarexception_ce_ptr, "usingExceptions",
sizeof("usingExceptions") -1, (zend_bool) 0, NULL TSRMLS_CC);
#else
zend_string *prop_name =
zend_string_init("usingExceptions", sizeof("usingExceptions") - 1, 0);
pval = zend_std_get_static_property(rarexception_ce_ptr, prop_name,
(zend_bool) 0);
zend_string_release(prop_name);
#endif
/* property always exists */
assert(pval != NULL);
#if PHP_MAJOR_VERSION < 7
assert(Z_TYPE_PP(pval) == IS_BOOL);
RETURN_ZVAL(*pval, 0, 0);
#else
assert(Z_TYPE_P(pval) == IS_TRUE || Z_TYPE_P(pval) == IS_FALSE);
RETURN_ZVAL(pval, 0, 0);
#endif
}
/* }}} */
@@ -224,8 +242,13 @@ void minit_rarerror(TSRMLS_D) /* {{{ */
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "RarException", php_rarexception_class_functions);
#if PHP_MAJOR_VERSION < 7
rarexception_ce_ptr = zend_register_internal_class_ex(&ce,
zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
#else
rarexception_ce_ptr = zend_register_internal_class_ex(&ce,
zend_exception_get_default(TSRMLS_C));
#endif
rarexception_ce_ptr->ce_flags |= ZEND_ACC_FINAL;
zend_declare_property_bool(rarexception_ce_ptr, "usingExceptions",
sizeof("usingExceptions") -1, 0L /* FALSE */,

View File

@@ -73,6 +73,9 @@ static void _rar_nav_get_depth_and_length(wchar_t *filenamew, const size_t file_
int *depth_out, size_t *wlen_out TSRMLS_DC);
static int _rar_nav_get_depth(const wchar_t *filenamew, const size_t file_size);
static int _rar_nav_compare_entries(const void *op1, const void *op2 TSRMLS_DC);
#if PHP_MAJOR_VERSION >= 7
static void _rar_nav_swap_entries(void *op1, void *op2);
#endif
static int _rar_nav_compare_entries_std(const void *op1, const void *op2);
static inline int _rar_nav_compare_values(const wchar_t *str1, const int depth1,
const wchar_t *str2, const int depth2,
@@ -113,9 +116,15 @@ void _rar_entry_search_start(rar_file_t *rar,
sizeof rar->entries->entries_array_s[0]);
memcpy(rar->entries->entries_array_s, rar->entries->entries_array,
rar->entries->num_entries * sizeof rar->entries->entries_array[0]);
#if PHP_MAJOR_VERSION < 7
zend_qsort(rar->entries->entries_array_s, rar->entries->num_entries,
sizeof *rar->entries->entries_array_s, _rar_nav_compare_entries
TSRMLS_CC);
#else
zend_qsort(rar->entries->entries_array_s, rar->entries->num_entries,
sizeof *rar->entries->entries_array_s, _rar_nav_compare_entries,
_rar_nav_swap_entries);
#endif
}
}
/* }}} */
@@ -137,7 +146,11 @@ void _rar_entry_search_seek(rar_find_output *state, size_t pos)
/* {{{ _rar_entry_search_end */
void _rar_entry_search_end(rar_find_output *state)
{
efree(state);
if (state) {
/* may not have been initialized due to error conditions
* in rararch_it_get_iterator that jumped out of the function */
efree(state);
}
}
/* }}} */
@@ -476,6 +489,21 @@ static int _rar_nav_compare_entries(const void *op1, const void *op2 TSRMLS_DC)
}
/* }}} */
#if PHP_MAJOR_VERSION >= 7
static void _rar_nav_swap_entries(void *op1, void *op2) /* {{{ */
{
/* just swaps two pointer values */
struct _rar_unique_entry **a = op1,
**b = op2,
*tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
/* }}} */
#endif
static int _rar_nav_compare_entries_std(const void *op1, const void *op2) /* {{{ */
{
const struct _rar_unique_entry *a = *((struct _rar_unique_entry **) op1),

View File

@@ -64,7 +64,7 @@ typedef struct php_rar_stream_data_t {
} php_rar_stream_data, *php_rar_stream_data_P;
typedef struct php_rar_dir_stream_data_t {
zval *rar_obj;
zval rar_obj;
rar_find_output *state;
struct RARHeaderDataEx *self_header; /* NULL for root */
wchar_t *directory;
@@ -416,12 +416,19 @@ static size_t php_rar_dir_ops_read(php_stream *stream, char *buf, size_t count T
entry.d_name, sizeof entry.d_name);
if (!self->no_encode) { /* urlencode entry */
#if PHP_MAJOR_VERSION < 7
int new_len;
char *encoded_name;
encoded_name = php_url_encode(entry.d_name, strlen(entry.d_name),
&new_len);
strlcpy(entry.d_name, encoded_name, sizeof entry.d_name);
efree(encoded_name);
#else
zend_string *encoded_name =
php_url_encode(entry.d_name, strlen(entry.d_name));
strlcpy(entry.d_name, encoded_name->val, sizeof entry.d_name);
zend_string_release(encoded_name);
#endif
}
@@ -437,7 +444,11 @@ static int php_rar_dir_ops_close(php_stream *stream, int close_handle TSRMLS_DC)
{
STREAM_DIR_DATA_FROM_STREAM
#if PHP_MAJOR_VERSION < 7
zval_dtor(&self->rar_obj);
#else
zval_ptr_dtor(&self->rar_obj);
#endif
efree(self->directory);
efree(self->state);
efree(self);
@@ -511,8 +522,7 @@ php_stream *php_stream_rar_open(char *arc_name,
if (cb_udata_ptr->password != NULL)
self->cb_userdata.password = estrdup(cb_udata_ptr->password);
if (cb_udata_ptr->callable != NULL) {
self->cb_userdata.callable = cb_udata_ptr->callable;
zval_add_ref(&self->cb_userdata.callable);
ZVAL_ALLOC_DUP(self->cb_userdata.callable, cb_udata_ptr->callable);
}
result = _rar_find_file_p(&self->open_data, position, &self->cb_userdata,
@@ -633,7 +643,10 @@ static void php_rar_process_context(php_stream_context *context,
char **file_password, /* can be NULL */
zval **volume_cb TSRMLS_DC)
{
zval **ctx_opt = NULL;
zval *ctx_opt;
#if PHP_MAJOR_VERSION < 7
zval **ctx_opt_p = NULL;
#endif
assert(context != NULL);
assert(open_password != NULL);
@@ -643,32 +656,50 @@ static void php_rar_process_context(php_stream_context *context,
/* TODO: don't know if I can log errors and not fail. check that */
if (php_stream_context_get_option(context, "rar", "open_password", &ctx_opt) ==
SUCCESS) {
if (Z_TYPE_PP(ctx_opt) != IS_STRING)
#if PHP_MAJOR_VERSION < 7
if (php_stream_context_get_option(
context, "rar", "open_password", &ctx_opt_p) == SUCCESS) {
ctx_opt = *ctx_opt_p;
#else
if ((ctx_opt = php_stream_context_get_option(
context, "rar", "open_password"))) {
#endif
if (Z_TYPE_P(ctx_opt) != IS_STRING)
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"RAR open password was provided, but not a string.");
else
*open_password = Z_STRVAL_PP(ctx_opt);
*open_password = Z_STRVAL_P(ctx_opt);
}
#if PHP_MAJOR_VERSION < 7
if (file_password != NULL && php_stream_context_get_option(context, "rar",
"file_password", &ctx_opt) == SUCCESS) {
if (Z_TYPE_PP(ctx_opt) != IS_STRING)
"file_password", &ctx_opt_p) == SUCCESS) {
ctx_opt = *ctx_opt_p;
#else
if (file_password != NULL && (ctx_opt = php_stream_context_get_option(
context, "rar", "file_password"))) {
#endif
if (Z_TYPE_P(ctx_opt) != IS_STRING)
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"RAR file password was provided, but not a string.");
else
*file_password = Z_STRVAL_PP(ctx_opt);
*file_password = Z_STRVAL_P(ctx_opt);
}
#if PHP_MAJOR_VERSION < 7
if (php_stream_context_get_option(context, "rar", "volume_callback",
&ctx_opt) == SUCCESS) {
#if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 2
if (zend_is_callable(*ctx_opt, IS_CALLABLE_STRICT, NULL)) {
&ctx_opt_p) == SUCCESS) {
ctx_opt = *ctx_opt_p;
#else
if (zend_is_callable(*ctx_opt, IS_CALLABLE_STRICT, NULL TSRMLS_CC)) {
if ((ctx_opt = php_stream_context_get_option(
context, "rar", "volume_callback"))) {
#endif
*volume_cb = *ctx_opt;
#if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 2
if (zend_is_callable(ctx_opt, IS_CALLABLE_STRICT, NULL)) {
#else
if (zend_is_callable(ctx_opt, IS_CALLABLE_STRICT, NULL TSRMLS_CC)) {
#endif
*volume_cb = ctx_opt;
}
else
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
@@ -736,7 +767,17 @@ static int _rar_get_archive_and_fragment(php_stream_wrapper *wrapper,
if (!(options & STREAM_ASSUME_REALPATH)) {
if (options & USE_PATH) {
#if PHP_MAJOR_VERSION < 7
*archive = zend_resolve_path(tmp_archive, tmp_arch_len TSRMLS_CC);
#else
zend_string *arc_str = zend_resolve_path(tmp_archive, tmp_arch_len);
if (arc_str != NULL) {
*archive = estrndup(arc_str->val, arc_str->len);
} else {
*archive = NULL;
}
zend_string_release(arc_str);
#endif
}
if (*archive == NULL) {
if ((*archive = expand_filepath(tmp_archive, NULL TSRMLS_CC))
@@ -804,10 +845,17 @@ cleanup:
/* {{{ php_stream_rar_opener */
static php_stream *php_stream_rar_opener(php_stream_wrapper *wrapper,
#if PHP_MAJOR_VERSION < 7
char *filename,
char *mode,
int options,
char **opened_path,
#else
const char *filename,
const char *mode,
int options,
zend_string **opened_path,
#endif
php_stream_context *context
STREAMS_DC TSRMLS_DC)
{
@@ -854,9 +902,7 @@ static php_stream *php_stream_rar_opener(php_stream_wrapper *wrapper,
if (open_passwd != NULL)
self->cb_userdata.password = estrdup(open_passwd);
if (volume_cb != NULL) {
self->cb_userdata.callable = volume_cb;
zval_add_ref(&self->cb_userdata.callable);
SEPARATE_ZVAL(&self->cb_userdata.callable);
ZVAL_ALLOC_DUP(self->cb_userdata.callable, volume_cb);
}
rar_result = _rar_find_file_w(&self->open_data, fragment,
@@ -918,10 +964,16 @@ static php_stream *php_stream_rar_opener(php_stream_wrapper *wrapper,
cleanup:
if (tmp_open_path != NULL) {
if (opened_path != NULL)
if (opened_path != NULL) {
#if PHP_MAJOR_VERSION < 7
*opened_path = tmp_open_path;
else
#else
*opened_path =
zend_string_init(tmp_open_path, strlen(tmp_open_path), 0);
#endif
} else {
efree(tmp_open_path);
}
}
if (fragment != NULL)
efree(fragment);
@@ -964,25 +1016,30 @@ static int _rar_get_cachable_rararch(php_stream_wrapper *wrapper,
const char* arch_path,
const char* open_passwd,
zval *volume_cb,
zval **rar_obj,
zval *rar_obj, /* output */
rar_file_t **rar TSRMLS_DC) /* {{{ */
{
char *cache_key = NULL;
uint cache_key_len;
int err_code,
ret = FAILURE;
zval *cache_zv;
assert(rar_obj != NULL);
#if PHP_MAJOR_VERSION < 7
INIT_ZVAL(*rar_obj);
#else
ZVAL_UNDEF(rar_obj);
#endif
_rar_arch_cache_get_key(arch_path, open_passwd, volume_cb, &cache_key,
&cache_key_len);
*rar_obj = RAR_G(contents_cache).get(cache_key, cache_key_len TSRMLS_CC);
if (*rar_obj == NULL) { /* cache miss */
ALLOC_INIT_ZVAL(*rar_obj);
cache_zv = RAR_G(contents_cache).get(
cache_key, cache_key_len, rar_obj TSRMLS_CC);
if (cache_zv == NULL) { /* cache miss */
if (_rar_create_rararch_obj(arch_path, open_passwd, volume_cb,
*rar_obj, &err_code TSRMLS_CC) == FAILURE) {
rar_obj, &err_code TSRMLS_CC) == FAILURE) {
const char *err_str = _rar_error_to_string(err_code);
if (err_str == NULL)
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
@@ -999,7 +1056,7 @@ static int _rar_get_cachable_rararch(php_stream_wrapper *wrapper,
int res;
const char *err_str;
if (_rar_get_file_resource_ex(*rar_obj, rar, 1 TSRMLS_CC)
if (_rar_get_file_resource_ex(rar_obj, rar, 1 TSRMLS_CC)
== FAILURE) {
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"Bug: could not retrieve RarArchive object from zval");
@@ -1015,15 +1072,15 @@ static int _rar_get_cachable_rararch(php_stream_wrapper *wrapper,
err_str);
goto cleanup;
}
RAR_G(contents_cache).put(cache_key, cache_key_len, *rar_obj
RAR_G(contents_cache).put(cache_key, cache_key_len, rar_obj
TSRMLS_CC);
_rar_close_file_resource(*rar);
}
}
else { /* cache hit */
/* refcount of rar_obj already incremented by cache get */
if (_rar_get_file_resource_ex(*rar_obj, rar, 1 TSRMLS_CC)
== FAILURE) {
/* cache get already put the value in rar_obj and incremented the
* refcount of the object */
if (_rar_get_file_resource_ex(rar_obj, rar, 1 TSRMLS_CC) == FAILURE) {
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"Bug: could not retrieve RarArchive object from zval");
goto cleanup;
@@ -1035,9 +1092,14 @@ cleanup:
if (cache_key != NULL)
efree(cache_key);
if (ret != SUCCESS && *rar_obj != NULL) {
if (ret != SUCCESS && Z_TYPE_P(rar_obj) == IS_OBJECT) {
#if PHP_MAJOR_VERSION < 7
zval_dtor(rar_obj);
Z_TYPE_P(rar_obj) = IS_NULL;
#else
zval_ptr_dtor(rar_obj);
*rar_obj = NULL;
ZVAL_UNDEF(rar_obj);
#endif
}
return ret;
@@ -1067,16 +1129,20 @@ static void _rar_stream_tidy_wrapper_error_log(php_stream_wrapper *wrapper TSRML
#else
static void _rar_stream_tidy_wrapper_error_log(php_stream_wrapper *wrapper TSRMLS_DC)
{
if (wrapper && FG(wrapper_errors)) {
zend_hash_del(FG(wrapper_errors), (const char*)&wrapper, sizeof wrapper);
}
if (wrapper && FG(wrapper_errors)) {
zend_hash_str_del(FG(wrapper_errors), (const char*)&wrapper, sizeof wrapper);
}
}
#endif
/* }}} */
/* {{{ php_stream_rar_stater */
static int php_stream_rar_stater(php_stream_wrapper *wrapper,
#if PHP_MAJOR_VERSION < 7
char *url,
#else
const char *url,
#endif
int flags,
php_stream_statbuf *ssb,
php_stream_context *context TSRMLS_DC)
@@ -1090,11 +1156,17 @@ static int php_stream_rar_stater(php_stream_wrapper *wrapper,
zval *volume_cb = NULL;
size_t fragment_len;
rar_file_t *rar;
zval *rararch = NULL;
zval rararch;
rar_find_output *state = NULL;
int ret = FAILURE;
/* {{{ preliminaries */
#if PHP_MAJOR_VERSION < 7
Z_TYPE(rararch) = IS_NULL;
#else
ZVAL_UNDEF(&rararch);
#endif
if (_rar_get_archive_and_fragment(wrapper, url, options, 1,
&open_path, &fragment, NULL TSRMLS_CC) == FAILURE) {
goto cleanup;
@@ -1135,16 +1207,24 @@ static int php_stream_rar_stater(php_stream_wrapper *wrapper,
ret = SUCCESS;
cleanup:
if (open_path != NULL)
if (open_path != NULL) {
efree(open_path);
}
if (fragment != NULL)
if (fragment != NULL) {
efree(fragment);
}
if (rararch != NULL)
if (Z_TYPE(rararch) == IS_OBJECT) {
#if PHP_MAJOR_VERSION < 7
zval_dtor(&rararch);
#else
zval_ptr_dtor(&rararch);
if (state != NULL)
#endif
}
if (state != NULL) {
_rar_entry_search_end(state);
}
/* note PHP_STREAM_URL_STAT_QUIET is not equivalent to ~REPORT_ERRORS.
* ~REPORT_ERRORS instead of emitting a notice, stores the error in the
@@ -1153,8 +1233,9 @@ cleanup:
* consistency, I treat both the same way but clean the wrapper in the end
* if necessary
*/
if (flags & PHP_STREAM_URL_STAT_QUIET)
if (flags & PHP_STREAM_URL_STAT_QUIET) {
_rar_stream_tidy_wrapper_error_log(wrapper TSRMLS_CC);
}
return ret;
}
@@ -1162,10 +1243,17 @@ cleanup:
/* {{{ php_stream_rar_dir_opener */
static php_stream *php_stream_rar_dir_opener(php_stream_wrapper *wrapper,
#if PHP_MAJOR_VERSION < 7
char *filename,
char *mode,
int options,
char **opened_path,
#else
const char *filename,
const char *mode,
int options,
zend_string **opened_path,
#endif
php_stream_context *context
STREAMS_DC TSRMLS_DC)
{
@@ -1263,22 +1351,35 @@ static php_stream *php_stream_rar_dir_opener(php_stream_wrapper *wrapper,
cleanup:
if (tmp_open_path != NULL) {
if (opened_path != NULL)
if (opened_path != NULL) {
#if PHP_MAJOR_VERSION < 7
*opened_path = tmp_open_path;
else
#else
*opened_path =
zend_string_init(tmp_open_path, strlen(tmp_open_path), 0);
#endif
} else {
efree(tmp_open_path);
}
}
if (fragment != NULL)
efree(fragment);
if (stream == NULL) { /* failed */
if (self != NULL) {
if (self->rar_obj != NULL)
if (Z_TYPE(self->rar_obj) == IS_OBJECT) {
#if PHP_MAJOR_VERSION < 7
zval_dtor(&self->rar_obj);
#else
zval_ptr_dtor(&self->rar_obj);
if (self->directory != NULL)
#endif
}
if (self->directory != NULL) {
efree(self->directory);
if (self->state != NULL)
}
if (self->state != NULL) {
_rar_entry_search_end(self->state);
}
efree(self);
}
}

263
rararch.c
View File

@@ -41,14 +41,23 @@ extern "C" {
/* {{{ Type definitions reserved for this translation unit */
typedef struct _ze_rararch_object {
#if PHP_MAJOR_VERSION < 7
zend_object parent;
rar_file_t *rar_file;
#else
rar_file_t *rar_file;
zend_object parent;
#endif
} ze_rararch_object;
typedef struct _rararch_iterator {
zend_object_iterator parent;
rar_find_output *state;
#if PHP_MAJOR_VERSION < 7
zval *value;
#else
zval value;
#endif
int empty_iterator; /* iterator should give nothing */
} rararch_iterator;
/* }}} */
@@ -72,15 +81,30 @@ static zend_object_handlers rararch_object_handlers;
/* }}} */
/* {{{ Function prototypes for functions with internal linkage */
static inline rar_obj_ref rar_obj_ref_fetch(zval *zv);
static inline void rar_obj_ref_make_zv(rar_obj_ref zo, zval *zv TSRMLS_DC);
#if PHP_MAJOR_VERSION >= 7
static inline ze_rararch_object *rararch_object_fetch(zend_object *zobj);
static ze_rararch_object *rararch_object_from_zv(const zval *zv);
static zend_object *rararch_ce_create_object(zend_class_entry *ce);
static void rararch_ce_free_object_storage(zend_object *zobj);
#else
#define rararch_object_from_zv zend_object_store_get_object
static zend_object_value rararch_ce_create_object(zend_class_entry *class_type TSRMLS_DC);
static void rararch_ce_free_object_storage(ze_rararch_object *object TSRMLS_DC);
#endif
/* }}} */
/* {{{ RarArchive handlers */
static int rararch_handlers_preamble(zval *object, rar_file_t **rar TSRMLS_DC);
static int rararch_dimensions_preamble(rar_file_t *rar, zval *offset, long *index, int quiet TSRMLS_DC);
static int rararch_count_elements(zval *object, long *count TSRMLS_DC);
#if PHP_MAJOR_VERSION < 7
static zval *rararch_read_dimension(zval *object, zval *offset, int type TSRMLS_DC);
#else
static zval *rararch_read_dimension(zval *object, zval *offset, int type, zval *rv);
#endif
static void rararch_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC);
static int rararch_has_dimension(zval *object, zval *offset, int check_empty TSRMLS_DC);
/* }}} */
@@ -124,15 +148,13 @@ int _rar_create_rararch_obj(const char* resolved_path,
rar->cb_userdata.password = estrdup(open_password);
}
if (volume_callback != NULL) {
rar->cb_userdata.callable = volume_callback;
zval_add_ref(&rar->cb_userdata.callable);
SEPARATE_ZVAL(&rar->cb_userdata.callable);
ZVAL_ALLOC_DUP(rar->cb_userdata.callable, volume_callback);
}
object_init_ex(object, rararch_ce_ptr);
zobj = zend_object_store_get_object(object TSRMLS_CC);
zobj = rararch_object_from_zv(object TSRMLS_CC);
zobj->rar_file = rar;
rar->id = Z_OBJ_HANDLE_P(object);
rar->obj_ref = rar_obj_ref_fetch(object);
RARSetCallback(rar->arch_handle, _rar_unrar_callback,
(LPARAM) &rar->cb_userdata);
@@ -172,7 +194,7 @@ void _rar_close_file_resource(rar_file_t *rar) /* {{{ */
int _rar_get_file_resource_ex(zval *zval_file, rar_file_t **rar_file, int silent TSRMLS_DC) /* {{{ */
{
ze_rararch_object *zobj;
zobj = zend_object_store_get_object(zval_file TSRMLS_CC);
zobj = rararch_object_from_zv(zval_file TSRMLS_CC);
if (zobj == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Could not find object in the store. This is a bug, please report it.");
@@ -196,16 +218,10 @@ int _rar_get_file_resource_ex(zval *zval_file, rar_file_t **rar_file, int silent
static void _rar_raw_entries_to_array(rar_file_t *rar, zval *target TSRMLS_DC) /* {{{ */
{
rar_find_output *state;
zval *rararch_obj;
zval rararch_obj;
/* create zval to point to the RarArchive object) */
MAKE_STD_ZVAL(rararch_obj);
Z_TYPE_P(rararch_obj) = IS_OBJECT;
Z_OBJ_HANDLE_P(rararch_obj) = rar->id;
Z_OBJ_HT_P(rararch_obj) = &rararch_object_handlers;
/* object has a new reference; if not incremented, the object would be
* be destroyed when this new zval we created was destroyed */
zend_objects_store_add_ref_by_handle(rar->id TSRMLS_CC);
/* make zval point to the RarArchive object */
rar_obj_ref_make_zv(rar->obj_ref, &rararch_obj TSRMLS_CC);
_rar_entry_search_start(rar, RAR_SEARCH_TRAVERSE, &state TSRMLS_CC);
do {
@@ -214,22 +230,70 @@ static void _rar_raw_entries_to_array(rar_file_t *rar, zval *target TSRMLS_DC) /
zval *entry_obj;
MAKE_STD_ZVAL(entry_obj);
_rar_entry_to_zval(rararch_obj, state->header, state->packed_size,
_rar_entry_to_zval(&rararch_obj, state->header, state->packed_size,
state->position, entry_obj TSRMLS_CC);
add_next_index_zval(target, entry_obj);
#if PHP_MAJOR_VERSION >= 7
/* PHP 7 copies the zval (but without increasing the refcount of the
* obj), while 5.x simply copies the pointer. Only for PHP 5.x do we
* keep the allocation) */
efree(entry_obj);
#endif
}
} while (state->eof == 0);
_rar_entry_search_end(state);
/* it was created with refcount=1 and incremented for each RarEntry object
* created, so we must decrease by one (this will also destroy it if
* there were no entries */
#if PHP_MAJOR_VERSION < 7
zval_dtor(&rararch_obj);
#else
zval_ptr_dtor(&rararch_obj);
#endif
}
/* }}} */
static zend_object_value rararch_ce_create_object(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
#if PHP_MAJOR_VERSION >=7
static inline rar_obj_ref rar_obj_ref_fetch(zval *zv)
{
return Z_OBJ(*zv);
}
static inline void rar_obj_ref_make_zv(rar_obj_ref zo, zval *zv TSRMLS_DC)
{
ZVAL_OBJ(zv, zo);
zval_addref_p(zv);
}
#else
inline rar_obj_ref rar_obj_ref_fetch(zval *zv)
{
return Z_OBJ_HANDLE_P(zv);
}
inline void rar_obj_ref_make_zv(rar_obj_ref zoh, zval *zv TSRMLS_DC)
{
INIT_ZVAL(*zv);
Z_TYPE_P(zv) = IS_OBJECT;
Z_OBJ_HANDLE_P(zv) = zoh;
Z_OBJ_HT_P(zv) = &rararch_object_handlers;
/* object has a new reference; if not incremented, the object would be
* be destroyed when this new zval we created was destroyed */
zend_objects_store_add_ref_by_handle(zoh TSRMLS_CC);
}
#endif
#if PHP_MAJOR_VERSION >=7
static inline ze_rararch_object *rararch_object_fetch(zend_object *zobj)
{
return (ze_rararch_object *)
((char *) zobj - XtOffsetOf(ze_rararch_object, parent));
}
static ze_rararch_object *rararch_object_from_zv(const zval *zv)
{
return rararch_object_fetch(Z_OBJ_P(zv));
}
#endif
/* {{{ */
#if PHP_MAJOR_VERSION < 7
static zend_object_value rararch_ce_create_object(zend_class_entry *class_type TSRMLS_DC)
{
zend_object_value zov;
ze_rararch_object *zobj;
@@ -253,10 +317,30 @@ static zend_object_value rararch_ce_create_object(zend_class_entry *class_type T
zov.handlers = &rararch_object_handlers;
return zov;
}
#else
static zend_object *rararch_ce_create_object(zend_class_entry *ce)
{
ze_rararch_object *zobj =
emalloc(sizeof(*zobj) + zend_object_properties_size(ce));
zobj->rar_file = NULL;
zend_object_std_init(&zobj->parent, ce);
zobj->parent.handlers = &rararch_object_handlers;
return &zobj->parent;
}
#endif
/* }}} */
static void rararch_ce_free_object_storage(ze_rararch_object *object TSRMLS_DC) /* {{{ */
/* {{{ */
#if PHP_MAJOR_VERSION < 7
static void rararch_ce_free_object_storage(ze_rararch_object *object TSRMLS_DC)
{
#else
static void rararch_ce_free_object_storage(zend_object *zobj)
{
ze_rararch_object *object = rararch_object_fetch(zobj);
#endif
rar_file_t *rar = object->rar_file;
/* may be NULL if the user did new RarArchive() */
@@ -279,8 +363,10 @@ static void rararch_ce_free_object_storage(ze_rararch_object *object TSRMLS_DC)
/* could call zend_objects_free_object_storage here (not before!), but
* instead I'll mimic its behaviour */
zend_object_std_dtor((zend_object*) object TSRMLS_CC);
zend_object_std_dtor(&object->parent TSRMLS_CC);
#if PHP_MAJOR_VERSION < 7
efree(object);
#endif
}
/* }}} */
@@ -348,10 +434,18 @@ static int rararch_dimensions_preamble(rar_file_t *rar,
}
else if (Z_TYPE_P(offset) == IS_OBJECT) {
if (Z_OBJ_HT_P(offset)->get) {
/* get handler cannot return NULL */
zval *newoffset = Z_OBJ_HT_P(offset)->get(offset TSRMLS_CC);
zval *newoffset = NULL;
int ret;
#if PHP_MAJOR_VERSION < 7
newoffset = Z_OBJ_HT_P(offset)->get(offset TSRMLS_CC);
#else
zval zv_holder;
ZVAL_NULL(&zv_holder);
newoffset = Z_OBJ_HT_P(offset)->get(offset, &zv_holder);
#endif
/* get handler cannot return NULL */
assert(newoffset != NULL);
if (Z_TYPE_P(newoffset) == IS_OBJECT) {
RAR_DOCREF_IF_UNQUIET(NULL TSRMLS_CC, E_WARNING,
"Could not convert object given as dimension index into "
@@ -361,7 +455,11 @@ static int rararch_dimensions_preamble(rar_file_t *rar,
ret = rararch_dimensions_preamble(rar, newoffset, index, quiet
TSRMLS_CC);
FREE_ZVAL(newoffset);
#if PHP_MAJOR_VERSION < 7
zval_ptr_dtor(&newoffset);
#else
zval_ptr_dtor(newoffset);
#endif
return ret;
}
else {
@@ -416,7 +514,11 @@ static int rararch_count_elements(zval *object, long *count TSRMLS_DC)
/* }}} */
/* {{{ RarArchive read_dimension handler */
#if PHP_MAJOR_VERSION < 7
static zval *rararch_read_dimension(zval *object, zval *offset, int type TSRMLS_DC)
#else
static zval *rararch_read_dimension(zval *object, zval *offset, int type, zval *rv)
#endif
{
long index;
rar_file_t *rar = NULL;
@@ -439,11 +541,17 @@ static zval *rararch_read_dimension(zval *object, zval *offset, int type TSRMLS_
_rar_entry_search_seek(out, (size_t) index);
_rar_entry_search_advance(out, NULL, 0, 0);
assert(out->found);
#if PHP_MAJOR_VERSION < 7
ALLOC_INIT_ZVAL(ret);
#else
ret = rv;
#endif
_rar_entry_to_zval(object, out->header, out->packed_size, out->position,
ret TSRMLS_CC);
_rar_entry_search_end(out);
#if PHP_MAJOR_VERSION < 7
Z_DELREF_P(ret); /* set refcount to 0 */
#endif
return ret;
}
/* }}} */
@@ -492,8 +600,8 @@ PHP_FUNCTION(rar_open)
char *filename;
char *password = NULL;
char resolved_path[MAXPATHLEN];
int filename_len;
int password_len = 0;
zpp_s_size_t filename_len,
password_len; /* both ignored */
zval *callable = NULL;
int err_code;
@@ -573,7 +681,7 @@ PHP_FUNCTION(rar_entry_get)
zval *file = getThis();
char *filename;
rar_file_t *rar = NULL;
int filename_len;
zpp_s_size_t filename_len;
wchar_t *filename_c = NULL;
rar_find_output *sstate;
@@ -657,7 +765,7 @@ PHP_FUNCTION(rar_comment_get)
if (cmt_state == 1) { /* comment read completely */
/* CmtSize - 1 because we don't need the null terminator */
RETURN_STRINGL(rar->list_open_data->CmtBuf,
RAR_RETURN_STRINGL(rar->list_open_data->CmtBuf,
rar->list_open_data->CmtSize - 1, 1);
}
}
@@ -764,7 +872,7 @@ PHP_METHOD(rararch, __toString)
is_closed?closed:"");
restring[restring_size - 1] = '\0'; /* just to be safe */
RETURN_STRINGL(restring, (int) restring_size - 1, 0);
RAR_RETURN_STRINGL(restring, (int) restring_size - 1, 0);
}
/* }}} */
@@ -814,8 +922,12 @@ static zend_object_iterator *rararch_it_get_iterator(zend_class_entry *ce,
static void rararch_it_dtor(zend_object_iterator *iter TSRMLS_DC);
static void rararch_it_fetch(rararch_iterator *it TSRMLS_DC);
static int rararch_it_valid(zend_object_iterator *iter TSRMLS_DC);
#if PHP_MAJOR_VERSION < 7
static void rararch_it_current_data(zend_object_iterator *iter,
zval ***data TSRMLS_DC);
#else
static zval *rararch_it_current_data(zend_object_iterator *iter);
#endif
static void rararch_it_move_forward(zend_object_iterator *iter TSRMLS_DC);
static void rararch_it_rewind(zend_object_iterator *iter TSRMLS_DC);
/* }}} */
@@ -836,6 +948,19 @@ static zend_object_iterator *rararch_it_get_iterator(zend_class_entry *ce,
it = emalloc(sizeof *it);
#if PHP_MAJOR_VERSION < 7
zval_add_ref(&object);
it->parent.data = object;
it->value = NULL;
#else
zend_iterator_init((zend_object_iterator *) it);
ZVAL_COPY(&it->parent.data, object);
ZVAL_UNDEF(&it->value);
#endif
it->parent.funcs = ce->iterator_funcs.funcs;
it->state = NULL;
res = _rar_get_file_resource_ex(object, &rar, 1 TSRMLS_CC);
if (res == FAILURE)
php_error_docref(NULL TSRMLS_CC, E_ERROR,
@@ -851,11 +976,7 @@ static zend_object_iterator *rararch_it_get_iterator(zend_class_entry *ce,
else
it->empty_iterator = 0;
zval_add_ref(&object);
it->parent.data = object;
it->parent.funcs = ce->iterator_funcs.funcs;
_rar_entry_search_start(rar, RAR_SEARCH_TRAVERSE, &it->state TSRMLS_CC);
it->value = NULL;
return (zend_object_iterator*) it;
}
/* }}} */
@@ -864,10 +985,15 @@ static zend_object_iterator *rararch_it_get_iterator(zend_class_entry *ce,
static void rararch_it_invalidate_current(zend_object_iterator *iter TSRMLS_DC)
{
rararch_iterator *it = (rararch_iterator *) iter;
#if PHP_MAJOR_VERSION < 7
if (it->value != NULL) {
zval_ptr_dtor(&it->value);
it->value = NULL;
}
#else
zval_ptr_dtor(&it->value);
ZVAL_UNDEF(&it->value);
#endif
}
/* }}} */
@@ -878,10 +1004,16 @@ static void rararch_it_dtor(zend_object_iterator *iter TSRMLS_DC)
rararch_it_invalidate_current((zend_object_iterator *) it TSRMLS_CC);
#if PHP_MAJOR_VERSION < 7
zval_ptr_dtor((zval**) &it->parent.data); /* decrease refcount on zval object */
#else
zval_ptr_dtor(&it->parent.data);
#endif
_rar_entry_search_end(it->state);
#if PHP_MAJOR_VERSION < 7
efree(it);
#endif
}
/* }}} */
@@ -890,40 +1022,72 @@ static void rararch_it_fetch(rararch_iterator *it TSRMLS_DC)
{
rar_file_t *rar_file;
int res;
zval *robj;
#if PHP_MAJOR_VERSION < 7
assert(it->value == NULL);
#else
assert(Z_TYPE(it->value) == IS_UNDEF);
#endif
if (it->empty_iterator) {
#if PHP_MAJOR_VERSION < 7
MAKE_STD_ZVAL(it->value);
ZVAL_FALSE(it->value);
#else
ZVAL_FALSE(&it->value);
#endif
return;
}
res = _rar_get_file_resource_ex(it->parent.data, &rar_file, 1 TSRMLS_CC);
#if PHP_MAJOR_VERSION < 7
robj = it->parent.data;
#else
robj = &it->parent.data;
#endif
res = _rar_get_file_resource_ex(robj, &rar_file, 1 TSRMLS_CC);
if (res == FAILURE)
php_error_docref(NULL TSRMLS_CC, E_ERROR,
"Cannot fetch RarArchive object");
_rar_entry_search_advance(it->state, NULL, 0, 0);
#if PHP_MAJOR_VERSION < 7
MAKE_STD_ZVAL(it->value);
if (it->state->found)
_rar_entry_to_zval(it->parent.data, it->state->header,
it->state->packed_size, it->state->position, it->value TSRMLS_CC);
else
_rar_entry_to_zval(robj, it->state->header, it->state->packed_size,
it->state->position, it->value TSRMLS_CC);
else {
ZVAL_FALSE(it->value);
}
#else
if (it->state->found)
_rar_entry_to_zval(&it->parent.data, it->state->header,
it->state->packed_size, it->state->position, &it->value TSRMLS_CC);
else {
ZVAL_FALSE(&it->value);
}
#endif
}
/* }}} */
/* {{{ rararch_it_valid */
static int rararch_it_valid(zend_object_iterator *iter TSRMLS_DC)
{
#if PHP_MAJOR_VERSION < 7
zval *value = ((rararch_iterator *) iter)->value;
assert(value != NULL);
return (Z_TYPE_P(value) != IS_BOOL)?SUCCESS:FAILURE;
#else
zval *value = &((rararch_iterator *) iter)->value;
assert(Z_TYPE_P(value) != IS_UNDEF);
return Z_TYPE_P(value) != IS_FALSE ? SUCCESS : FAILURE;
#endif
}
/* }}} */
/* {{{ rararch_it_current_data */
#if PHP_MAJOR_VERSION < 7
static void rararch_it_current_data(zend_object_iterator *iter,
zval ***data TSRMLS_DC)
{
@@ -931,6 +1095,20 @@ static void rararch_it_current_data(zend_object_iterator *iter,
assert(*value != NULL);
*data = value;
}
#else
static zval *rararch_it_current_data(zend_object_iterator *iter)
{
zval *ret;
#if PHP_MAJOR_VERSION < 7
ret = ((rararch_iterator *) iter)->value;
assert(ret != NULL);
#else
ret = &((rararch_iterator *) iter)->value;
assert(Z_TYPE_P(ret) != IS_UNDEF);
#endif
return ret;
}
#endif
/* }}} */
/* {{{ rararch_it_move_forward */
@@ -938,7 +1116,11 @@ static void rararch_it_move_forward(zend_object_iterator *iter TSRMLS_DC)
{
rararch_iterator *it = (rararch_iterator *) iter;
rararch_it_invalidate_current((zend_object_iterator *) it TSRMLS_CC);
#if PHP_MAJOR_VERSION < 7
it->value = NULL;
#else
ZVAL_UNDEF(&it->value);
#endif
rararch_it_fetch(it TSRMLS_CC);
}
/* }}} */
@@ -949,7 +1131,6 @@ static void rararch_it_rewind(zend_object_iterator *iter TSRMLS_DC)
rararch_iterator *it = (rararch_iterator *) iter;
rararch_it_invalidate_current((zend_object_iterator *) it TSRMLS_CC);
_rar_entry_search_rewind(it->state);
it->value = NULL;
rararch_it_fetch(it TSRMLS_CC);
}
/* }}} */
@@ -977,6 +1158,10 @@ void minit_rararch(TSRMLS_D)
rararch_object_handlers.write_dimension = rararch_write_dimension;
rararch_object_handlers.has_dimension = rararch_has_dimension;
rararch_object_handlers.unset_dimension = rararch_unset_dimension;
#if PHP_MAJOR_VERSION >= 7
rararch_object_handlers.free_obj = rararch_ce_free_object_storage;
rararch_object_handlers.offset = XtOffsetOf(ze_rararch_object, parent);
#endif
INIT_CLASS_ENTRY(ce, "RarArchive", php_rararch_class_functions);
rararch_ce_ptr = zend_register_internal_class(&ce TSRMLS_CC);

View File

@@ -41,23 +41,20 @@ extern "C" {
zend_class_entry *rar_class_entry_ptr;
/* }}} */
/* {{{ Globals with internal linkage */
static zend_object_handlers rarentry_object_handlers;
/* }}} */
/* {{{ Function prototypes for functions with internal linkage */
static int _rar_decl_priv_prop_null(zend_class_entry *ce, const char *name,
int name_length, char *doc_comment,
int doc_comment_len TSRMLS_DC);
static zval *_rar_entry_get_property(zval *entry_obj, char *name, int namelen TSRMLS_DC);
static void _rar_dos_date_to_text(int dos_time, char *date_string);
static zend_object_value rarentry_ce_create_object(zend_class_entry *class_type TSRMLS_DC);
/* }}} */
/* {{{ Functions with external linkage */
/* should be passed the last entry that corresponds to a given file
* only that one has the correct CRC. Still, it may have a wrong packedSize */
void _rar_entry_to_zval(zval *parent, /* zval to RarArchive object, will have its refcount increased */
/* parent is zval to RarArchive object. The object (not the zval, in PHP 5.x)
* will have its refcount increased */
void _rar_entry_to_zval(zval *parent,
struct RARHeaderDataEx *entry,
unsigned long packed_size,
size_t position,
@@ -68,10 +65,18 @@ void _rar_entry_to_zval(zval *parent, /* zval to RarArchive object, will have it
char *filename;
int filename_size, filename_len;
long unp_size; /* zval stores PHP ints as long, so use that here */
zval *parent_copy = parent;
#if PHP_MAJOR_VERSION < 7
/* allocate zval on the heap */
zval_addref_p(parent_copy);
SEPARATE_ZVAL(&parent_copy);
/* set refcount to 0; zend_update_property will increase it */
Z_DELREF_P(parent_copy);
#endif
object_init_ex(object, rar_class_entry_ptr);
zend_update_property(rar_class_entry_ptr, object, "rarfile",
sizeof("rararch") - 1, parent TSRMLS_CC);
sizeof("rararch") - 1, parent_copy TSRMLS_CC);
#if ULONG_MAX > 0xffffffffUL
unp_size = ((long) entry->UnpSize) + (((long) entry->UnpSizeHigh) << 32);
@@ -152,28 +157,55 @@ static int _rar_decl_priv_prop_null(zend_class_entry *ce, const char *name,
int name_length, char *doc_comment,
int doc_comment_len TSRMLS_DC) /* {{{ */
{
#if PHP_MAJOR_VERSION < 7
zval *property;
ALLOC_PERMANENT_ZVAL(property);
INIT_ZVAL(*property);
return zend_declare_property_ex(ce, name, name_length, property,
ZEND_ACC_PRIVATE, doc_comment, doc_comment_len TSRMLS_CC);
#else
zval property;
zend_string *name_str,
*doc_str;
int ret;
ZVAL_NULL(&property);
name_str = zend_string_init(name, (size_t) name_length, 1);
doc_str = zend_string_init(doc_comment, (size_t) doc_comment_len, 1);
ret = zend_declare_property_ex(ce, name_str, &property, ZEND_ACC_PRIVATE,
doc_str);
zend_string_release(name_str);
zend_string_release(doc_str);
return ret;
#endif
}
/* }}} */
static zval *_rar_entry_get_property(zval *entry_obj, char *name, int namelen TSRMLS_DC) /* {{{ */
{
zval *tmp;
#if PHP_MAJOR_VERSION >= 7
zval zv;
#endif
#if PHP_VERSION_ID < 70100
zend_class_entry *orig_scope = EG(scope);
EG(scope) = rar_class_entry_ptr;
#endif
#if PHP_MAJOR_VERSION < 7
tmp = zend_read_property(Z_OBJCE_P(entry_obj), entry_obj, name, namelen, 1 TSRMLS_CC);
#else
tmp = zend_read_property(Z_OBJCE_P(entry_obj), entry_obj, name, namelen, 1, &zv);
#endif
if (tmp == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Bug: unable to find property '%s'. Please report.", name);
}
#if PHP_VERSION_ID < 70100
EG(scope) = orig_scope;
#endif
return tmp;
}
@@ -192,29 +224,6 @@ static void _rar_dos_date_to_text(int dos_time, char *date_string) /* {{{ */
sprintf(date_string, "%u-%02u-%02u %02u:%02u:%02u", year, month, day, hour, minute, second);
}
/* }}} */
static zend_object_value rarentry_ce_create_object(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
{
zend_object_value zov;
zend_object *zobj;
zobj = emalloc(sizeof *zobj);
zend_object_std_init(zobj, class_type TSRMLS_CC);
#if PHP_VERSION_ID < 50399
zend_hash_copy(zobj->properties, &(class_type->default_properties),
(copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*));
#else
object_properties_init(zobj, class_type);
#endif
zov.handle = zend_objects_store_put(zobj,
(zend_objects_store_dtor_t) zend_objects_destroy_object,
(zend_objects_free_object_storage_t) zend_objects_free_object_storage,
NULL TSRMLS_CC);
zov.handlers = &rarentry_object_handlers;
return zov;
}
/* }}} */
/* }}} */
/* {{{ Methods */
@@ -226,7 +235,7 @@ PHP_METHOD(rarentry, extract)
char *dir,
*filepath = NULL,
*password = NULL;
int dir_len,
zpp_s_size_t dir_len,
filepath_len = 0,
password_len = 0;
char *considered_path;
@@ -360,7 +369,7 @@ PHP_METHOD(rarentry, getName)
RAR_GET_PROPERTY(tmp, "name");
RETURN_STRINGL(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), 1);
RAR_RETURN_STRINGL(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), 1);
}
/* }}} */
@@ -422,7 +431,7 @@ PHP_METHOD(rarentry, getFileTime)
RAR_GET_PROPERTY(tmp, "file_time");
RETURN_STRINGL(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), 1);
RAR_RETURN_STRINGL(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), 1);
}
/* }}} */
@@ -437,7 +446,7 @@ PHP_METHOD(rarentry, getCrc)
RAR_GET_PROPERTY(tmp, "crc");
RETURN_STRINGL(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), 1);
RAR_RETURN_STRINGL(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), 1);
}
/* }}} */
@@ -496,7 +505,7 @@ PHP_METHOD(rarentry, getStream)
zval *entry_obj = getThis();
php_stream *stream = NULL;
char *password = NULL;
int password_len; /* ignored */
zpp_s_size_t password_len; /* ignored */
rar_cb_user_data cb_udata = {NULL};
@@ -579,7 +588,7 @@ PHP_METHOD(rarentry, __toString)
char *name,
*crc;
char *restring;
int restring_len;
int restring_size;
const char format[] = "RarEntry for %s \"%s\" (%s)";
RAR_RETNULL_ON_ARGS();
@@ -595,14 +604,14 @@ PHP_METHOD(rarentry, __toString)
crc = Z_STRVAL_P(crc_zval);
/* 2 is size of %s, 8 is size of crc */
restring_len = (sizeof(format)-1) - 2 * 3 + (sizeof("directory")-1) +
restring_size = (sizeof(format)-1) - 2 * 3 + (sizeof("directory")-1) +
strlen(name) + 8 + 1;
restring = emalloc(restring_len);
snprintf(restring, restring_len, format, is_dir?"directory":"file",
restring = emalloc(restring_size);
snprintf(restring, restring_size, format, is_dir?"directory":"file",
name, crc);
restring[restring_len - 1] = '\0'; /* just to be safe */
restring[restring_size - 1] = '\0'; /* just to be safe */
RETURN_STRING(restring, 0);
RAR_RETURN_STRINGL(restring, strlen(restring), 0);
}
/* }}} */
/* }}} */
@@ -647,15 +656,10 @@ void minit_rarentry(TSRMLS_D)
{
zend_class_entry ce;
memcpy(&rarentry_object_handlers, zend_get_std_object_handlers(),
sizeof rarentry_object_handlers);
INIT_CLASS_ENTRY(ce, "RarEntry", php_rar_class_functions);
rar_class_entry_ptr = zend_register_internal_class(&ce TSRMLS_CC);
rar_class_entry_ptr->ce_flags |= ZEND_ACC_FINAL_CLASS;
rar_class_entry_ptr->clone = NULL;
/* Custom creation currently not really needed, but you never know... */
rar_class_entry_ptr->create_object = &rarentry_ce_create_object;
REG_RAR_PROPERTY("rarfile", "Associated RAR archive");
REG_RAR_PROPERTY("position", "Position inside the RAR archive");

View File

@@ -29,11 +29,11 @@ $rar = RarArchive::open($fn, null, "A::resolveInstance");
var_dump($rar);
echo "\nGiven callback that takes more arguments:\n";
$rar = RarArchive::open($fn, null, 'array_walk');
$rar = RarArchive::open($fn, null, 'strpos');
$rar->getEntries();
echo "\nGiven callback that takes another kind of arguments:\n";
$rar = RarArchive::open($fn, null, 'ksort');
$rar = RarArchive::open($fn, null, 'array_keys');
$rar->getEntries();
echo "\nGiven callback that returns another kind of arguments:\n";
@@ -64,15 +64,13 @@ bool(false)
Given callback that takes more arguments:
Warning: array_walk() expects at least %d parameters, 1 given in %s on line %d
Warning: strpos() expects at least %d parameters, 1 given in %s on line %d
Warning: RarArchive::getEntries(): ERAR_EOPEN (file open error) in %s on line %d
Given callback that takes another kind of arguments:
Warning: ksort() expects parameter 1 to be array, string given in %s on line %d
Warning: RarArchive::getEntries(): Wrong type returned by volume find callback, expected string or NULL in %s on line %d
Warning: array_keys() expects parameter 1 to be array, string given in %s on line %d
Warning: RarArchive::getEntries(): ERAR_EOPEN (file open error) in %s on line %d

View File

@@ -1,7 +1,9 @@
--TEST--
RarArchive direct instantiation does not crash
RarArchive direct instantiation does not crash (PHP 5.x)
--SKIPIF--
<?php if(!extension_loaded("rar")) die("skip");
<?php
if (!extension_loaded("rar")) die("skip");
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID >= 70000) die("skip for PHP 5.x");
--FILE--
<?php

View File

@@ -1,7 +1,8 @@
--TEST--
RarEntry direct instantiation does not crash
RarEntry direct instantiation does not crash (PHP 5.x)
--SKIPIF--
<?php if(!extension_loaded("rar")) die("skip");
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID >= 70000) die("skip for PHP 5.x");
--FILE--
<?php

17
tests/102.phpt Normal file
View File

@@ -0,0 +1,17 @@
--TEST--
RarArchive direct instantiation does not crash (PHP 7)
--SKIPIF--
<?php
if (!extension_loaded("rar")) die("skip");
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 70000) die("skip for PHP >= 7");
--FILE--
<?php
new RarArchive();
echo "Done\n";
--EXPECTF--
Fatal error: Uncaught Error: Call to private RarArchive::__construct() from invalid context in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d

16
tests/103.phpt Normal file
View File

@@ -0,0 +1,16 @@
--TEST--
RarEntry direct instantiation does not crash (PHP 7)
--SKIPIF--
<?php if(!extension_loaded("rar")) die("skip");
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 70000) die("skip for PHP >= 7");
--FILE--
<?php
new RarEntry();
echo "Done\n";
--EXPECTF--
Fatal error: Uncaught Error: Call to private RarEntry::__construct() from invalid context in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d

View File

@@ -34,18 +34,29 @@ function install_php {
}
function build_ext {
local readonly prefix=$1
local readonly prefix=$1 coverage=$2
"$prefix"/bin/phpize
if [[ $coverage = 'yes' ]]; then
export CPPFLAGS="$CPPFLAGS --coverage"
fi
./configure --with-php-config="$prefix/bin/php-config"
make -j $JOBS
}
function do_tests {
local readonly prefix=$1
local found_leaks= dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "--suppressions=$dir/valgrind.supp" | tee ~/.valgrindrc
TEST_PHP_EXECUTABLE="$prefix/bin/php" REPORT_EXIT_STATUS=1 \
"$prefix/bin/php" "$prefix"/lib/php/build/run-tests.php \
-q -d extension=modules/rar.so --set-timeout 300 --show-diff \
$RUN_TESTS_FLAGS tests
found_leaks=$(find tests -name '*.mem' | wc -l)
if [[ $found_leaks -gt 0 ]]; then
echo "Found $found_leaks leaks. Failing."
find tests -name "*.mem" -print -exec cat {} \;
return 1
fi
}
# public functions below
@@ -64,7 +75,7 @@ function build {
set -e
set -o pipefail
if [[ ! -f modules/rar.so ]]; then
build_ext "$(prefix $1 $2)"
build_ext "$(prefix $1 $2)" "$3"
fi
}

21
valgrind.supp Normal file
View File

@@ -0,0 +1,21 @@
{
<CRC32>
Memcheck:Value8
fun:_Z5CRC32jPKvm
fun:_ZN7RawRead8GetCRC15Eb
...
}
{
<Xor128>
Memcheck:Value8
fun:_Z6Xor128PhPKhS1_S1_S1_
fun:_ZN8Rijndael12blockDecryptEPKhmPh
...
}
{
<Xor128-2>
Memcheck:Value8
fun:Xor128
fun:_ZN8Rijndael12blockDecryptEPKhmPh
...
}