diff --git a/src/SerializedTypes.cpp b/src/SerializedTypes.cpp index 885886adf0..80d63de302 100644 --- a/src/SerializedTypes.cpp +++ b/src/SerializedTypes.cpp @@ -61,6 +61,16 @@ std::string STUInt64::getText() const return boost::lexical_cast(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) { return new STHash160(name, u.get160()); diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index 824d170fa7..dc231b49ec 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -14,10 +14,10 @@ enum SerializedTypeID // standard types 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 - STI_ACCOUNT=10, STI_TRANSACTION=10 + STI_ACCOUNT=100, STI_TRANSACTION=101 }; class SerializedType @@ -144,6 +144,31 @@ public: 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 { protected: diff --git a/src/Serializer.cpp b/src/Serializer.cpp index 30fbfae9c0..0962338b48 100644 --- a/src/Serializer.cpp +++ b/src/Serializer.cpp @@ -99,25 +99,32 @@ bool Serializer::get64(uint64& o, int offset) const 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 { - if((offset+20)>mData.size()) return false; - memcpy(o.begin(), &(mData.front())+offset, 20); + if((offset+(160/8))>mData.size()) return false; + memcpy(o.begin(), &(mData.front())+offset, (160/8)); return true; } bool Serializer::get256(uint256& o, int offset) const { - if((offset+32)>mData.size()) return false; - memcpy(o.begin(), &(mData.front())+offset, 32); + if((offset+(256/8))>mData.size()) return false; + memcpy(o.begin(), &(mData.front())+offset, (256/8)); return true; } uint256 Serializer::get256(int offset) const { uint256 ret; - if((offset+32)>mData.size()) return ret; - memcpy(&ret, &(mData.front())+offset, 32); + if((offset+(256/8))>mData.size()) return ret; + memcpy(&ret, &(mData.front())+offset, (256/8)); return ret; } @@ -403,7 +410,7 @@ bool Serializer::getTaggedList(std::vector& list, int offset, in return true; } -std::vector Serializer::encodeVL(int length) throw() +std::vector Serializer::encodeVL(int length) { unsigned char lenBytes[4]; if(length<=192) @@ -429,7 +436,7 @@ std::vector Serializer::encodeVL(int length) throw() 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<=192) return 1; @@ -438,7 +445,7 @@ int Serializer::encodeLengthLength(int length) throw() 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<=192) return 1; @@ -447,21 +454,21 @@ int Serializer::decodeLengthLength(int b1) throw() 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>254) throw(std::overflow_error("b1>254")); 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>240) throw(std::overflow_error("b1>240")); 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>254) throw(std::overflow_error("b1>254")); @@ -478,7 +485,7 @@ int SerializerIterator::getBytesLeft() return mSerializer.getLength()-mPos; } -unsigned char SerializerIterator::get8() throw() +unsigned char SerializerIterator::get8() { int val; if(!mSerializer.get8(val, mPos)) throw(0); @@ -486,7 +493,7 @@ unsigned char SerializerIterator::get8() throw() return val; } -uint16 SerializerIterator::get16() throw() +uint16 SerializerIterator::get16() { uint16 val; if(!mSerializer.get16(val, mPos)) throw(0); @@ -494,7 +501,7 @@ uint16 SerializerIterator::get16() throw() return val; } -uint32 SerializerIterator::get32() throw() +uint32 SerializerIterator::get32() { uint32 val; if(!mSerializer.get32(val, mPos)) throw(0); @@ -502,7 +509,7 @@ uint32 SerializerIterator::get32() throw() return val; } -uint64 SerializerIterator::get64() throw() +uint64 SerializerIterator::get64() { uint64 val; if(!mSerializer.get64(val, mPos)) throw(0); @@ -510,7 +517,15 @@ uint64 SerializerIterator::get64() throw() 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; if(!mSerializer.get160(val, mPos)) throw(0); @@ -518,7 +533,7 @@ uint160 SerializerIterator::get160() throw() return val; } -uint256 SerializerIterator::get256() throw() +uint256 SerializerIterator::get256() { uint256 val; if(!mSerializer.get256(val, mPos)) throw(0); @@ -526,7 +541,7 @@ uint256 SerializerIterator::get256() throw() return val; } -std::vector SerializerIterator::getVL() throw() +std::vector SerializerIterator::getVL() { int length; std::vector vl; @@ -535,7 +550,7 @@ std::vector SerializerIterator::getVL() throw() return vl; } -std::vector SerializerIterator::getTaggedList() throw() +std::vector SerializerIterator::getTaggedList() { int length; std::vector tl; diff --git a/src/Serializer.h b/src/Serializer.h index 8ed1ffd5a9..0573ef3be0 100644 --- a/src/Serializer.h +++ b/src/Serializer.h @@ -49,6 +49,7 @@ class Serializer bool get16(uint16&, int offset) const; bool get32(uint32&, int offset) const; bool get64(uint64&, int offset) const; + bool get128(uint128&, int offset) const; bool get160(uint160&, int offset) const; bool get256(uint256&, int offset) const; uint256 get256(int offset) const; @@ -87,12 +88,12 @@ class Serializer bool addSignature(CKey& rkey); // low-level VL length encode/decode functions - static std::vector encodeVL(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(); - static int decodeVLLength(int b1, int b2, int b3) throw(); + static std::vector encodeVL(int length); + static int encodeLengthLength(int length); // length to encode length + static int decodeLengthLength(int b1); + static int decodeVLLength(int b1); + static int decodeVLLength(int b1, int b2); + static int decodeVLLength(int b1, int b2, int b3); static void TestSerializer(); }; @@ -113,15 +114,17 @@ public: int getPos(void) { return mPos; } int getBytesLeft(); - unsigned char get8() throw(); - uint16 get16() throw(); - uint32 get32() throw(); - uint64 get64() throw(); - uint160 get160() throw(); - uint256 get256() throw(); + // get functions throw on error + unsigned char get8(); + uint16 get16(); + uint32 get32(); + uint64 get64(); + uint128 get128(); + uint160 get160(); + uint256 get256(); - std::vector getVL() throw(); - std::vector getTaggedList() throw(); + std::vector getVL(); + std::vector getTaggedList(); }; #endif