• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_OUTPUT_STREAM_H_
18 #define TINK_PYTHON_TINK_CC_PYTHON_OUTPUT_STREAM_H_
19 
20 #include <memory>
21 #include <string>
22 
23 #include "tink/output_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 OutputStream that writes to a PythonFileObjectAdapter.
32 class PythonOutputStream : public OutputStream {
33  public:
34   // Constructs an OutputStream that will write to 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 PythonOutputStream(std::shared_ptr<PythonFileObjectAdapter> adapter,
38                               int buffer_size = 0);
39 
40   ~PythonOutputStream() override;
41 
42   crypto::tink::util::StatusOr<int> Next(void** data) override;
43 
44   void BackUp(int count) override;
45 
46   crypto::tink::util::Status Close() override;
47 
48   int64_t Position() const override;
49 
50  private:
51   util::Status status_;
52   std::shared_ptr<PythonFileObjectAdapter> adapter_;
53   std::string buffer_;
54   bool is_first_call_;
55   int64_t position_;  // current position in the underlying stream (from the
56                       // beginning)
57 
58   // Counters that describe the state of the data in buffer_.
59   int count_in_buffer_;  // # bytes in buffer_ that will be eventually written
60   int buffer_offset_;    // offset where the returned *data starts in buffer_
61 };
62 
63 }  // namespace tink
64 }  // namespace crypto
65 
66 #endif  // TINK_PYTHON_TINK_CC_PYTHON_OUTPUT_STREAM_H_
67