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 | 5cae5e5 | 2013-07-10 19:41:20 -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 | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 8 | #include "common.hpp" |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 9 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 10 | #include "data.hpp" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 11 | |
| 12 | using namespace std; |
| 13 | |
| 14 | namespace ndn { |
| 15 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 16 | const Block& |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 17 | Data::wireEncode() const |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 18 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 19 | if (wire_.hasWire()) |
| 20 | return wire_; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | 277f469 | 2014-01-03 15:29:55 -0800 | [diff] [blame] | 22 | // Data ::= DATA-TLV TLV-LENGTH |
| 23 | // Name |
| 24 | // MetaInfo |
| 25 | // Content |
| 26 | // Signature |
| 27 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 28 | wire_ = Block(Tlv::Data); |
| 29 | |
| 30 | // Name |
| 31 | wire_.push_back(getName().wireEncode()); |
| 32 | |
| 33 | // MetaInfo |
| 34 | wire_.push_back(getMetaInfo().wireEncode()); |
| 35 | |
| 36 | // Content |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 37 | wire_.push_back(getContent()); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 38 | |
| 39 | if (!signature_) { |
| 40 | throw Error("Requested wire format, but data packet has not been signed yet"); |
| 41 | } |
| 42 | |
| 43 | /////////////// |
| 44 | // Signature // |
| 45 | /////////////// |
| 46 | |
| 47 | // SignatureInfo |
| 48 | wire_.push_back(signature_.getInfo()); |
| 49 | |
| 50 | // SignatureValue |
| 51 | wire_.push_back(signature_.getValue()); |
| 52 | |
| 53 | wire_.encode(); |
| 54 | return wire_; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Decode the input using a particular wire format and update this Data. |
| 59 | * @param input The input byte array to be decoded. |
| 60 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 61 | void |
| 62 | Data::wireDecode(const Block &wire) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 63 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 64 | wire_ = wire; |
| 65 | wire_.parse(); |
| 66 | |
Alexander Afanasyev | 277f469 | 2014-01-03 15:29:55 -0800 | [diff] [blame] | 67 | // Data ::= DATA-TLV TLV-LENGTH |
| 68 | // Name |
| 69 | // MetaInfo |
| 70 | // Content |
| 71 | // Signature |
| 72 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 73 | // Name |
| 74 | name_.wireDecode(wire_.get(Tlv::Name)); |
| 75 | |
| 76 | // MetaInfo |
| 77 | metaInfo_.wireDecode(wire_.get(Tlv::MetaInfo)); |
| 78 | |
| 79 | // Content |
| 80 | content_ = wire_.get(Tlv::Content); |
| 81 | |
| 82 | /////////////// |
| 83 | // Signature // |
| 84 | /////////////// |
| 85 | |
| 86 | // SignatureInfo |
| 87 | signature_.setInfo(wire_.get(Tlv::SignatureInfo)); |
| 88 | |
| 89 | // SignatureValue |
| 90 | signature_.setValue(wire_.get(Tlv::SignatureValue)); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 93 | std::ostream& |
| 94 | operator << (std::ostream &os, const Data &data) |
| 95 | { |
| 96 | os << "Name: " << data.getName() << "\n"; |
| 97 | os << "MetaInfo: " << data.getMetaInfo() << "\n"; |
| 98 | os << "Content: (size: " << data.getContent().value_size() << ")\n"; |
| 99 | os << "Signature: (type: " << data.getSignature().getType() << |
| 100 | ", value_length: "<< data.getSignature().getValue().value_size() << ")"; |
| 101 | os << std::endl; |
| 102 | |
| 103 | return os; |
| 104 | } |
| 105 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 106 | |
| 107 | } |