network: Use early return
This commit is contained in:
@@ -586,66 +586,60 @@ Buffer::Serialize(uint8_t* buffer, uint32_t maxSize) const
|
||||
uint32_t size = 0;
|
||||
|
||||
// Add the zero data length
|
||||
if (size + 4 <= maxSize)
|
||||
{
|
||||
size += 4;
|
||||
*p++ = m_zeroAreaEnd - m_zeroAreaStart;
|
||||
}
|
||||
else
|
||||
size += 4;
|
||||
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
*p++ = m_zeroAreaEnd - m_zeroAreaStart;
|
||||
|
||||
// Add the length of actual start data
|
||||
uint32_t dataStartLength = m_zeroAreaStart - m_start;
|
||||
if (size + 4 <= maxSize)
|
||||
{
|
||||
size += 4;
|
||||
*p++ = dataStartLength;
|
||||
}
|
||||
else
|
||||
size += 4;
|
||||
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t dataStartLength = m_zeroAreaStart - m_start;
|
||||
*p++ = dataStartLength;
|
||||
|
||||
// Add the actual data
|
||||
if (size + ((dataStartLength + 3) & (~3)) <= maxSize)
|
||||
{
|
||||
size += (dataStartLength + 3) & (~3);
|
||||
memcpy(p, m_data->m_data + m_start, dataStartLength);
|
||||
p += (((dataStartLength + 3) & (~3)) / 4); // Advance p, insuring 4 byte boundary
|
||||
}
|
||||
else
|
||||
size += (dataStartLength + 3) & (~3);
|
||||
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(p, m_data->m_data + m_start, dataStartLength);
|
||||
p += (((dataStartLength + 3) & (~3)) / 4); // Advance p, insuring 4 byte boundary
|
||||
|
||||
// Add the length of the actual end data
|
||||
uint32_t dataEndLength = m_end - m_zeroAreaEnd;
|
||||
if (size + 4 <= maxSize)
|
||||
{
|
||||
size += 4;
|
||||
*p++ = dataEndLength;
|
||||
}
|
||||
else
|
||||
size += 4;
|
||||
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t dataEndLength = m_end - m_zeroAreaEnd;
|
||||
*p++ = dataEndLength;
|
||||
|
||||
// Add the actual data
|
||||
if (size + ((dataEndLength + 3) & (~3)) <= maxSize)
|
||||
{
|
||||
// The following line is unnecessary.
|
||||
// size += (dataEndLength + 3) & (~3);
|
||||
memcpy(p, m_data->m_data + m_zeroAreaStart, dataEndLength);
|
||||
// The following line is unnecessary.
|
||||
// p += (((dataEndLength + 3) & (~3))/4); // Advance p, insuring 4 byte boundary
|
||||
}
|
||||
else
|
||||
size += (dataEndLength + 3) & (~3);
|
||||
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(p, m_data->m_data + m_zeroAreaStart, dataEndLength);
|
||||
// The following line is unnecessary.
|
||||
// p += (((dataEndLength + 3) & (~3))/4); // Advance p, insuring 4 byte boundary
|
||||
|
||||
// Serialized everything successfully
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -470,19 +470,16 @@ ByteTagList::Serialize(uint32_t* buffer, uint32_t maxSize) const
|
||||
uint32_t* p = buffer;
|
||||
uint32_t size = 0;
|
||||
|
||||
uint32_t* numberOfTags = nullptr;
|
||||
size += 4;
|
||||
|
||||
if (size + 4 <= maxSize)
|
||||
{
|
||||
numberOfTags = p;
|
||||
*p++ = 0;
|
||||
size += 4;
|
||||
}
|
||||
else
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t* numberOfTags = p;
|
||||
*p++ = 0;
|
||||
|
||||
ByteTagList::Iterator i = BeginAll();
|
||||
while (i.HasNext())
|
||||
{
|
||||
@@ -492,62 +489,56 @@ ByteTagList::Serialize(uint32_t* buffer, uint32_t maxSize) const
|
||||
|
||||
// ensure size is multiple of 4 bytes for 4 byte boundaries
|
||||
uint32_t hashSize = (sizeof(TypeId::hash_t) + 3) & (~3);
|
||||
if (size + hashSize <= maxSize)
|
||||
{
|
||||
TypeId::hash_t tid = item.tid.GetHash();
|
||||
memcpy(p, &tid, sizeof(TypeId::hash_t));
|
||||
p += hashSize / 4;
|
||||
size += hashSize;
|
||||
}
|
||||
else
|
||||
size += hashSize;
|
||||
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (size + 4 <= maxSize)
|
||||
{
|
||||
*p++ = item.size;
|
||||
size += 4;
|
||||
}
|
||||
else
|
||||
TypeId::hash_t tid = item.tid.GetHash();
|
||||
memcpy(p, &tid, sizeof(TypeId::hash_t));
|
||||
p += hashSize / 4;
|
||||
|
||||
size += 4;
|
||||
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (size + 4 <= maxSize)
|
||||
{
|
||||
*p++ = item.start;
|
||||
size += 4;
|
||||
}
|
||||
else
|
||||
*p++ = item.size;
|
||||
|
||||
size += 4;
|
||||
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (size + 4 <= maxSize)
|
||||
{
|
||||
*p++ = item.end;
|
||||
size += 4;
|
||||
}
|
||||
else
|
||||
*p++ = item.start;
|
||||
|
||||
size += 4;
|
||||
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
*p++ = item.end;
|
||||
|
||||
// ensure size is multiple of 4 bytes for 4 byte boundaries
|
||||
uint32_t tagWordSize = (item.size + 3) & (~3);
|
||||
size += tagWordSize;
|
||||
|
||||
if (size + tagWordSize <= maxSize)
|
||||
{
|
||||
item.buf.Read(reinterpret_cast<uint8_t*>(p), item.size);
|
||||
size += tagWordSize;
|
||||
p += tagWordSize / 4;
|
||||
}
|
||||
else
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
item.buf.Read(reinterpret_cast<uint8_t*>(p), item.size);
|
||||
p += tagWordSize / 4;
|
||||
|
||||
(*numberOfTags)++;
|
||||
}
|
||||
|
||||
|
||||
@@ -336,60 +336,54 @@ PacketTagList::Serialize(uint32_t* buffer, uint32_t maxSize) const
|
||||
uint32_t* p = buffer;
|
||||
uint32_t size = 0;
|
||||
|
||||
uint32_t* numberOfTags = nullptr;
|
||||
size += 4;
|
||||
|
||||
if (size + 4 <= maxSize)
|
||||
{
|
||||
numberOfTags = p;
|
||||
*p++ = 0;
|
||||
size += 4;
|
||||
}
|
||||
else
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t* numberOfTags = p;
|
||||
*p++ = 0;
|
||||
|
||||
for (TagData* cur = m_next; cur != nullptr; cur = cur->next)
|
||||
{
|
||||
if (size + 4 <= maxSize)
|
||||
{
|
||||
*p++ = cur->size;
|
||||
size += 4;
|
||||
}
|
||||
else
|
||||
size += 4;
|
||||
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
*p++ = cur->size;
|
||||
|
||||
NS_LOG_INFO("Serializing tag id " << cur->tid);
|
||||
|
||||
// ensure size is multiple of 4 bytes for 4 byte boundaries
|
||||
uint32_t hashSize = (sizeof(TypeId::hash_t) + 3) & (~3);
|
||||
if (size + hashSize <= maxSize)
|
||||
{
|
||||
TypeId::hash_t tid = cur->tid.GetHash();
|
||||
memcpy(p, &tid, sizeof(TypeId::hash_t));
|
||||
p += hashSize / 4;
|
||||
size += hashSize;
|
||||
}
|
||||
else
|
||||
size += hashSize;
|
||||
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
TypeId::hash_t tid = cur->tid.GetHash();
|
||||
memcpy(p, &tid, sizeof(TypeId::hash_t));
|
||||
p += hashSize / 4;
|
||||
|
||||
// ensure size is multiple of 4 bytes for 4 byte boundaries
|
||||
uint32_t tagWordSize = (cur->size + 3) & (~3);
|
||||
if (size + tagWordSize <= maxSize)
|
||||
{
|
||||
memcpy(p, cur->data, cur->size);
|
||||
size += tagWordSize;
|
||||
p += tagWordSize / 4;
|
||||
}
|
||||
else
|
||||
size += tagWordSize;
|
||||
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(p, cur->data, cur->size);
|
||||
p += tagWordSize / 4;
|
||||
|
||||
(*numberOfTags)++;
|
||||
}
|
||||
|
||||
|
||||
@@ -669,149 +669,130 @@ Packet::Serialize(uint8_t* buffer, uint32_t maxSize) const
|
||||
if (m_nixVector)
|
||||
{
|
||||
uint32_t nixSize = m_nixVector->GetSerializedSize();
|
||||
if (size + nixSize <= maxSize)
|
||||
{
|
||||
// put the total length of nix-vector in the
|
||||
// buffer. this includes 4-bytes for total
|
||||
// length itself
|
||||
*p++ = nixSize + 4;
|
||||
size += nixSize;
|
||||
|
||||
// serialize the nix-vector
|
||||
uint32_t serialized = m_nixVector->Serialize(p, nixSize);
|
||||
if (serialized)
|
||||
{
|
||||
// increment p by nixSize bytes
|
||||
// ensuring 4-byte boundary
|
||||
p += ((nixSize + 3) & (~3)) / 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
size += nixSize;
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// put the total length of nix-vector in the
|
||||
// buffer. this includes 4-bytes for total
|
||||
// length itself
|
||||
*p++ = nixSize + 4;
|
||||
|
||||
// serialize the nix-vector
|
||||
uint32_t serialized = m_nixVector->Serialize(p, nixSize);
|
||||
if (!serialized)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// increment p by nixSize bytes
|
||||
// ensuring 4-byte boundary
|
||||
p += ((nixSize + 3) & (~3)) / 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
// no nix vector, set zero length,
|
||||
// ie 4-bytes, since it must include
|
||||
// length for itself
|
||||
if (size + 4 <= maxSize)
|
||||
{
|
||||
size += 4;
|
||||
*p++ = 4;
|
||||
}
|
||||
else
|
||||
size += 4;
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
*p++ = 4;
|
||||
}
|
||||
|
||||
// Serialize byte tag list
|
||||
uint32_t byteTagSize = m_byteTagList.GetSerializedSize();
|
||||
if (size + byteTagSize <= maxSize)
|
||||
{
|
||||
// put the total length of byte tag list in the
|
||||
// buffer. this includes 4-bytes for total
|
||||
// length itself
|
||||
*p++ = byteTagSize + 4;
|
||||
size += byteTagSize;
|
||||
|
||||
// serialize the byte tag list
|
||||
uint32_t serialized = m_byteTagList.Serialize(p, byteTagSize);
|
||||
if (serialized)
|
||||
{
|
||||
// increment p by byteTagSize bytes
|
||||
// ensuring 4-byte boundary
|
||||
p += ((byteTagSize + 3) & (~3)) / 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
size += byteTagSize;
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// put the total length of byte tag list in the
|
||||
// buffer. this includes 4-bytes for total
|
||||
// length itself
|
||||
*p++ = byteTagSize + 4;
|
||||
|
||||
// serialize the byte tag list
|
||||
uint32_t serialized = m_byteTagList.Serialize(p, byteTagSize);
|
||||
if (!serialized)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// increment p by byteTagSize bytes
|
||||
// ensuring 4-byte boundary
|
||||
p += ((byteTagSize + 3) & (~3)) / 4;
|
||||
|
||||
// Serialize packet tag list
|
||||
uint32_t packetTagSize = m_packetTagList.GetSerializedSize();
|
||||
if (size + packetTagSize <= maxSize)
|
||||
{
|
||||
// put the total length of packet tag list in the
|
||||
// buffer. this includes 4-bytes for total
|
||||
// length itself
|
||||
*p++ = packetTagSize + 4;
|
||||
size += packetTagSize;
|
||||
|
||||
// serialize the packet tag list
|
||||
uint32_t serialized = m_packetTagList.Serialize(p, packetTagSize);
|
||||
if (serialized)
|
||||
{
|
||||
// increment p by packetTagSize bytes
|
||||
// ensuring 4-byte boundary
|
||||
p += ((packetTagSize + 3) & (~3)) / 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
size += packetTagSize;
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// put the total length of packet tag list in the
|
||||
// buffer. this includes 4-bytes for total
|
||||
// length itself
|
||||
*p++ = packetTagSize + 4;
|
||||
|
||||
// serialize the packet tag list
|
||||
serialized = m_packetTagList.Serialize(p, packetTagSize);
|
||||
if (!serialized)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// increment p by packetTagSize bytes
|
||||
// ensuring 4-byte boundary
|
||||
p += ((packetTagSize + 3) & (~3)) / 4;
|
||||
|
||||
// Serialize Metadata
|
||||
uint32_t metaSize = m_metadata.GetSerializedSize();
|
||||
if (size + metaSize <= maxSize)
|
||||
{
|
||||
// put the total length of metadata in the
|
||||
// buffer. this includes 4-bytes for total
|
||||
// length itself
|
||||
*p++ = metaSize + 4;
|
||||
size += metaSize;
|
||||
|
||||
// serialize the metadata
|
||||
uint32_t serialized = m_metadata.Serialize(reinterpret_cast<uint8_t*>(p), metaSize);
|
||||
if (serialized)
|
||||
{
|
||||
// increment p by metaSize bytes
|
||||
// ensuring 4-byte boundary
|
||||
p += ((metaSize + 3) & (~3)) / 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
size += metaSize;
|
||||
if (size > maxSize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// put the total length of metadata in the
|
||||
// buffer. this includes 4-bytes for total
|
||||
// length itself
|
||||
*p++ = metaSize + 4;
|
||||
|
||||
// serialize the metadata
|
||||
serialized = m_metadata.Serialize(reinterpret_cast<uint8_t*>(p), metaSize);
|
||||
if (!serialized)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// increment p by metaSize bytes
|
||||
// ensuring 4-byte boundary
|
||||
p += ((metaSize + 3) & (~3)) / 4;
|
||||
|
||||
// Serialize the packet contents
|
||||
uint32_t bufSize = m_buffer.GetSerializedSize();
|
||||
if (size + bufSize <= maxSize)
|
||||
size += bufSize;
|
||||
if (size > maxSize)
|
||||
{
|
||||
// put the total length of the buffer in the
|
||||
// buffer. this includes 4-bytes for total
|
||||
// length itself
|
||||
*p++ = bufSize + 4;
|
||||
|
||||
// serialize the buffer
|
||||
uint32_t serialized = m_buffer.Serialize(reinterpret_cast<uint8_t*>(p), bufSize);
|
||||
if (!serialized)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
|
||||
// put the total length of the buffer in the
|
||||
// buffer. this includes 4-bytes for total
|
||||
// length itself
|
||||
*p++ = bufSize + 4;
|
||||
|
||||
// serialize the buffer
|
||||
serialized = m_buffer.Serialize(reinterpret_cast<uint8_t*>(p), bufSize);
|
||||
if (!serialized)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user