Fabcoin Core  0.16.2
P2P Digital Currency
ABI.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #pragma once
23 
24 #include <libdevcore/Common.h>
25 #include <libdevcore/FixedHash.h>
26 #include <libdevcore/CommonData.h>
27 #include <libdevcore/SHA3.h>
28 
29 namespace dev
30 {
31 namespace eth
32 {
33 
34 inline string32 toString32(std::string const& _s)
35 {
36  string32 ret;
37  for (unsigned i = 0; i < 32; ++i)
38  ret[i] = i < _s.size() ? _s[i] : 0;
39  return ret;
40 }
41 
42 template <class T> struct ABISerialiser {};
43 template <unsigned N> struct ABISerialiser<FixedHash<N>> { static bytes serialise(FixedHash<N> const& _t) { static_assert(N <= 32, "Cannot serialise hash > 32 bytes."); static_assert(N > 0, "Cannot serialise zero-length hash."); return bytes(32 - N, 0) + _t.asBytes(); } };
44 template <> struct ABISerialiser<u256> { static bytes serialise(u256 const& _t) { return h256(_t).asBytes(); } };
45 template <> struct ABISerialiser<u160> { static bytes serialise(u160 const& _t) { return bytes(12, 0) + h160(_t).asBytes(); } };
46 template <> struct ABISerialiser<string32> { static bytes serialise(string32 const& _t) { bytes ret; bytesConstRef((byte const*)_t.data(), 32).populate(bytesRef(&ret)); return ret; } };
47 template <> struct ABISerialiser<std::string>
48 {
49  static bytes serialise(std::string const& _t)
50  {
51  bytes ret = h256(u256(32)).asBytes() + h256(u256(_t.size())).asBytes();
52  ret.resize(ret.size() + (_t.size() + 31) / 32 * 32);
53  bytesConstRef(&_t).populate(bytesRef(&ret).cropped(64));
54  return ret;
55  }
56 };
57 
58 inline bytes abiInAux() { return {}; }
59 template <class T, class ... U> bytes abiInAux(T const& _t, U const& ... _u)
60 {
61  return ABISerialiser<T>::serialise(_t) + abiInAux(_u ...);
62 }
63 
64 template <class ... T> bytes abiIn(std::string _id, T const& ... _t)
65 {
66  return sha3(_id).ref().cropped(0, 4).toBytes() + abiInAux(_t ...);
67 }
68 
69 template <class T> struct ABIDeserialiser {};
70 template <unsigned N> struct ABIDeserialiser<FixedHash<N>> { static FixedHash<N> deserialise(bytesConstRef& io_t) { static_assert(N <= 32, "Parameter sizes must be at most 32 bytes."); FixedHash<N> ret; io_t.cropped(32 - N, N).populate(ret.ref()); io_t = io_t.cropped(32); return ret; } };
71 template <> struct ABIDeserialiser<u256> { static u256 deserialise(bytesConstRef& io_t) { u256 ret = fromBigEndian<u256>(io_t.cropped(0, 32)); io_t = io_t.cropped(32); return ret; } };
72 template <> struct ABIDeserialiser<u160> { static u160 deserialise(bytesConstRef& io_t) { u160 ret = fromBigEndian<u160>(io_t.cropped(12, 20)); io_t = io_t.cropped(32); return ret; } };
73 template <> struct ABIDeserialiser<string32> { static string32 deserialise(bytesConstRef& io_t) { string32 ret; io_t.cropped(0, 32).populate(bytesRef((byte*)ret.data(), 32)); io_t = io_t.cropped(32); return ret; } };
74 template <> struct ABIDeserialiser<std::string>
75 {
76  static std::string deserialise(bytesConstRef& io_t)
77  {
78  unsigned o = (uint16_t)u256(h256(io_t.cropped(0, 32)));
79  unsigned s = (uint16_t)u256(h256(io_t.cropped(o, 32)));
80  std::string ret;
81  ret.resize(s);
82  io_t.cropped(o + 32, s).populate(bytesRef((byte*)ret.data(), s));
83  io_t = io_t.cropped(32);
84  return ret;
85  }
86 };
87 
88 template <class T> T abiOut(bytes const& _data)
89 {
90  bytesConstRef o(&_data);
91  return ABIDeserialiser<T>::deserialise(o);
92 }
93 
94 template <class T> T abiOut(bytesConstRef& _data)
95 {
96  return ABIDeserialiser<T>::deserialise(_data);
97 }
98 
99 }
100 }
string32 toString32(std::string const &_s)
Definition: ABI.h:34
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::vector< byte > bytes
Definition: Common.h:75
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
std::array< char, 32 > string32
Definition: Common.h:150
static bytes serialise(FixedHash< N > const &_t)
Definition: ABI.h:43