zip.c 42 KB

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