zip.c 44 KB

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