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 | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Jeff Thompson | 5490977 | 2013-07-07 22:38:57 -0700 | [diff] [blame] | 8 | #include <stdexcept> |
Jeff Thompson | b8f1b13 | 2013-08-13 11:07:43 -0700 | [diff] [blame] | 9 | #include <algorithm> |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 10 | #include <ndn-cpp/name.hpp> |
| 11 | #include "c/name.h" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 12 | |
| 13 | using namespace std; |
Jeff Thompson | d4144fe | 2013-09-18 15:59:57 -0700 | [diff] [blame] | 14 | using namespace ndn::ptr_lib; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 15 | |
| 16 | namespace ndn { |
| 17 | |
Jeff Thompson | 26c63d6 | 2013-07-02 18:00:26 -0700 | [diff] [blame] | 18 | static const char *WHITESPACE_CHARS = " \n\r\t"; |
| 19 | |
| 20 | /** |
| 21 | * Modify str in place to erase whitespace on the left. |
| 22 | * @param str |
| 23 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 24 | static inline void |
| 25 | trimLeft(string& str) |
Jeff Thompson | 26c63d6 | 2013-07-02 18:00:26 -0700 | [diff] [blame] | 26 | { |
| 27 | size_t found = str.find_first_not_of(WHITESPACE_CHARS); |
| 28 | if (found != string::npos) { |
| 29 | if (found > 0) |
| 30 | str.erase(0, found); |
| 31 | } |
| 32 | else |
| 33 | // All whitespace |
| 34 | str.clear(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Modify str in place to erase whitespace on the right. |
| 39 | * @param str |
| 40 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 41 | static inline void |
| 42 | trimRight(string& str) |
Jeff Thompson | 26c63d6 | 2013-07-02 18:00:26 -0700 | [diff] [blame] | 43 | { |
| 44 | size_t found = str.find_last_not_of(WHITESPACE_CHARS); |
| 45 | if (found != string::npos) { |
| 46 | if (found + 1 < str.size()) |
| 47 | str.erase(found + 1); |
| 48 | } |
| 49 | else |
| 50 | // All whitespace |
| 51 | str.clear(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Modify str in place to erase whitespace on the left and right. |
| 56 | * @param str |
| 57 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 58 | static void |
| 59 | trim(string& str) |
Jeff Thompson | 26c63d6 | 2013-07-02 18:00:26 -0700 | [diff] [blame] | 60 | { |
| 61 | trimLeft(str); |
| 62 | trimRight(str); |
| 63 | } |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 64 | |
Jeff Thompson | 26c63d6 | 2013-07-02 18:00:26 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Convert the hex character to an integer from 0 to 15, or -1 if not a hex character. |
| 67 | * @param c |
| 68 | * @return |
| 69 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 70 | static int |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 71 | fromHexChar(uint8_t c) |
Jeff Thompson | 26c63d6 | 2013-07-02 18:00:26 -0700 | [diff] [blame] | 72 | { |
| 73 | if (c >= '0' && c <= '9') |
| 74 | return (int)c - (int)'0'; |
| 75 | else if (c >= 'A' && c <= 'F') |
| 76 | return (int)c - (int)'A' + 10; |
| 77 | else if (c >= 'a' && c <= 'f') |
| 78 | return (int)c - (int)'a' + 10; |
| 79 | else |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Return a copy of str, converting each escaped "%XX" to the char value. |
| 85 | * @param str |
| 86 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 87 | static string |
| 88 | unescape(const string& str) |
Jeff Thompson | 26c63d6 | 2013-07-02 18:00:26 -0700 | [diff] [blame] | 89 | { |
| 90 | ostringstream result; |
| 91 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 92 | for (size_t i = 0; i < str.size(); ++i) { |
Jeff Thompson | 26c63d6 | 2013-07-02 18:00:26 -0700 | [diff] [blame] | 93 | if (str[i] == '%' && i + 2 < str.size()) { |
| 94 | int hi = fromHexChar(str[i + 1]); |
| 95 | int lo = fromHexChar(str[i + 2]); |
| 96 | |
| 97 | if (hi < 0 || lo < 0) |
| 98 | // Invalid hex characters, so just keep the escaped string. |
| 99 | result << str[i] << str[i + 1] << str[i + 2]; |
| 100 | else |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 101 | result << (uint8_t)(16 * hi + lo); |
Jeff Thompson | 26c63d6 | 2013-07-02 18:00:26 -0700 | [diff] [blame] | 102 | |
| 103 | // Skip ahead past the escaped value. |
| 104 | i += 2; |
| 105 | } |
| 106 | else |
| 107 | // Just copy through. |
| 108 | result << str[i]; |
| 109 | } |
| 110 | |
| 111 | return result.str(); |
| 112 | } |
| 113 | |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame] | 114 | uint64_t Name::Component::toNumberWithMarker(uint8_t marker) const |
| 115 | { |
| 116 | struct ndn_NameComponent componentStruct; |
| 117 | get(componentStruct); |
| 118 | uint64_t result; |
| 119 | |
| 120 | ndn_Error error; |
| 121 | if ((error = ndn_NameComponent_toNumberWithMarker(&componentStruct, marker, &result))) |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 122 | throw runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame] | 123 | |
| 124 | return result; |
| 125 | } |
| 126 | |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 127 | Name::Component |
| 128 | Name::Component::fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset) |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 129 | { |
Jeff Thompson | c1c12e4 | 2013-09-13 19:08:45 -0700 | [diff] [blame] | 130 | string trimmedString(escapedString + beginOffset, escapedString + endOffset); |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 131 | trim(trimmedString); |
| 132 | string component = unescape(trimmedString); |
| 133 | |
| 134 | if (component.find_first_not_of(".") == string::npos) { |
| 135 | // Special case for component of only periods. |
| 136 | if (component.size() <= 2) |
| 137 | // Zero, one or two periods is illegal. Ignore this component. |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 138 | return Component(); |
Jeff Thompson | 995aba5 | 2013-09-12 12:04:52 -0700 | [diff] [blame] | 139 | else |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 140 | // Remove 3 periods. |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 141 | return Component((const uint8_t *)&component[3], component.size() - 3); |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 142 | } |
Jeff Thompson | 995aba5 | 2013-09-12 12:04:52 -0700 | [diff] [blame] | 143 | else |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 144 | return Component((const uint8_t *)&component[0], component.size()); |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 147 | Name::Component |
| 148 | Name::Component::fromNumber(uint64_t number) |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 149 | { |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 150 | shared_ptr<vector<uint8_t> > value(new vector<uint8_t>()); |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 151 | |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 152 | // First encode in little endian. |
| 153 | while (number != 0) { |
| 154 | value->push_back(number & 0xff); |
| 155 | number >>= 8; |
| 156 | } |
| 157 | |
| 158 | // Make it big endian. |
| 159 | reverse(value->begin(), value->end()); |
| 160 | return Blob(value); |
| 161 | } |
| 162 | |
| 163 | Name::Component |
| 164 | Name::Component::fromNumberWithMarker(uint64_t number, uint8_t marker) |
| 165 | { |
| 166 | shared_ptr<vector<uint8_t> > value(new vector<uint8_t>()); |
| 167 | |
| 168 | // Add the leading marker. |
| 169 | value->push_back(marker); |
Jeff Thompson | b8f1b13 | 2013-08-13 11:07:43 -0700 | [diff] [blame] | 170 | |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 171 | // First encode in little endian. |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 172 | while (number != 0) { |
| 173 | value->push_back(number & 0xff); |
| 174 | number >>= 8; |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 177 | // Make it big endian. |
Jeff Thompson | 995aba5 | 2013-09-12 12:04:52 -0700 | [diff] [blame] | 178 | reverse(value->begin() + 1, value->end()); |
Jeff Thompson | 46411c9 | 2013-09-13 19:31:25 -0700 | [diff] [blame] | 179 | return Blob(value); |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 182 | void |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 183 | Name::Component::get(struct ndn_NameComponent& componentStruct) const |
| 184 | { |
| 185 | value_.get(componentStruct.value); |
| 186 | } |
| 187 | |
| 188 | uint64_t |
| 189 | Name::Component::toNumber() const |
| 190 | { |
| 191 | struct ndn_NameComponent componentStruct; |
| 192 | get(componentStruct); |
| 193 | return ndn_NameComponent_toNumber(&componentStruct); |
| 194 | } |
| 195 | |
| 196 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 197 | Name::set(const char *uri_cstr) |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 198 | { |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 199 | components_.clear(); |
| 200 | |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 201 | string uri = uri_cstr; |
| 202 | trim(uri); |
| 203 | if (uri.size() == 0) |
| 204 | return; |
| 205 | |
| 206 | size_t iColon = uri.find(':'); |
| 207 | if (iColon != string::npos) { |
| 208 | // Make sure the colon came before a '/'. |
| 209 | size_t iFirstSlash = uri.find('/'); |
| 210 | if (iFirstSlash == string::npos || iColon < iFirstSlash) { |
| 211 | // Omit the leading protocol such as ndn: |
| 212 | uri.erase(0, iColon + 1); |
| 213 | trim(uri); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Trim the leading slash and possibly the authority. |
| 218 | if (uri[0] == '/') { |
| 219 | if (uri.size() >= 2 && uri[1] == '/') { |
| 220 | // Strip the authority following "//". |
| 221 | size_t iAfterAuthority = uri.find('/', 2); |
| 222 | if (iAfterAuthority == string::npos) |
| 223 | // Unusual case: there was only an authority. |
| 224 | return; |
| 225 | else { |
| 226 | uri.erase(0, iAfterAuthority + 1); |
| 227 | trim(uri); |
| 228 | } |
| 229 | } |
| 230 | else { |
| 231 | uri.erase(0, 1); |
| 232 | trim(uri); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | size_t iComponentStart = 0; |
| 237 | |
| 238 | // Unescape the components. |
| 239 | while (iComponentStart < uri.size()) { |
| 240 | size_t iComponentEnd = uri.find("/", iComponentStart); |
| 241 | if (iComponentEnd == string::npos) |
| 242 | iComponentEnd = uri.size(); |
| 243 | |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 244 | Component component = Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd); |
Jeff Thompson | 46411c9 | 2013-09-13 19:31:25 -0700 | [diff] [blame] | 245 | // Ignore illegal components. This also gets rid of a trailing '/'. |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 246 | if (component.getValue()) |
Jeff Thompson | 46411c9 | 2013-09-13 19:31:25 -0700 | [diff] [blame] | 247 | components_.push_back(Component(component)); |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 248 | |
| 249 | iComponentStart = iComponentEnd + 1; |
| 250 | } |
| 251 | } |
| 252 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 253 | void |
| 254 | Name::get(struct ndn_Name& nameStruct) const |
Jeff Thompson | 4881511 | 2013-06-28 18:22:48 -0700 | [diff] [blame] | 255 | { |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 256 | if (nameStruct.maxComponents < components_.size()) |
| 257 | throw runtime_error("nameStruct.maxComponents must be >= this name getNComponents()"); |
| 258 | |
| 259 | nameStruct.nComponents = components_.size(); |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 260 | for (size_t i = 0; i < nameStruct.nComponents; ++i) |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 261 | components_[i].get(nameStruct.components[i]); |
Jeff Thompson | 4881511 | 2013-06-28 18:22:48 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 264 | void |
| 265 | Name::set(const struct ndn_Name& nameStruct) |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 266 | { |
| 267 | clear(); |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 268 | for (size_t i = 0; i < nameStruct.nComponents; ++i) |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 269 | addComponent(nameStruct.components[i].value.value, nameStruct.components[i].value.length); |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 272 | Name& |
| 273 | Name::append(const Name& name) |
| 274 | { |
| 275 | if (&name == this) |
| 276 | // Copying from this name, so need to make a copy first. |
| 277 | return append(Name(name)); |
| 278 | |
| 279 | for (size_t i = 0; i < name.components_.size(); ++i) |
| 280 | components_.push_back(name.components_[i]); |
| 281 | |
| 282 | return *this; |
| 283 | } |
| 284 | |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 285 | string |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 286 | Name::toUri() const |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 287 | { |
Jeff Thompson | 4b2479a | 2013-07-02 15:37:39 -0700 | [diff] [blame] | 288 | if (components_.size() == 0) |
| 289 | return "/"; |
| 290 | |
| 291 | ostringstream result; |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 292 | for (size_t i = 0; i < components_.size(); ++i) { |
Jeff Thompson | 4b2479a | 2013-07-02 15:37:39 -0700 | [diff] [blame] | 293 | result << "/"; |
Jeff Thompson | 9bdb3b2 | 2013-09-12 12:42:13 -0700 | [diff] [blame] | 294 | toEscapedString(*components_[i].getValue(), result); |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Jeff Thompson | 4b2479a | 2013-07-02 15:37:39 -0700 | [diff] [blame] | 297 | return result.str(); |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 300 | Name |
| 301 | Name::getSubName(size_t iStartComponent, size_t nComponents) const |
| 302 | { |
| 303 | Name result; |
| 304 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 305 | size_t iEnd = iStartComponent + nComponents; |
| 306 | for (size_t i = iStartComponent; i < iEnd && i < components_.size(); ++i) |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 307 | result.components_.push_back(components_[i]); |
| 308 | |
| 309 | return result; |
| 310 | } |
| 311 | |
| 312 | Name |
| 313 | Name::getSubName(size_t iStartComponent) const |
| 314 | { |
| 315 | Name result; |
| 316 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 317 | for (size_t i = iStartComponent; i < components_.size(); ++i) |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 318 | result.components_.push_back(components_[i]); |
| 319 | |
| 320 | return result; |
| 321 | } |
| 322 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 323 | bool |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 324 | Name::equals(const Name& name) const |
| 325 | { |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 326 | if (components_.size() != name.components_.size()) |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 327 | return false; |
| 328 | |
| 329 | for (size_t i = 0; i < components_.size(); ++i) { |
| 330 | if (*components_[i].getValue() != *name.components_[i].getValue()) |
| 331 | return false; |
| 332 | } |
| 333 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 334 | return true; |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | bool |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 338 | Name::match(const Name& name) const |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 339 | { |
| 340 | // Imitate ndn_Name_match. |
| 341 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 342 | // This name is longer than the name we are checking it against. |
| 343 | if (components_.size() > name.components_.size()) |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 344 | return false; |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 345 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 346 | // Check if at least one of given components doesn't match. |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 347 | for (size_t i = 0; i < components_.size(); ++i) { |
| 348 | if (*components_[i].getValue() != *name.components_[i].getValue()) |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 352 | return true; |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 355 | void |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 356 | Name::toEscapedString(const vector<uint8_t>& value, ostringstream& result) |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 357 | { |
| 358 | bool gotNonDot = false; |
| 359 | for (unsigned i = 0; i < value.size(); ++i) { |
| 360 | if (value[i] != 0x2e) { |
| 361 | gotNonDot = true; |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | if (!gotNonDot) { |
| 366 | // Special case for component of zero or more periods. Add 3 periods. |
| 367 | result << "..."; |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 368 | for (size_t i = 0; i < value.size(); ++i) |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 369 | result << '.'; |
| 370 | } |
| 371 | else { |
| 372 | // In case we need to escape, set to upper case hex and save the previous flags. |
| 373 | ios::fmtflags saveFlags = result.flags(ios::hex | ios::uppercase); |
| 374 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 375 | for (size_t i = 0; i < value.size(); ++i) { |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 376 | uint8_t x = value[i]; |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 377 | // Check for 0-9, A-Z, a-z, (+), (-), (.), (_) |
| 378 | if (x >= 0x30 && x <= 0x39 || x >= 0x41 && x <= 0x5a || |
| 379 | x >= 0x61 && x <= 0x7a || x == 0x2b || x == 0x2d || |
| 380 | x == 0x2e || x == 0x5f) |
| 381 | result << x; |
| 382 | else { |
| 383 | result << '%'; |
| 384 | if (x < 16) |
| 385 | result << '0'; |
| 386 | result << (unsigned int)x; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | // Restore. |
| 391 | result.flags(saveFlags); |
| 392 | } |
| 393 | } |
| 394 | |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 395 | string |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 396 | Name::toEscapedString(const vector<uint8_t>& value) |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 397 | { |
| 398 | ostringstream result; |
| 399 | toEscapedString(value, result); |
| 400 | return result.str(); |
| 401 | } |
| 402 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 403 | } |