From 03fdd995850e37e2ec168c26971910332a48d415 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Fri, 14 Jan 2022 19:46:32 -0600 Subject: [PATCH] Consistent usage of ValidateMessage() in level 3 packets --- .../rpg/linked_contour_vector_packet.cpp | 51 ++++++++------- .../scwx/wsr88d/rpg/linked_vector_packet.cpp | 63 ++++++++++--------- .../wsr88d/rpg/set_color_level_packet.cpp | 10 +++ .../rpg/unlinked_contour_vector_packet.cpp | 61 +++++++++++------- .../wsr88d/rpg/unlinked_vector_packet.cpp | 63 ++++++++++--------- 5 files changed, 146 insertions(+), 102 deletions(-) diff --git a/wxdata/source/scwx/wsr88d/rpg/linked_contour_vector_packet.cpp b/wxdata/source/scwx/wsr88d/rpg/linked_contour_vector_packet.cpp index e7be2913..cfc1714c 100644 --- a/wxdata/source/scwx/wsr88d/rpg/linked_contour_vector_packet.cpp +++ b/wxdata/source/scwx/wsr88d/rpg/linked_contour_vector_packet.cpp @@ -75,6 +75,8 @@ bool LinkedContourVectorPacket::Parse(std::istream& is) { bool blockValid = true; + std::streampos isBegin = is.tellg(); + is.read(reinterpret_cast(&p->packetCode_), 2); is.read(reinterpret_cast(&p->initialPointIndicator_), 2); is.read(reinterpret_cast(&p->startI_), 2); @@ -89,28 +91,6 @@ bool LinkedContourVectorPacket::Parse(std::istream& is) int vectorSize = static_cast(p->lengthOfVectors_); - if (is.eof()) - { - BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Reached end of file"; - blockValid = false; - } - - // The number of vectors is equal to the size divided by the number of bytes - // in a vector coordinate - int vectorCount = vectorSize / 4; - - p->endI_.resize(vectorCount); - p->endJ_.resize(vectorCount); - - for (int v = 0; v < vectorCount && !is.eof(); v++) - { - is.read(reinterpret_cast(&p->endI_[v]), 2); - is.read(reinterpret_cast(&p->endJ_[v]), 2); - - p->endI_[v] = ntohs(p->endI_[v]); - p->endJ_[v] = ntohs(p->endJ_[v]); - } - if (is.eof()) { BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Reached end of file"; @@ -133,6 +113,33 @@ bool LinkedContourVectorPacket::Parse(std::istream& is) } } + if (blockValid) + { + // The number of vectors is equal to the size divided by the number of + // bytes in a vector coordinate + int vectorCount = vectorSize / 4; + + p->endI_.resize(vectorCount); + p->endJ_.resize(vectorCount); + + for (int v = 0; v < vectorCount && !is.eof(); v++) + { + is.read(reinterpret_cast(&p->endI_[v]), 2); + is.read(reinterpret_cast(&p->endJ_[v]), 2); + + p->endI_[v] = ntohs(p->endI_[v]); + p->endJ_[v] = ntohs(p->endJ_[v]); + } + } + + std::streampos isEnd = is.tellg(); + std::streamoff bytesRead = isEnd - isBegin; + + if (!ValidateMessage(is, bytesRead)) + { + blockValid = false; + } + return blockValid; } diff --git a/wxdata/source/scwx/wsr88d/rpg/linked_vector_packet.cpp b/wxdata/source/scwx/wsr88d/rpg/linked_vector_packet.cpp index 344fcbcb..26b81047 100644 --- a/wxdata/source/scwx/wsr88d/rpg/linked_vector_packet.cpp +++ b/wxdata/source/scwx/wsr88d/rpg/linked_vector_packet.cpp @@ -81,6 +81,8 @@ bool LinkedVectorPacket::Parse(std::istream& is) { bool blockValid = true; + std::streampos isBegin = is.tellg(); + is.read(reinterpret_cast(&p->packetCode_), 2); is.read(reinterpret_cast(&p->lengthOfBlock_), 2); @@ -101,34 +103,6 @@ bool LinkedVectorPacket::Parse(std::istream& is) vectorSize -= 2; } - - is.read(reinterpret_cast(&p->startI_), 2); - is.read(reinterpret_cast(&p->startJ_), 2); - - p->startI_ = ntohs(p->startI_); - p->startJ_ = ntohs(p->startJ_); - - // The number of vectors is equal to the size divided by the number of bytes - // in a vector coordinate - int vectorCount = vectorSize / 4; - - p->endI_.resize(vectorCount); - p->endJ_.resize(vectorCount); - - for (int v = 0; v < vectorCount && !is.eof(); v++) - { - is.read(reinterpret_cast(&p->endI_[v]), 2); - is.read(reinterpret_cast(&p->endJ_[v]), 2); - - p->endI_[v] = ntohs(p->endI_[v]); - p->endJ_[v] = ntohs(p->endJ_[v]); - } - - if (is.eof()) - { - BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Reached end of file"; - blockValid = false; - } else { if (p->packetCode_ != 6 && p->packetCode_ != 9) @@ -139,6 +113,39 @@ bool LinkedVectorPacket::Parse(std::istream& is) } } + if (blockValid) + { + is.read(reinterpret_cast(&p->startI_), 2); + is.read(reinterpret_cast(&p->startJ_), 2); + + p->startI_ = ntohs(p->startI_); + p->startJ_ = ntohs(p->startJ_); + + // The number of vectors is equal to the size divided by the number of + // bytes in a vector coordinate + int vectorCount = vectorSize / 4; + + p->endI_.resize(vectorCount); + p->endJ_.resize(vectorCount); + + for (int v = 0; v < vectorCount && !is.eof(); v++) + { + is.read(reinterpret_cast(&p->endI_[v]), 2); + is.read(reinterpret_cast(&p->endJ_[v]), 2); + + p->endI_[v] = ntohs(p->endI_[v]); + p->endJ_[v] = ntohs(p->endJ_[v]); + } + } + + std::streampos isEnd = is.tellg(); + std::streamoff bytesRead = isEnd - isBegin; + + if (!ValidateMessage(is, bytesRead)) + { + blockValid = false; + } + return blockValid; } diff --git a/wxdata/source/scwx/wsr88d/rpg/set_color_level_packet.cpp b/wxdata/source/scwx/wsr88d/rpg/set_color_level_packet.cpp index 0d487f09..2382a0e5 100644 --- a/wxdata/source/scwx/wsr88d/rpg/set_color_level_packet.cpp +++ b/wxdata/source/scwx/wsr88d/rpg/set_color_level_packet.cpp @@ -64,6 +64,8 @@ bool SetColorLevelPacket::Parse(std::istream& is) { bool blockValid = true; + std::streampos isBegin = is.tellg(); + is.read(reinterpret_cast(&p->packetCode_), 2); is.read(reinterpret_cast(&p->colorValueIndicator_), 2); is.read(reinterpret_cast(&p->valueOfContour_), 2); @@ -94,6 +96,14 @@ bool SetColorLevelPacket::Parse(std::istream& is) } } + std::streampos isEnd = is.tellg(); + std::streamoff bytesRead = isEnd - isBegin; + + if (!ValidateMessage(is, bytesRead)) + { + blockValid = false; + } + return blockValid; } diff --git a/wxdata/source/scwx/wsr88d/rpg/unlinked_contour_vector_packet.cpp b/wxdata/source/scwx/wsr88d/rpg/unlinked_contour_vector_packet.cpp index 6ce7e9c2..6e86ee1c 100644 --- a/wxdata/source/scwx/wsr88d/rpg/unlinked_contour_vector_packet.cpp +++ b/wxdata/source/scwx/wsr88d/rpg/unlinked_contour_vector_packet.cpp @@ -70,36 +70,14 @@ bool UnlinkedContourVectorPacket::Parse(std::istream& is) { bool blockValid = true; + std::streampos isBegin = is.tellg(); + is.read(reinterpret_cast(&p->packetCode_), 2); is.read(reinterpret_cast(&p->lengthOfVectors_), 2); p->packetCode_ = ntohs(p->packetCode_); p->lengthOfVectors_ = ntohs(p->lengthOfVectors_); - int vectorSize = static_cast(p->lengthOfVectors_); - - // The number of vectors is equal to the size divided by the number of bytes - // in a vector - int vectorCount = vectorSize / 8; - - p->beginI_.resize(vectorCount); - p->beginJ_.resize(vectorCount); - p->endI_.resize(vectorCount); - p->endJ_.resize(vectorCount); - - for (int v = 0; v < vectorCount && !is.eof(); v++) - { - is.read(reinterpret_cast(&p->beginI_[v]), 2); - is.read(reinterpret_cast(&p->beginJ_[v]), 2); - is.read(reinterpret_cast(&p->endI_[v]), 2); - is.read(reinterpret_cast(&p->endJ_[v]), 2); - - p->beginI_[v] = ntohs(p->beginI_[v]); - p->beginJ_[v] = ntohs(p->beginJ_[v]); - p->endI_[v] = ntohs(p->endI_[v]); - p->endJ_[v] = ntohs(p->endJ_[v]); - } - if (is.eof()) { BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Reached end of file"; @@ -115,6 +93,41 @@ bool UnlinkedContourVectorPacket::Parse(std::istream& is) } } + if (blockValid) + { + int vectorSize = static_cast(p->lengthOfVectors_); + + // The number of vectors is equal to the size divided by the number of + // bytes in a vector + int vectorCount = vectorSize / 8; + + p->beginI_.resize(vectorCount); + p->beginJ_.resize(vectorCount); + p->endI_.resize(vectorCount); + p->endJ_.resize(vectorCount); + + for (int v = 0; v < vectorCount && !is.eof(); v++) + { + is.read(reinterpret_cast(&p->beginI_[v]), 2); + is.read(reinterpret_cast(&p->beginJ_[v]), 2); + is.read(reinterpret_cast(&p->endI_[v]), 2); + is.read(reinterpret_cast(&p->endJ_[v]), 2); + + p->beginI_[v] = ntohs(p->beginI_[v]); + p->beginJ_[v] = ntohs(p->beginJ_[v]); + p->endI_[v] = ntohs(p->endI_[v]); + p->endJ_[v] = ntohs(p->endJ_[v]); + } + } + + std::streampos isEnd = is.tellg(); + std::streamoff bytesRead = isEnd - isBegin; + + if (!ValidateMessage(is, bytesRead)) + { + blockValid = false; + } + return blockValid; } diff --git a/wxdata/source/scwx/wsr88d/rpg/unlinked_vector_packet.cpp b/wxdata/source/scwx/wsr88d/rpg/unlinked_vector_packet.cpp index a891f680..0cb9708b 100644 --- a/wxdata/source/scwx/wsr88d/rpg/unlinked_vector_packet.cpp +++ b/wxdata/source/scwx/wsr88d/rpg/unlinked_vector_packet.cpp @@ -82,6 +82,8 @@ bool UnlinkedVectorPacket::Parse(std::istream& is) { bool blockValid = true; + std::streampos isBegin = is.tellg(); + is.read(reinterpret_cast(&p->packetCode_), 2); is.read(reinterpret_cast(&p->lengthOfBlock_), 2); @@ -102,34 +104,6 @@ bool UnlinkedVectorPacket::Parse(std::istream& is) vectorSize -= 2; } - - // The number of vectors is equal to the size divided by the number of bytes - // in a vector - int vectorCount = vectorSize / 8; - - p->beginI_.resize(vectorCount); - p->beginJ_.resize(vectorCount); - p->endI_.resize(vectorCount); - p->endJ_.resize(vectorCount); - - for (int v = 0; v < vectorCount && !is.eof(); v++) - { - is.read(reinterpret_cast(&p->beginI_[v]), 2); - is.read(reinterpret_cast(&p->beginJ_[v]), 2); - is.read(reinterpret_cast(&p->endI_[v]), 2); - is.read(reinterpret_cast(&p->endJ_[v]), 2); - - p->beginI_[v] = ntohs(p->beginI_[v]); - p->beginJ_[v] = ntohs(p->beginJ_[v]); - p->endI_[v] = ntohs(p->endI_[v]); - p->endJ_[v] = ntohs(p->endJ_[v]); - } - - if (is.eof()) - { - BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Reached end of file"; - blockValid = false; - } else { if (p->packetCode_ != 7 && p->packetCode_ != 10) @@ -140,6 +114,39 @@ bool UnlinkedVectorPacket::Parse(std::istream& is) } } + if (blockValid) + { + // The number of vectors is equal to the size divided by the number of + // bytes in a vector + int vectorCount = vectorSize / 8; + + p->beginI_.resize(vectorCount); + p->beginJ_.resize(vectorCount); + p->endI_.resize(vectorCount); + p->endJ_.resize(vectorCount); + + for (int v = 0; v < vectorCount && !is.eof(); v++) + { + is.read(reinterpret_cast(&p->beginI_[v]), 2); + is.read(reinterpret_cast(&p->beginJ_[v]), 2); + is.read(reinterpret_cast(&p->endI_[v]), 2); + is.read(reinterpret_cast(&p->endJ_[v]), 2); + + p->beginI_[v] = ntohs(p->beginI_[v]); + p->beginJ_[v] = ntohs(p->beginJ_[v]); + p->endI_[v] = ntohs(p->endI_[v]); + p->endJ_[v] = ntohs(p->endJ_[v]); + } + } + + std::streampos isEnd = is.tellg(); + std::streamoff bytesRead = isEnd - isBegin; + + if (!ValidateMessage(is, bytesRead)) + { + blockValid = false; + } + return blockValid; }