Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * Derived from ContentObject.js by Meki Cheraoui. |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 7 | #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" |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 11 | #include "binary-xml-data.h" |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 12 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 13 | static ndn_Error encodeSignature(struct ndn_Signature *signature, struct ndn_BinaryXmlEncoder *encoder) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 14 | { |
| 15 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 16 | if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Signature))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 17 | return error; |
| 18 | |
| 19 | // TODO: Check if digestAlgorithm is the same as the default, and skip it, otherwise encode it as UDATA. |
| 20 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 21 | if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement |
| 22 | (encoder, ndn_BinaryXml_DTag_Witness, signature->witness, signature->witnessLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 23 | return error; |
| 24 | |
| 25 | // Require a signature. |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 26 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
| 27 | (encoder, ndn_BinaryXml_DTag_SignatureBits, signature->signature, signature->signatureLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 28 | return error; |
| 29 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 30 | if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 31 | return error; |
| 32 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 33 | return NDN_ERROR_success; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 36 | static ndn_Error decodeSignature(struct ndn_Signature *signature, struct ndn_BinaryXmlDecoder *decoder) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 37 | { |
| 38 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 39 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Signature))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 40 | return error; |
| 41 | |
| 42 | /* TODO: digestAlgorithm as UDATA */ signature->digestAlgorithm = 0; signature->digestAlgorithmLength = 0; |
| 43 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 44 | if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement |
| 45 | (decoder, ndn_BinaryXml_DTag_Witness, 0, &signature->witness, &signature->witnessLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 46 | return error; |
| 47 | |
| 48 | // Require a signature. |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 49 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement |
| 50 | (decoder, ndn_BinaryXml_DTag_SignatureBits, 0, &signature->signature, &signature->signatureLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 51 | return error; |
| 52 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 53 | if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 54 | return error; |
| 55 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 56 | return NDN_ERROR_success; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 59 | static ndn_Error encodeSignedInfo(struct ndn_MetaInfo *metaInfo, struct ndn_BinaryXmlEncoder *encoder) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 60 | { |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 61 | if (metaInfo->type < 0) |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 62 | return NDN_ERROR_success; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 63 | |
| 64 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 65 | if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_SignedInfo))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 66 | return error; |
| 67 | |
| 68 | // This will skip encoding if there is no publisherPublicKeyDigest. |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 69 | if ((error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&metaInfo->publisherPublicKeyDigest, encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 70 | return error; |
| 71 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 72 | if ((error = ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 73 | (encoder, ndn_BinaryXml_DTag_Timestamp, metaInfo->timestampMilliseconds))) |
Jeff Thompson | 2bcece3 | 2013-07-11 18:10:19 -0700 | [diff] [blame] | 74 | return error; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 75 | |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 76 | if (!(metaInfo->type < 0 || metaInfo->type == ndn_ContentType_DATA)) { |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 77 | // Not the default of DATA, so we need to encode the type. |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 78 | unsigned char *typeBytes; |
| 79 | unsigned int typeBytesLength = 3; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 80 | if (metaInfo->type == ndn_ContentType_ENCR) |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 81 | typeBytes = "\x10\xD0\x91"; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 82 | else if (metaInfo->type == ndn_ContentType_GONE) |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 83 | typeBytes = "\x18\xE3\x44"; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 84 | else if (metaInfo->type == ndn_ContentType_KEY) |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 85 | typeBytes = "\x28\x46\x3F"; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 86 | else if (metaInfo->type == ndn_ContentType_LINK) |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 87 | typeBytes = "\x2C\x83\x4A"; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 88 | else if (metaInfo->type == ndn_ContentType_NACK) |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 89 | typeBytes = "\x34\x00\x8A"; |
| 90 | else |
| 91 | return NDN_ERROR_unrecognized_ndn_ContentType; |
| 92 | |
| 93 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
| 94 | (encoder, ndn_BinaryXml_DTag_Type, typeBytes, typeBytesLength))) |
| 95 | return error; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 98 | if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 99 | (encoder, ndn_BinaryXml_DTag_FreshnessSeconds, metaInfo->freshnessSeconds))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 100 | return error; |
| 101 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 102 | if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 103 | (encoder, ndn_BinaryXml_DTag_FinalBlockID, metaInfo->finalBlockID, metaInfo->finalBlockIDLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 104 | return error; |
| 105 | |
| 106 | // This will skip encoding if there is no key locator. |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 107 | if ((error = ndn_encodeBinaryXmlKeyLocator(&metaInfo->keyLocator, encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 108 | return error; |
| 109 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 110 | if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 111 | return error; |
| 112 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 113 | return NDN_ERROR_success; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 116 | static ndn_Error decodeSignedInfo(struct ndn_MetaInfo *metaInfo, struct ndn_BinaryXmlDecoder *decoder) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 117 | { |
| 118 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 119 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_SignedInfo))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 120 | return error; |
| 121 | |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 122 | if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&metaInfo->publisherPublicKeyDigest, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 123 | return error; |
| 124 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 125 | if (error= ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 126 | (decoder, ndn_BinaryXml_DTag_Timestamp, &metaInfo->timestampMilliseconds)) |
Jeff Thompson | 210b92f | 2013-07-11 15:16:03 -0700 | [diff] [blame] | 127 | return error; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 128 | |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 129 | unsigned char *typeBytes; |
| 130 | unsigned int typeBytesLength; |
| 131 | if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement |
| 132 | (decoder, ndn_BinaryXml_DTag_Type, 0, &typeBytes, &typeBytesLength))) |
| 133 | return error; |
| 134 | if (typeBytesLength == 0) |
| 135 | // The default Type is DATA. |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 136 | metaInfo->type = ndn_ContentType_DATA; |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 137 | else if (typeBytesLength == 3) { |
| 138 | // All the recognized content types are 3 bytes. |
| 139 | if (ndn_memcmp(typeBytes, "\x0C\x04\xC0", typeBytesLength) == 0) |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 140 | metaInfo->type = ndn_ContentType_DATA; |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 141 | else if (ndn_memcmp(typeBytes, "\x10\xD0\x91", typeBytesLength) == 0) |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 142 | metaInfo->type = ndn_ContentType_ENCR; |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 143 | else if (ndn_memcmp(typeBytes, "\x18\xE3\x44", typeBytesLength) == 0) |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 144 | metaInfo->type = ndn_ContentType_GONE; |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 145 | else if (ndn_memcmp(typeBytes, "\x28\x46\x3F", typeBytesLength) == 0) |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 146 | metaInfo->type = ndn_ContentType_KEY; |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 147 | else if (ndn_memcmp(typeBytes, "\x2C\x83\x4A", typeBytesLength) == 0) |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 148 | metaInfo->type = ndn_ContentType_LINK; |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 149 | else if (ndn_memcmp(typeBytes, "\x34\x00\x8A", typeBytesLength) == 0) |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 150 | metaInfo->type = ndn_ContentType_NACK; |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 151 | else |
| 152 | return NDN_ERROR_unrecognized_ndn_ContentType; |
| 153 | } |
| 154 | else |
| 155 | return NDN_ERROR_unrecognized_ndn_ContentType; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 156 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 157 | if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 158 | (decoder, ndn_BinaryXml_DTag_FreshnessSeconds, &metaInfo->freshnessSeconds))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 159 | return error; |
| 160 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 161 | if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 162 | (decoder, ndn_BinaryXml_DTag_FinalBlockID, 0, &metaInfo->finalBlockID, &metaInfo->finalBlockIDLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 163 | return error; |
| 164 | |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 165 | if ((error = ndn_decodeOptionalBinaryXmlKeyLocator(&metaInfo->keyLocator, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 166 | return error; |
| 167 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 168 | if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 169 | return error; |
| 170 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 171 | return NDN_ERROR_success; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 174 | ndn_Error ndn_encodeBinaryXmlData |
| 175 | (struct ndn_Data *data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset, struct ndn_BinaryXmlEncoder *encoder) |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 176 | { |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 177 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 178 | if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_ContentObject))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 179 | return error; |
| 180 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 181 | if ((error = encodeSignature(&data->signature, encoder))) |
Jeff Thompson | ae8a6f8 | 2013-08-16 17:12:49 -0700 | [diff] [blame] | 182 | return error; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 183 | |
| 184 | *signedFieldsBeginOffset = encoder->offset; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 185 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 186 | if ((error = ndn_encodeBinaryXmlName(&data->name, encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 187 | return error; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 188 | |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 189 | if ((error = encodeSignedInfo(&data->metaInfo, encoder))) |
Jeff Thompson | ae8a6f8 | 2013-08-16 17:12:49 -0700 | [diff] [blame] | 190 | return error; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 191 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 192 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
| 193 | (encoder, ndn_BinaryXml_DTag_Content, data->content, data->contentLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 194 | return error; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 195 | |
| 196 | *signedFieldsEndOffset = encoder->offset; |
| 197 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 198 | if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 199 | return error; |
| 200 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 201 | return NDN_ERROR_success; |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Jeff Thompson | 2d4698a | 2013-08-12 11:28:34 -0700 | [diff] [blame] | 204 | ndn_Error ndn_decodeBinaryXmlData |
| 205 | (struct ndn_Data *data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset, struct ndn_BinaryXmlDecoder *decoder) |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 206 | { |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 207 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 208 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_ContentObject))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 209 | return error; |
| 210 | |
| 211 | int gotExpectedTag; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 212 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Signature, &gotExpectedTag))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 213 | return error; |
| 214 | if (gotExpectedTag) { |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 215 | if ((error = decodeSignature(&data->signature, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 216 | return error; |
| 217 | } |
| 218 | else |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 219 | ndn_Signature_initialize(&data->signature); |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 220 | |
Jeff Thompson | 2d4698a | 2013-08-12 11:28:34 -0700 | [diff] [blame] | 221 | *signedFieldsBeginOffset = decoder->offset; |
| 222 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 223 | if ((error = ndn_decodeBinaryXmlName(&data->name, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 224 | return error; |
| 225 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 226 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_SignedInfo, &gotExpectedTag))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 227 | return error; |
| 228 | if (gotExpectedTag) { |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 229 | if ((error = decodeSignedInfo(&data->metaInfo, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 230 | return error; |
| 231 | } |
| 232 | else |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 233 | ndn_MetaInfo_initialize(&data->metaInfo, data->metaInfo.keyLocator.keyName.components, data->metaInfo.keyLocator.keyName.maxComponents); |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 234 | |
| 235 | // Require a Content element, but set allowNull to allow a missing BLOB. |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 236 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement |
| 237 | (decoder, ndn_BinaryXml_DTag_Content, 1, &data->content, &data->contentLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 238 | return error; |
| 239 | |
Jeff Thompson | 2d4698a | 2013-08-12 11:28:34 -0700 | [diff] [blame] | 240 | *signedFieldsEndOffset = decoder->offset; |
| 241 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 242 | if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 243 | return error; |
| 244 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 245 | return NDN_ERROR_success; |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 246 | } |