• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ibrcommon/ibrcommon/ssl/HashStream.cpp

Go to the documentation of this file.
00001 /*
00002  * HashStream.cpp
00003  *
00004  *  Created on: 12.07.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #include "HashStream.h"
00009 
00010 namespace ibrcommon
00011 {
00012         HashStream::HashStream(const size_t hash, const size_t buffer)
00013          : iostream(this), data_buf_(new char[buffer]), data_size_(buffer), hash_buf_(new char[hash]), hash_size_(hash), final_(false)
00014         {
00015                 // Initialize get pointer.  This should be zero so that underflow is called upon first read.
00016                 setg(0, 0, 0);
00017                 setp(data_buf_, data_buf_ + data_size_ - 1);
00018         }
00019 
00020         HashStream::~HashStream()
00021         {
00022                 delete[] data_buf_;
00023                 delete[] hash_buf_;
00024         }
00025 
00026         int HashStream::sync()
00027         {
00028                 int ret = std::char_traits<char>::eq_int_type(this->overflow(
00029                                 std::char_traits<char>::eof()), std::char_traits<char>::eof()) ? -1
00030                                 : 0;
00031 
00032                 return ret;
00033         }
00034 
00035         int HashStream::overflow(int c)
00036         {
00037                 char *ibegin = data_buf_;
00038                 char *iend = pptr();
00039 
00040                 // mark the buffer as free
00041                 setp(data_buf_, data_buf_ + data_size_ - 1);
00042 
00043                 if (!std::char_traits<char>::eq_int_type(c, std::char_traits<char>::eof()))
00044                 {
00045                         *iend++ = std::char_traits<char>::to_char_type(c);
00046                 }
00047 
00048                 // if there is nothing to send, just return
00049                 if ((iend - ibegin) == 0)
00050                 {
00051                         return std::char_traits<char>::not_eof(c);
00052                 }
00053 
00054                 // hashing
00055                 update(data_buf_, (iend - ibegin));
00056 
00057                 return std::char_traits<char>::not_eof(c);
00058         }
00059 
00060         int HashStream::underflow()
00061         {
00062                 // TODO: add seek mechanisms to reset the istream
00063 
00064                 if (!final_)
00065                 {
00066                         finalize(hash_buf_, hash_size_);
00067                         final_ = true;
00068 
00069                         // Since the input buffer content is now valid (or is new)
00070                         // the get pointer should be initialized (or reset).
00071                         setg(hash_buf_, hash_buf_, hash_buf_ + hash_size_);
00072 
00073                         return std::char_traits<char>::not_eof(hash_buf_[0]);
00074                 }
00075 
00076                 return std::char_traits<char>::eof();
00077         }
00078 }

Generated on Thu Nov 11 2010 09:49:47 for IBR-DTNSuite by  doxygen 1.7.1