blob: 00a915ce5ce55fb436a197cdc7ce214ad4cd7713 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson5cae5e52013-07-10 19:41:20 -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 Thompson5cae5e52013-07-10 19:41:20 -07005 * See COPYING for copyright and distribution information.
6 */
7
Alexander Afanasyev09c613f2014-01-29 00:23:58 -08008#include "common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08009
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080010#include "data.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070011
12using namespace std;
13
14namespace ndn {
15
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080016const Block&
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080017Data::wireEncode() const
Jeff Thompson5cae5e52013-07-10 19:41:20 -070018{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080019 if (wire_.hasWire())
20 return wire_;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080021
Alexander Afanasyev277f4692014-01-03 15:29:55 -080022 // Data ::= DATA-TLV TLV-LENGTH
23 // Name
24 // MetaInfo
25 // Content
26 // Signature
27
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080028 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 Afanasyeve0c02f52013-12-28 20:44:25 -080037 wire_.push_back(getContent());
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080038
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 Afanasyevfadc97d2014-01-03 13:22:10 -080061void
62Data::wireDecode(const Block &wire)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070063{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080064 wire_ = wire;
65 wire_.parse();
66
Alexander Afanasyev277f4692014-01-03 15:29:55 -080067 // Data ::= DATA-TLV TLV-LENGTH
68 // Name
69 // MetaInfo
70 // Content
71 // Signature
72
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080073 // 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 Thompson5cae5e52013-07-10 19:41:20 -070091}
92
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -080093std::ostream&
94operator << (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 Thompson5cae5e52013-07-10 19:41:20 -0700106
107}