encoding: provide _block literal operator

_block literal operator is moved from tests and becomes public API.

refs #4722

Change-Id: I07cc040af3c4288104b66f11d91830dc9f28835a
diff --git a/src/encoding/block.cpp b/src/encoding/block.cpp
index fc9a0dc..31e6d22 100644
--- a/src/encoding/block.cpp
+++ b/src/encoding/block.cpp
@@ -25,6 +25,7 @@
 #include "buffer-stream.hpp"
 #include "encoding-buffer.hpp"
 #include "tlv.hpp"
+#include "../security/transform.hpp"
 #include "../util/string-helper.hpp"
 
 #include <boost/asio/buffer.hpp>
@@ -517,4 +518,28 @@
   return os;
 }
 
+Block
+operator "" _block(const char* input, std::size_t len)
+{
+  namespace t = security::transform;
+  t::StepSource ss;
+  OBufferStream os;
+  ss >> t::hexDecode() >> t::streamSink(os);
+
+  for (const char* end = input + len; input != end; ++input) {
+    if (std::strchr("0123456789ABCDEF", *input) != nullptr) {
+      ss.write(reinterpret_cast<const uint8_t*>(input), 1);
+    }
+  }
+
+  try {
+    ss.end();
+  }
+  catch (const t::Error&) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument("input has odd number of hexadecimal digits"));
+  }
+
+  return Block(os.buf());
+}
+
 } // namespace ndn
diff --git a/src/encoding/block.hpp b/src/encoding/block.hpp
index e25da9e..4bbb162 100644
--- a/src/encoding/block.hpp
+++ b/src/encoding/block.hpp
@@ -470,6 +470,22 @@
   return !(lhs == rhs);
 }
 
+/** \brief Construct a \c Block from hexadecimal \p input.
+ *  \param input a string containing hexadecimal bytes and comments.
+ *               0-9 and upper-case A-F are input; all other characters are comments.
+ *  \param len length of \p input.
+ *  \throw std::invalid_argument input is empty or has odd number of hexadecimal digits.
+ *  \throw tlv::Error input cannot be parsed into valid \c Block.
+ *
+ *  Example
+ *  \code
+ *  Block nameBlock = "0706 080141 080142"_block;
+ *  Block nackBlock = "FD032005 reason(no-route)=FD03210196"_block;
+ *  \endcode
+ */
+Block
+operator "" _block(const char* input, std::size_t len);
+
 } // namespace ndn
 
 #endif // NDN_ENCODING_BLOCK_HPP