2017-07-18 20:42:18 +01:00
|
|
|
/* Copyright (c) 2017, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#ifndef OPENSSL_HEADER_SSL_SPAN_H
|
|
|
|
#define OPENSSL_HEADER_SSL_SPAN_H
|
|
|
|
|
|
|
|
#include <openssl/base.h>
|
|
|
|
|
|
|
|
#if !defined(BORINGSSL_NO_CXX)
|
|
|
|
|
2017-07-27 19:05:20 +01:00
|
|
|
extern "C++" {
|
|
|
|
|
2017-07-18 20:42:18 +01:00
|
|
|
#include <algorithm>
|
2017-09-28 00:02:51 +01:00
|
|
|
#include <cstdlib>
|
2017-07-18 20:42:18 +01:00
|
|
|
#include <type_traits>
|
|
|
|
|
Support symbol prefixes
- In base.h, if BORINGSSL_PREFIX is defined, include
boringssl_prefix_symbols.h
- In all .S files, if BORINGSSL_PREFIX is defined, include
boringssl_prefix_symbols_asm.h
- In base.h, BSSL_NAMESPACE_BEGIN and BSSL_NAMESPACE_END are
defined with appropriate values depending on whether
BORINGSSL_PREFIX is defined; these macros are used in place
of 'namespace bssl {' and '}'
- Add util/make_prefix_headers.go, which takes a list of symbols
and auto-generates the header files mentioned above
- In CMakeLists.txt, if BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS
are defined, run util/make_prefix_headers.go to generate header
files
- In various CMakeLists.txt files, add "global_target" that all
targets depend on to give us a place to hook logic that must run
before all other targets (in particular, the header file generation
logic)
- Document this in BUILDING.md, including the fact that it is
the caller's responsibility to provide the symbol list and keep it
up to date
- Note that this scheme has not been tested on Windows, and likely
does not work on it; Windows support will need to be added in a
future commit
Change-Id: If66a7157f46b5b66230ef91e15826b910cf979a2
Reviewed-on: https://boringssl-review.googlesource.com/31364
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
Reviewed-by: David Benjamin <davidben@google.com>
2018-08-27 02:53:36 +01:00
|
|
|
BSSL_NAMESPACE_BEGIN
|
2017-07-18 20:42:18 +01:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Span;
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
template <typename T>
|
|
|
|
class SpanBase {
|
2017-08-19 00:21:50 +01:00
|
|
|
// Put comparison operator implementations into a base class with const T, so
|
|
|
|
// they can be used with any type that implicitly converts into a Span.
|
2017-07-18 20:42:18 +01:00
|
|
|
static_assert(std::is_const<T>::value,
|
|
|
|
"Span<T> must be derived from SpanBase<const T>");
|
|
|
|
|
|
|
|
friend bool operator==(Span<T> lhs, Span<T> rhs) {
|
2017-08-19 00:21:50 +01:00
|
|
|
// MSVC issues warning C4996 because std::equal is unsafe. The pragma to
|
|
|
|
// suppress the warning mysteriously has no effect, hence this
|
|
|
|
// implementation. See
|
|
|
|
// https://msdn.microsoft.com/en-us/library/aa985974.aspx.
|
2017-07-24 23:49:07 +01:00
|
|
|
if (lhs.size() != rhs.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (T *l = lhs.begin(), *r = rhs.begin(); l != lhs.end() && r != rhs.end();
|
|
|
|
++l, ++r) {
|
|
|
|
if (*l != *r) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2017-07-18 20:42:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator!=(Span<T> lhs, Span<T> rhs) { return !(lhs == rhs); }
|
|
|
|
};
|
|
|
|
} // namespace internal
|
|
|
|
|
2017-08-19 00:21:50 +01:00
|
|
|
// A Span<T> is a non-owning reference to a contiguous array of objects of type
|
|
|
|
// |T|. Conceptually, a Span is a simple a pointer to |T| and a count of
|
|
|
|
// elements accessible via that pointer. The elements referenced by the Span can
|
|
|
|
// be mutated if |T| is mutable.
|
|
|
|
//
|
|
|
|
// A Span can be constructed from container types implementing |data()| and
|
|
|
|
// |size()| methods. If |T| is constant, construction from a container type is
|
|
|
|
// implicit. This allows writing methods that accept data from some unspecified
|
|
|
|
// container type:
|
|
|
|
//
|
|
|
|
// // Foo views data referenced by v.
|
|
|
|
// void Foo(bssl::Span<const uint8_t> v) { ... }
|
|
|
|
//
|
|
|
|
// std::vector<uint8_t> vec;
|
|
|
|
// Foo(vec);
|
|
|
|
//
|
|
|
|
// For mutable Spans, conversion is explicit:
|
|
|
|
//
|
|
|
|
// // FooMutate mutates data referenced by v.
|
|
|
|
// void FooMutate(bssl::Span<uint8_t> v) { ... }
|
|
|
|
//
|
|
|
|
// FooMutate(bssl::Span<uint8_t>(vec));
|
|
|
|
//
|
|
|
|
// You can also use the |MakeSpan| and |MakeConstSpan| factory methods to
|
|
|
|
// construct Spans in order to deduce the type of the Span automatically.
|
|
|
|
//
|
|
|
|
// FooMutate(bssl::MakeSpan(vec));
|
|
|
|
//
|
|
|
|
// Note that Spans have value type sematics. They are cheap to construct and
|
|
|
|
// copy, and should be passed by value whenever a method would otherwise accept
|
|
|
|
// a reference or pointer to a container or array.
|
2017-07-18 20:42:18 +01:00
|
|
|
template <typename T>
|
|
|
|
class Span : private internal::SpanBase<const T> {
|
|
|
|
private:
|
|
|
|
// Heuristically test whether C is a container type that can be converted into
|
|
|
|
// a Span by checking for data() and size() member functions.
|
2017-09-26 21:12:15 +01:00
|
|
|
//
|
|
|
|
// TODO(davidben): Switch everything to std::enable_if_t when we remove
|
|
|
|
// support for MSVC 2015. Although we could write our own enable_if_t and MSVC
|
|
|
|
// 2015 has std::enable_if_t anyway, MSVC 2015's SFINAE implementation is
|
|
|
|
// problematic and does not work below unless we write the ::type at use.
|
2017-07-18 20:42:18 +01:00
|
|
|
template <typename C>
|
2017-09-26 21:12:15 +01:00
|
|
|
using EnableIfContainer = std::enable_if<
|
2017-07-18 20:42:18 +01:00
|
|
|
std::is_convertible<decltype(std::declval<C>().data()), T *>::value &&
|
|
|
|
std::is_integral<decltype(std::declval<C>().size())>::value>;
|
|
|
|
|
2017-10-05 18:28:54 +01:00
|
|
|
static const size_t npos = static_cast<size_t>(-1);
|
2017-09-28 00:02:51 +01:00
|
|
|
|
2017-07-18 20:42:18 +01:00
|
|
|
public:
|
|
|
|
constexpr Span() : Span(nullptr, 0) {}
|
|
|
|
constexpr Span(T *ptr, size_t len) : data_(ptr), size_(len) {}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
constexpr Span(T (&array)[N]) : Span(array, N) {}
|
|
|
|
|
2017-09-26 21:12:15 +01:00
|
|
|
template <
|
|
|
|
typename C, typename = typename EnableIfContainer<C>::type,
|
|
|
|
typename = typename std::enable_if<std::is_const<T>::value, C>::type>
|
2017-07-18 20:42:18 +01:00
|
|
|
Span(const C &container) : data_(container.data()), size_(container.size()) {}
|
|
|
|
|
2017-09-26 21:12:15 +01:00
|
|
|
template <
|
|
|
|
typename C, typename = typename EnableIfContainer<C>::type,
|
|
|
|
typename = typename std::enable_if<!std::is_const<T>::value, C>::type>
|
2017-07-18 20:42:18 +01:00
|
|
|
explicit Span(C &container)
|
|
|
|
: data_(container.data()), size_(container.size()) {}
|
|
|
|
|
|
|
|
T *data() const { return data_; }
|
|
|
|
size_t size() const { return size_; }
|
2017-09-28 00:02:51 +01:00
|
|
|
bool empty() const { return size_ == 0; }
|
2017-07-18 20:42:18 +01:00
|
|
|
|
|
|
|
T *begin() const { return data_; }
|
|
|
|
const T *cbegin() const { return data_; }
|
2019-02-07 17:11:47 +00:00
|
|
|
T *end() const { return data_ + size_; }
|
|
|
|
const T *cend() const { return end(); }
|
2017-07-18 20:42:18 +01:00
|
|
|
|
2017-10-04 23:14:28 +01:00
|
|
|
T &front() const {
|
2018-04-08 19:03:10 +01:00
|
|
|
if (size_ == 0) {
|
|
|
|
abort();
|
|
|
|
}
|
2017-10-04 23:14:28 +01:00
|
|
|
return data_[0];
|
|
|
|
}
|
|
|
|
T &back() const {
|
2018-04-08 19:03:10 +01:00
|
|
|
if (size_ == 0) {
|
|
|
|
abort();
|
|
|
|
}
|
2017-10-04 23:14:28 +01:00
|
|
|
return data_[size_ - 1];
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:03:10 +01:00
|
|
|
T &operator[](size_t i) const {
|
|
|
|
if (i >= size_) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return data_[i];
|
|
|
|
}
|
|
|
|
T &at(size_t i) const { return (*this)[i]; }
|
2017-07-18 20:42:18 +01:00
|
|
|
|
2017-09-28 00:02:51 +01:00
|
|
|
Span subspan(size_t pos = 0, size_t len = npos) const {
|
|
|
|
if (pos > size_) {
|
|
|
|
abort(); // absl::Span throws an exception here.
|
|
|
|
}
|
|
|
|
return Span(data_ + pos, std::min(size_ - pos, len));
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:42:18 +01:00
|
|
|
private:
|
|
|
|
T *data_;
|
|
|
|
size_t size_;
|
|
|
|
};
|
|
|
|
|
2017-09-28 00:02:51 +01:00
|
|
|
template <typename T>
|
|
|
|
const size_t Span<T>::npos;
|
|
|
|
|
2017-07-18 20:42:18 +01:00
|
|
|
template <typename T>
|
|
|
|
Span<T> MakeSpan(T *ptr, size_t size) {
|
|
|
|
return Span<T>(ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename C>
|
|
|
|
auto MakeSpan(C &c) -> decltype(MakeSpan(c.data(), c.size())) {
|
|
|
|
return MakeSpan(c.data(), c.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Span<const T> MakeConstSpan(T *ptr, size_t size) {
|
|
|
|
return Span<const T>(ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename C>
|
|
|
|
auto MakeConstSpan(const C &c) -> decltype(MakeConstSpan(c.data(), c.size())) {
|
|
|
|
return MakeConstSpan(c.data(), c.size());
|
|
|
|
}
|
|
|
|
|
Support symbol prefixes
- In base.h, if BORINGSSL_PREFIX is defined, include
boringssl_prefix_symbols.h
- In all .S files, if BORINGSSL_PREFIX is defined, include
boringssl_prefix_symbols_asm.h
- In base.h, BSSL_NAMESPACE_BEGIN and BSSL_NAMESPACE_END are
defined with appropriate values depending on whether
BORINGSSL_PREFIX is defined; these macros are used in place
of 'namespace bssl {' and '}'
- Add util/make_prefix_headers.go, which takes a list of symbols
and auto-generates the header files mentioned above
- In CMakeLists.txt, if BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS
are defined, run util/make_prefix_headers.go to generate header
files
- In various CMakeLists.txt files, add "global_target" that all
targets depend on to give us a place to hook logic that must run
before all other targets (in particular, the header file generation
logic)
- Document this in BUILDING.md, including the fact that it is
the caller's responsibility to provide the symbol list and keep it
up to date
- Note that this scheme has not been tested on Windows, and likely
does not work on it; Windows support will need to be added in a
future commit
Change-Id: If66a7157f46b5b66230ef91e15826b910cf979a2
Reviewed-on: https://boringssl-review.googlesource.com/31364
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
Reviewed-by: David Benjamin <davidben@google.com>
2018-08-27 02:53:36 +01:00
|
|
|
BSSL_NAMESPACE_END
|
2017-07-18 20:42:18 +01:00
|
|
|
|
|
|
|
} // extern C++
|
|
|
|
|
|
|
|
#endif // !defined(BORINGSSL_NO_CXX)
|
|
|
|
|
2017-08-19 00:21:50 +01:00
|
|
|
#endif // OPENSSL_HEADER_SSL_SPAN_H
|