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