blob: f7fa747616714bb5ae1ebf18c2b0ea805b7ccb33 [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 */
24 void get(struct ndn_Signature &signatureStruct) const;
25
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 */
30 void set(const struct ndn_Signature &signatureStruct);
31
32 const std::vector<unsigned char> getDigestAlgorithm() const { return digestAlgorithm_; }
33
34 const std::vector<unsigned char> getWitness() const { return witness_; }
35
36 const std::vector<unsigned char> getSignature() const { return signature_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070037
38 void setDigestAlgorithm(const std::vector<unsigned char> &digestAlgorithm) { digestAlgorithm_ = digestAlgorithm; }
39 void setDigestAlgorithm(const unsigned char *digestAlgorithm, unsigned int digestAlgorithmLength)
40 {
41 setVector(digestAlgorithm_, digestAlgorithm, digestAlgorithmLength);
42 }
43
44 void setWitness(const std::vector<unsigned char> &witness) { witness_ = witness; }
45 void setWitness(const unsigned char *witness, unsigned int witnessLength)
46 {
47 setVector(witness_, witness, witnessLength);
48 }
49
50 void setSignature(const std::vector<unsigned char> &signature) { signature_ = signature; }
51 void setSignature(const unsigned char *signature, unsigned int signatureLength)
52 {
53 setVector(signature_, signature, signatureLength);
54 }
55
Jeff Thompson5cae5e52013-07-10 19:41:20 -070056private:
57 std::vector<unsigned char> digestAlgorithm_; /**< if empty, the default is 2.16.840.1.101.3.4.2.1 (sha-256) */
58 std::vector<unsigned char> witness_;
59 std::vector<unsigned char> signature_;
60};
61
62class SignedInfo {
63public:
64 SignedInfo()
65 {
66 type_ = ndn_ContentType_DATA;
67 freshnessSeconds_ = -1;
68 }
69
70 /**
71 * Set the signedInfoStruct to point to the values in this signed info object, without copying any memory.
72 * WARNING: The resulting pointers in signedInfoStruct are invalid after a further use of this object which could reallocate memory.
73 * @param signedInfoStruct a C ndn_SignedInfo struct where the name components array is already allocated.
74 */
75 void get(struct ndn_SignedInfo &signedInfoStruct) const;
76
77 /**
78 * Clear this signed info, and set the values by copying from the ndn_SignedInfo struct.
79 * @param signedInfoStruct a C ndn_SignedInfo struct
80 */
81 void set(const struct ndn_SignedInfo &signedInfoStruct);
82
83 const PublisherPublicKeyDigest &getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
84
Jeff Thompson210b92f2013-07-11 15:16:03 -070085 double getTimestampMilliseconds() const { return timestampMilliseconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070086
87 int getType() const { return type_; }
88
89 int getFreshnessSeconds() const { return freshnessSeconds_; }
90
91 const std::vector<unsigned char> getFinalBlockID() const { return finalBlockID_; }
92
93 const KeyLocator &getKeyLocator() const { return keyLocator_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070094
95 PublisherPublicKeyDigest &getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
96 void setPublisherPublicKeyDigest(const PublisherPublicKeyDigest &publisherPublicKeyDigest) { publisherPublicKeyDigest_ = publisherPublicKeyDigest; }
97
98 void setTimestampMilliseconds(double timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; }
99
100 void setType(int type) { type_ = type; }
101
102 void setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; }
103
104 void setFinalBlockID(const std::vector<unsigned char> &finalBlockID) { finalBlockID_ = finalBlockID; }
105 void setFinalBlockID(const unsigned char *finalBlockID, unsigned int finalBlockIdLength)
106 {
107 setVector(finalBlockID_, finalBlockID, finalBlockIdLength);
108 }
109
110 KeyLocator &getKeyLocator() { return keyLocator_; }
111 void setKeyLocator(const KeyLocator &keyLocator) { keyLocator_ = keyLocator; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700112
113private:
114 PublisherPublicKeyDigest publisherPublicKeyDigest_;
Jeff Thompson210b92f2013-07-11 15:16:03 -0700115 double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */
116 int type_; /**< default is ndn_ContentType_DATA. -1 for none */
117 int freshnessSeconds_; /**< -1 for none */
118 std::vector<unsigned char> finalBlockID_; /** size 0 for none */
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700119 KeyLocator keyLocator_;
120};
121
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700122class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700123public:
Jeff Thompsonf5dbd272013-08-08 16:49:55 -0700124 Data()
125 {
126 }
127
128 Data(const Name &name)
129 : name_(name)
130 {
131 }
132
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700133 ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode(WireFormat &wireFormat) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700134 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700135 return wireFormat.encodeData(*this);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700136 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700137 ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode() const
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700138 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700139 return wireEncode(*WireFormat::getDefaultWireFormat());
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700140 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700141 void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700142 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700143 wireFormat.decodeData(*this, input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700144 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700145 void wireDecode(const unsigned char *input, unsigned int inputLength)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700146 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700147 wireDecode(input, inputLength, *WireFormat::getDefaultWireFormat());
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700148 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700149 void wireDecode(const std::vector<unsigned char> &input, WireFormat &wireFormat)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700150 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700151 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700152 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700153 void wireDecode(const std::vector<unsigned char> &input)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700154 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700155 wireDecode(&input[0], input.size());
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700156 }
157
158 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700159 * Set the dataStruct to point to the values in this interest, without copying any memory.
160 * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
161 * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700162 */
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700163 void get(struct ndn_Data &dataStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700164
165 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700166 * Clear this data object, and set the values by copying from the ndn_Data struct.
167 * @param dataStruct a C ndn_Data struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700168 */
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700169 void set(const struct ndn_Data &dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700170
171 const Signature &getSignature() const { return signature_; }
172
173 const Name &getName() const { return name_; }
174
175 const SignedInfo &getSignedInfo() const { return signedInfo_; }
176
177 const std::vector<unsigned char> getContent() const { return content_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700178
179 Signature &getSignature() { return signature_; }
180 void setSignature(const Signature &signature) { signature_ = signature; }
181
182 Name &getName() { return name_; }
183 void setName(const Name &name) { name_ = name; }
184
185 SignedInfo &getSignedInfo() { return signedInfo_; }
186 void setSignedInfo(const SignedInfo &signedInfo) { signedInfo_ = signedInfo; }
187
188 void setContent(const std::vector<unsigned char> &content) { content_ = content; }
189 void setContent(const unsigned char *content, unsigned int contentLength)
190 {
191 setVector(content_, content, contentLength);
192 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700193
194private:
195 Signature signature_;
196 Name name_;
197 SignedInfo signedInfo_;
198 std::vector<unsigned char> content_;
199};
200
201}
202
203#endif