From 6ff079b8e8d896d6afe92e3da4587b6c0afb00e9 Mon Sep 17 00:00:00 2001 From: avolkov Date: Tue, 15 Feb 2011 12:04:28 +0000 Subject: [PATCH] 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 --- sc2/ChangeLog | 1 + sc2/src/libs/reslib.h | 6 ++-- sc2/src/libs/resource/filecntl.c | 10 +++--- sc2/src/libs/resource/getres.c | 2 +- sc2/src/libs/resource/loadres.c | 58 +++++++++++--------------------- sc2/src/libs/resource/propfile.c | 4 ++- sc2/src/libs/strings/getstr.c | 2 +- sc2/src/uqm/load.c | 16 ++++----- sc2/src/uqm/save.c | 17 +++++----- 9 files changed, 50 insertions(+), 66 deletions(-) diff --git a/sc2/ChangeLog b/sc2/ChangeLog index fe6212444..25e5b9d3a 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -1,4 +1,5 @@ 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 - Fix UAC issues with installer for cleaner Vista/Win7 installs - Michael - Fixed compilation with Netplay disabled (bug #1091), from Sze Howe Koh diff --git a/sc2/src/libs/reslib.h b/sc2/src/libs/reslib.h index 39b67b174..c7486e8d0 100644 --- a/sc2/src/libs/reslib.h +++ b/sc2/src/libs/reslib.h @@ -48,14 +48,14 @@ typedef void *(ResourceLoadFileFun) (uio_Stream *fp, DWORD len); void *LoadResourceFromPath(const char *pathname, ResourceLoadFileFun fn); uio_Stream *res_OpenResFile (uio_DirHandle *dir, const char *filename, const char *mode); -int ReadResFile (void *lpBuf, COUNT size, COUNT count, uio_Stream *fp); -int WriteResFile (const void *lpBuf, COUNT size, COUNT count, uio_Stream *fp); +size_t ReadResFile (void *lpBuf, size_t size, size_t 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 PutResFileChar (char ch, uio_Stream *fp); int PutResFileNewline (uio_Stream *fp); long SeekResFile (uio_Stream *fp, long offset, int whence); long TellResFile (uio_Stream *fp); -long LengthResFile (uio_Stream *fp); +size_t LengthResFile (uio_Stream *fp); BOOLEAN res_CloseResFile (uio_Stream *fp); BOOLEAN DeleteResFile (uio_DirHandle *dir, const char *filename); diff --git a/sc2/src/libs/resource/filecntl.c b/sc2/src/libs/resource/filecntl.c index f07699a35..e2a81d9e7 100644 --- a/sc2/src/libs/resource/filecntl.c +++ b/sc2/src/libs/resource/filecntl.c @@ -60,8 +60,8 @@ DeleteResFile (uio_DirHandle *dir, const char *filename) return (uio_unlink (dir, filename) == 0); } -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 retval; @@ -70,8 +70,8 @@ ReadResFile (void *lpBuf, COUNT size, COUNT count, uio_Stream *fp) return (retval); } -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 retval; @@ -131,7 +131,7 @@ TellResFile (uio_Stream *fp) return (retval); } -long +size_t LengthResFile (uio_Stream *fp) { struct stat sb; diff --git a/sc2/src/libs/resource/getres.c b/sc2/src/libs/resource/getres.c index 03f971c48..39e24a9d0 100644 --- a/sc2/src/libs/resource/getres.c +++ b/sc2/src/libs/resource/getres.c @@ -43,7 +43,7 @@ void * LoadResourceFromPath (const char *path, ResourceLoadFileFun *loadFun) { uio_Stream *stream; - long dataLen; + unsigned long dataLen; void *resdata; stream = res_OpenResFile (contentDir, path, "rb"); diff --git a/sc2/src/libs/resource/loadres.c b/sc2/src/libs/resource/loadres.c index 52d5345f5..a9849e40b 100644 --- a/sc2/src/libs/resource/loadres.c +++ b/sc2/src/libs/resource/loadres.c @@ -17,56 +17,38 @@ */ #include "resintrn.h" -#include "libs/declib.h" #include "libs/memlib.h" +#include "libs/log.h" void * GetResourceData (uio_Stream *fp, DWORD length) { - BYTE *RDPtr; void *result; - DECODE_REF fh = 0; + DWORD compLen; - if (length == ~(DWORD)0) - length = LengthResFile (fp); - else if ((fh = copen (fp, FILE_STREAM, STREAM_READ))) - cfilelength (fh, &length); - else - length -= sizeof (DWORD); + // Resource data used to be prefixed by its length in package files. + // A valid length prefix indicated compressed data, and + // a length prefix ~0 meant uncompressed. + // Currently, .ct and .xlt files still carry a ~0 length prefix. + if (ReadResFile (&compLen, sizeof (compLen), 1, fp) != 1) + return NULL; + if (compLen != ~(DWORD)0) + { + log_add (log_Warning, "LZ-compressed binary data not supported"); + return NULL; + } + length -= sizeof (DWORD); result = AllocResourceData (length); - RDPtr = result; - if (RDPtr) + if (!result) + return NULL; + + if (ReadResFile (result, 1, length, fp) != length) { - COUNT num_read; - - 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; - } + FreeResourceData (result); + result = NULL; } - cclose (fh); - return result; } diff --git a/sc2/src/libs/resource/propfile.c b/sc2/src/libs/resource/propfile.c index b6bf377a9..1784600ff 100644 --- a/sc2/src/libs/resource/propfile.c +++ b/sc2/src/libs/resource/propfile.c @@ -98,7 +98,7 @@ PropFile_from_string (char *d, PROPERTY_HANDLER handler, const char *prefix) void PropFile_from_file (uio_Stream *f, PROPERTY_HANDLER handler, const char *prefix) { - long flen; + size_t flen; char *data; flen = LengthResFile (f); @@ -108,6 +108,8 @@ PropFile_from_file (uio_Stream *f, PROPERTY_HANDLER handler, const char *prefix) 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); data[flen] = '\0'; diff --git a/sc2/src/libs/strings/getstr.c b/sc2/src/libs/strings/getstr.c index 74d57a207..c6865cc45 100644 --- a/sc2/src/libs/strings/getstr.c +++ b/sc2/src/libs/strings/getstr.c @@ -65,7 +65,7 @@ void _GetConversationData (const char *path, RESOURCE_DATA *resdata) { uio_Stream *fp; - long dataLen; + unsigned long dataLen; void *result; int n, path_len, num_data_sets; DWORD opos, diff --git a/sc2/src/uqm/load.c b/sc2/src/uqm/load.c index 29943bb95..0c1e3de60 100644 --- a/sc2/src/uqm/load.c +++ b/sc2/src/uqm/load.c @@ -107,7 +107,7 @@ cread_a8 (DECODE_REF fh, BYTE *ar, COUNT count) return cread (ar, 1, count, fh) == count; } -static inline COUNT +static inline size_t read_8 (void *fp, BYTE *v) { BYTE t; @@ -116,7 +116,7 @@ read_8 (void *fp, BYTE *v) return ReadResFile (v, 1, 1, fp); } -static inline COUNT +static inline size_t read_16 (void *fp, UWORD *v) { UWORD t; @@ -125,7 +125,7 @@ read_16 (void *fp, UWORD *v) return ReadResFile (v, 2, 1, fp); } -static inline COUNT +static inline size_t read_32 (void *fp, DWORD *v) { DWORD t; @@ -134,7 +134,7 @@ read_32 (void *fp, DWORD *v) return ReadResFile (v, 4, 1, fp); } -static inline COUNT +static inline size_t read_32s (void *fp, SDWORD *v) { DWORD t; @@ -147,28 +147,28 @@ read_32s (void *fp, SDWORD *v) return ret; } -static inline COUNT +static inline size_t read_ptr (void *fp) { DWORD t; 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) { assert (ar != NULL); return ReadResFile (ar, 1, count, fp) == count; } -static inline COUNT +static inline size_t read_str (void *fp, char *str, COUNT count) { // no type conversion needed for strings return read_a8 (fp, (BYTE *)str, count); } -static inline COUNT +static inline size_t read_a16 (void *fp, UWORD *ar, COUNT count) { assert (ar != NULL); diff --git a/sc2/src/uqm/save.c b/sc2/src/uqm/save.c index f79e9dee1..43a21d16e 100644 --- a/sc2/src/uqm/save.c +++ b/sc2/src/uqm/save.c @@ -74,44 +74,44 @@ cwrite_a8 (DECODE_REF fh, const BYTE *ar, COUNT count) return cwrite (ar, 1, count, fh) == count; } -static inline COUNT +static inline size_t write_8 (void *fp, BYTE v) { return WriteResFile (&v, 1, 1, fp); } -static inline COUNT +static inline size_t write_16 (void *fp, UWORD v) { return WriteResFile (&v, 2, 1, fp); } -static inline COUNT +static inline size_t write_32 (void *fp, DWORD v) { return WriteResFile (&v, 4, 1, fp); } -static inline COUNT +static inline size_t write_ptr (void *fp) { 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) { return WriteResFile (ar, 1, count, fp) == count; } -static inline COUNT +static inline size_t write_str (void *fp, const char *str, COUNT count) { // no type conversion needed for strings return write_a8 (fp, (const BYTE *)str, count); } -static inline COUNT +static inline size_t write_a16 (void *fp, const UWORD *ar, COUNT count) { for ( ; count > 0; --count, ++ar) @@ -838,8 +838,7 @@ RetrySave: success = SaveSummary (SummPtr, out_fp); // Then write the rest of the data. - if (success && WriteResFile (h, (COUNT)flen, 1, - out_fp) == 0) + if (success && WriteResFile (h, flen, 1, out_fp) != 1) success = FALSE; if (res_CloseResFile ((uio_Stream *)out_fp) == 0)