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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
// -*- c++ -*-
//
// $Id: record.h 2826 2009-08-31 17:49:57Z 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 _RECORD_H
#define _RECORD_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_STRING_H
# include <string.h>
#endif
#include "../intl.h"
#include "bdbuffer.h"
#include "yapetexception.h"
namespace YAPET {
/**
* @brief Template for allocating/deallocating memory for structs
*
* The primary intend of this template is to make sure the memory allocated
* for a struct is zero'ed out upon deallocation.
*
* The template allocates enough memory on the heap for holding
* the struct of type \c T.
*
* It allows direct manipulation of the struct on the heap.
*/
template<class T>
class Record {
private:
/**
* @brief The size of the memory allocated.
*
* The size of the memory allocated. Used to zero out the
* memory used by the struct.
*/
uint32_t _size;
/**
* @brief Pointer to the struct.
*
* Pointer to the struct on the heap.
*/
T* data;
/**
* @brief Allocate memory for the struct.
*
* Allocates the proper amount of memory for holding the
* struct and sets the \c _size field.
*/
void alloc_mem() throw (YAPETException) {
data = (T*) malloc (sizeof (T) );
if (data == NULL)
throw YAPETException (_ ("Memory exhausted") );
_size = sizeof (T);
}
/**
* @brief Zero out and free memory.
*
* Zero'es the memory out and frees it.
*/
void free_mem() {
memset (data, 0, _size);
free (data);
}
public:
/**
* @brief Allocates memory.
*
* Allocates memory of the proper size and copies the
* content of the given struct \c d.
*
* @param d reference to the struct from where the content
* is copied to the allocated memory.
*/
Record<T> (const T& d) throw (YAPETException) {
alloc_mem();
memcpy (data, &d, sizeof (T) );
}
/**
* @brief Allocates memory to hold a struct of the type \c
* T.
*
* Allocates memory to hold a struct of the type \c T.
*/
Record<T>() throw (YAPETException) {
alloc_mem();
}
Record<T> (const Record<T>& r) throw (YAPETException) {
alloc_mem();
memcpy (data, r.data, _size);
}
virtual ~Record<T>() {
free_mem();
}
/**
* @brief Get the size of the allocated memory.
*
* Get the size of the allocated memory.
*/
uint32_t size() const {
return _size;
}
/**
* @brief Get the pointer to the struct.
*
* Gets the pointer to the struct on the heap.
*
* @return pointer to the struct on the heap.
*/
T* getData() {
return data;
}
/**
* @brief Get the pointer to the struct.
*
* Gets the pointer to the struct on the heap.
*
* @return pointer to the struct on the heap.
*/
const T* getData() const {
return data;
}
/**
* @brief Cast to the pointer of the struct.
*
* Cast to the pointer of the struct on the heap.
*
* @return pointer to the struct on the heap.
*/
operator T*() {
return data;
}
/**
* @brief Cast to the pointer of the struct.
*
* Cast to the pointer of the struct on the heap.
*
* @return pointer to the struct on the heap.
*/
operator const T*() const {
return data;
}
/**
* @brief Cast operator.
*
* Cast operator used by the openssl functions.
*
* @return pointer to the struct casted to an unsigned 8 bits
* integer pointer.
*/
operator uint8_t*() {
return (uint8_t*) data;
}
/**
* @brief Cast operator.
*
* Cast operator used by the openssl functions.
*
* @return pointer to the struct casted to an unsigned 8 bits
* integer pointer.
*/
operator const uint8_t*() const {
return (const uint8_t*) data;
}
/**
* @brief Assignment operator.
*
* Assigns another \c Record to \c this.
*
* @param r reference to a \c Record.
*
* @return const reference to \c this.
*/
const Record<T>& operator= (const Record<T>& r)
throw (YAPETException) {
if (this == &r) return *this;
free_mem();
// This sets _size member too
alloc_mem();
memcpy (data, r.data, r._size);
return *this;
}
/**
* @brief Assignment operator.
*
* Assigns a struct of type \c T to \c this.
*
* @param r reference to a struct of type \c T.
*
* @return const reference to \c this.
*/
const Record<T>& operator= (const T& r) throw (YAPETException) {
free_mem();
// This sets _size member too
alloc_mem();
memcpy (data, &r, _size);
return *this;
}
/**
* @brief Assignment operator.
*
* Assigns a struct of type \c T to \c this.
*
* @param r pointer to a struct of type \c T.
*
* @return const reference to \c this.
*/
const Record<T>& operator= (const T* r) throw (YAPETException) {
free_mem();
// This sets _size member too
alloc_mem();
memcpy (data, r, _size);
return *this;
}
/**
* @brief Assignment operator.
*
* Assigns a \c BDBuffer. If the size of the \c BDBuffer is smaller
* than the size of the struct, an exception is thrown.
*
* Only as much bytes as fit into struct are copied from the \c
* BDBuffer.
*
* @param bdb reference to a \c BDBuffer.
*
* @return const reference to \c this.
*/
const Record<T>& operator= (const BDBuffer& bdb)
throw (YAPETException) {
// As of version 0.6, the exnum is used to determine whether to use a 32 or 64 bit header
if (bdb.size() < _size)
throw YAPETException (_ ("BDBuffer too small. Cannot assign to Record<T>"), BDBUFFER_TOO_SMALL );
if (bdb.size() > _size)
throw YAPETException (_ ("BDBuffer too big. Cannot assign to Record<T>"), BDBUFFER_TOO_BIG );
free_mem();
// This sets _size member too
alloc_mem();
memcpy (data, bdb(), _size);
return *this;
}
};
}
#endif // _RECORD_H
|