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