summaryrefslogtreecommitdiff
path: root/src/newgrf_config.cpp
diff options
context:
space:
mode:
authormichi_cc <michi_cc@openttd.org>2012-02-04 13:29:04 +0000
committermichi_cc <michi_cc@openttd.org>2012-02-04 13:29:04 +0000
commit6db39410a1ea0bc8ece1f33fb7771e47d39cc2b7 (patch)
treed94bcfca3862721c5e6fccff0ec87b79e757cd24 /src/newgrf_config.cpp
parenta9b6c5cd86f4f17155695890bfaf2e30404f5921 (diff)
downloadopenttd-6db39410a1ea0bc8ece1f33fb7771e47d39cc2b7.tar.xz
(svn r23887) -Feature: [NewGRF] Support for container version 2.
Diffstat (limited to 'src/newgrf_config.cpp')
-rw-r--r--src/newgrf_config.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp
index f644315f1..4c296d20f 100644
--- a/src/newgrf_config.cpp
+++ b/src/newgrf_config.cpp
@@ -294,6 +294,28 @@ bool UpdateNewGRFConfigPalette(int32 p1)
}
/**
+ * Get the data section size of a GRF.
+ * @param f GRF.
+ * @return Size of the data section or SIZE_MAX if the file has no separate data section.
+ */
+size_t GRFGetSizeOfDataSection(FILE *f)
+{
+ extern const byte _grf_cont_v2_sig[];
+ static const uint header_len = 14;
+
+ byte data[header_len];
+ if (fread(data, 1, header_len, f) == header_len) {
+ if (data[0] == 0 && data[1] == 0 && MemCmpT(data + 2, _grf_cont_v2_sig, 8) == 0) {
+ /* Valid container version 2, get data section size. */
+ size_t offset = (data[13] << 24) | (data[12] << 16) | (data[11] << 8) | data[10];
+ return header_len + offset;
+ }
+ }
+
+ return SIZE_MAX;
+}
+
+/**
* Calculate the MD5 sum for a GRF, and store it in the config.
* @param config GRF to compute.
* @param subdir The subdirectory to look in.
@@ -310,6 +332,10 @@ static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir)
f = FioFOpenFile(config->filename, "rb", subdir, &size);
if (f == NULL) return false;
+ size_t start = ftell(f);
+ size = min(size, GRFGetSizeOfDataSection(f));
+ fseek(f, start, SEEK_SET);
+
/* calculate md5sum */
while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
size -= len;