home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip532.zip / fileio.c < prev    next >
C/C++ Source or Header  |  1997-10-12  |  68KB  |  2,063 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   fileio.c
  4.  
  5.   This file contains routines for doing direct but relatively generic input/
  6.   output, file-related sorts of things, plus some miscellaneous stuff.  Most
  7.   of the stuff has to do with opening, closing, reading and/or writing files.
  8.  
  9.   Contains:  open_input_file()
  10.              open_outfile()           (non-VMS, non-AOS/VS, non-CMS_MVS)
  11.              undefer_input()
  12.              defer_leftover_input()
  13.              readbuf()
  14.              readbyte()
  15.              fillinbuf()
  16.              flush()                  (non-VMS)
  17.              disk_error()             (non-VMS)
  18.              UzpMessagePrnt()
  19.              UzpMessageNull()         (DLL only)
  20.              UzpInput()
  21.              UzpMorePause()
  22.              UzpPassword()            (non-WINDLL)
  23.              handler()
  24.              dos_to_unix_time()       (non-VMS, non-VM/CMS, non-MVS)
  25.              check_for_newer()        (non-VMS, non-OS/2, non-VM/CMS, non-MVS)
  26.              do_string()
  27.              makeword()
  28.              makelong()
  29.              str2iso()                (CRYPT && NEED_STR2ISO, only)
  30.              str2oem()                (CRYPT && NEED_STR2OEM, only)
  31.              memset()                 (ZMEM only)
  32.              memcpy()                 (ZMEM only)
  33.              zstrnicmp()
  34.              zstat()                  (REGULUS only)
  35.              fLoadFarString()         (SMALL_MEM only)
  36.              fLoadFarStringSmall()    (SMALL_MEM only)
  37.              fLoadFarStringSmall2()   (SMALL_MEM only)
  38.              zfstrcpy()               (SMALL_MEM only)
  39.  
  40.   ---------------------------------------------------------------------------*/
  41.  
  42.  
  43. #define FILEIO_C
  44. #define UNZIP_INTERNAL
  45. #include "unzip.h"
  46. #ifdef WINDLL
  47. #  include <setjmp.h>
  48. #endif
  49. #include "crypt.h"
  50. #include "ttyio.h"
  51.  
  52. /* setup of codepage conversion for decryption passwords */
  53. #if CRYPT
  54. #  if (defined(CRYP_USES_ISO2OEM) && !defined(IZ_ISO2OEM_ARRAY))
  55. #    define IZ_ISO2OEM_ARRAY            /* pull in iso2oem[] table */
  56. #  endif
  57. #  if (defined(CRYP_USES_OEM2ISO) && !defined(IZ_OEM2ISO_ARRAY))
  58. #    define IZ_OEM2ISO_ARRAY            /* pull in oem2iso[] table */
  59. #  endif
  60. #endif
  61. #include "ebcdic.h"   /* definition/initialization of ebcdic[] */
  62.  
  63.  
  64. /*
  65.    Note: Under Windows, the maximum size of the buffer that can be used
  66.    with any of the *printf calls is 16,384, so win_fprintf was used to
  67.    feed the fprintf clone no more than 16K chunks at a time. This should
  68.    be valid for anything up to 64K (and probably beyond, assuming your
  69.    buffers are that big).
  70. */
  71. #ifdef WINDLL
  72. #  define WriteError(buf,len,strm) \
  73.    (win_fprintf(strm, (extent)len, (char far *)buf) != (int)(len))
  74. #else /* !WINDLL */
  75. #  ifdef USE_FWRITE
  76. #    define WriteError(buf,len,strm) \
  77.      ((extent)fwrite((char *)(buf),1,(extent)(len),strm) != (extent)(len))
  78. #  else
  79. #    define WriteError(buf,len,strm) \
  80.      ((extent)write(fileno(strm),(char *)(buf),(extent)(len)) != (extent)(len))
  81. #  endif
  82. #endif /* ?WINDLL */
  83.  
  84. static int disk_error OF((__GPRO));
  85.  
  86. #ifdef WINDLL
  87.    extern int win_fprintf(FILE *file, unsigned int, char far *);
  88.    extern jmp_buf dll_error_return;
  89. #endif
  90.  
  91.  
  92.  
  93. /****************************/
  94. /* Strings used in fileio.c */
  95. /****************************/
  96.  
  97. #if (defined(UNIX) || defined(DOS_FLX_OS2_W32) || defined(__BEOS__))
  98.    static char Far CannotDeleteOldFile[] = "error:  cannot delete old %s\n";
  99. #ifdef UNIXBACKUP
  100.    static char Far CannotRenameOldFile[] = "error:  cannot rename old %s\n";
  101.    static char Far BackupSuffix[] = "~";
  102. #endif
  103. #endif
  104.  
  105. static char Far CannotOpenZipfile[] = "error:  cannot open zipfile [ %s ]\n";
  106. #if (!defined(VMS) && !defined(AOS_VS) && !defined(CMS_MVS))
  107.    static char Far CannotCreateFile[] = "error:  cannot create %s\n";
  108. #endif
  109. #ifdef NOVELL_BUG_FAILSAFE
  110.    static char Far NovellBug[] =
  111.      "error:  %s: stat() says does not exist, but fopen() found anyway\n";
  112. #endif
  113. static char Far ReadError[] = "error:  zipfile read error\n";
  114. static char Far FilenameTooLongTrunc[] =
  115.   "warning:  filename too long--truncating.\n";
  116. static char Far ExtraFieldTooLong[] =
  117.   "warning:  extra field too long (%d).  Ignoring...\n";
  118.  
  119. #ifdef WINDLL
  120.    static char Far DiskFullQuery[] =
  121.      "%s:  write error (disk full?).\n";
  122. #else
  123.    static char Far DiskFullQuery[] =
  124.      "%s:  write error (disk full?).  Continue? (y/n/^C) ";
  125.    static char Far ZipfileCorrupt[] = "error:  zipfile probably corrupt (%s)\n";
  126. #  ifdef SYMLINKS
  127.      static char Far FileIsSymLink[] = "%s exists and is a symbolic link%s.\n";
  128. #  endif
  129. #  ifdef MORE
  130.      static char Far MorePrompt[] = "--More--(%lu)";
  131. #  endif
  132.    static char Far QuitPrompt[] =
  133.      "--- Press `Q' to quit, or any other key to continue ---";
  134.    static char Far HidePrompt[] = /* "\r                       \r"; */
  135.      "\r                                                         \r";
  136. #endif /* !WINDLL */
  137.  
  138.  
  139.  
  140.  
  141.  
  142. /******************************/
  143. /* Function open_input_file() */
  144. /******************************/
  145.  
  146. int open_input_file(__G)    /* return 1 if open failed */
  147.     __GDEF
  148. {
  149.     /*
  150.      *  open the zipfile for reading and in BINARY mode to prevent cr/lf
  151.      *  translation, which would corrupt the bitstreams
  152.      */
  153.  
  154. #if (defined(UNIX) || defined(TOPS20) || defined(AOS_VS) || defined(__BEOS__))
  155.     G.zipfd = open(G.zipfn, O_RDONLY);
  156. #else /* !(UNIX || TOPS20 || AOS_VS || __BEOS__) */
  157. #ifdef VMS
  158.     G.zipfd = open(G.zipfn, O_RDONLY, 0, "ctx=stm");
  159. #else /* !VMS */
  160. #ifdef MACOS
  161.     G.zipfd = open(G.zipfn, 0);
  162. #else /* !MACOS */
  163. #ifdef RISCOS
  164.     G.zipfd = fopen(G.zipfn, "rb");
  165. #else /* !RISCOS */
  166. #ifdef CMS_MVS
  167.     G.zipfd = vmmvs_open_infile(__G);
  168. #else /* !CMS_MVS */
  169.     G.zipfd = open(G.zipfn, O_RDONLY | O_BINARY);
  170. #endif /* ?CMS_MVS */
  171. #endif /* ?RISCOS */
  172. #endif /* ?MACOS */
  173. #endif /* ?VMS */
  174. #endif /* ?(UNIX || TOPS20 || AOS_VS || __BEOS__) */
  175.  
  176. #ifdef USE_STRM_INPUT
  177.     if (G.zipfd == NULL)
  178. #else
  179.     /* if (G.zipfd < 0) */  /* no good for Windows CE port */
  180.     if (G.zipfd == -1)
  181. #endif
  182.     {
  183.         Info(slide, 0x401, ((char *)slide, LoadFarString(CannotOpenZipfile),
  184.           G.zipfn));
  185.         return 1;
  186.     }
  187.     return 0;
  188.  
  189. } /* end function open_input_file() */
  190.  
  191.  
  192.  
  193.  
  194. #if (!defined(VMS) && !defined(AOS_VS) && !defined(CMS_MVS))
  195.  
  196. /***************************/
  197. /* Function open_outfile() */
  198. /***************************/
  199.  
  200. int open_outfile(__G)         /* return 1 if fail */
  201.     __GDEF
  202. {
  203. #ifdef DLL
  204.     if (G.redirect_data)
  205.         return (redirect_outfile(__G) == FALSE);
  206. #endif
  207. #ifdef QDOS
  208.     QFilename(__G__ G.filename);
  209. #endif
  210. #if (defined(DOS_FLX_OS2_W32) || defined(UNIX) || defined(__BEOS__))
  211. #ifdef BORLAND_STAT_BUG
  212.     /* Borland 5.0's stat() barfs if the filename has no extension and the
  213.      * file doesn't exist. */
  214.     if (access(G.filename, 0) == -1) {
  215.         FILE *tmp = fopen(G.filename, "wb+");
  216.  
  217.         /* file doesn't exist, so create a dummy file to keep stat() from
  218.          * failing (will be over-written anyway) */
  219.         fputc('0', tmp);  /* just to have something in the file */
  220.         fclose(tmp);
  221.     }
  222. #endif /* BORLAND_STAT_BUG */
  223. #ifdef SYMLINKS
  224.     if (SSTAT(G.filename, &G.statbuf) == 0 || lstat(G.filename,&G.statbuf) == 0)
  225. #else
  226.     if (SSTAT(G.filename, &G.statbuf) == 0)
  227. #endif /* ?SYMLINKS */
  228.     {
  229.         Trace((stderr, "open_outfile:  stat(%s) returns 0:  file exists\n",
  230.           FnFilter1(G.filename)));
  231. #ifdef UNIXBACKUP
  232.         if (G.B_flag) {    /* do backup */
  233.             char *tname;
  234.             int blen, flen, tlen;
  235.  
  236.             blen = strlen(BackupSuffix);
  237.             flen = strlen(G.filename);
  238.             tlen = flen + blen + 1;
  239.             if (tlen >= FILNAMSIZ) {   /* in case name is too long, truncate */
  240.                 tname = (char *)malloc(FILNAMSIZ);
  241.                 if (tname == NULL)
  242.                     return 1;                 /* in case we run out of space */
  243.                 tlen = FILNAMSIZ - 1 - blen;
  244.                 strcpy(tname, G.filename);    /* make backup name */
  245.                 tname[tlen] = '\0';
  246.             } else {
  247.                 tname = (char *)malloc(tlen);
  248.                 if (tname == NULL)
  249.                     return 1;                 /* in case we run out of space */
  250.                 strcpy(tname, G.filename);    /* make backup name */
  251.             }
  252.             strcpy(tname+flen, BackupSuffix);
  253.  
  254.             /* GRR:  should check if backup file exists, apply -n/-o to that */
  255.             if (rename(G.filename, tname) < 0) {   /* move file */
  256.                 Info(slide, 0x401, ((char *)slide,
  257.                   LoadFarString(CannotRenameOldFile), FnFilter1(G.filename)));
  258.                 free(tname);
  259.                 return 1;
  260.             }
  261.             free(tname);
  262.         } else
  263. #endif /* UNIXBACKUP */
  264. #ifdef DOS_FLX_OS2_W32
  265.         if (!(G.statbuf.st_mode & S_IWRITE)) {
  266.             Trace((stderr, "open_outfile:  existing file %s is read-only\n",
  267.               FnFilter1(G.filename)));
  268.             chmod(G.filename, S_IREAD | S_IWRITE);
  269.             Trace((stderr, "open_outfile:  %s now writable\n",
  270.               FnFilter1(G.filename)));
  271.         }
  272. #endif /* DOS_FLX_OS2_W32 */
  273.         if (unlink(G.filename) != 0) {
  274.             Info(slide, 0x401, ((char *)slide,
  275.               LoadFarString(CannotDeleteOldFile), FnFilter1(G.filename)));
  276.             return 1;
  277.         }
  278.         Trace((stderr, "open_outfile:  %s now deleted\n",
  279.           FnFilter1(G.filename)));
  280.     }
  281. #endif /* DOS_FLX_OS2_W32 || UNIX || __BEOS__ */
  282. #ifdef RISCOS
  283.     if (SWI_OS_File_7(G.filename,0xDEADDEAD,0xDEADDEAD,G.lrec.ucsize)!=NULL) {
  284.         Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
  285.           FnFilter1(G.filename)));
  286.         return 1;
  287.     }
  288. #endif /* RISCOS */
  289. #ifdef TOPS20
  290.     char *tfilnam;
  291.  
  292.     if ((tfilnam = (char *)malloc(2*strlen(G.filename)+1)) == (char *)NULL)
  293.         return 1;
  294.     strcpy(tfilnam, G.filename);
  295.     upper(tfilnam);
  296.     enquote(tfilnam);
  297.     if ((G.outfile = fopen(tfilnam, FOPW)) == (FILE *)NULL) {
  298.         Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
  299.           tfilnam));
  300.         free(tfilnam);
  301.         return 1;
  302.     }
  303.     free(tfilnam);
  304. #else /* !TOPS20 */
  305. #ifdef MTS
  306.     if (G.aflag)
  307.         G.outfile = fopen(G.filename, FOPWT);
  308.     else
  309.         G.outfile = fopen(G.filename, FOPW);
  310.     if (G.outfile == (FILE *)NULL) {
  311.         Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
  312.           FnFilter1(G.filename)));
  313.         return 1;
  314.     }
  315. #else /* !MTS */
  316. #ifdef TANDEM
  317.     if (stat(G.filename, &G.statbuf) == 0) {
  318.         Trace((stderr, "open_outfile:  stat(%s) returns 0 (file exists)\n",
  319.           FnFilter1(G.filename)));
  320.         if (unlink(G.filename) != 0) {
  321.             Trace((stderr, "open_outfile:  existing file %s is read-only\n",
  322.               FnFilter1(G.filename)));
  323.             chmod(G.filename, S_IRUSR | S_IWUSR);
  324.             Trace((stderr, "open_outfile:  %s now writable\n",
  325.               FnFilter1(G.filename)));
  326.             if (unlink(G.filename) != 0)
  327.                 return 1;
  328.         }
  329.         Trace((stderr, "open_outfile:  %s now deleted\n",
  330.           FnFilter1(G.filename)));
  331.     }
  332.     if (G.pInfo->textmode)
  333.         G.outfile = fopen(G.filename, FOPWT);
  334.     else
  335.         G.outfile = fopen(G.filename, FOPW);
  336.     if (G.outfile == (FILE *)NULL) {
  337.         Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
  338.           FnFilter1(G.filename)));
  339.         return 1;
  340.     }
  341. #else /* !TANDEM */
  342. #ifdef DEBUG
  343.     Info(slide, 1, ((char *)slide,
  344.       "open_outfile:  doing fopen(%s) for reading\n", FnFilter1(G.filename)));
  345.     if ((G.outfile = fopen(G.filename, FOPR)) == (FILE *)NULL)
  346.         Info(slide, 1, ((char *)slide,
  347.           "open_outfile:  fopen(%s) for reading failed:  does not exist\n",
  348.           FnFilter1(G.filename)));
  349.     else {
  350.         Info(slide, 1, ((char *)slide,
  351.           "open_outfile:  fopen(%s) for reading succeeded:  file exists\n",
  352.           FnFilter1(G.filename)));
  353.         fclose(G.outfile);
  354.     }
  355. #endif /* DEBUG */
  356. #ifdef NOVELL_BUG_FAILSAFE
  357.     if (G.dne && ((G.outfile = fopen(G.filename, FOPR)) != (FILE *)NULL)) {
  358.         Info(slide, 0x401, ((char *)slide, LoadFarString(NovellBug),
  359.           FnFilter1(G.filename)));
  360.         fclose(G.outfile);
  361.         return 1;   /* with "./" fix in checkdir(), should never reach here */
  362.     }
  363. #endif /* NOVELL_BUG_FAILSAFE */
  364.     Trace((stderr, "open_outfile:  doing fopen(%s) for writing\n",
  365.       FnFilter1(G.filename)));
  366.     if ((G.outfile = fopen(G.filename, FOPW)) == (FILE *)NULL) {
  367.         Info(slide, 0x401, ((char *)slide, LoadFarString(CannotCreateFile),
  368.           FnFilter1(G.filename)));
  369.         return 1;
  370.     }
  371.     Trace((stderr, "open_outfile:  fopen(%s) for writing succeeded\n",
  372.       FnFilter1(G.filename)));
  373. #endif /* !TANDEM */
  374. #endif /* !MTS */
  375. #endif /* !TOPS20 */
  376.  
  377. #if 0  /* This SUCKS!  On Ultrix it must be writing one byte at a time... */
  378.     setbuf(G.outfile, (char *)NULL);   /* make output unbuffered */
  379. #endif
  380.  
  381. #ifdef USE_FWRITE
  382. #ifdef DOS_OS2_W32
  383.     /* 16-bit MSC: buffer size must be strictly LESS than 32K (WSIZE):  bogus */
  384.     setbuf(G.outfile, (char *)NULL);   /* make output unbuffered */
  385. #else /* !DOS_OS2_W32 */
  386. #ifndef RISCOS
  387. #ifdef _IOFBF  /* make output fully buffered (works just about like write()) */
  388.     setvbuf(G.outfile, (char *)slide, _IOFBF, WSIZE);
  389. #else
  390.     setbuf(G.outfile, (char *)slide);
  391. #endif
  392. #endif /* !RISCOS */
  393. #endif /* ?DOS_OS2_W32 */
  394. #endif /* USE_FWRITE */
  395.     return 0;
  396.  
  397. } /* end function open_outfile() */
  398.  
  399. #endif /* !VMS && !AOS_VS && !CMS_MVS */
  400.  
  401.  
  402.  
  403.  
  404.  
  405. /*
  406.  * These functions allow NEXTBYTE to function without needing two bounds
  407.  * checks.  Call defer_leftover_input() if you ever have filled G.inbuf
  408.  * by some means other than readbyte(), and you then want to start using
  409.  * NEXTBYTE.  When going back to processing bytes without NEXTBYTE, call
  410.  * undefer_input().  For example, extract_or_test_member brackets its
  411.  * central section that does the decompression with these two functions.
  412.  * If you need to check the number of bytes remaining in the current
  413.  * file while using NEXTBYTE, check (G.csize + G.incnt), not G.csize.
  414.  */
  415.  
  416. /****************************/
  417. /* function undefer_input() */
  418. /****************************/
  419.  
  420. void undefer_input(__G)
  421.     __GDEF
  422. {
  423.     if (G.incnt > 0)
  424.         G.csize += G.incnt;
  425.     if (G.incnt_leftover > 0) {
  426.         /* We know that "(G.csize < MAXINT)" so we can cast G.csize to int:
  427.          * This condition was checked when G.incnt_leftover was set > 0 in
  428.          * defer_leftover_input(), and it is NOT allowed to touch G.csize
  429.          * before calling undefer_input() when (G.incnt_leftover > 0)
  430.          * (single exception: see read_byte()'s  "G.csize <= 0" handling) !!
  431.          */
  432.         G.incnt = G.incnt_leftover + (int)G.csize;
  433.         G.inptr = G.inptr_leftover - (int)G.csize;
  434.         G.incnt_leftover = 0;
  435.     } else if (G.incnt < 0)
  436.         G.incnt = 0;
  437. } /* end function undefer_input() */
  438.  
  439.  
  440.  
  441.  
  442.  
  443. /***********************************/
  444. /* function defer_leftover_input() */
  445. /***********************************/
  446.  
  447. void defer_leftover_input(__G)
  448.     __GDEF
  449. {
  450.     if ((long)G.incnt > G.csize) {
  451.         /* (G.csize < MAXINT), we can safely cast it to int !! */
  452.         if (G.csize < 0L)
  453.             G.csize = 0L;
  454.         G.inptr_leftover = G.inptr + (int)G.csize;
  455.         G.incnt_leftover = G.incnt - (int)G.csize;
  456.         G.incnt = (int)G.csize;
  457.     } else
  458.         G.incnt_leftover = 0;
  459.     G.csize -= G.incnt;
  460. } /* end function defer_leftover_input() */
  461.  
  462.  
  463.  
  464.  
  465.  
  466. /**********************/
  467. /* Function readbuf() */
  468. /**********************/
  469.  
  470. unsigned readbuf(__G__ buf, size)   /* return number of bytes read into buf */
  471.     __GDEF
  472.     char *buf;
  473.     register unsigned size;
  474. {
  475.     register unsigned count;
  476.     unsigned n;
  477.  
  478.     n = size;
  479.     while (size) {
  480.         if (G.incnt <= 0) {
  481.             if ((G.incnt = read(G.zipfd, (char *)G.inbuf, INBUFSIZ)) == 0)
  482.                 return (n-size);
  483.             else if (G.incnt < 0) {
  484.                 /* another hack, but no real harm copying same thing twice */
  485.                 (*G.message)((zvoid *)&G,
  486.                   (uch *)LoadFarString(ReadError),  /* CANNOT use slide */
  487.                   (ulg)strlen(LoadFarString(ReadError)), 0x401);
  488.                 return 0;  /* discarding some data; better than lock-up */
  489.             }
  490.             /* buffer ALWAYS starts on a block boundary:  */
  491.             G.cur_zipfile_bufstart += INBUFSIZ;
  492.             G.inptr = G.inbuf;
  493.         }
  494.         count = MIN(size, (unsigned)G.incnt);
  495.         memcpy(buf, G.inptr, count);
  496.         buf += count;
  497.         G.inptr += count;
  498.         G.incnt -= count;
  499.         size -= count;
  500.     }
  501.     return n;
  502.  
  503. } /* end function readbuf() */
  504.  
  505.  
  506.  
  507.  
  508.  
  509. /***********************/
  510. /* Function readbyte() */
  511. /***********************/
  512.  
  513. int readbyte(__G)   /* refill inbuf and return a byte if available, else EOF */
  514.     __GDEF
  515. {
  516.     if (G.mem_mode)
  517.         return EOF;
  518.     if (G.csize <= 0) {
  519.         G.csize--;             /* for tests done after exploding */
  520.         G.incnt = 0;
  521.         return EOF;
  522.     }
  523.     if (G.incnt <= 0) {
  524.         if ((G.incnt = read(G.zipfd, (char *)G.inbuf, INBUFSIZ)) == 0) {
  525.             G.incnt = 0;       /* do not allow negative value to affect stuff */
  526.             return EOF;
  527.         } else if (G.incnt < 0) {  /* "fail" (abort, retry, ...) returns this */
  528.             /* another hack, but no real harm copying same thing twice */
  529.             (*G.message)((zvoid *)&G,
  530.               (uch *)LoadFarString(ReadError),
  531.               (ulg)strlen(LoadFarString(ReadError)), 0x401);
  532.             echon();
  533. #ifdef WINDLL
  534.             longjmp(dll_error_return, 1);
  535. #else
  536.             DESTROYGLOBALS()
  537.             EXIT(PK_BADERR);    /* totally bailing; better than lock-up */
  538. #endif
  539.         }
  540.         G.cur_zipfile_bufstart += INBUFSIZ; /* always starts on block bndry */
  541.         G.inptr = G.inbuf;
  542.         defer_leftover_input(__G);           /* decrements G.csize */
  543.     }
  544.  
  545. #if CRYPT
  546.     if (G.pInfo->encrypted) {
  547.         uch *p;
  548.         int n;
  549.  
  550.         /* This was previously set to decrypt one byte beyond G.csize, when
  551.          * incnt reached that far.  GRR said, "but it's required:  why?"  This
  552.          * was a bug in fillinbuf() -- was it also a bug here?
  553.          */
  554.         for (n = G.incnt, p = G.inptr;  n--;  p++)
  555.             zdecode(*p);
  556.     }
  557. #endif /* CRYPT */
  558.  
  559.     --G.incnt;
  560.     return *G.inptr++;
  561.  
  562. } /* end function readbyte() */
  563.  
  564.  
  565.  
  566.  
  567.  
  568. #ifdef USE_ZLIB
  569.  
  570. /************************/
  571. /* Function fillinbuf() */
  572. /************************/
  573.  
  574. int fillinbuf(__G) /* like readbyte() except returns number of bytes in inbuf */
  575.     __GDEF
  576. {
  577.     if (G.mem_mode ||
  578.                   (G.incnt = read(G.zipfd, (char *)G.inbuf, INBUFSIZ)) <= 0)
  579.         return 0;
  580.     G.cur_zipfile_bufstart += INBUFSIZ;  /* always starts on a block boundary */
  581.     G.inptr = G.inbuf;
  582.     defer_leftover_input(__G);           /* decrements G.csize */
  583.  
  584. #if CRYPT
  585.     if (G.pInfo->encrypted) {
  586.         uch *p;
  587.         int n;
  588.  
  589.         for (n = G.incnt, p = G.inptr;  n--;  p++)
  590.             zdecode(*p);
  591.     }
  592. #endif /* CRYPT */
  593.  
  594.     return G.incnt;
  595.  
  596. } /* end function fillinbuf() */
  597.  
  598. #endif /* USE_ZLIB */
  599.  
  600.  
  601.  
  602.  
  603.  
  604. #ifndef VMS  /* for VMS use code in vms.c */
  605.  
  606. /********************/
  607. /* Function flush() */   /* if cflag => always returns 0; 50 if write error */
  608. /********************/
  609.  
  610. int flush(__G__ rawbuf, size, unshrink)
  611.     __GDEF
  612.     uch *rawbuf;
  613.     ulg size;
  614.     int unshrink;
  615. {
  616.     register uch *p, *q;
  617.     uch *transbuf;
  618. #if (defined(SMALL_MEM) || defined(MED_MEM) || defined(VMS_TEXT_CONV))
  619.     ulg transbufsiz;
  620. #endif
  621.     /* static int didCRlast = FALSE;    moved to globals.h */
  622.  
  623.  
  624. /*---------------------------------------------------------------------------
  625.     Compute the CRC first; if testing or if disk is full, that's it.
  626.   ---------------------------------------------------------------------------*/
  627.  
  628.     G.crc32val = crc32(G.crc32val, rawbuf, (extent)size);
  629.  
  630.     if (G.tflag || size == 0L)   /* testing or nothing to write:  all done */
  631.         return 0;
  632.  
  633.     if (G.disk_full)
  634.         return 50;            /* disk already full:  ignore rest of file */
  635.  
  636. /*---------------------------------------------------------------------------
  637.     Write the bytes rawbuf[0..size-1] to the output device, first converting
  638.     end-of-lines and ASCII/EBCDIC as needed.  If SMALL_MEM or MED_MEM are NOT
  639.     defined, outbuf is assumed to be at least as large as rawbuf and is not
  640.     necessarily checked for overflow.
  641.   ---------------------------------------------------------------------------*/
  642.  
  643.     if (!G.pInfo->textmode) {   /* write raw binary data */
  644.         /* GRR:  note that for standard MS-DOS compilers, size argument to
  645.          * fwrite() can never be more than 65534, so WriteError macro will
  646.          * have to be rewritten if size can ever be that large.  For now,
  647.          * never more than 32K.  Also note that write() returns an int, which
  648.          * doesn't necessarily limit size to 32767 bytes if write() is used
  649.          * on 16-bit systems but does make it more of a pain; however, because
  650.          * at least MSC 5.1 has a lousy implementation of fwrite() (as does
  651.          * DEC Ultrix cc), write() is used anyway.
  652.          */
  653. #ifdef DLL
  654.         if (G.redirect_data)
  655.             writeToMemory(__G__ rawbuf, size);
  656.         else
  657. #endif
  658.         if (!G.cflag && WriteError(rawbuf, size, G.outfile))
  659.             return disk_error(__G);
  660.         else if (G.cflag && (*G.message)((zvoid *)&G, rawbuf, size, 0))
  661.             return 0;
  662.     } else {   /* textmode:  aflag is true */
  663.         if (unshrink) {
  664.             /* rawbuf = outbuf */
  665.             transbuf = G.outbuf2;
  666. #if (defined(SMALL_MEM) || defined(MED_MEM) || defined(VMS_TEXT_CONV))
  667.             transbufsiz = TRANSBUFSIZ;
  668. #endif
  669.         } else {
  670.             /* rawbuf = slide */
  671.             transbuf = G.outbuf;
  672. #if (defined(SMALL_MEM) || defined(MED_MEM) || defined(VMS_TEXT_CONV))
  673.             transbufsiz = OUTBUFSIZ;
  674.             Trace((stderr, "\ntransbufsiz = OUTBUFSIZ = %u\n", OUTBUFSIZ));
  675. #endif
  676.         }
  677.         if (G.newfile) {
  678. #ifdef VMS_TEXT_CONV
  679.             /* GRR: really want to check if -aa (or -aaa?) was given... */
  680.             if (rawbuf[1]) {       /* first line is more than 255 chars long */
  681.                 Trace((stderr,
  682.       "\nfirst line of VMS `text' too long; switching to normal extraction\n"));
  683.                 G.VMS_line_state = -1;   /* -1: don't treat as VMS text */
  684.             } else
  685.                 G.VMS_line_state = 0;    /* 0: ready to read line length */
  686. #endif
  687.             G.didCRlast = FALSE;         /* no previous buffers written */
  688.             G.newfile = FALSE;
  689.         }
  690.  
  691. #ifdef VMS_TEXT_CONV
  692.         if (G.pInfo->hostnum == VMS_ && G.extra_field && G.VMS_line_state >= 0)
  693.         {
  694.             /* GRR: really want to check for actual VMS extra field, and
  695.              *      ideally for variable-length record format */
  696. /*
  697.             printf("\n>>>>>> GRR:  file is VMS text and has an extra field\n");
  698.  */
  699.  
  700.             p = rawbuf;
  701.             q = transbuf;
  702.             while(p < rawbuf+(unsigned)size) {
  703.                 switch (G.VMS_line_state) {
  704.  
  705.                     /* 0: ready to read line length */
  706.                     case 0:
  707.                         G.VMS_line_length = 0;
  708.                         G.VMS_line_pad = 0;
  709.                         if (p == rawbuf+(unsigned)size-1) {    /* last char */
  710.                             G.VMS_line_length = (int)((uch)(*p++));
  711.                             G.VMS_line_state = 1;
  712.                         } else {
  713.                             G.VMS_line_length = makeword(p);
  714.                             p += 2;
  715.                             G.VMS_line_state = 2;
  716.                         }
  717.                         if (G.VMS_line_length & 1)   /* odd */
  718.                             G.VMS_line_pad = 1;
  719.                         break;
  720.  
  721.                     /* 1: read one byte of length, need second */
  722.                     case 1:
  723.                         G.VMS_line_length += ((int)((uch)(*p++)) << 8);
  724.                         G.VMS_line_state = 2;
  725.                         break;
  726.  
  727.                     /* 2: ready to read VMS_line_length chars */
  728.                     case 2:
  729.                         if (G.VMS_line_length < rawbuf+(unsigned)size-p) {
  730.                             if (G.VMS_line_length >=
  731.                                 transbuf+(unsigned)transbufsiz-q)
  732.                             {
  733.                                 int outroom = transbuf+(unsigned)transbufsiz-q;
  734.  
  735. /* GRR:  need to change this to *q++ = native(*p++); loop or something */
  736.                                 memcpy(q, p, outroom);
  737. #ifdef DLL
  738.                                 if (G.redirect_data)
  739.                                     writeToMemory(__G__ transbuf,
  740.                                       (unsigned)outroom);
  741.                                 else
  742. #endif
  743.                                 if (!G.cflag && WriteError(transbuf,
  744.                                     (unsigned)outroom, G.outfile))
  745.                                     return disk_error(__G);
  746.                                 else if (G.cflag && (*G.message)((zvoid *)&G,
  747.                                          transbuf, (ulg)outroom, 0))
  748.                                     return 0;
  749.                                 q = transbuf;
  750.                                 p += outroom;
  751.                                 G.VMS_line_length -= outroom;
  752.                                 /* fall through to normal case */
  753.                             }
  754. /* GRR:  need to change this to *q++ = native(*p++); loop or something */
  755.                             memcpy(q, p, G.VMS_line_length);
  756.                             q += G.VMS_line_length;
  757.                             p += G.VMS_line_length;
  758.                             G.VMS_line_length = 0;   /* necessary?? */
  759.                             G.VMS_line_state = 3;
  760.  
  761.                         } else {  /* remaining input is less than full line */
  762.                             int remaining = rawbuf+(unsigned)size-p;
  763.  
  764.                             if (remaining <
  765.                                 transbuf+(unsigned)transbufsiz-q)
  766.                             {
  767.                                 int outroom = transbuf+(unsigned)transbufsiz-q;
  768.  
  769. /* GRR:  need to change this to *q++ = native(*p++); loop or something */
  770.                                 memcpy(q, p, outroom);
  771. #ifdef DLL
  772.                                 if (G.redirect_data)
  773.                                     writeToMemory(__G__ transbuf,
  774.                                       (unsigned)(outroom));
  775.                                 else
  776. #endif
  777.                                 if (!G.cflag && WriteError(transbuf,
  778.                                     (unsigned)outroom, G.outfile))
  779.                                     return disk_error(__G);
  780.                                 else if (G.cflag && (*G.message)((zvoid *)&G,
  781.                                          transbuf, (ulg)outroom, 0))
  782.                                     return 0;
  783.                                 q = transbuf;
  784.                                 p += outroom;
  785.                                 remaining -= outroom;
  786.                             }
  787. /* GRR:  need to change this to *q++ = native(*p++); loop or something */
  788.                             memcpy(q, p, remaining);
  789.                             q += remaining;
  790.                             p += remaining;
  791.                             G.VMS_line_length -= remaining;
  792.                             /* no change in G.VMS_line_state */
  793.                         }
  794.                         break;
  795.  
  796.                     /* 3: ready to PutNativeEOL */
  797.                     case 3:
  798.                         if (q > transbuf+(unsigned)transbufsiz-lenEOL) {
  799. #ifdef DLL
  800.                             if (G.redirect_data)
  801.                                 writeToMemory(__G__ transbuf,
  802.                                   (unsigned)(q-transbuf));
  803.                             else
  804. #endif
  805.                             if (!G.cflag &&
  806.                                 WriteError(transbuf, (unsigned)(q-transbuf),
  807.                                   G.outfile))
  808.                                 return disk_error(__G);
  809.                             else if (G.cflag && (*G.message)((zvoid *)&G,
  810.                                      transbuf, (ulg)(q-transbuf), 0))
  811.                                 return 0;
  812.                             q = transbuf;
  813.                         }
  814.                         PutNativeEOL
  815.                         if (G.VMS_line_pad)
  816.                             if (p < rawbuf+(unsigned)size) {
  817.                                 ++p;
  818.                                 G.VMS_line_state = 0;
  819.                             } else
  820.                                 G.VMS_line_state = 4;
  821.                         else
  822.                             G.VMS_line_state = 0;
  823.                         break;
  824.  
  825.                     /* 4: ready to read pad byte */
  826.                     case 4:
  827.                         ++p;
  828.                         G.VMS_line_state = 0;
  829.                         break;
  830.                 }
  831.             } /* end while */
  832.  
  833.         } else
  834. #endif /* VMS_TEXT_CONV */
  835.  
  836.     /*-----------------------------------------------------------------------
  837.         Algorithm:  CR/LF => native; lone CR => native; lone LF => native.
  838.         This routine is only for non-raw-VMS, non-raw-VM/CMS files (i.e.,
  839.         stream-oriented files, not record-oriented).
  840.       -----------------------------------------------------------------------*/
  841.  
  842.         /* else not VMS text */ {
  843.             p = rawbuf;
  844.             if (*p == LF && G.didCRlast)
  845.                 ++p;
  846.             G.didCRlast = FALSE;
  847.             for (q = transbuf;  p < rawbuf+(unsigned)size;  ++p) {
  848.                 if (*p == CR) {           /* lone CR or CR/LF: EOL either way */
  849.                     PutNativeEOL
  850.                     if (p == rawbuf+(unsigned)size-1)  /* last char in buffer */
  851.                         G.didCRlast = TRUE;
  852.                     else if (p[1] == LF)  /* get rid of accompanying LF */
  853.                         ++p;
  854.                 } else if (*p == LF)      /* lone LF */
  855.                     PutNativeEOL
  856.                 else
  857. #ifndef DOS_FLX_OS2_W32
  858.                 if (*p != CTRLZ)          /* lose all ^Z's */
  859. #endif
  860.                     *q++ = native(*p);
  861.  
  862. #if (defined(SMALL_MEM) || defined(MED_MEM))
  863. # if (lenEOL == 1)   /* don't check unshrink:  both buffers small but equal */
  864.                 if (!unshrink)
  865. # endif
  866.                     /* check for danger of buffer overflow and flush */
  867.                     if (q > transbuf+(unsigned)transbufsiz-lenEOL) {
  868.                         Trace((stderr,
  869.                           "p - rawbuf = %u   q-transbuf = %u   size = %lu\n",
  870.                           (unsigned)(p-rawbuf), (unsigned)(q-transbuf), size));
  871.                         if (!G.cflag && WriteError(transbuf,
  872.                             (unsigned)(q-transbuf), G.outfile))
  873.                             return disk_error(__G);
  874.                         else if (G.cflag && (*G.message)((zvoid *)&G, transbuf,
  875.                                  (ulg)(q-transbuf), 0))
  876.                             return 0;
  877.                         q = transbuf;
  878.                         continue;
  879.                     }
  880. #endif /* SMALL_MEM || MED_MEM */
  881.             }
  882.         }
  883.  
  884.     /*-----------------------------------------------------------------------
  885.         Done translating:  write whatever we've got to file (or screen).
  886.       -----------------------------------------------------------------------*/
  887.  
  888.         Trace((stderr, "p - rawbuf = %u   q-transbuf = %u   size = %lu\n",
  889.           (unsigned)(p-rawbuf), (unsigned)(q-transbuf), size));
  890.         if (q > transbuf)
  891. #ifdef DLL
  892.             if (G.redirect_data)
  893.                 writeToMemory(__G__ transbuf, (unsigned)(q-transbuf));
  894.             else
  895. #endif
  896.             if (!G.cflag && WriteError(transbuf, (unsigned)(q-transbuf),
  897.                 G.outfile))
  898.                 return disk_error(__G);
  899.             else if (G.cflag && (*G.message)((zvoid *)&G, transbuf,
  900.                 (ulg)(q-transbuf), 0))
  901.                 return 0;
  902.     }
  903.  
  904.     return 0;
  905.  
  906. } /* end function flush() */
  907.  
  908.  
  909.  
  910.  
  911.  
  912. /*************************/
  913. /* Function disk_error() */
  914. /*************************/
  915.  
  916. static int disk_error(__G)
  917.     __GDEF
  918. {
  919.     /* OK to use slide[] here because this file is finished regardless */
  920.     Info(slide, 0x4a1, ((char *)slide, LoadFarString(DiskFullQuery),
  921.       FnFilter1(G.filename)));
  922.  
  923. #ifndef WINDLL
  924.     fgets(G.answerbuf, 9, stdin);
  925.     if (*G.answerbuf == 'y')   /* stop writing to this file */
  926.         G.disk_full = 1;       /*  (outfile bad?), but new OK */
  927.     else
  928. #endif
  929.         G.disk_full = 2;       /* no:  exit program */
  930.  
  931.     return PK_DISK;
  932.  
  933. } /* end function disk_error() */
  934.  
  935. #endif /* !VMS */
  936.  
  937.  
  938.  
  939.  
  940.  
  941. /*****************************/
  942. /* Function UzpMessagePrnt() */
  943. /*****************************/
  944.  
  945. int UZ_EXP UzpMessagePrnt(pG, buf, size, flag)
  946.     zvoid *pG;   /* globals struct:  always passed */
  947.     uch *buf;    /* preformatted string to be printed */
  948.     ulg size;    /* length of string (may include nulls) */
  949.     int flag;    /* flag bits */
  950. {
  951.     /* IMPORTANT NOTE:
  952.      *    The name of the first parameter of UzpMessagePrnt(), which passes
  953.      *    the struct Globals address, >>> MUST <<< be identical to the string
  954.      *    expansion of the __G__ macro in the REENTRANT case (see globals.h).
  955.      *    This name identity is mandatory for the LoadFarString() macro
  956.      *    (in the SMALL_MEM case) !!!
  957.      */
  958.     int error;
  959.     uch *q=buf, *endbuf=buf+(unsigned)size;
  960. #ifdef MORE
  961.     uch *p=buf-1;
  962. #endif
  963.     FILE *outfp;
  964.  
  965.  
  966. /*---------------------------------------------------------------------------
  967.     These tests are here to allow fine-tuning of UnZip's output messages,
  968.     but none of them will do anything without setting the appropriate bit
  969.     in the flag argument of every Info() statement which is to be turned
  970.     *off*.  That is, all messages are currently turned on for all ports.
  971.     To turn off *all* messages, use the UzpMessageNull() function instead
  972.     of this one.
  973.   ---------------------------------------------------------------------------*/
  974.  
  975. #if (defined(OS2) && defined(DLL))
  976.     if (MSG_NO_DLL2(flag))  /* if OS/2 DLL bit is set, do NOT print this msg */
  977.         return 0;
  978. #endif
  979. #ifdef WINDLL
  980.     if (MSG_NO_WDLL(flag))
  981.         return 0;
  982. #endif
  983. #ifdef WINDLL
  984.     if (MSG_NO_WGUI(flag))
  985.         return 0;
  986. #endif
  987. /*
  988. #ifdef ACORN_GUI
  989.     if (MSG_NO_AGUI(flag))
  990.         return 0;
  991. #endif
  992.  */
  993. #ifdef DLL                 /* don't display message if data is redirected */
  994.     if (((struct Globals *)pG)->redirect_data &&
  995.         !((struct Globals *)pG)->redirect_text)
  996.         return 0;
  997. #endif
  998.  
  999.     if (MSG_STDERR(flag) && !((struct Globals *)pG)->tflag)
  1000.         outfp = (FILE *)stderr;
  1001.     else
  1002.         outfp = (FILE *)stdout;
  1003.  
  1004. #ifdef MPW
  1005.     if (MSG_MNEWLN(flag)) {   /* assumes writable buffer (e.g., slide[]) */
  1006.         *endbuf++ = '\n';     /*  with room for one more char at end of buf */
  1007.         ++size;               /*  (safe assumption:  only used for four */
  1008.     }                         /*  short queries in extract.c and fileio.c) */
  1009. #endif
  1010.  
  1011.     if (MSG_TNEWLN(flag)) {   /* again assumes writable buffer:  fragile... */
  1012.         if ((!size && !((struct Globals *)pG)->sol) ||
  1013.             (size && (endbuf[-1] != '\n')))
  1014.         {
  1015.             *endbuf++ = '\n';
  1016.             ++size;
  1017.         }
  1018.     }
  1019.  
  1020.     if (MSG_LNEWLN(flag) && !((struct Globals *)pG)->sol) {
  1021.         /* not at start of line:  want newline */
  1022. #ifdef OS2DLL
  1023.         if (!((struct Globals *)pG)->redirect_text) {
  1024. #endif
  1025.             putc('\n', outfp);          /* "more" will be off by one...oops */
  1026.             fflush(outfp);
  1027.             if (MSG_STDERR(flag) && ((struct Globals *)pG)->tflag &&
  1028.                 !isatty(1) && isatty(2))
  1029.             {
  1030.                 /* error output from testing redirected:  also send to stderr */
  1031.                 putc('\n', stderr);
  1032.                 fflush(stderr);
  1033.             }
  1034. #ifdef OS2DLL
  1035.         } else
  1036.            REDIRECTC('\n');
  1037. #endif
  1038.         ((struct Globals *)pG)->sol = TRUE;
  1039.     }
  1040.  
  1041.     /* put zipfile name, filename and/or error/warning keywords here */
  1042.  
  1043. #ifdef MORE
  1044.     /* room for --More-- and one line of overlap: */
  1045.     ((struct Globals *)pG)->height = SCREENLINES - 2;
  1046.  
  1047.     if (((struct Globals *)pG)->M_flag
  1048. #ifdef OS2DLL
  1049.          && !((struct Globals *)pG)->redirect_text
  1050. #endif
  1051.                                                  )
  1052.     {
  1053.         while (++p < endbuf) {
  1054.             if (*p == '\n') {
  1055.                 ++((struct Globals *)pG)->numlines;
  1056.                 if (((struct Globals *)pG)->numlines %
  1057.                     ((struct Globals *)pG)->height == 0L)    /* GRR: fix */
  1058.                 {
  1059.                     if ((error = WriteError(q, p-q+1, outfp)) != 0)
  1060.                         return error;
  1061.                     fflush(outfp);
  1062.                     ((struct Globals *)pG)->sol = TRUE;
  1063.                     q = p + 1;
  1064.                     (*((struct Globals *)pG)->mpause)((zvoid *)pG,
  1065.                       LoadFarString(MorePrompt), 1);
  1066.                 }
  1067.             }
  1068.         } /* end while */
  1069.         size = (ulg)(p - q);   /* remaining text */
  1070.     }
  1071. #endif /* MORE */
  1072.  
  1073.     if (size) {
  1074. #ifdef OS2DLL
  1075.         if (!((struct Globals *)pG)->redirect_text) {
  1076. #endif
  1077.             if ((error = WriteError(q, size, outfp)) != 0)
  1078.                 return error;
  1079.             fflush(outfp);
  1080.             if (MSG_STDERR(flag) && ((struct Globals *)pG)->tflag &&
  1081.                 !isatty(1) && isatty(2))
  1082.             {
  1083.                 /* error output from testing redirected:  also send to stderr */
  1084.                 if ((error = WriteError(q, size, stderr)) != 0)
  1085.                     return error;
  1086.                 fflush(stderr);
  1087.             }
  1088. #ifdef OS2DLL
  1089.         } else {                /* GRR:  this is ugly:  hide with macro */
  1090.             if ((error = REDIRECTPRINT(q, size)) != 0)
  1091.                 return error;
  1092.         }
  1093. #endif
  1094.         ((struct Globals *)pG)->sol = (endbuf[-1] == '\n');
  1095.     }
  1096.     return 0;
  1097.  
  1098. } /* end function UzpMessagePrnt() */
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104. #ifdef DLL
  1105.  
  1106. /*****************************/
  1107. /* Function UzpMessageNull() */  /* convenience routine for no output at all */
  1108. /*****************************/
  1109.  
  1110. int UZ_EXP UzpMessageNull(pG, buf, size, flag)
  1111.     zvoid *pG;    /* globals struct:  always passed */
  1112.     uch *buf;     /* preformatted string to be printed */
  1113.     ulg size;     /* length of string (may include nulls) */
  1114.     int flag;     /* flag bits */
  1115. {
  1116.     return 0;
  1117.  
  1118. } /* end function UzpMessageNull() */
  1119.  
  1120. #endif /* DLL */
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126. /***********************/
  1127. /* Function UzpInput() */   /* GRR:  this is a placeholder for now */
  1128. /***********************/
  1129.  
  1130. int UZ_EXP UzpInput(pG, buf, size, flag)
  1131.     zvoid *pG;    /* globals struct:  always passed */
  1132.     uch *buf;     /* preformatted string to be printed */
  1133.     int *size;    /* (address of) size of buf and of returned string */
  1134.     int flag;     /* flag bits (bit 0: no echo) */
  1135. {
  1136.     /* tell picky compilers to shut up about "unused variable" warnings */
  1137.     pG = pG; buf = buf; flag = flag;
  1138.  
  1139.     *size = 0;
  1140.     return 0;
  1141.  
  1142. } /* end function UzpInput() */
  1143.  
  1144.  
  1145.  
  1146.  
  1147.  
  1148. #if (!defined(WINDLL) && !defined(MACOS))
  1149.  
  1150. /***************************/
  1151. /* Function UzpMorePause() */
  1152. /***************************/
  1153.  
  1154. void UZ_EXP UzpMorePause(pG, prompt, flag)
  1155.     zvoid *pG;            /* globals struct:  always passed */
  1156.     ZCONST char *prompt;  /* "--More--" prompt */
  1157.     int flag;             /* 0 = any char OK; 1 = accept only '\n', ' ', q */
  1158. {
  1159.     uch c;
  1160.  
  1161. /*---------------------------------------------------------------------------
  1162.     Print a prompt and wait for the user to press a key, then erase prompt
  1163.     if possible.
  1164.   ---------------------------------------------------------------------------*/
  1165.  
  1166.     if (!((struct Globals *)pG)->sol)
  1167.         fprintf(stderr, "\n");
  1168.     /* numlines may or may not be used: */
  1169.     fprintf(stderr, prompt, ((struct Globals *)pG)->numlines);
  1170.     fflush(stderr);
  1171.     if (flag & 1) {
  1172.         do {
  1173.             c = (uch)FGETCH(0);
  1174.         } while (c != '\r' && c != '\n' && c != ' ' && c != 'q' && c != 'Q');
  1175.     } else
  1176.         c = (uch)FGETCH(0);
  1177.  
  1178.     /* newline was not echoed, so cover up prompt line */
  1179.     fprintf(stderr, LoadFarString(HidePrompt));
  1180.     fflush(stderr);
  1181.  
  1182.     if (ToLower(c) == 'q') {
  1183.         DESTROYGLOBALS()
  1184.         EXIT(PK_COOL);
  1185.     }
  1186.  
  1187.     ((struct Globals *)pG)->sol = TRUE;
  1188.  
  1189. } /* end function UzpMorePause() */
  1190.  
  1191. #endif /* !WINDLL && !MACOS */
  1192.  
  1193.  
  1194.  
  1195.  
  1196. #ifndef WINDLL
  1197.  
  1198. /**************************/
  1199. /* Function UzpPassword() */
  1200. /**************************/
  1201.  
  1202. int UZ_EXP UzpPassword (pG, rcnt, pwbuf, size, zfn, efn)
  1203.     zvoid *pG;         /* pointer to UnZip's internal global vars */
  1204.     int *rcnt;         /* retry counter */
  1205.     char *pwbuf;       /* buffer for password */
  1206.     int size;          /* size of password buffer */
  1207.     ZCONST char *zfn;  /* name of zip archive */
  1208.     ZCONST char *efn;  /* name of archive entry being processed */
  1209. {
  1210. #if CRYPT
  1211.     int r = IZ_PW_ENTERED;
  1212.     char *m;
  1213.     char *prompt;
  1214.  
  1215.     /* tell picky compilers to shut up about "unused variable" warnings */
  1216.     pG = pG;
  1217.  
  1218.     if (*rcnt == 0) {           /* First call for current entry */
  1219.         *rcnt = 2;
  1220.         if ((prompt = (char *)malloc(2*FILNAMSIZ + 15)) != (char *)NULL) {
  1221.             sprintf(prompt, "[%s] %s password: ", zfn, efn);
  1222.             m = prompt;
  1223.         } else
  1224.             m = "Enter password: ";
  1225.     } else {                    /* Retry call, previous password was wrong */
  1226.         (*rcnt)--;
  1227.         prompt = NULL;
  1228.         m = "password incorrect--reenter: ";
  1229.     }
  1230.  
  1231.     m = getp(__G__ m, pwbuf, size);
  1232.     if (prompt != (char *)NULL) {
  1233.         free(prompt);
  1234.     }
  1235.     if (m == (char *)NULL) {
  1236.         r = IZ_PW_ERROR;
  1237.     }
  1238.     else if (*pwbuf == '\0') {
  1239.         r = IZ_PW_CANCELALL;
  1240.     }
  1241.     return r;
  1242.  
  1243. #else /* !CRYPT */
  1244.     /* tell picky compilers to shut up about "unused variable" warnings */
  1245.     pG = pG; rcnt = rcnt; pwbuf = pwbuf; size = size; zfn = zfn; efn = efn;
  1246.  
  1247.     return IZ_PW_ERROR;  /* internal error; function should never get called */
  1248. #endif /* ?CRYPT */
  1249.  
  1250. } /* end function UzpPassword() */
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256. /**********************/
  1257. /* Function handler() */
  1258. /**********************/
  1259.  
  1260. void handler(signal)   /* upon interrupt, turn on echo and exit cleanly */
  1261.     int signal;
  1262. {
  1263.     GETGLOBALS();
  1264.  
  1265. #if !(defined(SIGBUS) || defined(SIGSEGV))      /* add a newline if not at */
  1266.     (*G.message)((zvoid *)&G, slide, 0L, 0x41); /*  start of line (to stderr; */
  1267. #endif                                          /*  slide[] should be safe) */
  1268.  
  1269.     echon();
  1270.  
  1271. #ifdef SIGBUS
  1272.     if (signal == SIGBUS) {
  1273.         Info(slide, 0x421, ((char *)slide, LoadFarString(ZipfileCorrupt),
  1274.           "bus error"));
  1275.         DESTROYGLOBALS()
  1276.         EXIT(PK_BADERR);
  1277.     }
  1278. #endif /* SIGBUS */
  1279.  
  1280. #ifdef SIGSEGV
  1281.     if (signal == SIGSEGV) {
  1282.         Info(slide, 0x421, ((char *)slide, LoadFarString(ZipfileCorrupt),
  1283.           "segmentation violation"));
  1284.         DESTROYGLOBALS()
  1285.         EXIT(PK_BADERR);
  1286.     }
  1287. #endif /* SIGSEGV */
  1288.  
  1289.     /* probably ctrl-C */
  1290.     DESTROYGLOBALS()
  1291. #if defined(AMIGA) && defined(__SASC)
  1292.     _abort();
  1293. #endif
  1294.     EXIT(IZ_CTRLC);       /* was EXIT(0), then EXIT(PK_ERR) */
  1295. }
  1296.  
  1297. #endif /* !WINDLL */
  1298.  
  1299.  
  1300.  
  1301.  
  1302. #if (!defined(VMS) && !defined(CMS_MVS))
  1303. #if (!defined(OS2) || defined(TIMESTAMP))
  1304.  
  1305. /* also used in amiga/filedate.c and win32/win32.c */
  1306. ZCONST ush ydays[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  1307.  
  1308. /*******************************/
  1309. /* Function dos_to_unix_time() */ /* used for freshening/updating/timestamps */
  1310. /*******************************/
  1311.  
  1312. time_t dos_to_unix_time(ddate, dtime)
  1313.     unsigned ddate, dtime;
  1314. {
  1315.     time_t m_time;
  1316.     int yr, mo, dy, hh, mm, ss;
  1317. #ifdef TOPS20
  1318. #   define YRBASE  1900
  1319.     struct tmx *tmx;
  1320.     char temp[20];
  1321. #else /* !TOPS20 */
  1322. #   define YRBASE  1970
  1323.     int leap;
  1324.     unsigned days;
  1325. #if (!defined(MACOS) && !defined(RISCOS))
  1326. #ifndef BSD4_4   /* GRR:  change to !defined(MODERN) ? */
  1327. #if (defined(BSD) || defined(MTS) || defined(__GO32__))
  1328.     struct timeb tbp;
  1329. #else /* !(BSD || MTS || __GO32__) */
  1330. #ifdef DECLARE_TIMEZONE
  1331.     extern time_t timezone;
  1332. #endif
  1333. #endif /* ?(BSD || MTS || __GO32__) */
  1334. #endif /* !BSD4_4 */
  1335. #endif /* !MACOS && !RISCOS */
  1336. #endif /* ?TOPS20 */
  1337.  
  1338.  
  1339.     /* dissect date */
  1340.     yr = ((ddate >> 9) & 0x7f) + (1980 - YRBASE);
  1341.     mo = ((ddate >> 5) & 0x0f) - 1;
  1342.     dy = (ddate & 0x1f) - 1;
  1343.  
  1344.     /* dissect time */
  1345.     hh = (dtime >> 11) & 0x1f;
  1346.     mm = (dtime >> 5) & 0x3f;
  1347.     ss = (dtime & 0x1f) * 2;
  1348.  
  1349. #ifdef TOPS20
  1350.     tmx = (struct tmx *)malloc(sizeof(struct tmx));
  1351.     sprintf (temp, "%02d/%02d/%02d %02d:%02d:%02d", mo+1, dy+1, yr, hh, mm, ss);
  1352.     time_parse(temp, tmx, (char *)0);
  1353.     m_time = time_make(tmx);
  1354.     free(tmx);
  1355.  
  1356. #else /* !TOPS20 */
  1357.  
  1358. /*---------------------------------------------------------------------------
  1359.     Calculate the number of seconds since the epoch, usually 1 January 1970.
  1360.   ---------------------------------------------------------------------------*/
  1361.  
  1362.     /* leap = # of leap yrs from YRBASE up to but not including current year */
  1363.     leap = ((yr + YRBASE - 1) / 4);   /* leap year base factor */
  1364.  
  1365.     /* calculate days from BASE to this year and add expired days this year */
  1366.     days = (yr * 365) + (leap - 492) + ydays[mo];
  1367.  
  1368.     /* if year is a leap year and month is after February, add another day */
  1369.     if ((mo > 1) && ((yr+YRBASE)%4 == 0) && ((yr+YRBASE) != 2100))
  1370.         ++days;                 /* OK through 2199 */
  1371.  
  1372.     /* convert date & time to seconds relative to 00:00:00, 01/01/YRBASE */
  1373.     m_time = (time_t)((unsigned long)(days + dy) * 86400L +
  1374.                       (unsigned long)hh * 3600L +
  1375.                       (unsigned long)(mm * 60 + ss));
  1376.       /* - 1;   MS-DOS times always rounded up to nearest even second */
  1377.     TTrace((stderr, "dos_to_unix_time:\n"));
  1378.     TTrace((stderr, "  m_time before timezone = %lu\n", (ulg)m_time));
  1379.  
  1380. /*---------------------------------------------------------------------------
  1381.     Adjust for local standard timezone offset.
  1382.   ---------------------------------------------------------------------------*/
  1383.  
  1384. #if (!defined(MACOS) && !defined(RISCOS) && !defined(QDOS) && !defined(TANDEM))
  1385. #if (defined(BSD) || defined(MTS) || defined(__GO32__))
  1386. #ifdef BSD4_4
  1387.     if ( (ddate >= (unsigned)DOSDATE_2038_01_18) &&
  1388.          (m_time < (time_t)0x70000000L) )
  1389.         m_time = U_TIME_T_MAX;  /* saturate in case of (unsigned) overflow */
  1390.     if (m_time < (time_t)0L)    /* a converted DOS time cannot be negative */
  1391.         m_time = S_TIME_T_MAX;  /*  -> saturate at max signed time_t value */
  1392.     m_time -= localtime(&m_time)->tm_gmtoff;    /* sec. EAST of GMT: subtr. */
  1393. #else /* !BSD4_4 */
  1394.     ftime(&tbp);                                /* get `timezone' */
  1395.     m_time += tbp.timezone * 60L;               /* seconds WEST of GMT:  add */
  1396. #endif /* ?BSD4_4 */
  1397. #else /* !(BSD || MTS || __GO32__) */
  1398. #ifdef WIN32
  1399.     {
  1400.         TIME_ZONE_INFORMATION tzinfo;
  1401.         DWORD res;
  1402.  
  1403.         /* account for timezone differences */
  1404.         res = GetTimeZoneInformation(&tzinfo);
  1405.         if (res != TIME_ZONE_ID_UNKNOWN)
  1406.             m_time += 60*(tzinfo.Bias);
  1407.     }
  1408. #else /* !WIN32 */
  1409.     tzset();                    /* set `timezone' variable */
  1410. #ifndef __BEOS__                /* BeOS DR8 has no timezones... */
  1411.     m_time += timezone;         /* seconds WEST of GMT:  add */
  1412. #endif
  1413. #endif /* ?WIN32 */
  1414. #endif /* ?(BSD || MTS || __GO32__) */
  1415.     TTrace((stderr, "  m_time after timezone =  %lu\n", (ulg)m_time));
  1416. #endif /* !MACOS && !RISCOS && !QDOS && !TANDEM */
  1417.  
  1418. /*---------------------------------------------------------------------------
  1419.     Adjust for local daylight savings (summer) time.
  1420.   ---------------------------------------------------------------------------*/
  1421.  
  1422. #ifndef BSD4_4  /* (DST already added to tm_gmtoff, so skip tm_isdst) */
  1423.     if ( (ddate >= (unsigned)DOSDATE_2038_01_18) &&
  1424.          (m_time < (time_t)0x70000000L) )
  1425.         m_time = U_TIME_T_MAX;  /* saturate in case of (unsigned) overflow */
  1426.     if (m_time < (time_t)0L)    /* a converted DOS time cannot be negative */
  1427.         m_time = S_TIME_T_MAX;  /*  -> saturate at max signed time_t value */
  1428.     TIMET_TO_NATIVE(m_time)     /* NOP unless MSC 7.0 or Macintosh */
  1429.     if (localtime((time_t *)&m_time)->tm_isdst)
  1430.         m_time -= 60L * 60L;    /* adjust for daylight savings time */
  1431.     NATIVE_TO_TIMET(m_time)     /* NOP unless MSC 7.0 or Macintosh */
  1432.     TTrace((stderr, "  m_time after DST =       %lu\n", (ulg)m_time));
  1433. #endif /* !BSD4_4 */
  1434.  
  1435. #endif /* ?TOPS20 */
  1436.  
  1437.     if ( (ddate >= (unsigned)DOSDATE_2038_01_18) &&
  1438.          (m_time < (time_t)0x70000000L) )
  1439.         m_time = U_TIME_T_MAX;  /* saturate in case of (unsigned) overflow */
  1440.     if (m_time < (time_t)0L)    /* a converted DOS time cannot be negative */
  1441.         m_time = S_TIME_T_MAX;  /*  -> saturate at max signed time_t value */
  1442.  
  1443.     return m_time;
  1444.  
  1445. } /* end function dos_to_unix_time() */
  1446.  
  1447. #endif /* !OS2 || TIMESTAMP */
  1448. #endif /* !VMS && !CMS_MVS */
  1449.  
  1450.  
  1451.  
  1452. #if (!defined(VMS) && !defined(OS2) && !defined(CMS_MVS))
  1453.  
  1454. /******************************/
  1455. /* Function check_for_newer() */  /* used for overwriting/freshening/updating */
  1456. /******************************/
  1457.  
  1458. int check_for_newer(__G__ filename)  /* return 1 if existing file is newer */
  1459.     __GDEF                           /*  or equal; 0 if older; -1 if doesn't */
  1460.     char *filename;                  /*  exist yet */
  1461. {
  1462.     time_t existing, archive;
  1463. #ifdef USE_EF_UT_TIME
  1464.     iztimes z_utime;
  1465. #endif
  1466. #ifdef AOS_VS
  1467.     long    dmm, ddd, dyy, dhh, dmin, dss;
  1468.  
  1469.  
  1470.     dmm = (lrec.last_mod_file_date >> 5) & 0x0f;
  1471.     ddd = lrec.last_mod_file_date & 0x1f;
  1472.     dyy = (lrec.last_mod_file_date >> 9) + 1980;
  1473.     dhh = (lrec.last_mod_file_time >> 11) & 0x1f;
  1474.     dmin = (lrec.last_mod_file_time >> 5) & 0x3f;
  1475.     dss = (lrec.last_mod_file_time & 0x1f) * 2;
  1476.  
  1477.     /* under AOS/VS, file times can only be set at creation time,
  1478.      * with the info in a special DG format.  Make sure we can create
  1479.      * it here - we delete it later & re-create it, whether or not
  1480.      * it exists now.
  1481.      */
  1482.     if (!zvs_create(filename, (((ulg)dgdate(dmm, ddd, dyy)) << 16) |
  1483.         (dhh*1800L + dmin*30L + dss/2L), -1L, -1L, (char *) -1, -1, -1, -1))
  1484.         return DOES_NOT_EXIST;
  1485. #endif /* AOS_VS */
  1486.  
  1487.     Trace((stderr, "check_for_newer:  doing stat(%s)\n", FnFilter1(filename)));
  1488.     if (SSTAT(filename, &G.statbuf)) {
  1489.         Trace((stderr,
  1490.           "check_for_newer:  stat(%s) returns %d:  file does not exist\n",
  1491.           FnFilter1(filename), stat(filename, &G.statbuf)));
  1492. #ifdef SYMLINKS
  1493.         Trace((stderr, "check_for_newer:  doing lstat(%s)\n",
  1494.           FnFilter1(filename)));
  1495.         /* GRR OPTION:  could instead do this test ONLY if G.symlnk is true */
  1496.         if (lstat(filename, &G.statbuf) == 0) {
  1497.             Trace((stderr,
  1498.               "check_for_newer:  lstat(%s) returns 0:  symlink does exist\n",
  1499.               FnFilter1(filename)));
  1500.             if (QCOND2)
  1501.                 Info(slide, 0, ((char *)slide, LoadFarString(FileIsSymLink),
  1502.                   FnFilter1(filename), " with no real file"));
  1503.             return EXISTS_AND_OLDER;   /* symlink dates are meaningless */
  1504.         }
  1505. #endif /* SYMLINKS */
  1506.         return DOES_NOT_EXIST;
  1507.     }
  1508.     Trace((stderr, "check_for_newer:  stat(%s) returns 0:  file exists\n",
  1509.       FnFilter1(filename)));
  1510.  
  1511. #ifdef SYMLINKS
  1512.     /* GRR OPTION:  could instead do this test ONLY if G.symlnk is true */
  1513.     if (lstat(filename, &G.statbuf) == 0 && S_ISLNK(G.statbuf.st_mode)) {
  1514.         Trace((stderr, "check_for_newer:  %s is a symbolic link\n",
  1515.           FnFilter1(filename)));
  1516.         if (QCOND2)
  1517.             Info(slide, 0, ((char *)slide, LoadFarString(FileIsSymLink),
  1518.               FnFilter1(filename), ""));
  1519.         return EXISTS_AND_OLDER;   /* symlink dates are meaningless */
  1520.     }
  1521. #endif /* SYMLINKS */
  1522.  
  1523.     NATIVE_TO_TIMET(G.statbuf.st_mtime)   /* NOP unless MSC 7.0 or Macintosh */
  1524.  
  1525. #ifdef USE_EF_UT_TIME
  1526.     /* The `Unix extra field mtime' should be used for comparison with the
  1527.      * time stamp of the existing file >>>ONLY<<< when the EF info is also
  1528.      * used to set the modification time of the extracted file.
  1529.      */
  1530.     if (G.extra_field &&
  1531.         (ef_scan_for_izux(G.extra_field, G.lrec.extra_field_length, 0,
  1532.                           G.lrec.last_mod_file_date, &z_utime, NULL)
  1533.          & EB_UT_FL_MTIME))
  1534.     {
  1535.         TTrace((stderr, "check_for_newer:  using Unix extra field mtime\n"));
  1536.         existing = G.statbuf.st_mtime;
  1537.         archive  = z_utime.mtime;
  1538.     } else {
  1539.         /* round up existing filetime to nearest 2 seconds for comparison,
  1540.          * but saturate in case of arithmetic overflow
  1541.          */
  1542.         existing = ((G.statbuf.st_mtime & 1) &&
  1543.                     (G.statbuf.st_mtime + 1 > G.statbuf.st_mtime)) ?
  1544.                    G.statbuf.st_mtime + 1 : G.statbuf.st_mtime;
  1545.         archive  = dos_to_unix_time(G.lrec.last_mod_file_date,
  1546.                                     G.lrec.last_mod_file_time);
  1547.     }
  1548. #else /* !USE_EF_UT_TIME */
  1549.     /* round up existing filetime to nearest 2 seconds for comparison,
  1550.      * but saturate in case of arithmetic overflow
  1551.      */
  1552.     existing = ((G.statbuf.st_mtime & 1) &&
  1553.                 (G.statbuf.st_mtime + 1 > G.statbuf.st_mtime)) ?
  1554.                G.statbuf.st_mtime + 1 : G.statbuf.st_mtime;
  1555.     archive  = dos_to_unix_time(G.lrec.last_mod_file_date,
  1556.                                 G.lrec.last_mod_file_time);
  1557. #endif /* ?USE_EF_UT_TIME */
  1558.  
  1559.     TTrace((stderr, "check_for_newer:  existing %lu, archive %lu, e-a %ld\n",
  1560.       (ulg)existing, (ulg)archive, (long)(existing-archive)));
  1561.  
  1562.     return (existing >= archive);
  1563.  
  1564. } /* end function check_for_newer() */
  1565.  
  1566. #endif /* !VMS && !OS2 && !CMS_MVS */
  1567.  
  1568.  
  1569.  
  1570.  
  1571.  
  1572. /************************/
  1573. /* Function do_string() */
  1574. /************************/
  1575.  
  1576. int do_string(__G__ len, option)      /* return PK-type error code */
  1577.     __GDEF
  1578.     unsigned int len;           /* without prototype, ush converted to this */
  1579.     int option;
  1580. {
  1581.     long comment_bytes_left, block_length;
  1582.     int error=PK_OK;
  1583.     ush extra_len;
  1584. #ifdef AMIGA
  1585.     char tmp_fnote[2 * AMIGA_FILENOTELEN];   /* extra room for squozen chars */
  1586. #endif
  1587.  
  1588.  
  1589. /*---------------------------------------------------------------------------
  1590.     This function processes arbitrary-length (well, usually) strings.  Four
  1591.     options are allowed:  SKIP, wherein the string is skipped (pretty logical,
  1592.     eh?); DISPLAY, wherein the string is printed to standard output after un-
  1593.     dergoing any necessary or unnecessary character conversions; DS_FN,
  1594.     wherein the string is put into the filename[] array after undergoing ap-
  1595.     propriate conversions (including case-conversion, if that is indicated:
  1596.     see the global variable pInfo->lcflag); and EXTRA_FIELD, wherein the
  1597.     `string' is assumed to be an extra field and is copied to the (freshly
  1598.     malloced) buffer G.extra_field.  The third option should be OK since
  1599.     filename is dimensioned at 1025, but we check anyway.
  1600.  
  1601.     The string, by the way, is assumed to start at the current file-pointer
  1602.     position; its length is given by len.  So start off by checking length
  1603.     of string:  if zero, we're already done.
  1604.   ---------------------------------------------------------------------------*/
  1605.  
  1606.     if (!len)
  1607.         return PK_COOL;
  1608.  
  1609.     switch (option) {
  1610.  
  1611.     /*
  1612.      * First case:  print string on standard output.  First set loop vari-
  1613.      * ables, then loop through the comment in chunks of OUTBUFSIZ bytes,
  1614.      * converting formats and printing as we go.  The second half of the
  1615.      * loop conditional was added because the file might be truncated, in
  1616.      * which case comment_bytes_left will remain at some non-zero value for
  1617.      * all time.  outbuf and slide are used as scratch buffers because they
  1618.      * are available (we should be either before or in between any file pro-
  1619.      * cessing).
  1620.      */
  1621.  
  1622.     case DISPLAY:
  1623.     case DISPL_8:
  1624.         comment_bytes_left = len;
  1625.         block_length = OUTBUFSIZ;    /* for the while statement, first time */
  1626.         while (comment_bytes_left > 0 && block_length > 0) {
  1627.             register uch *p = G.outbuf;
  1628.             register uch *q = G.outbuf;
  1629.  
  1630.             if ((block_length = readbuf(__G__ (char *)G.outbuf,
  1631.                    (unsigned) MIN((long)OUTBUFSIZ, comment_bytes_left))) == 0)
  1632.                 return PK_EOF;
  1633.             comment_bytes_left -= block_length;
  1634.  
  1635.             /* this is why we allocated an extra byte for outbuf:  terminate
  1636.              *  with zero (ASCIIZ) */
  1637.             G.outbuf[(unsigned)block_length] = '\0';
  1638.  
  1639.             /* remove all ASCII carriage returns from comment before printing
  1640.              * (since used before A_TO_N(), check for CR instead of '\r')
  1641.              */
  1642.             while (*p) {
  1643.                 while (*p == CR)
  1644.                     ++p;
  1645.                 *q++ = *p++;
  1646.             }
  1647.             /* could check whether (p - outbuf) == block_length here */
  1648.             *q = '\0';
  1649.  
  1650.             if (option == DISPL_8) {
  1651.                 /* translate the text coded in the entry's host-dependent
  1652.                    "extended ASCII" charset into the compiler's (system's)
  1653.                    internal text code page */
  1654.                 Ext_ASCII_TO_Native((char *)G.outbuf, G.pInfo->hostnum,
  1655.                                     G.crec.version_made_by[0]);
  1656. #ifdef WINDLL
  1657.                 /* translate to ANSI (RTL internal codepage may be OEM) */
  1658.                 INTERN_TO_ISO((char *)G.outbuf, (char *)G.outbuf);
  1659. #else /* !WINDLL */
  1660. #ifdef WIN32
  1661.                 if ( !IsWinNT() ) {
  1662.                     /* Win95 console uses OEM character coding */
  1663.                     INTERN_TO_OEM((char *)G.outbuf, (char *)G.outbuf);
  1664.                 } else {
  1665.                     /* >>>>> this guess has to be checked !!!! <<<<<< */
  1666.                     /* WinNT console uses ISO character coding */
  1667.                     INTERN_TO_ISO((char *)G.outbuf, (char *)G.outbuf);
  1668.                 }
  1669. #endif /* WIN32 */
  1670. #endif /* ?WINDLL */
  1671.             } else {
  1672.                 A_TO_N(G.outbuf);   /* translate string to native */
  1673.             }
  1674.  
  1675. #ifdef WINDLL
  1676.             /* ran out of local mem -- had to cheat */
  1677.             win_fprintf(stdout, len, (char *)G.outbuf);
  1678.             win_fprintf(stdout, 2, (char *)"\n\n");
  1679. #else /* !WINDLL */
  1680. #ifdef NATIVE          /* GRR:  can ANSI be used with EBCDIC? */
  1681.             (*G.message)((zvoid *)&G, G.outbuf, (ulg)(q-G.outbuf), 0);
  1682. #else /* ASCII */
  1683.             p = G.outbuf - 1;
  1684.             q = slide;
  1685.             while (*++p) {
  1686.                 int pause = FALSE;
  1687.  
  1688.                 if (*p == 0x1B) {          /* ASCII escape char */
  1689.                     *q++ = '^';
  1690.                     *q++ = '[';
  1691.                 } else if (*p == 0x13) {   /* ASCII ^S (pause) */
  1692.                     pause = TRUE;
  1693.                     if (p[1] == 0x0a)      /* ASCII LF */
  1694.                         *q++ = *++p;
  1695.                     else if (p[1] == 0x0d && p[2] == 0x0a) {  /* ASCII CR LF */
  1696.                         *q++ = *++p;
  1697.                         *q++ = *++p;
  1698.                     }
  1699.                 } else
  1700.                     *q++ = *p;
  1701.                 if ((unsigned)(q-slide) > WSIZE-3 || pause) {   /* flush */
  1702.                     (*G.message)((zvoid *)&G, slide, (ulg)(q-slide), 0);
  1703.                     q = slide;
  1704.                     if (pause && G.extract_flag) /* don't pause for list/test */
  1705.                         (*G.mpause)((zvoid *)&G, LoadFarString(QuitPrompt), 0);
  1706.                 }
  1707.             }
  1708.             (*G.message)((zvoid *)&G, slide, (ulg)(q-slide), 0);
  1709. #endif /* ?NATIVE */
  1710. #endif /* ?WINDLL */
  1711.         }
  1712.         /* add '\n' if not at start of line */
  1713.         (*G.message)((zvoid *)&G, slide, 0L, 0x40);
  1714.         break;
  1715.  
  1716.     /*
  1717.      * Second case:  read string into filename[] array.  The filename should
  1718.      * never ever be longer than FILNAMSIZ-1 (1024), but for now we'll check,
  1719.      * just to be sure.
  1720.      */
  1721.  
  1722.     case DS_FN:
  1723.         extra_len = 0;
  1724.         if (len >= FILNAMSIZ) {
  1725.             Info(slide, 0x401, ((char *)slide,
  1726.               LoadFarString(FilenameTooLongTrunc)));
  1727.             error = PK_WARN;
  1728.             extra_len = (ush)(len - FILNAMSIZ + 1);
  1729.             len = FILNAMSIZ - 1;
  1730.         }
  1731.         if (readbuf(__G__  G.filename, len) == 0)
  1732.             return PK_EOF;
  1733.         G.filename[len] = '\0';   /* terminate w/zero:  ASCIIZ */
  1734.  
  1735.         /* translate the Zip entry filename coded in host-dependent "extended
  1736.            ASCII" into the compiler's (system's) internal text code page */
  1737.         Ext_ASCII_TO_Native(G.filename, G.pInfo->hostnum,
  1738.                             G.crec.version_made_by[0]);
  1739.  
  1740.         if (G.pInfo->lcflag)      /* replace with lowercase filename */
  1741.             TOLOWER(G.filename, G.filename);
  1742.  
  1743.         if (G.pInfo->vollabel && len > 8 && G.filename[8] == '.') {
  1744.             char *p = G.filename+8;
  1745.             while (*p++)
  1746.                 p[-1] = *p;  /* disk label, and 8th char is dot:  remove dot */
  1747.         }
  1748.  
  1749.         if (!extra_len)         /* we're done here */
  1750.             break;
  1751.  
  1752.         /*
  1753.          * We truncated the filename, so print what's left and then fall
  1754.          * through to the SKIP routine.
  1755.          */
  1756.         Info(slide, 0x401, ((char *)slide, "[ %s ]\n", FnFilter1(G.filename)));
  1757.         len = extra_len;
  1758.         /*  FALL THROUGH...  */
  1759.  
  1760.     /*
  1761.      * Third case:  skip string, adjusting readbuf's internal variables
  1762.      * as necessary (and possibly skipping to and reading a new block of
  1763.      * data).
  1764.      */
  1765.  
  1766.     case SKIP:
  1767.         /* cur_zipfile_bufstart already takes account of extra_bytes, so don't
  1768.          * correct for it twice: */
  1769.         ZLSEEK(G.cur_zipfile_bufstart - G.extra_bytes +
  1770.                (G.inptr-G.inbuf) + len)
  1771.         break;
  1772.  
  1773.     /*
  1774.      * Fourth case:  assume we're at the start of an "extra field"; malloc
  1775.      * storage for it and read data into the allocated space.
  1776.      */
  1777.  
  1778.     case EXTRA_FIELD:
  1779.         if (G.extra_field != (uch *)NULL)
  1780.             free(G.extra_field);
  1781.         if ((G.extra_field = (uch *)malloc(len)) == (uch *)NULL) {
  1782.             Info(slide, 0x401, ((char *)slide, LoadFarString(ExtraFieldTooLong),
  1783.               len));
  1784.             /* cur_zipfile_bufstart already takes account of extra_bytes,
  1785.              * so don't correct for it twice: */
  1786.             ZLSEEK(G.cur_zipfile_bufstart - G.extra_bytes +
  1787.                    (G.inptr-G.inbuf) + len)
  1788.         } else
  1789.             if (readbuf(__G__  (char *)G.extra_field, len) == 0)
  1790.                 return PK_EOF;
  1791.         break;
  1792.  
  1793. #ifdef AMIGA
  1794.     /*
  1795.      * Fifth case, for the Amiga only:  take the comment that would ordinarily
  1796.      * be skipped over, and turn it into a 79 character string that will be
  1797.      * attached to the file as a "filenote" after it is extracted.
  1798.      */
  1799.  
  1800.     case FILENOTE:
  1801.         if ((extra_len = readbuf(__G__ tmp_fnote, (unsigned)
  1802.                                  MIN(len, 2 * AMIGA_FILENOTELEN - 1))) == 0)
  1803.             return PK_EOF;
  1804.         if ((len -= extra_len) > 0)     /* treat remainder as in case SKIP: */
  1805.             ZLSEEK(G.cur_zipfile_bufstart - G.extra_bytes
  1806.                    + (G.inptr - G.inbuf) + len)
  1807.         /* convert multi-line text into single line with no ctl-chars: */
  1808.         tmp_fnote[extra_len] = '\0';
  1809.         while ((short int) --extra_len >= 0)
  1810.             if ((unsigned) tmp_fnote[extra_len] < ' ')
  1811.                 if (tmp_fnote[extra_len+1] == ' ')     /* no excess */
  1812.                     strcpy(tmp_fnote+extra_len, tmp_fnote+extra_len+1);
  1813.                 else
  1814.                     tmp_fnote[extra_len] = ' ';
  1815.         tmp_fnote[AMIGA_FILENOTELEN - 1] = '\0';
  1816.         if (G.filenotes[G.filenote_slot])
  1817.             free(G.filenotes[G.filenote_slot]);     /* should not happen */
  1818.         G.filenotes[G.filenote_slot] = NULL;
  1819.         if (tmp_fnote[0]) {
  1820.             if (!(G.filenotes[G.filenote_slot] = malloc(strlen(tmp_fnote)+1)))
  1821.                 return PK_MEM;
  1822.             strcpy(G.filenotes[G.filenote_slot], tmp_fnote);
  1823.         }
  1824.         break;
  1825. #endif /* AMIGA */
  1826.  
  1827.     } /* end switch (option) */
  1828.  
  1829.     return error;
  1830.  
  1831. } /* end function do_string() */
  1832.  
  1833.  
  1834.  
  1835.  
  1836.  
  1837. /***********************/
  1838. /* Function makeword() */
  1839. /***********************/
  1840.  
  1841. ush makeword(b)
  1842.     uch *b;
  1843. {
  1844.     /*
  1845.      * Convert Intel style 'short' integer to non-Intel non-16-bit
  1846.      * host format.  This routine also takes care of byte-ordering.
  1847.      */
  1848.     return (ush)((b[1] << 8) | b[0]);
  1849. }
  1850.  
  1851.  
  1852.  
  1853.  
  1854.  
  1855. /***********************/
  1856. /* Function makelong() */
  1857. /***********************/
  1858.  
  1859. ulg makelong(sig)
  1860.     uch *sig;
  1861. {
  1862.     /*
  1863.      * Convert intel style 'long' variable to non-Intel non-16-bit
  1864.      * host format.  This routine also takes care of byte-ordering.
  1865.      */
  1866.     return (((ulg)sig[3]) << 24)
  1867.         + (((ulg)sig[2]) << 16)
  1868.         + (((ulg)sig[1]) << 8)
  1869.         + ((ulg)sig[0]);
  1870. }
  1871.  
  1872.  
  1873.  
  1874. #if CRYPT
  1875.  
  1876. #ifdef NEED_STR2ISO
  1877. /**********************/
  1878. /* Function str2iso() */
  1879. /**********************/
  1880.  
  1881. char *str2iso(dst, src)
  1882.     char *dst;                          /* destination buffer */
  1883.     register ZCONST char *src;          /* source string */
  1884. {
  1885. #ifdef INTERN_TO_ISO
  1886.     INTERN_TO_ISO(src, dst);
  1887. #else
  1888.     register uch c;
  1889.     register char *dstp = dst;
  1890.  
  1891.     do {
  1892.         c = (uch)foreign(*src++);
  1893.         *dstp++ = (char)ASCII2ISO(c);
  1894.     } while (c != '\0');
  1895. #endif
  1896.  
  1897.     return dst;
  1898. }
  1899. #endif /* NEED_STR2ISO */
  1900.  
  1901.  
  1902. #ifdef NEED_STR2OEM
  1903. /**********************/
  1904. /* Function str2oem() */
  1905. /**********************/
  1906.  
  1907. char *str2oem(dst, src)
  1908.     char *dst;                          /* destination buffer */
  1909.     register ZCONST char *src;          /* source string */
  1910. {
  1911. #ifdef INTERN_TO_OEM
  1912.     INTERN_TO_OEM(src, dst);
  1913. #else
  1914.     register uch c;
  1915.     register char *dstp = dst;
  1916.  
  1917.     do {
  1918.         c = (uch)foreign(*src++);
  1919.         *dstp++ = (char)ASCII2OEM(c);
  1920.     } while (c != '\0');
  1921. #endif
  1922.  
  1923.     return dst;
  1924. }
  1925. #endif /* NEED_STR2OEM */
  1926.  
  1927. #endif /* CRYPT */
  1928.  
  1929.  
  1930. #ifdef ZMEM  /* memset/memcpy for systems without either them or bzero/bcopy */
  1931.              /* (no known systems as of 960211) */
  1932.  
  1933. /*********************/
  1934. /* Function memset() */
  1935. /*********************/
  1936.  
  1937. zvoid *memset(buf, init, len)
  1938.     register zvoid *buf;        /* buffer location */
  1939.     register int init;          /* initializer character */
  1940.     register unsigned int len;  /* length of the buffer */
  1941. {
  1942.     zvoid *start;
  1943.  
  1944.     start = buf;
  1945.     while (len--)
  1946.         *((char *)buf++) = (char)init;
  1947.     return start;
  1948. }
  1949.  
  1950.  
  1951.  
  1952.  
  1953.  
  1954. /*********************/
  1955. /* Function memcpy() */
  1956. /*********************/
  1957.  
  1958. zvoid *memcpy(dst, src, len)
  1959.     register zvoid *dst;
  1960.     register ZCONST zvoid *src;
  1961.     register unsigned int len;
  1962. {
  1963.     zvoid *start;
  1964.  
  1965.     start = dst;
  1966.     while (len-- > 0)
  1967.         *((char *)dst)++ = *((char *)src)++;
  1968.     return start;
  1969. }
  1970.  
  1971. #endif /* ZMEM */
  1972.  
  1973.  
  1974.  
  1975.  
  1976.  
  1977. /************************/
  1978. /* Function zstrnicmp() */
  1979. /************************/
  1980.  
  1981. int zstrnicmp(s1, s2, n)
  1982.     register ZCONST char *s1, *s2;
  1983.     register unsigned n;
  1984. {
  1985.     for (; n > 0;  --n, ++s1, ++s2) {
  1986.  
  1987.         if (ToLower(*s1) != ToLower(*s2))
  1988.             /* test includes early termination of one string */
  1989.             return (ToLower(*s1) < ToLower(*s2))? -1 : 1;
  1990.  
  1991.         if (*s1 == '\0')   /* both strings terminate early */
  1992.             return 0;
  1993.     }
  1994.     return 0;
  1995. }
  1996.  
  1997.  
  1998.  
  1999.  
  2000.  
  2001. #ifdef REGULUS  /* returns the inode number on success(!)...argh argh argh */
  2002. #  undef stat
  2003.  
  2004. /********************/
  2005. /* Function zstat() */
  2006. /********************/
  2007.  
  2008. int zstat(p, s)
  2009.     char *p;
  2010.     struct stat *s;
  2011. {
  2012.     return (stat(p,s) >= 0? 0 : (-1));
  2013. }
  2014.  
  2015. #endif /* REGULUS */
  2016.  
  2017.  
  2018.  
  2019.  
  2020.  
  2021. #ifdef SMALL_MEM
  2022.  
  2023. /*******************************/
  2024. /*  Function fLoadFarString()  */   /* (and friends...) */
  2025. /*******************************/
  2026.  
  2027. char *fLoadFarString(__GPRO__ const char Far *sz)
  2028. {
  2029.     (void)zfstrcpy(G.rgchBigBuffer, sz);
  2030.     return G.rgchBigBuffer;
  2031. }
  2032.  
  2033. char *fLoadFarStringSmall(__GPRO__ const char Far *sz)
  2034. {
  2035.     (void)zfstrcpy(G.rgchSmallBuffer, sz);
  2036.     return G.rgchSmallBuffer;
  2037. }
  2038.  
  2039. char *fLoadFarStringSmall2(__GPRO__ const char Far *sz)
  2040. {
  2041.     (void)zfstrcpy(G.rgchSmallBuffer2, sz);
  2042.     return G.rgchSmallBuffer2;
  2043. }
  2044.  
  2045.  
  2046.  
  2047.  
  2048. #if (!defined(_MSC_VER) || (_MSC_VER < 600))
  2049. /*************************/
  2050. /*  Function zfstrcpy()  */   /* portable clone of _fstrcpy() */
  2051. /*************************/
  2052.  
  2053. char Far * Far zfstrcpy(char Far *s1, const char Far *s2)
  2054. {
  2055.     char Far *p = s1;
  2056.  
  2057.     while ((*s1++ = *s2++) != '\0');
  2058.     return p;
  2059. }
  2060. #endif /* !_MSC_VER || (_MSC_VER < 600) */
  2061.  
  2062. #endif /* SMALL_MEM */
  2063.