1 /* Copyright (c) 2017, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #ifndef OPENSSL_HEADER_SSL_SPAN_H
16 #define OPENSSL_HEADER_SSL_SPAN_H
17
18 #include <openssl/base.h>
19
20 #if !defined(BORINGSSL_NO_CXX)
21
22 extern "C++" {
23
24 #include <stdlib.h>
25
26 #include <algorithm>
27 #include <type_traits>
28
29 BSSL_NAMESPACE_BEGIN
30
31 template <typename T>
32 class Span;
33
34 namespace internal {
35 template <typename T>
36 class SpanBase {
37 // Put comparison operator implementations into a base class with const T, so
38 // they can be used with any type that implicitly converts into a Span.
39 static_assert(std::is_const<T>::value,
40 "Span<T> must be derived from SpanBase<const T>");
41
42 friend bool operator==(Span<T> lhs, Span<T> rhs) {
43 // MSVC issues warning C4996 because std::equal is unsafe. The pragma to
44 // suppress the warning mysteriously has no effect, hence this
45 // implementation. See
46 // https://msdn.microsoft.com/en-us/library/aa985974.aspx.
47 if (lhs.size() != rhs.size()) {
48 return false;
49 }
50 for (T *l = lhs.begin(), *r = rhs.begin(); l != lhs.end() && r != rhs.end();
51 ++l, ++r) {
52 if (*l != *r) {
53 return false;
54 }
55 }
56 return true;
57 }
58
59 friend bool operator!=(Span<T> lhs, Span<T> rhs) { return !(lhs == rhs); }
60 };
61 } // namespace internal
62
63 // A Span<T> is a non-owning reference to a contiguous array of objects of type
64 // |T|. Conceptually, a Span is a simple a pointer to |T| and a count of
65 // elements accessible via that pointer. The elements referenced by the Span can
66 // be mutated if |T| is mutable.
67 //
68 // A Span can be constructed from container types implementing |data()| and
69 // |size()| methods. If |T| is constant, construction from a container type is
70 // implicit. This allows writing methods that accept data from some unspecified
71 // container type:
72 //
73 // // Foo views data referenced by v.
74 // void Foo(bssl::Span<const uint8_t> v) { ... }
75 //
76 // std::vector<uint8_t> vec;
77 // Foo(vec);
78 //
79 // For mutable Spans, conversion is explicit:
80 //
81 // // FooMutate mutates data referenced by v.
82 // void FooMutate(bssl::Span<uint8_t> v) { ... }
83 //
84 // FooMutate(bssl::Span<uint8_t>(vec));
85 //
86 // You can also use the |MakeSpan| and |MakeConstSpan| factory methods to
87 // construct Spans in order to deduce the type of the Span automatically.
88 //
89 // FooMutate(bssl::MakeSpan(vec));
90 //
91 // Note that Spans have value type sematics. They are cheap to construct and
92 // copy, and should be passed by value whenever a method would otherwise accept
93 // a reference or pointer to a container or array.
94 template <typename T>
95 class Span : private internal::SpanBase<const T> {
96 private:
97 static const size_t npos = static_cast<size_t>(-1);
98
99 // Heuristically test whether C is a container type that can be converted into
100 // a Span by checking for data() and size() member functions.
101 //
102 // TODO(davidben): Require C++17 support for std::is_convertible_v, etc.
103 template <typename C>
104 using EnableIfContainer = std::enable_if_t<
105 std::is_convertible<decltype(std::declval<C>().data()), T *>::value &&
106 std::is_integral<decltype(std::declval<C>().size())>::value>;
107
108 public:
Span()109 constexpr Span() : Span(nullptr, 0) {}
Span(T * ptr,size_t len)110 constexpr Span(T *ptr, size_t len) : data_(ptr), size_(len) {}
111
112 template <size_t N>
Span(T (& array)[N])113 constexpr Span(T (&array)[N]) : Span(array, N) {}
114
115 template <typename C, typename = EnableIfContainer<C>,
116 typename = std::enable_if_t<std::is_const<T>::value, C>>
Span(const C & container)117 constexpr Span(const C &container)
118 : data_(container.data()), size_(container.size()) {}
119
120 template <typename C, typename = EnableIfContainer<C>,
121 typename = std::enable_if_t<!std::is_const<T>::value, C>>
Span(C & container)122 constexpr explicit Span(C &container)
123 : data_(container.data()), size_(container.size()) {}
124
data()125 constexpr T *data() const { return data_; }
size()126 constexpr size_t size() const { return size_; }
empty()127 constexpr bool empty() const { return size_ == 0; }
128
begin()129 constexpr T *begin() const { return data_; }
cbegin()130 constexpr const T *cbegin() const { return data_; }
end()131 constexpr T *end() const { return data_ + size_; }
cend()132 constexpr const T *cend() const { return end(); }
133
front()134 constexpr T &front() const {
135 if (size_ == 0) {
136 abort();
137 }
138 return data_[0];
139 }
back()140 constexpr T &back() const {
141 if (size_ == 0) {
142 abort();
143 }
144 return data_[size_ - 1];
145 }
146
147 constexpr T &operator[](size_t i) const {
148 if (i >= size_) {
149 abort();
150 }
151 return data_[i];
152 }
at(size_t i)153 T &at(size_t i) const { return (*this)[i]; }
154
155 constexpr Span subspan(size_t pos = 0, size_t len = npos) const {
156 if (pos > size_) {
157 // absl::Span throws an exception here. Note std::span and Chromium
158 // base::span additionally forbid pos + len being out of range, with a
159 // special case at npos/dynamic_extent, while absl::Span::subspan clips
160 // the span. For now, we align with absl::Span in case we switch to it in
161 // the future.
162 abort();
163 }
164 return Span(data_ + pos, std::min(size_ - pos, len));
165 }
166
first(size_t len)167 constexpr Span first(size_t len) const {
168 if (len > size_) {
169 abort();
170 }
171 return Span(data_, len);
172 }
173
last(size_t len)174 constexpr Span last(size_t len) const {
175 if (len > size_) {
176 abort();
177 }
178 return Span(data_ + size_ - len, len);
179 }
180
181 private:
182 T *data_;
183 size_t size_;
184 };
185
186 template <typename T>
187 const size_t Span<T>::npos;
188
189 template <typename T>
MakeSpan(T * ptr,size_t size)190 constexpr Span<T> MakeSpan(T *ptr, size_t size) {
191 return Span<T>(ptr, size);
192 }
193
194 template <typename C>
195 constexpr auto MakeSpan(C &c) -> decltype(MakeSpan(c.data(), c.size())) {
196 return MakeSpan(c.data(), c.size());
197 }
198
199 template <typename T>
MakeConstSpan(T * ptr,size_t size)200 constexpr Span<const T> MakeConstSpan(T *ptr, size_t size) {
201 return Span<const T>(ptr, size);
202 }
203
204 template <typename C>
205 constexpr auto MakeConstSpan(const C &c)
206 -> decltype(MakeConstSpan(c.data(), c.size())) {
207 return MakeConstSpan(c.data(), c.size());
208 }
209
210 template <typename T, size_t size>
MakeConstSpan(T (& array)[size])211 constexpr Span<const T> MakeConstSpan(T (&array)[size]) {
212 return array;
213 }
214
215 BSSL_NAMESPACE_END
216
217 } // extern C++
218
219 #endif // !defined(BORINGSSL_NO_CXX)
220
221 #endif // OPENSSL_HEADER_SSL_SPAN_H
222