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