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_FILE_OBJECT_ADAPTER_H_ 18 #define TINK_PYTHON_TINK_CC_PYTHON_FILE_OBJECT_ADAPTER_H_ 19 20 #include <string> 21 22 #include "absl/strings/string_view.h" 23 #include "tink/util/status.h" 24 #include "tink/util/statusor.h" 25 26 namespace crypto { 27 namespace tink { 28 29 // Adapts a Python file object for use in C++. 30 // This is wrapped with pybind and implemented in Python. 31 class PythonFileObjectAdapter { 32 public: 33 // Writes 'data' to the underlying Python file object and returns the number 34 // of bytes written, which can be less than the size of 'data'. 35 virtual util::StatusOr<int> Write(absl::string_view data) = 0; 36 37 // Closes the underlying Python file object. 38 virtual util::Status Close() = 0; 39 40 // Reads at most 'size' bytes from the underlying Python file object. Returns 41 // UNKNOWN status with error message that contains "EOFError" if the file 42 // object is alreday at EOF. 43 virtual util::StatusOr<std::string> Read(int size) = 0; 44 ~PythonFileObjectAdapter()45 virtual ~PythonFileObjectAdapter() {} 46 }; 47 48 } // namespace tink 49 } // namespace crypto 50 51 #endif // TINK_PYTHON_TINK_CC_PYTHON_FILE_OBJECT_ADAPTER_H_ 52