From 8cd48767f93deb6ac4ef084574971e8f74a21038 Mon Sep 17 00:00:00 2001 From: rubidium Date: Wed, 10 Nov 2010 17:48:31 +0000 Subject: [PATCH] (svn r21132) -Codechange: unify the language file version/validity checking --- src/strgen/strgen.h | 6 ++++++ src/strings.cpp | 17 +++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/strgen/strgen.h b/src/strgen/strgen.h index ecaeb05ff2..778c643619 100644 --- a/src/strgen/strgen.h +++ b/src/strgen/strgen.h @@ -42,6 +42,12 @@ struct LanguagePackHeader { uint16 winlangid; ///< windows language id uint8 newgrflangid; ///< newgrf language id byte pad[3]; ///< pad header to be a multiple of 4 + + /** + * Check whether the header is a valid header for OpenTTD. + * @return true iff the header is deemed valid. + */ + bool IsValid() const; }; assert_compile(sizeof(LanguagePackHeader) % 4 == 0); diff --git a/src/strings.cpp b/src/strings.cpp index 5b318f20cf..b2318ccc47 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -35,6 +35,7 @@ #include "string_func.h" #include "company_base.h" #include "smallmap_gui.h" +#include "debug.h" #include "table/strings.h" #include "table/control_codes.h" @@ -1302,6 +1303,13 @@ extern void SortNetworkLanguages(); static inline void SortNetworkLanguages() {} #endif /* ENABLE_NETWORK */ +bool LanguagePackHeader::IsValid() const +{ + return + this->ident == TO_LE32(LanguagePackHeader::IDENT) && + this->version == TO_LE32(LANGUAGE_PACK_VERSION); +} + bool ReadLanguagePack(int lang_index) { /* Current language pack */ @@ -1313,9 +1321,7 @@ bool ReadLanguagePack(int lang_index) const char *end = (char *)lang_pack + len + 1; /* We need at least one byte of lang_pack->data */ - if (end <= lang_pack->data || - lang_pack->ident != TO_LE32(LanguagePackHeader::IDENT) || - lang_pack->version != TO_LE32(LANGUAGE_PACK_VERSION)) { + if (end <= lang_pack->data || !lang_pack->IsValid()) { free(lang_pack); return false; } @@ -1452,9 +1458,7 @@ static bool GetLanguageFileHeader(const char *file, LanguagePack *hdr) size_t read = fread(hdr, sizeof(*hdr), 1, f); fclose(f); - bool ret = read == 1 && - hdr->ident == TO_LE32(LanguagePackHeader::IDENT) && - hdr->version == TO_LE32(LANGUAGE_PACK_VERSION); + bool ret = read == 1 && hdr->IsValid(); /* Convert endianness for the windows language ID */ if (ret) hdr->winlangid = FROM_LE16(hdr->winlangid); @@ -1491,6 +1495,7 @@ static int GetLanguageList(Language *langs, int start, int max, const char *path /* Check whether the file is of the correct version */ LanguagePack hdr; if (!GetLanguageFileHeader(langs[i].file, &hdr)) { + DEBUG(misc, 3, "%s is not a valid language file", langs[i].file); free(langs[i].file); continue; }