2009-06-15 00:11:06 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2009-06-15 00:11:06 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-06-15 00:11:06 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2009-06-15 00:11:06 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2009-06-15 00:11:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "tr-macros.h"
|
|
|
|
|
|
|
|
TR_BEGIN_DECLS
|
2009-06-15 00:11:06 +00:00
|
|
|
|
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
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint8_t* bits;
|
|
|
|
size_t alloc_count;
|
2011-03-30 04:14:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t bit_count;
|
2011-03-30 04:14:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t true_count;
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +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;
|
2021-08-15 09:41:48 +00:00
|
|
|
} tr_bitfield;
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bitfieldSetHasAll(tr_bitfield*);
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bitfieldSetHasNone(tr_bitfield*);
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bitfieldAdd(tr_bitfield*, size_t bit);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bitfieldRem(tr_bitfield*, size_t bit);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bitfieldAddRange(tr_bitfield*, size_t begin, size_t end);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +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
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bitfieldConstruct(tr_bitfield*, size_t bit_count);
|
2011-03-29 01:17:18 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static inline void tr_bitfieldDestruct(tr_bitfield* b)
|
2011-03-29 01:17:18 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_bitfieldSetFromFlags(tr_bitfield*, bool const* bytes, size_t n);
|
2011-09-20 23:39:40 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_bitfieldSetFromBitfield(tr_bitfield*, tr_bitfield const*);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_bitfieldSetRaw(tr_bitfield*, void const* bits, size_t byte_count, bool bounded);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void* tr_bitfieldGetRaw(tr_bitfield const* 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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t tr_bitfieldCountRange(tr_bitfield const*, size_t begin, size_t end);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t tr_bitfieldCountTrueBits(tr_bitfield const* b);
|
2011-02-23 03:54:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static inline bool tr_bitfieldHasAll(tr_bitfield const* b)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-05-01 15:47:49 +00:00
|
|
|
return b->bit_count != 0 ? (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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static inline bool tr_bitfieldHasNone(tr_bitfield const* b)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-05-01 15:47:49 +00:00
|
|
|
return b->bit_count != 0 ? (b->true_count == 0) : b->have_none_hint;
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_bitfieldHas(tr_bitfield const* b, size_t n);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
TR_END_DECLS
|