📄 process.c
字号:
/*--------------------------------------------------------------------------- process.c This file contains the top-level routines for processing multiple zipfiles. Contains: process_zipfiles() free_G_buffers() do_seekable() find_ecrec() uz_end_central() process_cdir_file_hdr() get_cdir_ent() process_local_file_hdr() ef_scan_for_izux() ---------------------------------------------------------------------------*/#define UNZIP_INTERNAL#include "unzip.h"#ifdef WINDLL# ifdef POCKET_UNZIP# include "wince/intrface.h"# else# include "windll/windll.h"# endif#endifstatic int do_seekable OF((__GPRO__ int lastchance));static int find_ecrec OF((__GPRO__ long searchlen));static ZCONST char Far CannotAllocateBuffers[] = "error: cannot allocate unzip buffers\n";#ifdef SFX static ZCONST char Far CannotFindMyself[] = "unzipsfx: cannot find myself! [%s]\n";#else /* !SFX */ /* process_zipfiles() strings */# if (defined(IZ_CHECK_TZ) && defined(USE_EF_UT_TIME)) static ZCONST char Far WarnInvalidTZ[] = "Warning: TZ environment variable not found, cannot use UTC times!!\n";# endif static ZCONST char Far FilesProcessOK[] = "%d archive%s successfully processed.\n"; static ZCONST char Far ArchiveWarning[] = "%d archive%s had warnings but no fatal errors.\n"; static ZCONST char Far ArchiveFatalError[] = "%d archive%s had fatal errors.\n"; static ZCONST char Far FileHadNoZipfileDir[] = "%d file%s had no zipfile directory.\n"; static ZCONST char Far ZipfileWasDir[] = "1 \"zipfile\" was a directory.\n"; static ZCONST char Far ManyZipfilesWereDir[] = "%d \"zipfiles\" were directories.\n"; static ZCONST char Far NoZipfileFound[] = "No zipfiles found.\n"; /* do_seekable() strings */# ifdef UNIX static ZCONST char Far CannotFindZipfileDirMsg[] = "%s: cannot find zipfile directory in one of %s or\n\ %s%s.zip, and cannot find %s, period.\n"; static ZCONST char Far CannotFindEitherZipfile[] = "%s: cannot find %s, %s.zip or %s.\n"; /* ", so there" removed 970918 */# else /* !UNIX */# ifndef AMIGA static ZCONST char Far CannotFindWildcardMatch[] = "%s: cannot find any matches for wildcard specification \"%s\".\n";# endif /* !AMIGA */ static ZCONST char Far CannotFindZipfileDirMsg[] = "%s: cannot find zipfile directory in %s,\n\ %sand cannot find %s, period.\n"; static ZCONST char Far CannotFindEitherZipfile[] = "%s: cannot find either %s or %s.\n"; /* ", so there" removed 970918 */# endif /* ?UNIX */ extern ZCONST char Far Zipnfo[]; /* in unzip.c */#ifndef WINDLL static ZCONST char Far Unzip[] = "unzip";#else static ZCONST char Far Unzip[] = "UnZip DLL";#endif static ZCONST char Far MaybeExe[] = "note: %s may be a plain executable, not an archive\n"; static ZCONST char Far CentDirNotInZipMsg[] = "\n\ [%s]:\n\ Zipfile is disk %u of a multi-disk archive, and this is not the disk on\n\ which the central zipfile directory begins (disk %u).\n"; static ZCONST char Far EndCentDirBogus[] = "\nwarning [%s]: end-of-central-directory record claims this\n\ is disk %u but that the central directory starts on disk %u; this is a\n\ contradiction. Attempting to process anyway.\n";# ifdef NO_MULTIPART static ZCONST char Far NoMultiDiskArcSupport[] = "\nerror [%s]: zipfile is part of multi-disk archive\n\ (sorry, not yet supported).\n"; static ZCONST char Far 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 ZCONST char Far 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 ZCONST char Far ExtraBytesAtStart[] = "warning [%s]: %ld extra byte%s at beginning or within zipfile\n\ (attempting to process anyway)\n";#endif /* ?SFX */static ZCONST char Far MissingBytes[] = "error [%s]: missing %ld bytes in zipfile\n\ (attempting to process anyway)\n";static ZCONST char Far NullCentDirOffset[] = "error [%s]: NULL central directory offset\n\ (attempting to process anyway)\n";static ZCONST char Far ZipfileEmpty[] = "warning [%s]: zipfile is empty\n";static ZCONST char Far CentDirStartNotFound[] = "error [%s]: start of central directory not found;\n\ zipfile corrupt.\n%s";#ifndef SFX static ZCONST char Far CentDirTooLong[] = "error [%s]: reported length of central directory is\n\ %ld bytes too long (Atari STZip zipfile? J.H.Holm ZIPSPLIT 1.1\n\ zipfile?). Compensating...\n"; static ZCONST char Far 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";#else /* SFX */ static ZCONST char Far CentDirEndSigNotFound[] = " End-of-central-directory signature not found.\n";#endif /* ?SFX */static ZCONST char Far ZipfileCommTrunc1[] = "\ncaution: zipfile comment truncated\n";/*******************************//* Function process_zipfiles() *//*******************************/int process_zipfiles(__G) /* return PK-type error code */ __GDEF{#ifndef SFX char *lastzipfn = (char *)NULL; int NumWinFiles, NumLoseFiles, NumWarnFiles; int NumMissDirs, NumMissFiles;#endif int error=0, error_in_archive=0;/*--------------------------------------------------------------------------- Start by allocating buffers and (re)constructing the various PK signature strings. ---------------------------------------------------------------------------*/ G.inbuf = (uch *)malloc(INBUFSIZ + 4); /* 4 extra for hold[] (below) */ G.outbuf = (uch *)malloc(OUTBUFSIZ + 1); /* 1 extra for string term. */ if ((G.inbuf == (uch *)NULL) || (G.outbuf == (uch *)NULL)) { Info(slide, 0x401, ((char *)slide, LoadFarString(CannotAllocateBuffers))); return(PK_MEM); } G.hold = G.inbuf + INBUFSIZ; /* to check for boundary-spanning sigs */#ifndef VMS /* VMS uses its own buffer scheme for textmode flush(). */#ifdef SMALL_MEM G.outbuf2 = G.outbuf+RAWBUFSIZ; /* never changes */#endif#endif /* !VMS */#if 0 /* CRC_32_TAB has been NULLified by CONSTRUCTGLOBALS !!!! */ /* allocate the CRC table only later when we know we have a zipfile */ CRC_32_TAB = NULL;#endif /* 0 */ /* finish up initialization of magic signature strings */ local_hdr_sig[0] /* = extd_local_sig[0] */ = 0x50; /* ASCII 'P', */ central_hdr_sig[0] = end_central_sig[0] = 0x50; /* not EBCDIC */ local_hdr_sig[1] /* = extd_local_sig[1] */ = 0x4B; /* ASCII 'K', */ central_hdr_sig[1] = end_central_sig[1] = 0x4B; /* not EBCDIC *//*--------------------------------------------------------------------------- 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 were initially copied from dos_to_unix_time() in fileio.c. probably, they are still too strict; any listed OS that supplies tzset(), regardless of whether the function does anything, should be removed from the ifdefs. ---------------------------------------------------------------------------*/#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; G.tz_is_valid = VALID_TIMEZONE(p);# ifndef SFX if (!G.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 *//* For systems that do not have tzset() but supply this function using another name (_tzset() or something similar), an appropiate "#define tzset ..." should be added to the system specifc configuration section. */#if (!defined(T20_VMS) && !defined(MACOS) && !defined(RISCOS) && !defined(QDOS))#if (!defined(BSD) && !defined(MTS) && !defined(CMS_MVS) && !defined(TANDEM)) tzset();#endif#endif/*--------------------------------------------------------------------------- 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. ---------------------------------------------------------------------------*/#ifdef SFX if ((error = do_seekable(__G__ 0)) == PK_NOZIP) {#ifdef EXE_EXTENSION int len=strlen(G.argv0); /* append .exe if appropriate; also .sfx? */ if ( (G.zipfn = (char *)malloc(len+sizeof(EXE_EXTENSION))) != (char *)NULL ) { strcpy(G.zipfn, G.argv0); strcpy(G.zipfn+len, EXE_EXTENSION); error = do_seekable(__G__ 0); free(G.zipfn); G.zipfn = G.argv0; /* for "cannot find myself" message only */ }#endif /* EXE_EXTENSION */#ifdef WIN32 G.zipfn = G.argv0; /* for "cannot find myself" message only */#endif } if (error) { if (error == IZ_DIR) error_in_archive = PK_NOZIP; else error_in_archive = error; if (error == PK_NOZIP) Info(slide, 1, ((char *)slide, LoadFarString(CannotFindMyself), G.zipfn)); }#else /* !SFX */ NumWinFiles = NumLoseFiles = NumWarnFiles = 0; NumMissDirs = NumMissFiles = 0; while ((G.zipfn = do_wild(__G__ G.wildzipfn)) != (char *)NULL) { Trace((stderr, "do_wild( %s ) returns %s\n", G.wildzipfn, G.zipfn)); lastzipfn = G.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) (*G.message)((zvoid *)&G, (uch *)"\n", 1L, 0); if ((error = do_seekable(__G__ 0)) == PK_WARN) ++NumWarnFiles; else if (error == IZ_DIR) ++NumMissDirs; else if (error == PK_NOZIP) ++NumMissFiles; else if (error) ++NumLoseFiles; else ++NumWinFiles; if (error != IZ_DIR && error > error_in_archive) error_in_archive = error; Trace((stderr, "do_seekable(0) returns %d\n", error));#ifdef WINDLL if (error == IZ_CTRLC) { free_G_buffers(__G); return error; }#endif } /* end while-loop (wildcard zipfiles) */ if ((NumWinFiles + NumWarnFiles + NumLoseFiles) == 0 && (NumMissDirs + NumMissFiles) == 1 && lastzipfn != (char *)NULL) { NumMissDirs = NumMissFiles = 0; if (error_in_archive == PK_NOZIP) error_in_archive = PK_COOL;#if (!defined(UNIX) && !defined(AMIGA)) /* filenames with wildcard characters */ if (iswild(G.wildzipfn)) Info(slide, 0x401, ((char *)slide, LoadFarString(CannotFindWildcardMatch), uO.zipinfo_mode? LoadFarStringSmall(Zipnfo) : LoadFarStringSmall(Unzip), G.wildzipfn)); else#endif { char *p = lastzipfn + strlen(lastzipfn); G.zipfn = lastzipfn; strcpy(p, ZSUFX);#if defined(UNIX) || defined(QDOS) /* 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(__G__ 0)) == PK_NOZIP || error == IZ_DIR) { if (error == IZ_DIR) ++NumMissDirs; strcpy(p, ALT_ZSUFX); error = do_seekable(__G__ 1); }#else error = do_seekable(__G__ 1);#endif if (error == PK_WARN) /* GRR: make this a switch/case stmt ... */ ++NumWarnFiles; else if (error == IZ_DIR) ++NumMissDirs; else if (error == PK_NOZIP) /* increment again => bug: "1 file had no zipfile directory." */ /* ++NumMissFiles */ ; else if (error)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -