Initial commit of 'new' libtorrent.

The Jamfile is broken and will hopefully be fixed soon.
This commit is contained in:
Arvid Norberg 2003-10-22 23:00:57 +00:00
commit ec34b1bfdb
49 changed files with 14134 additions and 0 deletions

38
Jamfile Executable file
View File

@ -0,0 +1,38 @@
SOURCES =
entry.cpp
peer_connection.cpp
piece_picker.cpp
policy.cpp
session.cpp
# socket_bsd.cpp
socket_win.cpp
stat.cpp
storage.cpp
torrent.cpp
torrent_info.cpp
url_handler.cpp
sha1.c
;
lib torrent
: src/$(SOURCES)
: <include>$(BOOST_ROOT)
<sysinclude>$(BOOST_ROOT)
<include>./include
<include>./
<threading>multi
<link>static
: debug release
;
#exe torrent_test
# : src/main.cpp <lib>torrent
# $(BOOST_ROOT)/libs/filesystem/build//boost_filesystem
# : <include>$(BOOST_ROOT)
# <sysinclude>$(BOOST_ROOT)
# <include>./
# <threading>multi
# : debug release
# ;

597
docs/index.html Executable file
View File

@ -0,0 +1,597 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>libtorrent</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1>libtorrent</h1>
<p>
libtorrent is a C++ library that aims to be a good alternative to all the
<a href="links.html">other bittorrent implementations</a> around. It is a
library and not a full featured client, although it comes with a working
example client.
</p>
<p>
The main goals of libtorrent are:
</p>
<ul>
<li>to be cpu efficient
<li>to be memory efficient
<li>to be very easy to use
</ul>
<p>
libtorrent is not finished. It is an ongoing project (including this documentation).
The current state includes the following features:
</p>
<ul>
<li>multitracker extension support
(as <a href="http://home.elp.rr.com/tur/multitracker-spec.txt">described by TheShadow</a>)
<li>serves multiple torrents on a single port and a single thread
<li>supports http proxies and proxy authentication
<li>gzipped tracker-responses
<li>piece picking on block-level (as opposed to piece-level) like in
<a href="http://azureus.sourceforge.net/">Azureus</a>
</ul>
<p>
libtorrent is portable at least among windows, macosx, and UNIX-systems. It uses boost.thread,
boost.filesystem and various other boost libraries and zlib.
</p>
<h1>building</h1>
<p>
To build libtorrent you need <a href="http://www.boost.org">boost</a> and bjam installed.
Then you can use <tt>bjam</tt> to build libtorrent.
</p>
<p>
To make bjam work, you need to set the environment variable <tt>BOOST_ROOT</tt> to the
path where boost is installed (e.g. c:\boost_1_30_2 on windows). Then you can just run
<tt>bjam</tt> in the libtorrent directory.
</p>
<p>
TODO: more detailed build instructions.
</p>
<h1>using</h1>
<p>
The interface of libtorrent consists of a few classes. The main class is
the <tt>session</tt>, it contains the main loop that serves all torrents.
</p>
<h2>session</h2>
<p>
The <tt>session</tt> class has the following synopsis:
</p>
<pre>
class session: public boost::noncopyable
{
session(int listen_port);
torrent_handle add_torrent(const torrent_info&amp; t, const std::string&amp; save_path);
void set_http_settings(const http_settings&amp; settings);
};
</pre>
<p>
Once it's created, it will spawn the main thread that will do all the work.
The main thread will be idle as long it doesn't have any torrents to participate in.
You add torrents through the <tt>add_torrent()</tt>-function where you give an
object representing the information found in the torrent file and the path where you
want to save the files. The <tt>save_path</tt> will be prepended to the directory-
structure in the torrent-file.
</p>
<p>
How to parse a torrent file and create a <tt>torrent_info</tt> object is described below.
</p>
<p>
The <a href="#torrent_handle"><tt>torrent_handle</tt></a> returned by <tt>add_torrent</tt>
can be used to retrieve information about the torrent's progress, its peers etc. It
is also used to abort a torrent.
</p>
<p>
The constructor takes a listen port as argument, if the given port is busy it will
increase the port number by one and try again. If it still fails it will continue
increasing the port number until it succeeds or has failed 9 ports. <i>This will
change in the future to give more control of the listen-port.</i>
</p>
<h2>parsing torrent files</h2>
<p>
The torrent files are <a href="http://bitconjurer.org/BitTorrent/protocol.html">
bencoded</a>. There are two functions in libtorrent that can encode and decode
bencoded data. They are:
</p>
<h3>
template&lt;class InIt&gt;
entry bdecode(InIt start, InIt end);<br>
template&lt;class OutIt&gt;
void bencode(OutIt out, const entry&amp; e);
</h3>
<p>
The <tt>entry</tt> class is the internal representation of the bencoded data
and it can be used to retreive information, an entry can also be build by
the program and given to <tt>bencode()</tt> to encode it into the <tt>OutIt</tt>
iterator.
</p>
<p>
The <tt>OutIt</tt> and <tt>InIt</tt> are iterators
(<a href="http://www.sgi.com/tech/stl/InputIterator.html"><tt>InputIterator</tt></a>
and <a href="http://www.sgi.com/tech/stl/OutputIterator.html"><tt>OutputIterator</tt></a>
respectively). They are templates and are usually instantiated as
<a href="http://www.sgi.com/tech/stl/ostream_iterator.html">
<tt>std::ostream_iterator</tt></a>,
<a href="http://www.sgi.com/tech/stl/back_insert_iterator.html">
<tt>std::back_insert_iterator</tt></a> or
<a href="http://www.sgi.com/tech/stl/istream_iterator.html">
<tt>std::istream_iterator</tt></a>. These functions will assume that the iterator
refers to a character (<tt>char</tt>). So, if you want to encode entry <tt>e</tt>
into a buffer in memory, you can do it like this:
</p>
<code>
std::vector&lt;char&gt; buffer;
bencode(std::back_insert_iterator&lt;std::vector&lt;char&gt; &gt;(buf), e);
</code>
<p>
If you want to decode a torrent file from a buffer in memory, you can do it like this:
</p>
<code>
std::vector&lt;char&gt; buffer;
// ...
entry e = bdecode(buf.begin(), buf.end());
</code>
<p>
Or, if you have a raw char buffer:
</p>
<code>
const char* buf;
// ...
entry e = bdecode(buf, buf + data_size);
</code>
<p>
Now we just need to know how to retrieve information from the <tt>entry</tt>.
</p>
<h2>entry</h2>
<p>
The <tt>entry</tt> class represents one node in a bencoded hierarchy. It works as a
variant type, it can be either a list, a dictionary (<tt>std::map</tt>), an integer
or a string. This is its synopsis:
</p>
<pre>
class entry
{
public:
typedef std::map&lt;std::string, entry&gt; dictionary_type;
typedef std::string string_type;
typedef std::vector&lt;entry&gt; list_type;
typedef <i>implementation-defined</i> integer_type;
enum data_type
{
int_t,
string_t,
list_t,
dictionary_t,
undefined_t
};
data_type type() const;
entry();
entry(data_type t);
entry(const entry&amp; e);
void operator=(const entry&amp; e);
integer_type&amp; integer()
const integer_type&amp; integer() const;
string_type&amp; string();
const string_type&amp; string() const;
list_type&amp; list();
const list_type&amp; list() const;
dictionary_type&amp; dict();
const dictionary_type&amp; dict() const;
void print(std::ostream&amp; os, int indent) const;
};
</pre>
<p>
The <tt>integer()</tt>, <tt>string()</tt>, <tt>list()</tt> and <tt>dict()</tt> functions
are accessorts that return the respecive type. If the <tt>entry</tt> object isn't of the
type you request, the accessor will throw <tt>type_error</tt> (which derives from
<tt>std::runtime_error</tt>). You can ask an <tt>entry</tt> for its type through the
<tt>type()</tt> function.
</p>
<p>
The <tt>print()</tt> function is there for debug purposes only.
</p>
<p>
If you want to create an <tt>entry</tt> you give it the type you want it to have in its
constructor, and then use one of the non-const accessors to get a reference which you then
can assign the value you want it to have.
</p>
<p>
The typical code to get info from a torrent file will then look like this:
</p>
<code>
entry torrent_file;
// ...
const entry::dictionary_type&amp; dict = torrent_file.dict();
entry::dictionary_type::const_iterator i;
i = dict.find("announce");
if (i != dict.end())
{
std::string tracker_url= i-&gt;second.string();
std::cout &lt;&lt; tracker_url &lt;&lt; "\n";
}
</code>
<p>
To make it easier to extract information from a torren file, the class <tt>torrent_info</tt>
exists.
</p>
<h2>torrent_info</h2>
<p>
The <tt>torrent_info</tt> has the following synopsis:
</p>
<pre>
class torrent_info
{
public:
torrent_info(const entry&amp; torrent_file)
typedef std::vector&lt;file&gt;::const_iterator file_iterator;
typedef std::vector&lt;file&gt;::const_reverse_iterator reverse_file_iterator;
file_iterator begin_files() const;
file_iterator end_files() const;
reverse_file_iterator rbegin_files() const;
reverse_file_iterator rend_files() const;
std::size_t num_files() const;
const file&amp; file_at(int index) const;
const std::vector&lt;announce_entry&gt;&amp; trackers() const;
int prioritize_tracker(int index);
entry::integer_type total_size() const;
entry::integer_type piece_length() const;
std::size_t num_pieces() const;
const sha1_hash&amp; info_hash() const;
void print(std::ostream&amp; os) const;
entry::integer_type piece_size(unsigned int index) const;
const sha1_hash&amp; hash_for_piece(unsigned int index) const;
};
</pre>
<p>
This class will need some explanation. First of all, to get a list of all files
in the torrent, you can use <tt>begin_files()</tt>, <tt>end_files()</tt>,
<tt>rbegin_files()</tt> and <tt>rend_files()</tt>. These will give you standard vector
iterators with the type <tt>file</tt>.
</p>
<pre>
struct file
{
std::string path;
std::string filename;
entry::integer_type size;
};
</pre>
<p>
If you need index-access to files you can use the <tt>num_files()</tt> and <tt>file_at()
</tt> to access files using indices.
</p>
<p>
The <tt>print()</tt> function is there for debug purposes only. It will print the info from
the torrent file to the given outstream.
</p>
<p>
The <tt>trackers()</tt> function will return a sorted vector of <tt>announce_entry</tt>.
Each announce entry contains a string, which is the tracker url, and a tier index. The
tier index is the high-level priority. No matter which trackers that works or not, the
ones with lower tier will always be tried before the one with higher tier number.
</p>
<pre>
struct announce_entry
{
std::string url;
int tier;
};
</pre>
<p>
The <tt>prioritize_tracker()</tt> is used internally to move a tracker to the front
of its tier group. i.e. It will never be moved pass a tracker with a different tier
number. For more information about how multiple trackers are dealt with, see the
<a href="http://home.elp.rr.com/tur/multitracker-spec.txt">specification</a>.
</p>
<p>
<tt>total_size()</tt>, <tt>piece_length()</tt> and <tt>num_pieces()</tt> returns the total
number of bytes the torrent-file represents (all the files in it), the number of byte for
each piece and the total number of pieces, respectively. The difference between
<tt>piece_size()</tt> and <tt>piece_length()</tt> is that <tt>piece_size()</tt> takes
the piece index as argument and gives you the exact size of that piece. It will always
be the same as <tt>piece_length()</tt> except in the case of the last piece, which may
be smaller.
</p>
<p>
<tt>hash_for_piece()</tt> takes a piece-index and returns the 20-bytes sha1-hash for that
piece and <tt>info_hash()</tt> returns the 20-bytes sha1-hash for the info-section of the
torrent file. For more information on the <tt>sha1_hash</tt>, see the <a href="#big_number">big_number</a> class.
</p>
<h2><a name="torrent_handle"></a>torrent_hande</h2>
<p>
You will usually have to store your <tt>torrent_handle</tt>s somewhere, since it's the
object thought which you retrieve infromation about the torrent and aborts the torrent.
Its declaration looks like this:
</p>
<pre>
struct torrent_handle
{
torrent_handle();
float progress() const;
void get_peer_info(std::vector&lt;peer_info&gt;&amp; v);
void abort();
enum state_t
{
checking_files,
connecting_to_tracker,
downloading,
seeding
};
state_t state() const;
};
</pre>
<!-- TODO: temporary comment -->
<p><tt>progress()</tt> and <tt>state()</tt>is not implemented yet</tt>.</p>
<p>
<tt>progress()</tt> will return a value in the range [0, 1], that represents the progress
of the torrent's current task. It may be checking files, connecting to tracker, or downloading
etc. You can get the torrent's current task bu calling <tt>state()</tt>, it will return one of
the following:
</p>
<table>
<tr>
<td>
<tt>checking_files</tt>
</td>
<td>
The torrent has not started its download yet, and is currently checking existing
files or is queued for having its files checked.
</td>
</tr>
<tr>
<td>
<tt>connecting_to_tracker</tt>
</td>
<td>
The torrent is waiting for tracker reply or waiting to retry a tracker connection.
If it's waiting to retry the progress meter will hint about when it will retry.
</td>
</tr>
<tr>
<td>
<tt>downloading</tt>
</td>
<td>
The torrent is being downloaded. This is the state most torrents will be in most
of the time. The progress meter will tell how much of the files that has been
downloaded.
</td>
</tr>
<tr>
<td>
<tt>seeding</tt>
</td>
<td>
In this state the torrent has finished downloading and is a pure seeder.
</td>
</tr>
</table>
<p>
<tt>abort()</tt> will close all peer connections associated with this torrent and tell
the tracker that we've stopped participating in the swarm. This handle will become invalid
shortly after this call has been made.
</p>
<p>
<tt>get_peer_info()</tt> takes a reference to a vector that will be cleared and filled
with one entry for each peer connected to this torrent. Each entry contains information about
that particular peer. It contains the following information:
</p>
<pre>
struct peer_info
{
enum
{
interesting = 0x1,
choked = 0x2,
remote_interested = 0x4,
remote_choked = 0x8
};
unsigned int flags;
address ip;
float up_speed;
float down_speed;
peer_id id;
std::vector&lt;bool&gt; pieces;
};
</pre>
<p>
The <tt>flags</tt> attribute tells you in which state the peer is. It is set to
any combination of the four enums above. Where <tt>interesting</tt> means that we
are interested in pieces from this peer. <tt>choked</tt> means that <u>we</u> has
choked this peer. <tt>remote_interested</tt> and <tt>remote_choked</tt> means the
same thing but that the peer is interested in pieces from us and the peer has choked
<u>us</u>.
</p>
<p>
The <tt>ip</tt> field is the IP-address to this peer. Its type is a wrapper around the
actual address and the port number. See <a href"#address">address</a> class.
</p>
<p>
<tt>up_speed</tt> and <tt>down_speed</tt> is the current upload and download speed
we have to and from this peer. These figures are updated aproximately once every second.
</p>
<p>
<tt>id</tt> is the peer's id as used in the bit torrent protocol. This id can be used to
extract 'fingerprints' from the peer. Sometimes it can tell you which client the peer
is using.
</p>
<p>
<tt>pieces</tt> is a vector of booleans that has as many entries as there are pieces
in the torrent. Each boolean tells you if the peer has that piece (if it's set to true)
or if the peer miss that piece (set to false).
</p>
<h2>http_settings</h2>
<p>
You have some control over tracker requests through the <tt>http_settings</tt> object. You
create it and fill it with your settings and the use <tt>session::set_http_settings()</tt>
to apply them. You have control over proxy and authorization settings and also the user-agent
that will be sent to the tracker. The user-agent is a good way to identify your client.
</p>
<pre>
struct http_settings
{
http_settings();
std::string proxy_ip;
int proxy_port;
std::string proxy_login;
std::string proxy_password;
std::string user_agent;
};
</pre>
<p>
TODO: address
</p>
<h2><a name="big_number"></a>big_number</h2>
<p>
Both the <tt>peer_id</tt> and <tt>sha1_hash</tt> types are typedefs of the class
<tt>big_number</tt>. It represents 20 bytes of data. Its synopsis follows:
</p>
<pre>
class big_number
{
public:
bool operator==(const big_number&amp; n) const;
bool operator!=(const big_number&amp; n) const;
bool operator&lt;(const big_number&amp; n) const;
const unsigned char* begin() const;
const unsigned char* end() const;
unsigned char* begin();
unsigned char* end();
};
</pre>
<p>
The iterators gives you access to individual bytes.
</p>
<h1>Credits</h1>
<p>
Copyright &copy; 2003 Arvid Norberg
</p>
<p>
<a href="http://sourceforge.net/">
<img style="border:0; width: 88px; height: 31px"
src="http://sourceforge.net/sflogo.php?group_id=79942"
alt="SourceForge" /></a>
<a href="http://validator.w3.org/check/referer">
<img style="border:0;width:88px;height:31px"
src="http://www.w3.org/Icons/valid-html401"
alt="Valid HTML 4.01!"></a>
<a href="http://jigsaw.w3.org/css-validator/">
<img style="border:0;width:88px;height:31px"
src="http://jigsaw.w3.org/css-validator/images/vcss"
alt="Valid CSS!"></a>
</p>
</body></html>

78
docs/style.css Executable file
View File

@ -0,0 +1,78 @@
body
{
background-color: white;
color: black;
margin: 1em 2em 1em 2em;
}
h1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; text-align: left; font-size: 140%; }
h2 { font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; text-align: left; font-size: 110%; }
h3 { font-family: "courier new", courier, monospace; font-weight: bold; text-align: left; font-size: 100%; }
pre
{
border: gray 1pt solid;
padding: 2pt;
display: block;
font-family: "courier new", courier, monospace;
background-color: #eeeeee;
color: black;
font-size: small
}
code
{
white-space: pre;
border: gray 1pt solid;
padding: 2pt;
display: block;
font-family: "courier new", courier, monospace;
color: black;
font-size: small
}
tt
{
display: inline;
font-family: "Courier New", Courier, monospace;
}
p
{
text-align: justify;
font-family: Georgia, "Times New Roman", Times, serif
}
ul
{
font-family: Georgia, "Times New Roman", Times, serif
}
ol
{
font-family: Georgia, "Times New Roman", Times, serif
}
a
{
font-weight: bold;
color: #003366;
text-decoration: none;
}
table
{
border: 1px solid black
}
td
{
border: 1px solid black
}
a:hover
{
color: #8080FF;
}

122
examples/client_test.cpp Executable file
View File

@ -0,0 +1,122 @@
/*
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.
*/
#include <iostream>
#include <fstream>
#include <iterator>
#include <exception>
#include "libtorrent/entry.hpp"
#include "libtorrent/bencode.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/http_settings.hpp"
#ifndef NDEBUG
struct cout_logger: libtorrent::logger
{
public:
virtual void log(const char* text) { std::cout << text; }
virtual void clear() {}
};
struct cout_log_creator: libtorrent::log_spawner
{
virtual libtorrent::logger* create_logger(const char* title)
{
cout_logger* log = new cout_logger();
return log;
}
};
#endif
int main(int argc, char* argv[])
{
using namespace libtorrent;
if (argc < 2)
{
std::cerr << "usage: torrent torrent-files ...\n";
return 1;
}
http_settings settings;
// settings.proxy_ip = "192.168.0.1";
// settings.proxy_port = 80;
// settings.proxy_login = "hyd";
// settings.proxy_password = "foobar";
settings.user_agent = "example";
try
{
std::vector<torrent_handle> handles;
#ifndef NDEBUG
cout_log_creator l;
session s(6881, &l);
#else
session s(6881);
#endif
s.set_http_settings(settings);
for (int i = 0; i < argc-1; ++i)
{
try
{
std::ifstream in(argv[i+1], std::ios_base::binary);
in.unsetf(std::ios_base::skipws);
entry e = bdecode(std::istream_iterator<char>(in), std::istream_iterator<char>());
torrent_info t(e);
t.print(std::cout);
handles.push_back(s.add_torrent(t, ""));
}
catch (std::exception& e)
{
std::cout << e.what() << "\n";
}
}
while (!handles.empty())
{
int a;
std::cin >> a;
handles.back().abort();
handles.pop_back();
}
}
catch (std::exception& e)
{
std::cout << e.what() << "\n";
}
return 0;
}

269
include/libtorrent/bencode.hpp Executable file
View File

@ -0,0 +1,269 @@
/*
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.
*/
#ifndef TORRENT_BENCODE_HPP_INCLUDED
#define TORRENT_BENCODE_HPP_INCLUDED
/*
* This file declares the following functions:
*
*----------------------------------
* template<class OutIt>
* void libtorrent::bencode(OutIt out, const libtorrent::entry& e);
*
* Encodes a message entry with bencoding into the output
* iterator given. The bencoding is described in the BitTorrent
* protocol description document OutIt must be an OutputIterator
* of type char. This may throw libtorrent::invalid_encoding if
* the entry contains invalid nodes (undefined_t for example).
*
*----------------------------------
* template<class InIt>
* libtorrent::entry libtorrent::bdecode(InIt start, InIt end);
*
* Decodes the buffer given by the start and end iterators
* and returns the decoded entry. InIt must be an InputIterator
* of type char. May throw libtorrent::invalid_encoding if
* the string is not correctly bencoded.
*
*/
#include <cstdlib>
#include <boost/lexical_cast.hpp>
#include "libtorrent/entry.hpp"
#ifdef _MSC_VER
namespace std
{
using ::isdigit;
using ::atoi;
};
#define for if (false) {} else for
#endif
namespace libtorrent
{
struct invalid_encoding: std::exception
{
virtual const char* what() const throw() { return "invalid bencoding"; }
};
namespace detail
{
template <class OutIt>
void write_string(OutIt& out, const std::string& val)
{
std::string::const_iterator end = val.begin()+val.length();
for (std::string::const_iterator i = val.begin(); i != end; ++i)
{
*out = *i;
++out;
}
}
template <class OutIt>
void write_integer(OutIt& out, entry::integer_type val)
{
write_string(out, boost::lexical_cast<std::string>(val));
}
template <class InIt>
std::string read_until(InIt& in, InIt end, char end_token)
{
if (in == end) throw invalid_encoding();
std::string ret;
while (*in != end_token)
{
ret += *in;
++in;
if (in == end) throw invalid_encoding();
}
return ret;
}
template<class InIt>
std::string read_string(InIt& in, InIt end, int len)
{
std::string ret;
for (int i = 0; i < len; ++i)
{
if (in == end) throw invalid_encoding();
ret += *in;
++in;
}
return ret;
}
template<class OutIt>
void bencode_recursive(OutIt& out, const entry& e)
{
switch(e.type())
{
case entry::int_t:
*out = 'i'; ++out;
write_integer(out, e.integer());
*out = 'e'; ++out;
break;
case entry::string_t:
write_integer(out, e.string().length());
*out = ':'; ++out;
write_string(out, e.string());
break;
case entry::list_t:
*out = 'l'; ++out;
for (entry::list_type::const_iterator i = e.list().begin(); i != e.list().end(); ++i)
bencode_recursive(out, *i);
*out = 'e'; ++out;
break;
case entry::dictionary_t:
*out = 'd'; ++out;
for (entry::dictionary_type::const_iterator i = e.dict().begin(); i != e.dict().end(); ++i)
{
// write key
write_integer(out, i->first.length());
*out = ':'; ++out;
write_string(out, i->first);
// write value
bencode_recursive(out, i->second);
}
*out = 'e'; ++out;
break;
default:
throw invalid_encoding();
}
}
template<class InIt>
entry bdecode_recursive(InIt& in, InIt end)
{
if (in == end) throw invalid_encoding();
entry ret;
switch (*in)
{
// ----------------------------------------------
// integer
case 'i':
{
++in; // 'i'
std::string val = read_until(in, end, 'e');
assert(*in == 'e');
++in; // 'e'
ret = entry(entry::int_t);
ret.integer() = boost::lexical_cast<entry::integer_type>(val);
} break;
// ----------------------------------------------
// list
case 'l':
{
ret = entry(entry::list_t);
++in; // 'l'
while (*in != 'e')
{
ret.list().push_back(bdecode_recursive(in, end));
if (in == end) throw invalid_encoding();
}
assert(*in == 'e');
++in; // 'e'
} break;
// ----------------------------------------------
// dictionary
case 'd':
{
ret = entry(entry::dictionary_t);
++in; // 'd'
while (*in != 'e')
{
entry key = bdecode_recursive(in, end);
ret.dict()[key.string()] = bdecode_recursive(in, end);
if (in == end) throw invalid_encoding();
}
assert(*in == 'e');
++in; // 'e'
} break;
// ----------------------------------------------
// string
default:
if (std::isdigit(*in))
{
std::string len_s = read_until(in, end, ':');
assert(*in == ':');
++in; // ':'
int len = std::atoi(len_s.c_str());
ret = entry(entry::string_t);
ret.string() = read_string(in, end, len);
}
else
{
throw invalid_encoding();
}
}
return ret;
}
}
template<class OutIt>
void bencode(OutIt out, const entry& e)
{
detail::bencode_recursive(out, e);
}
template<class InIt>
entry bdecode(InIt start, InIt end)
{
try
{
return detail::bdecode_recursive(start, end);
}
catch(type_error&)
{
throw invalid_encoding();
}
}
}
#endif // TORRENT_BENCODE_HPP_INCLUDED

50
include/libtorrent/config.hpp Executable file
View File

@ -0,0 +1,50 @@
/*
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.
*/
#ifndef TORRENT_CONFIG_HPP_INCLUDED
#define TORRENT_CONFIG_HPP_INCLUDED
#if defined(_WINDOWS)
#if defined(_USRDLL)
#define TORRENT_EXPORT __declspec(dllexport)
#else
#define TORRENT_EXPORT __declspec(dllimport)
#endif
#else
#define TORRENT_EXPORT
#endif
#endif

97
include/libtorrent/debug.hpp Executable file
View File

@ -0,0 +1,97 @@
/*
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.
*/
#ifndef TORRENT_DEBUG_HPP_INCLUDED
#define TORRENT_DEBUG_HPP_INCLUDED
#include <string>
#include <boost/lexical_cast.hpp>
namespace libtorrent
{
#ifndef NDEBUG
// DEBUG API
struct logger
{
logger& operator<<(const char* t)
{ log(t); return *this; }
logger& operator<<(const std::string& t)
{ log(t.c_str()); return *this; }
logger& operator<<(int i)
{
log(boost::lexical_cast<std::string>(i).c_str());
return *this;
}
logger& operator<<(unsigned int i)
{
log(boost::lexical_cast<std::string>(i).c_str());
return *this;
}
logger& operator<<(float i)
{
log(boost::lexical_cast<std::string>(i).c_str());
return *this;
}
logger& operator<<(char i)
{
char c[2];
c[0] = i;
c[1] = 0;
log(c);
return *this;
}
virtual void log(const char*) = 0;
virtual void clear() = 0;
virtual ~logger() {}
};
// this is an abstract base for a log-window creator
// this is called every time a new peer connects
// the logger that gets created will be deleted
// by the library when it's finished with it.
struct log_spawner
{
virtual logger* create_logger(const char* title) = 0;
virtual ~log_spawner() {}
};
#endif
}
#endif // TORRENT_DEBUG_HPP_INCLUDED

231
include/libtorrent/entry.hpp Executable file
View File

@ -0,0 +1,231 @@
/*
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.
*/
#ifndef TORRENT_ENTRY_HPP_INCLUDED
#define TORRENT_ENTRY_HPP_INCLUDED
/*
*
* This file declares the entry class. It is a
* variant-type that can be an integer, list,
* dictionary (map) or a string. This type is
* used to hold bdecoded data (which is the
* encoding BitTorrent messages uses).
*
* it has 4 accessors to access the actual
* type of the object. They are:
* integer()
* string()
* list()
* dict()
* The actual type has to match the type you
* are asking for, otherwise you will get an
* assertion failure.
* When you default construct an entry, it is
* uninitialized. You can initialize it through the
* assignment operator, copy-constructor or
* the constructor that takes a data_type enum.
*
*
*/
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <stdexcept>
#include <cassert>
namespace libtorrent
{
struct type_error: std::runtime_error
{
type_error(const char* error): std::runtime_error(error) {}
};
namespace detail
{
template<int v1, int v2>
struct max2 { enum { value = v1>v2?v1:v2 }; };
template<int v1, int v2, int v3>
struct max3
{
enum
{
temp = max2<v1,v2>::value,
value = temp>v3?temp:v3
};
};
template<int v1, int v2, int v3, int v4>
struct max4
{
enum
{
temp = max3<v1,v2, v3>::value,
value = temp>v4?temp:v4
};
};
}
class entry
{
public:
typedef std::map<std::string, entry> dictionary_type;
typedef std::string string_type;
typedef std::vector<entry> list_type;
#if defined(_MSC_VER) && _MSC_VER >= 1300
typedef __int64 integer_type;
#elif defined(_MSC_VER)
typedef int integer_type;
#elif defined(__GNUC__)
typedef long long integer_type;
#else
typedef long integer_type;
#endif
enum data_type
{
int_t,
string_t,
list_t,
dictionary_t,
undefined_t
};
data_type type() const { return m_type; }
entry(): m_type(undefined_t) {}
entry(data_type t): m_type(t) { construct(t); }
entry(const entry& e) { copy(e); }
~entry() { destruct(); }
void operator=(const entry& e)
{
destruct();
copy(e);
}
integer_type& integer()
{
if (m_type != int_t) throw type_error("invalid typ requested from entry");
return *reinterpret_cast<integer_type*>(data);
}
const integer_type& integer() const
{
if (m_type != int_t) throw type_error("invalid typ requested from entry");
return *reinterpret_cast<const integer_type*>(data);
}
string_type& string()
{
if (m_type != string_t) throw type_error("invalid typ requested from entry");
return *reinterpret_cast<string_type*>(data);
}
const string_type& string() const
{
if (m_type != string_t) throw type_error("invalid typ requested from entry");
return *reinterpret_cast<const string_type*>(data);
}
list_type& list()
{
if (m_type != list_t) throw type_error("invalid typ requested from entry");
return *reinterpret_cast<list_type*>(data);
}
const list_type& list() const
{
if (m_type != list_t) throw type_error("invalid typ requested from entry");
return *reinterpret_cast<const list_type*>(data);
}
dictionary_type& dict()
{
if (m_type != dictionary_t) throw type_error("invalid typ requested from entry");
return *reinterpret_cast<dictionary_type*>(data);
}
const dictionary_type& dict() const
{
if (m_type != dictionary_t) throw type_error("invalid typ requested from entry");
return *reinterpret_cast<const dictionary_type*>(data);
}
void print(std::ostream& os, int indent) const;
private:
void construct(data_type t);
void copy(const entry& e);
void destruct();
data_type m_type;
#if defined(_MSC_VER)
union
{
char data[detail::max4<sizeof(list_type)
, sizeof(std::map<std::string, char>)
, sizeof(string_type)
, sizeof(integer_type)>::value];
integer_type dummy_aligner;
};
#else
union
{
char data[detail::max4<sizeof(list_type)
, sizeof(dictionary_type)
, sizeof(string_type)
, sizeof(integer_type)>::value];
integer_type dummy_aligner;
};
#endif
};
inline std::ostream& operator<<(std::ostream& os, const entry& e)
{
e.print(os, 0);
return os;
}
}
#endif // TORRENT_ENTRY_HPP_INCLUDED

69
include/libtorrent/hasher.hpp Executable file
View File

@ -0,0 +1,69 @@
/*
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.
*/
#ifndef TORRENT_HASHER_HPP_INCLUDED
#define TORRENT_HASHER_HPP_INCLUDED
extern "C"
{
typedef struct {
unsigned int state[5];
unsigned int count[2];
unsigned char buffer[64];
} SHA1_CTX;
void SHA1Init(SHA1_CTX* context);
void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len);
void SHA1Final(unsigned char* digest, SHA1_CTX* context);
}
namespace libtorrent
{
class hasher
{
public:
hasher() { SHA1Init(&m_context); }
void update(const char* data, unsigned int len)
{ SHA1Update(&m_context, reinterpret_cast<unsigned char*>(const_cast<char*>(data)), len); }
void final(sha1_hash& digest) { SHA1Final(digest.begin(), &m_context); }
private:
SHA1_CTX m_context;
};
}
#endif // TORRENT_HASHER_HPP_INCLUDED

62
include/libtorrent/peer.hpp Executable file
View File

@ -0,0 +1,62 @@
/*
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.
*/
#ifndef TORRENT_PEER_HPP_INCLUDED
#define TORRENT_PEER_HPP_INCLUDED
#include <string>
#include "libtorrent/peer_id.hpp"
namespace libtorrent
{
struct peer
{
std::string ip;
int port;
peer_id id;
bool operator==(const peer& p) const
{
return id == p.id;
}
bool operator<(const peer& p) const
{
return id < p.id;
}
};
}
#endif // TORRENT_PEER_HPP_INCLUDED

View File

@ -0,0 +1,249 @@
/*
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.
*/
#ifndef TORRENT_PEER_CONNECTION_HPP_INCLUDED
#define TORRENT_PEER_CONNECTION_HPP_INCLUDED
#include <ctime>
#include <algorithm>
#include <vector>
#include <boost/smart_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <boost/array.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include "libtorrent/socket.hpp"
#include "libtorrent/peer_id.hpp"
#include "libtorrent/storage.hpp"
#include "libtorrent/stat.hpp"
#include "libtorrent/debug.hpp"
/*
* This file declares the following functions:
*
*----------------------------------
*
*
*
*/
namespace libtorrent
{
class torrent;
namespace detail
{
struct session_impl;
}
struct peer_request
{
int piece;
int start;
int length;
bool operator==(const peer_request& r)
{ return piece == r.piece && start == r.start && length == r.length; }
};
// TODO: add checks to make sure we only get pieces we ask for
class peer_connection: public boost::noncopyable
{
public:
// this is the constructor where the we are teh active part. The peer_conenction
// should handshake and verify that the other end has the correct id
peer_connection(detail::session_impl* ses, torrent* t, boost::shared_ptr<libtorrent::socket> s, const peer_id& p);
// with this constructor we have been contacted and we still don't know which torrent the
// connection belongs to
peer_connection(detail::session_impl* ses, boost::shared_ptr<libtorrent::socket> s);
~peer_connection();
// this adds an announcement in the announcement queue
// it will let the peer know that we have the given piece
void announce_piece(int index) { m_announce_queue.push_back(index); }
// called from the main loop when this connection has any
// work to do.
void send_data();
void receive_data();
// tells if this connection has data it want to send
bool has_data() const throw();
bool has_timed_out()
{
boost::posix_time::time_duration d;
d = boost::posix_time::second_clock::local_time() - m_last_receive;
return d.seconds() > m_timeout;
}
// will send a keep-alive message to the peer
void keep_alive();
const peer_id& id() const throw() { return m_peer_id; }
bool has_piece(int i) const throw() { return m_have_piece[i]; }
const std::vector<piece_block>& download_queue() const throw()
{ return m_download_queue; }
void choke();
void unchoke();
void interested();
void not_interested();
void request_block(piece_block block);
bool is_interesting() const throw() { return m_interesting; }
bool has_choked() const throw() { return m_choked; }
bool is_peer_interested() const throw() { return m_peer_interested; }
bool has_peer_choked() const throw() { return m_peer_choked; }
// returns the torrent this connection is a part of
// may be zero if the connection is an incoming connection
// and it hasn't received enough information to determine
// which torrent it should be associated with
torrent* associated_torrent() const throw() { return m_torrent; }
const stat& statistics() const { return m_statistics; }
// is called once every second by the main loop
void second_tick() { m_statistics.second_tick(); }
boost::shared_ptr<libtorrent::socket> get_socket() const { return m_socket; }
const peer_id& get_peer_id() const { return m_peer_id; }
const std::vector<bool>& get_bitfield() const { return m_have_piece; }
#ifndef NDEBUG
boost::shared_ptr<logger> m_logger;
#endif
private:
bool dispatch_message();
void send_bitfield();
void send_have(int index);
void send_handshake();
// is used during handshake
enum state
{
read_protocol_length = 0,
read_protocol_version,
read_info_hash,
read_peer_id,
read_packet_size,
read_packet
};
state m_state;
// the timeout in seconds
int m_timeout;
enum message_type
{
msg_choke = 0,
msg_unchoke,
msg_interested,
msg_not_interested,
msg_have,
msg_bitfield,
msg_request,
msg_piece,
msg_cancel
};
std::size_t m_packet_size;
std::size_t m_recv_pos;
std::vector<char> m_recv_buffer;
// this is the buffer where data that is
// to be sent is stored until
std::vector<char> m_send_buffer;
// timeouts
boost::posix_time::ptime m_last_receive;
boost::posix_time::ptime m_last_sent;
boost::shared_ptr<libtorrent::socket> m_socket;
torrent* m_torrent;
detail::session_impl* m_ses;
// is true if it was we that connected to the peer
// and false if we got an incomming connection
bool m_active;
// remote peer's id
peer_id m_peer_id;
// the pieces that we are sending and receiving
piece_file m_sending_piece;
piece_file m_receiving_piece;
// other side says that it's interested in downloading
// from us.
bool m_peer_interested;
// the other side has told us that it won't send anymore
// data to us for a while
bool m_peer_choked;
// the peer has pieces we are interested in
bool m_interesting;
// we have choked the upload to the peer
bool m_choked;
// the pieces the other end have
std::vector<bool> m_have_piece;
std::vector<peer_request> m_requests;
// a list of pieces that have become available
// and should be announced as available to
// the peer
std::vector<int> m_announce_queue;
std::vector<piece_block> m_download_queue;
stat m_statistics;
};
}
#endif // TORRENT_PEER_CONNECTION_HPP_INCLUDED

83
include/libtorrent/peer_id.hpp Executable file
View File

@ -0,0 +1,83 @@
/*
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.
*/
#ifndef TORRENT_PEER_ID_HPP_INCLUDED
#define TORRENT_PEER_ID_HPP_INCLUDED
namespace libtorrent
{
class big_number
{
// the number of bytes of the number
enum { number_size = 20 };
public:
bool operator==(const big_number& n) const
{
return std::equal(n.m_number, n.m_number+number_size, m_number);
}
bool operator!=(const big_number& n) const
{
return !std::equal(n.m_number, n.m_number+number_size, m_number);
}
bool operator<(const big_number& n) const
{
for(int i = 0; i < number_size; ++i)
{
if (m_number[i] < n.m_number[i]) return true;
if (m_number[i] > n.m_number[i]) return false;
}
return false;
}
const unsigned char* begin() const { return m_number; }
const unsigned char* end() const { return m_number+number_size; }
unsigned char* begin() { return m_number; }
unsigned char* end() { return m_number+number_size; }
private:
unsigned char m_number[number_size];
};
typedef big_number peer_id;
typedef big_number sha1_hash;
}
#endif // TORRENT_PEER_ID_HPP_INCLUDED

View File

@ -0,0 +1,61 @@
/*
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.
*/
#ifndef TORRENT_PEER_INFO_HPP_INCLUDED
#define TORRENT_PEER_INFO_HPP_INCLUDED
#include <vector>
#include "libtorrent/socket.hpp"
#include "libtorrent/peer_id.hpp"
namespace libtorrent
{
struct peer_info
{
enum
{
interesting = 0x1,
choked = 0x2,
remote_interested = 0x4,
remote_choked = 0x8
};
unsigned int flags;
address ip;
float up_speed;
float down_speed;
peer_id id;
std::vector<bool> pieces;
};
}
#endif // TORRENT_PEER_INFO_HPP_INCLUDED

View File

@ -0,0 +1,198 @@
/*
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.
*/
#ifndef TORRENT_PIECE_PICKER_HPP_INCLUDED
#define TORRENT_PIECE_PICKER_HPP_INCLUDED
#include <algorithm>
#include <vector>
#include <bitset>
#include <cassert>
namespace libtorrent
{
class torrent;
class address;
class peer_connection;
struct piece_block
{
piece_block(int p_index, int b_index)
: piece_index(p_index)
, block_index(b_index)
{}
int piece_index;
int block_index;
};
class piece_picker
{
public:
enum { max_blocks_per_piece = 128 };
piece_picker(int blocks_per_piece,
int total_num_blocks);
// this is called before any other method is called
// after the local files has been checked.
// the vector tells which pieces we already have
// and which we don't have.
void files_checked(const std::vector<bool>& pieces);
// increases the peer count for the given piece
// (is used when a HAVE or BITFIELD message is received)
// returns true if this piece was interesting
bool inc_refcount(int index);
void dec_refcount(int index);
// This indicates that we just received this piece
// it means that the refcounter will indicate that
// we are not interested in this piece anymore
// (i.e. we don't have to maintain a refcount)
void we_have(int index);
// pieces should be the vector that represents the pieces a
// client has. It returns a list of all pieces that this client
// has and that are interesting to download. It returns them in
// priority order. It doesn't care about the download flag.
// The user of this function must lookup if any piece is
// marked as being downloaded. If the user of this function
// decides to download a piece, it must mark it as being downloaded
// itself, by using the mark_as_downloading() member function.
// THIS IS DONE BY THE peer_connection::request_piece() MEMBER FUNCTION!
void pick_pieces(const std::vector<bool>& pieces,
std::vector<piece_block>& interesting_blocks) const;
// returns true if any client is currently downloading this
// piece-block, or if it's queued for downloading by some client
// or if it already has been successfully downlloaded
bool is_downloading(piece_block block) const;
// marks this piece-block as queued for downloading
void mark_as_downloading(piece_block block);
void mark_as_finished(piece_block block);
// if a piece had a hash-failure, it must be restured and
// made available fro redownloading
void restore_piece(int index);
// clears the given piece's download flag
// this means that this piece-block can be picked again
void abort_download(piece_block block);
bool is_piece_finished(int index) const;
#ifndef NDEBUG
// used in debug mode
void integrity_check(const torrent* t = 0) const;
#endif
private:
struct piece_pos
{
piece_pos() {}
piece_pos(int peer_count_, int index_)
: peer_count(peer_count_)
, downloading(0)
, index(index_)
{}
// selects which vector to look in
unsigned peer_count : 7;
// is 1 if the piece is marked as being downloaded
unsigned downloading : 1;
// index in to the piece_info vector
unsigned index : 24;
bool operator!=(piece_pos p)
{ return index != p.index || peer_count != p.peer_count; }
bool operator==(piece_pos p)
{ return index == p.index && peer_count == p.peer_count; }
};
struct downloading_piece
{
int index;
std::bitset<max_blocks_per_piece> requested_blocks;
std::bitset<max_blocks_per_piece> finished_blocks;
};
struct has_index
{
has_index(int i): index(i) {}
bool operator()(const downloading_piece& p) const
{ return p.index == index; }
int index;
};
void move(bool downloading, int vec_index, int elem_index);
void remove(bool downloading, int vec_index, int elem_index);
bool add_interesting_blocks(const std::vector<int>& piece_list,
const std::vector<bool>& pieces,
std::vector<piece_block>& interesting_pieces) const;
// this vector contains all pieces we don't have.
// in the first entry (index 0) is a vector of all pieces
// that no peer have, the vector at index 1 contains
// all pieces that exactly one peer have, index 2 contains
// all pieces exactly two peers have and so on.
std::vector<std::vector<int> > m_piece_info;
std::vector<std::vector<int> > m_downloading_piece_info;
// this maps indices to number of peers that has this piece and
// index into the m_piece_info vectors.
// 0xffffff means that we have the piece, so it doesn't
// exist in the piece_info buckets
std::vector<piece_pos> m_piece_map;
// each piece that's currently being downloaded
// has an entry in this list with block allocations.
// i.e. it says wich parts of the piece that
// is being downloaded
std::vector<downloading_piece> m_downloads;
int m_blocks_per_piece;
int m_blocks_in_last_piece;
};
}
#endif // TORRENT_PIECE_PICKER_HPP_INCLUDED

95
include/libtorrent/policy.hpp Executable file
View File

@ -0,0 +1,95 @@
/*
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.
*/
#ifndef TORRENT_POLICY_HPP_INCLUDED
#define TORRENT_POLICY_HPP_INCLUDED
#include <algorithm>
#include <vector>
#include "libtorrent/peer.hpp"
#include "piece_picker.hpp"
// TODO: should be able to close connections with too low bandwidth to save memory
namespace libtorrent
{
class torrent;
class address;
class peer_connection;
class policy
{
public:
policy(torrent* t);
// this is called each time we get an incoming connection
// return true to accept the connection
bool accept_connection(const address& remote);
// this is called once for every peer we get from
// the tracker
void peer_from_tracker(const address& remote, const peer_id& id);
// the given connection was just closed
void connection_closed(const peer_connection& c);
// the peer has got at least one interesting piece
void peer_is_interesting(peer_connection& c);
void piece_finished(peer_connection& c, int index, bool successfully_verified);
void block_finished(peer_connection& c, piece_block b);
// the peer choked us
void choked(peer_connection& c);
// the peer unchoked us
void unchoked(peer_connection& c);
// the peer is interested in our pieces
void interested(peer_connection& c);
// the peer is not interested in our pieces
void not_interested(peer_connection& c);
private:
int m_num_peers;
torrent* m_torrent;
};
}
#endif // TORRENT_POLICY_HPP_INCLUDED

235
include/libtorrent/session.hpp Executable file
View File

@ -0,0 +1,235 @@
/*
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.
*/
/*
implemented
* http-proxy authentication for tracker requests
* multitracker support
* spawns separate temporary threads for file checking
*
missing
* endgame-mode
* correct algorithm for choking and unchoking
*/
#ifndef TORRENT_SESION_HPP_INCLUDED
#define TORRENT_SESSION_HPP_INCLUDED
#include <ctime>
#include <algorithm>
#include <vector>
#include <set>
#include <list>
#include <boost/limits.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/thread.hpp>
#include "libtorrent/torrent.hpp"
#include "libtorrent/entry.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/socket.hpp"
#include "libtorrent/peer_connection.hpp"
#include "libtorrent/peer_id.hpp"
#include "libtorrent/policy.hpp"
#include "libtorrent/url_handler.hpp"
#include "libtorrent/peer_info.hpp"
#include "libtorrent/debug.hpp"
// TODO: if we're not interested and the peer isn't interested, close the connections
// TODO: instead of implementing end-game mode, have an algorithm that
// constantly prioritizes high-bandwidth sources.
namespace libtorrent
{
namespace detail
{
struct piece_checker_data
{
boost::shared_ptr<torrent> torrent_ptr;
std::string save_path;
// when the files has been checked
// the torrent is added to the session
session_impl* ses;
sha1_hash info_hash;
// must be locked to access the data
// below in this struct
boost::mutex mutex;
float progress;
};
struct piece_check_thread
{
piece_check_thread(const boost::shared_ptr<piece_checker_data>& p)
: m_data(p)
{}
void operator()();
boost::shared_ptr<piece_checker_data> m_data;
};
// this is the link between the main thread and the
// thread started to run the main downloader loop
struct session_impl
{
typedef std::map<boost::shared_ptr<socket>, boost::shared_ptr<peer_connection> > connection_map;
#ifndef NDEBUG
session_impl(log_spawner* log_creator): m_abort(false)
{
m_log_spawner = log_creator;
}
#else
session_impl(): m_abort(false)
{
}
#endif
// must be locked to access the data
// in this struct
boost::mutex m_mutex;
tracker_manager m_tracker_manager;
std::map<sha1_hash, boost::shared_ptr<torrent> > m_torrents;
connection_map m_connections;
// a list of all torrents that are currently checking
// their files (in separate threads)
std::map<sha1_hash, boost::shared_ptr<piece_checker_data> > m_checkers;
boost::thread_group m_checker_threads;
// the peer id that is generated at the start of each torrent
peer_id m_peer_id;
// this is where all active sockets are stored.
// the selector can sleep while there's no activity on
// them
selector m_selector;
bool m_abort;
void run(int listen_port);
torrent* find_torrent(const sha1_hash& info_hash);
const peer_id& get_peer_id() const { return m_peer_id; }
#ifndef NDEBUG
log_spawner* m_log_spawner;
boost::shared_ptr<logger> m_logger;
#endif
};
struct main_loop_thread
{
main_loop_thread(int listen_port, session_impl* s)
: m_ses(s), m_listen_port(listen_port) {}
void operator()() { m_ses->run(m_listen_port); }
session_impl* m_ses;
int m_listen_port;
};
}
struct http_settings;
struct torrent_handle
{
friend class session;
torrent_handle(): m_ses(0) {}
float progress() const;
void get_peer_info(std::vector<peer_info>& v);
void abort();
enum state_t
{
checking_files,
connecting_to_tracker,
downloading,
seeding
};
// state_t state() const;
private:
torrent_handle(detail::session_impl* s, const sha1_hash& h)
: m_ses(s)
, m_info_hash(h)
{}
detail::session_impl* m_ses;
sha1_hash m_info_hash; // should be replaced with a torrent*?
};
class session: public boost::noncopyable
{
public:
#ifndef NDEBUG
session(int listen_port, log_spawner* new_log)
: m_impl(new_log)
, m_thread(detail::main_loop_thread(listen_port, &m_impl))
{
}
#else
session(int listen_port)
: m_thread(detail::main_loop_thread(listen_port, &m_impl)) {}
#endif
~session();
// all torrent_handles must be destructed before the session is destructed!
torrent_handle add_torrent(const torrent_info& ti, const std::string& save_path);
void set_http_settings(const http_settings& s);
private:
// data shared between the threads
detail::session_impl m_impl;
boost::thread m_thread;
};
}
#endif // TORRENT_SESSION_HPP_INCLUDED

261
include/libtorrent/socket.hpp Executable file
View File

@ -0,0 +1,261 @@
/*
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.
*/
#ifndef TORRENT_SOCKET_WIN_HPP_INCLUDED
#define TORRENT_SOCKET_WIN_HPP_INCLUDED
#if defined(_WIN32)
#include <winsock2.h>
#else
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <pthread.h>
#include <fcntl.h>
#include <arpa/inet.h>
#endif
#include <boost/smart_ptr.hpp>
#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <vector>
#include <exception>
#include <string>
namespace libtorrent
{
class network_error : public std::exception
{
public:
network_error(int error_code): m_error_code(error_code) {}
virtual const char* what() const throw();
private:
int m_error_code;
};
class socket;
class address
{
friend class socket;
public:
address();
address(unsigned char a, unsigned char b, unsigned char c, unsigned char d, unsigned short port);
address(unsigned int addr, unsigned short port);
address(const std::string& addr, unsigned short port);
address(const address& a);
~address();
std::string as_string() const throw();
unsigned int ip() const throw() { return m_sockaddr.sin_addr.s_addr; }
unsigned short port() const throw() { return htons(m_sockaddr.sin_port); }
bool operator<(const address& a) const throw() { if (ip() == a.ip()) return port() < a.port(); else return ip() < a.ip(); }
bool operator!=(const address& a) const throw() { return (ip() != a.ip()) || port() != a.port(); }
bool operator==(const address& a) const throw() { return (ip() == a.ip()) && port() == a.port(); }
private:
sockaddr_in m_sockaddr;
};
class socket: public boost::noncopyable
{
friend class address;
friend class selector;
public:
enum type
{
tcp = 0,
udp
};
socket(type t, bool blocking = true, unsigned short receive_port = 0);
virtual ~socket();
void connect(const address& addr);
void close();
void set_blocking(bool blocking);
bool is_blocking() { return m_blocking; }
const address& sender() const { return m_sender; }
address name() const;
void listen(unsigned short port, int queue);
boost::shared_ptr<libtorrent::socket> accept();
template<class T> int send(const T& buffer);
template<class T> int send_to(const address& addr, const T& buffer);
template<class T> int receive(T& buf);
int send(const char* buffer, int size);
int send_to(const address& addr, const char* buffer, int size);
int receive(char* buffer, int size);
void set_receive_bufsize(int size);
void set_send_bufsize(int size);
bool is_readable() const;
bool is_writable() const;
enum error_code
{
#if defined(_WIN32)
netdown = WSAENETDOWN,
fault = WSAEFAULT,
access = WSAEACCES,
address_in_use = WSAEADDRINUSE,
address_not_available = WSAEADDRNOTAVAIL,
in_progress = WSAEINPROGRESS,
interrupted = WSAEINTR,
invalid = WSAEINVAL,
net_reset = WSAENETRESET,
not_connected = WSAENOTCONN,
no_buffers = WSAENOBUFS,
operation_not_supported = WSAEOPNOTSUPP,
not_socket = WSAENOTSOCK,
shutdown = WSAESHUTDOWN,
would_block = WSAEWOULDBLOCK,
connection_reset = WSAECONNRESET,
timed_out = WSAETIMEDOUT,
connection_aborted = WSAECONNABORTED,
message_size = WSAEMSGSIZE,
not_ready = WSAEALREADY,
no_support = WSAEAFNOSUPPORT,
connection_refused = WSAECONNREFUSED,
is_connected = WSAEISCONN,
net_unreachable = WSAENETUNREACH
#else
netdown = ENETDOWN,
fault = EFAULT,
access = EACCES,
address_in_use = EADDRINUSE,
address_not_available = EADDRNOTAVAIL,
in_progress = EINPROGRESS,
interrupted = EINTR,
invalid = EINVAL,
net_reset = ENETRESET,
not_connected = ENOTCONN,
no_buffers = ENOMEM,
operation_not_supported = EOPNOTSUPP,
not_socket = ENOTSOCK,
shutdown = ESHUTDOWN,
would_block = EAGAIN,
connection_reset = ECONNRESET,
timed_out = ETIMEDOUT,
connection_aborted = ECONNABORTED,
message_size = EMSGSIZE,
not_ready = EALREADY,
no_support = EAFNOSUPPORT,
connection_refused = ECONNREFUSED,
is_connected = EISCONN,
net_unreachable = ENETUNREACH
#endif
};
error_code last_error() const;
private:
socket(int sock, const address& sender);
int m_socket;
address m_sender;
bool m_blocking;
#ifndef NDEBUG
bool m_connected; // indicates that this socket has been connected
type m_type;
#endif
};
template<class T>
inline int socket::send(const T& buf)
{
return send(reinterpret_cast<const char*>(&buf), sizeof(buf));
}
template<class T>
inline int socket::send_to(const address& addr, const T& buf)
{
return send_to(addr, reinterpret_cast<const unsigned char*>(&buf), sizeof(buf));
}
template<class T>
inline int socket::receive(T& buf)
{
return receive(reinterpret_cast<char*>(&buf), sizeof(T));
}
// timeout is given in microseconds
// modified is cleared and filled with the sockets that is ready for reading or writing
// or have had an error
class selector
{
public:
void monitor_readability(boost::shared_ptr<socket> s) { m_readable.push_back(s); }
void monitor_writability(boost::shared_ptr<socket> s) { m_writable.push_back(s); }
void monitor_errors(boost::shared_ptr<socket> s) { m_error.push_back(s); }
void clear_readable() { m_readable.clear(); }
void clear_writable() { m_writable.clear(); }
void remove(boost::shared_ptr<socket> s);
void wait(int timeout
, std::vector<boost::shared_ptr<socket> >& readable
, std::vector<boost::shared_ptr<socket> >& writable
, std::vector<boost::shared_ptr<socket> >& error);
private:
std::vector<boost::shared_ptr<socket> > m_readable;
std::vector<boost::shared_ptr<socket> > m_writable;
std::vector<boost::shared_ptr<socket> > m_error;
};
}
#endif // TORRENT_SOCKET_WIN_HPP_INCLUDED

102
include/libtorrent/stat.hpp Executable file
View File

@ -0,0 +1,102 @@
/*
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.
*/
#ifndef TORRENT_STAT_HPP_INCLUDED
#define TORRENT_STAT_HPP_INCLUDED
#include <algorithm>
#include <vector>
namespace libtorrent
{
class stat
{
enum { history = 5 };
public:
stat()
: m_downloaded(0)
, m_uploaded(0)
, m_total_download(0)
, m_total_upload(0)
, m_peak_downloaded_per_second(0)
, m_peak_uploaded_per_second(0)
{
std::fill(m_download_per_second_history, m_download_per_second_history+history, 0);
std::fill(m_upload_per_second_history, m_upload_per_second_history+history, 0);
}
void received_bytes(int num_bytes)
{ m_downloaded += num_bytes; m_total_download += num_bytes; }
void sent_bytes(int num_bytes)
{ m_uploaded += num_bytes; m_total_upload += num_bytes; }
// should be called once every second
void second_tick();
float upload_rate() const { return m_mean_upload_per_second; }
float download_rate() const { return m_mean_download_per_second; }
float down_peak() const { return m_peak_downloaded_per_second; }
float up_peak() const { return m_peak_uploaded_per_second; }
unsigned int total_upload() const { return m_total_upload; }
unsigned int total_dowload() const { return m_total_download; }
private:
// history of download/upload speeds a few seconds back
unsigned int m_download_per_second_history[history];
unsigned int m_upload_per_second_history[history];
// the accumulators we are adding the downloads/upploads
// to this second
unsigned int m_downloaded;
unsigned int m_uploaded;
// total download/upload counters
unsigned int m_total_download;
unsigned int m_total_upload;
// peak mean download/upload rates
unsigned int m_peak_downloaded_per_second;
unsigned int m_peak_uploaded_per_second;
// current mean download/upload rates
unsigned int m_mean_download_per_second;
unsigned int m_mean_upload_per_second;
};
}
#endif // TORRENT_STAT_HPP_INCLUDED

174
include/libtorrent/storage.hpp Executable file
View File

@ -0,0 +1,174 @@
/*
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.
*/
#ifndef TORRENT_STORAGE_HPP_INCLUDE
#define TORRENT_STORAGE_HPP_INCLUDE
#include <ctime>
#include <iostream>
#include <ios>
#include <algorithm>
#include <vector>
#include <set>
#include <list>
#include <fstream>
#include <boost/limits.hpp>
#include <boost/filesystem/path.hpp>
#include "libtorrent/entry.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/socket.hpp"
#include "libtorrent/policy.hpp"
/*
* This file declares the following functions:
*
*----------------------------------
*
*
*/
namespace libtorrent
{
class session;
struct file_allocation_failed: std::exception
{
file_allocation_failed(const char* error_msg): m_msg(error_msg) {}
virtual const char* what() const throw() { return m_msg.c_str(); }
virtual ~file_allocation_failed() throw() {}
std::string m_msg;
};
// wraps access to pieces with a file-like interface
// TODO: add functionality for switching piece
// that's optimized not to closing the file if the
// new piece is in the same file
class piece_file
{
friend class storage;
public:
piece_file(): m_piece_index(-1), m_storage(0) {}
~piece_file()
{
if (m_piece_index >= 0) close();
}
enum open_mode { in, out };
// opens a piece with the given index from storage s
void open(storage* s, int index, open_mode m);
void close()
{
//std::cout << std::clock() << "close " << m_piece_index << "\n";
m_file.close();
m_piece_index = -1;
m_storage = 0;
}
void write(const char* buf, int size);
int read(char* buf, int size);
void seek_forward(int step);
// tells the position in the file
int tell() const { return m_piece_offset; }
int left() const { return m_piece_size - m_piece_offset; }
int index() const { return m_piece_index; }
private:
// the file itself
std::fstream m_file;
// the mode with which this file was opened
open_mode m_mode;
// the mode the fstream object was opened in
std::ios_base::openmode m_file_mode;
// file we're currently reading from/writing to
std::vector<file>::const_iterator m_file_iter;
// the position we're at in the current file
std::size_t m_file_offset;
// the byte offset in the current piece
std::size_t m_piece_offset;
// the size of the current piece
int m_piece_size;
// the index of the piece, -1 means the piece isn't open
int m_piece_index;
storage* m_storage;
};
class storage
{
friend class piece_file;
public:
void initialize_pieces(torrent* t, const boost::filesystem::path& path);
int bytes_left() const { return m_bytes_left; }
unsigned int num_pieces() const { return m_torrent_file->num_pieces(); }
bool have_piece(unsigned int index) const { return m_have_piece[index]; }
bool verify_piece(piece_file& index);
const std::vector<bool>& pieces() const { return m_have_piece; }
private:
// total number of bytes left to be downloaded
entry::integer_type m_bytes_left;
// the location where the downloaded file should be stored
boost::filesystem::path m_save_path;
// a bitmask representing the pieces we have
std::vector<bool> m_have_piece;
const torrent_info* m_torrent_file;
};
}
#endif // TORRENT_STORAGE_HPP_INCLUDED

288
include/libtorrent/torrent.hpp Executable file
View File

@ -0,0 +1,288 @@
/*
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.
*/
#ifndef TORRENT_TORRENT_HPP_INCLUDE
#define TORRENT_TORRENT_HPP_INCLUDE
#include <algorithm>
#include <vector>
#include <set>
#include <list>
#include <iostream>
#include <boost/limits.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include "libtorrent/entry.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/socket.hpp"
#include "libtorrent/policy.hpp"
#include "libtorrent/storage.hpp"
#include "libtorrent/url_handler.hpp"
namespace libtorrent
{
#ifndef NDEBUG
struct logger;
#endif
namespace detail
{
struct session_impl;
}
// TODO: each torrent should have a status string that
// reflects what's happening to it
// TODO: There should be a maximum number of peers that
// is maintained (if someone disconnects, try to connect to
// anotherone). There should also be a candidate slot where a
// new peer is tried for one minute, and if it has better ownload
// speed than one of the peers currently connected, it will be
// replaced to maximize bandwidth usage. It wil also have to
// depend on how many and which pieces the peers have.
// a torrent is a class that holds information
// for a specific download. It updates itself against
// the tracker
class torrent: public request_callback
{
public:
torrent(detail::session_impl* ses, const torrent_info& torrent_file);
~torrent()
{
int i = 0;
}
void abort() { m_abort = true; m_event = event_stopped; }
bool is_aborted() const { return m_abort; }
// returns the number of seconds left until we are to make
// another tracker-request
bool should_request() const throw()
{
boost::posix_time::time_duration d = m_next_request - boost::posix_time::second_clock::local_time();
return d.is_negative();
}
bool failed() const throw() { return !m_failed.empty(); }
const char* fail_reason() const throw() { return m_failed.c_str(); }
void print(std::ostream& os) const;
void allocate_files(const std::string& save_path)
{
m_storage.initialize_pieces(this, save_path);
m_picker.files_checked(m_storage.pieces());
#ifndef NDEBUG
m_picker.integrity_check(this);
#endif
}
void uploaded_bytes(int num_bytes) { assert(num_bytes > 0); m_bytes_uploaded += num_bytes; }
void downloaded_bytes(int num_bytes) { assert(num_bytes > 0); m_bytes_downloaded += num_bytes; }
int bytes_downloaded() const { return m_bytes_downloaded; }
int bytes_uploaded() const { return m_bytes_uploaded; }
int bytes_left() const { return m_storage.bytes_left(); }
// TODO: temporary implementation. Should count the actually
// verified pieces
float progress() const
{
return bytes_downloaded() / static_cast<float>(m_torrent_file.total_size());
}
void connect_to_peer(const address& a, const peer_id& id);
const torrent_info& torrent_file() const throw() { return m_torrent_file; }
policy& get_policy() { return *m_policy; }
storage* filesystem() { return &m_storage; }
// --------------------------------------------
// PEER MANAGEMENT
// used by peer_connection to attach itself to a torrent
// since incoming connections don't know what torrent
// they're a part of until they have received an info_hash.
void attach_peer(peer_connection* p)
{
assert(std::find(m_connections.begin(), m_connections.end(), p) == m_connections.end());
m_connections.push_back(p);
}
// this will remove the peer and make sure all
// the pieces it had have their reference counter
// decreased in the piece_picker
void remove_peer(peer_connection* p);
// the number of peers that belong to this torrent
int num_peers() const { return m_connections.size(); }
// returns the number of connections this torrent has to
// the given peer_id (should be kept at max 1)
int num_connections(const peer_id& id) const;
std::vector<peer_connection*>::const_iterator begin() const { return m_connections.begin(); }
std::vector<peer_connection*>::const_iterator end() const { return m_connections.end(); }
// --------------------------------------------
// TRACKER MANAGEMENT
// this is a callback called by the tracker_connection class
// when this torrent got a response from its tracker request
void tracker_response(const entry& e);
void tracker_request_timed_out()
{
std::cout << "TRACKER TIMED OUT\n";
try_next_tracker();
}
void tracker_request_error(const char* str)
{
std::cout << "TRACKER ERROR: " << str << "\n";
try_next_tracker();
}
// generates a request string for sending
// to the tracker
std::string generate_tracker_request(int port);
// --------------------------------------------
// PIECE MANAGEMENT
// returns true if we have downloaded the given piece
bool have_piece(unsigned int index) const { return m_storage.have_piece(index); }
// when we get a have- or bitfield- messages, this is called for every
// piece a peer has gained.
// returns true if this piece is interesting (i.e. if we would like to download it)
bool peer_has(int index)
{
return m_picker.inc_refcount(index);
}
// when peer disconnects, this is called for every piece it had
void peer_lost(int index)
{
m_picker.dec_refcount(index);
}
int block_size() const { return m_block_size; }
// this will tell all peers that we just got his piece
// and also let the piece picker know that we have this piece
// so it wont pick it for download
void announce_piece(int index);
void close_all_connections();
piece_picker& picker() { return m_picker; }
// DEBUG
#ifndef NDEBUG
logger* spawn_logger(const char* title);
#endif
private:
void try_next_tracker();
enum event_id
{
event_started = 0,
event_stopped,
event_completed,
event_none
};
// the size of a request block
// each piece is divided into these
// blocks when requested
int m_block_size;
bool m_abort;
event_id m_event;
void parse_response(const entry& e);
// total amount of bytes uploaded, downloaded and
// the number of bytes left to be downloaded
entry::integer_type m_bytes_uploaded;
entry::integer_type m_bytes_downloaded;
torrent_info m_torrent_file;
storage m_storage;
// the time of next tracker request
boost::posix_time::ptime m_next_request;
// -----------------------------
// DATA FROM TRACKER RESPONSE
// the number number of seconds between requests
// from the tracker
int m_duration;
std::string m_failed;
std::vector<peer_connection*> m_connections;
// TODO: move to a local function?
std::vector<peer> m_peer_list;
// -----------------------------
boost::shared_ptr<policy> m_policy;
detail::session_impl* m_ses;
piece_picker m_picker;
// this is an index into m_torrent_file.trackers()
int m_last_working_tracker;
int m_currently_trying_tracker;
};
}
#endif // TORRENT_TORRENT_HPP_INCLUDED

View File

@ -0,0 +1,153 @@
/*
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.
*/
#ifndef TORRENT_TORRENT_INFO_HPP_INCLUDE
#define TORRENT_TORRENT_INFO_HPP_INCLUDE
#include <string>
#include <vector>
#include "libtorrent/entry.hpp"
#include "libtorrent/socket.hpp"
#include "libtorrent/peer_id.hpp"
/*
* This file declares the following functions:
*
*
*
*
*/
namespace libtorrent
{
struct file
{
std::string path;
std::string filename;
entry::integer_type size;
};
struct announce_entry
{
std::string url;
int tier;
};
struct invalid_torrent_file: std::exception
{
virtual const char* what() const throw() { return "invalid torrent file"; }
};
class torrent_info
{
public:
torrent_info(const entry& torrent_file)
{
try
{
read_torrent_info(torrent_file);
}
catch(type_error&)
{
throw invalid_torrent_file();
}
}
typedef std::vector<file>::const_iterator file_iterator;
typedef std::vector<file>::const_reverse_iterator reverse_file_iterator;
// list the files in the torrent file
file_iterator begin_files() const { return m_files.begin(); }
file_iterator end_files() const { return m_files.end(); }
reverse_file_iterator rbegin_files() const { return m_files.rbegin(); }
reverse_file_iterator rend_files() const { return m_files.rend(); }
std::size_t num_files() const { return m_files.size(); }
const file& file_at(int index) const { assert(index >= 0 && index < m_files.size()); return m_files[index]; }
const std::vector<announce_entry>& trackers() const { return m_urls; }
// this will move the tracker with the given index
// to a prioritized position in the list (move it towards
// the begining) and return the new index to the tracker.
int prioritize_tracker(int index);
entry::integer_type total_size() const { return m_total_size; }
entry::integer_type piece_length() const { return m_piece_length; }
std::size_t num_pieces() const { return m_piece_hash.size(); }
const sha1_hash& info_hash() const { return m_info_hash; }
void print(std::ostream& os) const;
entry::integer_type piece_size(unsigned int index) const
{
if (index == num_pieces()-1)
return total_size() % m_piece_length;
else
return piece_length();
}
const sha1_hash& hash_for_piece(unsigned int index) const
{
assert(index < m_piece_hash.size());
return m_piece_hash[index];
}
private:
void read_torrent_info(const entry& libtorrent);
// the urls to the trackers
std::vector<announce_entry> m_urls;
// the length of one piece
entry::integer_type m_piece_length;
// the sha-1 hashes of each piece
std::vector<sha1_hash> m_piece_hash;
// the list of files that this torrent consists of
std::vector<file> m_files;
// the sum of all filesizes
entry::integer_type m_total_size;
// the hash that identifies this torrent
sha1_hash m_info_hash;
};
}
#endif // TORRENT_TORRENT_INFO_HPP_INCLUDED

174
src/entry.cpp Executable file
View File

@ -0,0 +1,174 @@
/*
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.
*/
#include "libtorrent/entry.hpp"
#if defined(_MSC_VER) && _MSV_CER < 1300
namespace std
{
using ::isprint;
}
#define for if (false) {} else for
#endif
namespace
{
template <class T>
void call_destructor(T* o)
{
o->~T();
}
}
void libtorrent::entry::construct(data_type t)
{
m_type = t;
switch(m_type)
{
case int_t:
new(data) integer_type;
break;
case string_t:
new(data) string_type;
break;
case list_t:
new(data) list_type;
break;
case dictionary_t:
new (data) dictionary_type;
break;
default:
m_type = undefined_t;
}
}
void libtorrent::entry::copy(const entry& e)
{
m_type = e.m_type;
switch(m_type)
{
case int_t:
new(data) integer_type(e.integer());
break;
case string_t:
new(data) string_type(e.string());
break;
case list_t:
new(data) list_type(e.list());
break;
case dictionary_t:
new (data) dictionary_type(e.dict());
break;
default:
m_type = undefined_t;
}
}
void libtorrent::entry::destruct()
{
switch(m_type)
{
case int_t:
call_destructor(reinterpret_cast<integer_type*>(data));
break;
case string_t:
call_destructor(reinterpret_cast<string_type*>(data));
break;
case list_t:
call_destructor(reinterpret_cast<list_type*>(data));
break;
case dictionary_t:
call_destructor(reinterpret_cast<dictionary_type*>(data));
break;
default:
break;
}
}
void libtorrent::entry::print(std::ostream& os, int indent) const
{
for (int i = 0; i < indent; ++i) os << " ";
switch (m_type)
{
case int_t:
os << integer() << "\n";
break;
case string_t:
{
bool binary_string = false;
for (std::string::const_iterator i = string().begin(); i != string().end(); ++i)
{
if (!std::isprint(static_cast<unsigned char>(*i)))
{
binary_string = true;
break;
}
}
if (binary_string)
{
os.unsetf(std::ios_base::dec);
os.setf(std::ios_base::hex);
for (std::string::const_iterator i = string().begin(); i != string().end(); ++i)
os << static_cast<unsigned int>((unsigned char)*i);
os.unsetf(std::ios_base::hex);
os.setf(std::ios_base::dec);
os << "\n";
}
else
{
os << string() << "\n";
}
} break;
case list_t:
{
os << "list\n";
for (list_type::const_iterator i = list().begin(); i != list().end(); ++i)
{
i->print(os, indent+1);
}
} break;
case dictionary_t:
{
os << "dictionary\n";
for (dictionary_type::const_iterator i = dict().begin(); i != dict().end(); ++i)
{
for (int j = 0; j < indent+1; ++j) os << " ";
os << "[" << i->first << "]";
if (i->second.type() != entry::string_t && i->second.type() != entry::int_t) os << "\n";
else os << " ";
i->second.print(os, indent+2);
}
} break;
default:
os << "<uninitialized>\n";
}
}

894
src/peer_connection.cpp Executable file
View File

@ -0,0 +1,894 @@
/*
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.
*/
#include <iostream>
#include <iomanip>
#include <vector>
#include "libtorrent/peer_connection.hpp"
#include "libtorrent/session.hpp"
#if defined(_MSC_VER) && _MSC_VER < 1300
#define for if (false) {} else for
#endif
#define VERBOSE
using namespace libtorrent;
namespace
{
// reads an integer from a byte stream
// in big endian byte order and converts
// it to native endianess
unsigned int read_int(const char* buf)
{
unsigned int val = 0;
val |= static_cast<unsigned char>(buf[0]) << 24;
val |= static_cast<unsigned char>(buf[1]) << 16;
val |= static_cast<unsigned char>(buf[2]) << 8;
val |= static_cast<unsigned char>(buf[3]);
return val;
}
void write_int(unsigned int val, char* buf)
{
buf[0] = static_cast<unsigned char>(val >> 24);
buf[1] = static_cast<unsigned char>(val >> 16);
buf[2] = static_cast<unsigned char>(val >> 8);
buf[3] = static_cast<unsigned char>(val);
}
}
libtorrent::peer_connection::peer_connection(detail::session_impl* ses, torrent* t, boost::shared_ptr<libtorrent::socket> s, const peer_id& p)
: m_state(read_protocol_length)
, m_timeout(120)
, m_packet_size(1)
, m_recv_pos(0)
, m_last_receive(std::time(0))
, m_last_sent(std::time(0))
, m_socket(s)
, m_torrent(t)
, m_ses(ses)
, m_active(true)
, m_peer_id(p)
, m_peer_interested(false)
, m_peer_choked(true)
, m_interesting(false)
, m_choked(true)
{
assert(m_torrent != 0);
#ifndef NDEBUG
m_logger = boost::shared_ptr<logger>(
m_torrent->spawn_logger(s->sender().as_string().c_str()));
#endif
send_handshake();
// start in the state where we are trying to read the
// handshake from the other side
m_recv_buffer.resize(1);
// assume the other end has no pieces
m_have_piece.resize(m_torrent->torrent_file().num_pieces());
std::fill(m_have_piece.begin(), m_have_piece.end(), false);
send_bitfield();
}
libtorrent::peer_connection::peer_connection(detail::session_impl* ses, boost::shared_ptr<libtorrent::socket> s)
: m_state(read_protocol_length)
, m_timeout(120)
, m_packet_size(1)
, m_recv_pos(0)
, m_last_receive(std::time(0))
, m_last_sent(std::time(0))
, m_socket(s)
, m_torrent(0)
, m_ses(ses)
, m_active(false)
, m_peer_id()
, m_peer_interested(false)
, m_peer_choked(true)
, m_interesting(false)
, m_choked(true)
{
#ifndef NDEBUG
m_logger = boost::shared_ptr<logger>(
m_ses->m_log_spawner->create_logger(s->sender().as_string().c_str()));
#endif
// we are not attached to any torrent yet.
// we have to wait for the handshake to see
// which torrent the connector want's to connect to
// start in the state where we are trying to read the
// handshake from the other side
m_recv_buffer.resize(1);
}
libtorrent::peer_connection::~peer_connection()
{
m_receiving_piece.close();
if (m_torrent) m_torrent->remove_peer(this);
}
void libtorrent::peer_connection::send_handshake()
{
assert(m_send_buffer.size() == 0);
// add handshake to the send buffer
m_send_buffer.resize(68);
const char* version_string = "BitTorrent protocol";
m_send_buffer[0] = 19;
std::copy(version_string, version_string+19, m_send_buffer.begin()+1);
std::fill(m_send_buffer.begin() + 20, m_send_buffer.begin() + 28, 0);
std::copy(m_torrent->torrent_file().info_hash().begin(), m_torrent->torrent_file().info_hash().end(), m_send_buffer.begin() + 28);
std::copy(m_ses->get_peer_id().begin(), m_ses->get_peer_id().end(), m_send_buffer.begin() + 48);
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " ==> HANDSHAKE\n";
#endif
}
bool libtorrent::peer_connection::dispatch_message()
{
int packet_type = m_recv_buffer[0];
if (packet_type > 8 || packet_type < 0)
return false;
switch (packet_type)
{
// *************** CHOKE ***************
case msg_choke:
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " <== CHOKE\n";
#endif
m_peer_choked = true;
m_torrent->get_policy().choked(*this);
// remove all pieces from this peers download queue and
// remove the 'downloading' flag from piece_picker.
for (std::vector<piece_block>::iterator i = m_download_queue.begin();
i != m_download_queue.end();
++i)
{
m_torrent->picker().abort_download(*i);
}
m_download_queue.clear();
#ifndef NDEBUG
m_torrent->picker().integrity_check(m_torrent);
#endif
break;
// *************** UNCHOKE ***************
case msg_unchoke:
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " <== UNCHOKE\n";
#endif
m_peer_choked = false;
m_torrent->get_policy().unchoked(*this);
break;
// *************** INTERESTED ***************
case msg_interested:
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " <== INTERESTED\n";
#endif
m_peer_interested = true;
m_torrent->get_policy().interested(*this);
break;
// *************** NOT INTERESTED ***************
case msg_not_interested:
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " <== NOT_INTERESTED\n";
#endif
m_peer_interested = false;
m_torrent->get_policy().not_interested(*this);
break;
// *************** HAVE ***************
case msg_have:
{
std::size_t index = read_int(&m_recv_buffer[1]);
// if we got an invalid message, abort
if (index >= m_have_piece.size())
return false;
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " <== HAVE [ piece: " << index << "]\n";
#endif
if (m_have_piece[index])
{
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " oops.. we already knew that: " << index << "\n";
#endif
}
else
{
m_have_piece[index] = true;
if (m_torrent->peer_has(index))
m_torrent->get_policy().peer_is_interesting(*this);
}
break;
}
// *************** BITFIELD ***************
case msg_bitfield:
{
if (m_packet_size - 1 != (m_have_piece.size() + 7) / 8)
return false;
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " <== BITFIELD\n";
#endif
bool interesting = false;
bool is_seed = true;
for (std::size_t i = 0; i < m_have_piece.size(); ++i)
{
bool have = m_recv_buffer[1 + (i>>3)] & (1 << (7 - (i&7)));
if (have && !m_have_piece[i])
{
m_have_piece[i] = true;
if (m_torrent->peer_has(i)) interesting = true;
}
else if (!have && m_have_piece[i])
{
m_have_piece[i] = false;
m_torrent->peer_lost(i);
}
if (!have) is_seed = false;
}
if (is_seed)
{
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " *** THIS IS A SEED ***\n";
#endif
}
if (interesting) m_torrent->get_policy().peer_is_interesting(*this);
break;
}
// *************** REQUEST ***************
case msg_request:
{
peer_request r;
r.piece = read_int(&m_recv_buffer[1]);
r.start = read_int(&m_recv_buffer[5]);
r.length = read_int(&m_recv_buffer[9]);
m_requests.push_back(r);
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " <== REQUEST [ piece: " << r.piece << " | s: " << r.start << " | l: " << r.length << " ]\n";
#endif
break;
}
// *************** PIECE ***************
case msg_piece:
{
std::size_t index = read_int(&m_recv_buffer[1]);
if (index < 0 || index >= m_torrent->torrent_file().num_pieces())
{
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " piece index invalid\n";
#endif
return false;
}
int offset = read_int(&m_recv_buffer[5]);
int len = m_packet_size - 9;
if (offset < 0)
{
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " offset < 0\n";
#endif
return false;
}
if (offset + len > m_torrent->torrent_file().piece_size(index))
{
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " piece packet contains more data than the piece size\n";
#endif
return false;
}
if (offset % m_torrent->block_size() != 0)
{
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " piece packet contains unaligned offset\n";
#endif
return false;
}
piece_block req = m_download_queue.front();
if (req.piece_index != index)
{
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " piece packet contains unrequested index\n";
#endif
return false;
}
if (req.block_index != offset / m_torrent->block_size())
{
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " piece packet contains unrequested offset\n";
#endif
return false;
}
// if (m_receiving_piece.index() < 0 || m_receiving_piece.index() != index)
// {
m_receiving_piece.open(m_torrent->filesystem(), index, piece_file::out);
// }
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " <== PIECE [ piece: " << index << " | s: " << offset << " | l: " << len << " ]\n";
#endif
m_receiving_piece.seek_forward(offset);
m_receiving_piece.write(&m_recv_buffer[9], len);
m_torrent->downloaded_bytes(len);
piece_picker& picker = m_torrent->picker();
piece_block block_finished(index, offset / m_torrent->block_size());
picker.mark_as_finished(block_finished);
// pop the request that just finished
// from the download queue
m_download_queue.erase(m_download_queue.begin());
// did we just finish the piece?
if (picker.is_piece_finished(index))
{
bool verified = m_torrent->filesystem()->verify_piece(m_receiving_piece);
if (verified)
{
m_torrent->announce_piece(index);
}
else
{
// TODO: we have to let the piece_picker know that
// this piece failed the check os it can restore it
// and mark it as being interesting for download
picker.restore_piece(index);
}
m_torrent->get_policy().piece_finished(*this, index, verified);
}
m_torrent->get_policy().block_finished(*this, block_finished);
break;
}
// *************** CANCEL ***************
case msg_cancel:
{
peer_request r;
r.piece = read_int(&m_recv_buffer[1]);
r.start = read_int(&m_recv_buffer[5]);
r.length = read_int(&m_recv_buffer[9]);
std::vector<peer_request>::iterator i
= std::find(m_requests.begin(), m_requests.end(), r);
if (i != m_requests.end())
{
m_requests.erase(i);
}
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " <== CANCEL [ piece: " << r.piece << " | s: " << r.start << " | l: " << r.length << " ]\n";
#endif
m_requests.clear();
break;
}
}
return true;
}
void libtorrent::peer_connection::request_block(piece_block block)
{
assert(block.piece_index >= 0);
assert(block.piece_index < m_torrent->torrent_file().num_pieces());
assert(!m_torrent->picker().is_downloading(block));
m_torrent->picker().mark_as_downloading(block);
m_download_queue.push_back(block);
int block_offset = block.block_index * m_torrent->block_size();
int block_size
= std::min((int)m_torrent->torrent_file().piece_size(block.piece_index)-block_offset,
m_torrent->block_size());
assert(block_size > 0);
assert(block_size <= m_torrent->block_size());
char buf[] = {0,0,0,13, msg_request};
std::size_t start_offset = m_send_buffer.size();
m_send_buffer.resize(start_offset + 17);
// TODO: add a timeout to disconnect peer if we don't get any piece messages when
// we have requested.
// TODO: there must be a way to ask the storage if a given
// piece already has some data (and how much). To avoid requesting
std::copy(buf, buf + 5, m_send_buffer.begin()+start_offset);
start_offset +=5;
// index
write_int(block.piece_index, &m_send_buffer[start_offset]);
start_offset += 4;
// begin
write_int(block_offset, &m_send_buffer[start_offset]);
start_offset += 4;
// length
write_int(block_size, &m_send_buffer[start_offset]);
start_offset += 4;
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " ==> REQUEST [ piece: " << block.piece_index << " | s: " << block_offset << " | l: " << block_size << " | " << block.block_index << " ]\n";
#endif
assert(start_offset == m_send_buffer.size());
}
void libtorrent::peer_connection::send_bitfield()
{
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " ==> BITFIELD\n";
#endif
const int packet_size = (m_have_piece.size() + 7) / 8 + 5;
const int old_size = m_send_buffer.size();
m_send_buffer.resize(old_size + packet_size);
write_int(packet_size - 4, &m_send_buffer[old_size]);
m_send_buffer[old_size+4] = msg_bitfield;
std::fill(m_send_buffer.begin()+old_size+5, m_send_buffer.end(), 0);
for (std::size_t i = 0; i < m_have_piece.size(); ++i)
{
if (m_torrent->have_piece(i))
m_send_buffer[old_size + 5 + (i>>3)] |= 1 << (7 - (i&7));
}
}
void libtorrent::peer_connection::choke()
{
if (m_choked) return;
char msg[] = {0,0,0,1,msg_choke};
m_send_buffer.insert(m_send_buffer.end(), msg, msg+sizeof(msg));
m_choked = true;
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " ==> CHOKE\n";
#endif
}
void libtorrent::peer_connection::unchoke()
{
if (!m_choked) return;
char msg[] = {0,0,0,1,msg_unchoke};
m_send_buffer.insert(m_send_buffer.end(), msg, msg+sizeof(msg));
m_choked = false;
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " ==> UNCHOKE\n";
#endif
}
void libtorrent::peer_connection::interested()
{
if (m_interesting) return;
char msg[] = {0,0,0,1,msg_interested};
m_send_buffer.insert(m_send_buffer.end(), msg, msg+sizeof(msg));
m_interesting = true;
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " ==> INTERESTED\n";
#endif
}
void libtorrent::peer_connection::not_interested()
{
if (!m_interesting) return;
char msg[] = {0,0,0,1,msg_not_interested};
m_send_buffer.insert(m_send_buffer.end(), msg, msg+sizeof(msg));
m_interesting = false;
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " ==> NOT_INTERESTED\n";
#endif
}
void libtorrent::peer_connection::send_have(int index)
{
char msg[9] = {0,0,0,5,msg_have};
write_int(index, msg+5);
m_send_buffer.insert(m_send_buffer.end(), msg, msg+9);
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " ==> HAVE [ piece: " << index << " ]\n";
#endif
}
// --------------------------
// RECEIVE DATA
// --------------------------
// throws exception when the client should be disconnected
void libtorrent::peer_connection::receive_data()
{
int received = m_socket->receive(&m_recv_buffer[m_recv_pos], m_packet_size - m_recv_pos);
// (*m_logger) << "<== RECV [ size: " << received << " ]\n";
if (received == 0) throw network_error(0);
if (received < 0)
{
if (m_socket->last_error() == socket::would_block) return;
// the connection was closed
throw network_error(0);
}
if (received > 0)
{
m_statistics.received_bytes(received);
m_last_receive = boost::posix_time::second_clock::local_time();
m_recv_pos += received;
if (m_recv_pos == m_packet_size)
{
switch(m_state)
{
case read_protocol_length:
m_packet_size = reinterpret_cast<unsigned char&>(m_recv_buffer[0]);
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " protocol length: " << m_packet_size << "\n";
#endif
m_state = read_protocol_version;
if (m_packet_size != 19)
throw network_error(0);
m_recv_buffer.resize(m_packet_size);
m_recv_pos = 0;
break;
case read_protocol_version:
{
const char* protocol_version = "BitTorrent protocol";
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " protocol name: " << std::string(m_recv_buffer.begin(), m_recv_buffer.end()) << "\n";
#endif
if (!std::equal(m_recv_buffer.begin(), m_recv_buffer.end(), protocol_version))
{
// unknown protocol, close connection
throw network_error(0);
}
m_state = read_info_hash;
m_packet_size = 28;
m_recv_pos = 0;
m_recv_buffer.resize(28);
}
break;
case read_info_hash:
{
// ok, now we have got enough of the handshake. Is this connection
// attached to a torrent?
if (m_torrent == 0)
{
// no, we have to see if there's a torrent with the
// info_hash we got from the peer
sha1_hash info_hash;
std::copy(m_recv_buffer.begin()+8, m_recv_buffer.begin() + 28, (char*)info_hash.begin());
m_torrent = m_ses->find_torrent(info_hash);
if (m_torrent == 0)
{
// we couldn't find the torrent!
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " couldn't find a torrent with the given info_hash\n";
#endif
throw network_error(0);
}
m_torrent->attach_peer(this);
// assume the other end has no pieces
m_have_piece.resize(m_torrent->torrent_file().num_pieces());
std::fill(m_have_piece.begin(), m_have_piece.end(), false);
// yes, we found the torrent
// reply with our handshake
std::copy(m_recv_buffer.begin()+28, m_recv_buffer.begin() + 48, (char*)m_peer_id.begin());
send_handshake();
send_bitfield();
}
else
{
// verify info hash
if (!std::equal(m_recv_buffer.begin()+8, m_recv_buffer.begin() + 28, (const char*)m_torrent->torrent_file().info_hash().begin()))
{
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " received invalid info_hash\n";
#endif
throw network_error(0);
}
}
m_state = read_peer_id;
m_packet_size = 20;
m_recv_pos = 0;
m_recv_buffer.resize(20);
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " info_hash received\n";
#endif
break;
}
case read_peer_id:
{
if (m_active)
{
// verify peer_id
// TODO: It seems like the original client ignores to check the peer id
if (!std::equal(m_recv_buffer.begin(), m_recv_buffer.begin() + 20, (const char*)m_peer_id.begin()))
{
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " invalid peer_id (it doesn't equal the one from the tracker)\n";
#endif
throw network_error(0);
}
}
else
{
// check to make sure we don't have another connection with the same
// info_hash and peer_id. If we do. close this connection.
std::copy(m_recv_buffer.begin(), m_recv_buffer.begin() + 20, (char*)m_peer_id.begin());
if (m_torrent->num_connections(m_peer_id) > 1)
{
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " duplicate connection, closing\n";
#endif
throw network_error(0);
}
}
m_state = read_packet_size;
m_packet_size = 4;
m_recv_pos = 0;
m_recv_buffer.resize(4);
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " received peer_id\n";
#endif
break;
}
case read_packet_size:
// convert from big endian to native byte order
m_packet_size = read_int(&m_recv_buffer[0]);
// don't accept packets larger than 1 MB
if (m_packet_size > 1024*1024 || m_packet_size < 0)
{
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " packet too large (packet_size > 1 Megabyte), abort\n";
#endif
// packet too large
throw network_error(0);
}
if (m_packet_size == 0)
{
// keepalive message
m_state = read_packet_size;
m_packet_size = 4;
}
else
{
m_state = read_packet;
m_recv_buffer.resize(m_packet_size);
}
m_recv_pos = 0;
break;
case read_packet:
if (!dispatch_message())
{
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " received invalid packet\n";
#endif
// invalid message
throw network_error(0);
}
m_state = read_packet_size;
m_packet_size = 4;
m_recv_buffer.resize(4);
m_recv_pos = 0;
break;
}
}
}
}
bool libtorrent::peer_connection::has_data() const throw()
{
// if we have requests or pending data to be sent or announcements to be made
// we want to send data
return !m_requests.empty() || !m_send_buffer.empty() || !m_announce_queue.empty();
}
// --------------------------
// SEND DATA
// --------------------------
// throws exception when the client should be disconnected
void libtorrent::peer_connection::send_data()
{
// only add new piece-chunks if the send buffer is empty
// otherwise there will be no end to how large it will be!
if (!m_requests.empty() && m_send_buffer.empty() && m_peer_interested && !m_choked)
{
peer_request& r = m_requests.front();
if (r.piece >= 0 && r.piece < m_have_piece.size() && m_torrent && m_torrent->have_piece(r.piece))
{
// make sure the request is ok
if (r.start + r.length > m_torrent->torrent_file().piece_size(r.piece))
{
// NOT OK! disconnect
throw network_error(0);
}
if (m_sending_piece.index() != r.piece)
{
m_sending_piece.open(m_torrent->filesystem(), r.piece, piece_file::in);
#ifndef NDEBUG
assert(m_torrent->filesystem()->verify_piece(m_sending_piece) && "internal error");
m_sending_piece.open(m_torrent->filesystem(), r.piece, piece_file::in);
#endif
}
const int packet_size = 4 + 5 + 4 + r.length;
m_send_buffer.resize(packet_size);
write_int(packet_size-4, &m_send_buffer[0]);
m_send_buffer[4] = msg_piece;
write_int(r.piece, &m_send_buffer[5]);
write_int(r.start, &m_send_buffer[9]);
if (r.start > m_sending_piece.tell())
{
m_sending_piece.seek_forward(r.start - m_sending_piece.tell());
}
else if (r.start < m_sending_piece.tell())
{
int index = m_sending_piece.index();
m_sending_piece.close();
m_sending_piece.open(m_torrent->filesystem(), index, piece_file::in);
m_sending_piece.seek_forward(r.start);
}
m_sending_piece.read(&m_send_buffer[13], r.length);
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " ==> PIECE [ idx: " << r.piece << " | s: " << r.start << " | l: " << r.length << " | dest: " << m_socket->sender().as_string() << " ]\n";
#endif
// let the torrent keep track of how much we have uploaded
m_torrent->uploaded_bytes(r.length);
m_requests.erase(m_requests.begin());
}
else
{
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " *** WARNING [ illegal piece request ]\n";
#endif
}
}
if (!m_announce_queue.empty())
{
for (std::vector<int>::iterator i = m_announce_queue.begin(); i != m_announce_queue.end(); ++i)
{
// (*m_logger) << "have piece: " << *i << " sent to: " << m_socket->sender().as_string() << "\n";
send_have(*i);
}
m_announce_queue.clear();
}
// send the actual buffer
if (!m_send_buffer.empty())
{
// we have data that's scheduled for sending
std::size_t sent = m_socket->send(&m_send_buffer[0], m_send_buffer.size());
#ifndef NDEBUG
(*m_logger) << m_socket->sender().as_string() << " ==> SENT [ length: " << sent << " ]\n";
#endif
if (sent > 0)
{
m_statistics.sent_bytes(sent);
// empty the entire buffer at once or if
// only a part of the buffer could be sent
// remove the part that was sent from the buffer
if (sent == m_send_buffer.size())
m_send_buffer.clear();
else
m_send_buffer.erase(m_send_buffer.begin(), m_send_buffer.begin() + sent);
}
m_last_sent = boost::posix_time::second_clock::local_time();
}
}
void libtorrent::peer_connection::keep_alive()
{
boost::posix_time::time_duration d;
d = boost::posix_time::second_clock::local_time() - m_last_sent;
if (d.seconds() > m_timeout / 2)
{
char noop[] = {0,0,0,0};
m_send_buffer.insert(m_send_buffer.end(), noop, noop+4);
m_last_sent = boost::posix_time::second_clock::local_time();
#if !defined(NDEBUG) && defined(VERBOSE)
(*m_logger) << m_socket->sender().as_string() << " ==> NOP\n";
#endif
}
}

564
src/piece_picker.cpp Executable file
View File

@ -0,0 +1,564 @@
/*
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.
*/
#include <vector>
#include <cmath>
#include "libtorrent/piece_picker.hpp"
#ifndef NDEBUG
#include "libtorrent/torrent.hpp"
#include "libtorrent/peer_connection.hpp"
#endif
#if defined(_MSC_VER) && _MSC_VER < 1300
#define for if (false) {} else for
#endif
namespace libtorrent
{
piece_picker::piece_picker(int blocks_per_piece, int total_num_blocks)
: m_piece_info(2)
, m_downloading_piece_info(2)
, m_piece_map((total_num_blocks + blocks_per_piece-1) / blocks_per_piece)
{
m_blocks_per_piece = blocks_per_piece;
m_blocks_in_last_piece = total_num_blocks % blocks_per_piece;
if (m_blocks_in_last_piece == 0) m_blocks_in_last_piece = blocks_per_piece;
assert(m_blocks_per_piece <= max_blocks_per_piece);
assert(m_blocks_in_last_piece <= m_blocks_per_piece);
assert(m_blocks_in_last_piece <= max_blocks_per_piece);
// allocate the piece_map to cover all pieces
// and make them invalid (as if though we already had every piece)
std::fill(m_piece_map.begin(), m_piece_map.end(), piece_pos(0, 0xffffff));
}
void piece_picker::files_checked(const std::vector<bool>& pieces)
{
for (std::vector<bool>::const_iterator i = pieces.begin();
i != pieces.end();
++i)
{
if (*i) continue;
int index = i - pieces.begin();
assert(index < m_piece_map.size());
assert(m_piece_map[index].index == 0xffffff);
int peer_count = m_piece_map[index].peer_count;
assert(peer_count == 0);
assert(m_piece_info.size() == 2);
m_piece_map[index].index = m_piece_info[peer_count].size();
m_piece_info[peer_count].push_back(index);
}
#ifndef NDEBUG
integrity_check();
#endif
// TODO: random_shuffle
}
// TODO: add a check to make sure m_downloads is intact
#ifndef NDEBUG
void piece_picker::integrity_check(const torrent* t) const
{
assert(sizeof(piece_pos) == 4);
if (t != 0)
assert(m_piece_map.size() == t->torrent_file().num_pieces());
int last_val = 0;
for (std::vector<piece_pos>::const_iterator i = m_piece_map.begin();
i != m_piece_map.end();
++i)
{
int index = i - m_piece_map.begin();
if (t != 0)
{
int actual_peer_count = 0;
for (std::vector<peer_connection*>::const_iterator peer = t->begin();
peer != t->end();
++peer)
{
if ((*peer)->has_piece(index)) actual_peer_count++;
}
assert(i->peer_count == actual_peer_count);
/*
int num_downloaders = 0;
for (std::vector<peer_connection*>::const_iterator peer = t->begin();
peer != t->end();
++peer)
{
const std::vector<piece_block>& queue = (*peer)->download_queue();
if (std::find_if(queue.begin(), queue.end(), has_index(index)) == queue.end()) continue;
++num_downloaders;
}
if (i->downloading)
{
assert(num_downloaders == 1);
}
else
{
assert(num_downloaders == 0);
}
*/
}
if (i->index == 0xffffff)
{
assert(t == 0 || t->have_piece(index));
assert(i->downloading == 0);
// make sure there's no entry
// with this index. (there shouldn't
// be since the piece_map is 0xffffff)
for (std::vector<std::vector<int> >::const_iterator i = m_piece_info.begin();
i != m_piece_info.end();
++i)
{
for (std::vector<int>::const_iterator j= i->begin();
j != i->end();
++j)
{
assert(*j != index);
}
}
for (std::vector<std::vector<int> >::const_iterator i = m_downloading_piece_info.begin();
i != m_downloading_piece_info.end();
++i)
{
for (std::vector<int>::const_iterator j = i->begin();
j != i->end();
++j)
{
assert(*j != index);
}
}
}
else
{
if (t != 0)
assert(!t->have_piece(index));
const std::vector<std::vector<int> >& c_vec = (i->downloading)?m_downloading_piece_info:m_piece_info;
assert(i->peer_count < c_vec.size());
const std::vector<int>& vec = c_vec[i->peer_count];
assert(i->index < vec.size());
assert(vec[i->index] == index);
}
std::vector<downloading_piece>::const_iterator down
= std::find_if(m_downloads.begin(),
m_downloads.end(),
has_index(index));
if (i->downloading == 1)
{
assert(down != m_downloads.end());
}
else
{
assert(down == m_downloads.end());
}
}
}
#endif
void piece_picker::move(bool downloading, int peer_count, int elem_index)
{
std::vector<std::vector<int> >& src_vec = (downloading)?m_downloading_piece_info:m_piece_info;
assert(src_vec.size() > peer_count);
assert(src_vec[peer_count].size() > elem_index);
int index = src_vec[peer_count][elem_index];
// update the piece_map
piece_pos& p = m_piece_map[index];
assert(p.downloading != downloading || p.peer_count != peer_count);
std::vector<std::vector<int> >& dst_vec = (p.downloading)?m_downloading_piece_info:m_piece_info;
if (dst_vec.size() <= p.peer_count)
{
dst_vec.resize(p.peer_count+1);
assert(dst_vec.size() > p.peer_count);
}
p.index = dst_vec[p.peer_count].size();
dst_vec[p.peer_count].push_back(index);
assert(p.index < dst_vec[p.peer_count].size());
assert(dst_vec[p.peer_count][p.index] == index);
// this will remove elem from the source vector without
// preserving order
int replace_index = src_vec[peer_count][elem_index] = src_vec[peer_count].back();
if (index != replace_index)
{
// update the entry we moved from the back
m_piece_map[replace_index].index = elem_index;
assert(src_vec[peer_count].size() > elem_index);
assert(m_piece_map[replace_index].peer_count == peer_count);
assert(m_piece_map[replace_index].index == elem_index);
assert(src_vec[peer_count][elem_index] == replace_index);
}
else
{
assert(src_vec[peer_count].size() == elem_index+1);
}
src_vec[peer_count].pop_back();
}
void piece_picker::remove(bool downloading, int peer_count, int elem_index)
{
std::vector<std::vector<int> >& src_vec = (downloading)?m_downloading_piece_info:m_piece_info;
assert(src_vec.size() > peer_count);
assert(src_vec[peer_count].size() > elem_index);
int index = src_vec[peer_count][elem_index];
m_piece_map[index].index = 0xffffff;
if (downloading)
{
std::vector<downloading_piece>::iterator i
= std::find_if(m_downloads.begin(),
m_downloads.end(),
has_index(index));
assert(i != m_downloads.end());
m_downloads.erase(i);
}
m_piece_map[index].downloading = 0;
// this will remove elem from the vector without
// preserving order
index = src_vec[peer_count][elem_index] = src_vec[peer_count].back();
// update the entry we moved from the back
if (src_vec[peer_count].size() > elem_index+1)
m_piece_map[index].index = elem_index;
src_vec[peer_count].pop_back();
}
void piece_picker::restore_piece(int index)
{
assert(index >= 0);
assert(index < m_piece_map.size());
assert(m_piece_map[index].downloading == 1);
std::vector<downloading_piece>::iterator i
= std::find_if(m_downloads.begin(),
m_downloads.end(),
has_index(index));
assert(i != m_downloads.end());
m_downloads.erase(i);
m_piece_map[index].downloading = 0;
move(true, m_piece_map[index].peer_count, m_piece_map[index].index);
#ifndef NDEBUG
integrity_check();
#endif
}
bool piece_picker::inc_refcount(int i)
{
assert(i >= 0);
assert(i < m_piece_map.size());
int peer_count = m_piece_map[i].peer_count;
int index = m_piece_map[i].index;
m_piece_map[i].peer_count++;
if (index == 0xffffff) return false;
move(m_piece_map[i].downloading, peer_count, index);
#ifndef NDEBUG
// integrity_check();
#endif
return true;
}
void piece_picker::dec_refcount(int i)
{
assert(i >= 0);
assert(i < m_piece_map.size());
int peer_count = m_piece_map[i].peer_count;
int index = m_piece_map[i].index;
assert(m_piece_map[i].peer_count > 0);
m_piece_map[i].peer_count--;
if (index == 0xffffff) return;
move(m_piece_map[i].downloading, peer_count, index);
#ifndef NDEBUG
integrity_check();
#endif
}
void piece_picker::we_have(int index)
{
assert(index < m_piece_map.size());
int info_index = m_piece_map[index].index;
int peer_count = m_piece_map[index].peer_count;
assert(m_piece_map[index].downloading == 1);
assert(info_index != 0xffffff);
remove(m_piece_map[index].downloading, peer_count, info_index);
#ifndef NDEBUG
integrity_check();
#endif
}
void piece_picker::pick_pieces(const std::vector<bool>& pieces, std::vector<piece_block>& interesting_pieces) const
{
// TODO: modify this!
assert(pieces.size() == m_piece_map.size());
// free refers to pieces that are free to download, noone else
// is downloading them.
// partial is pieces that are partially being downloaded, and
// parts of them may be free for download as well, the
// partially donloaded pieces will be prioritized
std::vector<std::vector<int> >::const_iterator free = m_piece_info.begin()+1;
std::vector<std::vector<int> >::const_iterator partial = m_downloading_piece_info.begin()+1;
while((free != m_piece_info.end()) || (partial != m_downloading_piece_info.end()))
{
if (partial != m_downloading_piece_info.end())
{
for (int i = 0; i < 2; ++i)
{
if (add_interesting_blocks(*partial, pieces, interesting_pieces))
{
return;
}
++partial;
if (partial == m_downloading_piece_info.end()) break;
}
}
if (free != m_piece_info.end())
{
if (add_interesting_blocks(*free, pieces, interesting_pieces))
{
return;
}
++free;
}
}
}
bool piece_picker::add_interesting_blocks(const std::vector<int>& piece_list,
const std::vector<bool>& pieces,
std::vector<piece_block>& interesting_blocks) const
{
for (std::vector<int>::const_iterator i = piece_list.begin();
i != piece_list.end();
++i)
{
assert(*i < m_piece_map.size());
// if the peer doesn't have the piece
// skip it
if (!pieces[*i]) continue;
// if there's at least one block that
// we can request from this peer
// we can break our search (return)
if (m_piece_map[*i].downloading == 0)
{
interesting_blocks.push_back(piece_block(*i, 0));
return true; // we have found a piece that's free to download
}
// calculate the number of blocks in this
// piece. It's always m_blocks_per_piece, except
// in the last piece.
int num_blocks_in_piece
= (*i == m_piece_map.size()-1)?m_blocks_in_last_piece:m_blocks_per_piece;
std::vector<downloading_piece>::const_iterator p
= std::find_if(m_downloads.begin(), m_downloads.end(), has_index(*i));
assert(p != m_downloads.end());
for (int j = 0; j < num_blocks_in_piece; ++j)
{
interesting_blocks.push_back(piece_block(*i, j));
if (p->requested_blocks[j] == 0)
return true; // we have found a piece that's free to download
}
}
return false;
}
bool piece_picker::is_piece_finished(int index) const
{
assert(index < m_piece_map.size());
assert(index >= 0);
if (m_piece_map[index].downloading == 0) return false;
std::vector<downloading_piece>::const_iterator i
= std::find_if(m_downloads.begin(), m_downloads.end(), has_index(index));
assert(i != m_downloads.end());
assert(i->finished_blocks.count() <= m_blocks_per_piece);
int max_blocks = m_blocks_per_piece;
if (index+1 == m_piece_map.size()) max_blocks = m_blocks_in_last_piece;
if (i->finished_blocks.count() != max_blocks) return false;
assert(i->requested_blocks.count() == max_blocks);
return true;
}
bool piece_picker::is_downloading(piece_block block) const
{
assert(block.piece_index < m_piece_map.size());
assert(block.block_index < max_blocks_per_piece);
if (m_piece_map[block.piece_index].downloading == 0) return false;
std::vector<downloading_piece>::const_iterator i
= std::find_if(m_downloads.begin(), m_downloads.end(), has_index(block.piece_index));
assert(i != m_downloads.end());
return i->requested_blocks[block.block_index];
}
void piece_picker::mark_as_downloading(piece_block block)
{
#ifndef NDEBUG
integrity_check();
#endif
assert(block.piece_index < m_piece_map.size());
assert(block.block_index < max_blocks_per_piece);
piece_pos& p = m_piece_map[block.piece_index];
if (p.downloading == 0)
{
p.downloading = 1;
move(false, p.peer_count, p.index);
downloading_piece dp;
dp.index = block.piece_index;
dp.requested_blocks[block.block_index] = 1;
m_downloads.push_back(dp);
}
else
{
std::vector<downloading_piece>::iterator i
= std::find_if(m_downloads.begin(), m_downloads.end(), has_index(block.piece_index));
assert(i != m_downloads.end());
assert(i->requested_blocks[block.block_index] == 0);
i->requested_blocks[block.block_index] = 1;
}
#ifndef NDEBUG
integrity_check();
#endif
}
void piece_picker::mark_as_finished(piece_block block)
{
#ifndef NDEBUG
integrity_check();
#endif
assert(block.piece_index < m_piece_map.size());
assert(block.block_index < max_blocks_per_piece);
assert(m_piece_map[block.piece_index].downloading == 1);
std::vector<downloading_piece>::iterator i
= std::find_if(m_downloads.begin(), m_downloads.end(), has_index(block.piece_index));
assert(i != m_downloads.end());
assert(i->requested_blocks[block.block_index] == 1);
i->finished_blocks[block.block_index] = 1;
#ifndef NDEBUG
integrity_check();
#endif
}
void piece_picker::abort_download(piece_block block)
{
#ifndef NDEBUG
integrity_check();
#endif
assert(block.piece_index < m_piece_map.size());
assert(block.block_index < max_blocks_per_piece);
if (m_piece_map[block.piece_index].downloading == 0)
{
assert(std::find_if(m_downloads.begin(), m_downloads.end(), has_index(block.piece_index)) == m_downloads.end());
return;
}
std::vector<downloading_piece>::iterator i
= std::find_if(m_downloads.begin(), m_downloads.end(), has_index(block.piece_index));
assert(i != m_downloads.end());
assert(i->requested_blocks[block.block_index] == 1);
// clear this block as being downloaded
i->requested_blocks[block.block_index] = 0;
// if there are no other blocks in this pieces
// that's being downloaded, remove it from the list
if (i->requested_blocks.count() == 0)
{
m_downloads.erase(i);
m_piece_map[block.piece_index].downloading = 0;
move(true, m_piece_map[block.piece_index].peer_count, m_piece_map[block.piece_index].index);
}
#ifndef NDEBUG
integrity_check();
#endif
}
}

171
src/policy.cpp Executable file
View File

@ -0,0 +1,171 @@
/*
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.
*/
#include <iostream>
#include "libtorrent/policy.hpp"
#include "libtorrent/torrent.hpp"
#include "libtorrent/socket.hpp"
#include "libtorrent/peer_connection.hpp"
namespace
{
using namespace libtorrent;
void request_a_block(torrent& t, peer_connection& c)
{
piece_picker& p = t.picker();
std::vector<piece_block> interesting_pieces;
interesting_pieces.reserve(100);
p.pick_pieces(c.get_bitfield(), interesting_pieces);
// this vector is filled with the interestin pieces
// that some other peer is currently downloading
// we should then compare this peer's download speed
// with the other's, to see if we should abort another
// peer_connection in favour of this one
std::vector<piece_block> busy_pieces;
busy_pieces.reserve(10);
for (std::vector<piece_block>::iterator i = interesting_pieces.begin();
i != interesting_pieces.end();
++i)
{
if (p.is_downloading(*i))
{
busy_pieces.push_back(*i);
continue;
}
// ok, we found a piece that's not being downloaded
// by somebody else. request it from this peer
c.request_block(*i);
return;
}
// TODO:
// compare this peer's bandwidth against the ones downloading
// these pieces (busy_pieces)
}
}
namespace libtorrent
{
/*
TODO: make two proxy classes that filter out
all unneccesary members from torrent and peer_connection
to make it easier to use them in the policy
useful member functions:
void torrent::connect_to_peer(address, peer_id);
piece_picker& torrent::picker();
std::vector<peer_connection*>::const_iterator torrent::begin() const
std::vector<peer_connection*>::const_iterator torrent::end() const
void peer_connection::interested();
void peer_connection::not_interested();
void peer_connection::choke();
void peer_connection::unchoke();
void peer_connection::request_piece(int index);
const std::vector<int>& peer_connection::download_queue();
*/
policy::policy(torrent* t)
: m_num_peers(0)
, m_torrent(t)
{}
// this is called when a connection is made, before any
// handshake (it's possible to ban certain ip:s).
bool policy::accept_connection(const address& remote)
{
m_num_peers++;
return true;
}
void policy::peer_from_tracker(const address& remote, const peer_id& id)
{
m_torrent->connect_to_peer(remote, id);
m_num_peers++;
}
// this is called when we are choked by a peer
// i.e. a peer lets us know that we will not receive
// anything for a while
void policy::choked(peer_connection& c)
{
c.choke();
}
void policy::piece_finished(peer_connection& c, int index, bool successfully_verified)
{
// TODO: if verification failed, mark the peers that were involved
// in some way
}
void policy::block_finished(peer_connection& c, piece_block b)
{
if (c.has_peer_choked()) return;
request_a_block(*m_torrent, c);
}
// this is called when we are unchoked by a peer
// i.e. a peer lets us know that we will receive
// data from now on
void policy::unchoked(peer_connection& c)
{
c.unchoke();
if (c.is_interesting()) request_a_block(*m_torrent, c);
}
void policy::interested(peer_connection& c)
{
c.unchoke();
}
void policy::not_interested(peer_connection& c)
{
}
void policy::connection_closed(const peer_connection& c)
{
}
void policy::peer_is_interesting(peer_connection& c)
{
c.interested();
if (c.has_peer_choked()) return;
request_a_block(*m_torrent, c);
}
}

540
src/session.cpp Executable file
View File

@ -0,0 +1,540 @@
/*
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.
*/
#include <ctime>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <set>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem/convenience.hpp>
#include "libtorrent/peer_id.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/url_handler.hpp"
#include "libtorrent/bencode.hpp"
#include "libtorrent/hasher.hpp"
#include "libtorrent/entry.hpp"
#include "libtorrent/session.hpp"
#if defined(_MSC_VER) && _MSC_VER < 1300
namespace std
{
using ::srand;
};
#endif
/*
DESIGN OVERVIEW AND RATIONALE
The main goal of this library is to be efficient, primarily memory-wise
but also CPU-wise. This goal has a number of implications:
* It must handle multiple torrents (multiple processes uses much more memory)
* It relies on a well working disk chache, since it will download directly to disk. This is
to scale better when having many peer connections.
*
*/
namespace
{
using namespace libtorrent;
peer_id generate_peer_id()
{
peer_id ret;
std::srand(std::time(0));
// unsigned char fingerprint[] = "h\0\0\0\0\0\0\0\0\0";
unsigned char fingerprint[] = "h\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
const int len = sizeof(fingerprint)-1;
std::copy(fingerprint, fingerprint+len, ret.begin());
for (unsigned char* i = ret.begin()+len; i != ret.end(); ++i) *i = rand();
return ret;
}
}
namespace libtorrent
{
namespace detail
{
void session_impl::run(int listen_port)
{
#ifndef NDEBUG
m_logger = boost::shared_ptr<logger>(
m_log_spawner->create_logger("main session"));
#endif
m_peer_id = generate_peer_id();
boost::shared_ptr<socket> listener(new socket(socket::tcp, false));
int max_port = listen_port + 9;
// create listener socket
for(;;)
{
try
{
listener->listen(listen_port, 5);
}
catch(network_error&)
{
if (listen_port > max_port) throw;
listen_port++;
continue;
}
break;
}
std::cout << "listening on port: " << listen_port << "\n";
m_selector.monitor_readability(listener);
m_selector.monitor_errors(listener);
/*
// temp
const peer& p = *m_peer_list.begin();
boost::shared_ptr<libtorrent::socket> s(new socket(socket::tcp, false));
address a(p.ip, p.port);
s->connect(a);
m_connections.insert(std::make_pair(s, peer_connection(this, s, p.id)));
m_selector.monitor_readability(s);
m_selector.monitor_errors(s);
// ~temp
*/
std::vector<boost::shared_ptr<socket> > readable_clients;
std::vector<boost::shared_ptr<socket> > writable_clients;
std::vector<boost::shared_ptr<socket> > error_clients;
boost::posix_time::ptime timer = boost::posix_time::second_clock::local_time();
for(;;)
{
// if nothing happens within 500000 microseconds (0.5 seconds)
// do the loop anyway to check if anything else has changed
// (*m_logger) << "sleeping\n";
m_selector.wait(500000, readable_clients, writable_clients, error_clients);
boost::mutex::scoped_lock l(m_mutex);
if (m_abort)
{
m_tracker_manager.abort_all_requests();
for (std::map<sha1_hash, boost::shared_ptr<torrent> >::iterator i =
m_torrents.begin();
i != m_torrents.end();
++i)
{
i->second->abort();
m_tracker_manager.queue_request(i->second->generate_tracker_request(listen_port));
}
m_connections.clear();
m_torrents.clear();
break;
}
// ************************
// RECEIVE SOCKETS
// ************************
// TODO: once every second or so, all sockets should receive_data() to purge connections
// that has been closed. Otherwise we have to wait 2 minutes for their timeout
// let the readable clients receive data
for (std::vector<boost::shared_ptr<socket> >::iterator i = readable_clients.begin(); i != readable_clients.end(); ++i)
{
// special case for listener socket
if (*i == listener)
{
boost::shared_ptr<libtorrent::socket> s = (*i)->accept();
if (s)
{
// we got a connection request!
#ifndef NDEBUG
(*m_logger) << s->sender().as_string() << " <== INCOMING CONNECTION\n";
#endif
// TODO: the send buffer size should be controllable from the outside
s->set_send_bufsize(2048);
// TODO: add some possibility to filter IP:s
boost::shared_ptr<peer_connection> c(new peer_connection(this, s));
m_connections.insert(std::make_pair(s, c));
m_selector.monitor_readability(s);
m_selector.monitor_errors(s);
}
continue;
}
connection_map::iterator p = m_connections.find(*i);
if(p == m_connections.end())
{
m_selector.remove(*i);
}
else
{
try
{
// (*m_logger) << "readable: " << p->first->sender().as_string() << "\n";
p->second->receive_data();
}
catch(network_error&)
{
// the connection wants to disconnect for some reason, remove it
// from the connection-list
m_selector.remove(*i);
m_connections.erase(p);
}
}
}
// ************************
// SEND SOCKETS
// ************************
// let the writable clients send data
for (std::vector<boost::shared_ptr<socket> >::iterator i = writable_clients.begin(); i != writable_clients.end(); ++i)
{
connection_map::iterator p = m_connections.find(*i);
// the connection may have been disconnected in the receive phase
if (p == m_connections.end())
{
m_selector.remove(*i);
}
else
{
try
{
// (*m_logger) << "writable: " << p->first->sender().as_string() << "\n";
p->second->send_data();
}
catch(network_error&)
{
// the connection wants to disconnect for some reason, remove it
// from the connection-list
m_selector.remove(*i);
m_connections.erase(p);
}
}
}
// ************************
// ERROR SOCKETS
// ************************
// disconnect the one we couldn't connect to
for (std::vector<boost::shared_ptr<socket> >::iterator i = error_clients.begin(); i != error_clients.end(); ++i)
{
connection_map::iterator p = m_connections.find(*i);
m_selector.remove(*i);
// the connection may have been disconnected in the receive or send phase
if (p != m_connections.end()) m_connections.erase(p);
}
// clear all writablility monitors and add
// the ones who still has data to send
m_selector.clear_writable();
// ************************
// BUILD WRITER LIST
// ************************
// loop over all clients and purge the ones that has timed out
// and check if they have pending data to be sent
for (connection_map::iterator i = m_connections.begin(); i != m_connections.end();)
{
connection_map::iterator j = i;
++i;
if (j->second->has_timed_out())
{
m_selector.remove(j->first);
m_connections.erase(j);
}
else
{
j->second->keep_alive();
if (j->second->has_data())
{
// (*m_logger) << j->first->sender().as_string() << " has data\n";
m_selector.monitor_writability(j->first);
}
else
{
// (*m_logger) << j->first->sender().as_string() << " has NO data\n";
}
}
}
// (*m_logger) << "time: " << std::clock()-timer << "\n";
boost::posix_time::time_duration d = boost::posix_time::second_clock::local_time() - timer;
if (d.seconds() < 1) continue;
timer = boost::posix_time::second_clock::local_time();
// ************************
// THE SECTION BELOW IS EXECUTED ONCE EVERY SECOND
// ************************
// do the second_tick() on each connection
// this will update their statistics (download and upload speeds)
for (connection_map::iterator i = m_connections.begin(); i != m_connections.end(); ++i)
{
i->second->second_tick();
}
// check each torrent for abortion or
// tracker updates
for (std::map<sha1_hash, boost::shared_ptr<torrent> >::iterator i = m_torrents.begin();
i != m_torrents.end();)
{
if (i->second->is_aborted())
{
m_tracker_manager.queue_request(i->second->generate_tracker_request(listen_port));
i->second->close_all_connections();
std::map<sha1_hash, boost::shared_ptr<torrent> >::iterator j = i;
++i;
m_torrents.erase(j);
continue;
}
else if (i->second->should_request())
{
m_tracker_manager.queue_request(
i->second->generate_tracker_request(listen_port),
boost::get_pointer(i->second));
}
++i;
}
m_tracker_manager.tick();
#ifndef NDEBUG
(*m_logger) << "peers: " << m_connections.size() << " \n";
for (connection_map::iterator i = m_connections.begin();
i != m_connections.end();
++i)
{
(*m_logger) << "h: " << i->first->sender().as_string()
<< " | down: " << i->second->statistics().download_rate()
<< " b/s | up: " << i->second->statistics().upload_rate()
<< " b/s \n";
}
m_logger->clear();
#else
std::cout << "peers: " << m_connections.size() << " \n";
for (connection_map::iterator i = m_connections.begin();
i != m_connections.end();
++i)
{
std::cout << "h: " << i->first->sender().as_string()
<< " | down: " << i->second->statistics().download_rate()
<< " b/s | up: " << i->second->statistics().upload_rate()
<< " b/s \n";
}
#endif
}
while (!m_tracker_manager.send_finished())
{
m_tracker_manager.tick();
boost::xtime t;
boost::xtime_get(&t, boost::TIME_UTC);
t.nsec += 1000000;
boost::thread::sleep(t);
}
}
torrent* session_impl::find_torrent(const sha1_hash& info_hash)
{
std::map<sha1_hash, boost::shared_ptr<torrent> >::iterator i = m_torrents.find(info_hash);
if (i == m_torrents.end()) return 0;
return boost::get_pointer(i->second);
}
void piece_check_thread::operator()()
{
// TODO: implement a way to abort a file check and
// to get feedback on how much of the data that has
// been checked and how much of the file we have
// (which should be about the same thing with the
// new allocation model)
try
{
m_data->torrent_ptr->allocate_files(m_data->save_path);
}
catch(...)
{
std::cout << "error while checking files\n";
}
// lock the session to add the new torrent
session_impl* ses = m_data->ses;
boost::mutex::scoped_lock l(ses->m_mutex);
ses->m_torrents.insert(
std::make_pair(m_data->info_hash, m_data->torrent_ptr)).first;
// remove ourself from the 'checking'-list
// (we're no longer in the checking state)
assert(ses->m_checkers.find(m_data->info_hash) != ses->m_checkers.end());
ses->m_checkers.erase(ses->m_checkers.find(m_data->info_hash));
}
}
torrent_handle session::add_torrent(const torrent_info& ti, const std::string& save_path)
{
// lock the session
boost::mutex::scoped_lock l(m_impl.m_mutex);
// is the torrent already active?
// TODO: Maybe this should throw?
if (m_impl.m_torrents.find(ti.info_hash()) != m_impl.m_torrents.end())
return torrent_handle(&m_impl, ti.info_hash());
// is the torrent currently being checked?
if (m_impl.m_checkers.find(ti.info_hash()) != m_impl.m_checkers.end())
return torrent_handle(&m_impl, ti.info_hash());
// create the torrent and the data associated with
// the checker thread and store it before starting
// the thread
boost::shared_ptr<torrent> torrent_ptr(new torrent(&m_impl, ti));
boost::shared_ptr<detail::piece_checker_data> d(new detail::piece_checker_data);
d->torrent_ptr = torrent_ptr;
d->save_path = save_path;
d->ses = &m_impl;
d->info_hash = ti.info_hash();
m_impl.m_checkers.insert(std::make_pair(ti.info_hash(), d));
m_impl.m_checker_threads.create_thread(detail::piece_check_thread(d));
return torrent_handle(&m_impl, ti.info_hash());
}
void session::set_http_settings(const http_settings& s)
{
boost::mutex::scoped_lock l(m_impl.m_mutex);
m_impl.m_tracker_manager.set_settings(s);
}
session::~session()
{
{
boost::mutex::scoped_lock l(m_impl.m_mutex);
m_impl.m_abort = true;
}
m_thread.join();
}
float torrent_handle::progress() const
{
if (m_ses == 0) return 0.f;
boost::mutex::scoped_lock l(m_ses->m_mutex);
std::map<sha1_hash, boost::shared_ptr<torrent> >::iterator i = m_ses->m_torrents.find(m_info_hash);
if (i == m_ses->m_torrents.end()) return 0.f;
return i->second->progress();
}
void torrent_handle::get_peer_info(std::vector<peer_info>& v)
{
v.clear();
if (m_ses == 0) return;
boost::mutex::scoped_lock l(m_ses->m_mutex);
std::map<sha1_hash, boost::shared_ptr<torrent> >::iterator i = m_ses->m_torrents.find(m_info_hash);
if (i == m_ses->m_torrents.end()) return;
const torrent* t = boost::get_pointer(i->second);
for (std::vector<peer_connection*>::const_iterator i = t->begin();
i != t->end();
++i)
{
peer_connection* peer = *i;
v.push_back(peer_info());
peer_info& p = v.back();
const stat& statistics = peer->statistics();
p.down_speed = statistics.download_rate();
p.up_speed = statistics.upload_rate();
p.id = peer->get_peer_id();
p.ip = peer->get_socket()->sender();
p.flags = 0;
if (peer->is_interesting()) p.flags |= peer_info::interesting;
if (peer->has_choked()) p.flags |= peer_info::choked;
if (peer->is_peer_interested()) p.flags |= peer_info::remote_interested;
if (peer->has_peer_choked()) p.flags |= peer_info::remote_choked;
p.pieces = peer->get_bitfield();
}
}
void torrent_handle::abort()
{
if (m_ses == 0) return;
boost::mutex::scoped_lock l(m_ses->m_mutex);
std::map<sha1_hash, boost::shared_ptr<torrent> >::iterator i = m_ses->m_torrents.find(m_info_hash);
if (i == m_ses->m_torrents.end()) return;
i->second->abort();
m_ses = 0;
}
}

66
src/stat.cpp Executable file
View File

@ -0,0 +1,66 @@
/*
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.
*/
#include <numeric>
#include "libtorrent/stat.hpp"
using namespace libtorrent;
void libtorrent::stat::second_tick()
{
std::copy(m_download_per_second_history,
m_download_per_second_history+history-1,
m_download_per_second_history+1);
std::copy(m_upload_per_second_history,
m_upload_per_second_history+history-1,
m_upload_per_second_history+1);
m_download_per_second_history[0] = m_downloaded;
m_upload_per_second_history[0] = m_uploaded;
m_downloaded = 0;
m_uploaded = 0;
m_mean_download_per_second
= std::accumulate(m_download_per_second_history,
m_download_per_second_history+history, 0) / history;
m_mean_upload_per_second
= std::accumulate(m_upload_per_second_history,
m_upload_per_second_history+history, 0) / history;
if (m_mean_download_per_second > m_peak_downloaded_per_second)
m_peak_downloaded_per_second = m_mean_download_per_second;
if (m_mean_upload_per_second > m_peak_uploaded_per_second)
m_peak_uploaded_per_second = m_mean_upload_per_second;
}

529
src/storage.cpp Executable file
View File

@ -0,0 +1,529 @@
/*
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.
*/
#include <ctime>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <set>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem/convenience.hpp>
#include "libtorrent/storage.hpp"
#include "libtorrent/torrent.hpp"
#include "libtorrent/hasher.hpp"
#if defined(_MSC_VER) && _MSV_CER < 1300
#define for if (false) {} else for
#endif
using namespace libtorrent;
// TODO: when a piece_file is opened, a seek offset should be
// accepted as an argument, this way we may avoid opening a
// file in vain if we're about to seek forward anyway
void libtorrent::piece_file::open(storage* s, int index, open_mode o)
{
open_mode old_mode = m_mode;
storage* old_storage = m_storage;
std::vector<file>::const_iterator old_file_iter = m_file_iter;
const file* old_file_info = &(*m_file_iter);
m_mode = o;
m_piece_index = index;
m_storage = s;
m_piece_size = m_storage->m_torrent_file->piece_size(m_piece_index);
assert(index < m_storage->m_torrent_file->num_pieces() && "internal error");
m_piece_offset = 0;
int piece_byte_offset = index * m_storage->m_torrent_file->piece_length();
entry::integer_type file_byte_offset = 0;
for (m_file_iter = m_storage->m_torrent_file->begin_files(); m_file_iter != m_storage->m_torrent_file->end_files(); ++m_file_iter)
{
if (file_byte_offset + m_file_iter->size > piece_byte_offset) break;
file_byte_offset += m_file_iter->size;
}
// m_file_iter now refers to the first file this piece is stored in
// if we're still in the same file, don't reopen it
if ((m_mode == out && !(m_file_mode & std::ios_base::out))
|| old_file_iter != m_file_iter
|| !m_file.is_open()
|| old_storage != m_storage)
{
std::ios_base::openmode m;
if (m_mode == out) m = std::ios_base::out | std::ios_base::in | std::ios_base::binary;
else m = std::ios_base::in | std::ios_base::binary;
const file& f = *m_file_iter;
boost::filesystem::path p = m_storage->m_save_path;
p /= f.path;
p /= f.filename;
m_file.close();
m_file.clear();
m_file_mode = m;
m_file.open(p.native_file_string().c_str(), m_file_mode);
std::cout << "opening file: '" << p.native_file_string() << "'\n";
if (m_file.fail())
{
// TODO: try to recover! create a new file?
assert(!m_file.fail());
}
}
m_file_offset = piece_byte_offset - file_byte_offset;
if (m_mode == in) m_file.seekg(m_file_offset, std::ios_base::beg);
else m_file.seekp(m_file_offset, std::ios_base::beg);
int gpos = m_file.tellg();
int ppos = m_file.tellp();
assert(m_mode == out || m_file_offset == gpos && "internal error");
assert(m_mode == in || m_file_offset == ppos && "internal error");
}
int libtorrent::piece_file::read(char* buf, int size)
{
assert(m_file_offset == (long)m_file.tellg() && "internal error");
// std::cout << std::clock() << "read " << m_piece_index << "\n";
assert(m_mode == in);
int left_to_read = size;
// make sure we don't read more than what belongs to this piece
if (m_piece_offset + left_to_read > m_piece_size)
left_to_read = m_piece_size - m_piece_offset;
int buf_pos = 0;
int read_total = 0;
do
{
assert(m_file_iter != m_storage->m_torrent_file->end_files() && "internal error, please report!");
int available = std::min(static_cast<entry::integer_type>(m_file_iter->size - m_file_offset),
static_cast<entry::integer_type>(left_to_read));
m_file.read(buf + buf_pos, available);
int read = m_file.gcount();
left_to_read -= read;
read_total += read;
buf_pos += read;
m_file_offset += read;
m_piece_offset += read;
if (m_file_offset == m_file_iter->size && m_piece_offset < m_piece_size)
{
++m_file_iter;
assert(m_file_iter != m_storage->m_torrent_file->end_files() && "internal error, please report!");
boost::filesystem::path path = m_storage->m_save_path;
path /= m_file_iter->path;
path /= m_file_iter->filename;
m_file_offset = 0;
m_file.close();
m_file.clear();
m_file.open(path.native_file_string().c_str(), m_file_mode);
std::cout << "opening file: '" << path.native_file_string() << "'\n";
if (m_file.fail())
{
// TODO: try to recover! create a new file?
assert(!m_file.fail());
}
}
else
{
assert(read != 0 && "internal error");
}
} while (left_to_read > 0);
assert(m_file_offset == (long)m_file.tellg() && "internal error");
return read_total;
}
void libtorrent::piece_file::write(const char* buf, int size)
{
assert(m_mode == out);
int left_to_write = size;
// make sure we don't write more than what belongs to this piece
if (m_piece_offset + left_to_write > m_piece_size)
left_to_write = m_piece_size - m_piece_offset;
int buf_pos = 0;
do
{
assert(m_file_iter != m_storage->m_torrent_file->end_files() && "internal error, please report!");
int this_write = std::min(static_cast<entry::integer_type>(m_file_iter->size - m_file_offset),
static_cast<entry::integer_type>(left_to_write));
m_file.write(buf + buf_pos, this_write);
left_to_write -= this_write;
buf_pos += this_write;
m_file_offset += this_write;
m_piece_offset += this_write;
if (m_file_offset == m_file_iter->size && m_piece_offset < m_piece_size)
{
++m_file_iter;
assert(m_file_iter != m_storage->m_torrent_file->end_files() && "internal error, please report!");
boost::filesystem::path path = m_storage->m_save_path;
path /= m_file_iter->path;
path /= m_file_iter->filename;
m_file_offset = 0;
m_file.close();
m_file.open(path.native_file_string().c_str(), m_file_mode);
std::cout << "opening file: '" << path.native_file_string() << "'\n";
if (m_file.fail())
{
// TODO: try to recover! create a new file?
assert(!m_file.fail());
}
}
} while (left_to_write > 0);
}
void libtorrent::piece_file::seek_forward(int step)
{
assert(step >= 0 && "you cannot seek backwards in piece files");
assert(m_file_offset == (long)m_file.tellg() && "internal error");
if (step == 0) return;
int left_to_seek = step;
// make sure we don't read more than what belongs to this piece
assert(m_piece_offset + step <= m_piece_size);
assert(m_file_iter != m_storage->m_torrent_file->end_files() && "internal error, please report!");
bool reopen = false;
while (m_file_iter->size - m_file_offset < left_to_seek)
{
left_to_seek -= m_file_iter->size - m_file_offset;
m_piece_offset += m_file_iter->size - m_file_offset;
m_file_offset = 0;
++m_file_iter;
reopen = true;
assert(m_file_iter != m_storage->m_torrent_file->end_files() && "internal error, please report!");
}
if (reopen)
{
boost::filesystem::path path = m_storage->m_save_path;
path /= m_file_iter->path;
path /= m_file_iter->filename;
m_file.close();
m_file.open(path.native_file_string().c_str(), std::ios_base::in | std::ios_base::binary);
}
m_file_offset += left_to_seek;
m_piece_offset +=left_to_seek;
if (m_mode == in)
{
m_file.seekg(left_to_seek, std::ios_base::cur);
assert(m_file_offset == (long)m_file.tellg() && "internal error");
}
else
{
m_file.seekp(left_to_seek, std::ios_base::cur);
assert(m_file_offset == (long)m_file.tellp() && "internal error");
}
}
bool libtorrent::storage::verify_piece(piece_file& file)
{
int index = file.index();
if (m_have_piece[index]) return true;
std::vector<char> buffer(m_torrent_file->piece_size(index));
file.open(this, index, piece_file::in);
int read = file.read(&buffer[0], buffer.size());
assert(read == m_torrent_file->piece_size(index));
// calculate hash for piece
sha1_hash digest;
hasher h;
h.update(&buffer[0], read);
h.final(digest);
if (std::equal(digest.begin(), digest.end(), m_torrent_file->hash_for_piece(index).begin()))
{
// tell all peer_connections to announce that
// this piece is available
m_bytes_left -= read;
// mark it as available
m_have_piece[index] = true;
#ifndef NDEBUG
int real_bytes_left = 0;
for (int i = 0; i < m_torrent_file->num_pieces(); ++i)
{
if (!m_have_piece[i]) real_bytes_left += m_torrent_file->piece_size(i);
}
assert(real_bytes_left == m_bytes_left);
#endif
return true;
}
return false;
}
// allocate files will create all files that are missing
// if there are some files that already exists, it checks
// that they have the correct filesize
void libtorrent::storage::initialize_pieces(torrent* t, const boost::filesystem::path& path)
{
m_save_path = path;
m_torrent_file = &t->torrent_file();
// TEMPORARY!
/*
m_bytes_left = 0;
m_have_piece.resize(m_torrent_file->num_pieces());
std::fill(m_have_piece.begin(), m_have_piece.end(), true);
return;
*/
/*
m_bytes_left = m_torrent_file->total_size();
m_have_piece.resize(m_torrent_file->num_pieces());
std::fill(m_have_piece.begin(), m_have_piece.end(), false);
return;
*/
// we don't know of any piece we have right now. Initialize
// it to say we don't have anything and fill it in later on.
m_have_piece.resize(m_torrent_file->num_pieces());
std::fill(m_have_piece.begin(), m_have_piece.end(), false);
m_bytes_left = m_torrent_file->total_size();
#ifndef NDEBUG
std::size_t sum = 0;
for (int i = 0; i < m_torrent_file->num_pieces(); ++i)
sum += m_torrent_file->piece_size(i);
assert(sum == m_bytes_left);
#endif
// this will be set to true if some file already exists
// in which case another pass will be made to check
// the hashes of all pieces to know which pieces we
// have
bool resume = false;
// the buffersize of the file writes
const int chunksize = 8192;
char zeros[chunksize];
std::fill(zeros, zeros+chunksize, 0);
std::cout << "allocating files\n";
// remember which directories we have created, so
// we don't have to ask the filesystem all the time
std::set<std::string> created_directories;
for (torrent_info::file_iterator i = m_torrent_file->begin_files(); i != m_torrent_file->end_files(); ++i)
{
boost::filesystem::path path = m_save_path;
path /= i->path;
if (created_directories.find(i->path) == created_directories.end())
{
if (boost::filesystem::exists(path))
{
if (!boost::filesystem::is_directory(path))
{
std::string msg = "Cannot create directory, the file \"";
msg += path.native_file_string();
msg += "\" is in the way.";
throw file_allocation_failed(msg.c_str());
}
}
else
{
boost::filesystem::create_directories(path);
}
created_directories.insert(i->path);
}
// allocate the file.
// fill it with zeros
path /= i->filename;
if (boost::filesystem::exists(path))
{
// the file exists, make sure it isn't a directory
if (boost::filesystem::is_directory(path))
{
std::string msg = "Cannot create file, the directory \"";
msg += path.native_file_string();
msg += "\" is in the way.";
throw file_allocation_failed(msg.c_str());
}
std::cout << "creating file: '" << path.native_file_string() << "'\n";
std::ifstream f(path.native_file_string().c_str(), std::ios_base::binary);
f.seekg(0, std::ios_base::end);
int filesize = f.tellg();
if (filesize != i->size)
{
// TODO: recover by padding the file with 0
std::string msg = "The file \"";
msg += path.native_file_string();
msg += "\" has the wrong size.";
throw file_allocation_failed(msg.c_str());
}
resume = true;
}
else
{
// The file doesn't exist, create it and fill it with zeros
std::ofstream f(path.native_file_string().c_str(), std::ios_base::binary);
entry::integer_type left_to_write = i->size;
while(left_to_write >= chunksize)
{
f.write(zeros, chunksize);
// TODO: Check if disk is full
left_to_write -= chunksize;
}
// TODO: Check if disk is full
if (left_to_write > 0) f.write(zeros, left_to_write);
}
}
// we have to check which pieces we have and which we don't have
if (resume)
{
int missing = 0;
std::cout << "checking existing files\n";
int num_pieces = m_torrent_file->num_pieces();
piece_file f;
for (unsigned int i = 0; i < num_pieces; ++i)
{
f.open(this, i, piece_file::in);
if (!verify_piece(f)) missing++;
std::cout << i+1 << " / " << m_torrent_file->num_pieces() << " missing: " << missing << "\r";
}
std::cout << "\n";
}
}
/*
// reads the piece with the given index from disk
// and writes it into the buffer. The buffer must have
// room for at least m_piece_length bytes.
// the return value is the size of the piece that was read
// and the number of bytes written to the buffer
int libtorrent::torrent::read_piece(unsigned int index, char* buffer) const
{
assert(index < m_torrent_file.num_pieces() && "internal error");
int piece_byte_offset = index * m_torrent_file.piece_length();
entry::integer_type file_byte_offset = 0;
std::vector<file>::const_iterator i;
for (i = m_torrent_file.begin(); i != m_torrent_file.end(); ++i)
{
if (file_byte_offset + i->size > piece_byte_offset) break;
file_byte_offset += i->size;
}
// i now refers to the first file this piece is stored in
int left_to_read = m_torrent_file.piece_size(index);
const int piece_size = m_torrent_file.piece_size(index);
while (left_to_read > 0)
{
assert(i != m_torrent_file.end() && "internal error, please report!");
boost::filesystem::path path = m_save_path;
path /= i->path;
path /= i->filename;
std::ifstream f(path.native_file_string().c_str(), std::ios_base::binary);
f.seekg(piece_byte_offset - file_byte_offset, std::ios_base::beg);
f.read(buffer + piece_size - left_to_read, left_to_read);
int read = f.gcount();
left_to_read -= read;
piece_byte_offset += read;
file_byte_offset += i->size;
++i;
}
return m_torrent_file.piece_size(index);
}
void libtorrent::torrent::write_piece(unsigned int index, const char* buffer) const
{
const int piece_byte_offset = index * m_torrent_file.piece_length();
entry::integer_type file_byte_offset = 0;
std::vector<file>::const_iterator i;
for (i = m_torrent_file.begin(); i != m_torrent_file.end(); ++i)
{
if (file_byte_offset + i->size > piece_byte_offset) break;
file_byte_offset += i->size;
}
assert(i != m_torrent_file.end() && "internal error, please report!");
// i now refers to the first file this piece is stored in
int piece_size = m_torrent_file.piece_size(index);
int written = 0;
while (written < piece_size)
{
boost::filesystem::path path = m_save_path;
path /= i->path;
path /= i->filename;
std::fstream f(path.native_file_string().c_str(), std::ios_base::binary | std::ios_base::in | std::ios_base::out);
f.seekp(piece_byte_offset + written - file_byte_offset, std::ios_base::beg);
int this_write = piece_size - written;
// check if this file is big enough to store the entire piece
if ( this_write > i->size - piece_byte_offset - written + file_byte_offset)
this_write = i->size - piece_byte_offset - written + file_byte_offset;
f.write(buffer + written, this_write);
written += this_write;
file_byte_offset += i->size;
++i;
}
}
*/

397
src/torrent.cpp Executable file
View File

@ -0,0 +1,397 @@
/*
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.
*/
#include <ctime>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <set>
#include <cctype>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem/convenience.hpp>
#include "libtorrent/session.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/url_handler.hpp"
#include "libtorrent/bencode.hpp"
#include "libtorrent/hasher.hpp"
#include "libtorrent/entry.hpp"
#include "libtorrent/peer.hpp"
#include "libtorrent/peer_id.hpp"
#if defined(_MSC_VER) && _MSC_VER < 1300
namespace std
{
using ::isalnum;
};
#endif
using namespace libtorrent;
namespace
{
enum
{
// wait 60 seconds before retrying a failed tracker
tracker_retry_delay = 60
};
int calculate_block_size(const torrent_info& i)
{
// TODO: if blocks_per_piece > 64 increase block-size
return 16*1024;
}
peer extract_peer_info(const entry& e)
{
peer ret;
const entry::dictionary_type& info = e.dict();
// extract peer id
entry::dictionary_type::const_iterator i = info.find("peer id");
if (i == info.end()) throw std::runtime_error("invalid response from tracker");
if (i->second.string().length() != 20) throw std::runtime_error("invalid response from tracker");
std::copy(i->second.string().begin(), i->second.string().end(), ret.id.begin());
// extract ip
i = info.find("ip");
if (i == info.end()) throw std::runtime_error("invalid response from tracker");
ret.ip = i->second.string();
// extract port
i = info.find("port");
if (i == info.end()) throw std::runtime_error("invalid response from tracker");
ret.port = i->second.integer();
return ret;
}
std::string escape_string(const char* str, int len)
{
std::stringstream ret;
ret << std::hex << std::setfill('0');
for (int i = 0; i < len; ++i)
{
// TODO: should alnum() be replaced with printable()?
if (std::isalnum(static_cast<unsigned char>(*str))) ret << *str;
else ret << "%" << std::setw(2) << (int)static_cast<unsigned char>(*str);
++str;
}
return ret.str();
}
struct find_peer
{
find_peer(const peer_id& i, const torrent* t): id(i), tor(t) {}
bool operator()(const detail::session_impl::connection_map::value_type& c) const
{
if (c.second->get_peer_id() != id) return false;
if (tor != c.second->associated_torrent()) return false;
return true;
}
const peer_id& id;
const torrent* tor;
};
}
namespace libtorrent
{
torrent::torrent(detail::session_impl* ses, const torrent_info& torrent_file)
: m_block_size(calculate_block_size(torrent_file))
, m_abort(false)
, m_event(event_started)
, m_bytes_uploaded(0)
, m_bytes_downloaded(0)
, m_torrent_file(torrent_file)
, m_next_request(boost::posix_time::second_clock::local_time())
, m_duration(1800)
, m_policy(new policy(this))
, m_ses(ses)
, m_picker(torrent_file.piece_length() / m_block_size,
(torrent_file.total_size()+m_block_size-1)/m_block_size)
, m_last_working_tracker(0)
, m_currently_trying_tracker(0)
{
}
void torrent::tracker_response(const entry& e)
{
try
{
// parse the response
parse_response(e);
}
catch(type_error& e)
{
tracker_request_error(e.what());
}
catch(std::runtime_error& e)
{
tracker_request_error(e.what());
}
m_last_working_tracker = m_torrent_file.prioritize_tracker(m_currently_trying_tracker);
m_next_request = boost::posix_time::second_clock::local_time() + boost::posix_time::seconds(m_duration);
m_currently_trying_tracker = 0;
// connect to random peers from the list
std::random_shuffle(m_peer_list.begin(), m_peer_list.end());
print(std::cout);
// for each of the peers we got from the tracker
for (std::vector<peer>::iterator i = m_peer_list.begin(); i != m_peer_list.end(); ++i)
{
// don't make connections to ourself
if (i->id == m_ses->get_peer_id())
continue;
address a(i->ip, i->port);
// if we aleady have a connection to the person, don't make another one
if (std::find_if(m_ses->m_connections.begin(), m_ses->m_connections.end(), find_peer(i->id, this)) != m_ses->m_connections.end())
continue;
m_policy->peer_from_tracker(a, i->id);
}
}
int torrent::num_connections(const peer_id& id) const
{
int num = 0;
for (detail::session_impl::connection_map::const_iterator i = m_ses->m_connections.begin();
i != m_ses->m_connections.end();
++i)
{
if (i->second->get_peer_id() == id && i->second->associated_torrent() == this) ++num;
}
return num;
}
void torrent::announce_piece(int index)
{
m_picker.we_have(index);
for (std::vector<peer_connection*>::iterator i = m_connections.begin(); i != m_connections.end(); ++i)
(*i)->announce_piece(index);
#ifndef NDEBUG
m_picker.integrity_check(this);
#endif
}
std::string torrent::generate_tracker_request(int port)
{
m_duration = 1800;
m_next_request = boost::posix_time::second_clock::local_time() + boost::posix_time::seconds(m_duration);
std::vector<char> buffer;
// TODO: temporary! support multi-tracker
std::string request = m_torrent_file.trackers()[m_currently_trying_tracker].url;
request += "?info_hash=";
request += escape_string(reinterpret_cast<const char*>(m_torrent_file.info_hash().begin()), 20);
request += "&peer_id=";
request += escape_string(reinterpret_cast<const char*>(m_ses->get_peer_id().begin()), 20);
request += "&port=";
request += boost::lexical_cast<std::string>(port);
request += "&uploaded=";
request += boost::lexical_cast<std::string>(m_bytes_uploaded);
request += "&downloaded=";
request += boost::lexical_cast<std::string>(m_bytes_downloaded);
request += "&left=";
request += boost::lexical_cast<std::string>(m_storage.bytes_left());
if (m_event != event_none)
{
const char* event_string[] = {"started", "stopped", "completed"};
request += "&event=";
request += event_string[m_event];
m_event = event_none;
}
return request;
}
void torrent::parse_response(const entry& e)
{
entry::dictionary_type::const_iterator i = e.dict().find("failure reason");
if (i != e.dict().end())
{
throw std::runtime_error(i->second.string().c_str());
}
const entry::dictionary_type& msg = e.dict();
i = msg.find("interval");
if (i == msg.end()) throw std::runtime_error("invalid response from tracker");
m_duration = i->second.integer();
i = msg.find("peers");
if (i == msg.end()) throw std::runtime_error("invalid response from tracker");
m_peer_list.clear();
const entry::list_type& peer_list = i->second.list();
for(entry::list_type::const_iterator i = peer_list.begin(); i != peer_list.end(); ++i)
{
peer p = extract_peer_info(*i);
m_peer_list.push_back(p);
}
}
void torrent::remove_peer(peer_connection* p)
{
std::vector<peer_connection*>::iterator i = std::find(m_connections.begin(), m_connections.end(), p);
assert(i != m_connections.end());
// if the peer_connection was downloading any pieces
// abort them
for (std::vector<piece_block>::const_iterator i = p->download_queue().begin();
i != p->download_queue().end();
++i)
{
m_picker.abort_download(*i);
}
for (std::size_t i = 0; i < torrent_file().num_pieces(); ++i)
{
if (p->has_piece(i)) peer_lost(i);
}
std::cout << p->get_socket()->sender().as_string() << " *** DISCONNECT\n";
m_policy->connection_closed(*p);
m_connections.erase(i);
#ifndef NDEBUG
m_picker.integrity_check(this);
#endif
}
void torrent::connect_to_peer(const address& a, const peer_id& id)
{
boost::shared_ptr<socket> s(new socket(socket::tcp, false));
// TODO: the send buffer size should be controllable from the outside
s->set_send_bufsize(2048);
s->connect(a);
boost::shared_ptr<peer_connection> c(new peer_connection(m_ses, this, s, id));
detail::session_impl::connection_map::iterator p =
m_ses->m_connections.insert(std::make_pair(s, c)).first;
attach_peer(boost::get_pointer(p->second));
m_ses->m_selector.monitor_writability(s);
m_ses->m_selector.monitor_readability(s);
m_ses->m_selector.monitor_errors(s);
std::cout << "connecting to: " << a.as_string() << ":" << a.port() << "\n";
}
void torrent::print(std::ostream& os) const
{
os << "interval: " << m_duration << "\n";
os << "peers:\n";
for (std::vector<peer>::const_iterator i = m_peer_list.begin(); i != m_peer_list.end(); ++i)
{
os << " " << std::setfill(' ') << std::setw(16) << i->ip << " " << std::setw(5) << std::dec << i->port << " ";
for (const unsigned char* j = i->id.begin(); j != i->id.end(); ++j)
os << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(*j);
os << "\n";
}
os << std::dec << std::setfill(' ');
}
#ifndef NDEBUG
logger* torrent::spawn_logger(const char* title)
{
return m_ses->m_log_spawner->create_logger(title);
}
#endif
void torrent::close_all_connections()
{
for (detail::session_impl::connection_map::iterator i = m_ses->m_connections.begin();
i != m_ses->m_connections.end();)
{
if (i->second->associated_torrent() == this)
{
#ifndef NDEBUG
std::size_t num_connections = m_connections.size();
peer_connection* pc = boost::get_pointer(i->second);
#endif
assert(std::find(m_connections.begin(), m_connections.end(), pc) != m_connections.end());
detail::session_impl::connection_map::iterator j = i;
++i;
m_ses->m_connections.erase(j);
assert(m_connections.size() + 1 == num_connections);
assert(std::find(m_connections.begin(), m_connections.end(), pc) == m_connections.end());
}
else
{
++i;
assert(std::find(m_connections.begin(), m_connections.end(), boost::get_pointer(i->second)) == m_connections.end());
}
}
assert(m_connections.empty());
}
void torrent::try_next_tracker()
{
m_currently_trying_tracker++;
if (m_currently_trying_tracker >= m_torrent_file.trackers().size())
{
// if we've looped the tracker list, wait a bit before retrying
m_currently_trying_tracker = 0;
m_next_request = boost::posix_time::second_clock::local_time() + boost::posix_time::seconds(tracker_retry_delay);
}
else
{
// don't delay before trying the next tracker
m_next_request = boost::posix_time::second_clock::local_time();
}
}
}

220
src/torrent_info.cpp Executable file
View File

@ -0,0 +1,220 @@
/*
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.
*/
#include <ctime>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <set>
#include <boost/lexical_cast.hpp>
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/bencode.hpp"
#include "libtorrent/hasher.hpp"
#include "libtorrent/entry.hpp"
using namespace libtorrent;
namespace
{
void extract_single_file(const entry::dictionary_type& dict, file& target)
{
entry::dictionary_type::const_iterator i = dict.find("length");
if (i == dict.end()) throw invalid_torrent_file();
target.size = i->second.integer();
i = dict.find("path");
if (i == dict.end()) throw invalid_torrent_file();
const entry::list_type& list = i->second.list();
for (entry::list_type::const_iterator i = list.begin(); i != list.end()-1; ++i)
{
target.path += '/';
target.path += i->string();
}
target.path += '/';
target.filename = list.back().string();
}
void extract_files(const entry::list_type& list, std::vector<file>& target, const std::string& root_directory)
{
for (entry::list_type::const_iterator i = list.begin(); i != list.end(); ++i)
{
target.push_back(file());
target.back().path = root_directory;
extract_single_file(i->dict(), target.back());
}
}
}
namespace libtorrent
{
// extracts information from a libtorrent file and fills in the structures in
// the torrent object
void torrent_info::read_torrent_info(const entry& torrent_file)
{
// extract the url of the tracker
const entry::dictionary_type& dict = torrent_file.dict();
entry::dictionary_type::const_iterator i = dict.find("announce-list");
if (i != dict.end())
{
const entry::list_type& l = i->second.list();
for (entry::list_type::const_iterator j = l.begin(); j != l.end(); ++j)
{
const entry::list_type& ll = j->list();
for (entry::list_type::const_iterator k = ll.begin(); k != ll.end(); ++k)
{
announce_entry e;
e.tier = j - l.begin();
e.url = k->string();
m_urls.push_back(e);
}
}
if (m_urls.size() == 0) throw invalid_torrent_file();
// shuffle each tier
std::vector<announce_entry>::iterator i = m_urls.begin();
std::vector<announce_entry>::iterator j;
int current_tier = m_urls.front().tier;
for (j = m_urls.begin(); j != m_urls.end(); ++j)
{
if (j->tier != current_tier)
{
std::random_shuffle(i, j);
i = j;
current_tier = j->tier;
}
}
std::random_shuffle(i, j);
}
else
{
i = dict.find("announce");
if (i == dict.end()) throw invalid_torrent_file();
announce_entry e;
e.tier = 0;
e.url = i->second.string();
m_urls.push_back(e);
}
i = dict.find("info");
if (i == dict.end()) throw invalid_torrent_file();
entry info = i->second;
// encode the info-field in order to calculate it's sha1-hash
std::vector<char> buf;
bencode(std::back_insert_iterator<std::vector<char> >(buf), info);
hasher h;
h.update(&buf[0], buf.size());
h.final(m_info_hash);
// extract piece length
i = info.dict().find("piece length");
if (i == info.dict().end()) throw invalid_torrent_file();
m_piece_length = i->second.integer();
// extract file name (or the directory name if it's a multifile libtorrent)
i = info.dict().find("name");
if (i == info.dict().end()) throw invalid_torrent_file();
std::string filename = i->second.string();
// extract file list
i = info.dict().find("files");
if (i == info.dict().end())
{
// if there's no list of files, there has to be a length
// field.
i = info.dict().find("length");
if (i == info.dict().end()) throw invalid_torrent_file();
m_files.push_back(file());
m_files.back().filename = filename;
m_files.back().size = i->second.integer();
}
else
{
extract_files(i->second.list(), m_files, filename);
}
// calculate total size of all pieces
m_total_size = 0;
for (std::vector<file>::iterator i = m_files.begin(); i != m_files.end(); ++i)
m_total_size += i->size;
// extract sha-1 hashes for all pieces
// we want this division to round upwards, that's why we have the
// extra addition
entry::integer_type num_pieces = (m_total_size + m_piece_length - 1) / m_piece_length;
i = info.dict().find("pieces");
if (i == info.dict().end()) throw invalid_torrent_file();
m_piece_hash.resize(num_pieces);
const std::string& hash_string = i->second.string();
if (hash_string.length() != num_pieces * 20) throw invalid_torrent_file();
for (int i = 0; i < num_pieces; ++i)
std::copy(hash_string.begin() + i*20, hash_string.begin() + (i+1)*20, m_piece_hash[i].begin());
}
int torrent_info::prioritize_tracker(int index)
{
if (index > m_urls.size()) return m_urls.size()-1;
while (index > 0 && m_urls[index].tier == m_urls[index-1].tier)
{
std::swap(m_urls[index].url, m_urls[index-1].url);
--index;
}
return index;
}
void torrent_info::print(std::ostream& os) const
{
os << "trackers:\n";
for (std::vector<announce_entry>::const_iterator i = trackers().begin();
i != trackers().end();
++i)
{
os << i->tier << ": " << i->url << "\n";
}
os << "number of pieces: " << num_pieces() << "\n";
os << "piece length: " << piece_length() << "\n";
os << "files:\n";
for (file_iterator i = begin_files(); i != end_files(); ++i)
os << " " << std::setw(11) << i->size << " " << i->path << " " << i->filename << "\n";
}
}

48
zlib/adler32.c Normal file
View File

@ -0,0 +1,48 @@
/* adler32.c -- compute the Adler-32 checksum of a data stream
* Copyright (C) 1995-2002 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* @(#) $Id$ */
#include "zlib.h"
#define BASE 65521L /* largest prime smaller than 65536 */
#define NMAX 5552
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
#define DO16(buf) DO8(buf,0); DO8(buf,8);
/* ========================================================================= */
uLong ZEXPORT adler32(adler, buf, len)
uLong adler;
const Bytef *buf;
uInt len;
{
unsigned long s1 = adler & 0xffff;
unsigned long s2 = (adler >> 16) & 0xffff;
int k;
if (buf == Z_NULL) return 1L;
while (len > 0) {
k = len < NMAX ? len : NMAX;
len -= k;
while (k >= 16) {
DO16(buf);
buf += 16;
k -= 16;
}
if (k != 0) do {
s1 += *buf++;
s2 += s1;
} while (--k);
s1 %= BASE;
s2 %= BASE;
}
return (s2 << 16) | s1;
}

68
zlib/compress.c Normal file
View File

@ -0,0 +1,68 @@
/* compress.c -- compress a memory buffer
* Copyright (C) 1995-2002 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* @(#) $Id$ */
#include "zlib.h"
/* ===========================================================================
Compresses the source buffer into the destination buffer. The level
parameter has the same meaning as in deflateInit. sourceLen is the byte
length of the source buffer. Upon entry, destLen is the total size of the
destination buffer, which must be at least 0.1% larger than sourceLen plus
12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_BUF_ERROR if there was not enough room in the output buffer,
Z_STREAM_ERROR if the level parameter is invalid.
*/
int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
Bytef *dest;
uLongf *destLen;
const Bytef *source;
uLong sourceLen;
int level;
{
z_stream stream;
int err;
stream.next_in = (Bytef*)source;
stream.avail_in = (uInt)sourceLen;
#ifdef MAXSEG_64K
/* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
#endif
stream.next_out = dest;
stream.avail_out = (uInt)*destLen;
if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
stream.opaque = (voidpf)0;
err = deflateInit(&stream, level);
if (err != Z_OK) return err;
err = deflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
deflateEnd(&stream);
return err == Z_OK ? Z_BUF_ERROR : err;
}
*destLen = stream.total_out;
err = deflateEnd(&stream);
return err;
}
/* ===========================================================================
*/
int ZEXPORT compress (dest, destLen, source, sourceLen)
Bytef *dest;
uLongf *destLen;
const Bytef *source;
uLong sourceLen;
{
return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
}

162
zlib/crc32.c Normal file
View File

@ -0,0 +1,162 @@
/* crc32.c -- compute the CRC-32 of a data stream
* Copyright (C) 1995-2002 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* @(#) $Id$ */
#include "zlib.h"
#define local static
#ifdef DYNAMIC_CRC_TABLE
local int crc_table_empty = 1;
local uLongf crc_table[256];
local void make_crc_table OF((void));
/*
Generate a table for a byte-wise 32-bit CRC calculation on the polynomial:
x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
Polynomials over GF(2) are represented in binary, one bit per coefficient,
with the lowest powers in the most significant bit. Then adding polynomials
is just exclusive-or, and multiplying a polynomial by x is a right shift by
one. If we call the above polynomial p, and represent a byte as the
polynomial q, also with the lowest power in the most significant bit (so the
byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
where a mod b means the remainder after dividing a by b.
This calculation is done using the shift-register method of multiplying and
taking the remainder. The register is initialized to zero, and for each
incoming bit, x^32 is added mod p to the register if the bit is a one (where
x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
x (which is shifting right by one and adding x^32 mod p if the bit shifted
out is a one). We start with the highest power (least significant bit) of
q and repeat for all eight bits of q.
The table is simply the CRC of all possible eight bit values. This is all
the information needed to generate CRC's on data a byte at a time for all
combinations of CRC register values and incoming bytes.
*/
local void make_crc_table()
{
uLong c;
int n, k;
uLong poly; /* polynomial exclusive-or pattern */
/* terms of polynomial defining this crc (except x^32): */
static const Byte p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
/* make exclusive-or pattern from polynomial (0xedb88320L) */
poly = 0L;
for (n = 0; n < sizeof(p)/sizeof(Byte); n++)
poly |= 1L << (31 - p[n]);
for (n = 0; n < 256; n++)
{
c = (uLong)n;
for (k = 0; k < 8; k++)
c = c & 1 ? poly ^ (c >> 1) : c >> 1;
crc_table[n] = c;
}
crc_table_empty = 0;
}
#else
/* ========================================================================
* Table of CRC-32's of all single-byte values (made by make_crc_table)
*/
local const uLongf crc_table[256] = {
0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
0x2d02ef8dL
};
#endif
/* =========================================================================
* This function can be used by asm versions of crc32()
*/
const uLongf * ZEXPORT get_crc_table()
{
#ifdef DYNAMIC_CRC_TABLE
if (crc_table_empty) make_crc_table();
#endif
return (const uLongf *)crc_table;
}
/* ========================================================================= */
#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
#define DO2(buf) DO1(buf); DO1(buf);
#define DO4(buf) DO2(buf); DO2(buf);
#define DO8(buf) DO4(buf); DO4(buf);
/* ========================================================================= */
uLong ZEXPORT crc32(crc, buf, len)
uLong crc;
const Bytef *buf;
uInt len;
{
if (buf == Z_NULL) return 0L;
#ifdef DYNAMIC_CRC_TABLE
if (crc_table_empty)
make_crc_table();
#endif
crc = crc ^ 0xffffffffL;
while (len >= 8)
{
DO8(buf);
len -= 8;
}
if (len) do {
DO1(buf);
} while (--len);
return crc ^ 0xffffffffL;
}

1350
zlib/deflate.c Normal file

File diff suppressed because it is too large Load Diff

318
zlib/deflate.h Normal file
View File

@ -0,0 +1,318 @@
/* deflate.h -- internal compression state
* Copyright (C) 1995-2002 Jean-loup Gailly
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* WARNING: this file should *not* be used by applications. It is
part of the implementation of the compression library and is
subject to change. Applications should only use zlib.h.
*/
/* @(#) $Id$ */
#ifndef _DEFLATE_H
#define _DEFLATE_H
#include "zutil.h"
/* ===========================================================================
* Internal compression state.
*/
#define LENGTH_CODES 29
/* number of length codes, not counting the special END_BLOCK code */
#define LITERALS 256
/* number of literal bytes 0..255 */
#define L_CODES (LITERALS+1+LENGTH_CODES)
/* number of Literal or Length codes, including the END_BLOCK code */
#define D_CODES 30
/* number of distance codes */
#define BL_CODES 19
/* number of codes used to transfer the bit lengths */
#define HEAP_SIZE (2*L_CODES+1)
/* maximum heap size */
#define MAX_BITS 15
/* All codes must not exceed MAX_BITS bits */
#define INIT_STATE 42
#define BUSY_STATE 113
#define FINISH_STATE 666
/* Stream status */
/* Data structure describing a single value and its code string. */
typedef struct ct_data_s {
union {
ush freq; /* frequency count */
ush code; /* bit string */
} fc;
union {
ush dad; /* father node in Huffman tree */
ush len; /* length of bit string */
} dl;
} FAR ct_data;
#define Freq fc.freq
#define Code fc.code
#define Dad dl.dad
#define Len dl.len
typedef struct static_tree_desc_s static_tree_desc;
typedef struct tree_desc_s {
ct_data *dyn_tree; /* the dynamic tree */
int max_code; /* largest code with non zero frequency */
static_tree_desc *stat_desc; /* the corresponding static tree */
} FAR tree_desc;
typedef ush Pos;
typedef Pos FAR Posf;
typedef unsigned IPos;
/* A Pos is an index in the character window. We use short instead of int to
* save space in the various tables. IPos is used only for parameter passing.
*/
typedef struct internal_state {
z_streamp strm; /* pointer back to this zlib stream */
int status; /* as the name implies */
Bytef *pending_buf; /* output still pending */
ulg pending_buf_size; /* size of pending_buf */
Bytef *pending_out; /* next pending byte to output to the stream */
int pending; /* nb of bytes in the pending buffer */
int noheader; /* suppress zlib header and adler32 */
Byte data_type; /* UNKNOWN, BINARY or ASCII */
Byte method; /* STORED (for zip only) or DEFLATED */
int last_flush; /* value of flush param for previous deflate call */
/* used by deflate.c: */
uInt w_size; /* LZ77 window size (32K by default) */
uInt w_bits; /* log2(w_size) (8..16) */
uInt w_mask; /* w_size - 1 */
Bytef *window;
/* Sliding window. Input bytes are read into the second half of the window,
* and move to the first half later to keep a dictionary of at least wSize
* bytes. With this organization, matches are limited to a distance of
* wSize-MAX_MATCH bytes, but this ensures that IO is always
* performed with a length multiple of the block size. Also, it limits
* the window size to 64K, which is quite useful on MSDOS.
* To do: use the user input buffer as sliding window.
*/
ulg window_size;
/* Actual size of window: 2*wSize, except when the user input buffer
* is directly used as sliding window.
*/
Posf *prev;
/* Link to older string with same hash index. To limit the size of this
* array to 64K, this link is maintained only for the last 32K strings.
* An index in this array is thus a window index modulo 32K.
*/
Posf *head; /* Heads of the hash chains or NIL. */
uInt ins_h; /* hash index of string to be inserted */
uInt hash_size; /* number of elements in hash table */
uInt hash_bits; /* log2(hash_size) */
uInt hash_mask; /* hash_size-1 */
uInt hash_shift;
/* Number of bits by which ins_h must be shifted at each input
* step. It must be such that after MIN_MATCH steps, the oldest
* byte no longer takes part in the hash key, that is:
* hash_shift * MIN_MATCH >= hash_bits
*/
long block_start;
/* Window position at the beginning of the current output block. Gets
* negative when the window is moved backwards.
*/
uInt match_length; /* length of best match */
IPos prev_match; /* previous match */
int match_available; /* set if previous match exists */
uInt strstart; /* start of string to insert */
uInt match_start; /* start of matching string */
uInt lookahead; /* number of valid bytes ahead in window */
uInt prev_length;
/* Length of the best match at previous step. Matches not greater than this
* are discarded. This is used in the lazy match evaluation.
*/
uInt max_chain_length;
/* To speed up deflation, hash chains are never searched beyond this
* length. A higher limit improves compression ratio but degrades the
* speed.
*/
uInt max_lazy_match;
/* Attempt to find a better match only when the current match is strictly
* smaller than this value. This mechanism is used only for compression
* levels >= 4.
*/
# define max_insert_length max_lazy_match
/* Insert new strings in the hash table only if the match length is not
* greater than this length. This saves time but degrades compression.
* max_insert_length is used only for compression levels <= 3.
*/
int level; /* compression level (1..9) */
int strategy; /* favor or force Huffman coding*/
uInt good_match;
/* Use a faster search when the previous match is longer than this */
int nice_match; /* Stop searching when current match exceeds this */
/* used by trees.c: */
/* Didn't use ct_data typedef below to supress compiler warning */
struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
struct tree_desc_s l_desc; /* desc. for literal tree */
struct tree_desc_s d_desc; /* desc. for distance tree */
struct tree_desc_s bl_desc; /* desc. for bit length tree */
ush bl_count[MAX_BITS+1];
/* number of codes at each bit length for an optimal tree */
int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
int heap_len; /* number of elements in the heap */
int heap_max; /* element of largest frequency */
/* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
* The same heap array is used to build all trees.
*/
uch depth[2*L_CODES+1];
/* Depth of each subtree used as tie breaker for trees of equal frequency
*/
uchf *l_buf; /* buffer for literals or lengths */
uInt lit_bufsize;
/* Size of match buffer for literals/lengths. There are 4 reasons for
* limiting lit_bufsize to 64K:
* - frequencies can be kept in 16 bit counters
* - if compression is not successful for the first block, all input
* data is still in the window so we can still emit a stored block even
* when input comes from standard input. (This can also be done for
* all blocks if lit_bufsize is not greater than 32K.)
* - if compression is not successful for a file smaller than 64K, we can
* even emit a stored file instead of a stored block (saving 5 bytes).
* This is applicable only for zip (not gzip or zlib).
* - creating new Huffman trees less frequently may not provide fast
* adaptation to changes in the input data statistics. (Take for
* example a binary file with poorly compressible code followed by
* a highly compressible string table.) Smaller buffer sizes give
* fast adaptation but have of course the overhead of transmitting
* trees more frequently.
* - I can't count above 4
*/
uInt last_lit; /* running index in l_buf */
ushf *d_buf;
/* Buffer for distances. To simplify the code, d_buf and l_buf have
* the same number of elements. To use different lengths, an extra flag
* array would be necessary.
*/
ulg opt_len; /* bit length of current block with optimal trees */
ulg static_len; /* bit length of current block with static trees */
uInt matches; /* number of string matches in current block */
int last_eob_len; /* bit length of EOB code for last block */
#ifdef DEBUG
ulg compressed_len; /* total bit length of compressed file mod 2^32 */
ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
#endif
ush bi_buf;
/* Output buffer. bits are inserted starting at the bottom (least
* significant bits).
*/
int bi_valid;
/* Number of valid bits in bi_buf. All bits above the last valid bit
* are always zero.
*/
} FAR deflate_state;
/* Output a byte on the stream.
* IN assertion: there is enough room in pending_buf.
*/
#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
/* Minimum amount of lookahead, except at the end of the input file.
* See deflate.c for comments about the MIN_MATCH+1.
*/
#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
/* In order to simplify the code, particularly on 16 bit machines, match
* distances are limited to MAX_DIST instead of WSIZE.
*/
/* in trees.c */
void _tr_init OF((deflate_state *s));
int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
void _tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len,
int eof));
void _tr_align OF((deflate_state *s));
void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
int eof));
#define d_code(dist) \
((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
/* Mapping from a distance to a distance code. dist is the distance - 1 and
* must not have side effects. _dist_code[256] and _dist_code[257] are never
* used.
*/
#ifndef DEBUG
/* Inline versions of _tr_tally for speed: */
#if defined(GEN_TREES_H) || !defined(STDC)
extern uch _length_code[];
extern uch _dist_code[];
#else
extern const uch _length_code[];
extern const uch _dist_code[];
#endif
# define _tr_tally_lit(s, c, flush) \
{ uch cc = (c); \
s->d_buf[s->last_lit] = 0; \
s->l_buf[s->last_lit++] = cc; \
s->dyn_ltree[cc].Freq++; \
flush = (s->last_lit == s->lit_bufsize-1); \
}
# define _tr_tally_dist(s, distance, length, flush) \
{ uch len = (length); \
ush dist = (distance); \
s->d_buf[s->last_lit] = dist; \
s->l_buf[s->last_lit++] = len; \
dist--; \
s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
s->dyn_dtree[d_code(dist)].Freq++; \
flush = (s->last_lit == s->lit_bufsize-1); \
}
#else
# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
# define _tr_tally_dist(s, distance, length, flush) \
flush = _tr_tally(s, distance, length)
#endif
#endif

875
zlib/gzio.c Normal file
View File

@ -0,0 +1,875 @@
/* gzio.c -- IO on .gz files
* Copyright (C) 1995-2002 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*
* Compile this file with -DNO_DEFLATE to avoid the compression code.
*/
/* @(#) $Id$ */
#include <stdio.h>
#include "zutil.h"
struct internal_state {int dummy;}; /* for buggy compilers */
#ifndef Z_BUFSIZE
# ifdef MAXSEG_64K
# define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
# else
# define Z_BUFSIZE 16384
# endif
#endif
#ifndef Z_PRINTF_BUFSIZE
# define Z_PRINTF_BUFSIZE 4096
#endif
#define ALLOC(size) malloc(size)
#define TRYFREE(p) {if (p) free(p);}
static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
/* gzip flag byte */
#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
#define COMMENT 0x10 /* bit 4 set: file comment present */
#define RESERVED 0xE0 /* bits 5..7: reserved */
typedef struct gz_stream {
z_stream stream;
int z_err; /* error code for last stream operation */
int z_eof; /* set if end of input file */
FILE *file; /* .gz file */
Byte *inbuf; /* input buffer */
Byte *outbuf; /* output buffer */
uLong crc; /* crc32 of uncompressed data */
char *msg; /* error message */
char *path; /* path name for debugging only */
int transparent; /* 1 if input file is not a .gz file */
char mode; /* 'w' or 'r' */
long startpos; /* start of compressed data in file (header skipped) */
} gz_stream;
local gzFile gz_open OF((const char *path, const char *mode, int fd));
local int do_flush OF((gzFile file, int flush));
local int get_byte OF((gz_stream *s));
local void check_header OF((gz_stream *s));
local int destroy OF((gz_stream *s));
local void putLong OF((FILE *file, uLong x));
local uLong getLong OF((gz_stream *s));
/* ===========================================================================
Opens a gzip (.gz) file for reading or writing. The mode parameter
is as in fopen ("rb" or "wb"). The file is given either by file descriptor
or path name (if fd == -1).
gz_open return NULL if the file could not be opened or if there was
insufficient memory to allocate the (de)compression state; errno
can be checked to distinguish the two cases (if errno is zero, the
zlib error is Z_MEM_ERROR).
*/
local gzFile gz_open (path, mode, fd)
const char *path;
const char *mode;
int fd;
{
int err;
int level = Z_DEFAULT_COMPRESSION; /* compression level */
int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
char *p = (char*)mode;
gz_stream *s;
char fmode[80]; /* copy of mode, without the compression level */
char *m = fmode;
if (!path || !mode) return Z_NULL;
s = (gz_stream *)ALLOC(sizeof(gz_stream));
if (!s) return Z_NULL;
s->stream.zalloc = (alloc_func)0;
s->stream.zfree = (free_func)0;
s->stream.opaque = (voidpf)0;
s->stream.next_in = s->inbuf = Z_NULL;
s->stream.next_out = s->outbuf = Z_NULL;
s->stream.avail_in = s->stream.avail_out = 0;
s->file = NULL;
s->z_err = Z_OK;
s->z_eof = 0;
s->crc = crc32(0L, Z_NULL, 0);
s->msg = NULL;
s->transparent = 0;
s->path = (char*)ALLOC(strlen(path)+1);
if (s->path == NULL) {
return destroy(s), (gzFile)Z_NULL;
}
strcpy(s->path, path); /* do this early for debugging */
s->mode = '\0';
do {
if (*p == 'r') s->mode = 'r';
if (*p == 'w' || *p == 'a') s->mode = 'w';
if (*p >= '0' && *p <= '9') {
level = *p - '0';
} else if (*p == 'f') {
strategy = Z_FILTERED;
} else if (*p == 'h') {
strategy = Z_HUFFMAN_ONLY;
} else {
*m++ = *p; /* copy the mode */
}
} while (*p++ && m != fmode + sizeof(fmode));
if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
if (s->mode == 'w') {
#ifdef NO_DEFLATE
err = Z_STREAM_ERROR;
#else
err = deflateInit2(&(s->stream), level,
Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
/* windowBits is passed < 0 to suppress zlib header */
s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
#endif
if (err != Z_OK || s->outbuf == Z_NULL) {
return destroy(s), (gzFile)Z_NULL;
}
} else {
s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
err = inflateInit2(&(s->stream), -MAX_WBITS);
/* windowBits is passed < 0 to tell that there is no zlib header.
* Note that in this case inflate *requires* an extra "dummy" byte
* after the compressed stream in order to complete decompression and
* return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
* present after the compressed stream.
*/
if (err != Z_OK || s->inbuf == Z_NULL) {
return destroy(s), (gzFile)Z_NULL;
}
}
s->stream.avail_out = Z_BUFSIZE;
errno = 0;
s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
if (s->file == NULL) {
return destroy(s), (gzFile)Z_NULL;
}
if (s->mode == 'w') {
/* Write a very simple .gz header:
*/
fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
s->startpos = 10L;
/* We use 10L instead of ftell(s->file) to because ftell causes an
* fflush on some systems. This version of the library doesn't use
* startpos anyway in write mode, so this initialization is not
* necessary.
*/
} else {
check_header(s); /* skip the .gz header */
s->startpos = (ftell(s->file) - s->stream.avail_in);
}
return (gzFile)s;
}
/* ===========================================================================
Opens a gzip (.gz) file for reading or writing.
*/
gzFile ZEXPORT gzopen (path, mode)
const char *path;
const char *mode;
{
return gz_open (path, mode, -1);
}
/* ===========================================================================
Associate a gzFile with the file descriptor fd. fd is not dup'ed here
to mimic the behavio(u)r of fdopen.
*/
gzFile ZEXPORT gzdopen (fd, mode)
int fd;
const char *mode;
{
char name[20];
if (fd < 0) return (gzFile)Z_NULL;
sprintf(name, "<fd:%d>", fd); /* for debugging */
return gz_open (name, mode, fd);
}
/* ===========================================================================
* Update the compression level and strategy
*/
int ZEXPORT gzsetparams (file, level, strategy)
gzFile file;
int level;
int strategy;
{
gz_stream *s = (gz_stream*)file;
if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
/* Make room to allow flushing */
if (s->stream.avail_out == 0) {
s->stream.next_out = s->outbuf;
if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
s->z_err = Z_ERRNO;
}
s->stream.avail_out = Z_BUFSIZE;
}
return deflateParams (&(s->stream), level, strategy);
}
/* ===========================================================================
Read a byte from a gz_stream; update next_in and avail_in. Return EOF
for end of file.
IN assertion: the stream s has been sucessfully opened for reading.
*/
local int get_byte(s)
gz_stream *s;
{
if (s->z_eof) return EOF;
if (s->stream.avail_in == 0) {
errno = 0;
s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
if (s->stream.avail_in == 0) {
s->z_eof = 1;
if (ferror(s->file)) s->z_err = Z_ERRNO;
return EOF;
}
s->stream.next_in = s->inbuf;
}
s->stream.avail_in--;
return *(s->stream.next_in)++;
}
/* ===========================================================================
Check the gzip header of a gz_stream opened for reading. Set the stream
mode to transparent if the gzip magic header is not present; set s->err
to Z_DATA_ERROR if the magic header is present but the rest of the header
is incorrect.
IN assertion: the stream s has already been created sucessfully;
s->stream.avail_in is zero for the first time, but may be non-zero
for concatenated .gz files.
*/
local void check_header(s)
gz_stream *s;
{
int method; /* method byte */
int flags; /* flags byte */
uInt len;
int c;
/* Check the gzip magic header */
for (len = 0; len < 2; len++) {
c = get_byte(s);
if (c != gz_magic[len]) {
if (len != 0) s->stream.avail_in++, s->stream.next_in--;
if (c != EOF) {
s->stream.avail_in++, s->stream.next_in--;
s->transparent = 1;
}
s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
return;
}
}
method = get_byte(s);
flags = get_byte(s);
if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
s->z_err = Z_DATA_ERROR;
return;
}
/* Discard time, xflags and OS code: */
for (len = 0; len < 6; len++) (void)get_byte(s);
if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
len = (uInt)get_byte(s);
len += ((uInt)get_byte(s))<<8;
/* len is garbage if EOF but the loop below will quit anyway */
while (len-- != 0 && get_byte(s) != EOF) ;
}
if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
while ((c = get_byte(s)) != 0 && c != EOF) ;
}
if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
while ((c = get_byte(s)) != 0 && c != EOF) ;
}
if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
for (len = 0; len < 2; len++) (void)get_byte(s);
}
s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
}
/* ===========================================================================
* Cleanup then free the given gz_stream. Return a zlib error code.
Try freeing in the reverse order of allocations.
*/
local int destroy (s)
gz_stream *s;
{
int err = Z_OK;
if (!s) return Z_STREAM_ERROR;
TRYFREE(s->msg);
if (s->stream.state != NULL) {
if (s->mode == 'w') {
#ifdef NO_DEFLATE
err = Z_STREAM_ERROR;
#else
err = deflateEnd(&(s->stream));
#endif
} else if (s->mode == 'r') {
err = inflateEnd(&(s->stream));
}
}
if (s->file != NULL && fclose(s->file)) {
#ifdef ESPIPE
if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
#endif
err = Z_ERRNO;
}
if (s->z_err < 0) err = s->z_err;
TRYFREE(s->inbuf);
TRYFREE(s->outbuf);
TRYFREE(s->path);
TRYFREE(s);
return err;
}
/* ===========================================================================
Reads the given number of uncompressed bytes from the compressed file.
gzread returns the number of bytes actually read (0 for end of file).
*/
int ZEXPORT gzread (file, buf, len)
gzFile file;
voidp buf;
unsigned len;
{
gz_stream *s = (gz_stream*)file;
Bytef *start = (Bytef*)buf; /* starting point for crc computation */
Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */
if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
if (s->z_err == Z_STREAM_END) return 0; /* EOF */
next_out = (Byte*)buf;
s->stream.next_out = (Bytef*)buf;
s->stream.avail_out = len;
while (s->stream.avail_out != 0) {
if (s->transparent) {
/* Copy first the lookahead bytes: */
uInt n = s->stream.avail_in;
if (n > s->stream.avail_out) n = s->stream.avail_out;
if (n > 0) {
zmemcpy(s->stream.next_out, s->stream.next_in, n);
next_out += n;
s->stream.next_out = next_out;
s->stream.next_in += n;
s->stream.avail_out -= n;
s->stream.avail_in -= n;
}
if (s->stream.avail_out > 0) {
s->stream.avail_out -= fread(next_out, 1, s->stream.avail_out,
s->file);
}
len -= s->stream.avail_out;
s->stream.total_in += (uLong)len;
s->stream.total_out += (uLong)len;
if (len == 0) s->z_eof = 1;
return (int)len;
}
if (s->stream.avail_in == 0 && !s->z_eof) {
errno = 0;
s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
if (s->stream.avail_in == 0) {
s->z_eof = 1;
if (ferror(s->file)) {
s->z_err = Z_ERRNO;
break;
}
}
s->stream.next_in = s->inbuf;
}
s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
if (s->z_err == Z_STREAM_END) {
/* Check CRC and original size */
s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
start = s->stream.next_out;
if (getLong(s) != s->crc) {
s->z_err = Z_DATA_ERROR;
} else {
(void)getLong(s);
/* The uncompressed length returned by above getlong() may
* be different from s->stream.total_out) in case of
* concatenated .gz files. Check for such files:
*/
check_header(s);
if (s->z_err == Z_OK) {
uLong total_in = s->stream.total_in;
uLong total_out = s->stream.total_out;
inflateReset(&(s->stream));
s->stream.total_in = total_in;
s->stream.total_out = total_out;
s->crc = crc32(0L, Z_NULL, 0);
}
}
}
if (s->z_err != Z_OK || s->z_eof) break;
}
s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
return (int)(len - s->stream.avail_out);
}
/* ===========================================================================
Reads one byte from the compressed file. gzgetc returns this byte
or -1 in case of end of file or error.
*/
int ZEXPORT gzgetc(file)
gzFile file;
{
unsigned char c;
return gzread(file, &c, 1) == 1 ? c : -1;
}
/* ===========================================================================
Reads bytes from the compressed file until len-1 characters are
read, or a newline character is read and transferred to buf, or an
end-of-file condition is encountered. The string is then terminated
with a null character.
gzgets returns buf, or Z_NULL in case of error.
The current implementation is not optimized at all.
*/
char * ZEXPORT gzgets(file, buf, len)
gzFile file;
char *buf;
int len;
{
char *b = buf;
if (buf == Z_NULL || len <= 0) return Z_NULL;
while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
*buf = '\0';
return b == buf && len > 0 ? Z_NULL : b;
}
#ifndef NO_DEFLATE
/* ===========================================================================
Writes the given number of uncompressed bytes into the compressed file.
gzwrite returns the number of bytes actually written (0 in case of error).
*/
int ZEXPORT gzwrite (file, buf, len)
gzFile file;
const voidp buf;
unsigned len;
{
gz_stream *s = (gz_stream*)file;
if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
s->stream.next_in = (Bytef*)buf;
s->stream.avail_in = len;
while (s->stream.avail_in != 0) {
if (s->stream.avail_out == 0) {
s->stream.next_out = s->outbuf;
if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
s->z_err = Z_ERRNO;
break;
}
s->stream.avail_out = Z_BUFSIZE;
}
s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
if (s->z_err != Z_OK) break;
}
s->crc = crc32(s->crc, (const Bytef *)buf, len);
return (int)(len - s->stream.avail_in);
}
/* ===========================================================================
Converts, formats, and writes the args to the compressed file under
control of the format string, as in fprintf. gzprintf returns the number of
uncompressed bytes actually written (0 in case of error).
*/
#ifdef STDC
#include <stdarg.h>
int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
{
char buf[Z_PRINTF_BUFSIZE];
va_list va;
int len;
va_start(va, format);
#ifdef HAS_vsnprintf
(void)vsnprintf(buf, sizeof(buf), format, va);
#else
(void)vsprintf(buf, format, va);
#endif
va_end(va);
len = strlen(buf); /* some *sprintf don't return the nb of bytes written */
if (len <= 0) return 0;
return gzwrite(file, buf, (unsigned)len);
}
#else /* not ANSI C */
int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
gzFile file;
const char *format;
int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
{
char buf[Z_PRINTF_BUFSIZE];
int len;
#ifdef HAS_snprintf
snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
#else
sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
#endif
len = strlen(buf); /* old sprintf doesn't return the nb of bytes written */
if (len <= 0) return 0;
return gzwrite(file, buf, len);
}
#endif
/* ===========================================================================
Writes c, converted to an unsigned char, into the compressed file.
gzputc returns the value that was written, or -1 in case of error.
*/
int ZEXPORT gzputc(file, c)
gzFile file;
int c;
{
unsigned char cc = (unsigned char) c; /* required for big endian systems */
return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
}
/* ===========================================================================
Writes the given null-terminated string to the compressed file, excluding
the terminating null character.
gzputs returns the number of characters written, or -1 in case of error.
*/
int ZEXPORT gzputs(file, s)
gzFile file;
const char *s;
{
return gzwrite(file, (char*)s, (unsigned)strlen(s));
}
/* ===========================================================================
Flushes all pending output into the compressed file. The parameter
flush is as in the deflate() function.
*/
local int do_flush (file, flush)
gzFile file;
int flush;
{
uInt len;
int done = 0;
gz_stream *s = (gz_stream*)file;
if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
s->stream.avail_in = 0; /* should be zero already anyway */
for (;;) {
len = Z_BUFSIZE - s->stream.avail_out;
if (len != 0) {
if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
s->z_err = Z_ERRNO;
return Z_ERRNO;
}
s->stream.next_out = s->outbuf;
s->stream.avail_out = Z_BUFSIZE;
}
if (done) break;
s->z_err = deflate(&(s->stream), flush);
/* Ignore the second of two consecutive flushes: */
if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
/* deflate has finished flushing only when it hasn't used up
* all the available space in the output buffer:
*/
done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
}
return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
}
int ZEXPORT gzflush (file, flush)
gzFile file;
int flush;
{
gz_stream *s = (gz_stream*)file;
int err = do_flush (file, flush);
if (err) return err;
fflush(s->file);
return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
}
#endif /* NO_DEFLATE */
/* ===========================================================================
Sets the starting position for the next gzread or gzwrite on the given
compressed file. The offset represents a number of bytes in the
gzseek returns the resulting offset location as measured in bytes from
the beginning of the uncompressed stream, or -1 in case of error.
SEEK_END is not implemented, returns error.
In this version of the library, gzseek can be extremely slow.
*/
z_off_t ZEXPORT gzseek (file, offset, whence)
gzFile file;
z_off_t offset;
int whence;
{
gz_stream *s = (gz_stream*)file;
if (s == NULL || whence == SEEK_END ||
s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
return -1L;
}
if (s->mode == 'w') {
#ifdef NO_DEFLATE
return -1L;
#else
if (whence == SEEK_SET) {
offset -= s->stream.total_in;
}
if (offset < 0) return -1L;
/* At this point, offset is the number of zero bytes to write. */
if (s->inbuf == Z_NULL) {
s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
zmemzero(s->inbuf, Z_BUFSIZE);
}
while (offset > 0) {
uInt size = Z_BUFSIZE;
if (offset < Z_BUFSIZE) size = (uInt)offset;
size = gzwrite(file, s->inbuf, size);
if (size == 0) return -1L;
offset -= size;
}
return (z_off_t)s->stream.total_in;
#endif
}
/* Rest of function is for reading only */
/* compute absolute position */
if (whence == SEEK_CUR) {
offset += s->stream.total_out;
}
if (offset < 0) return -1L;
if (s->transparent) {
/* map to fseek */
s->stream.avail_in = 0;
s->stream.next_in = s->inbuf;
if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
s->stream.total_in = s->stream.total_out = (uLong)offset;
return offset;
}
/* For a negative seek, rewind and use positive seek */
if ((uLong)offset >= s->stream.total_out) {
offset -= s->stream.total_out;
} else if (gzrewind(file) < 0) {
return -1L;
}
/* offset is now the number of bytes to skip. */
if (offset != 0 && s->outbuf == Z_NULL) {
s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
}
while (offset > 0) {
int size = Z_BUFSIZE;
if (offset < Z_BUFSIZE) size = (int)offset;
size = gzread(file, s->outbuf, (uInt)size);
if (size <= 0) return -1L;
offset -= size;
}
return (z_off_t)s->stream.total_out;
}
/* ===========================================================================
Rewinds input file.
*/
int ZEXPORT gzrewind (file)
gzFile file;
{
gz_stream *s = (gz_stream*)file;
if (s == NULL || s->mode != 'r') return -1;
s->z_err = Z_OK;
s->z_eof = 0;
s->stream.avail_in = 0;
s->stream.next_in = s->inbuf;
s->crc = crc32(0L, Z_NULL, 0);
if (s->startpos == 0) { /* not a compressed file */
rewind(s->file);
return 0;
}
(void) inflateReset(&s->stream);
return fseek(s->file, s->startpos, SEEK_SET);
}
/* ===========================================================================
Returns the starting position for the next gzread or gzwrite on the
given compressed file. This position represents a number of bytes in the
uncompressed data stream.
*/
z_off_t ZEXPORT gztell (file)
gzFile file;
{
return gzseek(file, 0L, SEEK_CUR);
}
/* ===========================================================================
Returns 1 when EOF has previously been detected reading the given
input stream, otherwise zero.
*/
int ZEXPORT gzeof (file)
gzFile file;
{
gz_stream *s = (gz_stream*)file;
return (s == NULL || s->mode != 'r') ? 0 : s->z_eof;
}
/* ===========================================================================
Outputs a long in LSB order to the given file
*/
local void putLong (file, x)
FILE *file;
uLong x;
{
int n;
for (n = 0; n < 4; n++) {
fputc((int)(x & 0xff), file);
x >>= 8;
}
}
/* ===========================================================================
Reads a long in LSB order from the given gz_stream. Sets z_err in case
of error.
*/
local uLong getLong (s)
gz_stream *s;
{
uLong x = (uLong)get_byte(s);
int c;
x += ((uLong)get_byte(s))<<8;
x += ((uLong)get_byte(s))<<16;
c = get_byte(s);
if (c == EOF) s->z_err = Z_DATA_ERROR;
x += ((uLong)c)<<24;
return x;
}
/* ===========================================================================
Flushes all pending output if necessary, closes the compressed file
and deallocates all the (de)compression state.
*/
int ZEXPORT gzclose (file)
gzFile file;
{
int err;
gz_stream *s = (gz_stream*)file;
if (s == NULL) return Z_STREAM_ERROR;
if (s->mode == 'w') {
#ifdef NO_DEFLATE
return Z_STREAM_ERROR;
#else
err = do_flush (file, Z_FINISH);
if (err != Z_OK) return destroy((gz_stream*)file);
putLong (s->file, s->crc);
putLong (s->file, s->stream.total_in);
#endif
}
return destroy((gz_stream*)file);
}
/* ===========================================================================
Returns the error message for the last error which occured on the
given compressed file. errnum is set to zlib error number. If an
error occured in the file system and not in the compression library,
errnum is set to Z_ERRNO and the application may consult errno
to get the exact error code.
*/
const char* ZEXPORT gzerror (file, errnum)
gzFile file;
int *errnum;
{
char *m;
gz_stream *s = (gz_stream*)file;
if (s == NULL) {
*errnum = Z_STREAM_ERROR;
return (const char*)ERR_MSG(Z_STREAM_ERROR);
}
*errnum = s->z_err;
if (*errnum == Z_OK) return (const char*)"";
m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
TRYFREE(s->msg);
s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
strcpy(s->msg, s->path);
strcat(s->msg, ": ");
strcat(s->msg, m);
return (const char*)s->msg;
}

183
zlib/inffast.c Normal file
View File

@ -0,0 +1,183 @@
/* inffast.c -- process literals and length/distance pairs fast
* Copyright (C) 1995-2002 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zutil.h"
#include "inftrees.h"
#include "infblock.h"
#include "infcodes.h"
#include "infutil.h"
#include "inffast.h"
struct inflate_codes_state {int dummy;}; /* for buggy compilers */
/* simplify the use of the inflate_huft type with some defines */
#define exop word.what.Exop
#define bits word.what.Bits
/* macros for bit input with no checking and for returning unused bytes */
#define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
#define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
/* Called with number of bytes left to write in window at least 258
(the maximum string length) and number of input bytes available
at least ten. The ten bytes are six bytes for the longest length/
distance pair plus four bytes for overloading the bit buffer. */
int inflate_fast(bl, bd, tl, td, s, z)
uInt bl, bd;
inflate_huft *tl;
inflate_huft *td; /* need separate declaration for Borland C++ */
inflate_blocks_statef *s;
z_streamp z;
{
inflate_huft *t; /* temporary pointer */
uInt e; /* extra bits or operation */
uLong b; /* bit buffer */
uInt k; /* bits in bit buffer */
Bytef *p; /* input data pointer */
uInt n; /* bytes available there */
Bytef *q; /* output window write pointer */
uInt m; /* bytes to end of window or read pointer */
uInt ml; /* mask for literal/length tree */
uInt md; /* mask for distance tree */
uInt c; /* bytes to copy */
uInt d; /* distance back to copy from */
Bytef *r; /* copy source pointer */
/* load input, output, bit values */
LOAD
/* initialize masks */
ml = inflate_mask[bl];
md = inflate_mask[bd];
/* do until not enough input or output space for fast loop */
do { /* assume called with m >= 258 && n >= 10 */
/* get literal/length code */
GRABBITS(20) /* max bits for literal/length code */
if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
{
DUMPBITS(t->bits)
Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
"inflate: * literal '%c'\n" :
"inflate: * literal 0x%02x\n", t->base));
*q++ = (Byte)t->base;
m--;
continue;
}
do {
DUMPBITS(t->bits)
if (e & 16)
{
/* get extra bits for length */
e &= 15;
c = t->base + ((uInt)b & inflate_mask[e]);
DUMPBITS(e)
Tracevv((stderr, "inflate: * length %u\n", c));
/* decode distance base of block to copy */
GRABBITS(15); /* max bits for distance code */
e = (t = td + ((uInt)b & md))->exop;
do {
DUMPBITS(t->bits)
if (e & 16)
{
/* get extra bits to add to distance base */
e &= 15;
GRABBITS(e) /* get extra bits (up to 13) */
d = t->base + ((uInt)b & inflate_mask[e]);
DUMPBITS(e)
Tracevv((stderr, "inflate: * distance %u\n", d));
/* do the copy */
m -= c;
r = q - d;
if (r < s->window) /* wrap if needed */
{
do {
r += s->end - s->window; /* force pointer in window */
} while (r < s->window); /* covers invalid distances */
e = s->end - r;
if (c > e)
{
c -= e; /* wrapped copy */
do {
*q++ = *r++;
} while (--e);
r = s->window;
do {
*q++ = *r++;
} while (--c);
}
else /* normal copy */
{
*q++ = *r++; c--;
*q++ = *r++; c--;
do {
*q++ = *r++;
} while (--c);
}
}
else /* normal copy */
{
*q++ = *r++; c--;
*q++ = *r++; c--;
do {
*q++ = *r++;
} while (--c);
}
break;
}
else if ((e & 64) == 0)
{
t += t->base;
e = (t += ((uInt)b & inflate_mask[e]))->exop;
}
else
{
z->msg = (char*)"invalid distance code";
UNGRAB
UPDATE
return Z_DATA_ERROR;
}
} while (1);
break;
}
if ((e & 64) == 0)
{
t += t->base;
if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
{
DUMPBITS(t->bits)
Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
"inflate: * literal '%c'\n" :
"inflate: * literal 0x%02x\n", t->base));
*q++ = (Byte)t->base;
m--;
break;
}
}
else if (e & 32)
{
Tracevv((stderr, "inflate: * end of block\n"));
UNGRAB
UPDATE
return Z_STREAM_END;
}
else
{
z->msg = (char*)"invalid literal/length code";
UNGRAB
UPDATE
return Z_DATA_ERROR;
}
} while (1);
} while (m >= 258 && n >= 10);
/* not enough input or output--restore pointers and return */
UNGRAB
UPDATE
return Z_OK;
}

17
zlib/inffast.h Normal file
View File

@ -0,0 +1,17 @@
/* inffast.h -- header to use inffast.c
* Copyright (C) 1995-2002 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* WARNING: this file should *not* be used by applications. It is
part of the implementation of the compression library and is
subject to change. Applications should only use zlib.h.
*/
extern int inflate_fast OF((
uInt,
uInt,
inflate_huft *,
inflate_huft *,
inflate_blocks_statef *,
z_streamp ));

151
zlib/inffixed.h Normal file
View File

@ -0,0 +1,151 @@
/* inffixed.h -- table for decoding fixed codes
* Generated automatically by the maketree.c program
*/
/* WARNING: this file should *not* be used by applications. It is
part of the implementation of the compression library and is
subject to change. Applications should only use zlib.h.
*/
local uInt fixed_bl = 9;
local uInt fixed_bd = 5;
local inflate_huft fixed_tl[] = {
{{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
{{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},192},
{{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},160},
{{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},224},
{{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},144},
{{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},208},
{{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},176},
{{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},240},
{{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
{{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},200},
{{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},168},
{{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},232},
{{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},152},
{{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},216},
{{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},184},
{{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},248},
{{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
{{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},196},
{{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},164},
{{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},228},
{{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},148},
{{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},212},
{{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},180},
{{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},244},
{{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
{{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},204},
{{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},172},
{{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},236},
{{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},156},
{{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},220},
{{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},188},
{{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},252},
{{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
{{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},194},
{{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},162},
{{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},226},
{{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},146},
{{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},210},
{{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},178},
{{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},242},
{{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
{{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},202},
{{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},170},
{{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},234},
{{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},154},
{{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},218},
{{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},186},
{{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},250},
{{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
{{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},198},
{{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},166},
{{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},230},
{{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},150},
{{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},214},
{{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},182},
{{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},246},
{{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
{{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},206},
{{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},174},
{{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},238},
{{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},158},
{{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},222},
{{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},190},
{{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},254},
{{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
{{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},193},
{{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},161},
{{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},225},
{{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},145},
{{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},209},
{{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},177},
{{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},241},
{{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
{{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},201},
{{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},169},
{{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},233},
{{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},153},
{{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},217},
{{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},185},
{{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},249},
{{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
{{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},197},
{{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},165},
{{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},229},
{{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},149},
{{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},213},
{{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},181},
{{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},245},
{{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
{{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},205},
{{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},173},
{{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},237},
{{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},157},
{{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},221},
{{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},189},
{{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},253},
{{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
{{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},195},
{{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},163},
{{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},227},
{{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},147},
{{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},211},
{{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},179},
{{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},243},
{{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
{{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},203},
{{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},171},
{{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},235},
{{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},155},
{{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},219},
{{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},187},
{{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},251},
{{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
{{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},199},
{{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},167},
{{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},231},
{{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},151},
{{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},215},
{{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},183},
{{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},247},
{{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
{{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},207},
{{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},175},
{{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},239},
{{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},159},
{{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},223},
{{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},191},
{{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},255}
};
local inflate_huft fixed_td[] = {
{{{80,5}},1}, {{{87,5}},257}, {{{83,5}},17}, {{{91,5}},4097},
{{{81,5}},5}, {{{89,5}},1025}, {{{85,5}},65}, {{{93,5}},16385},
{{{80,5}},3}, {{{88,5}},513}, {{{84,5}},33}, {{{92,5}},8193},
{{{82,5}},9}, {{{90,5}},2049}, {{{86,5}},129}, {{{192,5}},24577},
{{{80,5}},2}, {{{87,5}},385}, {{{83,5}},25}, {{{91,5}},6145},
{{{81,5}},7}, {{{89,5}},1537}, {{{85,5}},97}, {{{93,5}},24577},
{{{80,5}},4}, {{{88,5}},769}, {{{84,5}},49}, {{{92,5}},12289},
{{{82,5}},13}, {{{90,5}},3073}, {{{86,5}},193}, {{{192,5}},24577}
};

366
zlib/inflate.c Normal file
View File

@ -0,0 +1,366 @@
/* inflate.c -- zlib interface to inflate modules
* Copyright (C) 1995-2002 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zutil.h"
#include "infblock.h"
struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
typedef enum {
METHOD, /* waiting for method byte */
FLAG, /* waiting for flag byte */
DICT4, /* four dictionary check bytes to go */
DICT3, /* three dictionary check bytes to go */
DICT2, /* two dictionary check bytes to go */
DICT1, /* one dictionary check byte to go */
DICT0, /* waiting for inflateSetDictionary */
BLOCKS, /* decompressing blocks */
CHECK4, /* four check bytes to go */
CHECK3, /* three check bytes to go */
CHECK2, /* two check bytes to go */
CHECK1, /* one check byte to go */
DONE, /* finished check, done */
BAD} /* got an error--stay here */
inflate_mode;
/* inflate private state */
struct internal_state {
/* mode */
inflate_mode mode; /* current inflate mode */
/* mode dependent information */
union {
uInt method; /* if FLAGS, method byte */
struct {
uLong was; /* computed check value */
uLong need; /* stream check value */
} check; /* if CHECK, check values to compare */
uInt marker; /* if BAD, inflateSync's marker bytes count */
} sub; /* submode */
/* mode independent information */
int nowrap; /* flag for no wrapper */
uInt wbits; /* log2(window size) (8..15, defaults to 15) */
inflate_blocks_statef
*blocks; /* current inflate_blocks state */
};
int ZEXPORT inflateReset(z)
z_streamp z;
{
if (z == Z_NULL || z->state == Z_NULL)
return Z_STREAM_ERROR;
z->total_in = z->total_out = 0;
z->msg = Z_NULL;
z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
inflate_blocks_reset(z->state->blocks, z, Z_NULL);
Tracev((stderr, "inflate: reset\n"));
return Z_OK;
}
int ZEXPORT inflateEnd(z)
z_streamp z;
{
if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
return Z_STREAM_ERROR;
if (z->state->blocks != Z_NULL)
inflate_blocks_free(z->state->blocks, z);
ZFREE(z, z->state);
z->state = Z_NULL;
Tracev((stderr, "inflate: end\n"));
return Z_OK;
}
int ZEXPORT inflateInit2_(z, w, version, stream_size)
z_streamp z;
int w;
const char *version;
int stream_size;
{
if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
stream_size != sizeof(z_stream))
return Z_VERSION_ERROR;
/* initialize state */
if (z == Z_NULL)
return Z_STREAM_ERROR;
z->msg = Z_NULL;
if (z->zalloc == Z_NULL)
{
z->zalloc = zcalloc;
z->opaque = (voidpf)0;
}
if (z->zfree == Z_NULL) z->zfree = zcfree;
if ((z->state = (struct internal_state FAR *)
ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
return Z_MEM_ERROR;
z->state->blocks = Z_NULL;
/* handle undocumented nowrap option (no zlib header or check) */
z->state->nowrap = 0;
if (w < 0)
{
w = - w;
z->state->nowrap = 1;
}
/* set window size */
if (w < 8 || w > 15)
{
inflateEnd(z);
return Z_STREAM_ERROR;
}
z->state->wbits = (uInt)w;
/* create inflate_blocks state */
if ((z->state->blocks =
inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
== Z_NULL)
{
inflateEnd(z);
return Z_MEM_ERROR;
}
Tracev((stderr, "inflate: allocated\n"));
/* reset state */
inflateReset(z);
return Z_OK;
}
int ZEXPORT inflateInit_(z, version, stream_size)
z_streamp z;
const char *version;
int stream_size;
{
return inflateInit2_(z, DEF_WBITS, version, stream_size);
}
#define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
int ZEXPORT inflate(z, f)
z_streamp z;
int f;
{
int r;
uInt b;
if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
return Z_STREAM_ERROR;
f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
r = Z_BUF_ERROR;
while (1) switch (z->state->mode)
{
case METHOD:
NEEDBYTE
if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
{
z->state->mode = BAD;
z->msg = (char*)"unknown compression method";
z->state->sub.marker = 5; /* can't try inflateSync */
break;
}
if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
{
z->state->mode = BAD;
z->msg = (char*)"invalid window size";
z->state->sub.marker = 5; /* can't try inflateSync */
break;
}
z->state->mode = FLAG;
case FLAG:
NEEDBYTE
b = NEXTBYTE;
if (((z->state->sub.method << 8) + b) % 31)
{
z->state->mode = BAD;
z->msg = (char*)"incorrect header check";
z->state->sub.marker = 5; /* can't try inflateSync */
break;
}
Tracev((stderr, "inflate: zlib header ok\n"));
if (!(b & PRESET_DICT))
{
z->state->mode = BLOCKS;
break;
}
z->state->mode = DICT4;
case DICT4:
NEEDBYTE
z->state->sub.check.need = (uLong)NEXTBYTE << 24;
z->state->mode = DICT3;
case DICT3:
NEEDBYTE
z->state->sub.check.need += (uLong)NEXTBYTE << 16;
z->state->mode = DICT2;
case DICT2:
NEEDBYTE
z->state->sub.check.need += (uLong)NEXTBYTE << 8;
z->state->mode = DICT1;
case DICT1:
NEEDBYTE
z->state->sub.check.need += (uLong)NEXTBYTE;
z->adler = z->state->sub.check.need;
z->state->mode = DICT0;
return Z_NEED_DICT;
case DICT0:
z->state->mode = BAD;
z->msg = (char*)"need dictionary";
z->state->sub.marker = 0; /* can try inflateSync */
return Z_STREAM_ERROR;
case BLOCKS:
r = inflate_blocks(z->state->blocks, z, r);
if (r == Z_DATA_ERROR)
{
z->state->mode = BAD;
z->state->sub.marker = 0; /* can try inflateSync */
break;
}
if (r == Z_OK)
r = f;
if (r != Z_STREAM_END)
return r;
r = f;
inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
if (z->state->nowrap)
{
z->state->mode = DONE;
break;
}
z->state->mode = CHECK4;
case CHECK4:
NEEDBYTE
z->state->sub.check.need = (uLong)NEXTBYTE << 24;
z->state->mode = CHECK3;
case CHECK3:
NEEDBYTE
z->state->sub.check.need += (uLong)NEXTBYTE << 16;
z->state->mode = CHECK2;
case CHECK2:
NEEDBYTE
z->state->sub.check.need += (uLong)NEXTBYTE << 8;
z->state->mode = CHECK1;
case CHECK1:
NEEDBYTE
z->state->sub.check.need += (uLong)NEXTBYTE;
if (z->state->sub.check.was != z->state->sub.check.need)
{
z->state->mode = BAD;
z->msg = (char*)"incorrect data check";
z->state->sub.marker = 5; /* can't try inflateSync */
break;
}
Tracev((stderr, "inflate: zlib check ok\n"));
z->state->mode = DONE;
case DONE:
return Z_STREAM_END;
case BAD:
return Z_DATA_ERROR;
default:
return Z_STREAM_ERROR;
}
#ifdef NEED_DUMMY_RETURN
return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
#endif
}
int ZEXPORT inflateSetDictionary(z, dictionary, dictLength)
z_streamp z;
const Bytef *dictionary;
uInt dictLength;
{
uInt length = dictLength;
if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
return Z_STREAM_ERROR;
if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
z->adler = 1L;
if (length >= ((uInt)1<<z->state->wbits))
{
length = (1<<z->state->wbits)-1;
dictionary += dictLength - length;
}
inflate_set_dictionary(z->state->blocks, dictionary, length);
z->state->mode = BLOCKS;
return Z_OK;
}
int ZEXPORT inflateSync(z)
z_streamp z;
{
uInt n; /* number of bytes to look at */
Bytef *p; /* pointer to bytes */
uInt m; /* number of marker bytes found in a row */
uLong r, w; /* temporaries to save total_in and total_out */
/* set up */
if (z == Z_NULL || z->state == Z_NULL)
return Z_STREAM_ERROR;
if (z->state->mode != BAD)
{
z->state->mode = BAD;
z->state->sub.marker = 0;
}
if ((n = z->avail_in) == 0)
return Z_BUF_ERROR;
p = z->next_in;
m = z->state->sub.marker;
/* search */
while (n && m < 4)
{
static const Byte mark[4] = {0, 0, 0xff, 0xff};
if (*p == mark[m])
m++;
else if (*p)
m = 0;
else
m = 4 - m;
p++, n--;
}
/* restore */
z->total_in += p - z->next_in;
z->next_in = p;
z->avail_in = n;
z->state->sub.marker = m;
/* return no joy or set up to restart on a new block */
if (m != 4)
return Z_DATA_ERROR;
r = z->total_in; w = z->total_out;
inflateReset(z);
z->total_in = r; z->total_out = w;
z->state->mode = BLOCKS;
return Z_OK;
}
/* Returns true if inflate is currently at the end of a block generated
* by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
* implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
* but removes the length bytes of the resulting empty stored block. When
* decompressing, PPP checks that at the end of input packet, inflate is
* waiting for these length bytes.
*/
int ZEXPORT inflateSyncPoint(z)
z_streamp z;
{
if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
return Z_STREAM_ERROR;
return inflate_blocks_sync_point(z->state->blocks);
}

454
zlib/inftrees.c Normal file
View File

@ -0,0 +1,454 @@
/* inftrees.c -- generate Huffman trees for efficient decoding
* Copyright (C) 1995-2002 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zutil.h"
#include "inftrees.h"
#if !defined(BUILDFIXED) && !defined(STDC)
# define BUILDFIXED /* non ANSI compilers may not accept inffixed.h */
#endif
const char inflate_copyright[] =
" inflate 1.1.4 Copyright 1995-2002 Mark Adler ";
/*
If you use the zlib library in a product, an acknowledgment is welcome
in the documentation of your product. If for some reason you cannot
include such an acknowledgment, I would appreciate that you keep this
copyright string in the executable of your product.
*/
struct internal_state {int dummy;}; /* for buggy compilers */
/* simplify the use of the inflate_huft type with some defines */
#define exop word.what.Exop
#define bits word.what.Bits
local int huft_build OF((
uIntf *, /* code lengths in bits */
uInt, /* number of codes */
uInt, /* number of "simple" codes */
const uIntf *, /* list of base values for non-simple codes */
const uIntf *, /* list of extra bits for non-simple codes */
inflate_huft * FAR*,/* result: starting table */
uIntf *, /* maximum lookup bits (returns actual) */
inflate_huft *, /* space for trees */
uInt *, /* hufts used in space */
uIntf * )); /* space for values */
/* Tables for deflate from PKZIP's appnote.txt. */
local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
/* see note #13 above about 258 */
local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
8193, 12289, 16385, 24577};
local const uInt cpdext[30] = { /* Extra bits for distance codes */
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
12, 12, 13, 13};
/*
Huffman code decoding is performed using a multi-level table lookup.
The fastest way to decode is to simply build a lookup table whose
size is determined by the longest code. However, the time it takes
to build this table can also be a factor if the data being decoded
is not very long. The most common codes are necessarily the
shortest codes, so those codes dominate the decoding time, and hence
the speed. The idea is you can have a shorter table that decodes the
shorter, more probable codes, and then point to subsidiary tables for
the longer codes. The time it costs to decode the longer codes is
then traded against the time it takes to make longer tables.
This results of this trade are in the variables lbits and dbits
below. lbits is the number of bits the first level table for literal/
length codes can decode in one step, and dbits is the same thing for
the distance codes. Subsequent tables are also less than or equal to
those sizes. These values may be adjusted either when all of the
codes are shorter than that, in which case the longest code length in
bits is used, or when the shortest code is *longer* than the requested
table size, in which case the length of the shortest code in bits is
used.
There are two different values for the two tables, since they code a
different number of possibilities each. The literal/length table
codes 286 possible values, or in a flat code, a little over eight
bits. The distance table codes 30 possible values, or a little less
than five bits, flat. The optimum values for speed end up being
about one bit more than those, so lbits is 8+1 and dbits is 5+1.
The optimum values may differ though from machine to machine, and
possibly even between compilers. Your mileage may vary.
*/
/* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
#define BMAX 15 /* maximum bit length of any code */
local int huft_build(b, n, s, d, e, t, m, hp, hn, v)
uIntf *b; /* code lengths in bits (all assumed <= BMAX) */
uInt n; /* number of codes (assumed <= 288) */
uInt s; /* number of simple-valued codes (0..s-1) */
const uIntf *d; /* list of base values for non-simple codes */
const uIntf *e; /* list of extra bits for non-simple codes */
inflate_huft * FAR *t; /* result: starting table */
uIntf *m; /* maximum lookup bits, returns actual */
inflate_huft *hp; /* space for trees */
uInt *hn; /* hufts used in space */
uIntf *v; /* working area: values in order of bit length */
/* Given a list of code lengths and a maximum table size, make a set of
tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
if the given code set is incomplete (the tables are still built in this
case), or Z_DATA_ERROR if the input is invalid. */
{
uInt a; /* counter for codes of length k */
uInt c[BMAX+1]; /* bit length count table */
uInt f; /* i repeats in table every f entries */
int g; /* maximum code length */
int h; /* table level */
register uInt i; /* counter, current code */
register uInt j; /* counter */
register int k; /* number of bits in current code */
int l; /* bits per table (returned in m) */
uInt mask; /* (1 << w) - 1, to avoid cc -O bug on HP */
register uIntf *p; /* pointer into c[], b[], or v[] */
inflate_huft *q; /* points to current table */
struct inflate_huft_s r; /* table entry for structure assignment */
inflate_huft *u[BMAX]; /* table stack */
register int w; /* bits before this table == (l * h) */
uInt x[BMAX+1]; /* bit offsets, then code stack */
uIntf *xp; /* pointer into x */
int y; /* number of dummy codes added */
uInt z; /* number of entries in current table */
/* Generate counts for each bit length */
p = c;
#define C0 *p++ = 0;
#define C2 C0 C0 C0 C0
#define C4 C2 C2 C2 C2
C4 /* clear c[]--assume BMAX+1 is 16 */
p = b; i = n;
do {
c[*p++]++; /* assume all entries <= BMAX */
} while (--i);
if (c[0] == n) /* null input--all zero length codes */
{
*t = (inflate_huft *)Z_NULL;
*m = 0;
return Z_OK;
}
/* Find minimum and maximum length, bound *m by those */
l = *m;
for (j = 1; j <= BMAX; j++)
if (c[j])
break;
k = j; /* minimum code length */
if ((uInt)l < j)
l = j;
for (i = BMAX; i; i--)
if (c[i])
break;
g = i; /* maximum code length */
if ((uInt)l > i)
l = i;
*m = l;
/* Adjust last length count to fill out codes, if needed */
for (y = 1 << j; j < i; j++, y <<= 1)
if ((y -= c[j]) < 0)
return Z_DATA_ERROR;
if ((y -= c[i]) < 0)
return Z_DATA_ERROR;
c[i] += y;
/* Generate starting offsets into the value table for each length */
x[1] = j = 0;
p = c + 1; xp = x + 2;
while (--i) { /* note that i == g from above */
*xp++ = (j += *p++);
}
/* Make a table of values in order of bit lengths */
p = b; i = 0;
do {
if ((j = *p++) != 0)
v[x[j]++] = i;
} while (++i < n);
n = x[g]; /* set n to length of v */
/* Generate the Huffman codes and for each, make the table entries */
x[0] = i = 0; /* first Huffman code is zero */
p = v; /* grab values in bit order */
h = -1; /* no tables yet--level -1 */
w = -l; /* bits decoded == (l * h) */
u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */
q = (inflate_huft *)Z_NULL; /* ditto */
z = 0; /* ditto */
/* go through the bit lengths (k already is bits in shortest code) */
for (; k <= g; k++)
{
a = c[k];
while (a--)
{
/* here i is the Huffman code of length k bits for value *p */
/* make tables up to required level */
while (k > w + l)
{
h++;
w += l; /* previous table always l bits */
/* compute minimum size table less than or equal to l bits */
z = g - w;
z = z > (uInt)l ? l : z; /* table size upper limit */
if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
{ /* too few codes for k-w bit table */
f -= a + 1; /* deduct codes from patterns left */
xp = c + k;
if (j < z)
while (++j < z) /* try smaller tables up to z bits */
{
if ((f <<= 1) <= *++xp)
break; /* enough codes to use up j bits */
f -= *xp; /* else deduct codes from patterns */
}
}
z = 1 << j; /* table entries for j-bit table */
/* allocate new table */
if (*hn + z > MANY) /* (note: doesn't matter for fixed) */
return Z_DATA_ERROR; /* overflow of MANY */
u[h] = q = hp + *hn;
*hn += z;
/* connect to last table, if there is one */
if (h)
{
x[h] = i; /* save pattern for backing up */
r.bits = (Byte)l; /* bits to dump before this table */
r.exop = (Byte)j; /* bits in this table */
j = i >> (w - l);
r.base = (uInt)(q - u[h-1] - j); /* offset to this table */
u[h-1][j] = r; /* connect to last table */
}
else
*t = q; /* first table is returned result */
}
/* set up table entry in r */
r.bits = (Byte)(k - w);
if (p >= v + n)
r.exop = 128 + 64; /* out of values--invalid code */
else if (*p < s)
{
r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */
r.base = *p++; /* simple code is just the value */
}
else
{
r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
r.base = d[*p++ - s];
}
/* fill code-like entries with r */
f = 1 << (k - w);
for (j = i >> w; j < z; j += f)
q[j] = r;
/* backwards increment the k-bit code i */
for (j = 1 << (k - 1); i & j; j >>= 1)
i ^= j;
i ^= j;
/* backup over finished tables */
mask = (1 << w) - 1; /* needed on HP, cc -O bug */
while ((i & mask) != x[h])
{
h--; /* don't need to update q */
w -= l;
mask = (1 << w) - 1;
}
}
}
/* Return Z_BUF_ERROR if we were given an incomplete table */
return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
}
int inflate_trees_bits(c, bb, tb, hp, z)
uIntf *c; /* 19 code lengths */
uIntf *bb; /* bits tree desired/actual depth */
inflate_huft * FAR *tb; /* bits tree result */
inflate_huft *hp; /* space for trees */
z_streamp z; /* for messages */
{
int r;
uInt hn = 0; /* hufts used in space */
uIntf *v; /* work area for huft_build */
if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
return Z_MEM_ERROR;
r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL,
tb, bb, hp, &hn, v);
if (r == Z_DATA_ERROR)
z->msg = (char*)"oversubscribed dynamic bit lengths tree";
else if (r == Z_BUF_ERROR || *bb == 0)
{
z->msg = (char*)"incomplete dynamic bit lengths tree";
r = Z_DATA_ERROR;
}
ZFREE(z, v);
return r;
}
int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z)
uInt nl; /* number of literal/length codes */
uInt nd; /* number of distance codes */
uIntf *c; /* that many (total) code lengths */
uIntf *bl; /* literal desired/actual bit depth */
uIntf *bd; /* distance desired/actual bit depth */
inflate_huft * FAR *tl; /* literal/length tree result */
inflate_huft * FAR *td; /* distance tree result */
inflate_huft *hp; /* space for trees */
z_streamp z; /* for messages */
{
int r;
uInt hn = 0; /* hufts used in space */
uIntf *v; /* work area for huft_build */
/* allocate work area */
if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
return Z_MEM_ERROR;
/* build literal/length tree */
r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
if (r != Z_OK || *bl == 0)
{
if (r == Z_DATA_ERROR)
z->msg = (char*)"oversubscribed literal/length tree";
else if (r != Z_MEM_ERROR)
{
z->msg = (char*)"incomplete literal/length tree";
r = Z_DATA_ERROR;
}
ZFREE(z, v);
return r;
}
/* build distance tree */
r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
if (r != Z_OK || (*bd == 0 && nl > 257))
{
if (r == Z_DATA_ERROR)
z->msg = (char*)"oversubscribed distance tree";
else if (r == Z_BUF_ERROR) {
#ifdef PKZIP_BUG_WORKAROUND
r = Z_OK;
}
#else
z->msg = (char*)"incomplete distance tree";
r = Z_DATA_ERROR;
}
else if (r != Z_MEM_ERROR)
{
z->msg = (char*)"empty distance tree with lengths";
r = Z_DATA_ERROR;
}
ZFREE(z, v);
return r;
#endif
}
/* done */
ZFREE(z, v);
return Z_OK;
}
/* build fixed tables only once--keep them here */
#ifdef BUILDFIXED
local int fixed_built = 0;
#define FIXEDH 544 /* number of hufts used by fixed tables */
local inflate_huft fixed_mem[FIXEDH];
local uInt fixed_bl;
local uInt fixed_bd;
local inflate_huft *fixed_tl;
local inflate_huft *fixed_td;
#else
#include "inffixed.h"
#endif
int inflate_trees_fixed(bl, bd, tl, td, z)
uIntf *bl; /* literal desired/actual bit depth */
uIntf *bd; /* distance desired/actual bit depth */
inflate_huft * FAR *tl; /* literal/length tree result */
inflate_huft * FAR *td; /* distance tree result */
z_streamp z; /* for memory allocation */
{
#ifdef BUILDFIXED
/* build fixed tables if not already */
if (!fixed_built)
{
int k; /* temporary variable */
uInt f = 0; /* number of hufts used in fixed_mem */
uIntf *c; /* length list for huft_build */
uIntf *v; /* work area for huft_build */
/* allocate memory */
if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
return Z_MEM_ERROR;
if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
{
ZFREE(z, c);
return Z_MEM_ERROR;
}
/* literal table */
for (k = 0; k < 144; k++)
c[k] = 8;
for (; k < 256; k++)
c[k] = 9;
for (; k < 280; k++)
c[k] = 7;
for (; k < 288; k++)
c[k] = 8;
fixed_bl = 9;
huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl,
fixed_mem, &f, v);
/* distance table */
for (k = 0; k < 30; k++)
c[k] = 5;
fixed_bd = 5;
huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd,
fixed_mem, &f, v);
/* done */
ZFREE(z, v);
ZFREE(z, c);
fixed_built = 1;
}
#endif
*bl = fixed_bl;
*bd = fixed_bd;
*tl = fixed_tl;
*td = fixed_td;
return Z_OK;
}

58
zlib/inftrees.h Normal file
View File

@ -0,0 +1,58 @@
/* inftrees.h -- header to use inftrees.c
* Copyright (C) 1995-2002 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* WARNING: this file should *not* be used by applications. It is
part of the implementation of the compression library and is
subject to change. Applications should only use zlib.h.
*/
/* Huffman code lookup table entry--this entry is four bytes for machines
that have 16-bit pointers (e.g. PC's in the small or medium model). */
typedef struct inflate_huft_s FAR inflate_huft;
struct inflate_huft_s {
union {
struct {
Byte Exop; /* number of extra bits or operation */
Byte Bits; /* number of bits in this code or subcode */
} what;
uInt pad; /* pad structure to a power of 2 (4 bytes for */
} word; /* 16-bit, 8 bytes for 32-bit int's) */
uInt base; /* literal, length base, distance base,
or table offset */
};
/* Maximum size of dynamic tree. The maximum found in a long but non-
exhaustive search was 1004 huft structures (850 for length/literals
and 154 for distances, the latter actually the result of an
exhaustive search). The actual maximum is not known, but the
value below is more than safe. */
#define MANY 1440
extern int inflate_trees_bits OF((
uIntf *, /* 19 code lengths */
uIntf *, /* bits tree desired/actual depth */
inflate_huft * FAR *, /* bits tree result */
inflate_huft *, /* space for trees */
z_streamp)); /* for messages */
extern int inflate_trees_dynamic OF((
uInt, /* number of literal/length codes */
uInt, /* number of distance codes */
uIntf *, /* that many (total) code lengths */
uIntf *, /* literal desired/actual bit depth */
uIntf *, /* distance desired/actual bit depth */
inflate_huft * FAR *, /* literal/length tree result */
inflate_huft * FAR *, /* distance tree result */
inflate_huft *, /* space for trees */
z_streamp)); /* for messages */
extern int inflate_trees_fixed OF((
uIntf *, /* literal desired/actual bit depth */
uIntf *, /* distance desired/actual bit depth */
inflate_huft * FAR *, /* literal/length tree result */
inflate_huft * FAR *, /* distance tree result */
z_streamp)); /* for memory allocation */

1214
zlib/trees.c Normal file

File diff suppressed because it is too large Load Diff

128
zlib/trees.h Normal file
View File

@ -0,0 +1,128 @@
/* header created automatically with -DGEN_TREES_H */
local const ct_data static_ltree[L_CODES+2] = {
{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}},
{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}},
{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}},
{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}},
{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}},
{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}},
{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}},
{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}},
{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}},
{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}},
{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}},
{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}},
{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}},
{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}},
{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}},
{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}},
{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}},
{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}},
{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}},
{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}},
{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}},
{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}},
{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}},
{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}},
{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}},
{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}},
{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}},
{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}},
{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}},
{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}},
{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}},
{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}},
{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}},
{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}},
{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}},
{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}},
{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}},
{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}},
{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}},
{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}},
{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}},
{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}},
{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}},
{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}},
{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}},
{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}},
{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}},
{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}},
{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}},
{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}},
{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}},
{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}},
{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}},
{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}},
{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}},
{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}},
{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}},
{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
};
local const ct_data static_dtree[D_CODES] = {
{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
};
const uch _dist_code[DIST_CODE_LEN] = {
0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
};
const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {
0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
};
local const int base_length[LENGTH_CODES] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
64, 80, 96, 112, 128, 160, 192, 224, 0
};
local const int base_dist[D_CODES] = {
0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
};

58
zlib/uncompr.c Normal file
View File

@ -0,0 +1,58 @@
/* uncompr.c -- decompress a memory buffer
* Copyright (C) 1995-2002 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* @(#) $Id$ */
#include "zlib.h"
/* ===========================================================================
Decompresses the source buffer into the destination buffer. sourceLen is
the byte length of the source buffer. Upon entry, destLen is the total
size of the destination buffer, which must be large enough to hold the
entire uncompressed data. (The size of the uncompressed data must have
been saved previously by the compressor and transmitted to the decompressor
by some mechanism outside the scope of this compression library.)
Upon exit, destLen is the actual size of the compressed buffer.
This function can be used to decompress a whole file at once if the
input file is mmap'ed.
uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
enough memory, Z_BUF_ERROR if there was not enough room in the output
buffer, or Z_DATA_ERROR if the input data was corrupted.
*/
int ZEXPORT uncompress (dest, destLen, source, sourceLen)
Bytef *dest;
uLongf *destLen;
const Bytef *source;
uLong sourceLen;
{
z_stream stream;
int err;
stream.next_in = (Bytef*)source;
stream.avail_in = (uInt)sourceLen;
/* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
stream.next_out = dest;
stream.avail_out = (uInt)*destLen;
if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
err = inflateInit(&stream);
if (err != Z_OK) return err;
err = inflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
inflateEnd(&stream);
return err == Z_OK ? Z_BUF_ERROR : err;
}
*destLen = stream.total_out;
err = inflateEnd(&stream);
return err;
}

279
zlib/zconf.h Normal file
View File

@ -0,0 +1,279 @@
/* zconf.h -- configuration of the zlib compression library
* Copyright (C) 1995-2002 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* @(#) $Id$ */
#ifndef _ZCONF_H
#define _ZCONF_H
/*
* If you *really* need a unique prefix for all types and library functions,
* compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
*/
#ifdef Z_PREFIX
# define deflateInit_ z_deflateInit_
# define deflate z_deflate
# define deflateEnd z_deflateEnd
# define inflateInit_ z_inflateInit_
# define inflate z_inflate
# define inflateEnd z_inflateEnd
# define deflateInit2_ z_deflateInit2_
# define deflateSetDictionary z_deflateSetDictionary
# define deflateCopy z_deflateCopy
# define deflateReset z_deflateReset
# define deflateParams z_deflateParams
# define inflateInit2_ z_inflateInit2_
# define inflateSetDictionary z_inflateSetDictionary
# define inflateSync z_inflateSync
# define inflateSyncPoint z_inflateSyncPoint
# define inflateReset z_inflateReset
# define compress z_compress
# define compress2 z_compress2
# define uncompress z_uncompress
# define adler32 z_adler32
# define crc32 z_crc32
# define get_crc_table z_get_crc_table
# define Byte z_Byte
# define uInt z_uInt
# define uLong z_uLong
# define Bytef z_Bytef
# define charf z_charf
# define intf z_intf
# define uIntf z_uIntf
# define uLongf z_uLongf
# define voidpf z_voidpf
# define voidp z_voidp
#endif
#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
# define WIN32
#endif
#if defined(__GNUC__) || defined(WIN32) || defined(__386__) || defined(i386)
# ifndef __32BIT__
# define __32BIT__
# endif
#endif
#if defined(__MSDOS__) && !defined(MSDOS)
# define MSDOS
#endif
/*
* Compile with -DMAXSEG_64K if the alloc function cannot allocate more
* than 64k bytes at a time (needed on systems with 16-bit int).
*/
#if defined(MSDOS) && !defined(__32BIT__)
# define MAXSEG_64K
#endif
#ifdef MSDOS
# define UNALIGNED_OK
#endif
#if (defined(MSDOS) || defined(_WINDOWS) || defined(WIN32)) && !defined(STDC)
# define STDC
#endif
#if defined(__STDC__) || defined(__cplusplus) || defined(__OS2__)
# ifndef STDC
# define STDC
# endif
#endif
#ifndef STDC
# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
# define const
# endif
#endif
/* Some Mac compilers merge all .h files incorrectly: */
#if defined(__MWERKS__) || defined(applec) ||defined(THINK_C) ||defined(__SC__)
# define NO_DUMMY_DECL
#endif
/* Old Borland C incorrectly complains about missing returns: */
#if defined(__BORLANDC__) && (__BORLANDC__ < 0x500)
# define NEED_DUMMY_RETURN
#endif
/* Maximum value for memLevel in deflateInit2 */
#ifndef MAX_MEM_LEVEL
# ifdef MAXSEG_64K
# define MAX_MEM_LEVEL 8
# else
# define MAX_MEM_LEVEL 9
# endif
#endif
/* Maximum value for windowBits in deflateInit2 and inflateInit2.
* WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
* created by gzip. (Files created by minigzip can still be extracted by
* gzip.)
*/
#ifndef MAX_WBITS
# define MAX_WBITS 15 /* 32K LZ77 window */
#endif
/* The memory requirements for deflate are (in bytes):
(1 << (windowBits+2)) + (1 << (memLevel+9))
that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
plus a few kilobytes for small objects. For example, if you want to reduce
the default memory requirements from 256K to 128K, compile with
make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
Of course this will generally degrade compression (there's no free lunch).
The memory requirements for inflate are (in bytes) 1 << windowBits
that is, 32K for windowBits=15 (default value) plus a few kilobytes
for small objects.
*/
/* Type declarations */
#ifndef OF /* function prototypes */
# ifdef STDC
# define OF(args) args
# else
# define OF(args) ()
# endif
#endif
/* The following definitions for FAR are needed only for MSDOS mixed
* model programming (small or medium model with some far allocations).
* This was tested only with MSC; for other MSDOS compilers you may have
* to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
* just define FAR to be empty.
*/
#if (defined(M_I86SM) || defined(M_I86MM)) && !defined(__32BIT__)
/* MSC small or medium model */
# define SMALL_MEDIUM
# ifdef _MSC_VER
# define FAR _far
# else
# define FAR far
# endif
#endif
#if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__))
# ifndef __32BIT__
# define SMALL_MEDIUM
# define FAR _far
# endif
#endif
/* Compile with -DZLIB_DLL for Windows DLL support */
#if defined(ZLIB_DLL)
# if defined(_WINDOWS) || defined(WINDOWS)
# ifdef FAR
# undef FAR
# endif
# include <windows.h>
# define ZEXPORT WINAPI
# ifdef WIN32
# define ZEXPORTVA WINAPIV
# else
# define ZEXPORTVA FAR _cdecl _export
# endif
# endif
# if defined (__BORLANDC__)
# if (__BORLANDC__ >= 0x0500) && defined (WIN32)
# include <windows.h>
# define ZEXPORT __declspec(dllexport) WINAPI
# define ZEXPORTRVA __declspec(dllexport) WINAPIV
# else
# if defined (_Windows) && defined (__DLL__)
# define ZEXPORT _export
# define ZEXPORTVA _export
# endif
# endif
# endif
#endif
#if defined (__BEOS__)
# if defined (ZLIB_DLL)
# define ZEXTERN extern __declspec(dllexport)
# else
# define ZEXTERN extern __declspec(dllimport)
# endif
#endif
#ifndef ZEXPORT
# define ZEXPORT
#endif
#ifndef ZEXPORTVA
# define ZEXPORTVA
#endif
#ifndef ZEXTERN
# define ZEXTERN extern
#endif
#ifndef FAR
# define FAR
#endif
#if !defined(MACOS) && !defined(TARGET_OS_MAC)
typedef unsigned char Byte; /* 8 bits */
#endif
typedef unsigned int uInt; /* 16 bits or more */
typedef unsigned long uLong; /* 32 bits or more */
#ifdef SMALL_MEDIUM
/* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
# define Bytef Byte FAR
#else
typedef Byte FAR Bytef;
#endif
typedef char FAR charf;
typedef int FAR intf;
typedef uInt FAR uIntf;
typedef uLong FAR uLongf;
#ifdef STDC
typedef void FAR *voidpf;
typedef void *voidp;
#else
typedef Byte FAR *voidpf;
typedef Byte *voidp;
#endif
#ifdef HAVE_UNISTD_H
# include <sys/types.h> /* for off_t */
# include <unistd.h> /* for SEEK_* and off_t */
# define z_off_t off_t
#endif
#ifndef SEEK_SET
# define SEEK_SET 0 /* Seek from beginning of file. */
# define SEEK_CUR 1 /* Seek from current position. */
# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
#endif
#ifndef z_off_t
# define z_off_t long
#endif
/* MVS linker does not support external names larger than 8 bytes */
#if defined(__MVS__)
# pragma map(deflateInit_,"DEIN")
# pragma map(deflateInit2_,"DEIN2")
# pragma map(deflateEnd,"DEEND")
# pragma map(inflateInit_,"ININ")
# pragma map(inflateInit2_,"ININ2")
# pragma map(inflateEnd,"INEND")
# pragma map(inflateSync,"INSY")
# pragma map(inflateSetDictionary,"INSEDI")
# pragma map(inflate_blocks,"INBL")
# pragma map(inflate_blocks_new,"INBLNE")
# pragma map(inflate_blocks_free,"INBLFR")
# pragma map(inflate_blocks_reset,"INBLRE")
# pragma map(inflate_codes_free,"INCOFR")
# pragma map(inflate_codes,"INCO")
# pragma map(inflate_fast,"INFA")
# pragma map(inflate_flush,"INFLU")
# pragma map(inflate_mask,"INMA")
# pragma map(inflate_set_dictionary,"INSEDI2")
# pragma map(inflate_copyright,"INCOPY")
# pragma map(inflate_trees_bits,"INTRBI")
# pragma map(inflate_trees_dynamic,"INTRDY")
# pragma map(inflate_trees_fixed,"INTRFI")
# pragma map(inflate_trees_free,"INTRFR")
#endif
#endif /* _ZCONF_H */

893
zlib/zlib.h Normal file
View File

@ -0,0 +1,893 @@
/* zlib.h -- interface of the 'zlib' general purpose compression library
version 1.1.4, March 11th, 2002
Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jean-loup Gailly Mark Adler
jloup@gzip.org madler@alumni.caltech.edu
The data format used by the zlib library is described by RFCs (Request for
Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
(zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
*/
#ifndef _ZLIB_H
#define _ZLIB_H
#include "zconf.h"
#ifdef __cplusplus
extern "C" {
#endif
#define ZLIB_VERSION "1.1.4"
/*
The 'zlib' compression library provides in-memory compression and
decompression functions, including integrity checks of the uncompressed
data. This version of the library supports only one compression method
(deflation) but other algorithms will be added later and will have the same
stream interface.
Compression can be done in a single step if the buffers are large
enough (for example if an input file is mmap'ed), or can be done by
repeated calls of the compression function. In the latter case, the
application must provide more input and/or consume the output
(providing more output space) before each call.
The library also supports reading and writing files in gzip (.gz) format
with an interface similar to that of stdio.
The library does not install any signal handler. The decoder checks
the consistency of the compressed data, so the library should never
crash even in case of corrupted input.
*/
typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
typedef void (*free_func) OF((voidpf opaque, voidpf address));
struct internal_state;
typedef struct z_stream_s {
Bytef *next_in; /* next input byte */
uInt avail_in; /* number of bytes available at next_in */
uLong total_in; /* total nb of input bytes read so far */
Bytef *next_out; /* next output byte should be put there */
uInt avail_out; /* remaining free space at next_out */
uLong total_out; /* total nb of bytes output so far */
char *msg; /* last error message, NULL if no error */
struct internal_state FAR *state; /* not visible by applications */
alloc_func zalloc; /* used to allocate the internal state */
free_func zfree; /* used to free the internal state */
voidpf opaque; /* private data object passed to zalloc and zfree */
int data_type; /* best guess about the data type: ascii or binary */
uLong adler; /* adler32 value of the uncompressed data */
uLong reserved; /* reserved for future use */
} z_stream;
typedef z_stream FAR *z_streamp;
/*
The application must update next_in and avail_in when avail_in has
dropped to zero. It must update next_out and avail_out when avail_out
has dropped to zero. The application must initialize zalloc, zfree and
opaque before calling the init function. All other fields are set by the
compression library and must not be updated by the application.
The opaque value provided by the application will be passed as the first
parameter for calls of zalloc and zfree. This can be useful for custom
memory management. The compression library attaches no meaning to the
opaque value.
zalloc must return Z_NULL if there is not enough memory for the object.
If zlib is used in a multi-threaded application, zalloc and zfree must be
thread safe.
On 16-bit systems, the functions zalloc and zfree must be able to allocate
exactly 65536 bytes, but will not be required to allocate more than this
if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
pointers returned by zalloc for objects of exactly 65536 bytes *must*
have their offset normalized to zero. The default allocation function
provided by this library ensures this (see zutil.c). To reduce memory
requirements and avoid any allocation of 64K objects, at the expense of
compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
The fields total_in and total_out can be used for statistics or
progress reports. After compression, total_in holds the total size of
the uncompressed data and may be saved for use in the decompressor
(particularly if the decompressor wants to decompress everything in
a single step).
*/
/* constants */
#define Z_NO_FLUSH 0
#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
#define Z_SYNC_FLUSH 2
#define Z_FULL_FLUSH 3
#define Z_FINISH 4
/* Allowed flush values; see deflate() below for details */
#define Z_OK 0
#define Z_STREAM_END 1
#define Z_NEED_DICT 2
#define Z_ERRNO (-1)
#define Z_STREAM_ERROR (-2)
#define Z_DATA_ERROR (-3)
#define Z_MEM_ERROR (-4)
#define Z_BUF_ERROR (-5)
#define Z_VERSION_ERROR (-6)
/* Return codes for the compression/decompression functions. Negative
* values are errors, positive values are used for special but normal events.
*/
#define Z_NO_COMPRESSION 0
#define Z_BEST_SPEED 1
#define Z_BEST_COMPRESSION 9
#define Z_DEFAULT_COMPRESSION (-1)
/* compression levels */
#define Z_FILTERED 1
#define Z_HUFFMAN_ONLY 2
#define Z_DEFAULT_STRATEGY 0
/* compression strategy; see deflateInit2() below for details */
#define Z_BINARY 0
#define Z_ASCII 1
#define Z_UNKNOWN 2
/* Possible values of the data_type field */
#define Z_DEFLATED 8
/* The deflate compression method (the only one supported in this version) */
#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
#define zlib_version zlibVersion()
/* for compatibility with versions < 1.0.2 */
/* basic functions */
ZEXTERN const char * ZEXPORT zlibVersion OF((void));
/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
If the first character differs, the library code actually used is
not compatible with the zlib.h header file used by the application.
This check is automatically made by deflateInit and inflateInit.
*/
/*
ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
Initializes the internal stream state for compression. The fields
zalloc, zfree and opaque must be initialized before by the caller.
If zalloc and zfree are set to Z_NULL, deflateInit updates them to
use default allocation functions.
The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
1 gives best speed, 9 gives best compression, 0 gives no compression at
all (the input data is simply copied a block at a time).
Z_DEFAULT_COMPRESSION requests a default compromise between speed and
compression (currently equivalent to level 6).
deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
enough memory, Z_STREAM_ERROR if level is not a valid compression level,
Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
with the version assumed by the caller (ZLIB_VERSION).
msg is set to null if there is no error message. deflateInit does not
perform any compression: this will be done by deflate().
*/
ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
/*
deflate compresses as much data as possible, and stops when the input
buffer becomes empty or the output buffer becomes full. It may introduce some
output latency (reading input without producing any output) except when
forced to flush.
The detailed semantics are as follows. deflate performs one or both of the
following actions:
- Compress more input starting at next_in and update next_in and avail_in
accordingly. If not all input can be processed (because there is not
enough room in the output buffer), next_in and avail_in are updated and
processing will resume at this point for the next call of deflate().
- Provide more output starting at next_out and update next_out and avail_out
accordingly. This action is forced if the parameter flush is non zero.
Forcing flush frequently degrades the compression ratio, so this parameter
should be set only when necessary (in interactive applications).
Some output may be provided even if flush is not set.
Before the call of deflate(), the application should ensure that at least
one of the actions is possible, by providing more input and/or consuming
more output, and updating avail_in or avail_out accordingly; avail_out
should never be zero before the call. The application can consume the
compressed output when it wants, for example when the output buffer is full
(avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
and with zero avail_out, it must be called again after making room in the
output buffer because there might be more output pending.
If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
flushed to the output buffer and the output is aligned on a byte boundary, so
that the decompressor can get all input data available so far. (In particular
avail_in is zero after the call if enough output space has been provided
before the call.) Flushing may degrade compression for some compression
algorithms and so it should be used only when necessary.
If flush is set to Z_FULL_FLUSH, all output is flushed as with
Z_SYNC_FLUSH, and the compression state is reset so that decompression can
restart from this point if previous compressed data has been damaged or if
random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
the compression.
If deflate returns with avail_out == 0, this function must be called again
with the same value of the flush parameter and more output space (updated
avail_out), until the flush is complete (deflate returns with non-zero
avail_out).
If the parameter flush is set to Z_FINISH, pending input is processed,
pending output is flushed and deflate returns with Z_STREAM_END if there
was enough output space; if deflate returns with Z_OK, this function must be
called again with Z_FINISH and more output space (updated avail_out) but no
more input data, until it returns with Z_STREAM_END or an error. After
deflate has returned Z_STREAM_END, the only possible operations on the
stream are deflateReset or deflateEnd.
Z_FINISH can be used immediately after deflateInit if all the compression
is to be done in a single step. In this case, avail_out must be at least
0.1% larger than avail_in plus 12 bytes. If deflate does not return
Z_STREAM_END, then it must be called again as described above.
deflate() sets strm->adler to the adler32 checksum of all input read
so far (that is, total_in bytes).
deflate() may update data_type if it can make a good guess about
the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
binary. This field is only for information purposes and does not affect
the compression algorithm in any manner.
deflate() returns Z_OK if some progress has been made (more input
processed or more output produced), Z_STREAM_END if all input has been
consumed and all output has been produced (only when flush is set to
Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
(for example avail_in or avail_out was zero).
*/
ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
/*
All dynamically allocated data structures for this stream are freed.
This function discards any unprocessed input and does not flush any
pending output.
deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
stream state was inconsistent, Z_DATA_ERROR if the stream was freed
prematurely (some input or output was discarded). In the error case,
msg may be set but then points to a static string (which must not be
deallocated).
*/
/*
ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
Initializes the internal stream state for decompression. The fields
next_in, avail_in, zalloc, zfree and opaque must be initialized before by
the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
value depends on the compression method), inflateInit determines the
compression method from the zlib header and allocates all data structures
accordingly; otherwise the allocation will be deferred to the first call of
inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
use default allocation functions.
inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
version assumed by the caller. msg is set to null if there is no error
message. inflateInit does not perform any decompression apart from reading
the zlib header if present: this will be done by inflate(). (So next_in and
avail_in may be modified, but next_out and avail_out are unchanged.)
*/
ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
/*
inflate decompresses as much data as possible, and stops when the input
buffer becomes empty or the output buffer becomes full. It may some
introduce some output latency (reading input without producing any output)
except when forced to flush.
The detailed semantics are as follows. inflate performs one or both of the
following actions:
- Decompress more input starting at next_in and update next_in and avail_in
accordingly. If not all input can be processed (because there is not
enough room in the output buffer), next_in is updated and processing
will resume at this point for the next call of inflate().
- Provide more output starting at next_out and update next_out and avail_out
accordingly. inflate() provides as much output as possible, until there
is no more input data or no more space in the output buffer (see below
about the flush parameter).
Before the call of inflate(), the application should ensure that at least
one of the actions is possible, by providing more input and/or consuming
more output, and updating the next_* and avail_* values accordingly.
The application can consume the uncompressed output when it wants, for
example when the output buffer is full (avail_out == 0), or after each
call of inflate(). If inflate returns Z_OK and with zero avail_out, it
must be called again after making room in the output buffer because there
might be more output pending.
If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
output as possible to the output buffer. The flushing behavior of inflate is
not specified for values of the flush parameter other than Z_SYNC_FLUSH
and Z_FINISH, but the current implementation actually flushes as much output
as possible anyway.
inflate() should normally be called until it returns Z_STREAM_END or an
error. However if all decompression is to be performed in a single step
(a single call of inflate), the parameter flush should be set to
Z_FINISH. In this case all pending input is processed and all pending
output is flushed; avail_out must be large enough to hold all the
uncompressed data. (The size of the uncompressed data may have been saved
by the compressor for this purpose.) The next operation on this stream must
be inflateEnd to deallocate the decompression state. The use of Z_FINISH
is never required, but can be used to inform inflate that a faster routine
may be used for the single inflate() call.
If a preset dictionary is needed at this point (see inflateSetDictionary
below), inflate sets strm-adler to the adler32 checksum of the
dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
it sets strm->adler to the adler32 checksum of all output produced
so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
an error code as described below. At the end of the stream, inflate()
checks that its computed adler32 checksum is equal to that saved by the
compressor and returns Z_STREAM_END only if the checksum is correct.
inflate() returns Z_OK if some progress has been made (more input processed
or more output produced), Z_STREAM_END if the end of the compressed data has
been reached and all uncompressed output has been produced, Z_NEED_DICT if a
preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
corrupted (input stream not conforming to the zlib format or incorrect
adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
(for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
enough memory, Z_BUF_ERROR if no progress is possible or if there was not
enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
case, the application may then call inflateSync to look for a good
compression block.
*/
ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
/*
All dynamically allocated data structures for this stream are freed.
This function discards any unprocessed input and does not flush any
pending output.
inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
was inconsistent. In the error case, msg may be set but then points to a
static string (which must not be deallocated).
*/
/* Advanced functions */
/*
The following functions are needed only in some special applications.
*/
/*
ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
int level,
int method,
int windowBits,
int memLevel,
int strategy));
This is another version of deflateInit with more compression options. The
fields next_in, zalloc, zfree and opaque must be initialized before by
the caller.
The method parameter is the compression method. It must be Z_DEFLATED in
this version of the library.
The windowBits parameter is the base two logarithm of the window size
(the size of the history buffer). It should be in the range 8..15 for this
version of the library. Larger values of this parameter result in better
compression at the expense of memory usage. The default value is 15 if
deflateInit is used instead.
The memLevel parameter specifies how much memory should be allocated
for the internal compression state. memLevel=1 uses minimum memory but
is slow and reduces compression ratio; memLevel=9 uses maximum memory
for optimal speed. The default value is 8. See zconf.h for total memory
usage as a function of windowBits and memLevel.
The strategy parameter is used to tune the compression algorithm. Use the
value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
string match). Filtered data consists mostly of small values with a
somewhat random distribution. In this case, the compression algorithm is
tuned to compress them better. The effect of Z_FILTERED is to force more
Huffman coding and less string matching; it is somewhat intermediate
between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
the compression ratio but not the correctness of the compressed output even
if it is not set appropriately.
deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
method). msg is set to null if there is no error message. deflateInit2 does
not perform any compression: this will be done by deflate().
*/
ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
const Bytef *dictionary,
uInt dictLength));
/*
Initializes the compression dictionary from the given byte sequence
without producing any compressed output. This function must be called
immediately after deflateInit, deflateInit2 or deflateReset, before any
call of deflate. The compressor and decompressor must use exactly the same
dictionary (see inflateSetDictionary).
The dictionary should consist of strings (byte sequences) that are likely
to be encountered later in the data to be compressed, with the most commonly
used strings preferably put towards the end of the dictionary. Using a
dictionary is most useful when the data to be compressed is short and can be
predicted with good accuracy; the data can then be compressed better than
with the default empty dictionary.
Depending on the size of the compression data structures selected by
deflateInit or deflateInit2, a part of the dictionary may in effect be
discarded, for example if the dictionary is larger than the window size in
deflate or deflate2. Thus the strings most likely to be useful should be
put at the end of the dictionary, not at the front.
Upon return of this function, strm->adler is set to the Adler32 value
of the dictionary; the decompressor may later use this value to determine
which dictionary has been used by the compressor. (The Adler32 value
applies to the whole dictionary even if only a subset of the dictionary is
actually used by the compressor.)
deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
parameter is invalid (such as NULL dictionary) or the stream state is
inconsistent (for example if deflate has already been called for this stream
or if the compression method is bsort). deflateSetDictionary does not
perform any compression: this will be done by deflate().
*/
ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
z_streamp source));
/*
Sets the destination stream as a complete copy of the source stream.
This function can be useful when several compression strategies will be
tried, for example when there are several ways of pre-processing the input
data with a filter. The streams that will be discarded should then be freed
by calling deflateEnd. Note that deflateCopy duplicates the internal
compression state which can be quite large, so this strategy is slow and
can consume lots of memory.
deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
(such as zalloc being NULL). msg is left unchanged in both source and
destination.
*/
ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
/*
This function is equivalent to deflateEnd followed by deflateInit,
but does not free and reallocate all the internal compression state.
The stream will keep the same compression level and any other attributes
that may have been set by deflateInit2.
deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
stream state was inconsistent (such as zalloc or state being NULL).
*/
ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
int level,
int strategy));
/*
Dynamically update the compression level and compression strategy. The
interpretation of level and strategy is as in deflateInit2. This can be
used to switch between compression and straight copy of the input data, or
to switch to a different kind of input data requiring a different
strategy. If the compression level is changed, the input available so far
is compressed with the old level (and may be flushed); the new level will
take effect only at the next call of deflate().
Before the call of deflateParams, the stream state must be set as for
a call of deflate(), since the currently available input may have to
be compressed and flushed. In particular, strm->avail_out must be non-zero.
deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
if strm->avail_out was zero.
*/
/*
ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
int windowBits));
This is another version of inflateInit with an extra parameter. The
fields next_in, avail_in, zalloc, zfree and opaque must be initialized
before by the caller.
The windowBits parameter is the base two logarithm of the maximum window
size (the size of the history buffer). It should be in the range 8..15 for
this version of the library. The default value is 15 if inflateInit is used
instead. If a compressed stream with a larger window size is given as
input, inflate() will return with the error code Z_DATA_ERROR instead of
trying to allocate a larger window.
inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
memLevel). msg is set to null if there is no error message. inflateInit2
does not perform any decompression apart from reading the zlib header if
present: this will be done by inflate(). (So next_in and avail_in may be
modified, but next_out and avail_out are unchanged.)
*/
ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
const Bytef *dictionary,
uInt dictLength));
/*
Initializes the decompression dictionary from the given uncompressed byte
sequence. This function must be called immediately after a call of inflate
if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
can be determined from the Adler32 value returned by this call of
inflate. The compressor and decompressor must use exactly the same
dictionary (see deflateSetDictionary).
inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
parameter is invalid (such as NULL dictionary) or the stream state is
inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
expected one (incorrect Adler32 value). inflateSetDictionary does not
perform any decompression: this will be done by subsequent calls of
inflate().
*/
ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
/*
Skips invalid compressed data until a full flush point (see above the
description of deflate with Z_FULL_FLUSH) can be found, or until all
available input is skipped. No output is provided.
inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
if no more input was provided, Z_DATA_ERROR if no flush point has been found,
or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
case, the application may save the current current value of total_in which
indicates where valid compressed data was found. In the error case, the
application may repeatedly call inflateSync, providing more input each time,
until success or end of the input data.
*/
ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
/*
This function is equivalent to inflateEnd followed by inflateInit,
but does not free and reallocate all the internal decompression state.
The stream will keep attributes that may have been set by inflateInit2.
inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
stream state was inconsistent (such as zalloc or state being NULL).
*/
/* utility functions */
/*
The following utility functions are implemented on top of the
basic stream-oriented functions. To simplify the interface, some
default options are assumed (compression level and memory usage,
standard memory allocation functions). The source code of these
utility functions can easily be modified if you need special options.
*/
ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
const Bytef *source, uLong sourceLen));
/*
Compresses the source buffer into the destination buffer. sourceLen is
the byte length of the source buffer. Upon entry, destLen is the total
size of the destination buffer, which must be at least 0.1% larger than
sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
compressed buffer.
This function can be used to compress a whole file at once if the
input file is mmap'ed.
compress returns Z_OK if success, Z_MEM_ERROR if there was not
enough memory, Z_BUF_ERROR if there was not enough room in the output
buffer.
*/
ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
const Bytef *source, uLong sourceLen,
int level));
/*
Compresses the source buffer into the destination buffer. The level
parameter has the same meaning as in deflateInit. sourceLen is the byte
length of the source buffer. Upon entry, destLen is the total size of the
destination buffer, which must be at least 0.1% larger than sourceLen plus
12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_BUF_ERROR if there was not enough room in the output buffer,
Z_STREAM_ERROR if the level parameter is invalid.
*/
ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
const Bytef *source, uLong sourceLen));
/*
Decompresses the source buffer into the destination buffer. sourceLen is
the byte length of the source buffer. Upon entry, destLen is the total
size of the destination buffer, which must be large enough to hold the
entire uncompressed data. (The size of the uncompressed data must have
been saved previously by the compressor and transmitted to the decompressor
by some mechanism outside the scope of this compression library.)
Upon exit, destLen is the actual size of the compressed buffer.
This function can be used to decompress a whole file at once if the
input file is mmap'ed.
uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
enough memory, Z_BUF_ERROR if there was not enough room in the output
buffer, or Z_DATA_ERROR if the input data was corrupted.
*/
typedef voidp gzFile;
ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
/*
Opens a gzip (.gz) file for reading or writing. The mode parameter
is as in fopen ("rb" or "wb") but can also include a compression level
("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
Huffman only compression as in "wb1h". (See the description
of deflateInit2 for more information about the strategy parameter.)
gzopen can be used to read a file which is not in gzip format; in this
case gzread will directly read from the file without decompression.
gzopen returns NULL if the file could not be opened or if there was
insufficient memory to allocate the (de)compression state; errno
can be checked to distinguish the two cases (if errno is zero, the
zlib error is Z_MEM_ERROR). */
ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
/*
gzdopen() associates a gzFile with the file descriptor fd. File
descriptors are obtained from calls like open, dup, creat, pipe or
fileno (in the file has been previously opened with fopen).
The mode parameter is as in gzopen.
The next call of gzclose on the returned gzFile will also close the
file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
gzdopen returns NULL if there was insufficient memory to allocate
the (de)compression state.
*/
ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
/*
Dynamically update the compression level or strategy. See the description
of deflateInit2 for the meaning of these parameters.
gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
opened for writing.
*/
ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
/*
Reads the given number of uncompressed bytes from the compressed file.
If the input file was not in gzip format, gzread copies the given number
of bytes into the buffer.
gzread returns the number of uncompressed bytes actually read (0 for
end of file, -1 for error). */
ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
const voidp buf, unsigned len));
/*
Writes the given number of uncompressed bytes into the compressed file.
gzwrite returns the number of uncompressed bytes actually written
(0 in case of error).
*/
ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...));
/*
Converts, formats, and writes the args to the compressed file under
control of the format string, as in fprintf. gzprintf returns the number of
uncompressed bytes actually written (0 in case of error).
*/
ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
/*
Writes the given null-terminated string to the compressed file, excluding
the terminating null character.
gzputs returns the number of characters written, or -1 in case of error.
*/
ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
/*
Reads bytes from the compressed file until len-1 characters are read, or
a newline character is read and transferred to buf, or an end-of-file
condition is encountered. The string is then terminated with a null
character.
gzgets returns buf, or Z_NULL in case of error.
*/
ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
/*
Writes c, converted to an unsigned char, into the compressed file.
gzputc returns the value that was written, or -1 in case of error.
*/
ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
/*
Reads one byte from the compressed file. gzgetc returns this byte
or -1 in case of end of file or error.
*/
ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
/*
Flushes all pending output into the compressed file. The parameter
flush is as in the deflate() function. The return value is the zlib
error number (see function gzerror below). gzflush returns Z_OK if
the flush parameter is Z_FINISH and all output could be flushed.
gzflush should be called only when strictly necessary because it can
degrade compression.
*/
ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
z_off_t offset, int whence));
/*
Sets the starting position for the next gzread or gzwrite on the
given compressed file. The offset represents a number of bytes in the
uncompressed data stream. The whence parameter is defined as in lseek(2);
the value SEEK_END is not supported.
If the file is opened for reading, this function is emulated but can be
extremely slow. If the file is opened for writing, only forward seeks are
supported; gzseek then compresses a sequence of zeroes up to the new
starting position.
gzseek returns the resulting offset location as measured in bytes from
the beginning of the uncompressed stream, or -1 in case of error, in
particular if the file is opened for writing and the new starting position
would be before the current position.
*/
ZEXTERN int ZEXPORT gzrewind OF((gzFile file));
/*
Rewinds the given file. This function is supported only for reading.
gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
*/
ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file));
/*
Returns the starting position for the next gzread or gzwrite on the
given compressed file. This position represents a number of bytes in the
uncompressed data stream.
gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
*/
ZEXTERN int ZEXPORT gzeof OF((gzFile file));
/*
Returns 1 when EOF has previously been detected reading the given
input stream, otherwise zero.
*/
ZEXTERN int ZEXPORT gzclose OF((gzFile file));
/*
Flushes all pending output if necessary, closes the compressed file
and deallocates all the (de)compression state. The return value is the zlib
error number (see function gzerror below).
*/
ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
/*
Returns the error message for the last error which occurred on the
given compressed file. errnum is set to zlib error number. If an
error occurred in the file system and not in the compression library,
errnum is set to Z_ERRNO and the application may consult errno
to get the exact error code.
*/
/* checksum functions */
/*
These functions are not related to compression but are exported
anyway because they might be useful in applications using the
compression library.
*/
ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
/*
Update a running Adler-32 checksum with the bytes buf[0..len-1] and
return the updated checksum. If buf is NULL, this function returns
the required initial value for the checksum.
An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
much faster. Usage example:
uLong adler = adler32(0L, Z_NULL, 0);
while (read_buffer(buffer, length) != EOF) {
adler = adler32(adler, buffer, length);
}
if (adler != original_adler) error();
*/
ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
/*
Update a running crc with the bytes buf[0..len-1] and return the updated
crc. If buf is NULL, this function returns the required initial value
for the crc. Pre- and post-conditioning (one's complement) is performed
within this function so it shouldn't be done by the application.
Usage example:
uLong crc = crc32(0L, Z_NULL, 0);
while (read_buffer(buffer, length) != EOF) {
crc = crc32(crc, buffer, length);
}
if (crc != original_crc) error();
*/
/* various hacks, don't look :) */
/* deflateInit and inflateInit are macros to allow checking the zlib version
* and the compiler's view of z_stream:
*/
ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
const char *version, int stream_size));
ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
const char *version, int stream_size));
ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
int windowBits, int memLevel,
int strategy, const char *version,
int stream_size));
ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
const char *version, int stream_size));
#define deflateInit(strm, level) \
deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
#define inflateInit(strm) \
inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
(strategy), ZLIB_VERSION, sizeof(z_stream))
#define inflateInit2(strm, windowBits) \
inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
#if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)
struct internal_state {int dummy;}; /* hack for buggy compilers */
#endif
ZEXTERN const char * ZEXPORT zError OF((int err));
ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp z));
ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void));
#ifdef __cplusplus
}
#endif
#endif /* _ZLIB_H */

225
zlib/zutil.c Normal file
View File

@ -0,0 +1,225 @@
/* zutil.c -- target dependent utility functions for the compression library
* Copyright (C) 1995-2002 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* @(#) $Id$ */
#include "zutil.h"
struct internal_state {int dummy;}; /* for buggy compilers */
#ifndef STDC
extern void exit OF((int));
#endif
const char *z_errmsg[10] = {
"need dictionary", /* Z_NEED_DICT 2 */
"stream end", /* Z_STREAM_END 1 */
"", /* Z_OK 0 */
"file error", /* Z_ERRNO (-1) */
"stream error", /* Z_STREAM_ERROR (-2) */
"data error", /* Z_DATA_ERROR (-3) */
"insufficient memory", /* Z_MEM_ERROR (-4) */
"buffer error", /* Z_BUF_ERROR (-5) */
"incompatible version",/* Z_VERSION_ERROR (-6) */
""};
const char * ZEXPORT zlibVersion()
{
return ZLIB_VERSION;
}
#ifdef DEBUG
# ifndef verbose
# define verbose 0
# endif
int z_verbose = verbose;
void z_error (m)
char *m;
{
fprintf(stderr, "%s\n", m);
exit(1);
}
#endif
/* exported to allow conversion of error code to string for compress() and
* uncompress()
*/
const char * ZEXPORT zError(err)
int err;
{
return ERR_MSG(err);
}
#ifndef HAVE_MEMCPY
void zmemcpy(dest, source, len)
Bytef* dest;
const Bytef* source;
uInt len;
{
if (len == 0) return;
do {
*dest++ = *source++; /* ??? to be unrolled */
} while (--len != 0);
}
int zmemcmp(s1, s2, len)
const Bytef* s1;
const Bytef* s2;
uInt len;
{
uInt j;
for (j = 0; j < len; j++) {
if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
}
return 0;
}
void zmemzero(dest, len)
Bytef* dest;
uInt len;
{
if (len == 0) return;
do {
*dest++ = 0; /* ??? to be unrolled */
} while (--len != 0);
}
#endif
#ifdef __TURBOC__
#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
/* Small and medium model in Turbo C are for now limited to near allocation
* with reduced MAX_WBITS and MAX_MEM_LEVEL
*/
# define MY_ZCALLOC
/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
* and farmalloc(64K) returns a pointer with an offset of 8, so we
* must fix the pointer. Warning: the pointer must be put back to its
* original form in order to free it, use zcfree().
*/
#define MAX_PTR 10
/* 10*64K = 640K */
local int next_ptr = 0;
typedef struct ptr_table_s {
voidpf org_ptr;
voidpf new_ptr;
} ptr_table;
local ptr_table table[MAX_PTR];
/* This table is used to remember the original form of pointers
* to large buffers (64K). Such pointers are normalized with a zero offset.
* Since MSDOS is not a preemptive multitasking OS, this table is not
* protected from concurrent access. This hack doesn't work anyway on
* a protected system like OS/2. Use Microsoft C instead.
*/
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
{
voidpf buf = opaque; /* just to make some compilers happy */
ulg bsize = (ulg)items*size;
/* If we allocate less than 65520 bytes, we assume that farmalloc
* will return a usable pointer which doesn't have to be normalized.
*/
if (bsize < 65520L) {
buf = farmalloc(bsize);
if (*(ush*)&buf != 0) return buf;
} else {
buf = farmalloc(bsize + 16L);
}
if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
table[next_ptr].org_ptr = buf;
/* Normalize the pointer to seg:0 */
*((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
*(ush*)&buf = 0;
table[next_ptr++].new_ptr = buf;
return buf;
}
void zcfree (voidpf opaque, voidpf ptr)
{
int n;
if (*(ush*)&ptr != 0) { /* object < 64K */
farfree(ptr);
return;
}
/* Find the original pointer */
for (n = 0; n < next_ptr; n++) {
if (ptr != table[n].new_ptr) continue;
farfree(table[n].org_ptr);
while (++n < next_ptr) {
table[n-1] = table[n];
}
next_ptr--;
return;
}
ptr = opaque; /* just to make some compilers happy */
Assert(0, "zcfree: ptr not found");
}
#endif
#endif /* __TURBOC__ */
#if defined(M_I86) && !defined(__32BIT__)
/* Microsoft C in 16-bit mode */
# define MY_ZCALLOC
#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
# define _halloc halloc
# define _hfree hfree
#endif
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
{
if (opaque) opaque = 0; /* to make compiler happy */
return _halloc((long)items, size);
}
void zcfree (voidpf opaque, voidpf ptr)
{
if (opaque) opaque = 0; /* to make compiler happy */
_hfree(ptr);
}
#endif /* MSC */
#ifndef MY_ZCALLOC /* Any system without a special alloc function */
#ifndef STDC
extern voidp calloc OF((uInt items, uInt size));
extern void free OF((voidpf ptr));
#endif
voidpf zcalloc (opaque, items, size)
voidpf opaque;
unsigned items;
unsigned size;
{
if (opaque) items += size - size; /* make compiler happy */
return (voidpf)calloc(items, size);
}
void zcfree (opaque, ptr)
voidpf opaque;
voidpf ptr;
{
free(ptr);
if (opaque) return; /* make compiler happy */
}
#endif /* MY_ZCALLOC */

220
zlib/zutil.h Normal file
View File

@ -0,0 +1,220 @@
/* zutil.h -- internal interface and configuration of the compression library
* Copyright (C) 1995-2002 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* WARNING: this file should *not* be used by applications. It is
part of the implementation of the compression library and is
subject to change. Applications should only use zlib.h.
*/
/* @(#) $Id$ */
#ifndef _Z_UTIL_H
#define _Z_UTIL_H
#include "zlib.h"
#ifdef STDC
# include <stddef.h>
# include <string.h>
# include <stdlib.h>
#endif
#ifdef NO_ERRNO_H
extern int errno;
#else
# include <errno.h>
#endif
#ifndef local
# define local static
#endif
/* compile with -Dlocal if your debugger can't find static symbols */
typedef unsigned char uch;
typedef uch FAR uchf;
typedef unsigned short ush;
typedef ush FAR ushf;
typedef unsigned long ulg;
extern const char *z_errmsg[10]; /* indexed by 2-zlib_error */
/* (size given to avoid silly warnings with Visual C++) */
#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
#define ERR_RETURN(strm,err) \
return (strm->msg = (char*)ERR_MSG(err), (err))
/* To be used only when the state is known to be valid */
/* common constants */
#ifndef DEF_WBITS
# define DEF_WBITS MAX_WBITS
#endif
/* default windowBits for decompression. MAX_WBITS is for compression only */
#if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
#else
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
#endif
/* default memLevel */
#define STORED_BLOCK 0
#define STATIC_TREES 1
#define DYN_TREES 2
/* The three kinds of block type */
#define MIN_MATCH 3
#define MAX_MATCH 258
/* The minimum and maximum match lengths */
#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
/* target dependencies */
#ifdef MSDOS
# define OS_CODE 0x00
# if defined(__TURBOC__) || defined(__BORLANDC__)
# if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
/* Allow compilation with ANSI keywords only enabled */
void _Cdecl farfree( void *block );
void *_Cdecl farmalloc( unsigned long nbytes );
# else
# include <alloc.h>
# endif
# else /* MSC or DJGPP */
# include <malloc.h>
# endif
#endif
#ifdef OS2
# define OS_CODE 0x06
#endif
#ifdef WIN32 /* Window 95 & Windows NT */
# define OS_CODE 0x0b
#endif
#if defined(VAXC) || defined(VMS)
# define OS_CODE 0x02
# define F_OPEN(name, mode) \
fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
#endif
#ifdef AMIGA
# define OS_CODE 0x01
#endif
#if defined(ATARI) || defined(atarist)
# define OS_CODE 0x05
#endif
#if defined(MACOS) || defined(TARGET_OS_MAC)
# define OS_CODE 0x07
# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
# include <unix.h> /* for fdopen */
# else
# ifndef fdopen
# define fdopen(fd,mode) NULL /* No fdopen() */
# endif
# endif
#endif
#ifdef __50SERIES /* Prime/PRIMOS */
# define OS_CODE 0x0F
#endif
#ifdef TOPS20
# define OS_CODE 0x0a
#endif
#if defined(_BEOS_) || defined(RISCOS)
# define fdopen(fd,mode) NULL /* No fdopen() */
#endif
#if (defined(_MSC_VER) && (_MSC_VER > 600))
# define fdopen(fd,type) _fdopen(fd,type)
#endif
/* Common defaults */
#ifndef OS_CODE
# define OS_CODE 0x03 /* assume Unix */
#endif
#ifndef F_OPEN
# define F_OPEN(name, mode) fopen((name), (mode))
#endif
/* functions */
#ifdef HAVE_STRERROR
extern char *strerror OF((int));
# define zstrerror(errnum) strerror(errnum)
#else
# define zstrerror(errnum) ""
#endif
#if defined(pyr)
# define NO_MEMCPY
#endif
#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
/* Use our own functions for small and medium model with MSC <= 5.0.
* You may have to use the same strategy for Borland C (untested).
* The __SC__ check is for Symantec.
*/
# define NO_MEMCPY
#endif
#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
# define HAVE_MEMCPY
#endif
#ifdef HAVE_MEMCPY
# ifdef SMALL_MEDIUM /* MSDOS small or medium model */
# define zmemcpy _fmemcpy
# define zmemcmp _fmemcmp
# define zmemzero(dest, len) _fmemset(dest, 0, len)
# else
# define zmemcpy memcpy
# define zmemcmp memcmp
# define zmemzero(dest, len) memset(dest, 0, len)
# endif
#else
extern void zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
extern int zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
extern void zmemzero OF((Bytef* dest, uInt len));
#endif
/* Diagnostic functions */
#ifdef DEBUG
# include <stdio.h>
extern int z_verbose;
extern void z_error OF((char *m));
# define Assert(cond,msg) {if(!(cond)) z_error(msg);}
# define Trace(x) {if (z_verbose>=0) fprintf x ;}
# define Tracev(x) {if (z_verbose>0) fprintf x ;}
# define Tracevv(x) {if (z_verbose>1) fprintf x ;}
# define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
# define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
#else
# define Assert(cond,msg)
# define Trace(x)
# define Tracev(x)
# define Tracevv(x)
# define Tracec(c,x)
# define Tracecv(c,x)
#endif
typedef uLong (ZEXPORT *check_func) OF((uLong check, const Bytef *buf,
uInt len));
voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
void zcfree OF((voidpf opaque, voidpf ptr));
#define ZALLOC(strm, items, size) \
(*((strm)->zalloc))((strm)->opaque, (items), (size))
#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
#endif /* _Z_UTIL_H */