1 // Copyright 2019 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/buffer.h"
18
19 #include <memory>
20
21 #include "absl/memory/memory.h"
22 #include "absl/status/status.h"
23 #include "tink/util/status.h"
24 #include "tink/util/statusor.h"
25
26 namespace crypto {
27 namespace tink {
28 namespace util {
29 namespace {
30
31 class OwningBuffer : public Buffer {
32 public:
33 // Constructs a new Buffer which allocates a new memory block
34 // of size 'allocated_size' and uses it for the actual data.
35 // The allocated memory block is owned by this Buffer.
36 // It is assumed that 'allocated_size' is positive.
OwningBuffer(int allocated_size)37 explicit OwningBuffer(int allocated_size)
38 : allocated_size_(allocated_size), size_(allocated_size) {
39 owned_mem_block_ = absl::make_unique<char[]>(allocated_size);
40 }
41
get_mem_block() const42 char* const get_mem_block() const override {
43 return owned_mem_block_.get();
44 }
45
allocated_size() const46 int allocated_size() const override { return allocated_size_; }
47
size() const48 int size() const override { return size_; }
49
set_size(int new_size)50 util::Status set_size(int new_size) override {
51 if (new_size < 0 || new_size > allocated_size_) {
52 return Status(absl::StatusCode::kInvalidArgument,
53 "new_size must satisfy 0 <= new_size <= allocated_size()");
54 }
55 size_ = new_size;
56 return OkStatus();
57 }
58
59 ~OwningBuffer() override = default;
60
61 private:
62 std::unique_ptr<char[]> owned_mem_block_;
63 const int allocated_size_;
64 int size_;
65 };
66
67
68 class NonOwningBuffer : public Buffer {
69 public:
70 // Constructs a new Buffer which uses the given 'mem_block' as a buffer
71 // for the actual data.
72 // Does NOT take the ownership of 'mem_block' which must be non-null,
73 // must allocate at least 'allocated_size' bytes, and must remain alive
74 // as long as the returned Buffer is in use.
75 // It is assumed that 'mem_block' is non-null, and that
76 // 'allocated_size' is positive.
NonOwningBuffer(char * mem_block,int allocated_size)77 NonOwningBuffer(char* mem_block, int allocated_size)
78 : mem_block_(mem_block),
79 allocated_size_(allocated_size), size_(allocated_size) {}
80
get_mem_block() const81 char* const get_mem_block() const override { return mem_block_; };
82
allocated_size() const83 int allocated_size() const override { return allocated_size_; }
84
size() const85 int size() const override { return size_; }
86
set_size(int new_size)87 util::Status set_size(int new_size) override {
88 if (new_size < 0 || new_size > allocated_size_) {
89 return Status(absl::StatusCode::kInvalidArgument,
90 "new_size must satisfy 0 <= new_size <= allocated_size()");
91 }
92 size_ = new_size;
93 return OkStatus();
94 }
95
96 ~NonOwningBuffer() override = default;
97
98 private:
99 char* const mem_block_;
100 const int allocated_size_;
101 int size_;
102 };
103
104 } // namespace
105
106 // static
New(int allocated_size)107 StatusOr<std::unique_ptr<Buffer>> Buffer::New(int allocated_size) {
108 if (allocated_size <= 0) {
109 return Status(absl::StatusCode::kInvalidArgument,
110 "allocated_size must be positive");
111 }
112 return {absl::make_unique<OwningBuffer>(allocated_size)};
113 }
114
115 // static
NewNonOwning(char * mem_block,int allocated_size)116 StatusOr<std::unique_ptr<Buffer>> Buffer::NewNonOwning(
117 char* mem_block, int allocated_size) {
118 if (allocated_size <= 0) {
119 return Status(absl::StatusCode::kInvalidArgument,
120 "allocated_size must be positive");
121 }
122 if (mem_block == nullptr) {
123 return Status(absl::StatusCode::kInvalidArgument,
124 "mem_block must be non-null");
125 }
126 return {absl::make_unique<NonOwningBuffer>(mem_block, allocated_size)};
127 }
128
129
130 } // namespace util
131 } // namespace tink
132 } // namespace crypto
133