🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

1710 lines
53 KiB

  1. /*
  2. * ZIP support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE.txt in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon, with some peeking at "unzip.c"
  7. * by Gilles Vollant.
  8. */
  9. #define __PHYSICSFS_INTERNAL__
  10. #include "physfs_internal.h"
  11. #if PHYSFS_SUPPORTS_ZIP
  12. #include <errno.h>
  13. #include <time.h>
  14. #include "physfs_miniz.h"
  15. /*
  16. * A buffer of ZIP_READBUFSIZE is allocated for each compressed file opened,
  17. * and is freed when you close the file; compressed data is read into
  18. * this buffer, and then is decompressed into the buffer passed to
  19. * PHYSFS_read().
  20. *
  21. * Uncompressed entries in a zipfile do not allocate this buffer; they just
  22. * read data directly into the buffer passed to PHYSFS_read().
  23. *
  24. * Depending on your speed and memory requirements, you should tweak this
  25. * value.
  26. */
  27. #define ZIP_READBUFSIZE (16 * 1024)
  28. /*
  29. * Entries are "unresolved" until they are first opened. At that time,
  30. * local file headers parsed/validated, data offsets will be updated to look
  31. * at the actual file data instead of the header, and symlinks will be
  32. * followed and optimized. This means that we don't seek and read around the
  33. * archive until forced to do so, and after the first time, we had to do
  34. * less reading and parsing, which is very CD-ROM friendly.
  35. */
  36. typedef enum
  37. {
  38. ZIP_UNRESOLVED_FILE,
  39. ZIP_UNRESOLVED_SYMLINK,
  40. ZIP_RESOLVING,
  41. ZIP_RESOLVED,
  42. ZIP_DIRECTORY,
  43. ZIP_BROKEN_FILE,
  44. ZIP_BROKEN_SYMLINK
  45. } ZipResolveType;
  46. /*
  47. * One ZIPentry is kept for each file in an open ZIP archive.
  48. */
  49. typedef struct _ZIPentry
  50. {
  51. __PHYSFS_DirTreeEntry tree; /* manages directory tree */
  52. struct _ZIPentry *symlink; /* NULL or file we symlink to */
  53. ZipResolveType resolved; /* Have we resolved file/symlink? */
  54. PHYSFS_uint64 offset; /* offset of data in archive */
  55. PHYSFS_uint16 version; /* version made by */
  56. PHYSFS_uint16 version_needed; /* version needed to extract */
  57. PHYSFS_uint16 general_bits; /* general purpose bits */
  58. PHYSFS_uint16 compression_method; /* compression method */
  59. PHYSFS_uint32 crc; /* crc-32 */
  60. PHYSFS_uint64 compressed_size; /* compressed size */
  61. PHYSFS_uint64 uncompressed_size; /* uncompressed size */
  62. PHYSFS_sint64 last_mod_time; /* last file mod time */
  63. PHYSFS_uint32 dos_mod_time; /* original MS-DOS style mod time */
  64. } ZIPentry;
  65. /*
  66. * One ZIPinfo is kept for each open ZIP archive.
  67. */
  68. typedef struct
  69. {
  70. __PHYSFS_DirTree tree; /* manages directory tree. */
  71. PHYSFS_Io *io; /* the i/o interface for this archive. */
  72. int zip64; /* non-zero if this is a Zip64 archive. */
  73. int has_crypto; /* non-zero if any entry uses encryption. */
  74. } ZIPinfo;
  75. /*
  76. * One ZIPfileinfo is kept for each open file in a ZIP archive.
  77. */
  78. typedef struct
  79. {
  80. ZIPentry *entry; /* Info on file. */
  81. PHYSFS_Io *io; /* physical file handle. */
  82. PHYSFS_uint32 compressed_position; /* offset in compressed data. */
  83. PHYSFS_uint32 uncompressed_position; /* tell() position. */
  84. PHYSFS_uint8 *buffer; /* decompression buffer. */
  85. PHYSFS_uint32 crypto_keys[3]; /* for "traditional" crypto. */
  86. PHYSFS_uint32 initial_crypto_keys[3]; /* for "traditional" crypto. */
  87. z_stream stream; /* zlib stream state. */
  88. } ZIPfileinfo;
  89. /* Magic numbers... */
  90. #define ZIP_LOCAL_FILE_SIG 0x04034b50
  91. #define ZIP_CENTRAL_DIR_SIG 0x02014b50
  92. #define ZIP_END_OF_CENTRAL_DIR_SIG 0x06054b50
  93. #define ZIP64_END_OF_CENTRAL_DIR_SIG 0x06064b50
  94. #define ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIG 0x07064b50
  95. #define ZIP64_EXTENDED_INFO_EXTRA_FIELD_SIG 0x0001
  96. /* compression methods... */
  97. #define COMPMETH_NONE 0
  98. /* ...and others... */
  99. #define UNIX_FILETYPE_MASK 0170000
  100. #define UNIX_FILETYPE_SYMLINK 0120000
  101. #define ZIP_GENERAL_BITS_TRADITIONAL_CRYPTO (1 << 0)
  102. #define ZIP_GENERAL_BITS_IGNORE_LOCAL_HEADER (1 << 3)
  103. /* support for "traditional" PKWARE encryption. */
  104. static int zip_entry_is_tradional_crypto(const ZIPentry *entry)
  105. {
  106. return (entry->general_bits & ZIP_GENERAL_BITS_TRADITIONAL_CRYPTO) != 0;
  107. } /* zip_entry_is_traditional_crypto */
  108. static int zip_entry_ignore_local_header(const ZIPentry *entry)
  109. {
  110. return (entry->general_bits & ZIP_GENERAL_BITS_IGNORE_LOCAL_HEADER) != 0;
  111. } /* zip_entry_is_traditional_crypto */
  112. static PHYSFS_uint32 zip_crypto_crc32(const PHYSFS_uint32 crc, const PHYSFS_uint8 val)
  113. {
  114. int i;
  115. PHYSFS_uint32 xorval = (crc ^ ((PHYSFS_uint32) val)) & 0xFF;
  116. for (i = 0; i < 8; i++)
  117. xorval = ((xorval & 1) ? (0xEDB88320 ^ (xorval >> 1)) : (xorval >> 1));
  118. return xorval ^ (crc >> 8);
  119. } /* zip_crc32 */
  120. static void zip_update_crypto_keys(PHYSFS_uint32 *keys, const PHYSFS_uint8 val)
  121. {
  122. keys[0] = zip_crypto_crc32(keys[0], val);
  123. keys[1] = keys[1] + (keys[0] & 0x000000FF);
  124. keys[1] = (keys[1] * 134775813) + 1;
  125. keys[2] = zip_crypto_crc32(keys[2], (PHYSFS_uint8) ((keys[1] >> 24) & 0xFF));
  126. } /* zip_update_crypto_keys */
  127. static PHYSFS_uint8 zip_decrypt_byte(const PHYSFS_uint32 *keys)
  128. {
  129. const PHYSFS_uint16 tmp = keys[2] | 2;
  130. return (PHYSFS_uint8) ((tmp * (tmp ^ 1)) >> 8);
  131. } /* zip_decrypt_byte */
  132. static PHYSFS_sint64 zip_read_decrypt(ZIPfileinfo *finfo, void *buf, PHYSFS_uint64 len)
  133. {
  134. PHYSFS_Io *io = finfo->io;
  135. const PHYSFS_sint64 br = io->read(io, buf, len);
  136. /* Decompression the new data if necessary. */
  137. if (zip_entry_is_tradional_crypto(finfo->entry) && (br > 0))
  138. {
  139. PHYSFS_uint32 *keys = finfo->crypto_keys;
  140. PHYSFS_uint8 *ptr = (PHYSFS_uint8 *) buf;
  141. PHYSFS_sint64 i;
  142. for (i = 0; i < br; i++, ptr++)
  143. {
  144. const PHYSFS_uint8 ch = *ptr ^ zip_decrypt_byte(keys);
  145. zip_update_crypto_keys(keys, ch);
  146. *ptr = ch;
  147. } /* for */
  148. } /* if */
  149. return br;
  150. } /* zip_read_decrypt */
  151. static int zip_prep_crypto_keys(ZIPfileinfo *finfo, const PHYSFS_uint8 *crypto_header, const PHYSFS_uint8 *password)
  152. {
  153. /* It doesn't appear to be documented in PKWare's APPNOTE.TXT, but you
  154. need to use a different byte in the header to verify the password
  155. if general purpose bit 3 is set. Discovered this from Info-Zip.
  156. That's what the (verifier) value is doing, below. */
  157. PHYSFS_uint32 *keys = finfo->crypto_keys;
  158. const ZIPentry *entry = finfo->entry;
  159. const int usedate = zip_entry_ignore_local_header(entry);
  160. const PHYSFS_uint8 verifier = (PHYSFS_uint8) ((usedate ? (entry->dos_mod_time >> 8) : (entry->crc >> 24)) & 0xFF);
  161. PHYSFS_uint8 finalbyte = 0;
  162. int i = 0;
  163. /* initialize vector with defaults, then password, then header. */
  164. keys[0] = 305419896;
  165. keys[1] = 591751049;
  166. keys[2] = 878082192;
  167. while (*password)
  168. zip_update_crypto_keys(keys, *(password++));
  169. for (i = 0; i < 12; i++)
  170. {
  171. const PHYSFS_uint8 c = crypto_header[i] ^ zip_decrypt_byte(keys);
  172. zip_update_crypto_keys(keys, c);
  173. finalbyte = c;
  174. } /* for */
  175. /* you have a 1/256 chance of passing this test incorrectly. :/ */
  176. if (finalbyte != verifier)
  177. BAIL(PHYSFS_ERR_BAD_PASSWORD, 0);
  178. /* save the initial vector for seeking purposes. Not secure!! */
  179. memcpy(finfo->initial_crypto_keys, finfo->crypto_keys, 12);
  180. return 1;
  181. } /* zip_prep_crypto_keys */
  182. /*
  183. * Bridge physfs allocation functions to zlib's format...
  184. */
  185. static voidpf zlibPhysfsAlloc(voidpf opaque, uInt items, uInt size)
  186. {
  187. return ((PHYSFS_Allocator *) opaque)->Malloc(items * size);
  188. } /* zlibPhysfsAlloc */
  189. /*
  190. * Bridge physfs allocation functions to zlib's format...
  191. */
  192. static void zlibPhysfsFree(voidpf opaque, voidpf address)
  193. {
  194. ((PHYSFS_Allocator *) opaque)->Free(address);
  195. } /* zlibPhysfsFree */
  196. /*
  197. * Construct a new z_stream to a sane state.
  198. */
  199. static void initializeZStream(z_stream *pstr)
  200. {
  201. memset(pstr, '\0', sizeof (z_stream));
  202. pstr->zalloc = zlibPhysfsAlloc;
  203. pstr->zfree = zlibPhysfsFree;
  204. pstr->opaque = &allocator;
  205. } /* initializeZStream */
  206. static PHYSFS_ErrorCode zlib_error_code(int rc)
  207. {
  208. switch (rc)
  209. {
  210. case Z_OK: return PHYSFS_ERR_OK; /* not an error. */
  211. case Z_STREAM_END: return PHYSFS_ERR_OK; /* not an error. */
  212. case Z_ERRNO: return PHYSFS_ERR_IO;
  213. case Z_MEM_ERROR: return PHYSFS_ERR_OUT_OF_MEMORY;
  214. default: return PHYSFS_ERR_CORRUPT;
  215. } /* switch */
  216. } /* zlib_error_string */
  217. /*
  218. * Wrap all zlib calls in this, so the physfs error state is set appropriately.
  219. */
  220. static int zlib_err(const int rc)
  221. {
  222. PHYSFS_setErrorCode(zlib_error_code(rc));
  223. return rc;
  224. } /* zlib_err */
  225. /*
  226. * Read an unsigned 64-bit int and swap to native byte order.
  227. */
  228. static int readui64(PHYSFS_Io *io, PHYSFS_uint64 *val)
  229. {
  230. PHYSFS_uint64 v;
  231. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &v, sizeof (v)), 0);
  232. *val = PHYSFS_swapULE64(v);
  233. return 1;
  234. } /* readui64 */
  235. /*
  236. * Read an unsigned 32-bit int and swap to native byte order.
  237. */
  238. static int readui32(PHYSFS_Io *io, PHYSFS_uint32 *val)
  239. {
  240. PHYSFS_uint32 v;
  241. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &v, sizeof (v)), 0);
  242. *val = PHYSFS_swapULE32(v);
  243. return 1;
  244. } /* readui32 */
  245. /*
  246. * Read an unsigned 16-bit int and swap to native byte order.
  247. */
  248. static int readui16(PHYSFS_Io *io, PHYSFS_uint16 *val)
  249. {
  250. PHYSFS_uint16 v;
  251. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &v, sizeof (v)), 0);
  252. *val = PHYSFS_swapULE16(v);
  253. return 1;
  254. } /* readui16 */
  255. static PHYSFS_sint64 ZIP_read(PHYSFS_Io *_io, void *buf, PHYSFS_uint64 len)
  256. {
  257. ZIPfileinfo *finfo = (ZIPfileinfo *) _io->opaque;
  258. ZIPentry *entry = finfo->entry;
  259. PHYSFS_sint64 retval = 0;
  260. PHYSFS_sint64 maxread = (PHYSFS_sint64) len;
  261. PHYSFS_sint64 avail = entry->uncompressed_size -
  262. finfo->uncompressed_position;
  263. if (avail < maxread)
  264. maxread = avail;
  265. BAIL_IF_ERRPASS(maxread == 0, 0); /* quick rejection. */
  266. if (entry->compression_method == COMPMETH_NONE)
  267. retval = zip_read_decrypt(finfo, buf, maxread);
  268. else
  269. {
  270. finfo->stream.next_out = buf;
  271. finfo->stream.avail_out = (uInt) maxread;
  272. while (retval < maxread)
  273. {
  274. const PHYSFS_uint32 before = (PHYSFS_uint32) finfo->stream.total_out;
  275. int rc;
  276. if (finfo->stream.avail_in == 0)
  277. {
  278. PHYSFS_sint64 br;
  279. br = entry->compressed_size - finfo->compressed_position;
  280. if (br > 0)
  281. {
  282. if (br > ZIP_READBUFSIZE)
  283. br = ZIP_READBUFSIZE;
  284. br = zip_read_decrypt(finfo, finfo->buffer, (PHYSFS_uint64) br);
  285. if (br <= 0)
  286. break;
  287. finfo->compressed_position += (PHYSFS_uint32) br;
  288. finfo->stream.next_in = finfo->buffer;
  289. finfo->stream.avail_in = (unsigned int) br;
  290. } /* if */
  291. } /* if */
  292. rc = zlib_err(inflate(&finfo->stream, Z_SYNC_FLUSH));
  293. retval += (finfo->stream.total_out - before);
  294. if (rc != Z_OK)
  295. break;
  296. } /* while */
  297. } /* else */
  298. if (retval > 0)
  299. finfo->uncompressed_position += (PHYSFS_uint32) retval;
  300. return retval;
  301. } /* ZIP_read */
  302. static PHYSFS_sint64 ZIP_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
  303. {
  304. BAIL(PHYSFS_ERR_READ_ONLY, -1);
  305. } /* ZIP_write */
  306. static PHYSFS_sint64 ZIP_tell(PHYSFS_Io *io)
  307. {
  308. return ((ZIPfileinfo *) io->opaque)->uncompressed_position;
  309. } /* ZIP_tell */
  310. static int ZIP_seek(PHYSFS_Io *_io, PHYSFS_uint64 offset)
  311. {
  312. ZIPfileinfo *finfo = (ZIPfileinfo *) _io->opaque;
  313. ZIPentry *entry = finfo->entry;
  314. PHYSFS_Io *io = finfo->io;
  315. const int encrypted = zip_entry_is_tradional_crypto(entry);
  316. BAIL_IF(offset > entry->uncompressed_size, PHYSFS_ERR_PAST_EOF, 0);
  317. if (!encrypted && (entry->compression_method == COMPMETH_NONE))
  318. {
  319. PHYSFS_sint64 newpos = offset + entry->offset;
  320. BAIL_IF_ERRPASS(!io->seek(io, newpos), 0);
  321. finfo->uncompressed_position = (PHYSFS_uint32) offset;
  322. } /* if */
  323. else
  324. {
  325. /*
  326. * If seeking backwards, we need to redecode the file
  327. * from the start and throw away the compressed bits until we hit
  328. * the offset we need. If seeking forward, we still need to
  329. * decode, but we don't rewind first.
  330. */
  331. if (offset < finfo->uncompressed_position)
  332. {
  333. /* we do a copy so state is sane if inflateInit2() fails. */
  334. z_stream str;
  335. initializeZStream(&str);
  336. if (zlib_err(inflateInit2(&str, -MAX_WBITS)) != Z_OK)
  337. return 0;
  338. if (!io->seek(io, entry->offset + (encrypted ? 12 : 0)))
  339. return 0;
  340. inflateEnd(&finfo->stream);
  341. memcpy(&finfo->stream, &str, sizeof (z_stream));
  342. finfo->uncompressed_position = finfo->compressed_position = 0;
  343. if (encrypted)
  344. memcpy(finfo->crypto_keys, finfo->initial_crypto_keys, 12);
  345. } /* if */
  346. while (finfo->uncompressed_position != offset)
  347. {
  348. PHYSFS_uint8 buf[512];
  349. PHYSFS_uint32 maxread;
  350. maxread = (PHYSFS_uint32) (offset - finfo->uncompressed_position);
  351. if (maxread > sizeof (buf))
  352. maxread = sizeof (buf);
  353. if (ZIP_read(_io, buf, maxread) != maxread)
  354. return 0;
  355. } /* while */
  356. } /* else */
  357. return 1;
  358. } /* ZIP_seek */
  359. static PHYSFS_sint64 ZIP_length(PHYSFS_Io *io)
  360. {
  361. const ZIPfileinfo *finfo = (ZIPfileinfo *) io->opaque;
  362. return (PHYSFS_sint64) finfo->entry->uncompressed_size;
  363. } /* ZIP_length */
  364. static PHYSFS_Io *zip_get_io(PHYSFS_Io *io, ZIPinfo *inf, ZIPentry *entry);
  365. static PHYSFS_Io *ZIP_duplicate(PHYSFS_Io *io)
  366. {
  367. ZIPfileinfo *origfinfo = (ZIPfileinfo *) io->opaque;
  368. PHYSFS_Io *retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  369. ZIPfileinfo *finfo = (ZIPfileinfo *) allocator.Malloc(sizeof (ZIPfileinfo));
  370. GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, failed);
  371. GOTO_IF(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, failed);
  372. memset(finfo, '\0', sizeof (*finfo));
  373. finfo->entry = origfinfo->entry;
  374. finfo->io = zip_get_io(origfinfo->io, NULL, finfo->entry);
  375. GOTO_IF_ERRPASS(!finfo->io, failed);
  376. initializeZStream(&finfo->stream);
  377. if (finfo->entry->compression_method != COMPMETH_NONE)
  378. {
  379. finfo->buffer = (PHYSFS_uint8 *) allocator.Malloc(ZIP_READBUFSIZE);
  380. GOTO_IF(!finfo->buffer, PHYSFS_ERR_OUT_OF_MEMORY, failed);
  381. if (zlib_err(inflateInit2(&finfo->stream, -MAX_WBITS)) != Z_OK)
  382. goto failed;
  383. } /* if */
  384. memcpy(retval, io, sizeof (PHYSFS_Io));
  385. retval->opaque = finfo;
  386. return retval;
  387. failed:
  388. if (finfo != NULL)
  389. {
  390. if (finfo->io != NULL)
  391. finfo->io->destroy(finfo->io);
  392. if (finfo->buffer != NULL)
  393. {
  394. allocator.Free(finfo->buffer);
  395. inflateEnd(&finfo->stream);
  396. } /* if */
  397. allocator.Free(finfo);
  398. } /* if */
  399. if (retval != NULL)
  400. allocator.Free(retval);
  401. return NULL;
  402. } /* ZIP_duplicate */
  403. static int ZIP_flush(PHYSFS_Io *io) { return 1; /* no write support. */ }
  404. static void ZIP_destroy(PHYSFS_Io *io)
  405. {
  406. ZIPfileinfo *finfo = (ZIPfileinfo *) io->opaque;
  407. finfo->io->destroy(finfo->io);
  408. if (finfo->entry->compression_method != COMPMETH_NONE)
  409. inflateEnd(&finfo->stream);
  410. if (finfo->buffer != NULL)
  411. allocator.Free(finfo->buffer);
  412. allocator.Free(finfo);
  413. allocator.Free(io);
  414. } /* ZIP_destroy */
  415. static const PHYSFS_Io ZIP_Io =
  416. {
  417. CURRENT_PHYSFS_IO_API_VERSION, NULL,
  418. ZIP_read,
  419. ZIP_write,
  420. ZIP_seek,
  421. ZIP_tell,
  422. ZIP_length,
  423. ZIP_duplicate,
  424. ZIP_flush,
  425. ZIP_destroy
  426. };
  427. static PHYSFS_sint64 zip_find_end_of_central_dir(PHYSFS_Io *io, PHYSFS_sint64 *len)
  428. {
  429. PHYSFS_uint8 buf[256];
  430. PHYSFS_uint8 extra[4] = { 0, 0, 0, 0 };
  431. PHYSFS_sint32 i = 0;
  432. PHYSFS_sint64 filelen;
  433. PHYSFS_sint64 filepos;
  434. PHYSFS_sint32 maxread;
  435. PHYSFS_sint32 totalread = 0;
  436. int found = 0;
  437. filelen = io->length(io);
  438. BAIL_IF_ERRPASS(filelen == -1, -1);
  439. /*
  440. * Jump to the end of the file and start reading backwards.
  441. * The last thing in the file is the zipfile comment, which is variable
  442. * length, and the field that specifies its size is before it in the
  443. * file (argh!)...this means that we need to scan backwards until we
  444. * hit the end-of-central-dir signature. We can then sanity check that
  445. * the comment was as big as it should be to make sure we're in the
  446. * right place. The comment length field is 16 bits, so we can stop
  447. * searching for that signature after a little more than 64k at most,
  448. * and call it a corrupted zipfile.
  449. */
  450. if (sizeof (buf) < filelen)
  451. {
  452. filepos = filelen - sizeof (buf);
  453. maxread = sizeof (buf);
  454. } /* if */
  455. else
  456. {
  457. filepos = 0;
  458. maxread = (PHYSFS_uint32) filelen;
  459. } /* else */
  460. while ((totalread < filelen) && (totalread < 65557))
  461. {
  462. BAIL_IF_ERRPASS(!io->seek(io, filepos), -1);
  463. /* make sure we catch a signature between buffers. */
  464. if (totalread != 0)
  465. {
  466. if (!__PHYSFS_readAll(io, buf, maxread - 4))
  467. return -1;
  468. memcpy(&buf[maxread - 4], &extra, sizeof (extra));
  469. totalread += maxread - 4;
  470. } /* if */
  471. else
  472. {
  473. if (!__PHYSFS_readAll(io, buf, maxread))
  474. return -1;
  475. totalread += maxread;
  476. } /* else */
  477. memcpy(&extra, buf, sizeof (extra));
  478. for (i = maxread - 4; i > 0; i--)
  479. {
  480. if ((buf[i + 0] == 0x50) &&
  481. (buf[i + 1] == 0x4B) &&
  482. (buf[i + 2] == 0x05) &&
  483. (buf[i + 3] == 0x06) )
  484. {
  485. found = 1; /* that's the signature! */
  486. break;
  487. } /* if */
  488. } /* for */
  489. if (found)
  490. break;
  491. filepos -= (maxread - 4);
  492. if (filepos < 0)
  493. filepos = 0;
  494. } /* while */
  495. BAIL_IF(!found, PHYSFS_ERR_UNSUPPORTED, -1);
  496. if (len != NULL)
  497. *len = filelen;
  498. return (filepos + i);
  499. } /* zip_find_end_of_central_dir */
  500. static int isZip(PHYSFS_Io *io)
  501. {
  502. PHYSFS_uint32 sig = 0;
  503. int retval = 0;
  504. /*
  505. * The first thing in a zip file might be the signature of the
  506. * first local file record, so it makes for a quick determination.
  507. */
  508. if (readui32(io, &sig))
  509. {
  510. retval = (sig == ZIP_LOCAL_FILE_SIG);
  511. if (!retval)
  512. {
  513. /*
  514. * No sig...might be a ZIP with data at the start
  515. * (a self-extracting executable, etc), so we'll have to do
  516. * it the hard way...
  517. */
  518. retval = (zip_find_end_of_central_dir(io, NULL) != -1);
  519. } /* if */
  520. } /* if */
  521. return retval;
  522. } /* isZip */
  523. /* Convert paths from old, buggy DOS zippers... */
  524. static void zip_convert_dos_path(const PHYSFS_uint16 entryversion, char *path)
  525. {
  526. const PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((entryversion >> 8) & 0xFF);
  527. if (hosttype == 0) /* FS_FAT_ */
  528. {
  529. while (*path)
  530. {
  531. if (*path == '\\')
  532. *path = '/';
  533. path++;
  534. } /* while */
  535. } /* if */
  536. } /* zip_convert_dos_path */
  537. static void zip_expand_symlink_path(char *path)
  538. {
  539. char *ptr = path;
  540. char *prevptr = path;
  541. while (1)
  542. {
  543. ptr = strchr(ptr, '/');
  544. if (ptr == NULL)
  545. break;
  546. if (*(ptr + 1) == '.')
  547. {
  548. if (*(ptr + 2) == '/')
  549. {
  550. /* current dir in middle of string: ditch it. */
  551. memmove(ptr, ptr + 2, strlen(ptr + 2) + 1);
  552. } /* else if */
  553. else if (*(ptr + 2) == '\0')
  554. {
  555. /* current dir at end of string: ditch it. */
  556. *ptr = '\0';
  557. } /* else if */
  558. else if (*(ptr + 2) == '.')
  559. {
  560. if (*(ptr + 3) == '/')
  561. {
  562. /* parent dir in middle: move back one, if possible. */
  563. memmove(prevptr, ptr + 4, strlen(ptr + 4) + 1);
  564. ptr = prevptr;
  565. while (prevptr != path)
  566. {
  567. prevptr--;
  568. if (*prevptr == '/')
  569. {
  570. prevptr++;
  571. break;
  572. } /* if */
  573. } /* while */
  574. } /* if */
  575. if (*(ptr + 3) == '\0')
  576. {
  577. /* parent dir at end: move back one, if possible. */
  578. *prevptr = '\0';
  579. } /* if */
  580. } /* if */
  581. } /* if */
  582. else
  583. {
  584. prevptr = ptr;
  585. ptr++;
  586. } /* else */
  587. } /* while */
  588. } /* zip_expand_symlink_path */
  589. static inline ZIPentry *zip_find_entry(ZIPinfo *info, const char *path)
  590. {
  591. return (ZIPentry *) __PHYSFS_DirTreeFind(&info->tree, path);
  592. } /* zip_find_entry */
  593. /* (forward reference: zip_follow_symlink and zip_resolve call each other.) */
  594. static int zip_resolve(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry);
  595. /*
  596. * Look for the entry named by (path). If it exists, resolve it, and return
  597. * a pointer to that entry. If it's another symlink, keep resolving until you
  598. * hit a real file and then return a pointer to the final non-symlink entry.
  599. * If there's a problem, return NULL.
  600. */
  601. static ZIPentry *zip_follow_symlink(PHYSFS_Io *io, ZIPinfo *info, char *path)
  602. {
  603. ZIPentry *entry;
  604. zip_expand_symlink_path(path);
  605. entry = zip_find_entry(info, path);
  606. if (entry != NULL)
  607. {
  608. if (!zip_resolve(io, info, entry)) /* recursive! */
  609. entry = NULL;
  610. else
  611. {
  612. if (entry->symlink != NULL)
  613. entry = entry->symlink;
  614. } /* else */
  615. } /* if */
  616. return entry;
  617. } /* zip_follow_symlink */
  618. static int zip_resolve_symlink(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry)
  619. {
  620. const size_t size = (size_t) entry->uncompressed_size;
  621. char *path = NULL;
  622. int rc = 0;
  623. /*
  624. * We've already parsed the local file header of the symlink at this
  625. * point. Now we need to read the actual link from the file data and
  626. * follow it.
  627. */
  628. BAIL_IF_ERRPASS(!io->seek(io, entry->offset), 0);
  629. path = (char *) __PHYSFS_smallAlloc(size + 1);
  630. BAIL_IF(!path, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  631. if (entry->compression_method == COMPMETH_NONE)
  632. rc = __PHYSFS_readAll(io, path, size);
  633. else /* symlink target path is compressed... */
  634. {
  635. z_stream stream;
  636. const size_t complen = (size_t) entry->compressed_size;
  637. PHYSFS_uint8 *compressed = (PHYSFS_uint8*) __PHYSFS_smallAlloc(complen);
  638. if (compressed != NULL)
  639. {
  640. if (__PHYSFS_readAll(io, compressed, complen))
  641. {
  642. initializeZStream(&stream);
  643. stream.next_in = compressed;
  644. stream.avail_in = (unsigned int) complen;
  645. stream.next_out = (unsigned char *) path;
  646. stream.avail_out = (unsigned int) size;
  647. if (zlib_err(inflateInit2(&stream, -MAX_WBITS)) == Z_OK)
  648. {
  649. rc = zlib_err(inflate(&stream, Z_FINISH));
  650. inflateEnd(&stream);
  651. /* both are acceptable outcomes... */
  652. rc = ((rc == Z_OK) || (rc == Z_STREAM_END));
  653. } /* if */
  654. } /* if */
  655. __PHYSFS_smallFree(compressed);
  656. } /* if */
  657. } /* else */
  658. if (rc)
  659. {
  660. path[entry->uncompressed_size] = '\0'; /* null-terminate it. */
  661. zip_convert_dos_path(entry->version, path);
  662. entry->symlink = zip_follow_symlink(io, info, path);
  663. } /* else */
  664. __PHYSFS_smallFree(path);
  665. return (entry->symlink != NULL);
  666. } /* zip_resolve_symlink */
  667. /*
  668. * Parse the local file header of an entry, and update entry->offset.
  669. */
  670. static int zip_parse_local(PHYSFS_Io *io, ZIPentry *entry)
  671. {
  672. PHYSFS_uint32 ui32;
  673. PHYSFS_uint16 ui16;
  674. PHYSFS_uint16 fnamelen;
  675. PHYSFS_uint16 extralen;
  676. /*
  677. * crc and (un)compressed_size are always zero if this is a "JAR"
  678. * archive created with Sun's Java tools, apparently. We only
  679. * consider this archive corrupted if those entries don't match and
  680. * aren't zero. That seems to work well.
  681. * We also ignore a mismatch if the value is 0xFFFFFFFF here, since it's
  682. * possible that's a Zip64 thing.
  683. */
  684. /* !!! FIXME: apparently these are zero if general purpose bit 3 is set,
  685. !!! FIXME: which is probably true for Jar files, fwiw, but we don't
  686. !!! FIXME: care about these values anyhow. */
  687. BAIL_IF_ERRPASS(!io->seek(io, entry->offset), 0);
  688. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  689. BAIL_IF(ui32 != ZIP_LOCAL_FILE_SIG, PHYSFS_ERR_CORRUPT, 0);
  690. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  691. BAIL_IF(ui16 != entry->version_needed, PHYSFS_ERR_CORRUPT, 0);
  692. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0); /* general bits. */
  693. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  694. BAIL_IF(ui16 != entry->compression_method, PHYSFS_ERR_CORRUPT, 0);
  695. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0); /* date/time */
  696. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  697. BAIL_IF(ui32 && (ui32 != entry->crc), PHYSFS_ERR_CORRUPT, 0);
  698. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  699. BAIL_IF(ui32 && (ui32 != 0xFFFFFFFF) &&
  700. (ui32 != entry->compressed_size), PHYSFS_ERR_CORRUPT, 0);
  701. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  702. BAIL_IF(ui32 && (ui32 != 0xFFFFFFFF) &&
  703. (ui32 != entry->uncompressed_size), PHYSFS_ERR_CORRUPT, 0);
  704. BAIL_IF_ERRPASS(!readui16(io, &fnamelen), 0);
  705. BAIL_IF_ERRPASS(!readui16(io, &extralen), 0);
  706. entry->offset += fnamelen + extralen + 30;
  707. return 1;
  708. } /* zip_parse_local */
  709. static int zip_resolve(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry)
  710. {
  711. int retval = 1;
  712. const ZipResolveType resolve_type = entry->resolved;
  713. if (resolve_type == ZIP_DIRECTORY)
  714. return 1; /* we're good. */
  715. /* Don't bother if we've failed to resolve this entry before. */
  716. BAIL_IF(resolve_type == ZIP_BROKEN_FILE, PHYSFS_ERR_CORRUPT, 0);
  717. BAIL_IF(resolve_type == ZIP_BROKEN_SYMLINK, PHYSFS_ERR_CORRUPT, 0);
  718. /* uhoh...infinite symlink loop! */
  719. BAIL_IF(resolve_type == ZIP_RESOLVING, PHYSFS_ERR_SYMLINK_LOOP, 0);
  720. /*
  721. * We fix up the offset to point to the actual data on the
  722. * first open, since we don't want to seek across the whole file on
  723. * archive open (can be SLOW on large, CD-stored files), but we
  724. * need to check the local file header...not just for corruption,
  725. * but since it stores offset info the central directory does not.
  726. */
  727. if (resolve_type != ZIP_RESOLVED)
  728. {
  729. if (entry->tree.isdir) /* an ancestor dir that DirTree filled in? */
  730. {
  731. entry->resolved = ZIP_DIRECTORY;
  732. return 1;
  733. } /* if */
  734. retval = zip_parse_local(io, entry);
  735. if (retval)
  736. {
  737. /*
  738. * If it's a symlink, find the original file. This will cause
  739. * resolution of other entries (other symlinks and, eventually,
  740. * the real file) if all goes well.
  741. */
  742. if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
  743. retval = zip_resolve_symlink(io, info, entry);
  744. } /* if */
  745. if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
  746. entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_SYMLINK);
  747. else if (resolve_type == ZIP_UNRESOLVED_FILE)
  748. entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_FILE);
  749. } /* if */
  750. return retval;
  751. } /* zip_resolve */
  752. static int zip_entry_is_symlink(const ZIPentry *entry)
  753. {
  754. return ((entry->resolved == ZIP_UNRESOLVED_SYMLINK) ||
  755. (entry->resolved == ZIP_BROKEN_SYMLINK) ||
  756. (entry->symlink));
  757. } /* zip_entry_is_symlink */
  758. static int zip_version_does_symlinks(PHYSFS_uint32 version)
  759. {
  760. int retval = 0;
  761. PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((version >> 8) & 0xFF);
  762. switch (hosttype)
  763. {
  764. /*
  765. * These are the platforms that can NOT build an archive with
  766. * symlinks, according to the Info-ZIP project.
  767. */
  768. case 0: /* FS_FAT_ */
  769. case 1: /* AMIGA_ */
  770. case 2: /* VMS_ */
  771. case 4: /* VM_CSM_ */
  772. case 6: /* FS_HPFS_ */
  773. case 11: /* FS_NTFS_ */
  774. case 14: /* FS_VFAT_ */
  775. case 13: /* ACORN_ */
  776. case 15: /* MVS_ */
  777. case 18: /* THEOS_ */
  778. break; /* do nothing. */
  779. default: /* assume the rest to be unix-like. */
  780. retval = 1;
  781. break;
  782. } /* switch */
  783. return retval;
  784. } /* zip_version_does_symlinks */
  785. static inline int zip_has_symlink_attr(const ZIPentry *entry,
  786. const PHYSFS_uint32 extern_attr)
  787. {
  788. PHYSFS_uint16 xattr = ((extern_attr >> 16) & 0xFFFF);
  789. return ( (zip_version_does_symlinks(entry->version)) &&
  790. (entry->uncompressed_size > 0) &&
  791. ((xattr & UNIX_FILETYPE_MASK) == UNIX_FILETYPE_SYMLINK) );
  792. } /* zip_has_symlink_attr */
  793. static PHYSFS_sint64 zip_dos_time_to_physfs_time(PHYSFS_uint32 dostime)
  794. {
  795. PHYSFS_uint32 dosdate;
  796. struct tm unixtime;
  797. memset(&unixtime, '\0', sizeof (unixtime));
  798. dosdate = (PHYSFS_uint32) ((dostime >> 16) & 0xFFFF);
  799. dostime &= 0xFFFF;
  800. /* dissect date */
  801. unixtime.tm_year = ((dosdate >> 9) & 0x7F) + 80;
  802. unixtime.tm_mon = ((dosdate >> 5) & 0x0F) - 1;
  803. unixtime.tm_mday = ((dosdate ) & 0x1F);
  804. /* dissect time */
  805. unixtime.tm_hour = ((dostime >> 11) & 0x1F);
  806. unixtime.tm_min = ((dostime >> 5) & 0x3F);
  807. unixtime.tm_sec = ((dostime << 1) & 0x3E);
  808. /* let mktime calculate daylight savings time. */
  809. unixtime.tm_isdst = -1;
  810. return ((PHYSFS_sint64) mktime(&unixtime));
  811. } /* zip_dos_time_to_physfs_time */
  812. static ZIPentry *zip_load_entry(ZIPinfo *info, const int zip64,
  813. const PHYSFS_uint64 ofs_fixup)
  814. {
  815. PHYSFS_Io *io = info->io;
  816. ZIPentry entry;
  817. ZIPentry *retval = NULL;
  818. PHYSFS_uint16 fnamelen, extralen, commentlen;
  819. PHYSFS_uint32 external_attr;
  820. PHYSFS_uint32 starting_disk;
  821. PHYSFS_uint64 offset;
  822. PHYSFS_uint16 ui16;
  823. PHYSFS_uint32 ui32;
  824. PHYSFS_sint64 si64;
  825. char *name = NULL;
  826. int isdir = 0;
  827. /* sanity check with central directory signature... */
  828. BAIL_IF_ERRPASS(!readui32(io, &ui32), NULL);
  829. BAIL_IF(ui32 != ZIP_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, NULL);
  830. memset(&entry, '\0', sizeof (entry));
  831. /* Get the pertinent parts of the record... */
  832. BAIL_IF_ERRPASS(!readui16(io, &entry.version), NULL);
  833. BAIL_IF_ERRPASS(!readui16(io, &entry.version_needed), NULL);
  834. BAIL_IF_ERRPASS(!readui16(io, &entry.general_bits), NULL); /* general bits */
  835. BAIL_IF_ERRPASS(!readui16(io, &entry.compression_method), NULL);
  836. BAIL_IF_ERRPASS(!readui32(io, &entry.dos_mod_time), NULL);
  837. entry.last_mod_time = zip_dos_time_to_physfs_time(entry.dos_mod_time);
  838. BAIL_IF_ERRPASS(!readui32(io, &entry.crc), NULL);
  839. BAIL_IF_ERRPASS(!readui32(io, &ui32), NULL);
  840. entry.compressed_size = (PHYSFS_uint64) ui32;
  841. BAIL_IF_ERRPASS(!readui32(io, &ui32), NULL);
  842. entry.uncompressed_size = (PHYSFS_uint64) ui32;
  843. BAIL_IF_ERRPASS(!readui16(io, &fnamelen), NULL);
  844. BAIL_IF_ERRPASS(!readui16(io, &extralen), NULL);
  845. BAIL_IF_ERRPASS(!readui16(io, &commentlen), NULL);
  846. BAIL_IF_ERRPASS(!readui16(io, &ui16), NULL);
  847. starting_disk = (PHYSFS_uint32) ui16;
  848. BAIL_IF_ERRPASS(!readui16(io, &ui16), NULL); /* internal file attribs */
  849. BAIL_IF_ERRPASS(!readui32(io, &external_attr), NULL);
  850. BAIL_IF_ERRPASS(!readui32(io, &ui32), NULL);
  851. offset = (PHYSFS_uint64) ui32;
  852. name = (char *) __PHYSFS_smallAlloc(fnamelen + 1);
  853. BAIL_IF(!name, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  854. if (!__PHYSFS_readAll(io, name, fnamelen))
  855. {
  856. __PHYSFS_smallFree(name);
  857. return NULL;
  858. } /* if */
  859. if (name[fnamelen - 1] == '/')
  860. {
  861. name[fnamelen - 1] = '\0';
  862. isdir = 1;
  863. } /* if */
  864. name[fnamelen] = '\0'; /* null-terminate the filename. */
  865. zip_convert_dos_path(entry.version, name);
  866. retval = (ZIPentry *) __PHYSFS_DirTreeAdd(&info->tree, name, isdir);
  867. __PHYSFS_smallFree(name);
  868. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  869. /* It's okay to BAIL without freeing retval, because it's stored in the
  870. __PHYSFS_DirTree and will be freed later anyhow. */
  871. BAIL_IF(retval->last_mod_time != 0, PHYSFS_ERR_CORRUPT, NULL); /* dupe? */
  872. /* Move the data we already read into place in the official object. */
  873. memcpy(((PHYSFS_uint8 *) retval) + sizeof (__PHYSFS_DirTreeEntry),
  874. ((PHYSFS_uint8 *) &entry) + sizeof (__PHYSFS_DirTreeEntry),
  875. sizeof (*retval) - sizeof (__PHYSFS_DirTreeEntry));
  876. retval->symlink = NULL; /* will be resolved later, if necessary. */
  877. if (isdir)
  878. retval->resolved = ZIP_DIRECTORY;
  879. else
  880. {
  881. retval->resolved = (zip_has_symlink_attr(retval, external_attr)) ?
  882. ZIP_UNRESOLVED_SYMLINK : ZIP_UNRESOLVED_FILE;
  883. } /* else */
  884. si64 = io->tell(io);
  885. BAIL_IF_ERRPASS(si64 == -1, NULL);
  886. /* If the actual sizes didn't fit in 32-bits, look for the Zip64
  887. extended information extra field... */
  888. if ( (zip64) &&
  889. ((offset == 0xFFFFFFFF) ||
  890. (starting_disk == 0xFFFFFFFF) ||
  891. (retval->compressed_size == 0xFFFFFFFF) ||
  892. (retval->uncompressed_size == 0xFFFFFFFF)) )
  893. {
  894. int found = 0;
  895. PHYSFS_uint16 sig = 0;
  896. PHYSFS_uint16 len = 0;
  897. while (extralen > 4)
  898. {
  899. BAIL_IF_ERRPASS(!readui16(io, &sig), NULL);
  900. BAIL_IF_ERRPASS(!readui16(io, &len), NULL);
  901. si64 += 4 + len;
  902. extralen -= 4 + len;
  903. if (sig != ZIP64_EXTENDED_INFO_EXTRA_FIELD_SIG)
  904. {
  905. BAIL_IF_ERRPASS(!io->seek(io, si64), NULL);
  906. continue;
  907. } /* if */
  908. found = 1;
  909. break;
  910. } /* while */
  911. BAIL_IF(!found, PHYSFS_ERR_CORRUPT, NULL);
  912. if (retval->uncompressed_size == 0xFFFFFFFF)
  913. {
  914. BAIL_IF(len < 8, PHYSFS_ERR_CORRUPT, NULL);
  915. BAIL_IF_ERRPASS(!readui64(io, &retval->uncompressed_size), NULL);
  916. len -= 8;
  917. } /* if */
  918. if (retval->compressed_size == 0xFFFFFFFF)
  919. {
  920. BAIL_IF(len < 8, PHYSFS_ERR_CORRUPT, NULL);
  921. BAIL_IF_ERRPASS(!readui64(io, &retval->compressed_size), NULL);
  922. len -= 8;
  923. } /* if */
  924. if (offset == 0xFFFFFFFF)
  925. {
  926. BAIL_IF(len < 8, PHYSFS_ERR_CORRUPT, NULL);
  927. BAIL_IF_ERRPASS(!readui64(io, &offset), NULL);
  928. len -= 8;
  929. } /* if */
  930. if (starting_disk == 0xFFFFFFFF)
  931. {
  932. BAIL_IF(len < 8, PHYSFS_ERR_CORRUPT, NULL);
  933. BAIL_IF_ERRPASS(!readui32(io, &starting_disk), NULL);
  934. len -= 4;
  935. } /* if */
  936. BAIL_IF(len != 0, PHYSFS_ERR_CORRUPT, NULL);
  937. } /* if */
  938. BAIL_IF(starting_disk != 0, PHYSFS_ERR_CORRUPT, NULL);
  939. retval->offset = offset + ofs_fixup;
  940. /* seek to the start of the next entry in the central directory... */
  941. BAIL_IF_ERRPASS(!io->seek(io, si64 + extralen + commentlen), NULL);
  942. return retval; /* success. */
  943. } /* zip_load_entry */
  944. /* This leaves things allocated on error; the caller will clean up the mess. */
  945. static int zip_load_entries(ZIPinfo *info,
  946. const PHYSFS_uint64 data_ofs,
  947. const PHYSFS_uint64 central_ofs,
  948. const PHYSFS_uint64 entry_count)
  949. {
  950. PHYSFS_Io *io = info->io;
  951. const int zip64 = info->zip64;
  952. PHYSFS_uint64 i;
  953. BAIL_IF_ERRPASS(!io->seek(io, central_ofs), 0);
  954. for (i = 0; i < entry_count; i++)
  955. {
  956. ZIPentry *entry = zip_load_entry(info, zip64, data_ofs);
  957. BAIL_IF_ERRPASS(!entry, 0);
  958. if (zip_entry_is_tradional_crypto(entry))
  959. info->has_crypto = 1;
  960. } /* for */
  961. return 1;
  962. } /* zip_load_entries */
  963. static PHYSFS_sint64 zip64_find_end_of_central_dir(PHYSFS_Io *io,
  964. PHYSFS_sint64 _pos,
  965. PHYSFS_uint64 offset)
  966. {
  967. /*
  968. * Naturally, the offset is useless to us; it is the offset from the
  969. * start of file, which is meaningless if we've appended this .zip to
  970. * a self-extracting .exe. We need to find this on our own. It should
  971. * be directly before the locator record, but the record in question,
  972. * like the original end-of-central-directory record, ends with a
  973. * variable-length field. Unlike the original, which has to store the
  974. * size of that variable-length field in a 16-bit int and thus has to be
  975. * within 64k, the new one gets 64-bits.
  976. *
  977. * Fortunately, the only currently-specified record for that variable
  978. * length block is some weird proprietary thing that deals with EBCDIC
  979. * and tape backups or something. So we don't seek far.
  980. */
  981. PHYSFS_uint32 ui32;
  982. const PHYSFS_uint64 pos = (PHYSFS_uint64) _pos;
  983. assert(_pos > 0);
  984. /* Try offset specified in the Zip64 end of central directory locator. */
  985. /* This works if the entire PHYSFS_Io is the zip file. */
  986. BAIL_IF_ERRPASS(!io->seek(io, offset), -1);
  987. BAIL_IF_ERRPASS(!readui32(io, &ui32), -1);
  988. if (ui32 == ZIP64_END_OF_CENTRAL_DIR_SIG)
  989. return offset;
  990. /* Try 56 bytes before the Zip64 end of central directory locator. */
  991. /* This works if the record isn't variable length and is version 1. */
  992. if (pos > 56)
  993. {
  994. BAIL_IF_ERRPASS(!io->seek(io, pos-56), -1);
  995. BAIL_IF_ERRPASS(!readui32(io, &ui32), -1);
  996. if (ui32 == ZIP64_END_OF_CENTRAL_DIR_SIG)
  997. return pos-56;
  998. } /* if */
  999. /* Try 84 bytes before the Zip64 end of central directory locator. */
  1000. /* This works if the record isn't variable length and is version 2. */
  1001. if (pos > 84)
  1002. {
  1003. BAIL_IF_ERRPASS(!io->seek(io, pos-84), -1);
  1004. BAIL_IF_ERRPASS(!readui32(io, &ui32), -1);
  1005. if (ui32 == ZIP64_END_OF_CENTRAL_DIR_SIG)
  1006. return pos-84;
  1007. } /* if */
  1008. /* Ok, brute force: we know it's between (offset) and (pos) somewhere. */
  1009. /* Just try moving back at most 256k. Oh well. */
  1010. if ((offset < pos) && (pos > 4))
  1011. {
  1012. const size_t maxbuflen = 256 * 1024;
  1013. size_t len = (size_t) (pos - offset);
  1014. PHYSFS_uint8 *buf = NULL;
  1015. PHYSFS_sint32 i;
  1016. if (len > maxbuflen)
  1017. len = maxbuflen;
  1018. buf = (PHYSFS_uint8 *) __PHYSFS_smallAlloc(len);
  1019. BAIL_IF(!buf, PHYSFS_ERR_OUT_OF_MEMORY, -1);
  1020. if (!io->seek(io, pos - len) || !__PHYSFS_readAll(io, buf, len))
  1021. {
  1022. __PHYSFS_smallFree(buf);
  1023. return -1; /* error was set elsewhere. */
  1024. } /* if */
  1025. for (i = (PHYSFS_sint32) (len - 4); i >= 0; i--)
  1026. {
  1027. if ( (buf[i] == 0x50) && (buf[i+1] == 0x4b) &&
  1028. (buf[i+2] == 0x06) && (buf[i+3] == 0x06) )
  1029. {
  1030. __PHYSFS_smallFree(buf);
  1031. return pos - ((PHYSFS_sint64) (len - i));
  1032. } /* if */
  1033. } /* for */
  1034. __PHYSFS_smallFree(buf);
  1035. } /* if */
  1036. BAIL(PHYSFS_ERR_CORRUPT, -1); /* didn't find it. */
  1037. } /* zip64_find_end_of_central_dir */
  1038. static int zip64_parse_end_of_central_dir(ZIPinfo *info,
  1039. PHYSFS_uint64 *data_start,
  1040. PHYSFS_uint64 *dir_ofs,
  1041. PHYSFS_uint64 *entry_count,
  1042. PHYSFS_sint64 pos)
  1043. {
  1044. PHYSFS_Io *io = info->io;
  1045. PHYSFS_uint64 ui64;
  1046. PHYSFS_uint32 ui32;
  1047. PHYSFS_uint16 ui16;
  1048. /* We should be positioned right past the locator signature. */
  1049. if ((pos < 0) || (!io->seek(io, pos)))
  1050. return 0;
  1051. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1052. if (ui32 != ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIG)
  1053. return -1; /* it's not a Zip64 archive. Not an error, though! */
  1054. info->zip64 = 1;
  1055. /* number of the disk with the start of the central directory. */
  1056. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1057. BAIL_IF(ui32 != 0, PHYSFS_ERR_CORRUPT, 0);
  1058. /* offset of Zip64 end of central directory record. */
  1059. BAIL_IF_ERRPASS(!readui64(io, &ui64), 0);
  1060. /* total number of disks */
  1061. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1062. BAIL_IF(ui32 != 1, PHYSFS_ERR_CORRUPT, 0);
  1063. pos = zip64_find_end_of_central_dir(io, pos, ui64);
  1064. if (pos < 0)
  1065. return 0; /* oh well. */
  1066. /*
  1067. * For self-extracting archives, etc, there's crapola in the file
  1068. * before the zipfile records; we calculate how much data there is
  1069. * prepended by determining how far the zip64-end-of-central-directory
  1070. * offset is from where it is supposed to be...the difference in bytes
  1071. * is how much arbitrary data is at the start of the physical file.
  1072. */
  1073. assert(((PHYSFS_uint64) pos) >= ui64);
  1074. *data_start = ((PHYSFS_uint64) pos) - ui64;
  1075. BAIL_IF_ERRPASS(!io->seek(io, pos), 0);
  1076. /* check signature again, just in case. */
  1077. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1078. BAIL_IF(ui32 != ZIP64_END_OF_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, 0);
  1079. /* size of Zip64 end of central directory record. */
  1080. BAIL_IF_ERRPASS(!readui64(io, &ui64), 0);
  1081. /* version made by. */
  1082. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  1083. /* version needed to extract. */
  1084. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  1085. /* number of this disk. */
  1086. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1087. BAIL_IF(ui32 != 0, PHYSFS_ERR_CORRUPT, 0);
  1088. /* number of disk with start of central directory record. */
  1089. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1090. BAIL_IF(ui32 != 0, PHYSFS_ERR_CORRUPT, 0);
  1091. /* total number of entries in the central dir on this disk */
  1092. BAIL_IF_ERRPASS(!readui64(io, &ui64), 0);
  1093. /* total number of entries in the central dir */
  1094. BAIL_IF_ERRPASS(!readui64(io, entry_count), 0);
  1095. BAIL_IF(ui64 != *entry_count, PHYSFS_ERR_CORRUPT, 0);
  1096. /* size of the central directory */
  1097. BAIL_IF_ERRPASS(!readui64(io, &ui64), 0);
  1098. /* offset of central directory */
  1099. BAIL_IF_ERRPASS(!readui64(io, dir_ofs), 0);
  1100. /* Since we know the difference, fix up the central dir offset... */
  1101. *dir_ofs += *data_start;
  1102. /*
  1103. * There are more fields here, for encryption and feature-specific things,
  1104. * but we don't care about any of them at the moment.
  1105. */
  1106. return 1; /* made it. */
  1107. } /* zip64_parse_end_of_central_dir */
  1108. static int zip_parse_end_of_central_dir(ZIPinfo *info,
  1109. PHYSFS_uint64 *data_start,
  1110. PHYSFS_uint64 *dir_ofs,
  1111. PHYSFS_uint64 *entry_count)
  1112. {
  1113. PHYSFS_Io *io = info->io;
  1114. PHYSFS_uint16 entryCount16;
  1115. PHYSFS_uint32 offset32;
  1116. PHYSFS_uint32 ui32;
  1117. PHYSFS_uint16 ui16;
  1118. PHYSFS_sint64 len;
  1119. PHYSFS_sint64 pos;
  1120. int rc;
  1121. /* find the end-of-central-dir record, and seek to it. */
  1122. pos = zip_find_end_of_central_dir(io, &len);
  1123. BAIL_IF_ERRPASS(pos == -1, 0);
  1124. BAIL_IF_ERRPASS(!io->seek(io, pos), 0);
  1125. /* check signature again, just in case. */
  1126. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1127. BAIL_IF(ui32 != ZIP_END_OF_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, 0);
  1128. /* Seek back to see if "Zip64 end of central directory locator" exists. */
  1129. /* this record is 20 bytes before end-of-central-dir */
  1130. rc = zip64_parse_end_of_central_dir(info, data_start, dir_ofs,
  1131. entry_count, pos - 20);
  1132. /* Error or success? Bounce out of here. Keep going if not zip64. */
  1133. if ((rc == 0) || (rc == 1))
  1134. return rc;
  1135. assert(rc == -1); /* no error, just not a Zip64 archive. */
  1136. /* Not Zip64? Seek back to where we were and keep processing. */
  1137. BAIL_IF_ERRPASS(!io->seek(io, pos + 4), 0);
  1138. /* number of this disk */
  1139. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  1140. BAIL_IF(ui16 != 0, PHYSFS_ERR_CORRUPT, 0);
  1141. /* number of the disk with the start of the central directory */
  1142. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  1143. BAIL_IF(ui16 != 0, PHYSFS_ERR_CORRUPT, 0);
  1144. /* total number of entries in the central dir on this disk */
  1145. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  1146. /* total number of entries in the central dir */
  1147. BAIL_IF_ERRPASS(!readui16(io, &entryCount16), 0);
  1148. BAIL_IF(ui16 != entryCount16, PHYSFS_ERR_CORRUPT, 0);
  1149. *entry_count = entryCount16;
  1150. /* size of the central directory */
  1151. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1152. /* offset of central directory */
  1153. BAIL_IF_ERRPASS(!readui32(io, &offset32), 0);
  1154. *dir_ofs = (PHYSFS_uint64) offset32;
  1155. BAIL_IF(((PHYSFS_uint64) pos) < (*dir_ofs + ui32), PHYSFS_ERR_CORRUPT, 0);
  1156. /*
  1157. * For self-extracting archives, etc, there's crapola in the file
  1158. * before the zipfile records; we calculate how much data there is
  1159. * prepended by determining how far the central directory offset is
  1160. * from where it is supposed to be (start of end-of-central-dir minus
  1161. * sizeof central dir)...the difference in bytes is how much arbitrary
  1162. * data is at the start of the physical file.
  1163. */
  1164. *data_start = (PHYSFS_uint64) (pos - (*dir_ofs + ui32));
  1165. /* Now that we know the difference, fix up the central dir offset... */
  1166. *dir_ofs += *data_start;
  1167. /* zipfile comment length */
  1168. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  1169. /*
  1170. * Make sure that the comment length matches to the end of file...
  1171. * If it doesn't, we're either in the wrong part of the file, or the
  1172. * file is corrupted, but we give up either way.
  1173. */
  1174. BAIL_IF((pos + 22 + ui16) != len, PHYSFS_ERR_CORRUPT, 0);
  1175. return 1; /* made it. */
  1176. } /* zip_parse_end_of_central_dir */
  1177. static void ZIP_closeArchive(void *opaque)
  1178. {
  1179. ZIPinfo *info = (ZIPinfo *) (opaque);
  1180. if (!info)
  1181. return;
  1182. if (info->io)
  1183. info->io->destroy(info->io);
  1184. __PHYSFS_DirTreeDeinit(&info->tree);
  1185. allocator.Free(info);
  1186. } /* ZIP_closeArchive */
  1187. static void *ZIP_openArchive(PHYSFS_Io *io, const char *name,
  1188. int forWriting, int *claimed)
  1189. {
  1190. ZIPinfo *info = NULL;
  1191. ZIPentry *root = NULL;
  1192. PHYSFS_uint64 dstart = 0; /* data start */
  1193. PHYSFS_uint64 cdir_ofs; /* central dir offset */
  1194. PHYSFS_uint64 count;
  1195. assert(io != NULL); /* shouldn't ever happen. */
  1196. BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
  1197. BAIL_IF_ERRPASS(!isZip(io), NULL);
  1198. *claimed = 1;
  1199. info = (ZIPinfo *) allocator.Malloc(sizeof (ZIPinfo));
  1200. BAIL_IF(!info, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  1201. memset(info, '\0', sizeof (ZIPinfo));
  1202. info->io = io;
  1203. if (!zip_parse_end_of_central_dir(info, &dstart, &cdir_ofs, &count))
  1204. goto ZIP_openarchive_failed;
  1205. else if (!__PHYSFS_DirTreeInit(&info->tree, sizeof (ZIPentry)))
  1206. goto ZIP_openarchive_failed;
  1207. root = (ZIPentry *) info->tree.root;
  1208. root->resolved = ZIP_DIRECTORY;
  1209. if (!zip_load_entries(info, dstart, cdir_ofs, count))
  1210. goto ZIP_openarchive_failed;
  1211. assert(info->tree.root->sibling == NULL);
  1212. return info;
  1213. ZIP_openarchive_failed:
  1214. info->io = NULL; /* don't let ZIP_closeArchive destroy (io). */
  1215. ZIP_closeArchive(info);
  1216. return NULL;
  1217. } /* ZIP_openArchive */
  1218. static PHYSFS_Io *zip_get_io(PHYSFS_Io *io, ZIPinfo *inf, ZIPentry *entry)
  1219. {
  1220. int success;
  1221. PHYSFS_Io *retval = io->duplicate(io);
  1222. BAIL_IF_ERRPASS(!retval, NULL);
  1223. assert(!entry->tree.isdir); /* should have been checked before calling. */
  1224. /* (inf) can be NULL if we already resolved. */
  1225. success = (inf == NULL) || zip_resolve(retval, inf, entry);
  1226. if (success)
  1227. {
  1228. PHYSFS_sint64 offset;
  1229. offset = ((entry->symlink) ? entry->symlink->offset : entry->offset);
  1230. success = retval->seek(retval, offset);
  1231. } /* if */
  1232. if (!success)
  1233. {
  1234. retval->destroy(retval);
  1235. retval = NULL;
  1236. } /* if */
  1237. return retval;
  1238. } /* zip_get_io */
  1239. static PHYSFS_Io *ZIP_openRead(void *opaque, const char *filename)
  1240. {
  1241. PHYSFS_Io *retval = NULL;
  1242. ZIPinfo *info = (ZIPinfo *) opaque;
  1243. ZIPentry *entry = zip_find_entry(info, filename);
  1244. ZIPfileinfo *finfo = NULL;
  1245. PHYSFS_Io *io = NULL;
  1246. PHYSFS_uint8 *password = NULL;
  1247. /* if not found, see if maybe "$PASSWORD" is appended. */
  1248. if ((!entry) && (info->has_crypto))
  1249. {
  1250. const char *ptr = strrchr(filename, '$');
  1251. if (ptr != NULL)
  1252. {
  1253. const size_t len = (size_t) (ptr - filename);
  1254. char *str = (char *) __PHYSFS_smallAlloc(len + 1);
  1255. BAIL_IF(!str, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  1256. memcpy(str, filename, len);
  1257. str[len] = '\0';
  1258. entry = zip_find_entry(info, str);
  1259. __PHYSFS_smallFree(str);
  1260. password = (PHYSFS_uint8 *) (ptr + 1);
  1261. } /* if */
  1262. } /* if */
  1263. BAIL_IF_ERRPASS(!entry, NULL);
  1264. BAIL_IF_ERRPASS(!zip_resolve(info->io, info, entry), NULL);
  1265. BAIL_IF(entry->tree.isdir, PHYSFS_ERR_NOT_A_FILE, NULL);
  1266. retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  1267. GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed);
  1268. finfo = (ZIPfileinfo *) allocator.Malloc(sizeof (ZIPfileinfo));
  1269. GOTO_IF(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed);
  1270. memset(finfo, '\0', sizeof (ZIPfileinfo));
  1271. io = zip_get_io(info->io, info, entry);
  1272. GOTO_IF_ERRPASS(!io, ZIP_openRead_failed);
  1273. finfo->io = io;
  1274. finfo->entry = ((entry->symlink != NULL) ? entry->symlink : entry);
  1275. initializeZStream(&finfo->stream);
  1276. if (finfo->entry->compression_method != COMPMETH_NONE)
  1277. {
  1278. finfo->buffer = (PHYSFS_uint8 *) allocator.Malloc(ZIP_READBUFSIZE);
  1279. if (!finfo->buffer)
  1280. GOTO(PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed);
  1281. else if (zlib_err(inflateInit2(&finfo->stream, -MAX_WBITS)) != Z_OK)
  1282. goto ZIP_openRead_failed;
  1283. } /* if */
  1284. if (!zip_entry_is_tradional_crypto(entry))
  1285. GOTO_IF(password != NULL, PHYSFS_ERR_BAD_PASSWORD, ZIP_openRead_failed);
  1286. else
  1287. {
  1288. PHYSFS_uint8 crypto_header[12];
  1289. GOTO_IF(password == NULL, PHYSFS_ERR_BAD_PASSWORD, ZIP_openRead_failed);
  1290. if (io->read(io, crypto_header, 12) != 12)
  1291. goto ZIP_openRead_failed;
  1292. else if (!zip_prep_crypto_keys(finfo, crypto_header, password))
  1293. goto ZIP_openRead_failed;
  1294. } /* if */
  1295. memcpy(retval, &ZIP_Io, sizeof (PHYSFS_Io));
  1296. retval->opaque = finfo;
  1297. return retval;
  1298. ZIP_openRead_failed:
  1299. if (finfo != NULL)
  1300. {
  1301. if (finfo->io != NULL)
  1302. finfo->io->destroy(finfo->io);
  1303. if (finfo->buffer != NULL)
  1304. {
  1305. allocator.Free(finfo->buffer);
  1306. inflateEnd(&finfo->stream);
  1307. } /* if */
  1308. allocator.Free(finfo);
  1309. } /* if */
  1310. if (retval != NULL)
  1311. allocator.Free(retval);
  1312. return NULL;
  1313. } /* ZIP_openRead */
  1314. static PHYSFS_Io *ZIP_openWrite(void *opaque, const char *filename)
  1315. {
  1316. BAIL(PHYSFS_ERR_READ_ONLY, NULL);
  1317. } /* ZIP_openWrite */
  1318. static PHYSFS_Io *ZIP_openAppend(void *opaque, const char *filename)
  1319. {
  1320. BAIL(PHYSFS_ERR_READ_ONLY, NULL);
  1321. } /* ZIP_openAppend */
  1322. static int ZIP_remove(void *opaque, const char *name)
  1323. {
  1324. BAIL(PHYSFS_ERR_READ_ONLY, 0);
  1325. } /* ZIP_remove */
  1326. static int ZIP_mkdir(void *opaque, const char *name)
  1327. {
  1328. BAIL(PHYSFS_ERR_READ_ONLY, 0);
  1329. } /* ZIP_mkdir */
  1330. static int ZIP_stat(void *opaque, const char *filename, PHYSFS_Stat *stat)
  1331. {
  1332. ZIPinfo *info = (ZIPinfo *) opaque;
  1333. ZIPentry *entry = zip_find_entry(info, filename);
  1334. if (entry == NULL)
  1335. return 0;
  1336. else if (!zip_resolve(info->io, info, entry))
  1337. return 0;
  1338. else if (entry->resolved == ZIP_DIRECTORY)
  1339. {
  1340. stat->filesize = 0;
  1341. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  1342. } /* if */
  1343. else if (zip_entry_is_symlink(entry))
  1344. {
  1345. stat->filesize = 0;
  1346. stat->filetype = PHYSFS_FILETYPE_SYMLINK;
  1347. } /* else if */
  1348. else
  1349. {
  1350. stat->filesize = (PHYSFS_sint64) entry->uncompressed_size;
  1351. stat->filetype = PHYSFS_FILETYPE_REGULAR;
  1352. } /* else */
  1353. stat->modtime = ((entry) ? entry->last_mod_time : 0);
  1354. stat->createtime = stat->modtime;
  1355. stat->accesstime = -1;
  1356. stat->readonly = 1; /* .zip files are always read only */
  1357. return 1;
  1358. } /* ZIP_stat */
  1359. const PHYSFS_Archiver __PHYSFS_Archiver_ZIP =
  1360. {
  1361. CURRENT_PHYSFS_ARCHIVER_API_VERSION,
  1362. {
  1363. "ZIP",
  1364. "PkZip/WinZip/Info-Zip compatible",
  1365. "Ryan C. Gordon <icculus@icculus.org>",
  1366. "https://icculus.org/physfs/",
  1367. 1, /* supportsSymlinks */
  1368. },
  1369. ZIP_openArchive,
  1370. __PHYSFS_DirTreeEnumerate,
  1371. ZIP_openRead,
  1372. ZIP_openWrite,
  1373. ZIP_openAppend,
  1374. ZIP_remove,
  1375. ZIP_mkdir,
  1376. ZIP_stat,
  1377. ZIP_closeArchive
  1378. };
  1379. #endif /* defined PHYSFS_SUPPORTS_ZIP */
  1380. /* end of physfs_archiver_zip.c ... */