11#define MS_CLASS " RTC::TestSerializable:FooPacket"
2- // #define MS_LOG_DEV_LEVEL 3
2+ #define MS_LOG_DEV_LEVEL 3
33
44#include " RTC/TestSerializable/FooPacket.hpp"
55#include " Logger.hpp"
@@ -42,21 +42,18 @@ bool FooPacket::IsFooPacket(const uint8_t* buffer, size_t bufferLength)
4242 return true ;
4343}
4444
45- FooPacket* FooPacket::Parse (const uint8_t * buffer, size_t length )
45+ FooPacket* FooPacket::Parse (const uint8_t * buffer, size_t bufferLength )
4646{
4747 MS_TRACE ();
4848
49- if (!FooPacket::IsFooPacket (buffer, length ))
49+ if (!FooPacket::IsFooPacket (buffer, bufferLength ))
5050 {
5151 MS_WARN_DEV (" not a FooPacket" );
5252
5353 return nullptr ;
5454 }
5555
56- // NOTE: Here we are passing `length` as `bufferLength`. However we know
57- // that, due to FooPacket nature, a FooPacket Packet must occupy the whole
58- // given buffer.
59- auto * packet = new FooPacket (buffer, length);
56+ auto * packet = new FooPacket (buffer, bufferLength);
6057
6158 // Pointer that starts at the beginning of the buffer and it's incremented
6259 // to point to different parts of the Packet.
@@ -67,22 +64,21 @@ FooPacket* FooPacket::Parse(const uint8_t* buffer, size_t length)
6764
6865 while (ptr < buffer + packet->GetLengthField ())
6966 {
70- // Here we must anticipate the id of each item to use its appropriate.
71- if (ptr + FooItem::ItemHeaderLength > packet->GetPaddingPointer ())
67+ // The remaining length in the buffer (excluding padding in the packet)
68+ // is the potential buffer length of the item.
69+ size_t itemBufferLength = packet->GetPaddingPointer () - ptr;
70+
71+ FooItem::ItemId itemId;
72+ uint8_t valueLength;
73+
74+ if (!FooItem::IsFooItem (ptr, itemBufferLength, itemId, valueLength))
7275 {
73- MS_WARN_DEV (" no space for item header " );
76+ MS_WARN_DEV (" not a FooItem " );
7477
7578 delete packet;
7679 return nullptr ;
7780 }
7881
79- const auto * itemHeader = reinterpret_cast <const FooItem::ItemHeader*>(ptr);
80- auto itemId = itemHeader->id ;
81-
82- // The remaining length in the buffer (excluding padding in the packet)
83- // is the potential buffer length of the item.
84- size_t itemBufferLength = packet->GetPaddingPointer () - ptr;
85-
8682 FooItem* item{ nullptr };
8783
8884 MS_DEBUG_DEV (" parsing FooItem [ptr:%zu, id:%" PRIu8 " ]" , ptr - buffer, itemId);
@@ -154,9 +150,9 @@ FooPacket* FooPacket::Parse(const uint8_t* buffer, size_t length)
154150
155151 const size_t computedLength = Utils::Byte::PadTo4Bytes (static_cast <size_t >(ptr - buffer));
156152
157- // Ensure computed length (padded to 4 bytes) matches the total given
153+ // Ensure computed length (padded to 4 bytes) matches the total given buffer
158154 // length.
159- if (computedLength != length )
155+ if (computedLength != bufferLength )
160156 {
161157 MS_WARN_DEV (" computed padded length != buffer length" );
162158
0 commit comments