Imported Upstream version 0.3.5
[anytun.git] / src / buffer.cpp
1 /*
2  *  anytun
3  *
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 methods 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.
12  *
13  *
14  *  Copyright (C) 2007-2014 Markus Grüneis, Othmar Gsenger, Erwin Nindl,
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
19  *  Anytun is free software: you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License as published by
21  *  the Free Software Foundation, either version 3 of the License, or
22  *  any later version.
23  *
24  *  Anytun is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with Anytun.  If not, see <http://www.gnu.org/licenses/>.
31  *
32  *  In addition, as a special exception, the copyright holders give
33  *  permission to link the code of portions of this program with the
34  *  OpenSSL library under certain conditions as described in each
35  *  individual source file, and distribute linked combinations
36  *  including the two.
37  *  You must obey the GNU General Public License in all respects
38  *  for all of the code used other than OpenSSL.  If you modify
39  *  file(s) with this exception, you may extend this exception to your
40  *  version of the file(s), but you are not obligated to do so.  If you
41  *  do not wish to do so, delete this exception statement from your
42  *  version.  If you delete this exception statement from all source
43  *  files in the program, then also delete it here.
44  */
45
46 #include <cstring>
47 #include <stdexcept>
48 #include <string>
49 #include <sstream>
50 #include <iostream>
51 #include <boost/archive/text_oarchive.hpp>
52 #include <boost/archive/text_iarchive.hpp>
53 #include "datatypes.h"
54 #include "buffer.h"
55
56 Buffer::Buffer(bool allow_realloc) : buf_(0), length_(0), real_length_(0), allow_realloc_(allow_realloc)
57 {
58 }
59
60 Buffer::Buffer(uint32_t length, bool allow_realloc) : length_(length), real_length_(length_ + Buffer::OVER_SIZE_),
61   allow_realloc_(allow_realloc)
62 {
63   buf_ = new uint8_t[real_length_];
64   if(!buf_) {
65     length_ = 0;
66     real_length_ = 0;
67     throw std::bad_alloc();
68   }
69   std::memset(buf_, 0, real_length_);
70 }
71
72 Buffer::Buffer(uint8_t* data, uint32_t length, bool allow_realloc) : length_(length), real_length_(length + Buffer::OVER_SIZE_),
73   allow_realloc_(allow_realloc)
74 {
75   if(!data) {
76     length_ = 0;
77     real_length_ = 0;
78     return;
79   }
80
81   buf_ = new uint8_t[real_length_];
82   if(!buf_) {
83     length_ = 0;
84     real_length_ = 0;
85     throw std::bad_alloc();
86   }
87   std::memcpy(buf_, data, length_);
88 }
89
90 Buffer::Buffer(std::string hex_data, bool allow_realloc) : length_(static_cast<uint32_t>(hex_data.size())/2),
91   real_length_(length_ + Buffer::OVER_SIZE_),
92   allow_realloc_(allow_realloc)
93 {
94   buf_ = new uint8_t[real_length_];
95   if(!buf_) {
96     length_ = 0;
97     real_length_ = 0;
98     throw std::bad_alloc();
99   }
100
101   for(uint32_t i=0; i<length_; ++i) {
102     uint32_t tmp;
103     std::istringstream ss(std::string(hex_data.c_str(), i*2, 2));
104     if(!(ss >> std::hex >> tmp)) { tmp = 0; }
105     buf_[i] = static_cast<uint8_t>(tmp);
106   }
107 }
108
109 Buffer::~Buffer()
110 {
111   if(buf_) {
112     delete[] buf_;
113   }
114 }
115
116 Buffer::Buffer(const Buffer& src) : length_(src.length_), real_length_(src.real_length_), allow_realloc_(src.allow_realloc_)
117 {
118   buf_ = new uint8_t[real_length_];
119   if(!buf_) {
120     length_ = 0;
121     real_length_ = 0;
122     throw std::bad_alloc();
123   }
124   std::memcpy(buf_, src.buf_, length_);
125 }
126
127 void Buffer::operator=(const Buffer& src)
128 {
129   if(buf_) {
130     delete[] buf_;
131   }
132
133   length_ = src.length_;
134   real_length_ = src.real_length_;
135   allow_realloc_ = src.allow_realloc_;
136
137   buf_ = new uint8_t[real_length_];
138   if(!buf_) {
139     length_ = 0;
140     real_length_ = 0;
141     throw std::bad_alloc();
142   }
143   std::memcpy(buf_, src.buf_, length_);
144 }
145
146 bool Buffer::operator==(const Buffer& cmp) const
147 {
148   if(length_ != cmp.length_) {
149     return false;
150   }
151
152   if(!std::memcmp(buf_, cmp.buf_, length_)) {
153     return true;
154   }
155
156   return false;
157 }
158
159 Buffer Buffer::operator^(const Buffer& xor_by) const
160 {
161   uint32_t res_length = (xor_by.length_ > length_) ? xor_by.length_ : length_;
162   uint32_t min_length = (xor_by.length_ < length_) ? xor_by.length_ : length_;
163   Buffer res(res_length);
164
165   for(uint32_t index = 0; index < min_length; index++) {
166     res[index] = buf_[index] ^ xor_by[index];
167   }
168
169   return res;
170 }
171
172 uint32_t Buffer::getLength() const
173 {
174   return length_;
175 }
176
177 void Buffer::setLength(uint32_t new_length)
178 {
179   if(new_length == length_) {
180     return;
181   }
182
183   if(new_length > real_length_) {
184     if(!allow_realloc_) {
185       throw std::out_of_range("buffer::setLength() - reallocation not allowed for this Buffer");
186     }
187
188     uint8_t* old_buf = buf_;
189     uint32_t old_length = length_;
190
191     length_ = new_length;
192     real_length_ = length_ + Buffer::OVER_SIZE_;
193
194     buf_ = new uint8_t[real_length_];
195     if(!buf_) {
196       length_ = 0;
197       real_length_ = 0;
198       if(old_buf) {
199         delete[] old_buf;
200       }
201
202       throw std::bad_alloc();
203     }
204     std::memcpy(buf_, old_buf, old_length);
205
206     if(old_buf) {
207       delete[] old_buf;
208     }
209
210     old_buf = &buf_[old_length];
211     std::memset(old_buf, 0, real_length_ - old_length);
212   } else {
213     length_ = new_length;
214   }
215
216   reinit();
217 }
218
219
220 uint8_t* Buffer::getBuf()
221 {
222   return buf_;
223 }
224
225 uint8_t& Buffer::operator[](uint32_t index)
226 {
227   if(index >= length_) {
228     throw std::out_of_range("buffer::operator[]");
229   }
230
231   return buf_[index];
232 }
233
234 uint8_t Buffer::operator[](uint32_t index) const
235 {
236   if(index >= length_) {
237     throw std::out_of_range("buffer::operator[] const");
238   }
239
240   return buf_[index];
241 }
242
243 Buffer::operator uint8_t*()
244 {
245   return buf_;
246 }
247
248 std::string Buffer::getHexDump() const
249 {
250   std::stringstream ss;
251   ss << "Length=" << length_ << std::endl << std::hex << std::uppercase;
252   for(uint32_t index = 0; index < length_; index++) {
253     ss << std::setw(2) << std::setfill('0') << uint32_t(buf_[index]) << " ";
254     if(!((index+1) % 16)) {
255       ss << std::endl;
256       continue;
257     }
258     if(!((index+1) % 8)) {
259       ss << " ";
260     }
261   }
262   return ss.str();
263 }
264
265 std::string Buffer::getHexDumpOneLine() const
266 {
267   std::stringstream ss;
268   ss << length_ << " Bytes,'" << std::hex << std::uppercase;
269   for(uint32_t index = 0; index < length_; index++) {
270     ss << std::setw(2) << std::setfill('0') << uint32_t(buf_[index]);
271   }
272   ss << "'";
273   return ss.str();
274 }
275
276 bool Buffer::isReallocAllowed() const
277 {
278   return allow_realloc_;
279 }