From 14a9c0a8cba4c5bd6802f5f2278212234f2caeef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Mon, 11 Jan 2021 23:38:46 +0100 Subject: [PATCH] A little bit more sane binary search --- src/map/IMG/nodfile.cpp | 52 +++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/src/map/IMG/nodfile.cpp b/src/map/IMG/nodfile.cpp index b0a55108..eafe7b1a 100644 --- a/src/map/IMG/nodfile.cpp +++ b/src/map/IMG/nodfile.cpp @@ -1,4 +1,4 @@ -#include "bitstream.h" +#include "bitstream.h" #include "nodfile.h" @@ -257,7 +257,7 @@ bool NODFile::nodeBlock(Handle &hdl, quint32 nodeOffset, quint32 offsetSize = (_indexFlags & 3) + 1; while (low <= high) { - quint32 m = ((low + high) / 2); + int m = ((low + high) / 2); quint32 offset = _indexRecordSize * m + _indexOffset; quint32 blockOffset, prevBlockOffset; @@ -507,40 +507,32 @@ bool NODFile::linkType(Handle &hdl, const BlockInfo &blockInfo, quint8 linkId, quint32 offset = ((blockInfo.hdr.s10 * blockInfo.hdr.se + 7) >> 3) + 0x13 + blockInfo.offset + _blockOffset + ((blockInfo.hdr.s0 >> 0xb) & 1) + blockInfo.hdr.s11 * 3; - quint32 low = 0; - quint32 high = blockInfo.hdr.s12 - 1; - quint32 pos = blockInfo.hdr.s12; + int low = 0; + int high = blockInfo.hdr.s12 - 1; quint16 val; - if (high > 1) { - do { - pos = (low + high) / 2; + while (low <= high) { + int m = ((low + high) / 2); - if (!(seek(hdl, offset + _blockRecordSize * pos) - && readUInt16(hdl, val))) - return false; - - if ((val >> 8) <= linkId) - low = pos; - else - high = pos; - } while (low + 1 < high); - } - - if (!(seek(hdl, offset + _blockRecordSize * low) && readUInt16(hdl, val))) - return false; - - type = val & 0x3f; - - if ((low < high) && (pos != high)) { - if (!(seek(hdl, offset + _blockRecordSize * high) - && readUInt16(hdl, val))) + if (!(seek(hdl, offset + _blockRecordSize * m) && readUInt16(hdl, val))) return false; - if ((val >> 8) <= linkId) - type = (val & 0x3f); + + if ((val >> 8) < linkId) + low = m + 1; + else if ((val >> 8) > linkId) + high = m - 1; + else { + type = (val & 0x3f) << 8; + return true; + } } - type <<= 8; + if (high < 0) + return false; + if (blockInfo.hdr.s12 > 1 && !(seek(hdl, offset + _blockRecordSize * high) + && readUInt16(hdl, val))) + return false; + type = (val & 0x3f) << 8; return true; }