Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 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 | |
David Benjamin | af2b1e8 | 2017-07-27 14:05:20 -0400 | [diff] [blame] | 22 | extern "C++" { |
| 23 | |
David Benjamin | 061a7f5 | 2020-12-15 13:47:05 -0500 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | #include <type_traits> |
| 28 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 29 | BSSL_NAMESPACE_BEGIN |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 30 | |
| 31 | template <typename T> |
| 32 | class Span; |
| 33 | |
| 34 | namespace internal { |
| 35 | template <typename T> |
| 36 | class SpanBase { |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 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. |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 39 | static_assert(std::is_const<T>::value, |
| 40 | "Span<T> must be derived from SpanBase<const T>"); |
| 41 | |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 42 | friend bool operator==(Span<T> lhs, Span<T> rhs) { |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 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. |
Martin Kreichgauer | 2eee131 | 2017-07-24 15:49:07 -0700 | [diff] [blame] | 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; |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 57 | } |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 58 | |
| 59 | friend bool operator!=(Span<T> lhs, Span<T> rhs) { return !(lhs == rhs); } |
| 60 | }; |
| 61 | } // namespace internal |
| 62 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 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. |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 94 | template <typename T> |
| 95 | class Span : private internal::SpanBase<const T> { |
| 96 | private: |
David Benjamin | 12fdd08 | 2017-10-05 13:28:54 -0400 | [diff] [blame] | 97 | static const size_t npos = static_cast<size_t>(-1); |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 98 | |
David Benjamin | 2144076 | 2022-03-19 18:39:18 -0400 | [diff] [blame] | 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 | // |
David Benjamin | 493d5cb | 2022-04-18 17:20:27 -0400 | [diff] [blame] | 102 | // TODO(davidben): Require C++17 support for std::is_convertible_v, etc. |
David Benjamin | 2144076 | 2022-03-19 18:39:18 -0400 | [diff] [blame] | 103 | template <typename C> |
David Benjamin | 493d5cb | 2022-04-18 17:20:27 -0400 | [diff] [blame] | 104 | using EnableIfContainer = std::enable_if_t< |
David Benjamin | 2144076 | 2022-03-19 18:39:18 -0400 | [diff] [blame] | 105 | std::is_convertible<decltype(std::declval<C>().data()), T *>::value && |
David Benjamin | 493d5cb | 2022-04-18 17:20:27 -0400 | [diff] [blame] | 106 | std::is_integral<decltype(std::declval<C>().size())>::value>; |
David Benjamin | 2144076 | 2022-03-19 18:39:18 -0400 | [diff] [blame] | 107 | |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 108 | public: |
| 109 | constexpr Span() : Span(nullptr, 0) {} |
| 110 | constexpr Span(T *ptr, size_t len) : data_(ptr), size_(len) {} |
| 111 | |
| 112 | template <size_t N> |
| 113 | constexpr Span(T (&array)[N]) : Span(array, N) {} |
| 114 | |
David Benjamin | 493d5cb | 2022-04-18 17:20:27 -0400 | [diff] [blame] | 115 | template <typename C, typename = EnableIfContainer<C>, |
| 116 | typename = std::enable_if_t<std::is_const<T>::value, C>> |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 117 | Span(const C &container) : data_(container.data()), size_(container.size()) {} |
| 118 | |
David Benjamin | 493d5cb | 2022-04-18 17:20:27 -0400 | [diff] [blame] | 119 | template <typename C, typename = EnableIfContainer<C>, |
| 120 | typename = std::enable_if_t<!std::is_const<T>::value, C>> |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 121 | explicit Span(C &container) |
| 122 | : data_(container.data()), size_(container.size()) {} |
| 123 | |
| 124 | T *data() const { return data_; } |
| 125 | size_t size() const { return size_; } |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 126 | bool empty() const { return size_ == 0; } |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 127 | |
| 128 | T *begin() const { return data_; } |
| 129 | const T *cbegin() const { return data_; } |
David Benjamin | a6c689e | 2019-02-07 11:11:47 -0600 | [diff] [blame] | 130 | T *end() const { return data_ + size_; } |
| 131 | const T *cend() const { return end(); } |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 132 | |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 133 | T &front() const { |
David Benjamin | 68478b7 | 2018-04-08 14:03:10 -0400 | [diff] [blame] | 134 | if (size_ == 0) { |
| 135 | abort(); |
| 136 | } |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 137 | return data_[0]; |
| 138 | } |
| 139 | T &back() const { |
David Benjamin | 68478b7 | 2018-04-08 14:03:10 -0400 | [diff] [blame] | 140 | if (size_ == 0) { |
| 141 | abort(); |
| 142 | } |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 143 | return data_[size_ - 1]; |
| 144 | } |
| 145 | |
David Benjamin | 68478b7 | 2018-04-08 14:03:10 -0400 | [diff] [blame] | 146 | T &operator[](size_t i) const { |
| 147 | if (i >= size_) { |
| 148 | abort(); |
| 149 | } |
| 150 | return data_[i]; |
| 151 | } |
| 152 | T &at(size_t i) const { return (*this)[i]; } |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 153 | |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 154 | Span subspan(size_t pos = 0, size_t len = npos) const { |
| 155 | if (pos > size_) { |
David Benjamin | 006f20a | 2021-06-22 23:00:49 -0400 | [diff] [blame] | 156 | // absl::Span throws an exception here. Note std::span and Chromium |
| 157 | // base::span additionally forbid pos + len being out of range, with a |
| 158 | // special case at npos/dynamic_extent, while absl::Span::subspan clips |
| 159 | // the span. For now, we align with absl::Span in case we switch to it in |
| 160 | // the future. |
| 161 | abort(); |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 162 | } |
| 163 | return Span(data_ + pos, std::min(size_ - pos, len)); |
| 164 | } |
| 165 | |
David Benjamin | 006f20a | 2021-06-22 23:00:49 -0400 | [diff] [blame] | 166 | Span first(size_t len) { |
| 167 | if (len > size_) { |
| 168 | abort(); |
| 169 | } |
| 170 | return Span(data_, len); |
| 171 | } |
| 172 | |
| 173 | Span last(size_t len) { |
| 174 | if (len > size_) { |
| 175 | abort(); |
| 176 | } |
| 177 | return Span(data_ + size_ - len, len); |
| 178 | } |
| 179 | |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 180 | private: |
| 181 | T *data_; |
| 182 | size_t size_; |
| 183 | }; |
| 184 | |
| 185 | template <typename T> |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 186 | const size_t Span<T>::npos; |
| 187 | |
| 188 | template <typename T> |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 189 | Span<T> MakeSpan(T *ptr, size_t size) { |
| 190 | return Span<T>(ptr, size); |
| 191 | } |
| 192 | |
| 193 | template <typename C> |
| 194 | auto MakeSpan(C &c) -> decltype(MakeSpan(c.data(), c.size())) { |
| 195 | return MakeSpan(c.data(), c.size()); |
| 196 | } |
| 197 | |
| 198 | template <typename T> |
| 199 | Span<const T> MakeConstSpan(T *ptr, size_t size) { |
| 200 | return Span<const T>(ptr, size); |
| 201 | } |
| 202 | |
| 203 | template <typename C> |
| 204 | auto MakeConstSpan(const C &c) -> decltype(MakeConstSpan(c.data(), c.size())) { |
| 205 | return MakeConstSpan(c.data(), c.size()); |
| 206 | } |
| 207 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 208 | BSSL_NAMESPACE_END |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 209 | |
| 210 | } // extern C++ |
| 211 | |
| 212 | #endif // !defined(BORINGSSL_NO_CXX) |
| 213 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 214 | #endif // OPENSSL_HEADER_SSL_SPAN_H |