From 5b8d41afb70aaf79d2c3aac616de488f4c1de286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Sat, 29 Jul 2017 22:19:03 +0200 Subject: [PATCH] Detect OZF files by contents rather than by file extension --- src/offlinemap.cpp | 8 +------- src/ozf.cpp | 16 ++++++++++++++++ src/ozf.h | 2 ++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/offlinemap.cpp b/src/offlinemap.cpp index c38ac6c5..a0d0e240 100644 --- a/src/offlinemap.cpp +++ b/src/offlinemap.cpp @@ -342,13 +342,7 @@ bool OfflineMap::getImageInfo(const QString &path) return false; } - QString suffix = ii.suffix().toLower(); - if (suffix == "ozf4" || suffix == "ozfx4") { - _errorString = QString("%1: OZF4 image files not supported") - .arg(QFileInfo(_imgPath).fileName()); - return false; - } else if (suffix == "ozf2" || suffix == "ozfx2" || suffix == "ozf3" - || suffix == "ozfx3") { + if (OZF::isOZF(_imgPath)) { _ozf.load(_imgPath); _size = _ozf.size(); } else { diff --git a/src/ozf.cpp b/src/ozf.cpp index 90adf8de..4dea0551 100644 --- a/src/ozf.cpp +++ b/src/ozf.cpp @@ -232,3 +232,19 @@ QPixmap OZF::tile(int x, int y) return QPixmap::fromImage(img.mirrored()); } + +bool OZF::isOZF(const QString &path) +{ + QFile file(path); + quint16 magic; + + if (!file.open(QIODevice::ReadOnly)) + return false; + if (file.read((char*)&magic, sizeof(magic)) < (qint64)sizeof(magic)) + return false; + + if (magic == OZF2_MAGIC || magic == OZF3_MAGIC) + return true; + + return false; +} diff --git a/src/ozf.h b/src/ozf.h index ccce0246..f995df4e 100644 --- a/src/ozf.h +++ b/src/ozf.h @@ -22,6 +22,8 @@ public: QSize tileSize() const {return QSize(64, 64);} QPixmap tile(int x, int y); + static bool isOZF(const QString &path); + private: template bool readValue(T &val); bool read(void *data, size_t size);