Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Author: Jeff Thompson |
| 3 | * |
| 4 | * BSD license, See the LICENSE file for more information. |
| 5 | */ |
| 6 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 7 | #include <stdexcept> |
Jeff Thompson | 6cb56f9 | 2013-07-01 15:38:09 -0700 | [diff] [blame] | 8 | #include "../c/encoding/BinaryXMLName.h" |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 9 | #include "../c/encoding/BinaryXMLInterest.h" |
| 10 | #include "../Interest.hpp" |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 11 | #include "BinaryXMLEncoder.hpp" |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 12 | #include "../c/encoding/BinaryXMLDecoder.h" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 13 | #include "BinaryXMLWireFormat.hpp" |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 14 | |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 15 | using namespace std; |
| 16 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 17 | namespace ndn { |
| 18 | |
| 19 | BinaryXMLWireFormat BinaryXMLWireFormat::instance_; |
| 20 | |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 21 | void 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 Thompson | 58d798f | 2013-07-02 14:16:25 -0700 | [diff] [blame] | 30 | |
| 31 | encoder.appendTo(output); |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 34 | void 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 Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 40 | struct ndn_BinaryXMLDecoder decoder; |
| 41 | ndn_BinaryXMLDecoder_init(&decoder, (unsigned char *)input, inputLength); |
| 42 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 43 | char *error; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 44 | if (error = ndn_decodeBinaryXMLName(&nameStruct, &decoder)) |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 45 | throw std::runtime_error(error); |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 46 | |
| 47 | name.set(nameStruct); |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 50 | void 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 Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 56 | struct ndn_BinaryXMLDecoder decoder; |
| 57 | ndn_BinaryXMLDecoder_init(&decoder, (unsigned char *)input, inputLength); |
| 58 | |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 59 | char *error; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 60 | if (error = ndn_decodeBinaryXMLInterest(&interestStruct, &decoder)) |
Jeff Thompson | 7c30eda | 2013-07-03 18:37:07 -0700 | [diff] [blame] | 61 | throw std::runtime_error(error); |
| 62 | |
| 63 | interest.set(interestStruct); |
| 64 | } |
| 65 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 66 | } |