blob: 2bcc6aa4957c684abfc63b48945e6115d2012be4 [file] [log] [blame]
Jeff Thompson5cae5e52013-07-10 19:41:20 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
Jeff Thompson56ec9e22013-08-02 11:34:07 -07006#ifndef NDN_DATA_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -07007#define NDN_DATA_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -07008
Jeff Thompson46bd45f2013-08-08 16:46:41 -07009#include "common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "name.hpp"
11#include "publisher-public-key-digest.hpp"
12#include "key.hpp"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070013#include "c/data.h"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070014
15namespace ndn {
16
17class Signature {
18public:
19 /**
20 * Set the signatureStruct to point to the values in this signature object, without copying any memory.
21 * WARNING: The resulting pointers in signatureStruct are invalid after a further use of this object which could reallocate memory.
22 * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated.
23 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070024 void get(struct ndn_Signature& signatureStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070025
26 /**
27 * Clear this signature, and set the values by copying from the ndn_Signature struct.
28 * @param signatureStruct a C ndn_Signature struct
29 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070030 void set(const struct ndn_Signature& signatureStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070031
Jeff Thompson1656e6a2013-08-29 18:01:48 -070032 const std::vector<unsigned char>& getDigestAlgorithm() const { return digestAlgorithm_; }
33 std::vector<unsigned char>& getDigestAlgorithm() { return digestAlgorithm_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070034
Jeff Thompson1656e6a2013-08-29 18:01:48 -070035 const std::vector<unsigned char>& getWitness() const { return witness_; }
36 std::vector<unsigned char>& getWitness() { return witness_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070037
Jeff Thompson1656e6a2013-08-29 18:01:48 -070038 const std::vector<unsigned char>& getSignature() const { return signature_; }
39 std::vector<unsigned char>& getSignature() { return signature_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070040
Jeff Thompson1656e6a2013-08-29 18:01:48 -070041 void setDigestAlgorithm(const std::vector<unsigned char>& digestAlgorithm) { digestAlgorithm_ = digestAlgorithm; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070042 void setDigestAlgorithm(const unsigned char *digestAlgorithm, unsigned int digestAlgorithmLength)
43 {
44 setVector(digestAlgorithm_, digestAlgorithm, digestAlgorithmLength);
45 }
46
Jeff Thompson1656e6a2013-08-29 18:01:48 -070047 void setWitness(const std::vector<unsigned char>& witness) { witness_ = witness; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070048 void setWitness(const unsigned char *witness, unsigned int witnessLength)
49 {
50 setVector(witness_, witness, witnessLength);
51 }
52
Jeff Thompson1656e6a2013-08-29 18:01:48 -070053 void setSignature(const std::vector<unsigned char>& signature) { signature_ = signature; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070054 void setSignature(const unsigned char *signature, unsigned int signatureLength)
55 {
56 setVector(signature_, signature, signatureLength);
57 }
Jeff Thompsonf842d212013-08-12 11:05:28 -070058
59 /**
60 * Clear the digest algorithm, witness and signature fields.
61 */
62 void clear()
63 {
64 digestAlgorithm_.clear();
65 witness_.clear();
66 signature_.clear();
67 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070068
Jeff Thompson5cae5e52013-07-10 19:41:20 -070069private:
70 std::vector<unsigned char> digestAlgorithm_; /**< if empty, the default is 2.16.840.1.101.3.4.2.1 (sha-256) */
71 std::vector<unsigned char> witness_;
72 std::vector<unsigned char> signature_;
73};
74
Jeff Thompsonfec716d2013-09-11 13:54:36 -070075class MetaInfo {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070076public:
Jeff Thompsonfec716d2013-09-11 13:54:36 -070077 MetaInfo()
Jeff Thompson5cae5e52013-07-10 19:41:20 -070078 {
79 type_ = ndn_ContentType_DATA;
80 freshnessSeconds_ = -1;
81 }
82
83 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070084 * Set the metaInfoStruct to point to the values in this meta info object, without copying any memory.
85 * WARNING: The resulting pointers in metaInfoStruct are invalid after a further use of this object which could reallocate memory.
86 * @param metaInfoStruct a C ndn_MetaInfo struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070087 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070088 void get(struct ndn_MetaInfo& metaInfoStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070089
90 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070091 * Clear this meta info, and set the values by copying from the ndn_MetaInfo struct.
92 * @param metaInfoStruct a C ndn_MetaInfo struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -070093 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070094 void set(const struct ndn_MetaInfo& metaInfoStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070095
Jeff Thompson1656e6a2013-08-29 18:01:48 -070096 const PublisherPublicKeyDigest& getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
97 PublisherPublicKeyDigest& getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070098
Jeff Thompson210b92f2013-07-11 15:16:03 -070099 double getTimestampMilliseconds() const { return timestampMilliseconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700100
Jeff Thompsond8776352013-08-16 18:09:30 -0700101 ndn_ContentType getType() const { return type_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700102
103 int getFreshnessSeconds() const { return freshnessSeconds_; }
104
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700105 const std::vector<unsigned char>& getFinalBlockID() const { return finalBlockID_; }
106 std::vector<unsigned char>& getFinalBlockID() { return finalBlockID_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700107
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700108 const KeyLocator& getKeyLocator() const { return keyLocator_; }
109 KeyLocator& getKeyLocator() { return keyLocator_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700110
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700111 void setPublisherPublicKeyDigest(const PublisherPublicKeyDigest& publisherPublicKeyDigest) { publisherPublicKeyDigest_ = publisherPublicKeyDigest; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700112
113 void setTimestampMilliseconds(double timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; }
114
Jeff Thompsond8776352013-08-16 18:09:30 -0700115 void setType(ndn_ContentType type) { type_ = type; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700116
117 void setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; }
118
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700119 void setFinalBlockID(const std::vector<unsigned char>& finalBlockID) { finalBlockID_ = finalBlockID; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700120 void setFinalBlockID(const unsigned char *finalBlockID, unsigned int finalBlockIdLength)
121 {
122 setVector(finalBlockID_, finalBlockID, finalBlockIdLength);
123 }
124
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700125 void setKeyLocator(const KeyLocator& keyLocator) { keyLocator_ = keyLocator; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700126
127private:
128 PublisherPublicKeyDigest publisherPublicKeyDigest_;
Jeff Thompson210b92f2013-07-11 15:16:03 -0700129 double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */
Jeff Thompsond8776352013-08-16 18:09:30 -0700130 ndn_ContentType type_; /**< default is ndn_ContentType_DATA. -1 for none */
Jeff Thompson210b92f2013-07-11 15:16:03 -0700131 int freshnessSeconds_; /**< -1 for none */
132 std::vector<unsigned char> finalBlockID_; /** size 0 for none */
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700133 KeyLocator keyLocator_;
134};
135
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700136class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700137public:
Jeff Thompsonf5dbd272013-08-08 16:49:55 -0700138 Data()
139 {
140 }
141
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700142 Data(const Name& name)
Jeff Thompsonf5dbd272013-08-08 16:49:55 -0700143 : name_(name)
144 {
145 }
146
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700147 ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode(WireFormat& wireFormat) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700148 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700149 return wireFormat.encodeData(*this);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700150 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700151 ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode() const
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700152 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700153 return wireEncode(*WireFormat::getDefaultWireFormat());
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700154 }
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700155 void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat& wireFormat)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700156 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700157 wireFormat.decodeData(*this, input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700158 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700159 void wireDecode(const unsigned char *input, unsigned int inputLength)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700160 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700161 wireDecode(input, inputLength, *WireFormat::getDefaultWireFormat());
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700162 }
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700163 void wireDecode(const std::vector<unsigned char>& input, WireFormat& wireFormat)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700164 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700165 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700166 }
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700167 void wireDecode(const std::vector<unsigned char>& input)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700168 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700169 wireDecode(&input[0], input.size());
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700170 }
171
172 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700173 * Set the dataStruct to point to the values in this interest, without copying any memory.
174 * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
175 * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700176 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700177 void get(struct ndn_Data& dataStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700178
179 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700180 * Clear this data object, and set the values by copying from the ndn_Data struct.
181 * @param dataStruct a C ndn_Data struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700182 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700183 void set(const struct ndn_Data& dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700184
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700185 const Signature& getSignature() const { return signature_; }
186 Signature& getSignature() { return signature_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700187
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700188 const Name& getName() const { return name_; }
189 Name& getName() { return name_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700190
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700191 const MetaInfo& getMetaInfo() const { return metaInfo_; }
192 MetaInfo& getMetaInfo() { return metaInfo_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700193
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700194 const std::vector<unsigned char>& getContent() const { return content_; }
195 std::vector<unsigned char>& getContent() { return content_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700196
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700197 void setSignature(const Signature& signature) { signature_ = signature; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700198
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700199 void setName(const Name& name) { name_ = name; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700200
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700201 void setMetainfo(const MetaInfo& metaInfo) { metaInfo_ = metaInfo; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700202
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700203 void setContent(const std::vector<unsigned char>& content) { content_ = content; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700204 void setContent(const unsigned char *content, unsigned int contentLength)
205 {
206 setVector(content_, content, contentLength);
207 }
Jeff Thompsonac1b0ac2013-08-08 20:09:39 -0700208
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700209private:
210 Signature signature_;
211 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700212 MetaInfo metaInfo_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700213 std::vector<unsigned char> content_;
214};
215
216}
217
218#endif