physfs_archiver_zip.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  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. #if (PHYSFS_BYTEORDER == PHYSFS_LIL_ENDIAN)
  15. #define MINIZ_LITTLE_ENDIAN 1
  16. #else
  17. #define MINIZ_LITTLE_ENDIAN 0
  18. #endif
  19. #include "physfs_miniz.h"
  20. /*
  21. * A buffer of ZIP_READBUFSIZE is allocated for each compressed file opened,
  22. * and is freed when you close the file; compressed data is read into
  23. * this buffer, and then is decompressed into the buffer passed to
  24. * PHYSFS_read().
  25. *
  26. * Uncompressed entries in a zipfile do not allocate this buffer; they just
  27. * read data directly into the buffer passed to PHYSFS_read().
  28. *
  29. * Depending on your speed and memory requirements, you should tweak this
  30. * value.
  31. */
  32. #define ZIP_READBUFSIZE (16 * 1024)
  33. /*
  34. * Entries are "unresolved" until they are first opened. At that time,
  35. * local file headers parsed/validated, data offsets will be updated to look
  36. * at the actual file data instead of the header, and symlinks will be
  37. * followed and optimized. This means that we don't seek and read around the
  38. * archive until forced to do so, and after the first time, we had to do
  39. * less reading and parsing, which is very CD-ROM friendly.
  40. */
  41. typedef enum
  42. {
  43. ZIP_UNRESOLVED_FILE,
  44. ZIP_UNRESOLVED_SYMLINK,
  45. ZIP_RESOLVING,
  46. ZIP_RESOLVED,
  47. ZIP_DIRECTORY,
  48. ZIP_BROKEN_FILE,
  49. ZIP_BROKEN_SYMLINK
  50. } ZipResolveType;
  51. /*
  52. * One ZIPentry is kept for each file in an open ZIP archive.
  53. */
  54. typedef struct _ZIPentry
  55. {
  56. __PHYSFS_DirTreeEntry tree; /* manages directory tree */
  57. struct _ZIPentry *symlink; /* NULL or file we symlink to */
  58. ZipResolveType resolved; /* Have we resolved file/symlink? */
  59. PHYSFS_uint64 offset; /* offset of data in archive */
  60. PHYSFS_uint16 version; /* version made by */
  61. PHYSFS_uint16 version_needed; /* version needed to extract */
  62. PHYSFS_uint16 general_bits; /* general purpose bits */
  63. PHYSFS_uint16 compression_method; /* compression method */
  64. PHYSFS_uint32 crc; /* crc-32 */
  65. PHYSFS_uint64 compressed_size; /* compressed size */
  66. PHYSFS_uint64 uncompressed_size; /* uncompressed size */
  67. PHYSFS_sint64 last_mod_time; /* last file mod time */
  68. PHYSFS_uint32 dos_mod_time; /* original MS-DOS style mod time */
  69. } ZIPentry;
  70. /*
  71. * One ZIPinfo is kept for each open ZIP archive.
  72. */
  73. typedef struct
  74. {
  75. __PHYSFS_DirTree tree; /* manages directory tree. */
  76. PHYSFS_Io *io; /* the i/o interface for this archive. */
  77. int zip64; /* non-zero if this is a Zip64 archive. */
  78. int has_crypto; /* non-zero if any entry uses encryption. */
  79. } ZIPinfo;
  80. /*
  81. * One ZIPfileinfo is kept for each open file in a ZIP archive.
  82. */
  83. typedef struct
  84. {
  85. ZIPentry *entry; /* Info on file. */
  86. PHYSFS_Io *io; /* physical file handle. */
  87. PHYSFS_uint32 compressed_position; /* offset in compressed data. */
  88. PHYSFS_uint32 uncompressed_position; /* tell() position. */
  89. PHYSFS_uint8 *buffer; /* decompression buffer. */
  90. PHYSFS_uint32 crypto_keys[3]; /* for "traditional" crypto. */
  91. PHYSFS_uint32 initial_crypto_keys[3]; /* for "traditional" crypto. */
  92. z_stream stream; /* zlib stream state. */
  93. } ZIPfileinfo;
  94. /* Magic numbers... */
  95. #define ZIP_LOCAL_FILE_SIG 0x04034b50
  96. #define ZIP_CENTRAL_DIR_SIG 0x02014b50
  97. #define ZIP_END_OF_CENTRAL_DIR_SIG 0x06054b50
  98. #define ZIP64_END_OF_CENTRAL_DIR_SIG 0x06064b50
  99. #define ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIG 0x07064b50
  100. #define ZIP64_EXTENDED_INFO_EXTRA_FIELD_SIG 0x0001
  101. /* compression methods... */
  102. #define COMPMETH_NONE 0
  103. /* ...and others... */
  104. #define UNIX_FILETYPE_MASK 0170000
  105. #define UNIX_FILETYPE_SYMLINK 0120000
  106. #define ZIP_GENERAL_BITS_TRADITIONAL_CRYPTO (1 << 0)
  107. #define ZIP_GENERAL_BITS_IGNORE_LOCAL_HEADER (1 << 3)
  108. /* support for "traditional" PKWARE encryption. */
  109. static int zip_entry_is_tradional_crypto(const ZIPentry *entry)
  110. {
  111. return (entry->general_bits & ZIP_GENERAL_BITS_TRADITIONAL_CRYPTO) != 0;
  112. } /* zip_entry_is_traditional_crypto */
  113. static int zip_entry_ignore_local_header(const ZIPentry *entry)
  114. {
  115. return (entry->general_bits & ZIP_GENERAL_BITS_IGNORE_LOCAL_HEADER) != 0;
  116. } /* zip_entry_is_traditional_crypto */
  117. static PHYSFS_uint32 zip_crypto_crc32(const PHYSFS_uint32 crc, const PHYSFS_uint8 val)
  118. {
  119. int i;
  120. PHYSFS_uint32 xorval = (crc ^ ((PHYSFS_uint32) val)) & 0xFF;
  121. for (i = 0; i < 8; i++)
  122. xorval = ((xorval & 1) ? (0xEDB88320 ^ (xorval >> 1)) : (xorval >> 1));
  123. return xorval ^ (crc >> 8);
  124. } /* zip_crc32 */
  125. static void zip_update_crypto_keys(PHYSFS_uint32 *keys, const PHYSFS_uint8 val)
  126. {
  127. keys[0] = zip_crypto_crc32(keys[0], val);
  128. keys[1] = keys[1] + (keys[0] & 0x000000FF);
  129. keys[1] = (keys[1] * 134775813) + 1;
  130. keys[2] = zip_crypto_crc32(keys[2], (PHYSFS_uint8) ((keys[1] >> 24) & 0xFF));
  131. } /* zip_update_crypto_keys */
  132. static PHYSFS_uint8 zip_decrypt_byte(const PHYSFS_uint32 *keys)
  133. {
  134. const PHYSFS_uint16 tmp = keys[2] | 2;
  135. return (PHYSFS_uint8) ((tmp * (tmp ^ 1)) >> 8);
  136. } /* zip_decrypt_byte */
  137. static PHYSFS_sint64 zip_read_decrypt(ZIPfileinfo *finfo, void *buf, PHYSFS_uint64 len)
  138. {
  139. PHYSFS_Io *io = finfo->io;
  140. const PHYSFS_sint64 br = io->read(io, buf, len);
  141. /* Decompression the new data if necessary. */
  142. if (zip_entry_is_tradional_crypto(finfo->entry) && (br > 0))
  143. {
  144. PHYSFS_uint32 *keys = finfo->crypto_keys;
  145. PHYSFS_uint8 *ptr = (PHYSFS_uint8 *) buf;
  146. PHYSFS_sint64 i;
  147. for (i = 0; i < br; i++, ptr++)
  148. {
  149. const PHYSFS_uint8 ch = *ptr ^ zip_decrypt_byte(keys);
  150. zip_update_crypto_keys(keys, ch);
  151. *ptr = ch;
  152. } /* for */
  153. } /* if */
  154. return br;
  155. } /* zip_read_decrypt */
  156. static int zip_prep_crypto_keys(ZIPfileinfo *finfo, const PHYSFS_uint8 *crypto_header, const PHYSFS_uint8 *password)
  157. {
  158. /* It doesn't appear to be documented in PKWare's APPNOTE.TXT, but you
  159. need to use a different byte in the header to verify the password
  160. if general purpose bit 3 is set. Discovered this from Info-Zip.
  161. That's what the (verifier) value is doing, below. */
  162. PHYSFS_uint32 *keys = finfo->crypto_keys;
  163. const ZIPentry *entry = finfo->entry;
  164. const int usedate = zip_entry_ignore_local_header(entry);
  165. const PHYSFS_uint8 verifier = (PHYSFS_uint8) ((usedate ? (entry->dos_mod_time >> 8) : (entry->crc >> 24)) & 0xFF);
  166. PHYSFS_uint8 finalbyte = 0;
  167. int i = 0;
  168. /* initialize vector with defaults, then password, then header. */
  169. keys[0] = 305419896;
  170. keys[1] = 591751049;
  171. keys[2] = 878082192;
  172. while (*password)
  173. zip_update_crypto_keys(keys, *(password++));
  174. for (i = 0; i < 12; i++)
  175. {
  176. const PHYSFS_uint8 c = crypto_header[i] ^ zip_decrypt_byte(keys);
  177. zip_update_crypto_keys(keys, c);
  178. finalbyte = c;
  179. } /* for */
  180. /* you have a 1/256 chance of passing this test incorrectly. :/ */
  181. if (finalbyte != verifier)
  182. BAIL(PHYSFS_ERR_BAD_PASSWORD, 0);
  183. /* save the initial vector for seeking purposes. Not secure!! */
  184. memcpy(finfo->initial_crypto_keys, finfo->crypto_keys, 12);
  185. return 1;
  186. } /* zip_prep_crypto_keys */
  187. /*
  188. * Bridge physfs allocation functions to zlib's format...
  189. */
  190. static voidpf zlibPhysfsAlloc(voidpf opaque, uInt items, uInt size)
  191. {
  192. return ((PHYSFS_Allocator *) opaque)->Malloc(items * size);
  193. } /* zlibPhysfsAlloc */
  194. /*
  195. * Bridge physfs allocation functions to zlib's format...
  196. */
  197. static void zlibPhysfsFree(voidpf opaque, voidpf address)
  198. {
  199. ((PHYSFS_Allocator *) opaque)->Free(address);
  200. } /* zlibPhysfsFree */
  201. /*
  202. * Construct a new z_stream to a sane state.
  203. */
  204. static void initializeZStream(z_stream *pstr)
  205. {
  206. memset(pstr, '\0', sizeof (z_stream));
  207. pstr->zalloc = zlibPhysfsAlloc;
  208. pstr->zfree = zlibPhysfsFree;
  209. pstr->opaque = &allocator;
  210. } /* initializeZStream */
  211. static PHYSFS_ErrorCode zlib_error_code(int rc)
  212. {
  213. switch (rc)
  214. {
  215. case Z_OK: return PHYSFS_ERR_OK; /* not an error. */
  216. case Z_STREAM_END: return PHYSFS_ERR_OK; /* not an error. */
  217. case Z_ERRNO: return PHYSFS_ERR_IO;
  218. case Z_MEM_ERROR: return PHYSFS_ERR_OUT_OF_MEMORY;
  219. default: return PHYSFS_ERR_CORRUPT;
  220. } /* switch */
  221. } /* zlib_error_string */
  222. /*
  223. * Wrap all zlib calls in this, so the physfs error state is set appropriately.
  224. */
  225. static int zlib_err(const int rc)
  226. {
  227. PHYSFS_setErrorCode(zlib_error_code(rc));
  228. return rc;
  229. } /* zlib_err */
  230. /*
  231. * Read an unsigned 64-bit int and swap to native byte order.
  232. */
  233. static int readui64(PHYSFS_Io *io, PHYSFS_uint64 *val)
  234. {
  235. PHYSFS_uint64 v;
  236. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &v, sizeof (v)), 0);
  237. *val = PHYSFS_swapULE64(v);
  238. return 1;
  239. } /* readui64 */
  240. /*
  241. * Read an unsigned 32-bit int and swap to native byte order.
  242. */
  243. static int readui32(PHYSFS_Io *io, PHYSFS_uint32 *val)
  244. {
  245. PHYSFS_uint32 v;
  246. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &v, sizeof (v)), 0);
  247. *val = PHYSFS_swapULE32(v);
  248. return 1;
  249. } /* readui32 */
  250. /*
  251. * Read an unsigned 16-bit int and swap to native byte order.
  252. */
  253. static int readui16(PHYSFS_Io *io, PHYSFS_uint16 *val)
  254. {
  255. PHYSFS_uint16 v;
  256. BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &v, sizeof (v)), 0);
  257. *val = PHYSFS_swapULE16(v);
  258. return 1;
  259. } /* readui16 */
  260. static PHYSFS_sint64 ZIP_read(PHYSFS_Io *_io, void *buf, PHYSFS_uint64 len)
  261. {
  262. ZIPfileinfo *finfo = (ZIPfileinfo *) _io->opaque;
  263. ZIPentry *entry = finfo->entry;
  264. PHYSFS_sint64 retval = 0;
  265. PHYSFS_sint64 maxread = (PHYSFS_sint64) len;
  266. PHYSFS_sint64 avail = entry->uncompressed_size -
  267. finfo->uncompressed_position;
  268. if (avail < maxread)
  269. maxread = avail;
  270. BAIL_IF_ERRPASS(maxread == 0, 0); /* quick rejection. */
  271. if (entry->compression_method == COMPMETH_NONE)
  272. retval = zip_read_decrypt(finfo, buf, maxread);
  273. else
  274. {
  275. finfo->stream.next_out = buf;
  276. finfo->stream.avail_out = (uInt) maxread;
  277. while (retval < maxread)
  278. {
  279. const PHYSFS_uint32 before = (PHYSFS_uint32) finfo->stream.total_out;
  280. int rc;
  281. if (finfo->stream.avail_in == 0)
  282. {
  283. PHYSFS_sint64 br;
  284. br = entry->compressed_size - finfo->compressed_position;
  285. if (br > 0)
  286. {
  287. if (br > ZIP_READBUFSIZE)
  288. br = ZIP_READBUFSIZE;
  289. br = zip_read_decrypt(finfo, finfo->buffer, (PHYSFS_uint64) br);
  290. if (br <= 0)
  291. break;
  292. finfo->compressed_position += (PHYSFS_uint32) br;
  293. finfo->stream.next_in = finfo->buffer;
  294. finfo->stream.avail_in = (unsigned int) br;
  295. } /* if */
  296. } /* if */
  297. rc = zlib_err(inflate(&finfo->stream, Z_SYNC_FLUSH));
  298. retval += (finfo->stream.total_out - before);
  299. if (rc != Z_OK)
  300. break;
  301. } /* while */
  302. } /* else */
  303. if (retval > 0)
  304. finfo->uncompressed_position += (PHYSFS_uint32) retval;
  305. return retval;
  306. } /* ZIP_read */
  307. static PHYSFS_sint64 ZIP_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
  308. {
  309. BAIL(PHYSFS_ERR_READ_ONLY, -1);
  310. } /* ZIP_write */
  311. static PHYSFS_sint64 ZIP_tell(PHYSFS_Io *io)
  312. {
  313. return ((ZIPfileinfo *) io->opaque)->uncompressed_position;
  314. } /* ZIP_tell */
  315. static int ZIP_seek(PHYSFS_Io *_io, PHYSFS_uint64 offset)
  316. {
  317. ZIPfileinfo *finfo = (ZIPfileinfo *) _io->opaque;
  318. ZIPentry *entry = finfo->entry;
  319. PHYSFS_Io *io = finfo->io;
  320. const int encrypted = zip_entry_is_tradional_crypto(entry);
  321. BAIL_IF(offset > entry->uncompressed_size, PHYSFS_ERR_PAST_EOF, 0);
  322. if (!encrypted && (entry->compression_method == COMPMETH_NONE))
  323. {
  324. PHYSFS_sint64 newpos = offset + entry->offset;
  325. BAIL_IF_ERRPASS(!io->seek(io, newpos), 0);
  326. finfo->uncompressed_position = (PHYSFS_uint32) offset;
  327. } /* if */
  328. else
  329. {
  330. /*
  331. * If seeking backwards, we need to redecode the file
  332. * from the start and throw away the compressed bits until we hit
  333. * the offset we need. If seeking forward, we still need to
  334. * decode, but we don't rewind first.
  335. */
  336. if (offset < finfo->uncompressed_position)
  337. {
  338. /* we do a copy so state is sane if inflateInit2() fails. */
  339. z_stream str;
  340. initializeZStream(&str);
  341. if (zlib_err(inflateInit2(&str, -MAX_WBITS)) != Z_OK)
  342. return 0;
  343. if (!io->seek(io, entry->offset + (encrypted ? 12 : 0)))
  344. return 0;
  345. inflateEnd(&finfo->stream);
  346. memcpy(&finfo->stream, &str, sizeof (z_stream));
  347. finfo->uncompressed_position = finfo->compressed_position = 0;
  348. if (encrypted)
  349. memcpy(finfo->crypto_keys, finfo->initial_crypto_keys, 12);
  350. } /* if */
  351. while (finfo->uncompressed_position != offset)
  352. {
  353. PHYSFS_uint8 buf[512];
  354. PHYSFS_uint32 maxread;
  355. maxread = (PHYSFS_uint32) (offset - finfo->uncompressed_position);
  356. if (maxread > sizeof (buf))
  357. maxread = sizeof (buf);
  358. if (ZIP_read(_io, buf, maxread) != maxread)
  359. return 0;
  360. } /* while */
  361. } /* else */
  362. return 1;
  363. } /* ZIP_seek */
  364. static PHYSFS_sint64 ZIP_length(PHYSFS_Io *io)
  365. {
  366. const ZIPfileinfo *finfo = (ZIPfileinfo *) io->opaque;
  367. return (PHYSFS_sint64) finfo->entry->uncompressed_size;
  368. } /* ZIP_length */
  369. static PHYSFS_Io *zip_get_io(PHYSFS_Io *io, ZIPinfo *inf, ZIPentry *entry);
  370. static PHYSFS_Io *ZIP_duplicate(PHYSFS_Io *io)
  371. {
  372. ZIPfileinfo *origfinfo = (ZIPfileinfo *) io->opaque;
  373. PHYSFS_Io *retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  374. ZIPfileinfo *finfo = (ZIPfileinfo *) allocator.Malloc(sizeof (ZIPfileinfo));
  375. GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, failed);
  376. GOTO_IF(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, failed);
  377. memset(finfo, '\0', sizeof (*finfo));
  378. finfo->entry = origfinfo->entry;
  379. finfo->io = zip_get_io(origfinfo->io, NULL, finfo->entry);
  380. GOTO_IF_ERRPASS(!finfo->io, failed);
  381. initializeZStream(&finfo->stream);
  382. if (finfo->entry->compression_method != COMPMETH_NONE)
  383. {
  384. finfo->buffer = (PHYSFS_uint8 *) allocator.Malloc(ZIP_READBUFSIZE);
  385. GOTO_IF(!finfo->buffer, PHYSFS_ERR_OUT_OF_MEMORY, failed);
  386. if (zlib_err(inflateInit2(&finfo->stream, -MAX_WBITS)) != Z_OK)
  387. goto failed;
  388. } /* if */
  389. memcpy(retval, io, sizeof (PHYSFS_Io));
  390. retval->opaque = finfo;
  391. return retval;
  392. failed:
  393. if (finfo != NULL)
  394. {
  395. if (finfo->io != NULL)
  396. finfo->io->destroy(finfo->io);
  397. if (finfo->buffer != NULL)
  398. {
  399. allocator.Free(finfo->buffer);
  400. inflateEnd(&finfo->stream);
  401. } /* if */
  402. allocator.Free(finfo);
  403. } /* if */
  404. if (retval != NULL)
  405. allocator.Free(retval);
  406. return NULL;
  407. } /* ZIP_duplicate */
  408. static int ZIP_flush(PHYSFS_Io *io) { return 1; /* no write support. */ }
  409. static void ZIP_destroy(PHYSFS_Io *io)
  410. {
  411. ZIPfileinfo *finfo = (ZIPfileinfo *) io->opaque;
  412. finfo->io->destroy(finfo->io);
  413. if (finfo->entry->compression_method != COMPMETH_NONE)
  414. inflateEnd(&finfo->stream);
  415. if (finfo->buffer != NULL)
  416. allocator.Free(finfo->buffer);
  417. allocator.Free(finfo);
  418. allocator.Free(io);
  419. } /* ZIP_destroy */
  420. static const PHYSFS_Io ZIP_Io =
  421. {
  422. CURRENT_PHYSFS_IO_API_VERSION, NULL,
  423. ZIP_read,
  424. ZIP_write,
  425. ZIP_seek,
  426. ZIP_tell,
  427. ZIP_length,
  428. ZIP_duplicate,
  429. ZIP_flush,
  430. ZIP_destroy
  431. };
  432. static PHYSFS_sint64 zip_find_end_of_central_dir(PHYSFS_Io *io, PHYSFS_sint64 *len)
  433. {
  434. PHYSFS_uint8 buf[256];
  435. PHYSFS_uint8 extra[4] = { 0, 0, 0, 0 };
  436. PHYSFS_sint32 i = 0;
  437. PHYSFS_sint64 filelen;
  438. PHYSFS_sint64 filepos;
  439. PHYSFS_sint32 maxread;
  440. PHYSFS_sint32 totalread = 0;
  441. int found = 0;
  442. filelen = io->length(io);
  443. BAIL_IF_ERRPASS(filelen == -1, -1);
  444. /*
  445. * Jump to the end of the file and start reading backwards.
  446. * The last thing in the file is the zipfile comment, which is variable
  447. * length, and the field that specifies its size is before it in the
  448. * file (argh!)...this means that we need to scan backwards until we
  449. * hit the end-of-central-dir signature. We can then sanity check that
  450. * the comment was as big as it should be to make sure we're in the
  451. * right place. The comment length field is 16 bits, so we can stop
  452. * searching for that signature after a little more than 64k at most,
  453. * and call it a corrupted zipfile.
  454. */
  455. if (sizeof (buf) < (PHYSFS_uint64)filelen)
  456. {
  457. filepos = filelen - sizeof (buf);
  458. maxread = sizeof (buf);
  459. } /* if */
  460. else
  461. {
  462. filepos = 0;
  463. maxread = (PHYSFS_uint32) filelen;
  464. } /* else */
  465. while ((totalread < filelen) && (totalread < 65557))
  466. {
  467. BAIL_IF_ERRPASS(!io->seek(io, filepos), -1);
  468. /* make sure we catch a signature between buffers. */
  469. if (totalread != 0)
  470. {
  471. if (!__PHYSFS_readAll(io, buf, maxread - 4))
  472. return -1;
  473. memcpy(&buf[maxread - 4], extra, sizeof (extra));
  474. totalread += maxread - 4;
  475. } /* if */
  476. else
  477. {
  478. if (!__PHYSFS_readAll(io, buf, maxread))
  479. return -1;
  480. totalread += maxread;
  481. } /* else */
  482. memcpy(extra, buf, sizeof (extra));
  483. for (i = maxread - 4; i > 0; i--)
  484. {
  485. if ((buf[i + 0] == 0x50) &&
  486. (buf[i + 1] == 0x4B) &&
  487. (buf[i + 2] == 0x05) &&
  488. (buf[i + 3] == 0x06) )
  489. {
  490. found = 1; /* that's the signature! */
  491. break;
  492. } /* if */
  493. } /* for */
  494. if (found)
  495. break;
  496. filepos -= (maxread - 4);
  497. if (filepos < 0)
  498. filepos = 0;
  499. } /* while */
  500. BAIL_IF(!found, PHYSFS_ERR_UNSUPPORTED, -1);
  501. if (len != NULL)
  502. *len = filelen;
  503. return (filepos + i);
  504. } /* zip_find_end_of_central_dir */
  505. static int isZip(PHYSFS_Io *io)
  506. {
  507. PHYSFS_uint32 sig = 0;
  508. int retval = 0;
  509. /*
  510. * The first thing in a zip file might be the signature of the
  511. * first local file record, so it makes for a quick determination.
  512. */
  513. if (readui32(io, &sig))
  514. {
  515. retval = (sig == ZIP_LOCAL_FILE_SIG);
  516. if (!retval)
  517. {
  518. /*
  519. * No sig...might be a ZIP with data at the start
  520. * (a self-extracting executable, etc), so we'll have to do
  521. * it the hard way...
  522. */
  523. retval = (zip_find_end_of_central_dir(io, NULL) != -1);
  524. } /* if */
  525. } /* if */
  526. return retval;
  527. } /* isZip */
  528. /* Convert paths from old, buggy DOS zippers... */
  529. static void zip_convert_dos_path(const PHYSFS_uint16 entryversion, char *path)
  530. {
  531. const PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((entryversion >> 8) & 0xFF);
  532. if (hosttype == 0) /* FS_FAT_ */
  533. {
  534. while (*path)
  535. {
  536. if (*path == '\\')
  537. *path = '/';
  538. path++;
  539. } /* while */
  540. } /* if */
  541. } /* zip_convert_dos_path */
  542. static void zip_expand_symlink_path(char *path)
  543. {
  544. char *ptr = path;
  545. char *prevptr = path;
  546. while (1)
  547. {
  548. ptr = strchr(ptr, '/');
  549. if (ptr == NULL)
  550. break;
  551. if (*(ptr + 1) == '.')
  552. {
  553. if (*(ptr + 2) == '/')
  554. {
  555. /* current dir in middle of string: ditch it. */
  556. memmove(ptr, ptr + 2, strlen(ptr + 2) + 1);
  557. } /* else if */
  558. else if (*(ptr + 2) == '\0')
  559. {
  560. /* current dir at end of string: ditch it. */
  561. *ptr = '\0';
  562. } /* else if */
  563. else if (*(ptr + 2) == '.')
  564. {
  565. if (*(ptr + 3) == '/')
  566. {
  567. /* parent dir in middle: move back one, if possible. */
  568. memmove(prevptr, ptr + 4, strlen(ptr + 4) + 1);
  569. ptr = prevptr;
  570. while (prevptr != path)
  571. {
  572. prevptr--;
  573. if (*prevptr == '/')
  574. {
  575. prevptr++;
  576. break;
  577. } /* if */
  578. } /* while */
  579. } /* if */
  580. if (*(ptr + 3) == '\0')
  581. {
  582. /* parent dir at end: move back one, if possible. */
  583. *prevptr = '\0';
  584. } /* if */
  585. } /* if */
  586. } /* if */
  587. else
  588. {
  589. prevptr = ptr;
  590. ptr++;
  591. } /* else */
  592. } /* while */
  593. } /* zip_expand_symlink_path */
  594. static inline ZIPentry *zip_find_entry(ZIPinfo *info, const char *path)
  595. {
  596. return (ZIPentry *) __PHYSFS_DirTreeFind(&info->tree, path);
  597. } /* zip_find_entry */
  598. /* (forward reference: zip_follow_symlink and zip_resolve call each other.) */
  599. static int zip_resolve(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry);
  600. /*
  601. * Look for the entry named by (path). If it exists, resolve it, and return
  602. * a pointer to that entry. If it's another symlink, keep resolving until you
  603. * hit a real file and then return a pointer to the final non-symlink entry.
  604. * If there's a problem, return NULL.
  605. */
  606. static ZIPentry *zip_follow_symlink(PHYSFS_Io *io, ZIPinfo *info, char *path)
  607. {
  608. ZIPentry *entry;
  609. zip_expand_symlink_path(path);
  610. entry = zip_find_entry(info, path);
  611. if (entry != NULL)
  612. {
  613. if (!zip_resolve(io, info, entry)) /* recursive! */
  614. entry = NULL;
  615. else
  616. {
  617. if (entry->symlink != NULL)
  618. entry = entry->symlink;
  619. } /* else */
  620. } /* if */
  621. return entry;
  622. } /* zip_follow_symlink */
  623. static int zip_resolve_symlink(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry)
  624. {
  625. const size_t size = (size_t) entry->uncompressed_size;
  626. char *path = NULL;
  627. int rc = 0;
  628. /*
  629. * We've already parsed the local file header of the symlink at this
  630. * point. Now we need to read the actual link from the file data and
  631. * follow it.
  632. */
  633. BAIL_IF_ERRPASS(!io->seek(io, entry->offset), 0);
  634. path = (char *) __PHYSFS_smallAlloc(size + 1);
  635. BAIL_IF(!path, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  636. if (entry->compression_method == COMPMETH_NONE)
  637. rc = __PHYSFS_readAll(io, path, size);
  638. else /* symlink target path is compressed... */
  639. {
  640. z_stream stream;
  641. const size_t complen = (size_t) entry->compressed_size;
  642. PHYSFS_uint8 *compressed = (PHYSFS_uint8*) __PHYSFS_smallAlloc(complen);
  643. if (compressed != NULL)
  644. {
  645. if (__PHYSFS_readAll(io, compressed, complen))
  646. {
  647. initializeZStream(&stream);
  648. stream.next_in = compressed;
  649. stream.avail_in = (unsigned int) complen;
  650. stream.next_out = (unsigned char *) path;
  651. stream.avail_out = (unsigned int) size;
  652. if (zlib_err(inflateInit2(&stream, -MAX_WBITS)) == Z_OK)
  653. {
  654. rc = zlib_err(inflate(&stream, Z_FINISH));
  655. inflateEnd(&stream);
  656. /* both are acceptable outcomes... */
  657. rc = ((rc == Z_OK) || (rc == Z_STREAM_END));
  658. } /* if */
  659. } /* if */
  660. __PHYSFS_smallFree(compressed);
  661. } /* if */
  662. } /* else */
  663. if (rc)
  664. {
  665. path[entry->uncompressed_size] = '\0'; /* null-terminate it. */
  666. zip_convert_dos_path(entry->version, path);
  667. entry->symlink = zip_follow_symlink(io, info, path);
  668. } /* else */
  669. __PHYSFS_smallFree(path);
  670. return (entry->symlink != NULL);
  671. } /* zip_resolve_symlink */
  672. /*
  673. * Parse the local file header of an entry, and update entry->offset.
  674. */
  675. static int zip_parse_local(PHYSFS_Io *io, ZIPentry *entry)
  676. {
  677. PHYSFS_uint32 ui32;
  678. PHYSFS_uint16 ui16;
  679. PHYSFS_uint16 fnamelen;
  680. PHYSFS_uint16 extralen;
  681. /*
  682. * crc and (un)compressed_size are always zero if this is a "JAR"
  683. * archive created with Sun's Java tools, apparently. We only
  684. * consider this archive corrupted if those entries don't match and
  685. * aren't zero. That seems to work well.
  686. * We also ignore a mismatch if the value is 0xFFFFFFFF here, since it's
  687. * possible that's a Zip64 thing.
  688. */
  689. /* !!! FIXME: apparently these are zero if general purpose bit 3 is set,
  690. !!! FIXME: which is probably true for Jar files, fwiw, but we don't
  691. !!! FIXME: care about these values anyhow. */
  692. BAIL_IF_ERRPASS(!io->seek(io, entry->offset), 0);
  693. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  694. BAIL_IF(ui32 != ZIP_LOCAL_FILE_SIG, PHYSFS_ERR_CORRUPT, 0);
  695. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  696. /* Windows Explorer might rewrite the entire central directory, setting
  697. this field to 2.0/MS-DOS for all files, so favor the local version,
  698. which it leaves intact if it didn't alter that specific file. */
  699. entry->version_needed = ui16;
  700. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0); /* general bits. */
  701. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  702. BAIL_IF(ui16 != entry->compression_method, PHYSFS_ERR_CORRUPT, 0);
  703. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0); /* date/time */
  704. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  705. BAIL_IF(ui32 && (ui32 != entry->crc), PHYSFS_ERR_CORRUPT, 0);
  706. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  707. BAIL_IF(ui32 && (ui32 != 0xFFFFFFFF) &&
  708. (ui32 != entry->compressed_size), PHYSFS_ERR_CORRUPT, 0);
  709. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  710. BAIL_IF(ui32 && (ui32 != 0xFFFFFFFF) &&
  711. (ui32 != entry->uncompressed_size), PHYSFS_ERR_CORRUPT, 0);
  712. BAIL_IF_ERRPASS(!readui16(io, &fnamelen), 0);
  713. BAIL_IF_ERRPASS(!readui16(io, &extralen), 0);
  714. entry->offset += fnamelen + extralen + 30;
  715. return 1;
  716. } /* zip_parse_local */
  717. static int zip_resolve(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry)
  718. {
  719. int retval = 1;
  720. const ZipResolveType resolve_type = entry->resolved;
  721. if (resolve_type == ZIP_DIRECTORY)
  722. return 1; /* we're good. */
  723. /* Don't bother if we've failed to resolve this entry before. */
  724. BAIL_IF(resolve_type == ZIP_BROKEN_FILE, PHYSFS_ERR_CORRUPT, 0);
  725. BAIL_IF(resolve_type == ZIP_BROKEN_SYMLINK, PHYSFS_ERR_CORRUPT, 0);
  726. /* uhoh...infinite symlink loop! */
  727. BAIL_IF(resolve_type == ZIP_RESOLVING, PHYSFS_ERR_SYMLINK_LOOP, 0);
  728. /*
  729. * We fix up the offset to point to the actual data on the
  730. * first open, since we don't want to seek across the whole file on
  731. * archive open (can be SLOW on large, CD-stored files), but we
  732. * need to check the local file header...not just for corruption,
  733. * but since it stores offset info the central directory does not.
  734. */
  735. if (resolve_type != ZIP_RESOLVED)
  736. {
  737. if (entry->tree.isdir) /* an ancestor dir that DirTree filled in? */
  738. {
  739. entry->resolved = ZIP_DIRECTORY;
  740. return 1;
  741. } /* if */
  742. retval = zip_parse_local(io, entry);
  743. if (retval)
  744. {
  745. /*
  746. * If it's a symlink, find the original file. This will cause
  747. * resolution of other entries (other symlinks and, eventually,
  748. * the real file) if all goes well.
  749. */
  750. if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
  751. retval = zip_resolve_symlink(io, info, entry);
  752. } /* if */
  753. if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
  754. entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_SYMLINK);
  755. else if (resolve_type == ZIP_UNRESOLVED_FILE)
  756. entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_FILE);
  757. } /* if */
  758. return retval;
  759. } /* zip_resolve */
  760. static int zip_entry_is_symlink(const ZIPentry *entry)
  761. {
  762. return ((entry->resolved == ZIP_UNRESOLVED_SYMLINK) ||
  763. (entry->resolved == ZIP_BROKEN_SYMLINK) ||
  764. (entry->symlink));
  765. } /* zip_entry_is_symlink */
  766. static int zip_version_does_symlinks(PHYSFS_uint32 version)
  767. {
  768. int retval = 0;
  769. PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((version >> 8) & 0xFF);
  770. switch (hosttype)
  771. {
  772. /*
  773. * These are the platforms that can NOT build an archive with
  774. * symlinks, according to the Info-ZIP project.
  775. */
  776. case 0: /* FS_FAT_ */
  777. case 1: /* AMIGA_ */
  778. case 2: /* VMS_ */
  779. case 4: /* VM_CSM_ */
  780. case 6: /* FS_HPFS_ */
  781. case 11: /* FS_NTFS_ */
  782. case 14: /* FS_VFAT_ */
  783. case 13: /* ACORN_ */
  784. case 15: /* MVS_ */
  785. case 18: /* THEOS_ */
  786. break; /* do nothing. */
  787. default: /* assume the rest to be unix-like. */
  788. retval = 1;
  789. break;
  790. } /* switch */
  791. return retval;
  792. } /* zip_version_does_symlinks */
  793. static inline int zip_has_symlink_attr(const ZIPentry *entry,
  794. const PHYSFS_uint32 extern_attr)
  795. {
  796. PHYSFS_uint16 xattr = ((extern_attr >> 16) & 0xFFFF);
  797. return ( (zip_version_does_symlinks(entry->version)) &&
  798. (entry->uncompressed_size > 0) &&
  799. ((xattr & UNIX_FILETYPE_MASK) == UNIX_FILETYPE_SYMLINK) );
  800. } /* zip_has_symlink_attr */
  801. static PHYSFS_sint64 zip_dos_time_to_physfs_time(PHYSFS_uint32 dostime)
  802. {
  803. PHYSFS_uint32 dosdate;
  804. struct tm unixtime;
  805. memset(&unixtime, '\0', sizeof (unixtime));
  806. dosdate = (PHYSFS_uint32) ((dostime >> 16) & 0xFFFF);
  807. dostime &= 0xFFFF;
  808. /* dissect date */
  809. unixtime.tm_year = ((dosdate >> 9) & 0x7F) + 80;
  810. unixtime.tm_mon = ((dosdate >> 5) & 0x0F) - 1;
  811. unixtime.tm_mday = ((dosdate ) & 0x1F);
  812. /* dissect time */
  813. unixtime.tm_hour = ((dostime >> 11) & 0x1F);
  814. unixtime.tm_min = ((dostime >> 5) & 0x3F);
  815. unixtime.tm_sec = ((dostime << 1) & 0x3E);
  816. /* let mktime calculate daylight savings time. */
  817. unixtime.tm_isdst = -1;
  818. return ((PHYSFS_sint64) mktime(&unixtime));
  819. } /* zip_dos_time_to_physfs_time */
  820. static ZIPentry *zip_load_entry(ZIPinfo *info, const int zip64,
  821. const PHYSFS_uint64 ofs_fixup)
  822. {
  823. PHYSFS_Io *io = info->io;
  824. ZIPentry entry;
  825. ZIPentry *retval = NULL;
  826. PHYSFS_uint16 fnamelen, extralen, commentlen;
  827. PHYSFS_uint32 external_attr;
  828. PHYSFS_uint32 starting_disk;
  829. PHYSFS_uint64 offset;
  830. PHYSFS_uint16 ui16;
  831. PHYSFS_uint32 ui32;
  832. PHYSFS_sint64 si64;
  833. char *name = NULL;
  834. int isdir = 0;
  835. /* sanity check with central directory signature... */
  836. BAIL_IF_ERRPASS(!readui32(io, &ui32), NULL);
  837. BAIL_IF(ui32 != ZIP_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, NULL);
  838. memset(&entry, '\0', sizeof (entry));
  839. /* Get the pertinent parts of the record... */
  840. BAIL_IF_ERRPASS(!readui16(io, &entry.version), NULL);
  841. BAIL_IF_ERRPASS(!readui16(io, &entry.version_needed), NULL);
  842. BAIL_IF_ERRPASS(!readui16(io, &entry.general_bits), NULL); /* general bits */
  843. BAIL_IF_ERRPASS(!readui16(io, &entry.compression_method), NULL);
  844. BAIL_IF_ERRPASS(!readui32(io, &entry.dos_mod_time), NULL);
  845. entry.last_mod_time = zip_dos_time_to_physfs_time(entry.dos_mod_time);
  846. BAIL_IF_ERRPASS(!readui32(io, &entry.crc), NULL);
  847. BAIL_IF_ERRPASS(!readui32(io, &ui32), NULL);
  848. entry.compressed_size = (PHYSFS_uint64) ui32;
  849. BAIL_IF_ERRPASS(!readui32(io, &ui32), NULL);
  850. entry.uncompressed_size = (PHYSFS_uint64) ui32;
  851. BAIL_IF_ERRPASS(!readui16(io, &fnamelen), NULL);
  852. BAIL_IF_ERRPASS(!readui16(io, &extralen), NULL);
  853. BAIL_IF_ERRPASS(!readui16(io, &commentlen), NULL);
  854. BAIL_IF_ERRPASS(!readui16(io, &ui16), NULL);
  855. starting_disk = (PHYSFS_uint32) ui16;
  856. BAIL_IF_ERRPASS(!readui16(io, &ui16), NULL); /* internal file attribs */
  857. BAIL_IF_ERRPASS(!readui32(io, &external_attr), NULL);
  858. BAIL_IF_ERRPASS(!readui32(io, &ui32), NULL);
  859. offset = (PHYSFS_uint64) ui32;
  860. name = (char *) __PHYSFS_smallAlloc(fnamelen + 1);
  861. BAIL_IF(!name, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  862. if (!__PHYSFS_readAll(io, name, fnamelen))
  863. {
  864. __PHYSFS_smallFree(name);
  865. return NULL;
  866. } /* if */
  867. if (name[fnamelen - 1] == '/')
  868. {
  869. name[fnamelen - 1] = '\0';
  870. isdir = 1;
  871. } /* if */
  872. name[fnamelen] = '\0'; /* null-terminate the filename. */
  873. zip_convert_dos_path(entry.version, name);
  874. retval = (ZIPentry *) __PHYSFS_DirTreeAdd(&info->tree, name, isdir);
  875. __PHYSFS_smallFree(name);
  876. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  877. /* It's okay to BAIL without freeing retval, because it's stored in the
  878. __PHYSFS_DirTree and will be freed later anyhow. */
  879. BAIL_IF(retval->last_mod_time != 0, PHYSFS_ERR_CORRUPT, NULL); /* dupe? */
  880. /* Move the data we already read into place in the official object. */
  881. memcpy(((PHYSFS_uint8 *) retval) + sizeof (__PHYSFS_DirTreeEntry),
  882. ((PHYSFS_uint8 *) &entry) + sizeof (__PHYSFS_DirTreeEntry),
  883. sizeof (*retval) - sizeof (__PHYSFS_DirTreeEntry));
  884. retval->symlink = NULL; /* will be resolved later, if necessary. */
  885. if (isdir)
  886. retval->resolved = ZIP_DIRECTORY;
  887. else
  888. {
  889. retval->resolved = (zip_has_symlink_attr(retval, external_attr)) ?
  890. ZIP_UNRESOLVED_SYMLINK : ZIP_UNRESOLVED_FILE;
  891. } /* else */
  892. si64 = io->tell(io);
  893. BAIL_IF_ERRPASS(si64 == -1, NULL);
  894. /* If the actual sizes didn't fit in 32-bits, look for the Zip64
  895. extended information extra field... */
  896. if ( (zip64) &&
  897. ((offset == 0xFFFFFFFF) ||
  898. (starting_disk == 0xFFFFFFFF) ||
  899. (retval->compressed_size == 0xFFFFFFFF) ||
  900. (retval->uncompressed_size == 0xFFFFFFFF)) )
  901. {
  902. int found = 0;
  903. PHYSFS_uint16 sig = 0;
  904. PHYSFS_uint16 len = 0;
  905. while (extralen > 4)
  906. {
  907. BAIL_IF_ERRPASS(!readui16(io, &sig), NULL);
  908. BAIL_IF_ERRPASS(!readui16(io, &len), 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_tradional_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_tradional_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 ... */