zip.c 43 KB

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