zip.c 44 KB

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