zip.c 44 KB

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