Skip to content
Permalink
Browse files
Cleaned up all the readAll() cut and paste.
  • Loading branch information
icculus committed Mar 9, 2012
1 parent 25224b0 commit a0b21c9ae9b844dc0746b2f1db4824a598b1c2de
Showing with 62 additions and 86 deletions.
  1. +0 −1 docs/TODO.txt
  2. +9 −14 src/archiver_grp.c
  3. +9 −14 src/archiver_hog.c
  4. +9 −16 src/archiver_mvl.c
  5. +6 −12 src/archiver_qpak.c
  6. +11 −17 src/archiver_wad.c
  7. +7 −12 src/archiver_zip.c
  8. +6 −0 src/physfs.c
  9. +5 −0 src/physfs_internal.h
@@ -82,7 +82,6 @@ Other stuff I thought of...
- Doxygen replacement? (manpages suck.)
- Fix coding standards to match.
- See if we can ditch some #include lines...
- push readAll() to somewhere common.
- We lost Vista symlink support when removing isSymLink(). Pull it back from
revision control.
- PHYSFS_exists() fails if you mountIo with a NULL filename. We need to decide
@@ -29,11 +29,6 @@
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"

static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
{
return (io->read(io, buf, len) == len);
} /* readAll */

static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
{
PHYSFS_uint32 location = 16; /* sizeof sig. */
@@ -48,8 +43,8 @@ static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)

for (entry = entries; fileCount > 0; fileCount--, entry++)
{
GOTO_IF_MACRO(!readAll(io, &entry->name, 12), NULL, grpLoad_failed);
GOTO_IF_MACRO(!readAll(io, &entry->size, 4), NULL, grpLoad_failed);
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 12), NULL, failed);
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), NULL, failed);
entry->name[12] = '\0'; /* name isn't null-terminated in file. */
if ((ptr = strchr(entry->name, ' ')) != NULL)
*ptr = '\0'; /* trim extra spaces. */
@@ -61,7 +56,7 @@ static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)

return entries;

grpLoad_failed:
failed:
allocator.Free(entries);
return NULL;
} /* grpLoadEntries */
@@ -70,23 +65,23 @@ static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
static void *GRP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{
PHYSFS_uint8 buf[12];
PHYSFS_uint32 entryCount = 0;
PHYSFS_uint32 count = 0;
UNPKentry *entries = NULL;

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

BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);

BAIL_IF_MACRO(!readAll(io, buf, sizeof (buf)), NULL, NULL);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, sizeof (buf)), NULL, NULL);
if (memcmp(buf, "KenSilverman", sizeof (buf)) != 0)
BAIL_MACRO(ERR_NOT_AN_ARCHIVE, NULL);

BAIL_IF_MACRO(!readAll(io, &entryCount, sizeof (entryCount)), NULL, NULL);
entryCount = PHYSFS_swapULE32(entryCount);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL, NULL);
count = PHYSFS_swapULE32(count);

entries = grpLoadEntries(io, entryCount);
entries = grpLoadEntries(io, count);
BAIL_IF_MACRO(entries == NULL, NULL, NULL);
return UNPK_openArchive(io, entries, entryCount);
return UNPK_openArchive(io, entries, count);
} /* GRP_openArchive */


@@ -34,11 +34,6 @@
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"

static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
{
return (io->read(io, buf, len) == len);
} /* readAll */

static UNPKentry *hogLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 *_entCount)
{
const PHYSFS_uint64 iolen = io->length(io);
@@ -53,27 +48,27 @@ static UNPKentry *hogLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 *_entCount)
{
entCount++;
ptr = allocator.Realloc(ptr, sizeof (UNPKentry) * entCount);
GOTO_IF_MACRO(ptr == NULL, ERR_OUT_OF_MEMORY, hogLoad_failed);
GOTO_IF_MACRO(ptr == NULL, ERR_OUT_OF_MEMORY, failed);
entries = (UNPKentry *) ptr;
entry = &entries[entCount-1];

GOTO_IF_MACRO(!readAll(io, &entry->name, 13), NULL, hogLoad_failed);
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 13), NULL, failed);
pos += 13;
GOTO_IF_MACRO(!readAll(io, &size, 4), NULL, hogLoad_failed);
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &size, 4), NULL, failed);
pos += 4;

entry->size = PHYSFS_swapULE32(size);
entry->startPos = pos;
pos += size;

/* skip over entry */
GOTO_IF_MACRO(!io->seek(io, pos), NULL, hogLoad_failed);
GOTO_IF_MACRO(!io->seek(io, pos), NULL, failed);
} /* while */

*_entCount = entCount;
return entries;

hogLoad_failed:
failed:
allocator.Free(entries);
return NULL;
} /* hogLoadEntries */
@@ -82,17 +77,17 @@ static UNPKentry *hogLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 *_entCount)
static void *HOG_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{
PHYSFS_uint8 buf[3];
PHYSFS_uint32 entryCount = 0;
PHYSFS_uint32 count = 0;
UNPKentry *entries = NULL;

assert(io != NULL); /* shouldn't ever happen. */
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
BAIL_IF_MACRO(!readAll(io, buf, 3), NULL, 0);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, 3), NULL, 0);
BAIL_IF_MACRO(memcmp(buf, "DHF", 3) != 0, ERR_NOT_AN_ARCHIVE, NULL);

entries = hogLoadEntries(io, &entryCount);
entries = hogLoadEntries(io, &count);
BAIL_IF_MACRO(entries == NULL, NULL, NULL);
return UNPK_openArchive(io, entries, entryCount);
return UNPK_openArchive(io, entries, count);
} /* HOG_openArchive */


@@ -32,13 +32,6 @@
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"


static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
{
return (io->read(io, buf, len) == len);
} /* readAll */


static UNPKentry *mvlLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
{
PHYSFS_uint32 location = 8; /* sizeof sig. */
@@ -52,16 +45,16 @@ static UNPKentry *mvlLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)

for (entry = entries; fileCount > 0; fileCount--, entry++)
{
GOTO_IF_MACRO(!readAll(io, &entry->name, 13), NULL, mvlLoad_failed);
GOTO_IF_MACRO(!readAll(io, &entry->size, 4), NULL, mvlLoad_failed);
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 13), NULL, failed);
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), NULL, failed);
entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = location;
location += entry->size;
} /* for */

return entries;

mvlLoad_failed:
failed:
allocator.Free(entries);
return NULL;
} /* mvlLoadEntries */
@@ -70,19 +63,19 @@ static UNPKentry *mvlLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
static void *MVL_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{
PHYSFS_uint8 buf[4];
PHYSFS_uint32 entryCount = 0;
PHYSFS_uint32 count = 0;
UNPKentry *entries = NULL;

assert(io != NULL); /* shouldn't ever happen. */
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
BAIL_IF_MACRO(!readAll(io, buf, 4), NULL, NULL);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, 4), NULL, NULL);
BAIL_IF_MACRO(memcmp(buf, "DMVL", 4) != 0, ERR_NOT_AN_ARCHIVE, NULL);
BAIL_IF_MACRO(!readAll(io, &entryCount, sizeof (entryCount)), NULL, NULL);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL, NULL);

entryCount = PHYSFS_swapULE32(entryCount);
entries = mvlLoadEntries(io, entryCount);
count = PHYSFS_swapULE32(count);
entries = mvlLoadEntries(io, count);
BAIL_IF_MACRO(entries == NULL, NULL, NULL);
return UNPK_openArchive(io, entries, entryCount);
return UNPK_openArchive(io, entries, count);
} /* MVL_openArchive */


@@ -69,12 +69,6 @@ typedef struct
#define QPAK_SIG 0x4b434150 /* "PACK" in ASCII. */


static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
{
return (io->read(io, buf, len) == len);
} /* readAll */


static void QPAK_dirClose(dvoid *opaque)
{
QPAKinfo *info = ((QPAKinfo *) opaque);
@@ -225,9 +219,9 @@ static int qpak_load_entries(QPAKinfo *info)

for (entry = info->entries; fileCount > 0; fileCount--, entry++)
{
BAIL_IF_MACRO(!readAll(io, &entry->name, 56), NULL, 0);
BAIL_IF_MACRO(!readAll(io, &entry->startPos, 4), NULL, 0);
BAIL_IF_MACRO(!readAll(io, &entry->size, 4), NULL, 0);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 56), NULL, 0);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &entry->startPos, 4), NULL, 0);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), NULL, 0);
entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = PHYSFS_swapULE32(entry->startPos);
} /* for */
@@ -248,13 +242,13 @@ static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting)

BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);

BAIL_IF_MACRO(!readAll(io, &val, 4), NULL, NULL);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), NULL, NULL);
BAIL_IF_MACRO(PHYSFS_swapULE32(val) != QPAK_SIG, ERR_NOT_AN_ARCHIVE, NULL);

BAIL_IF_MACRO(!readAll(io, &val, 4), NULL, NULL);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), NULL, NULL);
pos = PHYSFS_swapULE32(val); /* directory table offset. */

BAIL_IF_MACRO(!readAll(io, &val, 4), NULL, NULL);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), NULL, NULL);
count = PHYSFS_swapULE32(val);

/* corrupted archive? */
@@ -47,19 +47,13 @@
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"

static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
{
return (io->read(io, buf, len) == len);
} /* readAll */


static UNPKentry *wadLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
{
PHYSFS_uint32 directoryOffset;
UNPKentry *entries = NULL;
UNPKentry *entry = NULL;

BAIL_IF_MACRO(!readAll(io, &directoryOffset, 4), NULL, 0);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &directoryOffset, 4), NULL, 0);
directoryOffset = PHYSFS_swapULE32(directoryOffset);

BAIL_IF_MACRO(!io->seek(io, directoryOffset), NULL, 0);
@@ -69,9 +63,9 @@ static UNPKentry *wadLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)

for (entry = entries; fileCount > 0; fileCount--, entry++)
{
GOTO_IF_MACRO(!readAll(io, &entry->startPos, 4), NULL, wadLoad_failed);
GOTO_IF_MACRO(!readAll(io, &entry->size, 4), NULL, wadLoad_failed);
GOTO_IF_MACRO(!readAll(io, &entry->name, 8), NULL, wadLoad_failed);
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->startPos, 4), NULL, failed);
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), NULL, failed);
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 8), NULL, failed);

entry->name[8] = '\0'; /* name might not be null-terminated in file. */
entry->size = PHYSFS_swapULE32(entry->size);
@@ -80,7 +74,7 @@ static UNPKentry *wadLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)

return entries;

wadLoad_failed:
failed:
allocator.Free(entries);
return NULL;
} /* wadLoadEntries */
@@ -90,21 +84,21 @@ static void *WAD_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{
PHYSFS_uint8 buf[4];
UNPKentry *entries = NULL;
PHYSFS_uint32 entryCount = 0;
PHYSFS_uint32 count = 0;

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

BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
BAIL_IF_MACRO(!readAll(io, buf, sizeof (buf)), NULL, NULL);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, sizeof (buf)), NULL, NULL);
if ((memcmp(buf, "IWAD", 4) != 0) && (memcmp(buf, "PWAD", 4) != 0))
BAIL_MACRO(ERR_NOT_AN_ARCHIVE, NULL);

BAIL_IF_MACRO(!readAll(io, &entryCount, sizeof (entryCount)), NULL, NULL);
entryCount = PHYSFS_swapULE32(entryCount);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL, NULL);
count = PHYSFS_swapULE32(count);

entries = wadLoadEntries(io, entryCount);
entries = wadLoadEntries(io, count);
BAIL_IF_MACRO(entries == NULL, NULL, NULL);
return UNPK_openArchive(io, entries, entryCount);
return UNPK_openArchive(io, entries, count);
} /* WAD_openArchive */


@@ -169,18 +169,13 @@ static int zlib_err(int rc)
} /* zlib_err */


static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
{
return (io->read(io, buf, len) == len);
} /* readAll */

/*
* Read an unsigned 32-bit int and swap to native byte order.
*/
static int readui32(PHYSFS_Io *io, PHYSFS_uint32 *val)
{
PHYSFS_uint32 v;
BAIL_IF_MACRO(!readAll(io, &v, sizeof (v)), NULL, 0);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &v, sizeof (v)), NULL, 0);
*val = PHYSFS_swapULE32(v);
return 1;
} /* readui32 */
@@ -192,7 +187,7 @@ static int readui32(PHYSFS_Io *io, PHYSFS_uint32 *val)
static int readui16(PHYSFS_Io *io, PHYSFS_uint16 *val)
{
PHYSFS_uint16 v;
BAIL_IF_MACRO(!readAll(io, &v, sizeof (v)), NULL, 0);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &v, sizeof (v)), NULL, 0);
*val = PHYSFS_swapULE16(v);
return 1;
} /* readui16 */
@@ -462,14 +457,14 @@ static PHYSFS_sint64 zip_find_end_of_central_dir(PHYSFS_Io *io, PHYSFS_sint64 *l
/* make sure we catch a signature between buffers. */
if (totalread != 0)
{
if (!readAll(io, buf, maxread - 4))
if (!__PHYSFS_readAll(io, buf, maxread - 4))
return -1;
memcpy(&buf[maxread - 4], &extra, sizeof (extra));
totalread += maxread - 4;
} /* if */
else
{
if (!readAll(io, buf, maxread))
if (!__PHYSFS_readAll(io, buf, maxread))
return -1;
totalread += maxread;
} /* else */
@@ -724,7 +719,7 @@ static int zip_resolve_symlink(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry)
BAIL_IF_MACRO(path == NULL, ERR_OUT_OF_MEMORY, 0);

if (entry->compression_method == COMPMETH_NONE)
rc = readAll(io, path, size);
rc = __PHYSFS_readAll(io, path, size);

else /* symlink target path is compressed... */
{
@@ -733,7 +728,7 @@ static int zip_resolve_symlink(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry)
PHYSFS_uint8 *compressed = (PHYSFS_uint8*) __PHYSFS_smallAlloc(complen);
if (compressed != NULL)
{
if (readAll(io, compressed, complen))
if (__PHYSFS_readAll(io, compressed, complen))
{
initializeZStream(&stream);
stream.next_in = compressed;
@@ -970,7 +965,7 @@ static int zip_load_entry(PHYSFS_Io *io, ZIPentry *entry, PHYSFS_uint32 ofs_fixu

entry->name = (char *) allocator.Malloc(fnamelen + 1);
BAIL_IF_MACRO(entry->name == NULL, ERR_OUT_OF_MEMORY, 0);
if (!readAll(io, entry->name, fnamelen))
if (!__PHYSFS_readAll(io, entry->name, fnamelen))
goto zip_load_entry_puked;

entry->name[fnamelen] = '\0'; /* null-terminate the filename. */
@@ -2777,5 +2777,11 @@ void __PHYSFS_smallFree(void *ptr)
} /* if */
} /* __PHYSFS_smallFree */


int __PHYSFS_readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
{
return (io->read(io, buf, len) == len);
} /* __PHYSFS_readAll */

/* end of physfs.c ... */

0 comments on commit a0b21c9

Please sign in to comment.