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