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)
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "physfs.h"
|
|
|
|
|
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_internal.h"
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
PHYSFS_sint64 last_mod_time;
|
|
|
|
PHYSFS_uint32 entryCount;
|
|
|
|
QPAKentry *entries;
|
|
|
|
} QPAKinfo;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
void *handle;
|
|
|
|
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);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(info->filename);
|
|
|
|
allocator.Free(info->entries);
|
|
|
|
allocator.Free(info);
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_dirClose */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static PHYSFS_sint64 QPAK_read(fvoid *opaque, void *buffer,
|
2003-07-21 02:27:09 +02:00
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
|
2003-07-21 02:27:09 +02:00
|
|
|
QPAKentry *entry = finfo->entry;
|
|
|
|
PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
|
|
|
|
PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
|
|
|
|
PHYSFS_sint64 rc;
|
|
|
|
|
|
|
|
if (objsLeft < objCount)
|
|
|
|
objCount = objsLeft;
|
|
|
|
|
|
|
|
rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
|
|
|
|
if (rc > 0)
|
|
|
|
finfo->curPos += (PHYSFS_uint32) (rc * objSize);
|
|
|
|
|
|
|
|
return(rc);
|
|
|
|
} /* QPAK_read */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static PHYSFS_sint64 QPAK_write(fvoid *opaque, const void *buffer,
|
2003-07-21 02:27:09 +02:00
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
|
|
|
|
{
|
|
|
|
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
|
|
|
|
} /* QPAK_write */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int QPAK_eof(fvoid *opaque)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
|
2003-07-21 02:27:09 +02:00
|
|
|
QPAKentry *entry = finfo->entry;
|
|
|
|
return(finfo->curPos >= entry->size);
|
|
|
|
} /* QPAK_eof */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static PHYSFS_sint64 QPAK_tell(fvoid *opaque)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
return(((QPAKfileinfo *) opaque)->curPos);
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_tell */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int QPAK_seek(fvoid *opaque, PHYSFS_uint64 offset)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
|
2003-07-21 02:27:09 +02:00
|
|
|
QPAKentry *entry = finfo->entry;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
|
|
|
|
BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
|
|
|
|
rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
|
|
|
|
if (rc)
|
|
|
|
finfo->curPos = (PHYSFS_uint32) offset;
|
|
|
|
|
|
|
|
return(rc);
|
|
|
|
} /* QPAK_seek */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static PHYSFS_sint64 QPAK_fileLength(fvoid *opaque)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
|
2003-07-21 02:27:09 +02:00
|
|
|
return((PHYSFS_sint64) finfo->entry->size);
|
|
|
|
} /* QPAK_fileLength */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int QPAK_fileClose(fvoid *opaque)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
QPAKfileinfo *finfo = (QPAKfileinfo *) opaque;
|
2003-07-21 02:27:09 +02:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(finfo);
|
2003-07-21 02:27:09 +02:00
|
|
|
return(1);
|
|
|
|
} /* QPAK_fileClose */
|
|
|
|
|
|
|
|
|
|
|
|
static int qpak_open(const char *filename, int forWriting,
|
|
|
|
void **fh, PHYSFS_uint32 *count)
|
|
|
|
{
|
|
|
|
PHYSFS_uint32 buf;
|
|
|
|
|
|
|
|
*fh = NULL;
|
|
|
|
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
|
|
|
|
|
|
|
|
*fh = __PHYSFS_platformOpenRead(filename);
|
|
|
|
BAIL_IF_MACRO(*fh == NULL, NULL, 0);
|
|
|
|
|
|
|
|
if (__PHYSFS_platformRead(*fh, &buf, sizeof (PHYSFS_uint32), 1) != 1)
|
|
|
|
goto openQpak_failed;
|
|
|
|
|
|
|
|
buf = PHYSFS_swapULE32(buf);
|
2005-03-13 04:33:11 +01:00
|
|
|
GOTO_IF_MACRO(buf != QPAK_SIG, ERR_UNSUPPORTED_ARCHIVE, openQpak_failed);
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
if (__PHYSFS_platformRead(*fh, &buf, sizeof (PHYSFS_uint32), 1) != 1)
|
|
|
|
goto openQpak_failed;
|
|
|
|
|
|
|
|
buf = PHYSFS_swapULE32(buf); /* directory table offset. */
|
|
|
|
|
|
|
|
if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
|
|
|
|
goto openQpak_failed;
|
|
|
|
|
|
|
|
*count = PHYSFS_swapULE32(*count);
|
|
|
|
|
2005-03-13 04:33:11 +01:00
|
|
|
/* corrupted archive? */
|
|
|
|
GOTO_IF_MACRO((*count % 64) != 0, ERR_CORRUPTED, openQpak_failed);
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
if (!__PHYSFS_platformSeek(*fh, buf))
|
|
|
|
goto openQpak_failed;
|
|
|
|
|
|
|
|
*count /= 64;
|
|
|
|
return(1);
|
|
|
|
|
|
|
|
openQpak_failed:
|
|
|
|
if (*fh != NULL)
|
|
|
|
__PHYSFS_platformClose(*fh);
|
|
|
|
|
|
|
|
*count = -1;
|
|
|
|
*fh = NULL;
|
|
|
|
return(0);
|
|
|
|
} /* qpak_open */
|
|
|
|
|
|
|
|
|
|
|
|
static int QPAK_isArchive(const char *filename, int forWriting)
|
|
|
|
{
|
|
|
|
void *fh;
|
|
|
|
PHYSFS_uint32 fileCount;
|
|
|
|
int retval = qpak_open(filename, forWriting, &fh, &fileCount);
|
|
|
|
|
|
|
|
if (fh != NULL)
|
|
|
|
__PHYSFS_platformClose(fh);
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* QPAK_isArchive */
|
|
|
|
|
|
|
|
|
|
|
|
static int qpak_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
|
|
|
|
{
|
|
|
|
QPAKentry *a = (QPAKentry *) _a;
|
2003-11-09 22:06:15 +01:00
|
|
|
return(QPAK_strcmp(a[one].name, a[two].name));
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* qpak_entry_cmp */
|
|
|
|
|
|
|
|
|
|
|
|
static void qpak_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 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));
|
|
|
|
} /* qpak_entry_swap */
|
|
|
|
|
|
|
|
|
|
|
|
static int qpak_load_entries(const char *name, int forWriting, QPAKinfo *info)
|
|
|
|
{
|
|
|
|
void *fh = NULL;
|
|
|
|
PHYSFS_uint32 fileCount;
|
|
|
|
QPAKentry *entry;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(!qpak_open(name, forWriting, &fh, &fileCount), NULL, 0);
|
|
|
|
info->entryCount = fileCount;
|
2005-03-14 12:49:30 +01:00
|
|
|
info->entries = (QPAKentry*) allocator.Malloc(sizeof(QPAKentry)*fileCount);
|
2003-07-21 02:27:09 +02:00
|
|
|
if (info->entries == NULL)
|
|
|
|
{
|
|
|
|
__PHYSFS_platformClose(fh);
|
|
|
|
BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
for (entry = info->entries; fileCount > 0; fileCount--, entry++)
|
|
|
|
{
|
|
|
|
PHYSFS_uint32 loc;
|
|
|
|
|
|
|
|
if (__PHYSFS_platformRead(fh,&entry->name,sizeof(entry->name),1) != 1)
|
|
|
|
{
|
|
|
|
__PHYSFS_platformClose(fh);
|
|
|
|
return(0);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (__PHYSFS_platformRead(fh,&loc,sizeof(loc),1) != 1)
|
|
|
|
{
|
|
|
|
__PHYSFS_platformClose(fh);
|
|
|
|
return(0);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (__PHYSFS_platformRead(fh,&entry->size,sizeof(entry->size),1) != 1)
|
|
|
|
{
|
|
|
|
__PHYSFS_platformClose(fh);
|
|
|
|
return(0);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
entry->size = PHYSFS_swapULE32(entry->size);
|
|
|
|
entry->startPos = PHYSFS_swapULE32(loc);
|
|
|
|
} /* for */
|
|
|
|
|
|
|
|
__PHYSFS_platformClose(fh);
|
|
|
|
|
|
|
|
__PHYSFS_sort(info->entries, info->entryCount,
|
|
|
|
qpak_entry_cmp, qpak_entry_swap);
|
|
|
|
return(1);
|
|
|
|
} /* qpak_load_entries */
|
|
|
|
|
|
|
|
|
2004-09-26 02:25:04 +02:00
|
|
|
static void *QPAK_openArchive(const char *name, int forWriting)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
QPAKinfo *info = (QPAKinfo *) allocator.Malloc(sizeof (QPAKinfo));
|
2003-07-21 02:27:09 +02:00
|
|
|
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
|
|
|
|
|
2004-09-26 02:25:04 +02:00
|
|
|
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
|
2003-07-21 02:27:09 +02:00
|
|
|
memset(info, '\0', sizeof (QPAKinfo));
|
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
|
2003-07-21 02:27:09 +02:00
|
|
|
if (info->filename == NULL)
|
|
|
|
{
|
|
|
|
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
|
|
|
|
goto QPAK_openArchive_failed;
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (!qpak_load_entries(name, forWriting, info))
|
|
|
|
goto QPAK_openArchive_failed;
|
|
|
|
|
|
|
|
strcpy(info->filename, name);
|
|
|
|
info->last_mod_time = modtime;
|
2004-09-26 02:25:04 +02:00
|
|
|
return(info);
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
QPAK_openArchive_failed:
|
2004-09-26 02:25:04 +02:00
|
|
|
if (info != NULL)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
if (info->filename != NULL)
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(info->filename);
|
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);
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return(NULL);
|
|
|
|
} /* 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;
|
|
|
|
PHYSFS_uint32 dlen = strlen(path);
|
|
|
|
PHYSFS_sint32 retval = -1;
|
|
|
|
const char *name;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (*path == '\0') /* root dir? */
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
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? */
|
|
|
|
return(middle);
|
|
|
|
|
|
|
|
if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
|
|
|
|
return(middle + 1);
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* 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
|
|
|
{
|
|
|
|
char *newstr = alloca(ln + 1);
|
|
|
|
if (newstr == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(newstr, str, ln);
|
|
|
|
newstr[ln] = '\0';
|
2005-09-18 23:44:42 +02:00
|
|
|
cb(callbackdata, odir, 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
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
dlen = strlen(dname);
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static QPAKentry *qpak_find_entry(QPAKinfo *info, const char *path, int *isDir)
|
|
|
|
{
|
|
|
|
QPAKentry *a = info->entries;
|
|
|
|
PHYSFS_sint32 pathlen = strlen(path);
|
|
|
|
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)
|
|
|
|
return(NULL);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (thispath[pathlen] == '\0') /* found entry? */
|
|
|
|
return(&a[middle]);
|
|
|
|
else
|
|
|
|
hi = middle - 1; /* adjust search params, try again. */
|
|
|
|
} /* if */
|
|
|
|
} /* while */
|
|
|
|
|
|
|
|
if (isDir != NULL)
|
|
|
|
*isDir = 0;
|
|
|
|
|
|
|
|
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
|
|
|
|
} /* qpak_find_entry */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int QPAK_exists(dvoid *opaque, const char *name)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
|
|
|
int isDir;
|
2004-09-26 02:25:04 +02:00
|
|
|
QPAKinfo *info = (QPAKinfo *) opaque;
|
2003-07-21 02:27:09 +02:00
|
|
|
QPAKentry *entry = qpak_find_entry(info, name, &isDir);
|
|
|
|
return((entry != NULL) || (isDir));
|
|
|
|
} /* QPAK_exists */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int QPAK_isDirectory(dvoid *opaque, const char *name, int *fileExists)
|
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
|
|
|
int isDir;
|
|
|
|
QPAKentry *entry = qpak_find_entry(info, name, &isDir);
|
|
|
|
|
|
|
|
*fileExists = ((isDir) || (entry != NULL));
|
|
|
|
if (isDir)
|
|
|
|
return(1); /* definitely a dir. */
|
|
|
|
|
|
|
|
BAIL_MACRO(ERR_NO_SUCH_FILE, 0);
|
|
|
|
} /* QPAK_isDirectory */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int QPAK_isSymLink(dvoid *opaque, const char *name, int *fileExists)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
*fileExists = QPAK_exists(opaque, name);
|
2003-07-21 02:27:09 +02:00
|
|
|
return(0); /* never symlinks in a quake pak. */
|
|
|
|
} /* QPAK_isSymLink */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static PHYSFS_sint64 QPAK_getLastModTime(dvoid *opaque,
|
2003-07-21 02:27:09 +02:00
|
|
|
const char *name,
|
|
|
|
int *fileExists)
|
|
|
|
{
|
|
|
|
int isDir;
|
2004-09-26 02:25:04 +02:00
|
|
|
QPAKinfo *info = ((QPAKinfo *) opaque);
|
2003-07-21 02:27:09 +02:00
|
|
|
PHYSFS_sint64 retval = -1;
|
|
|
|
QPAKentry *entry = qpak_find_entry(info, name, &isDir);
|
|
|
|
|
|
|
|
*fileExists = ((isDir) || (entry != NULL));
|
|
|
|
if (*fileExists) /* use time of QPAK itself in the physical filesystem. */
|
|
|
|
retval = info->last_mod_time;
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* QPAK_getLastModTime */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static fvoid *QPAK_openRead(dvoid *opaque, const char *fnm, int *fileExists)
|
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
|
|
|
QPAKfileinfo *finfo;
|
|
|
|
QPAKentry *entry;
|
|
|
|
int isDir;
|
|
|
|
|
|
|
|
entry = qpak_find_entry(info, fnm, &isDir);
|
|
|
|
*fileExists = ((entry != NULL) || (isDir));
|
|
|
|
BAIL_IF_MACRO(isDir, ERR_NOT_A_FILE, NULL);
|
|
|
|
BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
|
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
finfo = (QPAKfileinfo *) allocator.Malloc(sizeof (QPAKfileinfo));
|
2004-09-26 15:00:59 +02:00
|
|
|
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
|
2003-07-21 02:27:09 +02:00
|
|
|
|
|
|
|
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
|
|
|
|
if ( (finfo->handle == NULL) ||
|
|
|
|
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
|
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(finfo);
|
2003-07-21 02:27:09 +02:00
|
|
|
return(NULL);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
finfo->curPos = 0;
|
|
|
|
finfo->entry = entry;
|
2004-09-26 15:00:59 +02:00
|
|
|
return(finfo);
|
2003-07-21 02:27:09 +02:00
|
|
|
} /* QPAK_openRead */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static fvoid *QPAK_openWrite(dvoid *opaque, const char *name)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
|
|
|
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
|
|
|
|
} /* QPAK_openWrite */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static fvoid *QPAK_openAppend(dvoid *opaque, const char *name)
|
2003-07-21 02:27:09 +02:00
|
|
|
{
|
|
|
|
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
|
|
|
|
} /* 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
|
|
|
{
|
|
|
|
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
|
|
|
|
} /* 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
|
|
|
{
|
|
|
|
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
|
|
|
|
} /* QPAK_mkdir */
|
|
|
|
|
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_isArchive, /* isArchive() method */
|
|
|
|
QPAK_openArchive, /* openArchive() method */
|
|
|
|
QPAK_enumerateFiles, /* enumerateFiles() method */
|
|
|
|
QPAK_exists, /* exists() method */
|
|
|
|
QPAK_isDirectory, /* isDirectory() method */
|
|
|
|
QPAK_isSymLink, /* isSymLink() method */
|
|
|
|
QPAK_getLastModTime, /* getLastModTime() 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 */
|
|
|
|
QPAK_read, /* read() method */
|
|
|
|
QPAK_write, /* write() method */
|
|
|
|
QPAK_eof, /* eof() method */
|
|
|
|
QPAK_tell, /* tell() method */
|
|
|
|
QPAK_seek, /* seek() method */
|
|
|
|
QPAK_fileLength, /* fileLength() method */
|
|
|
|
QPAK_fileClose /* fileClose() method */
|
|
|
|
};
|
|
|
|
|
2003-07-21 02:27:09 +02:00
|
|
|
#endif /* defined PHYSFS_SUPPORTS_QPAK */
|
|
|
|
|
|
|
|
/* end of qpak.c ... */
|
|
|
|
|