2008-07-22 23:28:28 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2008-07-22 23:28:28 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-07-22 23:28:28 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2008-07-22 23:28:28 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2008-05-22 16:41:16 +00:00
|
|
|
#include <ctype.h> /* isdigit, isprint, isspace */
|
2008-01-30 15:39:41 +00:00
|
|
|
#include <errno.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2008-08-21 14:57:59 +00:00
|
|
|
#include <string.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2008-02-01 01:54:04 +00:00
|
|
|
#include <event.h> /* evbuffer */
|
2007-11-06 21:14:30 +00:00
|
|
|
|
2008-08-19 23:11:35 +00:00
|
|
|
#include "ConvertUTF.h"
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "bencode.h"
|
2008-06-11 20:09:36 +00:00
|
|
|
#include "json.h"
|
2008-03-04 19:29:51 +00:00
|
|
|
#include "list.h"
|
2008-01-31 02:24:43 +00:00
|
|
|
#include "ptrarray.h"
|
2008-02-01 01:54:04 +00:00
|
|
|
#include "utils.h" /* tr_new(), tr_free() */
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-04-04 17:19:44 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencIsType( const tr_benc * val,
|
|
|
|
int type )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
return ( val ) && ( val->type == type );
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
2008-01-31 15:20:42 +00:00
|
|
|
static int
|
2008-02-26 21:58:58 +00:00
|
|
|
isContainer( const tr_benc * val )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
return tr_bencIsList( val ) || tr_bencIsDict( val );
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static int
|
2008-02-26 21:58:58 +00:00
|
|
|
isSomething( const tr_benc * val )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
return isContainer( val ) || tr_bencIsInt( val ) || tr_bencIsString(
|
|
|
|
val );
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
|
|
|
|
2008-06-19 02:53:57 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencInit( tr_benc * val,
|
|
|
|
int type )
|
2008-06-19 02:53:57 +00:00
|
|
|
{
|
|
|
|
memset( val, 0, sizeof( *val ) );
|
|
|
|
val->type = type;
|
|
|
|
}
|
|
|
|
|
2008-02-02 16:23:49 +00:00
|
|
|
/***
|
|
|
|
**** tr_bencParse()
|
|
|
|
**** tr_bencLoad()
|
|
|
|
***/
|
2007-11-21 20:03:53 +00:00
|
|
|
|
2008-01-30 15:39:41 +00:00
|
|
|
/**
|
|
|
|
* The initial i and trailing e are beginning and ending delimiters.
|
|
|
|
* You can have negative numbers such as i-3e. You cannot prefix the
|
2008-09-23 19:11:04 +00:00
|
|
|
* number with a zero such as i04e. However, i0e is valid.
|
|
|
|
* Example: i3e represents the integer "3"
|
2008-01-30 15:39:41 +00:00
|
|
|
* NOTE: The maximum number of bit of this integer is unspecified,
|
|
|
|
* but to handle it as a signed 64bit integer is mandatory to handle
|
2008-09-23 19:11:04 +00:00
|
|
|
* "large files" aka .torrent for more that 4Gbyte
|
2008-01-30 15:39:41 +00:00
|
|
|
*/
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencParseInt( const uint8_t * buf,
|
|
|
|
const uint8_t * bufend,
|
|
|
|
const uint8_t ** setme_end,
|
|
|
|
int64_t * setme_val )
|
2008-01-30 15:39:41 +00:00
|
|
|
{
|
2008-10-03 04:49:06 +00:00
|
|
|
int err = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
char * endptr;
|
2008-01-30 15:39:41 +00:00
|
|
|
const void * begin;
|
|
|
|
const void * end;
|
2008-09-23 19:11:04 +00:00
|
|
|
int64_t val;
|
2008-01-30 15:39:41 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
if( buf >= bufend )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2008-01-30 15:39:41 +00:00
|
|
|
if( *buf != 'i' )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2008-01-30 15:39:41 +00:00
|
|
|
|
|
|
|
begin = buf + 1;
|
2008-09-23 19:11:04 +00:00
|
|
|
end = memchr( begin, 'e', ( bufend - buf ) - 1 );
|
2008-03-02 19:55:04 +00:00
|
|
|
if( end == NULL )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2008-01-30 15:39:41 +00:00
|
|
|
|
|
|
|
errno = 0;
|
2008-10-14 01:00:15 +00:00
|
|
|
val = evutil_strtoll( begin, &endptr, 10 );
|
2008-03-02 19:55:04 +00:00
|
|
|
if( errno || ( endptr != end ) ) /* incomplete parse */
|
2008-10-03 04:49:06 +00:00
|
|
|
err = EILSEQ;
|
2008-09-23 19:11:04 +00:00
|
|
|
else if( val && *(const char*)begin == '0' ) /* no leading zeroes! */
|
2008-10-03 04:49:06 +00:00
|
|
|
err = EILSEQ;
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
2008-10-13 22:26:02 +00:00
|
|
|
*setme_end = (const uint8_t*)end + 1;
|
2008-01-30 15:39:41 +00:00
|
|
|
*setme_val = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Byte strings are encoded as follows:
|
|
|
|
* <string length encoded in base ten ASCII>:<string data>
|
|
|
|
* Note that there is no constant beginning delimiter, and no ending delimiter.
|
2008-09-23 19:11:04 +00:00
|
|
|
* Example: 4:spam represents the string "spam"
|
2008-01-30 15:39:41 +00:00
|
|
|
*/
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencParseStr( const uint8_t * buf,
|
|
|
|
const uint8_t * bufend,
|
2008-01-30 15:39:41 +00:00
|
|
|
const uint8_t ** setme_end,
|
2008-08-21 18:40:40 +00:00
|
|
|
const uint8_t ** setme_str,
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t * setme_strlen )
|
2008-01-30 15:39:41 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t len;
|
2008-01-30 15:39:41 +00:00
|
|
|
const void * end;
|
2008-09-23 19:11:04 +00:00
|
|
|
char * endptr;
|
2008-01-30 15:39:41 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
if( buf >= bufend )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2008-01-30 15:39:41 +00:00
|
|
|
|
|
|
|
if( !isdigit( *buf ) )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2008-01-30 15:39:41 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
end = memchr( buf, ':', bufend - buf );
|
2008-03-02 19:55:04 +00:00
|
|
|
if( end == NULL )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2008-01-30 15:39:41 +00:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
len = strtoul( (const char*)buf, &endptr, 10 );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( errno || endptr != end )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2008-01-30 15:39:41 +00:00
|
|
|
|
2008-03-02 19:55:04 +00:00
|
|
|
if( (const uint8_t*)end + 1 + len > bufend )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2008-01-30 15:39:41 +00:00
|
|
|
|
2008-10-13 22:26:02 +00:00
|
|
|
*setme_end = (const uint8_t*)end + 1 + len;
|
|
|
|
*setme_str = (const uint8_t*)end + 1;
|
2008-01-30 15:39:41 +00:00
|
|
|
*setme_strlen = len;
|
2008-10-03 04:49:06 +00:00
|
|
|
return 0;
|
2008-01-30 15:39:41 +00:00
|
|
|
}
|
|
|
|
|
2008-08-22 15:13:01 +00:00
|
|
|
/* set to 1 to help expose bugs with tr_bencListAdd and tr_bencDictAdd */
|
2008-01-31 02:24:43 +00:00
|
|
|
#define LIST_SIZE 8 /* number of items to increment list/dict buffer by */
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
makeroom( tr_benc * val,
|
|
|
|
size_t count )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
|
|
|
assert( TYPE_LIST == val->type || TYPE_DICT == val->type );
|
|
|
|
|
2007-06-19 01:49:56 +00:00
|
|
|
if( val->val.l.count + count > val->val.l.alloc )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
2007-06-19 01:49:56 +00:00
|
|
|
/* We need a bigger boat */
|
|
|
|
const int len = val->val.l.alloc + count +
|
2008-09-23 19:11:04 +00:00
|
|
|
( count % LIST_SIZE ? LIST_SIZE -
|
|
|
|
( count % LIST_SIZE ) : 0 );
|
|
|
|
void * new = realloc( val->val.l.vals, len * sizeof( tr_benc ) );
|
2007-06-19 01:49:56 +00:00
|
|
|
if( NULL == new )
|
|
|
|
return 1;
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2007-06-19 01:49:56 +00:00
|
|
|
val->val.l.alloc = len;
|
|
|
|
val->val.l.vals = new;
|
2007-03-23 08:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
static tr_benc*
|
2008-09-23 19:11:04 +00:00
|
|
|
getNode( tr_benc * top,
|
|
|
|
tr_ptrArray * parentStack,
|
|
|
|
int type )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * parent;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( top );
|
|
|
|
assert( parentStack );
|
2008-01-31 02:24:43 +00:00
|
|
|
|
|
|
|
if( tr_ptrArrayEmpty( parentStack ) )
|
|
|
|
return top;
|
|
|
|
|
|
|
|
parent = tr_ptrArrayBack( parentStack );
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( parent );
|
2008-01-31 02:24:43 +00:00
|
|
|
|
|
|
|
/* dictionary keys must be strings */
|
|
|
|
if( ( parent->type == TYPE_DICT )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& ( type != TYPE_STR )
|
|
|
|
&& ( !( parent->val.l.count % 2 ) ) )
|
2008-01-31 02:24:43 +00:00
|
|
|
return NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
makeroom( parent, 1 );
|
|
|
|
return parent->val.l.vals + parent->val.l.count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-01 01:54:04 +00:00
|
|
|
* This function's previous recursive implementation was
|
|
|
|
* easier to read, but was vulnerable to a smash-stacking
|
|
|
|
* attack via maliciously-crafted bencoded data. (#667)
|
2008-01-31 02:24:43 +00:00
|
|
|
*/
|
2008-08-11 17:00:23 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencParseImpl( const void * buf_in,
|
|
|
|
const void * bufend_in,
|
|
|
|
tr_benc * top,
|
|
|
|
tr_ptrArray * parentStack,
|
2008-08-11 17:00:23 +00:00
|
|
|
const uint8_t ** setme_end )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int err;
|
2008-01-31 02:24:43 +00:00
|
|
|
const uint8_t * buf = buf_in;
|
|
|
|
const uint8_t * bufend = bufend_in;
|
|
|
|
|
2008-02-01 15:50:17 +00:00
|
|
|
tr_bencInit( top, 0 );
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
while( buf != bufend )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-01-31 02:24:43 +00:00
|
|
|
if( buf > bufend ) /* no more text to parse... */
|
2007-07-06 20:43:17 +00:00
|
|
|
return 1;
|
2007-03-30 00:12:39 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( *buf == 'i' ) /* int */
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int64_t val;
|
2008-01-31 02:24:43 +00:00
|
|
|
const uint8_t * end;
|
2008-09-23 19:11:04 +00:00
|
|
|
int err;
|
|
|
|
tr_benc * node;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( err = tr_bencParseInt( buf, bufend, &end, &val ) ) )
|
2008-01-31 02:24:43 +00:00
|
|
|
return err;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
node = getNode( top, parentStack, TYPE_INT );
|
|
|
|
if( !node )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
tr_bencInitInt( node, val );
|
|
|
|
buf = end;
|
2007-07-06 20:43:17 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
if( tr_ptrArrayEmpty( parentStack ) )
|
|
|
|
break;
|
2007-07-06 20:43:17 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else if( *buf == 'l' ) /* list */
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * node = getNode( top, parentStack, TYPE_LIST );
|
2008-01-31 02:24:43 +00:00
|
|
|
if( !node )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2008-01-31 02:24:43 +00:00
|
|
|
tr_bencInit( node, TYPE_LIST );
|
|
|
|
tr_ptrArrayAppend( parentStack, node );
|
|
|
|
++buf;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else if( *buf == 'd' ) /* dict */
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * node = getNode( top, parentStack, TYPE_DICT );
|
2008-01-31 02:24:43 +00:00
|
|
|
if( !node )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2008-01-31 02:24:43 +00:00
|
|
|
tr_bencInit( node, TYPE_DICT );
|
|
|
|
tr_ptrArrayAppend( parentStack, node );
|
|
|
|
++buf;
|
2007-07-06 20:43:17 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else if( *buf == 'e' ) /* end of list or dict */
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * node;
|
2007-07-06 20:43:17 +00:00
|
|
|
++buf;
|
2008-01-31 02:24:43 +00:00
|
|
|
if( tr_ptrArrayEmpty( parentStack ) )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2008-02-01 15:50:17 +00:00
|
|
|
|
|
|
|
node = tr_ptrArrayBack( parentStack );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencIsDict( node ) && ( node->val.l.count % 2 ) )
|
|
|
|
{
|
2008-08-11 17:00:23 +00:00
|
|
|
/* odd # of children in dict */
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencFree( &node->val.l.vals[--node->val.l.count] );
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2008-08-11 17:00:23 +00:00
|
|
|
}
|
2008-02-01 15:50:17 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
tr_ptrArrayPop( parentStack );
|
|
|
|
if( tr_ptrArrayEmpty( parentStack ) )
|
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else if( isdigit( *buf ) ) /* string? */
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
|
|
|
const uint8_t * end;
|
2008-08-21 18:40:40 +00:00
|
|
|
const uint8_t * str;
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t str_len;
|
|
|
|
int err;
|
|
|
|
tr_benc * node;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( err = tr_bencParseStr( buf, bufend, &end, &str, &str_len ) ) )
|
2008-01-31 02:24:43 +00:00
|
|
|
return err;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
node = getNode( top, parentStack, TYPE_STR );
|
2008-08-21 18:40:40 +00:00
|
|
|
if( !node )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_bencInitStr( node, str, str_len );
|
2008-01-31 02:24:43 +00:00
|
|
|
buf = end;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
if( tr_ptrArrayEmpty( parentStack ) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else /* invalid bencoded text... march past it */
|
|
|
|
{
|
|
|
|
++buf;
|
|
|
|
}
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-02-01 15:50:17 +00:00
|
|
|
err = !isSomething( top ) || !tr_ptrArrayEmpty( parentStack );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
if( !err && setme_end )
|
2008-01-31 02:24:43 +00:00
|
|
|
*setme_end = buf;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-08-11 17:00:23 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencParse( const void * buf,
|
|
|
|
const void * end,
|
|
|
|
tr_benc * top,
|
2008-08-11 17:00:23 +00:00
|
|
|
const uint8_t ** setme_end )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int err;
|
2008-08-11 17:00:23 +00:00
|
|
|
tr_ptrArray * parentStack = tr_ptrArrayNew( );
|
2008-08-11 19:05:02 +00:00
|
|
|
|
2008-08-22 15:13:01 +00:00
|
|
|
top->type = 0; /* set to `uninitialized' */
|
2008-08-11 19:05:02 +00:00
|
|
|
err = tr_bencParseImpl( buf, end, top, parentStack, setme_end );
|
2008-08-11 17:00:23 +00:00
|
|
|
if( err )
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencFree( top );
|
2008-08-11 19:05:02 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
tr_ptrArrayFree( parentStack, NULL );
|
|
|
|
return err;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencLoad( const void * buf_in,
|
|
|
|
size_t buflen,
|
|
|
|
tr_benc * setme_benc,
|
|
|
|
char ** setme_end )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-01-31 02:24:43 +00:00
|
|
|
const uint8_t * buf = buf_in;
|
|
|
|
const uint8_t * end;
|
2008-09-23 19:11:04 +00:00
|
|
|
const int ret = tr_bencParse( buf, buf + buflen, setme_benc, &end );
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
if( !ret && setme_end )
|
|
|
|
*setme_end = (char*) end;
|
|
|
|
return ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-02-02 16:23:49 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-06-01 01:40:32 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
dictIndexOf( const tr_benc * val,
|
|
|
|
const char * key )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-08-22 15:13:01 +00:00
|
|
|
if( tr_bencIsDict( val ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t i;
|
2008-09-04 14:42:32 +00:00
|
|
|
const size_t len = strlen( key );
|
2008-08-22 15:13:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; ( i + 1 ) < val->val.l.count; i += 2 )
|
2007-03-30 00:12:39 +00:00
|
|
|
{
|
2008-08-22 15:13:01 +00:00
|
|
|
const tr_benc * child = val->val.l.vals + i;
|
|
|
|
|
|
|
|
if( ( child->type == TYPE_STR )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& ( child->val.s.i == len )
|
|
|
|
&& !memcmp( child->val.s.s, key, len ) )
|
2008-08-22 15:13:01 +00:00
|
|
|
return i;
|
2007-03-30 00:12:39 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-06-01 01:40:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_benc *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictFind( tr_benc * val,
|
|
|
|
const char * key )
|
2008-06-01 01:40:32 +00:00
|
|
|
{
|
|
|
|
const int i = dictIndexOf( val, key );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
return i < 0 ? NULL : &val->val.l.vals[i + 1];
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 18:42:45 +00:00
|
|
|
static tr_benc*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictFindType( tr_benc * val,
|
|
|
|
const char * key,
|
|
|
|
int type )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * ret = tr_bencDictFind( val, key );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-22 15:13:01 +00:00
|
|
|
return ( ret && ( ret->type == type ) ) ? ret : NULL;
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
|
|
|
|
2008-09-04 14:42:32 +00:00
|
|
|
size_t
|
2008-05-08 03:25:21 +00:00
|
|
|
tr_bencListSize( const tr_benc * list )
|
|
|
|
{
|
|
|
|
return tr_bencIsList( list ) ? list->val.l.count : 0;
|
|
|
|
}
|
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencListChild( tr_benc * val,
|
|
|
|
size_t i )
|
2008-02-02 16:23:49 +00:00
|
|
|
{
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * ret = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-09-04 14:42:32 +00:00
|
|
|
if( tr_bencIsList( val ) && ( i < val->val.l.count ) )
|
2008-02-02 16:23:49 +00:00
|
|
|
ret = val->val.l.vals + i;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencGetInt( const tr_benc * val,
|
|
|
|
int64_t * setme )
|
2008-02-02 16:23:49 +00:00
|
|
|
{
|
2008-05-08 03:25:21 +00:00
|
|
|
const int success = tr_bencIsInt( val );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-08 03:25:21 +00:00
|
|
|
if( success )
|
2008-09-23 19:11:04 +00:00
|
|
|
*setme = val->val.i;
|
2008-05-08 03:25:21 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencGetStr( const tr_benc * val,
|
|
|
|
const char ** setme )
|
2008-05-08 03:25:21 +00:00
|
|
|
{
|
|
|
|
const int success = tr_bencIsString( val );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-08 03:25:21 +00:00
|
|
|
if( success )
|
|
|
|
*setme = val->val.s.s;
|
2008-04-13 22:31:07 +00:00
|
|
|
return success;
|
2008-02-02 16:23:49 +00:00
|
|
|
}
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictFindInt( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
int64_t * setme )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int found = FALSE;
|
2008-04-13 22:31:07 +00:00
|
|
|
tr_benc * child = tr_bencDictFindType( dict, key, TYPE_INT );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
if( child )
|
|
|
|
found = tr_bencGetInt( child, setme );
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2008-05-08 03:25:21 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictFindDouble( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
double * setme )
|
2008-05-08 03:25:21 +00:00
|
|
|
{
|
|
|
|
const char * str;
|
2008-09-23 19:11:04 +00:00
|
|
|
const int success = tr_bencDictFindStr( dict, key, &str );
|
|
|
|
|
2008-05-08 03:25:21 +00:00
|
|
|
if( success )
|
|
|
|
*setme = strtod( str, NULL );
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictFindList( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
tr_benc ** setme )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int found = FALSE;
|
2008-04-13 22:31:07 +00:00
|
|
|
tr_benc * child = tr_bencDictFindType( dict, key, TYPE_LIST );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
if( child )
|
|
|
|
{
|
2008-04-13 22:31:07 +00:00
|
|
|
*setme = child;
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictFindDict( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
tr_benc ** setme )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int found = FALSE;
|
2008-04-13 22:31:07 +00:00
|
|
|
tr_benc * child = tr_bencDictFindType( dict, key, TYPE_DICT );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
if( child )
|
|
|
|
{
|
2008-04-13 22:31:07 +00:00
|
|
|
*setme = child;
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictFindStr( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
const char ** setme )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int found = FALSE;
|
2008-04-13 22:31:07 +00:00
|
|
|
tr_benc * child = tr_bencDictFindType( dict, key, TYPE_STR );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
if( child )
|
|
|
|
{
|
2008-04-13 22:31:07 +00:00
|
|
|
*setme = child->val.s.s;
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2008-08-10 14:58:11 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictFindRaw( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
const uint8_t ** setme_raw,
|
|
|
|
size_t * setme_len )
|
2008-08-10 14:58:11 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int found = FALSE;
|
2008-08-10 14:58:11 +00:00
|
|
|
tr_benc * child = tr_bencDictFindType( dict, key, TYPE_STR );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
if( child )
|
|
|
|
{
|
2008-08-10 14:58:11 +00:00
|
|
|
*setme_raw = (uint8_t*) child->val.s.s;
|
|
|
|
*setme_len = child->val.s.i;
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2008-02-02 16:23:49 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-15 17:00:44 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencInitRaw( tr_benc * val,
|
|
|
|
const void * src,
|
|
|
|
size_t byteCount )
|
2008-04-15 17:00:44 +00:00
|
|
|
{
|
|
|
|
tr_bencInit( val, TYPE_STR );
|
|
|
|
val->val.s.i = byteCount;
|
2008-06-07 21:26:41 +00:00
|
|
|
val->val.s.s = tr_memdup( src, byteCount );
|
2008-04-15 17:00:44 +00:00
|
|
|
}
|
|
|
|
|
2008-08-21 14:57:59 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencInitStr( tr_benc * val,
|
|
|
|
const void * str,
|
|
|
|
int len )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_bencInit( val, TYPE_STR );
|
2008-08-21 17:03:44 +00:00
|
|
|
|
2008-08-21 14:57:59 +00:00
|
|
|
val->val.s.s = tr_strndup( str, len );
|
2008-08-21 17:03:44 +00:00
|
|
|
|
|
|
|
if( val->val.s.s == NULL )
|
|
|
|
val->val.s.i = 0;
|
|
|
|
else if( len < 0 )
|
|
|
|
val->val.s.i = strlen( val->val.s.s );
|
|
|
|
else
|
|
|
|
val->val.s.i = len;
|
2007-03-23 08:28:01 +00:00
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencInitInt( tr_benc * val,
|
|
|
|
int64_t num )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
|
|
|
tr_bencInit( val, TYPE_INT );
|
|
|
|
val->val.i = num;
|
|
|
|
}
|
|
|
|
|
2008-04-03 21:38:32 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencInitList( tr_benc * val,
|
|
|
|
size_t reserveCount )
|
2008-04-03 21:38:32 +00:00
|
|
|
{
|
|
|
|
tr_bencInit( val, TYPE_LIST );
|
|
|
|
return tr_bencListReserve( val, reserveCount );
|
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencListReserve( tr_benc * val,
|
|
|
|
size_t count )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
2008-04-04 17:19:44 +00:00
|
|
|
assert( tr_bencIsList( val ) );
|
2007-06-19 01:49:56 +00:00
|
|
|
return makeroom( val, count );
|
2007-03-23 08:28:01 +00:00
|
|
|
}
|
|
|
|
|
2008-04-03 21:38:32 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencInitDict( tr_benc * val,
|
|
|
|
size_t reserveCount )
|
2008-04-03 21:38:32 +00:00
|
|
|
{
|
|
|
|
tr_bencInit( val, TYPE_DICT );
|
|
|
|
return tr_bencDictReserve( val, reserveCount );
|
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictReserve( tr_benc * val,
|
|
|
|
size_t reserveCount )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
2008-04-04 17:19:44 +00:00
|
|
|
assert( tr_bencIsDict( val ) );
|
2008-09-04 14:42:32 +00:00
|
|
|
return makeroom( val, reserveCount * 2 );
|
2007-03-23 08:28:01 +00:00
|
|
|
}
|
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc *
|
|
|
|
tr_bencListAdd( tr_benc * list )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * item;
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2008-04-04 17:19:44 +00:00
|
|
|
assert( tr_bencIsList( list ) );
|
2008-04-13 22:31:07 +00:00
|
|
|
|
|
|
|
if( list->val.l.count == list->val.l.alloc )
|
|
|
|
tr_bencListReserve( list, LIST_SIZE );
|
|
|
|
|
2007-03-29 21:02:59 +00:00
|
|
|
assert( list->val.l.count < list->val.l.alloc );
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2007-03-29 21:02:59 +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
|
|
|
|
2007-03-29 21:02:59 +00:00
|
|
|
return item;
|
2007-03-23 08:28:01 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
tr_benc *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencListAddInt( tr_benc * list,
|
|
|
|
int64_t val )
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
|
|
|
tr_benc * node = tr_bencListAdd( list );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencInitInt( node, val );
|
|
|
|
return node;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_benc *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencListAddStr( tr_benc * list,
|
|
|
|
const char * val )
|
2008-04-14 11:52:50 +00:00
|
|
|
{
|
|
|
|
tr_benc * node = tr_bencListAdd( list );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_bencInitStr( node, val, -1 );
|
2008-04-14 11:52:50 +00:00
|
|
|
return node;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_benc*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencListAddList( tr_benc * list,
|
|
|
|
size_t reserveCount )
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
|
|
|
tr_benc * child = tr_bencListAdd( list );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencInitList( child, reserveCount );
|
|
|
|
return child;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_benc*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencListAddDict( tr_benc * list,
|
|
|
|
size_t reserveCount )
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
|
|
|
tr_benc * child = tr_bencListAdd( list );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencInitDict( child, reserveCount );
|
|
|
|
return child;
|
|
|
|
}
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAdd( tr_benc * dict,
|
|
|
|
const char * key )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * keyval, * itemval;
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2008-04-04 17:19:44 +00:00
|
|
|
assert( tr_bencIsDict( dict ) );
|
2008-05-11 22:42:53 +00:00
|
|
|
if( dict->val.l.count + 2 > dict->val.l.alloc )
|
|
|
|
makeroom( dict, 2 );
|
2007-03-29 21:02:59 +00:00
|
|
|
assert( dict->val.l.count + 2 <= dict->val.l.alloc );
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2007-06-19 03:01:57 +00:00
|
|
|
keyval = dict->val.l.vals + dict->val.l.count++;
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_bencInitStr( keyval, key, -1 );
|
2007-06-19 03:01:57 +00:00
|
|
|
|
|
|
|
itemval = dict->val.l.vals + dict->val.l.count++;
|
2007-03-29 21:02:59 +00:00
|
|
|
tr_bencInit( itemval, TYPE_INT );
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2007-03-29 21:02:59 +00:00
|
|
|
return itemval;
|
2007-03-23 08:28:01 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_benc*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddInt( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
int64_t val )
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
|
|
|
tr_benc * child = tr_bencDictAdd( dict, key );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencInitInt( child, val );
|
|
|
|
return child;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_benc*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddStr( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
const char * val )
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
|
|
|
tr_benc * child = tr_bencDictAdd( dict, key );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_bencInitStr( child, val, -1 );
|
2008-04-18 16:23:59 +00:00
|
|
|
return child;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_benc*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddDouble( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
double d )
|
2008-05-08 03:25:21 +00:00
|
|
|
{
|
|
|
|
char buf[128];
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( buf, sizeof( buf ), "%f", d );
|
2008-05-08 03:25:21 +00:00
|
|
|
return tr_bencDictAddStr( dict, key, buf );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-08 03:25:21 +00:00
|
|
|
tr_benc*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddList( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
size_t reserveCount )
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
|
|
|
tr_benc * child = tr_bencDictAdd( dict, key );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencInitList( child, reserveCount );
|
|
|
|
return child;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_benc*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddDict( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
size_t reserveCount )
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
|
|
|
tr_benc * child = tr_bencDictAdd( dict, key );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencInitDict( child, reserveCount );
|
|
|
|
return child;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_benc*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddRaw( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
const void * src,
|
|
|
|
size_t len )
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
|
|
|
tr_benc * child = tr_bencDictAdd( dict, key );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencInitRaw( child, src, len );
|
|
|
|
return child;
|
|
|
|
}
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2008-06-01 01:40:32 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictRemove( tr_benc * dict,
|
|
|
|
const char * key )
|
2008-06-01 01:40:32 +00:00
|
|
|
{
|
|
|
|
int i = dictIndexOf( dict, key );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-01 01:40:32 +00:00
|
|
|
if( i >= 0 )
|
|
|
|
{
|
|
|
|
const int n = dict->val.l.count;
|
|
|
|
tr_bencFree( &dict->val.l.vals[i] );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencFree( &dict->val.l.vals[i + 1] );
|
2008-06-01 01:40:32 +00:00
|
|
|
if( i + 2 < n )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
dict->val.l.vals[i] = dict->val.l.vals[n - 2];
|
|
|
|
dict->val.l.vals[i + 1] = dict->val.l.vals[n - 1];
|
2008-06-01 01:40:32 +00:00
|
|
|
}
|
|
|
|
dict->val.l.count -= 2;
|
|
|
|
}
|
|
|
|
return i >= 0; /* return true if found */
|
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
/***
|
|
|
|
**** BENC WALKING
|
|
|
|
***/
|
|
|
|
|
2007-11-06 21:14:30 +00:00
|
|
|
struct KeyIndex
|
2007-06-29 02:27:00 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * key;
|
|
|
|
int index;
|
2007-11-06 21:14:30 +00:00
|
|
|
};
|
2007-06-29 02:27:00 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
compareKeyIndex( const void * va,
|
|
|
|
const void * vb )
|
2007-06-29 02:27:00 +00:00
|
|
|
{
|
2007-11-06 21:14:30 +00:00
|
|
|
const struct KeyIndex * a = va;
|
|
|
|
const struct KeyIndex * b = vb;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-06-29 02:27:00 +00:00
|
|
|
return strcmp( a->key, b->key );
|
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
struct SaveNode
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const tr_benc * val;
|
|
|
|
int valIsVisited;
|
|
|
|
int childCount;
|
|
|
|
int childIndex;
|
|
|
|
int * children;
|
2008-01-31 02:24:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct SaveNode*
|
2008-02-26 21:58:58 +00:00
|
|
|
nodeNewDict( const tr_benc * val )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i, j;
|
|
|
|
int nKeys;
|
2008-01-31 02:24:43 +00:00
|
|
|
struct SaveNode * node;
|
|
|
|
struct KeyIndex * indices;
|
|
|
|
|
2008-04-04 17:19:44 +00:00
|
|
|
assert( tr_bencIsDict( val ) );
|
2008-01-31 02:24:43 +00:00
|
|
|
|
2008-02-01 15:50:17 +00:00
|
|
|
nKeys = val->val.l.count / 2;
|
2008-01-31 02:24:43 +00:00
|
|
|
node = tr_new0( struct SaveNode, 1 );
|
|
|
|
node->val = val;
|
2008-02-01 15:50:17 +00:00
|
|
|
node->children = tr_new0( int, nKeys * 2 );
|
2008-01-31 02:24:43 +00:00
|
|
|
|
|
|
|
/* ugh, a dictionary's children have to be sorted by key... */
|
2008-02-01 15:50:17 +00:00
|
|
|
indices = tr_new( struct KeyIndex, nKeys );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = j = 0; i < ( nKeys * 2 ); i += 2, ++j )
|
|
|
|
{
|
2008-01-31 02:24:43 +00:00
|
|
|
indices[j].key = val->val.l.vals[i].val.s.s;
|
|
|
|
indices[j].index = i;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
qsort( indices, j, sizeof( struct KeyIndex ), compareKeyIndex );
|
|
|
|
for( i = 0; i < j; ++i )
|
|
|
|
{
|
2008-01-31 02:24:43 +00:00
|
|
|
const int index = indices[i].index;
|
2008-09-23 19:11:04 +00:00
|
|
|
node->children[node->childCount++] = index;
|
|
|
|
node->children[node->childCount++] = index + 1;
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
|
|
|
|
2008-02-01 15:50:17 +00:00
|
|
|
assert( node->childCount == nKeys * 2 );
|
2008-01-31 02:24:43 +00:00
|
|
|
tr_free( indices );
|
|
|
|
return node;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static struct SaveNode*
|
2008-02-26 21:58:58 +00:00
|
|
|
nodeNewList( const tr_benc * val )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i, n;
|
2008-01-31 02:24:43 +00:00
|
|
|
struct SaveNode * node;
|
|
|
|
|
2008-04-04 17:19:44 +00:00
|
|
|
assert( tr_bencIsList( val ) );
|
2008-01-31 02:24:43 +00:00
|
|
|
|
|
|
|
n = val->val.l.count;
|
|
|
|
node = tr_new0( struct SaveNode, 1 );
|
|
|
|
node->val = val;
|
|
|
|
node->childCount = n;
|
|
|
|
node->children = tr_new0( int, n );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i ) /* a list's children don't need to be reordered */
|
2008-01-31 02:24:43 +00:00
|
|
|
node->children[i] = i;
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct SaveNode*
|
2008-02-26 21:58:58 +00:00
|
|
|
nodeNewLeaf( const tr_benc * val )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
|
|
|
struct SaveNode * node;
|
|
|
|
|
|
|
|
assert( !isContainer( val ) );
|
|
|
|
|
|
|
|
node = tr_new0( struct SaveNode, 1 );
|
|
|
|
node->val = val;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct SaveNode*
|
2008-02-26 21:58:58 +00:00
|
|
|
nodeNew( const tr_benc * val )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-01-31 15:20:42 +00:00
|
|
|
struct SaveNode * node;
|
2008-01-31 02:24:43 +00:00
|
|
|
|
2008-04-04 17:19:44 +00:00
|
|
|
if( tr_bencIsList( val ) )
|
2008-01-31 15:20:42 +00:00
|
|
|
node = nodeNewList( val );
|
2008-04-04 17:19:44 +00:00
|
|
|
else if( tr_bencIsDict( val ) )
|
2008-01-31 15:20:42 +00:00
|
|
|
node = nodeNewDict( val );
|
|
|
|
else
|
|
|
|
node = nodeNewLeaf( val );
|
|
|
|
|
|
|
|
return node;
|
2007-11-06 21:14:30 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
typedef void ( *BencWalkFunc )( const tr_benc * val, void * user_data );
|
2008-01-31 02:24:43 +00:00
|
|
|
|
|
|
|
struct WalkFuncs
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
BencWalkFunc intFunc;
|
|
|
|
BencWalkFunc stringFunc;
|
|
|
|
BencWalkFunc dictBeginFunc;
|
|
|
|
BencWalkFunc listBeginFunc;
|
|
|
|
BencWalkFunc containerEndFunc;
|
2008-01-31 02:24:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2008-02-01 01:54:04 +00:00
|
|
|
* This function's previous recursive implementation was
|
|
|
|
* easier to read, but was vulnerable to a smash-stacking
|
|
|
|
* attack via maliciously-crafted bencoded data. (#667)
|
2008-01-31 02:24:43 +00:00
|
|
|
*/
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
bencWalk( const tr_benc * top,
|
|
|
|
struct WalkFuncs * walkFuncs,
|
|
|
|
void * user_data )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
|
|
|
tr_ptrArray * stack = tr_ptrArrayNew( );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
tr_ptrArrayAppend( stack, nodeNew( top ) );
|
|
|
|
|
|
|
|
while( !tr_ptrArrayEmpty( stack ) )
|
|
|
|
{
|
|
|
|
struct SaveNode * node = tr_ptrArrayBack( stack );
|
2008-09-23 19:11:04 +00:00
|
|
|
const tr_benc * val;
|
2008-01-31 02:24:43 +00:00
|
|
|
|
2008-02-01 01:54:04 +00:00
|
|
|
if( !node->valIsVisited )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
|
|
|
val = node->val;
|
2008-02-01 01:54:04 +00:00
|
|
|
node->valIsVisited = TRUE;
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
|
|
|
else if( node->childIndex < node->childCount )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const int index = node->children[node->childIndex++];
|
2008-01-31 02:24:43 +00:00
|
|
|
val = node->val->val.l.vals + index;
|
|
|
|
}
|
|
|
|
else /* done with this node */
|
|
|
|
{
|
|
|
|
if( isContainer( node->val ) )
|
|
|
|
walkFuncs->containerEndFunc( node->val, user_data );
|
|
|
|
tr_ptrArrayPop( stack );
|
|
|
|
tr_free( node->children );
|
|
|
|
tr_free( node );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-05-24 18:22:16 +00:00
|
|
|
if( val ) switch( val->type )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
|
|
|
case TYPE_INT:
|
|
|
|
walkFuncs->intFunc( val, user_data );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_STR:
|
|
|
|
walkFuncs->stringFunc( val, user_data );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_LIST:
|
|
|
|
if( val != node->val )
|
|
|
|
tr_ptrArrayAppend( stack, nodeNew( val ) );
|
|
|
|
else
|
|
|
|
walkFuncs->listBeginFunc( val, user_data );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_DICT:
|
|
|
|
if( val != node->val )
|
|
|
|
tr_ptrArrayAppend( stack, nodeNew( val ) );
|
|
|
|
else
|
|
|
|
walkFuncs->dictBeginFunc( val, user_data );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* did caller give us an uninitialized val? */
|
|
|
|
tr_err( _( "Invalid metadata" ) );
|
|
|
|
break;
|
|
|
|
}
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_ptrArrayFree( stack, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
saveIntFunc( const tr_benc * val,
|
|
|
|
void * evbuf )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
evbuffer_add_printf( evbuf, "i%" PRId64 "e", val->val.i );
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
saveStringFunc( const tr_benc * val,
|
|
|
|
void * vevbuf )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-01-31 15:20:42 +00:00
|
|
|
struct evbuffer * evbuf = vevbuf;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-09-04 14:42:32 +00:00
|
|
|
evbuffer_add_printf( evbuf, "%lu:", (unsigned long)val->val.s.i );
|
2008-01-31 15:20:42 +00:00
|
|
|
evbuffer_add( evbuf, val->val.s.s, val->val.s.i );
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
saveDictBeginFunc( const tr_benc * val UNUSED,
|
|
|
|
void * evbuf )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-01-31 15:20:42 +00:00
|
|
|
evbuffer_add_printf( evbuf, "d" );
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
saveListBeginFunc( const tr_benc * val UNUSED,
|
|
|
|
void * evbuf )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-01-31 15:20:42 +00:00
|
|
|
evbuffer_add_printf( evbuf, "l" );
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
saveContainerEndFunc( const tr_benc * val UNUSED,
|
|
|
|
void * evbuf )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-01-31 15:20:42 +00:00
|
|
|
evbuffer_add_printf( evbuf, "e" );
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-11-06 21:14:30 +00:00
|
|
|
char*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencSave( const tr_benc * top,
|
|
|
|
int * len )
|
2007-11-06 21:14:30 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char * ret;
|
|
|
|
struct WalkFuncs walkFuncs;
|
2008-01-31 02:24:43 +00:00
|
|
|
struct evbuffer * out = evbuffer_new( );
|
|
|
|
|
|
|
|
walkFuncs.intFunc = saveIntFunc;
|
|
|
|
walkFuncs.stringFunc = saveStringFunc;
|
|
|
|
walkFuncs.dictBeginFunc = saveDictBeginFunc;
|
|
|
|
walkFuncs.listBeginFunc = saveListBeginFunc;
|
|
|
|
walkFuncs.containerEndFunc = saveContainerEndFunc;
|
2008-01-31 06:00:57 +00:00
|
|
|
bencWalk( top, &walkFuncs, out );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
if( len )
|
2008-01-31 02:24:43 +00:00
|
|
|
*len = EVBUFFER_LENGTH( out );
|
2008-08-21 16:12:17 +00:00
|
|
|
ret = tr_strndup( EVBUFFER_DATA( out ), EVBUFFER_LENGTH( out ) );
|
2008-01-31 02:24:43 +00:00
|
|
|
evbuffer_free( out );
|
2007-11-06 21:14:30 +00:00
|
|
|
return ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
freeDummyFunc( const tr_benc * val UNUSED,
|
|
|
|
void * buf UNUSED )
|
|
|
|
{}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
freeStringFunc( const tr_benc * val,
|
|
|
|
void * freeme )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_ptrArrayAppend( freeme, val->val.s.s );
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
freeContainerBeginFunc( const tr_benc * val,
|
|
|
|
void * freeme )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
|
|
|
tr_ptrArrayAppend( freeme, val->val.l.vals );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
void
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_bencFree( tr_benc * val )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-06-01 01:40:32 +00:00
|
|
|
if( val && val->type )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ptrArray * freeme = tr_ptrArrayNew( );
|
2008-01-31 02:24:43 +00:00
|
|
|
struct WalkFuncs walkFuncs;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
walkFuncs.intFunc = freeDummyFunc;
|
|
|
|
walkFuncs.stringFunc = freeStringFunc;
|
|
|
|
walkFuncs.dictBeginFunc = freeContainerBeginFunc;
|
|
|
|
walkFuncs.listBeginFunc = freeContainerBeginFunc;
|
|
|
|
walkFuncs.containerEndFunc = freeDummyFunc;
|
2008-01-31 06:00:57 +00:00
|
|
|
bencWalk( val, &walkFuncs, freeme );
|
2008-01-31 02:24:43 +00:00
|
|
|
|
|
|
|
tr_ptrArrayFree( freeme, tr_free );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-03-04 19:29:51 +00:00
|
|
|
struct ParentState
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int bencType;
|
|
|
|
int childIndex;
|
|
|
|
int childCount;
|
2008-03-04 19:29:51 +00:00
|
|
|
};
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-12 17:54:57 +00:00
|
|
|
struct jsonWalk
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_list * parents;
|
|
|
|
struct evbuffer * out;
|
2008-03-04 19:29:51 +00:00
|
|
|
};
|
|
|
|
|
2008-05-22 16:41:16 +00:00
|
|
|
static void
|
|
|
|
jsonIndent( struct jsonWalk * data )
|
|
|
|
{
|
|
|
|
const int width = tr_list_size( data->parents ) * 4;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-22 16:41:16 +00:00
|
|
|
evbuffer_add_printf( data->out, "\n%*.*s", width, width, " " );
|
|
|
|
}
|
|
|
|
|
2008-03-04 19:29:51 +00:00
|
|
|
static void
|
2008-05-12 17:54:57 +00:00
|
|
|
jsonChildFunc( struct jsonWalk * data )
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
|
|
|
if( data->parents )
|
|
|
|
{
|
|
|
|
struct ParentState * parentState = data->parents->data;
|
|
|
|
|
2008-05-12 17:54:57 +00:00
|
|
|
switch( parentState->bencType )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
case TYPE_DICT:
|
|
|
|
{
|
2008-05-12 17:54:57 +00:00
|
|
|
const int i = parentState->childIndex++;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !( i % 2 ) )
|
2008-05-12 17:54:57 +00:00
|
|
|
evbuffer_add_printf( data->out, ": " );
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
2008-05-13 12:52:58 +00:00
|
|
|
evbuffer_add_printf( data->out, ", " );
|
2008-05-22 16:41:16 +00:00
|
|
|
jsonIndent( data );
|
|
|
|
}
|
2008-05-12 17:54:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
case TYPE_LIST:
|
|
|
|
{
|
2008-05-13 12:52:58 +00:00
|
|
|
++parentState->childIndex;
|
|
|
|
evbuffer_add_printf( data->out, ", " );
|
2008-05-22 16:41:16 +00:00
|
|
|
jsonIndent( data );
|
2008-05-12 17:54:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-03-04 19:29:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
jsonPushParent( struct jsonWalk * data,
|
|
|
|
const tr_benc * benc )
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
|
|
|
struct ParentState * parentState = tr_new( struct ParentState, 1 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-12 17:54:57 +00:00
|
|
|
parentState->bencType = benc->type;
|
|
|
|
parentState->childIndex = 0;
|
|
|
|
parentState->childCount = benc->val.l.count;
|
2008-03-04 19:29:51 +00:00
|
|
|
tr_list_prepend( &data->parents, parentState );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-05-12 17:54:57 +00:00
|
|
|
jsonPopParent( struct jsonWalk * data )
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
|
|
|
tr_free( tr_list_pop_front( &data->parents ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
jsonIntFunc( const tr_benc * val,
|
|
|
|
void * vdata )
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
2008-05-12 17:54:57 +00:00
|
|
|
struct jsonWalk * data = vdata;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
evbuffer_add_printf( data->out, "%" PRId64, val->val.i );
|
2008-05-12 17:54:57 +00:00
|
|
|
jsonChildFunc( data );
|
2008-03-04 19:29:51 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-03-04 19:29:51 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
jsonStringFunc( const tr_benc * val,
|
|
|
|
void * vdata )
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
struct jsonWalk * data = vdata;
|
2008-08-19 23:11:35 +00:00
|
|
|
const unsigned char *it, *end;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-12 17:54:57 +00:00
|
|
|
evbuffer_add_printf( data->out, "\"" );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( it = (const unsigned char*)val->val.s.s, end = it + val->val.s.i;
|
|
|
|
it != end; ++it )
|
2008-05-12 17:54:57 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
switch( *it )
|
|
|
|
{
|
|
|
|
case '/':
|
|
|
|
evbuffer_add_printf( data->out, "\\/" ); break;
|
|
|
|
|
|
|
|
case '\b':
|
|
|
|
evbuffer_add_printf( data->out, "\\b" ); break;
|
|
|
|
|
|
|
|
case '\f':
|
|
|
|
evbuffer_add_printf( data->out, "\\f" ); break;
|
|
|
|
|
|
|
|
case '\n':
|
|
|
|
evbuffer_add_printf( data->out, "\\n" ); break;
|
|
|
|
|
|
|
|
case '\r':
|
|
|
|
evbuffer_add_printf( data->out, "\\r" ); break;
|
|
|
|
|
|
|
|
case '\t':
|
|
|
|
evbuffer_add_printf( data->out, "\\t" ); break;
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
evbuffer_add_printf( data->out, "\\\"" ); break;
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
evbuffer_add_printf( data->out, "\\\\" ); break;
|
|
|
|
|
2008-08-19 23:11:35 +00:00
|
|
|
default:
|
2008-09-23 19:11:04 +00:00
|
|
|
if( isascii( *it ) )
|
|
|
|
{
|
2008-08-19 23:11:35 +00:00
|
|
|
/*fprintf( stderr, "[%c]\n", *it );*/
|
|
|
|
evbuffer_add_printf( data->out, "%c", *it );
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-08-19 23:11:35 +00:00
|
|
|
const UTF8 * tmp = it;
|
2008-09-23 19:11:04 +00:00
|
|
|
UTF32 buf = 0;
|
|
|
|
UTF32 * u32 = &buf;
|
2008-10-06 01:52:29 +00:00
|
|
|
ConversionResult result = ConvertUTF8toUTF32( &tmp, end, &u32, &buf + 1, 0 );
|
|
|
|
if( ( result != conversionOK ) && ( tmp == it ) )
|
|
|
|
++it; /* it's beyond help; skip it */
|
|
|
|
else {
|
|
|
|
evbuffer_add_printf( data->out, "\\u%04x", buf );
|
|
|
|
it = tmp - 1;
|
|
|
|
}
|
2008-08-19 23:11:35 +00:00
|
|
|
/*fprintf( stderr, "[\\u%04x]\n", buf );*/
|
|
|
|
}
|
2008-05-12 17:54:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
evbuffer_add_printf( data->out, "\"" );
|
|
|
|
jsonChildFunc( data );
|
2008-03-04 19:29:51 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-03-04 19:29:51 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
jsonDictBeginFunc( const tr_benc * val,
|
|
|
|
void * vdata )
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
2008-05-12 17:54:57 +00:00
|
|
|
struct jsonWalk * data = vdata;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-12 17:54:57 +00:00
|
|
|
jsonPushParent( data, val );
|
2008-05-22 16:41:16 +00:00
|
|
|
evbuffer_add_printf( data->out, "{" );
|
|
|
|
if( val->val.l.count )
|
|
|
|
jsonIndent( data );
|
2008-03-04 19:29:51 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-03-04 19:29:51 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
jsonListBeginFunc( const tr_benc * val,
|
|
|
|
void * vdata )
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const size_t nChildren = tr_bencListSize( val );
|
2008-05-12 17:54:57 +00:00
|
|
|
struct jsonWalk * data = vdata;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-12 17:54:57 +00:00
|
|
|
jsonPushParent( data, val );
|
2008-05-22 16:41:16 +00:00
|
|
|
evbuffer_add_printf( data->out, "[" );
|
|
|
|
if( nChildren )
|
|
|
|
jsonIndent( data );
|
2008-03-04 19:29:51 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-03-04 19:29:51 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
jsonContainerEndFunc( const tr_benc * val,
|
|
|
|
void * vdata )
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t i;
|
2008-05-12 17:54:57 +00:00
|
|
|
struct jsonWalk * data = vdata;
|
2008-09-23 19:11:04 +00:00
|
|
|
char * str;
|
|
|
|
int emptyContainer = FALSE;
|
2008-05-22 16:41:16 +00:00
|
|
|
|
|
|
|
/* trim out the trailing comma, if any */
|
|
|
|
str = (char*) EVBUFFER_DATA( data->out );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = EVBUFFER_LENGTH( data->out ) - 1; i > 0; --i )
|
|
|
|
{
|
2008-05-22 16:41:16 +00:00
|
|
|
if( isspace( str[i] ) ) continue;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( str[i] == ',' )
|
2008-05-22 16:41:16 +00:00
|
|
|
EVBUFFER_LENGTH( data->out ) = i;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( str[i] == '{' || str[i] == '[' )
|
2008-05-22 16:41:16 +00:00
|
|
|
emptyContainer = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonPopParent( data );
|
|
|
|
if( !emptyContainer )
|
|
|
|
jsonIndent( data );
|
2008-05-13 12:52:58 +00:00
|
|
|
if( tr_bencIsDict( val ) )
|
2008-05-22 16:41:16 +00:00
|
|
|
evbuffer_add_printf( data->out, "}" );
|
2008-05-13 12:52:58 +00:00
|
|
|
else /* list */
|
2008-05-22 16:41:16 +00:00
|
|
|
evbuffer_add_printf( data->out, "]" );
|
2008-05-12 17:54:57 +00:00
|
|
|
jsonChildFunc( data );
|
2008-03-04 19:29:51 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-03-04 19:29:51 +00:00
|
|
|
char*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencSaveAsJSON( const tr_benc * top,
|
|
|
|
int * len )
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char * ret;
|
2008-03-04 19:29:51 +00:00
|
|
|
struct WalkFuncs walkFuncs;
|
2008-09-23 19:11:04 +00:00
|
|
|
struct jsonWalk data;
|
2008-03-04 19:29:51 +00:00
|
|
|
|
|
|
|
data.out = evbuffer_new( );
|
|
|
|
data.parents = NULL;
|
|
|
|
|
2008-05-12 17:54:57 +00:00
|
|
|
walkFuncs.intFunc = jsonIntFunc;
|
|
|
|
walkFuncs.stringFunc = jsonStringFunc;
|
|
|
|
walkFuncs.dictBeginFunc = jsonDictBeginFunc;
|
|
|
|
walkFuncs.listBeginFunc = jsonListBeginFunc;
|
|
|
|
walkFuncs.containerEndFunc = jsonContainerEndFunc;
|
2008-03-04 19:29:51 +00:00
|
|
|
|
|
|
|
bencWalk( top, &walkFuncs, &data );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-22 16:41:16 +00:00
|
|
|
if( EVBUFFER_LENGTH( data.out ) )
|
|
|
|
evbuffer_add_printf( data.out, "\n" );
|
2008-08-01 16:43:22 +00:00
|
|
|
if( len )
|
2008-03-04 19:29:51 +00:00
|
|
|
*len = EVBUFFER_LENGTH( data.out );
|
2008-08-21 16:12:17 +00:00
|
|
|
ret = tr_strndup( EVBUFFER_DATA( data.out ), EVBUFFER_LENGTH( data.out ) );
|
2008-03-04 19:29:51 +00:00
|
|
|
evbuffer_free( data.out );
|
|
|
|
return ret;
|
|
|
|
}
|
2008-04-14 11:52:50 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-06-11 20:09:36 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
saveFile( const char * filename,
|
|
|
|
const char * content,
|
|
|
|
size_t len )
|
2008-04-14 11:52:50 +00:00
|
|
|
{
|
2008-10-03 04:49:06 +00:00
|
|
|
int err = 0;
|
2008-04-14 11:52:50 +00:00
|
|
|
FILE * out = NULL;
|
|
|
|
|
|
|
|
out = fopen( filename, "wb+" );
|
2008-10-03 04:49:06 +00:00
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
if( !out )
|
|
|
|
{
|
2008-10-03 04:49:06 +00:00
|
|
|
err = errno;
|
2008-04-14 11:52:50 +00:00
|
|
|
tr_err( _( "Couldn't open \"%1$s\": %2$s" ),
|
2008-10-03 04:49:06 +00:00
|
|
|
filename, tr_strerror( errno ) );
|
2008-04-14 11:52:50 +00:00
|
|
|
}
|
|
|
|
else if( fwrite( content, sizeof( char ), len, out ) != (size_t)len )
|
|
|
|
{
|
2008-10-03 04:49:06 +00:00
|
|
|
err = errno;
|
2008-04-14 11:52:50 +00:00
|
|
|
tr_err( _( "Couldn't save file \"%1$s\": %2$s" ),
|
2008-09-23 19:11:04 +00:00
|
|
|
filename, tr_strerror( errno ) );
|
2008-04-14 11:52:50 +00:00
|
|
|
}
|
|
|
|
|
2008-04-14 20:06:37 +00:00
|
|
|
if( !err )
|
|
|
|
tr_dbg( "tr_bencSaveFile saved \"%s\"", filename );
|
2008-04-14 11:52:50 +00:00
|
|
|
if( out )
|
|
|
|
fclose( out );
|
2008-04-14 20:06:37 +00:00
|
|
|
return err;
|
2008-04-14 11:52:50 +00:00
|
|
|
}
|
|
|
|
|
2008-06-11 20:09:36 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencSaveFile( const char * filename,
|
|
|
|
const tr_benc * b )
|
2008-06-11 20:09:36 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int len;
|
|
|
|
char * content = tr_bencSave( b, &len );
|
2008-06-11 20:09:36 +00:00
|
|
|
const int err = saveFile( filename, content, len );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-11 20:09:36 +00:00
|
|
|
tr_free( content );
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2008-08-21 14:57:59 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencSaveJSONFile( const char * filename,
|
|
|
|
const tr_benc * b )
|
2008-08-21 14:57:59 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int len;
|
|
|
|
char * content = tr_bencSaveAsJSON( b, &len );
|
2008-08-21 14:57:59 +00:00
|
|
|
const int err = saveFile( filename, content, len );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_free( content );
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencLoadFile( const char * filename,
|
|
|
|
tr_benc * b )
|
2008-04-14 11:52:50 +00:00
|
|
|
{
|
2008-10-03 04:49:06 +00:00
|
|
|
int err;
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t contentLen;
|
2008-10-03 04:49:06 +00:00
|
|
|
uint8_t * content;
|
|
|
|
|
|
|
|
content = tr_loadFile( filename, &contentLen );
|
|
|
|
if( !content )
|
|
|
|
err = errno;
|
|
|
|
else
|
|
|
|
err = tr_bencLoad( content, contentLen, b, NULL );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
tr_free( content );
|
2008-10-03 04:49:06 +00:00
|
|
|
return err;
|
2008-04-14 11:52:50 +00:00
|
|
|
}
|
2008-06-11 20:09:36 +00:00
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencLoadJSONFile( const char * filename,
|
|
|
|
tr_benc * b )
|
2008-06-11 20:09:36 +00:00
|
|
|
{
|
2008-10-03 04:49:06 +00:00
|
|
|
int err;
|
|
|
|
size_t contentLen;
|
|
|
|
uint8_t * content;
|
|
|
|
|
|
|
|
content = tr_loadFile( filename, &contentLen );
|
|
|
|
if( !content )
|
|
|
|
err = errno;
|
|
|
|
else
|
|
|
|
err = tr_jsonParse( content, contentLen, b, NULL );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-11 20:09:36 +00:00
|
|
|
tr_free( content );
|
2008-10-03 04:49:06 +00:00
|
|
|
return err;
|
2008-06-11 20:09:36 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|