data: Ensure that content block always exists, even content wasn't set explicitly
Before this commit, when setContent was never called on a Data packet,
the library was skipping Content part, which violates the packet format.
Change-Id: Ic175c384c16e828fa40836d952f4688fe3edc311
diff --git a/src/data.hpp b/src/data.hpp
index e2d9451..29a38d6 100644
--- a/src/data.hpp
+++ b/src/data.hpp
@@ -216,6 +216,9 @@
inline const Block&
Data::getContent() const
{
+ if (content_.empty())
+ content_ = dataBlock(Tlv::Content, reinterpret_cast<const uint8_t*>(0), 0);
+
if (!content_.hasWire())
content_.encode();
return content_;
diff --git a/src/encoding/block.hpp b/src/encoding/block.hpp
index 9c2777d..984e8aa 100644
--- a/src/encoding/block.hpp
+++ b/src/encoding/block.hpp
@@ -90,6 +90,12 @@
*/
explicit
Block(uint32_t type, const Block &value);
+
+ /**
+ * @brief Check if the Block is empty
+ */
+ inline bool
+ empty() const;
/**
* @brief Check if the Block has fully encoded wire
@@ -207,6 +213,13 @@
////////////////////////////////////////////////////////////////////////////////
inline bool
+Block::empty() const
+{
+ return m_type == std::numeric_limits<uint32_t>::max();
+}
+
+
+inline bool
Block::hasWire() const
{
return m_buffer && (m_begin != m_end);