crypt/record.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //
00003 // $Id: record.h,v 1.6 2008-02-13 00:09:10 rafi Exp $
00004 //
00005 // YAPET -- Yet Another Password Encryption Tool
00006 // Copyright (C) 2008  Rafael Ostertag
00007 //
00008 // This program is free software: you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation, either version 3 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 //
00021 
00022 #ifndef _RECORD_H
00023 #define _RECORD_H
00024 
00025 #ifdef HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028 
00029 #ifdef HAVE_INTTYPES_H
00030 # include <inttypes.h>
00031 #endif
00032 
00033 #ifdef HAVE_STDLIB_H
00034 # include <stdlib.h>
00035 #endif
00036 
00037 #ifdef HAVE_STRING_H
00038 # include <string.h>
00039 #endif
00040 
00041 #include "bdbuffer.h"
00042 #include "yapetexception.h"
00043 
00044 namespace YAPET {
00045 
00057     template<class T>
00058     class Record {
00059     private:
00066         uint32_t _size;
00072         T* data;
00073 
00080         void alloc_mem() throw(YAPETException) {
00081         data = (T*) malloc(sizeof(T));
00082         if (data == NULL)
00083             throw YAPETException("Memory exhausted");
00084 
00085         _size = sizeof(T);
00086         }
00087 
00093         void free_mem() {
00094         memset(data, 0, _size);
00095         free(data);
00096         }
00097 
00098     public:
00108         Record<T>(const T& d) throw(YAPETException) {
00109         alloc_mem();
00110         memcpy(data, &d, sizeof(T));
00111         }
00112 
00119         Record<T>() throw (YAPETException){
00120         alloc_mem();
00121         }
00122 
00123         Record<T>(const Record<T>& r) throw (YAPETException) {
00124         alloc_mem();
00125         memcpy(data, r.data, _size);
00126         }
00127 
00128         virtual ~Record<T>() {
00129         free_mem();
00130         }
00131 
00137         uint32_t size() const { return _size; }
00138 
00146         T* getData() { return data; }
00154         const T* getData() const { return data; }
00155 
00163         operator T*() { return data; }
00171         operator const T*() const { return data; }
00180         operator uint8_t*() { return (uint8_t*)data; }
00189         operator const uint8_t*() const { return (const uint8_t*)data; }
00190 
00200         const Record<T>& operator=(const Record<T>& r)
00201         throw(YAPETException) {
00202         if (this == &r) return *this;
00203 
00204         free_mem();
00205 
00206         // This sets _size member too
00207         alloc_mem();
00208         memcpy(data, r.data, r._size);
00209 
00210         return *this;
00211         }
00212 
00222         const Record<T>& operator=(const T& r) throw(YAPETException) {
00223         free_mem();
00224         // This sets _size member too
00225         alloc_mem();
00226         memcpy(data, &r, _size);
00227 
00228         return *this;
00229         }
00230 
00240         const Record<T>& operator=(const T* r) throw(YAPETException){
00241         free_mem();
00242         // This sets _size member too
00243         alloc_mem();
00244         memcpy(data, r, _size);
00245 
00246         return *this;
00247         }
00248 
00262         const Record<T>& operator=(const BDBuffer& bdb)
00263         throw(YAPETException) {
00264         if (bdb.size() < _size)
00265             throw YAPETException("BDBuffer too small");
00266 
00267         free_mem();
00268         // This sets _size member too
00269         alloc_mem();
00270 
00271         memcpy(data, bdb(), _size);
00272 
00273         return *this;
00274         }
00275     };
00276 }
00277 
00278 #endif // _RECORD_H

Generated on Wed Feb 27 16:15:41 2008 for YAPET by  doxygen 1.5.4