forked from zlib-ng/minizip-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mz_zip.c
2771 lines (2329 loc) · 101 KB
/
mz_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
/* zip.c -- Zip manipulation
part of the minizip-ng project
Copyright (C) 2010-2021 Nathan Moinvaziri
https://github.com/zlib-ng/minizip-ng
Copyright (C) 2009-2010 Mathias Svensson
Modifications for Zip64 support
http://result42.com
Copyright (C) 2007-2008 Even Rouault
Modifications of Unzip for Zip64
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_crypt.h"
#include "mz_strm.h"
#ifdef HAVE_BZIP2
# include "mz_strm_bzip.h"
#endif
#ifdef HAVE_LIBCOMP
# include "mz_strm_libcomp.h"
#endif
#ifdef HAVE_LZMA
# include "mz_strm_lzma.h"
#endif
#include "mz_strm_mem.h"
#ifdef HAVE_PKCRYPT
# include "mz_strm_pkcrypt.h"
#endif
#ifdef HAVE_WZAES
# include "mz_strm_wzaes.h"
#endif
#ifdef HAVE_ZLIB
# include "mz_strm_zlib.h"
#endif
#ifdef HAVE_ZSTD
# include "mz_strm_zstd.h"
#endif
#include "mz_zip.h"
#include <ctype.h> /* tolower */
#include <stdio.h> /* snprintf */
#if defined(_MSC_VER) || defined(__MINGW32__)
# define localtime_r(t1,t2) (localtime_s(t2,t1) == 0 ? t1 : NULL)
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1900)
# define snprintf _snprintf
#endif
/***************************************************************************/
#define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
#define MZ_ZIP_MAGIC_LOCALHEADERU8 { 0x50, 0x4b, 0x03, 0x04 }
#define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
#define MZ_ZIP_MAGIC_CENTRALHEADERU8 { 0x50, 0x4b, 0x01, 0x02 }
#define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
#define MZ_ZIP_MAGIC_ENDHEADERU8 { 0x50, 0x4b, 0x05, 0x06 }
#define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
#define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
#define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
#define MZ_ZIP_MAGIC_DATADESCRIPTORU8 { 0x50, 0x4b, 0x07, 0x08 }
#define MZ_ZIP_SIZE_LD_ITEM (30)
#define MZ_ZIP_SIZE_CD_ITEM (46)
#define MZ_ZIP_SIZE_CD_LOCATOR64 (20)
#define MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR (24)
#define MZ_ZIP_OFFSET_CRC_SIZES (14)
#define MZ_ZIP_UNCOMPR_SIZE64_CUSHION (2 * 1024 * 1024)
#ifndef MZ_ZIP_EOCD_MAX_BACK
#define MZ_ZIP_EOCD_MAX_BACK (1 << 20)
#endif
/***************************************************************************/
typedef struct mz_zip_s {
mz_zip_file file_info;
mz_zip_file local_file_info;
void *stream; /* main stream */
void *cd_stream; /* pointer to the stream with the cd */
void *cd_mem_stream; /* memory stream for central directory */
void *compress_stream; /* compression stream */
void *crypt_stream; /* encryption stream */
void *file_info_stream; /* memory stream for storing file info */
void *local_file_info_stream; /* memory stream for storing local file info */
int32_t open_mode;
uint8_t recover;
uint8_t data_descriptor;
uint32_t disk_number_with_cd; /* number of the disk with the central dir */
int64_t disk_offset_shift; /* correction for zips that have wrong offset start of cd */
int64_t cd_start_pos; /* pos of the first file in the central dir stream */
int64_t cd_current_pos; /* pos of the current file in the central dir */
int64_t cd_offset; /* offset of start of central directory */
int64_t cd_size; /* size of the central directory */
uint32_t cd_signature; /* signature of central directory */
uint8_t entry_scanned; /* entry header information read ok */
uint8_t entry_opened; /* entry is open for read/write */
uint8_t entry_raw; /* entry opened with raw mode */
uint32_t entry_crc32; /* entry crc32 */
uint64_t number_entry;
uint16_t version_madeby;
char *comment;
} mz_zip;
/***************************************************************************/
#if 0
# define mz_zip_print printf
#else
# define mz_zip_print(fmt,...)
#endif
/***************************************************************************/
/* Locate the end of central directory */
static int32_t mz_zip_search_eocd(void *stream, int64_t *central_pos) {
int64_t file_size = 0;
int64_t max_back = MZ_ZIP_EOCD_MAX_BACK;
uint8_t find[4] = MZ_ZIP_MAGIC_ENDHEADERU8;
int32_t err = MZ_OK;
err = mz_stream_seek(stream, 0, MZ_SEEK_END);
if (err != MZ_OK)
return err;
file_size = mz_stream_tell(stream);
if (max_back <= 0 || max_back > file_size)
max_back = file_size;
return mz_stream_find_reverse(stream, (const void *)find, sizeof(find), max_back, central_pos);
}
/* Locate the end of central directory 64 of a zip file */
static int32_t mz_zip_search_zip64_eocd(void *stream, const int64_t end_central_offset, int64_t *central_pos) {
int64_t offset = 0;
uint32_t value32 = 0;
int32_t err = MZ_OK;
*central_pos = 0;
/* Zip64 end of central directory locator */
err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
/* Read locator signature */
if (err == MZ_OK) {
err = mz_stream_read_uint32(stream, &value32);
if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
err = MZ_FORMAT_ERROR;
}
/* Number of the disk with the start of the zip64 end of central directory */
if (err == MZ_OK)
err = mz_stream_read_uint32(stream, &value32);
/* Relative offset of the zip64 end of central directory record8 */
if (err == MZ_OK)
err = mz_stream_read_uint64(stream, (uint64_t *)&offset);
/* Total number of disks */
if (err == MZ_OK)
err = mz_stream_read_uint32(stream, &value32);
/* Goto end of central directory record */
if (err == MZ_OK)
err = mz_stream_seek(stream, (int64_t)offset, MZ_SEEK_SET);
/* The signature */
if (err == MZ_OK) {
err = mz_stream_read_uint32(stream, &value32);
if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
err = MZ_FORMAT_ERROR;
}
if (err == MZ_OK)
*central_pos = offset;
return err;
}
/* Get PKWARE traditional encryption verifier */
static uint16_t mz_zip_get_pk_verify(uint32_t dos_date, uint64_t crc, uint16_t flag)
{
/* Info-ZIP modification to ZipCrypto format: if bit 3 of the general
* purpose bit flag is set, it uses high byte of 16-bit File Time. */
if (flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
return ((dos_date >> 16) & 0xff) << 8 | ((dos_date >> 8) & 0xff);
return ((crc >> 16) & 0xff) << 8 | ((crc >> 24) & 0xff);
}
/* Get info about the current file in the zip file */
static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file *file_info, void *file_extra_stream) {
uint64_t ntfs_time = 0;
uint32_t reserved = 0;
uint32_t magic = 0;
uint32_t dos_date = 0;
uint32_t field_pos = 0;
uint16_t field_type = 0;
uint16_t field_length = 0;
uint32_t field_length_read = 0;
uint16_t ntfs_attrib_id = 0;
uint16_t ntfs_attrib_size = 0;
uint16_t linkname_size;
uint16_t value16 = 0;
uint32_t value32 = 0;
int64_t extrafield_pos = 0;
int64_t comment_pos = 0;
int64_t linkname_pos = 0;
int64_t saved_pos = 0;
int32_t err = MZ_OK;
char *linkname = NULL;
memset(file_info, 0, sizeof(mz_zip_file));
/* Check the magic */
err = mz_stream_read_uint32(stream, &magic);
if (err == MZ_END_OF_STREAM)
err = MZ_END_OF_LIST;
else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
err = MZ_END_OF_LIST;
else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
err = MZ_FORMAT_ERROR;
else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
err = MZ_FORMAT_ERROR;
/* Read header fields */
if (err == MZ_OK) {
if (!local)
err = mz_stream_read_uint16(stream, &file_info->version_madeby);
if (err == MZ_OK)
err = mz_stream_read_uint16(stream, &file_info->version_needed);
if (err == MZ_OK)
err = mz_stream_read_uint16(stream, &file_info->flag);
if (err == MZ_OK)
err = mz_stream_read_uint16(stream, &file_info->compression_method);
if (err == MZ_OK) {
err = mz_stream_read_uint32(stream, &dos_date);
file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
}
if (err == MZ_OK)
err = mz_stream_read_uint32(stream, &file_info->crc);
#ifdef HAVE_PKCRYPT
if (err == MZ_OK && file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) {
/* Use dos_date from header instead of derived from time in zip extensions */
file_info->pk_verify = mz_zip_get_pk_verify(dos_date, file_info->crc, file_info->flag);
}
#endif
if (err == MZ_OK) {
err = mz_stream_read_uint32(stream, &value32);
file_info->compressed_size = value32;
}
if (err == MZ_OK) {
err = mz_stream_read_uint32(stream, &value32);
file_info->uncompressed_size = value32;
}
if (err == MZ_OK)
err = mz_stream_read_uint16(stream, &file_info->filename_size);
if (err == MZ_OK)
err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
if (!local) {
if (err == MZ_OK)
err = mz_stream_read_uint16(stream, &file_info->comment_size);
if (err == MZ_OK) {
err = mz_stream_read_uint16(stream, &value16);
file_info->disk_number = value16;
}
if (err == MZ_OK)
err = mz_stream_read_uint16(stream, &file_info->internal_fa);
if (err == MZ_OK)
err = mz_stream_read_uint32(stream, &file_info->external_fa);
if (err == MZ_OK) {
err = mz_stream_read_uint32(stream, &value32);
file_info->disk_offset = value32;
}
}
}
if (err == MZ_OK)
err = mz_stream_seek(file_extra_stream, 0, MZ_SEEK_SET);
/* Copy variable length data to memory stream for later retrieval */
if ((err == MZ_OK) && (file_info->filename_size > 0))
err = mz_stream_copy(file_extra_stream, stream, file_info->filename_size);
mz_stream_write_uint8(file_extra_stream, 0);
extrafield_pos = mz_stream_tell(file_extra_stream);
if ((err == MZ_OK) && (file_info->extrafield_size > 0))
err = mz_stream_copy(file_extra_stream, stream, file_info->extrafield_size);
mz_stream_write_uint8(file_extra_stream, 0);
comment_pos = mz_stream_tell(file_extra_stream);
if ((err == MZ_OK) && (file_info->comment_size > 0))
err = mz_stream_copy(file_extra_stream, stream, file_info->comment_size);
mz_stream_write_uint8(file_extra_stream, 0);
linkname_pos = mz_stream_tell(file_extra_stream);
/* Overwrite if we encounter UNIX1 extra block */
mz_stream_write_uint8(file_extra_stream, 0);
if ((err == MZ_OK) && (file_info->extrafield_size > 0)) {
/* Seek to and parse the extra field */
err = mz_stream_seek(file_extra_stream, extrafield_pos, MZ_SEEK_SET);
while ((err == MZ_OK) && (field_pos + 4 <= file_info->extrafield_size)) {
err = mz_zip_extrafield_read(file_extra_stream, &field_type, &field_length);
if (err != MZ_OK)
break;
field_pos += 4;
/* Don't allow field length to exceed size of remaining extrafield */
if (field_length > (file_info->extrafield_size - field_pos))
field_length = (uint16_t)(file_info->extrafield_size - field_pos);
/* Read ZIP64 extra field */
if ((field_type == MZ_ZIP_EXTENSION_ZIP64) && (field_length >= 8)) {
if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX)) {
err = mz_stream_read_int64(file_extra_stream, &file_info->uncompressed_size);
if (file_info->uncompressed_size < 0)
err = MZ_FORMAT_ERROR;
}
if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX)) {
err = mz_stream_read_int64(file_extra_stream, &file_info->compressed_size);
if (file_info->compressed_size < 0)
err = MZ_FORMAT_ERROR;
}
if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX)) {
err = mz_stream_read_int64(file_extra_stream, &file_info->disk_offset);
if (file_info->disk_offset < 0)
err = MZ_FORMAT_ERROR;
}
if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
err = mz_stream_read_uint32(file_extra_stream, &file_info->disk_number);
}
/* Read NTFS extra field */
else if ((field_type == MZ_ZIP_EXTENSION_NTFS) && (field_length > 4)) {
if (err == MZ_OK)
err = mz_stream_read_uint32(file_extra_stream, &reserved);
field_length_read = 4;
while ((err == MZ_OK) && (field_length_read + 4 <= field_length)) {
err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_id);
if (err == MZ_OK)
err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_size);
field_length_read += 4;
if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24)) {
err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
if (err == MZ_OK) {
err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
}
if (err == MZ_OK) {
err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
}
} else if ((err == MZ_OK) && (field_length_read + ntfs_attrib_size <= field_length)) {
err = mz_stream_seek(file_extra_stream, ntfs_attrib_size, MZ_SEEK_CUR);
}
field_length_read += ntfs_attrib_size;
}
}
/* Read UNIX1 extra field */
else if ((field_type == MZ_ZIP_EXTENSION_UNIX1) && (field_length >= 12)) {
if (err == MZ_OK) {
err = mz_stream_read_uint32(file_extra_stream, &value32);
if (err == MZ_OK && file_info->accessed_date == 0)
file_info->accessed_date = value32;
}
if (err == MZ_OK) {
err = mz_stream_read_uint32(file_extra_stream, &value32);
if (err == MZ_OK && file_info->modified_date == 0)
file_info->modified_date = value32;
}
if (err == MZ_OK)
err = mz_stream_read_uint16(file_extra_stream, &value16); /* User id */
if (err == MZ_OK)
err = mz_stream_read_uint16(file_extra_stream, &value16); /* Group id */
/* Copy linkname to end of file extra stream so we can return null
terminated string */
linkname_size = field_length - 12;
if ((err == MZ_OK) && (linkname_size > 0)) {
linkname = (char *)MZ_ALLOC(linkname_size);
if (linkname != NULL) {
if (mz_stream_read(file_extra_stream, linkname, linkname_size) != linkname_size)
err = MZ_READ_ERROR;
if (err == MZ_OK) {
saved_pos = mz_stream_tell(file_extra_stream);
mz_stream_seek(file_extra_stream, linkname_pos, MZ_SEEK_SET);
mz_stream_write(file_extra_stream, linkname, linkname_size);
mz_stream_write_uint8(file_extra_stream, 0);
mz_stream_seek(file_extra_stream, saved_pos, MZ_SEEK_SET);
}
MZ_FREE(linkname);
}
}
}
#ifdef HAVE_WZAES
/* Read AES extra field */
else if ((field_type == MZ_ZIP_EXTENSION_AES) && (field_length == 7)) {
uint8_t value8 = 0;
/* Verify version info */
err = mz_stream_read_uint16(file_extra_stream, &value16);
/* Support AE-1 and AE-2 */
if (value16 != 1 && value16 != 2)
err = MZ_FORMAT_ERROR;
file_info->aes_version = value16;
if (err == MZ_OK)
err = mz_stream_read_uint8(file_extra_stream, &value8);
if ((char)value8 != 'A')
err = MZ_FORMAT_ERROR;
if (err == MZ_OK)
err = mz_stream_read_uint8(file_extra_stream, &value8);
if ((char)value8 != 'E')
err = MZ_FORMAT_ERROR;
/* Get AES encryption strength and actual compression method */
if (err == MZ_OK) {
err = mz_stream_read_uint8(file_extra_stream, &value8);
file_info->aes_encryption_mode = value8;
}
if (err == MZ_OK) {
err = mz_stream_read_uint16(file_extra_stream, &value16);
file_info->compression_method = value16;
}
}
#endif
else if (field_length > 0) {
err = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
}
field_pos += field_length;
}
}
/* Get pointers to variable length data */
mz_stream_mem_get_buffer(file_extra_stream, (const void **)&file_info->filename);
mz_stream_mem_get_buffer_at(file_extra_stream, extrafield_pos, (const void **)&file_info->extrafield);
mz_stream_mem_get_buffer_at(file_extra_stream, comment_pos, (const void **)&file_info->comment);
mz_stream_mem_get_buffer_at(file_extra_stream, linkname_pos, (const void **)&file_info->linkname);
/* Set to empty string just in-case */
if (file_info->filename == NULL)
file_info->filename = "";
if (file_info->extrafield == NULL)
file_info->extrafield_size = 0;
if (file_info->comment == NULL)
file_info->comment = "";
if (file_info->linkname == NULL)
file_info->linkname = "";
if (err == MZ_OK) {
mz_zip_print("Zip - Entry - Read header - %s (local %" PRId8 ")\n",
file_info->filename, local);
mz_zip_print("Zip - Entry - Read header compress (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
if (!local) {
mz_zip_print("Zip - Entry - Read header disk (disk %" PRIu32 " offset %" PRId64 ")\n",
file_info->disk_number, file_info->disk_offset);
}
mz_zip_print("Zip - Entry - Read header variable (fnl %" PRId32 " efs %" PRId32 " cms %" PRId32 ")\n",
file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
}
return err;
}
static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size) {
uint32_t value32 = 0;
int64_t value64 = 0;
int32_t err = MZ_OK;
err = mz_stream_read_uint32(stream, &value32);
if (value32 != MZ_ZIP_MAGIC_DATADESCRIPTOR)
err = MZ_FORMAT_ERROR;
if (err == MZ_OK)
err = mz_stream_read_uint32(stream, &value32);
if ((err == MZ_OK) && (crc32 != NULL))
*crc32 = value32;
if (err == MZ_OK) {
/* If zip 64 extension is enabled then read as 8 byte */
if (!zip64) {
err = mz_stream_read_uint32(stream, &value32);
value64 = value32;
} else {
err = mz_stream_read_int64(stream, &value64);
if (value64 < 0)
err = MZ_FORMAT_ERROR;
}
if ((err == MZ_OK) && (compressed_size != NULL))
*compressed_size = value64;
}
if (err == MZ_OK) {
if (!zip64) {
err = mz_stream_read_uint32(stream, &value32);
value64 = value32;
} else {
err = mz_stream_read_int64(stream, &value64);
if (value64 < 0)
err = MZ_FORMAT_ERROR;
}
if ((err == MZ_OK) && (uncompressed_size != NULL))
*uncompressed_size = value64;
}
return err;
}
static int32_t mz_zip_entry_write_crc_sizes(void *stream, uint8_t zip64, uint8_t mask, mz_zip_file *file_info) {
int32_t err = MZ_OK;
if (mask)
err = mz_stream_write_uint32(stream, 0);
else
err = mz_stream_write_uint32(stream, file_info->crc); /* crc */
/* For backwards-compatibility with older zip applications we set all sizes to UINT32_MAX
* when zip64 is needed, instead of only setting sizes larger than UINT32_MAX. */
if (err == MZ_OK) {
if (zip64) /* compr size */
err = mz_stream_write_uint32(stream, UINT32_MAX);
else
err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
}
if (err == MZ_OK) {
if (mask) /* uncompr size */
err = mz_stream_write_uint32(stream, 0);
else if (zip64)
err = mz_stream_write_uint32(stream, UINT32_MAX);
else
err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
}
return err;
}
static int32_t mz_zip_entry_needs_zip64(mz_zip_file *file_info, uint8_t local, uint8_t *zip64) {
uint32_t max_uncompressed_size = UINT32_MAX;
uint8_t needs_zip64 = 0;
if (zip64 == NULL)
return MZ_PARAM_ERROR;
*zip64 = 0;
if (local) {
/* At local header we might not know yet whether compressed size will overflow unsigned
32-bit integer which might happen for high entropy data so we give it some cushion */
max_uncompressed_size -= MZ_ZIP_UNCOMPR_SIZE64_CUSHION;
}
needs_zip64 = (file_info->uncompressed_size >= max_uncompressed_size) ||
(file_info->compressed_size >= UINT32_MAX);
if (!local) {
/* Disk offset and number only used in central directory header */
needs_zip64 |= (file_info->disk_offset >= UINT32_MAX) ||
(file_info->disk_number >= UINT16_MAX);
}
if (file_info->zip64 == MZ_ZIP64_AUTO) {
/* If uncompressed size is unknown, assume zip64 for 64-bit data descriptors */
if (local && file_info->uncompressed_size == 0) {
/* Don't use zip64 for local header directory entries */
if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) != MZ_OK) {
*zip64 = 1;
}
}
*zip64 |= needs_zip64;
} else if (file_info->zip64 == MZ_ZIP64_FORCE) {
*zip64 = 1;
} else if (file_info->zip64 == MZ_ZIP64_DISABLE) {
/* Zip64 extension is required to zip file */
if (needs_zip64)
return MZ_PARAM_ERROR;
}
return MZ_OK;
}
static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info) {
uint64_t ntfs_time = 0;
uint32_t reserved = 0;
uint32_t dos_date = 0;
uint16_t extrafield_size = 0;
uint16_t field_type = 0;
uint16_t field_length = 0;
uint16_t field_length_zip64 = 0;
uint16_t field_length_ntfs = 0;
uint16_t field_length_aes = 0;
uint16_t field_length_unix1 = 0;
uint16_t filename_size = 0;
uint16_t filename_length = 0;
uint16_t linkname_size = 0;
uint16_t version_needed = 0;
int32_t comment_size = 0;
int32_t err = MZ_OK;
int32_t err_mem = MZ_OK;
uint8_t zip64 = 0;
uint8_t skip_aes = 0;
uint8_t mask = 0;
uint8_t write_end_slash = 0;
const char *filename = NULL;
char masked_name[64];
void *file_extra_stream = NULL;
if (file_info == NULL)
return MZ_PARAM_ERROR;
if ((local) && (file_info->flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO))
mask = 1;
/* Determine if zip64 extra field is necessary */
err = mz_zip_entry_needs_zip64(file_info, local, &zip64);
if (err != MZ_OK)
return err;
/* Start calculating extra field sizes */
if (zip64) {
/* Both compressed and uncompressed sizes must be included (at least in local header) */
field_length_zip64 = 8 + 8;
if ((!local) && (file_info->disk_offset >= UINT32_MAX))
field_length_zip64 += 8;
extrafield_size += 4;
extrafield_size += field_length_zip64;
}
/* Calculate extra field size and check for duplicates */
if (file_info->extrafield_size > 0) {
mz_stream_mem_create(&file_extra_stream);
mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield,
file_info->extrafield_size);
do {
err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
if (err_mem == MZ_OK)
err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
if (err_mem != MZ_OK)
break;
/* Prefer incoming aes extensions over ours */
if (field_type == MZ_ZIP_EXTENSION_AES)
skip_aes = 1;
/* Prefer our zip64, ntfs, unix1 extension over incoming */
if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS &&
field_type != MZ_ZIP_EXTENSION_UNIX1)
extrafield_size += 4 + field_length;
if (err_mem == MZ_OK)
err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
} while (err_mem == MZ_OK);
}
#ifdef HAVE_WZAES
if (!skip_aes) {
if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version)) {
field_length_aes = 1 + 1 + 1 + 2 + 2;
extrafield_size += 4 + field_length_aes;
}
}
#else
MZ_UNUSED(field_length_aes);
MZ_UNUSED(skip_aes);
#endif
/* NTFS timestamps */
if ((file_info->modified_date != 0) &&
(file_info->accessed_date != 0) &&
(file_info->creation_date != 0) && (!mask)) {
field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2;
extrafield_size += 4 + field_length_ntfs;
}
/* Unix1 symbolic links */
if (file_info->linkname != NULL && *file_info->linkname != 0) {
linkname_size = (uint16_t)strlen(file_info->linkname);
field_length_unix1 = 12 + linkname_size;
extrafield_size += 4 + field_length_unix1;
}
if (local)
err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
else {
err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, file_info->version_madeby);
}
/* Calculate version needed to extract */
if (err == MZ_OK) {
version_needed = file_info->version_needed;
if (version_needed == 0) {
version_needed = 20;
if (zip64)
version_needed = 45;
#ifdef HAVE_WZAES
if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
version_needed = 51;
#endif
#if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP)
if ((file_info->compression_method == MZ_COMPRESS_METHOD_LZMA) ||
(file_info->compression_method == MZ_COMPRESS_METHOD_XZ))
version_needed = 63;
#endif
}
err = mz_stream_write_uint16(stream, version_needed);
}
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, file_info->flag);
if (err == MZ_OK) {
#ifdef HAVE_WZAES
if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
else
#endif
err = mz_stream_write_uint16(stream, file_info->compression_method);
}
if (err == MZ_OK) {
if (file_info->modified_date != 0 && !mask)
dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
err = mz_stream_write_uint32(stream, dos_date);
}
if (err == MZ_OK)
err = mz_zip_entry_write_crc_sizes(stream, zip64, mask, file_info);
if (mask) {
snprintf(masked_name, sizeof(masked_name), "%" PRIx32 "_%" PRIx64,
file_info->disk_number, file_info->disk_offset);
filename = masked_name;
} else {
filename = file_info->filename;
}
filename_length = (uint16_t)strlen(filename);
filename_size += filename_length;
if ((mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) &&
((filename[filename_length - 1] != '/') && (filename[filename_length - 1] != '\\'))) {
filename_size += 1;
write_end_slash = 1;
}
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, filename_size);
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, extrafield_size);
if (!local) {
if (file_info->comment != NULL) {
comment_size = (int32_t)strlen(file_info->comment);
if (comment_size > UINT16_MAX)
comment_size = UINT16_MAX;
}
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, (uint16_t)comment_size);
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, file_info->internal_fa);
if (err == MZ_OK)
err = mz_stream_write_uint32(stream, file_info->external_fa);
if (err == MZ_OK) {
if (file_info->disk_offset >= UINT32_MAX)
err = mz_stream_write_uint32(stream, UINT32_MAX);
else
err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
}
}
if (err == MZ_OK) {
if (mz_stream_write(stream, filename, filename_length) != filename_length)
err = MZ_WRITE_ERROR;
/* Ensure that directories have a slash appended to them for compatibility */
if (err == MZ_OK && write_end_slash)
err = mz_stream_write_uint8(stream, '/');
}
/* Write ZIP64 extra field first so we can update sizes later if data descriptor not used */
if ((err == MZ_OK) && (zip64)) {
err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_ZIP64, field_length_zip64);
if (err == MZ_OK) {
if (mask)
err = mz_stream_write_int64(stream, 0);
else
err = mz_stream_write_int64(stream, file_info->uncompressed_size);
}
if (err == MZ_OK)
err = mz_stream_write_int64(stream, file_info->compressed_size);
if ((err == MZ_OK) && (!local) && (file_info->disk_offset >= UINT32_MAX))
err = mz_stream_write_int64(stream, file_info->disk_offset);
if ((err == MZ_OK) && (!local) && (file_info->disk_number >= UINT16_MAX))
err = mz_stream_write_uint32(stream, file_info->disk_number);
}
/* Write NTFS extra field */
if ((err == MZ_OK) && (field_length_ntfs > 0)) {
err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_NTFS, field_length_ntfs);
if (err == MZ_OK)
err = mz_stream_write_uint32(stream, reserved);
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, 0x01);
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, field_length_ntfs - 8);
if (err == MZ_OK) {
mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
err = mz_stream_write_uint64(stream, ntfs_time);
}
if (err == MZ_OK) {
mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
err = mz_stream_write_uint64(stream, ntfs_time);
}
if (err == MZ_OK) {
mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
err = mz_stream_write_uint64(stream, ntfs_time);
}
}
/* Write UNIX extra block extra field */
if ((err == MZ_OK) && (field_length_unix1 > 0)) {
err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_UNIX1, field_length_unix1);
if (err == MZ_OK)
err = mz_stream_write_uint32(stream, (uint32_t)file_info->accessed_date);
if (err == MZ_OK)
err = mz_stream_write_uint32(stream, (uint32_t)file_info->modified_date);
if (err == MZ_OK) /* User id */
err = mz_stream_write_uint16(stream, 0);
if (err == MZ_OK) /* Group id */
err = mz_stream_write_uint16(stream, 0);
if (err == MZ_OK && linkname_size > 0) {
if (mz_stream_write(stream, file_info->linkname, linkname_size) != linkname_size)
err = MZ_WRITE_ERROR;
}
}
#ifdef HAVE_WZAES
/* Write AES extra field */
if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version)) {
err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_AES, field_length_aes);
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, file_info->aes_version);
if (err == MZ_OK)
err = mz_stream_write_uint8(stream, 'A');
if (err == MZ_OK)
err = mz_stream_write_uint8(stream, 'E');
if (err == MZ_OK)
err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, file_info->compression_method);
}
#endif
if (file_info->extrafield_size > 0) {
err_mem = mz_stream_mem_seek(file_extra_stream, 0, MZ_SEEK_SET);
while (err == MZ_OK && err_mem == MZ_OK) {
err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
if (err_mem == MZ_OK)
err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
if (err_mem != MZ_OK)
break;
/* Prefer our zip 64, ntfs, unix1 extensions over incoming */
if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS ||
field_type == MZ_ZIP_EXTENSION_UNIX1) {
err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
continue;
}
err = mz_stream_write_uint16(stream, field_type);
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, field_length);
if (err == MZ_OK)
err = mz_stream_copy(stream, file_extra_stream, field_length);
}
mz_stream_mem_delete(&file_extra_stream);
}
if ((err == MZ_OK) && (!local) && (file_info->comment != NULL)) {
if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != file_info->comment_size)
err = MZ_WRITE_ERROR;
}
return err;
}
static int32_t mz_zip_entry_write_descriptor(void *stream, uint8_t zip64, uint32_t crc32, int64_t compressed_size, int64_t uncompressed_size) {
int32_t err = MZ_OK;
err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
if (err == MZ_OK)
err = mz_stream_write_uint32(stream, crc32);
/* Store data descriptor as 8 bytes if zip 64 extension enabled */
if (err == MZ_OK) {
/* Zip 64 extension is enabled when uncompressed size is > UINT32_MAX */
if (!zip64)
err = mz_stream_write_uint32(stream, (uint32_t)compressed_size);
else
err = mz_stream_write_int64(stream, compressed_size);
}
if (err == MZ_OK) {
if (!zip64)
err = mz_stream_write_uint32(stream, (uint32_t)uncompressed_size);
else
err = mz_stream_write_int64(stream, uncompressed_size);
}
return err;
}
static int32_t mz_zip_read_cd(void *handle) {
mz_zip *zip = (mz_zip *)handle;
uint64_t number_entry_cd64 = 0;
uint64_t number_entry_cd = 0;
int64_t eocd_pos = 0;
int64_t eocd_pos64 = 0;
int64_t value64i = 0;
uint16_t value16 = 0;
uint32_t value32 = 0;
uint64_t value64 = 0;
uint16_t comment_size = 0;
int32_t comment_read = 0;
int32_t err = MZ_OK;
if (zip == NULL)
return MZ_PARAM_ERROR;
/* Read and cache central directory records */
err = mz_zip_search_eocd(zip->stream, &eocd_pos);
if (err == MZ_OK) {
/* The signature, already checked */
err = mz_stream_read_uint32(zip->stream, &value32);
/* Number of this disk */
if (err == MZ_OK)
err = mz_stream_read_uint16(zip->stream, &value16);
/* Number of the disk with the start of the central directory */
if (err == MZ_OK)
err = mz_stream_read_uint16(zip->stream, &value16);
zip->disk_number_with_cd = value16;
/* Total number of entries in the central dir on this disk */
if (err == MZ_OK)
err = mz_stream_read_uint16(zip->stream, &value16);
zip->number_entry = value16;
/* Total number of entries in the central dir */
if (err == MZ_OK)
err = mz_stream_read_uint16(zip->stream, &value16);
number_entry_cd = value16;
if (number_entry_cd != zip->number_entry)
err = MZ_FORMAT_ERROR;
/* Size of the central directory */
if (err == MZ_OK)
err = mz_stream_read_uint32(zip->stream, &value32);
if (err == MZ_OK)
zip->cd_size = value32;
/* Offset of start of central directory with respect to the starting disk number */
if (err == MZ_OK)
err = mz_stream_read_uint32(zip->stream, &value32);
if (err == MZ_OK)
zip->cd_offset = value32;
/* Zip file global comment length */
if (err == MZ_OK)
err = mz_stream_read_uint16(zip->stream, &comment_size);
if ((err == MZ_OK) && (comment_size > 0)) {
zip->comment = (char *)MZ_ALLOC(comment_size + 1);
if (zip->comment != NULL) {
comment_read = mz_stream_read(zip->stream, zip->comment, comment_size);
/* Don't fail if incorrect comment length read, not critical */
if (comment_read < 0)
comment_read = 0;
zip->comment[comment_read] = 0;
}
}
if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))) {
/* Format should be Zip64, as the central directory or file size is too large */
if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK) {
eocd_pos = eocd_pos64;
err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
/* The signature, already checked */
if (err == MZ_OK)
err = mz_stream_read_uint32(zip->stream, &value32);