blob: ba6c146e95202251ef86ddd76d587f256d8b5297 [file] [log] [blame]
Alexander Afanasyev15f67312014-07-22 15:11:09 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -07003 * Copyright (c) 2013-2016 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.hpp"
27
28#include "util/time.hpp"
29#include "util/string-helper.hpp"
30#include "encoding/block.hpp"
31#include "encoding/encoding-buffer.hpp"
32
Yingdi Yu90e23582014-11-06 14:21:04 -080033#include <boost/functional/hash.hpp>
34
Alexander Afanasyev15f67312014-07-22 15:11:09 -070035namespace ndn {
36
Junxiao Shic2b8d242014-11-04 08:35:29 -070037BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Name>));
38BOOST_CONCEPT_ASSERT((WireEncodable<Name>));
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070039BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Name>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070040BOOST_CONCEPT_ASSERT((WireDecodable<Name>));
41static_assert(std::is_base_of<tlv::Error, Name::Error>::value,
42 "Name::Error must inherit from tlv::Error");
43
Junxiao Shia6452ac2015-01-23 11:21:06 -070044const size_t Name::npos = std::numeric_limits<size_t>::max();
45
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080046Name::Name()
47 : m_nameBlock(tlv::Name)
48{
49}
50
51Name::Name(const Block& wire)
52{
53 m_nameBlock = wire;
54 m_nameBlock.parse();
55}
56
57Name::Name(const char* uri)
58{
59 construct(uri);
60}
61
62Name::Name(const std::string& uri)
63{
64 construct(uri.c_str());
65}
66
Alexander Afanasyev4f512fb2016-05-18 10:47:53 -070067Name
68Name::deepCopy() const
69{
70 Name copiedName(*this);
71 copiedName.m_nameBlock.resetWire();
72 copiedName.wireEncode(); // "compress" the underlying buffer
73 return copiedName;
74}
75
Alexander Afanasyev74633892015-02-08 18:08:46 -080076template<encoding::Tag TAG>
Alexander Afanasyev15f67312014-07-22 15:11:09 -070077size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080078Name::wireEncode(EncodingImpl<TAG>& encoder) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -070079{
80 size_t totalLength = 0;
81
82 for (const_reverse_iterator i = rbegin(); i != rend(); ++i)
83 {
84 totalLength += i->wireEncode(encoder);
85 }
86
87 totalLength += encoder.prependVarNumber(totalLength);
88 totalLength += encoder.prependVarNumber(tlv::Name);
89 return totalLength;
90}
91
92template size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070093Name::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
Alexander Afanasyev15f67312014-07-22 15:11:09 -070094
95template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080096Name::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
Alexander Afanasyev15f67312014-07-22 15:11:09 -070097
98const Block&
99Name::wireEncode() const
100{
101 if (m_nameBlock.hasWire())
102 return m_nameBlock;
103
104 EncodingEstimator estimator;
105 size_t estimatedSize = wireEncode(estimator);
106
107 EncodingBuffer buffer(estimatedSize, 0);
108 wireEncode(buffer);
109
110 m_nameBlock = buffer.block();
111 m_nameBlock.parse();
112
113 return m_nameBlock;
114}
115
116void
117Name::wireDecode(const Block& wire)
118{
119 if (wire.type() != tlv::Name)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700120 BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Name"));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700121
122 m_nameBlock = wire;
123 m_nameBlock.parse();
124}
125
126void
Alexander Afanasyevc89efb42015-02-10 18:26:42 -0800127Name::construct(const char* uriOrig)
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700128{
129 clear();
130
131 std::string uri = uriOrig;
132 trim(uri);
133 if (uri.size() == 0)
134 return;
135
136 size_t iColon = uri.find(':');
137 if (iColon != std::string::npos) {
138 // Make sure the colon came before a '/'.
139 size_t iFirstSlash = uri.find('/');
140 if (iFirstSlash == std::string::npos || iColon < iFirstSlash) {
141 // Omit the leading protocol such as ndn:
142 uri.erase(0, iColon + 1);
143 trim(uri);
144 }
145 }
146
147 // Trim the leading slash and possibly the authority.
148 if (uri[0] == '/') {
149 if (uri.size() >= 2 && uri[1] == '/') {
150 // Strip the authority following "//".
151 size_t iAfterAuthority = uri.find('/', 2);
152 if (iAfterAuthority == std::string::npos)
153 // Unusual case: there was only an authority.
154 return;
155 else {
156 uri.erase(0, iAfterAuthority + 1);
157 trim(uri);
158 }
159 }
160 else {
161 uri.erase(0, 1);
162 trim(uri);
163 }
164 }
165
166 size_t iComponentStart = 0;
167
168 // Unescape the components.
169 while (iComponentStart < uri.size()) {
170 size_t iComponentEnd = uri.find("/", iComponentStart);
171 if (iComponentEnd == std::string::npos)
172 iComponentEnd = uri.size();
173
Alexander Afanasyevd7eacc72015-04-03 13:06:26 -0700174 append(Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700175 iComponentStart = iComponentEnd + 1;
176 }
177}
178
Alexander Afanasyevc89efb42015-02-10 18:26:42 -0800179void
180Name::set(const char* uri)
181{
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800182 *this = Name(uri);
Alexander Afanasyevc89efb42015-02-10 18:26:42 -0800183}
184
185void
186Name::set(const std::string& uri)
187{
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800188 *this = Name(uri);
Alexander Afanasyevc89efb42015-02-10 18:26:42 -0800189}
190
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700191std::string
192Name::toUri() const
193{
194 std::ostringstream os;
195 os << *this;
196 return os.str();
197}
198
199Name&
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400200Name::append(const PartialName& name)
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700201{
202 if (&name == this)
203 // Copying from this name, so need to make a copy first.
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400204 return append(PartialName(name));
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700205
206 for (size_t i = 0; i < name.size(); ++i)
207 append(name.at(i));
208
209 return *this;
210}
211
212Name&
213Name::appendNumber(uint64_t number)
214{
215 m_nameBlock.push_back(Component::fromNumber(number));
216 return *this;
217}
218
219Name&
220Name::appendNumberWithMarker(uint8_t marker, uint64_t number)
221{
222 m_nameBlock.push_back(Component::fromNumberWithMarker(marker, number));
223 return *this;
224}
225
226Name&
227Name::appendVersion(uint64_t version)
228{
229 m_nameBlock.push_back(Component::fromVersion(version));
230 return *this;
231}
232
233Name&
234Name::appendVersion()
235{
Junxiao Shi937e4612014-10-22 15:39:07 -0700236 appendVersion(time::toUnixTimestamp(time::system_clock::now()).count());
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700237 return *this;
238}
239
240Name&
241Name::appendSegment(uint64_t segmentNo)
242{
243 m_nameBlock.push_back(Component::fromSegment(segmentNo));
244 return *this;
245}
246
247Name&
248Name::appendSegmentOffset(uint64_t offset)
249{
250 m_nameBlock.push_back(Component::fromSegmentOffset(offset));
251 return *this;
252}
253
254Name&
255Name::appendTimestamp(const time::system_clock::TimePoint& timePoint)
256{
257 m_nameBlock.push_back(Component::fromTimestamp(timePoint));
258 return *this;
259}
260
261Name&
262Name::appendSequenceNumber(uint64_t seqNo)
263{
264 m_nameBlock.push_back(Component::fromSequenceNumber(seqNo));
265 return *this;
266}
267
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700268Name&
269Name::appendImplicitSha256Digest(const ConstBufferPtr& digest)
270{
271 m_nameBlock.push_back(Component::fromImplicitSha256Digest(digest));
272 return *this;
273}
274
275Name&
276Name::appendImplicitSha256Digest(const uint8_t* digest, size_t digestSize)
277{
278 m_nameBlock.push_back(Component::fromImplicitSha256Digest(digest, digestSize));
279 return *this;
280}
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700281
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400282PartialName
283Name::getSubName(ssize_t iStartComponent, size_t nComponents) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700284{
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400285 PartialName result;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700286
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400287 ssize_t iStart = iStartComponent < 0 ? this->size() + iStartComponent : iStartComponent;
Junxiao Shia6452ac2015-01-23 11:21:06 -0700288 size_t iEnd = this->size();
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700289
Joao Pereira6f7cfd02015-06-15 11:36:26 -0400290 iStart = std::max(iStart, static_cast<ssize_t>(0));
291
292 if (nComponents != npos)
293 iEnd = std::min(this->size(), iStart + nComponents);
294
295 for (size_t i = iStart; i < iEnd; ++i)
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700296 result.append(at(i));
297
298 return result;
299}
300
301Name
302Name::getSuccessor() const
303{
304 if (empty()) {
305 static uint8_t firstValue[] = { 0 };
306 Name firstName;
307 firstName.append(firstValue, 1);
308 return firstName;
309 }
310
311 return getPrefix(-1).append(get(-1).getSuccessor());
312}
313
314bool
315Name::equals(const Name& name) const
316{
317 if (size() != name.size())
318 return false;
319
320 for (size_t i = 0; i < size(); ++i) {
321 if (at(i) != name.at(i))
322 return false;
323 }
324
325 return true;
326}
327
328bool
329Name::isPrefixOf(const Name& name) const
330{
331 // This name is longer than the name we are checking against.
332 if (size() > name.size())
333 return false;
334
335 // Check if at least one of given components doesn't match.
336 for (size_t i = 0; i < size(); ++i) {
337 if (at(i) != name.at(i))
338 return false;
339 }
340
341 return true;
342}
343
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700344int
Junxiao Shia6452ac2015-01-23 11:21:06 -0700345Name::compare(size_t pos1, size_t count1, const Name& other, size_t pos2, size_t count2) const
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700346{
Junxiao Shia6452ac2015-01-23 11:21:06 -0700347 count1 = std::min(count1, this->size() - pos1);
348 count2 = std::min(count2, other.size() - pos2);
349 size_t count = std::min(count1, count2);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700350
Junxiao Shia6452ac2015-01-23 11:21:06 -0700351 for (size_t i = 0; i < count; ++i) {
352 int comp = this->at(pos1 + i).compare(other.at(pos2 + i));
353 if (comp != 0) { // i-th component differs
354 return comp;
355 }
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700356 }
Junxiao Shia6452ac2015-01-23 11:21:06 -0700357 // [pos1, pos1+count) of this Name equals [pos2, pos2+count) of other Name
Joao Pereiraaa8fd162015-06-05 16:35:15 -0400358 return count1 - count2;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700359}
360
361std::ostream&
362operator<<(std::ostream& os, const Name& name)
363{
364 if (name.empty())
365 {
366 os << "/";
367 }
368 else
369 {
370 for (Name::const_iterator i = name.begin(); i != name.end(); i++) {
371 os << "/";
372 i->toUri(os);
373 }
374 }
375 return os;
376}
377
378std::istream&
379operator>>(std::istream& is, Name& name)
380{
381 std::string inputString;
382 is >> inputString;
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800383 name = Name(inputString);
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700384
385 return is;
386}
387
388} // namespace ndn
Yingdi Yu90e23582014-11-06 14:21:04 -0800389
390namespace std {
391size_t
392hash<ndn::Name>::operator()(const ndn::Name& name) const
393{
394 return boost::hash_range(name.wireEncode().wire(),
395 name.wireEncode().wire() + name.wireEncode().size());
396}
397
398} // namespace std