blob: ea726c1b3d1736ed897ac5e9c85118e1892864c5 [file] [log] [blame]
Jeff Thompson5cae5e52013-07-10 19:41:20 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson5cae5e52013-07-10 19:41:20 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#include "common.hpp"
Jeff Thompson56ec9e22013-08-02 11:34:07 -07008#include "data.hpp"
Jeff Thompson20af0732013-09-12 17:01:45 -07009#include "sha256-with-rsa-signature.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070010
11using namespace std;
12
13namespace ndn {
14
Jeff Thompson20af0732013-09-12 17:01:45 -070015Signature::~Signature()
Jeff Thompson5cae5e52013-07-10 19:41:20 -070016{
Jeff Thompson20af0732013-09-12 17:01:45 -070017}
Jeff Thompsonf4585af2013-09-11 14:56:59 -070018
Jeff Thompsonfec716d2013-09-11 13:54:36 -070019void MetaInfo::get(struct ndn_MetaInfo& metaInfoStruct) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -070020{
Jeff Thompsonfec716d2013-09-11 13:54:36 -070021 metaInfoStruct.timestampMilliseconds = timestampMilliseconds_;
22 metaInfoStruct.type = type_;
23 metaInfoStruct.freshnessSeconds = freshnessSeconds_;
Jeff Thompson145e2252013-09-12 12:51:35 -070024 finalBlockID_.get(metaInfoStruct.finalBlockID);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070025}
26
Jeff Thompsonfec716d2013-09-11 13:54:36 -070027void MetaInfo::set(const struct ndn_MetaInfo& metaInfoStruct)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070028{
Jeff Thompsonfec716d2013-09-11 13:54:36 -070029 timestampMilliseconds_ = metaInfoStruct.timestampMilliseconds;
30 type_ = metaInfoStruct.type;
31 freshnessSeconds_ = metaInfoStruct.freshnessSeconds;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070032 finalBlockID_ = Name::Component(Blob(metaInfoStruct.finalBlockID.value, metaInfoStruct.finalBlockID.valueLength));
Jeff Thompson5cae5e52013-07-10 19:41:20 -070033}
34
Jeff Thompson20af0732013-09-12 17:01:45 -070035Data::Data()
36: signature_(new Sha256WithRsaSignature())
37{
38}
39
40Data::Data(const Name& name)
41: name_(name), signature_(new Sha256WithRsaSignature())
42{
43}
44
Jeff Thompson1656e6a2013-08-29 18:01:48 -070045void Data::get(struct ndn_Data& dataStruct) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -070046{
Jeff Thompson20af0732013-09-12 17:01:45 -070047 signature_->get(dataStruct.signature);
Jeff Thompson56ec9e22013-08-02 11:34:07 -070048 name_.get(dataStruct.name);
Jeff Thompsonfec716d2013-09-11 13:54:36 -070049 metaInfo_.get(dataStruct.metaInfo);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070050
Jeff Thompson56ec9e22013-08-02 11:34:07 -070051 dataStruct.contentLength = content_.size();
Jeff Thompson5cae5e52013-07-10 19:41:20 -070052 if (content_.size() > 0)
Jeff Thompson0899c0f2013-09-12 12:15:31 -070053 dataStruct.content = (unsigned char*)content_.buf();
Jeff Thompson5cae5e52013-07-10 19:41:20 -070054 else
Jeff Thompson56ec9e22013-08-02 11:34:07 -070055 dataStruct.content = 0;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070056}
57
Jeff Thompson1656e6a2013-08-29 18:01:48 -070058void Data::set(const struct ndn_Data& dataStruct)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070059{
Jeff Thompson20af0732013-09-12 17:01:45 -070060 signature_->set(dataStruct.signature);
Jeff Thompson56ec9e22013-08-02 11:34:07 -070061 name_.set(dataStruct.name);
Jeff Thompsonfec716d2013-09-11 13:54:36 -070062 metaInfo_.set(dataStruct.metaInfo);
Jeff Thompson0899c0f2013-09-12 12:15:31 -070063 content_ = Blob(dataStruct.content, dataStruct.contentLength);
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070064
65 onChanged();
66}
67
68SignedBlob Data::wireEncode(WireFormat& wireFormat)
69{
70 unsigned int signedPortionBeginOffset, signedPortionEndOffset;
71 Blob encoding = wireFormat.encodeData(*this, &signedPortionBeginOffset, &signedPortionEndOffset);
72
73 wireEncoding_ = SignedBlob(encoding, signedPortionBeginOffset, signedPortionEndOffset);
74 return wireEncoding_;
75}
76
Jeff Thompson3352fc22013-09-17 12:07:14 -070077void Data::wireDecode(const unsigned char* input, unsigned int inputLength, WireFormat& wireFormat)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070078{
79 unsigned int signedPortionBeginOffset, signedPortionEndOffset;
80 wireFormat.decodeData(*this, input, inputLength, &signedPortionBeginOffset, &signedPortionEndOffset);
81
82 wireEncoding_ = SignedBlob(input, inputLength, signedPortionBeginOffset, signedPortionEndOffset);
83}
84
85void Data::onChanged()
86{
87 wireEncoding_ = SignedBlob();
Jeff Thompson5cae5e52013-07-10 19:41:20 -070088}
89
90}