2008-08-21 14:57:59 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2008-08-21 14:57:59 +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)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2008-08-21 14:57:59 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
#ifndef TR_BENCODE_H
|
|
|
|
#define TR_BENCODE_H 1
|
|
|
|
|
2007-06-14 11:41:09 +00:00
|
|
|
#include <inttypes.h> /* for int64_t */
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2008-08-21 14:57:59 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
TYPE_INT = 1,
|
|
|
|
TYPE_STR = 2,
|
|
|
|
TYPE_LIST = 4,
|
|
|
|
TYPE_DICT = 8
|
|
|
|
};
|
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
typedef struct tr_benc
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
char type;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
int64_t i;
|
|
|
|
struct
|
|
|
|
{
|
2008-09-04 14:42:32 +00:00
|
|
|
size_t i;
|
2008-02-02 16:09:10 +00:00
|
|
|
char * s;
|
2006-07-16 19:39:23 +00:00
|
|
|
} s;
|
|
|
|
struct
|
|
|
|
{
|
2008-09-04 14:42:32 +00:00
|
|
|
size_t alloc;
|
|
|
|
size_t 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-08-21 14:57:59 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
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,
|
2008-09-04 14:42:32 +00:00
|
|
|
size_t buflen,
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * setme_benc,
|
|
|
|
char ** setme_end );
|
2008-01-31 02:24:43 +00:00
|
|
|
|
2008-08-21 14:57:59 +00:00
|
|
|
int tr_bencLoadFile ( const char * filename, tr_benc * );
|
|
|
|
int tr_bencLoadJSONFile ( const char * filename, tr_benc * );
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
void tr_bencPrint( const tr_benc * );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void tr_bencFree( tr_benc * );
|
2007-03-23 08:28:01 +00:00
|
|
|
|
2008-06-11 20:09:36 +00:00
|
|
|
char* tr_bencSave ( const tr_benc * val, int * len );
|
|
|
|
char* tr_bencSaveAsJSON ( const tr_benc * top, int * len );
|
|
|
|
int tr_bencSaveFile ( const char * filename, const tr_benc * );
|
|
|
|
int tr_bencSaveJSONFile ( const char * filename, const tr_benc * );
|
2008-08-21 14:57:59 +00:00
|
|
|
|
|
|
|
void tr_bencInitStr( tr_benc *, const void * str, int str_len );
|
|
|
|
void tr_bencInitRaw( tr_benc *, const void * raw, size_t raw_len );
|
|
|
|
void tr_bencInitInt( tr_benc *, int64_t num );
|
2008-09-04 14:42:32 +00:00
|
|
|
int tr_bencInitDict( tr_benc *, size_t reserveCount );
|
|
|
|
int tr_bencInitList( tr_benc *, size_t reserveCount );
|
2008-08-21 14:57:59 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-09-04 14:42:32 +00:00
|
|
|
int tr_bencListReserve( tr_benc *, size_t reserveCount );
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_benc * tr_bencListAdd( tr_benc * );
|
|
|
|
tr_benc * tr_bencListAddInt( tr_benc *, int64_t val );
|
|
|
|
tr_benc * tr_bencListAddStr( tr_benc *, const char * val );
|
2008-09-04 14:42:32 +00:00
|
|
|
tr_benc * tr_bencListAddList( tr_benc *, size_t reserveCount );
|
|
|
|
tr_benc * tr_bencListAddDict( tr_benc *, size_t reserveCount );
|
2008-08-21 14:57:59 +00:00
|
|
|
|
2008-09-04 14:42:32 +00:00
|
|
|
size_t tr_bencListSize( const tr_benc * list );
|
|
|
|
tr_benc * tr_bencListChild( tr_benc * list, size_t n );
|
2008-08-21 14:57:59 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-09-04 14:42:32 +00:00
|
|
|
int tr_bencDictReserve( tr_benc *, size_t reserveCount );
|
2008-08-21 14:57:59 +00:00
|
|
|
int tr_bencDictRemove( tr_benc *, const char * key );
|
|
|
|
tr_benc * tr_bencDictAdd( tr_benc *, const char * key );
|
|
|
|
tr_benc * tr_bencDictAddDouble( tr_benc *, const char * key, double d );
|
|
|
|
tr_benc * tr_bencDictAddInt( tr_benc *, const char * key, int64_t val );
|
|
|
|
tr_benc * tr_bencDictAddStr( tr_benc *, const char * key, const char * val );
|
2008-09-04 14:42:32 +00:00
|
|
|
tr_benc * tr_bencDictAddList( tr_benc *, const char * key, size_t reserve );
|
|
|
|
tr_benc * tr_bencDictAddDict( tr_benc *, const char * key, size_t reserve );
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_benc * tr_bencDictAddRaw( tr_benc *, const char * key,
|
|
|
|
const void *, size_t len );
|
|
|
|
|
|
|
|
tr_benc* tr_bencDictFind( tr_benc *, const char * key );
|
|
|
|
int tr_bencDictFindList( tr_benc *, const char * key, tr_benc ** setme );
|
|
|
|
int tr_bencDictFindDict( tr_benc *, const char * key, tr_benc ** setme );
|
|
|
|
int tr_bencDictFindInt( tr_benc *, const char * key, int64_t * setme );
|
|
|
|
int tr_bencDictFindDouble( tr_benc *, const char * key, double * setme );
|
|
|
|
int tr_bencDictFindStr( tr_benc *, const char * key, const char ** setme );
|
|
|
|
int tr_bencDictFindRaw( tr_benc *, const char * key,
|
|
|
|
const uint8_t ** setme_raw, size_t * setme_len );
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2008-03-04 19:29:51 +00:00
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
int tr_bencGetInt( const tr_benc * val, int64_t * setme );
|
2008-05-08 03:25:21 +00:00
|
|
|
int tr_bencGetStr( const tr_benc * val, const char ** setme );
|
2008-04-04 17:19:44 +00:00
|
|
|
int tr_bencIsType( const tr_benc *, int type );
|
2008-08-21 14:57:59 +00:00
|
|
|
#define tr_bencIsInt( b ) tr_bencIsType( ( b ), TYPE_INT )
|
|
|
|
#define tr_bencIsDict( b ) tr_bencIsType( ( b ), TYPE_DICT )
|
|
|
|
#define tr_bencIsList( b ) tr_bencIsType( ( b ), TYPE_LIST )
|
|
|
|
#define tr_bencIsString( b ) tr_bencIsType( ( b ), TYPE_STR )
|
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,
|
2008-08-21 18:40:40 +00:00
|
|
|
const uint8_t ** setme_str,
|
2008-01-30 15:39:41 +00:00
|
|
|
size_t * setme_strlen );
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|