data: Implementing Data::getFullName() method to get Data packet name with implicit digest

Note that getFullName() method will throw a Data::Error if Data packet
does not have wire encoding, i.e., it has not been constructed from wire
and is not yet signed.

Change-Id: I7a9b8a6c9e4c6eced9bc907bfc81adb5d4f0e4b3
Refs: #1298
diff --git a/src/data.cpp b/src/data.cpp
index 8ce8f4c..596e20b 100644
--- a/src/data.cpp
+++ b/src/data.cpp
@@ -116,6 +116,7 @@
 void
 Data::wireDecode(const Block& wire)
 {
+  m_fullName.clear();
   m_wire = wire;
   m_wire.parse();
 
@@ -156,6 +157,21 @@
   return *this;
 }
 
+const Name&
+Data::getFullName() const
+{
+  if (m_fullName.empty()) {
+    if (!m_wire.hasWire()) {
+      throw Error("Full name requested, but Data packet does not have wire format "
+                  "(e.g., not signed)");
+    }
+    m_fullName = m_name;
+    m_fullName.append(name::Component(crypto::sha256(m_wire.wire(), m_wire.size())));
+  }
+
+  return m_fullName;
+}
+
 Data&
 Data::setMetaInfo(const MetaInfo& metaInfo)
 {
@@ -275,6 +291,7 @@
   // the application to do proper re-signing if necessary
 
   m_wire.reset();
+  m_fullName.clear();
 }
 
 bool
diff --git a/src/data.hpp b/src/data.hpp
index 9338258..70d1a72 100644
--- a/src/data.hpp
+++ b/src/data.hpp
@@ -157,6 +157,15 @@
   //
 
   /**
+   * @brief Get full name of Data packet, including the implicit digest
+   *
+   * @throws Error if Data packet doesn't have a full name yet (wire encoding has not been
+   *         yet created)
+   */
+  const Name&
+  getFullName() const;
+
+  /**
    * @brief Get MetaInfo block from Data packet
    */
   const MetaInfo&
@@ -306,6 +315,7 @@
   Signature m_signature;
 
   mutable Block m_wire;
+  mutable Name m_fullName;
 
   nfd::LocalControlHeader m_localControlHeader;
   friend class nfd::LocalControlHeader;
diff --git a/src/interest.cpp b/src/interest.cpp
index 590d527..986a5ec 100644
--- a/src/interest.cpp
+++ b/src/interest.cpp
@@ -68,13 +68,13 @@
     return false;
 
   if (getMinSuffixComponents() >= 0 &&
-    // Add 1 for the implicit digest.
-      !(name.size() + 1 - m_name.size() >= static_cast<size_t>(getMinSuffixComponents())))
+      // name must include implicit digest
+      !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents())))
     return false;
 
   if (getMaxSuffixComponents() >= 0 &&
-    // Add 1 for the implicit digest.
-      !(name.size() + 1 - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents())))
+      // name must include implicit digest
+      !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents())))
     return false;
 
   if (!getExclude().empty() &&
@@ -88,7 +88,7 @@
 bool
 Interest::matchesData(const Data& data) const
 {
-  if (!this->matchesName(data.getName())) {
+  if (!this->matchesName(data.getFullName())) {
     return false;
   }