zip.c 43 KB

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