Data: Change the cached wireEncoding_ to defaultWireEncoding_ so we only cache an encoding using the default wire format. Change getWireEncoding() to getDefaultWireEncoding().
diff --git a/include/ndn-cpp/data.hpp b/include/ndn-cpp/data.hpp
index 1cd00f6..43615d3 100644
--- a/include/ndn-cpp/data.hpp
+++ b/include/ndn-cpp/data.hpp
@@ -155,16 +155,18 @@
Data& operator=(const Data& data);
/**
- * Encode this Data for a particular wire format. Also, set the wireEncoding field to the encoded result.
- * This is not const because it updates the wireEncoding.
+ * Encode this Data for a particular wire format. If wireFormat is the default wire format, also set the defaultWireEncoding
+ * field to the encoded result.
+ * Even though this is const, if wireFormat is the default wire format we update the defaultWireEncoding.
* @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
* @return The encoded byte array.
*/
SignedBlob
- wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
+ wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const;
/**
- * Decode the input using a particular wire format and update this Data. Also, set the wireEncoding field to the input.
+ * Decode the input using a particular wire format and update this Data. If wireFormat is the default wire format, also
+ * set the defaultWireEncoding field to the input.
* @param input The input byte array to be decoded.
* @param inputLength The length of input.
* @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
@@ -173,7 +175,8 @@
wireDecode(const uint8_t* input, size_t inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
/**
- * Decode the input using a particular wire format and update this Data. Also, set the wireEncoding field to the input.
+ * Decode the input using a particular wire format and update this Data. If wireFormat is the default wire format, also
+ * set the defaultWireEncoding field to the input.
* @param input The input byte array to be decoded.
* @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
*/
@@ -235,10 +238,10 @@
getContent() const { return content_; }
/**
- * Return a pointer to the wireEncoding. It may be null.
+ * Return a pointer to the defaultWireEncoding. It may be null.
*/
const SignedBlob&
- getWireEncoding() const { return wireEncoding_; }
+ getDefaultWireEncoding() const { return defaultWireEncoding_; }
/**
* Set the signature to a copy of the given signature.
@@ -314,7 +317,7 @@
Name name_;
MetaInfo metaInfo_;
Blob content_;
- SignedBlob wireEncoding_;
+ SignedBlob defaultWireEncoding_;
};
}
diff --git a/ndn-cpp/data.cpp b/ndn-cpp/data.cpp
index a31878e..c9f21fa 100644
--- a/ndn-cpp/data.cpp
+++ b/ndn-cpp/data.cpp
@@ -48,7 +48,7 @@
}
Data::Data(const Data& data)
-: name_(data.name_), metaInfo_(data.metaInfo_), content_(data.content_), wireEncoding_(data.wireEncoding_)
+: name_(data.name_), metaInfo_(data.metaInfo_), content_(data.content_), defaultWireEncoding_(data.defaultWireEncoding_)
{
if (data.signature_)
signature_ = data.signature_->clone();
@@ -68,7 +68,7 @@
name_ = data.name_;
metaInfo_ = data.metaInfo_;
content_ = data.content_;
- wireEncoding_ = data.wireEncoding_;
+ defaultWireEncoding_ = data.defaultWireEncoding_;
return *this;
}
@@ -102,13 +102,17 @@
}
SignedBlob
-Data::wireEncode(WireFormat& wireFormat)
+Data::wireEncode(WireFormat& wireFormat) const
{
size_t signedPortionBeginOffset, signedPortionEndOffset;
Blob encoding = wireFormat.encodeData(*this, &signedPortionBeginOffset, &signedPortionEndOffset);
+ SignedBlob wireEncoding = SignedBlob(encoding, signedPortionBeginOffset, signedPortionEndOffset);
- wireEncoding_ = SignedBlob(encoding, signedPortionBeginOffset, signedPortionEndOffset);
- return wireEncoding_;
+ if (&wireFormat == WireFormat::getDefaultWireFormat())
+ // This is the default wire encoding.
+ const_cast<Data*>(this)->defaultWireEncoding_ = wireEncoding;
+
+ return wireEncoding;
}
void
@@ -117,13 +121,18 @@
size_t signedPortionBeginOffset, signedPortionEndOffset;
wireFormat.decodeData(*this, input, inputLength, &signedPortionBeginOffset, &signedPortionEndOffset);
- wireEncoding_ = SignedBlob(input, inputLength, signedPortionBeginOffset, signedPortionEndOffset);
+ if (&wireFormat == WireFormat::getDefaultWireFormat())
+ // This is the default wire encoding.
+ defaultWireEncoding_ = SignedBlob(input, inputLength, signedPortionBeginOffset, signedPortionEndOffset);
+ else
+ defaultWireEncoding_ = SignedBlob();
}
void
Data::onChanged()
{
- wireEncoding_ = SignedBlob();
+ // The values have changed, so the default wire encoding is invalidated.
+ defaultWireEncoding_ = SignedBlob();
}
}
diff --git a/ndn-cpp/security/key-chain.cpp b/ndn-cpp/security/key-chain.cpp
index 197fdfd..3fc3f77 100644
--- a/ndn-cpp/security/key-chain.cpp
+++ b/ndn-cpp/security/key-chain.cpp
@@ -77,11 +77,10 @@
if (signature->getDigestAlgorithm().size() != 0)
// TODO: Allow a non-default digest algorithm.
throw UnrecognizedDigestAlgorithmException("Cannot verify a data packet with a non-default digest algorithm.");
- if (!data.getWireEncoding())
- // Don't expect this to happen
- throw SecurityException("The Data wireEncoding is null.");
- uint8_t signedPortionDigest[SHA256_DIGEST_LENGTH];
- ndn_digestSha256(data.getWireEncoding().signedBuf(), data.getWireEncoding().signedSize(), signedPortionDigest);
+ if (!data.getDefaultWireEncoding())
+ data.wireEncode();
+ uint8_t signedPortionDigest[SHA256_DIGEST_LENGTH];
+ ndn_digestSha256(data.getDefaultWireEncoding().signedBuf(), data.getDefaultWireEncoding().signedSize(), signedPortionDigest);
// Verify the signedPortionDigest.
// Use a temporary pointer since d2i updates it.