1
/******************************************************************************
2
* $Id: bencode.h 6469 2008-08-10 15:15:51Z charles $
4
* Copyright (c) 2005-2008 Transmission authors and contributors
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:
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
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
*****************************************************************************/
2
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
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.
10
* $Id: bencode.h 6795 2008-09-23 19:11:04Z charles $
25
13
#ifndef TR_BENCODE_H
26
14
#define TR_BENCODE_H 1
28
16
#include <inttypes.h> /* for int64_t */
29
#include <string.h> /* for memset */
31
26
typedef struct tr_benc
51
41
struct tr_benc * vals;
56
/* backwards compatability */
57
typedef tr_benc benc_val_t;
59
int tr_bencParse( const void * buf,
62
const uint8_t ** setme_end );
64
int tr_bencLoad( const void * buf,
50
int tr_bencParse( const void * buf,
53
const uint8_t ** setme_end );
55
int tr_bencLoad( const void * buf,
60
int tr_bencLoadFile( const char * filename,
63
int tr_bencLoadJSONFile( const char * filename,
69
67
void tr_bencPrint( const tr_benc * );
70
71
void tr_bencFree( tr_benc * );
71
int tr_bencDictFindInt( tr_benc * dict, const char * key, int64_t * setme );
72
int tr_bencDictFindDouble( tr_benc * dict, const char * key, double * setme );
73
int tr_bencDictFindStr( tr_benc * dict, const char * key, const char ** setme );
74
int tr_bencDictFindRaw( tr_benc * dict, const char * key, const uint8_t ** setme_raw,
76
int tr_bencDictFindList( tr_benc * dict, const char * key, tr_benc ** setme );
77
int tr_bencDictFindDict( tr_benc * dict, const char * key, tr_benc ** setme );
78
tr_benc * tr_bencDictFind( tr_benc * dict, const char * key );
79
tr_benc * tr_bencDictFindType( tr_benc * dict, const char * key, int type );
80
tr_benc * tr_bencDictFindFirst( tr_benc * dict, ... );
82
/* convenience functions for building tr_benc structures */
84
#define tr_bencInitStr( a, b, c, d ) \
85
_tr_bencInitStr( (a), ( char * )(b), (c), (d) )
86
void _tr_bencInitStr( tr_benc * val, char * str, int len, int nofree );
87
int tr_bencInitStrDup( tr_benc * val, const char * str );
88
void tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount );
89
void tr_bencInitInt( tr_benc * val, int64_t num );
90
int tr_bencInitDict( tr_benc * val, int reserveCount );
91
int tr_bencInitList( tr_benc * val, int reserveCount );
92
int tr_bencListReserve( tr_benc * list, int count );
93
/* note that for one key-value pair, count should be 1, not 2 */
94
int tr_bencDictReserve( tr_benc * dict, int count );
95
tr_benc * tr_bencListAdd( tr_benc * list );
96
tr_benc * tr_bencListAddInt( tr_benc * list, int64_t val );
97
tr_benc * tr_bencListAddStr( tr_benc * list, const char * val );
98
tr_benc * tr_bencListAddList( tr_benc * list, int reserveCount );
99
tr_benc * tr_bencListAddDict( tr_benc * list, int reserveCount );
100
tr_benc * tr_bencDictAdd( tr_benc * dict, const char * key );
101
tr_benc * tr_bencDictAddDouble( tr_benc * dict, const char * key, double d );
102
tr_benc * tr_bencDictAddInt( tr_benc * dict, const char * key, int64_t val );
103
tr_benc * tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val );
104
tr_benc * tr_bencDictAddList( tr_benc * dict, const char * key, int reserveCount );
105
tr_benc * tr_bencDictAddDict( tr_benc * dict, const char * key, int reserveCount );
106
tr_benc * tr_bencDictAddRaw( tr_benc * dict, const char * key, const void *, size_t len );
107
int tr_bencDictRemove( tr_benc * dict, const char * key );
109
char* tr_bencSave ( const tr_benc * val, int * len );
110
char* tr_bencSaveAsJSON ( const tr_benc * top, int * len );
111
int tr_bencSaveFile ( const char * filename, const tr_benc * );
112
int tr_bencSaveJSONFile ( const char * filename, const tr_benc * );
113
int tr_bencLoadFile ( const char * filename, tr_benc * );
114
int tr_bencLoadJSONFile ( const char * filename, tr_benc * );
116
int tr_bencGetInt( const tr_benc * val, int64_t * setme );
117
int tr_bencGetStr( const tr_benc * val, const char ** setme );
119
int tr_bencIsType( const tr_benc *, int type );
120
#define tr_bencIsInt(b) (tr_bencIsType(b,TYPE_INT))
121
#define tr_bencIsDict(b) (tr_bencIsType(b,TYPE_DICT))
122
#define tr_bencIsList(b) (tr_bencIsType(b,TYPE_LIST))
123
#define tr_bencIsString(b) (tr_bencIsType(b,TYPE_STR))
73
char* tr_bencSave( const tr_benc * val,
76
char* tr_bencSaveAsJSON( const tr_benc * top,
79
int tr_bencSaveFile( const char * filename,
82
int tr_bencSaveJSONFile( const char * filename,
85
void tr_bencInitStr( tr_benc *,
89
void tr_bencInitRaw( tr_benc *,
93
void tr_bencInitInt( tr_benc *,
96
int tr_bencInitDict( tr_benc *,
97
size_t reserveCount );
99
int tr_bencInitList( tr_benc *,
100
size_t reserveCount );
106
int tr_bencListReserve( tr_benc *,
107
size_t reserveCount );
109
tr_benc * tr_bencListAdd( tr_benc * );
111
tr_benc * tr_bencListAddInt( tr_benc *,
114
tr_benc * tr_bencListAddStr( tr_benc *,
117
tr_benc * tr_bencListAddList( tr_benc *,
118
size_t reserveCount );
120
tr_benc * tr_bencListAddDict( tr_benc *,
121
size_t reserveCount );
123
size_t tr_bencListSize( const tr_benc * list );
125
tr_benc * tr_bencListChild( tr_benc * list,
132
int tr_bencDictReserve( tr_benc *,
133
size_t reserveCount );
135
int tr_bencDictRemove( tr_benc *,
138
tr_benc * tr_bencDictAdd( tr_benc *,
141
tr_benc * tr_bencDictAddDouble( tr_benc *,
145
tr_benc * tr_bencDictAddInt( tr_benc *,
149
tr_benc * tr_bencDictAddStr( tr_benc *,
153
tr_benc * tr_bencDictAddList( tr_benc *,
157
tr_benc * tr_bencDictAddDict( tr_benc *,
161
tr_benc * tr_bencDictAddRaw( tr_benc *,
166
tr_benc* tr_bencDictFind( tr_benc *,
169
int tr_bencDictFindList( tr_benc *,
173
int tr_bencDictFindDict( tr_benc *,
177
int tr_bencDictFindInt( tr_benc *,
181
int tr_bencDictFindDouble( tr_benc *,
185
int tr_bencDictFindStr( tr_benc *,
187
const char ** setme );
189
int tr_bencDictFindRaw( tr_benc *,
191
const uint8_t ** setme_raw,
192
size_t * setme_len );
198
int tr_bencGetInt( const tr_benc * val,
201
int tr_bencGetStr( const tr_benc * val,
202
const char ** setme );
204
int tr_bencIsType( const tr_benc *,
207
#define tr_bencIsInt( b ) tr_bencIsType( ( b ), TYPE_INT )
208
#define tr_bencIsDict( b ) tr_bencIsType( ( b ), TYPE_DICT )
209
#define tr_bencIsList( b ) tr_bencIsType( ( b ), TYPE_LIST )
210
#define tr_bencIsString( b ) tr_bencIsType( ( b ), TYPE_STR )
126
213
*** Treat these as private -- they're only made public here
127
214
*** so that the unit tests can find them
130
int tr_bencParseInt( const uint8_t * buf,
131
const uint8_t * bufend,
132
const uint8_t ** setme_end,
133
int64_t * setme_val );
135
int tr_bencParseStr( const uint8_t * buf,
136
const uint8_t * bufend,
137
const uint8_t ** setme_end,
138
uint8_t ** setme_str,
139
size_t * setme_strlen );
145
int tr_bencListSize( const tr_benc * list );
146
tr_benc * tr_bencListChild( tr_benc * list, int n );
217
int tr_bencParseInt( const uint8_t * buf,
218
const uint8_t * bufend,
219
const uint8_t ** setme_end,
220
int64_t * setme_val );
222
int tr_bencParseStr( const uint8_t * buf,
223
const uint8_t * bufend,
224
const uint8_t ** setme_end,
225
const uint8_t ** setme_str,
226
size_t * setme_strlen );