blob: d8174e6f1d3da1849203afc65a8043d46594f076 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07005 */
6
Jeff Thompson54909772013-07-07 22:38:57 -07007#include <stdexcept>
Jeff Thompsonb8f1b132013-08-13 11:07:43 -07008#include <algorithm>
Jeff Thompson53412192013-08-06 13:35:50 -07009#include "name.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070010
11using namespace std;
Jeff Thompsond4144fe2013-09-18 15:59:57 -070012using namespace ndn::ptr_lib;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070013
14namespace ndn {
15
Jeff Thompson26c63d62013-07-02 18:00:26 -070016static const char *WHITESPACE_CHARS = " \n\r\t";
17
18/**
19 * Modify str in place to erase whitespace on the left.
20 * @param str
21 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070022static inline void
23trimLeft(string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070024{
25 size_t found = str.find_first_not_of(WHITESPACE_CHARS);
26 if (found != string::npos) {
27 if (found > 0)
28 str.erase(0, found);
29 }
30 else
31 // All whitespace
32 str.clear();
33}
34
35/**
36 * Modify str in place to erase whitespace on the right.
37 * @param str
38 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070039static inline void
40trimRight(string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070041{
42 size_t found = str.find_last_not_of(WHITESPACE_CHARS);
43 if (found != string::npos) {
44 if (found + 1 < str.size())
45 str.erase(found + 1);
46 }
47 else
48 // All whitespace
49 str.clear();
50}
51
52/**
53 * Modify str in place to erase whitespace on the left and right.
54 * @param str
55 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070056static void
57trim(string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070058{
59 trimLeft(str);
60 trimRight(str);
61}
Jeff Thompson443398d2013-07-02 19:45:46 -070062
Jeff Thompson26c63d62013-07-02 18:00:26 -070063/**
64 * Convert the hex character to an integer from 0 to 15, or -1 if not a hex character.
65 * @param c
66 * @return
67 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070068static int
Jeff Thompson10ad12a2013-09-24 16:19:11 -070069fromHexChar(uint8_t c)
Jeff Thompson26c63d62013-07-02 18:00:26 -070070{
71 if (c >= '0' && c <= '9')
72 return (int)c - (int)'0';
73 else if (c >= 'A' && c <= 'F')
74 return (int)c - (int)'A' + 10;
75 else if (c >= 'a' && c <= 'f')
76 return (int)c - (int)'a' + 10;
77 else
78 return -1;
79}
80
81/**
82 * Return a copy of str, converting each escaped "%XX" to the char value.
83 * @param str
84 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070085static string
86unescape(const string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070087{
88 ostringstream result;
89
Jeff Thompson97223af2013-09-24 17:01:27 -070090 for (size_t i = 0; i < str.size(); ++i) {
Jeff Thompson26c63d62013-07-02 18:00:26 -070091 if (str[i] == '%' && i + 2 < str.size()) {
92 int hi = fromHexChar(str[i + 1]);
93 int lo = fromHexChar(str[i + 2]);
94
95 if (hi < 0 || lo < 0)
96 // Invalid hex characters, so just keep the escaped string.
97 result << str[i] << str[i + 1] << str[i + 2];
98 else
Jeff Thompson10ad12a2013-09-24 16:19:11 -070099 result << (uint8_t)(16 * hi + lo);
Jeff Thompson26c63d62013-07-02 18:00:26 -0700100
101 // Skip ahead past the escaped value.
102 i += 2;
103 }
104 else
105 // Just copy through.
106 result << str[i];
107 }
108
109 return result.str();
110}
111
Jeff Thompson0050abe2013-09-17 12:50:25 -0700112Blob
Jeff Thompson97223af2013-09-24 17:01:27 -0700113Name::Component::makeFromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset)
Jeff Thompson443398d2013-07-02 19:45:46 -0700114{
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700115 string trimmedString(escapedString + beginOffset, escapedString + endOffset);
Jeff Thompson443398d2013-07-02 19:45:46 -0700116 trim(trimmedString);
117 string component = unescape(trimmedString);
118
119 if (component.find_first_not_of(".") == string::npos) {
120 // Special case for component of only periods.
121 if (component.size() <= 2)
122 // Zero, one or two periods is illegal. Ignore this component.
Jeff Thompson46411c92013-09-13 19:31:25 -0700123 return Blob();
Jeff Thompson995aba52013-09-12 12:04:52 -0700124 else
Jeff Thompson443398d2013-07-02 19:45:46 -0700125 // Remove 3 periods.
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700126 return Blob((const uint8_t *)&component[3], component.size() - 3);
Jeff Thompson443398d2013-07-02 19:45:46 -0700127 }
Jeff Thompson995aba52013-09-12 12:04:52 -0700128 else
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700129 return Blob((const uint8_t *)&component[0], component.size());
Jeff Thompson443398d2013-07-02 19:45:46 -0700130}
131
Jeff Thompson0050abe2013-09-17 12:50:25 -0700132Blob
133Name::Component::makeSegment(unsigned long segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700134{
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700135 shared_ptr<vector<uint8_t> > value(new vector<uint8_t>());
Jeff Thompson8aac1992013-08-12 17:26:02 -0700136
Jeff Thompsonb8f1b132013-08-13 11:07:43 -0700137 // Add the leading zero.
Jeff Thompson995aba52013-09-12 12:04:52 -0700138 value->push_back(0);
Jeff Thompsonb8f1b132013-08-13 11:07:43 -0700139
Jeff Thompson8aac1992013-08-12 17:26:02 -0700140 // First encode in little endian.
141 while (segment != 0) {
Jeff Thompson995aba52013-09-12 12:04:52 -0700142 value->push_back(segment & 0xff);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700143 segment >>= 8;
144 }
145
Jeff Thompson8aac1992013-08-12 17:26:02 -0700146 // Make it big endian.
Jeff Thompson995aba52013-09-12 12:04:52 -0700147 reverse(value->begin() + 1, value->end());
Jeff Thompson46411c92013-09-13 19:31:25 -0700148 return Blob(value);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700149}
150
Jeff Thompson0050abe2013-09-17 12:50:25 -0700151void
152Name::set(const char *uri_cstr)
Jeff Thompson443398d2013-07-02 19:45:46 -0700153{
Jeff Thompson67515bd2013-08-15 17:43:22 -0700154 components_.clear();
155
Jeff Thompson443398d2013-07-02 19:45:46 -0700156 string uri = uri_cstr;
157 trim(uri);
158 if (uri.size() == 0)
159 return;
160
161 size_t iColon = uri.find(':');
162 if (iColon != string::npos) {
163 // Make sure the colon came before a '/'.
164 size_t iFirstSlash = uri.find('/');
165 if (iFirstSlash == string::npos || iColon < iFirstSlash) {
166 // Omit the leading protocol such as ndn:
167 uri.erase(0, iColon + 1);
168 trim(uri);
169 }
170 }
171
172 // Trim the leading slash and possibly the authority.
173 if (uri[0] == '/') {
174 if (uri.size() >= 2 && uri[1] == '/') {
175 // Strip the authority following "//".
176 size_t iAfterAuthority = uri.find('/', 2);
177 if (iAfterAuthority == string::npos)
178 // Unusual case: there was only an authority.
179 return;
180 else {
181 uri.erase(0, iAfterAuthority + 1);
182 trim(uri);
183 }
184 }
185 else {
186 uri.erase(0, 1);
187 trim(uri);
188 }
189 }
190
191 size_t iComponentStart = 0;
192
193 // Unescape the components.
194 while (iComponentStart < uri.size()) {
195 size_t iComponentEnd = uri.find("/", iComponentStart);
196 if (iComponentEnd == string::npos)
197 iComponentEnd = uri.size();
198
Jeff Thompson46411c92013-09-13 19:31:25 -0700199 Blob component = Component::makeFromEscapedString(&uri[0], iComponentStart, iComponentEnd);
200 // Ignore illegal components. This also gets rid of a trailing '/'.
201 if (component)
202 components_.push_back(Component(component));
Jeff Thompson443398d2013-07-02 19:45:46 -0700203
204 iComponentStart = iComponentEnd + 1;
205 }
206}
207
Jeff Thompson0050abe2013-09-17 12:50:25 -0700208void
209Name::get(struct ndn_Name& nameStruct) const
Jeff Thompson48815112013-06-28 18:22:48 -0700210{
Jeff Thompson016ed642013-07-02 14:39:06 -0700211 if (nameStruct.maxComponents < components_.size())
212 throw runtime_error("nameStruct.maxComponents must be >= this name getNComponents()");
213
214 nameStruct.nComponents = components_.size();
Jeff Thompson97223af2013-09-24 17:01:27 -0700215 for (size_t i = 0; i < nameStruct.nComponents; ++i)
Jeff Thompson016ed642013-07-02 14:39:06 -0700216 components_[i].get(nameStruct.components[i]);
Jeff Thompson48815112013-06-28 18:22:48 -0700217}
218
Jeff Thompson0050abe2013-09-17 12:50:25 -0700219void
220Name::set(const struct ndn_Name& nameStruct)
Jeff Thompsonb468c312013-07-01 17:50:14 -0700221{
222 clear();
Jeff Thompson97223af2013-09-24 17:01:27 -0700223 for (size_t i = 0; i < nameStruct.nComponents; ++i)
Jeff Thompson93034532013-10-08 11:52:43 -0700224 addComponent(nameStruct.components[i].value.value, nameStruct.components[i].value.length);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700225}
226
Jeff Thompson26b0d792013-09-23 16:19:01 -0700227Name&
228Name::append(const Name& name)
229{
230 if (&name == this)
231 // Copying from this name, so need to make a copy first.
232 return append(Name(name));
233
234 for (size_t i = 0; i < name.components_.size(); ++i)
235 components_.push_back(name.components_[i]);
236
237 return *this;
238}
239
Jeff Thompson0050abe2013-09-17 12:50:25 -0700240std::string
241Name::toUri() const
Jeff Thompsone6063512013-07-01 15:11:28 -0700242{
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700243 if (components_.size() == 0)
244 return "/";
245
246 ostringstream result;
Jeff Thompson97223af2013-09-24 17:01:27 -0700247 for (size_t i = 0; i < components_.size(); ++i) {
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700248 result << "/";
Jeff Thompson9bdb3b22013-09-12 12:42:13 -0700249 toEscapedString(*components_[i].getValue(), result);
Jeff Thompsone6063512013-07-01 15:11:28 -0700250 }
251
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700252 return result.str();
Jeff Thompsone6063512013-07-01 15:11:28 -0700253}
254
Jeff Thompsond0159d72013-09-23 13:34:15 -0700255Name
256Name::getSubName(size_t iStartComponent, size_t nComponents) const
257{
258 Name result;
259
Jeff Thompson97223af2013-09-24 17:01:27 -0700260 size_t iEnd = iStartComponent + nComponents;
261 for (size_t i = iStartComponent; i < iEnd && i < components_.size(); ++i)
Jeff Thompsond0159d72013-09-23 13:34:15 -0700262 result.components_.push_back(components_[i]);
263
264 return result;
265}
266
267Name
268Name::getSubName(size_t iStartComponent) const
269{
270 Name result;
271
Jeff Thompson97223af2013-09-24 17:01:27 -0700272 for (size_t i = iStartComponent; i < components_.size(); ++i)
Jeff Thompsond0159d72013-09-23 13:34:15 -0700273 result.components_.push_back(components_[i]);
274
275 return result;
276}
277
Jeff Thompson0050abe2013-09-17 12:50:25 -0700278bool
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700279Name::equals(const Name& name) const
280{
281 if (components_.size() != name.components_.size())
282 return false;
283
284 for (size_t i = 0; i < components_.size(); ++i) {
285 if (*components_[i].getValue() != *name.components_[i].getValue())
286 return false;
287 }
288
289 return true;
290}
291
292bool
Jeff Thompson0050abe2013-09-17 12:50:25 -0700293Name::match(const Name& name) const
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700294{
295 // Imitate ndn_Name_match.
296
297 // This name is longer than the name we are checking it against.
298 if (components_.size() > name.components_.size())
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700299 return false;
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700300
301 // Check if at least one of given components doesn't match.
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700302 for (size_t i = 0; i < components_.size(); ++i) {
303 if (*components_[i].getValue() != *name.components_[i].getValue())
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700304 return false;
305 }
306
307 return true;
308}
309
Jeff Thompson0050abe2013-09-17 12:50:25 -0700310void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700311Name::toEscapedString(const vector<uint8_t>& value, ostringstream& result)
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700312{
313 bool gotNonDot = false;
314 for (unsigned i = 0; i < value.size(); ++i) {
315 if (value[i] != 0x2e) {
316 gotNonDot = true;
317 break;
318 }
319 }
320 if (!gotNonDot) {
321 // Special case for component of zero or more periods. Add 3 periods.
322 result << "...";
Jeff Thompson97223af2013-09-24 17:01:27 -0700323 for (size_t i = 0; i < value.size(); ++i)
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700324 result << '.';
325 }
326 else {
327 // In case we need to escape, set to upper case hex and save the previous flags.
328 ios::fmtflags saveFlags = result.flags(ios::hex | ios::uppercase);
329
Jeff Thompson97223af2013-09-24 17:01:27 -0700330 for (size_t i = 0; i < value.size(); ++i) {
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700331 uint8_t x = value[i];
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700332 // Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
333 if (x >= 0x30 && x <= 0x39 || x >= 0x41 && x <= 0x5a ||
334 x >= 0x61 && x <= 0x7a || x == 0x2b || x == 0x2d ||
335 x == 0x2e || x == 0x5f)
336 result << x;
337 else {
338 result << '%';
339 if (x < 16)
340 result << '0';
341 result << (unsigned int)x;
342 }
343 }
344
345 // Restore.
346 result.flags(saveFlags);
347 }
348}
349
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700350string
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700351Name::toEscapedString(const vector<uint8_t>& value)
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700352{
353 ostringstream result;
354 toEscapedString(value, result);
355 return result.str();
356}
357
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700358}