fixed typo
[anytun.git] / buffer.cpp
blob250a8067274e0d63193b5ea97b7d67c5d0eeaaaa
1 /*
2 * anytun
4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <stdexcept>
32 #include <string>
33 #include <sstream>
34 #include <iostream>
35 #include <boost/archive/text_oarchive.hpp>
36 #include <boost/archive/text_iarchive.hpp>
37 #include "datatypes.h"
38 #include "buffer.h"
40 Buffer::Buffer(bool allow_realloc) : buf_(0), length_(0), real_length_(0), allow_realloc_(allow_realloc)
44 Buffer::Buffer(u_int32_t length, bool allow_realloc) : length_(length), real_length_(length_ + Buffer::OVER_SIZE_),
45 allow_realloc_(allow_realloc)
47 buf_ = new u_int8_t[real_length_];
48 if(!buf_) {
49 length_ = 0;
50 real_length_ = 0;
51 throw std::bad_alloc();
53 std::memset(buf_, 0, real_length_);
56 Buffer::Buffer(u_int8_t* data, u_int32_t length, bool allow_realloc) : length_(length), real_length_(length + Buffer::OVER_SIZE_),
57 allow_realloc_(allow_realloc)
59 if(!data) {
60 length_ = 0;
61 real_length_ = 0;
62 return;
65 buf_ = new u_int8_t[real_length_];
66 if(!buf_) {
67 length_ = 0;
68 real_length_ = 0;
69 throw std::bad_alloc();
71 std::memcpy(buf_, data, length_);
74 Buffer::Buffer(std::string hex_data, bool allow_realloc) : length_(hex_data.size()/2),
75 real_length_(length_ + Buffer::OVER_SIZE_),
76 allow_realloc_(allow_realloc)
78 buf_ = new u_int8_t[real_length_];
79 if(!buf_) {
80 length_ = 0;
81 real_length_ = 0;
82 throw std::bad_alloc();
85 for(u_int32_t i=0; i<length_; ++i)
87 u_int32_t tmp;
88 std::istringstream ss(std::string(hex_data.c_str(), i*2, 2));
89 if(!(ss >> std::hex >> tmp)) tmp = 0;
90 buf_[i] = tmp;
94 Buffer::~Buffer()
96 if(buf_)
97 delete[] buf_;
100 Buffer::Buffer(const Buffer &src) : length_(src.length_), real_length_(src.real_length_), allow_realloc_(src.allow_realloc_)
102 buf_ = new u_int8_t[real_length_];
103 if(!buf_) {
104 length_ = 0;
105 real_length_ = 0;
106 throw std::bad_alloc();
108 std::memcpy(buf_, src.buf_, length_);
111 void Buffer::operator=(const Buffer &src)
113 if(buf_)
114 delete[] buf_;
116 length_ = src.length_;
117 real_length_ = src.real_length_;
118 allow_realloc_ = src.allow_realloc_;
120 buf_ = new u_int8_t[real_length_];
121 if(!buf_) {
122 length_ = 0;
123 real_length_ = 0;
124 throw std::bad_alloc();
126 std::memcpy(buf_, src.buf_, length_);
131 bool Buffer::operator==(const Buffer &cmp) const
133 if(length_ != cmp.length_)
134 return false;
136 if(!std::memcmp(buf_, cmp.buf_, length_))
137 return true;
139 return false;
142 Buffer Buffer::operator^(const Buffer &xor_by) const
144 u_int32_t res_length = (xor_by.length_ > length_) ? xor_by.length_ : length_;
145 u_int32_t min_length = (xor_by.length_ < length_) ? xor_by.length_ : length_;
146 Buffer res(res_length);
148 for( u_int32_t index = 0; index < min_length; index++ )
149 res[index] = buf_[index] ^ xor_by[index];
151 return res;
154 u_int32_t Buffer::getLength() const
156 return length_;
159 void Buffer::setLength(u_int32_t new_length)
161 if(new_length == length_)
162 return;
164 if(new_length > real_length_)
166 if(!allow_realloc_)
167 throw std::out_of_range("buffer::setLength() - reallocation not allowed for this Buffer");
169 u_int8_t* old_buf = buf_;
170 u_int32_t old_length = length_;
172 length_ = new_length;
173 real_length_ = length_ + Buffer::OVER_SIZE_;
175 buf_ = new u_int8_t[real_length_];
176 if(!buf_) {
177 length_ = 0;
178 real_length_ = 0;
179 if(old_buf)
180 delete[] old_buf;
182 throw std::bad_alloc();
184 std::memcpy(buf_, old_buf, old_length);
186 if(old_buf)
187 delete[] old_buf;
189 old_buf = &buf_[old_length];
190 std::memset(old_buf, 0, real_length_ - old_length);
192 else
193 length_ = new_length;
195 reinit();
199 u_int8_t* Buffer::getBuf()
201 return buf_;
204 u_int8_t& Buffer::operator[](u_int32_t index)
206 if(index >= length_)
207 throw std::out_of_range("buffer::operator[]");
209 return buf_[index];
212 u_int8_t Buffer::operator[](u_int32_t index) const
214 if(index >= length_)
215 throw std::out_of_range("buffer::operator[] const");
217 return buf_[index];
220 Buffer::operator u_int8_t*()
222 return buf_;
225 std::string Buffer::getHexDump() const
227 std::stringstream ss;
228 ss << "Length=" << length_ << std::endl << std::hex << std::uppercase;
229 for( u_int32_t index = 0; index < length_; index++ )
231 ss << std::setw(2) << std::setfill('0') << u_int32_t(buf_[index]) << " ";
232 if(!((index+1) % 16)) {
233 ss << std::endl;
234 continue;
236 if(!((index+1) % 8))
237 ss << " ";
239 return ss.str();
242 std::string Buffer::getHexDumpOneLine() const
244 std::stringstream ss;
245 ss << length_ << " Bytes,'" << std::hex << std::uppercase;
246 for( u_int32_t index = 0; index < length_; index++ )
248 ss << std::setw(2) << std::setfill('0') << u_int32_t(buf_[index]);
250 ss << "'";
251 return ss.str();
254 bool Buffer::isReallocAllowed() const
256 return allow_realloc_;