Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 8 | #include <stdexcept> |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame^] | 9 | #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 Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 13 | #include "../c/encoding/binary-xml-interest.h" |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 14 | #include "../c/encoding/binary-xml-data.h" |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 15 | #include "../c/encoding/binary-xml-forwarding-entry.h" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 16 | #include "binary-xml-encoder.hpp" |
| 17 | #include "binary-xml-decoder.hpp" |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 18 | |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 19 | using namespace std; |
| 20 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 21 | namespace ndn { |
| 22 | |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 23 | // This is declared in the WireFormat class. |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 24 | WireFormat* |
| 25 | WireFormat::newInitialDefaultWireFormat() |
Jeff Thompson | fa181ac | 2013-08-02 19:00:51 -0700 | [diff] [blame] | 26 | { |
| 27 | return new BinaryXmlWireFormat(); |
| 28 | } |
| 29 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 30 | Blob |
| 31 | BinaryXmlWireFormat::encodeInterest(const Interest& interest) |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 32 | { |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 33 | struct ndn_NameComponent nameComponents[100]; |
| 34 | struct ndn_ExcludeEntry excludeEntries[100]; |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 35 | struct ndn_Interest interestStruct; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 36 | ndn_Interest_initialize |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 37 | (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 38 | excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0])); |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 39 | interest.get(interestStruct); |
| 40 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 41 | BinaryXmlEncoder encoder; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 42 | ndn_Error error; |
| 43 | if ((error = ndn_encodeBinaryXmlInterest(&interestStruct, &encoder))) |
| 44 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 45 | |
Jeff Thompson | b0979fd | 2013-07-30 15:48:21 -0700 | [diff] [blame] | 46 | return encoder.getOutput(); |
Jeff Thompson | 214c7be | 2013-07-08 15:23:00 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 49 | void |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 50 | BinaryXmlWireFormat::decodeInterest(Interest& interest, const uint8_t *input, size_t inputLength) |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 51 | { |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 52 | struct ndn_NameComponent nameComponents[100]; |
| 53 | struct ndn_ExcludeEntry excludeEntries[100]; |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 54 | struct ndn_Interest interestStruct; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 55 | ndn_Interest_initialize |
Jeff Thompson | f084ec6 | 2013-07-09 12:32:52 -0700 | [diff] [blame] | 56 | (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 57 | excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0])); |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 58 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 59 | BinaryXmlDecoder decoder(input, inputLength); |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 60 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 61 | if ((error = ndn_decodeBinaryXmlInterest(&interestStruct, &decoder))) |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 62 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 63 | |
| 64 | interest.set(interestStruct); |
| 65 | } |
| 66 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 67 | Blob |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 68 | BinaryXmlWireFormat::encodeData(const Data& data, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 69 | { |
| 70 | struct ndn_NameComponent nameComponents[100]; |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 71 | struct ndn_NameComponent keyNameComponents[100]; |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 72 | struct ndn_Data dataStruct; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 73 | ndn_Data_initialize |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 74 | (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 75 | keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0])); |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 76 | data.get(dataStruct); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 77 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 78 | BinaryXmlEncoder encoder; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 79 | ndn_Error error; |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 80 | if ((error = ndn_encodeBinaryXmlData(&dataStruct, signedPortionBeginOffset, signedPortionEndOffset, &encoder))) |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 81 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 82 | |
Jeff Thompson | b0979fd | 2013-07-30 15:48:21 -0700 | [diff] [blame] | 83 | return encoder.getOutput(); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 86 | void |
| 87 | BinaryXmlWireFormat::decodeData |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 88 | (Data& data, const uint8_t *input, size_t inputLength, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 89 | { |
| 90 | struct ndn_NameComponent nameComponents[100]; |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 91 | struct ndn_NameComponent keyNameComponents[100]; |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 92 | struct ndn_Data dataStruct; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 93 | ndn_Data_initialize |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 94 | (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]), |
| 95 | keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0])); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 96 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 97 | BinaryXmlDecoder decoder(input, inputLength); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 98 | ndn_Error error; |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 99 | if ((error = ndn_decodeBinaryXmlData(&dataStruct, signedPortionBeginOffset, signedPortionEndOffset, &decoder))) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 100 | throw std::runtime_error(ndn_getErrorString(error)); |
| 101 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 102 | data.set(dataStruct); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 105 | Blob |
| 106 | BinaryXmlWireFormat::encodeForwardingEntry(const ForwardingEntry& forwardingEntry) |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 107 | { |
| 108 | struct ndn_NameComponent prefixNameComponents[100]; |
| 109 | struct ndn_ForwardingEntry forwardingEntryStruct; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 110 | ndn_ForwardingEntry_initialize |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 111 | (&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))) |
| 117 | throw std::runtime_error(ndn_getErrorString(error)); |
| 118 | |
| 119 | return encoder.getOutput(); |
| 120 | } |
| 121 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 122 | void |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 123 | BinaryXmlWireFormat::decodeForwardingEntry(ForwardingEntry& forwardingEntry, const uint8_t *input, size_t inputLength) |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 124 | { |
| 125 | struct ndn_NameComponent prefixNameComponents[100]; |
| 126 | struct ndn_ForwardingEntry forwardingEntryStruct; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 127 | ndn_ForwardingEntry_initialize |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 128 | (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0])); |
| 129 | |
| 130 | BinaryXmlDecoder decoder(input, inputLength); |
| 131 | ndn_Error error; |
| 132 | if ((error = ndn_decodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &decoder))) |
| 133 | throw std::runtime_error(ndn_getErrorString(error)); |
| 134 | |
| 135 | forwardingEntry.set(forwardingEntryStruct); |
| 136 | } |
| 137 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 138 | } |