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