blob: a1b51a64ff96a9c06b51efbe54dc10424a2fa966 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson06f824a2013-07-08 17:14:30 -07004 * Derived from Interest.js by Meki Cheraoui.
Jeff Thompson47eecfc2013-07-07 22:56:46 -07005 * See COPYING for copyright and distribution information.
Jeff Thompsonb7f95562013-07-03 18:36:42 -07006 */
7
Jeff Thompson53412192013-08-06 13:35:50 -07008#include "binary-xml-encoder.h"
9#include "binary-xml-decoder.h"
10#include "binary-xml-name.h"
11#include "binary-xml-publisher-public-key-digest.h"
12#include "binary-xml-interest.h"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070013
Jeff Thompsonf0fea002013-07-30 17:22:42 -070014static ndn_Error encodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompsonf084ec62013-07-09 12:32:52 -070015{
16 if (exclude->nEntries == 0)
Jeff Thompsonadaf9232013-08-08 14:30:29 -070017 return NDN_ERROR_success;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070018
19 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070020 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Exclude)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070021 return error;
22
23 // TODO: Do we want to order the components (except for ANY)?
24 unsigned int i;
25 for (i = 0; i < exclude->nEntries; ++i) {
26 struct ndn_ExcludeEntry *entry = &exclude->entries[i];
27
28 if (entry->type == ndn_Exclude_COMPONENT) {
Jeff Thompson94ddc272013-08-08 14:17:38 -070029 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson38d0e082013-08-12 18:07:44 -070030 (encoder, ndn_BinaryXml_DTag_Component, entry->component.value, entry->component.valueLength)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070031 return error;
32 }
33 else if (entry->type == ndn_Exclude_ANY) {
Jeff Thompson94ddc272013-08-08 14:17:38 -070034 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Any)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070035 return error;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070036 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070037 return error;
38 }
39 else
40 return NDN_ERROR_unrecognized_ndn_ExcludeType;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070041 }
Jeff Thompsonf084ec62013-07-09 12:32:52 -070042
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070043 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070044 return error;
45
Jeff Thompsonadaf9232013-08-08 14:30:29 -070046 return NDN_ERROR_success;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070047}
48
Jeff Thompsonf0fea002013-07-30 17:22:42 -070049static ndn_Error decodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonf084ec62013-07-09 12:32:52 -070050{
51 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070052 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Exclude)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070053 return error;
54
55 exclude->nEntries = 0;
56 while (1) {
57 int gotExpectedTag;
58
Jeff Thompson94ddc272013-08-08 14:17:38 -070059 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Component, &gotExpectedTag)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070060 return error;
61 if (gotExpectedTag) {
62 // Component
63 unsigned char *component;
64 unsigned int componentLen;
Jeff Thompson94ddc272013-08-08 14:17:38 -070065 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Component, 0, &component, &componentLen)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070066 return error;
67
68 // Add the component entry.
69 if (exclude->nEntries >= exclude->maxEntries)
70 return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070071 ndn_ExcludeEntry_initialize(exclude->entries + exclude->nEntries, ndn_Exclude_COMPONENT, component, componentLen);
Jeff Thompsonf084ec62013-07-09 12:32:52 -070072 ++exclude->nEntries;
73
74 continue;
75 }
76
Jeff Thompson94ddc272013-08-08 14:17:38 -070077 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Any, &gotExpectedTag)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070078 return error;
79 if (gotExpectedTag) {
80 // Any
Jeff Thompson94ddc272013-08-08 14:17:38 -070081 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Any)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070082 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070083 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070084 return error;
85
86 // Add the any entry.
87 if (exclude->nEntries >= exclude->maxEntries)
88 return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070089 ndn_ExcludeEntry_initialize(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0);
Jeff Thompsonf084ec62013-07-09 12:32:52 -070090 ++exclude->nEntries;
91
92 continue;
93 }
94
Jeff Thompson94ddc272013-08-08 14:17:38 -070095 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Bloom, &gotExpectedTag)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070096 return error;
97 if (gotExpectedTag) {
98 // Skip the Bloom and treat it as Any.
99 unsigned char *value;
100 unsigned int valueLen;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700101 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Bloom, 0, &value, &valueLen)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700102 return error;
103
104 // Add the any entry.
105 if (exclude->nEntries >= exclude->maxEntries)
106 return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700107 ndn_ExcludeEntry_initialize(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0);
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700108 ++exclude->nEntries;
109
110 continue;
111 }
112
113 // Else no more entries.
114 break;
115 }
116
Jeff Thompson94ddc272013-08-08 14:17:38 -0700117 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700118 return error;
119
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700120 return NDN_ERROR_success;
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700121}
122
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700123ndn_Error ndn_encodeBinaryXmlInterest(struct ndn_Interest *interest, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson06f824a2013-07-08 17:14:30 -0700124{
125 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700126 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Interest)))
Jeff Thompson06f824a2013-07-08 17:14:30 -0700127 return error;
128
Jeff Thompson94ddc272013-08-08 14:17:38 -0700129 if ((error = ndn_encodeBinaryXmlName(&interest->name, encoder)))
Jeff Thompson06f824a2013-07-08 17:14:30 -0700130 return error;
131
Jeff Thompson94ddc272013-08-08 14:17:38 -0700132 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
133 (encoder, ndn_BinaryXml_DTag_MinSuffixComponents, interest->minSuffixComponents)))
Jeff Thompson45492a72013-07-11 12:02:47 -0700134 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700135 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
136 (encoder, ndn_BinaryXml_DTag_MaxSuffixComponents, interest->maxSuffixComponents)))
Jeff Thompson45492a72013-07-11 12:02:47 -0700137 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700138
Jeff Thompson8238d002013-07-10 11:56:49 -0700139 // This will skip encoding if there is no publisherPublicKeyDigest.
Jeff Thompson94ddc272013-08-08 14:17:38 -0700140 if ((error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&interest->publisherPublicKeyDigest, encoder)))
Jeff Thompson8238d002013-07-10 11:56:49 -0700141 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700142
Jeff Thompson8238d002013-07-10 11:56:49 -0700143 // This will skip encoding if there is no exclude.
Jeff Thompson94ddc272013-08-08 14:17:38 -0700144 if ((error = encodeExclude(&interest->exclude, encoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700145 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700146
Jeff Thompson94ddc272013-08-08 14:17:38 -0700147 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
148 (encoder, ndn_BinaryXml_DTag_ChildSelector, interest->childSelector)))
Jeff Thompson45492a72013-07-11 12:02:47 -0700149 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700150 if (interest->answerOriginKind >= 0 && interest->answerOriginKind != ndn_Interest_DEFAULT_ANSWER_ORIGIN_KIND) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700151 if ((error = ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement
152 (encoder, ndn_BinaryXml_DTag_AnswerOriginKind, (unsigned int)interest->answerOriginKind)))
Jeff Thompson06f824a2013-07-08 17:14:30 -0700153 return error;
154 }
Jeff Thompson94ddc272013-08-08 14:17:38 -0700155 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
156 (encoder, ndn_BinaryXml_DTag_Scope, interest->scope)))
Jeff Thompson45492a72013-07-11 12:02:47 -0700157 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700158
Jeff Thompson94ddc272013-08-08 14:17:38 -0700159 if ((error = ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement
160 (encoder, ndn_BinaryXml_DTag_InterestLifetime, interest->interestLifetimeMilliseconds)))
Jeff Thompsonedc22252013-07-11 18:05:44 -0700161 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700162
Jeff Thompson94ddc272013-08-08 14:17:38 -0700163 if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
164 (encoder, ndn_BinaryXml_DTag_Nonce, interest->nonce, interest->nonceLength)))
Jeff Thompsona8749a52013-07-11 11:48:05 -0700165 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700166
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700167 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompson06f824a2013-07-08 17:14:30 -0700168 return error;
169
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700170 return NDN_ERROR_success;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700171}
172
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700173ndn_Error ndn_decodeBinaryXmlInterest(struct ndn_Interest *interest, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700174{
Jeff Thompson8b666002013-07-08 01:16:26 -0700175 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700176 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Interest)))
Jeff Thompson22552902013-07-07 21:26:20 -0700177 return error;
178
Jeff Thompson94ddc272013-08-08 14:17:38 -0700179 if ((error = ndn_decodeBinaryXmlName(&interest->name, decoder)))
Jeff Thompson22552902013-07-07 21:26:20 -0700180 return error;
181
Jeff Thompson94ddc272013-08-08 14:17:38 -0700182 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
183 (decoder, ndn_BinaryXml_DTag_MinSuffixComponents, &interest->minSuffixComponents)))
Jeff Thompson22552902013-07-07 21:26:20 -0700184 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700185 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
186 (decoder, ndn_BinaryXml_DTag_MaxSuffixComponents, &interest->maxSuffixComponents)))
Jeff Thompson22552902013-07-07 21:26:20 -0700187 return error;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700188
Jeff Thompson94ddc272013-08-08 14:17:38 -0700189 if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&interest->publisherPublicKeyDigest, decoder)))
Jeff Thompson22552902013-07-07 21:26:20 -0700190 return error;
Jeff Thompson22552902013-07-07 21:26:20 -0700191
Jeff Thompson5633c302013-07-11 10:24:27 -0700192 int gotExpectedTag;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700193 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Exclude, &gotExpectedTag)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700194 return error;
195 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700196 if ((error = decodeExclude(&interest->exclude, decoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700197 return error;
198 }
199 else
200 interest->exclude.nEntries = 0;
Jeff Thompson22552902013-07-07 21:26:20 -0700201
Jeff Thompson94ddc272013-08-08 14:17:38 -0700202 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
203 (decoder, ndn_BinaryXml_DTag_ChildSelector, &interest->childSelector)))
Jeff Thompson22552902013-07-07 21:26:20 -0700204 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700205 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
206 (decoder, ndn_BinaryXml_DTag_AnswerOriginKind, &interest->answerOriginKind)))
Jeff Thompson22552902013-07-07 21:26:20 -0700207 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700208 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
209 (decoder, ndn_BinaryXml_DTag_Scope, &interest->scope)))
Jeff Thompson22552902013-07-07 21:26:20 -0700210 return error;
211
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700212 if (error= ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement
213 (decoder, ndn_BinaryXml_DTag_InterestLifetime, &interest->interestLifetimeMilliseconds))
Jeff Thompson22552902013-07-07 21:26:20 -0700214 return error;
Jeff Thompson22552902013-07-07 21:26:20 -0700215
Jeff Thompson94ddc272013-08-08 14:17:38 -0700216 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
217 (decoder, ndn_BinaryXml_DTag_Nonce, 0, &interest->nonce, &interest->nonceLength)))
Jeff Thompson22552902013-07-07 21:26:20 -0700218 return error;
Jeff Thompson22552902013-07-07 21:26:20 -0700219
Jeff Thompson94ddc272013-08-08 14:17:38 -0700220 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompson22552902013-07-07 21:26:20 -0700221 return error;
222
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700223 return NDN_ERROR_success;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700224}