blob: 7e954d25a7b0729fd5e9dce0126731229059e816 [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 Thompson67e9e0a2013-08-02 19:16:19 -0700124 ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode(WireFormat &wireFormat) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700125 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700126 return wireFormat.encodeData(*this);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700127 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700128 ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode() const
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700129 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700130 return wireEncode(*WireFormat::getDefaultWireFormat());
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700131 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700132 void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700133 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700134 wireFormat.decodeData(*this, input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700135 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700136 void wireDecode(const unsigned char *input, unsigned int inputLength)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700137 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700138 wireDecode(input, inputLength, *WireFormat::getDefaultWireFormat());
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700139 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700140 void wireDecode(const std::vector<unsigned char> &input, WireFormat &wireFormat)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700141 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700142 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700143 }
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700144 void wireDecode(const std::vector<unsigned char> &input)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700145 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700146 wireDecode(&input[0], input.size());
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700147 }
148
149 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700150 * Set the dataStruct to point to the values in this interest, without copying any memory.
151 * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
152 * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700153 */
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700154 void get(struct ndn_Data &dataStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700155
156 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700157 * Clear this data object, and set the values by copying from the ndn_Data struct.
158 * @param dataStruct a C ndn_Data struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700159 */
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700160 void set(const struct ndn_Data &dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700161
162 const Signature &getSignature() const { return signature_; }
163
164 const Name &getName() const { return name_; }
165
166 const SignedInfo &getSignedInfo() const { return signedInfo_; }
167
168 const std::vector<unsigned char> getContent() const { return content_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700169
170 Signature &getSignature() { return signature_; }
171 void setSignature(const Signature &signature) { signature_ = signature; }
172
173 Name &getName() { return name_; }
174 void setName(const Name &name) { name_ = name; }
175
176 SignedInfo &getSignedInfo() { return signedInfo_; }
177 void setSignedInfo(const SignedInfo &signedInfo) { signedInfo_ = signedInfo; }
178
179 void setContent(const std::vector<unsigned char> &content) { content_ = content; }
180 void setContent(const unsigned char *content, unsigned int contentLength)
181 {
182 setVector(content_, content, contentLength);
183 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700184
185private:
186 Signature signature_;
187 Name name_;
188 SignedInfo signedInfo_;
189 std::vector<unsigned char> content_;
190};
191
192}
193
194#endif