source: trunk/libtransmission/bencode.h @ 6600

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

make tr_bencDictFindType() private.

  • Property svn:keywords set to Date Rev Author Id
File size: 6.1 KB
Line 
1/******************************************************************************
2 * $Id: bencode.h 6600 2008-08-20 18:42:45Z 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_bencDictFindRaw( tr_benc * dict, const char * key, const uint8_t ** setme_raw,
75                                                                size_t * setme_len );
76int       tr_bencDictFindList( tr_benc * dict, const char * key, tr_benc ** setme );
77int       tr_bencDictFindDict( tr_benc * dict, const char * key, tr_benc ** setme );
78tr_benc * tr_bencDictFind( tr_benc * dict, const char * key );
79tr_benc * tr_bencDictFindFirst( tr_benc * dict, ... );
80
81/* convenience functions for building tr_benc    structures */
82
83#define tr_bencInitStr( a, b, c, d ) \
84    _tr_bencInitStr( (a), ( char * )(b), (c), (d) )
85void   _tr_bencInitStr( tr_benc * val, char * str, int len, int nofree );
86int    tr_bencInitStrDup( tr_benc * val, const char * str );
87void   tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount );
88void   tr_bencInitInt( tr_benc * val, int64_t num );
89int   tr_bencInitDict( tr_benc * val, int reserveCount );
90int   tr_bencInitList( tr_benc * val, int reserveCount );
91int   tr_bencListReserve( tr_benc * list, int count );
92/* note that for one key-value pair, count should be 1, not 2 */
93int   tr_bencDictReserve( tr_benc * dict, int count );
94tr_benc    * tr_bencListAdd( tr_benc  * list );
95tr_benc    * tr_bencListAddInt( tr_benc  * list, int64_t val );
96tr_benc    * tr_bencListAddStr( tr_benc  * list, const char * val );
97tr_benc    * tr_bencListAddList( tr_benc  * list, int reserveCount );
98tr_benc    * tr_bencListAddDict( tr_benc  * list, int reserveCount );
99tr_benc    * tr_bencDictAdd( tr_benc * dict, const char * key );
100tr_benc    * tr_bencDictAddDouble( tr_benc * dict, const char * key, double d );
101tr_benc    * tr_bencDictAddInt( tr_benc * dict, const char * key, int64_t val );
102tr_benc    * tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val );
103tr_benc    * tr_bencDictAddList( tr_benc * dict, const char * key, int reserveCount );
104tr_benc    * tr_bencDictAddDict( tr_benc * dict, const char * key, int reserveCount );
105tr_benc    * tr_bencDictAddRaw( tr_benc * dict, const char * key, const void *, size_t len );
106int          tr_bencDictRemove( tr_benc * dict, const char * key );
107
108char*  tr_bencSave         ( const tr_benc * val, int * len );
109char*  tr_bencSaveAsJSON   ( const tr_benc * top, int * len );
110int    tr_bencSaveFile     ( const char * filename, const tr_benc * );
111int    tr_bencSaveJSONFile ( const char * filename, const tr_benc * );
112int    tr_bencLoadFile     ( const char * filename, tr_benc * );
113int    tr_bencLoadJSONFile ( const char * filename, tr_benc * );
114
115int tr_bencGetInt( const tr_benc * val, int64_t * setme );
116int tr_bencGetStr( const tr_benc * val, const char ** setme );
117
118int tr_bencIsType( const tr_benc *, int type );
119#define tr_bencIsInt(b) (tr_bencIsType(b,TYPE_INT))
120#define tr_bencIsDict(b) (tr_bencIsType(b,TYPE_DICT))
121#define tr_bencIsList(b) (tr_bencIsType(b,TYPE_LIST))
122#define tr_bencIsString(b) (tr_bencIsType(b,TYPE_STR))
123
124/**
125***  Treat these as private -- they're only made public here
126***  so that the unit tests can find them
127**/
128
129int  tr_bencParseInt( const uint8_t  * buf,
130                      const uint8_t  * bufend,
131                      const uint8_t ** setme_end, 
132                      int64_t        * setme_val );
133
134int  tr_bencParseStr( const uint8_t  * buf,
135                      const uint8_t  * bufend,
136                      const uint8_t ** setme_end, 
137                      uint8_t       ** setme_str,
138                      size_t         * setme_strlen );
139
140/**
141***
142**/
143
144int       tr_bencListSize( const tr_benc * list );
145tr_benc * tr_bencListChild( tr_benc * list, int n );
146
147
148#endif
Note: See TracBrowser for help on using the repository browser.