home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / SOFTWARE / SOURCES / UNZIP52.ZIP / process.c (.txt) < prev    next >
C/C++ Source or Header  |  1996-04-29  |  42KB  |  1,105 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.              do_seekable()
  9.              find_ecrec()
  10.              uz_end_central()
  11.              process_cdir_file_hdr()
  12.              get_cdir_ent()
  13.              process_local_file_hdr()
  14.              ef_scan_for_izux()
  15.  
  16.   ---------------------------------------------------------------------------*/
  17.  
  18.  
  19. #define UNZIP_INTERNAL
  20. #include "unzip.h"
  21. #ifdef MSWIN
  22. #  include <windows.h>
  23. #  include "wingui\wizunzip.h"
  24. #endif
  25.  
  26. static int    do_seekable        OF((__GPRO__ int lastchance));
  27. static int    find_ecrec         OF((__GPRO__ long searchlen));
  28.  
  29. static char Far CantAllocateBuffers[] =
  30.   "error:  can't allocate unzip buffers\n";
  31.  
  32. #ifdef SFX
  33.    static char Far CantFindMyself[] =
  34.      "unzipsfx:  can't find myself! [%s]\n";
  35. #else /* !SFX */
  36.  
  37.    /* process_zipfiles() strings */
  38.    static char Far FilesProcessOK[] = "%d archive%s successfully processed.\n";
  39.    static char Far ArchiveWarning[] =
  40.      "%d archive%s had warnings but no fatal errors.\n";
  41.    static char Far ArchiveFatalError[] = "%d archive%s had fatal errors.\n";
  42.    static char Far FileHadNoZipfileDir[] =
  43.      "%d file%s had no zipfile directory.\n";
  44.    static char Far ZipfileWasDir[] = "1 \"zipfile\" was a directory.\n";
  45.    static char Far ManyZipfilesWereDir[] =
  46.      "%d \"zipfiles\" were directories.\n";
  47.    static char Far NoZipfileFound[] = "No zipfiles found.\n";
  48.  
  49.    /* do_seekable() strings */
  50. # ifdef UNIX
  51.    static char Far CantFindZipfileDirMsg[] =
  52.      "%s:  can't find zipfile directory in one of %s or\n\
  53.         %s%s.zip, and can't find %s, period.\n";
  54.    static char Far CantFindEitherZipfile[] =
  55.      "%s:  can't find %s, %s.zip or %s, so there.\n";
  56. # else /* !UNIX */
  57.    static char Far CantFindWildcardMatch[] =
  58.      "%s:  can't find any matches for wildcard specification \"%s\".\n";
  59.    static char Far CantFindZipfileDirMsg[] =
  60.      "%s:  can't find zipfile directory in %s,\n\
  61.         %sand can't find %s, period.\n";
  62.    static char Far CantFindEitherZipfile[] =
  63.      "%s:  can't find either %s or %s, so there.\n";
  64. # endif /* ?UNIX */
  65.    extern char Far Zipnfo[];              /* in unzip.c */
  66. #ifndef MSWIN
  67.    static char Far Unzip[] = "unzip";
  68. #else
  69.    static char Far Unzip[] = "WizUnZip";
  70. #endif
  71.    static char Far MaybeExe[] =
  72.      "note:  %s may be a plain executable, not an archive\n";
  73.    static char Far CentDirNotInZipMsg[] = "\n\
  74.    [%s]:\n\
  75.      Zipfile is disk %u of a multi-disk archive, and this is not the disk on\n\
  76.      which the central zipfile directory begins (disk %u).\n";
  77.    static char Far EndCentDirBogus[] =
  78.      "\nwarning [%s]:  end-of-central-directory record claims this\n\
  79.   is disk %u but that the central directory starts on disk %u; this is a\n\
  80.   contradiction.  Attempting to process anyway.\n";
  81. # ifdef NO_MULTIPART
  82.    static char Far NoMultiDiskArcSupport[] =
  83.      "\nerror [%s]:  zipfile is part of multi-disk archive\n\
  84.   (sorry, not yet supported).\n";
  85.    static char Far MaybePakBug[] = "warning [%s]:\
  86.   zipfile claims to be 2nd disk of a 2-part archive;\n\
  87.   attempting to process anyway.  If no further errors occur, this archive\n\
  88.   was probably created by PAK v2.51 or earlier.  This bug was reported to\n\
  89.   NoGate in March 1991 and was supposed to have been fixed by mid-1991; as\n\
  90.   of mid-1992 it still hadn't been.  (If further errors do occur, archive\n\
  91.   was probably created by PKZIP 2.04c or later; UnZip does not yet support\n\
  92.   multi-part archives.)\n";
  93. # else
  94.    static char Far MaybePakBug[] = "warning [%s]:\
  95.   zipfile claims to be last disk of a multi-part archive;\n\
  96.   attempting to process anyway, assuming all parts have been concatenated\n\
  97.   together in order.  Expect \"errors\" and warnings...true multi-part support\
  98. \n  doesn't exist yet (coming soon).\n";
  99. # endif
  100.    static char Far ExtraBytesAtStart[] =
  101.      "warning [%s]:  %ld extra byte%s at beginning or within zipfile\n\
  102.   (attempting to process anyway)\n";
  103. #endif /* ?SFX */
  104.  
  105. static char Far MissingBytes[] =
  106.   "error [%s]:  missing %ld bytes in zipfile\n\
  107.   (attempting to process anyway)\n";
  108. static char Far NullCentDirOffset[] =
  109.   "error [%s]:  NULL central directory offset\n\
  110.   (attempting to process anyway)\n";
  111. static char Far ZipfileEmpty[] = "warning [%s]:  zipfile is empty\n";
  112. static char Far CentDirStartNotFound[] =
  113.   "error [%s]:  start of central directory not found;\n\
  114.   zipfile corrupt.\n%s";
  115. static char Far CentDirTooLong[] =
  116.   "error [%s]:  reported length of central directory is\n\
  117.   %ld bytes too long (Atari STZip zipfile?  J.H.Holm ZIPSPLIT 1.1\n\
  118.   zipfile?).  Compensating...\n";
  119. static char Far CentDirEndSigNotFound[] = "\
  120.   End-of-central-directory signature not found.  Either this file is not\n\
  121.   a zipfile, or it constitutes one disk of a multi-part archive.  In the\n\
  122.   latter case the central directory and zipfile comment will be found on\n\
  123.   the last disk(s) of this archive.\n";
  124. static char Far ZipfileCommTrunc1[] = "\ncaution:  zipfile comment truncated\n";
  125.  
  126.  
  127.  
  128.  
  129. /*******************************/
  130. /* Function process_zipfiles() */
  131. /*******************************/
  132.  
  133. int process_zipfiles(__G)    /* return PK-type error code */
  134.      __GDEF
  135. {
  136. #ifndef SFX
  137.     char *lastzipfn = (char *)NULL;
  138.     int NumWinFiles, NumLoseFiles, NumWarnFiles;
  139.     int NumMissDirs, NumMissFiles;
  140. #endif
  141.     int error=0, error_in_archive=0;
  142.  
  143.  
  144. /*---------------------------------------------------------------------------
  145.     Start by allocating buffers and (re)constructing the various PK signature
  146.     strings.
  147.   ---------------------------------------------------------------------------*/
  148.  
  149.     G.inbuf = (uch *)malloc(INBUFSIZ + 4);    /* 4 extra for hold[] (below) */
  150.  
  151.     /* Normally realbuf and outbuf will be the same.  However, if the data
  152.      * are redirected to a large memory buffer, realbuf will point to the
  153.      * new location while outbuf will remain pointing to the malloc'd
  154.      * memory buffer.  (The extra "1" is for string termination.) */
  155.     G.realbuf = G.outbuf = (uch *)malloc(OUTBUFSIZ + 1);
  156.  
  157.     if ((G.inbuf == (uch *)NULL) || (G.outbuf == (uch *)NULL)) {
  158.         Info(slide, 0x401, ((char *)slide, LoadFarString(CantAllocateBuffers)));
  159.         RETURN(PK_MEM);
  160.     }
  161.     G.hold = G.inbuf + INBUFSIZ;     /* to check for boundary-spanning sigs */
  162. #ifndef VMS     /* VMS uses its own buffer scheme for textmode flush(). */
  163. #ifdef SMALL_MEM
  164.     G.outbuf2 = G.outbuf+RAWBUFSIZ;  /* never changes */
  165. #endif
  166. #endif /* !VMS */
  167.  
  168. #if 0 /* CRC_32_TAB has been NULLified by CONSTRUCTGLOBALS !!!! */
  169.     /* allocate the CRC table only later when we know we have a zipfile */
  170.     CRC_32_TAB = (ulg *)NULL;
  171. #endif /* 0 */
  172.  
  173.     G.local_hdr_sig[0]  /* = extd_local_sig[0] */  = 0x50;   /* ASCII 'P', */
  174.     G.central_hdr_sig[0] = G.end_central_sig[0] = 0x50;     /* not EBCDIC */
  175.  
  176.     G.local_hdr_sig[1]  /* = extd_local_sig[1] */  = 0x4B;   /* ASCII 'K', */
  177.     G.central_hdr_sig[1] = G.end_central_sig[1] = 0x4B;     /* not EBCDIC */
  178.  
  179.     /* GRR FIX:  no need for these to be in global struct; OK to "overwrite" */
  180.     strcpy(G.local_hdr_sig+2, LOCAL_HDR_SIG);
  181.     strcpy(G.central_hdr_sig+2, CENTRAL_HDR_SIG);
  182.     strcpy(G.end_central_sig+2, END_CENTRAL_SIG);
  183. /*  strcpy(extd_local_sig+2, EXTD_LOCAL_SIG);   still to be used in multi? */
  184.  
  185. #if (defined(OS2) && defined(__IBMC__))
  186.     _tzset();   /* initialize timezone info (from TZ) since no other chance */
  187. #endif          /*  (underscore name is accepted by all versions of C Set) */
  188.  
  189. /*---------------------------------------------------------------------------
  190.     Match (possible) wildcard zipfile specification with existing files and
  191.     attempt to process each.  If no hits, try again after appending ".zip"
  192.     suffix.  If still no luck, give up.
  193.   ---------------------------------------------------------------------------*/
  194.  
  195. #ifdef SFX
  196.     if ((error = do_seekable(__G__ 0)) == PK_NOZIP) {
  197. #ifdef EXE_EXTENSION
  198.         int len=strlen(G.argv0);
  199.  
  200.         /* append .exe if appropriate; also .sfx? */
  201.         if ( (G.zipfn = (char *)malloc(len+sizeof(EXE_EXTENSION)