blob: 883331fb21f05fe5a08034dd35445b0859fffad6 [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
7#define NDN_DATA_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -07008
9#include "Name.hpp"
10#include "PublisherPublicKeyDigest.hpp"
11#include "Key.hpp"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070012#include "c/data.h"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070013
14namespace ndn {
15
16class Signature {
17public:
18 /**
19 * Set the signatureStruct to point to the values in this signature object, without copying any memory.
20 * WARNING: The resulting pointers in signatureStruct are invalid after a further use of this object which could reallocate memory.
21 * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated.
22 */
23 void get(struct ndn_Signature &signatureStruct) const;
24
25 /**
26 * Clear this signature, and set the values by copying from the ndn_Signature struct.
27 * @param signatureStruct a C ndn_Signature struct
28 */
29 void set(const struct ndn_Signature &signatureStruct);
30
31 const std::vector<unsigned char> getDigestAlgorithm() const { return digestAlgorithm_; }
32
33 const std::vector<unsigned char> getWitness() const { return witness_; }
34
35 const std::vector<unsigned char> getSignature() const { return signature_; }
36
37private:
38 std::vector<unsigned char> digestAlgorithm_; /**< if empty, the default is 2.16.840.1.101.3.4.2.1 (sha-256) */
39 std::vector<unsigned char> witness_;
40 std::vector<unsigned char> signature_;
41};
42
43class SignedInfo {
44public:
45 SignedInfo()
46 {
47 type_ = ndn_ContentType_DATA;
48 freshnessSeconds_ = -1;
49 }
50
51 /**
52 * Set the signedInfoStruct to point to the values in this signed info object, without copying any memory.
53 * WARNING: The resulting pointers in signedInfoStruct are invalid after a further use of this object which could reallocate memory.
54 * @param signedInfoStruct a C ndn_SignedInfo struct where the name components array is already allocated.
55 */
56 void get(struct ndn_SignedInfo &signedInfoStruct) const;
57
58 /**
59 * Clear this signed info, and set the values by copying from the ndn_SignedInfo struct.
60 * @param signedInfoStruct a C ndn_SignedInfo struct
61 */
62 void set(const struct ndn_SignedInfo &signedInfoStruct);
63
64 const PublisherPublicKeyDigest &getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
65
Jeff Thompson210b92f2013-07-11 15:16:03 -070066 double getTimestampMilliseconds() const { return timestampMilliseconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070067
68 int getType() const { return type_; }
69
70 int getFreshnessSeconds() const { return freshnessSeconds_; }
71
72 const std::vector<unsigned char> getFinalBlockID() const { return finalBlockID_; }
73
74 const KeyLocator &getKeyLocator() const { return keyLocator_; }
75
76private:
77 PublisherPublicKeyDigest publisherPublicKeyDigest_;
Jeff Thompson210b92f2013-07-11 15:16:03 -070078 double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */
79 int type_; /**< default is ndn_ContentType_DATA. -1 for none */
80 int freshnessSeconds_; /**< -1 for none */
81 std::vector<unsigned char> finalBlockID_; /** size 0 for none */
Jeff Thompson5cae5e52013-07-10 19:41:20 -070082 KeyLocator keyLocator_;
83};
84
Jeff Thompson56ec9e22013-08-02 11:34:07 -070085class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070086public:
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070087 ptr_lib::shared_ptr<std::vector<unsigned char> > encode(WireFormat &wireFormat) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -070088 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -070089 return wireFormat.encodeData(*this);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070090 }
Jeff Thompsonb0979fd2013-07-30 15:48:21 -070091 ptr_lib::shared_ptr<std::vector<unsigned char> > encode() const
Jeff Thompson5cae5e52013-07-10 19:41:20 -070092 {
Jeff Thompsonb915b1d2013-07-31 10:53:13 -070093 return encode(BinaryXmlWireFormat::getInstance());
Jeff Thompson5cae5e52013-07-10 19:41:20 -070094 }
95 void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat)
96 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -070097 wireFormat.decodeData(*this, input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070098 }
99 void decode(const unsigned char *input, unsigned int inputLength)
100 {
Jeff Thompsonb915b1d2013-07-31 10:53:13 -0700101 decode(input, inputLength, BinaryXmlWireFormat::getInstance());
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700102 }
103 void decode(const std::vector<unsigned char> &input, WireFormat &wireFormat)
104 {
105 decode(&input[0], input.size(), wireFormat);
106 }
107 void decode(const std::vector<unsigned char> &input)
108 {
109 decode(&input[0], input.size());
110 }
111
112 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700113 * Set the dataStruct to point to the values in this interest, without copying any memory.
114 * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
115 * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700116 */
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700117 void get(struct ndn_Data &dataStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700118
119 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700120 * Clear this data object, and set the values by copying from the ndn_Data struct.
121 * @param dataStruct a C ndn_Data struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700122 */
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700123 void set(const struct ndn_Data &dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700124
125 const Signature &getSignature() const { return signature_; }
126
127 const Name &getName() const { return name_; }
128
129 const SignedInfo &getSignedInfo() const { return signedInfo_; }
130
131 const std::vector<unsigned char> getContent() const { return content_; }
132
133private:
134 Signature signature_;
135 Name name_;
136 SignedInfo signedInfo_;
137 std::vector<unsigned char> content_;
138};
139
140}
141
142#endif