Changeset 12921


Ignore:
Timestamp:
Sep 26, 2011, 10:50:42 PM (10 years ago)
Author:
jordan
Message:

(trunk libt) in tr_bitfieldSetRaw(), add a `bounded' argument for cases where we know how large the final bitfield will be. This can be used ensure that the excess bits at the end of the array are zeroed out and safe for bitfield.c's countArray() function.

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bitfield.c

    r12904 r12921  
    282282        tr_bitfieldSetHasNone( b );
    283283    else
    284         tr_bitfieldSetRaw( b, src->bits, src->alloc_count );
    285 }
    286 
    287 void
    288 tr_bitfieldSetRaw( tr_bitfield * b, const void * bits, size_t byte_count )
     284        tr_bitfieldSetRaw( b, src->bits, src->alloc_count, true );
     285}
     286
     287void
     288tr_bitfieldSetRaw( tr_bitfield * b, const void * bits, size_t byte_count, bool bounded )
    289289{
    290290    tr_bitfieldFreeArray( b );
    291291    b->true_count = 0;
     292
     293    if( bounded )
     294        byte_count = MIN( byte_count, get_bytes_needed( b->bit_count ) );
     295
    292296    b->bits = tr_memdup( bits, byte_count );
    293297    b->alloc_count = byte_count;
     298
     299    if( bounded ) {
     300        /* ensure the excess bits are set to '0' */
     301        const int excess_bit_count = byte_count*8 - b->bit_count;
     302        assert( excess_bit_count >= 0 );
     303        assert( excess_bit_count <= 7 );
     304        if( excess_bit_count )
     305            b->bits[b->alloc_count-1] &= ((0xff) << excess_bit_count);
     306    }
     307
    294308    tr_bitfieldRebuildTrueCount( b );
    295309}
  • trunk/libtransmission/bitfield.h

    r12902 r12921  
    7575void   tr_bitfieldSetFromBitfield( tr_bitfield*, const tr_bitfield* );
    7676
    77 void   tr_bitfieldSetRaw( tr_bitfield*, const void * bits, size_t byte_count );
     77void   tr_bitfieldSetRaw( tr_bitfield*, const void * bits, size_t byte_count, bool bounded );
    7878
    7979void*  tr_bitfieldGetRaw( const tr_bitfield * b, size_t * byte_count );
  • trunk/libtransmission/peer-msgs.c

    r12590 r12921  
    14151415            dbgmsg( msgs, "got a bitfield" );
    14161416            tr_peerIoReadBytes( msgs->peer->io, inbuf, tmp, msglen );
    1417             tr_bitfieldSetRaw( &msgs->peer->have, tmp, msglen );
     1417            tr_bitfieldSetRaw( &msgs->peer->have, tmp, msglen, tr_torrentHasMetadata( msgs->torrent ) );
    14181418            fireClientGotBitfield( msgs, &msgs->peer->have );
    14191419            updatePeerProgress( msgs );
  • trunk/libtransmission/resume.c

    r12291 r12921  
    592592                tr_bitfieldSetHasNone( &blocks );
    593593            else
    594                 tr_bitfieldSetRaw( &blocks, buf, buflen );
     594                tr_bitfieldSetRaw( &blocks, buf, buflen, true );
    595595        }
    596596        else if( tr_bencDictFindStr( prog, KEY_PROGRESS_HAVE, &str ) )
     
    603603        else if( tr_bencDictFindRaw( prog, KEY_PROGRESS_BITFIELD, &raw, &rawlen ) )
    604604        {
    605             tr_bitfieldSetRaw( &blocks, raw, rawlen );
     605            tr_bitfieldSetRaw( &blocks, raw, rawlen, true );
    606606        }
    607607        else err = "Couldn't find 'pieces' or 'have' or 'bitfield'";
Note: See TracChangeset for help on using the changeset viewer.