2001-07-07 10:24:47 +02:00
|
|
|
/*
|
|
|
|
* ZIP support routines for PhysicsFS.
|
|
|
|
*
|
2007-03-11 11:10:28 +01:00
|
|
|
* Please see the file LICENSE.txt in the source's root directory.
|
2001-07-07 10:24:47 +02:00
|
|
|
*
|
2002-07-13 12:17:13 +02:00
|
|
|
* This file written by Ryan C. Gordon, with some peeking at "unzip.c"
|
|
|
|
* by Gilles Vollant.
|
2001-07-07 10:24:47 +02:00
|
|
|
*/
|
|
|
|
|
2002-05-10 11:25:25 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_ZIP)
|
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
#include <errno.h>
|
2002-11-22 07:24:10 +01:00
|
|
|
#include <time.h>
|
2010-09-07 01:35:01 +02:00
|
|
|
|
2001-07-07 10:24:47 +02:00
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_internal.h"
|
|
|
|
|
2012-03-10 08:31:58 +01:00
|
|
|
#define USE_MINIZ 1
|
|
|
|
#if USE_MINIZ
|
|
|
|
#include "physfs_miniz.h"
|
|
|
|
#else
|
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
/*
|
2005-03-14 12:49:30 +01:00
|
|
|
* A buffer of ZIP_READBUFSIZE is allocated for each compressed file opened,
|
|
|
|
* and is freed when you close the file; compressed data is read into
|
2002-07-13 12:17:13 +02:00
|
|
|
* this buffer, and then is decompressed into the buffer passed to
|
|
|
|
* PHYSFS_read().
|
|
|
|
*
|
|
|
|
* Uncompressed entries in a zipfile do not allocate this buffer; they just
|
|
|
|
* read data directly into the buffer passed to PHYSFS_read().
|
|
|
|
*
|
|
|
|
* Depending on your speed and memory requirements, you should tweak this
|
|
|
|
* value.
|
|
|
|
*/
|
|
|
|
#define ZIP_READBUFSIZE (16 * 1024)
|
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Entries are "unresolved" until they are first opened. At that time,
|
|
|
|
* local file headers parsed/validated, data offsets will be updated to look
|
|
|
|
* at the actual file data instead of the header, and symlinks will be
|
|
|
|
* followed and optimized. This means that we don't seek and read around the
|
|
|
|
* archive until forced to do so, and after the first time, we had to do
|
|
|
|
* less reading and parsing, which is very CD-ROM friendly.
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
ZIP_UNRESOLVED_FILE,
|
|
|
|
ZIP_UNRESOLVED_SYMLINK,
|
|
|
|
ZIP_RESOLVING,
|
|
|
|
ZIP_RESOLVED,
|
|
|
|
ZIP_BROKEN_FILE,
|
2003-01-31 05:07:48 +01:00
|
|
|
ZIP_BROKEN_SYMLINK
|
2002-07-23 09:49:13 +02:00
|
|
|
} ZipResolveType;
|
|
|
|
|
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
/*
|
|
|
|
* One ZIPentry is kept for each file in an open ZIP archive.
|
|
|
|
*/
|
2002-07-23 09:49:13 +02:00
|
|
|
typedef struct _ZIPentry
|
2001-07-15 11:29:30 +02:00
|
|
|
{
|
2002-07-23 09:49:13 +02:00
|
|
|
char *name; /* Name of file in archive */
|
|
|
|
struct _ZIPentry *symlink; /* NULL or file we symlink to */
|
|
|
|
ZipResolveType resolved; /* Have we resolved file/symlink? */
|
|
|
|
PHYSFS_uint32 offset; /* offset of data in archive */
|
|
|
|
PHYSFS_uint16 version; /* version made by */
|
|
|
|
PHYSFS_uint16 version_needed; /* version needed to extract */
|
|
|
|
PHYSFS_uint16 compression_method; /* compression method */
|
|
|
|
PHYSFS_uint32 crc; /* crc-32 */
|
|
|
|
PHYSFS_uint32 compressed_size; /* compressed size */
|
|
|
|
PHYSFS_uint32 uncompressed_size; /* uncompressed size */
|
|
|
|
PHYSFS_sint64 last_mod_time; /* last file mod time */
|
2001-07-28 14:14:09 +02:00
|
|
|
} ZIPentry;
|
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
/*
|
|
|
|
* One ZIPinfo is kept for each open ZIP archive.
|
|
|
|
*/
|
2001-07-28 14:14:09 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io;
|
2002-07-13 12:17:13 +02:00
|
|
|
PHYSFS_uint16 entryCount; /* Number of files in ZIP. */
|
|
|
|
ZIPentry *entries; /* info on all files in ZIP. */
|
2001-07-15 11:29:30 +02:00
|
|
|
} ZIPinfo;
|
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
/*
|
|
|
|
* One ZIPfileinfo is kept for each open file in a ZIP archive.
|
|
|
|
*/
|
2001-07-15 11:29:30 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2002-07-13 12:17:13 +02:00
|
|
|
ZIPentry *entry; /* Info on file. */
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io; /* physical file handle. */
|
2002-07-13 12:17:13 +02:00
|
|
|
PHYSFS_uint32 compressed_position; /* offset in compressed data. */
|
|
|
|
PHYSFS_uint32 uncompressed_position; /* tell() position. */
|
|
|
|
PHYSFS_uint8 *buffer; /* decompression buffer. */
|
|
|
|
z_stream stream; /* zlib stream state. */
|
2001-07-15 11:29:30 +02:00
|
|
|
} ZIPfileinfo;
|
|
|
|
|
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
/* Magic numbers... */
|
|
|
|
#define ZIP_LOCAL_FILE_SIG 0x04034b50
|
|
|
|
#define ZIP_CENTRAL_DIR_SIG 0x02014b50
|
|
|
|
#define ZIP_END_OF_CENTRAL_DIR_SIG 0x06054b50
|
|
|
|
|
|
|
|
/* compression methods... */
|
|
|
|
#define COMPMETH_NONE 0
|
|
|
|
/* ...and others... */
|
|
|
|
|
|
|
|
|
2002-07-15 15:20:34 +02:00
|
|
|
#define UNIX_FILETYPE_MASK 0170000
|
|
|
|
#define UNIX_FILETYPE_SYMLINK 0120000
|
|
|
|
|
|
|
|
|
2004-09-23 08:45:36 +02:00
|
|
|
/*
|
|
|
|
* Bridge physfs allocation functions to zlib's format...
|
|
|
|
*/
|
|
|
|
static voidpf zlibPhysfsAlloc(voidpf opaque, uInt items, uInt size)
|
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return ((PHYSFS_Allocator *) opaque)->Malloc(items * size);
|
2004-09-23 08:45:36 +02:00
|
|
|
} /* zlibPhysfsAlloc */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bridge physfs allocation functions to zlib's format...
|
|
|
|
*/
|
|
|
|
static void zlibPhysfsFree(voidpf opaque, voidpf address)
|
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
((PHYSFS_Allocator *) opaque)->Free(address);
|
2004-09-23 08:45:36 +02:00
|
|
|
} /* zlibPhysfsFree */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct a new z_stream to a sane state.
|
|
|
|
*/
|
|
|
|
static void initializeZStream(z_stream *pstr)
|
|
|
|
{
|
|
|
|
memset(pstr, '\0', sizeof (z_stream));
|
|
|
|
pstr->zalloc = zlibPhysfsAlloc;
|
|
|
|
pstr->zfree = zlibPhysfsFree;
|
2005-03-14 12:49:30 +01:00
|
|
|
pstr->opaque = &allocator;
|
2004-09-23 08:45:36 +02:00
|
|
|
} /* initializeZStream */
|
|
|
|
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
static PHYSFS_ErrorCode zlib_error_code(int rc)
|
2002-07-13 12:17:13 +02:00
|
|
|
{
|
|
|
|
switch (rc)
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
case Z_OK: return PHYSFS_ERR_OK; /* not an error. */
|
|
|
|
case Z_STREAM_END: return PHYSFS_ERR_OK; /* not an error. */
|
|
|
|
case Z_ERRNO: return PHYSFS_ERR_IO;
|
|
|
|
case Z_MEM_ERROR: return PHYSFS_ERR_OUT_OF_MEMORY;
|
|
|
|
default: return PHYSFS_ERR_CORRUPT;
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* switch */
|
2002-07-28 23:03:27 +02:00
|
|
|
} /* zlib_error_string */
|
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2002-07-28 23:03:27 +02:00
|
|
|
/*
|
|
|
|
* Wrap all zlib calls in this, so the physfs error state is set appropriately.
|
|
|
|
*/
|
2012-03-20 20:38:12 +01:00
|
|
|
static int zlib_err(const int rc)
|
2002-07-28 23:03:27 +02:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
__PHYSFS_setError(zlib_error_code(rc));
|
2010-01-28 08:27:45 +01:00
|
|
|
return rc;
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* zlib_err */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read an unsigned 32-bit int and swap to native byte order.
|
|
|
|
*/
|
2010-08-30 09:01:57 +02:00
|
|
|
static int readui32(PHYSFS_Io *io, PHYSFS_uint32 *val)
|
2002-07-13 12:17:13 +02:00
|
|
|
{
|
|
|
|
PHYSFS_uint32 v;
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &v, sizeof (v)), ERRPASS, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
*val = PHYSFS_swapULE32(v);
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* readui32 */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read an unsigned 16-bit int and swap to native byte order.
|
|
|
|
*/
|
2010-08-30 09:01:57 +02:00
|
|
|
static int readui16(PHYSFS_Io *io, PHYSFS_uint16 *val)
|
2002-07-13 12:17:13 +02:00
|
|
|
{
|
|
|
|
PHYSFS_uint16 v;
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &v, sizeof (v)), ERRPASS, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
*val = PHYSFS_swapULE16(v);
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* readui16 */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_sint64 ZIP_read(PHYSFS_Io *_io, void *buf, PHYSFS_uint64 len)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
ZIPfileinfo *finfo = (ZIPfileinfo *) _io->opaque;
|
|
|
|
PHYSFS_Io *io = finfo->io;
|
2002-07-13 12:17:13 +02:00
|
|
|
ZIPentry *entry = finfo->entry;
|
|
|
|
PHYSFS_sint64 retval = 0;
|
2010-08-21 08:47:58 +02:00
|
|
|
PHYSFS_sint64 maxread = (PHYSFS_sint64) len;
|
2002-07-13 12:17:13 +02:00
|
|
|
PHYSFS_sint64 avail = entry->uncompressed_size -
|
|
|
|
finfo->uncompressed_position;
|
|
|
|
|
|
|
|
if (avail < maxread)
|
2010-08-21 08:47:58 +02:00
|
|
|
maxread = avail;
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(maxread == 0, ERRPASS, 0); /* quick rejection. */
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-08-21 08:47:58 +02:00
|
|
|
if (entry->compression_method == COMPMETH_NONE)
|
2010-08-30 09:01:57 +02:00
|
|
|
retval = io->read(io, buf, maxread);
|
2002-07-13 12:17:13 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
finfo->stream.next_out = buf;
|
2012-03-10 09:18:33 +01:00
|
|
|
finfo->stream.avail_out = (uInt) maxread;
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
while (retval < maxread)
|
|
|
|
{
|
|
|
|
PHYSFS_uint32 before = finfo->stream.total_out;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (finfo->stream.avail_in == 0)
|
|
|
|
{
|
|
|
|
PHYSFS_sint64 br;
|
|
|
|
|
|
|
|
br = entry->compressed_size - finfo->compressed_position;
|
|
|
|
if (br > 0)
|
|
|
|
{
|
|
|
|
if (br > ZIP_READBUFSIZE)
|
|
|
|
br = ZIP_READBUFSIZE;
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
br = io->read(io, finfo->buffer, (PHYSFS_uint64) br);
|
2002-07-13 12:17:13 +02:00
|
|
|
if (br <= 0)
|
|
|
|
break;
|
|
|
|
|
2003-03-12 06:39:51 +01:00
|
|
|
finfo->compressed_position += (PHYSFS_uint32) br;
|
2002-07-13 12:17:13 +02:00
|
|
|
finfo->stream.next_in = finfo->buffer;
|
2003-03-12 06:39:51 +01:00
|
|
|
finfo->stream.avail_in = (PHYSFS_uint32) br;
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* if */
|
|
|
|
} /* if */
|
2001-07-23 09:16:22 +02:00
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
rc = zlib_err(inflate(&finfo->stream, Z_SYNC_FLUSH));
|
|
|
|
retval += (finfo->stream.total_out - before);
|
2001-07-23 09:16:22 +02:00
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
if (rc != Z_OK)
|
|
|
|
break;
|
|
|
|
} /* while */
|
|
|
|
} /* else */
|
|
|
|
|
|
|
|
if (retval > 0)
|
2010-08-21 08:47:58 +02:00
|
|
|
finfo->uncompressed_position += (PHYSFS_uint32) retval;
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* ZIP_read */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_sint64 ZIP_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
|
2002-08-21 04:59:15 +02:00
|
|
|
{
|
2012-03-22 05:05:10 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, -1);
|
2002-08-21 04:59:15 +02:00
|
|
|
} /* ZIP_write */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_sint64 ZIP_tell(PHYSFS_Io *io)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
return ((ZIPfileinfo *) io->opaque)->uncompressed_position;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* ZIP_tell */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static int ZIP_seek(PHYSFS_Io *_io, PHYSFS_uint64 offset)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
ZIPfileinfo *finfo = (ZIPfileinfo *) _io->opaque;
|
2002-07-13 12:17:13 +02:00
|
|
|
ZIPentry *entry = finfo->entry;
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io = finfo->io;
|
2001-07-23 09:16:22 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(offset > entry->uncompressed_size, PHYSFS_ERR_PAST_EOF, 0);
|
2001-07-23 09:16:22 +02:00
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
if (entry->compression_method == COMPMETH_NONE)
|
2001-07-23 09:16:22 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
const PHYSFS_sint64 newpos = offset + entry->offset;
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!io->seek(io, newpos), ERRPASS, 0);
|
2003-03-30 20:44:59 +02:00
|
|
|
finfo->uncompressed_position = (PHYSFS_uint32) offset;
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* if */
|
2001-07-23 09:16:22 +02:00
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
else
|
2001-07-23 09:16:22 +02:00
|
|
|
{
|
2002-07-13 12:17:13 +02:00
|
|
|
/*
|
|
|
|
* If seeking backwards, we need to redecode the file
|
|
|
|
* from the start and throw away the compressed bits until we hit
|
|
|
|
* the offset we need. If seeking forward, we still need to
|
2002-07-23 09:49:13 +02:00
|
|
|
* decode, but we don't rewind first.
|
2002-07-13 12:17:13 +02:00
|
|
|
*/
|
|
|
|
if (offset < finfo->uncompressed_position)
|
|
|
|
{
|
|
|
|
/* we do a copy so state is sane if inflateInit2() fails. */
|
|
|
|
z_stream str;
|
2004-09-23 08:45:36 +02:00
|
|
|
initializeZStream(&str);
|
2002-07-13 12:17:13 +02:00
|
|
|
if (zlib_err(inflateInit2(&str, -MAX_WBITS)) != Z_OK)
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
if (!io->seek(io, entry->offset))
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2002-07-17 18:05:39 +02:00
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
inflateEnd(&finfo->stream);
|
|
|
|
memcpy(&finfo->stream, &str, sizeof (z_stream));
|
|
|
|
finfo->uncompressed_position = finfo->compressed_position = 0;
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
while (finfo->uncompressed_position != offset)
|
|
|
|
{
|
|
|
|
PHYSFS_uint8 buf[512];
|
2003-03-12 06:39:51 +01:00
|
|
|
PHYSFS_uint32 maxread;
|
|
|
|
|
|
|
|
maxread = (PHYSFS_uint32) (offset - finfo->uncompressed_position);
|
2002-07-13 12:17:13 +02:00
|
|
|
if (maxread > sizeof (buf))
|
|
|
|
maxread = sizeof (buf);
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
if (ZIP_read(_io, buf, maxread) != maxread)
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* while */
|
|
|
|
} /* else */
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* ZIP_seek */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_sint64 ZIP_length(PHYSFS_Io *io)
|
2001-07-09 06:15:35 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
const ZIPfileinfo *finfo = (ZIPfileinfo *) io->opaque;
|
2010-01-28 08:27:45 +01:00
|
|
|
return finfo->entry->uncompressed_size;
|
2010-08-30 09:01:57 +02:00
|
|
|
} /* ZIP_length */
|
2001-07-09 06:15:35 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_Io *zip_get_io(PHYSFS_Io *io, ZIPinfo *inf, ZIPentry *entry);
|
|
|
|
|
|
|
|
static PHYSFS_Io *ZIP_duplicate(PHYSFS_Io *io)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
ZIPfileinfo *origfinfo = (ZIPfileinfo *) io->opaque;
|
|
|
|
PHYSFS_Io *retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
|
|
|
|
ZIPfileinfo *finfo = (ZIPfileinfo *) allocator.Malloc(sizeof (ZIPfileinfo));
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, ZIP_duplicate_failed);
|
|
|
|
GOTO_IF_MACRO(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, ZIP_duplicate_failed);
|
2010-08-30 09:01:57 +02:00
|
|
|
memset(finfo, '\0', sizeof (*finfo));
|
|
|
|
|
|
|
|
finfo->entry = origfinfo->entry;
|
|
|
|
finfo->io = zip_get_io(origfinfo->io, NULL, finfo->entry);
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!finfo->io, ERRPASS, ZIP_duplicate_failed);
|
2010-08-30 09:01:57 +02:00
|
|
|
|
|
|
|
if (finfo->entry->compression_method != COMPMETH_NONE)
|
|
|
|
{
|
|
|
|
finfo->buffer = (PHYSFS_uint8 *) allocator.Malloc(ZIP_READBUFSIZE);
|
2012-03-20 20:38:12 +01:00
|
|
|
if (!finfo->buffer)
|
|
|
|
GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, ZIP_duplicate_failed);
|
|
|
|
else if (zlib_err(inflateInit2(&finfo->stream, -MAX_WBITS)) != Z_OK)
|
2010-08-30 09:01:57 +02:00
|
|
|
goto ZIP_duplicate_failed;
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
memcpy(retval, io, sizeof (PHYSFS_Io));
|
|
|
|
retval->opaque = finfo;
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
ZIP_duplicate_failed:
|
|
|
|
if (finfo != NULL)
|
|
|
|
{
|
|
|
|
if (finfo->io != NULL)
|
|
|
|
finfo->io->destroy(finfo->io);
|
|
|
|
|
|
|
|
if (finfo->buffer != NULL)
|
|
|
|
{
|
|
|
|
allocator.Free(finfo->buffer);
|
|
|
|
inflateEnd(&finfo->stream);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
allocator.Free(finfo);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (retval != NULL)
|
|
|
|
allocator.Free(retval);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
} /* ZIP_duplicate */
|
|
|
|
|
|
|
|
static int ZIP_flush(PHYSFS_Io *io) { return 1; /* no write support. */ }
|
|
|
|
|
|
|
|
static void ZIP_destroy(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
ZIPfileinfo *finfo = (ZIPfileinfo *) io->opaque;
|
|
|
|
finfo->io->destroy(finfo->io);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
if (finfo->entry->compression_method != COMPMETH_NONE)
|
|
|
|
inflateEnd(&finfo->stream);
|
|
|
|
|
|
|
|
if (finfo->buffer != NULL)
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(finfo->buffer);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(finfo);
|
2010-08-30 09:01:57 +02:00
|
|
|
allocator.Free(io);
|
|
|
|
} /* ZIP_destroy */
|
|
|
|
|
|
|
|
|
|
|
|
static const PHYSFS_Io ZIP_Io =
|
|
|
|
{
|
|
|
|
ZIP_read,
|
|
|
|
ZIP_write,
|
|
|
|
ZIP_seek,
|
|
|
|
ZIP_tell,
|
|
|
|
ZIP_length,
|
|
|
|
ZIP_duplicate,
|
|
|
|
ZIP_flush,
|
|
|
|
ZIP_destroy,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2001-07-08 05:25:12 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_sint64 zip_find_end_of_central_dir(PHYSFS_Io *io, PHYSFS_sint64 *len)
|
2002-07-13 12:17:13 +02:00
|
|
|
{
|
2002-07-23 09:49:13 +02:00
|
|
|
PHYSFS_uint8 buf[256];
|
2010-03-25 21:00:21 +01:00
|
|
|
PHYSFS_uint8 extra[4] = { 0, 0, 0, 0 };
|
2002-07-28 00:06:25 +02:00
|
|
|
PHYSFS_sint32 i = 0;
|
2002-07-13 12:17:13 +02:00
|
|
|
PHYSFS_sint64 filelen;
|
|
|
|
PHYSFS_sint64 filepos;
|
|
|
|
PHYSFS_sint32 maxread;
|
2002-07-23 09:49:13 +02:00
|
|
|
PHYSFS_sint32 totalread = 0;
|
|
|
|
int found = 0;
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
filelen = io->length(io);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(filelen == -1, ERRPASS, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Jump to the end of the file and start reading backwards.
|
|
|
|
* The last thing in the file is the zipfile comment, which is variable
|
|
|
|
* length, and the field that specifies its size is before it in the
|
|
|
|
* file (argh!)...this means that we need to scan backwards until we
|
|
|
|
* hit the end-of-central-dir signature. We can then sanity check that
|
|
|
|
* the comment was as big as it should be to make sure we're in the
|
|
|
|
* right place. The comment length field is 16 bits, so we can stop
|
2002-07-23 09:49:13 +02:00
|
|
|
* searching for that signature after a little more than 64k at most,
|
|
|
|
* and call it a corrupted zipfile.
|
2002-07-13 12:17:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (sizeof (buf) < filelen)
|
|
|
|
{
|
|
|
|
filepos = filelen - sizeof (buf);
|
|
|
|
maxread = sizeof (buf);
|
|
|
|
} /* if */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filepos = 0;
|
2003-03-12 07:19:37 +01:00
|
|
|
maxread = (PHYSFS_uint32) filelen;
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* else */
|
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
while ((totalread < filelen) && (totalread < 65557))
|
2002-07-13 12:17:13 +02:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!io->seek(io, filepos), ERRPASS, -1);
|
2002-07-23 09:49:13 +02:00
|
|
|
|
|
|
|
/* make sure we catch a signature between buffers. */
|
|
|
|
if (totalread != 0)
|
2002-07-13 12:17:13 +02:00
|
|
|
{
|
2012-03-09 10:50:27 +01:00
|
|
|
if (!__PHYSFS_readAll(io, buf, maxread - 4))
|
2010-01-28 08:27:45 +01:00
|
|
|
return -1;
|
2009-05-03 10:24:41 +02:00
|
|
|
memcpy(&buf[maxread - 4], &extra, sizeof (extra));
|
2002-07-23 09:49:13 +02:00
|
|
|
totalread += maxread - 4;
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* if */
|
2002-07-23 09:49:13 +02:00
|
|
|
else
|
|
|
|
{
|
2012-03-09 10:50:27 +01:00
|
|
|
if (!__PHYSFS_readAll(io, buf, maxread))
|
2010-01-28 08:27:45 +01:00
|
|
|
return -1;
|
2002-07-23 09:49:13 +02:00
|
|
|
totalread += maxread;
|
|
|
|
} /* else */
|
|
|
|
|
2009-05-03 10:24:41 +02:00
|
|
|
memcpy(&extra, buf, sizeof (extra));
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
for (i = maxread - 4; i > 0; i--)
|
|
|
|
{
|
|
|
|
if ((buf[i + 0] == 0x50) &&
|
|
|
|
(buf[i + 1] == 0x4B) &&
|
|
|
|
(buf[i + 2] == 0x05) &&
|
|
|
|
(buf[i + 3] == 0x06) )
|
|
|
|
{
|
|
|
|
found = 1; /* that's the signature! */
|
|
|
|
break;
|
|
|
|
} /* if */
|
|
|
|
} /* for */
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
break;
|
|
|
|
|
|
|
|
filepos -= (maxread - 4);
|
2007-05-05 07:52:43 +02:00
|
|
|
if (filepos < 0)
|
|
|
|
filepos = 0;
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* while */
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!found, PHYSFS_ERR_UNSUPPORTED, -1);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
if (len != NULL)
|
|
|
|
*len = filelen;
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return (filepos + i);
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_find_end_of_central_dir */
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static int isZip(PHYSFS_Io *io)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_uint32 sig = 0;
|
2002-07-31 06:18:58 +02:00
|
|
|
int retval = 0;
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The first thing in a zip file might be the signature of the
|
|
|
|
* first local file record, so it makes for a quick determination.
|
|
|
|
*/
|
2010-08-30 09:01:57 +02:00
|
|
|
if (readui32(io, &sig))
|
2001-07-15 11:29:30 +02:00
|
|
|
{
|
2002-07-31 06:18:58 +02:00
|
|
|
retval = (sig == ZIP_LOCAL_FILE_SIG);
|
|
|
|
if (!retval)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* No sig...might be a ZIP with data at the start
|
|
|
|
* (a self-extracting executable, etc), so we'll have to do
|
|
|
|
* it the hard way...
|
|
|
|
*/
|
2010-08-30 09:01:57 +02:00
|
|
|
retval = (zip_find_end_of_central_dir(io, NULL) != -1);
|
2002-07-31 06:18:58 +02:00
|
|
|
} /* if */
|
2001-07-15 11:29:30 +02:00
|
|
|
} /* if */
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2010-08-24 16:05:58 +02:00
|
|
|
} /* isZip */
|
2001-07-08 05:25:12 +02:00
|
|
|
|
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
static void zip_free_entries(ZIPentry *entries, PHYSFS_uint32 max)
|
2002-07-13 12:17:13 +02:00
|
|
|
{
|
2002-07-23 09:49:13 +02:00
|
|
|
PHYSFS_uint32 i;
|
|
|
|
for (i = 0; i < max; i++)
|
2002-07-13 12:17:13 +02:00
|
|
|
{
|
2002-07-23 09:49:13 +02:00
|
|
|
ZIPentry *entry = &entries[i];
|
|
|
|
if (entry->name != NULL)
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(entry->name);
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* for */
|
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(entries);
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_free_entries */
|
|
|
|
|
|
|
|
|
2002-08-29 01:32:29 +02:00
|
|
|
/*
|
|
|
|
* This will find the ZIPentry associated with a path in platform-independent
|
|
|
|
* notation. Directories don't have ZIPentries associated with them, but
|
|
|
|
* (*isDir) will be set to non-zero if a dir was hit.
|
|
|
|
*/
|
2010-02-15 20:02:36 +01:00
|
|
|
static ZIPentry *zip_find_entry(const ZIPinfo *info, const char *path,
|
|
|
|
int *isDir)
|
2002-07-23 09:49:13 +02:00
|
|
|
{
|
|
|
|
ZIPentry *a = info->entries;
|
2012-03-10 09:18:33 +01:00
|
|
|
PHYSFS_sint32 pathlen = (PHYSFS_sint32) strlen(path);
|
2002-08-30 16:30:49 +02:00
|
|
|
PHYSFS_sint32 lo = 0;
|
2002-07-23 09:49:13 +02:00
|
|
|
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
|
|
|
|
PHYSFS_sint32 middle;
|
2002-08-30 16:30:49 +02:00
|
|
|
const char *thispath = NULL;
|
2002-07-23 09:49:13 +02:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
while (lo <= hi)
|
|
|
|
{
|
2002-08-30 16:30:49 +02:00
|
|
|
middle = lo + ((hi - lo) / 2);
|
|
|
|
thispath = a[middle].name;
|
|
|
|
rc = strncmp(path, thispath, pathlen);
|
2002-08-29 01:32:29 +02:00
|
|
|
|
|
|
|
if (rc > 0)
|
2002-07-23 09:49:13 +02:00
|
|
|
lo = middle + 1;
|
2002-08-29 01:32:29 +02:00
|
|
|
|
|
|
|
else if (rc < 0)
|
2002-07-23 09:49:13 +02:00
|
|
|
hi = middle - 1;
|
2002-08-29 01:32:29 +02:00
|
|
|
|
2002-08-30 16:30:49 +02:00
|
|
|
else /* substring match...might be dir or entry or nothing. */
|
|
|
|
{
|
2002-08-29 01:32:29 +02:00
|
|
|
if (isDir != NULL)
|
|
|
|
{
|
2002-08-30 16:30:49 +02:00
|
|
|
*isDir = (thispath[pathlen] == '/');
|
|
|
|
if (*isDir)
|
2010-01-28 08:27:45 +01:00
|
|
|
return NULL;
|
2002-08-29 01:32:29 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (thispath[pathlen] == '\0') /* found entry? */
|
2010-01-28 08:27:45 +01:00
|
|
|
return &a[middle];
|
2011-02-18 18:38:05 +01:00
|
|
|
/* adjust search params, try again. */
|
|
|
|
else if (thispath[pathlen] > '/')
|
|
|
|
hi = middle - 1;
|
2002-08-30 16:30:49 +02:00
|
|
|
else
|
2011-02-18 18:38:05 +01:00
|
|
|
lo = middle + 1;
|
2002-08-30 16:30:49 +02:00
|
|
|
} /* if */
|
|
|
|
} /* while */
|
2002-08-29 01:32:29 +02:00
|
|
|
|
|
|
|
if (isDir != NULL)
|
|
|
|
*isDir = 0;
|
2002-07-23 09:49:13 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_NO_SUCH_PATH, NULL);
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_find_entry */
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
/* Convert paths from old, buggy DOS zippers... */
|
|
|
|
static void zip_convert_dos_path(ZIPentry *entry, char *path)
|
|
|
|
{
|
|
|
|
PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((entry->version >> 8) & 0xFF);
|
|
|
|
if (hosttype == 0) /* FS_FAT_ */
|
|
|
|
{
|
|
|
|
while (*path)
|
|
|
|
{
|
|
|
|
if (*path == '\\')
|
|
|
|
*path = '/';
|
|
|
|
path++;
|
|
|
|
} /* while */
|
|
|
|
} /* if */
|
|
|
|
} /* zip_convert_dos_path */
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
static void zip_expand_symlink_path(char *path)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2002-07-23 09:49:13 +02:00
|
|
|
char *ptr = path;
|
|
|
|
char *prevptr = path;
|
2001-07-15 11:29:30 +02:00
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
while (1)
|
2001-07-28 14:14:09 +02:00
|
|
|
{
|
2002-07-23 09:49:13 +02:00
|
|
|
ptr = strchr(ptr, '/');
|
|
|
|
if (ptr == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (*(ptr + 1) == '.')
|
|
|
|
{
|
|
|
|
if (*(ptr + 2) == '/')
|
|
|
|
{
|
|
|
|
/* current dir in middle of string: ditch it. */
|
|
|
|
memmove(ptr, ptr + 2, strlen(ptr + 2) + 1);
|
|
|
|
} /* else if */
|
|
|
|
|
|
|
|
else if (*(ptr + 2) == '\0')
|
|
|
|
{
|
|
|
|
/* current dir at end of string: ditch it. */
|
|
|
|
*ptr = '\0';
|
|
|
|
} /* else if */
|
|
|
|
|
|
|
|
else if (*(ptr + 2) == '.')
|
|
|
|
{
|
|
|
|
if (*(ptr + 3) == '/')
|
|
|
|
{
|
|
|
|
/* parent dir in middle: move back one, if possible. */
|
|
|
|
memmove(prevptr, ptr + 4, strlen(ptr + 4) + 1);
|
|
|
|
ptr = prevptr;
|
|
|
|
while (prevptr != path)
|
|
|
|
{
|
|
|
|
prevptr--;
|
|
|
|
if (*prevptr == '/')
|
|
|
|
{
|
|
|
|
prevptr++;
|
|
|
|
break;
|
|
|
|
} /* if */
|
|
|
|
} /* while */
|
|
|
|
} /* if */
|
2001-07-15 11:29:30 +02:00
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
if (*(ptr + 3) == '\0')
|
|
|
|
{
|
|
|
|
/* parent dir at end: move back one, if possible. */
|
|
|
|
*prevptr = '\0';
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prevptr = ptr;
|
2011-06-01 09:08:53 +02:00
|
|
|
ptr++;
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* else */
|
|
|
|
} /* while */
|
|
|
|
} /* zip_expand_symlink_path */
|
2001-07-15 11:29:30 +02:00
|
|
|
|
2004-09-29 08:18:04 +02:00
|
|
|
/* (forward reference: zip_follow_symlink and zip_resolve call each other.) */
|
2010-08-30 09:01:57 +02:00
|
|
|
static int zip_resolve(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry);
|
2001-07-15 11:29:30 +02:00
|
|
|
|
2002-03-29 09:20:06 +01:00
|
|
|
/*
|
2002-07-23 09:49:13 +02:00
|
|
|
* Look for the entry named by (path). If it exists, resolve it, and return
|
|
|
|
* a pointer to that entry. If it's another symlink, keep resolving until you
|
|
|
|
* hit a real file and then return a pointer to the final non-symlink entry.
|
|
|
|
* If there's a problem, return NULL. (path) is always free()'d by this
|
|
|
|
* function.
|
2002-03-29 09:20:06 +01:00
|
|
|
*/
|
2010-08-30 09:01:57 +02:00
|
|
|
static ZIPentry *zip_follow_symlink(PHYSFS_Io *io, ZIPinfo *info, char *path)
|
2001-07-28 14:14:09 +02:00
|
|
|
{
|
2002-07-23 09:49:13 +02:00
|
|
|
ZIPentry *entry;
|
|
|
|
|
|
|
|
zip_expand_symlink_path(path);
|
2002-08-29 01:32:29 +02:00
|
|
|
entry = zip_find_entry(info, path, NULL);
|
2002-07-23 09:49:13 +02:00
|
|
|
if (entry != NULL)
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
if (!zip_resolve(io, info, entry)) /* recursive! */
|
2002-07-23 09:49:13 +02:00
|
|
|
entry = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (entry->symlink != NULL)
|
|
|
|
entry = entry->symlink;
|
|
|
|
} /* else */
|
|
|
|
} /* if */
|
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(path);
|
2010-01-28 08:27:45 +01:00
|
|
|
return entry;
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_follow_symlink */
|
2001-07-08 05:25:12 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static int zip_resolve_symlink(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry)
|
2001-07-23 06:47:47 +02:00
|
|
|
{
|
2002-07-13 12:17:13 +02:00
|
|
|
char *path;
|
2010-08-30 09:01:57 +02:00
|
|
|
const PHYSFS_uint32 size = entry->uncompressed_size;
|
2002-07-13 12:17:13 +02:00
|
|
|
int rc = 0;
|
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
/*
|
|
|
|
* We've already parsed the local file header of the symlink at this
|
|
|
|
* point. Now we need to read the actual link from the file data and
|
|
|
|
* follow it.
|
|
|
|
*/
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!io->seek(io, entry->offset), ERRPASS, 0);
|
2002-07-23 09:49:13 +02:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
path = (char *) allocator.Malloc(size + 1);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(path == NULL, PHYSFS_ERR_OUT_OF_MEMORY, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
if (entry->compression_method == COMPMETH_NONE)
|
2012-03-09 10:50:27 +01:00
|
|
|
rc = __PHYSFS_readAll(io, path, size);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
else /* symlink target path is compressed... */
|
|
|
|
{
|
|
|
|
z_stream stream;
|
2010-08-30 09:01:57 +02:00
|
|
|
const PHYSFS_uint32 complen = entry->compressed_size;
|
2007-03-24 04:54:58 +01:00
|
|
|
PHYSFS_uint8 *compressed = (PHYSFS_uint8*) __PHYSFS_smallAlloc(complen);
|
2002-07-13 12:17:13 +02:00
|
|
|
if (compressed != NULL)
|
|
|
|
{
|
2012-03-09 10:50:27 +01:00
|
|
|
if (__PHYSFS_readAll(io, compressed, complen))
|
2002-07-13 12:17:13 +02:00
|
|
|
{
|
2004-09-23 08:45:36 +02:00
|
|
|
initializeZStream(&stream);
|
2002-07-13 12:17:13 +02:00
|
|
|
stream.next_in = compressed;
|
2007-03-24 04:54:58 +01:00
|
|
|
stream.avail_in = complen;
|
2003-01-31 05:07:48 +01:00
|
|
|
stream.next_out = (unsigned char *) path;
|
2002-07-13 12:17:13 +02:00
|
|
|
stream.avail_out = size;
|
|
|
|
if (zlib_err(inflateInit2(&stream, -MAX_WBITS)) == Z_OK)
|
|
|
|
{
|
2002-07-15 04:22:52 +02:00
|
|
|
rc = zlib_err(inflate(&stream, Z_FINISH));
|
2002-07-13 12:17:13 +02:00
|
|
|
inflateEnd(&stream);
|
2002-07-15 04:22:52 +02:00
|
|
|
|
|
|
|
/* both are acceptable outcomes... */
|
|
|
|
rc = ((rc == Z_OK) || (rc == Z_STREAM_END));
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* if */
|
|
|
|
} /* if */
|
2007-03-24 04:54:58 +01:00
|
|
|
__PHYSFS_smallFree(compressed);
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* if */
|
|
|
|
} /* else */
|
2002-03-29 09:20:06 +01:00
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
if (!rc)
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(path);
|
2002-07-23 09:49:13 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
path[entry->uncompressed_size] = '\0'; /* null-terminate it. */
|
|
|
|
zip_convert_dos_path(entry, path);
|
2010-08-30 09:01:57 +02:00
|
|
|
entry->symlink = zip_follow_symlink(io, info, path);
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* else */
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return (entry->symlink != NULL);
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_resolve_symlink */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the local file header of an entry, and update entry->offset.
|
|
|
|
*/
|
2010-08-30 09:01:57 +02:00
|
|
|
static int zip_parse_local(PHYSFS_Io *io, ZIPentry *entry)
|
2002-07-23 09:49:13 +02:00
|
|
|
{
|
|
|
|
PHYSFS_uint32 ui32;
|
|
|
|
PHYSFS_uint16 ui16;
|
|
|
|
PHYSFS_uint16 fnamelen;
|
|
|
|
PHYSFS_uint16 extralen;
|
|
|
|
|
2003-03-30 20:44:59 +02:00
|
|
|
/*
|
|
|
|
* crc and (un)compressed_size are always zero if this is a "JAR"
|
|
|
|
* archive created with Sun's Java tools, apparently. We only
|
|
|
|
* consider this archive corrupted if those entries don't match and
|
|
|
|
* aren't zero. That seems to work well.
|
|
|
|
*/
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!io->seek(io, entry->offset), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(ui32 != ZIP_LOCAL_FILE_SIG, PHYSFS_ERR_CORRUPT, 0);
|
|
|
|
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(ui16 != entry->version_needed, PHYSFS_ERR_CORRUPT, 0);
|
|
|
|
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); /* general bits. */
|
|
|
|
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(ui16 != entry->compression_method, PHYSFS_ERR_CORRUPT, 0);
|
|
|
|
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0); /* date/time */
|
|
|
|
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(ui32 && (ui32 != entry->crc), PHYSFS_ERR_CORRUPT, 0);
|
|
|
|
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(ui32 && (ui32!=entry->compressed_size),PHYSFS_ERR_CORRUPT,0);
|
|
|
|
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(ui32&&(ui32!=entry->uncompressed_size),PHYSFS_ERR_CORRUPT,0);
|
|
|
|
BAIL_IF_MACRO(!readui16(io, &fnamelen), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!readui16(io, &extralen), ERRPASS, 0);
|
2002-07-23 09:49:13 +02:00
|
|
|
|
|
|
|
entry->offset += fnamelen + extralen + 30;
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_parse_local */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static int zip_resolve(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry)
|
2002-07-23 09:49:13 +02:00
|
|
|
{
|
|
|
|
int retval = 1;
|
|
|
|
ZipResolveType resolve_type = entry->resolved;
|
|
|
|
|
|
|
|
/* Don't bother if we've failed to resolve this entry before. */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(resolve_type == ZIP_BROKEN_FILE, PHYSFS_ERR_CORRUPT, 0);
|
|
|
|
BAIL_IF_MACRO(resolve_type == ZIP_BROKEN_SYMLINK, PHYSFS_ERR_CORRUPT, 0);
|
2002-07-23 09:49:13 +02:00
|
|
|
|
|
|
|
/* uhoh...infinite symlink loop! */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(resolve_type == ZIP_RESOLVING, PHYSFS_ERR_SYMLINK_LOOP, 0);
|
2002-07-23 09:49:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We fix up the offset to point to the actual data on the
|
|
|
|
* first open, since we don't want to seek across the whole file on
|
|
|
|
* archive open (can be SLOW on large, CD-stored files), but we
|
|
|
|
* need to check the local file header...not just for corruption,
|
|
|
|
* but since it stores offset info the central directory does not.
|
|
|
|
*/
|
|
|
|
if (resolve_type != ZIP_RESOLVED)
|
|
|
|
{
|
|
|
|
entry->resolved = ZIP_RESOLVING;
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
retval = zip_parse_local(io, entry);
|
2002-07-23 09:49:13 +02:00
|
|
|
if (retval)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If it's a symlink, find the original file. This will cause
|
|
|
|
* resolution of other entries (other symlinks and, eventually,
|
|
|
|
* the real file) if all goes well.
|
|
|
|
*/
|
|
|
|
if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
|
2010-08-30 09:01:57 +02:00
|
|
|
retval = zip_resolve_symlink(io, info, entry);
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
|
|
|
|
entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_SYMLINK);
|
|
|
|
else if (resolve_type == ZIP_UNRESOLVED_FILE)
|
|
|
|
entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_FILE);
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* if */
|
2002-03-29 09:20:06 +01:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_resolve */
|
2001-07-23 06:47:47 +02:00
|
|
|
|
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
static int zip_version_does_symlinks(PHYSFS_uint32 version)
|
2001-07-23 06:47:47 +02:00
|
|
|
{
|
|
|
|
int retval = 0;
|
2002-04-03 09:40:27 +02:00
|
|
|
PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((version >> 8) & 0xFF);
|
2001-07-23 06:47:47 +02:00
|
|
|
|
|
|
|
switch (hosttype)
|
|
|
|
{
|
2002-07-15 15:20:34 +02:00
|
|
|
/*
|
|
|
|
* These are the platforms that can NOT build an archive with
|
|
|
|
* symlinks, according to the Info-ZIP project.
|
|
|
|
*/
|
|
|
|
case 0: /* FS_FAT_ */
|
|
|
|
case 1: /* AMIGA_ */
|
|
|
|
case 2: /* VMS_ */
|
|
|
|
case 4: /* VM_CSM_ */
|
|
|
|
case 6: /* FS_HPFS_ */
|
|
|
|
case 11: /* FS_NTFS_ */
|
|
|
|
case 14: /* FS_VFAT_ */
|
|
|
|
case 13: /* ACORN_ */
|
|
|
|
case 15: /* MVS_ */
|
|
|
|
case 18: /* THEOS_ */
|
|
|
|
break; /* do nothing. */
|
|
|
|
|
|
|
|
default: /* assume the rest to be unix-like. */
|
2001-07-23 06:47:47 +02:00
|
|
|
retval = 1;
|
|
|
|
break;
|
|
|
|
} /* switch */
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_version_does_symlinks */
|
|
|
|
|
|
|
|
|
2007-04-29 10:16:30 +02:00
|
|
|
static int zip_entry_is_symlink(const ZIPentry *entry)
|
2002-07-23 09:49:13 +02:00
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return ((entry->resolved == ZIP_UNRESOLVED_SYMLINK) ||
|
|
|
|
(entry->resolved == ZIP_BROKEN_SYMLINK) ||
|
|
|
|
(entry->symlink));
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_entry_is_symlink */
|
2001-07-23 06:47:47 +02:00
|
|
|
|
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
static int zip_has_symlink_attr(ZIPentry *entry, PHYSFS_uint32 extern_attr)
|
2001-07-23 06:47:47 +02:00
|
|
|
{
|
2002-07-15 15:20:34 +02:00
|
|
|
PHYSFS_uint16 xattr = ((extern_attr >> 16) & 0xFFFF);
|
2010-01-28 08:27:45 +01:00
|
|
|
return ( (zip_version_does_symlinks(entry->version)) &&
|
|
|
|
(entry->uncompressed_size > 0) &&
|
|
|
|
((xattr & UNIX_FILETYPE_MASK) == UNIX_FILETYPE_SYMLINK) );
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_has_symlink_attr */
|
2001-07-23 06:47:47 +02:00
|
|
|
|
|
|
|
|
2003-01-31 05:07:48 +01:00
|
|
|
static PHYSFS_sint64 zip_dos_time_to_physfs_time(PHYSFS_uint32 dostime)
|
2001-07-23 06:47:47 +02:00
|
|
|
{
|
2002-07-23 09:49:13 +02:00
|
|
|
PHYSFS_uint32 dosdate;
|
2002-07-13 12:17:13 +02:00
|
|
|
struct tm unixtime;
|
|
|
|
memset(&unixtime, '\0', sizeof (unixtime));
|
2001-07-28 14:14:09 +02:00
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
dosdate = (PHYSFS_uint32) ((dostime >> 16) & 0xFFFF);
|
|
|
|
dostime &= 0xFFFF;
|
2001-07-23 06:47:47 +02:00
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
/* dissect date */
|
|
|
|
unixtime.tm_year = ((dosdate >> 9) & 0x7F) + 80;
|
|
|
|
unixtime.tm_mon = ((dosdate >> 5) & 0x0F) - 1;
|
|
|
|
unixtime.tm_mday = ((dosdate ) & 0x1F);
|
|
|
|
|
|
|
|
/* dissect time */
|
|
|
|
unixtime.tm_hour = ((dostime >> 11) & 0x1F);
|
|
|
|
unixtime.tm_min = ((dostime >> 5) & 0x3F);
|
|
|
|
unixtime.tm_sec = ((dostime << 1) & 0x3E);
|
|
|
|
|
|
|
|
/* let mktime calculate daylight savings time. */
|
|
|
|
unixtime.tm_isdst = -1;
|
2001-07-28 14:14:09 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return ((PHYSFS_sint64) mktime(&unixtime));
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_dos_time_to_physfs_time */
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static int zip_load_entry(PHYSFS_Io *io, ZIPentry *entry, PHYSFS_uint32 ofs_fixup)
|
2002-07-13 12:17:13 +02:00
|
|
|
{
|
|
|
|
PHYSFS_uint16 fnamelen, extralen, commentlen;
|
|
|
|
PHYSFS_uint32 external_attr;
|
|
|
|
PHYSFS_uint16 ui16;
|
|
|
|
PHYSFS_uint32 ui32;
|
|
|
|
PHYSFS_sint64 si64;
|
|
|
|
|
|
|
|
/* sanity check with central directory signature... */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(ui32 != ZIP_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
/* Get the pertinent parts of the record... */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!readui16(io, &entry->version), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!readui16(io, &entry->version_needed), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); /* general bits */
|
|
|
|
BAIL_IF_MACRO(!readui16(io, &entry->compression_method), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
|
2002-07-23 09:49:13 +02:00
|
|
|
entry->last_mod_time = zip_dos_time_to_physfs_time(ui32);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!readui32(io, &entry->crc), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!readui32(io, &entry->compressed_size), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!readui32(io, &entry->uncompressed_size), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!readui16(io, &fnamelen), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!readui16(io, &extralen), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!readui16(io, &commentlen), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); /* disk number start */
|
|
|
|
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0); /* internal file attribs */
|
|
|
|
BAIL_IF_MACRO(!readui32(io, &external_attr), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!readui32(io, &entry->offset), ERRPASS, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
entry->offset += ofs_fixup;
|
2002-07-23 09:49:13 +02:00
|
|
|
|
|
|
|
entry->symlink = NULL; /* will be resolved later, if necessary. */
|
|
|
|
entry->resolved = (zip_has_symlink_attr(entry, external_attr)) ?
|
|
|
|
ZIP_UNRESOLVED_SYMLINK : ZIP_UNRESOLVED_FILE;
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
entry->name = (char *) allocator.Malloc(fnamelen + 1);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(entry->name == NULL, PHYSFS_ERR_OUT_OF_MEMORY, 0);
|
2012-03-09 10:50:27 +01:00
|
|
|
if (!__PHYSFS_readAll(io, entry->name, fnamelen))
|
2002-07-23 09:49:13 +02:00
|
|
|
goto zip_load_entry_puked;
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
entry->name[fnamelen] = '\0'; /* null-terminate the filename. */
|
2002-07-23 09:49:13 +02:00
|
|
|
zip_convert_dos_path(entry, entry->name);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
si64 = io->tell(io);
|
2002-07-13 12:17:13 +02:00
|
|
|
if (si64 == -1)
|
2002-07-23 09:49:13 +02:00
|
|
|
goto zip_load_entry_puked;
|
2001-07-28 14:14:09 +02:00
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
/* seek to the start of the next entry in the central directory... */
|
2010-08-30 09:01:57 +02:00
|
|
|
if (!io->seek(io, si64 + extralen + commentlen))
|
2002-07-23 09:49:13 +02:00
|
|
|
goto zip_load_entry_puked;
|
2001-07-28 14:14:09 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1; /* success. */
|
2001-07-28 14:14:09 +02:00
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
zip_load_entry_puked:
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(entry->name);
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0; /* failure. */
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_load_entry */
|
|
|
|
|
|
|
|
|
2002-08-20 03:34:27 +02:00
|
|
|
static int zip_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
|
2002-07-23 09:49:13 +02:00
|
|
|
{
|
2008-02-20 13:24:10 +01:00
|
|
|
if (one != two)
|
|
|
|
{
|
|
|
|
const ZIPentry *a = (const ZIPentry *) _a;
|
2010-01-28 08:27:45 +01:00
|
|
|
return strcmp(a[one].name, a[two].name);
|
2008-02-20 13:24:10 +01:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return 0;
|
2002-08-20 03:34:27 +02:00
|
|
|
} /* zip_entry_cmp */
|
2002-07-23 09:49:13 +02:00
|
|
|
|
|
|
|
|
2002-08-20 03:34:27 +02:00
|
|
|
static void zip_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
|
2002-07-23 09:49:13 +02:00
|
|
|
{
|
2008-02-20 13:24:10 +01:00
|
|
|
if (one != two)
|
|
|
|
{
|
|
|
|
ZIPentry tmp;
|
|
|
|
ZIPentry *first = &(((ZIPentry *) _a)[one]);
|
|
|
|
ZIPentry *second = &(((ZIPentry *) _a)[two]);
|
|
|
|
memcpy(&tmp, first, sizeof (ZIPentry));
|
|
|
|
memcpy(first, second, sizeof (ZIPentry));
|
|
|
|
memcpy(second, &tmp, sizeof (ZIPentry));
|
|
|
|
} /* if */
|
2002-08-20 03:34:27 +02:00
|
|
|
} /* zip_entry_swap */
|
2002-07-23 09:49:13 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static int zip_load_entries(PHYSFS_Io *io, ZIPinfo *info,
|
2002-07-13 12:17:13 +02:00
|
|
|
PHYSFS_uint32 data_ofs, PHYSFS_uint32 central_ofs)
|
|
|
|
{
|
|
|
|
PHYSFS_uint32 max = info->entryCount;
|
|
|
|
PHYSFS_uint32 i;
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!io->seek(io, central_ofs), ERRPASS, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
info->entries = (ZIPentry *) allocator.Malloc(sizeof (ZIPentry) * max);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!info->entries, PHYSFS_ERR_OUT_OF_MEMORY, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
for (i = 0; i < max; i++)
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
if (!zip_load_entry(io, &info->entries[i], data_ofs))
|
2001-07-28 14:14:09 +02:00
|
|
|
{
|
2002-07-23 09:49:13 +02:00
|
|
|
zip_free_entries(info->entries, i);
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2001-07-28 14:14:09 +02:00
|
|
|
} /* if */
|
|
|
|
} /* for */
|
|
|
|
|
2002-08-20 03:34:27 +02:00
|
|
|
__PHYSFS_sort(info->entries, max, zip_entry_cmp, zip_entry_swap);
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_load_entries */
|
2001-07-28 14:14:09 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static int zip_parse_end_of_central_dir(PHYSFS_Io *io, ZIPinfo *info,
|
2002-07-23 09:49:13 +02:00
|
|
|
PHYSFS_uint32 *data_start,
|
|
|
|
PHYSFS_uint32 *central_dir_ofs)
|
2001-07-28 14:14:09 +02:00
|
|
|
{
|
2002-07-13 12:17:13 +02:00
|
|
|
PHYSFS_uint32 ui32;
|
|
|
|
PHYSFS_uint16 ui16;
|
|
|
|
PHYSFS_sint64 len;
|
|
|
|
PHYSFS_sint64 pos;
|
2001-07-28 14:14:09 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
/* !!! FIXME: ERR_UNSUPPORTED should be ERR_CORRUPT...right? */
|
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
/* find the end-of-central-dir record, and seek to it. */
|
2010-08-30 09:01:57 +02:00
|
|
|
pos = zip_find_end_of_central_dir(io, &len);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(pos == -1, ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!io->seek(io, pos), ERRPASS, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
/* check signature again, just in case. */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(ui32 != ZIP_END_OF_CENTRAL_DIR_SIG, PHYSFS_ERR_UNSUPPORTED, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2002-08-30 16:30:49 +02:00
|
|
|
/* number of this disk */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(ui16 != 0, PHYSFS_ERR_UNSUPPORTED, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2002-08-30 16:30:49 +02:00
|
|
|
/* number of the disk with the start of the central directory */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(ui16 != 0, PHYSFS_ERR_UNSUPPORTED, 0);
|
2001-07-28 14:14:09 +02:00
|
|
|
|
2002-08-30 16:30:49 +02:00
|
|
|
/* total number of entries in the central dir on this disk */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2002-08-30 16:30:49 +02:00
|
|
|
/* total number of entries in the central dir */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!readui16(io, &info->entryCount), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(ui16 != info->entryCount, PHYSFS_ERR_UNSUPPORTED, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2002-08-30 16:30:49 +02:00
|
|
|
/* size of the central directory */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2002-08-30 16:30:49 +02:00
|
|
|
/* offset of central directory */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!readui32(io, central_dir_ofs), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(pos < *central_dir_ofs + ui32, PHYSFS_ERR_UNSUPPORTED, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For self-extracting archives, etc, there's crapola in the file
|
|
|
|
* before the zipfile records; we calculate how much data there is
|
|
|
|
* prepended by determining how far the central directory offset is
|
|
|
|
* from where it is supposed to be (start of end-of-central-dir minus
|
|
|
|
* sizeof central dir)...the difference in bytes is how much arbitrary
|
|
|
|
* data is at the start of the physical file.
|
|
|
|
*/
|
2003-03-12 07:19:37 +01:00
|
|
|
*data_start = (PHYSFS_uint32) (pos - (*central_dir_ofs + ui32));
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
/* Now that we know the difference, fix up the central dir offset... */
|
|
|
|
*central_dir_ofs += *data_start;
|
|
|
|
|
2002-08-30 16:30:49 +02:00
|
|
|
/* zipfile comment length */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure that the comment length matches to the end of file...
|
|
|
|
* If it doesn't, we're either in the wrong part of the file, or the
|
|
|
|
* file is corrupted, but we give up either way.
|
|
|
|
*/
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO((pos + 22 + ui16) != len, PHYSFS_ERR_UNSUPPORTED, 0);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1; /* made it. */
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_parse_end_of_central_dir */
|
2002-07-13 12:17:13 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static void *ZIP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
|
2002-07-13 12:17:13 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
ZIPinfo *info = NULL;
|
2002-07-13 12:17:13 +02:00
|
|
|
PHYSFS_uint32 data_start;
|
2002-07-23 09:49:13 +02:00
|
|
|
PHYSFS_uint32 cent_dir_ofs;
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
assert(io != NULL); /* shouldn't ever happen. */
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
|
|
|
|
BAIL_IF_MACRO(!isZip(io), ERRPASS, NULL);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
info = (ZIPinfo *) allocator.Malloc(sizeof (ZIPinfo));
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!info, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2010-08-30 09:01:57 +02:00
|
|
|
memset(info, '\0', sizeof (ZIPinfo));
|
|
|
|
info->io = io;
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
if (!zip_parse_end_of_central_dir(io, info, &data_start, ¢_dir_ofs))
|
|
|
|
goto ZIP_openarchive_failed;
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
if (!zip_load_entries(io, info, data_start, cent_dir_ofs))
|
|
|
|
goto ZIP_openarchive_failed;
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return info;
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
ZIP_openarchive_failed:
|
2004-09-26 02:25:04 +02:00
|
|
|
if (info != NULL)
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(info);
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return NULL;
|
2001-07-28 14:14:09 +02:00
|
|
|
} /* ZIP_openArchive */
|
2001-07-23 06:47:47 +02:00
|
|
|
|
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
static PHYSFS_sint32 zip_find_start_of_dir(ZIPinfo *info, const char *path,
|
|
|
|
int stop_on_first_find)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2002-07-23 09:49:13 +02:00
|
|
|
PHYSFS_sint32 lo = 0;
|
2003-07-20 23:13:25 +02:00
|
|
|
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
|
2002-07-23 09:49:13 +02:00
|
|
|
PHYSFS_sint32 middle;
|
2012-03-10 09:18:33 +01:00
|
|
|
PHYSFS_uint32 dlen = (PHYSFS_uint32) strlen(path);
|
2002-07-23 09:49:13 +02:00
|
|
|
PHYSFS_sint32 retval = -1;
|
|
|
|
const char *name;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (*path == '\0') /* root dir? */
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2001-07-15 11:29:30 +02:00
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
|
2001-07-23 06:47:47 +02:00
|
|
|
dlen--;
|
2001-07-15 11:29:30 +02:00
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
while (lo <= hi)
|
2001-07-15 11:29:30 +02:00
|
|
|
{
|
2002-07-23 09:49:13 +02:00
|
|
|
middle = lo + ((hi - lo) / 2);
|
|
|
|
name = info->entries[middle].name;
|
|
|
|
rc = strncmp(path, name, dlen);
|
|
|
|
if (rc == 0)
|
2001-07-23 06:47:47 +02:00
|
|
|
{
|
2002-07-23 09:49:13 +02:00
|
|
|
char ch = name[dlen];
|
2005-07-23 23:46:07 +02:00
|
|
|
if ('/' < ch) /* make sure this isn't just a substr match. */
|
2002-07-23 09:49:13 +02:00
|
|
|
rc = -1;
|
2005-07-23 23:46:07 +02:00
|
|
|
else if ('/' > ch)
|
2002-07-23 09:49:13 +02:00
|
|
|
rc = 1;
|
|
|
|
else
|
2001-07-23 06:47:47 +02:00
|
|
|
{
|
2002-07-25 06:41:43 +02:00
|
|
|
if (stop_on_first_find) /* Just checking dir's existance? */
|
2010-01-28 08:27:45 +01:00
|
|
|
return middle;
|
2001-07-23 06:47:47 +02:00
|
|
|
|
2002-07-25 06:41:43 +02:00
|
|
|
if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
|
2010-01-28 08:27:45 +01:00
|
|
|
return (middle + 1);
|
2002-07-25 06:41:43 +02:00
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
/* there might be more entries earlier in the list. */
|
|
|
|
retval = middle;
|
|
|
|
hi = middle - 1;
|
|
|
|
} /* else */
|
2001-07-23 06:47:47 +02:00
|
|
|
} /* if */
|
|
|
|
|
2002-07-23 09:49:13 +02:00
|
|
|
if (rc > 0)
|
|
|
|
lo = middle + 1;
|
2001-07-15 11:29:30 +02:00
|
|
|
else
|
2002-07-23 09:49:13 +02:00
|
|
|
hi = middle - 1;
|
|
|
|
} /* while */
|
2001-07-15 11:29:30 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* zip_find_start_of_dir */
|
2001-07-08 05:25:12 +02:00
|
|
|
|
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
/*
|
|
|
|
* Moved to seperate function so we can use alloca then immediately throw
|
|
|
|
* away the allocated stack space...
|
|
|
|
*/
|
2005-09-18 23:44:42 +02:00
|
|
|
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
|
|
|
|
const char *odir, const char *str, PHYSFS_sint32 ln)
|
2004-09-29 08:09:29 +02:00
|
|
|
{
|
2007-03-24 04:54:58 +01:00
|
|
|
char *newstr = __PHYSFS_smallAlloc(ln + 1);
|
2004-09-29 08:09:29 +02:00
|
|
|
if (newstr == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(newstr, str, ln);
|
|
|
|
newstr[ln] = '\0';
|
2005-09-18 23:44:42 +02:00
|
|
|
cb(callbackdata, odir, newstr);
|
2007-03-24 04:54:58 +01:00
|
|
|
__PHYSFS_smallFree(newstr);
|
2004-09-29 08:09:29 +02:00
|
|
|
} /* doEnumCallback */
|
|
|
|
|
|
|
|
|
|
|
|
static void ZIP_enumerateFiles(dvoid *opaque, const char *dname,
|
2005-09-18 23:44:42 +02:00
|
|
|
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
|
|
|
|
const char *origdir, void *callbackdata)
|
2001-07-23 06:47:47 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
ZIPinfo *info = ((ZIPinfo *) opaque);
|
2002-07-25 06:41:43 +02:00
|
|
|
PHYSFS_sint32 dlen, dlen_inc, max, i;
|
2002-07-23 19:52:56 +02:00
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
i = zip_find_start_of_dir(info, dname, 0);
|
|
|
|
if (i == -1) /* no such directory. */
|
|
|
|
return;
|
2001-07-23 06:47:47 +02:00
|
|
|
|
2012-03-10 09:18:33 +01:00
|
|
|
dlen = (PHYSFS_sint32) strlen(dname);
|
2004-09-29 08:09:29 +02:00
|
|
|
if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
|
2002-07-25 06:41:43 +02:00
|
|
|
dlen--;
|
2001-07-23 06:47:47 +02:00
|
|
|
|
2002-07-25 06:41:43 +02:00
|
|
|
dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
|
|
|
|
max = (PHYSFS_sint32) info->entryCount;
|
|
|
|
while (i < max)
|
|
|
|
{
|
|
|
|
char *e = info->entries[i].name;
|
2004-09-29 08:09:29 +02:00
|
|
|
if ((dlen) && ((strncmp(e, dname, dlen) != 0) || (e[dlen] != '/')))
|
2002-07-25 06:41:43 +02:00
|
|
|
break; /* past end of this dir; we're done. */
|
2002-07-23 09:49:13 +02:00
|
|
|
|
2002-07-25 06:41:43 +02:00
|
|
|
if ((omitSymLinks) && (zip_entry_is_symlink(&info->entries[i])))
|
|
|
|
i++;
|
|
|
|
else
|
2001-07-23 06:47:47 +02:00
|
|
|
{
|
2002-07-25 06:41:43 +02:00
|
|
|
char *add = e + dlen_inc;
|
|
|
|
char *ptr = strchr(add, '/');
|
|
|
|
PHYSFS_sint32 ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
|
2005-09-18 23:44:42 +02:00
|
|
|
doEnumCallback(cb, callbackdata, origdir, add, ln);
|
2002-07-25 06:41:43 +02:00
|
|
|
ln += dlen_inc; /* point past entry to children... */
|
|
|
|
|
|
|
|
/* increment counter and skip children of subdirs... */
|
|
|
|
while ((++i < max) && (ptr != NULL))
|
2001-07-23 06:47:47 +02:00
|
|
|
{
|
2002-07-25 06:41:43 +02:00
|
|
|
char *e_new = info->entries[i].name;
|
2002-07-26 19:03:26 +02:00
|
|
|
if ((strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
|
|
|
|
break;
|
2002-07-25 06:41:43 +02:00
|
|
|
} /* while */
|
|
|
|
} /* else */
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* while */
|
|
|
|
} /* ZIP_enumerateFiles */
|
2001-07-23 06:47:47 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_Io *zip_get_io(PHYSFS_Io *io, ZIPinfo *inf, ZIPentry *entry)
|
2002-07-23 09:49:13 +02:00
|
|
|
{
|
|
|
|
int success;
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *retval = io->duplicate(io);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!retval, ERRPASS, NULL);
|
|
|
|
|
|
|
|
/* !!! FIXME: if you open a dir here, it should bail ERR_NOT_A_FILE */
|
2002-07-23 09:49:13 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
/* (inf) can be NULL if we already resolved. */
|
|
|
|
success = (inf == NULL) || zip_resolve(retval, inf, entry);
|
2002-07-23 09:49:13 +02:00
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
PHYSFS_sint64 offset;
|
|
|
|
offset = ((entry->symlink) ? entry->symlink->offset : entry->offset);
|
2010-08-30 09:01:57 +02:00
|
|
|
success = retval->seek(retval, offset);
|
2002-07-23 09:49:13 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
retval->destroy(retval);
|
2002-07-23 09:49:13 +02:00
|
|
|
retval = NULL;
|
|
|
|
} /* if */
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2010-08-30 09:01:57 +02:00
|
|
|
} /* zip_get_io */
|
2002-07-23 09:49:13 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_Io *ZIP_openRead(dvoid *opaque, const char *fnm, int *fileExists)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *retval = NULL;
|
2004-09-26 02:25:04 +02:00
|
|
|
ZIPinfo *info = (ZIPinfo *) opaque;
|
2002-08-29 01:32:29 +02:00
|
|
|
ZIPentry *entry = zip_find_entry(info, fnm, NULL);
|
2001-07-23 09:16:22 +02:00
|
|
|
ZIPfileinfo *finfo = NULL;
|
2001-07-23 06:47:47 +02:00
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
*fileExists = (entry != NULL);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!entry, ERRPASS, NULL);
|
2001-07-23 06:47:47 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed);
|
2001-07-23 09:16:22 +02:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
finfo = (ZIPfileinfo *) allocator.Malloc(sizeof (ZIPfileinfo));
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed);
|
2002-07-13 12:17:13 +02:00
|
|
|
memset(finfo, '\0', sizeof (ZIPfileinfo));
|
2010-08-30 09:01:57 +02:00
|
|
|
|
|
|
|
finfo->io = zip_get_io(info->io, info, entry);
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!finfo->io, ERRPASS, ZIP_openRead_failed);
|
2002-07-23 09:49:13 +02:00
|
|
|
finfo->entry = ((entry->symlink != NULL) ? entry->symlink : entry);
|
2004-09-23 08:45:36 +02:00
|
|
|
initializeZStream(&finfo->stream);
|
2010-08-30 09:01:57 +02:00
|
|
|
|
2002-07-13 12:17:13 +02:00
|
|
|
if (finfo->entry->compression_method != COMPMETH_NONE)
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
finfo->buffer = (PHYSFS_uint8 *) allocator.Malloc(ZIP_READBUFSIZE);
|
2012-03-20 20:38:12 +01:00
|
|
|
if (!finfo->buffer)
|
|
|
|
GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed);
|
|
|
|
else if (zlib_err(inflateInit2(&finfo->stream, -MAX_WBITS)) != Z_OK)
|
2010-08-30 09:01:57 +02:00
|
|
|
goto ZIP_openRead_failed;
|
|
|
|
} /* if */
|
2002-07-13 12:17:13 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
memcpy(retval, &ZIP_Io, sizeof (PHYSFS_Io));
|
|
|
|
retval->opaque = finfo;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
ZIP_openRead_failed:
|
|
|
|
if (finfo != NULL)
|
|
|
|
{
|
|
|
|
if (finfo->io != NULL)
|
|
|
|
finfo->io->destroy(finfo->io);
|
|
|
|
|
|
|
|
if (finfo->buffer != NULL)
|
2002-07-13 12:17:13 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
allocator.Free(finfo->buffer);
|
|
|
|
inflateEnd(&finfo->stream);
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* if */
|
2010-08-30 09:01:57 +02:00
|
|
|
|
|
|
|
allocator.Free(finfo);
|
2002-07-13 12:17:13 +02:00
|
|
|
} /* if */
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
if (retval != NULL)
|
|
|
|
allocator.Free(retval);
|
|
|
|
|
|
|
|
return NULL;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* ZIP_openRead */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_Io *ZIP_openWrite(dvoid *opaque, const char *filename)
|
2002-08-21 04:59:15 +02:00
|
|
|
{
|
2012-03-22 05:05:10 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
|
2002-08-21 04:59:15 +02:00
|
|
|
} /* ZIP_openWrite */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_Io *ZIP_openAppend(dvoid *opaque, const char *filename)
|
2002-08-21 04:59:15 +02:00
|
|
|
{
|
2012-03-22 05:05:10 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
|
2002-08-21 04:59:15 +02:00
|
|
|
} /* ZIP_openAppend */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static void ZIP_dirClose(dvoid *opaque)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
ZIPinfo *zi = (ZIPinfo *) (opaque);
|
2010-08-30 09:01:57 +02:00
|
|
|
zi->io->destroy(zi->io);
|
2002-07-23 09:49:13 +02:00
|
|
|
zip_free_entries(zi->entries, zi->entryCount);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(zi);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* ZIP_dirClose */
|
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int ZIP_remove(dvoid *opaque, const char *name)
|
2002-08-21 04:59:15 +02:00
|
|
|
{
|
2012-03-22 05:05:10 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
|
2002-08-21 04:59:15 +02:00
|
|
|
} /* ZIP_remove */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int ZIP_mkdir(dvoid *opaque, const char *name)
|
2002-08-21 04:59:15 +02:00
|
|
|
{
|
2012-03-22 05:05:10 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
|
2002-08-21 04:59:15 +02:00
|
|
|
} /* ZIP_mkdir */
|
|
|
|
|
2004-09-29 08:18:04 +02:00
|
|
|
|
2010-08-22 06:37:25 +02:00
|
|
|
static int ZIP_stat(dvoid *opaque, const char *filename, int *exists,
|
2010-02-15 20:02:36 +01:00
|
|
|
PHYSFS_Stat *stat)
|
|
|
|
{
|
|
|
|
int isDir = 0;
|
|
|
|
const ZIPinfo *info = (const ZIPinfo *) opaque;
|
|
|
|
const ZIPentry *entry = zip_find_entry(info, filename, &isDir);
|
|
|
|
|
2010-09-05 08:41:13 +02:00
|
|
|
/* !!! FIXME: does this need to resolve entries here? */
|
|
|
|
|
2010-02-15 20:02:36 +01:00
|
|
|
*exists = isDir || (entry != 0);
|
|
|
|
if (!*exists)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (isDir)
|
|
|
|
{
|
|
|
|
stat->filesize = 0;
|
|
|
|
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
else if (zip_entry_is_symlink(entry))
|
|
|
|
{
|
|
|
|
stat->filesize = 0;
|
|
|
|
stat->filetype = PHYSFS_FILETYPE_SYMLINK;
|
|
|
|
} /* else if */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stat->filesize = entry->uncompressed_size;
|
|
|
|
stat->filetype = PHYSFS_FILETYPE_REGULAR;
|
|
|
|
} /* else */
|
|
|
|
|
|
|
|
stat->modtime = ((entry) ? entry->last_mod_time : 0);
|
|
|
|
stat->createtime = stat->modtime;
|
|
|
|
stat->accesstime = 0;
|
|
|
|
stat->readonly = 1; /* .zip files are always read only */
|
|
|
|
|
2010-08-22 01:10:42 +02:00
|
|
|
return 1;
|
2010-02-15 20:02:36 +01:00
|
|
|
} /* ZIP_stat */
|
|
|
|
|
|
|
|
|
2004-09-29 08:18:04 +02:00
|
|
|
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP =
|
|
|
|
{
|
|
|
|
"ZIP",
|
|
|
|
ZIP_ARCHIVE_DESCRIPTION,
|
2006-01-01 13:33:19 +01:00
|
|
|
"Ryan C. Gordon <icculus@icculus.org>",
|
2004-09-29 08:18:04 +02:00
|
|
|
"http://icculus.org/physfs/",
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const PHYSFS_Archiver __PHYSFS_Archiver_ZIP =
|
|
|
|
{
|
|
|
|
&__PHYSFS_ArchiveInfo_ZIP,
|
|
|
|
ZIP_openArchive, /* openArchive() method */
|
|
|
|
ZIP_enumerateFiles, /* enumerateFiles() method */
|
|
|
|
ZIP_openRead, /* openRead() method */
|
|
|
|
ZIP_openWrite, /* openWrite() method */
|
|
|
|
ZIP_openAppend, /* openAppend() method */
|
|
|
|
ZIP_remove, /* remove() method */
|
|
|
|
ZIP_mkdir, /* mkdir() method */
|
|
|
|
ZIP_dirClose, /* dirClose() method */
|
2010-08-30 09:01:57 +02:00
|
|
|
ZIP_stat /* stat() method */
|
2004-09-29 08:18:04 +02:00
|
|
|
};
|
|
|
|
|
2002-05-10 11:25:25 +02:00
|
|
|
#endif /* defined PHYSFS_SUPPORTS_ZIP */
|
|
|
|
|
2001-07-07 10:24:47 +02:00
|
|
|
/* end of zip.c ... */
|
|
|
|
|