forked from sskaje/unzip-lzfse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.c
2930 lines (2629 loc) · 105 KB
/
extract.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
/*
Copyright (c) 1990-2009 Info-ZIP. All rights reserved.
See the accompanying file LICENSE, version 2009-Jan-02 or later
(the contents of which are also included in unzip.h) for terms of use.
If, for some reason, all these files are missing, the Info-ZIP license
also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
*/
/*---------------------------------------------------------------------------
extract.c
This file contains the high-level routines ("driver routines") for extrac-
ting and testing zipfile members. It calls the low-level routines in files
explode.c, inflate.c, unreduce.c and unshrink.c.
Contains: extract_or_test_files()
store_info()
find_compr_idx()
extract_or_test_entrylist()
extract_or_test_member()
TestExtraField()
test_compr_eb()
memextract()
memflush()
extract_izvms_block() (VMS or VMS_TEXT_CONV)
set_deferred_symlink() (SYMLINKS only)
fnfilter()
dircomp() (SET_DIR_ATTRIB only)
UZbunzip2() (USE_BZIP2 only)
UZlzfse_decode() (USE_LZFSE only)
---------------------------------------------------------------------------*/
#define __EXTRACT_C /* identifies this source module */
#define UNZIP_INTERNAL
#include "unzip.h"
#ifdef WINDLL
# ifdef POCKET_UNZIP
# include "wince/intrface.h"
# else
# include "windll/windll.h"
# endif
#endif
#include "crc32.h"
#include "crypt.h"
#define GRRDUMP(buf,len) { \
int i, j; \
\
for (j = 0; j < (len)/16; ++j) { \
printf(" "); \
for (i = 0; i < 16; ++i) \
printf("%02x ", (uch)(buf)[i+(j<<4)]); \
printf("\n "); \
for (i = 0; i < 16; ++i) { \
char c = (char)(buf)[i+(j<<4)]; \
\
if (c == '\n') \
printf("\\n "); \
else if (c == '\r') \
printf("\\r "); \
else \
printf(" %c ", c); \
} \
printf("\n"); \
} \
if ((len) % 16) { \
printf(" "); \
for (i = j<<4; i < (len); ++i) \
printf("%02x ", (uch)(buf)[i]); \
printf("\n "); \
for (i = j<<4; i < (len); ++i) { \
char c = (char)(buf)[i]; \
\
if (c == '\n') \
printf("\\n "); \
else if (c == '\r') \
printf("\\r "); \
else \
printf(" %c ", c); \
} \
printf("\n"); \
} \
}
static int store_info OF((__GPRO));
#ifdef SET_DIR_ATTRIB
static int extract_or_test_entrylist OF((__GPRO__ unsigned numchunk,
ulg *pfilnum, ulg *pnum_bad_pwd, zoff_t *pold_extra_bytes,
unsigned *pnum_dirs, direntry **pdirlist,
int error_in_archive));
#else
static int extract_or_test_entrylist OF((__GPRO__ unsigned numchunk,
ulg *pfilnum, ulg *pnum_bad_pwd, zoff_t *pold_extra_bytes,
int error_in_archive));
#endif
static int extract_or_test_member OF((__GPRO));
#ifndef SFX
static int TestExtraField OF((__GPRO__ uch *ef, unsigned ef_len));
static int test_compr_eb OF((__GPRO__ uch *eb, unsigned eb_size,
unsigned compr_offset,
int (*test_uc_ebdata)(__GPRO__ uch *eb, unsigned eb_size,
uch *eb_ucptr, ulg eb_ucsize)));
#endif
#if (defined(VMS) || defined(VMS_TEXT_CONV))
static void decompress_bits OF((uch *outptr, unsigned needlen,
ZCONST uch *bitptr));
#endif
#ifdef SYMLINKS
static void set_deferred_symlink OF((__GPRO__ slinkentry *slnk_entry));
#endif
#ifdef SET_DIR_ATTRIB
static int Cdecl dircomp OF((ZCONST zvoid *a, ZCONST zvoid *b));
#endif
/*******************************/
/* Strings used in extract.c */
/*******************************/
static ZCONST char Far VersionMsg[] =
" skipping: %-22s need %s compat. v%u.%u (can do v%u.%u)\n";
static ZCONST char Far ComprMsgNum[] =
" skipping: %-22s unsupported compression method %u\n";
#ifndef SFX
static ZCONST char Far ComprMsgName[] =
" skipping: %-22s `%s' method not supported\n";
static ZCONST char Far CmprNone[] = "store";
static ZCONST char Far CmprShrink[] = "shrink";
static ZCONST char Far CmprReduce[] = "reduce";
static ZCONST char Far CmprImplode[] = "implode";
static ZCONST char Far CmprTokenize[] = "tokenize";
static ZCONST char Far CmprDeflate[] = "deflate";
static ZCONST char Far CmprDeflat64[] = "deflate64";
static ZCONST char Far CmprDCLImplode[] = "DCL implode";
static ZCONST char Far CmprBzip[] = "bzip2";
static ZCONST char Far CmprLZMA[] = "LZMA";
static ZCONST char Far CmprIBMTerse[] = "IBM/Terse";
static ZCONST char Far CmprIBMLZ77[] = "IBM LZ77";
static ZCONST char Far CmprWavPack[] = "WavPack";
static ZCONST char Far CmprPPMd[] = "PPMd";
static ZCONST char Far CmprLZFSE[] = "LZFSE";
static ZCONST char Far *ComprNames[NUM_METHODS] = {
CmprNone, CmprShrink, CmprReduce, CmprReduce, CmprReduce, CmprReduce,
CmprImplode, CmprTokenize, CmprDeflate, CmprDeflat64, CmprDCLImplode,
CmprBzip, CmprLZMA, CmprIBMTerse, CmprIBMLZ77, CmprWavPack, CmprPPMd,
CmprLZFSE
};
static ZCONST unsigned ComprIDs[NUM_METHODS] = {
STORED, SHRUNK, REDUCED1, REDUCED2, REDUCED3, REDUCED4,
IMPLODED, TOKENIZED, DEFLATED, ENHDEFLATED, DCLIMPLODED,
BZIPPED, LZMAED, IBMTERSED, IBMLZ77ED, WAVPACKED, PPMDED,
LZFSED
};
#endif /* !SFX */
static ZCONST char Far FilNamMsg[] =
"%s: bad filename length (%s)\n";
#ifndef SFX
static ZCONST char Far WarnNoMemCFName[] =
"%s: warning, no memory for comparison with local header\n";
static ZCONST char Far LvsCFNamMsg[] =
"%s: mismatching \"local\" filename (%s),\n\
continuing with \"central\" filename version\n";
#endif /* !SFX */
#if (!defined(SFX) && defined(UNICODE_SUPPORT))
static ZCONST char Far GP11FlagsDiffer[] =
"file #%lu (%s):\n\
mismatch between local and central GPF bit 11 (\"UTF-8\"),\n\
continuing with central flag (IsUTF8 = %d)\n";
#endif /* !SFX && UNICODE_SUPPORT */
static ZCONST char Far WrnStorUCSizCSizDiff[] =
"%s: ucsize %s <> csize %s for STORED entry\n\
continuing with \"compressed\" size value\n";
static ZCONST char Far ExtFieldMsg[] =
"%s: bad extra field length (%s)\n";
static ZCONST char Far OffsetMsg[] =
"file #%lu: bad zipfile offset (%s): %ld\n";
static ZCONST char Far ExtractMsg[] =
"%8sing: %-22s %s%s";
#ifndef SFX
static ZCONST char Far LengthMsg[] =
"%s %s: %s bytes required to uncompress to %s bytes;\n %s\
supposed to require %s bytes%s%s%s\n";
#endif
static ZCONST char Far BadFileCommLength[] = "%s: bad file comment length\n";
static ZCONST char Far LocalHdrSig[] = "local header sig";
static ZCONST char Far BadLocalHdr[] = "file #%lu: bad local header\n";
static ZCONST char Far AttemptRecompensate[] =
" (attempting to re-compensate)\n";
#ifndef SFX
static ZCONST char Far BackslashPathSep[] =
"warning: %s appears to use backslashes as path separators\n";
#endif
static ZCONST char Far AbsolutePathWarning[] =
"warning: stripped absolute path spec from %s\n";
static ZCONST char Far SkipVolumeLabel[] =
" skipping: %-22s %svolume label\n";
#ifdef SET_DIR_ATTRIB /* messages of code for setting directory attributes */
static ZCONST char Far DirlistEntryNoMem[] =
"warning: cannot alloc memory for dir times/permissions/UID/GID\n";
static ZCONST char Far DirlistSortNoMem[] =
"warning: cannot alloc memory to sort dir times/perms/etc.\n";
static ZCONST char Far DirlistSetAttrFailed[] =
"warning: set times/attribs failed for %s\n";
static ZCONST char Far DirlistFailAttrSum[] =
" failed setting times/attribs for %lu dir entries";
#endif
#ifdef SYMLINKS /* messages of the deferred symlinks handler */
static ZCONST char Far SymLnkWarnNoMem[] =
"warning: deferred symlink (%s) failed:\n\
out of memory\n";
static ZCONST char Far SymLnkWarnInvalid[] =
"warning: deferred symlink (%s) failed:\n\
invalid placeholder file\n";
static ZCONST char Far SymLnkDeferred[] =
"finishing deferred symbolic links:\n";
static ZCONST char Far SymLnkFinish[] =
" %-22s -> %s\n";
#endif
#ifndef WINDLL
static ZCONST char Far ReplaceQuery[] =
# ifdef VMS
"new version of %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ";
# else
"replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ";
# endif
static ZCONST char Far AssumeNone[] =
" NULL\n(EOF or read error, treating as \"[N]one\" ...)\n";
static ZCONST char Far NewNameQuery[] = "new name: ";
static ZCONST char Far InvalidResponse[] =
"error: invalid response [%s]\n";
#endif /* !WINDLL */
static ZCONST char Far ErrorInArchive[] =
"At least one %serror was detected in %s.\n";
static ZCONST char Far ZeroFilesTested[] =
"Caution: zero files tested in %s.\n";
#ifndef VMS
static ZCONST char Far VMSFormatQuery[] =
"\n%s: stored in VMS format. Extract anyway? (y/n) ";
#endif
#if CRYPT
static ZCONST char Far SkipCannotGetPasswd[] =
" skipping: %-22s unable to get password\n";
static ZCONST char Far SkipIncorrectPasswd[] =
" skipping: %-22s incorrect password\n";
static ZCONST char Far FilesSkipBadPasswd[] =
"%lu file%s skipped because of incorrect password.\n";
static ZCONST char Far MaybeBadPasswd[] =
" (may instead be incorrect password)\n";
#else
static ZCONST char Far SkipEncrypted[] =
" skipping: %-22s encrypted (not supported)\n";
#endif
static ZCONST char Far NoErrInCompData[] =
"No errors detected in compressed data of %s.\n";
static ZCONST char Far NoErrInTestedFiles[] =
"No errors detected in %s for the %lu file%s tested.\n";
static ZCONST char Far FilesSkipped[] =
"%lu file%s skipped because of unsupported compression or encoding.\n";
static ZCONST char Far ErrUnzipFile[] = " error: %s%s %s\n";
static ZCONST char Far ErrUnzipNoFile[] = "\n error: %s%s\n";
static ZCONST char Far NotEnoughMem[] = "not enough memory to ";
static ZCONST char Far InvalidComprData[] = "invalid compressed data to ";
static ZCONST char Far Inflate[] = "inflate";
#ifdef USE_BZIP2
static ZCONST char Far BUnzip[] = "bunzip";
#endif
#ifdef USE_LZFSE
static ZCONST char Far Lzfse[] = "lzfse";
#endif
#ifndef SFX
static ZCONST char Far Explode[] = "explode";
#ifndef LZW_CLEAN
static ZCONST char Far Unshrink[] = "unshrink";
#endif
#endif
#if (!defined(DELETE_IF_FULL) || !defined(HAVE_UNLINK))
static ZCONST char Far FileTruncated[] =
"warning: %s is probably truncated\n";
#endif
static ZCONST char Far FileUnknownCompMethod[] =
"%s: unknown compression method\n";
static ZCONST char Far BadCRC[] = " bad CRC %08lx (should be %08lx)\n";
/* TruncEAs[] also used in OS/2 mapname(), close_outfile() */
char ZCONST Far TruncEAs[] = " compressed EA data missing (%d bytes)%s";
char ZCONST Far TruncNTSD[] =
" compressed WinNT security data missing (%d bytes)%s";
#ifndef SFX
static ZCONST char Far InconsistEFlength[] = "bad extra-field entry:\n \
EF block length (%u bytes) exceeds remaining EF data (%u bytes)\n";
static ZCONST char Far InvalidComprDataEAs[] =
" invalid compressed data for EAs\n";
# if (defined(WIN32) && defined(NTSD_EAS))
static ZCONST char Far InvalidSecurityEAs[] =
" EAs fail security check\n";
# endif
static ZCONST char Far UnsuppNTSDVersEAs[] =
" unsupported NTSD EAs version %d\n";
static ZCONST char Far BadCRC_EAs[] = " bad CRC for extended attributes\n";
static ZCONST char Far UnknComprMethodEAs[] =
" unknown compression method for EAs (%u)\n";
static ZCONST char Far NotEnoughMemEAs[] =
" out of memory while inflating EAs\n";
static ZCONST char Far UnknErrorEAs[] =
" unknown error on extended attributes\n";
#endif /* !SFX */
static ZCONST char Far UnsupportedExtraField[] =
"\nerror: unsupported extra-field compression type (%u)--skipping\n";
static ZCONST char Far BadExtraFieldCRC[] =
"error [%s]: bad extra-field CRC %08lx (should be %08lx)\n";
/**************************************/
/* Function extract_or_test_files() */
/**************************************/
int extract_or_test_files(__G) /* return PK-type error code */
__GDEF
{
unsigned i, j;
zoff_t cd_bufstart;
uch *cd_inptr;
int cd_incnt;
ulg filnum=0L, blknum=0L;
int reached_end;
#ifndef SFX
int no_endsig_found;
#endif
int error, error_in_archive=PK_COOL;
int *fn_matched=NULL, *xn_matched=NULL;
zucn_t members_processed;
ulg num_skipped=0L, num_bad_pwd=0L;
zoff_t old_extra_bytes = 0L;
#ifdef SET_DIR_ATTRIB
unsigned num_dirs=0;
direntry *dirlist=(direntry *)NULL, **sorted_dirlist=(direntry **)NULL;
#endif
/*
* First, two general initializations are applied. These have been moved
* here from process_zipfiles() because they are only needed for accessing
* and/or extracting the data content of the zip archive.
*/
/* a) initialize the CRC table pointer (once) */
if (CRC_32_TAB == NULL) {
if ((CRC_32_TAB = get_crc_table()) == NULL) {
return PK_MEM;
}
}
#if (!defined(SFX) || defined(SFX_EXDIR))
/* b) check out if specified extraction root directory exists */
if (uO.exdir != (char *)NULL && G.extract_flag) {
G.create_dirs = !uO.fflag;
if ((error = checkdir(__G__ uO.exdir, ROOT)) > MPN_INF_SKIP) {
/* out of memory, or file in way */
return (error == MPN_NOMEM ? PK_MEM : PK_ERR);
}
}
#endif /* !SFX || SFX_EXDIR */
/*---------------------------------------------------------------------------
The basic idea of this function is as follows. Since the central di-
rectory lies at the end of the zipfile and the member files lie at the
beginning or middle or wherever, it is not very desirable to simply
read a central directory entry, jump to the member and extract it, and
then jump back to the central directory. In the case of a large zipfile
this would lead to a whole lot of disk-grinding, especially if each mem-
ber file is small. Instead, we read from the central directory the per-
tinent information for a block of files, then go extract/test the whole
block. Thus this routine contains two small(er) loops within a very
large outer loop: the first of the small ones reads a block of files
from the central directory; the second extracts or tests each file; and
the outer one loops over blocks. There's some file-pointer positioning
stuff in between, but that's about it. Btw, it's because of this jump-
ing around that we can afford to be lenient if an error occurs in one of
the member files: we should still be able to go find the other members,
since we know the offset of each from the beginning of the zipfile.
---------------------------------------------------------------------------*/
G.pInfo = G.info;
#if CRYPT
G.newzip = TRUE;
#endif
#ifndef SFX
G.reported_backslash = FALSE;
#endif
/* malloc space for check on unmatched filespecs (OK if one or both NULL) */
if (G.filespecs > 0 &&
(fn_matched=(int *)malloc(G.filespecs*sizeof(int))) != (int *)NULL)
for (i = 0; i < G.filespecs; ++i)
fn_matched[i] = FALSE;
if (G.xfilespecs > 0 &&
(xn_matched=(int *)malloc(G.xfilespecs*sizeof(int))) != (int *)NULL)
for (i = 0; i < G.xfilespecs; ++i)
xn_matched[i] = FALSE;
/*---------------------------------------------------------------------------
Begin main loop over blocks of member files. We know the entire central
directory is on this disk: we would not have any of this information un-
less the end-of-central-directory record was on this disk, and we would
not have gotten to this routine unless this is also the disk on which
the central directory starts. In practice, this had better be the ONLY
disk in the archive, but we'll add multi-disk support soon.
---------------------------------------------------------------------------*/
members_processed = 0;
#ifndef SFX
no_endsig_found = FALSE;
#endif
reached_end = FALSE;
while (!reached_end) {
j = 0;
#ifdef AMIGA
memzero(G.filenotes, DIR_BLKSIZ * sizeof(char *));
#endif
/*
* Loop through files in central directory, storing offsets, file
* attributes, case-conversion and text-conversion flags until block
* size is reached.
*/
while ((j < DIR_BLKSIZ)) {
G.pInfo = &G.info[j];
if (readbuf(__G__ G.sig, 4) == 0) {
error_in_archive = PK_EOF;
reached_end = TRUE; /* ...so no more left to do */
break;
}
if (memcmp(G.sig, central_hdr_sig, 4)) { /* is it a new entry? */
/* no new central directory entry
* -> is the number of processed entries compatible with the
* number of entries as stored in the end_central record?
*/
if ((members_processed
& (G.ecrec.have_ecr64 ? MASK_ZUCN64 : MASK_ZUCN16))
== G.ecrec.total_entries_central_dir) {
#ifndef SFX
/* yes, so look if we ARE back at the end_central record
*/
no_endsig_found =
( (memcmp(G.sig,
(G.ecrec.have_ecr64 ?
end_central64_sig : end_central_sig),
4) != 0)
&& (!G.ecrec.is_zip64_archive)
&& (memcmp(G.sig, end_central_sig, 4) != 0)
);
#endif /* !SFX */
} else {
/* no; we have found an error in the central directory
* -> report it and stop searching for more Zip entries
*/
Info(slide, 0x401, ((char *)slide,
LoadFarString(CentSigMsg), j + blknum*DIR_BLKSIZ + 1));
Info(slide, 0x401, ((char *)slide,
LoadFarString(ReportMsg)));
error_in_archive = PK_BADERR;
}
reached_end = TRUE; /* ...so no more left to do */
break;
}
/* process_cdir_file_hdr() sets pInfo->hostnum, pInfo->lcflag */
if ((error = process_cdir_file_hdr(__G)) != PK_COOL) {
error_in_archive = error; /* only PK_EOF defined */
reached_end = TRUE; /* ...so no more left to do */
break;
}
if ((error = do_string(__G__ G.crec.filename_length, DS_FN)) !=
PK_COOL)
{
if (error > error_in_archive)
error_in_archive = error;
if (error > PK_WARN) { /* fatal: no more left to do */
Info(slide, 0x401, ((char *)slide,
LoadFarString(FilNamMsg),
FnFilter1(G.filename), "central"));
reached_end = TRUE;
break;
}
}
if ((error = do_string(__G__ G.crec.extra_field_length,
EXTRA_FIELD)) != 0)
{
if (error > error_in_archive)
error_in_archive = error;
if (error > PK_WARN) { /* fatal */
Info(slide, 0x401, ((char *)slide,
LoadFarString(ExtFieldMsg),
FnFilter1(G.filename), "central"));
reached_end = TRUE;
break;
}
}
#ifdef AMIGA
G.filenote_slot = j;
if ((error = do_string(__G__ G.crec.file_comment_length,
uO.N_flag ? FILENOTE : SKIP)) != PK_COOL)
#else
if ((error = do_string(__G__ G.crec.file_comment_length, SKIP))
!= PK_COOL)
#endif
{
if (error > error_in_archive)
error_in_archive = error;
if (error > PK_WARN) { /* fatal */
Info(slide, 0x421, ((char *)slide,
LoadFarString(BadFileCommLength),
FnFilter1(G.filename)));
reached_end = TRUE;
break;
}
}
if (G.process_all_files) {
if (store_info(__G))
++j; /* file is OK; info[] stored; continue with next */
else
++num_skipped;
} else {
int do_this_file;
if (G.filespecs == 0)
do_this_file = TRUE;
else { /* check if this entry matches an `include' argument */
do_this_file = FALSE;
for (i = 0; i < G.filespecs; i++)
if (match(G.filename, G.pfnames[i], uO.C_flag WISEP)) {
do_this_file = TRUE; /* ^-- ignore case or not? */
if (fn_matched)
fn_matched[i] = TRUE;
break; /* found match, so stop looping */
}
}
if (do_this_file) { /* check if this is an excluded file */
for (i = 0; i < G.xfilespecs; i++)
if (match(G.filename, G.pxnames[i], uO.C_flag WISEP)) {
do_this_file = FALSE; /* ^-- ignore case or not? */
if (xn_matched)
xn_matched[i] = TRUE;
break;
}
}
if (do_this_file) {
if (store_info(__G))
++j; /* file is OK */
else
++num_skipped; /* unsupp. compression or encryption */
}
} /* end if (process_all_files) */
members_processed++;
} /* end while-loop (adding files to current block) */
/* save position in central directory so can come back later */
cd_bufstart = G.cur_zipfile_bufstart;
cd_inptr = G.inptr;
cd_incnt = G.incnt;
/*-----------------------------------------------------------------------
Second loop: process files in current block, extracting or testing
each one.
-----------------------------------------------------------------------*/
error = extract_or_test_entrylist(__G__ j,
&filnum, &num_bad_pwd, &old_extra_bytes,
#ifdef SET_DIR_ATTRIB
&num_dirs, &dirlist,
#endif
error_in_archive);
if (error != PK_COOL) {
if (error > error_in_archive)
error_in_archive = error;
/* ...and keep going (unless disk full or user break) */
if (G.disk_full > 1 || error_in_archive == IZ_CTRLC) {
/* clear reached_end to signal premature stop ... */
reached_end = FALSE;
/* ... and cancel scanning the central directory */
break;
}
}
/*
* Jump back to where we were in the central directory, then go and do
* the next batch of files.
*/
#ifdef USE_STRM_INPUT
zfseeko(G.zipfd, cd_bufstart, SEEK_SET);
G.cur_zipfile_bufstart = zftello(G.zipfd);
#else /* !USE_STRM_INPUT */
G.cur_zipfile_bufstart =
zlseek(G.zipfd, cd_bufstart, SEEK_SET);
#endif /* ?USE_STRM_INPUT */
read(G.zipfd, (char *)G.inbuf, INBUFSIZ); /* been here before... */
G.inptr = cd_inptr;
G.incnt = cd_incnt;
++blknum;
#ifdef TEST
printf("\ncd_bufstart = %ld (%.8lXh)\n", cd_bufstart, cd_bufstart);
printf("cur_zipfile_bufstart = %ld (%.8lXh)\n", cur_zipfile_bufstart,
cur_zipfile_bufstart);
printf("inptr-inbuf = %d\n", G.inptr-G.inbuf);
printf("incnt = %d\n\n", G.incnt);
#endif
} /* end while-loop (blocks of files in central directory) */
/*---------------------------------------------------------------------------
Process the list of deferred symlink extractions and finish up
the symbolic links.
---------------------------------------------------------------------------*/
#ifdef SYMLINKS
if (G.slink_last != NULL) {
if (QCOND2)
Info(slide, 0, ((char *)slide, LoadFarString(SymLnkDeferred)));
while (G.slink_head != NULL) {
set_deferred_symlink(__G__ G.slink_head);
/* remove the processed entry from the chain and free its memory */
G.slink_last = G.slink_head;
G.slink_head = G.slink_last->next;
free(G.slink_last);
}
G.slink_last = NULL;
}
#endif /* SYMLINKS */
/*---------------------------------------------------------------------------
Go back through saved list of directories, sort and set times/perms/UIDs
and GIDs from the deepest level on up.
---------------------------------------------------------------------------*/
#ifdef SET_DIR_ATTRIB
if (num_dirs > 0) {
sorted_dirlist = (direntry **)malloc(num_dirs*sizeof(direntry *));
if (sorted_dirlist == (direntry **)NULL) {
Info(slide, 0x401, ((char *)slide,
LoadFarString(DirlistSortNoMem)));
while (dirlist != (direntry *)NULL) {
direntry *d = dirlist;
dirlist = dirlist->next;
free(d);
}
} else {
ulg ndirs_fail = 0;
if (num_dirs == 1)
sorted_dirlist[0] = dirlist;
else {
for (i = 0; i < num_dirs; ++i) {
sorted_dirlist[i] = dirlist;
dirlist = dirlist->next;
}
qsort((char *)sorted_dirlist, num_dirs, sizeof(direntry *),
dircomp);
}
Trace((stderr, "setting directory times/perms/attributes\n"));
for (i = 0; i < num_dirs; ++i) {
direntry *d = sorted_dirlist[i];
Trace((stderr, "dir = %s\n", d->fn));
if ((error = set_direc_attribs(__G__ d)) != PK_OK) {
ndirs_fail++;
Info(slide, 0x201, ((char *)slide,
LoadFarString(DirlistSetAttrFailed), d->fn));
if (!error_in_archive)
error_in_archive = error;
}
free(d);
}
free(sorted_dirlist);
if (!uO.tflag && QCOND2) {
if (ndirs_fail > 0)
Info(slide, 0, ((char *)slide,
LoadFarString(DirlistFailAttrSum), ndirs_fail));
}
}
}
#endif /* SET_DIR_ATTRIB */
/*---------------------------------------------------------------------------
Check for unmatched filespecs on command line and print warning if any
found. Free allocated memory. (But suppress check when central dir
scan was interrupted prematurely.)
---------------------------------------------------------------------------*/
if (fn_matched) {
if (reached_end) for (i = 0; i < G.filespecs; ++i)
if (!fn_matched[i]) {
#ifdef DLL
if (!G.redirect_data && !G.redirect_text)
Info(slide, 0x401, ((char *)slide,
LoadFarString(FilenameNotMatched), G.pfnames[i]));
else
setFileNotFound(__G);
#else
Info(slide, 1, ((char *)slide,
LoadFarString(FilenameNotMatched), G.pfnames[i]));
#endif
if (error_in_archive <= PK_WARN)
error_in_archive = PK_FIND; /* some files not found */
}
free((zvoid *)fn_matched);
}
if (xn_matched) {
if (reached_end) for (i = 0; i < G.xfilespecs; ++i)
if (!xn_matched[i])
Info(slide, 0x401, ((char *)slide,
LoadFarString(ExclFilenameNotMatched), G.pxnames[i]));
free((zvoid *)xn_matched);
}
/*---------------------------------------------------------------------------
Now, all locally allocated memory has been released. When the central
directory processing has been interrupted prematurely, it is safe to
return immediately. All completeness checks and summary messages are
skipped in this case.
---------------------------------------------------------------------------*/
if (!reached_end)
return error_in_archive;
/*---------------------------------------------------------------------------
Double-check that we're back at the end-of-central-directory record, and
print quick summary of results, if we were just testing the archive. We
send the summary to stdout so that people doing the testing in the back-
ground and redirecting to a file can just do a "tail" on the output file.
---------------------------------------------------------------------------*/
#ifndef SFX
if (no_endsig_found) { /* just to make sure */
Info(slide, 0x401, ((char *)slide, LoadFarString(EndSigMsg)));
Info(slide, 0x401, ((char *)slide, LoadFarString(ReportMsg)));
if (!error_in_archive) /* don't overwrite stronger error */
error_in_archive = PK_WARN;
}
#endif /* !SFX */
if (uO.tflag) {
ulg num = filnum - num_bad_pwd;
if (uO.qflag < 2) { /* GRR 930710: was (uO.qflag == 1) */
if (error_in_archive)
Info(slide, 0, ((char *)slide, LoadFarString(ErrorInArchive),
(error_in_archive == PK_WARN)? "warning-" : "", G.zipfn));
else if (num == 0L)
Info(slide, 0, ((char *)slide, LoadFarString(ZeroFilesTested),
G.zipfn));
else if (G.process_all_files && (num_skipped+num_bad_pwd == 0L))
Info(slide, 0, ((char *)slide, LoadFarString(NoErrInCompData),
G.zipfn));
else
Info(slide, 0, ((char *)slide, LoadFarString(NoErrInTestedFiles)
, G.zipfn, num, (num==1L)? "":"s"));
if (num_skipped > 0L)
Info(slide, 0, ((char *)slide, LoadFarString(FilesSkipped),
num_skipped, (num_skipped==1L)? "":"s"));
#if CRYPT
if (num_bad_pwd > 0L)
Info(slide, 0, ((char *)slide, LoadFarString(FilesSkipBadPasswd)
, num_bad_pwd, (num_bad_pwd==1L)? "":"s"));
#endif /* CRYPT */
}
}
/* give warning if files not tested or extracted (first condition can still
* happen if zipfile is empty and no files specified on command line) */
if ((filnum == 0) && error_in_archive <= PK_WARN) {
if (num_skipped > 0L)
error_in_archive = IZ_UNSUP; /* unsupport. compression/encryption */
else
error_in_archive = PK_FIND; /* no files found at all */
}
#if CRYPT
else if ((filnum == num_bad_pwd) && error_in_archive <= PK_WARN)
error_in_archive = IZ_BADPWD; /* bad passwd => all files skipped */
#endif
else if ((num_skipped > 0L) && error_in_archive <= PK_WARN)
error_in_archive = IZ_UNSUP; /* was PK_WARN; Jean-loup complained */
#if CRYPT
else if ((num_bad_pwd > 0L) && !error_in_archive)
error_in_archive = PK_WARN;
#endif
return error_in_archive;
} /* end function extract_or_test_files() */
/***************************/
/* Function store_info() */
/***************************/
static int store_info(__G) /* return 0 if skipping, 1 if OK */
__GDEF
{
#ifdef USE_BZIP2
# define UNKN_BZ2 (G.crec.compression_method!=BZIPPED)
#else
# define UNKN_BZ2 TRUE /* bzip2 unknown */
#endif
#ifdef USE_LZMA
# define UNKN_LZMA (G.crec.compression_method!=LZMAED)
#else
# define UNKN_LZMA TRUE /* LZMA unknown */
#endif
#ifdef USE_WAVP
# define UNKN_WAVP (G.crec.compression_method!=WAVPACKED)
#else
# define UNKN_WAVP TRUE /* WavPack unknown */
#endif
#ifdef USE_PPMD
# define UNKN_PPMD (G.crec.compression_method!=PPMDED)
#else
# define UNKN_PPMD TRUE /* PPMd unknown */
#endif
#ifdef USE_LZFSE
# define UNKN_LZFSE (G.crec.compression_method!=LZFSED)
#else
# define UNKN_LZFSE TRUE /* LZFSE unknown */
#endif
#ifdef SFX
# ifdef USE_DEFLATE64
# define UNKN_COMPR \
(G.crec.compression_method!=STORED && G.crec.compression_method<DEFLATED \
&& G.crec.compression_method>ENHDEFLATED \
&& UNKN_BZ2 && UNKN_LZMA && UNKN_WAVP && UNKN_PPMD && UNKN_LZFSE)
# else
# define UNKN_COMPR \
(G.crec.compression_method!=STORED && G.crec.compression_method!=DEFLATED\
&& UNKN_BZ2 && UNKN_LZMA && UNKN_WAVP && UNKN_PPMD && UNKN_LZFSE)
# endif
#else
# ifdef COPYRIGHT_CLEAN /* no reduced files */
# define UNKN_RED (G.crec.compression_method >= REDUCED1 && \
G.crec.compression_method <= REDUCED4)
# else
# define UNKN_RED FALSE /* reducing not unknown */
# endif
# ifdef LZW_CLEAN /* no shrunk files */
# define UNKN_SHR (G.crec.compression_method == SHRUNK)
# else
# define UNKN_SHR FALSE /* unshrinking not unknown */
# endif
# ifdef USE_DEFLATE64
# define UNKN_COMPR (UNKN_RED || UNKN_SHR || \
G.crec.compression_method==TOKENIZED || \
(G.crec.compression_method>ENHDEFLATED && UNKN_BZ2 && UNKN_LZMA \
&& UNKN_WAVP && UNKN_PPMD && UNKN_LZFSE))
# else
# define UNKN_COMPR (UNKN_RED || UNKN_SHR || \
G.crec.compression_method==TOKENIZED || \
(G.crec.compression_method>DEFLATED && UNKN_BZ2 && UNKN_LZMA \
&& UNKN_WAVP && UNKN_PPMD && UNKN_LZFSE))
# endif
#endif
#if (defined(USE_BZIP2) && (UNZIP_VERSION < UNZIP_BZ2VERS))
int unzvers_support = (UNKN_BZ2 ? UNZIP_VERSION : UNZIP_BZ2VERS);
# define UNZVERS_SUPPORT unzvers_support
#else
# define UNZVERS_SUPPORT UNZIP_VERSION
#endif
/*---------------------------------------------------------------------------
Check central directory info for version/compatibility requirements.
---------------------------------------------------------------------------*/
G.pInfo->encrypted = G.crec.general_purpose_bit_flag & 1; /* bit field */
G.pInfo->ExtLocHdr = (G.crec.general_purpose_bit_flag & 8) == 8; /* bit */
G.pInfo->textfile = G.crec.internal_file_attributes & 1; /* bit field */
G.pInfo->crc = G.crec.crc32;
G.pInfo->compr_size = G.crec.csize;
G.pInfo->uncompr_size = G.crec.ucsize;
switch (uO.aflag) {
case 0:
G.pInfo->textmode = FALSE; /* bit field */
break;
case 1:
G.pInfo->textmode = G.pInfo->textfile; /* auto-convert mode */
break;
default: /* case 2: */
G.pInfo->textmode = TRUE;
break;
}
if (G.crec.version_needed_to_extract[1] == VMS_) {
if (G.crec.version_needed_to_extract[0] > VMS_UNZIP_VERSION) {
if (!((uO.tflag && uO.qflag) || (!uO.tflag && !QCOND2)))
Info(slide, 0x401, ((char *)slide, LoadFarString(VersionMsg),
FnFilter1(G.filename), "VMS",
G.crec.version_needed_to_extract[0] / 10,
G.crec.version_needed_to_extract[0] % 10,
VMS_UNZIP_VERSION / 10, VMS_UNZIP_VERSION % 10));
return 0;
}
#ifndef VMS /* won't be able to use extra field, but still have data */
else if (!uO.tflag && !IS_OVERWRT_ALL) { /* if -o, extract anyway */
Info(slide, 0x481, ((char *)slide, LoadFarString(VMSFormatQuery),
FnFilter1(G.filename)));
fgets(G.answerbuf, sizeof(G.answerbuf), stdin);
if ((*G.answerbuf != 'y') && (*G.answerbuf != 'Y'))
return 0;
}
#endif /* !VMS */
/* usual file type: don't need VMS to extract */
} else if (G.crec.version_needed_to_extract[0] > UNZVERS_SUPPORT) {
if (!((uO.tflag && uO.qflag) || (!uO.tflag && !QCOND2)))
Info(slide, 0x401, ((char *)slide, LoadFarString(VersionMsg),
FnFilter1(G.filename), "PK",
G.crec.version_needed_to_extract[0] / 10,
G.crec.version_needed_to_extract[0] % 10,
UNZVERS_SUPPORT / 10, UNZVERS_SUPPORT % 10));
return 0;
}
if (UNKN_COMPR) {
if (!((uO.tflag && uO.qflag) || (!uO.tflag && !QCOND2))) {
#ifndef SFX
unsigned cmpridx;
if ((cmpridx = find_compr_idx(G.crec.compression_method))
< NUM_METHODS)
Info(slide, 0x401, ((char *)slide, LoadFarString(ComprMsgName),
FnFilter1(G.filename),
LoadFarStringSmall(ComprNames[cmpridx])));
else
#endif
Info(slide, 0x401, ((char *)slide, LoadFarString(ComprMsgNum),
FnFilter1(G.filename),
G.crec.compression_method));
}
return 0;
}
#if (!CRYPT)
if (G.pInfo->encrypted) {
if (!((uO.tflag && uO.qflag) || (!uO.tflag && !QCOND2)))
Info(slide, 0x401, ((char *)slide, LoadFarString(SkipEncrypted),
FnFilter1(G.filename)));
return 0;
}
#endif /* !CRYPT */
#ifndef SFX
/* store a copy of the central header filename for later comparison */
if ((G.pInfo->cfilname = zfmalloc(strlen(G.filename) + 1)) == NULL) {
Info(slide, 0x401, ((char *)slide, LoadFarString(WarnNoMemCFName),
FnFilter1(G.filename)));
} else
zfstrcpy(G.pInfo->cfilname, G.filename);
#endif /* !SFX */
/* map whatever file attributes we have into the local format */
mapattr(__G); /* GRR: worry about return value later */
G.pInfo->diskstart = G.crec.disk_number_start;
G.pInfo->offset = (zoff_t)G.crec.relative_offset_local_header;
return 1;
} /* end function store_info() */