Refactor Serializer and SerializerIterator interfaces:

* Remove unused members
* SerialIter holds only a pointer and offset now
* Use free functions for some Serializer members
* Use SerialIter in some places instead of Serializer
This commit is contained in:
Vinnie Falco
2015-01-21 16:51:19 -08:00
committed by Nik Bougalis
parent a691632995
commit bb4127a6fb
57 changed files with 535 additions and 336 deletions

View File

@@ -35,7 +35,7 @@ std::string STAccount::getText () const
}
STAccount*
STAccount::construct (SerializerIterator& u, SField::ref name)
STAccount::construct (SerialIter& u, SField::ref name)
{
return new STAccount (name, u.getVL ());
}

View File

@@ -138,7 +138,7 @@ STAmount::STAmount (Issue const& issue,
}
std::unique_ptr<STAmount>
STAmount::construct (SerializerIterator& sit, SField::ref name)
STAmount::construct (SerialIter& sit, SField::ref name)
{
std::uint64_t value = sit.get64 ();
@@ -202,7 +202,7 @@ STAmount::createFromInt64 (SField::ref name, std::int64_t value)
: STAmount (name, static_cast<std::uint64_t> (-value), true);
}
STAmount STAmount::deserialize (SerializerIterator& it)
STAmount STAmount::deserialize (SerialIter& it)
{
auto s = construct (it, sfGeneric);

View File

@@ -25,7 +25,7 @@
namespace ripple {
std::unique_ptr<STBase>
STArray::deserialize (SerializerIterator& sit, SField::ref field)
STArray::deserialize (SerialIter& sit, SField::ref field)
{
std::unique_ptr <STArray> ret (std::make_unique <STArray> (field));
vector& value (ret->getValue ());

View File

@@ -23,7 +23,7 @@
namespace ripple {
STBlob::STBlob (SerializerIterator& st, SField::ref name)
STBlob::STBlob (SerialIter& st, SField::ref name)
: STBase (name)
{
value = st.getVL ();

View File

@@ -37,7 +37,7 @@ STUInt8::getSType () const
template <>
std::unique_ptr<STBase>
STUInt8::deserialize (SerializerIterator& sit, SField::ref name)
STUInt8::deserialize (SerialIter& sit, SField::ref name)
{
return std::make_unique <STUInt8> (name, sit.get8 ());
}
@@ -86,7 +86,7 @@ STUInt16::getSType () const
template <>
std::unique_ptr<STBase>
STUInt16::deserialize (SerializerIterator& sit, SField::ref name)
STUInt16::deserialize (SerialIter& sit, SField::ref name)
{
return std::make_unique <STUInt16> (name, sit.get16 ());
}
@@ -152,7 +152,7 @@ STUInt32::getSType () const
template <>
std::unique_ptr<STBase>
STUInt32::deserialize (SerializerIterator& sit, SField::ref name)
STUInt32::deserialize (SerialIter& sit, SField::ref name)
{
return std::make_unique <STUInt32> (name, sit.get32 ());
}
@@ -182,7 +182,7 @@ STUInt64::getSType () const
template <>
std::unique_ptr<STBase>
STUInt64::deserialize (SerializerIterator& sit, SField::ref name)
STUInt64::deserialize (SerialIter& sit, SField::ref name)
{
return std::make_unique <STUInt64> (name, sit.get64 ());
}

View File

@@ -28,7 +28,7 @@
namespace ripple {
STLedgerEntry::STLedgerEntry (
SerializerIterator& sit, uint256 const& index)
SerialIter& sit, uint256 const& index)
: STObject (sfLedgerEntry), mIndex (index), mMutable (true)
{
set (sit);
@@ -40,7 +40,7 @@ STLedgerEntry::STLedgerEntry (
: STObject (sfLedgerEntry), mIndex (index), mMutable (true)
{
// we know 's' isn't going away
SerializerIterator sit (const_cast<Serializer&> (s));
SerialIter sit (const_cast<Serializer&> (s));
set (sit);
setSLEType ();
}

View File

@@ -94,7 +94,7 @@ STObject::makeDefaultObject (SerializedTypeID id, SField::ref name)
// VFALCO TODO Remove the 'depth' parameter
std::unique_ptr<STBase>
STObject::makeDeserializedObject (SerializedTypeID id, SField::ref name,
SerializerIterator& sit, int depth)
SerialIter& sit, int depth)
{
switch (id)
{
@@ -259,7 +259,7 @@ bool STObject::isFieldAllowed (SField::ref field)
}
// return true = terminated with end-of-object
bool STObject::set (SerializerIterator& sit, int depth)
bool STObject::set (SerialIter& sit, int depth)
{
bool reachedEndOfObject = false;
@@ -313,7 +313,7 @@ bool STObject::set (SerializerIterator& sit, int depth)
std::unique_ptr<STBase>
STObject::deserialize (SerializerIterator& sit, SField::ref name)
STObject::deserialize (SerialIter& sit, SField::ref name)
{
std::unique_ptr <STObject> object (std::make_unique <STObject> (name));
object->set (sit, 1);

View File

@@ -51,7 +51,7 @@ STPathElement::get_hash (STPathElement const& element)
}
std::unique_ptr<STBase>
STPathSet::deserialize (SerializerIterator& sit, SField::ref name)
STPathSet::deserialize (SerialIter& sit, SField::ref name)
{
std::vector<STPathElement> path;

View File

@@ -74,7 +74,7 @@ STTx::STTx (STObject const& object)
}
}
STTx::STTx (SerializerIterator& sit)
STTx::STTx (SerialIter& sit)
: STObject (sfTransaction)
, sig_state_ (boost::indeterminate)
{

View File

@@ -25,7 +25,7 @@
namespace ripple {
STValidation::STValidation (SerializerIterator& sit, bool checkSignature)
STValidation::STValidation (SerialIter& sit, bool checkSignature)
: STObject (getFormat (), sit, sfValidation)
, mTrusted (false)
{

View File

@@ -26,7 +26,7 @@
namespace ripple {
std::unique_ptr<STBase>
STVector256::deserialize (SerializerIterator& sit, SField::ref name)
STVector256::deserialize (SerialIter& sit, SField::ref name)
{
auto vec = std::make_unique<STVector256> (name);

View File

@@ -337,25 +337,10 @@ uint256 Serializer::getSHA512Half (int size) const
if (size == 0)
return uint256();
if (size < 0 || size > mData.size())
return getSHA512Half (mData);
return ripple::getSHA512Half (mData);
return getSHA512Half (const_byte_view (
mData.data(), mData.data() + size));
}
uint256 Serializer::getSHA512Half (const_byte_view v)
{
uint256 j[2];
SHA512 (v.data(), v.size(),
reinterpret_cast<unsigned char*> (j));
return j[0];
}
uint256 Serializer::getSHA512Half (const unsigned char* data, int len)
{
uint256 j[2];
SHA512 (data, len, (unsigned char*) j);
return j[0];
return ripple::getSHA512Half (
mData.data(), size);
}
uint256 Serializer::getPrefixHash (std::uint32_t prefix, const unsigned char* data, int len)
@@ -565,87 +550,170 @@ int Serializer::decodeVLLength (int b1, int b2, int b3)
return 12481 + (b1 - 241) * 65536 + b2 * 256 + b3;
}
void Serializer::TestSerializer ()
//------------------------------------------------------------------------------
SerialIter::SerialIter (void const* data,
std::size_t size) noexcept
: p_ (reinterpret_cast<
std::uint8_t const*>(data))
, remain_ (size)
{
Serializer s (64);
}
int SerializerIterator::getBytesLeft ()
void
SerialIter::reset() noexcept
{
return mSerializer.size () - mPos;
p_ -= used_;
remain_ += used_;
used_ = 0;
}
void SerializerIterator::getFieldID (int& type, int& field)
unsigned char
SerialIter::get8()
{
if (!mSerializer.getFieldID (type, field, mPos))
throw std::runtime_error ("invalid serializer getFieldID");
++mPos;
if (type >= 16)
++mPos;
if (field >= 16)
++mPos;
if (remain_ < 1)
throw std::runtime_error(
"invalid SerialIter get8");
unsigned char t = *p_;
++p_;
++used_;
--remain_;
return t;
}
unsigned char SerializerIterator::get8 ()
std::uint16_t
SerialIter::get16()
{
int val;
if (!mSerializer.get8 (val, mPos)) throw std::runtime_error ("invalid serializer get8");
++mPos;
return val;
if (remain_ < 2)
throw std::runtime_error(
"invalid SerialIter get16");
auto t = p_;
p_ += 2;
used_ += 2;
remain_ -= 2;
return
(std::uint64_t(t[0]) << 8) +
std::uint64_t(t[1] );
}
std::uint16_t SerializerIterator::get16 ()
std::uint32_t
SerialIter::get32()
{
std::uint16_t val;
if (!mSerializer.get16 (val, mPos)) throw std::runtime_error ("invalid serializer get16");
mPos += 16 / 8;
return val;
if (remain_ < 4)
throw std::runtime_error(
"invalid SerialIter get32");
auto t = p_;
p_ += 4;
used_ += 4;
remain_ -= 4;
return
(std::uint64_t(t[0]) << 24) +
(std::uint64_t(t[1]) << 16) +
(std::uint64_t(t[2]) << 8) +
std::uint64_t(t[3] );
}
std::uint32_t SerializerIterator::get32 ()
std::uint64_t
SerialIter::get64 ()
{
std::uint32_t val;
if (!mSerializer.get32 (val, mPos)) throw std::runtime_error ("invalid serializer get32");
mPos += 32 / 8;
return val;
if (remain_ < 8)
throw std::runtime_error(
"invalid SerialIter get64");
auto t = p_;
p_ += 8;
used_ += 8;
remain_ -= 8;
return
(std::uint64_t(t[0]) << 56) +
(std::uint64_t(t[1]) << 48) +
(std::uint64_t(t[2]) << 40) +
(std::uint64_t(t[3]) << 32) +
(std::uint64_t(t[4]) << 24) +
(std::uint64_t(t[5]) << 16) +
(std::uint64_t(t[6]) << 8) +
std::uint64_t(t[7] );
}
std::uint64_t SerializerIterator::get64 ()
void
SerialIter::getFieldID (int& type, int& name)
{
std::uint64_t val;
type = get8();
name = type & 15;
type >>= 4;
if (!mSerializer.get64 (val, mPos)) throw std::runtime_error ("invalid serializer get64");
if (type == 0)
{
// uncommon type
type = get8();
if (type == 0 || type < 16)
throw std::runtime_error(
"gFID: uncommon type out of range " +
std::to_string(type));
}
mPos += 64 / 8;
return val;
if (name == 0)
{
// uncommon name
name = get8();
if (name == 0 || name < 16)
throw std::runtime_error(
"gFID: uncommon name out of range " +
std::to_string(name));
}
}
Blob SerializerIterator::getVL ()
// VFALCO DEPRECATED Returns a copy
Blob
SerialIter::getRaw (int size)
{
int length;
Blob vl;
if (remain_ < size)
throw std::runtime_error(
"invalid SerialIter getRaw");
Blob b (p_, p_ + size);
p_ += size;
used_ += size;
remain_ -= size;
return b;
if (!mSerializer.getVL (vl, mPos, length)) throw std::runtime_error ("invalid serializer getVL");
mPos += length;
return vl;
}
Blob SerializerIterator::getRaw (int iLength)
// VFALCO DEPRECATED Returns a copy
Blob
SerialIter::getVL()
{
int iPos = mPos;
mPos += iLength;
int b1 = get8();
int datLen;
int lenLen = Serializer::decodeLengthLength(b1);
if (lenLen == 1)
{
datLen = Serializer::decodeVLLength (b1);
}
else if (lenLen == 2)
{
int b2 = get8();
datLen = Serializer::decodeVLLength (b1, b2);
}
else
{
assert(lenLen == 3);
int b2 = get8();
int b3 = get8();
datLen = Serializer::decodeVLLength (b1, b2, b3);
}
return getRaw(datLen);
}
return mSerializer.getRaw (iPos, iLength);
//------------------------------------------------------------------------------
uint256
getSHA512Half (void const* data, int len)
{
uint256 j[2];
SHA512 (
reinterpret_cast<unsigned char const*>(
data), len, (unsigned char*) j);
return j[0];
}
} // ripple