retdec
Public Member Functions | Private Member Functions | Private Attributes | List of all members
retdec::utils::DynamicBuffer Class Reference

The class for dynamic buffered data manipulation taking the endianness of the data in account. More...

#include <dynamic_buffer.h>

Collaboration diagram for retdec::utils::DynamicBuffer:
Collaboration graph
[legend]

Public Member Functions

 DynamicBuffer (retdec::utils::Endianness endianness=retdec::utils::Endianness::LITTLE)
 
 DynamicBuffer (uint32_t capacity, retdec::utils::Endianness endianness=retdec::utils::Endianness::LITTLE)
 
 DynamicBuffer (const std::vector< uint8_t > &data, retdec::utils::Endianness endianness=retdec::utils::Endianness::LITTLE)
 
 DynamicBuffer (const DynamicBuffer &dynamicBuffer)
 
 DynamicBuffer (const DynamicBuffer &dynamicBuffer, uint32_t startPos, uint32_t amount)
 
DynamicBufferoperator= (DynamicBuffer dynamicBuffer)
 
void setCapacity (uint32_t capacity)
 
uint32_t getCapacity () const
 
void setEndianness (retdec::utils::Endianness endianness)
 
retdec::utils::Endianness getEndianness () const
 
uint32_t getRealDataSize () const
 
void erase (uint32_t startPos, uint32_t amount)
 
const uint8_t * getRawBuffer () const
 
std::vector< uint8_t > getBuffer () const
 
void forEach (const std::function< void(uint8_t &)> &func)
 
void forEachReverse (const std::function< void(uint8_t &)> &func)
 
template<typename T >
read (uint32_t pos, retdec::utils::Endianness endianness=retdec::utils::Endianness::UNKNOWN) const
 
std::string readString (uint32_t pos, uint32_t maxLength=0) const
 
template<typename T >
void write (const T &data, uint32_t pos, retdec::utils::Endianness endianness=retdec::utils::Endianness::UNKNOWN)
 
void writeRepeatingByte (uint8_t byte, uint32_t pos, uint32_t repeatAmount)
 

Private Member Functions

template<typename T >
void writeImpl (const T &data, uint32_t pos, retdec::utils::Endianness endianness)
 
template<typename T >
readImpl (uint32_t pos, retdec::utils::Endianness endianness) const
 

Private Attributes

std::vector< uint8_t > _data
 
retdec::utils::Endianness _endianness
 
uint32_t _capacity
 

Detailed Description

The class for dynamic buffered data manipulation taking the endianness of the data in account.

This class provides the wrapper around the vector of bytes. It allows to specify the endianness of the data. The data can be read from or written to this buffer using templated methods allowing not only per-byte manipulation, but also reading and writing of words, double words etc. The buffer has its initial capacity allowing it to grow into specified size. If the read or write requests the data from position that still falls into the capacity, the buffer resizes itself to this size dynamically. It also checks for out-of-bounds accesses. In case of reading it reads the bytes that would be out-of-bounds as 0 bytes and for writing it simply ignores the data that would be out-of-bounds.

Constructor & Destructor Documentation

◆ DynamicBuffer() [1/5]

retdec::utils::DynamicBuffer::DynamicBuffer ( retdec::utils::Endianness  endianness = retdec::utils::Endianness::LITTLE)

Creates the empty DynamicBuffer object with no capacity and specified endianness.

Parameters
endiannessEndianness of the bytes in the buffer.

◆ DynamicBuffer() [2/5]

retdec::utils::DynamicBuffer::DynamicBuffer ( uint32_t  capacity,
retdec::utils::Endianness  endianness = retdec::utils::Endianness::LITTLE 
)

Creates the DynamicBuffer object with specified capacity and endianness.

Parameters
capacityCapacity of the buffer.
endiannessEndianness of the bytes in the buffer.

◆ DynamicBuffer() [3/5]

retdec::utils::DynamicBuffer::DynamicBuffer ( const std::vector< uint8_t > &  data,
retdec::utils::Endianness  endianness = retdec::utils::Endianness::LITTLE 
)

Creates the DynamicBuffer object and fills it with specified data with specified endianness.

Parameters
dataThe bytes to initialize the buffer with.
endiannessEndiannes of the bytes in the buffer.

◆ DynamicBuffer() [4/5]

retdec::utils::DynamicBuffer::DynamicBuffer ( const DynamicBuffer dynamicBuffer)

Creates the copy of the DynamicBuffer object.

Parameters
dynamicBufferBuffer to copy.

◆ DynamicBuffer() [5/5]

retdec::utils::DynamicBuffer::DynamicBuffer ( const DynamicBuffer dynamicBuffer,
uint32_t  startPos,
uint32_t  amount 
)

Creates the copy of the DynamicBuffer object, but only the specified subbuffer.

Parameters
dynamicBufferBuffer to copy.
startPosStarting position in the specified buffer where to start the copying.
amountNumber of bytes from startPos to copy.

Member Function Documentation

◆ erase()

void retdec::utils::DynamicBuffer::erase ( uint32_t  startPos,
uint32_t  amount 
)

Erases the bytes from the buffer. Also reduces the capacity of the buffer.

Parameters
startPosThe starting position where to start erasing.
amountNumber of bytes from the startPos including to erase.

◆ forEach()

void retdec::utils::DynamicBuffer::forEach ( const std::function< void(uint8_t &)> &  func)

Runs the specified function for every single byte in the DynamicBuffer.

Parameters
funcFunction to run for every byte.

◆ forEachReverse()

void retdec::utils::DynamicBuffer::forEachReverse ( const std::function< void(uint8_t &)> &  func)

Runs the specified function for every single byte in the DynamicBuffer in the reverse order.

Parameters
funcFunction to run for every byte.

◆ getBuffer()

std::vector< uint8_t > retdec::utils::DynamicBuffer::getBuffer ( ) const

Gets the buffer as the vector of bytes.

Returns
The vector with the bytes.

◆ getCapacity()

uint32_t retdec::utils::DynamicBuffer::getCapacity ( ) const

Gets the actual capacity of the buffer.

Returns
The capacity of the buffer.

◆ getEndianness()

Endianness retdec::utils::DynamicBuffer::getEndianness ( ) const

Gets the current endianness of the buffer.

Returns
The endianness of the bytes in the buffer.

◆ getRawBuffer()

const uint8_t * retdec::utils::DynamicBuffer::getRawBuffer ( ) const

Gets the raw pointer to the bytes in the buffer.

Returns
The pointer to the bytes in the buffer.

◆ getRealDataSize()

uint32_t retdec::utils::DynamicBuffer::getRealDataSize ( ) const

Gets the size of the data that are actually written to the buffer. This cannot be greater than the capacity of the buffer.

Returns
The size of the written data to the buffer.

◆ operator=()

DynamicBuffer & retdec::utils::DynamicBuffer::operator= ( DynamicBuffer  rhs)

Assign operator, creates the copy of the DynamicBuffer.

Parameters
rhsRight hand side of the operator.
Returns
The new DynamicBuffer object.

◆ read()

template<typename T >
T retdec::utils::DynamicBuffer::read ( uint32_t  pos,
retdec::utils::Endianness  endianness = retdec::utils::Endianness::UNKNOWN 
) const
inline

Reads the data from the buffer. If the reading position is beyond the size of the real data, the real data are resized so this value can be read filling the new bytes with default (0) value. If the read overlaps the capacity of the buffer, only the bytes that still fall into the capacity are read and the rest is filled with default (0) value.

Template Parameters
Thetype of the data to read. This must be integral type.
Parameters
posPosition where to start the reading.
endiannessThe endianness in which the data should be read. If not specified, default endianness assigned to DynamicBuffer is used.
Returns
The read value from the buffer.

◆ readImpl()

template<typename T >
T retdec::utils::DynamicBuffer::readImpl ( uint32_t  pos,
retdec::utils::Endianness  endianness 
) const
inlineprivate

◆ readString()

std::string retdec::utils::DynamicBuffer::readString ( uint32_t  pos,
uint32_t  maxLength = 0 
) const

Reads the null or length terminated string from the buffer.

Parameters
posThe poisition in the buffer where to start reading.
maxLengthThe maximal length of the string that is read. If this is 0, the length limit is ignored and the string is read up to the next 0 byte.
Returns
String read from buffer.

◆ setCapacity()

void retdec::utils::DynamicBuffer::setCapacity ( uint32_t  capacity)

Sets the capacity of the buffer.

Parameters
capacityThe new capacity to set to the buffer.

◆ setEndianness()

void retdec::utils::DynamicBuffer::setEndianness ( retdec::utils::Endianness  endianness)

Sets the endianness of the bytes in the buffer. It doesn't result in any changes to the actual bytes in the buffer. It reflects only when reading from or writing to the buffer.

Parameters
endiannessThe endianness to set.

◆ write()

template<typename T >
void retdec::utils::DynamicBuffer::write ( const T &  data,
uint32_t  pos,
retdec::utils::Endianness  endianness = retdec::utils::Endianness::UNKNOWN 
)
inline

Writes the data to the buffer. If the writing poisition is beyond the size of the real data, the real data are resized so this value can be written filling the new bytes with default (0) value. If the write overlaps the capacity of the buffer, only the bytes that still fall into the capacity are written and the rest is ignored.

Template Parameters
Thetype of the data to write. This must be integral type.
Parameters
dataThe data to write.
posThe position where to start writing.
endiannessThe endianness in which the data should be written. If not specified, default endianness assigned to DynamicBuffer is used.

◆ writeImpl()

template<typename T >
void retdec::utils::DynamicBuffer::writeImpl ( const T &  data,
uint32_t  pos,
retdec::utils::Endianness  endianness 
)
inlineprivate

◆ writeRepeatingByte()

void retdec::utils::DynamicBuffer::writeRepeatingByte ( uint8_t  byte,
uint32_t  pos,
uint32_t  repeatAmount 
)

Writes the single byte into the buffer for repeating amount of times.

Parameters
byteThe byte to write into the buffer.
posThe position where to start writing the byte.
repeatAmountThe number of times the byte is written into the buffer starting from pos including.

Member Data Documentation

◆ _capacity

uint32_t retdec::utils::DynamicBuffer::_capacity
private

◆ _data

std::vector<uint8_t> retdec::utils::DynamicBuffer::_data
mutableprivate

◆ _endianness

retdec::utils::Endianness retdec::utils::DynamicBuffer::_endianness
private

The documentation for this class was generated from the following files: