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