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