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