2008-07-22 23:28:28 +00:00
|
|
|
/*
|
2010-01-04 21:00:47 +00:00
|
|
|
* This file Copyright (C) 2008-2010 Mnemosyne LLC
|
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>
|
2010-04-29 18:56:34 +00:00
|
|
|
#include <ctype.h> /* isdigit() */
|
2008-01-30 15:39:41 +00:00
|
|
|
#include <errno.h>
|
2010-04-29 18:56:34 +00:00
|
|
|
#include <math.h> /* fabs() */
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <stdio.h>
|
2010-04-29 18:56:34 +00:00
|
|
|
#include <stdlib.h> /* realpath() */
|
2008-08-21 14:57:59 +00:00
|
|
|
#include <string.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2010-06-22 00:12:52 +00:00
|
|
|
#ifdef WIN32 /* tr_mkstemp() */
|
2010-06-21 16:44:35 +00:00
|
|
|
#include <fcntl.h>
|
2010-06-22 00:12:52 +00:00
|
|
|
#define _S_IREAD 256
|
|
|
|
#define _S_IWRITE 128
|
2010-06-21 16:44:35 +00:00
|
|
|
#endif
|
|
|
|
|
2010-02-01 21:09:31 +00:00
|
|
|
#include <sys/types.h> /* stat() */
|
|
|
|
#include <sys/stat.h> /* stat() */
|
2008-11-27 18:35:26 +00:00
|
|
|
#include <locale.h>
|
2010-02-01 21:09:31 +00:00
|
|
|
#include <unistd.h> /* stat(), close() */
|
2008-11-27 18:35:26 +00:00
|
|
|
|
2010-04-29 18:56:34 +00:00
|
|
|
#include <event.h> /* struct 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"
|
2010-05-06 17:02:31 +00:00
|
|
|
#include "platform.h" /* TR_PATH_MAX */
|
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
|
|
|
|
2009-02-18 16:47:27 +00:00
|
|
|
#ifndef ENODATA
|
|
|
|
#define ENODATA EIO
|
|
|
|
#endif
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
static tr_bool
|
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-12-12 02:12:17 +00:00
|
|
|
static tr_bool
|
2008-02-26 21:58:58 +00:00
|
|
|
isSomething( const tr_benc * val )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2009-04-05 21:33:06 +00:00
|
|
|
return isContainer( val ) || tr_bencIsInt( val )
|
|
|
|
|| tr_bencIsString( val )
|
|
|
|
|| tr_bencIsReal( val )
|
|
|
|
|| tr_bencIsBool( val );
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
|
|
|
|
2008-06-19 02:53:57 +00:00
|
|
|
static void
|
2010-06-30 16:40:19 +00:00
|
|
|
tr_bencInit( tr_benc * val, char 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-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 */
|
2009-12-02 15:16:29 +00:00
|
|
|
return EILSEQ;
|
|
|
|
if( val && *(const char*)begin == '0' ) /* no leading zeroes! */
|
|
|
|
return EILSEQ;
|
|
|
|
|
|
|
|
*setme_end = (const uint8_t*)end + 1;
|
|
|
|
*setme_val = val;
|
|
|
|
return 0;
|
2008-01-30 15:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 */
|
2009-06-07 17:54:28 +00:00
|
|
|
#define LIST_SIZE 4 /* 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
|
|
|
{
|
2009-04-05 18:02:11 +00:00
|
|
|
assert( TR_TYPE_LIST == val->type || TR_TYPE_DICT == val->type );
|
2007-03-23 08:28:01 +00:00
|
|
|
|
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 );
|
2009-01-10 02:22:13 +00:00
|
|
|
void * tmp = realloc( val->val.l.vals, len * sizeof( tr_benc ) );
|
|
|
|
if( !tmp )
|
2007-06-19 01:49:56 +00:00
|
|
|
return 1;
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2007-06-19 01:49:56 +00:00
|
|
|
val->val.l.alloc = len;
|
2009-01-10 02:22:13 +00:00
|
|
|
val->val.l.vals = tmp;
|
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 */
|
2009-04-05 18:02:11 +00:00
|
|
|
if( ( parent->type == TR_TYPE_DICT )
|
|
|
|
&& ( type != TR_TYPE_STR )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& ( !( 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
|
|
|
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
|
|
|
|
2009-04-05 18:02:11 +00:00
|
|
|
node = getNode( top, parentStack, TR_TYPE_INT );
|
2008-01-31 02:24:43 +00:00
|
|
|
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
|
|
|
{
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_benc * node = getNode( top, parentStack, TR_TYPE_LIST );
|
2008-01-31 02:24:43 +00:00
|
|
|
if( !node )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_bencInit( node, TR_TYPE_LIST );
|
2008-01-31 02:24:43 +00:00
|
|
|
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
|
|
|
{
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_benc * node = getNode( top, parentStack, TR_TYPE_DICT );
|
2008-01-31 02:24:43 +00:00
|
|
|
if( !node )
|
2008-10-03 04:49:06 +00:00
|
|
|
return EILSEQ;
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_bencInit( node, TR_TYPE_DICT );
|
2008-01-31 02:24:43 +00:00
|
|
|
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;
|
|
|
|
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
|
|
|
|
2009-04-05 18:02:11 +00:00
|
|
|
node = getNode( top, parentStack, TR_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-12-29 08:54:36 +00:00
|
|
|
tr_ptrArray parentStack = TR_PTR_ARRAY_INIT;
|
2008-08-11 19:05:02 +00:00
|
|
|
|
2008-08-22 15:13:01 +00:00
|
|
|
top->type = 0; /* set to `uninitialized' */
|
2008-12-29 08:54:36 +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-12-29 08:54:36 +00:00
|
|
|
tr_ptrArrayDestruct( &parentStack, NULL );
|
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
|
|
|
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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-06-01 22:15:50 +00:00
|
|
|
/* returns true if the benc's string was malloced.
|
|
|
|
* this occurs when the string is too long for our string buffer */
|
2010-01-01 22:26:35 +00:00
|
|
|
static inline int
|
2009-06-01 22:15:50 +00:00
|
|
|
stringIsAlloced( const tr_benc * val )
|
|
|
|
{
|
2010-02-03 14:31:12 +00:00
|
|
|
return val->val.s.len >= sizeof( val->val.s.str.buf );
|
2009-06-01 22:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* returns a const pointer to the benc's string */
|
2010-01-01 22:26:35 +00:00
|
|
|
static inline const char*
|
2009-06-01 22:15:50 +00:00
|
|
|
getStr( const tr_benc* val )
|
|
|
|
{
|
|
|
|
return stringIsAlloced(val) ? val->val.s.str.ptr : val->val.s.str.buf;
|
|
|
|
}
|
|
|
|
|
2008-06-01 01:40:32 +00:00
|
|
|
static int
|
2009-06-01 22:15:50 +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;
|
2009-04-05 18:02:11 +00:00
|
|
|
if( ( child->type == TR_TYPE_STR )
|
2009-06-01 22:15:50 +00:00
|
|
|
&& ( child->val.s.len == len )
|
|
|
|
&& !memcmp( getStr(child), 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-12-12 02:12:17 +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
|
|
|
}
|
|
|
|
|
2009-04-09 00:26:53 +00:00
|
|
|
static tr_bool
|
|
|
|
tr_bencDictFindType( tr_benc * dict, const char * key, int type, tr_benc ** setme )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2009-04-09 00:26:53 +00:00
|
|
|
return tr_bencIsType( *setme = tr_bencDictFind( dict, key ), type );
|
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;
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:27:24 +00:00
|
|
|
int
|
|
|
|
tr_bencListRemove( tr_benc * list, size_t i )
|
|
|
|
{
|
|
|
|
if( tr_bencIsList( list ) && ( i < list->val.l.count ) )
|
|
|
|
{
|
|
|
|
tr_bencFree( &list->val.l.vals[i] );
|
|
|
|
tr_removeElementFromArray( list->val.l.vals, i, sizeof( tr_benc ), list->val.l.count-- );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-05 23:46:53 +00:00
|
|
|
static void
|
|
|
|
tr_benc_warning( const char * err )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "warning: %s\n", err );
|
|
|
|
}
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
tr_bool
|
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
|
|
|
{
|
2009-04-05 17:52:21 +00:00
|
|
|
tr_bool success = FALSE;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-04-05 17:52:21 +00:00
|
|
|
if( !success && (( success = tr_bencIsInt( val ))))
|
|
|
|
if( setme )
|
|
|
|
*setme = val->val.i;
|
|
|
|
|
|
|
|
if( !success && (( success = tr_bencIsBool( val )))) {
|
2009-04-05 23:46:53 +00:00
|
|
|
tr_benc_warning( "reading bool as an int" );
|
2009-04-05 17:52:21 +00:00
|
|
|
if( setme )
|
|
|
|
*setme = val->val.b ? 1 : 0;
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2008-05-08 03:25:21 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
tr_bool
|
2010-06-30 16:40:19 +00:00
|
|
|
tr_bencGetStr( const tr_benc * val, const char ** setme )
|
2008-05-08 03:25:21 +00:00
|
|
|
{
|
2010-06-30 16:40:19 +00:00
|
|
|
const tr_bool success = tr_bencIsString( val );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-08 03:25:21 +00:00
|
|
|
if( success )
|
2009-06-01 22:15:50 +00:00
|
|
|
*setme = getStr( val );
|
2009-03-30 01:46:48 +00:00
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
return success;
|
2008-02-02 16:23:49 +00:00
|
|
|
}
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
tr_bool
|
2009-03-30 01:46:48 +00:00
|
|
|
tr_bencGetBool( const tr_benc * val, tr_bool * setme )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2009-06-01 22:15:50 +00:00
|
|
|
const char * str;
|
2009-03-30 01:46:48 +00:00
|
|
|
tr_bool success = FALSE;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-04-05 17:52:21 +00:00
|
|
|
if(( success = tr_bencIsBool( val )))
|
|
|
|
*setme = val->val.b;
|
|
|
|
|
2009-04-06 01:32:55 +00:00
|
|
|
if( !success && tr_bencIsInt( val ) )
|
|
|
|
if(( success = ( val->val.i==0 || val->val.i==1 ) ))
|
2009-03-30 01:46:48 +00:00
|
|
|
*setme = val->val.i!=0;
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2009-06-01 22:15:50 +00:00
|
|
|
if( !success && tr_bencGetStr( val, &str ) )
|
|
|
|
if(( success = ( !strcmp(str,"true") || !strcmp(str,"false"))))
|
|
|
|
*setme = !strcmp(str,"true");
|
2009-03-30 01:46:48 +00:00
|
|
|
|
|
|
|
return success;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2009-03-29 18:45:35 +00:00
|
|
|
tr_bool
|
2009-03-30 01:46:48 +00:00
|
|
|
tr_bencGetReal( const tr_benc * val, double * setme )
|
2009-03-29 18:45:35 +00:00
|
|
|
{
|
2009-03-30 01:46:48 +00:00
|
|
|
tr_bool success = FALSE;
|
|
|
|
|
2009-04-05 17:52:21 +00:00
|
|
|
if( !success && (( success = tr_bencIsReal( val ))))
|
|
|
|
*setme = val->val.d;
|
|
|
|
|
|
|
|
if( !success && (( success = tr_bencIsInt( val ))))
|
|
|
|
*setme = val->val.i;
|
|
|
|
|
2009-03-30 01:46:48 +00:00
|
|
|
if( !success && tr_bencIsString(val) )
|
|
|
|
{
|
|
|
|
char * endptr;
|
2009-05-09 18:01:29 +00:00
|
|
|
char locale[128];
|
2009-03-30 04:45:40 +00:00
|
|
|
double d;
|
|
|
|
|
|
|
|
/* the json spec requires a '.' decimal point regardless of locale */
|
2009-05-09 18:01:29 +00:00
|
|
|
tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) );
|
2009-03-30 04:45:40 +00:00
|
|
|
setlocale( LC_NUMERIC, "POSIX" );
|
2009-06-01 22:15:50 +00:00
|
|
|
d = strtod( getStr(val), &endptr );
|
2009-03-30 04:45:40 +00:00
|
|
|
setlocale( LC_NUMERIC, locale );
|
|
|
|
|
2009-06-01 22:15:50 +00:00
|
|
|
if(( success = ( getStr(val) != endptr ) && !*endptr ))
|
2009-03-30 01:46:48 +00:00
|
|
|
*setme = d;
|
2009-03-29 23:05:32 +00:00
|
|
|
}
|
2009-03-30 01:46:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
return success;
|
2009-03-29 18:45:35 +00:00
|
|
|
}
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
tr_bool
|
2009-03-30 01:46:48 +00:00
|
|
|
tr_bencDictFindInt( tr_benc * dict, const char * key, int64_t * setme )
|
2008-05-08 03:25:21 +00:00
|
|
|
{
|
2009-03-30 01:46:48 +00:00
|
|
|
return tr_bencGetInt( tr_bencDictFind( dict, key ), setme );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-03-30 01:46:48 +00:00
|
|
|
tr_bool
|
|
|
|
tr_bencDictFindBool( tr_benc * dict, const char * key, tr_bool * setme )
|
|
|
|
{
|
|
|
|
return tr_bencGetBool( tr_bencDictFind( dict, key ), setme );
|
|
|
|
}
|
2008-12-12 02:12:17 +00:00
|
|
|
|
2009-03-30 01:46:48 +00:00
|
|
|
tr_bool
|
|
|
|
tr_bencDictFindReal( tr_benc * dict, const char * key, double * setme )
|
|
|
|
{
|
|
|
|
return tr_bencGetReal( tr_bencDictFind( dict, key ), setme );
|
2008-05-08 03:25:21 +00:00
|
|
|
}
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
tr_bool
|
2009-04-09 00:26:53 +00:00
|
|
|
tr_bencDictFindStr( tr_benc * dict, const char * key, const char ** setme )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2009-04-09 00:26:53 +00:00
|
|
|
return tr_bencGetStr( tr_bencDictFind( dict, key ), setme );
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
tr_bool
|
2009-04-09 00:26:53 +00:00
|
|
|
tr_bencDictFindList( tr_benc * dict, const char * key, tr_benc ** setme )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2009-04-09 00:26:53 +00:00
|
|
|
return tr_bencDictFindType( dict, key, TR_TYPE_LIST, setme );
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
tr_bool
|
2009-04-09 00:26:53 +00:00
|
|
|
tr_bencDictFindDict( tr_benc * dict, const char * key, tr_benc ** setme )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2009-04-09 00:26:53 +00:00
|
|
|
return tr_bencDictFindType( dict, key, TR_TYPE_DICT, setme );
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
tr_bool
|
|
|
|
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
|
|
|
{
|
2009-04-09 00:26:53 +00:00
|
|
|
tr_benc * child;
|
|
|
|
const tr_bool found = tr_bencDictFindType( dict, key, TR_TYPE_STR, &child );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-04-09 00:26:53 +00:00
|
|
|
if( found ) {
|
2009-06-01 22:15:50 +00:00
|
|
|
*setme_raw = (uint8_t*) getStr(child);
|
|
|
|
*setme_len = child->val.s.len;
|
2008-08-10 14:58:11 +00:00
|
|
|
}
|
2008-12-12 02:12:17 +00:00
|
|
|
|
2008-08-10 14:58:11 +00:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2008-02-02 16:23:49 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-15 17:00:44 +00:00
|
|
|
void
|
2009-06-01 22:15:50 +00:00
|
|
|
tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount )
|
2008-04-15 17:00:44 +00:00
|
|
|
{
|
2010-02-03 14:31:12 +00:00
|
|
|
char * setme;
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_bencInit( val, TR_TYPE_STR );
|
2009-06-01 22:15:50 +00:00
|
|
|
|
2010-02-03 14:31:12 +00:00
|
|
|
/* There's no way in benc notation to distinguish between
|
|
|
|
* zero-terminated strings and raw byte arrays.
|
|
|
|
* Because of this, tr_bencMergeDicts() and tr_bencListCopy()
|
|
|
|
* don't know whether or not a TR_TYPE_STR node needs a '\0'.
|
|
|
|
* Append one, een to the raw arrays, just to be safe. */
|
|
|
|
|
|
|
|
if( byteCount < sizeof( val->val.s.str.buf ) )
|
|
|
|
setme = val->val.s.str.buf;
|
2009-06-01 22:15:50 +00:00
|
|
|
else
|
2010-02-03 14:31:12 +00:00
|
|
|
setme = val->val.s.str.ptr = tr_new( char, byteCount + 1 );
|
|
|
|
|
|
|
|
memcpy( setme, src, byteCount );
|
|
|
|
setme[byteCount] = '\0';
|
|
|
|
val->val.s.len = byteCount;
|
2008-04-15 17:00:44 +00:00
|
|
|
}
|
|
|
|
|
2008-08-21 14:57:59 +00:00
|
|
|
void
|
2009-06-01 22:15:50 +00:00
|
|
|
tr_bencInitStr( tr_benc * val, const void * str, int len )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
2009-06-01 22:15:50 +00:00
|
|
|
if( str == NULL )
|
|
|
|
len = 0;
|
2008-08-21 17:03:44 +00:00
|
|
|
else if( len < 0 )
|
2009-06-01 22:15:50 +00:00
|
|
|
len = strlen( str );
|
|
|
|
|
2010-02-03 14:31:12 +00:00
|
|
|
tr_bencInitRaw( val, str, len );
|
2007-03-23 08:28:01 +00:00
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
void
|
2009-04-05 17:52:21 +00:00
|
|
|
tr_bencInitBool( tr_benc * b, int value )
|
|
|
|
{
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_bencInit( b, TR_TYPE_BOOL );
|
2009-04-05 17:52:21 +00:00
|
|
|
b->val.b = value != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_bencInitReal( tr_benc * b, double value )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_bencInit( b, TR_TYPE_REAL );
|
2009-04-05 17:52:21 +00:00
|
|
|
b->val.d = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_bencInitInt( tr_benc * b, int64_t value )
|
|
|
|
{
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_bencInit( b, TR_TYPE_INT );
|
2009-04-05 17:52:21 +00:00
|
|
|
b->val.i = value;
|
2007-03-23 08:28:01 +00:00
|
|
|
}
|
|
|
|
|
2008-04-03 21:38:32 +00:00
|
|
|
int
|
2009-04-05 17:52:21 +00:00
|
|
|
tr_bencInitList( tr_benc * b, size_t reserveCount )
|
2008-04-03 21:38:32 +00:00
|
|
|
{
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_bencInit( b, TR_TYPE_LIST );
|
2009-04-05 17:52:21 +00:00
|
|
|
return tr_bencListReserve( b, reserveCount );
|
2008-04-03 21:38:32 +00:00
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
int
|
2009-04-05 17:52:21 +00:00
|
|
|
tr_bencListReserve( tr_benc * b, size_t count )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
2009-04-05 17:52:21 +00:00
|
|
|
assert( tr_bencIsList( b ) );
|
|
|
|
return makeroom( b, count );
|
2007-03-23 08:28:01 +00:00
|
|
|
}
|
|
|
|
|
2008-04-03 21:38:32 +00:00
|
|
|
int
|
2009-04-05 17:52:21 +00:00
|
|
|
tr_bencInitDict( tr_benc * b, size_t reserveCount )
|
2008-04-03 21:38:32 +00:00
|
|
|
{
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_bencInit( b, TR_TYPE_DICT );
|
2009-04-05 17:52:21 +00:00
|
|
|
return tr_bencDictReserve( b, reserveCount );
|
2008-04-03 21:38:32 +00:00
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
int
|
2009-04-05 17:52:21 +00:00
|
|
|
tr_bencDictReserve( tr_benc * b, size_t reserveCount )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
2009-04-05 17:52:21 +00:00
|
|
|
assert( tr_bencIsDict( b ) );
|
|
|
|
return makeroom( b, 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++;
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_bencInit( item, TR_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 *
|
2010-02-02 22:45:22 +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 *
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_bencListAddReal( tr_benc * list, double val )
|
2008-04-14 11:52:50 +00:00
|
|
|
{
|
|
|
|
tr_benc * node = tr_bencListAdd( list );
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_bencInitReal( node, val );
|
|
|
|
return node;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_benc *
|
|
|
|
tr_bencListAddBool( tr_benc * list, tr_bool val )
|
|
|
|
{
|
|
|
|
tr_benc * node = tr_bencListAdd( list );
|
|
|
|
tr_bencInitBool( node, val );
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_benc *
|
|
|
|
tr_bencListAddStr( tr_benc * list, const char * val )
|
|
|
|
{
|
|
|
|
tr_benc * node = tr_bencListAdd( list );
|
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
|
|
|
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_benc *
|
|
|
|
tr_bencListAddRaw( tr_benc * list, const uint8_t * val, size_t len )
|
|
|
|
{
|
|
|
|
tr_benc * node = tr_bencListAdd( list );
|
|
|
|
tr_bencInitRaw( node, val, len );
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
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++;
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_bencInit( itemval, TR_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
|
|
|
|
2009-04-05 17:52:21 +00:00
|
|
|
static tr_benc*
|
|
|
|
dictFindOrAdd( tr_benc * dict, const char * key, int type )
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_benc * child;
|
|
|
|
|
|
|
|
/* see if it already exists, and if so, try to reuse it */
|
|
|
|
if(( child = tr_bencDictFind( dict, key ))) {
|
2009-04-05 17:52:21 +00:00
|
|
|
if( !tr_bencIsType( child, type ) ) {
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencDictRemove( dict, key );
|
|
|
|
child = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if it doesn't exist, create it */
|
|
|
|
if( child == NULL )
|
|
|
|
child = tr_bencDictAdd( dict, key );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-04-05 17:52:21 +00:00
|
|
|
return child;
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2009-04-05 17:52:21 +00:00
|
|
|
tr_benc*
|
|
|
|
tr_bencDictAddInt( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
int64_t val )
|
|
|
|
{
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_benc * child = dictFindOrAdd( dict, key, TR_TYPE_INT );
|
2009-04-05 17:52:21 +00:00
|
|
|
tr_bencInitInt( child, val );
|
2008-04-18 16:23:59 +00:00
|
|
|
return child;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-03-29 18:45:35 +00:00
|
|
|
tr_benc*
|
|
|
|
tr_bencDictAddBool( tr_benc * dict, const char * key, tr_bool val )
|
|
|
|
{
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_benc * child = dictFindOrAdd( dict, key, TR_TYPE_BOOL );
|
2009-04-05 17:52:21 +00:00
|
|
|
tr_bencInitBool( child, val );
|
|
|
|
return child;
|
|
|
|
}
|
2009-03-29 18:45:35 +00:00
|
|
|
|
2009-04-05 17:52:21 +00:00
|
|
|
tr_benc*
|
|
|
|
tr_bencDictAddReal( tr_benc * dict, const char * key, double val )
|
|
|
|
{
|
2009-04-05 18:02:11 +00:00
|
|
|
tr_benc * child = dictFindOrAdd( dict, key, TR_TYPE_REAL );
|
2009-04-05 17:52:21 +00:00
|
|
|
tr_bencInitReal( child, val );
|
|
|
|
return child;
|
2009-03-29 18:45:35 +00:00
|
|
|
}
|
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_benc*
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val )
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_benc * child;
|
|
|
|
|
|
|
|
/* see if it already exists, and if so, try to reuse it */
|
|
|
|
if(( child = tr_bencDictFind( dict, key ))) {
|
2009-06-01 22:15:50 +00:00
|
|
|
if( tr_bencIsString( child ) ) {
|
|
|
|
if( stringIsAlloced( child ) )
|
|
|
|
tr_free( child->val.s.str.ptr );
|
|
|
|
} else {
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencDictRemove( dict, key );
|
|
|
|
child = NULL;
|
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
/* if it doesn't exist, create it */
|
|
|
|
if( child == NULL )
|
|
|
|
child = tr_bencDictAdd( dict, key );
|
|
|
|
|
|
|
|
/* set it */
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_bencInitStr( child, val, -1 );
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
return child;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-08 03:25:21 +00:00
|
|
|
tr_benc*
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_bencDictAddRaw( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
const void * src,
|
|
|
|
size_t len )
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_benc * child;
|
|
|
|
|
|
|
|
/* see if it already exists, and if so, try to reuse it */
|
|
|
|
if(( child = tr_bencDictFind( dict, key ))) {
|
|
|
|
if( tr_bencIsString( child ) ) {
|
|
|
|
if( stringIsAlloced( child ) )
|
|
|
|
tr_free( child->val.s.str.ptr );
|
|
|
|
} else {
|
|
|
|
tr_bencDictRemove( dict, key );
|
|
|
|
child = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if it doesn't exist, create it */
|
|
|
|
if( child == NULL )
|
|
|
|
child = tr_bencDictAdd( dict, key );
|
|
|
|
|
|
|
|
/* set it */
|
|
|
|
tr_bencInitRaw( child, src, len );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
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*
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_bencDictAddList( tr_benc * dict,
|
2008-09-23 19:11:04 +00:00
|
|
|
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
|
|
|
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_bencInitList( child, reserveCount );
|
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*
|
2010-02-02 22:45:22 +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
|
|
|
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_bencInitDict( child, reserveCount );
|
2008-04-18 16:23:59 +00:00
|
|
|
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 )
|
|
|
|
{
|
2009-06-01 22:15:50 +00:00
|
|
|
indices[j].key = getStr(&val->val.l.vals[i]);
|
2008-01-31 02:24:43 +00:00
|
|
|
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;
|
2009-04-05 17:52:21 +00:00
|
|
|
BencWalkFunc boolFunc;
|
|
|
|
BencWalkFunc realFunc;
|
2008-09-23 19:11:04 +00:00
|
|
|
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
|
2009-04-16 21:01:00 +00:00
|
|
|
bencWalk( const tr_benc * top,
|
|
|
|
const struct WalkFuncs * walkFuncs,
|
|
|
|
void * user_data )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArray stack = TR_PTR_ARRAY_INIT;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArrayAppend( &stack, nodeNew( top ) );
|
2008-01-31 02:24:43 +00:00
|
|
|
|
2008-12-29 08:54:36 +00:00
|
|
|
while( !tr_ptrArrayEmpty( &stack ) )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-12-29 08:54:36 +00:00
|
|
|
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 );
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArrayPop( &stack );
|
2008-01-31 02:24:43 +00:00
|
|
|
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
|
|
|
{
|
2009-04-05 18:02:11 +00:00
|
|
|
case TR_TYPE_INT:
|
2008-09-23 19:11:04 +00:00
|
|
|
walkFuncs->intFunc( val, user_data );
|
|
|
|
break;
|
|
|
|
|
2009-04-05 18:02:11 +00:00
|
|
|
case TR_TYPE_BOOL:
|
2009-04-05 17:52:21 +00:00
|
|
|
walkFuncs->boolFunc( val, user_data );
|
|
|
|
break;
|
|
|
|
|
2009-04-05 18:02:11 +00:00
|
|
|
case TR_TYPE_REAL:
|
2009-04-05 17:52:21 +00:00
|
|
|
walkFuncs->realFunc( val, user_data );
|
|
|
|
break;
|
|
|
|
|
2009-04-05 18:02:11 +00:00
|
|
|
case TR_TYPE_STR:
|
2008-09-23 19:11:04 +00:00
|
|
|
walkFuncs->stringFunc( val, user_data );
|
|
|
|
break;
|
|
|
|
|
2009-04-05 18:02:11 +00:00
|
|
|
case TR_TYPE_LIST:
|
2008-09-23 19:11:04 +00:00
|
|
|
if( val != node->val )
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArrayAppend( &stack, nodeNew( val ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
walkFuncs->listBeginFunc( val, user_data );
|
|
|
|
break;
|
|
|
|
|
2009-04-05 18:02:11 +00:00
|
|
|
case TR_TYPE_DICT:
|
2008-09-23 19:11:04 +00:00
|
|
|
if( val != node->val )
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArrayAppend( &stack, nodeNew( val ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
walkFuncs->dictBeginFunc( val, user_data );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* did caller give us an uninitialized val? */
|
2009-06-23 14:15:23 +00:00
|
|
|
tr_err( "%s", _( "Invalid metadata" ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
|
|
|
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArrayDestruct( &stack, NULL );
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
static void
|
2009-04-16 21:01:00 +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
|
|
|
|
2009-04-05 17:52:21 +00:00
|
|
|
static void
|
|
|
|
saveBoolFunc( const tr_benc * val, void * evbuf )
|
|
|
|
{
|
2009-04-08 17:25:17 +00:00
|
|
|
if( val->val.b )
|
|
|
|
evbuffer_add( evbuf, "i1e", 3 );
|
|
|
|
else
|
|
|
|
evbuffer_add( evbuf, "i0e", 3 );
|
2009-04-05 17:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
saveRealFunc( const tr_benc * val, void * evbuf )
|
|
|
|
{
|
|
|
|
char buf[128];
|
2009-05-09 18:01:29 +00:00
|
|
|
char locale[128];
|
2009-04-05 17:52:21 +00:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* always use a '.' decimal point s.t. locale-hopping doesn't bite us */
|
2009-05-09 18:01:29 +00:00
|
|
|
tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) );
|
2009-04-05 17:52:21 +00:00
|
|
|
setlocale( LC_NUMERIC, "POSIX" );
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%f", val->val.d );
|
|
|
|
setlocale( LC_NUMERIC, locale );
|
|
|
|
|
|
|
|
len = strlen( buf );
|
2009-04-05 23:16:45 +00:00
|
|
|
evbuffer_add_printf( evbuf, "%lu:", (unsigned long)len );
|
2009-04-05 17:52:21 +00:00
|
|
|
evbuffer_add( evbuf, buf, len );
|
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static void
|
2009-04-16 21:01:00 +00:00
|
|
|
saveStringFunc( const tr_benc * val, void * evbuf )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2009-06-01 22:15:50 +00:00
|
|
|
evbuffer_add_printf( evbuf, "%lu:", (unsigned long)val->val.s.len );
|
|
|
|
evbuffer_add( evbuf, getStr(val), val->val.s.len );
|
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
|
2009-04-16 21:01:00 +00:00
|
|
|
saveDictBeginFunc( const tr_benc * val UNUSED, void * evbuf )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2009-04-08 17:25:17 +00:00
|
|
|
evbuffer_add( evbuf, "d", 1 );
|
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
|
2009-04-16 21:01:00 +00:00
|
|
|
saveListBeginFunc( const tr_benc * val UNUSED, void * evbuf )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2009-04-08 17:25:17 +00:00
|
|
|
evbuffer_add( evbuf, "l", 1 );
|
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
|
2009-04-16 21:01:00 +00:00
|
|
|
saveContainerEndFunc( const tr_benc * val UNUSED, void * evbuf )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2009-04-08 17:25:17 +00:00
|
|
|
evbuffer_add( evbuf, "e", 1 );
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-04-16 21:01:00 +00:00
|
|
|
static const struct WalkFuncs saveFuncs = { saveIntFunc,
|
|
|
|
saveBoolFunc,
|
|
|
|
saveRealFunc,
|
|
|
|
saveStringFunc,
|
|
|
|
saveDictBeginFunc,
|
|
|
|
saveListBeginFunc,
|
|
|
|
saveContainerEndFunc };
|
|
|
|
|
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
|
|
|
{
|
2009-06-01 22:15:50 +00:00
|
|
|
if( stringIsAlloced( val ) )
|
|
|
|
tr_ptrArrayAppend( freeme, val->val.s.str.ptr );
|
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
|
|
|
|
2009-04-16 21:01:00 +00:00
|
|
|
static const struct WalkFuncs freeWalkFuncs = { freeDummyFunc,
|
|
|
|
freeDummyFunc,
|
|
|
|
freeDummyFunc,
|
|
|
|
freeStringFunc,
|
|
|
|
freeContainerBeginFunc,
|
|
|
|
freeContainerBeginFunc,
|
|
|
|
freeDummyFunc };
|
|
|
|
|
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
|
|
|
{
|
2009-04-16 21:01:00 +00:00
|
|
|
if( isSomething( val ) )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArray a = TR_PTR_ARRAY_INIT;
|
2009-04-16 21:01:00 +00:00
|
|
|
bencWalk( val, &freeWalkFuncs, &a );
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArrayDestruct( &a, tr_free );
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief Implementation helper class for tr_bencToBuffer(TR_FMT_JSON) */
|
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
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief Implementation helper class for tr_bencToBuffer(TR_FMT_JSON) */
|
2008-05-12 17:54:57 +00:00
|
|
|
struct jsonWalk
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
2009-05-16 05:51:52 +00:00
|
|
|
tr_bool doIndent;
|
|
|
|
tr_list * parents;
|
2008-09-23 19:11:04 +00:00
|
|
|
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 )
|
|
|
|
{
|
2009-05-16 05:51:52 +00:00
|
|
|
if( data->doIndent )
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
const int width = tr_list_size( data->parents ) * 4;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-05-16 05:51:52 +00:00
|
|
|
buf[0] = '\n';
|
|
|
|
memset( buf+1, ' ', width );
|
|
|
|
evbuffer_add( data->out, buf, 1+width );
|
|
|
|
}
|
2008-05-22 16:41:16 +00:00
|
|
|
}
|
|
|
|
|
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 )
|
|
|
|
{
|
2009-04-05 18:02:11 +00:00
|
|
|
case TR_TYPE_DICT:
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-05-12 17:54:57 +00:00
|
|
|
const int i = parentState->childIndex++;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !( i % 2 ) )
|
2009-05-16 15:38:47 +00:00
|
|
|
evbuffer_add( data->out, ": ", data->doIndent ? 2 : 1 );
|
2009-06-02 01:48:48 +00:00
|
|
|
else {
|
|
|
|
const tr_bool isLast = parentState->childIndex == parentState->childCount;
|
|
|
|
if( !isLast ) {
|
|
|
|
evbuffer_add( data->out, ", ", data->doIndent ? 2 : 1 );
|
|
|
|
jsonIndent( data );
|
|
|
|
}
|
2008-05-22 16:41:16 +00:00
|
|
|
}
|
2008-05-12 17:54:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-04-05 18:02:11 +00:00
|
|
|
case TR_TYPE_LIST:
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-06-02 01:48:48 +00:00
|
|
|
const tr_bool isLast = ++parentState->childIndex == parentState->childCount;
|
|
|
|
if( !isLast ) {
|
|
|
|
evbuffer_add( data->out, ", ", data->doIndent ? 2 : 1 );
|
|
|
|
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
|
|
|
|
2009-04-05 17:52:21 +00:00
|
|
|
static void
|
|
|
|
jsonBoolFunc( const tr_benc * val, void * vdata )
|
|
|
|
{
|
|
|
|
struct jsonWalk * data = vdata;
|
|
|
|
|
2009-04-08 17:25:17 +00:00
|
|
|
if( val->val.b )
|
|
|
|
evbuffer_add( data->out, "true", 4 );
|
|
|
|
else
|
|
|
|
evbuffer_add( data->out, "false", 5 );
|
|
|
|
|
2009-04-05 17:52:21 +00:00
|
|
|
jsonChildFunc( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
jsonRealFunc( const tr_benc * val, void * vdata )
|
|
|
|
{
|
|
|
|
struct jsonWalk * data = vdata;
|
2009-05-09 18:01:29 +00:00
|
|
|
char locale[128];
|
2009-04-05 17:52:21 +00:00
|
|
|
|
2010-07-31 14:45:07 +00:00
|
|
|
if( fabs( val->val.d - (int)val->val.d ) < 0.00001 )
|
|
|
|
evbuffer_add_printf( data->out, "%d", (int)val->val.d );
|
2009-05-16 15:38:47 +00:00
|
|
|
else {
|
|
|
|
/* json requires a '.' decimal point regardless of locale */
|
|
|
|
tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) );
|
|
|
|
setlocale( LC_NUMERIC, "POSIX" );
|
2009-08-03 23:18:02 +00:00
|
|
|
evbuffer_add_printf( data->out, "%.4f", tr_truncd( val->val.d, 4 ) );
|
2009-05-16 15:38:47 +00:00
|
|
|
setlocale( LC_NUMERIC, locale );
|
|
|
|
}
|
2009-04-05 17:52:21 +00:00
|
|
|
|
|
|
|
jsonChildFunc( data );
|
|
|
|
}
|
|
|
|
|
2008-03-04 19:29:51 +00:00
|
|
|
static void
|
2009-04-08 17:25:17 +00:00
|
|
|
jsonStringFunc( const tr_benc * val, void * vdata )
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
2009-04-08 17:25:17 +00:00
|
|
|
struct jsonWalk * data = vdata;
|
2009-06-01 22:15:50 +00:00
|
|
|
const unsigned char * it = (const unsigned char *) getStr(val);
|
|
|
|
const unsigned char * end = it + val->val.s.len;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-06-01 22:15:50 +00:00
|
|
|
evbuffer_expand( data->out, val->val.s.len + 2 );
|
2009-04-08 17:25:17 +00:00
|
|
|
evbuffer_add( data->out, "\"", 1 );
|
2009-04-08 18:47:48 +00:00
|
|
|
|
|
|
|
for( ; it!=end; ++it )
|
2008-05-12 17:54:57 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
switch( *it )
|
|
|
|
{
|
2009-04-09 00:26:53 +00:00
|
|
|
case '\b': evbuffer_add( data->out, "\\b", 2 ); break;
|
|
|
|
case '\f': evbuffer_add( data->out, "\\f", 2 ); break;
|
|
|
|
case '\n': evbuffer_add( data->out, "\\n", 2 ); break;
|
|
|
|
case '\r': evbuffer_add( data->out, "\\r", 2 ); break;
|
|
|
|
case '\t': evbuffer_add( data->out, "\\t", 2 ); break;
|
|
|
|
case '"': evbuffer_add( data->out, "\\\"", 2 ); break;
|
|
|
|
case '\\': evbuffer_add( data->out, "\\\\", 2 ); break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-19 23:11:35 +00:00
|
|
|
default:
|
2008-09-23 19:11:04 +00:00
|
|
|
if( isascii( *it ) )
|
2009-04-08 17:25:17 +00:00
|
|
|
evbuffer_add( data->out, it, 1 );
|
2009-04-08 18:47:48 +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 );
|
2009-09-02 15:26:34 +00:00
|
|
|
if((( result==conversionOK ) || (result==targetExhausted)) && (tmp!=it)) {
|
2009-01-10 19:29:01 +00:00
|
|
|
evbuffer_add_printf( data->out, "\\u%04x", (unsigned int)buf );
|
2008-10-06 01:52:29 +00:00
|
|
|
it = tmp - 1;
|
|
|
|
}
|
2008-08-19 23:11:35 +00:00
|
|
|
}
|
2008-05-12 17:54:57 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-08 17:25:17 +00:00
|
|
|
evbuffer_add( data->out, "\"", 1 );
|
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
|
|
|
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 );
|
2009-04-08 17:25:17 +00:00
|
|
|
evbuffer_add( data->out, "{", 1 );
|
2008-05-22 16:41:16 +00:00
|
|
|
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 );
|
2009-04-08 17:25:17 +00:00
|
|
|
evbuffer_add( data->out, "[", 1 );
|
2008-05-22 16:41:16 +00:00
|
|
|
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-05-12 17:54:57 +00:00
|
|
|
struct jsonWalk * data = vdata;
|
2008-09-23 19:11:04 +00:00
|
|
|
int emptyContainer = FALSE;
|
2008-05-22 16:41:16 +00:00
|
|
|
|
|
|
|
jsonPopParent( data );
|
|
|
|
if( !emptyContainer )
|
|
|
|
jsonIndent( data );
|
2008-05-13 12:52:58 +00:00
|
|
|
if( tr_bencIsDict( val ) )
|
2009-04-08 17:25:17 +00:00
|
|
|
evbuffer_add( data->out, "}", 1 );
|
2008-05-13 12:52:58 +00:00
|
|
|
else /* list */
|
2009-04-08 17:25:17 +00:00
|
|
|
evbuffer_add( data->out, "]", 1 );
|
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
|
|
|
|
2009-04-16 21:01:00 +00:00
|
|
|
static const struct WalkFuncs jsonWalkFuncs = { jsonIntFunc,
|
|
|
|
jsonBoolFunc,
|
|
|
|
jsonRealFunc,
|
|
|
|
jsonStringFunc,
|
|
|
|
jsonDictBeginFunc,
|
|
|
|
jsonListBeginFunc,
|
|
|
|
jsonContainerEndFunc };
|
2009-05-29 20:52:42 +00:00
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-02-02 22:45:22 +00:00
|
|
|
static void
|
|
|
|
tr_bencListCopy( tr_benc * target, const tr_benc * src )
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
const tr_benc * val;
|
|
|
|
|
|
|
|
while(( val = tr_bencListChild( (tr_benc*)src, i++ )))
|
|
|
|
{
|
|
|
|
if( tr_bencIsBool( val ) )
|
|
|
|
{
|
|
|
|
tr_bool boolVal = 0;
|
|
|
|
tr_bencGetBool( val, &boolVal );
|
|
|
|
tr_bencListAddBool( target, boolVal );
|
|
|
|
}
|
|
|
|
else if( tr_bencIsReal( val ) )
|
|
|
|
{
|
|
|
|
double realVal = 0;
|
|
|
|
tr_bencGetReal( val, &realVal );
|
|
|
|
tr_bencListAddReal( target, realVal );
|
|
|
|
}
|
|
|
|
else if( tr_bencIsInt( val ) )
|
|
|
|
{
|
|
|
|
int64_t intVal = 0;
|
|
|
|
tr_bencGetInt( val, &intVal );
|
|
|
|
tr_bencListAddInt( target, intVal );
|
|
|
|
}
|
|
|
|
else if( tr_bencIsString( val ) )
|
|
|
|
{
|
|
|
|
tr_bencListAddRaw( target, (const uint8_t*)getStr( val ), val->val.s.len );
|
|
|
|
}
|
|
|
|
else if( tr_bencIsDict( val ) )
|
|
|
|
{
|
|
|
|
tr_bencMergeDicts( tr_bencListAddDict( target, 0 ), val );
|
|
|
|
}
|
|
|
|
else if ( tr_bencIsList( val ) )
|
|
|
|
{
|
|
|
|
tr_bencListCopy( tr_bencListAddList( target, 0 ), val );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_err( "tr_bencListCopy skipping item" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
static size_t
|
|
|
|
tr_bencDictSize( const tr_benc * dict )
|
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
|
|
|
|
if( tr_bencIsDict( dict ) )
|
|
|
|
count = dict->val.l.count / 2;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2009-06-01 18:12:36 +00:00
|
|
|
tr_bool
|
|
|
|
tr_bencDictChild( tr_benc * dict, size_t n, const char ** key, tr_benc ** val )
|
2008-12-12 02:12:17 +00:00
|
|
|
{
|
|
|
|
tr_bool success = 0;
|
|
|
|
|
|
|
|
assert( tr_bencIsDict( dict ) );
|
|
|
|
|
|
|
|
if( tr_bencIsDict( dict ) && (n*2)+1 <= dict->val.l.count )
|
|
|
|
{
|
|
|
|
tr_benc * k = dict->val.l.vals + (n*2);
|
|
|
|
tr_benc * v = dict->val.l.vals + (n*2) + 1;
|
|
|
|
if(( success = tr_bencGetStr( k, key ) && isSomething( v )))
|
|
|
|
*val = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2009-08-12 14:40:32 +00:00
|
|
|
void
|
2008-12-12 02:12:17 +00:00
|
|
|
tr_bencMergeDicts( tr_benc * target, const tr_benc * source )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
const size_t sourceCount = tr_bencDictSize( source );
|
|
|
|
|
|
|
|
assert( tr_bencIsDict( target ) );
|
|
|
|
assert( tr_bencIsDict( source ) );
|
|
|
|
|
|
|
|
for( i=0; i<sourceCount; ++i )
|
|
|
|
{
|
|
|
|
const char * key;
|
2009-06-01 18:12:36 +00:00
|
|
|
tr_benc * val;
|
2009-04-05 23:46:53 +00:00
|
|
|
tr_benc * t;
|
2008-12-12 02:12:17 +00:00
|
|
|
|
2009-06-01 18:12:36 +00:00
|
|
|
if( tr_bencDictChild( (tr_benc*)source, i, &key, &val ) )
|
2008-12-12 02:12:17 +00:00
|
|
|
{
|
2009-04-05 23:46:53 +00:00
|
|
|
if( tr_bencIsBool( val ) )
|
|
|
|
{
|
|
|
|
tr_bool boolVal;
|
|
|
|
tr_bencGetBool( val, &boolVal );
|
|
|
|
tr_bencDictAddBool( target, key, boolVal );
|
|
|
|
}
|
|
|
|
else if( tr_bencIsReal( val ) )
|
|
|
|
{
|
2009-07-17 18:00:29 +00:00
|
|
|
double realVal = 0;
|
2009-04-05 23:46:53 +00:00
|
|
|
tr_bencGetReal( val, &realVal );
|
|
|
|
tr_bencDictAddReal( target, key, realVal );
|
|
|
|
}
|
|
|
|
else if( tr_bencIsInt( val ) )
|
2008-12-12 02:12:17 +00:00
|
|
|
{
|
2009-07-17 18:00:29 +00:00
|
|
|
int64_t intVal = 0;
|
2009-04-05 23:46:53 +00:00
|
|
|
tr_bencGetInt( val, &intVal );
|
|
|
|
tr_bencDictAddInt( target, key, intVal );
|
2008-12-12 02:12:17 +00:00
|
|
|
}
|
2009-04-05 23:46:53 +00:00
|
|
|
else if( tr_bencIsString( val ) )
|
2008-12-12 02:12:17 +00:00
|
|
|
{
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_bencDictAddRaw( target, key, getStr( val ), val->val.s.len );
|
2008-12-12 02:12:17 +00:00
|
|
|
}
|
2008-12-12 02:26:33 +00:00
|
|
|
else if( tr_bencIsDict( val ) && tr_bencDictFindDict( target, key, &t ) )
|
|
|
|
{
|
|
|
|
tr_bencMergeDicts( t, val );
|
|
|
|
}
|
2010-02-02 22:45:22 +00:00
|
|
|
else if( tr_bencIsList( val ) )
|
|
|
|
{
|
|
|
|
if( tr_bencDictFind( target, key ) == NULL )
|
|
|
|
{
|
|
|
|
tr_bencListCopy( tr_bencDictAddList( target, key, tr_bencListSize( val ) ), val );
|
|
|
|
}
|
|
|
|
}
|
2008-12-12 02:12:17 +00:00
|
|
|
else
|
|
|
|
{
|
2008-12-12 02:26:33 +00:00
|
|
|
tr_dbg( "tr_bencMergeDicts skipping \"%s\"", key );
|
2008-12-12 02:12:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-02 01:48:48 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2008-12-12 02:12:17 +00:00
|
|
|
|
2009-06-02 01:48:48 +00:00
|
|
|
void
|
|
|
|
tr_bencToBuf( const tr_benc * top, tr_fmt_mode mode, struct evbuffer * buf )
|
|
|
|
{
|
|
|
|
evbuffer_drain( buf, EVBUFFER_LENGTH( buf ) );
|
2010-02-11 00:53:50 +00:00
|
|
|
evbuffer_expand( buf, 4096 ); /* alloc a little memory to start off with */
|
2009-06-02 01:48:48 +00:00
|
|
|
|
|
|
|
switch( mode )
|
|
|
|
{
|
|
|
|
case TR_FMT_BENC:
|
|
|
|
bencWalk( top, &saveFuncs, buf );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_FMT_JSON:
|
|
|
|
case TR_FMT_JSON_LEAN: {
|
|
|
|
struct jsonWalk data;
|
|
|
|
data.doIndent = mode==TR_FMT_JSON;
|
|
|
|
data.out = buf;
|
|
|
|
data.parents = NULL;
|
|
|
|
bencWalk( top, &jsonWalkFuncs, &data );
|
|
|
|
if( EVBUFFER_LENGTH( buf ) )
|
|
|
|
evbuffer_add_printf( buf, "\n" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
tr_bencToStr( const tr_benc * top, tr_fmt_mode mode, int * len )
|
|
|
|
{
|
|
|
|
char * ret;
|
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
|
|
|
tr_bencToBuf( top, mode, buf );
|
|
|
|
ret = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
|
|
|
|
if( len != NULL )
|
|
|
|
*len = (int) EVBUFFER_LENGTH( buf );
|
|
|
|
evbuffer_free( buf );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-06-22 00:12:52 +00:00
|
|
|
/* portability wrapper for mkstemp(). */
|
|
|
|
static int
|
|
|
|
tr_mkstemp( char * template )
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
const int flags = O_RDWR | O_BINARY | O_CREAT | O_EXCL | _O_SHORT_LIVED;
|
|
|
|
const mode_t mode = _S_IREAD | _S_IWRITE;
|
|
|
|
mktemp( template );
|
|
|
|
return open( template, flags, mode );
|
|
|
|
#else
|
|
|
|
return mkstemp( template );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* portability wrapper for fsync(). */
|
|
|
|
static void
|
|
|
|
tr_fsync( int fd )
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
_commit( fd );
|
|
|
|
#else
|
|
|
|
fsync( fd );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-02 01:48:48 +00:00
|
|
|
int
|
|
|
|
tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename )
|
2008-04-14 11:52:50 +00:00
|
|
|
{
|
2010-02-02 01:05:27 +00:00
|
|
|
char * tmp;
|
|
|
|
int fd;
|
2009-05-29 20:52:42 +00:00
|
|
|
int err = 0;
|
2010-05-06 17:02:31 +00:00
|
|
|
char buf[TR_PATH_MAX];
|
2010-04-29 18:56:34 +00:00
|
|
|
|
|
|
|
/* follow symlinks to find the "real" file, to make sure the temporary
|
2010-06-22 00:12:52 +00:00
|
|
|
* we build with tr_mkstemp() is created on the right partition */
|
2010-06-21 16:44:35 +00:00
|
|
|
if( tr_realpath( filename, buf ) != NULL )
|
2010-04-29 18:56:34 +00:00
|
|
|
filename = buf;
|
2010-02-01 21:09:31 +00:00
|
|
|
|
|
|
|
/* if the file already exists, try to move it out of the way & keep it as a backup */
|
2010-02-02 01:05:27 +00:00
|
|
|
tmp = tr_strdup_printf( "%s.tmp.XXXXXX", filename );
|
2010-06-22 00:12:52 +00:00
|
|
|
fd = tr_mkstemp( tmp );
|
2010-02-02 01:05:27 +00:00
|
|
|
if( fd >= 0 )
|
2008-04-14 11:52:50 +00:00
|
|
|
{
|
2009-06-06 19:30:32 +00:00
|
|
|
int len;
|
|
|
|
char * str = tr_bencToStr( top, mode, &len );
|
2010-02-02 01:05:27 +00:00
|
|
|
tr_dbg( "Writing %d bytes to temporary file \"%s\"", (int)len, tmp );
|
2009-06-03 21:08:54 +00:00
|
|
|
|
2010-02-02 01:05:27 +00:00
|
|
|
if( write( fd, str, len ) == (ssize_t)len )
|
|
|
|
{
|
2010-06-22 00:12:52 +00:00
|
|
|
tr_fsync( fd );
|
2010-02-02 01:05:27 +00:00
|
|
|
close( fd );
|
|
|
|
|
|
|
|
if( !unlink( filename ) || ( errno == ENOENT ) )
|
|
|
|
{
|
|
|
|
tr_dbg( "Renaming \"%s\" as \"%s\"", tmp, filename );
|
|
|
|
|
|
|
|
if( !rename( tmp, filename ) )
|
|
|
|
{
|
|
|
|
tr_inf( _( "Saved \"%s\"" ), filename );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = errno;
|
|
|
|
tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
|
|
|
|
unlink( tmp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = errno;
|
|
|
|
tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
|
|
|
|
unlink( tmp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-06 19:30:32 +00:00
|
|
|
err = errno;
|
2010-02-02 01:05:27 +00:00
|
|
|
tr_err( _( "Couldn't save temporary file \"%1$s\": %2$s" ), tmp, tr_strerror( err ) );
|
|
|
|
close( fd );
|
|
|
|
unlink( tmp );
|
2009-06-02 01:48:48 +00:00
|
|
|
}
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2009-06-06 19:30:32 +00:00
|
|
|
tr_free( str );
|
2009-06-02 01:48:48 +00:00
|
|
|
}
|
2010-02-02 01:05:27 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
err = errno;
|
|
|
|
tr_err( _( "Couldn't save temporary file \"%1$s\": %2$s" ), tmp, tr_strerror( err ) );
|
2010-02-01 21:09:31 +00:00
|
|
|
}
|
|
|
|
|
2010-02-02 01:05:27 +00:00
|
|
|
tr_free( tmp );
|
2008-04-14 20:06:37 +00:00
|
|
|
return err;
|
2008-04-14 11:52:50 +00:00
|
|
|
}
|
|
|
|
|
2008-08-21 14:57:59 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
int
|
2009-06-02 01:48:48 +00:00
|
|
|
tr_bencLoadFile( tr_benc * setme, tr_fmt_mode mode, const char * filename )
|
2008-04-14 11:52:50 +00:00
|
|
|
{
|
2009-06-02 01:48:48 +00:00
|
|
|
int err;
|
|
|
|
size_t contentLen;
|
2008-10-03 04:49:06 +00:00
|
|
|
uint8_t * content;
|
|
|
|
|
|
|
|
content = tr_loadFile( filename, &contentLen );
|
2009-02-18 16:47:27 +00:00
|
|
|
if( !content && errno )
|
2008-10-03 04:49:06 +00:00
|
|
|
err = errno;
|
2009-02-18 16:47:27 +00:00
|
|
|
else if( !content )
|
|
|
|
err = ENODATA;
|
2009-06-02 01:48:48 +00:00
|
|
|
else {
|
|
|
|
if( mode == TR_FMT_BENC )
|
|
|
|
err = tr_bencLoad( content, contentLen, setme, NULL );
|
|
|
|
else
|
|
|
|
err = tr_jsonParse( filename, content, contentLen, setme, 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
|
|
|
}
|
2010-06-22 00:12:52 +00:00
|
|
|
|