blob: c26adbacc6719197826ac5ea724a536374d66739 [file] [log] [blame]
Jeff Thompson9915bdd2013-07-11 11:27:09 -07001/**
2 * @author: Jeff Thompson
3 * Derived from Key.js by Meki Cheraoui.
4 * See COPYING for copyright and distribution information.
5 */
6
Jeff Thompson53412192013-08-06 13:35:50 -07007#include "binary-xml.h"
8#include "binary-xml-structure-decoder.h"
9#include "binary-xml-key.h"
Jeff Thompson9915bdd2013-07-11 11:27:09 -070010
Jeff Thompsonf0fea002013-07-30 17:22:42 -070011ndn_Error ndn_encodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -070012{
13 if (keyLocator->type < 0)
14 return 0;
15
16 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070017 if (error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_KeyLocator))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070018 return error;
19
20 if (keyLocator->type == ndn_KeyLocatorType_KEY) {
Jeff Thompsonf0fea002013-07-30 17:22:42 -070021 if (error = ndn_BinaryXmlEncoder_writeBlobDTagElement
22 (encoder, ndn_BinaryXml_DTag_Key, keyLocator->keyOrCertificate, keyLocator->keyOrCertificateLength))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070023 return error;
24 }
25 else if (keyLocator->type == ndn_KeyLocatorType_CERTIFICATE) {
Jeff Thompsonf0fea002013-07-30 17:22:42 -070026 if (error = ndn_BinaryXmlEncoder_writeBlobDTagElement
27 (encoder, ndn_BinaryXml_DTag_Certificate, keyLocator->keyOrCertificate, keyLocator->keyOrCertificateLength))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070028 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 Thompsonf0fea002013-07-30 17:22:42 -070036 if (error = ndn_BinaryXmlEncoder_writeElementClose(encoder))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070037 return error;
38
39 return 0;
40}
41
Jeff Thompsonf0fea002013-07-30 17:22:42 -070042ndn_Error ndn_decodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -070043{
44 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070045 if (error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_KeyLocator))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070046 return error;
47
48 int gotExpectedTag;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070049 if (error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Key, &gotExpectedTag))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070050 return error;
51 if (gotExpectedTag) {
52 keyLocator->type = ndn_KeyLocatorType_KEY;
53
Jeff Thompsonf0fea002013-07-30 17:22:42 -070054 if (error = ndn_BinaryXmlDecoder_readBinaryDTagElement
55 (decoder, ndn_BinaryXml_DTag_Key, 0, &keyLocator->keyOrCertificate, &keyLocator->keyOrCertificateLength))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070056 return error;
57 }
58 else {
Jeff Thompsonf0fea002013-07-30 17:22:42 -070059 if (error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Certificate, &gotExpectedTag))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070060 return error;
61 if (gotExpectedTag) {
62 keyLocator->type = ndn_KeyLocatorType_CERTIFICATE;
63
Jeff Thompsonf0fea002013-07-30 17:22:42 -070064 if (error = ndn_BinaryXmlDecoder_readBinaryDTagElement
65 (decoder, ndn_BinaryXml_DTag_Certificate, 0, &keyLocator->keyOrCertificate, &keyLocator->keyOrCertificateLength))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070066 return error;
67 }
68 else {
Jeff Thompsonf0fea002013-07-30 17:22:42 -070069 if (error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyName, &gotExpectedTag))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070070 return error;
71 if (gotExpectedTag) {
Jeff Thompson46c06512013-07-14 23:26:09 -070072 // TODO: Implement keyName. For now, just use a structure decoder to skip it.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070073 struct ndn_BinaryXmlStructureDecoder structureDecoder;
74 ndn_BinaryXmlStructureDecoder_init(&structureDecoder);
Jeff Thompson46c06512013-07-14 23:26:09 -070075
Jeff Thompsonf0fea002013-07-30 17:22:42 -070076 ndn_BinaryXmlStructureDecoder_seek(&structureDecoder, decoder->offset);
77 if (error = ndn_BinaryXmlStructureDecoder_findElementEnd(&structureDecoder, decoder->input, decoder->inputLength))
Jeff Thompson46c06512013-07-14 23:26:09 -070078 return error;
79 if (!structureDecoder.gotElementEnd)
80 return NDN_ERROR_read_past_the_end_of_the_input;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070081 ndn_BinaryXmlDecoder_seek(decoder, structureDecoder.offset);
Jeff Thompson9915bdd2013-07-11 11:27:09 -070082 }
83 else
Jeff Thompsonf0fea002013-07-30 17:22:42 -070084 return NDN_ERROR_decodeBinaryXmlKeyLocator_unrecognized_key_locator_type;
Jeff Thompson9915bdd2013-07-11 11:27:09 -070085 }
86 }
87
Jeff Thompsonf0fea002013-07-30 17:22:42 -070088 if (error = ndn_BinaryXmlDecoder_readElementClose(decoder))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070089 return error;
90
91 return 0;
92}
93
Jeff Thompsonf0fea002013-07-30 17:22:42 -070094ndn_Error ndn_decodeOptionalBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -070095{
96 int gotExpectedTag;
97 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070098 if (error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyLocator, &gotExpectedTag))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070099 return error;
100 if (gotExpectedTag) {
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700101 if (error = ndn_decodeBinaryXmlKeyLocator(keyLocator, decoder))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700102 return error;
103 }
104 else
105 ndn_KeyLocator_init(keyLocator);
106
107 return 0;
108}