zip.c 43 KB

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