-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.c
2814 lines (2453 loc) · 103 KB
/
process.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:https://ftp.info-zip.org/pub/infozip/license.html
*/
/*---------------------------------------------------------------------------
process.c
This file contains the top-level routines for processing multiple zipfiles.
Contains: process_zipfiles()
free_G_buffers()
do_seekable()
file_size()
rec_find()
find_ecrec64()
find_ecrec()
process_zip_cmmnt()
process_cdir_file_hdr()
get_cdir_ent()
process_local_file_hdr()
getZip64Data()
ef_scan_for_izux()
getRISCOSexfield()
---------------------------------------------------------------------------*/
#define UNZIP_INTERNAL
#include "unzip.h"
#ifdef WINDLL
# ifdef POCKET_UNZIP
# include "wince/intrface.h"
# else
# include "windll/windll.h"
# endif
#endif
#if defined(DYNALLOC_CRCTAB) || defined(UNICODE_SUPPORT)
# include "crc32.h"
#endif
static int do_seekable (Uz_Globs *pG, int lastchance);
#ifdef DO_SAFECHECK_2GB
static zoff_t file_size (FILE *file);
#endif /* DO_SAFECHECK_2GB */
static int rec_find (Uz_Globs *pG, zoff_t, char *, int);
static int find_ecrec64 (Uz_Globs *pG, zoff_t searchlen);
static int find_ecrec (Uz_Globs *pG, zoff_t searchlen);
static int process_zip_cmmnt (Uz_Globs *pG);
static int get_cdir_ent (Uz_Globs *pG);
#ifdef IZ_HAVE_UXUIDGID
static int read_ux3_value OF((const uch *dbuf, unsigned uidgid_sz,
ulg *p_uidgid));
#endif /* IZ_HAVE_UXUIDGID */
static const char CannotAllocateBuffers[] =
"error: cannot allocate unzip buffers\n";
/* process_zipfiles() strings */
# if (defined(IZ_CHECK_TZ) && defined(USE_EF_UT_TIME))
static const char WarnInvalidTZ[] =
"Warning: TZ environment variable not found, cannot use UTC times!!\n";
# endif
# if !(defined(UNIX) || defined(AMIGA))
static const char CannotFindWildcardMatch[] =
"%s: cannot find any matches for wildcard specification \"%s\".\n";
# endif /* !(UNIX || AMIGA) */
static const char FilesProcessOK[] =
"%d archive%s successfully processed.\n";
static const char ArchiveWarning[] =
"%d archive%s had warnings but no fatal errors.\n";
static const char ArchiveFatalError[] =
"%d archive%s had fatal errors.\n";
static const char FileHadNoZipfileDir[] =
"%d file%s had no zipfile directory.\n";
static const char ZipfileWasDir[] = "1 \"zipfile\" was a directory.\n";
static const char ManyZipfilesWereDir[] =
"%d \"zipfiles\" were directories.\n";
static const char NoZipfileFound[] = "No zipfiles found.\n";
/* do_seekable() strings */
# ifdef UNIX
static const char CannotFindZipfileDirMsg[] =
"%s: cannot find zipfile directory in one of %s or\n\
%s%s.zip, and cannot find %s, period.\n";
static const char CannotFindEitherZipfile[] =
"%s: cannot find or open %s, %s.zip or %s.\n";
# else /* !UNIX */
static const char CannotFindZipfileDirMsg[] =
"%s: cannot find zipfile directory in %s,\n\
%sand cannot find %s, period.\n";
static const char CannotFindEitherZipfile[] =
"%s: cannot find either %s or %s.\n";
# endif /* ?UNIX */
extern const char Zipnfo[]; /* in unzip.c */
#ifndef WINDLL
static const char Unzip[] = "unzip";
#else
static const char Unzip[] = "UnZip DLL";
#endif
#ifdef DO_SAFECHECK_2GB
static const char ZipfileTooBig[] =
"Trying to read large file (> 2 GiB) without large file support\n";
#endif /* DO_SAFECHECK_2GB */
static const char MaybeExe[] =
"note: %s may be a plain executable, not an archive\n";
static const char CentDirNotInZipMsg[] = "\n\
[%s]:\n\
Zipfile is disk %lu of a multi-disk archive, and this is not the disk on\n\
which the central zipfile directory begins (disk %lu).\n";
static const char EndCentDirBogus[] =
"\nwarning [%s]: end-of-central-directory record claims this\n\
is disk %lu but that the central directory starts on disk %lu; this is a\n\
contradiction. Attempting to process anyway.\n";
# ifdef NO_MULTIPART
static const char NoMultiDiskArcSupport[] =
"\nerror [%s]: zipfile is part of multi-disk archive\n\
(sorry, not yet supported).\n";
static const char MaybePakBug[] = "warning [%s]:\
zipfile claims to be 2nd disk of a 2-part archive;\n\
attempting to process anyway. If no further errors occur, this archive\n\
was probably created by PAK v2.51 or earlier. This bug was reported to\n\
NoGate in March 1991 and was supposed to have been fixed by mid-1991; as\n\
of mid-1992 it still hadn't been. (If further errors do occur, archive\n\
was probably created by PKZIP 2.04c or later; UnZip does not yet support\n\
multi-part archives.)\n";
# else
static const char MaybePakBug[] = "warning [%s]:\
zipfile claims to be last disk of a multi-part archive;\n\
attempting to process anyway, assuming all parts have been concatenated\n\
together in order. Expect \"errors\" and warnings...true multi-part support\
\n doesn't exist yet (coming soon).\n";
# endif
static const char ExtraBytesAtStart[] =
"warning [%s]: %s extra byte%s at beginning or within zipfile\n\
(attempting to process anyway)\n";
#if ((!defined(WINDLL)) || !defined(NO_ZIPINFO))
static const char LogInitline[] = "Archive: %s\n";
#endif
static const char MissingBytes[] =
"error [%s]: missing %s bytes in zipfile\n\
(attempting to process anyway)\n";
static const char NullCentDirOffset[] =
"error [%s]: NULL central directory offset\n\
(attempting to process anyway)\n";
static const char ZipfileEmpty[] = "warning [%s]: zipfile is empty\n";
static const char CentDirStartNotFound[] =
"error [%s]: start of central directory not found;\n\
zipfile corrupt.\n%s";
static const char Cent64EndSigSearchErr[] =
"fatal error: read failure while seeking for End-of-centdir-64 signature.\n\
This zipfile is corrupt.\n";
static const char Cent64EndSigSearchOff[] =
"error: End-of-centdir-64 signature not where expected (prepended bytes?)\n\
(attempting to process anyway)\n";
static const char CentDirTooLong[] =
"error [%s]: reported length of central directory is\n\
%s bytes too long (Atari STZip zipfile? J.H.Holm ZIPSPLIT 1.1\n\
zipfile?). Compensating...\n";
static const char CentDirEndSigNotFound[] = "\
End-of-central-directory signature not found. Either this file is not\n\
a zipfile, or it constitutes one disk of a multi-part archive. In the\n\
latter case the central directory and zipfile comment will be found on\n\
the last disk(s) of this archive.\n";
#ifdef TIMESTAMP
static const char ZipTimeStampFailed[] =
"warning: cannot set time for %s\n";
static const char ZipTimeStampSuccess[] =
"Updated time stamp for %s.\n";
#endif
static const char ZipfileCommTrunc1[] =
"\ncaution: zipfile comment truncated\n";
#ifndef NO_ZIPINFO
static const char NoZipfileComment[] =
"There is no zipfile comment.\n";
static const char ZipfileCommentDesc[] =
"The zipfile comment is %u bytes long and contains the following text:\n";
static const char ZipfileCommBegin[] =
"======================== zipfile comment begins\
==========================\n";
static const char ZipfileCommEnd[] =
"========================= zipfile comment ends\
===========================\n";
static const char ZipfileCommTrunc2[] =
"\n The zipfile comment is truncated.\n";
#endif /* !NO_ZIPINFO */
#ifdef UNICODE_SUPPORT
static const char UnicodeVersionError[] =
"\nwarning: Unicode Path version > 1\n";
static const char UnicodeMismatchError[] =
"\nwarning: Unicode Path checksum invalid\n";
#endif
/*******************************/
/* Function process_zipfiles() */
/*******************************/
int
process_zipfiles ( /* return PK-type error code */
Uz_Globs *pG
)
{
char *lastzipfn = (char *)NULL;
int NumWinFiles, NumLoseFiles, NumWarnFiles;
int NumMissDirs, NumMissFiles;
int error=0, error_in_archive=0;
/*---------------------------------------------------------------------------
Start by allocating buffers and (re)constructing the various PK signature
strings.
---------------------------------------------------------------------------*/
(*(Uz_Globs *)pG).inbuf = (uch *)malloc(INBUFSIZ + 4); /* 4 extra for hold[] (below) */
(*(Uz_Globs *)pG).outbuf = (uch *)malloc(OUTBUFSIZ + 1); /* 1 extra for string term. */
if (((*(Uz_Globs *)pG).inbuf == (uch *)NULL) || ((*(Uz_Globs *)pG).outbuf == (uch *)NULL)) {
Info(slide, 0x401, ((char *)slide,
LoadFarString(CannotAllocateBuffers)));
return(PK_MEM);
}
(*(Uz_Globs *)pG).hold = (*(Uz_Globs *)pG).inbuf + INBUFSIZ; /* to check for boundary-spanning sigs */
#if 0 /* CRC_32_TAB has been NULLified by CONSTRUCTGLOBALS !!!! */
/* allocate the CRC table later when we know we can read zipfile data */
CRC_32_TAB = NULL;
#endif /* 0 */
/* finish up initialization of magic signature strings */
local_hdr_sig[0] /* = extd_local_sig[0] */ = /* ASCII 'P', */
central_hdr_sig[0] = end_central_sig[0] = /* not EBCDIC */
end_centloc64_sig[0] = end_central64_sig[0] = 0x50;
local_hdr_sig[1] /* = extd_local_sig[1] */ = /* ASCII 'K', */
central_hdr_sig[1] = end_central_sig[1] = /* not EBCDIC */
end_centloc64_sig[1] = end_central64_sig[1] = 0x4B;
/*---------------------------------------------------------------------------
Make sure timezone info is set correctly; localtime() returns GMT on some
OSes (e.g., Solaris 2.x) if this isn't done first. The ifdefs around
tzset() were initially copied from dos_to_unix_time() in fileio.c. They
may still be too strict; any listed OS that supplies tzset(), regardless
of whether the function does anything, should be removed from the ifdefs.
---------------------------------------------------------------------------*/
#if (defined(WIN32) && defined(USE_EF_UT_TIME))
/* For the Win32 environment, we may have to "prepare" the environment
prior to the tzset() call, to work around tzset() implementation bugs.
*/
iz_w32_prepareTZenv();
#endif
#if (defined(IZ_CHECK_TZ) && defined(USE_EF_UT_TIME))
# ifndef VALID_TIMEZONE
# define VALID_TIMEZONE(tmp) \
(((tmp = getenv("TZ")) != NULL) && (*tmp != '\0'))
# endif
{
char *p;
(*(Uz_Globs *)pG).tz_is_valid = VALID_TIMEZONE(p);
# ifndef SFX
if (!(*(Uz_Globs *)pG).tz_is_valid) {
Info(slide, 0x401, ((char *)slide, LoadFarString(WarnInvalidTZ)));
error_in_archive = error = PK_WARN;
}
# endif /* !SFX */
}
#endif /* IZ_CHECK_TZ && USE_EF_UT_TIME */
tzset();
/* Initialize UnZip's built-in pseudo hard-coded "ISO <--> OEM" translation,
depending on the detected codepage setup. */
#ifdef NEED_ISO_OEM_INIT
prepare_ISO_OEM_translat(pG);
#endif
/*---------------------------------------------------------------------------
Initialize the internal flag holding the mode of processing "overwrite
existing file" cases. We do not use the calling interface flags directly
because the overwrite mode may be changed by user interaction while
processing archive files. Such a change should not affect the option
settings as passed through the DLL calling interface.
In case of conflicting options, the 'safer' flag uO.overwrite_none takes
precedence.
---------------------------------------------------------------------------*/
(*(Uz_Globs *)pG).overwrite_mode = (uO.overwrite_none ? OVERWRT_NEVER :
(uO.overwrite_all ? OVERWRT_ALWAYS : OVERWRT_QUERY));
/*---------------------------------------------------------------------------
Match (possible) wildcard zipfile specification with existing files and
attempt to process each. If no hits, try again after appending ".zip"
suffix. If still no luck, give up.
---------------------------------------------------------------------------*/
NumWinFiles = NumLoseFiles = NumWarnFiles = 0;
NumMissDirs = NumMissFiles = 0;
while (((*(Uz_Globs *)pG).zipfn = do_wild(pG, (*(Uz_Globs *)pG).wildzipfn)) != (char *)NULL) {
Trace((stderr, "do_wild( %s ) returns %s\n", (*(Uz_Globs *)pG).wildzipfn, (*(Uz_Globs *)pG).zipfn));
lastzipfn = (*(Uz_Globs *)pG).zipfn;
/* print a blank line between the output of different zipfiles */
if (!uO.qflag && error != PK_NOZIP && error != IZ_DIR
#ifdef TIMESTAMP
&& (!uO.T_flag || uO.zipinfo_mode)
#endif
&& (NumWinFiles+NumLoseFiles+NumWarnFiles+NumMissFiles) > 0)
(*(*(Uz_Globs *)pG).message)((void *)&(*(Uz_Globs *)pG), (uch *)"\n", 1L, 0);
if ((error = do_seekable(pG, 0)) == PK_WARN)
++NumWarnFiles;
else if (error == IZ_DIR)
++NumMissDirs;
else if (error == PK_NOZIP)
++NumMissFiles;
else if (error != PK_OK)
++NumLoseFiles;
else
++NumWinFiles;
Trace((stderr, "do_seekable(0) returns %d\n", error));
if (error != IZ_DIR && error > error_in_archive)
error_in_archive = error;
#ifdef WINDLL
if (error == IZ_CTRLC) {
free_G_buffers(pG);
return error;
}
#endif
} /* end while-loop (wildcard zipfiles) */
if ((NumWinFiles + NumWarnFiles + NumLoseFiles) == 0 &&
(NumMissDirs + NumMissFiles) == 1 && lastzipfn != (char *)NULL)
{
#if (!defined(UNIX) && !defined(AMIGA)) /* filenames with wildcard characters */
if (iswild((*(Uz_Globs *)pG).wildzipfn)) {
if (iswild(lastzipfn)) {
NumMissDirs = NumMissFiles = 0;
error_in_archive = PK_COOL;
if (uO.qflag < 3)
Info(slide, 0x401, ((char *)slide,
LoadFarString(CannotFindWildcardMatch),
LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
(*(Uz_Globs *)pG).wildzipfn));
}
} else
#endif
{
#ifndef VMS
/* 2004-11-24 SMS.
* VMS has already tried a default file type of ".zip" in
* do_wild(), so adding ZSUFX here only causes confusion by
* corrupting some valid (though nonexistent) file names.
* Complaining below about "fred;4.zip" is unlikely to be
* helpful to the victim.
*/
/* 2005-08-14 Chr. Spieler
* Although we already "know" the failure result, we call
* do_seekable() again with the same zipfile name (and the
* lastchance flag set), just to trigger the error report...
*/
#if defined(UNIX) || defined(QDOS)
char *p =
#endif
strcpy(lastzipfn + strlen(lastzipfn), ZSUFX);
#endif /* !VMS */
(*(Uz_Globs *)pG).zipfn = lastzipfn;
NumMissDirs = NumMissFiles = 0;
error_in_archive = PK_COOL;
#if defined(UNIX)
/* only Unix has case-sensitive filesystems */
/* Well FlexOS (sometimes) also has them, but support is per media */
/* and a pig to code for, so treat as case insensitive for now */
/* we do this under QDOS to check for .zip as well as _zip */
if ((error = do_seekable(pG, 0)) == PK_NOZIP || error == IZ_DIR) {
if (error == IZ_DIR)
++NumMissDirs;
strcpy(p, ALT_ZSUFX);
error = do_seekable(pG, 1);
}
#else
error = do_seekable(pG, 1);
#endif
Trace((stderr, "do_seekable(1) returns %d\n", error));
switch (error) {
case PK_WARN:
++NumWarnFiles;
break;
case IZ_DIR:
++NumMissDirs;
error = PK_NOZIP;
break;
case PK_NOZIP:
/* increment again => bug:
"1 file had no zipfile directory." */
/* ++NumMissFiles */ ;
break;
default:
if (error)
++NumLoseFiles;
else
++NumWinFiles;
break;
}
if (error > error_in_archive)
error_in_archive = error;
#ifdef WINDLL
if (error == IZ_CTRLC) {
free_G_buffers(pG);
return error;
}
#endif
}
}
/*---------------------------------------------------------------------------
Print summary of all zipfiles, assuming zipfile spec was a wildcard (no
need for a summary if just one zipfile).
---------------------------------------------------------------------------*/
if (iswild((*(Uz_Globs *)pG).wildzipfn) && uO.qflag < 3
#ifdef TIMESTAMP
&& !(uO.T_flag && !uO.zipinfo_mode && uO.qflag > 1)
#endif
)
{
if ((NumMissFiles + NumLoseFiles + NumWarnFiles > 0 || NumWinFiles != 1)
#ifdef TIMESTAMP
&& !(uO.T_flag && !uO.zipinfo_mode && uO.qflag)
#endif
&& !(uO.tflag && uO.qflag > 1))
(*(*(Uz_Globs *)pG).message)((void *)&(*(Uz_Globs *)pG), (uch *)"\n", 1L, 0x401);
if ((NumWinFiles > 1) ||
(NumWinFiles == 1 &&
NumMissDirs + NumMissFiles + NumLoseFiles + NumWarnFiles > 0))
Info(slide, 0x401, ((char *)slide, LoadFarString(FilesProcessOK),
NumWinFiles, (NumWinFiles == 1)? " was" : "s were"));
if (NumWarnFiles > 0)
Info(slide, 0x401, ((char *)slide, LoadFarString(ArchiveWarning),
NumWarnFiles, (NumWarnFiles == 1)? "" : "s"));
if (NumLoseFiles > 0)
Info(slide, 0x401, ((char *)slide, LoadFarString(ArchiveFatalError),
NumLoseFiles, (NumLoseFiles == 1)? "" : "s"));
if (NumMissFiles > 0)
Info(slide, 0x401, ((char *)slide,
LoadFarString(FileHadNoZipfileDir), NumMissFiles,
(NumMissFiles == 1)? "" : "s"));
if (NumMissDirs == 1)
Info(slide, 0x401, ((char *)slide, LoadFarString(ZipfileWasDir)));
else if (NumMissDirs > 0)
Info(slide, 0x401, ((char *)slide,
LoadFarString(ManyZipfilesWereDir), NumMissDirs));
if (NumWinFiles + NumLoseFiles + NumWarnFiles == 0)
Info(slide, 0x401, ((char *)slide, LoadFarString(NoZipfileFound)));
}
/* free allocated memory */
free_G_buffers(pG);
return error_in_archive;
} /* end function process_zipfiles() */
/*****************************/
/* Function free_G_buffers() */
/*****************************/
void
free_G_buffers ( /* releases all memory allocated in global vars */
Uz_Globs *pG
)
{
unsigned i;
#ifdef SYSTEM_SPECIFIC_DTOR
SYSTEM_SPECIFIC_DTOR(pG);
#endif
inflate_free(pG);
checkdir(pG, (char *)NULL, END);
#ifdef DYNALLOC_CRCTAB
if (CRC_32_TAB) {
free_crc_table();
CRC_32_TAB = NULL;
}
#endif
if ((*(Uz_Globs *)pG).key != (char *)NULL) {
free((*(Uz_Globs *)pG).key);
(*(Uz_Globs *)pG).key = (char *)NULL;
}
if ((*(Uz_Globs *)pG).extra_field != (uch *)NULL) {
free((*(Uz_Globs *)pG).extra_field);
(*(Uz_Globs *)pG).extra_field = (uch *)NULL;
}
#if (!defined(VMS) && !defined(SMALL_MEM))
/* VMS uses its own buffer scheme for textmode flush() */
if ((*(Uz_Globs *)pG).outbuf2) {
free((*(Uz_Globs *)pG).outbuf2); /* malloc'd ONLY if unshrink and -a */
(*(Uz_Globs *)pG).outbuf2 = (uch *)NULL;
}
#endif
if ((*(Uz_Globs *)pG).outbuf)
free((*(Uz_Globs *)pG).outbuf);
if ((*(Uz_Globs *)pG).inbuf)
free((*(Uz_Globs *)pG).inbuf);
(*(Uz_Globs *)pG).inbuf = (*(Uz_Globs *)pG).outbuf = (uch *)NULL;
#ifdef UNICODE_SUPPORT
if ((*(Uz_Globs *)pG).filename_full) {
free((*(Uz_Globs *)pG).filename_full);
(*(Uz_Globs *)pG).filename_full = (char *)NULL;
(*(Uz_Globs *)pG).fnfull_bufsize = 0;
}
#endif /* UNICODE_SUPPORT */
for (i = 0; i < DIR_BLKSIZ; i++) {
if ((*(Uz_Globs *)pG).info[i].cfilname != (char *)NULL) {
free((*(Uz_Globs *)pG).info[i].cfilname);
(*(Uz_Globs *)pG).info[i].cfilname = (char *)NULL;
}
}
#ifdef MALLOC_WORK
if ((*(Uz_Globs *)pG).area.Slide) {
free((*(Uz_Globs *)pG).area.Slide);
(*(Uz_Globs *)pG).area.Slide = (uch *)NULL;
}
#endif
} /* end function free_G_buffers() */
/**************************/
/* Function do_seekable() */
/**************************/
static int
do_seekable ( /* return PK-type error code */
Uz_Globs *pG,
int lastchance
)
{
/* static int no_ecrec = FALSE; SKM: moved to globals.h */
int maybe_exe=FALSE;
int too_weird_to_continue=FALSE;
#ifdef TIMESTAMP
time_t uxstamp;
ulg nmember = 0L;
#endif
int error=0, error_in_archive;
/*---------------------------------------------------------------------------
Open the zipfile for reading in BINARY mode to prevent CR/LF translation,
which would corrupt the bit streams.
---------------------------------------------------------------------------*/
if (SSTAT((*(Uz_Globs *)pG).zipfn, &(*(Uz_Globs *)pG).statbuf) ||
(error = S_ISDIR((*(Uz_Globs *)pG).statbuf.st_mode)) != 0)
{
if (lastchance && (uO.qflag < 3)) {
#if defined(UNIX)
if ((*(Uz_Globs *)pG).no_ecrec)
Info(slide, 1, ((char *)slide,
LoadFarString(CannotFindZipfileDirMsg),
LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
(*(Uz_Globs *)pG).wildzipfn, uO.zipinfo_mode? " " : "", (*(Uz_Globs *)pG).wildzipfn,
(*(Uz_Globs *)pG).zipfn));
else
Info(slide, 1, ((char *)slide,
LoadFarString(CannotFindEitherZipfile),
LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
(*(Uz_Globs *)pG).wildzipfn, (*(Uz_Globs *)pG).wildzipfn, (*(Uz_Globs *)pG).zipfn));
#else /* !(UNIX || QDOS) */
if ((*(Uz_Globs *)pG).no_ecrec)
Info(slide, 0x401, ((char *)slide,
LoadFarString(CannotFindZipfileDirMsg),
LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
(*(Uz_Globs *)pG).wildzipfn, uO.zipinfo_mode? " " : "", (*(Uz_Globs *)pG).zipfn));
else
Info(slide, 0x401, ((char *)slide,
LoadFarString(CannotFindEitherZipfile),
LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
(*(Uz_Globs *)pG).wildzipfn, (*(Uz_Globs *)pG).zipfn));
#endif /* ?(UNIX ) */
}
return error? IZ_DIR : PK_NOZIP;
}
(*(Uz_Globs *)pG).ziplen = (*(Uz_Globs *)pG).statbuf.st_size;
#if defined(UNIX) || defined(DOS_OS2_W32) || defined(THEOS)
if ((*(Uz_Globs *)pG).statbuf.st_mode & S_IEXEC) /* no extension on Unix exes: might */
maybe_exe = TRUE; /* find unzip, not unzip.zip; etc. */
#endif
if (open_input_file(pG)) /* this should never happen, given */
return PK_NOZIP; /* the stat() test above, but... */
#ifdef DO_SAFECHECK_2GB
/* Need more care: Do not trust the size returned by stat() but
determine it by reading beyond the end of the file. */
(*(Uz_Globs *)pG).ziplen = file_size((*(Uz_Globs *)pG).zipfd);
if ((*(Uz_Globs *)pG).ziplen == EOF) {
Info(slide, 0x401, ((char *)slide, LoadFarString(ZipfileTooBig)));
/*
printf(
" We need a better error message for: 64-bit file, 32-bit program.\n");
*/
CLOSE_INFILE();
return IZ_ERRBF;
}
#endif /* DO_SAFECHECK_2GB */
/*---------------------------------------------------------------------------
Find and process the end-of-central-directory header. UnZip need only
check last 65557 bytes of zipfile: comment may be up to 65535, end-of-
central-directory record is 18 bytes, and signature itself is 4 bytes;
add some to allow for appended garbage. Since ZipInfo is often used as
a debugging tool, search the whole zipfile if zipinfo_mode is true.
---------------------------------------------------------------------------*/
(*(Uz_Globs *)pG).cur_zipfile_bufstart = 0;
(*(Uz_Globs *)pG).inptr = (*(Uz_Globs *)pG).inbuf;
#if ((!defined(WINDLL) ) || !defined(NO_ZIPINFO))
# if (!defined(WINDLL) )
if ( (!uO.zipinfo_mode && !uO.qflag
# ifdef TIMESTAMP
&& !uO.T_flag
# endif
)
# ifndef NO_ZIPINFO
|| (uO.zipinfo_mode && uO.hflag)
# endif
)
# else /* not (!WINDLL && !SFX) ==> !NO_ZIPINFO !! */
if (uO.zipinfo_mode && uO.hflag)
# endif /* if..else..: (!WINDLL && !SFX) */
# ifdef WIN32 /* Win32 console may require codepage conversion for (*(Uz_Globs *)pG).zipfn */
Info(slide, 0, ((char *)slide, LoadFarString(LogInitline),
FnFilter1((*(Uz_Globs *)pG).zipfn)));
# else
Info(slide, 0, ((char *)slide, LoadFarString(LogInitline), (*(Uz_Globs *)pG).zipfn));
# endif
#endif /* (!WINDLL && !SFX) || !NO_ZIPINFO */
if ( (error_in_archive = find_ecrec(pG,
#ifndef NO_ZIPINFO
uO.zipinfo_mode ? (*(Uz_Globs *)pG).ziplen :
#endif
MIN((*(Uz_Globs *)pG).ziplen, 66000L)))
> PK_WARN )
{
CLOSE_INFILE();
if (maybe_exe)
Info(slide, 0x401, ((char *)slide, LoadFarString(MaybeExe),
(*(Uz_Globs *)pG).zipfn));
if (lastchance)
return error_in_archive;
else {
(*(Uz_Globs *)pG).no_ecrec = TRUE; /* assume we found wrong file: e.g., */
return PK_NOZIP; /* unzip instead of unzip.zip */
}
}
if ((uO.zflag > 0) && !uO.zipinfo_mode) { /* unzip: zflag = comment ONLY */
CLOSE_INFILE();
return error_in_archive;
}
/*---------------------------------------------------------------------------
Test the end-of-central-directory info for incompatibilities (multi-disk
archives) or inconsistencies (missing or extra bytes in zipfile).
---------------------------------------------------------------------------*/
#ifdef NO_MULTIPART
error = !uO.zipinfo_mode && ((*(Uz_Globs *)pG).ecrec.number_this_disk == 1) &&
((*(Uz_Globs *)pG).ecrec.num_disk_start_cdir == 1);
#else
error = !uO.zipinfo_mode && ((*(Uz_Globs *)pG).ecrec.number_this_disk != 0);
#endif
if (uO.zipinfo_mode &&
(*(Uz_Globs *)pG).ecrec.number_this_disk != (*(Uz_Globs *)pG).ecrec.num_disk_start_cdir)
{
if ((*(Uz_Globs *)pG).ecrec.number_this_disk > (*(Uz_Globs *)pG).ecrec.num_disk_start_cdir) {
Info(slide, 0x401, ((char *)slide,
LoadFarString(CentDirNotInZipMsg), (*(Uz_Globs *)pG).zipfn,
(ulg)(*(Uz_Globs *)pG).ecrec.number_this_disk,
(ulg)(*(Uz_Globs *)pG).ecrec.num_disk_start_cdir));
error_in_archive = PK_FIND;
too_weird_to_continue = TRUE;
} else {
Info(slide, 0x401, ((char *)slide,
LoadFarString(EndCentDirBogus), (*(Uz_Globs *)pG).zipfn,
(ulg)(*(Uz_Globs *)pG).ecrec.number_this_disk,
(ulg)(*(Uz_Globs *)pG).ecrec.num_disk_start_cdir));
error_in_archive = PK_WARN;
}
#ifdef NO_MULTIPART /* concatenation of multiple parts works in some cases */
} else if (!uO.zipinfo_mode && !error && (*(Uz_Globs *)pG).ecrec.number_this_disk != 0) {
Info(slide, 0x401, ((char *)slide, LoadFarString(NoMultiDiskArcSupport),
(*(Uz_Globs *)pG).zipfn));
error_in_archive = PK_FIND;
too_weird_to_continue = TRUE;
#endif
}
if (!too_weird_to_continue) { /* (relatively) normal zipfile: go for it */
if (error) {
Info(slide, 0x401, ((char *)slide, LoadFarString(MaybePakBug),
(*(Uz_Globs *)pG).zipfn));
error_in_archive = PK_WARN;
}
if (((*(Uz_Globs *)pG).extra_bytes = (*(Uz_Globs *)pG).real_ecrec_offset-(*(Uz_Globs *)pG).expect_ecrec_offset) <
(zoff_t)0)
{
Info(slide, 0x401, ((char *)slide, LoadFarString(MissingBytes),
(*(Uz_Globs *)pG).zipfn, FmZofft((-(*(Uz_Globs *)pG).extra_bytes), NULL, NULL)));
error_in_archive = PK_ERR;
} else if ((*(Uz_Globs *)pG).extra_bytes > 0) {
if (((*(Uz_Globs *)pG).ecrec.offset_start_central_directory == 0) &&
((*(Uz_Globs *)pG).ecrec.size_central_directory != 0)) /* zip 1.5 -go bug */
{
Info(slide, 0x401, ((char *)slide,
LoadFarString(NullCentDirOffset), (*(Uz_Globs *)pG).zipfn));
(*(Uz_Globs *)pG).ecrec.offset_start_central_directory = (*(Uz_Globs *)pG).extra_bytes;
(*(Uz_Globs *)pG).extra_bytes = 0;
error_in_archive = PK_ERR;
}
else {
Info(slide, 0x401, ((char *)slide,
LoadFarString(ExtraBytesAtStart), (*(Uz_Globs *)pG).zipfn,
FmZofft((*(Uz_Globs *)pG).extra_bytes, NULL, NULL),
((*(Uz_Globs *)pG).extra_bytes == 1)? "":"s"));
error_in_archive = PK_WARN;
}
}
/*-----------------------------------------------------------------------
Check for empty zipfile and exit now if so.
-----------------------------------------------------------------------*/
if ((*(Uz_Globs *)pG).expect_ecrec_offset==0L && (*(Uz_Globs *)pG).ecrec.size_central_directory==0) {
if (uO.zipinfo_mode)
Info(slide, 0, ((char *)slide, "%sEmpty zipfile.\n",
uO.lflag>9? "\n " : ""));
else
Info(slide, 0x401, ((char *)slide, LoadFarString(ZipfileEmpty),
(*(Uz_Globs *)pG).zipfn));
CLOSE_INFILE();
return (error_in_archive > PK_WARN)? error_in_archive : PK_WARN;
}
/*-----------------------------------------------------------------------
Compensate for missing or extra bytes, and seek to where the start
of central directory should be. If header not found, uncompensate
and try again (necessary for at least some Atari archives created
with STZip, as well as archives created by J.H. Holm's ZIPSPLIT 1.1).
-----------------------------------------------------------------------*/
error = seek_zipf(pG, (*(Uz_Globs *)pG).ecrec.offset_start_central_directory);
if (error == PK_BADERR) {
CLOSE_INFILE();
return PK_BADERR;
}
#ifdef OLD_SEEK_TEST
if (error != PK_OK || readbuf(pG, (*(Uz_Globs *)pG).sig, 4) == 0) {
CLOSE_INFILE();
return PK_ERR; /* file may be locked, or possibly disk error(?) */
}
if (memcmp((*(Uz_Globs *)pG).sig, central_hdr_sig, 4))
#else
if ((error != PK_OK) || (readbuf(pG, (*(Uz_Globs *)pG).sig, 4) == 0) ||
memcmp((*(Uz_Globs *)pG).sig, central_hdr_sig, 4))
#endif
{
zoff_t tmp = (*(Uz_Globs *)pG).extra_bytes;
(*(Uz_Globs *)pG).extra_bytes = 0;
error = seek_zipf(pG, (*(Uz_Globs *)pG).ecrec.offset_start_central_directory);
if ((error != PK_OK) || (readbuf(pG, (*(Uz_Globs *)pG).sig, 4) == 0) ||
memcmp((*(Uz_Globs *)pG).sig, central_hdr_sig, 4))
{
if (error != PK_BADERR)
Info(slide, 0x401, ((char *)slide,
LoadFarString(CentDirStartNotFound), (*(Uz_Globs *)pG).zipfn,
LoadFarStringSmall(ReportMsg)));
CLOSE_INFILE();
return (error != PK_OK ? error : PK_BADERR);
}
Info(slide, 0x401, ((char *)slide, LoadFarString(CentDirTooLong),
(*(Uz_Globs *)pG).zipfn, FmZofft((-tmp), NULL, NULL)));
error_in_archive = PK_ERR;
}
/*-----------------------------------------------------------------------
Seek to the start of the central directory one last time, since we
have just read the first entry's signature bytes; then list, extract
or test member files as instructed, and close the zipfile.
-----------------------------------------------------------------------*/
error = seek_zipf(pG, (*(Uz_Globs *)pG).ecrec.offset_start_central_directory);
if (error != PK_OK) {
CLOSE_INFILE();
return error;
}
Trace((stderr, "about to extract/list files (error = %d)\n",
error_in_archive));
#ifdef DLL
/* (*(Uz_Globs *)pG).fValidate is used only to look at an archive to see if
it appears to be a valid archive. There is no interest
in what the archive contains, nor in validating that the
entries in the archive are in good condition. This is
currently used only in the Windows DLLs for purposes of
checking archives within an archive to determine whether
or not to display the inner archives.
*/
if (!(*(Uz_Globs *)pG).fValidate)
#endif
{
#ifndef NO_ZIPINFO
if (uO.zipinfo_mode)
error = zipinfo(pG); /* ZIPINFO 'EM */
else
#endif
#ifdef TIMESTAMP
if (uO.T_flag)
error = get_time_stamp(pG, &uxstamp, &nmember);
else
#endif
if (uO.vflag && !uO.tflag && !uO.cflag)
error = list_files(pG); /* LIST 'EM */
else
error = extract_or_test_files(pG); /* EXTRACT OR TEST 'EM */
Trace((stderr, "done with extract/list files (error = %d)\n",
error));
}
if (error > error_in_archive) /* don't overwrite stronger error */
error_in_archive = error; /* with (for example) a warning */
} /* end if (!too_weird_to_continue) */
CLOSE_INFILE();
#ifdef TIMESTAMP
if (uO.T_flag && !uO.zipinfo_mode && (nmember > 0L)) {
# ifdef WIN32
if (stamp_file(pG, (*(Uz_Globs *)pG).zipfn, uxstamp)) { /* TIME-STAMP 'EM */
# else
if (stamp_file((*(Uz_Globs *)pG).zipfn, uxstamp)) { /* TIME-STAMP 'EM */
# endif
if (uO.qflag < 3)
Info(slide, 0x201, ((char *)slide,
LoadFarString(ZipTimeStampFailed), (*(Uz_Globs *)pG).zipfn));
if (error_in_archive < PK_WARN)
error_in_archive = PK_WARN;
} else {
if (!uO.qflag)
Info(slide, 0, ((char *)slide,
LoadFarString(ZipTimeStampSuccess), (*(Uz_Globs *)pG).zipfn));
}
}
#endif
return error_in_archive;
} /* end function do_seekable() */
#ifdef DO_SAFECHECK_2GB
/************************/
/* Function file_size() */
/************************/
/* File size determination which does not mislead for large files in a
small-file program. Probably should be somewhere else.
The file has to be opened previously
*/
static zoff_t file_size(file)
FILE *file;
{
int sts;
size_t siz;
zoff_t ofs;
char waste[4];
/* Seek to actual EOF. */
sts = zfseeko(file, 0, SEEK_END);
if (sts != 0) {
/* fseeko() failed. (Unlikely.) */
ofs = EOF;
} else {
/* Get apparent offset at EOF. */
ofs = zftello(file);
if (ofs < 0) {
/* Offset negative (overflow). File too big. */
ofs = EOF;
} else {
/* Seek to apparent EOF offset.
Won't be at actual EOF if offset was truncated.
*/
sts = zfseeko(file, ofs, SEEK_SET);
if (sts != 0) {
/* fseeko() failed. (Unlikely.) */
ofs = EOF;
} else {
/* Read a byte at apparent EOF. Should set EOF flag. */
siz = fread(waste, 1, 1, file);
if (feof(file) == 0) {
/* Not at EOF, but should be. File too big. */
ofs = EOF;
}
}
}
}
return ofs;
} /* end function file_size() */
#endif /* DO_SAFECHECK_2GB */
/***********************/
/* Function rec_find() */
/***********************/
static int rec_find(pG, searchlen, signature, rec_size)
/* return 0 when rec found, 1 when not found, 2 in case of read error */
Uz_Globs *pG;
zoff_t searchlen;
char* signature;
int rec_size;
{
int i, numblks, found=FALSE;
zoff_t tail_len;
/*---------------------------------------------------------------------------
Zipfile is longer than INBUFSIZ: may need to loop. Start with short
block at end of zipfile (if not TOO short).
---------------------------------------------------------------------------*/
if ((tail_len = (*(Uz_Globs *)pG).ziplen % INBUFSIZ) > rec_size) {
zfseeko((*(Uz_Globs *)pG).zipfd, (*(Uz_Globs *)pG).ziplen-tail_len, SEEK_SET);
(*(Uz_Globs *)pG).cur_zipfile_bufstart = zftello((*(Uz_Globs *)pG).zipfd);
if (((*(Uz_Globs *)pG).incnt = read((*(Uz_Globs *)pG).zipfd, (char *)(*(Uz_Globs *)pG).inbuf,
(unsigned int)tail_len)) != (int)tail_len)
return 2; /* it's expedient... */
/* 'P' must be at least (rec_size+4) bytes from end of zipfile */
for ((*(Uz_Globs *)pG).inptr = (*(Uz_Globs *)pG).inbuf+(int)tail_len-(rec_size+4);
(*(Uz_Globs *)pG).inptr >= (*(Uz_Globs *)pG).inbuf;
--(*(Uz_Globs *)pG).inptr) {
if ( (*(*(Uz_Globs *)pG).inptr == (uch)0x50) && /* ASCII 'P' */
!memcmp((char *)(*(Uz_Globs *)pG).inptr, signature, 4) ) {
(*(Uz_Globs *)pG).incnt -= (int)((*(Uz_Globs *)pG).inptr - (*(Uz_Globs *)pG).inbuf);
found = TRUE;
break;
}
}
/* sig may span block boundary: */
memcpy((char *)(*(Uz_Globs *)pG).hold, (char *)(*(Uz_Globs *)pG).inbuf, 3);
} else
(*(Uz_Globs *)pG).cur_zipfile_bufstart = (*(Uz_Globs *)pG).ziplen - tail_len;
/*-----------------------------------------------------------------------