variable length buffer for LeaseSet

This commit is contained in:
orignal 2015-04-08 09:39:02 -04:00
parent e9edc7b205
commit 9ce9d9b7fc
2 changed files with 15 additions and 7 deletions

View File

@ -14,8 +14,9 @@ namespace i2p
namespace data namespace data
{ {
LeaseSet::LeaseSet (const uint8_t * buf, int len) LeaseSet::LeaseSet (const uint8_t * buf, size_t len)
{ {
m_Buffer = new uint8_t[len];
memcpy (m_Buffer, buf, len); memcpy (m_Buffer, buf, len);
m_BufferLen = len; m_BufferLen = len;
ReadFromBuffer (); ReadFromBuffer ();
@ -27,10 +28,12 @@ namespace data
const i2p::data::LocalDestination * localDestination = pool.GetLocalDestination (); const i2p::data::LocalDestination * localDestination = pool.GetLocalDestination ();
if (!localDestination) if (!localDestination)
{ {
m_Buffer = nullptr;
m_BufferLen = 0; m_BufferLen = 0;
LogPrint (eLogError, "Destination for local LeaseSet doesn't exist"); LogPrint (eLogError, "Destination for local LeaseSet doesn't exist");
return; return;
} }
m_Buffer = new uint8_t[localDestination->GetIdentity ().GetFullLen ()];
m_BufferLen = localDestination->GetIdentity ().ToBuffer (m_Buffer, MAX_LS_BUFFER_SIZE); m_BufferLen = localDestination->GetIdentity ().ToBuffer (m_Buffer, MAX_LS_BUFFER_SIZE);
memcpy (m_Buffer + m_BufferLen, localDestination->GetEncryptionPublicKey (), 256); memcpy (m_Buffer + m_BufferLen, localDestination->GetEncryptionPublicKey (), 256);
m_BufferLen += 256; m_BufferLen += 256;
@ -62,9 +65,15 @@ namespace data
ReadFromBuffer (); ReadFromBuffer ();
} }
void LeaseSet::Update (const uint8_t * buf, int len) void LeaseSet::Update (const uint8_t * buf, size_t len)
{ {
m_Leases.clear (); m_Leases.clear ();
if (len > m_BufferLen)
{
auto oldBuffer = m_Buffer;
m_Buffer = new uint8_t[len];
delete[] oldBuffer;
}
memcpy (m_Buffer, buf, len); memcpy (m_Buffer, buf, len);
m_BufferLen = len; m_BufferLen = len;
ReadFromBuffer (); ReadFromBuffer ();

View File

@ -36,11 +36,10 @@ namespace data
{ {
public: public:
LeaseSet (const uint8_t * buf, int len); LeaseSet (const uint8_t * buf, size_t len);
LeaseSet (const LeaseSet& ) = default;
LeaseSet (const i2p::tunnel::TunnelPool& pool); LeaseSet (const i2p::tunnel::TunnelPool& pool);
LeaseSet& operator=(const LeaseSet& ) = default; ~LeaseSet () { delete[] m_Buffer; };
void Update (const uint8_t * buf, int len); void Update (const uint8_t * buf, size_t len);
const IdentityEx& GetIdentity () const { return m_Identity; }; const IdentityEx& GetIdentity () const { return m_Identity; };
const uint8_t * GetBuffer () const { return m_Buffer; }; const uint8_t * GetBuffer () const { return m_Buffer; };
@ -64,7 +63,7 @@ namespace data
std::vector<Lease> m_Leases; std::vector<Lease> m_Leases;
IdentityEx m_Identity; IdentityEx m_Identity;
uint8_t m_EncryptionKey[256]; uint8_t m_EncryptionKey[256];
uint8_t m_Buffer[MAX_LS_BUFFER_SIZE]; uint8_t * m_Buffer;
size_t m_BufferLen; size_t m_BufferLen;
}; };
} }