Changeset 6730


Ignore:
Timestamp:
Sep 6, 2008, 1:25:21 PM (13 years ago)
Author:
charles
Message:

(libT) calculate a piece's checksum on the stack, rather than the heap

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/inout.c

    r6425 r6730  
    1919#include <sys/stat.h>
    2020#include <unistd.h>
     21
     22#include <openssl/sha.h>
    2123
    2224#include "transmission.h"
     
    227229                 uint8_t           * setme )
    228230{
    229     static uint8_t * buf = NULL;
    230     static int buflen = 0;
    231     static tr_lock * lock = NULL;
    232 
    233     int n;
    234     tr_errno err;
    235 
    236     /* only check one block at a time to prevent disk thrashing.
    237      * this also lets us reuse the same buffer each time. */
    238     if( lock == NULL )
    239         lock = tr_lockNew( );
    240 
    241     tr_lockLock( lock );
     231    size_t bytesLeft;
     232    size_t n;
     233    uint32_t offset = 0;
     234    tr_errno err =  0;
     235    SHA_CTX sha;
    242236
    243237    assert( tor );
     
    245239    assert( pieceIndex < tor->info.pieceCount );
    246240
    247     n = tr_torPieceCountBytes( tor, pieceIndex );
    248 
    249     if( buflen < n ) {
    250         buflen = n;
    251         buf = tr_renew( uint8_t, buf, buflen );
     241    SHA1_Init( &sha );
     242    n = bytesLeft = tr_torPieceCountBytes( tor, pieceIndex );
     243
     244    while( bytesLeft )
     245    {
     246        uint8_t buf[2048];
     247        const int len = MIN( bytesLeft, sizeof( buf ) );
     248        if(( err = tr_ioRead( tor, pieceIndex, offset, len, buf )))
     249            break;
     250        SHA1_Update( &sha, buf, len );
     251        offset += len;
     252        bytesLeft -= len;
    252253    }
    253        
    254     err = tr_ioRead( tor, pieceIndex, 0, n, buf );
     254
    255255    if( !err )
    256         tr_sha1( setme, buf, n, NULL );
    257 
    258     tr_lockUnlock( lock );
     256        SHA1_Final( setme, &sha );
     257
    259258    return err;
    260259}
Note: See TracChangeset for help on using the changeset viewer.