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