Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 4 | * |
| 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" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 29 | #include "encoding/block.hpp" |
| 30 | #include "encoding/encoding-buffer.hpp" |
| 31 | |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame^] | 32 | #include <boost/algorithm/string/trim.hpp> |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 33 | #include <boost/functional/hash.hpp> |
| 34 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 35 | namespace ndn { |
| 36 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 37 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Name>)); |
| 38 | BOOST_CONCEPT_ASSERT((WireEncodable<Name>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 39 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Name>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 40 | BOOST_CONCEPT_ASSERT((WireDecodable<Name>)); |
| 41 | static_assert(std::is_base_of<tlv::Error, Name::Error>::value, |
| 42 | "Name::Error must inherit from tlv::Error"); |
| 43 | |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 44 | const size_t Name::npos = std::numeric_limits<size_t>::max(); |
| 45 | |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 46 | Name::Name() |
| 47 | : m_nameBlock(tlv::Name) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | Name::Name(const Block& wire) |
| 52 | { |
| 53 | m_nameBlock = wire; |
| 54 | m_nameBlock.parse(); |
| 55 | } |
| 56 | |
| 57 | Name::Name(const char* uri) |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 58 | : Name(std::string(uri)) |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 59 | { |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 62 | Name::Name(std::string uri) |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 63 | { |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame^] | 64 | boost::algorithm::trim(uri); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 65 | if (uri.empty()) |
| 66 | return; |
| 67 | |
| 68 | size_t iColon = uri.find(':'); |
| 69 | if (iColon != std::string::npos) { |
| 70 | // Make sure the colon came before a '/'. |
| 71 | size_t iFirstSlash = uri.find('/'); |
| 72 | if (iFirstSlash == std::string::npos || iColon < iFirstSlash) { |
| 73 | // Omit the leading protocol such as ndn: |
| 74 | uri.erase(0, iColon + 1); |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame^] | 75 | boost::algorithm::trim(uri); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | // Trim the leading slash and possibly the authority. |
| 80 | if (uri[0] == '/') { |
| 81 | if (uri.size() >= 2 && uri[1] == '/') { |
| 82 | // Strip the authority following "//". |
| 83 | size_t iAfterAuthority = uri.find('/', 2); |
| 84 | if (iAfterAuthority == std::string::npos) |
| 85 | // Unusual case: there was only an authority. |
| 86 | return; |
| 87 | else { |
| 88 | uri.erase(0, iAfterAuthority + 1); |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame^] | 89 | boost::algorithm::trim(uri); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | else { |
| 93 | uri.erase(0, 1); |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame^] | 94 | boost::algorithm::trim(uri); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | size_t iComponentStart = 0; |
| 99 | |
| 100 | // Unescape the components. |
| 101 | while (iComponentStart < uri.size()) { |
| 102 | size_t iComponentEnd = uri.find("/", iComponentStart); |
| 103 | if (iComponentEnd == std::string::npos) |
| 104 | iComponentEnd = uri.size(); |
| 105 | |
| 106 | append(Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd)); |
| 107 | iComponentStart = iComponentEnd + 1; |
| 108 | } |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 111 | Name |
| 112 | Name::deepCopy() const |
| 113 | { |
| 114 | Name copiedName(*this); |
| 115 | copiedName.m_nameBlock.resetWire(); |
| 116 | copiedName.wireEncode(); // "compress" the underlying buffer |
| 117 | return copiedName; |
| 118 | } |
| 119 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 120 | template<encoding::Tag TAG> |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 121 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 122 | Name::wireEncode(EncodingImpl<TAG>& encoder) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 123 | { |
| 124 | size_t totalLength = 0; |
| 125 | |
| 126 | for (const_reverse_iterator i = rbegin(); i != rend(); ++i) |
| 127 | { |
| 128 | totalLength += i->wireEncode(encoder); |
| 129 | } |
| 130 | |
| 131 | totalLength += encoder.prependVarNumber(totalLength); |
| 132 | totalLength += encoder.prependVarNumber(tlv::Name); |
| 133 | return totalLength; |
| 134 | } |
| 135 | |
| 136 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 137 | Name::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 138 | |
| 139 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 140 | Name::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 141 | |
| 142 | const Block& |
| 143 | Name::wireEncode() const |
| 144 | { |
| 145 | if (m_nameBlock.hasWire()) |
| 146 | return m_nameBlock; |
| 147 | |
| 148 | EncodingEstimator estimator; |
| 149 | size_t estimatedSize = wireEncode(estimator); |
| 150 | |
| 151 | EncodingBuffer buffer(estimatedSize, 0); |
| 152 | wireEncode(buffer); |
| 153 | |
| 154 | m_nameBlock = buffer.block(); |
| 155 | m_nameBlock.parse(); |
| 156 | |
| 157 | return m_nameBlock; |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | Name::wireDecode(const Block& wire) |
| 162 | { |
| 163 | if (wire.type() != tlv::Name) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 164 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Name")); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 165 | |
| 166 | m_nameBlock = wire; |
| 167 | m_nameBlock.parse(); |
| 168 | } |
| 169 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 170 | std::string |
| 171 | Name::toUri() const |
| 172 | { |
| 173 | std::ostringstream os; |
| 174 | os << *this; |
| 175 | return os.str(); |
| 176 | } |
| 177 | |
| 178 | Name& |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 179 | Name::append(const PartialName& name) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 180 | { |
| 181 | if (&name == this) |
| 182 | // Copying from this name, so need to make a copy first. |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 183 | return append(PartialName(name)); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 184 | |
| 185 | for (size_t i = 0; i < name.size(); ++i) |
| 186 | append(name.at(i)); |
| 187 | |
| 188 | return *this; |
| 189 | } |
| 190 | |
| 191 | Name& |
| 192 | Name::appendNumber(uint64_t number) |
| 193 | { |
| 194 | m_nameBlock.push_back(Component::fromNumber(number)); |
| 195 | return *this; |
| 196 | } |
| 197 | |
| 198 | Name& |
| 199 | Name::appendNumberWithMarker(uint8_t marker, uint64_t number) |
| 200 | { |
| 201 | m_nameBlock.push_back(Component::fromNumberWithMarker(marker, number)); |
| 202 | return *this; |
| 203 | } |
| 204 | |
| 205 | Name& |
| 206 | Name::appendVersion(uint64_t version) |
| 207 | { |
| 208 | m_nameBlock.push_back(Component::fromVersion(version)); |
| 209 | return *this; |
| 210 | } |
| 211 | |
| 212 | Name& |
| 213 | Name::appendVersion() |
| 214 | { |
Junxiao Shi | 937e461 | 2014-10-22 15:39:07 -0700 | [diff] [blame] | 215 | appendVersion(time::toUnixTimestamp(time::system_clock::now()).count()); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 216 | return *this; |
| 217 | } |
| 218 | |
| 219 | Name& |
| 220 | Name::appendSegment(uint64_t segmentNo) |
| 221 | { |
| 222 | m_nameBlock.push_back(Component::fromSegment(segmentNo)); |
| 223 | return *this; |
| 224 | } |
| 225 | |
| 226 | Name& |
| 227 | Name::appendSegmentOffset(uint64_t offset) |
| 228 | { |
| 229 | m_nameBlock.push_back(Component::fromSegmentOffset(offset)); |
| 230 | return *this; |
| 231 | } |
| 232 | |
| 233 | Name& |
| 234 | Name::appendTimestamp(const time::system_clock::TimePoint& timePoint) |
| 235 | { |
| 236 | m_nameBlock.push_back(Component::fromTimestamp(timePoint)); |
| 237 | return *this; |
| 238 | } |
| 239 | |
| 240 | Name& |
| 241 | Name::appendSequenceNumber(uint64_t seqNo) |
| 242 | { |
| 243 | m_nameBlock.push_back(Component::fromSequenceNumber(seqNo)); |
| 244 | return *this; |
| 245 | } |
| 246 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 247 | Name& |
| 248 | Name::appendImplicitSha256Digest(const ConstBufferPtr& digest) |
| 249 | { |
| 250 | m_nameBlock.push_back(Component::fromImplicitSha256Digest(digest)); |
| 251 | return *this; |
| 252 | } |
| 253 | |
| 254 | Name& |
| 255 | Name::appendImplicitSha256Digest(const uint8_t* digest, size_t digestSize) |
| 256 | { |
| 257 | m_nameBlock.push_back(Component::fromImplicitSha256Digest(digest, digestSize)); |
| 258 | return *this; |
| 259 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 260 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 261 | PartialName |
| 262 | Name::getSubName(ssize_t iStartComponent, size_t nComponents) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 263 | { |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 264 | PartialName result; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 265 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 266 | ssize_t iStart = iStartComponent < 0 ? this->size() + iStartComponent : iStartComponent; |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 267 | size_t iEnd = this->size(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 268 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 269 | iStart = std::max(iStart, static_cast<ssize_t>(0)); |
| 270 | |
| 271 | if (nComponents != npos) |
| 272 | iEnd = std::min(this->size(), iStart + nComponents); |
| 273 | |
| 274 | for (size_t i = iStart; i < iEnd; ++i) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 275 | result.append(at(i)); |
| 276 | |
| 277 | return result; |
| 278 | } |
| 279 | |
| 280 | Name |
| 281 | Name::getSuccessor() const |
| 282 | { |
| 283 | if (empty()) { |
| 284 | static uint8_t firstValue[] = { 0 }; |
| 285 | Name firstName; |
| 286 | firstName.append(firstValue, 1); |
| 287 | return firstName; |
| 288 | } |
| 289 | |
| 290 | return getPrefix(-1).append(get(-1).getSuccessor()); |
| 291 | } |
| 292 | |
| 293 | bool |
| 294 | Name::equals(const Name& name) const |
| 295 | { |
| 296 | if (size() != name.size()) |
| 297 | return false; |
| 298 | |
| 299 | for (size_t i = 0; i < size(); ++i) { |
| 300 | if (at(i) != name.at(i)) |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | bool |
| 308 | Name::isPrefixOf(const Name& name) const |
| 309 | { |
| 310 | // This name is longer than the name we are checking against. |
| 311 | if (size() > name.size()) |
| 312 | return false; |
| 313 | |
| 314 | // Check if at least one of given components doesn't match. |
| 315 | for (size_t i = 0; i < size(); ++i) { |
| 316 | if (at(i) != name.at(i)) |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | return true; |
| 321 | } |
| 322 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 323 | int |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 324 | Name::compare(size_t pos1, size_t count1, const Name& other, size_t pos2, size_t count2) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 325 | { |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 326 | count1 = std::min(count1, this->size() - pos1); |
| 327 | count2 = std::min(count2, other.size() - pos2); |
| 328 | size_t count = std::min(count1, count2); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 329 | |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 330 | for (size_t i = 0; i < count; ++i) { |
| 331 | int comp = this->at(pos1 + i).compare(other.at(pos2 + i)); |
| 332 | if (comp != 0) { // i-th component differs |
| 333 | return comp; |
| 334 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 335 | } |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 336 | // [pos1, pos1+count) of this Name equals [pos2, pos2+count) of other Name |
Joao Pereira | aa8fd16 | 2015-06-05 16:35:15 -0400 | [diff] [blame] | 337 | return count1 - count2; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | std::ostream& |
| 341 | operator<<(std::ostream& os, const Name& name) |
| 342 | { |
| 343 | if (name.empty()) |
| 344 | { |
| 345 | os << "/"; |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | for (Name::const_iterator i = name.begin(); i != name.end(); i++) { |
| 350 | os << "/"; |
| 351 | i->toUri(os); |
| 352 | } |
| 353 | } |
| 354 | return os; |
| 355 | } |
| 356 | |
| 357 | std::istream& |
| 358 | operator>>(std::istream& is, Name& name) |
| 359 | { |
| 360 | std::string inputString; |
| 361 | is >> inputString; |
Alexander Afanasyev | 66ca203 | 2015-12-04 13:17:02 -0800 | [diff] [blame] | 362 | name = Name(inputString); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 363 | |
| 364 | return is; |
| 365 | } |
| 366 | |
| 367 | } // namespace ndn |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 368 | |
| 369 | namespace std { |
| 370 | size_t |
| 371 | hash<ndn::Name>::operator()(const ndn::Name& name) const |
| 372 | { |
| 373 | return boost::hash_range(name.wireEncode().wire(), |
| 374 | name.wireEncode().wire() + name.wireEncode().size()); |
| 375 | } |
| 376 | |
| 377 | } // namespace std |