physfs_archiver_zip.c 53 KB

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