YAPET::BDBuffer Class Reference

A wrapper-class for allocating and securely deallocating memory. More...

#include <bdbuffer.h>

List of all members.

Public Member Functions

const uint8_t * at (uint32_t pos) const throw (std::out_of_range)
 Access a location inside the memory chunk.
uint8_t * at (uint32_t pos) throw (std::out_of_range)
 Access a location inside the memory chunk.
 BDBuffer (const BDBuffer &ed) throw (YAPETException)
 BDBuffer ()
 BDBuffer (uint32_t is) throw (YAPETException)
 Initializes the object with a given size of memory.
 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.
uint8_t * operator() ()
 Returns the pointer to the memory chunk.
const uint8_t * operator() () const
 Returns the pointer to the memory chunk.
const BDBufferoperator= (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


Detailed Description

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 60 of file bdbuffer.h.


Constructor & Destructor Documentation

BDBuffer::BDBuffer ( uint32_t  is  )  throw (YAPETException)

Initializes the object by allocating is bytes.

Parameters:
is number of bytes to be allocated.

Definition at line 62 of file bdbuffer.cc.

References _size, alloc_mem(), and data.

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 72 of file bdbuffer.cc.

BDBuffer::BDBuffer ( const BDBuffer ed  )  throw (YAPETException)

Definition at line 74 of file bdbuffer.cc.

References _size, alloc_mem(), and data.

BDBuffer::~BDBuffer (  ) 

Deallocates the memory and zero'es it out.

See also:
BDBuffer::free_mem()

Definition at line 91 of file bdbuffer.cc.

References _size, data, and free_mem().


Member Function Documentation

uint8_t * BDBuffer::alloc_mem ( uint32_t  s  )  throw (YAPETException) [protected]

Allocates s bytes of memory on the heap.

Parameters:
s size of the memory chunk to be allocated in bytes
Returns:
the pointer to the start of the allocated memory.
Exceptions:
YAPETException if the memory could not be allocated.

Definition at line 34 of file bdbuffer.cc.

Referenced by BDBuffer(), operator=(), and resize().

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.

Parameters:
pos the index of the byte to retrieve.
Returns:
the pointer to the byte at position specified.
Exceptions:
std::out_of_range exception if pos is not a valid index.

Definition at line 169 of file bdbuffer.cc.

References _size, and data.

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.

Parameters:
pos the index of the byte to retrieve.
Returns:
the pointer to the byte at position specified.
Exceptions:
std::out_of_range exception if pos is not a valid index.

Definition at line 147 of file bdbuffer.cc.

References _size, and data.

Referenced by YAPET::Crypt::decrypt(), and YAPET::Crypt::encrypt().

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.

Parameters:
d pointer to the memory to be free'd
s size of the memory chunk. Needed to clear out the memory.

Definition at line 52 of file bdbuffer.cc.

Referenced by operator=(), resize(), and ~BDBuffer().

YAPET::BDBuffer::operator const uint8_t * (  )  const [inline]

Definition at line 115 of file bdbuffer.h.

References data.

YAPET::BDBuffer::operator const void * (  )  [inline]

Definition at line 118 of file bdbuffer.h.

References data.

YAPET::BDBuffer::operator uint8_t * (  )  [inline]

Definition at line 113 of file bdbuffer.h.

References data.

uint8_t* YAPET::BDBuffer::operator() (  )  [inline]

Definition at line 110 of file bdbuffer.h.

References data.

const uint8_t* YAPET::BDBuffer::operator() (  )  const [inline]

Definition at line 108 of file bdbuffer.h.

References data.

const BDBuffer & BDBuffer::operator= ( const BDBuffer ed  ) 

Definition at line 177 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.

Parameters:
ns the new size of the memory chunk serving as buffer

Definition at line 111 of file bdbuffer.cc.

References _size, alloc_mem(), data, and free_mem().

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.

Returns:
the size of the allocated memory in bytes.

Definition at line 100 of file bdbuffer.h.

References _size.

Referenced by YAPET::Crypt::decrypt(), and YAPET::Crypt::encrypt().


Member Data Documentation

uint32_t YAPET::BDBuffer::_size [private]

Holds the size of the allocated memory chunk in bytes.

Definition at line 67 of file bdbuffer.h.

Referenced by at(), BDBuffer(), operator=(), resize(), size(), and ~BDBuffer().

uint8_t* YAPET::BDBuffer::data [private]

Holds the pointer to the allocated memory.

Definition at line 74 of file bdbuffer.h.

Referenced by at(), BDBuffer(), operator const uint8_t *(), operator const void *(), operator uint8_t *(), operator()(), operator=(), resize(), and ~BDBuffer().


The documentation for this class was generated from the following files:
Generated on Wed Feb 27 16:15:42 2008 for YAPET by  doxygen 1.5.4