• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

crypt/bdbuffer.cc

Go to the documentation of this file.
00001 // $Id: bdbuffer.cc 3342 2010-09-17 18:32:00Z java $
00002 //
00003 // Copyright (C) 2008-2010  Rafael Ostertag
00004 //
00005 // This file is part of YAPET.
00006 //
00007 // YAPET is free software: you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the Free Software
00009 // Foundation, either version 3 of the License, or (at your option) any later
00010 // version.
00011 //
00012 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015 // details.
00016 //
00017 // You should have received a copy of the GNU General Public License along with
00018 // YAPET.  If not, see <http://www.gnu.org/licenses/>.
00019 //
00020 // Additional permission under GNU GPL version 3 section 7
00021 //
00022 // If you modify this program, or any covered work, by linking or combining it
00023 // with the OpenSSL project's OpenSSL library (or a modified version of that
00024 // library), containing parts covered by the terms of the OpenSSL or SSLeay
00025 // licenses, Rafael Ostertag grants you additional permission to convey the
00026 // resulting work.  Corresponding Source for a non-source form of such a
00027 // combination shall include the source code for the parts of OpenSSL used as
00028 // well as that of the covered work.
00029 //
00030 
00031 #include "bdbuffer.h"
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 "../intl.h"
00042 
00043 using namespace YAPET;
00044 
00054 uint8_t*
00055 BDBuffer::alloc_mem (uint32_t s) throw (YAPETException) {
00056     uint8_t* tmp = (uint8_t*) malloc (s);
00057 
00058     if (tmp == NULL)
00059         throw YAPETException (_ ("Memory exhausted") );
00060 
00061     return tmp;
00062 }
00063 
00073 void
00074 BDBuffer::free_mem (uint8_t* d, uint32_t s) {
00075     memset (d, 0, s);
00076     free (d);
00077 }
00078 
00084 BDBuffer::BDBuffer (uint32_t is) throw (YAPETException) : _size (is) {
00085     data = alloc_mem (_size);
00086 }
00087 
00094 BDBuffer::BDBuffer() : _size (0), data (NULL) { }
00095 
00096 BDBuffer::BDBuffer (const BDBuffer& ed) throw (YAPETException) {
00097     if (ed.data == NULL) {
00098         data = NULL;
00099         _size = 0;
00100         return;
00101     }
00102 
00103     data = alloc_mem (ed._size);
00104     memcpy (data, ed.data, ed._size);
00105     _size = ed._size;
00106 }
00107 
00113 BDBuffer::~BDBuffer() {
00114     if (data == NULL) return;
00115 
00116     free_mem (data, _size);
00117 }
00118 
00133 void
00134 BDBuffer::resize (uint32_t ns) throw (YAPETException) {
00135     if (data == NULL) {
00136         data = alloc_mem (ns);
00137         _size = ns;
00138         return;
00139     }
00140 
00141     uint8_t* newbuf = alloc_mem (ns);
00142 
00143     if (ns > _size)
00144         memcpy (newbuf, data, _size);
00145     else
00146         memcpy (newbuf, data, ns);
00147 
00148     free_mem (data, _size);
00149     _size = ns;
00150     data = newbuf;
00151 }
00152 
00168 uint8_t*
00169 BDBuffer::at (uint32_t pos) throw (std::out_of_range) {
00170     if (pos > (_size - 1) )
00171         throw std::out_of_range (_ ("Position out of range") );
00172 
00173     return data + pos;
00174 }
00175 
00190 const uint8_t*
00191 BDBuffer::at (uint32_t pos) const throw (std::out_of_range) {
00192     if (pos > (_size - 1) )
00193         throw std::out_of_range (_ ("Position out of range") );
00194 
00195     return data + pos;
00196 }
00197 
00198 const BDBuffer&
00199 BDBuffer::operator= (const BDBuffer & ed) {
00200     if (this == &ed) return *this;
00201 
00202     if (data != NULL)
00203         free_mem (data, _size);
00204 
00205     if (ed.data != NULL) {
00206         data = alloc_mem (ed._size);
00207         memcpy (data, ed.data, ed._size);
00208     } else {
00209         data = NULL;
00210     }
00211 
00212     _size = ed._size;
00213     return *this;
00214 }

Generated on Sun Sep 19 2010 15:37:13 for YAPET by  doxygen 1.7.1