vector_ref.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #include <cstring>
  3. #include <cassert>
  4. #include <type_traits>
  5. #include <vector>
  6. #include <string>
  7. #ifdef __INTEL_COMPILER
  8. #pragma warning(disable:597) //will not be called for implicit or explicit conversions
  9. #endif
  10. namespace dev
  11. {
  12. /**
  13. * A modifiable reference to an existing object or vector in memory.
  14. */
  15. template <class _T>
  16. class vector_ref
  17. {
  18. public:
  19. using value_type = _T;
  20. using element_type = _T;
  21. using mutable_value_type = typename std::conditional<std::is_const<_T>::value, typename std::remove_const<_T>::type, _T>::type;
  22. static_assert(std::is_pod<value_type>::value, "vector_ref can only be used with PODs due to its low-level treatment of data.");
  23. vector_ref(): m_data(nullptr), m_count(0) {}
  24. /// Creates a new vector_ref to point to @a _count elements starting at @a _data.
  25. vector_ref(_T* _data, size_t _count): m_data(_data), m_count(_count) {}
  26. /// Creates a new vector_ref pointing to the data part of a string (given as pointer).
  27. vector_ref(typename std::conditional<std::is_const<_T>::value, std::string const*, std::string*>::type _data): m_data(reinterpret_cast<_T*>(_data->data())), m_count(_data->size() / sizeof(_T)) {}
  28. /// Creates a new vector_ref pointing to the data part of a vector (given as pointer).
  29. vector_ref(typename std::conditional<std::is_const<_T>::value, std::vector<typename std::remove_const<_T>::type> const*, std::vector<_T>*>::type _data): m_data(_data->data()), m_count(_data->size()) {}
  30. /// Creates a new vector_ref pointing to the data part of a string (given as reference).
  31. vector_ref(typename std::conditional<std::is_const<_T>::value, std::string const&, std::string&>::type _data): m_data(reinterpret_cast<_T*>(_data.data())), m_count(_data.size() / sizeof(_T)) {}
  32. #if DEV_LDB
  33. vector_ref(ldb::Slice const& _s): m_data(reinterpret_cast<_T*>(_s.data())), m_count(_s.size() / sizeof(_T)) {}
  34. #endif
  35. explicit operator bool() const { return m_data && m_count; }
  36. bool contentsEqual(std::vector<mutable_value_type> const& _c) const { if (!m_data || m_count == 0) return _c.empty(); else return _c.size() == m_count && !memcmp(_c.data(), m_data, m_count * sizeof(_T)); }
  37. std::vector<mutable_value_type> toVector() const { return std::vector<mutable_value_type>(m_data, m_data + m_count); }
  38. std::vector<unsigned char> toBytes() const { return std::vector<unsigned char>(reinterpret_cast<unsigned char const*>(m_data), reinterpret_cast<unsigned char const*>(m_data) + m_count * sizeof(_T)); }
  39. std::string toString() const { return std::string((char const*)m_data, ((char const*)m_data) + m_count * sizeof(_T)); }
  40. template <class _T2> explicit operator vector_ref<_T2>() const { assert(m_count * sizeof(_T) / sizeof(_T2) * sizeof(_T2) / sizeof(_T) == m_count); return vector_ref<_T2>(reinterpret_cast<_T2*>(m_data), m_count * sizeof(_T) / sizeof(_T2)); }
  41. operator vector_ref<_T const>() const { return vector_ref<_T const>(m_data, m_count); }
  42. _T* data() const { return m_data; }
  43. /// @returns the number of elements referenced (not necessarily number of bytes).
  44. size_t count() const { return m_count; }
  45. /// @returns the number of elements referenced (not necessarily number of bytes).
  46. size_t size() const { return m_count; }
  47. bool empty() const { return !m_count; }
  48. /// @returns a new vector_ref pointing at the next chunk of @a size() elements.
  49. vector_ref<_T> next() const { if (!m_data) return *this; else return vector_ref<_T>(m_data + m_count, m_count); }
  50. /// @returns a new vector_ref which is a shifted and shortened view of the original data.
  51. /// If this goes out of bounds in any way, returns an empty vector_ref.
  52. /// If @a _count is ~size_t(0), extends the view to the end of the data.
  53. vector_ref<_T> cropped(size_t _begin, size_t _count) const { if (m_data && _begin <= m_count && _count <= m_count && _begin + _count <= m_count) return vector_ref<_T>(m_data + _begin, _count == ~size_t(0) ? m_count - _begin : _count); else return vector_ref<_T>(); }
  54. /// @returns a new vector_ref which is a shifted view of the original data (not going beyond it).
  55. vector_ref<_T> cropped(size_t _begin) const { if (m_data && _begin <= m_count) return vector_ref<_T>(m_data + _begin, m_count - _begin); else return vector_ref<_T>(); }
  56. void retarget(_T* _d, size_t _s) { m_data = _d; m_count = _s; }
  57. void retarget(std::vector<_T> const& _t) { m_data = _t.data(); m_count = _t.size(); }
  58. template <class T> bool overlapsWith(vector_ref<T> _t) const { void const* f1 = data(); void const* t1 = data() + size(); void const* f2 = _t.data(); void const* t2 = _t.data() + _t.size(); return f1 < t2 && t1 > f2; }
  59. /// Copies the contents of this vector_ref to the contents of @a _t, up to the max size of @a _t.
  60. void copyTo(vector_ref<typename std::remove_const<_T>::type> _t) const { if (overlapsWith(_t)) memmove(_t.data(), m_data, std::min(_t.size(), m_count) * sizeof(_T)); else memcpy(_t.data(), m_data, std::min(_t.size(), m_count) * sizeof(_T)); }
  61. /// Copies the contents of this vector_ref to the contents of @a _t, and zeros further trailing elements in @a _t.
  62. void populate(vector_ref<typename std::remove_const<_T>::type> _t) const { copyTo(_t); memset(_t.data() + m_count, 0, std::max(_t.size(), m_count) - m_count); }
  63. /// Securely overwrite the memory.
  64. /// @note adapted from OpenSSL's implementation.
  65. void cleanse()
  66. {
  67. static unsigned char s_cleanseCounter = 0;
  68. uint8_t* p = (uint8_t*)begin();
  69. size_t const len = (uint8_t*)end() - p;
  70. size_t loop = len;
  71. size_t count = s_cleanseCounter;
  72. while (loop--)
  73. {
  74. *(p++) = (uint8_t)count;
  75. count += (17 + ((size_t)p & 0xf));
  76. }
  77. p = (uint8_t*)memchr((uint8_t*)begin(), (uint8_t)count, len);
  78. if (p)
  79. count += (63 + (size_t)p);
  80. s_cleanseCounter = (uint8_t)count;
  81. memset((uint8_t*)begin(), 0, len);
  82. }
  83. _T* begin() { return m_data; }
  84. _T* end() { return m_data + m_count; }
  85. _T const* begin() const { return m_data; }
  86. _T const* end() const { return m_data + m_count; }
  87. _T& operator[](size_t _i) { assert(m_data); assert(_i < m_count); return m_data[_i]; }
  88. _T const& operator[](size_t _i) const { assert(m_data); assert(_i < m_count); return m_data[_i]; }
  89. bool operator==(vector_ref<_T> const& _cmp) const { return m_data == _cmp.m_data && m_count == _cmp.m_count; }
  90. bool operator!=(vector_ref<_T> const& _cmp) const { return !operator==(_cmp); }
  91. #if DEV_LDB
  92. operator ldb::Slice() const { return ldb::Slice((char const*)m_data, m_count * sizeof(_T)); }
  93. #endif
  94. void reset() { m_data = nullptr; m_count = 0; }
  95. private:
  96. _T* m_data;
  97. size_t m_count;
  98. };
  99. template<class _T> vector_ref<_T const> ref(_T const& _t) { return vector_ref<_T const>(&_t, 1); }
  100. template<class _T> vector_ref<_T> ref(_T& _t) { return vector_ref<_T>(&_t, 1); }
  101. template<class _T> vector_ref<_T const> ref(std::vector<_T> const& _t) { return vector_ref<_T const>(&_t); }
  102. template<class _T> vector_ref<_T> ref(std::vector<_T>& _t) { return vector_ref<_T>(&_t); }
  103. }