From b29ba709b37d8873ef882322687d9f12cb65fb6a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 27 Jan 2008 17:03:58 +0000 Subject: [PATCH] tr_ioRecalculateHash(): use a static buffer instead of malloc/free. --- libtransmission/inout.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/libtransmission/inout.c b/libtransmission/inout.c index 6e05580d0..50c80c50a 100644 --- a/libtransmission/inout.c +++ b/libtransmission/inout.c @@ -18,6 +18,8 @@ #include #include +#include + #include "transmission.h" #include "completion.h" #include "crypto.h" @@ -224,25 +226,34 @@ tr_ioRecalculateHash( tr_torrent * tor, int pieceIndex, uint8_t * setme ) { - int n; - int ret; - uint8_t * buf; + int offset; + int bytesLeft; + uint8_t buf[4096]; const tr_info * info; + SHA_CTX sha; assert( tor != NULL ); assert( setme != NULL ); assert( 0<=pieceIndex && pieceIndexinfo.pieceCount ); info = &tor->info; - n = tr_torPieceCountBytes( tor, pieceIndex ); + offset = 0; + bytesLeft = tr_torPieceCountBytes( tor, pieceIndex ); + SHA1_Init( &sha ); - buf = tr_new( uint8_t, n ); - ret = tr_ioRead( tor, pieceIndex, 0, n, buf ); - if( !ret ) - tr_sha1( setme, buf, n, NULL ); - tr_free( buf ); + while( bytesLeft > 0 ) + { + const int bytesThisPass = MIN( bytesLeft, (int)sizeof(buf) ); + int ret = tr_ioRead( tor, pieceIndex, offset, bytesThisPass, buf ); + if( ret ) + return ret; + SHA1_Update( &sha, buf, bytesThisPass ); + bytesLeft -= bytesThisPass; + offset += bytesThisPass; + } - return ret; + SHA1_Final( setme, &sha ); + return 0; } static int