zip.c 46 KB

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