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/istream_input_stream.h"
18
19 #include <errno.h>
20 #include <stdint.h>
21
22 #include <algorithm>
23 #include <cstring>
24 #include <istream>
25 #include <memory>
26 #include <utility>
27
28 #include "absl/memory/memory.h"
29 #include "absl/status/status.h"
30 #include "tink/util/errors.h"
31 #include "tink/util/status.h"
32 #include "tink/util/statusor.h"
33
34 namespace crypto {
35 namespace tink {
36 namespace util {
37
IstreamInputStream(std::unique_ptr<std::istream> input,int buffer_size)38 IstreamInputStream::IstreamInputStream(std::unique_ptr<std::istream> input,
39 int buffer_size) :
40 buffer_size_(buffer_size > 0 ? buffer_size : 128 * 1024) { // 128 KB
41 input_ = std::move(input);
42 count_in_buffer_ = 0;
43 count_backedup_ = 0;
44 position_ = 0;
45 buffer_ = absl::make_unique<uint8_t[]>(buffer_size_);
46 buffer_offset_ = 0;
47 status_ = util::OkStatus();
48 }
49
Next(const void ** data)50 crypto::tink::util::StatusOr<int> IstreamInputStream::Next(const void** data) {
51 if (!status_.ok()) return status_;
52 if (count_backedup_ > 0) { // Return the backed-up bytes.
53 buffer_offset_ = buffer_offset_ + (count_in_buffer_ - count_backedup_);
54 count_in_buffer_ = count_backedup_;
55 count_backedup_ = 0;
56 *data = buffer_.get() + buffer_offset_;
57 position_ = position_ + count_in_buffer_;
58 return count_in_buffer_;
59 }
60 // Read new bytes to buffer_.
61 input_->read(reinterpret_cast<char*>(buffer_.get()), buffer_size_);
62 int count_read = input_->gcount();
63 if (count_read == 0) { // Could not read bytes, EOF or an I/O error.
64 if (input_->good()) return count_read; // No bytes could be read.
65 // If !good(), distinguish EOF from other failures.
66 if (input_->eof()) {
67 status_ = Status(absl::StatusCode::kOutOfRange, "EOF");
68 } else {
69 status_ = ToStatusF(absl::StatusCode::kInternal, "I/O error: %s",
70 strerror(errno));
71 }
72 return status_;
73 }
74 buffer_offset_ = 0;
75 count_backedup_ = 0;
76 count_in_buffer_ = count_read;
77 position_ = position_ + count_in_buffer_;
78 *data = buffer_.get();
79 return count_in_buffer_;
80 }
81
BackUp(int count)82 void IstreamInputStream::BackUp(int count) {
83 if (!status_.ok() || count < 1 || count_backedup_ == count_in_buffer_) return;
84 int actual_count = std::min(count, count_in_buffer_ - count_backedup_);
85 count_backedup_ = count_backedup_ + actual_count;
86 position_ = position_ - actual_count;
87 }
88
89 IstreamInputStream::~IstreamInputStream() = default;
90
Position() const91 int64_t IstreamInputStream::Position() const {
92 return position_;
93 }
94
95 } // namespace util
96 } // namespace tink
97 } // namespace crypto
98