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 | /* |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/data.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 23 | #include "ndn-cxx/util/sha256.hpp" |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 24 | |
| 25 | namespace ndn { |
| 26 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 27 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Data>)); |
| 28 | BOOST_CONCEPT_ASSERT((WireEncodable<Data>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 29 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Data>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 30 | BOOST_CONCEPT_ASSERT((WireDecodable<Data>)); |
| 31 | static_assert(std::is_base_of<tlv::Error, Data::Error>::value, |
| 32 | "Data::Error must inherit from tlv::Error"); |
| 33 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 34 | Data::Data(const Name& name) |
| 35 | : m_name(name) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | Data::Data(const Block& wire) |
| 40 | { |
| 41 | wireDecode(wire); |
| 42 | } |
| 43 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 44 | template<encoding::Tag TAG> |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 45 | size_t |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 46 | Data::wireEncode(EncodingImpl<TAG>& encoder, bool wantUnsignedPortionOnly) const |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 47 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 48 | // Data = DATA-TYPE TLV-LENGTH |
| 49 | // Name |
| 50 | // [MetaInfo] |
| 51 | // [Content] |
| 52 | // SignatureInfo |
| 53 | // SignatureValue |
| 54 | // (elements are encoded in reverse order) |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 56 | size_t totalLength = 0; |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 57 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 58 | // SignatureValue |
| 59 | if (!wantUnsignedPortionOnly) { |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 60 | if (!m_signatureInfo) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 61 | NDN_THROW(Error("Requested wire format, but Data has not been signed")); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 62 | } |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 63 | totalLength += encoder.prependBlock(m_signatureValue); |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 64 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 65 | |
| 66 | // SignatureInfo |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 67 | totalLength += m_signatureInfo.wireEncode(encoder, SignatureInfo::Type::Data); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 68 | |
| 69 | // Content |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 70 | if (hasContent()) { |
| 71 | totalLength += encoder.prependBlock(m_content); |
| 72 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 73 | |
| 74 | // MetaInfo |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 75 | totalLength += m_metaInfo.wireEncode(encoder); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 76 | |
| 77 | // Name |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 78 | totalLength += m_name.wireEncode(encoder); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 79 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 80 | if (!wantUnsignedPortionOnly) { |
| 81 | totalLength += encoder.prependVarNumber(totalLength); |
| 82 | totalLength += encoder.prependVarNumber(tlv::Data); |
| 83 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 84 | return totalLength; |
| 85 | } |
| 86 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 87 | template size_t |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 88 | Data::wireEncode<encoding::EncoderTag>(EncodingBuffer&, bool) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 89 | |
| 90 | template size_t |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 91 | Data::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, bool) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 92 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 93 | const Block& |
| 94 | Data::wireEncode(EncodingBuffer& encoder, const Block& signatureValue) const |
| 95 | { |
| 96 | size_t totalLength = encoder.size(); |
| 97 | totalLength += encoder.appendBlock(signatureValue); |
| 98 | |
Davide Pesavento | 9bd4d98 | 2015-05-13 14:31:19 +0200 | [diff] [blame] | 99 | encoder.prependVarNumber(totalLength); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 100 | encoder.prependVarNumber(tlv::Data); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 101 | |
| 102 | const_cast<Data*>(this)->wireDecode(encoder.block()); |
| 103 | return m_wire; |
| 104 | } |
| 105 | |
| 106 | const Block& |
| 107 | Data::wireEncode() const |
| 108 | { |
| 109 | if (m_wire.hasWire()) |
| 110 | return m_wire; |
| 111 | |
| 112 | EncodingEstimator estimator; |
| 113 | size_t estimatedSize = wireEncode(estimator); |
| 114 | |
| 115 | EncodingBuffer buffer(estimatedSize, 0); |
| 116 | wireEncode(buffer); |
| 117 | |
| 118 | const_cast<Data*>(this)->wireDecode(buffer.block()); |
| 119 | return m_wire; |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | Data::wireDecode(const Block& wire) |
| 124 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 125 | if (wire.type() != tlv::Data) { |
| 126 | NDN_THROW(Error("Data", wire.type())); |
| 127 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 128 | m_wire = wire; |
| 129 | m_wire.parse(); |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 130 | |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 131 | // Data = DATA-TYPE TLV-LENGTH |
| 132 | // Name |
| 133 | // [MetaInfo] |
| 134 | // [Content] |
| 135 | // SignatureInfo |
| 136 | // SignatureValue |
| 137 | |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 138 | auto element = m_wire.elements_begin(); |
| 139 | if (element == m_wire.elements_end() || element->type() != tlv::Name) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 140 | NDN_THROW(Error("Name element is missing or out of order")); |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 141 | } |
| 142 | m_name.wireDecode(*element); |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 143 | |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 144 | m_metaInfo = {}; |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 145 | m_content = {}; |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 146 | m_signatureInfo = {}; |
| 147 | m_signatureValue = {}; |
Junxiao Shi | 7d9039b | 2018-04-14 15:56:28 +0000 | [diff] [blame] | 148 | m_fullName.clear(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 149 | |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 150 | int lastElement = 1; // last recognized element index, in spec order |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 151 | for (++element; element != m_wire.elements_end(); ++element) { |
| 152 | switch (element->type()) { |
Junxiao Shi | 7d9039b | 2018-04-14 15:56:28 +0000 | [diff] [blame] | 153 | case tlv::MetaInfo: { |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 154 | if (lastElement >= 2) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 155 | NDN_THROW(Error("MetaInfo element is out of order")); |
Junxiao Shi | 7d9039b | 2018-04-14 15:56:28 +0000 | [diff] [blame] | 156 | } |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 157 | m_metaInfo.wireDecode(*element); |
| 158 | lastElement = 2; |
Junxiao Shi | 7d9039b | 2018-04-14 15:56:28 +0000 | [diff] [blame] | 159 | break; |
| 160 | } |
| 161 | case tlv::Content: { |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 162 | if (lastElement >= 3) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 163 | NDN_THROW(Error("Content element is out of order")); |
Junxiao Shi | 7d9039b | 2018-04-14 15:56:28 +0000 | [diff] [blame] | 164 | } |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 165 | m_content = *element; |
| 166 | lastElement = 3; |
Junxiao Shi | 7d9039b | 2018-04-14 15:56:28 +0000 | [diff] [blame] | 167 | break; |
| 168 | } |
| 169 | case tlv::SignatureInfo: { |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 170 | if (lastElement >= 4) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 171 | NDN_THROW(Error("SignatureInfo element is out of order")); |
Junxiao Shi | 7d9039b | 2018-04-14 15:56:28 +0000 | [diff] [blame] | 172 | } |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 173 | m_signatureInfo.wireDecode(*element); |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 174 | lastElement = 4; |
Junxiao Shi | 7d9039b | 2018-04-14 15:56:28 +0000 | [diff] [blame] | 175 | break; |
| 176 | } |
| 177 | case tlv::SignatureValue: { |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 178 | if (lastElement >= 5) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 179 | NDN_THROW(Error("SignatureValue element is out of order")); |
Junxiao Shi | 7d9039b | 2018-04-14 15:56:28 +0000 | [diff] [blame] | 180 | } |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 181 | m_signatureValue = *element; |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 182 | lastElement = 5; |
Junxiao Shi | 7d9039b | 2018-04-14 15:56:28 +0000 | [diff] [blame] | 183 | break; |
| 184 | } |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 185 | default: { // unrecognized element |
| 186 | // if the TLV-TYPE is critical, abort decoding |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 187 | if (tlv::isCriticalType(element->type())) { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 188 | NDN_THROW(Error("Unrecognized element of critical type " + to_string(element->type()))); |
Junxiao Shi | 7d9039b | 2018-04-14 15:56:28 +0000 | [diff] [blame] | 189 | } |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 190 | // otherwise, ignore it |
Junxiao Shi | 7d9039b | 2018-04-14 15:56:28 +0000 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | } |
| 194 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 195 | |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 196 | if (!m_signatureInfo) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 197 | NDN_THROW(Error("SignatureInfo element is missing")); |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 198 | } |
Davide Pesavento | 46cdf90 | 2020-06-08 16:38:23 -0400 | [diff] [blame] | 199 | if (!m_signatureValue.isValid()) { |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 200 | NDN_THROW(Error("SignatureValue element is missing")); |
| 201 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 204 | const Name& |
| 205 | Data::getFullName() const |
| 206 | { |
| 207 | if (m_fullName.empty()) { |
| 208 | if (!m_wire.hasWire()) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 209 | NDN_THROW(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] | 210 | } |
| 211 | m_fullName = m_name; |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 212 | m_fullName.appendImplicitSha256Digest(util::Sha256::computeDigest(m_wire.wire(), m_wire.size())); |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return m_fullName; |
| 216 | } |
| 217 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 218 | void |
| 219 | Data::resetWire() |
| 220 | { |
| 221 | m_wire.reset(); |
| 222 | m_fullName.clear(); |
| 223 | } |
| 224 | |
| 225 | Data& |
| 226 | Data::setName(const Name& name) |
| 227 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 228 | if (name != m_name) { |
| 229 | m_name = name; |
| 230 | resetWire(); |
| 231 | } |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 232 | return *this; |
| 233 | } |
| 234 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 235 | Data& |
| 236 | Data::setMetaInfo(const MetaInfo& metaInfo) |
| 237 | { |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 238 | m_metaInfo = metaInfo; |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 239 | resetWire(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 240 | return *this; |
| 241 | } |
| 242 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 243 | Data& |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 244 | Data::setContent(const Block& block) |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 245 | { |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 246 | if (!block.isValid()) { |
| 247 | NDN_THROW(std::invalid_argument("Content block must be valid")); |
| 248 | } |
| 249 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 250 | if (block.type() == tlv::Content) { |
| 251 | m_content = block; |
| 252 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 253 | else { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 254 | m_content = Block(tlv::Content, block); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 255 | } |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 256 | m_content.encode(); |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 257 | resetWire(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 258 | return *this; |
| 259 | } |
| 260 | |
| 261 | Data& |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 262 | Data::setContent(const uint8_t* value, size_t length) |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 263 | { |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 264 | if (value == nullptr && length != 0) { |
| 265 | NDN_THROW(std::invalid_argument("Content buffer cannot be nullptr")); |
| 266 | } |
| 267 | |
| 268 | m_content = makeBinaryBlock(tlv::Content, value, length); |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 269 | resetWire(); |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 270 | return *this; |
| 271 | } |
| 272 | |
| 273 | Data& |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 274 | Data::setContent(ConstBufferPtr value) |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 275 | { |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 276 | if (value == nullptr) { |
| 277 | NDN_THROW(std::invalid_argument("Content buffer cannot be nullptr")); |
| 278 | } |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 279 | |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 280 | m_content = Block(tlv::Content, std::move(value)); |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 281 | resetWire(); |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 282 | return *this; |
| 283 | } |
| 284 | |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 285 | Data& |
| 286 | Data::unsetContent() |
| 287 | { |
| 288 | m_content = {}; |
| 289 | resetWire(); |
| 290 | return *this; |
| 291 | } |
| 292 | |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 293 | Data& |
| 294 | Data::setSignatureInfo(const SignatureInfo& info) |
| 295 | { |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 296 | m_signatureInfo = info; |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 297 | resetWire(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 298 | return *this; |
| 299 | } |
| 300 | |
| 301 | Data& |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 302 | Data::setSignatureValue(ConstBufferPtr value) |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 303 | { |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 304 | if (value == nullptr) { |
| 305 | NDN_THROW(std::invalid_argument("SignatureValue buffer cannot be nullptr")); |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 306 | } |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 307 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 308 | m_signatureValue = Block(tlv::SignatureValue, std::move(value)); |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 309 | resetWire(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 310 | return *this; |
| 311 | } |
| 312 | |
Eric Newberry | e36aae1 | 2020-08-31 22:23:13 -0700 | [diff] [blame] | 313 | InputBuffers |
| 314 | Data::extractSignedRanges() const |
| 315 | { |
| 316 | InputBuffers bufs; |
| 317 | bufs.reserve(1); // One range containing data value up to, but not including, SignatureValue |
| 318 | |
| 319 | wireEncode(); |
| 320 | auto lastSignedIt = std::prev(m_wire.find(tlv::SignatureValue)); |
| 321 | bufs.emplace_back(m_wire.value(), |
| 322 | std::distance(m_wire.value_begin(), lastSignedIt->end())); |
| 323 | return bufs; |
| 324 | } |
| 325 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 326 | Data& |
| 327 | Data::setContentType(uint32_t type) |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 328 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 329 | if (type != m_metaInfo.getType()) { |
| 330 | m_metaInfo.setType(type); |
| 331 | resetWire(); |
| 332 | } |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 333 | return *this; |
| 334 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 335 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 336 | Data& |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 337 | Data::setFreshnessPeriod(time::milliseconds freshnessPeriod) |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 338 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 339 | if (freshnessPeriod != m_metaInfo.getFreshnessPeriod()) { |
| 340 | m_metaInfo.setFreshnessPeriod(freshnessPeriod); |
| 341 | resetWire(); |
| 342 | } |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 343 | return *this; |
| 344 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 345 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 346 | Data& |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 347 | Data::setFinalBlock(optional<name::Component> finalBlockId) |
| 348 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 349 | if (finalBlockId != m_metaInfo.getFinalBlock()) { |
| 350 | m_metaInfo.setFinalBlock(std::move(finalBlockId)); |
| 351 | resetWire(); |
| 352 | } |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 353 | return *this; |
| 354 | } |
| 355 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 356 | bool |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 357 | operator==(const Data& lhs, const Data& rhs) |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 358 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 359 | return lhs.getName() == rhs.getName() && |
Davide Pesavento | ef654dd | 2019-07-18 20:02:44 -0400 | [diff] [blame] | 360 | lhs.getMetaInfo().wireEncode() == rhs.getMetaInfo().wireEncode() && |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame] | 361 | lhs.getContent() == rhs.getContent() && |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 362 | lhs.getSignatureInfo() == rhs.getSignatureInfo() && |
| 363 | lhs.getSignatureValue() == rhs.getSignatureValue(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | std::ostream& |
| 367 | operator<<(std::ostream& os, const Data& data) |
| 368 | { |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 369 | os << "Name: " << data.getName() << "\n" |
| 370 | << "MetaInfo: [" << data.getMetaInfo() << "]\n"; |
| 371 | |
| 372 | if (data.hasContent()) { |
| 373 | os << "Content: [" << data.getContent().value_size() << " bytes]\n"; |
| 374 | } |
| 375 | |
| 376 | os << "Signature: [type: " << static_cast<tlv::SignatureTypeValue>(data.getSignatureType()) |
| 377 | << ", length: "<< data.getSignatureValue().value_size() << "]\n"; |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 378 | |
| 379 | return os; |
| 380 | } |
| 381 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 382 | } // namespace ndn |