Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added typedefs and platform-specific i/o.
  • Loading branch information
icculus committed Mar 24, 2002
1 parent 3b79873 commit f2887cf
Show file tree
Hide file tree
Showing 12 changed files with 452 additions and 145 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG
Expand Up @@ -70,7 +70,11 @@
"write" functions to get data from a "const" buffer. Added an
"extras" dir, which currently contains PhysFS->SDL_RWops glue code.
03202002 - Patched platform/win32.c to compile.
03242002 - Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit().
03242002 - Added __PHYSFS_platformInit() and __PHYSFS_platformDeinit(). Win32
improvements by Gregory S. Read. Added PHYSFS_[us]int(8|16|32)
types...this breaks binary compatibility with previous PhysicsFS
releases! Added platform specific i/o functions, so we don't have
to rely on stdio anymore.

--ryan. (icculus@clutteredmind.org)

Expand Down
3 changes: 2 additions & 1 deletion Makefile
Expand Up @@ -136,7 +136,8 @@ BINDIR := bin
SRCDIR := .

CFLAGS += $(use_asm) -I$(SRCDIR) -I/usr/include/readline -D_REENTRANT -fsigned-char -DPLATFORM_UNIX
CFLAGS += -Wall -Werror -fno-exceptions -fno-rtti -ansi -pedantic
CFLAGS += -Wall -Werror -fno-exceptions -fno-rtti -ansi
#-pedantic

LDFLAGS += -lm

Expand Down
28 changes: 14 additions & 14 deletions archivers/dir.c
Expand Up @@ -16,14 +16,14 @@
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"

static int DIR_read(FileHandle *handle, void *buffer,
unsigned int objSize, unsigned int objCount);
static int DIR_write(FileHandle *handle, const void *buffer,
unsigned int objSize, unsigned int objCount);
static PHYSFS_sint64 DIR_read(FileHandle *handle, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
static PHYSFS_sint64 DIR_write(FileHandle *handle, const void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
static int DIR_eof(FileHandle *handle);
static int DIR_tell(FileHandle *handle);
static int DIR_seek(FileHandle *handle, int offset);
static int DIR_fileLength(FileHandle *handle);
static PHYSFS_sint64 DIR_tell(FileHandle *handle);
static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset);
static PHYSFS_sint64 DIR_fileLength(FileHandle *handle);
static int DIR_fileClose(FileHandle *handle);
static int DIR_isArchive(const char *filename, int forWriting);
static DirHandle *DIR_openArchive(const char *name, int forWriting);
Expand Down Expand Up @@ -94,8 +94,8 @@ const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
#endif


static int DIR_read(FileHandle *handle, void *buffer,
unsigned int objSize, unsigned int objCount)
static PHYSFS_sint64 DIR_read(FileHandle *handle, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
{
FILE *h = (FILE *) (handle->opaque);
size_t retval;
Expand All @@ -109,8 +109,8 @@ static int DIR_read(FileHandle *handle, void *buffer,
} /* DIR_read */


static int DIR_write(FileHandle *handle, const void *buffer,
unsigned int objSize, unsigned int objCount)
static PHYSFS_sint64 DIR_write(FileHandle *handle, const void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
{
FILE *h = (FILE *) (handle->opaque);
size_t retval;
Expand All @@ -130,19 +130,19 @@ static int DIR_eof(FileHandle *handle)
} /* DIR_eof */


static int DIR_tell(FileHandle *handle)
static PHYSFS_sint64 DIR_tell(FileHandle *handle)
{
return(ftell((FILE *) (handle->opaque)));
} /* DIR_tell */


static int DIR_seek(FileHandle *handle, int offset)
static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset)
{
return(fseek((FILE *) (handle->opaque), offset, SEEK_SET) == 0);
} /* DIR_seek */


static int DIR_fileLength(FileHandle *handle)
static PHYSFS_sint64 DIR_fileLength(FileHandle *handle)
{
return(__PHYSFS_platformFileLength((FILE *) (handle->opaque)));
} /* DIR_fileLength */
Expand Down
65 changes: 31 additions & 34 deletions archivers/grp.c
Expand Up @@ -39,35 +39,33 @@
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"

#if 0
#if (!defined PHYSFS_SUPPORTS_GRP)
#error PHYSFS_SUPPORTS_GRP must be defined.
#endif
#endif

/* !!! FIXME: Using the same file handle for all reads is a RACE CONDITION! */

typedef struct
{
FILE *handle;
int totalEntries;
PHYSFS_uint32 totalEntries;
} GRPinfo;

typedef struct
{
int startPos;
int curPos;
int size;
PHYSFS_uint32 startPos;
PHYSFS_uint32 curPos;
PHYSFS_uint32 size;
} GRPfileinfo;


static void GRP_dirClose(DirHandle *h);
static int GRP_read(FileHandle *handle, void *buffer,
unsigned int objSize, unsigned int objCount);
static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
static int GRP_eof(FileHandle *handle);
static int GRP_tell(FileHandle *handle);
static int GRP_seek(FileHandle *handle, int offset);
static int GRP_fileLength(FileHandle *handle);
static PHYSFS_sint64 GRP_tell(FileHandle *handle);
static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset);
static PHYSFS_sint64 GRP_fileLength(FileHandle *handle);
static int GRP_fileClose(FileHandle *handle);
static int GRP_isArchive(const char *filename, int forWriting);
static DirHandle *GRP_openArchive(const char *name, int forWriting);
Expand Down Expand Up @@ -125,13 +123,13 @@ static void GRP_dirClose(DirHandle *h)
} /* GRP_dirClose */


static int GRP_read(FileHandle *handle, void *buffer,
unsigned int objSize, unsigned int objCount)
static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
{
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
FILE *fh = (FILE *) (((GRPinfo *) (handle->dirHandle->opaque))->handle);
int bytesLeft = (finfo->startPos + finfo->size) - finfo->curPos;
unsigned int objsLeft = bytesLeft / objSize;
PHYSFS_uint32 bytesLeft = (finfo->startPos + finfo->size) - finfo->curPos;
PHYSFS_uint32 objsLeft = bytesLeft / objSize;
size_t retval = 0;

if (objsLeft < objCount)
Expand All @@ -144,9 +142,9 @@ static int GRP_read(FileHandle *handle, void *buffer,
retval = fread(buffer, objSize, objCount, fh);
finfo->curPos += (retval * objSize);
BAIL_IF_MACRO((retval < (size_t) objCount) && (ferror(fh)),
strerror(errno), (int) retval);
strerror(errno), (PHYSFS_sint64) retval);

return((int) retval);
return((PHYSFS_sint64) retval);
} /* GRP_read */


Expand All @@ -157,14 +155,14 @@ static int GRP_eof(FileHandle *handle)
} /* GRP_eof */


static int GRP_tell(FileHandle *handle)
static PHYSFS_sint64 GRP_tell(FileHandle *handle)
{
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
return(finfo->curPos - finfo->startPos);
} /* GRP_tell */


static int GRP_seek(FileHandle *handle, int offset)
static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset)
{
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
int newPos = finfo->startPos + offset;
Expand All @@ -176,7 +174,7 @@ static int GRP_seek(FileHandle *handle, int offset)
} /* GRP_seek */


static int GRP_fileLength(FileHandle *handle)
static PHYSFS_sint64 GRP_fileLength(FileHandle *handle)
{
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
return(finfo->size);
Expand All @@ -191,14 +189,10 @@ static int GRP_fileClose(FileHandle *handle)
} /* GRP_fileClose */


static int openGrp(const char *filename, int forWriting, FILE **fh, int *count)
static int openGrp(const char *filename, int forWriting, FILE **fh, PHYSFS_sint32 *count)
{
char buf[12];

/* !!! FIXME: Get me platform-independent typedefs! */
if (sizeof (int) != 4)
assert(0);

*fh = NULL;
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);

Expand All @@ -208,10 +202,10 @@ static int openGrp(const char *filename, int forWriting, FILE **fh, int *count)

errno = 0;
BAIL_IF_MACRO(fread(buf, 12, 1, *fh) != 1, strerror(errno), 0);
BAIL_IF_MACRO(strncmp(buf, "KenSilverman", 12) != 0,
BAIL_IF_MACRO(memcmp(buf, "KenSilverman", 12) != 0,
ERR_UNSUPPORTED_ARCHIVE, 0);

if (fread(count, 4, 1, *fh) != 1)
if (fread(count, sizeof (PHYSFS_sint32), 1, *fh) != 1)
*count = 0;

return(1);
Expand Down Expand Up @@ -312,7 +306,8 @@ static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
} /* GRP_enumerateFiles */


static int getFileEntry(DirHandle *h, const char *name, int *size)
static PHYSFS_sint32 getFileEntry(DirHandle *h, const char *name,
PHYSFS_sint32 *size)
{
char buf[16];
GRPinfo *g = (GRPinfo *) (h->opaque);
Expand All @@ -338,23 +333,24 @@ static int getFileEntry(DirHandle *h, const char *name, int *size)

for (i = 0; i < g->totalEntries; i++)
{
int fsize;
PHYSFS_sint32 l = 0;

errno = 0;
BAIL_IF_MACRO(fread(buf, 16, 1, fh) != 1, strerror(errno), -1);
BAIL_IF_MACRO(fread(buf, 12, 1, fh) != 1, strerror(errno), -1);

fsize = *((int *) (buf + 12));
errno = 0;
BAIL_IF_MACRO(fread(&l, sizeof (l), 1, fh) != 1, strerror(errno), -1);

buf[12] = '\0'; /* FILENAME.EXT is all you get. */

if (__PHYSFS_platformStricmp(buf, name) == 0)
{
if (size != NULL)
*size = fsize;
*size = l;
return(retval);
} /* if */

retval += fsize;
retval += l;
} /* for */

return(-1); /* not found. */
Expand Down Expand Up @@ -383,7 +379,8 @@ static FileHandle *GRP_openRead(DirHandle *h, const char *name)
{
FileHandle *retval;
GRPfileinfo *finfo;
int size, offset;
PHYSFS_sint32 size;
PHYSFS_sint32 offset;

offset = getFileEntry(h, name, &size);
BAIL_IF_MACRO(offset == -1, ERR_NO_SUCH_FILE, NULL);
Expand Down
32 changes: 16 additions & 16 deletions archivers/zip.c
Expand Up @@ -58,12 +58,12 @@ typedef struct
#define SYMLINK_RECURSE_COUNT 20


static int ZIP_read(FileHandle *handle, void *buffer,
unsigned int objSize, unsigned int objCount);
static PHYSFS_sint64 ZIP_read(FileHandle *handle, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
static int ZIP_eof(FileHandle *handle);
static int ZIP_tell(FileHandle *handle);
static int ZIP_seek(FileHandle *handle, int offset);
static int ZIP_fileLength(FileHandle *handle);
static PHYSFS_sint64 ZIP_tell(FileHandle *handle);
static int ZIP_seek(FileHandle *handle, PHYSFS_uint64 offset);
static PHYSFS_sint64 ZIP_fileLength(FileHandle *handle);
static int ZIP_fileClose(FileHandle *handle);
static int ZIP_isArchive(const char *filename, int forWriting);
static char *ZIP_realpath(unzFile fh, unz_file_info *info);
Expand Down Expand Up @@ -116,12 +116,12 @@ const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP =



static int ZIP_read(FileHandle *handle, void *buffer,
unsigned int objSize, unsigned int objCount)
static PHYSFS_sint64 ZIP_read(FileHandle *handle, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
{
unzFile fh = ((ZIPfileinfo *) (handle->opaque))->handle;
int bytes = objSize * objCount;
int rc = unzReadCurrentFile(fh, buffer, bytes);
int bytes = (int) (objSize * objCount); /* !!! FIXME: overflow? */
PHYSFS_sint32 rc = unzReadCurrentFile(fh, buffer, bytes);

if (rc < bytes)
__PHYSFS_setError(ERR_PAST_EOF);
Expand All @@ -140,18 +140,18 @@ static int ZIP_eof(FileHandle *handle)
} /* ZIP_eof */


static int ZIP_tell(FileHandle *handle)
static PHYSFS_sint64 ZIP_tell(FileHandle *handle)
{
return(unztell(((ZIPfileinfo *) (handle->opaque))->handle));
} /* ZIP_tell */


static int ZIP_seek(FileHandle *handle, int offset)
static int ZIP_seek(FileHandle *handle, PHYSFS_uint64 offset)
{
/* this blows. */
/* !!! FIXME : this blows. */
unzFile fh = ((ZIPfileinfo *) (handle->opaque))->handle;
char *buf = NULL;
int bufsize = 4096 * 2;
PHYSFS_uint32 bufsize = 4096 * 2;

BAIL_IF_MACRO(unztell(fh) == offset, NULL, 1);
BAIL_IF_MACRO(ZIP_fileLength(handle) <= offset, ERR_PAST_EOF, 0);
Expand All @@ -169,8 +169,8 @@ static int ZIP_seek(FileHandle *handle, int offset)

while (offset > 0)
{
int chunk = (offset > bufsize) ? bufsize : offset;
int rc = unzReadCurrentFile(fh, buf, chunk);
PHYSFS_uint32 chunk = (offset > bufsize) ? bufsize : offset;
PHYSFS_sint32 rc = unzReadCurrentFile(fh, buf, chunk);
BAIL_IF_MACRO(rc == 0, ERR_IO_ERROR, 0); /* shouldn't happen. */
BAIL_IF_MACRO(rc == UNZ_ERRNO, ERR_IO_ERROR, 0);
BAIL_IF_MACRO(rc < 0, ERR_COMPRESSION, 0);
Expand All @@ -182,7 +182,7 @@ static int ZIP_seek(FileHandle *handle, int offset)
} /* ZIP_seek */


static int ZIP_fileLength(FileHandle *handle)
static PHYSFS_sint64 ZIP_fileLength(FileHandle *handle)
{
ZIPfileinfo *finfo = (ZIPfileinfo *) (handle->opaque);
unz_file_info info;
Expand Down

0 comments on commit f2887cf

Please sign in to comment.