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 | */ |
| 20 | static inline void trimLeft(string &str) |
| 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 | */ |
| 36 | static inline void trimRight(string &str) |
| 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 | */ |
| 52 | static void trim(string &str) |
| 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 | */ |
| 79 | static string unescape(const string &str) |
| 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; |
| 116 | else { |
| 117 | // Remove 3 periods. |
| 118 | value_.clear(); |
| 119 | value_.insert(value_.begin(), component.begin() + 3, component.end()); |
| 120 | } |
| 121 | } |
| 122 | else { |
| 123 | value_.clear(); |
| 124 | value_.insert(value_.begin(), component.begin(), component.end()); |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 130 | void Name::Component::setSegment(unsigned long segment) |
| 131 | { |
| 132 | value_.clear(); |
| 133 | |
Jeff Thompson | b8f1b13 | 2013-08-13 11:07:43 -0700 | [diff] [blame] | 134 | // Add the leading zero. |
| 135 | value_.push_back(0); |
| 136 | |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 137 | // First encode in little endian. |
| 138 | while (segment != 0) { |
| 139 | value_.push_back(segment & 0xff); |
| 140 | segment >>= 8; |
| 141 | } |
| 142 | |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 143 | // Make it big endian. |
Jeff Thompson | b8f1b13 | 2013-08-13 11:07:43 -0700 | [diff] [blame] | 144 | reverse(value_.begin() + 1, value_.end()); |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 147 | void Name::set(const char *uri_cstr) |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 148 | { |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 149 | components_.clear(); |
| 150 | |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 151 | string uri = uri_cstr; |
| 152 | trim(uri); |
| 153 | if (uri.size() == 0) |
| 154 | return; |
| 155 | |
| 156 | size_t iColon = uri.find(':'); |
| 157 | if (iColon != string::npos) { |
| 158 | // Make sure the colon came before a '/'. |
| 159 | size_t iFirstSlash = uri.find('/'); |
| 160 | if (iFirstSlash == string::npos || iColon < iFirstSlash) { |
| 161 | // Omit the leading protocol such as ndn: |
| 162 | uri.erase(0, iColon + 1); |
| 163 | trim(uri); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Trim the leading slash and possibly the authority. |
| 168 | if (uri[0] == '/') { |
| 169 | if (uri.size() >= 2 && uri[1] == '/') { |
| 170 | // Strip the authority following "//". |
| 171 | size_t iAfterAuthority = uri.find('/', 2); |
| 172 | if (iAfterAuthority == string::npos) |
| 173 | // Unusual case: there was only an authority. |
| 174 | return; |
| 175 | else { |
| 176 | uri.erase(0, iAfterAuthority + 1); |
| 177 | trim(uri); |
| 178 | } |
| 179 | } |
| 180 | else { |
| 181 | uri.erase(0, 1); |
| 182 | trim(uri); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | size_t iComponentStart = 0; |
| 187 | |
| 188 | // Unescape the components. |
| 189 | while (iComponentStart < uri.size()) { |
| 190 | size_t iComponentEnd = uri.find("/", iComponentStart); |
| 191 | if (iComponentEnd == string::npos) |
| 192 | iComponentEnd = uri.size(); |
| 193 | |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 194 | components_.push_back(Component()); |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 195 | if (!components_[components_.size() - 1].setFromEscapedString(&uri[iComponentStart], &uri[iComponentEnd])) |
| 196 | // Ignore the illegal component. This also gets rid of a trailing '/'. |
| 197 | components_.pop_back(); |
| 198 | |
| 199 | iComponentStart = iComponentEnd + 1; |
| 200 | } |
| 201 | } |
| 202 | |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 203 | void Name::get(struct ndn_Name &nameStruct) const |
Jeff Thompson | 4881511 | 2013-06-28 18:22:48 -0700 | [diff] [blame] | 204 | { |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 205 | if (nameStruct.maxComponents < components_.size()) |
| 206 | throw runtime_error("nameStruct.maxComponents must be >= this name getNComponents()"); |
| 207 | |
| 208 | nameStruct.nComponents = components_.size(); |
| 209 | for (unsigned int i = 0; i < nameStruct.nComponents; ++i) |
| 210 | components_[i].get(nameStruct.components[i]); |
Jeff Thompson | 4881511 | 2013-06-28 18:22:48 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Jeff Thompson | dd3d229 | 2013-07-10 11:59:44 -0700 | [diff] [blame] | 213 | void Name::set(const struct ndn_Name &nameStruct) |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 214 | { |
| 215 | clear(); |
Jeff Thompson | ccb13c1 | 2013-07-01 18:16:00 -0700 | [diff] [blame] | 216 | for (unsigned int i = 0; i < nameStruct.nComponents; ++i) |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 217 | addComponent(nameStruct.components[i].value, nameStruct.components[i].valueLength); |
| 218 | } |
| 219 | |
Jeff Thompson | 21844fc | 2013-08-08 14:52:51 -0700 | [diff] [blame] | 220 | std::string Name::toUri() const |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 221 | { |
Jeff Thompson | 4b2479a | 2013-07-02 15:37:39 -0700 | [diff] [blame] | 222 | if (components_.size() == 0) |
| 223 | return "/"; |
| 224 | |
| 225 | ostringstream result; |
Jeff Thompson | ccb13c1 | 2013-07-01 18:16:00 -0700 | [diff] [blame] | 226 | for (unsigned int i = 0; i < components_.size(); ++i) { |
Jeff Thompson | 4b2479a | 2013-07-02 15:37:39 -0700 | [diff] [blame] | 227 | result << "/"; |
| 228 | toEscapedString(components_[i].getValue(), result); |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Jeff Thompson | 4b2479a | 2013-07-02 15:37:39 -0700 | [diff] [blame] | 231 | return result.str(); |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Jeff Thompson | 15dc964 | 2013-08-27 17:19:35 -0700 | [diff] [blame] | 234 | bool Name::match(const Name &name) const |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 235 | { |
| 236 | // Imitate ndn_Name_match. |
| 237 | |
| 238 | // This name is longer than the name we are checking it against. |
| 239 | if (components_.size() > name.components_.size()) |
| 240 | return 0; |
| 241 | |
| 242 | // Check if at least one of given components doesn't match. |
| 243 | unsigned int i; |
| 244 | for (i = 0; i < components_.size(); ++i) { |
| 245 | const Component &selfComponent = components_[i]; |
| 246 | const Component &nameComponent = name.components_[i]; |
| 247 | |
| 248 | if (selfComponent.getValue() != nameComponent.getValue()) |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | return true; |
| 253 | } |
| 254 | |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 255 | void Name::toEscapedString(const vector<unsigned char> &value, ostringstream &result) |
| 256 | { |
| 257 | bool gotNonDot = false; |
| 258 | for (unsigned i = 0; i < value.size(); ++i) { |
| 259 | if (value[i] != 0x2e) { |
| 260 | gotNonDot = true; |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | if (!gotNonDot) { |
| 265 | // Special case for component of zero or more periods. Add 3 periods. |
| 266 | result << "..."; |
| 267 | for (unsigned int i = 0; i < value.size(); ++i) |
| 268 | result << '.'; |
| 269 | } |
| 270 | else { |
| 271 | // In case we need to escape, set to upper case hex and save the previous flags. |
| 272 | ios::fmtflags saveFlags = result.flags(ios::hex | ios::uppercase); |
| 273 | |
| 274 | for (unsigned int i = 0; i < value.size(); ++i) { |
| 275 | unsigned char x = value[i]; |
| 276 | // Check for 0-9, A-Z, a-z, (+), (-), (.), (_) |
| 277 | if (x >= 0x30 && x <= 0x39 || x >= 0x41 && x <= 0x5a || |
| 278 | x >= 0x61 && x <= 0x7a || x == 0x2b || x == 0x2d || |
| 279 | x == 0x2e || x == 0x5f) |
| 280 | result << x; |
| 281 | else { |
| 282 | result << '%'; |
| 283 | if (x < 16) |
| 284 | result << '0'; |
| 285 | result << (unsigned int)x; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // Restore. |
| 290 | result.flags(saveFlags); |
| 291 | } |
| 292 | } |
| 293 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 294 | } |