physfs_archiver_zip.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  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 PHYSFS_uint64 size = 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 PHYSFS_uint64 complen = 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. entry->resolved = ZIP_RESOLVING;
  729. retval = zip_parse_local(io, entry);
  730. if (retval)
  731. {
  732. /*
  733. * If it's a symlink, find the original file. This will cause
  734. * resolution of other entries (other symlinks and, eventually,
  735. * the real file) if all goes well.
  736. */
  737. if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
  738. retval = zip_resolve_symlink(io, info, entry);
  739. } /* if */
  740. if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
  741. entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_SYMLINK);
  742. else if (resolve_type == ZIP_UNRESOLVED_FILE)
  743. entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_FILE);
  744. } /* if */
  745. return retval;
  746. } /* zip_resolve */
  747. static int zip_entry_is_symlink(const ZIPentry *entry)
  748. {
  749. return ((entry->resolved == ZIP_UNRESOLVED_SYMLINK) ||
  750. (entry->resolved == ZIP_BROKEN_SYMLINK) ||
  751. (entry->symlink));
  752. } /* zip_entry_is_symlink */
  753. static int zip_version_does_symlinks(PHYSFS_uint32 version)
  754. {
  755. int retval = 0;
  756. PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((version >> 8) & 0xFF);
  757. switch (hosttype)
  758. {
  759. /*
  760. * These are the platforms that can NOT build an archive with
  761. * symlinks, according to the Info-ZIP project.
  762. */
  763. case 0: /* FS_FAT_ */
  764. case 1: /* AMIGA_ */
  765. case 2: /* VMS_ */
  766. case 4: /* VM_CSM_ */
  767. case 6: /* FS_HPFS_ */
  768. case 11: /* FS_NTFS_ */
  769. case 14: /* FS_VFAT_ */
  770. case 13: /* ACORN_ */
  771. case 15: /* MVS_ */
  772. case 18: /* THEOS_ */
  773. break; /* do nothing. */
  774. default: /* assume the rest to be unix-like. */
  775. retval = 1;
  776. break;
  777. } /* switch */
  778. return retval;
  779. } /* zip_version_does_symlinks */
  780. static inline int zip_has_symlink_attr(const ZIPentry *entry,
  781. const PHYSFS_uint32 extern_attr)
  782. {
  783. PHYSFS_uint16 xattr = ((extern_attr >> 16) & 0xFFFF);
  784. return ( (zip_version_does_symlinks(entry->version)) &&
  785. (entry->uncompressed_size > 0) &&
  786. ((xattr & UNIX_FILETYPE_MASK) == UNIX_FILETYPE_SYMLINK) );
  787. } /* zip_has_symlink_attr */
  788. static PHYSFS_sint64 zip_dos_time_to_physfs_time(PHYSFS_uint32 dostime)
  789. {
  790. PHYSFS_uint32 dosdate;
  791. struct tm unixtime;
  792. memset(&unixtime, '\0', sizeof (unixtime));
  793. dosdate = (PHYSFS_uint32) ((dostime >> 16) & 0xFFFF);
  794. dostime &= 0xFFFF;
  795. /* dissect date */
  796. unixtime.tm_year = ((dosdate >> 9) & 0x7F) + 80;
  797. unixtime.tm_mon = ((dosdate >> 5) & 0x0F) - 1;
  798. unixtime.tm_mday = ((dosdate ) & 0x1F);
  799. /* dissect time */
  800. unixtime.tm_hour = ((dostime >> 11) & 0x1F);
  801. unixtime.tm_min = ((dostime >> 5) & 0x3F);
  802. unixtime.tm_sec = ((dostime << 1) & 0x3E);
  803. /* let mktime calculate daylight savings time. */
  804. unixtime.tm_isdst = -1;
  805. return ((PHYSFS_sint64) mktime(&unixtime));
  806. } /* zip_dos_time_to_physfs_time */
  807. static ZIPentry *zip_load_entry(ZIPinfo *info, const int zip64,
  808. const PHYSFS_uint64 ofs_fixup)
  809. {
  810. PHYSFS_Io *io = info->io;
  811. ZIPentry entry;
  812. ZIPentry *retval = NULL;
  813. PHYSFS_uint16 fnamelen, extralen, commentlen;
  814. PHYSFS_uint32 external_attr;
  815. PHYSFS_uint32 starting_disk;
  816. PHYSFS_uint64 offset;
  817. PHYSFS_uint16 ui16;
  818. PHYSFS_uint32 ui32;
  819. PHYSFS_sint64 si64;
  820. char *name = NULL;
  821. int isdir = 0;
  822. /* sanity check with central directory signature... */
  823. BAIL_IF_ERRPASS(!readui32(io, &ui32), NULL);
  824. BAIL_IF(ui32 != ZIP_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, NULL);
  825. memset(&entry, '\0', sizeof (entry));
  826. /* Get the pertinent parts of the record... */
  827. BAIL_IF_ERRPASS(!readui16(io, &entry.version), NULL);
  828. BAIL_IF_ERRPASS(!readui16(io, &entry.version_needed), NULL);
  829. BAIL_IF_ERRPASS(!readui16(io, &entry.general_bits), NULL); /* general bits */
  830. BAIL_IF_ERRPASS(!readui16(io, &entry.compression_method), NULL);
  831. BAIL_IF_ERRPASS(!readui32(io, &entry.dos_mod_time), NULL);
  832. entry.last_mod_time = zip_dos_time_to_physfs_time(entry.dos_mod_time);
  833. BAIL_IF_ERRPASS(!readui32(io, &entry.crc), NULL);
  834. BAIL_IF_ERRPASS(!readui32(io, &ui32), NULL);
  835. entry.compressed_size = (PHYSFS_uint64) ui32;
  836. BAIL_IF_ERRPASS(!readui32(io, &ui32), NULL);
  837. entry.uncompressed_size = (PHYSFS_uint64) ui32;
  838. BAIL_IF_ERRPASS(!readui16(io, &fnamelen), NULL);
  839. BAIL_IF_ERRPASS(!readui16(io, &extralen), NULL);
  840. BAIL_IF_ERRPASS(!readui16(io, &commentlen), NULL);
  841. BAIL_IF_ERRPASS(!readui16(io, &ui16), NULL);
  842. starting_disk = (PHYSFS_uint32) ui16;
  843. BAIL_IF_ERRPASS(!readui16(io, &ui16), NULL); /* internal file attribs */
  844. BAIL_IF_ERRPASS(!readui32(io, &external_attr), NULL);
  845. BAIL_IF_ERRPASS(!readui32(io, &ui32), NULL);
  846. offset = (PHYSFS_uint64) ui32;
  847. name = (char *) __PHYSFS_smallAlloc(fnamelen + 1);
  848. BAIL_IF(!name, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  849. if (!__PHYSFS_readAll(io, name, fnamelen))
  850. {
  851. __PHYSFS_smallFree(name);
  852. return NULL;
  853. } /* if */
  854. if (name[fnamelen - 1] == '/')
  855. {
  856. name[fnamelen - 1] = '\0';
  857. isdir = 1;
  858. } /* if */
  859. name[fnamelen] = '\0'; /* null-terminate the filename. */
  860. zip_convert_dos_path(entry.version, name);
  861. retval = (ZIPentry *) __PHYSFS_DirTreeAdd(&info->tree, name, isdir);
  862. __PHYSFS_smallFree(name);
  863. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  864. /* It's okay to BAIL without freeing retval, because it's stored in the
  865. __PHYSFS_DirTree and will be freed later anyhow. */
  866. BAIL_IF(retval->last_mod_time != 0, PHYSFS_ERR_CORRUPT, NULL); /* dupe? */
  867. /* Move the data we already read into place in the official object. */
  868. memcpy(((PHYSFS_uint8 *) retval) + sizeof (__PHYSFS_DirTreeEntry),
  869. ((PHYSFS_uint8 *) &entry) + sizeof (__PHYSFS_DirTreeEntry),
  870. sizeof (*retval) - sizeof (__PHYSFS_DirTreeEntry));
  871. retval->symlink = NULL; /* will be resolved later, if necessary. */
  872. if (isdir)
  873. retval->resolved = ZIP_DIRECTORY;
  874. else
  875. {
  876. retval->resolved = (zip_has_symlink_attr(retval, external_attr)) ?
  877. ZIP_UNRESOLVED_SYMLINK : ZIP_UNRESOLVED_FILE;
  878. } /* else */
  879. si64 = io->tell(io);
  880. BAIL_IF_ERRPASS(si64 == -1, NULL);
  881. /* If the actual sizes didn't fit in 32-bits, look for the Zip64
  882. extended information extra field... */
  883. if ( (zip64) &&
  884. ((offset == 0xFFFFFFFF) ||
  885. (starting_disk == 0xFFFFFFFF) ||
  886. (retval->compressed_size == 0xFFFFFFFF) ||
  887. (retval->uncompressed_size == 0xFFFFFFFF)) )
  888. {
  889. int found = 0;
  890. PHYSFS_uint16 sig, len;
  891. while (extralen > 4)
  892. {
  893. BAIL_IF_ERRPASS(!readui16(io, &sig), NULL);
  894. BAIL_IF_ERRPASS(!readui16(io, &len), NULL);
  895. si64 += 4 + len;
  896. extralen -= 4 + len;
  897. if (sig != ZIP64_EXTENDED_INFO_EXTRA_FIELD_SIG)
  898. {
  899. BAIL_IF_ERRPASS(!io->seek(io, si64), NULL);
  900. continue;
  901. } /* if */
  902. found = 1;
  903. break;
  904. } /* while */
  905. BAIL_IF(!found, PHYSFS_ERR_CORRUPT, NULL);
  906. if (retval->uncompressed_size == 0xFFFFFFFF)
  907. {
  908. BAIL_IF(len < 8, PHYSFS_ERR_CORRUPT, NULL);
  909. BAIL_IF_ERRPASS(!readui64(io, &retval->uncompressed_size), NULL);
  910. len -= 8;
  911. } /* if */
  912. if (retval->compressed_size == 0xFFFFFFFF)
  913. {
  914. BAIL_IF(len < 8, PHYSFS_ERR_CORRUPT, NULL);
  915. BAIL_IF_ERRPASS(!readui64(io, &retval->compressed_size), NULL);
  916. len -= 8;
  917. } /* if */
  918. if (offset == 0xFFFFFFFF)
  919. {
  920. BAIL_IF(len < 8, PHYSFS_ERR_CORRUPT, NULL);
  921. BAIL_IF_ERRPASS(!readui64(io, &offset), NULL);
  922. len -= 8;
  923. } /* if */
  924. if (starting_disk == 0xFFFFFFFF)
  925. {
  926. BAIL_IF(len < 8, PHYSFS_ERR_CORRUPT, NULL);
  927. BAIL_IF_ERRPASS(!readui32(io, &starting_disk), NULL);
  928. len -= 4;
  929. } /* if */
  930. BAIL_IF(len != 0, PHYSFS_ERR_CORRUPT, NULL);
  931. } /* if */
  932. BAIL_IF(starting_disk != 0, PHYSFS_ERR_CORRUPT, NULL);
  933. retval->offset = offset + ofs_fixup;
  934. /* seek to the start of the next entry in the central directory... */
  935. BAIL_IF_ERRPASS(!io->seek(io, si64 + extralen + commentlen), NULL);
  936. return retval; /* success. */
  937. } /* zip_load_entry */
  938. /* This leaves things allocated on error; the caller will clean up the mess. */
  939. static int zip_load_entries(ZIPinfo *info,
  940. const PHYSFS_uint64 data_ofs,
  941. const PHYSFS_uint64 central_ofs,
  942. const PHYSFS_uint64 entry_count)
  943. {
  944. PHYSFS_Io *io = info->io;
  945. const int zip64 = info->zip64;
  946. PHYSFS_uint64 i;
  947. BAIL_IF_ERRPASS(!io->seek(io, central_ofs), 0);
  948. for (i = 0; i < entry_count; i++)
  949. {
  950. ZIPentry *entry = zip_load_entry(info, zip64, data_ofs);
  951. BAIL_IF_ERRPASS(!entry, 0);
  952. if (zip_entry_is_tradional_crypto(entry))
  953. info->has_crypto = 1;
  954. } /* for */
  955. return 1;
  956. } /* zip_load_entries */
  957. static PHYSFS_sint64 zip64_find_end_of_central_dir(PHYSFS_Io *io,
  958. PHYSFS_sint64 _pos,
  959. PHYSFS_uint64 offset)
  960. {
  961. /*
  962. * Naturally, the offset is useless to us; it is the offset from the
  963. * start of file, which is meaningless if we've appended this .zip to
  964. * a self-extracting .exe. We need to find this on our own. It should
  965. * be directly before the locator record, but the record in question,
  966. * like the original end-of-central-directory record, ends with a
  967. * variable-length field. Unlike the original, which has to store the
  968. * size of that variable-length field in a 16-bit int and thus has to be
  969. * within 64k, the new one gets 64-bits.
  970. *
  971. * Fortunately, the only currently-specified record for that variable
  972. * length block is some weird proprietary thing that deals with EBCDIC
  973. * and tape backups or something. So we don't seek far.
  974. */
  975. PHYSFS_uint32 ui32;
  976. const PHYSFS_uint64 pos = (PHYSFS_uint64) _pos;
  977. assert(_pos > 0);
  978. /* Try offset specified in the Zip64 end of central directory locator. */
  979. /* This works if the entire PHYSFS_Io is the zip file. */
  980. BAIL_IF_ERRPASS(!io->seek(io, offset), -1);
  981. BAIL_IF_ERRPASS(!readui32(io, &ui32), -1);
  982. if (ui32 == ZIP64_END_OF_CENTRAL_DIR_SIG)
  983. return offset;
  984. /* Try 56 bytes before the Zip64 end of central directory locator. */
  985. /* This works if the record isn't variable length and is version 1. */
  986. if (pos > 56)
  987. {
  988. BAIL_IF_ERRPASS(!io->seek(io, pos-56), -1);
  989. BAIL_IF_ERRPASS(!readui32(io, &ui32), -1);
  990. if (ui32 == ZIP64_END_OF_CENTRAL_DIR_SIG)
  991. return pos-56;
  992. } /* if */
  993. /* Try 84 bytes before the Zip64 end of central directory locator. */
  994. /* This works if the record isn't variable length and is version 2. */
  995. if (pos > 84)
  996. {
  997. BAIL_IF_ERRPASS(!io->seek(io, pos-84), -1);
  998. BAIL_IF_ERRPASS(!readui32(io, &ui32), -1);
  999. if (ui32 == ZIP64_END_OF_CENTRAL_DIR_SIG)
  1000. return pos-84;
  1001. } /* if */
  1002. /* Ok, brute force: we know it's between (offset) and (pos) somewhere. */
  1003. /* Just try moving back at most 256k. Oh well. */
  1004. if ((offset < pos) && (pos > 4))
  1005. {
  1006. const PHYSFS_uint64 maxbuflen = 256 * 1024;
  1007. PHYSFS_uint64 len = pos - offset;
  1008. PHYSFS_uint8 *buf = NULL;
  1009. PHYSFS_sint32 i;
  1010. if (len > maxbuflen)
  1011. len = maxbuflen;
  1012. buf = (PHYSFS_uint8 *) __PHYSFS_smallAlloc(len);
  1013. BAIL_IF(!buf, PHYSFS_ERR_OUT_OF_MEMORY, -1);
  1014. if (!io->seek(io, pos - len) || !__PHYSFS_readAll(io, buf, len))
  1015. {
  1016. __PHYSFS_smallFree(buf);
  1017. return -1; /* error was set elsewhere. */
  1018. } /* if */
  1019. for (i = (PHYSFS_sint32) (len - 4); i >= 0; i--)
  1020. {
  1021. if ( (buf[i] == 0x50) && (buf[i+1] == 0x4b) &&
  1022. (buf[i+2] == 0x06) && (buf[i+3] == 0x06) )
  1023. {
  1024. __PHYSFS_smallFree(buf);
  1025. return pos - (len - i);
  1026. } /* if */
  1027. } /* for */
  1028. __PHYSFS_smallFree(buf);
  1029. } /* if */
  1030. BAIL(PHYSFS_ERR_CORRUPT, -1); /* didn't find it. */
  1031. } /* zip64_find_end_of_central_dir */
  1032. static int zip64_parse_end_of_central_dir(ZIPinfo *info,
  1033. PHYSFS_uint64 *data_start,
  1034. PHYSFS_uint64 *dir_ofs,
  1035. PHYSFS_uint64 *entry_count,
  1036. PHYSFS_sint64 pos)
  1037. {
  1038. PHYSFS_Io *io = info->io;
  1039. PHYSFS_uint64 ui64;
  1040. PHYSFS_uint32 ui32;
  1041. PHYSFS_uint16 ui16;
  1042. /* We should be positioned right past the locator signature. */
  1043. if ((pos < 0) || (!io->seek(io, pos)))
  1044. return 0;
  1045. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1046. if (ui32 != ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIG)
  1047. return -1; /* it's not a Zip64 archive. Not an error, though! */
  1048. info->zip64 = 1;
  1049. /* number of the disk with the start of the central directory. */
  1050. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1051. BAIL_IF(ui32 != 0, PHYSFS_ERR_CORRUPT, 0);
  1052. /* offset of Zip64 end of central directory record. */
  1053. BAIL_IF_ERRPASS(!readui64(io, &ui64), 0);
  1054. /* total number of disks */
  1055. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1056. BAIL_IF(ui32 != 1, PHYSFS_ERR_CORRUPT, 0);
  1057. pos = zip64_find_end_of_central_dir(io, pos, ui64);
  1058. if (pos < 0)
  1059. return 0; /* oh well. */
  1060. /*
  1061. * For self-extracting archives, etc, there's crapola in the file
  1062. * before the zipfile records; we calculate how much data there is
  1063. * prepended by determining how far the zip64-end-of-central-directory
  1064. * offset is from where it is supposed to be...the difference in bytes
  1065. * is how much arbitrary data is at the start of the physical file.
  1066. */
  1067. assert(((PHYSFS_uint64) pos) >= ui64);
  1068. *data_start = ((PHYSFS_uint64) pos) - ui64;
  1069. BAIL_IF_ERRPASS(!io->seek(io, pos), 0);
  1070. /* check signature again, just in case. */
  1071. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1072. BAIL_IF(ui32 != ZIP64_END_OF_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, 0);
  1073. /* size of Zip64 end of central directory record. */
  1074. BAIL_IF_ERRPASS(!readui64(io, &ui64), 0);
  1075. /* version made by. */
  1076. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  1077. /* version needed to extract. */
  1078. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  1079. /* number of this disk. */
  1080. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1081. BAIL_IF(ui32 != 0, PHYSFS_ERR_CORRUPT, 0);
  1082. /* number of disk with start of central directory record. */
  1083. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1084. BAIL_IF(ui32 != 0, PHYSFS_ERR_CORRUPT, 0);
  1085. /* total number of entries in the central dir on this disk */
  1086. BAIL_IF_ERRPASS(!readui64(io, &ui64), 0);
  1087. /* total number of entries in the central dir */
  1088. BAIL_IF_ERRPASS(!readui64(io, entry_count), 0);
  1089. BAIL_IF(ui64 != *entry_count, PHYSFS_ERR_CORRUPT, 0);
  1090. /* size of the central directory */
  1091. BAIL_IF_ERRPASS(!readui64(io, &ui64), 0);
  1092. /* offset of central directory */
  1093. BAIL_IF_ERRPASS(!readui64(io, dir_ofs), 0);
  1094. /* Since we know the difference, fix up the central dir offset... */
  1095. *dir_ofs += *data_start;
  1096. /*
  1097. * There are more fields here, for encryption and feature-specific things,
  1098. * but we don't care about any of them at the moment.
  1099. */
  1100. return 1; /* made it. */
  1101. } /* zip64_parse_end_of_central_dir */
  1102. static int zip_parse_end_of_central_dir(ZIPinfo *info,
  1103. PHYSFS_uint64 *data_start,
  1104. PHYSFS_uint64 *dir_ofs,
  1105. PHYSFS_uint64 *entry_count)
  1106. {
  1107. PHYSFS_Io *io = info->io;
  1108. PHYSFS_uint16 entryCount16;
  1109. PHYSFS_uint32 offset32;
  1110. PHYSFS_uint32 ui32;
  1111. PHYSFS_uint16 ui16;
  1112. PHYSFS_sint64 len;
  1113. PHYSFS_sint64 pos;
  1114. int rc;
  1115. /* find the end-of-central-dir record, and seek to it. */
  1116. pos = zip_find_end_of_central_dir(io, &len);
  1117. BAIL_IF_ERRPASS(pos == -1, 0);
  1118. BAIL_IF_ERRPASS(!io->seek(io, pos), 0);
  1119. /* check signature again, just in case. */
  1120. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1121. BAIL_IF(ui32 != ZIP_END_OF_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, 0);
  1122. /* Seek back to see if "Zip64 end of central directory locator" exists. */
  1123. /* this record is 20 bytes before end-of-central-dir */
  1124. rc = zip64_parse_end_of_central_dir(info, data_start, dir_ofs,
  1125. entry_count, pos - 20);
  1126. /* Error or success? Bounce out of here. Keep going if not zip64. */
  1127. if ((rc == 0) || (rc == 1))
  1128. return rc;
  1129. assert(rc == -1); /* no error, just not a Zip64 archive. */
  1130. /* Not Zip64? Seek back to where we were and keep processing. */
  1131. BAIL_IF_ERRPASS(!io->seek(io, pos + 4), 0);
  1132. /* number of this disk */
  1133. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  1134. BAIL_IF(ui16 != 0, PHYSFS_ERR_CORRUPT, 0);
  1135. /* number of the disk with the start of the central directory */
  1136. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  1137. BAIL_IF(ui16 != 0, PHYSFS_ERR_CORRUPT, 0);
  1138. /* total number of entries in the central dir on this disk */
  1139. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  1140. /* total number of entries in the central dir */
  1141. BAIL_IF_ERRPASS(!readui16(io, &entryCount16), 0);
  1142. BAIL_IF(ui16 != entryCount16, PHYSFS_ERR_CORRUPT, 0);
  1143. *entry_count = entryCount16;
  1144. /* size of the central directory */
  1145. BAIL_IF_ERRPASS(!readui32(io, &ui32), 0);
  1146. /* offset of central directory */
  1147. BAIL_IF_ERRPASS(!readui32(io, &offset32), 0);
  1148. *dir_ofs = (PHYSFS_uint64) offset32;
  1149. BAIL_IF(((PHYSFS_uint64) pos) < (*dir_ofs + ui32), PHYSFS_ERR_CORRUPT, 0);
  1150. /*
  1151. * For self-extracting archives, etc, there's crapola in the file
  1152. * before the zipfile records; we calculate how much data there is
  1153. * prepended by determining how far the central directory offset is
  1154. * from where it is supposed to be (start of end-of-central-dir minus
  1155. * sizeof central dir)...the difference in bytes is how much arbitrary
  1156. * data is at the start of the physical file.
  1157. */
  1158. *data_start = (PHYSFS_uint64) (pos - (*dir_ofs + ui32));
  1159. /* Now that we know the difference, fix up the central dir offset... */
  1160. *dir_ofs += *data_start;
  1161. /* zipfile comment length */
  1162. BAIL_IF_ERRPASS(!readui16(io, &ui16), 0);
  1163. /*
  1164. * Make sure that the comment length matches to the end of file...
  1165. * If it doesn't, we're either in the wrong part of the file, or the
  1166. * file is corrupted, but we give up either way.
  1167. */
  1168. BAIL_IF((pos + 22 + ui16) != len, PHYSFS_ERR_CORRUPT, 0);
  1169. return 1; /* made it. */
  1170. } /* zip_parse_end_of_central_dir */
  1171. static void ZIP_closeArchive(void *opaque)
  1172. {
  1173. ZIPinfo *info = (ZIPinfo *) (opaque);
  1174. if (!info)
  1175. return;
  1176. if (info->io)
  1177. info->io->destroy(info->io);
  1178. __PHYSFS_DirTreeDeinit(&info->tree);
  1179. allocator.Free(info);
  1180. } /* ZIP_closeArchive */
  1181. static void *ZIP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
  1182. {
  1183. ZIPinfo *info = NULL;
  1184. ZIPentry *root = NULL;
  1185. PHYSFS_uint64 dstart = 0; /* data start */
  1186. PHYSFS_uint64 cdir_ofs; /* central dir offset */
  1187. PHYSFS_uint64 count;
  1188. assert(io != NULL); /* shouldn't ever happen. */
  1189. BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
  1190. BAIL_IF_ERRPASS(!isZip(io), NULL);
  1191. info = (ZIPinfo *) allocator.Malloc(sizeof (ZIPinfo));
  1192. BAIL_IF(!info, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  1193. memset(info, '\0', sizeof (ZIPinfo));
  1194. info->io = io;
  1195. if (!zip_parse_end_of_central_dir(info, &dstart, &cdir_ofs, &count))
  1196. goto ZIP_openarchive_failed;
  1197. else if (!__PHYSFS_DirTreeInit(&info->tree, sizeof (ZIPentry)))
  1198. goto ZIP_openarchive_failed;
  1199. root = (ZIPentry *) info->tree.root;
  1200. root->resolved = ZIP_DIRECTORY;
  1201. if (!zip_load_entries(info, dstart, cdir_ofs, count))
  1202. goto ZIP_openarchive_failed;
  1203. assert(info->tree.root->sibling == NULL);
  1204. return info;
  1205. ZIP_openarchive_failed:
  1206. info->io = NULL; /* don't let ZIP_closeArchive destroy (io). */
  1207. ZIP_closeArchive(info);
  1208. return NULL;
  1209. } /* ZIP_openArchive */
  1210. static PHYSFS_Io *zip_get_io(PHYSFS_Io *io, ZIPinfo *inf, ZIPentry *entry)
  1211. {
  1212. int success;
  1213. PHYSFS_Io *retval = io->duplicate(io);
  1214. BAIL_IF_ERRPASS(!retval, NULL);
  1215. assert(!entry->tree.isdir); /* should have been checked before calling. */
  1216. /* (inf) can be NULL if we already resolved. */
  1217. success = (inf == NULL) || zip_resolve(retval, inf, entry);
  1218. if (success)
  1219. {
  1220. PHYSFS_sint64 offset;
  1221. offset = ((entry->symlink) ? entry->symlink->offset : entry->offset);
  1222. success = retval->seek(retval, offset);
  1223. } /* if */
  1224. if (!success)
  1225. {
  1226. retval->destroy(retval);
  1227. retval = NULL;
  1228. } /* if */
  1229. return retval;
  1230. } /* zip_get_io */
  1231. static PHYSFS_Io *ZIP_openRead(void *opaque, const char *filename)
  1232. {
  1233. PHYSFS_Io *retval = NULL;
  1234. ZIPinfo *info = (ZIPinfo *) opaque;
  1235. ZIPentry *entry = zip_find_entry(info, filename);
  1236. ZIPfileinfo *finfo = NULL;
  1237. PHYSFS_Io *io = NULL;
  1238. PHYSFS_uint8 *password = NULL;
  1239. /* if not found, see if maybe "$PASSWORD" is appended. */
  1240. if ((!entry) && (info->has_crypto))
  1241. {
  1242. const char *ptr = strrchr(filename, '$');
  1243. if (ptr != NULL)
  1244. {
  1245. const PHYSFS_uint64 len = (PHYSFS_uint64) (ptr - filename);
  1246. char *str = (char *) __PHYSFS_smallAlloc(len + 1);
  1247. BAIL_IF(!str, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  1248. memcpy(str, filename, (size_t) len);
  1249. str[len] = '\0';
  1250. entry = zip_find_entry(info, str);
  1251. __PHYSFS_smallFree(str);
  1252. password = (PHYSFS_uint8 *) (ptr + 1);
  1253. } /* if */
  1254. } /* if */
  1255. BAIL_IF_ERRPASS(!entry, NULL);
  1256. BAIL_IF(entry->tree.isdir, PHYSFS_ERR_NOT_A_FILE, NULL);
  1257. retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  1258. GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed);
  1259. finfo = (ZIPfileinfo *) allocator.Malloc(sizeof (ZIPfileinfo));
  1260. GOTO_IF(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed);
  1261. memset(finfo, '\0', sizeof (ZIPfileinfo));
  1262. io = zip_get_io(info->io, info, entry);
  1263. GOTO_IF_ERRPASS(!io, ZIP_openRead_failed);
  1264. finfo->io = io;
  1265. finfo->entry = ((entry->symlink != NULL) ? entry->symlink : entry);
  1266. initializeZStream(&finfo->stream);
  1267. if (finfo->entry->compression_method != COMPMETH_NONE)
  1268. {
  1269. finfo->buffer = (PHYSFS_uint8 *) allocator.Malloc(ZIP_READBUFSIZE);
  1270. if (!finfo->buffer)
  1271. GOTO(PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed);
  1272. else if (zlib_err(inflateInit2(&finfo->stream, -MAX_WBITS)) != Z_OK)
  1273. goto ZIP_openRead_failed;
  1274. } /* if */
  1275. if (!zip_entry_is_tradional_crypto(entry))
  1276. GOTO_IF(password != NULL, PHYSFS_ERR_BAD_PASSWORD, ZIP_openRead_failed);
  1277. else
  1278. {
  1279. PHYSFS_uint8 crypto_header[12];
  1280. GOTO_IF(password == NULL, PHYSFS_ERR_BAD_PASSWORD, ZIP_openRead_failed);
  1281. if (io->read(io, crypto_header, 12) != 12)
  1282. goto ZIP_openRead_failed;
  1283. else if (!zip_prep_crypto_keys(finfo, crypto_header, password))
  1284. goto ZIP_openRead_failed;
  1285. } /* if */
  1286. memcpy(retval, &ZIP_Io, sizeof (PHYSFS_Io));
  1287. retval->opaque = finfo;
  1288. return retval;
  1289. ZIP_openRead_failed:
  1290. if (finfo != NULL)
  1291. {
  1292. if (finfo->io != NULL)
  1293. finfo->io->destroy(finfo->io);
  1294. if (finfo->buffer != NULL)
  1295. {
  1296. allocator.Free(finfo->buffer);
  1297. inflateEnd(&finfo->stream);
  1298. } /* if */
  1299. allocator.Free(finfo);
  1300. } /* if */
  1301. if (retval != NULL)
  1302. allocator.Free(retval);
  1303. return NULL;
  1304. } /* ZIP_openRead */
  1305. static PHYSFS_Io *ZIP_openWrite(void *opaque, const char *filename)
  1306. {
  1307. BAIL(PHYSFS_ERR_READ_ONLY, NULL);
  1308. } /* ZIP_openWrite */
  1309. static PHYSFS_Io *ZIP_openAppend(void *opaque, const char *filename)
  1310. {
  1311. BAIL(PHYSFS_ERR_READ_ONLY, NULL);
  1312. } /* ZIP_openAppend */
  1313. static int ZIP_remove(void *opaque, const char *name)
  1314. {
  1315. BAIL(PHYSFS_ERR_READ_ONLY, 0);
  1316. } /* ZIP_remove */
  1317. static int ZIP_mkdir(void *opaque, const char *name)
  1318. {
  1319. BAIL(PHYSFS_ERR_READ_ONLY, 0);
  1320. } /* ZIP_mkdir */
  1321. static int ZIP_stat(void *opaque, const char *filename, PHYSFS_Stat *stat)
  1322. {
  1323. ZIPinfo *info = (ZIPinfo *) opaque;
  1324. const ZIPentry *entry = zip_find_entry(info, filename);
  1325. if (entry == NULL)
  1326. return 0;
  1327. else if (entry->resolved == ZIP_DIRECTORY)
  1328. {
  1329. stat->filesize = 0;
  1330. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  1331. } /* if */
  1332. else if (zip_entry_is_symlink(entry))
  1333. {
  1334. stat->filesize = 0;
  1335. stat->filetype = PHYSFS_FILETYPE_SYMLINK;
  1336. } /* else if */
  1337. else
  1338. {
  1339. stat->filesize = (PHYSFS_sint64) entry->uncompressed_size;
  1340. stat->filetype = PHYSFS_FILETYPE_REGULAR;
  1341. } /* else */
  1342. stat->modtime = ((entry) ? entry->last_mod_time : 0);
  1343. stat->createtime = stat->modtime;
  1344. stat->accesstime = -1;
  1345. stat->readonly = 1; /* .zip files are always read only */
  1346. return 1;
  1347. } /* ZIP_stat */
  1348. const PHYSFS_Archiver __PHYSFS_Archiver_ZIP =
  1349. {
  1350. CURRENT_PHYSFS_ARCHIVER_API_VERSION,
  1351. {
  1352. "ZIP",
  1353. "PkZip/WinZip/Info-Zip compatible",
  1354. "Ryan C. Gordon <icculus@icculus.org>",
  1355. "https://icculus.org/physfs/",
  1356. 1, /* supportsSymlinks */
  1357. },
  1358. ZIP_openArchive,
  1359. __PHYSFS_DirTreeEnumerateFiles,
  1360. ZIP_openRead,
  1361. ZIP_openWrite,
  1362. ZIP_openAppend,
  1363. ZIP_remove,
  1364. ZIP_mkdir,
  1365. ZIP_stat,
  1366. ZIP_closeArchive
  1367. };
  1368. #endif /* defined PHYSFS_SUPPORTS_ZIP */
  1369. /* end of physfs_archiver_zip.c ... */