blob: 34a7f65af9cbded5de947c13dc6314b77660d974 [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
Jeff Thompson237fbc02013-08-08 20:33:21 -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 Thompson237fbc02013-08-08 20:33:21 -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 Thompson237fbc02013-08-08 20:33:21 -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
41 void setDigestAlgorithm(const std::vector<unsigned char> &digestAlgorithm) { digestAlgorithm_ = digestAlgorithm; }
42 void setDigestAlgorithm(const unsigned char *digestAlgorithm, unsigned int digestAlgorithmLength)
43 {
44 setVector(digestAlgorithm_, digestAlgorithm, digestAlgorithmLength);
45 }
46
47 void setWitness(const std::vector<unsigned char> &witness) { witness_ = witness; }
48 void setWitness(const unsigned char *witness, unsigned int witnessLength)
49 {
50 setVector(witness_, witness, witnessLength);
51 }
52
53 void setSignature(const std::vector<unsigned char> &signature) { signature_ = signature; }
54 void setSignature(const unsigned char *signature, unsigned int signatureLength)
55 {
56 setVector(signature_, signature, signatureLength);
57 }
58
Jeff Thompson5cae5e52013-07-10 19:41:20 -070059private:
60 std::vector<unsigned char> digestAlgorithm_; /**< if empty, the default is 2.16.840.1.101.3.4.2.1 (sha-256) */
61 std::vector<unsigned char> witness_;
62 std::vector<unsigned char> signature_;
63};
64
65class SignedInfo {
66public:
67 SignedInfo()
68 {
69 type_ = ndn_ContentType_DATA;
70 freshnessSeconds_ = -1;
71 }
72
73 /**
74 * Set the signedInfoStruct to point to the values in this signed info object, without copying any memory.
75 * WARNING: The resulting pointers in signedInfoStruct are invalid after a further use of this object which could reallocate memory.
76 * @param signedInfoStruct a C ndn_SignedInfo struct where the name components array is already allocated.
77 */
78 void get(struct ndn_SignedInfo &signedInfoStruct) const;
79
80 /**
81 * Clear this signed info, and set the values by copying from the ndn_SignedInfo struct.
82 * @param signedInfoStruct a C ndn_SignedInfo struct
83 */
84 void set(const struct ndn_SignedInfo &signedInfoStruct);
85
86 const PublisherPublicKeyDigest &getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
Jeff Thompsonac1b0ac2013-08-08 20:09:39 -070087 PublisherPublicKeyDigest &getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070088
Jeff Thompson210b92f2013-07-11 15:16:03 -070089 double getTimestampMilliseconds() const { return timestampMilliseconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070090
91 int getType() const { return type_; }
92
93 int getFreshnessSeconds() const { return freshnessSeconds_; }
94
95 const std::vector<unsigned char> getFinalBlockID() const { return finalBlockID_; }
Jeff Thompsonac1b0ac2013-08-08 20:09:39 -070096 std::vector<unsigned char> getFinalBlockID() { return finalBlockID_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070097
98 const KeyLocator &getKeyLocator() const { return keyLocator_; }
Jeff Thompsonac1b0ac2013-08-08 20:09:39 -070099 KeyLocator &getKeyLocator() { return keyLocator_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700100
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700101 void setPublisherPublicKeyDigest(const PublisherPublicKeyDigest &publisherPublicKeyDigest) { publisherPublicKeyDigest_ = publisherPublicKeyDigest; }
102
103 void setTimestampMilliseconds(double timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; }
104
105 void setType(int type) { type_ = type; }
106
107 void setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; }
108
109 void setFinalBlockID(const std::vector<unsigned char> &finalBlockID) { finalBlockID_ = finalBlockID; }
110 void setFinalBlockID(const unsigned char *finalBlockID, unsigned int finalBlockIdLength)
111 {
112 setVector(finalBlockID_, finalBlockID, finalBlockIdLength);
113 }
114
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700115 void setKeyLocator(const KeyLocator &keyLocator) { keyLocator_ = keyLocator; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700116
117private:
118 PublisherPublicKeyDigest publisherPublicKeyDigest_;
Jeff Thompson210b92f2013-07-11 15:16:03 -0700119 double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */
120 int type_; /**< default is ndn_ContentType_DATA. -1 for none */
121 int freshnessSeconds_; /**< -1 for none */
122 std::vector<unsigned char> finalBlockID_; /** size 0 for none */
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700123 KeyLocator keyLocator_;
124};
125
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700126class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700127public:
Jeff Thompsonf5dbd272013-08-08 16:49:55 -0700128 Data()
129 {
130 }
131
132 Data(const Name &name)
133 : name_(name)
134 {
135 }
136
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700137 ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode(WireFormat &wireFormat) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700138 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700139 return wireFormat.encodeData(*this);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700140 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700141 ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode() const
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700142 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700143 return wireEncode(*WireFormat::getDefaultWireFormat());
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, WireFormat &wireFormat)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700146 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700147 wireFormat.decodeData(*this, input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700148 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700149 void wireDecode(const unsigned char *input, unsigned int inputLength)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700150 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700151 wireDecode(input, inputLength, *WireFormat::getDefaultWireFormat());
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700152 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700153 void wireDecode(const std::vector<unsigned char> &input, WireFormat &wireFormat)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700154 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700155 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700156 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700157 void wireDecode(const std::vector<unsigned char> &input)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700158 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700159 wireDecode(&input[0], input.size());
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700160 }
161
162 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700163 * Set the dataStruct to point to the values in this interest, without copying any memory.
164 * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
165 * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700166 */
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700167 void get(struct ndn_Data &dataStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700168
169 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700170 * Clear this data object, and set the values by copying from the ndn_Data struct.
171 * @param dataStruct a C ndn_Data struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700172 */
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700173 void set(const struct ndn_Data &dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700174
175 const Signature &getSignature() const { return signature_; }
Jeff Thompson237fbc02013-08-08 20:33:21 -0700176 Signature &getSignature() { return signature_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700177
178 const Name &getName() const { return name_; }
Jeff Thompson237fbc02013-08-08 20:33:21 -0700179 Name &getName() { return name_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700180
181 const SignedInfo &getSignedInfo() const { return signedInfo_; }
Jeff Thompson237fbc02013-08-08 20:33:21 -0700182 SignedInfo &getSignedInfo() { return signedInfo_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700183
184 const std::vector<unsigned char> getContent() const { return content_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700185
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700186 void setSignature(const Signature &signature) { signature_ = signature; }
187
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700188 void setName(const Name &name) { name_ = name; }
189
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700190 void setSignedInfo(const SignedInfo &signedInfo) { signedInfo_ = signedInfo; }
191
192 void setContent(const std::vector<unsigned char> &content) { content_ = content; }
193 void setContent(const unsigned char *content, unsigned int contentLength)
194 {
195 setVector(content_, content, contentLength);
196 }
Jeff Thompsonac1b0ac2013-08-08 20:09:39 -0700197
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700198private:
199 Signature signature_;
200 Name name_;
201 SignedInfo signedInfo_;
202 std::vector<unsigned char> content_;
203};
204
205}
206
207#endif