home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip531.zip / extract.c < prev    next >
C/C++ Source or Header  |  1997-05-25  |  72KB  |  1,873 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   extract.c
  4.  
  5.   This file contains the high-level routines ("driver routines") for extrac-
  6.   ting and testing zipfile members.  It calls the low-level routines in files
  7.   explode.c, inflate.c, unreduce.c and unshrink.c.
  8.  
  9.   Contains:  extract_or_test_files()
  10.              store_info()
  11.              extract_or_test_member()
  12.              TestExtraField()
  13.              test_OS2()
  14.              memextract()
  15.              memflush()
  16.              fnfilter()
  17.  
  18.   ---------------------------------------------------------------------------*/
  19.  
  20.  
  21. #define EXTRACT_C
  22. #define UNZIP_INTERNAL
  23. #include "unzip.h"
  24. #include "crypt.h"
  25. #ifdef WINDLL
  26. #  ifdef POCKET_UNZIP
  27. #    include "wince/intrface.h"
  28. #  else
  29. #    include "windll/windll.h"
  30. #  endif
  31. #endif
  32.  
  33. #define GRRDUMP(buf,len) { \
  34.     int i, j; \
  35.  \
  36.     for (j = 0;  j < (len)/16;  ++j) { \
  37.         printf("        "); \
  38.         for (i = 0;  i < 16;  ++i) \
  39.             printf("%02x ", (uch)(buf)[i+(j<<4)]); \
  40.         printf("\n        "); \
  41.         for (i = 0;  i < 16;  ++i) { \
  42.             char c = (char)(buf)[i+(j<<4)]; \
  43.  \
  44.             if (c == '\n') \
  45.                 printf("\\n "); \
  46.             else if (c == '\r') \
  47.                 printf("\\r "); \
  48.             else \
  49.                 printf(" %c ", c); \
  50.         } \
  51.         printf("\n"); \
  52.     } \
  53.     if ((len) % 16) { \
  54.         printf("        "); \
  55.         for (i = j<<4;  i < (len);  ++i) \
  56.             printf("%02x ", (uch)(buf)[i]); \
  57.         printf("\n        "); \
  58.         for (i = j<<4;  i < (len);  ++i) { \
  59.             char c = (char)(buf)[i]; \
  60.  \
  61.             if (c == '\n') \
  62.                 printf("\\n "); \
  63.             else if (c == '\r') \
  64.                 printf("\\r "); \
  65.             else \
  66.                 printf(" %c ", c); \
  67.         } \
  68.         printf("\n"); \
  69.     } \
  70. }
  71.  
  72. static int store_info OF((__GPRO));
  73. static int extract_or_test_member OF((__GPRO));
  74. #ifndef SFX
  75.    static int TestExtraField OF((__GPRO__ uch *ef, unsigned ef_len));
  76.    static int test_OS2 OF((__GPRO__ uch *eb, unsigned eb_size));
  77. #endif
  78. #ifdef UNIX
  79.    static int dircomp OF((ZCONST zvoid *a, ZCONST zvoid *b));
  80. #endif
  81.  
  82.  
  83.  
  84. /*******************************/
  85. /*  Strings used in extract.c  */
  86. /*******************************/
  87.  
  88. static char Far VersionMsg[] =
  89.   "   skipping: %-22s  need %s compat. v%u.%u (can do v%u.%u)\n";
  90. static char Far ComprMsgNum[] =
  91.   "   skipping: %-22s  unsupported compression method %d\n";
  92. #ifndef SFX
  93.    static char Far ComprMsgName[] =
  94.      "   skipping: %-22s  `%s' method not supported\n";
  95.    static char Far CmprNone[]       = "store";
  96.    static char Far CmprShrink[]     = "shrink";
  97.    static char Far CmprReduce[]     = "reduce";
  98.    static char Far CmprImplode[]    = "implode";
  99.    static char Far CmprTokenize[]   = "tokenize";
  100.    static char Far CmprDeflate[]    = "deflate";
  101.    static char Far CmprEnDeflate[]  = "enhanced deflate";
  102.    static char Far CmprDCLImplode[] = "DCL implode";
  103.    static char Far *ComprNames[NUM_METHODS] = {
  104.      CmprNone, CmprShrink, CmprReduce, CmprReduce, CmprReduce, CmprReduce,
  105.      CmprImplode, CmprTokenize, CmprDeflate, CmprEnDeflate, CmprDCLImplode
  106.    };
  107. #endif /* !SFX */
  108. static char Far FilNamMsg[] =
  109.   "%s:  bad filename length (%s)\n";
  110. static char Far ExtFieldMsg[] =
  111.   "%s:  bad extra field length (%s)\n";
  112. static char Far OffsetMsg[] =
  113.   "file #%d:  bad zipfile offset (%s):  %ld\n";
  114. static char Far ExtractMsg[] =
  115.   "%8sing: %-22s  %s%s";
  116. #ifndef SFX
  117.    static char Far LengthMsg[] =
  118.      "%s  %s:  %ld bytes required to uncompress to %lu bytes;\n    %s\
  119.       supposed to require %lu bytes%s%s%s\n";
  120. #endif
  121.  
  122. static char Far BadFileCommLength[] = "%s:  bad file comment length\n";
  123. static char Far LocalHdrSig[] = "local header sig";
  124. static char Far BadLocalHdr[] = "file #%d:  bad local header\n";
  125. static char Far AttemptRecompensate[] = "  (attempting to re-compensate)\n";
  126. #ifndef SFX
  127.    static char Far BackslashPathSep[] =
  128.      "warning:  %s appears to use backslashes as path separators\n";
  129. #endif
  130. static char Far SkipVolumeLabel[] = "   skipping: %-22s  %svolume label\n";
  131.  
  132. #ifdef UNIX  /* messages of code for setting directory attributes */
  133.    static char Far DirlistNoMem[] =
  134.      "warning: can't alloc memory for dir times/permissions/UIDs/GIDs\n";
  135.    static char Far DirlistEntryNoMem[] =
  136.      "can't alloc memory for dir times/permissions/UID/GID\n";
  137.    static char Far DirlistSortNoMem[] =
  138.      "warning: can't alloc memory to sort dir times/perms/etc.\n";
  139.    static char Far DirlistUidGidFailed[] =
  140.      "warning:  can't set UID %d and/or GID %d for %s\n";
  141.    static char Far DirlistUtimeFailed[] =
  142.      "warning:  can't set modification, access times for %s\n";
  143. #  ifndef NO_CHMOD
  144.      static char Far DirlistChmodFailed[] =
  145.        "warning:  can't set permissions for %s\n";
  146. #  endif
  147. #endif
  148.  
  149. #ifndef WINDLL
  150.    static char Far ReplaceQuery[] =
  151.      "replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ";
  152.    static char Far AssumeNone[] = " NULL\n(assuming [N]one)\n";
  153.    static char Far NewNameQuery[] = "new name: ";
  154.    static char Far InvalidResponse[] = "error:  invalid response [%c]\n";
  155. #endif /* !WINDLL */
  156.  
  157. static char Far ErrorInArchive[] = "At least one %serror was detected in %s.\n";
  158. static char Far ZeroFilesTested[] = "Caution:  zero files tested in %s.\n";
  159.  
  160. #ifndef VMS
  161.    static char Far VMSFormatQuery[] =
  162.      "\n%s:  stored in VMS format.  Extract anyway? (y/n) ";
  163. #endif
  164.  
  165. #if CRYPT
  166.    static char Far SkipCantGetPasswd[] =
  167.      "   skipping: %-22s  unable to get password\n";
  168.    static char Far SkipIncorrectPasswd[] =
  169.      "   skipping: %-22s  incorrect password\n";
  170.    static char Far FilesSkipBadPasswd[] =
  171.      "%d file%s skipped because of incorrect password.\n";
  172.    static char Far MaybeBadPasswd[] =
  173.      "    (may instead be incorrect password)\n";
  174. #else
  175.    static char Far SkipEncrypted[] =
  176.      "   skipping: %-22s  encrypted (not supported)\n";
  177. #endif
  178.  
  179. static char Far NoErrInCompData[] =
  180.   "No errors detected in compressed data of %s.\n";
  181. static char Far NoErrInTestedFiles[] =
  182.   "No errors detected in %s for the %d file%s tested.\n";
  183. static char Far FilesSkipped[] =
  184.   "%d file%s skipped because of unsupported compression or encoding.\n";
  185.  
  186. static char Far ErrUnzipFile[] = "  error:  %s%s %s\n";
  187. static char Far ErrUnzipNoFile[] = "\n  error:  %s%s\n";
  188. static char Far NotEnoughMem[] = "not enough memory to ";
  189. static char Far InvalidComprData[] = "invalid compressed data to ";
  190. static char Far Inflate[] = "inflate";
  191.  
  192. #ifndef SFX
  193.    static char Far Explode[] = "explode";
  194. #ifndef LZW_CLEAN
  195.    static char Far Unshrink[] = "unshrink";
  196. #endif
  197. #endif
  198.  
  199. #if (!defined(DELETE_IF_FULL) || !defined(HAVE_UNLINK))
  200.    static char Far FileTruncated[] = "warning:  %s is probably truncated\n";
  201. #endif
  202.  
  203. static char Far FileUnknownCompMethod[] = "%s:  unknown compression method\n";
  204. static char Far BadCRC[] = " bad CRC %08lx  (should be %08lx)\n";
  205.  
  206.       /* TruncEAs[] also used in OS/2 mapname(), close_outfile() */
  207. char Far TruncEAs[] = " compressed EA data missing (%d bytes)%s";
  208. char Far TruncNTSD[] = " compressed WinNT security data missing (%d bytes)%s";
  209.  
  210. #ifndef SFX
  211.    static char Far InconsistEFlength[] =
  212.      "bad EF entry: block length %u > rest EF_size %u\n";
  213.    static char Far InvalidComprDataEAs[] = " invalid compressed data for EAs\n";
  214.    static char Far BadCRC_EAs[] = " bad CRC for extended attributes\n";
  215.    static char Far UnknComprMethodEAs[] =
  216.      " unknown compression method for EAs (%u)\n";
  217.    static char Far NotEnoughMemEAs[] = " out of memory while inflating EAs\n";
  218.    static char Far UnknErrorEAs[] = " unknown error on extended attributes\n";
  219. #endif /* !SFX */
  220.  
  221. static char Far UnsupportedExtraField[] =
  222.   "\nerror:  unsupported extra field compression type (%u)--skipping\n";
  223. static char Far BadExtraFieldCRC[] =
  224.   "error [%s]:  bad extra field CRC %08lx (should be %08lx)\n";
  225.  
  226.  
  227.  
  228.  
  229.  
  230. /**************************************/
  231. /*  Function extract_or_test_files()  */
  232. /**************************************/
  233.  
  234. int extract_or_test_files(__G)    /* return PK-type error code */
  235.      __GDEF
  236. {
  237.     uch *cd_inptr;
  238.     int i, j, cd_incnt, renamed, query, filnum=(-1), blknum=0;
  239.     int error, error_in_archive=PK_COOL, *fn_matched=NULL, *xn_matched=NULL;
  240. #ifdef WINDLL
  241.     int done_once = 0;
  242. #else
  243.     int len;
  244. #endif
  245.     ush members_remaining, num_skipped=0, num_bad_pwd=0;
  246.     long cd_bufstart, bufstart, inbuf_offset, request;
  247.     LONGINT old_extra_bytes = 0L;
  248. #ifdef UNIX
  249.     int do_dirtimes=TRUE, num_dirs=0;
  250.     dirtime *dirlist=(dirtime *)NULL, **sorted_dirlist=(dirtime **)NULL;
  251. #endif
  252.  
  253.  
  254. /*---------------------------------------------------------------------------
  255.     The basic idea of this function is as follows.  Since the central di-
  256.     rectory lies at the end of the zipfile and the member files lie at the
  257.     beginning or middle or wherever, it is not very desirable to simply
  258.     read a central directory entry, jump to the member and extract it, and
  259.     then jump back to the central directory.  In the case of a large zipfile
  260.     this would lead to a whole lot of disk-grinding, especially if each mem-
  261.     ber file is small.  Instead, we read from the central directory the per-
  262.     tinent information for a block of files, then go extract/test the whole
  263.     block.  Thus this routine contains two small(er) loops within a very
  264.     large outer loop:  the first of the small ones reads a block of files
  265.     from the central directory; the second extracts or tests each file; and
  266.     the outer one loops over blocks.  There's some file-pointer positioning
  267.     stuff in between, but that's about it.  Btw, it's because of this jump-
  268.     ing around that we can afford to be lenient if an error occurs in one of
  269.     the member files:  we should still be able to go find the other members,
  270.     since we know the offset of each from the beginning of the zipfile.
  271.   ---------------------------------------------------------------------------*/
  272.  
  273.     G.pInfo = G.info;
  274.     members_remaining = G.ecrec.total_entries_central_dir;
  275.  
  276. #ifdef UNIX
  277.     dirlist = (dirtime *)malloc(members_remaining*sizeof(dirtime));
  278.     if (dirlist == (dirtime *)NULL) {
  279.         Info(slide, 0x401, ((char *)slide, LoadFarString(DirlistNoMem)));
  280.         do_dirtimes = FALSE;
  281.     }
  282. #endif /* UNIX */
  283.  
  284. #if CRYPT
  285.     G.newzip = TRUE;
  286. #endif
  287. #ifndef SFX
  288.     G.reported_backslash = FALSE;
  289. #endif
  290.  
  291.     /* malloc space for check on unmatched filespecs (OK if one or both NULL) */
  292.     if (G.filespecs > 0  &&
  293.         (fn_matched=(int *)malloc(G.filespecs*sizeof(int))) != (int *)NULL)
  294.         for (i = 0;  i < G.filespecs;  ++i)
  295.             fn_matched[i] = FALSE;
  296.     if (G.xfilespecs > 0  &&
  297.         (xn_matched=(int *)malloc(G.xfilespecs*sizeof(int))) != (int *)NULL)
  298.         for (i = 0;  i < G.xfilespecs;  ++i)
  299.             xn_matched[i] = FALSE;
  300.  
  301. /*---------------------------------------------------------------------------
  302.     Begin main loop over blocks of member files.  We know the entire central
  303.     directory is on this disk:  we would not have any of this information un-
  304.     less the end-of-central-directory record was on this disk, and we would
  305.     not have gotten to this routine unless this is also the disk on which
  306.     the central directory starts.  In practice, this had better be the ONLY
  307.     disk in the archive, but we'll add multi-disk support soon.
  308.   ---------------------------------------------------------------------------*/
  309.  
  310.     while (members_remaining) {
  311.         j = 0;
  312. #ifdef AMIGA
  313.         memzero(G.filenotes, DIR_BLKSIZ * sizeof(char *));
  314. #endif
  315.  
  316.         /*
  317.          * Loop through files in central directory, storing offsets, file
  318.          * attributes, case-conversion and text-conversion flags until block
  319.          * size is reached.
  320.          */
  321.  
  322.         while (members_remaining && (j < DIR_BLKSIZ)) {
  323.             --members_remaining;
  324.             G.pInfo = &G.info[j];
  325.  
  326.             if (readbuf(__G__ G.sig, 4) == 0) {
  327.                 error_in_archive = PK_EOF;
  328.                 members_remaining = 0;  /* ...so no more left to do */
  329.                 break;
  330.             }
  331.             if (strncmp(G.sig, G.central_hdr_sig, 4)) {  /* just to make sure */
  332.                 Info(slide, 0x401, ((char *)slide, LoadFarString(CentSigMsg),
  333.                   j));
  334.                 Info(slide, 0x401, ((char *)slide, LoadFarString(ReportMsg)));
  335.                 error_in_archive = PK_BADERR;
  336.                 members_remaining = 0;  /* ...so no more left to do */
  337.                 break;
  338.             }
  339.             /* process_cdir_file_hdr() sets pInfo->hostnum, pInfo->lcflag */
  340.             if ((error = process_cdir_file_hdr(__G)) != PK_COOL) {
  341.                 error_in_archive = error;   /* only PK_EOF defined */
  342.                 members_remaining = 0;  /* ...so no more left to do */
  343.                 break;
  344.             }
  345.             if ((error = do_string(__G__ G.crec.filename_length, DS_FN)) !=
  346.                  PK_COOL)
  347.             {
  348.                 if (error > error_in_archive)
  349.                     error_in_archive = error;
  350.                 if (error > PK_WARN) {  /* fatal:  no more left to do */
  351.                     Info(slide, 0x401, ((char *)slide, LoadFarString(FilNamMsg),
  352.                       FnFilter1(G.filename), "central"));
  353.                     members_remaining = 0;
  354.                     break;
  355.                 }
  356.             }
  357.             if ((error = do_string(__G__ G.crec.extra_field_length,
  358.                 EXTRA_FIELD)) != 0)
  359.             {
  360.                 if (error > error_in_archive)
  361.                     error_in_archive = error;
  362.                 if (error > PK_WARN) {  /* fatal */
  363.                     Info(slide, 0x401, ((char *)slide,
  364.                       LoadFarString(ExtFieldMsg),
  365.                       FnFilter1(G.filename), "central"));
  366.                     members_remaining = 0;
  367.                     break;
  368.                 }
  369.             }
  370. #ifdef AMIGA
  371.             G.filenote_slot = j;
  372.             if ((error = do_string(__G__ G.crec.file_comment_length,
  373.                                    G.N_flag ? FILENOTE : SKIP)) != PK_COOL)
  374. #else
  375.             if ((error = do_string(__G__ G.crec.file_comment_length, SKIP))
  376.                 != PK_COOL)
  377. #endif
  378.             {
  379.                 if (error > error_in_archive)
  380.                     error_in_archive = error;
  381.                 if (error > PK_WARN) {  /* fatal */
  382.                     Info(slide, 0x421, ((char *)slide,
  383.                       LoadFarString(BadFileCommLength),
  384.                       FnFilter1(G.filename)));
  385.                     members_remaining = 0;
  386.                     break;
  387.                 }
  388.             }
  389.             if (G.process_all_files) {
  390.                 if (store_info(__G))
  391.                     ++j;  /* file is OK; info[] stored; continue with next */
  392.                 else
  393.                     ++num_skipped;
  394.             } else {
  395.                 int   do_this_file = FALSE;
  396.                 char  **pfn = G.pfnames-1;
  397.  
  398.                 while (*++pfn)
  399.                     if (match(G.filename, *pfn, G.C_flag)) {
  400.                         do_this_file = TRUE;   /* ^-- ignore case or not? */
  401.                         if (fn_matched)
  402.                             fn_matched[(int)(pfn-G.pfnames)] = TRUE;
  403.                         break;       /* found match, so stop looping */
  404.                     }
  405.                 if (do_this_file) {  /* check if this is an excluded file */
  406.                     char  **pxn = G.pxnames-1;
  407.  
  408.                     while (*++pxn)
  409.                         if (match(G.filename, *pxn, G.C_flag)) {
  410.                             do_this_file = FALSE;  /* ^-- ignore case or not? */
  411.                             if (xn_matched)
  412.                                 xn_matched[(int)(pxn-G.pxnames)] = TRUE;
  413.                             break;
  414.                         }
  415.                 }
  416.                 if (do_this_file)
  417.                     if (store_info(__G))
  418.                         ++j;            /* file is OK */
  419.                     else
  420.                         ++num_skipped;  /* unsupp. compression or encryption */
  421.             } /* end if (process_all_files) */
  422.  
  423.  
  424.         } /* end while-loop (adding files to current block) */
  425.  
  426.         /* save position in central directory so can come back later */
  427.         cd_bufstart = G.cur_zipfile_bufstart;
  428.         cd_inptr = G.inptr;
  429.         cd_incnt = G.incnt;
  430.  
  431.     /*-----------------------------------------------------------------------
  432.         Second loop:  process files in current block, extracting or testing
  433.         each one.
  434.       -----------------------------------------------------------------------*/
  435.  
  436.         for (i = 0; i < j; ++i) {
  437.             filnum = i + blknum*DIR_BLKSIZ;
  438.             G.pInfo = &G.info[i];
  439. #ifdef NOVELL_BUG_FAILSAFE
  440.             G.dne = FALSE;  /* assume file exists until stat() says otherwise */
  441. #endif
  442.  
  443.             /* if the target position is not within the current input buffer
  444.              * (either haven't yet read far enough, or (maybe) skipping back-
  445.              * ward), skip to the target position and reset readbuf(). */
  446.  
  447.             /* ZLSEEK(pInfo->offset):  */
  448.             request = G.pInfo->offset + G.extra_bytes;
  449.             inbuf_offset = request % INBUFSIZ;
  450.             bufstart = request - inbuf_offset;
  451.  
  452.             Trace((stderr, "\ndebug: request = %ld, inbuf_offset = %ld\n",
  453.               request, inbuf_offset));
  454.             Trace((stderr,
  455.               "debug: bufstart = %ld, cur_zipfile_bufstart = %ld\n",
  456.               bufstart, G.cur_zipfile_bufstart));
  457.             if (request < 0) {
  458.                 Info(slide, 0x401, ((char *)slide, LoadFarStringSmall(SeekMsg),
  459.                   G.zipfn, LoadFarString(ReportMsg)));
  460.                 error_in_archive = PK_ERR;
  461.                 if (filnum == 0 && G.extra_bytes != 0L) {
  462.                     Info(slide, 0x401, ((char *)slide,
  463.                       LoadFarString(AttemptRecompensate)));
  464.                     old_extra_bytes = G.extra_bytes;
  465.                     G.extra_bytes = 0L;
  466.                     request = G.pInfo->offset;  /* could also check if != 0 */
  467.                     inbuf_offset = request % INBUFSIZ;
  468.                     bufstart = request - inbuf_offset;
  469.                     Trace((stderr, "debug: request = %ld, inbuf_offset = %ld\n",
  470.                       request, inbuf_offset));
  471.                     Trace((stderr,
  472.                       "debug: bufstart = %ld, cur_zipfile_bufstart = %ld\n",
  473.                       bufstart, G.cur_zipfile_bufstart));
  474.                 } else {
  475.                     error_in_archive = PK_BADERR;
  476.                     continue;  /* this one hosed; try next */
  477.                 }
  478.             }
  479.             /* try again */
  480.             if (request < 0) {
  481.                 Trace((stderr, "debug: recompensated request still < 0\n"));
  482.                 Info(slide, 0x401, ((char *)slide, LoadFarStringSmall(SeekMsg),
  483.                   G.zipfn, LoadFarString(ReportMsg)));
  484.                 error_in_archive = PK_BADERR;
  485.                 continue;
  486.             } else if (bufstart != G.cur_zipfile_bufstart) {
  487.                 Trace((stderr, "debug: bufstart != cur_zipfile_bufstart\n"));
  488. #ifdef USE_STRM_INPUT
  489.                 fseek((FILE *)G.zipfd,(LONGINT)bufstart,SEEK_SET);
  490.                 G.cur_zipfile_bufstart = ftell((FILE *)G.zipfd);
  491. #else /* !USE_STRM_INPUT */
  492.                 G.cur_zipfile_bufstart =
  493.                   lseek(G.zipfd,(LONGINT)bufstart,SEEK_SET);
  494. #endif /* ?USE_STRM_INPUT */
  495.                 if ((G.incnt = read(G.zipfd,(char *)G.inbuf,INBUFSIZ)) <= 0)
  496.                 {
  497.                     Info(slide, 0x401, ((char *)slide, LoadFarString(OffsetMsg),
  498.                       filnum, "lseek", bufstart));
  499.                     error_in_archive = PK_BADERR;
  500.                     continue;   /* can still do next file */
  501.                 }
  502.                 G.inptr = G.inbuf + (int)inbuf_offset;
  503.                 G.incnt -= (int)inbuf_offset;
  504.             } else {
  505.                 G.incnt += (int)(G.inptr-G.inbuf) - (int)inbuf_offset;
  506.                 G.inptr = G.inbuf + (int)inbuf_offset;
  507.             }
  508.  
  509.             /* should be in proper position now, so check for sig */
  510.             if (readbuf(__G__ G.sig, 4) == 0) {  /* bad offset */
  511.                 Info(slide, 0x401, ((char *)slide, LoadFarString(OffsetMsg),
  512.                   filnum, "EOF", request));
  513.                 error_in_archive = PK_BADERR;
  514.                 continue;   /* but can still try next one */
  515.             }
  516.             if (strncmp(G.sig, G.local_hdr_sig, 4)) {
  517.                 Info(slide, 0x401, ((char *)slide, LoadFarString(OffsetMsg),
  518.                   filnum, LoadFarStringSmall(LocalHdrSig), request));
  519.                 /*
  520.                     GRRDUMP(G.sig, 4)
  521.                     GRRDUMP(G.local_hdr_sig, 4)
  522.                  */
  523.                 error_in_archive = PK_ERR;
  524.                 if ((filnum == 0 && G.extra_bytes != 0L) ||
  525.                     (G.extra_bytes == 0L && old_extra_bytes != 0L)) {
  526.                     Info(slide, 0x401, ((char *)slide,
  527.                       LoadFarString(AttemptRecompensate)));
  528.                     if (G.extra_bytes) {
  529.                         old_extra_bytes = G.extra_bytes;
  530.                         G.extra_bytes = 0L;
  531.                     } else
  532.                         G.extra_bytes = old_extra_bytes;  /* third attempt */
  533.                     ZLSEEK(G.pInfo->offset)
  534.                     if (readbuf(__G__ G.sig, 4) == 0) {  /* bad offset */
  535.                         Info(slide, 0x401, ((char *)slide,
  536.                           LoadFarString(OffsetMsg), filnum, "EOF", request));
  537.                         error_in_archive = PK_BADERR;
  538.                         continue;   /* but can still try next one */
  539.                     }
  540.                     if (strncmp(G.sig, G.local_hdr_sig, 4)) {
  541.                         Info(slide, 0x401, ((char *)slide,
  542.                           LoadFarString(OffsetMsg), filnum,
  543.                           LoadFarStringSmall(LocalHdrSig), request));
  544.                         error_in_archive = PK_BADERR;
  545.                         continue;
  546.                     }
  547.                 } else
  548.                     continue;  /* this one hosed; try next */
  549.             }
  550.             if ((error = process_local_file_hdr(__G)) != PK_COOL) {
  551.                 Info(slide, 0x421, ((char *)slide, LoadFarString(BadLocalHdr),
  552.                   filnum));
  553.                 error_in_archive = error;   /* only PK_EOF defined */
  554.                 continue;   /* can still try next one */
  555.             }
  556.             if ((error = do_string(__G__ G.lrec.filename_length, DS_FN)) !=
  557.                  PK_COOL)
  558.             {
  559.                 if (error > error_in_archive)
  560.                     error_in_archive = error;
  561.                 if (error > PK_WARN) {
  562.                     Info(slide, 0x401, ((char *)slide, LoadFarString(FilNamMsg),
  563.                       FnFilter1(G.filename), "local"));
  564.                     continue;   /* go on to next one */
  565.                 }
  566.             }
  567.             if (G.extra_field != (uch *)NULL) {
  568.                 free(G.extra_field);
  569.                 G.extra_field = (uch *)NULL;
  570.             }
  571.             if ((error =
  572.                  do_string(__G__ G.lrec.extra_field_length, EXTRA_FIELD)) != 0)
  573.             {
  574.                 if (error > error_in_archive)
  575.                     error_in_archive = error;
  576.                 if (error > PK_WARN) {
  577.                     Info(slide, 0x401, ((char *)slide,
  578.                       LoadFarString(ExtFieldMsg),
  579.                       FnFilter1(G.filename), "local"));
  580.                     continue;   /* go on */
  581.                 }
  582.             }
  583.  
  584.             /*
  585.              * just about to extract file:  if extracting to disk, check if
  586.              * already exists, and if so, take appropriate action according to
  587.              * fflag/uflag/overwrite_all/etc. (we couldn't do this in upper
  588.              * loop because we don't store the possibly renamed filename[] in
  589.              * info[])
  590.              */
  591. #ifdef DLL
  592.             if (!G.tflag && !G.cflag && !G.redirect_data)
  593. #else
  594.             if (!G.tflag && !G.cflag)
  595. #endif
  596.             {
  597.                 renamed = FALSE;   /* user hasn't renamed output file yet */
  598.  
  599. startover:
  600.                 query = FALSE;
  601. #ifdef MACOS
  602.                 G.macflag = (G.pInfo->hostnum == MAC_);
  603. #endif
  604.                 /* for files from DOS FAT, check for use of backslash instead
  605.                  *  of slash as directory separator (bug in some zipper(s); so
  606.                  *  far, not a problem in HPFS, NTFS or VFAT systems)
  607.                  */
  608. #ifndef SFX
  609.                 if (G.pInfo->hostnum == FS_FAT_ && !strchr(G.filename, '/')) {
  610.                     char *p=G.filename-1;
  611.  
  612.                     while (*++p) {
  613.                         if (*p == '\\') {
  614.                             if (!G.reported_backslash) {
  615.                                 Info(slide, 0x21, ((char *)slide,
  616.                                   LoadFarString(BackslashPathSep), G.zipfn));
  617.                                 G.reported_backslash = TRUE;
  618.                                 if (!error_in_archive)
  619.                                     error_in_archive = PK_WARN;
  620.                             }
  621.                             *p = '/';
  622.                         }
  623.                     }
  624.                 }
  625. #endif /* !SFX */
  626.  
  627.                 /* mapname can create dirs if not freshening or if renamed */
  628.                 if ((error = mapname(__G__ renamed)) > PK_WARN) {
  629.                     if (error == IZ_CREATED_DIR) {
  630. #ifdef UNIX
  631.                         if (do_dirtimes) {
  632.                             unsigned eb_izux_flg;
  633.  
  634.                             dirlist[num_dirs].fn =
  635.                               (char *)malloc(strlen(G.filename) + 1);
  636.                             if (dirlist[num_dirs].fn == (char *)NULL) {
  637.                                 Info(slide, 0x401, ((char *)slide,
  638.                                   LoadFarString(DirlistEntryNoMem)));
  639.                                 if (!error_in_archive)
  640.                                     error_in_archive = PK_WARN;
  641.                                 continue;
  642.                             }
  643.                             strcpy(dirlist[num_dirs].fn, G.filename);
  644.                             dirlist[num_dirs].perms = G.pInfo->file_attr;
  645.                             eb_izux_flg = G.extra_field ?
  646.                               ef_scan_for_izux(G.extra_field,
  647.                                                G.lrec.extra_field_length, 0,
  648.                                                &dirlist[num_dirs].u.t3,
  649.                                                dirlist[num_dirs].uidgid)
  650.                               : 0;
  651.                             if (eb_izux_flg & EB_UT_FL_MTIME) {
  652.                                 TTrace((stderr,
  653.                                   "\nextract:  Unix dir e.f. modtime = %ld\n",
  654.                                   dirlist[num_dirs].u.t3.mtime));
  655.                             } else {
  656.                                 dirlist[num_dirs].u.t3.mtime =
  657.                                   dos_to_unix_time(G.lrec.last_mod_file_date,
  658.                                                    G.lrec.last_mod_file_time);
  659.                             }
  660.                             if (eb_izux_flg & EB_UT_FL_ATIME) {
  661.                                 TTrace((stderr,
  662.                                   "\nextract:  Unix dir e.f. actime = %ld\n",
  663.                                   dirlist[num_dirs].u.t3.atime));
  664.                             } else {
  665.                                 dirlist[num_dirs].u.t3.atime =
  666.                                   dirlist[num_dirs].u.t3.mtime;
  667.                             }
  668.                             dirlist[num_dirs].have_uidgid =
  669.                                 (G.X_flag && (eb_izux_flg & EB_UX2_VALID));
  670.                             ++num_dirs;
  671.                         }
  672. #endif /* UNIX */
  673.                     } else if (error == IZ_VOL_LABEL) {
  674. #ifdef DOS_OS2_W32
  675.                         Info(slide, 0x401, ((char *)slide,
  676.                           LoadFarString(SkipVolumeLabel),
  677.                           FnFilter1(G.filename),
  678.                           G.volflag? "hard disk " : ""));
  679. #else
  680.                         Info(slide, 1, ((char *)slide,
  681.                           LoadFarString(SkipVolumeLabel),
  682.                           FnFilter1(G.filename), ""));
  683. #endif
  684.                     /*  if (!error_in_archive)
  685.                             error_in_archive = PK_WARN;  */
  686.                     } else if (error > PK_ERR  &&  error_in_archive < PK_ERR)
  687.                         error_in_archive = PK_ERR;
  688.                     Trace((stderr, "mapname(%s) returns error = %d\n",
  689.                       FnFilter1(G.filename), error));
  690.                     continue;   /* go on to next file */
  691.                 }
  692.  
  693. #ifdef QDOS
  694.                 QFilename(__G__ G.filename);
  695. #endif
  696.                 switch (check_for_newer(__G__ G.filename)) {
  697.                     case DOES_NOT_EXIST:
  698. #ifdef NOVELL_BUG_FAILSAFE
  699.                         G.dne = TRUE;   /* stat() says file DOES NOT EXIST */
  700. #endif
  701.                         /* if freshening, don't skip if just renamed */
  702.                         if (G.fflag && !renamed)
  703.                             continue;   /* freshen (no new files):  skip */
  704.                         break;
  705.                     case EXISTS_AND_OLDER:
  706.                         if (G.overwrite_none) {
  707. #ifdef WINDLL
  708.                             char szStr[PATH_MAX];
  709.  
  710.                             if ((!lpDCL->PromptToOverwrite) || (done_once)) {
  711.                                 sprintf(szStr,
  712.                                   "Target file exists.\nSkipping %s\n",
  713.                                   FnFilter1(G.filename));
  714.                                 win_fprintf(stdout, strlen(szStr), szStr);
  715.                             } else {
  716.                                 query = TRUE;
  717.                                 break;
  718.                             }
  719. #endif /* WINDLL */
  720.                             continue;   /* never overwrite:  skip file */
  721.                         }
  722. #ifdef UNIXBACKUP
  723.                         if (!G.overwrite_all && !G.B_flag)
  724. #else
  725.                         if (!G.overwrite_all)
  726. #endif
  727.                             query = TRUE;
  728.                         break;
  729.                     case EXISTS_AND_NEWER:             /* (or equal) */
  730.                         if (G.overwrite_none || (G.uflag && !renamed)) {
  731. #ifdef WINDLL
  732.                             char szStr[PATH_MAX];
  733.  
  734.                             if ((!lpDCL->PromptToOverwrite) || (done_once)) {
  735.                                 sprintf(szStr,
  736.                                   "Target file newer.\nSkipping %s\n",
  737.                                   FnFilter1(G.filename));
  738.                                 win_fprintf(stdout, strlen(szStr), szStr);
  739.                             } else {
  740.                                 query = TRUE;
  741.                                 break;
  742.                             }
  743. #endif /* WINDLL */
  744.                             continue;  /* skip if update/freshen & orig name */
  745.                         }
  746. #ifdef UNIXBACKUP
  747.                         if (!G.overwrite_all && !G.B_flag)
  748. #else
  749.                         if (!G.overwrite_all)
  750. #endif
  751.                             query = TRUE;
  752.                         break;
  753.                 }
  754.                 if (query) {
  755. #ifdef WINDLL
  756.                     switch (G.replace != NULL ?
  757.                             (*G.replace)(G.filename) : IDM_REPLACE_NONE) {
  758.                         case IDM_REPLACE_RENAME:
  759.                             _ISO_INTERN(G.filename);
  760.                             renamed = TRUE;
  761.                             goto startover;
  762.                         case IDM_REPLACE_YES:
  763.                             break;
  764.                         case IDM_REPLACE_ALL:
  765.                             G.overwrite_all = TRUE;
  766.                             G.overwrite_none = FALSE;  /* just to make sure */
  767.                             break;
  768.                         case IDM_REPLACE_NONE:
  769.                             G.overwrite_none = TRUE;
  770.                             G.overwrite_all = FALSE;  /* make sure */
  771.                             done_once = TRUE;
  772.                             /* FALL THROUGH, skip */
  773.                         case IDM_REPLACE_NO:
  774.                             {
  775.                                 char szStr[PATH_MAX];
  776.  
  777.                                 sprintf(szStr,
  778.                                   "Target file newer.\nSkipping %s\n",
  779.                                   FnFilter1(G.filename));
  780.                                 win_fprintf(stdout, strlen(szStr), szStr);
  781.                             }
  782.                             continue;
  783.                     }
  784. #else /* !WINDLL */
  785. reprompt:
  786.                     Info(slide, 0x81, ((char *)slide,
  787.                       LoadFarString(ReplaceQuery),
  788.                       FnFilter1(G.filename)));
  789.                     if (fgets(G.answerbuf, 9, stdin) == (char *)NULL) {
  790.                         Info(slide, 1, ((char *)slide,
  791.                           LoadFarString(AssumeNone)));
  792.                         *G.answerbuf = 'N';
  793.                         if (!error_in_archive)
  794.                             error_in_archive = 1;  /* not extracted:  warning */
  795.                     }
  796.                     switch (*G.answerbuf) {
  797.                         case 'A':   /* dangerous option:  force caps */
  798.                             G.overwrite_all = TRUE;
  799.                             G.overwrite_none = FALSE;  /* just to make sure */
  800.                             break;
  801.                         case 'r':
  802.                         case 'R':
  803.                             do {
  804.                                 Info(slide, 0x81, ((char *)slide,
  805.                                   LoadFarString(NewNameQuery)));
  806.                                 fgets(G.filename, FILNAMSIZ, stdin);
  807.                                 /* usually get \n here:  better check for it */
  808.                                 len = strlen(G.filename);
  809.                                 if (G.filename[len-1] == '\n')
  810.                                     G.filename[--len] = 0;
  811.                             } while (len == 0);
  812. #ifdef WIN32  /* WIN32 fgets( ... , stdin) returns OEM coded strings */
  813.                             _OEM_INTERN(G.filename);
  814. #endif
  815.                             renamed = TRUE;
  816.                             goto startover;   /* sorry for a goto */
  817.                         case 'y':
  818.                         case 'Y':
  819.                             break;
  820.                         case 'N':
  821.                             G.overwrite_none = TRUE;
  822.                             G.overwrite_all = FALSE;  /* make sure */
  823.                             /* FALL THROUGH, skip */
  824.                         case 'n':
  825.                             continue;   /* skip file */
  826.                         default:
  827.                             Info(slide, 1, ((char *)slide,
  828.                               LoadFarString(InvalidResponse), *G.answerbuf));
  829.                             goto reprompt;   /* yet another goto? */
  830.                     } /* end switch (*answerbuf) */
  831. #endif /* ?WINDLL */
  832.                 } /* end if (query) */
  833.             } /* end if (extracting to disk) */
  834.  
  835. #if CRYPT
  836.             if (G.pInfo->encrypted && (error = decrypt(__G)) != PK_COOL) {
  837.                 if (error == PK_WARN) {
  838.                     if (!((G.tflag && G.qflag) || (!G.tflag && !QCOND2)))
  839.                         Info(slide, 0x401, ((char *)slide,
  840.                           LoadFarString(SkipIncorrectPasswd),
  841.                           FnFilter1(G.filename)));
  842.                     ++num_bad_pwd;
  843.                 } else {  /* (error > PK_WARN) */
  844.                     if (error > error_in_archive)
  845.                         error_in_archive = error;
  846.                     Info(slide, 0x401, ((char *)slide,
  847.                       LoadFarString(SkipCantGetPasswd),
  848.                       FnFilter1(G.filename)));
  849.                 }
  850.                 continue;   /* go on to next file */
  851.             }
  852. #endif /* CRYPT */
  853. #ifdef WINDLL         /* GRR:  this should be made generic:  ifdef SOUND? */
  854.             /* play sound during extraction or test, if requested */
  855.             if (G.sound != NULL)
  856.                 (*G.sound)();
  857. #endif
  858. #ifdef AMIGA
  859.             G.filenote_slot = i;
  860. #endif
  861.             G.disk_full = 0;
  862.             if ((error = extract_or_test_member(__G)) != PK_COOL) {
  863.                 if (error > error_in_archive)
  864.                     error_in_archive = error;       /* ...and keep going */
  865.                 if (G.disk_full > 1) {
  866.                     if (fn_matched)
  867.                         free((zvoid *)fn_matched);
  868.                     if (xn_matched)
  869.                         free((zvoid *)xn_matched);
  870.                     return error_in_archive;        /* (unless disk full) */
  871.                 }
  872.             }
  873.         } /* end for-loop (i:  files in current block) */
  874.  
  875.  
  876.         /*
  877.          * Jump back to where we were in the central directory, then go and do
  878.          * the next batch of files.
  879.          */
  880.  
  881. #ifdef USE_STRM_INPUT
  882.         fseek((FILE *)G.zipfd, (LONGINT)cd_bufstart, SEEK_SET);
  883.         G.cur_zipfile_bufstart = ftell((FILE *)G.zipfd);
  884. #else /* !USE_STRM_INPUT */
  885.         G.cur_zipfile_bufstart = lseek(G.zipfd,(LONGINT)cd_bufstart,SEEK_SET);
  886. #endif /* ?USE_STRM_INPUT */
  887.         read(G.zipfd, (char *)G.inbuf, INBUFSIZ);  /* been here before... */
  888.         G.inptr = cd_inptr;
  889.         G.incnt = cd_incnt;
  890.         ++blknum;
  891.  
  892. #ifdef TEST
  893.         printf("\ncd_bufstart = %ld (%.8lXh)\n", cd_bufstart, cd_bufstart);
  894.         printf("cur_zipfile_bufstart = %ld (%.8lXh)\n", cur_zipfile_bufstart,
  895.           cur_zipfile_bufstart);
  896.         printf("inptr-inbuf = %d\n", G.inptr-G.inbuf);
  897.         printf("incnt = %d\n\n", G.incnt);
  898. #endif
  899.  
  900.     } /* end while-loop (blocks of files in central directory) */
  901.  
  902. /*---------------------------------------------------------------------------
  903.     Go back through saved list of directories, sort and set times/perms/UIDs
  904.     and GIDs from the deepest level on up.
  905.   ---------------------------------------------------------------------------*/
  906.  
  907. #ifdef UNIX
  908.     if (num_dirs > 0) {
  909.         sorted_dirlist = (dirtime **)malloc(num_dirs*sizeof(dirtime *));
  910.         if (sorted_dirlist == (dirtime **)NULL) {
  911.             Info(slide, 0x401, ((char *)slide,
  912.               LoadFarString(DirlistSortNoMem)));
  913.         } else {
  914.             if (num_dirs == 1)
  915.                 sorted_dirlist[0] = dirlist;
  916.             else {
  917.                 for (i = 0;  i < num_dirs;  ++i)
  918.                     sorted_dirlist[i] = &dirlist[i];
  919.                 qsort((char *)sorted_dirlist, num_dirs, sizeof(dirtime *),
  920.                   dircomp);
  921.             }
  922.  
  923.             Trace((stderr, "setting Unix dir times/perms/UIDs/GIDs\n"));
  924.             for (i = 0;  i < num_dirs;  ++i) {
  925.                 dirtime *d = sorted_dirlist[i];
  926.  
  927.                 Trace((stderr, "dir = %s\n", d->fn));
  928.                 if (d->have_uidgid &&
  929.                     chown(d->fn, (uid_t)d->uidgid[0], (gid_t)d->uidgid[1]))
  930.                 {
  931.                     Info(slide, 0x201, ((char *)slide,
  932.                       LoadFarString(DirlistUidGidFailed),
  933.                       d->uidgid[0], d->uidgid[1], d->fn));
  934.                     if (!error_in_archive)
  935.                         error_in_archive = PK_WARN;
  936.                 }
  937.                 if (utime(d->fn, &d->u.t2)) {
  938.                     Info(slide, 0x201, ((char *)slide,
  939.                       LoadFarString(DirlistUtimeFailed), d->fn));
  940.                     if (!error_in_archive)
  941.                         error_in_archive = PK_WARN;
  942.                 }
  943. #ifndef NO_CHMOD
  944.                 if (chmod(d->fn, 0xffff & d->perms)) {
  945.                     Info(slide, 0x201, ((char *)slide,
  946.                       LoadFarString(DirlistChmodFailed), d->fn));
  947.                     /* perror("chmod (file attributes) error"); */
  948.                     if (!error_in_archive)
  949.                         error_in_archive = PK_WARN;
  950.                 }
  951. #endif /* !NO_CHMOD */
  952.                 free(d->fn);
  953.             }
  954.             free(sorted_dirlist);
  955.         }
  956.         free(dirlist);
  957.     }
  958. #endif /* UNIX */
  959.  
  960. #if (defined(WIN32) && defined(NTSD_EAS))
  961.     process_defer_NT(__G);  /* process any deferred items for this .zip file */
  962. #endif
  963.  
  964. /*---------------------------------------------------------------------------
  965.     Check for unmatched filespecs on command line and print warning if any
  966.     found.  Free allocated memory.
  967.   ---------------------------------------------------------------------------*/
  968.  
  969.     if (fn_matched) {
  970.         for (i = 0;  i < G.filespecs;  ++i)
  971.             if (!fn_matched[i]) {
  972. #ifdef DLL
  973.                 if (!G.redirect_data && !G.redirect_text)
  974.                     Info(slide, 0x401, ((char *)slide,
  975.                       LoadFarString(FilenameNotMatched), G.pfnames[i]));
  976.                 else
  977.                     setFileNotFound(__G);
  978. #else
  979.                 Info(slide, 1, ((char *)slide,
  980.                   LoadFarString(FilenameNotMatched), G.pfnames[i]));
  981. #endif
  982.                 if (error_in_archive <= PK_WARN)
  983.                     error_in_archive = PK_FIND;   /* some files not found */
  984.             }
  985.         free((zvoid *)fn_matched);
  986.     }
  987.     if (xn_matched) {
  988.         for (i = 0;  i < G.xfilespecs;  ++i)
  989.             if (!xn_matched[i])
  990.                 Info(slide, 0x401, ((char *)slide,
  991.                   LoadFarString(ExclFilenameNotMatched), G.pxnames[i]));
  992.         free((zvoid *)xn_matched);
  993.     }
  994.  
  995. /*---------------------------------------------------------------------------
  996.     Double-check that we're back at the end-of-central-directory record, and
  997.     print quick summary of results, if we were just testing the archive.  We
  998.     send the summary to stdout so that people doing the testing in the back-
  999.     ground and redirecting to a file can just do a "tail" on the output file.
  1000.   ---------------------------------------------------------------------------*/
  1001.  
  1002. #ifndef SFX
  1003.     if (readbuf(__G__ G.sig, 4) == 0)
  1004.         error_in_archive = PK_EOF;
  1005.     if (strncmp(G.sig, G.end_central_sig, 4)) {         /* just to make sure */
  1006.         Info(slide, 0x401, ((char *)slide, LoadFarString(EndSigMsg)));
  1007.         Info(slide, 0x401, ((char *)slide, LoadFarString(ReportMsg)));
  1008.         if (!error_in_archive)       /* don't overwrite stronger error */
  1009.             error_in_archive = PK_WARN;
  1010.     }
  1011. #endif /* !SFX */
  1012.     ++filnum;  /* initialized to -1, so now zero if no files found */
  1013.     if (G.tflag) {
  1014.         int num=filnum - num_bad_pwd;
  1015.  
  1016.         if (G.qflag < 2) {         /* GRR 930710:  was (G.qflag == 1) */
  1017.             if (error_in_archive)
  1018.                 Info(slide, 0, ((char *)slide, LoadFarString(ErrorInArchive),
  1019.                   (error_in_archive == 1)? "warning-" : "", G.zipfn));
  1020.             else if (num == 0)
  1021.                 Info(slide, 0, ((char *)slide, LoadFarString(ZeroFilesTested),
  1022.                   G.zipfn));
  1023.             else if (G.process_all_files && (num_skipped+num_bad_pwd == 0))
  1024.                 Info(slide, 0, ((char *)slide, LoadFarString(NoErrInCompData),
  1025.                   G.zipfn));
  1026.             else
  1027.                 Info(slide, 0, ((char *)slide, LoadFarString(NoErrInTestedFiles)
  1028.                   , G.zipfn, num, (num==1)? "":"s"));
  1029.             if (num_skipped > 0)
  1030.                 Info(slide, 0, ((char *)slide, LoadFarString(FilesSkipped),
  1031.                   num_skipped, (num_skipped==1)? "":"s"));
  1032. #if CRYPT
  1033.             if (num_bad_pwd > 0)
  1034.                 Info(slide, 0, ((char *)slide, LoadFarString(FilesSkipBadPasswd)
  1035.                   , num_bad_pwd, (num_bad_pwd==1)? "":"s"));
  1036. #endif /* CRYPT */
  1037.         } else if ((G.qflag == 0) && !error_in_archive && (num == 0))
  1038.             Info(slide, 0, ((char *)slide, LoadFarString(ZeroFilesTested),
  1039.               G.zipfn));
  1040.     }
  1041.  
  1042.     /* give warning if files not tested or extracted (first condition can still
  1043.      * happen if zipfile is empty and no files specified on command line) */
  1044.  
  1045.     if ((filnum == 0) && error_in_archive <= PK_WARN) {
  1046.         if (num_skipped > 0)
  1047.             error_in_archive = IZ_UNSUP;  /* unsup. compression/encryption */
  1048.         else
  1049.             error_in_archive = PK_FIND;   /* no files found at all */
  1050.     }
  1051. #if CRYPT
  1052.     else if ((filnum == num_bad_pwd) && error_in_archive <= PK_WARN)
  1053.         error_in_archive = IZ_BADPWD;     /* bad passwd => all files skipped */
  1054. #endif
  1055.     else if ((num_skipped > 0) && !error_in_archive)
  1056.         error_in_archive = PK_WARN;
  1057. #if CRYPT
  1058.     else if ((num_bad_pwd > 0) && !error_in_archive)
  1059.         error_in_archive = PK_WARN;
  1060. #endif
  1061.  
  1062.     return error_in_archive;
  1063.  
  1064. } /* end function extract_or_test_files() */
  1065.  
  1066.  
  1067.  
  1068.  
  1069.  
  1070. /***************************/
  1071. /*  Function store_info()  */
  1072. /***************************/
  1073.  
  1074. static int store_info(__G)   /* return 0 if skipping, 1 if OK */
  1075.     __GDEF
  1076. {
  1077. #ifdef SFX
  1078. #  define UNKN_COMPR \
  1079.    (G.crec.compression_method!=STORED && G.crec.compression_method!=DEFLATED)
  1080. #else
  1081. #  ifdef COPYRIGHT_CLEAN  /* no reduced files */
  1082. #    define UNKN_RED (G.crec.compression_method >= REDUCED1 && \
  1083.                       G.crec.compression_method <= REDUCED4)
  1084. #  else
  1085. #    define UNKN_RED  FALSE  /* reducing not unknown */
  1086. #  endif
  1087. #  ifdef LZW_CLEAN  /* no shrunk files */
  1088. #    define UNKN_SHR (G.crec.compression_method == SHRUNK)
  1089. #  else
  1090. #    define UNKN_SHR  FALSE  /* unshrinking not unknown */
  1091. #  endif
  1092. #  define UNKN_COMPR (UNKN_RED || UNKN_SHR || \
  1093.    G.crec.compression_method==TOKENIZED || G.crec.compression_method>DEFLATED)
  1094. #endif
  1095.  
  1096. /*---------------------------------------------------------------------------
  1097.     Check central directory info for version/compatibility requirements.
  1098.   ---------------------------------------------------------------------------*/
  1099.  
  1100.     G.pInfo->encrypted = G.crec.general_purpose_bit_flag & 1;   /* bit field */
  1101.     G.pInfo->ExtLocHdr = (G.crec.general_purpose_bit_flag & 8) == 8;  /* bit */
  1102.     G.pInfo->textfile = G.crec.internal_file_attributes & 1;    /* bit field */
  1103.     G.pInfo->crc = G.crec.crc32;
  1104.     G.pInfo->compr_size = G.crec.csize;
  1105.  
  1106.     switch (G.aflag) {
  1107.         case 0:
  1108.             G.pInfo->textmode = FALSE;   /* bit field */
  1109.             break;
  1110.         case 1:
  1111.             G.pInfo->textmode = G.pInfo->textfile;   /* auto-convert mode */
  1112.             break;
  1113.         default:  /* case 2: */
  1114.             G.pInfo->textmode = TRUE;
  1115.             break;
  1116.     }
  1117.  
  1118.     if (G.crec.version_needed_to_extract[1] == VMS_) {
  1119.         if (G.crec.version_needed_to_extract[0] > VMS_UNZIP_VERSION) {
  1120.             if (!((G.tflag && G.qflag) || (!G.tflag && !QCOND2)))
  1121.                 Info(slide, 0x401, ((char *)slide, LoadFarString(VersionMsg),
  1122.                   FnFilter1(G.filename), "VMS",
  1123.                   G.crec.version_needed_to_extract[0] / 10,
  1124.                   G.crec.version_needed_to_extract[0] % 10,
  1125.                   VMS_UNZIP_VERSION / 10, VMS_UNZIP_VERSION % 10));
  1126.             return 0;
  1127.         }
  1128. #ifndef VMS   /* won't be able to use extra field, but still have data */
  1129.         else if (!G.tflag && !G.overwrite_all) {   /* if -o, extract anyway */
  1130.             Info(slide, 0x481, ((char *)slide, LoadFarString(VMSFormatQuery),
  1131.               FnFilter1(G.filename)));
  1132.             fgets(G.answerbuf, 9, stdin);
  1133.             if ((*G.answerbuf != 'y') && (*G.answerbuf != 'Y'))
  1134.                 return 0;
  1135.         }
  1136. #endif /* !VMS */
  1137.     /* usual file type:  don't need VMS to extract */
  1138.     } else if (G.crec.version_needed_to_extract[0] > UNZIP_VERSION) {
  1139.         if (!((G.tflag && G.qflag) || (!G.tflag && !QCOND2)))
  1140.             Info(slide, 0x401, ((char *)slide, LoadFarString(VersionMsg),
  1141.               FnFilter1(G.filename), "PK",
  1142.               G.crec.version_needed_to_extract[0] / 10,
  1143.               G.crec.version_needed_to_extract[0] % 10,
  1144.               UNZIP_VERSION / 10, UNZIP_VERSION % 10));
  1145.         return 0;
  1146.     }
  1147.  
  1148.     if UNKN_COMPR {
  1149.         if (!((G.tflag && G.qflag) || (!G.tflag && !QCOND2)))
  1150. #ifndef SFX
  1151.             if (G.crec.compression_method < NUM_METHODS)
  1152.                 Info(slide, 0x401, ((char *)slide, LoadFarString(ComprMsgName),
  1153.                   FnFilter1(G.filename),
  1154.                   LoadFarStringSmall(ComprNames[G.crec.compression_method])));
  1155.             else
  1156. #endif
  1157.                 Info(slide, 0x401, ((char *)slide, LoadFarString(ComprMsgNum),
  1158.                   FnFilter1(G.filename),
  1159.                   G.crec.compression_method));
  1160.         return 0;
  1161.     }
  1162. #if (!CRYPT)
  1163.     if (G.pInfo->encrypted) {
  1164.         if (!((G.tflag && G.qflag) || (!G.tflag && !QCOND2)))
  1165.             Info(slide, 0x401, ((char *)slide, LoadFarString(SkipEncrypted),
  1166.               FnFilter1(G.filename)));
  1167.         return 0;
  1168.     }
  1169. #endif /* !CRYPT */
  1170.  
  1171.     /* map whatever file attributes we have into the local format */
  1172.     mapattr(__G);   /* GRR:  worry about return value later */
  1173.  
  1174.     G.pInfo->offset = (long)G.crec.relative_offset_local_header;
  1175.     return 1;
  1176.  
  1177. } /* end function store_info() */
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183. /***************************************/
  1184. /*  Function extract_or_test_member()  */
  1185. /***************************************/
  1186.  
  1187. static int extract_or_test_member(__G)    /* return PK-type error code */
  1188.      __GDEF
  1189. {
  1190.     char *nul="[empty] ", *txt="[text]  ", *bin="[binary]";
  1191. #ifdef CMS_MVS
  1192.     char *ebc="[ebcdic]";
  1193. #endif
  1194.     register int b;
  1195.     int r, error=PK_COOL;
  1196.     ulg wsize;
  1197.  
  1198.  
  1199. /*---------------------------------------------------------------------------
  1200.     Initialize variables, buffers, etc.
  1201.   ---------------------------------------------------------------------------*/
  1202.  
  1203.     G.bits_left = 0;
  1204.     G.bitbuf = 0L;       /* unreduce and unshrink only */
  1205.     G.zipeof = 0;
  1206.     G.newfile = TRUE;
  1207.     G.crc32val = CRCVAL_INITIAL;
  1208.  
  1209. #ifdef SYMLINKS
  1210.     /* if file came from Unix and is a symbolic link and we are extracting
  1211.      * to disk, prepare to restore the link */
  1212.     if (S_ISLNK(G.pInfo->file_attr) &&
  1213.         (G.pInfo->hostnum == UNIX_ || G.pInfo->hostnum == ATARI_ ||
  1214.          G.pInfo->hostnum == BEOS_) &&
  1215.         !G.tflag && !G.cflag && (G.lrec.ucsize > 0))
  1216.         G.symlnk = TRUE;
  1217.     else
  1218.         G.symlnk = FALSE;
  1219. #endif /* SYMLINKS */
  1220.  
  1221.     if (G.tflag) {
  1222.         if (!G.qflag)
  1223.             Info(slide, 0, ((char *)slide, LoadFarString(ExtractMsg), "test",
  1224.               FnFilter1(G.filename), "", ""));
  1225.     } else {
  1226. #ifdef DLL
  1227.         if (G.cflag && !G.redirect_data)
  1228. #else
  1229.         if (G.cflag)
  1230. #endif
  1231.         {
  1232. #if (defined(OS2) && defined(__IBMC__) && (__IBMC__ >= 200))
  1233.             G.outfile = freopen("", "wb", stdout);   /* VAC++ ignores setmode */
  1234. #else
  1235.             G.outfile = stdout;
  1236. #endif
  1237. #ifdef DOS_H68_OS2_W32
  1238. #ifdef __HIGHC__
  1239.             setmode(G.outfile, _BINARY);
  1240. #else
  1241.             setmode(fileno(G.outfile), O_BINARY);
  1242. #endif
  1243. #           define NEWLINE "\r\n"
  1244. #else /* !DOS_H68_OS2_W32 */
  1245. #           define NEWLINE "\n"
  1246. #endif /* ?DOS_H68_OS2_W32 */
  1247. #ifdef VMS
  1248.             if (open_outfile(__G))   /* VMS:  required even for stdout! */
  1249.                 return PK_DISK;
  1250. #endif
  1251.         } else if (open_outfile(__G))
  1252.             return PK_DISK;
  1253.     }
  1254.  
  1255. /*---------------------------------------------------------------------------
  1256.     Unpack the file.
  1257.   ---------------------------------------------------------------------------*/
  1258.  
  1259.     defer_leftover_input(__G);    /* so NEXTBYTE bounds check will work */
  1260.     switch (G.lrec.compression_method) {
  1261.         case STORED:
  1262.             if (!G.tflag && QCOND2) {
  1263. #ifdef SYMLINKS
  1264.                 if (G.symlnk)   /* can also be deflated, but rarer... */
  1265.                     Info(slide, 0, ((char *)slide, LoadFarString(ExtractMsg),
  1266.                       "link", FnFilter1(G.filename), "", ""));
  1267.                 else
  1268. #endif /* SYMLINKS */
  1269.                 Info(slide, 0, ((char *)slide, LoadFarString(ExtractMsg),
  1270.                   "extract", FnFilter1(G.filename),
  1271.                   (G.aflag != 1 /* && G.pInfo->textfile==G.pInfo->textmode */)?
  1272.                   "" : (G.lrec.ucsize == 0L? nul : (G.pInfo->textfile? txt :
  1273.                   bin)), G.cflag? NEWLINE : ""));
  1274.             }
  1275. #ifdef DLL
  1276.             if (G.redirect_data)
  1277.                 wsize = G.redirect_size+1, G.outptr = G.redirect_buffer;
  1278.             else
  1279. #endif
  1280.                 wsize = WSIZE, G.outptr = slide;
  1281.             G.outcnt = 0L;
  1282.             while ((b = NEXTBYTE) != EOF && !G.disk_full) {
  1283.                 *G.outptr++ = (uch)b;
  1284.                 if (++G.outcnt == wsize) {
  1285.                     flush(__G__ slide, G.outcnt, 0);
  1286.                     G.outptr = slide;
  1287.                     G.outcnt = 0L;
  1288.                 }
  1289.             }
  1290. #ifdef DLL
  1291.             if (G.outcnt && !G.redirect_data)
  1292. #else
  1293.             if (G.outcnt)          /* flush final (partial) buffer */
  1294. #endif
  1295.                 flush(__G__ slide, G.outcnt, 0);
  1296.             break;
  1297.  
  1298. #ifndef SFX
  1299. #ifndef LZW_CLEAN
  1300.         case SHRUNK:
  1301.             if (!G.tflag && QCOND2) {
  1302.                 Info(slide, 0, ((char *)slide, LoadFarString(ExtractMsg),
  1303.                   LoadFarStringSmall(Unshrink), FnFilter1(G.filename),
  1304.                   (G.aflag != 1 /* && G.pInfo->textfile==G.pInfo->textmode */)?
  1305.                   "" : (G.pInfo->textfile? txt : bin), G.cflag? NEWLINE : ""));
  1306.             }
  1307.             if ((r = unshrink(__G)) != PK_COOL) {
  1308.                 if ((G.tflag && G.qflag) || (!G.tflag && !QCOND2))
  1309.                     Info(slide, 0x401, ((char *)slide,
  1310.                       LoadFarStringSmall(ErrUnzipFile),
  1311.                       LoadFarString(NotEnoughMem),
  1312.                       LoadFarStringSmall2(Unshrink),
  1313.                       FnFilter1(G.filename)));
  1314.                 else
  1315.                     Info(slide, 0x401, ((char *)slide,
  1316.                       LoadFarStringSmall(ErrUnzipNoFile),
  1317.                       LoadFarString(NotEnoughMem),
  1318.                       LoadFarStringSmall2(Unshrink)));
  1319.                 error = r;
  1320.             }
  1321.             break;
  1322. #endif /* !LZW_CLEAN */
  1323.  
  1324. #ifndef COPYRIGHT_CLEAN
  1325.         case REDUCED1:
  1326.         case REDUCED2:
  1327.         case REDUCED3:
  1328.         case REDUCED4:
  1329.             if (!G.tflag && QCOND2) {
  1330.                 Info(slide, 0, ((char *)slide, LoadFarString(ExtractMsg),
  1331.                   "unreduc", FnFilter1(G.filename),
  1332.                   (G.aflag != 1 /* && G.pInfo->textfile==G.pInfo->textmode */)?
  1333.                   "" : (G.pInfo->textfile? txt : bin), G.cflag? NEWLINE : ""));
  1334.             }
  1335.             unreduce(__G);
  1336.             break;
  1337. #endif /* !COPYRIGHT_CLEAN */
  1338.  
  1339.         case IMPLODED:
  1340.             if (!G.tflag && QCOND2) {
  1341.                 Info(slide, 0, ((char *)slide, LoadFarString(ExtractMsg),
  1342.                   "explod", FnFilter1(G.filename),
  1343.                   (G.aflag != 1 /* && G.pInfo->textfile==G.pInfo->textmode */)?
  1344.                   "" : (G.pInfo->textfile? txt : bin), G.cflag? NEWLINE : ""));
  1345.             }
  1346.             if (((r = explode(__G)) != 0) && (r != 5)) { /* treat 5 specially */
  1347.                 if ((G.tflag && G.qflag) || (!G.tflag && !QCOND2))
  1348.                     Info(slide, 0x401, ((char *)slide,
  1349.                       LoadFarStringSmall(ErrUnzipFile), r == 3?
  1350.                       LoadFarString(NotEnoughMem) :
  1351.                       LoadFarString(InvalidComprData),
  1352.                       LoadFarStringSmall2(Explode),
  1353.                       FnFilter1(G.filename)));
  1354.                 else
  1355.                     Info(slide, 0x401, ((char *)slide,
  1356.                       LoadFarStringSmall(ErrUnzipNoFile), r == 3?
  1357.                       LoadFarString(NotEnoughMem) :
  1358.                       LoadFarString(InvalidComprData),
  1359.                       LoadFarStringSmall2(Explode)));
  1360.                 error = (r == 3)? PK_MEM3 : PK_ERR;
  1361.             }
  1362.             if (r == 5) {
  1363.                 int warning = ((ulg)G.used_csize <= G.lrec.csize);
  1364.  
  1365.                 if ((G.tflag && G.qflag) || (!G.tflag && !QCOND2))
  1366.                     Info(slide, 0x401, ((char *)slide, LoadFarString(LengthMsg),
  1367.                       "", warning?  "warning" : "error", G.used_csize,
  1368.                       G.lrec.ucsize, warning?  "  " : "", G.lrec.csize,
  1369.                       " [", FnFilter1(G.filename), "]"));
  1370.                 else
  1371.                     Info(slide, 0x401, ((char *)slide, LoadFarString(LengthMsg),
  1372.                       "\n", warning? "warning" : "error", G.used_csize,
  1373.                       G.lrec.ucsize, warning? "  ":"", G.lrec.csize,
  1374.                       "", "", "."));
  1375.                 error = warning? PK_WARN : PK_ERR;
  1376.             }
  1377.             break;
  1378. #endif /* !SFX */
  1379.  
  1380.         case DEFLATED:
  1381.             if (!G.tflag && QCOND2) {
  1382.                 Info(slide, 0, ((char *)slide, LoadFarString(ExtractMsg),
  1383.                   "inflat", FnFilter1(G.filename),
  1384.                   (G.aflag != 1 /* && G.pInfo->textfile==G.pInfo->textmode */)?
  1385.                   "" : (G.pInfo->textfile? txt : bin), G.cflag? NEWLINE : ""));
  1386.             }
  1387. #ifndef USE_ZLIB  /* zlib's function is called inflate(), too */
  1388. #  define UZinflate inflate
  1389. #endif
  1390.             if ((r = UZinflate(__G)) != 0) {
  1391.                 if ((G.tflag && G.qflag) || (!G.tflag && !QCOND2))
  1392.                     Info(slide, 0x401, ((char *)slide,
  1393.                       LoadFarStringSmall(ErrUnzipFile), r == 3?
  1394.                       LoadFarString(NotEnoughMem) :
  1395.                       LoadFarString(InvalidComprData),
  1396.                       LoadFarStringSmall2(Inflate),
  1397.                       FnFilter1(G.filename)));
  1398.                 else
  1399.                     Info(slide, 0x401, ((char *)slide,
  1400.                       LoadFarStringSmall(ErrUnzipNoFile), r == 3?
  1401.                       LoadFarString(NotEnoughMem) :
  1402.                       LoadFarString(InvalidComprData),
  1403.                       LoadFarStringSmall2(Inflate)));
  1404.                 error = (r == 3)? PK_MEM3 : PK_ERR;
  1405.             }
  1406.             break;
  1407.  
  1408.         default:   /* should never get to this point */
  1409.             Info(slide, 0x401, ((char *)slide,
  1410.               LoadFarString(FileUnknownCompMethod), FnFilter1(G.filename)));
  1411.             /* close and delete file before return? */
  1412.             undefer_input(__G);
  1413.             return PK_WARN;
  1414.  
  1415.     } /* end switch (compression method) */
  1416.  
  1417. /*---------------------------------------------------------------------------
  1418.     Close the file and set its date and time (not necessarily in that order),
  1419.     and make sure the CRC checked out OK.  Logical-AND the CRC for 64-bit
  1420.     machines (redundant on 32-bit machines).
  1421.   ---------------------------------------------------------------------------*/
  1422.  
  1423. #ifdef VMS                  /* VMS:  required even for stdout! (final flush) */
  1424.     if (!G.tflag)           /* don't close NULL file */
  1425.         close_outfile(__G);
  1426. #else
  1427. #ifdef DLL
  1428.     if (!G.tflag && (!G.cflag || G.redirect_data))
  1429.         if (G.redirect_data)
  1430.             FINISH_REDIRECT();
  1431.         else
  1432.             close_outfile(__G);
  1433. #else
  1434.     if (!G.tflag && !G.cflag)   /* don't close NULL file or stdout */
  1435.         close_outfile(__G);
  1436. #endif
  1437. #endif /* VMS */
  1438.  
  1439.             /* GRR: CONVERT close_outfile() TO NON-VOID:  CHECK FOR ERRORS! */
  1440.  
  1441.  
  1442.     if (G.disk_full) {            /* set by flush() */
  1443.         if (G.disk_full > 1) {
  1444. #if (defined(DELETE_IF_FULL) && defined(HAVE_UNLINK))
  1445.             /* delete the incomplete file if we can */
  1446.             if (unlink(G.filename) != 0)
  1447.                 Trace((stderr, "extract.c:  could not delete %s\n",
  1448.                   FnFilter1(G.filename)));
  1449. #else
  1450.             /* warn user about the incomplete file */
  1451.             Info(slide, 0x421, ((char *)slide, LoadFarString(FileTruncated),
  1452.               FnFilter1(G.filename)));
  1453. #endif
  1454.             error = PK_DISK;
  1455.         } else {
  1456.             error = PK_WARN;
  1457.         }
  1458.     }
  1459.  
  1460.     if (error > PK_WARN) {/* don't print redundant CRC error if error already */
  1461.         undefer_input(__G);
  1462.         return error;
  1463.     }
  1464.     if (G.crc32val != G.lrec.crc32) {
  1465.         /* if quiet enough, we haven't output the filename yet:  do it */
  1466.         if ((G.tflag && G.qflag) || (!G.tflag && !QCOND2))
  1467.             Info(slide, 0x401, ((char *)slide, "%-22s ",
  1468.               FnFilter1(G.filename)));
  1469.         Info(slide, 0x401, ((char *)slide, LoadFarString(BadCRC), G.crc32val,
  1470.           G.lrec.crc32));
  1471. #if CRYPT
  1472.         if (G.pInfo->encrypted)
  1473.             Info(slide, 0x401, ((char *)slide, LoadFarString(MaybeBadPasswd)));
  1474. #endif
  1475.         error = PK_ERR;
  1476.     } else if (G.tflag) {
  1477. #ifndef SFX
  1478.         if (G.extra_field) {
  1479.             if ((r = TestExtraField(__G__ G.extra_field,
  1480.                                     G.lrec.extra_field_length)) > error)
  1481.                 error = r;
  1482.         } else
  1483. #endif /* !SFX */
  1484.         if (!G.qflag)
  1485.             Info(slide, 0, ((char *)slide, " OK\n"));
  1486.     } else {
  1487.         if (QCOND2 && !error)   /* GRR:  is stdout reset to text mode yet? */
  1488.             Info(slide, 0, ((char *)slide, "\n"));
  1489.     }
  1490.  
  1491.     undefer_input(__G);
  1492.     return error;
  1493.  
  1494. } /* end function extract_or_test_member() */
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500. #ifndef SFX
  1501.  
  1502. /*******************************/
  1503. /*  Function TestExtraField()  */
  1504. /*******************************/
  1505.  
  1506. static int TestExtraField(__G__ ef, ef_len)
  1507.     __GDEF
  1508.     uch *ef;
  1509.     unsigned ef_len;
  1510. {
  1511.     ush ebID;
  1512.     unsigned ebLen;
  1513.     int r;
  1514.  
  1515.     /* we know the regular compressed file data tested out OK, or else we
  1516.      * wouldn't be here ==> print filename if any extra-field errors found
  1517.      */
  1518.     while (ef_len >= EB_HEADSIZE) {
  1519.         ebID = makeword(ef);
  1520.         ebLen = (unsigned)makeword(ef+EB_LEN);
  1521.  
  1522.         if (ebLen > (ef_len - EB_HEADSIZE)) {
  1523.            /* Discovered some extra field inconsistency! */
  1524.             if (G.qflag)
  1525.                 Info(slide, 1, ((char *)slide, "%-22s ",
  1526.                   FnFilter1(G.filename)));
  1527.             Info(slide, 1, ((char *)slide, LoadFarString(InconsistEFlength),
  1528.               ebLen, (ef_len - EB_HEADSIZE)));
  1529.             return PK_ERR;
  1530.         }
  1531.  
  1532.         switch (ebID) {
  1533.             case EF_OS2:
  1534.             case EF_ACL:
  1535.                 if ((r = test_OS2(__G__ ef, ebLen)) != PK_OK) {
  1536.                     if (G.qflag)
  1537.                         Info(slide, 1, ((char *)slide, "%-22s ",
  1538.                           FnFilter1(G.filename)));
  1539.                     switch (r) {
  1540.                         case IZ_EF_TRUNC:
  1541.                             Info(slide, 1, ((char *)slide,
  1542.                               LoadFarString(TruncEAs),
  1543.                               makeword(ef+2)-10, "\n"));
  1544.                             break;
  1545.                         case PK_ERR:
  1546.                             Info(slide, 1, ((char *)slide,
  1547.                               LoadFarString(InvalidComprDataEAs)));
  1548.                             break;
  1549.                         case PK_MEM3:
  1550.                         case PK_MEM4:
  1551.                             Info(slide, 1, ((char *)slide,
  1552.                               LoadFarString(NotEnoughMemEAs)));
  1553.                             break;
  1554.                         default:
  1555.                             if ((r & 0xff) != PK_ERR)
  1556.                                 Info(slide, 1, ((char *)slide,
  1557.                                   LoadFarString(UnknErrorEAs)));
  1558.                             else {
  1559.                                 ush m = (ush)(r >> 8);
  1560.                                 if (m == DEFLATED)            /* GRR KLUDGE! */
  1561.                                     Info(slide, 1, ((char *)slide,
  1562.                                       LoadFarString(BadCRC_EAs)));
  1563.                                 else
  1564.                                     Info(slide, 1, ((char *)slide,
  1565.                                       LoadFarString(UnknComprMethodEAs), m));
  1566.                             }
  1567.                             break;
  1568.                     }
  1569.                     return r;
  1570.                 }
  1571.                 break;
  1572.  
  1573.             case EF_NTSD:
  1574. #ifdef WIN32
  1575.                 if ((r = test_NT(__G__ ef, ebLen)) != PK_OK) {
  1576.                     if (G.qflag)
  1577.                         Info(slide, 1, ((char *)slide, "%-22s ",
  1578.                           FnFilter1(G.filename)));
  1579.                     switch (r) {
  1580.                         case IZ_EF_TRUNC:
  1581.                             Info(slide, 1, ((char *)slide,
  1582.                               LoadFarString(TruncNTSD),
  1583.                               makeword(ef+2)-11, "\n"));
  1584.                             break;
  1585.                         case PK_ERR:
  1586.                             Info(slide, 1, ((char *)slide,
  1587.                               LoadFarString(InvalidComprDataEAs)));
  1588.                             break;
  1589.                         case PK_MEM3:
  1590.                         case PK_MEM4:
  1591.                             Info(slide, 1, ((char *)slide,
  1592.                               LoadFarString(NotEnoughMemEAs)));
  1593.                             break;
  1594.                         default:
  1595.                             if ((r & 0xff) != PK_ERR)
  1596.                                 Info(slide, 1, ((char *)slide,
  1597.                                   LoadFarString(UnknErrorEAs)));
  1598.                             else {
  1599.                                 ush m = (ush)(r >> 8);
  1600.                                 if (m == DEFLATED)            /* GRR KLUDGE! */
  1601.                                     Info(slide, 1, ((char *)slide,
  1602.                                       LoadFarString(BadCRC_EAs)));
  1603.                                 else
  1604.                                     Info(slide, 1, ((char *)slide,
  1605.                                       LoadFarString(UnknComprMethodEAs), m));
  1606.                             }
  1607.                             break;
  1608.                     }
  1609.                     return r;
  1610.                 }
  1611.                 break;
  1612. #endif
  1613.             case EF_PKVMS:
  1614.             case EF_ASIUNIX:
  1615.             case EF_IZVMS:
  1616.             case EF_IZUNIX:
  1617.             case EF_VMCMS:
  1618.             case EF_MVS:
  1619.             case EF_SPARK:
  1620.             case EF_AV:
  1621.             default:
  1622.                 break;
  1623.         }
  1624.         ef_len -= (ebLen + EB_HEADSIZE);
  1625.         ef += (ebLen + EB_HEADSIZE);
  1626.     }
  1627.  
  1628.     if (!G.qflag)
  1629.         Info(slide, 0, ((char *)slide, " OK\n"));
  1630.  
  1631.     return PK_COOL;
  1632.  
  1633. } /* end function TestExtraField() */
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639. /*************************/
  1640. /*  Function test_OS2()  */
  1641. /*************************/
  1642.  
  1643. static int test_OS2(__G__ eb, eb_size)
  1644.     __GDEF
  1645.     uch *eb;
  1646.     unsigned eb_size;
  1647. {
  1648.     ulg eb_ucsize = makelong(eb+4);
  1649.     uch *eb_uncompressed;
  1650.     int r;
  1651.  
  1652.     if (eb_ucsize > 0L && eb_size <= 10)
  1653.         return IZ_EF_TRUNC;               /* no compressed data! */
  1654.  
  1655.     if ((eb_uncompressed = (uch *)malloc((extent)eb_ucsize)) == (uch *)NULL)
  1656.         return PK_MEM4;
  1657.  
  1658.     r = memextract(__G__ eb_uncompressed, eb_ucsize, eb+8, (ulg)(eb_size-4));
  1659.  
  1660.     free(eb_uncompressed);
  1661.     return r;
  1662.  
  1663. } /* end function test_OS2() */
  1664.  
  1665. #endif /* !SFX */
  1666.  
  1667.  
  1668.  
  1669.  
  1670.  
  1671. /***************************/
  1672. /*  Function memextract()  */
  1673. /***************************/
  1674.  
  1675. int memextract(__G__ tgt, tgtsize, src, srcsize)  /* extract compressed */
  1676.     __GDEF                                        /*  extra field block; */
  1677.     uch *tgt, *src;                               /*  return PK-type error */
  1678.     ulg tgtsize, srcsize;                         /*  level */
  1679. {
  1680.     uch *old_inptr=G.inptr;
  1681.     int  old_incnt=G.incnt, r, error=PK_OK;
  1682.     ush  method;
  1683.     ulg  extra_field_crc;
  1684.  
  1685.  
  1686.     method = makeword(src);
  1687.     extra_field_crc = makelong(src+2);
  1688.  
  1689.     /* compressed extra field exists completely in memory at this location: */
  1690.     G.inptr = src + 2 + 4;      /* method and extra_field_crc */
  1691.     G.incnt = (int)(G.csize = (long)(srcsize - (2 + 4)));
  1692.     G.mem_mode = TRUE;
  1693.     G.outbufptr = tgt;
  1694.     G.outsize = tgtsize;
  1695.  
  1696.     switch (method) {
  1697.         case STORED:
  1698.             memcpy((char *)tgt, (char *)G.inptr, (extent)G.incnt);
  1699.             G.outcnt = G.csize;   /* for CRC calculation */
  1700.             break;
  1701.         case DEFLATED:
  1702.             G.outcnt = 0L;
  1703.             if ((r = UZinflate(__G)) != 0) {
  1704.                 if (!G.tflag)
  1705.                     Info(slide, 0x401, ((char *)slide,
  1706.                       LoadFarStringSmall(ErrUnzipNoFile), r == 3?
  1707.                       LoadFarString(NotEnoughMem) :
  1708.                       LoadFarString(InvalidComprData),
  1709.                       LoadFarStringSmall2(Inflate)));
  1710.                 error = (r == 3)? PK_MEM3 : PK_ERR;
  1711.             }
  1712.             if (G.outcnt == 0L)   /* inflate's final FLUSH sets outcnt */
  1713.                 break;
  1714.             break;
  1715.         default:
  1716.             if (G.tflag)
  1717.                 error = PK_ERR | ((int)method << 8);
  1718.             else {
  1719.                 Info(slide, 0x401, ((char *)slide,
  1720.                   LoadFarString(UnsupportedExtraField), method));
  1721.                 error = PK_ERR;  /* GRR:  should be passed on up via SetEAs() */
  1722.             }
  1723.             break;
  1724.     }
  1725.  
  1726.     G.inptr = old_inptr;
  1727.     G.incnt = old_incnt;
  1728.     G.mem_mode = FALSE;
  1729.  
  1730.     if (!error) {
  1731.         register ulg crcval = crc32(CRCVAL_INITIAL, tgt, (extent)G.outcnt);
  1732.  
  1733.         if (crcval != extra_field_crc) {
  1734.             if (G.tflag)
  1735.                 error = PK_ERR | (DEFLATED << 8);  /* kludge for now */
  1736.             else {
  1737.                 Info(slide, 0x401, ((char *)slide,
  1738.                   LoadFarString(BadExtraFieldCRC), G.zipfn, crcval,
  1739.                   extra_field_crc));
  1740.                 error = PK_ERR;
  1741.             }
  1742.         }
  1743.     }
  1744.     return error;
  1745.  
  1746. } /* end function memextract() */
  1747.  
  1748.  
  1749.  
  1750.  
  1751.  
  1752. /*************************/
  1753. /*  Function memflush()  */
  1754. /*************************/
  1755.  
  1756. int memflush(__G__ rawbuf, size)
  1757.     __GDEF
  1758.     uch *rawbuf;
  1759.     ulg size;
  1760. {
  1761.     if (size > G.outsize)
  1762.         return 50;   /* more data than output buffer can hold */
  1763.  
  1764.     memcpy((char *)G.outbufptr, (char *)rawbuf, (extent)size);
  1765.     G.outbufptr += (unsigned int)size;
  1766.     G.outsize -= size;
  1767.     G.outcnt += size;
  1768.  
  1769.     return 0;
  1770.  
  1771. } /* end function memflush() */
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777. /*************************/
  1778. /*  Function fnfilter()  */        /* here instead of in list.c for SFX */
  1779. /*************************/
  1780.  
  1781. char *fnfilter(raw, space)         /* convert name to safely printable form */
  1782.     char *raw;
  1783.     uch *space;
  1784. {
  1785. #ifndef NATIVE   /* ASCII:  filter ANSI escape codes, etc. */
  1786.     uch *r=(uch *)raw, *s=space;
  1787.  
  1788.     while (*r) {
  1789. #ifdef QDOS
  1790.         if (qlflag & 2) {
  1791.             if (*r == '/' || *r == '.') {
  1792.                 ++r;
  1793.                 *s++ = '_';
  1794.                 continue;
  1795.             }
  1796.         } else
  1797. #endif
  1798.         if (*r < 32)
  1799.             *s++ = '^', *s++ = (uch)(64 + *r++);
  1800.         else
  1801.             *s++ = *r++;
  1802.     }
  1803.     *s = 0;
  1804.  
  1805. #ifdef WINDLL
  1806.     INTERN_TO_ISO((char *)space, (char *)space);  /* translate to ANSI */
  1807. #else
  1808. #ifdef WIN32
  1809.     if ( !IsWinNT() ) {
  1810.         /* Win95 console uses OEM character coding */
  1811.         INTERN_TO_OEM((char *)space, (char *)space);
  1812.     } else {
  1813.         /* >>>>> this guess has to be checked !!!! <<<<<< */
  1814.         /* WinNT console uses ISO character coding */
  1815.         INTERN_TO_ISO((char *)space, (char *)space);
  1816.     }
  1817. #endif /* WIN32 */
  1818. #endif /* ?WINDLL */
  1819.  
  1820.     return (char *)space;
  1821.  
  1822. #else /* NATIVE:  EBCDIC or whatever */
  1823.     return raw;
  1824. #endif
  1825.  
  1826. } /* end function fnfilter() */
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832. #ifdef UNIX   /* must sort saved directories so can set perms from bottom up */
  1833.  
  1834. /************************/
  1835. /*  Function dircomp()  */
  1836. /************************/
  1837.  
  1838. static int dircomp(a, b)   /* used by qsort(); swiped from Zip */
  1839.     ZCONST zvoid *a, *b;
  1840. {
  1841.     /* order is significant:  this sorts in reverse order (deepest first) */
  1842.     return strcmp((*(dirtime **)b)->fn, (*(dirtime **)a)->fn);
  1843.  /* return namecmp((*(dirtime **)b)->fn, (*(dirtime **)a)->fn); */
  1844. }
  1845.  
  1846.  
  1847.  
  1848. #if 0   /* not used in Unix, but maybe for future OSes? */
  1849.  
  1850. /************************/
  1851. /*  Function namecmp()  */
  1852. /************************/
  1853.  
  1854. static int namecmp(s1, s2)   /* [not] used by dircomp(); swiped from Zip */
  1855.     ZCONST char *s1, *s2;
  1856. {
  1857.     int d;
  1858.  
  1859.     for (;;) {
  1860.         d = (int)(uch)case_map(*s1)
  1861.           - (int)(uch)case_map(*s2);
  1862.  
  1863.         if (d || *s1 == 0 || *s2 == 0)
  1864.             return d;
  1865.  
  1866.         s1++;
  1867.         s2++;
  1868.     }
  1869. }
  1870.  
  1871. #endif /* 0 */
  1872. #endif /* UNIX */
  1873.