file_progress fix
[libtorrent.git] / src / escape_string.cpp
blob616570014de2c8750ea415e50e876c5e0256afa5
1 /*
3 Copyright (c) 2003, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include "libtorrent/pch.hpp"
35 #include <string>
36 #include <stdexcept>
37 #include <sstream>
38 #include <iomanip>
39 #include <cctype>
40 #include <algorithm>
41 #include <iostream>
43 #include <boost/optional.hpp>
45 #include "libtorrent/assert.hpp"
47 namespace libtorrent
49 std::string unescape_string(std::string const& s)
51 std::string ret;
52 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
54 if(*i == '+')
56 ret += ' ';
58 else if (*i != '%')
60 ret += *i;
62 else
64 ++i;
65 if (i == s.end())
66 #ifdef BOOST_NO_EXCEPTIONS
67 return ret;
68 #else
69 throw std::runtime_error("invalid escaped string");
70 #endif
72 int high;
73 if(*i >= '0' && *i <= '9') high = *i - '0';
74 else if(*i >= 'A' && *i <= 'F') high = *i + 10 - 'A';
75 else if(*i >= 'a' && *i <= 'f') high = *i + 10 - 'a';
76 else
77 #ifdef BOOST_NO_EXCEPTIONS
78 return ret;
79 #else
80 throw std::runtime_error("invalid escaped string");
81 #endif
83 ++i;
84 if (i == s.end())
85 #ifdef BOOST_NO_EXCEPTIONS
86 return ret;
87 #else
88 throw std::runtime_error("invalid escaped string");
89 #endif
91 int low;
92 if(*i >= '0' && *i <= '9') low = *i - '0';
93 else if(*i >= 'A' && *i <= 'F') low = *i + 10 - 'A';
94 else if(*i >= 'a' && *i <= 'f') low = *i + 10 - 'a';
95 else
96 #ifdef BOOST_NO_EXCEPTIONS
97 return ret;
98 #else
99 throw std::runtime_error("invalid escaped string");
100 #endif
102 ret += char(high * 16 + low);
105 return ret;
108 std::string escape_string(const char* str, int len)
110 TORRENT_ASSERT(str != 0);
111 TORRENT_ASSERT(len >= 0);
112 // http://www.ietf.org/rfc/rfc2396.txt
113 // section 2.3
114 // some trackers seems to require that ' is escaped
115 // static const char unreserved_chars[] = "-_.!~*'()";
116 static const char unreserved_chars[] = "-_.!~*()"
117 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
118 "0123456789";
120 std::stringstream ret;
121 ret << std::hex << std::setfill('0');
122 for (int i = 0; i < len; ++i)
124 if (std::count(
125 unreserved_chars
126 , unreserved_chars+sizeof(unreserved_chars)-1
127 , *str))
129 ret << *str;
131 else
133 ret << '%'
134 << std::setw(2)
135 << (int)static_cast<unsigned char>(*str);
137 ++str;
139 return ret.str();
142 std::string escape_path(const char* str, int len)
144 TORRENT_ASSERT(str != 0);
145 TORRENT_ASSERT(len >= 0);
146 static const char unreserved_chars[] = "/-_.!~*()"
147 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
148 "0123456789";
150 std::stringstream ret;
151 ret << std::hex << std::setfill('0');
152 for (int i = 0; i < len; ++i)
154 if (std::count(
155 unreserved_chars
156 , unreserved_chars+sizeof(unreserved_chars)-1
157 , *str))
159 ret << *str;
161 else
163 ret << '%'
164 << std::setw(2)
165 << (int)static_cast<unsigned char>(*str);
167 ++str;
169 return ret.str();
172 std::string base64encode(const std::string& s)
174 static const char base64_table[] =
176 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
177 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
178 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
179 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
180 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
181 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
182 'w', 'x', 'y', 'z', '0', '1', '2', '3',
183 '4', '5', '6', '7', '8', '9', '+', '/'
186 unsigned char inbuf[3];
187 unsigned char outbuf[4];
189 std::string ret;
190 for (std::string::const_iterator i = s.begin(); i != s.end();)
192 // available input is 1,2 or 3 bytes
193 // since we read 3 bytes at a time at most
194 int available_input = (std::min)(3, (int)std::distance(i, s.end()));
196 // clear input buffer
197 std::fill(inbuf, inbuf+3, 0);
199 // read a chunk of input into inbuf
200 std::copy(i, i + available_input, inbuf);
201 i += available_input;
203 // encode inbuf to outbuf
204 outbuf[0] = (inbuf[0] & 0xfc) >> 2;
205 outbuf[1] = ((inbuf[0] & 0x03) << 4) | ((inbuf [1] & 0xf0) >> 4);
206 outbuf[2] = ((inbuf[1] & 0x0f) << 2) | ((inbuf [2] & 0xc0) >> 6);
207 outbuf[3] = inbuf[2] & 0x3f;
209 // write output
210 for (int j = 0; j < available_input+1; ++j)
212 ret += base64_table[outbuf[j]];
215 // write pad
216 for (int j = 0; j < 3 - available_input; ++j)
218 ret += '=';
221 return ret;
224 std::string base32encode(std::string const& s)
226 static const char base32_table[] =
228 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
229 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
230 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
231 'Y', 'Z', '2', '3', '4', '5', '6', '7'
234 int input_output_mapping[] = {0, 2, 4, 5, 7, 8};
236 unsigned char inbuf[5];
237 unsigned char outbuf[8];
239 std::string ret;
240 for (std::string::const_iterator i = s.begin(); i != s.end();)
242 int available_input = (std::min)(5, (int)std::distance(i, s.end()));
244 // clear input buffer
245 std::fill(inbuf, inbuf+5, 0);
247 // read a chunk of input into inbuf
248 std::copy(i, i + available_input, inbuf);
249 i += available_input;
251 // encode inbuf to outbuf
252 outbuf[0] = (inbuf[0] & 0xf8) >> 3;
253 outbuf[1] = ((inbuf[0] & 0x07) << 2) | ((inbuf[1] & 0xc0) >> 6);
254 outbuf[2] = ((inbuf[1] & 0x3e) >> 1);
255 outbuf[3] = ((inbuf[1] & 0x01) << 4) | ((inbuf[2] & 0xf0) >> 4);
256 outbuf[4] = ((inbuf[2] & 0x0f) << 1) | ((inbuf[3] & 0x80) >> 7);
257 outbuf[5] = ((inbuf[3] & 0x7c) >> 2);
258 outbuf[6] = ((inbuf[3] & 0x03) << 3) | ((inbuf[4] & 0xe0) >> 5);
259 outbuf[7] = inbuf[4] & 0x1f;
261 // write output
262 int num_out = input_output_mapping[available_input];
263 for (int j = 0; j < num_out; ++j)
265 ret += base32_table[outbuf[j]];
268 // write pad
269 for (int j = 0; j < 8 - num_out; ++j)
271 ret += '=';
274 return ret;
277 std::string base32decode(std::string const& s)
279 unsigned char inbuf[8];
280 unsigned char outbuf[5];
282 std::string ret;
283 for (std::string::const_iterator i = s.begin(); i != s.end();)
285 int available_input = (std::min)(8, (int)std::distance(i, s.end()));
287 int pad_start = 0;
288 if (available_input < 8) pad_start = available_input;
290 // clear input buffer
291 std::fill(inbuf, inbuf+8, 0);
292 for (int j = 0; j < available_input; ++j)
294 char in = std::toupper(*i++);
295 if (in >= 'A' && in <= 'Z')
296 inbuf[j] = in - 'A';
297 else if (in >= '2' && in <= '7')
298 inbuf[j] = in - '2' + ('Z' - 'A') + 1;
299 else if (in == '=')
301 inbuf[j] = 0;
302 if (pad_start == 0) pad_start = j;
304 else if (in == '1')
305 inbuf[j] = 'I' - 'A';
306 else
307 return std::string();
308 TORRENT_ASSERT(inbuf[j] == (inbuf[j] & 0x1f));
311 // decode inbuf to outbuf
312 outbuf[0] = inbuf[0] << 3;
313 outbuf[0] |= inbuf[1] >> 2;
314 outbuf[1] = (inbuf[1] & 0x3) << 6;
315 outbuf[1] |= inbuf[2] << 1;
316 outbuf[1] |= (inbuf[3] & 0x10) >> 4;
317 outbuf[2] = (inbuf[3] & 0x0f) << 4;
318 outbuf[2] |= (inbuf[4] & 0x1e) >> 1;
319 outbuf[3] = (inbuf[4] & 0x01) << 7;
320 outbuf[3] |= (inbuf[5] & 0x1f) << 2;
321 outbuf[3] |= (inbuf[6] & 0x18) >> 3;
322 outbuf[4] = (inbuf[6] & 0x07) << 5;
323 outbuf[4] |= inbuf[7];
325 int input_output_mapping[] = {5, 1, 1, 2, 2, 3, 4, 4, 5};
326 int num_out = input_output_mapping[pad_start];
328 // write output
329 std::copy(outbuf, outbuf + num_out, std::back_inserter(ret));
331 return ret;
334 boost::optional<std::string> url_has_argument(
335 std::string const& url, std::string argument)
337 size_t i = url.find('?');
338 if (i == std::string::npos) return boost::optional<std::string>();
339 ++i;
341 argument += '=';
343 if (url.compare(i, argument.size(), argument) == 0)
345 size_t pos = i + argument.size();
346 return url.substr(pos, url.find('&', pos) - pos);
348 argument.insert(0, "&");
349 i = url.find(argument, i);
350 if (i == std::string::npos) return boost::optional<std::string>();
351 size_t pos = i + argument.size();
352 return url.substr(pos, url.find('&', pos) - pos);
355 TORRENT_EXPORT std::string to_hex(std::string const& s)
357 std::string ret;
358 char* digits = "0123456789abcdef";
359 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
361 ret += digits[((unsigned char)*i) >> 4];
362 ret += digits[((unsigned char)*i) & 0xf];
364 return ret;