blob: 62cdf3b2b57c80cc24540f43a9f6ee906a429d53 [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)
51: name_(data.name_), metaInfo_(data.metaInfo_), content_(data.content_), wireEncoding_(data.wireEncoding_)
52{
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_;
71 wireEncoding_ = data.wireEncoding_;
72
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 Thompson0050abe2013-09-17 12:50:25 -070096SignedBlob
97Data::wireEncode(WireFormat& wireFormat)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070098{
Jeff Thompson97223af2013-09-24 17:01:27 -070099 size_t signedPortionBeginOffset, signedPortionEndOffset;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700100 Blob encoding = wireFormat.encodeData(*this, &signedPortionBeginOffset, &signedPortionEndOffset);
101
102 wireEncoding_ = SignedBlob(encoding, signedPortionBeginOffset, signedPortionEndOffset);
103 return wireEncoding_;
104}
105
Jeff Thompson0050abe2013-09-17 12:50:25 -0700106void
Jeff Thompson97223af2013-09-24 17:01:27 -0700107Data::wireDecode(const uint8_t* input, size_t inputLength, WireFormat& wireFormat)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700108{
Jeff Thompson97223af2013-09-24 17:01:27 -0700109 size_t signedPortionBeginOffset, signedPortionEndOffset;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700110 wireFormat.decodeData(*this, input, inputLength, &signedPortionBeginOffset, &signedPortionEndOffset);
111
112 wireEncoding_ = SignedBlob(input, inputLength, signedPortionBeginOffset, signedPortionEndOffset);
113}
114
Jeff Thompson0050abe2013-09-17 12:50:25 -0700115void
116Data::onChanged()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700117{
118 wireEncoding_ = SignedBlob();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700119}
120
121}