1
0

zip.c 45 KB

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