Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "data.hpp" |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 23 | #include "encoding/block-helpers.hpp" |
Junxiao Shi | 6938e34 | 2017-07-25 21:56:58 +0000 | [diff] [blame] | 24 | #include "util/sha256.hpp" |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 28 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Data>)); |
| 29 | BOOST_CONCEPT_ASSERT((WireEncodable<Data>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 30 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Data>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 31 | BOOST_CONCEPT_ASSERT((WireDecodable<Data>)); |
| 32 | static_assert(std::is_base_of<tlv::Error, Data::Error>::value, |
| 33 | "Data::Error must inherit from tlv::Error"); |
| 34 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 35 | Data::Data(const Name& name) |
| 36 | : m_name(name) |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 37 | , m_content(tlv::Content) |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
| 41 | Data::Data(const Block& wire) |
| 42 | { |
| 43 | wireDecode(wire); |
| 44 | } |
| 45 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 46 | template<encoding::Tag TAG> |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 47 | size_t |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 48 | Data::wireEncode(EncodingImpl<TAG>& encoder, bool wantUnsignedPortionOnly) const |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 49 | { |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 50 | // Data ::= DATA-TLV TLV-LENGTH |
| 51 | // Name |
| 52 | // MetaInfo |
| 53 | // Content |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 54 | // SignatureInfo |
| 55 | // SignatureValue |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 56 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 57 | size_t totalLength = 0; |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 58 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 59 | // SignatureValue |
| 60 | if (!wantUnsignedPortionOnly) { |
| 61 | if (!m_signature) { |
| 62 | BOOST_THROW_EXCEPTION(Error("Requested wire format, but Data has not been signed")); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 63 | } |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 64 | totalLength += encoder.prependBlock(m_signature.getValue()); |
| 65 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 66 | |
| 67 | // SignatureInfo |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 68 | totalLength += encoder.prependBlock(m_signature.getInfo()); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 69 | |
| 70 | // Content |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 71 | totalLength += encoder.prependBlock(getContent()); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 72 | |
| 73 | // MetaInfo |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 74 | totalLength += getMetaInfo().wireEncode(encoder); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 75 | |
| 76 | // Name |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 77 | totalLength += getName().wireEncode(encoder); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 78 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 79 | if (!wantUnsignedPortionOnly) { |
| 80 | totalLength += encoder.prependVarNumber(totalLength); |
| 81 | totalLength += encoder.prependVarNumber(tlv::Data); |
| 82 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 83 | return totalLength; |
| 84 | } |
| 85 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 86 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 87 | Data::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder, |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 88 | bool wantUnsignedPortionOnly) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 89 | |
| 90 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 91 | Data::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder, |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 92 | bool wantUnsignedPortionOnly) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 94 | const Block& |
| 95 | Data::wireEncode(EncodingBuffer& encoder, const Block& signatureValue) const |
| 96 | { |
| 97 | size_t totalLength = encoder.size(); |
| 98 | totalLength += encoder.appendBlock(signatureValue); |
| 99 | |
Davide Pesavento | 9bd4d98 | 2015-05-13 14:31:19 +0200 | [diff] [blame] | 100 | encoder.prependVarNumber(totalLength); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 101 | encoder.prependVarNumber(tlv::Data); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 102 | |
| 103 | const_cast<Data*>(this)->wireDecode(encoder.block()); |
| 104 | return m_wire; |
| 105 | } |
| 106 | |
| 107 | const Block& |
| 108 | Data::wireEncode() const |
| 109 | { |
| 110 | if (m_wire.hasWire()) |
| 111 | return m_wire; |
| 112 | |
| 113 | EncodingEstimator estimator; |
| 114 | size_t estimatedSize = wireEncode(estimator); |
| 115 | |
| 116 | EncodingBuffer buffer(estimatedSize, 0); |
| 117 | wireEncode(buffer); |
| 118 | |
| 119 | const_cast<Data*>(this)->wireDecode(buffer.block()); |
| 120 | return m_wire; |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | Data::wireDecode(const Block& wire) |
| 125 | { |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 126 | m_fullName.clear(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 127 | m_wire = wire; |
| 128 | m_wire.parse(); |
| 129 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 130 | // Name |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 131 | m_name.wireDecode(m_wire.get(tlv::Name)); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 132 | |
| 133 | // MetaInfo |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 134 | m_metaInfo.wireDecode(m_wire.get(tlv::MetaInfo)); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 135 | |
| 136 | // Content |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 137 | m_content = m_wire.get(tlv::Content); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 138 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 139 | // SignatureInfo |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 140 | m_signature.setInfo(m_wire.get(tlv::SignatureInfo)); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 141 | |
| 142 | // SignatureValue |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 143 | Block::element_const_iterator val = m_wire.find(tlv::SignatureValue); |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 144 | if (val != m_wire.elements_end()) { |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 145 | m_signature.setValue(*val); |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 146 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 149 | const Name& |
| 150 | Data::getFullName() const |
| 151 | { |
| 152 | if (m_fullName.empty()) { |
| 153 | if (!m_wire.hasWire()) { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 154 | BOOST_THROW_EXCEPTION(Error("Cannot compute full name because Data has no wire encoding (not signed)")); |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 155 | } |
| 156 | m_fullName = m_name; |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 157 | m_fullName.appendImplicitSha256Digest(util::Sha256::computeDigest(m_wire.wire(), m_wire.size())); |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | return m_fullName; |
| 161 | } |
| 162 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 163 | void |
| 164 | Data::resetWire() |
| 165 | { |
| 166 | m_wire.reset(); |
| 167 | m_fullName.clear(); |
| 168 | } |
| 169 | |
| 170 | Data& |
| 171 | Data::setName(const Name& name) |
| 172 | { |
| 173 | resetWire(); |
| 174 | m_name = name; |
| 175 | return *this; |
| 176 | } |
| 177 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 178 | Data& |
| 179 | Data::setMetaInfo(const MetaInfo& metaInfo) |
| 180 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 181 | resetWire(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 182 | m_metaInfo = metaInfo; |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 183 | return *this; |
| 184 | } |
| 185 | |
| 186 | const Block& |
| 187 | Data::getContent() const |
| 188 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 189 | if (!m_content.hasWire()) { |
| 190 | const_cast<Block&>(m_content).encode(); |
| 191 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 192 | return m_content; |
| 193 | } |
| 194 | |
| 195 | Data& |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 196 | Data::setContent(const Block& block) |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 197 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 198 | resetWire(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 199 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 200 | if (block.type() == tlv::Content) { |
| 201 | m_content = block; |
| 202 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 203 | else { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 204 | m_content = Block(tlv::Content, block); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | return *this; |
| 208 | } |
| 209 | |
| 210 | Data& |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 211 | Data::setContent(const uint8_t* value, size_t valueSize) |
| 212 | { |
| 213 | resetWire(); |
| 214 | m_content = makeBinaryBlock(tlv::Content, value, valueSize); |
| 215 | return *this; |
| 216 | } |
| 217 | |
| 218 | Data& |
| 219 | Data::setContent(const ConstBufferPtr& value) |
| 220 | { |
| 221 | resetWire(); |
| 222 | m_content = Block(tlv::Content, value); |
| 223 | return *this; |
| 224 | } |
| 225 | |
| 226 | Data& |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 227 | Data::setSignature(const Signature& signature) |
| 228 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 229 | resetWire(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 230 | m_signature = signature; |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 231 | return *this; |
| 232 | } |
| 233 | |
| 234 | Data& |
| 235 | Data::setSignatureValue(const Block& value) |
| 236 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 237 | resetWire(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 238 | m_signature.setValue(value); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 239 | return *this; |
| 240 | } |
| 241 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 242 | Data& |
| 243 | Data::setContentType(uint32_t type) |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 244 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 245 | resetWire(); |
| 246 | m_metaInfo.setType(type); |
| 247 | return *this; |
| 248 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 249 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 250 | Data& |
| 251 | Data::setFreshnessPeriod(const time::milliseconds& freshnessPeriod) |
| 252 | { |
| 253 | resetWire(); |
| 254 | m_metaInfo.setFreshnessPeriod(freshnessPeriod); |
| 255 | return *this; |
| 256 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 257 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 258 | Data& |
| 259 | Data::setFinalBlockId(const name::Component& finalBlockId) |
| 260 | { |
| 261 | resetWire(); |
| 262 | m_metaInfo.setFinalBlockId(finalBlockId); |
| 263 | return *this; |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | bool |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 267 | operator==(const Data& lhs, const Data& rhs) |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 268 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 269 | return lhs.getName() == rhs.getName() && |
| 270 | lhs.getMetaInfo() == rhs.getMetaInfo() && |
| 271 | lhs.getContent() == rhs.getContent() && |
| 272 | lhs.getSignature() == rhs.getSignature(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | std::ostream& |
| 276 | operator<<(std::ostream& os, const Data& data) |
| 277 | { |
| 278 | os << "Name: " << data.getName() << "\n"; |
| 279 | os << "MetaInfo: " << data.getMetaInfo() << "\n"; |
| 280 | os << "Content: (size: " << data.getContent().value_size() << ")\n"; |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 281 | os << "Signature: (type: " << data.getSignature().getType() |
| 282 | << ", value_length: "<< data.getSignature().getValue().value_size() << ")"; |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 283 | os << std::endl; |
| 284 | |
| 285 | return os; |
| 286 | } |
| 287 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 288 | } // namespace ndn |