zip.c 44 KB

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