blob: 36a01f9ec0fb91746863e8f13e0c65fe46d6b2c3 [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 Pesaventodd461432017-01-28 21:47:26 -05003 * Copyright (c) 2013-2017 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
130
131void
132Component::toUri(std::ostream& result) const
133{
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700134 if (type() == tlv::ImplicitSha256DigestComponent) {
135 result << getSha256DigestUriPrefix();
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700136
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700137 printHex(result, value(), value_size(), false);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700138 }
139 else {
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700140 const uint8_t* value = this->value();
141 size_t valueSize = value_size();
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700142
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700143 bool gotNonDot = false;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700144 for (size_t i = 0; i < valueSize; ++i) {
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700145 if (value[i] != 0x2e) {
146 gotNonDot = true;
147 break;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700148 }
149 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700150 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.
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500158 auto savedFlags = result.flags(std::ios::hex | std::ios::uppercase);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700159
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700160 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.
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500176 result.flags(savedFlags);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700177 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700178 }
179}
180
181std::string
182Component::toUri() const
183{
184 std::ostringstream os;
185 toUri(os);
186 return os.str();
187}
188
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700189////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700190
191bool
192Component::isNumber() const
193{
194 return (value_size() == 1 || value_size() == 2 ||
195 value_size() == 4 || value_size() == 8);
196}
197
198bool
199Component::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
206bool
207Component::isVersion() const
208{
209 return isNumberWithMarker(VERSION_MARKER);
210}
211
212bool
213Component::isSegment() const
214{
215 return isNumberWithMarker(SEGMENT_MARKER);
216}
217
218bool
219Component::isSegmentOffset() const
220{
221 return isNumberWithMarker(SEGMENT_OFFSET_MARKER);
222}
223
224bool
225Component::isTimestamp() const
226{
227 return isNumberWithMarker(TIMESTAMP_MARKER);
228}
229
230bool
231Component::isSequenceNumber() const
232{
233 return isNumberWithMarker(SEQUENCE_NUMBER_MARKER);
234}
235
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700236////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700237
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700238uint64_t
239Component::toNumber() const
240{
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700241 if (!isNumber())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700242 BOOST_THROW_EXCEPTION(Error("Name component does not have nonNegativeInteger value"));
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700243
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700244 return readNonNegativeInteger(*this);
245}
246
247uint64_t
248Component::toNumberWithMarker(uint8_t marker) const
249{
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700250 if (!isNumberWithMarker(marker))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700251 BOOST_THROW_EXCEPTION(Error("Name component does not have the requested marker "
252 "or the value is not a nonNegativeInteger"));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700253
254 Buffer::const_iterator valueBegin = value_begin() + 1;
255 return tlv::readNonNegativeInteger(value_size() - 1, valueBegin, value_end());
256}
257
258uint64_t
259Component::toVersion() const
260{
261 return toNumberWithMarker(VERSION_MARKER);
262}
263
264uint64_t
265Component::toSegment() const
266{
267 return toNumberWithMarker(SEGMENT_MARKER);
268}
269
270uint64_t
271Component::toSegmentOffset() const
272{
273 return toNumberWithMarker(SEGMENT_OFFSET_MARKER);
274}
275
276time::system_clock::TimePoint
277Component::toTimestamp() const
278{
279 uint64_t value = toNumberWithMarker(TIMESTAMP_MARKER);
280 return time::getUnixEpoch() + time::microseconds(value);
281}
282
283uint64_t
284Component::toSequenceNumber() const
285{
286 return toNumberWithMarker(SEQUENCE_NUMBER_MARKER);
287}
288
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700289////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700290
291Component
292Component::fromNumber(uint64_t number)
293{
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700294 return makeNonNegativeIntegerBlock(tlv::NameComponent, number);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700295}
296
297Component
298Component::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
317Component
318Component::fromVersion(uint64_t version)
319{
320 return fromNumberWithMarker(VERSION_MARKER, version);
321}
322
323Component
324Component::fromSegment(uint64_t segmentNo)
325{
326 return fromNumberWithMarker(SEGMENT_MARKER, segmentNo);
327}
328
329Component
330Component::fromSegmentOffset(uint64_t offset)
331{
332 return fromNumberWithMarker(SEGMENT_OFFSET_MARKER, offset);
333}
334
335Component
336Component::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
343Component
344Component::fromSequenceNumber(uint64_t seqNo)
345{
346 return fromNumberWithMarker(SEQUENCE_NUMBER_MARKER, seqNo);
347}
348
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700349////////////////////////////////////////////////////////////////////////////////
350
351bool
352Component::isGeneric() const
353{
354 return (type() == tlv::NameComponent);
355}
356
357bool
358Component::isImplicitSha256Digest() const
359{
360 return (type() == tlv::ImplicitSha256DigestComponent &&
Davide Pesavento10b24be2017-07-12 23:23:46 -0400361 value_size() == util::Sha256::DIGEST_SIZE);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700362}
363
364Component
365Component::fromImplicitSha256Digest(const ConstBufferPtr& digest)
366{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400367 if (digest->size() != util::Sha256::DIGEST_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700368 BOOST_THROW_EXCEPTION(Error("Cannot create ImplicitSha256DigestComponent (input digest must be " +
Davide Pesavento10b24be2017-07-12 23:23:46 -0400369 to_string(util::Sha256::DIGEST_SIZE) + " octets)"));
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700370
371 return Block(tlv::ImplicitSha256DigestComponent, digest);
372}
373
374Component
375Component::fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize)
376{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400377 if (digestSize != util::Sha256::DIGEST_SIZE)
Davide Pesavento96b96af2015-09-19 23:00:40 +0200378 BOOST_THROW_EXCEPTION(Error("Cannot create ImplicitSha256DigestComponent (input digest must be " +
Davide Pesavento10b24be2017-07-12 23:23:46 -0400379 to_string(util::Sha256::DIGEST_SIZE) + " octets)"));
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700380
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700381 return makeBinaryBlock(tlv::ImplicitSha256DigestComponent, digest, digestSize);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700382}
383
384////////////////////////////////////////////////////////////////////////////////
385
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000386bool
387Component::equals(const Component& other) const
388{
389 return type() == other.type() &&
390 value_size() == other.value_size() &&
Davide Pesaventoe245b052017-10-31 13:00:44 -0400391 (empty() || // needed with Apple clang < 9.0.0 due to libc++ bug
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000392 std::equal(value_begin(), value_end(), other.value_begin()));
393}
394
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700395int
396Component::compare(const Component& other) const
397{
Junxiao Shi010f0862016-10-11 21:08:32 +0000398 if (this->hasWire() && other.hasWire()) {
399 // In the common case where both components have wire encoding,
400 // it's more efficient to simply compare the wire encoding.
401 // This works because lexical order of TLV encoding happens to be
402 // the same as canonical order of the value.
403 return std::memcmp(wire(), other.wire(), std::min(size(), other.size()));
404 }
405
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000406 int cmpType = type() - other.type();
407 if (cmpType != 0)
408 return cmpType;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700409
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000410 int cmpSize = value_size() - other.value_size();
411 if (cmpSize != 0)
412 return cmpSize;
413
Davide Pesaventoe245b052017-10-31 13:00:44 -0400414 if (empty())
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700415 return 0;
416
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700417 return std::memcmp(value(), other.value(), value_size());
418}
419
420Component
421Component::getSuccessor() const
422{
423 size_t totalLength = 0;
424 EncodingBuffer encoder(size() + 1, 1); // + 1 in case there is an overflow
425 // in unlikely case TLV length increases,
426 // EncodingBuffer will take care of that
427
428 bool isOverflow = true;
429 size_t i = value_size();
430 for (; isOverflow && i > 0; i--) {
431 uint8_t newValue = static_cast<uint8_t>((value()[i - 1] + 1) & 0xFF);
432 totalLength += encoder.prependByte(newValue);
433 isOverflow = (newValue == 0);
434 }
435 totalLength += encoder.prependByteArray(value(), i);
436
437 if (isOverflow) {
438 // new name components has to be extended
439 totalLength += encoder.appendByte(0);
440 }
441
Davide Pesavento9bd4d982015-05-13 14:31:19 +0200442 encoder.prependVarNumber(totalLength);
443 encoder.prependVarNumber(type());
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700444
445 return encoder.block();
446}
447
Alexander Afanasyev74633892015-02-08 18:08:46 -0800448template<encoding::Tag TAG>
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700449size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700450Component::wireEncode(EncodingImpl<TAG>& encoder) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700451{
452 size_t totalLength = 0;
453 if (value_size() > 0)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700454 totalLength += encoder.prependByteArray(value(), value_size());
455 totalLength += encoder.prependVarNumber(value_size());
456 totalLength += encoder.prependVarNumber(type());
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700457 return totalLength;
458}
459
Davide Pesavento88a0d812017-08-19 21:31:42 -0400460NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Component);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700461
462const Block&
463Component::wireEncode() const
464{
465 if (this->hasWire())
466 return *this;
467
468 EncodingEstimator estimator;
469 size_t estimatedSize = wireEncode(estimator);
470
471 EncodingBuffer buffer(estimatedSize, 0);
472 wireEncode(buffer);
473
474 const_cast<Component&>(*this) = buffer.block();
475 return *this;
476}
477
478void
479Component::wireDecode(const Block& wire)
480{
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700481 *this = wire;
Qiuhan Ding2c3cbe42014-11-25 18:10:23 -0800482 // validity check is done within Component(const Block& wire)
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700483}
484
485} // namespace name
486} // namespace ndn