forked from premiere/premiere-libtorrent
added base32encode and moved it into escape_string.hpp
This commit is contained in:
parent
f3d45fcfc1
commit
9727634f09
|
@ -41,6 +41,12 @@ namespace libtorrent
|
|||
std::string TORRENT_EXPORT unescape_string(std::string const& s);
|
||||
std::string TORRENT_EXPORT escape_string(const char* str, int len);
|
||||
std::string TORRENT_EXPORT escape_path(const char* str, int len);
|
||||
|
||||
// encodes a string using the base64 scheme
|
||||
TORRENT_EXPORT std::string base64encode(const std::string& s);
|
||||
// encodes a string using the base32 scheme
|
||||
TORRENT_EXPORT std::string base32encode(const std::string& s);
|
||||
|
||||
}
|
||||
|
||||
#endif // TORRENT_ESCAPE_STRING_HPP_INCLUDED
|
||||
|
|
|
@ -71,9 +71,6 @@ namespace libtorrent
|
|||
struct timeout_handler;
|
||||
struct tracker_connection;
|
||||
|
||||
// encodes a string using the base64 scheme
|
||||
TORRENT_EXPORT std::string base64encode(const std::string& s);
|
||||
|
||||
// returns -1 if gzip header is invalid or the header size in bytes
|
||||
TORRENT_EXPORT int gzip_header(const char* buf, int size);
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
#include <iomanip>
|
||||
#include <cctype>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
#include "libtorrent/assert.hpp"
|
||||
|
||||
|
@ -148,4 +149,120 @@ namespace libtorrent
|
|||
}
|
||||
return ret.str();
|
||||
}
|
||||
|
||||
std::string base64encode(const std::string& s)
|
||||
{
|
||||
static const char base64_table[] =
|
||||
{
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
||||
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
||||
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
||||
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
||||
'w', 'x', 'y', 'z', '0', '1', '2', '3',
|
||||
'4', '5', '6', '7', '8', '9', '+', '/'
|
||||
};
|
||||
|
||||
unsigned char inbuf[3];
|
||||
unsigned char outbuf[4];
|
||||
|
||||
std::string ret;
|
||||
for (std::string::const_iterator i = s.begin(); i != s.end();)
|
||||
{
|
||||
// available input is 1,2 or 3 bytes
|
||||
// since we read 3 bytes at a time at most
|
||||
int available_input = (std::min)(3, (int)std::distance(i, s.end()));
|
||||
|
||||
// clear input buffer
|
||||
std::fill(inbuf, inbuf+3, 0);
|
||||
|
||||
// read a chunk of input into inbuf
|
||||
for (int j = 0; j < available_input; ++j)
|
||||
{
|
||||
inbuf[j] = *i;
|
||||
++i;
|
||||
}
|
||||
|
||||
// encode inbuf to outbuf
|
||||
outbuf[0] = (inbuf[0] & 0xfc) >> 2;
|
||||
outbuf[1] = ((inbuf[0] & 0x03) << 4) | ((inbuf [1] & 0xf0) >> 4);
|
||||
outbuf[2] = ((inbuf[1] & 0x0f) << 2) | ((inbuf [2] & 0xc0) >> 6);
|
||||
outbuf[3] = inbuf[2] & 0x3f;
|
||||
|
||||
// write output
|
||||
for (int j = 0; j < available_input+1; ++j)
|
||||
{
|
||||
ret += base64_table[outbuf[j]];
|
||||
}
|
||||
|
||||
// write pad
|
||||
for (int j = 0; j < 3 - available_input; ++j)
|
||||
{
|
||||
ret += '=';
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string base32encode(std::string const& s)
|
||||
{
|
||||
static const char base32_table[] =
|
||||
{
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
||||
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
||||
'Y', 'Z', '2', '3', '4', '5', '6', '7'
|
||||
};
|
||||
|
||||
int input_output_mapping[] = {0, 2, 4, 5, 7, 8};
|
||||
|
||||
unsigned char inbuf[5];
|
||||
unsigned char outbuf[8];
|
||||
|
||||
std::cerr << "base32(" << s << ") = ";
|
||||
std::string ret;
|
||||
for (std::string::const_iterator i = s.begin(); i != s.end();)
|
||||
{
|
||||
// available input is 1,2 or 3 bytes
|
||||
// since we read 3 bytes at a time at most
|
||||
int available_input = (std::min)(5, (int)std::distance(i, s.end()));
|
||||
|
||||
// clear input buffer
|
||||
std::fill(inbuf, inbuf+5, 0);
|
||||
|
||||
// read a chunk of input into inbuf
|
||||
for (int j = 0; j < available_input; ++j)
|
||||
{
|
||||
inbuf[j] = *i;
|
||||
++i;
|
||||
}
|
||||
|
||||
// encode inbuf to outbuf
|
||||
outbuf[0] = (inbuf[0] & 0xf8) >> 3;
|
||||
outbuf[1] = ((inbuf[0] & 0x07) << 2) | ((inbuf[1] & 0xc0) >> 6);
|
||||
outbuf[2] = ((inbuf[1] & 0x3e) >> 1);
|
||||
outbuf[3] = ((inbuf[1] & 0x01) << 4) | ((inbuf[2] & 0xf0) >> 4);
|
||||
outbuf[4] = ((inbuf[2] & 0x0f) << 1) | ((inbuf[3] & 0x80) >> 7);
|
||||
outbuf[5] = ((inbuf[3] & 0x7c) >> 2);
|
||||
outbuf[6] = ((inbuf[3] & 0x03) << 3) | ((inbuf[4] & 0xe0) >> 5);
|
||||
outbuf[7] = inbuf[4] & 0x1f;
|
||||
|
||||
// write output
|
||||
int num_out = input_output_mapping[available_input];
|
||||
for (int j = 0; j < num_out; ++j)
|
||||
{
|
||||
ret += base32_table[outbuf[j]];
|
||||
}
|
||||
|
||||
// write pad
|
||||
for (int j = 0; j < 8 - num_out; ++j)
|
||||
{
|
||||
ret += '=';
|
||||
}
|
||||
}
|
||||
std::cerr << ret << std::endl;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
*/
|
||||
|
||||
#include "libtorrent/http_connection.hpp"
|
||||
#include "libtorrent/escape_string.hpp"
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
|
|
@ -33,7 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
#include "libtorrent/pch.hpp"
|
||||
|
||||
#include "libtorrent/http_stream.hpp"
|
||||
#include "libtorrent/tracker_manager.hpp" // for base64encode
|
||||
#include "libtorrent/escape_string.hpp" // for base64encode
|
||||
|
||||
namespace libtorrent
|
||||
{
|
||||
|
|
|
@ -234,61 +234,6 @@ namespace libtorrent
|
|||
return false;
|
||||
}
|
||||
|
||||
std::string base64encode(const std::string& s)
|
||||
{
|
||||
static const char base64_table[] =
|
||||
{
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
||||
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
||||
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
||||
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
||||
'w', 'x', 'y', 'z', '0', '1', '2', '3',
|
||||
'4', '5', '6', '7', '8', '9', '+', '/'
|
||||
};
|
||||
|
||||
unsigned char inbuf[3];
|
||||
unsigned char outbuf[4];
|
||||
|
||||
std::string ret;
|
||||
for (std::string::const_iterator i = s.begin(); i != s.end();)
|
||||
{
|
||||
// available input is 1,2 or 3 bytes
|
||||
// since we read 3 bytes at a time at most
|
||||
int available_input = (std::min)(3, (int)std::distance(i, s.end()));
|
||||
|
||||
// clear input buffer
|
||||
std::fill(inbuf, inbuf+3, 0);
|
||||
|
||||
// read a chunk of input into inbuf
|
||||
for (int j = 0; j < available_input; ++j)
|
||||
{
|
||||
inbuf[j] = *i;
|
||||
++i;
|
||||
}
|
||||
|
||||
// encode inbuf to outbuf
|
||||
outbuf[0] = (inbuf[0] & 0xfc) >> 2;
|
||||
outbuf[1] = ((inbuf[0] & 0x03) << 4) | ((inbuf [1] & 0xf0) >> 4);
|
||||
outbuf[2] = ((inbuf[1] & 0x0f) << 2) | ((inbuf [2] & 0xc0) >> 6);
|
||||
outbuf[3] = inbuf[2] & 0x3f;
|
||||
|
||||
// write output
|
||||
for (int j = 0; j < available_input+1; ++j)
|
||||
{
|
||||
ret += base64_table[outbuf[j]];
|
||||
}
|
||||
|
||||
// write pad
|
||||
for (int j = 0; j < 3 - available_input; ++j)
|
||||
{
|
||||
ret += '=';
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
timeout_handler::timeout_handler(asio::strand& str)
|
||||
: m_strand(str)
|
||||
, m_start_time(time_now())
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
#include "libtorrent/upnp.hpp"
|
||||
#include "libtorrent/entry.hpp"
|
||||
#include "libtorrent/torrent_info.hpp"
|
||||
#include "libtorrent/escape_string.hpp"
|
||||
|
||||
#include <boost/tuple/tuple.hpp>
|
||||
#include <boost/tuple/tuple_comparison.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
|
@ -89,6 +91,14 @@ int test_main()
|
|||
TEST_CHECK(base64encode("fooba") == "Zm9vYmE=");
|
||||
TEST_CHECK(base64encode("foobar") == "Zm9vYmFy");
|
||||
|
||||
TEST_CHECK(base32encode("") == "");
|
||||
TEST_CHECK(base32encode("f") == "MY======");
|
||||
TEST_CHECK(base32encode("fo") == "MZXQ====");
|
||||
TEST_CHECK(base32encode("foo") == "MZXW6===");
|
||||
TEST_CHECK(base32encode("foob") == "MZXW6YQ=");
|
||||
TEST_CHECK(base32encode("fooba") == "MZXW6YTB");
|
||||
TEST_CHECK(base32encode("foobar") == "MZXW6YTBOI======");
|
||||
|
||||
// HTTP request parser
|
||||
|
||||
http_parser parser;
|
||||
|
|
Loading…
Reference in New Issue