IBR-DTNSuite  0.10
InputCipherStream.cpp
Go to the documentation of this file.
1 /*
2  * InputCipherStream.cpp
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
23 
24 namespace ibrcommon
25 {
26  InputCipherStream::InputCipherStream(std::istream &stream, const CipherMode mode, const size_t buffer)
27  : std::istream(this), _stream(stream), _mode(mode), data_buf_(buffer), data_size_(buffer)
28  {
29  // Initialize get pointer. This should be zero so that underflow is called upon first read.
30  setg(0, 0, 0);
31  setp(&data_buf_[0], &data_buf_[0] + data_size_ - 1);
32  }
33 
35  {
36  }
37 
38  std::char_traits<char>::int_type InputCipherStream::underflow()
39  {
40  _stream.read(&data_buf_[0], data_size_);
41  int bytes = _stream.gcount();
42 
43  if (bytes == 0)
44  {
45  return std::char_traits<char>::eof();
46  }
47 
48  // Since the input buffer content is now valid (or is new)
49  // the get pointer should be initialized (or reset).
50  setg(&data_buf_[0], &data_buf_[0], &data_buf_[0] + bytes);
51 
52  return std::char_traits<char>::not_eof(data_buf_[0]);
53  }
54 }