misc/libphysfs/archiver_zip.c
author nemo
Mon, 10 Apr 2017 09:05:16 -0400
changeset 12217 ea891871f481
parent 10017 de822cd3df3a
child 12218 bb5522e88ab2
permissions -rw-r--r--
merge in patch used in arch/fedora - fixes a lazy physfs memcpy that was breaking upstream's stricter checking
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     1
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     2
 * ZIP support routines for PhysicsFS.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     3
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     4
 * Please see the file LICENSE.txt in the source's root directory.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     5
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     6
 *  This file written by Ryan C. Gordon, with some peeking at "unzip.c"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     7
 *   by Gilles Vollant.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     8
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     9
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    10
#define __PHYSICSFS_INTERNAL__
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    11
#include "physfs_internal.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    12
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    13
#if PHYSFS_SUPPORTS_ZIP
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    14
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    15
#include <errno.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    16
#include <time.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    17
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    18
#define USE_MINIZ 1
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    19
#if USE_MINIZ
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    20
#include "physfs_miniz.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    21
#else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    22
#include <zlib.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    23
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    24
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    25
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    26
 * A buffer of ZIP_READBUFSIZE is allocated for each compressed file opened,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    27
 *  and is freed when you close the file; compressed data is read into
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    28
 *  this buffer, and then is decompressed into the buffer passed to
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    29
 *  PHYSFS_read().
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    30
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    31
 * Uncompressed entries in a zipfile do not allocate this buffer; they just
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    32
 *  read data directly into the buffer passed to PHYSFS_read().
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    33
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    34
 * Depending on your speed and memory requirements, you should tweak this
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    35
 *  value.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    36
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    37
#define ZIP_READBUFSIZE   (16 * 1024)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    38
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    39
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    40
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    41
 * Entries are "unresolved" until they are first opened. At that time,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    42
 *  local file headers parsed/validated, data offsets will be updated to look
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    43
 *  at the actual file data instead of the header, and symlinks will be
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    44
 *  followed and optimized. This means that we don't seek and read around the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    45
 *  archive until forced to do so, and after the first time, we had to do
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    46
 *  less reading and parsing, which is very CD-ROM friendly.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    47
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    48
typedef enum
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    49
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    50
    ZIP_UNRESOLVED_FILE,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    51
    ZIP_UNRESOLVED_SYMLINK,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    52
    ZIP_RESOLVING,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    53
    ZIP_RESOLVED,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    54
    ZIP_BROKEN_FILE,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    55
    ZIP_BROKEN_SYMLINK
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    56
} ZipResolveType;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    57
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    58
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    59
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    60
 * One ZIPentry is kept for each file in an open ZIP archive.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    61
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    62
typedef struct _ZIPentry
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    63
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    64
    char *name;                         /* Name of file in archive        */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    65
    struct _ZIPentry *symlink;          /* NULL or file we symlink to     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    66
    ZipResolveType resolved;            /* Have we resolved file/symlink? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    67
    PHYSFS_uint64 offset;               /* offset of data in archive      */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    68
    PHYSFS_uint16 version;              /* version made by                */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    69
    PHYSFS_uint16 version_needed;       /* version needed to extract      */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    70
    PHYSFS_uint16 compression_method;   /* compression method             */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    71
    PHYSFS_uint32 crc;                  /* crc-32                         */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    72
    PHYSFS_uint64 compressed_size;      /* compressed size                */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    73
    PHYSFS_uint64 uncompressed_size;    /* uncompressed size              */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    74
    PHYSFS_sint64 last_mod_time;        /* last file mod time             */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    75
} ZIPentry;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    76
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    77
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    78
 * One ZIPinfo is kept for each open ZIP archive.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    79
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    80
typedef struct
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    81
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    82
    PHYSFS_Io *io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    83
    int zip64;                /* non-zero if this is a Zip64 archive. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    84
    PHYSFS_uint64 entryCount; /* Number of files in ZIP.              */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    85
    ZIPentry *entries;        /* info on all files in ZIP.            */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    86
} ZIPinfo;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    87
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    88
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    89
 * One ZIPfileinfo is kept for each open file in a ZIP archive.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    90
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    91
typedef struct
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    92
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    93
    ZIPentry *entry;                      /* Info on file.              */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    94
    PHYSFS_Io *io;                        /* physical file handle.      */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    95
    PHYSFS_uint32 compressed_position;    /* offset in compressed data. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    96
    PHYSFS_uint32 uncompressed_position;  /* tell() position.           */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    97
    PHYSFS_uint8 *buffer;                 /* decompression buffer.      */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    98
    z_stream stream;                      /* zlib stream state.         */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    99
} ZIPfileinfo;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   100
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   101
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   102
/* Magic numbers... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   103
#define ZIP_LOCAL_FILE_SIG                          0x04034b50
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   104
#define ZIP_CENTRAL_DIR_SIG                         0x02014b50
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   105
#define ZIP_END_OF_CENTRAL_DIR_SIG                  0x06054b50
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   106
#define ZIP64_END_OF_CENTRAL_DIR_SIG                0x06064b50
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   107
#define ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIG  0x07064b50
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   108
#define ZIP64_EXTENDED_INFO_EXTRA_FIELD_SIG         0x0001
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   109
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   110
/* compression methods... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   111
#define COMPMETH_NONE 0
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   112
/* ...and others... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   113
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   114
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   115
#define UNIX_FILETYPE_MASK    0170000
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   116
#define UNIX_FILETYPE_SYMLINK 0120000
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   117
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   118
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   119
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   120
 * Bridge physfs allocation functions to zlib's format...
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   121
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   122
static voidpf zlibPhysfsAlloc(voidpf opaque, uInt items, uInt size)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   123
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   124
    return ((PHYSFS_Allocator *) opaque)->Malloc(items * size);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   125
} /* zlibPhysfsAlloc */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   126
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   127
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   128
 * Bridge physfs allocation functions to zlib's format...
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   129
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   130
static void zlibPhysfsFree(voidpf opaque, voidpf address)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   131
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   132
    ((PHYSFS_Allocator *) opaque)->Free(address);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   133
} /* zlibPhysfsFree */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   134
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   135
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   136
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   137
 * Construct a new z_stream to a sane state.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   138
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   139
static void initializeZStream(z_stream *pstr)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   140
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   141
    memset(pstr, '\0', sizeof (z_stream));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   142
    pstr->zalloc = zlibPhysfsAlloc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   143
    pstr->zfree = zlibPhysfsFree;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   144
    pstr->opaque = &allocator;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   145
} /* initializeZStream */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   146
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   147
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   148
static PHYSFS_ErrorCode zlib_error_code(int rc)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   149
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   150
    switch (rc)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   151
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   152
        case Z_OK: return PHYSFS_ERR_OK;  /* not an error. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   153
        case Z_STREAM_END: return PHYSFS_ERR_OK; /* not an error. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   154
        case Z_ERRNO: return PHYSFS_ERR_IO;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   155
        case Z_MEM_ERROR: return PHYSFS_ERR_OUT_OF_MEMORY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   156
        default: return PHYSFS_ERR_CORRUPT;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   157
    } /* switch */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   158
} /* zlib_error_string */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   159
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   160
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   161
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   162
 * Wrap all zlib calls in this, so the physfs error state is set appropriately.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   163
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   164
static int zlib_err(const int rc)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   165
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   166
    __PHYSFS_setError(zlib_error_code(rc));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   167
    return rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   168
} /* zlib_err */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   169
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   170
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   171
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   172
 * Read an unsigned 64-bit int and swap to native byte order.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   173
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   174
static int readui64(PHYSFS_Io *io, PHYSFS_uint64 *val)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   175
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   176
    PHYSFS_uint64 v;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   177
    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &v, sizeof (v)), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   178
    *val = PHYSFS_swapULE64(v);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   179
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   180
} /* readui64 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   181
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   182
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   183
 * Read an unsigned 32-bit int and swap to native byte order.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   184
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   185
static int readui32(PHYSFS_Io *io, PHYSFS_uint32 *val)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   186
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   187
    PHYSFS_uint32 v;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   188
    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &v, sizeof (v)), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   189
    *val = PHYSFS_swapULE32(v);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   190
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   191
} /* readui32 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   192
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   193
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   194
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   195
 * Read an unsigned 16-bit int and swap to native byte order.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   196
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   197
static int readui16(PHYSFS_Io *io, PHYSFS_uint16 *val)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   198
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   199
    PHYSFS_uint16 v;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   200
    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &v, sizeof (v)), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   201
    *val = PHYSFS_swapULE16(v);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   202
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   203
} /* readui16 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   204
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   205
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   206
static PHYSFS_sint64 ZIP_read(PHYSFS_Io *_io, void *buf, PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   207
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   208
    ZIPfileinfo *finfo = (ZIPfileinfo *) _io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   209
    PHYSFS_Io *io = finfo->io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   210
    ZIPentry *entry = finfo->entry;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   211
    PHYSFS_sint64 retval = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   212
    PHYSFS_sint64 maxread = (PHYSFS_sint64) len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   213
    PHYSFS_sint64 avail = entry->uncompressed_size -
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   214
                          finfo->uncompressed_position;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   215
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   216
    if (avail < maxread)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   217
        maxread = avail;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   218
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   219
    BAIL_IF_MACRO(maxread == 0, ERRPASS, 0);    /* quick rejection. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   220
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   221
    if (entry->compression_method == COMPMETH_NONE)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   222
        retval = io->read(io, buf, maxread);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   223
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   224
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   225
        finfo->stream.next_out = buf;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   226
        finfo->stream.avail_out = (uInt) maxread;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   227
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   228
        while (retval < maxread)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   229
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   230
            PHYSFS_uint32 before = finfo->stream.total_out;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   231
            int rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   232
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   233
            if (finfo->stream.avail_in == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   234
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   235
                PHYSFS_sint64 br;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   236
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   237
                br = entry->compressed_size - finfo->compressed_position;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   238
                if (br > 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   239
                {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   240
                    if (br > ZIP_READBUFSIZE)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   241
                        br = ZIP_READBUFSIZE;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   242
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   243
                    br = io->read(io, finfo->buffer, (PHYSFS_uint64) br);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   244
                    if (br <= 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   245
                        break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   246
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   247
                    finfo->compressed_position += (PHYSFS_uint32) br;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   248
                    finfo->stream.next_in = finfo->buffer;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   249
                    finfo->stream.avail_in = (PHYSFS_uint32) br;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   250
                } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   251
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   252
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   253
            rc = zlib_err(inflate(&finfo->stream, Z_SYNC_FLUSH));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   254
            retval += (finfo->stream.total_out - before);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   255
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   256
            if (rc != Z_OK)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   257
                break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   258
        } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   259
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   260
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   261
    if (retval > 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   262
        finfo->uncompressed_position += (PHYSFS_uint32) retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   263
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   264
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   265
} /* ZIP_read */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   266
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   267
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   268
static PHYSFS_sint64 ZIP_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   269
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   270
    BAIL_MACRO(PHYSFS_ERR_READ_ONLY, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   271
} /* ZIP_write */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   272
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   273
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   274
static PHYSFS_sint64 ZIP_tell(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   275
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   276
    return ((ZIPfileinfo *) io->opaque)->uncompressed_position;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   277
} /* ZIP_tell */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   278
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   279
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   280
static int ZIP_seek(PHYSFS_Io *_io, PHYSFS_uint64 offset)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   281
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   282
    ZIPfileinfo *finfo = (ZIPfileinfo *) _io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   283
    ZIPentry *entry = finfo->entry;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   284
    PHYSFS_Io *io = finfo->io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   285
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   286
    BAIL_IF_MACRO(offset > entry->uncompressed_size, PHYSFS_ERR_PAST_EOF, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   287
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   288
    if (entry->compression_method == COMPMETH_NONE)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   289
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   290
        const PHYSFS_sint64 newpos = offset + entry->offset;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   291
        BAIL_IF_MACRO(!io->seek(io, newpos), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   292
        finfo->uncompressed_position = (PHYSFS_uint32) offset;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   293
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   294
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   295
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   296
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   297
        /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   298
         * If seeking backwards, we need to redecode the file
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   299
         *  from the start and throw away the compressed bits until we hit
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   300
         *  the offset we need. If seeking forward, we still need to
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   301
         *  decode, but we don't rewind first.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   302
         */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   303
        if (offset < finfo->uncompressed_position)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   304
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   305
            /* we do a copy so state is sane if inflateInit2() fails. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   306
            z_stream str;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   307
            initializeZStream(&str);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   308
            if (zlib_err(inflateInit2(&str, -MAX_WBITS)) != Z_OK)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   309
                return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   310
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   311
            if (!io->seek(io, entry->offset))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   312
                return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   313
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   314
            inflateEnd(&finfo->stream);
12217
ea891871f481 merge in patch used in arch/fedora - fixes a lazy physfs memcpy that was breaking upstream's stricter checking
nemo
parents: 10017
diff changeset
   315
            inflateCopy(&finfo->stream, &str);
ea891871f481 merge in patch used in arch/fedora - fixes a lazy physfs memcpy that was breaking upstream's stricter checking
nemo
parents: 10017
diff changeset
   316
            inflateEnd(&str);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   317
            finfo->uncompressed_position = finfo->compressed_position = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   318
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   319
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   320
        while (finfo->uncompressed_position != offset)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   321
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   322
            PHYSFS_uint8 buf[512];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   323
            PHYSFS_uint32 maxread;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   324
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   325
            maxread = (PHYSFS_uint32) (offset - finfo->uncompressed_position);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   326
            if (maxread > sizeof (buf))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   327
                maxread = sizeof (buf);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   328
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   329
            if (ZIP_read(_io, buf, maxread) != maxread)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   330
                return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   331
        } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   332
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   333
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   334
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   335
} /* ZIP_seek */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   336
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   337
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   338
static PHYSFS_sint64 ZIP_length(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   339
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   340
    const ZIPfileinfo *finfo = (ZIPfileinfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   341
    return (PHYSFS_sint64) finfo->entry->uncompressed_size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   342
} /* ZIP_length */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   343
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   344
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   345
static PHYSFS_Io *zip_get_io(PHYSFS_Io *io, ZIPinfo *inf, ZIPentry *entry);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   346
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   347
static PHYSFS_Io *ZIP_duplicate(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   348
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   349
    ZIPfileinfo *origfinfo = (ZIPfileinfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   350
    PHYSFS_Io *retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   351
    ZIPfileinfo *finfo = (ZIPfileinfo *) allocator.Malloc(sizeof (ZIPfileinfo));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   352
    GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   353
    GOTO_IF_MACRO(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   354
    memset(finfo, '\0', sizeof (*finfo));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   355
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   356
    finfo->entry = origfinfo->entry;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   357
    finfo->io = zip_get_io(origfinfo->io, NULL, finfo->entry);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   358
    GOTO_IF_MACRO(!finfo->io, ERRPASS, failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   359
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   360
    if (finfo->entry->compression_method != COMPMETH_NONE)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   361
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   362
        finfo->buffer = (PHYSFS_uint8 *) allocator.Malloc(ZIP_READBUFSIZE);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   363
        GOTO_IF_MACRO(!finfo->buffer, PHYSFS_ERR_OUT_OF_MEMORY, failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   364
        if (zlib_err(inflateInit2(&finfo->stream, -MAX_WBITS)) != Z_OK)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   365
            goto failed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   366
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   367
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   368
    memcpy(retval, io, sizeof (PHYSFS_Io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   369
    retval->opaque = finfo;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   370
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   371
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   372
failed:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   373
    if (finfo != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   374
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   375
        if (finfo->io != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   376
            finfo->io->destroy(finfo->io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   377
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   378
        if (finfo->buffer != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   379
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   380
            allocator.Free(finfo->buffer);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   381
            inflateEnd(&finfo->stream);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   382
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   383
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   384
        allocator.Free(finfo);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   385
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   386
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   387
    if (retval != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   388
        allocator.Free(retval);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   389
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   390
    return NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   391
} /* ZIP_duplicate */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   392
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   393
static int ZIP_flush(PHYSFS_Io *io) { return 1;  /* no write support. */ }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   394
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   395
static void ZIP_destroy(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   396
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   397
    ZIPfileinfo *finfo = (ZIPfileinfo *) io->opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   398
    finfo->io->destroy(finfo->io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   399
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   400
    if (finfo->entry->compression_method != COMPMETH_NONE)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   401
        inflateEnd(&finfo->stream);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   402
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   403
    if (finfo->buffer != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   404
        allocator.Free(finfo->buffer);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   405
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   406
    allocator.Free(finfo);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   407
    allocator.Free(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   408
} /* ZIP_destroy */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   409
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   410
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   411
static const PHYSFS_Io ZIP_Io =
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   412
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   413
    CURRENT_PHYSFS_IO_API_VERSION, NULL,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   414
    ZIP_read,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   415
    ZIP_write,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   416
    ZIP_seek,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   417
    ZIP_tell,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   418
    ZIP_length,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   419
    ZIP_duplicate,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   420
    ZIP_flush,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   421
    ZIP_destroy
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   422
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   423
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   424
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   425
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   426
static PHYSFS_sint64 zip_find_end_of_central_dir(PHYSFS_Io *io, PHYSFS_sint64 *len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   427
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   428
    PHYSFS_uint8 buf[256];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   429
    PHYSFS_uint8 extra[4] = { 0, 0, 0, 0 };
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   430
    PHYSFS_sint32 i = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   431
    PHYSFS_sint64 filelen;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   432
    PHYSFS_sint64 filepos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   433
    PHYSFS_sint32 maxread;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   434
    PHYSFS_sint32 totalread = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   435
    int found = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   436
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   437
    filelen = io->length(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   438
    BAIL_IF_MACRO(filelen == -1, ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   439
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   440
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   441
     * Jump to the end of the file and start reading backwards.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   442
     *  The last thing in the file is the zipfile comment, which is variable
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   443
     *  length, and the field that specifies its size is before it in the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   444
     *  file (argh!)...this means that we need to scan backwards until we
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   445
     *  hit the end-of-central-dir signature. We can then sanity check that
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   446
     *  the comment was as big as it should be to make sure we're in the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   447
     *  right place. The comment length field is 16 bits, so we can stop
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   448
     *  searching for that signature after a little more than 64k at most,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   449
     *  and call it a corrupted zipfile.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   450
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   451
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   452
    if (sizeof (buf) < filelen)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   453
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   454
        filepos = filelen - sizeof (buf);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   455
        maxread = sizeof (buf);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   456
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   457
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   458
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   459
        filepos = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   460
        maxread = (PHYSFS_uint32) filelen;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   461
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   462
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   463
    while ((totalread < filelen) && (totalread < 65557))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   464
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   465
        BAIL_IF_MACRO(!io->seek(io, filepos), ERRPASS, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   466
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   467
        /* make sure we catch a signature between buffers. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   468
        if (totalread != 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   469
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   470
            if (!__PHYSFS_readAll(io, buf, maxread - 4))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   471
                return -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   472
            memcpy(&buf[maxread - 4], &extra, sizeof (extra));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   473
            totalread += maxread - 4;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   474
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   475
        else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   476
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   477
            if (!__PHYSFS_readAll(io, buf, maxread))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   478
                return -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   479
            totalread += maxread;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   480
        } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   481
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   482
        memcpy(&extra, buf, sizeof (extra));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   483
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   484
        for (i = maxread - 4; i > 0; i--)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   485
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   486
            if ((buf[i + 0] == 0x50) &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   487
                (buf[i + 1] == 0x4B) &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   488
                (buf[i + 2] == 0x05) &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   489
                (buf[i + 3] == 0x06) )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   490
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   491
                found = 1;  /* that's the signature! */
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8524
diff changeset
   492
                break;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   493
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   494
        } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   495
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   496
        if (found)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   497
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   498
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   499
        filepos -= (maxread - 4);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   500
        if (filepos < 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   501
            filepos = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   502
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   503
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   504
    BAIL_IF_MACRO(!found, PHYSFS_ERR_UNSUPPORTED, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   505
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   506
    if (len != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   507
        *len = filelen;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   508
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   509
    return (filepos + i);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   510
} /* zip_find_end_of_central_dir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   511
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   512
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   513
static int isZip(PHYSFS_Io *io)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   514
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   515
    PHYSFS_uint32 sig = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   516
    int retval = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   517
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   518
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   519
     * The first thing in a zip file might be the signature of the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   520
     *  first local file record, so it makes for a quick determination.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   521
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   522
    if (readui32(io, &sig))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   523
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   524
        retval = (sig == ZIP_LOCAL_FILE_SIG);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   525
        if (!retval)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   526
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   527
            /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   528
             * No sig...might be a ZIP with data at the start
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   529
             *  (a self-extracting executable, etc), so we'll have to do
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   530
             *  it the hard way...
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   531
             */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   532
            retval = (zip_find_end_of_central_dir(io, NULL) != -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   533
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   534
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   535
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   536
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   537
} /* isZip */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   538
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   539
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   540
static void zip_free_entries(ZIPentry *entries, PHYSFS_uint64 max)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   541
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   542
    PHYSFS_uint64 i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   543
    for (i = 0; i < max; i++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   544
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   545
        ZIPentry *entry = &entries[i];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   546
        if (entry->name != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   547
            allocator.Free(entry->name);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   548
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   549
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   550
    allocator.Free(entries);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   551
} /* zip_free_entries */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   552
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   553
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   554
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   555
 * This will find the ZIPentry associated with a path in platform-independent
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8524
diff changeset
   556
 *  notation. Directories don't have ZIPentries associated with them, but
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   557
 *  (*isDir) will be set to non-zero if a dir was hit.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   558
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   559
static ZIPentry *zip_find_entry(const ZIPinfo *info, const char *path,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   560
                                int *isDir)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   561
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   562
    ZIPentry *a = info->entries;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   563
    PHYSFS_sint32 pathlen = (PHYSFS_sint32) strlen(path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   564
    PHYSFS_sint64 lo = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   565
    PHYSFS_sint64 hi = (PHYSFS_sint64) (info->entryCount - 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   566
    PHYSFS_sint64 middle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   567
    const char *thispath = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   568
    int rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   569
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   570
    while (lo <= hi)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   571
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   572
        middle = lo + ((hi - lo) / 2);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   573
        thispath = a[middle].name;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   574
        rc = strncmp(path, thispath, pathlen);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   575
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   576
        if (rc > 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   577
            lo = middle + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   578
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   579
        else if (rc < 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   580
            hi = middle - 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   581
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   582
        else /* substring match...might be dir or entry or nothing. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   583
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   584
            if (isDir != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   585
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   586
                *isDir = (thispath[pathlen] == '/');
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   587
                if (*isDir)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   588
                    return NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   589
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   590
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   591
            if (thispath[pathlen] == '\0') /* found entry? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   592
                return &a[middle];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   593
            /* adjust search params, try again. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   594
            else if (thispath[pathlen] > '/')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   595
                hi = middle - 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   596
            else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   597
                lo = middle + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   598
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   599
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   600
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   601
    if (isDir != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   602
        *isDir = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   603
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   604
    BAIL_MACRO(PHYSFS_ERR_NO_SUCH_PATH, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   605
} /* zip_find_entry */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   606
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   607
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   608
/* Convert paths from old, buggy DOS zippers... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   609
static void zip_convert_dos_path(ZIPentry *entry, char *path)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   610
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   611
    PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((entry->version >> 8) & 0xFF);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   612
    if (hosttype == 0)  /* FS_FAT_ */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   613
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   614
        while (*path)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   615
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   616
            if (*path == '\\')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   617
                *path = '/';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   618
            path++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   619
        } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   620
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   621
} /* zip_convert_dos_path */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   622
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   623
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   624
static void zip_expand_symlink_path(char *path)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   625
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   626
    char *ptr = path;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   627
    char *prevptr = path;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   628
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   629
    while (1)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   630
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   631
        ptr = strchr(ptr, '/');
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   632
        if (ptr == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   633
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   634
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   635
        if (*(ptr + 1) == '.')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   636
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   637
            if (*(ptr + 2) == '/')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   638
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   639
                /* current dir in middle of string: ditch it. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   640
                memmove(ptr, ptr + 2, strlen(ptr + 2) + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   641
            } /* else if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   642
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   643
            else if (*(ptr + 2) == '\0')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   644
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   645
                /* current dir at end of string: ditch it. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   646
                *ptr = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   647
            } /* else if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   648
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   649
            else if (*(ptr + 2) == '.')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   650
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   651
                if (*(ptr + 3) == '/')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   652
                {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   653
                    /* parent dir in middle: move back one, if possible. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   654
                    memmove(prevptr, ptr + 4, strlen(ptr + 4) + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   655
                    ptr = prevptr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   656
                    while (prevptr != path)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   657
                    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   658
                        prevptr--;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   659
                        if (*prevptr == '/')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   660
                        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   661
                            prevptr++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   662
                            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   663
                        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   664
                    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   665
                } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   666
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   667
                if (*(ptr + 3) == '\0')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   668
                {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   669
                    /* parent dir at end: move back one, if possible. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   670
                    *prevptr = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   671
                } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   672
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   673
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   674
        else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   675
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   676
            prevptr = ptr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   677
            ptr++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   678
        } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   679
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   680
} /* zip_expand_symlink_path */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   681
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   682
/* (forward reference: zip_follow_symlink and zip_resolve call each other.) */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   683
static int zip_resolve(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   684
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   685
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   686
 * Look for the entry named by (path). If it exists, resolve it, and return
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   687
 *  a pointer to that entry. If it's another symlink, keep resolving until you
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   688
 *  hit a real file and then return a pointer to the final non-symlink entry.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   689
 *  If there's a problem, return NULL.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   690
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   691
static ZIPentry *zip_follow_symlink(PHYSFS_Io *io, ZIPinfo *info, char *path)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   692
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   693
    ZIPentry *entry;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   694
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   695
    zip_expand_symlink_path(path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   696
    entry = zip_find_entry(info, path, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   697
    if (entry != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   698
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   699
        if (!zip_resolve(io, info, entry))  /* recursive! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   700
            entry = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   701
        else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   702
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   703
            if (entry->symlink != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   704
                entry = entry->symlink;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   705
        } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   706
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   707
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   708
    return entry;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   709
} /* zip_follow_symlink */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   710
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   711
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   712
static int zip_resolve_symlink(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   713
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   714
    const PHYSFS_uint64 size = entry->uncompressed_size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   715
    char *path = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   716
    int rc = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   717
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   718
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   719
     * We've already parsed the local file header of the symlink at this
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   720
     *  point. Now we need to read the actual link from the file data and
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   721
     *  follow it.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   722
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   723
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   724
    BAIL_IF_MACRO(!io->seek(io, entry->offset), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   725
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   726
    path = (char *) __PHYSFS_smallAlloc(size + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   727
    BAIL_IF_MACRO(!path, PHYSFS_ERR_OUT_OF_MEMORY, 0);
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8524
diff changeset
   728
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   729
    if (entry->compression_method == COMPMETH_NONE)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   730
        rc = __PHYSFS_readAll(io, path, size);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   731
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   732
    else  /* symlink target path is compressed... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   733
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   734
        z_stream stream;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   735
        const PHYSFS_uint64 complen = entry->compressed_size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   736
        PHYSFS_uint8 *compressed = (PHYSFS_uint8*) __PHYSFS_smallAlloc(complen);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   737
        if (compressed != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   738
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   739
            if (__PHYSFS_readAll(io, compressed, complen))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   740
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   741
                initializeZStream(&stream);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   742
                stream.next_in = compressed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   743
                stream.avail_in = complen;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   744
                stream.next_out = (unsigned char *) path;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   745
                stream.avail_out = size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   746
                if (zlib_err(inflateInit2(&stream, -MAX_WBITS)) == Z_OK)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   747
                {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   748
                    rc = zlib_err(inflate(&stream, Z_FINISH));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   749
                    inflateEnd(&stream);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   750
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   751
                    /* both are acceptable outcomes... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   752
                    rc = ((rc == Z_OK) || (rc == Z_STREAM_END));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   753
                } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   754
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   755
            __PHYSFS_smallFree(compressed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   756
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   757
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   758
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   759
    if (rc)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   760
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   761
        path[entry->uncompressed_size] = '\0';    /* null-terminate it. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   762
        zip_convert_dos_path(entry, path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   763
        entry->symlink = zip_follow_symlink(io, info, path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   764
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   765
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   766
    __PHYSFS_smallFree(path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   767
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   768
    return (entry->symlink != NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   769
} /* zip_resolve_symlink */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   770
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   771
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   772
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   773
 * Parse the local file header of an entry, and update entry->offset.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   774
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   775
static int zip_parse_local(PHYSFS_Io *io, ZIPentry *entry)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   776
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   777
    PHYSFS_uint32 ui32;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   778
    PHYSFS_uint16 ui16;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   779
    PHYSFS_uint16 fnamelen;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   780
    PHYSFS_uint16 extralen;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   781
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   782
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   783
     * crc and (un)compressed_size are always zero if this is a "JAR"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   784
     *  archive created with Sun's Java tools, apparently. We only
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   785
     *  consider this archive corrupted if those entries don't match and
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   786
     *  aren't zero. That seems to work well.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   787
     * We also ignore a mismatch if the value is 0xFFFFFFFF here, since it's
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   788
     *  possible that's a Zip64 thing.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   789
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   790
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   791
    BAIL_IF_MACRO(!io->seek(io, entry->offset), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   792
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   793
    BAIL_IF_MACRO(ui32 != ZIP_LOCAL_FILE_SIG, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   794
    BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   795
    BAIL_IF_MACRO(ui16 != entry->version_needed, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   796
    BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);  /* general bits. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   797
    BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   798
    BAIL_IF_MACRO(ui16 != entry->compression_method, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   799
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);  /* date/time */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   800
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   801
    BAIL_IF_MACRO(ui32 && (ui32 != entry->crc), PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   802
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   803
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   804
    BAIL_IF_MACRO(ui32 && (ui32 != 0xFFFFFFFF) &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   805
                  (ui32 != entry->compressed_size), PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   806
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   807
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   808
    BAIL_IF_MACRO(ui32 && (ui32 != 0xFFFFFFFF) &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   809
                 (ui32 != entry->uncompressed_size), PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   810
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   811
    BAIL_IF_MACRO(!readui16(io, &fnamelen), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   812
    BAIL_IF_MACRO(!readui16(io, &extralen), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   813
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   814
    entry->offset += fnamelen + extralen + 30;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   815
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   816
} /* zip_parse_local */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   817
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   818
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   819
static int zip_resolve(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   820
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   821
    int retval = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   822
    ZipResolveType resolve_type = entry->resolved;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   823
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   824
    /* Don't bother if we've failed to resolve this entry before. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   825
    BAIL_IF_MACRO(resolve_type == ZIP_BROKEN_FILE, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   826
    BAIL_IF_MACRO(resolve_type == ZIP_BROKEN_SYMLINK, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   827
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   828
    /* uhoh...infinite symlink loop! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   829
    BAIL_IF_MACRO(resolve_type == ZIP_RESOLVING, PHYSFS_ERR_SYMLINK_LOOP, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   830
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   831
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   832
     * We fix up the offset to point to the actual data on the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   833
     *  first open, since we don't want to seek across the whole file on
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   834
     *  archive open (can be SLOW on large, CD-stored files), but we
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   835
     *  need to check the local file header...not just for corruption,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   836
     *  but since it stores offset info the central directory does not.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   837
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   838
    if (resolve_type != ZIP_RESOLVED)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   839
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   840
        entry->resolved = ZIP_RESOLVING;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   841
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   842
        retval = zip_parse_local(io, entry);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   843
        if (retval)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   844
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   845
            /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   846
             * If it's a symlink, find the original file. This will cause
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   847
             *  resolution of other entries (other symlinks and, eventually,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   848
             *  the real file) if all goes well.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   849
             */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   850
            if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   851
                retval = zip_resolve_symlink(io, info, entry);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   852
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   853
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   854
        if (resolve_type == ZIP_UNRESOLVED_SYMLINK)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   855
            entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_SYMLINK);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   856
        else if (resolve_type == ZIP_UNRESOLVED_FILE)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   857
            entry->resolved = ((retval) ? ZIP_RESOLVED : ZIP_BROKEN_FILE);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   858
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   859
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   860
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   861
} /* zip_resolve */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   862
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   863
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   864
static int zip_version_does_symlinks(PHYSFS_uint32 version)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   865
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   866
    int retval = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   867
    PHYSFS_uint8 hosttype = (PHYSFS_uint8) ((version >> 8) & 0xFF);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   868
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   869
    switch (hosttype)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   870
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   871
            /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   872
             * These are the platforms that can NOT build an archive with
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   873
             *  symlinks, according to the Info-ZIP project.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   874
             */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   875
        case 0:  /* FS_FAT_  */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   876
        case 1:  /* AMIGA_   */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   877
        case 2:  /* VMS_     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   878
        case 4:  /* VM_CSM_  */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   879
        case 6:  /* FS_HPFS_ */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   880
        case 11: /* FS_NTFS_ */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   881
        case 14: /* FS_VFAT_ */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   882
        case 13: /* ACORN_   */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   883
        case 15: /* MVS_     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   884
        case 18: /* THEOS_   */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   885
            break;  /* do nothing. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   886
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   887
        default:  /* assume the rest to be unix-like. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   888
            retval = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   889
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   890
    } /* switch */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   891
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   892
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   893
} /* zip_version_does_symlinks */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   894
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   895
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   896
static int zip_entry_is_symlink(const ZIPentry *entry)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   897
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   898
    return ((entry->resolved == ZIP_UNRESOLVED_SYMLINK) ||
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   899
            (entry->resolved == ZIP_BROKEN_SYMLINK) ||
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   900
            (entry->symlink));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   901
} /* zip_entry_is_symlink */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   902
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   903
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   904
static int zip_has_symlink_attr(ZIPentry *entry, PHYSFS_uint32 extern_attr)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   905
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   906
    PHYSFS_uint16 xattr = ((extern_attr >> 16) & 0xFFFF);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   907
    return ( (zip_version_does_symlinks(entry->version)) &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   908
             (entry->uncompressed_size > 0) &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   909
             ((xattr & UNIX_FILETYPE_MASK) == UNIX_FILETYPE_SYMLINK) );
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   910
} /* zip_has_symlink_attr */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   911
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   912
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   913
static PHYSFS_sint64 zip_dos_time_to_physfs_time(PHYSFS_uint32 dostime)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   914
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   915
    PHYSFS_uint32 dosdate;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   916
    struct tm unixtime;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   917
    memset(&unixtime, '\0', sizeof (unixtime));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   918
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   919
    dosdate = (PHYSFS_uint32) ((dostime >> 16) & 0xFFFF);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   920
    dostime &= 0xFFFF;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   921
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   922
    /* dissect date */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   923
    unixtime.tm_year = ((dosdate >> 9) & 0x7F) + 80;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   924
    unixtime.tm_mon  = ((dosdate >> 5) & 0x0F) - 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   925
    unixtime.tm_mday = ((dosdate     ) & 0x1F);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   926
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   927
    /* dissect time */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   928
    unixtime.tm_hour = ((dostime >> 11) & 0x1F);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   929
    unixtime.tm_min  = ((dostime >>  5) & 0x3F);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   930
    unixtime.tm_sec  = ((dostime <<  1) & 0x3E);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   931
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   932
    /* let mktime calculate daylight savings time. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   933
    unixtime.tm_isdst = -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   934
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   935
    return ((PHYSFS_sint64) mktime(&unixtime));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   936
} /* zip_dos_time_to_physfs_time */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   937
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   938
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   939
static int zip_load_entry(PHYSFS_Io *io, const int zip64, ZIPentry *entry,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   940
                          PHYSFS_uint64 ofs_fixup)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   941
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   942
    PHYSFS_uint16 fnamelen, extralen, commentlen;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   943
    PHYSFS_uint32 external_attr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   944
    PHYSFS_uint32 starting_disk;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   945
    PHYSFS_uint64 offset;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   946
    PHYSFS_uint16 ui16;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   947
    PHYSFS_uint32 ui32;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   948
    PHYSFS_sint64 si64;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   949
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   950
    /* sanity check with central directory signature... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   951
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   952
    BAIL_IF_MACRO(ui32 != ZIP_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   953
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   954
    /* Get the pertinent parts of the record... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   955
    BAIL_IF_MACRO(!readui16(io, &entry->version), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   956
    BAIL_IF_MACRO(!readui16(io, &entry->version_needed), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   957
    BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);  /* general bits */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   958
    BAIL_IF_MACRO(!readui16(io, &entry->compression_method), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   959
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   960
    entry->last_mod_time = zip_dos_time_to_physfs_time(ui32);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   961
    BAIL_IF_MACRO(!readui32(io, &entry->crc), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   962
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   963
    entry->compressed_size = (PHYSFS_uint64) ui32;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   964
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   965
    entry->uncompressed_size = (PHYSFS_uint64) ui32;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   966
    BAIL_IF_MACRO(!readui16(io, &fnamelen), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   967
    BAIL_IF_MACRO(!readui16(io, &extralen), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   968
    BAIL_IF_MACRO(!readui16(io, &commentlen), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   969
    BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   970
    starting_disk = (PHYSFS_uint32) ui16;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   971
    BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);  /* internal file attribs */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   972
    BAIL_IF_MACRO(!readui32(io, &external_attr), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   973
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   974
    offset = (PHYSFS_uint64) ui32;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   975
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   976
    entry->symlink = NULL;  /* will be resolved later, if necessary. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   977
    entry->resolved = (zip_has_symlink_attr(entry, external_attr)) ?
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   978
                            ZIP_UNRESOLVED_SYMLINK : ZIP_UNRESOLVED_FILE;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   979
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   980
    entry->name = (char *) allocator.Malloc(fnamelen + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   981
    BAIL_IF_MACRO(entry->name == NULL, PHYSFS_ERR_OUT_OF_MEMORY, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   982
    if (!__PHYSFS_readAll(io, entry->name, fnamelen))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   983
        goto zip_load_entry_puked;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   984
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   985
    entry->name[fnamelen] = '\0';  /* null-terminate the filename. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   986
    zip_convert_dos_path(entry, entry->name);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   987
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   988
    si64 = io->tell(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   989
    if (si64 == -1)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   990
        goto zip_load_entry_puked;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   991
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   992
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   993
     * The actual sizes didn't fit in 32-bits; look for the Zip64
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   994
     *  extended information extra field...
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   995
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   996
    if ( (zip64) &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   997
         ((offset == 0xFFFFFFFF) ||
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   998
          (starting_disk == 0xFFFFFFFF) ||
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   999
          (entry->compressed_size == 0xFFFFFFFF) ||
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1000
          (entry->uncompressed_size == 0xFFFFFFFF)) )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1001
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1002
        int found = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1003
        PHYSFS_uint16 sig, len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1004
        while (extralen > 4)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1005
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1006
            if (!readui16(io, &sig))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1007
                goto zip_load_entry_puked;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1008
            else if (!readui16(io, &len))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1009
                goto zip_load_entry_puked;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1010
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1011
            si64 += 4 + len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1012
            extralen -= 4 + len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1013
            if (sig != ZIP64_EXTENDED_INFO_EXTRA_FIELD_SIG)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1014
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1015
                if (!io->seek(io, si64))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1016
                    goto zip_load_entry_puked;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1017
                continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1018
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1019
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1020
            found = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1021
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1022
        } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1023
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1024
        GOTO_IF_MACRO(!found, PHYSFS_ERR_CORRUPT, zip_load_entry_puked);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1025
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1026
        if (entry->uncompressed_size == 0xFFFFFFFF)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1027
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1028
            GOTO_IF_MACRO(len < 8, PHYSFS_ERR_CORRUPT, zip_load_entry_puked);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1029
            if (!readui64(io, &entry->uncompressed_size))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1030
                goto zip_load_entry_puked;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1031
            len -= 8;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1032
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1033
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1034
        if (entry->compressed_size == 0xFFFFFFFF)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1035
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1036
            GOTO_IF_MACRO(len < 8, PHYSFS_ERR_CORRUPT, zip_load_entry_puked);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1037
            if (!readui64(io, &entry->compressed_size))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1038
                goto zip_load_entry_puked;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1039
            len -= 8;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1040
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1041
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1042
        if (offset == 0xFFFFFFFF)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1043
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1044
            GOTO_IF_MACRO(len < 8, PHYSFS_ERR_CORRUPT, zip_load_entry_puked);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1045
            if (!readui64(io, &offset))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1046
                goto zip_load_entry_puked;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1047
            len -= 8;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1048
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1049
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1050
        if (starting_disk == 0xFFFFFFFF)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1051
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1052
            GOTO_IF_MACRO(len < 8, PHYSFS_ERR_CORRUPT, zip_load_entry_puked);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1053
            if (!readui32(io, &starting_disk))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1054
                goto zip_load_entry_puked;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1055
            len -= 4;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1056
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1057
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1058
        GOTO_IF_MACRO(len != 0, PHYSFS_ERR_CORRUPT, zip_load_entry_puked);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1059
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1060
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1061
    GOTO_IF_MACRO(starting_disk != 0, PHYSFS_ERR_CORRUPT, zip_load_entry_puked);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1062
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1063
    entry->offset = offset + ofs_fixup;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1064
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1065
    /* seek to the start of the next entry in the central directory... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1066
    if (!io->seek(io, si64 + extralen + commentlen))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1067
        goto zip_load_entry_puked;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1068
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1069
    return 1;  /* success. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1070
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1071
zip_load_entry_puked:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1072
    allocator.Free(entry->name);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1073
    return 0;  /* failure. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1074
} /* zip_load_entry */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1075
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1076
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1077
static int zip_entry_cmp(void *_a, size_t one, size_t two)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1078
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1079
    if (one != two)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1080
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1081
        const ZIPentry *a = (const ZIPentry *) _a;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1082
        return strcmp(a[one].name, a[two].name);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1083
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1084
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1085
    return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1086
} /* zip_entry_cmp */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1087
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1088
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1089
static void zip_entry_swap(void *_a, size_t one, size_t two)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1090
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1091
    if (one != two)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1092
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1093
        ZIPentry tmp;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1094
        ZIPentry *first = &(((ZIPentry *) _a)[one]);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1095
        ZIPentry *second = &(((ZIPentry *) _a)[two]);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1096
        memcpy(&tmp, first, sizeof (ZIPentry));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1097
        memcpy(first, second, sizeof (ZIPentry));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1098
        memcpy(second, &tmp, sizeof (ZIPentry));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1099
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1100
} /* zip_entry_swap */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1101
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1102
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1103
static int zip_load_entries(PHYSFS_Io *io, ZIPinfo *info,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1104
                            const PHYSFS_uint64 data_ofs,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1105
                            const PHYSFS_uint64 central_ofs)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1106
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1107
    const PHYSFS_uint64 max = info->entryCount;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1108
    const int zip64 = info->zip64;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1109
    PHYSFS_uint64 i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1110
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1111
    BAIL_IF_MACRO(!io->seek(io, central_ofs), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1112
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1113
    info->entries = (ZIPentry *) allocator.Malloc(sizeof (ZIPentry) * max);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1114
    BAIL_IF_MACRO(!info->entries, PHYSFS_ERR_OUT_OF_MEMORY, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1115
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1116
    for (i = 0; i < max; i++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1117
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1118
        if (!zip_load_entry(io, zip64, &info->entries[i], data_ofs))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1119
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1120
            zip_free_entries(info->entries, i);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1121
            return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1122
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1123
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1124
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1125
    __PHYSFS_sort(info->entries, (size_t) max, zip_entry_cmp, zip_entry_swap);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1126
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1127
} /* zip_load_entries */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1128
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1129
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1130
static PHYSFS_sint64 zip64_find_end_of_central_dir(PHYSFS_Io *io,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1131
                                                   PHYSFS_sint64 _pos,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1132
                                                   PHYSFS_uint64 offset)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1133
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1134
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1135
     * Naturally, the offset is useless to us; it is the offset from the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1136
     *  start of file, which is meaningless if we've appended this .zip to
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1137
     *  a self-extracting .exe. We need to find this on our own. It should
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1138
     *  be directly before the locator record, but the record in question,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1139
     *  like the original end-of-central-directory record, ends with a
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1140
     *  variable-length field. Unlike the original, which has to store the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1141
     *  size of that variable-length field in a 16-bit int and thus has to be
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1142
     *  within 64k, the new one gets 64-bits.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1143
     *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1144
     * Fortunately, the only currently-specified record for that variable
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1145
     *  length block is some weird proprietary thing that deals with EBCDIC
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1146
     *  and tape backups or something. So we don't seek far.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1147
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1148
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1149
    PHYSFS_uint32 ui32;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1150
    const PHYSFS_uint64 pos = (PHYSFS_uint64) _pos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1151
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1152
    assert(_pos > 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1153
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1154
    /* Try offset specified in the Zip64 end of central directory locator. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1155
    /* This works if the entire PHYSFS_Io is the zip file. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1156
    BAIL_IF_MACRO(!io->seek(io, offset), ERRPASS, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1157
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1158
    if (ui32 == ZIP64_END_OF_CENTRAL_DIR_SIG)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1159
        return offset;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1160
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1161
    /* Try 56 bytes before the Zip64 end of central directory locator. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1162
    /* This works if the record isn't variable length and is version 1. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1163
    if (pos > 56)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1164
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1165
        BAIL_IF_MACRO(!io->seek(io, pos-56), ERRPASS, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1166
        BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1167
        if (ui32 == ZIP64_END_OF_CENTRAL_DIR_SIG)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1168
            return pos-56;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1169
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1170
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1171
    /* Try 84 bytes before the Zip64 end of central directory locator. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1172
    /* This works if the record isn't variable length and is version 2. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1173
    if (pos > 84)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1174
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1175
        BAIL_IF_MACRO(!io->seek(io, pos-84), ERRPASS, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1176
        BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1177
        if (ui32 == ZIP64_END_OF_CENTRAL_DIR_SIG)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1178
            return pos-84;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1179
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1180
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1181
    /* Ok, brute force: we know it's between (offset) and (pos) somewhere. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1182
    /*  Just try moving back at most 256k. Oh well. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1183
    if ((offset < pos) && (pos > 4))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1184
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1185
        /* we assume you can eat this stack if you handle Zip64 files. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1186
        PHYSFS_uint8 buf[256 * 1024];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1187
        PHYSFS_uint64 len = pos - offset;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1188
        PHYSFS_sint32 i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1189
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1190
        if (len > sizeof (buf))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1191
            len = sizeof (buf);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1192
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1193
        BAIL_IF_MACRO(!io->seek(io, pos - len), ERRPASS, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1194
        BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, len), ERRPASS, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1195
        for (i = (PHYSFS_sint32) (len - 4); i >= 0; i--)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1196
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1197
            if (buf[i] != 0x50)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1198
                continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1199
            if ( (buf[i+1] == 0x4b) &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1200
                 (buf[i+2] == 0x06) &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1201
                 (buf[i+3] == 0x06) )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1202
                return pos - (len - i);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1203
        } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1204
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1205
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1206
    BAIL_MACRO(PHYSFS_ERR_CORRUPT, -1);  /* didn't find it. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1207
} /* zip64_find_end_of_central_dir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1208
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1209
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1210
static int zip64_parse_end_of_central_dir(PHYSFS_Io *io, ZIPinfo *info,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1211
                                          PHYSFS_uint64 *data_start,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1212
                                          PHYSFS_uint64 *dir_ofs,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1213
                                          PHYSFS_sint64 pos)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1214
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1215
    PHYSFS_uint64 ui64;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1216
    PHYSFS_uint32 ui32;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1217
    PHYSFS_uint16 ui16;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1218
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1219
    /* We should be positioned right past the locator signature. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1220
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1221
    if ((pos < 0) || (!io->seek(io, pos)))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1222
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1223
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1224
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1225
    if (ui32 != ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIG)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1226
        return -1;  /* it's not a Zip64 archive. Not an error, though! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1227
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1228
    info->zip64 = 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1229
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1230
    /* number of the disk with the start of the central directory. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1231
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1232
    BAIL_IF_MACRO(ui32 != 0, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1233
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1234
    /* offset of Zip64 end of central directory record. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1235
    BAIL_IF_MACRO(!readui64(io, &ui64), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1236
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1237
    /* total number of disks */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1238
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1239
    BAIL_IF_MACRO(ui32 != 1, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1240
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1241
    pos = zip64_find_end_of_central_dir(io, pos, ui64);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1242
    if (pos < 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1243
        return 0;  /* oh well. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1244
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1245
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1246
     * For self-extracting archives, etc, there's crapola in the file
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1247
     *  before the zipfile records; we calculate how much data there is
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1248
     *  prepended by determining how far the zip64-end-of-central-directory
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1249
     *  offset is from where it is supposed to be...the difference in bytes
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1250
     *  is how much arbitrary data is at the start of the physical file.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1251
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1252
    assert(((PHYSFS_uint64) pos) >= ui64);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1253
    *data_start = ((PHYSFS_uint64) pos) - ui64;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1254
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1255
    BAIL_IF_MACRO(!io->seek(io, pos), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1256
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1257
    /* check signature again, just in case. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1258
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1259
    BAIL_IF_MACRO(ui32 != ZIP64_END_OF_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1260
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1261
    /* size of Zip64 end of central directory record. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1262
    BAIL_IF_MACRO(!readui64(io, &ui64), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1263
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1264
    /* version made by. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1265
    BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1266
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1267
    /* version needed to extract. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1268
    BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1269
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1270
    /* number of this disk. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1271
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1272
    BAIL_IF_MACRO(ui32 != 0, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1273
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1274
    /* number of disk with start of central directory record. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1275
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1276
    BAIL_IF_MACRO(ui32 != 0, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1277
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1278
    /* total number of entries in the central dir on this disk */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1279
    BAIL_IF_MACRO(!readui64(io, &ui64), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1280
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1281
    /* total number of entries in the central dir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1282
    BAIL_IF_MACRO(!readui64(io, &info->entryCount), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1283
    BAIL_IF_MACRO(ui64 != info->entryCount, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1284
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1285
    /* size of the central directory */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1286
    BAIL_IF_MACRO(!readui64(io, &ui64), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1287
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1288
    /* offset of central directory */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1289
    BAIL_IF_MACRO(!readui64(io, dir_ofs), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1290
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1291
    /* Since we know the difference, fix up the central dir offset... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1292
    *dir_ofs += *data_start;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1293
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1294
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1295
     * There are more fields here, for encryption and feature-specific things,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1296
     *  but we don't care about any of them at the moment.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1297
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1298
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1299
    return 1;  /* made it. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1300
} /* zip64_parse_end_of_central_dir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1301
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1302
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1303
static int zip_parse_end_of_central_dir(PHYSFS_Io *io, ZIPinfo *info,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1304
                                        PHYSFS_uint64 *data_start,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1305
                                        PHYSFS_uint64 *dir_ofs)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1306
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1307
    PHYSFS_uint16 entryCount16;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1308
    PHYSFS_uint32 offset32;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1309
    PHYSFS_uint32 ui32;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1310
    PHYSFS_uint16 ui16;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1311
    PHYSFS_sint64 len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1312
    PHYSFS_sint64 pos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1313
    int rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1314
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1315
    /* find the end-of-central-dir record, and seek to it. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1316
    pos = zip_find_end_of_central_dir(io, &len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1317
    BAIL_IF_MACRO(pos == -1, ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1318
    BAIL_IF_MACRO(!io->seek(io, pos), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1319
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1320
    /* check signature again, just in case. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1321
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1322
    BAIL_IF_MACRO(ui32 != ZIP_END_OF_CENTRAL_DIR_SIG, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1323
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1324
    /* Seek back to see if "Zip64 end of central directory locator" exists. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1325
    /* this record is 20 bytes before end-of-central-dir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1326
    rc = zip64_parse_end_of_central_dir(io, info, data_start, dir_ofs, pos-20);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1327
    BAIL_IF_MACRO(rc == 0, ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1328
    if (rc == 1)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1329
        return 1;  /* we're done here. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1330
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1331
    assert(rc == -1);  /* no error, just not a Zip64 archive. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1332
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1333
    /* Not Zip64? Seek back to where we were and keep processing. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1334
    BAIL_IF_MACRO(!io->seek(io, pos + 4), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1335
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1336
    /* number of this disk */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1337
    BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1338
    BAIL_IF_MACRO(ui16 != 0, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1339
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1340
    /* number of the disk with the start of the central directory */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1341
    BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1342
    BAIL_IF_MACRO(ui16 != 0, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1343
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1344
    /* total number of entries in the central dir on this disk */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1345
    BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1346
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1347
    /* total number of entries in the central dir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1348
    BAIL_IF_MACRO(!readui16(io, &entryCount16), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1349
    BAIL_IF_MACRO(ui16 != entryCount16, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1350
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1351
    info->entryCount = entryCount16;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1352
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1353
    /* size of the central directory */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1354
    BAIL_IF_MACRO(!readui32(io, &ui32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1355
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1356
    /* offset of central directory */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1357
    BAIL_IF_MACRO(!readui32(io, &offset32), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1358
    *dir_ofs = (PHYSFS_uint64) offset32;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1359
    BAIL_IF_MACRO(pos < (*dir_ofs + ui32), PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1360
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1361
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1362
     * For self-extracting archives, etc, there's crapola in the file
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1363
     *  before the zipfile records; we calculate how much data there is
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1364
     *  prepended by determining how far the central directory offset is
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1365
     *  from where it is supposed to be (start of end-of-central-dir minus
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1366
     *  sizeof central dir)...the difference in bytes is how much arbitrary
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1367
     *  data is at the start of the physical file.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1368
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1369
    *data_start = (PHYSFS_uint64) (pos - (*dir_ofs + ui32));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1370
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1371
    /* Now that we know the difference, fix up the central dir offset... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1372
    *dir_ofs += *data_start;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1373
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1374
    /* zipfile comment length */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1375
    BAIL_IF_MACRO(!readui16(io, &ui16), ERRPASS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1376
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1377
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1378
     * Make sure that the comment length matches to the end of file...
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1379
     *  If it doesn't, we're either in the wrong part of the file, or the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1380
     *  file is corrupted, but we give up either way.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1381
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1382
    BAIL_IF_MACRO((pos + 22 + ui16) != len, PHYSFS_ERR_CORRUPT, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1383
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1384
    return 1;  /* made it. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1385
} /* zip_parse_end_of_central_dir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1386
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1387
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1388
static void *ZIP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1389
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1390
    ZIPinfo *info = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1391
    PHYSFS_uint64 data_start;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1392
    PHYSFS_uint64 cent_dir_ofs;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1393
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1394
    assert(io != NULL);  /* shouldn't ever happen. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1395
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1396
    BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1397
    BAIL_IF_MACRO(!isZip(io), ERRPASS, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1398
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1399
    info = (ZIPinfo *) allocator.Malloc(sizeof (ZIPinfo));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1400
    BAIL_IF_MACRO(!info, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1401
    memset(info, '\0', sizeof (ZIPinfo));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1402
    info->io = io;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1403
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1404
    if (!zip_parse_end_of_central_dir(io, info, &data_start, &cent_dir_ofs))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1405
        goto ZIP_openarchive_failed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1406
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1407
    if (!zip_load_entries(io, info, data_start, cent_dir_ofs))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1408
        goto ZIP_openarchive_failed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1409
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1410
    return info;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1411
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1412
ZIP_openarchive_failed:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1413
    if (info != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1414
        allocator.Free(info);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1415
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1416
    return NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1417
} /* ZIP_openArchive */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1418
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1419
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1420
static PHYSFS_sint64 zip_find_start_of_dir(ZIPinfo *info, const char *path,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1421
                                            int stop_on_first_find)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1422
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1423
    PHYSFS_sint64 lo = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1424
    PHYSFS_sint64 hi = (PHYSFS_sint64) (info->entryCount - 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1425
    PHYSFS_sint64 middle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1426
    PHYSFS_uint32 dlen = (PHYSFS_uint32) strlen(path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1427
    PHYSFS_sint64 retval = -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1428
    const char *name;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1429
    int rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1430
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1431
    if (*path == '\0')  /* root dir? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1432
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1433
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1434
    if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1435
        dlen--;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1436
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1437
    while (lo <= hi)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1438
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1439
        middle = lo + ((hi - lo) / 2);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1440
        name = info->entries[middle].name;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1441
        rc = strncmp(path, name, dlen);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1442
        if (rc == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1443
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1444
            char ch = name[dlen];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1445
            if ('/' < ch) /* make sure this isn't just a substr match. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1446
                rc = -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1447
            else if ('/' > ch)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1448
                rc = 1;
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8524
diff changeset
  1449
            else
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1450
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1451
                if (stop_on_first_find) /* Just checking dir's existance? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1452
                    return middle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1453
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1454
                if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1455
                    return (middle + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1456
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1457
                /* there might be more entries earlier in the list. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1458
                retval = middle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1459
                hi = middle - 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1460
            } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1461
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1462
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1463
        if (rc > 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1464
            lo = middle + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1465
        else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1466
            hi = middle - 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1467
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1468
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1469
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1470
} /* zip_find_start_of_dir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1471
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1472
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1473
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1474
 * Moved to seperate function so we can use alloca then immediately throw
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1475
 *  away the allocated stack space...
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1476
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1477
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1478
                           const char *odir, const char *str, PHYSFS_sint32 ln)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1479
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1480
    char *newstr = __PHYSFS_smallAlloc(ln + 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1481
    if (newstr == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1482
        return;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1483
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1484
    memcpy(newstr, str, ln);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1485
    newstr[ln] = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1486
    cb(callbackdata, odir, newstr);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1487
    __PHYSFS_smallFree(newstr);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1488
} /* doEnumCallback */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1489
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1490
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1491
static void ZIP_enumerateFiles(PHYSFS_Dir *opaque, const char *dname,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1492
                               int omitSymLinks, PHYSFS_EnumFilesCallback cb,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1493
                               const char *origdir, void *callbackdata)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1494
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1495
    ZIPinfo *info = ((ZIPinfo *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1496
    PHYSFS_sint32 dlen, dlen_inc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1497
    PHYSFS_sint64 i, max;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1498
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1499
    i = zip_find_start_of_dir(info, dname, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1500
    if (i == -1)  /* no such directory. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1501
        return;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1502
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1503
    dlen = (PHYSFS_sint32) strlen(dname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1504
    if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1505
        dlen--;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1506
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1507
    dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1508
    max = (PHYSFS_sint64) info->entryCount;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1509
    while (i < max)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1510
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1511
        char *e = info->entries[i].name;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1512
        if ((dlen) && ((strncmp(e, dname, dlen) != 0) || (e[dlen] != '/')))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1513
            break;  /* past end of this dir; we're done. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1514
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1515
        if ((omitSymLinks) && (zip_entry_is_symlink(&info->entries[i])))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1516
            i++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1517
        else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1518
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1519
            char *add = e + dlen_inc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1520
            char *ptr = strchr(add, '/');
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1521
            PHYSFS_sint32 ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1522
            doEnumCallback(cb, callbackdata, origdir, add, ln);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1523
            ln += dlen_inc;  /* point past entry to children... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1524
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1525
            /* increment counter and skip children of subdirs... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1526
            while ((++i < max) && (ptr != NULL))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1527
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1528
                char *e_new = info->entries[i].name;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1529
                if ((strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1530
                    break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1531
            } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1532
        } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1533
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1534
} /* ZIP_enumerateFiles */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1535
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1536
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1537
static PHYSFS_Io *zip_get_io(PHYSFS_Io *io, ZIPinfo *inf, ZIPentry *entry)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1538
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1539
    int success;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1540
    PHYSFS_Io *retval = io->duplicate(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1541
    BAIL_IF_MACRO(!retval, ERRPASS, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1542
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1543
    /* !!! FIXME: if you open a dir here, it should bail ERR_NOT_A_FILE */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1544
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1545
    /* (inf) can be NULL if we already resolved. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1546
    success = (inf == NULL) || zip_resolve(retval, inf, entry);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1547
    if (success)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1548
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1549
        PHYSFS_sint64 offset;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1550
        offset = ((entry->symlink) ? entry->symlink->offset : entry->offset);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1551
        success = retval->seek(retval, offset);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1552
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1553
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1554
    if (!success)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1555
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1556
        retval->destroy(retval);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1557
        retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1558
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1559
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1560
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1561
} /* zip_get_io */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1562
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1563
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1564
static PHYSFS_Io *ZIP_openRead(PHYSFS_Dir *opaque, const char *fnm,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1565
                               int *fileExists)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1566
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1567
    PHYSFS_Io *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1568
    ZIPinfo *info = (ZIPinfo *) opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1569
    ZIPentry *entry = zip_find_entry(info, fnm, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1570
    ZIPfileinfo *finfo = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1571
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1572
    *fileExists = (entry != NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1573
    BAIL_IF_MACRO(!entry, ERRPASS, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1574
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1575
    retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1576
    GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1577
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1578
    finfo = (ZIPfileinfo *) allocator.Malloc(sizeof (ZIPfileinfo));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1579
    GOTO_IF_MACRO(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1580
    memset(finfo, '\0', sizeof (ZIPfileinfo));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1581
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1582
    finfo->io = zip_get_io(info->io, info, entry);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1583
    GOTO_IF_MACRO(!finfo->io, ERRPASS, ZIP_openRead_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1584
    finfo->entry = ((entry->symlink != NULL) ? entry->symlink : entry);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1585
    initializeZStream(&finfo->stream);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1586
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1587
    if (finfo->entry->compression_method != COMPMETH_NONE)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1588
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1589
        finfo->buffer = (PHYSFS_uint8 *) allocator.Malloc(ZIP_READBUFSIZE);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1590
        if (!finfo->buffer)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1591
            GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, ZIP_openRead_failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1592
        else if (zlib_err(inflateInit2(&finfo->stream, -MAX_WBITS)) != Z_OK)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1593
            goto ZIP_openRead_failed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1594
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1595
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1596
    memcpy(retval, &ZIP_Io, sizeof (PHYSFS_Io));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1597
    retval->opaque = finfo;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1598
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1599
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1600
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1601
ZIP_openRead_failed:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1602
    if (finfo != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1603
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1604
        if (finfo->io != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1605
            finfo->io->destroy(finfo->io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1606
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1607
        if (finfo->buffer != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1608
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1609
            allocator.Free(finfo->buffer);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1610
            inflateEnd(&finfo->stream);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1611
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1612
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1613
        allocator.Free(finfo);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1614
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1615
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1616
    if (retval != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1617
        allocator.Free(retval);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1618
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1619
    return NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1620
} /* ZIP_openRead */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1621
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1622
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1623
static PHYSFS_Io *ZIP_openWrite(PHYSFS_Dir *opaque, const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1624
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1625
    BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1626
} /* ZIP_openWrite */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1627
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1628
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1629
static PHYSFS_Io *ZIP_openAppend(PHYSFS_Dir *opaque, const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1630
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1631
    BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1632
} /* ZIP_openAppend */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1633
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1634
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1635
static void ZIP_closeArchive(PHYSFS_Dir *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1636
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1637
    ZIPinfo *zi = (ZIPinfo *) (opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1638
    zi->io->destroy(zi->io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1639
    zip_free_entries(zi->entries, zi->entryCount);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1640
    allocator.Free(zi);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1641
} /* ZIP_closeArchive */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1642
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1643
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1644
static int ZIP_remove(PHYSFS_Dir *opaque, const char *name)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1645
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1646
    BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1647
} /* ZIP_remove */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1648
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1649
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1650
static int ZIP_mkdir(PHYSFS_Dir *opaque, const char *name)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1651
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1652
    BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1653
} /* ZIP_mkdir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1654
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1655
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1656
static int ZIP_stat(PHYSFS_Dir *opaque, const char *filename, int *exists,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1657
                    PHYSFS_Stat *stat)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1658
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1659
    int isDir = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1660
    const ZIPinfo *info = (const ZIPinfo *) opaque;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1661
    const ZIPentry *entry = zip_find_entry(info, filename, &isDir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1662
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1663
    /* !!! FIXME: does this need to resolve entries here? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1664
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1665
    *exists = isDir || (entry != 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1666
    if (!*exists)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1667
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1668
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1669
    if (isDir)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1670
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1671
        stat->filesize = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1672
        stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1673
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1674
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1675
    else if (zip_entry_is_symlink(entry))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1676
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1677
        stat->filesize = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1678
        stat->filetype = PHYSFS_FILETYPE_SYMLINK;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1679
    } /* else if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1680
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1681
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1682
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1683
        stat->filesize = (PHYSFS_sint64) entry->uncompressed_size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1684
        stat->filetype = PHYSFS_FILETYPE_REGULAR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1685
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1686
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1687
    stat->modtime = ((entry) ? entry->last_mod_time : 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1688
    stat->createtime = stat->modtime;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1689
    stat->accesstime = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1690
    stat->readonly = 1; /* .zip files are always read only */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1691
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1692
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1693
} /* ZIP_stat */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1694
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1695
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1696
const PHYSFS_Archiver __PHYSFS_Archiver_ZIP =
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1697
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1698
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1699
        "ZIP",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1700
        "PkZip/WinZip/Info-Zip compatible",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1701
        "Ryan C. Gordon <icculus@icculus.org>",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1702
        "http://icculus.org/physfs/",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1703
    },
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1704
    ZIP_openArchive,        /* openArchive() method    */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1705
    ZIP_enumerateFiles,     /* enumerateFiles() method */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1706
    ZIP_openRead,           /* openRead() method       */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1707
    ZIP_openWrite,          /* openWrite() method      */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1708
    ZIP_openAppend,         /* openAppend() method     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1709
    ZIP_remove,             /* remove() method         */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1710
    ZIP_mkdir,              /* mkdir() method          */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1711
    ZIP_closeArchive,       /* closeArchive() method   */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1712
    ZIP_stat                /* stat() method           */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1713
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1714
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1715
#endif  /* defined PHYSFS_SUPPORTS_ZIP */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1716
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1717
/* end of zip.c ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
  1718