source: trunk/libtransmission/bencode.h @ 6162

Last change on this file since 6162 was 6162, checked in by charles, 14 years ago

unify the daemon and gtk client's config files so that you can easily swap back and forth between clients and keep the same torrents and preferences.

  • Property svn:keywords set to Date Rev Author Id
File size: 6.1 KB
Line 
1/******************************************************************************
2 * $Id: bencode.h 6162 2008-06-12 16:25:36Z charles $
3 *
4 * Copyright (c) 2005-2008 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#ifndef TR_BENCODE_H
26#define TR_BENCODE_H 1
27
28#include <inttypes.h> /* for int64_t */
29#include <string.h> /* for memset */
30
31typedef struct tr_benc
32{
33#define TYPE_INT  1
34#define TYPE_STR  2
35#define TYPE_LIST 4
36#define TYPE_DICT 8
37    char   type;
38    union
39    {
40        int64_t i;
41        struct
42        {
43            int    i;
44            int    nofree;
45            char * s;
46        } s;
47        struct
48        {
49            int alloc;
50            int count;
51            struct tr_benc * vals;
52        } l;
53    } val;
54} tr_benc;
55
56/* backwards compatability */
57typedef tr_benc benc_val_t;
58
59int tr_bencParse( const void      * buf,
60                  const void      * bufend,
61                  tr_benc         * setme_benc,
62                  const uint8_t  ** setme_end );
63
64int tr_bencLoad( const void  * buf,
65                 int           buflen,
66                 tr_benc     * setme_benc,
67                 char       ** setme_end );
68
69void      tr_bencPrint( const tr_benc * );
70void      tr_bencFree( tr_benc * );
71int       tr_bencDictFindInt( tr_benc * dict, const char * key, int64_t * setme );
72int       tr_bencDictFindDouble( tr_benc * dict, const char * key, double * setme );
73int       tr_bencDictFindStr( tr_benc * dict, const char * key, const char ** setme );
74int       tr_bencDictFindList( tr_benc * dict, const char * key, tr_benc ** setme );
75int       tr_bencDictFindDict( tr_benc * dict, const char * key, tr_benc ** setme );
76tr_benc * tr_bencDictFind( tr_benc * dict, const char * key );
77tr_benc * tr_bencDictFindType( tr_benc * dict, const char * key, int type );
78tr_benc * tr_bencDictFindFirst( tr_benc * dict, ... );
79
80/* convenience functions for building tr_benc    structures */
81
82static inline void tr_bencInit( tr_benc    * val, int type )
83{
84    memset( val, 0, sizeof( *val ) );
85    val->type = type;
86}
87
88#define tr_bencInitStr( a, b, c, d ) \
89    _tr_bencInitStr( (a), ( char * )(b), (c), (d) )
90void   _tr_bencInitStr( tr_benc * val, char * str, int len, int nofree );
91int    tr_bencInitStrDup( tr_benc * val, const char * str );
92void   tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount );
93void   tr_bencInitInt( tr_benc * val, int64_t num );
94int   tr_bencInitDict( tr_benc * val, int reserveCount );
95int   tr_bencInitList( tr_benc * val, int reserveCount );
96int   tr_bencListReserve( tr_benc * list, int count );
97/* note that for one key-value pair, count should be 1, not 2 */
98int   tr_bencDictReserve( tr_benc * dict, int count );
99tr_benc    * tr_bencListAdd( tr_benc  * list );
100tr_benc    * tr_bencListAddInt( tr_benc  * list, int64_t val );
101tr_benc    * tr_bencListAddStr( tr_benc  * list, const char * val );
102tr_benc    * tr_bencListAddList( tr_benc  * list, int reserveCount );
103tr_benc    * tr_bencListAddDict( tr_benc  * list, int reserveCount );
104tr_benc    * tr_bencDictAdd( tr_benc * dict, const char * key );
105tr_benc    * tr_bencDictAddDouble( tr_benc * dict, const char * key, double d );
106tr_benc    * tr_bencDictAddInt( tr_benc * dict, const char * key, int64_t val );
107tr_benc    * tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val );
108tr_benc    * tr_bencDictAddList( tr_benc * dict, const char * key, int reserveCount );
109tr_benc    * tr_bencDictAddDict( tr_benc * dict, const char * key, int reserveCount );
110tr_benc    * tr_bencDictAddRaw( tr_benc * dict, const char * key, const void *, size_t len );
111int          tr_bencDictRemove( tr_benc * dict, const char * key );
112
113char*  tr_bencSave         ( const tr_benc * val, int * len );
114char*  tr_bencSaveAsJSON   ( const tr_benc * top, int * len );
115int    tr_bencSaveFile     ( const char * filename, const tr_benc * );
116int    tr_bencSaveJSONFile ( const char * filename, const tr_benc * );
117int    tr_bencLoadFile     ( const char * filename, tr_benc * );
118int    tr_bencLoadJSONFile ( const char * filename, tr_benc * );
119
120int tr_bencGetInt( const tr_benc * val, int64_t * setme );
121int tr_bencGetStr( const tr_benc * val, const char ** setme );
122
123int tr_bencIsType( const tr_benc *, int type );
124#define tr_bencIsInt(b) (tr_bencIsType(b,TYPE_INT))
125#define tr_bencIsDict(b) (tr_bencIsType(b,TYPE_DICT))
126#define tr_bencIsList(b) (tr_bencIsType(b,TYPE_LIST))
127#define tr_bencIsString(b) (tr_bencIsType(b,TYPE_STR))
128
129/**
130***  Treat these as private -- they're only made public here
131***  so that the unit tests can find them
132**/
133
134int  tr_bencParseInt( const uint8_t  * buf,
135                      const uint8_t  * bufend,
136                      const uint8_t ** setme_end, 
137                      int64_t        * setme_val );
138
139int  tr_bencParseStr( const uint8_t  * buf,
140                      const uint8_t  * bufend,
141                      const uint8_t ** setme_end, 
142                      uint8_t       ** setme_str,
143                      size_t         * setme_strlen );
144
145/**
146***
147**/
148
149int       tr_bencListSize( const tr_benc * list );
150tr_benc * tr_bencListChild( tr_benc * list, int n );
151
152
153#endif
Note: See TracBrowser for help on using the repository browser.