📄 unzip.c
字号:
/*--------------------------------------------------------------------------- unzip.c UnZip - a zipfile extraction utility. See below for make instructions, or read the comments in Makefile for a more detailed explanation. To join Info-ZIP, send a message to info-zip-request@cs.ucla.edu. UnZip 4.x is nearly a complete rewrite of version 3.x, mainly to allow access to zipfiles via the central directory (and hence to the OS bytes, so we can make intelligent decisions about what to do with the extracted files). Based on unzip.c 3.15+ and zipinfo.c 0.90. For a complete revi- sion history, see UnzpHist.zip at Info-ZIP headquarters (below). For a (partial) list of the many (near infinite) contributors, see "CONTRIBS" in the UnZip source distribution. --------------------------------------------------------------------------- To compile (partial instructions): under Unix (cc): make <system name> (type "make list" for a list of valid names, or read Makefile for details. "make unzip" works for most systems. If you have a NEW system, not covered by any of the existing targets, send FULL infor- mation--hardware, OS, versions, etc.--to zip-bugs@cs.ucla.edu) under MS-DOS (TurboC): make -fMAKEFILE.DOS for command line compiles (or use the integrated environment and the included files TCCONFIG.TC and UNZIP.PRJ. Tweak for your environment.) under MS-DOS (MSC): make MAKEFILE.DOS (or use Makefile if you have MSC 6.0: "nmake msc_dos") under OS/2 (MSC): make MAKEFILE.DOS (edit appropriately) (or use Makefile if you have MSC 6.0: "nmake msc_os2") under Atari OS: needs a little work yet... under VMS: DEFINE LNK$LIBRARY SYS$LIBRARY:VAXCRTL.OLB (see VMSNOTES) CC UNZIP,FILE_IO,MAPNAME,MATCH,...,UNSHRINK LINK UNZIP,FILE_IO,MAPNAME,MATCH,...,UNSHRINK UNZIP :== $DISKNAME:[DIRECTORY]UNZIP.EXE under Macintosh OS: Double click on unzip.make. Press <Command>-M. --------------------------------------------------------------------------- Version: unzip42.{arc | tar.Z | zip | zoo} for Unix, VMS, OS/2, MS-DOS, Mac & Amiga. Source: valeria.cs.ucla.edu (131.179.64.36) in /pub wuarchive.wustl.edu (128.252.135.4) in /mirrors/misc/unix wsmr-simtel20.army.mil (192.88.110.20) in pd1:[misc.unix] Copyrights: see accompanying file "COPYING" in UnZip source distribution. ---------------------------------------------------------------------------*/#include "unzip.h" /* includes, defines, and macros *//* #define VERSION "v4.20x BETA of 3-20-92" internal beta level */#define VERSION "v4.2 of 20 March 1992"#define PAKFIX /* temporary solution to PAK-created zipfiles *//**********************//* Global Variables *//**********************/int aflag; /* -a: do ASCII to EBCDIC translation, or CR-LF *//* int bflag; RESERVED for -b: extract as binary */int cflag; /* -c: output to stdout */int fflag; /* -f: "freshen" (extract only newer files) */int jflag; /* -j: junk pathnames */int overwrite_none; /* -n: never overwrite files (no prompting) */int overwrite_all; /* -o: OK to overwrite files without prompting */int force_flag; /* (shares -o for now): force to override errors, etc. */int quietflg; /* -q: produce a lot less output */#ifdef DOS_OS2 /* to CR or LF conversion of extracted files */ int sflag; /* -s: allow spaces (blanks) in filenames */#endif /* DOS_OS2 */int tflag; /* -t: test */int uflag; /* -u: "update" (extract only newer & brand-new files) */static int U_flag; /* -U: leave filenames in upper or mixed case */static int vflag; /* -v: view directory (only used in unzip.c) */int V_flag; /* -V: don't strip VMS version numbers */#ifdef VMS int secinf; /* -X: keep owner/protection */#endif /* VMS */int zflag; /* -z: display only the archive comment */int process_all_files;longint csize; /* used by list_files(), ReadByte(): must be signed */longint ucsize; /* used by list_files(), unReduce(), unImplode() */char *fnames[2] = {"*", NULL}; /* default filenames vector */char **fnv = fnames;char sig[5];char answerbuf[10];min_info info[DIR_BLKSIZ], *pInfo=info;#ifdef OS2 int longname; /* used only in mapname.c and file_io.c */ char longfilename[FILNAMSIZ];#endif /* OS2 */#ifdef CRYPT char *key = NULL; /* password with which to decrypt data, or NULL */#endif /* CRYPT *//*--------------------------------------------------------------------------- unShrink/unReduce/unImplode working storage: ---------------------------------------------------------------------------*//* prefix_of (for unShrink) is biggest storage area, esp. on Crays...space *//* is shared by lit_nodes (unImplode) and followers (unReduce) */short prefix_of[HSIZE + 1]; /* (8193 * sizeof(short)) */#ifdef MACOS byte *suffix_of; byte *stack;#else byte suffix_of[HSIZE + 1]; /* also s-f length_nodes (smaller) */ byte stack[HSIZE + 1]; /* also s-f distance_nodes (smaller) */#endifULONG crc32val;ULONG mask_bits[] ={0x00000000L, 0x00000001L, 0x00000003L, 0x00000007L, 0x0000000fL, 0x0000001fL, 0x0000003fL, 0x0000007fL, 0x000000ffL, 0x000001ffL, 0x000003ffL, 0x000007ffL, 0x00000fffL, 0x00001fffL, 0x00003fffL, 0x00007fffL, 0x0000ffffL, 0x0001ffffL, 0x0003ffffL, 0x0007ffffL, 0x000fffffL, 0x001fffffL, 0x003fffffL, 0x007fffffL, 0x00ffffffL, 0x01ffffffL, 0x03ffffffL, 0x07ffffffL, 0x0fffffffL, 0x1fffffffL, 0x3fffffffL, 0x7fffffffL, 0xffffffffL};/*--------------------------------------------------------------------------- Input file variables: ---------------------------------------------------------------------------*/byte *inbuf, *inptr; /* input buffer (any size is legal) and pointer */int incnt;ULONG bitbuf;int bits_left;boolean zipeof;int zipfd; /* zipfile file handle */char zipfn[FILNAMSIZ];char local_hdr_sig[5] = "\120"; /* remaining signature bytes come later */char central_hdr_sig[5] = "\120"; /* (must initialize at runtime so unzip */char end_central_sig[5] = "\120"; /* executable won't look like a zipfile) */cdir_file_hdr crec; /* used in unzip.c, extract.c, misc.c */local_file_hdr lrec; /* used in unzip.c, extract.c */ecdir_rec ecrec; /* used in unzip.c, extract.c */struct stat statbuf; /* used by main(), mapped_name(), check_for_newer() */longint extra_bytes = 0; /* used in unzip.c, misc.c */longint cur_zipfile_bufstart; /* extract_or_test_files, readbuf, ReadByte */byte *extra_field = NULL; /* currently used by VMS version only *//*--------------------------------------------------------------------------- Output stream variables: ---------------------------------------------------------------------------*/byte *outbuf; /* buffer for rle look-back */byte *outptr;byte *outout; /* scratch pad for ASCII-native trans */longint outpos; /* absolute position in outfile */int outcnt; /* current position in outbuf */int outfd;int disk_full;char filename[FILNAMSIZ];/*--------------------------------------------------------------------------- unzip.c static global variables (visible only within this file): ---------------------------------------------------------------------------*/static byte *hold;static char unkn[10];static longint ziplen;static UWORD methnum;/*--------------------------------------------------------------------------- unzip.c repeated error messages (we use all of these at least twice) ---------------------------------------------------------------------------*/char *EndSigMsg = "\nwarning:\ didn't find end-of-central-dir signature at end of central dir.\n";char *CentSigMsg = "error: expected central file header signature not found (file #%u).\n";char *SeekMsg = "error: attempt to seek before beginning of zipfile\n%s";#ifdef VMSchar *ReportMsg = "\ (please check that you have transferred or created the zipfile in the\n\ appropriate BINARY mode--this includes ftp, Kermit, AND unzip'd zipfiles)\n";#else /* !VMS */char *ReportMsg = "\ (please check that you have transferred or created the zipfile in the\n\ appropriate BINARY mode and that you have compiled unzip properly)\n";#endif /* ?VMS *//******************//* Main program *//******************/main(argc, argv) /* return PK-type error code (except under VMS) */int argc;char *argv[];{ char *s; int c, error=FALSE;/*--------------------------------------------------------------------------- Macintosh initialization code. ---------------------------------------------------------------------------*/#ifdef MACOS#ifdef THINK_C #include <console.h> static char *argstr[30], args[30*64]; Point p; SFTypeList sfT; int a; EventRecord theEvent; short eMask; SFReply fileRep;#endif /* THINK_C */ typedef struct sf_node { /* node in a true shannon-fano tree */ UWORD left; /* 0 means leaf node */ UWORD right; /* or value if leaf node */ } sf_node; extern sf_node *lit_nodes, *length_nodes, *distance_nodes;#ifdef MCH_MACINTOSH defSpin(0x20);#endif suffix_of = (byte *)calloc(HSIZE+1, sizeof(byte)); stack = (byte *)calloc(HSIZE+1, sizeof(byte)); length_nodes = (sf_node *) suffix_of; /* 2*LENVALS nodes */ distance_nodes = (sf_node *) stack; /* 2*DISTVALS nodes */#ifdef THINK_C for (a=0; a<30; a+=1) argstr[a] = &args[a*64];start: tflag=vflag=cflag=aflag=jflag=U_flag=quietflg=fflag=uflag=zflag = 0; local_hdr_sig[1]=central_hdr_sig[1]=end_central_sig[1]='\0'; argc = ccommand(&argv); SetPt(&p, 40,40); SFGetFile(p, "\pSpecify ZIP file:", 0L, -1, sfT, 0L, &fileRep); if (!fileRep.good) exit(1); macfstest(fileRep.vRefNum); SetMacVol(NULL, fileRep.vRefNum); for (a=1; a<argc; a+=1) if (argv[a][0] == '-') BlockMove(argv[a], argstr[a], (strlen(argv[a])>63) ? 64 : strlen(argv[a])+1); else break; PtoCstr((char *)fileRep.fName); strcpy(argstr[a], (char *)fileRep.fName); for (;a<argc; a+=1) BlockMove(argv[a], argstr[a+1], (strlen(argv[a])>63) ? 64 : strlen(argv[a])+1); argc+=1; argv = argstr; if (hfsflag == FALSE) /* can't support directories: junk pathnames */ jflag = 1;#endif /* THINK_C */#endif /* MACOS *//*--------------------------------------------------------------------------- Debugging info for checking on structure padding: ---------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -