mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
More work on the new serialized types.
This commit is contained in:
101
src/SerializedTypes.cpp
Normal file
101
src/SerializedTypes.cpp
Normal 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());
|
||||
}
|
||||
@@ -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<unsigned char> serialize() const;
|
||||
virtual int add(std::vector<unsigned char>&) 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<unsigned char>&, 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<unsigned char>&) 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<unsigned char>&, 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<unsigned char>&) 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<unsigned char>&, 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<unsigned char>&) 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<unsigned char>&, 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<unsigned char>&) 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<unsigned char>&, 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<unsigned char>&) 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<unsigned char>&, 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<unsigned char>&) 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<unsigned char>& v) : value(v) { ; }
|
||||
STUVariableLength() { ; }
|
||||
static STUVariableLength* construct(const std::vector<unsigned char>&, 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<unsigned char>&) const;
|
||||
virtual void add(Serializer& s) const { s.addVL(value); }
|
||||
|
||||
const std::vector<unsigned char>& peekValue() const { return value; }
|
||||
std::vector<unsigned char>& peekValue() { return value; }
|
||||
@@ -217,14 +208,13 @@ public:
|
||||
|
||||
STUTaggedList(const std::list<TaggedListItem>& v) : value(v) { ; }
|
||||
STUTaggedList() { ; }
|
||||
static STUTaggedList* construct(const std::vector<unsigned char>&, 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<unsigned char>&) const;
|
||||
virtual void add(Serializer& s) const { s.addTaggedList(value); }
|
||||
|
||||
const std::list<TaggedListItem>& peekValue() const { return value; }
|
||||
std::list<TaggedListItem>& peekValue() { return value; }
|
||||
|
||||
@@ -257,6 +257,20 @@ int Serializer::addTaggedList(const std::list<TaggedListItem>& list)
|
||||
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
|
||||
{
|
||||
int b1;
|
||||
|
||||
@@ -38,6 +38,7 @@ class Serializer
|
||||
int addVL(const std::vector<unsigned char> &vector);
|
||||
int addVL(const void *ptr, int len);
|
||||
int addTaggedList(const std::list<TaggedListItem>&);
|
||||
int addTaggedList(const std::vector<TaggedListItem>&);
|
||||
|
||||
// disassemble functions
|
||||
bool get8(int&, int offset) const;
|
||||
|
||||
Reference in New Issue
Block a user