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