// check if the fastresume data is up to date // if it is, use it and return true. If it // isn't return false and the full check // will be run int piece_manager::check_fastresume( lazy_entry const& rd, error_code& error) { mutex::scoped_lock lock(m_mutex); INVARIANT_CHECK; TORRENT_ASSERT(m_files.piece_length() > 0); m_current_slot = 0; // if we don't have any resume data, return if (rd.type() == lazy_entry::none_t) return check_no_fastresume(error); if (rd.type() != lazy_entry::dict_t) { error = errors::not_a_dictionary; return check_no_fastresume(error); } int block_size = (std::min)(16 * 1024, m_files.piece_length()); int blocks_per_piece = int(rd.dict_find_int_value("blocks per piece", -1)); if (blocks_per_piece != -1 && blocks_per_piece != m_files.piece_length() / block_size) { error = errors::invalid_blocks_per_piece; return check_no_fastresume(error); } if (!m_storage->verify_resume_data(rd, error)) return check_no_fastresume(error); return check_init_storage(error); }
bool uTorrentParser::parse_torrent_file(lazy_entry const& torrent_file, error_code& ec) { if (torrent_file.type() != lazy_entry::dict_t) { ec = errors::torrent_is_no_dict; return false; } mTorrents.clear(); for (int i = 0, end(torrent_file.dict_size()); i < end; ++i) { std::pair<std::string, lazy_entry const*> n = torrent_file.dict_at(i); if (lazy_entry::dict_t == n.second->type()) { std::pair<std::string, std::string> torrent; torrent.first = n.first; torrent.second = n.second->dict_find_string_value("path"); std::string caption = n.second->dict_find_string_value("caption"); if (caption.length() > 0) { size_t pos = torrent.second.find(caption.c_str(), 0, caption.length()); torrent.second = torrent.second.substr(0, pos); } mTorrents.push_back(torrent); } } return true; }
// convert a lazy_entry into an old skool entry void entry::operator=(lazy_entry const& e) { switch (e.type()) { case lazy_entry::string_t: this->string() = e.string_value(); break; case lazy_entry::int_t: this->integer() = e.int_value(); break; case lazy_entry::dict_t: { dictionary_type& d = this->dict(); for (int i = 0; i < e.dict_size(); ++i) { std::pair<std::string, lazy_entry const*> elem = e.dict_at(i); d[elem.first] = *elem.second; } break; } case lazy_entry::list_t: { list_type& l = this->list(); for (int i = 0; i < e.list_size(); ++i) { l.push_back(entry()); l.back() = *e.list_at(i); } break; } case lazy_entry::none_t: destruct(); break; } }
int line_longer_than(lazy_entry const& e, int limit) { int line_len = 0; switch (e.type()) { case lazy_entry::list_t: line_len += 4; if (line_len > limit) return -1; for (int i = 0; i < e.list_size(); ++i) { int ret = line_longer_than(*e.list_at(i), limit - line_len); if (ret == -1) return -1; line_len += ret + 2; } break; case lazy_entry::dict_t: line_len += 4; if (line_len > limit) return -1; for (int i = 0; i < e.dict_size(); ++i) { line_len += 4 + e.dict_at(i).first.size(); if (line_len > limit) return -1; int ret = line_longer_than(*e.dict_at(i).second, limit - line_len); if (ret == -1) return -1; line_len += ret + 1; } break; case lazy_entry::string_t: line_len += 3 + e.string_length(); break; case lazy_entry::int_t: { size_type val = e.int_value(); while (val > 0) { ++line_len; val /= 10; } line_len += 2; } break; case lazy_entry::none_t: line_len += 4; break; } if (line_len > limit) return -1; return line_len; }
void session_handle::load_state(lazy_entry const& ses_state , std::uint32_t const flags) { if (ses_state.type() == lazy_entry::none_t) return; std::pair<char const*, int> buf = ses_state.data_section(); bdecode_node e; error_code ec; #if TORRENT_USE_ASSERTS || !defined BOOST_NO_EXCEPTIONS int ret = #endif bdecode(buf.first, buf.first + buf.second, e, ec); TORRENT_ASSERT(ret == 0); #ifndef BOOST_NO_EXCEPTIONS if (ret != 0) throw system_error(ec); #endif sync_call(&session_impl::load_state, &e, flags); }
bool http_tracker_connection::extract_peer_info(lazy_entry const& info, peer_entry& ret) { // extract peer id (if any) if (info.type() != lazy_entry::dict_t) { fail(error_code(errors::invalid_peer_dict)); return false; } lazy_entry const* i = info.dict_find_string("peer id"); if (i != 0 && i->string_length() == 20) { std::copy(i->string_ptr(), i->string_ptr()+20, ret.pid.begin()); } else { // if there's no peer_id, just initialize it to a bunch of zeroes std::fill_n(ret.pid.begin(), 20, 0); } // extract ip i = info.dict_find_string("ip"); if (i == 0) { fail(error_code(errors::invalid_tracker_response)); return false; } ret.ip = i->string_value(); // extract port i = info.dict_find_int("port"); if (i == 0) { fail(error_code(errors::invalid_tracker_response)); return false; } ret.port = (unsigned short)i->int_value(); return true; }
std::string print_entry(lazy_entry const& e, bool single_line, int indent) { char indent_str[200]; memset(indent_str, ' ', 200); indent_str[0] = ','; indent_str[1] = '\n'; indent_str[199] = 0; if (indent < 197 && indent >= 0) indent_str[indent+2] = 0; std::string ret; switch (e.type()) { case lazy_entry::none_t: return "none"; case lazy_entry::int_t: { char str[100]; snprintf(str, sizeof(str), "%"PRId64, e.int_value()); return str; } case lazy_entry::string_t: { bool printable = true; char const* str = e.string_ptr(); for (int i = 0; i < e.string_length(); ++i) { using namespace std; if (is_print((unsigned char)str[i])) continue; printable = false; break; } ret += "'"; if (printable) { ret += e.string_value(); ret += "'"; return ret; } for (int i = 0; i < e.string_length(); ++i) { char tmp[5]; snprintf(tmp, sizeof(tmp), "%02x", (unsigned char)str[i]); ret += tmp; } ret += "'"; return ret; } case lazy_entry::list_t: { ret += '['; bool one_liner = line_longer_than(e, 200) != -1 || single_line; if (!one_liner) ret += indent_str + 1; for (int i = 0; i < e.list_size(); ++i) { if (i == 0 && one_liner) ret += " "; ret += print_entry(*e.list_at(i), single_line, indent + 2); if (i < e.list_size() - 1) ret += (one_liner?", ":indent_str); else ret += (one_liner?" ":indent_str+1); } ret += "]"; return ret; } case lazy_entry::dict_t: { ret += "{"; bool one_liner = line_longer_than(e, 200) != -1 || single_line; if (!one_liner) ret += indent_str+1; for (int i = 0; i < e.dict_size(); ++i) { if (i == 0 && one_liner) ret += " "; std::pair<std::string, lazy_entry const*> ent = e.dict_at(i); ret += "'"; ret += ent.first; ret += "': "; ret += print_entry(*ent.second, single_line, indent + 2); if (i < e.dict_size() - 1) ret += (one_liner?", ":indent_str); else ret += (one_liner?" ":indent_str+1); } ret += "}"; return ret; } } return ret; }
std::string print_entry(lazy_entry const& e, bool single_line, int indent) { char indent_str[200]; memset(indent_str, ' ', 200); indent_str[0] = ','; indent_str[1] = '\n'; indent_str[199] = 0; if (indent < 197 && indent >= 0) indent_str[indent+2] = 0; std::string ret; switch (e.type()) { case lazy_entry::none_t: return "none"; case lazy_entry::int_t: { char str[100]; std::snprintf(str, sizeof(str), "%" PRId64, e.int_value()); return str; } case lazy_entry::string_t: { print_string(ret, e.string_ptr(), e.string_length(), single_line); return ret; } case lazy_entry::list_t: { ret += '['; bool one_liner = line_longer_than(e, 200) != -1 || single_line; if (!one_liner) ret += indent_str + 1; for (int i = 0; i < e.list_size(); ++i) { if (i == 0 && one_liner) ret += " "; ret += print_entry(*e.list_at(i), single_line, indent + 2); if (i < e.list_size() - 1) ret += (one_liner?", ":indent_str); else ret += (one_liner?" ":indent_str+1); } ret += "]"; return ret; } case lazy_entry::dict_t: { ret += "{"; bool one_liner = line_longer_than(e, 200) != -1 || single_line; if (!one_liner) ret += indent_str+1; for (int i = 0; i < e.dict_size(); ++i) { if (i == 0 && one_liner) ret += " "; std::pair<std::string, lazy_entry const*> ent = e.dict_at(i); print_string(ret, ent.first.c_str(), int(ent.first.size()), true); ret += ": "; ret += print_entry(*ent.second, single_line, indent + 2); if (i < e.dict_size() - 1) ret += (one_liner?", ":indent_str); else ret += (one_liner?" ":indent_str+1); } ret += "}"; return ret; } } return ret; }
std::string print_entry(lazy_entry const& e) { std::string ret; switch (e.type()) { case lazy_entry::none_t: return "none"; case lazy_entry::int_t: { char str[100]; snprintf(str, sizeof(str), "%"PRId64, e.int_value()); return str; } case lazy_entry::string_t: { bool printable = true; char const* str = e.string_ptr(); for (int i = 0; i < e.string_length(); ++i) { using namespace std; if (is_print((unsigned char)str[i])) continue; printable = false; break; } ret += "'"; if (printable) { ret += e.string_value(); ret += "'"; return ret; } for (int i = 0; i < e.string_length(); ++i) { char tmp[5]; snprintf(tmp, sizeof(tmp), "%02x", (unsigned char)str[i]); ret += tmp; } ret += "'"; return ret; } case lazy_entry::list_t: { ret += '['; bool one_liner = (e.list_size() == 0 || (e.list_at(0)->type() == lazy_entry::int_t && e.list_size() < 20) || (e.list_at(0)->type() == lazy_entry::string_t && (e.list_at(0)->string_length() < 10 || e.list_size() < 2) && e.list_size() < 5)); if (!one_liner) ret += "\n"; for (int i = 0; i < e.list_size(); ++i) { if (i == 0 && one_liner) ret += " "; ret += print_entry(*e.list_at(i)); if (i < e.list_size() - 1) ret += (one_liner?", ":",\n"); else ret += (one_liner?" ":"\n"); } ret += "]"; return ret; } case lazy_entry::dict_t: { ret += "{"; bool one_liner = (e.dict_size() == 0 || e.dict_at(0).second->type() == lazy_entry::int_t || (e.dict_at(0).second->type() == lazy_entry::string_t && e.dict_at(0).second->string_length() < 30) || e.dict_at(0).first.size() < 10) && e.dict_size() < 5; if (!one_liner) ret += "\n"; for (int i = 0; i < e.dict_size(); ++i) { if (i == 0 && one_liner) ret += " "; std::pair<std::string, lazy_entry const*> ent = e.dict_at(i); ret += "'"; ret += ent.first; ret += "': "; ret += print_entry(*ent.second); if (i < e.dict_size() - 1) ret += (one_liner?", ":",\n"); else ret += (one_liner?" ":"\n"); } ret += "}"; return ret; } } return ret; }