A wrapper-class for allocating and securely deallocating memory. More...
#include <bdbuffer.h>
Public Member Functions | |
| uint8_t * | at (uint32_t pos) throw (std::out_of_range) |
| Access a location inside the memory chunk. | |
| const uint8_t * | at (uint32_t pos) const throw (std::out_of_range) |
| Access a location inside the memory chunk. | |
| BDBuffer () | |
| BDBuffer (uint32_t is) throw (YAPETException) | |
| Initializes the object with a given size of memory. | |
| BDBuffer (const BDBuffer &ed) throw (YAPETException) | |
| operator const uint8_t * () const | |
| Returns the pointer to the memory chunk. | |
| operator const void * () | |
| Returns the pointer to the memory chunk. | |
| operator uint8_t * () | |
| Returns the pointer to the memory chunk. | |
| const uint8_t * | operator() () const |
| Returns the pointer to the memory chunk. | |
| uint8_t * | operator() () |
| Returns the pointer to the memory chunk. | |
| const BDBuffer & | operator= (const BDBuffer &ed) |
| void | resize (uint32_t ns) throw (YAPETException) |
| Resize the memory to a given size. | |
| uint32_t | size () const |
| Get the size of the buffer. | |
| ~BDBuffer () | |
| Destructor. | |
Protected Member Functions | |
| uint8_t * | alloc_mem (uint32_t s) throw (YAPETException) |
| Allocates memory of a given size. | |
| void | free_mem (uint8_t *d, uint32_t s) |
| Clears and frees memory. | |
Private Attributes | |
| uint32_t | _size |
| Size of allocated memory chunk. | |
| uint8_t * | data |
| pointer to the allocated memory | |
The BDBuffer class is a wrapper-class for allocating and deallocating memory for data of unsigned 8bit integers.
Its primary intend is to make sure the memory is cleared after deallocation. It does so by zero'ing out the entire buffer upon destruction of the object.
The class provides some basic methods for accessing the memory.
The pointer to the allocated memory can be obtained by casting to an uint8_t pointer.
Definition at line 71 of file bdbuffer.h.
| BDBuffer::BDBuffer | ( | uint32_t | is | ) | throw (YAPETException) |
Initializes the object by allocating is bytes.
| is | number of bytes to be allocated. |
Definition at line 84 of file bdbuffer.cc.
| BDBuffer::BDBuffer | ( | ) |
Initializes the object, but does not allocate memory.
If the object is created using this constructor, functions returning pointer to the buffer will return NULL
Definition at line 94 of file bdbuffer.cc.
| BDBuffer::BDBuffer | ( | const BDBuffer & | ed | ) | throw (YAPETException) |
Definition at line 96 of file bdbuffer.cc.
| BDBuffer::~BDBuffer | ( | ) |
Deallocates the memory and zero'es it out.
Definition at line 113 of file bdbuffer.cc.
References _size, data, and free_mem().

| uint8_t * BDBuffer::alloc_mem | ( | uint32_t | s | ) | throw (YAPETException) [protected] |
Allocates s bytes of memory on the heap.
| s | size of the memory chunk to be allocated in bytes |
| YAPETException | if the memory could not be allocated. |
Definition at line 55 of file bdbuffer.cc.
Referenced by operator=().

| uint8_t * BDBuffer::at | ( | uint32_t | pos | ) | throw (std::out_of_range) |
Returns a pointer to the n-th byte of the buffer, where n=pos. pos is zero-based, the first byte of the buffer is at position 0.
It checks pos for a valid value. In case pos overruns the buffer, a std::out_of_range exception is thrown.
| pos | the index of the byte to retrieve. |
| std::out_of_range | exception if pos is not a valid index. |
Definition at line 169 of file bdbuffer.cc.
Referenced by YAPET::Crypt::decrypt(), and YAPET::Crypt::encrypt().

| const uint8_t * BDBuffer::at | ( | uint32_t | pos | ) | const throw (std::out_of_range) |
Returns a pointer to the n-th byte of the buffer, where n=pos. pos is zero-based, the first byte of the buffer is at position 0.
It checks pos for a valid value. In case pos overruns the buffer, a std::out_of_range exception is thrown.
| pos | the index of the byte to retrieve. |
| std::out_of_range | exception if pos is not a valid index. |
Definition at line 191 of file bdbuffer.cc.
| void BDBuffer::free_mem | ( | uint8_t * | d, | |
| uint32_t | s | |||
| ) | [protected] |
Frees the memory associated with the pointer provided. Before the memory is deallocated, s bytes of the memory region starting from the pointer d will be zero'ed out.
| d | pointer to the memory to be free'd | |
| s | size of the memory chunk. Needed to clear out the memory. |
Definition at line 74 of file bdbuffer.cc.
Referenced by operator=(), and ~BDBuffer().

| YAPET::BDBuffer::operator const uint8_t * | ( | ) | const [inline] |
Definition at line 134 of file bdbuffer.h.
References data.
| YAPET::BDBuffer::operator const void * | ( | ) | [inline] |
Definition at line 139 of file bdbuffer.h.
References data.
| YAPET::BDBuffer::operator uint8_t * | ( | ) | [inline] |
Definition at line 130 of file bdbuffer.h.
References data.
| uint8_t* YAPET::BDBuffer::operator() | ( | ) | [inline] |
Definition at line 125 of file bdbuffer.h.
References data.
| const uint8_t* YAPET::BDBuffer::operator() | ( | ) | const [inline] |
Definition at line 121 of file bdbuffer.h.
References data.
Definition at line 199 of file bdbuffer.cc.
References _size, alloc_mem(), data, and free_mem().

| void BDBuffer::resize | ( | uint32_t | ns | ) | throw (YAPETException) |
Resizes the buffer to the given size ns. It does not utilize realloc(), so you can be sure the pointer to the resized buffer will change.
The content of the old buffer memory region will be copied over to the new location. If the newly allocated memory is smaller than the former buffer size, the content of the old buffer will be truncated to fill the entire space of the new buffer. If the new size is larger than the old size, the entire old buffer is preserved while copying.
| ns | the new size of the memory chunk serving as buffer |
Definition at line 134 of file bdbuffer.cc.
Referenced by YAPET::Crypt::decrypt(), and YAPET::Crypt::encrypt().

| uint32_t YAPET::BDBuffer::size | ( | ) | const [inline] |
Returns the size of the allocated memory chunk used as buffer.
Definition at line 111 of file bdbuffer.h.
References _size.
Referenced by YAPET::Crypt::decrypt(), and YAPET::Crypt::encrypt().

uint32_t YAPET::BDBuffer::_size [private] |
Holds the size of the allocated memory chunk in bytes.
Definition at line 78 of file bdbuffer.h.
Referenced by operator=(), size(), and ~BDBuffer().
uint8_t* YAPET::BDBuffer::data [private] |
Holds the pointer to the allocated memory.
Definition at line 85 of file bdbuffer.h.
Referenced by operator const uint8_t *(), operator const void *(), operator uint8_t *(), operator()(), operator=(), and ~BDBuffer().
1.7.1