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