blob: c9f21fa84e068eda6997ec41d33d9fd99bd7a6f1 [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;
Jeff Thompson25bfdca2013-10-16 17:05:41 -070014using namespace ndn::ptr_lib;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070015
16namespace ndn {
17
Jeff Thompson20af0732013-09-12 17:01:45 -070018Signature::~Signature()
Jeff Thompson5cae5e52013-07-10 19:41:20 -070019{
Jeff Thompson20af0732013-09-12 17:01:45 -070020}
Jeff Thompsonf4585af2013-09-11 14:56:59 -070021
Jeff Thompson0050abe2013-09-17 12:50:25 -070022void
23MetaInfo::get(struct ndn_MetaInfo& metaInfoStruct) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -070024{
Jeff Thompsonfec716d2013-09-11 13:54:36 -070025 metaInfoStruct.timestampMilliseconds = timestampMilliseconds_;
26 metaInfoStruct.type = type_;
27 metaInfoStruct.freshnessSeconds = freshnessSeconds_;
Jeff Thompson145e2252013-09-12 12:51:35 -070028 finalBlockID_.get(metaInfoStruct.finalBlockID);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070029}
30
Jeff Thompson0050abe2013-09-17 12:50:25 -070031void
32MetaInfo::set(const struct ndn_MetaInfo& metaInfoStruct)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070033{
Jeff Thompsonfec716d2013-09-11 13:54:36 -070034 timestampMilliseconds_ = metaInfoStruct.timestampMilliseconds;
35 type_ = metaInfoStruct.type;
36 freshnessSeconds_ = metaInfoStruct.freshnessSeconds;
Jeff Thompson93034532013-10-08 11:52:43 -070037 finalBlockID_ = Name::Component(Blob(metaInfoStruct.finalBlockID.value));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070038}
39
Jeff Thompson20af0732013-09-12 17:01:45 -070040Data::Data()
41: signature_(new Sha256WithRsaSignature())
42{
43}
44
45Data::Data(const Name& name)
46: name_(name), signature_(new Sha256WithRsaSignature())
47{
48}
49
Jeff Thompson25bfdca2013-10-16 17:05:41 -070050Data::Data(const Data& data)
Jeff Thompsonf1ffba82013-10-19 17:57:12 -070051: name_(data.name_), metaInfo_(data.metaInfo_), content_(data.content_), defaultWireEncoding_(data.defaultWireEncoding_)
Jeff Thompson25bfdca2013-10-16 17:05:41 -070052{
53 if (data.signature_)
54 signature_ = data.signature_->clone();
55}
56
57Data::~Data()
58{
59}
60
61Data& Data::operator=(const Data& data)
62{
63 if (data.signature_)
64 signature_ = data.signature_->clone();
65 else
66 signature_ = shared_ptr<Signature>();
67
68 name_ = data.name_;
69 metaInfo_ = data.metaInfo_;
70 content_ = data.content_;
Jeff Thompsonf1ffba82013-10-19 17:57:12 -070071 defaultWireEncoding_ = data.defaultWireEncoding_;
Jeff Thompson25bfdca2013-10-16 17:05:41 -070072
73 return *this;
74}
75
Jeff Thompson0050abe2013-09-17 12:50:25 -070076void
77Data::get(struct ndn_Data& dataStruct) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -070078{
Jeff Thompson20af0732013-09-12 17:01:45 -070079 signature_->get(dataStruct.signature);
Jeff Thompson56ec9e22013-08-02 11:34:07 -070080 name_.get(dataStruct.name);
Jeff Thompsonfec716d2013-09-11 13:54:36 -070081 metaInfo_.get(dataStruct.metaInfo);
Jeff Thompson93034532013-10-08 11:52:43 -070082 content_.get(dataStruct.content);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070083}
84
Jeff Thompson0050abe2013-09-17 12:50:25 -070085void
86Data::set(const struct ndn_Data& dataStruct)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070087{
Jeff Thompson20af0732013-09-12 17:01:45 -070088 signature_->set(dataStruct.signature);
Jeff Thompson56ec9e22013-08-02 11:34:07 -070089 name_.set(dataStruct.name);
Jeff Thompsonfec716d2013-09-11 13:54:36 -070090 metaInfo_.set(dataStruct.metaInfo);
Jeff Thompson93034532013-10-08 11:52:43 -070091 content_ = Blob(dataStruct.content);
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070092
93 onChanged();
94}
95
Jeff Thompson6d591972013-10-17 11:16:32 -070096Data&
97Data::setName(const Name& name)
98{
99 name_ = name;
100 onChanged();
101 return *this;
102}
103
Jeff Thompson0050abe2013-09-17 12:50:25 -0700104SignedBlob
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700105Data::wireEncode(WireFormat& wireFormat) const
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700106{
Jeff Thompson97223af2013-09-24 17:01:27 -0700107 size_t signedPortionBeginOffset, signedPortionEndOffset;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700108 Blob encoding = wireFormat.encodeData(*this, &signedPortionBeginOffset, &signedPortionEndOffset);
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700109 SignedBlob wireEncoding = SignedBlob(encoding, signedPortionBeginOffset, signedPortionEndOffset);
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700110
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700111 if (&wireFormat == WireFormat::getDefaultWireFormat())
112 // This is the default wire encoding.
113 const_cast<Data*>(this)->defaultWireEncoding_ = wireEncoding;
114
115 return wireEncoding;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700116}
117
Jeff Thompson0050abe2013-09-17 12:50:25 -0700118void
Jeff Thompson97223af2013-09-24 17:01:27 -0700119Data::wireDecode(const uint8_t* input, size_t inputLength, WireFormat& wireFormat)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700120{
Jeff Thompson97223af2013-09-24 17:01:27 -0700121 size_t signedPortionBeginOffset, signedPortionEndOffset;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700122 wireFormat.decodeData(*this, input, inputLength, &signedPortionBeginOffset, &signedPortionEndOffset);
123
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700124 if (&wireFormat == WireFormat::getDefaultWireFormat())
125 // This is the default wire encoding.
126 defaultWireEncoding_ = SignedBlob(input, inputLength, signedPortionBeginOffset, signedPortionEndOffset);
127 else
128 defaultWireEncoding_ = SignedBlob();
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700129}
130
Jeff Thompson0050abe2013-09-17 12:50:25 -0700131void
132Data::onChanged()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700133{
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700134 // The values have changed, so the default wire encoding is invalidated.
135 defaultWireEncoding_ = SignedBlob();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700136}
137
138}