blob: a5402225784c1fc83c3565c10b2c6a06c0e3d3ab [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
Jeff Thompson06f824a2013-07-08 17:14:30 -07003 * Derived from Interest.js by Meki Cheraoui.
Jeff Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompsonb7f95562013-07-03 18:36:42 -07005 */
6
Jeff Thompson53412192013-08-06 13:35:50 -07007#include "binary-xml-encoder.h"
8#include "binary-xml-decoder.h"
9#include "binary-xml-name.h"
10#include "binary-xml-publisher-public-key-digest.h"
11#include "binary-xml-interest.h"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070012
Jeff Thompsonf0fea002013-07-30 17:22:42 -070013static ndn_Error encodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompsonf084ec62013-07-09 12:32:52 -070014{
15 if (exclude->nEntries == 0)
Jeff Thompsonadaf9232013-08-08 14:30:29 -070016 return NDN_ERROR_success;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070017
18 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070019 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Exclude)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070020 return error;
21
22 // TODO: Do we want to order the components (except for ANY)?
23 unsigned int i;
24 for (i = 0; i < exclude->nEntries; ++i) {
25 struct ndn_ExcludeEntry *entry = &exclude->entries[i];
26
27 if (entry->type == ndn_Exclude_COMPONENT) {
Jeff Thompson94ddc272013-08-08 14:17:38 -070028 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
29 (encoder, ndn_BinaryXml_DTag_Component, entry->component, entry->componentLength)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070030 return error;
31 }
32 else if (entry->type == ndn_Exclude_ANY) {
Jeff Thompson94ddc272013-08-08 14:17:38 -070033 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Any)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070034 return error;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070035 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070036 return error;
37 }
38 else
39 return NDN_ERROR_unrecognized_ndn_ExcludeType;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070040 }
Jeff Thompsonf084ec62013-07-09 12:32:52 -070041
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070042 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070043 return error;
44
Jeff Thompsonadaf9232013-08-08 14:30:29 -070045 return NDN_ERROR_success;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070046}
47
Jeff Thompsonf0fea002013-07-30 17:22:42 -070048static ndn_Error decodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonf084ec62013-07-09 12:32:52 -070049{
50 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070051 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Exclude)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070052 return error;
53
54 exclude->nEntries = 0;
55 while (1) {
56 int gotExpectedTag;
57
Jeff Thompson94ddc272013-08-08 14:17:38 -070058 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Component, &gotExpectedTag)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070059 return error;
60 if (gotExpectedTag) {
61 // Component
62 unsigned char *component;
63 unsigned int componentLen;
Jeff Thompson94ddc272013-08-08 14:17:38 -070064 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Component, 0, &component, &componentLen)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070065 return error;
66
67 // Add the component entry.
68 if (exclude->nEntries >= exclude->maxEntries)
69 return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
70 ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_COMPONENT, component, componentLen);
71 ++exclude->nEntries;
72
73 continue;
74 }
75
Jeff Thompson94ddc272013-08-08 14:17:38 -070076 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Any, &gotExpectedTag)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070077 return error;
78 if (gotExpectedTag) {
79 // Any
Jeff Thompson94ddc272013-08-08 14:17:38 -070080 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Any)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070081 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070082 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070083 return error;
84
85 // Add the any entry.
86 if (exclude->nEntries >= exclude->maxEntries)
87 return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
88 ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0);
89 ++exclude->nEntries;
90
91 continue;
92 }
93
Jeff Thompson94ddc272013-08-08 14:17:38 -070094 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Bloom, &gotExpectedTag)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070095 return error;
96 if (gotExpectedTag) {
97 // Skip the Bloom and treat it as Any.
98 unsigned char *value;
99 unsigned int valueLen;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700100 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Bloom, 0, &value, &valueLen)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700101 return error;
102
103 // Add the any entry.
104 if (exclude->nEntries >= exclude->maxEntries)
105 return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
106 ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0);
107 ++exclude->nEntries;
108
109 continue;
110 }
111
112 // Else no more entries.
113 break;
114 }
115
Jeff Thompson94ddc272013-08-08 14:17:38 -0700116 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700117 return error;
118
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700119 return NDN_ERROR_success;
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700120}
121
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700122ndn_Error ndn_encodeBinaryXmlInterest(struct ndn_Interest *interest, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson06f824a2013-07-08 17:14:30 -0700123{
124 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700125 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Interest)))
Jeff Thompson06f824a2013-07-08 17:14:30 -0700126 return error;
127
Jeff Thompson94ddc272013-08-08 14:17:38 -0700128 if ((error = ndn_encodeBinaryXmlName(&interest->name, encoder)))
Jeff Thompson06f824a2013-07-08 17:14:30 -0700129 return error;
130
Jeff Thompson94ddc272013-08-08 14:17:38 -0700131 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
132 (encoder, ndn_BinaryXml_DTag_MinSuffixComponents, interest->minSuffixComponents)))
Jeff Thompson45492a72013-07-11 12:02:47 -0700133 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700134 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
135 (encoder, ndn_BinaryXml_DTag_MaxSuffixComponents, interest->maxSuffixComponents)))
Jeff Thompson45492a72013-07-11 12:02:47 -0700136 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700137
Jeff Thompson8238d002013-07-10 11:56:49 -0700138 // This will skip encoding if there is no publisherPublicKeyDigest.
Jeff Thompson94ddc272013-08-08 14:17:38 -0700139 if ((error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&interest->publisherPublicKeyDigest, encoder)))
Jeff Thompson8238d002013-07-10 11:56:49 -0700140 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700141
Jeff Thompson8238d002013-07-10 11:56:49 -0700142 // This will skip encoding if there is no exclude.
Jeff Thompson94ddc272013-08-08 14:17:38 -0700143 if ((error = encodeExclude(&interest->exclude, encoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700144 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700145
Jeff Thompson94ddc272013-08-08 14:17:38 -0700146 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
147 (encoder, ndn_BinaryXml_DTag_ChildSelector, interest->childSelector)))
Jeff Thompson45492a72013-07-11 12:02:47 -0700148 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700149 if (interest->answerOriginKind >= 0 && interest->answerOriginKind != ndn_Interest_DEFAULT_ANSWER_ORIGIN_KIND) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700150 if ((error = ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement
151 (encoder, ndn_BinaryXml_DTag_AnswerOriginKind, (unsigned int)interest->answerOriginKind)))
Jeff Thompson06f824a2013-07-08 17:14:30 -0700152 return error;
153 }
Jeff Thompson94ddc272013-08-08 14:17:38 -0700154 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
155 (encoder, ndn_BinaryXml_DTag_Scope, interest->scope)))
Jeff Thompson45492a72013-07-11 12:02:47 -0700156 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700157
Jeff Thompson94ddc272013-08-08 14:17:38 -0700158 if ((error = ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement
159 (encoder, ndn_BinaryXml_DTag_InterestLifetime, interest->interestLifetimeMilliseconds)))
Jeff Thompsonedc22252013-07-11 18:05:44 -0700160 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700161
Jeff Thompson94ddc272013-08-08 14:17:38 -0700162 if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
163 (encoder, ndn_BinaryXml_DTag_Nonce, interest->nonce, interest->nonceLength)))
Jeff Thompsona8749a52013-07-11 11:48:05 -0700164 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700165
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700166 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompson06f824a2013-07-08 17:14:30 -0700167 return error;
168
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700169 return NDN_ERROR_success;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700170}
171
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700172ndn_Error ndn_decodeBinaryXmlInterest(struct ndn_Interest *interest, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700173{
Jeff Thompson8b666002013-07-08 01:16:26 -0700174 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700175 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Interest)))
Jeff Thompson22552902013-07-07 21:26:20 -0700176 return error;
177
Jeff Thompson94ddc272013-08-08 14:17:38 -0700178 if ((error = ndn_decodeBinaryXmlName(&interest->name, decoder)))
Jeff Thompson22552902013-07-07 21:26:20 -0700179 return error;
180
Jeff Thompson94ddc272013-08-08 14:17:38 -0700181 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
182 (decoder, ndn_BinaryXml_DTag_MinSuffixComponents, &interest->minSuffixComponents)))
Jeff Thompson22552902013-07-07 21:26:20 -0700183 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700184 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
185 (decoder, ndn_BinaryXml_DTag_MaxSuffixComponents, &interest->maxSuffixComponents)))
Jeff Thompson22552902013-07-07 21:26:20 -0700186 return error;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700187
Jeff Thompson94ddc272013-08-08 14:17:38 -0700188 if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&interest->publisherPublicKeyDigest, decoder)))
Jeff Thompson22552902013-07-07 21:26:20 -0700189 return error;
Jeff Thompson22552902013-07-07 21:26:20 -0700190
Jeff Thompson5633c302013-07-11 10:24:27 -0700191 int gotExpectedTag;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700192 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Exclude, &gotExpectedTag)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700193 return error;
194 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700195 if ((error = decodeExclude(&interest->exclude, decoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700196 return error;
197 }
198 else
199 interest->exclude.nEntries = 0;
Jeff Thompson22552902013-07-07 21:26:20 -0700200
Jeff Thompson94ddc272013-08-08 14:17:38 -0700201 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
202 (decoder, ndn_BinaryXml_DTag_ChildSelector, &interest->childSelector)))
Jeff Thompson22552902013-07-07 21:26:20 -0700203 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700204 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
205 (decoder, ndn_BinaryXml_DTag_AnswerOriginKind, &interest->answerOriginKind)))
Jeff Thompson22552902013-07-07 21:26:20 -0700206 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700207 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
208 (decoder, ndn_BinaryXml_DTag_Scope, &interest->scope)))
Jeff Thompson22552902013-07-07 21:26:20 -0700209 return error;
210
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700211 if (error= ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement
212 (decoder, ndn_BinaryXml_DTag_InterestLifetime, &interest->interestLifetimeMilliseconds))
Jeff Thompson22552902013-07-07 21:26:20 -0700213 return error;
Jeff Thompson22552902013-07-07 21:26:20 -0700214
Jeff Thompson94ddc272013-08-08 14:17:38 -0700215 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
216 (decoder, ndn_BinaryXml_DTag_Nonce, 0, &interest->nonce, &interest->nonceLength)))
Jeff Thompson22552902013-07-07 21:26:20 -0700217 return error;
Jeff Thompson22552902013-07-07 21:26:20 -0700218
Jeff Thompson94ddc272013-08-08 14:17:38 -0700219 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompson22552902013-07-07 21:26:20 -0700220 return error;
221
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700222 return NDN_ERROR_success;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700223}