Debian Codegraph @

src/cdogs/cwolfmap/zip/zip.c

raw symbols SCIP
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 STRCLONE(STR) ((STR) ? _strdup(STR) : NULL)
22 #define HAS_DEVICE(P) \
23 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) && \
24 (P)[1] == ':')
25 #define FILESYSTEM_PREFIX_LEN(P) (HAS_DEVICE(P) ? 2 : 0)
26
27 #else
28
29 #include <unistd.h> // needed for symlink()
30 #define STRCLONE(STR) ((STR) ? strdup(STR) : NULL)
31
32 #endif
33
34 #ifdef __MINGW32__
35 #include <sys/types.h>
36 #include <unistd.h>
37 #endif
38
39 #include "miniz.h"
40 #include "zip.h"
41
42 #ifdef _MSC_VER
43 #include <io.h>
44
45 #define ftruncate(fd, sz) (-(_chsize_s((fd), (sz)) != 0))
46 #define fileno _fileno
47 #endif
48
49 #if defined(__TINYC__) && (defined(_WIN32) || defined(_WIN64))
50 #include <io.h>
51
52 #define ftruncate(fd, sz) (-(_chsize_s((fd), (sz)) != 0))
53 #define fileno _fileno
54 #endif
55
56 #ifndef HAS_DEVICE
57 #define HAS_DEVICE(P) 0
58 #endif
59
60 #ifndef FILESYSTEM_PREFIX_LEN
61 #define FILESYSTEM_PREFIX_LEN(P) 0
62 #endif
63
64 #ifndef ISSLASH
65 #define ISSLASH(C) ((C) == '/' || (C) == '\\')
66 #endif
67
68 #define CLEANUP(ptr) \
69 do { \
70 if (ptr) { \
71 free((void *)ptr); \
72 ptr = NULL; \
73 } \
74 } while (0)
75
76 #define UNX_IFDIR 0040000 /* Unix directory */
77 #define UNX_IFREG 0100000 /* Unix regular file */
78 #define UNX_IFSOCK 0140000 /* Unix socket (BSD, not SysV or Amiga) */
79 #define UNX_IFLNK 0120000 /* Unix symbolic link (not SysV, Amiga) */
80 #define UNX_IFBLK 0060000 /* Unix block special (not Amiga) */
81 #define UNX_IFCHR 0020000 /* Unix character special (not Amiga) */
82 #define UNX_IFIFO 0010000 /* Unix fifo (BCC, not MSC or Amiga) */
83
84 struct zip_entry_t {
85 ssize_t index;
86 char *name;
87 mz_uint64 uncomp_size;
88 mz_uint64 comp_size;
89 mz_uint32 uncomp_crc32;
90 mz_uint64 offset;
91 mz_uint8 header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
92 mz_uint64 header_offset;
93 mz_uint16 method;
94 mz_zip_writer_add_state state;
95 tdefl_compressor comp;
96 mz_uint32 external_attr;
97 time_t m_time;
98 };
99
100 struct zip_t {
101 mz_zip_archive archive;
102 mz_uint level;
103 struct zip_entry_t entry;
104 };
105
106 enum zip_modify_t {
107 MZ_KEEP = 0,
108 MZ_DELETE = 1,
109 MZ_MOVE = 2,
110 };
111
112 struct zip_entry_mark_t {
113 ssize_t file_index;
114 enum zip_modify_t type;
115 mz_uint64 m_local_header_ofs;
116 size_t lf_length;
117 };
118
119 static const char *const zip_errlist[30] = {
120 NULL,
121 "not initialized\0",
122 "invalid entry name\0",
123 "entry not found\0",
124 "invalid zip mode\0",
125 "invalid compression level\0",
126 "no zip 64 support\0",
127 "memset error\0",
128 "cannot write data to entry\0",
129 "cannot initialize tdefl compressor\0",
130 "invalid index\0",
131 "header not found\0",
132 "cannot flush tdefl buffer\0",
133 "cannot write entry header\0",
134 "cannot create entry header\0",
135 "cannot write to central dir\0",
136 "cannot open file\0",
137 "invalid entry type\0",
138 "extracting data using no memory allocation\0",
139 "file not found\0",
140 "no permission\0",
141 "out of memory\0",
142 "invalid zip archive name\0",
143 "make dir error\0",
144 "symlink error\0",
145 "close archive error\0",
146 "capacity size too small\0",
147 "fseek error\0",
148 "fread error\0",
149 "fwrite error\0",
150 };
151
152 const char *zip_strerror(int errnum) {
153 errnum = -errnum;
154 if (errnum <= 0 || errnum >= 30) {
155 return NULL;
156 }
157
158 return zip_errlist[errnum];
159 }
160
161 static const char *zip_basename(const char *name) {
162 char const *p;
163 char const *base = name += FILESYSTEM_PREFIX_LEN(name);
164 int all_slashes = 1;
165
166 for (p = name; *p; p++) {
167 if (ISSLASH(*p))
168 base = p + 1;
169 else
170 all_slashes = 0;
171 }
172
173 /* If NAME is all slashes, arrange to return `/'. */
174 if (*base == '\0' && ISSLASH(*name) && all_slashes)
175 --base;
176
177 return base;
178 }
179
180 static int zip_mkpath(char *path) {
181 char *p;
182 char npath[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE + 1];
183 int len = 0;
184 int has_device = HAS_DEVICE(path);
185
186 memset(npath, 0, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE + 1);
187 if (has_device) {
188 // only on windows
189 npath[0] = path[0];
190 npath[1] = path[1];
191 len = 2;
192 }
193 for (p = path + len; *p && len < MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE; p++) {
194 if (ISSLASH(*p) && ((!has_device && len > 0) || (has_device && len > 2))) {
195 #if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \
196 defined(__MINGW32__)
197 #else
198 if ('\\' == *p) {
199 *p = '/';
200 }
201 #endif
202
203 if (MZ_MKDIR(npath) == -1) {
204 if (errno != EEXIST) {
205 return ZIP_EMKDIR;
206 }
207 }
208 }
209 npath[len++] = *p;
210 }
211
212 return 0;
213 }
214
215 static char *zip_strrpl(const char *str, size_t n, char oldchar, char newchar) {
216 char c;
217 size_t i;
218 char *rpl = (char *)calloc((1 + n), sizeof(char));
219 char *begin = rpl;
220 if (!rpl) {
221 return NULL;
222 }
223
224 for (i = 0; (i < n) && (c = *str++); ++i) {
225 if (c == oldchar) {
226 c = newchar;
227 }
228 *rpl++ = c;
229 }
230
231 return begin;
232 }
233
234 static char *zip_name_normalize(char *name, char *const nname, size_t len) {
235 size_t offn = 0;
236 size_t offnn = 0, ncpy = 0;
237
238 if (name == NULL || nname == NULL || len <= 0) {
239 return NULL;
240 }
241 // skip trailing '/'
242 while (ISSLASH(*name))
243 name++;
244
245 for (; offn < len; offn++) {
246 if (ISSLASH(name[offn])) {
247 if (ncpy > 0 && strcmp(&nname[offnn], ".\0") &&
248 strcmp(&nname[offnn], "..\0")) {
249 offnn += ncpy;
250 nname[offnn++] = name[offn]; // append '/'
251 }
252 ncpy = 0;
253 } else {
254 nname[offnn + ncpy] = name[offn];
255 ncpy++;
256 }
257 }
258
259 // at the end, extra check what we've already copied
260 if (ncpy == 0 || !strcmp(&nname[offnn], ".\0") ||
261 !strcmp(&nname[offnn], "..\0")) {
262 nname[offnn] = 0;
263 }
264 return nname;
265 }
266
267 static mz_bool zip_name_match(const char *name1, const char *name2) {
268 char *nname2 = NULL;
269
270 #ifdef ZIP_RAW_ENTRYNAME
271 nname2 = STRCLONE(name2);
272 #else
273 nname2 = zip_strrpl(name2, strlen(name2), '\\', '/');
274 #endif
275
276 if (!nname2) {
277 return MZ_FALSE;
278 }
279
280 mz_bool res = (strcmp(name1, nname2) == 0) ? MZ_TRUE : MZ_FALSE;
281 CLEANUP(nname2);
282 return res;
283 }
284
285 static int zip_archive_truncate(mz_zip_archive *pzip) {
286 mz_zip_internal_state *pState = pzip->m_pState;
287 mz_uint64 file_size = pzip->m_archive_size;
288 if ((pzip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem)) {
289 return 0;
290 }
291 if (pzip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED) {
292 if (pState->m_pFile) {
293 int fd = fileno(pState->m_pFile);
294 return ftruncate(fd, file_size);
295 }
296 }
297 return 0;
298 }
299
300 static int zip_archive_extract(mz_zip_archive *zip_archive, const char *dir,
301 int (*on_extract)(const char *filename,
302 void *arg),
303 void *arg) {
304 int err = 0;
305 mz_uint i, n;
306 char path[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE + 1];
307 char symlink_to[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE + 1];
308 mz_zip_archive_file_stat info;
309 size_t dirlen = 0, filename_size = MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE;
310 mz_uint32 xattr = 0;
311
312 memset(path, 0, sizeof(path));
313 memset(symlink_to, 0, sizeof(symlink_to));
314
315 dirlen = strlen(dir);
316 if (dirlen + 1 > MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE) {
317 return ZIP_EINVENTNAME;
318 }
319
320 memset((void *)&info, 0, sizeof(mz_zip_archive_file_stat));
321
322 #if defined(_MSC_VER)
323 strcpy_s(path, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE, dir);
324 #else
325 strcpy(path, dir);
326 #endif
327
328 if (!ISSLASH(path[dirlen - 1])) {
329 #if defined(_WIN32) || defined(__WIN32__)
330 path[dirlen] = '\\';
331 #else
332 path[dirlen] = '/';
333 #endif
334 ++dirlen;
335 }
336
337 if (filename_size > MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - dirlen) {
338 filename_size = MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - dirlen;
339 }
340 // Get and print information about each file in the archive.
341 n = mz_zip_reader_get_num_files(zip_archive);
342 for (i = 0; i < n; ++i) {
343 if (!mz_zip_reader_file_stat(zip_archive, i, &info)) {
344 // Cannot get information about zip archive;
345 err = ZIP_ENOENT;
346 goto out;
347 }
348
349 if (!zip_name_normalize(info.m_filename, info.m_filename,
350 strlen(info.m_filename))) {
351 // Cannot normalize file name;
352 err = ZIP_EINVENTNAME;
353 goto out;
354 }
355
356 #if defined(_MSC_VER)
357 strncpy_s(&path[dirlen], filename_size, info.m_filename,
358 filename_size);
359 #else
360 strncpy(&path[dirlen], info.m_filename, filename_size);
361 #endif
362 err = zip_mkpath(path);
363 if (err < 0) {
364 // Cannot make a path
365 goto out;
366 }
367
368 if ((((info.m_version_made_by >> 8) == 3) ||
369 ((info.m_version_made_by >> 8) ==
370 19)) // if zip is produced on Unix or macOS (3 and 19 from
371 // section 4.4.2.2 of zip standard)
372 && info.m_external_attr &
373 (0x20 << 24)) { // and has sym link attribute (0x80 is file, 0x40
374 // is directory)
375 #if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \
376 defined(__MINGW32__)
377 #else
378 if (info.m_uncomp_size > MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE ||
379 !mz_zip_reader_extract_to_mem_no_alloc(zip_archive, i, symlink_to,
380 MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE, 0, NULL, 0)) {
381 err = ZIP_EMEMNOALLOC;
382 goto out;
383 }
384 symlink_to[info.m_uncomp_size] = '\0';
385 if (symlink(symlink_to, path) != 0) {
386 err = ZIP_ESYMLINK;
387 goto out;
388 }
389 #endif
390 } else {
391 if (!mz_zip_reader_is_file_a_directory(zip_archive, i)) {
392 if (!mz_zip_reader_extract_to_file(zip_archive, i, path, 0)) {
393 // Cannot extract zip archive to file
394 err = ZIP_ENOFILE;
395 goto out;
396 }
397 }
398
399 #if defined(_MSC_VER) || defined(PS4)
400 (void)xattr; // unused
401 #else
402 xattr = (info.m_external_attr >> 16) & 0xFFFF;
403 if (xattr > 0 && xattr <= MZ_UINT16_MAX) {
404 if (CHMOD(path, (mode_t)xattr) < 0) {
405 err = ZIP_ENOPERM;
406 goto out;
407 }
408 }
409 #endif
410 }
411
412 if (on_extract) {
413 if (on_extract(path, arg) < 0) {
414 goto out;
415 }
416 }
417 }
418
419 out:
420 // Close the archive, freeing any resources it was using
421 if (!mz_zip_reader_end(zip_archive)) {
422 // Cannot end zip reader
423 err = ZIP_ECLSZIP;
424 }
425 return err;
426 }
427
428 static inline void zip_archive_finalize(mz_zip_archive *pzip) {
429 mz_zip_writer_finalize_archive(pzip);
430 zip_archive_truncate(pzip);
431 }
432
433 static ssize_t zip_entry_mark(struct zip_t *zip,
434 struct zip_entry_mark_t *entry_mark,
435 const ssize_t n, char *const entries[],
436 const size_t len) {
437 ssize_t i = 0;
438 ssize_t err = 0;
439 if (!zip || !entry_mark || !entries) {
440 return ZIP_ENOINIT;
441 }
442
443 mz_zip_archive_file_stat file_stat;
444 mz_uint64 d_pos = ~0UL;
445 for (i = 0; i < n; ++i) {
446 if ((err = zip_entry_openbyindex(zip, i))) {
447 return (ssize_t)err;
448 }
449
450 mz_bool name_matches = MZ_FALSE;
451 {
452 size_t j;
453 for (j = 0; j < len; ++j) {
454 if (zip_name_match(zip->entry.name, entries[j])) {
455 name_matches = MZ_TRUE;
456 break;
457 }
458 }
459 }
460 if (name_matches) {
461 entry_mark[i].type = MZ_DELETE;
462 } else {
463 entry_mark[i].type = MZ_KEEP;
464 }
465
466 if (!mz_zip_reader_file_stat(&zip->archive, i, &file_stat)) {
467 return ZIP_ENOENT;
468 }
469
470 zip_entry_close(zip);
471
472 entry_mark[i].m_local_header_ofs = file_stat.m_local_header_ofs;
473 entry_mark[i].file_index = (ssize_t)-1;
474 entry_mark[i].lf_length = 0;
475 if ((entry_mark[i].type) == MZ_DELETE &&
476 (d_pos > entry_mark[i].m_local_header_ofs)) {
477 d_pos = entry_mark[i].m_local_header_ofs;
478 }
479 }
480
481 for (i = 0; i < n; ++i) {
482 if ((entry_mark[i].m_local_header_ofs > d_pos) &&
483 (entry_mark[i].type != MZ_DELETE)) {
484 entry_mark[i].type = MZ_MOVE;
485 }
486 }
487 return err;
488 }
489
490 static ssize_t zip_index_next(mz_uint64 *local_header_ofs_array,
491 ssize_t cur_index) {
492 ssize_t new_index = 0, i;
493 for (i = cur_index - 1; i >= 0; --i) {
494 if (local_header_ofs_array[cur_index] > local_header_ofs_array[i]) {
495 new_index = i + 1;
496 return new_index;
497 }
498 }
499 return new_index;
500 }
501
502 static ssize_t zip_sort(mz_uint64 *local_header_ofs_array, ssize_t cur_index) {
503 ssize_t nxt_index = zip_index_next(local_header_ofs_array, cur_index);
504
505 if (nxt_index != cur_index) {
506 mz_uint64 temp = local_header_ofs_array[cur_index];
507 ssize_t i;
508 for (i = cur_index; i > nxt_index; i--) {
509 local_header_ofs_array[i] = local_header_ofs_array[i - 1];
510 }
511 local_header_ofs_array[nxt_index] = temp;
512 }
513 return nxt_index;
514 }
515
516 static int zip_index_update(struct zip_entry_mark_t *entry_mark,
517 ssize_t last_index, ssize_t nxt_index) {
518 ssize_t j;
519 for (j = 0; j < last_index; j++) {
520 if (entry_mark[j].file_index >= nxt_index) {
521 entry_mark[j].file_index += 1;
522 }
523 }
524 entry_mark[nxt_index].file_index = last_index;
525 return 0;
526 }
527
528 static int zip_entry_finalize(struct zip_t *zip,
529 struct zip_entry_mark_t *entry_mark,
530 const ssize_t n) {
531
532 ssize_t i = 0;
533 mz_uint64 *local_header_ofs_array = (mz_uint64 *)calloc(n, sizeof(mz_uint64));
534 if (!local_header_ofs_array) {
535 return ZIP_EOOMEM;
536 }
537
538 for (i = 0; i < n; ++i) {
539 local_header_ofs_array[i] = entry_mark[i].m_local_header_ofs;
540 ssize_t index = zip_sort(local_header_ofs_array, i);
541
542 if (index != i) {
543 zip_index_update(entry_mark, i, index);
544 }
545 entry_mark[i].file_index = index;
546 }
547
548 size_t *length = (size_t *)calloc(n, sizeof(size_t));
549 if (!length) {
550 CLEANUP(local_header_ofs_array);
551 return ZIP_EOOMEM;
552 }
553 for (i = 0; i < n - 1; i++) {
554 length[i] =
555 (size_t)(local_header_ofs_array[i + 1] - local_header_ofs_array[i]);
556 }
557 length[n - 1] =
558 (size_t)(zip->archive.m_archive_size - local_header_ofs_array[n - 1]);
559
560 for (i = 0; i < n; i++) {
561 entry_mark[i].lf_length = length[entry_mark[i].file_index];
562 }
563
564 CLEANUP(length);
565 CLEANUP(local_header_ofs_array);
566 return 0;
567 }
568
569 static ssize_t zip_entry_set(struct zip_t *zip,
570 struct zip_entry_mark_t *entry_mark, ssize_t n,
571 char *const entries[], const size_t len) {
572 ssize_t err = 0;
573
574 if ((err = zip_entry_mark(zip, entry_mark, n, entries, len)) < 0) {
575 return err;
576 }
577 if ((err = zip_entry_finalize(zip, entry_mark, n)) < 0) {
578 return err;
579 }
580 return 0;
581 }
582
583 static ssize_t zip_file_move(MZ_FILE *m_pFile, const mz_uint64 to,
584 const mz_uint64 from, const size_t length,
585 mz_uint8 *move_buf, const size_t capacity_size) {
586 if (length > capacity_size) {
587 return ZIP_ECAPSIZE;
588 }
589 if (MZ_FSEEK64(m_pFile, from, SEEK_SET)) {
590 return ZIP_EFSEEK;
591 }
592 if (fread(move_buf, 1, length, m_pFile) != length) {
593 return ZIP_EFREAD;
594 }
595 if (MZ_FSEEK64(m_pFile, to, SEEK_SET)) {
596 return ZIP_EFSEEK;
597 }
598 if (fwrite(move_buf, 1, length, m_pFile) != length) {
599 return ZIP_EFWRITE;
600 }
601 return (ssize_t)length;
602 }
603
604 static ssize_t zip_files_move(MZ_FILE *m_pFile, mz_uint64 writen_num,
605 mz_uint64 read_num, size_t length) {
606 ssize_t n = 0;
607 const size_t page_size = 1 << 12; // 4K
608 mz_uint8 *move_buf = (mz_uint8 *)calloc(1, page_size);
609 if (!move_buf) {
610 return ZIP_EOOMEM;
611 }
612
613 ssize_t moved_length = 0;
614 ssize_t move_count = 0;
615 while ((mz_int64)length > 0) {
616 move_count = (length >= page_size) ? page_size : length;
617 n = zip_file_move(m_pFile, writen_num, read_num, move_count, move_buf,
618 page_size);
619 if (n < 0) {
620 moved_length = n;
621 goto cleanup;
622 }
623
624 if (n != move_count) {
625 goto cleanup;
626 }
627
628 writen_num += move_count;
629 read_num += move_count;
630 length -= move_count;
631 moved_length += move_count;
632 }
633
634 cleanup:
635 CLEANUP(move_buf);
636 return moved_length;
637 }
638
639 static int zip_central_dir_move(mz_zip_internal_state *pState, int begin,
640 int end, int entry_num) {
641 if (begin == entry_num) {
642 return 0;
643 }
644
645 size_t l_size = 0;
646 size_t r_size = 0;
647 mz_uint32 d_size = 0;
648 mz_uint8 *next = NULL;
649 mz_uint8 *deleted = &MZ_ZIP_ARRAY_ELEMENT(
650 &pState->m_central_dir, mz_uint8,
651 MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, begin));
652 l_size = (size_t)(deleted - (mz_uint8 *)(pState->m_central_dir.m_p));
653 if (end == entry_num) {
654 r_size = 0;
655 } else {
656 next = &MZ_ZIP_ARRAY_ELEMENT(
657 &pState->m_central_dir, mz_uint8,
658 MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, end));
659 r_size = pState->m_central_dir.m_size -
660 (mz_uint32)(next - (mz_uint8 *)(pState->m_central_dir.m_p));
661 d_size = (mz_uint32)(next - deleted);
662 }
663
664 if (next && l_size == 0) {
665 memmove(pState->m_central_dir.m_p, next, r_size);
666 pState->m_central_dir.m_p = MZ_REALLOC(pState->m_central_dir.m_p, r_size);
667 {
668 int i;
669 for (i = end; i < entry_num; i++) {
670 MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, i) -=
671 d_size;
672 }
673 }
674 }
675
676 if (next && l_size * r_size != 0) {
677 memmove(deleted, next, r_size);
678 {
679 int i;
680 for (i = end; i < entry_num; i++) {
681 MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, i) -=
682 d_size;
683 }
684 }
685 }
686
687 pState->m_central_dir.m_size = l_size + r_size;
688 return 0;
689 }
690
691 static int zip_central_dir_delete(mz_zip_internal_state *pState,
692 int *deleted_entry_index_array,
693 int entry_num) {
694 int i = 0;
695 int begin = 0;
696 int end = 0;
697 int d_num = 0;
698 while (i < entry_num) {
699 while ((i < entry_num) && (!deleted_entry_index_array[i])) {
700 i++;
701 }
702 begin = i;
703
704 while ((i < entry_num) && (deleted_entry_index_array[i])) {
705 i++;
706 }
707 end = i;
708 zip_central_dir_move(pState, begin, end, entry_num);
709 }
710
711 i = 0;
712 while (i < entry_num) {
713 while ((i < entry_num) && (!deleted_entry_index_array[i])) {
714 i++;
715 }
716 begin = i;
717 if (begin == entry_num) {
718 break;
719 }
720 while ((i < entry_num) && (deleted_entry_index_array[i])) {
721 i++;
722 }
723 end = i;
724 int k = 0, j;
725 for (j = end; j < entry_num; j++) {
726 MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32,
727 begin + k) =
728 (mz_uint32)MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets,
729 mz_uint32, j);
730 k++;
731 }
732 d_num += end - begin;
733 }
734
735 pState->m_central_dir_offsets.m_size =
736 sizeof(mz_uint32) * (entry_num - d_num);
737 return 0;
738 }
739
740 static ssize_t zip_entries_delete_mark(struct zip_t *zip,
741 struct zip_entry_mark_t *entry_mark,
742 int entry_num) {
743 mz_uint64 writen_num = 0;
744 mz_uint64 read_num = 0;
745 size_t deleted_length = 0;
746 size_t move_length = 0;
747 int i = 0;
748 size_t deleted_entry_num = 0;
749 ssize_t n = 0;
750
751 mz_bool *deleted_entry_flag_array =
752 (mz_bool *)calloc(entry_num, sizeof(mz_bool));
753 if (deleted_entry_flag_array == NULL) {
754 return ZIP_EOOMEM;
755 }
756
757 mz_zip_internal_state *pState = zip->archive.m_pState;
758 zip->archive.m_zip_mode = MZ_ZIP_MODE_WRITING;
759
760 if ((!pState->m_pFile) || MZ_FSEEK64(pState->m_pFile, 0, SEEK_SET)) {
761 CLEANUP(deleted_entry_flag_array);
762 return ZIP_ENOENT;
763 }
764
765 while (i < entry_num) {
766 while ((i < entry_num) && (entry_mark[i].type == MZ_KEEP)) {
767 writen_num += entry_mark[i].lf_length;
768 read_num = writen_num;
769 i++;
770 }
771
772 while ((i < entry_num) && (entry_mark[i].type == MZ_DELETE)) {
773 deleted_entry_flag_array[i] = MZ_TRUE;
774 read_num += entry_mark[i].lf_length;
775 deleted_length += entry_mark[i].lf_length;
776 i++;
777 deleted_entry_num++;
778 }
779
780 while ((i < entry_num) && (entry_mark[i].type == MZ_MOVE)) {
781 move_length += entry_mark[i].lf_length;
782 mz_uint8 *p = &MZ_ZIP_ARRAY_ELEMENT(
783 &pState->m_central_dir, mz_uint8,
784 MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, i));
785 if (!p) {
786 CLEANUP(deleted_entry_flag_array);
787 return ZIP_ENOENT;
788 }
789 mz_uint32 offset = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS);
790 offset -= (mz_uint32)deleted_length;
791 MZ_WRITE_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS, offset);
792 i++;
793 }
794
795 n = zip_files_move(pState->m_pFile, writen_num, read_num, move_length);
796 if (n != (ssize_t)move_length) {
797 CLEANUP(deleted_entry_flag_array);
798 return n;
799 }
800 writen_num += move_length;
801 read_num += move_length;
802 }
803
804 zip->archive.m_archive_size -= (mz_uint64)deleted_length;
805 zip->archive.m_total_files =
806 (mz_uint32)entry_num - (mz_uint32)deleted_entry_num;
807
808 zip_central_dir_delete(pState, deleted_entry_flag_array, entry_num);
809 CLEANUP(deleted_entry_flag_array);
810
811 return (ssize_t)deleted_entry_num;
812 }
813
814 struct zip_t *zip_open(const char *zipname, int level, char mode) {
815 struct zip_t *zip = NULL;
816
817 if (!zipname || strlen(zipname) < 1) {
818 // zip_t archive name is empty or NULL
819 goto cleanup;
820 }
821
822 if (level < 0)
823 level = MZ_DEFAULT_LEVEL;
824 if ((level & 0xF) > MZ_UBER_COMPRESSION) {
825 // Wrong compression level
826 goto cleanup;
827 }
828
829 zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t));
830 if (!zip)
831 goto cleanup;
832
833 zip->level = (mz_uint)level;
834 switch (mode) {
835 case 'w':
836 // Create a new archive.
837 if (!mz_zip_writer_init_file_v2(&(zip->archive), zipname, 0,
838 MZ_ZIP_FLAG_WRITE_ZIP64)) {
839 // Cannot initialize zip_archive writer
840 goto cleanup;
841 }
842 break;
843
844 case 'r':
845 if (!mz_zip_reader_init_file_v2(
846 &(zip->archive), zipname,
847 zip->level | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0)) {
848 // An archive file does not exist or cannot initialize
849 // zip_archive reader
850 goto cleanup;
851 }
852 break;
853
854 case 'a':
855 case 'd':
856 if (!mz_zip_reader_init_file_v2_rpb(
857 &(zip->archive), zipname,
858 zip->level | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0)) {
859 // An archive file does not exist or cannot initialize
860 // zip_archive reader
861 goto cleanup;
862 }
863 if ((mode == 'a' || mode == 'd')) {
864 if (!mz_zip_writer_init_from_reader_v2_noreopen(&(zip->archive), zipname,
865 0)) {
866 mz_zip_reader_end(&(zip->archive));
867 goto cleanup;
868 }
869 }
870 break;
871
872 default:
873 goto cleanup;
874 }
875
876 return zip;
877
878 cleanup:
879 CLEANUP(zip);
880 return NULL;
881 }
882
883 void zip_close(struct zip_t *zip) {
884 if (zip) {
885 // Always finalize, even if adding failed for some reason, so we have a
886 // valid central directory.
887 mz_zip_writer_finalize_archive(&(zip->archive));
888 zip_archive_truncate(&(zip->archive));
889 mz_zip_writer_end(&(zip->archive));
890 mz_zip_reader_end(&(zip->archive));
891
892 CLEANUP(zip);
893 }
894 }
895
896 int zip_is64(struct zip_t *zip) {
897 if (!zip || !zip->archive.m_pState) {
898 // zip_t handler or zip state is not initialized
899 return ZIP_ENOINIT;
900 }
901
902 return (int)zip->archive.m_pState->m_zip64;
903 }
904
905 static int _zip_entry_open(struct zip_t *zip, const char *entryname,
906 int case_sensitive) {
907 size_t entrylen = 0;
908 mz_zip_archive *pzip = NULL;
909 mz_uint num_alignment_padding_bytes, level;
910 mz_zip_archive_file_stat stats;
911 int err = 0;
912 mz_uint16 dos_time, dos_date;
913 mz_uint32 extra_size = 0;
914 mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];
915 mz_uint64 local_dir_header_ofs = 0;
916
917 if (!zip) {
918 return ZIP_ENOINIT;
919 }
920
921 local_dir_header_ofs = zip->archive.m_archive_size;
922
923 if (!entryname) {
924 return ZIP_EINVENTNAME;
925 }
926
927 entrylen = strlen(entryname);
928 if (entrylen == 0) {
929 return ZIP_EINVENTNAME;
930 }
931
932 /*
933 .ZIP File Format Specification Version: 6.3.3
934
935 4.4.17.1 The name of the file, with optional relative path.
936 The path stored MUST not contain a drive or
937 device letter, or a leading slash. All slashes
938 MUST be forward slashes '/' as opposed to
939 backwards slashes '\' for compatibility with Amiga
940 and UNIX file systems etc. If input came from standard
941 input, there is no file name field.
942 */
943 if (zip->entry.name) {
944 CLEANUP(zip->entry.name);
945 }
946 #ifdef ZIP_RAW_ENTRYNAME
947 zip->entry.name = STRCLONE(entryname);
948 #else
949 zip->entry.name = zip_strrpl(entryname, entrylen, '\\', '/');
950 #endif
951
952 if (!zip->entry.name) {
953 // Cannot parse zip entry name
954 return ZIP_EINVENTNAME;
955 }
956
957 pzip = &(zip->archive);
958 if (pzip->m_zip_mode == MZ_ZIP_MODE_READING) {
959 zip->entry.index = (ssize_t)mz_zip_reader_locate_file(
960 pzip, zip->entry.name, NULL,
961 case_sensitive ? MZ_ZIP_FLAG_CASE_SENSITIVE : 0);
962 if (zip->entry.index < (ssize_t)0) {
963 err = ZIP_ENOENT;
964 goto cleanup;
965 }
966
967 if (!mz_zip_reader_file_stat(pzip, (mz_uint)zip->entry.index, &stats)) {
968 err = ZIP_ENOENT;
969 goto cleanup;
970 }
971
972 zip->entry.comp_size = stats.m_comp_size;
973 zip->entry.uncomp_size = stats.m_uncomp_size;
974 zip->entry.uncomp_crc32 = stats.m_crc32;
975 zip->entry.offset = stats.m_central_dir_ofs;
976 zip->entry.header_offset = stats.m_local_header_ofs;
977 zip->entry.method = stats.m_method;
978 zip->entry.external_attr = stats.m_external_attr;
979 #ifndef MINIZ_NO_TIME
980 zip->entry.m_time = stats.m_time;
981 #endif
982
983 return 0;
984 }
985
986 level = zip->level & 0xF;
987
988 zip->entry.index = (ssize_t)zip->archive.m_total_files;
989 zip->entry.comp_size = 0;
990 zip->entry.uncomp_size = 0;
991 zip->entry.uncomp_crc32 = MZ_CRC32_INIT;
992 zip->entry.offset = zip->archive.m_archive_size;
993 zip->entry.header_offset = zip->archive.m_archive_size;
994 memset(zip->entry.header, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE * sizeof(mz_uint8));
995 zip->entry.method = level ? MZ_DEFLATED : 0;
996
997 // UNIX or APPLE
998 #if MZ_PLATFORM == 3 || MZ_PLATFORM == 19
999 // regular file with rw-r--r-- permissions
1000 zip->entry.external_attr = (mz_uint32)(0100644) << 16;
1001 #else
1002 zip->entry.external_attr = 0;
1003 #endif
1004
1005 num_alignment_padding_bytes =
1006 mz_zip_writer_compute_padding_needed_for_file_alignment(pzip);
1007
1008 if (!pzip->m_pState || (pzip->m_zip_mode != MZ_ZIP_MODE_WRITING)) {
1009 // Invalid zip mode
1010 err = ZIP_EINVMODE;
1011 goto cleanup;
1012 }
1013 if (zip->level & MZ_ZIP_FLAG_COMPRESSED_DATA) {
1014 // Invalid zip compression level
1015 err = ZIP_EINVLVL;
1016 goto cleanup;
1017 }
1018
1019 if (!mz_zip_writer_write_zeros(pzip, zip->entry.offset,
1020 num_alignment_padding_bytes)) {
1021 // Cannot memset zip entry header
1022 err = ZIP_EMEMSET;
1023 goto cleanup;
1024 }
1025 local_dir_header_ofs += num_alignment_padding_bytes;
1026
1027 zip->entry.m_time = time(NULL);
1028 #ifndef MINIZ_NO_TIME
1029 mz_zip_time_t_to_dos_time(zip->entry.m_time, &dos_time, &dos_date);
1030 #endif
1031
1032 // ZIP64 header with NULL sizes (sizes will be in the data descriptor, just
1033 // after file data)
1034 extra_size = mz_zip_writer_create_zip64_extra_data(
1035 extra_data, NULL, NULL,
1036 (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
1037
1038 if (!mz_zip_writer_create_local_dir_header(
1039 pzip, zip->entry.header, entrylen, (mz_uint16)extra_size, 0, 0, 0,
1040 zip->entry.method,
1041 MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8 |
1042 MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR,
1043 dos_time, dos_date)) {
1044 // Cannot create zip entry header
1045 err = ZIP_EMEMSET;
1046 goto cleanup;
1047 }
1048
1049 zip->entry.header_offset = zip->entry.offset + num_alignment_padding_bytes;
1050
1051 if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.header_offset,
1052 zip->entry.header,
1053 sizeof(zip->entry.header)) != sizeof(zip->entry.header)) {
1054 // Cannot write zip entry header
1055 err = ZIP_EMEMSET;
1056 goto cleanup;
1057 }
1058
1059 if (pzip->m_file_offset_alignment) {
1060 MZ_ASSERT(
1061 (zip->entry.header_offset & (pzip->m_file_offset_alignment - 1)) == 0);
1062 }
1063 zip->entry.offset += num_alignment_padding_bytes + sizeof(zip->entry.header);
1064
1065 if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, zip->entry.name,
1066 entrylen) != entrylen) {
1067 // Cannot write data to zip entry
1068 err = ZIP_EWRTENT;
1069 goto cleanup;
1070 }
1071
1072 zip->entry.offset += entrylen;
1073
1074 if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, extra_data,
1075 extra_size) != extra_size) {
1076 // Cannot write ZIP64 data to zip entry
1077 err = ZIP_EWRTENT;
1078 goto cleanup;
1079 }
1080 zip->entry.offset += extra_size;
1081
1082 if (level) {
1083 zip->entry.state.m_pZip = pzip;
1084 zip->entry.state.m_cur_archive_file_ofs = zip->entry.offset;
1085 zip->entry.state.m_comp_size = 0;
1086
1087 if (tdefl_init(&(zip->entry.comp), mz_zip_writer_add_put_buf_callback,
1088 &(zip->entry.state),
1089 (int)tdefl_create_comp_flags_from_zip_params(
1090 (int)level, -15, MZ_DEFAULT_STRATEGY)) !=
1091 TDEFL_STATUS_OKAY) {
1092 // Cannot initialize the zip compressor
1093 err = ZIP_ETDEFLINIT;
1094 goto cleanup;
1095 }
1096 }
1097
1098 return 0;
1099
1100 cleanup:
1101 CLEANUP(zip->entry.name);
1102 return err;
1103 }
1104
1105 int zip_entry_open(struct zip_t *zip, const char *entryname) {
1106 return _zip_entry_open(zip, entryname, 0);
1107 }
1108
1109 int zip_entry_opencasesensitive(struct zip_t *zip, const char *entryname) {
1110 return _zip_entry_open(zip, entryname, 1);
1111 }
1112
1113 int zip_entry_openbyindex(struct zip_t *zip, size_t index) {
1114 mz_zip_archive *pZip = NULL;
1115 mz_zip_archive_file_stat stats;
1116 mz_uint namelen;
1117 const mz_uint8 *pHeader;
1118 const char *pFilename;
1119
1120 if (!zip) {
1121 // zip_t handler is not initialized
1122 return ZIP_ENOINIT;
1123 }
1124
1125 pZip = &(zip->archive);
1126 if (pZip->m_zip_mode != MZ_ZIP_MODE_READING) {
1127 // open by index requires readonly mode
1128 return ZIP_EINVMODE;
1129 }
1130
1131 if (index >= (size_t)pZip->m_total_files) {
1132 // index out of range
1133 return ZIP_EINVIDX;
1134 }
1135
1136 if (!(pHeader = &MZ_ZIP_ARRAY_ELEMENT(
1137 &pZip->m_pState->m_central_dir, mz_uint8,
1138 MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets,
1139 mz_uint32, index)))) {
1140 // cannot find header in central directory
1141 return ZIP_ENOHDR;
1142 }
1143
1144 namelen = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS);
1145 pFilename = (const char *)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
1146
1147 /*
1148 .ZIP File Format Specification Version: 6.3.3
1149
1150 4.4.17.1 The name of the file, with optional relative path.
1151 The path stored MUST not contain a drive or
1152 device letter, or a leading slash. All slashes
1153 MUST be forward slashes '/' as opposed to
1154 backwards slashes '\' for compatibility with Amiga
1155 and UNIX file systems etc. If input came from standard
1156 input, there is no file name field.
1157 */
1158 if (zip->entry.name) {
1159 CLEANUP(zip->entry.name);
1160 }
1161 #ifdef ZIP_RAW_ENTRYNAME
1162 zip->entry.name = STRCLONE(pFilename);
1163 #else
1164 zip->entry.name = zip_strrpl(pFilename, namelen, '\\', '/');
1165 #endif
1166
1167 if (!zip->entry.name) {
1168 // local entry name is NULL
1169 return ZIP_EINVENTNAME;
1170 }
1171
1172 if (!mz_zip_reader_file_stat(pZip, (mz_uint)index, &stats)) {
1173 return ZIP_ENOENT;
1174 }
1175
1176 zip->entry.index = (ssize_t)index;
1177 zip->entry.comp_size = stats.m_comp_size;
1178 zip->entry.uncomp_size = stats.m_uncomp_size;
1179 zip->entry.uncomp_crc32 = stats.m_crc32;
1180 zip->entry.offset = stats.m_central_dir_ofs;
1181 zip->entry.header_offset = stats.m_local_header_ofs;
1182 zip->entry.method = stats.m_method;
1183 zip->entry.external_attr = stats.m_external_attr;
1184 #ifndef MINIZ_NO_TIME
1185 zip->entry.m_time = stats.m_time;
1186 #endif
1187
1188 return 0;
1189 }
1190
1191 int zip_entry_close(struct zip_t *zip) {
1192 mz_zip_archive *pzip = NULL;
1193 mz_uint level;
1194 tdefl_status done;
1195 mz_uint16 entrylen;
1196 mz_uint16 dos_time = 0, dos_date = 0;
1197 int err = 0;
1198 mz_uint8 *pExtra_data = NULL;
1199 mz_uint32 extra_size = 0;
1200 mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];
1201 mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64];
1202 mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64;
1203
1204 if (!zip) {
1205 // zip_t handler is not initialized
1206 err = ZIP_ENOINIT;
1207 goto cleanup;
1208 }
1209
1210 pzip = &(zip->archive);
1211 if (pzip->m_zip_mode == MZ_ZIP_MODE_READING) {
1212 goto cleanup;
1213 }
1214
1215 level = zip->level & 0xF;
1216 if (level) {
1217 done = tdefl_compress_buffer(&(zip->entry.comp), "", 0, TDEFL_FINISH);
1218 if (done != TDEFL_STATUS_DONE && done != TDEFL_STATUS_OKAY) {
1219 // Cannot flush compressed buffer
1220 err = ZIP_ETDEFLBUF;
1221 goto cleanup;
1222 }
1223 zip->entry.comp_size = zip->entry.state.m_comp_size;
1224 zip->entry.offset = zip->entry.state.m_cur_archive_file_ofs;
1225 zip->entry.method = MZ_DEFLATED;
1226 }
1227
1228 entrylen = (mz_uint16)strlen(zip->entry.name);
1229 #ifndef MINIZ_NO_TIME
1230 mz_zip_time_t_to_dos_time(zip->entry.m_time, &dos_time, &dos_date);
1231 #endif
1232
1233 MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID);
1234 MZ_WRITE_LE32(local_dir_footer + 4, zip->entry.uncomp_crc32);
1235 MZ_WRITE_LE64(local_dir_footer + 8, zip->entry.comp_size);
1236 MZ_WRITE_LE64(local_dir_footer + 16, zip->entry.uncomp_size);
1237
1238 if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, local_dir_footer,
1239 local_dir_footer_size) != local_dir_footer_size) {
1240 // Cannot write zip entry header
1241 err = ZIP_EWRTHDR;
1242 goto cleanup;
1243 }
1244 zip->entry.offset += local_dir_footer_size;
1245
1246 pExtra_data = extra_data;
1247 extra_size = mz_zip_writer_create_zip64_extra_data(
1248 extra_data,
1249 (zip->entry.uncomp_size >= MZ_UINT32_MAX) ? &zip->entry.uncomp_size
1250 : NULL,
1251 (zip->entry.comp_size >= MZ_UINT32_MAX) ? &zip->entry.comp_size : NULL,
1252 (zip->entry.header_offset >= MZ_UINT32_MAX) ? &zip->entry.header_offset
1253 : NULL);
1254
1255 if ((entrylen) && (zip->entry.name[entrylen - 1] == '/') &&
1256 !zip->entry.uncomp_size) {
1257 /* Set DOS Subdirectory attribute bit. */
1258 zip->entry.external_attr |= MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG;
1259 }
1260
1261 if (!mz_zip_writer_add_to_central_dir(
1262 pzip, zip->entry.name, entrylen, pExtra_data, (mz_uint16)extra_size,
1263 "", 0, zip->entry.uncomp_size, zip->entry.comp_size,
1264 zip->entry.uncomp_crc32, zip->entry.method,
1265 MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8 |
1266 MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR,
1267 dos_time, dos_date, zip->entry.header_offset,
1268 zip->entry.external_attr, NULL, 0)) {
1269 // Cannot write to zip central dir
1270 err = ZIP_EWRTDIR;
1271 goto cleanup;
1272 }
1273
1274 pzip->m_total_files++;
1275 pzip->m_archive_size = zip->entry.offset;
1276
1277 cleanup:
1278 if (zip) {
1279 zip->entry.m_time = 0;
1280 CLEANUP(zip->entry.name);
1281 }
1282 return err;
1283 }
1284
1285 const char *zip_entry_name(struct zip_t *zip) {
1286 if (!zip) {
1287 // zip_t handler is not initialized
1288 return NULL;
1289 }
1290
1291 return zip->entry.name;
1292 }
1293
1294 ssize_t zip_entry_index(struct zip_t *zip) {
1295 if (!zip) {
1296 // zip_t handler is not initialized
1297 return (ssize_t)ZIP_ENOINIT;
1298 }
1299
1300 return zip->entry.index;
1301 }
1302
1303 int zip_entry_isdir(struct zip_t *zip) {
1304 if (!zip) {
1305 // zip_t handler is not initialized
1306 return ZIP_ENOINIT;
1307 }
1308
1309 if (zip->entry.index < (ssize_t)0) {
1310 // zip entry is not opened
1311 return ZIP_EINVIDX;
1312 }
1313
1314 return (int)mz_zip_reader_is_file_a_directory(&zip->archive,
1315 (mz_uint)zip->entry.index);
1316 }
1317
1318 unsigned long long zip_entry_size(struct zip_t *zip) {
1319 return zip_entry_uncomp_size(zip);
1320 }
1321
1322 unsigned long long zip_entry_uncomp_size(struct zip_t *zip) {
1323 return zip ? zip->entry.uncomp_size : 0;
1324 }
1325
1326 unsigned long long zip_entry_comp_size(struct zip_t *zip) {
1327 return zip ? zip->entry.comp_size : 0;
1328 }
1329
1330 unsigned int zip_entry_crc32(struct zip_t *zip) {
1331 return zip ? zip->entry.uncomp_crc32 : 0;
1332 }
1333
1334 int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize) {
1335 mz_uint level;
1336 mz_zip_archive *pzip = NULL;
1337 tdefl_status status;
1338
1339 if (!zip) {
1340 // zip_t handler is not initialized
1341 return ZIP_ENOINIT;
1342 }
1343
1344 pzip = &(zip->archive);
1345 if (buf && bufsize > 0) {
1346 zip->entry.uncomp_size += bufsize;
1347 zip->entry.uncomp_crc32 = (mz_uint32)mz_crc32(
1348 zip->entry.uncomp_crc32, (const mz_uint8 *)buf, bufsize);
1349
1350 level = zip->level & 0xF;
1351 if (!level) {
1352 if ((pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, buf,
1353 bufsize) != bufsize)) {
1354 // Cannot write buffer
1355 return ZIP_EWRTENT;
1356 }
1357 zip->entry.offset += bufsize;
1358 zip->entry.comp_size += bufsize;
1359 } else {
1360 status = tdefl_compress_buffer(&(zip->entry.comp), buf, bufsize,
1361 TDEFL_NO_FLUSH);
1362 if (status != TDEFL_STATUS_DONE && status != TDEFL_STATUS_OKAY) {
1363 // Cannot compress buffer
1364 return ZIP_ETDEFLBUF;
1365 }
1366 }
1367 }
1368
1369 return 0;
1370 }
1371
1372 int zip_entry_fwrite(struct zip_t *zip, const char *filename) {
1373 int err = 0;
1374 size_t n = 0;
1375 MZ_FILE *stream = NULL;
1376 mz_uint8 buf[MZ_ZIP_MAX_IO_BUF_SIZE];
1377 struct MZ_FILE_STAT_STRUCT file_stat;
1378 mz_uint16 modes;
1379
1380 if (!zip) {
1381 // zip_t handler is not initialized
1382 return ZIP_ENOINIT;
1383 }
1384
1385 memset(buf, 0, MZ_ZIP_MAX_IO_BUF_SIZE);
1386 memset((void *)&file_stat, 0, sizeof(struct MZ_FILE_STAT_STRUCT));
1387 if (MZ_FILE_STAT(filename, &file_stat) != 0) {
1388 // problem getting information - check errno
1389 return ZIP_ENOENT;
1390 }
1391
1392 #if defined(_WIN32) || defined(__WIN32__) || defined(DJGPP)
1393 (void)modes; // unused
1394 #else
1395 /* Initialize with permission bits--which are not implementation-optional */
1396 modes = file_stat.st_mode &
1397 (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX);
1398 if (S_ISDIR(file_stat.st_mode))
1399 modes |= UNX_IFDIR;
1400 if (S_ISREG(file_stat.st_mode))
1401 modes |= UNX_IFREG;
1402 if (S_ISLNK(file_stat.st_mode))
1403 modes |= UNX_IFLNK;
1404 if (S_ISBLK(file_stat.st_mode))
1405 modes |= UNX_IFBLK;
1406 if (S_ISCHR(file_stat.st_mode))
1407 modes |= UNX_IFCHR;
1408 if (S_ISFIFO(file_stat.st_mode))
1409 modes |= UNX_IFIFO;
1410 if (S_ISSOCK(file_stat.st_mode))
1411 modes |= UNX_IFSOCK;
1412 zip->entry.external_attr = (modes << 16) | !(file_stat.st_mode & S_IWUSR);
1413 if ((file_stat.st_mode & S_IFMT) == S_IFDIR) {
1414 zip->entry.external_attr |= MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG;
1415 }
1416 #endif
1417
1418 zip->entry.m_time = file_stat.st_mtime;
1419
1420 if (!(stream = MZ_FOPEN(filename, "rb"))) {
1421 // Cannot open filename
1422 return ZIP_EOPNFILE;
1423 }
1424
1425 while ((n = fread(buf, sizeof(mz_uint8), MZ_ZIP_MAX_IO_BUF_SIZE, stream)) >
1426 0) {
1427 if (zip_entry_write(zip, buf, n) < 0) {
1428 err = ZIP_EWRTENT;
1429 break;
1430 }
1431 }
1432 fclose(stream);
1433
1434 return err;
1435 }
1436
1437 ssize_t zip_entry_read(struct zip_t *zip, void **buf, size_t *bufsize) {
1438 mz_zip_archive *pzip = NULL;
1439 mz_uint idx;
1440 size_t size = 0;
1441
1442 if (!zip) {
1443 // zip_t handler is not initialized
1444 return (ssize_t)ZIP_ENOINIT;
1445 }
1446
1447 pzip = &(zip->archive);
1448 if (pzip->m_zip_mode != MZ_ZIP_MODE_READING ||
1449 zip->entry.index < (ssize_t)0) {
1450 // the entry is not found or we do not have read access
1451 return (ssize_t)ZIP_ENOENT;
1452 }
1453
1454 idx = (mz_uint)zip->entry.index;
1455 if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
1456 // the entry is a directory
1457 return (ssize_t)ZIP_EINVENTTYPE;
1458 }
1459
1460 *buf = mz_zip_reader_extract_to_heap(pzip, idx, &size, 0);
1461 if (*buf && bufsize) {
1462 *bufsize = size;
1463 }
1464 return (ssize_t)size;
1465 }
1466
1467 ssize_t zip_entry_noallocread(struct zip_t *zip, void *buf, size_t bufsize) {
1468 mz_zip_archive *pzip = NULL;
1469
1470 if (!zip) {
1471 // zip_t handler is not initialized
1472 return (ssize_t)ZIP_ENOINIT;
1473 }
1474
1475 pzip = &(zip->archive);
1476 if (pzip->m_zip_mode != MZ_ZIP_MODE_READING ||
1477 zip->entry.index < (ssize_t)0) {
1478 // the entry is not found or we do not have read access
1479 return (ssize_t)ZIP_ENOENT;
1480 }
1481
1482 if (!mz_zip_reader_extract_to_mem_no_alloc(pzip, (mz_uint)zip->entry.index,
1483 buf, bufsize, 0, NULL, 0)) {
1484 return (ssize_t)ZIP_EMEMNOALLOC;
1485 }
1486
1487 return (ssize_t)zip->entry.uncomp_size;
1488 }
1489
1490 int zip_entry_fread(struct zip_t *zip, const char *filename) {
1491 mz_zip_archive *pzip = NULL;
1492 mz_uint idx;
1493 mz_uint32 xattr = 0;
1494 mz_zip_archive_file_stat info;
1495
1496 if (!zip) {
1497 // zip_t handler is not initialized
1498 return ZIP_ENOINIT;
1499 }
1500
1501 memset((void *)&info, 0, sizeof(mz_zip_archive_file_stat));
1502 pzip = &(zip->archive);
1503 if (pzip->m_zip_mode != MZ_ZIP_MODE_READING ||
1504 zip->entry.index < (ssize_t)0) {
1505 // the entry is not found or we do not have read access
1506 return ZIP_ENOENT;
1507 }
1508
1509 idx = (mz_uint)zip->entry.index;
1510 if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
1511 // the entry is a directory
1512 return ZIP_EINVENTTYPE;
1513 }
1514
1515 if (!mz_zip_reader_extract_to_file(pzip, idx, filename, 0)) {
1516 return ZIP_ENOFILE;
1517 }
1518
1519 #if defined(_MSC_VER) || defined(PS4)
1520 (void)xattr; // unused
1521 #else
1522 if (!mz_zip_reader_file_stat(pzip, idx, &info)) {
1523 // Cannot get information about zip archive;
1524 return ZIP_ENOFILE;
1525 }
1526
1527 xattr = (info.m_external_attr >> 16) & 0xFFFF;
1528 if (xattr > 0 && xattr <= MZ_UINT16_MAX) {
1529 if (CHMOD(filename, (mode_t)xattr) < 0) {
1530 return ZIP_ENOPERM;
1531 }
1532 }
1533 #endif
1534
1535 return 0;
1536 }
1537
1538 int zip_entry_extract(struct zip_t *zip,
1539 size_t (*on_extract)(void *arg, uint64_t offset,
1540 const void *buf, size_t bufsize),
1541 void *arg) {
1542 mz_zip_archive *pzip = NULL;
1543 mz_uint idx;
1544
1545 if (!zip) {
1546 // zip_t handler is not initialized
1547 return ZIP_ENOINIT;
1548 }
1549
1550 pzip = &(zip->archive);
1551 if (pzip->m_zip_mode != MZ_ZIP_MODE_READING ||
1552 zip->entry.index < (ssize_t)0) {
1553 // the entry is not found or we do not have read access
1554 return ZIP_ENOENT;
1555 }
1556
1557 idx = (mz_uint)zip->entry.index;
1558 return (mz_zip_reader_extract_to_callback(pzip, idx, on_extract, arg, 0))
1559 ? 0
1560 : ZIP_EINVIDX;
1561 }
1562
1563 ssize_t zip_entries_total(struct zip_t *zip) {
1564 if (!zip) {
1565 // zip_t handler is not initialized
1566 return ZIP_ENOINIT;
1567 }
1568
1569 return (ssize_t)zip->archive.m_total_files;
1570 }
1571
1572 ssize_t zip_entries_delete(struct zip_t *zip, char *const entries[],
1573 size_t len) {
1574 ssize_t n = 0;
1575 ssize_t err = 0;
1576 struct zip_entry_mark_t *entry_mark = NULL;
1577
1578 if (zip == NULL || (entries == NULL && len != 0)) {
1579 return ZIP_ENOINIT;
1580 }
1581
1582 if (entries == NULL && len == 0) {
1583 return 0;
1584 }
1585
1586 n = zip_entries_total(zip);
1587
1588 entry_mark = (struct zip_entry_mark_t *)calloc(
1589 (size_t)n, sizeof(struct zip_entry_mark_t));
1590 if (!entry_mark) {
1591 return ZIP_EOOMEM;
1592 }
1593
1594 zip->archive.m_zip_mode = MZ_ZIP_MODE_READING;
1595
1596 err = zip_entry_set(zip, entry_mark, n, entries, len);
1597 if (err < 0) {
1598 CLEANUP(entry_mark);
1599 return err;
1600 }
1601
1602 err = zip_entries_delete_mark(zip, entry_mark, (int)n);
1603 CLEANUP(entry_mark);
1604 return err;
1605 }
1606
1607 int zip_stream_extract(const char *stream, size_t size, const char *dir,
1608 int (*on_extract)(const char *filename, void *arg),
1609 void *arg) {
1610 mz_zip_archive zip_archive;
1611 if (!stream || !dir) {
1612 // Cannot parse zip archive stream
1613 return ZIP_ENOINIT;
1614 }
1615 if (!memset(&zip_archive, 0, sizeof(mz_zip_archive))) {
1616 // Cannot memset zip archive
1617 return ZIP_EMEMSET;
1618 }
1619 if (!mz_zip_reader_init_mem(&zip_archive, stream, size, 0)) {
1620 // Cannot initialize zip_archive reader
1621 return ZIP_ENOINIT;
1622 }
1623
1624 return zip_archive_extract(&zip_archive, dir, on_extract, arg);
1625 }
1626
1627 struct zip_t *zip_stream_open(const char *stream, size_t size, int level,
1628 char mode) {
1629 struct zip_t *zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t));
1630 if (!zip) {
1631 return NULL;
1632 }
1633
1634 if (level < 0) {
1635 level = MZ_DEFAULT_LEVEL;
1636 }
1637 if ((level & 0xF) > MZ_UBER_COMPRESSION) {
1638 // Wrong compression level
1639 goto cleanup;
1640 }
1641 zip->level = (mz_uint)level;
1642
1643 if ((stream != NULL) && (size > 0) && (mode == 'r')) {
1644 if (!mz_zip_reader_init_mem(&(zip->archive), stream, size, 0)) {
1645 goto cleanup;
1646 }
1647 } else if ((stream == NULL) && (size == 0) && (mode == 'w')) {
1648 // Create a new archive.
1649 if (!mz_zip_writer_init_heap(&(zip->archive), 0, 1024)) {
1650 // Cannot initialize zip_archive writer
1651 goto cleanup;
1652 }
1653 } else {
1654 goto cleanup;
1655 }
1656 return zip;
1657
1658 cleanup:
1659 CLEANUP(zip);
1660 return NULL;
1661 }
1662
1663 ssize_t zip_stream_copy(struct zip_t *zip, void **buf, size_t *bufsize) {
1664 size_t n;
1665
1666 if (!zip) {
1667 return (ssize_t)ZIP_ENOINIT;
1668 }
1669 zip_archive_finalize(&(zip->archive));
1670
1671 n = (size_t)zip->archive.m_archive_size;
1672 if (bufsize != NULL) {
1673 *bufsize = n;
1674 }
1675
1676 *buf = calloc(n, sizeof(unsigned char));
1677 memcpy(*buf, zip->archive.m_pState->m_pMem, n);
1678
1679 return (ssize_t)n;
1680 }
1681
1682 void zip_stream_close(struct zip_t *zip) {
1683 if (zip) {
1684 mz_zip_writer_end(&(zip->archive));
1685 mz_zip_reader_end(&(zip->archive));
1686 CLEANUP(zip);
1687 }
1688 }
1689
1690 int zip_create(const char *zipname, const char *filenames[], size_t len) {
1691 int err = 0;
1692 size_t i;
1693 mz_zip_archive zip_archive;
1694 struct MZ_FILE_STAT_STRUCT file_stat;
1695 mz_uint32 ext_attributes = 0;
1696 mz_uint16 modes;
1697
1698 if (!zipname || strlen(zipname) < 1) {
1699 // zip_t archive name is empty or NULL
1700 return ZIP_EINVZIPNAME;
1701 }
1702
1703 // Create a new archive.
1704 if (!memset(&(zip_archive), 0, sizeof(zip_archive))) {
1705 // Cannot memset zip archive
1706 return ZIP_EMEMSET;
1707 }
1708
1709 if (!mz_zip_writer_init_file(&zip_archive, zipname, 0)) {
1710 // Cannot initialize zip_archive writer
1711 return ZIP_ENOINIT;
1712 }
1713
1714 if (!memset((void *)&file_stat, 0, sizeof(struct MZ_FILE_STAT_STRUCT))) {
1715 return ZIP_EMEMSET;
1716 }
1717
1718 for (i = 0; i < len; ++i) {
1719 const char *name = filenames[i];
1720 if (!name) {
1721 err = ZIP_EINVENTNAME;
1722 break;
1723 }
1724
1725 if (MZ_FILE_STAT(name, &file_stat) != 0) {
1726 // problem getting information - check errno
1727 err = ZIP_ENOFILE;
1728 break;
1729 }
1730
1731 #if defined(_WIN32) || defined(__WIN32__) || defined(DJGPP)
1732 (void)modes; // unused
1733 #else
1734
1735 /* Initialize with permission bits--which are not implementation-optional */
1736 modes = file_stat.st_mode &
1737 (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX);
1738 if (S_ISDIR(file_stat.st_mode))
1739 modes |= UNX_IFDIR;
1740 if (S_ISREG(file_stat.st_mode))
1741 modes |= UNX_IFREG;
1742 if (S_ISLNK(file_stat.st_mode))
1743 modes |= UNX_IFLNK;
1744 if (S_ISBLK(file_stat.st_mode))
1745 modes |= UNX_IFBLK;
1746 if (S_ISCHR(file_stat.st_mode))
1747 modes |= UNX_IFCHR;
1748 if (S_ISFIFO(file_stat.st_mode))
1749 modes |= UNX_IFIFO;
1750 if (S_ISSOCK(file_stat.st_mode))
1751 modes |= UNX_IFSOCK;
1752 ext_attributes = (modes << 16) | !(file_stat.st_mode & S_IWUSR);
1753 if ((file_stat.st_mode & S_IFMT) == S_IFDIR) {
1754 ext_attributes |= MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG;
1755 }
1756 #endif
1757
1758 if (!mz_zip_writer_add_file(&zip_archive, zip_basename(name), name, "", 0,
1759 ZIP_DEFAULT_COMPRESSION_LEVEL,
1760 ext_attributes)) {
1761 // Cannot add file to zip_archive
1762 err = ZIP_ENOFILE;
1763 break;
1764 }
1765 }
1766
1767 mz_zip_writer_finalize_archive(&zip_archive);
1768 mz_zip_writer_end(&zip_archive);
1769 return err;
1770 }
1771
1772 int zip_extract(const char *zipname, const char *dir,
1773 int (*on_extract)(const char *filename, void *arg), void *arg) {
1774 mz_zip_archive zip_archive;
1775
1776 if (!zipname || !dir) {
1777 // Cannot parse zip archive name
1778 return ZIP_EINVZIPNAME;
1779 }
1780
1781 if (!memset(&zip_archive, 0, sizeof(mz_zip_archive))) {
1782 // Cannot memset zip archive
1783 return ZIP_EMEMSET;
1784 }
1785
1786 // Now try to open the archive.
1787 if (!mz_zip_reader_init_file(&zip_archive, zipname, 0)) {
1788 // Cannot initialize zip_archive reader
1789 return ZIP_ENOINIT;
1790 }
1791
1792 return zip_archive_extract(&zip_archive, dir, on_extract, arg);
1793 }