blob: dedbfd130eba6b1cc4b603b8daa707cd48f4e5fa [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07004 */
5
Jeff Thompson54909772013-07-07 22:38:57 -07006#include <stdexcept>
Jeff Thompsonb8f1b132013-08-13 11:07:43 -07007#include <algorithm>
Jeff Thompson53412192013-08-06 13:35:50 -07008#include "name.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07009
10using namespace std;
11
12namespace ndn {
13
Jeff Thompson26c63d62013-07-02 18:00:26 -070014static 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 Thompson1656e6a2013-08-29 18:01:48 -070020static inline void trimLeft(string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070021{
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 Thompson1656e6a2013-08-29 18:01:48 -070036static inline void trimRight(string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070037{
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 Thompson1656e6a2013-08-29 18:01:48 -070052static void trim(string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070053{
54 trimLeft(str);
55 trimRight(str);
56}
Jeff Thompson443398d2013-07-02 19:45:46 -070057
Jeff Thompson26c63d62013-07-02 18:00:26 -070058/**
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 */
63static 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 Thompson1656e6a2013-08-29 18:01:48 -070079static string unescape(const string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070080{
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 Thompson5a6b5ab2013-08-05 15:43:47 -0700105bool Name::Component::setFromEscapedString(const char *first, const char *last)
Jeff Thompson443398d2013-07-02 19:45:46 -0700106{
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 Thompson995aba52013-09-12 12:04:52 -0700116 else
Jeff Thompson443398d2013-07-02 19:45:46 -0700117 // Remove 3 periods.
Jeff Thompson995aba52013-09-12 12:04:52 -0700118 value_ = Blob((const unsigned char *)&component[3], component.size() - 3);
Jeff Thompson443398d2013-07-02 19:45:46 -0700119 }
Jeff Thompson995aba52013-09-12 12:04:52 -0700120 else
121 value_ = Blob((const unsigned char *)&component[0], component.size());
Jeff Thompson443398d2013-07-02 19:45:46 -0700122
123 return true;
124}
125
Jeff Thompson8aac1992013-08-12 17:26:02 -0700126void Name::Component::setSegment(unsigned long segment)
127{
Jeff Thompson995aba52013-09-12 12:04:52 -0700128 ptr_lib::shared_ptr<vector<unsigned char> > value;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700129
Jeff Thompsonb8f1b132013-08-13 11:07:43 -0700130 // Add the leading zero.
Jeff Thompson995aba52013-09-12 12:04:52 -0700131 value->push_back(0);
Jeff Thompsonb8f1b132013-08-13 11:07:43 -0700132
Jeff Thompson8aac1992013-08-12 17:26:02 -0700133 // First encode in little endian.
134 while (segment != 0) {
Jeff Thompson995aba52013-09-12 12:04:52 -0700135 value->push_back(segment & 0xff);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700136 segment >>= 8;
137 }
138
Jeff Thompson8aac1992013-08-12 17:26:02 -0700139 // Make it big endian.
Jeff Thompson995aba52013-09-12 12:04:52 -0700140 reverse(value->begin() + 1, value->end());
141 value_ = value;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700142}
143
Jeff Thompson67515bd2013-08-15 17:43:22 -0700144void Name::set(const char *uri_cstr)
Jeff Thompson443398d2013-07-02 19:45:46 -0700145{
Jeff Thompson67515bd2013-08-15 17:43:22 -0700146 components_.clear();
147
Jeff Thompson443398d2013-07-02 19:45:46 -0700148 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 Thompson5a6b5ab2013-08-05 15:43:47 -0700191 components_.push_back(Component());
Jeff Thompson443398d2013-07-02 19:45:46 -0700192 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 Thompson1656e6a2013-08-29 18:01:48 -0700200void Name::get(struct ndn_Name& nameStruct) const
Jeff Thompson48815112013-06-28 18:22:48 -0700201{
Jeff Thompson016ed642013-07-02 14:39:06 -0700202 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 Thompson48815112013-06-28 18:22:48 -0700208}
209
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700210void Name::set(const struct ndn_Name& nameStruct)
Jeff Thompsonb468c312013-07-01 17:50:14 -0700211{
212 clear();
Jeff Thompsonccb13c12013-07-01 18:16:00 -0700213 for (unsigned int i = 0; i < nameStruct.nComponents; ++i)
Jeff Thompsonb468c312013-07-01 17:50:14 -0700214 addComponent(nameStruct.components[i].value, nameStruct.components[i].valueLength);
215}
216
Jeff Thompson21844fc2013-08-08 14:52:51 -0700217std::string Name::toUri() const
Jeff Thompsone6063512013-07-01 15:11:28 -0700218{
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700219 if (components_.size() == 0)
220 return "/";
221
222 ostringstream result;
Jeff Thompsonccb13c12013-07-01 18:16:00 -0700223 for (unsigned int i = 0; i < components_.size(); ++i) {
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700224 result << "/";
225 toEscapedString(components_[i].getValue(), result);
Jeff Thompsone6063512013-07-01 15:11:28 -0700226 }
227
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700228 return result.str();
Jeff Thompsone6063512013-07-01 15:11:28 -0700229}
230
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700231bool Name::match(const Name& name) const
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700232{
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 Thompson1656e6a2013-08-29 18:01:48 -0700252void Name::toEscapedString(const vector<unsigned char>& value, ostringstream& result)
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700253{
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 Thompson9c41dfe2013-06-27 12:10:25 -0700291}