1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 19:55:53 +01:00

Made OZF3 files parsing more robust.

This commit is contained in:
Martin Tůma 2017-08-22 15:57:37 +02:00
parent bd20d40ba7
commit ebb30a3fca

View File

@ -1,3 +1,4 @@
#include <cstring>
#include <QtEndian> #include <QtEndian>
#include <QFile> #include <QFile>
#include "ozf.h" #include "ozf.h"
@ -52,8 +53,8 @@ bool OZF::read(void *data, size_t size, size_t decryptSize)
bool OZF::initOZF3() bool OZF::initOZF3()
{ {
quint8 randomNumber, initial; quint8 randomNumber, initial;
quint32 keyblock;
quint8 h1[8]; quint8 h1[8];
quint8 h2[16], h2d[16];
if (!_file.seek(14)) if (!_file.seek(14))
@ -75,41 +76,23 @@ bool OZF::initOZF3()
return false; return false;
_tileSize = *(h1 + 6); _tileSize = *(h1 + 6);
if (!_file.seek(15 + randomNumber)) if (!_file.seek(15 + randomNumber + 4))
return false; return false;
if (!readValue(keyblock)) if (_file.read((char*)h2, sizeof(h2)) != (qint64)sizeof(h2))
return false; return false;
switch (keyblock & 0xFF) { for (int i = 0; i < 256; i++) {
case 0xf1: memcpy(h2d, h2, sizeof(h2d));
initial += 0x8a; decrypt(h2d, sizeof(h2d), (quint8)i);
break;
case 0x18: if ((quint32)*h2d == 40 && (quint16)*(h2d + 12) == 1
case 0x54: && (quint16)*(h2d + 14) == 8) {
initial += 0xa0; _key = (quint8)i;
break; return true;
case 0x56: }
initial += 0xb9;
break;
case 0x43:
initial += 0x6a;
break;
case 0x83:
initial += 0xa4;
break;
case 0xc5:
initial += 0x7e;
break;
case 0x38:
initial += 0xc1;
break;
default:
break;
} }
_key = initial; return false;
return true;
} }
bool OZF::initOZF2() bool OZF::initOZF2()
@ -135,8 +118,10 @@ bool OZF::readHeaders()
} else if (magic == OZF3_MAGIC) { } else if (magic == OZF3_MAGIC) {
if (!initOZF3()) if (!initOZF3())
return false; return false;
} else } else {
qWarning("%s: not a OZF2/OZF3 file", qPrintable(_file.fileName()));
return false; return false;
}
return true; return true;
} }
@ -209,13 +194,13 @@ bool OZF::load(const QString &path)
return false; return false;
if (!readHeaders()) { if (!readHeaders()) {
qWarning("%s: not a OZF2/OZF3 file", qPrintable(_file.fileName())); qWarning("%s: Invalid header", qPrintable(_file.fileName()));
_file.close(); _file.close();
return false; return false;
} }
if (!readTileTable()) { if (!readTileTable()) {
qWarning("%s: file format error", qPrintable(_file.fileName())); qWarning("%s: Invalid tile table", qPrintable(_file.fileName()));
_file.close(); _file.close();
return false; return false;
} }