encoding: Extending Block and EncodingBuffer interfaces

Now it is possible to use constructors to create Block from
EncodingBuffer and EncodingBuffer from Block.

Block->EncodingBuffer conversion is potentially dangerous and should be
used only in exceptional cases, such as Data packet encoding: to encode
the signed part first and then extend signed part with signature and
other related fields.

Change-Id: I5a13bf0c196ecd0d45dfa14c4cb6f4a9f612420c
diff --git a/src/encoding/block.cpp b/src/encoding/block.cpp
index 5409879..37b2e6f 100644
--- a/src/encoding/block.cpp
+++ b/src/encoding/block.cpp
@@ -11,6 +11,7 @@
 
 #include "block.hpp"
 #include "tlv.hpp"
+#include "encoding-buffer.hpp"
 
 namespace ndn {
 
@@ -19,6 +20,23 @@
 {
 }
 
+Block::Block(const EncodingBuffer& buffer)
+  : m_buffer(buffer.m_buffer)
+  , m_begin(buffer.begin())
+  , m_end(buffer.end())
+  , m_size(m_end - m_begin)
+{
+  m_value_begin = m_begin;
+  m_value_end   = m_end;
+
+  m_type = Tlv::readType(m_value_begin, m_value_end);
+  uint64_t length = Tlv::readVarNumber(m_value_begin, m_value_end);
+  if (length != static_cast<uint64_t>(m_value_end - m_value_begin))
+    {
+      throw Tlv::Error("TLV length doesn't match buffer length");
+    }
+}
+
 Block::Block(const ConstBufferPtr &wire,
              uint32_t type,
              const Buffer::const_iterator &begin, const Buffer::const_iterator &end,