1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.11: http://docutils.sourceforge.net/" />
<title></title>
<meta name="author" content="Arvid Norberg, arvid@libtorrent.org" />
<meta name=viewport content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="rst.css" />
<style type="text/css">
/* Hides from IE-mac \*/
* html pre { height: 1%; }
/* End hide from IE-mac */
</style>
</head>
<body>
<div class="document">
<div id="container">
<a href="index.html"><table id="header">
<tr><td id="orange"></td>
<td id="logo">libtorrent</td></tr>
</table></a>
<div id="main">
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>Arvid Norberg, <a class="last reference external" href="mailto:arvid@libtorrent.org">arvid@libtorrent.org</a></td></tr>
<tr><th class="docinfo-name">Version:</th>
<td>1.1.11</td></tr>
</tbody>
</table>
<p><a class="reference external" href="reference.html">home</a></p>
<div class="section" id="utility">
<h1>Utility</h1>
<div class="contents topic" id="table-of-contents">
<p class="topic-title first">Table of contents</p>
<ul class="simple">
<li><a class="reference internal" href="#utility" id="id46">Utility</a><ul>
<li><a class="reference internal" href="#bitfield" id="id47">bitfield</a></li>
<li><a class="reference internal" href="#hasher" id="id48">hasher</a></li>
<li><a class="reference internal" href="#sha1-hash" id="id49">sha1_hash</a></li>
<li><a class="reference internal" href="#hash-value" id="id50">hash_value()</a></li>
<li><a class="reference internal" href="#id40" id="id51">operator<<()</a></li>
<li><a class="reference internal" href="#id43" id="id52">operator>>()</a></li>
</ul>
</li>
</ul>
</div>
<a name="bitfield"></a><div class="section" id="bitfield">
<h2>bitfield</h2>
<p>Declared in "<a class="reference external" href="../include/libtorrent/bitfield.hpp">libtorrent/bitfield.hpp</a>"</p>
<p>The <a class="reference external" href="reference-Utility.html#bitfield">bitfield</a> type stores any number of bits as a <a class="reference external" href="reference-Utility.html#bitfield">bitfield</a>
in a heap allocated array.</p>
<pre class="literal-block">
struct bitfield
{
<strong>bitfield</strong> (int bits);
<strong>bitfield</strong> (bitfield const& rhs);
<strong>bitfield</strong> (bitfield&& rhs);
<strong>bitfield</strong> (int bits, bool val);
<strong>bitfield</strong> ();
<strong>bitfield</strong> (char const* b, int bits);
void <strong>assign</strong> (char const* b, int bits);
bool <strong>operator[]</strong> (int index) const;
bool <strong>get_bit</strong> (int index) const;
void <strong>clear_bit</strong> (int index);
void <strong>set_bit</strong> (int index);
bool <strong>all_set</strong> () const;
bool <strong>none_set</strong> () const;
int <strong>size</strong> () const;
int <strong>num_words</strong> () const;
bool <strong>empty</strong> () const;
char const* <strong>data</strong> () const;
bitfield& <strong>operator=</strong> (bitfield const& rhs);
int <strong>count</strong> () const;
};
</pre>
<a name="bitfield()"></a><div class="section" id="id5">
<h3>bitfield()</h3>
<pre class="literal-block">
<strong>bitfield</strong> (int bits);
<strong>bitfield</strong> (bitfield const& rhs);
<strong>bitfield</strong> (bitfield&& rhs);
<strong>bitfield</strong> (int bits, bool val);
<strong>bitfield</strong> ();
<strong>bitfield</strong> (char const* b, int bits);
</pre>
<p>constructs a new <a class="reference external" href="reference-Utility.html#bitfield">bitfield</a>. The default constructor creates an empty
<a class="reference external" href="reference-Utility.html#bitfield">bitfield</a>. <tt class="docutils literal">bits</tt> is the size of the <a class="reference external" href="reference-Utility.html#bitfield">bitfield</a> (specified in bits).
<tt class="docutils literal">val</tt> is the value to initialize the bits to. If not specified
all bits are initialized to 0.</p>
<p>The constructor taking a pointer <tt class="docutils literal">b</tt> and <tt class="docutils literal">bits</tt> copies a <a class="reference external" href="reference-Utility.html#bitfield">bitfield</a>
from the specified buffer, and <tt class="docutils literal">bits</tt> number of bits (rounded up to
the nearest byte boundary).</p>
<a name="assign()"></a></div>
<div class="section" id="assign">
<h3>assign()</h3>
<pre class="literal-block">
void <strong>assign</strong> (char const* b, int bits);
</pre>
<p>copy <a class="reference external" href="reference-Utility.html#bitfield">bitfield</a> from buffer <tt class="docutils literal">b</tt> of <tt class="docutils literal">bits</tt> number of bits, rounded up to
the nearest byte boundary.</p>
<a name="operator[]()"></a></div>
<div class="section" id="operator">
<h3>operator[]()</h3>
<pre class="literal-block">
bool <strong>operator[]</strong> (int index) const;
</pre>
<p>query bit at <tt class="docutils literal">index</tt>. Returns true if bit is 1, otherwise false.</p>
<a name="set_bit()"></a>
<a name="clear_bit()"></a></div>
<div class="section" id="set-bit-clear-bit">
<h3>set_bit() clear_bit()</h3>
<pre class="literal-block">
void <strong>clear_bit</strong> (int index);
void <strong>set_bit</strong> (int index);
</pre>
<p>set bit at <tt class="docutils literal">index</tt> to 0 (clear_bit) or 1 (set_bit).</p>
<a name="all_set()"></a></div>
<div class="section" id="all-set">
<h3>all_set()</h3>
<pre class="literal-block">
bool <strong>all_set</strong> () const;
</pre>
<p>returns true if all bits in the <a class="reference external" href="reference-Utility.html#bitfield">bitfield</a> are set</p>
<a name="size()"></a></div>
<div class="section" id="size">
<h3>size()</h3>
<pre class="literal-block">
int <strong>size</strong> () const;
</pre>
<p>returns the size of the <a class="reference external" href="reference-Utility.html#bitfield">bitfield</a> in bits.</p>
<a name="empty()"></a></div>
<div class="section" id="empty">
<h3>empty()</h3>
<pre class="literal-block">
bool <strong>empty</strong> () const;
</pre>
<p>returns true if the <a class="reference external" href="reference-Utility.html#bitfield">bitfield</a> has zero size.</p>
<a name="data()"></a></div>
<div class="section" id="data">
<h3>data()</h3>
<pre class="literal-block">
char const* <strong>data</strong> () const;
</pre>
<p>returns a pointer to the internal buffer of the <a class="reference external" href="reference-Utility.html#bitfield">bitfield</a>.</p>
<a name="operator=()"></a></div>
<div class="section" id="id15">
<h3>operator=()</h3>
<pre class="literal-block">
bitfield& <strong>operator=</strong> (bitfield const& rhs);
</pre>
<p>copy operator</p>
<a name="count()"></a></div>
<div class="section" id="count">
<h3>count()</h3>
<pre class="literal-block">
int <strong>count</strong> () const;
</pre>
<p>count the number of bits in the <a class="reference external" href="reference-Utility.html#bitfield">bitfield</a> that are set to 1.</p>
<a name="hasher"></a></div>
</div>
<div class="section" id="hasher">
<h2>hasher</h2>
<p>Declared in "<a class="reference external" href="../include/libtorrent/hasher.hpp">libtorrent/hasher.hpp</a>"</p>
<p>this is a SHA-1 hash class.</p>
<p>You use it by first instantiating it, then call <tt class="docutils literal">update()</tt> to feed it
with data. i.e. you don't have to keep the entire buffer of which you want to
create the hash in memory. You can feed the <a class="reference external" href="reference-Utility.html#hasher">hasher</a> parts of it at a time. When
You have fed the <a class="reference external" href="reference-Utility.html#hasher">hasher</a> with all the data, you call <tt class="docutils literal">final()</tt> and it
will return the sha1-hash of the data.</p>
<p>The constructor that takes a <tt class="docutils literal">char const*</tt> and an integer will construct the
sha1 context and feed it the data passed in.</p>
<p>If you want to reuse the <a class="reference external" href="reference-Utility.html#hasher">hasher</a> object once you have created a hash, you have to
call <tt class="docutils literal">reset()</tt> to reinitialize it.</p>
<p>The sha1-algorithm used was implemented by Steve Reid and released as public domain.
For more info, see <tt class="docutils literal">src/sha1.cpp</tt>.</p>
<pre class="literal-block">
class hasher
{
<strong>hasher</strong> ();
<strong>hasher</strong> (const char* data, int len);
hasher& <strong>operator=</strong> (hasher const& h);
<strong>hasher</strong> (hasher const& h);
hasher& <strong>update</strong> (std::string const& data);
hasher& <strong>update</strong> (const char* data, int len);
sha1_hash <strong>final</strong> ();
void <strong>reset</strong> ();
<strong>~hasher</strong> ();
};
</pre>
<a name="hasher()"></a><div class="section" id="id21">
<h3>hasher()</h3>
<pre class="literal-block">
<strong>hasher</strong> (const char* data, int len);
</pre>
<p>this is the same as default constructing followed by a call to
<tt class="docutils literal">update(data, len)</tt>.</p>
<a name="update()"></a></div>
<div class="section" id="update">
<h3>update()</h3>
<pre class="literal-block">
hasher& <strong>update</strong> (std::string const& data);
hasher& <strong>update</strong> (const char* data, int len);
</pre>
<p>append the following bytes to what is being hashed</p>
<a name="final()"></a></div>
<div class="section" id="final">
<h3>final()</h3>
<pre class="literal-block">
sha1_hash <strong>final</strong> ();
</pre>
<p>returns the SHA-1 digest of the buffers previously passed to
<a class="reference external" href="reference-Utility.html#update()">update()</a> and the <a class="reference external" href="reference-Utility.html#hasher">hasher</a> constructor.</p>
<a name="reset()"></a></div>
<div class="section" id="reset">
<h3>reset()</h3>
<pre class="literal-block">
void <strong>reset</strong> ();
</pre>
<p>restore the <a class="reference external" href="reference-Utility.html#hasher">hasher</a> state to be as if the <a class="reference external" href="reference-Utility.html#hasher">hasher</a> has just been
default constructed.</p>
<a name="sha1_hash"></a></div>
</div>
<div class="section" id="sha1-hash">
<h2>sha1_hash</h2>
<p>Declared in "<a class="reference external" href="../include/libtorrent/sha1_hash.hpp">libtorrent/sha1_hash.hpp</a>"</p>
<p>This type holds a SHA-1 digest or any other kind of 20 byte
sequence. It implements a number of convenience functions, such
as bit operations, comparison operators etc.</p>
<p>In libtorrent it is primarily used to hold info-hashes, piece-hashes,
peer IDs, node IDs etc.</p>
<pre class="literal-block">
class sha1_hash
{
<strong>sha1_hash</strong> ();
static sha1_hash <strong>max</strong> ();
static sha1_hash <strong>min</strong> ();
explicit <strong>sha1_hash</strong> (char const* s);
void <strong>assign</strong> (char const* str);
explicit <strong>sha1_hash</strong> (std::string const& s);
void <strong>assign</strong> (std::string const& s);
char* <strong>data</strong> ();
char const* <strong>data</strong> () const;
void <strong>clear</strong> ();
bool <strong>is_all_zeros</strong> () const;
sha1_hash& <strong>operator<<=</strong> (int n);
sha1_hash& <strong>operator>>=</strong> (int n);
bool <strong>operator!=</strong> (sha1_hash const& n) const;
bool <strong>operator<</strong> (sha1_hash const& n) const;
bool <strong>operator==</strong> (sha1_hash const& n) const;
sha1_hash <strong>operator~</strong> () const;
sha1_hash <strong>operator^</strong> (sha1_hash const& n) const;
sha1_hash& <strong>operator^=</strong> (sha1_hash const& n);
sha1_hash <strong>operator&</strong> (sha1_hash const& n) const;
sha1_hash& <strong>operator&=</strong> (sha1_hash const& n);
sha1_hash& <strong>operator|=</strong> (sha1_hash const& n);
boost::uint8_t& <strong>operator[]</strong> (int i);
boost::uint8_t const& <strong>operator[]</strong> (int i) const;
const_iterator <strong>begin</strong> () const;
iterator <strong>begin</strong> ();
iterator <strong>end</strong> ();
const_iterator <strong>end</strong> () const;
std::string <strong>to_string</strong> () const;
};
</pre>
<a name="sha1_hash()"></a><div class="section" id="id27">
<h3>sha1_hash()</h3>
<pre class="literal-block">
<strong>sha1_hash</strong> ();
</pre>
<p>constructs an all-zero sha1-hash</p>
<a name="max()"></a></div>
<div class="section" id="max">
<h3>max()</h3>
<pre class="literal-block">
static sha1_hash <strong>max</strong> ();
</pre>
<p>returns an all-F sha1-hash. i.e. the maximum value
representable by a 160 bit number (20 bytes). This is
a static member function.</p>
<a name="min()"></a></div>
<div class="section" id="min">
<h3>min()</h3>
<pre class="literal-block">
static sha1_hash <strong>min</strong> ();
</pre>
<p>returns an all-zero sha1-hash. i.e. the minimum value
representable by a 160 bit number (20 bytes). This is
a static member function.</p>
<a name="assign()"></a>
<a name="sha1_hash()"></a></div>
<div class="section" id="assign-sha1-hash">
<h3>assign() sha1_hash()</h3>
<pre class="literal-block">
explicit <strong>sha1_hash</strong> (char const* s);
void <strong>assign</strong> (char const* str);
explicit <strong>sha1_hash</strong> (std::string const& s);
void <strong>assign</strong> (std::string const& s);
</pre>
<p>copies 20 bytes from the pointer provided, into the sha1-hash.
The passed in string MUST be at least 20 bytes. NULL terminators
are ignored, <tt class="docutils literal">s</tt> is treated like a raw memory buffer.</p>
<a name="clear()"></a></div>
<div class="section" id="clear">
<h3>clear()</h3>
<pre class="literal-block">
void <strong>clear</strong> ();
</pre>
<p>set the sha1-hash to all zeros.</p>
<a name="is_all_zeros()"></a></div>
<div class="section" id="is-all-zeros">
<h3>is_all_zeros()</h3>
<pre class="literal-block">
bool <strong>is_all_zeros</strong> () const;
</pre>
<p>return true if the sha1-hash is all zero.</p>
<a name="operator<<=()"></a></div>
<div class="section" id="id28">
<h3>operator<<=()</h3>
<pre class="literal-block">
sha1_hash& <strong>operator<<=</strong> (int n);
</pre>
<p>shift left <tt class="docutils literal">n</tt> bits.</p>
<a name="operator>>=()"></a></div>
<div class="section" id="id29">
<h3>operator>>=()</h3>
<pre class="literal-block">
sha1_hash& <strong>operator>>=</strong> (int n);
</pre>
<p>shift r <tt class="docutils literal">n</tt> bits.</p>
<a name="operator!=()"></a>
<a name="operator<()"></a>
<a name="operator==()"></a></div>
<div class="section" id="operator-operator-operator">
<h3>operator!=() operator<() operator==()</h3>
<pre class="literal-block">
bool <strong>operator!=</strong> (sha1_hash const& n) const;
bool <strong>operator<</strong> (sha1_hash const& n) const;
bool <strong>operator==</strong> (sha1_hash const& n) const;
</pre>
<p>standard comparison operators</p>
<a name="operator~()"></a></div>
<div class="section" id="id30">
<h3>operator~()</h3>
<pre class="literal-block">
sha1_hash <strong>operator~</strong> () const;
</pre>
<p>returns a bit-wise negated copy of the sha1-hash</p>
<a name="operator^()"></a></div>
<div class="section" id="id31">
<h3>operator^()</h3>
<pre class="literal-block">
sha1_hash <strong>operator^</strong> (sha1_hash const& n) const;
</pre>
<p>returns the bit-wise XOR of the two sha1-hashes.</p>
<a name="operator^=()"></a></div>
<div class="section" id="id32">
<h3>operator^=()</h3>
<pre class="literal-block">
sha1_hash& <strong>operator^=</strong> (sha1_hash const& n);
</pre>
<p>in-place bit-wise XOR with the passed in <a class="reference external" href="reference-Utility.html#sha1_hash">sha1_hash</a>.</p>
<a name="operator&()"></a></div>
<div class="section" id="id34">
<h3>operator&()</h3>
<pre class="literal-block">
sha1_hash <strong>operator&</strong> (sha1_hash const& n) const;
</pre>
<p>returns the bit-wise AND of the two sha1-hashes.</p>
<a name="operator&=()"></a></div>
<div class="section" id="id35">
<h3>operator&=()</h3>
<pre class="literal-block">
sha1_hash& <strong>operator&=</strong> (sha1_hash const& n);
</pre>
<p>in-place bit-wise AND of the passed in <a class="reference external" href="reference-Utility.html#sha1_hash">sha1_hash</a></p>
<a name="operator|=()"></a></div>
<div class="section" id="id37">
<h3>operator|=()</h3>
<pre class="literal-block">
sha1_hash& <strong>operator|=</strong> (sha1_hash const& n);
</pre>
<p>in-place bit-wise OR of the two sha1-hash.</p>
<a name="operator[]()"></a></div>
<div class="section" id="id38">
<h3>operator[]()</h3>
<pre class="literal-block">
boost::uint8_t& <strong>operator[]</strong> (int i);
boost::uint8_t const& <strong>operator[]</strong> (int i) const;
</pre>
<p>accessors for specific bytes</p>
<a name="begin()"></a>
<a name="end()"></a></div>
<div class="section" id="begin-end">
<h3>begin() end()</h3>
<pre class="literal-block">
const_iterator <strong>begin</strong> () const;
iterator <strong>begin</strong> ();
iterator <strong>end</strong> ();
const_iterator <strong>end</strong> () const;
</pre>
<p>start and end iterators for the hash. The value type
of these iterators is <tt class="docutils literal"><span class="pre">boost::uint8_t</span></tt>.</p>
<a name="to_string()"></a></div>
<div class="section" id="to-string">
<h3>to_string()</h3>
<pre class="literal-block">
std::string <strong>to_string</strong> () const;
</pre>
<p>return a copy of the 20 bytes representing the sha1-hash as a std::string.
It's still a binary string with 20 binary characters.</p>
<a name="hash_value()"></a></div>
</div>
<div class="section" id="hash-value">
<h2>hash_value()</h2>
<p>Declared in "<a class="reference external" href="../include/libtorrent/sha1_hash.hpp">libtorrent/sha1_hash.hpp</a>"</p>
<pre class="literal-block">
inline std::size_t <strong>hash_value</strong> (sha1_hash const& b);
</pre>
<a name="operator<<()"></a></div>
<div class="section" id="id40">
<h2>operator<<()</h2>
<p>Declared in "<a class="reference external" href="../include/libtorrent/sha1_hash.hpp">libtorrent/sha1_hash.hpp</a>"</p>
<pre class="literal-block">
inline std::ostream& <strong>operator<<</strong> (std::ostream& os, sha1_hash const& peer);
</pre>
<p>print a <a class="reference external" href="reference-Utility.html#sha1_hash">sha1_hash</a> object to an ostream as 40 hexadecimal digits</p>
<a name="operator>>()"></a></div>
<div class="section" id="id43">
<h2>operator>>()</h2>
<p>Declared in "<a class="reference external" href="../include/libtorrent/sha1_hash.hpp">libtorrent/sha1_hash.hpp</a>"</p>
<pre class="literal-block">
inline std::istream& <strong>operator>></strong> (std::istream& is, sha1_hash& peer);
</pre>
<p>read 40 hexadecimal digits from an istream into a <a class="reference external" href="reference-Utility.html#sha1_hash">sha1_hash</a></p>
</div>
</div>
</div>
</div>
<div id="gradient"></div>
<div id="footer">
<table>
<tr>
<td><a href="index.html">home</a></td>
<td><a href="https://blog.libtorrent.org">blog</a></td>
<td><a href="utp.html">uTP</a></td>
</tr>
<tr>
<td><a href="https://sourceforge.net/projects/libtorrent/files/libtorrent/">download</a></td>
<td><a href="reference.html">documentation</a></td>
<td><a href="dht_store.html">DHT put extension</a></td>
</tr>
<tr>
<td><a href="https://sourceforge.net/projects/libtorrent/files/py-libtorrent/">python bindings</a></td>
<td><a href="features.html">features</a></td>
<td><a href="dht_sec.html">DHT security extension</a></td>
</tr>
<tr>
<td><a href="https://sourceforge.net/p/libtorrent/mailman/libtorrent-discuss/">mailing list archive</a></td>
<td><a href="contributing.html">contributing</a></td>
<td><a href="streaming.html">streaming</a></td>
</tr>
<tr>
<td><a href="https://github.com/arvidn/libtorrent/issues">report a bug</a></td>
<td><a href="building.html">building</a></td>
<td><a href="bittorrent.pdf">bittorrent slides</a></td>
</tr>
</table>
</div>
<div id="filler"></div>
</div>
</body>
</html>
|