transmission/libtransmission/bencode.c

506 lines
12 KiB
C
Raw Normal View History

2006-07-16 19:39:23 +00:00
/******************************************************************************
* $Id$
*
2008-01-01 17:20:20 +00:00
* Copyright (c) 2005-2008 Transmission authors and contributors
2006-07-16 19:39:23 +00:00
*
* 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.
*****************************************************************************/
2007-11-09 20:07:52 +00:00
#include <assert.h>
2007-07-12 17:51:45 +00:00
#include <ctype.h> /* isdigit, isprint */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <event.h>
2006-07-16 19:39:23 +00:00
#include "transmission.h"
2007-07-09 20:10:42 +00:00
#include "bencode.h"
#include "utils.h"
2006-07-16 19:39:23 +00:00
/**
***
**/
static int
tr_bencIsInt ( const benc_val_t * val ) {
return val!=NULL && val->type==TYPE_INT;
}
static int
tr_bencIsList( const benc_val_t * val ) {
return val!=NULL && val->type==TYPE_LIST;
}
static int
tr_bencIsDict( const benc_val_t * val ) {
return val!=NULL && val->type==TYPE_DICT;
}
/**
***
**/
/* setting to 1 to help expose bugs with tr_bencListAdd and tr_bencDictAdd */
#define LIST_SIZE 20 /* number of items to increment list/dict buffer by */
2006-07-16 19:39:23 +00:00
2007-03-23 08:28:01 +00:00
static int makeroom( benc_val_t * val, int count )
{
assert( TYPE_LIST == val->type || TYPE_DICT == val->type );
if( val->val.l.count + count > val->val.l.alloc )
2007-03-23 08:28:01 +00:00
{
/* We need a bigger boat */
const int len = val->val.l.alloc + count +
( count % LIST_SIZE ? LIST_SIZE - ( count % LIST_SIZE ) : 0 );
void * new = realloc( val->val.l.vals, len * sizeof( benc_val_t ) );
if( NULL == new )
return 1;
2007-03-23 08:28:01 +00:00
val->val.l.alloc = len;
val->val.l.vals = new;
2007-03-23 08:28:01 +00:00
}
return 0;
}
2006-09-25 18:37:45 +00:00
int _tr_bencLoad( char * buf, int len, benc_val_t * val, char ** end )
2006-07-16 19:39:23 +00:00
{
char * p, * e, * foo;
if( !end )
{
/* So we only have to check once */
end = &foo;
}
for( ;; )
2006-07-16 19:39:23 +00:00
{
if( !buf || len<1 ) /* no more text to parse... */
return 1;
if( *buf=='i' ) /* Integer: i1234e */
2006-07-16 19:39:23 +00:00
{
int64_t num;
2006-07-16 19:39:23 +00:00
e = memchr( &buf[1], 'e', len - 1 );
if( !e )
return 1;
2006-07-16 19:39:23 +00:00
*e = '\0';
num = strtoll( &buf[1], &p, 10 );
*e = 'e';
2006-07-16 19:39:23 +00:00
if( p != e )
return 1;
tr_bencInitInt( val, num );
*end = p + 1;
break;
}
else if( *buf=='l' || *buf=='d' )
2006-07-16 19:39:23 +00:00
{
/* List: l<item1><item2>e
Dict: d<string1><item1><string2><item2>e
A dictionary is just a special kind of list with an even
count of items, and where even items are strings. */
char * cur;
char is_dict;
char str_expected;
is_dict = ( buf[0] == 'd' );
cur = &buf[1];
str_expected = 1;
tr_bencInit( val, ( is_dict ? TYPE_DICT : TYPE_LIST ) );
while( cur - buf < len && cur[0] != 'e' )
2006-07-16 19:39:23 +00:00
{
if( makeroom( val, 1 ) ||
tr_bencLoad( cur, len - (cur - buf),
&val->val.l.vals[val->val.l.count], &p ) )
{
tr_bencFree( val );
return 1;
}
val->val.l.count++;
if( is_dict && str_expected &&
val->val.l.vals[val->val.l.count - 1].type != TYPE_STR )
{
tr_bencFree( val );
return 1;
}
str_expected = !str_expected;
cur = p;
2006-07-16 19:39:23 +00:00
}
if( is_dict && ( val->val.l.count & 1 ) )
2006-07-16 19:39:23 +00:00
{
tr_bencFree( val );
return 1;
}
*end = cur + 1;
break;
2006-07-16 19:39:23 +00:00
}
else if( isdigit(*buf) )
2006-07-16 19:39:23 +00:00
{
int slen;
char * sbuf;
2006-07-16 19:39:23 +00:00
e = memchr( buf, ':', len );
if( NULL == e )
{
return 1;
}
/* String: 12:whateverword */
e[0] = '\0';
slen = strtol( buf, &p, 10 );
e[0] = ':';
2006-07-16 19:39:23 +00:00
if( p != e || 0 > slen || len - ( ( p + 1 ) - buf ) < slen )
{
return 1;
}
sbuf = malloc( slen + 1 );
if( NULL == sbuf )
{
return 1;
}
2006-07-16 19:39:23 +00:00
memcpy( sbuf, p + 1, slen );
sbuf[slen] = '\0';
tr_bencInitStr( val, sbuf, slen, 0 );
*end = p + 1 + val->val.s.i;
break;
}
else /* invalid bencoded text... march past it */
2006-07-16 19:39:23 +00:00
{
++buf;
--len;
2006-07-16 19:39:23 +00:00
}
}
return 0;
}
static void __bencPrint( benc_val_t * val, int space )
{
int ii;
2006-07-16 19:39:23 +00:00
for( ii = 0; ii < space; ii++ )
2006-07-16 19:39:23 +00:00
{
putc( ' ', stderr );
2006-07-16 19:39:23 +00:00
}
switch( val->type )
{
case TYPE_INT:
fprintf( stderr, "int: %"PRId64"\n", tr_bencGetInt(val) );
2006-07-16 19:39:23 +00:00
break;
case TYPE_STR:
for( ii = 0; val->val.s.i > ii; ii++ )
{
if( '\\' == val->val.s.s[ii] )
{
putc( '\\', stderr );
putc( '\\', stderr );
}
else if( isprint( val->val.s.s[ii] ) )
{
putc( val->val.s.s[ii], stderr );
}
else
{
fprintf( stderr, "\\x%02x", val->val.s.s[ii] );
}
}
putc( '\n', stderr );
2006-07-16 19:39:23 +00:00
break;
case TYPE_LIST:
fprintf( stderr, "list\n" );
for( ii = 0; ii < val->val.l.count; ii++ )
{
__bencPrint( &val->val.l.vals[ii], space + 1 );
}
2006-07-16 19:39:23 +00:00
break;
case TYPE_DICT:
fprintf( stderr, "dict\n" );
for( ii = 0; ii < val->val.l.count; ii++ )
{
__bencPrint( &val->val.l.vals[ii], space + 1 );
}
2006-07-16 19:39:23 +00:00
break;
}
}
void tr_bencPrint( benc_val_t * val )
{
__bencPrint( val, 0 );
}
void tr_bencFree( benc_val_t * val )
{
int i;
switch( val->type )
{
case TYPE_INT:
break;
case TYPE_STR:
2007-03-23 08:28:01 +00:00
if( !val->val.s.nofree )
2006-07-16 19:39:23 +00:00
{
free( val->val.s.s );
}
break;
case TYPE_LIST:
case TYPE_DICT:
for( i = 0; i < val->val.l.count; i++ )
{
tr_bencFree( &val->val.l.vals[i] );
}
free( val->val.l.vals );
break;
}
}
benc_val_t * tr_bencDictFind( benc_val_t * val, const char * key )
2006-07-16 19:39:23 +00:00
{
int len, ii;
2006-07-16 19:39:23 +00:00
if( val->type != TYPE_DICT )
{
return NULL;
}
len = strlen( key );
2006-07-16 19:39:23 +00:00
for( ii = 0; ii + 1 < val->val.l.count; ii += 2 )
2006-07-16 19:39:23 +00:00
{
if( TYPE_STR != val->val.l.vals[ii].type ||
len != val->val.l.vals[ii].val.s.i ||
0 != memcmp( val->val.l.vals[ii].val.s.s, key, len ) )
{
continue;
}
return &val->val.l.vals[ii+1];
2006-07-16 19:39:23 +00:00
}
return NULL;
}
benc_val_t * tr_bencDictFindFirst( benc_val_t * val, ... )
{
const char * key;
benc_val_t * ret;
va_list ap;
2007-04-18 16:41:37 +00:00
ret = NULL;
va_start( ap, val );
while( ( key = va_arg( ap, const char * ) ) )
{
ret = tr_bencDictFind( val, key );
if( NULL != ret )
{
break;
}
}
va_end( ap );
return ret;
}
2007-03-23 08:28:01 +00:00
char * tr_bencStealStr( benc_val_t * val )
{
assert( TYPE_STR == val->type );
val->val.s.nofree = 1;
return val->val.s.s;
}
void _tr_bencInitStr( benc_val_t * 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;
}
int tr_bencInitStrDup( benc_val_t * val, const char * str )
{
char * newStr = tr_strdup( str );
if( newStr == NULL )
return 1;
2007-03-23 08:28:01 +00:00
_tr_bencInitStr( val, newStr, 0, 0 );
2007-03-23 08:28:01 +00:00
return 0;
}
void tr_bencInitInt( benc_val_t * val, int64_t num )
{
tr_bencInit( val, TYPE_INT );
val->val.i = num;
}
int tr_bencListReserve( benc_val_t * val, int count )
2007-03-23 08:28:01 +00:00
{
assert( TYPE_LIST == val->type );
return makeroom( val, count );
2007-03-23 08:28:01 +00:00
}
int tr_bencDictReserve( benc_val_t * val, int count )
2007-03-23 08:28:01 +00:00
{
assert( TYPE_DICT == val->type );
2007-03-23 08:28:01 +00:00
return makeroom( val, count * 2 );
2007-03-23 08:28:01 +00:00
}
benc_val_t * tr_bencListAdd( benc_val_t * list )
2007-03-23 08:28:01 +00:00
{
benc_val_t * item;
2007-03-23 08:28:01 +00:00
assert( tr_bencIsList( list ) );
assert( list->val.l.count < list->val.l.alloc );
2007-03-23 08:28:01 +00:00
item = &list->val.l.vals[list->val.l.count];
list->val.l.count++;
tr_bencInit( item, TYPE_INT );
2007-03-23 08:28:01 +00:00
return item;
2007-03-23 08:28:01 +00:00
}
benc_val_t * tr_bencDictAdd( benc_val_t * dict, const char * key )
2007-03-23 08:28:01 +00:00
{
benc_val_t * keyval, * itemval;
2007-03-23 08:28:01 +00:00
assert( tr_bencIsDict( dict ) );
assert( dict->val.l.count + 2 <= dict->val.l.alloc );
2007-03-23 08:28:01 +00:00
keyval = dict->val.l.vals + dict->val.l.count++;
tr_bencInitStr( keyval, (char*)key, -1, 1 );
itemval = dict->val.l.vals + dict->val.l.count++;
tr_bencInit( itemval, TYPE_INT );
2007-03-23 08:28:01 +00:00
return itemval;
2007-03-23 08:28:01 +00:00
}
struct KeyIndex
{
const char * key;
int index;
};
static int compareKeyIndex( const void * va, const void * vb )
{
const struct KeyIndex * a = va;
const struct KeyIndex * b = vb;
return strcmp( a->key, b->key );
}
static void
saveImpl( struct evbuffer * out, const benc_val_t * val )
2006-07-16 19:39:23 +00:00
{
int ii;
2006-07-16 19:39:23 +00:00
switch( val->type )
{
case TYPE_INT:
evbuffer_add_printf( out, "i%"PRId64"e", tr_bencGetInt(val) );
2006-07-16 19:39:23 +00:00
break;
case TYPE_STR:
2007-11-16 06:37:01 +00:00
evbuffer_add_printf( out, "%i:", val->val.s.i );
evbuffer_add( out, val->val.s.s, val->val.s.i );
2006-07-16 19:39:23 +00:00
break;
case TYPE_LIST:
evbuffer_add_printf( out, "l" );
2006-07-16 19:39:23 +00:00
for( ii = 0; val->val.l.count > ii; ii++ )
saveImpl( out, val->val.l.vals + ii );
evbuffer_add_printf( out, "e" );
break;
case TYPE_DICT:
/* Keys must be strings and appear in sorted order
(sorted as raw strings, not alphanumerics). */
evbuffer_add_printf( out, "d" );
if( 1 ) {
int i;
struct KeyIndex * indices = tr_new( struct KeyIndex, val->val.l.count );
for( ii=i=0; i<val->val.l.count; i+=2 ) {
indices[ii].key = val->val.l.vals[i].val.s.s;
indices[ii].index = i;
ii++;
2006-07-16 19:39:23 +00:00
}
qsort( indices, ii, sizeof(struct KeyIndex), compareKeyIndex );
for( i=0; i<ii; ++i ) {
const int index = indices[i].index;
saveImpl( out, val->val.l.vals + index );
saveImpl( out, val->val.l.vals + index + 1 );
}
tr_free( indices );
}
evbuffer_add_printf( out, "e" );
2006-07-16 19:39:23 +00:00
break;
}
}
2006-07-16 19:39:23 +00:00
char*
tr_bencSave( const benc_val_t * val, int * len )
{
struct evbuffer * buf = evbuffer_new( );
char * ret;
saveImpl( buf, val );
if( len != NULL )
*len = EVBUFFER_LENGTH( buf );
ret = tr_strndup( (char*) EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
evbuffer_free( buf );
return ret;
2006-07-16 19:39:23 +00:00
}
/**
***
**/
benc_val_t*
tr_bencDictFindType( benc_val_t * val, const char * key, int type )
{
benc_val_t * ret = tr_bencDictFind( val, key );
return ret && ret->type == type ? ret : NULL;
}
int64_t
tr_bencGetInt ( const benc_val_t * val )
{
assert( tr_bencIsInt( val ) );
return val->val.i;
}