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