-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
zip.c
2081 lines (1777 loc) · 54.6 KB
/
zip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#define __STDC_WANT_LIB_EXT1__ 1
#include <errno.h>
#include <sys/stat.h>
#include <time.h>
#if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \
defined(__MINGW32__)
/* Win32, DOS, MSVC, MSVS */
#include <direct.h>
#define HAS_DEVICE(P) \
((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) && \
(P)[1] == ':')
#define FILESYSTEM_PREFIX_LEN(P) (HAS_DEVICE(P) ? 2 : 0)
#else
#include <unistd.h> // needed for symlink()
#endif
#ifdef __MINGW32__
#include <sys/types.h>
#include <unistd.h>
#endif
#include "miniz.h"
#include "zip.h"
#ifdef _MSC_VER
#include <io.h>
#define ftruncate(fd, sz) (-(_chsize_s((fd), (sz)) != 0))
#define fileno _fileno
#endif
#if defined(__TINYC__) && (defined(_WIN32) || defined(_WIN64))
#include <io.h>
#define ftruncate(fd, sz) (-(_chsize_s((fd), (sz)) != 0))
#define fileno _fileno
#endif
#ifndef HAS_DEVICE
#define HAS_DEVICE(P) 0
#endif
#ifndef FILESYSTEM_PREFIX_LEN
#define FILESYSTEM_PREFIX_LEN(P) 0
#endif
#ifndef ISSLASH
#define ISSLASH(C) ((C) == '/' || (C) == '\\')
#endif
#define CLEANUP(ptr) \
do { \
if (ptr) { \
free((void *)ptr); \
ptr = NULL; \
} \
} while (0)
#define UNX_IFDIR 0040000 /* Unix directory */
#define UNX_IFREG 0100000 /* Unix regular file */
#define UNX_IFSOCK 0140000 /* Unix socket (BSD, not SysV or Amiga) */
#define UNX_IFLNK 0120000 /* Unix symbolic link (not SysV, Amiga) */
#define UNX_IFBLK 0060000 /* Unix block special (not Amiga) */
#define UNX_IFCHR 0020000 /* Unix character special (not Amiga) */
#define UNX_IFIFO 0010000 /* Unix fifo (BCC, not MSC or Amiga) */
struct zip_entry_t {
ssize_t index;
char *name;
mz_uint64 uncomp_size;
mz_uint64 comp_size;
mz_uint32 uncomp_crc32;
mz_uint64 dir_offset;
mz_uint8 header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
mz_uint64 header_offset;
mz_uint16 method;
mz_zip_writer_add_state state;
tdefl_compressor comp;
mz_uint32 external_attr;
time_t m_time;
};
struct zip_t {
mz_zip_archive archive;
mz_uint level;
struct zip_entry_t entry;
};
enum zip_modify_t {
MZ_KEEP = 0,
MZ_DELETE = 1,
MZ_MOVE = 2,
};
struct zip_entry_mark_t {
ssize_t file_index;
enum zip_modify_t type;
mz_uint64 m_local_header_ofs;
size_t lf_length;
};
static const char *const zip_errlist[33] = {
NULL,
"not initialized\0",
"invalid entry name\0",
"entry not found\0",
"invalid zip mode\0",
"invalid compression level\0",
"no zip 64 support\0",
"memset error\0",
"cannot write data to entry\0",
"cannot initialize tdefl compressor\0",
"invalid index\0",
"header not found\0",
"cannot flush tdefl buffer\0",
"cannot write entry header\0",
"cannot create entry header\0",
"cannot write to central dir\0",
"cannot open file\0",
"invalid entry type\0",
"extracting data using no memory allocation\0",
"file not found\0",
"no permission\0",
"out of memory\0",
"invalid zip archive name\0",
"make dir error\0",
"symlink error\0",
"close archive error\0",
"capacity size too small\0",
"fseek error\0",
"fread error\0",
"fwrite error\0",
"cannot initialize reader\0",
"cannot initialize writer\0",
"cannot initialize writer from reader\0",
};
const char *zip_strerror(int errnum) {
errnum = -errnum;
if (errnum <= 0 || errnum >= 33) {
return NULL;
}
return zip_errlist[errnum];
}
static const char *zip_basename(const char *name) {
char const *p;
char const *base = name += FILESYSTEM_PREFIX_LEN(name);
int all_slashes = 1;
for (p = name; *p; p++) {
if (ISSLASH(*p))
base = p + 1;
else
all_slashes = 0;
}
/* If NAME is all slashes, arrange to return `/'. */
if (*base == '\0' && ISSLASH(*name) && all_slashes)
--base;
return base;
}
static int zip_mkpath(char *path) {
char *p;
char npath[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE + 1];
int len = 0;
int has_device = HAS_DEVICE(path);
memset(npath, 0, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE + 1);
if (has_device) {
// only on windows
npath[0] = path[0];
npath[1] = path[1];
len = 2;
}
for (p = path + len; *p && len < MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE; p++) {
if (ISSLASH(*p) && ((!has_device && len > 0) || (has_device && len > 2))) {
#if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \
defined(__MINGW32__)
#else
if ('\\' == *p) {
*p = '/';
}
#endif
if (MZ_MKDIR(npath) == -1) {
if (errno != EEXIST) {
return ZIP_EMKDIR;
}
}
}
npath[len++] = *p;
}
return 0;
}
static char *zip_strclone(const char *str, size_t n) {
char c;
size_t i;
char *rpl = (char *)calloc((1 + n), sizeof(char));
char *begin = rpl;
if (!rpl) {
return NULL;
}
for (i = 0; (i < n) && (c = *str++); ++i) {
*rpl++ = c;
}
return begin;
}
static char *zip_strrpl(const char *str, size_t n, char oldchar, char newchar) {
char c;
size_t i;
char *rpl = (char *)calloc((1 + n), sizeof(char));
char *begin = rpl;
if (!rpl) {
return NULL;
}
for (i = 0; (i < n) && (c = *str++); ++i) {
if (c == oldchar) {
c = newchar;
}
*rpl++ = c;
}
return begin;
}
static inline int zip_strchr_match(const char *const str, size_t len, char c) {
size_t i;
for (i = 0; i < len; ++i) {
if (str[i] != c) {
return 0;
}
}
return 1;
}
static char *zip_name_normalize(char *name, char *const nname, size_t len) {
size_t offn = 0, ncpy = 0;
char c;
if (name == NULL || nname == NULL || len <= 0) {
return NULL;
}
// skip trailing '/'
while (ISSLASH(*name)) {
name++;
}
while ((c = *name++)) {
if (ISSLASH(c)) {
if (ncpy > 0 && !zip_strchr_match(&nname[offn], ncpy, '.')) {
offn += ncpy;
nname[offn++] = c; // append '/'
}
ncpy = 0;
} else {
nname[offn + ncpy] = c;
if (c) {
ncpy++;
}
}
}
if (!zip_strchr_match(&nname[offn], ncpy, '.')) {
nname[offn + ncpy] = '\0';
} else {
nname[offn] = '\0';
}
return nname;
}
static int zip_archive_truncate(mz_zip_archive *pzip) {
mz_zip_internal_state *pState = pzip->m_pState;
mz_uint64 file_size = pzip->m_archive_size;
if ((pzip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem)) {
return 0;
}
if (pzip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED) {
if (pState->m_pFile) {
int fd = fileno(pState->m_pFile);
return ftruncate(fd, file_size);
}
}
return 0;
}
static int zip_archive_extract(mz_zip_archive *zip_archive, const char *dir,
int (*on_extract)(const char *filename,
void *arg),
void *arg) {
int err = 0;
mz_uint i, n;
char path[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE + 1];
char symlink_to[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE + 1];
mz_zip_archive_file_stat info;
size_t dirlen = 0, filename_size = MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE;
mz_uint32 xattr = 0;
memset(path, 0, sizeof(path));
memset(symlink_to, 0, sizeof(symlink_to));
dirlen = strlen(dir);
if (dirlen + 1 > MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE) {
return ZIP_EINVENTNAME;
}
memset((void *)&info, 0, sizeof(mz_zip_archive_file_stat));
#if defined(_MSC_VER)
strcpy_s(path, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE, dir);
#else
strncpy(path, dir, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE);
#endif
if (!ISSLASH(path[dirlen - 1])) {
#if defined(_WIN32) || defined(__WIN32__)
path[dirlen] = '\\';
#else
path[dirlen] = '/';
#endif
++dirlen;
}
if (filename_size > MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - dirlen) {
filename_size = MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - dirlen;
}
// Get and print information about each file in the archive.
n = mz_zip_reader_get_num_files(zip_archive);
for (i = 0; i < n; ++i) {
if (!mz_zip_reader_file_stat(zip_archive, i, &info)) {
// Cannot get information about zip archive;
err = ZIP_ENOENT;
goto out;
}
if (!zip_name_normalize(info.m_filename, info.m_filename,
strlen(info.m_filename))) {
// Cannot normalize file name;
err = ZIP_EINVENTNAME;
goto out;
}
#if defined(_MSC_VER)
strncpy_s(&path[dirlen], filename_size, info.m_filename, filename_size);
#else
strncpy(&path[dirlen], info.m_filename, filename_size);
#endif
err = zip_mkpath(path);
if (err < 0) {
// Cannot make a path
goto out;
}
if ((((info.m_version_made_by >> 8) == 3) ||
((info.m_version_made_by >> 8) ==
19)) // if zip is produced on Unix or macOS (3 and 19 from
// section 4.4.2.2 of zip standard)
&& info.m_external_attr &
(0x20 << 24)) { // and has sym link attribute (0x80 is file, 0x40
// is directory)
#if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \
defined(__MINGW32__)
#else
if (info.m_uncomp_size > MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE ||
!mz_zip_reader_extract_to_mem_no_alloc(
zip_archive, i, symlink_to, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE, 0,
NULL, 0)) {
err = ZIP_EMEMNOALLOC;
goto out;
}
symlink_to[info.m_uncomp_size] = '\0';
if (symlink(symlink_to, path) != 0) {
err = ZIP_ESYMLINK;
goto out;
}
#endif
} else {
if (!mz_zip_reader_is_file_a_directory(zip_archive, i)) {
if (!mz_zip_reader_extract_to_file(zip_archive, i, path, 0)) {
// Cannot extract zip archive to file
err = ZIP_ENOFILE;
goto out;
}
}
#if defined(_MSC_VER) || defined(PS4)
(void)xattr; // unused
#else
xattr = (info.m_external_attr >> 16) & 0xFFFF;
if (xattr > 0 && xattr <= MZ_UINT16_MAX) {
if (CHMOD(path, (mode_t)xattr) < 0) {
err = ZIP_ENOPERM;
goto out;
}
}
#endif
}
if (on_extract) {
if (on_extract(path, arg) < 0) {
goto out;
}
}
}
out:
// Close the archive, freeing any resources it was using
if (!mz_zip_reader_end(zip_archive)) {
// Cannot end zip reader
err = ZIP_ECLSZIP;
}
return err;
}
static inline void zip_archive_finalize(mz_zip_archive *pzip) {
mz_zip_writer_finalize_archive(pzip);
zip_archive_truncate(pzip);
}
static ssize_t zip_entry_mark(struct zip_t *zip,
struct zip_entry_mark_t *entry_mark,
const ssize_t n, char *const entries[],
const size_t len) {
ssize_t i = 0;
ssize_t err = 0;
if (!zip || !entry_mark || !entries) {
return ZIP_ENOINIT;
}
mz_zip_archive_file_stat file_stat;
mz_uint64 d_pos = UINT64_MAX;
for (i = 0; i < n; ++i) {
if ((err = zip_entry_openbyindex(zip, i))) {
return (ssize_t)err;
}
mz_bool name_matches = MZ_FALSE;
{
size_t j;
for (j = 0; j < len; ++j) {
if (strcmp(zip->entry.name, entries[j]) == 0) {
name_matches = MZ_TRUE;
break;
}
}
}
if (name_matches) {
entry_mark[i].type = MZ_DELETE;
} else {
entry_mark[i].type = MZ_KEEP;
}
if (!mz_zip_reader_file_stat(&zip->archive, i, &file_stat)) {
return ZIP_ENOENT;
}
zip_entry_close(zip);
entry_mark[i].m_local_header_ofs = file_stat.m_local_header_ofs;
entry_mark[i].file_index = (ssize_t)-1;
entry_mark[i].lf_length = 0;
if ((entry_mark[i].type) == MZ_DELETE &&
(d_pos > entry_mark[i].m_local_header_ofs)) {
d_pos = entry_mark[i].m_local_header_ofs;
}
}
for (i = 0; i < n; ++i) {
if ((entry_mark[i].m_local_header_ofs > d_pos) &&
(entry_mark[i].type != MZ_DELETE)) {
entry_mark[i].type = MZ_MOVE;
}
}
return err;
}
static ssize_t zip_entry_markbyindex(struct zip_t *zip,
struct zip_entry_mark_t *entry_mark,
const ssize_t n, size_t entries[],
const size_t len) {
ssize_t i = 0;
ssize_t err = 0;
if (!zip || !entry_mark || !entries) {
return ZIP_ENOINIT;
}
mz_zip_archive_file_stat file_stat;
mz_uint64 d_pos = UINT64_MAX;
for (i = 0; i < n; ++i) {
if ((err = zip_entry_openbyindex(zip, i))) {
return (ssize_t)err;
}
mz_bool matches = MZ_FALSE;
{
size_t j;
for (j = 0; j < len; ++j) {
if ((size_t)i == entries[j]) {
matches = MZ_TRUE;
break;
}
}
}
if (matches) {
entry_mark[i].type = MZ_DELETE;
} else {
entry_mark[i].type = MZ_KEEP;
}
if (!mz_zip_reader_file_stat(&zip->archive, i, &file_stat)) {
return ZIP_ENOENT;
}
zip_entry_close(zip);
entry_mark[i].m_local_header_ofs = file_stat.m_local_header_ofs;
entry_mark[i].file_index = (ssize_t)-1;
entry_mark[i].lf_length = 0;
if ((entry_mark[i].type) == MZ_DELETE &&
(d_pos > entry_mark[i].m_local_header_ofs)) {
d_pos = entry_mark[i].m_local_header_ofs;
}
}
for (i = 0; i < n; ++i) {
if ((entry_mark[i].m_local_header_ofs > d_pos) &&
(entry_mark[i].type != MZ_DELETE)) {
entry_mark[i].type = MZ_MOVE;
}
}
return err;
}
static ssize_t zip_index_next(mz_uint64 *local_header_ofs_array,
ssize_t cur_index) {
ssize_t new_index = 0, i;
for (i = cur_index - 1; i >= 0; --i) {
if (local_header_ofs_array[cur_index] > local_header_ofs_array[i]) {
new_index = i + 1;
return new_index;
}
}
return new_index;
}
static ssize_t zip_sort(mz_uint64 *local_header_ofs_array, ssize_t cur_index) {
ssize_t nxt_index = zip_index_next(local_header_ofs_array, cur_index);
if (nxt_index != cur_index) {
mz_uint64 temp = local_header_ofs_array[cur_index];
ssize_t i;
for (i = cur_index; i > nxt_index; i--) {
local_header_ofs_array[i] = local_header_ofs_array[i - 1];
}
local_header_ofs_array[nxt_index] = temp;
}
return nxt_index;
}
static int zip_index_update(struct zip_entry_mark_t *entry_mark,
ssize_t last_index, ssize_t nxt_index) {
ssize_t j;
for (j = 0; j < last_index; j++) {
if (entry_mark[j].file_index >= nxt_index) {
entry_mark[j].file_index += 1;
}
}
entry_mark[nxt_index].file_index = last_index;
return 0;
}
static int zip_entry_finalize(struct zip_t *zip,
struct zip_entry_mark_t *entry_mark,
const ssize_t n) {
ssize_t i = 0;
mz_uint64 *local_header_ofs_array = (mz_uint64 *)calloc(n, sizeof(mz_uint64));
if (!local_header_ofs_array) {
return ZIP_EOOMEM;
}
for (i = 0; i < n; ++i) {
local_header_ofs_array[i] = entry_mark[i].m_local_header_ofs;
ssize_t index = zip_sort(local_header_ofs_array, i);
if (index != i) {
zip_index_update(entry_mark, i, index);
}
entry_mark[i].file_index = index;
}
size_t *length = (size_t *)calloc(n, sizeof(size_t));
if (!length) {
CLEANUP(local_header_ofs_array);
return ZIP_EOOMEM;
}
for (i = 0; i < n - 1; i++) {
length[i] =
(size_t)(local_header_ofs_array[i + 1] - local_header_ofs_array[i]);
}
length[n - 1] =
(size_t)(zip->archive.m_archive_size - local_header_ofs_array[n - 1]);
for (i = 0; i < n; i++) {
entry_mark[i].lf_length = length[entry_mark[i].file_index];
}
CLEANUP(length);
CLEANUP(local_header_ofs_array);
return 0;
}
static ssize_t zip_entry_set(struct zip_t *zip,
struct zip_entry_mark_t *entry_mark, ssize_t n,
char *const entries[], const size_t len) {
ssize_t err = 0;
if ((err = zip_entry_mark(zip, entry_mark, n, entries, len)) < 0) {
return err;
}
if ((err = zip_entry_finalize(zip, entry_mark, n)) < 0) {
return err;
}
return 0;
}
static ssize_t zip_entry_setbyindex(struct zip_t *zip,
struct zip_entry_mark_t *entry_mark,
ssize_t n, size_t entries[],
const size_t len) {
ssize_t err = 0;
if ((err = zip_entry_markbyindex(zip, entry_mark, n, entries, len)) < 0) {
return err;
}
if ((err = zip_entry_finalize(zip, entry_mark, n)) < 0) {
return err;
}
return 0;
}
static ssize_t zip_mem_move(void *pBuf, size_t bufSize, const mz_uint64 to,
const mz_uint64 from, const size_t length) {
uint8_t *dst = NULL, *src = NULL, *end = NULL;
if (!pBuf) {
return ZIP_EINVIDX;
}
end = (uint8_t *)pBuf + bufSize;
if (to > bufSize) {
return ZIP_EINVIDX;
}
if (from > bufSize) {
return ZIP_EINVIDX;
}
dst = (uint8_t *)pBuf + to;
src = (uint8_t *)pBuf + from;
if (((dst + length) > end) || ((src + length) > end)) {
return ZIP_EINVIDX;
}
memmove(dst, src, length);
return length;
}
static ssize_t zip_file_move(MZ_FILE *m_pFile, const mz_uint64 to,
const mz_uint64 from, const size_t length,
mz_uint8 *move_buf, const size_t capacity_size) {
if (length > capacity_size) {
return ZIP_ECAPSIZE;
}
if (MZ_FSEEK64(m_pFile, from, SEEK_SET)) {
return ZIP_EFSEEK;
}
if (fread(move_buf, 1, length, m_pFile) != length) {
return ZIP_EFREAD;
}
if (MZ_FSEEK64(m_pFile, to, SEEK_SET)) {
return ZIP_EFSEEK;
}
if (fwrite(move_buf, 1, length, m_pFile) != length) {
return ZIP_EFWRITE;
}
return (ssize_t)length;
}
static ssize_t zip_files_move(struct zip_t *zip, mz_uint64 writen_num,
mz_uint64 read_num, size_t length) {
ssize_t n = 0;
const size_t page_size = 1 << 12; // 4K
mz_zip_internal_state *pState = zip->archive.m_pState;
mz_uint8 *move_buf = (mz_uint8 *)calloc(1, page_size);
if (!move_buf) {
return ZIP_EOOMEM;
}
ssize_t moved_length = 0;
ssize_t move_count = 0;
while ((mz_int64)length > 0) {
move_count = (length >= page_size) ? page_size : length;
if (pState->m_pFile) {
n = zip_file_move(pState->m_pFile, writen_num, read_num, move_count,
move_buf, page_size);
} else if (pState->m_pMem) {
n = zip_mem_move(pState->m_pMem, pState->m_mem_size, writen_num, read_num,
move_count);
} else {
return ZIP_ENOFILE;
}
if (n < 0) {
moved_length = n;
goto cleanup;
}
if (n != move_count) {
goto cleanup;
}
writen_num += move_count;
read_num += move_count;
length -= move_count;
moved_length += move_count;
}
cleanup:
CLEANUP(move_buf);
return moved_length;
}
static int zip_central_dir_move(mz_zip_internal_state *pState, int begin,
int end, int entry_num) {
if (begin == entry_num) {
return 0;
}
size_t l_size = 0;
size_t r_size = 0;
mz_uint32 d_size = 0;
mz_uint8 *next = NULL;
mz_uint8 *deleted = &MZ_ZIP_ARRAY_ELEMENT(
&pState->m_central_dir, mz_uint8,
MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, begin));
l_size = (size_t)(deleted - (mz_uint8 *)(pState->m_central_dir.m_p));
if (end == entry_num) {
r_size = 0;
} else {
next = &MZ_ZIP_ARRAY_ELEMENT(
&pState->m_central_dir, mz_uint8,
MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, end));
r_size = pState->m_central_dir.m_size -
(mz_uint32)(next - (mz_uint8 *)(pState->m_central_dir.m_p));
d_size = (mz_uint32)(next - deleted);
}
if (next && l_size == 0) {
memmove(pState->m_central_dir.m_p, next, r_size);
pState->m_central_dir.m_p = MZ_REALLOC(pState->m_central_dir.m_p, r_size);
{
int i;
for (i = end; i < entry_num; i++) {
MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, i) -=
d_size;
}
}
}
if (next && l_size * r_size != 0) {
memmove(deleted, next, r_size);
{
int i;
for (i = end; i < entry_num; i++) {
MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, i) -=
d_size;
}
}
}
pState->m_central_dir.m_size = l_size + r_size;
return 0;
}
static int zip_central_dir_delete(mz_zip_internal_state *pState,
int *deleted_entry_index_array,
int entry_num) {
int i = 0;
int begin = 0;
int end = 0;
int d_num = 0;
while (i < entry_num) {
while ((i < entry_num) && (!deleted_entry_index_array[i])) {
i++;
}
begin = i;
while ((i < entry_num) && (deleted_entry_index_array[i])) {
i++;
}
end = i;
zip_central_dir_move(pState, begin, end, entry_num);
}
i = 0;
while (i < entry_num) {
while ((i < entry_num) && (!deleted_entry_index_array[i])) {
i++;
}
begin = i;
if (begin == entry_num) {
break;
}
while ((i < entry_num) && (deleted_entry_index_array[i])) {
i++;
}
end = i;
int k = 0, j;
for (j = end; j < entry_num; j++) {
MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32,
begin + k) =
(mz_uint32)MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets,
mz_uint32, j);
k++;
}
d_num += end - begin;
}
pState->m_central_dir_offsets.m_size =
sizeof(mz_uint32) * (entry_num - d_num);
return 0;
}
static ssize_t zip_entries_delete_mark(struct zip_t *zip,
struct zip_entry_mark_t *entry_mark,
int entry_num) {
mz_uint64 writen_num = 0;
mz_uint64 read_num = 0;
size_t deleted_length = 0;
size_t move_length = 0;
int i = 0;
size_t deleted_entry_num = 0;
ssize_t n = 0;
mz_bool *deleted_entry_flag_array =
(mz_bool *)calloc(entry_num, sizeof(mz_bool));
if (deleted_entry_flag_array == NULL) {
return ZIP_EOOMEM;
}
mz_zip_internal_state *pState = zip->archive.m_pState;
zip->archive.m_zip_mode = MZ_ZIP_MODE_WRITING;
if (pState->m_pFile) {
if (MZ_FSEEK64(pState->m_pFile, 0, SEEK_SET)) {
CLEANUP(deleted_entry_flag_array);
return ZIP_ENOENT;
}
}
while (i < entry_num) {
while ((i < entry_num) && (entry_mark[i].type == MZ_KEEP)) {
writen_num += entry_mark[i].lf_length;
read_num = writen_num;
i++;
}
while ((i < entry_num) && (entry_mark[i].type == MZ_DELETE)) {
deleted_entry_flag_array[i] = MZ_TRUE;
read_num += entry_mark[i].lf_length;
deleted_length += entry_mark[i].lf_length;
i++;
deleted_entry_num++;
}
while ((i < entry_num) && (entry_mark[i].type == MZ_MOVE)) {
move_length += entry_mark[i].lf_length;
mz_uint8 *p = &MZ_ZIP_ARRAY_ELEMENT(
&pState->m_central_dir, mz_uint8,
MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, i));
if (!p) {
CLEANUP(deleted_entry_flag_array);
return ZIP_ENOENT;
}
mz_uint32 offset = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS);
offset -= (mz_uint32)deleted_length;
MZ_WRITE_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS, offset);
i++;
}
n = zip_files_move(zip, writen_num, read_num, move_length);
if (n != (ssize_t)move_length) {
CLEANUP(deleted_entry_flag_array);
return n;
}
writen_num += move_length;
read_num += move_length;
}
zip->archive.m_archive_size -= (mz_uint64)deleted_length;
zip->archive.m_total_files =
(mz_uint32)entry_num - (mz_uint32)deleted_entry_num;
zip_central_dir_delete(pState, deleted_entry_flag_array, entry_num);
CLEANUP(deleted_entry_flag_array);
return (ssize_t)deleted_entry_num;
}
struct zip_t *zip_open(const char *zipname, int level, char mode) {
int errnum = 0;
return zip_openwitherror(zipname, level, mode, &errnum);
}
struct zip_t *zip_openwitherror(const char *zipname, int level, char mode,
int *errnum) {
struct zip_t *zip = NULL;
*errnum = 0;
if (!zipname || strlen(zipname) < 1) {
// zip_t archive name is empty or NULL
*errnum = ZIP_EINVZIPNAME;
goto cleanup;
}
if (level < 0)
level = MZ_DEFAULT_LEVEL;
if ((level & 0xF) > MZ_UBER_COMPRESSION) {
// Wrong compression level
*errnum = ZIP_EINVLVL;
goto cleanup;
}
zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t));
if (!zip) {
// out of memory
*errnum = ZIP_EOOMEM;
goto cleanup;
}
zip->level = (mz_uint)level;
zip->entry.index = -1;
switch (mode) {
case 'w':
// Create a new archive.
if (!mz_zip_writer_init_file_v2(&(zip->archive), zipname, 0,
MZ_ZIP_FLAG_WRITE_ZIP64)) {
// Cannot initialize zip_archive writer
*errnum = ZIP_EWINIT;
goto cleanup;
}
break;
case 'r':
if (!mz_zip_reader_init_file_v2(
&(zip->archive), zipname,
zip->level | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0)) {
// An archive file does not exist or cannot initialize
// zip_archive reader
*errnum = ZIP_ERINIT;
goto cleanup;
}
break;
case 'a':
case 'd':
if (!mz_zip_reader_init_file_v2_rpb(
&(zip->archive), zipname,
zip->level | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0)) {
// An archive file does not exist or cannot initialize