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 #include "tink/util/ostream_output_stream.h"
18
19 #include <algorithm>
20 #include <cerrno>
21 #include <cstring>
22 #include <memory>
23 #include <ostream>
24 #include <utility>
25
26 #include "absl/memory/memory.h"
27 #include "absl/status/status.h"
28 #include "tink/output_stream.h"
29 #include "tink/util/errors.h"
30 #include "tink/util/status.h"
31 #include "tink/util/statusor.h"
32
33 namespace crypto {
34 namespace tink {
35 namespace util {
36
OstreamOutputStream(std::unique_ptr<std::ostream> output,int buffer_size)37 OstreamOutputStream::OstreamOutputStream(std::unique_ptr<std::ostream> output,
38 int buffer_size) :
39 buffer_size_(buffer_size > 0 ? buffer_size : 128 * 1024) { // 128 KB
40 output_ = std::move(output);
41 count_in_buffer_ = 0;
42 count_backedup_ = 0;
43 buffer_ = nullptr;
44 position_ = 0;
45 buffer_offset_ = 0;
46 status_ = OkStatus();
47 }
48
Next(void ** data)49 crypto::tink::util::StatusOr<int> OstreamOutputStream::Next(void** data) {
50 if (!status_.ok()) return status_;
51
52 if (buffer_ == nullptr) { // possible only at the first call to Next()
53 buffer_ = absl::make_unique<uint8_t[]>(buffer_size_);
54 *data = buffer_.get();
55 count_in_buffer_ = buffer_size_;
56 position_ = buffer_size_;
57 return buffer_size_;
58 }
59
60 // If some space was backed up, return it first.
61 if (count_backedup_ > 0) {
62 position_ = position_ + count_backedup_;
63 buffer_offset_ = count_in_buffer_;
64 count_in_buffer_ = count_in_buffer_ + count_backedup_;
65 int backedup = count_backedup_;
66 count_backedup_ = 0;
67 *data = buffer_.get() + buffer_offset_;
68 return backedup;
69 }
70
71 // No space was backed up, so count_in_buffer_ == buffer_size_ holds here.
72 // Write the data from the buffer, and return available space in buffer_.
73 // The available space might not span the entire buffer_, as writing
74 // may succeed only for a prefix of buffer_ -- in this case the data still
75 // to be written is shifted in buffer_ and the remaining space is returned.
76 int write_result = output_->rdbuf()->sputn(
77 reinterpret_cast<char*>(buffer_.get()), buffer_size_);
78 if (write_result == 0) { // No data written or an I/O error occurred.
79 if (output_->good()) return 0;
80 status_ = ToStatusF(absl::StatusCode::kInternal, "I/O error upon write: %s",
81 std::strerror(errno));
82 return status_;
83 }
84 // Some data was written, so we can return some portion of buffer_.
85 position_ = position_ + write_result;
86 count_in_buffer_ = buffer_size_;
87 count_backedup_ = 0;
88 buffer_offset_ = buffer_size_ - write_result;
89 *data = buffer_.get() + buffer_offset_;
90 if (write_result < buffer_size_) {
91 // Only part of the data was written, shift the remaining data in buffer_.
92 // Using memmove, as source and destination may overlap.
93 std::memmove(buffer_.get(), buffer_.get() + write_result, buffer_offset_);
94 }
95 return write_result;
96 }
97
BackUp(int count)98 void OstreamOutputStream::BackUp(int count) {
99 if (!status_.ok() || count < 1 || count_in_buffer_ == 0) return;
100 int curr_buffer_size = buffer_size_ - buffer_offset_;
101 int actual_count = std::min(count, curr_buffer_size - count_backedup_);
102 count_backedup_ += actual_count;
103 count_in_buffer_ -= actual_count;
104 position_ -= actual_count;
105 }
106
~OstreamOutputStream()107 OstreamOutputStream::~OstreamOutputStream() {
108 Close().IgnoreError();
109 }
110
Close()111 Status OstreamOutputStream::Close() {
112 if (!status_.ok()) return status_;
113 if (count_in_buffer_ > 0) {
114 // Try to write the remaining bytes.
115 output_->write(reinterpret_cast<char*>(buffer_.get()), count_in_buffer_);
116 if (!output_->good()) { // An I/O error occurred.
117 status_ = ToStatusF(absl::StatusCode::kInternal,
118 "I/O error upon write: %d", errno);
119 return status_;
120 }
121 }
122 output_->flush();
123 if (!output_->good()) {
124 status_ = ToStatusF(absl::StatusCode::kInternal,
125 "I/O error upon flushing: %d", errno);
126 return status_;
127 }
128 status_ = Status(absl::StatusCode::kFailedPrecondition, "Stream closed");
129 return OkStatus();
130 }
131
Position() const132 int64_t OstreamOutputStream::Position() const {
133 return position_;
134 }
135
136 } // namespace util
137 } // namespace tink
138 } // namespace crypto
139