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.

3093 lines
107KB

  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. process.c
  10. This file contains the top-level routines for processing multiple zipfiles.
  11. Contains: process_zipfiles()
  12. free_G_buffers()
  13. do_seekable()
  14. file_size()
  15. rec_find()
  16. find_ecrec64()
  17. find_ecrec()
  18. process_zip_cmmnt()
  19. process_cdir_file_hdr()
  20. get_cdir_ent()
  21. process_local_file_hdr()
  22. getZip64Data()
  23. ef_scan_for_izux()
  24. getRISCOSexfield()
  25. ---------------------------------------------------------------------------*/
  26. #define UNZIP_INTERNAL
  27. #include "unzip.h"
  28. #ifdef WINDLL
  29. # ifdef POCKET_UNZIP
  30. # include "wince/intrface.h"
  31. # else
  32. # include "windll/windll.h"
  33. # endif
  34. #endif
  35. #if defined(DYNALLOC_CRCTAB) || defined(UNICODE_SUPPORT)
  36. # include "crc32.h"
  37. #endif
  38. #include "unzipfx/appDetails.h"
  39. static int do_seekable OF((__GPRO__ int lastchance));
  40. #ifdef DO_SAFECHECK_2GB
  41. # ifdef USE_STRM_INPUT
  42. static zoff_t file_size OF((FILE *file));
  43. # else
  44. static zoff_t file_size OF((int fh));
  45. # endif
  46. #endif /* DO_SAFECHECK_2GB */
  47. static int rec_find OF((__GPRO__ zoff_t, char *, int));
  48. static int find_ecrec64 OF((__GPRO__ zoff_t searchlen));
  49. static int find_ecrec OF((__GPRO__ zoff_t searchlen));
  50. static int process_zip_cmmnt OF((__GPRO));
  51. static int get_cdir_ent OF((__GPRO));
  52. #ifdef IZ_HAVE_UXUIDGID
  53. static int read_ux3_value OF((ZCONST uch *dbuf, unsigned uidgid_sz,
  54. ulg *p_uidgid));
  55. #endif /* IZ_HAVE_UXUIDGID */
  56. static ZCONST char Far CannotAllocateBuffers[] =
  57. "error: cannot allocate unzip buffers\n";
  58. #ifdef SFX
  59. static ZCONST char Far CannotFindMyself[] =
  60. "unzipsfx: cannot find myself! [%s]\n";
  61. # ifdef CHEAP_SFX_AUTORUN
  62. static ZCONST char Far AutorunPrompt[] =
  63. "\nAuto-run command: %s\nExecute this command? [y/n] ";
  64. static ZCONST char Far NotAutoRunning[] =
  65. "Not executing auto-run command.";
  66. # endif
  67. #else /* !SFX */
  68. /* process_zipfiles() strings */
  69. # if (defined(IZ_CHECK_TZ) && defined(USE_EF_UT_TIME))
  70. static ZCONST char Far WarnInvalidTZ[] =
  71. "Warning: TZ environment variable not found, cannot use UTC times!!\n";
  72. # endif
  73. # if !(defined(UNIX) || defined(AMIGA))
  74. static ZCONST char Far CannotFindWildcardMatch[] =
  75. "%s: cannot find any matches for wildcard specification \"%s\".\n";
  76. # endif /* !(UNIX || AMIGA) */
  77. static ZCONST char Far FilesProcessOK[] =
  78. "%d archive%s successfully processed.\n";
  79. static ZCONST char Far ArchiveWarning[] =
  80. "%d archive%s had warnings but no fatal errors.\n";
  81. static ZCONST char Far ArchiveFatalError[] =
  82. "%d archive%s had fatal errors.\n";
  83. static ZCONST char Far FileHadNoZipfileDir[] =
  84. "%d file%s had no zipfile directory.\n";
  85. static ZCONST char Far ZipfileWasDir[] = "1 \"zipfile\" was a directory.\n";
  86. static ZCONST char Far ManyZipfilesWereDir[] =
  87. "%d \"zipfiles\" were directories.\n";
  88. static ZCONST char Far NoZipfileFound[] = "No zipfiles found.\n";
  89. /* do_seekable() strings */
  90. # ifdef UNIX
  91. static ZCONST char Far CannotFindZipfileDirMsg[] =
  92. "%s: cannot find zipfile directory in one of %s or\n\
  93. %s%s.zip, and cannot find %s, period.\n";
  94. static ZCONST char Far CannotFindEitherZipfile[] =
  95. "%s: cannot find or open %s, %s.zip or %s.\n";
  96. # else /* !UNIX */
  97. static ZCONST char Far CannotFindZipfileDirMsg[] =
  98. "%s: cannot find zipfile directory in %s,\n\
  99. %sand cannot find %s, period.\n";
  100. # ifdef VMS
  101. static ZCONST char Far CannotFindEitherZipfile[] =
  102. "%s: cannot find %s (%s).\n";
  103. # else /* !VMS */
  104. static ZCONST char Far CannotFindEitherZipfile[] =
  105. "%s: cannot find either %s or %s.\n";
  106. # endif /* ?VMS */
  107. # endif /* ?UNIX */
  108. extern ZCONST char Far Zipnfo[]; /* in unzip.c */
  109. #ifndef WINDLL
  110. static ZCONST char Far Unzip[] = "unzip";
  111. #else
  112. static ZCONST char Far Unzip[] = "UnZip DLL";
  113. #endif
  114. #ifdef DO_SAFECHECK_2GB
  115. static ZCONST char Far ZipfileTooBig[] =
  116. "Trying to read large file (> 2 GiB) without large file support\n";
  117. #endif /* DO_SAFECHECK_2GB */
  118. static ZCONST char Far MaybeExe[] =
  119. "note: %s may be a plain executable, not an archive\n";
  120. static ZCONST char Far CentDirNotInZipMsg[] = "\n\
  121. [%s]:\n\
  122. Zipfile is disk %lu of a multi-disk archive, and this is not the disk on\n\
  123. which the central zipfile directory begins (disk %lu).\n";
  124. static ZCONST char Far EndCentDirBogus[] =
  125. "\nwarning [%s]: end-of-central-directory record claims this\n\
  126. is disk %lu but that the central directory starts on disk %lu; this is a\n\
  127. contradiction. Attempting to process anyway.\n";
  128. # ifdef NO_MULTIPART
  129. static ZCONST char Far NoMultiDiskArcSupport[] =
  130. "\nerror [%s]: zipfile is part of multi-disk archive\n\
  131. (sorry, not yet supported).\n";
  132. static ZCONST char Far MaybePakBug[] = "warning [%s]:\
  133. zipfile claims to be 2nd disk of a 2-part archive;\n\
  134. attempting to process anyway. If no further errors occur, this archive\n\
  135. was probably created by PAK v2.51 or earlier. This bug was reported to\n\
  136. NoGate in March 1991 and was supposed to have been fixed by mid-1991; as\n\
  137. of mid-1992 it still hadn't been. (If further errors do occur, archive\n\
  138. was probably created by PKZIP 2.04c or later; UnZip does not yet support\n\
  139. multi-part archives.)\n";
  140. # else
  141. static ZCONST char Far MaybePakBug[] = "warning [%s]:\
  142. zipfile claims to be last disk of a multi-part archive;\n\
  143. attempting to process anyway, assuming all parts have been concatenated\n\
  144. together in order. Expect \"errors\" and warnings...true multi-part support\
  145. \n doesn't exist yet (coming soon).\n";
  146. # endif
  147. static ZCONST char Far ExtraBytesAtStart[] =
  148. "warning [%s]: %s extra byte%s at beginning or within zipfile\n\
  149. (attempting to process anyway)\n";
  150. #endif /* ?SFX */
  151. #if ((!defined(WINDLL) && !defined(SFX)) || !defined(NO_ZIPINFO))
  152. static ZCONST char Far LogInitline[] = "Archive: %s\n";
  153. #endif
  154. static ZCONST char Far MissingBytes[] =
  155. "error [%s]: missing %s bytes in zipfile\n\
  156. (attempting to process anyway)\n";
  157. static ZCONST char Far NullCentDirOffset[] =
  158. "error [%s]: NULL central directory offset\n\
  159. (attempting to process anyway)\n";
  160. static ZCONST char Far ZipfileEmpty[] = "warning [%s]: zipfile is empty\n";
  161. static ZCONST char Far CentDirStartNotFound[] =
  162. "error [%s]: start of central directory not found;\n\
  163. zipfile corrupt.\n%s";
  164. static ZCONST char Far Cent64EndSigSearchErr[] =
  165. "fatal error: read failure while seeking for End-of-centdir-64 signature.\n\
  166. This zipfile is corrupt.\n";
  167. static ZCONST char Far Cent64EndSigSearchOff[] =
  168. "error: End-of-centdir-64 signature not where expected (prepended bytes?)\n\
  169. (attempting to process anyway)\n";
  170. #ifndef SFX
  171. static ZCONST char Far CentDirTooLong[] =
  172. "error [%s]: reported length of central directory is\n\
  173. %s bytes too long (Atari STZip zipfile? J.H.Holm ZIPSPLIT 1.1\n\
  174. zipfile?). Compensating...\n";
  175. static ZCONST char Far CentDirEndSigNotFound[] = "\
  176. End-of-central-directory signature not found. Either this file is not\n\
  177. a zipfile, or it constitutes one disk of a multi-part archive. In the\n\
  178. latter case the central directory and zipfile comment will be found on\n\
  179. the last disk(s) of this archive.\n";
  180. #else /* SFX */
  181. static ZCONST char Far CentDirEndSigNotFound[] =
  182. " End-of-central-directory signature not found.\n";
  183. #endif /* ?SFX */
  184. #ifdef TIMESTAMP
  185. static ZCONST char Far ZipTimeStampFailed[] =
  186. "warning: cannot set time for %s\n";
  187. static ZCONST char Far ZipTimeStampSuccess[] =
  188. "Updated time stamp for %s.\n";
  189. #endif
  190. static ZCONST char Far ZipfileCommTrunc1[] =
  191. "\ncaution: zipfile comment truncated\n";
  192. #ifndef NO_ZIPINFO
  193. static ZCONST char Far NoZipfileComment[] =
  194. "There is no zipfile comment.\n";
  195. static ZCONST char Far ZipfileCommentDesc[] =
  196. "The zipfile comment is %u bytes long and contains the following text:\n";
  197. static ZCONST char Far ZipfileCommBegin[] =
  198. "======================== zipfile comment begins\
  199. ==========================\n";
  200. static ZCONST char Far ZipfileCommEnd[] =
  201. "========================= zipfile comment ends\
  202. ===========================\n";
  203. static ZCONST char Far ZipfileCommTrunc2[] =
  204. "\n The zipfile comment is truncated.\n";
  205. #endif /* !NO_ZIPINFO */
  206. #ifdef UNICODE_SUPPORT
  207. static ZCONST char Far UnicodeVersionError[] =
  208. "\nwarning: Unicode Path version > 1\n";
  209. static ZCONST char Far UnicodeMismatchError[] =
  210. "\nwarning: Unicode Path checksum invalid\n";
  211. #endif
  212. /*******************************/
  213. /* Function process_zipfiles() */
  214. /*******************************/
  215. int process_zipfiles(__G) /* return PK-type error code */
  216. __GDEF
  217. {
  218. #ifndef SFX
  219. char *lastzipfn = (char *)NULL;
  220. int NumWinFiles, NumLoseFiles, NumWarnFiles;
  221. int NumMissDirs, NumMissFiles;
  222. #endif
  223. int error=0, error_in_archive=0;
  224. /*---------------------------------------------------------------------------
  225. Start by allocating buffers and (re)constructing the various PK signature
  226. strings.
  227. ---------------------------------------------------------------------------*/
  228. G.inbuf = (uch *)malloc(INBUFSIZ + 4); /* 4 extra for hold[] (below) */
  229. G.outbuf = (uch *)malloc(OUTBUFSIZ + 1); /* 1 extra for string term. */
  230. if ((G.inbuf == (uch *)NULL) || (G.outbuf == (uch *)NULL)) {
  231. Info(slide, 0x401, ((char *)slide,
  232. LoadFarString(CannotAllocateBuffers)));
  233. return(PK_MEM);
  234. }
  235. G.hold = G.inbuf + INBUFSIZ; /* to check for boundary-spanning sigs */
  236. #ifndef VMS /* VMS uses its own buffer scheme for textmode flush(). */
  237. #ifdef SMALL_MEM
  238. G.outbuf2 = G.outbuf+RAWBUFSIZ; /* never changes */
  239. #endif
  240. #endif /* !VMS */
  241. #if 0 /* CRC_32_TAB has been NULLified by CONSTRUCTGLOBALS !!!! */
  242. /* allocate the CRC table later when we know we can read zipfile data */
  243. CRC_32_TAB = NULL;
  244. #endif /* 0 */
  245. /* finish up initialization of magic signature strings */
  246. local_hdr_sig[0] /* = extd_local_sig[0] */ = /* ASCII 'P', */
  247. central_hdr_sig[0] = end_central_sig[0] = /* not EBCDIC */
  248. end_centloc64_sig[0] = end_central64_sig[0] = 0x50;
  249. local_hdr_sig[1] /* = extd_local_sig[1] */ = /* ASCII 'K', */
  250. central_hdr_sig[1] = end_central_sig[1] = /* not EBCDIC */
  251. end_centloc64_sig[1] = end_central64_sig[1] = 0x4B;
  252. /*---------------------------------------------------------------------------
  253. Make sure timezone info is set correctly; localtime() returns GMT on some
  254. OSes (e.g., Solaris 2.x) if this isn't done first. The ifdefs around
  255. tzset() were initially copied from dos_to_unix_time() in fileio.c. They
  256. may still be too strict; any listed OS that supplies tzset(), regardless
  257. of whether the function does anything, should be removed from the ifdefs.
  258. ---------------------------------------------------------------------------*/
  259. #if (defined(WIN32) && defined(USE_EF_UT_TIME))
  260. /* For the Win32 environment, we may have to "prepare" the environment
  261. prior to the tzset() call, to work around tzset() implementation bugs.
  262. */
  263. iz_w32_prepareTZenv();
  264. #endif
  265. #if (defined(IZ_CHECK_TZ) && defined(USE_EF_UT_TIME))
  266. # ifndef VALID_TIMEZONE
  267. # define VALID_TIMEZONE(tmp) \
  268. (((tmp = getenv("TZ")) != NULL) && (*tmp != '\0'))
  269. # endif
  270. {
  271. char *p;
  272. G.tz_is_valid = VALID_TIMEZONE(p);
  273. # ifndef SFX
  274. if (!G.tz_is_valid) {
  275. Info(slide, 0x401, ((char *)slide, LoadFarString(WarnInvalidTZ)));
  276. error_in_archive = error = PK_WARN;
  277. }
  278. # endif /* !SFX */
  279. }
  280. #endif /* IZ_CHECK_TZ && USE_EF_UT_TIME */
  281. /* For systems that do not have tzset() but supply this function using another
  282. name (_tzset() or something similar), an appropiate "#define tzset ..."
  283. should be added to the system specifc configuration section. */
  284. #if (!defined(T20_VMS) && !defined(MACOS) && !defined(RISCOS) && !defined(QDOS))
  285. #if (!defined(BSD) && !defined(MTS) && !defined(CMS_MVS) && !defined(TANDEM))
  286. tzset();
  287. #endif
  288. #endif
  289. /* Initialize UnZip's built-in pseudo hard-coded "ISO <--> OEM" translation,
  290. depending on the detected codepage setup. */
  291. #ifdef NEED_ISO_OEM_INIT
  292. prepare_ISO_OEM_translat(__G);
  293. #endif
  294. /*---------------------------------------------------------------------------
  295. Initialize the internal flag holding the mode of processing "overwrite
  296. existing file" cases. We do not use the calling interface flags directly
  297. because the overwrite mode may be changed by user interaction while
  298. processing archive files. Such a change should not affect the option
  299. settings as passed through the DLL calling interface.
  300. In case of conflicting options, the 'safer' flag uO.overwrite_none takes
  301. precedence.
  302. ---------------------------------------------------------------------------*/
  303. G.overwrite_mode = (uO.overwrite_none ? OVERWRT_NEVER :
  304. (uO.overwrite_all ? OVERWRT_ALWAYS : OVERWRT_QUERY));
  305. /*---------------------------------------------------------------------------
  306. Match (possible) wildcard zipfile specification with existing files and
  307. attempt to process each. If no hits, try again after appending ".zip"
  308. suffix. If still no luck, give up.
  309. ---------------------------------------------------------------------------*/
  310. #ifdef SFX
  311. if ((error = do_seekable(__G__ 0)) == PK_NOZIP) {
  312. #ifdef EXE_EXTENSION
  313. int len=strlen(G.argv0);
  314. /* append .exe if appropriate; also .sfx? */
  315. if ( (G.zipfn = (char *)malloc(len+sizeof(EXE_EXTENSION))) !=
  316. (char *)NULL ) {
  317. strcpy(G.zipfn, G.argv0);
  318. strcpy(G.zipfn+len, EXE_EXTENSION);
  319. error = do_seekable(__G__ 0);
  320. free(G.zipfn);
  321. G.zipfn = G.argv0; /* for "cannot find myself" message only */
  322. }
  323. #endif /* EXE_EXTENSION */
  324. #ifdef WIN32
  325. G.zipfn = G.argv0; /* for "cannot find myself" message only */
  326. #endif
  327. }
  328. if (error) {
  329. if (error == IZ_DIR)
  330. error_in_archive = PK_NOZIP;
  331. else
  332. error_in_archive = error;
  333. if (error == PK_NOZIP)
  334. Info(slide, 1, ((char *)slide, LoadFarString(CannotFindMyself),
  335. G.zipfn));
  336. }
  337. #ifdef CHEAP_SFX_AUTORUN
  338. if (G.autorun_command[0] && !uO.qflag) { /* NO autorun without prompt! */
  339. Info(slide, 0x81, ((char *)slide, LoadFarString(AutorunPrompt),
  340. FnFilter1(G.autorun_command)));
  341. if (fgets(G.answerbuf, 9, stdin) != (char *)NULL
  342. && toupper(*G.answerbuf) == 'Y')
  343. system(G.autorun_command);
  344. else
  345. Info(slide, 1, ((char *)slide, LoadFarString(NotAutoRunning)));
  346. }
  347. #endif /* CHEAP_SFX_AUTORUN */
  348. int sfx_app_ret = sfx_app_autorun_now();
  349. #else /* !SFX */
  350. NumWinFiles = NumLoseFiles = NumWarnFiles = 0;
  351. NumMissDirs = NumMissFiles = 0;
  352. while ((G.zipfn = do_wild(__G__ G.wildzipfn)) != (char *)NULL) {
  353. Trace((stderr, "do_wild( %s ) returns %s\n", G.wildzipfn, G.zipfn));
  354. lastzipfn = G.zipfn;
  355. /* print a blank line between the output of different zipfiles */
  356. if (!uO.qflag && error != PK_NOZIP && error != IZ_DIR
  357. #ifdef TIMESTAMP
  358. && (!uO.T_flag || uO.zipinfo_mode)
  359. #endif
  360. && (NumWinFiles+NumLoseFiles+NumWarnFiles+NumMissFiles) > 0)
  361. (*G.message)((zvoid *)&G, (uch *)"\n", 1L, 0);
  362. if ((error = do_seekable(__G__ 0)) == PK_WARN)
  363. ++NumWarnFiles;
  364. else if (error == IZ_DIR)
  365. ++NumMissDirs;
  366. else if (error == PK_NOZIP)
  367. ++NumMissFiles;
  368. else if (error != PK_OK)
  369. ++NumLoseFiles;
  370. else
  371. ++NumWinFiles;
  372. Trace((stderr, "do_seekable(0) returns %d\n", error));
  373. if (error != IZ_DIR && error > error_in_archive)
  374. error_in_archive = error;
  375. #ifdef WINDLL
  376. if (error == IZ_CTRLC) {
  377. free_G_buffers(__G);
  378. return error;
  379. }
  380. #endif
  381. } /* end while-loop (wildcard zipfiles) */
  382. if ((NumWinFiles + NumWarnFiles + NumLoseFiles) == 0 &&
  383. (NumMissDirs + NumMissFiles) == 1 && lastzipfn != (char *)NULL)
  384. {
  385. #if (!defined(UNIX) && !defined(AMIGA)) /* filenames with wildcard characters */
  386. if (iswild(G.wildzipfn)) {
  387. if (iswild(lastzipfn)) {
  388. NumMissDirs = NumMissFiles = 0;
  389. error_in_archive = PK_COOL;
  390. if (uO.qflag < 3)
  391. Info(slide, 0x401, ((char *)slide,
  392. LoadFarString(CannotFindWildcardMatch),
  393. LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
  394. G.wildzipfn));
  395. }
  396. } else
  397. #endif
  398. {
  399. #ifndef VMS
  400. /* 2004-11-24 SMS.
  401. * VMS has already tried a default file type of ".zip" in
  402. * do_wild(), so adding ZSUFX here only causes confusion by
  403. * corrupting some valid (though nonexistent) file names.
  404. * Complaining below about "fred;4.zip" is unlikely to be
  405. * helpful to the victim.
  406. */
  407. /* 2005-08-14 Chr. Spieler
  408. * Although we already "know" the failure result, we call
  409. * do_seekable() again with the same zipfile name (and the
  410. * lastchance flag set), just to trigger the error report...
  411. */
  412. #if defined(UNIX) || defined(QDOS)
  413. char *p =
  414. #endif
  415. strcpy(lastzipfn + strlen(lastzipfn), ZSUFX);
  416. #endif /* !VMS */
  417. G.zipfn = lastzipfn;
  418. NumMissDirs = NumMissFiles = 0;
  419. error_in_archive = PK_COOL;
  420. #if defined(UNIX) || defined(QDOS)
  421. /* only Unix has case-sensitive filesystems */
  422. /* Well FlexOS (sometimes) also has them, but support is per media */
  423. /* and a pig to code for, so treat as case insensitive for now */
  424. /* we do this under QDOS to check for .zip as well as _zip */
  425. if ((error = do_seekable(__G__ 0)) == PK_NOZIP || error == IZ_DIR) {
  426. if (error == IZ_DIR)
  427. ++NumMissDirs;
  428. strcpy(p, ALT_ZSUFX);
  429. error = do_seekable(__G__ 1);
  430. }
  431. #else
  432. error = do_seekable(__G__ 1);
  433. #endif
  434. Trace((stderr, "do_seekable(1) returns %d\n", error));
  435. switch (error) {
  436. case PK_WARN:
  437. ++NumWarnFiles;
  438. break;
  439. case IZ_DIR:
  440. ++NumMissDirs;
  441. error = PK_NOZIP;
  442. break;
  443. case PK_NOZIP:
  444. /* increment again => bug:
  445. "1 file had no zipfile directory." */
  446. /* ++NumMissFiles */ ;
  447. break;
  448. default:
  449. if (error)
  450. ++NumLoseFiles;
  451. else
  452. ++NumWinFiles;
  453. break;
  454. }
  455. if (error > error_in_archive)
  456. error_in_archive = error;
  457. #ifdef WINDLL
  458. if (error == IZ_CTRLC) {
  459. free_G_buffers(__G);
  460. return error;
  461. }
  462. #endif
  463. }
  464. }
  465. #endif /* ?SFX */
  466. /*---------------------------------------------------------------------------
  467. Print summary of all zipfiles, assuming zipfile spec was a wildcard (no
  468. need for a summary if just one zipfile).
  469. ---------------------------------------------------------------------------*/
  470. #ifndef SFX
  471. if (iswild(G.wildzipfn) && uO.qflag < 3
  472. #ifdef TIMESTAMP
  473. && !(uO.T_flag && !uO.zipinfo_mode && uO.qflag > 1)
  474. #endif
  475. )
  476. {
  477. if ((NumMissFiles + NumLoseFiles + NumWarnFiles > 0 || NumWinFiles != 1)
  478. #ifdef TIMESTAMP
  479. && !(uO.T_flag && !uO.zipinfo_mode && uO.qflag)
  480. #endif
  481. && !(uO.tflag && uO.qflag > 1))
  482. (*G.message)((zvoid *)&G, (uch *)"\n", 1L, 0x401);
  483. if ((NumWinFiles > 1) ||
  484. (NumWinFiles == 1 &&
  485. NumMissDirs + NumMissFiles + NumLoseFiles + NumWarnFiles > 0))
  486. Info(slide, 0x401, ((char *)slide, LoadFarString(FilesProcessOK),
  487. NumWinFiles, (NumWinFiles == 1)? " was" : "s were"));
  488. if (NumWarnFiles > 0)
  489. Info(slide, 0x401, ((char *)slide, LoadFarString(ArchiveWarning),
  490. NumWarnFiles, (NumWarnFiles == 1)? "" : "s"));
  491. if (NumLoseFiles > 0)
  492. Info(slide, 0x401, ((char *)slide, LoadFarString(ArchiveFatalError),
  493. NumLoseFiles, (NumLoseFiles == 1)? "" : "s"));
  494. if (NumMissFiles > 0)
  495. Info(slide, 0x401, ((char *)slide,
  496. LoadFarString(FileHadNoZipfileDir), NumMissFiles,
  497. (NumMissFiles == 1)? "" : "s"));
  498. if (NumMissDirs == 1)
  499. Info(slide, 0x401, ((char *)slide, LoadFarString(ZipfileWasDir)));
  500. else if (NumMissDirs > 0)
  501. Info(slide, 0x401, ((char *)slide,
  502. LoadFarString(ManyZipfilesWereDir), NumMissDirs));
  503. if (NumWinFiles + NumLoseFiles + NumWarnFiles == 0)
  504. Info(slide, 0x401, ((char *)slide, LoadFarString(NoZipfileFound)));
  505. }
  506. #endif /* !SFX */
  507. /* free allocated memory */
  508. free_G_buffers(__G);
  509. return sfx_app_ret;
  510. } /* end function process_zipfiles() */
  511. /*****************************/
  512. /* Function free_G_buffers() */
  513. /*****************************/
  514. void free_G_buffers(__G) /* releases all memory allocated in global vars */
  515. __GDEF
  516. {
  517. #ifndef SFX
  518. unsigned i;
  519. #endif
  520. #ifdef SYSTEM_SPECIFIC_DTOR
  521. SYSTEM_SPECIFIC_DTOR(__G);
  522. #endif
  523. inflate_free(__G);
  524. checkdir(__G__ (char *)NULL, END);
  525. #ifdef DYNALLOC_CRCTAB
  526. if (CRC_32_TAB) {
  527. free_crc_table();
  528. CRC_32_TAB = NULL;
  529. }
  530. #endif
  531. if (G.key != (char *)NULL) {
  532. free(G.key);
  533. G.key = (char *)NULL;
  534. }
  535. if (G.extra_field != (uch *)NULL) {
  536. free(G.extra_field);
  537. G.extra_field = (uch *)NULL;
  538. }
  539. #if (!defined(VMS) && !defined(SMALL_MEM))
  540. /* VMS uses its own buffer scheme for textmode flush() */
  541. if (G.outbuf2) {
  542. free(G.outbuf2); /* malloc'd ONLY if unshrink and -a */
  543. G.outbuf2 = (uch *)NULL;
  544. }
  545. #endif
  546. if (G.outbuf)
  547. free(G.outbuf);
  548. if (G.inbuf)
  549. free(G.inbuf);
  550. G.inbuf = G.outbuf = (uch *)NULL;
  551. #ifdef UNICODE_SUPPORT
  552. if (G.filename_full) {
  553. free(G.filename_full);
  554. G.filename_full = (char *)NULL;
  555. G.fnfull_bufsize = 0;
  556. }
  557. #endif /* UNICODE_SUPPORT */
  558. #ifndef SFX
  559. for (i = 0; i < DIR_BLKSIZ; i++) {
  560. if (G.info[i].cfilname != (char Far *)NULL) {
  561. zffree(G.info[i].cfilname);
  562. G.info[i].cfilname = (char Far *)NULL;
  563. }
  564. }
  565. #endif
  566. #ifdef MALLOC_WORK
  567. if (G.area.Slide) {
  568. free(G.area.Slide);
  569. G.area.Slide = (uch *)NULL;
  570. }
  571. #endif
  572. } /* end function free_G_buffers() */
  573. /**************************/
  574. /* Function do_seekable() */
  575. /**************************/
  576. static int do_seekable(__G__ lastchance) /* return PK-type error code */
  577. __GDEF
  578. int lastchance;
  579. {
  580. #ifndef SFX
  581. /* static int no_ecrec = FALSE; SKM: moved to globals.h */
  582. int maybe_exe=FALSE;
  583. int too_weird_to_continue=FALSE;
  584. #ifdef TIMESTAMP
  585. time_t uxstamp;
  586. ulg nmember = 0L;
  587. #endif
  588. #endif
  589. int error=0, error_in_archive;
  590. /*---------------------------------------------------------------------------
  591. Open the zipfile for reading in BINARY mode to prevent CR/LF translation,
  592. which would corrupt the bit streams.
  593. ---------------------------------------------------------------------------*/
  594. if (SSTAT(G.zipfn, &G.statbuf) ||
  595. #ifdef THEOS
  596. (error = S_ISLIB(G.statbuf.st_mode)) != 0 ||
  597. #endif
  598. (error = S_ISDIR(G.statbuf.st_mode)) != 0)
  599. {
  600. #ifndef SFX
  601. if (lastchance && (uO.qflag < 3)) {
  602. #if defined(UNIX) || defined(QDOS)
  603. if (G.no_ecrec)
  604. Info(slide, 1, ((char *)slide,
  605. LoadFarString(CannotFindZipfileDirMsg),
  606. LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
  607. G.wildzipfn, uO.zipinfo_mode? " " : "", G.wildzipfn,
  608. G.zipfn));
  609. else
  610. Info(slide, 1, ((char *)slide,
  611. LoadFarString(CannotFindEitherZipfile),
  612. LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
  613. G.wildzipfn, G.wildzipfn, G.zipfn));
  614. #else /* !(UNIX || QDOS) */
  615. if (G.no_ecrec)
  616. Info(slide, 0x401, ((char *)slide,
  617. LoadFarString(CannotFindZipfileDirMsg),
  618. LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
  619. G.wildzipfn, uO.zipinfo_mode? " " : "", G.zipfn));
  620. else
  621. #ifdef VMS
  622. Info(slide, 0x401, ((char *)slide,
  623. LoadFarString(CannotFindEitherZipfile),
  624. LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
  625. G.wildzipfn,
  626. (*G.zipfn ? G.zipfn : vms_msg_text())));
  627. #else /* !VMS */
  628. Info(slide, 0x401, ((char *)slide,
  629. LoadFarString(CannotFindEitherZipfile),
  630. LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
  631. G.wildzipfn, G.zipfn));
  632. #endif /* ?VMS */
  633. #endif /* ?(UNIX || QDOS) */
  634. }
  635. #endif /* !SFX */
  636. return error? IZ_DIR : PK_NOZIP;
  637. }
  638. G.ziplen = G.statbuf.st_size;
  639. #ifndef SFX
  640. #if defined(UNIX) || defined(DOS_OS2_W32) || defined(THEOS)
  641. if (G.statbuf.st_mode & S_IEXEC) /* no extension on Unix exes: might */
  642. maybe_exe = TRUE; /* find unzip, not unzip.zip; etc. */
  643. #endif
  644. #endif /* !SFX */
  645. #ifdef VMS
  646. if (check_format(__G)) /* check for variable-length format */
  647. return PK_ERR;
  648. #endif
  649. if (open_input_file(__G)) /* this should never happen, given */
  650. return PK_NOZIP; /* the stat() test above, but... */
  651. #ifdef DO_SAFECHECK_2GB
  652. /* Need more care: Do not trust the size returned by stat() but
  653. determine it by reading beyond the end of the file. */
  654. G.ziplen = file_size(G.zipfd);
  655. if (G.ziplen == EOF) {
  656. Info(slide, 0x401, ((char *)slide, LoadFarString(ZipfileTooBig)));
  657. /*
  658. printf(
  659. " We need a better error message for: 64-bit file, 32-bit program.\n");
  660. */
  661. CLOSE_INFILE();
  662. return IZ_ERRBF;
  663. }
  664. #endif /* DO_SAFECHECK_2GB */
  665. /*---------------------------------------------------------------------------
  666. Find and process the end-of-central-directory header. UnZip need only
  667. check last 65557 bytes of zipfile: comment may be up to 65535, end-of-
  668. central-directory record is 18 bytes, and signature itself is 4 bytes;
  669. add some to allow for appended garbage. Since ZipInfo is often used as
  670. a debugging tool, search the whole zipfile if zipinfo_mode is true.
  671. ---------------------------------------------------------------------------*/
  672. G.cur_zipfile_bufstart = 0;
  673. G.inptr = G.inbuf;
  674. #if ((!defined(WINDLL) && !defined(SFX)) || !defined(NO_ZIPINFO))
  675. # if (!defined(WINDLL) && !defined(SFX))
  676. if ( (!uO.zipinfo_mode && !uO.qflag
  677. # ifdef TIMESTAMP
  678. && !uO.T_flag
  679. # endif
  680. )
  681. # ifndef NO_ZIPINFO
  682. || (uO.zipinfo_mode && uO.hflag)
  683. # endif
  684. )
  685. # else /* not (!WINDLL && !SFX) ==> !NO_ZIPINFO !! */
  686. if (uO.zipinfo_mode && uO.hflag)
  687. # endif /* if..else..: (!WINDLL && !SFX) */
  688. # ifdef WIN32 /* Win32 console may require codepage conversion for G.zipfn */
  689. Info(slide, 0, ((char *)slide, LoadFarString(LogInitline),
  690. FnFilter1(G.zipfn)));
  691. # else
  692. Info(slide, 0, ((char *)slide, LoadFarString(LogInitline), G.zipfn));
  693. # endif
  694. #endif /* (!WINDLL && !SFX) || !NO_ZIPINFO */
  695. if ( (error_in_archive = find_ecrec(__G__
  696. #ifndef NO_ZIPINFO
  697. uO.zipinfo_mode ? G.ziplen :
  698. #endif
  699. MIN(G.ziplen, 66000L)))
  700. > PK_WARN )
  701. {
  702. CLOSE_INFILE();
  703. #ifdef SFX
  704. ++lastchance; /* avoid picky compiler warnings */
  705. return error_in_archive;
  706. #else
  707. if (maybe_exe)
  708. Info(slide, 0x401, ((char *)slide, LoadFarString(MaybeExe),
  709. G.zipfn));
  710. if (lastchance)
  711. return error_in_archive;
  712. else {
  713. G.no_ecrec = TRUE; /* assume we found wrong file: e.g., */
  714. return PK_NOZIP; /* unzip instead of unzip.zip */
  715. }
  716. #endif /* ?SFX */
  717. }
  718. if ((uO.zflag > 0) && !uO.zipinfo_mode) { /* unzip: zflag = comment ONLY */
  719. CLOSE_INFILE();
  720. return error_in_archive;
  721. }
  722. /*---------------------------------------------------------------------------
  723. Test the end-of-central-directory info for incompatibilities (multi-disk
  724. archives) or inconsistencies (missing or extra bytes in zipfile).
  725. ---------------------------------------------------------------------------*/
  726. #ifdef NO_MULTIPART
  727. error = !uO.zipinfo_mode && (G.ecrec.number_this_disk == 1) &&
  728. (G.ecrec.num_disk_start_cdir == 1);
  729. #else
  730. error = !uO.zipinfo_mode && (G.ecrec.number_this_disk != 0);
  731. #endif
  732. #ifndef SFX
  733. if (uO.zipinfo_mode &&
  734. G.ecrec.number_this_disk != G.ecrec.num_disk_start_cdir)
  735. {
  736. if (G.ecrec.number_this_disk > G.ecrec.num_disk_start_cdir) {
  737. Info(slide, 0x401, ((char *)slide,
  738. LoadFarString(CentDirNotInZipMsg), G.zipfn,
  739. (ulg)G.ecrec.number_this_disk,
  740. (ulg)G.ecrec.num_disk_start_cdir));
  741. error_in_archive = PK_FIND;
  742. too_weird_to_continue = TRUE;
  743. } else {
  744. Info(slide, 0x401, ((char *)slide,
  745. LoadFarString(EndCentDirBogus), G.zipfn,
  746. (ulg)G.ecrec.number_this_disk,
  747. (ulg)G.ecrec.num_disk_start_cdir));
  748. error_in_archive = PK_WARN;
  749. }
  750. #ifdef NO_MULTIPART /* concatenation of multiple parts works in some cases */
  751. } else if (!uO.zipinfo_mode && !error && G.ecrec.number_this_disk != 0) {
  752. Info(slide, 0x401, ((char *)slide, LoadFarString(NoMultiDiskArcSupport),
  753. G.zipfn));
  754. error_in_archive = PK_FIND;
  755. too_weird_to_continue = TRUE;
  756. #endif
  757. }
  758. if (!too_weird_to_continue) { /* (relatively) normal zipfile: go for it */
  759. if (error) {
  760. Info(slide, 0x401, ((char *)slide, LoadFarString(MaybePakBug),
  761. G.zipfn));
  762. error_in_archive = PK_WARN;
  763. }
  764. #endif /* !SFX */
  765. if ((G.extra_bytes = G.real_ecrec_offset-G.expect_ecrec_offset) <
  766. (zoff_t)0)
  767. {
  768. Info(slide, 0x401, ((char *)slide, LoadFarString(MissingBytes),
  769. G.zipfn, FmZofft((-G.extra_bytes), NULL, NULL)));
  770. error_in_archive = PK_ERR;
  771. } else if (G.extra_bytes > 0) {
  772. if ((G.ecrec.offset_start_central_directory == 0) &&
  773. (G.ecrec.size_central_directory != 0)) /* zip 1.5 -go bug */
  774. {
  775. Info(slide, 0x401, ((char *)slide,
  776. LoadFarString(NullCentDirOffset), G.zipfn));
  777. G.ecrec.offset_start_central_directory = G.extra_bytes;
  778. G.extra_bytes = 0;
  779. error_in_archive = PK_ERR;
  780. }
  781. #ifndef SFX
  782. else {
  783. Info(slide, 0x401, ((char *)slide,
  784. LoadFarString(ExtraBytesAtStart), G.zipfn,
  785. FmZofft(G.extra_bytes, NULL, NULL),
  786. (G.extra_bytes == 1)? "":"s"));
  787. error_in_archive = PK_WARN;
  788. }
  789. #endif /* !SFX */
  790. }
  791. /*-----------------------------------------------------------------------
  792. Check for empty zipfile and exit now if so.
  793. -----------------------------------------------------------------------*/
  794. if (G.expect_ecrec_offset==0L && G.ecrec.size_central_directory==0) {
  795. if (uO.zipinfo_mode)
  796. Info(slide, 0, ((char *)slide, "%sEmpty zipfile.\n",
  797. uO.lflag>9? "\n " : ""));
  798. else
  799. Info(slide, 0x401, ((char *)slide, LoadFarString(ZipfileEmpty),
  800. G.zipfn));
  801. CLOSE_INFILE();
  802. return (error_in_archive > PK_WARN)? error_in_archive : PK_WARN;
  803. }
  804. /*-----------------------------------------------------------------------
  805. Compensate for missing or extra bytes, and seek to where the start
  806. of central directory should be. If header not found, uncompensate
  807. and try again (necessary for at least some Atari archives created
  808. with STZip, as well as archives created by J.H. Holm's ZIPSPLIT 1.1).
  809. -----------------------------------------------------------------------*/
  810. error = seek_zipf(__G__ G.ecrec.offset_start_central_directory);
  811. if (error == PK_BADERR) {
  812. CLOSE_INFILE();
  813. return PK_BADERR;
  814. }
  815. #ifdef OLD_SEEK_TEST
  816. if (error != PK_OK || readbuf(__G__ G.sig, 4) == 0) {
  817. CLOSE_INFILE();
  818. return PK_ERR; /* file may be locked, or possibly disk error(?) */
  819. }
  820. if (memcmp(G.sig, central_hdr_sig, 4))
  821. #else
  822. if ((error != PK_OK) || (readbuf(__G__ G.sig, 4) == 0) ||
  823. memcmp(G.sig, central_hdr_sig, 4))
  824. #endif
  825. {
  826. #ifndef SFX
  827. zoff_t tmp = G.extra_bytes;
  828. #endif
  829. G.extra_bytes = 0;
  830. error = seek_zipf(__G__ G.ecrec.offset_start_central_directory);
  831. if ((error != PK_OK) || (readbuf(__G__ G.sig, 4) == 0) ||
  832. memcmp(G.sig, central_hdr_sig, 4))
  833. {
  834. if (error != PK_BADERR)
  835. Info(slide, 0x401, ((char *)slide,
  836. LoadFarString(CentDirStartNotFound), G.zipfn,
  837. LoadFarStringSmall(ReportMsg)));
  838. CLOSE_INFILE();
  839. return (error != PK_OK ? error : PK_BADERR);
  840. }
  841. #ifndef SFX
  842. Info(slide, 0x401, ((char *)slide, LoadFarString(CentDirTooLong),
  843. G.zipfn, FmZofft((-tmp), NULL, NULL)));
  844. #endif
  845. error_in_archive = PK_ERR;
  846. }
  847. /*-----------------------------------------------------------------------
  848. Seek to the start of the central directory one last time, since we
  849. have just read the first entry's signature bytes; then list, extract
  850. or test member files as instructed, and close the zipfile.
  851. -----------------------------------------------------------------------*/
  852. error = seek_zipf(__G__ G.ecrec.offset_start_central_directory);
  853. if (error != PK_OK) {
  854. CLOSE_INFILE();
  855. return error;
  856. }
  857. Trace((stderr, "about to extract/list files (error = %d)\n",
  858. error_in_archive));
  859. #ifdef DLL
  860. /* G.fValidate is used only to look at an archive to see if
  861. it appears to be a valid archive. There is no interest
  862. in what the archive contains, nor in validating that the
  863. entries in the archive are in good condition. This is
  864. currently used only in the Windows DLLs for purposes of
  865. checking archives within an archive to determine whether
  866. or not to display the inner archives.
  867. */
  868. if (!G.fValidate)
  869. #endif
  870. {
  871. #ifndef NO_ZIPINFO
  872. if (uO.zipinfo_mode)
  873. error = zipinfo(__G); /* ZIPINFO 'EM */
  874. else
  875. #endif
  876. #ifndef SFX
  877. #ifdef TIMESTAMP
  878. if (uO.T_flag)
  879. error = get_time_stamp(__G__ &uxstamp, &nmember);
  880. else
  881. #endif
  882. if (uO.vflag && !uO.tflag && !uO.cflag)
  883. error = list_files(__G); /* LIST 'EM */
  884. else
  885. #endif /* !SFX */
  886. error = extract_or_test_files(__G); /* EXTRACT OR TEST 'EM */
  887. Trace((stderr, "done with extract/list files (error = %d)\n",
  888. error));
  889. }
  890. if (error > error_in_archive) /* don't overwrite stronger error */
  891. error_in_archive = error; /* with (for example) a warning */
  892. #ifndef SFX
  893. } /* end if (!too_weird_to_continue) */
  894. #endif
  895. CLOSE_INFILE();
  896. #ifdef TIMESTAMP
  897. if (uO.T_flag && !uO.zipinfo_mode && (nmember > 0L)) {
  898. # ifdef WIN32
  899. if (stamp_file(__G__ G.zipfn, uxstamp)) { /* TIME-STAMP 'EM */
  900. # else
  901. if (stamp_file(G.zipfn, uxstamp)) { /* TIME-STAMP 'EM */
  902. # endif
  903. if (uO.qflag < 3)
  904. Info(slide, 0x201, ((char *)slide,
  905. LoadFarString(ZipTimeStampFailed), G.zipfn));
  906. if (error_in_archive < PK_WARN)
  907. error_in_archive = PK_WARN;
  908. } else {
  909. if (!uO.qflag)
  910. Info(slide, 0, ((char *)slide,
  911. LoadFarString(ZipTimeStampSuccess), G.zipfn));
  912. }
  913. }
  914. #endif
  915. return error_in_archive;
  916. } /* end function do_seekable() */
  917. #ifdef DO_SAFECHECK_2GB
  918. /************************/
  919. /* Function file_size() */
  920. /************************/
  921. /* File size determination which does not mislead for large files in a
  922. small-file program. Probably should be somewhere else.
  923. The file has to be opened previously
  924. */
  925. #ifdef USE_STRM_INPUT
  926. static zoff_t file_size(file)
  927. FILE *file;
  928. {
  929. int sts;
  930. size_t siz;
  931. #else /* !USE_STRM_INPUT */
  932. static zoff_t file_size(fh)
  933. int fh;
  934. {
  935. int siz;
  936. #endif /* ?USE_STRM_INPUT */
  937. zoff_t ofs;
  938. char waste[4];
  939. #ifdef USE_STRM_INPUT
  940. /* Seek to actual EOF. */
  941. sts = zfseeko(file, 0, SEEK_END);
  942. if (sts != 0) {
  943. /* fseeko() failed. (Unlikely.) */
  944. ofs = EOF;
  945. } else {
  946. /* Get apparent offset at EOF. */
  947. ofs = zftello(file);
  948. if (ofs < 0) {
  949. /* Offset negative (overflow). File too big. */
  950. ofs = EOF;
  951. } else {
  952. /* Seek to apparent EOF offset.
  953. Won't be at actual EOF if offset was truncated.
  954. */
  955. sts = zfseeko(file, ofs, SEEK_SET);
  956. if (sts != 0) {
  957. /* fseeko() failed. (Unlikely.) */
  958. ofs = EOF;
  959. } else {
  960. /* Read a byte at apparent EOF. Should set EOF flag. */
  961. siz = fread(waste, 1, 1, file);
  962. if (feof(file) == 0) {
  963. /* Not at EOF, but should be. File too big. */
  964. ofs = EOF;
  965. }
  966. }
  967. }
  968. }
  969. #else /* !USE_STRM_INPUT */
  970. /* Seek to actual EOF. */
  971. ofs = zlseek(fh, 0, SEEK_END);
  972. if (ofs == (zoff_t) -1) {
  973. /* zlseek() failed. (Unlikely.) */
  974. ofs = EOF;
  975. } else if (ofs < 0) {
  976. /* Offset negative (overflow). File too big. */
  977. ofs = EOF;
  978. } else {
  979. /* Seek to apparent EOF offset.
  980. Won't be at actual EOF if offset was truncated.
  981. */
  982. ofs = zlseek(fh, ofs, SEEK_SET);
  983. if (ofs == (zoff_t) -1) {
  984. /* zlseek() failed. (Unlikely.) */
  985. ofs = EOF;
  986. } else {
  987. /* Read a byte at apparent EOF. Should set EOF flag. */
  988. siz = read(fh, waste, 1);
  989. if (siz != 0) {
  990. /* Not at EOF, but should be. File too big. */
  991. ofs = EOF;
  992. }
  993. }
  994. }
  995. #endif /* ?USE_STRM_INPUT */
  996. return ofs;
  997. } /* end function file_size() */
  998. #endif /* DO_SAFECHECK_2GB */
  999. /***********************/
  1000. /* Function rec_find() */
  1001. /***********************/
  1002. static int rec_find(__G__ searchlen, signature, rec_size)
  1003. /* return 0 when rec found, 1 when not found, 2 in case of read error */
  1004. __GDEF
  1005. zoff_t searchlen;
  1006. char* signature;
  1007. int rec_size;
  1008. {
  1009. int i, numblks, found=FALSE;
  1010. zoff_t tail_len;
  1011. /*---------------------------------------------------------------------------
  1012. Zipfile is longer than INBUFSIZ: may need to loop. Start with short
  1013. block at end of zipfile (if not TOO short).
  1014. ---------------------------------------------------------------------------*/
  1015. if ((tail_len = G.ziplen % INBUFSIZ) > rec_size) {
  1016. #ifdef USE_STRM_INPUT
  1017. zfseeko(G.zipfd, G.ziplen-tail_len, SEEK_SET);
  1018. G.cur_zipfile_bufstart = zftello(G.zipfd);
  1019. #else /* !USE_STRM_INPUT */
  1020. G.cur_zipfile_bufstart = zlseek(G.zipfd, G.ziplen-tail_len, SEEK_SET);
  1021. #endif /* ?USE_STRM_INPUT */
  1022. if ((G.incnt = read(G.zipfd, (char *)G.inbuf,
  1023. (unsigned int)tail_len)) != (int)tail_len)
  1024. return 2; /* it's expedient... */
  1025. /* 'P' must be at least (rec_size+4) bytes from end of zipfile */
  1026. for (G.inptr = G.inbuf+(int)tail_len-(rec_size+4);
  1027. G.inptr >= G.inbuf;
  1028. --G.inptr) {
  1029. if ( (*G.inptr == (uch)0x50) && /* ASCII 'P' */
  1030. !memcmp((char *)G.inptr, signature, 4) ) {
  1031. G.incnt -= (int)(G.inptr - G.inbuf);
  1032. found = TRUE;
  1033. break;
  1034. }
  1035. }
  1036. /* sig may span block boundary: */
  1037. memcpy((char *)G.hold, (char *)G.inbuf, 3);
  1038. } else
  1039. G.cur_zipfile_bufstart = G.ziplen - tail_len;
  1040. /*-----------------------------------------------------------------------
  1041. Loop through blocks of zipfile data, starting at the end and going
  1042. toward the beginning. In general, need not check whole zipfile for
  1043. signature, but may want to do so if testing.
  1044. -----------------------------------------------------------------------*/
  1045. numblks = (int)((searchlen - tail_len + (INBUFSIZ-1)) / INBUFSIZ);
  1046. /* ==amount= ==done== ==rounding== =blksiz= */
  1047. for (i = 1; !found && (i <= numblks); ++i) {
  1048. G.cur_zipfile_bufstart -= INBUFSIZ;
  1049. #ifdef USE_STRM_INPUT
  1050. zfseeko(G.zipfd, G.cur_zipfile_bufstart, SEEK_SET);
  1051. #else /* !USE_STRM_INPUT */
  1052. zlseek(G.zipfd, G.cur_zipfile_bufstart, SEEK_SET);
  1053. #endif /* ?USE_STRM_INPUT */
  1054. if ((G.incnt = read(G.zipfd,(char *)G.inbuf,INBUFSIZ))
  1055. != INBUFSIZ)
  1056. return 2; /* read error is fatal failure */
  1057. for (G.inptr = G.inbuf+INBUFSIZ-1; G.inptr >= G.inbuf; --G.inptr)
  1058. if ( (*G.inptr == (uch)0x50) && /* ASCII 'P' */
  1059. !memcmp((char *)G.inptr, signature, 4) ) {
  1060. G.incnt -= (int)(G.inptr - G.inbuf);
  1061. found = TRUE;
  1062. break;
  1063. }
  1064. /* sig may span block boundary: */
  1065. memcpy((char *)G.hold, (char *)G.inbuf, 3);
  1066. }
  1067. return (found ? 0 : 1);
  1068. } /* end function rec_find() */
  1069. #if 0
  1070. /********************************/
  1071. /* Function check_ecrec_zip64() */
  1072. /********************************/
  1073. static int check_ecrec_zip64(__G)
  1074. __GDEF
  1075. {
  1076. return G.ecrec.offset_start_central_directory == 0xFFFFFFFFL
  1077. || G.ecrec.size_central_directory == 0xFFFFFFFFL
  1078. || G.ecrec.total_entries_central_dir == 0xFFFF
  1079. || G.ecrec.num_entries_centrl_dir_ths_disk == 0xFFFF
  1080. || G.ecrec.num_disk_start_cdir == 0xFFFF
  1081. || G.ecrec.number_this_disk == 0xFFFF;
  1082. } /* end function check_ecrec_zip64() */
  1083. #endif /* never */
  1084. /***************************/
  1085. /* Function find_ecrec64() */
  1086. /***************************/
  1087. static int find_ecrec64(__G__ searchlen) /* return PK-class error */
  1088. __GDEF
  1089. zoff_t searchlen;
  1090. {
  1091. ec_byte_rec64 byterec; /* buf for ecrec64 */
  1092. ec_byte_loc64 byterecL; /* buf for ecrec64 locator */
  1093. zoff_t ecloc64_start_offset; /* start offset of ecrec64 locator */
  1094. zusz_t ecrec64_start_offset; /* start offset of ecrec64 */
  1095. zuvl_t ecrec64_start_disk; /* start disk of ecrec64 */
  1096. zuvl_t ecloc64_total_disks; /* total disks */
  1097. zuvl_t ecrec64_disk_cdstart; /* disk number of central dir start */
  1098. zucn_t ecrec64_this_entries; /* entries on disk with ecrec64 */
  1099. zucn_t ecrec64_tot_entries; /* total number of entries */
  1100. zusz_t ecrec64_cdirsize; /* length of central dir */
  1101. zusz_t ecrec64_offs_cdstart; /* offset of central dir start */
  1102. /* First, find the ecrec64 locator. By definition, this must be before
  1103. ecrec with nothing in between. We back up the size of the ecrec64
  1104. locator and check. */
  1105. ecloc64_start_offset = G.real_ecrec_offset - (ECLOC64_SIZE+4);
  1106. if (ecloc64_start_offset < 0)
  1107. /* Seeking would go past beginning, so probably empty archive */
  1108. return PK_COOL;
  1109. #ifdef USE_STRM_INPUT
  1110. zfseeko(G.zipfd, ecloc64_start_offset, SEEK_SET);
  1111. G.cur_zipfile_bufstart = zftello(G.zipfd);
  1112. #else /* !USE_STRM_INPUT */
  1113. G.cur_zipfile_bufstart = zlseek(G.zipfd, ecloc64_start_offset, SEEK_SET);
  1114. #endif /* ?USE_STRM_INPUT */
  1115. if ((G.incnt = read(G.zipfd, (char *)byterecL, ECLOC64_SIZE+4))
  1116. != (ECLOC64_SIZE+4)) {
  1117. if (uO.qflag || uO.zipinfo_mode)
  1118. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1119. Info(slide, 0x401, ((char *)slide,
  1120. LoadFarString(Cent64EndSigSearchErr)));
  1121. return PK_ERR;
  1122. }
  1123. if (memcmp((char *)byterecL, end_centloc64_sig, 4) ) {
  1124. /* not found */
  1125. return PK_COOL;
  1126. }
  1127. /* Read the locator. */
  1128. ecrec64_start_disk = (zuvl_t)makelong(&byterecL[NUM_DISK_START_EOCDR64]);
  1129. ecrec64_start_offset = (zusz_t)makeint64(&byterecL[OFFSET_START_EOCDR64]);
  1130. ecloc64_total_disks = (zuvl_t)makelong(&byterecL[NUM_THIS_DISK_LOC64]);
  1131. /* Check for consistency */
  1132. #ifdef TEST
  1133. fprintf(stdout,"\nnumber of disks (ECR) %u, (ECLOC64) %lu\n",
  1134. G.ecrec.number_this_disk, ecloc64_total_disks); fflush(stdout);
  1135. #endif
  1136. if ((G.ecrec.number_this_disk != 0xFFFF) &&
  1137. (G.ecrec.number_this_disk != ecloc64_total_disks - 1)) {
  1138. /* Note: For some unknown reason, the developers at PKWARE decided to
  1139. store the "zip64 total disks" value as a counter starting from 1,
  1140. whereas all other "split/span volume" related fields use 0-based
  1141. volume numbers. Sigh... */
  1142. /* When the total number of disks as found in the traditional ecrec
  1143. is not 0xFFFF, the disk numbers in ecrec and ecloc64 must match.
  1144. When this is not the case, the found ecrec64 locator cannot be valid.
  1145. -> This is not a Zip64 archive.
  1146. */
  1147. Trace((stderr,
  1148. "\ninvalid ECLOC64, differing disk# (ECR %u, ECL64 %lu)\n",
  1149. G.ecrec.number_this_disk, ecloc64_total_disks - 1));
  1150. return PK_COOL;
  1151. }
  1152. /* If found locator, look for ecrec64 where the locator says it is. */
  1153. /* For now assume that ecrec64 is on the same disk as ecloc64 and ecrec,
  1154. which is usually the case and is how Zip writes it. To do this right,
  1155. however, we should allow the ecrec64 to be on another disk since
  1156. the AppNote allows it and the ecrec64 can be large, especially if
  1157. Version 2 is used (AppNote uses 8 bytes for the size of this record). */
  1158. /* FIX BELOW IF ADD SUPPORT FOR MULTIPLE DISKS */
  1159. if (ecrec64_start_offset > (zusz_t)ecloc64_start_offset) {
  1160. /* ecrec64 has to be before ecrec64 locator */
  1161. if (uO.qflag || uO.zipinfo_mode)
  1162. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1163. Info(slide, 0x401, ((char *)slide,
  1164. LoadFarString(Cent64EndSigSearchErr)));
  1165. return PK_ERR;
  1166. }
  1167. #ifdef USE_STRM_INPUT
  1168. zfseeko(G.zipfd, ecrec64_start_offset, SEEK_SET);
  1169. G.cur_zipfile_bufstart = zftello(G.zipfd);
  1170. #else /* !USE_STRM_INPUT */
  1171. G.cur_zipfile_bufstart = zlseek(G.zipfd, ecrec64_start_offset, SEEK_SET);
  1172. #endif /* ?USE_STRM_INPUT */
  1173. if ((G.incnt = read(G.zipfd, (char *)byterec, ECREC64_SIZE+4))
  1174. != (ECREC64_SIZE+4)) {
  1175. if (uO.qflag || uO.zipinfo_mode)
  1176. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1177. Info(slide, 0x401, ((char *)slide,
  1178. LoadFarString(Cent64EndSigSearchErr)));
  1179. return PK_ERR;
  1180. }
  1181. if (memcmp((char *)byterec, end_central64_sig, 4) ) {
  1182. /* Zip64 EOCD Record not found */
  1183. /* Since we already have seen the Zip64 EOCD Locator, it's
  1184. possible we got here because there are bytes prepended
  1185. to the archive, like the sfx prefix. */
  1186. /* Make a guess as to where the Zip64 EOCD Record might be */
  1187. ecrec64_start_offset = ecloc64_start_offset - ECREC64_SIZE - 4;
  1188. #ifdef USE_STRM_INPUT
  1189. zfseeko(G.zipfd, ecrec64_start_offset, SEEK_SET);
  1190. G.cur_zipfile_bufstart = zftello(G.zipfd);
  1191. #else /* !USE_STRM_INPUT */
  1192. G.cur_zipfile_bufstart = zlseek(G.zipfd, ecrec64_start_offset, SEEK_SET);
  1193. #endif /* ?USE_STRM_INPUT */
  1194. if ((G.incnt = read(G.zipfd, (char *)byterec, ECREC64_SIZE+4))
  1195. != (ECREC64_SIZE+4)) {
  1196. if (uO.qflag || uO.zipinfo_mode)
  1197. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1198. Info(slide, 0x401, ((char *)slide,
  1199. LoadFarString(Cent64EndSigSearchErr)));
  1200. return PK_ERR;
  1201. }
  1202. if (memcmp((char *)byterec, end_central64_sig, 4) ) {
  1203. /* Zip64 EOCD Record not found */
  1204. /* Probably something not so easy to handle so exit */
  1205. if (uO.qflag || uO.zipinfo_mode)
  1206. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1207. Info(slide, 0x401, ((char *)slide,
  1208. LoadFarString(Cent64EndSigSearchErr)));
  1209. return PK_ERR;
  1210. }
  1211. if (uO.qflag || uO.zipinfo_mode)
  1212. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1213. Info(slide, 0x401, ((char *)slide,
  1214. LoadFarString(Cent64EndSigSearchOff)));
  1215. }
  1216. /* Check consistency of found ecrec64 with ecloc64 (and ecrec): */
  1217. if ( (zuvl_t)makelong(&byterec[NUMBER_THIS_DSK_REC64])
  1218. != ecrec64_start_disk )
  1219. /* found ecrec64 does not match ecloc64 info -> no Zip64 archive */
  1220. return PK_COOL;
  1221. /* Read all relevant ecrec64 fields and compare them to the corresponding
  1222. ecrec fields unless those are set to "all-ones".
  1223. */
  1224. ecrec64_disk_cdstart =
  1225. (zuvl_t)makelong(&byterec[NUM_DISK_START_CEN_DIR64]);
  1226. if ( (G.ecrec.num_disk_start_cdir != 0xFFFF) &&
  1227. (G.ecrec.num_disk_start_cdir != ecrec64_disk_cdstart) )
  1228. return PK_COOL;
  1229. ecrec64_this_entries
  1230. = makeint64(&byterec[NUM_ENTRIES_CEN_DIR_THS_DISK64]);
  1231. if ( (G.ecrec.num_entries_centrl_dir_ths_disk != 0xFFFF) &&
  1232. (G.ecrec.num_entries_centrl_dir_ths_disk != ecrec64_this_entries) )
  1233. return PK_COOL;
  1234. ecrec64_tot_entries
  1235. = makeint64(&byterec[TOTAL_ENTRIES_CENTRAL_DIR64]);
  1236. if ( (G.ecrec.total_entries_central_dir != 0xFFFF) &&
  1237. (G.ecrec.total_entries_central_dir != ecrec64_tot_entries) )
  1238. return PK_COOL;
  1239. ecrec64_cdirsize
  1240. = makeint64(&byterec[SIZE_CENTRAL_DIRECTORY64]);
  1241. if ( (G.ecrec.size_central_directory != 0xFFFFFFFFL) &&
  1242. (G.ecrec.size_central_directory != ecrec64_cdirsize) )
  1243. return PK_COOL;
  1244. ecrec64_offs_cdstart
  1245. = makeint64(&byterec[OFFSET_START_CENTRAL_DIRECT64]);
  1246. if ( (G.ecrec.offset_start_central_directory != 0xFFFFFFFFL) &&
  1247. (G.ecrec.offset_start_central_directory != ecrec64_offs_cdstart) )
  1248. return PK_COOL;
  1249. /* Now, we are (almost) sure that we have a Zip64 archive. */
  1250. G.ecrec.have_ecr64 = 1;
  1251. /* Update the "end-of-central-dir offset" for later checks. */
  1252. G.real_ecrec_offset = ecrec64_start_offset;
  1253. /* Update all ecdir_rec data that are flagged to be invalid
  1254. in Zip64 mode. Set the ecrec64-mandatory flag when such a
  1255. case is found. */
  1256. if (G.ecrec.number_this_disk == 0xFFFF) {
  1257. G.ecrec.number_this_disk = ecrec64_start_disk;
  1258. if (ecrec64_start_disk != 0xFFFF) G.ecrec.is_zip64_archive = TRUE;
  1259. }
  1260. if (G.ecrec.num_disk_start_cdir == 0xFFFF) {
  1261. G.ecrec.num_disk_start_cdir = ecrec64_disk_cdstart;
  1262. if (ecrec64_disk_cdstart != 0xFFFF) G.ecrec.is_zip64_archive = TRUE;
  1263. }
  1264. if (G.ecrec.num_entries_centrl_dir_ths_disk == 0xFFFF) {
  1265. G.ecrec.num_entries_centrl_dir_ths_disk = ecrec64_this_entries;
  1266. if (ecrec64_this_entries != 0xFFFF) G.ecrec.is_zip64_archive = TRUE;
  1267. }
  1268. if (G.ecrec.total_entries_central_dir == 0xFFFF) {
  1269. G.ecrec.total_entries_central_dir = ecrec64_tot_entries;
  1270. if (ecrec64_tot_entries != 0xFFFF) G.ecrec.is_zip64_archive = TRUE;
  1271. }
  1272. if (G.ecrec.size_central_directory == 0xFFFFFFFFL) {
  1273. G.ecrec.size_central_directory = ecrec64_cdirsize;
  1274. if (ecrec64_cdirsize != 0xFFFFFFFF) G.ecrec.is_zip64_archive = TRUE;
  1275. }
  1276. if (G.ecrec.offset_start_central_directory == 0xFFFFFFFFL) {
  1277. G.ecrec.offset_start_central_directory = ecrec64_offs_cdstart;
  1278. if (ecrec64_offs_cdstart != 0xFFFFFFFF) G.ecrec.is_zip64_archive = TRUE;
  1279. }
  1280. return PK_COOL;
  1281. } /* end function find_ecrec64() */
  1282. /*************************/
  1283. /* Function find_ecrec() */
  1284. /*************************/
  1285. static int find_ecrec(__G__ searchlen) /* return PK-class error */
  1286. __GDEF
  1287. zoff_t searchlen;
  1288. {
  1289. int found = FALSE;
  1290. int error_in_archive;
  1291. int result;
  1292. ec_byte_rec byterec;
  1293. /*---------------------------------------------------------------------------
  1294. Treat case of short zipfile separately.
  1295. ---------------------------------------------------------------------------*/
  1296. if (G.ziplen <= INBUFSIZ) {
  1297. #ifdef USE_STRM_INPUT
  1298. zfseeko(G.zipfd, 0L, SEEK_SET);
  1299. #else /* !USE_STRM_INPUT */
  1300. zlseek(G.zipfd, 0L, SEEK_SET);
  1301. #endif /* ?USE_STRM_INPUT */
  1302. if ((G.incnt = read(G.zipfd,(char *)G.inbuf,(unsigned int)G.ziplen))
  1303. == (int)G.ziplen)
  1304. /* 'P' must be at least (ECREC_SIZE+4) bytes from end of zipfile */
  1305. for (G.inptr = G.inbuf+(int)G.ziplen-(ECREC_SIZE+4);
  1306. G.inptr >= G.inbuf;
  1307. --G.inptr) {
  1308. if ( (*G.inptr == (uch)0x50) && /* ASCII 'P' */
  1309. !memcmp((char *)G.inptr, end_central_sig, 4)) {
  1310. G.incnt -= (int)(G.inptr - G.inbuf);
  1311. found = TRUE;
  1312. break;
  1313. }
  1314. }
  1315. /*---------------------------------------------------------------------------
  1316. Zipfile is longer than INBUFSIZ:
  1317. MB - this next block of code moved to rec_find so that same code can be
  1318. used to look for zip64 ec record. No need to include code above since
  1319. a zip64 ec record will only be looked for if it is a BIG file.
  1320. ---------------------------------------------------------------------------*/
  1321. } else {
  1322. found =
  1323. (rec_find(__G__ searchlen, end_central_sig, ECREC_SIZE) == 0
  1324. ? TRUE : FALSE);
  1325. } /* end if (ziplen > INBUFSIZ) */
  1326. /*---------------------------------------------------------------------------
  1327. Searched through whole region where signature should be without finding
  1328. it. Print informational message and die a horrible death.
  1329. ---------------------------------------------------------------------------*/
  1330. if (!found) {
  1331. if (uO.qflag || uO.zipinfo_mode)
  1332. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1333. Info(slide, 0x401, ((char *)slide,
  1334. LoadFarString(CentDirEndSigNotFound)));
  1335. return PK_ERR; /* failed */
  1336. }
  1337. /*---------------------------------------------------------------------------
  1338. Found the signature, so get the end-central data before returning. Do
  1339. any necessary machine-type conversions (byte ordering, structure padding
  1340. compensation) by reading data into character array and copying to struct.
  1341. ---------------------------------------------------------------------------*/
  1342. G.real_ecrec_offset = G.cur_zipfile_bufstart + (G.inptr-G.inbuf);
  1343. #ifdef TEST
  1344. printf("\n found end-of-central-dir signature at offset %s (%sh)\n",
  1345. FmZofft(G.real_ecrec_offset, NULL, NULL),
  1346. FmZofft(G.real_ecrec_offset, FZOFFT_HEX_DOT_WID, "X"));
  1347. printf(" from beginning of file; offset %d (%.4Xh) within block\n",
  1348. G.inptr-G.inbuf, G.inptr-G.inbuf);
  1349. #endif
  1350. if (readbuf(__G__ (char *)byterec, ECREC_SIZE+4) == 0)
  1351. return PK_EOF;
  1352. G.ecrec.number_this_disk =
  1353. makeword(&byterec[NUMBER_THIS_DISK]);
  1354. G.ecrec.num_disk_start_cdir =
  1355. makeword(&byterec[NUM_DISK_WITH_START_CEN_DIR]);
  1356. G.ecrec.num_entries_centrl_dir_ths_disk =
  1357. makeword(&byterec[NUM_ENTRIES_CEN_DIR_THS_DISK]);
  1358. G.ecrec.total_entries_central_dir =
  1359. makeword(&byterec[TOTAL_ENTRIES_CENTRAL_DIR]);
  1360. G.ecrec.size_central_directory =
  1361. makelong(&byterec[SIZE_CENTRAL_DIRECTORY]);
  1362. G.ecrec.offset_start_central_directory =
  1363. makelong(&byterec[OFFSET_START_CENTRAL_DIRECTORY]);
  1364. G.ecrec.zipfile_comment_length =
  1365. makeword(&byterec[ZIPFILE_COMMENT_LENGTH]);
  1366. /* Now, we have to read the archive comment, BEFORE the file pointer
  1367. is moved away backwards to seek for a Zip64 ECLOC64 structure.
  1368. */
  1369. if ( (error_in_archive = process_zip_cmmnt(__G)) > PK_WARN )
  1370. return error_in_archive;
  1371. /* Next: Check for existence of Zip64 end-of-cent-dir locator
  1372. ECLOC64. This structure must reside on the same volume as the
  1373. classic ECREC, at exactly (ECLOC64_SIZE+4) bytes in front
  1374. of the ECREC.
  1375. The ECLOC64 structure directs to the longer ECREC64 structure
  1376. A ECREC64 will ALWAYS exist for a proper Zip64 archive, as
  1377. the "Version Needed To Extract" field is required to be set
  1378. to 4.5 or higher whenever any Zip64 features are used anywhere
  1379. in the archive, so just check for that to see if this is a
  1380. Zip64 archive.
  1381. */
  1382. result = find_ecrec64(__G__ searchlen+76);
  1383. /* 76 bytes for zip64ec & zip64 locator */
  1384. if (result != PK_COOL) {
  1385. if (error_in_archive < result)
  1386. error_in_archive = result;
  1387. return error_in_archive;
  1388. }
  1389. G.expect_ecrec_offset = G.ecrec.offset_start_central_directory +
  1390. G.ecrec.size_central_directory;
  1391. #ifndef NO_ZIPINFO
  1392. if (uO.zipinfo_mode) {
  1393. /* In ZipInfo mode, additional info about the data found in the
  1394. end-of-central-directory areas is printed out.
  1395. */
  1396. zi_end_central(__G);
  1397. }
  1398. #endif
  1399. return error_in_archive;
  1400. } /* end function find_ecrec() */
  1401. /********************************/
  1402. /* Function process_zip_cmmnt() */
  1403. /********************************/
  1404. static int process_zip_cmmnt(__G) /* return PK-type error code */
  1405. __GDEF
  1406. {
  1407. int error = PK_COOL;
  1408. /*---------------------------------------------------------------------------
  1409. Get the zipfile comment (up to 64KB long), if any, and print it out.
  1410. ---------------------------------------------------------------------------*/
  1411. #ifdef WINDLL
  1412. /* for comment button: */
  1413. if ((!G.fValidate) && (G.lpUserFunctions != NULL))
  1414. G.lpUserFunctions->cchComment = G.ecrec.zipfile_comment_length;
  1415. #endif /* WINDLL */
  1416. #ifndef NO_ZIPINFO
  1417. /* ZipInfo, verbose format */
  1418. if (uO.zipinfo_mode && uO.lflag > 9) {
  1419. /*-------------------------------------------------------------------
  1420. Get the zipfile comment, if any, and print it out.
  1421. (Comment may be up to 64KB long. May the fleas of a thousand
  1422. camels infest the arm-pits of anyone who actually takes advantage
  1423. of this fact.)
  1424. -------------------------------------------------------------------*/
  1425. if (!G.ecrec.zipfile_comment_length)
  1426. Info(slide, 0, ((char *)slide, LoadFarString(NoZipfileComment)));
  1427. else {
  1428. Info(slide, 0, ((char *)slide, LoadFarString(ZipfileCommentDesc),
  1429. G.ecrec.zipfile_comment_length));
  1430. Info(slide, 0, ((char *)slide, LoadFarString(ZipfileCommBegin)));
  1431. if (do_string(__G__ G.ecrec.zipfile_comment_length, DISPLAY))
  1432. error = PK_WARN;
  1433. Info(slide, 0, ((char *)slide, LoadFarString(ZipfileCommEnd)));
  1434. if (error)
  1435. Info(slide, 0, ((char *)slide,
  1436. LoadFarString(ZipfileCommTrunc2)));
  1437. } /* endif (comment exists) */
  1438. /* ZipInfo, non-verbose mode: print zipfile comment only if requested */
  1439. } else if (G.ecrec.zipfile_comment_length &&
  1440. (uO.zflag > 0) && uO.zipinfo_mode) {
  1441. if (do_string(__G__ G.ecrec.zipfile_comment_length, DISPLAY)) {
  1442. Info(slide, 0x401, ((char *)slide,
  1443. LoadFarString(ZipfileCommTrunc1)));
  1444. error = PK_WARN;
  1445. }
  1446. } else
  1447. #endif /* !NO_ZIPINFO */
  1448. if ( G.ecrec.zipfile_comment_length &&
  1449. (uO.zflag > 0
  1450. #ifndef WINDLL
  1451. || (uO.zflag == 0
  1452. # ifndef NO_ZIPINFO
  1453. && !uO.zipinfo_mode
  1454. # endif
  1455. # ifdef TIMESTAMP
  1456. && !uO.T_flag
  1457. # endif
  1458. && !uO.qflag)
  1459. #endif /* !WINDLL */
  1460. ) )
  1461. {
  1462. if (do_string(__G__ G.ecrec.zipfile_comment_length,
  1463. #if (defined(SFX) && defined(CHEAP_SFX_AUTORUN))
  1464. # ifndef NO_ZIPINFO
  1465. (oU.zipinfo_mode ? DISPLAY : CHECK_AUTORUN)
  1466. # else
  1467. CHECK_AUTORUN
  1468. # endif
  1469. #else
  1470. DISPLAY
  1471. #endif
  1472. ))
  1473. {
  1474. Info(slide, 0x401, ((char *)slide,
  1475. LoadFarString(ZipfileCommTrunc1)));
  1476. error = PK_WARN;
  1477. }
  1478. }
  1479. #if (defined(SFX) && defined(CHEAP_SFX_AUTORUN))
  1480. else if (G.ecrec.zipfile_comment_length) {
  1481. if (do_string(__G__ G.ecrec.zipfile_comment_length, CHECK_AUTORUN_Q))
  1482. {
  1483. Info(slide, 0x401, ((char *)slide,
  1484. LoadFarString(ZipfileCommTrunc1)));
  1485. error = PK_WARN;
  1486. }
  1487. }
  1488. #endif
  1489. return error;
  1490. } /* end function process_zip_cmmnt() */
  1491. /************************************/
  1492. /* Function process_cdir_file_hdr() */
  1493. /************************************/
  1494. int process_cdir_file_hdr(__G) /* return PK-type error code */
  1495. __GDEF
  1496. {
  1497. int error;
  1498. /*---------------------------------------------------------------------------
  1499. Get central directory info, save host and method numbers, and set flag
  1500. for lowercase conversion of filename, depending on the OS from which the
  1501. file is coming.
  1502. ---------------------------------------------------------------------------*/
  1503. if ((error = get_cdir_ent(__G)) != 0)
  1504. return error;
  1505. G.pInfo->hostver = G.crec.version_made_by[0];
  1506. G.pInfo->hostnum = MIN(G.crec.version_made_by[1], NUM_HOSTS);
  1507. /* extnum = MIN(crec.version_needed_to_extract[1], NUM_HOSTS); */
  1508. G.pInfo->lcflag = 0;
  1509. if (uO.L_flag == 1) /* name conversion for monocase systems */
  1510. switch (G.pInfo->hostnum) {
  1511. case FS_FAT_: /* PKZIP and zip -k store in uppercase */
  1512. case CPM_: /* like MS-DOS, right? */
  1513. case VM_CMS_: /* all caps? */
  1514. case MVS_: /* all caps? */
  1515. case TANDEM_:
  1516. case TOPS20_:
  1517. case VMS_: /* our Zip uses lowercase, but ASi's doesn't */
  1518. /* case Z_SYSTEM_: ? */
  1519. /* case QDOS_: ? */
  1520. G.pInfo->lcflag = 1; /* convert filename to lowercase */
  1521. break;
  1522. default: /* AMIGA_, FS_HPFS_, FS_NTFS_, MAC_, UNIX_, ATARI_, */
  1523. break; /* FS_VFAT_, ATHEOS_, BEOS_ (Z_SYSTEM_), THEOS_: */
  1524. /* no conversion */
  1525. }
  1526. else if (uO.L_flag > 1) /* let -LL force lower case for all names */
  1527. G.pInfo->lcflag = 1;
  1528. /* do Amigas (AMIGA_) also have volume labels? */
  1529. if (IS_VOLID(G.crec.external_file_attributes) &&
  1530. (G.pInfo->hostnum == FS_FAT_ || G.pInfo->hostnum == FS_HPFS_ ||
  1531. G.pInfo->hostnum == FS_NTFS_ || G.pInfo->hostnum == ATARI_))
  1532. {
  1533. G.pInfo->vollabel = TRUE;
  1534. G.pInfo->lcflag = 0; /* preserve case of volume labels */
  1535. } else
  1536. G.pInfo->vollabel = FALSE;
  1537. /* this flag is needed to detect archives made by "PKZIP for Unix" when
  1538. deciding which kind of codepage conversion has to be applied to
  1539. strings (see do_string() function in fileio.c) */
  1540. G.pInfo->HasUxAtt = (G.crec.external_file_attributes & 0xffff0000L) != 0L;
  1541. #ifdef UNICODE_SUPPORT
  1542. /* remember the state of GPB11 (General Purpuse Bit 11) which indicates
  1543. that the standard path and comment are UTF-8. */
  1544. G.pInfo->GPFIsUTF8
  1545. = (G.crec.general_purpose_bit_flag & (1 << 11)) == (1 << 11);
  1546. #endif
  1547. return PK_COOL;
  1548. } /* end function process_cdir_file_hdr() */
  1549. /***************************/
  1550. /* Function get_cdir_ent() */
  1551. /***************************/
  1552. static int get_cdir_ent(__G) /* return PK-type error code */
  1553. __GDEF
  1554. {
  1555. cdir_byte_hdr byterec;
  1556. /*---------------------------------------------------------------------------
  1557. Read the next central directory entry and do any necessary machine-type
  1558. conversions (byte ordering, structure padding compensation--do so by
  1559. copying the data from the array into which it was read (byterec) to the
  1560. usable struct (crec)).
  1561. ---------------------------------------------------------------------------*/
  1562. if (readbuf(__G__ (char *)byterec, CREC_SIZE) == 0)
  1563. return PK_EOF;
  1564. G.crec.version_made_by[0] = byterec[C_VERSION_MADE_BY_0];
  1565. G.crec.version_made_by[1] = byterec[C_VERSION_MADE_BY_1];
  1566. G.crec.version_needed_to_extract[0] =
  1567. byterec[C_VERSION_NEEDED_TO_EXTRACT_0];
  1568. G.crec.version_needed_to_extract[1] =
  1569. byterec[C_VERSION_NEEDED_TO_EXTRACT_1];
  1570. G.crec.general_purpose_bit_flag =
  1571. makeword(&byterec[C_GENERAL_PURPOSE_BIT_FLAG]);
  1572. G.crec.compression_method =
  1573. makeword(&byterec[C_COMPRESSION_METHOD]);
  1574. G.crec.last_mod_dos_datetime =
  1575. makelong(&byterec[C_LAST_MOD_DOS_DATETIME]);
  1576. G.crec.crc32 =
  1577. makelong(&byterec[C_CRC32]);
  1578. G.crec.csize =
  1579. makelong(&byterec[C_COMPRESSED_SIZE]);
  1580. G.crec.ucsize =
  1581. makelong(&byterec[C_UNCOMPRESSED_SIZE]);
  1582. G.crec.filename_length =
  1583. makeword(&byterec[C_FILENAME_LENGTH]);
  1584. G.crec.extra_field_length =
  1585. makeword(&byterec[C_EXTRA_FIELD_LENGTH]);
  1586. G.crec.file_comment_length =
  1587. makeword(&byterec[C_FILE_COMMENT_LENGTH]);
  1588. G.crec.disk_number_start =
  1589. makeword(&byterec[C_DISK_NUMBER_START]);
  1590. G.crec.internal_file_attributes =
  1591. makeword(&byterec[C_INTERNAL_FILE_ATTRIBUTES]);
  1592. G.crec.external_file_attributes =
  1593. makelong(&byterec[C_EXTERNAL_FILE_ATTRIBUTES]); /* LONG, not word! */
  1594. G.crec.relative_offset_local_header =
  1595. makelong(&byterec[C_RELATIVE_OFFSET_LOCAL_HEADER]);
  1596. return PK_COOL;
  1597. } /* end function get_cdir_ent() */
  1598. /*************************************/
  1599. /* Function process_local_file_hdr() */
  1600. /*************************************/
  1601. int process_local_file_hdr(__G) /* return PK-type error code */
  1602. __GDEF
  1603. {
  1604. local_byte_hdr byterec;
  1605. /*---------------------------------------------------------------------------
  1606. Read the next local file header and do any necessary machine-type con-
  1607. versions (byte ordering, structure padding compensation--do so by copy-
  1608. ing the data from the array into which it was read (byterec) to the
  1609. usable struct (lrec)).
  1610. ---------------------------------------------------------------------------*/
  1611. if (readbuf(__G__ (char *)byterec, LREC_SIZE) == 0)
  1612. return PK_EOF;
  1613. G.lrec.version_needed_to_extract[0] =
  1614. byterec[L_VERSION_NEEDED_TO_EXTRACT_0];
  1615. G.lrec.version_needed_to_extract[1] =
  1616. byterec[L_VERSION_NEEDED_TO_EXTRACT_1];
  1617. G.lrec.general_purpose_bit_flag =
  1618. makeword(&byterec[L_GENERAL_PURPOSE_BIT_FLAG]);
  1619. G.lrec.compression_method = makeword(&byterec[L_COMPRESSION_METHOD]);
  1620. G.lrec.last_mod_dos_datetime = makelong(&byterec[L_LAST_MOD_DOS_DATETIME]);
  1621. G.lrec.crc32 = makelong(&byterec[L_CRC32]);
  1622. G.lrec.csize = makelong(&byterec[L_COMPRESSED_SIZE]);
  1623. G.lrec.ucsize = makelong(&byterec[L_UNCOMPRESSED_SIZE]);
  1624. G.lrec.filename_length = makeword(&byterec[L_FILENAME_LENGTH]);
  1625. G.lrec.extra_field_length = makeword(&byterec[L_EXTRA_FIELD_LENGTH]);
  1626. if ((G.lrec.general_purpose_bit_flag & 8) != 0) {
  1627. /* can't trust local header, use central directory: */
  1628. G.lrec.crc32 = G.pInfo->crc;
  1629. G.lrec.csize = G.pInfo->compr_size;
  1630. G.lrec.ucsize = G.pInfo->uncompr_size;
  1631. }
  1632. G.csize = G.lrec.csize;
  1633. return PK_COOL;
  1634. } /* end function process_local_file_hdr() */
  1635. /*******************************/
  1636. /* Function getZip64Data() */
  1637. /*******************************/
  1638. int getZip64Data(__G__ ef_buf, ef_len)
  1639. __GDEF
  1640. ZCONST uch *ef_buf; /* buffer containing extra field */
  1641. unsigned ef_len; /* total length of extra field */
  1642. {
  1643. unsigned eb_id;
  1644. unsigned eb_len;
  1645. /*---------------------------------------------------------------------------
  1646. This function scans the extra field for zip64 information, ie 8-byte
  1647. versions of compressed file size, uncompressed file size, relative offset
  1648. and a 4-byte version of disk start number.
  1649. Sets both local header and central header fields. Not terribly clever,
  1650. but it means that this procedure is only called in one place.
  1651. ---------------------------------------------------------------------------*/
  1652. if (ef_len == 0 || ef_buf == NULL)
  1653. return PK_COOL;
  1654. Trace((stderr,"\ngetZip64Data: scanning extra field of length %u\n",
  1655. ef_len));
  1656. while (ef_len >= EB_HEADSIZE) {
  1657. eb_id = makeword(EB_ID + ef_buf);
  1658. eb_len = makeword(EB_LEN + ef_buf);
  1659. if (eb_len > (ef_len - EB_HEADSIZE)) {
  1660. /* discovered some extra field inconsistency! */
  1661. Trace((stderr,
  1662. "getZip64Data: block length %u > rest ef_size %u\n", eb_len,
  1663. ef_len - EB_HEADSIZE));
  1664. break;
  1665. }
  1666. if (eb_id == EF_PKSZ64) {
  1667. int offset = EB_HEADSIZE;
  1668. if (G.crec.ucsize == 0xffffffff || G.lrec.ucsize == 0xffffffff){
  1669. G.lrec.ucsize = G.crec.ucsize = makeint64(offset + ef_buf);
  1670. offset += sizeof(G.crec.ucsize);
  1671. }
  1672. if (G.crec.csize == 0xffffffff || G.lrec.csize == 0xffffffff){
  1673. G.csize = G.lrec.csize = G.crec.csize = makeint64(offset + ef_buf);
  1674. offset += sizeof(G.crec.csize);
  1675. }
  1676. if (G.crec.relative_offset_local_header == 0xffffffff){
  1677. G.crec.relative_offset_local_header = makeint64(offset + ef_buf);
  1678. offset += sizeof(G.crec.relative_offset_local_header);
  1679. }
  1680. if (G.crec.disk_number_start == 0xffff){
  1681. G.crec.disk_number_start = (zuvl_t)makelong(offset + ef_buf);
  1682. offset += sizeof(G.crec.disk_number_start);
  1683. }
  1684. }
  1685. /* Skip this extra field block */
  1686. ef_buf += (eb_len + EB_HEADSIZE);
  1687. ef_len -= (eb_len + EB_HEADSIZE);
  1688. }
  1689. return PK_COOL;
  1690. } /* end function getZip64Data() */
  1691. #ifdef UNICODE_SUPPORT
  1692. /*******************************/
  1693. /* Function getUnicodeData() */
  1694. /*******************************/
  1695. int getUnicodeData(__G__ ef_buf, ef_len)
  1696. __GDEF
  1697. ZCONST uch *ef_buf; /* buffer containing extra field */
  1698. unsigned ef_len; /* total length of extra field */
  1699. {
  1700. unsigned eb_id;
  1701. unsigned eb_len;
  1702. /*---------------------------------------------------------------------------
  1703. This function scans the extra field for Unicode information, ie UTF-8
  1704. path extra fields.
  1705. On return, G.unipath_filename =
  1706. NULL, if no Unicode path extra field or error
  1707. "", if the standard path is UTF-8 (free when done)
  1708. null-terminated UTF-8 path (free when done)
  1709. Return PK_COOL if no error.
  1710. ---------------------------------------------------------------------------*/
  1711. G.unipath_filename = NULL;
  1712. if (ef_len == 0 || ef_buf == NULL)
  1713. return PK_COOL;
  1714. Trace((stderr,"\ngetUnicodeData: scanning extra field of length %u\n",
  1715. ef_len));
  1716. while (ef_len >= EB_HEADSIZE) {
  1717. eb_id = makeword(EB_ID + ef_buf);
  1718. eb_len = makeword(EB_LEN + ef_buf);
  1719. if (eb_len > (ef_len - EB_HEADSIZE)) {
  1720. /* discovered some extra field inconsistency! */
  1721. Trace((stderr,
  1722. "getUnicodeData: block length %u > rest ef_size %u\n", eb_len,
  1723. ef_len - EB_HEADSIZE));
  1724. break;
  1725. }
  1726. if (eb_id == EF_UNIPATH) {
  1727. int offset = EB_HEADSIZE;
  1728. ush ULen = eb_len - 5;
  1729. ulg chksum = CRCVAL_INITIAL;
  1730. /* version */
  1731. G.unipath_version = (uch) *(offset + ef_buf);
  1732. offset += 1;
  1733. if (G.unipath_version > 1) {
  1734. /* can do only version 1 */
  1735. Info(slide, 0x401, ((char *)slide,
  1736. LoadFarString(UnicodeVersionError)));
  1737. return PK_ERR;
  1738. }
  1739. /* filename CRC */
  1740. G.unipath_checksum = makelong(offset + ef_buf);
  1741. offset += 4;
  1742. /*
  1743. * Compute 32-bit crc
  1744. */
  1745. chksum = crc32(chksum, (uch *)(G.filename_full),
  1746. strlen(G.filename_full));
  1747. /* If the checksums's don't match then likely filename has been
  1748. * modified and the Unicode Path is no longer valid.
  1749. */
  1750. if (chksum != G.unipath_checksum) {
  1751. Info(slide, 0x401, ((char *)slide,
  1752. LoadFarString(UnicodeMismatchError)));
  1753. if (G.unicode_mismatch == 1) {
  1754. /* warn and continue */
  1755. } else if (G.unicode_mismatch == 2) {
  1756. /* ignore and continue */
  1757. } else if (G.unicode_mismatch == 0) {
  1758. }
  1759. return PK_ERR;
  1760. }
  1761. /* UTF-8 Path */
  1762. if ((G.unipath_filename = malloc(ULen + 1)) == NULL) {
  1763. return PK_ERR;
  1764. }
  1765. if (ULen == 0) {
  1766. /* standard path is UTF-8 so use that */
  1767. G.unipath_filename[0] = '\0';
  1768. } else {
  1769. /* UTF-8 path */
  1770. strncpy(G.unipath_filename,
  1771. (ZCONST char *)(offset + ef_buf), ULen);
  1772. G.unipath_filename[ULen] = '\0';
  1773. }
  1774. }
  1775. /* Skip this extra field block */
  1776. ef_buf += (eb_len + EB_HEADSIZE);
  1777. ef_len -= (eb_len + EB_HEADSIZE);
  1778. }
  1779. return PK_COOL;
  1780. } /* end function getUnicodeData() */
  1781. #ifdef UNICODE_WCHAR
  1782. /*---------------------------------------------
  1783. * Unicode conversion functions
  1784. *
  1785. * Based on functions provided by Paul Kienitz
  1786. *
  1787. *---------------------------------------------
  1788. */
  1789. /*
  1790. NOTES APPLICABLE TO ALL STRING FUNCTIONS:
  1791. All of the x_to_y functions take parameters for an output buffer and
  1792. its available length, and return an int. The value returned is the
  1793. length of the string that the input produces, which may be larger than
  1794. the provided buffer length. If the returned value is less than the
  1795. buffer length, then the contents of the buffer will be null-terminated;
  1796. otherwise, it will not be terminated and may be invalid, possibly
  1797. stopping in the middle of a multibyte sequence.
  1798. In all cases you may pass NULL as the buffer and/or 0 as the length, if
  1799. you just want to learn how much space the string is going to require.
  1800. The functions will return -1 if the input is invalid UTF-8 or cannot be
  1801. encoded as UTF-8.
  1802. */
  1803. static int utf8_char_bytes OF((ZCONST char *utf8));
  1804. static ulg ucs4_char_from_utf8 OF((ZCONST char **utf8));
  1805. static int utf8_to_ucs4_string OF((ZCONST char *utf8, ulg *ucs4buf,
  1806. int buflen));
  1807. /* utility functions for managing UTF-8 and UCS-4 strings */
  1808. /* utf8_char_bytes
  1809. *
  1810. * Returns the number of bytes used by the first character in a UTF-8
  1811. * string, or -1 if the UTF-8 is invalid or null.
  1812. */
  1813. static int utf8_char_bytes(utf8)
  1814. ZCONST char *utf8;
  1815. {
  1816. int t, r;
  1817. unsigned lead;
  1818. if (!utf8)
  1819. return -1; /* no input */
  1820. lead = (unsigned char) *utf8;
  1821. if (lead < 0x80)
  1822. r = 1; /* an ascii-7 character */
  1823. else if (lead < 0xC0)
  1824. return -1; /* error: trailing byte without lead byte */
  1825. else if (lead < 0xE0)
  1826. r = 2; /* an 11 bit character */
  1827. else if (lead < 0xF0)
  1828. r = 3; /* a 16 bit character */
  1829. else if (lead < 0xF8)
  1830. r = 4; /* a 21 bit character (the most currently used) */
  1831. else if (lead < 0xFC)
  1832. r = 5; /* a 26 bit character (shouldn't happen) */
  1833. else if (lead < 0xFE)
  1834. r = 6; /* a 31 bit character (shouldn't happen) */
  1835. else
  1836. return -1; /* error: invalid lead byte */
  1837. for (t = 1; t < r; t++)
  1838. if ((unsigned char) utf8[t] < 0x80 || (unsigned char) utf8[t] >= 0xC0)
  1839. return -1; /* error: not enough valid trailing bytes */
  1840. return r;
  1841. }
  1842. /* ucs4_char_from_utf8
  1843. *
  1844. * Given a reference to a pointer into a UTF-8 string, returns the next
  1845. * UCS-4 character and advances the pointer to the next character sequence.
  1846. * Returns ~0 (= -1 in twos-complement notation) and does not advance the
  1847. * pointer when input is ill-formed.
  1848. */
  1849. static ulg ucs4_char_from_utf8(utf8)
  1850. ZCONST char **utf8;
  1851. {
  1852. ulg ret;
  1853. int t, bytes;
  1854. if (!utf8)
  1855. return ~0L; /* no input */
  1856. bytes = utf8_char_bytes(*utf8);
  1857. if (bytes <= 0)
  1858. return ~0L; /* invalid input */
  1859. if (bytes == 1)
  1860. ret = **utf8; /* ascii-7 */
  1861. else
  1862. ret = **utf8 & (0x7F >> bytes); /* lead byte of a multibyte sequence */
  1863. (*utf8)++;
  1864. for (t = 1; t < bytes; t++) /* consume trailing bytes */
  1865. ret = (ret << 6) | (*((*utf8)++) & 0x3F);
  1866. return (zwchar) ret;
  1867. }
  1868. #if 0 /* currently unused */
  1869. /* utf8_from_ucs4_char - Convert UCS char to UTF-8
  1870. *
  1871. * Returns the number of bytes put into utf8buf to represent ch, from 1 to 6,
  1872. * or -1 if ch is too large to represent. utf8buf must have room for 6 bytes.
  1873. */
  1874. static int utf8_from_ucs4_char(utf8buf, ch)
  1875. char *utf8buf;
  1876. ulg ch;
  1877. {
  1878. int trailing = 0;
  1879. int leadmask = 0x80;
  1880. int leadbits = 0x3F;
  1881. int tch = ch;
  1882. int ret;
  1883. if (ch > 0x7FFFFFFFL)
  1884. return -1; /* UTF-8 can represent 31 bits */
  1885. if (ch < 0x7F)
  1886. {
  1887. *utf8buf++ = (char) ch; /* ascii-7 */
  1888. return 1;
  1889. }
  1890. do {
  1891. trailing++;
  1892. leadmask = (leadmask >> 1) | 0x80;
  1893. leadbits >>= 1;
  1894. tch >>= 6;
  1895. } while (tch & ~leadbits);
  1896. ret = trailing + 1;
  1897. /* produce lead byte */
  1898. *utf8buf++ = (char) (leadmask | (ch >> (6 * trailing)));
  1899. while (--trailing >= 0)
  1900. /* produce trailing bytes */
  1901. *utf8buf++ = (char) (0x80 | ((ch >> (6 * trailing)) & 0x3F));
  1902. return ret;
  1903. }
  1904. #endif /* unused */
  1905. /*===================================================================*/
  1906. /* utf8_to_ucs4_string - convert UTF-8 string to UCS string
  1907. *
  1908. * Return UCS count. Now returns int so can return -1.
  1909. */
  1910. static int utf8_to_ucs4_string(utf8, ucs4buf, buflen)
  1911. ZCONST char *utf8;
  1912. ulg *ucs4buf;
  1913. int buflen;
  1914. {
  1915. int count = 0;
  1916. for (;;)
  1917. {
  1918. ulg ch = ucs4_char_from_utf8(&utf8);
  1919. if (ch == ~0L)
  1920. return -1;
  1921. else
  1922. {
  1923. if (ucs4buf && count < buflen)
  1924. ucs4buf[count] = ch;
  1925. if (ch == 0)
  1926. return count;
  1927. count++;
  1928. }
  1929. }
  1930. }
  1931. #if 0 /* currently unused */
  1932. /* ucs4_string_to_utf8
  1933. *
  1934. *
  1935. */
  1936. static int ucs4_string_to_utf8(ucs4, utf8buf, buflen)
  1937. ZCONST ulg *ucs4;
  1938. char *utf8buf;
  1939. int buflen;
  1940. {
  1941. char mb[6];
  1942. int count = 0;
  1943. if (!ucs4)
  1944. return -1;
  1945. for (;;)
  1946. {
  1947. int mbl = utf8_from_ucs4_char(mb, *ucs4++);
  1948. int c;
  1949. if (mbl <= 0)
  1950. return -1;
  1951. /* We could optimize this a bit by passing utf8buf + count */
  1952. /* directly to utf8_from_ucs4_char when buflen >= count + 6... */
  1953. c = buflen - count;
  1954. if (mbl < c)
  1955. c = mbl;
  1956. if (utf8buf && count < buflen)
  1957. strncpy(utf8buf + count, mb, c);
  1958. if (mbl == 1 && !mb[0])
  1959. return count; /* terminating nul */
  1960. count += mbl;
  1961. }
  1962. }
  1963. /* utf8_chars
  1964. *
  1965. * Wrapper: counts the actual unicode characters in a UTF-8 string.
  1966. */
  1967. static int utf8_chars(utf8)
  1968. ZCONST char *utf8;
  1969. {
  1970. return utf8_to_ucs4_string(utf8, NULL, 0);
  1971. }
  1972. #endif /* unused */
  1973. /* --------------------------------------------------- */
  1974. /* Unicode Support
  1975. *
  1976. * These functions common for all Unicode ports.
  1977. *
  1978. * These functions should allocate and return strings that can be
  1979. * freed with free().
  1980. *
  1981. * 8/27/05 EG
  1982. *
  1983. * Use zwchar for wide char which is unsigned long
  1984. * in zip.h and 32 bits. This avoids problems with
  1985. * different sizes of wchar_t.
  1986. */
  1987. #if 0 /* currently unused */
  1988. /* is_ascii_string
  1989. * Checks if a string is all ascii
  1990. */
  1991. int is_ascii_string(mbstring)
  1992. ZCONST char *mbstring;
  1993. {
  1994. char *p;
  1995. uch c;
  1996. for (p = mbstring; c = (uch)*p; p++) {
  1997. if (c > 0x7F) {
  1998. return 0;
  1999. }
  2000. }
  2001. return 1;
  2002. }
  2003. /* local to UTF-8 */
  2004. char *local_to_utf8_string(local_string)
  2005. ZCONST char *local_string;
  2006. {
  2007. return wide_to_utf8_string(local_to_wide_string(local_string));
  2008. }
  2009. # endif /* unused */
  2010. /* wide_to_escape_string
  2011. provides a string that represents a wide char not in local char set
  2012. An initial try at an algorithm. Suggestions welcome.
  2013. According to the standard, Unicode character points are restricted to
  2014. the number range from 0 to 0x10FFFF, respective 21 bits.
  2015. For a hexadecimal notation, 2 octets are sufficient for the mostly
  2016. used characters from the "Basic Multilingual Plane", all other
  2017. Unicode characters can be represented by 3 octets (= 6 hex digits).
  2018. The Unicode standard suggests to write Unicode character points
  2019. as 4 resp. 6 hex digits, preprended by "U+".
  2020. (e.g.: U+10FFFF for the highest character point, or U+0030 for the ASCII
  2021. digit "0")
  2022. However, for the purpose of escaping non-ASCII chars in an ASCII character
  2023. stream, the "U" is not a very good escape initializer. Therefore, we
  2024. use the following convention within our Info-ZIP code:
  2025. If not an ASCII char probably need 2 bytes at least. So if
  2026. a 2-byte wide encode it as 4 hex digits with a leading #U. If
  2027. needs 3 bytes then prefix the string with #L. So
  2028. #U1234
  2029. is a 2-byte wide character with bytes 0x12 and 0x34 while
  2030. #L123456
  2031. is a 3-byte wide character with bytes 0x12, 0x34, 0x56.
  2032. On Windows, wide that need two wide characters need to be converted
  2033. to a single number.
  2034. */
  2035. /* set this to the max bytes an escape can be */
  2036. #define MAX_ESCAPE_BYTES 8
  2037. char *wide_to_escape_string(wide_char)
  2038. zwchar wide_char;
  2039. {
  2040. int i;
  2041. zwchar w = wide_char;
  2042. uch b[sizeof(zwchar)];
  2043. char d[3];
  2044. char e[11];
  2045. int len;
  2046. char *r;
  2047. /* fill byte array with zeros */
  2048. memzero(b, sizeof(zwchar));
  2049. /* get bytes in right to left order */
  2050. for (len = 0; w; len++) {
  2051. b[len] = (char)(w % 0x100);
  2052. w /= 0x100;
  2053. }
  2054. strcpy(e, "#");
  2055. /* either 2 bytes or 3 bytes */
  2056. if (len <= 2) {
  2057. len = 2;
  2058. strcat(e, "U");
  2059. } else {
  2060. strcat(e, "L");
  2061. }
  2062. for (i = len - 1; i >= 0; i--) {
  2063. sprintf(d, "%02x", b[i]);
  2064. strcat(e, d);
  2065. }
  2066. if ((r = malloc(strlen(e) + 1)) == NULL) {
  2067. return NULL;
  2068. }
  2069. strcpy(r, e);
  2070. return r;
  2071. }
  2072. #if 0 /* currently unused */
  2073. /* returns the wide character represented by the escape string */
  2074. zwchar escape_string_to_wide(escape_string)
  2075. ZCONST char *escape_string;
  2076. {
  2077. int i;
  2078. zwchar w;
  2079. char c;
  2080. int len;
  2081. ZCONST char *e = escape_string;
  2082. if (e == NULL) {
  2083. return 0;
  2084. }
  2085. if (e[0] != '#') {
  2086. /* no leading # */
  2087. return 0;
  2088. }
  2089. len = strlen(e);
  2090. /* either #U1234 or #L123456 format */
  2091. if (len != 6 && len != 8) {
  2092. return 0;
  2093. }
  2094. w = 0;
  2095. if (e[1] == 'L') {
  2096. if (len != 8) {
  2097. return 0;
  2098. }
  2099. /* 3 bytes */
  2100. for (i = 2; i < 8; i++) {
  2101. c = e[i];
  2102. if (c < '0' || c > '9') {
  2103. return 0;
  2104. }
  2105. w = w * 0x10 + (zwchar)(c - '0');
  2106. }
  2107. } else if (e[1] == 'U') {
  2108. /* 2 bytes */
  2109. for (i = 2; i < 6; i++) {
  2110. c = e[i];
  2111. if (c < '0' || c > '9') {
  2112. return 0;
  2113. }
  2114. w = w * 0x10 + (zwchar)(c - '0');
  2115. }
  2116. }
  2117. return w;
  2118. }
  2119. #endif /* unused */
  2120. #ifndef WIN32 /* WIN32 supplies a special variant of this function */
  2121. /* convert wide character string to multi-byte character string */
  2122. char *wide_to_local_string(wide_string, escape_all)
  2123. ZCONST zwchar *wide_string;
  2124. int escape_all;
  2125. {
  2126. int i;
  2127. wchar_t wc;
  2128. int b;
  2129. int state_dependent;
  2130. int wsize = 0;
  2131. int max_bytes = MB_CUR_MAX;
  2132. char buf[9];
  2133. char *buffer = NULL;
  2134. char *local_string = NULL;
  2135. for (wsize = 0; wide_string[wsize]; wsize++) ;
  2136. if (max_bytes < MAX_ESCAPE_BYTES)
  2137. max_bytes = MAX_ESCAPE_BYTES;
  2138. if ((buffer = (char *)malloc(wsize * max_bytes + 1)) == NULL) {
  2139. return NULL;
  2140. }
  2141. /* convert it */
  2142. buffer[0] = '\0';
  2143. /* set initial state if state-dependent encoding */
  2144. wc = (wchar_t)'a';
  2145. b = wctomb(NULL, wc);
  2146. if (b == 0)
  2147. state_dependent = 0;
  2148. else
  2149. state_dependent = 1;
  2150. for (i = 0; i < wsize; i++) {
  2151. if (sizeof(wchar_t) < 4 && wide_string[i] > 0xFFFF) {
  2152. /* wchar_t probably 2 bytes */
  2153. /* could do surrogates if state_dependent and wctomb can do */
  2154. wc = zwchar_to_wchar_t_default_char;
  2155. } else {
  2156. wc = (wchar_t)wide_string[i];
  2157. }
  2158. b = wctomb(buf, wc);
  2159. if (escape_all) {
  2160. if (b == 1 && (uch)buf[0] <= 0x7f) {
  2161. /* ASCII */
  2162. strncat(buffer, buf, b);
  2163. } else {
  2164. /* use escape for wide character */
  2165. char *escape_string = wide_to_escape_string(wide_string[i]);
  2166. strcat(buffer, escape_string);
  2167. free(escape_string);
  2168. }
  2169. } else if (b > 0) {
  2170. /* multi-byte char */
  2171. strncat(buffer, buf, b);
  2172. } else {
  2173. /* no MB for this wide */
  2174. /* use escape for wide character */
  2175. char *escape_string = wide_to_escape_string(wide_string[i]);
  2176. strcat(buffer, escape_string);
  2177. free(escape_string);
  2178. }
  2179. }
  2180. if ((local_string = (char *)malloc(strlen(buffer) + 1)) != NULL) {
  2181. strcpy(local_string, buffer);
  2182. }
  2183. free(buffer);
  2184. return local_string;
  2185. }
  2186. #endif /* !WIN32 */
  2187. #if 0 /* currently unused */
  2188. /* convert local string to display character set string */
  2189. char *local_to_display_string(local_string)
  2190. ZCONST char *local_string;
  2191. {
  2192. char *display_string;
  2193. /* For Windows, OEM string should never be bigger than ANSI string, says
  2194. CharToOem description.
  2195. For all other ports, just make a copy of local_string.
  2196. */
  2197. if ((display_string = (char *)malloc(strlen(local_string) + 1)) == NULL) {
  2198. return NULL;
  2199. }
  2200. strcpy(display_string, local_string);
  2201. #ifdef EBCDIC
  2202. {
  2203. char *ebc;
  2204. if ((ebc = malloc(strlen(display_string) + 1)) == NULL) {
  2205. return NULL;
  2206. }
  2207. strtoebc(ebc, display_string);
  2208. free(display_string);
  2209. display_string = ebc;
  2210. }
  2211. #endif
  2212. return display_string;
  2213. }
  2214. #endif /* unused */
  2215. /* UTF-8 to local */
  2216. char *utf8_to_local_string(utf8_string, escape_all)
  2217. ZCONST char *utf8_string;
  2218. int escape_all;
  2219. {
  2220. zwchar *wide = utf8_to_wide_string(utf8_string);
  2221. char *loc = wide_to_local_string(wide, escape_all);
  2222. free(wide);
  2223. return loc;
  2224. }
  2225. #if 0 /* currently unused */
  2226. /* convert multi-byte character string to wide character string */
  2227. zwchar *local_to_wide_string(local_string)
  2228. ZCONST char *local_string;
  2229. {
  2230. int wsize;
  2231. wchar_t *wc_string;
  2232. zwchar *wide_string;
  2233. /* for now try to convert as string - fails if a bad char in string */
  2234. wsize = mbstowcs(NULL, local_string, strlen(local_string) + 1);
  2235. if (wsize == (size_t)-1) {
  2236. /* could not convert */
  2237. return NULL;
  2238. }
  2239. /* convert it */
  2240. if ((wc_string = (wchar_t *)malloc((wsize + 1) * sizeof(wchar_t))) == NULL) {
  2241. return NULL;
  2242. }
  2243. wsize = mbstowcs(wc_string, local_string, strlen(local_string) + 1);
  2244. wc_string[wsize] = (wchar_t) 0;
  2245. /* in case wchar_t is not zwchar */
  2246. if ((wide_string = (zwchar *)malloc((wsize + 1) * sizeof(zwchar))) == NULL) {
  2247. return NULL;
  2248. }
  2249. for (wsize = 0; wide_string[wsize] = (zwchar)wc_string[wsize]; wsize++) ;
  2250. wide_string[wsize] = (zwchar) 0;
  2251. free(wc_string);
  2252. return wide_string;
  2253. }
  2254. /* convert wide string to UTF-8 */
  2255. char *wide_to_utf8_string(wide_string)
  2256. ZCONST zwchar *wide_string;
  2257. {
  2258. int mbcount;
  2259. char *utf8_string;
  2260. /* get size of utf8 string */
  2261. mbcount = ucs4_string_to_utf8(wide_string, NULL, 0);
  2262. if (mbcount == -1)
  2263. return NULL;
  2264. if ((utf8_string = (char *) malloc(mbcount + 1)) == NULL) {
  2265. return NULL;
  2266. }
  2267. mbcount = ucs4_string_to_utf8(wide_string, utf8_string, mbcount + 1);
  2268. if (mbcount == -1)
  2269. return NULL;
  2270. return utf8_string;
  2271. }
  2272. #endif /* unused */
  2273. /* convert UTF-8 string to wide string */
  2274. zwchar *utf8_to_wide_string(utf8_string)
  2275. ZCONST char *utf8_string;
  2276. {
  2277. int wcount;
  2278. zwchar *wide_string;
  2279. wcount = utf8_to_ucs4_string(utf8_string, NULL, 0);
  2280. if (wcount == -1)
  2281. return NULL;
  2282. if ((wide_string = (zwchar *) malloc((wcount + 1) * sizeof(zwchar)))
  2283. == NULL) {
  2284. return NULL;
  2285. }
  2286. wcount = utf8_to_ucs4_string(utf8_string, wide_string, wcount + 1);
  2287. return wide_string;
  2288. }
  2289. #endif /* UNICODE_WCHAR */
  2290. #endif /* UNICODE_SUPPORT */
  2291. #ifdef USE_EF_UT_TIME
  2292. #ifdef IZ_HAVE_UXUIDGID
  2293. static int read_ux3_value(dbuf, uidgid_sz, p_uidgid)
  2294. ZCONST uch *dbuf; /* buffer a uid or gid value */
  2295. unsigned uidgid_sz; /* size of uid/gid value */
  2296. ulg *p_uidgid; /* return storage: uid or gid value */
  2297. {
  2298. zusz_t uidgid64;
  2299. switch (uidgid_sz) {
  2300. case 2:
  2301. *p_uidgid = (ulg)makeword(dbuf);
  2302. break;
  2303. case 4:
  2304. *p_uidgid = (ulg)makelong(dbuf);
  2305. break;
  2306. case 8:
  2307. uidgid64 = makeint64(dbuf);
  2308. #ifndef LARGE_FILE_SUPPORT
  2309. if (uidgid64 == (zusz_t)0xffffffffL)
  2310. return FALSE;
  2311. #endif
  2312. *p_uidgid = (ulg)uidgid64;
  2313. if ((zusz_t)(*p_uidgid) != uidgid64)
  2314. return FALSE;
  2315. break;
  2316. }
  2317. return TRUE;
  2318. }
  2319. #endif /* IZ_HAVE_UXUIDGID */
  2320. /*******************************/
  2321. /* Function ef_scan_for_izux() */
  2322. /*******************************/
  2323. unsigned ef_scan_for_izux(ef_buf, ef_len, ef_is_c, dos_mdatetime,
  2324. z_utim, z_uidgid)
  2325. ZCONST uch *ef_buf; /* buffer containing extra field */
  2326. unsigned ef_len; /* total length of extra field */
  2327. int ef_is_c; /* flag indicating "is central extra field" */
  2328. ulg dos_mdatetime; /* last_mod_file_date_time in DOS format */
  2329. iztimes *z_utim; /* return storage: atime, mtime, ctime */
  2330. ulg *z_uidgid; /* return storage: uid and gid */
  2331. {
  2332. unsigned flags = 0;
  2333. unsigned eb_id;
  2334. unsigned eb_len;
  2335. int have_new_type_eb = 0;
  2336. long i_time; /* buffer for Unix style 32-bit integer time value */
  2337. #ifdef TIME_T_TYPE_DOUBLE
  2338. int ut_in_archive_sgn = 0;
  2339. #else
  2340. int ut_zip_unzip_compatible = FALSE;
  2341. #endif
  2342. /*---------------------------------------------------------------------------
  2343. This function scans the extra field for EF_TIME, EF_IZUNIX2, EF_IZUNIX, or
  2344. EF_PKUNIX blocks containing Unix-style time_t (GMT) values for the entry's
  2345. access, creation, and modification time.
  2346. If a valid block is found, the time stamps are copied to the iztimes
  2347. structure (provided the z_utim pointer is not NULL).
  2348. If a IZUNIX2 block is found or the IZUNIX block contains UID/GID fields,
  2349. and the z_uidgid array pointer is valid (!= NULL), the owner info is
  2350. transfered as well.
  2351. The presence of an EF_TIME or EF_IZUNIX2 block results in ignoring all
  2352. data from probably present obsolete EF_IZUNIX blocks.
  2353. If multiple blocks of the same type are found, only the information from
  2354. the last block is used.
  2355. The return value is a combination of the EF_TIME Flags field with an
  2356. additional flag bit indicating the presence of valid UID/GID info,
  2357. or 0 in case of failure.
  2358. ---------------------------------------------------------------------------*/
  2359. if (ef_len == 0 || ef_buf == NULL || (z_utim == 0 && z_uidgid == NULL))
  2360. return 0;
  2361. TTrace((stderr,"\nef_scan_for_izux: scanning extra field of length %u\n",
  2362. ef_len));
  2363. while (ef_len >= EB_HEADSIZE) {
  2364. eb_id = makeword(EB_ID + ef_buf);
  2365. eb_len = makeword(EB_LEN + ef_buf);
  2366. if (eb_len > (ef_len - EB_HEADSIZE)) {
  2367. /* discovered some extra field inconsistency! */
  2368. TTrace((stderr,
  2369. "ef_scan_for_izux: block length %u > rest ef_size %u\n", eb_len,
  2370. ef_len - EB_HEADSIZE));
  2371. break;
  2372. }
  2373. switch (eb_id) {
  2374. case EF_TIME:
  2375. flags &= ~0x0ff; /* ignore previous IZUNIX or EF_TIME fields */
  2376. have_new_type_eb = 1;
  2377. if ( eb_len >= EB_UT_MINLEN && z_utim != NULL) {
  2378. unsigned eb_idx = EB_UT_TIME1;
  2379. TTrace((stderr,"ef_scan_for_izux: found TIME extra field\n"));
  2380. flags |= (ef_buf[EB_HEADSIZE+EB_UT_FLAGS] & 0x0ff);
  2381. if ((flags & EB_UT_FL_MTIME)) {
  2382. if ((eb_idx+4) <= eb_len) {
  2383. i_time = (long)makelong((EB_HEADSIZE+eb_idx) + ef_buf);
  2384. eb_idx += 4;
  2385. TTrace((stderr," UT e.f. modification time = %ld\n",
  2386. i_time));
  2387. #ifdef TIME_T_TYPE_DOUBLE
  2388. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2389. if (dos_mdatetime == DOSTIME_MINIMUM) {
  2390. ut_in_archive_sgn = -1;
  2391. z_utim->mtime =
  2392. (time_t)((long)i_time | (~(long)0x7fffffffL));
  2393. } else if (dos_mdatetime >= DOSTIME_2038_01_18) {
  2394. ut_in_archive_sgn = 1;
  2395. z_utim->mtime =
  2396. (time_t)((ulg)i_time & (ulg)0xffffffffL);
  2397. } else {
  2398. ut_in_archive_sgn = 0;
  2399. /* cannot determine sign of mtime;
  2400. without modtime: ignore complete UT field */
  2401. flags &= ~0x0ff; /* no time_t times available */
  2402. TTrace((stderr,
  2403. " UT modtime range error; ignore e.f.!\n"));
  2404. break; /* stop scanning this field */
  2405. }
  2406. } else {
  2407. /* cannot determine, safe assumption is FALSE */
  2408. ut_in_archive_sgn = 0;
  2409. z_utim->mtime = (time_t)i_time;
  2410. }
  2411. #else /* !TIME_T_TYPE_DOUBLE */
  2412. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2413. ut_zip_unzip_compatible =
  2414. ((time_t)0x80000000L < (time_t)0L)
  2415. ? (dos_mdatetime == DOSTIME_MINIMUM)
  2416. : (dos_mdatetime >= DOSTIME_2038_01_18);
  2417. if (!ut_zip_unzip_compatible) {
  2418. /* UnZip interprets mtime differently than Zip;
  2419. without modtime: ignore complete UT field */
  2420. flags &= ~0x0ff; /* no time_t times available */
  2421. TTrace((stderr,
  2422. " UT modtime range error; ignore e.f.!\n"));
  2423. break; /* stop scanning this field */
  2424. }
  2425. } else {
  2426. /* cannot determine, safe assumption is FALSE */
  2427. ut_zip_unzip_compatible = FALSE;
  2428. }
  2429. z_utim->mtime = (time_t)i_time;
  2430. #endif /* ?TIME_T_TYPE_DOUBLE */
  2431. } else {
  2432. flags &= ~EB_UT_FL_MTIME;
  2433. TTrace((stderr," UT e.f. truncated; no modtime\n"));
  2434. }
  2435. }
  2436. if (ef_is_c) {
  2437. break; /* central version of TIME field ends here */
  2438. }
  2439. if (flags & EB_UT_FL_ATIME) {
  2440. if ((eb_idx+4) <= eb_len) {
  2441. i_time = (long)makelong((EB_HEADSIZE+eb_idx) + ef_buf);
  2442. eb_idx += 4;
  2443. TTrace((stderr," UT e.f. access time = %ld\n",
  2444. i_time));
  2445. #ifdef TIME_T_TYPE_DOUBLE
  2446. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2447. if (ut_in_archive_sgn == -1)
  2448. z_utim->atime =
  2449. (time_t)((long)i_time | (~(long)0x7fffffffL));
  2450. } else if (ut_in_archive_sgn == 1) {
  2451. z_utim->atime =
  2452. (time_t)((ulg)i_time & (ulg)0xffffffffL);
  2453. } else {
  2454. /* sign of 32-bit time is unknown -> ignore it */
  2455. flags &= ~EB_UT_FL_ATIME;
  2456. TTrace((stderr,
  2457. " UT access time range error: skip time!\n"));
  2458. }
  2459. } else {
  2460. z_utim->atime = (time_t)i_time;
  2461. }
  2462. #else /* !TIME_T_TYPE_DOUBLE */
  2463. if (((ulg)(i_time) & (ulg)(0x80000000L)) &&
  2464. !ut_zip_unzip_compatible) {
  2465. flags &= ~EB_UT_FL_ATIME;
  2466. TTrace((stderr,
  2467. " UT access time range error: skip time!\n"));
  2468. } else {
  2469. z_utim->atime = (time_t)i_time;
  2470. }
  2471. #endif /* ?TIME_T_TYPE_DOUBLE */
  2472. } else {
  2473. flags &= ~EB_UT_FL_ATIME;
  2474. }
  2475. }
  2476. if (flags & EB_UT_FL_CTIME) {
  2477. if ((eb_idx+4) <= eb_len) {
  2478. i_time = (long)makelong((EB_HEADSIZE+eb_idx) + ef_buf);
  2479. TTrace((stderr," UT e.f. creation time = %ld\n",
  2480. i_time));
  2481. #ifdef TIME_T_TYPE_DOUBLE
  2482. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2483. if (ut_in_archive_sgn == -1)
  2484. z_utim->ctime =
  2485. (time_t)((long)i_time | (~(long)0x7fffffffL));
  2486. } else if (ut_in_archive_sgn == 1) {
  2487. z_utim->ctime =
  2488. (time_t)((ulg)i_time & (ulg)0xffffffffL);
  2489. } else {
  2490. /* sign of 32-bit time is unknown -> ignore it */
  2491. flags &= ~EB_UT_FL_CTIME;
  2492. TTrace((stderr,
  2493. " UT creation time range error: skip time!\n"));
  2494. }
  2495. } else {
  2496. z_utim->ctime = (time_t)i_time;
  2497. }
  2498. #else /* !TIME_T_TYPE_DOUBLE */
  2499. if (((ulg)(i_time) & (ulg)(0x80000000L)) &&
  2500. !ut_zip_unzip_compatible) {
  2501. flags &= ~EB_UT_FL_CTIME;
  2502. TTrace((stderr,
  2503. " UT creation time range error: skip time!\n"));
  2504. } else {
  2505. z_utim->ctime = (time_t)i_time;
  2506. }
  2507. #endif /* ?TIME_T_TYPE_DOUBLE */
  2508. } else {
  2509. flags &= ~EB_UT_FL_CTIME;
  2510. }
  2511. }
  2512. }
  2513. break;
  2514. case EF_IZUNIX2:
  2515. if (have_new_type_eb == 0) {
  2516. flags &= ~0x0ff; /* ignore any previous IZUNIX field */
  2517. have_new_type_eb = 1;
  2518. }
  2519. #ifdef IZ_HAVE_UXUIDGID
  2520. if (have_new_type_eb > 1)
  2521. break; /* IZUNIX3 overrides IZUNIX2 e.f. block ! */
  2522. if (eb_len == EB_UX2_MINLEN && z_uidgid != NULL) {
  2523. z_uidgid[0] = (ulg)makeword((EB_HEADSIZE+EB_UX2_UID) + ef_buf);
  2524. z_uidgid[1] = (ulg)makeword((EB_HEADSIZE+EB_UX2_GID) + ef_buf);
  2525. flags |= EB_UX2_VALID; /* signal success */
  2526. }
  2527. #endif
  2528. break;
  2529. case EF_IZUNIX3:
  2530. /* new 3rd generation Unix ef */
  2531. have_new_type_eb = 2;
  2532. /*
  2533. Version 1 byte version of this extra field, currently 1
  2534. UIDSize 1 byte Size of UID field
  2535. UID Variable UID for this entry
  2536. GIDSize 1 byte Size of GID field
  2537. GID Variable GID for this entry
  2538. */
  2539. #ifdef IZ_HAVE_UXUIDGID
  2540. if (eb_len >= EB_UX3_MINLEN
  2541. && z_uidgid != NULL
  2542. && (*((EB_HEADSIZE + 0) + ef_buf) == 1)
  2543. /* only know about version 1 */
  2544. {
  2545. uch uid_size;
  2546. uch gid_size;
  2547. uid_size = *((EB_HEADSIZE + 1) + ef_buf);
  2548. gid_size = *((EB_HEADSIZE + uid_size + 2) + ef_buf);
  2549. flags &= ~0x0ff; /* ignore any previous UNIX field */
  2550. if ( read_ux3_value((EB_HEADSIZE + 2) + ef_buf,
  2551. uid_size, z_uidgid[0])
  2552. &&
  2553. read_ux3_value((EB_HEADSIZE + uid_size + 3) + ef_buf,
  2554. gid_size, z_uidgid[1]) )
  2555. {
  2556. flags |= EB_UX2_VALID; /* signal success */
  2557. }
  2558. }
  2559. #endif /* IZ_HAVE_UXUIDGID */
  2560. break;
  2561. case EF_IZUNIX:
  2562. case EF_PKUNIX: /* PKUNIX e.f. layout is identical to IZUNIX */
  2563. if (eb_len >= EB_UX_MINLEN) {
  2564. TTrace((stderr,"ef_scan_for_izux: found %s extra field\n",
  2565. (eb_id == EF_IZUNIX ? "IZUNIX" : "PKUNIX")));
  2566. if (have_new_type_eb > 0) {
  2567. break; /* Ignore IZUNIX extra field block ! */
  2568. }
  2569. if (z_utim != NULL) {
  2570. flags |= (EB_UT_FL_MTIME | EB_UT_FL_ATIME);
  2571. i_time = (long)makelong((EB_HEADSIZE+EB_UX_MTIME)+ef_buf);
  2572. TTrace((stderr," Unix EF modtime = %ld\n", i_time));
  2573. #ifdef TIME_T_TYPE_DOUBLE
  2574. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2575. if (dos_mdatetime == DOSTIME_MINIMUM) {
  2576. ut_in_archive_sgn = -1;
  2577. z_utim->mtime =
  2578. (time_t)((long)i_time | (~(long)0x7fffffffL));
  2579. } else if (dos_mdatetime >= DOSTIME_2038_01_18) {
  2580. ut_in_archive_sgn = 1;
  2581. z_utim->mtime =
  2582. (time_t)((ulg)i_time & (ulg)0xffffffffL);
  2583. } else {
  2584. ut_in_archive_sgn = 0;
  2585. /* cannot determine sign of mtime;
  2586. without modtime: ignore complete UT field */
  2587. flags &= ~0x0ff; /* no time_t times available */
  2588. TTrace((stderr,
  2589. " UX modtime range error: ignore e.f.!\n"));
  2590. }
  2591. } else {
  2592. /* cannot determine, safe assumption is FALSE */
  2593. ut_in_archive_sgn = 0;
  2594. z_utim->mtime = (time_t)i_time;
  2595. }
  2596. #else /* !TIME_T_TYPE_DOUBLE */
  2597. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2598. ut_zip_unzip_compatible =
  2599. ((time_t)0x80000000L < (time_t)0L)
  2600. ? (dos_mdatetime == DOSTIME_MINIMUM)
  2601. : (dos_mdatetime >= DOSTIME_2038_01_18);
  2602. if (!ut_zip_unzip_compatible) {
  2603. /* UnZip interpretes mtime differently than Zip;
  2604. without modtime: ignore complete UT field */
  2605. flags &= ~0x0ff; /* no time_t times available */
  2606. TTrace((stderr,
  2607. " UX modtime range error: ignore e.f.!\n"));
  2608. }
  2609. } else {
  2610. /* cannot determine, safe assumption is FALSE */
  2611. ut_zip_unzip_compatible = FALSE;
  2612. }
  2613. z_utim->mtime = (time_t)i_time;
  2614. #endif /* ?TIME_T_TYPE_DOUBLE */
  2615. i_time = (long)makelong((EB_HEADSIZE+EB_UX_ATIME)+ef_buf);
  2616. TTrace((stderr," Unix EF actime = %ld\n", i_time));
  2617. #ifdef TIME_T_TYPE_DOUBLE
  2618. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2619. if (ut_in_archive_sgn == -1)
  2620. z_utim->atime =
  2621. (time_t)((long)i_time | (~(long)0x7fffffffL));
  2622. } else if (ut_in_archive_sgn == 1) {
  2623. z_utim->atime =
  2624. (time_t)((ulg)i_time & (ulg)0xffffffffL);
  2625. } else if (flags & 0x0ff) {
  2626. /* sign of 32-bit time is unknown -> ignore it */
  2627. flags &= ~EB_UT_FL_ATIME;
  2628. TTrace((stderr,
  2629. " UX access time range error: skip time!\n"));
  2630. }
  2631. } else {
  2632. z_utim->atime = (time_t)i_time;
  2633. }
  2634. #else /* !TIME_T_TYPE_DOUBLE */
  2635. if (((ulg)(i_time) & (ulg)(0x80000000L)) &&
  2636. !ut_zip_unzip_compatible && (flags & 0x0ff)) {
  2637. /* atime not in range of UnZip's time_t */
  2638. flags &= ~EB_UT_FL_ATIME;
  2639. TTrace((stderr,
  2640. " UX access time range error: skip time!\n"));
  2641. } else {
  2642. z_utim->atime = (time_t)i_time;
  2643. }
  2644. #endif /* ?TIME_T_TYPE_DOUBLE */
  2645. }
  2646. #ifdef IZ_HAVE_UXUIDGID
  2647. if (eb_len >= EB_UX_FULLSIZE && z_uidgid != NULL) {
  2648. z_uidgid[0] = makeword((EB_HEADSIZE+EB_UX_UID) + ef_buf);
  2649. z_uidgid[1] = makeword((EB_HEADSIZE+EB_UX_GID) + ef_buf);
  2650. flags |= EB_UX2_VALID;
  2651. }
  2652. #endif /* IZ_HAVE_UXUIDGID */
  2653. }
  2654. break;
  2655. default:
  2656. break;
  2657. }
  2658. /* Skip this extra field block */
  2659. ef_buf += (eb_len + EB_HEADSIZE);
  2660. ef_len -= (eb_len + EB_HEADSIZE);
  2661. }
  2662. return flags;
  2663. }
  2664. #endif /* USE_EF_UT_TIME */
  2665. #if (defined(RISCOS) || defined(ACORN_FTYPE_NFS))
  2666. #define SPARKID_2 0x30435241 /* = "ARC0" */
  2667. /*******************************/
  2668. /* Function getRISCOSexfield() */
  2669. /*******************************/
  2670. zvoid *getRISCOSexfield(ef_buf, ef_len)
  2671. ZCONST uch *ef_buf; /* buffer containing extra field */
  2672. unsigned ef_len; /* total length of extra field */
  2673. {
  2674. unsigned eb_id;
  2675. unsigned eb_len;
  2676. /*---------------------------------------------------------------------------
  2677. This function scans the extra field for a Acorn SPARK filetype ef-block.
  2678. If a valid block is found, the function returns a pointer to the start
  2679. of the SPARK_EF block in the extra field buffer. Otherwise, a NULL
  2680. pointer is returned.
  2681. ---------------------------------------------------------------------------*/
  2682. if (ef_len == 0 || ef_buf == NULL)
  2683. return NULL;
  2684. Trace((stderr,"\ngetRISCOSexfield: scanning extra field of length %u\n",
  2685. ef_len));
  2686. while (ef_len >= EB_HEADSIZE) {
  2687. eb_id = makeword(EB_ID + ef_buf);
  2688. eb_len = makeword(EB_LEN + ef_buf);
  2689. if (eb_len > (ef_len - EB_HEADSIZE)) {
  2690. /* discovered some extra field inconsistency! */
  2691. Trace((stderr,
  2692. "getRISCOSexfield: block length %u > rest ef_size %u\n", eb_len,
  2693. ef_len - EB_HEADSIZE));
  2694. break;
  2695. }
  2696. if (eb_id == EF_SPARK && (eb_len == 24 || eb_len == 20)) {
  2697. if (makelong(EB_HEADSIZE + ef_buf) == SPARKID_2) {
  2698. /* Return a pointer to the valid SPARK filetype ef block */
  2699. return (zvoid *)ef_buf;
  2700. }
  2701. }
  2702. /* Skip this extra field block */
  2703. ef_buf += (eb_len + EB_HEADSIZE);
  2704. ef_len -= (eb_len + EB_HEADSIZE);
  2705. }
  2706. return NULL;
  2707. }
  2708. #endif /* (RISCOS || ACORN_FTYPE_NFS) */