summaryrefslogtreecommitdiff
path: root/ext/zip/php_zip.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/zip/php_zip.c')
-rw-r--r--ext/zip/php_zip.c456
1 files changed, 169 insertions, 287 deletions
diff --git a/ext/zip/php_zip.c b/ext/zip/php_zip.c
index e607f0f077..22f9895ad8 100644
--- a/ext/zip/php_zip.c
+++ b/ext/zip/php_zip.c
@@ -2,7 +2,7 @@
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
- | Copyright (c) 1997-2018 The PHP Group |
+ | Copyright (c) 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 |
@@ -28,11 +28,7 @@
#include "ext/standard/php_string.h"
#include "ext/pcre/php_pcre.h"
#include "ext/standard/php_filestat.h"
-#if PHP_VERSION_ID >= 70200
#include "zend_interfaces.h"
-#elif defined(HAVE_SPL)
-#include "ext/spl/spl_iterators.h"
-#endif
#include "php_zip.h"
/* zip_open is a macro for renaming libzip zipopen, so we need to use PHP_NAMED_FUNCTION */
@@ -84,10 +80,10 @@ static int le_zip_entry;
#define PHP_ZIP_SET_FILE_COMMENT(za, index, comment, comment_len) \
if (comment_len == 0) { \
/* Passing NULL remove the existing comment */ \
- if (zip_set_file_comment(za, index, NULL, 0) < 0) { \
+ if (zip_file_set_comment(za, index, NULL, 0, 0) < 0) { \
RETURN_FALSE; \
} \
- } else if (zip_set_file_comment(za, index, comment, comment_len) < 0) { \
+ } else if (zip_file_set_comment(za, index, comment, comment_len, 0) < 0) { \
RETURN_FALSE; \
} \
RETURN_TRUE;
@@ -412,7 +408,7 @@ static int php_zip_parse_options(zval *options, zend_long *remove_all_path, char
#endif
/* }}} */
-static int php_zip_status(struct zip *za) /* {{{ */
+static zend_long php_zip_status(struct zip *za) /* {{{ */
{
#if LIBZIP_VERSION_MAJOR < 1
int zep, syp;
@@ -430,7 +426,7 @@ static int php_zip_status(struct zip *za) /* {{{ */
}
/* }}} */
-static int php_zip_status_sys(struct zip *za) /* {{{ */
+static zend_long php_zip_status_sys(struct zip *za) /* {{{ */
{
#if LIBZIP_VERSION_MAJOR < 1
int zep, syp;
@@ -448,9 +444,10 @@ static int php_zip_status_sys(struct zip *za) /* {{{ */
}
/* }}} */
-static int php_zip_get_num_files(struct zip *za) /* {{{ */
+static zend_long php_zip_get_num_files(struct zip *za) /* {{{ */
{
- return zip_get_num_files(za);
+ zip_int64_t num = zip_get_num_entries(za, 0);
+ return MIN(num, ZEND_LONG_MAX);
}
/* }}} */
@@ -512,9 +509,9 @@ static char * php_zipobj_get_zip_comment(struct zip *za, int *len) /* {{{ */
int php_zip_glob(char *pattern, int pattern_len, zend_long flags, zval *return_value) /* {{{ */
{
#ifdef HAVE_GLOB
- char cwd[MAXPATHLEN];
int cwd_skip = 0;
#ifdef ZTS
+ char cwd[MAXPATHLEN];
char work_pattern[MAXPATHLEN];
char *result;
#endif
@@ -577,8 +574,7 @@ int php_zip_glob(char *pattern, int pattern_len, zend_long flags, zval *return_v
/* we assume that any glob pattern will match files from one directory only
so checking the dirname of the first match should be sufficient */
- strncpy(cwd, globbuf.gl_pathv[0], MAXPATHLEN);
- if (ZIP_OPENBASEDIR_CHECKPATH(cwd)) {
+ if (ZIP_OPENBASEDIR_CHECKPATH(globbuf.gl_pathv[0])) {
return -1;
}
@@ -619,7 +615,6 @@ int php_zip_pcre(zend_string *regexp, char *path, int path_len, zval *return_val
{
#ifdef ZTS
char cwd[MAXPATHLEN];
- int cwd_skip = 0;
char work_path[MAXPATHLEN];
char *result;
#endif
@@ -638,8 +633,6 @@ int php_zip_pcre(zend_string *regexp, char *path, int path_len, zval *return_val
cwd[2] = '\0';
}
#endif
- cwd_skip = strlen(cwd)+1;
-
snprintf(work_path, MAXPATHLEN, "%s%c%s", cwd, DEFAULT_SLASH, path);
path = work_path;
}
@@ -654,10 +647,10 @@ int php_zip_pcre(zend_string *regexp, char *path, int path_len, zval *return_val
if (files_cnt > 0) {
pcre2_code *re = NULL;
pcre2_match_data *match_data = NULL;
- uint32_t preg_options = 0, i, capture_count;
+ uint32_t i, capture_count;
int rc;
- re = pcre_get_compiled_regex(regexp, &capture_count, &preg_options);
+ re = pcre_get_compiled_regex(regexp, &capture_count);
if (!re) {
php_error_docref(NULL, E_WARNING, "Invalid expression");
return -1;
@@ -684,29 +677,29 @@ int php_zip_pcre(zend_string *regexp, char *path, int path_len, zval *return_val
break;
}
- snprintf(fullpath, MAXPATHLEN, "%s%c%s", path, DEFAULT_SLASH, ZSTR_VAL(namelist[i]));
-
- if (0 != VCWD_STAT(fullpath, &s)) {
- php_error_docref(NULL, E_WARNING, "Cannot read <%s>", fullpath);
+ match_data = php_pcre_create_match_data(capture_count, re);
+ if (!match_data) {
+ /* Allocation failed, but can proceed to the next pattern. */
zend_string_release_ex(namelist[i], 0);
continue;
}
-
- if (S_IFDIR == (s.st_mode & S_IFMT)) {
+ rc = pcre2_match(re, (PCRE2_SPTR)ZSTR_VAL(namelist[i]), ZSTR_LEN(namelist[i]), 0, 0, match_data, mctx);
+ php_pcre_free_match_data(match_data);
+ /* 0 means that the vector is too small to hold all the captured substring offsets */
+ if (rc < 0) {
zend_string_release_ex(namelist[i], 0);
continue;
}
- match_data = php_pcre_create_match_data(capture_count, re);
- if (!match_data) {
- /* Allocation failed, but can proceed to the next pattern. */
+ snprintf(fullpath, MAXPATHLEN, "%s%c%s", path, DEFAULT_SLASH, ZSTR_VAL(namelist[i]));
+
+ if (0 != VCWD_STAT(fullpath, &s)) {
+ php_error_docref(NULL, E_WARNING, "Cannot read <%s>", fullpath);
zend_string_release_ex(namelist[i], 0);
continue;
}
- rc = pcre2_match(re, (PCRE2_SPTR)ZSTR_VAL(namelist[i]), ZSTR_LEN(namelist[i]), 0, preg_options, match_data, mctx);
- php_pcre_free_match_data(match_data);
- /* 0 means that the vector is too small to hold all the captured substring offsets */
- if (rc < 0) {
+
+ if (S_IFDIR == (s.st_mode & S_IFMT)) {
zend_string_release_ex(namelist[i], 0);
continue;
}
@@ -791,7 +784,7 @@ static zend_object_handlers zip_object_handlers;
static HashTable zip_prop_handlers;
-typedef int (*zip_read_int_t)(struct zip *za);
+typedef zend_long (*zip_read_int_t)(struct zip *za);
typedef char *(*zip_read_const_char_t)(struct zip *za, int *len);
typedef char *(*zip_read_const_char_from_ze_t)(ze_zip_object *obj);
@@ -827,7 +820,7 @@ static void php_zip_register_prop_handler(HashTable *prop_handler, char *name, z
static zval *php_zip_property_reader(ze_zip_object *obj, zip_prop_handler *hnd, zval *rv) /* {{{ */
{
const char *retchar = NULL;
- int retint = 0;
+ zend_long retint = 0;
int len = 0;
if (obj && obj->za != NULL) {
@@ -859,10 +852,10 @@ static zval *php_zip_property_reader(ze_zip_object *obj, zip_prop_handler *hnd,
break;
/* case IS_TRUE */
case IS_FALSE:
- ZVAL_BOOL(rv, (long)retint);
+ ZVAL_BOOL(rv, retint);
break;
case IS_LONG:
- ZVAL_LONG(rv, (long)retint);
+ ZVAL_LONG(rv, retint);
break;
default:
ZVAL_NULL(rv);
@@ -880,7 +873,11 @@ static zval *php_zip_get_property_ptr_ptr(zval *object, zval *member, int type,
zip_prop_handler *hnd = NULL;
if (Z_TYPE_P(member) != IS_STRING) {
- ZVAL_STR(&tmp_member, zval_get_string_func(member));
+ zend_string *str = zval_try_get_string_func(member);
+ if (UNEXPECTED(!str)) {
+ return NULL;
+ }
+ ZVAL_STR(&tmp_member, str);
member = &tmp_member;
cache_slot = NULL;
}
@@ -911,7 +908,11 @@ static zval *php_zip_read_property(zval *object, zval *member, int type, void **
zip_prop_handler *hnd = NULL;
if (Z_TYPE_P(member) != IS_STRING) {
- ZVAL_STR(&tmp_member, zval_get_string_func(member));
+ zend_string *str = zval_try_get_string_func(member);
+ if (UNEXPECTED(!str)) {
+ return &EG(uninitialized_zval);
+ }
+ ZVAL_STR(&tmp_member, str);
member = &tmp_member;
cache_slot = NULL;
}
@@ -947,7 +948,11 @@ static int php_zip_has_property(zval *object, zval *member, int type, void **cac
int retval = 0;
if (Z_TYPE_P(member) != IS_STRING) {
- ZVAL_STR(&tmp_member, zval_get_string_func(member));
+ zend_string *str = zval_try_get_string_func(member);
+ if (UNEXPECTED(!str)) {
+ return 0;
+ }
+ ZVAL_STR(&tmp_member, str);
member = &tmp_member;
cache_slot = NULL;
}
@@ -1030,7 +1035,7 @@ static void php_zip_object_free_storage(zend_object *object) /* {{{ */
if (intern->za) {
if (zip_close(intern->za) != 0) {
#if LIBZIP_VERSION_MAJOR == 1 && LIBZIP_VERSION_MINOR == 3 && LIBZIP_VERSION_MICRO == 1
- php_error_docref(NULL, E_WARNING, "Cannot destroy the zip context: %s", "zip_close have failed");
+ php_error_docref(NULL, E_WARNING, "Cannot destroy the zip context: zip_close have failed");
#else
php_error_docref(NULL, E_WARNING, "Cannot destroy the zip context: %s", zip_strerror(intern->za));
zip_discard(intern->za);
@@ -1172,7 +1177,7 @@ static PHP_NAMED_FUNCTION(zif_zip_open)
}
rsrc_int->index_current = 0;
- rsrc_int->num_files = zip_get_num_files(rsrc_int->za);
+ rsrc_int->num_files = zip_get_num_entries(rsrc_int->za, 0);
RETURN_RES(zend_register_resource(rsrc_int, le_zip_dir));
}
@@ -1354,47 +1359,36 @@ static void php_zip_entry_get_info(INTERNAL_FUNCTION_PARAMETERS, int opt) /* {{{
switch (opt) {
case 0:
RETURN_STRING((char *)zr_rsrc->sb.name);
- break;
case 1:
RETURN_LONG((zend_long) (zr_rsrc->sb.comp_size));
- break;
case 2:
RETURN_LONG((zend_long) (zr_rsrc->sb.size));
- break;
case 3:
switch (zr_rsrc->sb.comp_method) {
case 0:
RETURN_STRING("stored");
- break;
case 1:
RETURN_STRING("shrunk");
- break;
case 2:
case 3:
case 4:
case 5:
RETURN_STRING("reduced");
- break;
case 6:
RETURN_STRING("imploded");
- break;
case 7:
RETURN_STRING("tokenized");
break;
case 8:
RETURN_STRING("deflated");
- break;
case 9:
RETURN_STRING("deflatedX");
break;
case 10:
RETURN_STRING("implodedX");
- break;
default:
RETURN_FALSE;
}
- RETURN_LONG((zend_long) (zr_rsrc->sb.comp_method));
- break;
}
}
@@ -1441,17 +1435,15 @@ static ZIPARCHIVE_METHOD(open)
zend_long flags = 0;
char *resolved_path;
zend_string *filename;
- zval *self = getThis();
- ze_zip_object *ze_obj = NULL;
+ zval *self = ZEND_THIS;
+ ze_zip_object *ze_obj;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|l", &filename, &flags) == FAILURE) {
return;
}
- if (self) {
- /* We do not use ZIP_FROM_OBJECT, zip init function here */
- ze_obj = Z_ZIP_P(self);
- }
+ /* We do not use ZIP_FROM_OBJECT, zip init function here */
+ ze_obj = Z_ZIP_P(self);
if (ZSTR_LEN(filename) == 0) {
php_error_docref(NULL, E_WARNING, "Empty string as source");
@@ -1497,14 +1489,10 @@ Set the password for the active archive */
static ZIPARCHIVE_METHOD(setPassword)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
char *password;
size_t password_len;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &password, &password_len) == FAILURE) {
@@ -1529,21 +1517,17 @@ close the zip archive */
static ZIPARCHIVE_METHOD(close)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
ze_zip_object *ze_obj;
int err;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
ze_obj = Z_ZIP_P(self);
if ((err = zip_close(intern))) {
#if LIBZIP_VERSION_MAJOR == 1 && LIBZIP_VERSION_MINOR == 3 && LIBZIP_VERSION_MICRO == 1
- php_error_docref(NULL, E_WARNING, "%s", "zip_close have failed");
+ php_error_docref(NULL, E_WARNING, "zip_close have failed");
#else
php_error_docref(NULL, E_WARNING, "%s", zip_strerror(intern));
zip_discard(intern);
@@ -1568,15 +1552,13 @@ close the zip archive */
static ZIPARCHIVE_METHOD(count)
{
struct zip *intern;
- zval *self = getThis();
-
- if (!self) {
- RETURN_FALSE;
- }
+ zval *self = ZEND_THIS;
+ zip_int64_t num;
ZIP_FROM_OBJECT(intern, self);
- RETVAL_LONG(zip_get_num_files(intern));
+ num = zip_get_num_entries(intern, 0);
+ RETVAL_LONG(MIN(num, ZEND_LONG_MAX));
}
/* }}} */
@@ -1585,7 +1567,7 @@ static ZIPARCHIVE_METHOD(count)
static ZIPARCHIVE_METHOD(getStatusString)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
#if LIBZIP_VERSION_MAJOR < 1
int zep, syp, len;
char error_string[128];
@@ -1593,10 +1575,6 @@ static ZIPARCHIVE_METHOD(getStatusString)
zip_error_t *err;
#endif
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
#if LIBZIP_VERSION_MAJOR < 1
@@ -1617,17 +1595,13 @@ Returns the index of the entry named filename in the archive */
static ZIPARCHIVE_METHOD(addEmptyDir)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
char *dirname;
size_t dirname_len;
int idx;
struct zip_stat sb;
char *s;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
@@ -1652,11 +1626,12 @@ static ZIPARCHIVE_METHOD(addEmptyDir)
if (idx >= 0) {
RETVAL_FALSE;
} else {
- if (zip_add_dir(intern, (const char *)s) == -1) {
+ if (zip_dir_add(intern, (const char *)s, 0) == -1) {
RETVAL_FALSE;
+ } else {
+ zip_error_clear(intern);
+ RETVAL_TRUE;
}
- zip_error_clear(intern);
- RETVAL_TRUE;
}
if (s != dirname) {
@@ -1668,7 +1643,7 @@ static ZIPARCHIVE_METHOD(addEmptyDir)
static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
char *path = ".";
char *remove_path = NULL, *save_remove_path;
char *add_path = NULL;
@@ -1679,10 +1654,6 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
int found;
zend_string *pattern;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
/* 1 == glob, 2 == pcre */
if (type == 1) {
@@ -1797,16 +1768,12 @@ Add a file in a Zip archive using its path and the name to use. */
static ZIPARCHIVE_METHOD(addFile)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
char *entry_name = NULL;
size_t entry_name_len = 0;
zend_long offset_start = 0, offset_len = 0;
zend_string *filename;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|sll",
@@ -1837,7 +1804,7 @@ Add a file using content and the entry name */
static ZIPARCHIVE_METHOD(addFromString)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
zend_string *buffer;
char *name;
size_t name_len;
@@ -1846,10 +1813,6 @@ static ZIPARCHIVE_METHOD(addFromString)
int pos = 0;
int cur_idx;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sS",
@@ -1884,7 +1847,7 @@ static ZIPARCHIVE_METHOD(addFromString)
}
}
- if (zip_add(intern, name, zs) == -1) {
+ if (zip_file_add(intern, name, zs, 0) == -1) {
zip_source_free(zs);
RETURN_FALSE;
} else {
@@ -1899,15 +1862,11 @@ Returns the information about a the zip entry filename */
static ZIPARCHIVE_METHOD(statName)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
zend_long flags = 0;
struct zip_stat sb;
zend_string *name;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|l", &name, &flags) == FAILURE) {
@@ -1921,19 +1880,15 @@ static ZIPARCHIVE_METHOD(statName)
/* }}} */
/* {{{ proto resource ZipArchive::statIndex(int index[, int flags])
-Returns the zip entry informations using its index */
+Returns the zip entry information using its index */
static ZIPARCHIVE_METHOD(statIndex)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
zend_long index, flags = 0;
struct zip_stat sb;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l",
@@ -1953,15 +1908,11 @@ Returns the index of the entry named filename in the archive */
static ZIPARCHIVE_METHOD(locateName)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
zend_long flags = 0;
zend_long idx = -1;
zend_string *name;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|l", &name, &flags) == FAILURE) {
@@ -1987,14 +1938,10 @@ Returns the name of the file at position index */
static ZIPARCHIVE_METHOD(getNameIndex)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
const char *name;
zend_long flags = 0, index = 0;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l",
@@ -2017,20 +1964,22 @@ Set or remove (NULL/'') the comment of the archive */
static ZIPARCHIVE_METHOD(setArchiveComment)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
size_t comment_len;
char * comment;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &comment, &comment_len) == FAILURE) {
return;
}
- if (zip_set_archive_comment(intern, (const char *)comment, (int)comment_len)) {
+
+ if (comment_len > 0xffff) {
+ php_error_docref(NULL, E_WARNING, "Comment must not exceed 65535 bytes");
+ RETURN_FALSE;
+ }
+
+ if (zip_set_archive_comment(intern, (const char *)comment, comment_len)) {
RETURN_FALSE;
} else {
RETURN_TRUE;
@@ -2043,15 +1992,11 @@ Returns the comment of an entry using its index */
static ZIPARCHIVE_METHOD(getArchiveComment)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
zend_long flags = 0;
const char * comment;
int comment_len = 0;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &flags) == FAILURE) {
@@ -2071,15 +2016,11 @@ Set or remove (NULL/'') the comment of an entry using its Name */
static ZIPARCHIVE_METHOD(setCommentName)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
size_t comment_len, name_len;
char * comment, *name;
int idx;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss",
@@ -2091,6 +2032,11 @@ static ZIPARCHIVE_METHOD(setCommentName)
php_error_docref(NULL, E_NOTICE, "Empty string as entry name");
}
+ if (comment_len > 0xffff) {
+ php_error_docref(NULL, E_WARNING, "Comment must not exceed 65535 bytes");
+ RETURN_FALSE;
+ }
+
idx = zip_name_locate(intern, name, 0);
if (idx < 0) {
RETURN_FALSE;
@@ -2104,16 +2050,12 @@ Set or remove (NULL/'') the comment of an entry using its index */
static ZIPARCHIVE_METHOD(setCommentIndex)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
zend_long index;
size_t comment_len;
char * comment;
struct zip_stat sb;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls",
@@ -2121,6 +2063,11 @@ static ZIPARCHIVE_METHOD(setCommentIndex)
return;
}
+ if (comment_len > 0xffff) {
+ php_error_docref(NULL, E_WARNING, "Comment must not exceed 65535 bytes");
+ RETURN_FALSE;
+ }
+
PHP_ZIP_STAT_INDEX(intern, index, 0, sb);
PHP_ZIP_SET_FILE_COMMENT(intern, index, comment, comment_len);
}
@@ -2134,16 +2081,12 @@ Set external attributes for file in zip, using its name */
static ZIPARCHIVE_METHOD(setExternalAttributesName)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
size_t name_len;
char *name;
zend_long flags=0, opsys, attr;
zip_int64_t idx;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sll|l",
@@ -2172,14 +2115,10 @@ Set external attributes for file in zip, using its index */
static ZIPARCHIVE_METHOD(setExternalAttributesIndex)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
zend_long index, flags=0, opsys, attr;
struct zip_stat sb;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll|l",
@@ -2201,7 +2140,7 @@ Get external attributes for file in zip, using its name */
static ZIPARCHIVE_METHOD(getExternalAttributesName)
{
struct zip *intern;
- zval *self = getThis(), *z_opsys, *z_attr;
+ zval *self = ZEND_THIS, *z_opsys, *z_attr;
size_t name_len;
char *name;
zend_long flags=0;
@@ -2209,13 +2148,9 @@ static ZIPARCHIVE_METHOD(getExternalAttributesName)
zip_uint32_t attr;
zip_int64_t idx;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz/z/|l",
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "szz|l",
&name, &name_len, &z_opsys, &z_attr, &flags) == FAILURE) {
return;
}
@@ -2232,10 +2167,8 @@ static ZIPARCHIVE_METHOD(getExternalAttributesName)
(zip_flags_t)flags, &opsys, &attr) < 0) {
RETURN_FALSE;
}
- zval_ptr_dtor(z_opsys);
- ZVAL_LONG(z_opsys, opsys);
- zval_ptr_dtor(z_attr);
- ZVAL_LONG(z_attr, attr);
+ ZEND_TRY_ASSIGN_REF_LONG(z_opsys, opsys);
+ ZEND_TRY_ASSIGN_REF_LONG(z_attr, attr);
RETURN_TRUE;
}
/* }}} */
@@ -2245,19 +2178,15 @@ Get external attributes for file in zip, using its index */
static ZIPARCHIVE_METHOD(getExternalAttributesIndex)
{
struct zip *intern;
- zval *self = getThis(), *z_opsys, *z_attr;
+ zval *self = ZEND_THIS, *z_opsys, *z_attr;
zend_long index, flags=0;
zip_uint8_t opsys;
zip_uint32_t attr;
struct zip_stat sb;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz/z/|l",
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "lzz|l",
&index, &z_opsys, &z_attr, &flags) == FAILURE) {
return;
}
@@ -2267,10 +2196,8 @@ static ZIPARCHIVE_METHOD(getExternalAttributesIndex)
(zip_flags_t)flags, &opsys, &attr) < 0) {
RETURN_FALSE;
}
- zval_ptr_dtor(z_opsys);
- ZVAL_LONG(z_opsys, opsys);
- zval_ptr_dtor(z_attr);
- ZVAL_LONG(z_attr, attr);
+ ZEND_TRY_ASSIGN_REF_LONG(z_opsys, opsys);
+ ZEND_TRY_ASSIGN_REF_LONG(z_attr, attr);
RETURN_TRUE;
}
/* }}} */
@@ -2282,16 +2209,12 @@ Set encryption method for file in zip, using its name */
static ZIPARCHIVE_METHOD(setEncryptionName)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
zend_long method;
zip_int64_t idx;
char *name, *password = NULL;
size_t name_len, password_len;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl|s",
@@ -2320,15 +2243,11 @@ Set encryption method for file in zip, using its index */
static ZIPARCHIVE_METHOD(setEncryptionIndex)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
zend_long index, method;
char *password = NULL;
size_t password_len;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll|s",
@@ -2349,18 +2268,14 @@ Returns the comment of an entry using its name */
static ZIPARCHIVE_METHOD(getCommentName)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
size_t name_len;
int idx;
zend_long flags = 0;
- int comment_len = 0;
+ zip_uint32_t comment_len = 0;
const char * comment;
char *name;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l",
@@ -2377,8 +2292,8 @@ static ZIPARCHIVE_METHOD(getCommentName)
RETURN_FALSE;
}
- comment = zip_get_file_comment(intern, idx, &comment_len, (int)flags);
- RETURN_STRINGL((char *)comment, (zend_long)comment_len);
+ comment = zip_file_get_comment(intern, idx, &comment_len, (zip_flags_t)flags);
+ RETURN_STRINGL((char *)comment, comment_len);
}
/* }}} */
@@ -2387,16 +2302,12 @@ Returns the comment of an entry using its index */
static ZIPARCHIVE_METHOD(getCommentIndex)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
zend_long index, flags = 0;
const char * comment;
- int comment_len = 0;
+ zip_uint32_t comment_len = 0;
struct zip_stat sb;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l",
@@ -2405,8 +2316,8 @@ static ZIPARCHIVE_METHOD(getCommentIndex)
}
PHP_ZIP_STAT_INDEX(intern, index, 0, sb);
- comment = zip_get_file_comment(intern, index, &comment_len, (int)flags);
- RETURN_STRINGL((char *)comment, (zend_long)comment_len);
+ comment = zip_file_get_comment(intern, index, &comment_len, (zip_flags_t)flags);
+ RETURN_STRINGL((char *)comment, comment_len);
}
/* }}} */
@@ -2415,16 +2326,12 @@ Set the compression of a file in zip, using its name */
static ZIPARCHIVE_METHOD(setCompressionName)
{
struct zip *intern;
- zval *this = getThis();
+ zval *this = ZEND_THIS;
size_t name_len;
char *name;
zip_int64_t idx;
zend_long comp_method, comp_flags = 0;
- if (!this) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, this);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl|l",
@@ -2454,14 +2361,10 @@ Set the compression of a file in zip, using its index */
static ZIPARCHIVE_METHOD(setCompressionIndex)
{
struct zip *intern;
- zval *this = getThis();
+ zval *this = ZEND_THIS;
zend_long index;
zend_long comp_method, comp_flags = 0;
- if (!this) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, this);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll|l",
@@ -2482,13 +2385,9 @@ Delete a file using its index */
static ZIPARCHIVE_METHOD(deleteIndex)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
zend_long index;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
@@ -2512,15 +2411,11 @@ Delete a file using its index */
static ZIPARCHIVE_METHOD(deleteName)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
size_t name_len;
char *name;
struct zip_stat sb;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
@@ -2543,15 +2438,11 @@ Rename an entry selected by its index to new_name */
static ZIPARCHIVE_METHOD(renameIndex)
{
struct zip *intern;
- zval *self = getThis();
-
+ zval *self = ZEND_THIS;
char *new_name;
size_t new_name_len;
zend_long index;
- if (!self) {
- RETURN_FALSE;
- }
ZIP_FROM_OBJECT(intern, self);
@@ -2567,7 +2458,7 @@ static ZIPARCHIVE_METHOD(renameIndex)
php_error_docref(NULL, E_NOTICE, "Empty string as new entry name");
RETURN_FALSE;
}
- if (zip_rename(intern, index, (const char *)new_name) != 0) {
+ if (zip_file_rename(intern, index, (const char *)new_name, 0) != 0) {
RETURN_FALSE;
}
RETURN_TRUE;
@@ -2579,15 +2470,11 @@ Rename an entry selected by its name to new_name */
static ZIPARCHIVE_METHOD(renameName)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
struct zip_stat sb;
char *name, *new_name;
size_t name_len, new_name_len;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &name, &name_len, &new_name, &new_name_len) == FAILURE) {
@@ -2601,7 +2488,7 @@ static ZIPARCHIVE_METHOD(renameName)
PHP_ZIP_STAT_PATH(intern, name, name_len, 0, sb);
- if (zip_rename(intern, sb.index, (const char *)new_name)) {
+ if (zip_file_rename(intern, sb.index, (const char *)new_name, 0)) {
RETURN_FALSE;
}
RETURN_TRUE;
@@ -2613,13 +2500,9 @@ Changes to the file at position index are reverted */
static ZIPARCHIVE_METHOD(unchangeIndex)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
zend_long index;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
@@ -2643,15 +2526,11 @@ Changes to the file named 'name' are reverted */
static ZIPARCHIVE_METHOD(unchangeName)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
struct zip_stat sb;
char *name;
size_t name_len;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
@@ -2677,11 +2556,7 @@ All changes to files and global information in archive are reverted */
static ZIPARCHIVE_METHOD(unchangeAll)
{
struct zip *intern;
- zval *self = getThis();
-
- if (!self) {
- RETURN_FALSE;
- }
+ zval *self = ZEND_THIS;
ZIP_FROM_OBJECT(intern, self);
@@ -2698,11 +2573,7 @@ Revert all global changes to the archive archive. For now, this only reverts ar
static ZIPARCHIVE_METHOD(unchangeArchive)
{
struct zip *intern;
- zval *self = getThis();
-
- if (!self) {
- RETURN_FALSE;
- }
+ zval *self = ZEND_THIS;
ZIP_FROM_OBJECT(intern, self);
@@ -2717,7 +2588,7 @@ static ZIPARCHIVE_METHOD(unchangeArchive)
/* {{{ proto bool ZipArchive::extractTo(string pathto[, mixed files])
Extract one or more file from a zip archive */
/* TODO:
- * - allow index or array of indeces
+ * - allow index or array of indices
* - replace path
* - patterns
*/
@@ -2725,19 +2596,13 @@ static ZIPARCHIVE_METHOD(extractTo)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
zval *zval_files = NULL;
zval *zval_file = NULL;
php_stream_statbuf ssb;
char *pathto;
size_t pathto_len;
- int ret, i;
-
- int nelems;
-
- if (!self) {
- RETURN_FALSE;
- }
+ int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|z", &pathto, &pathto_len, &zval_files) == FAILURE) {
return;
@@ -2756,6 +2621,8 @@ static ZIPARCHIVE_METHOD(extractTo)
ZIP_FROM_OBJECT(intern, self);
if (zval_files && (Z_TYPE_P(zval_files) != IS_NULL)) {
+ uint32_t nelems, i;
+
switch (Z_TYPE_P(zval_files)) {
case IS_STRING:
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_files), Z_STRLEN_P(zval_files))) {
@@ -2788,7 +2655,7 @@ static ZIPARCHIVE_METHOD(extractTo)
}
} else {
/* Extract all files */
- int filecount = zip_get_num_files(intern);
+ zip_int64_t i, filecount = zip_get_num_entries(intern, 0);
if (filecount == -1) {
php_error_docref(NULL, E_WARNING, "Illegal archive");
@@ -2809,7 +2676,7 @@ static ZIPARCHIVE_METHOD(extractTo)
static void php_zip_get_from(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
struct zip_stat sb;
struct zip_file *zf;
@@ -2823,10 +2690,6 @@ static void php_zip_get_from(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
int n = 0;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (type == 1) {
@@ -2893,17 +2756,13 @@ get a stream for an entry using its name */
static ZIPARCHIVE_METHOD(getStream)
{
struct zip *intern;
- zval *self = getThis();
+ zval *self = ZEND_THIS;
struct zip_stat sb;
char *mode = "rb";
zend_string *filename;
php_stream *stream;
ze_zip_object *obj;
- if (!self) {
- RETURN_FALSE;
- }
-
ZIP_FROM_OBJECT(intern, self);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &filename) == FAILURE) {
@@ -3180,16 +3039,15 @@ static PHP_MINIT_FUNCTION(zip)
php_zip_register_prop_handler(&zip_prop_handlers, "numFiles", php_zip_get_num_files, NULL, NULL, IS_LONG);
php_zip_register_prop_handler(&zip_prop_handlers, "filename", NULL, NULL, php_zipobj_get_filename, IS_STRING);
php_zip_register_prop_handler(&zip_prop_handlers, "comment", NULL, php_zipobj_get_zip_comment, NULL, IS_STRING);
-#if PHP_VERSION_ID >= 70200
zend_class_implements(zip_class_entry, 1, zend_ce_countable);
-#elif defined(HAVE_SPL)
- zend_class_implements(zip_class_entry, 1, spl_ce_Countable);
-#endif
REGISTER_ZIP_CLASS_CONST_LONG("CREATE", ZIP_CREATE);
REGISTER_ZIP_CLASS_CONST_LONG("EXCL", ZIP_EXCL);
REGISTER_ZIP_CLASS_CONST_LONG("CHECKCONS", ZIP_CHECKCONS);
REGISTER_ZIP_CLASS_CONST_LONG("OVERWRITE", ZIP_OVERWRITE);
+#ifdef ZIP_RDONLY
+ REGISTER_ZIP_CLASS_CONST_LONG("RDONLY", ZIP_RDONLY);
+#endif
REGISTER_ZIP_CLASS_CONST_LONG("FL_NOCASE", ZIP_FL_NOCASE);
REGISTER_ZIP_CLASS_CONST_LONG("FL_NODIR", ZIP_FL_NODIR);
@@ -3226,6 +3084,12 @@ static PHP_MINIT_FUNCTION(zip)
REGISTER_ZIP_CLASS_CONST_LONG("CM_PKWARE_IMPLODE", ZIP_CM_PKWARE_IMPLODE);
REGISTER_ZIP_CLASS_CONST_LONG("CM_BZIP2", ZIP_CM_BZIP2);
REGISTER_ZIP_CLASS_CONST_LONG("CM_LZMA", ZIP_CM_LZMA);
+#ifdef ZIP_CM_LZMA2
+ REGISTER_ZIP_CLASS_CONST_LONG("CM_LZMA2", ZIP_CM_LZMA2);
+#endif
+#ifdef ZIP_CM_XZ
+ REGISTER_ZIP_CLASS_CONST_LONG("CM_XZ", ZIP_CM_XZ);
+#endif
REGISTER_ZIP_CLASS_CONST_LONG("CM_TERSE", ZIP_CM_TERSE);
REGISTER_ZIP_CLASS_CONST_LONG("CM_LZ77", ZIP_CM_LZ77);
REGISTER_ZIP_CLASS_CONST_LONG("CM_WAVPACK", ZIP_CM_WAVPACK);
@@ -3256,6 +3120,27 @@ static PHP_MINIT_FUNCTION(zip)
REGISTER_ZIP_CLASS_CONST_LONG("ER_INCONS", ZIP_ER_INCONS); /* N Zip archive inconsistent */
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 */
+ REGISTER_ZIP_CLASS_CONST_LONG("ER_ENCRNOTSUPP", ZIP_ER_ENCRNOTSUPP);/* N Encryption method not supported */
+ REGISTER_ZIP_CLASS_CONST_LONG("ER_RDONLY", ZIP_ER_RDONLY); /* N Read-only archive */
+ REGISTER_ZIP_CLASS_CONST_LONG("ER_NOPASSWD", ZIP_ER_NOPASSWD); /* N Entry has been deleted */
+ REGISTER_ZIP_CLASS_CONST_LONG("ER_WRONGPASSWD", ZIP_ER_WRONGPASSWD);/* N Wrong password provided */
+/* since 1.0.0 */
+#ifdef ZIP_ER_OPNOTSUPP
+ REGISTER_ZIP_CLASS_CONST_LONG("ER_OPNOTSUPP", ZIP_ER_OPNOTSUPP); /* N Operation not supported */
+#endif
+#ifdef ZIP_ER_INUSE
+ REGISTER_ZIP_CLASS_CONST_LONG("ER_INUSE", ZIP_ER_INUSE); /* N Resource still in use */
+#endif
+#ifdef ZIP_ER_TELL
+ REGISTER_ZIP_CLASS_CONST_LONG("ER_TELL", ZIP_ER_TELL); /* S Tell error */
+#endif
+/* since 1.6.0 */
+#ifdef ZIP_ER_COMPRESSED_DATA
+ REGISTER_ZIP_CLASS_CONST_LONG("ER_COMPRESSED_DATA", ZIP_ER_COMPRESSED_DATA);/* N Compressed data invalid */
+#endif
+#ifdef ZIP_ER_CANCELLED
+ REGISTER_ZIP_CLASS_CONST_LONG("ER_CANCELLED", ZIP_ER_CANCELLED); /* N Operation cancelled */
+#endif
#ifdef ZIP_OPSYS_DEFAULT
REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_DOS", ZIP_OPSYS_DOS);
@@ -3290,6 +3175,12 @@ static PHP_MINIT_FUNCTION(zip)
REGISTER_ZIP_CLASS_CONST_LONG("EM_AES_256", ZIP_EM_AES_256);
#endif
+#if HAVE_LIBZIP_VERSION
+ zend_declare_class_constant_string(zip_class_entry, "LIBZIP_VERSION", sizeof("LIBZIP_VERSION")-1, zip_libzip_version());
+#else
+ zend_declare_class_constant_string(zip_class_entry, "LIBZIP_VERSION", sizeof("LIBZIP_VERSION")-1, LIBZIP_VERSION);
+#endif
+
php_register_url_stream_wrapper("zip", &php_stream_zip_wrapper);
le_zip_dir = zend_register_list_destructors_ex(php_zip_free_dir, NULL, le_zip_dir_name, module_number);
@@ -3327,12 +3218,3 @@ static PHP_MINFO_FUNCTION(zip)
php_info_print_table_end();
}
/* }}} */
-
-/*
- * Local variables:
- * tab-width: 4
- * c-basic-offset: 4
- * End:
- * vim600: noet sw=4 ts=4 fdm=marker
- * vim<600: noet sw=4 ts=4
- */