Repository navigation
Expand file tree
/
Copy pathminizip.c
More file actions
1303 lines (1105 loc) · 41.8 KB
/
Copy pathminizip.c
File metadata and controls
1303 lines (1105 loc) · 41.8 KB
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
/* minizip.c
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_buf.h"
#include "mz_strm_os.h"
#include "mz_strm_split.h"
#include "mz_zip.h"
#include "mz_zip_rw.h"
#include <ctype.h>
#include <stdio.h> /* printf */
#if defined(_WIN32)
# include <windows.h>
#endif
/***************************************************************************/
typedef struct minizip_opt_s {
int64_t disk_size;
int32_t encoding;
uint8_t include_path;
int16_t compress_level;
uint8_t compress_method;
uint8_t overwrite;
uint8_t append;
uint8_t follow_links;
uint8_t store_links;
uint8_t zip_cd;
uint8_t verbose;
uint8_t aes;
char *current_path;
} minizip_opt;
/***************************************************************************/
int32_t minizip_banner(void);
int32_t minizip_help(void);
int32_t minizip_list(const char *path, int32_t encoding);
int32_t minizip_add_entry_cb(void *handle, void *userdata, mz_zip_file *file_info);
int32_t minizip_add_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
int32_t minizip_add_overwrite_cb(void *handle, void *userdata, const char *path);
int32_t minizip_add(const char *path, const char *password, minizip_opt *options, int32_t arg_count, const char **args);
int32_t minizip_extract_entry_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
int32_t minizip_extract_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
int32_t minizip_extract_overwrite_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
int32_t minizip_extract(const char *path, const char *pattern, const char *destination, const char *password,
minizip_opt *options);
int32_t minizip_erase(const char *src_path, const char *target_path, int32_t arg_count, const char **args);
/***************************************************************************/
int32_t minizip_banner(void) {
printf("minizip-ng %s - https://github.com/zlib-ng/minizip-ng\n", MZ_VERSION);
printf("---------------------------------------------------\n");
return MZ_OK;
}
int32_t minizip_help(void) {
printf(
"Usage: minizip [-x][-d dir|-l|-e][-o][-f]
10BC8
[-y][-c cp][-a][-0 to -9][-b|-m|-t|-g][-k 512][-p pwd][-s] file.zip "
"[files]\n\n"
" -x Extract files\n"
" -l List files\n"
" -d Destination directory\n"
" -e Erase files\n"
" -o Overwrite existing files\n"
" -c File names use cp437 encoding (or specified codepage)\n"
" -a Append to existing zip file\n"
" -i Include full path of files\n"
" -f Follow symbolic links\n"
" -y Store symbolic links\n"
" -v Verbose info\n"
" -0 Store only\n"
" -1 Compress faster\n"
" -9 Compress better\n"
" -k Disk size in KB\n"
" -z Zip central directory\n"
" -p Encryption password\n"
" -s AES encryption\n"
" -b BZIP2 compression\n"
" -m LZMA compression\n"
" -n XZ compression\n"
" -t ZSTD compression\n"
" -g PPMD compression\n\n");
return MZ_OK;
}
/***************************************************************************/
int32_t minizip_list(const char *path, int32_t encoding) {
mz_zip_file *file_info = NULL;
uint32_t ratio = 0;
int32_t err = MZ_OK;
int32_t entry_encoding = 0;
struct tm tmu_date;
const char *method = NULL;
char *utf8_string = NULL;
char crypt = ' ';
void *reader = NULL;
reader = mz_zip_reader_create();
if (!reader)
return MZ_MEM_ERROR;
err = mz_zip_reader_open_file(reader, path);
if (err != MZ_OK) {
printf("Error %" PRId32 " opening archive %s\n", err, path);
mz_zip_reader_delete(&reader);
return err;
}
mz_zip_reader_set_encoding(reader, encoding);
err = mz_zip_reader_goto_first_entry(reader);
if (err != MZ_OK && err != MZ_END_OF_LIST) {
printf("Error %" PRId32 " going to first entry in archive\n", err);
mz_zip_reader_delete(&reader);
return err;
}
printf(" Packed Unpacked Ratio Method Attribs Date Time CRC-32 Name\n");
printf(" ------ -------- ----- ------ ------- ---- ---- ------ ----\n");
/* Enumerate all entries in the archive */
while (err == MZ_OK) {
err = mz_zip_reader_entry_get_info(reader, &file_info);
if (err != MZ_OK) {
printf("Error %" PRId32 " getting entry info in archive\n", err);
break;
}
ratio = 0;
if (file_info->uncompressed_size > 0)
ratio = (uint32_t)((file_info->compressed_size * 100) / file_info->uncompressed_size);
/* Display a '*' if the file is encrypted */
if (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED)
crypt = '*';
else
crypt = ' ';
method = mz_zip_get_compression_method_string(file_info->compression_method);
mz_zip_time_t_to_tm(file_info->modified_date, &tmu_date);
entry_encoding = encoding;
if ((entry_encoding <= 0) && (file_info->flag & MZ_ZIP_FLAG_UTF8) == 0 &&
mz_os_utf8_string_is_valid(file_info->filename) != MZ_OK) {
/* Not valid utf8 and no encoding specified; use the system
default code page as a fallback */
entry_encoding = mz_os_get_default_encoding();
}
if ((entry_encoding > 0) && (file_info->flag & MZ_ZIP_FLAG_UTF8) == 0) {
utf8_string = mz_os_utf8_string_create(file_info->filename, entry_encoding);
if (!utf8_string) {
err = MZ_MEM_ERROR;
printf("Error %" PRId32 " creating UTF-8 string\n", err);
break;
}
}
/* Print entry information */
printf("%12" PRId64 " %12" PRId64 " %3" PRIu32 "%% %6s%c %8" PRIx32 " %2.2" PRIu32 "-%2.2" PRIu32
"-%2.2" PRIu32 " %2.2" PRIu32 ":%2.2" PRIu32 " %8.8" PRIx32 " %s\n",
file_info->compressed_size, file_info->uncompressed_size, ratio, method, crypt, file_info->external_fa,
(uint32_t)tmu_date.tm_mon + 1, (uint32_t)tmu_date.tm_mday, (uint32_t)tmu_date.tm_year % 100,
(uint32_t)tmu_date.tm_hour, (uint32_t)tmu_date.tm_min, file_info->crc,
utf8_string ? utf8_string : file_info->filename);
if (utf8_string)
mz_os_utf8_string_delete(&utf8_string);
err = mz_zip_reader_goto_next_entry(reader);
if (err != MZ_OK && err != MZ_END_OF_LIST) {
printf("Error %" PRId32 " going to next entry in archive\n", err);
break;
}
}
mz_zip_reader_delete(&reader);
if (err == MZ_END_OF_LIST)
return MZ_OK;
return err;
}
/***************************************************************************/
int32_t minizip_add_entry_cb(void *handle, void *userdata, mz_zip_file *file_info) {
MZ_UNUSED(handle);
MZ_UNUSED(userdata);
/* Print the current file we are trying to compress */
printf("Adding %s\n", file_info->filename);
return MZ_OK;
}
int32_t minizip_add_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position) {
minizip_opt *options = (minizip_opt *)userdata;
double progress = 0;
uint8_t raw = 0;
MZ_UNUSED(userdata);
mz_zip_writer_get_raw(handle, &raw);
if (raw && file_info->compressed_size > 0)
progress = ((double)position / file_info->compressed_size) * 100;
else if (!raw && file_info->uncompressed_size > 0)
progress = ((double)position / file_info->uncompressed_size) * 100;
/* Print the progress of the current compress operation */
if (options->verbose) {
printf("%s - %" PRId64 " / %" PRId64 " (%.02f%%)\n", file_info->filename, position,
file_info->uncompressed_size, progress);
}
return MZ_OK;
}
int32_t minizip_add_overwrite_cb(void *handle, void *userdata, const char *path) {
minizip_opt *options = (minizip_opt *)userdata;
MZ_UNUSED(handle);
if (!options->overwrite) {
/* If ask the user what to do because append and overwrite args not set */
char rep = 0;
do {
char answer[128];
printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ", path);
if (scanf("%1s", answer) != 1)
exit(EXIT_FAILURE);
rep = answer[0];
if ((rep >= 'a') && (rep <= 'z'))
rep -= 0x20;
} while ((rep != 'Y') && (rep != 'N') && (rep != 'A'));
if (rep == 'A') {
return MZ_EXIST_ERROR;
} else if (rep == 'N') {
return MZ_INTERNAL_ERROR;
}
}
return MZ_OK;
}
static int32_t minizip_path_combine_alloc(const char *path, const char *name, char **combined_path) {
size_t path_length = 0;
size_t name_length = 0;
size_t combined_length = 0;
uint8_t append_slash = 0;
if (!path || !name || !combined_path)
return MZ_PARAM_ERROR;
*combined_path = NULL;
path_length = strlen(path);
name_length = strlen(name);
append_slash = path_length > 0 && !mz_os_is_dir_separator(path[path_length - 1]);
if (path_length > (size_t)-1 - name_length - append_slash - 1)
return MZ_MEM_ERROR;
combined_length = path_length + append_slash + name_length + 1;
*combined_path = (char *)malloc(combined_length);
if (!*combined_path)
return MZ_MEM_ERROR;
memcpy(*combined_path, path, path_length);
if (append_slash)
(*combined_path)[path_length++] = MZ_PATH_SLASH_PLATFORM;
memcpy(*combined_path + path_length, name, name_length + 1);
return MZ_OK;
}
static int32_t minizip_resolve_path(const char *path, char **resolved_path) {
const char *filename = NULL;
char *component = NULL;
char *current_path = NULL;
char *remaining_path = NULL;
char *next_path = NULL;
char *resolved_parent = NULL;
int32_t err = MZ_OK;
if (!path || !resolved_path)
return MZ_PARAM_ERROR;
*resolved_path = NULL;
current_path = (char *)strdup(path);
if (!current_path)
return MZ_MEM_ERROR;
while (1) {
err = mz_os_get_replace_path(current_path, &resolved_parent);
if (err == MZ_OK)
break;
if (err != MZ_EXIST_ERROR)
goto cleanup;
err = mz_path_get_filename(current_path, &filename);
if (err != MZ_OK || filename[0] == 0) {
err = MZ_EXIST_ERROR;
goto cleanup;
}
component = (char *)strdup(filename);
if (!component) {
err = MZ_MEM_ERROR;
goto cleanup;
}
if (remaining_path) {
err = minizip_path_combine_alloc(component, remaining_path, &next_path);
free(component);
component = NULL;
if (err != MZ_OK)
goto cleanup;
free(remaining_path);
remaining_path = next_path;
next_path = NULL;
} else {
remaining_path = component;
component = NULL;
}
mz_path_remove_filename(current_path);
if (current_path[0] == 0) {
free(current_path);
current_path = (char *)strdup(".");
if (!current_path) {
err = MZ_MEM_ERROR;
goto cleanup;
}
}
}
if (remaining_path) {
err = minizip_path_combine_alloc(resolved_parent, remaining_path, resolved_path);
} else {
*resolved_path = resolved_parent;
resolved_parent = NULL;
}
cleanup:
free(component);
free(current_path);
free(remaining_path);
free(next_path);
free(resolved_parent);
return err;
}
static uint8_t minizip_path_char_matches(char path_char, char expected_char) {
if (mz_os_is_dir_separator(path_char) && mz_os_is_dir_separator(expected_char))
return 1;
#if defined(_WIN32)
return (uint8_t)(tolower((unsigned char)path_char) == tolower((unsigned char)expected_char));
#else
return (uint8_t)(path_char == expected_char);
#endif
}
static uint8_t minizip_paths_equal(const char *path, const char *expected_path) {
if (!path || !expected_path)
return 0;
while (*path != 0 && *expected_path != 0) {
if (!minizip_path_char_matches(*path, *expected_path))
return 0;
path += 1;
expected_path += 1;
}
return (uint8_t)(*path == 0 && *expected_path == 0);
}
static uint8_t minizip_path_is_within(const char *path, const char *directory) {
size_t directory_length = 0;
size_t index = 0;
if (!path || !directory)
return 0;
directory_length = strlen(directory);
while (directory_length > 0 && mz_os_is_dir_separator(directory[directory_length - 1]))
directory_length -= 1;
for (index = 0; index < directory_length; index += 1) {
if (path[index] == 0 || !minizip_path_char_matches(path[index], directory[index]))
return 0;
}
if (directory_length == 0)
return (uint8_t)mz_os_is_dir_separator(path[0]);
return (uint8_t)mz_os_is_dir_separator(path[directory_length]);
}
static int32_t minizip_wildcard_may_include_path(const char *path, const char *wildcard_path, uint8_t *matches) {
const char *component_end = NULL;
const char *wildcard = NULL;
char *component = NULL;
char *directory = NULL;
char *resolved_directory = NULL;
size_t component_length = 0;
size_t directory_length = 0;
int32_t err = MZ_OK;
if (!path || !wildcard_path || !matches)
return MZ_PARAM_ERROR;
*matches = 0;
if (mz_path_get_filename(wildcard_path, &wildcard) != MZ_OK)
wildcard = wildcard_path;
directory = (char *)strdup(wildcard_path);
if (!directory)
return MZ_MEM_ERROR;
mz_path_remove_filename(directory);
if (directory[0] == 0) {
free(directory);
directory = (char *)strdup(".");
if (!directory)
return MZ_MEM_ERROR;
}
err = minizip_resolve_path(directory, &resolved_directory);
if (err != MZ_OK)
goto cleanup;
if (!minizip_path_is_within(path, resolved_directory))
goto cleanup;
directory_length = strlen(resolved_directory);
while (directory_length > 0 && mz_os_is_dir_separator(resolved_directory[directory_length - 1]))
directory_length -= 1;
path += directory_length;
while (mz_os_is_dir_separator(*path))
path += 1;
component_end = path;
while (*component_end != 0 && !mz_os_is_dir_separator(*component_end))
component_end += 1;
component_length = (size_t)(component_end - path);
component = (char *)malloc(component_length + 1);
if (!component) {
err = MZ_MEM_ERROR;
goto cleanup;
}
memcpy(component, path, component_length);
component[component_length] = 0;
if (mz_path_compare_wc(component, wildcard, 1) == MZ_OK)
*matches = 1;
cleanup:
free(component);
free(directory);
free(resolved_directory);
return err;
}
static int32_t minizip_inputs_may_include_output(const char *output_path, minizip_opt *options, int32_t arg_count,
const char **args, uint8_t *may_include) {
char *resolved_input = NULL;
char *resolved_output = NULL;
int32_t err = MZ_OK;
int32_t i = 0;
uint8_t matches = 0;
if (!output_path || !options || (!args && arg_count > 0) || !may_include || arg_count < 0)
return MZ_PARAM_ERROR;
*may_include = 0;
err = minizip_resolve_path(output_path, &resolved_output);
if (err != MZ_OK)
goto cleanup;
for (i = 0; i < arg_count && !*may_include; i += 1) {
if (strrchr(args[i], '*') && mz_os_file_exists(args[i]) != MZ_OK) {
err = minizip_wildcard_may_include_path(resolved_output, args[i], &matches);
if (err == MZ_OK && matches)
*may_include = 1;
if (err != MZ_OK)
break;
continue;
}
if (mz_os_is_symlink(args[i]) == MZ_OK && (options->store_links || !options->follow_links))
continue;
if (mz_os_path_same_file(args[i], output_path) == MZ_OK) {
*may_include = 1;
continue;
}
err = minizip_resolve_path(args[i], &resolved_input);
if (err != MZ_OK)
break;
if (mz_os_is_dir(args[i]) == MZ_OK)
*may_include = minizip_path_is_within(resolved_output, resolved_input);
else
*may_include = minizip_paths_equal(resolved_output, resolved_input);
free(resolved_input);
resolved_input = NULL;
}
cleanup:
free(resolved_input);
free(resolved_output);
return err;
}
static int32_t minizip_open_temp_file(void *writer, const char *path, char **temp_path) {
const char temp_suffix[] = ".mz_tmp.ffff";
size_t path_length = 0;
size_t temp_path_size = 0;
uint32_t attempt = 0;
int32_t err = MZ_OK;
char *candidate = NULL;
if (!writer || !path || !temp_path)
return MZ_PARAM_ERROR;
*temp_path = NULL;
path_length = strlen(path);
if (path_length > (size_t)-1 - sizeof(temp_suffix))
return MZ_MEM_ERROR;
temp_path_size = path_length + sizeof(temp_suffix);
candidate = (char *)malloc(temp_path_size);
if (!candidate)
return MZ_MEM_ERROR;
memcpy(candidate, path, path_length);
for (attempt = 0; attempt <= UINT16_MAX; attempt += 1) {
snprintf(candidate + path_length, temp_path_size - path_length, ".mz_tmp.%04" PRIx32, attempt);
err = mz_zip_writer_open_file_exclusive(writer, candidate);
if (err == MZ_OK) {
*temp_path = candidate;
return MZ_OK;
}
if (err != MZ_EXIST_ERROR)
break;
}
free(candidate);
return err;
}
static int32_t minizip_make_parent_directory(const char *path) {
char *directory = NULL;
int32_t err = MZ_OK;
if (!path)
return MZ_PARAM_ERROR;
directory = (char *)strdup(path);
if (!directory)
return MZ_MEM_ERROR;
mz_path_remove_filename(directory);
if (directory[0] != 0 && mz_os_is_dir(directory) != MZ_OK)
err = mz_dir_make(directory);
free(directory);
return err;
}
static int32_t minizip_parent_directory_missing(const char *path, uint8_t *missing) {
char *directory = NULL;
if (!path || !missing)
return MZ_PARAM_ERROR;
*missing = 0;
directory = (char *)strdup(path);
if (!directory)
return MZ_MEM_ERROR;
mz_path_remove_filename(directory);
if (directory[0] != 0 && mz_os_is_dir(directory) != MZ_OK)
*missing = 1;
free(directory);
return MZ_OK;
}
static int32_t minizip_copy_temp_file(const char *source_path, const char *path, char **temp_path) {
const char temp_suffix[] = ".mz_tmp.ffff";
size_t path_length = 0;
size_t temp_path_size = 0;
uint32_t attempt = 0;
int32_t err = MZ_OK;
int32_t err_close = MZ_OK;
char *candidate = NULL;
void *source_stream = NULL;
void *target_stream = NULL;
if (!source_path || !path || !temp_path)
return MZ_PARAM_ERROR;
*temp_path = NULL;
path_length = strlen(path);
if (path_length > (size_t)-1 - sizeof(temp_suffix))
return MZ_MEM_ERROR;
temp_path_size = path_length + sizeof(temp_suffix);
candidate = (char *)malloc(temp_path_size);
if (!candidate)
return MZ_MEM_ERROR;
memcpy(candidate, path, path_length);
source_stream = mz_stream_os_create();
if (!source_stream) {
free(candidate);
return MZ_MEM_ERROR;
}
err = mz_stream_os_open(source_stream, source_path, MZ_OPEN_MODE_READ);
for (attempt = 0; err == MZ_OK && attempt <= UINT16_MAX; attempt += 1) {
snprintf(candidate + path_length, temp_path_size - path_length, ".mz_tmp.%04" PRIx32, attempt);
target_stream = mz_stream_os_create();
if (!target_stream) {
err = MZ_MEM_ERROR;
break;
}
err = mz_stream_os_open(target_stream, candidate,
MZ_OPEN_MODE_WRITE | MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_EXCLUSIVE);
if (err == MZ_EXIST_ERROR) {
mz_stream_os_delete(&target_stream);
if (attempt == UINT16_MAX)
break;
err = MZ_OK;
continue;
}
if (err != MZ_OK)
break;
err = mz_stream_copy_to_end(target_stream, source_stream);
err_close = mz_stream_os_close(target_stream);
if (err == MZ_OK && err_close != MZ_OK)
err = err_close;
mz_stream_os_delete(&target_stream);
if (err == MZ_OK) {
*temp_path = candidate;
candidate = NULL;
} else {
mz_os_unlink(candidate);
}
break;
}
if (target_stream)
mz_stream_os_delete(&target_stream);
err_close = mz_stream_os_close(source_stream);
if (err == MZ_OK && err_close != MZ_OK) {
err = err_close;
if (*temp_path) {
mz_os_unlink(*temp_path);
free(*temp_path);
*temp_path = NULL;
}
}
mz_stream_os_delete(&source_stream);
free(candidate);
return err;
}
static int32_t minizip_process_paths(void *writer, minizip_opt *options, int32_t arg_count, const char **args,
uint8_t prepare) {
int32_t err = MZ_OK;
int32_t i = 0;
for (i = 0; i < arg_count; i += 1) {
if (prepare)
err = mz_zip_writer_prepare_path(writer, args[i], NULL, options->include_path, 1);
else
err = mz_zip_writer_add_path(writer, args[i], NULL, options->include_path, 1);
if (err != MZ_OK) {
printf("Error %" PRId32 " adding path to archive %s\n", err, args[i]);
break;
}
}
return err;
}
int32_t minizip_add(const char *path, const char *password, minizip_opt *options, int32_t arg_count,
const char **args) {
void *writer = NULL;
int32_t err = MZ_OK;
int32_t err_close = MZ_OK;
uint8_t append = options->append;
uint8_t archive_exists = 0;
uint8_t preserve_temp = 0;
uint8_t prepare_paths = 0;
uint8_t self_inclusion_possible = 0;
uint8_t temp_created = 0;
uint8_t use_temp_file = 0;
char *replace_path = NULL;
char *temp_path = NULL;
printf("Archive %s\n", path);
archive_exists = (mz_os_file_exists(path) == MZ_OK);
err = minizip_parent_directory_missing(path, &prepare_paths);
if (err == MZ_OK && !prepare_paths)
err = minizip_inputs_may_include_output(path, options, arg_count, args, &self_inclusion_possible);
if (err != MZ_OK)
return err;
use_temp_file = (options->disk_size == 0 && !prepare_paths && self_inclusion_possible);
if (use_temp_file && archive_exists && !append) {
err = minizip_add_overwrite_cb(NULL, options, path);
if (err == MZ_EXIST_ERROR) {
append = 1;
err = MZ_OK;
} else if (err != MZ_OK) {
return err;
}
}
/* Create zip writer */
writer = mz_zip_writer_create();
if (!writer)
return MZ_MEM_ERROR;
mz_zip_writer_set_password(writer, password);
mz_zip_writer_set_aes(writer, options->aes);
mz_zip_writer_set_compress_method(writer, options->compress_method);
mz_zip_writer_set_compress_level(writer, options->compress_level);
mz_zip_writer_set_follow_links(writer, options->follow_links);
mz_zip_writer_set_store_links(writer, options->store_links);
if (!use_temp_file)
mz_zip_writer_set_overwrite_cb(writer, options, minizip_add_overwrite_cb);
mz_zip_writer_set_progress_cb(writer, options, minizip_add_progress_cb);
mz_zip_writer_set_entry_cb(writer, options, minizip_add_entry_cb);
mz_zip_writer_set_zip_cd(writer, options->zip_cd);
if (err == MZ_OK)
err = mz_zip_writer_set_exclude_path(writer, path);
if (err == MZ_OK && prepare_paths)
err = minizip_process_paths(writer, options, arg_count, args, 1);
if (err == MZ_OK && use_temp_file) {
err = minizip_make_parent_directory(path);
if (err == MZ_OK)
err = mz_os_get_replace_path(path, &replace_path);
}
if (err == MZ_OK) {
if (use_temp_file) {
if (archive_exists && append) {
err = minizip_copy_temp_file(replace_path, replace_path, &temp_path);
if (err == MZ_OK) {
temp_created = 1;
err = mz_zip_writer_open_file(writer, temp_path, 0, 1);
}
} else {
err = minizip_open_temp_file(writer, replace_path, &temp_path);
if (err == MZ_OK)
temp_created = 1;
}
} else {
err = mz_zip_writer_open_file(writer, path, options->disk_size, append);
}
if (err != MZ_OK)
printf("Error %" PRId32 " opening archive for writing\n", err);
}
if (err == MZ_OK) {
if (prepare_paths) {
err = mz_zip_writer_add_prepared_paths(writer);
if (err != MZ_OK)
printf("Error %" PRId32 " adding prepared paths to archive\n", err);
} else {
err = minizip_process_paths(writer, options, arg_count, args, 0);
}
}
err_close = mz_zip_writer_close(writer);
if (err_close != MZ_OK) {
printf("Error %" PRId32 " closing archive for writing %s\n", err_close, path);
if (err == MZ_OK)
err = err_close;
}
mz_zip_writer_delete(&writer);
if (use_temp_file && temp_path) {
if (err == MZ_OK) {
err = mz_os_replace_resolved(temp_path, replace_path);
if (err == MZ_INTERNAL_ERROR)
preserve_temp = 1;
if (err != MZ_OK)
printf("Error replacing archive with temp %s\n", temp_path);
}
if (err != MZ_OK && !preserve_temp && temp_created && mz_os_file_exists(temp_path) == MZ_OK)
mz_os_unlink(temp_path);
}
free(replace_path);
free(temp_path);
return err;
}
/***************************************************************************/
int32_t minizip_extract_entry_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path) {
minizip_opt *options = (minizip_opt *)userdata;
MZ_UNUSED(handle);
MZ_UNUSED(file_info);
free(options->current_path);
options->current_path = NULL;
if (options->verbose)
options->current_path = (char *)strdup(path);
/* Print the current entry extracting */
printf("Extracting %s\n", path);
return MZ_OK;
}
int32_t minizip_extract_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position) {
minizip_opt *options = (minizip_opt *)userdata;
double progress = 0;
uint8_t raw = 0;
mz_zip_reader_get_raw(handle, &raw);
if (raw && file_info->compressed_size > 0)
progress = ((double)position / file_info->compressed_size) * 100;
else if (!raw && file_info->uncompressed_size > 0)
progress = ((double)position / file_info->uncompressed_size) * 100;
/* Print the progress of the current extraction */
if (options->verbose) {
printf("%s - %" PRId64 " / %" PRId64 " (%.02f%%)\n",
options->current_path ? options->current_path : file_info->filename, position,
file_info->uncompressed_size, progress);
}
return MZ_OK;
}
int32_t minizip_extract_overwrite_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path) {
minizip_opt *options = (minizip_opt *)userdata;
MZ_UNUSED(handle);
MZ_UNUSED(file_info);
/* Verify if we want to overwrite current entry on disk */
if (!options->overwrite) {
char rep = 0;
do {
char answer[128];
printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ", path);
if (scanf("%1s", answer) != 1)
exit(EXIT_FAILURE);
rep = answer[0];
if ((rep >= 'a') && (rep <= 'z'))
rep -= 0x20;
} while ((rep != 'Y') && (rep != 'N') && (rep != 'A'));
if (rep == 'N')
return MZ_EXIST_ERROR;
if (rep == 'A')
options->overwrite = 1;
}
return MZ_OK;
}
int32_t minizip_extract(const char *path, const char *pattern, const char *destination, const char *password,
minizip_opt *options) {
void *reader = NULL;
int32_t err = MZ_OK;
int32_t err_close = MZ_OK;
printf("Archive %s\n", path);
/* Create zip reader */
reader = mz_zip_reader_create();
if (!reader)
return MZ_MEM_ERROR;
mz_zip_reader_set_pattern(reader, pattern, 1);
mz_zip_reader_set_password(reader, password);
mz_zip_reader_set_encoding(reader, options->encoding);
mz_zip_reader_set_entry_cb(reader, options, minizip_extract_entry_cb);
mz_zip_reader_set_progress_cb(reader, options, minizip_extract_progress_cb);
mz_zip_reader_set_overwrite_cb(reader, options, minizip_extract_overwrite_cb);
err = mz_zip_reader_open_file(reader, path);
if (err != MZ_OK) {
printf("Error %" PRId32 " opening archive %s\n", err, path);
} else {
/* Save all entries in archive to destination directory */
err = mz_zip_reader_save_all(reader, destination);
if (err == MZ_END_OF_LIST) {
if (pattern) {
printf("Files matching %s not found in archive\n", pattern);
} else {
printf("No files in archive\n");
err = MZ_OK;
}
} else if (err != MZ_OK) {
printf("Error %" PRId32 " saving entries to disk %s\n", err, path);
}
}
err_close = mz_zip_reader_close(reader);
if (err_close != MZ_OK) {
printf("Error %" PRId32 " closing archive for reading\n", err_close);
err = err_close;
}
mz_zip_reader_delete(&reader);
free(options->current_path);
options->current_path = NULL;
return err;
}
/***************************************************************************/
int32_t minizip_erase(const char *src_path, const char *target_path, int32_t arg_count, const char **args) {
mz_zip_file *file_info = NULL;
const char *filename_in_zip = NULL;
const char *target_path_ptr = target_path;
void *reader = NULL;
void *writer = NULL;
int32_t skip = 0;
int32_t err = MZ_OK;
int32_t i = 0;
uint8_t zip_cd = 0;
char bak_path[256];
char tmp_path[256];
if (!target_path) {
/* Construct temporary zip name with random suffix */
if (mz_os_get_temp_path(tmp_path, sizeof(tmp_path), "mz_") != MZ_OK)
return MZ_INTERNAL_ERROR;
#ifndef _WIN32
/* POSIX rename(2) returns EXDEV across mounts; fall back to a same-dir tmp when
TMPDIR is on a different filesystem. MoveFileExW absorbs cross-volume natively
on Win32, so the check is skipped there. */
{
char tmp_dir[256];
strncpy(tmp_dir, tmp_path, sizeof(tmp_dir) - 1);
tmp_dir[sizeof(tmp_dir) - 1] = 0;
mz_path_remove_filename(tmp_dir);
if (mz_os_path_same_fs(tmp_dir, src_path) != MZ_OK) {
int32_t result = snprintf(tmp_path, sizeof(tmp_path), "%s.mz_tmp.%llu", src_path,
(unsigned long long)mz_os_ms_time());
if (result < 0 || result >= (int32_t)sizeof(tmp_path))
return MZ_BUF_ERROR;
if (mz_os_file_exists(tmp_path) == MZ_OK)
return MZ_EXIST_ERROR;
}
}
#endif
target_path_ptr = tmp_path;
}
reader = mz_zip_reader_create();
if (!reader)
return MZ_MEM_ERROR;
writer = mz_zip_writer_create();
if (!writer) {
mz_zip_reader_delete(&reader);
return MZ_MEM_ERROR;
}
/* Open original archive we want to erase an entry in */
err = mz_zip_reader_open_file(reader, src_path);
if (err != MZ_OK) {
printf("Error %" PRId32 " opening archive for reading %s\n", err, src_path);
mz_zip_reader_delete(&reader);
mz_zip_writer_delete(&writer);
return err;
}