Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -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 | /* |
Davide Pesavento | 08378cb | 2018-02-01 16:10:54 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -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 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
| 22 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 23 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| 24 | */ |
| 25 | |
| 26 | #include "name-component.hpp" |
| 27 | |
| 28 | #include "encoding/block-helpers.hpp" |
| 29 | #include "encoding/encoding-buffer.hpp" |
Junxiao Shi | 6938e34 | 2017-07-25 21:56:58 +0000 | [diff] [blame] | 30 | #include "util/sha256.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 31 | #include "util/string-helper.hpp" |
| 32 | |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame] | 33 | #include <boost/algorithm/string/trim.hpp> |
Davide Pesavento | e245b05 | 2017-10-31 13:00:44 -0400 | [diff] [blame] | 34 | #include <cstring> |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 35 | #include <sstream> |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 37 | namespace ndn { |
| 38 | namespace name { |
| 39 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 40 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Component>)); |
| 41 | BOOST_CONCEPT_ASSERT((WireEncodable<Component>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 42 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Component>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 43 | BOOST_CONCEPT_ASSERT((WireDecodable<Component>)); |
| 44 | static_assert(std::is_base_of<tlv::Error, Component::Error>::value, |
| 45 | "name::Component::Error must inherit from tlv::Error"); |
| 46 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 47 | static const std::string& |
| 48 | getSha256DigestUriPrefix() |
| 49 | { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 50 | static const std::string prefix{"sha256digest="}; |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 51 | return prefix; |
| 52 | } |
| 53 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 54 | void |
| 55 | Component::ensureValid() const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 56 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 57 | if (type() < tlv::NameComponentMin || type() > tlv::NameComponentMax) { |
| 58 | BOOST_THROW_EXCEPTION(Error("TLV-TYPE " + to_string(type()) + " is not a valid NameComponent")); |
| 59 | } |
| 60 | if (type() == tlv::ImplicitSha256DigestComponent && value_size() != util::Sha256::DIGEST_SIZE) { |
| 61 | BOOST_THROW_EXCEPTION(Error("ImplicitSha256DigestComponent TLV-LENGTH must be " + |
| 62 | to_string(util::Sha256::DIGEST_SIZE))); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | Component::Component(uint32_t type) |
| 67 | : Block(type) |
| 68 | { |
| 69 | ensureValid(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | Component::Component(const Block& wire) |
| 73 | : Block(wire) |
| 74 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 75 | ensureValid(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 78 | Component::Component(uint32_t type, ConstBufferPtr buffer) |
| 79 | : Block(type, std::move(buffer)) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 80 | { |
| 81 | } |
| 82 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 83 | Component::Component(uint32_t type, const uint8_t* value, size_t valueLen) |
| 84 | : Block(makeBinaryBlock(type, value, valueLen)) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 85 | { |
| 86 | } |
| 87 | |
| 88 | Component::Component(const char* str) |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 89 | : Block(makeBinaryBlock(tlv::GenericNameComponent, str, std::char_traits<char>::length(str))) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 90 | { |
| 91 | } |
| 92 | |
| 93 | Component::Component(const std::string& str) |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 94 | : Block(makeStringBlock(tlv::GenericNameComponent, str)) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 95 | { |
| 96 | } |
| 97 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 98 | static Component |
| 99 | parseSha256DigestUri(std::string input) |
| 100 | { |
| 101 | input.erase(0, getSha256DigestUriPrefix().size()); |
| 102 | |
| 103 | try { |
| 104 | return Component::fromImplicitSha256Digest(fromHex(input)); |
| 105 | } |
| 106 | catch (const StringHelperError&) { |
| 107 | BOOST_THROW_EXCEPTION(Component::Error("Cannot convert to a ImplicitSha256DigestComponent " |
| 108 | "(invalid hex encoding)")); |
| 109 | } |
| 110 | } |
| 111 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 112 | Component |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 113 | Component::fromEscapedString(std::string input) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 114 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 115 | boost::algorithm::trim(input); |
| 116 | uint32_t type = tlv::GenericNameComponent; |
| 117 | size_t equalPos = input.find('='); |
| 118 | if (equalPos != std::string::npos) { |
| 119 | if (equalPos + 1 == getSha256DigestUriPrefix().size() && |
| 120 | input.compare(0, getSha256DigestUriPrefix().size(), getSha256DigestUriPrefix()) == 0) { |
| 121 | return parseSha256DigestUri(std::move(input)); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 122 | } |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 123 | |
| 124 | long parsedType = std::strtol(input.data(), nullptr, 10); |
| 125 | if (parsedType < tlv::NameComponentMin || parsedType > tlv::NameComponentMax || |
| 126 | parsedType == tlv::ImplicitSha256DigestComponent || parsedType == tlv::GenericNameComponent || |
| 127 | to_string(parsedType).size() != equalPos) { |
| 128 | BOOST_THROW_EXCEPTION(Error("Incorrect TLV-TYPE in NameComponent URI")); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 129 | } |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 130 | type = static_cast<uint32_t>(parsedType); |
| 131 | input.erase(0, equalPos + 1); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 132 | } |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 133 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 134 | std::string value = unescape(input); |
| 135 | if (value.find_first_not_of('.') == std::string::npos) { // all periods |
| 136 | if (value.size() < 3) { |
| 137 | BOOST_THROW_EXCEPTION(Error("Illegal URI (name component cannot be . or ..)")); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 138 | } |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 139 | return Component(type, reinterpret_cast<const uint8_t*>(value.data()), value.size() - 3); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 140 | } |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 141 | return Component(type, reinterpret_cast<const uint8_t*>(value.data()), value.size()); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 144 | void |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 145 | Component::toUri(std::ostream& os) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 146 | { |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 147 | if (type() == tlv::ImplicitSha256DigestComponent) { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 148 | os << getSha256DigestUriPrefix(); |
| 149 | printHex(os, value(), value_size(), false); |
| 150 | return; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 151 | } |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 152 | |
| 153 | if (type() != tlv::GenericNameComponent) { |
| 154 | os << type() << '='; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 155 | } |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 156 | |
| 157 | if (std::all_of(value_begin(), value_end(), [] (uint8_t x) { return x == '.'; })) { // all periods |
| 158 | os << "..."; |
| 159 | } |
| 160 | |
| 161 | escape(os, reinterpret_cast<const char*>(value()), value_size()); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | std::string |
| 165 | Component::toUri() const |
| 166 | { |
| 167 | std::ostringstream os; |
| 168 | toUri(os); |
| 169 | return os.str(); |
| 170 | } |
| 171 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 172 | //////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 173 | |
| 174 | bool |
| 175 | Component::isNumber() const |
| 176 | { |
| 177 | return (value_size() == 1 || value_size() == 2 || |
| 178 | value_size() == 4 || value_size() == 8); |
| 179 | } |
| 180 | |
| 181 | bool |
| 182 | Component::isNumberWithMarker(uint8_t marker) const |
| 183 | { |
| 184 | return (!empty() && value()[0] == marker && |
| 185 | (value_size() == 2 || value_size() == 3 || |
| 186 | value_size() == 5 || value_size() == 9)); |
| 187 | } |
| 188 | |
| 189 | bool |
| 190 | Component::isVersion() const |
| 191 | { |
| 192 | return isNumberWithMarker(VERSION_MARKER); |
| 193 | } |
| 194 | |
| 195 | bool |
| 196 | Component::isSegment() const |
| 197 | { |
| 198 | return isNumberWithMarker(SEGMENT_MARKER); |
| 199 | } |
| 200 | |
| 201 | bool |
| 202 | Component::isSegmentOffset() const |
| 203 | { |
| 204 | return isNumberWithMarker(SEGMENT_OFFSET_MARKER); |
| 205 | } |
| 206 | |
| 207 | bool |
| 208 | Component::isTimestamp() const |
| 209 | { |
| 210 | return isNumberWithMarker(TIMESTAMP_MARKER); |
| 211 | } |
| 212 | |
| 213 | bool |
| 214 | Component::isSequenceNumber() const |
| 215 | { |
| 216 | return isNumberWithMarker(SEQUENCE_NUMBER_MARKER); |
| 217 | } |
| 218 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 219 | //////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 220 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 221 | uint64_t |
| 222 | Component::toNumber() const |
| 223 | { |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 224 | if (!isNumber()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 225 | BOOST_THROW_EXCEPTION(Error("Name component does not have nonNegativeInteger value")); |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 226 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 227 | return readNonNegativeInteger(*this); |
| 228 | } |
| 229 | |
| 230 | uint64_t |
| 231 | Component::toNumberWithMarker(uint8_t marker) const |
| 232 | { |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 233 | if (!isNumberWithMarker(marker)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 234 | BOOST_THROW_EXCEPTION(Error("Name component does not have the requested marker " |
| 235 | "or the value is not a nonNegativeInteger")); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 236 | |
| 237 | Buffer::const_iterator valueBegin = value_begin() + 1; |
| 238 | return tlv::readNonNegativeInteger(value_size() - 1, valueBegin, value_end()); |
| 239 | } |
| 240 | |
| 241 | uint64_t |
| 242 | Component::toVersion() const |
| 243 | { |
| 244 | return toNumberWithMarker(VERSION_MARKER); |
| 245 | } |
| 246 | |
| 247 | uint64_t |
| 248 | Component::toSegment() const |
| 249 | { |
| 250 | return toNumberWithMarker(SEGMENT_MARKER); |
| 251 | } |
| 252 | |
| 253 | uint64_t |
| 254 | Component::toSegmentOffset() const |
| 255 | { |
| 256 | return toNumberWithMarker(SEGMENT_OFFSET_MARKER); |
| 257 | } |
| 258 | |
| 259 | time::system_clock::TimePoint |
| 260 | Component::toTimestamp() const |
| 261 | { |
| 262 | uint64_t value = toNumberWithMarker(TIMESTAMP_MARKER); |
| 263 | return time::getUnixEpoch() + time::microseconds(value); |
| 264 | } |
| 265 | |
| 266 | uint64_t |
| 267 | Component::toSequenceNumber() const |
| 268 | { |
| 269 | return toNumberWithMarker(SEQUENCE_NUMBER_MARKER); |
| 270 | } |
| 271 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 272 | //////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 273 | |
| 274 | Component |
| 275 | Component::fromNumber(uint64_t number) |
| 276 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 277 | return makeNonNegativeIntegerBlock(tlv::GenericNameComponent, number); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | Component |
| 281 | Component::fromNumberWithMarker(uint8_t marker, uint64_t number) |
| 282 | { |
| 283 | EncodingEstimator estimator; |
| 284 | |
| 285 | size_t valueLength = estimator.prependNonNegativeInteger(number); |
| 286 | valueLength += estimator.prependByteArray(&marker, 1); |
| 287 | size_t totalLength = valueLength; |
| 288 | totalLength += estimator.prependVarNumber(valueLength); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 289 | totalLength += estimator.prependVarNumber(tlv::GenericNameComponent); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 290 | |
| 291 | EncodingBuffer encoder(totalLength, 0); |
| 292 | encoder.prependNonNegativeInteger(number); |
| 293 | encoder.prependByteArray(&marker, 1); |
| 294 | encoder.prependVarNumber(valueLength); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 295 | encoder.prependVarNumber(tlv::GenericNameComponent); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 296 | |
| 297 | return encoder.block(); |
| 298 | } |
| 299 | |
| 300 | Component |
| 301 | Component::fromVersion(uint64_t version) |
| 302 | { |
| 303 | return fromNumberWithMarker(VERSION_MARKER, version); |
| 304 | } |
| 305 | |
| 306 | Component |
| 307 | Component::fromSegment(uint64_t segmentNo) |
| 308 | { |
| 309 | return fromNumberWithMarker(SEGMENT_MARKER, segmentNo); |
| 310 | } |
| 311 | |
| 312 | Component |
| 313 | Component::fromSegmentOffset(uint64_t offset) |
| 314 | { |
| 315 | return fromNumberWithMarker(SEGMENT_OFFSET_MARKER, offset); |
| 316 | } |
| 317 | |
| 318 | Component |
| 319 | Component::fromTimestamp(const time::system_clock::TimePoint& timePoint) |
| 320 | { |
| 321 | using namespace time; |
| 322 | uint64_t value = duration_cast<microseconds>(timePoint - getUnixEpoch()).count(); |
| 323 | return fromNumberWithMarker(TIMESTAMP_MARKER, value); |
| 324 | } |
| 325 | |
| 326 | Component |
| 327 | Component::fromSequenceNumber(uint64_t seqNo) |
| 328 | { |
| 329 | return fromNumberWithMarker(SEQUENCE_NUMBER_MARKER, seqNo); |
| 330 | } |
| 331 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 332 | //////////////////////////////////////////////////////////////////////////////// |
| 333 | |
| 334 | bool |
| 335 | Component::isGeneric() const |
| 336 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 337 | return type() == tlv::GenericNameComponent; |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | bool |
| 341 | Component::isImplicitSha256Digest() const |
| 342 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 343 | return type() == tlv::ImplicitSha256DigestComponent && |
| 344 | value_size() == util::Sha256::DIGEST_SIZE; |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | Component |
| 348 | Component::fromImplicitSha256Digest(const ConstBufferPtr& digest) |
| 349 | { |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 350 | if (digest->size() != util::Sha256::DIGEST_SIZE) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 351 | BOOST_THROW_EXCEPTION(Error("Cannot create ImplicitSha256DigestComponent (input digest must be " + |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 352 | to_string(util::Sha256::DIGEST_SIZE) + " octets)")); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 353 | |
| 354 | return Block(tlv::ImplicitSha256DigestComponent, digest); |
| 355 | } |
| 356 | |
| 357 | Component |
| 358 | Component::fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize) |
| 359 | { |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 360 | if (digestSize != util::Sha256::DIGEST_SIZE) |
Davide Pesavento | 96b96af | 2015-09-19 23:00:40 +0200 | [diff] [blame] | 361 | BOOST_THROW_EXCEPTION(Error("Cannot create ImplicitSha256DigestComponent (input digest must be " + |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 362 | to_string(util::Sha256::DIGEST_SIZE) + " octets)")); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 363 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 364 | return makeBinaryBlock(tlv::ImplicitSha256DigestComponent, digest, digestSize); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | //////////////////////////////////////////////////////////////////////////////// |
| 368 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 369 | bool |
| 370 | Component::equals(const Component& other) const |
| 371 | { |
| 372 | return type() == other.type() && |
| 373 | value_size() == other.value_size() && |
Davide Pesavento | e245b05 | 2017-10-31 13:00:44 -0400 | [diff] [blame] | 374 | (empty() || // needed with Apple clang < 9.0.0 due to libc++ bug |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 375 | std::equal(value_begin(), value_end(), other.value_begin())); |
| 376 | } |
| 377 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 378 | int |
| 379 | Component::compare(const Component& other) const |
| 380 | { |
Junxiao Shi | 010f086 | 2016-10-11 21:08:32 +0000 | [diff] [blame] | 381 | if (this->hasWire() && other.hasWire()) { |
| 382 | // In the common case where both components have wire encoding, |
| 383 | // it's more efficient to simply compare the wire encoding. |
| 384 | // This works because lexical order of TLV encoding happens to be |
| 385 | // the same as canonical order of the value. |
| 386 | return std::memcmp(wire(), other.wire(), std::min(size(), other.size())); |
| 387 | } |
| 388 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 389 | int cmpType = type() - other.type(); |
| 390 | if (cmpType != 0) |
| 391 | return cmpType; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 392 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 393 | int cmpSize = value_size() - other.value_size(); |
| 394 | if (cmpSize != 0) |
| 395 | return cmpSize; |
| 396 | |
Davide Pesavento | e245b05 | 2017-10-31 13:00:44 -0400 | [diff] [blame] | 397 | if (empty()) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 398 | return 0; |
| 399 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 400 | return std::memcmp(value(), other.value(), value_size()); |
| 401 | } |
| 402 | |
| 403 | Component |
| 404 | Component::getSuccessor() const |
| 405 | { |
| 406 | size_t totalLength = 0; |
| 407 | EncodingBuffer encoder(size() + 1, 1); // + 1 in case there is an overflow |
| 408 | // in unlikely case TLV length increases, |
| 409 | // EncodingBuffer will take care of that |
| 410 | |
| 411 | bool isOverflow = true; |
| 412 | size_t i = value_size(); |
| 413 | for (; isOverflow && i > 0; i--) { |
| 414 | uint8_t newValue = static_cast<uint8_t>((value()[i - 1] + 1) & 0xFF); |
| 415 | totalLength += encoder.prependByte(newValue); |
| 416 | isOverflow = (newValue == 0); |
| 417 | } |
| 418 | totalLength += encoder.prependByteArray(value(), i); |
| 419 | |
| 420 | if (isOverflow) { |
| 421 | // new name components has to be extended |
| 422 | totalLength += encoder.appendByte(0); |
| 423 | } |
| 424 | |
Davide Pesavento | 9bd4d98 | 2015-05-13 14:31:19 +0200 | [diff] [blame] | 425 | encoder.prependVarNumber(totalLength); |
| 426 | encoder.prependVarNumber(type()); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 427 | |
| 428 | return encoder.block(); |
| 429 | } |
| 430 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 431 | template<encoding::Tag TAG> |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 432 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 433 | Component::wireEncode(EncodingImpl<TAG>& encoder) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 434 | { |
| 435 | size_t totalLength = 0; |
| 436 | if (value_size() > 0) |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 437 | totalLength += encoder.prependByteArray(value(), value_size()); |
| 438 | totalLength += encoder.prependVarNumber(value_size()); |
| 439 | totalLength += encoder.prependVarNumber(type()); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 440 | return totalLength; |
| 441 | } |
| 442 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 443 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Component); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 444 | |
| 445 | const Block& |
| 446 | Component::wireEncode() const |
| 447 | { |
| 448 | if (this->hasWire()) |
| 449 | return *this; |
| 450 | |
| 451 | EncodingEstimator estimator; |
| 452 | size_t estimatedSize = wireEncode(estimator); |
| 453 | |
| 454 | EncodingBuffer buffer(estimatedSize, 0); |
| 455 | wireEncode(buffer); |
| 456 | |
| 457 | const_cast<Component&>(*this) = buffer.block(); |
| 458 | return *this; |
| 459 | } |
| 460 | |
| 461 | void |
| 462 | Component::wireDecode(const Block& wire) |
| 463 | { |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 464 | *this = wire; |
Qiuhan Ding | 2c3cbe4 | 2014-11-25 18:10:23 -0800 | [diff] [blame] | 465 | // validity check is done within Component(const Block& wire) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | } // namespace name |
| 469 | } // namespace ndn |