blob: 2b5c6a9243420e8f5b3ad868ec0a9ae2c0a93678 [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 Thompson46411c92013-09-13 19:31:25 -0700106Blob Name::Component::makeFromEscapedString(const char *escapedString, unsigned int beginOffset, unsigned int endOffset)
Jeff Thompson443398d2013-07-02 19:45:46 -0700107{
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700108 string trimmedString(escapedString + beginOffset, escapedString + endOffset);
Jeff Thompson443398d2013-07-02 19:45:46 -0700109 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.
Jeff Thompson46411c92013-09-13 19:31:25 -0700116 return Blob();
Jeff Thompson995aba52013-09-12 12:04:52 -0700117 else
Jeff Thompson443398d2013-07-02 19:45:46 -0700118 // Remove 3 periods.
Jeff Thompson46411c92013-09-13 19:31:25 -0700119 return 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
Jeff Thompson46411c92013-09-13 19:31:25 -0700122 return Blob((const unsigned char *)&component[0], component.size());
Jeff Thompson443398d2013-07-02 19:45:46 -0700123}
124
Jeff Thompson46411c92013-09-13 19:31:25 -0700125Blob Name::Component::makeSegment(unsigned long segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700126{
Jeff Thompson995aba52013-09-12 12:04:52 -0700127 ptr_lib::shared_ptr<vector<unsigned char> > value;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700128
Jeff Thompsonb8f1b132013-08-13 11:07:43 -0700129 // Add the leading zero.
Jeff Thompson995aba52013-09-12 12:04:52 -0700130 value->push_back(0);
Jeff Thompsonb8f1b132013-08-13 11:07:43 -0700131
Jeff Thompson8aac1992013-08-12 17:26:02 -0700132 // First encode in little endian.
133 while (segment != 0) {
Jeff Thompson995aba52013-09-12 12:04:52 -0700134 value->push_back(segment & 0xff);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700135 segment >>= 8;
136 }
137
Jeff Thompson8aac1992013-08-12 17:26:02 -0700138 // Make it big endian.
Jeff Thompson995aba52013-09-12 12:04:52 -0700139 reverse(value->begin() + 1, value->end());
Jeff Thompson46411c92013-09-13 19:31:25 -0700140 return Blob(value);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700141}
142
Jeff Thompson67515bd2013-08-15 17:43:22 -0700143void Name::set(const char *uri_cstr)
Jeff Thompson443398d2013-07-02 19:45:46 -0700144{
Jeff Thompson67515bd2013-08-15 17:43:22 -0700145 components_.clear();
146
Jeff Thompson443398d2013-07-02 19:45:46 -0700147 string uri = uri_cstr;
148 trim(uri);
149 if (uri.size() == 0)
150 return;
151
152 size_t iColon = uri.find(':');
153 if (iColon != string::npos) {
154 // Make sure the colon came before a '/'.
155 size_t iFirstSlash = uri.find('/');
156 if (iFirstSlash == string::npos || iColon < iFirstSlash) {
157 // Omit the leading protocol such as ndn:
158 uri.erase(0, iColon + 1);
159 trim(uri);
160 }
161 }
162
163 // Trim the leading slash and possibly the authority.
164 if (uri[0] == '/') {
165 if (uri.size() >= 2 && uri[1] == '/') {
166 // Strip the authority following "//".
167 size_t iAfterAuthority = uri.find('/', 2);
168 if (iAfterAuthority == string::npos)
169 // Unusual case: there was only an authority.
170 return;
171 else {
172 uri.erase(0, iAfterAuthority + 1);
173 trim(uri);
174 }
175 }
176 else {
177 uri.erase(0, 1);
178 trim(uri);
179 }
180 }
181
182 size_t iComponentStart = 0;
183
184 // Unescape the components.
185 while (iComponentStart < uri.size()) {
186 size_t iComponentEnd = uri.find("/", iComponentStart);
187 if (iComponentEnd == string::npos)
188 iComponentEnd = uri.size();
189
Jeff Thompson46411c92013-09-13 19:31:25 -0700190 Blob component = Component::makeFromEscapedString(&uri[0], iComponentStart, iComponentEnd);
191 // Ignore illegal components. This also gets rid of a trailing '/'.
192 if (component)
193 components_.push_back(Component(component));
Jeff Thompson443398d2013-07-02 19:45:46 -0700194
195 iComponentStart = iComponentEnd + 1;
196 }
197}
198
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700199void Name::get(struct ndn_Name& nameStruct) const
Jeff Thompson48815112013-06-28 18:22:48 -0700200{
Jeff Thompson016ed642013-07-02 14:39:06 -0700201 if (nameStruct.maxComponents < components_.size())
202 throw runtime_error("nameStruct.maxComponents must be >= this name getNComponents()");
203
204 nameStruct.nComponents = components_.size();
205 for (unsigned int i = 0; i < nameStruct.nComponents; ++i)
206 components_[i].get(nameStruct.components[i]);
Jeff Thompson48815112013-06-28 18:22:48 -0700207}
208
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700209void Name::set(const struct ndn_Name& nameStruct)
Jeff Thompsonb468c312013-07-01 17:50:14 -0700210{
211 clear();
Jeff Thompsonccb13c12013-07-01 18:16:00 -0700212 for (unsigned int i = 0; i < nameStruct.nComponents; ++i)
Jeff Thompsonb468c312013-07-01 17:50:14 -0700213 addComponent(nameStruct.components[i].value, nameStruct.components[i].valueLength);
214}
215
Jeff Thompson21844fc2013-08-08 14:52:51 -0700216std::string Name::toUri() const
Jeff Thompsone6063512013-07-01 15:11:28 -0700217{
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700218 if (components_.size() == 0)
219 return "/";
220
221 ostringstream result;
Jeff Thompsonccb13c12013-07-01 18:16:00 -0700222 for (unsigned int i = 0; i < components_.size(); ++i) {
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700223 result << "/";
Jeff Thompson9bdb3b22013-09-12 12:42:13 -0700224 toEscapedString(*components_[i].getValue(), result);
Jeff Thompsone6063512013-07-01 15:11:28 -0700225 }
226
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700227 return result.str();
Jeff Thompsone6063512013-07-01 15:11:28 -0700228}
229
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700230bool Name::match(const Name& name) const
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700231{
232 // Imitate ndn_Name_match.
233
234 // This name is longer than the name we are checking it against.
235 if (components_.size() > name.components_.size())
236 return 0;
237
238 // Check if at least one of given components doesn't match.
239 unsigned int i;
240 for (i = 0; i < components_.size(); ++i) {
241 const Component &selfComponent = components_[i];
242 const Component &nameComponent = name.components_[i];
243
244 if (selfComponent.getValue() != nameComponent.getValue())
245 return false;
246 }
247
248 return true;
249}
250
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700251void Name::toEscapedString(const vector<unsigned char>& value, ostringstream& result)
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700252{
253 bool gotNonDot = false;
254 for (unsigned i = 0; i < value.size(); ++i) {
255 if (value[i] != 0x2e) {
256 gotNonDot = true;
257 break;
258 }
259 }
260 if (!gotNonDot) {
261 // Special case for component of zero or more periods. Add 3 periods.
262 result << "...";
263 for (unsigned int i = 0; i < value.size(); ++i)
264 result << '.';
265 }
266 else {
267 // In case we need to escape, set to upper case hex and save the previous flags.
268 ios::fmtflags saveFlags = result.flags(ios::hex | ios::uppercase);
269
270 for (unsigned int i = 0; i < value.size(); ++i) {
271 unsigned char x = value[i];
272 // Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
273 if (x >= 0x30 && x <= 0x39 || x >= 0x41 && x <= 0x5a ||
274 x >= 0x61 && x <= 0x7a || x == 0x2b || x == 0x2d ||
275 x == 0x2e || x == 0x5f)
276 result << x;
277 else {
278 result << '%';
279 if (x < 16)
280 result << '0';
281 result << (unsigned int)x;
282 }
283 }
284
285 // Restore.
286 result.flags(saveFlags);
287 }
288}
289
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700290}