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 | /* |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 87 | Data::wireEncode<encoding::EncoderTag>(EncodingBuffer&, bool) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 88 | |
| 89 | template size_t |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 90 | Data::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, bool) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 92 | const Block& |
| 93 | Data::wireEncode(EncodingBuffer& encoder, const Block& signatureValue) const |
| 94 | { |
| 95 | size_t totalLength = encoder.size(); |
| 96 | totalLength += encoder.appendBlock(signatureValue); |
| 97 | |
Davide Pesavento | 9bd4d98 | 2015-05-13 14:31:19 +0200 | [diff] [blame] | 98 | encoder.prependVarNumber(totalLength); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 99 | encoder.prependVarNumber(tlv::Data); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 100 | |
| 101 | const_cast<Data*>(this)->wireDecode(encoder.block()); |
| 102 | return m_wire; |
| 103 | } |
| 104 | |
| 105 | const Block& |
| 106 | Data::wireEncode() const |
| 107 | { |
| 108 | if (m_wire.hasWire()) |
| 109 | return m_wire; |
| 110 | |
| 111 | EncodingEstimator estimator; |
| 112 | size_t estimatedSize = wireEncode(estimator); |
| 113 | |
| 114 | EncodingBuffer buffer(estimatedSize, 0); |
| 115 | wireEncode(buffer); |
| 116 | |
| 117 | const_cast<Data*>(this)->wireDecode(buffer.block()); |
| 118 | return m_wire; |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | Data::wireDecode(const Block& wire) |
| 123 | { |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 124 | m_fullName.clear(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 125 | m_wire = wire; |
| 126 | m_wire.parse(); |
| 127 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 128 | // Name |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 129 | m_name.wireDecode(m_wire.get(tlv::Name)); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 130 | |
| 131 | // MetaInfo |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 132 | m_metaInfo.wireDecode(m_wire.get(tlv::MetaInfo)); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 133 | |
| 134 | // Content |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 135 | m_content = m_wire.get(tlv::Content); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 136 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 137 | // SignatureInfo |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 138 | m_signature.setInfo(m_wire.get(tlv::SignatureInfo)); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 139 | |
| 140 | // SignatureValue |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 141 | Block::element_const_iterator val = m_wire.find(tlv::SignatureValue); |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 142 | if (val != m_wire.elements_end()) { |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 143 | m_signature.setValue(*val); |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 144 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 147 | const Name& |
| 148 | Data::getFullName() const |
| 149 | { |
| 150 | if (m_fullName.empty()) { |
| 151 | if (!m_wire.hasWire()) { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 152 | 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] | 153 | } |
| 154 | m_fullName = m_name; |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 155 | m_fullName.appendImplicitSha256Digest(util::Sha256::computeDigest(m_wire.wire(), m_wire.size())); |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return m_fullName; |
| 159 | } |
| 160 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 161 | void |
| 162 | Data::resetWire() |
| 163 | { |
| 164 | m_wire.reset(); |
| 165 | m_fullName.clear(); |
| 166 | } |
| 167 | |
| 168 | Data& |
| 169 | Data::setName(const Name& name) |
| 170 | { |
| 171 | resetWire(); |
| 172 | m_name = name; |
| 173 | return *this; |
| 174 | } |
| 175 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 176 | Data& |
| 177 | Data::setMetaInfo(const MetaInfo& metaInfo) |
| 178 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 179 | resetWire(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 180 | m_metaInfo = metaInfo; |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 181 | return *this; |
| 182 | } |
| 183 | |
| 184 | const Block& |
| 185 | Data::getContent() const |
| 186 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 187 | if (!m_content.hasWire()) { |
| 188 | const_cast<Block&>(m_content).encode(); |
| 189 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 190 | return m_content; |
| 191 | } |
| 192 | |
| 193 | Data& |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 194 | Data::setContent(const Block& block) |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 195 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 196 | resetWire(); |
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 | if (block.type() == tlv::Content) { |
| 199 | m_content = block; |
| 200 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 201 | else { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 202 | m_content = Block(tlv::Content, block); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | return *this; |
| 206 | } |
| 207 | |
| 208 | Data& |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 209 | Data::setContent(const uint8_t* value, size_t valueSize) |
| 210 | { |
| 211 | resetWire(); |
| 212 | m_content = makeBinaryBlock(tlv::Content, value, valueSize); |
| 213 | return *this; |
| 214 | } |
| 215 | |
| 216 | Data& |
| 217 | Data::setContent(const ConstBufferPtr& value) |
| 218 | { |
| 219 | resetWire(); |
| 220 | m_content = Block(tlv::Content, value); |
| 221 | return *this; |
| 222 | } |
| 223 | |
| 224 | Data& |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 225 | Data::setSignature(const Signature& signature) |
| 226 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 227 | resetWire(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 228 | m_signature = signature; |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 229 | return *this; |
| 230 | } |
| 231 | |
| 232 | Data& |
| 233 | Data::setSignatureValue(const Block& value) |
| 234 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 235 | resetWire(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 236 | m_signature.setValue(value); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 237 | return *this; |
| 238 | } |
| 239 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 240 | Data& |
| 241 | Data::setContentType(uint32_t type) |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 242 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 243 | resetWire(); |
| 244 | m_metaInfo.setType(type); |
| 245 | return *this; |
| 246 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 247 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 248 | Data& |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 249 | Data::setFreshnessPeriod(time::milliseconds freshnessPeriod) |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 250 | { |
| 251 | resetWire(); |
| 252 | m_metaInfo.setFreshnessPeriod(freshnessPeriod); |
| 253 | return *this; |
| 254 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 255 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 256 | Data& |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 257 | Data::setFinalBlock(optional<name::Component> finalBlockId) |
| 258 | { |
| 259 | resetWire(); |
| 260 | m_metaInfo.setFinalBlock(std::move(finalBlockId)); |
| 261 | return *this; |
| 262 | } |
| 263 | |
| 264 | name::Component |
| 265 | Data::getFinalBlockId() const |
| 266 | { |
| 267 | return m_metaInfo.getFinalBlockId(); |
| 268 | } |
| 269 | |
| 270 | Data& |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 271 | Data::setFinalBlockId(const name::Component& finalBlockId) |
| 272 | { |
| 273 | resetWire(); |
| 274 | m_metaInfo.setFinalBlockId(finalBlockId); |
| 275 | return *this; |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | bool |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 279 | operator==(const Data& lhs, const Data& rhs) |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 280 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 281 | return lhs.getName() == rhs.getName() && |
| 282 | lhs.getMetaInfo() == rhs.getMetaInfo() && |
| 283 | lhs.getContent() == rhs.getContent() && |
| 284 | lhs.getSignature() == rhs.getSignature(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | std::ostream& |
| 288 | operator<<(std::ostream& os, const Data& data) |
| 289 | { |
| 290 | os << "Name: " << data.getName() << "\n"; |
| 291 | os << "MetaInfo: " << data.getMetaInfo() << "\n"; |
| 292 | os << "Content: (size: " << data.getContent().value_size() << ")\n"; |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 293 | os << "Signature: (type: " << data.getSignature().getType() |
| 294 | << ", value_length: "<< data.getSignature().getValue().value_size() << ")"; |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 295 | os << std::endl; |
| 296 | |
| 297 | return os; |
| 298 | } |
| 299 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 300 | } // namespace ndn |