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 | 06f824a | 2013-07-08 17:14:30 -0700 | [diff] [blame] | 12 | ndn_Error ndn_encodeBinaryXMLInterest(struct ndn_Interest *interest, struct ndn_BinaryXMLEncoder *encoder) |
| 13 | { |
| 14 | ndn_Error error; |
| 15 | if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Interest)) |
| 16 | return error; |
| 17 | |
| 18 | if (error = ndn_encodeBinaryXMLName(&interest->name, encoder)) |
| 19 | return error; |
| 20 | |
| 21 | if (interest->minSuffixComponents >= 0) { |
| 22 | if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement |
| 23 | (encoder, ndn_BinaryXML_DTag_MinSuffixComponents, (unsigned int)interest->minSuffixComponents)) |
| 24 | return error; |
| 25 | } |
| 26 | if (interest->maxSuffixComponents >= 0) { |
| 27 | if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement |
| 28 | (encoder, ndn_BinaryXML_DTag_MaxSuffixComponents, (unsigned int)interest->maxSuffixComponents)) |
| 29 | return error; |
| 30 | } |
| 31 | |
| 32 | if (interest->publisherPublicKeyDigest && interest->publisherPublicKeyDigestLength > 0) { |
| 33 | if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement |
| 34 | (encoder, ndn_BinaryXML_DTag_PublisherPublicKeyDigest, interest->publisherPublicKeyDigest, interest->publisherPublicKeyDigestLength)) |
| 35 | return error; |
| 36 | } |
| 37 | |
| 38 | // TODO: Implement exclude |
| 39 | |
| 40 | if (interest->childSelector >= 0) { |
| 41 | if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement |
| 42 | (encoder, ndn_BinaryXML_DTag_ChildSelector, (unsigned int)interest->childSelector)) |
| 43 | return error; |
| 44 | } |
| 45 | if (interest->answerOriginKind >= 0 && interest->answerOriginKind != ndn_Interest_DEFAULT_ANSWER_ORIGIN_KIND) { |
| 46 | if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement |
| 47 | (encoder, ndn_BinaryXML_DTag_AnswerOriginKind, (unsigned int)interest->answerOriginKind)) |
| 48 | return error; |
| 49 | } |
| 50 | if (interest->scope >= 0) { |
| 51 | if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement |
| 52 | (encoder, ndn_BinaryXML_DTag_Scope, (unsigned int)interest->scope)) |
| 53 | return error; |
| 54 | } |
| 55 | |
| 56 | if (interest->interestLifetime >= 0) { |
| 57 | if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_InterestLifetime)) |
| 58 | return error; |
| 59 | |
| 60 | unsigned int ticks = (unsigned int)(((double)interest->interestLifetime / 1000.0) * 4096.0); |
| 61 | if (error = ndn_BinaryXMLEncoder_writeUnsignedIntBigEndianBlob(encoder, ticks)) |
| 62 | return error; |
| 63 | |
| 64 | if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder)) |
| 65 | return error; |
| 66 | } |
| 67 | |
| 68 | if (interest->nonce && interest->nonceLength > 0) { |
| 69 | if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement |
| 70 | (encoder, ndn_BinaryXML_DTag_Nonce, interest->nonce, interest->nonceLength)) |
| 71 | return error; |
| 72 | } |
| 73 | |
| 74 | if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder)) |
| 75 | return error; |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 80 | ndn_Error ndn_decodeBinaryXMLInterest(struct ndn_Interest *interest, struct ndn_BinaryXMLDecoder *decoder) |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 81 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 82 | ndn_Error error; |
Jeff Thompson | 2255290 | 2013-07-07 21:26:20 -0700 | [diff] [blame] | 83 | if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Interest)) |
| 84 | return error; |
| 85 | |
| 86 | if (error = ndn_decodeBinaryXMLName(&interest->name, decoder)) |
| 87 | return error; |
| 88 | |
| 89 | if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement |
| 90 | (decoder, ndn_BinaryXML_DTag_MinSuffixComponents, &interest->minSuffixComponents)) |
| 91 | return error; |
| 92 | if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement |
| 93 | (decoder, ndn_BinaryXML_DTag_MaxSuffixComponents, &interest->maxSuffixComponents)) |
| 94 | return error; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 95 | |
Jeff Thompson | 2255290 | 2013-07-07 21:26:20 -0700 | [diff] [blame] | 96 | int gotExpectedTag; |
| 97 | if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_PublisherPublicKeyDigest, &gotExpectedTag)) |
| 98 | return error; |
| 99 | if (gotExpectedTag) { |
| 100 | if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement |
| 101 | (decoder, ndn_BinaryXML_DTag_PublisherPublicKeyDigest, 0, &interest->publisherPublicKeyDigest, |
| 102 | &interest->publisherPublicKeyDigestLength)) |
| 103 | return error; |
| 104 | } |
| 105 | else { |
| 106 | interest->publisherPublicKeyDigest = 0; |
| 107 | interest->publisherPublicKeyDigestLength = 0; |
| 108 | } |
| 109 | |
| 110 | // TODO: Implement exclude |
| 111 | |
| 112 | if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement |
| 113 | (decoder, ndn_BinaryXML_DTag_ChildSelector, &interest->childSelector)) |
| 114 | return error; |
| 115 | if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement |
| 116 | (decoder, ndn_BinaryXML_DTag_AnswerOriginKind, &interest->answerOriginKind)) |
| 117 | return error; |
| 118 | if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement |
| 119 | (decoder, ndn_BinaryXML_DTag_Scope, &interest->scope)) |
| 120 | return error; |
| 121 | |
| 122 | if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_InterestLifetime, &gotExpectedTag)) |
| 123 | return error; |
| 124 | if (gotExpectedTag) { |
| 125 | unsigned char *interestLifetime; |
| 126 | unsigned int interestLifetimeLength; |
| 127 | if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement |
| 128 | (decoder, ndn_BinaryXML_DTag_InterestLifetime, 0, &interestLifetime, &interestLifetimeLength)) |
| 129 | return error; |
| 130 | |
Jeff Thompson | 06f824a | 2013-07-08 17:14:30 -0700 | [diff] [blame] | 131 | interest->interestLifetime = (int)(1000.0 * |
| 132 | (double)ndn_BinaryXMLDecoder_bigEndianToUnsignedInt(interestLifetime, interestLifetimeLength) / 4096.0); |
Jeff Thompson | 2255290 | 2013-07-07 21:26:20 -0700 | [diff] [blame] | 133 | } |
| 134 | else |
| 135 | interest->interestLifetime = -1; |
| 136 | |
| 137 | if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Nonce, &gotExpectedTag)) |
| 138 | return error; |
| 139 | if (gotExpectedTag) { |
| 140 | if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement |
| 141 | (decoder, ndn_BinaryXML_DTag_Nonce, 0, &interest->nonce, &interest->nonceLength)) |
| 142 | return error; |
| 143 | } |
| 144 | else { |
| 145 | interest->nonce = 0; |
| 146 | interest->nonceLength = 0; |
| 147 | } |
| 148 | |
| 149 | if (error = ndn_BinaryXMLDecoder_readElementClose(decoder)) |
| 150 | return error; |
| 151 | |
| 152 | return 0; |
Jeff Thompson | 06f824a | 2013-07-08 17:14:30 -0700 | [diff] [blame] | 153 | } |