source: trunk/libtransmission/bencode.h @ 10783

Last change on this file since 10783 was 10783, checked in by charles, 11 years ago

(trunk) #2983: add command-line utilities for creating .torrent files, for editing passkeys, for adding/removing trackers, etc.

  • Property svn:keywords set to Date Rev Author Id
File size: 8.0 KB
Line 
1/*
2 * This file Copyright (C) 2008-2010 Mnemosyne LLC
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: bencode.h 10783 2010-06-16 14:27:24Z charles $
11 */
12
13#ifndef TR_BENCODE_H
14#define TR_BENCODE_H 1
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20#include <inttypes.h> /* for int64_t */
21
22struct evbuffer;
23
24/**
25 * @addtogroup tr_benc Variant
26 *
27 * An object that acts like a union for
28 * integers, strings, lists, dictionaries, booleans, and floating-point numbers.
29 * The structure is named tr_benc due to the historical reason that it was
30 * originally tightly coupled with bencoded data.  It currently supports
31 * being parsed from, and serialized to, both bencoded notation and json notation.
32 *
33 * @{
34 */
35
36/* these are PRIVATE IMPLEMENTATION details that should not be touched.
37 * I'll probably change them just to break your code! HA HA HA!
38 * it's included in the header for inlining and composition */
39enum
40{
41    TR_TYPE_INT  = 1,
42    TR_TYPE_STR  = 2,
43    TR_TYPE_LIST = 4,
44    TR_TYPE_DICT = 8,
45    TR_TYPE_BOOL = 16,
46    TR_TYPE_REAL = 32
47};
48
49/* These are PRIVATE IMPLEMENTATION details that should not be touched.
50 * I'll probably change them just to break your code! HA HA HA!
51 * it's included in the header for inlining and composition */
52typedef struct tr_benc
53{
54    union
55    {
56        uint8_t b; /* bool type */
57
58        double d;  /* double type */
59
60        int64_t i; /* int type */
61
62        struct /* string type */
63        {
64            size_t len; /* the string length */
65            union {
66                char buf[16]; /* local buffer for short strings */
67                char * ptr; /* alloc'ed pointer for long strings */
68            } str;
69        } s;
70
71        struct /* list & dict types */
72        {
73            struct tr_benc * vals; /* nodes */
74            size_t alloc; /* nodes allocated */
75            size_t count; /* nodes used */
76        } l;
77    } val;
78
79    char type;
80} tr_benc;
81
82/***
83****
84***/
85
86int       tr_bencParse( const void     * buf,
87                        const void     * bufend,
88                        tr_benc        * setme_benc,
89                        const uint8_t ** setme_end );
90
91int       tr_bencLoad( const void   * buf,
92                       size_t         buflen,
93                       tr_benc      * setme_benc,
94                       char        ** setme_end );
95
96void      tr_bencFree( tr_benc * );
97
98void      tr_bencInitStr( tr_benc *, const void * str, int str_len );
99
100void      tr_bencInitRaw( tr_benc *, const void * raw, size_t raw_len );
101
102void      tr_bencInitInt( tr_benc *, int64_t num );
103
104int       tr_bencInitDict( tr_benc *, size_t reserveCount );
105
106int       tr_bencInitList( tr_benc *, size_t reserveCount );
107
108void      tr_bencInitBool( tr_benc *, int value );
109
110void      tr_bencInitReal( tr_benc *, double value );
111
112/***
113****  Serialization / Deserialization
114***/
115
116typedef enum
117{
118    TR_FMT_BENC,
119    TR_FMT_JSON,
120    TR_FMT_JSON_LEAN /* saves bandwidth by omitting all whitespace. */
121}
122tr_fmt_mode;
123
124int tr_bencToFile( const tr_benc *, tr_fmt_mode, const char * filename );
125
126char* tr_bencToStr( const tr_benc *, tr_fmt_mode, int * len );
127
128void tr_bencToBuf( const tr_benc *, tr_fmt_mode, struct evbuffer * );
129
130/* TR_FMT_JSON_LEAN and TR_FMT_JSON are equivalent in this function. */
131int tr_bencLoadFile( tr_benc * setme, tr_fmt_mode, const char * filename );
132
133/***
134****
135***/
136
137int tr_bencListReserve( tr_benc *, size_t reserveCount );
138
139tr_benc * tr_bencListAdd( tr_benc * );
140
141tr_benc * tr_bencListAddBool( tr_benc *, tr_bool val );
142
143tr_benc * tr_bencListAddInt( tr_benc *, int64_t val );
144
145tr_benc * tr_bencListAddReal( tr_benc *, double val );
146
147tr_benc * tr_bencListAddStr( tr_benc *, const char * val );
148
149tr_benc * tr_bencListAddRaw( tr_benc *, const uint8_t * val, size_t len );
150
151tr_benc * tr_bencListAddList( tr_benc *, size_t reserveCount );
152
153tr_benc * tr_bencListAddDict( tr_benc *, size_t reserveCount );
154
155size_t    tr_bencListSize( const tr_benc * list );
156
157tr_benc * tr_bencListChild( tr_benc * list, size_t n );
158
159int       tr_bencListRemove( tr_benc *, size_t n );
160
161/***
162****
163***/
164
165int       tr_bencDictReserve( tr_benc *, size_t reserveCount );
166
167int       tr_bencDictRemove( tr_benc *, const char * key );
168
169tr_benc * tr_bencDictAdd( tr_benc *, const char * key );
170
171tr_benc * tr_bencDictAddReal( tr_benc *, const char * key, double );
172
173tr_benc * tr_bencDictAddInt( tr_benc *, const char * key, int64_t );
174
175tr_benc * tr_bencDictAddBool( tr_benc *, const char * key, tr_bool );
176
177tr_benc * tr_bencDictAddStr( tr_benc *, const char * key, const char * );
178
179tr_benc * tr_bencDictAddList( tr_benc *, const char * key, size_t reserve );
180
181tr_benc * tr_bencDictAddDict( tr_benc *, const char * key, size_t reserve );
182
183tr_benc * tr_bencDictAddRaw( tr_benc *, const char * key,
184                             const void * raw, size_t rawlen );
185
186tr_bool   tr_bencDictChild( tr_benc *, size_t i, const char ** key, tr_benc ** val );
187
188tr_benc*  tr_bencDictFind( tr_benc *, const char * key );
189
190tr_bool   tr_bencDictFindList( tr_benc *, const char * key, tr_benc ** setme );
191
192tr_bool   tr_bencDictFindDict( tr_benc *, const char * key, tr_benc ** setme );
193
194tr_bool   tr_bencDictFindInt( tr_benc *, const char * key, int64_t * setme );
195
196tr_bool   tr_bencDictFindReal( tr_benc *, const char * key, double * setme );
197
198tr_bool   tr_bencDictFindBool( tr_benc *, const char * key, tr_bool * setme );
199
200tr_bool   tr_bencDictFindStr( tr_benc *, const char * key, const char ** setme );
201
202tr_bool   tr_bencDictFindRaw( tr_benc *, const char * key,
203                              const uint8_t ** setme_raw, size_t * setme_len );
204
205/***
206****
207***/
208
209/** @brief Get an int64_t from a variant object
210    @return TRUE if successful, or FALSE if the variant could not be represented as an int64_t  */
211tr_bool   tr_bencGetInt( const tr_benc * val, int64_t * setme );
212
213/** @brief Get an string from a variant object
214    @return TRUE if successful, or FALSE if the variant could not be represented as a string  */
215tr_bool   tr_bencGetStr( const tr_benc * val, const char ** setme );
216
217/** @brief Get a boolean from a variant object
218    @return TRUE if successful, or FALSE if the variant could not be represented as a boolean  */
219tr_bool   tr_bencGetBool( const tr_benc * val, tr_bool * setme );
220
221/** @brief Get a floating-point number from a variant object
222    @return TRUE if successful, or FALSE if the variant could not be represented as a floating-point number  */
223tr_bool   tr_bencGetReal( const tr_benc * val, double * setme );
224
225static inline tr_bool tr_bencIsType  ( const tr_benc * b, int type ) { return ( b != NULL ) && ( b->type == type ); }
226
227static inline tr_bool tr_bencIsInt   ( const tr_benc * b ) { return tr_bencIsType( b, TR_TYPE_INT ); }
228static inline tr_bool tr_bencIsDict  ( const tr_benc * b ) { return tr_bencIsType( b, TR_TYPE_DICT ); }
229static inline tr_bool tr_bencIsList  ( const tr_benc * b ) { return tr_bencIsType( b, TR_TYPE_LIST ); }
230static inline tr_bool tr_bencIsString( const tr_benc * b ) { return tr_bencIsType( b, TR_TYPE_STR ); }
231static inline tr_bool tr_bencIsBool  ( const tr_benc * b ) { return tr_bencIsType( b, TR_TYPE_BOOL ); }
232static inline tr_bool tr_bencIsReal  ( const tr_benc * b ) { return tr_bencIsType( b, TR_TYPE_REAL ); }
233
234/** @brief Private function that's exposed here only for unit tests */
235int tr_bencParseInt( const uint8_t *  buf,
236                     const uint8_t *  bufend,
237                     const uint8_t ** setme_end,
238                     int64_t *        setme_val );
239
240/** @brief Private function that's exposed here only for unit tests */
241int tr_bencParseStr( const uint8_t *  buf,
242                     const uint8_t *  bufend,
243                     const uint8_t ** setme_end,
244                     const uint8_t ** setme_str,
245                     size_t *         setme_strlen );
246
247/**
248***
249**/
250
251/* this is only quasi-supported.  don't rely on it too heavily outside of libT */
252void  tr_bencMergeDicts( tr_benc * target, const tr_benc * source );
253
254/* @} */
255
256#ifdef __cplusplus
257}
258#endif
259
260#endif
Note: See TracBrowser for help on using the repository browser.