blob: 9226eb4a208adb29169fb77f72d5e7869321b2e3 [file] [log] [blame]
Alexander Afanasyev15f67312014-07-22 15:11:09 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev74633892015-02-08 18:08:46 -08003 * Copyright (c) 2013-2015 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"
30#include "util/string-helper.hpp"
Alexander Afanasyev6486d522014-10-23 14:14:11 -070031#include "security/cryptopp.hpp"
32#include "util/crypto.hpp"
Alexander Afanasyev15f67312014-07-22 15:11:09 -070033
Alexander Afanasyev6486d522014-10-23 14:14:11 -070034#include <boost/lexical_cast.hpp>
35
Alexander Afanasyev15f67312014-07-22 15:11:09 -070036namespace ndn {
37namespace name {
38
Junxiao Shic2b8d242014-11-04 08:35:29 -070039BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Component>));
40BOOST_CONCEPT_ASSERT((WireEncodable<Component>));
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070041BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Component>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070042BOOST_CONCEPT_ASSERT((WireDecodable<Component>));
43static_assert(std::is_base_of<tlv::Error, Component::Error>::value,
44 "name::Component::Error must inherit from tlv::Error");
45
Alexander Afanasyev6486d522014-10-23 14:14:11 -070046static const std::string&
47getSha256DigestUriPrefix()
48{
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070049 static const std::string prefix{"sha256digest="};
Alexander Afanasyev6486d522014-10-23 14:14:11 -070050 return prefix;
51}
52
Alexander Afanasyev15f67312014-07-22 15:11:09 -070053Component::Component()
54 : Block(tlv::NameComponent)
55{
56}
57
58Component::Component(const Block& wire)
59 : Block(wire)
60{
Qiuhan Ding2c3cbe42014-11-25 18:10:23 -080061 if (!isGeneric() && !isImplicitSha256Digest())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070062 BOOST_THROW_EXCEPTION(Error("Cannot construct name::Component from not a NameComponent "
63 "or ImplicitSha256DigestComponent TLV wire block"));
Alexander Afanasyev15f67312014-07-22 15:11:09 -070064}
65
66Component::Component(const ConstBufferPtr& buffer)
67 : Block(tlv::NameComponent, buffer)
68{
69}
70
71Component::Component(const Buffer& value)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070072 : Block(makeBinaryBlock(tlv::NameComponent, value.buf(), value.size()))
Alexander Afanasyev15f67312014-07-22 15:11:09 -070073{
74}
75
76Component::Component(const uint8_t* value, size_t valueLen)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070077 : Block(makeBinaryBlock(tlv::NameComponent, value, valueLen))
Alexander Afanasyev15f67312014-07-22 15:11:09 -070078{
79}
80
81Component::Component(const char* str)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070082 : Block(makeBinaryBlock(tlv::NameComponent, str, std::char_traits<char>::length(str)))
Alexander Afanasyev15f67312014-07-22 15:11:09 -070083{
84}
85
86Component::Component(const std::string& str)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070087 : Block(makeStringBlock(tlv::NameComponent, str))
Alexander Afanasyev15f67312014-07-22 15:11:09 -070088{
89}
90
91
92Component
93Component::fromEscapedString(const char* escapedString, size_t beginOffset, size_t endOffset)
94{
95 std::string trimmedString(escapedString + beginOffset, escapedString + endOffset);
96 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) {
100 if (trimmedString.size() != getSha256DigestUriPrefix().size() + crypto::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 }
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700108 catch (StringHelperError& e) {
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.
158 std::ios::fmtflags saveFlags = 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.
176 result.flags(saveFlags);
177 }
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 &&
361 value_size() == crypto::SHA256_DIGEST_SIZE);
362}
363
364Component
365Component::fromImplicitSha256Digest(const ConstBufferPtr& digest)
366{
367 if (digest->size() != crypto::SHA256_DIGEST_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700368 BOOST_THROW_EXCEPTION(Error("Cannot create ImplicitSha256DigestComponent (input digest must be " +
369 std::to_string(crypto::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{
377 if (digestSize != crypto::SHA256_DIGEST_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700378 BOOST_THROW_EXCEPTION(Error("Cannot create ImplicitSha256DigestComponent (input digest must be "
379 + std::to_string(crypto::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
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700386int
387Component::compare(const Component& other) const
388{
389 // Imitate ndn_Exclude_compareComponents.
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700390 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 Afanasyev15f67312014-07-22 15:11:09 -0700395 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
406Component
407Component::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 Pesavento9bd4d982015-05-13 14:31:19 +0200428 encoder.prependVarNumber(totalLength);
429 encoder.prependVarNumber(type());
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700430
431 return encoder.block();
432}
433
434
Alexander Afanasyev74633892015-02-08 18:08:46 -0800435template<encoding::Tag TAG>
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700436size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700437Component::wireEncode(EncodingImpl<TAG>& encoder) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700438{
439 size_t totalLength = 0;
440 if (value_size() > 0)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700441 totalLength += encoder.prependByteArray(value(), value_size());
442 totalLength += encoder.prependVarNumber(value_size());
443 totalLength += encoder.prependVarNumber(type());
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700444 return totalLength;
445}
446
447template size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700448Component::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700449
450template size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700451Component::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700452
453const Block&
454Component::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
469void
470Component::wireDecode(const Block& wire)
471{
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700472 *this = wire;
Qiuhan Ding2c3cbe42014-11-25 18:10:23 -0800473 // validity check is done within Component(const Block& wire)
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700474}
475
476} // namespace name
477} // namespace ndn