From c2392fd81af9fbf45e7b44034a955673db62a75e Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 27 Mar 2015 09:57:00 +0100 Subject: [PATCH] Gaa. Get them out of that namespace! --- abi/main.cpp | 55 ++++++++++++++++++++++++++++++++++++++++------- libethereum/ABI.h | 8 +++---- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/abi/main.cpp b/abi/main.cpp index 223ff8283..22233f1f0 100644 --- a/abi/main.cpp +++ b/abi/main.cpp @@ -162,6 +162,8 @@ struct ABIType { if (base == Base::Fixed) size = ssize = 16; + else if (base == Base::Address || base == Base::Bytes) + size = 0; else size = 32; return; @@ -188,7 +190,7 @@ struct ABIType string ret; switch (base) { - case Base::Bytes: ret = "bytes" + toString(size); break; + case Base::Bytes: ret = "bytes" + (size > 0 ? toString(size) : ""); break; case Base::Address: ret = "address"; break; case Base::Int: ret = "int" + toString(size); break; case Base::Uint: ret = "uint" + toString(size); break; @@ -361,15 +363,30 @@ struct ABIMethod { for (unsigned j = 0; j < inArity[ii]; ++j) { - ret += aligned(_params[pi].first, i, _params[pi].second, (i.base == Base::Bytes && i.size == 1) ? 1 : 32); + if (i.base == Base::Bytes && !i.size) + { + ret += _params[pi].first; + while (ret.size() % 32 != 0) + ret.push_back(0); + } + else + ret += aligned(_params[pi].first, i, _params[pi].second, 32); ++pi; } ++ii; - while (ret.size() % 32 != 0) - ret.push_back(0); } return ret; } + string decode(bytes const& _data, int _index = -1) + { + stringstream out; + if (_index == -1) + out << "["; + (void)_data; + if (_index == -1) + out << "]"; + return out.str(); + } }; string canonSig(string const& _name, vector const& _args) @@ -517,10 +534,7 @@ int main(int argc, char** argv) } string abiData; - if (abiFile == "--") - for (int i = cin.get(); i != -1; i = cin.get()) - abiData.push_back((char)i); - else if (!abiFile.empty()) + if (abiFile.empty()) abiData = contentsString(abiFile); if (mode == Mode::Encode) @@ -546,6 +560,31 @@ int main(int argc, char** argv) } else if (mode == Mode::Decode) { + if (abiData.empty()) + { + cerr << "Please specify an ABI file." << endl; + exit(-1); + } + else + { + ABI abi(abiData); + ABIMethod m; + if (verbose) + cerr << "ABI:" << endl << abi; + try { + m = abi.method(method, args); + } + catch(...) + { + cerr << "Unknown method in ABI." << endl; + exit(-1); + } + string encoded; + for (int i = cin.get(); i != -1; i = cin.get()) + encoded.push_back((char)i); + cout << m.decode(fromHex(encoded)); + } + // TODO: read abi to determine output format. (void)encoding; (void)clearZeroes; diff --git a/libethereum/ABI.h b/libethereum/ABI.h index fabda4dc2..f5cab10b2 100644 --- a/libethereum/ABI.h +++ b/libethereum/ABI.h @@ -21,15 +21,15 @@ #pragma once +#include +#include +#include + namespace dev { namespace eth { -#include -#include -#include - template struct ABISerialiser {}; template struct ABISerialiser> { static bytes serialise(FixedHash 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(); } }; template <> struct ABISerialiser { static bytes serialise(u256 const& _t) { return h256(_t).asBytes(); } };