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 #include "tink/cc/python_input_stream.h"
18
19 #include <algorithm>
20 #include <memory>
21 #include <string>
22
23 #include "absl/memory/memory.h"
24 #include "absl/status/status.h"
25 #include "absl/strings/match.h"
26 #include "tink/input_stream.h"
27 #include "tink/subtle/subtle_util.h"
28 #include "tink/util/status.h"
29 #include "tink/util/statusor.h"
30 #include "tink/cc/python_file_object_adapter.h"
31
32 namespace crypto {
33 namespace tink {
34
35 namespace {
36
is_eof(const util::Status & status)37 bool is_eof(const util::Status& status) {
38 return status.code() == absl::StatusCode::kUnknown &&
39 absl::StrContains(status.message(), "EOFError");
40 }
41
42 } // namespace
43
PythonInputStream(std::shared_ptr<PythonFileObjectAdapter> adapter,int buffer_size)44 PythonInputStream::PythonInputStream(
45 std::shared_ptr<PythonFileObjectAdapter> adapter, int buffer_size) {
46 if (buffer_size <= 0) buffer_size = 128 * 1024; // 128 KB
47 adapter_ = adapter;
48 count_in_buffer_ = 0;
49 count_backedup_ = 0;
50 position_ = 0;
51 subtle::ResizeStringUninitialized(&buffer_, buffer_size);
52 buffer_offset_ = 0;
53 status_ = util::OkStatus();
54 }
55
Next(const void ** data)56 util::StatusOr<int> PythonInputStream::Next(const void** data) {
57 if (!status_.ok()) return status_;
58
59 if (count_backedup_ > 0) { // Return the backed-up bytes.
60 buffer_offset_ += count_in_buffer_ - count_backedup_;
61 count_in_buffer_ = count_backedup_;
62 count_backedup_ = 0;
63 position_ += count_in_buffer_;
64 *data = &buffer_[buffer_offset_];
65 return count_in_buffer_;
66 }
67
68 // Read new bytes to buffer_.
69 auto read_result = adapter_->Read(buffer_.size());
70 if (is_eof(read_result.status())) {
71 return status_ = util::Status(absl::StatusCode::kOutOfRange, "EOF");
72 } else if (read_result.status().code() == absl::StatusCode::kOutOfRange) {
73 // We need to change the error code because for InputStream OUT_OF_RANGE
74 // status always means EOF.
75 return status_ = util::Status(absl::StatusCode::kUnknown,
76 read_result.status().message());
77 } else if (!read_result.ok()) {
78 return status_ = read_result.status();
79 }
80 std::string read_string = read_result.value();
81 int count_read = read_string.length();
82 buffer_.replace(0, count_read, read_string);
83 buffer_offset_ = 0;
84 count_backedup_ = 0;
85 count_in_buffer_ = count_read;
86 position_ += count_in_buffer_;
87 *data = &buffer_[0];
88 return count_in_buffer_;
89 }
90
BackUp(int count)91 void PythonInputStream::BackUp(int count) {
92 if (!status_.ok() || count < 1 || count_backedup_ == count_in_buffer_) return;
93 int actual_count = std::min(count, count_in_buffer_ - count_backedup_);
94 count_backedup_ += actual_count;
95 position_ -= actual_count;
96 }
97
~PythonInputStream()98 PythonInputStream::~PythonInputStream() {}
99
Position() const100 int64_t PythonInputStream::Position() const { return position_; }
101
102 } // namespace tink
103 } // namespace crypto
104