1 | /* |
2 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
3 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
4 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
5 | * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
6 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
7 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
8 | * OTHER DEALINGS IN THE SOFTWARE. |
9 | */ |
10 | #define __STDC_WANT_LIB_EXT1__ 1 |
11 | |
12 | #include <errno.h> |
13 | #include <sys/stat.h> |
14 | #include <time.h> |
15 | |
16 | #if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \ |
17 | defined(__MINGW32__) |
18 | /* Win32, DOS, MSVC, MSVS */ |
19 | #include <direct.h> |
20 | |
21 | #define MKDIR(DIRNAME) _mkdir(DIRNAME) |
22 | #define STRCLONE(STR) ((STR) ? _strdup(STR) : NULL) |
23 | #define HAS_DEVICE(P) \ |
24 | ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) && \ |
25 | (P)[1] == ':') |
26 | #define FILESYSTEM_PREFIX_LEN(P) (HAS_DEVICE(P) ? 2 : 0) |
27 | |
28 | #else |
29 | |
30 | #include <unistd.h> // needed for symlink() on BSD |
31 | int symlink(const char *target, const char *linkpath); // needed on Linux |
32 | |
33 | #define MKDIR(DIRNAME) mkdir(DIRNAME, 0755) |
34 | #define STRCLONE(STR) ((STR) ? strdup(STR) : NULL) |
35 | |
36 | #endif |
37 | |
38 | #ifdef __MINGW32__ |
39 | #include <sys/types.h> |
40 | #include <unistd.h> |
41 | #endif |
42 | |
43 | #ifdef _MSC_VER |
44 | #define ftruncate(fd, sz) (-(_chsize_s((fd), (__int64)(sz)) != 0)) |
45 | #endif |
46 | |
47 | #include "miniz.h" |
48 | #include "zip.h" |
49 | |
50 | #ifndef HAS_DEVICE |
51 | #define HAS_DEVICE(P) 0 |
52 | #endif |
53 | |
54 | #ifndef FILESYSTEM_PREFIX_LEN |
55 | #define FILESYSTEM_PREFIX_LEN(P) 0 |
56 | #endif |
57 | |
58 | #ifndef ISSLASH |
59 | #define ISSLASH(C) ((C) == '/' || (C) == '\\') |
60 | #endif |
61 | |
62 | #define CLEANUP(ptr) \ |
63 | do { \ |
64 | if (ptr) { \ |
65 | free((void *)ptr); \ |
66 | ptr = NULL; \ |
67 | } \ |
68 | } while (0) |
69 | |
70 | static int file_truncate(mz_zip_archive *pZip) { |
71 | mz_zip_internal_state *pState = pZip->m_pState; |
72 | mz_uint64 file_size = pZip->m_archive_size; |
73 | |
74 | if (pZip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED) { |
75 | if (pState->m_pFile) { |
76 | int fd = fileno(pState->m_pFile); |
77 | return ftruncate(fd, file_size); |
78 | } |
79 | } |
80 | return 0; |
81 | } |
82 | |
83 | static const char *base_name(const char *name) { |
84 | char const *p; |
85 | char const *base = name += FILESYSTEM_PREFIX_LEN(name); |
86 | int all_slashes = 1; |
87 | |
88 | for (p = name; *p; p++) { |
89 | if (ISSLASH(*p)) |
90 | base = p + 1; |
91 | else |
92 | all_slashes = 0; |
93 | } |
94 | |
95 | /* If NAME is all slashes, arrange to return `/'. */ |
96 | if (*base == '\0' && ISSLASH(*name) && all_slashes) |
97 | --base; |
98 | |
99 | return base; |
100 | } |
101 | |
102 | static int mkpath(char *path) { |
103 | char *p; |
104 | char npath[MAX_PATH + 1]; |
105 | int len = 0; |
106 | int has_device = HAS_DEVICE(path); |
107 | |
108 | memset(npath, 0, MAX_PATH + 1); |
109 | if (has_device) { |
110 | // only on windows |
111 | npath[0] = path[0]; |
112 | npath[1] = path[1]; |
113 | len = 2; |
114 | } |
115 | for (p = path + len; *p && len < MAX_PATH; p++) { |
116 | if (ISSLASH(*p) && ((!has_device && len > 0) || (has_device && len > 2))) { |
117 | #if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \ |
118 | defined(__MINGW32__) |
119 | #else |
120 | if ('\\' == *p) { |
121 | *p = '/'; |
122 | } |
123 | #endif |
124 | |
125 | if (MKDIR(npath) == -1) { |
126 | if (errno != EEXIST) { |
127 | return -1; |
128 | } |
129 | } |
130 | } |
131 | npath[len++] = *p; |
132 | } |
133 | |
134 | return 0; |
135 | } |
136 | |
137 | static char *strrpl(const char *str, size_t n, char oldchar, char newchar) { |
138 | char c; |
139 | size_t i; |
140 | char *rpl = (char *)calloc((1 + n), sizeof(char)); |
141 | char *begin = rpl; |
142 | if (!rpl) { |
143 | return NULL; |
144 | } |
145 | |
146 | for (i = 0; (i < n) && (c = *str++); ++i) { |
147 | if (c == oldchar) { |
148 | c = newchar; |
149 | } |
150 | *rpl++ = c; |
151 | } |
152 | |
153 | return begin; |
154 | } |
155 | |
156 | struct zip_entry_t { |
157 | int index; |
158 | char *name; |
159 | mz_uint64 uncomp_size; |
160 | mz_uint64 comp_size; |
161 | mz_uint32 uncomp_crc32; |
162 | mz_uint64 offset; |
163 | mz_uint8 [MZ_ZIP_LOCAL_DIR_HEADER_SIZE]; |
164 | mz_uint64 ; |
165 | mz_uint16 method; |
166 | mz_zip_writer_add_state state; |
167 | tdefl_compressor comp; |
168 | mz_uint32 external_attr; |
169 | time_t m_time; |
170 | }; |
171 | |
172 | struct zip_t { |
173 | mz_zip_archive archive; |
174 | mz_uint level; |
175 | struct zip_entry_t entry; |
176 | }; |
177 | |
178 | struct zip_t *zip_open(const char *zipname, int level, char mode) { |
179 | struct zip_t *zip = NULL; |
180 | |
181 | if (!zipname || strlen(zipname) < 1) { |
182 | // zip_t archive name is empty or NULL |
183 | goto cleanup; |
184 | } |
185 | |
186 | if (level < 0) |
187 | level = MZ_DEFAULT_LEVEL; |
188 | if ((level & 0xF) > MZ_UBER_COMPRESSION) { |
189 | // Wrong compression level |
190 | goto cleanup; |
191 | } |
192 | |
193 | zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t)); |
194 | if (!zip) |
195 | goto cleanup; |
196 | |
197 | zip->level = (mz_uint)level; |
198 | switch (mode) { |
199 | case 'w': |
200 | // Create a new archive. |
201 | if (!mz_zip_writer_init_file(&(zip->archive), zipname, 0)) { |
202 | // Cannot initialize zip_archive writer |
203 | goto cleanup; |
204 | } |
205 | break; |
206 | |
207 | case 'r': |
208 | case 'a': |
209 | case 'd': |
210 | if (!mz_zip_reader_init_file( |
211 | &(zip->archive), zipname, |
212 | zip->level | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY)) { |
213 | // An archive file does not exist or cannot initialize |
214 | // zip_archive reader |
215 | goto cleanup; |
216 | } |
217 | if ((mode == 'a' || mode == 'd') && |
218 | !mz_zip_writer_init_from_reader(&(zip->archive), zipname)) { |
219 | mz_zip_reader_end(&(zip->archive)); |
220 | goto cleanup; |
221 | } |
222 | break; |
223 | |
224 | default: |
225 | goto cleanup; |
226 | } |
227 | |
228 | return zip; |
229 | |
230 | cleanup: |
231 | CLEANUP(zip); |
232 | return NULL; |
233 | } |
234 | |
235 | void zip_close(struct zip_t *zip) { |
236 | if (zip) { |
237 | // Always finalize, even if adding failed for some reason, so we have a |
238 | // valid central directory. |
239 | mz_zip_writer_finalize_archive(&(zip->archive)); |
240 | file_truncate(&(zip->archive)); |
241 | mz_zip_writer_end(&(zip->archive)); |
242 | mz_zip_reader_end(&(zip->archive)); |
243 | |
244 | CLEANUP(zip); |
245 | } |
246 | } |
247 | |
248 | int zip_is64(struct zip_t *zip) { |
249 | if (!zip) { |
250 | // zip_t handler is not initialized |
251 | return -1; |
252 | } |
253 | |
254 | if (!zip->archive.m_pState) { |
255 | // zip state is not initialized |
256 | return -1; |
257 | } |
258 | |
259 | return (int)zip->archive.m_pState->m_zip64; |
260 | } |
261 | |
262 | int zip_entry_open(struct zip_t *zip, const char *entryname) { |
263 | size_t entrylen = 0; |
264 | mz_zip_archive *pzip = NULL; |
265 | mz_uint num_alignment_padding_bytes, level; |
266 | mz_zip_archive_file_stat stats; |
267 | |
268 | if (!zip || !entryname) { |
269 | return -1; |
270 | } |
271 | |
272 | entrylen = strlen(entryname); |
273 | if (entrylen < 1) { |
274 | return -1; |
275 | } |
276 | |
277 | /* |
278 | .ZIP File Format Specification Version: 6.3.3 |
279 | |
280 | 4.4.17.1 The name of the file, with optional relative path. |
281 | The path stored MUST not contain a drive or |
282 | device letter, or a leading slash. All slashes |
283 | MUST be forward slashes '/' as opposed to |
284 | backwards slashes '\' for compatibility with Amiga |
285 | and UNIX file systems etc. If input came from standard |
286 | input, there is no file name field. |
287 | */ |
288 | if (zip->entry.name) { |
289 | CLEANUP(zip->entry.name); |
290 | } |
291 | zip->entry.name = strrpl(entryname, entrylen, '\\', '/'); |
292 | if (!zip->entry.name) { |
293 | // Cannot parse zip entry name |
294 | return -1; |
295 | } |
296 | |
297 | pzip = &(zip->archive); |
298 | if (pzip->m_zip_mode == MZ_ZIP_MODE_READING) { |
299 | zip->entry.index = |
300 | mz_zip_reader_locate_file(pzip, zip->entry.name, NULL, 0); |
301 | if (zip->entry.index < 0) { |
302 | goto cleanup; |
303 | } |
304 | |
305 | if (!mz_zip_reader_file_stat(pzip, (mz_uint)zip->entry.index, &stats)) { |
306 | goto cleanup; |
307 | } |
308 | |
309 | zip->entry.comp_size = stats.m_comp_size; |
310 | zip->entry.uncomp_size = stats.m_uncomp_size; |
311 | zip->entry.uncomp_crc32 = stats.m_crc32; |
312 | zip->entry.offset = stats.m_central_dir_ofs; |
313 | zip->entry.header_offset = stats.m_local_header_ofs; |
314 | zip->entry.method = stats.m_method; |
315 | zip->entry.external_attr = stats.m_external_attr; |
316 | #ifndef MINIZ_NO_TIME |
317 | zip->entry.m_time = stats.m_time; |
318 | #endif |
319 | |
320 | return 0; |
321 | } |
322 | |
323 | zip->entry.index = (int)zip->archive.m_total_files; |
324 | zip->entry.comp_size = 0; |
325 | zip->entry.uncomp_size = 0; |
326 | zip->entry.uncomp_crc32 = MZ_CRC32_INIT; |
327 | zip->entry.offset = zip->archive.m_archive_size; |
328 | zip->entry.header_offset = zip->archive.m_archive_size; |
329 | memset(zip->entry.header, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE * sizeof(mz_uint8)); |
330 | zip->entry.method = 0; |
331 | |
332 | // UNIX or APPLE |
333 | #if MZ_PLATFORM == 3 || MZ_PLATFORM == 19 |
334 | // regular file with rw-r--r-- persmissions |
335 | zip->entry.external_attr = (mz_uint32)(0100644) << 16; |
336 | #else |
337 | zip->entry.external_attr = 0; |
338 | #endif |
339 | |
340 | num_alignment_padding_bytes = |
341 | mz_zip_writer_compute_padding_needed_for_file_alignment(pzip); |
342 | |
343 | if (!pzip->m_pState || (pzip->m_zip_mode != MZ_ZIP_MODE_WRITING)) { |
344 | // Wrong zip mode |
345 | goto cleanup; |
346 | } |
347 | if (zip->level & MZ_ZIP_FLAG_COMPRESSED_DATA) { |
348 | // Wrong zip compression level |
349 | goto cleanup; |
350 | } |
351 | // no zip64 support yet |
352 | if ((pzip->m_total_files == 0xFFFF) || |
353 | ((pzip->m_archive_size + num_alignment_padding_bytes + |
354 | MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + |
355 | entrylen) > 0xFFFFFFFF)) { |
356 | // No zip64 support yet |
357 | goto cleanup; |
358 | } |
359 | if (!mz_zip_writer_write_zeros(pzip, zip->entry.offset, |
360 | num_alignment_padding_bytes + |
361 | sizeof(zip->entry.header))) { |
362 | // Cannot memset zip entry header |
363 | goto cleanup; |
364 | } |
365 | |
366 | zip->entry.header_offset += num_alignment_padding_bytes; |
367 | if (pzip->m_file_offset_alignment) { |
368 | MZ_ASSERT( |
369 | (zip->entry.header_offset & (pzip->m_file_offset_alignment - 1)) == 0); |
370 | } |
371 | zip->entry.offset += num_alignment_padding_bytes + sizeof(zip->entry.header); |
372 | |
373 | if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, zip->entry.name, |
374 | entrylen) != entrylen) { |
375 | // Cannot write data to zip entry |
376 | goto cleanup; |
377 | } |
378 | |
379 | zip->entry.offset += entrylen; |
380 | level = zip->level & 0xF; |
381 | if (level) { |
382 | zip->entry.state.m_pZip = pzip; |
383 | zip->entry.state.m_cur_archive_file_ofs = zip->entry.offset; |
384 | zip->entry.state.m_comp_size = 0; |
385 | |
386 | if (tdefl_init(&(zip->entry.comp), mz_zip_writer_add_put_buf_callback, |
387 | &(zip->entry.state), |
388 | (int)tdefl_create_comp_flags_from_zip_params( |
389 | (int)level, -15, MZ_DEFAULT_STRATEGY)) != |
390 | TDEFL_STATUS_OKAY) { |
391 | // Cannot initialize the zip compressor |
392 | goto cleanup; |
393 | } |
394 | } |
395 | |
396 | zip->entry.m_time = time(NULL); |
397 | |
398 | return 0; |
399 | |
400 | cleanup: |
401 | CLEANUP(zip->entry.name); |
402 | return -1; |
403 | } |
404 | |
405 | int zip_entry_openbyindex(struct zip_t *zip, int index) { |
406 | mz_zip_archive *pZip = NULL; |
407 | mz_zip_archive_file_stat stats; |
408 | mz_uint namelen; |
409 | const mz_uint8 *; |
410 | const char *pFilename; |
411 | |
412 | if (!zip) { |
413 | // zip_t handler is not initialized |
414 | return -1; |
415 | } |
416 | |
417 | pZip = &(zip->archive); |
418 | if (pZip->m_zip_mode != MZ_ZIP_MODE_READING) { |
419 | // open by index requires readonly mode |
420 | return -1; |
421 | } |
422 | |
423 | if (index < 0 || (mz_uint)index >= pZip->m_total_files) { |
424 | // index out of range |
425 | return -1; |
426 | } |
427 | |
428 | if (!(pHeader = &MZ_ZIP_ARRAY_ELEMENT( |
429 | &pZip->m_pState->m_central_dir, mz_uint8, |
430 | MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, |
431 | mz_uint32, index)))) { |
432 | // cannot find header in central directory |
433 | return -1; |
434 | } |
435 | |
436 | namelen = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS); |
437 | pFilename = (const char *)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; |
438 | |
439 | /* |
440 | .ZIP File Format Specification Version: 6.3.3 |
441 | |
442 | 4.4.17.1 The name of the file, with optional relative path. |
443 | The path stored MUST not contain a drive or |
444 | device letter, or a leading slash. All slashes |
445 | MUST be forward slashes '/' as opposed to |
446 | backwards slashes '\' for compatibility with Amiga |
447 | and UNIX file systems etc. If input came from standard |
448 | input, there is no file name field. |
449 | */ |
450 | if (zip->entry.name) { |
451 | CLEANUP(zip->entry.name); |
452 | } |
453 | zip->entry.name = strrpl(pFilename, namelen, '\\', '/'); |
454 | if (!zip->entry.name) { |
455 | // local entry name is NULL |
456 | return -1; |
457 | } |
458 | |
459 | if (!mz_zip_reader_file_stat(pZip, (mz_uint)index, &stats)) { |
460 | return -1; |
461 | } |
462 | |
463 | zip->entry.index = index; |
464 | zip->entry.comp_size = stats.m_comp_size; |
465 | zip->entry.uncomp_size = stats.m_uncomp_size; |
466 | zip->entry.uncomp_crc32 = stats.m_crc32; |
467 | zip->entry.offset = stats.m_central_dir_ofs; |
468 | zip->entry.header_offset = stats.m_local_header_ofs; |
469 | zip->entry.method = stats.m_method; |
470 | zip->entry.external_attr = stats.m_external_attr; |
471 | #ifndef MINIZ_NO_TIME |
472 | zip->entry.m_time = stats.m_time; |
473 | #endif |
474 | |
475 | return 0; |
476 | } |
477 | |
478 | int zip_entry_close(struct zip_t *zip) { |
479 | mz_zip_archive *pzip = NULL; |
480 | mz_uint level; |
481 | tdefl_status done; |
482 | mz_uint16 entrylen; |
483 | mz_uint16 dos_time = 0, dos_date = 0; |
484 | int status = -1; |
485 | |
486 | if (!zip) { |
487 | // zip_t handler is not initialized |
488 | goto cleanup; |
489 | } |
490 | |
491 | pzip = &(zip->archive); |
492 | if (pzip->m_zip_mode == MZ_ZIP_MODE_READING) { |
493 | status = 0; |
494 | goto cleanup; |
495 | } |
496 | |
497 | level = zip->level & 0xF; |
498 | if (level) { |
499 | done = tdefl_compress_buffer(&(zip->entry.comp), "" , 0, TDEFL_FINISH); |
500 | if (done != TDEFL_STATUS_DONE && done != TDEFL_STATUS_OKAY) { |
501 | // Cannot flush compressed buffer |
502 | goto cleanup; |
503 | } |
504 | zip->entry.comp_size = zip->entry.state.m_comp_size; |
505 | zip->entry.offset = zip->entry.state.m_cur_archive_file_ofs; |
506 | zip->entry.method = MZ_DEFLATED; |
507 | } |
508 | |
509 | entrylen = (mz_uint16)strlen(zip->entry.name); |
510 | // no zip64 support yet |
511 | if ((zip->entry.comp_size > 0xFFFFFFFF) || (zip->entry.offset > 0xFFFFFFFF)) { |
512 | // No zip64 support, yet |
513 | goto cleanup; |
514 | } |
515 | |
516 | #ifndef MINIZ_NO_TIME |
517 | mz_zip_time_t_to_dos_time(zip->entry.m_time, &dos_time, &dos_date); |
518 | #endif |
519 | |
520 | if (!mz_zip_writer_create_local_dir_header( |
521 | pzip, zip->entry.header, entrylen, 0, zip->entry.uncomp_size, |
522 | zip->entry.comp_size, zip->entry.uncomp_crc32, zip->entry.method, 0, |
523 | dos_time, dos_date)) { |
524 | // Cannot create zip entry header |
525 | goto cleanup; |
526 | } |
527 | |
528 | if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.header_offset, |
529 | zip->entry.header, |
530 | sizeof(zip->entry.header)) != sizeof(zip->entry.header)) { |
531 | // Cannot write zip entry header |
532 | goto cleanup; |
533 | } |
534 | |
535 | if (!mz_zip_writer_add_to_central_dir( |
536 | pzip, zip->entry.name, entrylen, NULL, 0, "" , 0, |
537 | zip->entry.uncomp_size, zip->entry.comp_size, zip->entry.uncomp_crc32, |
538 | zip->entry.method, 0, dos_time, dos_date, zip->entry.header_offset, |
539 | zip->entry.external_attr)) { |
540 | // Cannot write to zip central dir |
541 | goto cleanup; |
542 | } |
543 | |
544 | pzip->m_total_files++; |
545 | pzip->m_archive_size = zip->entry.offset; |
546 | status = 0; |
547 | |
548 | cleanup: |
549 | if (zip) { |
550 | zip->entry.m_time = 0; |
551 | CLEANUP(zip->entry.name); |
552 | } |
553 | return status; |
554 | } |
555 | |
556 | const char *zip_entry_name(struct zip_t *zip) { |
557 | if (!zip) { |
558 | // zip_t handler is not initialized |
559 | return NULL; |
560 | } |
561 | |
562 | return zip->entry.name; |
563 | } |
564 | |
565 | int zip_entry_index(struct zip_t *zip) { |
566 | if (!zip) { |
567 | // zip_t handler is not initialized |
568 | return -1; |
569 | } |
570 | |
571 | return zip->entry.index; |
572 | } |
573 | |
574 | int zip_entry_isdir(struct zip_t *zip) { |
575 | if (!zip) { |
576 | // zip_t handler is not initialized |
577 | return -1; |
578 | } |
579 | |
580 | if (zip->entry.index < 0) { |
581 | // zip entry is not opened |
582 | return -1; |
583 | } |
584 | |
585 | return (int)mz_zip_reader_is_file_a_directory(&zip->archive, |
586 | (mz_uint)zip->entry.index); |
587 | } |
588 | |
589 | unsigned long long zip_entry_size(struct zip_t *zip) { |
590 | return zip ? zip->entry.uncomp_size : 0; |
591 | } |
592 | |
593 | unsigned int zip_entry_crc32(struct zip_t *zip) { |
594 | return zip ? zip->entry.uncomp_crc32 : 0; |
595 | } |
596 | |
597 | int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize) { |
598 | mz_uint level; |
599 | mz_zip_archive *pzip = NULL; |
600 | tdefl_status status; |
601 | |
602 | if (!zip) { |
603 | // zip_t handler is not initialized |
604 | return -1; |
605 | } |
606 | |
607 | pzip = &(zip->archive); |
608 | if (buf && bufsize > 0) { |
609 | zip->entry.uncomp_size += bufsize; |
610 | zip->entry.uncomp_crc32 = (mz_uint32)mz_crc32( |
611 | zip->entry.uncomp_crc32, (const mz_uint8 *)buf, bufsize); |
612 | |
613 | level = zip->level & 0xF; |
614 | if (!level) { |
615 | if ((pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, buf, |
616 | bufsize) != bufsize)) { |
617 | // Cannot write buffer |
618 | return -1; |
619 | } |
620 | zip->entry.offset += bufsize; |
621 | zip->entry.comp_size += bufsize; |
622 | } else { |
623 | status = tdefl_compress_buffer(&(zip->entry.comp), buf, bufsize, |
624 | TDEFL_NO_FLUSH); |
625 | if (status != TDEFL_STATUS_DONE && status != TDEFL_STATUS_OKAY) { |
626 | // Cannot compress buffer |
627 | return -1; |
628 | } |
629 | } |
630 | } |
631 | |
632 | return 0; |
633 | } |
634 | |
635 | int zip_entry_fwrite(struct zip_t *zip, const char *filename) { |
636 | int status = 0; |
637 | size_t n = 0; |
638 | FILE *stream = NULL; |
639 | mz_uint8 buf[MZ_ZIP_MAX_IO_BUF_SIZE]; |
640 | struct MZ_FILE_STAT_STRUCT file_stat; |
641 | |
642 | if (!zip) { |
643 | // zip_t handler is not initialized |
644 | return -1; |
645 | } |
646 | |
647 | memset(buf, 0, MZ_ZIP_MAX_IO_BUF_SIZE); |
648 | memset((void *)&file_stat, 0, sizeof(struct MZ_FILE_STAT_STRUCT)); |
649 | if (MZ_FILE_STAT(filename, &file_stat) != 0) { |
650 | // problem getting information - check errno |
651 | return -1; |
652 | } |
653 | |
654 | if ((file_stat.st_mode & 0200) == 0) { |
655 | // MS-DOS read-only attribute |
656 | zip->entry.external_attr |= 0x01; |
657 | } |
658 | zip->entry.external_attr |= (mz_uint32)((file_stat.st_mode & 0xFFFF) << 16); |
659 | zip->entry.m_time = file_stat.st_mtime; |
660 | |
661 | #if defined(_MSC_VER) |
662 | if (fopen_s(&stream, filename, "rb" )) |
663 | #else |
664 | if (!(stream = fopen(filename, "rb" ))) |
665 | #endif |
666 | { |
667 | // Cannot open filename |
668 | return -1; |
669 | } |
670 | |
671 | while ((n = fread(buf, sizeof(mz_uint8), MZ_ZIP_MAX_IO_BUF_SIZE, stream)) > |
672 | 0) { |
673 | if (zip_entry_write(zip, buf, n) < 0) { |
674 | status = -1; |
675 | break; |
676 | } |
677 | } |
678 | fclose(stream); |
679 | |
680 | return status; |
681 | } |
682 | |
683 | ssize_t zip_entry_read(struct zip_t *zip, void **buf, size_t *bufsize) { |
684 | mz_zip_archive *pzip = NULL; |
685 | mz_uint idx; |
686 | size_t size = 0; |
687 | |
688 | if (!zip) { |
689 | // zip_t handler is not initialized |
690 | return -1; |
691 | } |
692 | |
693 | pzip = &(zip->archive); |
694 | if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) { |
695 | // the entry is not found or we do not have read access |
696 | return -1; |
697 | } |
698 | |
699 | idx = (mz_uint)zip->entry.index; |
700 | if (mz_zip_reader_is_file_a_directory(pzip, idx)) { |
701 | // the entry is a directory |
702 | return -1; |
703 | } |
704 | |
705 | *buf = mz_zip_reader_extract_to_heap(pzip, idx, &size, 0); |
706 | if (*buf && bufsize) { |
707 | *bufsize = size; |
708 | } |
709 | return size; |
710 | } |
711 | |
712 | ssize_t zip_entry_noallocread(struct zip_t *zip, void *buf, size_t bufsize) { |
713 | mz_zip_archive *pzip = NULL; |
714 | |
715 | if (!zip) { |
716 | // zip_t handler is not initialized |
717 | return -1; |
718 | } |
719 | |
720 | pzip = &(zip->archive); |
721 | if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) { |
722 | // the entry is not found or we do not have read access |
723 | return -1; |
724 | } |
725 | |
726 | if (!mz_zip_reader_extract_to_mem_no_alloc(pzip, (mz_uint)zip->entry.index, |
727 | buf, bufsize, 0, NULL, 0)) { |
728 | return -1; |
729 | } |
730 | |
731 | return (ssize_t)zip->entry.uncomp_size; |
732 | } |
733 | |
734 | int zip_entry_fread(struct zip_t *zip, const char *filename) { |
735 | mz_zip_archive *pzip = NULL; |
736 | mz_uint idx; |
737 | mz_uint32 xattr = 0; |
738 | mz_zip_archive_file_stat info; |
739 | |
740 | if (!zip) { |
741 | // zip_t handler is not initialized |
742 | return -1; |
743 | } |
744 | |
745 | memset((void *)&info, 0, sizeof(mz_zip_archive_file_stat)); |
746 | pzip = &(zip->archive); |
747 | if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) { |
748 | // the entry is not found or we do not have read access |
749 | return -1; |
750 | } |
751 | |
752 | idx = (mz_uint)zip->entry.index; |
753 | if (mz_zip_reader_is_file_a_directory(pzip, idx)) { |
754 | // the entry is a directory |
755 | return -1; |
756 | } |
757 | |
758 | if (!mz_zip_reader_extract_to_file(pzip, idx, filename, 0)) { |
759 | return -1; |
760 | } |
761 | |
762 | #if defined(_MSC_VER) |
763 | #else |
764 | if (!mz_zip_reader_file_stat(pzip, idx, &info)) { |
765 | // Cannot get information about zip archive; |
766 | return -1; |
767 | } |
768 | |
769 | xattr = (info.m_external_attr >> 16) & 0xFFFF; |
770 | if (xattr > 0) { |
771 | if (chmod(filename, (mode_t)xattr) < 0) { |
772 | return -1; |
773 | } |
774 | } |
775 | #endif |
776 | |
777 | return 0; |
778 | } |
779 | |
780 | int (struct zip_t *zip, |
781 | size_t (*)(void *arg, unsigned long long offset, |
782 | const void *buf, size_t bufsize), |
783 | void *arg) { |
784 | mz_zip_archive *pzip = NULL; |
785 | mz_uint idx; |
786 | |
787 | if (!zip) { |
788 | // zip_t handler is not initialized |
789 | return -1; |
790 | } |
791 | |
792 | pzip = &(zip->archive); |
793 | if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) { |
794 | // the entry is not found or we do not have read access |
795 | return -1; |
796 | } |
797 | |
798 | idx = (mz_uint)zip->entry.index; |
799 | return (mz_zip_reader_extract_to_callback(pzip, idx, on_extract, arg, 0)) |
800 | ? 0 |
801 | : -1; |
802 | } |
803 | |
804 | int zip_total_entries(struct zip_t *zip) { |
805 | if (!zip) { |
806 | // zip_t handler is not initialized |
807 | return -1; |
808 | } |
809 | |
810 | return (int)zip->archive.m_total_files; |
811 | } |
812 | |
813 | int zip_create(const char *zipname, const char *filenames[], size_t len) { |
814 | int status = 0; |
815 | size_t i; |
816 | mz_zip_archive zip_archive; |
817 | struct MZ_FILE_STAT_STRUCT file_stat; |
818 | mz_uint32 ext_attributes = 0; |
819 | |
820 | if (!zipname || strlen(zipname) < 1) { |
821 | // zip_t archive name is empty or NULL |
822 | return -1; |
823 | } |
824 | |
825 | // Create a new archive. |
826 | if (!memset(&(zip_archive), 0, sizeof(zip_archive))) { |
827 | // Cannot memset zip archive |
828 | return -1; |
829 | } |
830 | |
831 | if (!mz_zip_writer_init_file(&zip_archive, zipname, 0)) { |
832 | // Cannot initialize zip_archive writer |
833 | return -1; |
834 | } |
835 | |
836 | memset((void *)&file_stat, 0, sizeof(struct MZ_FILE_STAT_STRUCT)); |
837 | |
838 | for (i = 0; i < len; ++i) { |
839 | const char *name = filenames[i]; |
840 | if (!name) { |
841 | status = -1; |
842 | break; |
843 | } |
844 | |
845 | if (MZ_FILE_STAT(name, &file_stat) != 0) { |
846 | // problem getting information - check errno |
847 | status = -1; |
848 | break; |
849 | } |
850 | |
851 | if ((file_stat.st_mode & 0200) == 0) { |
852 | // MS-DOS read-only attribute |
853 | ext_attributes |= 0x01; |
854 | } |
855 | ext_attributes |= (mz_uint32)((file_stat.st_mode & 0xFFFF) << 16); |
856 | |
857 | if (!mz_zip_writer_add_file(&zip_archive, base_name(name), name, "" , 0, |
858 | ZIP_DEFAULT_COMPRESSION_LEVEL, |
859 | ext_attributes)) { |
860 | // Cannot add file to zip_archive |
861 | status = -1; |
862 | break; |
863 | } |
864 | } |
865 | |
866 | mz_zip_writer_finalize_archive(&zip_archive); |
867 | mz_zip_writer_end(&zip_archive); |
868 | return status; |
869 | } |
870 | |
871 | static char *normalize(char *name, char *const nname, size_t len) { |
872 | size_t offn = 0; |
873 | size_t offnn = 0, ncpy = 0; |
874 | |
875 | if (name == NULL || nname == NULL || len <= 0) { |
876 | return NULL; |
877 | } |
878 | // skip trailing '/' |
879 | while (ISSLASH(*name)) |
880 | name++; |
881 | |
882 | for (; offn < len; offn++) { |
883 | if (ISSLASH(name[offn])) { |
884 | if (ncpy > 0 && strncmp(&nname[offnn], "." , 1) && |
885 | strncmp(&nname[offnn], ".." , 2)) { |
886 | offnn += ncpy; |
887 | nname[offnn++] = name[offn]; // append '/' |
888 | } |
889 | ncpy = 0; |
890 | } else { |
891 | nname[offnn + ncpy] = name[offn]; |
892 | ncpy++; |
893 | } |
894 | } |
895 | |
896 | // at the end, extra check what we've already copied |
897 | if (ncpy == 0 || !strncmp(&nname[offnn], "." , 1) || |
898 | !strncmp(&nname[offnn], ".." , 2)) { |
899 | nname[offnn] = 0; |
900 | } |
901 | return nname; |
902 | } |
903 | |
904 | static int (mz_zip_archive *zip_archive, const char *dir, |
905 | int (*)(const char *filename, void *arg), |
906 | void *arg) { |
907 | int status = -1; |
908 | mz_uint i, n; |
909 | char path[MAX_PATH + 1]; |
910 | char symlink_to[MAX_PATH + 1]; |
911 | mz_zip_archive_file_stat info; |
912 | size_t dirlen = 0; |
913 | mz_uint32 xattr = 0; |
914 | |
915 | memset(path, 0, sizeof(path)); |
916 | memset(symlink_to, 0, sizeof(symlink_to)); |
917 | |
918 | dirlen = strlen(dir); |
919 | if (dirlen + 1 > MAX_PATH) { |
920 | return -1; |
921 | } |
922 | |
923 | memset((void *)&info, 0, sizeof(mz_zip_archive_file_stat)); |
924 | |
925 | #if defined(_MSC_VER) |
926 | strcpy_s(path, MAX_PATH, dir); |
927 | #else |
928 | strcpy(path, dir); |
929 | #endif |
930 | |
931 | if (!ISSLASH(path[dirlen - 1])) { |
932 | #if defined(_WIN32) || defined(__WIN32__) |
933 | path[dirlen] = '\\'; |
934 | #else |
935 | path[dirlen] = '/'; |
936 | #endif |
937 | ++dirlen; |
938 | } |
939 | |
940 | // Get and print information about each file in the archive. |
941 | n = mz_zip_reader_get_num_files(zip_archive); |
942 | for (i = 0; i < n; ++i) { |
943 | if (!mz_zip_reader_file_stat(zip_archive, i, &info)) { |
944 | // Cannot get information about zip archive; |
945 | goto out; |
946 | } |
947 | if (!normalize(info.m_filename, info.m_filename, strlen(info.m_filename))) { |
948 | // Cannot normalize file name; |
949 | goto out; |
950 | } |
951 | #if defined(_MSC_VER) |
952 | strncpy_s(&path[dirlen], MAX_PATH - dirlen, info.m_filename, |
953 | MAX_PATH - dirlen); |
954 | #else |
955 | strncpy(&path[dirlen], info.m_filename, MAX_PATH - dirlen); |
956 | #endif |
957 | if (mkpath(path) < 0) { |
958 | // Cannot make a path |
959 | goto out; |
960 | } |
961 | |
962 | if ((((info.m_version_made_by >> 8) == 3) || |
963 | ((info.m_version_made_by >> 8) == |
964 | 19)) // if zip is produced on Unix or macOS (3 and 19 from |
965 | // section 4.4.2.2 of zip standard) |
966 | && info.m_external_attr & |
967 | (0x20 << 24)) { // and has sym link attribute (0x80 is file, 0x40 |
968 | // is directory) |
969 | #if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \ |
970 | defined(__MINGW32__) |
971 | #else |
972 | if (info.m_uncomp_size > MAX_PATH || |
973 | !mz_zip_reader_extract_to_mem_no_alloc(zip_archive, i, symlink_to, |
974 | MAX_PATH, 0, NULL, 0)) { |
975 | goto out; |
976 | } |
977 | symlink_to[info.m_uncomp_size] = '\0'; |
978 | if (symlink(symlink_to, path) != 0) { |
979 | goto out; |
980 | } |
981 | #endif |
982 | } else { |
983 | if (!mz_zip_reader_is_file_a_directory(zip_archive, i)) { |
984 | if (!mz_zip_reader_extract_to_file(zip_archive, i, path, 0)) { |
985 | // Cannot extract zip archive to file |
986 | goto out; |
987 | } |
988 | } |
989 | |
990 | #if defined(_MSC_VER) |
991 | #else |
992 | xattr = (info.m_external_attr >> 16) & 0xFFFF; |
993 | if (xattr > 0) { |
994 | if (chmod(path, (mode_t)xattr) < 0) { |
995 | goto out; |
996 | } |
997 | } |
998 | #endif |
999 | } |
1000 | |
1001 | if (on_extract) { |
1002 | if (on_extract(path, arg) < 0) { |
1003 | goto out; |
1004 | } |
1005 | } |
1006 | } |
1007 | status = 0; |
1008 | |
1009 | out: |
1010 | // Close the archive, freeing any resources it was using |
1011 | if (!mz_zip_reader_end(zip_archive)) { |
1012 | // Cannot end zip reader |
1013 | status = -1; |
1014 | } |
1015 | return status; |
1016 | } |
1017 | |
1018 | int (const char *zipname, const char *dir, |
1019 | int (*)(const char *filename, void *arg), void *arg) { |
1020 | mz_zip_archive zip_archive; |
1021 | if (!zipname || !dir) { |
1022 | // Cannot parse zip archive name |
1023 | return -1; |
1024 | } |
1025 | if (!memset(&zip_archive, 0, sizeof(mz_zip_archive))) { |
1026 | // Cannot memset zip archive |
1027 | return MZ_FALSE; |
1028 | } |
1029 | // Now try to open the archive. |
1030 | if (!mz_zip_reader_init_file(&zip_archive, zipname, 0)) { |
1031 | // Cannot initialize zip_archive reader |
1032 | return MZ_FALSE; |
1033 | } |
1034 | |
1035 | int status = extract(&zip_archive, dir, on_extract, arg); |
1036 | |
1037 | return status; |
1038 | } |
1039 | |
1040 | int (const char *stream, size_t size, const char *dir, |
1041 | int (*)(const char *filename, void *arg), |
1042 | void *arg) { |
1043 | mz_zip_archive zip_archive; |
1044 | if (!stream || !dir) { |
1045 | // Cannot parse zip archive stream |
1046 | return -1; |
1047 | } |
1048 | if (!memset(&zip_archive, 0, sizeof(mz_zip_archive))) { |
1049 | // Cannot memset zip archive |
1050 | return MZ_FALSE; |
1051 | } |
1052 | if (!mz_zip_reader_init_mem(&zip_archive, stream, size, 0)) { |
1053 | // Cannot initialize zip_archive reader |
1054 | return MZ_FALSE; |
1055 | } |
1056 | |
1057 | int status = extract(&zip_archive, dir, on_extract, arg); |
1058 | |
1059 | return status; |
1060 | } |
1061 | |
1062 | typedef enum { |
1063 | MZ_KEEP = 0, |
1064 | MZ_DELETE = 1, |
1065 | MZ_MOVE = 2, |
1066 | } modify_type; |
1067 | |
1068 | struct entry_mark { |
1069 | int file_index; |
1070 | modify_type type; |
1071 | mz_uint64 ; |
1072 | mz_uint64 lf_length; |
1073 | }; |
1074 | |
1075 | struct zip_t *zip_open_stream(const char *stream, size_t size) { |
1076 | struct zip_t *zip = NULL; |
1077 | zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t)); |
1078 | if (!zip) { |
1079 | return NULL; |
1080 | } |
1081 | if (!mz_zip_reader_init_mem(&(zip->archive), stream, size, 0)) { |
1082 | goto cleanup; |
1083 | } |
1084 | return zip; |
1085 | |
1086 | cleanup: |
1087 | CLEANUP(zip); |
1088 | return NULL; |
1089 | } |
1090 | |
1091 | static mz_bool file_name_matches(const char *file_name, |
1092 | const char *delete_name) { |
1093 | int delete_name_length = strlen(delete_name); |
1094 | char *delete_entry_name = strrpl(delete_name, delete_name_length, '\\', '/'); |
1095 | if (!delete_entry_name) { |
1096 | return MZ_FALSE; |
1097 | } |
1098 | |
1099 | mz_bool res = |
1100 | (strcmp(file_name, delete_entry_name) == 0) ? MZ_TRUE : MZ_FALSE; |
1101 | CLEANUP(delete_entry_name); |
1102 | return res; |
1103 | } |
1104 | |
1105 | static int init_entry_mark_array(struct zip_t *zip, |
1106 | struct entry_mark *entry_mark_array, int n, |
1107 | char *const entries[], const size_t len) { |
1108 | if (!zip || !entry_mark_array || !entries) { |
1109 | return -1; |
1110 | } |
1111 | |
1112 | mz_zip_archive_file_stat file_stat; |
1113 | mz_uint64 d_pos = ~0; |
1114 | for (int i = 0; i < n; ++i) { |
1115 | if (zip_entry_openbyindex(zip, i)) { |
1116 | return -1; |
1117 | } |
1118 | mz_bool name_matches = MZ_FALSE; |
1119 | for (int j = 0; j < (const int)len; ++j) { |
1120 | if (file_name_matches(zip->entry.name, entries[j])) { |
1121 | name_matches = MZ_TRUE; |
1122 | break; |
1123 | } |
1124 | } |
1125 | if (name_matches) { |
1126 | entry_mark_array[i].type = MZ_DELETE; |
1127 | } else { |
1128 | entry_mark_array[i].type = MZ_KEEP; |
1129 | } |
1130 | |
1131 | if (!mz_zip_reader_file_stat(&zip->archive, i, &file_stat)) { |
1132 | return -1; |
1133 | } |
1134 | zip_entry_close(zip); |
1135 | entry_mark_array[i].m_local_header_ofs = file_stat.m_local_header_ofs; |
1136 | entry_mark_array[i].file_index = -1; |
1137 | entry_mark_array[i].lf_length = 0; |
1138 | if ((entry_mark_array[i].type) == MZ_DELETE && |
1139 | (d_pos > entry_mark_array[i].m_local_header_ofs)) { |
1140 | d_pos = entry_mark_array[i].m_local_header_ofs; |
1141 | } |
1142 | } |
1143 | for (int i = 0; i < n; ++i) { |
1144 | if ((entry_mark_array[i].m_local_header_ofs > d_pos) && |
1145 | (entry_mark_array[i].type != MZ_DELETE)) { |
1146 | entry_mark_array[i].type = MZ_MOVE; |
1147 | } |
1148 | } |
1149 | return 0; |
1150 | } |
1151 | |
1152 | static int get_new_index(mz_uint64 *, int cur_index) { |
1153 | int new_index = 0; |
1154 | for (int i = cur_index - 1; i >= 0; --i) { |
1155 | if (local_header_ofs_array[cur_index] > local_header_ofs_array[i]) { |
1156 | new_index = i + 1; |
1157 | return new_index; |
1158 | } |
1159 | } |
1160 | return new_index; |
1161 | } |
1162 | |
1163 | static int sort_insert(mz_uint64 *, int cur_index) { |
1164 | int new_index = get_new_index(local_header_ofs_array, cur_index); |
1165 | |
1166 | if (new_index != cur_index) { |
1167 | mz_uint64 temp = local_header_ofs_array[cur_index]; |
1168 | for (int i = cur_index; i > new_index; i--) { |
1169 | local_header_ofs_array[i] = local_header_ofs_array[i - 1]; |
1170 | } |
1171 | local_header_ofs_array[new_index] = temp; |
1172 | } |
1173 | return new_index; |
1174 | } |
1175 | |
1176 | static int update_file_index_array(struct entry_mark *entry_mark_array, |
1177 | int end_index, int insert_index) { |
1178 | for (int j = 0; j < end_index; j++) { |
1179 | if (entry_mark_array[j].file_index >= insert_index) { |
1180 | entry_mark_array[j].file_index += 1; |
1181 | } |
1182 | } |
1183 | entry_mark_array[insert_index].file_index = end_index; |
1184 | return 0; |
1185 | } |
1186 | |
1187 | static int finalize_entry_mark_array(struct zip_t *zip, |
1188 | struct entry_mark *entry_mark_array, |
1189 | const int n) { |
1190 | |
1191 | mz_uint64 * = (mz_uint64 *)calloc(n, sizeof(mz_uint64)); |
1192 | if (local_header_ofs_array == NULL) { |
1193 | return -1; |
1194 | } |
1195 | |
1196 | for (int i = 0; i < n; ++i) { |
1197 | local_header_ofs_array[i] = entry_mark_array[i].m_local_header_ofs; |
1198 | int new_index = sort_insert(local_header_ofs_array, i); |
1199 | |
1200 | if (new_index != i) { |
1201 | update_file_index_array(entry_mark_array, i, new_index); |
1202 | } |
1203 | entry_mark_array[i].file_index = new_index; |
1204 | } |
1205 | |
1206 | mz_uint64 *length = (mz_uint64 *)calloc(n, sizeof(mz_uint64)); |
1207 | if (length == NULL) { |
1208 | CLEANUP(local_header_ofs_array); |
1209 | return -1; |
1210 | } |
1211 | for (int i = 0; i < n - 1; i++) { |
1212 | length[i] = local_header_ofs_array[i + 1] - local_header_ofs_array[i]; |
1213 | } |
1214 | length[n - 1] = zip->archive.m_archive_size - local_header_ofs_array[n - 1]; |
1215 | |
1216 | for (int i = 0; i < n; i++) { |
1217 | entry_mark_array[i].lf_length = length[entry_mark_array[i].file_index]; |
1218 | } |
1219 | |
1220 | CLEANUP(length); |
1221 | CLEANUP(local_header_ofs_array); |
1222 | return 0; |
1223 | } |
1224 | |
1225 | static int set_entry_mark_array(struct zip_t *zip, |
1226 | struct entry_mark *entry_mark_array, int n, |
1227 | char *const entries[], const size_t len) { |
1228 | if (init_entry_mark_array(zip, entry_mark_array, n, entries, len)) { |
1229 | return -1; |
1230 | } |
1231 | if (finalize_entry_mark_array(zip, entry_mark_array, n)) { |
1232 | return -1; |
1233 | } |
1234 | return 0; |
1235 | } |
1236 | |
1237 | static mz_int64 file_move(MZ_FILE *m_pFile, const mz_uint64 to, |
1238 | const mz_uint64 from, const mz_uint64 length, |
1239 | mz_uint8 *move_buf, const mz_int64 capacity_size) { |
1240 | if ((mz_int64)length > capacity_size) { |
1241 | return -1; |
1242 | } |
1243 | if (MZ_FSEEK64(m_pFile, from, SEEK_SET)) { |
1244 | MZ_FCLOSE(m_pFile); |
1245 | return -1; |
1246 | } |
1247 | |
1248 | if (fread(move_buf, 1, length, m_pFile) != length) { |
1249 | MZ_FCLOSE(m_pFile); |
1250 | return -1; |
1251 | } |
1252 | if (MZ_FSEEK64(m_pFile, to, SEEK_SET)) { |
1253 | MZ_FCLOSE(m_pFile); |
1254 | return -1; |
1255 | } |
1256 | if (fwrite(move_buf, 1, length, m_pFile) != length) { |
1257 | MZ_FCLOSE(m_pFile); |
1258 | return -1; |
1259 | } |
1260 | return (mz_int64)length; |
1261 | } |
1262 | |
1263 | static mz_int64 move_files(MZ_FILE *m_pFile, mz_uint64 writen_num, |
1264 | mz_uint64 read_num, mz_uint64 length) { |
1265 | const mz_int64 page_size = 1 << 12; // 4K |
1266 | mz_uint8 *move_buf = (mz_uint8 *)calloc(1, page_size); |
1267 | if (move_buf == NULL) { |
1268 | return -1; |
1269 | } |
1270 | |
1271 | mz_int64 moved_length = 0; |
1272 | mz_int64 move_count = 0; |
1273 | while ((mz_int64)length > 0) { |
1274 | move_count = ((mz_int64)length >= page_size) ? page_size : (mz_int64)length; |
1275 | if (file_move(m_pFile, writen_num, read_num, move_count, move_buf, |
1276 | page_size) != move_count) { |
1277 | goto cleanup; |
1278 | } |
1279 | writen_num += move_count; |
1280 | read_num += move_count; |
1281 | length -= move_count; |
1282 | moved_length += move_count; |
1283 | } |
1284 | |
1285 | cleanup: |
1286 | CLEANUP(move_buf); |
1287 | return moved_length; |
1288 | } |
1289 | |
1290 | static int move_central_dir_entry(mz_zip_internal_state *pState, int begin, |
1291 | int end, int entry_num) { |
1292 | if (begin == entry_num) { |
1293 | return 0; |
1294 | } |
1295 | |
1296 | mz_uint64 l_size = 0; |
1297 | mz_uint64 r_size = 0; |
1298 | mz_uint64 d_size = 0; |
1299 | mz_uint8 *next = NULL; |
1300 | mz_uint8 *deleted = &MZ_ZIP_ARRAY_ELEMENT( |
1301 | &pState->m_central_dir, mz_uint8, |
1302 | MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, begin)); |
1303 | l_size = (mz_uint32)(deleted - (mz_uint8 *)(pState->m_central_dir.m_p)); |
1304 | if (end == entry_num) { |
1305 | r_size = 0; |
1306 | } else { |
1307 | next = &MZ_ZIP_ARRAY_ELEMENT( |
1308 | &pState->m_central_dir, mz_uint8, |
1309 | MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, end)); |
1310 | r_size = pState->m_central_dir.m_size - |
1311 | (mz_uint32)(next - (mz_uint8 *)(pState->m_central_dir.m_p)); |
1312 | d_size = next - deleted; |
1313 | } |
1314 | |
1315 | if (l_size == 0) { |
1316 | memmove(pState->m_central_dir.m_p, next, r_size); |
1317 | pState->m_central_dir.m_p = MZ_REALLOC(pState->m_central_dir.m_p, r_size); |
1318 | for (int i = end; i < entry_num; i++) { |
1319 | MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, i) -= |
1320 | d_size; |
1321 | } |
1322 | } |
1323 | |
1324 | if (l_size * r_size != 0) { |
1325 | memmove(deleted, next, r_size); |
1326 | for (int i = end; i < entry_num; i++) { |
1327 | MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, i) -= |
1328 | d_size; |
1329 | } |
1330 | } |
1331 | |
1332 | pState->m_central_dir.m_size = l_size + r_size; |
1333 | return 0; |
1334 | } |
1335 | |
1336 | static int delete_central_dir_entries(mz_zip_internal_state *pState, |
1337 | int *deleted_entry_index_array, |
1338 | int entry_num) { |
1339 | int i = 0; |
1340 | int begin = 0; |
1341 | int end = 0; |
1342 | int d_num = 0; |
1343 | while (i < entry_num) { |
1344 | while ((!deleted_entry_index_array[i]) && (i < entry_num)) { |
1345 | i++; |
1346 | } |
1347 | begin = i; |
1348 | |
1349 | while ((deleted_entry_index_array[i]) && (i < entry_num)) { |
1350 | i++; |
1351 | } |
1352 | end = i; |
1353 | move_central_dir_entry(pState, begin, end, entry_num); |
1354 | } |
1355 | |
1356 | i = 0; |
1357 | while (i < entry_num) { |
1358 | while ((!deleted_entry_index_array[i]) && (i < entry_num)) { |
1359 | i++; |
1360 | } |
1361 | begin = i; |
1362 | if (begin == entry_num) { |
1363 | break; |
1364 | } |
1365 | while ((deleted_entry_index_array[i]) && (i < entry_num)) { |
1366 | i++; |
1367 | } |
1368 | end = i; |
1369 | int k = 0; |
1370 | for (int j = end; j < entry_num; j++) { |
1371 | MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, |
1372 | begin + k) = |
1373 | (mz_uint32)MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, |
1374 | mz_uint32, j); |
1375 | k++; |
1376 | } |
1377 | d_num += end - begin; |
1378 | } |
1379 | |
1380 | pState->m_central_dir_offsets.m_size = |
1381 | sizeof(mz_uint32) * (entry_num - d_num); |
1382 | return 0; |
1383 | } |
1384 | |
1385 | static int delete_entries(struct zip_t *zip, |
1386 | struct entry_mark *entry_mark_array, int entry_num) { |
1387 | mz_bool *deleted_entry_flag_array = |
1388 | (mz_bool *)calloc(entry_num, sizeof(mz_bool)); |
1389 | if (deleted_entry_flag_array == NULL) { |
1390 | return -1; |
1391 | } |
1392 | |
1393 | mz_zip_internal_state *pState = zip->archive.m_pState; |
1394 | zip->archive.m_zip_mode = MZ_ZIP_MODE_WRITING; |
1395 | |
1396 | if (MZ_FSEEK64(pState->m_pFile, 0, SEEK_SET)) { |
1397 | CLEANUP(deleted_entry_flag_array); |
1398 | return -1; |
1399 | } |
1400 | |
1401 | mz_uint64 writen_num = 0; |
1402 | mz_uint64 read_num = 0; |
1403 | mz_uint64 deleted_length = 0; |
1404 | mz_uint64 move_length = 0; |
1405 | int i = 0; |
1406 | int deleted_entry_num = 0; |
1407 | while (i < entry_num) { |
1408 | while ((entry_mark_array[i].type == MZ_KEEP) && (i < entry_num)) { |
1409 | writen_num += entry_mark_array[i].lf_length; |
1410 | read_num = writen_num; |
1411 | i++; |
1412 | } |
1413 | |
1414 | while ((entry_mark_array[i].type == MZ_DELETE) && (i < entry_num)) { |
1415 | deleted_entry_flag_array[i] = MZ_TRUE; |
1416 | read_num += entry_mark_array[i].lf_length; |
1417 | deleted_length += entry_mark_array[i].lf_length; |
1418 | i++; |
1419 | deleted_entry_num++; |
1420 | } |
1421 | |
1422 | while ((entry_mark_array[i].type == MZ_MOVE) && (i < entry_num)) { |
1423 | move_length += entry_mark_array[i].lf_length; |
1424 | mz_uint8 *p = &MZ_ZIP_ARRAY_ELEMENT( |
1425 | &pState->m_central_dir, mz_uint8, |
1426 | MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, i)); |
1427 | if (!p) { |
1428 | CLEANUP(deleted_entry_flag_array); |
1429 | return -1; |
1430 | } |
1431 | mz_uint32 offset = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS); |
1432 | offset -= (mz_uint32)deleted_length; |
1433 | MZ_WRITE_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS, offset); |
1434 | i++; |
1435 | } |
1436 | if (move_files(pState->m_pFile, writen_num, read_num, move_length) != |
1437 | (mz_int64)move_length) { |
1438 | CLEANUP(deleted_entry_flag_array); |
1439 | return -1; |
1440 | } |
1441 | writen_num += move_length; |
1442 | read_num += move_length; |
1443 | } |
1444 | |
1445 | zip->archive.m_archive_size -= deleted_length; |
1446 | zip->archive.m_total_files = entry_num - deleted_entry_num; |
1447 | |
1448 | delete_central_dir_entries(pState, deleted_entry_flag_array, entry_num); |
1449 | CLEANUP(deleted_entry_flag_array); |
1450 | |
1451 | return deleted_entry_num; |
1452 | } |
1453 | |
1454 | int zip_entries_delete(struct zip_t *zip, char *const entries[], |
1455 | const size_t len) { |
1456 | if (zip == NULL || (entries == NULL && len != 0)) { |
1457 | return -1; |
1458 | } |
1459 | if (entries == NULL && len == 0) { |
1460 | return 0; |
1461 | } |
1462 | int n = zip_total_entries(zip); |
1463 | struct entry_mark *entry_mark_array = |
1464 | (struct entry_mark *)calloc(n, sizeof(struct entry_mark)); |
1465 | if (entry_mark_array == NULL) { |
1466 | return -1; |
1467 | } |
1468 | zip->archive.m_zip_mode = MZ_ZIP_MODE_READING; |
1469 | |
1470 | if (set_entry_mark_array(zip, entry_mark_array, n, entries, len)) { |
1471 | CLEANUP(entry_mark_array); |
1472 | return -1; |
1473 | } |
1474 | |
1475 | int res = delete_entries(zip, entry_mark_array, n); |
1476 | CLEANUP(entry_mark_array); |
1477 | return res; |
1478 | } |
1479 | |