Skip to content

Commit 1bd42a7

Browse files
author
kat
committed
Add ReadPointer to BinaryReader class
1 parent ac3490a commit 1bd42a7

2 files changed

Lines changed: 247 additions & 0 deletions

File tree

‎binaryninjaapi.h‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5516,6 +5516,14 @@ namespace BinaryNinja {
55165516
*/
55175517
uint64_t Read64();
55185518

5519+
/*! Read a pointer (size of BinaryView::GetAddressSize()) from the current cursor position and advance
5520+
and advance it that many bytes
5521+
5522+
\throws ReadException
5523+
\return The value that was read
5524+
*/
5525+
uint64_t ReadPointer();
5526+
55195527
/*! Read a uint16_t from the current cursor position, explicitly as a little endian value,
55205528
and advance the cursor by 4 bytes
55215529

@@ -5540,6 +5548,14 @@ namespace BinaryNinja {
55405548
*/
55415549
uint64_t ReadLE64();
55425550

5551+
/*! Read a pointer (size of BinaryView::GetAddressSize()) as little-endian from the current cursor
5552+
position and advance and advance it that many bytes
5553+
5554+
\throws ReadException
5555+
\return The value that was read
5556+
*/
5557+
uint64_t ReadLEPointer();
5558+
55435559
/*! Read a uint16_t from the current cursor position, explicitly as a big endian value,
55445560
and advance the cursor by 4 bytes
55455561

@@ -5564,6 +5580,14 @@ namespace BinaryNinja {
55645580
*/
55655581
uint64_t ReadBE64();
55665582

5583+
/*! Read a pointer (size of BinaryView::GetAddressSize()) as big-endian from the current cursor
5584+
position and advance and advance it that many bytes
5585+
5586+
\throws ReadException
5587+
\return The value that was read
5588+
*/
5589+
uint64_t ReadBEPointer();
5590+
55675591
/*! Try reading a value, returning false whenever that read fails
55685592

55695593
\param dest Address to write the bytes to
@@ -5616,6 +5640,13 @@ namespace BinaryNinja {
56165640
*/
56175641
bool TryRead64(uint64_t& result);
56185642

5643+
/*! Try reading a pointer (size of BinaryView::GetAddressSize())
5644+
5645+
\param result Reference to a uint64_t to write to
5646+
\return Whether the read succeeded.
5647+
*/
5648+
bool TryReadPointer(uint64_t& result);
5649+
56195650
/*! Try reading a uint16_t, explicitly as little endian
56205651

56215652
\param result Reference to a uint16_t to write to
@@ -5637,6 +5668,13 @@ namespace BinaryNinja {
56375668
*/
56385669
bool TryReadLE64(uint64_t& result);
56395670

5671+
/*! Try reading a pointer (size of BinaryView::GetAddressSize()) as little-endian
5672+
5673+
\param result Reference to a uint64_t to write to
5674+
\return Whether the read succeeded.
5675+
*/
5676+
bool TryReadLEPointer(uint64_t& result);
5677+
56405678
/*! Try reading a uint16_t, explicitly as big endian
56415679

56425680
\param result Reference to a uint16_t to write to
@@ -5658,6 +5696,13 @@ namespace BinaryNinja {
56585696
*/
56595697
bool TryReadBE64(uint64_t& result);
56605698

5699+
/*! Try reading a pointer (size of BinaryView::GetAddressSize()) as big-endian
5700+
5701+
\param result Reference to a uint64_t to write to
5702+
\return Whether the read succeeded.
5703+
*/
5704+
bool TryReadBEPointer(uint64_t& result);
5705+
56615706
/*! Get the current cursor position
56625707

56635708
\return The current cursor position

‎binaryreader.cpp‎

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ uint64_t BinaryReader::Read64()
106106
return result;
107107
}
108108

109+
uint64_t BinaryReader::ReadPointer()
110+
{
111+
size_t addressSize = m_view->GetAddressSize();
112+
if (addressSize > 8 || addressSize == 0)
113+
throw ReadException();
114+
uint64_t result;
115+
if (!BNReadData(m_stream, &result, addressSize))
116+
throw ReadException();
117+
return result;
118+
}
119+
109120

110121
uint16_t BinaryReader::ReadLE16()
111122
{
@@ -133,6 +144,51 @@ uint64_t BinaryReader::ReadLE64()
133144
return result;
134145
}
135146

147+
uint64_t BinaryReader::ReadLEPointer()
148+
{
149+
uint64_t result;
150+
size_t addressSize = m_view->GetAddressSize();
151+
switch (addressSize)
152+
{
153+
case 1:
154+
{
155+
uint8_t r;
156+
if (!BNRead8(m_stream, &r))
157+
throw ReadException();
158+
result = r;
159+
break;
160+
}
161+
case 2:
162+
{
163+
uint16_t r;
164+
if (!BNReadLE16(m_stream, &r))
165+
throw ReadException();
166+
result = r;
167+
break;
168+
}
169+
case 4:
170+
{
171+
uint32_t r;
172+
if (!BNReadLE32(m_stream, &r))
173+
throw ReadException();
174+
result = r;
175+
break;
176+
}
177+
case 8:
178+
{
179+
if (!BNReadLE64(m_stream, &result))
180+
throw ReadException();
181+
break;
182+
}
183+
default:
184+
{
185+
throw ReadException();
186+
break;
187+
}
188+
}
189+
return result;
190+
}
191+
136192

137193
uint16_t BinaryReader::ReadBE16()
138194
{
@@ -161,6 +217,52 @@ uint64_t BinaryReader::ReadBE64()
161217
}
162218

163219

220+
uint64_t BinaryReader::ReadBEPointer()
221+
{
222+
uint64_t result;
223+
size_t addressSize = m_view->GetAddressSize();
224+
switch (addressSize)
225+
{
226+
case 1:
227+
{
228+
uint8_t r;
229+
if (!BNRead8(m_stream, &r))
230+
throw ReadException();
231+
result = r;
232+
break;
233+
}
234+
case 2:
235+
{
236+
uint16_t r;
237+
if (!BNReadBE16(m_stream, &r))
238+
throw ReadException();
239+
result = r;
240+
break;
241+
}
242+
case 4:
243+
{
244+
uint32_t r;
245+
if (!BNReadBE32(m_stream, &r))
246+
throw ReadException();
247+
result = r;
248+
break;
249+
}
250+
case 8:
251+
{
252+
if (!BNReadBE64(m_stream, &result))
253+
throw ReadException();
254+
break;
255+
}
256+
default:
257+
{
258+
throw ReadException();
259+
break;
260+
}
261+
}
262+
return result;
263+
}
264+
265+
164266
bool BinaryReader::TryRead(void* dest, size_t len)
165267
{
166268
return BNReadData(m_stream, dest, len);
@@ -208,6 +310,17 @@ bool BinaryReader::TryRead64(uint64_t& result)
208310
}
209311

210312

313+
bool BinaryReader::TryReadPointer(uint64_t& result)
314+
{
315+
size_t addressSize = m_view->GetAddressSize();
316+
if (addressSize > 8 || addressSize == 0)
317+
return false;
318+
if (!BNReadData(m_stream, &result, addressSize))
319+
return false;
320+
return true;
321+
}
322+
323+
211324
bool BinaryReader::TryReadLE16(uint16_t& result)
212325
{
213326
return BNReadLE16(m_stream, &result);
@@ -226,6 +339,50 @@ bool BinaryReader::TryReadLE64(uint64_t& result)
226339
}
227340

228341

342+
bool BinaryReader::TryReadLEPointer(uint64_t& result)
343+
{
344+
size_t addressSize = m_view->GetAddressSize();
345+
switch (addressSize)
346+
{
347+
case 1:
348+
{
349+
uint8_t r;
350+
if (!BNRead8(m_stream, &r))
351+
return false;
352+
result = r;
353+
break;
354+
}
355+
case 2:
356+
{
357+
uint16_t r;
358+
if (!BNReadLE16(m_stream, &r))
359+
return false;
360+
result = r;
361+
break;
362+
}
363+
case 4:
364+
{
365+
uint32_t r;
366+
if (!BNReadLE32(m_stream, &r))
367+
return false;
368+
result = r;
369+
break;
370+
}
371+
case 8:
372+
{
373+
if (!BNReadLE64(m_stream, &result))
374+
return false;
375+
break;
376+
}
377+
default:
378+
{
379+
return false;
380+
}
381+
}
382+
return true;
383+
}
384+
385+
229386
bool BinaryReader::TryReadBE16(uint16_t& result)
230387
{
231388
return BNReadBE16(m_stream, &result);
@@ -244,6 +401,51 @@ bool BinaryReader::TryReadBE64(uint64_t& result)
244401
}
245402

246403

404+
405+
bool BinaryReader::TryReadBEPointer(uint64_t& result)
406+
{
407+
size_t addressSize = m_view->GetAddressSize();
408+
switch (addressSize)
409+
{
410+
case 1:
411+
{
412+
uint8_t r;
413+
if (!BNRead8(m_stream, &r))
414+
return false;
415+
result = r;
416+
break;
417+
}
418+
case 2:
419+
{
420+
uint16_t r;
421+
if (!BNReadBE16(m_stream, &r))
422+
return false;
423+
result = r;
424+
break;
425+
}
426+
case 4:
427+
{
428+
uint32_t r;
429+
if (!BNReadBE32(m_stream, &r))
430+
return false;
431+
result = r;
432+
break;
433+
}
434+
case 8:
435+
{
436+
if (!BNReadBE64(m_stream, &result))
437+
return false;
438+
break;
439+
}
440+
default:
441+
{
442+
return false;
443+
}
444+
}
445+
return true;
446+
}
447+
448+
247449
uint64_t BinaryReader::GetOffset() const
248450
{
249451
return BNGetReaderPosition(m_stream);

0 commit comments

Comments
 (0)