Skip to content

Commit

Permalink
Cleaned up all the readAll() cut and paste.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Mar 9, 2012
1 parent 25224b0 commit a0b21c9
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 86 deletions.
1 change: 0 additions & 1 deletion docs/TODO.txt
Expand Up @@ -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
Expand Down
23 changes: 9 additions & 14 deletions src/archiver_grp.c
Expand Up @@ -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. */
Expand All @@ -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. */
Expand All @@ -61,7 +56,7 @@ static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)

return entries;

grpLoad_failed:
failed:
allocator.Free(entries);
return NULL;
} /* grpLoadEntries */
Expand All @@ -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 */


Expand Down
23 changes: 9 additions & 14 deletions src/archiver_hog.c
Expand Up @@ -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);
Expand All @@ -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 */
Expand All @@ -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 */


Expand Down
25 changes: 9 additions & 16 deletions src/archiver_mvl.c
Expand Up @@ -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. */
Expand All @@ -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 */
Expand All @@ -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 */


Expand Down
18 changes: 6 additions & 12 deletions src/archiver_qpak.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand All @@ -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? */
Expand Down
28 changes: 11 additions & 17 deletions src/archiver_wad.c
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -80,7 +74,7 @@ static UNPKentry *wadLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)

return entries;

wadLoad_failed:
failed:
allocator.Free(entries);
return NULL;
} /* wadLoadEntries */
Expand All @@ -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 */


Expand Down
19 changes: 7 additions & 12 deletions src/archiver_zip.c
Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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... */
{
Expand All @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down
6 changes: 6 additions & 0 deletions src/physfs.c
Expand Up @@ -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.