physfs_archiver_zip.c 54 KB

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