Imported Upstream version 0.3.4
[anytun.git] / src / buffer.cpp
index 574c034..320129d 100644 (file)
@@ -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.
 #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(uint32_t length, bool allow_realloc) : length_(length), real_length_(length_ + Buffer::OVER_SIZE_),
+  allow_realloc_(allow_realloc)
 {
-  buf_ = new u_int8_t[real_length_];
+  buf_ = new uint8_t[real_length_];
   if(!buf_) {
     length_ = 0;
     real_length_ = 0;
@@ -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(uint8_t* data, uint32_t length, bool allow_realloc) : length_(length), real_length_(length + Buffer::OVER_SIZE_),
+  allow_realloc_(allow_realloc)
 {
   if(!data) {
     length_ = 0;
@@ -65,7 +65,7 @@ Buffer::Buffer(u_int8_t* data, u_int32_t length, bool allow_realloc) : length_(l
     return;
   }
 
-  buf_ = new u_int8_t[real_length_];
+  buf_ = new uint8_t[real_length_];
   if(!buf_) {
     length_ = 0;
     real_length_ = 0;
@@ -74,35 +74,35 @@ 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<uint32_t>(hex_data.size())/2),
+  real_length_(length_ + Buffer::OVER_SIZE_),
+  allow_realloc_(allow_realloc)
 {
-  buf_ = new u_int8_t[real_length_];
+  buf_ = new uint8_t[real_length_];
   if(!buf_) {
     length_ = 0;
     real_length_ = 0;
     throw std::bad_alloc();
   }
-  
-  for(u_int32_t i=0; i<length_; ++i)
-  {
-    u_int32_t tmp;
+
+  for(uint32_t i=0; i<length_; ++i) {
+    uint32_t tmp;
     std::istringstream ss(std::string(hex_data.c_str(), i*2, 2));
-    if(!(ss >> std::hex >> tmp)) tmp = 0;
-    buf_[i] = static_cast<u_int8_t>(tmp);
+    if(!(ss >> std::hex >> tmp)) { tmp = 0; }
+    buf_[i] = static_cast<uint8_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 Buffersrc) : length_(src.length_), real_length_(src.real_length_), allow_realloc_(src.allow_realloc_)
 {
-  buf_ = new u_int8_t[real_length_];
+  buf_ = new uint8_t[real_length_];
   if(!buf_) {
     length_ = 0;
     real_length_ = 0;
@@ -111,16 +111,17 @@ 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 Buffersrc)
 {
-  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_];
+
+  buf_ = new uint8_t[real_length_];
   if(!buf_) {
     length_ = 0;
     real_length_ = 0;
@@ -129,96 +130,104 @@ void Buffer::operator=(const Buffer &src)
   std::memcpy(buf_, src.buf_, length_);
 }
 
-bool Buffer::operator==(const Buffer &cmp) const
+bool Buffer::operator==(const Buffercmp) 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 Bufferxor_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_;
+  uint32_t res_length = (xor_by.length_ > length_) ? xor_by.length_ : length_;
+  uint32_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(uint32_t index = 0; index < min_length; index++) {
     res[index] = buf_[index] ^ xor_by[index];
-  
+  }
+
   return res;
 }
 
-u_int32_t Buffer::getLength() const
+uint32_t Buffer::getLength() const
 {
   return length_;
 }
 
-void Buffer::setLength(u_int32_t new_length)
+void Buffer::setLength(uint32_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_;
+    uint8_t* old_buf = buf_;
+    uint32_t old_length = length_;
 
     length_ = new_length;
     real_length_ = length_ + Buffer::OVER_SIZE_;
-    
-    buf_ = new u_int8_t[real_length_];
+
+    buf_ = new uint8_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()
+uint8_t* Buffer::getBuf()
 {
   return buf_;
 }
 
-u_int8_t& Buffer::operator[](u_int32_t index)
+uint8_t& Buffer::operator[](uint32_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
+uint8_t Buffer::operator[](uint32_t index) const
 {
-  if(index >= length_)
+  if(index >= length_) {
     throw std::out_of_range("buffer::operator[] const");
+  }
 
   return buf_[index];
 }
 
-Buffer::operator u_int8_t*()
+Buffer::operator uint8_t*()
 {
   return buf_;
 }
@@ -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++ )
-  {
-    ss << std::setw(2) << std::setfill('0') << u_int32_t(buf_[index]) << " ";
+  for(uint32_t index = 0; index < length_; index++) {
+    ss << std::setw(2) << std::setfill('0') << uint32_t(buf_[index]) << " ";
     if(!((index+1) % 16)) {
       ss << std::endl;
       continue;
     }
-    if(!((index+1) % 8))
+    if(!((index+1) % 8)) {
       ss << " ";
+    }
   }
   return ss.str();
 }
@@ -244,9 +253,8 @@ std::string Buffer::getHexDumpOneLine() const
 {
   std::stringstream ss;
   ss << length_ << " Bytes,'" << std::hex << std::uppercase;
-  for( u_int32_t index = 0; index < length_; index++ )
-  {
-    ss << std::setw(2) << std::setfill('0') << u_int32_t(buf_[index]);
+  for(uint32_t index = 0; index < length_; index++) {
+    ss << std::setw(2) << std::setfill('0') << uint32_t(buf_[index]);
   }
   ss << "'";
   return ss.str();