datastream.hpp 14.9 KB
Newer Older
1 2 3 4
/**
 *  @file db.h
 *  @copyright defined in eos/LICENSE.txt
 */
5
#pragma once
6 7 8
#include <eosiolib/system.h>
#include <eosiolib/memory.h>
#include <eosiolib/vector.hpp>
D
Daniel Larimer 已提交
9
#include <boost/container/flat_map.hpp>
10
#include <eosiolib/varint.hpp>
D
Daniel Larimer 已提交
11
#include <array>
12
#include <string>
13 14


P
Pravin 已提交
15
namespace eosio {
16 17 18
/**
 *  @brief A data stream for reading and writing data in the form of bytes
 */
19 20 21 22
template<typename T>
class datastream {
   public:
      datastream( T start, size_t s )
23
      :_start(start),_pos(start),_end(start+s){}
A
arhag 已提交
24

25 26 27 28 29
     /**
      *  Skips a specified number of bytes from this stream
      *  @brief Skips a specific number of bytes from this stream
      *  @param s The number of bytes to skip
      */
30
      inline void skip( size_t s ){ _pos += s; }
A
arhag 已提交
31

32 33 34 35 36 37
     /**
      *  Reads a specified number of bytes from the stream into a buffer
      *  @brief Reads a specified number of bytes from this stream into a buffer
      *  @param d pointer to the destination buffer
      *  @param s the number of bytes to read
      */
38
      inline bool read( char* d, size_t s ) {
39
        eosio_assert( size_t(_end - _pos) >= (size_t)s, "read" );
40 41 42 43
        memcpy( d, _pos, s );
        _pos += s;
        return true;
      }
44 45 46 47 48 49 50

     /**
      *  Writes a specified number of bytes into the stream from a buffer
      *  @brief Writes a specified number of bytes into the stream from a buffer
      *  @param d pointer to the source buffer
      *  @param s The number of bytes to write
      */
51
      inline bool write( const char* d, size_t s ) {
52
        eosio_assert( _end - _pos >= (int32_t)s, "write" );
53 54 55 56
        memcpy( _pos, d, s );
        _pos += s;
        return true;
      }
A
arhag 已提交
57

58 59 60 61 62
     /**
      *  Writes a byte into the stream
      *  @brief Writes a byte into the stream
      *  @param c byte to write
      */
A
arhag 已提交
63
      inline bool put(char c) {
64
        eosio_assert( _pos < _end, "put" );
A
arhag 已提交
65 66
        *_pos = c;
        ++_pos;
67 68
        return true;
      }
A
arhag 已提交
69

70 71 72 73 74
     /**
      *  Reads a byte from the stream
      *  @brief Reads a byte from the stream
      *  @param c reference to destination byte
      */
75
      inline bool get( unsigned char& c ) { return get( *(char*)&c ); }
A
arhag 已提交
76
      inline bool get( char& c )
77
      {
78
        eosio_assert( _pos < _end, "get" );
79
        c = *_pos;
A
arhag 已提交
80
        ++_pos;
81 82
        return true;
      }
83 84 85 86 87 88 89 90

     /**
      *  Retrieves the current position of the stream
      *  @brief Retrieves the current position of the stream
      *  @return the current position of the stream
      */
      T pos()const { return _pos; }
      inline bool valid()const { return _pos <= _end && _pos >= _start;  }
A
arhag 已提交
91

92 93 94 95 96 97 98 99 100 101 102 103
     /**
      *  Sets the position within the current stream
      *  @brief Sets the position within the current stream
      *  @param p offset relative to the origin
      */
      inline bool seekp(size_t p) { _pos = _start + p; return _pos <= _end; }

     /**
      *  Gets the position within the current stream
      *  @brief Gets the position within the current stream
      *  @return p the position within the current stream
      */
D
Daniel Larimer 已提交
104
      inline size_t tellp()const      { return size_t(_pos - _start); }
A
arhag 已提交
105

106 107 108 109 110 111
     /**
      *  Returns the number of remaining bytes that can be read/skipped
      *  @brief Returns the number of remaining bytes that can be read/skipped
      *  @return number of remaining bytes
      */
      inline size_t remaining()const  { return _end - _pos; }
112 113 114 115 116 117
    private:
      T _start;
      T _pos;
      T _end;
};

118 119 120
/**
 *  @brief Specialization of datastream used to help determine the final size of a serialized value
 */
121 122 123
template<>
class datastream<size_t> {
   public:
124
     datastream( size_t init_size = 0):_size(init_size){}
125 126 127 128 129 130 131 132 133 134 135
     inline bool     skip( size_t s )                 { _size += s; return true;  }
     inline bool     write( const char* ,size_t s )  { _size += s; return true;  }
     inline bool     put(char )                      { ++_size; return  true;    }
     inline bool     valid()const                     { return true;              }
     inline bool     seekp(size_t p)                  { _size = p;  return true;  }
     inline size_t   tellp()const                     { return _size;             }
     inline size_t   remaining()const                 { return 0;                 }
  private:
     size_t _size;
};

136
/**
A
arhag 已提交
137 138
 *  Serialize a key256 into a stream
 *  @brief Serialize a key256
139 140 141
 *  @param ds stream to write
 *  @param d value to serialize
 */
142
template<typename Stream>
A
arhag 已提交
143 144
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const key256 d) {
  ds.write( (const char*)d.data(), d.size() );
145 146
  return ds;
}
147
/**
A
arhag 已提交
148 149
 *  Deserialize a key256 from a stream
 *  @brief Deserialize a key256
150 151 152
 *  @param ds stream to read
 *  @param d destination for deserialized value
 */
153
template<typename Stream>
A
arhag 已提交
154 155
inline datastream<Stream>& operator>>(datastream<Stream>& ds, key256& d) {
  ds.read((char*)d.data(), d.size() );
156 157 158
  return ds;
}

159

160 161 162 163 164 165
/**
 *  Serialize a uint128_t into a stream
 *  @brief Serialize a uint128_t
 *  @param ds stream to write
 *  @param d value to serialize
 */
166 167 168 169 170
template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const uint128_t d) {
  ds.write( (const char*)&d, sizeof(d) );
  return ds;
}
171 172 173 174 175 176
/**
 *  Deserialize a uint128_t from a stream
 *  @brief Deserialize a uint128_t
 *  @param ds stream to read
 *  @param d destination for deserialized value
 */
177 178 179 180 181 182
template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, uint128_t& d) {
  ds.read((char*)&d, sizeof(d) );
  return ds;
}

183 184 185 186 187 188
/**
 *  Serialize a int128_t into a stream
 *  @brief Serialize a int128_t
 *  @param ds stream to write
 *  @param d value to serialize
 */
189 190 191 192 193
template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const int128_t d) {
  ds.write( (const char*)&d, sizeof(d) );
  return ds;
}
194 195 196 197 198 199
/**
 *  Deserialize a int128_t from a stream
 *  @brief Deserialize a int128_t
 *  @param ds stream to read
 *  @param d destination for deserialized value
 */
200 201 202 203 204 205
template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, int128_t& d) {
  ds.read((char*)&d, sizeof(d) );
  return ds;
}

206 207 208 209 210 211
/**
 *  Serialize a int32_t into a stream
 *  @brief Serialize a int32_t
 *  @param ds stream to write
 *  @param d value to serialize
 */
212 213 214 215 216
template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const int32_t d) {
  ds.write( (const char*)&d, sizeof(d) );
  return ds;
}
217 218 219 220 221 222
/**
 *  Deserialize a int32_t from a stream
 *  @brief Deserialize a int32_t
 *  @param ds stream to read
 *  @param d destination for deserialized value
 */
223 224 225 226 227 228
template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, int32_t& d) {
  ds.read((char*)&d, sizeof(d) );
  return ds;
}

229 230 231 232 233 234
/**
 *  Serialize a uint32_t into a stream
 *  @brief Serialize a uint32_t
 *  @param ds stream to write
 *  @param d value to serialize
 */
235 236 237 238 239
template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const uint32_t d) {
  ds.write( (const char*)&d, sizeof(d) );
  return ds;
}
240 241 242 243 244 245
/**
 *  Deserialize a uint32_t from a stream
 *  @brief Deserialize a uint32_t
 *  @param ds stream to read
 *  @param d destination for deserialized value
 */
246 247 248 249 250 251
template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, uint32_t& d) {
  ds.read((char*)&d, sizeof(d) );
  return ds;
}

252 253 254 255 256 257 258 259 260 261 262 263 264
template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const bool& d) {
  return ds << uint8_t(d);
}
template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, bool& d) {
  uint8_t t;
  ds >> t;
  d = t;
  return ds;
}


265 266 267 268 269 270
/**
 *  Serialize a int64_t into a stream
 *  @brief Serialize a int64_t
 *  @param ds stream to write
 *  @param d value to serialize
 */
271 272 273 274 275
template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const int64_t d) {
  ds.write( (const char*)&d, sizeof(d) );
  return ds;
}
276 277 278 279 280 281
/**
 *  Deserialize a int64_t from a stream
 *  @brief Deserialize a int64_t
 *  @param ds stream to read
 *  @param d destination for deserialized value
 */
282 283 284 285 286 287
template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, int64_t& d) {
  ds.read((char*)&d, sizeof(d) );
  return ds;
}

288 289 290 291 292 293
/**
 *  Serialize a uint64_t into a stream
 *  @brief Serialize a uint64_t
 *  @param ds stream to write
 *  @param d value to serialize
 */
294
template<typename Stream>
D
Daniel Larimer 已提交
295
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const uint64_t& d) {
296 297 298
  ds.write( (const char*)&d, sizeof(d) );
  return ds;
}
D
Daniel Larimer 已提交
299

300 301 302 303 304 305
/**
 *  Deserialize a uint64_t from a stream
 *  @brief Deserialize a uint64_t
 *  @param ds stream to read
 *  @param d destination for deserialized value
 */
306 307 308 309 310 311
template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, uint64_t& d) {
  ds.read((char*)&d, sizeof(d) );
  return ds;
}

D
Daniel Larimer 已提交
312 313 314 315 316 317 318 319 320 321 322 323
template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const double& d) {
  ds.write( (const char*)&d, sizeof(d) );
  return ds;
}

template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, double& d) {
  ds.read((char*)&d, sizeof(d) );
  return ds;
}

324 325 326 327 328 329
/**
 *  Serialize a int16_t into a stream
 *  @brief Serialize a int16_t
 *  @param ds stream to write
 *  @param d value to serialize
 */
330 331 332 333 334
template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const int16_t d) {
  ds.write( (const char*)&d, sizeof(d) );
  return ds;
}
335 336 337 338 339 340
/**
 *  Deserialize a int16_t from a stream
 *  @brief Deserialize a int16_t
 *  @param ds stream to read
 *  @param d destination for deserialized value
 */
341 342 343 344 345 346
template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, int16_t& d) {
  ds.read((char*)&d, sizeof(d) );
  return ds;
}

347 348 349 350 351 352
/**
 *  Serialize a uint16_t into a stream
 *  @brief Serialize a uint16_t
 *  @param ds stream to write
 *  @param d value to serialize
 */
353 354 355 356 357
template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const uint16_t d) {
  ds.write( (const char*)&d, sizeof(d) );
  return ds;
}
358 359 360 361 362 363
/**
 *  Deserialize a uint16_t from a stream
 *  @brief Deserialize a uint16_t
 *  @param ds stream to read
 *  @param d destination for deserialized value
 */
364 365 366 367 368 369
template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, uint16_t& d) {
  ds.read((char*)&d, sizeof(d) );
  return ds;
}

370 371 372 373 374 375
/**
 *  Serialize a int8_t into a stream
 *  @brief Serialize a int8_t
 *  @param ds stream to write
 *  @param d value to serialize
 */
376 377 378 379 380
template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const int8_t d) {
  ds.write( (const char*)&d, sizeof(d) );
  return ds;
}
381 382 383 384 385 386
/**
 *  Deserialize a int8_t from a stream
 *  @brief Deserialize a int8_t
 *  @param ds stream to read
 *  @param d destination for deserialized value
 */
387 388 389 390 391 392
template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, int8_t& d) {
  ds.read((char*)&d, sizeof(d) );
  return ds;
}

393 394 395 396 397 398
/**
 *  Serialize a uint8_t into a stream
 *  @brief Serialize a uint8_t
 *  @param ds stream to write
 *  @param d value to serialize
 */
399 400 401 402 403
template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const uint8_t d) {
  ds.write( (const char*)&d, sizeof(d) );
  return ds;
}
404 405 406 407 408 409
/**
 *  Deserialize a uint8_t from a stream
 *  @brief Deserialize a uint8_t
 *  @param ds stream to read
 *  @param d destination for deserialized value
 */
410 411 412 413 414
template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, uint8_t& d) {
  ds.read((char*)&d, sizeof(d) );
  return ds;
}
415

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
template<typename DataStream>
DataStream& operator << ( DataStream& ds, const std::string& v ) {
   ds << unsigned_int( v.size() );
   for( const auto& i : v )
      ds << i;
   return ds;
}

template<typename DataStream>
DataStream& operator >> ( DataStream& ds, std::string& v ) {
   unsigned_int s;
   ds >> s;
   v.resize(s.value);
   for( auto& i : v )
      ds >> i;
   return ds;
}

D
Daniel Larimer 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447
template<typename DataStream, typename T, std::size_t N>
DataStream& operator << ( DataStream& ds, const std::array<T,N>& v ) {
   for( const auto& i : v )
      ds << i;
   return ds;
}

template<typename DataStream, typename T, std::size_t N>
DataStream& operator >> ( DataStream& ds, std::array<T,N>& v ) {
   for( auto& i : v )
      ds >> i;
   return ds;
}

D
Daniel Larimer 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
template<typename DataStream, typename K, typename V>
DataStream& operator<<( DataStream& ds, const boost::container::flat_map<K,V>& m ) {
   ds << unsigned_int( m.size() );
   for( const auto& i : m ) 
      ds << i.first << i.second;
   return ds;
}
template<typename DataStream, typename K, typename V>
DataStream& operator>>( DataStream& ds, boost::container::flat_map<K,V>& m ) {
   m.clear();
   unsigned_int s; ds >> s;

   for( uint32_t i = 0; i < s.value; ++i ) {
      K k; V v;
      ds >> k >> v;
      m.emplace( std::move(k), std::move(v) );
   }
   return ds;
}


D
Daniel Larimer 已提交
469

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
template<typename DataStream, typename T>
DataStream& operator << ( DataStream& ds, const vector<T>& v ) {
   ds << unsigned_int( v.size() );
   for( const auto& i : v )
      ds << i;
   return ds;
}

template<typename DataStream, typename T>
DataStream& operator >> ( DataStream& ds, vector<T>& v ) {
   unsigned_int s;
   ds >> s;
   v.resize(s.value);
   for( auto& i : v )
      ds >> i;
   return ds;
}

template<typename T>
T unpack( const char* buffer, size_t len ) {
   T result;
   datastream<const char*> ds(buffer,len);
   ds >> result;
   return result;
}

template<typename T>
size_t pack_size( const T& value ) {
A
arhag 已提交
498
  datastream<size_t> ps;
499 500 501 502 503 504 505 506 507 508 509 510 511 512
  ps << value;
  return ps.tellp();
}

template<typename T>
bytes pack( const T& value ) {
  bytes result;
  result.resize(pack_size(value));

  datastream<char*> ds( result.data(), result.size() );
  ds << value;
  return result;
}

K
Khaled Al-Hassanieh 已提交
513
template<typename Stream>
514 515
inline eosio::datastream<Stream>& operator<<(eosio::datastream<Stream>& ds, const public_key pk) {
   ds.write((const char*)&pk, sizeof(pk));
K
Khaled Al-Hassanieh 已提交
516 517
   return ds;
}
518

K
Khaled Al-Hassanieh 已提交
519
template<typename Stream>
520 521 522 523 524
inline eosio::datastream<Stream>& operator>>(eosio::datastream<Stream>& ds, public_key& pk) {
   ds.read((char*)&pk, sizeof(pk));
   return ds;
}

525 526
template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const checksum160& cs) {
527
   ds.write((const char*)&cs, sizeof(cs));
K
Khaled Al-Hassanieh 已提交
528 529 530
   return ds;
}

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, checksum160& cs) {
   ds.read((char*)&cs, sizeof(cs));
   return ds;
}

template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const checksum256& cs) {
   ds.write((const char*)&cs, sizeof(cs));
   return ds;
}
   
template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, checksum256& cs) {
   ds.read((char*)&cs, sizeof(cs));
   return ds;
}

template<typename Stream>
inline datastream<Stream>& operator<<(datastream<Stream>& ds, const checksum512& cs) {
   ds.write((const char*)&cs, sizeof(cs));
   return ds;
}

template<typename Stream>
inline datastream<Stream>& operator>>(datastream<Stream>& ds, checksum512& cs) {
557 558 559
   ds.read((char*)&cs, sizeof(cs));
   return ds;
}
K
Khaled Al-Hassanieh 已提交
560

A
Anton Perkov 已提交
561
}