diff --git a/src/SerializedTypes.cpp b/src/SerializedTypes.cpp new file mode 100644 index 0000000000..793192e194 --- /dev/null +++ b/src/SerializedTypes.cpp @@ -0,0 +1,101 @@ + +#include + +#include "SerializedTypes.h" +#include "SerializedObject.h" + +STUInt8* STUInt8::construct(SerializerIterator& u) +{ + return new STUInt8(u.get8()); +} + +std::string STUInt8::getText() const +{ + return boost::lexical_cast(value); +} + +STUInt16* STUInt16::construct(SerializerIterator& u) +{ + return new STUInt16(u.get16()); +} + +std::string STUInt16::getText() const +{ + return boost::lexical_cast(value); +} + +STUInt32* STUInt32::construct(SerializerIterator& u) +{ + return new STUInt32(u.get32()); +} + +std::string STUInt32::getText() const +{ + return boost::lexical_cast(value); +} + +STUInt64* STUInt64::construct(SerializerIterator& u) +{ + return new STUInt64(u.get64()); +} + +std::string STUInt64::getText() const +{ + return boost::lexical_cast(value); +} + +STUHash160* STUHash160::construct(SerializerIterator& u) +{ + return new STUHash160(u.get160()); +} + +std::string STUHash160::getText() const +{ + return value.GetHex(); +} + +STUHash256* STUHash256::construct(SerializerIterator& u) +{ + return new STUHash256(u.get256()); +} + +std::string STUHash256::getText() const +{ + return value.GetHex(); +} + +static std::string hex(const std::vector& value) +{ + int dlen=value.size(), i=0; + char psz[dlen*2 + 1]; + for(std::vector::const_iterator it=value.begin(), end=value.end(); it!=end; ++it) + sprintf(psz + 2*(i++), "%02X", *it); + return std::string(psz, psz + value.size()*2); +} + +std::string STUVariableLength::getText() const +{ + return hex(value); +} + +STUVariableLength* STUVariableLength::construct(SerializerIterator& u) +{ + return new STUVariableLength(u.getVL()); +} + +std::string STUTaggedList::getText() const +{ + std::string ret; + for(std::list::const_iterator it=value.begin(); it!=value.end(); ++it) + { + ret+=boost::lexical_cast(it->first); + ret+=","; + ret+=hex(it->second); + } + return ret; +} + +STUTaggedList* STUTaggedList::construct(SerializerIterator& u) +{ + return new STUTaggedList(u.getTaggedList()); +} diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index 1a454072f6..e9280e5fc1 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -26,10 +26,8 @@ public: virtual SerializedType* duplicate() const { return new SerializedType(); } virtual std::string getText() const { return std::string(); } - virtual std::string getSQL() const { return std::string(); } - virtual std::vector serialize() const; - virtual int add(std::vector&) const { return 0; } + virtual void add(Serializer& s) const { return; } SerializedType* new_clone(const SerializedType& s) { return s.duplicate(); } void delete_clone(const SerializedType* s) { boost::checked_delete(s); } @@ -43,14 +41,13 @@ protected: public: STUInt8(unsigned char v=0) : value(v) { ; } - static STUInt8* construct(const std::vector&, int start_offset, int& length_consumed); + static STUInt8* construct(SerializerIterator&); int getLength() const { return 1; } SerializedTypeID getType() const { return STI_UINT8; } STUInt8 *duplicate() const { return new STUInt8(value); } std::string getText() const; - std::string getSQL() const; - virtual int add(std::vector&) const; + virtual void add(Serializer& s) const { s.add8(value); } unsigned char getValue() const { return value; } void setValue(unsigned char v) { value=v; } @@ -67,14 +64,13 @@ protected: public: STUInt16(uint16 v=0) : value(v) { ; } - static STUInt16* construct(const std::vector&, int start_offset, int& length_consumed); + static STUInt16* construct(SerializerIterator&); int getLength() const { return 2; } SerializedTypeID getType() const { return STI_UINT16; } STUInt16 *duplicate() const { return new STUInt16(value); } std::string getText() const; - std::string getSQL() const; - virtual int add(std::vector&) const; + virtual void add(Serializer& s) const { s.add16(value); } uint16 getValue() const { return value; } void setValue(uint16 v) { value=v; } @@ -91,14 +87,13 @@ protected: public: STUInt32(uint32 v=0) : value(v) { ; } - static STUInt32* construct(const std::vector&, int start_offset, int& length_consumed); + static STUInt32* construct(SerializerIterator&); int getLength() const { return 4; } SerializedTypeID getType() const { return STI_UINT32; } STUInt32 *duplicate() const { return new STUInt32(value); } std::string getText() const; - std::string getSQL() const; - virtual int add(std::vector&) const; + virtual void add(Serializer& s) const { s.add32(value); } uint32 getValue() const { return value; } void setValue(uint32 v) { value=v; } @@ -115,14 +110,13 @@ protected: public: STUInt64(uint64 v=0) : value(v) { ; } - static STUInt64* construct(const std::vector&, int start_offset, int& length_consumed); + static STUInt64* construct(SerializerIterator&); int getLength() const { return 8; } SerializedTypeID getType() const { return STI_UINT64; } STUInt64 *duplicate() const { return new STUInt64(value); } std::string getText() const; - std::string getSQL() const; - virtual int add(std::vector&) const; + virtual void add(Serializer& s) const { s.add64(value); } uint64 getValue() const { return value; } void setValue(uint64 v) { value=v; } @@ -140,14 +134,13 @@ public: STUHash160(const uint160& v) : value(v) { ; } STUHash160() { ; } - static STUHash160* construct(const std::vector&, int start_offset, int& length_consumed); + static STUHash160* construct(SerializerIterator&); int getLength() const { return 20; } SerializedTypeID getType() const { return STI_HASH160; } STUHash160 *duplicate() const { return new STUHash160(value); } std::string getText() const; - std::string getSQL() const; - virtual int add(std::vector&) const; + virtual void add(Serializer& s) const { s.add160(value); } const uint160& getValue() const { return value; } void setValue(const uint160& v) { value=v; } @@ -165,14 +158,13 @@ public: STUHash256(const uint256& v) : value(v) { ; } STUHash256() { ; } - static STUHash256* construct(const std::vector&, int start_offset, int& length_consumed); + static STUHash256* construct(SerializerIterator&); int getLength() const { return 32; } SerializedTypeID getType() const { return STI_HASH256; } STUHash256 *duplicate() const { return new STUHash256(value); } std::string getText() const; - std::string getSQL() const; - virtual int add(std::vector&) const; + virtual void add(Serializer& s) const { s.add256(value); } const uint256& getValue() const { return value; } void setValue(const uint256& v) { value=v; } @@ -190,14 +182,13 @@ public: STUVariableLength(const std::vector& v) : value(v) { ; } STUVariableLength() { ; } - static STUVariableLength* construct(const std::vector&, int start_offset, int& length_consumed); + static STUVariableLength* construct(SerializerIterator&); int getLength() const; SerializedTypeID getType() const { return STI_VL; } STUVariableLength *duplicate() const { return new STUVariableLength(value); } std::string getText() const; - std::string getSQL() const; - virtual int add(std::vector&) const; + virtual void add(Serializer& s) const { s.addVL(value); } const std::vector& peekValue() const { return value; } std::vector& peekValue() { return value; } @@ -217,14 +208,13 @@ public: STUTaggedList(const std::list& v) : value(v) { ; } STUTaggedList() { ; } - static STUTaggedList* construct(const std::vector&, int start_offset, int& length_consumed); + static STUTaggedList* construct(SerializerIterator&); int getLength() const; SerializedTypeID getType() const { return STI_TL; } STUTaggedList *duplicate() const { return new STUTaggedList(value); } std::string getText() const; - std::string getSQL() const; - virtual int add(std::vector&) const; + virtual void add(Serializer& s) const { s.addTaggedList(value); } const std::list& peekValue() const { return value; } std::list& peekValue() { return value; } diff --git a/src/Serializer.cpp b/src/Serializer.cpp index 6c083d7075..9d7351e95e 100644 --- a/src/Serializer.cpp +++ b/src/Serializer.cpp @@ -257,6 +257,20 @@ int Serializer::addTaggedList(const std::list& list) return ret; } +int Serializer::addTaggedList(const std::vector& list) +{ + int size=list.size(); + if(size>255) return -1; + int ret=add8(size); + if(size!=0) + for(std::vector::const_iterator it=list.begin(); it!=list.end(); ++it) + { + add8(it->first); + addVL(it->second); + } + return ret; +} + bool Serializer::getVL(std::vector& objectVL, int offset, int& length) const { int b1; diff --git a/src/Serializer.h b/src/Serializer.h index 3f7efd5a1e..702be674f6 100644 --- a/src/Serializer.h +++ b/src/Serializer.h @@ -38,6 +38,7 @@ class Serializer int addVL(const std::vector &vector); int addVL(const void *ptr, int len); int addTaggedList(const std::list&); + int addTaggedList(const std::vector&); // disassemble functions bool get8(int&, int offset) const;