blob: fb89f656dc51f56b4e5f84cb189bc558e154ff83 [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 {
Davide Pesavento38a061d2018-02-15 22:45:48 -0500138 bool hasNonDot = std::any_of(value_begin(), value_end(),
139 [] (uint8_t x) { return x != '.'; });
140 if (!hasNonDot) {
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700141 // Special case for component of zero or more periods. Add 3 periods.
142 result << "...";
Davide Pesavento38a061d2018-02-15 22:45:48 -0500143 for (size_t i = 0; i < value_size(); ++i)
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700144 result << '.';
145 }
146 else {
Davide Pesavento38a061d2018-02-15 22:45:48 -0500147 escape(result, reinterpret_cast<const char*>(value()), value_size());
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700148 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700149 }
150}
151
152std::string
153Component::toUri() const
154{
155 std::ostringstream os;
156 toUri(os);
157 return os.str();
158}
159
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700160////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700161
162bool
163Component::isNumber() const
164{
165 return (value_size() == 1 || value_size() == 2 ||
166 value_size() == 4 || value_size() == 8);
167}
168
169bool
170Component::isNumberWithMarker(uint8_t marker) const
171{
172 return (!empty() && value()[0] == marker &&
173 (value_size() == 2 || value_size() == 3 ||
174 value_size() == 5 || value_size() == 9));
175}
176
177bool
178Component::isVersion() const
179{
180 return isNumberWithMarker(VERSION_MARKER);
181}
182
183bool
184Component::isSegment() const
185{
186 return isNumberWithMarker(SEGMENT_MARKER);
187}
188
189bool
190Component::isSegmentOffset() const
191{
192 return isNumberWithMarker(SEGMENT_OFFSET_MARKER);
193}
194
195bool
196Component::isTimestamp() const
197{
198 return isNumberWithMarker(TIMESTAMP_MARKER);
199}
200
201bool
202Component::isSequenceNumber() const
203{
204 return isNumberWithMarker(SEQUENCE_NUMBER_MARKER);
205}
206
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700207////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700208
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700209uint64_t
210Component::toNumber() const
211{
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700212 if (!isNumber())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700213 BOOST_THROW_EXCEPTION(Error("Name component does not have nonNegativeInteger value"));
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700214
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700215 return readNonNegativeInteger(*this);
216}
217
218uint64_t
219Component::toNumberWithMarker(uint8_t marker) const
220{
Alexander Afanasyev0f232c52014-10-23 13:07:31 -0700221 if (!isNumberWithMarker(marker))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700222 BOOST_THROW_EXCEPTION(Error("Name component does not have the requested marker "
223 "or the value is not a nonNegativeInteger"));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700224
225 Buffer::const_iterator valueBegin = value_begin() + 1;
226 return tlv::readNonNegativeInteger(value_size() - 1, valueBegin, value_end());
227}
228
229uint64_t
230Component::toVersion() const
231{
232 return toNumberWithMarker(VERSION_MARKER);
233}
234
235uint64_t
236Component::toSegment() const
237{
238 return toNumberWithMarker(SEGMENT_MARKER);
239}
240
241uint64_t
242Component::toSegmentOffset() const
243{
244 return toNumberWithMarker(SEGMENT_OFFSET_MARKER);
245}
246
247time::system_clock::TimePoint
248Component::toTimestamp() const
249{
250 uint64_t value = toNumberWithMarker(TIMESTAMP_MARKER);
251 return time::getUnixEpoch() + time::microseconds(value);
252}
253
254uint64_t
255Component::toSequenceNumber() const
256{
257 return toNumberWithMarker(SEQUENCE_NUMBER_MARKER);
258}
259
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700260////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700261
262Component
263Component::fromNumber(uint64_t number)
264{
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700265 return makeNonNegativeIntegerBlock(tlv::NameComponent, number);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700266}
267
268Component
269Component::fromNumberWithMarker(uint8_t marker, uint64_t number)
270{
271 EncodingEstimator estimator;
272
273 size_t valueLength = estimator.prependNonNegativeInteger(number);
274 valueLength += estimator.prependByteArray(&marker, 1);
275 size_t totalLength = valueLength;
276 totalLength += estimator.prependVarNumber(valueLength);
277 totalLength += estimator.prependVarNumber(tlv::NameComponent);
278
279 EncodingBuffer encoder(totalLength, 0);
280 encoder.prependNonNegativeInteger(number);
281 encoder.prependByteArray(&marker, 1);
282 encoder.prependVarNumber(valueLength);
283 encoder.prependVarNumber(tlv::NameComponent);
284
285 return encoder.block();
286}
287
288Component
289Component::fromVersion(uint64_t version)
290{
291 return fromNumberWithMarker(VERSION_MARKER, version);
292}
293
294Component
295Component::fromSegment(uint64_t segmentNo)
296{
297 return fromNumberWithMarker(SEGMENT_MARKER, segmentNo);
298}
299
300Component
301Component::fromSegmentOffset(uint64_t offset)
302{
303 return fromNumberWithMarker(SEGMENT_OFFSET_MARKER, offset);
304}
305
306Component
307Component::fromTimestamp(const time::system_clock::TimePoint& timePoint)
308{
309 using namespace time;
310 uint64_t value = duration_cast<microseconds>(timePoint - getUnixEpoch()).count();
311 return fromNumberWithMarker(TIMESTAMP_MARKER, value);
312}
313
314Component
315Component::fromSequenceNumber(uint64_t seqNo)
316{
317 return fromNumberWithMarker(SEQUENCE_NUMBER_MARKER, seqNo);
318}
319
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700320////////////////////////////////////////////////////////////////////////////////
321
322bool
323Component::isGeneric() const
324{
325 return (type() == tlv::NameComponent);
326}
327
328bool
329Component::isImplicitSha256Digest() const
330{
331 return (type() == tlv::ImplicitSha256DigestComponent &&
Davide Pesavento10b24be2017-07-12 23:23:46 -0400332 value_size() == util::Sha256::DIGEST_SIZE);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700333}
334
335Component
336Component::fromImplicitSha256Digest(const ConstBufferPtr& digest)
337{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400338 if (digest->size() != util::Sha256::DIGEST_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700339 BOOST_THROW_EXCEPTION(Error("Cannot create ImplicitSha256DigestComponent (input digest must be " +
Davide Pesavento10b24be2017-07-12 23:23:46 -0400340 to_string(util::Sha256::DIGEST_SIZE) + " octets)"));
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700341
342 return Block(tlv::ImplicitSha256DigestComponent, digest);
343}
344
345Component
346Component::fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize)
347{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400348 if (digestSize != util::Sha256::DIGEST_SIZE)
Davide Pesavento96b96af2015-09-19 23:00:40 +0200349 BOOST_THROW_EXCEPTION(Error("Cannot create ImplicitSha256DigestComponent (input digest must be " +
Davide Pesavento10b24be2017-07-12 23:23:46 -0400350 to_string(util::Sha256::DIGEST_SIZE) + " octets)"));
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700351
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700352 return makeBinaryBlock(tlv::ImplicitSha256DigestComponent, digest, digestSize);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700353}
354
355////////////////////////////////////////////////////////////////////////////////
356
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000357bool
358Component::equals(const Component& other) const
359{
360 return type() == other.type() &&
361 value_size() == other.value_size() &&
Davide Pesaventoe245b052017-10-31 13:00:44 -0400362 (empty() || // needed with Apple clang < 9.0.0 due to libc++ bug
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000363 std::equal(value_begin(), value_end(), other.value_begin()));
364}
365
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700366int
367Component::compare(const Component& other) const
368{
Junxiao Shi010f0862016-10-11 21:08:32 +0000369 if (this->hasWire() && other.hasWire()) {
370 // In the common case where both components have wire encoding,
371 // it's more efficient to simply compare the wire encoding.
372 // This works because lexical order of TLV encoding happens to be
373 // the same as canonical order of the value.
374 return std::memcmp(wire(), other.wire(), std::min(size(), other.size()));
375 }
376
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000377 int cmpType = type() - other.type();
378 if (cmpType != 0)
379 return cmpType;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700380
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000381 int cmpSize = value_size() - other.value_size();
382 if (cmpSize != 0)
383 return cmpSize;
384
Davide Pesaventoe245b052017-10-31 13:00:44 -0400385 if (empty())
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700386 return 0;
387
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700388 return std::memcmp(value(), other.value(), value_size());
389}
390
391Component
392Component::getSuccessor() const
393{
394 size_t totalLength = 0;
395 EncodingBuffer encoder(size() + 1, 1); // + 1 in case there is an overflow
396 // in unlikely case TLV length increases,
397 // EncodingBuffer will take care of that
398
399 bool isOverflow = true;
400 size_t i = value_size();
401 for (; isOverflow && i > 0; i--) {
402 uint8_t newValue = static_cast<uint8_t>((value()[i - 1] + 1) & 0xFF);
403 totalLength += encoder.prependByte(newValue);
404 isOverflow = (newValue == 0);
405 }
406 totalLength += encoder.prependByteArray(value(), i);
407
408 if (isOverflow) {
409 // new name components has to be extended
410 totalLength += encoder.appendByte(0);
411 }
412
Davide Pesavento9bd4d982015-05-13 14:31:19 +0200413 encoder.prependVarNumber(totalLength);
414 encoder.prependVarNumber(type());
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700415
416 return encoder.block();
417}
418
Alexander Afanasyev74633892015-02-08 18:08:46 -0800419template<encoding::Tag TAG>
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700420size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700421Component::wireEncode(EncodingImpl<TAG>& encoder) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700422{
423 size_t totalLength = 0;
424 if (value_size() > 0)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700425 totalLength += encoder.prependByteArray(value(), value_size());
426 totalLength += encoder.prependVarNumber(value_size());
427 totalLength += encoder.prependVarNumber(type());
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700428 return totalLength;
429}
430
Davide Pesavento88a0d812017-08-19 21:31:42 -0400431NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Component);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700432
433const Block&
434Component::wireEncode() const
435{
436 if (this->hasWire())
437 return *this;
438
439 EncodingEstimator estimator;
440 size_t estimatedSize = wireEncode(estimator);
441
442 EncodingBuffer buffer(estimatedSize, 0);
443 wireEncode(buffer);
444
445 const_cast<Component&>(*this) = buffer.block();
446 return *this;
447}
448
449void
450Component::wireDecode(const Block& wire)
451{
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700452 *this = wire;
Qiuhan Ding2c3cbe42014-11-25 18:10:23 -0800453 // validity check is done within Component(const Block& wire)
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700454}
455
456} // namespace name
457} // namespace ndn