From 1bd42a73e612f50c68d802acda674c21a30e980c Mon Sep 17 00:00:00 2001 From: kat Date: Thu, 18 May 2023 12:44:11 -0400 Subject: Add ReadPointer to BinaryReader class --- binaryreader.cpp | 202 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) (limited to 'binaryreader.cpp') diff --git a/binaryreader.cpp b/binaryreader.cpp index a574f908..536f55c9 100644 --- a/binaryreader.cpp +++ b/binaryreader.cpp @@ -106,6 +106,17 @@ uint64_t BinaryReader::Read64() return result; } +uint64_t BinaryReader::ReadPointer() +{ + size_t addressSize = m_view->GetAddressSize(); + if (addressSize > 8 || addressSize == 0) + throw ReadException(); + uint64_t result; + if (!BNReadData(m_stream, &result, addressSize)) + throw ReadException(); + return result; +} + uint16_t BinaryReader::ReadLE16() { @@ -133,6 +144,51 @@ uint64_t BinaryReader::ReadLE64() return result; } +uint64_t BinaryReader::ReadLEPointer() +{ + uint64_t result; + size_t addressSize = m_view->GetAddressSize(); + switch (addressSize) + { + case 1: + { + uint8_t r; + if (!BNRead8(m_stream, &r)) + throw ReadException(); + result = r; + break; + } + case 2: + { + uint16_t r; + if (!BNReadLE16(m_stream, &r)) + throw ReadException(); + result = r; + break; + } + case 4: + { + uint32_t r; + if (!BNReadLE32(m_stream, &r)) + throw ReadException(); + result = r; + break; + } + case 8: + { + if (!BNReadLE64(m_stream, &result)) + throw ReadException(); + break; + } + default: + { + throw ReadException(); + break; + } + } + return result; +} + uint16_t BinaryReader::ReadBE16() { @@ -161,6 +217,52 @@ uint64_t BinaryReader::ReadBE64() } +uint64_t BinaryReader::ReadBEPointer() +{ + uint64_t result; + size_t addressSize = m_view->GetAddressSize(); + switch (addressSize) + { + case 1: + { + uint8_t r; + if (!BNRead8(m_stream, &r)) + throw ReadException(); + result = r; + break; + } + case 2: + { + uint16_t r; + if (!BNReadBE16(m_stream, &r)) + throw ReadException(); + result = r; + break; + } + case 4: + { + uint32_t r; + if (!BNReadBE32(m_stream, &r)) + throw ReadException(); + result = r; + break; + } + case 8: + { + if (!BNReadBE64(m_stream, &result)) + throw ReadException(); + break; + } + default: + { + throw ReadException(); + break; + } + } + return result; +} + + bool BinaryReader::TryRead(void* dest, size_t len) { return BNReadData(m_stream, dest, len); @@ -208,6 +310,17 @@ bool BinaryReader::TryRead64(uint64_t& result) } +bool BinaryReader::TryReadPointer(uint64_t& result) +{ + size_t addressSize = m_view->GetAddressSize(); + if (addressSize > 8 || addressSize == 0) + return false; + if (!BNReadData(m_stream, &result, addressSize)) + return false; + return true; +} + + bool BinaryReader::TryReadLE16(uint16_t& result) { return BNReadLE16(m_stream, &result); @@ -226,6 +339,50 @@ bool BinaryReader::TryReadLE64(uint64_t& result) } +bool BinaryReader::TryReadLEPointer(uint64_t& result) +{ + size_t addressSize = m_view->GetAddressSize(); + switch (addressSize) + { + case 1: + { + uint8_t r; + if (!BNRead8(m_stream, &r)) + return false; + result = r; + break; + } + case 2: + { + uint16_t r; + if (!BNReadLE16(m_stream, &r)) + return false; + result = r; + break; + } + case 4: + { + uint32_t r; + if (!BNReadLE32(m_stream, &r)) + return false; + result = r; + break; + } + case 8: + { + if (!BNReadLE64(m_stream, &result)) + return false; + break; + } + default: + { + return false; + } + } + return true; +} + + bool BinaryReader::TryReadBE16(uint16_t& result) { return BNReadBE16(m_stream, &result); @@ -244,6 +401,51 @@ bool BinaryReader::TryReadBE64(uint64_t& result) } + +bool BinaryReader::TryReadBEPointer(uint64_t& result) +{ + size_t addressSize = m_view->GetAddressSize(); + switch (addressSize) + { + case 1: + { + uint8_t r; + if (!BNRead8(m_stream, &r)) + return false; + result = r; + break; + } + case 2: + { + uint16_t r; + if (!BNReadBE16(m_stream, &r)) + return false; + result = r; + break; + } + case 4: + { + uint32_t r; + if (!BNReadBE32(m_stream, &r)) + return false; + result = r; + break; + } + case 8: + { + if (!BNReadBE64(m_stream, &result)) + return false; + break; + } + default: + { + return false; + } + } + return true; +} + + uint64_t BinaryReader::GetOffset() const { return BNGetReaderPosition(m_stream); -- cgit v1.3.1