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