blob: 57893945b529fec92e56290905f29476c8b656fc [file] [log] [blame]
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07001/*
2 * Author: Jeff Thompson
3 *
4 * BSD license, See the LICENSE file for more information.
5 */
6
Jeff Thompson4c89ad62013-06-28 12:50:13 -07007#include <stdexcept>
Jeff Thompson6cb56f92013-07-01 15:38:09 -07008#include "../c/encoding/BinaryXMLName.h"
Jeff Thompson7c30eda2013-07-03 18:37:07 -07009#include "../c/encoding/BinaryXMLInterest.h"
10#include "../Interest.hpp"
Jeff Thompson1f3f5172013-07-01 19:02:36 -070011#include "BinaryXMLEncoder.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070012#include "BinaryXMLWireFormat.hpp"
Jeff Thompson4c89ad62013-06-28 12:50:13 -070013
Jeff Thompson1f3f5172013-07-01 19:02:36 -070014using namespace std;
15
Jeff Thompson4c89ad62013-06-28 12:50:13 -070016namespace ndn {
17
18BinaryXMLWireFormat BinaryXMLWireFormat::instance_;
19
Jeff Thompson1f3f5172013-07-01 19:02:36 -070020void BinaryXMLWireFormat::encodeName(Name &name, vector<unsigned char> &output)
21{
22 struct ndn_Name nameStruct;
23 struct ndn_NameComponent components[100];
24 ndn_Name_init(&nameStruct, components, sizeof(components) / sizeof(components[0]));
25 name.get(nameStruct);
26
27 BinaryXMLEncoder encoder;
28 ndn_encodeBinaryXMLName(&nameStruct, encoder.getEncoder());
Jeff Thompson58d798f2013-07-02 14:16:25 -070029
30 encoder.appendTo(output);
Jeff Thompson1f3f5172013-07-01 19:02:36 -070031}
32
Jeff Thompson4c89ad62013-06-28 12:50:13 -070033void BinaryXMLWireFormat::decodeName(Name &name, const unsigned char *input, unsigned int inputLength)
34{
35 struct ndn_NameComponent components[100];
36 struct ndn_Name nameStruct;
37 ndn_Name_init(&nameStruct, components, sizeof(components) / sizeof(components[0]));
38
39 char *error;
40 if (error = ndn_decodeBinaryXMLName(&nameStruct, (unsigned char *)input, inputLength))
41 throw std::runtime_error(error);
Jeff Thompson1f3f5172013-07-01 19:02:36 -070042
43 name.set(nameStruct);
Jeff Thompson4c89ad62013-06-28 12:50:13 -070044}
45
Jeff Thompson7c30eda2013-07-03 18:37:07 -070046void BinaryXMLWireFormat::decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength)
47{
48 struct ndn_NameComponent components[100];
49 struct ndn_Interest interestStruct;
50 ndn_Interest_init(&interestStruct, components, sizeof(components) / sizeof(components[0]));
51
52 char *error;
53 if (error = ndn_decodeBinaryXMLInterest(&interestStruct, (unsigned char *)input, inputLength))
54 throw std::runtime_error(error);
55
56 interest.set(interestStruct);
57}
58
Jeff Thompson4c89ad62013-06-28 12:50:13 -070059}