rippled
Loading...
Searching...
No Matches
Serializer.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_SERIALIZER_H_INCLUDED
21#define RIPPLE_PROTOCOL_SERIALIZER_H_INCLUDED
22
23#include <xrpl/basics/Blob.h>
24#include <xrpl/basics/Buffer.h>
25#include <xrpl/basics/Slice.h>
26#include <xrpl/basics/base_uint.h>
27#include <xrpl/basics/contract.h>
28#include <xrpl/basics/safe_cast.h>
29#include <xrpl/basics/strHex.h>
30#include <xrpl/beast/utility/instrumentation.h>
31#include <xrpl/protocol/HashPrefix.h>
32#include <xrpl/protocol/SField.h>
33
34#include <cstdint>
35#include <cstring>
36#include <iomanip>
37#include <type_traits>
38
39namespace ripple {
40
42{
43private:
44 // DEPRECATED
46
47public:
48 explicit Serializer(int n = 256)
49 {
50 mData.reserve(n);
51 }
52
54 {
56
57 if (size)
58 {
59 XRPL_ASSERT(
60 data,
61 "ripple::Serializer::Serializer(void const*) : non-null input");
63 }
64 }
65
66 Slice
67 slice() const noexcept
68 {
69 return Slice(mData.data(), mData.size());
70 }
71
73 size() const noexcept
74 {
75 return mData.size();
76 }
77
78 void const*
79 data() const noexcept
80 {
81 return mData.data();
82 }
83
84 // assemble functions
85 int
86 add8(unsigned char i);
87 int
89
90 template <typename T>
91 requires(std::is_same_v<
94 int
95 add32(T i)
96 {
97 int ret = mData.size();
98 mData.push_back(static_cast<unsigned char>((i >> 24) & 0xff));
99 mData.push_back(static_cast<unsigned char>((i >> 16) & 0xff));
100 mData.push_back(static_cast<unsigned char>((i >> 8) & 0xff));
101 mData.push_back(static_cast<unsigned char>(i & 0xff));
102 return ret;
103 }
104
105 int
106 add32(HashPrefix p);
107
108 template <typename T>
109 requires(std::is_same_v<
112 int
113 add64(T i)
114 {
115 int ret = mData.size();
116 mData.push_back(static_cast<unsigned char>((i >> 56) & 0xff));
117 mData.push_back(static_cast<unsigned char>((i >> 48) & 0xff));
118 mData.push_back(static_cast<unsigned char>((i >> 40) & 0xff));
119 mData.push_back(static_cast<unsigned char>((i >> 32) & 0xff));
120 mData.push_back(static_cast<unsigned char>((i >> 24) & 0xff));
121 mData.push_back(static_cast<unsigned char>((i >> 16) & 0xff));
122 mData.push_back(static_cast<unsigned char>((i >> 8) & 0xff));
123 mData.push_back(static_cast<unsigned char>(i & 0xff));
124 return ret;
125 }
126
127 template <typename Integer>
128 int addInteger(Integer);
129
130 template <std::size_t Bits, class Tag>
131 int
133 {
134 return addRaw(v.data(), v.size());
135 }
136
137 int
138 addRaw(Blob const& vector);
139 int
141 int
142 addRaw(const void* ptr, int len);
143 int
144 addRaw(const Serializer& s);
145
146 int
147 addVL(Blob const& vector);
148 int
149 addVL(Slice const& slice);
150 template <class Iter>
151 int
152 addVL(Iter begin, Iter end, int len);
153 int
154 addVL(const void* ptr, int len);
155
156 // disassemble functions
157 bool
158 get8(int&, int offset) const;
159
160 template <typename Integer>
161 bool
162 getInteger(Integer& number, int offset)
163 {
164 static const auto bytes = sizeof(Integer);
165 if ((offset + bytes) > mData.size())
166 return false;
167 number = 0;
168
169 auto ptr = &mData[offset];
170 for (auto i = 0; i < bytes; ++i)
171 {
172 if (i)
173 number <<= 8;
174 number |= *ptr++;
175 }
176 return true;
177 }
178
179 template <std::size_t Bits, typename Tag = void>
180 bool
182 {
183 auto success = (offset + (Bits / 8)) <= mData.size();
184 if (success)
185 memcpy(data.begin(), &(mData.front()) + offset, (Bits / 8));
186 return success;
187 }
188
189 int
190 addFieldID(int type, int name);
191 int
193 {
194 return addFieldID(safe_cast<int>(type), name);
195 }
196
197 // DEPRECATED
198 uint256
199 getSHA512Half() const;
200
201 // totality functions
202 Blob const&
203 peekData() const
204 {
205 return mData;
206 }
207 Blob
208 getData() const
209 {
210 return mData;
211 }
212 Blob&
214 {
215 return mData;
216 }
217
218 int
220 {
221 return mData.size();
222 }
223 const void*
225 {
226 return mData.data();
227 }
228 void*
230 {
231 return mData.data();
232 }
233 int
234 getLength() const
235 {
236 return mData.size();
237 }
239 getString() const
240 {
241 return std::string(static_cast<const char*>(getDataPtr()), size());
242 }
243 void
245 {
246 mData.clear();
247 }
248 bool
249 chop(int num);
250
251 // vector-like functions
252 Blob ::iterator
254 {
255 return mData.begin();
256 }
257 Blob ::iterator
259 {
260 return mData.end();
261 }
262 Blob ::const_iterator
263 begin() const
264 {
265 return mData.begin();
266 }
267 Blob ::const_iterator
268 end() const
269 {
270 return mData.end();
271 }
272 void
273 reserve(size_t n)
274 {
275 mData.reserve(n);
276 }
277 void
278 resize(size_t n)
279 {
280 mData.resize(n);
281 }
282 size_t
283 capacity() const
284 {
285 return mData.capacity();
286 }
287
288 bool
289 operator==(Blob const& v) const
290 {
291 return v == mData;
292 }
293 bool
294 operator!=(Blob const& v) const
295 {
296 return v != mData;
297 }
298 bool
299 operator==(const Serializer& v) const
300 {
301 return v.mData == mData;
302 }
303 bool
304 operator!=(const Serializer& v) const
305 {
306 return v.mData != mData;
307 }
308
309 static int
310 decodeLengthLength(int b1);
311 static int
312 decodeVLLength(int b1);
313 static int
314 decodeVLLength(int b1, int b2);
315 static int
316 decodeVLLength(int b1, int b2, int b3);
317
318private:
319 static int
320 encodeLengthLength(int length); // length to encode length
321 int
322 addEncoded(int length);
323};
324
325template <class Iter>
326int
327Serializer::addVL(Iter begin, Iter end, int len)
328{
329 int ret = addEncoded(len);
330 for (; begin != end; ++begin)
331 {
332 addRaw(begin->data(), begin->size());
333#ifndef NDEBUG
334 len -= begin->size();
335#endif
336 }
337 XRPL_ASSERT(
338 len == 0, "ripple::Serializer::addVL : length matches distance");
339 return ret;
340}
341
342//------------------------------------------------------------------------------
343
344// DEPRECATED
345// Transitional adapter to new serialization interfaces
347{
348private:
352
353public:
354 SerialIter(void const* data, std::size_t size) noexcept;
355
356 SerialIter(Slice const& slice) : SerialIter(slice.data(), slice.size())
357 {
358 }
359
360 // Infer the size of the data based on the size of the passed array.
361 template <int N>
362 explicit SerialIter(std::uint8_t const (&data)[N]) : SerialIter(&data[0], N)
363 {
364 static_assert(N > 0, "");
365 }
366
368 empty() const noexcept
369 {
370 return remain_ == 0;
371 }
372
373 void
374 reset() noexcept;
375
376 int
377 getBytesLeft() const noexcept
378 {
379 return static_cast<int>(remain_);
380 }
381
382 // get functions throw on error
383 unsigned char
384 get8();
385
387 get16();
388
390 get32();
392 geti32();
393
395 get64();
397 geti64();
398
399 template <std::size_t Bits, class Tag = void>
401 getBitString();
402
403 uint128
405 {
406 return getBitString<128>();
407 }
408
409 uint160
411 {
412 return getBitString<160>();
413 }
414
415 uint192
417 {
418 return getBitString<192>();
419 }
420
421 uint256
423 {
424 return getBitString<256>();
425 }
426
427 void
428 getFieldID(int& type, int& name);
429
430 // Returns the size of the VL if the
431 // next object is a VL. Advances the iterator
432 // to the beginning of the VL.
433 int
435
436 Slice
437 getSlice(std::size_t bytes);
438
439 // VFALCO DEPRECATED Returns a copy
440 Blob
441 getRaw(int size);
442
443 // VFALCO DEPRECATED Returns a copy
444 Blob
445 getVL();
446
447 void
448 skip(int num);
449
450 Buffer
451 getVLBuffer();
452
453 template <class T>
454 T
455 getRawHelper(int size);
456};
457
458template <std::size_t Bits, class Tag>
459base_uint<Bits, Tag>
461{
462 auto const n = Bits / 8;
463
464 if (remain_ < n)
465 Throw<std::runtime_error>("invalid SerialIter getBitString");
466
467 auto const x = p_;
468
469 p_ += n;
470 used_ += n;
471 remain_ -= n;
472
474}
475
476} // namespace ripple
477
478#endif
T begin(T... args)
T capacity(T... args)
Like std::vector<char> but better.
Definition: Buffer.h:35
SerialIter(std::uint8_t const (&data)[N])
Definition: Serializer.h:362
std::size_t remain_
Definition: Serializer.h:350
uint128 get128()
Definition: Serializer.h:404
uint192 get192()
Definition: Serializer.h:416
int getBytesLeft() const noexcept
Definition: Serializer.h:377
uint160 get160()
Definition: Serializer.h:410
Slice getSlice(std::size_t bytes)
Definition: Serializer.cpp:511
void skip(int num)
Definition: Serializer.cpp:343
SerialIter(Slice const &slice)
Definition: Serializer.h:356
std::int64_t geti64()
Definition: Serializer.cpp:417
std::uint8_t const * p_
Definition: Serializer.h:349
std::int32_t geti32()
Definition: Serializer.cpp:405
void getFieldID(int &type, int &name)
Definition: Serializer.cpp:429
T getRawHelper(int size)
Definition: Serializer.cpp:457
Blob getRaw(int size)
Definition: Serializer.cpp:479
void reset() noexcept
Definition: Serializer.cpp:335
std::size_t used_
Definition: Serializer.h:351
std::uint16_t get16()
Definition: Serializer.cpp:365
std::uint32_t get32()
Definition: Serializer.cpp:377
base_uint< Bits, Tag > getBitString()
Definition: Serializer.h:460
unsigned char get8()
Definition: Serializer.cpp:353
std::size_t empty() const noexcept
Definition: Serializer.h:368
uint256 get256()
Definition: Serializer.h:422
std::uint64_t get64()
Definition: Serializer.cpp:390
std::size_t size() const noexcept
Definition: Serializer.h:73
void const * data() const noexcept
Definition: Serializer.h:79
Blob::iterator begin()
Definition: Serializer.h:253
bool operator!=(const Serializer &v) const
Definition: Serializer.h:304
int addFieldID(int type, int name)
Definition: Serializer.cpp:120
bool operator==(Blob const &v) const
Definition: Serializer.h:289
bool getBitString(base_uint< Bits, Tag > &data, int offset) const
Definition: Serializer.h:181
int addFieldID(SerializedTypeID type, int name)
Definition: Serializer.h:192
size_t capacity() const
Definition: Serializer.h:283
Slice slice() const noexcept
Definition: Serializer.h:67
bool getInteger(Integer &number, int offset)
Definition: Serializer.h:162
bool get8(int &, int offset) const
Definition: Serializer.cpp:164
void resize(size_t n)
Definition: Serializer.h:278
Blob const & peekData() const
Definition: Serializer.h:203
Blob::const_iterator end() const
Definition: Serializer.h:268
Blob getData() const
Definition: Serializer.h:208
uint256 getSHA512Half() const
Definition: Serializer.cpp:184
Serializer(void const *data, std::size_t size)
Definition: Serializer.h:53
int getDataLength() const
Definition: Serializer.h:219
static int encodeLengthLength(int length)
Definition: Serializer.cpp:254
int addEncoded(int length)
Definition: Serializer.cpp:222
int addInteger(Integer)
int addRaw(Blob const &vector)
Definition: Serializer.cpp:88
int addBitString(base_uint< Bits, Tag > const &v)
Definition: Serializer.h:132
int addVL(Blob const &vector)
Definition: Serializer.cpp:190
bool chop(int num)
Definition: Serializer.cpp:174
Blob::const_iterator begin() const
Definition: Serializer.h:263
static int decodeLengthLength(int b1)
Definition: Serializer.cpp:273
int getLength() const
Definition: Serializer.h:234
int add16(std::uint16_t i)
Definition: Serializer.cpp:43
const void * getDataPtr() const
Definition: Serializer.h:224
int add8(unsigned char i)
Definition: Serializer.cpp:156
Blob::iterator end()
Definition: Serializer.h:258
bool operator!=(Blob const &v) const
Definition: Serializer.h:294
bool operator==(const Serializer &v) const
Definition: Serializer.h:299
Serializer(int n=256)
Definition: Serializer.h:48
void reserve(size_t n)
Definition: Serializer.h:273
static int decodeVLLength(int b1)
Definition: Serializer.cpp:292
void * getDataPtr()
Definition: Serializer.h:229
std::string getString() const
Definition: Serializer.h:239
An immutable linear range of bytes.
Definition: Slice.h:46
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:86
static base_uint fromVoid(void const *data)
Definition: base_uint.h:319
pointer data()
Definition: base_uint.h:125
static constexpr std::size_t size()
Definition: base_uint.h:526
T clear(T... args)
T data(T... args)
T end(T... args)
T front(T... args)
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:26
SerializedTypeID
Definition: SField.h:108
HashPrefix
Prefix for hashing functions.
Definition: HashPrefix.h:55
T push_back(T... args)
T reserve(T... args)
T resize(T... args)
T size(T... args)