home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip540.zip / process.c < prev    next >
C/C++ Source or Header  |  1998-09-01  |  52KB  |  1,379 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   process.c
  4.  
  5.   This file contains the top-level routines for processing multiple zipfiles.
  6.  
  7.   Contains:  process_zipfiles()
  8.              free_G_buffers()
  9.              do_seekable()
  10.              find_ecrec()
  11.              uz_end_central()
  12.              process_cdir_file_hdr()
  13.              get_cdir_ent()
  14.              process_local_file_hdr()
  15.              ef_scan_for_izux()
  16.  
  17.   ---------------------------------------------------------------------------*/
  18.  
  19.  
  20. #define UNZIP_INTERNAL
  21. #include "unzip.h"
  22. #ifdef WINDLL
  23. #  ifdef POCKET_UNZIP
  24. #    include "wince/intrface.h"
  25. #  else
  26. #    include "windll/windll.h"
  27. #  endif
  28. #endif
  29.  
  30. static int    do_seekable        OF((__GPRO__ int lastchance));
  31. static int    find_ecrec         OF((__GPRO__ long searchlen));
  32.  
  33. static ZCONST char Far CannotAllocateBuffers[] =
  34.   "error:  cannot allocate unzip buffers\n";
  35.  
  36. #ifdef SFX
  37.    static ZCONST char Far CannotFindMyself[] =
  38.      "unzipsfx:  cannot find myself! [%s]\n";
  39.  
  40. #else /* !SFX */
  41.    /* process_zipfiles() strings */
  42. # if (defined(IZ_CHECK_TZ) && defined(USE_EF_UT_TIME))
  43.      static ZCONST char Far WarnInvalidTZ[] =
  44.        "Warning: TZ environment variable not found, cannot use UTC times!!\n";
  45. # endif
  46.    static ZCONST char Far FilesProcessOK[] =
  47.      "%d archive%s successfully processed.\n";
  48.    static ZCONST char Far ArchiveWarning[] =
  49.      "%d archive%s had warnings but no fatal errors.\n";
  50.    static ZCONST char Far ArchiveFatalError[] =
  51.      "%d archive%s had fatal errors.\n";
  52.    static ZCONST char Far FileHadNoZipfileDir[] =
  53.      "%d file%s had no zipfile directory.\n";
  54.    static ZCONST char Far ZipfileWasDir[] = "1 \"zipfile\" was a directory.\n";
  55.    static ZCONST char Far ManyZipfilesWereDir[] =
  56.      "%d \"zipfiles\" were directories.\n";
  57.    static ZCONST char Far NoZipfileFound[] = "No zipfiles found.\n";
  58.  
  59.    /* do_seekable() strings */
  60. # ifdef UNIX
  61.    static ZCONST char Far CannotFindZipfileDirMsg[] =
  62.      "%s:  cannot find zipfile directory in one of %s or\n\
  63.         %s%s.zip, and cannot find %s, period.\n";
  64.    static ZCONST char Far CannotFindEitherZipfile[] =
  65.      "%s:  cannot find %s, %s.zip or %s.\n";   /* ", so there" removed 970918 */
  66. # else /* !UNIX */
  67. # ifndef AMIGA
  68.    static ZCONST char Far CannotFindWildcardMatch[] =
  69.      "%s:  cannot find any matches for wildcard specification \"%s\".\n";
  70. # endif /* !AMIGA */
  71.    static ZCONST char Far CannotFindZipfileDirMsg[] =
  72.      "%s:  cannot find zipfile directory in %s,\n\
  73.         %sand cannot find %s, period.\n";
  74.    static ZCONST char Far CannotFindEitherZipfile[] =
  75.      "%s:  cannot find either %s or %s.\n";    /* ", so there" removed 970918 */
  76. # endif /* ?UNIX */
  77.    extern ZCONST char Far Zipnfo[];       /* in unzip.c */
  78. #ifndef WINDLL
  79.    static ZCONST char Far Unzip[] = "unzip";
  80. #else
  81.    static ZCONST char Far Unzip[] = "UnZip DLL";
  82. #endif
  83.    static ZCONST char Far MaybeExe[] =
  84.      "note:  %s may be a plain executable, not an archive\n";
  85.    static ZCONST char Far CentDirNotInZipMsg[] = "\n\
  86.    [%s]:\n\
  87.      Zipfile is disk %u of a multi-disk archive, and this is not the disk on\n\
  88.      which the central zipfile directory begins (disk %u).\n";
  89.    static ZCONST char Far EndCentDirBogus[] =
  90.      "\nwarning [%s]:  end-of-central-directory record claims this\n\
  91.   is disk %u but that the central directory starts on disk %u; this is a\n\
  92.   contradiction.  Attempting to process anyway.\n";
  93. # ifdef NO_MULTIPART
  94.    static ZCONST char Far NoMultiDiskArcSupport[] =
  95.      "\nerror [%s]:  zipfile is part of multi-disk archive\n\
  96.   (sorry, not yet supported).\n";
  97.    static ZCONST char Far MaybePakBug[] = "warning [%s]:\
  98.   zipfile claims to be 2nd disk of a 2-part archive;\n\
  99.   attempting to process anyway.  If no further errors occur, this archive\n\
  100.   was probably created by PAK v2.51 or earlier.  This bug was reported to\n\
  101.   NoGate in March 1991 and was supposed to have been fixed by mid-1991; as\n\
  102.   of mid-1992 it still hadn't been.  (If further errors do occur, archive\n\
  103.   was probably created by PKZIP 2.04c or later; UnZip does not yet support\n\
  104.   multi-part archives.)\n";
  105. # else
  106.    static ZCONST char Far MaybePakBug[] = "warning [%s]:\
  107.   zipfile claims to be last disk of a multi-part archive;\n\
  108.   attempting to process anyway, assuming all parts have been concatenated\n\
  109.   together in order.  Expect \"errors\" and warnings...true multi-part support\
  110. \n  doesn't exist yet (coming soon).\n";
  111. # endif
  112.    static ZCONST char Far ExtraBytesAtStart[] =
  113.      "warning [%s]:  %ld extra byte%s at beginning or within zipfile\n\
  114.   (attempting to process anyway)\n";
  115. #endif /* ?SFX */
  116.  
  117. static ZCONST char Far MissingBytes[] =
  118.   "error [%s]:  missing %ld bytes in zipfile\n\
  119.   (attempting to process anyway)\n";
  120. static ZCONST char Far NullCentDirOffset[] =
  121.   "error [%s]:  NULL central directory offset\n\
  122.   (attempting to process anyway)\n";
  123. static ZCONST char Far ZipfileEmpty[] = "warning [%s]:  zipfile is empty\n";
  124. static ZCONST char Far CentDirStartNotFound[] =
  125.   "error [%s]:  start of central directory not found;\n\
  126.   zipfile corrupt.\n%s";
  127. #ifndef SFX
  128.    static ZCONST char Far CentDirTooLong[] =
  129.      "error [%s]:  reported length of central directory is\n\
  130.   %ld bytes too long (Atari STZip zipfile?  J.H.Holm ZIPSPLIT 1.1\n\
  131.   zipfile?).  Compensating...\n";
  132.    static ZCONST char Far CentDirEndSigNotFound[] = "\
  133.   End-of-central-directory signature not found.  Either this file is not\n\
  134.   a zipfile, or it constitutes one disk of a multi-part archive.  In the\n\
  135.   latter case the central directory and zipfile comment will be found on\n\
  136.   the last disk(s) of this archive.\n";
  137. #else /* SFX */
  138.    static ZCONST char Far CentDirEndSigNotFound[] =
  139.      "  End-of-central-directory signature not found.\n";
  140. #endif /* ?SFX */
  141. static ZCONST char Far ZipfileCommTrunc1[] =
  142.   "\ncaution:  zipfile comment truncated\n";
  143.  
  144.  
  145.  
  146.  
  147. /*******************************/
  148. /* Function process_zipfiles() */
  149. /*******************************/
  150.  
  151. int process_zipfiles(__G)    /* return PK-type error code */
  152.     __GDEF
  153. {
  154. #ifndef SFX
  155.     char *lastzipfn = (char *)NULL;
  156.     int NumWinFiles, NumLoseFiles, NumWarnFiles;
  157.     int NumMissDirs, NumMissFiles;
  158. #endif
  159.     int error=0, error_in_archive=0;
  160.  
  161.  
  162. /*---------------------------------------------------------------------------
  163.     Start by allocating buffers and (re)constructing the various PK signature
  164.     strings.
  165.   ---------------------------------------------------------------------------*/
  166.  
  167.     G.inbuf = (uch *)malloc(INBUFSIZ + 4);    /* 4 extra for hold[] (below) */
  168.     G.outbuf = (uch *)malloc(OUTBUFSIZ + 1);  /* 1 extra for string term. */
  169.  
  170.     if ((G.inbuf == (uch *)NULL) || (G.outbuf == (uch *)NULL)) {
  171.         Info(slide, 0x401, ((char *)slide,
  172.           LoadFarString(CannotAllocateBuffers)));
  173.         return(PK_MEM);
  174.     }
  175.     G.hold = G.inbuf + INBUFSIZ;     /* to check for boundary-spanning sigs */
  176. #ifndef VMS     /* VMS uses its own buffer scheme for textmode flush(). */
  177. #ifdef SMALL_MEM
  178.     G.outbuf2 = G.outbuf+RAWBUFSIZ;  /* never changes */
  179. #endif
  180. #endif /* !VMS */
  181.  
  182. #if 0 /* CRC_32_TAB has been NULLified by CONSTRUCTGLOBALS !!!! */
  183.     /* allocate the CRC table only later when we know we have a zipfile */
  184.     CRC_32_TAB = NULL;
  185. #endif /* 0 */
  186.  
  187.     /* finish up initialization of magic signature strings */
  188.     local_hdr_sig[0]  /* = extd_local_sig[0] */  = 0x50;   /* ASCII 'P', */
  189.     central_hdr_sig[0] = end_central_sig[0] = 0x50;     /* not EBCDIC */
  190.  
  191.     local_hdr_sig[1]  /* = extd_local_sig[1] */  = 0x4B;   /* ASCII 'K', */
  192.     central_hdr_sig[1] = end_central_sig[1] = 0x4B;     /* not EBCDIC */
  193.  
  194. /*---------------------------------------------------------------------------
  195.     Make sure timezone info is set correctly; localtime() returns GMT on
  196.     some OSes (e.g., Solaris 2.x) if this isn't done first.  The ifdefs were
  197.     initially copied from dos_to_unix_time() in fileio.c. probably, they are
  198.     still too strict; any listed OS that supplies tzset(), regardless of
  199.     whether the function does anything, should be removed from the ifdefs.
  200.   ---------------------------------------------------------------------------*/
  201.  
  202. #if (defined(IZ_CHECK_TZ) && defined(USE_EF_UT_TIME))
  203. #  ifndef VALID_TIMEZONE
  204. #     define VALID_TIMEZONE(tmp) \
  205.              (((tmp = getenv("TZ")) != NULL) && (*tmp != '\0'))
  206. #  endif
  207.     {
  208.         char *p;
  209.         G.tz_is_valid = VALID_TIMEZONE(p);
  210. #  ifndef SFX
  211.         if (!G.tz_is_valid) {
  212.             Info(slide, 0x401, ((char *)slide, LoadFarString(WarnInvalidTZ)));
  213.             error_in_archive = error = PK_WARN;
  214.         }
  215. #  endif /* !SFX */
  216.     }
  217. #endif /* IZ_CHECK_TZ && USE_EF_UT_TIME */
  218.  
  219. /* For systems that do not have tzset() but supply this function using another
  220.    name (_tzset() or something similar), an appropiate "#define tzset ..."
  221.    should be added to the system specifc configuration section.  */
  222. #if (!defined(T20_VMS) && !defined(MACOS) && !defined(RISCOS) && !defined(QDOS))
  223. #if (!defined(BSD) && !defined(MTS) && !defined(CMS_MVS) && !defined(TANDEM))
  224.     tzset();
  225. #endif
  226. #endif
  227.  
  228. /*---------------------------------------------------------------------------
  229.     Match (possible) wildcard zipfile specification with existing files and
  230.     attempt to process each.  If no hits, try again after appending ".zip"
  231.     suffix.  If still no luck, give up.
  232.   ---------------------------------------------------------------------------*/
  233.  
  234. #ifdef SFX
  235.     if ((error = do_seekable(__G__ 0)) == PK_NOZIP) {
  236. #ifdef EXE_EXTENSION
  237.         int len=strlen(G.argv0);
  238.  
  239.         /* append .exe if appropriate; also .sfx? */
  240.         if ( (G.zipfn = (char *)malloc(len+sizeof(EXE_EXTENSION))) !=
  241.              (char *)NULL ) {
  242.             strcpy(G.zipfn, G.argv0);
  243.             strcpy(G.zipfn+len, EXE_EXTENSION);
  244.             error = do_seekable(__G__ 0);
  245.             free(G.zipfn);
  246.             G.zipfn = G.argv0;  /* for "cannot find myself" message only */
  247.         }
  248. #endif /* EXE_EXTENSION */
  249. #ifdef WIN32
  250.         G.zipfn = G.argv0;  /* for "cannot find myself" message only */
  251. #endif
  252.     }
  253.     if (error) {
  254.         if (error == IZ_DIR)
  255.             error_in_archive = PK_NOZIP;
  256.         else
  257.             error_in_archive = error;
  258.         if (error == PK_NOZIP)
  259.             Info(slide, 1, ((char *)slide, LoadFarString(CannotFindMyself),
  260.               G.zipfn));
  261.     }
  262.  
  263. #else /* !SFX */
  264.     NumWinFiles = NumLoseFiles = NumWarnFiles = 0;
  265.     NumMissDirs = NumMissFiles = 0;
  266.  
  267.     while ((G.zipfn = do_wild(__G__ G.wildzipfn)) != (char *)NULL) {
  268.         Trace((stderr, "do_wild( %s ) returns %s\n", G.wildzipfn, G.zipfn));
  269.  
  270.         lastzipfn = G.zipfn;
  271.  
  272.         /* print a blank line between the output of different zipfiles */
  273.         if (!uO.qflag  &&  error != PK_NOZIP  &&  error != IZ_DIR
  274. #ifdef TIMESTAMP
  275.             && (!uO.T_flag || uO.zipinfo_mode)
  276. #endif
  277.             && (NumWinFiles+NumLoseFiles+NumWarnFiles+NumMissFiles) > 0)
  278.             (*G.message)((zvoid *)&G, (uch *)"\n", 1L, 0);
  279.  
  280.         if ((error = do_seekable(__G__ 0)) == PK_WARN)
  281.             ++NumWarnFiles;
  282.         else if (error == IZ_DIR)
  283.             ++NumMissDirs;
  284.         else if (error == PK_NOZIP)
  285.             ++NumMissFiles;
  286.         else if (error)
  287.             ++NumLoseFiles;
  288.         else
  289.             ++NumWinFiles;
  290.  
  291.         if (error != IZ_DIR && error > error_in_archive)
  292.             error_in_archive = error;
  293.         Trace((stderr, "do_seekable(0) returns %d\n", error));
  294. #ifdef WINDLL
  295.         if (error == IZ_CTRLC) {
  296.             free_G_buffers(__G);
  297.             return error;
  298.         }
  299. #endif
  300.  
  301.     } /* end while-loop (wildcard zipfiles) */
  302.  
  303.     if ((NumWinFiles + NumWarnFiles + NumLoseFiles) == 0  &&
  304.         (NumMissDirs + NumMissFiles) == 1  &&  lastzipfn != (char *)NULL)
  305.     {
  306.         NumMissDirs = NumMissFiles = 0;
  307.         if (error_in_archive == PK_NOZIP)
  308.             error_in_archive = PK_COOL;
  309.  
  310. #if (!defined(UNIX) && !defined(AMIGA)) /* filenames with wildcard characters */
  311.         if (iswild(G.wildzipfn))
  312.             Info(slide, 0x401, ((char *)slide,
  313.               LoadFarString(CannotFindWildcardMatch), uO.zipinfo_mode?
  314.               LoadFarStringSmall(Zipnfo) : LoadFarStringSmall(Unzip),
  315.               G.wildzipfn));
  316.         else
  317. #endif
  318.         {
  319.             char *p = lastzipfn + strlen(lastzipfn);
  320.  
  321.             G.zipfn = lastzipfn;
  322.             strcpy(p, ZSUFX);
  323.  
  324. #if defined(UNIX) || defined(QDOS)
  325.    /* only Unix has case-sensitive filesystems */
  326.    /* Well FlexOS (sometimes) also has them,  but support is per media */
  327.    /* and a pig to code for,  so treat as case insensitive for now */
  328.    /* we do this under QDOS to check for .zip as well as _zip */
  329.             if ((error = do_seekable(__G__ 0)) == PK_NOZIP || error == IZ_DIR) {
  330.                 if (error == IZ_DIR)
  331.                     ++NumMissDirs;
  332.                 strcpy(p, ALT_ZSUFX);
  333.                 error = do_seekable(__G__ 1);
  334.             }
  335. #else
  336.             error = do_seekable(__G__ 1);
  337. #endif
  338.             if (error == PK_WARN)   /* GRR: make this a switch/case stmt ... */
  339.                 ++NumWarnFiles;
  340.             else if (error == IZ_DIR)
  341.                 ++NumMissDirs;
  342.             else if (error == PK_NOZIP)
  343.                 /* increment again => bug: "1 file had no zipfile directory." */
  344.                 /* ++NumMissFiles */ ;
  345.             else if (error)
  346.                 ++NumLoseFiles;
  347.             else
  348.                 ++NumWinFiles;
  349.  
  350.             if (error > error_in_archive)
  351.                 error_in_archive = error;
  352.             Trace((stderr, "do_seekable(1) returns %d\n", error));
  353. #ifdef WINDLL
  354.             if (error == IZ_CTRLC) {
  355.                 free_G_buffers(__G);
  356.                 return error;
  357.             }
  358. #endif
  359.         }
  360.     }
  361. #endif /* ?SFX */
  362.  
  363. /*---------------------------------------------------------------------------
  364.     Print summary of all zipfiles, assuming zipfile spec was a wildcard (no
  365.     need for a summary if just one zipfile).
  366.   ---------------------------------------------------------------------------*/
  367.  
  368. #ifndef SFX
  369.     if (iswild(G.wildzipfn) && uO.qflag < 3
  370. #ifdef TIMESTAMP
  371.         && !(uO.T_flag && uO.qflag && !uO.zipinfo_mode)
  372. #endif
  373.                                                     )
  374.     {
  375.         if ((NumMissFiles + NumLoseFiles + NumWarnFiles > 0 || NumWinFiles != 1)
  376. #ifdef TIMESTAMP
  377.             && !(uO.T_flag && !uO.zipinfo_mode)
  378. #endif
  379.             && !(uO.tflag && uO.qflag > 1))
  380.             (*G.message)((zvoid *)&G, (uch *)"\n", 1L, 0x401);
  381.         if ((NumWinFiles > 1) || (NumWinFiles == 1 &&
  382.             NumMissDirs + NumMissFiles + NumLoseFiles + NumWarnFiles > 0))
  383.             Info(slide, 0x401, ((char *)slide, LoadFarString(FilesProcessOK),
  384.               NumWinFiles, (NumWinFiles == 1)? " was" : "s were"));
  385.         if (NumWarnFiles > 0)
  386.             Info(slide, 0x401, ((char *)slide, LoadFarString(ArchiveWarning),
  387.               NumWarnFiles, (NumWarnFiles == 1)? "" : "s"));
  388.         if (NumLoseFiles > 0)
  389.             Info(slide, 0x401, ((char *)slide, LoadFarString(ArchiveFatalError),
  390.               NumLoseFiles, (NumLoseFiles == 1)? "" : "s"));
  391.         if (NumMissFiles > 0)
  392.             Info(slide, 0x401, ((char *)slide,
  393.               LoadFarString(FileHadNoZipfileDir), NumMissFiles,
  394.               (NumMissFiles == 1)? "" : "s"));
  395.         if (NumMissDirs == 1)
  396.             Info(slide, 0x401, ((char *)slide, LoadFarString(ZipfileWasDir)));
  397.         else if (NumMissDirs > 0)
  398.             Info(slide, 0x401, ((char *)slide,
  399.               LoadFarString(ManyZipfilesWereDir), NumMissDirs));
  400.         if (NumWinFiles + NumLoseFiles + NumWarnFiles == 0)
  401.             Info(slide, 0x401, ((char *)slide, LoadFarString(NoZipfileFound)));
  402.     }
  403. #endif /* !SFX */
  404.  
  405.     /* free allocated memory */
  406.     free_G_buffers(__G);
  407.  
  408.     return error_in_archive;
  409.  
  410. } /* end function process_zipfiles() */
  411.  
  412.  
  413.  
  414.  
  415.  
  416. /*****************************/
  417. /* Function free_G_buffers() */
  418. /*****************************/
  419.  
  420. void free_G_buffers(__G)     /* releases all memory allocated in global vars */
  421.     __GDEF
  422. {
  423.     inflate_free(__G);
  424.     checkdir(__G__ (char *)NULL, END);
  425.  
  426. #ifdef DYNALLOC_CRCTAB
  427.     if (CRC_32_TAB) {
  428.         free_crc_table();
  429.         CRC_32_TAB = NULL;
  430.     }
  431. #endif
  432.  
  433.    if (G.key != (char *)NULL) {
  434.         free(G.key);
  435.         G.key = (char *)NULL;
  436.    }
  437.  
  438. #if (!defined(VMS) && !defined(SMALL_MEM))
  439.     /* VMS uses its own buffer scheme for textmode flush() */
  440.     if (G.outbuf2) {
  441.         free(G.outbuf2);   /* malloc'd ONLY if unshrink and -a */
  442.         G.outbuf2 = (uch *)NULL;
  443.     }
  444. #endif
  445.  
  446.     if (G.outbuf)
  447.         free(G.outbuf);
  448.     if (G.inbuf)
  449.         free(G.inbuf);
  450.     G.inbuf = G.outbuf = (uch *)NULL;
  451.  
  452. #ifdef MALLOC_WORK
  453.     if (G.area.Slide) {
  454.         free(G.area.Slide);
  455.         G.area.Slide = (uch *)NULL;
  456.     }
  457. #endif
  458.  
  459. } /* end function free_G_buffers() */
  460.  
  461.  
  462.  
  463.  
  464.  
  465. /**************************/
  466. /* Function do_seekable() */
  467. /**************************/
  468.  
  469. static int do_seekable(__G__ lastchance)        /* return PK-type error code */
  470.     __GDEF
  471.     int lastchance;
  472. {
  473. #ifndef SFX
  474.     /* static int no_ecrec = FALSE;  SKM: moved to globals.h */
  475.     int maybe_exe=FALSE;
  476.     int too_weird_to_continue=FALSE;
  477. #ifdef TIMESTAMP
  478.     time_t uxstamp;
  479.     unsigned nmember = 0;
  480. #endif
  481. #endif
  482.     int error=0, error_in_archive;
  483.  
  484.  
  485. /*---------------------------------------------------------------------------
  486.     Open the zipfile for reading in BINARY mode to prevent CR/LF translation,
  487.     which would corrupt the bit streams.
  488.   ---------------------------------------------------------------------------*/
  489.  
  490.     if (SSTAT(G.zipfn, &G.statbuf) ||
  491.         (error = S_ISDIR(G.statbuf.st_mode)) != 0)
  492.     {
  493. #ifndef SFX
  494.         if (lastchance) {
  495. #if defined(UNIX) || defined(QDOS)
  496.             if (G.no_ecrec)
  497.                 Info(slide, 1, ((char *)slide,
  498.                   LoadFarString(CannotFindZipfileDirMsg), uO.zipinfo_mode?
  499.                   LoadFarStringSmall(Zipnfo) : LoadFarStringSmall(Unzip),
  500.                   G.wildzipfn, uO.zipinfo_mode? "  " : "", G.wildzipfn,
  501.                   G.zipfn));
  502.             else
  503.                 Info(slide, 1, ((char *)slide,
  504.                   LoadFarString(CannotFindEitherZipfile), uO.zipinfo_mode?
  505.                   LoadFarStringSmall(Zipnfo) : LoadFarStringSmall(Unzip),
  506.                   G.wildzipfn, G.wildzipfn, G.zipfn));
  507. #else /* !UNIX */
  508.             if (G.no_ecrec)
  509.                 Info(slide, 0x401, ((char *)slide,
  510.                   LoadFarString(CannotFindZipfileDirMsg), uO.zipinfo_mode?
  511.                   LoadFarStringSmall(Zipnfo) : LoadFarStringSmall(Unzip),
  512.                   G.wildzipfn, uO.zipinfo_mode? "  " : "", G.zipfn));
  513.             else
  514.                 Info(slide, 0x401, ((char *)slide,
  515.                   LoadFarString(CannotFindEitherZipfile), uO.zipinfo_mode?
  516.                   LoadFarStringSmall(Zipnfo) : LoadFarStringSmall(Unzip),
  517.                   G.wildzipfn, G.zipfn));
  518. #endif /* ?UNIX */
  519.         }
  520. #endif /* !SFX */
  521.         return error? IZ_DIR : PK_NOZIP;
  522.     }
  523.     G.ziplen = G.statbuf.st_size;
  524.  
  525. #ifndef SFX
  526. #if defined(UNIX) || defined(DOS_OS2_W32)
  527.     if (G.statbuf.st_mode & S_IEXEC)   /* no extension on Unix exes:  might */
  528.         maybe_exe = TRUE;               /*  find unzip, not unzip.zip; etc. */
  529. #endif
  530. #endif /* !SFX */
  531.  
  532. #ifdef VMS
  533.     if (check_format(__G))              /* check for variable-length format */
  534.         return PK_ERR;
  535. #endif
  536.  
  537.     if (open_input_file(__G))   /* this should never happen, given */
  538.         return PK_NOZIP;        /*  the stat() test above, but... */
  539.  
  540. /*---------------------------------------------------------------------------
  541.     Find and process the end-of-central-directory header.  UnZip need only
  542.     check last 65557 bytes of zipfile:  comment may be up to 65535, end-of-
  543.     central-directory record is 18 bytes, and signature itself is 4 bytes;
  544.     add some to allow for appended garbage.  Since ZipInfo is often used as
  545.     a debugging tool, search the whole zipfile if zipinfo_mode is true.
  546.   ---------------------------------------------------------------------------*/
  547.  
  548.     /* initialize the CRC table pointer (once) */
  549.     if (CRC_32_TAB == NULL) {
  550.         if ((CRC_32_TAB = get_crc_table()) == NULL)
  551.             return PK_MEM;
  552.     }
  553.  
  554. #if (!defined(SFX) || defined(SFX_EXDIR))
  555.     /* check out if specified extraction root directory exists */
  556.     if (uO.exdir != (char *)NULL && G.extract_flag) {
  557.         G.create_dirs = !uO.fflag;
  558.         if ((error = checkdir(__G__ uO.exdir, ROOT)) > 2)
  559.             return error;   /* out of memory, or file in way */
  560.     }
  561. #endif /* !SFX || SFX_EXDIR */
  562.  
  563.     G.cur_zipfile_bufstart = 0;
  564.     G.inptr = G.inbuf;
  565.  
  566. #if (!defined(WINDLL) && !defined(SFX))
  567. #ifdef TIMESTAMP
  568.     if (!uO.zipinfo_mode && !uO.qflag && !uO.T_flag)
  569. #else
  570.     if (!uO.zipinfo_mode && !uO.qflag)
  571. #endif
  572. #ifdef WIN32    /* Win32 console may require codepage conversion for G.zipfn */
  573.         Info(slide, 0, ((char *)slide, "Archive:  %s\n", FnFilter1(G.zipfn)));
  574. #else
  575.         Info(slide, 0, ((char *)slide, "Archive:  %s\n", G.zipfn));
  576. #endif
  577. #endif /* !WINDLL && !SFX */
  578.  
  579.     if ((
  580. #ifndef NO_ZIPINFO
  581.          uO.zipinfo_mode &&
  582.           ((error_in_archive = find_ecrec(__G__ G.ziplen)) != 0 ||
  583.           (error_in_archive = zi_end_central(__G)) > PK_WARN))
  584.         || (!uO.zipinfo_mode &&
  585. #endif
  586.           ((error_in_archive = find_ecrec(__G__ MIN(G.ziplen,66000L))) != 0 ||
  587.           (error_in_archive = uz_end_central(__G)) > PK_WARN)))
  588.     {
  589.         CLOSE_INFILE();
  590.  
  591. #ifdef SFX
  592.         ++lastchance;   /* avoid picky compiler warnings */
  593.         return error_in_archive;
  594. #else
  595.         if (maybe_exe)
  596.             Info(slide, 0x401, ((char *)slide, LoadFarString(MaybeExe),
  597.             G.zipfn));
  598.         if (lastchance)
  599.             return error_in_archive;
  600.         else {
  601.             G.no_ecrec = TRUE;    /* assume we found wrong file:  e.g., */
  602.             return PK_NOZIP;       /*  unzip instead of unzip.zip */
  603.         }
  604. #endif /* ?SFX */
  605.     }
  606.  
  607.     if ((uO.zflag > 0) && !uO.zipinfo_mode) { /* unzip: zflag = comment ONLY */
  608.         CLOSE_INFILE();
  609.         return error_in_archive;
  610.     }
  611.  
  612. /*---------------------------------------------------------------------------
  613.     Test the end-of-central-directory info for incompatibilities (multi-disk
  614.     archives) or inconsistencies (missing or extra bytes in zipfile).
  615.   ---------------------------------------------------------------------------*/
  616.  
  617. #ifdef NO_MULTIPART
  618.     error = !uO.zipinfo_mode && (G.ecrec.number_this_disk == 1) &&
  619.             (G.ecrec.num_disk_start_cdir == 1);
  620. #else
  621.     error = !uO.zipinfo_mode && (G.ecrec.number_this_disk != 0);
  622. #endif
  623.  
  624. #ifndef SFX
  625.     if (uO.zipinfo_mode &&
  626.         G.ecrec.number_this_disk != G.ecrec.num_disk_start_cdir)
  627.     {
  628.         if (G.ecrec.number_this_disk > G.ecrec.num_disk_start_cdir) {
  629.             Info(slide, 0x401, ((char *)slide,
  630.               LoadFarString(CentDirNotInZipMsg), G.zipfn,
  631.               G.ecrec.number_this_disk, G.ecrec.num_disk_start_cdir));
  632.             error_in_archive = PK_FIND;
  633.             too_weird_to_continue = TRUE;
  634.         } else {
  635.             Info(slide, 0x401, ((char *)slide,
  636.               LoadFarString(EndCentDirBogus), G.zipfn,
  637.               G.ecrec.number_this_disk, G.ecrec.num_disk_start_cdir));
  638.             error_in_archive = PK_WARN;
  639.         }
  640. #ifdef NO_MULTIPART   /* concatenation of multiple parts works in some cases */
  641.     } else if (!uO.zipinfo_mode && !error && G.ecrec.number_this_disk != 0) {
  642.         Info(slide, 0x401, ((char *)slide, LoadFarString(NoMultiDiskArcSupport),
  643.           G.zipfn));
  644.         error_in_archive = PK_FIND;
  645.         too_weird_to_continue = TRUE;
  646. #endif
  647.     }
  648.  
  649.     if (!too_weird_to_continue) {  /* (relatively) normal zipfile:  go for it */
  650.         if (error) {
  651.             Info(slide, 0x401, ((char *)slide, LoadFarString(MaybePakBug),
  652.               G.zipfn));
  653.             error_in_archive = PK_WARN;
  654.         }
  655. #endif /* !SFX */
  656.         if ((G.extra_bytes = G.real_ecrec_offset-G.expect_ecrec_offset) <
  657.             (LONGINT)0)
  658.         {
  659.             Info(slide, 0x401, ((char *)slide, LoadFarString(MissingBytes),
  660.               G.zipfn, (long)(-G.extra_bytes)));
  661.             error_in_archive = PK_ERR;
  662.         } else if (G.extra_bytes > 0) {
  663.             if ((G.ecrec.offset_start_central_directory == 0) &&
  664.                 (G.ecrec.size_central_directory != 0))   /* zip 1.5 -go bug */
  665.             {
  666.                 Info(slide, 0x401, ((char *)slide,
  667.                   LoadFarString(NullCentDirOffset), G.zipfn));
  668.                 G.ecrec.offset_start_central_directory = G.extra_bytes;
  669.                 G.extra_bytes = 0;
  670.                 error_in_archive = PK_ERR;
  671.             }
  672. #ifndef SFX
  673.             else {
  674.                 Info(slide, 0x401, ((char *)slide,
  675.                   LoadFarString(ExtraBytesAtStart), G.zipfn,
  676.                   (long)G.extra_bytes, (G.extra_bytes == 1)? "":"s"));
  677.                 error_in_archive = PK_WARN;
  678.             }
  679. #endif /* !SFX */
  680.         }
  681.  
  682.     /*-----------------------------------------------------------------------
  683.         Check for empty zipfile and exit now if so.
  684.       -----------------------------------------------------------------------*/
  685.  
  686.         if (G.expect_ecrec_offset==0L && G.ecrec.size_central_directory==0) {
  687.             if (uO.zipinfo_mode)
  688.                 Info(slide, 0, ((char *)slide, "%sEmpty zipfile.\n",
  689.                   uO.lflag>9? "\n  " : ""));
  690.             else
  691.                 Info(slide, 0x401, ((char *)slide, LoadFarString(ZipfileEmpty),
  692.                                     G.zipfn));
  693.             CLOSE_INFILE();
  694.             return (error_in_archive > PK_WARN)? error_in_archive : PK_WARN;
  695.         }
  696.  
  697.     /*-----------------------------------------------------------------------
  698.         Compensate for missing or extra bytes, and seek to where the start
  699.         of central directory should be.  If header not found, uncompensate
  700.         and try again (necessary for at least some Atari archives created
  701.         with STZip, as well as archives created by J.H. Holm's ZIPSPLIT 1.1).
  702.       -----------------------------------------------------------------------*/
  703.  
  704.         ZLSEEK( G.ecrec.offset_start_central_directory )
  705. #ifdef OLD_SEEK_TEST
  706.         if (readbuf(G.sig, 4) == 0) {
  707.             CLOSE_INFILE();
  708.             return PK_ERR;  /* file may be locked, or possibly disk error(?) */
  709.         }
  710.         if (strncmp(G.sig, central_hdr_sig, 4))
  711. #else
  712.         if ((readbuf(__G__ G.sig, 4) == 0) ||
  713.              strncmp(G.sig, central_hdr_sig, 4))
  714. #endif
  715.         {
  716. #ifndef SFX
  717.             long tmp = G.extra_bytes;
  718. #endif
  719.  
  720.             G.extra_bytes = 0;
  721.             ZLSEEK( G.ecrec.offset_start_central_directory )
  722.             if ((readbuf(__G__ G.sig, 4) == 0) ||
  723.                 strncmp(G.sig, central_hdr_sig, 4))
  724.             {
  725.                 Info(slide, 0x401, ((char *)slide,
  726.                   LoadFarString(CentDirStartNotFound), G.zipfn,
  727.                   LoadFarStringSmall(ReportMsg)));
  728.                 CLOSE_INFILE();
  729.                 return PK_BADERR;
  730.             }
  731. #ifndef SFX
  732.             Info(slide, 0x401, ((char *)slide, LoadFarString(CentDirTooLong),
  733.               G.zipfn, -tmp));
  734. #endif
  735.             error_in_archive = PK_ERR;
  736.         }
  737.  
  738.     /*-----------------------------------------------------------------------
  739.         Seek to the start of the central directory one last time, since we
  740.         have just read the first entry's signature bytes; then list, extract
  741.         or test member files as instructed, and close the zipfile.
  742.       -----------------------------------------------------------------------*/
  743.  
  744.         Trace((stderr, "about to extract/list files (error = %d)\n",
  745.           error_in_archive));
  746.  
  747.         ZLSEEK( G.ecrec.offset_start_central_directory )
  748.  
  749. #ifdef DLL
  750.         /* G.fValidate is used only to look at an archive to see if
  751.            it appears to be a valid archive.  There is no interest
  752.            in what the archive contains, nor in validating that the
  753.            entries in the archive are in good condition.  This is
  754.            currently used only in the Windows DLLs for purposes of
  755.            checking archives within an archive to determine whether
  756.            or not to display the inner archives.
  757.          */
  758.         if (!G.fValidate)
  759. #endif
  760.         {
  761. #ifndef NO_ZIPINFO
  762.             if (uO.zipinfo_mode)
  763.                 error = zipinfo(__G);                     /* ZIPINFO 'EM */
  764.             else
  765. #endif
  766. #ifndef SFX
  767. #ifdef TIMESTAMP
  768.             if (uO.T_flag)
  769.                 error = get_time_stamp(__G__ &uxstamp, &nmember);
  770.             else
  771. #endif
  772.             if (uO.vflag && !uO.tflag && !uO.cflag)
  773.                 error = list_files(__G);              /* LIST 'EM */
  774.             else
  775. #endif /* !SFX */
  776.                 error = extract_or_test_files(__G);   /* EXTRACT OR TEST 'EM */
  777.  
  778.             Trace((stderr, "done with extract/list files (error = %d)\n",
  779.                    error));
  780.         }
  781.  
  782.         if (error > error_in_archive)   /* don't overwrite stronger error */
  783.             error_in_archive = error;   /*  with (for example) a warning */
  784. #ifndef SFX
  785.     } /* end if (!too_weird_to_continue) */
  786. #endif
  787.  
  788.     CLOSE_INFILE();
  789.  
  790. #ifdef TIMESTAMP
  791.     if (uO.T_flag && !uO.zipinfo_mode && (nmember > 0)) {
  792. # ifdef WIN32
  793.         if (stamp_file(__G__ G.zipfn, uxstamp)) {       /* TIME-STAMP 'EM */
  794. # else
  795.         if (stamp_file(G.zipfn, uxstamp)) {             /* TIME-STAMP 'EM */
  796. # endif
  797.             Info(slide, 0x201, ((char *)slide,
  798.               "warning:  cannot set time for %s\n", G.zipfn));
  799.             if (error_in_archive < PK_WARN)
  800.                 error_in_archive = PK_WARN;
  801.         }
  802.     }
  803. #endif
  804.     return error_in_archive;
  805.  
  806. } /* end function do_seekable() */
  807.  
  808.  
  809.  
  810.  
  811.  
  812. /*************************/
  813. /* Function find_ecrec() */
  814. /*************************/
  815.  
  816. static int find_ecrec(__G__ searchlen)          /* return PK-class error */
  817.     __GDEF
  818.     long searchlen;
  819. {
  820.     int i, numblks, found=FALSE;
  821.     LONGINT tail_len;
  822.     ec_byte_rec byterec;
  823.  
  824.  
  825. /*---------------------------------------------------------------------------
  826.     Treat case of short zipfile separately.
  827.   ---------------------------------------------------------------------------*/
  828.  
  829.     if (G.ziplen <= INBUFSIZ) {
  830.         lseek(G.zipfd, 0L, SEEK_SET);
  831.         if ((G.incnt = read(G.zipfd,(char *)G.inbuf,(unsigned int)G.ziplen))
  832.             == (int)G.ziplen)
  833.  
  834.             /* 'P' must be at least 22 bytes from end of zipfile */
  835.             for (G.inptr = G.inbuf+(int)G.ziplen-22;  G.inptr >= G.inbuf;
  836.                  --G.inptr)
  837.                 if ((native(*G.inptr) == 'P')  &&
  838.                      !strncmp((char *)G.inptr, end_central_sig, 4)) {
  839.                     G.incnt -= (int)(G.inptr - G.inbuf);
  840.                     found = TRUE;
  841.                     break;
  842.                 }
  843.  
  844. /*---------------------------------------------------------------------------
  845.     Zipfile is longer than INBUFSIZ:  may need to loop.  Start with short
  846.     block at end of zipfile (if not TOO short).
  847.   ---------------------------------------------------------------------------*/
  848.  
  849.     } else {
  850.         if ((tail_len = G.ziplen % INBUFSIZ) > ECREC_SIZE) {
  851. #ifdef USE_STRM_INPUT
  852.             fseek((FILE *)G.zipfd, G.ziplen-tail_len, SEEK_SET);
  853.             G.cur_zipfile_bufstart = ftell((FILE *)G.zipfd);
  854. #else /* !USE_STRM_INPUT */
  855.             G.cur_zipfile_bufstart = lseek(G.zipfd, G.ziplen-tail_len,
  856.               SEEK_SET);
  857. #endif /* ?USE_STRM_INPUT */
  858.             if ((G.incnt = read(G.zipfd, (char *)G.inbuf,
  859.                 (unsigned int)tail_len)) != (int)tail_len)
  860.                 goto fail;      /* it's expedient... */
  861.  
  862.             /* 'P' must be at least 22 bytes from end of zipfile */
  863.             for (G.inptr = G.inbuf+(int)tail_len-22;  G.inptr >= G.inbuf;
  864.                  --G.inptr)
  865.                 if ((native(*G.inptr) == 'P')  &&
  866.                      !strncmp((char *)G.inptr, end_central_sig, 4)) {
  867.                     G.incnt -= (int)(G.inptr - G.inbuf);
  868.                     found = TRUE;
  869.                     break;
  870.                 }
  871.             /* sig may span block boundary: */
  872.             strncpy((char *)G.hold, (char *)G.inbuf, 3);
  873.         } else
  874.             G.cur_zipfile_bufstart = G.ziplen - tail_len;
  875.  
  876.     /*-----------------------------------------------------------------------
  877.         Loop through blocks of zipfile data, starting at the end and going
  878.         toward the beginning.  In general, need not check whole zipfile for
  879.         signature, but may want to do so if testing.
  880.       -----------------------------------------------------------------------*/
  881.  
  882.         numblks = (int)((searchlen - tail_len + (INBUFSIZ-1)) / INBUFSIZ);
  883.         /*               ==amount=   ==done==   ==rounding==    =blksiz=  */
  884.  
  885.         for (i = 1;  !found && (i <= numblks);  ++i) {
  886.             G.cur_zipfile_bufstart -= INBUFSIZ;
  887.             lseek(G.zipfd, G.cur_zipfile_bufstart, SEEK_SET);
  888.             if ((G.incnt = read(G.zipfd,(char *)G.inbuf,INBUFSIZ))
  889.                 != INBUFSIZ)
  890.                 break;          /* fall through and fail */
  891.  
  892.             for (G.inptr = G.inbuf+INBUFSIZ-1;  G.inptr >= G.inbuf;
  893.                  --G.inptr)
  894.                 if ((native(*G.inptr) == 'P')  &&
  895.                      !strncmp((char *)G.inptr, end_central_sig, 4)) {
  896.                     G.incnt -= (int)(G.inptr - G.inbuf);
  897.                     found = TRUE;
  898.                     break;
  899.                 }
  900.             /* sig may span block boundary: */
  901.             strncpy((char *)G.hold, (char *)G.inbuf, 3);
  902.         }
  903.     } /* end if (ziplen > INBUFSIZ) */
  904.  
  905. /*---------------------------------------------------------------------------
  906.     Searched through whole region where signature should be without finding
  907.     it.  Print informational message and die a horrible death.
  908.   ---------------------------------------------------------------------------*/
  909.  
  910. fail:
  911.     if (!found) {
  912.         if (uO.qflag || uO.zipinfo_mode)
  913.             Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  914.         Info(slide, 0x401, ((char *)slide,
  915.           LoadFarString(CentDirEndSigNotFound)));
  916.         return PK_ERR;   /* failed */
  917.     }
  918.  
  919. /*---------------------------------------------------------------------------
  920.     Found the signature, so get the end-central data before returning.  Do
  921.     any necessary machine-type conversions (byte ordering, structure padding
  922.     compensation) by reading data into character array and copying to struct.
  923.   ---------------------------------------------------------------------------*/
  924.  
  925.     G.real_ecrec_offset = G.cur_zipfile_bufstart + (G.inptr-G.inbuf);
  926. #ifdef TEST
  927.     printf("\n  found end-of-central-dir signature at offset %ld (%.8lXh)\n",
  928.       G.real_ecrec_offset, G.real_ecrec_offset);
  929.     printf("    from beginning of file; offset %d (%.4Xh) within block\n",
  930.       G.inptr-G.inbuf, G.inptr-G.inbuf);
  931. #endif
  932.  
  933.     if (readbuf(__G__ (char *)byterec, ECREC_SIZE+4) == 0)
  934.         return PK_EOF;
  935.  
  936.     G.ecrec.number_this_disk =
  937.       makeword(&byterec[NUMBER_THIS_DISK]);
  938.     G.ecrec.num_disk_start_cdir =
  939.       makeword(&byterec[NUM_DISK_WITH_START_CENTRAL_DIR]);
  940.     G.ecrec.num_entries_centrl_dir_ths_disk =
  941.       makeword(&byterec[NUM_ENTRIES_CENTRL_DIR_THS_DISK]);
  942.     G.ecrec.total_entries_central_dir =
  943.       makeword(&byterec[TOTAL_ENTRIES_CENTRAL_DIR]);
  944.     G.ecrec.size_central_directory =
  945.       makelong(&byterec[SIZE_CENTRAL_DIRECTORY]);
  946.     G.ecrec.offset_start_central_directory =
  947.       makelong(&byterec[OFFSET_START_CENTRAL_DIRECTORY]);
  948.     G.ecrec.zipfile_comment_length =
  949.       makeword(&byterec[ZIPFILE_COMMENT_LENGTH]);
  950.  
  951.     G.expect_ecrec_offset = G.ecrec.offset_start_central_directory +
  952.                           G.ecrec.size_central_directory;
  953.     return PK_COOL;
  954.  
  955. } /* end function find_ecrec() */
  956.  
  957.  
  958.  
  959.  
  960.  
  961. /*****************************/
  962. /* Function uz_end_central() */
  963. /*****************************/
  964.  
  965. int uz_end_central(__G)    /* return PK-type error code */
  966.     __GDEF
  967. {
  968.     int error = PK_COOL;
  969.  
  970.  
  971. /*---------------------------------------------------------------------------
  972.     Get the zipfile comment (up to 64KB long), if any, and print it out.
  973.     Then position the file pointer to the beginning of the central directory
  974.     and fill buffer.
  975.   ---------------------------------------------------------------------------*/
  976.  
  977. #ifdef WINDLL
  978.     /* for comment button: */
  979.     if ((!G.fValidate) && (G.lpUserFunctions != NULL))
  980.        G.lpUserFunctions->cchComment = G.ecrec.zipfile_comment_length;
  981.     if (G.ecrec.zipfile_comment_length && (uO.zflag > 0))
  982. #else /* !WINDLL */
  983.     if (G.ecrec.zipfile_comment_length && (uO.zflag > 0 ||
  984.         (uO.zflag == 0 &&
  985. #ifdef TIMESTAMP
  986.                           !uO.T_flag &&
  987. #endif
  988.                                         !uO.qflag)))
  989. #endif /* ?WINDLL */
  990.     {
  991.         if (do_string(__G__ G.ecrec.zipfile_comment_length, DISPLAY)) {
  992.             Info(slide, 0x401, ((char *)slide,
  993.               LoadFarString(ZipfileCommTrunc1)));
  994.             error = PK_WARN;
  995.         }
  996.     }
  997.     return error;
  998.  
  999. } /* end function uz_end_central() */
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005. /************************************/
  1006. /* Function process_cdir_file_hdr() */
  1007. /************************************/
  1008.  
  1009. int process_cdir_file_hdr(__G)    /* return PK-type error code */
  1010.     __GDEF
  1011. {
  1012.     int error;
  1013.  
  1014.  
  1015. /*---------------------------------------------------------------------------
  1016.     Get central directory info, save host and method numbers, and set flag
  1017.     for lowercase conversion of filename, depending on the OS from which the
  1018.     file is coming.
  1019.   ---------------------------------------------------------------------------*/
  1020.  
  1021.     if ((error = get_cdir_ent(__G)) != 0)
  1022.         return error;
  1023.  
  1024.     G.pInfo->hostnum = MIN(G.crec.version_made_by[1], NUM_HOSTS);
  1025. /*  extnum = MIN(crec.version_needed_to_extract[1], NUM_HOSTS); */
  1026.  
  1027.     G.pInfo->lcflag = 0;
  1028.     if (uO.L_flag)            /* user specified case-conversion */
  1029.         switch (G.pInfo->hostnum) {
  1030.             case FS_FAT_:     /* PKZIP and zip -k store in uppercase */
  1031.             case CPM_:        /* like MS-DOS, right? */
  1032.             case VM_CMS_:     /* all caps? */
  1033.             case MVS_:        /* all caps? */
  1034.             case TANDEM_:
  1035.             case TOPS20_:
  1036.             case VMS_:        /* our Zip uses lowercase, but ASi's doesn't */
  1037.         /*  case Z_SYSTEM_:   ? */
  1038.         /*  case QDOS_:       ? */
  1039.                 G.pInfo->lcflag = 1;   /* convert filename to lowercase */
  1040.                 break;
  1041.  
  1042.             default:     /* AMIGA_, FS_HPFS_, FS_NTFS_, MAC_, UNIX_, ATARI_, */
  1043.                 break;   /*  FS_VFAT_, BEOS_ (Z_SYSTEM_):  no conversion */
  1044.         }
  1045.  
  1046.     /* do Amigas (AMIGA_) also have volume labels? */
  1047.     if (IS_VOLID(G.crec.external_file_attributes) &&
  1048.         (G.pInfo->hostnum == FS_FAT_ || G.pInfo->hostnum == FS_HPFS_ ||
  1049.          G.pInfo->hostnum == FS_NTFS_ || G.pInfo->hostnum == ATARI_))
  1050.     {
  1051.         G.pInfo->vollabel = TRUE;
  1052.         G.pInfo->lcflag = 0;        /* preserve case of volume labels */
  1053.     } else
  1054.         G.pInfo->vollabel = FALSE;
  1055.  
  1056.     return PK_COOL;
  1057.  
  1058. } /* end function process_cdir_file_hdr() */
  1059.  
  1060.  
  1061.  
  1062.  
  1063.  
  1064. /***************************/
  1065. /* Function get_cdir_ent() */
  1066. /***************************/
  1067.  
  1068. int get_cdir_ent(__G)   /* return PK-type error code */
  1069.     __GDEF
  1070. {
  1071.     cdir_byte_hdr byterec;
  1072.  
  1073.  
  1074. /*---------------------------------------------------------------------------
  1075.     Read the next central directory entry and do any necessary machine-type
  1076.     conversions (byte ordering, structure padding compensation--do so by
  1077.     copying the data from the array into which it was read (byterec) to the
  1078.     usable struct (crec)).
  1079.   ---------------------------------------------------------------------------*/
  1080.  
  1081.     if (readbuf(__G__ (char *)byterec, CREC_SIZE) == 0)
  1082.         return PK_EOF;
  1083.  
  1084.     G.crec.version_made_by[0] = byterec[C_VERSION_MADE_BY_0];
  1085.     G.crec.version_made_by[1] = byterec[C_VERSION_MADE_BY_1];
  1086.     G.crec.version_needed_to_extract[0] =
  1087.       byterec[C_VERSION_NEEDED_TO_EXTRACT_0];
  1088.     G.crec.version_needed_to_extract[1] =
  1089.       byterec[C_VERSION_NEEDED_TO_EXTRACT_1];
  1090.  
  1091.     G.crec.general_purpose_bit_flag =
  1092.       makeword(&byterec[C_GENERAL_PURPOSE_BIT_FLAG]);
  1093.     G.crec.compression_method =
  1094.       makeword(&byterec[C_COMPRESSION_METHOD]);
  1095.     G.crec.last_mod_dos_datetime =
  1096.       makelong(&byterec[C_LAST_MOD_DOS_DATETIME]);
  1097.     G.crec.crc32 =
  1098.       makelong(&byterec[C_CRC32]);
  1099.     G.crec.csize =
  1100.       makelong(&byterec[C_COMPRESSED_SIZE]);
  1101.     G.crec.ucsize =
  1102.       makelong(&byterec[C_UNCOMPRESSED_SIZE]);
  1103.     G.crec.filename_length =
  1104.       makeword(&byterec[C_FILENAME_LENGTH]);
  1105.     G.crec.extra_field_length =
  1106.       makeword(&byterec[C_EXTRA_FIELD_LENGTH]);
  1107.     G.crec.file_comment_length =
  1108.       makeword(&byterec[C_FILE_COMMENT_LENGTH]);
  1109.     G.crec.disk_number_start =
  1110.       makeword(&byterec[C_DISK_NUMBER_START]);
  1111.     G.crec.internal_file_attributes =
  1112.       makeword(&byterec[C_INTERNAL_FILE_ATTRIBUTES]);
  1113.     G.crec.external_file_attributes =
  1114.       makelong(&byterec[C_EXTERNAL_FILE_ATTRIBUTES]);  /* LONG, not word! */
  1115.     G.crec.relative_offset_local_header =
  1116.       makelong(&byterec[C_RELATIVE_OFFSET_LOCAL_HEADER]);
  1117.  
  1118.     return PK_COOL;
  1119.  
  1120. } /* end function get_cdir_ent() */
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126. /*************************************/
  1127. /* Function process_local_file_hdr() */
  1128. /*************************************/
  1129.  
  1130. int process_local_file_hdr(__G)    /* return PK-type error code */
  1131.     __GDEF
  1132. {
  1133.     local_byte_hdr byterec;
  1134.  
  1135.  
  1136. /*---------------------------------------------------------------------------
  1137.     Read the next local file header and do any necessary machine-type con-
  1138.     versions (byte ordering, structure padding compensation--do so by copy-
  1139.     ing the data from the array into which it was read (byterec) to the
  1140.     usable struct (lrec)).
  1141.   ---------------------------------------------------------------------------*/
  1142.  
  1143.     if (readbuf(__G__ (char *)byterec, LREC_SIZE) == 0)
  1144.         return PK_EOF;
  1145.  
  1146.     G.lrec.version_needed_to_extract[0] =
  1147.       byterec[L_VERSION_NEEDED_TO_EXTRACT_0];
  1148.     G.lrec.version_needed_to_extract[1] =
  1149.       byterec[L_VERSION_NEEDED_TO_EXTRACT_1];
  1150.  
  1151.     G.lrec.general_purpose_bit_flag =
  1152.       makeword(&byterec[L_GENERAL_PURPOSE_BIT_FLAG]);
  1153.     G.lrec.compression_method = makeword(&byterec[L_COMPRESSION_METHOD]);
  1154.     G.lrec.last_mod_dos_datetime = makelong(&byterec[L_LAST_MOD_DOS_DATETIME]);
  1155.     G.lrec.crc32 = makelong(&byterec[L_CRC32]);
  1156.     G.lrec.csize = makelong(&byterec[L_COMPRESSED_SIZE]);
  1157.     G.lrec.ucsize = makelong(&byterec[L_UNCOMPRESSED_SIZE]);
  1158.     G.lrec.filename_length = makeword(&byterec[L_FILENAME_LENGTH]);
  1159.     G.lrec.extra_field_length = makeword(&byterec[L_EXTRA_FIELD_LENGTH]);
  1160.  
  1161.     G.csize = (long) G.lrec.csize;
  1162.     G.ucsize = (long) G.lrec.ucsize;
  1163.  
  1164.     if ((G.lrec.general_purpose_bit_flag & 8) != 0) {
  1165.         /* can't trust local header, use central directory: */
  1166.         G.lrec.crc32 = G.pInfo->crc;
  1167.         G.csize = (long)(G.lrec.csize = G.pInfo->compr_size);
  1168.         G.ucsize = (long)(G.lrec.ucsize = G.pInfo->uncompr_size);
  1169.     }
  1170.  
  1171.     return PK_COOL;
  1172.  
  1173. } /* end function process_local_file_hdr() */
  1174.  
  1175.  
  1176. #ifdef USE_EF_UT_TIME
  1177.  
  1178. /*******************************/
  1179. /* Function ef_scan_for_izux() */
  1180. /*******************************/
  1181.  
  1182. unsigned ef_scan_for_izux(ef_buf, ef_len, ef_is_c, dos_mdatetime,
  1183.                           z_utim, z_uidgid)
  1184.     uch *ef_buf;        /* buffer containing extra field */
  1185.     unsigned ef_len;    /* total length of extra field */
  1186.     int ef_is_c;        /* flag indicating "is central extra field" */
  1187.     ulg dos_mdatetime;  /* last_mod_file_date_time in DOS format */
  1188.     iztimes *z_utim;    /* return storage: atime, mtime, ctime */
  1189.     ush *z_uidgid;      /* return storage: uid and gid */
  1190. {
  1191.     unsigned flags = 0;
  1192.     unsigned eb_id;
  1193.     unsigned eb_len;
  1194.     int have_new_type_eb = FALSE;
  1195.     int ut_zip_unzip_compatible = FALSE;
  1196.  
  1197. /*---------------------------------------------------------------------------
  1198.     This function scans the extra field for EF_TIME, EF_IZUNIX2, EF_IZUNIX, or
  1199.     EF_PKUNIX blocks containing Unix-style time_t (GMT) values for the entry's
  1200.     access, creation, and modification time.
  1201.     If a valid block is found, the time stamps are copied to the iztimes
  1202.     structure (provided the z_utim pointer is not NULL).
  1203.     If a IZUNIX2 block is found or the IZUNIX block contains UID/GID fields,
  1204.     and the z_uidgid array pointer is valid (!= NULL), the owner info is
  1205.     transfered as well.
  1206.     The presence of an EF_TIME or EF_IZUNIX2 block results in ignoring all
  1207.     data from probably present obsolete EF_IZUNIX blocks.
  1208.     If multiple blocks of the same type are found, only the information from
  1209.     the last block is used.
  1210.     The return value is a combination of the EF_TIME Flags field with an
  1211.     additional flag bit indicating the presence of valid UID/GID info,
  1212.     or 0 in case of failure.
  1213.   ---------------------------------------------------------------------------*/
  1214.  
  1215.     if (ef_len == 0 || ef_buf == NULL || (z_utim == 0 && z_uidgid == NULL))
  1216.         return 0;
  1217.  
  1218.     TTrace((stderr,"\nef_scan_for_izux: scanning extra field of length %u\n",
  1219.       ef_len));
  1220.  
  1221.     while (ef_len >= EB_HEADSIZE) {
  1222.         eb_id = makeword(EB_ID + ef_buf);
  1223.         eb_len = makeword(EB_LEN + ef_buf);
  1224.  
  1225.         if (eb_len > (ef_len - EB_HEADSIZE)) {
  1226.             /* discovered some extra field inconsistency! */
  1227.             TTrace((stderr,
  1228.               "ef_scan_for_izux: block length %u > rest ef_size %u\n", eb_len,
  1229.               ef_len - EB_HEADSIZE));
  1230.             break;
  1231.         }
  1232.  
  1233.         switch (eb_id) {
  1234.           case EF_TIME:
  1235.             flags &= ~0x0ff;    /* ignore previous IZUNIX or EF_TIME fields */
  1236.             have_new_type_eb = TRUE;
  1237.             if ( eb_len >= EB_UT_MINLEN && z_utim != NULL) {
  1238.                 unsigned eb_idx = EB_UT_TIME1;
  1239.                 TTrace((stderr,"ef_scan_for_izux: found TIME extra field\n"));
  1240.                 flags |= (ef_buf[EB_HEADSIZE+EB_UT_FLAGS] & 0x0ff);
  1241.                 if ((flags & EB_UT_FL_MTIME)) {
  1242.                     if ((eb_idx+4) <= eb_len) {
  1243.                         z_utim->mtime = makelong((EB_HEADSIZE+eb_idx) + ef_buf);
  1244.                         eb_idx += 4;
  1245.                         TTrace((stderr,"  UT e.f. modification time = %ld\n",
  1246.                                 z_utim->mtime));
  1247.  
  1248.                         if ((ulg)(z_utim->mtime) & (ulg)(0x80000000L)) {
  1249.                             ut_zip_unzip_compatible =
  1250.                               ((time_t)0x80000000L < (time_t)0L)
  1251.                               ? (dos_mdatetime == DOSTIME_MINIMUM)
  1252.                               : (dos_mdatetime >= DOSTIME_2038_01_18);
  1253.                             if (!ut_zip_unzip_compatible) {
  1254.                             /* UnZip interpretes mtime differently than Zip;
  1255.                                without modtime: ignore complete UT field */
  1256.                               flags &= ~0x0ff;  /* no time_t times available */
  1257.                               TTrace((stderr,
  1258.                                 "  UT modtime range error; ignore e.f.!\n"));
  1259.                               break;            /* stop scanning this field */
  1260.                             }
  1261.                         } else {
  1262.                             /* cannot determine, safe assumption is FALSE */
  1263.                             ut_zip_unzip_compatible = FALSE;
  1264.                         }
  1265.                     } else {
  1266.                         flags &= ~EB_UT_FL_MTIME;
  1267.                         TTrace((stderr,"  UT e.f. truncated; no modtime\n"));
  1268.                     }
  1269.                 }
  1270.                 if (ef_is_c) {
  1271.                     break;      /* central version of TIME field ends here */
  1272.                 }
  1273.  
  1274.                 if (flags & EB_UT_FL_ATIME) {
  1275.                     if ((eb_idx+4) <= eb_len) {
  1276.                         z_utim->atime = makelong((EB_HEADSIZE+eb_idx) + ef_buf);
  1277.                         eb_idx += 4;
  1278.                         TTrace((stderr,"  UT e.f. access time = %ld\n",
  1279.                                 z_utim->atime));
  1280.                         if (((ulg)(z_utim->atime) & (ulg)(0x80000000L)) &&
  1281.                             !ut_zip_unzip_compatible) {
  1282.                             flags &= ~EB_UT_FL_ATIME;
  1283.                             TTrace((stderr,
  1284.                               "  UT access time range error: skip time!\n"));
  1285.                         }
  1286.                     } else {
  1287.                         flags &= ~EB_UT_FL_ATIME;
  1288.                     }
  1289.                 }
  1290.                 if (flags & EB_UT_FL_CTIME) {
  1291.                     if ((eb_idx+4) <= eb_len) {
  1292.                         z_utim->ctime = makelong((EB_HEADSIZE+eb_idx) + ef_buf);
  1293.                         TTrace((stderr,"  UT e.f. creation time = %ld\n",
  1294.                                 z_utim->ctime));
  1295.                         if (((ulg)(z_utim->ctime) & (ulg)(0x80000000L)) &&
  1296.                             !ut_zip_unzip_compatible) {
  1297.                             flags &= ~EB_UT_FL_CTIME;
  1298.                             TTrace((stderr,
  1299.                               "  UT creation time range error: skip time!\n"));
  1300.                         }
  1301.                     } else {
  1302.                         flags &= ~EB_UT_FL_CTIME;
  1303.                     }
  1304.                 }
  1305.             }
  1306.             break;
  1307.  
  1308.           case EF_IZUNIX2:
  1309.             if (!have_new_type_eb) {
  1310.                 flags &= ~0x0ff;        /* ignore any previous IZUNIX field */
  1311.                 have_new_type_eb = TRUE;
  1312.             }
  1313.             if (eb_len >= EB_UX2_MINLEN && z_uidgid != NULL) {
  1314.                 z_uidgid[0] = makeword((EB_HEADSIZE+EB_UX2_UID) + ef_buf);
  1315.                 z_uidgid[1] = makeword((EB_HEADSIZE+EB_UX2_GID) + ef_buf);
  1316.                 flags |= EB_UX2_VALID;   /* signal success */
  1317.             }
  1318.             break;
  1319.  
  1320.           case EF_IZUNIX:
  1321.           case EF_PKUNIX:       /* PKUNIX e.f. layout is identical to IZUNIX */
  1322.             if (eb_len >= EB_UX_MINLEN) {
  1323.                 TTrace((stderr,"ef_scan_for_izux: found %s extra field\n",
  1324.                         (eb_id == EF_IZUNIX ? "IZUNIX" : "PKUNIX")));
  1325.                 if (have_new_type_eb) {
  1326.                     break;      /* Ignore IZUNIX extra field block ! */
  1327.                 }
  1328.                 if (z_utim != NULL) {
  1329.                     z_utim->atime = makelong((EB_HEADSIZE+EB_UX_ATIME)+ef_buf);
  1330.                     z_utim->mtime = makelong((EB_HEADSIZE+EB_UX_MTIME)+ef_buf);
  1331.                     TTrace((stderr,"  Unix EF actime = %ld\n", z_utim->atime));
  1332.                     TTrace((stderr,"  Unix EF modtime = %ld\n", z_utim->mtime));
  1333.                     flags |= (EB_UT_FL_MTIME | EB_UT_FL_ATIME);
  1334.                     if ((ulg)(z_utim->mtime) & (ulg)(0x80000000L)) {
  1335.                         ut_zip_unzip_compatible =
  1336.                           ((time_t)0x80000000L < (time_t)0L)
  1337.                           ? (dos_mdatetime == DOSTIME_MINIMUM)
  1338.                           : (dos_mdatetime >= DOSTIME_2038_01_18);
  1339.                         if (!ut_zip_unzip_compatible) {
  1340.                             /* UnZip interpretes mtime differently than Zip;
  1341.                                without modtime: ignore complete UT field */
  1342.                             flags &= ~0x0ff;    /* no time_t times available */
  1343.                             TTrace((stderr,
  1344.                                   "  UX modtime range error: ignore e.f.!\n"));
  1345.                         }
  1346.                     } else {
  1347.                         /* cannot determine, safe assumption is FALSE */
  1348.                         ut_zip_unzip_compatible = FALSE;
  1349.                     }
  1350.                     if ((ulg)(z_utim->atime) & (ulg)(0x80000000L) &&
  1351.                         !ut_zip_unzip_compatible && (flags & 0x0ff)) {
  1352.                         /* atime not in range of UnZip's time_t */
  1353.                         flags &= ~EB_UT_FL_ATIME;
  1354.                         TTrace((stderr,
  1355.                                 "  UX access time range error: skip time!\n"));
  1356.                     }
  1357.                 }
  1358.                 if (eb_len >= EB_UX_FULLSIZE && z_uidgid != NULL) {
  1359.                     z_uidgid[0] = makeword((EB_HEADSIZE+EB_UX_UID) + ef_buf);
  1360.                     z_uidgid[1] = makeword((EB_HEADSIZE+EB_UX_GID) + ef_buf);
  1361.                     flags |= EB_UX2_VALID;
  1362.                 }
  1363.             }
  1364.             break;
  1365.  
  1366.           default:
  1367.             break;
  1368.         }
  1369.  
  1370.         /* Skip this extra field block */
  1371.         ef_buf += (eb_len + EB_HEADSIZE);
  1372.         ef_len -= (eb_len + EB_HEADSIZE);
  1373.     }
  1374.  
  1375.     return flags;
  1376. }
  1377.  
  1378. #endif /* USE_EF_UT_TIME */
  1379.