forked from zlib-ng/minizip-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mz_compat.c
1375 lines (1134 loc) · 43.5 KB
/
mz_compat.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
/* mz_compat.c -- Backwards compatible interface for older versions
part of the minizip-ng project
Copyright (C) Nathan Moinvaziri
https://github.com/zlib-ng/minizip-ng
Copyright (C) 1998-2010 Gilles Vollant
https://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_mem.h"
#include "mz_strm_os.h"
#include "mz_strm_zlib.h"
#include "mz_zip.h"
#include <stdio.h> /* SEEK */
#include "mz_compat.h"
/***************************************************************************/
typedef struct mz_compat_s {
void *stream;
void *handle;
uint64_t entry_index;
int64_t entry_pos;
int64_t total_out;
} mz_compat;
/***************************************************************************/
typedef struct mz_stream_ioapi_s {
mz_stream stream;
void *handle;
zlib_filefunc_def filefunc;
zlib_filefunc64_def filefunc64;
} mz_stream_ioapi;
/***************************************************************************/
static int32_t mz_stream_ioapi_open(void *stream, const char *path, int32_t mode);
static int32_t mz_stream_ioapi_is_open(void *stream);
static int32_t mz_stream_ioapi_read(void *stream, void *buf, int32_t size);
static int32_t mz_stream_ioapi_write(void *stream, const void *buf, int32_t size);
static int64_t mz_stream_ioapi_tell(void *stream);
static int32_t mz_stream_ioapi_seek(void *stream, int64_t offset, int32_t origin);
static int32_t mz_stream_ioapi_close(void *stream);
static int32_t mz_stream_ioapi_error(void *stream);
static void *mz_stream_ioapi_create(void);
static void mz_stream_ioapi_delete(void **stream);
/***************************************************************************/
static mz_stream_vtbl mz_stream_ioapi_vtbl = {
mz_stream_ioapi_open,
mz_stream_ioapi_is_open,
mz_stream_ioapi_read,
mz_stream_ioapi_write,
mz_stream_ioapi_tell,
mz_stream_ioapi_seek,
mz_stream_ioapi_close,
mz_stream_ioapi_error,
mz_stream_ioapi_create,
mz_stream_ioapi_delete,
NULL,
NULL
};
/***************************************************************************/
static int32_t mz_stream_ioapi_open(void *stream, const char *path, int32_t mode) {
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
int32_t ioapi_mode = 0;
if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ)
ioapi_mode = ZLIB_FILEFUNC_MODE_READ;
else if (mode & MZ_OPEN_MODE_APPEND)
ioapi_mode = ZLIB_FILEFUNC_MODE_EXISTING;
else if (mode & MZ_OPEN_MODE_CREATE)
ioapi_mode = ZLIB_FILEFUNC_MODE_CREATE;
else
return MZ_OPEN_ERROR;
if (ioapi->filefunc64.zopen64_file)
ioapi->handle = ioapi->filefunc64.zopen64_file(ioapi->filefunc64.opaque, path, ioapi_mode);
else if (ioapi->filefunc.zopen_file)
ioapi->handle = ioapi->filefunc.zopen_file(ioapi->filefunc.opaque, path, ioapi_mode);
if (!ioapi->handle)
return MZ_PARAM_ERROR;
return MZ_OK;
}
static int32_t mz_stream_ioapi_is_open(void *stream) {
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
if (!ioapi->handle)
return MZ_OPEN_ERROR;
return MZ_OK;
}
static int32_t mz_stream_ioapi_read(void *stream, void *buf, int32_t size) {
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
read_file_func zread = NULL;
void *opaque = NULL;
if (mz_stream_ioapi_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
if (ioapi->filefunc64.zread_file) {
zread = ioapi->filefunc64.zread_file;
opaque = ioapi->filefunc64.opaque;
} else if (ioapi->filefunc.zread_file) {
zread = ioapi->filefunc.zread_file;
opaque = ioapi->filefunc.opaque;
} else
return MZ_PARAM_ERROR;
return (int32_t)zread(opaque, ioapi->handle, buf, size);
}
static int32_t mz_stream_ioapi_write(void *stream, const void *buf, int32_t size) {
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
write_file_func zwrite = NULL;
int32_t written = 0;
void *opaque = NULL;
if (mz_stream_ioapi_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
if (ioapi->filefunc64.zwrite_file) {
zwrite = ioapi->filefunc64.zwrite_file;
opaque = ioapi->filefunc64.opaque;
} else if (ioapi->filefunc.zwrite_file) {
zwrite = ioapi->filefunc.zwrite_file;
opaque = ioapi->filefunc.opaque;
} else
return MZ_PARAM_ERROR;
written = (int32_t)zwrite(opaque, ioapi->handle, buf, size);
return written;
}
static int64_t mz_stream_ioapi_tell(void *stream) {
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
if (mz_stream_ioapi_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
if (ioapi->filefunc64.ztell64_file)
return ioapi->filefunc64.ztell64_file(ioapi->filefunc64.opaque, ioapi->handle);
else if (ioapi->filefunc.ztell_file)
return ioapi->filefunc.ztell_file(ioapi->filefunc.opaque, ioapi->handle);
return MZ_INTERNAL_ERROR;
}
static int32_t mz_stream_ioapi_seek(void *stream, int64_t offset, int32_t origin) {
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
if (mz_stream_ioapi_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
if (ioapi->filefunc64.zseek64_file) {
if (ioapi->filefunc64.zseek64_file(ioapi->filefunc64.opaque, ioapi->handle, offset, origin) != 0)
return MZ_INTERNAL_ERROR;
} else if (ioapi->filefunc.zseek_file) {
if (ioapi->filefunc.zseek_file(ioapi->filefunc.opaque, ioapi->handle, (int32_t)offset, origin) != 0)
return MZ_INTERNAL_ERROR;
} else
return MZ_PARAM_ERROR;
return MZ_OK;
}
static int32_t mz_stream_ioapi_close(void *stream) {
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
close_file_func zclose = NULL;
void *opaque = NULL;
if (mz_stream_ioapi_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
if (ioapi->filefunc.zclose_file) {
zclose = ioapi->filefunc.zclose_file;
opaque = ioapi->filefunc.opaque;
} else if (ioapi->filefunc64.zclose_file) {
zclose = ioapi->filefunc64.zclose_file;
opaque = ioapi->filefunc64.opaque;
} else
return MZ_PARAM_ERROR;
if (zclose(opaque, ioapi->handle) != 0)
return MZ_CLOSE_ERROR;
ioapi->handle = NULL;
return MZ_OK;
}
static int32_t mz_stream_ioapi_error(void *stream) {
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
testerror_file_func zerror = NULL;
void *opaque = NULL;
if (mz_stream_ioapi_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
if (ioapi->filefunc.zerror_file) {
zerror = ioapi->filefunc.zerror_file;
opaque = ioapi->filefunc.opaque;
} else if (ioapi->filefunc64.zerror_file) {
zerror = ioapi->filefunc64.zerror_file;
opaque = ioapi->filefunc64.opaque;
} else
return MZ_PARAM_ERROR;
return zerror(opaque, ioapi->handle);
}
static int32_t mz_stream_ioapi_set_filefunc(void *stream, zlib_filefunc_def *filefunc) {
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
memcpy(&ioapi->filefunc, filefunc, sizeof(zlib_filefunc_def));
return MZ_OK;
}
static int32_t mz_stream_ioapi_set_filefunc64(void *stream, zlib_filefunc64_def *filefunc) {
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
memcpy(&ioapi->filefunc64, filefunc, sizeof(zlib_filefunc64_def));
return MZ_OK;
}
static void *mz_stream_ioapi_create(void) {
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)calloc(1, sizeof(mz_stream_ioapi));
if (ioapi)
ioapi->stream.vtbl = &mz_stream_ioapi_vtbl;
return ioapi;
}
static void mz_stream_ioapi_delete(void **stream) {
mz_stream_ioapi *ioapi = NULL;
if (!stream)
return;
ioapi = (mz_stream_ioapi *)*stream;
if (ioapi)
free(ioapi);
*stream = NULL;
}
/***************************************************************************/
void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def) {
/* For 32-bit file support only, compile with MZ_FILE32_API */
if (pzlib_filefunc_def)
memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc_def));
}
void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def) {
/* All mz_stream_os_* support large files if compilation supports it */
if (pzlib_filefunc_def)
memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc64_def));
}
void fill_win32_filefunc(zlib_filefunc_def *pzlib_filefunc_def) {
/* Handled by mz_stream_os_win32 */
if (pzlib_filefunc_def)
memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc_def));
}
void fill_win32_filefunc64(zlib_filefunc64_def *pzlib_filefunc_def) {
/* Automatically supported in mz_stream_os_win32 */
if (pzlib_filefunc_def)
memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc64_def));
}
void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def) {
/* Automatically supported in mz_stream_os_win32 */
if (pzlib_filefunc_def)
memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc64_def));
}
/* NOTE: fill_win32_filefunc64W is no longer necessary since wide-character
support is automatically handled by the underlying os stream. Do not
pass wide-characters to zipOpen or unzOpen. */
void fill_memory_filefunc(zlib_filefunc_def *pzlib_filefunc_def) {
/* Use opaque to indicate which stream interface to create */
if (pzlib_filefunc_def) {
memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc_def));
pzlib_filefunc_def->opaque = mz_stream_mem_get_interface();
}
}
/***************************************************************************/
static int32_t zipConvertAppendToStreamMode(int append) {
int32_t mode = MZ_OPEN_MODE_WRITE;
switch (append) {
case APPEND_STATUS_CREATE:
mode |= MZ_OPEN_MODE_CREATE;
break;
case APPEND_STATUS_CREATEAFTER:
mode |= MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_APPEND;
break;
case APPEND_STATUS_ADDINZIP:
mode |= MZ_OPEN_MODE_READ | MZ_OPEN_MODE_APPEND;
break;
}
return mode;
}
zipFile zipOpen(const char *path, int append) {
return zipOpen2(path, append, NULL, NULL);
}
zipFile zipOpen64(const void *path, int append) {
return zipOpen2(path, append, NULL, NULL);
}
zipFile zipOpen2(const char *path, int append, const char **globalcomment,
zlib_filefunc_def *pzlib_filefunc_def) {
zipFile zip = NULL;
int32_t mode = zipConvertAppendToStreamMode(append);
void *stream = NULL;
if (pzlib_filefunc_def) {
if (pzlib_filefunc_def->zopen_file) {
stream = mz_stream_ioapi_create();
if (!stream)
return NULL;
mz_stream_ioapi_set_filefunc(stream, pzlib_filefunc_def);
} else if (pzlib_filefunc_def->opaque) {
stream = mz_stream_create((mz_stream_vtbl *)pzlib_filefunc_def->opaque);
if (!stream)
return NULL;
}
}
if (!stream) {
stream = mz_stream_os_create();
if (!stream)
return NULL;
}
if (mz_stream_open(stream, path, mode) != MZ_OK) {
mz_stream_delete(&stream);
return NULL;
}
zip = zipOpen_MZ(stream, append, globalcomment);
if (!zip) {
mz_stream_delete(&stream);
return NULL;
}
return zip;
}
zipFile zipOpen2_64(const void *path, int append, const char **globalcomment,
zlib_filefunc64_def *pzlib_filefunc_def) {
zipFile zip = NULL;
int32_t mode = zipConvertAppendToStreamMode(append);
void *stream = NULL;
if (pzlib_filefunc_def) {
if (pzlib_filefunc_def->zopen64_file) {
stream = mz_stream_ioapi_create();
if (!stream)
return NULL;
mz_stream_ioapi_set_filefunc64(stream, pzlib_filefunc_def);
} else if (pzlib_filefunc_def->opaque) {
stream = mz_stream_create((mz_stream_vtbl *)pzlib_filefunc_def->opaque);
if (!stream)
return NULL;
}
}
if (!stream) {
stream = mz_stream_os_create();
if (!stream)
return NULL;
}
if (mz_stream_open(stream, path, mode) != MZ_OK) {
mz_stream_delete(&stream);
return NULL;
}
zip = zipOpen_MZ(stream, append, globalcomment);
if (!zip) {
mz_stream_delete(&stream);
return NULL;
}
return zip;
}
zipFile zipOpen_MZ(void *stream, int append, const char **globalcomment) {
mz_compat *compat = NULL;
int32_t err = MZ_OK;
int32_t mode = zipConvertAppendToStreamMode(append);
void *handle = NULL;
handle = mz_zip_create();
if (!handle)
return NULL;
err = mz_zip_open(handle, stream, mode);
if (err != MZ_OK) {
mz_zip_delete(&handle);
return NULL;
}
if (globalcomment)
mz_zip_get_comment(handle, globalcomment);
compat = (mz_compat *)calloc(1, sizeof(mz_compat));
if (compat) {
compat->handle = handle;
compat->stream = stream;
} else {
mz_zip_delete(&handle);
}
return (zipFile)compat;
}
void* zipGetHandle_MZ(zipFile file) {
mz_compat *compat = (mz_compat *)file;
if (!compat)
return NULL;
return compat->handle;
}
void* zipGetStream_MZ(zipFile file) {
mz_compat *compat = (mz_compat *)file;
if (!compat)
return NULL;
return (void *)compat->stream;
}
int zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
unsigned long crc_for_crypting, unsigned long version_madeby, unsigned long flag_base, int zip64) {
mz_compat *compat = (mz_compat *)file;
mz_zip_file file_info;
MZ_UNUSED(strategy);
MZ_UNUSED(memLevel);
MZ_UNUSED(windowBits);
MZ_UNUSED(size_extrafield_local);
MZ_UNUSED(extrafield_local);
MZ_UNUSED(crc_for_crypting);
if (!compat)
return ZIP_PARAMERROR;
/* The filename and comment length must fit in 16 bits. */
if (filename && strlen(filename) > 0xffff)
return ZIP_PARAMERROR;
if (comment && strlen(comment) > 0xffff)
return ZIP_PARAMERROR;
memset(&file_info, 0, sizeof(file_info));
if (zipfi) {
uint64_t dos_date = 0;
if (zipfi->mz_dos_date != 0)
dos_date = zipfi->mz_dos_date;
else
dos_date = mz_zip_tm_to_dosdate(&zipfi->tmz_date);
file_info.modified_date = mz_zip_dosdate_to_time_t(dos_date);
file_info.external_fa = (uint32_t)zipfi->external_fa;
file_info.internal_fa = (uint16_t)zipfi->internal_fa;
}
if (!filename)
filename = "-";
file_info.compression_method = (uint16_t)compression_method;
file_info.filename = filename;
/* file_info.extrafield_local = extrafield_local; */
/* file_info.extrafield_local_size = size_extrafield_local; */
file_info.extrafield = extrafield_global;
file_info.extrafield_size = size_extrafield_global;
file_info.version_madeby = (uint16_t)version_madeby;
file_info.comment = comment;
if (file_info.comment)
file_info.comment_size = (uint16_t)strlen(file_info.comment);
file_info.flag = (uint16_t)flag_base;
if (zip64)
file_info.zip64 = MZ_ZIP64_FORCE;
else
file_info.zip64 = MZ_ZIP64_DISABLE;
#ifdef HAVE_WZAES
if (password || (raw && (file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)))
file_info.aes_version = MZ_AES_VERSION;
#endif
return mz_zip_entry_write_open(compat->handle, &file_info, (int16_t)level, (uint8_t)raw, password);
}
int zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
unsigned long crc_for_crypting, unsigned long version_madeby, unsigned long flag_base, int zip64) {
return zipOpenNewFileInZip5(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
memLevel, strategy, password, crc_for_crypting, version_madeby, flag_base, zip64);
}
int zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
unsigned long crc_for_crypting, unsigned long version_madeby, unsigned long flag_base) {
return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
memLevel, strategy, password, crc_for_crypting, version_madeby, flag_base, 0);
}
int zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
unsigned long crc_for_crypting) {
return zipOpenNewFileInZip3_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
memLevel, strategy, password, crc_for_crypting, 0);
}
int zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
unsigned long crc_for_crypting, int zip64) {
return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
memLevel, strategy, password, crc_for_crypting, MZ_VERSION_MADEBY, 0, zip64);
}
int zipOpenNewFileInZip2(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
int raw) {
return zipOpenNewFileInZip3_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw,
0, 0, 0, NULL, 0, 0);
}
int zipOpenNewFileInZip2_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
int raw, int zip64) {
return zipOpenNewFileInZip3_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, 0,
0, 0, NULL, 0, zip64);
}
int zipOpenNewFileInZip(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, int compression_method, int level) {
return zipOpenNewFileInZip_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, 0);
}
int zipOpenNewFileInZip64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
int zip64) {
return zipOpenNewFileInZip2_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, 0, zip64);
}
int zipOpenNewFileInZip_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
int zip64) {
return zipOpenNewFileInZip2_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, 0, zip64);
}
int zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len) {
mz_compat *compat = (mz_compat *)file;
int32_t written = 0;
if (!compat || len >= INT32_MAX)
return ZIP_PARAMERROR;
written = mz_zip_entry_write(compat->handle, buf, (int32_t)len);
if ((written < 0) || ((uint32_t)written != len))
return ZIP_ERRNO;
return ZIP_OK;
}
int zipCloseFileInZipRaw(zipFile file, unsigned long uncompressed_size, unsigned long crc32) {
return zipCloseFileInZipRaw64(file, uncompressed_size, crc32);
}
int zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_size, unsigned long crc32) {
mz_compat *compat = (mz_compat *)file;
if (!compat)
return ZIP_PARAMERROR;
return mz_zip_entry_close_raw(compat->handle, (int64_t)uncompressed_size, (uint32_t)crc32);
}
int zipCloseFileInZip(zipFile file) {
return zipCloseFileInZip64(file);
}
int zipCloseFileInZip64(zipFile file) {
mz_compat *compat = (mz_compat *)file;
if (!compat)
return ZIP_PARAMERROR;
return mz_zip_entry_close(compat->handle);
}
int zipClose(zipFile file, const char *global_comment) {
return zipClose_64(file, global_comment);
}
int zipClose_64(zipFile file, const char *global_comment) {
return zipClose2_64(file, global_comment, MZ_VERSION_MADEBY);
}
int zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby) {
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
if (compat->handle)
err = zipClose2_MZ(file, global_comment, version_madeby);
if (compat->stream) {
mz_stream_close(compat->stream);
mz_stream_delete(&compat->stream);
}
free(compat);
return err;
}
/* Only closes the zip handle, does not close the stream */
int zipClose_MZ(zipFile file, const char *global_comment) {
return zipClose2_MZ(file, global_comment, MZ_VERSION_MADEBY);
}
/* Only closes the zip handle, does not close the stream */
int zipClose2_MZ(zipFile file, const char *global_comment, uint16_t version_madeby) {
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
if (!compat)
return ZIP_PARAMERROR;
if (!compat->handle)
return err;
if (global_comment)
mz_zip_set_comment(compat->handle, global_comment);
mz_zip_set_version_madeby(compat->handle, version_madeby);
err = mz_zip_close(compat->handle);
mz_zip_delete(&compat->handle);
return err;
}
/***************************************************************************/
unzFile unzOpen(const char *path) {
return unzOpen64(path);
}
unzFile unzOpen64(const void *path) {
return unzOpen2(path, NULL);
}
unzFile unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def) {
unzFile unz = NULL;
void *stream = NULL;
if (pzlib_filefunc_def) {
if (pzlib_filefunc_def->zopen_file) {
stream = mz_stream_ioapi_create();
if (!stream)
return NULL;
mz_stream_ioapi_set_filefunc(stream, pzlib_filefunc_def);
} else if (pzlib_filefunc_def->opaque) {
stream = mz_stream_create((mz_stream_vtbl *)pzlib_filefunc_def->opaque);
if (!stream)
return NULL;
}
}
if (!stream) {
stream = mz_stream_os_create();
if (!stream)
return NULL;
}
if (mz_stream_open(stream, path, MZ_OPEN_MODE_READ) != MZ_OK) {
mz_stream_delete(&stream);
return NULL;
}
unz = unzOpen_MZ(stream);
if (!unz) {
mz_stream_close(stream);
mz_stream_delete(&stream);
return NULL;
}
return unz;
}
unzFile unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def) {
unzFile unz = NULL;
void *stream = NULL;
if (pzlib_filefunc_def) {
if (pzlib_filefunc_def->zopen64_file) {
stream = mz_stream_ioapi_create();
if (!stream)
return NULL;
mz_stream_ioapi_set_filefunc64(stream, pzlib_filefunc_def);
} else if (pzlib_filefunc_def->opaque) {
stream = mz_stream_create((mz_stream_vtbl *)pzlib_filefunc_def->opaque);
if (!stream)
return NULL;
}
}
if (!stream) {
stream = mz_stream_os_create();
if (!stream)
return NULL;
}
if (mz_stream_open(stream, path, MZ_OPEN_MODE_READ) != MZ_OK) {
mz_stream_delete(&stream);
return NULL;
}
unz = unzOpen_MZ(stream);
if (!unz) {
mz_stream_close(stream);
mz_stream_delete(&stream);
return NULL;
}
return unz;
}
void* unzGetHandle_MZ(unzFile file) {
mz_compat *compat = (mz_compat *)file;
if (!compat)
return NULL;
return compat->handle;
}
void* unzGetStream_MZ(unzFile file) {
mz_compat *compat = (mz_compat *)file;
if (!compat)
return NULL;
return compat->stream;
}
unzFile unzOpen_MZ(void *stream) {
mz_compat *compat = NULL;
int32_t err = MZ_OK;
void *handle = NULL;
handle = mz_zip_create();
if (!handle)
return NULL;
err = mz_zip_open(handle, stream, MZ_OPEN_MODE_READ);
if (err != MZ_OK) {
mz_zip_delete(&handle);
return NULL;
}
compat = (mz_compat *)calloc(1, sizeof(mz_compat));
if (compat) {
compat->handle = handle;
compat->stream = stream;
mz_zip_goto_first_entry(compat->handle);
} else {
mz_zip_delete(&handle);
}
return (unzFile)compat;
}
int unzClose(unzFile file) {
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
if (!compat)
return UNZ_PARAMERROR;
if (compat->handle)
err = unzClose_MZ(file);
if (compat->stream) {
mz_stream_close(compat->stream);
mz_stream_delete(&compat->stream);
}
free(compat);
return err;
}
/* Only closes the zip handle, does not close the stream */
int unzClose_MZ(unzFile file) {
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
if (!compat)
return UNZ_PARAMERROR;
err = mz_zip_close(compat->handle);
mz_zip_delete(&compat->handle);
return err;
}
int unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32) {
mz_compat *compat = (mz_compat *)file;
unz_global_info64 global_info64;
int32_t err = MZ_OK;
memset(pglobal_info32, 0, sizeof(unz_global_info));
if (!compat)
return UNZ_PARAMERROR;
err = unzGetGlobalInfo64(file, &global_info64);
if (err == MZ_OK) {
pglobal_info32->number_entry = (uint32_t)global_info64.number_entry;
pglobal_info32->size_comment = global_info64.size_comment;
pglobal_info32->number_disk_with_CD = global_info64.number_disk_with_CD;
}
return err;
}
int unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info) {
mz_compat *compat = (mz_compat *)file;
const char *comment_ptr = NULL;
int32_t err = MZ_OK;
memset(pglobal_info, 0, sizeof(unz_global_info64));
if (!compat)
return UNZ_PARAMERROR;
err = mz_zip_get_comment(compat->handle, &comment_ptr);
if (err == MZ_OK)
pglobal_info->size_comment = (uint16_t)strlen(comment_ptr);
if ((err == MZ_OK) || (err == MZ_EXIST_ERROR))
err = mz_zip_get_number_entry(compat->handle, &pglobal_info->number_entry);
if (err == MZ_OK)
err = mz_zip_get_disk_number_with_cd(compat->handle, &pglobal_info->number_disk_with_CD);
return err;
}
int unzGetGlobalComment(unzFile file, char *comment, unsigned long comment_size) {
mz_compat *compat = (mz_compat *)file;
const char *comment_ptr = NULL;
int32_t err = MZ_OK;
if (!comment || !comment_size)
return UNZ_PARAMERROR;
err = mz_zip_get_comment(compat->handle, &comment_ptr);
if (err == MZ_OK) {
strncpy(comment, comment_ptr, comment_size - 1);
comment[comment_size - 1] = 0;
}
return err;
}
int unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password) {
mz_compat *compat = (mz_compat *)file;
mz_zip_file *file_info = NULL;
int32_t err = MZ_OK;
void *stream = NULL;
if (!compat)
return UNZ_PARAMERROR;
if (method)
*method = 0;
if (level)
*level = 0;
if (mz_zip_entry_is_open(compat->handle) == MZ_OK) {
/* zlib minizip does not error out here if close returns errors */
unzCloseCurrentFile(file);
}
compat->total_out = 0;
err = mz_zip_entry_read_open(compat->handle, (uint8_t)raw, password);
if (err == MZ_OK)
err = mz_zip_entry_get_info(compat->handle, &file_info);
if (err == MZ_OK) {
if (method) {
*method = file_info->compression_method;
}
if (level) {
*level = 6;
switch (file_info->flag & 0x06) {
case MZ_ZIP_FLAG_DEFLATE_SUPER_FAST:
*level = 1;
break;
case MZ_ZIP_FLAG_DEFLATE_FAST:
*level = 2;
break;
case MZ_ZIP_FLAG_DEFLATE_MAX:
*level = 9;
break;
}
}
}
if (err == MZ_OK)
err = mz_zip_get_stream(compat->handle, &stream);
if (err == MZ_OK)
compat->entry_pos = mz_stream_tell(stream);
return err;
}
int unzOpenCurrentFile(unzFile file) {
return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
}
int unzOpenCurrentFilePassword(unzFile file, const char *password) {
return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
}
int unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw) {
return unzOpenCurrentFile3(file, method, level, raw, NULL);
}
int unzReadCurrentFile(unzFile file, void *buf, uint32_t len) {
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
if (!compat || len >= INT32_MAX)
return UNZ_PARAMERROR;
err = mz_zip_entry_read(compat->handle, buf, (int32_t)len);
if (err > 0)
compat->total_out += (uint32_t)err;
return err;
}
int unzCloseCurrentFile(unzFile file) {
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
if (!compat)
return UNZ_PARAMERROR;
err = mz_zip_entry_close(compat->handle);
return err;
}
int unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
unsigned long filename_size, void *extrafield, unsigned long extrafield_size, char *comment,
unsigned long comment_size) {
mz_compat *compat = (mz_compat *)file;
mz_zip_file *file_info = NULL;
uint16_t bytes_to_copy = 0;
int32_t err = MZ_OK;
if (!compat)
return UNZ_PARAMERROR;
err = mz_zip_entry_get_info(compat->handle, &file_info);
if (err != MZ_OK)
return err;
if (pfile_info) {
pfile_info->version = file_info->version_madeby;
pfile_info->version_needed = file_info->version_needed;
pfile_info->flag = file_info->flag;
pfile_info->compression_method = file_info->compression_method;
pfile_info->mz_dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
mz_zip_time_t_to_tm(file_info->modified_date, &pfile_info->tmu_date);
pfile_info->tmu_date.tm_year += 1900;
pfile_info->crc = file_info->crc;
pfile_info->size_filename = file_info->filename_size;
pfile_info->size_file_extra = file_info->extrafield_size;
pfile_info->size_file_comment = file_info->comment_size;
pfile_info->disk_num_start = (uint16_t)file_info->disk_number;
pfile_info->internal_fa = file_info->internal_fa;
pfile_info->external_fa = file_info->external_fa;