diff --git a/src/SerializedObject.h b/src/SerializedObject.h index abc42c42c..e6245853f 100644 --- a/src/SerializedObject.h +++ b/src/SerializedObject.h @@ -37,9 +37,9 @@ public: SerializedTypeID getType() const { return STI_OBJECT; } STUObject* duplicate() const { return new STUObject(*this); } - std::vector serialize() const; + void add(Serializer& s) const; std::string getText() const; - std::string getSQL() const; + void addObject(const SerializedType& t) { data.push_back(t.duplicate()); } void giveObject(SerializedType* t) { data.push_back(t); } diff --git a/src/SerializedTypes.cpp b/src/SerializedTypes.cpp index 793192e19..fa9b23a28 100644 --- a/src/SerializedTypes.cpp +++ b/src/SerializedTypes.cpp @@ -83,10 +83,15 @@ STUVariableLength* STUVariableLength::construct(SerializerIterator& u) return new STUVariableLength(u.getVL()); } +int STUVariableLength::getLength() const +{ + return Serializer::encodeLengthLength(value.size()) + value.size(); +} + std::string STUTaggedList::getText() const { std::string ret; - for(std::list::const_iterator it=value.begin(); it!=value.end(); ++it) + for(std::vector::const_iterator it=value.begin(); it!=value.end(); ++it) { ret+=boost::lexical_cast(it->first); ret+=","; @@ -99,3 +104,10 @@ STUTaggedList* STUTaggedList::construct(SerializerIterator& u) { return new STUTaggedList(u.getTaggedList()); } + +int STUTaggedList::getLength() const +{ + int ret=Serializer::getTaggedListLength(value); + if(ret<0) throw(0); + return ret; +} diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index e9280e5fc..3fe912600 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -202,11 +202,11 @@ public: class STUTaggedList { protected: - std::list value; + std::vector value; public: - STUTaggedList(const std::list& v) : value(v) { ; } + STUTaggedList(const std::vector& v) : value(v) { ; } STUTaggedList() { ; } static STUTaggedList* construct(SerializerIterator&); @@ -214,13 +214,13 @@ public: SerializedTypeID getType() const { return STI_TL; } STUTaggedList *duplicate() const { return new STUTaggedList(value); } std::string getText() const; - virtual void add(Serializer& s) const { s.addTaggedList(value); } + virtual void add(Serializer& s) const { if(s.addTaggedList(value)<0) throw(0); } - const std::list& peekValue() const { return value; } - std::list& peekValue() { return value; } - std::list getValue() const { return value; } + const std::vector& peekValue() const { return value; } + std::vector& peekValue() { return value; } + std::vector getValue() const { return value; } - void setValue(std::list& v) { value=v; } + void setValue(std::vector& v) { value=v; } int getItemCount() const { return value.size(); } bool isEmpty() const { return value.empty(); } @@ -228,8 +228,8 @@ public: void clear() { value.erase(value.begin(), value.end()); } void addItem(const TaggedListItem& v) { value.push_back(v); } - operator std::list() const { return value; } - STUTaggedList& operator=(const std::list& v) { value=v; return *this; } + operator std::vector() const { return value; } + STUTaggedList& operator=(const std::vector& v) { value=v; return *this; } }; #endif diff --git a/src/Serializer.cpp b/src/Serializer.cpp index 9d7351e95..645243a30 100644 --- a/src/Serializer.cpp +++ b/src/Serializer.cpp @@ -271,6 +271,28 @@ int Serializer::addTaggedList(const std::vector& list) return ret; } +int Serializer::getTaggedListLength(const std::list& list) +{ + int size=list.size(); + if(size>255) return -1; + int ret=1; + if(size!=0) + for(std::list::const_iterator it=list.begin(); it!=list.end(); ++it) + ret+=1 + it->second.size() + Serializer::encodeLengthLength(it->second.size()); + return ret; +} + +int Serializer::getTaggedListLength(const std::vector& list) +{ + int size=list.size(); + if(size>255) return -1; + int ret=1; + if(size!=0) + for(std::vector::const_iterator it=list.begin(); it!=list.end(); ++it) + ret+=1 + it->second.size() + Serializer::encodeLengthLength(it->second.size()); + return ret; +} + bool Serializer::getVL(std::vector& objectVL, int offset, int& length) const { int b1; @@ -355,6 +377,25 @@ bool Serializer::getTaggedList(std::list& list, int offset, int& return true; } +bool Serializer::getTaggedList(std::vector& list, int offset, int& length) const +{ + list.clear(); + int startOffset=offset; + int numElem; + if(!get8(numElem, offset++)) return false; + for(int i=0; i data; + if(!get8(tag, offset++)) return false; + if(!getVL(data, offset, len)) return false; + offset+=len; + list.push_back(std::make_pair(tag, data)); + } + length=offset-startOffset; + return true; +} + std::vector Serializer::encodeVL(int length) throw() { unsigned char lenBytes[4]; @@ -381,7 +422,7 @@ std::vector Serializer::encodeVL(int length) throw() else throw(std::overflow_error("lenlen")); } -int encodeLengthLength(int length) throw() +int Serializer::encodeLengthLength(int length) throw() { if(length<0) throw(std::overflow_error("len<0")); if(length<=192) return 1; @@ -487,10 +528,10 @@ std::vector SerializerIterator::getVL() throw() return vl; } -std::list SerializerIterator::getTaggedList() throw() +std::vector SerializerIterator::getTaggedList() throw() { int length; - std::list tl; + std::vector tl; if(!mSerializer.getTaggedList(tl, mPos, length)) throw(0); mPos+=length; return tl; diff --git a/src/Serializer.h b/src/Serializer.h index 702be674f..8e7da2c22 100644 --- a/src/Serializer.h +++ b/src/Serializer.h @@ -39,6 +39,8 @@ class Serializer int addVL(const void *ptr, int len); int addTaggedList(const std::list&); int addTaggedList(const std::vector&); + static int getTaggedListLength(const std::list&); + static int getTaggedListLength(const std::vector&); // disassemble functions bool get8(int&, int offset) const; @@ -54,7 +56,8 @@ class Serializer bool getVL(std::vector& objectVL, int offset, int& length) const; bool getVLLength(int& length, int offset) const; - bool getTaggedList(std::list&, int offset, int& legnth) const; + bool getTaggedList(std::list&, int offset, int& length) const; + bool getTaggedList(std::vector&, int offset, int& length) const; // hash functions @@ -84,7 +87,7 @@ class Serializer // low-level VL length encode/decode functions static std::vector encodeVL(int length) throw(); - static int encodeLengthLength(int length) throw(); + static int encodeLengthLength(int length) throw(); // length to encode length static int decodeLengthLength(int b1) throw(); static int decodeVLLength(int b1) throw(); static int decodeVLLength(int b1, int b2) throw(); @@ -117,7 +120,7 @@ public: uint256 get256() throw(); std::vector getVL() throw(); - std::list getTaggedList() throw(); + std::vector getTaggedList() throw(); }; #endif