Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * Derived from Key.js by Meki Cheraoui. |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "BinaryXML.h" |
Jeff Thompson | 46c0651 | 2013-07-14 23:26:09 -0700 | [diff] [blame] | 8 | #include "BinaryXMLStructureDecoder.h" |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 9 | #include "BinaryXMLKey.h" |
| 10 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 11 | ndn_Error ndn_encodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlEncoder *encoder) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 12 | { |
| 13 | if (keyLocator->type < 0) |
| 14 | return 0; |
| 15 | |
| 16 | ndn_Error error; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 17 | if (error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_KeyLocator)) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 18 | return error; |
| 19 | |
| 20 | if (keyLocator->type == ndn_KeyLocatorType_KEY) { |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 21 | if (error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
| 22 | (encoder, ndn_BinaryXml_DTag_Key, keyLocator->keyOrCertificate, keyLocator->keyOrCertificateLength)) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 23 | return error; |
| 24 | } |
| 25 | else if (keyLocator->type == ndn_KeyLocatorType_CERTIFICATE) { |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 26 | if (error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
| 27 | (encoder, ndn_BinaryXml_DTag_Certificate, keyLocator->keyOrCertificate, keyLocator->keyOrCertificateLength)) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 28 | return error; |
| 29 | } |
| 30 | else if (keyLocator->type == ndn_KeyLocatorType_KEYNAME) { |
| 31 | // TODO: Implement keyName |
| 32 | } |
| 33 | else |
| 34 | return NDN_ERROR_unrecognized_ndn_KeyLocatorType; |
| 35 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 36 | if (error = ndn_BinaryXmlEncoder_writeElementClose(encoder)) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 37 | return error; |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 42 | ndn_Error ndn_decodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 43 | { |
| 44 | ndn_Error error; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 45 | if (error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_KeyLocator)) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 46 | return error; |
| 47 | |
| 48 | int gotExpectedTag; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 49 | if (error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Key, &gotExpectedTag)) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 50 | return error; |
| 51 | if (gotExpectedTag) { |
| 52 | keyLocator->type = ndn_KeyLocatorType_KEY; |
| 53 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 54 | if (error = ndn_BinaryXmlDecoder_readBinaryDTagElement |
| 55 | (decoder, ndn_BinaryXml_DTag_Key, 0, &keyLocator->keyOrCertificate, &keyLocator->keyOrCertificateLength)) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 56 | return error; |
| 57 | } |
| 58 | else { |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 59 | if (error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Certificate, &gotExpectedTag)) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 60 | return error; |
| 61 | if (gotExpectedTag) { |
| 62 | keyLocator->type = ndn_KeyLocatorType_CERTIFICATE; |
| 63 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 64 | if (error = ndn_BinaryXmlDecoder_readBinaryDTagElement |
| 65 | (decoder, ndn_BinaryXml_DTag_Certificate, 0, &keyLocator->keyOrCertificate, &keyLocator->keyOrCertificateLength)) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 66 | return error; |
| 67 | } |
| 68 | else { |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 69 | if (error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyName, &gotExpectedTag)) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 70 | return error; |
| 71 | if (gotExpectedTag) { |
Jeff Thompson | 46c0651 | 2013-07-14 23:26:09 -0700 | [diff] [blame] | 72 | // TODO: Implement keyName. For now, just use a structure decoder to skip it. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 73 | struct ndn_BinaryXmlStructureDecoder structureDecoder; |
| 74 | ndn_BinaryXmlStructureDecoder_init(&structureDecoder); |
Jeff Thompson | 46c0651 | 2013-07-14 23:26:09 -0700 | [diff] [blame] | 75 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 76 | ndn_BinaryXmlStructureDecoder_seek(&structureDecoder, decoder->offset); |
| 77 | if (error = ndn_BinaryXmlStructureDecoder_findElementEnd(&structureDecoder, decoder->input, decoder->inputLength)) |
Jeff Thompson | 46c0651 | 2013-07-14 23:26:09 -0700 | [diff] [blame] | 78 | return error; |
| 79 | if (!structureDecoder.gotElementEnd) |
| 80 | return NDN_ERROR_read_past_the_end_of_the_input; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 81 | ndn_BinaryXmlDecoder_seek(decoder, structureDecoder.offset); |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 82 | } |
| 83 | else |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 84 | return NDN_ERROR_decodeBinaryXmlKeyLocator_unrecognized_key_locator_type; |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 88 | if (error = ndn_BinaryXmlDecoder_readElementClose(decoder)) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 89 | return error; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 94 | ndn_Error ndn_decodeOptionalBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 95 | { |
| 96 | int gotExpectedTag; |
| 97 | ndn_Error error; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 98 | if (error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyLocator, &gotExpectedTag)) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 99 | return error; |
| 100 | if (gotExpectedTag) { |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 101 | if (error = ndn_decodeBinaryXmlKeyLocator(keyLocator, decoder)) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 102 | return error; |
| 103 | } |
| 104 | else |
| 105 | ndn_KeyLocator_init(keyLocator); |
| 106 | |
| 107 | return 0; |
| 108 | } |