Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
Jeff Thompson | 06f824a | 2013-07-08 17:14:30 -0700 | [diff] [blame] | 3 | * Derived from Interest.js by Meki Cheraoui. |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include "BinaryXMLEncoder.h" |
| 8 | #include "BinaryXMLDecoder.h" |
Jeff Thompson | 2255290 | 2013-07-07 21:26:20 -0700 | [diff] [blame] | 9 | #include "BinaryXMLName.h" |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 10 | #include "BinaryXMLInterest.h" |
| 11 | |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame^] | 12 | static ndn_Error encodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXMLEncoder *encoder) |
| 13 | { |
| 14 | if (exclude->nEntries == 0) |
| 15 | return; |
| 16 | |
| 17 | ndn_Error error; |
| 18 | if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Exclude)) |
| 19 | return error; |
| 20 | |
| 21 | // TODO: Do we want to order the components (except for ANY)? |
| 22 | unsigned int i; |
| 23 | for (i = 0; i < exclude->nEntries; ++i) { |
| 24 | struct ndn_ExcludeEntry *entry = &exclude->entries[i]; |
| 25 | |
| 26 | if (entry->type == ndn_Exclude_COMPONENT) { |
| 27 | if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement |
| 28 | (encoder, ndn_BinaryXML_DTag_Component, entry->component, entry->componentLength)) |
| 29 | return error; |
| 30 | } |
| 31 | else if (entry->type == ndn_Exclude_ANY) { |
| 32 | if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Any)) |
| 33 | return error; |
| 34 | if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder)) |
| 35 | return error; |
| 36 | } |
| 37 | else |
| 38 | return NDN_ERROR_unrecognized_ndn_ExcludeType; |
| 39 | } |
| 40 | |
| 41 | if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder)) |
| 42 | return error; |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static ndn_Error decodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXMLDecoder *decoder) |
| 48 | { |
| 49 | ndn_Error error; |
| 50 | if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Exclude)) |
| 51 | return error; |
| 52 | |
| 53 | exclude->nEntries = 0; |
| 54 | while (1) { |
| 55 | int gotExpectedTag; |
| 56 | |
| 57 | if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Component, &gotExpectedTag)) |
| 58 | return error; |
| 59 | if (gotExpectedTag) { |
| 60 | // Component |
| 61 | unsigned char *component; |
| 62 | unsigned int componentLen; |
| 63 | if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(decoder, ndn_BinaryXML_DTag_Component, 0, &component, &componentLen)) |
| 64 | return error; |
| 65 | |
| 66 | // Add the component entry. |
| 67 | if (exclude->nEntries >= exclude->maxEntries) |
| 68 | return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude; |
| 69 | ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_COMPONENT, component, componentLen); |
| 70 | ++exclude->nEntries; |
| 71 | |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Any, &gotExpectedTag)) |
| 76 | return error; |
| 77 | if (gotExpectedTag) { |
| 78 | // Any |
| 79 | if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Any)) |
| 80 | return error; |
| 81 | if (error = ndn_BinaryXMLDecoder_readElementClose(decoder)) |
| 82 | return error; |
| 83 | |
| 84 | // Add the any entry. |
| 85 | if (exclude->nEntries >= exclude->maxEntries) |
| 86 | return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude; |
| 87 | ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0); |
| 88 | ++exclude->nEntries; |
| 89 | |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Bloom, &gotExpectedTag)) |
| 94 | return error; |
| 95 | if (gotExpectedTag) { |
| 96 | // Skip the Bloom and treat it as Any. |
| 97 | unsigned char *value; |
| 98 | unsigned int valueLen; |
| 99 | if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(decoder, ndn_BinaryXML_DTag_Bloom, 0, &value, &valueLen)) |
| 100 | return error; |
| 101 | |
| 102 | // Add the any entry. |
| 103 | if (exclude->nEntries >= exclude->maxEntries) |
| 104 | return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude; |
| 105 | ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0); |
| 106 | ++exclude->nEntries; |
| 107 | |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | // Else no more entries. |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | if (error = ndn_BinaryXMLDecoder_readElementClose(decoder)) |
| 116 | return error; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Jeff Thompson | 06f824a | 2013-07-08 17:14:30 -0700 | [diff] [blame] | 121 | ndn_Error ndn_encodeBinaryXMLInterest(struct ndn_Interest *interest, struct ndn_BinaryXMLEncoder *encoder) |
| 122 | { |
| 123 | ndn_Error error; |
| 124 | if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Interest)) |
| 125 | return error; |
| 126 | |
| 127 | if (error = ndn_encodeBinaryXMLName(&interest->name, encoder)) |
| 128 | return error; |
| 129 | |
| 130 | if (interest->minSuffixComponents >= 0) { |
| 131 | if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement |
| 132 | (encoder, ndn_BinaryXML_DTag_MinSuffixComponents, (unsigned int)interest->minSuffixComponents)) |
| 133 | return error; |
| 134 | } |
| 135 | if (interest->maxSuffixComponents >= 0) { |
| 136 | if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement |
| 137 | (encoder, ndn_BinaryXML_DTag_MaxSuffixComponents, (unsigned int)interest->maxSuffixComponents)) |
| 138 | return error; |
| 139 | } |
| 140 | |
| 141 | if (interest->publisherPublicKeyDigest && interest->publisherPublicKeyDigestLength > 0) { |
| 142 | if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement |
| 143 | (encoder, ndn_BinaryXML_DTag_PublisherPublicKeyDigest, interest->publisherPublicKeyDigest, interest->publisherPublicKeyDigestLength)) |
| 144 | return error; |
| 145 | } |
| 146 | |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame^] | 147 | // This will check for no exclude. |
| 148 | if (error = encodeExclude(&interest->exclude, encoder)) |
| 149 | return error; |
Jeff Thompson | 06f824a | 2013-07-08 17:14:30 -0700 | [diff] [blame] | 150 | |
| 151 | if (interest->childSelector >= 0) { |
| 152 | if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement |
| 153 | (encoder, ndn_BinaryXML_DTag_ChildSelector, (unsigned int)interest->childSelector)) |
| 154 | return error; |
| 155 | } |
| 156 | if (interest->answerOriginKind >= 0 && interest->answerOriginKind != ndn_Interest_DEFAULT_ANSWER_ORIGIN_KIND) { |
| 157 | if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement |
| 158 | (encoder, ndn_BinaryXML_DTag_AnswerOriginKind, (unsigned int)interest->answerOriginKind)) |
| 159 | return error; |
| 160 | } |
| 161 | if (interest->scope >= 0) { |
| 162 | if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement |
| 163 | (encoder, ndn_BinaryXML_DTag_Scope, (unsigned int)interest->scope)) |
| 164 | return error; |
| 165 | } |
| 166 | |
| 167 | if (interest->interestLifetime >= 0) { |
| 168 | if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_InterestLifetime)) |
| 169 | return error; |
| 170 | |
| 171 | unsigned int ticks = (unsigned int)(((double)interest->interestLifetime / 1000.0) * 4096.0); |
| 172 | if (error = ndn_BinaryXMLEncoder_writeUnsignedIntBigEndianBlob(encoder, ticks)) |
| 173 | return error; |
| 174 | |
| 175 | if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder)) |
| 176 | return error; |
| 177 | } |
| 178 | |
| 179 | if (interest->nonce && interest->nonceLength > 0) { |
| 180 | if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement |
| 181 | (encoder, ndn_BinaryXML_DTag_Nonce, interest->nonce, interest->nonceLength)) |
| 182 | return error; |
| 183 | } |
| 184 | |
| 185 | if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder)) |
| 186 | return error; |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 191 | ndn_Error ndn_decodeBinaryXMLInterest(struct ndn_Interest *interest, struct ndn_BinaryXMLDecoder *decoder) |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 192 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 193 | ndn_Error error; |
Jeff Thompson | 2255290 | 2013-07-07 21:26:20 -0700 | [diff] [blame] | 194 | if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Interest)) |
| 195 | return error; |
| 196 | |
| 197 | if (error = ndn_decodeBinaryXMLName(&interest->name, decoder)) |
| 198 | return error; |
| 199 | |
| 200 | if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement |
| 201 | (decoder, ndn_BinaryXML_DTag_MinSuffixComponents, &interest->minSuffixComponents)) |
| 202 | return error; |
| 203 | if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement |
| 204 | (decoder, ndn_BinaryXML_DTag_MaxSuffixComponents, &interest->maxSuffixComponents)) |
| 205 | return error; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 206 | |
Jeff Thompson | 2255290 | 2013-07-07 21:26:20 -0700 | [diff] [blame] | 207 | int gotExpectedTag; |
| 208 | if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_PublisherPublicKeyDigest, &gotExpectedTag)) |
| 209 | return error; |
| 210 | if (gotExpectedTag) { |
| 211 | if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement |
| 212 | (decoder, ndn_BinaryXML_DTag_PublisherPublicKeyDigest, 0, &interest->publisherPublicKeyDigest, |
| 213 | &interest->publisherPublicKeyDigestLength)) |
| 214 | return error; |
| 215 | } |
| 216 | else { |
| 217 | interest->publisherPublicKeyDigest = 0; |
| 218 | interest->publisherPublicKeyDigestLength = 0; |
| 219 | } |
| 220 | |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame^] | 221 | if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Exclude, &gotExpectedTag)) |
| 222 | return error; |
| 223 | if (gotExpectedTag) { |
| 224 | if (error = decodeExclude(&interest->exclude, decoder)) |
| 225 | return error; |
| 226 | } |
| 227 | else |
| 228 | interest->exclude.nEntries = 0; |
Jeff Thompson | 2255290 | 2013-07-07 21:26:20 -0700 | [diff] [blame] | 229 | |
| 230 | if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement |
| 231 | (decoder, ndn_BinaryXML_DTag_ChildSelector, &interest->childSelector)) |
| 232 | return error; |
| 233 | if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement |
| 234 | (decoder, ndn_BinaryXML_DTag_AnswerOriginKind, &interest->answerOriginKind)) |
| 235 | return error; |
| 236 | if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement |
| 237 | (decoder, ndn_BinaryXML_DTag_Scope, &interest->scope)) |
| 238 | return error; |
| 239 | |
| 240 | if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_InterestLifetime, &gotExpectedTag)) |
| 241 | return error; |
| 242 | if (gotExpectedTag) { |
| 243 | unsigned char *interestLifetime; |
| 244 | unsigned int interestLifetimeLength; |
| 245 | if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement |
| 246 | (decoder, ndn_BinaryXML_DTag_InterestLifetime, 0, &interestLifetime, &interestLifetimeLength)) |
| 247 | return error; |
| 248 | |
Jeff Thompson | 06f824a | 2013-07-08 17:14:30 -0700 | [diff] [blame] | 249 | interest->interestLifetime = (int)(1000.0 * |
| 250 | (double)ndn_BinaryXMLDecoder_bigEndianToUnsignedInt(interestLifetime, interestLifetimeLength) / 4096.0); |
Jeff Thompson | 2255290 | 2013-07-07 21:26:20 -0700 | [diff] [blame] | 251 | } |
| 252 | else |
| 253 | interest->interestLifetime = -1; |
| 254 | |
| 255 | if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Nonce, &gotExpectedTag)) |
| 256 | return error; |
| 257 | if (gotExpectedTag) { |
| 258 | if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement |
| 259 | (decoder, ndn_BinaryXML_DTag_Nonce, 0, &interest->nonce, &interest->nonceLength)) |
| 260 | return error; |
| 261 | } |
| 262 | else { |
| 263 | interest->nonce = 0; |
| 264 | interest->nonceLength = 0; |
| 265 | } |
| 266 | |
| 267 | if (error = ndn_BinaryXMLDecoder_readElementClose(decoder)) |
| 268 | return error; |
| 269 | |
| 270 | return 0; |
Jeff Thompson | 06f824a | 2013-07-08 17:14:30 -0700 | [diff] [blame] | 271 | } |