forked from premiere/premiere-libtorrent
added magnet-uri functions and a base32 decode function
This commit is contained in:
parent
a7b5250058
commit
20621cae02
1
Jamfile
1
Jamfile
|
@ -243,6 +243,7 @@ SOURCES =
|
||||||
disk_io_thread
|
disk_io_thread
|
||||||
enum_net
|
enum_net
|
||||||
broadcast_socket
|
broadcast_socket
|
||||||
|
magnet_uri
|
||||||
;
|
;
|
||||||
|
|
||||||
KADEMLIA_SOURCES =
|
KADEMLIA_SOURCES =
|
||||||
|
|
|
@ -62,6 +62,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include "libtorrent/identify_client.hpp"
|
#include "libtorrent/identify_client.hpp"
|
||||||
#include "libtorrent/alert_types.hpp"
|
#include "libtorrent/alert_types.hpp"
|
||||||
#include "libtorrent/ip_filter.hpp"
|
#include "libtorrent/ip_filter.hpp"
|
||||||
|
#include "libtorrent/magnet_uri.hpp"
|
||||||
|
|
||||||
using boost::bind;
|
using boost::bind;
|
||||||
|
|
||||||
|
@ -1049,6 +1050,7 @@ int main(int ac, char* av[])
|
||||||
out << "peers: " << s.num_peers << " "
|
out << "peers: " << s.num_peers << " "
|
||||||
<< "seeds: " << s.num_seeds << " "
|
<< "seeds: " << s.num_seeds << " "
|
||||||
<< "distributed copies: " << s.distributed_copies << "\n"
|
<< "distributed copies: " << s.distributed_copies << "\n"
|
||||||
|
<< " magnet-link: " << make_magnet_uri(h) << "\n"
|
||||||
<< " download: " << esc("32") << (s.download_rate > 0 ? add_suffix(s.download_rate) + "/s ": " ") << esc("0")
|
<< " download: " << esc("32") << (s.download_rate > 0 ? add_suffix(s.download_rate) + "/s ": " ") << esc("0")
|
||||||
<< "(" << esc("32") << add_suffix(s.total_download) << esc("0") << ") ";
|
<< "(" << esc("32") << add_suffix(s.total_download) << esc("0") << ") ";
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,7 @@ namespace libtorrent
|
||||||
TORRENT_EXPORT std::string base64encode(std::string const& s);
|
TORRENT_EXPORT std::string base64encode(std::string const& s);
|
||||||
// encodes a string using the base32 scheme
|
// encodes a string using the base32 scheme
|
||||||
TORRENT_EXPORT std::string base32encode(std::string const& s);
|
TORRENT_EXPORT std::string base32encode(std::string const& s);
|
||||||
|
TORRENT_EXPORT std::string base32decode(std::string const& s);
|
||||||
|
|
||||||
TORRENT_EXPORT boost::optional<std::string> url_has_argument(
|
TORRENT_EXPORT boost::optional<std::string> url_has_argument(
|
||||||
std::string const& url, std::string argument);
|
std::string const& url, std::string argument);
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
Copyright (c) 2007, Arvid Norberg
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions
|
||||||
|
are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer in
|
||||||
|
the documentation and/or other materials provided with the distribution.
|
||||||
|
* Neither the name of the author nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived
|
||||||
|
from this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef TORRENT_MAGNET_URI_HPP_INCLUDED
|
||||||
|
#define TORRENT_MAGNET_URI_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include "libtorrent/config.hpp"
|
||||||
|
#include "libtorrent/torrent_handle.hpp"
|
||||||
|
#include "libtorrent/session.hpp"
|
||||||
|
#include <boost/filesystem/path.hpp>
|
||||||
|
|
||||||
|
namespace libtorrent
|
||||||
|
{
|
||||||
|
namespace fs = boost::filesystem;
|
||||||
|
|
||||||
|
struct torrent_handle;
|
||||||
|
|
||||||
|
std::string TORRENT_EXPORT make_magnet_uri(torrent_handle const& handle);
|
||||||
|
|
||||||
|
torrent_handle TORRENT_EXPORT add_magnet_uri(session& ses, std::string const& uri
|
||||||
|
, fs::path const& save_path
|
||||||
|
, storage_mode_t storage_mode = storage_mode_sparse
|
||||||
|
, bool paused = false
|
||||||
|
, storage_constructor_type sc = default_storage_constructor
|
||||||
|
, void* userdata = 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include <boost/optional.hpp>
|
#include <boost/optional.hpp>
|
||||||
|
|
||||||
|
@ -178,11 +179,8 @@ namespace libtorrent
|
||||||
std::fill(inbuf, inbuf+3, 0);
|
std::fill(inbuf, inbuf+3, 0);
|
||||||
|
|
||||||
// read a chunk of input into inbuf
|
// read a chunk of input into inbuf
|
||||||
for (int j = 0; j < available_input; ++j)
|
std::copy(i, i + available_input, inbuf);
|
||||||
{
|
i += available_input;
|
||||||
inbuf[j] = *i;
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// encode inbuf to outbuf
|
// encode inbuf to outbuf
|
||||||
outbuf[0] = (inbuf[0] & 0xfc) >> 2;
|
outbuf[0] = (inbuf[0] & 0xfc) >> 2;
|
||||||
|
@ -223,19 +221,14 @@ namespace libtorrent
|
||||||
std::string ret;
|
std::string ret;
|
||||||
for (std::string::const_iterator i = s.begin(); i != s.end();)
|
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()));
|
int available_input = (std::min)(5, (int)std::distance(i, s.end()));
|
||||||
|
|
||||||
// clear input buffer
|
// clear input buffer
|
||||||
std::fill(inbuf, inbuf+5, 0);
|
std::fill(inbuf, inbuf+5, 0);
|
||||||
|
|
||||||
// read a chunk of input into inbuf
|
// read a chunk of input into inbuf
|
||||||
for (int j = 0; j < available_input; ++j)
|
std::copy(i, i + available_input, inbuf);
|
||||||
{
|
i += available_input;
|
||||||
inbuf[j] = *i;
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// encode inbuf to outbuf
|
// encode inbuf to outbuf
|
||||||
outbuf[0] = (inbuf[0] & 0xf8) >> 3;
|
outbuf[0] = (inbuf[0] & 0xf8) >> 3;
|
||||||
|
@ -263,6 +256,64 @@ namespace libtorrent
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string base32decode(std::string const& s)
|
||||||
|
{
|
||||||
|
unsigned char inbuf[8];
|
||||||
|
unsigned char outbuf[5];
|
||||||
|
|
||||||
|
std::string ret;
|
||||||
|
for (std::string::const_iterator i = s.begin(); i != s.end();)
|
||||||
|
{
|
||||||
|
int available_input = (std::min)(8, (int)std::distance(i, s.end()));
|
||||||
|
|
||||||
|
int pad_start = 0;
|
||||||
|
if (available_input < 8) pad_start = available_input;
|
||||||
|
|
||||||
|
// clear input buffer
|
||||||
|
std::fill(inbuf, inbuf+8, 0);
|
||||||
|
for (int j = 0; j < available_input; ++j)
|
||||||
|
{
|
||||||
|
char in = std::toupper(*i++);
|
||||||
|
if (in >= 'A' && in <= 'Z')
|
||||||
|
inbuf[j] = in - 'A';
|
||||||
|
else if (in >= '2' && in <= '7')
|
||||||
|
inbuf[j] = in - '2' + ('Z' - 'A') + 1;
|
||||||
|
else if (in == '=')
|
||||||
|
{
|
||||||
|
inbuf[j] = 0;
|
||||||
|
if (pad_start == 0) pad_start = j;
|
||||||
|
}
|
||||||
|
else if (in == '1')
|
||||||
|
inbuf[j] = 'I' - 'A';
|
||||||
|
else
|
||||||
|
return std::string();
|
||||||
|
TORRENT_ASSERT(inbuf[j] == (inbuf[j] & 0x1f));
|
||||||
|
}
|
||||||
|
|
||||||
|
// decode inbuf to outbuf
|
||||||
|
outbuf[0] = inbuf[0] << 3;
|
||||||
|
outbuf[0] |= inbuf[1] >> 2;
|
||||||
|
outbuf[1] = (inbuf[1] & 0x3) << 6;
|
||||||
|
outbuf[1] |= inbuf[2] << 1;
|
||||||
|
outbuf[1] |= (inbuf[3] & 0x10) >> 4;
|
||||||
|
outbuf[2] = (inbuf[3] & 0x0f) << 4;
|
||||||
|
outbuf[2] |= (inbuf[4] & 0x1e) >> 1;
|
||||||
|
outbuf[3] = (inbuf[4] & 0x01) << 7;
|
||||||
|
outbuf[3] |= (inbuf[5] & 0x1f) << 2;
|
||||||
|
outbuf[3] |= (inbuf[6] & 0x18) >> 3;
|
||||||
|
outbuf[4] = (inbuf[6] & 0x07) << 5;
|
||||||
|
outbuf[4] |= inbuf[7];
|
||||||
|
|
||||||
|
int input_output_mapping[] = {5, 1, 1, 2, 2, 3, 4, 4, 5};
|
||||||
|
int num_out = input_output_mapping[pad_start];
|
||||||
|
|
||||||
|
// write output
|
||||||
|
std::copy(outbuf, outbuf + num_out, std::back_inserter(ret));
|
||||||
|
}
|
||||||
|
std::cerr << " base32decode(): " << ret << std::endl;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
boost::optional<std::string> url_has_argument(
|
boost::optional<std::string> url_has_argument(
|
||||||
std::string const& url, std::string argument)
|
std::string const& url, std::string argument)
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,99 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
Copyright (c) 2007, Arvid Norberg
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions
|
||||||
|
are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer in
|
||||||
|
the documentation and/or other materials provided with the distribution.
|
||||||
|
* Neither the name of the author nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived
|
||||||
|
from this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "libtorrent/magnet_uri.hpp"
|
||||||
|
#include "libtorrent/session.hpp"
|
||||||
|
#include "libtorrent/torrent_handle.hpp"
|
||||||
|
#include "libtorrent/escape_string.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace libtorrent
|
||||||
|
{
|
||||||
|
std::string make_magnet_uri(torrent_handle const& handle)
|
||||||
|
{
|
||||||
|
std::stringstream ret;
|
||||||
|
if (!handle.is_valid()) return ret.str();
|
||||||
|
|
||||||
|
std::string name = handle.name();
|
||||||
|
|
||||||
|
ret << "magnet:?xt=urn:btih:" << base32encode((char*)handle.info_hash().begin());
|
||||||
|
if (!name.empty())
|
||||||
|
ret << "&dn=" << escape_string(name.c_str(), name.length());
|
||||||
|
torrent_status st = handle.status();
|
||||||
|
if (!st.current_tracker.empty())
|
||||||
|
{
|
||||||
|
ret << "&tr=" << escape_string(st.current_tracker.c_str()
|
||||||
|
, st.current_tracker.length());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::vector<announce_entry> const& tr = handle.trackers();
|
||||||
|
if (!tr.empty())
|
||||||
|
{
|
||||||
|
ret << "&tr=" << escape_string(tr[0].url.c_str()
|
||||||
|
, tr[0].url.length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
torrent_handle add_magnet_uri(session& ses, std::string const& uri
|
||||||
|
, fs::path const& save_path
|
||||||
|
, storage_mode_t storage_mode
|
||||||
|
, bool paused
|
||||||
|
, storage_constructor_type sc
|
||||||
|
, void* userdata)
|
||||||
|
{
|
||||||
|
std::string name;
|
||||||
|
std::string tracker;
|
||||||
|
|
||||||
|
boost::optional<std::string> display_name = url_has_argument(uri, "dn");
|
||||||
|
if (display_name) name = unescape_string(display_name->c_str());
|
||||||
|
boost::optional<std::string> tracker_string = url_has_argument(uri, "tr");
|
||||||
|
if (tracker_string) tracker = unescape_string(tracker_string->c_str());
|
||||||
|
|
||||||
|
boost::optional<std::string> btih = url_has_argument(uri, "xt");
|
||||||
|
if (!btih) return torrent_handle();
|
||||||
|
|
||||||
|
if (btih->compare(0, 9, "urn:btih:") != 0) return torrent_handle();
|
||||||
|
|
||||||
|
sha1_hash info_hash(base32decode(btih->substr(9)));
|
||||||
|
|
||||||
|
return ses.add_torrent(tracker.empty() ? 0 : tracker.c_str(), info_hash
|
||||||
|
, name.empty() ? 0 : name.c_str(), save_path, entry()
|
||||||
|
, storage_mode, paused, sc, userdata);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -101,6 +101,25 @@ int test_main()
|
||||||
TEST_CHECK(base32encode("fooba") == "MZXW6YTB");
|
TEST_CHECK(base32encode("fooba") == "MZXW6YTB");
|
||||||
TEST_CHECK(base32encode("foobar") == "MZXW6YTBOI======");
|
TEST_CHECK(base32encode("foobar") == "MZXW6YTBOI======");
|
||||||
|
|
||||||
|
TEST_CHECK(base32decode("") == "");
|
||||||
|
TEST_CHECK(base32decode("MY======") == "f");
|
||||||
|
TEST_CHECK(base32decode("MZXQ====") == "fo");
|
||||||
|
TEST_CHECK(base32decode("MZXW6===") == "foo");
|
||||||
|
TEST_CHECK(base32decode("MZXW6YQ=") == "foob");
|
||||||
|
TEST_CHECK(base32decode("MZXW6YTB") == "fooba");
|
||||||
|
TEST_CHECK(base32decode("MZXW6YTBOI======") == "foobar");
|
||||||
|
|
||||||
|
TEST_CHECK(base32decode("MY") == "f");
|
||||||
|
TEST_CHECK(base32decode("MZXW6YQ") == "foob");
|
||||||
|
TEST_CHECK(base32decode("MZXW6YTBOI") == "foobar");
|
||||||
|
TEST_CHECK(base32decode("mZXw6yTBO1======") == "foobar");
|
||||||
|
|
||||||
|
std::string test;
|
||||||
|
for (int i = 0; i < 255; ++i)
|
||||||
|
test += char(i);
|
||||||
|
|
||||||
|
TEST_CHECK(base32decode(base32encode(test)) == test);
|
||||||
|
|
||||||
// url_has_argument
|
// url_has_argument
|
||||||
|
|
||||||
TEST_CHECK(!url_has_argument("http://127.0.0.1/test", "test"));
|
TEST_CHECK(!url_has_argument("http://127.0.0.1/test", "test"));
|
||||||
|
|
Loading…
Reference in New Issue