From 5e9e80eddc209edb14a3438e4ed2e19a872d45f4 Mon Sep 17 00:00:00 2001 From: harry Date: Wed, 25 Jan 2023 21:46:30 -0500 Subject: [PATCH] Change EMUFILE fseek,ftell, and other related functions to use types that follow the C standard (long int for position offsets and size_t for sizes). This allows for larger max file sizes on unix platforms and cleaner code that better maps to the C standard. --- src/emufile.cpp | 2 +- src/emufile.h | 47 ++++++++++++++++++++++++----------------------- src/file.cpp | 2 +- src/file.h | 2 +- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/emufile.cpp b/src/emufile.cpp index e0e83115..63de55ff 100644 --- a/src/emufile.cpp +++ b/src/emufile.cpp @@ -78,7 +78,7 @@ void EMUFILE_FILE::open(const char* fname, const char* mode) } -void EMUFILE_FILE::truncate(s32 length) +void EMUFILE_FILE::truncate(size_t length) { ::fflush(fp); #ifdef _MSC_VER diff --git a/src/emufile.h b/src/emufile.h index 78e4d6ee..5ea94a2b 100644 --- a/src/emufile.h +++ b/src/emufile.h @@ -109,13 +109,13 @@ public: double readdouble(); size_t readdouble(double* val); - virtual int fseek(int offset, int origin) = 0; + virtual int fseek(long int offset, int origin) = 0; - virtual int ftell() = 0; - virtual int size() = 0; + virtual long int ftell() = 0; + virtual size_t size() = 0; virtual void fflush() = 0; - virtual void truncate(s32 length) = 0; + virtual void truncate(size_t length) = 0; }; //todo - handle read-only specially? @@ -123,9 +123,10 @@ class EMUFILE_MEMORY : public EMUFILE { protected: std::vector *vec; bool ownvec; - s32 pos, len; + long int pos; + size_t len; - void reserve(u32 amt) { + void reserve(size_t amt) { if(vec->size() < amt) vec->resize(amt); } @@ -133,12 +134,12 @@ protected: public: EMUFILE_MEMORY(std::vector *underlying) : vec(underlying), ownvec(false), pos(0), len((s32)underlying->size()) { } - EMUFILE_MEMORY(u32 preallocate) : vec(new std::vector()), ownvec(true), pos(0), len(0) { + EMUFILE_MEMORY(size_t preallocate) : vec(new std::vector()), ownvec(true), pos(0), len(0) { vec->resize(preallocate); len = preallocate; } EMUFILE_MEMORY() : vec(new std::vector()), ownvec(true), pos(0), len(0) { vec->reserve(1024); } - EMUFILE_MEMORY(void* buf, s32 size) : vec(new std::vector()), ownvec(true), pos(0), len(size) { + EMUFILE_MEMORY(void* buf, size_t size) : vec(new std::vector()), ownvec(true), pos(0), len(size) { vec->resize(size); if(size != 0) memcpy(&vec->front(),buf,size); @@ -150,7 +151,7 @@ public: virtual EMUFILE* memwrap(); - virtual void truncate(s32 length) + virtual void truncate(size_t length) { vec->resize(length); len = length; @@ -216,13 +217,13 @@ public: //they handle the return values correctly virtual void fwrite(const void *ptr, size_t bytes){ - reserve(pos+(s32)bytes); + reserve(pos+bytes); memcpy(buf()+pos,ptr,bytes); - pos += (s32)bytes; + pos += bytes; len = std::max(pos,len); } - virtual int fseek(int offset, int origin){ + virtual int fseek(long int offset, int origin){ //work differently for read-only...? switch(origin) { case SEEK_SET: @@ -241,13 +242,13 @@ public: return 0; } - virtual int ftell() { + virtual long int ftell() { return pos; } virtual void fflush() {} - void set_len(s32 length) + void set_len(size_t length) { len = length; if(pos > length) @@ -258,7 +259,7 @@ public: vec->resize(len); } - virtual int size() { return (int)len; } + virtual size_t size() { return len; } }; class EMUFILE_FILE : public EMUFILE { @@ -288,7 +289,7 @@ public: bool is_open() { return fp != NULL; } - virtual void truncate(s32 length); + virtual void truncate(size_t length); virtual int fprintf(const char *format, ...) { va_list argptr; @@ -321,20 +322,20 @@ public: failbit = true; } - virtual int fseek(int offset, int origin) { + virtual int fseek(long int offset, int origin) { return ::fseek(fp, offset, origin); } - virtual int ftell() { - return (u32)::ftell(fp); + virtual long int ftell() { + return ::ftell(fp); } - virtual int size() { - int oldpos = ftell(); + virtual size_t size() { + long int oldpos = ftell(); fseek(0,SEEK_END); - int len = ftell(); + long int len = ftell(); fseek(oldpos,SEEK_SET); - return len; + return static_cast(len); } virtual void fflush() { diff --git a/src/file.cpp b/src/file.cpp index 74091a52..0a18b240 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -327,7 +327,7 @@ FCEUFILE * FCEU_fopen(const char *path, const char *ipsfn, const char *mode, cha if(gzfile) { delete fp; - int size; + size_t size; for(size=0; gzgetc(gzfile) != EOF; size++) {} EMUFILE_MEMORY* ms = new EMUFILE_MEMORY(size); gzseek(gzfile,0,SEEK_SET); diff --git a/src/file.h b/src/file.h index 84235f70..05ee2eaf 100644 --- a/src/file.h +++ b/src/file.h @@ -35,7 +35,7 @@ struct FCEUFILE { int archiveIndex; //the size of the file - int size; + size_t size; //whether the file is contained in an archive bool isArchive() { return archiveCount > 0; }