forked from zlib-ng/minizip-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mz_zip_rw.c
2005 lines (1606 loc) · 57.8 KB
/
mz_zip_rw.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_zip_rw.c -- Zip reader/writer
Version 2.7.5, November 13, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
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 <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include "mz.h"
#include "mz_crypt.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_buf.h"
#include "mz_strm_mem.h"
#include "mz_strm_os.h"
#include "mz_strm_split.h"
#include "mz_strm_wzaes.h"
#include "mz_zip.h"
#include "mz_zip_rw.h"
/***************************************************************************/
#define MZ_DEFAULT_PROGRESS_INTERVAL (1000u)
#define MZ_ZIP_CD_FILENAME ("__cdcd__")
/***************************************************************************/
typedef struct mz_zip_reader_s {
void *zip_handle;
void *file_stream;
void *buffered_stream;
void *split_stream;
void *mem_stream;
void *hash;
uint16_t hash_algorithm;
uint16_t hash_digest_size;
mz_zip_file *file_info;
const char *pattern;
uint8_t pattern_ignore_case;
const char *password;
void *overwrite_userdata;
mz_zip_reader_overwrite_cb
overwrite_cb;
void *password_userdata;
mz_zip_reader_password_cb
password_cb;
void *progress_userdata;
mz_zip_reader_progress_cb
progress_cb;
uint32_t progress_cb_interval_ms;
void *entry_userdata;
mz_zip_reader_entry_cb
entry_cb;
uint8_t raw;
uint8_t buffer[UINT16_MAX];
int32_t encoding;
uint8_t sign_required;
uint8_t cd_verified;
uint8_t entry_verified;
} mz_zip_reader;
/***************************************************************************/
int32_t mz_zip_reader_is_open(void *handle)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
if (reader == NULL)
return MZ_PARAM_ERROR;
if (reader->zip_handle == NULL)
return MZ_PARAM_ERROR;
return MZ_OK;
}
int32_t mz_zip_reader_open(void *handle, void *stream)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
int32_t err = MZ_OK;
reader->cd_verified = 0;
mz_zip_create(&reader->zip_handle);
mz_zip_set_recover(reader->zip_handle, 1);
err = mz_zip_open(reader->zip_handle, stream, MZ_OPEN_MODE_READ);
if (err != MZ_OK)
{
mz_zip_reader_close(handle);
return err;
}
mz_zip_reader_unzip_cd(reader);
return MZ_OK;
}
int32_t mz_zip_reader_open_file(void *handle, const char *path)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
int32_t err = MZ_OK;
mz_zip_reader_close(handle);
mz_stream_os_create(&reader->file_stream);
mz_stream_buffered_create(&reader->buffered_stream);
mz_stream_split_create(&reader->split_stream);
mz_stream_set_base(reader->buffered_stream, reader->file_stream);
mz_stream_set_base(reader->split_stream, reader->buffered_stream);
err = mz_stream_open(reader->split_stream, path, MZ_OPEN_MODE_READ);
if (err == MZ_OK)
err = mz_zip_reader_open(handle, reader->split_stream);
return err;
}
int32_t mz_zip_reader_open_file_in_memory(void *handle, const char *path)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
void *file_stream = NULL;
int64_t file_size = 0;
int32_t err = 0;
mz_zip_reader_close(handle);
mz_stream_os_create(&file_stream);
err = mz_stream_os_open(file_stream, path, MZ_OPEN_MODE_READ);
if (err != MZ_OK)
{
mz_stream_os_delete(&file_stream);
mz_zip_reader_close(handle);
return err;
}
mz_stream_os_seek(file_stream, 0, MZ_SEEK_END);
file_size = mz_stream_os_tell(file_stream);
mz_stream_os_seek(file_stream, 0, MZ_SEEK_SET);
if ((file_size <= 0) || (file_size > UINT32_MAX))
{
// Memory size is too large or too small
mz_stream_os_close(file_stream);
mz_stream_os_delete(&file_stream);
mz_zip_reader_close(handle);
return MZ_MEM_ERROR;
}
mz_stream_mem_create(&reader->mem_stream);
mz_stream_mem_set_grow_size(reader->mem_stream, (int32_t)file_size);
mz_stream_mem_open(reader->mem_stream, NULL, MZ_OPEN_MODE_CREATE);
err = mz_stream_copy(reader->mem_stream, file_stream, (int32_t)file_size);
mz_stream_os_close(file_stream);
mz_stream_os_delete(&file_stream);
if (err == MZ_OK)
err = mz_zip_reader_open(handle, reader->mem_stream);
if (err != MZ_OK)
mz_zip_reader_close(handle);
return err;
}
int32_t mz_zip_reader_open_buffer(void *handle, uint8_t *buf, int32_t len, uint8_t copy)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
int32_t err = MZ_OK;
mz_zip_reader_close(handle);
mz_stream_mem_create(&reader->mem_stream);
if (copy)
{
mz_stream_mem_set_grow_size(reader->mem_stream, len);
mz_stream_mem_open(reader->mem_stream, NULL, MZ_OPEN_MODE_CREATE);
mz_stream_mem_write(reader->mem_stream, buf, len);
mz_stream_mem_seek(reader->mem_stream, 0, MZ_SEEK_SET);
}
else
{
mz_stream_mem_open(reader->mem_stream, NULL, MZ_OPEN_MODE_READ);
mz_stream_mem_set_buffer(reader->mem_stream, buf, len);
}
if (err == MZ_OK)
err = mz_zip_reader_open(handle, reader->mem_stream);
return err;
}
int32_t mz_zip_reader_close(void *handle)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
int32_t err = MZ_OK;
if (reader->zip_handle != NULL)
{
err = mz_zip_close(reader->zip_handle);
mz_zip_delete(&reader->zip_handle);
}
if (reader->split_stream != NULL)
{
mz_stream_split_close(reader->split_stream);
mz_stream_split_delete(&reader->split_stream);
}
if (reader->buffered_stream != NULL)
mz_stream_buffered_delete(&reader->buffered_stream);
if (reader->file_stream != NULL)
mz_stream_os_delete(&reader->file_stream);
if (reader->mem_stream != NULL)
{
mz_stream_mem_close(reader->mem_stream);
mz_stream_mem_delete(&reader->mem_stream);
}
return err;
}
/***************************************************************************/
int32_t mz_zip_reader_unzip_cd(void *handle)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
mz_zip_file *cd_info = NULL;
void *cd_mem_stream = NULL;
void *new_cd_stream = NULL;
void *file_extra_stream = NULL;
uint64_t number_entry = 0;
int32_t err = MZ_OK;
err = mz_zip_reader_goto_first_entry(handle);
if (err != MZ_OK)
return err;
err = mz_zip_reader_entry_get_info(handle, &cd_info);
if (err != MZ_OK)
return err;
if (strcmp(cd_info->filename, MZ_ZIP_CD_FILENAME) != 0)
return mz_zip_reader_goto_first_entry(handle);
err = mz_zip_reader_entry_open(handle);
if (err != MZ_OK)
return err;
mz_stream_mem_create(&file_extra_stream);
mz_stream_mem_set_buffer(file_extra_stream, (void *)cd_info->extrafield, cd_info->extrafield_size);
err = mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_CDCD, NULL);
if (err == MZ_OK)
err = mz_stream_read_uint64(file_extra_stream, &number_entry);
mz_stream_mem_delete(&file_extra_stream);
if (err != MZ_OK)
return err;
mz_zip_get_cd_mem_stream(reader->zip_handle, &cd_mem_stream);
if (mz_stream_mem_is_open(cd_mem_stream) != MZ_OK)
mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
err = mz_stream_copy_stream(cd_mem_stream, NULL, handle, mz_zip_reader_entry_read,
(int32_t)cd_info->uncompressed_size);
if (err == MZ_OK)
{
mz_zip_set_cd_start_pos(reader->zip_handle, 0);
mz_zip_set_number_entry(reader->zip_handle, number_entry);
err = mz_zip_reader_goto_first_entry(handle);
}
reader->cd_verified = reader->entry_verified;
mz_stream_mem_delete(&new_cd_stream);
return err;
}
/***************************************************************************/
static int32_t mz_zip_reader_locate_entry_cb(void *handle, void *userdata, mz_zip_file *file_info)
{
mz_zip_reader *reader = (mz_zip_reader *)userdata;
int32_t result = 0;
MZ_UNUSED(handle);
result = mz_path_compare_wc(file_info->filename, reader->pattern, reader->pattern_ignore_case);
return result;
}
int32_t mz_zip_reader_goto_first_entry(void *handle)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
int32_t err = MZ_OK;
if (mz_zip_reader_is_open(handle) != MZ_OK)
return MZ_PARAM_ERROR;
if (mz_zip_entry_is_open(reader->zip_handle) == MZ_OK)
mz_zip_reader_entry_close(handle);
if (reader->pattern == NULL)
err = mz_zip_goto_first_entry(reader->zip_handle);
else
err = mz_zip_locate_first_entry(reader->zip_handle, reader, mz_zip_reader_locate_entry_cb);
reader->file_info = NULL;
if (err == MZ_OK)
err = mz_zip_entry_get_info(reader->zip_handle, &reader->file_info);
return err;
}
int32_t mz_zip_reader_goto_next_entry(void *handle)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
int32_t err = MZ_OK;
if (mz_zip_reader_is_open(handle) != MZ_OK)
return MZ_PARAM_ERROR;
if (mz_zip_entry_is_open(reader->zip_handle) == MZ_OK)
mz_zip_reader_entry_close(handle);
if (reader->pattern == NULL)
err = mz_zip_goto_next_entry(reader->zip_handle);
else
err = mz_zip_locate_next_entry(reader->zip_handle, reader, mz_zip_reader_locate_entry_cb);
reader->file_info = NULL;
if (err == MZ_OK)
err = mz_zip_entry_get_info(reader->zip_handle, &reader->file_info);
return err;
}
int32_t mz_zip_reader_locate_entry(void *handle, const char *filename, uint8_t ignore_case)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
int32_t err = MZ_OK;
if (mz_zip_entry_is_open(reader->zip_handle) == MZ_OK)
mz_zip_reader_entry_close(handle);
err = mz_zip_locate_entry(reader->zip_handle, filename, ignore_case);
reader->file_info = NULL;
if (err == MZ_OK)
err = mz_zip_entry_get_info(reader->zip_handle, &reader->file_info);
return err;
}
/***************************************************************************/
int32_t mz_zip_reader_entry_open(void *handle)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
int32_t err = MZ_OK;
const char *password = NULL;
char password_buf[120];
reader->entry_verified = 0;
if (mz_zip_reader_is_open(reader) != MZ_OK)
return MZ_PARAM_ERROR;
if (reader->file_info == NULL)
return MZ_PARAM_ERROR;
// If the entry isn't open for reading, open it
if (mz_zip_entry_is_open(reader->zip_handle) == MZ_OK)
return MZ_OK;
password = reader->password;
// Check if we need a password and ask for it if we need to
if ((reader->file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL) &&
(reader->password_cb != NULL))
{
reader->password_cb(handle, reader->password_userdata, reader->file_info,
password_buf, sizeof(password_buf));
password = password_buf;
}
err = mz_zip_entry_read_open(reader->zip_handle, reader->raw, password);
#ifndef MZ_ZIP_NO_ENCRYPTION
if (err != MZ_OK)
return err;
if (mz_zip_reader_entry_get_first_hash(handle, &reader->hash_algorithm, &reader->hash_digest_size) == MZ_OK)
{
mz_crypt_sha_create(&reader->hash);
if (reader->hash_algorithm == MZ_HASH_SHA1)
mz_crypt_sha_set_algorithm(reader->hash, MZ_HASH_SHA1);
else if (reader->hash_algorithm == MZ_HASH_SHA256)
mz_crypt_sha_set_algorithm(reader->hash, MZ_HASH_SHA256);
else
err = MZ_SUPPORT_ERROR;
if (err == MZ_OK)
mz_crypt_sha_begin(reader->hash);
#ifndef MZ_ZIP_NO_SIGNING
if (err == MZ_OK)
{
if (mz_zip_reader_entry_has_sign(handle) == MZ_OK)
{
err = mz_zip_reader_entry_sign_verify(handle);
if (err == MZ_OK)
reader->entry_verified = 1;
}
else if (reader->sign_required && !reader->cd_verified)
err = MZ_SIGN_ERROR;
}
#endif
}
else if (reader->sign_required && !reader->cd_verified)
err = MZ_SIGN_ERROR;
#endif
return err;
}
int32_t mz_zip_reader_entry_close(void *handle)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
int32_t err = MZ_OK;
int32_t err_close = MZ_OK;
#ifndef MZ_ZIP_NO_ENCRYPTION
int32_t err_hash = MZ_OK;
uint8_t computed_hash[MZ_HASH_MAX_SIZE];
uint8_t expected_hash[MZ_HASH_MAX_SIZE];
if (reader->hash != NULL)
{
mz_crypt_sha_end(reader->hash, computed_hash, sizeof(computed_hash));
mz_crypt_sha_delete(&reader->hash);
err_hash = mz_zip_reader_entry_get_hash(handle, reader->hash_algorithm, expected_hash,
reader->hash_digest_size);
if (err_hash == MZ_OK)
{
// Verify expected hash against computed hash
if (memcmp(computed_hash, expected_hash, reader->hash_digest_size) != 0)
err = MZ_CRC_ERROR;
}
}
#endif
err_close = mz_zip_entry_close(reader->zip_handle);
if (err == MZ_OK)
err = err_close;
return err;
}
int32_t mz_zip_reader_entry_read(void *handle, void *buf, int32_t len)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
int32_t read = 0;
read = mz_zip_entry_read(reader->zip_handle, buf, len);
#ifndef MZ_ZIP_NO_ENCRYPTION
if ((read > 0) && (reader->hash != NULL))
mz_crypt_sha_update(reader->hash, buf, read);
#endif
return read;
}
int32_t mz_zip_reader_entry_has_sign(void *handle)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
void *file_extra_stream = NULL;
int32_t err = MZ_OK;
mz_stream_mem_create(&file_extra_stream);
mz_stream_mem_set_buffer(file_extra_stream, (void *)reader->file_info->extrafield,
reader->file_info->extrafield_size);
err = mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_SIGN, NULL);
mz_stream_mem_delete(&file_extra_stream);
return err;
}
#if !defined(MZ_ZIP_NO_ENCRYPTION) && !defined(MZ_ZIP_NO_SIGNING)
int32_t mz_zip_reader_entry_sign_verify(void *handle)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
void *file_extra_stream = NULL;
int32_t err = MZ_OK;
uint8_t *signature = NULL;
uint16_t signature_size = 0;
uint8_t hash[MZ_HASH_MAX_SIZE];
if (reader == NULL || mz_zip_entry_is_open(reader->zip_handle) != MZ_OK)
return MZ_PARAM_ERROR;
mz_stream_mem_create(&file_extra_stream);
mz_stream_mem_set_buffer(file_extra_stream, (void *)reader->file_info->extrafield,
reader->file_info->extrafield_size);
err = mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_SIGN, &signature_size);
if ((err == MZ_OK) && (signature_size > 0))
{
signature = (uint8_t *)MZ_ALLOC(signature_size);
if (mz_stream_read(file_extra_stream, signature, signature_size) != signature_size)
err = MZ_READ_ERROR;
}
mz_stream_mem_delete(&file_extra_stream);
if (err == MZ_OK)
{
// Get most secure hash to verify signature against
err = mz_zip_reader_entry_get_hash(handle, reader->hash_algorithm, hash, reader->hash_digest_size);
}
if (err == MZ_OK)
{
// Verify the pkcs signature
err = mz_crypt_sign_verify(hash, reader->hash_digest_size, signature, signature_size);
}
if (signature != NULL)
MZ_FREE(signature);
return err;
}
#endif
int32_t mz_zip_reader_entry_get_hash(void *handle, uint16_t algorithm, uint8_t *digest, int32_t digest_size)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
void *file_extra_stream = NULL;
int32_t err = MZ_OK;
int32_t return_err = MZ_EXIST_ERROR;
uint16_t cur_algorithm = 0;
uint16_t cur_digest_size = 0;
mz_stream_mem_create(&file_extra_stream);
mz_stream_mem_set_buffer(file_extra_stream, (void *)reader->file_info->extrafield,
reader->file_info->extrafield_size);
do
{
err = mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_HASH, NULL);
if (err != MZ_OK)
break;
err = mz_stream_read_uint16(file_extra_stream, &cur_algorithm);
if (err == MZ_OK)
err = mz_stream_read_uint16(file_extra_stream, &cur_digest_size);
if ((cur_algorithm == algorithm) && (cur_digest_size <= digest_size) &&
(cur_digest_size <= MZ_HASH_MAX_SIZE))
{
// Read hash digest
if (mz_stream_read(file_extra_stream, digest, digest_size) != cur_digest_size)
err = MZ_FORMAT_ERROR;
else
return_err = MZ_OK;
break;
}
else
{
err = mz_stream_seek(file_extra_stream, cur_digest_size, MZ_SEEK_CUR);
}
}
while (err == MZ_OK);
mz_stream_mem_delete(&file_extra_stream);
return return_err;
}
int32_t mz_zip_reader_entry_get_first_hash(void *handle, uint16_t *algorithm, uint16_t *digest_size)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
void *file_extra_stream = NULL;
int32_t err = MZ_OK;
uint16_t cur_algorithm = 0;
uint16_t cur_digest_size = 0;
if (reader == NULL || algorithm == NULL)
return MZ_PARAM_ERROR;
mz_stream_mem_create(&file_extra_stream);
mz_stream_mem_set_buffer(file_extra_stream, (void *)reader->file_info->extrafield,
reader->file_info->extrafield_size);
err = mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_HASH, NULL);
if (err == MZ_OK)
err = mz_stream_read_uint16(file_extra_stream, &cur_algorithm);
if (err == MZ_OK)
err = mz_stream_read_uint16(file_extra_stream, &cur_digest_size);
if (algorithm != NULL)
*algorithm = cur_algorithm;
if (digest_size != NULL)
*digest_size = cur_digest_size;
mz_stream_mem_delete(&file_extra_stream);
return err;
}
int32_t mz_zip_reader_entry_get_info(void *handle, mz_zip_file **file_info)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
int32_t err = MZ_OK;
if (file_info == NULL || mz_zip_reader_is_open(handle) != MZ_OK)
return MZ_PARAM_ERROR;
*file_info = reader->file_info;
if (*file_info == NULL)
return MZ_EXIST_ERROR;
return err;
}
int32_t mz_zip_reader_entry_is_dir(void *handle)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
if (mz_zip_reader_is_open(handle) != MZ_OK)
return MZ_PARAM_ERROR;
return mz_zip_entry_is_dir(reader->zip_handle);
}
int32_t mz_zip_reader_entry_save_process(void *handle, void *stream, mz_stream_write_cb write_cb)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
int32_t err = MZ_OK;
int32_t read = 0;
int32_t written = 0;
if (mz_zip_reader_is_open(reader) != MZ_OK)
return MZ_PARAM_ERROR;
if (reader->file_info == NULL)
return MZ_PARAM_ERROR;
if (write_cb == NULL)
return MZ_PARAM_ERROR;
// If the entry isn't open for reading, open it
if (mz_zip_entry_is_open(reader->zip_handle) != MZ_OK)
err = mz_zip_reader_entry_open(handle);
if (err != MZ_OK)
return err;
// Unzip entry in zip file
read = mz_zip_reader_entry_read(handle, reader->buffer, sizeof(reader->buffer));
if (read == 0)
{
// If we are done close the entry
err = mz_zip_reader_entry_close(handle);
if (err != MZ_OK)
return err;
return MZ_END_OF_STREAM;
}
if (read > 0)
{
// Write the data to the specified stream
written = write_cb(stream, reader->buffer, read);
if (written != read)
return MZ_WRITE_ERROR;
}
return read;
}
int32_t mz_zip_reader_entry_save(void *handle, void *stream, mz_stream_write_cb write_cb)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
uint64_t current_time = 0;
uint64_t update_time = 0;
int64_t current_pos = 0;
int64_t update_pos = 0;
int32_t err = MZ_OK;
int32_t written = 0;
if (mz_zip_reader_is_open(reader) != MZ_OK)
return MZ_PARAM_ERROR;
if (reader->file_info == NULL)
return MZ_PARAM_ERROR;
// Update the progress at the beginning
if (reader->progress_cb != NULL)
reader->progress_cb(handle, reader->progress_userdata, reader->file_info, current_pos);
// Write data to stream until done
while (err == MZ_OK)
{
written = mz_zip_reader_entry_save_process(handle, stream, write_cb);
if (written == MZ_END_OF_STREAM)
break;
if (written > 0)
current_pos += written;
if (written < 0)
err = written;
// Update progress if enough time have passed
current_time = mz_os_ms_time();
if ((current_time - update_time) > reader->progress_cb_interval_ms)
{
if (reader->progress_cb != NULL)
reader->progress_cb(handle, reader->progress_userdata, reader->file_info, current_pos);
update_pos = current_pos;
update_time = current_time;
}
}
// Update the progress at the end
if (reader->progress_cb != NULL && update_pos != current_pos)
reader->progress_cb(handle, reader->progress_userdata, reader->file_info, current_pos);
return err;
}
int32_t mz_zip_reader_entry_save_file(void *handle, const char *path)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
void *file_stream = NULL;
uint32_t target_attrib = 0;
int32_t err_attrib = 0;
int32_t err = MZ_OK;
int32_t err_cb = MZ_OK;
char directory[512];
if (mz_zip_reader_is_open(reader) != MZ_OK)
return MZ_PARAM_ERROR;
if (reader->file_info == NULL || path == NULL)
return MZ_PARAM_ERROR;
if (reader->entry_cb != NULL)
reader->entry_cb(handle, reader->entry_userdata, reader->file_info, path);
strncpy(directory, path, sizeof(directory) - 1);
directory[sizeof(directory) - 1] = 0;
mz_path_remove_filename(directory);
// If it is a directory entry then create a directory instead of writing file
if (mz_zip_entry_is_dir(reader->zip_handle) == MZ_OK)
{
err = mz_dir_make(directory);
return err;
}
// Check if file exists and ask if we want to overwrite
if ((mz_os_file_exists(path) == MZ_OK) && (reader->overwrite_cb != NULL))
{
err_cb = reader->overwrite_cb(handle, reader->overwrite_userdata, reader->file_info, path);
if (err_cb != MZ_OK)
return err;
mz_os_delete(path);
}
// Create the output directory if it doesn't already exist
if (mz_os_is_dir(directory) != MZ_OK)
{
err = mz_dir_make(directory);
if (err != MZ_OK)
return err;
}
mz_stream_os_create(&file_stream);
// Create the file on disk so we can save to it
err = mz_stream_os_open(file_stream, path, MZ_OPEN_MODE_CREATE);
if (err == MZ_OK)
err = mz_zip_reader_entry_save(handle, file_stream, mz_stream_os_write);
mz_stream_os_close(file_stream);
mz_stream_os_delete(&file_stream);
if (err == MZ_OK)
{
// Set the time of the file that has been created
mz_os_set_file_date(path, reader->file_info->modified_date,
reader->file_info->accessed_date, reader->file_info->creation_date);
}
if (err == MZ_OK)
{
// Set file attributes for the correct system
err_attrib = mz_zip_attrib_convert(MZ_HOST_SYSTEM(reader->file_info->version_madeby),
reader->file_info->external_fa, MZ_VERSION_MADEBY_HOST_SYSTEM, &target_attrib);
if (err_attrib == MZ_OK)
mz_os_set_file_attribs(path, target_attrib);
}
return err;
}
int32_t mz_zip_reader_entry_save_buffer(void *handle, void *buf, int32_t len)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
void *mem_stream = NULL;
int32_t err = MZ_OK;
if (mz_zip_reader_is_open(reader) != MZ_OK)
return MZ_PARAM_ERROR;
if (reader->file_info == NULL)
return MZ_PARAM_ERROR;
if (reader->file_info->uncompressed_size > INT32_MAX)
return MZ_PARAM_ERROR;
if (len != (int32_t)reader->file_info->uncompressed_size)
return MZ_PARAM_ERROR;
// Create a memory stream backed by our buffer and save to it
mz_stream_mem_create(&mem_stream);
mz_stream_mem_set_buffer(mem_stream, buf, len);
err = mz_stream_mem_open(mem_stream, NULL, MZ_OPEN_MODE_READ);
if (err == MZ_OK)
err = mz_zip_reader_entry_save(handle, mem_stream, mz_stream_mem_write);
mz_stream_mem_delete(&mem_stream);
return err;
}
int32_t mz_zip_reader_entry_save_buffer_length(void *handle)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
if (mz_zip_reader_is_open(reader) != MZ_OK)
return MZ_PARAM_ERROR;
if (reader->file_info == NULL)
return MZ_PARAM_ERROR;
if (reader->file_info->uncompressed_size > INT32_MAX)
return MZ_PARAM_ERROR;
// Get the maximum size required for the save buffer
return (int32_t)reader->file_info->uncompressed_size;
}
/***************************************************************************/
int32_t mz_zip_reader_save_all(void *handle, const char *destination_dir)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
int32_t err = MZ_OK;
uint8_t *utf8_string = NULL;
char path[512];
char utf8_name[256];
char resolved_name[256];
err = mz_zip_reader_goto_first_entry(handle);
while (err == MZ_OK)
{
// Construct output path
path[0] = 0;
strncpy(utf8_name, reader->file_info->filename, sizeof(utf8_name) - 1);
utf8_name[sizeof(utf8_name) - 1] = 0;
if ((reader->encoding > 0) && (reader->file_info->flag & MZ_ZIP_FLAG_UTF8) == 0)
{
utf8_string = mz_os_utf8_string_create(reader->file_info->filename, reader->encoding);
if (utf8_string)
{
strncpy(utf8_name, (char *)utf8_string, sizeof(utf8_name));
mz_os_utf8_string_delete(&utf8_string);
}
}
err = mz_path_resolve(utf8_name, resolved_name, sizeof(resolved_name));
if (err != MZ_OK)
break;
if (destination_dir != NULL)
mz_path_combine(path, destination_dir, sizeof(path));
mz_path_combine(path, resolved_name, sizeof(path));
// Save file to disk
err = mz_zip_reader_entry_save_file(handle, path);
if (err == MZ_OK)
err = mz_zip_reader_goto_next_entry(handle);
}
if (err == MZ_END_OF_LIST)
return MZ_OK;
return err;
}
/***************************************************************************/
void mz_zip_reader_set_pattern(void *handle, const char *pattern, uint8_t ignore_case)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
reader->pattern = pattern;
reader->pattern_ignore_case = ignore_case;
}
void mz_zip_reader_set_password(void *handle, const char *password)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
reader->password = password;
}
void mz_zip_reader_set_raw(void *handle, uint8_t raw)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
reader->raw = raw;
}
int32_t mz_zip_reader_get_raw(void *handle, uint8_t *raw)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
if (raw == NULL)
return MZ_PARAM_ERROR;
*raw = reader->raw;
return MZ_OK;
}
int32_t mz_zip_reader_get_comment(void *handle, const char **comment)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
if (mz_zip_reader_is_open(reader) != MZ_OK)
return MZ_PARAM_ERROR;
if (comment == NULL)
return MZ_PARAM_ERROR;
return mz_zip_get_comment(reader->zip_handle, comment);
}
void mz_zip_reader_set_encoding(void *handle, int32_t encoding)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
reader->encoding = encoding;
}
void mz_zip_reader_set_sign_required(void *handle, uint8_t sign_required)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
reader->sign_required = sign_required;
}
void mz_zip_reader_set_overwrite_cb(void *handle, void *userdata, mz_zip_reader_overwrite_cb cb)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
reader->overwrite_cb = cb;
reader->overwrite_userdata = userdata;
}
void mz_zip_reader_set_password_cb(void *handle, void *userdata, mz_zip_reader_password_cb cb)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
reader->password_cb = cb;
reader->password_userdata = userdata;
}
void mz_zip_reader_set_progress_cb(void *handle, void *userdata, mz_zip_reader_progress_cb cb)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
reader->progress_cb = cb;
reader->progress_userdata = userdata;
}
void mz_zip_reader_set_progress_interval(void *handle, uint32_t milliseconds)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
reader->progress_cb_interval_ms = milliseconds;
}
void mz_zip_reader_set_entry_cb(void *handle, void *userdata, mz_zip_reader_entry_cb cb)
{
mz_zip_reader *reader = (mz_zip_reader *)handle;
reader->entry_cb = cb;
reader->entry_userdata = userdata;
}