This repository was archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathRLP.cpp
More file actions
378 lines (353 loc) · 9.46 KB
/
RLP.cpp
File metadata and controls
378 lines (353 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file RLP.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include "RLP.h"
using namespace std;
using namespace dev;
bytes dev::RLPNull = rlp("");
bytes dev::RLPEmptyList = rlpList();
RLP::RLP(bytesConstRef _d, Strictness _s):
m_data(_d)
{
if ((_s & FailIfTooBig) && actualSize() < _d.size())
{
if (_s & ThrowOnFail)
BOOST_THROW_EXCEPTION(OversizeRLP());
else
m_data.reset();
}
if ((_s & FailIfTooSmall) && actualSize() > _d.size())
{
if (_s & ThrowOnFail)
BOOST_THROW_EXCEPTION(UndersizeRLP());
else
m_data.reset();
}
}
RLP::iterator& RLP::iterator::operator++()
{
if (m_remaining)
{
m_currentItem.retarget(m_currentItem.next().data(), m_remaining);
m_currentItem = m_currentItem.cropped(0, sizeAsEncoded(m_currentItem));
m_remaining -= std::min<size_t>(m_remaining, m_currentItem.size());
}
else
m_currentItem.retarget(m_currentItem.next().data(), 0);
return *this;
}
RLP::iterator::iterator(RLP const& _parent, bool _begin)
{
if (_begin && _parent.isList())
{
auto pl = _parent.payload();
m_currentItem = pl.cropped(0, sizeAsEncoded(pl));
m_remaining = pl.size() - m_currentItem.size();
}
else
{
m_currentItem = _parent.data().cropped(_parent.data().size());
m_remaining = 0;
}
}
RLP RLP::operator[](size_t _i) const
{
if (_i < m_lastIndex)
{
m_lastEnd = sizeAsEncoded(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, sizeAsEncoded(m_lastItem));
m_lastEnd += m_lastItem.size();
}
return RLP(m_lastItem, ThrowOnFail | FailIfTooSmall);
}
RLPs RLP::toList(int _flags) const
{
RLPs ret;
if (!isList())
{
if (_flags & ThrowOnFail)
BOOST_THROW_EXCEPTION(BadCast());
else
return ret;
}
for (auto const& i: *this)
ret.push_back(i);
return ret;
}
size_t RLP::actualSize() const
{
if (isNull())
return 0;
if (isSingleByte())
return 1;
if (isData() || isList())
return payloadOffset() + length();
return 0;
}
void RLP::requireGood() const
{
if (isNull())
BOOST_THROW_EXCEPTION(BadRLP());
byte n = m_data[0];
if (n != c_rlpDataImmLenStart + 1)
return;
if (m_data.size() < 2)
BOOST_THROW_EXCEPTION(BadRLP());
if (m_data[1] < c_rlpDataImmLenStart)
BOOST_THROW_EXCEPTION(BadRLP());
}
bool RLP::isInt() const
{
if (isNull())
return false;
requireGood();
byte n = m_data[0];
if (n < c_rlpDataImmLenStart)
return !!n;
else if (n == c_rlpDataImmLenStart)
return true;
else if (n <= c_rlpDataIndLenZero)
{
if (m_data.size() <= 1)
BOOST_THROW_EXCEPTION(BadRLP());
return m_data[1] != 0;
}
else if (n < c_rlpListStart)
{
if (m_data.size() <= size_t(1 + n - c_rlpDataIndLenZero))
BOOST_THROW_EXCEPTION(BadRLP());
return m_data[1 + n - c_rlpDataIndLenZero] != 0;
}
else
return false;
return false;
}
size_t RLP::length() const
{
if (isNull())
return 0;
requireGood();
size_t ret = 0;
byte const n = m_data[0];
if (n < c_rlpDataImmLenStart)
return 1;
else if (n <= c_rlpDataIndLenZero)
return n - c_rlpDataImmLenStart;
else if (n < c_rlpListStart)
{
if (m_data.size() <= size_t(n - c_rlpDataIndLenZero))
BOOST_THROW_EXCEPTION(BadRLP());
if (m_data.size() > 1)
if (m_data[1] == 0)
BOOST_THROW_EXCEPTION(BadRLP());
unsigned lengthSize = n - c_rlpDataIndLenZero;
if (lengthSize > sizeof(ret))
// We did not check, but would most probably not fit in our memory.
BOOST_THROW_EXCEPTION(UndersizeRLP());
// No leading zeroes.
if (!m_data[1])
BOOST_THROW_EXCEPTION(BadRLP());
for (unsigned i = 0; i < lengthSize; ++i)
ret = (ret << 8) | m_data[i + 1];
// Must be greater than the limit.
if (ret < c_rlpListStart - c_rlpDataImmLenStart - c_rlpMaxLengthBytes)
BOOST_THROW_EXCEPTION(BadRLP());
}
else if (n <= c_rlpListIndLenZero)
return n - c_rlpListStart;
else
{
unsigned lengthSize = n - c_rlpListIndLenZero;
if (m_data.size() <= lengthSize)
BOOST_THROW_EXCEPTION(BadRLP());
if (m_data.size() > 1)
if (m_data[1] == 0)
BOOST_THROW_EXCEPTION(BadRLP());
if (lengthSize > sizeof(ret))
// We did not check, but would most probably not fit in our memory.
BOOST_THROW_EXCEPTION(UndersizeRLP());
if (!m_data[1])
BOOST_THROW_EXCEPTION(BadRLP());
for (unsigned i = 0; i < lengthSize; ++i)
ret = (ret << 8) | m_data[i + 1];
if (ret < 0x100 - c_rlpListStart - c_rlpMaxLengthBytes)
BOOST_THROW_EXCEPTION(BadRLP());
}
// We have to be able to add payloadOffset to length without overflow.
// This rejects roughly 4GB-sized RLPs on some platforms.
if (ret >= std::numeric_limits<size_t>::max() - 0x100)
BOOST_THROW_EXCEPTION(UndersizeRLP());
return ret;
}
size_t RLP::items() const
{
if (isList())
{
bytesConstRef d = payload();
size_t i = 0;
for (; d.size(); ++i)
d = d.cropped(sizeAsEncoded(d));
return i;
}
return 0;
}
RLPStream& RLPStream::appendRaw(bytesConstRef _s, size_t _itemCount)
{
m_out.insert(m_out.end(), _s.begin(), _s.end());
noteAppended(_itemCount);
return *this;
}
void RLPStream::noteAppended(size_t _itemCount)
{
if (!_itemCount)
return;
// cdebug << "noteAppended(" << _itemCount << ")";
while (m_listStack.size())
{
if (m_listStack.back().first < _itemCount)
BOOST_THROW_EXCEPTION(RLPException() << errinfo_comment("itemCount too large") << RequirementError((bigint)m_listStack.back().first, (bigint)_itemCount));
m_listStack.back().first -= _itemCount;
if (m_listStack.back().first)
break;
else
{
auto p = m_listStack.back().second;
m_listStack.pop_back();
size_t s = m_out.size() - p; // list size
auto brs = bytesRequired(s);
unsigned encodeSize = s < c_rlpListImmLenCount ? 1 : (1 + brs);
// cdebug << "s: " << s << ", p: " << p << ", m_out.size(): " << m_out.size() << ", encodeSize: " << encodeSize << " (br: " << brs << ")";
auto os = m_out.size();
m_out.resize(os + encodeSize);
memmove(m_out.data() + p + encodeSize, m_out.data() + p, os - p);
if (s < c_rlpListImmLenCount)
m_out[p] = (byte)(c_rlpListStart + s);
else if (c_rlpListIndLenZero + brs <= 0xff)
{
m_out[p] = (byte)(c_rlpListIndLenZero + brs);
byte* b = &(m_out[p + brs]);
for (; s; s >>= 8)
*(b--) = (byte)s;
}
else
BOOST_THROW_EXCEPTION(RLPException() << errinfo_comment("itemCount too large for RLP"));
}
_itemCount = 1; // for all following iterations, we've effectively appended a single item only since we completed a list.
}
}
RLPStream& RLPStream::appendList(size_t _items)
{
// cdebug << "appendList(" << _items << ")";
if (_items)
m_listStack.push_back(std::make_pair(_items, m_out.size()));
else
appendList(bytes());
return *this;
}
RLPStream& RLPStream::appendList(bytesConstRef _rlp)
{
if (_rlp.size() < c_rlpListImmLenCount)
m_out.push_back((byte)(_rlp.size() + c_rlpListStart));
else
pushCount(_rlp.size(), c_rlpListIndLenZero);
appendRaw(_rlp, 1);
return *this;
}
RLPStream& RLPStream::append(bytesConstRef _s, bool _compact)
{
size_t s = _s.size();
byte const* d = _s.data();
if (_compact)
for (size_t i = 0; i < _s.size() && !*d; ++i, --s, ++d) {}
if (s == 1 && *d < c_rlpDataImmLenStart)
m_out.push_back(*d);
else
{
if (s < c_rlpDataImmLenCount)
m_out.push_back((byte)(s + c_rlpDataImmLenStart));
else
pushCount(s, c_rlpDataIndLenZero);
appendRaw(bytesConstRef(d, s), 0);
}
noteAppended();
return *this;
}
RLPStream& RLPStream::append(bigint _i)
{
if (!_i)
m_out.push_back(c_rlpDataImmLenStart);
else if (_i < c_rlpDataImmLenStart)
m_out.push_back((byte)_i);
else
{
unsigned br = bytesRequired(_i);
if (br < c_rlpDataImmLenCount)
m_out.push_back((byte)(br + c_rlpDataImmLenStart));
else
{
auto brbr = bytesRequired(br);
if (c_rlpDataIndLenZero + brbr > 0xff)
BOOST_THROW_EXCEPTION(RLPException() << errinfo_comment("Number too large for RLP"));
m_out.push_back((byte)(c_rlpDataIndLenZero + brbr));
pushInt(br, brbr);
}
pushInt(_i, br);
}
noteAppended();
return *this;
}
void RLPStream::pushCount(size_t _count, byte _base)
{
auto br = bytesRequired(_count);
if (int(br) + _base > 0xff)
BOOST_THROW_EXCEPTION(RLPException() << errinfo_comment("Count too large for RLP"));
m_out.push_back((byte)(br + _base)); // max 8 bytes.
pushInt(_count, br);
}
static void streamOut(std::ostream& _out, dev::RLP const& _d, unsigned _depth = 0)
{
if (_depth > 64)
_out << "<max-depth-reached>";
else if (_d.isNull())
_out << "null";
else if (_d.isInt())
_out << std::showbase << std::hex << std::nouppercase << _d.toInt<bigint>(RLP::LaissezFaire) << dec;
else if (_d.isData())
_out << escaped(_d.toString(), false);
else if (_d.isList())
{
_out << "[";
int j = 0;
for (auto i: _d)
{
_out << (j++ ? ", " : " ");
streamOut(_out, i, _depth + 1);
}
_out << " ]";
}
}
std::ostream& dev::operator<<(std::ostream& _out, RLP const& _d)
{
streamOut(_out, _d);
return _out;
}