Reworked the "unpacked" archivers to use DirTree.

This cleaned up a lot of code and improved things, and also allowed a lot of
the restrictions on unpacked archivers to be removed.
This commit is contained in:
Ryan C. Gordon 2017-07-16 04:39:14 -04:00
parent ffa1836335
commit 62ad133862
8 changed files with 222 additions and 400 deletions

View File

@ -29,36 +29,30 @@
#if PHYSFS_SUPPORTS_GRP #if PHYSFS_SUPPORTS_GRP
static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount) static int grpLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
{ {
PHYSFS_uint32 location = 16; /* sizeof sig. */ PHYSFS_uint32 location = 16 + (16 * count); /* past sig+metadata. */
UNPKentry *entries = NULL; PHYSFS_uint32 i;
UNPKentry *entry = NULL;
char *ptr = NULL;
entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount); for (i = 0; i < count; i++)
BAIL_IF(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
location += (16 * fileCount);
for (entry = entries; fileCount > 0; fileCount--, entry++)
{ {
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->name, 12), failed); char *ptr;
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->size, 4), failed); char name[13];
entry->name[12] = '\0'; /* name isn't null-terminated in file. */ PHYSFS_uint32 size;
if ((ptr = strchr(entry->name, ' ')) != NULL) BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 12), 0);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
name[12] = '\0'; /* name isn't null-terminated in file. */
if ((ptr = strchr(name, ' ')) != NULL)
*ptr = '\0'; /* trim extra spaces. */ *ptr = '\0'; /* trim extra spaces. */
entry->size = PHYSFS_swapULE32(entry->size); size = PHYSFS_swapULE32(size);
entry->startPos = location; BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, location, size), 0);
location += entry->size;
location += size;
} /* for */ } /* for */
return entries; return 1;
failed:
allocator.Free(entries);
return NULL;
} /* grpLoadEntries */ } /* grpLoadEntries */
@ -66,7 +60,7 @@ static void *GRP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{ {
PHYSFS_uint8 buf[12]; PHYSFS_uint8 buf[12];
PHYSFS_uint32 count = 0; PHYSFS_uint32 count = 0;
UNPKentry *entries = NULL; void *unpkarc = NULL;
assert(io != NULL); /* shouldn't ever happen. */ assert(io != NULL); /* shouldn't ever happen. */
@ -79,9 +73,16 @@ static void *GRP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof(count)), NULL); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof(count)), NULL);
count = PHYSFS_swapULE32(count); count = PHYSFS_swapULE32(count);
entries = grpLoadEntries(io, count); unpkarc = UNPK_openArchive(io, count);
BAIL_IF_ERRPASS(!entries, NULL); BAIL_IF_ERRPASS(!unpkarc, NULL);
return UNPK_openArchive(io, entries, count);
if (!grpLoadEntries(io, count, unpkarc))
{
UNPK_closeArchive(unpkarc);
return NULL;
} /* if */
return unpkarc;
} /* GRP_openArchive */ } /* GRP_openArchive */

View File

@ -34,43 +34,30 @@
#if PHYSFS_SUPPORTS_HOG #if PHYSFS_SUPPORTS_HOG
static UNPKentry *hogLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 *_entCount) static int hogLoadEntries(PHYSFS_Io *io, void *unpkarc)
{ {
const PHYSFS_uint64 iolen = io->length(io); const PHYSFS_uint64 iolen = io->length(io);
PHYSFS_uint32 entCount = 0;
void *ptr = NULL;
UNPKentry *entries = NULL;
UNPKentry *entry = NULL;
PHYSFS_uint32 size = 0;
PHYSFS_uint32 pos = 3; PHYSFS_uint32 pos = 3;
while (pos < iolen) while (pos < iolen)
{ {
entCount++; PHYSFS_uint32 size;
ptr = allocator.Realloc(ptr, sizeof (UNPKentry) * entCount); char name[13];
GOTO_IF(ptr == NULL, PHYSFS_ERR_OUT_OF_MEMORY, failed);
entries = (UNPKentry *) ptr;
entry = &entries[entCount-1];
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->name, 13), failed); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 13), 0);
pos += 13; BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), failed); name[12] = '\0'; /* just in case. */
pos += 4; pos += 13 + 4;
entry->size = PHYSFS_swapULE32(size); size = PHYSFS_swapULE32(size);
entry->startPos = pos; BAIL_IF_ERRPASS(!UNPK_addEntry(unpkarc, name, 0, pos, size), 0);
pos += size; pos += size;
/* skip over entry */ /* skip over entry */
GOTO_IF_ERRPASS(!io->seek(io, pos), failed); BAIL_IF_ERRPASS(!io->seek(io, pos), 0);
} /* while */ } /* while */
*_entCount = entCount; return 1;
return entries;
failed:
allocator.Free(entries);
return NULL;
} /* hogLoadEntries */ } /* hogLoadEntries */
@ -78,16 +65,23 @@ static void *HOG_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{ {
PHYSFS_uint8 buf[3]; PHYSFS_uint8 buf[3];
PHYSFS_uint32 count = 0; PHYSFS_uint32 count = 0;
UNPKentry *entries = NULL; void *unpkarc = NULL;
assert(io != NULL); /* shouldn't ever happen. */ assert(io != NULL); /* shouldn't ever happen. */
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL); BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, 3), NULL); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, 3), NULL);
BAIL_IF(memcmp(buf, "DHF", 3) != 0, PHYSFS_ERR_UNSUPPORTED, NULL); BAIL_IF(memcmp(buf, "DHF", 3) != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
entries = hogLoadEntries(io, &count); unpkarc = UNPK_openArchive(io, count);
BAIL_IF_ERRPASS(!entries, NULL); BAIL_IF_ERRPASS(!unpkarc, NULL);
return UNPK_openArchive(io, entries, count);
if (!hogLoadEntries(io, unpkarc))
{
UNPK_closeArchive(unpkarc);
return NULL;
} /* if */
return unpkarc;
} /* HOG_openArchive */ } /* HOG_openArchive */

View File

@ -32,31 +32,24 @@
#if PHYSFS_SUPPORTS_MVL #if PHYSFS_SUPPORTS_MVL
static UNPKentry *mvlLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount) static int mvlLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
{ {
PHYSFS_uint32 location = 8; /* sizeof sig. */ PHYSFS_uint32 location = 8 + (17 * count); /* past sig+metadata. */
UNPKentry *entries = NULL; PHYSFS_uint32 i;
UNPKentry *entry = NULL;
entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount); for (i = 0; i < count; i++)
BAIL_IF(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
location += (17 * fileCount);
for (entry = entries; fileCount > 0; fileCount--, entry++)
{ {
if (!__PHYSFS_readAll(io, &entry->name, 13)) goto failed; PHYSFS_uint32 size;
if (!__PHYSFS_readAll(io, &entry->size, 4)) goto failed; char name[13];
entry->size = PHYSFS_swapULE32(entry->size); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 13), 0);
entry->startPos = location; BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
location += entry->size; name[12] = '\0'; /* just in case. */
size = PHYSFS_swapULE32(size);
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, location, size), 0);
location += size;
} /* for */ } /* for */
return entries; return 1;
failed:
allocator.Free(entries);
return NULL;
} /* mvlLoadEntries */ } /* mvlLoadEntries */
@ -64,7 +57,7 @@ static void *MVL_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{ {
PHYSFS_uint8 buf[4]; PHYSFS_uint8 buf[4];
PHYSFS_uint32 count = 0; PHYSFS_uint32 count = 0;
UNPKentry *entries = NULL; void *unpkarc;
assert(io != NULL); /* shouldn't ever happen. */ assert(io != NULL); /* shouldn't ever happen. */
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL); BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
@ -73,8 +66,17 @@ static void *MVL_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof(count)), NULL); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof(count)), NULL);
count = PHYSFS_swapULE32(count); count = PHYSFS_swapULE32(count);
entries = mvlLoadEntries(io, count);
return (!entries) ? NULL : UNPK_openArchive(io, entries, count); unpkarc = UNPK_openArchive(io, count);
BAIL_IF_ERRPASS(!unpkarc, NULL);
if (!mvlLoadEntries(io, count, unpkarc))
{
UNPK_closeArchive(unpkarc);
return NULL;
} /* if */
return unpkarc;
} /* MVL_openArchive */ } /* MVL_openArchive */

View File

@ -36,37 +36,32 @@
#define QPAK_SIG 0x4B434150 /* "PACK" in ASCII. */ #define QPAK_SIG 0x4B434150 /* "PACK" in ASCII. */
static UNPKentry *qpakLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount) static int qpakLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
{ {
UNPKentry *entries = NULL; PHYSFS_uint32 i;
UNPKentry *entry = NULL; for (i = 0; i < count; i++)
entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
BAIL_IF(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
for (entry = entries; fileCount > 0; fileCount--, entry++)
{ {
if (!__PHYSFS_readAll(io, &entry->name, 56)) goto failed; PHYSFS_uint32 size;
if (!__PHYSFS_readAll(io, &entry->startPos, 4)) goto failed; PHYSFS_uint32 location;
if (!__PHYSFS_readAll(io, &entry->size, 4)) goto failed; char name[56];
entry->size = PHYSFS_swapULE32(entry->size); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 56), 0);
entry->startPos = PHYSFS_swapULE32(entry->startPos); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &location, 4), 0);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
size = PHYSFS_swapULE32(size);
location = PHYSFS_swapULE32(location);
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, location, size), 0);
} /* for */ } /* for */
return entries; return 1;
failed:
allocator.Free(entries);
return NULL;
} /* qpakLoadEntries */ } /* qpakLoadEntries */
static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting) static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{ {
UNPKentry *entries = NULL;
PHYSFS_uint32 val = 0; PHYSFS_uint32 val = 0;
PHYSFS_uint32 pos = 0; PHYSFS_uint32 pos = 0;
PHYSFS_uint32 count = 0; PHYSFS_uint32 count = 0;
void *unpkarc;
assert(io != NULL); /* shouldn't ever happen. */ assert(io != NULL); /* shouldn't ever happen. */
@ -88,9 +83,16 @@ static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
BAIL_IF_ERRPASS(!io->seek(io, pos), NULL); BAIL_IF_ERRPASS(!io->seek(io, pos), NULL);
entries = qpakLoadEntries(io, count); unpkarc = UNPK_openArchive(io, count);
BAIL_IF_ERRPASS(!entries, NULL); BAIL_IF_ERRPASS(!unpkarc, NULL);
return UNPK_openArchive(io, entries, count);
if (!qpakLoadEntries(io, count, unpkarc))
{
UNPK_closeArchive(unpkarc);
return NULL;
} /* if */
return unpkarc;
} /* QPAK_openArchive */ } /* QPAK_openArchive */

View File

@ -20,79 +20,80 @@
#if PHYSFS_SUPPORTS_SLB #if PHYSFS_SUPPORTS_SLB
static UNPKentry *slbLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount) static int slbLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
{ {
UNPKentry *entries = NULL; PHYSFS_uint32 i;
UNPKentry *entry = NULL; for (i = 0; i < count; i++)
entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
BAIL_IF(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
for (entry = entries; fileCount > 0; fileCount--, entry++)
{ {
PHYSFS_uint32 location;
PHYSFS_uint32 size;
char name[64];
char backslash;
char *ptr; char *ptr;
/* don't include the '\' in the beginning */ /* don't include the '\' in the beginning */
char backslash; BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &backslash, 1), 0);
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &backslash, 1), failed); BAIL_IF(backslash != '\\', PHYSFS_ERR_CORRUPT, 0);
GOTO_IF_ERRPASS(backslash != '\\', failed);
/* read the rest of the buffer, 63 bytes */ /* read the rest of the buffer, 63 bytes */
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->name, 63), failed); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &name, 63), 0);
entry->name[63] = '\0'; /* in case the name lacks the null terminator */ name[63] = '\0'; /* in case the name lacks the null terminator */
/* convert backslashes */ /* convert backslashes */
for (ptr = entry->name; *ptr; ptr++) for (ptr = name; *ptr; ptr++)
{ {
if (*ptr == '\\') if (*ptr == '\\')
*ptr = '/'; *ptr = '/';
} /* for */ } /* for */
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->startPos, 4), failed); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &location, 4), 0);
entry->startPos = PHYSFS_swapULE32(entry->startPos); location = PHYSFS_swapULE32(location);
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->size, 4), failed); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
entry->size = PHYSFS_swapULE32(entry->size); size = PHYSFS_swapULE32(size);
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, location, size), 0);
} /* for */ } /* for */
return entries;
failed:
allocator.Free(entries);
return NULL;
return 1;
} /* slbLoadEntries */ } /* slbLoadEntries */
static void *SLB_openArchive(PHYSFS_Io *io, const char *name, int forWriting) static void *SLB_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{ {
PHYSFS_uint32 version; PHYSFS_uint32 version;
PHYSFS_uint32 count = 0; PHYSFS_uint32 count;
PHYSFS_uint32 tocPos = 0; PHYSFS_uint32 tocPos;
UNPKentry *entries = NULL; void *unpkarc;
assert(io != NULL); /* shouldn't ever happen. */ assert(io != NULL); /* shouldn't ever happen. */
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL); BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &version, sizeof(version)), NULL); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &version, sizeof (version)), NULL);
version = PHYSFS_swapULE32(version); version = PHYSFS_swapULE32(version);
BAIL_IF(version != 0, PHYSFS_ERR_UNSUPPORTED, NULL); BAIL_IF(version != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof(count)), NULL); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL);
count = PHYSFS_swapULE32(count); count = PHYSFS_swapULE32(count);
/* offset of the table of contents */ /* offset of the table of contents */
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &tocPos, sizeof(tocPos)), NULL); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &tocPos, sizeof (tocPos)), NULL);
tocPos = PHYSFS_swapULE32(tocPos); tocPos = PHYSFS_swapULE32(tocPos);
/* seek to the table of contents */ /* seek to the table of contents */
BAIL_IF_ERRPASS(!io->seek(io, tocPos), NULL); BAIL_IF_ERRPASS(!io->seek(io, tocPos), NULL);
entries = slbLoadEntries(io, count); unpkarc = UNPK_openArchive(io, count);
BAIL_IF_ERRPASS(!entries, NULL); BAIL_IF_ERRPASS(!unpkarc, NULL);
return UNPK_openArchive(io, entries, count); if (!slbLoadEntries(io, count, unpkarc))
{
UNPK_closeArchive(unpkarc);
return NULL;
} /* if */
return unpkarc;
} /* SLB_openArchive */ } /* SLB_openArchive */
@ -120,4 +121,3 @@ const PHYSFS_Archiver __PHYSFS_Archiver_SLB =
#endif /* defined PHYSFS_SUPPORTS_SLB */ #endif /* defined PHYSFS_SUPPORTS_SLB */
/* end of archiver_slb.c ... */ /* end of archiver_slb.c ... */

View File

@ -5,10 +5,8 @@
* formats that can just hand back a list of files and the offsets of their * formats that can just hand back a list of files and the offsets of their
* uncompressed data. There are an alarming number of formats like this. * uncompressed data. There are an alarming number of formats like this.
* *
* RULES: Archive entries must be uncompressed, must not have separate subdir * RULES: Archive entries must be uncompressed. Dirs and files allowed, but no
* entries (but can have subdirs), must be case insensitive LOW ASCII * symlinks, etc. We can relax some of these rules as necessary.
* filenames <= 64 bytes. No symlinks, etc. We can relax some of these rules
* as necessary.
* *
* Please see the file LICENSE.txt in the source's root directory. * Please see the file LICENSE.txt in the source's root directory.
* *
@ -20,11 +18,16 @@
typedef struct typedef struct
{ {
__PHYSFS_DirTree tree;
PHYSFS_Io *io; PHYSFS_Io *io;
PHYSFS_uint32 entryCount;
UNPKentry *entries;
} UNPKinfo; } UNPKinfo;
typedef struct
{
__PHYSFS_DirTreeEntry tree;
PHYSFS_uint64 startPos;
PHYSFS_uint64 size;
} UNPKentry;
typedef struct typedef struct
{ {
@ -37,9 +40,15 @@ typedef struct
void UNPK_closeArchive(void *opaque) void UNPK_closeArchive(void *opaque)
{ {
UNPKinfo *info = ((UNPKinfo *) opaque); UNPKinfo *info = ((UNPKinfo *) opaque);
info->io->destroy(info->io); if (info)
allocator.Free(info->entries); {
allocator.Free(info); __PHYSFS_DirTreeDeinit(&info->tree);
if (info->io)
info->io->destroy(info->io);
allocator.Free(info);
} /* if */
} /* UNPK_closeArchive */ } /* UNPK_closeArchive */
@ -145,202 +154,9 @@ static const PHYSFS_Io UNPK_Io =
}; };
static int entryCmp(void *_a, size_t one, size_t two) static inline UNPKentry *findEntry(UNPKinfo *info, const char *path)
{ {
if (one != two) return (UNPKentry *) __PHYSFS_DirTreeFind(&info->tree, path);
{
const UNPKentry *a = (const UNPKentry *) _a;
return __PHYSFS_stricmpASCII(a[one].name, a[two].name);
} /* if */
return 0;
} /* entryCmp */
static void entrySwap(void *_a, size_t one, size_t two)
{
if (one != two)
{
UNPKentry tmp;
UNPKentry *first = &(((UNPKentry *) _a)[one]);
UNPKentry *second = &(((UNPKentry *) _a)[two]);
memcpy(&tmp, first, sizeof (UNPKentry));
memcpy(first, second, sizeof (UNPKentry));
memcpy(second, &tmp, sizeof (UNPKentry));
} /* if */
} /* entrySwap */
static PHYSFS_sint32 findStartOfDir(UNPKinfo *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 = (PHYSFS_uint32) 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;
rc = __PHYSFS_strnicmpASCII(path, name, dlen);
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;
} /* findStartOfDir */
/*
* Moved to seperate function so we can use alloca then immediately throw
* away the allocated stack space...
*/
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
const char *odir, const char *str, PHYSFS_sint32 ln)
{
char *newstr = __PHYSFS_smallAlloc(ln + 1);
if (newstr == NULL)
return;
memcpy(newstr, str, ln);
newstr[ln] = '\0';
cb(callbackdata, odir, newstr);
__PHYSFS_smallFree(newstr);
} /* doEnumCallback */
void UNPK_enumerateFiles(void *opaque, const char *dname,
PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
{
UNPKinfo *info = ((UNPKinfo *) opaque);
PHYSFS_sint32 dlen, dlen_inc, max, i;
i = findStartOfDir(info, dname, 0);
if (i == -1) /* no such directory. */
return;
dlen = (PHYSFS_sint32) strlen(dname);
if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
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;
if ((dlen) &&
((__PHYSFS_strnicmpASCII(e, dname, dlen)) || (e[dlen] != '/')))
{
break; /* past end of this dir; we're done. */
} /* if */
add = e + dlen_inc;
ptr = strchr(add, '/');
ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
doEnumCallback(cb, callbackdata, origdir, add, ln);
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;
if ((__PHYSFS_strnicmpASCII(e, e_new, ln) != 0) ||
(e_new[ln] != '/'))
{
break;
} /* if */
} /* while */
} /* while */
} /* UNPK_enumerateFiles */
/*
* This will find the UNPKentry associated with a path in platform-independent
* notation. Directories don't have UNPKentries associated with them, but
* (*isDir) will be set to non-zero if a dir was hit.
*/
static UNPKentry *findEntry(const UNPKinfo *info, const char *path, int *isDir)
{
UNPKentry *a = info->entries;
PHYSFS_sint32 pathlen = (PHYSFS_sint32) 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;
rc = __PHYSFS_strnicmpASCII(path, thispath, pathlen);
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];
/* adjust search params, try again. */
else if (thispath[pathlen] > '/')
hi = middle - 1;
else
lo = middle + 1;
} /* if */
} /* while */
if (isDir != NULL)
*isDir = 0;
BAIL(PHYSFS_ERR_NOT_FOUND, NULL);
} /* findEntry */ } /* findEntry */
@ -349,11 +165,10 @@ PHYSFS_Io *UNPK_openRead(void *opaque, const char *name)
PHYSFS_Io *retval = NULL; PHYSFS_Io *retval = NULL;
UNPKinfo *info = (UNPKinfo *) opaque; UNPKinfo *info = (UNPKinfo *) opaque;
UNPKfileinfo *finfo = NULL; UNPKfileinfo *finfo = NULL;
int isdir = 0; UNPKentry *entry = findEntry(info, name);
UNPKentry *entry = findEntry(info, name, &isdir);
GOTO_IF(isdir, PHYSFS_ERR_NOT_A_FILE, UNPK_openRead_failed); BAIL_IF_ERRPASS(!entry, NULL);
GOTO_IF_ERRPASS(!entry, UNPK_openRead_failed); BAIL_IF(entry->tree.isdir, PHYSFS_ERR_NOT_A_FILE, NULL);
retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io)); retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed); GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
@ -413,25 +228,22 @@ int UNPK_mkdir(void *opaque, const char *name)
} /* UNPK_mkdir */ } /* UNPK_mkdir */
int UNPK_stat(void *opaque, const char *filename, PHYSFS_Stat *stat) int UNPK_stat(void *opaque, const char *path, PHYSFS_Stat *stat)
{ {
int isDir = 0; UNPKinfo *info = (UNPKinfo *) opaque;
const UNPKinfo *info = (const UNPKinfo *) opaque; const UNPKentry *entry = findEntry(info, path);
const UNPKentry *entry = findEntry(info, filename, &isDir);
if (isDir) BAIL_IF_ERRPASS(!entry, 0);
if (entry->tree.isdir)
{ {
stat->filetype = PHYSFS_FILETYPE_DIRECTORY; stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
stat->filesize = 0; stat->filesize = 0;
} /* if */ } /* if */
else if (entry != NULL) else
{ {
stat->filetype = PHYSFS_FILETYPE_REGULAR; stat->filetype = PHYSFS_FILETYPE_REGULAR;
stat->filesize = entry->size; stat->filesize = entry->size;
} /* else if */
else
{
return 0;
} /* else */ } /* else */
stat->modtime = -1; stat->modtime = -1;
@ -443,19 +255,34 @@ int UNPK_stat(void *opaque, const char *filename, PHYSFS_Stat *stat)
} /* UNPK_stat */ } /* UNPK_stat */
void *UNPK_openArchive(PHYSFS_Io *io, UNPKentry *e, const PHYSFS_uint32 num) void *UNPK_addEntry(void *opaque, char *name, const int isdir,
const PHYSFS_uint64 pos, const PHYSFS_uint64 len)
{
UNPKinfo *info = (UNPKinfo *) opaque;
UNPKentry *entry;
entry = (UNPKentry *) __PHYSFS_DirTreeAdd(&info->tree, name, isdir);
BAIL_IF_ERRPASS(!entry, NULL);
entry->startPos = pos;
entry->size = len;
return entry;
} /* UNPK_addEntry */
void *UNPK_openArchive(PHYSFS_Io *io, const PHYSFS_uint64 entry_count)
{ {
UNPKinfo *info = (UNPKinfo *) allocator.Malloc(sizeof (UNPKinfo)); UNPKinfo *info = (UNPKinfo *) allocator.Malloc(sizeof (UNPKinfo));
if (info == NULL) BAIL_IF(!info, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
if (!__PHYSFS_DirTreeInit(&info->tree, entry_count, sizeof (UNPKentry)))
{ {
allocator.Free(e); allocator.Free(info);
BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL); return NULL;
} /* if */ } /* if */
__PHYSFS_sort(e, (size_t) num, entryCmp, entrySwap);
info->io = io; info->io = io;
info->entryCount = num;
info->entries = e;
return info; return info;
} /* UNPK_openArchive */ } /* UNPK_openArchive */

View File

@ -47,44 +47,35 @@
#if PHYSFS_SUPPORTS_WAD #if PHYSFS_SUPPORTS_WAD
static UNPKentry *wadLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount) static int wadLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
{ {
PHYSFS_uint32 directoryOffset; PHYSFS_uint32 i;
UNPKentry *entries = NULL; for (i = 0; i < count; i++)
UNPKentry *entry = NULL;
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &directoryOffset, 4), 0);
directoryOffset = PHYSFS_swapULE32(directoryOffset);
BAIL_IF_ERRPASS(!io->seek(io, directoryOffset), 0);
entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
BAIL_IF(!entries, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
for (entry = entries; fileCount > 0; fileCount--, entry++)
{ {
if (!__PHYSFS_readAll(io, &entry->startPos, 4)) goto failed; PHYSFS_uint32 location;
if (!__PHYSFS_readAll(io, &entry->size, 4)) goto failed; PHYSFS_uint32 size;
if (!__PHYSFS_readAll(io, &entry->name, 8)) goto failed; char name[9];
entry->name[8] = '\0'; /* name might not be null-terminated in file. */ BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &location, 4), 0);
entry->size = PHYSFS_swapULE32(entry->size); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
entry->startPos = PHYSFS_swapULE32(entry->startPos); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 8), 0);
name[8] = '\0'; /* name might not be null-terminated in file. */
size = PHYSFS_swapULE32(size);
location = PHYSFS_swapULE32(location);
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, location, size), 0);
} /* for */ } /* for */
return entries; return 1;
failed:
allocator.Free(entries);
return NULL;
} /* wadLoadEntries */ } /* wadLoadEntries */
static void *WAD_openArchive(PHYSFS_Io *io, const char *name, int forWriting) static void *WAD_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{ {
PHYSFS_uint8 buf[4]; PHYSFS_uint8 buf[4];
UNPKentry *entries = NULL; PHYSFS_uint32 count;
PHYSFS_uint32 count = 0; PHYSFS_uint32 directoryOffset;
void *unpkarc;
assert(io != NULL); /* shouldn't ever happen. */ assert(io != NULL); /* shouldn't ever happen. */
@ -96,9 +87,21 @@ static void *WAD_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL);
count = PHYSFS_swapULE32(count); count = PHYSFS_swapULE32(count);
entries = wadLoadEntries(io, count); BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &directoryOffset, 4), 0);
BAIL_IF_ERRPASS(!entries, NULL); directoryOffset = PHYSFS_swapULE32(directoryOffset);
return UNPK_openArchive(io, entries, count);
BAIL_IF_ERRPASS(!io->seek(io, directoryOffset), 0);
unpkarc = UNPK_openArchive(io, count);
BAIL_IF_ERRPASS(!unpkarc, NULL);
if (!wadLoadEntries(io, count, unpkarc))
{
UNPK_closeArchive(unpkarc);
return NULL;
} /* if */
return unpkarc;
} /* WAD_openArchive */ } /* WAD_openArchive */

View File

@ -342,24 +342,17 @@ int __PHYSFS_readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len);
/* These are shared between some archivers. */ /* These are shared between some archivers. */
typedef struct
{
char name[64];
PHYSFS_uint32 startPos;
PHYSFS_uint32 size;
} UNPKentry;
void UNPK_closeArchive(void *opaque); void UNPK_closeArchive(void *opaque);
void *UNPK_openArchive(PHYSFS_Io *io,UNPKentry *e,const PHYSFS_uint32 n); void *UNPK_openArchive(PHYSFS_Io *io, const PHYSFS_uint64 entry_count);
void UNPK_enumerateFiles(void *opaque, const char *dname, void *UNPK_addEntry(void *opaque, char *name, const int isdir,
PHYSFS_EnumFilesCallback cb, const PHYSFS_uint64 pos, const PHYSFS_uint64 len);
const char *origdir, void *callbackdata);
PHYSFS_Io *UNPK_openRead(void *opaque, const char *name); PHYSFS_Io *UNPK_openRead(void *opaque, const char *name);
PHYSFS_Io *UNPK_openWrite(void *opaque, const char *name); PHYSFS_Io *UNPK_openWrite(void *opaque, const char *name);
PHYSFS_Io *UNPK_openAppend(void *opaque, const char *name); PHYSFS_Io *UNPK_openAppend(void *opaque, const char *name);
int UNPK_remove(void *opaque, const char *name); int UNPK_remove(void *opaque, const char *name);
int UNPK_mkdir(void *opaque, const char *name); int UNPK_mkdir(void *opaque, const char *name);
int UNPK_stat(void *opaque, const char *fn, PHYSFS_Stat *st); int UNPK_stat(void *opaque, const char *fn, PHYSFS_Stat *st);
#define UNPK_enumerateFiles __PHYSFS_DirTreeEnumerateFiles