Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 8 | #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 Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 12 | |
| 13 | using namespace std; |
| 14 | |
| 15 | namespace ndn { |
| 16 | |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 17 | Signature::~Signature() |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 18 | { |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 19 | } |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 20 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 21 | void |
| 22 | MetaInfo::get(struct ndn_MetaInfo& metaInfoStruct) const |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 23 | { |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 24 | metaInfoStruct.timestampMilliseconds = timestampMilliseconds_; |
| 25 | metaInfoStruct.type = type_; |
| 26 | metaInfoStruct.freshnessSeconds = freshnessSeconds_; |
Jeff Thompson | 145e225 | 2013-09-12 12:51:35 -0700 | [diff] [blame] | 27 | finalBlockID_.get(metaInfoStruct.finalBlockID); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 30 | void |
| 31 | MetaInfo::set(const struct ndn_MetaInfo& metaInfoStruct) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 32 | { |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 33 | timestampMilliseconds_ = metaInfoStruct.timestampMilliseconds; |
| 34 | type_ = metaInfoStruct.type; |
| 35 | freshnessSeconds_ = metaInfoStruct.freshnessSeconds; |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 36 | finalBlockID_ = Name::Component(Blob(metaInfoStruct.finalBlockID.value)); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 39 | Data::Data() |
| 40 | : signature_(new Sha256WithRsaSignature()) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | Data::Data(const Name& name) |
| 45 | : name_(name), signature_(new Sha256WithRsaSignature()) |
| 46 | { |
| 47 | } |
| 48 | |
Jeff Thompson | 25bfdca | 2013-10-16 17:05:41 -0700 | [diff] [blame] | 49 | Data::Data(const Data& data) |
Jeff Thompson | f1ffba8 | 2013-10-19 17:57:12 -0700 | [diff] [blame] | 50 | : name_(data.name_), metaInfo_(data.metaInfo_), content_(data.content_), defaultWireEncoding_(data.defaultWireEncoding_) |
Jeff Thompson | 25bfdca | 2013-10-16 17:05:41 -0700 | [diff] [blame] | 51 | { |
| 52 | if (data.signature_) |
| 53 | signature_ = data.signature_->clone(); |
| 54 | } |
| 55 | |
| 56 | Data::~Data() |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | Data& Data::operator=(const Data& data) |
| 61 | { |
| 62 | if (data.signature_) |
| 63 | signature_ = data.signature_->clone(); |
| 64 | else |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 65 | signature_ = ptr_lib::shared_ptr<Signature>(); |
Jeff Thompson | 25bfdca | 2013-10-16 17:05:41 -0700 | [diff] [blame] | 66 | |
| 67 | name_ = data.name_; |
| 68 | metaInfo_ = data.metaInfo_; |
| 69 | content_ = data.content_; |
Jeff Thompson | f1ffba8 | 2013-10-19 17:57:12 -0700 | [diff] [blame] | 70 | defaultWireEncoding_ = data.defaultWireEncoding_; |
Jeff Thompson | 25bfdca | 2013-10-16 17:05:41 -0700 | [diff] [blame] | 71 | |
| 72 | return *this; |
| 73 | } |
| 74 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 75 | void |
| 76 | Data::get(struct ndn_Data& dataStruct) const |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 77 | { |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 78 | signature_->get(dataStruct.signature); |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 79 | name_.get(dataStruct.name); |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 80 | metaInfo_.get(dataStruct.metaInfo); |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 81 | content_.get(dataStruct.content); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 84 | void |
| 85 | Data::set(const struct ndn_Data& dataStruct) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 86 | { |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 87 | signature_->set(dataStruct.signature); |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 88 | name_.set(dataStruct.name); |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 89 | metaInfo_.set(dataStruct.metaInfo); |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 90 | content_ = Blob(dataStruct.content); |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 91 | |
| 92 | onChanged(); |
| 93 | } |
| 94 | |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 95 | Data& |
| 96 | Data::setName(const Name& name) |
| 97 | { |
| 98 | name_ = name; |
| 99 | onChanged(); |
| 100 | return *this; |
| 101 | } |
| 102 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 103 | SignedBlob |
Jeff Thompson | f1ffba8 | 2013-10-19 17:57:12 -0700 | [diff] [blame] | 104 | Data::wireEncode(WireFormat& wireFormat) const |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 105 | { |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 106 | size_t signedPortionBeginOffset, signedPortionEndOffset; |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 107 | Blob encoding = wireFormat.encodeData(*this, &signedPortionBeginOffset, &signedPortionEndOffset); |
Jeff Thompson | f1ffba8 | 2013-10-19 17:57:12 -0700 | [diff] [blame] | 108 | SignedBlob wireEncoding = SignedBlob(encoding, signedPortionBeginOffset, signedPortionEndOffset); |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 109 | |
Jeff Thompson | f1ffba8 | 2013-10-19 17:57:12 -0700 | [diff] [blame] | 110 | if (&wireFormat == WireFormat::getDefaultWireFormat()) |
| 111 | // This is the default wire encoding. |
| 112 | const_cast<Data*>(this)->defaultWireEncoding_ = wireEncoding; |
| 113 | |
| 114 | return wireEncoding; |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 117 | void |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 118 | Data::wireDecode(const uint8_t* input, size_t inputLength, WireFormat& wireFormat) |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 119 | { |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 120 | size_t signedPortionBeginOffset, signedPortionEndOffset; |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 121 | wireFormat.decodeData(*this, input, inputLength, &signedPortionBeginOffset, &signedPortionEndOffset); |
| 122 | |
Jeff Thompson | f1ffba8 | 2013-10-19 17:57:12 -0700 | [diff] [blame] | 123 | if (&wireFormat == WireFormat::getDefaultWireFormat()) |
| 124 | // This is the default wire encoding. |
| 125 | defaultWireEncoding_ = SignedBlob(input, inputLength, signedPortionBeginOffset, signedPortionEndOffset); |
| 126 | else |
| 127 | defaultWireEncoding_ = SignedBlob(); |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 130 | void |
| 131 | Data::onChanged() |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 132 | { |
Jeff Thompson | f1ffba8 | 2013-10-19 17:57:12 -0700 | [diff] [blame] | 133 | // The values have changed, so the default wire encoding is invalidated. |
| 134 | defaultWireEncoding_ = SignedBlob(); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | } |