1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-27 21:24:47 +01:00

Improved error handling + code cleanup

This commit is contained in:
Martin Tůma 2022-04-13 22:13:57 +02:00
parent 82ea54cacc
commit 53cdc86aa0
2 changed files with 25 additions and 25 deletions

View File

@ -6,6 +6,8 @@ using namespace IMG;
#define ARRAY_SIZE(array) \ #define ARRAY_SIZE(array) \
(sizeof(array) / sizeof(array[0])) (sizeof(array) / sizeof(array[0]))
#define DELTA(val, s1, s2) \
(((int)((val) << (s1)) >> (s1)) << (s2))
static const struct static const struct
{ {
@ -166,6 +168,7 @@ bool NODFile::linkInfo(Handle &hdl, const BlockInfo &blockInfo, quint32 linkId,
if (!(linkInfo.flags & 0x100)) { if (!(linkInfo.flags & 0x100)) {
if (!bs.read(s1, linkInfo.linkOffset)) if (!bs.read(s1, linkInfo.linkOffset))
return false; return false;
linkInfo.nodeOffset = 0xFFFFFFFF;
} else { } else {
if (!bs.read(s1 - s2, linkInfo.linkOffset)) if (!bs.read(s1 - s2, linkInfo.linkOffset))
return false; return false;
@ -180,50 +183,48 @@ bool NODFile::linkInfo(Handle &hdl, const BlockInfo &blockInfo, quint32 linkId,
return true; return true;
} }
bool NODFile::nodeInfo(Handle &hdl, const BlockInfo &blockInfo, bool NODFile::nodeInfo(Handle &hdl, AdjacencyInfo &adj) const
quint32 nodeOffset, NodeInfo &nodeInfo) const
{ {
quint32 infoOffset = (nodeOffset << _nodeShift) + _block.offset; quint32 infoOffset = (adj.nodeOffset << _nodeShift) + _block.offset;
if (infoOffset > _block.offset + _block.size || infoOffset < blockInfo.offset) if (infoOffset > _block.offset + _block.size
|| infoOffset < adj.blockInfo.offset)
return false; return false;
if (!seek(hdl, infoOffset)) if (!seek(hdl, infoOffset))
return false; return false;
BitStream1 bs(*this, hdl, _block.offset + _block.size - infoOffset); BitStream1 bs(*this, hdl, _block.offset + _block.size - infoOffset);
if (!bs.read(8, nodeInfo.flags)) if (!bs.read(8, adj.nodeInfo.flags))
return false; return false;
if ((nodeInfo.flags & 7) >= ARRAY_SIZE(LLBITS)) if ((adj.nodeInfo.flags & 7) >= ARRAY_SIZE(LLBITS))
return false; return false;
quint8 lonBits = LLBITS[nodeInfo.flags & 7].lon; quint8 lonBits = LLBITS[adj.nodeInfo.flags & 7].lon;
quint8 latBits = LLBITS[nodeInfo.flags & 7].lat; quint8 latBits = LLBITS[adj.nodeInfo.flags & 7].lat;
quint8 maxBits = ((_flags >> 10) & 7) | 0x18; quint8 maxBits = ((_flags >> 10) & 7) | 0x18;
quint32 lon, lat; quint32 lon, lat;
if (!(bs.read(lonBits, lon) && bs.read(latBits, lat))) if (!(bs.read(lonBits, lon) && bs.read(latBits, lat)))
return false; return false;
quint8 lonShift = 0x20 - lonBits;
quint8 latShift = 0x20 - latBits;
quint8 shift = 0x20 - maxBits; quint8 shift = 0x20 - maxBits;
QPoint pos( QPoint pos(
(((int)(lon << lonShift) >> lonShift) << shift) + blockInfo.hdr.nodeLonBase, adj.blockInfo.hdr.nodeLonBase + DELTA(lon, 0x20 - lonBits, shift),
(((int)(lat << latShift) >> latShift) << shift) + blockInfo.hdr.nodeLatBase); adj.blockInfo.hdr.nodeLatBase + DELTA(lat, 0x20 - latBits, shift));
nodeInfo.bytes = ((lonBits + latBits) >> 3) + 1; adj.nodeInfo.bytes = ((lonBits + latBits) >> 3) + 1;
if ((maxBits < 0x1c) && (nodeInfo.flags & 8)) { if ((maxBits < 0x1c) && (adj.nodeInfo.flags & 8)) {
quint8 extraBits = 0x1c - maxBits; quint8 extraBits = 0x1c - maxBits;
quint32 extraLon, extraLat; quint32 extraLon, extraLat;
if (!(bs.read(extraBits, extraLon) && bs.read(extraBits, extraLat))) if (!(bs.read(extraBits, extraLon) && bs.read(extraBits, extraLat)))
return false; return false;
pos.setX(pos.x() | extraLon << 4); pos.setY(pos.y() | extraLat << 4); pos.setX(pos.x() | extraLon << 4); pos.setY(pos.y() | extraLat << 4);
nodeInfo.bytes++; adj.nodeInfo.bytes++;
} }
// TODO?: check and adjust (shift) coordinates // TODO?: check and adjust (shift) coordinates
nodeInfo.pos = pos; adj.nodeInfo.pos = pos;
return true; return true;
} }
@ -480,14 +481,14 @@ bool NODFile::relAdjInfo(Handle &hdl, AdjacencyInfo &adj) const
return true; return true;
} }
int NODFile::nextNode(Handle &hdl, AdjacencyInfo &adjInfo) const int NODFile::nextNode(Handle &hdl, AdjacencyInfo &adj) const
{ {
if (adjInfo.nodeOffset == 0xFFFFFFFF) if (adj.nodeOffset == 0xFFFFFFFF)
return 1; return 1;
if (!nodeInfo(hdl, adjInfo.blockInfo, adjInfo.nodeOffset, adjInfo.nodeInfo)) if (!nodeInfo(hdl, adj))
return -1; return -1;
if (!adjacencyInfo(hdl, adjInfo)) if (!adjacencyInfo(hdl, adj))
return -1; return -1;
return 0; return 0;

View File

@ -78,13 +78,14 @@ public:
LinkInfo &linkInfo) const; LinkInfo &linkInfo) const;
bool linkType(Handle &hdl, const BlockInfo &blockInfo, quint8 linkId, bool linkType(Handle &hdl, const BlockInfo &blockInfo, quint8 linkId,
quint32 &type) const; quint32 &type) const;
int nextNode(Handle &hdl, AdjacencyInfo &adjInfo) const; int nextNode(Handle &hdl, AdjacencyInfo &adj) const;
private: private:
bool nodeInfo(Handle &hdl, const BlockInfo &blockInfo, quint32 nodeOffset,
NodeInfo &nodeInfo) const;
bool nodeOffset(Handle &hdl, const BlockInfo &blockInfo, quint8 nodeId, bool nodeOffset(Handle &hdl, const BlockInfo &blockInfo, quint8 nodeId,
quint32 &nodeOffset) const; quint32 &nodeOffset) const;
bool nodeBlock(Handle &hdl, quint32 nodeOffset, BlockInfo &blockInfo) const;
bool readBlock(Handle &hdl, quint32 blockOffset, BlockInfo &blockInfo) const;
bool nodeInfo(Handle &hdl, AdjacencyInfo &adj) const;
bool absAdjInfo(Handle &hdl, AdjacencyInfo &adj) const; bool absAdjInfo(Handle &hdl, AdjacencyInfo &adj) const;
bool relAdjInfo(Handle &hdl, AdjacencyInfo &adj) const; bool relAdjInfo(Handle &hdl, AdjacencyInfo &adj) const;
bool adjacencyInfo(Handle &hdl, AdjacencyInfo &adj) const bool adjacencyInfo(Handle &hdl, AdjacencyInfo &adj) const
@ -92,8 +93,6 @@ private:
return (adj.nodeInfo.flags & 0x20) ? absAdjInfo(hdl, adj) return (adj.nodeInfo.flags & 0x20) ? absAdjInfo(hdl, adj)
: relAdjInfo(hdl, adj); : relAdjInfo(hdl, adj);
} }
bool nodeBlock(Handle &hdl, quint32 nodeOffset, BlockInfo &blockInfo) const;
bool readBlock(Handle &hdl, quint32 blockOffset, BlockInfo &blockInfo) const;
Section _block, _index; Section _block, _index;
quint32 _flags, _indexFlags; quint32 _flags, _indexFlags;