From 58881d45bec1430f710ea1ba4c0ffc32251187d1 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 21 Aug 2008 16:12:17 +0000 Subject: [PATCH] more metainfo/bencode cleanup --- libtransmission/bencode.c | 4 +-- libtransmission/clients.c | 2 +- libtransmission/metainfo.c | 58 ++++++++++++++++++++---------------- libtransmission/rpc-server.c | 2 +- libtransmission/session.c | 8 +++++ libtransmission/tracker.c | 4 +-- libtransmission/utils-test.c | 7 ----- libtransmission/utils.c | 18 ++++------- libtransmission/utils.h | 4 +-- 9 files changed, 53 insertions(+), 54 deletions(-) diff --git a/libtransmission/bencode.c b/libtransmission/bencode.c index cda93e818..4184f2cb5 100644 --- a/libtransmission/bencode.c +++ b/libtransmission/bencode.c @@ -912,7 +912,7 @@ tr_bencSave( const tr_benc * top, int * len ) if( len ) *len = EVBUFFER_LENGTH( out ); - ret = tr_strndup( (char*) EVBUFFER_DATA( out ), EVBUFFER_LENGTH( out ) ); + ret = tr_strndup( EVBUFFER_DATA( out ), EVBUFFER_LENGTH( out ) ); evbuffer_free( out ); return ret; } @@ -1221,7 +1221,7 @@ tr_bencSaveAsJSON( const tr_benc * top, int * len ) evbuffer_add_printf( data.out, "\n" ); if( len ) *len = EVBUFFER_LENGTH( data.out ); - ret = tr_strndup( (char*) EVBUFFER_DATA( data.out ), EVBUFFER_LENGTH( data.out ) ); + ret = tr_strndup( EVBUFFER_DATA( data.out ), EVBUFFER_LENGTH( data.out ) ); evbuffer_free( data.out ); return ret; } diff --git a/libtransmission/clients.c b/libtransmission/clients.c index 23963c0be..a1992d3d8 100644 --- a/libtransmission/clients.c +++ b/libtransmission/clients.c @@ -375,7 +375,7 @@ tr_clientForId( char * buf, size_t buflen, const void * id_in ) evbuffer_add_printf( out, "%%%02X", (unsigned int)*in ); } - tr_strlcpy( buf, (const char*)EVBUFFER_DATA(out), buflen ); + tr_strlcpy( buf, EVBUFFER_DATA( out ), buflen ); evbuffer_free( out ); } } diff --git a/libtransmission/metainfo.c b/libtransmission/metainfo.c index 9fc740fb0..2ae21323b 100644 --- a/libtransmission/metainfo.c +++ b/libtransmission/metainfo.c @@ -78,9 +78,9 @@ tr_metainfoMigrate( tr_handle * handle, tr_info * inf ) { struct stat new_sb; - char * new_name = getTorrentFilename( handle, inf ); + char * name = getTorrentFilename( handle, inf ); - if( stat( new_name, &new_sb ) || ( ( new_sb.st_mode & S_IFMT ) != S_IFREG ) ) + if( stat( name, &new_sb ) || ( ( new_sb.st_mode & S_IFMT ) != S_IFREG ) ) { char * old_name = getOldTorrentFilename( handle, inf ); size_t contentLen; @@ -91,18 +91,20 @@ tr_metainfoMigrate( tr_handle * handle, { FILE * out; errno = 0; - out = fopen( new_name, "wb+" ); + out = fopen( name, "wb+" ); if( !out ) { - tr_nerr( inf->name, _( "Couldn't create \"%1$s\": %2$s" ), new_name, tr_strerror( errno ) ); + tr_nerr( inf->name, _( "Couldn't create \"%1$s\": %2$s" ), + name, tr_strerror( errno ) ); } else { - if( fwrite( content, sizeof( uint8_t ), contentLen, out ) == contentLen ) + if( fwrite( content, sizeof( uint8_t ), contentLen, out ) + == contentLen ) { tr_free( inf->torrent ); - inf->torrent = tr_strdup( new_name ); - tr_sessionSetTorrentFile( handle, inf->hashString, new_name ); + inf->torrent = tr_strdup( name ); + tr_sessionSetTorrentFile( handle, inf->hashString, name ); unlink( old_name ); } fclose( out ); @@ -113,7 +115,7 @@ tr_metainfoMigrate( tr_handle * handle, tr_free( old_name ); } - tr_free( new_name ); + tr_free( name ); } /*** @@ -138,7 +140,8 @@ getfile( char ** setme, const char * root, tr_benc * path ) evbuffer_add( buf, root, strlen( root ) ); for( i=0; itotalSize = 0; if( tr_bencIsList( files ) ) /* multi-file mode */ { + tr_file_index_t i; + inf->isMultifile = 1; inf->fileCount = tr_bencListSize( files ); inf->files = tr_new0( tr_file, inf->fileCount ); @@ -190,14 +195,14 @@ parseFiles( tr_info * inf, tr_benc * files, tr_benc * length ) inf->totalSize += length; } } - else if( tr_bencIsInt( length ) ) /* single-file mode */ + else if( tr_bencGetInt( length, &len ) ) /* single-file mode */ { inf->isMultifile = 0; inf->fileCount = 1; inf->files = tr_new0( tr_file, 1 ); inf->files[0].name = tr_strdup( inf->name ); - inf->files[0].length = length->val.i; - inf->totalSize += length->val.i; + inf->files[0].length = len; + inf->totalSize += len; } else { @@ -225,7 +230,7 @@ announceToScrape( const char * announce ) evbuffer_add( buf, announce, s-announce ); evbuffer_add( buf, "scrape", 6 ); evbuffer_add_printf( buf, "%s", s+8 ); - scrape = tr_strdup( ( char * ) EVBUFFER_DATA( buf ) ); + scrape = tr_strdup( EVBUFFER_DATA( buf ) ); evbuffer_free( buf ); } @@ -245,24 +250,27 @@ getannounce( tr_info * inf, tr_benc * meta ) { int n; int i, j; + const int numTiers = tr_bencListSize( tiers ); n = 0; - for( i=0; ival.l.count; ++i ) - n += tiers->val.l.vals[i].val.l.count; + for( i=0; ival.l.count; ++i ) { - const tr_benc * tier = &tiers->val.l.vals[i]; - for( j=0; tr_bencIsList(tier) && jval.l.count; ++j ) { - const tr_benc * a = &tier->val.l.vals[j]; - if( tr_bencIsString( a ) && tr_httpIsValidURL( a->val.s.s ) ) { + for( i=0; itier = i; - t->announce = tr_strndup( a->val.s.s, a->val.s.i ); - t->scrape = announceToScrape( a->val.s.s ); - /*fprintf( stderr, "tier %d: %s\n", i, a->val.s.s );*/ + t->announce = tr_strdup( str ); + t->scrape = announceToScrape( str ); + /*fprintf( stderr, "tier %d: %s\n", i, str );*/ } } } diff --git a/libtransmission/rpc-server.c b/libtransmission/rpc-server.c index 7b441e223..867de3306 100644 --- a/libtransmission/rpc-server.c +++ b/libtransmission/rpc-server.c @@ -558,7 +558,7 @@ cidrize( const char * acl ) } /* the -1 is to eat the final ", " */ - ret = tr_strndup( (char*) EVBUFFER_DATA(out), EVBUFFER_LENGTH(out)-1 ); + ret = tr_strndup( EVBUFFER_DATA( out ), EVBUFFER_LENGTH( out ) - 1 ); evbuffer_free( out ); return ret; } diff --git a/libtransmission/session.c b/libtransmission/session.c index 73250efcc..ca36249a2 100644 --- a/libtransmission/session.c +++ b/libtransmission/session.c @@ -104,6 +104,14 @@ tr_sessionSetEncryption( tr_session * session, tr_encryption_mode mode ) **** ***/ +static int +tr_stringEndsWith( const char * str, const char * end ) +{ + const size_t slen = strlen( str ); + const size_t elen = strlen( end ); + return slen>=elen && !memcmp( &str[slen-elen], end, elen ); +} + static void loadBlocklists( tr_session * session ) { diff --git a/libtransmission/tracker.c b/libtransmission/tracker.c index e7bc45586..38c252517 100644 --- a/libtransmission/tracker.c +++ b/libtransmission/tracker.c @@ -633,7 +633,7 @@ createRequest( tr_session * session, tr_tracker * tracker, int reqtype ) req->session = session; req->reqtype = reqtype; req->done_func = isStopping ? onStoppedResponse : onTrackerResponse; - req->url = tr_strdup( ( char * ) EVBUFFER_DATA( url ) ); + req->url = tr_strdup( EVBUFFER_DATA( url ) ); memcpy( req->torrent_hash, tracker->hash, SHA_DIGEST_LENGTH ); evbuffer_free( url ); @@ -654,7 +654,7 @@ createScrape( tr_session * session, tr_tracker * tracker ) req = tr_new0( struct tr_tracker_request, 1 ); req->session = session; req->reqtype = TR_REQ_SCRAPE; - req->url = tr_strdup( ( char * ) EVBUFFER_DATA( url ) ); + req->url = tr_strdup( EVBUFFER_DATA( url ) ); req->done_func = onScrapeResponse; memcpy( req->torrent_hash, tracker->hash, SHA_DIGEST_LENGTH ); diff --git a/libtransmission/utils-test.c b/libtransmission/utils-test.c index 7f3a61174..71b5987d8 100644 --- a/libtransmission/utils-test.c +++ b/libtransmission/utils-test.c @@ -106,13 +106,6 @@ main( void ) tr_free( in ); tr_free( out ); - /* tr_stringEndsWith */ - check( tr_stringEndsWith( "the", "the" ) ); - check( tr_stringEndsWith( "dress", "dress" ) ); - check( tr_stringEndsWith( "address", "dress" ) ); - check( !tr_stringEndsWith( "foo.bin", "gfoo.bin" ) ); - check( !tr_stringEndsWith( "xyz", "xyw" ) ); - /* simple bitfield tests */ for( l=0; l=elen && !memcmp( &str[slen-elen], end, elen ); -} - int tr_snprintf( char * buf, size_t buflen, const char * fmt, ... ) { @@ -963,7 +955,7 @@ tr_snprintf( char * buf, size_t buflen, const char * fmt, ... ) * Returns strlen(src); if retval >= siz, truncation occurred. */ size_t -tr_strlcpy(char *dst, const char *src, size_t siz) +tr_strlcpy(char *dst, const void * src, size_t siz) { #ifdef HAVE_STRLCPY return strlcpy( dst, src, siz ); @@ -991,7 +983,7 @@ tr_strlcpy(char *dst, const char *src, size_t siz) ; } - return(s - src - 1); /* count does not include NUL */ + return(s - (char*)src - 1); /* count does not include NUL */ #endif } diff --git a/libtransmission/utils.h b/libtransmission/utils.h index c526ea610..a5a857295 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -193,11 +193,9 @@ char* tr_strdup_printf( const char * fmt, ... ) TR_GNUC_PRINTF( 1, 2 ) TR_GNUC_ char* tr_base64_encode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC; char* tr_base64_decode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC; -size_t tr_strlcpy( char * dst, const char * src, size_t siz ); +size_t tr_strlcpy( char * dst, const void * src, size_t siz ); int tr_snprintf( char * buf, size_t buflen, const char * fmt, ... ); -int tr_stringEndsWith( const char * string, const char * end ); - const char* tr_strerror( int ); /***