summaryrefslogtreecommitdiff
path: root/binaryreader.cpp
diff options
context:
space:
mode:
authorkat <kat@vector35.com>2023-05-18 12:44:11 -0400
committerkat <kat@vector35.com>2023-05-18 12:44:33 -0400
commit1bd42a73e612f50c68d802acda674c21a30e980c (patch)
treec93704af02ae5c0b005cc9086400834179e21d1b /binaryreader.cpp
parentac3490ad00ba9d7d4298565168163b9becc66deb (diff)
Add ReadPointer to BinaryReader class
Diffstat (limited to 'binaryreader.cpp')
-rw-r--r--binaryreader.cpp202
1 files changed, 202 insertions, 0 deletions
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);