blob: db60f80c20abe6c9ae9e92bf9a017fdd8078918b [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
Jeff Thompson25b4e612013-10-10 16:03:24 -07008#include <ndn-cpp/common.hpp>
9#include <ndn-cpp/data.hpp>
10#include <ndn-cpp/sha256-with-rsa-signature.hpp>
11#include "c/data.h"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070012
13using namespace std;
14
15namespace ndn {
16
Jeff Thompson20af0732013-09-12 17:01:45 -070017Signature::~Signature()
Jeff Thompson5cae5e52013-07-10 19:41:20 -070018{
Jeff Thompson20af0732013-09-12 17:01:45 -070019}
Jeff Thompsonf4585af2013-09-11 14:56:59 -070020
Jeff Thompson0050abe2013-09-17 12:50:25 -070021void
22MetaInfo::get(struct ndn_MetaInfo& metaInfoStruct) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -070023{
Jeff Thompsonfec716d2013-09-11 13:54:36 -070024 metaInfoStruct.timestampMilliseconds = timestampMilliseconds_;
25 metaInfoStruct.type = type_;
26 metaInfoStruct.freshnessSeconds = freshnessSeconds_;
Jeff Thompson145e2252013-09-12 12:51:35 -070027 finalBlockID_.get(metaInfoStruct.finalBlockID);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070028}
29
Jeff Thompson0050abe2013-09-17 12:50:25 -070030void
31MetaInfo::set(const struct ndn_MetaInfo& metaInfoStruct)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070032{
Jeff Thompsonfec716d2013-09-11 13:54:36 -070033 timestampMilliseconds_ = metaInfoStruct.timestampMilliseconds;
34 type_ = metaInfoStruct.type;
35 freshnessSeconds_ = metaInfoStruct.freshnessSeconds;
Jeff Thompson93034532013-10-08 11:52:43 -070036 finalBlockID_ = Name::Component(Blob(metaInfoStruct.finalBlockID.value));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070037}
38
Jeff Thompson20af0732013-09-12 17:01:45 -070039Data::Data()
40: signature_(new Sha256WithRsaSignature())
41{
42}
43
44Data::Data(const Name& name)
45: name_(name), signature_(new Sha256WithRsaSignature())
46{
47}
48
Jeff Thompson25bfdca2013-10-16 17:05:41 -070049Data::Data(const Data& data)
Jeff Thompsonf1ffba82013-10-19 17:57:12 -070050: name_(data.name_), metaInfo_(data.metaInfo_), content_(data.content_), defaultWireEncoding_(data.defaultWireEncoding_)
Jeff Thompson25bfdca2013-10-16 17:05:41 -070051{
52 if (data.signature_)
53 signature_ = data.signature_->clone();
54}
55
56Data::~Data()
57{
58}
59
60Data& Data::operator=(const Data& data)
61{
62 if (data.signature_)
63 signature_ = data.signature_->clone();
64 else
Jeff Thompsonce115762013-12-18 14:59:56 -080065 signature_ = ptr_lib::shared_ptr<Signature>();
Jeff Thompson25bfdca2013-10-16 17:05:41 -070066
67 name_ = data.name_;
68 metaInfo_ = data.metaInfo_;
69 content_ = data.content_;
Jeff Thompsonf1ffba82013-10-19 17:57:12 -070070 defaultWireEncoding_ = data.defaultWireEncoding_;
Jeff Thompson25bfdca2013-10-16 17:05:41 -070071
72 return *this;
73}
74
Jeff Thompson0050abe2013-09-17 12:50:25 -070075void
76Data::get(struct ndn_Data& dataStruct) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -070077{
Jeff Thompson20af0732013-09-12 17:01:45 -070078 signature_->get(dataStruct.signature);
Jeff Thompson56ec9e22013-08-02 11:34:07 -070079 name_.get(dataStruct.name);
Jeff Thompsonfec716d2013-09-11 13:54:36 -070080 metaInfo_.get(dataStruct.metaInfo);
Jeff Thompson93034532013-10-08 11:52:43 -070081 content_.get(dataStruct.content);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070082}
83
Jeff Thompson0050abe2013-09-17 12:50:25 -070084void
85Data::set(const struct ndn_Data& dataStruct)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070086{
Jeff Thompson20af0732013-09-12 17:01:45 -070087 signature_->set(dataStruct.signature);
Jeff Thompson56ec9e22013-08-02 11:34:07 -070088 name_.set(dataStruct.name);
Jeff Thompsonfec716d2013-09-11 13:54:36 -070089 metaInfo_.set(dataStruct.metaInfo);
Jeff Thompson93034532013-10-08 11:52:43 -070090 content_ = Blob(dataStruct.content);
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070091
92 onChanged();
93}
94
Jeff Thompson6d591972013-10-17 11:16:32 -070095Data&
96Data::setName(const Name& name)
97{
98 name_ = name;
99 onChanged();
100 return *this;
101}
102
Jeff Thompson0050abe2013-09-17 12:50:25 -0700103SignedBlob
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700104Data::wireEncode(WireFormat& wireFormat) const
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700105{
Jeff Thompson97223af2013-09-24 17:01:27 -0700106 size_t signedPortionBeginOffset, signedPortionEndOffset;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700107 Blob encoding = wireFormat.encodeData(*this, &signedPortionBeginOffset, &signedPortionEndOffset);
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700108 SignedBlob wireEncoding = SignedBlob(encoding, signedPortionBeginOffset, signedPortionEndOffset);
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700109
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700110 if (&wireFormat == WireFormat::getDefaultWireFormat())
111 // This is the default wire encoding.
112 const_cast<Data*>(this)->defaultWireEncoding_ = wireEncoding;
113
114 return wireEncoding;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700115}
116
Jeff Thompson0050abe2013-09-17 12:50:25 -0700117void
Jeff Thompson97223af2013-09-24 17:01:27 -0700118Data::wireDecode(const uint8_t* input, size_t inputLength, WireFormat& wireFormat)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700119{
Jeff Thompson97223af2013-09-24 17:01:27 -0700120 size_t signedPortionBeginOffset, signedPortionEndOffset;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700121 wireFormat.decodeData(*this, input, inputLength, &signedPortionBeginOffset, &signedPortionEndOffset);
122
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700123 if (&wireFormat == WireFormat::getDefaultWireFormat())
124 // This is the default wire encoding.
125 defaultWireEncoding_ = SignedBlob(input, inputLength, signedPortionBeginOffset, signedPortionEndOffset);
126 else
127 defaultWireEncoding_ = SignedBlob();
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700128}
129
Jeff Thompson0050abe2013-09-17 12:50:25 -0700130void
131Data::onChanged()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700132{
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700133 // The values have changed, so the default wire encoding is invalidated.
134 defaultWireEncoding_ = SignedBlob();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700135}
136
137}