Skip to content

Commit

Permalink
Reworked the "unpacked" archivers to use DirTree.
Browse files Browse the repository at this point in the history
This cleaned up a lot of code and improved things, and also allowed a lot of
the restrictions on unpacked archivers to be removed.
  • Loading branch information
icculus committed Jul 16, 2017
1 parent ffa1836 commit 62ad133
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 403 deletions.
55 changes: 28 additions & 27 deletions src/archiver_grp.c
Expand Up @@ -29,44 +29,38 @@

#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. */
UNPKentry *entries = NULL;
UNPKentry *entry = NULL;
char *ptr = NULL;
PHYSFS_uint32 location = 16 + (16 * count); /* past sig+metadata. */
PHYSFS_uint32 i;

entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
BAIL_IF(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);

location += (16 * fileCount);

for (entry = entries; fileCount > 0; fileCount--, entry++)
for (i = 0; i < count; i++)
{
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->name, 12), failed);
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->size, 4), failed);
entry->name[12] = '\0'; /* name isn't null-terminated in file. */
if ((ptr = strchr(entry->name, ' ')) != NULL)
char *ptr;
char name[13];
PHYSFS_uint32 size;
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. */

entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = location;
location += entry->size;
} /* for */
size = PHYSFS_swapULE32(size);
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, location, size), 0);

return entries;
location += size;
} /* for */

failed:
allocator.Free(entries);
return NULL;
return 1;
} /* grpLoadEntries */


static void *GRP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{
PHYSFS_uint8 buf[12];
PHYSFS_uint32 count = 0;
UNPKentry *entries = NULL;
void *unpkarc = NULL;

assert(io != NULL); /* shouldn't ever happen. */

Expand All @@ -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);
count = PHYSFS_swapULE32(count);

entries = grpLoadEntries(io, count);
BAIL_IF_ERRPASS(!entries, NULL);
return UNPK_openArchive(io, entries, count);
unpkarc = UNPK_openArchive(io, count);
BAIL_IF_ERRPASS(!unpkarc, NULL);

if (!grpLoadEntries(io, count, unpkarc))
{
UNPK_closeArchive(unpkarc);
return NULL;
} /* if */

return unpkarc;
} /* GRP_openArchive */


Expand Down
54 changes: 24 additions & 30 deletions src/archiver_hog.c
Expand Up @@ -34,60 +34,54 @@

#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);
PHYSFS_uint32 entCount = 0;
void *ptr = NULL;
UNPKentry *entries = NULL;
UNPKentry *entry = NULL;
PHYSFS_uint32 size = 0;
PHYSFS_uint32 pos = 3;

while (pos < iolen)
{
entCount++;
ptr = allocator.Realloc(ptr, sizeof (UNPKentry) * entCount);
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);
pos += 13;
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), failed);
pos += 4;

entry->size = PHYSFS_swapULE32(size);
entry->startPos = pos;
PHYSFS_uint32 size;
char name[13];

BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 13), 0);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
name[12] = '\0'; /* just in case. */
pos += 13 + 4;

size = PHYSFS_swapULE32(size);
BAIL_IF_ERRPASS(!UNPK_addEntry(unpkarc, name, 0, pos, size), 0);
pos += size;

/* skip over entry */
GOTO_IF_ERRPASS(!io->seek(io, pos), failed);
BAIL_IF_ERRPASS(!io->seek(io, pos), 0);
} /* while */

*_entCount = entCount;
return entries;

failed:
allocator.Free(entries);
return NULL;
return 1;
} /* hogLoadEntries */


static void *HOG_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{
PHYSFS_uint8 buf[3];
PHYSFS_uint32 count = 0;
UNPKentry *entries = NULL;
void *unpkarc = NULL;

assert(io != NULL); /* shouldn't ever happen. */
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, 3), NULL);
BAIL_IF(memcmp(buf, "DHF", 3) != 0, PHYSFS_ERR_UNSUPPORTED, NULL);

entries = hogLoadEntries(io, &count);
BAIL_IF_ERRPASS(!entries, NULL);
return UNPK_openArchive(io, entries, count);
unpkarc = UNPK_openArchive(io, count);
BAIL_IF_ERRPASS(!unpkarc, NULL);

if (!hogLoadEntries(io, unpkarc))
{
UNPK_closeArchive(unpkarc);
return NULL;
} /* if */

return unpkarc;
} /* HOG_openArchive */


Expand Down
48 changes: 25 additions & 23 deletions src/archiver_mvl.c
Expand Up @@ -32,39 +32,32 @@

#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. */
UNPKentry *entries = NULL;
UNPKentry *entry = NULL;
PHYSFS_uint32 location = 8 + (17 * count); /* past sig+metadata. */
PHYSFS_uint32 i;

entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
BAIL_IF(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);

location += (17 * fileCount);

for (entry = entries; fileCount > 0; fileCount--, entry++)
for (i = 0; i < count; i++)
{
if (!__PHYSFS_readAll(io, &entry->name, 13)) goto failed;
if (!__PHYSFS_readAll(io, &entry->size, 4)) goto failed;
entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = location;
location += entry->size;
PHYSFS_uint32 size;
char name[13];
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 13), 0);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
name[12] = '\0'; /* just in case. */
size = PHYSFS_swapULE32(size);
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, location, size), 0);
location += size;
} /* for */

return entries;

failed:
allocator.Free(entries);
return NULL;
return 1;
} /* mvlLoadEntries */


static void *MVL_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{
PHYSFS_uint8 buf[4];
PHYSFS_uint32 count = 0;
UNPKentry *entries = NULL;
void *unpkarc;

assert(io != NULL); /* shouldn't ever happen. */
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
Expand All @@ -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);

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 */


Expand Down
46 changes: 24 additions & 22 deletions src/archiver_qpak.c
Expand Up @@ -36,37 +36,32 @@

#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;
UNPKentry *entry = NULL;

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 i;
for (i = 0; i < count; i++)
{
if (!__PHYSFS_readAll(io, &entry->name, 56)) goto failed;
if (!__PHYSFS_readAll(io, &entry->startPos, 4)) goto failed;
if (!__PHYSFS_readAll(io, &entry->size, 4)) goto failed;
entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = PHYSFS_swapULE32(entry->startPos);
PHYSFS_uint32 size;
PHYSFS_uint32 location;
char name[56];
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 56), 0);
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 */

return entries;

failed:
allocator.Free(entries);
return NULL;
return 1;
} /* qpakLoadEntries */


static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{
UNPKentry *entries = NULL;
PHYSFS_uint32 val = 0;
PHYSFS_uint32 pos = 0;
PHYSFS_uint32 count = 0;
void *unpkarc;

assert(io != NULL); /* shouldn't ever happen. */

Expand All @@ -88,9 +83,16 @@ static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting)

BAIL_IF_ERRPASS(!io->seek(io, pos), NULL);

entries = qpakLoadEntries(io, count);
BAIL_IF_ERRPASS(!entries, NULL);
return UNPK_openArchive(io, entries, count);
unpkarc = UNPK_openArchive(io, count);
BAIL_IF_ERRPASS(!unpkarc, NULL);

if (!qpakLoadEntries(io, count, unpkarc))
{
UNPK_closeArchive(unpkarc);
return NULL;
} /* if */

return unpkarc;
} /* QPAK_openArchive */


Expand Down

0 comments on commit 62ad133

Please sign in to comment.