zip.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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.
  7. */
  8. #if HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. #if (defined PHYSFS_SUPPORTS_ZIP)
  12. /*
  13. * !!! FIXME: overall design bugs.
  14. *
  15. * Maybe add a seekToStartOfCurrentFile() in unzip.c if complete seek
  16. * semantics are impossible.
  17. *
  18. * Could be more i/o efficient if we combined unzip.c and this file.
  19. * (and thus lose all the unzGoToNextFile() dummy loops.
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <assert.h>
  25. #include "physfs.h"
  26. #include "unzip.h"
  27. #define __PHYSICSFS_INTERNAL__
  28. #include "physfs_internal.h"
  29. #define MAXZIPENTRYSIZE 256
  30. typedef struct
  31. {
  32. char *name;
  33. unz_file_info info;
  34. char *symlink;
  35. } ZIPentry;
  36. typedef struct
  37. {
  38. char *archiveName;
  39. unz_global_info global;
  40. ZIPentry *entries;
  41. } ZIPinfo;
  42. typedef struct
  43. {
  44. unzFile handle;
  45. } ZIPfileinfo;
  46. /* Number of symlinks to follow before we assume it's a recursive link... */
  47. #define SYMLINK_RECURSE_COUNT 20
  48. static PHYSFS_sint64 ZIP_read(FileHandle *handle, void *buffer,
  49. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  50. static int ZIP_eof(FileHandle *handle);
  51. static PHYSFS_sint64 ZIP_tell(FileHandle *handle);
  52. static int ZIP_seek(FileHandle *handle, PHYSFS_uint64 offset);
  53. static PHYSFS_sint64 ZIP_fileLength(FileHandle *handle);
  54. static int ZIP_fileClose(FileHandle *handle);
  55. static int ZIP_isArchive(const char *filename, int forWriting);
  56. static char *ZIP_realpath(unzFile fh, unz_file_info *info, ZIPentry *entry);
  57. static DirHandle *ZIP_openArchive(const char *name, int forWriting);
  58. static LinkedStringList *ZIP_enumerateFiles(DirHandle *h,
  59. const char *dirname,
  60. int omitSymLinks);
  61. static int ZIP_exists(DirHandle *h, const char *name);
  62. static int ZIP_isDirectory(DirHandle *h, const char *name);
  63. static int ZIP_isSymLink(DirHandle *h, const char *name);
  64. static FileHandle *ZIP_openRead(DirHandle *h, const char *filename);
  65. static void ZIP_dirClose(DirHandle *h);
  66. static const FileFunctions __PHYSFS_FileFunctions_ZIP =
  67. {
  68. ZIP_read, /* read() method */
  69. NULL, /* write() method */
  70. ZIP_eof, /* eof() method */
  71. ZIP_tell, /* tell() method */
  72. ZIP_seek, /* seek() method */
  73. ZIP_fileLength, /* fileLength() method */
  74. ZIP_fileClose /* fileClose() method */
  75. };
  76. const DirFunctions __PHYSFS_DirFunctions_ZIP =
  77. {
  78. ZIP_isArchive, /* isArchive() method */
  79. ZIP_openArchive, /* openArchive() method */
  80. ZIP_enumerateFiles, /* enumerateFiles() method */
  81. ZIP_exists, /* exists() method */
  82. ZIP_isDirectory, /* isDirectory() method */
  83. ZIP_isSymLink, /* isSymLink() method */
  84. ZIP_openRead, /* openRead() method */
  85. NULL, /* openWrite() method */
  86. NULL, /* openAppend() method */
  87. NULL, /* remove() method */
  88. NULL, /* mkdir() method */
  89. ZIP_dirClose /* dirClose() method */
  90. };
  91. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP =
  92. {
  93. "ZIP",
  94. "PkZip/WinZip/Info-Zip compatible",
  95. "Ryan C. Gordon <icculus@clutteredmind.org>",
  96. "http://www.icculus.org/physfs/",
  97. };
  98. static PHYSFS_sint64 ZIP_read(FileHandle *handle, void *buffer,
  99. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  100. {
  101. unzFile fh = ((ZIPfileinfo *) (handle->opaque))->handle;
  102. int bytes = (int) (objSize * objCount); /* !!! FIXME: overflow? */
  103. PHYSFS_sint32 rc = unzReadCurrentFile(fh, buffer, bytes);
  104. if (rc < bytes)
  105. __PHYSFS_setError(ERR_PAST_EOF);
  106. else if (rc == UNZ_ERRNO)
  107. __PHYSFS_setError(ERR_IO_ERROR);
  108. else if (rc < 0)
  109. __PHYSFS_setError(ERR_COMPRESSION);
  110. return(rc / objSize);
  111. } /* ZIP_read */
  112. static int ZIP_eof(FileHandle *handle)
  113. {
  114. return(unzeof(((ZIPfileinfo *) (handle->opaque))->handle));
  115. } /* ZIP_eof */
  116. static PHYSFS_sint64 ZIP_tell(FileHandle *handle)
  117. {
  118. return(unztell(((ZIPfileinfo *) (handle->opaque))->handle));
  119. } /* ZIP_tell */
  120. static int ZIP_seek(FileHandle *handle, PHYSFS_uint64 offset)
  121. {
  122. /* !!! FIXME : this blows. */
  123. unzFile fh = ((ZIPfileinfo *) (handle->opaque))->handle;
  124. char *buf = NULL;
  125. PHYSFS_uint32 bufsize = 4096 * 2;
  126. BAIL_IF_MACRO(unztell(fh) == offset, NULL, 1);
  127. BAIL_IF_MACRO(ZIP_fileLength(handle) <= (PHYSFS_sint64) offset, ERR_PAST_EOF, 0);
  128. /* reset to the start of the zipfile. */
  129. unzCloseCurrentFile(fh);
  130. BAIL_IF_MACRO(unzOpenCurrentFile(fh) != UNZ_OK, ERR_IO_ERROR, 0);
  131. while ((buf == NULL) && (bufsize >= 512))
  132. {
  133. bufsize >>= 1; /* divides by two. */
  134. buf = (char *) malloc(bufsize);
  135. } /* while */
  136. BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, 0);
  137. while (offset > 0)
  138. {
  139. PHYSFS_uint32 chunk = (offset > bufsize) ? bufsize : (PHYSFS_uint32)offset;
  140. PHYSFS_sint32 rc = unzReadCurrentFile(fh, buf, chunk);
  141. BAIL_IF_MACRO(rc == 0, ERR_IO_ERROR, 0); /* shouldn't happen. */
  142. BAIL_IF_MACRO(rc == UNZ_ERRNO, ERR_IO_ERROR, 0);
  143. BAIL_IF_MACRO(rc < 0, ERR_COMPRESSION, 0);
  144. offset -= rc;
  145. } /* while */
  146. free(buf);
  147. return(offset == 0);
  148. } /* ZIP_seek */
  149. static PHYSFS_sint64 ZIP_fileLength(FileHandle *handle)
  150. {
  151. ZIPfileinfo *finfo = (ZIPfileinfo *) (handle->opaque);
  152. unz_file_info info;
  153. unzGetCurrentFileInfo(finfo->handle, &info, NULL, 0, NULL, 0, NULL, 0);
  154. return(info.uncompressed_size);
  155. } /* ZIP_fileLength */
  156. static int ZIP_fileClose(FileHandle *handle)
  157. {
  158. ZIPfileinfo *finfo = (ZIPfileinfo *) (handle->opaque);
  159. unzClose(finfo->handle);
  160. free(finfo);
  161. free(handle);
  162. return(1);
  163. } /* ZIP_fileClose */
  164. static int ZIP_isArchive(const char *filename, int forWriting)
  165. {
  166. int retval = 0;
  167. unzFile unz = unzOpen(filename);
  168. unz_global_info global;
  169. if (unz != NULL)
  170. {
  171. if (unzGetGlobalInfo(unz, &global) == UNZ_OK)
  172. retval = 1;
  173. unzClose(unz);
  174. } /* if */
  175. return(retval);
  176. } /* ZIP_isArchive */
  177. static void freeEntries(ZIPinfo *info, int count, const char *errmsg)
  178. {
  179. int i;
  180. for (i = 0; i < count; i++)
  181. {
  182. free(info->entries[i].name);
  183. if (info->entries[i].symlink != NULL)
  184. free(info->entries[i].symlink);
  185. } /* for */
  186. free(info->entries);
  187. if (errmsg != NULL)
  188. __PHYSFS_setError(errmsg);
  189. } /* freeEntries */
  190. /*
  191. * !!! FIXME: Really implement this.
  192. * !!! FIXME: symlinks in zipfiles can be relative paths, including
  193. * !!! FIXME: "." and ".." entries. These need to be parsed out.
  194. * !!! FIXME: For now, though, we're just copying the relative path. Oh well.
  195. */
  196. static char *expand_symlink_path(const char *path, ZIPentry *entry)
  197. {
  198. char *retval = (char *) malloc(strlen(path) + 1);
  199. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  200. strcpy(retval, path);
  201. return(retval);
  202. } /* expand_symlink_path */
  203. static char *ZIP_realpath(unzFile fh, unz_file_info *info, ZIPentry *entry)
  204. {
  205. char path[MAXZIPENTRYSIZE];
  206. int size = info->uncompressed_size;
  207. int rc;
  208. BAIL_IF_MACRO(size >= sizeof (path), ERR_IO_ERROR, NULL);
  209. BAIL_IF_MACRO(unzOpenCurrentFile(fh) != UNZ_OK, ERR_IO_ERROR, NULL);
  210. rc = unzReadCurrentFile(fh, path, size);
  211. unzCloseCurrentFile(fh);
  212. BAIL_IF_MACRO(rc != size, ERR_IO_ERROR, NULL);
  213. path[size] = '\0'; /* null terminate it. */
  214. return(expand_symlink_path(path, entry)); /* retval is malloc()'d. */
  215. } /* ZIP_realpath */
  216. static int version_does_symlinks(uLong version)
  217. {
  218. int retval = 0;
  219. PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((version >> 8) & 0xFF);
  220. /*
  221. * These are the platforms that can build an archive with symlinks,
  222. * according to the Info-ZIP project.
  223. */
  224. switch (hosttype)
  225. {
  226. case 3: /* Unix */
  227. case 16: /* BeOS */
  228. case 5: /* Atari */
  229. retval = 1;
  230. break;
  231. } /* switch */
  232. return(retval);
  233. } /* version_does_symlinks */
  234. static int entry_is_symlink(unz_file_info *info)
  235. {
  236. return (
  237. (version_does_symlinks(info->version)) &&
  238. (info->uncompressed_size > 0) &&
  239. (info->external_fa & 0x0120000) /* symlink flag. */
  240. );
  241. } /* entry_is_symlink */
  242. static int loadZipEntries(ZIPinfo *info, unzFile unz)
  243. {
  244. int i, max;
  245. BAIL_IF_MACRO(unzGetGlobalInfo(unz, &(info->global)) != UNZ_OK,
  246. ERR_IO_ERROR, 0);
  247. BAIL_IF_MACRO(unzGoToFirstFile(unz) != UNZ_OK, ERR_IO_ERROR, 0);
  248. max = info->global.number_entry;
  249. info->entries = (ZIPentry *) malloc(sizeof (ZIPentry) * max);
  250. BAIL_IF_MACRO(info->entries == NULL, ERR_OUT_OF_MEMORY, 0);
  251. for (i = 0; i < max; i++)
  252. {
  253. unz_file_info *d = &((info->entries[i]).info);
  254. if (unzGetCurrentFileInfo(unz, d, NULL, 0, NULL, 0, NULL, 0) != UNZ_OK)
  255. {
  256. freeEntries(info, i, ERR_IO_ERROR);
  257. return(0);
  258. } /* if */
  259. (info->entries[i]).name = (char *) malloc(d->size_filename + 1);
  260. if ((info->entries[i]).name == NULL)
  261. {
  262. freeEntries(info, i, ERR_OUT_OF_MEMORY);
  263. return(0);
  264. } /* if */
  265. info->entries[i].symlink = NULL;
  266. if (unzGetCurrentFileInfo(unz, NULL, (info->entries[i]).name,
  267. d->size_filename + 1, NULL, 0,
  268. NULL, 0) != UNZ_OK)
  269. {
  270. freeEntries(info, i + 1, ERR_IO_ERROR);
  271. return(0);
  272. } /* if */
  273. if (entry_is_symlink(d))
  274. {
  275. info->entries[i].symlink = ZIP_realpath(unz, d, &info->entries[i]);
  276. if (info->entries[i].symlink == NULL)
  277. {
  278. freeEntries(info, i + 1, NULL);
  279. return(0);
  280. } /* if */
  281. } /* if */
  282. if ((unzGoToNextFile(unz) != UNZ_OK) && (i + 1 < max))
  283. {
  284. freeEntries(info, i + 1, ERR_IO_ERROR);
  285. return(0);
  286. } /* if */
  287. } /* for */
  288. return(1);
  289. } /* loadZipEntries */
  290. static DirHandle *ZIP_openArchive(const char *name, int forWriting)
  291. {
  292. unzFile unz = NULL;
  293. DirHandle *retval = NULL;
  294. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, NULL);
  295. retval = malloc(sizeof (DirHandle));
  296. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  297. unz = unzOpen(name);
  298. if (unz == NULL)
  299. {
  300. free(retval);
  301. BAIL_IF_MACRO(1, ERR_UNSUPPORTED_ARCHIVE, NULL);
  302. } /* if */
  303. retval->opaque = malloc(sizeof (ZIPinfo));
  304. if (retval->opaque == NULL)
  305. {
  306. free(retval);
  307. unzClose(unz);
  308. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  309. } /* if */
  310. ((ZIPinfo *) (retval->opaque))->archiveName = malloc(strlen(name) + 1);
  311. if ( (((ZIPinfo *) (retval->opaque))->archiveName == NULL) ||
  312. (!loadZipEntries( (ZIPinfo *) (retval->opaque), unz)) )
  313. {
  314. if (((ZIPinfo *) (retval->opaque))->archiveName != NULL)
  315. free(((ZIPinfo *) (retval->opaque))->archiveName);
  316. free(retval->opaque);
  317. free(retval);
  318. unzClose(unz);
  319. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  320. } /* if */
  321. unzClose(unz);
  322. strcpy(((ZIPinfo *) (retval->opaque))->archiveName, name);
  323. retval->funcs = &__PHYSFS_DirFunctions_ZIP;
  324. return(retval);
  325. } /* ZIP_openArchive */
  326. /* !!! This is seriously ugly. */
  327. static LinkedStringList *ZIP_enumerateFiles(DirHandle *h,
  328. const char *dirname,
  329. int omitSymLinks)
  330. {
  331. ZIPinfo *zi = (ZIPinfo *) (h->opaque);
  332. unsigned int i;
  333. int dlen;
  334. LinkedStringList *retval = NULL;
  335. LinkedStringList *l = NULL;
  336. LinkedStringList *prev = NULL;
  337. char *d;
  338. ZIPentry *entry;
  339. char buf[MAXZIPENTRYSIZE];
  340. dlen = strlen(dirname);
  341. d = malloc(dlen + 1);
  342. BAIL_IF_MACRO(d == NULL, ERR_OUT_OF_MEMORY, NULL);
  343. strcpy(d, dirname);
  344. if ((dlen > 0) && (d[dlen - 1] == '/')) /* no trailing slash. */
  345. {
  346. dlen--;
  347. d[dlen] = '\0';
  348. } /* if */
  349. for (i = 0, entry = zi->entries; i < zi->global.number_entry; i++, entry++)
  350. {
  351. char *ptr;
  352. char *add_file;
  353. int this_dlen;
  354. if ((omitSymLinks) && (entry->symlink != NULL))
  355. continue;
  356. this_dlen = strlen(entry->name);
  357. if (this_dlen + 1 > MAXZIPENTRYSIZE)
  358. continue; /* ugh. */
  359. strcpy(buf, entry->name);
  360. if ((this_dlen > 0) && (buf[this_dlen - 1] == '/')) /* no trailing slash. */
  361. {
  362. this_dlen--;
  363. buf[this_dlen] = '\0';
  364. } /* if */
  365. if (this_dlen <= dlen) /* not in this dir. */
  366. continue;
  367. if (*d == '\0')
  368. add_file = buf;
  369. else
  370. {
  371. if (buf[dlen] != '/') /* can't be in same directory? */
  372. continue;
  373. buf[dlen] = '\0';
  374. if (__PHYSFS_platformStricmp(d, buf) != 0) /* not same directory? */
  375. continue;
  376. add_file = buf + dlen + 1;
  377. } /* else */
  378. /* handle subdirectories... */
  379. ptr = strchr(add_file, '/');
  380. if (ptr != NULL)
  381. {
  382. LinkedStringList *j;
  383. *ptr = '\0';
  384. for (j = retval; j != NULL; j = j->next)
  385. {
  386. if (__PHYSFS_platformStricmp(j->str, ptr) == 0)
  387. break;
  388. } /* for */
  389. if (j != NULL)
  390. continue;
  391. } /* if */
  392. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  393. if (l == NULL)
  394. break;
  395. l->str = (char *) malloc(strlen(add_file) + 1);
  396. if (l->str == NULL)
  397. {
  398. free(l);
  399. break;
  400. } /* if */
  401. strcpy(l->str, add_file);
  402. if (retval == NULL)
  403. retval = l;
  404. else
  405. prev->next = l;
  406. prev = l;
  407. l->next = NULL;
  408. } /* for */
  409. free(d);
  410. return(retval);
  411. } /* ZIP_enumerateFiles */
  412. /* !!! This is seriously ugly. */
  413. static int ZIP_exists_symcheck(DirHandle *h, const char *name, int follow)
  414. {
  415. char buf[MAXZIPENTRYSIZE];
  416. ZIPinfo *zi = (ZIPinfo *) (h->opaque);
  417. int dlen;
  418. char *d;
  419. unsigned int i;
  420. ZIPentry *entry;
  421. dlen = strlen(name);
  422. d = malloc(dlen + 1);
  423. BAIL_IF_MACRO(d == NULL, ERR_OUT_OF_MEMORY, -1);
  424. strcpy(d, name);
  425. if ((dlen > 0) && (d[dlen - 1] == '/')) /* no trailing slash. */
  426. {
  427. dlen--;
  428. d[dlen] = '\0';
  429. } /* if */
  430. for (i = 0, entry = zi->entries; i < zi->global.number_entry; i++, entry++)
  431. {
  432. int this_dlen = strlen(entry->name);
  433. if (this_dlen + 1 > MAXZIPENTRYSIZE)
  434. continue; /* ugh. */
  435. strcpy(buf, entry->name);
  436. if ((this_dlen > 0) && (buf[this_dlen - 1] == '/')) /* no trailing slash. */
  437. {
  438. this_dlen--;
  439. buf[this_dlen] = '\0';
  440. } /* if */
  441. if ( ((buf[dlen] == '/') || (buf[dlen] == '\0')) &&
  442. (strncmp(d, buf, dlen) == 0) )
  443. {
  444. int retval = i;
  445. free(d);
  446. if (follow) /* follow symlinks? */
  447. {
  448. if (entry->symlink != NULL)
  449. retval = ZIP_exists_symcheck(h, entry->symlink, follow-1);
  450. } /* if */
  451. return(retval);
  452. } /* if */
  453. } /* for */
  454. free(d);
  455. return(-1);
  456. } /* ZIP_exists_symcheck */
  457. static int ZIP_exists(DirHandle *h, const char *name)
  458. {
  459. int retval = ZIP_exists_symcheck(h, name, SYMLINK_RECURSE_COUNT);
  460. int is_sym;
  461. if (retval == -1)
  462. return(0);
  463. /* if it's a symlink, then we ran into a possible symlink loop. */
  464. is_sym = ( ((ZIPinfo *)(h->opaque))->entries[retval].symlink != NULL );
  465. BAIL_IF_MACRO(is_sym, ERR_TOO_MANY_SYMLINKS, 0);
  466. return(1);
  467. } /* ZIP_exists */
  468. static int ZIP_isDirectory(DirHandle *h, const char *name)
  469. {
  470. int dlen;
  471. int is_sym;
  472. int retval = ZIP_exists_symcheck(h, name, SYMLINK_RECURSE_COUNT);
  473. if (retval == -1)
  474. return(0);
  475. /* if it's a symlink, then we ran into a possible symlink loop. */
  476. is_sym = ( ((ZIPinfo *)(h->opaque))->entries[retval].symlink != NULL );
  477. BAIL_IF_MACRO(is_sym, ERR_TOO_MANY_SYMLINKS, 0);
  478. dlen = strlen(name);
  479. /* !!! yikes. Better way to check? */
  480. retval = (((ZIPinfo *)(h->opaque))->entries[retval].name[dlen] == '/');
  481. return(retval);
  482. } /* ZIP_isDirectory */
  483. static int ZIP_isSymLink(DirHandle *h, const char *name)
  484. {
  485. int retval = ZIP_exists_symcheck(h, name, 0);
  486. if (retval == -1)
  487. return(0);
  488. retval = ( ((ZIPinfo *)(h->opaque))->entries[retval].symlink != NULL );
  489. return(retval);
  490. } /* ZIP_isSymLink */
  491. static FileHandle *ZIP_openRead(DirHandle *h, const char *filename)
  492. {
  493. FileHandle *retval = NULL;
  494. ZIPinfo *zi = ((ZIPinfo *) (h->opaque));
  495. ZIPfileinfo *finfo = NULL;
  496. int pos = ZIP_exists_symcheck(h, filename, SYMLINK_RECURSE_COUNT);
  497. unzFile f;
  498. BAIL_IF_MACRO(pos == -1, ERR_NO_SUCH_FILE, NULL);
  499. f = unzOpen(zi->archiveName);
  500. BAIL_IF_MACRO(f == NULL, ERR_IO_ERROR, NULL);
  501. if (unzGoToFirstFile(f) != UNZ_OK)
  502. {
  503. unzClose(f);
  504. BAIL_IF_MACRO(1, ERR_IO_ERROR, NULL);
  505. } /* if */
  506. for (; pos > 0; pos--)
  507. {
  508. if (unzGoToNextFile(f) != UNZ_OK)
  509. {
  510. unzClose(f);
  511. BAIL_IF_MACRO(1, ERR_IO_ERROR, NULL);
  512. } /* if */
  513. } /* for */
  514. if ( (unzOpenCurrentFile(f) != UNZ_OK) ||
  515. ( (finfo = (ZIPfileinfo *) malloc(sizeof (ZIPfileinfo))) == NULL ) )
  516. {
  517. unzClose(f);
  518. BAIL_IF_MACRO(1, ERR_IO_ERROR, NULL);
  519. } /* if */
  520. if ( (!(retval = (FileHandle *) malloc(sizeof (FileHandle)))) ||
  521. (!(retval->opaque = (ZIPfileinfo *) malloc(sizeof (ZIPfileinfo)))) )
  522. {
  523. if (retval)
  524. free(retval);
  525. unzClose(f);
  526. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  527. } /* if */
  528. finfo->handle = f;
  529. retval->opaque = (void *) finfo;
  530. retval->funcs = &__PHYSFS_FileFunctions_ZIP;
  531. retval->dirHandle = h;
  532. return(retval);
  533. } /* ZIP_openRead */
  534. static void ZIP_dirClose(DirHandle *h)
  535. {
  536. ZIPinfo *zi = (ZIPinfo *) (h->opaque);
  537. freeEntries(zi, zi->global.number_entry, NULL);
  538. free(zi->archiveName);
  539. free(zi);
  540. free(h);
  541. } /* ZIP_dirClose */
  542. #endif /* defined PHYSFS_SUPPORTS_ZIP */
  543. /* end of zip.c ... */