zip.c 45 KB

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