1 // Copyright 2020 Google LLC 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_PYTHON_TINK_CC_PYTHON_INPUT_STREAM_H_ 18 #define TINK_PYTHON_TINK_CC_PYTHON_INPUT_STREAM_H_ 19 20 #include <memory> 21 #include <string> 22 23 #include "tink/input_stream.h" 24 #include "tink/util/status.h" 25 #include "tink/util/statusor.h" 26 #include "tink/cc/python_file_object_adapter.h" 27 28 namespace crypto { 29 namespace tink { 30 31 // An InputStream that reads from a PythonFileObjectAdapter. 32 class PythonInputStream : public InputStream { 33 public: 34 // Constructs an InputStream that will read from the PythonFileObjectAdapter 35 // specified via 'adapter', using a buffer of the specified size, if any 36 // (if 'buffer_size' <= 0, a reasonable default will be used). 37 explicit PythonInputStream(std::shared_ptr<PythonFileObjectAdapter> adapter, 38 int buffer_size = 0); 39 40 ~PythonInputStream() override; 41 42 util::StatusOr<int> Next(const void** data) override; 43 44 void BackUp(int count) override; 45 46 int64_t Position() const override; 47 48 private: 49 util::Status status_; 50 std::shared_ptr<PythonFileObjectAdapter> adapter_; 51 std::string buffer_; 52 int64_t position_; // current position in the file object (from the 53 // beginning) 54 55 // Counters that describe the state of the data in buffer_. 56 int count_in_buffer_; // # of bytes available in buffer_ 57 int count_backedup_; // # of bytes available in buffer_ that were backed up 58 int buffer_offset_; // offset at which the returned bytes start in buffer_ 59 }; 60 61 } // namespace tink 62 } // namespace crypto 63 64 #endif // TINK_PYTHON_TINK_CC_PYTHON_INPUT_STREAM_H_ 65