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 <type_traits>
37
38namespace ripple {
39
41{
42private:
43 // DEPRECATED
45
46public:
47 explicit Serializer(int n = 256)
48 {
49 mData.reserve(n);
50 }
51
53 {
55
56 if (size)
57 {
58 XRPL_ASSERT(
59 data,
60 "ripple::Serializer::Serializer(void const*) : non-null input");
62 }
63 }
64
65 Slice
66 slice() const noexcept
67 {
68 return Slice(mData.data(), mData.size());
69 }
70
72 size() const noexcept
73 {
74 return mData.size();
75 }
76
77 void const*
78 data() const noexcept
79 {
80 return mData.data();
81 }
82
83 // assemble functions
84 int
85 add8(unsigned char i);
86 int
88
89 template <typename T>
90 requires(std::is_same_v<
93 int
94 add32(T i)
95 {
96 int ret = mData.size();
97 mData.push_back(static_cast<unsigned char>((i >> 24) & 0xff));
98 mData.push_back(static_cast<unsigned char>((i >> 16) & 0xff));
99 mData.push_back(static_cast<unsigned char>((i >> 8) & 0xff));
100 mData.push_back(static_cast<unsigned char>(i & 0xff));
101 return ret;
102 }
103
104 int
105 add32(HashPrefix p);
106
107 template <typename T>
108 requires(std::is_same_v<
111 int
112 add64(T i)
113 {
114 int ret = mData.size();
115 mData.push_back(static_cast<unsigned char>((i >> 56) & 0xff));
116 mData.push_back(static_cast<unsigned char>((i >> 48) & 0xff));
117 mData.push_back(static_cast<unsigned char>((i >> 40) & 0xff));
118 mData.push_back(static_cast<unsigned char>((i >> 32) & 0xff));
119 mData.push_back(static_cast<unsigned char>((i >> 24) & 0xff));
120 mData.push_back(static_cast<unsigned char>((i >> 16) & 0xff));
121 mData.push_back(static_cast<unsigned char>((i >> 8) & 0xff));
122 mData.push_back(static_cast<unsigned char>(i & 0xff));
123 return ret;
124 }
125
126 template <typename Integer>
127 int addInteger(Integer);
128
129 template <std::size_t Bits, class Tag>
130 int
132 {
133 return addRaw(v.data(), v.size());
134 }
135
136 int
137 addRaw(Blob const& vector);
138 int
140 int
141 addRaw(void const* ptr, int len);
142 int
143 addRaw(Serializer const& s);
144
145 int
146 addVL(Blob const& vector);
147 int
148 addVL(Slice const& slice);
149 template <class Iter>
150 int
151 addVL(Iter begin, Iter end, int len);
152 int
153 addVL(void const* ptr, int len);
154
155 // disassemble functions
156 bool
157 get8(int&, int offset) const;
158
159 template <typename Integer>
160 bool
161 getInteger(Integer& number, int offset)
162 {
163 static auto const bytes = sizeof(Integer);
164 if ((offset + bytes) > mData.size())
165 return false;
166 number = 0;
167
168 auto ptr = &mData[offset];
169 for (auto i = 0; i < bytes; ++i)
170 {
171 if (i)
172 number <<= 8;
173 number |= *ptr++;
174 }
175 return true;
176 }
177
178 template <std::size_t Bits, typename Tag = void>
179 bool
181 {
182 auto success = (offset + (Bits / 8)) <= mData.size();
183 if (success)
184 memcpy(data.begin(), &(mData.front()) + offset, (Bits / 8));
185 return success;
186 }
187
188 int
189 addFieldID(int type, int name);
190 int
192 {
193 return addFieldID(safe_cast<int>(type), name);
194 }
195
196 // DEPRECATED
197 uint256
198 getSHA512Half() const;
199
200 // totality functions
201 Blob const&
202 peekData() const
203 {
204 return mData;
205 }
206 Blob
207 getData() const
208 {
209 return mData;
210 }
211 Blob&
213 {
214 return mData;
215 }
216
217 int
219 {
220 return mData.size();
221 }
222 void const*
224 {
225 return mData.data();
226 }
227 void*
229 {
230 return mData.data();
231 }
232 int
233 getLength() const
234 {
235 return mData.size();
236 }
238 getString() const
239 {
240 return std::string(static_cast<char const*>(getDataPtr()), size());
241 }
242 void
244 {
245 mData.clear();
246 }
247 bool
248 chop(int num);
249
250 // vector-like functions
251 Blob ::iterator
253 {
254 return mData.begin();
255 }
256 Blob ::iterator
258 {
259 return mData.end();
260 }
261 Blob ::const_iterator
262 begin() const
263 {
264 return mData.begin();
265 }
266 Blob ::const_iterator
267 end() const
268 {
269 return mData.end();
270 }
271 void
272 reserve(size_t n)
273 {
274 mData.reserve(n);
275 }
276 void
277 resize(size_t n)
278 {
279 mData.resize(n);
280 }
281 size_t
282 capacity() const
283 {
284 return mData.capacity();
285 }
286
287 bool
288 operator==(Blob const& v) const
289 {
290 return v == mData;
291 }
292 bool
293 operator!=(Blob const& v) const
294 {
295 return v != mData;
296 }
297 bool
298 operator==(Serializer const& v) const
299 {
300 return v.mData == mData;
301 }
302 bool
303 operator!=(Serializer const& v) const
304 {
305 return v.mData != mData;
306 }
307
308 static int
309 decodeLengthLength(int b1);
310 static int
311 decodeVLLength(int b1);
312 static int
313 decodeVLLength(int b1, int b2);
314 static int
315 decodeVLLength(int b1, int b2, int b3);
316
317private:
318 static int
319 encodeLengthLength(int length); // length to encode length
320 int
321 addEncoded(int length);
322};
323
324template <class Iter>
325int
326Serializer::addVL(Iter begin, Iter end, int len)
327{
328 int ret = addEncoded(len);
329 for (; begin != end; ++begin)
330 {
331 addRaw(begin->data(), begin->size());
332#ifndef NDEBUG
333 len -= begin->size();
334#endif
335 }
336 XRPL_ASSERT(
337 len == 0, "ripple::Serializer::addVL : length matches distance");
338 return ret;
339}
340
341//------------------------------------------------------------------------------
342
343// DEPRECATED
344// Transitional adapter to new serialization interfaces
346{
347private:
351
352public:
353 SerialIter(void const* data, std::size_t size) noexcept;
354
355 SerialIter(Slice const& slice) : SerialIter(slice.data(), slice.size())
356 {
357 }
358
359 // Infer the size of the data based on the size of the passed array.
360 template <int N>
361 explicit SerialIter(std::uint8_t const (&data)[N]) : SerialIter(&data[0], N)
362 {
363 static_assert(N > 0, "");
364 }
365
367 empty() const noexcept
368 {
369 return remain_ == 0;
370 }
371
372 void
373 reset() noexcept;
374
375 int
376 getBytesLeft() const noexcept
377 {
378 return static_cast<int>(remain_);
379 }
380
381 // get functions throw on error
382 unsigned char
383 get8();
384
386 get16();
387
389 get32();
391 geti32();
392
394 get64();
396 geti64();
397
398 template <std::size_t Bits, class Tag = void>
400 getBitString();
401
402 uint128
404 {
405 return getBitString<128>();
406 }
407
408 uint160
410 {
411 return getBitString<160>();
412 }
413
414 uint192
416 {
417 return getBitString<192>();
418 }
419
420 uint256
422 {
423 return getBitString<256>();
424 }
425
426 void
427 getFieldID(int& type, int& name);
428
429 // Returns the size of the VL if the
430 // next object is a VL. Advances the iterator
431 // to the beginning of the VL.
432 int
434
435 Slice
436 getSlice(std::size_t bytes);
437
438 // VFALCO DEPRECATED Returns a copy
439 Blob
440 getRaw(int size);
441
442 // VFALCO DEPRECATED Returns a copy
443 Blob
444 getVL();
445
446 void
447 skip(int num);
448
449 Buffer
450 getVLBuffer();
451
452 template <class T>
453 T
454 getRawHelper(int size);
455};
456
457template <std::size_t Bits, class Tag>
458base_uint<Bits, Tag>
460{
461 auto const n = Bits / 8;
462
463 if (remain_ < n)
464 Throw<std::runtime_error>("invalid SerialIter getBitString");
465
466 auto const x = p_;
467
468 p_ += n;
469 used_ += n;
470 remain_ -= n;
471
473}
474
475} // namespace ripple
476
477#endif
T begin(T... args)
T capacity(T... args)
Like std::vector<char> but better.
Definition Buffer.h:36
SerialIter(std::uint8_t const (&data)[N])
Definition Serializer.h:361
std::size_t remain_
Definition Serializer.h:349
int getBytesLeft() const noexcept
Definition Serializer.h:376
Slice getSlice(std::size_t bytes)
void skip(int num)
SerialIter(Slice const &slice)
Definition Serializer.h:355
std::int64_t geti64()
std::uint8_t const * p_
Definition Serializer.h:348
std::int32_t geti32()
void getFieldID(int &type, int &name)
T getRawHelper(int size)
Blob getRaw(int size)
void reset() noexcept
std::size_t used_
Definition Serializer.h:350
std::uint16_t get16()
std::uint32_t get32()
base_uint< Bits, Tag > getBitString()
Definition Serializer.h:459
unsigned char get8()
std::size_t empty() const noexcept
Definition Serializer.h:367
std::uint64_t get64()
std::size_t size() const noexcept
Definition Serializer.h:72
void const * data() const noexcept
Definition Serializer.h:78
Blob::iterator begin()
Definition Serializer.h:252
int addFieldID(int type, int name)
bool operator==(Blob const &v) const
Definition Serializer.h:288
bool getBitString(base_uint< Bits, Tag > &data, int offset) const
Definition Serializer.h:180
int addFieldID(SerializedTypeID type, int name)
Definition Serializer.h:191
size_t capacity() const
Definition Serializer.h:282
Slice slice() const noexcept
Definition Serializer.h:66
bool getInteger(Integer &number, int offset)
Definition Serializer.h:161
bool get8(int &, int offset) const
void resize(size_t n)
Definition Serializer.h:277
Blob const & peekData() const
Definition Serializer.h:202
Blob::const_iterator end() const
Definition Serializer.h:267
Blob getData() const
Definition Serializer.h:207
uint256 getSHA512Half() const
Serializer(void const *data, std::size_t size)
Definition Serializer.h:52
int getDataLength() const
Definition Serializer.h:218
bool operator==(Serializer const &v) const
Definition Serializer.h:298
static int encodeLengthLength(int length)
int addEncoded(int length)
int addInteger(Integer)
int addRaw(Blob const &vector)
int addBitString(base_uint< Bits, Tag > const &v)
Definition Serializer.h:131
bool operator!=(Serializer const &v) const
Definition Serializer.h:303
int addVL(Blob const &vector)
bool chop(int num)
Blob::const_iterator begin() const
Definition Serializer.h:262
static int decodeLengthLength(int b1)
int getLength() const
Definition Serializer.h:233
int add16(std::uint16_t i)
int add8(unsigned char i)
Blob::iterator end()
Definition Serializer.h:257
bool operator!=(Blob const &v) const
Definition Serializer.h:293
Serializer(int n=256)
Definition Serializer.h:47
void reserve(size_t n)
Definition Serializer.h:272
static int decodeVLLength(int b1)
void const * getDataPtr() const
Definition Serializer.h:223
std::string getString() const
Definition Serializer.h:238
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
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:25
SerializedTypeID
Definition SField.h:110
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)