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