forked from sskaje/unzip-lzfse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unzip.c
2664 lines (2463 loc) · 95.1 KB
/
unzip.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
*/
/*---------------------------------------------------------------------------
unzip.c
UnZip - a zipfile extraction utility. See below for make instructions, or
read the comments in Makefile and the various Contents files for more de-
tailed explanations. To report a bug, submit a *complete* description via
//www.info-zip.org/zip-bug.html; include machine type, operating system and
version, compiler and version, and reasonably detailed error messages or
problem report. To join Info-ZIP, see the instructions in README.
UnZip 5.x is a greatly expanded and partially rewritten successor to 4.x,
which in turn was almost a complete rewrite of version 3.x. For a detailed
revision history, see UnzpHist.zip at quest.jpl.nasa.gov. For a list of
the many (near infinite) contributors, see "CONTRIBS" in the UnZip source
distribution.
UnZip 6.0 adds support for archives larger than 4 GiB using the Zip64
extensions as well as support for Unicode information embedded per the
latest zip standard additions.
---------------------------------------------------------------------------
[from original zipinfo.c]
This program reads great gobs of totally nifty information, including the
central directory stuff, from ZIP archives ("zipfiles" for short). It
started as just a testbed for fooling with zipfiles, but at this point it
is actually a useful utility. It also became the basis for the rewrite of
UnZip (3.16 -> 4.0), using the central directory for processing rather than
the individual (local) file headers.
As of ZipInfo v2.0 and UnZip v5.1, the two programs are combined into one.
If the executable is named "unzip" (or "unzip.exe", depending), it behaves
like UnZip by default; if it is named "zipinfo" or "ii", it behaves like
ZipInfo. The ZipInfo behavior may also be triggered by use of unzip's -Z
option; for example, "unzip -Z [zipinfo_options] archive.zip".
Another dandy product from your buddies at Newtware!
Author: Greg Roelofs, [email protected], http://pobox.com/~newt/
23 August 1990 -> April 1997
---------------------------------------------------------------------------
Version: unzip5??.{tar.Z | tar.gz | zip} for Unix, VMS, OS/2, MS-DOS, Amiga,
Atari, Windows 3.x/95/NT/CE, Macintosh, Human68K, Acorn RISC OS,
AtheOS, BeOS, SMS/QDOS, VM/CMS, MVS, AOS/VS, Tandem NSK, Theos
and TOPS-20.
Copyrights: see accompanying file "LICENSE" in UnZip source distribution.
(This software is free but NOT IN THE PUBLIC DOMAIN.)
---------------------------------------------------------------------------*/
#define __UNZIP_C /* identifies this source module */
#define UNZIP_INTERNAL
#include "unzip.h" /* includes, typedefs, macros, prototypes, etc. */
#include "crypt.h"
#include "unzvers.h"
#ifndef WINDLL /* The WINDLL port uses windll/windll.c instead... */
/***************************/
/* Local type declarations */
/***************************/
#if (defined(REENTRANT) && !defined(NO_EXCEPT_SIGNALS))
typedef struct _sign_info
{
struct _sign_info *previous;
void (*sighandler)(int);
int sigtype;
} savsigs_info;
#endif
/*******************/
/* Local Functions */
/*******************/
#if (defined(REENTRANT) && !defined(NO_EXCEPT_SIGNALS))
static int setsignalhandler OF((__GPRO__ savsigs_info **p_savedhandler_chain,
int signal_type, void (*newhandler)(int)));
#endif
#ifndef SFX
static void help_extended OF((__GPRO));
static void show_version_info OF((__GPRO));
#endif
/*************/
/* Constants */
/*************/
#include "consts.h" /* all constant global variables are in here */
/* (non-constant globals were moved to globals.c) */
/* constant local variables: */
#ifndef SFX
#ifndef _WIN32_WCE /* Win CE does not support environment variables */
static ZCONST char Far EnvUnZip[] = ENV_UNZIP;
static ZCONST char Far EnvUnZip2[] = ENV_UNZIP2;
static ZCONST char Far EnvZipInfo[] = ENV_ZIPINFO;
static ZCONST char Far EnvZipInfo2[] = ENV_ZIPINFO2;
#ifdef RISCOS
static ZCONST char Far EnvUnZipExts[] = ENV_UNZIPEXTS;
#endif /* RISCOS */
static ZCONST char Far NoMemEnvArguments[] =
"envargs: cannot get memory for arguments";
#endif /* !_WIN32_WCE */
static ZCONST char Far CmdLineParamTooLong[] =
"error: command line parameter #%d exceeds internal size limit\n";
#endif /* !SFX */
#if (defined(REENTRANT) && !defined(NO_EXCEPT_SIGNALS))
static ZCONST char Far CantSaveSigHandler[] =
"error: cannot save signal handler settings\n";
#endif
#if (!defined(SFX) || defined(SFX_EXDIR))
static ZCONST char Far NotExtracting[] =
"caution: not extracting; -d ignored\n";
static ZCONST char Far MustGiveExdir[] =
"error: must specify directory to which to extract with -d option\n";
static ZCONST char Far OnlyOneExdir[] =
"error: -d option used more than once (only one exdir allowed)\n";
#endif
#if (defined(UNICODE_SUPPORT) && !defined(UNICODE_WCHAR))
static ZCONST char Far UTF8EscapeUnSupp[] =
"warning: -U \"escape all non-ASCII UTF-8 chars\" is not supported\n";
#endif
#if CRYPT
static ZCONST char Far MustGivePasswd[] =
"error: must give decryption password with -P option\n";
#endif
#ifndef SFX
static ZCONST char Far Zfirst[] =
"error: -Z must be first option for ZipInfo mode (check UNZIP variable?)\n";
#endif
static ZCONST char Far InvalidOptionsMsg[] = "error:\
-fn or any combination of -c, -l, -p, -t, -u and -v options invalid\n";
static ZCONST char Far IgnoreOOptionMsg[] =
"caution: both -n and -o specified; ignoring -o\n";
/* usage() strings */
#ifndef SFX
#ifdef VMS
static ZCONST char Far Example3[] = "vms.c";
static ZCONST char Far Example2[] = " unzip \"-V\" foo \"Bar\"\
(Quote names to preserve case, unless SET PROC/PARS=EXT)\n";
#else /* !VMS */
static ZCONST char Far Example3[] = "ReadMe";
#ifdef RISCOS
static ZCONST char Far Example2[] =
" unzip foo -d RAM:$ => extract all files from foo into RAMDisc\n";
#else /* !RISCOS */
#if (defined(OS2) || (defined(DOS_FLX_OS2_W32) && defined(MORE)))
static ZCONST char Far Example2[] =
""; /* no room: too many local3[] items */
#else /* !OS2 */
#ifdef MACOS
static ZCONST char Far Example2[] = ""; /* not needed */
#else /* !MACOS */
static ZCONST char Far Example2[] = " \
unzip -p foo | more => send contents of foo.zip via pipe into program more\n";
#endif /* ?MACOS */
#endif /* ?OS2 */
#endif /* ?RISCOS */
#endif /* ?VMS */
/* local1[]: command options */
#if defined(TIMESTAMP)
static ZCONST char Far local1[] =
" -T timestamp archive to latest";
#else /* !TIMESTAMP */
static ZCONST char Far local1[] = "";
#endif /* ?TIMESTAMP */
/* local2[] and local3[]: modifier options */
#ifdef DOS_FLX_H68_OS2_W32
#ifdef FLEXOS
static ZCONST char Far local2[] = "";
#else
static ZCONST char Far local2[] =
" -$ label removables (-$$ => fixed disks)";
#endif
#ifdef OS2
#ifdef MORE
static ZCONST char Far local3[] = "\
-X restore ACLs if supported -s spaces in filenames => '_'\n\
-M pipe through \"more\" pager\n";
#else
static ZCONST char Far local3[] = " \
-X restore ACLs if supported -s spaces in filenames => '_'\n\n";
#endif /* ?MORE */
#else /* !OS2 */
#ifdef WIN32
#ifdef NTSD_EAS
#ifdef MORE
static ZCONST char Far local3[] = "\
-X restore ACLs (-XX => use privileges) -s spaces in filenames => '_'\n\
-M pipe through \"more\" pager\n";
#else
static ZCONST char Far local3[] = " \
-X restore ACLs (-XX => use privileges) -s spaces in filenames => '_'\n\n";
#endif /* ?MORE */
#else /* !NTSD_EAS */
#ifdef MORE
static ZCONST char Far local3[] = "\
-M pipe through \"more\" pager \
-s spaces in filenames => '_'\n\n";
#else
static ZCONST char Far local3[] = " \
-s spaces in filenames => '_'\n\n";
#endif /* ?MORE */
#endif /* ?NTSD_EAS */
#else /* !WIN32 */
#ifdef MORE
static ZCONST char Far local3[] = " -\
M pipe through \"more\" pager -s spaces in filenames => '_'\n\n";
#else
static ZCONST char Far local3[] = "\
-s spaces in filenames => '_'\n";
#endif
#endif /* ?WIN32 */
#endif /* ?OS2 || ?WIN32 */
#else /* !DOS_FLX_OS2_W32 */
#ifdef VMS
static ZCONST char Far local2[] = " -X restore owner/ACL protection info";
#ifdef MORE
static ZCONST char Far local3[] = "\
-Y treat \".nnn\" as \";nnn\" version -2 force ODS2 names\n\
--D restore dir (-D: no) timestamps -M pipe through \"more\" pager\n\
(Must quote upper-case options, like \"-V\", unless SET PROC/PARSE=EXTEND.)\
\n\n";
#else
static ZCONST char Far local3[] = "\n\
-Y treat \".nnn\" as \";nnn\" version -2 force ODS2 names\n\
--D restore dir (-D: no) timestamps\n\
(Must quote upper-case options, like \"-V\", unless SET PROC/PARSE=EXTEND.)\
\n\n";
#endif
#else /* !VMS */
#ifdef ATH_BEO_UNX
static ZCONST char Far local2[] = " -X restore UID/GID info";
#ifdef MORE
static ZCONST char Far local3[] = "\
-K keep setuid/setgid/tacky permissions -M pipe through \"more\" pager\n";
#else
static ZCONST char Far local3[] = "\
-K keep setuid/setgid/tacky permissions\n";
#endif
#else /* !ATH_BEO_UNX */
#ifdef TANDEM
static ZCONST char Far local2[] = "\
-X restore Tandem User ID -r remove file extensions\n\
-b create 'C' (180) text files ";
#ifdef MORE
static ZCONST char Far local3[] = " \
-M pipe through \"more\" pager\n";
#else
static ZCONST char Far local3[] = "\n";
#endif
#else /* !TANDEM */
#ifdef AMIGA
static ZCONST char Far local2[] = " -N restore comments as filenotes";
#ifdef MORE
static ZCONST char Far local3[] = " \
-M pipe through \"more\" pager\n";
#else
static ZCONST char Far local3[] = "\n";
#endif
#else /* !AMIGA */
#ifdef MACOS
static ZCONST char Far local2[] = " -E show Mac info during extraction";
static ZCONST char Far local3[] = " \
-i ignore filenames in mac extra info -J junk (ignore) Mac extra info\n\
\n";
#else /* !MACOS */
#ifdef MORE
static ZCONST char Far local2[] = " -M pipe through \"more\" pager";
static ZCONST char Far local3[] = "\n";
#else
static ZCONST char Far local2[] = ""; /* Atari, Mac, CMS/MVS etc. */
static ZCONST char Far local3[] = "";
#endif
#endif /* ?MACOS */
#endif /* ?AMIGA */
#endif /* ?TANDEM */
#endif /* ?ATH_BEO_UNX */
#endif /* ?VMS */
#endif /* ?DOS_FLX_OS2_W32 */
#endif /* !SFX */
#ifndef NO_ZIPINFO
#ifdef VMS
static ZCONST char Far ZipInfoExample[] = "* or % (e.g., \"*font-%.zip\")";
#else
static ZCONST char Far ZipInfoExample[] = "*, ?, [] (e.g., \"[a-j]*.zip\")";
#endif
static ZCONST char Far ZipInfoUsageLine1[] = "\
ZipInfo %d.%d%d%s of %s, by Greg Roelofs and the Info-ZIP group.\n\
\n\
List name, date/time, attribute, size, compression method, etc., about files\n\
in list (excluding those in xlist) contained in the specified .zip archive(s).\
\n\"file[.zip]\" may be a wildcard name containing %s.\n\n\
usage: zipinfo [-12smlvChMtTz] file[.zip] [list...] [-x xlist...]\n\
or: unzip %s-Z%s [-12smlvChMtTz] file[.zip] [list...] [-x xlist...]\n";
static ZCONST char Far ZipInfoUsageLine2[] = "\nmain\
listing-format options: -s short Unix \"ls -l\" format (def.)\n\
-1 filenames ONLY, one per line -m medium Unix \"ls -l\" format\n\
-2 just filenames but allow -h/-t/-z -l long Unix \"ls -l\" format\n\
-v verbose, multi-page format\n";
static ZCONST char Far ZipInfoUsageLine3[] = "miscellaneous options:\n\
-h print header line -t print totals for listed files or for all\n\
-z print zipfile comment -T print file times in sortable decimal format\
\n -C be case-insensitive %s\
-x exclude filenames that follow from listing\n";
#ifdef MORE
static ZCONST char Far ZipInfoUsageLine4[] =
" -M page output through built-in \"more\"\n";
#else /* !MORE */
static ZCONST char Far ZipInfoUsageLine4[] = "";
#endif /* ?MORE */
#endif /* !NO_ZIPINFO */
#ifdef BETA
# ifdef VMSCLI
/* BetaVersion[] is also used in vms/cmdline.c: do not make it static */
ZCONST char Far BetaVersion[] = "%s\
THIS IS STILL A BETA VERSION OF UNZIP%s -- DO NOT DISTRIBUTE.\n\n";
# else
static ZCONST char Far BetaVersion[] = "%s\
THIS IS STILL A BETA VERSION OF UNZIP%s -- DO NOT DISTRIBUTE.\n\n";
# endif
#endif
#ifdef SFX
# ifdef VMSCLI
/* UnzipSFXBanner[] is also used in vms/cmdline.c: do not make it static */
ZCONST char Far UnzipSFXBanner[] =
# else
static ZCONST char Far UnzipSFXBanner[] =
# endif
"UnZipSFX %d.%d%d%s of %s, by Info-ZIP (http://www.info-zip.org).\n";
# ifdef SFX_EXDIR
static ZCONST char Far UnzipSFXOpts[] =
"Valid options are -tfupcz and -d <exdir>; modifiers are -abjnoqCL%sV%s.\n";
# else
static ZCONST char Far UnzipSFXOpts[] =
"Valid options are -tfupcz; modifiers are -abjnoqCL%sV%s.\n";
# endif
#else /* !SFX */
static ZCONST char Far CompileOptions[] =
"UnZip special compilation options:\n";
static ZCONST char Far CompileOptFormat[] = " %s\n";
#ifndef _WIN32_WCE /* Win CE does not support environment variables */
static ZCONST char Far EnvOptions[] =
"\nUnZip and ZipInfo environment options:\n";
static ZCONST char Far EnvOptFormat[] = "%16s: %.1024s\n";
#endif
static ZCONST char Far None[] = "[none]";
# ifdef ACORN_FTYPE_NFS
static ZCONST char Far AcornFtypeNFS[] = "ACORN_FTYPE_NFS";
# endif
# ifdef ASM_CRC
static ZCONST char Far AsmCRC[] = "ASM_CRC";
# endif
# ifdef ASM_INFLATECODES
static ZCONST char Far AsmInflateCodes[] = "ASM_INFLATECODES";
# endif
# ifdef CHECK_VERSIONS
static ZCONST char Far Check_Versions[] = "CHECK_VERSIONS";
# endif
# ifdef COPYRIGHT_CLEAN
static ZCONST char Far Copyright_Clean[] =
"COPYRIGHT_CLEAN (PKZIP 0.9x unreducing method not supported)";
# endif
# ifdef DEBUG
static ZCONST char Far UDebug[] = "DEBUG";
# endif
# ifdef DEBUG_TIME
static ZCONST char Far DebugTime[] = "DEBUG_TIME";
# endif
# ifdef DLL
static ZCONST char Far Dll[] = "DLL";
# endif
# ifdef DOSWILD
static ZCONST char Far DosWild[] = "DOSWILD";
# endif
# ifdef LZW_CLEAN
static ZCONST char Far LZW_Clean[] =
"LZW_CLEAN (PKZIP/Zip 1.x unshrinking method not supported)";
# endif
# ifndef MORE
static ZCONST char Far No_More[] = "NO_MORE";
# endif
# ifdef NO_ZIPINFO
static ZCONST char Far No_ZipInfo[] = "NO_ZIPINFO";
# endif
# ifdef NTSD_EAS
static ZCONST char Far NTSDExtAttrib[] = "NTSD_EAS";
# endif
# if defined(WIN32) && defined(NO_W32TIMES_IZFIX)
static ZCONST char Far W32NoIZTimeFix[] = "NO_W32TIMES_IZFIX";
# endif
# ifdef OLD_THEOS_EXTRA
static ZCONST char Far OldTheosExtra[] =
"OLD_THEOS_EXTRA (handle also old Theos port extra field)";
# endif
# ifdef OS2_EAS
static ZCONST char Far OS2ExtAttrib[] = "OS2_EAS";
# endif
# ifdef QLZIP
static ZCONST char Far SMSExFldOnUnix[] = "QLZIP";
# endif
# ifdef REENTRANT
static ZCONST char Far Reentrant[] = "REENTRANT";
# endif
# ifdef REGARGS
static ZCONST char Far RegArgs[] = "REGARGS";
# endif
# ifdef RETURN_CODES
static ZCONST char Far Return_Codes[] = "RETURN_CODES";
# endif
# ifdef SET_DIR_ATTRIB
static ZCONST char Far SetDirAttrib[] = "SET_DIR_ATTRIB";
# endif
# ifdef SYMLINKS
static ZCONST char Far SymLinkSupport[] =
"SYMLINKS (symbolic links supported, if RTL and file system permit)";
# endif
# ifdef TIMESTAMP
static ZCONST char Far TimeStamp[] = "TIMESTAMP";
# endif
# ifdef UNIXBACKUP
static ZCONST char Far UnixBackup[] = "UNIXBACKUP";
# endif
# ifdef USE_EF_UT_TIME
static ZCONST char Far Use_EF_UT_time[] = "USE_EF_UT_TIME";
# endif
# ifndef LZW_CLEAN
static ZCONST char Far Use_Unshrink[] =
"USE_UNSHRINK (PKZIP/Zip 1.x unshrinking method supported)";
# endif
# ifndef COPYRIGHT_CLEAN
static ZCONST char Far Use_Smith_Code[] =
"USE_SMITH_CODE (PKZIP 0.9x unreducing method supported)";
# endif
# ifdef USE_DEFLATE64
static ZCONST char Far Use_Deflate64[] =
"USE_DEFLATE64 (PKZIP 4.x Deflate64(tm) supported)";
# endif
# ifdef UNICODE_SUPPORT
# ifdef UTF8_MAYBE_NATIVE
# ifdef UNICODE_WCHAR
/* direct native UTF-8 check AND charset transform via wchar_t */
static ZCONST char Far Use_Unicode[] =
"UNICODE_SUPPORT [wide-chars, char coding: %s] (handle UTF-8 paths)";
# else
/* direct native UTF-8 check, only */
static ZCONST char Far Use_Unicode[] =
"UNICODE_SUPPORT [char coding: %s] (handle UTF-8 paths)";
# endif
static ZCONST char Far SysChUTF8[] = "UTF-8";
static ZCONST char Far SysChOther[] = "other";
# else /* !UTF8_MAYBE_NATIVE */
/* charset transform via wchar_t, no native UTF-8 support */
static ZCONST char Far Use_Unicode[] =
"UNICODE_SUPPORT [wide-chars] (handle UTF-8 paths)";
# endif /* ?UTF8_MAYBE_NATIVE */
# endif /* UNICODE_SUPPORT */
# ifdef _MBCS
static ZCONST char Far Have_MBCS_Support[] =
"MBCS-support (multibyte character support, MB_CUR_MAX = %u)";
# endif
# ifdef MULT_VOLUME
static ZCONST char Far Use_MultiVol[] =
"MULT_VOLUME (multi-volume archives supported)";
# endif
# ifdef LARGE_FILE_SUPPORT
static ZCONST char Far Use_LFS[] =
"LARGE_FILE_SUPPORT (large files over 2 GiB supported)";
# endif
# ifdef ZIP64_SUPPORT
static ZCONST char Far Use_Zip64[] =
"ZIP64_SUPPORT (archives using Zip64 for large files supported)";
# endif
# if (defined(__DJGPP__) && (__DJGPP__ >= 2))
# ifdef USE_DJGPP_ENV
static ZCONST char Far Use_DJGPP_Env[] = "USE_DJGPP_ENV";
# endif
# ifdef USE_DJGPP_GLOB
static ZCONST char Far Use_DJGPP_Glob[] = "USE_DJGPP_GLOB";
# endif
# endif /* __DJGPP__ && (__DJGPP__ >= 2) */
# ifdef USE_VFAT
static ZCONST char Far Use_VFAT_support[] = "USE_VFAT";
# endif
# ifdef USE_ZLIB
static ZCONST char Far UseZlib[] =
"USE_ZLIB (compiled with version %s; using version %s)";
# endif
# ifdef USE_BZIP2
static ZCONST char Far UseBZip2[] =
"USE_BZIP2 (PKZIP 4.6+, using bzip2 lib version %s)";
# endif
# ifdef USE_LZFSE
static ZCONST char Far Use_Lzfse[] =
"USE_LZFSE";
# endif
# ifdef VMS_TEXT_CONV
static ZCONST char Far VmsTextConv[] = "VMS_TEXT_CONV";
# endif
# ifdef VMSCLI
static ZCONST char Far VmsCLI[] = "VMSCLI";
# endif
# ifdef VMSWILD
static ZCONST char Far VmsWild[] = "VMSWILD";
# endif
# ifdef WILD_STOP_AT_DIR
static ZCONST char Far WildStopAtDir[] = "WILD_STOP_AT_DIR";
# endif
# if CRYPT
# ifdef PASSWD_FROM_STDIN
static ZCONST char Far PasswdStdin[] = "PASSWD_FROM_STDIN";
# endif
static ZCONST char Far Decryption[] =
" [decryption, version %d.%d%s of %s]\n";
static ZCONST char Far CryptDate[] = CR_VERSION_DATE;
# endif
# ifndef __RSXNT__
# ifdef __EMX__
static ZCONST char Far EnvEMX[] = "EMX";
static ZCONST char Far EnvEMXOPT[] = "EMXOPT";
# endif
# if (defined(__GO32__) && (!defined(__DJGPP__) || (__DJGPP__ < 2)))
static ZCONST char Far EnvGO32[] = "GO32";
static ZCONST char Far EnvGO32TMP[] = "GO32TMP";
# endif
# endif /* !__RSXNT__ */
#ifdef VMS
/* UnzipUsageLine1[] is also used in vms/cmdline.c: do not make it static */
ZCONST char Far UnzipUsageLine1[] = "\
UnZip %d.%d%d%s of %s, by Info-ZIP. For more details see: unzip -v.\n\n";
# ifdef COPYRIGHT_CLEAN
static ZCONST char Far UnzipUsageLine1v[] = "\
UnZip %d.%d%d%s of %s, by Info-ZIP. Maintained by C. Spieler. Send\n\
bug reports using http://www.info-zip.org/zip-bug.html; see README for details.\
\n\n";
# else
static ZCONST char Far UnzipUsageLine1v[] = "\
UnZip %d.%d%d%s of %s, by Info-ZIP. UnReduce (c) 1989 by S. H. Smith.\n\
Send bug reports using //www.info-zip.org/zip-bug.html; see README for details.\
\n\n";
# endif /* ?COPYRIGHT_CLEAN */
#else /* !VMS */
# ifdef COPYRIGHT_CLEAN
static ZCONST char Far UnzipUsageLine1[] = "\
UnZip %d.%d%d%s of %s, by Info-ZIP. Maintained by C. Spieler. Send\n\
bug reports using http://www.info-zip.org/zip-bug.html; see README for details.\
\n\n";
# else
static ZCONST char Far UnzipUsageLine1[] = "\
UnZip %d.%d%d%s of %s, by Info-ZIP. UnReduce (c) 1989 by S. H. Smith.\n\
Send bug reports using //www.info-zip.org/zip-bug.html; see README for details.\
\n\n";
# endif /* ?COPYRIGHT_CLEAN */
# define UnzipUsageLine1v UnzipUsageLine1
#endif /* ?VMS */
static ZCONST char Far UnzipUsageLine2v[] = "\
Latest sources and executables are at ftp://ftp.info-zip.org/pub/infozip/ ;\
\nsee ftp://ftp.info-zip.org/pub/infozip/UnZip.html for other sites.\
\n\n";
#ifdef MACOS
static ZCONST char Far UnzipUsageLine2[] = "\
Usage: unzip %s[-opts[modifiers]] file[.zip] [list] [-d exdir]\n \
Default action is to extract files in list, to exdir;\n\
file[.zip] may be a wildcard. %s\n";
#else /* !MACOS */
#ifdef VM_CMS
static ZCONST char Far UnzipUsageLine2[] = "\
Usage: unzip %s[-opts[modifiers]] file[.zip] [list] [-x xlist] [-d fm]\n \
Default action is to extract files in list, except those in xlist, to disk fm;\
\n file[.zip] may be a wildcard. %s\n";
#else /* !VM_CMS */
static ZCONST char Far UnzipUsageLine2[] = "\
Usage: unzip %s[-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]\n \
Default action is to extract files in list, except those in xlist, to exdir;\n\
file[.zip] may be a wildcard. %s\n";
#endif /* ?VM_CMS */
#endif /* ?MACOS */
#ifdef NO_ZIPINFO
# define ZIPINFO_MODE_OPTION ""
static ZCONST char Far ZipInfoMode[] =
"(ZipInfo mode is disabled in this version.)";
#else
# define ZIPINFO_MODE_OPTION "[-Z] "
static ZCONST char Far ZipInfoMode[] =
"-Z => ZipInfo mode (\"unzip -Z\" for usage).";
#endif /* ?NO_ZIPINFO */
#ifdef VMS
static ZCONST char Far VMSusageLine2b[] = "\
=> define foreign command symbol in LOGIN.COM: $ unzip :== $dev:[dir]unzip.exe\
\n";
#endif
#ifdef MACOS
static ZCONST char Far UnzipUsageLine3[] = "\n\
-d extract files into exdir -l list files (short format)\n\
-f freshen existing files, create none -t test compressed archive data\n\
-u update files, create if necessary -z display archive comment only\n\
-v list verbosely/show version info %s\n";
#else /* !MACOS */
#ifdef VM_CMS
static ZCONST char Far UnzipUsageLine3[] = "\n\
-p extract files to pipe, no messages -l list files (short format)\n\
-f freshen existing files, create none -t test compressed archive data\n\
-u update files, create if necessary -z display archive comment only\n\
-v list verbosely/show version info %s\n\
-x exclude files that follow (in xlist) -d extract files onto disk fm\n";
#else /* !VM_CMS */
static ZCONST char Far UnzipUsageLine3[] = "\n\
-p extract files to pipe, no messages -l list files (short format)\n\
-f freshen existing files, create none -t test compressed archive data\n\
-u update files, create if necessary -z display archive comment only\n\
-v list verbosely/show version info %s\n\
-x exclude files that follow (in xlist) -d extract files into exdir\n";
#endif /* ?VM_CMS */
#endif /* ?MACOS */
/* There is not enough space on a standard 80x25 Windows console screen for
* the additional line advertising the UTF-8 debugging options. This may
* eventually also be the case for other ports. Probably, the -U option need
* not be shown on the introductory screen at all. [Chr. Spieler, 2008-02-09]
*
* Likely, other advanced options should be moved to an extended help page and
* the option to list that page put here. [E. Gordon, 2008-3-16]
*/
#if (defined(UNICODE_SUPPORT) && !defined(WIN32))
#ifdef VMS
static ZCONST char Far UnzipUsageLine4[] = "\
modifiers:\n\
-n never overwrite or make a new version of an existing file\n\
-o always make a new version (-oo: overwrite original) of an existing file\n\
-q quiet mode (-qq => quieter) -a auto-convert any text files\n\
-j junk paths (do not make directories) -aa treat ALL files as text\n\
-U use escapes for all non-ASCII Unicode -UU ignore any Unicode fields\n\
-C match filenames case-insensitively -L make (some) names \
lowercase\n %-42s -V retain VMS version numbers\n%s";
#else /* !VMS */
static ZCONST char Far UnzipUsageLine4[] = "\
modifiers:\n\
-n never overwrite existing files -q quiet mode (-qq => quieter)\n\
-o overwrite files WITHOUT prompting -a auto-convert any text files\n\
-j junk paths (do not make directories) -aa treat ALL files as text\n\
-U use escapes for all non-ASCII Unicode -UU ignore any Unicode fields\n\
-C match filenames case-insensitively -L make (some) names \
lowercase\n %-42s -V retain VMS version numbers\n%s";
#endif /* ?VMS */
#else /* !UNICODE_SUPPORT */
#ifdef VMS
static ZCONST char Far UnzipUsageLine4[] = "\
modifiers:\n\
-n never overwrite or make a new version of an existing file\n\
-o always make a new version (-oo: overwrite original) of an existing file\n\
-q quiet mode (-qq => quieter) -a auto-convert any text files\n\
-j junk paths (do not make directories) -aa treat ALL files as text\n\
-C match filenames case-insensitively -L make (some) names \
lowercase\n %-42s -V retain VMS version numbers\n%s";
#else /* !VMS */
static ZCONST char Far UnzipUsageLine4[] = "\
modifiers:\n\
-n never overwrite existing files -q quiet mode (-qq => quieter)\n\
-o overwrite files WITHOUT prompting -a auto-convert any text files\n\
-j junk paths (do not make directories) -aa treat ALL files as text\n\
-C match filenames case-insensitively -L make (some) names \
lowercase\n %-42s -V retain VMS version numbers\n%s";
#endif /* ?VMS */
#endif /* ?UNICODE_SUPPORT */
static ZCONST char Far UnzipUsageLine5[] = "\
See \"unzip -hh\" or unzip.txt for more help. Examples:\n\
unzip data1 -x joe => extract all files except joe from zipfile data1.zip\n\
%s\
unzip -fo foo %-6s => quietly replace existing %s if archive file newer\n";
#endif /* ?SFX */
/*****************************/
/* main() / UzpMain() stub */
/*****************************/
int MAIN(argc, argv) /* return PK-type error code (except under VMS) */
int argc;
char *argv[];
{
int r;
CONSTRUCTGLOBALS();
r = unzip(__G__ argc, argv);
DESTROYGLOBALS();
RETURN(r);
}
/*******************************/
/* Primary UnZip entry point */
/*******************************/
int unzip(__G__ argc, argv)
__GDEF
int argc;
char *argv[];
{
#ifndef NO_ZIPINFO
char *p;
#endif
#if (defined(DOS_FLX_H68_NLM_OS2_W32) || !defined(SFX))
int i;
#endif
int retcode, error=FALSE;
#ifndef NO_EXCEPT_SIGNALS
#ifdef REENTRANT
savsigs_info *oldsighandlers = NULL;
# define SET_SIGHANDLER(sigtype, newsighandler) \
if ((retcode = setsignalhandler(__G__ &oldsighandlers, (sigtype), \
(newsighandler))) > PK_WARN) \
goto cleanup_and_exit
#else
# define SET_SIGHANDLER(sigtype, newsighandler) \
signal((sigtype), (newsighandler))
#endif
#endif /* NO_EXCEPT_SIGNALS */
/* initialize international char support to the current environment */
SETLOCALE(LC_CTYPE, "");
#ifdef UNICODE_SUPPORT
/* see if can use UTF-8 Unicode locale */
# ifdef UTF8_MAYBE_NATIVE
{
char *codeset;
# if !(defined(NO_NL_LANGINFO) || defined(NO_LANGINFO_H))
/* get the codeset (character set encoding) currently used */
# include <langinfo.h>
codeset = nl_langinfo(CODESET);
# else /* NO_NL_LANGINFO || NO_LANGINFO_H */
/* query the current locale setting for character classification */
codeset = setlocale(LC_CTYPE, NULL);
if (codeset != NULL) {
/* extract the codeset portion of the locale name */
codeset = strchr(codeset, '.');
if (codeset != NULL) ++codeset;
}
# endif /* ?(NO_NL_LANGINFO || NO_LANGINFO_H) */
/* is the current codeset UTF-8 ? */
if ((codeset != NULL) && (strcmp(codeset, "UTF-8") == 0)) {
/* successfully found UTF-8 char coding */
G.native_is_utf8 = TRUE;
} else {
/* Current codeset is not UTF-8 or cannot be determined. */
G.native_is_utf8 = FALSE;
}
/* Note: At least for UnZip, trying to change the process codeset to
* UTF-8 does not work. For the example Linux setup of the
* UnZip maintainer, a successful switch to "en-US.UTF-8"
* resulted in garbage display of all non-basic ASCII characters.
*/
}
# endif /* UTF8_MAYBE_NATIVE */
/* initialize Unicode */
G.unicode_escape_all = 0;
G.unicode_mismatch = 0;
G.unipath_version = 0;
G.unipath_checksum = 0;
G.unipath_filename = NULL;
#endif /* UNICODE_SUPPORT */
#if (defined(__IBMC__) && defined(__DEBUG_ALLOC__))
extern void DebugMalloc(void);
atexit(DebugMalloc);
#endif
#ifdef MALLOC_WORK
/* The following (rather complex) expression determines the allocation
size of the decompression work area. It simulates what the
combined "union" and "struct" declaration of the "static" work
area reservation achieves automatically at compile time.
Any decent compiler should evaluate this expression completely at
compile time and provide constants to the zcalloc() call.
(For better readability, some subexpressions are encapsulated
in temporarly defined macros.)
*/
# define UZ_SLIDE_CHUNK (sizeof(shrint)+sizeof(uch)+sizeof(uch))
# define UZ_NUMOF_CHUNKS \
(unsigned)(((WSIZE+UZ_SLIDE_CHUNK-1)/UZ_SLIDE_CHUNK > HSIZE) ? \
(WSIZE+UZ_SLIDE_CHUNK-1)/UZ_SLIDE_CHUNK : HSIZE)
G.area.Slide = (uch *)zcalloc(UZ_NUMOF_CHUNKS, UZ_SLIDE_CHUNK);
# undef UZ_SLIDE_CHUNK
# undef UZ_NUMOF_CHUNKS
G.area.shrink.Parent = (shrint *)G.area.Slide;
G.area.shrink.value = G.area.Slide + (sizeof(shrint)*(HSIZE));
G.area.shrink.Stack = G.area.Slide +
(sizeof(shrint) + sizeof(uch))*(HSIZE);
#endif
/*---------------------------------------------------------------------------
Set signal handler for restoring echo, warn of zipfile corruption, etc.
---------------------------------------------------------------------------*/
#ifndef NO_EXCEPT_SIGNALS
#ifdef SIGINT
SET_SIGHANDLER(SIGINT, handler);
#endif
#ifdef SIGTERM /* some systems really have no SIGTERM */
SET_SIGHANDLER(SIGTERM, handler);
#endif
#if defined(SIGABRT) && !(defined(AMIGA) && defined(__SASC))
SET_SIGHANDLER(SIGABRT, handler);
#endif
#ifdef SIGBREAK
SET_SIGHANDLER(SIGBREAK, handler);
#endif
#ifdef SIGBUS
SET_SIGHANDLER(SIGBUS, handler);
#endif
#ifdef SIGILL
SET_SIGHANDLER(SIGILL, handler);
#endif
#ifdef SIGSEGV
SET_SIGHANDLER(SIGSEGV, handler);
#endif
#endif /* NO_EXCEPT_SIGNALS */
#if (defined(WIN32) && defined(__RSXNT__))
for (i = 0 ; i < argc; i++) {
_ISO_INTERN(argv[i]);
}
#endif
/*---------------------------------------------------------------------------
Macintosh initialization code.
---------------------------------------------------------------------------*/
#ifdef MACOS
{
int a;
for (a = 0; a < 4; ++a)
G.rghCursor[a] = GetCursor(a+128);
G.giCursor = 0;
}
#endif
/*---------------------------------------------------------------------------
NetWare initialization code.
---------------------------------------------------------------------------*/
#ifdef NLM
InitUnZipConsole();
#endif
/*---------------------------------------------------------------------------
Acorn RISC OS initialization code.
---------------------------------------------------------------------------*/
#ifdef RISCOS
set_prefix();
#endif
/*---------------------------------------------------------------------------
Theos initialization code.
---------------------------------------------------------------------------*/
#ifdef THEOS
/* The easiest way found to force creation of libraries when selected
* members are to be unzipped. Explicitly add libraries names to the
* arguments list before the first member of the library.
*/
if (! _setargv(&argc, &argv)) {
Info(slide, 0x401, ((char *)slide, "cannot process argv\n"));
retcode = PK_MEM;
goto cleanup_and_exit;
}
#endif
/*---------------------------------------------------------------------------
Sanity checks. Commentary by Otis B. Driftwood and Fiorello:
D: It's all right. That's in every contract. That's what they
call a sanity clause.
F: Ha-ha-ha-ha-ha. You can't fool me. There ain't no Sanity
Claus.
---------------------------------------------------------------------------*/
#ifdef DEBUG
# ifdef LARGE_FILE_SUPPORT
/* test if we can support large files - 10/6/04 EG */
if (sizeof(zoff_t) < 8) {
Info(slide, 0x401, ((char *)slide, "LARGE_FILE_SUPPORT set but not supported\n"));
retcode = PK_BADERR;
goto cleanup_and_exit;
}
/* test if we can show 64-bit values */
{
zoff_t z = ~(zoff_t)0; /* z should be all 1s now */
char *sz;
sz = FmZofft(z, FZOFFT_HEX_DOT_WID, "X");
if ((sz[0] != 'F') || (strlen(sz) != 16))
{
z = 0;
}
/* shift z so only MSB is set */
z <<= 63;
sz = FmZofft(z, FZOFFT_HEX_DOT_WID, "X");
if ((sz[0] != '8') || (strlen(sz) != 16))
{
Info(slide, 0x401, ((char *)slide,
"Can't show 64-bit values correctly\n"));
retcode = PK_BADERR;
goto cleanup_and_exit;
}
}
# endif /* LARGE_FILE_SUPPORT */
/* 2004-11-30 SMS.
Test the NEXTBYTE macro for proper operation.
*/
{
int test_char;
static uch test_buf[2] = { 'a', 'b' };
G.inptr = test_buf;
G.incnt = 1;
test_char = NEXTBYTE; /* Should get 'a'. */
if (test_char == 'a')
{
test_char = NEXTBYTE; /* Should get EOF, not 'b'. */
}
if (test_char != EOF)
{
Info(slide, 0x401, ((char *)slide,
"NEXTBYTE macro failed. Try compiling with ALT_NEXTBYTE defined?"));
retcode = PK_BADERR;
goto cleanup_and_exit;
}
}
#endif /* DEBUG */
/*---------------------------------------------------------------------------
First figure out if we're running in UnZip mode or ZipInfo mode, and put
the appropriate environment-variable options into the queue. Then rip
through any command-line options lurking about...
---------------------------------------------------------------------------*/
#ifdef SFX
G.argv0 = argv[0];
#if (defined(OS2) || defined(WIN32))
G.zipfn = GetLoadPath(__G);/* non-MSC NT puts path into G.filename[] */
#else
G.zipfn = G.argv0;
#endif
#ifdef VMSCLI