2003-07-21 02:27:09 +02:00
|
|
|
/*
|
|
|
|
* QPAK support routines for PhysicsFS.
|
|
|
|
*
|
|
|
|
* This archiver handles the archive format utilized by Quake 1 and 2.
|
|
|
|
* Quake3-based games use the PkZip/Info-Zip format (which our zip.c
|
|
|
|
* archiver handles).
|
|
|
|
*
|
|
|
|
* ========================================================================
|
|
|
|
*
|
|
|
|
* This format info (in more detail) comes from:
|
|
|
|
* http://debian.fmi.uni-sofia.bg/~sergei/cgsr/docs/pak.txt
|
|
|
|
*
|
|
|
|
* Quake PAK Format
|
|
|
|
*
|
|
|
|
* Header
|
|
|
|
* (4 bytes) signature = 'PACK'
|
|
|
|
* (4 bytes) directory offset
|
|
|
|
* (4 bytes) directory length
|
|
|
|
*
|
|
|
|
* Directory
|
|
|
|
* (56 bytes) file name
|
|
|
|
* (4 bytes) file position
|
|
|
|
* (4 bytes) file length
|
|
|
|
*
|
|
|
|
* ========================================================================
|
|
|
|
*
|
2007-03-11 11:10:28 +01:00
|
|
|
* Please see the file LICENSE.txt in the source's root directory.
|
2003-07-21 02:27:09 +02:00
|
|
|
*
|
|
|
|
* This file written by Ryan C. Gordon.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if (defined PHYSFS_SUPPORTS_QPAK)
|
|
|
|
|
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_internal.h"
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
/* !!! FIXME: what is this here for? */
|
2003-11-09 22:06:15 +01:00
|
|
|
#if 1 /* Make this case insensitive? */
|
2007-03-15 09:16:23 +01:00
|
|
|
#define QPAK_strcmp(x, y) __PHYSFS_stricmpASCII(x, y)
|
|
|
|
#define QPAK_strncmp(x, y, z) __PHYSFS_strnicmpASCII(x, y, z)
|
2003-11-09 22:06:15 +01:00
|
|
|
#else
|
|
|
|
#define QPAK_strcmp(x, y) strcmp(x, y)
|
|
|
|
#define QPAK_strncmp(x, y, z) strncmp(x, y, z)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2003-07-21 02:27:09 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char name[56];
|
|
|
|
PHYSFS_uint32 startPos;
|
|
|
|
PHYSFS_uint32 size;
|
|
|
|
} QPAKentry;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io;
|
2003-07-21 02:27:09 +02:00
|
|
|
PHYSFS_uint32 entryCount;
|
|
|
|
QPAKentry *entries;
|
|
|
|
} QPAKinfo;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io;
|
2003-07-21 02:27:09 +02:00
|
|
|
QPAKentry *entry;
|
|
|
|
PHYSFS_uint32 curPos;
|
|
|
|
} QPAKfileinfo;
|
|
|
|
|
|
|
|
/* Magic numbers... */
|
2005-03-13 04:33:11 +01:00
|
|
|
#define QPAK_SIG 0x4b434150 /* "PACK" in ASCII. */
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static void QPAK_dirClose(dvoid *opaque)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
QPAKinfo *info = ((QPAKinfo *) opaque);
|
2010-08-30 09:01:57 +02:00
|
|
|
info->io->destroy(info->io);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(info->entries);
|
|
|
|
allocator.Free(info);
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_dirClose */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_sint64 QPAK_read(PHYSFS_Io *io, void *buffer, PHYSFS_uint64 len)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
QPAKfileinfo *finfo = (QPAKfileinfo *) io->opaque;
|
2010-08-21 08:47:58 +02:00
|
|
|
const QPAKentry *entry = finfo->entry;
|
|
|
|
const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
|
2003-07-21 02:27:09 +02:00
|
|
|
PHYSFS_sint64 rc;
|
|
|
|
|
2010-08-21 08:47:58 +02:00
|
|
|
if (bytesLeft < len)
|
|
|
|
len = bytesLeft;
|
2003-07-21 02:27:09 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
rc = finfo->io->read(finfo->io, buffer, len);
|
2003-07-21 02:27:09 +02:00
|
|
|
if (rc > 0)
|
2010-08-21 08:47:58 +02:00
|
|
|
finfo->curPos += (PHYSFS_uint32) rc;
|
2003-07-21 02:27:09 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return rc;
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_read */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_sint64 QPAK_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, -1);
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_write */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_sint64 QPAK_tell(PHYSFS_Io *io)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
return ((QPAKfileinfo *) io->opaque)->curPos;
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_tell */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static int QPAK_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
QPAKfileinfo *finfo = (QPAKfileinfo *) io->opaque;
|
|
|
|
const QPAKentry *entry = finfo->entry;
|
2003-07-21 02:27:09 +02:00
|
|
|
int rc;
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(offset >= entry->size, PHYSFS_ERR_PAST_EOF, 0);
|
2010-08-30 09:01:57 +02:00
|
|
|
rc = finfo->io->seek(finfo->io, entry->startPos + offset);
|
2003-07-21 02:27:09 +02:00
|
|
|
if (rc)
|
|
|
|
finfo->curPos = (PHYSFS_uint32) offset;
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return rc;
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_seek */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_sint64 QPAK_length(PHYSFS_Io *io)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
const QPAKfileinfo *finfo = (QPAKfileinfo *) io->opaque;
|
2010-01-28 08:27:45 +01:00
|
|
|
return ((PHYSFS_sint64) finfo->entry->size);
|
2010-08-30 09:01:57 +02:00
|
|
|
} /* QPAK_length */
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_Io *QPAK_duplicate(PHYSFS_Io *_io)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
QPAKfileinfo *origfinfo = (QPAKfileinfo *) _io->opaque;
|
|
|
|
PHYSFS_Io *io = NULL;
|
|
|
|
PHYSFS_Io *retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
|
|
|
|
QPAKfileinfo *finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, QPAK_duplicate_failed);
|
|
|
|
GOTO_IF_MACRO(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, QPAK_duplicate_failed);
|
2003-07-21 02:27:09 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
io = origfinfo->io->duplicate(origfinfo->io);
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!io, ERRPASS, QPAK_duplicate_failed);
|
2010-08-30 09:01:57 +02:00
|
|
|
finfo->io = io;
|
|
|
|
finfo->entry = origfinfo->entry;
|
|
|
|
finfo->curPos = 0;
|
|
|
|
memcpy(retval, _io, sizeof (PHYSFS_Io));
|
|
|
|
retval->opaque = finfo;
|
|
|
|
return retval;
|
2003-07-21 02:27:09 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
QPAK_duplicate_failed:
|
|
|
|
if (finfo != NULL) allocator.Free(finfo);
|
|
|
|
if (retval != NULL) allocator.Free(retval);
|
|
|
|
if (io != NULL) io->destroy(io);
|
|
|
|
return NULL;
|
|
|
|
} /* QPAK_duplicate */
|
2003-07-21 02:27:09 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static int QPAK_flush(PHYSFS_Io *io) { return 1; /* no write support. */ }
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static void QPAK_destroy(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
QPAKfileinfo *finfo = (QPAKfileinfo *) io->opaque;
|
|
|
|
finfo->io->destroy(finfo->io);
|
|
|
|
allocator.Free(finfo);
|
|
|
|
allocator.Free(io);
|
|
|
|
} /* QPAK_destroy */
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static const PHYSFS_Io QPAK_Io =
|
|
|
|
{
|
|
|
|
QPAK_read,
|
|
|
|
QPAK_write,
|
|
|
|
QPAK_seek,
|
|
|
|
QPAK_tell,
|
|
|
|
QPAK_length,
|
|
|
|
QPAK_duplicate,
|
|
|
|
QPAK_flush,
|
|
|
|
QPAK_destroy,
|
|
|
|
NULL
|
|
|
|
};
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static int qpakEntryCmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2008-02-20 13:24:10 +01:00
|
|
|
if (one != two)
|
|
|
|
{
|
|
|
|
const QPAKentry *a = (const QPAKentry *) _a;
|
2010-01-28 08:27:45 +01:00
|
|
|
return QPAK_strcmp(a[one].name, a[two].name);
|
2008-02-20 13:24:10 +01:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return 0;
|
2010-08-30 09:01:57 +02:00
|
|
|
} /* qpakEntryCmp */
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static void qpakEntrySwap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2008-02-20 13:24:10 +01:00
|
|
|
if (one != two)
|
|
|
|
{
|
|
|
|
QPAKentry tmp;
|
|
|
|
QPAKentry *first = &(((QPAKentry *) _a)[one]);
|
|
|
|
QPAKentry *second = &(((QPAKentry *) _a)[two]);
|
|
|
|
memcpy(&tmp, first, sizeof (QPAKentry));
|
|
|
|
memcpy(first, second, sizeof (QPAKentry));
|
|
|
|
memcpy(second, &tmp, sizeof (QPAKentry));
|
|
|
|
} /* if */
|
2010-08-30 09:01:57 +02:00
|
|
|
} /* qpakEntrySwap */
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static int qpak_load_entries(QPAKinfo *info)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io = info->io;
|
|
|
|
PHYSFS_uint32 fileCount = info->entryCount;
|
2003-07-21 02:27:09 +02:00
|
|
|
QPAKentry *entry;
|
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
info->entries = (QPAKentry*) allocator.Malloc(sizeof(QPAKentry)*fileCount);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(info->entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, 0);
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
for (entry = info->entries; fileCount > 0; fileCount--, entry++)
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 56), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &entry->startPos, 4), ERRPASS, 0);
|
|
|
|
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), ERRPASS, 0);
|
2003-07-21 02:27:09 +02:00
|
|
|
entry->size = PHYSFS_swapULE32(entry->size);
|
2010-08-21 08:47:58 +02:00
|
|
|
entry->startPos = PHYSFS_swapULE32(entry->startPos);
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* for */
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
__PHYSFS_sort(info->entries, info->entryCount, qpakEntryCmp, qpakEntrySwap);
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* qpak_load_entries */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
QPAKinfo *info = NULL;
|
|
|
|
PHYSFS_uint32 val = 0;
|
|
|
|
PHYSFS_uint32 pos = 0;
|
|
|
|
PHYSFS_uint32 count = 0;
|
2003-07-21 02:27:09 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
assert(io != NULL); /* shouldn't ever happen. */
|
2003-07-21 02:27:09 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
|
2003-07-21 02:27:09 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), ERRPASS, NULL);
|
|
|
|
if (PHYSFS_swapULE32(val) != QPAK_SIG)
|
|
|
|
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
|
2003-07-21 02:27:09 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), ERRPASS, NULL);
|
2010-08-30 09:01:57 +02:00
|
|
|
pos = PHYSFS_swapULE32(val); /* directory table offset. */
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), ERRPASS, NULL);
|
2010-08-30 09:01:57 +02:00
|
|
|
count = PHYSFS_swapULE32(val);
|
2003-07-21 02:27:09 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
/* corrupted archive? */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO((count % 64) != 0, PHYSFS_ERR_CORRUPT, NULL);
|
2010-08-30 09:01:57 +02:00
|
|
|
count /= 64;
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!io->seek(io, pos), ERRPASS, NULL);
|
2010-08-30 09:01:57 +02:00
|
|
|
|
|
|
|
info = (QPAKinfo *) allocator.Malloc(sizeof (QPAKinfo));
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(info == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2010-08-30 09:01:57 +02:00
|
|
|
memset(info, '\0', sizeof (QPAKinfo));
|
|
|
|
info->io = io;
|
|
|
|
info->entryCount = count;
|
|
|
|
|
|
|
|
if (!qpak_load_entries(info))
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
if (info->entries != NULL)
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(info->entries);
|
|
|
|
allocator.Free(info);
|
2010-08-30 09:01:57 +02:00
|
|
|
return NULL;
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* if */
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
return info;
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_openArchive */
|
|
|
|
|
|
|
|
|
|
|
|
static PHYSFS_sint32 qpak_find_start_of_dir(QPAKinfo *info, const char *path,
|
|
|
|
int stop_on_first_find)
|
|
|
|
{
|
|
|
|
PHYSFS_sint32 lo = 0;
|
|
|
|
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
|
|
|
|
PHYSFS_sint32 middle;
|
2012-03-10 09:18:33 +01:00
|
|
|
PHYSFS_uint32 dlen = (PHYSFS_uint32) strlen(path);
|
2003-07-21 02:27:09 +02:00
|
|
|
PHYSFS_sint32 retval = -1;
|
|
|
|
const char *name;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (*path == '\0') /* root dir? */
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
|
|
|
|
dlen--;
|
|
|
|
|
|
|
|
while (lo <= hi)
|
|
|
|
{
|
|
|
|
middle = lo + ((hi - lo) / 2);
|
|
|
|
name = info->entries[middle].name;
|
2003-11-09 22:06:15 +01:00
|
|
|
rc = QPAK_strncmp(path, name, dlen);
|
2003-07-21 02:27:09 +02:00
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
char ch = name[dlen];
|
|
|
|
if (ch < '/') /* make sure this isn't just a substr match. */
|
|
|
|
rc = -1;
|
|
|
|
else if (ch > '/')
|
|
|
|
rc = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (stop_on_first_find) /* Just checking dir's existance? */
|
2010-01-28 08:27:45 +01:00
|
|
|
return middle;
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
|
2010-01-28 08:27:45 +01:00
|
|
|
return (middle + 1);
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
/* there might be more entries earlier in the list. */
|
|
|
|
retval = middle;
|
|
|
|
hi = middle - 1;
|
|
|
|
} /* else */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (rc > 0)
|
|
|
|
lo = middle + 1;
|
|
|
|
else
|
|
|
|
hi = middle - 1;
|
|
|
|
} /* while */
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* qpak_find_start_of_dir */
|
|
|
|
|
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
/*
|
|
|
|
* Moved to seperate function so we can use alloca then immediately throw
|
|
|
|
* away the allocated stack space...
|
|
|
|
*/
|
2005-09-18 23:44:42 +02:00
|
|
|
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
|
|
|
|
const char *odir, const char *str, PHYSFS_sint32 ln)
|
2004-09-29 08:09:29 +02:00
|
|
|
{
|
2007-03-24 04:54:58 +01:00
|
|
|
char *newstr = __PHYSFS_smallAlloc(ln + 1);
|
2004-09-29 08:09:29 +02:00
|
|
|
if (newstr == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(newstr, str, ln);
|
|
|
|
newstr[ln] = '\0';
|
2005-09-18 23:44:42 +02:00
|
|
|
cb(callbackdata, odir, newstr);
|
2007-03-24 04:54:58 +01:00
|
|
|
__PHYSFS_smallFree(newstr);
|
2004-09-29 08:09:29 +02:00
|
|
|
} /* doEnumCallback */
|
|
|
|
|
|
|
|
|
|
|
|
static void QPAK_enumerateFiles(dvoid *opaque, const char *dname,
|
2005-09-18 23:44:42 +02:00
|
|
|
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
|
|
|
|
const char *origdir, void *callbackdata)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
QPAKinfo *info = ((QPAKinfo *) opaque);
|
2003-07-21 02:27:09 +02:00
|
|
|
PHYSFS_sint32 dlen, dlen_inc, max, i;
|
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
i = qpak_find_start_of_dir(info, dname, 0);
|
|
|
|
if (i == -1) /* no such directory. */
|
|
|
|
return;
|
2003-07-21 02:27:09 +02:00
|
|
|
|
2012-03-10 09:18:33 +01:00
|
|
|
dlen = (PHYSFS_sint32) strlen(dname);
|
2004-09-29 08:09:29 +02:00
|
|
|
if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
|
2003-07-21 02:27:09 +02:00
|
|
|
dlen--;
|
|
|
|
|
|
|
|
dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
|
|
|
|
max = (PHYSFS_sint32) info->entryCount;
|
|
|
|
while (i < max)
|
|
|
|
{
|
|
|
|
char *add;
|
|
|
|
char *ptr;
|
|
|
|
PHYSFS_sint32 ln;
|
|
|
|
char *e = info->entries[i].name;
|
2004-09-29 08:09:29 +02:00
|
|
|
if ((dlen) && ((QPAK_strncmp(e, dname, dlen)) || (e[dlen] != '/')))
|
2003-07-21 02:27:09 +02:00
|
|
|
break; /* past end of this dir; we're done. */
|
|
|
|
|
|
|
|
add = e + dlen_inc;
|
|
|
|
ptr = strchr(add, '/');
|
|
|
|
ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
|
2005-09-18 23:44:42 +02:00
|
|
|
doEnumCallback(cb, callbackdata, origdir, add, ln);
|
2003-07-21 02:27:09 +02:00
|
|
|
ln += dlen_inc; /* point past entry to children... */
|
|
|
|
|
|
|
|
/* increment counter and skip children of subdirs... */
|
|
|
|
while ((++i < max) && (ptr != NULL))
|
|
|
|
{
|
|
|
|
char *e_new = info->entries[i].name;
|
2003-11-09 22:06:15 +01:00
|
|
|
if ((QPAK_strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
|
2003-07-21 02:27:09 +02:00
|
|
|
break;
|
|
|
|
} /* while */
|
|
|
|
} /* while */
|
|
|
|
} /* QPAK_enumerateFiles */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This will find the QPAKentry associated with a path in platform-independent
|
|
|
|
* notation. Directories don't have QPAKentries associated with them, but
|
|
|
|
* (*isDir) will be set to non-zero if a dir was hit.
|
|
|
|
*/
|
2010-02-15 20:02:36 +01:00
|
|
|
static QPAKentry *qpak_find_entry(const QPAKinfo *info, const char *path,
|
|
|
|
int *isDir)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
|
|
|
QPAKentry *a = info->entries;
|
2012-03-10 09:18:33 +01:00
|
|
|
PHYSFS_sint32 pathlen = (PHYSFS_sint32) strlen(path);
|
2003-07-21 02:27:09 +02:00
|
|
|
PHYSFS_sint32 lo = 0;
|
|
|
|
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
|
|
|
|
PHYSFS_sint32 middle;
|
|
|
|
const char *thispath = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
while (lo <= hi)
|
|
|
|
{
|
|
|
|
middle = lo + ((hi - lo) / 2);
|
|
|
|
thispath = a[middle].name;
|
2003-11-09 22:06:15 +01:00
|
|
|
rc = QPAK_strncmp(path, thispath, pathlen);
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
if (rc > 0)
|
|
|
|
lo = middle + 1;
|
|
|
|
|
|
|
|
else if (rc < 0)
|
|
|
|
hi = middle - 1;
|
|
|
|
|
|
|
|
else /* substring match...might be dir or entry or nothing. */
|
|
|
|
{
|
|
|
|
if (isDir != NULL)
|
|
|
|
{
|
|
|
|
*isDir = (thispath[pathlen] == '/');
|
|
|
|
if (*isDir)
|
2010-01-28 08:27:45 +01:00
|
|
|
return NULL;
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (thispath[pathlen] == '\0') /* found entry? */
|
2010-01-28 08:27:45 +01:00
|
|
|
return &a[middle];
|
2011-02-18 18:38:05 +01:00
|
|
|
/* adjust search params, try again. */
|
|
|
|
else if (thispath[pathlen] > '/')
|
|
|
|
hi = middle - 1;
|
2003-07-21 02:27:09 +02:00
|
|
|
else
|
2011-02-18 18:38:05 +01:00
|
|
|
lo = middle + 1;
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* if */
|
|
|
|
} /* while */
|
|
|
|
|
|
|
|
if (isDir != NULL)
|
|
|
|
*isDir = 0;
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_NO_SUCH_PATH, NULL);
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* qpak_find_entry */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_Io *QPAK_openRead(dvoid *opaque, const char *fnm, int *fileExists)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io = NULL;
|
2004-09-26 02:25:04 +02:00
|
|
|
QPAKinfo *info = ((QPAKinfo *) opaque);
|
2003-07-21 02:27:09 +02:00
|
|
|
QPAKfileinfo *finfo;
|
|
|
|
QPAKentry *entry;
|
|
|
|
int isDir;
|
|
|
|
|
|
|
|
entry = qpak_find_entry(info, fnm, &isDir);
|
|
|
|
*fileExists = ((entry != NULL) || (isDir));
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(isDir, PHYSFS_ERR_NOT_A_FILE, NULL);
|
|
|
|
BAIL_IF_MACRO(entry == NULL, PHYSFS_ERR_NO_SUCH_PATH, NULL);
|
2003-07-21 02:27:09 +02:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(finfo == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2003-07-21 02:27:09 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
finfo->io = info->io->duplicate(info->io);
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(finfo->io == NULL, ERRPASS, QPAK_openRead_failed);
|
2010-08-30 09:01:57 +02:00
|
|
|
if (!finfo->io->seek(finfo->io, entry->startPos))
|
2012-03-20 20:38:12 +01:00
|
|
|
goto QPAK_openRead_failed;
|
2010-08-30 09:01:57 +02:00
|
|
|
|
|
|
|
finfo->curPos = 0;
|
|
|
|
finfo->entry = entry;
|
|
|
|
io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(io == NULL, PHYSFS_ERR_OUT_OF_MEMORY, QPAK_openRead_failed);
|
2010-08-30 09:01:57 +02:00
|
|
|
memcpy(io, &QPAK_Io, sizeof (PHYSFS_Io));
|
|
|
|
io->opaque = finfo;
|
|
|
|
|
|
|
|
return io;
|
|
|
|
|
|
|
|
QPAK_openRead_failed:
|
|
|
|
if (finfo != NULL)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
if (finfo->io != NULL)
|
|
|
|
finfo->io->destroy(finfo->io);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(finfo);
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* if */
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
if (io != NULL)
|
|
|
|
allocator.Free(io);
|
|
|
|
|
|
|
|
return NULL;
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_openRead */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_Io *QPAK_openWrite(dvoid *opaque, const char *name)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_openWrite */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_Io *QPAK_openAppend(dvoid *opaque, const char *name)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_openAppend */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int QPAK_remove(dvoid *opaque, const char *name)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, 0);
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_remove */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int QPAK_mkdir(dvoid *opaque, const char *name)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, 0);
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_mkdir */
|
|
|
|
|
2004-09-29 08:18:04 +02:00
|
|
|
|
2010-08-22 06:37:25 +02:00
|
|
|
static int QPAK_stat(dvoid *opaque, const char *filename, int *exists,
|
2010-02-15 20:02:36 +01:00
|
|
|
PHYSFS_Stat *stat)
|
|
|
|
{
|
|
|
|
int isDir = 0;
|
|
|
|
const QPAKinfo *info = (const QPAKinfo *) opaque;
|
|
|
|
const QPAKentry *entry = qpak_find_entry(info, filename, &isDir);
|
|
|
|
|
|
|
|
if (isDir)
|
|
|
|
{
|
2011-10-18 21:55:29 +02:00
|
|
|
*exists = 1;
|
2010-02-15 20:02:36 +01:00
|
|
|
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
|
|
|
|
stat->filesize = 0;
|
|
|
|
} /* if */
|
2011-10-18 21:55:29 +02:00
|
|
|
else if (entry != NULL)
|
2010-02-15 20:02:36 +01:00
|
|
|
{
|
2011-10-18 21:55:29 +02:00
|
|
|
*exists = 1;
|
2010-02-15 20:02:36 +01:00
|
|
|
stat->filetype = PHYSFS_FILETYPE_REGULAR;
|
|
|
|
stat->filesize = entry->size;
|
2011-10-18 21:55:29 +02:00
|
|
|
} /* else if */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*exists = 0;
|
|
|
|
return 0;
|
2010-02-15 20:02:36 +01:00
|
|
|
} /* else */
|
|
|
|
|
2010-08-25 07:28:28 +02:00
|
|
|
stat->modtime = -1;
|
|
|
|
stat->createtime = -1;
|
|
|
|
stat->accesstime = -1;
|
2010-02-15 20:02:36 +01:00
|
|
|
stat->readonly = 1;
|
|
|
|
|
2010-08-22 01:10:42 +02:00
|
|
|
return 1;
|
2010-02-15 20:02:36 +01:00
|
|
|
} /* QPAK_stat */
|
|
|
|
|
|
|
|
|
2004-09-29 08:18:04 +02:00
|
|
|
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
|
|
|
|
{
|
|
|
|
"PAK",
|
|
|
|
QPAK_ARCHIVE_DESCRIPTION,
|
2006-01-01 13:33:19 +01:00
|
|
|
"Ryan C. Gordon <icculus@icculus.org>",
|
2004-09-29 08:18:04 +02:00
|
|
|
"http://icculus.org/physfs/",
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const PHYSFS_Archiver __PHYSFS_Archiver_QPAK =
|
|
|
|
{
|
|
|
|
&__PHYSFS_ArchiveInfo_QPAK,
|
|
|
|
QPAK_openArchive, /* openArchive() method */
|
|
|
|
QPAK_enumerateFiles, /* enumerateFiles() method */
|
|
|
|
QPAK_openRead, /* openRead() method */
|
|
|
|
QPAK_openWrite, /* openWrite() method */
|
|
|
|
QPAK_openAppend, /* openAppend() method */
|
|
|
|
QPAK_remove, /* remove() method */
|
|
|
|
QPAK_mkdir, /* mkdir() method */
|
|
|
|
QPAK_dirClose, /* dirClose() method */
|
2010-08-30 09:01:57 +02:00
|
|
|
QPAK_stat /* stat() method */
|
2004-09-29 08:18:04 +02:00
|
|
|
};
|
|
|
|
|
2003-07-21 02:27:09 +02:00
|
|
|
#endif /* defined PHYSFS_SUPPORTS_QPAK */
|
|
|
|
|
|
|
|
/* end of qpak.c ... */
|
|
|
|
|