Changeset 4842


Ignore:
Timestamp:
Jan 27, 2008, 5:03:58 PM (14 years ago)
Author:
charles
Message:

tr_ioRecalculateHash(): use a static buffer instead of malloc/free.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/inout.c

    r4777 r4842  
    1818#include <sys/stat.h>
    1919#include <unistd.h>
     20
     21#include <openssl/sha.h>
    2022
    2123#include "transmission.h"
     
    225227                      uint8_t     * setme )
    226228{
    227     int n;
    228     int ret;
    229     uint8_t * buf;
     229    int offset;
     230    int bytesLeft;
     231    uint8_t buf[4096];
    230232    const tr_info * info;
     233    SHA_CTX sha;
    231234
    232235    assert( tor != NULL );
     
    235238
    236239    info = &tor->info;
    237     n = tr_torPieceCountBytes( tor, pieceIndex );
    238 
    239     buf = tr_new( uint8_t, n );
    240     ret = tr_ioRead( tor, pieceIndex, 0, n, buf );
    241     if( !ret )
    242         tr_sha1( setme, buf, n, NULL );
    243     tr_free( buf );
    244 
    245     return ret;
     240    offset = 0;
     241    bytesLeft = tr_torPieceCountBytes( tor, pieceIndex );
     242    SHA1_Init( &sha );
     243
     244    while( bytesLeft > 0 )
     245    {
     246        const int bytesThisPass = MIN( bytesLeft, (int)sizeof(buf) );
     247        int ret = tr_ioRead( tor, pieceIndex, offset, bytesThisPass, buf );
     248        if( ret )
     249            return ret;
     250        SHA1_Update( &sha, buf, bytesThisPass );
     251        bytesLeft -= bytesThisPass;
     252        offset += bytesThisPass;
     253    }
     254
     255    SHA1_Final( setme, &sha );
     256    return 0;
    246257}
    247258
Note: See TracChangeset for help on using the changeset viewer.