Full support for 128-bit types. Remove throw() specifications.

This commit is contained in:
JoelKatz
2012-04-05 21:08:34 -07:00
parent 250d8b02b1
commit 11cbf862e4
4 changed files with 89 additions and 36 deletions

View File

@@ -61,6 +61,16 @@ std::string STUInt64::getText() const
return boost::lexical_cast<std::string>(value); return boost::lexical_cast<std::string>(value);
} }
STHash128* STHash128::construct(SerializerIterator& u, const char *name)
{
return new STHash128(name, u.get128());
}
std::string STHash128::getText() const
{
return value.GetHex();
}
STHash160* STHash160::construct(SerializerIterator& u, const char *name) STHash160* STHash160::construct(SerializerIterator& u, const char *name)
{ {
return new STHash160(name, u.get160()); return new STHash160(name, u.get160());

View File

@@ -14,10 +14,10 @@ enum SerializedTypeID
// standard types // standard types
STI_OBJECT=1, STI_UINT8=2, STI_UINT16=3, STI_UINT32=4, STI_UINT64=5, STI_OBJECT=1, STI_UINT8=2, STI_UINT16=3, STI_UINT32=4, STI_UINT64=5,
STI_HASH160=6, STI_HASH256=7, STI_VL=8, STI_TL=9, STI_HASH128=6, STI_HASH160=7, STI_HASH256=8, STI_VL=9, STI_TL=10,
// high level types // high level types
STI_ACCOUNT=10, STI_TRANSACTION=10 STI_ACCOUNT=100, STI_TRANSACTION=101
}; };
class SerializedType class SerializedType
@@ -144,6 +144,31 @@ public:
STUInt64& operator=(uint64 v) { value=v; return *this; } STUInt64& operator=(uint64 v) { value=v; return *this; }
}; };
class STHash128 : public SerializedType
{
protected:
uint128 value;
public:
STHash128(const uint128& v=uint128()) : value(v) { ; }
STHash128(const char *n, const uint128& v=uint128()) : SerializedType(n), value(v) { ; }
STHash128() { ; }
static STHash128* construct(SerializerIterator&, const char *name=NULL);
int getLength() const { return 20; }
SerializedTypeID getType() const { return STI_HASH128; }
STHash128* duplicate() const { return new STHash128(name, value); }
virtual std::string getText() const;
void add(Serializer& s) const { s.add128(value); }
const uint128& getValue() const { return value; }
void setValue(const uint128& v) { value=v; }
operator uint128() const { return value; }
STHash128& operator=(const uint128& v) { value=v; return *this; }
};
class STHash160 : public SerializedType class STHash160 : public SerializedType
{ {
protected: protected:

View File

@@ -99,25 +99,32 @@ bool Serializer::get64(uint64& o, int offset) const
return true; return true;
} }
bool Serializer::get128(uint128& o, int offset) const
{
if((offset+(128/8))>mData.size()) return false;
memcpy(o.begin(), &(mData.front())+offset, (128/8));
return true;
}
bool Serializer::get160(uint160& o, int offset) const bool Serializer::get160(uint160& o, int offset) const
{ {
if((offset+20)>mData.size()) return false; if((offset+(160/8))>mData.size()) return false;
memcpy(o.begin(), &(mData.front())+offset, 20); memcpy(o.begin(), &(mData.front())+offset, (160/8));
return true; return true;
} }
bool Serializer::get256(uint256& o, int offset) const bool Serializer::get256(uint256& o, int offset) const
{ {
if((offset+32)>mData.size()) return false; if((offset+(256/8))>mData.size()) return false;
memcpy(o.begin(), &(mData.front())+offset, 32); memcpy(o.begin(), &(mData.front())+offset, (256/8));
return true; return true;
} }
uint256 Serializer::get256(int offset) const uint256 Serializer::get256(int offset) const
{ {
uint256 ret; uint256 ret;
if((offset+32)>mData.size()) return ret; if((offset+(256/8))>mData.size()) return ret;
memcpy(&ret, &(mData.front())+offset, 32); memcpy(&ret, &(mData.front())+offset, (256/8));
return ret; return ret;
} }
@@ -403,7 +410,7 @@ bool Serializer::getTaggedList(std::vector<TaggedListItem>& list, int offset, in
return true; return true;
} }
std::vector<unsigned char> Serializer::encodeVL(int length) throw() std::vector<unsigned char> Serializer::encodeVL(int length)
{ {
unsigned char lenBytes[4]; unsigned char lenBytes[4];
if(length<=192) if(length<=192)
@@ -429,7 +436,7 @@ std::vector<unsigned char> Serializer::encodeVL(int length) throw()
else throw(std::overflow_error("lenlen")); else throw(std::overflow_error("lenlen"));
} }
int Serializer::encodeLengthLength(int length) throw() int Serializer::encodeLengthLength(int length)
{ {
if(length<0) throw(std::overflow_error("len<0")); if(length<0) throw(std::overflow_error("len<0"));
if(length<=192) return 1; if(length<=192) return 1;
@@ -438,7 +445,7 @@ int Serializer::encodeLengthLength(int length) throw()
throw(std::overflow_error("len>918644")); throw(std::overflow_error("len>918644"));
} }
int Serializer::decodeLengthLength(int b1) throw() int Serializer::decodeLengthLength(int b1)
{ {
if(b1<0) throw(std::overflow_error("b1<0")); if(b1<0) throw(std::overflow_error("b1<0"));
if(b1<=192) return 1; if(b1<=192) return 1;
@@ -447,21 +454,21 @@ int Serializer::decodeLengthLength(int b1) throw()
throw(std::overflow_error("b1>254")); throw(std::overflow_error("b1>254"));
} }
int Serializer::decodeVLLength(int b1) throw() int Serializer::decodeVLLength(int b1)
{ {
if(b1<0) throw(std::overflow_error("b1<0")); if(b1<0) throw(std::overflow_error("b1<0"));
if(b1>254) throw(std::overflow_error("b1>254")); if(b1>254) throw(std::overflow_error("b1>254"));
return b1; return b1;
} }
int Serializer::decodeVLLength(int b1, int b2) throw() int Serializer::decodeVLLength(int b1, int b2)
{ {
if(b1<193) throw(std::overflow_error("b1<193")); if(b1<193) throw(std::overflow_error("b1<193"));
if(b1>240) throw(std::overflow_error("b1>240")); if(b1>240) throw(std::overflow_error("b1>240"));
return 193+(b1-193)*256+b2; return 193+(b1-193)*256+b2;
} }
int Serializer::decodeVLLength(int b1, int b2, int b3) throw() int Serializer::decodeVLLength(int b1, int b2, int b3)
{ {
if(b1<241) throw(std::overflow_error("b1<241")); if(b1<241) throw(std::overflow_error("b1<241"));
if(b1>254) throw(std::overflow_error("b1>254")); if(b1>254) throw(std::overflow_error("b1>254"));
@@ -478,7 +485,7 @@ int SerializerIterator::getBytesLeft()
return mSerializer.getLength()-mPos; return mSerializer.getLength()-mPos;
} }
unsigned char SerializerIterator::get8() throw() unsigned char SerializerIterator::get8()
{ {
int val; int val;
if(!mSerializer.get8(val, mPos)) throw(0); if(!mSerializer.get8(val, mPos)) throw(0);
@@ -486,7 +493,7 @@ unsigned char SerializerIterator::get8() throw()
return val; return val;
} }
uint16 SerializerIterator::get16() throw() uint16 SerializerIterator::get16()
{ {
uint16 val; uint16 val;
if(!mSerializer.get16(val, mPos)) throw(0); if(!mSerializer.get16(val, mPos)) throw(0);
@@ -494,7 +501,7 @@ uint16 SerializerIterator::get16() throw()
return val; return val;
} }
uint32 SerializerIterator::get32() throw() uint32 SerializerIterator::get32()
{ {
uint32 val; uint32 val;
if(!mSerializer.get32(val, mPos)) throw(0); if(!mSerializer.get32(val, mPos)) throw(0);
@@ -502,7 +509,7 @@ uint32 SerializerIterator::get32() throw()
return val; return val;
} }
uint64 SerializerIterator::get64() throw() uint64 SerializerIterator::get64()
{ {
uint64 val; uint64 val;
if(!mSerializer.get64(val, mPos)) throw(0); if(!mSerializer.get64(val, mPos)) throw(0);
@@ -510,7 +517,15 @@ uint64 SerializerIterator::get64() throw()
return val; return val;
} }
uint160 SerializerIterator::get160() throw() uint128 SerializerIterator::get128()
{
uint128 val;
if(!mSerializer.get128(val, mPos)) throw(0);
mPos+=128/8;
return val;
}
uint160 SerializerIterator::get160()
{ {
uint160 val; uint160 val;
if(!mSerializer.get160(val, mPos)) throw(0); if(!mSerializer.get160(val, mPos)) throw(0);
@@ -518,7 +533,7 @@ uint160 SerializerIterator::get160() throw()
return val; return val;
} }
uint256 SerializerIterator::get256() throw() uint256 SerializerIterator::get256()
{ {
uint256 val; uint256 val;
if(!mSerializer.get256(val, mPos)) throw(0); if(!mSerializer.get256(val, mPos)) throw(0);
@@ -526,7 +541,7 @@ uint256 SerializerIterator::get256() throw()
return val; return val;
} }
std::vector<unsigned char> SerializerIterator::getVL() throw() std::vector<unsigned char> SerializerIterator::getVL()
{ {
int length; int length;
std::vector<unsigned char> vl; std::vector<unsigned char> vl;
@@ -535,7 +550,7 @@ std::vector<unsigned char> SerializerIterator::getVL() throw()
return vl; return vl;
} }
std::vector<TaggedListItem> SerializerIterator::getTaggedList() throw() std::vector<TaggedListItem> SerializerIterator::getTaggedList()
{ {
int length; int length;
std::vector<TaggedListItem> tl; std::vector<TaggedListItem> tl;

View File

@@ -49,6 +49,7 @@ class Serializer
bool get16(uint16&, int offset) const; bool get16(uint16&, int offset) const;
bool get32(uint32&, int offset) const; bool get32(uint32&, int offset) const;
bool get64(uint64&, int offset) const; bool get64(uint64&, int offset) const;
bool get128(uint128&, int offset) const;
bool get160(uint160&, int offset) const; bool get160(uint160&, int offset) const;
bool get256(uint256&, int offset) const; bool get256(uint256&, int offset) const;
uint256 get256(int offset) const; uint256 get256(int offset) const;
@@ -87,12 +88,12 @@ class Serializer
bool addSignature(CKey& rkey); bool addSignature(CKey& rkey);
// low-level VL length encode/decode functions // low-level VL length encode/decode functions
static std::vector<unsigned char> encodeVL(int length) throw(); static std::vector<unsigned char> encodeVL(int length);
static int encodeLengthLength(int length) throw(); // length to encode length static int encodeLengthLength(int length); // length to encode length
static int decodeLengthLength(int b1) throw(); static int decodeLengthLength(int b1);
static int decodeVLLength(int b1) throw(); static int decodeVLLength(int b1);
static int decodeVLLength(int b1, int b2) throw(); static int decodeVLLength(int b1, int b2);
static int decodeVLLength(int b1, int b2, int b3) throw(); static int decodeVLLength(int b1, int b2, int b3);
static void TestSerializer(); static void TestSerializer();
}; };
@@ -113,15 +114,17 @@ public:
int getPos(void) { return mPos; } int getPos(void) { return mPos; }
int getBytesLeft(); int getBytesLeft();
unsigned char get8() throw(); // get functions throw on error
uint16 get16() throw(); unsigned char get8();
uint32 get32() throw(); uint16 get16();
uint64 get64() throw(); uint32 get32();
uint160 get160() throw(); uint64 get64();
uint256 get256() throw(); uint128 get128();
uint160 get160();
uint256 get256();
std::vector<unsigned char> getVL() throw(); std::vector<unsigned char> getVL();
std::vector<TaggedListItem> getTaggedList() throw(); std::vector<TaggedListItem> getTaggedList();
}; };
#endif #endif