Skip to content

Commit ab61a5c

Browse files
committed
ext/zip: further micro optimisations.
- forgotten remaining libzip < 1.0.0 code path. - earlier return on invalid inputs from some ZipArchive methods.
1 parent 9300a50 commit ab61a5c

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

ext/zip/php_zip.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,6 +2417,15 @@ PHP_METHOD(ZipArchive, setCompressionIndex)
24172417
RETURN_THROWS();
24182418
}
24192419

2420+
if (index < 0) {
2421+
RETURN_FALSE;
2422+
}
2423+
2424+
if (comp_flags < 0 || comp_flags > USHRT_MAX) {
2425+
// comp_flags is cast down accordingly in libzip, zip_entry_t compression_level is of zip_uint16_t
2426+
RETURN_FALSE;
2427+
}
2428+
24202429
ZIP_FROM_OBJECT(intern, this);
24212430

24222431
RETURN_BOOL(zip_set_file_compression(intern, (zip_uint64_t)index,
@@ -2440,13 +2449,13 @@ PHP_METHOD(ZipArchive, setMtimeName)
24402449
RETURN_THROWS();
24412450
}
24422451

2443-
ZIP_FROM_OBJECT(intern, this);
2444-
24452452
if (name_len == 0) {
24462453
zend_argument_must_not_be_empty_error(1);
24472454
RETURN_THROWS();
24482455
}
24492456

2457+
ZIP_FROM_OBJECT(intern, this);
2458+
24502459
idx = zip_name_locate(intern, name, 0);
24512460

24522461
if (idx < 0) {
@@ -2513,17 +2522,15 @@ PHP_METHOD(ZipArchive, deleteName)
25132522
RETURN_THROWS();
25142523
}
25152524

2516-
ZIP_FROM_OBJECT(intern, self);
2517-
25182525
if (name_len < 1) {
25192526
RETURN_FALSE;
25202527
}
25212528

2529+
ZIP_FROM_OBJECT(intern, self);
2530+
25222531
PHP_ZIP_STAT_PATH(intern, name, name_len, 0, sb);
2523-
if (zip_delete(intern, sb.index)) {
2524-
RETURN_FALSE;
2525-
}
2526-
RETURN_TRUE;
2532+
2533+
RETURN_BOOL(zip_delete(intern, sb.index) == 0);
25272534
}
25282535
/* }}} */
25292536

@@ -2544,13 +2551,13 @@ PHP_METHOD(ZipArchive, renameIndex)
25442551
RETURN_FALSE;
25452552
}
25462553

2547-
ZIP_FROM_OBJECT(intern, self);
2548-
25492554
if (new_name_len == 0) {
25502555
zend_argument_must_not_be_empty_error(2);
25512556
RETURN_THROWS();
25522557
}
25532558

2559+
ZIP_FROM_OBJECT(intern, self);
2560+
25542561
RETURN_BOOL(zip_file_rename(intern, index, (const char *)new_name, 0) == 0);
25552562
}
25562563
/* }}} */
@@ -2592,12 +2599,12 @@ PHP_METHOD(ZipArchive, unchangeIndex)
25922599
RETURN_THROWS();
25932600
}
25942601

2595-
ZIP_FROM_OBJECT(intern, self);
2596-
25972602
if (index < 0) {
25982603
RETURN_FALSE;
25992604
}
26002605

2606+
ZIP_FROM_OBJECT(intern, self);
2607+
26012608
RETURN_BOOL(zip_unchange(intern, index) == 0);
26022609
}
26032610
/* }}} */
@@ -2615,12 +2622,12 @@ PHP_METHOD(ZipArchive, unchangeName)
26152622
RETURN_THROWS();
26162623
}
26172624

2618-
ZIP_FROM_OBJECT(intern, self);
2619-
26202625
if (name_len < 1) {
26212626
RETURN_FALSE;
26222627
}
26232628

2629+
ZIP_FROM_OBJECT(intern, self);
2630+
26242631
PHP_ZIP_STAT_PATH(intern, name, name_len, 0, sb);
26252632

26262633
RETURN_BOOL(zip_unchange(intern, sb.index) == 0);
@@ -2684,8 +2691,6 @@ PHP_METHOD(ZipArchive, extractTo)
26842691
Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(files_ht, files_str)
26852692
ZEND_PARSE_PARAMETERS_END();
26862693

2687-
ZIP_FROM_OBJECT(intern, self);
2688-
26892694
if (pathto_len < 1) {
26902695
RETURN_FALSE;
26912696
}
@@ -2698,6 +2703,7 @@ PHP_METHOD(ZipArchive, extractTo)
26982703
}
26992704

27002705
uint32_t nelems, i;
2706+
ZIP_FROM_OBJECT(intern, self);
27012707

27022708
if (files_str) {
27032709
if (!php_zip_extract_file(intern, pathto, ZSTR_VAL(files_str), ZSTR_LEN(files_str), -1)) {
@@ -2794,7 +2800,7 @@ static void php_zip_get_from(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
27942800
RETURN_FALSE;
27952801
}
27962802

2797-
buffer = zend_string_safe_alloc(1, len, 0, 0);
2803+
buffer = zend_string_safe_alloc(1, len, 0, false);
27982804
n = zip_fread(zf, ZSTR_VAL(buffer), ZSTR_LEN(buffer));
27992805
if (n < 1) {
28002806
zend_string_efree(buffer);
@@ -3003,6 +3009,9 @@ PHP_METHOD(ZipArchive, isCompressionMethodSupported)
30033009
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &method, &enc) == FAILURE) {
30043010
return;
30053011
}
3012+
if (method < 0) {
3013+
RETURN_FALSE;
3014+
}
30063015
RETVAL_BOOL(zip_compression_method_supported((zip_int32_t)method, enc));
30073016
}
30083017
/* }}} */
@@ -3016,6 +3025,9 @@ PHP_METHOD(ZipArchive, isEncryptionMethodSupported)
30163025
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &method, &enc) == FAILURE) {
30173026
return;
30183027
}
3028+
if (method < 0) {
3029+
RETURN_FALSE;
3030+
}
30193031
RETVAL_BOOL(zip_encryption_method_supported((zip_uint16_t)method, enc));
30203032
}
30213033
/* }}} */

ext/zip/zip_stream.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,11 @@ static ssize_t php_zip_ops_read(php_stream *stream, char *buf, size_t count)
4949
if (self->zf) {
5050
n = zip_fread(self->zf, buf, count);
5151
if (n < 0) {
52-
#if LIBZIP_VERSION_MAJOR < 1
53-
int ze, se;
54-
zip_file_error_get(self->zf, &ze, &se);
55-
stream->eof = 1;
56-
php_error_docref(NULL, E_WARNING, "Zip stream error: %s", zip_file_strerror(self->zf));
57-
#else
5852
zip_error_t *err;
5953
err = zip_file_get_error(self->zf);
6054
stream->eof = 1;
6155
php_error_docref(NULL, E_WARNING, "Zip stream error: %s", zip_error_strerror(err));
6256
zip_error_fini(err);
63-
#endif
6457
return -1;
6558
}
6659
/* cast count to signed value to avoid possibly negative n
@@ -157,7 +150,7 @@ static int php_zip_ops_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{
157150
fragment++;
158151

159152
if (ZIP_OPENBASEDIR_CHECKPATH(file_dirname)) {
160-
zend_string_release_ex(file_basename, 0);
153+
zend_string_release_ex(file_basename, false);
161154
return -1;
162155
}
163156

@@ -166,7 +159,7 @@ static int php_zip_ops_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{
166159
memset(ssb, 0, sizeof(php_stream_statbuf));
167160
if (zip_stat(za, fragment, ZIP_FL_NOCASE, &sb) != 0) {
168161
zip_close(za);
169-
zend_string_release_ex(file_basename, 0);
162+
zend_string_release_ex(file_basename, false);
170163
return -1;
171164
}
172165
zip_close(za);
@@ -190,7 +183,7 @@ static int php_zip_ops_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{
190183
#endif
191184
ssb->sb.st_ino = -1;
192185
}
193-
zend_string_release_ex(file_basename, 0);
186+
zend_string_release_ex(file_basename, false);
194187
return 0;
195188
}
196189
/* }}} */
@@ -319,7 +312,7 @@ php_stream *php_stream_zip_opener(php_stream_wrapper *wrapper,
319312
fragment++;
320313

321314
if (ZIP_OPENBASEDIR_CHECKPATH(file_dirname)) {
322-
zend_string_release_ex(file_basename, 0);
315+
zend_string_release_ex(file_basename, false);
323316
return NULL;
324317
}
325318

@@ -351,14 +344,14 @@ php_stream *php_stream_zip_opener(php_stream_wrapper *wrapper,
351344
}
352345

353346
if (opened_path) {
354-
*opened_path = zend_string_init(path, strlen(path), 0);
347+
*opened_path = zend_string_init(path, path_len, false);
355348
}
356349
} else {
357350
zip_close(za);
358351
}
359352
}
360353

361-
zend_string_release_ex(file_basename, 0);
354+
zend_string_release_ex(file_basename, false);
362355

363356
if (!stream) {
364357
return NULL;

0 commit comments

Comments
 (0)