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/common.hpp b/src/common.hpp
index 647e306..8d26a14 100644
--- a/src/common.hpp
+++ b/src/common.hpp
@@ -54,6 +54,10 @@
 using std::make_shared;
 using std::enable_shared_from_this;
 
+using std::static_pointer_cast;
+using std::dynamic_pointer_cast;
+using std::const_pointer_cast;
+
 using std::function;
 using std::bind;
 using std::placeholders; // _1, _2, ..
@@ -79,6 +83,10 @@
 using boost::make_shared;
 using boost::enable_shared_from_this;
 
+using boost::static_pointer_cast;
+using boost::dynamic_pointer_cast;
+using boost::const_pointer_cast;
+
 using boost::function;
 using boost::bind;
 
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,
diff --git a/src/encoding/block.hpp b/src/encoding/block.hpp
index a2f19be..895df49 100644
--- a/src/encoding/block.hpp
+++ b/src/encoding/block.hpp
@@ -39,6 +39,12 @@
   Block();
 
   /**
+   * @brief Create block based on EncodingBuffer object
+   */
+  explicit
+  Block(const EncodingBuffer& buffer);
+  
+  /**
    * @brief A helper version of a constructor to create Block from the raw buffer (type and value-length parsing)
    */
   Block(const ConstBufferPtr &buffer);
diff --git a/src/encoding/encoding-buffer.hpp b/src/encoding/encoding-buffer.hpp
index b4c9018..fe5dc3a 100644
--- a/src/encoding/encoding-buffer.hpp
+++ b/src/encoding/encoding-buffer.hpp
@@ -55,6 +55,24 @@
     m_begin = m_end = m_buffer->end () - (reserveFromBack < totalReserve ? reserveFromBack : 0);
   }
 
+  /**
+   * @brief Create EncodingBlock from existing block
+   *
+   * This is a dangerous constructor and should be used with caution.
+   * It will modify contents of the buffer that is used by block and may
+   * impact data in other blocks.
+   *
+   * The primary purpose for this method is to be used to extend Block
+   * after sign operation.
+   */
+  explicit
+  EncodingImpl (const Block& block)
+    : m_buffer(const_pointer_cast<Buffer>(block.m_buffer))
+    , m_begin(m_buffer->begin() + (block.begin() - m_buffer->begin()))
+    , m_end(m_buffer->begin()   + (block.end()   - m_buffer->begin()))
+  {
+  }
+  
   inline size_t
   size () const;
 
@@ -128,6 +146,8 @@
   Buffer::iterator m_begin;
   // invariant: m_end always points to the position of next unwritten byte (if appending data)
   Buffer::iterator m_end;
+
+  friend class Block;
 };