blob: e89afaf85264a6e50e12f782b3f122e6b1e6034c [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 Thompsone2c232d2013-07-03 18:47:29 -070012#include "../c/encoding/BinaryXMLDecoder.h"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070013#include "BinaryXMLWireFormat.hpp"
Jeff Thompson4c89ad62013-06-28 12:50:13 -070014
Jeff Thompson1f3f5172013-07-01 19:02:36 -070015using namespace std;
16
Jeff Thompson4c89ad62013-06-28 12:50:13 -070017namespace ndn {
18
19BinaryXMLWireFormat BinaryXMLWireFormat::instance_;
20
Jeff Thompson1f3f5172013-07-01 19:02:36 -070021void BinaryXMLWireFormat::encodeName(Name &name, vector<unsigned char> &output)
22{
23 struct ndn_Name nameStruct;
24 struct ndn_NameComponent components[100];
25 ndn_Name_init(&nameStruct, components, sizeof(components) / sizeof(components[0]));
26 name.get(nameStruct);
27
28 BinaryXMLEncoder encoder;
29 ndn_encodeBinaryXMLName(&nameStruct, encoder.getEncoder());
Jeff Thompson58d798f2013-07-02 14:16:25 -070030
31 encoder.appendTo(output);
Jeff Thompson1f3f5172013-07-01 19:02:36 -070032}
33
Jeff Thompson4c89ad62013-06-28 12:50:13 -070034void BinaryXMLWireFormat::decodeName(Name &name, const unsigned char *input, unsigned int inputLength)
35{
36 struct ndn_NameComponent components[100];
37 struct ndn_Name nameStruct;
38 ndn_Name_init(&nameStruct, components, sizeof(components) / sizeof(components[0]));
39
Jeff Thompsone2c232d2013-07-03 18:47:29 -070040 struct ndn_BinaryXMLDecoder decoder;
41 ndn_BinaryXMLDecoder_init(&decoder, (unsigned char *)input, inputLength);
42
Jeff Thompson4c89ad62013-06-28 12:50:13 -070043 char *error;
Jeff Thompsone2c232d2013-07-03 18:47:29 -070044 if (error = ndn_decodeBinaryXMLName(&nameStruct, &decoder))
Jeff Thompson4c89ad62013-06-28 12:50:13 -070045 throw std::runtime_error(error);
Jeff Thompson1f3f5172013-07-01 19:02:36 -070046
47 name.set(nameStruct);
Jeff Thompson4c89ad62013-06-28 12:50:13 -070048}
49
Jeff Thompson7c30eda2013-07-03 18:37:07 -070050void BinaryXMLWireFormat::decodeInterest(Interest &interest, const unsigned char *input, unsigned int inputLength)
51{
52 struct ndn_NameComponent components[100];
53 struct ndn_Interest interestStruct;
54 ndn_Interest_init(&interestStruct, components, sizeof(components) / sizeof(components[0]));
55
Jeff Thompsone2c232d2013-07-03 18:47:29 -070056 struct ndn_BinaryXMLDecoder decoder;
57 ndn_BinaryXMLDecoder_init(&decoder, (unsigned char *)input, inputLength);
58
Jeff Thompson7c30eda2013-07-03 18:37:07 -070059 char *error;
Jeff Thompsone2c232d2013-07-03 18:47:29 -070060 if (error = ndn_decodeBinaryXMLInterest(&interestStruct, &decoder))
Jeff Thompson7c30eda2013-07-03 18:37:07 -070061 throw std::runtime_error(error);
62
63 interest.set(interestStruct);
64}
65
Jeff Thompson4c89ad62013-06-28 12:50:13 -070066}