More work on the new serialized types.

This commit is contained in:
JoelKatz
2012-03-14 17:15:41 -07:00
parent e7c350ffec
commit 47e1f5a1d4
4 changed files with 133 additions and 27 deletions

101
src/SerializedTypes.cpp Normal file
View File

@@ -0,0 +1,101 @@
#include <boost/lexical_cast.hpp>
#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<std::string>(value);
}
STUInt16* STUInt16::construct(SerializerIterator& u)
{
return new STUInt16(u.get16());
}
std::string STUInt16::getText() const
{
return boost::lexical_cast<std::string>(value);
}
STUInt32* STUInt32::construct(SerializerIterator& u)
{
return new STUInt32(u.get32());
}
std::string STUInt32::getText() const
{
return boost::lexical_cast<std::string>(value);
}
STUInt64* STUInt64::construct(SerializerIterator& u)
{
return new STUInt64(u.get64());
}
std::string STUInt64::getText() const
{
return boost::lexical_cast<std::string>(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<unsigned char>& value)
{
int dlen=value.size(), i=0;
char psz[dlen*2 + 1];
for(std::vector<unsigned char>::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<TaggedListItem>::const_iterator it=value.begin(); it!=value.end(); ++it)
{
ret+=boost::lexical_cast<std::string>(it->first);
ret+=",";
ret+=hex(it->second);
}
return ret;
}
STUTaggedList* STUTaggedList::construct(SerializerIterator& u)
{
return new STUTaggedList(u.getTaggedList());
}

View File

@@ -26,10 +26,8 @@ public:
virtual SerializedType* duplicate() const { return new SerializedType(); } virtual SerializedType* duplicate() const { return new SerializedType(); }
virtual std::string getText() const { return std::string(); } virtual std::string getText() const { return std::string(); }
virtual std::string getSQL() const { return std::string(); }
virtual std::vector<unsigned char> serialize() const; virtual void add(Serializer& s) const { return; }
virtual int add(std::vector<unsigned char>&) const { return 0; }
SerializedType* new_clone(const SerializedType& s) { return s.duplicate(); } SerializedType* new_clone(const SerializedType& s) { return s.duplicate(); }
void delete_clone(const SerializedType* s) { boost::checked_delete(s); } void delete_clone(const SerializedType* s) { boost::checked_delete(s); }
@@ -43,14 +41,13 @@ protected:
public: public:
STUInt8(unsigned char v=0) : value(v) { ; } STUInt8(unsigned char v=0) : value(v) { ; }
static STUInt8* construct(const std::vector<unsigned char>&, int start_offset, int& length_consumed); static STUInt8* construct(SerializerIterator&);
int getLength() const { return 1; } int getLength() const { return 1; }
SerializedTypeID getType() const { return STI_UINT8; } SerializedTypeID getType() const { return STI_UINT8; }
STUInt8 *duplicate() const { return new STUInt8(value); } STUInt8 *duplicate() const { return new STUInt8(value); }
std::string getText() const; std::string getText() const;
std::string getSQL() const; virtual void add(Serializer& s) const { s.add8(value); }
virtual int add(std::vector<unsigned char>&) const;
unsigned char getValue() const { return value; } unsigned char getValue() const { return value; }
void setValue(unsigned char v) { value=v; } void setValue(unsigned char v) { value=v; }
@@ -67,14 +64,13 @@ protected:
public: public:
STUInt16(uint16 v=0) : value(v) { ; } STUInt16(uint16 v=0) : value(v) { ; }
static STUInt16* construct(const std::vector<unsigned char>&, int start_offset, int& length_consumed); static STUInt16* construct(SerializerIterator&);
int getLength() const { return 2; } int getLength() const { return 2; }
SerializedTypeID getType() const { return STI_UINT16; } SerializedTypeID getType() const { return STI_UINT16; }
STUInt16 *duplicate() const { return new STUInt16(value); } STUInt16 *duplicate() const { return new STUInt16(value); }
std::string getText() const; std::string getText() const;
std::string getSQL() const; virtual void add(Serializer& s) const { s.add16(value); }
virtual int add(std::vector<unsigned char>&) const;
uint16 getValue() const { return value; } uint16 getValue() const { return value; }
void setValue(uint16 v) { value=v; } void setValue(uint16 v) { value=v; }
@@ -91,14 +87,13 @@ protected:
public: public:
STUInt32(uint32 v=0) : value(v) { ; } STUInt32(uint32 v=0) : value(v) { ; }
static STUInt32* construct(const std::vector<unsigned char>&, int start_offset, int& length_consumed); static STUInt32* construct(SerializerIterator&);
int getLength() const { return 4; } int getLength() const { return 4; }
SerializedTypeID getType() const { return STI_UINT32; } SerializedTypeID getType() const { return STI_UINT32; }
STUInt32 *duplicate() const { return new STUInt32(value); } STUInt32 *duplicate() const { return new STUInt32(value); }
std::string getText() const; std::string getText() const;
std::string getSQL() const; virtual void add(Serializer& s) const { s.add32(value); }
virtual int add(std::vector<unsigned char>&) const;
uint32 getValue() const { return value; } uint32 getValue() const { return value; }
void setValue(uint32 v) { value=v; } void setValue(uint32 v) { value=v; }
@@ -115,14 +110,13 @@ protected:
public: public:
STUInt64(uint64 v=0) : value(v) { ; } STUInt64(uint64 v=0) : value(v) { ; }
static STUInt64* construct(const std::vector<unsigned char>&, int start_offset, int& length_consumed); static STUInt64* construct(SerializerIterator&);
int getLength() const { return 8; } int getLength() const { return 8; }
SerializedTypeID getType() const { return STI_UINT64; } SerializedTypeID getType() const { return STI_UINT64; }
STUInt64 *duplicate() const { return new STUInt64(value); } STUInt64 *duplicate() const { return new STUInt64(value); }
std::string getText() const; std::string getText() const;
std::string getSQL() const; virtual void add(Serializer& s) const { s.add64(value); }
virtual int add(std::vector<unsigned char>&) const;
uint64 getValue() const { return value; } uint64 getValue() const { return value; }
void setValue(uint64 v) { value=v; } void setValue(uint64 v) { value=v; }
@@ -140,14 +134,13 @@ public:
STUHash160(const uint160& v) : value(v) { ; } STUHash160(const uint160& v) : value(v) { ; }
STUHash160() { ; } STUHash160() { ; }
static STUHash160* construct(const std::vector<unsigned char>&, int start_offset, int& length_consumed); static STUHash160* construct(SerializerIterator&);
int getLength() const { return 20; } int getLength() const { return 20; }
SerializedTypeID getType() const { return STI_HASH160; } SerializedTypeID getType() const { return STI_HASH160; }
STUHash160 *duplicate() const { return new STUHash160(value); } STUHash160 *duplicate() const { return new STUHash160(value); }
std::string getText() const; std::string getText() const;
std::string getSQL() const; virtual void add(Serializer& s) const { s.add160(value); }
virtual int add(std::vector<unsigned char>&) const;
const uint160& getValue() const { return value; } const uint160& getValue() const { return value; }
void setValue(const uint160& v) { value=v; } void setValue(const uint160& v) { value=v; }
@@ -165,14 +158,13 @@ public:
STUHash256(const uint256& v) : value(v) { ; } STUHash256(const uint256& v) : value(v) { ; }
STUHash256() { ; } STUHash256() { ; }
static STUHash256* construct(const std::vector<unsigned char>&, int start_offset, int& length_consumed); static STUHash256* construct(SerializerIterator&);
int getLength() const { return 32; } int getLength() const { return 32; }
SerializedTypeID getType() const { return STI_HASH256; } SerializedTypeID getType() const { return STI_HASH256; }
STUHash256 *duplicate() const { return new STUHash256(value); } STUHash256 *duplicate() const { return new STUHash256(value); }
std::string getText() const; std::string getText() const;
std::string getSQL() const; virtual void add(Serializer& s) const { s.add256(value); }
virtual int add(std::vector<unsigned char>&) const;
const uint256& getValue() const { return value; } const uint256& getValue() const { return value; }
void setValue(const uint256& v) { value=v; } void setValue(const uint256& v) { value=v; }
@@ -190,14 +182,13 @@ public:
STUVariableLength(const std::vector<unsigned char>& v) : value(v) { ; } STUVariableLength(const std::vector<unsigned char>& v) : value(v) { ; }
STUVariableLength() { ; } STUVariableLength() { ; }
static STUVariableLength* construct(const std::vector<unsigned char>&, int start_offset, int& length_consumed); static STUVariableLength* construct(SerializerIterator&);
int getLength() const; int getLength() const;
SerializedTypeID getType() const { return STI_VL; } SerializedTypeID getType() const { return STI_VL; }
STUVariableLength *duplicate() const { return new STUVariableLength(value); } STUVariableLength *duplicate() const { return new STUVariableLength(value); }
std::string getText() const; std::string getText() const;
std::string getSQL() const; virtual void add(Serializer& s) const { s.addVL(value); }
virtual int add(std::vector<unsigned char>&) const;
const std::vector<unsigned char>& peekValue() const { return value; } const std::vector<unsigned char>& peekValue() const { return value; }
std::vector<unsigned char>& peekValue() { return value; } std::vector<unsigned char>& peekValue() { return value; }
@@ -217,14 +208,13 @@ public:
STUTaggedList(const std::list<TaggedListItem>& v) : value(v) { ; } STUTaggedList(const std::list<TaggedListItem>& v) : value(v) { ; }
STUTaggedList() { ; } STUTaggedList() { ; }
static STUTaggedList* construct(const std::vector<unsigned char>&, int start_offset, int& length_consumed); static STUTaggedList* construct(SerializerIterator&);
int getLength() const; int getLength() const;
SerializedTypeID getType() const { return STI_TL; } SerializedTypeID getType() const { return STI_TL; }
STUTaggedList *duplicate() const { return new STUTaggedList(value); } STUTaggedList *duplicate() const { return new STUTaggedList(value); }
std::string getText() const; std::string getText() const;
std::string getSQL() const; virtual void add(Serializer& s) const { s.addTaggedList(value); }
virtual int add(std::vector<unsigned char>&) const;
const std::list<TaggedListItem>& peekValue() const { return value; } const std::list<TaggedListItem>& peekValue() const { return value; }
std::list<TaggedListItem>& peekValue() { return value; } std::list<TaggedListItem>& peekValue() { return value; }

View File

@@ -257,6 +257,20 @@ int Serializer::addTaggedList(const std::list<TaggedListItem>& list)
return ret; return ret;
} }
int Serializer::addTaggedList(const std::vector<TaggedListItem>& list)
{
int size=list.size();
if(size>255) return -1;
int ret=add8(size);
if(size!=0)
for(std::vector<TaggedListItem>::const_iterator it=list.begin(); it!=list.end(); ++it)
{
add8(it->first);
addVL(it->second);
}
return ret;
}
bool Serializer::getVL(std::vector<unsigned char>& objectVL, int offset, int& length) const bool Serializer::getVL(std::vector<unsigned char>& objectVL, int offset, int& length) const
{ {
int b1; int b1;

View File

@@ -38,6 +38,7 @@ class Serializer
int addVL(const std::vector<unsigned char> &vector); int addVL(const std::vector<unsigned char> &vector);
int addVL(const void *ptr, int len); int addVL(const void *ptr, int len);
int addTaggedList(const std::list<TaggedListItem>&); int addTaggedList(const std::list<TaggedListItem>&);
int addTaggedList(const std::vector<TaggedListItem>&);
// disassemble functions // disassemble functions
bool get8(int&, int offset) const; bool get8(int&, int offset) const;