From 8196b96f658dc239ed60025a10c701a42b06c285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Fri, 19 Mar 2021 20:09:11 +0100 Subject: [PATCH] Fixed error handling --- src/common/garmin.h | 12 ++++++++++++ src/map/IMG/lblfile.cpp | 12 ------------ src/map/IMG/netfile.cpp | 4 ++-- src/map/IMG/nodfile.cpp | 35 +++++++++++++++-------------------- 4 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/common/garmin.h b/src/common/garmin.h index 57eef1c5..ddfa3f9c 100644 --- a/src/common/garmin.h +++ b/src/common/garmin.h @@ -26,4 +26,16 @@ inline quint8 bs(const quint8 val) return (val + 7) >> 3; } +inline quint8 byteSize(quint32 val) +{ + quint8 ret = 0; + + do { + ret++; + val = val >> 8; + } while (val != 0); + + return ret; +} + #endif // GARMIN_H diff --git a/src/map/IMG/lblfile.cpp b/src/map/IMG/lblfile.cpp index b4147afa..1a665db3 100644 --- a/src/map/IMG/lblfile.cpp +++ b/src/map/IMG/lblfile.cpp @@ -55,18 +55,6 @@ static QString capitalized(const QString &str) return ret; } -static quint8 byteSize(quint32 val) -{ - quint8 ret = 0; - - do { - ret++; - val = val >> 8; - } while (val != 0); - - return ret; -} - LBLFile::~LBLFile() { diff --git a/src/map/IMG/netfile.cpp b/src/map/IMG/netfile.cpp index d42e2ade..2423de72 100644 --- a/src/map/IMG/netfile.cpp +++ b/src/map/IMG/netfile.cpp @@ -381,7 +381,7 @@ bool NETFile::load(Handle &hdl, const RGNFile *rgn, Handle &rgnHdl) && readUInt32(hdl, _size) && readByte(hdl, &_shift))) return false; - if (hdrLen >= 0x47) { + if (hdrLen >= 0x4C) { quint32 info; if (!(seek(hdl, _gmpOffset + 0x37) && readUInt32(hdl, info))) return false; @@ -389,7 +389,7 @@ bool NETFile::load(Handle &hdl, const RGNFile *rgn, Handle &rgnHdl) && readUInt32(hdl, _linksSize) && readByte(hdl, &_linksShift))) return false; - quint8 tableId = ((info >> 2) & 0xF); + quint8 tableId = ((info >> 2) & 0x0F); if (_linksSize && (!rgn->huffmanTable() || rgn->huffmanTable()->id() != tableId)) { _huffmanTable = new HuffmanTable(tableId); diff --git a/src/map/IMG/nodfile.cpp b/src/map/IMG/nodfile.cpp index d2a3f145..20fd02f0 100644 --- a/src/map/IMG/nodfile.cpp +++ b/src/map/IMG/nodfile.cpp @@ -88,30 +88,25 @@ bool NODFile::load(Handle &hdl) if (!(seek(hdl, _gmpOffset) && readUInt16(hdl, hdrLen))) return false; - if (hdrLen < 0x7b) - return true; - if (!(seek(hdl, _gmpOffset + 0x1d) && readUInt32(hdl, _flags) - && readByte(hdl, &_blockShift) && readByte(hdl, &_nodeShift))) - return false; + if (hdrLen >= 0x7F) { + if (!(seek(hdl, _gmpOffset + 0x1d) && readUInt32(hdl, _flags) + && readByte(hdl, &_blockShift) && readByte(hdl, &_nodeShift))) + return false; - if (!(seek(hdl, _gmpOffset + 0x67) && readUInt32(hdl, _blockOffset) - && readUInt32(hdl, _blockSize) && readUInt16(hdl, _blockRecordSize) - && readUInt32(hdl, _indexOffset) && readUInt32(hdl, _indexSize) - && readUInt16(hdl, _indexRecordSize) && readUInt32(hdl, _indexFlags))) - return false; + if (!(seek(hdl, _gmpOffset + 0x67) && readUInt32(hdl, _blockOffset) + && readUInt32(hdl, _blockSize) && readUInt16(hdl, _blockRecordSize) + && readUInt32(hdl, _indexOffset) && readUInt32(hdl, _indexSize) + && readUInt16(hdl, _indexRecordSize) && readUInt32(hdl, _indexFlags))) + return false; - if (!_indexRecordSize) - return false; - quint32 indexCount = _indexSize / _indexRecordSize; - if (indexCount <= 0x100) - _indexIdSize = 1; - else if (indexCount <= 0x1000) - _indexIdSize = 2; - else if (indexCount <= 0x1000000) - _indexIdSize = 3; + if (!_indexRecordSize || _indexSize < _indexRecordSize) + return false; + quint32 indexCount = _indexSize / _indexRecordSize; + _indexIdSize = byteSize(indexCount - 1); + } - return (_indexIdSize > 0); + return true; } bool NODFile::readBlock(Handle &hdl, quint32 blockOffset,