mirror of
https://github.com/transmission/transmission
synced 2024-12-25 17:17:31 +00:00
tr_ioRecalculateHash(): use a static buffer instead of malloc/free.
This commit is contained in:
parent
f32d3b24fe
commit
b29ba709b3
1 changed files with 21 additions and 10 deletions
|
@ -18,6 +18,8 @@
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <openssl/sha.h>
|
||||||
|
|
||||||
#include "transmission.h"
|
#include "transmission.h"
|
||||||
#include "completion.h"
|
#include "completion.h"
|
||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
|
@ -224,25 +226,34 @@ tr_ioRecalculateHash( tr_torrent * tor,
|
||||||
int pieceIndex,
|
int pieceIndex,
|
||||||
uint8_t * setme )
|
uint8_t * setme )
|
||||||
{
|
{
|
||||||
int n;
|
int offset;
|
||||||
int ret;
|
int bytesLeft;
|
||||||
uint8_t * buf;
|
uint8_t buf[4096];
|
||||||
const tr_info * info;
|
const tr_info * info;
|
||||||
|
SHA_CTX sha;
|
||||||
|
|
||||||
assert( tor != NULL );
|
assert( tor != NULL );
|
||||||
assert( setme != NULL );
|
assert( setme != NULL );
|
||||||
assert( 0<=pieceIndex && pieceIndex<tor->info.pieceCount );
|
assert( 0<=pieceIndex && pieceIndex<tor->info.pieceCount );
|
||||||
|
|
||||||
info = &tor->info;
|
info = &tor->info;
|
||||||
n = tr_torPieceCountBytes( tor, pieceIndex );
|
offset = 0;
|
||||||
|
bytesLeft = tr_torPieceCountBytes( tor, pieceIndex );
|
||||||
|
SHA1_Init( &sha );
|
||||||
|
|
||||||
buf = tr_new( uint8_t, n );
|
while( bytesLeft > 0 )
|
||||||
ret = tr_ioRead( tor, pieceIndex, 0, n, buf );
|
{
|
||||||
if( !ret )
|
const int bytesThisPass = MIN( bytesLeft, (int)sizeof(buf) );
|
||||||
tr_sha1( setme, buf, n, NULL );
|
int ret = tr_ioRead( tor, pieceIndex, offset, bytesThisPass, buf );
|
||||||
tr_free( buf );
|
if( ret )
|
||||||
|
return ret;
|
||||||
|
SHA1_Update( &sha, buf, bytesThisPass );
|
||||||
|
bytesLeft -= bytesThisPass;
|
||||||
|
offset += bytesThisPass;
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
SHA1_Final( setme, &sha );
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
|
Loading…
Reference in a new issue