Skip to content

Commit fa1f5f1

Browse files
committed
adding a wrapper for zip_file_set_encryption workaround.
1 parent 44f4c65 commit fa1f5f1

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

ext/zip/php_zip.c

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ static int le_zip_entry;
7474
# define add_ascii_assoc_string add_assoc_string
7575
# define add_ascii_assoc_long add_assoc_long
7676

77+
static bool php_zip_file_set_encryption(struct zip *intern, zend_long index, zend_long method, char *password) {
78+
// FIXME: is a workaround to reset/free the password in case of consecutive calls.
79+
// when libzip 1.11.5 is available, we can save this call in this case.
80+
if (UNEXPECTED(zip_file_set_encryption(intern, (zip_uint64_t)index, ZIP_EM_NONE, NULL) < 0)) {
81+
php_error_docref(NULL, E_WARNING, "password reset failed");
82+
return false;
83+
}
84+
85+
return (zip_file_set_encryption(intern, (zip_uint64_t)index, (zip_uint16_t)method, password) == 0);
86+
}
87+
7788
/* Flatten a path by making a relative path (to .)*/
7889
static char * php_zip_make_relative_path(char *path, size_t path_len) /* {{{ */
7990
{
@@ -1756,12 +1767,7 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
17561767
}
17571768
#ifdef HAVE_ENCRYPTION
17581769
if (opts.enc_method >= 0) {
1759-
if (UNEXPECTED(zip_file_set_encryption(ze_obj->za, ze_obj->last_id, ZIP_EM_NONE, NULL) < 0)) {
1760-
zend_array_destroy(Z_ARR_P(return_value));
1761-
php_error_docref(NULL, E_WARNING, "password reset failed");
1762-
RETURN_FALSE;
1763-
}
1764-
if (zip_file_set_encryption(ze_obj->za, ze_obj->last_id, opts.enc_method, opts.enc_password)) {
1770+
if (php_zip_file_set_encryption(ze_obj->za, ze_obj->last_id, opts.enc_method, opts.enc_password)) {
17651771
zend_array_destroy(Z_ARR_P(return_value));
17661772
RETURN_FALSE;
17671773
}
@@ -2284,15 +2290,7 @@ PHP_METHOD(ZipArchive, setEncryptionName)
22842290
RETURN_FALSE;
22852291
}
22862292

2287-
if (UNEXPECTED(zip_file_set_encryption(intern, idx, ZIP_EM_NONE, NULL) < 0)) {
2288-
php_error_docref(NULL, E_WARNING, "password reset failed");
2289-
RETURN_FALSE;
2290-
}
2291-
2292-
if (zip_file_set_encryption(intern, idx, (zip_uint16_t)method, password)) {
2293-
RETURN_FALSE;
2294-
}
2295-
RETURN_TRUE;
2293+
RETURN_BOOL(php_zip_file_set_encryption(intern, idx, method, password));
22962294
}
22972295
/* }}} */
22982296

@@ -2312,12 +2310,7 @@ PHP_METHOD(ZipArchive, setEncryptionIndex)
23122310

23132311
ZIP_FROM_OBJECT(intern, self);
23142312

2315-
if (UNEXPECTED(zip_file_set_encryption(intern, index, ZIP_EM_NONE, NULL) < 0)) {
2316-
php_error_docref(NULL, E_WARNING, "password reset failed");
2317-
RETURN_FALSE;
2318-
}
2319-
2320-
RETURN_BOOL(zip_file_set_encryption(intern, index, (zip_uint16_t)method, password) == 0);
2313+
RETURN_BOOL(php_zip_file_set_encryption(intern, index, method, password));
23212314
}
23222315
/* }}} */
23232316
#endif

0 commit comments

Comments
 (0)