merged merkle torrent creation fix from RC_0_16
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
* fix uTP edge case where udp socket buffer fills up
|
* fix uTP edge case where udp socket buffer fills up
|
||||||
* fix nagle implementation in uTP
|
* fix nagle implementation in uTP
|
||||||
|
|
||||||
|
* fixed merkle tree torrent creation bug
|
||||||
* fixed crash with empty url-lists in torrent files
|
* fixed crash with empty url-lists in torrent files
|
||||||
* added missing max_connections() function to python bindings
|
* added missing max_connections() function to python bindings
|
||||||
|
|
||||||
|
@@ -255,8 +255,14 @@ int main(int argc, char* argv[])
|
|||||||
if (!merklefile.empty())
|
if (!merklefile.empty())
|
||||||
{
|
{
|
||||||
output = fopen(merklefile.c_str(), "wb+");
|
output = fopen(merklefile.c_str(), "wb+");
|
||||||
|
if (output == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "failed to open file \"%s\": (%d) %s\n"
|
||||||
|
, merklefile.c_str(), errno, strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
int ret = fwrite(&t.merkle_tree()[0], 20, t.merkle_tree().size(), output);
|
int ret = fwrite(&t.merkle_tree()[0], 20, t.merkle_tree().size(), output);
|
||||||
if (ret != t.merkle_tree().size() * 20)
|
if (ret != t.merkle_tree().size())
|
||||||
{
|
{
|
||||||
fprintf(stderr, "failed to write %s: (%d) %s\n"
|
fprintf(stderr, "failed to write %s: (%d) %s\n"
|
||||||
, merklefile.c_str(), errno, strerror(errno));
|
, merklefile.c_str(), errno, strerror(errno));
|
||||||
|
@@ -74,6 +74,11 @@ namespace libtorrent
|
|||||||
, tracker_retry_delay_max = 60 * 60
|
, tracker_retry_delay_max = 60 * 60
|
||||||
};
|
};
|
||||||
|
|
||||||
|
TORRENT_EXTRA_EXPORT int merkle_num_leafs(int);
|
||||||
|
TORRENT_EXTRA_EXPORT int merkle_num_nodes(int);
|
||||||
|
TORRENT_EXTRA_EXPORT int merkle_get_parent(int);
|
||||||
|
TORRENT_EXTRA_EXPORT int merkle_get_sibling(int);
|
||||||
|
|
||||||
struct TORRENT_EXPORT announce_entry
|
struct TORRENT_EXPORT announce_entry
|
||||||
{
|
{
|
||||||
announce_entry(std::string const& u);
|
announce_entry(std::string const& u);
|
||||||
|
@@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#include "libtorrent/file_pool.hpp"
|
#include "libtorrent/file_pool.hpp"
|
||||||
#include "libtorrent/storage.hpp"
|
#include "libtorrent/storage.hpp"
|
||||||
#include "libtorrent/escape_string.hpp"
|
#include "libtorrent/escape_string.hpp"
|
||||||
|
#include "libtorrent/torrent_info.hpp" // for merkle_*()
|
||||||
|
|
||||||
#include <boost/bind.hpp>
|
#include <boost/bind.hpp>
|
||||||
#include <boost/next_prior.hpp>
|
#include <boost/next_prior.hpp>
|
||||||
@@ -45,11 +46,6 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
namespace libtorrent
|
namespace libtorrent
|
||||||
{
|
{
|
||||||
// defined in torrent_info.cpp
|
|
||||||
int merkle_num_leafs(int);
|
|
||||||
int merkle_num_nodes(int);
|
|
||||||
int merkle_get_parent(int);
|
|
||||||
int merkle_get_sibling(int);
|
|
||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
|
@@ -421,10 +421,11 @@ namespace libtorrent
|
|||||||
|
|
||||||
int merkle_num_leafs(int pieces)
|
int merkle_num_leafs(int pieces)
|
||||||
{
|
{
|
||||||
|
TORRENT_ASSERT(pieces > 0);
|
||||||
// round up to nearest 2 exponent
|
// round up to nearest 2 exponent
|
||||||
int i;
|
int ret = 1;
|
||||||
for (i = 0; pieces > 0; pieces >>= 1, ++i);
|
while (pieces > ret) ret <<= 1;
|
||||||
return 1 << i;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_file(std::string const& filename, std::vector<char>& v, error_code& ec, int limit)
|
int load_file(std::string const& filename, std::vector<char>& v, error_code& ec, int limit)
|
||||||
|
@@ -1913,6 +1913,74 @@ int test_main()
|
|||||||
|
|
||||||
test1.resize(100, true);
|
test1.resize(100, true);
|
||||||
TEST_CHECK(test1.all_set() == true);
|
TEST_CHECK(test1.all_set() == true);
|
||||||
|
|
||||||
|
// test merkle_*() functions
|
||||||
|
|
||||||
|
// this is the structure:
|
||||||
|
// 0
|
||||||
|
// 1 2
|
||||||
|
// 3 4 5 6
|
||||||
|
// 7 8 9 10 11 12 13 14
|
||||||
|
// num_leafs = 8
|
||||||
|
|
||||||
|
TEST_EQUAL(merkle_num_leafs(1), 1);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(2), 2);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(3), 4);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(4), 4);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(5), 8);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(6), 8);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(7), 8);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(8), 8);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(9), 16);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(10), 16);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(11), 16);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(12), 16);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(13), 16);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(14), 16);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(15), 16);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(16), 16);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(17), 32);
|
||||||
|
TEST_EQUAL(merkle_num_leafs(18), 32);
|
||||||
|
|
||||||
|
// parents
|
||||||
|
TEST_EQUAL(merkle_get_parent(1), 0);
|
||||||
|
TEST_EQUAL(merkle_get_parent(2), 0);
|
||||||
|
TEST_EQUAL(merkle_get_parent(3), 1);
|
||||||
|
TEST_EQUAL(merkle_get_parent(4), 1);
|
||||||
|
TEST_EQUAL(merkle_get_parent(5), 2);
|
||||||
|
TEST_EQUAL(merkle_get_parent(6), 2);
|
||||||
|
TEST_EQUAL(merkle_get_parent(7), 3);
|
||||||
|
TEST_EQUAL(merkle_get_parent(8), 3);
|
||||||
|
TEST_EQUAL(merkle_get_parent(9), 4);
|
||||||
|
TEST_EQUAL(merkle_get_parent(10), 4);
|
||||||
|
TEST_EQUAL(merkle_get_parent(11), 5);
|
||||||
|
TEST_EQUAL(merkle_get_parent(12), 5);
|
||||||
|
TEST_EQUAL(merkle_get_parent(13), 6);
|
||||||
|
TEST_EQUAL(merkle_get_parent(14), 6);
|
||||||
|
|
||||||
|
// siblings
|
||||||
|
TEST_EQUAL(merkle_get_sibling(1), 2);
|
||||||
|
TEST_EQUAL(merkle_get_sibling(2), 1);
|
||||||
|
TEST_EQUAL(merkle_get_sibling(3), 4);
|
||||||
|
TEST_EQUAL(merkle_get_sibling(4), 3);
|
||||||
|
TEST_EQUAL(merkle_get_sibling(5), 6);
|
||||||
|
TEST_EQUAL(merkle_get_sibling(6), 5);
|
||||||
|
TEST_EQUAL(merkle_get_sibling(7), 8);
|
||||||
|
TEST_EQUAL(merkle_get_sibling(8), 7);
|
||||||
|
TEST_EQUAL(merkle_get_sibling(9), 10);
|
||||||
|
TEST_EQUAL(merkle_get_sibling(10), 9);
|
||||||
|
TEST_EQUAL(merkle_get_sibling(11), 12);
|
||||||
|
TEST_EQUAL(merkle_get_sibling(12), 11);
|
||||||
|
TEST_EQUAL(merkle_get_sibling(13), 14);
|
||||||
|
TEST_EQUAL(merkle_get_sibling(14), 13);
|
||||||
|
|
||||||
|
// total number of nodes given the number of leafs
|
||||||
|
TEST_EQUAL(merkle_num_nodes(1), 1);
|
||||||
|
TEST_EQUAL(merkle_num_nodes(2), 3);
|
||||||
|
TEST_EQUAL(merkle_num_nodes(4), 7);
|
||||||
|
TEST_EQUAL(merkle_num_nodes(8), 15);
|
||||||
|
TEST_EQUAL(merkle_num_nodes(16), 31);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user