35 BOOST_THROW_EXCEPTION(OversizeRLP());
42 BOOST_THROW_EXCEPTION(UndersizeRLP());
52 m_currentItem.retarget(m_currentItem.next().data(), m_remaining);
53 m_currentItem = m_currentItem.cropped(0,
sizeAsEncoded(m_currentItem));
54 m_remaining -= std::min<size_t>(m_remaining, m_currentItem.size());
57 m_currentItem.retarget(m_currentItem.next().data(), 0);
63 if (_begin && _parent.
isList())
67 m_remaining = pl.
size() - m_currentItem.size();
99 BOOST_THROW_EXCEPTION(BadCast());
103 for (
auto const& i: *
this)
122 BOOST_THROW_EXCEPTION(BadRLP());
124 if (n != c_rlpDataImmLenStart + 1)
127 BOOST_THROW_EXCEPTION(BadRLP());
128 if (
m_data[1] < c_rlpDataImmLenStart)
129 BOOST_THROW_EXCEPTION(BadRLP());
138 if (n < c_rlpDataImmLenStart)
140 else if (n == c_rlpDataImmLenStart)
142 else if (n <= c_rlpDataIndLenZero)
145 BOOST_THROW_EXCEPTION(BadRLP());
148 else if (n < c_rlpListStart)
150 if (
m_data.
size() <= size_t(1 + n - c_rlpDataIndLenZero))
151 BOOST_THROW_EXCEPTION(BadRLP());
152 return m_data[1 + n - c_rlpDataIndLenZero] != 0;
166 if (n < c_rlpDataImmLenStart)
168 else if (n <= c_rlpDataIndLenZero)
169 return n - c_rlpDataImmLenStart;
170 else if (n < c_rlpListStart)
172 if (
m_data.
size() <= size_t(n - c_rlpDataIndLenZero))
173 BOOST_THROW_EXCEPTION(BadRLP());
176 BOOST_THROW_EXCEPTION(BadRLP());
177 unsigned lengthSize = n - c_rlpDataIndLenZero;
178 if (lengthSize >
sizeof(ret))
180 BOOST_THROW_EXCEPTION(UndersizeRLP());
183 BOOST_THROW_EXCEPTION(BadRLP());
185 ret = (ret << 8) |
m_data[i + 1];
187 if (ret < c_rlpListStart - c_rlpDataImmLenStart - c_rlpMaxLengthBytes)
188 BOOST_THROW_EXCEPTION(BadRLP());
190 else if (n <= c_rlpListIndLenZero)
191 return n - c_rlpListStart;
194 unsigned lengthSize = n - c_rlpListIndLenZero;
196 BOOST_THROW_EXCEPTION(BadRLP());
199 BOOST_THROW_EXCEPTION(BadRLP());
200 if (lengthSize >
sizeof(ret))
202 BOOST_THROW_EXCEPTION(UndersizeRLP());
204 BOOST_THROW_EXCEPTION(BadRLP());
206 ret = (ret << 8) |
m_data[i + 1];
207 if (ret < 0x100 - c_rlpListStart - c_rlpMaxLengthBytes)
208 BOOST_THROW_EXCEPTION(BadRLP());
213 BOOST_THROW_EXCEPTION(UndersizeRLP());
223 for (; d.
size(); ++i)
232 m_out.insert(m_out.end(), _s.
begin(), _s.
end());
233 noteAppended(_itemCount);
242 while (m_listStack.size())
244 if (m_listStack.back().first < _itemCount)
246 m_listStack.back().first -= _itemCount;
247 if (m_listStack.back().first)
251 auto p = m_listStack.back().second;
252 m_listStack.pop_back();
253 size_t s = m_out.size() - p;
255 unsigned encodeSize = s < c_rlpListImmLenCount ? 1 : (1 + brs);
257 auto os = m_out.size();
258 m_out.resize(os + encodeSize);
259 memmove(m_out.data() + p + encodeSize, m_out.data() + p, os - p);
260 if (s < c_rlpListImmLenCount)
261 m_out[p] = (
byte)(c_rlpListStart + s);
262 else if (c_rlpListIndLenZero + brs <= 0xff)
264 m_out[p] = (
byte)(c_rlpListIndLenZero + brs);
265 byte*
b = &(m_out[p + brs]);
280 m_listStack.push_back(std::make_pair(_items, m_out.size()));
288 if (_rlp.
size() < c_rlpListImmLenCount)
289 m_out.push_back((
byte)(_rlp.
size() + c_rlpListStart));
291 pushCount(_rlp.
size(), c_rlpListIndLenZero);
298 size_t s = _s.
size();
301 for (
size_t i = 0; i < _s.
size() && !*
d; ++i, --s, ++
d) {}
303 if (s == 1 && *d < c_rlpDataImmLenStart)
307 if (s < c_rlpDataImmLenCount)
308 m_out.push_back((
byte)(s + c_rlpDataImmLenStart));
310 pushCount(s, c_rlpDataIndLenZero);
320 m_out.push_back(c_rlpDataImmLenStart);
321 else if (_i < c_rlpDataImmLenStart)
322 m_out.push_back((
byte)_i);
326 if (br < c_rlpDataImmLenCount)
327 m_out.push_back((
byte)(br + c_rlpDataImmLenStart));
331 if (c_rlpDataIndLenZero + brbr > 0xff)
333 m_out.push_back((
byte)(c_rlpDataIndLenZero + brbr));
345 if (
int(br) + _base > 0xff)
347 m_out.push_back((
byte)(br + _base));
351 static void streamOut(std::ostream& _out,
dev::RLP const& _d,
unsigned _depth = 0)
354 _out <<
"<max-depth-reached>";
367 _out << (j++ ?
", " :
" ");
368 streamOut(_out, i, _depth + 1);
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
bool isNull() const
No value.
bool isInt() const
Integer value. Must not have a leading zero.
bool isList() const
List value.
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
bytesConstRef data() const
The bare data of the RLP.
RLPStream & append(unsigned _s)
Append given datum to the byte stream.
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
void requireGood() const
Throws if is non-canonical data (i.e. single byte done in two bytes that could be done in one)...
RLP operator[](size_t _i) const
Subscript operator.
unsigned bytesRequired(T _i)
Determine bytes required to encode the given integer value.
std::hash for asio::adress
bool isData() const
String value.
void pushCount(size_t _count, byte _offset)
Push the node-type byte (using _base) along with the item count _count.
vector_ref< _T > cropped(size_t _begin, size_t _count) const
RLP()
Construct a null node.
std::string escaped(std::string const &_s, bool _all=true)
Escapes a string into the C-string representation.
static size_t sizeAsEncoded(bytesConstRef _data)
bytes RLPNull
The empty string in RLP format.
unsigned lengthSize() const
bytesConstRef payload() const
std::vector< byte > bytes
vector_ref< byte const > bytesConstRef
RLPStream & appendList(size_t _items)
Appends a list.
bytesConstRef m_data
Our byte data.
void noteAppended(size_t _itemCount=1)
RLPs toList(int _flags=Strict) const
Converts to RLPs collection object. Useful if you need random access to sub items or will iterate ove...
bytes rlpList()
Export a list of items in RLP format, returning a byte array.
std::ostream & operator<<(std::ostream &_out, bytes const &_e)
bool isSingleByte() const
Single-byte data payload.
Base class for all RLP exceptions.
size_t actualSize() const
boost::error_info< struct tag_comment, std::string > errinfo_comment
Iterator class for iterating through items of RLP list.
size_t m_lastIndex
The list-indexing cache.
std::string toString(int _flags=LaissezFaire) const
Converts to string.
size_t payloadOffset() const
boost::tuple< errinfo_required, errinfo_got > RequirementError
_T toInt(int _flags=Strict) const
Converts to int of type given; if isString(), decodes as big-endian bytestream.
void * memmove(void *a, const void *b, size_t c)
Class for writing to an RLP bytestream.
RLPStream & appendRaw(bytesConstRef _rlp, size_t _itemCount=1)
Appends raw (pre-serialised) RLP data. Use with caution.
Class for interpreting Recursive Linear-Prefix Data.
bytes RLPEmptyList
The empty list in RLP format.