blob: fbb26754c6ddaf717ddb400d89c7b9b23e8fd97e [file] [log] [blame]
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07001/*
2 * Author: Jeff Thompson
3 *
4 * BSD license, See the LICENSE file for more information.
5 */
6
Jeff Thompsone6063512013-07-01 15:11:28 -07007#include <sstream>
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07008#include "Name.hpp"
9
10using namespace std;
11
12namespace ndn {
13
Jeff Thompson4b2479a2013-07-02 15:37:39 -070014/**
15 * Write the value to result, escaping characters according to the NDN URI Scheme.
16 * This also adds "..." to a value with zero or more ".".
17 * @param value the buffer with the value to escape
18 * @param result the string stream to write to.
19 */
20static void toEscapedString(const vector<unsigned char> &value, ostringstream &result)
21{
22 bool gotNonDot = false;
23 for (unsigned i = 0; i < value.size(); ++i) {
24 if (value[i] != 0x2e) {
25 gotNonDot = true;
26 break;
27 }
28 }
29 if (!gotNonDot) {
30 // Special case for component of zero or more periods. Add 3 periods.
31 result << "...";
32 for (unsigned int i = 0; i < value.size(); ++i)
33 result << ".";
34 }
35 else {
36 // In case we need to escape, set to upper case hex and save the previous flags.
37 ios::fmtflags saveFlags = result.flags(ios::hex | ios::uppercase);
38
39 for (unsigned int i = 0; i < value.size(); ++i) {
40 unsigned char x = value[i];
41 // Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
42 if (x >= 0x30 && x <= 0x39 || x >= 0x41 && x <= 0x5a ||
43 x >= 0x61 && x <= 0x7a || x == 0x2b || x == 0x2d ||
44 x == 0x2e || x == 0x5f)
45 result << x;
46 else {
47 result << "%";
48 if (x < 16)
49 result << "0";
50 result << (unsigned int)x;
51 }
52 }
53
54 // Restore.
55 result.flags(saveFlags);
56 }
57}
Jeff Thompson26c63d62013-07-02 18:00:26 -070058
59static const char *WHITESPACE_CHARS = " \n\r\t";
60
61/**
62 * Modify str in place to erase whitespace on the left.
63 * @param str
64 */
65static inline void trimLeft(string &str)
66{
67 size_t found = str.find_first_not_of(WHITESPACE_CHARS);
68 if (found != string::npos) {
69 if (found > 0)
70 str.erase(0, found);
71 }
72 else
73 // All whitespace
74 str.clear();
75}
76
77/**
78 * Modify str in place to erase whitespace on the right.
79 * @param str
80 */
81static inline void trimRight(string &str)
82{
83 size_t found = str.find_last_not_of(WHITESPACE_CHARS);
84 if (found != string::npos) {
85 if (found + 1 < str.size())
86 str.erase(found + 1);
87 }
88 else
89 // All whitespace
90 str.clear();
91}
92
93/**
94 * Modify str in place to erase whitespace on the left and right.
95 * @param str
96 */
97static void trim(string &str)
98{
99 trimLeft(str);
100 trimRight(str);
101}
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700102
Jeff Thompson26c63d62013-07-02 18:00:26 -0700103/**
104 * Convert the hex character to an integer from 0 to 15, or -1 if not a hex character.
105 * @param c
106 * @return
107 */
108static int fromHexChar(unsigned char c)
109{
110 if (c >= '0' && c <= '9')
111 return (int)c - (int)'0';
112 else if (c >= 'A' && c <= 'F')
113 return (int)c - (int)'A' + 10;
114 else if (c >= 'a' && c <= 'f')
115 return (int)c - (int)'a' + 10;
116 else
117 return -1;
118}
119
120/**
121 * Return a copy of str, converting each escaped "%XX" to the char value.
122 * @param str
123 */
124static string unescape(const string &str)
125{
126 ostringstream result;
127
128 for (unsigned int i = 0; i < str.size(); ++i) {
129 if (str[i] == '%' && i + 2 < str.size()) {
130 int hi = fromHexChar(str[i + 1]);
131 int lo = fromHexChar(str[i + 2]);
132
133 if (hi < 0 || lo < 0)
134 // Invalid hex characters, so just keep the escaped string.
135 result << str[i] << str[i + 1] << str[i + 2];
136 else
137 result << (unsigned char)(16 * hi + lo);
138
139 // Skip ahead past the escaped value.
140 i += 2;
141 }
142 else
143 // Just copy through.
144 result << str[i];
145 }
146
147 return result.str();
148}
149
Jeff Thompson016ed642013-07-02 14:39:06 -0700150void Name::get(struct ndn_Name &nameStruct)
Jeff Thompson48815112013-06-28 18:22:48 -0700151{
Jeff Thompson016ed642013-07-02 14:39:06 -0700152 if (nameStruct.maxComponents < components_.size())
153 throw runtime_error("nameStruct.maxComponents must be >= this name getNComponents()");
154
155 nameStruct.nComponents = components_.size();
156 for (unsigned int i = 0; i < nameStruct.nComponents; ++i)
157 components_[i].get(nameStruct.components[i]);
Jeff Thompson48815112013-06-28 18:22:48 -0700158}
159
Jeff Thompsonb468c312013-07-01 17:50:14 -0700160void Name::set(struct ndn_Name &nameStruct)
161{
162 clear();
Jeff Thompsonccb13c12013-07-01 18:16:00 -0700163 for (unsigned int i = 0; i < nameStruct.nComponents; ++i)
Jeff Thompsonb468c312013-07-01 17:50:14 -0700164 addComponent(nameStruct.components[i].value, nameStruct.components[i].valueLength);
165}
166
Jeff Thompsone6063512013-07-01 15:11:28 -0700167std::string Name::to_uri()
168{
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700169 if (components_.size() == 0)
170 return "/";
171
172 ostringstream result;
Jeff Thompsonccb13c12013-07-01 18:16:00 -0700173 for (unsigned int i = 0; i < components_.size(); ++i) {
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700174 result << "/";
175 toEscapedString(components_[i].getValue(), result);
Jeff Thompsone6063512013-07-01 15:11:28 -0700176 }
177
Jeff Thompson4b2479a2013-07-02 15:37:39 -0700178 return result.str();
Jeff Thompsone6063512013-07-01 15:11:28 -0700179}
180
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700181}