Change ReadResFile/WriteResFile interface to take size_t args; removes 64KB size limitation on key-value files; bug #1112

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3558 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
avolkov
2011-02-15 12:04:28 +00:00
parent 19e4587288
commit 6ff079b8e8
9 changed files with 50 additions and 66 deletions
+1
View File
@@ -1,4 +1,5 @@
Changes towards version 0.7: Changes towards version 0.7:
- Fixed the 64KB size limitation on key-value files (bug #1112) - Alex
- Update download paths for the new SourceForge File Release system - Michael - Update download paths for the new SourceForge File Release system - Michael
- Fix UAC issues with installer for cleaner Vista/Win7 installs - Michael - Fix UAC issues with installer for cleaner Vista/Win7 installs - Michael
- Fixed compilation with Netplay disabled (bug #1091), from Sze Howe Koh - Fixed compilation with Netplay disabled (bug #1091), from Sze Howe Koh
+3 -3
View File
@@ -48,14 +48,14 @@ typedef void *(ResourceLoadFileFun) (uio_Stream *fp, DWORD len);
void *LoadResourceFromPath(const char *pathname, ResourceLoadFileFun fn); void *LoadResourceFromPath(const char *pathname, ResourceLoadFileFun fn);
uio_Stream *res_OpenResFile (uio_DirHandle *dir, const char *filename, const char *mode); uio_Stream *res_OpenResFile (uio_DirHandle *dir, const char *filename, const char *mode);
int ReadResFile (void *lpBuf, COUNT size, COUNT count, uio_Stream *fp); size_t ReadResFile (void *lpBuf, size_t size, size_t count, uio_Stream *fp);
int WriteResFile (const void *lpBuf, COUNT size, COUNT count, uio_Stream *fp); size_t WriteResFile (const void *lpBuf, size_t size, size_t count, uio_Stream *fp);
int GetResFileChar (uio_Stream *fp); int GetResFileChar (uio_Stream *fp);
int PutResFileChar (char ch, uio_Stream *fp); int PutResFileChar (char ch, uio_Stream *fp);
int PutResFileNewline (uio_Stream *fp); int PutResFileNewline (uio_Stream *fp);
long SeekResFile (uio_Stream *fp, long offset, int whence); long SeekResFile (uio_Stream *fp, long offset, int whence);
long TellResFile (uio_Stream *fp); long TellResFile (uio_Stream *fp);
long LengthResFile (uio_Stream *fp); size_t LengthResFile (uio_Stream *fp);
BOOLEAN res_CloseResFile (uio_Stream *fp); BOOLEAN res_CloseResFile (uio_Stream *fp);
BOOLEAN DeleteResFile (uio_DirHandle *dir, const char *filename); BOOLEAN DeleteResFile (uio_DirHandle *dir, const char *filename);
+5 -5
View File
@@ -60,8 +60,8 @@ DeleteResFile (uio_DirHandle *dir, const char *filename)
return (uio_unlink (dir, filename) == 0); return (uio_unlink (dir, filename) == 0);
} }
int size_t
ReadResFile (void *lpBuf, COUNT size, COUNT count, uio_Stream *fp) ReadResFile (void *lpBuf, size_t size, size_t count, uio_Stream *fp)
{ {
int retval; int retval;
@@ -70,8 +70,8 @@ ReadResFile (void *lpBuf, COUNT size, COUNT count, uio_Stream *fp)
return (retval); return (retval);
} }
int size_t
WriteResFile (const void *lpBuf, COUNT size, COUNT count, uio_Stream *fp) WriteResFile (const void *lpBuf, size_t size, size_t count, uio_Stream *fp)
{ {
int retval; int retval;
@@ -131,7 +131,7 @@ TellResFile (uio_Stream *fp)
return (retval); return (retval);
} }
long size_t
LengthResFile (uio_Stream *fp) LengthResFile (uio_Stream *fp)
{ {
struct stat sb; struct stat sb;
+1 -1
View File
@@ -43,7 +43,7 @@ void *
LoadResourceFromPath (const char *path, ResourceLoadFileFun *loadFun) LoadResourceFromPath (const char *path, ResourceLoadFileFun *loadFun)
{ {
uio_Stream *stream; uio_Stream *stream;
long dataLen; unsigned long dataLen;
void *resdata; void *resdata;
stream = res_OpenResFile (contentDir, path, "rb"); stream = res_OpenResFile (contentDir, path, "rb");
+20 -38
View File
@@ -17,56 +17,38 @@
*/ */
#include "resintrn.h" #include "resintrn.h"
#include "libs/declib.h"
#include "libs/memlib.h" #include "libs/memlib.h"
#include "libs/log.h"
void * void *
GetResourceData (uio_Stream *fp, DWORD length) GetResourceData (uio_Stream *fp, DWORD length)
{ {
BYTE *RDPtr;
void *result; void *result;
DECODE_REF fh = 0; DWORD compLen;
if (length == ~(DWORD)0) // Resource data used to be prefixed by its length in package files.
length = LengthResFile (fp); // A valid length prefix indicated compressed data, and
else if ((fh = copen (fp, FILE_STREAM, STREAM_READ))) // a length prefix ~0 meant uncompressed.
cfilelength (fh, &length); // Currently, .ct and .xlt files still carry a ~0 length prefix.
else if (ReadResFile (&compLen, sizeof (compLen), 1, fp) != 1)
length -= sizeof (DWORD); return NULL;
if (compLen != ~(DWORD)0)
{
log_add (log_Warning, "LZ-compressed binary data not supported");
return NULL;
}
length -= sizeof (DWORD);
result = AllocResourceData (length); result = AllocResourceData (length);
RDPtr = result; if (!result)
if (RDPtr) return NULL;
if (ReadResFile (result, 1, length, fp) != length)
{ {
COUNT num_read; FreeResourceData (result);
result = NULL;
do
{
#define READ_LENGTH 0x00007FFFL
num_read = length >= READ_LENGTH ?
(COUNT)READ_LENGTH : (COUNT)length;
if (fh)
{
if (cread (RDPtr, 1, num_read, fh) != num_read)
break;
}
else
{
if ((int)(ReadResFile (RDPtr, 1, num_read, fp)) != (int)num_read)
break;
}
RDPtr += num_read;
} while (length -= num_read);
if (length > 0)
{
FreeResourceData (result);
result = NULL;
}
} }
cclose (fh);
return result; return result;
} }
+3 -1
View File
@@ -98,7 +98,7 @@ PropFile_from_string (char *d, PROPERTY_HANDLER handler, const char *prefix)
void void
PropFile_from_file (uio_Stream *f, PROPERTY_HANDLER handler, const char *prefix) PropFile_from_file (uio_Stream *f, PROPERTY_HANDLER handler, const char *prefix)
{ {
long flen; size_t flen;
char *data; char *data;
flen = LengthResFile (f); flen = LengthResFile (f);
@@ -108,6 +108,8 @@ PropFile_from_file (uio_Stream *f, PROPERTY_HANDLER handler, const char *prefix)
return; return;
} }
// We may end up with less bytes than we asked for due to the
// DOS->Unix newline conversion
flen = ReadResFile (data, 1, flen, f); flen = ReadResFile (data, 1, flen, f);
data[flen] = '\0'; data[flen] = '\0';
+1 -1
View File
@@ -65,7 +65,7 @@ void
_GetConversationData (const char *path, RESOURCE_DATA *resdata) _GetConversationData (const char *path, RESOURCE_DATA *resdata)
{ {
uio_Stream *fp; uio_Stream *fp;
long dataLen; unsigned long dataLen;
void *result; void *result;
int n, path_len, num_data_sets; int n, path_len, num_data_sets;
DWORD opos, DWORD opos,
+8 -8
View File
@@ -107,7 +107,7 @@ cread_a8 (DECODE_REF fh, BYTE *ar, COUNT count)
return cread (ar, 1, count, fh) == count; return cread (ar, 1, count, fh) == count;
} }
static inline COUNT static inline size_t
read_8 (void *fp, BYTE *v) read_8 (void *fp, BYTE *v)
{ {
BYTE t; BYTE t;
@@ -116,7 +116,7 @@ read_8 (void *fp, BYTE *v)
return ReadResFile (v, 1, 1, fp); return ReadResFile (v, 1, 1, fp);
} }
static inline COUNT static inline size_t
read_16 (void *fp, UWORD *v) read_16 (void *fp, UWORD *v)
{ {
UWORD t; UWORD t;
@@ -125,7 +125,7 @@ read_16 (void *fp, UWORD *v)
return ReadResFile (v, 2, 1, fp); return ReadResFile (v, 2, 1, fp);
} }
static inline COUNT static inline size_t
read_32 (void *fp, DWORD *v) read_32 (void *fp, DWORD *v)
{ {
DWORD t; DWORD t;
@@ -134,7 +134,7 @@ read_32 (void *fp, DWORD *v)
return ReadResFile (v, 4, 1, fp); return ReadResFile (v, 4, 1, fp);
} }
static inline COUNT static inline size_t
read_32s (void *fp, SDWORD *v) read_32s (void *fp, SDWORD *v)
{ {
DWORD t; DWORD t;
@@ -147,28 +147,28 @@ read_32s (void *fp, SDWORD *v)
return ret; return ret;
} }
static inline COUNT static inline size_t
read_ptr (void *fp) read_ptr (void *fp)
{ {
DWORD t; DWORD t;
return read_32 (fp, &t); /* ptrs are useless in saves */ return read_32 (fp, &t); /* ptrs are useless in saves */
} }
static inline COUNT static inline size_t
read_a8 (void *fp, BYTE *ar, COUNT count) read_a8 (void *fp, BYTE *ar, COUNT count)
{ {
assert (ar != NULL); assert (ar != NULL);
return ReadResFile (ar, 1, count, fp) == count; return ReadResFile (ar, 1, count, fp) == count;
} }
static inline COUNT static inline size_t
read_str (void *fp, char *str, COUNT count) read_str (void *fp, char *str, COUNT count)
{ {
// no type conversion needed for strings // no type conversion needed for strings
return read_a8 (fp, (BYTE *)str, count); return read_a8 (fp, (BYTE *)str, count);
} }
static inline COUNT static inline size_t
read_a16 (void *fp, UWORD *ar, COUNT count) read_a16 (void *fp, UWORD *ar, COUNT count)
{ {
assert (ar != NULL); assert (ar != NULL);
+8 -9
View File
@@ -74,44 +74,44 @@ cwrite_a8 (DECODE_REF fh, const BYTE *ar, COUNT count)
return cwrite (ar, 1, count, fh) == count; return cwrite (ar, 1, count, fh) == count;
} }
static inline COUNT static inline size_t
write_8 (void *fp, BYTE v) write_8 (void *fp, BYTE v)
{ {
return WriteResFile (&v, 1, 1, fp); return WriteResFile (&v, 1, 1, fp);
} }
static inline COUNT static inline size_t
write_16 (void *fp, UWORD v) write_16 (void *fp, UWORD v)
{ {
return WriteResFile (&v, 2, 1, fp); return WriteResFile (&v, 2, 1, fp);
} }
static inline COUNT static inline size_t
write_32 (void *fp, DWORD v) write_32 (void *fp, DWORD v)
{ {
return WriteResFile (&v, 4, 1, fp); return WriteResFile (&v, 4, 1, fp);
} }
static inline COUNT static inline size_t
write_ptr (void *fp) write_ptr (void *fp)
{ {
return write_32 (fp, 0); /* ptrs are useless in saves */ return write_32 (fp, 0); /* ptrs are useless in saves */
} }
static inline COUNT static inline size_t
write_a8 (void *fp, const BYTE *ar, COUNT count) write_a8 (void *fp, const BYTE *ar, COUNT count)
{ {
return WriteResFile (ar, 1, count, fp) == count; return WriteResFile (ar, 1, count, fp) == count;
} }
static inline COUNT static inline size_t
write_str (void *fp, const char *str, COUNT count) write_str (void *fp, const char *str, COUNT count)
{ {
// no type conversion needed for strings // no type conversion needed for strings
return write_a8 (fp, (const BYTE *)str, count); return write_a8 (fp, (const BYTE *)str, count);
} }
static inline COUNT static inline size_t
write_a16 (void *fp, const UWORD *ar, COUNT count) write_a16 (void *fp, const UWORD *ar, COUNT count)
{ {
for ( ; count > 0; --count, ++ar) for ( ; count > 0; --count, ++ar)
@@ -838,8 +838,7 @@ RetrySave:
success = SaveSummary (SummPtr, out_fp); success = SaveSummary (SummPtr, out_fp);
// Then write the rest of the data. // Then write the rest of the data.
if (success && WriteResFile (h, (COUNT)flen, 1, if (success && WriteResFile (h, flen, 1, out_fp) != 1)
out_fp) == 0)
success = FALSE; success = FALSE;
if (res_CloseResFile ((uio_Stream *)out_fp) == 0) if (res_CloseResFile ((uio_Stream *)out_fp) == 0)