archiver_zip.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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. } /* zlib_error_string */
  139. /*
  140. * Wrap all zlib calls in this, so the physfs error state is set appropriately.
  141. */
  142. static int zlib_err(int rc)
  143. {
  144. const char *str = zlib_error_string(rc);
  145. if (str != NULL)
  146. __PHYSFS_setError(str);
  147. return rc;
  148. } /* zlib_err */
  149. static inline int readAll(void *fh, void *buf, const PHYSFS_uint64 len)
  150. {
  151. return (__PHYSFS_platformRead(fh, buf, len) == len);
  152. } /* readAll */
  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(!readAll(in, &v, sizeof (v)), 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(!readAll(in, &v, sizeof (v)), NULL, 0);
  170. *val = PHYSFS_swapULE16(v);
  171. return 1;
  172. } /* readui16 */
  173. static PHYSFS_sint64 ZIP_read(fvoid *opaque, void *buf, PHYSFS_uint64 len)
  174. {
  175. ZIPfileinfo *finfo = (ZIPfileinfo *) opaque;
  176. ZIPentry *entry = finfo->entry;
  177. PHYSFS_sint64 retval = 0;
  178. PHYSFS_sint64 maxread = (PHYSFS_sint64) len;
  179. PHYSFS_sint64 avail = entry->uncompressed_size -
  180. finfo->uncompressed_position;
  181. if (avail < maxread)
  182. maxread = avail;
  183. BAIL_IF_MACRO(maxread == 0, NULL, 0); /* quick rejection. */
  184. if (entry->compression_method == COMPMETH_NONE)
  185. retval = __PHYSFS_platformRead(finfo->handle, buf, maxread);
  186. else
  187. {
  188. finfo->stream.next_out = buf;
  189. finfo->stream.avail_out = maxread;
  190. while (retval < maxread)
  191. {
  192. PHYSFS_uint32 before = finfo->stream.total_out;
  193. int rc;
  194. if (finfo->stream.avail_in == 0)
  195. {
  196. PHYSFS_sint64 br;
  197. br = entry->compressed_size - finfo->compressed_position;
  198. if (br > 0)
  199. {
  200. if (br > ZIP_READBUFSIZE)
  201. br = ZIP_READBUFSIZE;
  202. br = __PHYSFS_platformRead(finfo->handle, finfo->buffer,
  203. (PHYSFS_uint64) br);
  204. if (br <= 0)
  205. break;
  206. finfo->compressed_position += (PHYSFS_uint32) br;
  207. finfo->stream.next_in = finfo->buffer;
  208. finfo->stream.avail_in = (PHYSFS_uint32) br;
  209. } /* if */
  210. } /* if */
  211. rc = zlib_err(inflate(&finfo->stream, Z_SYNC_FLUSH));
  212. retval += (finfo->stream.total_out - before);
  213. if (rc != Z_OK)
  214. break;
  215. } /* while */
  216. } /* else */
  217. if (retval > 0)
  218. finfo->uncompressed_position += (PHYSFS_uint32) retval;
  219. return retval;
  220. } /* ZIP_read */
  221. static PHYSFS_sint64 ZIP_write(fvoid *f, const void *buf, PHYSFS_uint64 len)
  222. {
  223. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  224. } /* ZIP_write */
  225. static int ZIP_eof(fvoid *opaque)
  226. {
  227. ZIPfileinfo *finfo = (ZIPfileinfo *) opaque;
  228. return (finfo->uncompressed_position >= finfo->entry->uncompressed_size);
  229. } /* ZIP_eof */
  230. static PHYSFS_sint64 ZIP_tell(fvoid *opaque)
  231. {
  232. return ((ZIPfileinfo *) opaque)->uncompressed_position;
  233. } /* ZIP_tell */
  234. static int ZIP_seek(fvoid *opaque, PHYSFS_uint64 offset)
  235. {
  236. ZIPfileinfo *finfo = (ZIPfileinfo *) opaque;
  237. ZIPentry *entry = finfo->entry;
  238. void *in = finfo->handle;
  239. BAIL_IF_MACRO(offset > entry->uncompressed_size, ERR_PAST_EOF, 0);
  240. if (entry->compression_method == COMPMETH_NONE)
  241. {
  242. PHYSFS_sint64 newpos = offset + entry->offset;
  243. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, newpos), NULL, 0);
  244. finfo->uncompressed_position = (PHYSFS_uint32) offset;
  245. } /* if */
  246. else
  247. {
  248. /*
  249. * If seeking backwards, we need to redecode the file
  250. * from the start and throw away the compressed bits until we hit
  251. * the offset we need. If seeking forward, we still need to
  252. * decode, but we don't rewind first.
  253. */
  254. if (offset < finfo->uncompressed_position)
  255. {
  256. /* we do a copy so state is sane if inflateInit2() fails. */
  257. z_stream str;
  258. initializeZStream(&str);
  259. if (zlib_err(inflateInit2(&str, -MAX_WBITS)) != Z_OK)
  260. return 0;
  261. if (!__PHYSFS_platformSeek(in, entry->offset))
  262. return 0;
  263. inflateEnd(&finfo->stream);
  264. memcpy(&finfo->stream, &str, sizeof (z_stream));
  265. finfo->uncompressed_position = finfo->compressed_position = 0;
  266. } /* if */
  267. while (finfo->uncompressed_position != offset)
  268. {
  269. PHYSFS_uint8 buf[512];
  270. PHYSFS_uint32 maxread;
  271. maxread = (PHYSFS_uint32) (offset - finfo->uncompressed_position);
  272. if (maxread > sizeof (buf))
  273. maxread = sizeof (buf);
  274. if (ZIP_read(finfo, buf, maxread) != maxread)
  275. return 0;
  276. } /* while */
  277. } /* else */
  278. return 1;
  279. } /* ZIP_seek */
  280. static PHYSFS_sint64 ZIP_fileLength(fvoid *opaque)
  281. {
  282. ZIPfileinfo *finfo = (ZIPfileinfo *) opaque;
  283. return finfo->entry->uncompressed_size;
  284. } /* ZIP_fileLength */
  285. static int ZIP_fileClose(fvoid *opaque)
  286. {
  287. ZIPfileinfo *finfo = (ZIPfileinfo *) opaque;
  288. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  289. if (finfo->entry->compression_method != COMPMETH_NONE)
  290. inflateEnd(&finfo->stream);
  291. if (finfo->buffer != NULL)
  292. allocator.Free(finfo->buffer);
  293. allocator.Free(finfo);
  294. return 1;
  295. } /* ZIP_fileClose */
  296. static PHYSFS_sint64 zip_find_end_of_central_dir(void *in, PHYSFS_sint64 *len)
  297. {
  298. PHYSFS_uint8 buf[256];
  299. PHYSFS_uint8 extra[4] = { 0, 0, 0, 0 };
  300. PHYSFS_sint32 i = 0;
  301. PHYSFS_sint64 filelen;
  302. PHYSFS_sint64 filepos;
  303. PHYSFS_sint32 maxread;
  304. PHYSFS_sint32 totalread = 0;
  305. int found = 0;
  306. filelen = __PHYSFS_platformFileLength(in);
  307. BAIL_IF_MACRO(filelen == -1, NULL, 0); /* !!! FIXME: unlocalized string */
  308. BAIL_IF_MACRO(filelen > 0xFFFFFFFF, "ZIP bigger than 4 gigs?!", 0);
  309. /*
  310. * Jump to the end of the file and start reading backwards.
  311. * The last thing in the file is the zipfile comment, which is variable
  312. * length, and the field that specifies its size is before it in the
  313. * file (argh!)...this means that we need to scan backwards until we
  314. * hit the end-of-central-dir signature. We can then sanity check that
  315. * the comment was as big as it should be to make sure we're in the
  316. * right place. The comment length field is 16 bits, so we can stop
  317. * searching for that signature after a little more than 64k at most,
  318. * and call it a corrupted zipfile.
  319. */
  320. if (sizeof (buf) < filelen)
  321. {
  322. filepos = filelen - sizeof (buf);
  323. maxread = sizeof (buf);
  324. } /* if */
  325. else
  326. {
  327. filepos = 0;
  328. maxread = (PHYSFS_uint32) filelen;
  329. } /* else */
  330. while ((totalread < filelen) && (totalread < 65557))
  331. {
  332. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, filepos), NULL, -1);
  333. /* make sure we catch a signature between buffers. */
  334. if (totalread != 0)
  335. {
  336. if (!readAll(in, buf, maxread - 4))
  337. return -1;
  338. memcpy(&buf[maxread - 4], &extra, sizeof (extra));
  339. totalread += maxread - 4;
  340. } /* if */
  341. else
  342. {
  343. if (!readAll(in, buf, maxread))
  344. return -1;
  345. totalread += maxread;
  346. } /* else */
  347. memcpy(&extra, buf, sizeof (extra));
  348. for (i = maxread - 4; i > 0; i--)
  349. {
  350. if ((buf[i + 0] == 0x50) &&
  351. (buf[i + 1] == 0x4B) &&
  352. (buf[i + 2] == 0x05) &&
  353. (buf[i + 3] == 0x06) )
  354. {
  355. found = 1; /* that's the signature! */
  356. break;
  357. } /* if */
  358. } /* for */
  359. if (found)
  360. break;
  361. filepos -= (maxread - 4);
  362. if (filepos < 0)
  363. filepos = 0;
  364. } /* while */
  365. BAIL_IF_MACRO(!found, ERR_NOT_AN_ARCHIVE, -1);
  366. if (len != NULL)
  367. *len = filelen;
  368. return (filepos + i);
  369. } /* zip_find_end_of_central_dir */
  370. static int ZIP_isArchive(const char *filename, int forWriting)
  371. {
  372. PHYSFS_uint32 sig;
  373. int retval = 0;
  374. void *in;
  375. in = __PHYSFS_platformOpenRead(filename);
  376. BAIL_IF_MACRO(in == NULL, NULL, 0);
  377. /*
  378. * The first thing in a zip file might be the signature of the
  379. * first local file record, so it makes for a quick determination.
  380. */
  381. if (readui32(in, &sig))
  382. {
  383. retval = (sig == ZIP_LOCAL_FILE_SIG);
  384. if (!retval)
  385. {
  386. /*
  387. * No sig...might be a ZIP with data at the start
  388. * (a self-extracting executable, etc), so we'll have to do
  389. * it the hard way...
  390. */
  391. retval = (zip_find_end_of_central_dir(in, NULL) != -1);
  392. } /* if */
  393. } /* if */
  394. __PHYSFS_platformClose(in);
  395. return retval;
  396. } /* ZIP_isArchive */
  397. static void zip_free_entries(ZIPentry *entries, PHYSFS_uint32 max)
  398. {
  399. PHYSFS_uint32 i;
  400. for (i = 0; i < max; i++)
  401. {
  402. ZIPentry *entry = &entries[i];
  403. if (entry->name != NULL)
  404. allocator.Free(entry->name);
  405. } /* for */
  406. allocator.Free(entries);
  407. } /* zip_free_entries */
  408. /*
  409. * This will find the ZIPentry associated with a path in platform-independent
  410. * notation. Directories don't have ZIPentries associated with them, but
  411. * (*isDir) will be set to non-zero if a dir was hit.
  412. */
  413. static ZIPentry *zip_find_entry(const ZIPinfo *info, const char *path,
  414. int *isDir)
  415. {
  416. ZIPentry *a = info->entries;
  417. PHYSFS_sint32 pathlen = strlen(path);
  418. PHYSFS_sint32 lo = 0;
  419. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  420. PHYSFS_sint32 middle;
  421. const char *thispath = NULL;
  422. int rc;
  423. while (lo <= hi)
  424. {
  425. middle = lo + ((hi - lo) / 2);
  426. thispath = a[middle].name;
  427. rc = strncmp(path, thispath, pathlen);
  428. if (rc > 0)
  429. lo = middle + 1;
  430. else if (rc < 0)
  431. hi = middle - 1;
  432. else /* substring match...might be dir or entry or nothing. */
  433. {
  434. if (isDir != NULL)
  435. {
  436. *isDir = (thispath[pathlen] == '/');
  437. if (*isDir)
  438. return NULL;
  439. } /* if */
  440. if (thispath[pathlen] == '\0') /* found entry? */
  441. return &a[middle];
  442. else
  443. hi = middle - 1; /* adjust search params, try again. */
  444. } /* if */
  445. } /* while */
  446. if (isDir != NULL)
  447. *isDir = 0;
  448. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  449. } /* zip_find_entry */
  450. /* Convert paths from old, buggy DOS zippers... */
  451. static void zip_convert_dos_path(ZIPentry *entry, char *path)
  452. {
  453. PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((entry->version >> 8) & 0xFF);
  454. if (hosttype == 0) /* FS_FAT_ */
  455. {
  456. while (*path)
  457. {
  458. if (*path == '\\')
  459. *path = '/';
  460. path++;
  461. } /* while */
  462. } /* if */
  463. } /* zip_convert_dos_path */
  464. static void zip_expand_symlink_path(char *path)
  465. {
  466. char *ptr = path;
  467. char *prevptr = path;
  468. while (1)
  469. {
  470. ptr = strchr(ptr, '/');
  471. if (ptr == NULL)
  472. break;
  473. if (*(ptr + 1) == '.')
  474. {
  475. if (*(ptr + 2) == '/')
  476. {
  477. /* current dir in middle of string: ditch it. */
  478. memmove(ptr, ptr + 2, strlen(ptr + 2) + 1);
  479. } /* else if */
  480. else if (*(ptr + 2) == '\0')
  481. {
  482. /* current dir at end of string: ditch it. */
  483. *ptr = '\0';
  484. } /* else if */
  485. else if (*(ptr + 2) == '.')
  486. {
  487. if (*(ptr + 3) == '/')
  488. {
  489. /* parent dir in middle: move back one, if possible. */
  490. memmove(prevptr, ptr + 4, strlen(ptr + 4) + 1);
  491. ptr = prevptr;
  492. while (prevptr != path)
  493. {
  494. prevptr--;
  495. if (*prevptr == '/')
  496. {
  497. prevptr++;
  498. break;
  499. } /* if */
  500. } /* while */
  501. } /* if */
  502. if (*(ptr + 3) == '\0')
  503. {
  504. /* parent dir at end: move back one, if possible. */
  505. *prevptr = '\0';
  506. } /* if */
  507. } /* if */
  508. } /* if */
  509. else
  510. {
  511. prevptr = ptr;
  512. } /* else */
  513. } /* while */
  514. } /* zip_expand_symlink_path */
  515. /* (forward reference: zip_follow_symlink and zip_resolve call each other.) */
  516. static int zip_resolve(void *in, ZIPinfo *info, ZIPentry *entry);
  517. /*
  518. * Look for the entry named by (path). If it exists, resolve it, and return
  519. * a pointer to that entry. If it's another symlink, keep resolving until you
  520. * hit a real file and then return a pointer to the final non-symlink entry.
  521. * If there's a problem, return NULL. (path) is always free()'d by this
  522. * function.
  523. */
  524. static ZIPentry *zip_follow_symlink(void *in, ZIPinfo *info, char *path)
  525. {
  526. ZIPentry *entry;
  527. zip_expand_symlink_path(path);
  528. entry = zip_find_entry(info, path, NULL);
  529. if (entry != NULL)
  530. {
  531. if (!zip_resolve(in, info, entry)) /* recursive! */
  532. entry = NULL;
  533. else
  534. {
  535. if (entry->symlink != NULL)
  536. entry = entry->symlink;
  537. } /* else */
  538. } /* if */
  539. allocator.Free(path);
  540. return entry;
  541. } /* zip_follow_symlink */
  542. static int zip_resolve_symlink(void *in, ZIPinfo *info, ZIPentry *entry)
  543. {
  544. char *path;
  545. PHYSFS_uint32 size = entry->uncompressed_size;
  546. int rc = 0;
  547. /*
  548. * We've already parsed the local file header of the symlink at this
  549. * point. Now we need to read the actual link from the file data and
  550. * follow it.
  551. */
  552. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, entry->offset), NULL, 0);
  553. path = (char *) allocator.Malloc(size + 1);
  554. BAIL_IF_MACRO(path == NULL, ERR_OUT_OF_MEMORY, 0);
  555. if (entry->compression_method == COMPMETH_NONE)
  556. rc = readAll(in, path, size);
  557. else /* symlink target path is compressed... */
  558. {
  559. z_stream stream;
  560. PHYSFS_uint32 complen = entry->compressed_size;
  561. PHYSFS_uint8 *compressed = (PHYSFS_uint8*) __PHYSFS_smallAlloc(complen);
  562. if (compressed != NULL)
  563. {
  564. if (readAll(in, compressed, complen))
  565. {
  566. initializeZStream(&stream);
  567. stream.next_in = compressed;
  568. stream.avail_in = complen;
  569. stream.next_out = (unsigned char *) path;
  570. stream.avail_out = size;
  571. if (zlib_err(inflateInit2(&stream, -MAX_WBITS)) == Z_OK)
  572. {
  573. rc = zlib_err(inflate(&stream, Z_FINISH));
  574. inflateEnd(&stream);
  575. /* both are acceptable outcomes... */
  576. rc = ((rc == Z_OK) || (rc == Z_STREAM_END));
  577. } /* if */
  578. } /* if */
  579. __PHYSFS_smallFree(compressed);
  580. } /* if */
  581. } /* else */
  582. if (!rc)
  583. allocator.Free(path);
  584. else
  585. {
  586. path[entry->uncompressed_size] = '\0'; /* null-terminate it. */
  587. zip_convert_dos_path(entry, path);
  588. entry->symlink = zip_follow_symlink(in, info, path);
  589. } /* else */
  590. return (entry->symlink != NULL);
  591. } /* zip_resolve_symlink */
  592. /*
  593. * Parse the local file header of an entry, and update entry->offset.
  594. */
  595. static int zip_parse_local(void *in, ZIPentry *entry)
  596. {
  597. PHYSFS_uint32 ui32;
  598. PHYSFS_uint16 ui16;
  599. PHYSFS_uint16 fnamelen;
  600. PHYSFS_uint16 extralen;
  601. /*
  602. * crc and (un)compressed_size are always zero if this is a "JAR"
  603. * archive created with Sun's Java tools, apparently. We only
  604. * consider this archive corrupted if those entries don't match and
  605. * aren't zero. That seems to work well.
  606. */
  607. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, entry->offset), NULL, 0);
  608. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  609. BAIL_IF_MACRO(ui32 != ZIP_LOCAL_FILE_SIG, ERR_CORRUPTED, 0);
  610. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  611. BAIL_IF_MACRO(ui16 != entry->version_needed, ERR_CORRUPTED, 0);
  612. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* general bits. */
  613. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  614. BAIL_IF_MACRO(ui16 != entry->compression_method, ERR_CORRUPTED, 0);
  615. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0); /* date/time */
  616. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  617. BAIL_IF_MACRO(ui32 && (ui32 != entry->crc), ERR_CORRUPTED, 0);
  618. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  619. BAIL_IF_MACRO(ui32 && (ui32 != entry->compressed_size), ERR_CORRUPTED, 0);
  620. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  621. BAIL_IF_MACRO(ui32 && (ui32 != entry->uncompressed_size),ERR_CORRUPTED,0);
  622. BAIL_IF_MACRO(!readui16(in, &fnamelen), NULL, 0);
  623. BAIL_IF_MACRO(!readui16(in, &extralen), NULL, 0);
  624. entry->offset += fnamelen + extralen + 30;
  625. return 1;
  626. } /* zip_parse_local */
  627. static int zip_resolve(void *in, ZIPinfo *info, ZIPentry *entry)
  628. {
  629. int retval = 1;
  630. ZipResolveType resolve_type = entry->resolved;
  631. /* Don't bother if we've failed to resolve this entry before. */
  632. BAIL_IF_MACRO(resolve_type == ZIP_BROKEN_FILE, ERR_CORRUPTED, 0);
  633. BAIL_IF_MACRO(resolve_type == ZIP_BROKEN_SYMLINK, ERR_CORRUPTED, 0);
  634. /* uhoh...infinite symlink loop! */
  635. BAIL_IF_MACRO(resolve_type == ZIP_RESOLVING, ERR_SYMLINK_LOOP, 0);
  636. /*
  637. * We fix up the offset to point to the actual data on the
  638. * first open, since we don't want to seek across the whole file on
  639. * archive open (can be SLOW on large, CD-stored files), but we
  640. * need to check the local file header...not just for corruption,
  641. * but since it stores offset info the central directory does not.
  642. */
  643. if (resolve_type != ZIP_RESOLVED)
  644. {
  645. entry->resolved = ZIP_RESOLVING;
  646. retval = zip_parse_local(in, entry);
  647. if (retval)
  648. {
  649. /*
  650. * If it's a symlink, find the original file. This will cause
  651. * resolution of other entries (other symlinks and, eventually,
  652. * the real file) if all goes well.
  653. */
  654. if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
  655. retval = zip_resolve_symlink(in, info, entry);
  656. } /* if */
  657. if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
  658. entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_SYMLINK);
  659. else if (resolve_type == ZIP_UNRESOLVED_FILE)
  660. entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_FILE);
  661. } /* if */
  662. return retval;
  663. } /* zip_resolve */
  664. static int zip_version_does_symlinks(PHYSFS_uint32 version)
  665. {
  666. int retval = 0;
  667. PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((version >> 8) & 0xFF);
  668. switch (hosttype)
  669. {
  670. /*
  671. * These are the platforms that can NOT build an archive with
  672. * symlinks, according to the Info-ZIP project.
  673. */
  674. case 0: /* FS_FAT_ */
  675. case 1: /* AMIGA_ */
  676. case 2: /* VMS_ */
  677. case 4: /* VM_CSM_ */
  678. case 6: /* FS_HPFS_ */
  679. case 11: /* FS_NTFS_ */
  680. case 14: /* FS_VFAT_ */
  681. case 13: /* ACORN_ */
  682. case 15: /* MVS_ */
  683. case 18: /* THEOS_ */
  684. break; /* do nothing. */
  685. default: /* assume the rest to be unix-like. */
  686. retval = 1;
  687. break;
  688. } /* switch */
  689. return retval;
  690. } /* zip_version_does_symlinks */
  691. static int zip_entry_is_symlink(const ZIPentry *entry)
  692. {
  693. return ((entry->resolved == ZIP_UNRESOLVED_SYMLINK) ||
  694. (entry->resolved == ZIP_BROKEN_SYMLINK) ||
  695. (entry->symlink));
  696. } /* zip_entry_is_symlink */
  697. static int zip_has_symlink_attr(ZIPentry *entry, PHYSFS_uint32 extern_attr)
  698. {
  699. PHYSFS_uint16 xattr = ((extern_attr >> 16) & 0xFFFF);
  700. return ( (zip_version_does_symlinks(entry->version)) &&
  701. (entry->uncompressed_size > 0) &&
  702. ((xattr & UNIX_FILETYPE_MASK) == UNIX_FILETYPE_SYMLINK) );
  703. } /* zip_has_symlink_attr */
  704. static PHYSFS_sint64 zip_dos_time_to_physfs_time(PHYSFS_uint32 dostime)
  705. {
  706. #ifdef _WIN32_WCE
  707. /* We have no struct tm and no mktime right now.
  708. FIXME: This should probably be fixed at some point.
  709. */
  710. return -1;
  711. #else
  712. PHYSFS_uint32 dosdate;
  713. struct tm unixtime;
  714. memset(&unixtime, '\0', sizeof (unixtime));
  715. dosdate = (PHYSFS_uint32) ((dostime >> 16) & 0xFFFF);
  716. dostime &= 0xFFFF;
  717. /* dissect date */
  718. unixtime.tm_year = ((dosdate >> 9) & 0x7F) + 80;
  719. unixtime.tm_mon = ((dosdate >> 5) & 0x0F) - 1;
  720. unixtime.tm_mday = ((dosdate ) & 0x1F);
  721. /* dissect time */
  722. unixtime.tm_hour = ((dostime >> 11) & 0x1F);
  723. unixtime.tm_min = ((dostime >> 5) & 0x3F);
  724. unixtime.tm_sec = ((dostime << 1) & 0x3E);
  725. /* let mktime calculate daylight savings time. */
  726. unixtime.tm_isdst = -1;
  727. return ((PHYSFS_sint64) mktime(&unixtime));
  728. #endif
  729. } /* zip_dos_time_to_physfs_time */
  730. static int zip_load_entry(void *in, ZIPentry *entry, PHYSFS_uint32 ofs_fixup)
  731. {
  732. PHYSFS_uint16 fnamelen, extralen, commentlen;
  733. PHYSFS_uint32 external_attr;
  734. PHYSFS_uint16 ui16;
  735. PHYSFS_uint32 ui32;
  736. PHYSFS_sint64 si64;
  737. /* sanity check with central directory signature... */
  738. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  739. BAIL_IF_MACRO(ui32 != ZIP_CENTRAL_DIR_SIG, ERR_CORRUPTED, 0);
  740. /* Get the pertinent parts of the record... */
  741. BAIL_IF_MACRO(!readui16(in, &entry->version), NULL, 0);
  742. BAIL_IF_MACRO(!readui16(in, &entry->version_needed), NULL, 0);
  743. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* general bits */
  744. BAIL_IF_MACRO(!readui16(in, &entry->compression_method), NULL, 0);
  745. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  746. entry->last_mod_time = zip_dos_time_to_physfs_time(ui32);
  747. BAIL_IF_MACRO(!readui32(in, &entry->crc), NULL, 0);
  748. BAIL_IF_MACRO(!readui32(in, &entry->compressed_size), NULL, 0);
  749. BAIL_IF_MACRO(!readui32(in, &entry->uncompressed_size), NULL, 0);
  750. BAIL_IF_MACRO(!readui16(in, &fnamelen), NULL, 0);
  751. BAIL_IF_MACRO(!readui16(in, &extralen), NULL, 0);
  752. BAIL_IF_MACRO(!readui16(in, &commentlen), NULL, 0);
  753. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* disk number start */
  754. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* internal file attribs */
  755. BAIL_IF_MACRO(!readui32(in, &external_attr), NULL, 0);
  756. BAIL_IF_MACRO(!readui32(in, &entry->offset), NULL, 0);
  757. entry->offset += ofs_fixup;
  758. entry->symlink = NULL; /* will be resolved later, if necessary. */
  759. entry->resolved = (zip_has_symlink_attr(entry, external_attr)) ?
  760. ZIP_UNRESOLVED_SYMLINK : ZIP_UNRESOLVED_FILE;
  761. entry->name = (char *) allocator.Malloc(fnamelen + 1);
  762. BAIL_IF_MACRO(entry->name == NULL, ERR_OUT_OF_MEMORY, 0);
  763. if (!readAll(in, entry->name, fnamelen))
  764. goto zip_load_entry_puked;
  765. entry->name[fnamelen] = '\0'; /* null-terminate the filename. */
  766. zip_convert_dos_path(entry, entry->name);
  767. si64 = __PHYSFS_platformTell(in);
  768. if (si64 == -1)
  769. goto zip_load_entry_puked;
  770. /* seek to the start of the next entry in the central directory... */
  771. if (!__PHYSFS_platformSeek(in, si64 + extralen + commentlen))
  772. goto zip_load_entry_puked;
  773. return 1; /* success. */
  774. zip_load_entry_puked:
  775. allocator.Free(entry->name);
  776. return 0; /* failure. */
  777. } /* zip_load_entry */
  778. static int zip_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  779. {
  780. if (one != two)
  781. {
  782. const ZIPentry *a = (const ZIPentry *) _a;
  783. return strcmp(a[one].name, a[two].name);
  784. } /* if */
  785. return 0;
  786. } /* zip_entry_cmp */
  787. static void zip_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  788. {
  789. if (one != two)
  790. {
  791. ZIPentry tmp;
  792. ZIPentry *first = &(((ZIPentry *) _a)[one]);
  793. ZIPentry *second = &(((ZIPentry *) _a)[two]);
  794. memcpy(&tmp, first, sizeof (ZIPentry));
  795. memcpy(first, second, sizeof (ZIPentry));
  796. memcpy(second, &tmp, sizeof (ZIPentry));
  797. } /* if */
  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. const 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. static int ZIP_stat(dvoid *opaque, const char *filename, int *exists,
  1142. PHYSFS_Stat *stat)
  1143. {
  1144. int isDir = 0;
  1145. const ZIPinfo *info = (const ZIPinfo *) opaque;
  1146. const ZIPentry *entry = zip_find_entry(info, filename, &isDir);
  1147. *exists = isDir || (entry != 0);
  1148. if (!*exists)
  1149. return 0;
  1150. if (isDir)
  1151. {
  1152. stat->filesize = 0;
  1153. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  1154. } /* if */
  1155. else if (zip_entry_is_symlink(entry))
  1156. {
  1157. stat->filesize = 0;
  1158. stat->filetype = PHYSFS_FILETYPE_SYMLINK;
  1159. } /* else if */
  1160. else
  1161. {
  1162. stat->filesize = entry->uncompressed_size;
  1163. stat->filetype = PHYSFS_FILETYPE_REGULAR;
  1164. } /* else */
  1165. stat->modtime = ((entry) ? entry->last_mod_time : 0);
  1166. stat->createtime = stat->modtime;
  1167. stat->accesstime = 0;
  1168. stat->readonly = 1; /* .zip files are always read only */
  1169. return 1;
  1170. } /* ZIP_stat */
  1171. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP =
  1172. {
  1173. "ZIP",
  1174. ZIP_ARCHIVE_DESCRIPTION,
  1175. "Ryan C. Gordon <icculus@icculus.org>",
  1176. "http://icculus.org/physfs/",
  1177. };
  1178. const PHYSFS_Archiver __PHYSFS_Archiver_ZIP =
  1179. {
  1180. &__PHYSFS_ArchiveInfo_ZIP,
  1181. ZIP_isArchive, /* isArchive() method */
  1182. ZIP_openArchive, /* openArchive() method */
  1183. ZIP_enumerateFiles, /* enumerateFiles() method */
  1184. ZIP_exists, /* exists() method */
  1185. ZIP_isDirectory, /* isDirectory() method */
  1186. ZIP_isSymLink, /* isSymLink() method */
  1187. ZIP_getLastModTime, /* getLastModTime() method */
  1188. ZIP_openRead, /* openRead() method */
  1189. ZIP_openWrite, /* openWrite() method */
  1190. ZIP_openAppend, /* openAppend() method */
  1191. ZIP_remove, /* remove() method */
  1192. ZIP_mkdir, /* mkdir() method */
  1193. ZIP_dirClose, /* dirClose() method */
  1194. ZIP_stat, /* stat() method */
  1195. ZIP_read, /* read() method */
  1196. ZIP_write, /* write() method */
  1197. ZIP_eof, /* eof() method */
  1198. ZIP_tell, /* tell() method */
  1199. ZIP_seek, /* seek() method */
  1200. ZIP_fileLength, /* fileLength() method */
  1201. ZIP_fileClose /* fileClose() method */
  1202. };
  1203. #endif /* defined PHYSFS_SUPPORTS_ZIP */
  1204. /* end of zip.c ... */