Go to the documentation of this file.00001
00002
00003
00004
00005
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
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
00046 if ((iend - ibegin) == 0)
00047 {
00048 return std::char_traits<char>::not_eof(c);
00049 }
00050
00051
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
00064 _stream.write(data_buf_, (iend - ibegin));
00065
00066 return std::char_traits<char>::not_eof(c);
00067 }
00068 }