1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
// -*- c++ -*-
//
// $Id: bdbuffer.h 2719 2009-08-20 01:30:25Z rafi $
//
// Copyright (C) 2008, 2009 Rafael Ostertag
//
// This file is part of YAPET.
//
// YAPET is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// YAPET. If not, see <http://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with the OpenSSL project's OpenSSL library (or a modified version of that
// library), containing parts covered by the terms of the OpenSSL or SSLeay
// licenses, Rafael Ostertag grants you additional permission to convey the
// resulting work. Corresponding Source for a non-source form of such a
// combination shall include the source code for the parts of OpenSSL used as
// well as that of the covered work.
//
#ifndef _BDBUFFER_H
#define _BDBUFFER_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#ifdef HAVE_STDLIB_h
# include <stdlib.h>
#endif
#ifdef HAVE_STDEXCEPT
# include <stdexcept>
#endif
#include "yapetexception.h"
namespace YAPET {
/**
* @brief A wrapper-class for allocating and securely deallocating
* 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 \c uint8_t pointer.
*/
class BDBuffer {
private:
/**
* @brief Size of allocated memory chunk
*
* Holds the size of the allocated memory chunk in bytes.
*/
uint32_t _size;
/**
* @brief pointer to the allocated memory
*
* Holds the pointer to the allocated memory.
*/
uint8_t* data;
protected:
//! Allocates memory of a given size
uint8_t* alloc_mem (uint32_t s) throw (YAPETException);
//! Clears and frees memory
void free_mem (uint8_t* d, uint32_t s);
public:
//! Initializes the object with a given size of memory
BDBuffer (uint32_t is) throw (YAPETException);
BDBuffer();
BDBuffer (const BDBuffer& ed) throw (YAPETException);
//! Destructor
~BDBuffer();
//! Resize the memory to a given size
void resize (uint32_t ns) throw (YAPETException);
/**
* @brief Get the size of the buffer
*
* Returns the size of the allocated memory chunk used as
* buffer.
*
* @return the size of the allocated memory in bytes.
*/
uint32_t size() const {
return _size;
}
//! 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
const uint8_t* at (uint32_t pos) const throw (std::out_of_range);
//! Returns the pointer to the memory chunk
const uint8_t* operator() () const {
return data;
}
//! Returns the pointer to the memory chunk
uint8_t* operator() () {
return data;
}
//! Returns the pointer to the memory chunk
operator uint8_t*() {
return data;
}
//! Returns the pointer to the memory chunk
operator const uint8_t*() const {
return data;
}
//! Returns the pointer to the memory chunk
operator const void*() {
return (void*) data;
}
const BDBuffer& operator= (const BDBuffer& ed);
};
}
#endif // _BDBUFFER_H
|