Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 6 | #ifndef NDN_DATA_HPP |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 7 | #define NDN_DATA_HPP |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 8 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 9 | #include "common.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 10 | #include "name.hpp" |
| 11 | #include "publisher-public-key-digest.hpp" |
| 12 | #include "key.hpp" |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 13 | #include "c/data.h" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | class Signature { |
| 18 | public: |
| 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 Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 37 | |
| 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 Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 56 | private: |
| 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 | |
| 62 | class SignedInfo { |
| 63 | public: |
| 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 Thompson | 210b92f | 2013-07-11 15:16:03 -0700 | [diff] [blame] | 85 | double getTimestampMilliseconds() const { return timestampMilliseconds_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 86 | |
| 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 Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 94 | |
| 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 Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 112 | |
| 113 | private: |
| 114 | PublisherPublicKeyDigest publisherPublicKeyDigest_; |
Jeff Thompson | 210b92f | 2013-07-11 15:16:03 -0700 | [diff] [blame] | 115 | 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 Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 119 | KeyLocator keyLocator_; |
| 120 | }; |
| 121 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 122 | class Data { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 123 | public: |
Jeff Thompson | f5dbd27 | 2013-08-08 16:49:55 -0700 | [diff] [blame^] | 124 | Data() |
| 125 | { |
| 126 | } |
| 127 | |
| 128 | Data(const Name &name) |
| 129 | : name_(name) |
| 130 | { |
| 131 | } |
| 132 | |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 133 | ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode(WireFormat &wireFormat) const |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 134 | { |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 135 | return wireFormat.encodeData(*this); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 136 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 137 | ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode() const |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 138 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 139 | return wireEncode(*WireFormat::getDefaultWireFormat()); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 140 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 141 | void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 142 | { |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 143 | wireFormat.decodeData(*this, input, inputLength); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 144 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 145 | void wireDecode(const unsigned char *input, unsigned int inputLength) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 146 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 147 | wireDecode(input, inputLength, *WireFormat::getDefaultWireFormat()); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 148 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 149 | void wireDecode(const std::vector<unsigned char> &input, WireFormat &wireFormat) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 150 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 151 | wireDecode(&input[0], input.size(), wireFormat); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 152 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 153 | void wireDecode(const std::vector<unsigned char> &input) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 154 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 155 | wireDecode(&input[0], input.size()); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 159 | * 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 Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 162 | */ |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 163 | void get(struct ndn_Data &dataStruct) const; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 164 | |
| 165 | /** |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 166 | * Clear this data object, and set the values by copying from the ndn_Data struct. |
| 167 | * @param dataStruct a C ndn_Data struct |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 168 | */ |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 169 | void set(const struct ndn_Data &dataStruct); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 170 | |
| 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 Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 178 | |
| 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 Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 193 | |
| 194 | private: |
| 195 | Signature signature_; |
| 196 | Name name_; |
| 197 | SignedInfo signedInfo_; |
| 198 | std::vector<unsigned char> content_; |
| 199 | }; |
| 200 | |
| 201 | } |
| 202 | |
| 203 | #endif |