retdec
|
The class for dynamic buffered data manipulation taking the endianness of the data in account. More...
#include <dynamic_buffer.h>
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) | |
DynamicBuffer & | operator= (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 > | |
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 > | |
T | readImpl (uint32_t pos, retdec::utils::Endianness endianness) const |
Private Attributes | |
std::vector< uint8_t > | _data |
retdec::utils::Endianness | _endianness |
uint32_t | _capacity |
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.
retdec::utils::DynamicBuffer::DynamicBuffer | ( | retdec::utils::Endianness | endianness = retdec::utils::Endianness::LITTLE | ) |
Creates the empty DynamicBuffer object with no capacity and specified endianness.
endianness | Endianness of the bytes in the buffer. |
retdec::utils::DynamicBuffer::DynamicBuffer | ( | uint32_t | capacity, |
retdec::utils::Endianness | endianness = retdec::utils::Endianness::LITTLE |
||
) |
Creates the DynamicBuffer object with specified capacity and endianness.
capacity | Capacity of the buffer. |
endianness | Endianness of the bytes in the buffer. |
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.
data | The bytes to initialize the buffer with. |
endianness | Endiannes of the bytes in the buffer. |
retdec::utils::DynamicBuffer::DynamicBuffer | ( | const DynamicBuffer & | dynamicBuffer | ) |
Creates the copy of the DynamicBuffer object.
dynamicBuffer | Buffer to copy. |
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.
dynamicBuffer | Buffer to copy. |
startPos | Starting position in the specified buffer where to start the copying. |
amount | Number of bytes from startPos to copy. |
void retdec::utils::DynamicBuffer::erase | ( | uint32_t | startPos, |
uint32_t | amount | ||
) |
Erases the bytes from the buffer. Also reduces the capacity of the buffer.
startPos | The starting position where to start erasing. |
amount | Number of bytes from the startPos including to erase. |
void retdec::utils::DynamicBuffer::forEach | ( | const std::function< void(uint8_t &)> & | func | ) |
Runs the specified function for every single byte in the DynamicBuffer.
func | Function to run for every byte. |
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.
func | Function to run for every byte. |
std::vector< uint8_t > retdec::utils::DynamicBuffer::getBuffer | ( | ) | const |
Gets the buffer as the vector of bytes.
uint32_t retdec::utils::DynamicBuffer::getCapacity | ( | ) | const |
Gets the actual capacity of the buffer.
Endianness retdec::utils::DynamicBuffer::getEndianness | ( | ) | const |
Gets the current endianness of the buffer.
const uint8_t * retdec::utils::DynamicBuffer::getRawBuffer | ( | ) | const |
Gets the raw pointer to the bytes in the buffer.
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.
DynamicBuffer & retdec::utils::DynamicBuffer::operator= | ( | DynamicBuffer | rhs | ) |
Assign operator, creates the copy of the DynamicBuffer.
rhs | Right hand side of the operator. |
|
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.
The | type of the data to read. This must be integral type. |
pos | Position where to start the reading. |
endianness | The endianness in which the data should be read. If not specified, default endianness assigned to DynamicBuffer is used. |
|
inlineprivate |
std::string retdec::utils::DynamicBuffer::readString | ( | uint32_t | pos, |
uint32_t | maxLength = 0 |
||
) | const |
Reads the null or length terminated string from the buffer.
pos | The poisition in the buffer where to start reading. |
maxLength | The 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. |
void retdec::utils::DynamicBuffer::setCapacity | ( | uint32_t | capacity | ) |
Sets the capacity of the buffer.
capacity | The new capacity to set to the buffer. |
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.
endianness | The endianness to set. |
|
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.
The | type of the data to write. This must be integral type. |
data | The data to write. |
pos | The position where to start writing. |
endianness | The endianness in which the data should be written. If not specified, default endianness assigned to DynamicBuffer is used. |
|
inlineprivate |
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.
byte | The byte to write into the buffer. |
pos | The position where to start writing the byte. |
repeatAmount | The number of times the byte is written into the buffer starting from pos including. |
|
private |
|
mutableprivate |
|
private |