blob: 8d51b4dfaea662612172691d51fbb7ae5dbe2999 [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"
8#include "BinaryXMLKey.h"
9
10ndn_Error ndn_encodeBinaryXMLKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXMLEncoder *encoder)
11{
12 if (keyLocator->type < 0)
13 return 0;
14
15 ndn_Error error;
16 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_KeyLocator))
17 return error;
18
19 if (keyLocator->type == ndn_KeyLocatorType_KEY) {
20 if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
21 (encoder, ndn_BinaryXML_DTag_Key, keyLocator->keyOrCertificate, keyLocator->keyOrCertificateLength))
22 return error;
23 }
24 else if (keyLocator->type == ndn_KeyLocatorType_CERTIFICATE) {
25 if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
26 (encoder, ndn_BinaryXML_DTag_Certificate, keyLocator->keyOrCertificate, keyLocator->keyOrCertificateLength))
27 return error;
28 }
29 else if (keyLocator->type == ndn_KeyLocatorType_KEYNAME) {
30 // TODO: Implement keyName
31 }
32 else
33 return NDN_ERROR_unrecognized_ndn_KeyLocatorType;
34
35 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
36 return error;
37
38 return 0;
39}
40
41ndn_Error ndn_decodeBinaryXMLKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXMLDecoder *decoder)
42{
43 ndn_Error error;
44 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_KeyLocator))
45 return error;
46
47 int gotExpectedTag;
48 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Key, &gotExpectedTag))
49 return error;
50 if (gotExpectedTag) {
51 keyLocator->type = ndn_KeyLocatorType_KEY;
52
53 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
54 (decoder, ndn_BinaryXML_DTag_Key, 0, &keyLocator->keyOrCertificate, &keyLocator->keyOrCertificateLength))
55 return error;
56 }
57 else {
58 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Certificate, &gotExpectedTag))
59 return error;
60 if (gotExpectedTag) {
61 keyLocator->type = ndn_KeyLocatorType_CERTIFICATE;
62
63 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
64 (decoder, ndn_BinaryXML_DTag_Certificate, 0, &keyLocator->keyOrCertificate, &keyLocator->keyOrCertificateLength))
65 return error;
66 }
67 else {
68 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_KeyName, &gotExpectedTag))
69 return error;
70 if (gotExpectedTag) {
71 // TODO: Implement keyName
72 }
73 else
74 return NDN_ERROR_decodeBinaryXMLKeyLocator_unrecognized_key_locator_type;
75 }
76 }
77
78 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
79 return error;
80
81 return 0;
82}
83
84ndn_Error ndn_decodeOptionalBinaryXMLKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXMLDecoder *decoder)
85{
86 int gotExpectedTag;
87 ndn_Error error;
88 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_KeyLocator, &gotExpectedTag))
89 return error;
90 if (gotExpectedTag) {
91 if (error = ndn_decodeBinaryXMLKeyLocator(keyLocator, decoder))
92 return error;
93 }
94 else
95 ndn_KeyLocator_init(keyLocator);
96
97 return 0;
98}