zip.c 44 KB

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