diff --git a/CMakeLists.txt b/CMakeLists.txt index 91d01b168..e5a211420 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,5 @@ else () message(FATAL_ERROR "Your C++ compiler does not support C++11.") endif () - aux_source_directory(. SRC_LIST) add_executable(${PROJECT_NAME} ${SRC_LIST}) diff --git a/Common.cpp b/Common.cpp new file mode 100644 index 000000000..910cd233e --- /dev/null +++ b/Common.cpp @@ -0,0 +1,2 @@ +#include "Common.h" + diff --git a/Common.h b/Common.h new file mode 100644 index 000000000..e4bcd22d7 --- /dev/null +++ b/Common.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include "foreign.h" + +namespace eth +{ + +typedef uint8_t byte; +typedef foreign Bytes; + +template std::string toString(_T const& _t) { std::ostringstream o; o << _t; return o.str(); } + +} diff --git a/RLP.cpp b/RLP.cpp new file mode 100644 index 000000000..071d0b72e --- /dev/null +++ b/RLP.cpp @@ -0,0 +1,2 @@ +#include "RLP.h" + diff --git a/RLP.h b/RLP.h new file mode 100644 index 000000000..e4b83baa9 --- /dev/null +++ b/RLP.h @@ -0,0 +1,170 @@ +#pragma once + +#include +#include +#include +#include +#include "foreign.h" +#include "Common.h" + +namespace eth +{ + +class RLP; +typedef std::vector RLPs; +using bigint = boost::multiprecision::cpp_int; +using uint = uint64_t; +using sint = int64_t; + +class RLP +{ +public: + RLP() {} + RLP(Bytes _d): m_data(_d) {} + + explicit operator bool() const { return !isNull(); } + + /// No value. + bool isNull() const { return m_data.size() == 0; } + + /// String value. + bool isString() const { assert(!isNull()); return m_data[0] >= 0x40 && m_data[0] < 0x80; } + + /// List value. + bool isList() const { assert(!isNull()); return m_data[0] >= 0x80 && m_data[0] < 0xc0; } + + /// Integer value. Either isNormalInt() or isBigInt(). + bool isInt() const { assert(!isNull()); return m_data[0] < 0x40; } + + /// Fits into eth::uint type. Can use toInt() to read. + bool isNormalInt() const { assert(!isNull()); return m_data[0] < 0x18; } + + /// Fits only into eth::bigint type. Use only toBigInt() to read. + bool isBigInt() const { assert(!isNull()); return m_data[0] < 0x40 && m_data[0] >= 0x18; } + + std::string toString() const + { + if (!isString()) + return std::string(); + return payload().cropped(0, items()).toString(); + } + + uint toInt(uint _def = 0) const + { + if (!isInt()) + return _def; + if (isDirectValueInt()) + return m_data[0]; + uint ret = 0; + auto s = intSize(); + for (uint i = 0; i < s; ++i) + ret = (ret << 8) | m_data[i + 1]; + return ret; + } + + bigint toBigInt(bigint _def = 0) const + { + if (!isInt()) + return _def; + if (isDirectValueInt()) + return m_data[0]; + bigint ret = 0; + auto s = intSize() - intLengthSize(); + uint l = 1 + intLengthSize(); + for (uint i = 0; i < s; ++i) + ret = (ret << 8) | m_data[i + l]; + return ret; + } + + RLPs toList() const + { + RLPs ret; + if (!isList()) + return ret; + uint64_t c = items(); + Bytes d = payload(); + for (uint64_t i = 0; i < c; ++i, d = d.cropped(RLP(d).size())) + ret.push_back(RLP(d)); + return ret; + } + +private: + /// Direct value integer. + bool isDirectValueInt() const { assert(!isNull()); return m_data[0] < 0x18; } + + /// Indirect-value integer. + bool isIndirectValueInt() const { assert(!isNull()); return m_data[0] >= 0x18 && m_data[0] < 0x38; } + + /// Indirect addressed integer. + bool isIndirectAddressedInt() const { assert(!isNull()); return m_data[0] < 0x40 && m_data[0] >= 0x38; } + + /// Direct-length string. + bool isSmallString() const { assert(!isNull()); return m_data[0] >= 0x40 && m_data[0] < 0x78; } + + /// Direct-length list. + bool isSmallList() const { assert(!isNull()); return m_data[0] >= 0x80 && m_data[0] < 0xb8; } + + uint size() const + { + if (isInt()) + return 1 + intSize(); + if (isString()) + return payload().data() - m_data.data() + items(); + if (isList()) + { + Bytes d = payload(); + uint64_t c = items(); + for (uint64_t i = 0; i < c; ++i, d = d.cropped(RLP(d).size())) {} + return d.data() - m_data.data(); + } + return 0; + } + + uint intLengthSize() const { return isIndirectAddressedInt() ? m_data[0] - 0x37 : 0; } + uint intSize() const { return (!isInt() || isDirectValueInt()) ? 0 : isIndirectAddressedInt() ? intLengthSize() + items() : (m_data[0] - 0x17); } + + uint items() const + { + auto n = (m_data[0] & 0x3f); + if (n < 0x38) + return n; + uint ret = 0; + for (int i = 0; i < n - 0x37; ++i) + ret = (ret << 8) | m_data[i + 1]; + return ret; + } + + Bytes payload() const + { + assert(isString() || isList()); + auto n = (m_data[0] & 0x3f); + return m_data.cropped(1 + (n < 0x38 ? 0 : (n - 0x37))); + } + + Bytes m_data; +}; + +} + +inline std::ostream& operator<<(std::ostream& _out, eth::RLP _d) +{ + if (_d.isNull()) + _out << "null"; + else if (_d.isBigInt()) + _out << _d.toBigInt(); + else if (_d.isInt()) + _out << _d.toInt(); + else if (_d.isString()) + _out << "\"" << _d.toString() << "\""; + else if (_d.isList()) + { + _out << "["; + int j = 0; + for (auto i: _d.toList()) + _out << (j++ ? ", " : " ") << i; + _out << " ]"; + } + + return _out; +} + diff --git a/foreign.h b/foreign.h new file mode 100644 index 000000000..c76bc5504 --- /dev/null +++ b/foreign.h @@ -0,0 +1,50 @@ +#pragma once + +#include +#include +#include + +namespace eth +{ + +template +class foreign +{ +public: + typedef _T value_type; + typedef _T element_type; + + foreign(): m_data(nullptr), m_count(0) {} + foreign(std::vector::type>* _data): m_data(_data->data()), m_count(_data->size()) {} + foreign(_T* _data, unsigned _count): m_data(_data), m_count(_count) {} + + explicit operator bool() const { return m_data && m_count; } + + std::vector<_T> toVector() const { return std::vector<_T>(m_data, m_data + m_count); } + std::string toString() const { return std::string((char const*)m_data, ((char const*)m_data) + m_count); } + template operator foreign<_T2>() const { assert(m_count * sizeof(_T) / sizeof(_T2) * sizeof(_T2) / sizeof(_T) == m_count); return foreign<_T2>((_T2*)m_data, m_count * sizeof(_T) / sizeof(_T2)); } + + _T* data() const { return m_data; } + unsigned count() const { return m_count; } + unsigned size() const { return m_count; } + foreign<_T> next() const { return foreign<_T>(m_data + m_count, m_count); } + foreign<_T> cropped(unsigned _begin, int _count = -1) const { if (m_data && _begin + std::max(0, _count) <= m_count) return foreign<_T>(m_data + _begin, _count < 0 ? m_count - _begin : _count); else return foreign<_T>(); } + void retarget(_T const* _d, size_t _s) { m_data = _d; m_count = _s; } + void retarget(std::vector<_T> const& _t) { m_data = _t.data(); m_count = _t.size(); } + + _T* begin() { return m_data; } + _T* end() { return m_data + m_count; } + _T const* begin() const { return m_data; } + _T const* end() const { return m_data + m_count; } + + _T& operator[](unsigned _i) { assert(m_data); assert(_i < m_count); return m_data[_i]; } + _T const& operator[](unsigned _i) const { assert(m_data); assert(_i < m_count); return m_data[_i]; } + + void reset() { m_data = nullptr; m_count = 0; } + +private: + _T* m_data; + unsigned m_count; +}; + +} diff --git a/main.cpp b/main.cpp index 890e5a87d..7a8dcdb4a 100644 --- a/main.cpp +++ b/main.cpp @@ -1,233 +1,45 @@ -#include -#include -#include -#include -#include -#include -#include - -typedef uint8_t byte; - -template -class foreign -{ -public: - typedef _T value_type; - typedef _T element_type; - - foreign(): m_data(nullptr), m_count(0) {} - foreign(std::vector::type>* _data): m_data(_data->data()), m_count(_data->size()) {} - foreign(_T* _data, unsigned _count): m_data(_data), m_count(_count) {} - foreign(std::shared_ptr::type> > const& _data): m_data(_data->data()), m_count(_data->size()) {} - - explicit operator bool() const { return m_data && m_count; } - - std::vector<_T> toVector() const { return std::vector<_T>(m_data, m_data + m_count); } - std::string toString() const { return std::string((char const*)m_data, ((char const*)m_data) + m_count); } - template operator foreign<_T2>() const { assert(m_count * sizeof(_T) / sizeof(_T2) * sizeof(_T2) / sizeof(_T) == m_count); return foreign<_T2>((_T2*)m_data, m_count * sizeof(_T) / sizeof(_T2)); } - - _T* data() const { return m_data; } - unsigned count() const { return m_count; } - unsigned size() const { return m_count; } - foreign<_T> next() const { return foreign<_T>(m_data + m_count, m_count); } - foreign<_T> cropped(unsigned _begin, int _count = -1) const { if (m_data && _begin + std::max(0, _count) <= m_count) return foreign<_T>(m_data + _begin, _count < 0 ? m_count - _begin : _count); else return foreign<_T>(); } - void retarget(_T const* _d, size_t _s) { m_data = _d; m_count = _s; } - void retarget(std::vector<_T> const& _t) { m_data = _t.data(); m_count = _t.size(); } - - _T* begin() { return m_data; } - _T* end() { return m_data + m_count; } - _T const* begin() const { return m_data; } - _T const* end() const { return m_data + m_count; } - - _T& operator[](unsigned _i) { assert(m_data); assert(_i < m_count); return m_data[_i]; } - _T const& operator[](unsigned _i) const { assert(m_data); assert(_i < m_count); return m_data[_i]; } - - void reset() { m_data = nullptr; m_count = 0; } - -private: - _T* m_data; - unsigned m_count; -}; - -typedef foreign Bytes; -class RLP; -typedef std::vector RLPs; - -class RLP -{ -public: - RLP() {} - RLP(Bytes _d): m_data(_d) {} - - explicit operator bool() const { return !isNull(); } - bool isNull() const { return m_data.size() == 0; } - bool isString() const { assert(!isNull()); return m_data[0] >= 0x40 && m_data[0] < 0x80; } - bool isList() const { assert(!isNull()); return m_data[0] >= 0x80 && m_data[0] < 0xc0; } - bool isInt() const { assert(!isNull()); return m_data[0] < 0x40; } - bool isSmallString() const { assert(!isNull()); return m_data[0] >= 0x40 && m_data[0] < 0x78; } - bool isSmallList() const { assert(!isNull()); return m_data[0] >= 0x80 && m_data[0] < 0xb8; } - bool isSmallInt() const { assert(!isNull()); return m_data[0] < 0x20; } - int intSize() const { return (!isInt() || isSmallInt()) ? 0 : (m_data[0] - 0x1f); } - - uint64_t size() const - { - if (isInt()) - return 1 + intSize(); - if (isString()) - return payload().data() - m_data.data() + items(); - if (isList()) - { - Bytes d = payload(); - uint64_t c = items(); - for (uint64_t i = 0; i < c; ++i, d = d.cropped(RLP(d).size())) {} - return d.data() - m_data.data(); - } - return 0; - } - - std::string toString() const - { - if (!isString()) - return std::string(); - return payload().cropped(0, items()).toString(); - } - - uint64_t toInt(uint64_t _def = 0) const - { - if (!isInt()) - return _def; - if (isSmallInt()) - return m_data[0]; - uint64_t ret = 0; - auto s = intSize(); - for (int i = 0; i < s; ++i) - ret = (ret << 8) | m_data[i + 1]; - return ret; - } - - RLPs toList() const - { - RLPs ret; - if (!isList()) - return ret; - uint64_t c = items(); - Bytes d = payload(); - for (uint64_t i = 0; i < c; ++i, d = d.cropped(RLP(d).size())) - ret.push_back(RLP(d)); - return ret; - } - -/* static uint64_t intLength(Bytes _b) - { - return _b[0] < 253 ? 1 : _b[0] == 253 ? 3 : _b[0] == 254 ? 5 : 9; - } - - static uint64_t toInt(Bytes _b) - { - return _b[0] < 253 ? - _b[0] : - _b[0] == 253 ? - (((uint64_t)_b[1]) << 8) | - _b[2] : - _b[0] == 254 ? - (((uint64_t)_b[1]) << 24) | - (((uint64_t)_b[2]) << 16) | - (((uint64_t)_b[3]) << 8) | - _b[4] - : ( - (((uint64_t)_b[1]) << 56) | - (((uint64_t)_b[2]) << 48) | - (((uint64_t)_b[3]) << 40) | - (((uint64_t)_b[4]) << 32) | - (((uint64_t)_b[5]) << 24) | - (((uint64_t)_b[6]) << 16) | - (((uint64_t)_b[7]) << 8) | - _b[8] - ); - }*/ - -private: - uint64_t items() const - { - assert(isString() || isList()); - auto n = (m_data[0] & 0x3f); - if (n < 0x38) - return n; - uint64_t ret = 0; - for (int i = 0; i < n - 0x37; ++i) - ret = (ret << 8) | m_data[i + 1]; - return ret; - } - - Bytes payload() const - { - assert(isString() || isList()); - auto n = (m_data[0] & 0x3f); - return m_data.cropped(1 + (n < 0x38 ? 0 : (n - 0x37))); - } - - Bytes m_data; -}; - -std::ostream& operator<<(std::ostream& _out, RLP _d) -{ - if (_d.isNull()) - _out << "null"; - else if (_d.isInt()) - _out << _d.toInt(); - else if (_d.isString()) - _out << "\"" << _d.toString() << "\""; - else if (_d.isList()) - { - _out << "["; - int j = 0; - for (auto i: _d.toList()) - _out << (j++ ? ", " : " ") << i; - _out << " ]"; - } - - return _out; -} - +#include "RLP.h" using namespace std; +using namespace eth; int main() { { string t = "\x0f"; - cout << RLP(Bytes((byte*)t.data(), t.size())) << endl; - // 15 + assert(toString(RLP(Bytes((byte*)t.data(), t.size()))) == "15"); } { string t = "\x43""dog"; - cout << RLP(Bytes((byte*)t.data(), t.size())) << endl; - // "dog" + assert(toString(RLP(Bytes((byte*)t.data(), t.size()))) == "\"dog\""); } { string t = "\x82\x0f\x43""dog"; - cout << RLP(Bytes((byte*)t.data(), t.size())) << endl; - // [ 15, "dog" ] + assert(toString(RLP(Bytes((byte*)t.data(), t.size()))) == "[ 15, \"dog\" ]"); + } + { + string t = "\x18\x45"; + assert(toString(RLP(Bytes((byte*)t.data(), t.size()))) == "69"); } { - string t = "\x20\x45"; - cout << RLP(Bytes((byte*)t.data(), t.size())) << endl; - // 69 + string t = "\x19\x01\x01"; + assert(toString(RLP(Bytes((byte*)t.data(), t.size()))) == "257"); } { - string t = "\x21\x2a\x45"; - cout << hex << RLP(Bytes((byte*)t.data(), t.size())) << endl; - // 2a45 + string t = "\x37\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"; + ostringstream o; + o << hex << RLP(Bytes((byte*)t.data(), t.size())); + assert(o.str() == "100102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"); } { - string t = "\x3f\x01\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"; - cout << hex << RLP(Bytes((byte*)t.data(), t.size())) << endl; - // 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f - // Well... not yet - we can only have 64-bit ints currently - need a bigint class. + // 33-byte int + string t = "\x38\x21\x20\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"; + ostringstream o; + o << hex << RLP(Bytes((byte*)t.data(), t.size())); + assert(o.str() == "20100102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"); } { string t = "\x78\x38""Lorem ipsum dolor sit amet, consectetur adipisicing elit"; - cout << RLP(Bytes((byte*)t.data(), t.size())) << endl; - // "Lorem ipsum dolor sit amet, consectetur adipisicing elit" + assert(toString(RLP(Bytes((byte*)t.data(), t.size()))) == "\"Lorem ipsum dolor sit amet, consectetur adipisicing elit\""); } return 0; }