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