zip.c 44 KB

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