2003-10-26 04:18:17 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
#include <cstdio> // for snprintf
|
|
|
|
#include <cinttypes> // for PRId64 et.al.
|
|
|
|
|
2003-10-26 04:18:17 +01:00
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
2003-11-28 18:29:27 +01:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/announce_entry.hpp"
|
2015-03-12 06:20:12 +01:00
|
|
|
#include "libtorrent/bdecode.hpp"
|
2008-08-27 20:44:35 +02:00
|
|
|
#include "libtorrent/magnet_uri.hpp"
|
2003-10-26 04:18:17 +01:00
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
int load_file(std::string const& filename, std::vector<char>& v
|
2017-04-12 20:05:53 +02:00
|
|
|
, lt::error_code& ec, int limit = 8000000)
|
2013-07-19 23:41:33 +02:00
|
|
|
{
|
|
|
|
ec.clear();
|
2016-05-17 15:24:06 +02:00
|
|
|
FILE* f = std::fopen(filename.c_str(), "rb");
|
2016-06-20 17:32:06 +02:00
|
|
|
if (f == nullptr)
|
2013-07-19 23:41:33 +02:00
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2013-07-19 23:41:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int r = fseek(f, 0, SEEK_END);
|
|
|
|
if (r != 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
long s = ftell(f);
|
|
|
|
if (s < 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s > limit)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = fseek(f, 0, SEEK_SET);
|
|
|
|
if (r != 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
v.resize(s);
|
|
|
|
if (s == 0)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-25 23:22:09 +02:00
|
|
|
r = int(fread(&v[0], 1, v.size(), f));
|
2013-07-19 23:41:33 +02:00
|
|
|
if (r < 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fclose(f);
|
2013-07-19 23:41:33 +02:00
|
|
|
|
|
|
|
if (r != s) return -3;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-10-26 04:18:17 +01:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2003-10-26 04:18:17 +01:00
|
|
|
|
2010-11-25 03:49:50 +01:00
|
|
|
if (argc < 2 || argc > 4)
|
2003-10-26 04:18:17 +01:00
|
|
|
{
|
2010-11-25 03:49:50 +01:00
|
|
|
fputs("usage: dump_torrent torrent-file [total-items-limit] [recursion-limit]\n", stderr);
|
2003-10-26 04:18:17 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2004-03-17 13:14:44 +01:00
|
|
|
|
2010-12-11 23:00:20 +01:00
|
|
|
int item_limit = 1000000;
|
2010-11-25 03:49:50 +01:00
|
|
|
int depth_limit = 1000;
|
|
|
|
|
|
|
|
if (argc > 2) item_limit = atoi(argv[2]);
|
|
|
|
if (argc > 3) depth_limit = atoi(argv[3]);
|
|
|
|
|
2014-05-10 22:20:57 +02:00
|
|
|
std::vector<char> buf;
|
|
|
|
error_code ec;
|
|
|
|
int ret = load_file(argv[1], buf, ec, 40 * 1000000);
|
|
|
|
if (ret == -1)
|
2003-10-26 04:18:17 +01:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "file too big, aborting\n");
|
2009-02-26 08:09:56 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2014-05-10 22:20:57 +02:00
|
|
|
|
2009-12-09 08:10:49 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "failed to load file: %s\n", ec.message().c_str());
|
2009-12-09 08:10:49 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2015-03-12 06:20:12 +01:00
|
|
|
bdecode_node e;
|
2013-12-23 02:40:05 +01:00
|
|
|
int pos = -1;
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("decoding. recursion limit: %d total item count limit: %d\n"
|
2010-11-25 03:49:50 +01:00
|
|
|
, depth_limit, item_limit);
|
2015-03-12 06:20:12 +01:00
|
|
|
ret = bdecode(&buf[0], &buf[0] + buf.size(), e, ec, &pos
|
2010-11-25 03:49:50 +01:00
|
|
|
, depth_limit, item_limit);
|
2003-10-26 04:18:17 +01:00
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("\n\n----- raw info -----\n\n%s\n", print_entry(e).c_str());
|
2011-06-25 19:42:49 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "failed to decode: '%s' at character: %d\n", ec.message().c_str(), pos);
|
2009-02-26 08:09:56 +01:00
|
|
|
return 1;
|
2003-10-26 04:18:17 +01:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
|
|
|
torrent_info t(e, ec);
|
|
|
|
if (ec)
|
2003-10-26 04:18:17 +01:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "%s\n", ec.message().c_str());
|
2009-02-26 08:09:56 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2010-11-25 00:49:22 +01:00
|
|
|
e.clear();
|
|
|
|
std::vector<char>().swap(buf);
|
2009-02-26 08:09:56 +01:00
|
|
|
|
|
|
|
// print info about torrent
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("\n\n----- torrent file info -----\n\n"
|
2009-12-09 08:10:49 +01:00
|
|
|
"nodes:\n");
|
2016-10-25 23:27:48 +02:00
|
|
|
for (auto const& i : t.nodes())
|
|
|
|
std::printf("%s: %d\n", i.first.c_str(), i.second);
|
2009-12-09 08:10:49 +01:00
|
|
|
|
|
|
|
puts("trackers:\n");
|
2016-10-25 23:27:48 +02:00
|
|
|
for (auto const& i : t.trackers())
|
|
|
|
std::printf("%2d: %s\n", i.tier, i.url.c_str());
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2016-06-04 16:01:43 +02:00
|
|
|
std::stringstream ih;
|
|
|
|
ih << t.info_hash();
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("number of pieces: %d\n"
|
2009-12-09 08:10:49 +01:00
|
|
|
"piece length: %d\n"
|
|
|
|
"info hash: %s\n"
|
|
|
|
"comment: %s\n"
|
|
|
|
"created by: %s\n"
|
|
|
|
"magnet link: %s\n"
|
|
|
|
"name: %s\n"
|
2010-11-25 00:49:22 +01:00
|
|
|
"number of files: %d\n"
|
2009-12-09 08:10:49 +01:00
|
|
|
"files:\n"
|
|
|
|
, t.num_pieces()
|
|
|
|
, t.piece_length()
|
2016-06-04 16:01:43 +02:00
|
|
|
, ih.str().c_str()
|
2009-12-09 08:10:49 +01:00
|
|
|
, t.comment().c_str()
|
|
|
|
, t.creator().c_str()
|
|
|
|
, make_magnet_uri(t).c_str()
|
2010-11-25 00:49:22 +01:00
|
|
|
, t.name().c_str()
|
|
|
|
, t.num_files());
|
2013-09-15 14:18:27 +02:00
|
|
|
file_storage const& st = t.files();
|
2016-12-22 16:42:33 +01:00
|
|
|
for (file_index_t i(0); i < file_index_t(st.num_files()); ++i)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const first = st.map_file(i, 0, 0).piece;
|
|
|
|
piece_index_t const last = st.map_file(i, (std::max)(std::int64_t(st.file_size(i))-1, std::int64_t(0)), 0).piece;
|
2017-08-03 11:31:03 +02:00
|
|
|
auto const flags = st.file_flags(i);
|
2016-06-04 16:01:43 +02:00
|
|
|
std::stringstream file_hash;
|
|
|
|
if (!st.hash(i).is_all_zeros())
|
|
|
|
file_hash << st.hash(i);
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf(" %8" PRIx64 " %11" PRId64 " %c%c%c%c [ %5d, %5d ] %7u %s %s %s%s\n"
|
2013-09-15 14:18:27 +02:00
|
|
|
, st.file_offset(i)
|
|
|
|
, st.file_size(i)
|
|
|
|
, ((flags & file_storage::flag_pad_file)?'p':'-')
|
|
|
|
, ((flags & file_storage::flag_executable)?'x':'-')
|
|
|
|
, ((flags & file_storage::flag_hidden)?'h':'-')
|
|
|
|
, ((flags & file_storage::flag_symlink)?'l':'-')
|
2016-12-22 16:42:33 +01:00
|
|
|
, static_cast<int>(first)
|
|
|
|
, static_cast<int>(last)
|
2016-06-18 20:01:38 +02:00
|
|
|
, std::uint32_t(st.mtime(i))
|
2016-06-04 16:01:43 +02:00
|
|
|
, file_hash.str().c_str()
|
2013-09-15 14:18:27 +02:00
|
|
|
, st.file_path(i).c_str()
|
|
|
|
, (flags & file_storage::flag_symlink) ? "-> " : ""
|
|
|
|
, (flags & file_storage::flag_symlink) ? st.symlink(i).c_str() : "");
|
2003-10-26 04:18:17 +01:00
|
|
|
}
|
2016-08-13 00:46:59 +02:00
|
|
|
std::printf("web seeds:\n");
|
|
|
|
for (auto const& ws : t.web_seeds())
|
|
|
|
{
|
|
|
|
std::printf("%s %s\n"
|
|
|
|
, ws.type == web_seed_entry::url_seed ? "BEP19" : "BEP17"
|
|
|
|
, ws.url.c_str());
|
|
|
|
}
|
2003-10-26 04:18:17 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|