(svn r21132) -Codechange: unify the language file version/validity checking

This commit is contained in:
rubidium 2010-11-10 17:48:31 +00:00
parent 2df2c021df
commit 8cd48767f9
2 changed files with 17 additions and 6 deletions

View File

@ -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);

View File

@ -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;
}