1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 01:27:28 +00:00

more metainfo/bencode cleanup

This commit is contained in:
Charles Kerr 2008-08-21 16:12:17 +00:00
parent dcecf09d96
commit 58881d45be
9 changed files with 53 additions and 54 deletions

View file

@ -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;
}

View file

@ -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 );
}
}

View file

@ -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; i<n; ++i ) {
const char * str;
if( tr_bencGetStr( tr_bencListChild( path, i ), &str ) && strcmp( str, ".." ) ) {
if( tr_bencGetStr( tr_bencListChild( path, i ), &str )
&& strcmp( str, ".." ) ) {
evbuffer_add( buf, TR_PATH_DELIMITER_STR, 1 );
evbuffer_add( buf, str, strlen( str ) );
}
@ -154,14 +157,16 @@ getfile( char ** setme, const char * root, tr_benc * path )
}
static const char*
parseFiles( tr_info * inf, tr_benc * files, tr_benc * length )
parseFiles( tr_info * inf, tr_benc * files, const tr_benc * length )
{
tr_file_index_t i;
int64_t len;
inf->totalSize = 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; i<tiers->val.l.count; ++i )
n += tiers->val.l.vals[i].val.l.count;
for( i=0; i<numTiers; ++i )
n += tr_bencListSize( tr_bencListChild( tiers, i ) );
trackers = tr_new0( tr_tracker_info, n );
trackerCount = 0;
for( i=0; i<tiers->val.l.count; ++i ) {
const tr_benc * tier = &tiers->val.l.vals[i];
for( j=0; tr_bencIsList(tier) && j<tier->val.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; i<numTiers; ++i ) {
tr_benc * tier = tr_bencListChild( tiers, i );
const int tierSize = tr_bencListSize( tier );
for( j=0; j<tierSize; ++j ) {
const char * str;
if( tr_bencGetStr( tr_bencListChild( tier, j ), &str )
&& tr_httpIsValidURL( str ) ) {
tr_tracker_info * t = trackers + trackerCount++;
t->tier = 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 );*/
}
}
}

View file

@ -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;
}

View file

@ -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 )
{

View file

@ -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 );

View file

@ -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<NUM_LOOPS; ++l )
if(( i = test_bitfields( )))

View file

@ -190,7 +190,7 @@ tr_deepLog( const char * file, int line, const char * name, const char * fmt, ..
evbuffer_add_vprintf( buf, fmt, args );
va_end( args );
evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line );
fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp );
fwrite( EVBUFFER_DATA( buf ), 1, EVBUFFER_LENGTH( buf ), fp );
tr_free( myfile );
evbuffer_free( buf );
@ -543,7 +543,7 @@ tr_buildPath ( char *buf, size_t buflen, const char *first_element, ... )
element = (const char*) va_arg( vl, const char* );
}
if( EVBUFFER_LENGTH(evbuf) )
tr_strlcpy( buf, (char*)EVBUFFER_DATA(evbuf), buflen );
tr_strlcpy( buf, EVBUFFER_DATA( evbuf ), buflen );
else
*buf = '\0';
@ -664,7 +664,7 @@ tr_strdup_printf( const char * fmt, ... )
va_start( ap, fmt );
if( evbuffer_add_vprintf( buf, fmt, ap ) != -1 )
ret = tr_strdup( (char*)EVBUFFER_DATA( buf ) );
ret = tr_strdup( EVBUFFER_DATA( buf ) );
va_end( ap );
evbuffer_free( buf );
@ -937,14 +937,6 @@ tr_wait( uint64_t delay_milliseconds )
****
***/
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 );
}
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
}

View file

@ -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 );
/***