blob: 271029b915ff06881bb5a2e9dc29676fbb6f3ff1 [file] [log] [blame]
Jeff Thompson990599b2013-08-27 15:14:25 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson990599b2013-08-27 15:14:25 -07004 * Derived from ForwardingEntry.js by Meki Cheraoui.
5 * See COPYING for copyright and distribution information.
6 */
7
8#include "binary-xml.h"
9#include "binary-xml-forwarding-entry.h"
10
11ndn_Error ndn_encodeBinaryXmlForwardingEntry(struct ndn_ForwardingEntry *forwardingEntry, struct ndn_BinaryXmlEncoder *encoder)
12{
13 ndn_Error error;
14 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_ForwardingEntry)))
15 return error;
16
17 if ((error = ndn_BinaryXmlEncoder_writeOptionalUDataDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070018 (encoder, ndn_BinaryXml_DTag_Action, &forwardingEntry->action)))
Jeff Thompson990599b2013-08-27 15:14:25 -070019 return error;
20 if ((error = ndn_encodeBinaryXmlName(&forwardingEntry->prefix, encoder)))
21 return error;
22 // This will skip encoding if there is no publisherPublicKeyDigest.
23 if ((error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&forwardingEntry->publisherPublicKeyDigest, encoder)))
24 return error;
25 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
26 (encoder, ndn_BinaryXml_DTag_FaceID, forwardingEntry->faceId)))
27 return error;
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070028 if ((error = ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement
29 (encoder, ndn_BinaryXml_DTag_ForwardingFlags,
30 ndn_ForwardingFlags_getForwardingEntryFlags(&forwardingEntry->forwardingFlags))))
Jeff Thompson990599b2013-08-27 15:14:25 -070031 return error;
32 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
33 (encoder, ndn_BinaryXml_DTag_FreshnessSeconds, forwardingEntry->freshnessSeconds)))
34 return error;
35
36 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
37 return error;
38
39 return NDN_ERROR_success;
40}
41
42ndn_Error ndn_decodeBinaryXmlForwardingEntry(struct ndn_ForwardingEntry *forwardingEntry, struct ndn_BinaryXmlDecoder *decoder)
43{
44 ndn_Error error;
45 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_ForwardingEntry)))
46 return error;
47
48 if ((error = ndn_BinaryXmlDecoder_readOptionalUDataDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070049 (decoder, ndn_BinaryXml_DTag_Action, &forwardingEntry->action)))
Jeff Thompson990599b2013-08-27 15:14:25 -070050 return error;
51 if ((error = ndn_decodeBinaryXmlName(&forwardingEntry->prefix, decoder)))
52 return error;
53 if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&forwardingEntry->publisherPublicKeyDigest, decoder)))
54 return error;
55 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
56 (decoder, ndn_BinaryXml_DTag_FaceID, &forwardingEntry->faceId)))
57 return error;
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070058
59 int forwardingEntryFlags;
Jeff Thompson990599b2013-08-27 15:14:25 -070060 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070061 (decoder, ndn_BinaryXml_DTag_ForwardingFlags, &forwardingEntryFlags)))
Jeff Thompson990599b2013-08-27 15:14:25 -070062 return error;
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070063 if (forwardingEntryFlags >= 0)
64 ndn_ForwardingFlags_setForwardingEntryFlags(&forwardingEntry->forwardingFlags, forwardingEntryFlags);
65 else
66 // This sets the default flags.
67 ndn_ForwardingFlags_initialize(&forwardingEntry->forwardingFlags);
68
Jeff Thompson990599b2013-08-27 15:14:25 -070069 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
70 (decoder, ndn_BinaryXml_DTag_FreshnessSeconds, &forwardingEntry->freshnessSeconds)))
71 return error;
72
73 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
74 return error;
75
76 return NDN_ERROR_success;
77}