Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions binaryninjaapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -9006,6 +9006,8 @@ namespace BinaryNinja {
Ref<BinaryView> m_view;
BNBinaryReader* m_stream;

BinaryReader(Ref<BinaryView> data, BNBinaryReader* stream);

public:
/*! Create a BinaryReader instance given a BinaryView and endianness.

Expand Down Expand Up @@ -9090,6 +9092,20 @@ namespace BinaryNinja {
*/
uint64_t Read64();

/*! Read a signed LEB128 from the current cursor position

\throws ReadException
\return The read value
*/
int64_t ReadSLEB128();

/*! Read an unsigned LEB128 from the current cursor position

\throws ReadException
\return The read value
*/
uint64_t ReadULEB128();

/*! Read a pointer (size of BinaryView::GetAddressSize()) from the current cursor position and advance
and advance it that many bytes

Expand Down Expand Up @@ -9214,6 +9230,18 @@ namespace BinaryNinja {
*/
bool TryRead64(uint64_t& result);

/*! Try reading an unsigned LEB128 value.

The cursor and result are unchanged on failure.
*/
bool TryReadULEB128(uint64_t& result);

/*! Try reading a signed LEB128 value.

The cursor and result are unchanged on failure.
*/
bool TryReadSLEB128(int64_t& result);

/*! Try reading a pointer (size of BinaryView::GetAddressSize())

\param result Reference to a uint64_t to write to
Expand Down Expand Up @@ -9311,6 +9339,15 @@ namespace BinaryNinja {

*/
bool IsEndOfFile() const;

/*! Create a new reader that represents a bounded slice of this reader

\param offset The start offset for the new slice
\param length The length of the new slice

\return A new reader representing this slice
*/
BinaryReader Slice(uint64_t offset, size_t length) const;
};

/*! Raised whenever a write is performed out of bounds.
Expand Down
3 changes: 3 additions & 0 deletions binaryninjacore.h
Original file line number Diff line number Diff line change
Expand Up @@ -5649,6 +5649,7 @@ extern "C"
// Stream reader object
BINARYNINJACOREAPI BNBinaryReader* BNCreateBinaryReader(BNBinaryView* view);
BINARYNINJACOREAPI void BNFreeBinaryReader(BNBinaryReader* stream);
BINARYNINJACOREAPI BNBinaryReader* BNSliceBinaryReader(BNBinaryReader* stream, uint64_t offset, size_t length);
BINARYNINJACOREAPI BNEndianness BNGetBinaryReaderEndianness(BNBinaryReader* stream);
BINARYNINJACOREAPI void BNSetBinaryReaderEndianness(BNBinaryReader* stream, BNEndianness endian);

Expand All @@ -5663,6 +5664,8 @@ extern "C"
BINARYNINJACOREAPI bool BNReadBE16(BNBinaryReader* stream, uint16_t* result);
BINARYNINJACOREAPI bool BNReadBE32(BNBinaryReader* stream, uint32_t* result);
BINARYNINJACOREAPI bool BNReadBE64(BNBinaryReader* stream, uint64_t* result);
BINARYNINJACOREAPI bool BNReadSLEB128(BNBinaryReader* stream, int64_t* result);
BINARYNINJACOREAPI bool BNReadULEB128(BNBinaryReader* stream, uint64_t* result);
BINARYNINJACOREAPI bool BNReadPointer(BNBinaryView* view, BNBinaryReader* stream, uint64_t* result);

BINARYNINJACOREAPI uint64_t BNGetReaderPosition(BNBinaryReader* stream);
Expand Down
41 changes: 41 additions & 0 deletions binaryreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ BinaryReader::BinaryReader(BinaryView* data, BNEndianness endian) : m_view(data)
}


BinaryReader::BinaryReader(Ref<BinaryView> data, BNBinaryReader* stream) : m_view(data), m_stream(stream)
{
}


BinaryReader::~BinaryReader()
{
BNFreeBinaryReader(m_stream);
Expand Down Expand Up @@ -219,6 +224,24 @@ uint64_t BinaryReader::ReadBE64()
}


uint64_t BinaryReader::ReadULEB128()
{
uint64_t result;
if (!BNReadULEB128(m_stream, &result))
throw ReadException();
return result;
}


int64_t BinaryReader::ReadSLEB128()
{
int64_t result;
if (!BNReadSLEB128(m_stream, &result))
throw ReadException();
return result;
}


uint64_t BinaryReader::ReadBEPointer()
{
uint64_t result;
Expand Down Expand Up @@ -312,6 +335,18 @@ bool BinaryReader::TryRead64(uint64_t& result)
}


bool BinaryReader::TryReadSLEB128(int64_t& result)
{
return BNReadSLEB128(m_stream, &result);
}


bool BinaryReader::TryReadULEB128(uint64_t& result)
{
return BNReadULEB128(m_stream, &result);
}


bool BinaryReader::TryReadPointer(uint64_t& result)
{
return BNReadPointer(m_view->GetObject(), m_stream, &result);
Expand Down Expand Up @@ -443,6 +478,12 @@ bool BinaryReader::TryReadBEPointer(uint64_t& result)
}


BinaryReader BinaryReader::Slice(uint64_t offset, size_t length) const
{
return BinaryReader(m_view, BNSliceBinaryReader(m_stream, offset, length));
}


uint64_t BinaryReader::GetOffset() const
{
return BNGetReaderPosition(m_stream);
Expand Down
Loading