From b3dfe07c2f687779097454b8eaeb6c67e35cb3e9 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 2 Jul 2015 17:37:15 +0200 Subject: [PATCH] Helper to find the length of an RLP item. --- libdevcore/RLP.cpp | 10 +++++----- libdevcore/RLP.h | 5 ++++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/libdevcore/RLP.cpp b/libdevcore/RLP.cpp index cf4218050..960843f4c 100644 --- a/libdevcore/RLP.cpp +++ b/libdevcore/RLP.cpp @@ -50,7 +50,7 @@ RLP::iterator& RLP::iterator::operator++() if (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(m_remaining, m_currentItem.size()); } else @@ -63,7 +63,7 @@ RLP::iterator::iterator(RLP const& _parent, bool _begin) if (_begin && _parent.isList()) { 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(); } else @@ -77,14 +77,14 @@ RLP RLP::operator[](size_t _i) const { if (_i < m_lastIndex) { - m_lastEnd = RLP(payload(), ThrowOnFail | FailIfTooSmall).actualSize(); + m_lastEnd = actualSizeOfPrefix(payload()); m_lastItem = payload().cropped(0, m_lastEnd); m_lastIndex = 0; } for (; m_lastIndex < _i && m_lastItem.size(); ++m_lastIndex) { 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(); } return RLP(m_lastItem, ThrowOnFail | FailIfTooSmall); @@ -204,7 +204,7 @@ size_t RLP::items() const bytesConstRef d = payload().cropped(0, length()); size_t i = 0; for (; d.size(); ++i) - d = d.cropped(RLP(d, ThrowOnFail | FailIfTooSmall).actualSize()); + d = d.cropped(actualSizeOfPrefix(d)); return i; } return 0; diff --git a/libdevcore/RLP.h b/libdevcore/RLP.h index 1586616d8..e124ea2b6 100644 --- a/libdevcore/RLP.h +++ b/libdevcore/RLP.h @@ -301,7 +301,7 @@ public: /// @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); } - /// @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. size_t actualSize() const; @@ -327,6 +327,9 @@ private: /// @returns the number of data items. 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. bytesConstRef m_data;