2009-06-15 00:11:06 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2009-06-15 00:11:06 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2012-12-05 17:29:46 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2 (b)
|
2009-08-10 20:04:08 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2009-06-15 00:11:06 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2010-10-01 13:33:39 +00:00
|
|
|
* $Id$
|
2009-06-15 00:11:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __TRANSMISSION__
|
2010-01-14 14:20:49 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2009-06-15 00:11:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TR_BITFIELD_H
|
|
|
|
#define TR_BITFIELD_H 1
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief Implementation of the BitTorrent spec's Bitfield array of bits */
|
2009-06-15 00:11:06 +00:00
|
|
|
typedef struct tr_bitfield
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
uint8_t * bits;
|
|
|
|
size_t alloc_count;
|
2011-03-30 04:14:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
size_t bit_count;
|
2011-03-30 04:14:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
size_t true_count;
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* Special cases for when full or empty but we don't know the bitCount.
|
|
|
|
This occurs when a magnet link's peers send have all / have none */
|
|
|
|
bool have_all_hint;
|
|
|
|
bool have_none_hint;
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
tr_bitfield;
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_bitfieldSetHasAll (tr_bitfield*);
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_bitfieldSetHasNone (tr_bitfield*);
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_bitfieldAdd (tr_bitfield*, size_t bit);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_bitfieldRem (tr_bitfield*, size_t bit);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_bitfieldAddRange (tr_bitfield*, size_t begin, size_t end);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_bitfieldRemRange (tr_bitfield*, size_t begin, size_t end);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-03-29 01:17:18 +00:00
|
|
|
/***
|
|
|
|
**** life cycle
|
|
|
|
***/
|
|
|
|
|
|
|
|
extern const tr_bitfield TR_BITFIELD_INIT;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_bitfieldConstruct (tr_bitfield*, size_t bit_count);
|
2011-03-29 01:17:18 +00:00
|
|
|
|
|
|
|
static inline void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_bitfieldDestruct (tr_bitfield * b)
|
2011-03-29 01:17:18 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_bitfieldSetHasNone (b);
|
2011-03-29 01:17:18 +00:00
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_bitfieldSetFromFlags (tr_bitfield*, const bool * bytes, size_t n);
|
2011-09-20 23:39:40 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_bitfieldSetFromBitfield (tr_bitfield*, const tr_bitfield*);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_bitfieldSetRaw (tr_bitfield*, const void * bits, size_t byte_count, bool bounded);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void* tr_bitfieldGetRaw (const tr_bitfield * b, size_t * byte_count);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
size_t tr_bitfieldCountRange (const tr_bitfield*, size_t begin, size_t end);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
size_t tr_bitfieldCountTrueBits (const tr_bitfield * b);
|
2011-02-23 03:54:04 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
static inline bool
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_bitfieldHasAll (const tr_bitfield * b)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return b->bit_count ? (b->true_count == b->bit_count) : b->have_all_hint;
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2011-02-23 03:54:04 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
static inline bool
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_bitfieldHasNone (const tr_bitfield * b)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return b->bit_count ? (b->true_count == 0) : b->have_none_hint;
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2012-12-13 02:00:45 +00:00
|
|
|
bool tr_bitfieldHas (const tr_bitfield * b, size_t n);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
|
|
|
#endif
|