Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2856 lines
92KB

  1. /*
  2. Copyright (c) 1990-2009 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2009-Jan-02 or later
  4. (the contents of which are also included in unzip.h) for terms of use.
  5. If, for some reason, all these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  7. */
  8. /*---------------------------------------------------------------------------
  9. fileio.c
  10. This file contains routines for doing direct but relatively generic input/
  11. output, file-related sorts of things, plus some miscellaneous stuff. Most
  12. of the stuff has to do with opening, closing, reading and/or writing files.
  13. Contains: open_input_file()
  14. open_outfile() (not: VMS, AOS/VS, CMSMVS, MACOS, TANDEM)
  15. undefer_input()
  16. defer_leftover_input()
  17. readbuf()
  18. readbyte()
  19. fillinbuf()
  20. seek_zipf()
  21. flush() (non-VMS)
  22. is_vms_varlen_txt() (non-VMS, VMS_TEXT_CONV only)
  23. disk_error() (non-VMS)
  24. UzpMessagePrnt()
  25. UzpMessageNull() (DLL only)
  26. UzpInput()
  27. UzpMorePause()
  28. UzpPassword() (non-WINDLL)
  29. handler()
  30. dos_to_unix_time() (non-VMS, non-VM/CMS, non-MVS)
  31. check_for_newer() (non-VMS, non-OS/2, non-VM/CMS, non-MVS)
  32. do_string()
  33. makeword()
  34. makelong()
  35. makeint64()
  36. fzofft()
  37. str2iso() (CRYPT && NEED_STR2ISO, only)
  38. str2oem() (CRYPT && NEED_STR2OEM, only)
  39. memset() (ZMEM only)
  40. memcpy() (ZMEM only)
  41. zstrnicmp() (NO_STRNICMP only)
  42. zstat() (REGULUS only)
  43. plastchar() (_MBCS only)
  44. uzmbclen() (_MBCS && NEED_UZMBCLEN, only)
  45. uzmbschr() (_MBCS && NEED_UZMBSCHR, only)
  46. uzmbsrchr() (_MBCS && NEED_UZMBSRCHR, only)
  47. fLoadFarString() (SMALL_MEM only)
  48. fLoadFarStringSmall() (SMALL_MEM only)
  49. fLoadFarStringSmall2() (SMALL_MEM only)
  50. zfstrcpy() (SMALL_MEM only)
  51. zfstrcmp() (SMALL_MEM && !(SFX || FUNZIP) only)
  52. ---------------------------------------------------------------------------*/
  53. #define __FILEIO_C /* identifies this source module */
  54. #define UNZIP_INTERNAL
  55. #include "unzip.h"
  56. #ifdef WINDLL
  57. # ifdef POCKET_UNZIP
  58. # include "wince/intrface.h"
  59. # else
  60. # include "windll/windll.h"
  61. # endif
  62. # include <setjmp.h>
  63. #endif
  64. #include "crc32.h"
  65. #include "crypt.h"
  66. #include "ttyio.h"
  67. /* setup of codepage conversion for decryption passwords */
  68. #if CRYPT
  69. # if (defined(CRYP_USES_ISO2OEM) && !defined(IZ_ISO2OEM_ARRAY))
  70. # define IZ_ISO2OEM_ARRAY /* pull in iso2oem[] table */
  71. # endif
  72. # if (defined(CRYP_USES_OEM2ISO) && !defined(IZ_OEM2ISO_ARRAY))
  73. # define IZ_OEM2ISO_ARRAY /* pull in oem2iso[] table */
  74. # endif
  75. #endif
  76. #include "ebcdic.h" /* definition/initialization of ebcdic[] */
  77. /*
  78. Note: Under Windows, the maximum size of the buffer that can be used
  79. with any of the *printf calls is 16,384, so win_fprintf was used to
  80. feed the fprintf clone no more than 16K chunks at a time. This should
  81. be valid for anything up to 64K (and probably beyond, assuming your
  82. buffers are that big).
  83. */
  84. #ifdef WINDLL
  85. # define WriteError(buf,len,strm) \
  86. (win_fprintf(pG, strm, (extent)len, (char far *)buf) != (int)(len))
  87. #else /* !WINDLL */
  88. # ifdef USE_FWRITE
  89. # define WriteError(buf,len,strm) \
  90. ((extent)fwrite((char *)(buf),1,(extent)(len),strm) != (extent)(len))
  91. # else
  92. # define WriteError(buf,len,strm) \
  93. ((extent)write(fileno(strm),(char *)(buf),(extent)(len)) != (extent)(len))
  94. # endif
  95. #endif /* ?WINDLL */
  96. /*
  97. 2005-09-16 SMS.
  98. On VMS, when output is redirected to a file, as in a command like
  99. "PIPE UNZIP -v > X.OUT", the output file is created with VFC record
  100. format, and multiple calls to write() or fwrite() will produce multiple
  101. records, even when there's no newline terminator in the buffer.
  102. The result is unsightly output with spurious newlines. Using fprintf()
  103. instead of write() here, and disabling a fflush(stdout) in UzpMessagePrnt()
  104. below, together seem to solve the problem.
  105. According to the C RTL manual, "The write and decc$record_write
  106. functions always generate at least one record." Also, "[T]he fwrite
  107. function always generates at least <number_items> records." So,
  108. "fwrite(buf, len, 1, strm)" is much better ("1" record) than
  109. "fwrite(buf, 1, len, strm)" ("len" (1-character) records, _really_
  110. ugly), but neither is better than write(). Similarly, "The fflush
  111. function always generates a record if there is unwritten data in the
  112. buffer." Apparently fprintf() buffers the stuff somewhere, and puts
  113. out a record (only) when it sees a newline.
  114. */
  115. #ifdef VMS
  116. # define WriteTxtErr(buf,len,strm) \
  117. ((extent)fprintf(strm, "%.*s", len, buf) != (extent)(len))
  118. #else
  119. # define WriteTxtErr(buf,len,strm) WriteError(buf,len,strm)
  120. #endif
  121. #if (defined(USE_DEFLATE64) && defined(__16BIT__))
  122. static int partflush OF((__GPRO__ uch *rawbuf, ulg size, int unshrink));
  123. #endif
  124. #ifdef VMS_TEXT_CONV
  125. static int is_vms_varlen_txt OF((__GPRO__ uch *ef_buf, unsigned ef_len));
  126. #endif
  127. static int disk_error OF((__GPRO));
  128. /****************************/
  129. /* Strings used in fileio.c */
  130. /****************************/
  131. static ZCONST char Far CannotOpenZipfile[] =
  132. "error: cannot open zipfile [ %s ]\n %s\n";
  133. #if (!defined(VMS) && !defined(AOS_VS) && !defined(CMS_MVS) && !defined(MACOS))
  134. #if (!defined(TANDEM))
  135. #if (defined(ATH_BEO_THS_UNX) || defined(DOS_FLX_NLM_OS2_W32))
  136. static ZCONST char Far CannotDeleteOldFile[] =
  137. "error: cannot delete old %s\n %s\n";
  138. #ifdef UNIXBACKUP
  139. static ZCONST char Far CannotRenameOldFile[] =
  140. "error: cannot rename old %s\n %s\n";
  141. static ZCONST char Far BackupSuffix[] = "~";
  142. #endif
  143. #endif /* ATH_BEO_THS_UNX || DOS_FLX_NLM_OS2_W32 */
  144. #ifdef NOVELL_BUG_FAILSAFE
  145. static ZCONST char Far NovellBug[] =
  146. "error: %s: stat() says does not exist, but fopen() found anyway\n";
  147. #endif
  148. static ZCONST char Far CannotCreateFile[] =
  149. "error: cannot create %s\n %s\n";
  150. #endif /* !TANDEM */
  151. #endif /* !VMS && !AOS_VS && !CMS_MVS && !MACOS */
  152. static ZCONST char Far ReadError[] = "error: zipfile read error\n";
  153. static ZCONST char Far FilenameTooLongTrunc[] =
  154. "warning: filename too long--truncating.\n";
  155. #ifdef UNICODE_SUPPORT
  156. static ZCONST char Far UFilenameTooLongTrunc[] =
  157. "warning: Converted unicode filename too long--truncating.\n";
  158. #endif
  159. static ZCONST char Far ExtraFieldTooLong[] =
  160. "warning: extra field too long (%d). Ignoring...\n";
  161. #ifdef WINDLL
  162. static ZCONST char Far DiskFullQuery[] =
  163. "%s: write error (disk full?).\n";
  164. #else
  165. static ZCONST char Far DiskFullQuery[] =
  166. "%s: write error (disk full?). Continue? (y/n/^C) ";
  167. static ZCONST char Far ZipfileCorrupt[] =
  168. "error: zipfile probably corrupt (%s)\n";
  169. # ifdef SYMLINKS
  170. static ZCONST char Far FileIsSymLink[] =
  171. "%s exists and is a symbolic link%s.\n";
  172. # endif
  173. # ifdef MORE
  174. static ZCONST char Far MorePrompt[] = "--More--(%lu)";
  175. # endif
  176. static ZCONST char Far QuitPrompt[] =
  177. "--- Press `Q' to quit, or any other key to continue ---";
  178. static ZCONST char Far HidePrompt[] = /* "\r \r"; */
  179. "\r \r";
  180. # if CRYPT
  181. # ifdef MACOS
  182. /* SPC: are names on MacOS REALLY so much longer than elsewhere ??? */
  183. static ZCONST char Far PasswPrompt[] = "[%s]\n %s password: ";
  184. # else
  185. static ZCONST char Far PasswPrompt[] = "[%s] %s password: ";
  186. # endif
  187. static ZCONST char Far PasswPrompt2[] = "Enter password: ";
  188. static ZCONST char Far PasswRetry[] = "password incorrect--reenter: ";
  189. # endif /* CRYPT */
  190. #endif /* !WINDLL */
  191. /******************************/
  192. /* Function open_input_file() */
  193. /******************************/
  194. int open_input_file(__G) /* return 1 if open failed */
  195. __GDEF
  196. {
  197. /*
  198. * open the zipfile for reading and in BINARY mode to prevent cr/lf
  199. * translation, which would corrupt the bitstreams
  200. */
  201. #ifdef VMS
  202. G.zipfd = open(G.zipfn, O_RDONLY, 0, OPNZIP_RMS_ARGS);
  203. #else /* !VMS */
  204. #ifdef MACOS
  205. G.zipfd = open(G.zipfn, 0);
  206. #else /* !MACOS */
  207. #ifdef CMS_MVS
  208. G.zipfd = vmmvs_open_infile(__G);
  209. #else /* !CMS_MVS */
  210. #ifdef USE_STRM_INPUT
  211. G.zipfd = fopen(G.zipfn, FOPR);
  212. #else /* !USE_STRM_INPUT */
  213. G.zipfd = open(G.zipfn, O_RDONLY | O_BINARY);
  214. #endif /* ?USE_STRM_INPUT */
  215. #endif /* ?CMS_MVS */
  216. #endif /* ?MACOS */
  217. #endif /* ?VMS */
  218. #ifdef USE_STRM_INPUT
  219. if (G.zipfd == NULL)
  220. #else
  221. /* if (G.zipfd < 0) */ /* no good for Windows CE port */
  222. if (G.zipfd == -1)
  223. #endif
  224. {
  225. Info(slide, 0x401, ((char *)slide, LoadFarString(CannotOpenZipfile),
  226. G.zipfn, strerror(errno)));
  227. return 1;
  228. }
  229. return 0;
  230. } /* end function open_input_file() */
  231. #if (!defined(VMS) && !defined(AOS_VS) && !defined(CMS_MVS) && !defined(MACOS))
  232. #if (!defined(TANDEM))
  233. /***************************/
  234. /* Function open_outfile() */
  235. /***************************/
  236. int open_outfile(__G) /* return 1 if fail */
  237. __GDEF
  238. {
  239. #ifdef DLL
  240. if (G.redirect_data)
  241. return (redirect_outfile(__G) == FALSE);
  242. #endif
  243. #ifdef QDOS
  244. QFilename(__G__ G.filename);
  245. #endif
  246. #if (defined(DOS_FLX_NLM_OS2_W32) || defined(ATH_BEO_THS_UNX))
  247. #ifdef BORLAND_STAT_BUG
  248. /* Borland 5.0's stat() barfs if the filename has no extension and the
  249. * file doesn't exist. */
  250. if (access(G.filename, 0) == -1) {
  251. FILE *tmp = fopen(G.filename, "wb+");
  252. /* file doesn't exist, so create a dummy file to keep stat() from
  253. * failing (will be over-written anyway) */
  254. fputc('0', tmp); /* just to have something in the file */
  255. fclose(tmp);
  256. }
  257. #endif /* BORLAND_STAT_BUG */
  258. #ifdef SYMLINKS
  259. if (SSTAT(G.filename, &G.statbuf) == 0 ||
  260. lstat(G.filename, &G.statbuf) == 0)
  261. #else
  262. if (SSTAT(G.filename, &G.statbuf) == 0)
  263. #endif /* ?SYMLINKS */
  264. {
  265. Trace((stderr, "open_outfile: stat(%s) returns 0: file exists\n",
  266. FnFilter1(G.filename)));
  267. #ifdef UNIXBACKUP
  268. if (uO.B_flag) { /* do backup */
  269. char *tname;
  270. z_stat tmpstat;
  271. int blen, flen, tlen;
  272. blen = strlen(BackupSuffix);
  273. flen = strlen(G.filename);
  274. tlen = flen + blen + 6; /* includes space for 5 digits */
  275. if (tlen >= FILNAMSIZ) { /* in case name is too long, truncate */
  276. tname = (char *)malloc(FILNAMSIZ);
  277. if (tname == NULL)
  278. return 1; /* in case we run out of space */
  279. tlen = FILNAMSIZ - 1 - blen;
  280. strcpy(tname, G.filename); /* make backup name */
  281. tname[tlen] = '\0';
  282. if (flen > tlen) flen = tlen;
  283. tlen = FILNAMSIZ;
  284. } else {
  285. tname = (char *)malloc(tlen);
  286. if (tname == NULL)
  287. return 1; /* in case we run out of space */
  288. strcpy(tname, G.filename); /* make backup name */
  289. }
  290. strcpy(tname+flen, BackupSuffix);
  291. if (IS_OVERWRT_ALL) {
  292. /* If there is a previous backup file, delete it,
  293. * otherwise the following rename operation may fail.
  294. */
  295. if (SSTAT(tname, &tmpstat) == 0)
  296. unlink(tname);
  297. } else {
  298. /* Check if backupname exists, and, if it's true, try
  299. * appending numbers of up to 5 digits (or the maximum
  300. * "unsigned int" number on 16-bit systems) to the
  301. * BackupSuffix, until an unused name is found.
  302. */
  303. unsigned maxtail, i;
  304. char *numtail = tname + flen + blen;
  305. /* take account of the "unsigned" limit on 16-bit systems: */
  306. maxtail = ( ((~0) >= 99999L) ? 99999 : (~0) );
  307. switch (tlen - flen - blen - 1) {
  308. case 4: maxtail = 9999; break;
  309. case 3: maxtail = 999; break;
  310. case 2: maxtail = 99; break;
  311. case 1: maxtail = 9; break;
  312. case 0: maxtail = 0; break;
  313. }
  314. /* while filename exists */
  315. for (i = 0; (i < maxtail) && (SSTAT(tname, &tmpstat) == 0);)
  316. sprintf(numtail,"%u", ++i);
  317. }
  318. if (rename(G.filename, tname) != 0) { /* move file */
  319. Info(slide, 0x401, ((char *)slide,
  320. LoadFarString(CannotRenameOldFile),
  321. FnFilter1(G.filename), strerror(errno)));
  322. free(tname);
  323. return 1;
  324. }
  325. Trace((stderr, "open_outfile: %s now renamed into %s\n",
  326. FnFilter1(G.filename), FnFilter2(tname)));
  327. free(tname);
  328. } else
  329. #endif /* UNIXBACKUP */
  330. {
  331. #ifdef DOS_FLX_OS2_W32
  332. if (!(G.statbuf.st_mode & S_IWRITE)) {
  333. Trace((stderr,
  334. "open_outfile: existing file %s is read-only\n",
  335. FnFilter1(G.filename)));
  336. chmod(G.filename, S_IREAD | S_IWRITE);
  337. Trace((stderr, "open_outfile: %s now writable\n",
  338. FnFilter1(G.filename)));
  339. }
  340. #endif /* DOS_FLX_OS2_W32 */
  341. #ifdef NLM
  342. /* Give the file read/write permission (non-POSIX shortcut) */
  343. chmod(G.filename, 0);
  344. #endif /* NLM */
  345. if (unlink(G.filename) != 0) {
  346. Info(slide, 0x401, ((char *)slide,
  347. LoadFarString(CannotDeleteOldFile),
  348. FnFilter1(G.filename), strerror(errno)));
  349. return 1;
  350. }
  351. Trace((stderr, "open_outfile: %s now deleted\n",
  352. FnFilter1(G.filename)));
  353. }
  354. }
  355. #endif /* DOS_FLX_NLM_OS2_W32 || ATH_BEO_THS_UNX */
  356. #ifdef RISCOS
  357. if (SWI_OS_File_7(G.filename,0xDEADDEAD,0xDEADDEAD,G.lrec.ucsize)!=NULL) {
  358. Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
  359. FnFilter1(G.filename), strerror(errno)));
  360. return 1;
  361. }
  362. #endif /* RISCOS */
  363. #ifdef TOPS20
  364. char *tfilnam;
  365. if ((tfilnam = (char *)malloc(2*strlen(G.filename)+1)) == (char *)NULL)
  366. return 1;
  367. strcpy(tfilnam, G.filename);
  368. upper(tfilnam);
  369. enquote(tfilnam);
  370. if ((G.outfile = fopen(tfilnam, FOPW)) == (FILE *)NULL) {
  371. Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
  372. tfilnam, strerror(errno)));
  373. free(tfilnam);
  374. return 1;
  375. }
  376. free(tfilnam);
  377. #else /* !TOPS20 */
  378. #ifdef MTS
  379. if (uO.aflag)
  380. G.outfile = zfopen(G.filename, FOPWT);
  381. else
  382. G.outfile = zfopen(G.filename, FOPW);
  383. if (G.outfile == (FILE *)NULL) {
  384. Info(slide, 1, ((char *)slide, LoadFarString(CannotCreateFile),
  385. FnFilter1(G.filename), strerror(errno)));
  386. return 1;
  387. }
  388. #else /* !MTS */
  389. #ifdef DEBUG
  390. Info(slide, 1, ((char *)slide,
  391. "open_outfile: doing fopen(%s) for reading\n", FnFilter1(G.filename)));
  392. if ((G.outfile = zfopen(G.filename, FOPR)) == (FILE *)NULL)
  393. Info(slide, 1, ((char *)slide,
  394. "open_outfile: fopen(%s) for reading failed: does not exist\n",
  395. FnFilter1(G.filename)));
  396. else {
  397. Info(slide, 1, ((char *)slide,
  398. "open_outfile: fopen(%s) for reading succeeded: file exists\n",
  399. FnFilter1(G.filename)));
  400. fclose(G.outfile);
  401. }
  402. #endif /* DEBUG */
  403. #ifdef NOVELL_BUG_FAILSAFE
  404. if (G.dne && ((G.outfile = zfopen(G.filename, FOPR)) != (FILE *)NULL)) {
  405. Info(slide, 0x401, ((char *)slide, LoadFarString(NovellBug),
  406. FnFilter1(G.filename)));
  407. fclose(G.outfile);
  408. return 1; /* with "./" fix in checkdir(), should never reach here */
  409. }
  410. #endif /* NOVELL_BUG_FAILSAFE */
  411. Trace((stderr, "open_outfile: doing fopen(%s) for writing\n",
  412. FnFilter1(G.filename)));
  413. {
  414. #if defined(ATH_BE_UNX) || defined(AOS_VS) || defined(QDOS) || defined(TANDEM)
  415. mode_t umask_sav = umask(0077);
  416. #endif
  417. #if defined(SYMLINKS) || defined(QLZIP)
  418. /* These features require the ability to re-read extracted data from
  419. the output files. Output files are created with Read&Write access.
  420. */
  421. G.outfile = zfopen(G.filename, FOPWR);
  422. #else
  423. G.outfile = zfopen(G.filename, FOPW);
  424. #endif
  425. #if defined(ATH_BE_UNX) || defined(AOS_VS) || defined(QDOS) || defined(TANDEM)
  426. umask(umask_sav);
  427. #endif
  428. }
  429. if (G.outfile == (FILE *)NULL) {
  430. Info(slide, 0x401, ((char *)slide, LoadFarString(CannotCreateFile),
  431. FnFilter1(G.filename), strerror(errno)));
  432. return 1;
  433. }
  434. Trace((stderr, "open_outfile: fopen(%s) for writing succeeded\n",
  435. FnFilter1(G.filename)));
  436. #endif /* !MTS */
  437. #endif /* !TOPS20 */
  438. #ifdef USE_FWRITE
  439. #ifdef DOS_NLM_OS2_W32
  440. /* 16-bit MSC: buffer size must be strictly LESS than 32K (WSIZE): bogus */
  441. setbuf(G.outfile, (char *)NULL); /* make output unbuffered */
  442. #else /* !DOS_NLM_OS2_W32 */
  443. #ifndef RISCOS
  444. #ifdef _IOFBF /* make output fully buffered (works just about like write()) */
  445. setvbuf(G.outfile, (char *)slide, _IOFBF, WSIZE);
  446. #else
  447. setbuf(G.outfile, (char *)slide);
  448. #endif
  449. #endif /* !RISCOS */
  450. #endif /* ?DOS_NLM_OS2_W32 */
  451. #endif /* USE_FWRITE */
  452. #ifdef OS2_W32
  453. /* preallocate the final file size to prevent file fragmentation */
  454. SetFileSize(G.outfile, G.lrec.ucsize);
  455. #endif
  456. return 0;
  457. } /* end function open_outfile() */
  458. #endif /* !TANDEM */
  459. #endif /* !VMS && !AOS_VS && !CMS_MVS && !MACOS */
  460. /*
  461. * These functions allow NEXTBYTE to function without needing two bounds
  462. * checks. Call defer_leftover_input() if you ever have filled G.inbuf
  463. * by some means other than readbyte(), and you then want to start using
  464. * NEXTBYTE. When going back to processing bytes without NEXTBYTE, call
  465. * undefer_input(). For example, extract_or_test_member brackets its
  466. * central section that does the decompression with these two functions.
  467. * If you need to check the number of bytes remaining in the current
  468. * file while using NEXTBYTE, check (G.csize + G.incnt), not G.csize.
  469. */
  470. /****************************/
  471. /* function undefer_input() */
  472. /****************************/
  473. void undefer_input(__G)
  474. __GDEF
  475. {
  476. if (G.incnt > 0)
  477. G.csize += G.incnt;
  478. if (G.incnt_leftover > 0) {
  479. /* We know that "(G.csize < MAXINT)" so we can cast G.csize to int:
  480. * This condition was checked when G.incnt_leftover was set > 0 in
  481. * defer_leftover_input(), and it is NOT allowed to touch G.csize
  482. * before calling undefer_input() when (G.incnt_leftover > 0)
  483. * (single exception: see read_byte()'s "G.csize <= 0" handling) !!
  484. */
  485. G.incnt = G.incnt_leftover + (int)G.csize;
  486. G.inptr = G.inptr_leftover - (int)G.csize;
  487. G.incnt_leftover = 0;
  488. } else if (G.incnt < 0)
  489. G.incnt = 0;
  490. } /* end function undefer_input() */
  491. /***********************************/
  492. /* function defer_leftover_input() */
  493. /***********************************/
  494. void defer_leftover_input(__G)
  495. __GDEF
  496. {
  497. if ((zoff_t)G.incnt > G.csize) {
  498. /* (G.csize < MAXINT), we can safely cast it to int !! */
  499. if (G.csize < 0L)
  500. G.csize = 0L;
  501. G.inptr_leftover = G.inptr + (int)G.csize;
  502. G.incnt_leftover = G.incnt - (int)G.csize;
  503. G.incnt = (int)G.csize;
  504. } else
  505. G.incnt_leftover = 0;
  506. G.csize -= G.incnt;
  507. } /* end function defer_leftover_input() */
  508. /**********************/
  509. /* Function readbuf() */
  510. /**********************/
  511. unsigned readbuf(__G__ buf, size) /* return number of bytes read into buf */
  512. __GDEF
  513. char *buf;
  514. register unsigned size;
  515. {
  516. register unsigned count;
  517. unsigned n;
  518. n = size;
  519. while (size) {
  520. if (G.incnt <= 0) {
  521. if ((G.incnt = read(G.zipfd, (char *)G.inbuf, INBUFSIZ)) == 0)
  522. return (n-size);
  523. else if (G.incnt < 0) {
  524. /* another hack, but no real harm copying same thing twice */
  525. (*G.message)((zvoid *)&G,
  526. (uch *)LoadFarString(ReadError), /* CANNOT use slide */
  527. (ulg)strlen(LoadFarString(ReadError)), 0x401);
  528. return 0; /* discarding some data; better than lock-up */
  529. }
  530. /* buffer ALWAYS starts on a block boundary: */
  531. G.cur_zipfile_bufstart += INBUFSIZ;
  532. G.inptr = G.inbuf;
  533. }
  534. count = MIN(size, (unsigned)G.incnt);
  535. memcpy(buf, G.inptr, count);
  536. buf += count;
  537. G.inptr += count;
  538. G.incnt -= count;
  539. size -= count;
  540. }
  541. return n;
  542. } /* end function readbuf() */
  543. /***********************/
  544. /* Function readbyte() */
  545. /***********************/
  546. int readbyte(__G) /* refill inbuf and return a byte if available, else EOF */
  547. __GDEF
  548. {
  549. if (G.mem_mode)
  550. return EOF;
  551. if (G.csize <= 0) {
  552. G.csize--; /* for tests done after exploding */
  553. G.incnt = 0;
  554. return EOF;
  555. }
  556. if (G.incnt <= 0) {
  557. if ((G.incnt = read(G.zipfd, (char *)G.inbuf, INBUFSIZ)) == 0) {
  558. return EOF;
  559. } else if (G.incnt < 0) { /* "fail" (abort, retry, ...) returns this */
  560. /* another hack, but no real harm copying same thing twice */
  561. (*G.message)((zvoid *)&G,
  562. (uch *)LoadFarString(ReadError),
  563. (ulg)strlen(LoadFarString(ReadError)), 0x401);
  564. echon();
  565. #ifdef WINDLL
  566. longjmp(dll_error_return, 1);
  567. #else
  568. DESTROYGLOBALS();
  569. EXIT(PK_BADERR); /* totally bailing; better than lock-up */
  570. #endif
  571. }
  572. G.cur_zipfile_bufstart += INBUFSIZ; /* always starts on block bndry */
  573. G.inptr = G.inbuf;
  574. defer_leftover_input(__G); /* decrements G.csize */
  575. }
  576. #if CRYPT
  577. if (G.pInfo->encrypted) {
  578. uch *p;
  579. int n;
  580. /* This was previously set to decrypt one byte beyond G.csize, when
  581. * incnt reached that far. GRR said, "but it's required: why?" This
  582. * was a bug in fillinbuf() -- was it also a bug here?
  583. */
  584. for (n = G.incnt, p = G.inptr; n--; p++)
  585. zdecode(*p);
  586. }
  587. #endif /* CRYPT */
  588. --G.incnt;
  589. return *G.inptr++;
  590. } /* end function readbyte() */
  591. #if defined(USE_ZLIB) || defined(USE_BZIP2)
  592. /************************/
  593. /* Function fillinbuf() */
  594. /************************/
  595. int fillinbuf(__G) /* like readbyte() except returns number of bytes in inbuf */
  596. __GDEF
  597. {
  598. if (G.mem_mode ||
  599. (G.incnt = read(G.zipfd, (char *)G.inbuf, INBUFSIZ)) <= 0)
  600. return 0;
  601. G.cur_zipfile_bufstart += INBUFSIZ; /* always starts on a block boundary */
  602. G.inptr = G.inbuf;
  603. defer_leftover_input(__G); /* decrements G.csize */
  604. #if CRYPT
  605. if (G.pInfo->encrypted) {
  606. uch *p;
  607. int n;
  608. for (n = G.incnt, p = G.inptr; n--; p++)
  609. zdecode(*p);
  610. }
  611. #endif /* CRYPT */
  612. return G.incnt;
  613. } /* end function fillinbuf() */
  614. #endif /* USE_ZLIB || USE_BZIP2 */
  615. /************************/
  616. /* Function seek_zipf() */
  617. /************************/
  618. int seek_zipf(__G__ abs_offset)
  619. __GDEF
  620. zoff_t abs_offset;
  621. {
  622. /*
  623. * Seek to the block boundary of the block which includes abs_offset,
  624. * then read block into input buffer and set pointers appropriately.
  625. * If block is already in the buffer, just set the pointers. This function
  626. * is used by do_seekable (process.c), extract_or_test_entrylist (extract.c)
  627. * and do_string (fileio.c). Also, a slightly modified version is embedded
  628. * within extract_or_test_entrylist (extract.c). readbyte() and readbuf()
  629. * (fileio.c) are compatible. NOTE THAT abs_offset is intended to be the
  630. * "proper offset" (i.e., if there were no extra bytes prepended);
  631. * cur_zipfile_bufstart contains the corrected offset.
  632. *
  633. * Since seek_zipf() is never used during decompression, it is safe to
  634. * use the slide[] buffer for the error message.
  635. *
  636. * returns PK error codes:
  637. * PK_BADERR if effective offset in zipfile is negative
  638. * PK_EOF if seeking past end of zipfile
  639. * PK_OK when seek was successful
  640. */
  641. zoff_t request = abs_offset + G.extra_bytes;
  642. zoff_t inbuf_offset = request % INBUFSIZ;
  643. zoff_t bufstart = request - inbuf_offset;
  644. if (request < 0) {
  645. Info(slide, 1, ((char *)slide, LoadFarStringSmall(SeekMsg),
  646. G.zipfn, LoadFarString(ReportMsg)));
  647. return(PK_BADERR);
  648. } else if (bufstart != G.cur_zipfile_bufstart) {
  649. Trace((stderr,
  650. "fpos_zip: abs_offset = %s, G.extra_bytes = %s\n",
  651. FmZofft(abs_offset, NULL, NULL),
  652. FmZofft(G.extra_bytes, NULL, NULL)));
  653. #ifdef USE_STRM_INPUT
  654. zfseeko(G.zipfd, bufstart, SEEK_SET);
  655. G.cur_zipfile_bufstart = zftello(G.zipfd);
  656. #else /* !USE_STRM_INPUT */
  657. G.cur_zipfile_bufstart = zlseek(G.zipfd, bufstart, SEEK_SET);
  658. #endif /* ?USE_STRM_INPUT */
  659. Trace((stderr,
  660. " request = %s, (abs+extra) = %s, inbuf_offset = %s\n",
  661. FmZofft(request, NULL, NULL),
  662. FmZofft((abs_offset+G.extra_bytes), NULL, NULL),
  663. FmZofft(inbuf_offset, NULL, NULL)));
  664. Trace((stderr, " bufstart = %s, cur_zipfile_bufstart = %s\n",
  665. FmZofft(bufstart, NULL, NULL),
  666. FmZofft(G.cur_zipfile_bufstart, NULL, NULL)));
  667. if ((G.incnt = read(G.zipfd, (char *)G.inbuf, INBUFSIZ)) <= 0)
  668. return(PK_EOF);
  669. G.incnt -= (int)inbuf_offset;
  670. G.inptr = G.inbuf + (int)inbuf_offset;
  671. } else {
  672. G.incnt += (G.inptr-G.inbuf) - (int)inbuf_offset;
  673. G.inptr = G.inbuf + (int)inbuf_offset;
  674. }
  675. return(PK_OK);
  676. } /* end function seek_zipf() */
  677. #ifndef VMS /* for VMS use code in vms.c */
  678. /********************/
  679. /* Function flush() */ /* returns PK error codes: */
  680. /********************/ /* if tflag => always 0; PK_DISK if write error */
  681. int flush(__G__ rawbuf, size, unshrink)
  682. __GDEF
  683. uch *rawbuf;
  684. ulg size;
  685. int unshrink;
  686. #if (defined(USE_DEFLATE64) && defined(__16BIT__))
  687. {
  688. int ret;
  689. /* On 16-bit systems (MSDOS, OS/2 1.x), the standard C library functions
  690. * cannot handle writes of 64k blocks at once. For these systems, the
  691. * blocks to flush are split into pieces of 32k or less.
  692. */
  693. while (size > 0x8000L) {
  694. ret = partflush(__G__ rawbuf, 0x8000L, unshrink);
  695. if (ret != PK_OK)
  696. return ret;
  697. size -= 0x8000L;
  698. rawbuf += (extent)0x8000;
  699. }
  700. return partflush(__G__ rawbuf, size, unshrink);
  701. } /* end function flush() */
  702. /************************/
  703. /* Function partflush() */ /* returns PK error codes: */
  704. /************************/ /* if tflag => always 0; PK_DISK if write error */
  705. static int partflush(__G__ rawbuf, size, unshrink)
  706. __GDEF
  707. uch *rawbuf; /* cannot be ZCONST, gets passed to (*G.message)() */
  708. ulg size;
  709. int unshrink;
  710. #endif /* USE_DEFLATE64 && __16BIT__ */
  711. {
  712. register uch *p;
  713. register uch *q;
  714. uch *transbuf;
  715. #if (defined(SMALL_MEM) || defined(MED_MEM) || defined(VMS_TEXT_CONV))
  716. ulg transbufsiz;
  717. #endif
  718. /* static int didCRlast = FALSE; moved to globals.h */
  719. /*---------------------------------------------------------------------------
  720. Compute the CRC first; if testing or if disk is full, that's it.
  721. ---------------------------------------------------------------------------*/
  722. G.crc32val = crc32(G.crc32val, rawbuf, (extent)size);
  723. #ifdef DLL
  724. if ((G.statreportcb != NULL) &&
  725. (*G.statreportcb)(__G__ UZ_ST_IN_PROGRESS, G.zipfn, G.filename, NULL))
  726. return IZ_CTRLC; /* cancel operation by user request */
  727. #endif
  728. if (uO.tflag || size == 0L) /* testing or nothing to write: all done */
  729. return PK_OK;
  730. if (G.disk_full)
  731. return PK_DISK; /* disk already full: ignore rest of file */
  732. /*---------------------------------------------------------------------------
  733. Write the bytes rawbuf[0..size-1] to the output device, first converting
  734. end-of-lines and ASCII/EBCDIC as needed. If SMALL_MEM or MED_MEM are NOT
  735. defined, outbuf is assumed to be at least as large as rawbuf and is not
  736. necessarily checked for overflow.
  737. ---------------------------------------------------------------------------*/
  738. if (!G.pInfo->textmode) { /* write raw binary data */
  739. /* GRR: note that for standard MS-DOS compilers, size argument to
  740. * fwrite() can never be more than 65534, so WriteError macro will
  741. * have to be rewritten if size can ever be that large. For now,
  742. * never more than 32K. Also note that write() returns an int, which
  743. * doesn't necessarily limit size to 32767 bytes if write() is used
  744. * on 16-bit systems but does make it more of a pain; however, because
  745. * at least MSC 5.1 has a lousy implementation of fwrite() (as does
  746. * DEC Ultrix cc), write() is used anyway.
  747. */
  748. #ifdef DLL
  749. if (G.redirect_data) {
  750. #ifdef NO_SLIDE_REDIR
  751. if (writeToMemory(__G__ rawbuf, (extent)size)) return PK_ERR;
  752. #else
  753. writeToMemory(__G__ rawbuf, (extent)size);
  754. #endif
  755. } else
  756. #endif
  757. if (!uO.cflag && WriteError(rawbuf, size, G.outfile))
  758. return disk_error(__G);
  759. else if (uO.cflag && (*G.message)((zvoid *)&G, rawbuf, size, 0))
  760. return PK_OK;
  761. } else { /* textmode: aflag is true */
  762. if (unshrink) {
  763. /* rawbuf = outbuf */
  764. transbuf = G.outbuf2;
  765. #if (defined(SMALL_MEM) || defined(MED_MEM) || defined(VMS_TEXT_CONV))
  766. transbufsiz = TRANSBUFSIZ;
  767. #endif
  768. } else {
  769. /* rawbuf = slide */
  770. transbuf = G.outbuf;
  771. #if (defined(SMALL_MEM) || defined(MED_MEM) || defined(VMS_TEXT_CONV))
  772. transbufsiz = OUTBUFSIZ;
  773. Trace((stderr, "\ntransbufsiz = OUTBUFSIZ = %u\n",
  774. (unsigned)OUTBUFSIZ));
  775. #endif
  776. }
  777. if (G.newfile) {
  778. #ifdef VMS_TEXT_CONV
  779. if (G.pInfo->hostnum == VMS_ && G.extra_field &&
  780. is_vms_varlen_txt(__G__ G.extra_field,
  781. G.lrec.extra_field_length))
  782. G.VMS_line_state = 0; /* 0: ready to read line length */
  783. else
  784. G.VMS_line_state = -1; /* -1: don't treat as VMS text */
  785. #endif
  786. G.didCRlast = FALSE; /* no previous buffers written */
  787. G.newfile = FALSE;
  788. }
  789. #ifdef VMS_TEXT_CONV
  790. if (G.VMS_line_state >= 0)
  791. {
  792. p = rawbuf;
  793. q = transbuf;
  794. while ((extent)(p-rawbuf) < (extent)size) {
  795. switch (G.VMS_line_state) {
  796. /* 0: ready to read line length */
  797. case 0:
  798. G.VMS_line_length = 0;
  799. if ((extent)(p-rawbuf) == (extent)size-1) {
  800. /* last char */
  801. G.VMS_line_length = (unsigned)(*p++);
  802. G.VMS_line_state = 1;
  803. } else {
  804. G.VMS_line_length = makeword(p);
  805. p += 2;
  806. G.VMS_line_state = 2;
  807. }
  808. G.VMS_line_pad =
  809. ((G.VMS_line_length & 1) != 0); /* odd */
  810. break;
  811. /* 1: read one byte of length, need second */
  812. case 1:
  813. G.VMS_line_length += ((unsigned)(*p++) << 8);
  814. G.VMS_line_state = 2;
  815. break;
  816. /* 2: ready to read VMS_line_length chars */
  817. case 2:
  818. {
  819. extent remaining = (extent)size+(rawbuf-p);
  820. extent outroom;
  821. if (G.VMS_line_length < remaining) {
  822. remaining = G.VMS_line_length;
  823. G.VMS_line_state = 3;
  824. }
  825. outroom = transbuf+(extent)transbufsiz-q;
  826. if (remaining >= outroom) {
  827. remaining -= outroom;
  828. for (;outroom > 0; p++, outroom--)
  829. *q++ = native(*p);
  830. #ifdef DLL
  831. if (G.redirect_data) {
  832. if (writeToMemory(__G__ transbuf,
  833. (extent)(q-transbuf))) return PK_ERR;
  834. } else
  835. #endif
  836. if (!uO.cflag && WriteError(transbuf,
  837. (extent)(q-transbuf), G.outfile))
  838. return disk_error(__G);
  839. else if (uO.cflag && (*G.message)((zvoid *)&G,
  840. transbuf, (ulg)(q-transbuf), 0))
  841. return PK_OK;
  842. q = transbuf;
  843. /* fall through to normal case */
  844. }
  845. G.VMS_line_length -= remaining;
  846. for (;remaining > 0; p++, remaining--)
  847. *q++ = native(*p);
  848. }
  849. break;
  850. /* 3: ready to PutNativeEOL */
  851. case 3:
  852. if (q > transbuf+(extent)transbufsiz-lenEOL) {
  853. #ifdef DLL
  854. if (G.redirect_data) {
  855. if (writeToMemory(__G__ transbuf,
  856. (extent)(q-transbuf))) return PK_ERR;
  857. } else
  858. #endif
  859. if (!uO.cflag &&
  860. WriteError(transbuf, (extent)(q-transbuf),
  861. G.outfile))
  862. return disk_error(__G);
  863. else if (uO.cflag && (*G.message)((zvoid *)&G,
  864. transbuf, (ulg)(q-transbuf), 0))
  865. return PK_OK;
  866. q = transbuf;
  867. }
  868. PutNativeEOL
  869. G.VMS_line_state = G.VMS_line_pad ? 4 : 0;
  870. break;
  871. /* 4: ready to read pad byte */
  872. case 4:
  873. ++p;
  874. G.VMS_line_state = 0;
  875. break;
  876. }
  877. } /* end while */
  878. } else
  879. #endif /* VMS_TEXT_CONV */
  880. /*-----------------------------------------------------------------------
  881. Algorithm: CR/LF => native; lone CR => native; lone LF => native.
  882. This routine is only for non-raw-VMS, non-raw-VM/CMS files (i.e.,
  883. stream-oriented files, not record-oriented).
  884. -----------------------------------------------------------------------*/
  885. /* else not VMS text */ {
  886. p = rawbuf;
  887. if (*p == LF && G.didCRlast)
  888. ++p;
  889. G.didCRlast = FALSE;
  890. for (q = transbuf; (extent)(p-rawbuf) < (extent)size; ++p) {
  891. if (*p == CR) { /* lone CR or CR/LF: treat as EOL */
  892. PutNativeEOL
  893. if ((extent)(p-rawbuf) == (extent)size-1)
  894. /* last char in buffer */
  895. G.didCRlast = TRUE;
  896. else if (p[1] == LF) /* get rid of accompanying LF */
  897. ++p;
  898. } else if (*p == LF) /* lone LF */
  899. PutNativeEOL
  900. else
  901. #ifndef DOS_FLX_OS2_W32
  902. if (*p != CTRLZ) /* lose all ^Z's */
  903. #endif
  904. *q++ = native(*p);
  905. #if (defined(SMALL_MEM) || defined(MED_MEM))
  906. # if (lenEOL == 1) /* don't check unshrink: both buffers small but equal */
  907. if (!unshrink)
  908. # endif
  909. /* check for danger of buffer overflow and flush */
  910. if (q > transbuf+(extent)transbufsiz-lenEOL) {
  911. Trace((stderr,
  912. "p - rawbuf = %u q-transbuf = %u size = %lu\n",
  913. (unsigned)(p-rawbuf), (unsigned)(q-transbuf), size));
  914. if (!uO.cflag && WriteError(transbuf,
  915. (extent)(q-transbuf), G.outfile))
  916. return disk_error(__G);
  917. else if (uO.cflag && (*G.message)((zvoid *)&G,
  918. transbuf, (ulg)(q-transbuf), 0))
  919. return PK_OK;
  920. q = transbuf;
  921. continue;
  922. }
  923. #endif /* SMALL_MEM || MED_MEM */
  924. }
  925. }
  926. /*-----------------------------------------------------------------------
  927. Done translating: write whatever we've got to file (or screen).
  928. -----------------------------------------------------------------------*/
  929. Trace((stderr, "p - rawbuf = %u q-transbuf = %u size = %lu\n",
  930. (unsigned)(p-rawbuf), (unsigned)(q-transbuf), size));
  931. if (q > transbuf) {
  932. #ifdef DLL
  933. if (G.redirect_data) {
  934. if (writeToMemory(__G__ transbuf, (extent)(q-transbuf)))
  935. return PK_ERR;
  936. } else
  937. #endif
  938. if (!uO.cflag && WriteError(transbuf, (extent)(q-transbuf),
  939. G.outfile))
  940. return disk_error(__G);
  941. else if (uO.cflag && (*G.message)((zvoid *)&G, transbuf,
  942. (ulg)(q-transbuf), 0))
  943. return PK_OK;
  944. }
  945. }
  946. return PK_OK;
  947. } /* end function flush() [resp. partflush() for 16-bit Deflate64 support] */
  948. #ifdef VMS_TEXT_CONV
  949. /********************************/
  950. /* Function is_vms_varlen_txt() */
  951. /********************************/
  952. static int is_vms_varlen_txt(__G__ ef_buf, ef_len)
  953. __GDEF
  954. uch *ef_buf; /* buffer containing extra field */
  955. unsigned ef_len; /* total length of extra field */
  956. {
  957. unsigned eb_id;
  958. unsigned eb_len;
  959. uch *eb_data;
  960. unsigned eb_datlen;
  961. #define VMSREC_C_UNDEF 0
  962. #define VMSREC_C_VAR 2
  963. uch vms_rectype = VMSREC_C_UNDEF;
  964. /* uch vms_fileorg = 0; */ /* currently, fileorg is not used... */
  965. #define VMSPK_ITEMID 0
  966. #define VMSPK_ITEMLEN 2
  967. #define VMSPK_ITEMHEADSZ 4
  968. #define VMSATR_C_RECATTR 4
  969. #define VMS_FABSIG 0x42414656 /* "VFAB" */
  970. /* offsets of interesting fields in VMS fabdef structure */
  971. #define VMSFAB_B_RFM 31 /* record format byte */
  972. #define VMSFAB_B_ORG 29 /* file organization byte */
  973. if (ef_len == 0 || ef_buf == NULL)
  974. return FALSE;
  975. while (ef_len >= EB_HEADSIZE) {
  976. eb_id = makeword(EB_ID + ef_buf);
  977. eb_len = makeword(EB_LEN + ef_buf);
  978. if (eb_len > (ef_len - EB_HEADSIZE)) {
  979. /* discovered some extra field inconsistency! */
  980. Trace((stderr,
  981. "is_vms_varlen_txt: block length %u > rest ef_size %u\n", eb_len,
  982. ef_len - EB_HEADSIZE));
  983. break;
  984. }
  985. switch (eb_id) {
  986. case EF_PKVMS:
  987. /* The PKVMS e.f. raw data part consists of:
  988. * a) 4 bytes CRC checksum
  989. * b) list of uncompressed variable-length data items
  990. * Each data item is introduced by a fixed header
  991. * - 2 bytes data type ID
  992. * - 2 bytes <size> of data
  993. * - <size> bytes of actual attribute data
  994. */
  995. /* get pointer to start of data and its total length */
  996. eb_data = ef_buf+(EB_HEADSIZE+4);
  997. eb_datlen = eb_len-4;
  998. /* test the CRC checksum */
  999. if (makelong(ef_buf+EB_HEADSIZE) !=
  1000. crc32(CRCVAL_INITIAL, eb_data, (extent)eb_datlen))
  1001. {
  1002. Info(slide, 1, ((char *)slide,
  1003. "[Warning: CRC error, discarding PKWARE extra field]\n"));
  1004. /* skip over the data analysis code */
  1005. break;
  1006. }
  1007. /* scan through the attribute data items */
  1008. while (eb_datlen > 4)
  1009. {
  1010. unsigned fldsize = makeword(&eb_data[VMSPK_ITEMLEN]);
  1011. /* check the item type word */
  1012. switch (makeword(&eb_data[VMSPK_ITEMID])) {
  1013. case VMSATR_C_RECATTR:
  1014. /* we have found the (currently only) interesting
  1015. * data item */
  1016. if (fldsize >= 1) {
  1017. vms_rectype = eb_data[VMSPK_ITEMHEADSZ] & 15;
  1018. /* vms_fileorg = eb_data[VMSPK_ITEMHEADSZ] >> 4; */
  1019. }
  1020. break;
  1021. default:
  1022. break;
  1023. }
  1024. /* skip to next data item */
  1025. eb_datlen -= fldsize + VMSPK_ITEMHEADSZ;
  1026. eb_data += fldsize + VMSPK_ITEMHEADSZ;
  1027. }
  1028. break;
  1029. case EF_IZVMS:
  1030. if (makelong(ef_buf+EB_HEADSIZE) == VMS_FABSIG) {
  1031. if ((eb_data = extract_izvms_block(__G__
  1032. ef_buf+EB_HEADSIZE, eb_len,
  1033. &eb_datlen, NULL, 0))
  1034. != NULL)
  1035. {
  1036. if (eb_datlen >= VMSFAB_B_RFM+1) {
  1037. vms_rectype = eb_data[VMSFAB_B_RFM] & 15;
  1038. /* vms_fileorg = eb_data[VMSFAB_B_ORG] >> 4; */
  1039. }
  1040. free(eb_data);
  1041. }
  1042. }
  1043. break;
  1044. default:
  1045. break;
  1046. }
  1047. /* Skip this extra field block */
  1048. ef_buf += (eb_len + EB_HEADSIZE);
  1049. ef_len -= (eb_len + EB_HEADSIZE);
  1050. }
  1051. return (vms_rectype == VMSREC_C_VAR);
  1052. } /* end function is_vms_varlen_txtfile() */
  1053. #endif /* VMS_TEXT_CONV */
  1054. /*************************/
  1055. /* Function disk_error() */
  1056. /*************************/
  1057. static int disk_error(__G)
  1058. __GDEF
  1059. {
  1060. /* OK to use slide[] here because this file is finished regardless */
  1061. Info(slide, 0x4a1, ((char *)slide, LoadFarString(DiskFullQuery),
  1062. FnFilter1(G.filename)));
  1063. #ifndef WINDLL
  1064. fgets(G.answerbuf, sizeof(G.answerbuf), stdin);
  1065. if (*G.answerbuf == 'y') /* stop writing to this file */
  1066. G.disk_full = 1; /* (outfile bad?), but new OK */
  1067. else
  1068. #endif
  1069. G.disk_full = 2; /* no: exit program */
  1070. return PK_DISK;
  1071. } /* end function disk_error() */
  1072. #endif /* !VMS */
  1073. /*****************************/
  1074. /* Function UzpMessagePrnt() */
  1075. /*****************************/
  1076. int UZ_EXP UzpMessagePrnt(pG, buf, size, flag)
  1077. zvoid *pG; /* globals struct: always passed */
  1078. uch *buf; /* preformatted string to be printed */
  1079. ulg size; /* length of string (may include nulls) */
  1080. int flag; /* flag bits */
  1081. {
  1082. /* IMPORTANT NOTE:
  1083. * The name of the first parameter of UzpMessagePrnt(), which passes
  1084. * the "Uz_Globs" address, >>> MUST <<< be identical to the string
  1085. * expansion of the __G__ macro in the REENTRANT case (see globals.h).
  1086. * This name identity is mandatory for the LoadFarString() macro
  1087. * (in the SMALL_MEM case) !!!
  1088. */
  1089. int error;
  1090. uch *q=buf, *endbuf=buf+(unsigned)size;
  1091. #ifdef MORE
  1092. uch *p=buf;
  1093. #if (defined(SCREENWIDTH) && defined(SCREENLWRAP))
  1094. int islinefeed = FALSE;
  1095. #endif
  1096. #endif
  1097. FILE *outfp;
  1098. /*---------------------------------------------------------------------------
  1099. These tests are here to allow fine-tuning of UnZip's output messages,
  1100. but none of them will do anything without setting the appropriate bit
  1101. in the flag argument of every Info() statement which is to be turned
  1102. *off*. That is, all messages are currently turned on for all ports.
  1103. To turn off *all* messages, use the UzpMessageNull() function instead
  1104. of this one.
  1105. ---------------------------------------------------------------------------*/
  1106. #if (defined(OS2) && defined(DLL))
  1107. if (MSG_NO_DLL2(flag)) /* if OS/2 DLL bit is set, do NOT print this msg */
  1108. return 0;
  1109. #endif
  1110. #ifdef WINDLL
  1111. if (MSG_NO_WDLL(flag))
  1112. return 0;
  1113. #endif
  1114. #ifdef WINDLL
  1115. if (MSG_NO_WGUI(flag))
  1116. return 0;
  1117. #endif
  1118. /*
  1119. #ifdef ACORN_GUI
  1120. if (MSG_NO_AGUI(flag))
  1121. return 0;
  1122. #endif
  1123. */
  1124. #ifdef DLL /* don't display message if data is redirected */
  1125. if (((Uz_Globs *)pG)->redirect_data &&
  1126. !((Uz_Globs *)pG)->redirect_text)
  1127. return 0;
  1128. #endif
  1129. if (MSG_STDERR(flag) && !((Uz_Globs *)pG)->UzO.tflag)
  1130. outfp = (FILE *)stderr;
  1131. else
  1132. outfp = (FILE *)stdout;
  1133. #ifdef QUERY_TRNEWLN
  1134. /* some systems require termination of query prompts with '\n' to force
  1135. * immediate display */
  1136. if (MSG_MNEWLN(flag)) { /* assumes writable buffer (e.g., slide[]) */
  1137. *endbuf++ = '\n'; /* with room for one more char at end of buf */
  1138. ++size; /* (safe assumption: only used for four */
  1139. } /* short queries in extract.c and fileio.c) */
  1140. #endif
  1141. if (MSG_TNEWLN(flag)) { /* again assumes writable buffer: fragile... */
  1142. if ((!size && !((Uz_Globs *)pG)->sol) ||
  1143. (size && (endbuf[-1] != '\n')))
  1144. {
  1145. *endbuf++ = '\n';
  1146. ++size;
  1147. }
  1148. }
  1149. #ifdef MORE
  1150. # ifdef SCREENSIZE
  1151. /* room for --More-- and one line of overlap: */
  1152. # if (defined(SCREENWIDTH) && defined(SCREENLWRAP))
  1153. SCREENSIZE(&((Uz_Globs *)pG)->height, &((Uz_Globs *)pG)->width);
  1154. # else
  1155. SCREENSIZE(&((Uz_Globs *)pG)->height, (int *)NULL);
  1156. # endif
  1157. ((Uz_Globs *)pG)->height -= 2;
  1158. # else
  1159. /* room for --More-- and one line of overlap: */
  1160. ((Uz_Globs *)pG)->height = SCREENLINES - 2;
  1161. # if (defined(SCREENWIDTH) && defined(SCREENLWRAP))
  1162. ((Uz_Globs *)pG)->width = SCREENWIDTH;
  1163. # endif
  1164. # endif
  1165. #endif /* MORE */
  1166. if (MSG_LNEWLN(flag) && !((Uz_Globs *)pG)->sol) {
  1167. /* not at start of line: want newline */
  1168. #ifdef OS2DLL
  1169. if (!((Uz_Globs *)pG)->redirect_text) {
  1170. #endif
  1171. putc('\n', outfp);
  1172. fflush(outfp);
  1173. #ifdef MORE
  1174. if (((Uz_Globs *)pG)->M_flag)
  1175. {
  1176. #if (defined(SCREENWIDTH) && defined(SCREENLWRAP))
  1177. ((Uz_Globs *)pG)->chars = 0;
  1178. #endif
  1179. ++((Uz_Globs *)pG)->numlines;
  1180. ++((Uz_Globs *)pG)->lines;
  1181. if (((Uz_Globs *)pG)->lines >= ((Uz_Globs *)pG)->height)
  1182. (*((Uz_Globs *)pG)->mpause)((zvoid *)pG,
  1183. LoadFarString(MorePrompt), 1);
  1184. }
  1185. #endif /* MORE */
  1186. if (MSG_STDERR(flag) && ((Uz_Globs *)pG)->UzO.tflag &&
  1187. !isatty(1) && isatty(2))
  1188. {
  1189. /* error output from testing redirected: also send to stderr */
  1190. putc('\n', stderr);
  1191. fflush(stderr);
  1192. }
  1193. #ifdef OS2DLL
  1194. } else
  1195. REDIRECTC('\n');
  1196. #endif
  1197. ((Uz_Globs *)pG)->sol = TRUE;
  1198. }
  1199. /* put zipfile name, filename and/or error/warning keywords here */
  1200. #ifdef MORE
  1201. if (((Uz_Globs *)pG)->M_flag
  1202. #ifdef OS2DLL
  1203. && !((Uz_Globs *)pG)->redirect_text
  1204. #endif
  1205. )
  1206. {
  1207. while (p < endbuf) {
  1208. if (*p == '\n') {
  1209. #if (defined(SCREENWIDTH) && defined(SCREENLWRAP))
  1210. islinefeed = TRUE;
  1211. } else if (SCREENLWRAP) {
  1212. if (*p == '\r') {
  1213. ((Uz_Globs *)pG)->chars = 0;
  1214. } else {
  1215. # ifdef TABSIZE
  1216. if (*p == '\t')
  1217. ((Uz_Globs *)pG)->chars +=
  1218. (TABSIZE - (((Uz_Globs *)pG)->chars % TABSIZE));
  1219. else
  1220. # endif
  1221. ++((Uz_Globs *)pG)->chars;
  1222. if (((Uz_Globs *)pG)->chars >= ((Uz_Globs *)pG)->width)
  1223. islinefeed = TRUE;
  1224. }
  1225. }
  1226. if (islinefeed) {
  1227. islinefeed = FALSE;
  1228. ((Uz_Globs *)pG)->chars = 0;
  1229. #endif /* (SCREENWIDTH && SCREEN_LWRAP) */
  1230. ++((Uz_Globs *)pG)->numlines;
  1231. ++((Uz_Globs *)pG)->lines;
  1232. if (((Uz_Globs *)pG)->lines >= ((Uz_Globs *)pG)->height)
  1233. {
  1234. if ((error = WriteTxtErr(q, p-q+1, outfp)) != 0)
  1235. return error;
  1236. fflush(outfp);
  1237. ((Uz_Globs *)pG)->sol = TRUE;
  1238. q = p + 1;
  1239. (*((Uz_Globs *)pG)->mpause)((zvoid *)pG,
  1240. LoadFarString(MorePrompt), 1);
  1241. }
  1242. }
  1243. INCSTR(p);
  1244. } /* end while */
  1245. size = (ulg)(p - q); /* remaining text */
  1246. }
  1247. #endif /* MORE */
  1248. if (size) {
  1249. #ifdef OS2DLL
  1250. if (!((Uz_Globs *)pG)->redirect_text) {
  1251. #endif
  1252. if ((error = WriteTxtErr(q, size, outfp)) != 0)
  1253. return error;
  1254. #ifndef VMS /* 2005-09-16 SMS. See note at "WriteTxtErr()", above. */
  1255. fflush(outfp);
  1256. #endif
  1257. if (MSG_STDERR(flag) && ((Uz_Globs *)pG)->UzO.tflag &&
  1258. !isatty(1) && isatty(2))
  1259. {
  1260. /* error output from testing redirected: also send to stderr */
  1261. if ((error = WriteTxtErr(q, size, stderr)) != 0)
  1262. return error;
  1263. fflush(stderr);
  1264. }
  1265. #ifdef OS2DLL
  1266. } else { /* GRR: this is ugly: hide with macro */
  1267. if ((error = REDIRECTPRINT(q, size)) != 0)
  1268. return error;
  1269. }
  1270. #endif /* OS2DLL */
  1271. ((Uz_Globs *)pG)->sol = (endbuf[-1] == '\n');
  1272. }
  1273. return 0;
  1274. } /* end function UzpMessagePrnt() */
  1275. #ifdef DLL
  1276. /*****************************/
  1277. /* Function UzpMessageNull() */ /* convenience routine for no output at all */
  1278. /*****************************/
  1279. int UZ_EXP UzpMessageNull(pG, buf, size, flag)
  1280. zvoid *pG; /* globals struct: always passed */
  1281. uch *buf; /* preformatted string to be printed */
  1282. ulg size; /* length of string (may include nulls) */
  1283. int flag; /* flag bits */
  1284. {
  1285. return 0;
  1286. } /* end function UzpMessageNull() */
  1287. #endif /* DLL */
  1288. /***********************/
  1289. /* Function UzpInput() */ /* GRR: this is a placeholder for now */
  1290. /***********************/
  1291. int UZ_EXP UzpInput(pG, buf, size, flag)
  1292. zvoid *pG; /* globals struct: always passed */
  1293. uch *buf; /* preformatted string to be printed */
  1294. int *size; /* (address of) size of buf and of returned string */
  1295. int flag; /* flag bits (bit 0: no echo) */
  1296. {
  1297. /* tell picky compilers to shut up about "unused variable" warnings */
  1298. pG = pG; buf = buf; flag = flag;
  1299. *size = 0;
  1300. return 0;
  1301. } /* end function UzpInput() */
  1302. #if (!defined(WINDLL) && !defined(MACOS))
  1303. /***************************/
  1304. /* Function UzpMorePause() */
  1305. /***************************/
  1306. void UZ_EXP UzpMorePause(pG, prompt, flag)
  1307. zvoid *pG; /* globals struct: always passed */
  1308. ZCONST char *prompt; /* "--More--" prompt */
  1309. int flag; /* 0 = any char OK; 1 = accept only '\n', ' ', q */
  1310. {
  1311. uch c;
  1312. /*---------------------------------------------------------------------------
  1313. Print a prompt and wait for the user to press a key, then erase prompt
  1314. if possible.
  1315. ---------------------------------------------------------------------------*/
  1316. if (!((Uz_Globs *)pG)->sol)
  1317. fprintf(stderr, "\n");
  1318. /* numlines may or may not be used: */
  1319. fprintf(stderr, prompt, ((Uz_Globs *)pG)->numlines);
  1320. fflush(stderr);
  1321. if (flag & 1) {
  1322. do {
  1323. c = (uch)FGETCH(0);
  1324. } while (
  1325. #ifdef THEOS
  1326. c != 17 && /* standard QUIT key */
  1327. #endif
  1328. c != '\r' && c != '\n' && c != ' ' && c != 'q' && c != 'Q');
  1329. } else
  1330. c = (uch)FGETCH(0);
  1331. /* newline was not echoed, so cover up prompt line */
  1332. fprintf(stderr, LoadFarString(HidePrompt));
  1333. fflush(stderr);
  1334. if (
  1335. #ifdef THEOS
  1336. (c == 17) || /* standard QUIT key */
  1337. #endif
  1338. (ToLower(c) == 'q')) {
  1339. DESTROYGLOBALS();
  1340. EXIT(PK_COOL);
  1341. }
  1342. ((Uz_Globs *)pG)->sol = TRUE;
  1343. #ifdef MORE
  1344. /* space for another screen, enter for another line. */
  1345. if ((flag & 1) && c == ' ')
  1346. ((Uz_Globs *)pG)->lines = 0;
  1347. #endif /* MORE */
  1348. } /* end function UzpMorePause() */
  1349. #endif /* !WINDLL && !MACOS */
  1350. #ifndef WINDLL
  1351. /**************************/
  1352. /* Function UzpPassword() */
  1353. /**************************/
  1354. int UZ_EXP UzpPassword (pG, rcnt, pwbuf, size, zfn, efn)
  1355. zvoid *pG; /* pointer to UnZip's internal global vars */
  1356. int *rcnt; /* retry counter */
  1357. char *pwbuf; /* buffer for password */
  1358. int size; /* size of password buffer */
  1359. ZCONST char *zfn; /* name of zip archive */
  1360. ZCONST char *efn; /* name of archive entry being processed */
  1361. {
  1362. #if CRYPT
  1363. int r = IZ_PW_ENTERED;
  1364. char *m;
  1365. char *prompt;
  1366. #ifndef REENTRANT
  1367. /* tell picky compilers to shut up about "unused variable" warnings */
  1368. pG = pG;
  1369. #endif
  1370. if (*rcnt == 0) { /* First call for current entry */
  1371. *rcnt = 2;
  1372. if ((prompt = (char *)malloc(2*FILNAMSIZ + 15)) != (char *)NULL) {
  1373. sprintf(prompt, LoadFarString(PasswPrompt),
  1374. FnFilter1(zfn), FnFilter2(efn));
  1375. m = prompt;
  1376. } else
  1377. m = (char *)LoadFarString(PasswPrompt2);
  1378. } else { /* Retry call, previous password was wrong */
  1379. (*rcnt)--;
  1380. prompt = NULL;
  1381. m = (char *)LoadFarString(PasswRetry);
  1382. }
  1383. m = getp(__G__ m, pwbuf, size);
  1384. if (prompt != (char *)NULL) {
  1385. free(prompt);
  1386. }
  1387. if (m == (char *)NULL) {
  1388. r = IZ_PW_ERROR;
  1389. }
  1390. else if (*pwbuf == '\0') {
  1391. r = IZ_PW_CANCELALL;
  1392. }
  1393. return r;
  1394. #else /* !CRYPT */
  1395. /* tell picky compilers to shut up about "unused variable" warnings */
  1396. pG = pG; rcnt = rcnt; pwbuf = pwbuf; size = size; zfn = zfn; efn = efn;
  1397. return IZ_PW_ERROR; /* internal error; function should never get called */
  1398. #endif /* ?CRYPT */
  1399. } /* end function UzpPassword() */
  1400. /**********************/
  1401. /* Function handler() */
  1402. /**********************/
  1403. void handler(signal) /* upon interrupt, turn on echo and exit cleanly */
  1404. int signal;
  1405. {
  1406. GETGLOBALS();
  1407. #if !(defined(SIGBUS) || defined(SIGSEGV)) /* add a newline if not at */
  1408. (*G.message)((zvoid *)&G, slide, 0L, 0x41); /* start of line (to stderr; */
  1409. #endif /* slide[] should be safe) */
  1410. echon();
  1411. #ifdef SIGBUS
  1412. if (signal == SIGBUS) {
  1413. Info(slide, 0x421, ((char *)slide, LoadFarString(ZipfileCorrupt),
  1414. "bus error"));
  1415. DESTROYGLOBALS();
  1416. EXIT(PK_BADERR);
  1417. }
  1418. #endif /* SIGBUS */
  1419. #ifdef SIGILL
  1420. if (signal == SIGILL) {
  1421. Info(slide, 0x421, ((char *)slide, LoadFarString(ZipfileCorrupt),
  1422. "illegal instruction"));
  1423. DESTROYGLOBALS();
  1424. EXIT(PK_BADERR);
  1425. }
  1426. #endif /* SIGILL */
  1427. #ifdef SIGSEGV
  1428. if (signal == SIGSEGV) {
  1429. Info(slide, 0x421, ((char *)slide, LoadFarString(ZipfileCorrupt),
  1430. "segmentation violation"));
  1431. DESTROYGLOBALS();
  1432. EXIT(PK_BADERR);
  1433. }
  1434. #endif /* SIGSEGV */
  1435. /* probably ctrl-C */
  1436. DESTROYGLOBALS();
  1437. #if defined(AMIGA) && defined(__SASC)
  1438. _abort();
  1439. #endif
  1440. EXIT(IZ_CTRLC); /* was EXIT(0), then EXIT(PK_ERR) */
  1441. }
  1442. #endif /* !WINDLL */
  1443. #if (!defined(VMS) && !defined(CMS_MVS))
  1444. #if (!defined(OS2) || defined(TIMESTAMP))
  1445. #if (!defined(HAVE_MKTIME) || defined(WIN32))
  1446. /* also used in amiga/filedate.c and win32/win32.c */
  1447. ZCONST ush ydays[] =
  1448. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
  1449. #endif
  1450. /*******************************/
  1451. /* Function dos_to_unix_time() */ /* used for freshening/updating/timestamps */
  1452. /*******************************/
  1453. time_t dos_to_unix_time(dosdatetime)
  1454. ulg dosdatetime;
  1455. {
  1456. time_t m_time;
  1457. #ifdef HAVE_MKTIME
  1458. ZCONST time_t now = time(NULL);
  1459. struct tm *tm;
  1460. # define YRBASE 1900
  1461. tm = localtime(&now);
  1462. tm->tm_isdst = -1; /* let mktime determine if DST is in effect */
  1463. /* dissect date */
  1464. tm->tm_year = ((int)(dosdatetime >> 25) & 0x7f) + (1980 - YRBASE);
  1465. tm->tm_mon = ((int)(dosdatetime >> 21) & 0x0f) - 1;
  1466. tm->tm_mday = ((int)(dosdatetime >> 16) & 0x1f);
  1467. /* dissect time */
  1468. tm->tm_hour = (int)((unsigned)dosdatetime >> 11) & 0x1f;
  1469. tm->tm_min = (int)((unsigned)dosdatetime >> 5) & 0x3f;
  1470. tm->tm_sec = (int)((unsigned)dosdatetime << 1) & 0x3e;
  1471. m_time = mktime(tm);
  1472. NATIVE_TO_TIMET(m_time) /* NOP unless MSC 7.0 or Macintosh */
  1473. TTrace((stderr, " final m_time = %lu\n", (ulg)m_time));
  1474. #else /* !HAVE_MKTIME */
  1475. int yr, mo, dy, hh, mm, ss;
  1476. #ifdef TOPS20
  1477. # define YRBASE 1900
  1478. struct tmx *tmx;
  1479. char temp[20];
  1480. #else /* !TOPS20 */
  1481. # define YRBASE 1970
  1482. int leap;
  1483. unsigned days;
  1484. struct tm *tm;
  1485. #if (!defined(MACOS) && !defined(RISCOS) && !defined(QDOS) && !defined(TANDEM))
  1486. #ifdef WIN32
  1487. TIME_ZONE_INFORMATION tzinfo;
  1488. DWORD res;
  1489. #else /* ! WIN32 */
  1490. #ifndef BSD4_4 /* GRR: change to !defined(MODERN) ? */
  1491. #if (defined(BSD) || defined(MTS) || defined(__GO32__))
  1492. struct timeb tbp;
  1493. #else /* !(BSD || MTS || __GO32__) */
  1494. #ifdef DECLARE_TIMEZONE
  1495. extern time_t timezone;
  1496. #endif
  1497. #endif /* ?(BSD || MTS || __GO32__) */
  1498. #endif /* !BSD4_4 */
  1499. #endif /* ?WIN32 */
  1500. #endif /* !MACOS && !RISCOS && !QDOS && !TANDEM */
  1501. #endif /* ?TOPS20 */
  1502. /* dissect date */
  1503. yr = ((int)(dosdatetime >> 25) & 0x7f) + (1980 - YRBASE);
  1504. mo = ((int)(dosdatetime >> 21) & 0x0f) - 1;
  1505. dy = ((int)(dosdatetime >> 16) & 0x1f) - 1;
  1506. /* dissect time */
  1507. hh = (int)((unsigned)dosdatetime >> 11) & 0x1f;
  1508. mm = (int)((unsigned)dosdatetime >> 5) & 0x3f;
  1509. ss = (int)((unsigned)dosdatetime & 0x1f) * 2;
  1510. #ifdef TOPS20
  1511. tmx = (struct tmx *)malloc(sizeof(struct tmx));
  1512. sprintf (temp, "%02d/%02d/%02d %02d:%02d:%02d", mo+1, dy+1, yr, hh, mm, ss);
  1513. time_parse(temp, tmx, (char *)0);
  1514. m_time = time_make(tmx);
  1515. free(tmx);
  1516. #else /* !TOPS20 */
  1517. /*---------------------------------------------------------------------------
  1518. Calculate the number of seconds since the epoch, usually 1 January 1970.
  1519. ---------------------------------------------------------------------------*/
  1520. /* leap = # of leap yrs from YRBASE up to but not including current year */
  1521. leap = ((yr + YRBASE - 1) / 4); /* leap year base factor */
  1522. /* calculate days from BASE to this year and add expired days this year */
  1523. days = (yr * 365) + (leap - 492) + ydays[mo];
  1524. /* if year is a leap year and month is after February, add another day */
  1525. if ((mo > 1) && ((yr+YRBASE)%4 == 0) && ((yr+YRBASE) != 2100))
  1526. ++days; /* OK through 2199 */
  1527. /* convert date & time to seconds relative to 00:00:00, 01/01/YRBASE */
  1528. m_time = (time_t)((unsigned long)(days + dy) * 86400L +
  1529. (unsigned long)hh * 3600L +
  1530. (unsigned long)(mm * 60 + ss));
  1531. /* - 1; MS-DOS times always rounded up to nearest even second */
  1532. TTrace((stderr, "dos_to_unix_time:\n"));
  1533. TTrace((stderr, " m_time before timezone = %lu\n", (ulg)m_time));
  1534. /*---------------------------------------------------------------------------
  1535. Adjust for local standard timezone offset.
  1536. ---------------------------------------------------------------------------*/
  1537. #if (!defined(MACOS) && !defined(RISCOS) && !defined(QDOS) && !defined(TANDEM))
  1538. #ifdef WIN32
  1539. /* account for timezone differences */
  1540. res = GetTimeZoneInformation(&tzinfo);
  1541. if (res != TIME_ZONE_ID_INVALID)
  1542. {
  1543. m_time += 60*(tzinfo.Bias);
  1544. #else /* !WIN32 */
  1545. #if (defined(BSD) || defined(MTS) || defined(__GO32__))
  1546. #ifdef BSD4_4
  1547. if ( (dosdatetime >= DOSTIME_2038_01_18) &&
  1548. (m_time < (time_t)0x70000000L) )
  1549. m_time = U_TIME_T_MAX; /* saturate in case of (unsigned) overflow */
  1550. if (m_time < (time_t)0L) /* a converted DOS time cannot be negative */
  1551. m_time = S_TIME_T_MAX; /* -> saturate at max signed time_t value */
  1552. if ((tm = localtime(&m_time)) != (struct tm *)NULL)
  1553. m_time -= tm->tm_gmtoff; /* sec. EAST of GMT: subtr. */
  1554. #else /* !(BSD4_4 */
  1555. ftime(&tbp); /* get `timezone' */
  1556. m_time += tbp.timezone * 60L; /* seconds WEST of GMT: add */
  1557. #endif /* ?(BSD4_4 || __EMX__) */
  1558. #else /* !(BSD || MTS || __GO32__) */
  1559. /* tzset was already called at start of process_zipfiles() */
  1560. /* tzset(); */ /* set `timezone' variable */
  1561. #ifndef __BEOS__ /* BeOS DR8 has no timezones... */
  1562. m_time += timezone; /* seconds WEST of GMT: add */
  1563. #endif
  1564. #endif /* ?(BSD || MTS || __GO32__) */
  1565. #endif /* ?WIN32 */
  1566. TTrace((stderr, " m_time after timezone = %lu\n", (ulg)m_time));
  1567. /*---------------------------------------------------------------------------
  1568. Adjust for local daylight savings (summer) time.
  1569. ---------------------------------------------------------------------------*/
  1570. #ifndef BSD4_4 /* (DST already added to tm_gmtoff, so skip tm_isdst) */
  1571. if ( (dosdatetime >= DOSTIME_2038_01_18) &&
  1572. (m_time < (time_t)0x70000000L) )
  1573. m_time = U_TIME_T_MAX; /* saturate in case of (unsigned) overflow */
  1574. if (m_time < (time_t)0L) /* a converted DOS time cannot be negative */
  1575. m_time = S_TIME_T_MAX; /* -> saturate at max signed time_t value */
  1576. TIMET_TO_NATIVE(m_time) /* NOP unless MSC 7.0 or Macintosh */
  1577. if (((tm = localtime((time_t *)&m_time)) != NULL) && tm->tm_isdst)
  1578. #ifdef WIN32
  1579. m_time += 60L * tzinfo.DaylightBias; /* adjust with DST bias */
  1580. else
  1581. m_time += 60L * tzinfo.StandardBias; /* add StdBias (normally 0) */
  1582. #else
  1583. m_time -= 60L * 60L; /* adjust for daylight savings time */
  1584. #endif
  1585. NATIVE_TO_TIMET(m_time) /* NOP unless MSC 7.0 or Macintosh */
  1586. TTrace((stderr, " m_time after DST = %lu\n", (ulg)m_time));
  1587. #endif /* !BSD4_4 */
  1588. #ifdef WIN32
  1589. }
  1590. #endif
  1591. #endif /* !MACOS && !RISCOS && !QDOS && !TANDEM */
  1592. #endif /* ?TOPS20 */
  1593. #endif /* ?HAVE_MKTIME */
  1594. if ( (dosdatetime >= DOSTIME_2038_01_18) &&
  1595. (m_time < (time_t)0x70000000L) )
  1596. m_time = U_TIME_T_MAX; /* saturate in case of (unsigned) overflow */
  1597. if (m_time < (time_t)0L) /* a converted DOS time cannot be negative */
  1598. m_time = S_TIME_T_MAX; /* -> saturate at max signed time_t value */
  1599. return m_time;
  1600. } /* end function dos_to_unix_time() */
  1601. #endif /* !OS2 || TIMESTAMP */
  1602. #endif /* !VMS && !CMS_MVS */
  1603. #if (!defined(VMS) && !defined(OS2) && !defined(CMS_MVS))
  1604. /******************************/
  1605. /* Function check_for_newer() */ /* used for overwriting/freshening/updating */
  1606. /******************************/
  1607. int check_for_newer(__G__ filename) /* return 1 if existing file is newer */
  1608. __GDEF /* or equal; 0 if older; -1 if doesn't */
  1609. char *filename; /* exist yet */
  1610. {
  1611. time_t existing, archive;
  1612. #ifdef USE_EF_UT_TIME
  1613. iztimes z_utime;
  1614. #endif
  1615. #ifdef AOS_VS
  1616. long dyy, dmm, ddd, dhh, dmin, dss;
  1617. dyy = (lrec.last_mod_dos_datetime >> 25) + 1980;
  1618. dmm = (lrec.last_mod_dos_datetime >> 21) & 0x0f;
  1619. ddd = (lrec.last_mod_dos_datetime >> 16) & 0x1f;
  1620. dhh = (lrec.last_mod_dos_datetime >> 11) & 0x1f;
  1621. dmin = (lrec.last_mod_dos_datetime >> 5) & 0x3f;
  1622. dss = (lrec.last_mod_dos_datetime & 0x1f) * 2;
  1623. /* under AOS/VS, file times can only be set at creation time,
  1624. * with the info in a special DG format. Make sure we can create
  1625. * it here - we delete it later & re-create it, whether or not
  1626. * it exists now.
  1627. */
  1628. if (!zvs_create(filename, (((ulg)dgdate(dmm, ddd, dyy)) << 16) |
  1629. (dhh*1800L + dmin*30L + dss/2L), -1L, -1L, (char *) -1, -1, -1, -1))
  1630. return DOES_NOT_EXIST;
  1631. #endif /* AOS_VS */
  1632. Trace((stderr, "check_for_newer: doing stat(%s)\n", FnFilter1(filename)));
  1633. if (SSTAT(filename, &G.statbuf)) {
  1634. Trace((stderr,
  1635. "check_for_newer: stat(%s) returns %d: file does not exist\n",
  1636. FnFilter1(filename), SSTAT(filename, &G.statbuf)));
  1637. #ifdef SYMLINKS
  1638. Trace((stderr, "check_for_newer: doing lstat(%s)\n",
  1639. FnFilter1(filename)));
  1640. /* GRR OPTION: could instead do this test ONLY if G.symlnk is true */
  1641. if (lstat(filename, &G.statbuf) == 0) {
  1642. Trace((stderr,
  1643. "check_for_newer: lstat(%s) returns 0: symlink does exist\n",
  1644. FnFilter1(filename)));
  1645. if (QCOND2 && !IS_OVERWRT_ALL)
  1646. Info(slide, 0, ((char *)slide, LoadFarString(FileIsSymLink),
  1647. FnFilter1(filename), " with no real file"));
  1648. return EXISTS_AND_OLDER; /* symlink dates are meaningless */
  1649. }
  1650. #endif /* SYMLINKS */
  1651. return DOES_NOT_EXIST;
  1652. }
  1653. Trace((stderr, "check_for_newer: stat(%s) returns 0: file exists\n",
  1654. FnFilter1(filename)));
  1655. #ifdef SYMLINKS
  1656. /* GRR OPTION: could instead do this test ONLY if G.symlnk is true */
  1657. if (lstat(filename, &G.statbuf) == 0 && S_ISLNK(G.statbuf.st_mode)) {
  1658. Trace((stderr, "check_for_newer: %s is a symbolic link\n",
  1659. FnFilter1(filename)));
  1660. if (QCOND2 && !IS_OVERWRT_ALL)
  1661. Info(slide, 0, ((char *)slide, LoadFarString(FileIsSymLink),
  1662. FnFilter1(filename), ""));
  1663. return EXISTS_AND_OLDER; /* symlink dates are meaningless */
  1664. }
  1665. #endif /* SYMLINKS */
  1666. NATIVE_TO_TIMET(G.statbuf.st_mtime) /* NOP unless MSC 7.0 or Macintosh */
  1667. #ifdef USE_EF_UT_TIME
  1668. /* The `Unix extra field mtime' should be used for comparison with the
  1669. * time stamp of the existing file >>>ONLY<<< when the EF info is also
  1670. * used to set the modification time of the extracted file.
  1671. */
  1672. if (G.extra_field &&
  1673. #ifdef IZ_CHECK_TZ
  1674. G.tz_is_valid &&
  1675. #endif
  1676. (ef_scan_for_izux(G.extra_field, G.lrec.extra_field_length, 0,
  1677. G.lrec.last_mod_dos_datetime, &z_utime, NULL)
  1678. & EB_UT_FL_MTIME))
  1679. {
  1680. TTrace((stderr, "check_for_newer: using Unix extra field mtime\n"));
  1681. existing = G.statbuf.st_mtime;
  1682. archive = z_utime.mtime;
  1683. } else {
  1684. /* round up existing filetime to nearest 2 seconds for comparison,
  1685. * but saturate in case of arithmetic overflow
  1686. */
  1687. existing = ((G.statbuf.st_mtime & 1) &&
  1688. (G.statbuf.st_mtime + 1 > G.statbuf.st_mtime)) ?
  1689. G.statbuf.st_mtime + 1 : G.statbuf.st_mtime;
  1690. archive = dos_to_unix_time(G.lrec.last_mod_dos_datetime);
  1691. }
  1692. #else /* !USE_EF_UT_TIME */
  1693. /* round up existing filetime to nearest 2 seconds for comparison,
  1694. * but saturate in case of arithmetic overflow
  1695. */
  1696. existing = ((G.statbuf.st_mtime & 1) &&
  1697. (G.statbuf.st_mtime + 1 > G.statbuf.st_mtime)) ?
  1698. G.statbuf.st_mtime + 1 : G.statbuf.st_mtime;
  1699. archive = dos_to_unix_time(G.lrec.last_mod_dos_datetime);
  1700. #endif /* ?USE_EF_UT_TIME */
  1701. TTrace((stderr, "check_for_newer: existing %lu, archive %lu, e-a %ld\n",
  1702. (ulg)existing, (ulg)archive, (long)(existing-archive)));
  1703. return (existing >= archive);
  1704. } /* end function check_for_newer() */
  1705. #endif /* !VMS && !OS2 && !CMS_MVS */
  1706. /************************/
  1707. /* Function do_string() */
  1708. /************************/
  1709. int do_string(__G__ length, option) /* return PK-type error code */
  1710. __GDEF
  1711. unsigned int length; /* without prototype, ush converted to this */
  1712. int option;
  1713. {
  1714. unsigned comment_bytes_left;
  1715. unsigned int block_len;
  1716. int error=PK_OK;
  1717. #ifdef AMIGA
  1718. char tmp_fnote[2 * AMIGA_FILENOTELEN]; /* extra room for squozen chars */
  1719. #endif
  1720. /*---------------------------------------------------------------------------
  1721. This function processes arbitrary-length (well, usually) strings. Four
  1722. major options are allowed: SKIP, wherein the string is skipped (pretty
  1723. logical, eh?); DISPLAY, wherein the string is printed to standard output
  1724. after undergoing any necessary or unnecessary character conversions;
  1725. DS_FN, wherein the string is put into the filename[] array after under-
  1726. going appropriate conversions (including case-conversion, if that is
  1727. indicated: see the global variable pInfo->lcflag); and EXTRA_FIELD,
  1728. wherein the `string' is assumed to be an extra field and is copied to
  1729. the (freshly malloced) buffer G.extra_field. The third option should
  1730. be OK since filename is dimensioned at 1025, but we check anyway.
  1731. The string, by the way, is assumed to start at the current file-pointer
  1732. position; its length is given by 'length'. So start off by checking the
  1733. length of the string: if zero, we're already done.
  1734. ---------------------------------------------------------------------------*/
  1735. if (!length)
  1736. return PK_COOL;
  1737. switch (option) {
  1738. #if (defined(SFX) && defined(CHEAP_SFX_AUTORUN))
  1739. /*
  1740. * Special case: See if the comment begins with an autorun command line.
  1741. * Save that and display (or skip) the remainder.
  1742. */
  1743. case CHECK_AUTORUN:
  1744. case CHECK_AUTORUN_Q:
  1745. comment_bytes_left = length;
  1746. if (length >= 10)
  1747. {
  1748. block_len = readbuf(__G__ (char *)G.outbuf, 10);
  1749. if (block_len == 0)
  1750. return PK_EOF;
  1751. comment_bytes_left -= block_len;
  1752. G.outbuf[block_len] = '\0';
  1753. if (!strcmp((char *)G.outbuf, "$AUTORUN$>")) {
  1754. char *eol;
  1755. length -= 10;
  1756. block_len = readbuf(__G__ G.autorun_command,
  1757. MIN(length, sizeof(G.autorun_command)-1));
  1758. if (block_len == 0)
  1759. return PK_EOF;
  1760. comment_bytes_left -= block_len;
  1761. G.autorun_command[block_len] = '\0';
  1762. A_TO_N(G.autorun_command);
  1763. eol = strchr(G.autorun_command, '\n');
  1764. if (!eol)
  1765. eol = G.autorun_command + strlen(G.autorun_command) - 1;
  1766. length -= eol + 1 - G.autorun_command;
  1767. while (eol >= G.autorun_command && isspace(*eol))
  1768. *eol-- = '\0';
  1769. #if (defined(WIN32) && !defined(_WIN32_WCE))
  1770. /* Win9x console always uses OEM character coding, and
  1771. WinNT console is set to OEM charset by default, too */
  1772. INTERN_TO_OEM(G.autorun_command, G.autorun_command);
  1773. #endif /* (WIN32 && !_WIN32_WCE) */
  1774. }
  1775. }
  1776. if (option == CHECK_AUTORUN_Q) /* don't display the remainder */
  1777. length = 0;
  1778. /* seek to beginning of remaining part of comment -- rewind if */
  1779. /* displaying entire comment, or skip to end if discarding it */
  1780. seek_zipf(__G__ G.cur_zipfile_bufstart - G.extra_bytes +
  1781. (G.inptr - G.inbuf) + comment_bytes_left - length);
  1782. if (!length)
  1783. break;
  1784. /* FALL THROUGH... */
  1785. #endif /* SFX && CHEAP_SFX_AUTORUN */
  1786. /*
  1787. * First normal case: print string on standard output. First set loop
  1788. * variables, then loop through the comment in chunks of OUTBUFSIZ bytes,
  1789. * converting formats and printing as we go. The second half of the
  1790. * loop conditional was added because the file might be truncated, in
  1791. * which case comment_bytes_left will remain at some non-zero value for
  1792. * all time. outbuf and slide are used as scratch buffers because they
  1793. * are available (we should be either before or in between any file pro-
  1794. * cessing).
  1795. */
  1796. case DISPLAY:
  1797. case DISPL_8:
  1798. comment_bytes_left = length;
  1799. block_len = OUTBUFSIZ; /* for the while statement, first time */
  1800. while (comment_bytes_left > 0 && block_len > 0) {
  1801. register uch *p = G.outbuf;
  1802. register uch *q = G.outbuf;
  1803. if ((block_len = readbuf(__G__ (char *)G.outbuf,
  1804. MIN((unsigned)OUTBUFSIZ, comment_bytes_left))) == 0)
  1805. return PK_EOF;
  1806. comment_bytes_left -= block_len;
  1807. /* this is why we allocated an extra byte for outbuf: terminate
  1808. * with zero (ASCIIZ) */
  1809. G.outbuf[block_len] = '\0';
  1810. /* remove all ASCII carriage returns from comment before printing
  1811. * (since used before A_TO_N(), check for CR instead of '\r')
  1812. */
  1813. while (*p) {
  1814. while (*p == CR)
  1815. ++p;
  1816. *q++ = *p++;
  1817. }
  1818. /* could check whether (p - outbuf) == block_len here */
  1819. *q = '\0';
  1820. if (option == DISPL_8) {
  1821. /* translate the text coded in the entry's host-dependent
  1822. "extended ASCII" charset into the compiler's (system's)
  1823. internal text code page */
  1824. Ext_ASCII_TO_Native((char *)G.outbuf, G.pInfo->hostnum,
  1825. G.pInfo->hostver, G.pInfo->HasUxAtt,
  1826. FALSE);
  1827. #ifdef WINDLL
  1828. /* translate to ANSI (RTL internal codepage may be OEM) */
  1829. INTERN_TO_ISO((char *)G.outbuf, (char *)G.outbuf);
  1830. #else /* !WINDLL */
  1831. #if (defined(WIN32) && !defined(_WIN32_WCE))
  1832. /* Win9x console always uses OEM character coding, and
  1833. WinNT console is set to OEM charset by default, too */
  1834. INTERN_TO_OEM((char *)G.outbuf, (char *)G.outbuf);
  1835. #endif /* (WIN32 && !_WIN32_WCE) */
  1836. #endif /* ?WINDLL */
  1837. } else {
  1838. A_TO_N(G.outbuf); /* translate string to native */
  1839. }
  1840. #ifdef WINDLL
  1841. /* ran out of local mem -- had to cheat */
  1842. win_fprintf((zvoid *)&G, stdout, (extent)(q-G.outbuf),
  1843. (char *)G.outbuf);
  1844. win_fprintf((zvoid *)&G, stdout, 2, (char *)"\n\n");
  1845. #else /* !WINDLL */
  1846. #ifdef NOANSIFILT /* GRR: can ANSI be used with EBCDIC? */
  1847. (*G.message)((zvoid *)&G, G.outbuf, (ulg)(q-G.outbuf), 0);
  1848. #else /* ASCII, filter out ANSI escape sequences and handle ^S (pause) */
  1849. p = G.outbuf - 1;
  1850. q = slide;
  1851. while (*++p) {
  1852. int pause = FALSE;
  1853. if (*p == 0x1B) { /* ASCII escape char */
  1854. *q++ = '^';
  1855. *q++ = '[';
  1856. } else if (*p == 0x13) { /* ASCII ^S (pause) */
  1857. pause = TRUE;
  1858. if (p[1] == LF) /* ASCII LF */
  1859. *q++ = *++p;
  1860. else if (p[1] == CR && p[2] == LF) { /* ASCII CR LF */
  1861. *q++ = *++p;
  1862. *q++ = *++p;
  1863. }
  1864. } else
  1865. *q++ = *p;
  1866. if ((unsigned)(q-slide) > WSIZE-3 || pause) { /* flush */
  1867. (*G.message)((zvoid *)&G, slide, (ulg)(q-slide), 0);
  1868. q = slide;
  1869. if (pause && G.extract_flag) /* don't pause for list/test */
  1870. (*G.mpause)((zvoid *)&G, LoadFarString(QuitPrompt), 0);
  1871. }
  1872. }
  1873. (*G.message)((zvoid *)&G, slide, (ulg)(q-slide), 0);
  1874. #endif /* ?NOANSIFILT */
  1875. #endif /* ?WINDLL */
  1876. }
  1877. /* add '\n' if not at start of line */
  1878. (*G.message)((zvoid *)&G, slide, 0L, 0x40);
  1879. break;
  1880. /*
  1881. * Second case: read string into filename[] array. The filename should
  1882. * never ever be longer than FILNAMSIZ-1 (1024), but for now we'll check,
  1883. * just to be sure.
  1884. */
  1885. case DS_FN:
  1886. case DS_FN_L:
  1887. #ifdef UNICODE_SUPPORT
  1888. /* get the whole filename as need it for Unicode checksum */
  1889. if (G.fnfull_bufsize <= length) {
  1890. extent fnbufsiz = FILNAMSIZ;
  1891. if (fnbufsiz <= length)
  1892. fnbufsiz = length + 1;
  1893. if (G.filename_full)
  1894. free(G.filename_full);
  1895. G.filename_full = malloc(fnbufsiz);
  1896. if (G.filename_full == NULL)
  1897. return PK_MEM;
  1898. G.fnfull_bufsize = fnbufsiz;
  1899. }
  1900. if (readbuf(__G__ G.filename_full, length) == 0)
  1901. return PK_EOF;
  1902. G.filename_full[length] = '\0'; /* terminate w/zero: ASCIIZ */
  1903. /* if needed, chop off end so standard filename is a valid length */
  1904. if (length >= FILNAMSIZ) {
  1905. Info(slide, 0x401, ((char *)slide,
  1906. LoadFarString(FilenameTooLongTrunc)));
  1907. error = PK_WARN;
  1908. length = FILNAMSIZ - 1;
  1909. }
  1910. /* no excess size */
  1911. block_len = 0;
  1912. strncpy(G.filename, G.filename_full, length);
  1913. G.filename[length] = '\0'; /* terminate w/zero: ASCIIZ */
  1914. #else /* !UNICODE_SUPPORT */
  1915. if (length >= FILNAMSIZ) {
  1916. Info(slide, 0x401, ((char *)slide,
  1917. LoadFarString(FilenameTooLongTrunc)));
  1918. error = PK_WARN;
  1919. /* remember excess length in block_len */
  1920. block_len = length - (FILNAMSIZ - 1);
  1921. length = FILNAMSIZ - 1;
  1922. } else
  1923. /* no excess size */
  1924. block_len = 0;
  1925. if (readbuf(__G__ G.filename, length) == 0)
  1926. return PK_EOF;
  1927. G.filename[length] = '\0'; /* terminate w/zero: ASCIIZ */
  1928. #endif /* ?UNICODE_SUPPORT */
  1929. /* translate the Zip entry filename coded in host-dependent "extended
  1930. ASCII" into the compiler's (system's) internal text code page */
  1931. Ext_ASCII_TO_Native(G.filename, G.pInfo->hostnum, G.pInfo->hostver,
  1932. G.pInfo->HasUxAtt, (option == DS_FN_L));
  1933. if (G.pInfo->lcflag) /* replace with lowercase filename */
  1934. STRLOWER(G.filename, G.filename);
  1935. if (G.pInfo->vollabel && length > 8 && G.filename[8] == '.') {
  1936. char *p = G.filename+8;
  1937. while (*p++)
  1938. p[-1] = *p; /* disk label, and 8th char is dot: remove dot */
  1939. }
  1940. if (!block_len) /* no overflow, we're done here */
  1941. break;
  1942. /*
  1943. * We truncated the filename, so print what's left and then fall
  1944. * through to the SKIP routine.
  1945. */
  1946. Info(slide, 0x401, ((char *)slide, "[ %s ]\n", FnFilter1(G.filename)));
  1947. length = block_len; /* SKIP the excess bytes... */
  1948. /* FALL THROUGH... */
  1949. /*
  1950. * Third case: skip string, adjusting readbuf's internal variables
  1951. * as necessary (and possibly skipping to and reading a new block of
  1952. * data).
  1953. */
  1954. case SKIP:
  1955. /* cur_zipfile_bufstart already takes account of extra_bytes, so don't
  1956. * correct for it twice: */
  1957. seek_zipf(__G__ G.cur_zipfile_bufstart - G.extra_bytes +
  1958. (G.inptr-G.inbuf) + length);
  1959. break;
  1960. /*
  1961. * Fourth case: assume we're at the start of an "extra field"; malloc
  1962. * storage for it and read data into the allocated space.
  1963. */
  1964. case EXTRA_FIELD:
  1965. if (G.extra_field != (uch *)NULL)
  1966. free(G.extra_field);
  1967. if ((G.extra_field = (uch *)malloc(length)) == (uch *)NULL) {
  1968. Info(slide, 0x401, ((char *)slide, LoadFarString(ExtraFieldTooLong),
  1969. length));
  1970. /* cur_zipfile_bufstart already takes account of extra_bytes,
  1971. * so don't correct for it twice: */
  1972. seek_zipf(__G__ G.cur_zipfile_bufstart - G.extra_bytes +
  1973. (G.inptr-G.inbuf) + length);
  1974. } else {
  1975. if (readbuf(__G__ (char *)G.extra_field, length) == 0)
  1976. return PK_EOF;
  1977. /* Looks like here is where extra fields are read */
  1978. getZip64Data(__G__ G.extra_field, length);
  1979. #ifdef UNICODE_SUPPORT
  1980. G.unipath_filename = NULL;
  1981. if (G.UzO.U_flag < 2) {
  1982. /* check if GPB11 (General Purpuse Bit 11) is set indicating
  1983. the standard path and comment are UTF-8 */
  1984. if (G.pInfo->GPFIsUTF8) {
  1985. /* if GPB11 set then filename_full is untruncated UTF-8 */
  1986. G.unipath_filename = G.filename_full;
  1987. } else {
  1988. /* Get the Unicode fields if exist */
  1989. getUnicodeData(__G__ G.extra_field, length);
  1990. if (G.unipath_filename && strlen(G.unipath_filename) == 0) {
  1991. /* the standard filename field is UTF-8 */
  1992. free(G.unipath_filename);
  1993. G.unipath_filename = G.filename_full;
  1994. }
  1995. }
  1996. if (G.unipath_filename) {
  1997. # ifdef UTF8_MAYBE_NATIVE
  1998. if (G.native_is_utf8
  1999. # ifdef UNICODE_WCHAR
  2000. && (!G.unicode_escape_all)
  2001. # endif
  2002. ) {
  2003. strncpy(G.filename, G.unipath_filename, FILNAMSIZ - 1);
  2004. /* make sure filename is short enough */
  2005. if (strlen(G.unipath_filename) >= FILNAMSIZ) {
  2006. G.filename[FILNAMSIZ - 1] = '\0';
  2007. Info(slide, 0x401, ((char *)slide,
  2008. LoadFarString(UFilenameTooLongTrunc)));
  2009. error = PK_WARN;
  2010. }
  2011. }
  2012. # ifdef UNICODE_WCHAR
  2013. else
  2014. # endif
  2015. # endif /* UTF8_MAYBE_NATIVE */
  2016. # ifdef UNICODE_WCHAR
  2017. {
  2018. char *fn;
  2019. /* convert UTF-8 to local character set */
  2020. fn = utf8_to_local_string(G.unipath_filename,
  2021. G.unicode_escape_all);
  2022. /* make sure filename is short enough */
  2023. if (strlen(fn) >= FILNAMSIZ) {
  2024. fn[FILNAMSIZ - 1] = '\0';
  2025. Info(slide, 0x401, ((char *)slide,
  2026. LoadFarString(UFilenameTooLongTrunc)));
  2027. error = PK_WARN;
  2028. }
  2029. /* replace filename with converted UTF-8 */
  2030. strcpy(G.filename, fn);
  2031. free(fn);
  2032. }
  2033. # endif /* UNICODE_WCHAR */
  2034. if (G.unipath_filename != G.filename_full)
  2035. free(G.unipath_filename);
  2036. G.unipath_filename = NULL;
  2037. }
  2038. }
  2039. #endif /* UNICODE_SUPPORT */
  2040. }
  2041. break;
  2042. #ifdef AMIGA
  2043. /*
  2044. * Fifth case, for the Amiga only: take the comment that would ordinarily
  2045. * be skipped over, and turn it into a 79 character string that will be
  2046. * attached to the file as a "filenote" after it is extracted.
  2047. */
  2048. case FILENOTE:
  2049. if ((block_len = readbuf(__G__ tmp_fnote, (unsigned)
  2050. MIN(length, 2 * AMIGA_FILENOTELEN - 1))) == 0)
  2051. return PK_EOF;
  2052. if ((length -= block_len) > 0) /* treat remainder as in case SKIP: */
  2053. seek_zipf(__G__ G.cur_zipfile_bufstart - G.extra_bytes
  2054. + (G.inptr - G.inbuf) + length);
  2055. /* convert multi-line text into single line with no ctl-chars: */
  2056. tmp_fnote[block_len] = '\0';
  2057. while ((short int) --block_len >= 0)
  2058. if ((unsigned) tmp_fnote[block_len] < ' ')
  2059. if (tmp_fnote[block_len+1] == ' ') /* no excess */
  2060. strcpy(tmp_fnote+block_len, tmp_fnote+block_len+1);
  2061. else
  2062. tmp_fnote[block_len] = ' ';
  2063. tmp_fnote[AMIGA_FILENOTELEN - 1] = '\0';
  2064. if (G.filenotes[G.filenote_slot])
  2065. free(G.filenotes[G.filenote_slot]); /* should not happen */
  2066. G.filenotes[G.filenote_slot] = NULL;
  2067. if (tmp_fnote[0]) {
  2068. if (!(G.filenotes[G.filenote_slot] = malloc(strlen(tmp_fnote)+1)))
  2069. return PK_MEM;
  2070. strcpy(G.filenotes[G.filenote_slot], tmp_fnote);
  2071. }
  2072. break;
  2073. #endif /* AMIGA */
  2074. } /* end switch (option) */
  2075. return error;
  2076. } /* end function do_string() */
  2077. /***********************/
  2078. /* Function makeword() */
  2079. /***********************/
  2080. ush makeword(b)
  2081. ZCONST uch *b;
  2082. {
  2083. /*
  2084. * Convert Intel style 'short' integer to non-Intel non-16-bit
  2085. * host format. This routine also takes care of byte-ordering.
  2086. */
  2087. return (ush)((b[1] << 8) | b[0]);
  2088. }
  2089. /***********************/
  2090. /* Function makelong() */
  2091. /***********************/
  2092. ulg makelong(sig)
  2093. ZCONST uch *sig;
  2094. {
  2095. /*
  2096. * Convert intel style 'long' variable to non-Intel non-16-bit
  2097. * host format. This routine also takes care of byte-ordering.
  2098. */
  2099. return (((ulg)sig[3]) << 24)
  2100. + (((ulg)sig[2]) << 16)
  2101. + (ulg)((((unsigned)sig[1]) << 8)
  2102. + ((unsigned)sig[0]));
  2103. }
  2104. /************************/
  2105. /* Function makeint64() */
  2106. /************************/
  2107. zusz_t makeint64(sig)
  2108. ZCONST uch *sig;
  2109. {
  2110. #ifdef LARGE_FILE_SUPPORT
  2111. /*
  2112. * Convert intel style 'int64' variable to non-Intel non-16-bit
  2113. * host format. This routine also takes care of byte-ordering.
  2114. */
  2115. return (((zusz_t)sig[7]) << 56)
  2116. + (((zusz_t)sig[6]) << 48)
  2117. + (((zusz_t)sig[4]) << 32)
  2118. + (zusz_t)((((ulg)sig[3]) << 24)
  2119. + (((ulg)sig[2]) << 16)
  2120. + (((unsigned)sig[1]) << 8)
  2121. + (sig[0]));
  2122. #else /* !LARGE_FILE_SUPPORT */
  2123. if ((sig[7] | sig[6] | sig[5] | sig[4]) != 0)
  2124. return (zusz_t)0xffffffffL;
  2125. else
  2126. return (zusz_t)((((ulg)sig[3]) << 24)
  2127. + (((ulg)sig[2]) << 16)
  2128. + (((unsigned)sig[1]) << 8)
  2129. + (sig[0]));
  2130. #endif /* ?LARGE_FILE_SUPPORT */
  2131. }
  2132. /*********************/
  2133. /* Function fzofft() */
  2134. /*********************/
  2135. /* Format a zoff_t value in a cylindrical buffer set. */
  2136. char *fzofft(__G__ val, pre, post)
  2137. __GDEF
  2138. zoff_t val;
  2139. ZCONST char *pre;
  2140. ZCONST char *post;
  2141. {
  2142. /* Storage cylinder. (now in globals.h) */
  2143. /*static char fzofft_buf[FZOFFT_NUM][FZOFFT_LEN];*/
  2144. /*static int fzofft_index = 0;*/
  2145. /* Temporary format string storage. */
  2146. char fmt[16];
  2147. /* Assemble the format string. */
  2148. fmt[0] = '%';
  2149. fmt[1] = '\0'; /* Start after initial "%". */
  2150. if (pre == FZOFFT_HEX_WID) /* Special hex width. */
  2151. {
  2152. strcat(fmt, FZOFFT_HEX_WID_VALUE);
  2153. }
  2154. else if (pre == FZOFFT_HEX_DOT_WID) /* Special hex ".width". */
  2155. {
  2156. strcat(fmt, ".");
  2157. strcat(fmt, FZOFFT_HEX_WID_VALUE);
  2158. }
  2159. else if (pre != NULL) /* Caller's prefix (width). */
  2160. {
  2161. strcat(fmt, pre);
  2162. }
  2163. strcat(fmt, FZOFFT_FMT); /* Long or long-long or whatever. */
  2164. if (post == NULL)
  2165. strcat(fmt, "d"); /* Default radix = decimal. */
  2166. else
  2167. strcat(fmt, post); /* Caller's radix. */
  2168. /* Advance the cylinder. */
  2169. G.fzofft_index = (G.fzofft_index + 1) % FZOFFT_NUM;
  2170. /* Write into the current chamber. */
  2171. sprintf(G.fzofft_buf[G.fzofft_index], fmt, val);
  2172. /* Return a pointer to this chamber. */
  2173. return G.fzofft_buf[G.fzofft_index];
  2174. }
  2175. #if CRYPT
  2176. #ifdef NEED_STR2ISO
  2177. /**********************/
  2178. /* Function str2iso() */
  2179. /**********************/
  2180. char *str2iso(dst, src)
  2181. char *dst; /* destination buffer */
  2182. register ZCONST char *src; /* source string */
  2183. {
  2184. #ifdef INTERN_TO_ISO
  2185. INTERN_TO_ISO(src, dst);
  2186. #else
  2187. register uch c;
  2188. register char *dstp = dst;
  2189. do {
  2190. c = (uch)foreign(*src++);
  2191. *dstp++ = (char)ASCII2ISO(c);
  2192. } while (c != '\0');
  2193. #endif
  2194. return dst;
  2195. }
  2196. #endif /* NEED_STR2ISO */
  2197. #ifdef NEED_STR2OEM
  2198. /**********************/
  2199. /* Function str2oem() */
  2200. /**********************/
  2201. char *str2oem(dst, src)
  2202. char *dst; /* destination buffer */
  2203. register ZCONST char *src; /* source string */
  2204. {
  2205. #ifdef INTERN_TO_OEM
  2206. INTERN_TO_OEM(src, dst);
  2207. #else
  2208. register uch c;
  2209. register char *dstp = dst;
  2210. do {
  2211. c = (uch)foreign(*src++);
  2212. *dstp++ = (char)ASCII2OEM(c);
  2213. } while (c != '\0');
  2214. #endif
  2215. return dst;
  2216. }
  2217. #endif /* NEED_STR2OEM */
  2218. #endif /* CRYPT */
  2219. #ifdef ZMEM /* memset/memcmp/memcpy for systems without either them or */
  2220. /* bzero/bcmp/bcopy */
  2221. /* (no known systems as of 960211) */
  2222. /*********************/
  2223. /* Function memset() */
  2224. /*********************/
  2225. zvoid *memset(buf, init, len)
  2226. register zvoid *buf; /* buffer location */
  2227. register int init; /* initializer character */
  2228. register unsigned int len; /* length of the buffer */
  2229. {
  2230. zvoid *start;
  2231. start = buf;
  2232. while (len--)
  2233. *((char *)buf++) = (char)init;
  2234. return start;
  2235. }
  2236. /*********************/
  2237. /* Function memcmp() */
  2238. /*********************/
  2239. int memcmp(b1, b2, len)
  2240. register ZCONST zvoid *b1;
  2241. register ZCONST zvoid *b2;
  2242. register unsigned int len;
  2243. {
  2244. register int c;
  2245. if (len > 0) do {
  2246. if ((c = (int)(*((ZCONST unsigned char *)b1)++) -
  2247. (int)(*((ZCONST unsigned char *)b2)++)) != 0)
  2248. return c;
  2249. } while (--len > 0)
  2250. return 0;
  2251. }
  2252. /*********************/
  2253. /* Function memcpy() */
  2254. /*********************/
  2255. zvoid *memcpy(dst, src, len)
  2256. register zvoid *dst;
  2257. register ZCONST zvoid *src;
  2258. register unsigned int len;
  2259. {
  2260. zvoid *start;
  2261. start = dst;
  2262. while (len-- > 0)
  2263. *((char *)dst)++ = *((ZCONST char *)src)++;
  2264. return start;
  2265. }
  2266. #endif /* ZMEM */
  2267. #ifdef NO_STRNICMP
  2268. /************************/
  2269. /* Function zstrnicmp() */
  2270. /************************/
  2271. int zstrnicmp(s1, s2, n)
  2272. register ZCONST char *s1, *s2;
  2273. register unsigned n;
  2274. {
  2275. for (; n > 0; --n, ++s1, ++s2) {
  2276. if (ToLower(*s1) != ToLower(*s2))
  2277. /* test includes early termination of one string */
  2278. return ((uch)ToLower(*s1) < (uch)ToLower(*s2))? -1 : 1;
  2279. if (*s1 == '\0') /* both strings terminate early */
  2280. return 0;
  2281. }
  2282. return 0;
  2283. }
  2284. #endif /* NO_STRNICMP */
  2285. #ifdef REGULUS /* returns the inode number on success(!)...argh argh argh */
  2286. # undef stat
  2287. /********************/
  2288. /* Function zstat() */
  2289. /********************/
  2290. int zstat(p, s)
  2291. ZCONST char *p;
  2292. struct stat *s;
  2293. {
  2294. return (stat((char *)p,s) >= 0? 0 : (-1));
  2295. }
  2296. #endif /* REGULUS */
  2297. #ifdef _MBCS
  2298. /* DBCS support for Info-ZIP's zip (mainly for japanese (-: )
  2299. * by Yoshioka Tsuneo (QWF00133@nifty.ne.jp,tsuneo-y@is.aist-nara.ac.jp)
  2300. * This code is public domain! Date: 1998/12/20
  2301. */
  2302. /************************/
  2303. /* Function plastchar() */
  2304. /************************/
  2305. char *plastchar(ptr, len)
  2306. ZCONST char *ptr;
  2307. extent len;
  2308. {
  2309. unsigned clen;
  2310. ZCONST char *oldptr = ptr;
  2311. while(*ptr != '\0' && len > 0){
  2312. oldptr = ptr;
  2313. clen = CLEN(ptr);
  2314. ptr += clen;
  2315. len -= clen;
  2316. }
  2317. return (char *)oldptr;
  2318. }
  2319. #ifdef NEED_UZMBCLEN
  2320. /***********************/
  2321. /* Function uzmbclen() */
  2322. /***********************/
  2323. extent uzmbclen(ptr)
  2324. ZCONST unsigned char *ptr;
  2325. {
  2326. int mbl;
  2327. mbl = mblen((ZCONST char *)ptr, MB_CUR_MAX);
  2328. /* For use in code scanning through MBCS strings, we need a strictly
  2329. positive "MB char bytes count". For our scanning purpose, it is not
  2330. not relevant whether the MB character is valid or not. And, the NUL
  2331. char '\0' has a byte count of 1, but mblen() returns 0. So, we make
  2332. sure that the uzmbclen() return value is not less than 1.
  2333. */
  2334. return (extent)(mbl > 0 ? mbl : 1);
  2335. }
  2336. #endif /* NEED_UZMBCLEN */
  2337. #ifdef NEED_UZMBSCHR
  2338. /***********************/
  2339. /* Function uzmbschr() */
  2340. /***********************/
  2341. unsigned char *uzmbschr(str, c)
  2342. ZCONST unsigned char *str;
  2343. unsigned int c;
  2344. {
  2345. while(*str != '\0'){
  2346. if (*str == c) {return (unsigned char *)str;}
  2347. INCSTR(str);
  2348. }
  2349. return NULL;
  2350. }
  2351. #endif /* NEED_UZMBSCHR */
  2352. #ifdef NEED_UZMBSRCHR
  2353. /************************/
  2354. /* Function uzmbsrchr() */
  2355. /************************/
  2356. unsigned char *uzmbsrchr(str, c)
  2357. ZCONST unsigned char *str;
  2358. unsigned int c;
  2359. {
  2360. unsigned char *match = NULL;
  2361. while(*str != '\0'){
  2362. if (*str == c) {match = (unsigned char *)str;}
  2363. INCSTR(str);
  2364. }
  2365. return match;
  2366. }
  2367. #endif /* NEED_UZMBSRCHR */
  2368. #endif /* _MBCS */
  2369. #ifdef SMALL_MEM
  2370. /*******************************/
  2371. /* Function fLoadFarString() */ /* (and friends...) */
  2372. /*******************************/
  2373. char *fLoadFarString(__GPRO__ const char Far *sz)
  2374. {
  2375. (void)zfstrcpy(G.rgchBigBuffer, sz);
  2376. return G.rgchBigBuffer;
  2377. }
  2378. char *fLoadFarStringSmall(__GPRO__ const char Far *sz)
  2379. {
  2380. (void)zfstrcpy(G.rgchSmallBuffer, sz);
  2381. return G.rgchSmallBuffer;
  2382. }
  2383. char *fLoadFarStringSmall2(__GPRO__ const char Far *sz)
  2384. {
  2385. (void)zfstrcpy(G.rgchSmallBuffer2, sz);
  2386. return G.rgchSmallBuffer2;
  2387. }
  2388. #if (!defined(_MSC_VER) || (_MSC_VER < 600))
  2389. /*************************/
  2390. /* Function zfstrcpy() */ /* portable clone of _fstrcpy() */
  2391. /*************************/
  2392. char Far * Far zfstrcpy(char Far *s1, const char Far *s2)
  2393. {
  2394. char Far *p = s1;
  2395. while ((*s1++ = *s2++) != '\0');
  2396. return p;
  2397. }
  2398. #if (!(defined(SFX) || defined(FUNZIP)))
  2399. /*************************/
  2400. /* Function zfstrcmp() */ /* portable clone of _fstrcmp() */
  2401. /*************************/
  2402. int Far zfstrcmp(const char Far *s1, const char Far *s2)
  2403. {
  2404. int ret;
  2405. while ((ret = (int)(uch)*s1 - (int)(uch)*s2) == 0
  2406. && *s2 != '\0') {
  2407. ++s2; ++s1;
  2408. }
  2409. return ret;
  2410. }
  2411. #endif /* !(SFX || FUNZIP) */
  2412. #endif /* !_MSC_VER || (_MSC_VER < 600) */
  2413. #endif /* SMALL_MEM */