added some precautionary checks in bdecoder
[libtorrent.git] / include / libtorrent / torrent_info.hpp
bloba027e3460924f333329cbfd3b3abd6e5b05c266f
1 /*
3 Copyright (c) 2003-2008, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #ifndef TORRENT_TORRENT_INFO_HPP_INCLUDED
34 #define TORRENT_TORRENT_INFO_HPP_INCLUDED
36 #include <string>
37 #include <vector>
38 #include <iosfwd>
40 #ifdef _MSC_VER
41 #pragma warning(push, 1)
42 #endif
44 #include <boost/optional.hpp>
45 #include <boost/filesystem/path.hpp>
46 #include <boost/shared_array.hpp>
48 #ifdef _MSC_VER
49 #pragma warning(pop)
50 #endif
52 #include "libtorrent/entry.hpp"
53 #include "libtorrent/lazy_entry.hpp"
54 #include "libtorrent/socket.hpp"
55 #include "libtorrent/peer_id.hpp"
56 #include "libtorrent/size_type.hpp"
57 #include "libtorrent/config.hpp"
58 #include "libtorrent/time.hpp"
59 #include "libtorrent/intrusive_ptr_base.hpp"
60 #include "libtorrent/assert.hpp"
61 #include "libtorrent/file_storage.hpp"
63 namespace libtorrent
65 namespace pt = boost::posix_time;
66 namespace gr = boost::gregorian;
67 namespace fs = boost::filesystem;
69 struct TORRENT_EXPORT announce_entry
71 announce_entry(std::string const& u): url(u), tier(0) {}
72 std::string url;
73 int tier;
76 #ifndef BOOST_NO_EXCEPTIONS
77 struct TORRENT_EXPORT invalid_torrent_file: std::exception
79 virtual const char* what() const throw() { return "invalid torrent file"; }
81 #endif
83 int TORRENT_EXPORT load_file(fs::path const& filename, std::vector<char>& v);
85 class TORRENT_EXPORT torrent_info : public intrusive_ptr_base<torrent_info>
87 public:
89 torrent_info(sha1_hash const& info_hash);
90 torrent_info(lazy_entry const& torrent_file);
91 torrent_info(char const* buffer, int size);
92 torrent_info(fs::path const& filename);
93 ~torrent_info();
95 file_storage const& files() const { return m_files; }
97 void add_tracker(std::string const& url, int tier = 0);
98 std::vector<announce_entry> const& trackers() const { return m_urls; }
100 std::vector<std::string> const& url_seeds() const
101 { return m_url_seeds; }
102 void add_url_seed(std::string const& url)
103 { m_url_seeds.push_back(url); }
105 size_type total_size() const { return m_files.total_size(); }
106 int piece_length() const { return m_files.piece_length(); }
107 int num_pieces() const { return m_files.num_pieces(); }
108 const sha1_hash& info_hash() const { return m_info_hash; }
109 const std::string& name() const { return m_files.name(); }
111 typedef file_storage::iterator file_iterator;
112 typedef file_storage::reverse_iterator reverse_file_iterator;
114 file_iterator begin_files() const { return m_files.begin(); }
115 file_iterator end_files() const { return m_files.end(); }
116 reverse_file_iterator rbegin_files() const { return m_files.rbegin(); }
117 reverse_file_iterator rend_files() const { return m_files.rend(); }
118 int num_files() const { return m_files.num_files(); }
119 file_entry const& file_at(int index) const { return m_files.at(index); }
121 file_iterator file_at_offset(size_type offset) const
122 { return m_files.file_at_offset(offset); }
123 std::vector<file_slice> map_block(int piece, size_type offset, int size) const
124 { return m_files.map_block(piece, offset, size); }
125 peer_request map_file(int file, size_type offset, int size) const
126 { return m_files.map_file(file, offset, size); }
128 #ifndef TORRENT_NO_DEPRECATE
129 // ------- start deprecation -------
130 // these functions will be removed in a future version
131 torrent_info(entry const& torrent_file) TORRENT_DEPRECATED;
132 void print(std::ostream& os) const TORRENT_DEPRECATED;
133 // ------- end deprecation -------
134 #endif
136 bool is_valid() const { return m_files.is_valid(); }
138 bool priv() const { return m_private; }
140 int piece_size(int index) const { return m_files.piece_size(index); }
142 sha1_hash hash_for_piece(int index) const
143 { return sha1_hash(hash_for_piece_ptr(index)); }
145 char const* hash_for_piece_ptr(int index) const
147 TORRENT_ASSERT(index >= 0);
148 TORRENT_ASSERT(index < m_files.num_pieces());
149 TORRENT_ASSERT(m_piece_hashes);
150 TORRENT_ASSERT(m_piece_hashes >= m_info_section.get());
151 TORRENT_ASSERT(m_piece_hashes < m_info_section.get() + m_info_section_size);
152 return &m_piece_hashes[index*20];
155 boost::optional<pt::ptime> creation_date() const;
157 const std::string& creator() const
158 { return m_created_by; }
160 const std::string& comment() const
161 { return m_comment; }
163 // dht nodes to add to the routing table/bootstrap from
164 typedef std::vector<std::pair<std::string, int> > nodes_t;
166 nodes_t const& nodes() const
167 { return m_nodes; }
168 void add_node(std::pair<std::string, int> const& node)
169 { m_nodes.push_back(node); }
171 bool parse_info_section(lazy_entry const& e, std::string& error);
173 lazy_entry const* info(char const* key) const
175 if (m_info_dict.type() == lazy_entry::none_t)
176 lazy_bdecode(m_info_section.get(), m_info_section.get()
177 + m_info_section_size, m_info_dict);
178 return m_info_dict.dict_find(key);
181 void swap(torrent_info& ti);
183 boost::shared_array<char> metadata() const
184 { return m_info_section; }
186 int metadata_size() const { return m_info_section_size; }
188 private:
190 bool parse_torrent_file(lazy_entry const& libtorrent, std::string& error);
192 file_storage m_files;
194 // the urls to the trackers
195 std::vector<announce_entry> m_urls;
196 std::vector<std::string> m_url_seeds;
197 nodes_t m_nodes;
199 // the hash that identifies this torrent
200 // is mutable because it's calculated
201 // lazily
202 sha1_hash m_info_hash;
204 // if a creation date is found in the torrent file
205 // this will be set to that, otherwise it'll be
206 // 1970, Jan 1
207 pt::ptime m_creation_date;
209 // if a comment is found in the torrent file
210 // this will be set to that comment
211 std::string m_comment;
213 // an optional string naming the software used
214 // to create the torrent file
215 std::string m_created_by;
217 // this is used when creating a torrent. If there's
218 // only one file there are cases where it's impossible
219 // to know if it should be written as a multifile torrent
220 // or not. e.g. test/test there's one file and one directory
221 // and they have the same name.
222 bool m_multifile;
224 // this is true if the torrent is private. i.e., is should not
225 // be announced on the dht
226 bool m_private;
228 // this is a copy of the info section from the torrent.
229 // it use maintained in this flat format in order to
230 // make it available through the metadata extension
231 boost::shared_array<char> m_info_section;
232 int m_info_section_size;
234 // this is a pointer into the m_info_section buffer
235 // pointing to the first byte of the first sha-1 hash
236 char const* m_piece_hashes;
238 // the info section parsed. points into m_info_section
239 // parsed lazily
240 mutable lazy_entry m_info_dict;
245 #endif // TORRENT_TORRENT_INFO_HPP_INCLUDED