zip.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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. inflateEnd(&finfo->stream);
  291. memcpy(&finfo->stream, &str, sizeof (z_stream));
  292. finfo->uncompressed_position = finfo->compressed_position = 0;
  293. } /* if */
  294. while (finfo->uncompressed_position != offset)
  295. {
  296. PHYSFS_uint8 buf[512];
  297. PHYSFS_uint32 maxread = offset - finfo->uncompressed_position;
  298. if (maxread > sizeof (buf))
  299. maxread = sizeof (buf);
  300. if (ZIP_read(handle, buf, maxread, 1) != 1)
  301. return(0);
  302. } /* while */
  303. } /* else */
  304. return(1);
  305. } /* ZIP_seek */
  306. static PHYSFS_sint64 ZIP_fileLength(FileHandle *handle)
  307. {
  308. ZIPfileinfo *finfo = (ZIPfileinfo *) (handle->opaque);
  309. return(finfo->entry->uncompressed_size);
  310. } /* ZIP_fileLength */
  311. static int ZIP_fileClose(FileHandle *handle)
  312. {
  313. ZIPfileinfo *finfo = (ZIPfileinfo *) (handle->opaque);
  314. __PHYSFS_platformClose(finfo->handle);
  315. if (finfo->entry->compression_method != COMPMETH_NONE)
  316. inflateEnd(&finfo->stream);
  317. if (finfo->buffer != NULL)
  318. free(finfo->buffer);
  319. free(finfo);
  320. return(1);
  321. } /* ZIP_fileClose */
  322. static PHYSFS_sint64 find_end_of_central_dir(void *in, PHYSFS_sint64 *len)
  323. {
  324. /* !!! FIXME: potential race condition! */
  325. /* !!! FIXME: mutex this or switch to smaller stack-based buffer. */
  326. static PHYSFS_uint8 buf[0xFFFF + 20];
  327. PHYSFS_sint32 i;
  328. PHYSFS_sint64 filelen;
  329. PHYSFS_sint64 filepos;
  330. PHYSFS_sint32 maxread;
  331. filelen = __PHYSFS_platformFileLength(in);
  332. BAIL_IF_MACRO(filelen == -1, NULL, 0);
  333. /*
  334. * Jump to the end of the file and start reading backwards.
  335. * The last thing in the file is the zipfile comment, which is variable
  336. * length, and the field that specifies its size is before it in the
  337. * file (argh!)...this means that we need to scan backwards until we
  338. * hit the end-of-central-dir signature. We can then sanity check that
  339. * the comment was as big as it should be to make sure we're in the
  340. * right place. The comment length field is 16 bits, so we can stop
  341. * searching for that signature after 64k at most, and call it a
  342. * corrupted zipfile.
  343. */
  344. /*
  345. * !!! FIXME: This was easier than reading backwards in chunks, but it's
  346. * !!! FIXME: rather memory hungry.
  347. */
  348. if (sizeof (buf) < filelen)
  349. {
  350. filepos = filelen - sizeof (buf);
  351. maxread = sizeof (buf);
  352. } /* if */
  353. else
  354. {
  355. filepos = 0;
  356. maxread = filelen;
  357. } /* else */
  358. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, filepos), NULL, -1);
  359. BAIL_IF_MACRO(__PHYSFS_platformRead(in, buf, maxread, 1) != 1, NULL, -1);
  360. for (i = maxread - 4; i > 0; i--)
  361. {
  362. if ((buf[i + 0] == 0x50) &&
  363. (buf[i + 1] == 0x4B) &&
  364. (buf[i + 2] == 0x05) &&
  365. (buf[i + 3] == 0x06) )
  366. {
  367. break; /* that's the signature! */
  368. } /* if */
  369. } /* for */
  370. BAIL_IF_MACRO(i < 0, ERR_NOT_AN_ARCHIVE, -1);
  371. if (len != NULL)
  372. *len = filelen;
  373. return(filelen - (maxread - i));
  374. } /* find_end_of_central_dir */
  375. static int ZIP_isArchive(const char *filename, int forWriting)
  376. {
  377. PHYSFS_uint32 sig;
  378. int retval;
  379. void *in;
  380. in = __PHYSFS_platformOpenRead(filename);
  381. BAIL_IF_MACRO(in == NULL, NULL, 0);
  382. /*
  383. * The first thing in a zip file might be the signature of the
  384. * first local file record, so it makes for a quick determination.
  385. */
  386. BAIL_IF_MACRO(!readui32(in, &sig), NULL, 0);
  387. retval = (sig == ZIP_LOCAL_FILE_SIG);
  388. if (!retval)
  389. {
  390. /*
  391. * No sig...might be a ZIP with data at the start
  392. * (a self-extracting executable, etc), so we'll have to do
  393. * it the hard way...
  394. */
  395. retval = (find_end_of_central_dir(in, NULL) == -1);
  396. } /* if */
  397. __PHYSFS_platformClose(in);
  398. return(retval);
  399. } /* ZIP_isArchive */
  400. static int zip_set_open_position(void *in, ZIPentry *entry)
  401. {
  402. /*
  403. * We fix up the offset to point to the actual data on the
  404. * first open, since we don't want to seek across the whole file on
  405. * archive open (can be SLOW on large, CD-stored files), but we
  406. * need to check the local file header...not just for corruption,
  407. * but since it stores offset info the central directory does not.
  408. */
  409. if (!entry->fixed_up)
  410. {
  411. PHYSFS_uint32 ui32;
  412. PHYSFS_uint16 ui16;
  413. PHYSFS_uint16 fnamelen;
  414. PHYSFS_uint16 extralen;
  415. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, entry->offset), NULL, 0);
  416. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  417. BAIL_IF_MACRO(ui32 != ZIP_LOCAL_FILE_SIG, ERR_CORRUPTED, 0);
  418. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  419. BAIL_IF_MACRO(ui16 != entry->version_needed, ERR_CORRUPTED, 0);
  420. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* general bits. */
  421. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  422. BAIL_IF_MACRO(ui16 != entry->compression_method, ERR_CORRUPTED, 0);
  423. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0); /* date/time */
  424. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  425. BAIL_IF_MACRO(ui32 != entry->crc, ERR_CORRUPTED, 0);
  426. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  427. BAIL_IF_MACRO(ui32 != entry->compressed_size, ERR_CORRUPTED, 0);
  428. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  429. BAIL_IF_MACRO(ui32 != entry->uncompressed_size, ERR_CORRUPTED, 0);
  430. BAIL_IF_MACRO(!readui16(in, &fnamelen), NULL, 0);
  431. BAIL_IF_MACRO(!readui16(in, &extralen), NULL, 0);
  432. entry->offset += fnamelen + extralen + 30;
  433. entry->fixed_up = 1;
  434. } /* if */
  435. return(__PHYSFS_platformSeek(in, entry->offset));
  436. } /* zip_set_open_position */
  437. static void freeEntries(ZIPinfo *info, int count)
  438. {
  439. int i;
  440. for (i = 0; i < count; i++)
  441. {
  442. free(info->entries[i].name);
  443. if (info->entries[i].symlink != NULL)
  444. free(info->entries[i].symlink);
  445. } /* for */
  446. free(info->entries);
  447. } /* freeEntries */
  448. /*
  449. * !!! FIXME: Really implement this.
  450. * !!! FIXME: symlinks in zipfiles can be relative paths, including
  451. * !!! FIXME: "." and ".." entries. These need to be parsed out.
  452. * !!! FIXME: For now, though, we're just copying the relative path. Oh well.
  453. */
  454. static char *expand_symlink_path(char *path, ZIPentry *entry)
  455. {
  456. /*
  457. char *retval = (char *) malloc(strlen(path) + 1);
  458. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  459. strcpy(retval, path);
  460. return(retval);
  461. */
  462. return(path); /* !!! FIXME */
  463. } /* expand_symlink_path */
  464. static char *get_zip_realpath(void *in, ZIPentry *entry)
  465. {
  466. char *path;
  467. PHYSFS_uint32 size = entry->uncompressed_size;
  468. int rc = 0;
  469. BAIL_IF_MACRO(!zip_set_open_position(in, entry), NULL, NULL);
  470. path = (char *) malloc(size + 1);
  471. BAIL_IF_MACRO(path == NULL, ERR_OUT_OF_MEMORY, NULL);
  472. if (entry->compression_method == COMPMETH_NONE)
  473. rc = (__PHYSFS_platformRead(in, path, size, 1) == 1);
  474. else /* symlink target path is compressed... */
  475. {
  476. z_stream stream;
  477. PHYSFS_uint32 compsize = entry->compressed_size;
  478. PHYSFS_uint8 *compressed = (PHYSFS_uint8 *) malloc(compsize);
  479. if (compressed != NULL)
  480. {
  481. if (__PHYSFS_platformRead(in, compressed, compsize, 1) == 1)
  482. {
  483. memset(&stream, '\0', sizeof (z_stream));
  484. stream.next_in = compressed;
  485. stream.avail_in = compsize;
  486. stream.next_out = path;
  487. stream.avail_out = size;
  488. if (zlib_err(inflateInit2(&stream, -MAX_WBITS)) == Z_OK)
  489. {
  490. rc = zlib_err(inflate(&stream, Z_FINISH));
  491. inflateEnd(&stream);
  492. /* both are acceptable outcomes... */
  493. rc = ((rc == Z_OK) || (rc == Z_STREAM_END));
  494. } /* if */
  495. } /* if */
  496. free(compressed);
  497. } /* if */
  498. } /* else */
  499. if (!rc)
  500. {
  501. free(path);
  502. return(NULL);
  503. } /* if */
  504. path[entry->uncompressed_size] = '\0'; /* null-terminate it. */
  505. return(expand_symlink_path(path, entry)); /* retval is realloc()'d path. */
  506. } /* get_zip_realpath */
  507. static int version_does_symlinks(PHYSFS_uint32 version)
  508. {
  509. int retval = 0;
  510. PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((version >> 8) & 0xFF);
  511. switch (hosttype)
  512. {
  513. /*
  514. * These are the platforms that can NOT build an archive with
  515. * symlinks, according to the Info-ZIP project.
  516. */
  517. case 0: /* FS_FAT_ */
  518. case 1: /* AMIGA_ */
  519. case 2: /* VMS_ */
  520. case 4: /* VM_CSM_ */
  521. case 6: /* FS_HPFS_ */
  522. case 11: /* FS_NTFS_ */
  523. case 14: /* FS_VFAT_ */
  524. case 13: /* ACORN_ */
  525. case 15: /* MVS_ */
  526. case 18: /* THEOS_ */
  527. break; /* do nothing. */
  528. default: /* assume the rest to be unix-like. */
  529. retval = 1;
  530. break;
  531. } /* switch */
  532. return(retval);
  533. } /* version_does_symlinks */
  534. static int entry_is_symlink(ZIPentry *entry, PHYSFS_uint32 extern_attr)
  535. {
  536. PHYSFS_uint16 xattr = ((extern_attr >> 16) & 0xFFFF);
  537. return (
  538. (version_does_symlinks(entry->version)) &&
  539. (entry->uncompressed_size > 0) &&
  540. ((xattr & UNIX_FILETYPE_MASK) == UNIX_FILETYPE_SYMLINK)
  541. );
  542. } /* entry_is_symlink */
  543. PHYSFS_sint64 dos_time_to_physfs_time(PHYSFS_uint32 dostime)
  544. {
  545. PHYSFS_uint32 dosdate = (PHYSFS_uint32) (dostime >> 16);
  546. struct tm unixtime;
  547. memset(&unixtime, '\0', sizeof (unixtime));
  548. unixtime.tm_mday = (PHYSFS_uint32) (dosdate & 0x1F);
  549. unixtime.tm_mon = (PHYSFS_uint32) ((((dosdate) & 0x1E0) / 0x20) - 1);
  550. unixtime.tm_year = (PHYSFS_uint32) (((dosdate & 0x0FE00) / 0x0200) + 80);
  551. unixtime.tm_hour = (PHYSFS_uint32) ((dostime & 0xF800) / 0x800);
  552. unixtime.tm_min = (PHYSFS_uint32) ((dostime & 0x7E0) / 0x20);
  553. unixtime.tm_sec = (PHYSFS_uint32) (2 * (dostime & 0x1F));
  554. return((PHYSFS_sint64) mktime(&unixtime));
  555. } /* dos_time_to_physfs_time */
  556. static int load_zip_entry(void *in, ZIPentry *entry, PHYSFS_uint32 ofs_fixup)
  557. {
  558. PHYSFS_uint16 fnamelen, extralen, commentlen;
  559. PHYSFS_uint32 external_attr;
  560. PHYSFS_uint16 ui16;
  561. PHYSFS_uint32 ui32;
  562. PHYSFS_sint64 si64;
  563. /* sanity check with central directory signature... */
  564. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  565. BAIL_IF_MACRO(ui32 != ZIP_CENTRAL_DIR_SIG, ERR_CORRUPTED, 0);
  566. /* Get the pertinent parts of the record... */
  567. BAIL_IF_MACRO(!readui16(in, &entry->version), NULL, 0);
  568. BAIL_IF_MACRO(!readui16(in, &entry->version_needed), NULL, 0);
  569. BAIL_IF_MACRO(!readui16(in, &entry->flag), NULL, 0);
  570. BAIL_IF_MACRO(!readui16(in, &entry->compression_method), NULL, 0);
  571. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  572. entry->last_mod_time = dos_time_to_physfs_time(ui32);
  573. BAIL_IF_MACRO(!readui32(in, &entry->crc), NULL, 0);
  574. BAIL_IF_MACRO(!readui32(in, &entry->compressed_size), NULL, 0);
  575. BAIL_IF_MACRO(!readui32(in, &entry->uncompressed_size), NULL, 0);
  576. BAIL_IF_MACRO(!readui16(in, &fnamelen), NULL, 0);
  577. BAIL_IF_MACRO(!readui16(in, &extralen), NULL, 0);
  578. BAIL_IF_MACRO(!readui16(in, &commentlen), NULL, 0);
  579. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* disk number start */
  580. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0); /* internal file attribs */
  581. BAIL_IF_MACRO(!readui32(in, &external_attr), NULL, 0);
  582. BAIL_IF_MACRO(!readui32(in, &entry->offset), NULL, 0);
  583. entry->offset += ofs_fixup;
  584. entry->fixed_up = 0; /* we need to do a second fixup at openRead(). */
  585. entry->name = (char *) malloc(fnamelen + 1);
  586. BAIL_IF_MACRO(entry->name == NULL, ERR_OUT_OF_MEMORY, 0);
  587. if (__PHYSFS_platformRead(in, entry->name, fnamelen, 1) != 1)
  588. goto load_zip_entry_puked;
  589. entry->name[fnamelen] = '\0'; /* null-terminate the filename. */
  590. si64 = __PHYSFS_platformTell(in);
  591. if (si64 == -1)
  592. goto load_zip_entry_puked;
  593. /* If this is a symlink, resolve it. */
  594. if (!entry_is_symlink(entry, external_attr))
  595. entry->symlink = NULL;
  596. else
  597. {
  598. entry->symlink = get_zip_realpath(in, entry);
  599. if (entry->symlink == NULL)
  600. goto load_zip_entry_puked;
  601. } /* else */
  602. /* seek to the start of the next entry in the central directory... */
  603. if (!__PHYSFS_platformSeek(in, si64 + extralen + commentlen))
  604. goto load_zip_entry_puked;
  605. return(1); /* success. */
  606. load_zip_entry_puked:
  607. free(entry->name);
  608. return(0); /* failure. */
  609. } /* load_zip_entry */
  610. static int load_zip_entries(void *in, DirHandle *dirh,
  611. PHYSFS_uint32 data_ofs, PHYSFS_uint32 central_ofs)
  612. {
  613. ZIPinfo *info = (ZIPinfo *) dirh->opaque;
  614. PHYSFS_uint32 max = info->entryCount;
  615. PHYSFS_uint32 i;
  616. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, central_ofs), NULL, 0);
  617. info->entries = (ZIPentry *) malloc(sizeof (ZIPentry) * max);
  618. BAIL_IF_MACRO(info->entries == NULL, ERR_OUT_OF_MEMORY, 0);
  619. for (i = 0; i < max; i++)
  620. {
  621. if (!load_zip_entry(in, &info->entries[i], data_ofs))
  622. {
  623. freeEntries(info, i);
  624. return(0);
  625. } /* if */
  626. } /* for */
  627. return(1);
  628. } /* load_zip_entries */
  629. static int parse_end_of_central_dir(void *in, DirHandle *dirh,
  630. PHYSFS_uint32 *data_start,
  631. PHYSFS_uint32 *central_dir_ofs)
  632. {
  633. ZIPinfo *zipinfo = (ZIPinfo *) dirh->opaque;
  634. PHYSFS_uint32 ui32;
  635. PHYSFS_uint16 ui16;
  636. PHYSFS_sint64 len;
  637. PHYSFS_sint64 pos;
  638. /* find the end-of-central-dir record, and seek to it. */
  639. pos = find_end_of_central_dir(in, &len);
  640. BAIL_IF_MACRO(pos == -1, NULL, 0);
  641. BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, pos), NULL, 0);
  642. /* check signature again, just in case. */
  643. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  644. BAIL_IF_MACRO(ui32 != ZIP_END_OF_CENTRAL_DIR_SIG, ERR_NOT_AN_ARCHIVE, 0);
  645. /* number of this disk */
  646. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  647. BAIL_IF_MACRO(ui16 != 0, ERR_UNSUPPORTED_ARCHIVE, 0);
  648. /* number of the disk with the start of the central directory */
  649. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  650. BAIL_IF_MACRO(ui16 != 0, ERR_UNSUPPORTED_ARCHIVE, 0);
  651. /* total number of entries in the central dir on this disk */
  652. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  653. /* total number of entries in the central dir */
  654. BAIL_IF_MACRO(!readui16(in, &zipinfo->entryCount), NULL, 0);
  655. BAIL_IF_MACRO(ui16 != zipinfo->entryCount, ERR_UNSUPPORTED_ARCHIVE, 0);
  656. /* size of the central directory */
  657. BAIL_IF_MACRO(!readui32(in, &ui32), NULL, 0);
  658. /* offset of central directory */
  659. BAIL_IF_MACRO(!readui32(in, central_dir_ofs), NULL, 0);
  660. BAIL_IF_MACRO(pos < *central_dir_ofs + ui32, ERR_UNSUPPORTED_ARCHIVE, 0);
  661. /*
  662. * For self-extracting archives, etc, there's crapola in the file
  663. * before the zipfile records; we calculate how much data there is
  664. * prepended by determining how far the central directory offset is
  665. * from where it is supposed to be (start of end-of-central-dir minus
  666. * sizeof central dir)...the difference in bytes is how much arbitrary
  667. * data is at the start of the physical file.
  668. */
  669. *data_start = pos - (*central_dir_ofs + ui32);
  670. /* Now that we know the difference, fix up the central dir offset... */
  671. *central_dir_ofs += *data_start;
  672. /* zipfile comment length */
  673. BAIL_IF_MACRO(!readui16(in, &ui16), NULL, 0);
  674. /*
  675. * Make sure that the comment length matches to the end of file...
  676. * If it doesn't, we're either in the wrong part of the file, or the
  677. * file is corrupted, but we give up either way.
  678. */
  679. BAIL_IF_MACRO((pos + 22 + ui16) != len, ERR_UNSUPPORTED_ARCHIVE, 0);
  680. return(1); /* made it. */
  681. } /* parse_end_of_central_dir */
  682. static DirHandle *allocate_zip_dirhandle(const char *name)
  683. {
  684. char *ptr;
  685. DirHandle *retval = malloc(sizeof (DirHandle));
  686. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  687. memset(retval, '\0', sizeof (DirHandle));
  688. retval->opaque = malloc(sizeof (ZIPinfo));
  689. if (retval->opaque == NULL)
  690. {
  691. free(retval);
  692. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  693. } /* if */
  694. memset(retval->opaque, '\0', sizeof (ZIPinfo));
  695. ptr = (char *) malloc(strlen(name) + 1);
  696. if (ptr == NULL)
  697. {
  698. free(retval->opaque);
  699. free(retval);
  700. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  701. } /* if */
  702. ((ZIPinfo *) (retval->opaque))->archiveName = ptr;
  703. strcpy(((ZIPinfo *) (retval->opaque))->archiveName, name);
  704. retval->funcs = &__PHYSFS_DirFunctions_ZIP;
  705. return(retval);
  706. } /* allocate_zip_dirhandle */
  707. static DirHandle *ZIP_openArchive(const char *name, int forWriting)
  708. {
  709. DirHandle *retval = NULL;
  710. void *in = NULL;
  711. PHYSFS_uint32 data_start;
  712. PHYSFS_uint32 central_dir_ofs;
  713. int success = 0;
  714. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, NULL);
  715. if ((in = __PHYSFS_platformOpenRead(name)) == NULL)
  716. goto zip_openarchive_end;
  717. if ((retval = allocate_zip_dirhandle(name)) == NULL)
  718. goto zip_openarchive_end;
  719. if (!parse_end_of_central_dir(in, retval, &data_start, &central_dir_ofs))
  720. goto zip_openarchive_end;
  721. if (!load_zip_entries(in, retval, data_start, central_dir_ofs))
  722. goto zip_openarchive_end;
  723. success = 1; /* ...and we're good to go. :) */
  724. zip_openarchive_end:
  725. if (!success) /* clean up for failures. */
  726. {
  727. if (retval != NULL)
  728. {
  729. if (retval->opaque != NULL)
  730. {
  731. if (((ZIPinfo *) (retval->opaque))->archiveName != NULL)
  732. free(((ZIPinfo *) (retval->opaque))->archiveName);
  733. free(retval->opaque);
  734. } /* if */
  735. free(retval);
  736. retval = NULL;
  737. } /* if */
  738. } /* if */
  739. if (in != NULL)
  740. __PHYSFS_platformClose(in); /* Close this even with success. */
  741. return(retval);
  742. } /* ZIP_openArchive */
  743. /* !!! FIXME: This is seriously ugly. */
  744. static LinkedStringList *ZIP_enumerateFiles(DirHandle *h,
  745. const char *dirname,
  746. int omitSymLinks)
  747. {
  748. ZIPinfo *zi = (ZIPinfo *) (h->opaque);
  749. unsigned int i;
  750. int dlen;
  751. LinkedStringList *retval = NULL;
  752. LinkedStringList *l = NULL;
  753. LinkedStringList *prev = NULL;
  754. char *d;
  755. ZIPentry *entry;
  756. char buf[MAXZIPENTRYSIZE];
  757. dlen = strlen(dirname);
  758. d = malloc(dlen + 1);
  759. BAIL_IF_MACRO(d == NULL, ERR_OUT_OF_MEMORY, NULL);
  760. strcpy(d, dirname);
  761. if ((dlen > 0) && (d[dlen - 1] == '/')) /* remove trailing slash. */
  762. {
  763. dlen--;
  764. d[dlen] = '\0';
  765. } /* if */
  766. for (i = 0, entry = zi->entries; i < zi->entryCount; i++, entry++)
  767. {
  768. char *ptr;
  769. char *add_file;
  770. int this_dlen;
  771. if ((omitSymLinks) && (entry->symlink != NULL))
  772. continue;
  773. this_dlen = strlen(entry->name);
  774. if (this_dlen + 1 > MAXZIPENTRYSIZE) /* !!! FIXME */
  775. continue; /* ugh. */
  776. strcpy(buf, entry->name);
  777. if ((this_dlen > 0) && (buf[this_dlen - 1] == '/')) /* no trailing slash. */
  778. {
  779. this_dlen--;
  780. buf[this_dlen] = '\0';
  781. } /* if */
  782. if (this_dlen <= dlen) /* not in this dir. */
  783. continue;
  784. if (*d == '\0')
  785. add_file = buf;
  786. else
  787. {
  788. if (buf[dlen] != '/') /* can't be in same directory? */
  789. continue;
  790. buf[dlen] = '\0';
  791. if (__PHYSFS_platformStricmp(d, buf) != 0) /* not same directory? */
  792. continue;
  793. add_file = buf + dlen + 1;
  794. } /* else */
  795. /* handle subdirectories... */
  796. ptr = strchr(add_file, '/');
  797. if (ptr != NULL)
  798. {
  799. LinkedStringList *j;
  800. *ptr = '\0';
  801. for (j = retval; j != NULL; j = j->next)
  802. {
  803. if (__PHYSFS_platformStricmp(j->str, ptr) == 0)
  804. break;
  805. } /* for */
  806. if (j != NULL)
  807. continue;
  808. } /* if */
  809. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  810. if (l == NULL)
  811. break;
  812. l->str = (char *) malloc(strlen(add_file) + 1);
  813. if (l->str == NULL)
  814. {
  815. free(l);
  816. break;
  817. } /* if */
  818. strcpy(l->str, add_file);
  819. if (retval == NULL)
  820. retval = l;
  821. else
  822. prev->next = l;
  823. prev = l;
  824. l->next = NULL;
  825. } /* for */
  826. free(d);
  827. return(retval);
  828. } /* ZIP_enumerateFiles */
  829. /* !!! FIXME: This is seriously ugly. */
  830. static int ZIP_exists_symcheck(DirHandle *h, const char *name, int follow)
  831. {
  832. char buf[MAXZIPENTRYSIZE];
  833. ZIPinfo *zi = (ZIPinfo *) (h->opaque);
  834. int dlen;
  835. char *d;
  836. unsigned int i;
  837. ZIPentry *entry;
  838. dlen = strlen(name);
  839. d = malloc(dlen + 1);
  840. BAIL_IF_MACRO(d == NULL, ERR_OUT_OF_MEMORY, -1);
  841. strcpy(d, name);
  842. if ((dlen > 0) && (d[dlen - 1] == '/')) /* no trailing slash. */
  843. {
  844. dlen--;
  845. d[dlen] = '\0';
  846. } /* if */
  847. for (i = 0, entry = zi->entries; i < zi->entryCount; i++, entry++)
  848. {
  849. int this_dlen = strlen(entry->name);
  850. if (this_dlen + 1 > MAXZIPENTRYSIZE)
  851. continue; /* ugh. */
  852. strcpy(buf, entry->name);
  853. if ((this_dlen > 0) && (buf[this_dlen - 1] == '/')) /* no trailing slash. */
  854. {
  855. this_dlen--;
  856. buf[this_dlen] = '\0';
  857. } /* if */
  858. if ( ((buf[dlen] == '/') || (buf[dlen] == '\0')) &&
  859. (strncmp(d, buf, dlen) == 0) )
  860. {
  861. int retval = i;
  862. free(d);
  863. if (follow) /* follow symlinks? */
  864. {
  865. if (entry->symlink != NULL)
  866. retval = ZIP_exists_symcheck(h, entry->symlink, follow-1);
  867. } /* if */
  868. return(retval);
  869. } /* if */
  870. } /* for */
  871. free(d);
  872. return(-1);
  873. } /* ZIP_exists_symcheck */
  874. static int ZIP_exists(DirHandle *h, const char *name)
  875. {
  876. int pos = ZIP_exists_symcheck(h, name, SYMLINK_RECURSE_COUNT);
  877. int is_sym;
  878. BAIL_IF_MACRO(pos == -1, ERR_NO_SUCH_FILE, 0);
  879. /* if it's a symlink, then we ran into a possible symlink loop. */
  880. is_sym = ( ((ZIPinfo *)(h->opaque))->entries[pos].symlink != NULL );
  881. BAIL_IF_MACRO(is_sym, ERR_TOO_MANY_SYMLINKS, 0);
  882. return(1);
  883. } /* ZIP_exists */
  884. static PHYSFS_sint64 ZIP_getLastModTime(DirHandle *h, const char *name)
  885. {
  886. ZIPinfo *zi = (ZIPinfo *) (h->opaque);
  887. int pos = ZIP_exists_symcheck(h, name, SYMLINK_RECURSE_COUNT);
  888. ZIPentry *entry;
  889. BAIL_IF_MACRO(pos == -1, ERR_NO_SUCH_FILE, 0);
  890. entry = &zi->entries[pos];
  891. /* if it's a symlink, then we ran into a possible symlink loop. */
  892. BAIL_IF_MACRO(entry->symlink != NULL, ERR_TOO_MANY_SYMLINKS, 0);
  893. return(entry->last_mod_time);
  894. } /* ZIP_getLastModTime */
  895. static int ZIP_isDirectory(DirHandle *h, const char *name)
  896. {
  897. int dlen;
  898. int is_sym;
  899. int pos = ZIP_exists_symcheck(h, name, SYMLINK_RECURSE_COUNT);
  900. BAIL_IF_MACRO(pos == -1, ERR_NO_SUCH_FILE, 0);
  901. /* if it's a symlink, then we ran into a possible symlink loop. */
  902. is_sym = ( ((ZIPinfo *)(h->opaque))->entries[pos].symlink != NULL );
  903. BAIL_IF_MACRO(is_sym, ERR_TOO_MANY_SYMLINKS, 0);
  904. dlen = strlen(name);
  905. /* !!! FIXME: yikes. Better way to check? */
  906. return((((ZIPinfo *)(h->opaque))->entries[pos].name[dlen] == '/'));
  907. } /* ZIP_isDirectory */
  908. static int ZIP_isSymLink(DirHandle *h, const char *name)
  909. {
  910. int pos = ZIP_exists_symcheck(h, name, 0);
  911. BAIL_IF_MACRO(pos == -1, ERR_NO_SUCH_FILE, 0);
  912. return( ((ZIPinfo *)(h->opaque))->entries[pos].symlink != NULL );
  913. } /* ZIP_isSymLink */
  914. static FileHandle *ZIP_openRead(DirHandle *h, const char *filename)
  915. {
  916. FileHandle *retval = NULL;
  917. ZIPinfo *zi = ((ZIPinfo *) (h->opaque));
  918. ZIPfileinfo *finfo = NULL;
  919. int pos = ZIP_exists_symcheck(h, filename, SYMLINK_RECURSE_COUNT);
  920. void *in;
  921. BAIL_IF_MACRO(pos == -1, ERR_NO_SUCH_FILE, NULL);
  922. /* if it's a symlink, then we ran into a possible symlink loop. */
  923. BAIL_IF_MACRO(zi->entries[pos].symlink != NULL, ERR_TOO_MANY_SYMLINKS, 0);
  924. in = __PHYSFS_platformOpenRead(zi->archiveName);
  925. BAIL_IF_MACRO(in == NULL, ERR_IO_ERROR, NULL);
  926. if (!zip_set_open_position(in, &zi->entries[pos]))
  927. {
  928. __PHYSFS_platformClose(in);
  929. return(0);
  930. } /* if */
  931. if ( ((retval = (FileHandle *) malloc(sizeof (FileHandle))) == NULL) ||
  932. ((finfo = (ZIPfileinfo *) malloc(sizeof (ZIPfileinfo))) == NULL) )
  933. {
  934. if (retval)
  935. free(retval);
  936. __PHYSFS_platformClose(in);
  937. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  938. } /* if */
  939. retval->opaque = (void *) finfo;
  940. retval->funcs = &__PHYSFS_FileFunctions_ZIP;
  941. retval->dirHandle = h;
  942. memset(finfo, '\0', sizeof (ZIPfileinfo));
  943. finfo->handle = in;
  944. finfo->entry = &zi->entries[pos];
  945. if (finfo->entry->compression_method != COMPMETH_NONE)
  946. {
  947. if (zlib_err(inflateInit2(&finfo->stream, -MAX_WBITS)) != Z_OK)
  948. {
  949. ZIP_fileClose(retval);
  950. return(NULL);
  951. } /* if */
  952. finfo->buffer = (PHYSFS_uint8 *) malloc(ZIP_READBUFSIZE);
  953. if (finfo->buffer == NULL)
  954. {
  955. ZIP_fileClose(retval);
  956. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  957. } /* if */
  958. } /* if */
  959. return(retval);
  960. } /* ZIP_openRead */
  961. static void ZIP_dirClose(DirHandle *h)
  962. {
  963. ZIPinfo *zi = (ZIPinfo *) (h->opaque);
  964. freeEntries(zi, zi->entryCount);
  965. free(zi->archiveName);
  966. free(zi);
  967. free(h);
  968. } /* ZIP_dirClose */
  969. #endif /* defined PHYSFS_SUPPORTS_ZIP */
  970. /* end of zip.c ... */