1 // Copyright 2018 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 /////////////////////////////////////////////////////////////////////////////// 16 17 #ifndef TINK_UTIL_ISTREAM_INPUT_STREAM_H_ 18 #define TINK_UTIL_ISTREAM_INPUT_STREAM_H_ 19 20 #include <stdint.h> 21 22 #include <istream> 23 #include <memory> 24 25 #include "tink/input_stream.h" 26 #include "tink/util/status.h" 27 #include "tink/util/statusor.h" 28 29 namespace crypto { 30 namespace tink { 31 namespace util { 32 33 // An InputStream that reads from a std::istream. 34 class IstreamInputStream : public crypto::tink::InputStream { 35 public: 36 // Constructs an InputStream that will read from the 'input' istream, 37 // using a buffer of the specified size, if any (if no legal 'buffer_size' 38 // is given, a reasonable default will be used). 39 explicit IstreamInputStream(std::unique_ptr<std::istream> input, 40 int buffer_size = -1); 41 42 ~IstreamInputStream() override; 43 44 crypto::tink::util::StatusOr<int> Next(const void** data) override; 45 46 void BackUp(int count) override; 47 48 int64_t Position() const override; 49 50 private: 51 util::Status status_; 52 std::unique_ptr<std::istream> input_; 53 std::unique_ptr<uint8_t[]> buffer_; 54 const int buffer_size_; 55 int64_t position_; // current position in the istream (from the beginning) 56 57 // Counters that describe the state of the data in buffer_. 58 int count_in_buffer_; // # of bytes available in buffer_ 59 int count_backedup_; // # of bytes available in buffer_ that were backed up 60 int buffer_offset_; // offset at which the returned bytes start in buffer_ 61 }; 62 63 } // namespace util 64 } // namespace tink 65 } // namespace crypto 66 67 #endif // TINK_UTIL_ISTREAM_INPUT_STREAM_H_ 68