Skip to content
Permalink
Browse files
Added typedefs and platform-specific i/o.
  • Loading branch information
icculus committed Mar 24, 2002
1 parent 3b79873 commit f2887cf04719606b487d9885bb539ec07eae842f
Showing with 452 additions and 145 deletions.
  1. +5 −1 CHANGELOG
  2. +2 −1 Makefile
  3. +14 −14 archivers/dir.c
  4. +31 −34 archivers/grp.c
  5. +16 −16 archivers/zip.c
  6. +30 −10 extras/physfsrwops.c
  7. +0 −6 extras/physfsrwops.h
  8. +8 −8 physfs.c
  9. +50 −12 physfs.h
  10. +159 −17 physfs_internal.h
  11. +129 −21 platform/unix.c
  12. +8 −5 test/test_physfs.c
@@ -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)

@@ -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

@@ -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);
@@ -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;
@@ -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;
@@ -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 */
@@ -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);
@@ -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)
@@ -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 */


@@ -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;
@@ -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);
@@ -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);

@@ -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);
@@ -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);
@@ -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. */
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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;

0 comments on commit f2887cf

Please sign in to comment.