LCOV - code coverage report
Current view: top level - src/jamidht/eth/libdevcore - vector_ref.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 25 49 51.0 %
Date: 2021-07-24 07:34:39 Functions: 12 18 66.7 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <cstring>
       4             : #include <cassert>
       5             : #include <type_traits>
       6             : #include <vector>
       7             : #include <string>
       8             : #include <atomic>
       9             : 
      10             : #ifdef __INTEL_COMPILER
      11             : #pragma warning(disable : 597) // will not be called for implicit or explicit conversions
      12             : #endif
      13             : 
      14             : namespace dev {
      15             : 
      16             : /**
      17             :  * A modifiable reference to an existing object or vector in memory.
      18             :  */
      19             : template<class _T>
      20             : class vector_ref
      21             : {
      22             : public:
      23             :     using value_type = _T;
      24             :     using element_type = _T;
      25             :     using mutable_value_type = typename std::
      26             :         conditional<std::is_const<_T>::value, typename std::remove_const<_T>::type, _T>::type;
      27             : 
      28             :     static_assert(std::is_pod<value_type>::value,
      29             :                   "vector_ref can only be used with PODs due to its low-level treatment of data.");
      30             : 
      31          30 :     vector_ref()
      32             :         : m_data(nullptr)
      33          30 :         , m_count(0)
      34          30 :     {}
      35             :     /// Creates a new vector_ref to point to @a _count elements starting at @a _data.
      36        3747 :     vector_ref(_T* _data, size_t _count)
      37             :         : m_data(_data)
      38        3747 :         , m_count(_count)
      39        3747 :     {}
      40             :     /// Creates a new vector_ref pointing to the data part of a string (given as pointer).
      41             :     vector_ref(
      42             :         typename std::conditional<std::is_const<_T>::value, std::string const*, std::string*>::type
      43             :             _data)
      44             :         : m_data(reinterpret_cast<_T*>(_data->data()))
      45             :         , m_count(_data->size() / sizeof(_T))
      46             :     {}
      47             :     /// Creates a new vector_ref pointing to the data part of a vector (given as pointer).
      48           0 :     vector_ref(typename std::conditional<std::is_const<_T>::value,
      49             :                                          std::vector<typename std::remove_const<_T>::type> const*,
      50             :                                          std::vector<_T>*>::type _data)
      51             :         : m_data(_data->data())
      52           0 :         , m_count(_data->size())
      53           0 :     {}
      54             :     /// Creates a new vector_ref pointing to the data part of a string (given as reference).
      55             :     vector_ref(
      56             :         typename std::conditional<std::is_const<_T>::value, std::string const&, std::string&>::type
      57             :             _data)
      58             :         : m_data(reinterpret_cast<_T*>(_data.data()))
      59             :         , m_count(_data.size() / sizeof(_T))
      60             :     {}
      61             : #if DEV_LDB
      62             :     vector_ref(ldb::Slice const& _s)
      63             :         : m_data(reinterpret_cast<_T*>(_s.data()))
      64             :         , m_count(_s.size() / sizeof(_T))
      65             :     {}
      66             : #endif
      67             :     explicit operator bool() const { return m_data && m_count; }
      68             : 
      69             :     bool contentsEqual(std::vector<mutable_value_type> const& _c) const
      70             :     {
      71             :         if (!m_data || m_count == 0)
      72             :             return _c.empty();
      73             :         else
      74             :             return _c.size() == m_count && !memcmp(_c.data(), m_data, m_count * sizeof(_T));
      75             :     }
      76             :     std::vector<mutable_value_type> toVector() const
      77             :     {
      78             :         return std::vector<mutable_value_type>(m_data, m_data + m_count);
      79             :     }
      80           0 :     std::vector<unsigned char> toBytes() const
      81             :     {
      82           0 :         return std::vector<unsigned char>(reinterpret_cast<unsigned char const*>(m_data),
      83           0 :                                           reinterpret_cast<unsigned char const*>(m_data)
      84           0 :                                               + m_count * sizeof(_T));
      85             :     }
      86             :     std::string toString() const
      87             :     {
      88             :         return std::string((char const*) m_data, ((char const*) m_data) + m_count * sizeof(_T));
      89             :     }
      90             : 
      91             :     template<class _T2>
      92             :     explicit operator vector_ref<_T2>() const
      93             :     {
      94             :         assert(m_count * sizeof(_T) / sizeof(_T2) * sizeof(_T2) / sizeof(_T) == m_count);
      95             :         return vector_ref<_T2>(reinterpret_cast<_T2*>(m_data), m_count * sizeof(_T) / sizeof(_T2));
      96             :     }
      97           0 :     operator vector_ref<_T const>() const { return vector_ref<_T const>(m_data, m_count); }
      98             : 
      99        1304 :     _T* data() const { return m_data; }
     100             :     /// @returns the number of elements referenced (not necessarily number of bytes).
     101             :     size_t count() const { return m_count; }
     102             :     /// @returns the number of elements referenced (not necessarily number of bytes).
     103        1303 :     size_t size() const { return m_count; }
     104             :     bool empty() const { return !m_count; }
     105             :     /// @returns a new vector_ref pointing at the next chunk of @a size() elements.
     106             :     vector_ref<_T> next() const
     107             :     {
     108             :         if (!m_data)
     109             :             return *this;
     110             :         else
     111             :             return vector_ref<_T>(m_data + m_count, m_count);
     112             :     }
     113             :     /// @returns a new vector_ref which is a shifted and shortened view of the original data.
     114             :     /// If this goes out of bounds in any way, returns an empty vector_ref.
     115             :     /// If @a _count is ~size_t(0), extends the view to the end of the data.
     116             :     vector_ref<_T> cropped(size_t _begin, size_t _count) const
     117             :     {
     118             :         if (m_data && _begin <= m_count && _count <= m_count && _begin + _count <= m_count)
     119             :             return vector_ref<_T>(m_data + _begin, _count == ~size_t(0) ? m_count - _begin : _count);
     120             :         else
     121             :             return vector_ref<_T>();
     122             :     }
     123             :     /// @returns a new vector_ref which is a shifted view of the original data (not going beyond it).
     124             :     vector_ref<_T> cropped(size_t _begin) const
     125             :     {
     126             :         if (m_data && _begin <= m_count)
     127             :             return vector_ref<_T>(m_data + _begin, m_count - _begin);
     128             :         else
     129             :             return vector_ref<_T>();
     130             :     }
     131             :     void retarget(_T* _d, size_t _s)
     132             :     {
     133             :         m_data = _d;
     134             :         m_count = _s;
     135             :     }
     136             :     void retarget(std::vector<_T> const& _t)
     137             :     {
     138             :         m_data = _t.data();
     139             :         m_count = _t.size();
     140             :     }
     141             :     template<class T>
     142           0 :     bool overlapsWith(vector_ref<T> _t) const
     143             :     {
     144           0 :         void const* f1 = data();
     145           0 :         void const* t1 = data() + size();
     146           0 :         void const* f2 = _t.data();
     147           0 :         void const* t2 = _t.data() + _t.size();
     148           0 :         return f1 < t2 && t1 > f2;
     149             :     }
     150             :     /// Copies the contents of this vector_ref to the contents of @a _t, up to the max size of @a _t.
     151           0 :     void copyTo(vector_ref<typename std::remove_const<_T>::type> _t) const
     152             :     {
     153           0 :         if (overlapsWith(_t))
     154           0 :             memmove(_t.data(), m_data, std::min(_t.size(), m_count) * sizeof(_T));
     155             :         else
     156           0 :             memcpy(_t.data(), m_data, std::min(_t.size(), m_count) * sizeof(_T));
     157           0 :     }
     158             :     /// Copies the contents of this vector_ref to the contents of @a _t, and zeros further trailing
     159             :     /// elements in @a _t.
     160           0 :     void populate(vector_ref<typename std::remove_const<_T>::type> _t) const
     161             :     {
     162           0 :         copyTo(_t);
     163           0 :         memset(_t.data() + m_count, 0, std::max(_t.size(), m_count) - m_count);
     164           0 :     }
     165             :     /// Securely overwrite the memory.
     166             :     /// @note adapted from OpenSSL's implementation.
     167        2156 :     void cleanse()
     168             :     {
     169             :         static std::atomic<unsigned char> s_cleanseCounter {0u};
     170        2156 :         uint8_t* p = (uint8_t*) begin();
     171        2156 :         size_t const len = (uint8_t*) end() - p;
     172        2156 :         size_t loop = len;
     173        2156 :         size_t count = s_cleanseCounter;
     174       71148 :         while (loop--) {
     175       68992 :             *(p++) = (uint8_t) count;
     176       68992 :             count += (17 + ((size_t) p & 0xf));
     177             :         }
     178        2156 :         p = (uint8_t*) memchr((uint8_t*) begin(), (uint8_t) count, len);
     179        2156 :         if (p)
     180           0 :             count += (63 + (size_t) p);
     181        2156 :         s_cleanseCounter = (uint8_t) count;
     182        2156 :         memset((uint8_t*) begin(), 0, len);
     183        2156 :     }
     184             : 
     185        6468 :     _T* begin() { return m_data; }
     186        2156 :     _T* end() { return m_data + m_count; }
     187         318 :     _T const* begin() const { return m_data; }
     188         318 :     _T const* end() const { return m_data + m_count; }
     189             : 
     190             :     _T& operator[](size_t _i)
     191             :     {
     192             :         assert(m_data);
     193             :         assert(_i < m_count);
     194             :         return m_data[_i];
     195             :     }
     196             :     _T const& operator[](size_t _i) const
     197             :     {
     198             :         assert(m_data);
     199             :         assert(_i < m_count);
     200             :         return m_data[_i];
     201             :     }
     202             : 
     203             :     bool operator==(vector_ref<_T> const& _cmp) const
     204             :     {
     205             :         return m_data == _cmp.m_data && m_count == _cmp.m_count;
     206             :     }
     207             :     bool operator!=(vector_ref<_T> const& _cmp) const { return !operator==(_cmp); }
     208             : 
     209             : #if DEV_LDB
     210             :     operator ldb::Slice() const { return ldb::Slice((char const*) m_data, m_count * sizeof(_T)); }
     211             : #endif
     212             : 
     213             :     void reset()
     214             :     {
     215             :         m_data = nullptr;
     216             :         m_count = 0;
     217             :     }
     218             : 
     219             : private:
     220             :     _T* m_data;
     221             :     size_t m_count;
     222             : };
     223             : 
     224             : template<class _T>
     225             : vector_ref<_T const>
     226             : ref(_T const& _t)
     227             : {
     228             :     return vector_ref<_T const>(&_t, 1);
     229             : }
     230             : template<class _T>
     231             : vector_ref<_T>
     232             : ref(_T& _t)
     233             : {
     234             :     return vector_ref<_T>(&_t, 1);
     235             : }
     236             : template<class _T>
     237             : vector_ref<_T const>
     238             : ref(std::vector<_T> const& _t)
     239             : {
     240             :     return vector_ref<_T const>(&_t);
     241             : }
     242             : template<class _T>
     243             : vector_ref<_T>
     244             : ref(std::vector<_T>& _t)
     245             : {
     246             :     return vector_ref<_T>(&_t);
     247             : }
     248             : 
     249             : } // namespace dev

Generated by: LCOV version 1.14