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