Added readOptionalBinaryDTagElement
diff --git a/ndn-cpp/c/encoding/BinaryXMLDecoder.c b/ndn-cpp/c/encoding/BinaryXMLDecoder.c
index 60040ae..2e65a85 100644
--- a/ndn-cpp/c/encoding/BinaryXMLDecoder.c
+++ b/ndn-cpp/c/encoding/BinaryXMLDecoder.c
@@ -139,7 +139,7 @@
}
ndn_Error ndn_BinaryXMLDecoder_readBinaryDTagElement
- (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLen)
+ (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLength)
{
ndn_Error error;
if (error = ndn_BinaryXMLDecoder_readElementStartDTag(self, expectedTag))
@@ -153,17 +153,17 @@
// The binary item is missing, and this is allowed, so read the element close and return a null value.
++self->offset;
*value = 0;
- *valueLen = 0;
+ *valueLength = 0;
return 0;
}
}
unsigned int itemType;
- if (error = ndn_BinaryXMLDecoder_decodeTypeAndValue(self, &itemType, valueLen))
+ if (error = ndn_BinaryXMLDecoder_decodeTypeAndValue(self, &itemType, valueLength))
return error;
// Ignore itemType.
*value = self->input + self->offset;
- self->offset += *valueLen;
+ self->offset += *valueLength;
if (error = ndn_BinaryXMLDecoder_readElementClose(self))
return error;
@@ -171,20 +171,39 @@
return 0;
}
+ndn_Error ndn_BinaryXMLDecoder_readOptionalBinaryDTagElement
+ (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLength)
+{
+ ndn_Error error;
+ int gotExpectedTag;
+ if (error = ndn_BinaryXMLDecoder_peekDTag(self, expectedTag, &gotExpectedTag))
+ return error;
+ if (gotExpectedTag) {
+ if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(self, expectedTag, allowNull, value, valueLength))
+ return error;
+ }
+ else {
+ *value = 0;
+ *valueLength = 0;
+ }
+
+ return 0;
+}
+
ndn_Error ndn_BinaryXMLDecoder_readUDataDTagElement
- (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLen)
+ (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLength)
{
ndn_Error error;
if (error = ndn_BinaryXMLDecoder_readElementStartDTag(self, expectedTag))
return error;
unsigned int itemType;
- if (error = ndn_BinaryXMLDecoder_decodeTypeAndValue(self, &itemType, valueLen))
+ if (error = ndn_BinaryXMLDecoder_decodeTypeAndValue(self, &itemType, valueLength))
return error;
if (itemType != ndn_BinaryXML_UDATA)
return NDN_ERROR_item_is_not_UDATA;
*value = self->input + self->offset;
- self->offset += *valueLen;
+ self->offset += *valueLength;
if (error = ndn_BinaryXMLDecoder_readElementClose(self))
return error;
diff --git a/ndn-cpp/c/encoding/BinaryXMLDecoder.h b/ndn-cpp/c/encoding/BinaryXMLDecoder.h
index 0c1427e..2686e81 100644
--- a/ndn-cpp/c/encoding/BinaryXMLDecoder.h
+++ b/ndn-cpp/c/encoding/BinaryXMLDecoder.h
@@ -70,13 +70,28 @@
* @param allowNull 1 if the binary item may be missing
* @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the
* binary data item is absent, then return 0.
- * @param valueLen output the length of the binary data. However, if allowNull is 1 and the
+ * @param valueLength output the length of the binary data. However, if allowNull is 1 and the
* binary data item is absent, then return 0.
* @return 0 for success, else an error code, including an error if not the expected tag, or if allowNull is 0
* and the binary data is absent
*/
ndn_Error ndn_BinaryXMLDecoder_readBinaryDTagElement
- (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLen);
+ (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLength);
+
+/**
+ * Peek at the next element and if it is the expectedTag, call ndn_BinaryXMLDecoder_readBinaryDTagElement.
+ * Otherwise, set value and valueLength to 0.
+ * @param self pointer to the ndn_BinaryXMLDecoder struct
+ * @param expectedTag the expected value for DTAG
+ * @param allowNull 1 if the binary item may be missing
+ * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the
+ * binary data item is absent, then return 0.
+ * @param valueLength output the length of the binary data. However, if allowNull is 1 and the
+ * binary data item is absent, then return 0.
+ * @return 0 for success, else an error code, including if allowNull is 0 and the binary data is absent
+ */
+ndn_Error ndn_BinaryXMLDecoder_readOptionalBinaryDTagElement
+ (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLength);
/**
* Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
@@ -85,11 +100,11 @@
* @param self pointer to the ndn_BinaryXMLDecoder struct
* @param expectedTag the expected value for DTAG
* @param value output a pointer to the binary data inside self's input buffer.
- * @param valueLen output the length of the binary data.
+ * @param valueLength output the length of the binary data.
* @return 0 for success, else an error code, including an error if not the expected tag, or if the item is not UDATA.
*/
ndn_Error ndn_BinaryXMLDecoder_readUDataDTagElement
- (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLen);
+ (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLength);
/**
* Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
diff --git a/ndn-cpp/c/encoding/BinaryXMLInterest.c b/ndn-cpp/c/encoding/BinaryXMLInterest.c
index a6e4ea8..6b02176 100644
--- a/ndn-cpp/c/encoding/BinaryXMLInterest.c
+++ b/ndn-cpp/c/encoding/BinaryXMLInterest.c
@@ -241,17 +241,9 @@
else
interest->interestLifetime = -1;
- if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Nonce, &gotExpectedTag))
+ if (error = ndn_BinaryXMLDecoder_readOptionalBinaryDTagElement
+ (decoder, ndn_BinaryXML_DTag_Nonce, 0, &interest->nonce, &interest->nonceLength))
return error;
- if (gotExpectedTag) {
- if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
- (decoder, ndn_BinaryXML_DTag_Nonce, 0, &interest->nonce, &interest->nonceLength))
- return error;
- }
- else {
- interest->nonce = 0;
- interest->nonceLength = 0;
- }
if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
return error;