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