zip.c 44 KB

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