00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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 if (tmp == NULL)
00058 throw YAPETException(_("Memory exhausted"));
00059
00060 return tmp;
00061 }
00062
00072 void
00073 BDBuffer::free_mem(uint8_t* d, uint32_t s) {
00074 memset(d, 0, s);
00075 free(d);
00076 }
00077
00083 BDBuffer::BDBuffer(uint32_t is) throw(YAPETException) : _size(is) {
00084 data = alloc_mem(_size);
00085 }
00086
00093 BDBuffer::BDBuffer() : _size(0), data(NULL) { }
00094
00095 BDBuffer::BDBuffer(const BDBuffer& ed) throw(YAPETException) {
00096 if (ed.data == NULL) {
00097 data = NULL;
00098 _size = 0;
00099 return;
00100 }
00101
00102 data = alloc_mem(ed._size);
00103 memcpy(data, ed.data, ed._size);
00104 _size = ed._size;
00105 }
00106
00112 BDBuffer::~BDBuffer() {
00113 if (data == NULL) return;
00114 free_mem(data, _size);
00115 }
00116
00131 void
00132 BDBuffer::resize(uint32_t ns) throw(YAPETException) {
00133 if (data == NULL) {
00134 data = alloc_mem(ns);
00135 _size = ns;
00136 return;
00137 }
00138
00139 uint8_t* newbuf = alloc_mem(ns);
00140
00141 if (ns > _size)
00142 memcpy(newbuf, data, _size);
00143 else
00144 memcpy(newbuf, data, ns);
00145
00146 free_mem(data, _size);
00147
00148 _size = ns;
00149 data = newbuf;
00150 }
00151
00167 uint8_t*
00168 BDBuffer::at(uint32_t pos) throw(std::out_of_range) {
00169 if (pos > (_size - 1))
00170 throw std::out_of_range(_("Position out of range"));
00171
00172 return data + pos;
00173 }
00174
00189 const uint8_t*
00190 BDBuffer::at(uint32_t pos) const throw(std::out_of_range) {
00191 if (pos > (_size - 1))
00192 throw std::out_of_range(_("Position out of range"));
00193
00194 return data + pos;
00195 }
00196
00197 const BDBuffer&
00198 BDBuffer::operator=(const BDBuffer& ed) {
00199 if (this == &ed) return *this;
00200
00201 if (data != NULL)
00202 free_mem(data, _size);
00203
00204 if (ed.data != NULL) {
00205 data = alloc_mem(ed._size);
00206 memcpy(data, ed.data, ed._size);
00207 } else {
00208 data = NULL;
00209 }
00210
00211 _size = ed._size;
00212 return *this;
00213 }