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

ibrcommon/ibrcommon/ssl/CipherStream.cpp

Go to the documentation of this file.
00001 /*
00002  * CipherStream.cpp
00003  *
00004  *  Created on: 13.07.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrcommon/ssl/CipherStream.h"
00009 
00010 namespace ibrcommon
00011 {
00012         CipherStream::CipherStream(std::ostream &stream, const CipherMode mode, const size_t buffer)
00013          : std::ostream(this), _stream(stream), _mode(mode), data_buf_(new char[buffer]), data_size_(buffer)
00014         {
00015                 setp(data_buf_, data_buf_ + data_size_ - 1);
00016         }
00017 
00018         CipherStream::~CipherStream()
00019         {
00020                 delete[] data_buf_;
00021         }
00022 
00023         int CipherStream::sync()
00024         {
00025                 int ret = std::char_traits<char>::eq_int_type(this->overflow(
00026                                 std::char_traits<char>::eof()), std::char_traits<char>::eof()) ? -1
00027                                 : 0;
00028 
00029                 return ret;
00030         }
00031 
00032         int CipherStream::overflow(int c)
00033         {
00034                 char *ibegin = data_buf_;
00035                 char *iend = pptr();
00036 
00037                 // mark the buffer as free
00038                 setp(data_buf_, data_buf_ + data_size_ - 1);
00039 
00040                 if (!std::char_traits<char>::eq_int_type(c, std::char_traits<char>::eof()))
00041                 {
00042                         *iend++ = std::char_traits<char>::to_char_type(c);
00043                 }
00044 
00045                 // if there is nothing to send, just return
00046                 if ((iend - ibegin) == 0)
00047                 {
00048                         return std::char_traits<char>::not_eof(c);
00049                 }
00050 
00051                 // do cipher stuff
00052                 switch (_mode)
00053                 {
00054                 case CIPHER_ENCRYPT:
00055                         encrypt(data_buf_, (iend - ibegin));
00056                         break;
00057 
00058                 case CIPHER_DECRYPT:
00059                         decrypt(data_buf_, (iend - ibegin));
00060                         break;
00061                 }
00062 
00063                 // write result to stream
00064                 _stream.write(data_buf_, (iend - ibegin));
00065 
00066                 return std::char_traits<char>::not_eof(c);
00067         }
00068 }

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