zip.c 45 KB

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