data: recognize Data in Packet Format v0.3

Data::wireDecode accepts both v0.2 and v0.3 formats,
but Data::wireEncode only encodes into v0.2 format.

refs #4568

Change-Id: I287d12c599f44d802a2a2cd2f13132c9f98a842a
diff --git a/src/data.cpp b/src/data.cpp
index 6b50dca..090b82d 100644
--- a/src/data.cpp
+++ b/src/data.cpp
@@ -49,8 +49,8 @@
 {
   // Data ::= DATA-TLV TLV-LENGTH
   //            Name
-  //            MetaInfo
-  //            Content
+  //            MetaInfo?
+  //            Content?
   //            SignatureInfo
   //            SignatureValue
 
@@ -121,26 +121,75 @@
 void
 Data::wireDecode(const Block& wire)
 {
-  m_fullName.clear();
   m_wire = wire;
   m_wire.parse();
+  bool hasName = false, hasSigInfo = false;
+  m_name.clear();
+  m_metaInfo = MetaInfo();
+  m_content = Block(tlv::Content);
+  m_signature = Signature();
+  m_fullName.clear();
 
-  // Name
-  m_name.wireDecode(m_wire.get(tlv::Name));
+  int lastEle = 0; // last recognized element index, in spec order
+  for (const Block& ele : m_wire.elements()) {
+    switch (ele.type()) {
+      case tlv::Name: {
+        if (lastEle >= 1) {
+          BOOST_THROW_EXCEPTION(Error("Name element is out of order"));
+        }
+        hasName = true;
+        m_name.wireDecode(ele);
+        lastEle = 1;
+        break;
+      }
+      case tlv::MetaInfo: {
+        if (lastEle >= 2) {
+          BOOST_THROW_EXCEPTION(Error("MetaInfo element is out of order"));
+        }
+        m_metaInfo.wireDecode(ele);
+        lastEle = 2;
+        break;
+      }
+      case tlv::Content: {
+        if (lastEle >= 3) {
+          BOOST_THROW_EXCEPTION(Error("Content element is out of order"));
+        }
+        m_content = ele;
+        lastEle = 3;
+        break;
+      }
+      case tlv::SignatureInfo: {
+        if (lastEle >= 4) {
+          BOOST_THROW_EXCEPTION(Error("SignatureInfo element is out of order"));
+        }
+        hasSigInfo = true;
+        m_signature.setInfo(ele);
+        lastEle = 4;
+        break;
+      }
+      case tlv::SignatureValue: {
+        if (lastEle >= 5) {
+          BOOST_THROW_EXCEPTION(Error("SignatureValue element is out of order"));
+        }
+        m_signature.setValue(ele);
+        lastEle = 5;
+        break;
+      }
+      default: {
+        if (tlv::isCriticalType(ele.type())) {
+          BOOST_THROW_EXCEPTION(Error("unrecognized element of critical type " +
+                                      to_string(ele.type())));
+        }
+        break;
+      }
+    }
+  }
 
-  // MetaInfo
-  m_metaInfo.wireDecode(m_wire.get(tlv::MetaInfo));
-
-  // Content
-  m_content = m_wire.get(tlv::Content);
-
-  // SignatureInfo
-  m_signature.setInfo(m_wire.get(tlv::SignatureInfo));
-
-  // SignatureValue
-  Block::element_const_iterator val = m_wire.find(tlv::SignatureValue);
-  if (val != m_wire.elements_end()) {
-    m_signature.setValue(*val);
+  if (!hasName) {
+    BOOST_THROW_EXCEPTION(Error("Name element is missing"));
+  }
+  if (!hasSigInfo) {
+    BOOST_THROW_EXCEPTION(Error("SignatureInfo element is missing"));
   }
 }
 
diff --git a/src/data.hpp b/src/data.hpp
index 8eb1b3d..2368017 100644
--- a/src/data.hpp
+++ b/src/data.hpp
@@ -45,21 +45,23 @@
     }
   };
 
-  /** @brief Create a new Data with the given name and empty Content
-   *  @warning In certain contexts that use Data::shared_from_this(), Data must be created
-   *           using `make_shared`. Otherwise, .shared_from_this() will trigger undefined behavior.
+  /** @brief Construct an unsigned Data packet with given @p name and empty Content.
+   *  @warning In certain contexts that use `Data::shared_from_this()`, Data must be created
+   *           using `make_shared`. Otherwise, `shared_from_this()` will trigger undefined behavior.
    */
   explicit
   Data(const Name& name = Name());
 
-  /** @brief Create from wire encoding
-   *  @warning In certain contexts that use Data::shared_from_this(), Data must be created
-   *           using `make_shared`. Otherwise, .shared_from_this() will trigger undefined behavior.
+  /** @brief Construct a Data packet by decoding from @p wire.
+   *  @param wire @c tlv::Data element as defined in NDN Packet Format v0.2 or v0.3.
+   *              It may be signed or unsigned.
+   *  @warning In certain contexts that use `Data::shared_from_this()`, Data must be created
+   *           using `make_shared`. Otherwise, `shared_from_this()` will trigger undefined behavior.
    */
   explicit
   Data(const Block& wire);
 
-  /** @brief Fast encoding or block size estimation
+  /** @brief Prepend wire encoding to @p encoder in NDN Packet Format v0.2.
    *  @param encoder EncodingEstimator or EncodingBuffer instance
    *  @param wantUnsignedPortionOnly If true, only prepends Name, MetaInfo, Content, and
    *         SignatureInfo to @p encoder, but omit SignatureValue and outmost Type-Length of Data
@@ -89,17 +91,22 @@
   const Block&
   wireEncode(EncodingBuffer& encoder, const Block& signatureValue) const;
 
-  /** @brief Encode to a wire format
+  /** @brief Encode to a @c Block.
+   *  @pre Data is signed.
+   *
+   *  Normally, this function encodes to NDN Packet Format v0.2. However, if this instance has
+   *  cached wire encoding (\c hasWire() is true), the cached encoding is returned and it might
+   *  be in v0.3 format.
    */
   const Block&
   wireEncode() const;
 
-  /** @brief Decode from the wire format
+  /** @brief Decode from @p wire in NDN Packet Format v0.2 or v0.3.
    */
   void
   wireDecode(const Block& wire);
 
-  /** @brief Check if already has wire
+  /** @brief Check if this instance has cached wire encoding.
    */
   bool
   hasWire() const