blob: 270e13bb8c869d0ecaf104c3b82e936cd18ece82 [file] [log] [blame]
/**
* @author: Jeff Thompson
* Derived from Interest.js by Meki Cheraoui.
* See COPYING for copyright and distribution information.
*/
#include "BinaryXMLEncoder.h"
#include "BinaryXMLDecoder.h"
#include "BinaryXMLName.h"
#include "BinaryXMLInterest.h"
static ndn_Error encodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXMLEncoder *encoder)
{
if (exclude->nEntries == 0)
return 0;
ndn_Error error;
if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Exclude))
return error;
// TODO: Do we want to order the components (except for ANY)?
unsigned int i;
for (i = 0; i < exclude->nEntries; ++i) {
struct ndn_ExcludeEntry *entry = &exclude->entries[i];
if (entry->type == ndn_Exclude_COMPONENT) {
if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
(encoder, ndn_BinaryXML_DTag_Component, entry->component, entry->componentLength))
return error;
}
else if (entry->type == ndn_Exclude_ANY) {
if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Any))
return error;
if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
return error;
}
else
return NDN_ERROR_unrecognized_ndn_ExcludeType;
}
if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
return error;
return 0;
}
static ndn_Error decodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXMLDecoder *decoder)
{
ndn_Error error;
if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Exclude))
return error;
exclude->nEntries = 0;
while (1) {
int gotExpectedTag;
if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Component, &gotExpectedTag))
return error;
if (gotExpectedTag) {
// Component
unsigned char *component;
unsigned int componentLen;
if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(decoder, ndn_BinaryXML_DTag_Component, 0, &component, &componentLen))
return error;
// Add the component entry.
if (exclude->nEntries >= exclude->maxEntries)
return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_COMPONENT, component, componentLen);
++exclude->nEntries;
continue;
}
if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Any, &gotExpectedTag))
return error;
if (gotExpectedTag) {
// Any
if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Any))
return error;
if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
return error;
// Add the any entry.
if (exclude->nEntries >= exclude->maxEntries)
return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0);
++exclude->nEntries;
continue;
}
if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Bloom, &gotExpectedTag))
return error;
if (gotExpectedTag) {
// Skip the Bloom and treat it as Any.
unsigned char *value;
unsigned int valueLen;
if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(decoder, ndn_BinaryXML_DTag_Bloom, 0, &value, &valueLen))
return error;
// Add the any entry.
if (exclude->nEntries >= exclude->maxEntries)
return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0);
++exclude->nEntries;
continue;
}
// Else no more entries.
break;
}
if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
return error;
return 0;
}
ndn_Error ndn_encodeBinaryXMLInterest(struct ndn_Interest *interest, struct ndn_BinaryXMLEncoder *encoder)
{
ndn_Error error;
if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Interest))
return error;
if (error = ndn_encodeBinaryXMLName(&interest->name, encoder))
return error;
if (interest->minSuffixComponents >= 0) {
if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
(encoder, ndn_BinaryXML_DTag_MinSuffixComponents, (unsigned int)interest->minSuffixComponents))
return error;
}
if (interest->maxSuffixComponents >= 0) {
if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
(encoder, ndn_BinaryXML_DTag_MaxSuffixComponents, (unsigned int)interest->maxSuffixComponents))
return error;
}
if (interest->publisherPublicKeyDigest && interest->publisherPublicKeyDigestLength > 0) {
if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
(encoder, ndn_BinaryXML_DTag_PublisherPublicKeyDigest, interest->publisherPublicKeyDigest, interest->publisherPublicKeyDigestLength))
return error;
}
// This will check for no exclude.
if (error = encodeExclude(&interest->exclude, encoder))
return error;
if (interest->childSelector >= 0) {
if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
(encoder, ndn_BinaryXML_DTag_ChildSelector, (unsigned int)interest->childSelector))
return error;
}
if (interest->answerOriginKind >= 0 && interest->answerOriginKind != ndn_Interest_DEFAULT_ANSWER_ORIGIN_KIND) {
if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
(encoder, ndn_BinaryXML_DTag_AnswerOriginKind, (unsigned int)interest->answerOriginKind))
return error;
}
if (interest->scope >= 0) {
if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
(encoder, ndn_BinaryXML_DTag_Scope, (unsigned int)interest->scope))
return error;
}
if (interest->interestLifetime >= 0) {
if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_InterestLifetime))
return error;
unsigned int ticks = (unsigned int)(((double)interest->interestLifetime / 1000.0) * 4096.0);
if (error = ndn_BinaryXMLEncoder_writeUnsignedIntBigEndianBlob(encoder, ticks))
return error;
if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
return error;
}
if (interest->nonce && interest->nonceLength > 0) {
if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
(encoder, ndn_BinaryXML_DTag_Nonce, interest->nonce, interest->nonceLength))
return error;
}
if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
return error;
return 0;
}
ndn_Error ndn_decodeBinaryXMLInterest(struct ndn_Interest *interest, struct ndn_BinaryXMLDecoder *decoder)
{
ndn_Error error;
if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Interest))
return error;
if (error = ndn_decodeBinaryXMLName(&interest->name, decoder))
return error;
if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
(decoder, ndn_BinaryXML_DTag_MinSuffixComponents, &interest->minSuffixComponents))
return error;
if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
(decoder, ndn_BinaryXML_DTag_MaxSuffixComponents, &interest->maxSuffixComponents))
return error;
int gotExpectedTag;
if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_PublisherPublicKeyDigest, &gotExpectedTag))
return error;
if (gotExpectedTag) {
if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
(decoder, ndn_BinaryXML_DTag_PublisherPublicKeyDigest, 0, &interest->publisherPublicKeyDigest,
&interest->publisherPublicKeyDigestLength))
return error;
}
else {
interest->publisherPublicKeyDigest = 0;
interest->publisherPublicKeyDigestLength = 0;
}
if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Exclude, &gotExpectedTag))
return error;
if (gotExpectedTag) {
if (error = decodeExclude(&interest->exclude, decoder))
return error;
}
else
interest->exclude.nEntries = 0;
if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
(decoder, ndn_BinaryXML_DTag_ChildSelector, &interest->childSelector))
return error;
if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
(decoder, ndn_BinaryXML_DTag_AnswerOriginKind, &interest->answerOriginKind))
return error;
if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
(decoder, ndn_BinaryXML_DTag_Scope, &interest->scope))
return error;
if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_InterestLifetime, &gotExpectedTag))
return error;
if (gotExpectedTag) {
unsigned char *interestLifetime;
unsigned int interestLifetimeLength;
if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
(decoder, ndn_BinaryXML_DTag_InterestLifetime, 0, &interestLifetime, &interestLifetimeLength))
return error;
interest->interestLifetime = (int)(1000.0 *
(double)ndn_BinaryXMLDecoder_bigEndianToUnsignedInt(interestLifetime, interestLifetimeLength) / 4096.0);
}
else
interest->interestLifetime = -1;
if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Nonce, &gotExpectedTag))
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;
return 0;
}