37 for (
unsigned i = 0; i < 32; ++i)
38 ret[i] = i < _s.size() ? _s[i] : 0;
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> 49 static bytes serialise(std::string const& _t) 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)); 58 inline bytes abiInAux() { return {}; } 59 template <class T, class ... U> bytes abiInAux(T const& _t, U const& ... _u) 61 return ABISerialiser<T>::serialise(_t) + abiInAux(_u ...); 64 template <class ... T> bytes abiIn(std::string _id, T const& ... _t) 66 return sha3(_id).ref().cropped(0, 4).toBytes() + abiInAux(_t ...); 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> 76 static std::string deserialise(bytesConstRef& io_t) 78 unsigned o = (uint16_t)u256(h256(io_t.cropped(0, 32))); 79 unsigned s = (uint16_t)u256(h256(io_t.cropped(o, 32))); 82 io_t.cropped(o + 32, s).populate(bytesRef((byte*)ret.data(), s)); 83 io_t = io_t.cropped(32); 88 template <class T> T abiOut(bytes const& _data) 90 bytesConstRef o(&_data); 91 return ABIDeserialiser<T>::deserialise(o); 94 template <class T> T abiOut(bytesConstRef& _data) 96 return ABIDeserialiser<T>::deserialise(_data); string32 toString32(std::string const &_s)
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
std::vector< byte > bytes
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
std::array< char, 32 > string32
static bytes serialise(FixedHash< N > const &_t)