blob: 136fb6434929e476edd2efcdb2fd440688ccc8f8 [file] [log] [blame]
Alexander Afanasyev15f67312014-07-22 15:11:09 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento10b24be2017-07-12 23:23:46 -04002/*
Davide Pesavento08378cb2018-02-01 16:10:54 -05003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyev15f67312014-07-22 15:11:09 -07004 *
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 Shi6938e342017-07-25 21:56:58 +000030#include "util/sha256.hpp"
Alexander Afanasyev15f67312014-07-22 15:11:09 -070031#include "util/string-helper.hpp"
32
Davide Pesaventodd461432017-01-28 21:47:26 -050033#include <boost/algorithm/string/trim.hpp>
Davide Pesaventoe245b052017-10-31 13:00:44 -040034#include <cstring>
Davide Pesaventoa84f4642017-08-23 16:14:51 -040035#include <sstream>
Alexander Afanasyev6486d522014-10-23 14:14:11 -070036
Alexander Afanasyev15f67312014-07-22 15:11:09 -070037namespace ndn {
38namespace name {
39
Junxiao Shic2b8d242014-11-04 08:35:29 -070040BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Component>));
41BOOST_CONCEPT_ASSERT((WireEncodable<Component>));
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070042BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Component>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070043BOOST_CONCEPT_ASSERT((WireDecodable<Component>));
44static_assert(std::is_base_of<tlv::Error, Component::Error>::value,
45 "name::Component::Error must inherit from tlv::Error");
46
Alexander Afanasyev6486d522014-10-23 14:14:11 -070047static const std::string&
48getSha256DigestUriPrefix()
49{
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070050 static const std::string prefix{"sha256digest="};
Alexander Afanasyev6486d522014-10-23 14:14:11 -070051 return prefix;
52}
53
Alexander Afanasyev15f67312014-07-22 15:11:09 -070054Component::Component()
55 : Block(tlv::NameComponent)
56{
57}
58
59Component::Component(const Block& wire)
60 : Block(wire)
61{
Qiuhan Ding2c3cbe42014-11-25 18:10:23 -080062 if (!isGeneric() && !isImplicitSha256Digest())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070063 BOOST_THROW_EXCEPTION(Error("Cannot construct name::Component from not a NameComponent "
64 "or ImplicitSha256DigestComponent TLV wire block"));
Alexander Afanasyev15f67312014-07-22 15:11:09 -070065}
66
67Component::Component(const ConstBufferPtr& buffer)
68 : Block(tlv::NameComponent, buffer)
69{
70}
71
72Component::Component(const Buffer& value)
Davide Pesavento5d0b0102017-10-07 13:43:16 -040073 : Block(makeBinaryBlock(tlv::NameComponent, value.data(), value.size()))
Alexander Afanasyev15f67312014-07-22 15:11:09 -070074{
75}
76
77Component::Component(const uint8_t* value, size_t valueLen)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070078 : Block(makeBinaryBlock(tlv::NameComponent, value, valueLen))
Alexander Afanasyev15f67312014-07-22 15:11:09 -070079{
80}
81
82Component::Component(const char* str)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070083 : Block(makeBinaryBlock(tlv::NameComponent, str, std::char_traits<char>::length(str)))
Alexander Afanasyev15f67312014-07-22 15:11:09 -070084{
85}
86
87Component::Component(const std::string& str)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070088 : Block(makeStringBlock(tlv::NameComponent, str))
Alexander Afanasyev15f67312014-07-22 15:11:09 -070089{
90}
91
Alexander Afanasyev15f67312014-07-22 15:11:09 -070092Component
93Component::fromEscapedString(const char* escapedString, size_t beginOffset, size_t endOffset)
94{
95 std::string trimmedString(escapedString + beginOffset, escapedString + endOffset);
Davide Pesaventodd461432017-01-28 21:47:26 -050096 boost::algorithm::trim(trimmedString);
Alexander Afanasyev15f67312014-07-22 15:11:09 -070097
Alexander Afanasyev6486d522014-10-23 14:14:11 -070098 if (trimmedString.compare(0, getSha256DigestUriPrefix().size(),
99 getSha256DigestUriPrefix()) == 0) {
Davide Pesavento10b24be2017-07-12 23:23:46 -0400100 if (trimmedString.size() != getSha256DigestUriPrefix().size() + util::Sha256::DIGEST_SIZE * 2)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700101 BOOST_THROW_EXCEPTION(Error("Cannot convert to ImplicitSha256DigestComponent"
102 "(expected sha256 in hex encoding)"));
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700103
104 try {
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700105 trimmedString.erase(0, getSha256DigestUriPrefix().size());
106 return fromImplicitSha256Digest(fromHex(trimmedString));
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700107 }
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500108 catch (const StringHelperError&) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700109 BOOST_THROW_EXCEPTION(Error("Cannot convert to a ImplicitSha256DigestComponent (invalid hex "
110 "encoding)"));
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700111 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700112 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700113 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 Mastorakis0d2ed2e2015-07-27 19:09:12 -0700120 BOOST_THROW_EXCEPTION(Error("Illegal URI (name component cannot be . or ..)"));
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700121 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 Afanasyev15f67312014-07-22 15:11:09 -0700128}
129
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700130void
131Component::toUri(std::ostream& result) const
132{
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700133 if (type() == tlv::ImplicitSha256DigestComponent) {
134 result << getSha256DigestUriPrefix();
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700135 printHex(result, value(), value_size(), false);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700136 }
137 else {
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700138 const uint8_t* value = this->value();
139 size_t valueSize = value_size();
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700140
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700141 bool gotNonDot = false;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700142 for (size_t i = 0; i < valueSize; ++i) {
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700143 if (value[i] != 0x2e) {
144 gotNonDot = true;
145 break;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700146 }
147 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700148 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 Pesaventoe78eeca2017-02-23 23:22:32 -0500156 auto savedFlags = result.flags(std::ios::hex | std::ios::uppercase);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700157
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700158 for (size_t i = 0; i < valueSize; ++i) {
159 uint8_t x = value[i];
Davide Pesavento08378cb2018-02-01 16:10:54 -0500160 // 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 Afanasyev6486d522014-10-23 14:14:11 -0700166 result << x;
Davide Pesavento08378cb2018-02-01 16:10:54 -0500167 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700168 else {
169 result << '%';
170 if (x < 16)
171 result << '0';
Davide Pesavento08378cb2018-02-01 16:10:54 -0500172 result << static_cast<int>(x);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700173 }
174 }
175
176 // Restore.
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500177 result.flags(savedFlags);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700178 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700179 }
180}
181
182std::string
183Component::toUri() const
184{
185 std::ostringstream os;
186 toUri(os);
187 return os.str();
188}
189
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700190////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700191
192bool
193Component::isNumber() const
194{
195 return (value_size() == 1 || value_size() == 2 ||
196 value_size() == 4 || value_size() == 8);
197}
198
199bool
200Component::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
207bool
208Component::isVersion() const
209{
210 return isNumberWithMarker(VERSION_MARKER);
211}
212
213bool
214Component::isSegment() const
215{
216 return isNumberWithMarker(SEGMENT_MARKER);
217}
218
219bool
220Component::isSegmentOffset() const
221{
222 return isNumberWithMarker(SEGMENT_OFFSET_MARKER);
223}
224
225bool
226Component::isTimestamp() const
227{
228 return isNumberWithMarker(TIMESTAMP_MARKER);
229}
230
231bool
232Component::isSequenceNumber() const
233{
234 return isNumberWithMarker(SEQUENCE_NUMBER_MARKER);
235}
236
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700237////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700238
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700239uint64_t
240Component::toNumber() const
241{
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700242 if (!isNumber())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700243 BOOST_THROW_EXCEPTION(Error("Name component does not have nonNegativeInteger value"));
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700244
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700245 return readNonNegativeInteger(*this);
246}
247
248uint64_t
249Component::toNumberWithMarker(uint8_t marker) const
250{
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700251 if (!isNumberWithMarker(marker))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700252 BOOST_THROW_EXCEPTION(Error("Name component does not have the requested marker "
253 "or the value is not a nonNegativeInteger"));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700254
255 Buffer::const_iterator valueBegin = value_begin() + 1;
256 return tlv::readNonNegativeInteger(value_size() - 1, valueBegin, value_end());
257}
258
259uint64_t
260Component::toVersion() const
261{
262 return toNumberWithMarker(VERSION_MARKER);
263}
264
265uint64_t
266Component::toSegment() const
267{
268 return toNumberWithMarker(SEGMENT_MARKER);
269}
270
271uint64_t
272Component::toSegmentOffset() const
273{
274 return toNumberWithMarker(SEGMENT_OFFSET_MARKER);
275}
276
277time::system_clock::TimePoint
278Component::toTimestamp() const
279{
280 uint64_t value = toNumberWithMarker(TIMESTAMP_MARKER);
281 return time::getUnixEpoch() + time::microseconds(value);
282}
283
284uint64_t
285Component::toSequenceNumber() const
286{
287 return toNumberWithMarker(SEQUENCE_NUMBER_MARKER);
288}
289
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700290////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700291
292Component
293Component::fromNumber(uint64_t number)
294{
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700295 return makeNonNegativeIntegerBlock(tlv::NameComponent, number);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700296}
297
298Component
299Component::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
318Component
319Component::fromVersion(uint64_t version)
320{
321 return fromNumberWithMarker(VERSION_MARKER, version);
322}
323
324Component
325Component::fromSegment(uint64_t segmentNo)
326{
327 return fromNumberWithMarker(SEGMENT_MARKER, segmentNo);
328}
329
330Component
331Component::fromSegmentOffset(uint64_t offset)
332{
333 return fromNumberWithMarker(SEGMENT_OFFSET_MARKER, offset);
334}
335
336Component
337Component::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
344Component
345Component::fromSequenceNumber(uint64_t seqNo)
346{
347 return fromNumberWithMarker(SEQUENCE_NUMBER_MARKER, seqNo);
348}
349
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700350////////////////////////////////////////////////////////////////////////////////
351
352bool
353Component::isGeneric() const
354{
355 return (type() == tlv::NameComponent);
356}
357
358bool
359Component::isImplicitSha256Digest() const
360{
361 return (type() == tlv::ImplicitSha256DigestComponent &&
Davide Pesavento10b24be2017-07-12 23:23:46 -0400362 value_size() == util::Sha256::DIGEST_SIZE);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700363}
364
365Component
366Component::fromImplicitSha256Digest(const ConstBufferPtr& digest)
367{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400368 if (digest->size() != util::Sha256::DIGEST_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700369 BOOST_THROW_EXCEPTION(Error("Cannot create ImplicitSha256DigestComponent (input digest must be " +
Davide Pesavento10b24be2017-07-12 23:23:46 -0400370 to_string(util::Sha256::DIGEST_SIZE) + " octets)"));
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700371
372 return Block(tlv::ImplicitSha256DigestComponent, digest);
373}
374
375Component
376Component::fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize)
377{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400378 if (digestSize != util::Sha256::DIGEST_SIZE)
Davide Pesavento96b96af2015-09-19 23:00:40 +0200379 BOOST_THROW_EXCEPTION(Error("Cannot create ImplicitSha256DigestComponent (input digest must be " +
Davide Pesavento10b24be2017-07-12 23:23:46 -0400380 to_string(util::Sha256::DIGEST_SIZE) + " octets)"));
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700381
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700382 return makeBinaryBlock(tlv::ImplicitSha256DigestComponent, digest, digestSize);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700383}
384
385////////////////////////////////////////////////////////////////////////////////
386
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000387bool
388Component::equals(const Component& other) const
389{
390 return type() == other.type() &&
391 value_size() == other.value_size() &&
Davide Pesaventoe245b052017-10-31 13:00:44 -0400392 (empty() || // needed with Apple clang < 9.0.0 due to libc++ bug
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000393 std::equal(value_begin(), value_end(), other.value_begin()));
394}
395
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700396int
397Component::compare(const Component& other) const
398{
Junxiao Shi010f0862016-10-11 21:08:32 +0000399 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 Shidf4b24e2016-07-14 21:41:43 +0000407 int cmpType = type() - other.type();
408 if (cmpType != 0)
409 return cmpType;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700410
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000411 int cmpSize = value_size() - other.value_size();
412 if (cmpSize != 0)
413 return cmpSize;
414
Davide Pesaventoe245b052017-10-31 13:00:44 -0400415 if (empty())
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700416 return 0;
417
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700418 return std::memcmp(value(), other.value(), value_size());
419}
420
421Component
422Component::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 Pesavento9bd4d982015-05-13 14:31:19 +0200443 encoder.prependVarNumber(totalLength);
444 encoder.prependVarNumber(type());
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700445
446 return encoder.block();
447}
448
Alexander Afanasyev74633892015-02-08 18:08:46 -0800449template<encoding::Tag TAG>
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700450size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700451Component::wireEncode(EncodingImpl<TAG>& encoder) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700452{
453 size_t totalLength = 0;
454 if (value_size() > 0)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700455 totalLength += encoder.prependByteArray(value(), value_size());
456 totalLength += encoder.prependVarNumber(value_size());
457 totalLength += encoder.prependVarNumber(type());
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700458 return totalLength;
459}
460
Davide Pesavento88a0d812017-08-19 21:31:42 -0400461NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Component);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700462
463const Block&
464Component::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
479void
480Component::wireDecode(const Block& wire)
481{
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700482 *this = wire;
Qiuhan Ding2c3cbe42014-11-25 18:10:23 -0800483 // validity check is done within Component(const Block& wire)
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700484}
485
486} // namespace name
487} // namespace ndn