blob: 0b8ab912e49c256966a32c1000fb2cc26963cec5 [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 Thompson1656e6a2013-08-29 18:01:48 -070021static inline void trimLeft(string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070022{
23 size_t found = str.find_first_not_of(WHITESPACE_CHARS);
24 if (found != string::npos) {
25 if (found > 0)
26 str.erase(0, found);
27 }
28 else
29 // All whitespace
30 str.clear();
31}
32
33/**
34 * Modify str in place to erase whitespace on the right.
35 * @param str
36 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070037static inline void trimRight(string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070038{
39 size_t found = str.find_last_not_of(WHITESPACE_CHARS);
40 if (found != string::npos) {
41 if (found + 1 < str.size())
42 str.erase(found + 1);
43 }
44 else
45 // All whitespace
46 str.clear();
47}
48
49/**
50 * Modify str in place to erase whitespace on the left and right.
51 * @param str
52 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070053static void trim(string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070054{
55 trimLeft(str);
56 trimRight(str);
57}
Jeff Thompson443398d2013-07-02 19:45:46 -070058
Jeff Thompson26c63d62013-07-02 18:00:26 -070059/**
60 * Convert the hex character to an integer from 0 to 15, or -1 if not a hex character.
61 * @param c
62 * @return
63 */
64static int fromHexChar(unsigned char c)
65{
66 if (c >= '0' && c <= '9')
67 return (int)c - (int)'0';
68 else if (c >= 'A' && c <= 'F')
69 return (int)c - (int)'A' + 10;
70 else if (c >= 'a' && c <= 'f')
71 return (int)c - (int)'a' + 10;
72 else
73 return -1;
74}
75
76/**
77 * Return a copy of str, converting each escaped "%XX" to the char value.
78 * @param str
79 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070080static string unescape(const string& str)
Jeff Thompson26c63d62013-07-02 18:00:26 -070081{
82 ostringstream result;
83
84 for (unsigned int i = 0; i < str.size(); ++i) {
85 if (str[i] == '%' && i + 2 < str.size()) {
86 int hi = fromHexChar(str[i + 1]);
87 int lo = fromHexChar(str[i + 2]);
88
89 if (hi < 0 || lo < 0)
90 // Invalid hex characters, so just keep the escaped string.
91 result << str[i] << str[i + 1] << str[i + 2];
92 else
93 result << (unsigned char)(16 * hi + lo);
94
95 // Skip ahead past the escaped value.
96 i += 2;
97 }
98 else
99 // Just copy through.
100 result << str[i];
101 }
102
103 return result.str();
104}
105
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700106bool Name::Component::setFromEscapedString(const char *first, const char *last)
Jeff Thompson443398d2013-07-02 19:45:46 -0700107{
108 string trimmedString(first, last);
109 trim(trimmedString);
110 string component = unescape(trimmedString);
111
112 if (component.find_first_not_of(".") == string::npos) {
113 // Special case for component of only periods.
114 if (component.size() <= 2)
115 // Zero, one or two periods is illegal. Ignore this component.
116 return false;
Jeff Thompson995aba52013-09-12 12:04:52 -0700117 else
Jeff Thompson443398d2013-07-02 19:45:46 -0700118 // Remove 3 periods.
Jeff Thompson995aba52013-09-12 12:04:52 -0700119 value_ = Blob((const unsigned char *)&component[3], component.size() - 3);
Jeff Thompson443398d2013-07-02 19:45:46 -0700120 }
Jeff Thompson995aba52013-09-12 12:04:52 -0700121 else
122 value_ = Blob((const unsigned char *)&component[0], component.size());
Jeff Thompson443398d2013-07-02 19:45:46 -0700123
124 return true;
125}
126
Jeff Thompson8aac1992013-08-12 17:26:02 -0700127void Name::Component::setSegment(unsigned long segment)
128{
Jeff Thompson995aba52013-09-12 12:04:52 -0700129 ptr_lib::shared_ptr<vector<unsigned char> > value;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700130
Jeff Thompsonb8f1b132013-08-13 11:07:43 -0700131 // Add the leading zero.
Jeff Thompson995aba52013-09-12 12:04:52 -0700132 value->push_back(0);
Jeff Thompsonb8f1b132013-08-13 11:07:43 -0700133
Jeff Thompson8aac1992013-08-12 17:26:02 -0700134 // First encode in little endian.
135 while (segment != 0) {
Jeff Thompson995aba52013-09-12 12:04:52 -0700136 value->push_back(segment & 0xff);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700137 segment >>= 8;
138 }
139
Jeff Thompson8aac1992013-08-12 17:26:02 -0700140 // Make it big endian.
Jeff Thompson995aba52013-09-12 12:04:52 -0700141 reverse(value->begin() + 1, value->end());
142 value_ = value;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700143}
144
Jeff Thompson67515bd2013-08-15 17:43:22 -0700145void Name::set(const char *uri_cstr)
Jeff Thompson443398d2013-07-02 19:45:46 -0700146{
Jeff Thompson67515bd2013-08-15 17:43:22 -0700147 components_.clear();
148
Jeff Thompson443398d2013-07-02 19:45:46 -0700149 string uri = uri_cstr;
150 trim(uri);
151 if (uri.size() == 0)
152 return;
153
154 size_t iColon = uri.find(':');
155 if (iColon != string::npos) {
156 // Make sure the colon came before a '/'.
157 size_t iFirstSlash = uri.find('/');
158 if (iFirstSlash == string::npos || iColon < iFirstSlash) {
159 // Omit the leading protocol such as ndn:
160 uri.erase(0, iColon + 1);
161 trim(uri);
162 }
163 }
164
165 // Trim the leading slash and possibly the authority.
166 if (uri[0] == '/') {
167 if (uri.size() >= 2 && uri[1] == '/') {
168 // Strip the authority following "//".
169 size_t iAfterAuthority = uri.find('/', 2);
170 if (iAfterAuthority == string::npos)
171 // Unusual case: there was only an authority.
172 return;
173 else {
174 uri.erase(0, iAfterAuthority + 1);
175 trim(uri);
176 }
177 }
178 else {
179 uri.erase(0, 1);
180 trim(uri);
181 }
182 }
183
184 size_t iComponentStart = 0;
185
186 // Unescape the components.
187 while (iComponentStart < uri.size()) {
188 size_t iComponentEnd = uri.find("/", iComponentStart);
189 if (iComponentEnd == string::npos)
190 iComponentEnd = uri.size();
191
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700192 components_.push_back(Component());
Jeff Thompson443398d2013-07-02 19:45:46 -0700193 if (!components_[components_.size() - 1].setFromEscapedString(&uri[iComponentStart], &uri[iComponentEnd]))
194 // Ignore the illegal component. This also gets rid of a trailing '/'.
195 components_.pop_back();
196
197 iComponentStart = iComponentEnd + 1;
198 }
199}
200
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700201void Name::get(struct ndn_Name& nameStruct) const
Jeff Thompson48815112013-06-28 18:22:48 -0700202{
Jeff Thompson016ed642013-07-02 14:39:06 -0700203 if (nameStruct.maxComponents < components_.size())
204 throw runtime_error("nameStruct.maxComponents must be >= this name getNComponents()");
205
206 nameStruct.nComponents = components_.size();
207 for (unsigned int i = 0; i < nameStruct.nComponents; ++i)
208 components_[i].get(nameStruct.components[i]);
Jeff Thompson48815112013-06-28 18:22:48 -0700209}
210
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700211void Name::set(const struct ndn_Name& nameStruct)
Jeff Thompsonb468c312013-07-01 17:50:14 -0700212{
213 clear();
Jeff Thompsonccb13c12013-07-01 18:16:00 -0700214 for (unsigned int i = 0; i < nameStruct.nComponents; ++i)
Jeff Thompsonb468c312013-07-01 17:50:14 -0700215 addComponent(nameStruct.components[i].value, nameStruct.components[i].valueLength);
216}
217
Jeff Thompson21844fc2013-08-08 14:52:51 -0700218std::string Name::toUri() const
Jeff Thompsone6063512013-07-01 15:11:28 -0700219{
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700220 if (components_.size() == 0)
221 return "/";
222
223 ostringstream result;
Jeff Thompsonccb13c12013-07-01 18:16:00 -0700224 for (unsigned int i = 0; i < components_.size(); ++i) {
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700225 result << "/";
Jeff Thompson9bdb3b22013-09-12 12:42:13 -0700226 toEscapedString(*components_[i].getValue(), result);
Jeff Thompsone6063512013-07-01 15:11:28 -0700227 }
228
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700229 return result.str();
Jeff Thompsone6063512013-07-01 15:11:28 -0700230}
231
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700232bool Name::match(const Name& name) const
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700233{
234 // Imitate ndn_Name_match.
235
236 // This name is longer than the name we are checking it against.
237 if (components_.size() > name.components_.size())
238 return 0;
239
240 // Check if at least one of given components doesn't match.
241 unsigned int i;
242 for (i = 0; i < components_.size(); ++i) {
243 const Component &selfComponent = components_[i];
244 const Component &nameComponent = name.components_[i];
245
246 if (selfComponent.getValue() != nameComponent.getValue())
247 return false;
248 }
249
250 return true;
251}
252
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700253void Name::toEscapedString(const vector<unsigned char>& value, ostringstream& result)
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700254{
255 bool gotNonDot = false;
256 for (unsigned i = 0; i < value.size(); ++i) {
257 if (value[i] != 0x2e) {
258 gotNonDot = true;
259 break;
260 }
261 }
262 if (!gotNonDot) {
263 // Special case for component of zero or more periods. Add 3 periods.
264 result << "...";
265 for (unsigned int i = 0; i < value.size(); ++i)
266 result << '.';
267 }
268 else {
269 // In case we need to escape, set to upper case hex and save the previous flags.
270 ios::fmtflags saveFlags = result.flags(ios::hex | ios::uppercase);
271
272 for (unsigned int i = 0; i < value.size(); ++i) {
273 unsigned char x = value[i];
274 // Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
275 if (x >= 0x30 && x <= 0x39 || x >= 0x41 && x <= 0x5a ||
276 x >= 0x61 && x <= 0x7a || x == 0x2b || x == 0x2d ||
277 x == 0x2e || x == 0x5f)
278 result << x;
279 else {
280 result << '%';
281 if (x < 16)
282 result << '0';
283 result << (unsigned int)x;
284 }
285 }
286
287 // Restore.
288 result.flags(saveFlags);
289 }
290}
291
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700292}