blob: b7583efc6fe1e17ee8ea15bba6910142870b82eb [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
7#include "BinaryXML.h"
Jeff Thompson46c06512013-07-14 23:26:09 -07008#include "BinaryXMLStructureDecoder.h"
Jeff Thompson9915bdd2013-07-11 11:27:09 -07009#include "BinaryXMLKey.h"
10
11ndn_Error ndn_encodeBinaryXMLKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXMLEncoder *encoder)
12{
13 if (keyLocator->type < 0)
14 return 0;
15
16 ndn_Error error;
17 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_KeyLocator))
18 return error;
19
20 if (keyLocator->type == ndn_KeyLocatorType_KEY) {
21 if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
22 (encoder, ndn_BinaryXML_DTag_Key, keyLocator->keyOrCertificate, keyLocator->keyOrCertificateLength))
23 return error;
24 }
25 else if (keyLocator->type == ndn_KeyLocatorType_CERTIFICATE) {
26 if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
27 (encoder, ndn_BinaryXML_DTag_Certificate, keyLocator->keyOrCertificate, keyLocator->keyOrCertificateLength))
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
36 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
37 return error;
38
39 return 0;
40}
41
42ndn_Error ndn_decodeBinaryXMLKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXMLDecoder *decoder)
43{
44 ndn_Error error;
45 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_KeyLocator))
46 return error;
47
48 int gotExpectedTag;
49 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Key, &gotExpectedTag))
50 return error;
51 if (gotExpectedTag) {
52 keyLocator->type = ndn_KeyLocatorType_KEY;
53
54 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
55 (decoder, ndn_BinaryXML_DTag_Key, 0, &keyLocator->keyOrCertificate, &keyLocator->keyOrCertificateLength))
56 return error;
57 }
58 else {
59 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Certificate, &gotExpectedTag))
60 return error;
61 if (gotExpectedTag) {
62 keyLocator->type = ndn_KeyLocatorType_CERTIFICATE;
63
64 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
65 (decoder, ndn_BinaryXML_DTag_Certificate, 0, &keyLocator->keyOrCertificate, &keyLocator->keyOrCertificateLength))
66 return error;
67 }
68 else {
69 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_KeyName, &gotExpectedTag))
70 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.
73 struct ndn_BinaryXMLStructureDecoder structureDecoder;
74 ndn_BinaryXMLStructureDecoder_init(&structureDecoder);
75
76 ndn_BinaryXMLStructureDecoder_seek(&structureDecoder, decoder->offset);
77 if (error = ndn_BinaryXMLStructureDecoder_findElementEnd(&structureDecoder, decoder->input, decoder->inputLength))
78 return error;
79 if (!structureDecoder.gotElementEnd)
80 return NDN_ERROR_read_past_the_end_of_the_input;
81 ndn_BinaryXMLDecoder_seek(decoder, structureDecoder.offset);
Jeff Thompson9915bdd2013-07-11 11:27:09 -070082 }
83 else
84 return NDN_ERROR_decodeBinaryXMLKeyLocator_unrecognized_key_locator_type;
85 }
86 }
87
88 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
89 return error;
90
91 return 0;
92}
93
94ndn_Error ndn_decodeOptionalBinaryXMLKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXMLDecoder *decoder)
95{
96 int gotExpectedTag;
97 ndn_Error error;
98 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_KeyLocator, &gotExpectedTag))
99 return error;
100 if (gotExpectedTag) {
101 if (error = ndn_decodeBinaryXMLKeyLocator(keyLocator, decoder))
102 return error;
103 }
104 else
105 ndn_KeyLocator_init(keyLocator);
106
107 return 0;
108}