From 8138121e39d4d2fbf8afefc338a4c05ac401b98e Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Tue, 5 Apr 2022 08:15:53 +0100 Subject: [PATCH] Fix endianness handling in language.cpp --- Source/utils/language.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Source/utils/language.cpp b/Source/utils/language.cpp index 6118b4c5590..46caacf4d84 100644 --- a/Source/utils/language.cpp +++ b/Source/utils/language.cpp @@ -38,11 +38,27 @@ struct MoHead { uint32_t dstOffset; }; +void SwapLE(MoHead &head) +{ + head.magic = SDL_SwapLE32(head.magic); + head.revision.major = SDL_SwapLE16(head.revision.major); + head.revision.minor = SDL_SwapLE16(head.revision.minor); + head.nbMappings = SDL_SwapLE32(head.nbMappings); + head.srcOffset = SDL_SwapLE32(head.srcOffset); + head.dstOffset = SDL_SwapLE32(head.dstOffset); +} + struct MoEntry { uint32_t length; uint32_t offset; }; +void SwapLE(MoEntry &entry) +{ + entry.length = SDL_SwapLE32(entry.length); + entry.offset = SDL_SwapLE32(entry.offset); +} + char *StrTrimLeft(char *s) { while (*s != '\0' && isblank(*s) != 0) { @@ -316,12 +332,12 @@ void LanguageInitialize() return; // Read header and do sanity checks - // FIXME: Endianness. MoHead head; if (SDL_RWread(rw, &head, sizeof(MoHead), 1) != 1) { SDL_RWclose(rw); return; } + SwapLE(head); if (head.magic != MO_MAGIC) { SDL_RWclose(rw); @@ -351,11 +367,13 @@ void LanguageInitialize() SDL_RWclose(rw); return; } - // FIXME: Endianness. if (static_cast(SDL_RWread(rw, dst.get(), sizeof(MoEntry), head.nbMappings)) != head.nbMappings) { SDL_RWclose(rw); return; } + for (size_t i = 0; i < head.nbMappings; ++i) { + SwapLE(dst[i]); + } std::vector key; std::vector value;