2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-01-01 17:20:20 +00:00
|
|
|
* Copyright (c) 2005-2008 Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef TR_BENCODE_H
|
|
|
|
#define TR_BENCODE_H 1
|
|
|
|
|
2007-06-14 11:41:09 +00:00
|
|
|
#include <inttypes.h> /* for int64_t */
|
2007-06-06 00:30:13 +00:00
|
|
|
#include <string.h> /* for memset */
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
typedef struct tr_benc
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
#define TYPE_INT 1
|
|
|
|
#define TYPE_STR 2
|
|
|
|
#define TYPE_LIST 4
|
|
|
|
#define TYPE_DICT 8
|
|
|
|
char type;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
int64_t i;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
int i;
|
2007-03-23 08:28:01 +00:00
|
|
|
int nofree;
|
2008-02-02 16:09:10 +00:00
|
|
|
char * s;
|
2006-07-16 19:39:23 +00:00
|
|
|
} s;
|
|
|
|
struct
|
|
|
|
{
|
2008-01-31 02:24:43 +00:00
|
|
|
int alloc;
|
|
|
|
int count;
|
2008-02-26 21:58:58 +00:00
|
|
|
struct tr_benc * vals;
|
2006-07-16 19:39:23 +00:00
|
|
|
} l;
|
|
|
|
} val;
|
2008-02-26 21:58:58 +00:00
|
|
|
} tr_benc;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
/* backwards compatability */
|
|
|
|
typedef tr_benc benc_val_t;
|
2008-01-30 15:39:41 +00:00
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
int tr_bencParse( const void * buf,
|
|
|
|
const void * bufend,
|
|
|
|
tr_benc * setme_benc,
|
|
|
|
const uint8_t ** setme_end );
|
2008-01-31 02:24:43 +00:00
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
int tr_bencLoad( const void * buf,
|
|
|
|
int buflen,
|
|
|
|
tr_benc * setme_benc,
|
|
|
|
char ** setme_end );
|
2008-01-31 02:24:43 +00:00
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
void tr_bencPrint( const tr_benc * );
|
|
|
|
void tr_bencFree( tr_benc * );
|
|
|
|
tr_benc * tr_bencDictFind( tr_benc * dict, const char * key );
|
|
|
|
tr_benc * tr_bencDictFindType( tr_benc * dict, const char * key, int type );
|
|
|
|
tr_benc * tr_bencDictFindFirst( tr_benc * dict, ... );
|
2007-03-23 08:28:01 +00:00
|
|
|
|
|
|
|
/* marks a string as 'do not free' and returns it */
|
2008-02-26 21:58:58 +00:00
|
|
|
char * tr_bencStealStr( tr_benc * val );
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
/* convenience functions for building tr_benc structures */
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
static inline void tr_bencInit( tr_benc * val, int type )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
|
|
|
memset( val, 0, sizeof( *val ) );
|
|
|
|
val->type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define tr_bencInitStr( a, b, c, d ) \
|
|
|
|
_tr_bencInitStr( (a), ( char * )(b), (c), (d) )
|
2008-02-26 21:58:58 +00:00
|
|
|
void _tr_bencInitStr( tr_benc * val, char * str, int len, int nofree );
|
|
|
|
int tr_bencInitStrDup( tr_benc * val, const char * str );
|
|
|
|
void tr_bencInitInt( tr_benc * val, int64_t num );
|
|
|
|
int tr_bencListReserve( tr_benc * list, int count );
|
2007-03-29 21:02:59 +00:00
|
|
|
/* note that for one key-value pair, count should be 1, not 2 */
|
2008-02-26 21:58:58 +00:00
|
|
|
int tr_bencDictReserve( tr_benc * dict, int count );
|
|
|
|
tr_benc * tr_bencListAdd( tr_benc * list );
|
2007-03-29 21:02:59 +00:00
|
|
|
/* note: key must not be freed or modified while val is in use */
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * tr_bencDictAdd( tr_benc * dict, const char * key );
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
char* tr_bencSave( const tr_benc * val, int * len );
|
2008-03-04 19:29:51 +00:00
|
|
|
char* tr_bencSaveAsSerializedPHP( const tr_benc * top, int * len );
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
int64_t tr_bencGetInt( const tr_benc * val );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-01-30 15:39:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*** Treat these as private -- they're only made public here
|
|
|
|
*** so that the unit tests can find them
|
|
|
|
**/
|
|
|
|
|
|
|
|
int tr_bencParseInt( const uint8_t * buf,
|
2008-01-31 02:24:43 +00:00
|
|
|
const uint8_t * bufend,
|
2008-01-30 15:39:41 +00:00
|
|
|
const uint8_t ** setme_end,
|
|
|
|
int64_t * setme_val );
|
|
|
|
|
|
|
|
int tr_bencParseStr( const uint8_t * buf,
|
2008-01-31 02:24:43 +00:00
|
|
|
const uint8_t * bufend,
|
2008-01-30 15:39:41 +00:00
|
|
|
const uint8_t ** setme_end,
|
|
|
|
uint8_t ** setme_str,
|
|
|
|
size_t * setme_strlen );
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * tr_bencListGetNthChild( tr_benc * list, int n );
|
2008-01-30 15:39:41 +00:00
|
|
|
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|