zip.c 43 KB

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