bencode cleanup: remove unused functions and unnecessary #includes

This commit is contained in:
Charles Kerr 2008-08-21 14:57:59 +00:00
parent 7edebaf18e
commit dcecf09d96
11 changed files with 126 additions and 151 deletions

View File

@ -25,6 +25,7 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>

View File

@ -1,5 +1,6 @@
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "transmission.h"
#include "bencode.h"
#include "json.h"

View File

@ -13,9 +13,9 @@
#include <assert.h>
#include <ctype.h> /* isdigit, isprint, isspace */
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <event.h> /* evbuffer */
@ -293,7 +293,7 @@ tr_bencParseImpl( const void * buf_in,
return TR_ERROR;
}
tr_bencInitStr( node, str, str_len, 0 );
tr_bencInitStr( node, str, str_len );
buf = end;
if( tr_ptrArrayEmpty( parentStack ) )
@ -387,23 +387,6 @@ tr_bencDictFindType( tr_benc * val, const char * key, int type )
return ret && ret->type == type ? ret : NULL;
}
tr_benc *
tr_bencDictFindFirst( tr_benc * val, ... )
{
const char * key;
tr_benc * ret;
va_list ap;
ret = NULL;
va_start( ap, val );
while(( key = va_arg( ap, const char * )))
if(( ret = tr_bencDictFind( val, key )))
break;
va_end( ap );
return ret;
}
int
tr_bencListSize( const tr_benc * list )
{
@ -514,37 +497,20 @@ tr_bencDictFindRaw( tr_benc * dict,
****
***/
void
_tr_bencInitStr( tr_benc * val, char * str, int len, int nofree )
{
tr_bencInit( val, TYPE_STR );
val->val.s.s = str;
val->val.s.nofree = nofree;
if( 0 >= len )
{
len = ( NULL == str ? 0 : strlen( str ) );
}
val->val.s.i = len;
}
void
tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount )
{
tr_bencInit( val, TYPE_STR );
val->val.s.i = byteCount;
val->val.s.s = tr_memdup( src, byteCount );
val->val.s.nofree = 0;
}
int
tr_bencInitStrDup( tr_benc * val, const char * str )
void
tr_bencInitStr( tr_benc * val, const void * str, int len )
{
char * newStr = tr_strdup( str );
if( newStr == NULL )
return 1;
_tr_bencInitStr( val, newStr, 0, 0 );
return 0;
tr_bencInit( val, TYPE_STR );
val->val.s.s = tr_strndup( str, len );
val->val.s.i = val->val.s.s ? strlen( val->val.s.s ) : 0;
}
void
@ -611,7 +577,7 @@ tr_benc *
tr_bencListAddStr( tr_benc * list, const char * val )
{
tr_benc * node = tr_bencListAdd( list );
tr_bencInitStrDup( node, val );
tr_bencInitStr( node, val, -1 );
return node;
}
tr_benc*
@ -640,7 +606,7 @@ tr_bencDictAdd( tr_benc * dict, const char * key )
assert( dict->val.l.count + 2 <= dict->val.l.alloc );
keyval = dict->val.l.vals + dict->val.l.count++;
tr_bencInitStrDup( keyval, key );
tr_bencInitStr( keyval, key, -1 );
itemval = dict->val.l.vals + dict->val.l.count++;
tr_bencInit( itemval, TYPE_INT );
@ -658,7 +624,7 @@ tr_benc*
tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val )
{
tr_benc * child = tr_bencDictAdd( dict, key );
tr_bencInitStrDup( child, val );
tr_bencInitStr( child, val, -1 );
return child;
}
tr_benc*
@ -962,8 +928,7 @@ freeDummyFunc( const tr_benc * val UNUSED, void * buf UNUSED )
static void
freeStringFunc( const tr_benc * val, void * freeme )
{
if( !val->val.s.nofree )
tr_ptrArrayAppend( freeme, val->val.s.s );
tr_ptrArrayAppend( freeme, val->val.s.s );
}
static void
freeContainerBeginFunc( const tr_benc * val, void * freeme )
@ -993,6 +958,7 @@ tr_bencFree( tr_benc * val )
****
***/
#if 0
struct WalkPrint
{
int depth;
@ -1069,6 +1035,7 @@ tr_bencPrint( const tr_benc * val )
walkPrint.depth = 0;
bencWalk( val, &walkFuncs, &walkPrint );
}
#endif
/***
****
@ -1300,6 +1267,20 @@ tr_bencSaveFile( const char * filename, const tr_benc * b )
return err;
}
int
tr_bencSaveJSONFile( const char * filename, const tr_benc * b )
{
int len;
char * content = tr_bencSaveAsJSON( b, &len );
const int err = saveFile( filename, content, len );
tr_free( content );
return err;
}
/***
****
***/
int
tr_bencLoadFile( const char * filename, tr_benc * b )
{
@ -1312,16 +1293,6 @@ tr_bencLoadFile( const char * filename, tr_benc * b )
return ret;
}
int
tr_bencSaveJSONFile( const char * filename, const tr_benc * b )
{
int len;
char * content = tr_bencSaveAsJSON( b, &len );
const int err = saveFile( filename, content, len );
tr_free( content );
return err;
}
int
tr_bencLoadJSONFile( const char * filename, tr_benc * b )
{

View File

@ -1,39 +1,30 @@
/******************************************************************************
/*
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
*
* This file is licensed by the GPL version 2. Works owned by the
* Transmission project are granted a special exemption to clause 2(b)
* so that the bulk of its code can remain under the MIT license.
* This exemption does not extend to derived works not owned by
* the Transmission project.
*
* $Id$
*
* Copyright (c) 2005-2008 Transmission authors and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
*/
#ifndef TR_BENCODE_H
#define TR_BENCODE_H 1
#include <inttypes.h> /* for int64_t */
#include <string.h> /* for memset */
enum
{
TYPE_INT = 1,
TYPE_STR = 2,
TYPE_LIST = 4,
TYPE_DICT = 8
};
typedef struct tr_benc
{
#define TYPE_INT 1
#define TYPE_STR 2
#define TYPE_LIST 4
#define TYPE_DICT 8
char type;
union
{
@ -41,7 +32,6 @@ typedef struct tr_benc
struct
{
int i;
int nofree;
char * s;
} s;
struct
@ -53,8 +43,9 @@ typedef struct tr_benc
} val;
} tr_benc;
/* backwards compatability */
typedef tr_benc benc_val_t;
/***
****
***/
int tr_bencParse( const void * buf,
const void * bufend,
@ -66,60 +57,75 @@ int tr_bencLoad( const void * buf,
tr_benc * setme_benc,
char ** setme_end );
void tr_bencPrint( const tr_benc * );
void tr_bencFree( tr_benc * );
int tr_bencDictFindInt( tr_benc * dict, const char * key, int64_t * setme );
int tr_bencDictFindDouble( tr_benc * dict, const char * key, double * setme );
int tr_bencDictFindStr( tr_benc * dict, const char * key, const char ** setme );
int tr_bencDictFindRaw( tr_benc * dict, const char * key, const uint8_t ** setme_raw,
size_t * setme_len );
int tr_bencDictFindList( tr_benc * dict, const char * key, tr_benc ** setme );
int tr_bencDictFindDict( tr_benc * dict, const char * key, tr_benc ** setme );
tr_benc * tr_bencDictFind( tr_benc * dict, const char * key );
tr_benc * tr_bencDictFindFirst( tr_benc * dict, ... );
int tr_bencLoadFile ( const char * filename, tr_benc * );
int tr_bencLoadJSONFile ( const char * filename, tr_benc * );
/* convenience functions for building tr_benc structures */
#if 0
void tr_bencPrint( const tr_benc * );
#endif
#define tr_bencInitStr( a, b, c, d ) \
_tr_bencInitStr( (a), ( char * )(b), (c), (d) )
void _tr_bencInitStr( tr_benc * val, char * str, int len, int nofree );
int tr_bencInitStrDup( tr_benc * val, const char * str );
void tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount );
void tr_bencInitInt( tr_benc * val, int64_t num );
int tr_bencInitDict( tr_benc * val, int reserveCount );
int tr_bencInitList( tr_benc * val, int reserveCount );
int tr_bencListReserve( tr_benc * list, int count );
/* note that for one key-value pair, count should be 1, not 2 */
int tr_bencDictReserve( tr_benc * dict, int count );
tr_benc * tr_bencListAdd( tr_benc * list );
tr_benc * tr_bencListAddInt( tr_benc * list, int64_t val );
tr_benc * tr_bencListAddStr( tr_benc * list, const char * val );
tr_benc * tr_bencListAddList( tr_benc * list, int reserveCount );
tr_benc * tr_bencListAddDict( tr_benc * list, int reserveCount );
tr_benc * tr_bencDictAdd( tr_benc * dict, const char * key );
tr_benc * tr_bencDictAddDouble( tr_benc * dict, const char * key, double d );
tr_benc * tr_bencDictAddInt( tr_benc * dict, const char * key, int64_t val );
tr_benc * tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val );
tr_benc * tr_bencDictAddList( tr_benc * dict, const char * key, int reserveCount );
tr_benc * tr_bencDictAddDict( tr_benc * dict, const char * key, int reserveCount );
tr_benc * tr_bencDictAddRaw( tr_benc * dict, const char * key, const void *, size_t len );
int tr_bencDictRemove( tr_benc * dict, const char * key );
void tr_bencFree( tr_benc * );
char* tr_bencSave ( const tr_benc * val, int * len );
char* tr_bencSaveAsJSON ( const tr_benc * top, int * len );
int tr_bencSaveFile ( const char * filename, const tr_benc * );
int tr_bencSaveJSONFile ( const char * filename, const tr_benc * );
int tr_bencLoadFile ( const char * filename, tr_benc * );
int tr_bencLoadJSONFile ( const char * filename, tr_benc * );
void tr_bencInitStr( tr_benc *, const void * str, int str_len );
void tr_bencInitRaw( tr_benc *, const void * raw, size_t raw_len );
void tr_bencInitInt( tr_benc *, int64_t num );
int tr_bencInitDict( tr_benc *, int reserveCount );
int tr_bencInitList( tr_benc *, int reserveCount );
/***
****
***/
int tr_bencListReserve( tr_benc *, int count );
tr_benc * tr_bencListAdd( tr_benc * );
tr_benc * tr_bencListAddInt( tr_benc *, int64_t val );
tr_benc * tr_bencListAddStr( tr_benc *, const char * val );
tr_benc * tr_bencListAddList( tr_benc *, int reserveCount );
tr_benc * tr_bencListAddDict( tr_benc *, int reserveCount );
int tr_bencListSize( const tr_benc * list );
tr_benc * tr_bencListChild( tr_benc * list, int n );
/***
****
***/
int tr_bencDictReserve( tr_benc *, int count );
int tr_bencDictRemove( tr_benc *, const char * key );
tr_benc * tr_bencDictAdd( tr_benc *, const char * key );
tr_benc * tr_bencDictAddDouble( tr_benc *, const char * key, double d );
tr_benc * tr_bencDictAddInt( tr_benc *, const char * key, int64_t val );
tr_benc * tr_bencDictAddStr( tr_benc *, const char * key, const char * val );
tr_benc * tr_bencDictAddList( tr_benc *, const char * key, int reserveCount );
tr_benc * tr_bencDictAddDict( tr_benc *, const char * key, int reserveCount );
tr_benc * tr_bencDictAddRaw( tr_benc *, const char * key,
const void *, size_t len );
tr_benc* tr_bencDictFind( tr_benc *, const char * key );
int tr_bencDictFindList( tr_benc *, const char * key, tr_benc ** setme );
int tr_bencDictFindDict( tr_benc *, const char * key, tr_benc ** setme );
int tr_bencDictFindInt( tr_benc *, const char * key, int64_t * setme );
int tr_bencDictFindDouble( tr_benc *, const char * key, double * setme );
int tr_bencDictFindStr( tr_benc *, const char * key, const char ** setme );
int tr_bencDictFindRaw( tr_benc *, const char * key,
const uint8_t ** setme_raw, size_t * setme_len );
/***
****
***/
int tr_bencGetInt( const tr_benc * val, int64_t * setme );
int tr_bencGetStr( const tr_benc * val, const char ** setme );
int tr_bencIsType( const tr_benc *, int type );
#define tr_bencIsInt(b) (tr_bencIsType(b,TYPE_INT))
#define tr_bencIsDict(b) (tr_bencIsType(b,TYPE_DICT))
#define tr_bencIsList(b) (tr_bencIsType(b,TYPE_LIST))
#define tr_bencIsString(b) (tr_bencIsType(b,TYPE_STR))
#define tr_bencIsInt( b ) tr_bencIsType( ( b ), TYPE_INT )
#define tr_bencIsDict( b ) tr_bencIsType( ( b ), TYPE_DICT )
#define tr_bencIsList( b ) tr_bencIsType( ( b ), TYPE_LIST )
#define tr_bencIsString( b ) tr_bencIsType( ( b ), TYPE_STR )
/**
*** Treat these as private -- they're only made public here
@ -137,12 +143,4 @@ int tr_bencParseStr( const uint8_t * buf,
uint8_t ** setme_str,
size_t * setme_strlen );
/**
***
**/
int tr_bencListSize( const tr_benc * list );
tr_benc * tr_bencListChild( tr_benc * list, int n );
#endif

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <string.h>
#include "transmission.h"
#include "bencode.h"
#include "json.h"

View File

@ -87,7 +87,7 @@ callback( void * vdata, int type, const JSON_value * value )
case JSON_T_FLOAT: {
char buf[128];
tr_snprintf( buf, sizeof( buf ), "%f", (double)value->vu.float_value );
tr_bencInitStrDup( getNode( data ), buf );
tr_bencInitStr( getNode( data ), buf, -1 );
break;
}
@ -107,7 +107,7 @@ callback( void * vdata, int type, const JSON_value * value )
break;
case JSON_T_STRING:
tr_bencInitStrDup( getNode( data ), value->vu.str.value );
tr_bencInitStr( getNode( data ), value->vu.str.value, value->vu.str.length );
break;
case JSON_T_KEY:

View File

@ -14,6 +14,7 @@
#include <errno.h>
#include <stdio.h> /* FILE, stderr */
#include <stdlib.h> /* qsort */
#include <string.h> /* strcmp, strlen */
#include <sys/types.h>
#include <sys/stat.h>

View File

@ -25,6 +25,7 @@
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
@ -294,7 +295,7 @@ getannounce( tr_info * inf, tr_benc * meta )
static void
geturllist( tr_info * inf, tr_benc * meta )
{
benc_val_t * urls;
tr_benc * urls;
if( tr_bencDictFindList( meta, "url-list", &urls ) )
{

View File

@ -1858,7 +1858,7 @@ sendPex( tr_peermsgs * msgs )
tr_pex * newPex = NULL;
const int newCount = tr_peerMgrGetPeers( msgs->session->peerMgr, msgs->torrent->info.hash, &newPex );
PexDiffs diffs;
tr_benc val, *added, *dropped;
tr_benc val;
uint8_t *tmp, *walk;
char * benc;
int bencLen;
@ -1886,14 +1886,14 @@ sendPex( tr_peermsgs * msgs )
tr_bencInitDict( &val, 3 );
/* "added" */
added = tr_bencDictAdd( &val, "added" );
tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 );
for( i=0; i<diffs.addedCount; ++i ) {
memcpy( walk, &diffs.added[i].in_addr, 4 ); walk += 4;
memcpy( walk, &diffs.added[i].port, 2 ); walk += 2;
}
assert( ( walk - tmp ) == diffs.addedCount * 6 );
tr_bencInitStr( added, tmp, walk-tmp, FALSE );
tr_bencDictAddRaw( &val, "added", tmp, walk-tmp );
tr_free( tmp );
/* "added.f" */
tmp = walk = tr_new( uint8_t, diffs.addedCount );
@ -1904,14 +1904,14 @@ sendPex( tr_peermsgs * msgs )
tr_free( tmp );
/* "dropped" */
dropped = tr_bencDictAdd( &val, "dropped" );
tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 );
for( i=0; i<diffs.droppedCount; ++i ) {
memcpy( walk, &diffs.dropped[i].in_addr, 4 ); walk += 4;
memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2;
}
assert( ( walk - tmp ) == diffs.droppedCount * 6 );
tr_bencInitStr( dropped, tmp, walk-tmp, FALSE );
tr_bencDictAddRaw( &val, "dropped", tmp, walk-tmp );
tr_free( tmp );
/* write the pex message */
benc = tr_bencSave( &val, &bencLen );

View File

@ -777,7 +777,7 @@ tr_rpc_parse_list_str( tr_benc * setme,
if( isNum )
tr_bencInitInt( setme, strtol( str, NULL, 10 ) );
else if( !isNumList )
tr_bencInitStrDup( setme, str );
tr_bencInitStr( setme, str, len );
else {
tr_bencInitList( setme, commaCount + 1 );
walk = str;

View File

@ -108,12 +108,13 @@ tr_ctorSetMetainfoFromFile( tr_ctor * ctor,
if( ctor->isSet_metainfo ) {
tr_benc * info;
if( tr_bencDictFindDict( &ctor->metainfo, "info", &info ) ) {
tr_benc * name = tr_bencDictFindFirst( info, "name.utf-8", "name", NULL );
if( name == NULL )
name = tr_bencDictAdd( info, "name" );
if( name->type!=TYPE_STR || !name->val.s.s || !*name->val.s.s ) {
const char * name;
if( !tr_bencDictFindStr( info, "name.utf-8", &name ) )
if( !tr_bencDictFindStr( info, "name", &name ) )
name = NULL;
if( !name || !*name ) {
char * tmp = tr_strdup( filename );
tr_bencInitStrDup( name, basename( tmp ) );
tr_bencDictAddStr( info, "name", basename( tmp ) );
tr_free( tmp );
}
}