blob: 1496219a7da45eace2e68659bf443ea802d4b689 [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;
12
13namespace ndn {
14
Jeff Thompson26c63d62013-07-02 18:00:26 -070015static 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 Thompson0050abe2013-09-17 12:50:25 -070021static inline void
22trimLeft(string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070023{
24 size_t found = str.find_first_not_of(WHITESPACE_CHARS);
25 if (found != string::npos) {
26 if (found > 0)
27 str.erase(0, found);
28 }
29 else
30 // All whitespace
31 str.clear();
32}
33
34/**
35 * Modify str in place to erase whitespace on the right.
36 * @param str
37 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070038static inline void
39trimRight(string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070040{
41 size_t found = str.find_last_not_of(WHITESPACE_CHARS);
42 if (found != string::npos) {
43 if (found + 1 < str.size())
44 str.erase(found + 1);
45 }
46 else
47 // All whitespace
48 str.clear();
49}
50
51/**
52 * Modify str in place to erase whitespace on the left and right.
53 * @param str
54 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070055static void
56trim(string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070057{
58 trimLeft(str);
59 trimRight(str);
60}
Jeff Thompson443398d2013-07-02 19:45:46 -070061
Jeff Thompson26c63d62013-07-02 18:00:26 -070062/**
63 * Convert the hex character to an integer from 0 to 15, or -1 if not a hex character.
64 * @param c
65 * @return
66 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070067static int
68fromHexChar(unsigned char c)
Jeff Thompson26c63d62013-07-02 18:00:26 -070069{
70 if (c >= '0' && c <= '9')
71 return (int)c - (int)'0';
72 else if (c >= 'A' && c <= 'F')
73 return (int)c - (int)'A' + 10;
74 else if (c >= 'a' && c <= 'f')
75 return (int)c - (int)'a' + 10;
76 else
77 return -1;
78}
79
80/**
81 * Return a copy of str, converting each escaped "%XX" to the char value.
82 * @param str
83 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070084static string
85unescape(const string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070086{
87 ostringstream result;
88
89 for (unsigned int i = 0; i < str.size(); ++i) {
90 if (str[i] == '%' && i + 2 < str.size()) {
91 int hi = fromHexChar(str[i + 1]);
92 int lo = fromHexChar(str[i + 2]);
93
94 if (hi < 0 || lo < 0)
95 // Invalid hex characters, so just keep the escaped string.
96 result << str[i] << str[i + 1] << str[i + 2];
97 else
98 result << (unsigned char)(16 * hi + lo);
99
100 // Skip ahead past the escaped value.
101 i += 2;
102 }
103 else
104 // Just copy through.
105 result << str[i];
106 }
107
108 return result.str();
109}
110
Jeff Thompson0050abe2013-09-17 12:50:25 -0700111Blob
112Name::Component::makeFromEscapedString(const char *escapedString, unsigned int beginOffset, unsigned int endOffset)
Jeff Thompson443398d2013-07-02 19:45:46 -0700113{
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700114 string trimmedString(escapedString + beginOffset, escapedString + endOffset);
Jeff Thompson443398d2013-07-02 19:45:46 -0700115 trim(trimmedString);
116 string component = unescape(trimmedString);
117
118 if (component.find_first_not_of(".") == string::npos) {
119 // Special case for component of only periods.
120 if (component.size() <= 2)
121 // Zero, one or two periods is illegal. Ignore this component.
Jeff Thompson46411c92013-09-13 19:31:25 -0700122 return Blob();
Jeff Thompson995aba52013-09-12 12:04:52 -0700123 else
Jeff Thompson443398d2013-07-02 19:45:46 -0700124 // Remove 3 periods.
Jeff Thompson46411c92013-09-13 19:31:25 -0700125 return Blob((const unsigned char *)&component[3], component.size() - 3);
Jeff Thompson443398d2013-07-02 19:45:46 -0700126 }
Jeff Thompson995aba52013-09-12 12:04:52 -0700127 else
Jeff Thompson46411c92013-09-13 19:31:25 -0700128 return Blob((const unsigned char *)&component[0], component.size());
Jeff Thompson443398d2013-07-02 19:45:46 -0700129}
130
Jeff Thompson0050abe2013-09-17 12:50:25 -0700131Blob
132Name::Component::makeSegment(unsigned long segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700133{
Jeff Thompson995aba52013-09-12 12:04:52 -0700134 ptr_lib::shared_ptr<vector<unsigned char> > value;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700135
Jeff Thompsonb8f1b132013-08-13 11:07:43 -0700136 // Add the leading zero.
Jeff Thompson995aba52013-09-12 12:04:52 -0700137 value->push_back(0);
Jeff Thompsonb8f1b132013-08-13 11:07:43 -0700138
Jeff Thompson8aac1992013-08-12 17:26:02 -0700139 // First encode in little endian.
140 while (segment != 0) {
Jeff Thompson995aba52013-09-12 12:04:52 -0700141 value->push_back(segment & 0xff);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700142 segment >>= 8;
143 }
144
Jeff Thompson8aac1992013-08-12 17:26:02 -0700145 // Make it big endian.
Jeff Thompson995aba52013-09-12 12:04:52 -0700146 reverse(value->begin() + 1, value->end());
Jeff Thompson46411c92013-09-13 19:31:25 -0700147 return Blob(value);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700148}
149
Jeff Thompson0050abe2013-09-17 12:50:25 -0700150void
151Name::set(const char *uri_cstr)
Jeff Thompson443398d2013-07-02 19:45:46 -0700152{
Jeff Thompson67515bd2013-08-15 17:43:22 -0700153 components_.clear();
154
Jeff Thompson443398d2013-07-02 19:45:46 -0700155 string uri = uri_cstr;
156 trim(uri);
157 if (uri.size() == 0)
158 return;
159
160 size_t iColon = uri.find(':');
161 if (iColon != string::npos) {
162 // Make sure the colon came before a '/'.
163 size_t iFirstSlash = uri.find('/');
164 if (iFirstSlash == string::npos || iColon < iFirstSlash) {
165 // Omit the leading protocol such as ndn:
166 uri.erase(0, iColon + 1);
167 trim(uri);
168 }
169 }
170
171 // Trim the leading slash and possibly the authority.
172 if (uri[0] == '/') {
173 if (uri.size() >= 2 && uri[1] == '/') {
174 // Strip the authority following "//".
175 size_t iAfterAuthority = uri.find('/', 2);
176 if (iAfterAuthority == string::npos)
177 // Unusual case: there was only an authority.
178 return;
179 else {
180 uri.erase(0, iAfterAuthority + 1);
181 trim(uri);
182 }
183 }
184 else {
185 uri.erase(0, 1);
186 trim(uri);
187 }
188 }
189
190 size_t iComponentStart = 0;
191
192 // Unescape the components.
193 while (iComponentStart < uri.size()) {
194 size_t iComponentEnd = uri.find("/", iComponentStart);
195 if (iComponentEnd == string::npos)
196 iComponentEnd = uri.size();
197
Jeff Thompson46411c92013-09-13 19:31:25 -0700198 Blob component = Component::makeFromEscapedString(&uri[0], iComponentStart, iComponentEnd);
199 // Ignore illegal components. This also gets rid of a trailing '/'.
200 if (component)
201 components_.push_back(Component(component));
Jeff Thompson443398d2013-07-02 19:45:46 -0700202
203 iComponentStart = iComponentEnd + 1;
204 }
205}
206
Jeff Thompson0050abe2013-09-17 12:50:25 -0700207void
208Name::get(struct ndn_Name& nameStruct) const
Jeff Thompson48815112013-06-28 18:22:48 -0700209{
Jeff Thompson016ed642013-07-02 14:39:06 -0700210 if (nameStruct.maxComponents < components_.size())
211 throw runtime_error("nameStruct.maxComponents must be >= this name getNComponents()");
212
213 nameStruct.nComponents = components_.size();
214 for (unsigned int i = 0; i < nameStruct.nComponents; ++i)
215 components_[i].get(nameStruct.components[i]);
Jeff Thompson48815112013-06-28 18:22:48 -0700216}
217
Jeff Thompson0050abe2013-09-17 12:50:25 -0700218void
219Name::set(const struct ndn_Name& nameStruct)
Jeff Thompsonb468c312013-07-01 17:50:14 -0700220{
221 clear();
Jeff Thompsonccb13c12013-07-01 18:16:00 -0700222 for (unsigned int i = 0; i < nameStruct.nComponents; ++i)
Jeff Thompsonb468c312013-07-01 17:50:14 -0700223 addComponent(nameStruct.components[i].value, nameStruct.components[i].valueLength);
224}
225
Jeff Thompson0050abe2013-09-17 12:50:25 -0700226std::string
227Name::toUri() const
Jeff Thompsone6063512013-07-01 15:11:28 -0700228{
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700229 if (components_.size() == 0)
230 return "/";
231
232 ostringstream result;
Jeff Thompsonccb13c12013-07-01 18:16:00 -0700233 for (unsigned int i = 0; i < components_.size(); ++i) {
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700234 result << "/";
Jeff Thompson9bdb3b22013-09-12 12:42:13 -0700235 toEscapedString(*components_[i].getValue(), result);
Jeff Thompsone6063512013-07-01 15:11:28 -0700236 }
237
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700238 return result.str();
Jeff Thompsone6063512013-07-01 15:11:28 -0700239}
240
Jeff Thompson0050abe2013-09-17 12:50:25 -0700241bool
242Name::match(const Name& name) const
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700243{
244 // Imitate ndn_Name_match.
245
246 // This name is longer than the name we are checking it against.
247 if (components_.size() > name.components_.size())
248 return 0;
249
250 // Check if at least one of given components doesn't match.
251 unsigned int i;
252 for (i = 0; i < components_.size(); ++i) {
253 const Component &selfComponent = components_[i];
254 const Component &nameComponent = name.components_[i];
255
256 if (selfComponent.getValue() != nameComponent.getValue())
257 return false;
258 }
259
260 return true;
261}
262
Jeff Thompson0050abe2013-09-17 12:50:25 -0700263void
264Name::toEscapedString(const vector<unsigned char>& value, ostringstream& result)
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700265{
266 bool gotNonDot = false;
267 for (unsigned i = 0; i < value.size(); ++i) {
268 if (value[i] != 0x2e) {
269 gotNonDot = true;
270 break;
271 }
272 }
273 if (!gotNonDot) {
274 // Special case for component of zero or more periods. Add 3 periods.
275 result << "...";
276 for (unsigned int i = 0; i < value.size(); ++i)
277 result << '.';
278 }
279 else {
280 // In case we need to escape, set to upper case hex and save the previous flags.
281 ios::fmtflags saveFlags = result.flags(ios::hex | ios::uppercase);
282
283 for (unsigned int i = 0; i < value.size(); ++i) {
284 unsigned char x = value[i];
285 // Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
286 if (x >= 0x30 && x <= 0x39 || x >= 0x41 && x <= 0x5a ||
287 x >= 0x61 && x <= 0x7a || x == 0x2b || x == 0x2d ||
288 x == 0x2e || x == 0x5f)
289 result << x;
290 else {
291 result << '%';
292 if (x < 16)
293 result << '0';
294 result << (unsigned int)x;
295 }
296 }
297
298 // Restore.
299 result.flags(saveFlags);
300 }
301}
302
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700303}