xrpld
Loading...
Searching...
No Matches
Serializer.h
1#pragma once
2
3#include <xrpl/basics/Blob.h>
4#include <xrpl/basics/Buffer.h>
5#include <xrpl/basics/Slice.h>
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/basics/contract.h>
8#include <xrpl/basics/safe_cast.h>
9#include <xrpl/beast/utility/instrumentation.h>
10#include <xrpl/protocol/HashPrefix.h>
11#include <xrpl/protocol/SField.h>
12
13#include <cstddef>
14#include <cstdint>
15#include <cstring>
16#include <stdexcept>
17#include <string>
18#include <type_traits>
19
20namespace xrpl {
21
23{
24private:
25 // DEPRECATED
27
28public:
33 static constexpr int kMaxNumberOfBytesInHeader = 3;
34
35 // A field whose size varies is stored as a header holding its length, then
36 // the field data. The header is 1, 2 or 3 bytes long. Nothing outside it says
37 // which, so the decoder reads the first byte and its value says how long the
38 // header is:
39 //
40 // 0 ... 192 kMin/kMaxValueOfFirstByteFor1ByteHeader
41 // 193 ... 240 kMin/kMaxValueOfFirstByteFor2ByteHeader
42 // 241 ... 254 kMin/kMaxValueOfFirstByteFor3ByteHeader
43 // 255 belongs to no header
44 //
45 // Each range starts one past the end of the range before it.
46
47 static constexpr int kMinValueOfFirstByteFor1ByteHeader = 0;
48 static constexpr int kMaxValueOfFirstByteFor1ByteHeader = 192;
49
52 static constexpr int kMaxValueOfFirstByteFor2ByteHeader = 240;
53
56
57 static constexpr int kMaxValueOfFirstByteFor3ByteHeader = 254;
58
59 // A length x too big for one byte is split across the header. For 2 bytes:
60 //
61 // first byte = 193 + (x - 193) / 256
62 // second byte = (x - 193) % 256
63 //
64 // so 300 is stored as 193, 107. For 3 bytes it is the same, from 241, with
65 // the remainder split across two bytes: 20,000 is stored as 241, 29, 95.
66
67 static constexpr int kNumberOfValuesInOneByte = 256;
68 static constexpr int kNumberOfValuesInTwoBytes =
70
71 // Each header length therefore covers a range of field lengths:
72 //
73 // 0 ... 192 kMin/kMaxValueOfLengthFor1ByteHeader
74 // 193 ... 12,480 kMin/kMaxValueOfLengthFor2ByteHeader
75 // 12,481 ... 918,744 kMin/kMaxValueOfLengthFor3ByteHeader
76 //
77 // The encoder always uses the shortest header that fits.
78
85
87
96 1;
97
99
111 1;
112
118 static constexpr int kMaxValueOfLengthFor3ByteHeader = 918744;
119
120 static_assert(
122 "a length the encoder writes must be one a header can state");
123
124 explicit Serializer(int n = 256)
125 {
126 data_.reserve(n);
127 }
128
130 {
131 data_.resize(size);
132
133 if (size != 0u)
134 {
135 XRPL_ASSERT(data, "xrpl::Serializer::Serializer(void const*) : non-null input");
136 std::memcpy(data_.data(), data, size);
137 }
138 }
139
140 [[nodiscard]] Slice
141 slice() const noexcept
142 {
143 return Slice(data_.data(), data_.size());
144 }
145
146 [[nodiscard]] std::size_t
147 size() const noexcept
148 {
149 return data_.size();
150 }
151
152 [[nodiscard]] void const*
153 data() const noexcept
154 {
155 return data_.data();
156 }
157
158 // assemble functions
159 int
160 add8(unsigned char byteValue);
161 int
163
164 template <typename T>
166 int
167 add32(T i)
168 {
169 int const ret = data_.size();
170 data_.push_back(static_cast<unsigned char>((i >> 24) & 0xff));
171 data_.push_back(static_cast<unsigned char>((i >> 16) & 0xff));
172 data_.push_back(static_cast<unsigned char>((i >> 8) & 0xff));
173 data_.push_back(static_cast<unsigned char>(i & 0xff));
174 return ret;
175 }
176
177 int
178 add32(HashPrefix p);
179
180 template <typename T>
182 int
183 add64(T i)
184 {
185 int const ret = data_.size();
186 data_.push_back(static_cast<unsigned char>((i >> 56) & 0xff));
187 data_.push_back(static_cast<unsigned char>((i >> 48) & 0xff));
188 data_.push_back(static_cast<unsigned char>((i >> 40) & 0xff));
189 data_.push_back(static_cast<unsigned char>((i >> 32) & 0xff));
190 data_.push_back(static_cast<unsigned char>((i >> 24) & 0xff));
191 data_.push_back(static_cast<unsigned char>((i >> 16) & 0xff));
192 data_.push_back(static_cast<unsigned char>((i >> 8) & 0xff));
193 data_.push_back(static_cast<unsigned char>(i & 0xff));
194 return ret;
195 }
196
197 template <typename Integer>
198 int addInteger(Integer);
199
200 template <std::size_t Bits, class Tag>
201 int
203 {
204 return addRaw(v.data(), v.size());
205 }
206
207 int
208 addRaw(Blob const& vector);
209 int
211 int
212 addRaw(void const* ptr, int len);
213 int
214 addRaw(Serializer const& s);
215
216 int
217 addVL(Blob const& vector);
218 int
219 addVL(Slice const& slice);
220 template <class Iter>
221 int
222 addVL(Iter begin, Iter end, int len);
223 int
224 addVL(void const* ptr, int len);
225
226 // disassemble functions
227 bool
228 get8(int&, int offset) const;
229
230 template <typename Integer>
231 bool
232 getInteger(Integer& number, int offset)
233 {
234 static auto const kBytes = sizeof(Integer);
235 if ((offset + kBytes) > data_.size())
236 return false;
237 number = 0;
238
239 auto ptr = &data_[offset];
240 for (auto i = 0; i < kBytes; ++i)
241 {
242 if (i)
243 number <<= 8;
244 number |= *ptr++;
245 }
246 return true;
247 }
248
249 template <std::size_t Bits, typename Tag = void>
250 bool
252 {
253 auto success = (offset + (Bits / 8)) <= data_.size();
254 if (success)
255 memcpy(data.begin(), &(data_.front()) + offset, (Bits / 8));
256 return success;
257 }
258
259 int
260 addFieldID(int type, int name);
261 int
263 {
264 return addFieldID(safeCast<int>(type), name);
265 }
266
267 // DEPRECATED
268 [[nodiscard]] UInt256
269 getSHA512Half() const;
270
271 // totality functions
272 [[nodiscard]] Blob const&
273 peekData() const
274 {
275 return data_;
276 }
277 [[nodiscard]] Blob
278 getData() const
279 {
280 return data_;
281 }
282 Blob&
284 {
285 return data_;
286 }
287
288 [[nodiscard]] int
290 {
291 return data_.size();
292 }
293 [[nodiscard]] void const*
295 {
296 return data_.data();
297 }
298 void*
300 {
301 return data_.data();
302 }
303 [[nodiscard]] int
304 getLength() const
305 {
306 return data_.size();
307 }
308 [[nodiscard]] std::string
309 getString() const
310 {
311 return std::string(static_cast<char const*>(getDataPtr()), size());
312 }
313 void
315 {
316 data_.clear();
317 }
318 bool
319 chop(int num);
320
321 // vector-like functions
322 Blob::iterator
324 {
325 return data_.begin();
326 }
327 Blob::iterator
329 {
330 return data_.end();
331 }
332 [[nodiscard]] Blob::const_iterator
333 begin() const
334 {
335 return data_.begin();
336 }
337 [[nodiscard]] Blob::const_iterator
338 end() const
339 {
340 return data_.end();
341 }
342 void
343 reserve(size_t n)
344 {
345 data_.reserve(n);
346 }
347 void
348 resize(size_t n)
349 {
350 data_.resize(n);
351 }
352 [[nodiscard]] size_t
353 capacity() const
354 {
355 return data_.capacity();
356 }
357
358 bool
359 operator==(Blob const& v) const
360 {
361 return v == data_;
362 }
363 bool
364 operator==(Serializer const& v) const
365 {
366 return v.data_ == data_;
367 }
368
381 static int
382 decodeLengthLength(std::byte firstByte);
383
393 static int
394 decodeVLLength(std::byte firstByte);
395
407 static int
408 decodeVLLength(std::byte firstByte, std::byte secondByte);
409
424 static int
425 decodeVLLength(std::byte firstByte, std::byte secondByte, std::byte thirdByte);
426
427private:
440 static int
441 encodeLengthLength(int length);
442
453 int
454 addEncoded(int length);
455};
456
457template <class Iter>
458int
459Serializer::addVL(Iter begin, Iter end, int len)
460{
461 int const ret = addEncoded(len);
462 for (; begin != end; ++begin)
463 {
464 addRaw(begin->data(), begin->size());
465#ifndef NDEBUG
466 len -= begin->size();
467#endif
468 }
469 XRPL_ASSERT(len == 0, "xrpl::Serializer::addVL : length matches distance");
470 return ret;
471}
472
473//------------------------------------------------------------------------------
474
475// DEPRECATED
476// Transitional adapter to new serialization interfaces
478{
479private:
483
484public:
485 SerialIter(void const* data, std::size_t size) noexcept;
486
487 SerialIter(Slice const& slice) : SerialIter(slice.data(), slice.size())
488 {
489 }
490
491 // Infer the size of the data based on the size of the passed array.
492 template <int N>
493 explicit SerialIter(std::uint8_t const (&data)[N]) : SerialIter(&data[0], N)
494 {
495 static_assert(N > 0);
496 }
497
498 [[nodiscard]] bool
499 empty() const noexcept
500 {
501 return remain_ == 0;
502 }
503
504 void
505 reset() noexcept;
506
507 [[nodiscard]] int
508 getBytesLeft() const noexcept
509 {
510 return static_cast<int>(remain_);
511 }
512
513 // get functions throw on error
514 unsigned char
515 get8();
516
518 get16();
519
521 get32();
523 geti32();
524
526 get64();
528 geti64();
529
530 template <std::size_t Bits, class Tag = void>
532 getBitString();
533
534 UInt128
536 {
537 return getBitString<128>();
538 }
539
540 UInt160
542 {
543 return getBitString<160>();
544 }
545
546 UInt192
548 {
549 return getBitString<192>();
550 }
551
552 UInt256
554 {
555 return getBitString<256>();
556 }
557
558 void
559 getFieldID(int& type, int& name);
560
570 int
572
573 Slice
574 getSlice(std::size_t bytes);
575
576 // VFALCO DEPRECATED Returns a copy
577 Blob
578 getRaw(int size);
579
580 // VFALCO DEPRECATED Returns a copy
581 Blob
582 getVL();
583
584 void
585 skip(int num);
586
587 Buffer
588 getVLBuffer();
589
590 template <class T>
591 T
592 getRawHelper(int size);
593};
594
595template <std::size_t Bits, class Tag>
596BaseUInt<Bits, Tag>
598{
599 auto const n = Bits / 8;
600
601 if (remain_ < n)
602 Throw<std::runtime_error>("invalid SerialIter getBitString");
603
604 auto const x = p_;
605
606 p_ += n;
607 used_ += n;
608 remain_ -= n;
609
611}
612
613} // namespace xrpl
Integers of any length that is a multiple of 32-bits.
Definition base_uint.h:82
static BaseUInt fromVoid(void const *data)
Definition base_uint.h:339
pointer data()
Definition base_uint.h:117
static constexpr std::size_t size()
Definition base_uint.h:548
Like std::vector<char> but better.
Definition Buffer.h:19
int getBytesLeft() const noexcept
Definition Serializer.h:508
SerialIter(Slice const &slice)
Definition Serializer.h:487
UInt256 get256()
Definition Serializer.h:553
int getVLDataLength()
Reads the length header at the read position and steps past it.
BaseUInt< Bits, Tag > getBitString()
Definition Serializer.h:597
UInt192 get192()
Definition Serializer.h:547
bool empty() const noexcept
Definition Serializer.h:499
UInt160 get160()
Definition Serializer.h:541
Slice getSlice(std::size_t bytes)
void getFieldID(int &type, int &name)
UInt128 get128()
Definition Serializer.h:535
std::size_t remain_
Definition Serializer.h:481
SerialIter(void const *data, std::size_t size) noexcept
std::uint8_t const * p_
Definition Serializer.h:480
std::size_t used_
Definition Serializer.h:482
SerialIter(std::uint8_t const (&data)[N])
Definition Serializer.h:493
int addFieldID(int type, int name)
Blob::const_iterator begin() const
Definition Serializer.h:333
void const * getDataPtr() const
Definition Serializer.h:294
static int decodeLengthLength(std::byte firstByte)
Works out how long a header is, from its first byte.
int addInteger(Integer)
static constexpr int kMaxValueOfFirstByteFor1ByteHeader
Definition Serializer.h:48
Blob const & peekData() const
Definition Serializer.h:273
bool getInteger(Integer &number, int offset)
Definition Serializer.h:232
int addBitString(BaseUInt< Bits, Tag > const &v)
Definition Serializer.h:202
Blob::iterator begin()
Definition Serializer.h:323
static constexpr int kMaxValueOfFirstByteFor2ByteHeader
Definition Serializer.h:52
static constexpr int kMinValueOfLengthFor2ByteHeader
Definition Serializer.h:86
static constexpr int kMaxRepresentableLength
14 values of the first byte mean a 3 byte header, and each of them covers 65,536 lengths.
Definition Serializer.h:108
static constexpr int kMaxValueOfLengthFor1ByteHeader
Definition Serializer.h:84
static constexpr int kMaxNumberOfBytesInHeader
A header is never longer than this.
Definition Serializer.h:33
static constexpr int kMinValueOfLengthFor1ByteHeader
A 1 byte header holds the length in the byte itself, so both ends of this range are the same numbers ...
Definition Serializer.h:83
static constexpr int kMaxValueOfLengthFor3ByteHeader
The largest length the encoder will write.
Definition Serializer.h:118
bool getBitString(BaseUInt< Bits, Tag > &data, int offset) const
Definition Serializer.h:251
size_t capacity() const
Definition Serializer.h:353
std::string getString() const
Definition Serializer.h:309
static constexpr int kMaxValueOfLengthFor2ByteHeader
48 values of the first byte mean a 2 byte header, and each of them covers 256 lengths.
Definition Serializer.h:93
static constexpr int kNumberOfValuesInOneByte
Definition Serializer.h:67
int addVL(Blob const &vector)
Blob::iterator end()
Definition Serializer.h:328
int add8(unsigned char byteValue)
int getLength() const
Definition Serializer.h:304
Serializer(void const *data, std::size_t size)
Definition Serializer.h:129
static constexpr int kMinValueOfFirstByteFor2ByteHeader
Definition Serializer.h:50
int addFieldID(SerializedTypeID type, int name)
Definition Serializer.h:262
int addEncoded(int length)
Appends the length header for a field of the given length.
int addRaw(Blob const &vector)
Slice slice() const noexcept
Definition Serializer.h:141
bool operator==(Blob const &v) const
Definition Serializer.h:359
bool operator==(Serializer const &v) const
Definition Serializer.h:364
void * getDataPtr()
Definition Serializer.h:299
static constexpr int kNumberOfValuesInTwoBytes
Definition Serializer.h:68
static int decodeVLLength(std::byte firstByte)
Reads the field length out of a 1 byte header.
static constexpr int kMaxValueOfFirstByteFor3ByteHeader
Definition Serializer.h:57
Blob getData() const
Definition Serializer.h:278
std::size_t size() const noexcept
Definition Serializer.h:147
bool get8(int &, int offset) const
int getDataLength() const
Definition Serializer.h:289
Blob::const_iterator end() const
Definition Serializer.h:338
static int encodeLengthLength(int length)
Works out how many bytes the header needs for the given length.
Serializer(int n=256)
Definition Serializer.h:124
void reserve(size_t n)
Definition Serializer.h:343
static constexpr int kMinValueOfFirstByteFor3ByteHeader
Definition Serializer.h:54
void resize(size_t n)
Definition Serializer.h:348
void const * data() const noexcept
Definition Serializer.h:153
static constexpr int kMinValueOfFirstByteFor1ByteHeader
Definition Serializer.h:47
static constexpr int kMinValueOfLengthFor3ByteHeader
Definition Serializer.h:98
An immutable linear range of bytes.
Definition Slice.h:28
T is_same_v
T memcpy(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BaseUInt< 128 > UInt128
Definition base_uint.h:578
BaseUInt< 160 > UInt160
Definition base_uint.h:579
constexpr Dest safeCast(Src s) noexcept
Definition safe_cast.h:21
BaseUInt< 192 > UInt192
Definition base_uint.h:581
BaseUInt< 256 > UInt256
Definition base_uint.h:580
SerializedTypeID
Definition SField.h:94
HashPrefix
Prefix for hashing functions.
Definition HashPrefix.h:35
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:11
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52