Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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" |
| 29 | #include "util/string-helper.hpp" |
| 30 | #include "encoding/block.hpp" |
| 31 | #include "encoding/encoding-buffer.hpp" |
| 32 | |
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) |
| 58 | { |
| 59 | construct(uri); |
| 60 | } |
| 61 | |
| 62 | Name::Name(const std::string& uri) |
| 63 | { |
| 64 | construct(uri.c_str()); |
| 65 | } |
| 66 | |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 67 | Name |
| 68 | Name::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 Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 76 | template<encoding::Tag TAG> |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 77 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 78 | Name::wireEncode(EncodingImpl<TAG>& encoder) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 79 | { |
| 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 | |
| 92 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 93 | Name::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 94 | |
| 95 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 96 | Name::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 97 | |
| 98 | const Block& |
| 99 | Name::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 | |
| 116 | void |
| 117 | Name::wireDecode(const Block& wire) |
| 118 | { |
| 119 | if (wire.type() != tlv::Name) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 120 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Name")); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 121 | |
| 122 | m_nameBlock = wire; |
| 123 | m_nameBlock.parse(); |
| 124 | } |
| 125 | |
| 126 | void |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 127 | Name::construct(const char* uriOrig) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 128 | { |
| 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 Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 174 | append(Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd)); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 175 | iComponentStart = iComponentEnd + 1; |
| 176 | } |
| 177 | } |
| 178 | |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 179 | void |
| 180 | Name::set(const char* uri) |
| 181 | { |
Alexander Afanasyev | 66ca203 | 2015-12-04 13:17:02 -0800 | [diff] [blame] | 182 | *this = Name(uri); |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void |
| 186 | Name::set(const std::string& uri) |
| 187 | { |
Alexander Afanasyev | 66ca203 | 2015-12-04 13:17:02 -0800 | [diff] [blame] | 188 | *this = Name(uri); |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 191 | std::string |
| 192 | Name::toUri() const |
| 193 | { |
| 194 | std::ostringstream os; |
| 195 | os << *this; |
| 196 | return os.str(); |
| 197 | } |
| 198 | |
| 199 | Name& |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 200 | Name::append(const PartialName& name) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 201 | { |
| 202 | if (&name == this) |
| 203 | // Copying from this name, so need to make a copy first. |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 204 | return append(PartialName(name)); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 205 | |
| 206 | for (size_t i = 0; i < name.size(); ++i) |
| 207 | append(name.at(i)); |
| 208 | |
| 209 | return *this; |
| 210 | } |
| 211 | |
| 212 | Name& |
| 213 | Name::appendNumber(uint64_t number) |
| 214 | { |
| 215 | m_nameBlock.push_back(Component::fromNumber(number)); |
| 216 | return *this; |
| 217 | } |
| 218 | |
| 219 | Name& |
| 220 | Name::appendNumberWithMarker(uint8_t marker, uint64_t number) |
| 221 | { |
| 222 | m_nameBlock.push_back(Component::fromNumberWithMarker(marker, number)); |
| 223 | return *this; |
| 224 | } |
| 225 | |
| 226 | Name& |
| 227 | Name::appendVersion(uint64_t version) |
| 228 | { |
| 229 | m_nameBlock.push_back(Component::fromVersion(version)); |
| 230 | return *this; |
| 231 | } |
| 232 | |
| 233 | Name& |
| 234 | Name::appendVersion() |
| 235 | { |
Junxiao Shi | 937e461 | 2014-10-22 15:39:07 -0700 | [diff] [blame] | 236 | appendVersion(time::toUnixTimestamp(time::system_clock::now()).count()); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 237 | return *this; |
| 238 | } |
| 239 | |
| 240 | Name& |
| 241 | Name::appendSegment(uint64_t segmentNo) |
| 242 | { |
| 243 | m_nameBlock.push_back(Component::fromSegment(segmentNo)); |
| 244 | return *this; |
| 245 | } |
| 246 | |
| 247 | Name& |
| 248 | Name::appendSegmentOffset(uint64_t offset) |
| 249 | { |
| 250 | m_nameBlock.push_back(Component::fromSegmentOffset(offset)); |
| 251 | return *this; |
| 252 | } |
| 253 | |
| 254 | Name& |
| 255 | Name::appendTimestamp(const time::system_clock::TimePoint& timePoint) |
| 256 | { |
| 257 | m_nameBlock.push_back(Component::fromTimestamp(timePoint)); |
| 258 | return *this; |
| 259 | } |
| 260 | |
| 261 | Name& |
| 262 | Name::appendSequenceNumber(uint64_t seqNo) |
| 263 | { |
| 264 | m_nameBlock.push_back(Component::fromSequenceNumber(seqNo)); |
| 265 | return *this; |
| 266 | } |
| 267 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 268 | Name& |
| 269 | Name::appendImplicitSha256Digest(const ConstBufferPtr& digest) |
| 270 | { |
| 271 | m_nameBlock.push_back(Component::fromImplicitSha256Digest(digest)); |
| 272 | return *this; |
| 273 | } |
| 274 | |
| 275 | Name& |
| 276 | Name::appendImplicitSha256Digest(const uint8_t* digest, size_t digestSize) |
| 277 | { |
| 278 | m_nameBlock.push_back(Component::fromImplicitSha256Digest(digest, digestSize)); |
| 279 | return *this; |
| 280 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 281 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 282 | PartialName |
| 283 | Name::getSubName(ssize_t iStartComponent, size_t nComponents) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 284 | { |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 285 | PartialName result; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 286 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 287 | ssize_t iStart = iStartComponent < 0 ? this->size() + iStartComponent : iStartComponent; |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 288 | size_t iEnd = this->size(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 289 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 290 | 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 Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 296 | result.append(at(i)); |
| 297 | |
| 298 | return result; |
| 299 | } |
| 300 | |
| 301 | Name |
| 302 | Name::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 | |
| 314 | bool |
| 315 | Name::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 | |
| 328 | bool |
| 329 | Name::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 Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 344 | int |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 345 | 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] | 346 | { |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 347 | count1 = std::min(count1, this->size() - pos1); |
| 348 | count2 = std::min(count2, other.size() - pos2); |
| 349 | size_t count = std::min(count1, count2); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 350 | |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 351 | 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 Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 356 | } |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 357 | // [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] | 358 | return count1 - count2; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | std::ostream& |
| 362 | operator<<(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 | |
| 378 | std::istream& |
| 379 | operator>>(std::istream& is, Name& name) |
| 380 | { |
| 381 | std::string inputString; |
| 382 | is >> inputString; |
Alexander Afanasyev | 66ca203 | 2015-12-04 13:17:02 -0800 | [diff] [blame] | 383 | name = Name(inputString); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 384 | |
| 385 | return is; |
| 386 | } |
| 387 | |
| 388 | } // namespace ndn |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 389 | |
| 390 | namespace std { |
| 391 | size_t |
| 392 | hash<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 |