source: trunk/libtransmission/bencode.h @ 8561

Last change on this file since 8561 was 8561, checked in by charles, 13 years ago

(trunk libT) re-start work on making libT doxygen friendly. still a long ways to go on this.

  • Property svn:keywords set to Date Rev Author Id
File size: 6.8 KB
Line 
1/*
2 * This file Copyright (C) 2008-2009 Charles Kerr <charles@transmissionbt.com>
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 8561 2009-05-29 19:17:12Z 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 * it's included in the header for inlining and composition */
38enum
39{
40    TR_TYPE_INT  = 1,
41    TR_TYPE_STR  = 2,
42    TR_TYPE_LIST = 4,
43    TR_TYPE_DICT = 8,
44    TR_TYPE_BOOL = 16,
45    TR_TYPE_REAL = 32
46};
47   
48/* these are PRIVATE IMPLEMENTATION details that should not be touched.
49 * it's included in the header for inlining and composition */
50typedef struct tr_benc
51{
52    char type;
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 i; /* the string length */
65            char * s; /* the string */
66        } s;
67
68        struct /* list & dict types */
69        {
70            size_t alloc; /* nodes allocated */
71            size_t count; /* nodes used */
72            struct tr_benc * vals; /* nodes */
73        } l;
74    } val;
75} tr_benc;
76
77/***
78****
79***/
80
81int       tr_bencParse( const void     * buf,
82                        const void     * bufend,
83                        tr_benc        * setme_benc,
84                        const uint8_t ** setme_end );
85
86int       tr_bencLoad( const void   * buf,
87                       size_t         buflen,
88                       tr_benc      * setme_benc,
89                       char        ** setme_end );
90
91int       tr_bencLoadFile( const char * filename, tr_benc * setme );
92
93int       tr_bencLoadJSONFile( const char * filename, tr_benc * setme );
94
95void      tr_bencFree( tr_benc * );
96
97char*     tr_bencSave( const tr_benc * val, int * len );
98
99char*     tr_bencSaveAsJSON( const tr_benc * top, struct evbuffer * out, tr_bool doIndent );
100
101char*     tr_bencToJSON( const tr_benc * top, tr_bool doIndent );
102
103int       tr_bencSaveFile( const char * filename, const tr_benc * );
104
105int       tr_bencSaveJSONFile( const char * filename, const tr_benc * );
106
107void      tr_bencInitStr( tr_benc *, const void * str, int str_len );
108
109void      tr_bencInitRaw( tr_benc *, const void * raw, size_t raw_len );
110
111void      tr_bencInitInt( tr_benc *, int64_t num );
112
113int       tr_bencInitDict( tr_benc *, size_t reserveCount );
114
115int       tr_bencInitList( tr_benc *, size_t reserveCount );
116
117void      tr_bencInitBool( tr_benc *, int value );
118
119void      tr_bencInitReal( tr_benc *, double value );
120
121/***
122****
123***/
124
125int       tr_bencListReserve( tr_benc *, size_t reserveCount );
126
127tr_benc * tr_bencListAdd( tr_benc * );
128
129tr_benc * tr_bencListAddInt( tr_benc *, int64_t val );
130
131tr_benc * tr_bencListAddStr( tr_benc *, const char * val );
132
133tr_benc * tr_bencListAddList( tr_benc *, size_t reserveCount );
134
135tr_benc * tr_bencListAddDict( tr_benc *, size_t reserveCount );
136
137size_t    tr_bencListSize( const tr_benc * list );
138
139tr_benc * tr_bencListChild( tr_benc * list, size_t n );
140
141/***
142****
143***/
144
145int       tr_bencDictReserve( tr_benc *, size_t reserveCount );
146
147int       tr_bencDictRemove( tr_benc *, const char * key );
148
149tr_benc * tr_bencDictAdd( tr_benc *, const char * key );
150
151tr_benc * tr_bencDictAddReal( tr_benc *, const char * key, double );
152
153tr_benc * tr_bencDictAddInt( tr_benc *, const char * key, int64_t );
154
155tr_benc * tr_bencDictAddBool( tr_benc *, const char * key, tr_bool );
156
157tr_benc * tr_bencDictAddStr( tr_benc *, const char * key, const char * );
158
159tr_benc * tr_bencDictAddList( tr_benc *, const char * key, size_t reserve );
160
161tr_benc * tr_bencDictAddDict( tr_benc *, const char * key, size_t reserve );
162
163tr_benc * tr_bencDictAddRaw( tr_benc *, const char * key,
164                             const void * raw, size_t rawlen );
165
166tr_benc*  tr_bencDictFind( tr_benc *, const char * key );
167
168tr_bool   tr_bencDictFindList( tr_benc *, const char * key, tr_benc ** setme );
169
170tr_bool   tr_bencDictFindDict( tr_benc *, const char * key, tr_benc ** setme );
171
172tr_bool   tr_bencDictFindInt( tr_benc *, const char * key, int64_t * setme );
173
174tr_bool   tr_bencDictFindReal( tr_benc *, const char * key, double * setme );
175
176tr_bool   tr_bencDictFindBool( tr_benc *, const char * key, tr_bool * setme );
177
178tr_bool   tr_bencDictFindStr( tr_benc *, const char * key, const char ** setme );
179
180tr_bool   tr_bencDictFindRaw( tr_benc *, const char * key,
181                              const uint8_t ** setme_raw, size_t * setme_len );
182
183/***
184****
185***/
186
187tr_bool   tr_bencGetInt( const tr_benc * val, int64_t * setme );
188tr_bool   tr_bencGetStr( const tr_benc * val, const char ** setme );
189tr_bool   tr_bencGetBool( const tr_benc * val, tr_bool * setme );
190tr_bool   tr_bencGetReal( const tr_benc * val, double * setme );
191
192static TR_INLINE tr_bool tr_bencIsType  ( const tr_benc * b, int type ) { return ( b != NULL ) && ( b->type == type ); }
193static TR_INLINE tr_bool tr_bencIsInt   ( const tr_benc * b ) { return tr_bencIsType( b, TR_TYPE_INT ); }
194static TR_INLINE tr_bool tr_bencIsDict  ( const tr_benc * b ) { return tr_bencIsType( b, TR_TYPE_DICT ); }
195static TR_INLINE tr_bool tr_bencIsList  ( const tr_benc * b ) { return tr_bencIsType( b, TR_TYPE_LIST ); }
196static TR_INLINE tr_bool tr_bencIsString( const tr_benc * b ) { return tr_bencIsType( b, TR_TYPE_STR ); }
197static TR_INLINE tr_bool tr_bencIsBool  ( const tr_benc * b ) { return tr_bencIsType( b, TR_TYPE_BOOL ); }
198static TR_INLINE tr_bool tr_bencIsReal  ( const tr_benc * b ) { return tr_bencIsType( b, TR_TYPE_REAL ); }
199
200/**
201***  Treat these as private -- they're only made public here
202***  so that the unit tests can find them
203**/
204
205int tr_bencParseInt( const uint8_t *  buf,
206                     const uint8_t *  bufend,
207                     const uint8_t ** setme_end,
208                     int64_t *        setme_val );
209
210int tr_bencParseStr( const uint8_t *  buf,
211                     const uint8_t *  bufend,
212                     const uint8_t ** setme_end,
213                     const uint8_t ** setme_str,
214                     size_t *         setme_strlen );
215
216/**
217***
218**/
219
220void  tr_bencMergeDicts( tr_benc * target, const tr_benc * source );
221
222/* @} */
223
224#ifdef __cplusplus
225}
226#endif
227
228#endif
Note: See TracBrowser for help on using the repository browser.