crypt/bdbuffer.cc

Go to the documentation of this file.
00001 // $Id: bdbuffer.cc,v 1.4 2008-02-07 23:39:09 rafi Exp $
00002 //
00003 // YAPET -- Yet Another Password Encryption Tool
00004 // Copyright (C) 2008  Rafael Ostertag
00005 //
00006 // This program is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 //
00019 
00020 #include "bdbuffer.h"
00021 
00022 using namespace YAPET;
00023 
00033 uint8_t*
00034 BDBuffer::alloc_mem(uint32_t s) throw(YAPETException) {
00035     uint8_t* tmp = (uint8_t*) malloc(s);
00036     if (tmp == NULL)
00037     throw YAPETException("Memory exhausted");
00038 
00039     return tmp;
00040 }
00041 
00051 void
00052 BDBuffer::free_mem(uint8_t* d, uint32_t s) {
00053     memset(d, 0, s);
00054     free(d);
00055 }
00056 
00062 BDBuffer::BDBuffer(uint32_t is) throw(YAPETException) : _size(is) {
00063     data = alloc_mem(_size);
00064 }
00065 
00072 BDBuffer::BDBuffer() : _size(0), data(NULL) { }
00073 
00074 BDBuffer::BDBuffer(const BDBuffer& ed) throw(YAPETException) {
00075     if (ed.data == NULL) {
00076     data = NULL;
00077     _size = 0;
00078     return;
00079     }
00080 
00081     data = alloc_mem(ed._size);
00082     memcpy(data, ed.data, ed._size);
00083     _size = ed._size;
00084 }
00085 
00091 BDBuffer::~BDBuffer() {
00092     if (data == NULL) return;
00093     free_mem(data, _size);
00094 }
00095 
00110 void
00111 BDBuffer::resize(uint32_t ns) throw(YAPETException) {
00112     if (data == NULL) {
00113     data = alloc_mem(ns);
00114     _size = ns;
00115     return;
00116     }
00117 
00118     uint8_t* newbuf = alloc_mem(ns);
00119 
00120     if (ns > _size) 
00121     memcpy(newbuf, data, _size);
00122     else
00123     memcpy(newbuf, data, ns);
00124 
00125     free_mem(data, _size);
00126 
00127     _size = ns;
00128     data = newbuf;
00129 }
00130 
00146 uint8_t*
00147 BDBuffer::at(uint32_t pos) throw(std::out_of_range) {
00148     if (pos > (_size - 1))
00149     throw std::out_of_range("Position out of range");
00150 
00151     return data + pos;
00152 }
00153 
00168 const uint8_t*
00169 BDBuffer::at(uint32_t pos) const 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 
00176 const BDBuffer&
00177 BDBuffer::operator=(const BDBuffer& ed) {
00178     if (this == &ed) return *this;
00179 
00180     if (data != NULL)
00181     free_mem(data, _size);
00182 
00183     if (ed.data != NULL) {
00184     data = alloc_mem(ed._size);
00185     memcpy(data, ed.data, ed._size);
00186     } else {
00187     data = NULL;
00188     }
00189 
00190     _size = ed._size;
00191     return *this;
00192 }

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