zip.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. /*
  2. * ZIP support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon, with some peeking at "unzip.c"
  7. * by Gilles Vollant.
  8. */
  9. #if HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #if (defined PHYSFS_SUPPORTS_ZIP)
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <assert.h>
  17. #include <time.h>
  18. #include <errno.h>
  19. #include "physfs.h"
  20. #include "zlib.h"
  21. #define __PHYSICSFS_INTERNAL__
  22. #include "physfs_internal.h"
  23. /*
  24. * A buffer of ZIP_READBUFSIZE is malloc() for each compressed file opened,
  25. * and is free()'d when you close the file; compressed data is read into
  26. * this buffer, and then is decompressed into the buffer passed to
  27. * PHYSFS_read().
  28. *
  29. * Uncompressed entries in a zipfile do not allocate this buffer; they just
  30. * read data directly into the buffer passed to PHYSFS_read().
  31. *
  32. * Depending on your speed and memory requirements, you should tweak this
  33. * value.
  34. */
  35. #define ZIP_READBUFSIZE (16 * 1024)
  36. /*
  37. * One ZIPentry is kept for each file in an open ZIP archive.
  38. */
  39. typedef struct
  40. {
  41. char *name; /* Name of file in archive */
  42. char *symlink; /* NULL or file we link to */
  43. int fixed_up; /* Have we updated offset? */
  44. PHYSFS_uint32 offset; /* offset of data in file */
  45. PHYSFS_uint16 version; /* version made by */
  46. PHYSFS_uint16 version_needed; /* version needed to extract */
  47. PHYSFS_uint16 flag; /* general purpose bit flag */
  48. PHYSFS_uint16 compression_method; /* compression method */
  49. PHYSFS_uint32 crc; /* crc-32 */
  50. PHYSFS_uint32 compressed_size; /* compressed size */
  51. PHYSFS_uint32 uncompressed_size; /* uncompressed size */
  52. PHYSFS_sint64 last_mod_time; /* last file mod time */
  53. } ZIPentry;
  54. /*
  55. * One ZIPinfo is kept for each open ZIP archive.
  56. */
  57. typedef struct
  58. {
  59. char *archiveName; /* path to ZIP in platform-dependent notation. */
  60. PHYSFS_uint16 entryCount; /* Number of files in ZIP. */
  61. ZIPentry *entries; /* info on all files in ZIP. */
  62. } ZIPinfo;
  63. /*
  64. * One ZIPfileinfo is kept for each open file in a ZIP archive.
  65. */
  66. typedef struct
  67. {
  68. ZIPentry *entry; /* Info on file. */
  69. void *handle; /* physical file handle. */
  70. PHYSFS_uint32 compressed_position; /* offset in compressed data. */
  71. PHYSFS_uint32 uncompressed_position; /* tell() position. */
  72. PHYSFS_uint8 *buffer; /* decompression buffer. */
  73. z_stream stream; /* zlib stream state. */
  74. } ZIPfileinfo;
  75. /* Magic numbers... */
  76. #define ZIP_LOCAL_FILE_SIG 0x04034b50
  77. #define ZIP_CENTRAL_DIR_SIG 0x02014b50
  78. #define ZIP_END_OF_CENTRAL_DIR_SIG 0x06054b50
  79. /* compression methods... */
  80. #define COMPMETH_NONE 0
  81. /* ...and others... */
  82. #define UNIX_FILETYPE_MASK 0170000
  83. #define UNIX_FILETYPE_SYMLINK 0120000
  84. #define MAXZIPENTRYSIZE 256 /* !!! FIXME: get rid of this. */
  85. /* Number of symlinks to follow before we assume it's a recursive link... */
  86. #define SYMLINK_RECURSE_COUNT 20
  87. static PHYSFS_sint64 ZIP_read(FileHandle *handle, void *buffer,
  88. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  89. static int ZIP_eof(FileHandle *handle);
  90. static PHYSFS_sint64 ZIP_tell(FileHandle *handle);
  91. static int ZIP_seek(FileHandle *handle, PHYSFS_uint64 offset);
  92. static PHYSFS_sint64 ZIP_fileLength(FileHandle *handle);
  93. static int ZIP_fileClose(FileHandle *handle);
  94. static int ZIP_isArchive(const char *filename, int forWriting);
  95. static char *get_zip_realpath(void *in, ZIPentry *entry);
  96. static DirHandle *ZIP_openArchive(const char *name, int forWriting);
  97. static LinkedStringList *ZIP_enumerateFiles(DirHandle *h,
  98. const char *dirname,
  99. int omitSymLinks);
  100. static int ZIP_exists(DirHandle *h, const char *name);
  101. static int ZIP_isDirectory(DirHandle *h, const char *name);
  102. static int ZIP_isSymLink(DirHandle *h, const char *name);
  103. static PHYSFS_sint64 ZIP_getLastModTime(DirHandle *h, const char *name);
  104. static FileHandle *ZIP_openRead(DirHandle *h, const char *filename);
  105. static void ZIP_dirClose(DirHandle *h);
  106. static const FileFunctions __PHYSFS_FileFunctions_ZIP =
  107. {
  108. ZIP_read, /* read() method */
  109. NULL, /* write() method */
  110. ZIP_eof, /* eof() method */
  111. ZIP_tell, /* tell() method */
  112. ZIP_seek, /* seek() method */
  113. ZIP_fileLength, /* fileLength() method */
  114. ZIP_fileClose /* fileClose() method */
  115. };
  116. const DirFunctions __PHYSFS_DirFunctions_ZIP =
  117. {
  118. ZIP_isArchive, /* isArchive() method */
  119. ZIP_openArchive, /* openArchive() method */
  120. ZIP_enumerateFiles, /* enumerateFiles() method */
  121. ZIP_exists, /* exists() method */
  122. ZIP_isDirectory, /* isDirectory() method */
  123. ZIP_isSymLink, /* isSymLink() method */
  124. ZIP_getLastModTime, /* getLastModTime() method */
  125. ZIP_openRead, /* openRead() method */
  126. NULL, /* openWrite() method */
  127. NULL, /* openAppend() method */
  128. NULL, /* remove() method */
  129. NULL, /* mkdir() method */
  130. ZIP_dirClose /* dirClose() method */
  131. };
  132. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP =
  133. {
  134. "ZIP",
  135. "PkZip/WinZip/Info-Zip compatible",
  136. "Ryan C. Gordon <icculus@clutteredmind.org>",
  137. "http://www.icculus.org/physfs/",
  138. };
  139. /*
  140. * Wrap all zlib calls in this, so the physfs error state is set appropriately.
  141. */
  142. static int zlib_err(int rc)
  143. {
  144. const char *err = NULL;
  145. switch (rc)
  146. {
  147. case Z_OK:
  148. case Z_STREAM_END:
  149. break; /* not errors. */
  150. case Z_ERRNO:
  151. err = strerror(errno);
  152. break;
  153. case Z_NEED_DICT:
  154. err = "zlib: need dictionary";
  155. break;
  156. case Z_DATA_ERROR:
  157. err = "zlib: need dictionary";
  158. break;
  159. case Z_MEM_ERROR:
  160. err = "zlib: memory error";
  161. break;
  162. case Z_BUF_ERROR:
  163. err = "zlib: buffer error";
  164. break;
  165. case Z_VERSION_ERROR:
  166. err = "zlib: version error";
  167. break;
  168. default:
  169. err = "unknown zlib error";
  170. break;
  171. } /* switch */
  172. if (err != NULL)
  173. __PHYSFS_setError(err);
  174. return(rc);
  175. } /* zlib_err */
  176. /*
  177. * Read an unsigned 32-bit int and swap to native byte order.
  178. */
  179. static int readui32(void *in, PHYSFS_uint32 *val)
  180. {
  181. PHYSFS_uint32 v;
  182. BAIL_IF_MACRO(__PHYSFS_platformRead(in, &v, sizeof (v), 1) != 1, NULL, 0);
  183. *val = PHYSFS_swapULE32(v);
  184. return(1);
  185. } /* readui32 */
  186. /*
  187. * Read an unsigned 16-bit int and swap to native byte order.
  188. */
  189. static int readui16(void *in, PHYSFS_uint16 *val)
  190. {
  191. PHYSFS_uint16 v;
  192. BAIL_IF_MACRO(__PHYSFS_platformRead(in, &v, sizeof (v), 1) != 1, NULL, 0);
  193. *val = PHYSFS_swapULE16(v);
  194. return(1);
  195. } /* readui16 */
  196. static PHYSFS_sint64 ZIP_read(FileHandle *handle, void *buf,
  197. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  198. {
  199. ZIPfileinfo *finfo = (ZIPfileinfo *) (handle->opaque);
  200. ZIPentry *entry = finfo->entry;
  201. PHYSFS_sint64 retval = 0;
  202. PHYSFS_sint64 maxread = ((PHYSFS_sint64) objSize) * objCount;
  203. PHYSFS_sint64 avail = entry->uncompressed_size -
  204. finfo->uncompressed_position;
  205. BAIL_IF_MACRO(maxread == 0, NULL, 0); /* quick rejection. */
  206. if (avail < maxread)
  207. {
  208. maxread = avail - (avail % objSize);
  209. objCount = maxread / objSize;
  210. BAIL_IF_MACRO(objCount == 0, ERR_PAST_EOF, 0); /* quick rejection. */
  211. __PHYSFS_setError(ERR_PAST_EOF); /* this is always true here. */
  212. } /* if */
  213. if (entry->compression_method == COMPMETH_NONE)
  214. {
  215. retval = __PHYSFS_platformRead(finfo->handle, buf, objSize, objCount);
  216. } /* if */
  217. else
  218. {
  219. finfo->stream.next_out = buf;
  220. finfo->stream.avail_out = objSize * objCount;
  221. while (retval < maxread)
  222. {
  223. PHYSFS_uint32 before = finfo->stream.total_out;
  224. int rc;
  225. if (finfo->stream.avail_in == 0)
  226. {
  227. PHYSFS_sint64 br;
  228. br = entry->compressed_size - finfo->compressed_position;
  229. if (br > 0)
  230. {
  231. if (br > ZIP_READBUFSIZE)
  232. br = ZIP_READBUFSIZE;
  233. br = __PHYSFS_platformRead(finfo->handle,
  234. finfo->buffer,
  235. 1, br);
  236. if (br <= 0)
  237. break;
  238. finfo->compressed_position += br;
  239. finfo->stream.next_in = finfo->buffer;
  240. finfo->stream.avail_in = br;
  241. } /* if */
  242. } /* if */
  243. rc = zlib_err(inflate(&finfo->stream, Z_SYNC_FLUSH));
  244. retval += (finfo->stream.total_out - before);
  245. if (rc != Z_OK)
  246. break;
  247. } /* while */
  248. retval /= objSize;
  249. } /* else */
  250. if (retval > 0)
  251. finfo->uncompressed_position += (retval * objSize);
  252. return(retval);
  253. } /* ZIP_read */
  254. static int ZIP_eof(FileHandle *handle)
  255. {
  256. ZIPfileinfo *finfo = ((ZIPfileinfo *) (handle->opaque));
  257. return(finfo->uncompressed_position >= finfo->entry->uncompressed_size);
  258. } /* ZIP_eof */
  259. static PHYSFS_sint64 ZIP_tell(FileHandle *handle)
  260. {
  261. return(((ZIPfileinfo *) (handle->opaque))->uncompressed_position);
  262. } /* ZIP_tell */
  263. static int ZIP_seek(FileHandle *handle, PHYSFS_uint64 offset)
  264. {
  265. ZIPfileinfo *finfo = (ZIPfileinfo *) (handle->opaque);
  266. ZIPentry *entry = finfo->entry;
  267. void *in = finfo->handle;
  268. BAIL_IF_MACRO(offset > entry->uncompressed_size, ERR_PAST_EOF, 0);
  269. if (entry->compression_method == COMPMETH_NONE)
  270. {
  271. PHYSFS_sint64 newpos = offset + entry->offset;
  272. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, newpos), NULL, 0);
  273. finfo->uncompressed_position = newpos;
  274. } /* if */
  275. else
  276. {
  277. /*
  278. * If seeking backwards, we need to redecode the file
  279. * from the start and throw away the compressed bits until we hit
  280. * the offset we need. If seeking forward, we still need to
  281. * redecode, but we don't rewind first.
  282. */
  283. if (offset < finfo->uncompressed_position)
  284. {
  285. /* we do a copy so state is sane if inflateInit2() fails. */
  286. z_stream str;
  287. memset(&str, '\0', sizeof (z_stream));
  288. if (zlib_err(inflateInit2(&str, -MAX_WBITS)) != Z_OK)
  289. return(0);
  290. if (!__PHYSFS_platformSeek(in, entry->offset))
  291. return(0);
  292. inflateEnd(&finfo->stream);
  293. memcpy(&finfo->stream, &str, sizeof (z_stream));
  294. finfo->uncompressed_position = finfo->compressed_position = 0;
  295. } /* if */
  296. while (finfo->uncompressed_position != offset)
  297. {
  298. PHYSFS_uint8 buf[512];
  299. PHYSFS_uint32 maxread = offset - finfo->uncompressed_position;
  300. if (maxread > sizeof (buf))
  301. maxread = sizeof (buf);
  302. if (ZIP_read(handle, buf, maxread, 1) != 1)
  303. return(0);
  304. } /* while */
  305. } /* else */
  306. return(1);
  307. } /* ZIP_seek */
  308. static PHYSFS_sint64 ZIP_fileLength(FileHandle *handle)
  309. {
  310. ZIPfileinfo *finfo = (ZIPfileinfo *) (handle->opaque);
  311. return(finfo->entry->uncompressed_size);
  312. } /* ZIP_fileLength */
  313. static int ZIP_fileClose(FileHandle *handle)
  314. {
  315. ZIPfileinfo *finfo = (ZIPfileinfo *) (handle->opaque);
  316. __PHYSFS_platformClose(finfo->handle);
  317. if (finfo->entry->compression_method != COMPMETH_NONE)
  318. inflateEnd(&finfo->stream);
  319. if (finfo->buffer != NULL)
  320. free(finfo->buffer);
  321. free(finfo);
  322. return(1);
  323. } /* ZIP_fileClose */
  324. static PHYSFS_sint64 find_end_of_central_dir(void *in, PHYSFS_sint64 *len)
  325. {
  326. /* !!! FIXME: potential race condition! */
  327. /* !!! FIXME: mutex this or switch to smaller stack-based buffer. */
  328. static PHYSFS_uint8 buf[0xFFFF + 20];
  329. PHYSFS_sint32 i;
  330. PHYSFS_sint64 filelen;
  331. PHYSFS_sint64 filepos;
  332. PHYSFS_sint32 maxread;
  333. filelen = __PHYSFS_platformFileLength(in);
  334. BAIL_IF_MACRO(filelen == -1, NULL, 0);
  335. /*
  336. * Jump to the end of the file and start reading backwards.
  337. * The last thing in the file is the zipfile comment, which is variable
  338. * length, and the field that specifies its size is before it in the
  339. * file (argh!)...this means that we need to scan backwards until we
  340. * hit the end-of-central-dir signature. We can then sanity check that
  341. * the comment was as big as it should be to make sure we're in the
  342. * right place. The comment length field is 16 bits, so we can stop
  343. * searching for that signature after 64k at most, and call it a
  344. * corrupted zipfile.
  345. */
  346. /*
  347. * !!! FIXME: This was easier than reading backwards in chunks, but it's
  348. * !!! FIXME: rather memory hungry.
  349. */
  350. if (sizeof (buf) < filelen)
  351. {
  352. filepos = filelen - sizeof (buf);
  353. maxread = sizeof (buf);
  354. } /* if */
  355. else
  356. {
  357. filepos = 0;
  358. maxread = filelen;
  359. } /* else */
  360. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, filepos), NULL, -1);
  361. BAIL_IF_MACRO(__PHYSFS_platformRead(in, buf, maxread, 1) != 1, NULL, -1);
  362. for (i = maxread - 4; i > 0; i--)
  363. {
  364. if ((buf[i + 0] == 0x50) &&
  365. (buf[i + 1] == 0x4B) &&
  366. (buf[i + 2] == 0x05) &&
  367. (buf[i + 3] == 0x06) )
  368. {
  369. break; /* that's the signature! */
  370. } /* if */
  371. } /* for */
  372. BAIL_IF_MACRO(i < 0, ERR_NOT_AN_ARCHIVE, -1);
  373. if (len != NULL)
  374. *len = filelen;
  375. return(filelen - (maxread - i));
  376. } /* find_end_of_central_dir */
  377. static int ZIP_isArchive(const char *filename, int forWriting)
  378. {
  379. PHYSFS_uint32 sig;
  380. int retval;
  381. void *in;
  382. in = __PHYSFS_platformOpenRead(filename);
  383. BAIL_IF_MACRO(in == NULL, NULL, 0);
  384. /*
  385. * The first thing in a zip file might be the signature of the
  386. * first local file record, so it makes for a quick determination.
  387. */
  388. BAIL_IF_MACRO(!readui32(in, &sig), NULL, 0);
  389. retval = (sig == ZIP_LOCAL_FILE_SIG);
  390. if (!retval)
  391. {
  392. /*
  393. * No sig...might be a ZIP with data at the start
  394. * (a self-extracting executable, etc), so we'll have to do
  395. * it the hard way...
  396. */
  397. retval = (find_end_of_central_dir(in, NULL) == -1);
  398. } /* if */
  399. __PHYSFS_platformClose(in);
  400. return(retval);
  401. } /* ZIP_isArchive */
  402. static int zip_set_open_position(void *in, ZIPentry *entry)
  403. {
  404. /*
  405. * We fix up the offset to point to the actual data on the
  406. * first open, since we don't want to seek across the whole file on
  407. * archive open (can be SLOW on large, CD-stored files), but we
  408. * need to check the local file header...not just for corruption,
  409. * but since it stores offset info the central directory does not.
  410. */
  411. if (!entry->fixed_up)
  412. {
  413. PHYSFS_uint32 ui32;
  414. PHYSFS_uint16 ui16;
  415. PHYSFS_uint16 fnamelen;
  416. PHYSFS_uint16 extralen;
  417. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, entry->offset), NULL, 0);
  418. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  419. BAIL_IF_MACRO(ui32 != ZIP_LOCAL_FILE_SIG, ERR_CORRUPTED, 0);
  420. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  421. BAIL_IF_MACRO(ui16 != entry->version_needed, ERR_CORRUPTED, 0);
  422. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* general bits. */
  423. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  424. BAIL_IF_MACRO(ui16 != entry->compression_method, ERR_CORRUPTED, 0);
  425. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0); /* date/time */
  426. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  427. BAIL_IF_MACRO(ui32 != entry->crc, ERR_CORRUPTED, 0);
  428. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  429. BAIL_IF_MACRO(ui32 != entry->compressed_size, ERR_CORRUPTED, 0);
  430. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  431. BAIL_IF_MACRO(ui32 != entry->uncompressed_size, ERR_CORRUPTED, 0);
  432. BAIL_IF_MACRO(!readui16(in, &fnamelen), NULL, 0);
  433. BAIL_IF_MACRO(!readui16(in, &extralen), NULL, 0);
  434. entry->offset += fnamelen + extralen + 30;
  435. entry->fixed_up = 1;
  436. } /* if */
  437. return(__PHYSFS_platformSeek(in, entry->offset));
  438. } /* zip_set_open_position */
  439. static void freeEntries(ZIPinfo *info, int count)
  440. {
  441. int i;
  442. for (i = 0; i < count; i++)
  443. {
  444. free(info->entries[i].name);
  445. if (info->entries[i].symlink != NULL)
  446. free(info->entries[i].symlink);
  447. } /* for */
  448. free(info->entries);
  449. } /* freeEntries */
  450. /*
  451. * !!! FIXME: Really implement this.
  452. * !!! FIXME: symlinks in zipfiles can be relative paths, including
  453. * !!! FIXME: "." and ".." entries. These need to be parsed out.
  454. * !!! FIXME: For now, though, we're just copying the relative path. Oh well.
  455. */
  456. static char *expand_symlink_path(char *path, ZIPentry *entry)
  457. {
  458. /*
  459. char *retval = (char *) malloc(strlen(path) + 1);
  460. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  461. strcpy(retval, path);
  462. return(retval);
  463. */
  464. return(path); /* !!! FIXME */
  465. } /* expand_symlink_path */
  466. static char *get_zip_realpath(void *in, ZIPentry *entry)
  467. {
  468. char *path;
  469. PHYSFS_uint32 size = entry->uncompressed_size;
  470. int rc = 0;
  471. BAIL_IF_MACRO(!zip_set_open_position(in, entry), NULL, NULL);
  472. path = (char *) malloc(size + 1);
  473. BAIL_IF_MACRO(path == NULL, ERR_OUT_OF_MEMORY, NULL);
  474. if (entry->compression_method == COMPMETH_NONE)
  475. rc = (__PHYSFS_platformRead(in, path, size, 1) == 1);
  476. else /* symlink target path is compressed... */
  477. {
  478. z_stream stream;
  479. PHYSFS_uint32 compsize = entry->compressed_size;
  480. PHYSFS_uint8 *compressed = (PHYSFS_uint8 *) malloc(compsize);
  481. if (compressed != NULL)
  482. {
  483. if (__PHYSFS_platformRead(in, compressed, compsize, 1) == 1)
  484. {
  485. memset(&stream, '\0', sizeof (z_stream));
  486. stream.next_in = compressed;
  487. stream.avail_in = compsize;
  488. stream.next_out = path;
  489. stream.avail_out = size;
  490. if (zlib_err(inflateInit2(&stream, -MAX_WBITS)) == Z_OK)
  491. {
  492. rc = zlib_err(inflate(&stream, Z_FINISH));
  493. inflateEnd(&stream);
  494. /* both are acceptable outcomes... */
  495. rc = ((rc == Z_OK) || (rc == Z_STREAM_END));
  496. } /* if */
  497. } /* if */
  498. free(compressed);
  499. } /* if */
  500. } /* else */
  501. if (!rc)
  502. {
  503. free(path);
  504. return(NULL);
  505. } /* if */
  506. path[entry->uncompressed_size] = '\0'; /* null-terminate it. */
  507. return(expand_symlink_path(path, entry)); /* retval is realloc()'d path. */
  508. } /* get_zip_realpath */
  509. static int version_does_symlinks(PHYSFS_uint32 version)
  510. {
  511. int retval = 0;
  512. PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((version >> 8) & 0xFF);
  513. switch (hosttype)
  514. {
  515. /*
  516. * These are the platforms that can NOT build an archive with
  517. * symlinks, according to the Info-ZIP project.
  518. */
  519. case 0: /* FS_FAT_ */
  520. case 1: /* AMIGA_ */
  521. case 2: /* VMS_ */
  522. case 4: /* VM_CSM_ */
  523. case 6: /* FS_HPFS_ */
  524. case 11: /* FS_NTFS_ */
  525. case 14: /* FS_VFAT_ */
  526. case 13: /* ACORN_ */
  527. case 15: /* MVS_ */
  528. case 18: /* THEOS_ */
  529. break; /* do nothing. */
  530. default: /* assume the rest to be unix-like. */
  531. retval = 1;
  532. break;
  533. } /* switch */
  534. return(retval);
  535. } /* version_does_symlinks */
  536. static int entry_is_symlink(ZIPentry *entry, PHYSFS_uint32 extern_attr)
  537. {
  538. PHYSFS_uint16 xattr = ((extern_attr >> 16) & 0xFFFF);
  539. return (
  540. (version_does_symlinks(entry->version)) &&
  541. (entry->uncompressed_size > 0) &&
  542. ((xattr & UNIX_FILETYPE_MASK) == UNIX_FILETYPE_SYMLINK)
  543. );
  544. } /* entry_is_symlink */
  545. PHYSFS_sint64 dos_time_to_physfs_time(PHYSFS_uint32 dostime)
  546. {
  547. PHYSFS_uint32 dosdate = (PHYSFS_uint32) (dostime >> 16);
  548. struct tm unixtime;
  549. memset(&unixtime, '\0', sizeof (unixtime));
  550. unixtime.tm_mday = (PHYSFS_uint32) (dosdate & 0x1F);
  551. unixtime.tm_mon = (PHYSFS_uint32) ((((dosdate) & 0x1E0) / 0x20) - 1);
  552. unixtime.tm_year = (PHYSFS_uint32) (((dosdate & 0x0FE00) / 0x0200) + 80);
  553. unixtime.tm_hour = (PHYSFS_uint32) ((dostime & 0xF800) / 0x800);
  554. unixtime.tm_min = (PHYSFS_uint32) ((dostime & 0x7E0) / 0x20);
  555. unixtime.tm_sec = (PHYSFS_uint32) (2 * (dostime & 0x1F));
  556. return((PHYSFS_sint64) mktime(&unixtime));
  557. } /* dos_time_to_physfs_time */
  558. static int load_zip_entry(void *in, ZIPentry *entry, PHYSFS_uint32 ofs_fixup)
  559. {
  560. PHYSFS_uint16 fnamelen, extralen, commentlen;
  561. PHYSFS_uint32 external_attr;
  562. PHYSFS_uint16 ui16;
  563. PHYSFS_uint32 ui32;
  564. PHYSFS_sint64 si64;
  565. /* sanity check with central directory signature... */
  566. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  567. BAIL_IF_MACRO(ui32 != ZIP_CENTRAL_DIR_SIG, ERR_CORRUPTED, 0);
  568. /* Get the pertinent parts of the record... */
  569. BAIL_IF_MACRO(!readui16(in, &entry->version), NULL, 0);
  570. BAIL_IF_MACRO(!readui16(in, &entry->version_needed), NULL, 0);
  571. BAIL_IF_MACRO(!readui16(in, &entry->flag), NULL, 0);
  572. BAIL_IF_MACRO(!readui16(in, &entry->compression_method), NULL, 0);
  573. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  574. entry->last_mod_time = dos_time_to_physfs_time(ui32);
  575. BAIL_IF_MACRO(!readui32(in, &entry->crc), NULL, 0);
  576. BAIL_IF_MACRO(!readui32(in, &entry->compressed_size), NULL, 0);
  577. BAIL_IF_MACRO(!readui32(in, &entry->uncompressed_size), NULL, 0);
  578. BAIL_IF_MACRO(!readui16(in, &fnamelen), NULL, 0);
  579. BAIL_IF_MACRO(!readui16(in, &extralen), NULL, 0);
  580. BAIL_IF_MACRO(!readui16(in, &commentlen), NULL, 0);
  581. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* disk number start */
  582. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* internal file attribs */
  583. BAIL_IF_MACRO(!readui32(in, &external_attr), NULL, 0);
  584. BAIL_IF_MACRO(!readui32(in, &entry->offset), NULL, 0);
  585. entry->offset += ofs_fixup;
  586. entry->fixed_up = 0; /* we need to do a second fixup at openRead(). */
  587. entry->name = (char *) malloc(fnamelen + 1);
  588. BAIL_IF_MACRO(entry->name == NULL, ERR_OUT_OF_MEMORY, 0);
  589. if (__PHYSFS_platformRead(in, entry->name, fnamelen, 1) != 1)
  590. goto load_zip_entry_puked;
  591. entry->name[fnamelen] = '\0'; /* null-terminate the filename. */
  592. si64 = __PHYSFS_platformTell(in);
  593. if (si64 == -1)
  594. goto load_zip_entry_puked;
  595. /* If this is a symlink, resolve it. */
  596. if (!entry_is_symlink(entry, external_attr))
  597. entry->symlink = NULL;
  598. else
  599. {
  600. entry->symlink = get_zip_realpath(in, entry);
  601. if (entry->symlink == NULL)
  602. goto load_zip_entry_puked;
  603. } /* else */
  604. /* seek to the start of the next entry in the central directory... */
  605. if (!__PHYSFS_platformSeek(in, si64 + extralen + commentlen))
  606. goto load_zip_entry_puked;
  607. return(1); /* success. */
  608. load_zip_entry_puked:
  609. free(entry->name);
  610. return(0); /* failure. */
  611. } /* load_zip_entry */
  612. static int load_zip_entries(void *in, DirHandle *dirh,
  613. PHYSFS_uint32 data_ofs, PHYSFS_uint32 central_ofs)
  614. {
  615. ZIPinfo *info = (ZIPinfo *) dirh->opaque;
  616. PHYSFS_uint32 max = info->entryCount;
  617. PHYSFS_uint32 i;
  618. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, central_ofs), NULL, 0);
  619. info->entries = (ZIPentry *) malloc(sizeof (ZIPentry) * max);
  620. BAIL_IF_MACRO(info->entries == NULL, ERR_OUT_OF_MEMORY, 0);
  621. for (i = 0; i < max; i++)
  622. {
  623. if (!load_zip_entry(in, &info->entries[i], data_ofs))
  624. {
  625. freeEntries(info, i);
  626. return(0);
  627. } /* if */
  628. } /* for */
  629. return(1);
  630. } /* load_zip_entries */
  631. static int parse_end_of_central_dir(void *in, DirHandle *dirh,
  632. PHYSFS_uint32 *data_start,
  633. PHYSFS_uint32 *central_dir_ofs)
  634. {
  635. ZIPinfo *zipinfo = (ZIPinfo *) dirh->opaque;
  636. PHYSFS_uint32 ui32;
  637. PHYSFS_uint16 ui16;
  638. PHYSFS_sint64 len;
  639. PHYSFS_sint64 pos;
  640. /* find the end-of-central-dir record, and seek to it. */
  641. pos = find_end_of_central_dir(in, &len);
  642. BAIL_IF_MACRO(pos == -1, NULL, 0);
  643. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, pos), NULL, 0);
  644. /* check signature again, just in case. */
  645. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  646. BAIL_IF_MACRO(ui32 != ZIP_END_OF_CENTRAL_DIR_SIG, ERR_NOT_AN_ARCHIVE, 0);
  647. /* number of this disk */
  648. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  649. BAIL_IF_MACRO(ui16 != 0, ERR_UNSUPPORTED_ARCHIVE, 0);
  650. /* number of the disk with the start of the central directory */
  651. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  652. BAIL_IF_MACRO(ui16 != 0, ERR_UNSUPPORTED_ARCHIVE, 0);
  653. /* total number of entries in the central dir on this disk */
  654. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  655. /* total number of entries in the central dir */
  656. BAIL_IF_MACRO(!readui16(in, &zipinfo->entryCount), NULL, 0);
  657. BAIL_IF_MACRO(ui16 != zipinfo->entryCount, ERR_UNSUPPORTED_ARCHIVE, 0);
  658. /* size of the central directory */
  659. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  660. /* offset of central directory */
  661. BAIL_IF_MACRO(!readui32(in, central_dir_ofs), NULL, 0);
  662. BAIL_IF_MACRO(pos < *central_dir_ofs + ui32, ERR_UNSUPPORTED_ARCHIVE, 0);
  663. /*
  664. * For self-extracting archives, etc, there's crapola in the file
  665. * before the zipfile records; we calculate how much data there is
  666. * prepended by determining how far the central directory offset is
  667. * from where it is supposed to be (start of end-of-central-dir minus
  668. * sizeof central dir)...the difference in bytes is how much arbitrary
  669. * data is at the start of the physical file.
  670. */
  671. *data_start = pos - (*central_dir_ofs + ui32);
  672. /* Now that we know the difference, fix up the central dir offset... */
  673. *central_dir_ofs += *data_start;
  674. /* zipfile comment length */
  675. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  676. /*
  677. * Make sure that the comment length matches to the end of file...
  678. * If it doesn't, we're either in the wrong part of the file, or the
  679. * file is corrupted, but we give up either way.
  680. */
  681. BAIL_IF_MACRO((pos + 22 + ui16) != len, ERR_UNSUPPORTED_ARCHIVE, 0);
  682. return(1); /* made it. */
  683. } /* parse_end_of_central_dir */
  684. static DirHandle *allocate_zip_dirhandle(const char *name)
  685. {
  686. char *ptr;
  687. DirHandle *retval = malloc(sizeof (DirHandle));
  688. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  689. memset(retval, '\0', sizeof (DirHandle));
  690. retval->opaque = malloc(sizeof (ZIPinfo));
  691. if (retval->opaque == NULL)
  692. {
  693. free(retval);
  694. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  695. } /* if */
  696. memset(retval->opaque, '\0', sizeof (ZIPinfo));
  697. ptr = (char *) malloc(strlen(name) + 1);
  698. if (ptr == NULL)
  699. {
  700. free(retval->opaque);
  701. free(retval);
  702. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  703. } /* if */
  704. ((ZIPinfo *) (retval->opaque))->archiveName = ptr;
  705. strcpy(((ZIPinfo *) (retval->opaque))->archiveName, name);
  706. retval->funcs = &__PHYSFS_DirFunctions_ZIP;
  707. return(retval);
  708. } /* allocate_zip_dirhandle */
  709. static DirHandle *ZIP_openArchive(const char *name, int forWriting)
  710. {
  711. DirHandle *retval = NULL;
  712. void *in = NULL;
  713. PHYSFS_uint32 data_start;
  714. PHYSFS_uint32 central_dir_ofs;
  715. int success = 0;
  716. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, NULL);
  717. if ((in = __PHYSFS_platformOpenRead(name)) == NULL)
  718. goto zip_openarchive_end;
  719. if ((retval = allocate_zip_dirhandle(name)) == NULL)
  720. goto zip_openarchive_end;
  721. if (!parse_end_of_central_dir(in, retval, &data_start, &central_dir_ofs))
  722. goto zip_openarchive_end;
  723. if (!load_zip_entries(in, retval, data_start, central_dir_ofs))
  724. goto zip_openarchive_end;
  725. success = 1; /* ...and we're good to go. :) */
  726. zip_openarchive_end:
  727. if (!success) /* clean up for failures. */
  728. {
  729. if (retval != NULL)
  730. {
  731. if (retval->opaque != NULL)
  732. {
  733. if (((ZIPinfo *) (retval->opaque))->archiveName != NULL)
  734. free(((ZIPinfo *) (retval->opaque))->archiveName);
  735. free(retval->opaque);
  736. } /* if */
  737. free(retval);
  738. retval = NULL;
  739. } /* if */
  740. } /* if */
  741. if (in != NULL)
  742. __PHYSFS_platformClose(in); /* Close this even with success. */
  743. return(retval);
  744. } /* ZIP_openArchive */
  745. /* !!! FIXME: This is seriously ugly. */
  746. static LinkedStringList *ZIP_enumerateFiles(DirHandle *h,
  747. const char *dirname,
  748. int omitSymLinks)
  749. {
  750. ZIPinfo *zi = (ZIPinfo *) (h->opaque);
  751. unsigned int i;
  752. int dlen;
  753. LinkedStringList *retval = NULL;
  754. LinkedStringList *l = NULL;
  755. LinkedStringList *prev = NULL;
  756. char *d;
  757. ZIPentry *entry;
  758. char buf[MAXZIPENTRYSIZE];
  759. dlen = strlen(dirname);
  760. d = malloc(dlen + 1);
  761. BAIL_IF_MACRO(d == NULL, ERR_OUT_OF_MEMORY, NULL);
  762. strcpy(d, dirname);
  763. if ((dlen > 0) && (d[dlen - 1] == '/')) /* remove trailing slash. */
  764. {
  765. dlen--;
  766. d[dlen] = '\0';
  767. } /* if */
  768. for (i = 0, entry = zi->entries; i < zi->entryCount; i++, entry++)
  769. {
  770. char *ptr;
  771. char *add_file;
  772. int this_dlen;
  773. if ((omitSymLinks) && (entry->symlink != NULL))
  774. continue;
  775. this_dlen = strlen(entry->name);
  776. if (this_dlen + 1 > MAXZIPENTRYSIZE) /* !!! FIXME */
  777. continue; /* ugh. */
  778. strcpy(buf, entry->name);
  779. if ((this_dlen > 0) && (buf[this_dlen - 1] == '/')) /* no trailing slash. */
  780. {
  781. this_dlen--;
  782. buf[this_dlen] = '\0';
  783. } /* if */
  784. if (this_dlen <= dlen) /* not in this dir. */
  785. continue;
  786. if (*d == '\0')
  787. add_file = buf;
  788. else
  789. {
  790. if (buf[dlen] != '/') /* can't be in same directory? */
  791. continue;
  792. buf[dlen] = '\0';
  793. if (__PHYSFS_platformStricmp(d, buf) != 0) /* not same directory? */
  794. continue;
  795. add_file = buf + dlen + 1;
  796. } /* else */
  797. /* handle subdirectories... */
  798. ptr = strchr(add_file, '/');
  799. if (ptr != NULL)
  800. {
  801. LinkedStringList *j;
  802. *ptr = '\0';
  803. for (j = retval; j != NULL; j = j->next)
  804. {
  805. if (__PHYSFS_platformStricmp(j->str, ptr) == 0)
  806. break;
  807. } /* for */
  808. if (j != NULL)
  809. continue;
  810. } /* if */
  811. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  812. if (l == NULL)
  813. break;
  814. l->str = (char *) malloc(strlen(add_file) + 1);
  815. if (l->str == NULL)
  816. {
  817. free(l);
  818. break;
  819. } /* if */
  820. strcpy(l->str, add_file);
  821. if (retval == NULL)
  822. retval = l;
  823. else
  824. prev->next = l;
  825. prev = l;
  826. l->next = NULL;
  827. } /* for */
  828. free(d);
  829. return(retval);
  830. } /* ZIP_enumerateFiles */
  831. /* !!! FIXME: This is seriously ugly. */
  832. static int ZIP_exists_symcheck(DirHandle *h, const char *name, int follow)
  833. {
  834. char buf[MAXZIPENTRYSIZE];
  835. ZIPinfo *zi = (ZIPinfo *) (h->opaque);
  836. int dlen;
  837. char *d;
  838. unsigned int i;
  839. ZIPentry *entry;
  840. dlen = strlen(name);
  841. d = malloc(dlen + 1);
  842. BAIL_IF_MACRO(d == NULL, ERR_OUT_OF_MEMORY, -1);
  843. strcpy(d, name);
  844. if ((dlen > 0) && (d[dlen - 1] == '/')) /* no trailing slash. */
  845. {
  846. dlen--;
  847. d[dlen] = '\0';
  848. } /* if */
  849. for (i = 0, entry = zi->entries; i < zi->entryCount; i++, entry++)
  850. {
  851. int this_dlen = strlen(entry->name);
  852. if (this_dlen + 1 > MAXZIPENTRYSIZE)
  853. continue; /* ugh. */
  854. strcpy(buf, entry->name);
  855. if ((this_dlen > 0) && (buf[this_dlen - 1] == '/')) /* no trailing slash. */
  856. {
  857. this_dlen--;
  858. buf[this_dlen] = '\0';
  859. } /* if */
  860. if ( ((buf[dlen] == '/') || (buf[dlen] == '\0')) &&
  861. (strncmp(d, buf, dlen) == 0) )
  862. {
  863. int retval = i;
  864. free(d);
  865. if (follow) /* follow symlinks? */
  866. {
  867. if (entry->symlink != NULL)
  868. retval = ZIP_exists_symcheck(h, entry->symlink, follow-1);
  869. } /* if */
  870. return(retval);
  871. } /* if */
  872. } /* for */
  873. free(d);
  874. return(-1);
  875. } /* ZIP_exists_symcheck */
  876. static int ZIP_exists(DirHandle *h, const char *name)
  877. {
  878. int pos = ZIP_exists_symcheck(h, name, SYMLINK_RECURSE_COUNT);
  879. int is_sym;
  880. BAIL_IF_MACRO(pos == -1, ERR_NO_SUCH_FILE, 0);
  881. /* if it's a symlink, then we ran into a possible symlink loop. */
  882. is_sym = ( ((ZIPinfo *)(h->opaque))->entries[pos].symlink != NULL );
  883. BAIL_IF_MACRO(is_sym, ERR_SYMLINK_LOOP, 0);
  884. return(1);
  885. } /* ZIP_exists */
  886. static PHYSFS_sint64 ZIP_getLastModTime(DirHandle *h, const char *name)
  887. {
  888. ZIPinfo *zi = (ZIPinfo *) (h->opaque);
  889. int pos = ZIP_exists_symcheck(h, name, SYMLINK_RECURSE_COUNT);
  890. ZIPentry *entry;
  891. BAIL_IF_MACRO(pos == -1, ERR_NO_SUCH_FILE, 0);
  892. entry = &zi->entries[pos];
  893. /* if it's a symlink, then we ran into a possible symlink loop. */
  894. BAIL_IF_MACRO(entry->symlink != NULL, ERR_SYMLINK_LOOP, 0);
  895. return(entry->last_mod_time);
  896. } /* ZIP_getLastModTime */
  897. static int ZIP_isDirectory(DirHandle *h, const char *name)
  898. {
  899. int dlen;
  900. int is_sym;
  901. int pos = ZIP_exists_symcheck(h, name, SYMLINK_RECURSE_COUNT);
  902. BAIL_IF_MACRO(pos == -1, ERR_NO_SUCH_FILE, 0);
  903. /* if it's a symlink, then we ran into a possible symlink loop. */
  904. is_sym = ( ((ZIPinfo *)(h->opaque))->entries[pos].symlink != NULL );
  905. BAIL_IF_MACRO(is_sym, ERR_SYMLINK_LOOP, 0);
  906. dlen = strlen(name);
  907. /* !!! FIXME: yikes. Better way to check? */
  908. return((((ZIPinfo *)(h->opaque))->entries[pos].name[dlen] == '/'));
  909. } /* ZIP_isDirectory */
  910. static int ZIP_isSymLink(DirHandle *h, const char *name)
  911. {
  912. int pos = ZIP_exists_symcheck(h, name, 0);
  913. BAIL_IF_MACRO(pos == -1, ERR_NO_SUCH_FILE, 0);
  914. return( ((ZIPinfo *)(h->opaque))->entries[pos].symlink != NULL );
  915. } /* ZIP_isSymLink */
  916. static FileHandle *ZIP_openRead(DirHandle *h, const char *filename)
  917. {
  918. FileHandle *retval = NULL;
  919. ZIPinfo *zi = ((ZIPinfo *) (h->opaque));
  920. ZIPfileinfo *finfo = NULL;
  921. int pos = ZIP_exists_symcheck(h, filename, SYMLINK_RECURSE_COUNT);
  922. void *in;
  923. BAIL_IF_MACRO(pos == -1, ERR_NO_SUCH_FILE, NULL);
  924. /* if it's a symlink, then we ran into a possible symlink loop. */
  925. BAIL_IF_MACRO(zi->entries[pos].symlink != NULL, ERR_SYMLINK_LOOP, 0);
  926. in = __PHYSFS_platformOpenRead(zi->archiveName);
  927. BAIL_IF_MACRO(in == NULL, ERR_IO_ERROR, NULL);
  928. if (!zip_set_open_position(in, &zi->entries[pos]))
  929. {
  930. __PHYSFS_platformClose(in);
  931. return(0);
  932. } /* if */
  933. if ( ((retval = (FileHandle *) malloc(sizeof (FileHandle))) == NULL) ||
  934. ((finfo = (ZIPfileinfo *) malloc(sizeof (ZIPfileinfo))) == NULL) )
  935. {
  936. if (retval)
  937. free(retval);
  938. __PHYSFS_platformClose(in);
  939. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  940. } /* if */
  941. retval->opaque = (void *) finfo;
  942. retval->funcs = &__PHYSFS_FileFunctions_ZIP;
  943. retval->dirHandle = h;
  944. memset(finfo, '\0', sizeof (ZIPfileinfo));
  945. finfo->handle = in;
  946. finfo->entry = &zi->entries[pos];
  947. if (finfo->entry->compression_method != COMPMETH_NONE)
  948. {
  949. if (zlib_err(inflateInit2(&finfo->stream, -MAX_WBITS)) != Z_OK)
  950. {
  951. ZIP_fileClose(retval);
  952. return(NULL);
  953. } /* if */
  954. finfo->buffer = (PHYSFS_uint8 *) malloc(ZIP_READBUFSIZE);
  955. if (finfo->buffer == NULL)
  956. {
  957. ZIP_fileClose(retval);
  958. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  959. } /* if */
  960. } /* if */
  961. return(retval);
  962. } /* ZIP_openRead */
  963. static void ZIP_dirClose(DirHandle *h)
  964. {
  965. ZIPinfo *zi = (ZIPinfo *) (h->opaque);
  966. freeEntries(zi, zi->entryCount);
  967. free(zi->archiveName);
  968. free(zi);
  969. free(h);
  970. } /* ZIP_dirClose */
  971. #endif /* defined PHYSFS_SUPPORTS_ZIP */
  972. /* end of zip.c ... */