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