Skip to content

Commit 1e4ac3a

Browse files
committed
use index to avoid search by name
1 parent b84c0f1 commit 1e4ac3a

File tree

6 files changed

+78
-42
lines changed

6 files changed

+78
-42
lines changed

php5/php_zip.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static char * php_zip_make_relative_path(char *path, int path_len) /* {{{ */
154154
#endif
155155

156156
/* {{{ php_zip_extract_file */
157-
static int php_zip_extract_file(struct zip * za, char *dest, char *file, int file_len TSRMLS_DC)
157+
static int php_zip_extract_file(struct zip * za, char *dest, const char *file, int file_len, zip_int64_t idx TSRMLS_DC)
158158
{
159159
php_stream_statbuf ssb;
160160
struct zip_file *zf;
@@ -173,6 +173,12 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil
173173
size_t path_cleaned_len;
174174
cwd_state new_state;
175175

176+
if (idx <0) {
177+
idx = zip_name_locate(za, file, 0);
178+
if (idx < 0) {
179+
return 0;
180+
}
181+
}
176182
new_state.cwd = CWD_STATE_ALLOC(1);
177183
new_state.cwd[0] = '\0';
178184
new_state.cwd_length = 0;
@@ -188,7 +194,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil
188194
}
189195
path_cleaned_len = strlen(path_cleaned);
190196

191-
if (path_cleaned_len >= MAXPATHLEN || zip_stat(za, file, 0, &sb) != 0) {
197+
if (path_cleaned_len >= MAXPATHLEN || zip_stat_index(za, idx, 0, &sb) != 0) {
192198
CWD_STATE_FREE(new_state.cwd);
193199
return 0;
194200
}
@@ -264,7 +270,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil
264270
return 0;
265271
}
266272

267-
zf = zip_fopen(za, file, 0);
273+
zf = zip_fopen_index(za, idx, 0);
268274
if (zf == NULL) {
269275
n = -1;
270276
goto done;
@@ -3164,7 +3170,7 @@ static ZIPARCHIVE_METHOD(extractTo)
31643170

31653171
switch (Z_TYPE_P(zval_files)) {
31663172
case IS_STRING:
3167-
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_files), Z_STRLEN_P(zval_files) TSRMLS_CC)) {
3173+
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_files), Z_STRLEN_P(zval_files), -1 TSRMLS_CC)) {
31683174
RETURN_FALSE;
31693175
}
31703176
break;
@@ -3179,7 +3185,7 @@ static ZIPARCHIVE_METHOD(extractTo)
31793185
case IS_LONG:
31803186
break;
31813187
case IS_STRING:
3182-
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_PP(zval_file), Z_STRLEN_PP(zval_file) TSRMLS_CC)) {
3188+
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_PP(zval_file), Z_STRLEN_PP(zval_file), -1 TSRMLS_CC)) {
31833189
RETURN_FALSE;
31843190
}
31853191
break;
@@ -3202,8 +3208,8 @@ static ZIPARCHIVE_METHOD(extractTo)
32023208
}
32033209

32043210
for (i = 0; i < filecount; i++) {
3205-
char *file = (char*)zip_get_name(intern, i, ZIP_FL_UNCHANGED);
3206-
if (!file || !php_zip_extract_file(intern, pathto, file, strlen(file) TSRMLS_CC)) {
3211+
const char *file = zip_get_name(intern, i, ZIP_FL_UNCHANGED);
3212+
if (!file || !php_zip_extract_file(intern, pathto, file, strlen(file), i TSRMLS_CC)) {
32073213
RETURN_FALSE;
32083214
}
32093215
}

php7/php_zip.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static char * php_zip_make_relative_path(char *path, size_t path_len) /* {{{ */
137137
# define CWD_STATE_FREE(s) efree(s)
138138

139139
/* {{{ php_zip_extract_file */
140-
static int php_zip_extract_file(struct zip * za, char *dest, char *file, int file_len)
140+
static int php_zip_extract_file(struct zip * za, char *dest, const char *file, size_t file_len, zip_int64_t idx)
141141
{
142142
php_stream_statbuf ssb;
143143
struct zip_file *zf;
@@ -155,6 +155,12 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil
155155
cwd_state new_state;
156156
zend_string *file_basename;
157157

158+
if (idx <0) {
159+
idx = zip_name_locate(za, file, 0);
160+
if (idx < 0) {
161+
return 0;
162+
}
163+
}
158164
new_state.cwd = CWD_STATE_ALLOC(1);
159165
new_state.cwd[0] = '\0';
160166
new_state.cwd_length = 0;
@@ -170,7 +176,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil
170176
}
171177
path_cleaned_len = strlen(path_cleaned);
172178

173-
if (path_cleaned_len >= MAXPATHLEN || zip_stat(za, file, 0, &sb) != 0) {
179+
if (path_cleaned_len >= MAXPATHLEN || zip_stat_index(za, idx, 0, &sb) != 0) {
174180
CWD_STATE_FREE(new_state.cwd);
175181
return 0;
176182
}
@@ -245,7 +251,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil
245251
return 0;
246252
}
247253

248-
zf = zip_fopen(za, file, 0);
254+
zf = zip_fopen_index(za, idx, 0);
249255
if (zf == NULL) {
250256
n = -1;
251257
goto done;
@@ -3062,7 +3068,7 @@ static ZIPARCHIVE_METHOD(extractTo)
30623068

30633069
switch (Z_TYPE_P(zval_files)) {
30643070
case IS_STRING:
3065-
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_files), Z_STRLEN_P(zval_files))) {
3071+
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_files), Z_STRLEN_P(zval_files), -1)) {
30663072
RETURN_FALSE;
30673073
}
30683074
break;
@@ -3079,7 +3085,7 @@ static ZIPARCHIVE_METHOD(extractTo)
30793085
case IS_LONG:
30803086
break;
30813087
case IS_STRING:
3082-
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_file), Z_STRLEN_P(zval_file))) {
3088+
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_file), Z_STRLEN_P(zval_file), -1)) {
30833089
RETURN_FALSE;
30843090
}
30853091
break;
@@ -3102,8 +3108,8 @@ static ZIPARCHIVE_METHOD(extractTo)
31023108
}
31033109

31043110
for (i = 0; i < filecount; i++) {
3105-
char *file = (char*)zip_get_name(intern, i, ZIP_FL_UNCHANGED);
3106-
if (!file || !php_zip_extract_file(intern, pathto, file, strlen(file))) {
3111+
const char *file = zip_get_name(intern, i, ZIP_FL_UNCHANGED);
3112+
if (!file || !php_zip_extract_file(intern, pathto, file, strlen(file), i)) {
31073113
RETURN_FALSE;
31083114
}
31093115
}

php73/php_zip.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static char * php_zip_make_relative_path(char *path, size_t path_len) /* {{{ */
137137
# define CWD_STATE_FREE(s) efree(s)
138138

139139
/* {{{ php_zip_extract_file */
140-
static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t file_len)
140+
static int php_zip_extract_file(struct zip * za, char *dest, const char *file, size_t file_len, zip_int64_t idx)
141141
{
142142
php_stream_statbuf ssb;
143143
struct zip_file *zf;
@@ -155,6 +155,12 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
155155
cwd_state new_state;
156156
zend_string *file_basename;
157157

158+
if (idx <0) {
159+
idx = zip_name_locate(za, file, 0);
160+
if (idx < 0) {
161+
return 0;
162+
}
163+
}
158164
new_state.cwd = CWD_STATE_ALLOC(1);
159165
new_state.cwd[0] = '\0';
160166
new_state.cwd_length = 0;
@@ -170,7 +176,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
170176
}
171177
path_cleaned_len = strlen(path_cleaned);
172178

173-
if (path_cleaned_len >= MAXPATHLEN || zip_stat(za, file, 0, &sb) != 0) {
179+
if (path_cleaned_len >= MAXPATHLEN || zip_stat_index(za, idx, 0, &sb) != 0) {
174180
CWD_STATE_FREE(new_state.cwd);
175181
return 0;
176182
}
@@ -245,7 +251,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
245251
return 0;
246252
}
247253

248-
zf = zip_fopen(za, file, 0);
254+
zf = zip_fopen_index(za, idx, 0);
249255
if (zf == NULL) {
250256
n = -1;
251257
goto done;
@@ -3065,7 +3071,7 @@ static ZIPARCHIVE_METHOD(extractTo)
30653071

30663072
switch (Z_TYPE_P(zval_files)) {
30673073
case IS_STRING:
3068-
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_files), Z_STRLEN_P(zval_files))) {
3074+
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_files), Z_STRLEN_P(zval_files), -1)) {
30693075
RETURN_FALSE;
30703076
}
30713077
break;
@@ -3081,7 +3087,7 @@ static ZIPARCHIVE_METHOD(extractTo)
30813087
case IS_LONG:
30823088
break;
30833089
case IS_STRING:
3084-
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_file), Z_STRLEN_P(zval_file))) {
3090+
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_file), Z_STRLEN_P(zval_file), -1)) {
30853091
RETURN_FALSE;
30863092
}
30873093
break;
@@ -3104,8 +3110,8 @@ static ZIPARCHIVE_METHOD(extractTo)
31043110
}
31053111

31063112
for (i = 0; i < filecount; i++) {
3107-
char *file = (char*)zip_get_name(intern, i, ZIP_FL_UNCHANGED);
3108-
if (!file || !php_zip_extract_file(intern, pathto, file, strlen(file))) {
3113+
const char *file = zip_get_name(intern, i, ZIP_FL_UNCHANGED);
3114+
if (!file || !php_zip_extract_file(intern, pathto, file, strlen(file), i)) {
31093115
RETURN_FALSE;
31103116
}
31113117
}

php74/php_zip.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static char * php_zip_make_relative_path(char *path, size_t path_len) /* {{{ */
133133
# define CWD_STATE_FREE(s) efree(s)
134134

135135
/* {{{ php_zip_extract_file */
136-
static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t file_len)
136+
static int php_zip_extract_file(struct zip * za, char *dest, const char *file, size_t file_len, zip_int64_t idx)
137137
{
138138
php_stream_statbuf ssb;
139139
struct zip_file *zf;
@@ -151,6 +151,12 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
151151
cwd_state new_state;
152152
zend_string *file_basename;
153153

154+
if (idx <0) {
155+
idx = zip_name_locate(za, file, 0);
156+
if (idx < 0) {
157+
return 0;
158+
}
159+
}
154160
new_state.cwd = CWD_STATE_ALLOC(1);
155161
new_state.cwd[0] = '\0';
156162
new_state.cwd_length = 0;
@@ -166,7 +172,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
166172
}
167173
path_cleaned_len = strlen(path_cleaned);
168174

169-
if (path_cleaned_len >= MAXPATHLEN || zip_stat(za, file, 0, &sb) != 0) {
175+
if (path_cleaned_len >= MAXPATHLEN || zip_stat_index(za, idx, 0, &sb) != 0) {
170176
CWD_STATE_FREE(new_state.cwd);
171177
return 0;
172178
}
@@ -241,7 +247,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
241247
return 0;
242248
}
243249

244-
zf = zip_fopen(za, file, 0);
250+
zf = zip_fopen_index(za, idx, 0);
245251
if (zf == NULL) {
246252
n = -1;
247253
goto done;
@@ -2922,7 +2928,7 @@ static ZIPARCHIVE_METHOD(extractTo)
29222928

29232929
switch (Z_TYPE_P(zval_files)) {
29242930
case IS_STRING:
2925-
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_files), Z_STRLEN_P(zval_files))) {
2931+
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_files), Z_STRLEN_P(zval_files), -1)) {
29262932
RETURN_FALSE;
29272933
}
29282934
break;
@@ -2938,7 +2944,7 @@ static ZIPARCHIVE_METHOD(extractTo)
29382944
case IS_LONG:
29392945
break;
29402946
case IS_STRING:
2941-
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_file), Z_STRLEN_P(zval_file))) {
2947+
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_file), Z_STRLEN_P(zval_file), -1)) {
29422948
RETURN_FALSE;
29432949
}
29442950
break;
@@ -2961,8 +2967,8 @@ static ZIPARCHIVE_METHOD(extractTo)
29612967
}
29622968

29632969
for (i = 0; i < filecount; i++) {
2964-
char *file = (char*)zip_get_name(intern, i, ZIP_FL_UNCHANGED);
2965-
if (!file || !php_zip_extract_file(intern, pathto, file, strlen(file))) {
2970+
const char *file = zip_get_name(intern, i, ZIP_FL_UNCHANGED);
2971+
if (!file || !php_zip_extract_file(intern, pathto, file, strlen(file), i)) {
29662972
RETURN_FALSE;
29672973
}
29682974
}

php8/php_zip.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static char * php_zip_make_relative_path(char *path, size_t path_len) /* {{{ */
121121
# define CWD_STATE_FREE(s) efree(s)
122122

123123
/* {{{ php_zip_extract_file */
124-
static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t file_len)
124+
static int php_zip_extract_file(struct zip * za, char *dest, const char *file, size_t file_len, zip_int64_t idx)
125125
{
126126
php_stream_statbuf ssb;
127127
struct zip_file *zf;
@@ -139,6 +139,12 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
139139
cwd_state new_state;
140140
zend_string *file_basename;
141141

142+
if (idx <0) {
143+
idx = zip_name_locate(za, file, 0);
144+
if (idx < 0) {
145+
return 0;
146+
}
147+
}
142148
new_state.cwd = CWD_STATE_ALLOC(1);
143149
new_state.cwd[0] = '\0';
144150
new_state.cwd_length = 0;
@@ -154,7 +160,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
154160
}
155161
path_cleaned_len = strlen(path_cleaned);
156162

157-
if (path_cleaned_len >= MAXPATHLEN || zip_stat(za, file, 0, &sb) != 0) {
163+
if (path_cleaned_len >= MAXPATHLEN || zip_stat_index(za, idx, 0, &sb) != 0) {
158164
CWD_STATE_FREE(new_state.cwd);
159165
return 0;
160166
}
@@ -229,7 +235,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
229235
return 0;
230236
}
231237

232-
zf = zip_fopen(za, file, 0);
238+
zf = zip_fopen_index(za, idx, 0);
233239
if (zf == NULL) {
234240
n = -1;
235241
goto done;
@@ -2815,7 +2821,7 @@ PHP_METHOD(ZipArchive, extractTo)
28152821
uint32_t nelems, i;
28162822

28172823
if (files_str) {
2818-
if (!php_zip_extract_file(intern, pathto, ZSTR_VAL(files_str), ZSTR_LEN(files_str))) {
2824+
if (!php_zip_extract_file(intern, pathto, ZSTR_VAL(files_str), ZSTR_LEN(files_str), -1)) {
28192825
RETURN_FALSE;
28202826
}
28212827
} else if (files_ht) {
@@ -2830,7 +2836,7 @@ PHP_METHOD(ZipArchive, extractTo)
28302836
case IS_LONG:
28312837
break;
28322838
case IS_STRING:
2833-
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_file), Z_STRLEN_P(zval_file))) {
2839+
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_file), Z_STRLEN_P(zval_file), -1)) {
28342840
RETURN_FALSE;
28352841
}
28362842
break;
@@ -2847,8 +2853,8 @@ PHP_METHOD(ZipArchive, extractTo)
28472853
}
28482854

28492855
for (i = 0; i < filecount; i++) {
2850-
char *file = (char*)zip_get_name(intern, i, ZIP_FL_UNCHANGED);
2851-
if (!file || !php_zip_extract_file(intern, pathto, file, strlen(file))) {
2856+
const char *file = zip_get_name(intern, i, ZIP_FL_UNCHANGED);
2857+
if (!file || !php_zip_extract_file(intern, pathto, file, strlen(file), i)) {
28522858
RETURN_FALSE;
28532859
}
28542860
}

0 commit comments

Comments
 (0)