Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | a98000c | 2013-12-16 14:40:09 -0800 | [diff] [blame] | 5 | * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 6 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Jeff Thompson | 5490977 | 2013-07-07 22:38:57 -0700 | [diff] [blame] | 9 | #include <stdexcept> |
Jeff Thompson | b8f1b13 | 2013-08-13 11:07:43 -0700 | [diff] [blame] | 10 | #include <algorithm> |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 11 | #include <cstring> |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 12 | #include "name.hpp" |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 13 | #include "util/time.hpp" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 14 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 15 | #include "util/string-helper.hpp" |
| 16 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 17 | using namespace std; |
| 18 | |
| 19 | namespace ndn { |
| 20 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 21 | uint64_t |
| 22 | Name::Component::toNumberWithMarker(uint8_t marker) const |
Jeff Thompson | 26c63d6 | 2013-07-02 18:00:26 -0700 | [diff] [blame] | 23 | { |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 24 | if (empty() || *getValue().begin() != marker) |
| 25 | throw runtime_error("Name component does not begin with the expected marker"); |
Jeff Thompson | 26c63d6 | 2013-07-02 18:00:26 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 27 | uint64_t result = 0; |
| 28 | for (Buffer::const_iterator i = getValue().begin()+1; i != getValue().end(); ++i) { |
| 29 | result <<= 8; |
| 30 | result |= *i; |
Jeff Thompson | 26c63d6 | 2013-07-02 18:00:26 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame] | 33 | return result; |
| 34 | } |
| 35 | |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 36 | Name::Component |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 37 | Name::Component::fromNumber(uint64_t number) |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 38 | { |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 39 | ptr_lib::shared_ptr<Buffer> value(new Buffer); |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 40 | |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 41 | // First encode in little endian. |
| 42 | while (number != 0) { |
| 43 | value->push_back(number & 0xff); |
| 44 | number >>= 8; |
| 45 | } |
| 46 | |
| 47 | // Make it big endian. |
| 48 | reverse(value->begin(), value->end()); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 49 | return Component(value); |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Alexander Afanasyev | 848c61a | 2014-01-03 13:52:04 -0800 | [diff] [blame] | 52 | Name::Component |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 53 | Name::Component::fromNumberWithMarker(uint64_t number, uint8_t marker) |
| 54 | { |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 55 | ptr_lib::shared_ptr<Buffer> value(new Buffer); |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 56 | |
| 57 | // Add the leading marker. |
| 58 | value->push_back(marker); |
Jeff Thompson | b8f1b13 | 2013-08-13 11:07:43 -0700 | [diff] [blame] | 59 | |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 60 | // First encode in little endian. |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 61 | while (number != 0) { |
| 62 | value->push_back(number & 0xff); |
| 63 | number >>= 8; |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 66 | // Make it big endian. |
Jeff Thompson | 995aba5 | 2013-09-12 12:04:52 -0700 | [diff] [blame] | 67 | reverse(value->begin() + 1, value->end()); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 68 | return Component(value); |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | uint64_t |
| 72 | Name::Component::toNumber() const |
| 73 | { |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 74 | uint64_t result = 0; |
| 75 | for (Buffer::const_iterator i = getValue().begin(); i != getValue().end(); ++i) { |
| 76 | result <<= 8; |
| 77 | result |= *i; |
| 78 | } |
| 79 | |
| 80 | return result; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jeff Thompson | a98000c | 2013-12-16 14:40:09 -0800 | [diff] [blame] | 83 | int |
| 84 | Name::Component::compare(const Name::Component& other) const |
| 85 | { |
| 86 | // Imitate ndn_Exclude_compareComponents. |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 87 | if (getValue().size() < other.getValue().size()) |
Jeff Thompson | a98000c | 2013-12-16 14:40:09 -0800 | [diff] [blame] | 88 | return -1; |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 89 | if (getValue().size() > other.getValue().size()) |
Jeff Thompson | a98000c | 2013-12-16 14:40:09 -0800 | [diff] [blame] | 90 | return 1; |
| 91 | |
| 92 | // The components are equal length. Just do a byte compare. |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 93 | return std::memcmp(getValue().buf(), other.getValue().buf(), getValue().size()); |
Jeff Thompson | a98000c | 2013-12-16 14:40:09 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 96 | inline size_t |
| 97 | Name::Component::wireEncode (EncodingBuffer& blk) |
| 98 | { |
| 99 | size_t total_len = 0; |
| 100 | total_len += blk.prependBuffer (*value_); |
| 101 | total_len += blk.prependVarNumber (value_->size ()); |
| 102 | total_len += blk.prependVarNumber (Tlv::NameComponent); |
| 103 | return total_len; |
| 104 | } |
| 105 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 106 | // const Block & |
| 107 | // Name::wireEncode() const |
| 108 | // { |
| 109 | |
| 110 | // } |
| 111 | |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 112 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 113 | Name::set(const char *uri_cstr) |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 114 | { |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 115 | components_.clear(); |
| 116 | |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 117 | string uri = uri_cstr; |
| 118 | trim(uri); |
| 119 | if (uri.size() == 0) |
| 120 | return; |
| 121 | |
| 122 | size_t iColon = uri.find(':'); |
| 123 | if (iColon != string::npos) { |
| 124 | // Make sure the colon came before a '/'. |
| 125 | size_t iFirstSlash = uri.find('/'); |
| 126 | if (iFirstSlash == string::npos || iColon < iFirstSlash) { |
| 127 | // Omit the leading protocol such as ndn: |
| 128 | uri.erase(0, iColon + 1); |
| 129 | trim(uri); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Trim the leading slash and possibly the authority. |
| 134 | if (uri[0] == '/') { |
| 135 | if (uri.size() >= 2 && uri[1] == '/') { |
| 136 | // Strip the authority following "//". |
| 137 | size_t iAfterAuthority = uri.find('/', 2); |
| 138 | if (iAfterAuthority == string::npos) |
| 139 | // Unusual case: there was only an authority. |
| 140 | return; |
| 141 | else { |
| 142 | uri.erase(0, iAfterAuthority + 1); |
| 143 | trim(uri); |
| 144 | } |
| 145 | } |
| 146 | else { |
| 147 | uri.erase(0, 1); |
| 148 | trim(uri); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | size_t iComponentStart = 0; |
| 153 | |
| 154 | // Unescape the components. |
| 155 | while (iComponentStart < uri.size()) { |
| 156 | size_t iComponentEnd = uri.find("/", iComponentStart); |
| 157 | if (iComponentEnd == string::npos) |
| 158 | iComponentEnd = uri.size(); |
| 159 | |
Jeff Thompson | d8e53e6 | 2013-10-29 16:59:49 -0700 | [diff] [blame] | 160 | Component component(fromEscapedString(&uri[0], iComponentStart, iComponentEnd)); |
Jeff Thompson | 46411c9 | 2013-09-13 19:31:25 -0700 | [diff] [blame] | 161 | // Ignore illegal components. This also gets rid of a trailing '/'. |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 162 | if (!component.empty()) |
Jeff Thompson | 46411c9 | 2013-09-13 19:31:25 -0700 | [diff] [blame] | 163 | components_.push_back(Component(component)); |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 164 | |
| 165 | iComponentStart = iComponentEnd + 1; |
| 166 | } |
| 167 | } |
| 168 | |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 169 | Name& |
| 170 | Name::append(const Name& name) |
| 171 | { |
| 172 | if (&name == this) |
| 173 | // Copying from this name, so need to make a copy first. |
| 174 | return append(Name(name)); |
| 175 | |
| 176 | for (size_t i = 0; i < name.components_.size(); ++i) |
| 177 | components_.push_back(name.components_[i]); |
| 178 | |
| 179 | return *this; |
| 180 | } |
| 181 | |
Alexander Afanasyev | 594cdb2 | 2014-01-03 15:11:33 -0800 | [diff] [blame] | 182 | Name& |
| 183 | Name::appendVersion() |
| 184 | { |
| 185 | appendVersion(ndn_getNowMilliseconds()); |
| 186 | return *this; |
| 187 | } |
| 188 | |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 189 | Name |
| 190 | Name::getSubName(size_t iStartComponent, size_t nComponents) const |
| 191 | { |
| 192 | Name result; |
| 193 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 194 | size_t iEnd = iStartComponent + nComponents; |
| 195 | for (size_t i = iStartComponent; i < iEnd && i < components_.size(); ++i) |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 196 | result.components_.push_back(components_[i]); |
| 197 | |
| 198 | return result; |
| 199 | } |
| 200 | |
| 201 | Name |
| 202 | Name::getSubName(size_t iStartComponent) const |
| 203 | { |
| 204 | Name result; |
| 205 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 206 | for (size_t i = iStartComponent; i < components_.size(); ++i) |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 207 | result.components_.push_back(components_[i]); |
| 208 | |
| 209 | return result; |
| 210 | } |
| 211 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 212 | bool |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 213 | Name::equals(const Name& name) const |
| 214 | { |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 215 | if (components_.size() != name.components_.size()) |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 216 | return false; |
| 217 | |
| 218 | for (size_t i = 0; i < components_.size(); ++i) { |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 219 | if (components_[i].getValue() != name.components_[i].getValue()) |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 223 | return true; |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | bool |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 227 | Name::isPrefixOf(const Name& name) const |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 228 | { |
| 229 | // Imitate ndn_Name_match. |
| 230 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 231 | // This name is longer than the name we are checking it against. |
| 232 | if (components_.size() > name.components_.size()) |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 233 | return false; |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 234 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 235 | // Check if at least one of given components doesn't match. |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 236 | for (size_t i = 0; i < components_.size(); ++i) { |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 237 | if (components_[i].getValue() != name.components_[i].getValue()) |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 238 | return false; |
| 239 | } |
| 240 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 241 | return true; |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 244 | Name::Component |
Jeff Thompson | d8e53e6 | 2013-10-29 16:59:49 -0700 | [diff] [blame] | 245 | Name::fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset) |
| 246 | { |
| 247 | string trimmedString(escapedString + beginOffset, escapedString + endOffset); |
| 248 | trim(trimmedString); |
| 249 | string value = unescape(trimmedString); |
| 250 | |
| 251 | if (value.find_first_not_of(".") == string::npos) { |
| 252 | // Special case for component of only periods. |
| 253 | if (value.size() <= 2) |
| 254 | // Zero, one or two periods is illegal. Ignore this component. |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 255 | return Component(); |
Jeff Thompson | d8e53e6 | 2013-10-29 16:59:49 -0700 | [diff] [blame] | 256 | else |
| 257 | // Remove 3 periods. |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 258 | return Component((const uint8_t *)&value[3], value.size() - 3); |
Jeff Thompson | d8e53e6 | 2013-10-29 16:59:49 -0700 | [diff] [blame] | 259 | } |
| 260 | else |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 261 | return Component((const uint8_t *)&value[0], value.size()); |
Jeff Thompson | d8e53e6 | 2013-10-29 16:59:49 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 264 | Name::Component |
Jeff Thompson | d8e53e6 | 2013-10-29 16:59:49 -0700 | [diff] [blame] | 265 | Name::fromEscapedString(const char *escapedString) |
| 266 | { |
| 267 | return fromEscapedString(escapedString, 0, ::strlen(escapedString)); |
| 268 | } |
| 269 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 270 | void |
Alexander Afanasyev | 2a74276 | 2013-12-28 14:09:46 -0800 | [diff] [blame] | 271 | Name::toEscapedString(const uint8_t *value, size_t valueSize, std::ostream& result) |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 272 | { |
| 273 | bool gotNonDot = false; |
Alexander Afanasyev | 2a74276 | 2013-12-28 14:09:46 -0800 | [diff] [blame] | 274 | for (unsigned i = 0; i < valueSize; ++i) { |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 275 | if (value[i] != 0x2e) { |
| 276 | gotNonDot = true; |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | if (!gotNonDot) { |
| 281 | // Special case for component of zero or more periods. Add 3 periods. |
| 282 | result << "..."; |
Alexander Afanasyev | 2a74276 | 2013-12-28 14:09:46 -0800 | [diff] [blame] | 283 | for (size_t i = 0; i < valueSize; ++i) |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 284 | result << '.'; |
| 285 | } |
| 286 | else { |
| 287 | // In case we need to escape, set to upper case hex and save the previous flags. |
| 288 | ios::fmtflags saveFlags = result.flags(ios::hex | ios::uppercase); |
| 289 | |
Alexander Afanasyev | 2a74276 | 2013-12-28 14:09:46 -0800 | [diff] [blame] | 290 | for (size_t i = 0; i < valueSize; ++i) { |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 291 | uint8_t x = value[i]; |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 292 | // Check for 0-9, A-Z, a-z, (+), (-), (.), (_) |
Jeff Thompson | f68a74a | 2013-11-20 15:16:11 -0800 | [diff] [blame] | 293 | if ((x >= 0x30 && x <= 0x39) || (x >= 0x41 && x <= 0x5a) || |
| 294 | (x >= 0x61 && x <= 0x7a) || x == 0x2b || x == 0x2d || |
| 295 | x == 0x2e || x == 0x5f) |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 296 | result << x; |
| 297 | else { |
| 298 | result << '%'; |
| 299 | if (x < 16) |
| 300 | result << '0'; |
| 301 | result << (unsigned int)x; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // Restore. |
| 306 | result.flags(saveFlags); |
| 307 | } |
| 308 | } |
| 309 | |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 310 | int |
| 311 | Name::compare(const Name& other) const |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 312 | { |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 313 | for (size_t i = 0; i < size() && i < other.size(); ++i) { |
| 314 | int comparison = components_[i].compare(other.components_[i]); |
| 315 | if (comparison == 0) |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 316 | // The components at this index are equal, so check the next components. |
| 317 | continue; |
| 318 | |
| 319 | // Otherwise, the result is based on the components at this index. |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 320 | return comparison; |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 323 | // The components up to min(this.size(), other.size()) are equal, so the shorter name is less. |
| 324 | if (size() < other.size()) |
| 325 | return -1; |
| 326 | else if (size() > other.size()) |
| 327 | return 1; |
| 328 | else |
| 329 | return 0; |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 332 | std::ostream& |
| 333 | operator << (std::ostream& os, const Name& name) |
| 334 | { |
| 335 | if (name.empty()) |
| 336 | { |
| 337 | os << "/"; |
| 338 | } |
| 339 | else |
| 340 | { |
| 341 | for (Name::const_iterator i = name.begin(); i != name.end(); i++) { |
| 342 | os << "/"; |
| 343 | i->toEscapedString(os); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return os; |
| 348 | } |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 349 | |
Alexander Afanasyev | 848c61a | 2014-01-03 13:52:04 -0800 | [diff] [blame] | 350 | const Block & |
| 351 | Name::wireEncode() const |
| 352 | { |
| 353 | if (wire_.hasWire()) |
| 354 | return wire_; |
| 355 | |
| 356 | wire_ = Block(Tlv::Name); |
| 357 | for (Name::const_iterator i = begin(); i != end(); i++) { |
| 358 | OBufferStream os; |
| 359 | Tlv::writeVarNumber(os, Tlv::NameComponent); |
| 360 | Tlv::writeVarNumber(os, i->getValue().size()); |
| 361 | os.write(reinterpret_cast<const char*>(i->getValue().buf()), i->getValue().size()); |
| 362 | |
| 363 | wire_.push_back(Block(os.buf())); |
| 364 | } |
| 365 | |
| 366 | wire_.encode(); |
| 367 | return wire_; |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | Name::wireDecode(const Block &wire) |
| 372 | { |
Alexander Afanasyev | 2a74276 | 2013-12-28 14:09:46 -0800 | [diff] [blame] | 373 | clear(); |
| 374 | |
Alexander Afanasyev | 848c61a | 2014-01-03 13:52:04 -0800 | [diff] [blame] | 375 | wire_ = wire; |
| 376 | wire_.parse(); |
| 377 | |
Alexander Afanasyev | 7b4a7fc | 2014-01-10 10:11:07 -0800 | [diff] [blame] | 378 | components_.clear(); |
| 379 | components_.reserve(wire_.getAll().size()); |
| 380 | |
Alexander Afanasyev | 848c61a | 2014-01-03 13:52:04 -0800 | [diff] [blame] | 381 | for (Block::element_const_iterator i = wire_.getAll().begin(); |
| 382 | i != wire_.getAll().end(); |
| 383 | ++i) |
| 384 | { |
| 385 | append(i->value(), i->value_size()); |
| 386 | } |
| 387 | } |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 388 | |
| 389 | |
| 390 | size_t |
| 391 | Name::wireEncode (EncodingBuffer& blk) |
| 392 | { |
| 393 | size_t total_len = 0; |
| 394 | |
| 395 | for (std::vector<Component>::reverse_iterator i = components_.rbegin (); |
| 396 | i != components_.rend (); |
| 397 | ++i) |
| 398 | { |
| 399 | total_len += i->wireEncode (blk); |
| 400 | } |
| 401 | |
| 402 | total_len += blk.prependVarNumber (total_len); |
| 403 | total_len += blk.prependVarNumber (Tlv::Name); |
| 404 | return total_len; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 405 | } |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 406 | |
| 407 | } // namespace ndn |