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