Merge branch 'master' into unl

This commit is contained in:
Arthur Britto
2012-03-21 14:43:37 -07:00
4 changed files with 58 additions and 134 deletions

View File

@@ -1,6 +1,8 @@
#ifndef __SERIALIZEDOBJECT__ #ifndef __SERIALIZEDOBJECT__
#define __SERIALIZEDOBJECT__ #define __SERIALIZEDOBJECT__
#include <vector>
#include <boost/ptr_container/ptr_vector.hpp> #include <boost/ptr_container/ptr_vector.hpp>
#include "SerializedTypes.h" #include "SerializedTypes.h"
@@ -18,22 +20,16 @@ struct SOElement
int e_flags; int e_flags;
}; };
struct SOType
{ // A type of serialized object
const char *name;
std::list<SOElement> elements;
};
class STUObject : public SerializedType class STUObject : public SerializedType
{ {
protected: protected:
SOType *type;
boost::ptr_vector<SerializedType> data; boost::ptr_vector<SerializedType> data;
std::vector<SOElement*> type;
public: public:
STUObject(const char *n=NULL) : SerializedType(n), type(NULL) { ; } STUObject(const char *n=NULL) : SerializedType(n) { ; }
STUObject(SOType *t) : type(t) { ; } STUObject(SOElement *t, const char *n=NULL);
STUObject(SOType *t, SerializerIterator& u); STUObject(SOElement *t, SerializerIterator& u, const char *n=NULL);
virtual ~STUObject() { ; } virtual ~STUObject() { ; }
int getLength() const; int getLength() const;

View File

@@ -10,38 +10,7 @@ SerializedTransaction::SerializedTransaction(TransactionType type)
mMiddleTxn.giveObject(new STVariableLength("Signature")); // signature mMiddleTxn.giveObject(new STVariableLength("Signature")); // signature
mMiddleTxn.giveObject(new STUInt8("Type", static_cast<unsigned char>(type))); mMiddleTxn.giveObject(new STUInt8("Type", static_cast<unsigned char>(type)));
SOElement* elem=mFormat->elements; mInnerTxn=STUObject(mFormat->elements, "InnerTransaction");
while(elem->e_id!=STI_DONE)
{
if( (elem->e_type==SOE_IFFLAG) || (elem->e_type==SOE_IFNFLAG) )
mInnerTxn.giveObject(new STUObject(elem->e_name));
else switch(elem->e_id)
{
case STI_UINT16:
mInnerTxn.giveObject(new STUInt16(elem->e_name));
break;
case STI_UINT32:
mInnerTxn.giveObject(new STUInt32(elem->e_name));
break;
case STI_UINT64:
mInnerTxn.giveObject(new STUInt64(elem->e_name));
case STI_HASH160:
case STI_HASH256:
case STI_VL:
mInnerTxn.giveObject(new STVariableLength(elem->e_name));
break;
case STI_TL:
mInnerTxn.giveObject(new STTaggedList(elem->e_name));
break;
#if 0
case STI_ACCOUNT: // CHECKME: Should an account be variable length?
mInnerTxn.giveObject(new STVariableLength(elem->e_name));
break;
#endif
default: throw(std::runtime_error("invalid transaction element"));
}
elem++;
}
} }
SerializedTransaction::SerializedTransaction(SerializerIterator& sit, int length) SerializedTransaction::SerializedTransaction(SerializerIterator& sit, int length)
@@ -57,9 +26,13 @@ SerializedTransaction::SerializedTransaction(SerializerIterator& sit, int length
mMiddleTxn.giveObject(new STUInt32("Magic", TransactionMagic)); mMiddleTxn.giveObject(new STUInt32("Magic", TransactionMagic));
mMiddleTxn.giveObject(new STVariableLength("Signature", sit.getVL())); mMiddleTxn.giveObject(new STVariableLength("Signature", sit.getVL()));
mMiddleTxn.giveObject(new STUInt32("Type", sit.get32()));
// WRITEME int type=sit.get32();
mMiddleTxn.giveObject(new STUInt32("Type", type));
mFormat=getFormat(static_cast<TransactionType>(type));
if(!mFormat) throw(std::runtime_error("Transaction has invalid type"));
mInnerTxn=STUObject(mFormat->elements, sit, "InnerTransaction");
} }
int SerializedTransaction::getLength() const int SerializedTransaction::getLength() const

View File

@@ -20,9 +20,9 @@ std::string SerializedType::getFullText() const
return ret; return ret;
} }
STUInt8* STUInt8::construct(SerializerIterator& u) STUInt8* STUInt8::construct(SerializerIterator& u, const char *name)
{ {
return new STUInt8(u.get8()); return new STUInt8(name, u.get8());
} }
std::string STUInt8::getText() const std::string STUInt8::getText() const
@@ -30,9 +30,9 @@ std::string STUInt8::getText() const
return boost::lexical_cast<std::string>(value); return boost::lexical_cast<std::string>(value);
} }
STUInt16* STUInt16::construct(SerializerIterator& u) STUInt16* STUInt16::construct(SerializerIterator& u, const char *name)
{ {
return new STUInt16(u.get16()); return new STUInt16(name, u.get16());
} }
std::string STUInt16::getText() const std::string STUInt16::getText() const
@@ -40,9 +40,9 @@ std::string STUInt16::getText() const
return boost::lexical_cast<std::string>(value); return boost::lexical_cast<std::string>(value);
} }
STUInt32* STUInt32::construct(SerializerIterator& u) STUInt32* STUInt32::construct(SerializerIterator& u, const char *name)
{ {
return new STUInt32(u.get32()); return new STUInt32(name, u.get32());
} }
std::string STUInt32::getText() const std::string STUInt32::getText() const
@@ -50,9 +50,9 @@ std::string STUInt32::getText() const
return boost::lexical_cast<std::string>(value); return boost::lexical_cast<std::string>(value);
} }
STUInt64* STUInt64::construct(SerializerIterator& u) STUInt64* STUInt64::construct(SerializerIterator& u, const char *name)
{ {
return new STUInt64(u.get64()); return new STUInt64(name, u.get64());
} }
std::string STUInt64::getText() const std::string STUInt64::getText() const
@@ -60,22 +60,22 @@ std::string STUInt64::getText() const
return boost::lexical_cast<std::string>(value); return boost::lexical_cast<std::string>(value);
} }
STUHash160* STUHash160::construct(SerializerIterator& u) STHash160* STHash160::construct(SerializerIterator& u, const char *name)
{ {
return new STUHash160(u.get160()); return new STHash160(name, u.get160());
} }
std::string STUHash160::getText() const std::string STHash160::getText() const
{ {
return value.GetHex(); return value.GetHex();
} }
STUHash256* STUHash256::construct(SerializerIterator& u) STHash256* STHash256::construct(SerializerIterator& u, const char *name)
{ {
return new STUHash256(u.get256()); return new STHash256(name, u.get256());
} }
std::string STUHash256::getText() const std::string STHash256::getText() const
{ {
return value.GetHex(); return value.GetHex();
} }
@@ -94,7 +94,7 @@ std::string STVariableLength::getText() const
return hex(value); return hex(value);
} }
STVariableLength* STVariableLength::construct(SerializerIterator& u) STVariableLength* STVariableLength::construct(SerializerIterator& u, const char *name)
{ {
return new STVariableLength(u.getVL()); return new STVariableLength(u.getVL());
} }
@@ -116,60 +116,15 @@ std::string STTaggedList::getText() const
return ret; return ret;
} }
STTaggedList* STTaggedList::construct(SerializerIterator& u) STTaggedList* STTaggedList::construct(SerializerIterator& u, const char *name)
{ {
return new STTaggedList(u.getTaggedList()); return new STTaggedList(name, u.getTaggedList());
} }
int STTaggedList::getLength() const int STTaggedList::getLength() const
{ {
int ret=Serializer::getTaggedListLength(value); int ret=Serializer::getTaggedListLength(value);
if(ret<0) throw(0); if(ret<0) throw(std::overflow_error("bad TL length"));
return ret; return ret;
} }
std::string STUObject::getFullText() const
{
std::string ret;
if(name!=NULL)
{
ret=name;
ret+=" = {";
}
else ret="{";
for(boost::ptr_vector<SerializedType>::const_iterator it=data.begin(), end=data.end(); it!=end; ++it)
ret+=it->getFullText();
ret+="}";
return ret;
}
int STUObject::getLength() const
{
int ret=0;
for(boost::ptr_vector<SerializedType>::const_iterator it=data.begin(), end=data.end(); it!=end; ++it)
ret+=it->getLength();
return ret;
}
void STUObject::add(Serializer& s) const
{
for(boost::ptr_vector<SerializedType>::const_iterator it=data.begin(), end=data.end(); it!=end; ++it)
it->add(s);
}
std::string STUObject::getText() const
{
std::string ret="{";
bool first=false;
for(boost::ptr_vector<SerializedType>::const_iterator it=data.begin(), end=data.end(); it!=end; ++it)
{
if(!first)
{
ret+=", ";
first=false;
}
ret+=it->getText();
}
ret+="}";
return ret;
}

View File

@@ -36,7 +36,7 @@ public:
virtual int getLength() const { return 0; } virtual int getLength() const { return 0; }
virtual SerializedTypeID getType() const { return STI_NOTPRESENT; } virtual SerializedTypeID getType() const { return STI_NOTPRESENT; }
virtual SerializedType* duplicate() const { return new SerializedType(); } virtual SerializedType* duplicate() const { return new SerializedType(name); }
virtual std::string getFullText() const; virtual std::string getFullText() const;
virtual std::string getText() const // just the value virtual std::string getText() const // just the value
@@ -57,11 +57,11 @@ public:
STUInt8(unsigned char v=0) : value(v) { ; } STUInt8(unsigned char v=0) : value(v) { ; }
STUInt8(const char *n, unsigned char v=0) : SerializedType(n), value(v) { ; } STUInt8(const char *n, unsigned char v=0) : SerializedType(n), value(v) { ; }
static STUInt8* construct(SerializerIterator&); static STUInt8* construct(SerializerIterator&, const char *name=NULL);
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(name, value); }
std::string getText() const; std::string getText() const;
virtual void add(Serializer& s) const { s.add8(value); } virtual void add(Serializer& s) const { s.add8(value); }
@@ -81,11 +81,11 @@ public:
STUInt16(uint16 v=0) : value(v) { ; } STUInt16(uint16 v=0) : value(v) { ; }
STUInt16(const char *n, uint16 v=0) : SerializedType(n), value(v) { ; } STUInt16(const char *n, uint16 v=0) : SerializedType(n), value(v) { ; }
static STUInt16* construct(SerializerIterator&); static STUInt16* construct(SerializerIterator&, const char *name=NULL);
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(name, value); }
std::string getText() const; std::string getText() const;
virtual void add(Serializer& s) const { s.add16(value); } virtual void add(Serializer& s) const { s.add16(value); }
@@ -105,11 +105,11 @@ public:
STUInt32(uint32 v=0) : value(v) { ; } STUInt32(uint32 v=0) : value(v) { ; }
STUInt32(const char *n, uint32 v=0) : SerializedType(n), value(v) { ; } STUInt32(const char *n, uint32 v=0) : SerializedType(n), value(v) { ; }
static STUInt32* construct(SerializerIterator&); static STUInt32* construct(SerializerIterator&, const char *name=NULL);
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(name, value); }
std::string getText() const; std::string getText() const;
virtual void add(Serializer& s) const { s.add32(value); } virtual void add(Serializer& s) const { s.add32(value); }
@@ -129,11 +129,11 @@ public:
STUInt64(uint64 v=0) : value(v) { ; } STUInt64(uint64 v=0) : value(v) { ; }
STUInt64(const char *n, uint64 v=0) : SerializedType(n), value(v) { ; } STUInt64(const char *n, uint64 v=0) : SerializedType(n), value(v) { ; }
static STUInt64* construct(SerializerIterator&); static STUInt64* construct(SerializerIterator&, const char *name=NULL);
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(name, value); }
std::string getText() const; std::string getText() const;
virtual void add(Serializer& s) const { s.add64(value); } virtual void add(Serializer& s) const { s.add64(value); }
@@ -144,21 +144,21 @@ public:
STUInt64& operator=(uint64 v) { value=v; return *this; } STUInt64& operator=(uint64 v) { value=v; return *this; }
}; };
class STUHash160 : public SerializedType class STHash160 : public SerializedType
{ {
protected: protected:
uint160 value; uint160 value;
public: public:
STUHash160(const uint160& v=uint160()) : value(v) { ; } STHash160(const uint160& v=uint160()) : value(v) { ; }
STUHash160(const char *n, const uint160& v=uint160()) : SerializedType(n), value(v) { ; } STHash160(const char *n, const uint160& v=uint160()) : SerializedType(n), value(v) { ; }
STUHash160() { ; } STHash160() { ; }
static STUHash160* construct(SerializerIterator&); static STHash160* construct(SerializerIterator&, const char *name=NULL);
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); } STHash160 *duplicate() const { return new STHash160(name, value); }
std::string getText() const; std::string getText() const;
virtual void add(Serializer& s) const { s.add160(value); } virtual void add(Serializer& s) const { s.add160(value); }
@@ -166,24 +166,24 @@ public:
void setValue(const uint160& v) { value=v; } void setValue(const uint160& v) { value=v; }
operator uint160() const { return value; } operator uint160() const { return value; }
STUHash160& operator=(const uint160& v) { value=v; return *this; } STHash160& operator=(const uint160& v) { value=v; return *this; }
}; };
class STUHash256 : public SerializedType class STHash256 : public SerializedType
{ {
protected: protected:
uint256 value; uint256 value;
public: public:
STUHash256(const uint256& v) : value(v) { ; } STHash256(const uint256& v) : value(v) { ; }
STUHash256(const char *n, const uint256& v=uint256()) : SerializedType(n), value(v) { ; } STHash256(const char *n, const uint256& v=uint256()) : SerializedType(n), value(v) { ; }
STUHash256() { ; } STHash256() { ; }
static STUHash256* construct(SerializerIterator&); static STHash256* construct(SerializerIterator&, const char *name=NULL);
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); } STHash256 *duplicate() const { return new STHash256(name, value); }
std::string getText() const; std::string getText() const;
virtual void add(Serializer& s) const { s.add256(value); } virtual void add(Serializer& s) const { s.add256(value); }
@@ -191,7 +191,7 @@ public:
void setValue(const uint256& v) { value=v; } void setValue(const uint256& v) { value=v; }
operator uint256() const { return value; } operator uint256() const { return value; }
STUHash256& operator=(const uint256& v) { value=v; return *this; } STHash256& operator=(const uint256& v) { value=v; return *this; }
}; };
class STVariableLength : public SerializedType class STVariableLength : public SerializedType
@@ -205,11 +205,11 @@ public:
STVariableLength(const char *n, const std::vector<unsigned char>& v) : SerializedType(n), value(v) { ; } STVariableLength(const char *n, const std::vector<unsigned char>& v) : SerializedType(n), value(v) { ; }
STVariableLength(const char *n) : SerializedType(n) { ; } STVariableLength(const char *n) : SerializedType(n) { ; }
STVariableLength() { ; } STVariableLength() { ; }
static STVariableLength* construct(SerializerIterator&); static STVariableLength* construct(SerializerIterator&, const char *name=NULL);
int getLength() const; int getLength() const;
SerializedTypeID getType() const { return STI_VL; } SerializedTypeID getType() const { return STI_VL; }
STVariableLength *duplicate() const { return new STVariableLength(value); } STVariableLength *duplicate() const { return new STVariableLength(name, value); }
std::string getText() const; std::string getText() const;
virtual void add(Serializer& s) const { s.addVL(value); } virtual void add(Serializer& s) const { s.addVL(value); }
@@ -233,11 +233,11 @@ public:
STTaggedList(const char *n) : SerializedType(n) { ; } STTaggedList(const char *n) : SerializedType(n) { ; }
STTaggedList(const std::vector<TaggedListItem>& v) : value(v) { ; } STTaggedList(const std::vector<TaggedListItem>& v) : value(v) { ; }
STTaggedList(const char *n, const std::vector<TaggedListItem>& v) : SerializedType(n), value(v) { ; } STTaggedList(const char *n, const std::vector<TaggedListItem>& v) : SerializedType(n), value(v) { ; }
static STTaggedList* construct(SerializerIterator&); static STTaggedList* construct(SerializerIterator&, const char *name=NULL);
int getLength() const; int getLength() const;
SerializedTypeID getType() const { return STI_TL; } SerializedTypeID getType() const { return STI_TL; }
STTaggedList *duplicate() const { return new STTaggedList(value); } STTaggedList *duplicate() const { return new STTaggedList(name, value); }
std::string getText() const; std::string getText() const;
virtual void add(Serializer& s) const { if(s.addTaggedList(value)<0) throw(0); } virtual void add(Serializer& s) const { if(s.addTaggedList(value)<0) throw(0); }