summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/fileio.cpp12
-rw-r--r--src/fileio.h2
-rw-r--r--src/gfxinit.cpp7
-rw-r--r--src/newgrf_config.cpp7
4 files changed, 19 insertions, 9 deletions
diff --git a/src/fileio.cpp b/src/fileio.cpp
index ad278e15f..2d1c01c71 100644
--- a/src/fileio.cpp
+++ b/src/fileio.cpp
@@ -267,7 +267,7 @@ char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir)
return buf;
}
-FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir)
+FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
{
#if defined(WIN32) && defined(UNICODE)
/* fopen is implemented as a define with ellipses for
@@ -293,11 +293,17 @@ FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subd
f = fopen(buf, mode);
}
#endif
+ if (f != NULL && filesize != NULL) {
+ /* Find the size of the file */
+ fseek(f, 0, SEEK_END);
+ *filesize = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ }
return f;
}
/** Opens OpenTTD files somewhere in a personal or global directory */
-FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir)
+FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *size)
{
FILE *f = NULL;
Searchpath sp;
@@ -305,7 +311,7 @@ FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir)
assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY);
FOR_ALL_SEARCHPATHS(sp) {
- f = FioFOpenFileSp(filename, mode, sp, subdir);
+ f = FioFOpenFileSp(filename, mode, sp, subdir, size);
if (f != NULL || subdir == NO_DIRECTORY) break;
}
diff --git a/src/fileio.h b/src/fileio.h
index a12c01e65..a5333653b 100644
--- a/src/fileio.h
+++ b/src/fileio.h
@@ -73,7 +73,7 @@ static inline bool IsValidSearchPath(Searchpath sp)
/** Iterator for all the search paths */
#define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
-FILE *FioFOpenFile(const char *filename, const char *mode = "rb", Subdirectory subdir = DATA_DIR);
+FILE *FioFOpenFile(const char *filename, const char *mode = "rb", Subdirectory subdir = DATA_DIR, size_t *filesize = NULL);
bool FioCheckFileExists(const char *filename, Subdirectory subdir = DATA_DIR);
char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename);
char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename);
diff --git a/src/gfxinit.cpp b/src/gfxinit.cpp
index 408b5f623..86a16c391 100644
--- a/src/gfxinit.cpp
+++ b/src/gfxinit.cpp
@@ -114,7 +114,8 @@ static bool CheckMD5Digest(const MD5File file, md5_byte_t *digest, bool warn)
* returns true if the checksum is correct */
static bool FileMD5(const MD5File file, bool warn)
{
- FILE *f = FioFOpenFile(file.filename);
+ size_t size;
+ FILE *f = FioFOpenFile(file.filename, "rb", DATA_DIR, &size);
if (f != NULL) {
md5_state_t filemd5state;
@@ -123,8 +124,10 @@ static bool FileMD5(const MD5File file, bool warn)
size_t len;
md5_init(&filemd5state);
- while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0)
+ while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
+ size -= len;
md5_append(&filemd5state, buffer, len);
+ }
if (ferror(f) && warn) ShowInfoF("Error Reading from %s \n", file.filename);
fclose(f);
diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp
index f320d2c05..acd471d64 100644
--- a/src/newgrf_config.cpp
+++ b/src/newgrf_config.cpp
@@ -37,15 +37,16 @@ static bool CalcGRFMD5Sum(GRFConfig *config)
FILE *f;
md5_state_t md5state;
md5_byte_t buffer[1024];
- size_t len;
+ size_t len, size;
/* open the file */
- f = FioFOpenFile(config->filename);
+ f = FioFOpenFile(config->filename, "rb", DATA_DIR, &size);
if (f == NULL) return false;
/* calculate md5sum */
md5_init(&md5state);
- while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0) {
+ while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
+ size -= len;
md5_append(&md5state, buffer, len);
}
md5_finish(&md5state, config->md5sum);