Browse Source

Helper to find the length of an RLP item.

cl-refactor
chriseth 10 years ago
parent
commit
b3dfe07c2f
  1. 10
      libdevcore/RLP.cpp
  2. 5
      libdevcore/RLP.h

10
libdevcore/RLP.cpp

@ -50,7 +50,7 @@ RLP::iterator& RLP::iterator::operator++()
if (m_remaining) if (m_remaining)
{ {
m_currentItem.retarget(m_currentItem.next().data(), m_remaining); m_currentItem.retarget(m_currentItem.next().data(), m_remaining);
m_currentItem = m_currentItem.cropped(0, RLP(m_currentItem, ThrowOnFail | FailIfTooSmall).actualSize()); m_currentItem = m_currentItem.cropped(0, actualSizeOfPrefix(m_currentItem));
m_remaining -= std::min<size_t>(m_remaining, m_currentItem.size()); m_remaining -= std::min<size_t>(m_remaining, m_currentItem.size());
} }
else else
@ -63,7 +63,7 @@ RLP::iterator::iterator(RLP const& _parent, bool _begin)
if (_begin && _parent.isList()) if (_begin && _parent.isList())
{ {
auto pl = _parent.payload(); auto pl = _parent.payload();
m_currentItem = pl.cropped(0, RLP(pl, ThrowOnFail | FailIfTooSmall).actualSize()); m_currentItem = pl.cropped(0, actualSizeOfPrefix(pl));
m_remaining = pl.size() - m_currentItem.size(); m_remaining = pl.size() - m_currentItem.size();
} }
else else
@ -77,14 +77,14 @@ RLP RLP::operator[](size_t _i) const
{ {
if (_i < m_lastIndex) if (_i < m_lastIndex)
{ {
m_lastEnd = RLP(payload(), ThrowOnFail | FailIfTooSmall).actualSize(); m_lastEnd = actualSizeOfPrefix(payload());
m_lastItem = payload().cropped(0, m_lastEnd); m_lastItem = payload().cropped(0, m_lastEnd);
m_lastIndex = 0; m_lastIndex = 0;
} }
for (; m_lastIndex < _i && m_lastItem.size(); ++m_lastIndex) for (; m_lastIndex < _i && m_lastItem.size(); ++m_lastIndex)
{ {
m_lastItem = payload().cropped(m_lastEnd); m_lastItem = payload().cropped(m_lastEnd);
m_lastItem = m_lastItem.cropped(0, RLP(m_lastItem, ThrowOnFail | FailIfTooSmall).actualSize()); m_lastItem = m_lastItem.cropped(0, actualSizeOfPrefix(m_lastItem));
m_lastEnd += m_lastItem.size(); m_lastEnd += m_lastItem.size();
} }
return RLP(m_lastItem, ThrowOnFail | FailIfTooSmall); return RLP(m_lastItem, ThrowOnFail | FailIfTooSmall);
@ -204,7 +204,7 @@ size_t RLP::items() const
bytesConstRef d = payload().cropped(0, length()); bytesConstRef d = payload().cropped(0, length());
size_t i = 0; size_t i = 0;
for (; d.size(); ++i) for (; d.size(); ++i)
d = d.cropped(RLP(d, ThrowOnFail | FailIfTooSmall).actualSize()); d = d.cropped(actualSizeOfPrefix(d));
return i; return i;
} }
return 0; return 0;

5
libdevcore/RLP.h

@ -301,7 +301,7 @@ public:
/// @returns the data payload. Valid for all types. /// @returns the data payload. Valid for all types.
bytesConstRef payload() const { auto l = length(); if (l > m_data.size()) BOOST_THROW_EXCEPTION(BadRLP()); return m_data.cropped(payloadOffset(), l); } bytesConstRef payload() const { auto l = length(); if (l > m_data.size()) BOOST_THROW_EXCEPTION(BadRLP()); return m_data.cropped(payloadOffset(), l); }
/// @returns the theoretical size of this item. /// @returns the theoretical size of this item as encoded in the data.
/// @note Under normal circumstances, is equivalent to m_data.size() - use that unless you know it won't work. /// @note Under normal circumstances, is equivalent to m_data.size() - use that unless you know it won't work.
size_t actualSize() const; size_t actualSize() const;
@ -327,6 +327,9 @@ private:
/// @returns the number of data items. /// @returns the number of data items.
size_t items() const; size_t items() const;
/// @returns the "actualSize" of the RLP encoded in the largest prefix of @a _data and throws on error.
static size_t actualSizeOfPrefix(bytesConstRef _data) { return RLP(_data, ThrowOnFail | FailIfTooSmall).actualSize(); }
/// Our byte data. /// Our byte data.
bytesConstRef m_data; bytesConstRef m_data;

Loading…
Cancel
Save