3 Copyright (c) 2003, Arvid Norberg
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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"
43 #include <boost/optional.hpp>
45 #include "libtorrent/assert.hpp"
49 std::string
unescape_string(std::string
const& s
)
52 for (std::string::const_iterator i
= s
.begin(); i
!= s
.end(); ++i
)
66 #ifdef BOOST_NO_EXCEPTIONS
69 throw std::runtime_error("invalid escaped string");
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';
77 #ifdef BOOST_NO_EXCEPTIONS
80 throw std::runtime_error("invalid escaped string");
85 #ifdef BOOST_NO_EXCEPTIONS
88 throw std::runtime_error("invalid escaped string");
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';
96 #ifdef BOOST_NO_EXCEPTIONS
99 throw std::runtime_error("invalid escaped string");
102 ret
+= char(high
* 16 + low
);
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
114 // some trackers seems to require that ' is escaped
115 // static const char unreserved_chars[] = "-_.!~*'()";
116 static const char unreserved_chars
[] = "-_.!~*()"
117 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
120 std::stringstream ret
;
121 ret
<< std::hex
<< std::setfill('0');
122 for (int i
= 0; i
< len
; ++i
)
126 , unreserved_chars
+sizeof(unreserved_chars
)-1
135 << (int)static_cast<unsigned char>(*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"
150 std::stringstream ret
;
151 ret
<< std::hex
<< std::setfill('0');
152 for (int i
= 0; i
< len
; ++i
)
156 , unreserved_chars
+sizeof(unreserved_chars
)-1
165 << (int)static_cast<unsigned char>(*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];
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;
210 for (int j
= 0; j
< available_input
+1; ++j
)
212 ret
+= base64_table
[outbuf
[j
]];
216 for (int j
= 0; j
< 3 - available_input
; ++j
)
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];
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;
262 int num_out
= input_output_mapping
[available_input
];
263 for (int j
= 0; j
< num_out
; ++j
)
265 ret
+= base32_table
[outbuf
[j
]];
269 for (int j
= 0; j
< 8 - num_out
; ++j
)
277 std::string
base32decode(std::string
const& s
)
279 unsigned char inbuf
[8];
280 unsigned char outbuf
[5];
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()));
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')
297 else if (in
>= '2' && in
<= '7')
298 inbuf
[j
] = in
- '2' + ('Z' - 'A') + 1;
302 if (pad_start
== 0) pad_start
= j
;
305 inbuf
[j
] = 'I' - 'A';
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
];
329 std::copy(outbuf
, outbuf
+ num_out
, std::back_inserter(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
>();
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
)
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];