zip.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. /*
  2. * ZIP support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE 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. #if HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #if (defined PHYSFS_SUPPORTS_ZIP)
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #ifndef _WIN32_WCE
  17. #include <errno.h>
  18. #include <time.h>
  19. #endif
  20. #include "physfs.h"
  21. #include "zlib.h"
  22. #define __PHYSICSFS_INTERNAL__
  23. #include "physfs_internal.h"
  24. /*
  25. * A buffer of ZIP_READBUFSIZE is malloc() for each compressed file opened,
  26. * and is free()'d when you close the file; compressed data is read into
  27. * this buffer, and then is decompressed into the buffer passed to
  28. * PHYSFS_read().
  29. *
  30. * Uncompressed entries in a zipfile do not allocate this buffer; they just
  31. * read data directly into the buffer passed to PHYSFS_read().
  32. *
  33. * Depending on your speed and memory requirements, you should tweak this
  34. * value.
  35. */
  36. #define ZIP_READBUFSIZE (16 * 1024)
  37. /*
  38. * Entries are "unresolved" until they are first opened. At that time,
  39. * local file headers parsed/validated, data offsets will be updated to look
  40. * at the actual file data instead of the header, and symlinks will be
  41. * followed and optimized. This means that we don't seek and read around the
  42. * archive until forced to do so, and after the first time, we had to do
  43. * less reading and parsing, which is very CD-ROM friendly.
  44. */
  45. typedef enum
  46. {
  47. ZIP_UNRESOLVED_FILE,
  48. ZIP_UNRESOLVED_SYMLINK,
  49. ZIP_RESOLVING,
  50. ZIP_RESOLVED,
  51. ZIP_BROKEN_FILE,
  52. ZIP_BROKEN_SYMLINK
  53. } ZipResolveType;
  54. /*
  55. * One ZIPentry is kept for each file in an open ZIP archive.
  56. */
  57. typedef struct _ZIPentry
  58. {
  59. char *name; /* Name of file in archive */
  60. struct _ZIPentry *symlink; /* NULL or file we symlink to */
  61. ZipResolveType resolved; /* Have we resolved file/symlink? */
  62. PHYSFS_uint32 offset; /* offset of data in archive */
  63. PHYSFS_uint16 version; /* version made by */
  64. PHYSFS_uint16 version_needed; /* version needed to extract */
  65. PHYSFS_uint16 compression_method; /* compression method */
  66. PHYSFS_uint32 crc; /* crc-32 */
  67. PHYSFS_uint32 compressed_size; /* compressed size */
  68. PHYSFS_uint32 uncompressed_size; /* uncompressed size */
  69. PHYSFS_sint64 last_mod_time; /* last file mod time */
  70. } ZIPentry;
  71. /*
  72. * One ZIPinfo is kept for each open ZIP archive.
  73. */
  74. typedef struct
  75. {
  76. char *archiveName; /* path to ZIP in platform-dependent notation. */
  77. PHYSFS_uint16 entryCount; /* Number of files in ZIP. */
  78. ZIPentry *entries; /* info on all files in ZIP. */
  79. } ZIPinfo;
  80. /*
  81. * One ZIPfileinfo is kept for each open file in a ZIP archive.
  82. */
  83. typedef struct
  84. {
  85. ZIPentry *entry; /* Info on file. */
  86. void *handle; /* physical file handle. */
  87. PHYSFS_uint32 compressed_position; /* offset in compressed data. */
  88. PHYSFS_uint32 uncompressed_position; /* tell() position. */
  89. PHYSFS_uint8 *buffer; /* decompression buffer. */
  90. z_stream stream; /* zlib stream state. */
  91. } ZIPfileinfo;
  92. /* Magic numbers... */
  93. #define ZIP_LOCAL_FILE_SIG 0x04034b50
  94. #define ZIP_CENTRAL_DIR_SIG 0x02014b50
  95. #define ZIP_END_OF_CENTRAL_DIR_SIG 0x06054b50
  96. /* compression methods... */
  97. #define COMPMETH_NONE 0
  98. /* ...and others... */
  99. #define UNIX_FILETYPE_MASK 0170000
  100. #define UNIX_FILETYPE_SYMLINK 0120000
  101. static PHYSFS_sint64 ZIP_read(fvoid *opaque, void *buffer,
  102. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  103. static PHYSFS_sint64 ZIP_write(fvoid *opaque, const void *buffer,
  104. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  105. static int ZIP_eof(fvoid *opaque);
  106. static PHYSFS_sint64 ZIP_tell(fvoid *opaque);
  107. static int ZIP_seek(fvoid *opaque, PHYSFS_uint64 offset);
  108. static PHYSFS_sint64 ZIP_fileLength(fvoid *opaque);
  109. static int ZIP_fileClose(fvoid *opaque);
  110. static int ZIP_isArchive(const char *filename, int forWriting);
  111. static void *ZIP_openArchive(const char *name, int forWriting);
  112. static LinkedStringList *ZIP_enumerateFiles(dvoid *opaque,
  113. const char *dirname,
  114. int omitSymLinks);
  115. static int ZIP_exists(dvoid *opaque, const char *name);
  116. static int ZIP_isDirectory(dvoid *opaque, const char *name, int *fileExists);
  117. static int ZIP_isSymLink(dvoid *opaque, const char *name, int *fileExists);
  118. static PHYSFS_sint64 ZIP_getLastModTime(dvoid *opaque, const char *n, int *e);
  119. static fvoid *ZIP_openRead(dvoid *opaque, const char *filename, int *e);
  120. static fvoid *ZIP_openWrite(dvoid *opaque, const char *filename);
  121. static fvoid *ZIP_openAppend(dvoid *opaque, const char *filename);
  122. static void ZIP_dirClose(dvoid *opaque);
  123. static int zip_resolve(void *in, ZIPinfo *info, ZIPentry *entry);
  124. static int ZIP_remove(dvoid *opaque, const char *name);
  125. static int ZIP_mkdir(dvoid *opaque, const char *name);
  126. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP =
  127. {
  128. "ZIP",
  129. ZIP_ARCHIVE_DESCRIPTION,
  130. "Ryan C. Gordon <icculus@clutteredmind.org>",
  131. "http://icculus.org/physfs/",
  132. };
  133. const PHYSFS_Archiver __PHYSFS_Archiver_ZIP =
  134. {
  135. &__PHYSFS_ArchiveInfo_ZIP,
  136. ZIP_isArchive, /* isArchive() method */
  137. ZIP_openArchive, /* openArchive() method */
  138. ZIP_enumerateFiles, /* enumerateFiles() method */
  139. ZIP_exists, /* exists() method */
  140. ZIP_isDirectory, /* isDirectory() method */
  141. ZIP_isSymLink, /* isSymLink() method */
  142. ZIP_getLastModTime, /* getLastModTime() method */
  143. ZIP_openRead, /* openRead() method */
  144. ZIP_openWrite, /* openWrite() method */
  145. ZIP_openAppend, /* openAppend() method */
  146. ZIP_remove, /* remove() method */
  147. ZIP_mkdir, /* mkdir() method */
  148. ZIP_dirClose, /* dirClose() method */
  149. ZIP_read, /* read() method */
  150. ZIP_write, /* write() method */
  151. ZIP_eof, /* eof() method */
  152. ZIP_tell, /* tell() method */
  153. ZIP_seek, /* seek() method */
  154. ZIP_fileLength, /* fileLength() method */
  155. ZIP_fileClose /* fileClose() method */
  156. };
  157. /*
  158. * Bridge physfs allocation functions to zlib's format...
  159. */
  160. static voidpf zlibPhysfsAlloc(voidpf opaque, uInt items, uInt size)
  161. {
  162. return(((PHYSFS_Allocator *) opaque)->malloc(items * size));
  163. } /* zlibPhysfsAlloc */
  164. /*
  165. * Bridge physfs allocation functions to zlib's format...
  166. */
  167. static void zlibPhysfsFree(voidpf opaque, voidpf address)
  168. {
  169. ((PHYSFS_Allocator *) opaque)->free(address);
  170. } /* zlibPhysfsFree */
  171. /*
  172. * Construct a new z_stream to a sane state.
  173. */
  174. static void initializeZStream(z_stream *pstr)
  175. {
  176. memset(pstr, '\0', sizeof (z_stream));
  177. pstr->zalloc = zlibPhysfsAlloc;
  178. pstr->zfree = zlibPhysfsFree;
  179. pstr->opaque = __PHYSFS_getAllocator();
  180. } /* initializeZStream */
  181. static const char *zlib_error_string(int rc)
  182. {
  183. switch (rc)
  184. {
  185. case Z_OK: return(NULL); /* not an error. */
  186. case Z_STREAM_END: return(NULL); /* not an error. */
  187. #ifndef _WIN32_WCE
  188. case Z_ERRNO: return(strerror(errno));
  189. #endif
  190. case Z_NEED_DICT: return(ERR_ZLIB_NEED_DICT);
  191. case Z_DATA_ERROR: return(ERR_ZLIB_DATA_ERROR);
  192. case Z_MEM_ERROR: return(ERR_ZLIB_MEMORY_ERROR);
  193. case Z_BUF_ERROR: return(ERR_ZLIB_BUFFER_ERROR);
  194. case Z_VERSION_ERROR: return(ERR_ZLIB_VERSION_ERROR);
  195. default: return(ERR_ZLIB_UNKNOWN_ERROR);
  196. } /* switch */
  197. return(NULL);
  198. } /* zlib_error_string */
  199. /*
  200. * Wrap all zlib calls in this, so the physfs error state is set appropriately.
  201. */
  202. static int zlib_err(int rc)
  203. {
  204. const char *str = zlib_error_string(rc);
  205. if (str != NULL)
  206. __PHYSFS_setError(str);
  207. return(rc);
  208. } /* zlib_err */
  209. /*
  210. * Read an unsigned 32-bit int and swap to native byte order.
  211. */
  212. static int readui32(void *in, PHYSFS_uint32 *val)
  213. {
  214. PHYSFS_uint32 v;
  215. BAIL_IF_MACRO(__PHYSFS_platformRead(in, &v, sizeof (v), 1) != 1, NULL, 0);
  216. *val = PHYSFS_swapULE32(v);
  217. return(1);
  218. } /* readui32 */
  219. /*
  220. * Read an unsigned 16-bit int and swap to native byte order.
  221. */
  222. static int readui16(void *in, PHYSFS_uint16 *val)
  223. {
  224. PHYSFS_uint16 v;
  225. BAIL_IF_MACRO(__PHYSFS_platformRead(in, &v, sizeof (v), 1) != 1, NULL, 0);
  226. *val = PHYSFS_swapULE16(v);
  227. return(1);
  228. } /* readui16 */
  229. static PHYSFS_sint64 ZIP_read(fvoid *opaque, void *buf,
  230. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  231. {
  232. ZIPfileinfo *finfo = (ZIPfileinfo *) opaque;
  233. ZIPentry *entry = finfo->entry;
  234. PHYSFS_sint64 retval = 0;
  235. PHYSFS_sint64 maxread = ((PHYSFS_sint64) objSize) * objCount;
  236. PHYSFS_sint64 avail = entry->uncompressed_size -
  237. finfo->uncompressed_position;
  238. BAIL_IF_MACRO(maxread == 0, NULL, 0); /* quick rejection. */
  239. if (avail < maxread)
  240. {
  241. maxread = avail - (avail % objSize);
  242. objCount = (PHYSFS_uint32) (maxread / objSize);
  243. BAIL_IF_MACRO(objCount == 0, ERR_PAST_EOF, 0); /* quick rejection. */
  244. __PHYSFS_setError(ERR_PAST_EOF); /* this is always true here. */
  245. } /* if */
  246. if (entry->compression_method == COMPMETH_NONE)
  247. {
  248. retval = __PHYSFS_platformRead(finfo->handle, buf, objSize, objCount);
  249. } /* if */
  250. else
  251. {
  252. finfo->stream.next_out = buf;
  253. finfo->stream.avail_out = objSize * objCount;
  254. while (retval < maxread)
  255. {
  256. PHYSFS_uint32 before = finfo->stream.total_out;
  257. int rc;
  258. if (finfo->stream.avail_in == 0)
  259. {
  260. PHYSFS_sint64 br;
  261. br = entry->compressed_size - finfo->compressed_position;
  262. if (br > 0)
  263. {
  264. if (br > ZIP_READBUFSIZE)
  265. br = ZIP_READBUFSIZE;
  266. br = __PHYSFS_platformRead(finfo->handle,
  267. finfo->buffer,
  268. 1, (PHYSFS_uint32) br);
  269. if (br <= 0)
  270. break;
  271. finfo->compressed_position += (PHYSFS_uint32) br;
  272. finfo->stream.next_in = finfo->buffer;
  273. finfo->stream.avail_in = (PHYSFS_uint32) br;
  274. } /* if */
  275. } /* if */
  276. rc = zlib_err(inflate(&finfo->stream, Z_SYNC_FLUSH));
  277. retval += (finfo->stream.total_out - before);
  278. if (rc != Z_OK)
  279. break;
  280. } /* while */
  281. retval /= objSize;
  282. } /* else */
  283. if (retval > 0)
  284. finfo->uncompressed_position += (PHYSFS_uint32) (retval * objSize);
  285. return(retval);
  286. } /* ZIP_read */
  287. static PHYSFS_sint64 ZIP_write(fvoid *opaque, const void *buf,
  288. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  289. {
  290. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  291. } /* ZIP_write */
  292. static int ZIP_eof(fvoid *opaque)
  293. {
  294. ZIPfileinfo *finfo = (ZIPfileinfo *) opaque;
  295. return(finfo->uncompressed_position >= finfo->entry->uncompressed_size);
  296. } /* ZIP_eof */
  297. static PHYSFS_sint64 ZIP_tell(fvoid *opaque)
  298. {
  299. return(((ZIPfileinfo *) opaque)->uncompressed_position);
  300. } /* ZIP_tell */
  301. static int ZIP_seek(fvoid *opaque, PHYSFS_uint64 offset)
  302. {
  303. ZIPfileinfo *finfo = (ZIPfileinfo *) opaque;
  304. ZIPentry *entry = finfo->entry;
  305. void *in = finfo->handle;
  306. BAIL_IF_MACRO(offset > entry->uncompressed_size, ERR_PAST_EOF, 0);
  307. if (entry->compression_method == COMPMETH_NONE)
  308. {
  309. PHYSFS_sint64 newpos = offset + entry->offset;
  310. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, newpos), NULL, 0);
  311. finfo->uncompressed_position = (PHYSFS_uint32) offset;
  312. } /* if */
  313. else
  314. {
  315. /*
  316. * If seeking backwards, we need to redecode the file
  317. * from the start and throw away the compressed bits until we hit
  318. * the offset we need. If seeking forward, we still need to
  319. * decode, but we don't rewind first.
  320. */
  321. if (offset < finfo->uncompressed_position)
  322. {
  323. /* we do a copy so state is sane if inflateInit2() fails. */
  324. z_stream str;
  325. initializeZStream(&str);
  326. if (zlib_err(inflateInit2(&str, -MAX_WBITS)) != Z_OK)
  327. return(0);
  328. if (!__PHYSFS_platformSeek(in, entry->offset))
  329. return(0);
  330. inflateEnd(&finfo->stream);
  331. memcpy(&finfo->stream, &str, sizeof (z_stream));
  332. finfo->uncompressed_position = finfo->compressed_position = 0;
  333. } /* if */
  334. while (finfo->uncompressed_position != offset)
  335. {
  336. PHYSFS_uint8 buf[512];
  337. PHYSFS_uint32 maxread;
  338. maxread = (PHYSFS_uint32) (offset - finfo->uncompressed_position);
  339. if (maxread > sizeof (buf))
  340. maxread = sizeof (buf);
  341. if (ZIP_read(finfo, buf, maxread, 1) != 1)
  342. return(0);
  343. } /* while */
  344. } /* else */
  345. return(1);
  346. } /* ZIP_seek */
  347. static PHYSFS_sint64 ZIP_fileLength(fvoid *opaque)
  348. {
  349. ZIPfileinfo *finfo = (ZIPfileinfo *) opaque;
  350. return(finfo->entry->uncompressed_size);
  351. } /* ZIP_fileLength */
  352. static int ZIP_fileClose(fvoid *opaque)
  353. {
  354. ZIPfileinfo *finfo = (ZIPfileinfo *) opaque;
  355. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  356. if (finfo->entry->compression_method != COMPMETH_NONE)
  357. inflateEnd(&finfo->stream);
  358. if (finfo->buffer != NULL)
  359. free(finfo->buffer);
  360. free(finfo);
  361. return(1);
  362. } /* ZIP_fileClose */
  363. static PHYSFS_sint64 zip_find_end_of_central_dir(void *in, PHYSFS_sint64 *len)
  364. {
  365. PHYSFS_uint8 buf[256];
  366. PHYSFS_sint32 i = 0;
  367. PHYSFS_sint64 filelen;
  368. PHYSFS_sint64 filepos;
  369. PHYSFS_sint32 maxread;
  370. PHYSFS_sint32 totalread = 0;
  371. int found = 0;
  372. PHYSFS_uint32 extra = 0;
  373. filelen = __PHYSFS_platformFileLength(in);
  374. BAIL_IF_MACRO(filelen == -1, NULL, 0);
  375. BAIL_IF_MACRO(filelen > 0xFFFFFFFF, "ZIP bigger than 2 gigs?!", 0);
  376. /*
  377. * Jump to the end of the file and start reading backwards.
  378. * The last thing in the file is the zipfile comment, which is variable
  379. * length, and the field that specifies its size is before it in the
  380. * file (argh!)...this means that we need to scan backwards until we
  381. * hit the end-of-central-dir signature. We can then sanity check that
  382. * the comment was as big as it should be to make sure we're in the
  383. * right place. The comment length field is 16 bits, so we can stop
  384. * searching for that signature after a little more than 64k at most,
  385. * and call it a corrupted zipfile.
  386. */
  387. if (sizeof (buf) < filelen)
  388. {
  389. filepos = filelen - sizeof (buf);
  390. maxread = sizeof (buf);
  391. } /* if */
  392. else
  393. {
  394. filepos = 0;
  395. maxread = (PHYSFS_uint32) filelen;
  396. } /* else */
  397. while ((totalread < filelen) && (totalread < 65557))
  398. {
  399. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, filepos), NULL, -1);
  400. /* make sure we catch a signature between buffers. */
  401. if (totalread != 0)
  402. {
  403. if (__PHYSFS_platformRead(in, buf, maxread - 4, 1) != 1)
  404. return(-1);
  405. *((PHYSFS_uint32 *) (&buf[maxread - 4])) = extra;
  406. totalread += maxread - 4;
  407. } /* if */
  408. else
  409. {
  410. if (__PHYSFS_platformRead(in, buf, maxread, 1) != 1)
  411. return(-1);
  412. totalread += maxread;
  413. } /* else */
  414. extra = *((PHYSFS_uint32 *) (&buf[0]));
  415. for (i = maxread - 4; i > 0; i--)
  416. {
  417. if ((buf[i + 0] == 0x50) &&
  418. (buf[i + 1] == 0x4B) &&
  419. (buf[i + 2] == 0x05) &&
  420. (buf[i + 3] == 0x06) )
  421. {
  422. found = 1; /* that's the signature! */
  423. break;
  424. } /* if */
  425. } /* for */
  426. if (found)
  427. break;
  428. filepos -= (maxread - 4);
  429. } /* while */
  430. BAIL_IF_MACRO(!found, ERR_NOT_AN_ARCHIVE, -1);
  431. if (len != NULL)
  432. *len = filelen;
  433. return(filepos + i);
  434. } /* zip_find_end_of_central_dir */
  435. static int ZIP_isArchive(const char *filename, int forWriting)
  436. {
  437. PHYSFS_uint32 sig;
  438. int retval = 0;
  439. void *in;
  440. in = __PHYSFS_platformOpenRead(filename);
  441. BAIL_IF_MACRO(in == NULL, NULL, 0);
  442. /*
  443. * The first thing in a zip file might be the signature of the
  444. * first local file record, so it makes for a quick determination.
  445. */
  446. if (readui32(in, &sig))
  447. {
  448. retval = (sig == ZIP_LOCAL_FILE_SIG);
  449. if (!retval)
  450. {
  451. /*
  452. * No sig...might be a ZIP with data at the start
  453. * (a self-extracting executable, etc), so we'll have to do
  454. * it the hard way...
  455. */
  456. retval = (zip_find_end_of_central_dir(in, NULL) != -1);
  457. } /* if */
  458. } /* if */
  459. __PHYSFS_platformClose(in);
  460. return(retval);
  461. } /* ZIP_isArchive */
  462. static void zip_free_entries(ZIPentry *entries, PHYSFS_uint32 max)
  463. {
  464. PHYSFS_uint32 i;
  465. for (i = 0; i < max; i++)
  466. {
  467. ZIPentry *entry = &entries[i];
  468. if (entry->name != NULL)
  469. free(entry->name);
  470. } /* for */
  471. free(entries);
  472. } /* zip_free_entries */
  473. /*
  474. * This will find the ZIPentry associated with a path in platform-independent
  475. * notation. Directories don't have ZIPentries associated with them, but
  476. * (*isDir) will be set to non-zero if a dir was hit.
  477. */
  478. static ZIPentry *zip_find_entry(ZIPinfo *info, const char *path, int *isDir)
  479. {
  480. ZIPentry *a = info->entries;
  481. PHYSFS_sint32 pathlen = strlen(path);
  482. PHYSFS_sint32 lo = 0;
  483. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  484. PHYSFS_sint32 middle;
  485. const char *thispath = NULL;
  486. int rc;
  487. while (lo <= hi)
  488. {
  489. middle = lo + ((hi - lo) / 2);
  490. thispath = a[middle].name;
  491. rc = strncmp(path, thispath, pathlen);
  492. if (rc > 0)
  493. lo = middle + 1;
  494. else if (rc < 0)
  495. hi = middle - 1;
  496. else /* substring match...might be dir or entry or nothing. */
  497. {
  498. if (isDir != NULL)
  499. {
  500. *isDir = (thispath[pathlen] == '/');
  501. if (*isDir)
  502. return(NULL);
  503. } /* if */
  504. if (thispath[pathlen] == '\0') /* found entry? */
  505. return(&a[middle]);
  506. else
  507. hi = middle - 1; /* adjust search params, try again. */
  508. } /* if */
  509. } /* while */
  510. if (isDir != NULL)
  511. *isDir = 0;
  512. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  513. } /* zip_find_entry */
  514. /* Convert paths from old, buggy DOS zippers... */
  515. static void zip_convert_dos_path(ZIPentry *entry, char *path)
  516. {
  517. PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((entry->version >> 8) & 0xFF);
  518. if (hosttype == 0) /* FS_FAT_ */
  519. {
  520. while (*path)
  521. {
  522. if (*path == '\\')
  523. *path = '/';
  524. path++;
  525. } /* while */
  526. } /* if */
  527. } /* zip_convert_dos_path */
  528. static void zip_expand_symlink_path(char *path)
  529. {
  530. char *ptr = path;
  531. char *prevptr = path;
  532. while (1)
  533. {
  534. ptr = strchr(ptr, '/');
  535. if (ptr == NULL)
  536. break;
  537. if (*(ptr + 1) == '.')
  538. {
  539. if (*(ptr + 2) == '/')
  540. {
  541. /* current dir in middle of string: ditch it. */
  542. memmove(ptr, ptr + 2, strlen(ptr + 2) + 1);
  543. } /* else if */
  544. else if (*(ptr + 2) == '\0')
  545. {
  546. /* current dir at end of string: ditch it. */
  547. *ptr = '\0';
  548. } /* else if */
  549. else if (*(ptr + 2) == '.')
  550. {
  551. if (*(ptr + 3) == '/')
  552. {
  553. /* parent dir in middle: move back one, if possible. */
  554. memmove(prevptr, ptr + 4, strlen(ptr + 4) + 1);
  555. ptr = prevptr;
  556. while (prevptr != path)
  557. {
  558. prevptr--;
  559. if (*prevptr == '/')
  560. {
  561. prevptr++;
  562. break;
  563. } /* if */
  564. } /* while */
  565. } /* if */
  566. if (*(ptr + 3) == '\0')
  567. {
  568. /* parent dir at end: move back one, if possible. */
  569. *prevptr = '\0';
  570. } /* if */
  571. } /* if */
  572. } /* if */
  573. else
  574. {
  575. prevptr = ptr;
  576. } /* else */
  577. } /* while */
  578. } /* zip_expand_symlink_path */
  579. /*
  580. * Look for the entry named by (path). If it exists, resolve it, and return
  581. * a pointer to that entry. If it's another symlink, keep resolving until you
  582. * hit a real file and then return a pointer to the final non-symlink entry.
  583. * If there's a problem, return NULL. (path) is always free()'d by this
  584. * function.
  585. */
  586. static ZIPentry *zip_follow_symlink(void *in, ZIPinfo *info, char *path)
  587. {
  588. ZIPentry *entry;
  589. zip_expand_symlink_path(path);
  590. entry = zip_find_entry(info, path, NULL);
  591. if (entry != NULL)
  592. {
  593. if (!zip_resolve(in, info, entry)) /* recursive! */
  594. entry = NULL;
  595. else
  596. {
  597. if (entry->symlink != NULL)
  598. entry = entry->symlink;
  599. } /* else */
  600. } /* if */
  601. free(path);
  602. return(entry);
  603. } /* zip_follow_symlink */
  604. static int zip_resolve_symlink(void *in, ZIPinfo *info, ZIPentry *entry)
  605. {
  606. char *path;
  607. PHYSFS_uint32 size = entry->uncompressed_size;
  608. int rc = 0;
  609. /*
  610. * We've already parsed the local file header of the symlink at this
  611. * point. Now we need to read the actual link from the file data and
  612. * follow it.
  613. */
  614. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, entry->offset), NULL, 0);
  615. path = (char *) malloc(size + 1);
  616. BAIL_IF_MACRO(path == NULL, ERR_OUT_OF_MEMORY, 0);
  617. if (entry->compression_method == COMPMETH_NONE)
  618. rc = (__PHYSFS_platformRead(in, path, size, 1) == 1);
  619. else /* symlink target path is compressed... */
  620. {
  621. z_stream stream;
  622. PHYSFS_uint32 compsize = entry->compressed_size;
  623. PHYSFS_uint8 *compressed = (PHYSFS_uint8 *) malloc(compsize);
  624. if (compressed != NULL)
  625. {
  626. if (__PHYSFS_platformRead(in, compressed, compsize, 1) == 1)
  627. {
  628. initializeZStream(&stream);
  629. stream.next_in = compressed;
  630. stream.avail_in = compsize;
  631. stream.next_out = (unsigned char *) path;
  632. stream.avail_out = size;
  633. if (zlib_err(inflateInit2(&stream, -MAX_WBITS)) == Z_OK)
  634. {
  635. rc = zlib_err(inflate(&stream, Z_FINISH));
  636. inflateEnd(&stream);
  637. /* both are acceptable outcomes... */
  638. rc = ((rc == Z_OK) || (rc == Z_STREAM_END));
  639. } /* if */
  640. } /* if */
  641. free(compressed);
  642. } /* if */
  643. } /* else */
  644. if (!rc)
  645. free(path);
  646. else
  647. {
  648. path[entry->uncompressed_size] = '\0'; /* null-terminate it. */
  649. zip_convert_dos_path(entry, path);
  650. entry->symlink = zip_follow_symlink(in, info, path);
  651. } /* else */
  652. return(entry->symlink != NULL);
  653. } /* zip_resolve_symlink */
  654. /*
  655. * Parse the local file header of an entry, and update entry->offset.
  656. */
  657. static int zip_parse_local(void *in, ZIPentry *entry)
  658. {
  659. PHYSFS_uint32 ui32;
  660. PHYSFS_uint16 ui16;
  661. PHYSFS_uint16 fnamelen;
  662. PHYSFS_uint16 extralen;
  663. /*
  664. * crc and (un)compressed_size are always zero if this is a "JAR"
  665. * archive created with Sun's Java tools, apparently. We only
  666. * consider this archive corrupted if those entries don't match and
  667. * aren't zero. That seems to work well.
  668. */
  669. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, entry->offset), NULL, 0);
  670. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  671. BAIL_IF_MACRO(ui32 != ZIP_LOCAL_FILE_SIG, ERR_CORRUPTED, 0);
  672. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  673. BAIL_IF_MACRO(ui16 != entry->version_needed, ERR_CORRUPTED, 0);
  674. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* general bits. */
  675. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  676. BAIL_IF_MACRO(ui16 != entry->compression_method, ERR_CORRUPTED, 0);
  677. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0); /* date/time */
  678. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  679. BAIL_IF_MACRO(ui32 && (ui32 != entry->crc), ERR_CORRUPTED, 0);
  680. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  681. BAIL_IF_MACRO(ui32 && (ui32 != entry->compressed_size), ERR_CORRUPTED, 0);
  682. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  683. BAIL_IF_MACRO(ui32 && (ui32 != entry->uncompressed_size),ERR_CORRUPTED,0);
  684. BAIL_IF_MACRO(!readui16(in, &fnamelen), NULL, 0);
  685. BAIL_IF_MACRO(!readui16(in, &extralen), NULL, 0);
  686. entry->offset += fnamelen + extralen + 30;
  687. return(1);
  688. } /* zip_parse_local */
  689. static int zip_resolve(void *in, ZIPinfo *info, ZIPentry *entry)
  690. {
  691. int retval = 1;
  692. ZipResolveType resolve_type = entry->resolved;
  693. /* Don't bother if we've failed to resolve this entry before. */
  694. BAIL_IF_MACRO(resolve_type == ZIP_BROKEN_FILE, ERR_CORRUPTED, 0);
  695. BAIL_IF_MACRO(resolve_type == ZIP_BROKEN_SYMLINK, ERR_CORRUPTED, 0);
  696. /* uhoh...infinite symlink loop! */
  697. BAIL_IF_MACRO(resolve_type == ZIP_RESOLVING, ERR_SYMLINK_LOOP, 0);
  698. /*
  699. * We fix up the offset to point to the actual data on the
  700. * first open, since we don't want to seek across the whole file on
  701. * archive open (can be SLOW on large, CD-stored files), but we
  702. * need to check the local file header...not just for corruption,
  703. * but since it stores offset info the central directory does not.
  704. */
  705. if (resolve_type != ZIP_RESOLVED)
  706. {
  707. entry->resolved = ZIP_RESOLVING;
  708. retval = zip_parse_local(in, entry);
  709. if (retval)
  710. {
  711. /*
  712. * If it's a symlink, find the original file. This will cause
  713. * resolution of other entries (other symlinks and, eventually,
  714. * the real file) if all goes well.
  715. */
  716. if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
  717. retval = zip_resolve_symlink(in, info, entry);
  718. } /* if */
  719. if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
  720. entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_SYMLINK);
  721. else if (resolve_type == ZIP_UNRESOLVED_FILE)
  722. entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_FILE);
  723. } /* if */
  724. return(retval);
  725. } /* zip_resolve */
  726. static int zip_version_does_symlinks(PHYSFS_uint32 version)
  727. {
  728. int retval = 0;
  729. PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((version >> 8) & 0xFF);
  730. switch (hosttype)
  731. {
  732. /*
  733. * These are the platforms that can NOT build an archive with
  734. * symlinks, according to the Info-ZIP project.
  735. */
  736. case 0: /* FS_FAT_ */
  737. case 1: /* AMIGA_ */
  738. case 2: /* VMS_ */
  739. case 4: /* VM_CSM_ */
  740. case 6: /* FS_HPFS_ */
  741. case 11: /* FS_NTFS_ */
  742. case 14: /* FS_VFAT_ */
  743. case 13: /* ACORN_ */
  744. case 15: /* MVS_ */
  745. case 18: /* THEOS_ */
  746. break; /* do nothing. */
  747. default: /* assume the rest to be unix-like. */
  748. retval = 1;
  749. break;
  750. } /* switch */
  751. return(retval);
  752. } /* zip_version_does_symlinks */
  753. static int zip_entry_is_symlink(ZIPentry *entry)
  754. {
  755. return((entry->resolved == ZIP_UNRESOLVED_SYMLINK) ||
  756. (entry->resolved == ZIP_BROKEN_SYMLINK) ||
  757. (entry->symlink));
  758. } /* zip_entry_is_symlink */
  759. static int zip_has_symlink_attr(ZIPentry *entry, PHYSFS_uint32 extern_attr)
  760. {
  761. PHYSFS_uint16 xattr = ((extern_attr >> 16) & 0xFFFF);
  762. return (
  763. (zip_version_does_symlinks(entry->version)) &&
  764. (entry->uncompressed_size > 0) &&
  765. ((xattr & UNIX_FILETYPE_MASK) == UNIX_FILETYPE_SYMLINK)
  766. );
  767. } /* zip_has_symlink_attr */
  768. static PHYSFS_sint64 zip_dos_time_to_physfs_time(PHYSFS_uint32 dostime)
  769. {
  770. #ifdef _WIN32_WCE
  771. /* We have no struct tm and no mktime right now.
  772. FIXME: This should probably be fixed at some point.
  773. */
  774. return -1;
  775. #else
  776. PHYSFS_uint32 dosdate;
  777. struct tm unixtime;
  778. memset(&unixtime, '\0', sizeof (unixtime));
  779. dosdate = (PHYSFS_uint32) ((dostime >> 16) & 0xFFFF);
  780. dostime &= 0xFFFF;
  781. /* dissect date */
  782. unixtime.tm_year = ((dosdate >> 9) & 0x7F) + 80;
  783. unixtime.tm_mon = ((dosdate >> 5) & 0x0F) - 1;
  784. unixtime.tm_mday = ((dosdate ) & 0x1F);
  785. /* dissect time */
  786. unixtime.tm_hour = ((dostime >> 11) & 0x1F);
  787. unixtime.tm_min = ((dostime >> 5) & 0x3F);
  788. unixtime.tm_sec = ((dostime << 1) & 0x3E);
  789. /* let mktime calculate daylight savings time. */
  790. unixtime.tm_isdst = -1;
  791. return((PHYSFS_sint64) mktime(&unixtime));
  792. #endif
  793. } /* zip_dos_time_to_physfs_time */
  794. static int zip_load_entry(void *in, ZIPentry *entry, PHYSFS_uint32 ofs_fixup)
  795. {
  796. PHYSFS_uint16 fnamelen, extralen, commentlen;
  797. PHYSFS_uint32 external_attr;
  798. PHYSFS_uint16 ui16;
  799. PHYSFS_uint32 ui32;
  800. PHYSFS_sint64 si64;
  801. /* sanity check with central directory signature... */
  802. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  803. BAIL_IF_MACRO(ui32 != ZIP_CENTRAL_DIR_SIG, ERR_CORRUPTED, 0);
  804. /* Get the pertinent parts of the record... */
  805. BAIL_IF_MACRO(!readui16(in, &entry->version), NULL, 0);
  806. BAIL_IF_MACRO(!readui16(in, &entry->version_needed), NULL, 0);
  807. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* general bits */
  808. BAIL_IF_MACRO(!readui16(in, &entry->compression_method), NULL, 0);
  809. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  810. entry->last_mod_time = zip_dos_time_to_physfs_time(ui32);
  811. BAIL_IF_MACRO(!readui32(in, &entry->crc), NULL, 0);
  812. BAIL_IF_MACRO(!readui32(in, &entry->compressed_size), NULL, 0);
  813. BAIL_IF_MACRO(!readui32(in, &entry->uncompressed_size), NULL, 0);
  814. BAIL_IF_MACRO(!readui16(in, &fnamelen), NULL, 0);
  815. BAIL_IF_MACRO(!readui16(in, &extralen), NULL, 0);
  816. BAIL_IF_MACRO(!readui16(in, &commentlen), NULL, 0);
  817. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* disk number start */
  818. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* internal file attribs */
  819. BAIL_IF_MACRO(!readui32(in, &external_attr), NULL, 0);
  820. BAIL_IF_MACRO(!readui32(in, &entry->offset), NULL, 0);
  821. entry->offset += ofs_fixup;
  822. entry->symlink = NULL; /* will be resolved later, if necessary. */
  823. entry->resolved = (zip_has_symlink_attr(entry, external_attr)) ?
  824. ZIP_UNRESOLVED_SYMLINK : ZIP_UNRESOLVED_FILE;
  825. entry->name = (char *) malloc(fnamelen + 1);
  826. BAIL_IF_MACRO(entry->name == NULL, ERR_OUT_OF_MEMORY, 0);
  827. if (__PHYSFS_platformRead(in, entry->name, fnamelen, 1) != 1)
  828. goto zip_load_entry_puked;
  829. entry->name[fnamelen] = '\0'; /* null-terminate the filename. */
  830. zip_convert_dos_path(entry, entry->name);
  831. si64 = __PHYSFS_platformTell(in);
  832. if (si64 == -1)
  833. goto zip_load_entry_puked;
  834. /* seek to the start of the next entry in the central directory... */
  835. if (!__PHYSFS_platformSeek(in, si64 + extralen + commentlen))
  836. goto zip_load_entry_puked;
  837. return(1); /* success. */
  838. zip_load_entry_puked:
  839. free(entry->name);
  840. return(0); /* failure. */
  841. } /* zip_load_entry */
  842. static int zip_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  843. {
  844. ZIPentry *a = (ZIPentry *) _a;
  845. return(strcmp(a[one].name, a[two].name));
  846. } /* zip_entry_cmp */
  847. static void zip_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  848. {
  849. ZIPentry tmp;
  850. ZIPentry *first = &(((ZIPentry *) _a)[one]);
  851. ZIPentry *second = &(((ZIPentry *) _a)[two]);
  852. memcpy(&tmp, first, sizeof (ZIPentry));
  853. memcpy(first, second, sizeof (ZIPentry));
  854. memcpy(second, &tmp, sizeof (ZIPentry));
  855. } /* zip_entry_swap */
  856. static int zip_load_entries(void *in, ZIPinfo *info,
  857. PHYSFS_uint32 data_ofs, PHYSFS_uint32 central_ofs)
  858. {
  859. PHYSFS_uint32 max = info->entryCount;
  860. PHYSFS_uint32 i;
  861. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, central_ofs), NULL, 0);
  862. info->entries = (ZIPentry *) malloc(sizeof (ZIPentry) * max);
  863. BAIL_IF_MACRO(info->entries == NULL, ERR_OUT_OF_MEMORY, 0);
  864. for (i = 0; i < max; i++)
  865. {
  866. if (!zip_load_entry(in, &info->entries[i], data_ofs))
  867. {
  868. zip_free_entries(info->entries, i);
  869. return(0);
  870. } /* if */
  871. } /* for */
  872. __PHYSFS_sort(info->entries, max, zip_entry_cmp, zip_entry_swap);
  873. return(1);
  874. } /* zip_load_entries */
  875. static int zip_parse_end_of_central_dir(void *in, ZIPinfo *info,
  876. PHYSFS_uint32 *data_start,
  877. PHYSFS_uint32 *central_dir_ofs)
  878. {
  879. PHYSFS_uint32 ui32;
  880. PHYSFS_uint16 ui16;
  881. PHYSFS_sint64 len;
  882. PHYSFS_sint64 pos;
  883. /* find the end-of-central-dir record, and seek to it. */
  884. pos = zip_find_end_of_central_dir(in, &len);
  885. BAIL_IF_MACRO(pos == -1, NULL, 0);
  886. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, pos), NULL, 0);
  887. /* check signature again, just in case. */
  888. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  889. BAIL_IF_MACRO(ui32 != ZIP_END_OF_CENTRAL_DIR_SIG, ERR_NOT_AN_ARCHIVE, 0);
  890. /* number of this disk */
  891. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  892. BAIL_IF_MACRO(ui16 != 0, ERR_UNSUPPORTED_ARCHIVE, 0);
  893. /* number of the disk with the start of the central directory */
  894. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  895. BAIL_IF_MACRO(ui16 != 0, ERR_UNSUPPORTED_ARCHIVE, 0);
  896. /* total number of entries in the central dir on this disk */
  897. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  898. /* total number of entries in the central dir */
  899. BAIL_IF_MACRO(!readui16(in, &info->entryCount), NULL, 0);
  900. BAIL_IF_MACRO(ui16 != info->entryCount, ERR_UNSUPPORTED_ARCHIVE, 0);
  901. /* size of the central directory */
  902. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  903. /* offset of central directory */
  904. BAIL_IF_MACRO(!readui32(in, central_dir_ofs), NULL, 0);
  905. BAIL_IF_MACRO(pos < *central_dir_ofs + ui32, ERR_UNSUPPORTED_ARCHIVE, 0);
  906. /*
  907. * For self-extracting archives, etc, there's crapola in the file
  908. * before the zipfile records; we calculate how much data there is
  909. * prepended by determining how far the central directory offset is
  910. * from where it is supposed to be (start of end-of-central-dir minus
  911. * sizeof central dir)...the difference in bytes is how much arbitrary
  912. * data is at the start of the physical file.
  913. */
  914. *data_start = (PHYSFS_uint32) (pos - (*central_dir_ofs + ui32));
  915. /* Now that we know the difference, fix up the central dir offset... */
  916. *central_dir_ofs += *data_start;
  917. /* zipfile comment length */
  918. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  919. /*
  920. * Make sure that the comment length matches to the end of file...
  921. * If it doesn't, we're either in the wrong part of the file, or the
  922. * file is corrupted, but we give up either way.
  923. */
  924. BAIL_IF_MACRO((pos + 22 + ui16) != len, ERR_UNSUPPORTED_ARCHIVE, 0);
  925. return(1); /* made it. */
  926. } /* zip_parse_end_of_central_dir */
  927. static ZIPinfo *zip_create_zipinfo(const char *name)
  928. {
  929. char *ptr;
  930. ZIPinfo *info = (ZIPinfo *) malloc(sizeof (ZIPinfo));
  931. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
  932. memset(info, '\0', sizeof (ZIPinfo));
  933. ptr = (char *) malloc(strlen(name) + 1);
  934. if (ptr == NULL)
  935. {
  936. free(info);
  937. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  938. } /* if */
  939. info->archiveName = ptr;
  940. strcpy(info->archiveName, name);
  941. return(info);
  942. } /* zip_create_zipinfo */
  943. static void *ZIP_openArchive(const char *name, int forWriting)
  944. {
  945. void *in = NULL;
  946. ZIPinfo *info = NULL;
  947. PHYSFS_uint32 data_start;
  948. PHYSFS_uint32 cent_dir_ofs;
  949. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, NULL);
  950. if ((in = __PHYSFS_platformOpenRead(name)) == NULL)
  951. goto zip_openarchive_failed;
  952. if ((info = zip_create_zipinfo(name)) == NULL)
  953. goto zip_openarchive_failed;
  954. if (!zip_parse_end_of_central_dir(in, info, &data_start, &cent_dir_ofs))
  955. goto zip_openarchive_failed;
  956. if (!zip_load_entries(in, info, data_start, cent_dir_ofs))
  957. goto zip_openarchive_failed;
  958. __PHYSFS_platformClose(in);
  959. return(info);
  960. zip_openarchive_failed:
  961. if (info != NULL)
  962. {
  963. if (info->archiveName != NULL)
  964. free(info->archiveName);
  965. free(info);
  966. } /* if */
  967. if (in != NULL)
  968. __PHYSFS_platformClose(in);
  969. return(NULL);
  970. } /* ZIP_openArchive */
  971. static PHYSFS_sint32 zip_find_start_of_dir(ZIPinfo *info, const char *path,
  972. int stop_on_first_find)
  973. {
  974. PHYSFS_sint32 lo = 0;
  975. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  976. PHYSFS_sint32 middle;
  977. PHYSFS_uint32 dlen = strlen(path);
  978. PHYSFS_sint32 retval = -1;
  979. const char *name;
  980. int rc;
  981. if (*path == '\0') /* root dir? */
  982. return(0);
  983. if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
  984. dlen--;
  985. while (lo <= hi)
  986. {
  987. middle = lo + ((hi - lo) / 2);
  988. name = info->entries[middle].name;
  989. rc = strncmp(path, name, dlen);
  990. if (rc == 0)
  991. {
  992. char ch = name[dlen];
  993. if (ch < '/') /* make sure this isn't just a substr match. */
  994. rc = -1;
  995. else if (ch > '/')
  996. rc = 1;
  997. else
  998. {
  999. if (stop_on_first_find) /* Just checking dir's existance? */
  1000. return(middle);
  1001. if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
  1002. return(middle + 1);
  1003. /* there might be more entries earlier in the list. */
  1004. retval = middle;
  1005. hi = middle - 1;
  1006. } /* else */
  1007. } /* if */
  1008. if (rc > 0)
  1009. lo = middle + 1;
  1010. else
  1011. hi = middle - 1;
  1012. } /* while */
  1013. return(retval);
  1014. } /* zip_find_start_of_dir */
  1015. static LinkedStringList *ZIP_enumerateFiles(dvoid *opaque,
  1016. const char *dirname,
  1017. int omitSymLinks)
  1018. {
  1019. ZIPinfo *info = ((ZIPinfo *) opaque);
  1020. LinkedStringList *retval = NULL, *p = NULL;
  1021. PHYSFS_sint32 dlen, dlen_inc, max, i;
  1022. i = zip_find_start_of_dir(info, dirname, 0);
  1023. BAIL_IF_MACRO(i == -1, ERR_NO_SUCH_FILE, NULL);
  1024. dlen = strlen(dirname);
  1025. if ((dlen > 0) && (dirname[dlen - 1] == '/')) /* ignore trailing slash. */
  1026. dlen--;
  1027. dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
  1028. max = (PHYSFS_sint32) info->entryCount;
  1029. while (i < max)
  1030. {
  1031. char *e = info->entries[i].name;
  1032. if ((dlen) && ((strncmp(e, dirname, dlen) != 0) || (e[dlen] != '/')))
  1033. break; /* past end of this dir; we're done. */
  1034. if ((omitSymLinks) && (zip_entry_is_symlink(&info->entries[i])))
  1035. i++;
  1036. else
  1037. {
  1038. char *add = e + dlen_inc;
  1039. char *ptr = strchr(add, '/');
  1040. PHYSFS_sint32 ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
  1041. retval = __PHYSFS_addToLinkedStringList(retval, &p, add, ln);
  1042. ln += dlen_inc; /* point past entry to children... */
  1043. /* increment counter and skip children of subdirs... */
  1044. while ((++i < max) && (ptr != NULL))
  1045. {
  1046. char *e_new = info->entries[i].name;
  1047. if ((strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
  1048. break;
  1049. } /* while */
  1050. } /* else */
  1051. } /* while */
  1052. return(retval);
  1053. } /* ZIP_enumerateFiles */
  1054. static int ZIP_exists(dvoid *opaque, const char *name)
  1055. {
  1056. int isDir;
  1057. ZIPinfo *info = (ZIPinfo *) opaque;
  1058. ZIPentry *entry = zip_find_entry(info, name, &isDir);
  1059. return((entry != NULL) || (isDir));
  1060. } /* ZIP_exists */
  1061. static PHYSFS_sint64 ZIP_getLastModTime(dvoid *opaque,
  1062. const char *name,
  1063. int *fileExists)
  1064. {
  1065. int isDir;
  1066. ZIPinfo *info = (ZIPinfo *) opaque;
  1067. ZIPentry *entry = zip_find_entry(info, name, &isDir);
  1068. *fileExists = ((isDir) || (entry != NULL));
  1069. if (isDir)
  1070. return(1); /* Best I can do for a dir... */
  1071. BAIL_IF_MACRO(entry == NULL, NULL, -1);
  1072. return(entry->last_mod_time);
  1073. } /* ZIP_getLastModTime */
  1074. static int ZIP_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  1075. {
  1076. ZIPinfo *info = (ZIPinfo *) opaque;
  1077. int isDir;
  1078. ZIPentry *entry = zip_find_entry(info, name, &isDir);
  1079. *fileExists = ((isDir) || (entry != NULL));
  1080. if (isDir)
  1081. return(1); /* definitely a dir. */
  1082. /* Follow symlinks. This means we might need to resolve entries. */
  1083. BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, 0);
  1084. if (entry->resolved == ZIP_UNRESOLVED_SYMLINK) /* gotta resolve it. */
  1085. {
  1086. int rc;
  1087. void *in = __PHYSFS_platformOpenRead(info->archiveName);
  1088. BAIL_IF_MACRO(in == NULL, NULL, 0);
  1089. rc = zip_resolve(in, info, entry);
  1090. __PHYSFS_platformClose(in);
  1091. if (!rc)
  1092. return(0);
  1093. } /* if */
  1094. BAIL_IF_MACRO(entry->resolved == ZIP_BROKEN_SYMLINK, NULL, 0);
  1095. BAIL_IF_MACRO(entry->symlink == NULL, ERR_NOT_A_DIR, 0);
  1096. return(zip_find_start_of_dir(info, entry->symlink->name, 1) >= 0);
  1097. } /* ZIP_isDirectory */
  1098. static int ZIP_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  1099. {
  1100. int isDir;
  1101. ZIPentry *entry = zip_find_entry((ZIPinfo *) opaque, name, &isDir);
  1102. *fileExists = ((isDir) || (entry != NULL));
  1103. BAIL_IF_MACRO(entry == NULL, NULL, 0);
  1104. return(zip_entry_is_symlink(entry));
  1105. } /* ZIP_isSymLink */
  1106. static void *zip_get_file_handle(const char *fn, ZIPinfo *inf, ZIPentry *entry)
  1107. {
  1108. int success;
  1109. void *retval = __PHYSFS_platformOpenRead(fn);
  1110. BAIL_IF_MACRO(retval == NULL, NULL, NULL);
  1111. success = zip_resolve(retval, inf, entry);
  1112. if (success)
  1113. {
  1114. PHYSFS_sint64 offset;
  1115. offset = ((entry->symlink) ? entry->symlink->offset : entry->offset);
  1116. success = __PHYSFS_platformSeek(retval, offset);
  1117. } /* if */
  1118. if (!success)
  1119. {
  1120. __PHYSFS_platformClose(retval);
  1121. retval = NULL;
  1122. } /* if */
  1123. return(retval);
  1124. } /* zip_get_file_handle */
  1125. static fvoid *ZIP_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  1126. {
  1127. ZIPinfo *info = (ZIPinfo *) opaque;
  1128. ZIPentry *entry = zip_find_entry(info, fnm, NULL);
  1129. ZIPfileinfo *finfo = NULL;
  1130. void *in;
  1131. *fileExists = (entry != NULL);
  1132. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  1133. in = zip_get_file_handle(info->archiveName, info, entry);
  1134. BAIL_IF_MACRO(in == NULL, NULL, NULL);
  1135. finfo = (ZIPfileinfo *) malloc(sizeof (ZIPfileinfo));
  1136. if (finfo == NULL)
  1137. {
  1138. __PHYSFS_platformClose(in);
  1139. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  1140. } /* if */
  1141. memset(finfo, '\0', sizeof (ZIPfileinfo));
  1142. finfo->handle = in;
  1143. finfo->entry = ((entry->symlink != NULL) ? entry->symlink : entry);
  1144. initializeZStream(&finfo->stream);
  1145. if (finfo->entry->compression_method != COMPMETH_NONE)
  1146. {
  1147. if (zlib_err(inflateInit2(&finfo->stream, -MAX_WBITS)) != Z_OK)
  1148. {
  1149. ZIP_fileClose(finfo);
  1150. return(NULL);
  1151. } /* if */
  1152. finfo->buffer = (PHYSFS_uint8 *) malloc(ZIP_READBUFSIZE);
  1153. if (finfo->buffer == NULL)
  1154. {
  1155. ZIP_fileClose(finfo);
  1156. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  1157. } /* if */
  1158. } /* if */
  1159. return(finfo);
  1160. } /* ZIP_openRead */
  1161. static fvoid *ZIP_openWrite(dvoid *opaque, const char *filename)
  1162. {
  1163. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  1164. } /* ZIP_openWrite */
  1165. static fvoid *ZIP_openAppend(dvoid *opaque, const char *filename)
  1166. {
  1167. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  1168. } /* ZIP_openAppend */
  1169. static void ZIP_dirClose(dvoid *opaque)
  1170. {
  1171. ZIPinfo *zi = (ZIPinfo *) (opaque);
  1172. zip_free_entries(zi->entries, zi->entryCount);
  1173. free(zi->archiveName);
  1174. free(zi);
  1175. } /* ZIP_dirClose */
  1176. static int ZIP_remove(dvoid *opaque, const char *name)
  1177. {
  1178. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  1179. } /* ZIP_remove */
  1180. static int ZIP_mkdir(dvoid *opaque, const char *name)
  1181. {
  1182. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  1183. } /* ZIP_mkdir */
  1184. #endif /* defined PHYSFS_SUPPORTS_ZIP */
  1185. /* end of zip.c ... */