rippled
Serializer.cpp
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 #include <ripple/basics/contract.h>
21 #include <ripple/basics/Log.h>
22 #include <ripple/protocol/digest.h>
23 #include <ripple/protocol/Serializer.h>
24 #include <type_traits>
25 
26 namespace ripple {
27 
28 int Serializer::addZeros (size_t uBytes)
29 {
30  int ret = mData.size ();
31 
32  while (uBytes--)
33  mData.push_back (0);
34 
35  return ret;
36 }
37 
39 {
40  int ret = mData.size ();
41  mData.push_back (static_cast<unsigned char> (i >> 8));
42  mData.push_back (static_cast<unsigned char> (i & 0xff));
43  return ret;
44 }
45 
47 {
48  int ret = mData.size ();
49  mData.push_back (static_cast<unsigned char> (i >> 24));
50  mData.push_back (static_cast<unsigned char> ((i >> 16) & 0xff));
51  mData.push_back (static_cast<unsigned char> ((i >> 8) & 0xff));
52  mData.push_back (static_cast<unsigned char> (i & 0xff));
53  return ret;
54 }
55 
56 
58 {
59  // This should never trigger; the size & type of a hash prefix are
60  // integral parts of the protocol and unlikely to ever change.
61  static_assert(std::is_same_v<std::uint32_t,
62  std::underlying_type_t<decltype(p)>>);
63 
64  return add32(safe_cast<std::uint32_t>(p));
65 }
66 
68 {
69  int ret = mData.size ();
70  mData.push_back (static_cast<unsigned char> (i >> 56));
71  mData.push_back (static_cast<unsigned char> ((i >> 48) & 0xff));
72  mData.push_back (static_cast<unsigned char> ((i >> 40) & 0xff));
73  mData.push_back (static_cast<unsigned char> ((i >> 32) & 0xff));
74  mData.push_back (static_cast<unsigned char> ((i >> 24) & 0xff));
75  mData.push_back (static_cast<unsigned char> ((i >> 16) & 0xff));
76  mData.push_back (static_cast<unsigned char> ((i >> 8) & 0xff));
77  mData.push_back (static_cast<unsigned char> (i & 0xff));
78  return ret;
79 }
80 
81 template <> int Serializer::addInteger(unsigned char i) { return add8(i); }
82 template <> int Serializer::addInteger(std::uint16_t i) { return add16(i); }
83 template <> int Serializer::addInteger(std::uint32_t i) { return add32(i); }
84 template <> int Serializer::addInteger(std::uint64_t i) { return add64(i); }
85 
87 {
88  int ret = mData.size ();
89  mData.insert (mData.end (), i.begin (), i.end ());
90  return ret;
91 }
92 
94 {
95  int ret = mData.size ();
96  mData.insert (mData.end (), i.begin (), i.end ());
97  return ret;
98 }
99 
100 int Serializer::addRaw (Blob const& vector)
101 {
102  int ret = mData.size ();
103  mData.insert (mData.end (), vector.begin (), vector.end ());
104  return ret;
105 }
106 
108 {
109  int ret = mData.size ();
110  mData.insert (mData.end (), s.begin (), s.end ());
111  return ret;
112 }
113 
114 int Serializer::addRaw (const void* ptr, int len)
115 {
116  int ret = mData.size ();
117  mData.insert (mData.end (), (const char*) ptr, ((const char*)ptr) + len);
118  return ret;
119 }
120 
121 bool Serializer::get256 (uint256& o, int offset) const
122 {
123  if ((offset + (256 / 8)) > mData.size ()) return false;
124 
125  memcpy (o.begin (), & (mData.front ()) + offset, (256 / 8));
126  return true;
127 }
128 
129 int Serializer::addFieldID (int type, int name)
130 {
131  int ret = mData.size ();
132  assert ((type > 0) && (type < 256) && (name > 0) && (name < 256));
133 
134  if (type < 16)
135  {
136  if (name < 16) // common type, common name
137  mData.push_back (static_cast<unsigned char> ((type << 4) | name));
138  else
139  {
140  // common type, uncommon name
141  mData.push_back (static_cast<unsigned char> (type << 4));
142  mData.push_back (static_cast<unsigned char> (name));
143  }
144  }
145  else if (name < 16)
146  {
147  // uncommon type, common name
148  mData.push_back (static_cast<unsigned char> (name));
149  mData.push_back (static_cast<unsigned char> (type));
150  }
151  else
152  {
153  // uncommon type, uncommon name
154  mData.push_back (static_cast<unsigned char> (0));
155  mData.push_back (static_cast<unsigned char> (type));
156  mData.push_back (static_cast<unsigned char> (name));
157  }
158 
159  return ret;
160 }
161 
162 int Serializer::add8 (unsigned char byte)
163 {
164  int ret = mData.size ();
165  mData.push_back (byte);
166  return ret;
167 }
168 
169 bool Serializer::get8 (int& byte, int offset) const
170 {
171  if (offset >= mData.size ()) return false;
172 
173  byte = mData[offset];
174  return true;
175 }
176 
177 bool Serializer::chop (int bytes)
178 {
179  if (bytes > mData.size ()) return false;
180 
181  mData.resize (mData.size () - bytes);
182  return true;
183 }
184 
185 bool Serializer::getRaw (Blob& o, int offset, int length) const
186 {
187  if ((offset + length) > mData.size ()) return false;
188 
189  o.assign (mData.begin () + offset, mData.begin () + offset + length);
190  return true;
191 }
192 
193 Blob Serializer::getRaw (int offset, int length) const
194 {
195  Blob o;
196 
197  if ((offset + length) > mData.size ()) return o;
198 
199  o.assign (mData.begin () + offset, mData.begin () + offset + length);
200  return o;
201 }
202 
204 {
205  return sha512Half(makeSlice(mData));
206 }
207 
208 int Serializer::addVL (Blob const& vector)
209 {
210  int ret = addEncoded (vector.size ());
211  addRaw (vector);
212  assert (mData.size () == (ret + vector.size () + encodeLengthLength (vector.size ())));
213  return ret;
214 }
215 
216 int Serializer::addVL (Slice const& slice)
217 {
218  int ret = addEncoded (slice.size());
219  if (slice.size())
220  addRaw (slice.data(), slice.size());
221  return ret;
222 }
223 
224 int Serializer::addVL (const void* ptr, int len)
225 {
226  int ret = addEncoded (len);
227 
228  if (len)
229  addRaw (ptr, len);
230 
231  return ret;
232 }
233 
234 bool Serializer::getVL (Blob& objectVL, int offset, int& length) const
235 {
236  int b1;
237 
238  if (!get8 (b1, offset++)) return false;
239 
240  int datLen, lenLen = decodeLengthLength (b1);
241 
242  try
243  {
244  if (lenLen == 1)
245  datLen = decodeVLLength (b1);
246  else if (lenLen == 2)
247  {
248  int b2;
249 
250  if (!get8 (b2, offset++)) return false;
251 
252  datLen = decodeVLLength (b1, b2);
253  }
254  else if (lenLen == 3)
255  {
256  int b2, b3;
257 
258  if (!get8 (b2, offset++)) return false;
259 
260  if (!get8 (b3, offset++)) return false;
261 
262  datLen = decodeVLLength (b1, b2, b3);
263  }
264  else return false;
265  }
266  catch (std::exception const&)
267  {
268  return false;
269  }
270 
271  length = lenLen + datLen;
272  return getRaw (objectVL, offset, datLen);
273 }
274 
275 bool Serializer::getVLLength (int& length, int offset) const
276 {
277  int b1;
278 
279  if (!get8 (b1, offset++)) return false;
280 
281  int lenLen = decodeLengthLength (b1);
282 
283  try
284  {
285  if (lenLen == 1)
286  length = decodeVLLength (b1);
287  else if (lenLen == 2)
288  {
289  int b2;
290 
291  if (!get8 (b2, offset++)) return false;
292 
293  length = decodeVLLength (b1, b2);
294  }
295  else if (lenLen == 3)
296  {
297  int b2, b3;
298 
299  if (!get8 (b2, offset++)) return false;
300 
301  if (!get8 (b3, offset++)) return false;
302 
303  length = decodeVLLength (b1, b2, b3);
304  }
305  else return false;
306  }
307  catch (std::exception const&)
308  {
309  return false;
310  }
311 
312  return true;
313 }
314 
315 int Serializer::addEncoded (int length)
316 {
318  int numBytes = 0;
319 
320  if (length <= 192)
321  {
322  bytes[0] = static_cast<unsigned char> (length);
323  numBytes = 1;
324  }
325  else if (length <= 12480)
326  {
327  length -= 193;
328  bytes[0] = 193 + static_cast<unsigned char> (length >> 8);
329  bytes[1] = static_cast<unsigned char> (length & 0xff);
330  numBytes = 2;
331  }
332  else if (length <= 918744)
333  {
334  length -= 12481;
335  bytes[0] = 241 + static_cast<unsigned char> (length >> 16);
336  bytes[1] = static_cast<unsigned char> ((length >> 8) & 0xff);
337  bytes[2] = static_cast<unsigned char> (length & 0xff);
338  numBytes = 3;
339  }
340  else Throw<std::overflow_error> ("lenlen");
341 
342  return addRaw (&bytes[0], numBytes);
343 }
344 
346 {
347  if (length < 0) Throw<std::overflow_error> ("len<0");
348 
349  if (length <= 192) return 1;
350 
351  if (length <= 12480) return 2;
352 
353  if (length <= 918744) return 3;
354 
355  Throw<std::overflow_error> ("len>918744");
356  return 0; // Silence compiler warning.
357 }
358 
360 {
361  if (b1 < 0) Throw<std::overflow_error> ("b1<0");
362 
363  if (b1 <= 192) return 1;
364 
365  if (b1 <= 240) return 2;
366 
367  if (b1 <= 254) return 3;
368 
369  Throw<std::overflow_error> ("b1>254");
370  return 0; // Silence compiler warning.
371 }
372 
374 {
375  if (b1 < 0) Throw<std::overflow_error> ("b1<0");
376 
377  if (b1 > 254) Throw<std::overflow_error> ("b1>254");
378 
379  return b1;
380 }
381 
382 int Serializer::decodeVLLength (int b1, int b2)
383 {
384  if (b1 < 193) Throw<std::overflow_error> ("b1<193");
385 
386  if (b1 > 240) Throw<std::overflow_error> ("b1>240");
387 
388  return 193 + (b1 - 193) * 256 + b2;
389 }
390 
391 int Serializer::decodeVLLength (int b1, int b2, int b3)
392 {
393  if (b1 < 241) Throw<std::overflow_error> ("b1<241");
394 
395  if (b1 > 254) Throw<std::overflow_error> ("b1>254");
396 
397  return 12481 + (b1 - 241) * 65536 + b2 * 256 + b3;
398 }
399 
400 //------------------------------------------------------------------------------
401 
402 SerialIter::SerialIter (void const* data,
403  std::size_t size) noexcept
404  : p_ (reinterpret_cast<
405  std::uint8_t const*>(data))
406  , remain_ (size)
407 {
408 }
409 
410 void
412 {
413  p_ -= used_;
414  remain_ += used_;
415  used_ = 0;
416 }
417 
418 void
419 SerialIter::skip (int length)
420 {
421  if (remain_ < length)
422  Throw<std::runtime_error> (
423  "invalid SerialIter skip");
424  p_ += length;
425  used_ += length;
426  remain_ -= length;
427 }
428 
429 unsigned char
431 {
432  if (remain_ < 1)
433  Throw<std::runtime_error> (
434  "invalid SerialIter get8");
435  unsigned char t = *p_;
436  ++p_;
437  ++used_;
438  --remain_;
439  return t;
440 }
441 
444 {
445  if (remain_ < 2)
446  Throw<std::runtime_error> (
447  "invalid SerialIter get16");
448  auto t = p_;
449  p_ += 2;
450  used_ += 2;
451  remain_ -= 2;
452  return
453  (std::uint64_t(t[0]) << 8) +
454  std::uint64_t(t[1] );
455 }
456 
459 {
460  if (remain_ < 4)
461  Throw<std::runtime_error> (
462  "invalid SerialIter get32");
463  auto t = p_;
464  p_ += 4;
465  used_ += 4;
466  remain_ -= 4;
467  return
468  (std::uint64_t(t[0]) << 24) +
469  (std::uint64_t(t[1]) << 16) +
470  (std::uint64_t(t[2]) << 8) +
471  std::uint64_t(t[3] );
472 }
473 
476 {
477  if (remain_ < 8)
478  Throw<std::runtime_error> (
479  "invalid SerialIter get64");
480  auto t = p_;
481  p_ += 8;
482  used_ += 8;
483  remain_ -= 8;
484  return
485  (std::uint64_t(t[0]) << 56) +
486  (std::uint64_t(t[1]) << 48) +
487  (std::uint64_t(t[2]) << 40) +
488  (std::uint64_t(t[3]) << 32) +
489  (std::uint64_t(t[4]) << 24) +
490  (std::uint64_t(t[5]) << 16) +
491  (std::uint64_t(t[6]) << 8) +
492  std::uint64_t(t[7] );
493 }
494 
495 void
496 SerialIter::getFieldID (int& type, int& name)
497 {
498  type = get8();
499  name = type & 15;
500  type >>= 4;
501 
502  if (type == 0)
503  {
504  // uncommon type
505  type = get8();
506  if (type == 0 || type < 16)
507  Throw<std::runtime_error> (
508  "gFID: uncommon type out of range " +
509  std::to_string(type));
510  }
511 
512  if (name == 0)
513  {
514  // uncommon name
515  name = get8();
516  if (name == 0 || name < 16)
517  Throw<std::runtime_error> (
518  "gFID: uncommon name out of range " +
519  std::to_string(name));
520  }
521 }
522 
523 // getRaw for blob or buffer
524 template<class T>
526 {
527  static_assert(std::is_same<T, Blob>::value ||
529  if (remain_ < size)
530  Throw<std::runtime_error> (
531  "invalid SerialIter getRaw");
532  T result (size);
533  if (size != 0)
534  {
535  // It's normally safe to call memcpy with size set to 0 (see the
536  // C99 standard 7.21.1/2). However, here this could mean that
537  // result.data would be null, which would trigger undefined behavior.
538  std::memcpy(result.data(), p_, size);
539  p_ += size;
540  used_ += size;
541  remain_ -= size;
542  }
543  return result;
544 }
545 
546 // VFALCO DEPRECATED Returns a copy
547 Blob
549 {
550  return getRawHelper<Blob> (size);
551 }
552 
554 {
555  int b1 = get8();
556  int datLen;
557  int lenLen = Serializer::decodeLengthLength(b1);
558  if (lenLen == 1)
559  {
560  datLen = Serializer::decodeVLLength (b1);
561  }
562  else if (lenLen == 2)
563  {
564  int b2 = get8();
565  datLen = Serializer::decodeVLLength (b1, b2);
566  }
567  else
568  {
569  assert(lenLen == 3);
570  int b2 = get8();
571  int b3 = get8();
572  datLen = Serializer::decodeVLLength (b1, b2, b3);
573  }
574  return datLen;
575 }
576 
577 Slice
579 {
580  if (bytes > remain_)
581  Throw<std::runtime_error> (
582  "invalid SerialIter getSlice");
583  Slice s(p_, bytes);
584  p_ += bytes;
585  used_ += bytes;
586  remain_ -= bytes;
587  return s;
588 }
589 
590 // VFALCO DEPRECATED Returns a copy
591 Blob
593 {
594  return getRaw(getVLDataLength ());
595 }
596 
597 Buffer
599 {
600  return getRawHelper<Buffer> (getVLDataLength ());
601 }
602 
603 } // ripple
ripple::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:77
std::is_same_v
T is_same_v
std::vector::resize
T resize(T... args)
ripple::Serializer::end
Blob ::iterator end()
Definition: Serializer.h:222
ripple::makeSlice
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition: Slice.h:199
ripple::Serializer::chop
bool chop(int num)
Definition: Serializer.cpp:177
ripple::Serializer::add8
int add8(unsigned char byte)
Definition: Serializer.cpp:162
ripple::Serializer::mData
Blob mData
Definition: Serializer.h:47
std::exception
STL class.
ripple::Serializer::addFieldID
int addFieldID(int type, int name)
Definition: Serializer.cpp:129
ripple::Serializer::addInteger
int addInteger(Integer)
ripple::Serializer::getVLLength
bool getVLLength(int &length, int offset) const
Definition: Serializer.cpp:275
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:43
ripple::Serializer::getVL
bool getVL(Blob &objectVL, int offset, int &length) const
Definition: Serializer.cpp:234
std::vector< unsigned char >
std::vector::size
T size(T... args)
ripple::base_uint::end
iterator end()
Definition: base_uint.h:107
ripple::Slice::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:87
ripple::SerialIter::getFieldID
void getFieldID(int &type, int &name)
Definition: Serializer.cpp:496
ripple::Serializer::decodeVLLength
static int decodeVLLength(int b1)
Definition: Serializer.cpp:373
ripple::Serializer::add32
int add32(std::uint32_t)
Definition: Serializer.cpp:46
ripple::Buffer
Like std::vector<char> but better.
Definition: Buffer.h:35
ripple::Serializer::getSHA512Half
uint256 getSHA512Half() const
Definition: Serializer.cpp:203
ripple::Serializer::decodeLengthLength
static int decodeLengthLength(int b1)
Definition: Serializer.cpp:359
std::vector::front
T front(T... args)
ripple::Serializer::add16
int add16(std::uint16_t)
Definition: Serializer.cpp:38
ripple::Serializer::begin
Blob ::iterator begin()
Definition: Serializer.h:218
ripple::Serializer::get256
bool get256(uint256 &, int offset) const
Definition: Serializer.cpp:121
ripple::SerialIter::SerialIter
SerialIter(void const *data, std::size_t size) noexcept
Definition: Serializer.cpp:402
std::underlying_type_t
std::vector::push_back
T push_back(T... args)
ripple::base_uint
Definition: base_uint.h:65
ripple::SerialIter::get8
unsigned char get8()
Definition: Serializer.cpp:430
ripple::HashPrefix
HashPrefix
Prefix for hashing functions.
Definition: HashPrefix.h:56
ripple::SerialIter::get64
std::uint64_t get64()
Definition: Serializer.cpp:475
ripple::Serializer::getRaw
bool getRaw(Blob &, int offset, int length) const
Definition: Serializer.cpp:185
ripple::Serializer::addRaw
int addRaw(Blob const &vector)
Definition: Serializer.cpp:100
std::to_string
T to_string(T... args)
std::array< std::uint8_t, 4 >
ripple::Serializer::get8
bool get8(int &, int offset) const
Definition: Serializer.cpp:169
ripple::Serializer::slice
Slice slice() const noexcept
Definition: Serializer.h:67
ripple::Serializer::add128
int add128(const uint128 &)
Definition: Serializer.cpp:86
ripple::Serializer::addEncoded
int addEncoded(int length)
Definition: Serializer.cpp:315
ripple::SerialIter::getVL
Blob getVL()
Definition: Serializer.cpp:592
std::uint16_t
ripple::SerialIter::skip
void skip(int num)
Definition: Serializer.cpp:419
ripple::SerialIter::used_
std::size_t used_
Definition: Serializer.h:316
ripple::SerialIter::remain_
std::size_t remain_
Definition: Serializer.h:315
ripple::Serializer
Definition: Serializer.h:43
ripple::SerialIter::getRaw
Blob getRaw(int size)
Definition: Serializer.cpp:548
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Serializer::addZeros
int addZeros(size_t uBytes)
Definition: Serializer.cpp:28
ripple::base_uint::begin
iterator begin()
Definition: base_uint.h:106
std::vector::begin
T begin(T... args)
std::vector::insert
T insert(T... args)
ripple::sha512Half
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:268
ripple::SerialIter::getVLDataLength
int getVLDataLength()
Definition: Serializer.cpp:553
ripple::SerialIter::getRawHelper
T getRawHelper(int size)
Definition: Serializer.cpp:525
ripple::Serializer::add64
int add64(std::uint64_t)
Definition: Serializer.cpp:67
ripple::SerialIter::p_
std::uint8_t const * p_
Definition: Serializer.h:314
ripple::Serializer::addVL
int addVL(Blob const &vector)
Definition: Serializer.cpp:208
std::vector::assign
T assign(T... args)
std::size_t
std::memcpy
T memcpy(T... args)
std::vector::end
T end(T... args)
ripple::SerialIter::get32
std::uint32_t get32()
Definition: Serializer.cpp:458
ripple::Serializer::encodeLengthLength
static int encodeLengthLength(int length)
Definition: Serializer.cpp:345
ripple::SerialIter::reset
void reset() noexcept
Definition: Serializer.cpp:411
ripple::SerialIter::getVLBuffer
Buffer getVLBuffer()
Definition: Serializer.cpp:598
type_traits
ripple::SerialIter::getSlice
Slice getSlice(std::size_t bytes)
Definition: Serializer.cpp:578
ripple::SerialIter::get16
std::uint16_t get16()
Definition: Serializer.cpp:443
ripple::Serializer::add256
int add256(uint256 const &)
Definition: Serializer.cpp:93