summaryrefslogtreecommitdiff
path: root/src/buffer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/buffer.cpp')
-rw-r--r--src/buffer.cpp96
1 files changed, 52 insertions, 44 deletions
diff --git a/src/buffer.cpp b/src/buffer.cpp
index 574c034..c4b0c5c 100644
--- a/src/buffer.cpp
+++ b/src/buffer.cpp
@@ -11,7 +11,7 @@
* tunneling and relaying of packets of any protocol.
*
*
- * Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl,
+ * Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl,
* Christian Pointner <satp@wirdorange.org>
*
* This file is part of Anytun.
@@ -41,11 +41,11 @@
#include "buffer.h"
Buffer::Buffer(bool allow_realloc) : buf_(0), length_(0), real_length_(0), allow_realloc_(allow_realloc)
-{
+{
}
-Buffer::Buffer(u_int32_t length, bool allow_realloc) : length_(length), real_length_(length_ + Buffer::OVER_SIZE_),
- allow_realloc_(allow_realloc)
+Buffer::Buffer(u_int32_t length, bool allow_realloc) : length_(length), real_length_(length_ + Buffer::OVER_SIZE_),
+ allow_realloc_(allow_realloc)
{
buf_ = new u_int8_t[real_length_];
if(!buf_) {
@@ -56,8 +56,8 @@ Buffer::Buffer(u_int32_t length, bool allow_realloc) : length_(length), real_len
std::memset(buf_, 0, real_length_);
}
-Buffer::Buffer(u_int8_t* data, u_int32_t length, bool allow_realloc) : length_(length), real_length_(length + Buffer::OVER_SIZE_),
- allow_realloc_(allow_realloc)
+Buffer::Buffer(u_int8_t* data, u_int32_t length, bool allow_realloc) : length_(length), real_length_(length + Buffer::OVER_SIZE_),
+ allow_realloc_(allow_realloc)
{
if(!data) {
length_ = 0;
@@ -74,9 +74,9 @@ Buffer::Buffer(u_int8_t* data, u_int32_t length, bool allow_realloc) : length_(l
std::memcpy(buf_, data, length_);
}
-Buffer::Buffer(std::string hex_data, bool allow_realloc) : length_(static_cast<u_int32_t>(hex_data.size())/2),
- real_length_(length_ + Buffer::OVER_SIZE_),
- allow_realloc_(allow_realloc)
+Buffer::Buffer(std::string hex_data, bool allow_realloc) : length_(static_cast<u_int32_t>(hex_data.size())/2),
+ real_length_(length_ + Buffer::OVER_SIZE_),
+ allow_realloc_(allow_realloc)
{
buf_ = new u_int8_t[real_length_];
if(!buf_) {
@@ -84,23 +84,23 @@ Buffer::Buffer(std::string hex_data, bool allow_realloc) : length_(static_cast<u
real_length_ = 0;
throw std::bad_alloc();
}
-
- for(u_int32_t i=0; i<length_; ++i)
- {
+
+ for(u_int32_t i=0; i<length_; ++i) {
u_int32_t tmp;
std::istringstream ss(std::string(hex_data.c_str(), i*2, 2));
- if(!(ss >> std::hex >> tmp)) tmp = 0;
+ if(!(ss >> std::hex >> tmp)) { tmp = 0; }
buf_[i] = static_cast<u_int8_t>(tmp);
}
}
Buffer::~Buffer()
{
- if(buf_)
+ if(buf_) {
delete[] buf_;
+ }
}
-Buffer::Buffer(const Buffer &src) : length_(src.length_), real_length_(src.real_length_), allow_realloc_(src.allow_realloc_)
+Buffer::Buffer(const Buffer& src) : length_(src.length_), real_length_(src.real_length_), allow_realloc_(src.allow_realloc_)
{
buf_ = new u_int8_t[real_length_];
if(!buf_) {
@@ -111,15 +111,16 @@ Buffer::Buffer(const Buffer &src) : length_(src.length_), real_length_(src.real_
std::memcpy(buf_, src.buf_, length_);
}
-void Buffer::operator=(const Buffer &src)
+void Buffer::operator=(const Buffer& src)
{
- if(buf_)
+ if(buf_) {
delete[] buf_;
-
+ }
+
length_ = src.length_;
- real_length_ = src.real_length_;
+ real_length_ = src.real_length_;
allow_realloc_ = src.allow_realloc_;
-
+
buf_ = new u_int8_t[real_length_];
if(!buf_) {
length_ = 0;
@@ -129,26 +130,29 @@ void Buffer::operator=(const Buffer &src)
std::memcpy(buf_, src.buf_, length_);
}
-bool Buffer::operator==(const Buffer &cmp) const
+bool Buffer::operator==(const Buffer& cmp) const
{
- if(length_ != cmp.length_)
+ if(length_ != cmp.length_) {
return false;
+ }
- if(!std::memcmp(buf_, cmp.buf_, length_))
+ if(!std::memcmp(buf_, cmp.buf_, length_)) {
return true;
+ }
return false;
}
-Buffer Buffer::operator^(const Buffer &xor_by) const
+Buffer Buffer::operator^(const Buffer& xor_by) const
{
u_int32_t res_length = (xor_by.length_ > length_) ? xor_by.length_ : length_;
u_int32_t min_length = (xor_by.length_ < length_) ? xor_by.length_ : length_;
Buffer res(res_length);
- for( u_int32_t index = 0; index < min_length; index++ )
+ for(u_int32_t index = 0; index < min_length; index++) {
res[index] = buf_[index] ^ xor_by[index];
-
+ }
+
return res;
}
@@ -159,42 +163,45 @@ u_int32_t Buffer::getLength() const
void Buffer::setLength(u_int32_t new_length)
{
- if(new_length == length_)
+ if(new_length == length_) {
return;
+ }
- if(new_length > real_length_)
- {
- if(!allow_realloc_)
+ if(new_length > real_length_) {
+ if(!allow_realloc_) {
throw std::out_of_range("buffer::setLength() - reallocation not allowed for this Buffer");
+ }
u_int8_t* old_buf = buf_;
u_int32_t old_length = length_;
length_ = new_length;
real_length_ = length_ + Buffer::OVER_SIZE_;
-
+
buf_ = new u_int8_t[real_length_];
if(!buf_) {
length_ = 0;
real_length_ = 0;
- if(old_buf)
+ if(old_buf) {
delete[] old_buf;
-
+ }
+
throw std::bad_alloc();
}
std::memcpy(buf_, old_buf, old_length);
- if(old_buf)
+ if(old_buf) {
delete[] old_buf;
+ }
old_buf = &buf_[old_length];
std::memset(old_buf, 0, real_length_ - old_length);
- }
- else
+ } else {
length_ = new_length;
+ }
reinit();
-}
+}
u_int8_t* Buffer::getBuf()
@@ -204,16 +211,18 @@ u_int8_t* Buffer::getBuf()
u_int8_t& Buffer::operator[](u_int32_t index)
{
- if(index >= length_)
+ if(index >= length_) {
throw std::out_of_range("buffer::operator[]");
+ }
return buf_[index];
}
u_int8_t Buffer::operator[](u_int32_t index) const
{
- if(index >= length_)
+ if(index >= length_) {
throw std::out_of_range("buffer::operator[] const");
+ }
return buf_[index];
}
@@ -227,15 +236,15 @@ std::string Buffer::getHexDump() const
{
std::stringstream ss;
ss << "Length=" << length_ << std::endl << std::hex << std::uppercase;
- for( u_int32_t index = 0; index < length_; index++ )
- {
+ for(u_int32_t index = 0; index < length_; index++) {
ss << std::setw(2) << std::setfill('0') << u_int32_t(buf_[index]) << " ";
if(!((index+1) % 16)) {
ss << std::endl;
continue;
}
- if(!((index+1) % 8))
+ if(!((index+1) % 8)) {
ss << " ";
+ }
}
return ss.str();
}
@@ -244,8 +253,7 @@ std::string Buffer::getHexDumpOneLine() const
{
std::stringstream ss;
ss << length_ << " Bytes,'" << std::hex << std::uppercase;
- for( u_int32_t index = 0; index < length_; index++ )
- {
+ for(u_int32_t index = 0; index < length_; index++) {
ss << std::setw(2) << std::setfill('0') << u_int32_t(buf_[index]);
}
ss << "'";