blob: b1f4007c3920978ec459b16466cd12f1348af36a [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07005 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07006 */
7
Jeff Thompson4c89ad62013-06-28 12:50:13 -07008#include <stdexcept>
Jeff Thompson25b4e612013-10-10 16:03:24 -07009#include <ndn-cpp/interest.hpp>
10#include <ndn-cpp/data.hpp>
11#include <ndn-cpp/forwarding-entry.hpp>
12#include <ndn-cpp/encoding/binary-xml-wire-format.hpp>
Jeff Thompson53412192013-08-06 13:35:50 -070013#include "../c/encoding/binary-xml-interest.h"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070014#include "../c/encoding/binary-xml-data.h"
Jeff Thompson990599b2013-08-27 15:14:25 -070015#include "../c/encoding/binary-xml-forwarding-entry.h"
Jeff Thompson53412192013-08-06 13:35:50 -070016#include "binary-xml-encoder.hpp"
17#include "binary-xml-decoder.hpp"
Jeff Thompson4c89ad62013-06-28 12:50:13 -070018
Jeff Thompson1f3f5172013-07-01 19:02:36 -070019using namespace std;
20
Jeff Thompson4c89ad62013-06-28 12:50:13 -070021namespace ndn {
22
Jeff Thompsonfa181ac2013-08-02 19:00:51 -070023// This is declared in the WireFormat class.
Jeff Thompson0050abe2013-09-17 12:50:25 -070024WireFormat*
25WireFormat::newInitialDefaultWireFormat()
Jeff Thompsonfa181ac2013-08-02 19:00:51 -070026{
27 return new BinaryXmlWireFormat();
28}
29
Jeff Thompson0050abe2013-09-17 12:50:25 -070030Blob
31BinaryXmlWireFormat::encodeInterest(const Interest& interest)
Jeff Thompson214c7be2013-07-08 15:23:00 -070032{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070033 struct ndn_NameComponent nameComponents[100];
34 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson214c7be2013-07-08 15:23:00 -070035 struct ndn_Interest interestStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070036 ndn_Interest_initialize
Jeff Thompsonf084ec62013-07-09 12:32:52 -070037 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
38 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson214c7be2013-07-08 15:23:00 -070039 interest.get(interestStruct);
40
Jeff Thompsonf0fea002013-07-30 17:22:42 -070041 BinaryXmlEncoder encoder;
Jeff Thompsonaa020712013-08-08 21:20:06 -070042 ndn_Error error;
43 if ((error = ndn_encodeBinaryXmlInterest(&interestStruct, &encoder)))
Jeff Thompson4affbf52013-10-18 14:36:46 -070044 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson214c7be2013-07-08 15:23:00 -070045
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070046 return encoder.getOutput();
Jeff Thompson214c7be2013-07-08 15:23:00 -070047}
48
Jeff Thompson0050abe2013-09-17 12:50:25 -070049void
Jeff Thompson97223af2013-09-24 17:01:27 -070050BinaryXmlWireFormat::decodeInterest(Interest& interest, const uint8_t *input, size_t inputLength)
Jeff Thompson7c30eda2013-07-03 18:37:07 -070051{
Jeff Thompsonf084ec62013-07-09 12:32:52 -070052 struct ndn_NameComponent nameComponents[100];
53 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson7c30eda2013-07-03 18:37:07 -070054 struct ndn_Interest interestStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070055 ndn_Interest_initialize
Jeff Thompsonf084ec62013-07-09 12:32:52 -070056 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
57 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070058
Jeff Thompsonf0fea002013-07-30 17:22:42 -070059 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070060 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070061 if ((error = ndn_decodeBinaryXmlInterest(&interestStruct, &decoder)))
Jeff Thompson4affbf52013-10-18 14:36:46 -070062 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson7c30eda2013-07-03 18:37:07 -070063
64 interest.set(interestStruct);
65}
66
Jeff Thompson0050abe2013-09-17 12:50:25 -070067Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070068BinaryXmlWireFormat::encodeData(const Data& data, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070069{
70 struct ndn_NameComponent nameComponents[100];
Jeff Thompson7329a132013-08-16 15:57:37 -070071 struct ndn_NameComponent keyNameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070072 struct ndn_Data dataStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070073 ndn_Data_initialize
Jeff Thompson7329a132013-08-16 15:57:37 -070074 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
75 keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0]));
Jeff Thompson56ec9e22013-08-02 11:34:07 -070076 data.get(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070077
Jeff Thompsonf0fea002013-07-30 17:22:42 -070078 BinaryXmlEncoder encoder;
Jeff Thompsonaa020712013-08-08 21:20:06 -070079 ndn_Error error;
Jeff Thompson9c661702013-09-13 14:35:44 -070080 if ((error = ndn_encodeBinaryXmlData(&dataStruct, signedPortionBeginOffset, signedPortionEndOffset, &encoder)))
Jeff Thompson4affbf52013-10-18 14:36:46 -070081 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070082
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070083 return encoder.getOutput();
Jeff Thompson5cae5e52013-07-10 19:41:20 -070084}
85
Jeff Thompson0050abe2013-09-17 12:50:25 -070086void
87BinaryXmlWireFormat::decodeData
Jeff Thompson97223af2013-09-24 17:01:27 -070088 (Data& data, const uint8_t *input, size_t inputLength, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070089{
90 struct ndn_NameComponent nameComponents[100];
Jeff Thompson7329a132013-08-16 15:57:37 -070091 struct ndn_NameComponent keyNameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -070092 struct ndn_Data dataStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070093 ndn_Data_initialize
Jeff Thompson7329a132013-08-16 15:57:37 -070094 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
95 keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0]));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070096
Jeff Thompsonf0fea002013-07-30 17:22:42 -070097 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070098 ndn_Error error;
Jeff Thompson9c661702013-09-13 14:35:44 -070099 if ((error = ndn_decodeBinaryXmlData(&dataStruct, signedPortionBeginOffset, signedPortionEndOffset, &decoder)))
Jeff Thompson4affbf52013-10-18 14:36:46 -0700100 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700101
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700102 data.set(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700103}
104
Jeff Thompson0050abe2013-09-17 12:50:25 -0700105Blob
106BinaryXmlWireFormat::encodeForwardingEntry(const ForwardingEntry& forwardingEntry)
Jeff Thompson990599b2013-08-27 15:14:25 -0700107{
108 struct ndn_NameComponent prefixNameComponents[100];
109 struct ndn_ForwardingEntry forwardingEntryStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700110 ndn_ForwardingEntry_initialize
Jeff Thompson990599b2013-08-27 15:14:25 -0700111 (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0]));
112 forwardingEntry.get(forwardingEntryStruct);
113
114 BinaryXmlEncoder encoder;
115 ndn_Error error;
116 if ((error = ndn_encodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &encoder)))
Jeff Thompson4affbf52013-10-18 14:36:46 -0700117 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson990599b2013-08-27 15:14:25 -0700118
119 return encoder.getOutput();
120}
121
Jeff Thompson0050abe2013-09-17 12:50:25 -0700122void
Jeff Thompson97223af2013-09-24 17:01:27 -0700123BinaryXmlWireFormat::decodeForwardingEntry(ForwardingEntry& forwardingEntry, const uint8_t *input, size_t inputLength)
Jeff Thompson990599b2013-08-27 15:14:25 -0700124{
125 struct ndn_NameComponent prefixNameComponents[100];
126 struct ndn_ForwardingEntry forwardingEntryStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700127 ndn_ForwardingEntry_initialize
Jeff Thompson990599b2013-08-27 15:14:25 -0700128 (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0]));
129
130 BinaryXmlDecoder decoder(input, inputLength);
131 ndn_Error error;
132 if ((error = ndn_decodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &decoder)))
Jeff Thompson4affbf52013-10-18 14:36:46 -0700133 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson990599b2013-08-27 15:14:25 -0700134
135 forwardingEntry.set(forwardingEntryStruct);
136}
137
Jeff Thompson4c89ad62013-06-28 12:50:13 -0700138}