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