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