diff options
| author | kat <kat@vector35.com> | 2023-05-18 12:44:11 -0400 |
|---|---|---|
| committer | kat <kat@vector35.com> | 2023-05-18 12:44:33 -0400 |
| commit | 1bd42a73e612f50c68d802acda674c21a30e980c (patch) | |
| tree | c93704af02ae5c0b005cc9086400834179e21d1b | |
| parent | ac3490ad00ba9d7d4298565168163b9becc66deb (diff) | |
Add ReadPointer to BinaryReader class
| -rw-r--r-- | binaryninjaapi.h | 45 | ||||
| -rw-r--r-- | binaryreader.cpp | 202 |
2 files changed, 247 insertions, 0 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 21549c92..f4b1d9dc 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -5516,6 +5516,14 @@ namespace BinaryNinja { */ uint64_t Read64(); + /*! Read a pointer (size of BinaryView::GetAddressSize()) from the current cursor position and advance + and advance it that many bytes + + \throws ReadException + \return The value that was read + */ + uint64_t ReadPointer(); + /*! Read a uint16_t from the current cursor position, explicitly as a little endian value, and advance the cursor by 4 bytes @@ -5540,6 +5548,14 @@ namespace BinaryNinja { */ uint64_t ReadLE64(); + /*! Read a pointer (size of BinaryView::GetAddressSize()) as little-endian from the current cursor + position and advance and advance it that many bytes + + \throws ReadException + \return The value that was read + */ + uint64_t ReadLEPointer(); + /*! Read a uint16_t from the current cursor position, explicitly as a big endian value, and advance the cursor by 4 bytes @@ -5564,6 +5580,14 @@ namespace BinaryNinja { */ uint64_t ReadBE64(); + /*! Read a pointer (size of BinaryView::GetAddressSize()) as big-endian from the current cursor + position and advance and advance it that many bytes + + \throws ReadException + \return The value that was read + */ + uint64_t ReadBEPointer(); + /*! Try reading a value, returning false whenever that read fails \param dest Address to write the bytes to @@ -5616,6 +5640,13 @@ namespace BinaryNinja { */ bool TryRead64(uint64_t& result); + /*! Try reading a pointer (size of BinaryView::GetAddressSize()) + + \param result Reference to a uint64_t to write to + \return Whether the read succeeded. + */ + bool TryReadPointer(uint64_t& result); + /*! Try reading a uint16_t, explicitly as little endian \param result Reference to a uint16_t to write to @@ -5637,6 +5668,13 @@ namespace BinaryNinja { */ bool TryReadLE64(uint64_t& result); + /*! Try reading a pointer (size of BinaryView::GetAddressSize()) as little-endian + + \param result Reference to a uint64_t to write to + \return Whether the read succeeded. + */ + bool TryReadLEPointer(uint64_t& result); + /*! Try reading a uint16_t, explicitly as big endian \param result Reference to a uint16_t to write to @@ -5658,6 +5696,13 @@ namespace BinaryNinja { */ bool TryReadBE64(uint64_t& result); + /*! Try reading a pointer (size of BinaryView::GetAddressSize()) as big-endian + + \param result Reference to a uint64_t to write to + \return Whether the read succeeded. + */ + bool TryReadBEPointer(uint64_t& result); + /*! Get the current cursor position \return The current cursor position 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); |
