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 | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 9 | #include "../Name.hpp" |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame^] | 10 | #include "BinaryXMLEncoder.hpp" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 11 | #include "BinaryXMLWireFormat.hpp" |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 12 | |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame^] | 13 | using namespace std; |
| 14 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 15 | namespace ndn { |
| 16 | |
| 17 | BinaryXMLWireFormat BinaryXMLWireFormat::instance_; |
| 18 | |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame^] | 19 | void BinaryXMLWireFormat::encodeName(Name &name, vector<unsigned char> &output) |
| 20 | { |
| 21 | struct ndn_Name nameStruct; |
| 22 | struct ndn_NameComponent components[100]; |
| 23 | ndn_Name_init(&nameStruct, components, sizeof(components) / sizeof(components[0])); |
| 24 | name.get(nameStruct); |
| 25 | |
| 26 | BinaryXMLEncoder encoder; |
| 27 | ndn_encodeBinaryXMLName(&nameStruct, encoder.getEncoder()); |
| 28 | |
| 29 | output = vector<unsigned char>(encoder.getEncoder()->output.array, encoder.getEncoder()->output.array + encoder.getEncoder()->offset); |
| 30 | } |
| 31 | |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 32 | void BinaryXMLWireFormat::decodeName(Name &name, const unsigned char *input, unsigned int inputLength) |
| 33 | { |
| 34 | struct ndn_NameComponent components[100]; |
| 35 | struct ndn_Name nameStruct; |
| 36 | ndn_Name_init(&nameStruct, components, sizeof(components) / sizeof(components[0])); |
| 37 | |
| 38 | char *error; |
| 39 | if (error = ndn_decodeBinaryXMLName(&nameStruct, (unsigned char *)input, inputLength)) |
| 40 | throw std::runtime_error(error); |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame^] | 41 | |
| 42 | name.set(nameStruct); |
Jeff Thompson | 4c89ad6 | 2013-06-28 12:50:13 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | } |