From facbf9c178e1e237b0443aad79f5e0286a71c188 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 19 Jul 2013 21:41:33 +0000 Subject: [PATCH] don't expose load_file as a public function --- examples/client_test.cpp | 61 +++++++++++++++++++++++++++++ examples/dump_torrent.cpp | 61 +++++++++++++++++++++++++++++ examples/make_torrent.cpp | 61 +++++++++++++++++++++++++++++ examples/rss_reader.cpp | 61 +++++++++++++++++++++++++++++ include/libtorrent/lazy_entry.hpp | 6 +-- include/libtorrent/torrent_info.hpp | 4 -- src/torrent_info.cpp | 2 +- test/setup_transfer.cpp | 61 +++++++++++++++++++++++++++++ test/setup_transfer.hpp | 2 + test/test_http_connection.cpp | 1 + test/test_rss.cpp | 1 + 11 files changed, 312 insertions(+), 9 deletions(-) diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 90d548a90..22eb53fa5 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -192,6 +192,67 @@ bool print_send_bufs = true; // without having received a response (successful or failure) int num_outstanding_resume_data = 0; +int load_file(std::string const& filename, std::vector& v, libtorrent::error_code& ec, int limit = 8000000) +{ + ec.clear(); + FILE* f = fopen(filename.c_str(), "rb"); + if (f == NULL) + { + ec.assign(errno, boost::system::get_generic_category()); + return -1; + } + + int r = fseek(f, 0, SEEK_END); + if (r != 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + long s = ftell(f); + if (s < 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + if (s > limit) + { + fclose(f); + return -2; + } + + r = fseek(f, 0, SEEK_SET); + if (r != 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + v.resize(s); + if (s == 0) + { + fclose(f); + return 0; + } + + r = fread(&v[0], 1, v.size(), f); + if (r < 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + fclose(f); + + if (r != s) return -3; + + return 0; +} + enum { torrents_all, torrents_downloading, diff --git a/examples/dump_torrent.cpp b/examples/dump_torrent.cpp index f89e49c44..6b57d7303 100644 --- a/examples/dump_torrent.cpp +++ b/examples/dump_torrent.cpp @@ -36,6 +36,67 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/lazy_entry.hpp" #include "libtorrent/magnet_uri.hpp" +int load_file(std::string const& filename, std::vector& v, libtorrent::error_code& ec, int limit = 8000000) +{ + ec.clear(); + FILE* f = fopen(filename.c_str(), "rb"); + if (f == NULL) + { + ec.assign(errno, boost::system::get_generic_category()); + return -1; + } + + int r = fseek(f, 0, SEEK_END); + if (r != 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + long s = ftell(f); + if (s < 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + if (s > limit) + { + fclose(f); + return -2; + } + + r = fseek(f, 0, SEEK_SET); + if (r != 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + v.resize(s); + if (s == 0) + { + fclose(f); + return 0; + } + + r = fread(&v[0], 1, v.size(), f); + if (r < 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + fclose(f); + + if (r != s) return -3; + + return 0; +} + int main(int argc, char* argv[]) { using namespace libtorrent; diff --git a/examples/make_torrent.cpp b/examples/make_torrent.cpp index 01f35bc47..45b32fda4 100644 --- a/examples/make_torrent.cpp +++ b/examples/make_torrent.cpp @@ -44,6 +44,67 @@ POSSIBILITY OF SUCH DAMAGE. using namespace libtorrent; +int load_file(std::string const& filename, std::vector& v, libtorrent::error_code& ec, int limit = 8000000) +{ + ec.clear(); + FILE* f = fopen(filename.c_str(), "rb"); + if (f == NULL) + { + ec.assign(errno, boost::system::get_generic_category()); + return -1; + } + + int r = fseek(f, 0, SEEK_END); + if (r != 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + long s = ftell(f); + if (s < 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + if (s > limit) + { + fclose(f); + return -2; + } + + r = fseek(f, 0, SEEK_SET); + if (r != 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + v.resize(s); + if (s == 0) + { + fclose(f); + return 0; + } + + r = fread(&v[0], 1, v.size(), f); + if (r < 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + fclose(f); + + if (r != s) return -3; + + return 0; +} + // do not include files and folders whose // name starts with a . bool file_filter(std::string const& f) diff --git a/examples/rss_reader.cpp b/examples/rss_reader.cpp index ee7b32c28..ffe3aa3f9 100644 --- a/examples/rss_reader.cpp +++ b/examples/rss_reader.cpp @@ -39,6 +39,67 @@ POSSIBILITY OF SUCH DAMAGE. using namespace libtorrent; +int load_file(std::string const& filename, std::vector& v, libtorrent::error_code& ec, int limit = 8000000) +{ + ec.clear(); + FILE* f = fopen(filename.c_str(), "rb"); + if (f == NULL) + { + ec.assign(errno, boost::system::get_generic_category()); + return -1; + } + + int r = fseek(f, 0, SEEK_END); + if (r != 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + long s = ftell(f); + if (s < 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + if (s > limit) + { + fclose(f); + return -2; + } + + r = fseek(f, 0, SEEK_SET); + if (r != 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + v.resize(s); + if (s == 0) + { + fclose(f); + return 0; + } + + r = fread(&v[0], 1, v.size(), f); + if (r < 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + fclose(f); + + if (r != s) return -3; + + return 0; +} + void print_feed(feed_status const& f) { printf("FEED: %s\n",f.url.c_str()); diff --git a/include/libtorrent/lazy_entry.hpp b/include/libtorrent/lazy_entry.hpp index 53f8c4b45..99f4f37f0 100644 --- a/include/libtorrent/lazy_entry.hpp +++ b/include/libtorrent/lazy_entry.hpp @@ -50,9 +50,6 @@ namespace libtorrent { struct lazy_entry; - TORRENT_EXPORT char const* parse_int(char const* start, char const* end - , char delimiter, boost::int64_t& val); - // return 0 = success TORRENT_EXPORT int lazy_bdecode(char const* start, char const* end , lazy_entry& ret, error_code& ec, int* error_pos = 0 @@ -66,7 +63,7 @@ namespace libtorrent , lazy_entry& ret, int depth_limit = 1000, int item_limit = 1000000) TORRENT_DEPRECATED; #endif - struct pascal_string + struct TORRENT_EXPORT pascal_string { pascal_string(char const* p, int l): len(l), ptr(p) {} int len; @@ -282,6 +279,7 @@ namespace libtorrent lazy_entry val; }; + // TODO: 3 does this need to be a public function? TORRENT_EXPORT std::string print_entry(lazy_entry const& e , bool single_line = false, int indent = 0); } diff --git a/include/libtorrent/torrent_info.hpp b/include/libtorrent/torrent_info.hpp index 232b9786a..deacb69d3 100644 --- a/include/libtorrent/torrent_info.hpp +++ b/include/libtorrent/torrent_info.hpp @@ -235,10 +235,6 @@ namespace libtorrent typedef libtorrent_exception invalid_torrent_file; #endif - // TODO: 3 should this really be a public symbol? - int TORRENT_EXPORT load_file(std::string const& filename - , std::vector& v, error_code& ec, int limit = 8000000); - class TORRENT_EXPORT torrent_info : public intrusive_ptr_base { public: diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index e6b7947b2..7d35e2e86 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -457,7 +457,7 @@ namespace libtorrent return ret; } - int load_file(std::string const& filename, std::vector& v, error_code& ec, int limit) + int load_file(std::string const& filename, std::vector& v, error_code& ec, int limit = 8000000) { ec.clear(); file f; diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index d2d41d260..846c57799 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -95,6 +95,67 @@ std::auto_ptr wait_for_alert(session& ses, int type) return ret; } +int load_file(std::string const& filename, std::vector& v, libtorrent::error_code& ec, int limit) +{ + ec.clear(); + FILE* f = fopen(filename.c_str(), "rb"); + if (f == NULL) + { + ec.assign(errno, boost::system::get_generic_category()); + return -1; + } + + int r = fseek(f, 0, SEEK_END); + if (r != 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + long s = ftell(f); + if (s < 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + if (s > limit) + { + fclose(f); + return -2; + } + + r = fseek(f, 0, SEEK_SET); + if (r != 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + v.resize(s); + if (s == 0) + { + fclose(f); + return 0; + } + + r = fread(&v[0], 1, v.size(), f); + if (r < 0) + { + ec.assign(errno, boost::system::get_generic_category()); + fclose(f); + return -1; + } + + fclose(f); + + if (r != s) return -3; + + return 0; +} + bool print_alerts(libtorrent::session& ses, char const* name , bool allow_disconnects, bool allow_no_torrents, bool allow_failed_fastresume , bool (*predicate)(libtorrent::alert*), bool no_output) diff --git a/test/setup_transfer.hpp b/test/setup_transfer.hpp index 2396bb456..5e701a2d4 100644 --- a/test/setup_transfer.hpp +++ b/test/setup_transfer.hpp @@ -46,6 +46,8 @@ namespace libtorrent extern EXPORT bool tests_failure; +int EXPORT load_file(std::string const& filename, std::vector& v, libtorrent::error_code& ec, int limit = 8000000); + void EXPORT report_failure(char const* err, char const* file, int line); std::auto_ptr EXPORT wait_for_alert(libtorrent::session& ses, int type); diff --git a/test/test_http_connection.cpp b/test/test_http_connection.cpp index f9322077e..8e1f10d2b 100644 --- a/test/test_http_connection.cpp +++ b/test/test_http_connection.cpp @@ -126,6 +126,7 @@ void run_test(std::string const& url, int size, int status, int connected std::cerr << "handler_called: " << handler_called << std::endl; std::cerr << "status: " << http_status << std::endl; std::cerr << "size: " << data_size << std::endl; + std::cerr << "expected-size: " << size << std::endl; std::cerr << "error_code: " << g_error_code.message() << std::endl; TEST_CHECK(connect_handler_called == connected); TEST_CHECK(handler_called == 1); diff --git a/test/test_rss.cpp b/test/test_rss.cpp index c90ef1453..7376a90c5 100644 --- a/test/test_rss.cpp +++ b/test/test_rss.cpp @@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/http_parser.hpp" #include "test.hpp" +#include "setup_transfer.hpp" // for load_file using namespace libtorrent;