1 | /* |
---|
2 | * This file Copyright (C) 2008 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 7552 2008-12-30 22:07:39Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef TR_BENCODE_H |
---|
14 | #define TR_BENCODE_H 1 |
---|
15 | |
---|
16 | #include <inttypes.h> /* for int64_t */ |
---|
17 | |
---|
18 | struct evbuffer; |
---|
19 | |
---|
20 | enum |
---|
21 | { |
---|
22 | TYPE_INT = 1, |
---|
23 | TYPE_STR = 2, |
---|
24 | TYPE_LIST = 4, |
---|
25 | TYPE_DICT = 8 |
---|
26 | }; |
---|
27 | |
---|
28 | typedef struct tr_benc |
---|
29 | { |
---|
30 | char type; |
---|
31 | union |
---|
32 | { |
---|
33 | int64_t i; |
---|
34 | struct |
---|
35 | { |
---|
36 | size_t i; |
---|
37 | char * s; |
---|
38 | } s; |
---|
39 | struct |
---|
40 | { |
---|
41 | size_t alloc; |
---|
42 | size_t count; |
---|
43 | struct tr_benc * vals; |
---|
44 | } l; |
---|
45 | } val; |
---|
46 | } tr_benc; |
---|
47 | |
---|
48 | /*** |
---|
49 | **** |
---|
50 | ***/ |
---|
51 | |
---|
52 | int tr_bencParse( const void * buf, |
---|
53 | const void * bufend, |
---|
54 | tr_benc * setme_benc, |
---|
55 | const uint8_t ** setme_end ); |
---|
56 | |
---|
57 | int tr_bencLoad( const void * buf, |
---|
58 | size_t buflen, |
---|
59 | tr_benc * setme_benc, |
---|
60 | char ** setme_end ); |
---|
61 | |
---|
62 | int tr_bencLoadFile( const char * filename, |
---|
63 | tr_benc * ); |
---|
64 | |
---|
65 | int tr_bencLoadJSONFile( const char * filename, |
---|
66 | tr_benc * ); |
---|
67 | |
---|
68 | #if 0 |
---|
69 | void tr_bencPrint( const tr_benc * ); |
---|
70 | |
---|
71 | #endif |
---|
72 | |
---|
73 | void tr_bencFree( tr_benc * ); |
---|
74 | |
---|
75 | char* tr_bencSave( const tr_benc * val, int * len ); |
---|
76 | |
---|
77 | char* tr_bencSaveAsJSON( const tr_benc * top, struct evbuffer * out ); |
---|
78 | |
---|
79 | int tr_bencSaveFile( const char * filename, const tr_benc * ); |
---|
80 | |
---|
81 | int tr_bencSaveJSONFile( const char * filename, const tr_benc * ); |
---|
82 | |
---|
83 | void tr_bencInitStr( tr_benc *, const void * str, int str_len ); |
---|
84 | |
---|
85 | void tr_bencInitRaw( tr_benc *, const void * raw, size_t raw_len ); |
---|
86 | |
---|
87 | void tr_bencInitInt( tr_benc *, int64_t num ); |
---|
88 | |
---|
89 | int tr_bencInitDict( tr_benc *, size_t reserveCount ); |
---|
90 | |
---|
91 | int tr_bencInitList( tr_benc *, size_t reserveCount ); |
---|
92 | |
---|
93 | /*** |
---|
94 | **** |
---|
95 | ***/ |
---|
96 | |
---|
97 | int tr_bencListReserve( tr_benc *, size_t reserveCount ); |
---|
98 | |
---|
99 | tr_benc * tr_bencListAdd( tr_benc * ); |
---|
100 | |
---|
101 | tr_benc * tr_bencListAddInt( tr_benc *, int64_t val ); |
---|
102 | |
---|
103 | tr_benc * tr_bencListAddStr( tr_benc *, const char * val ); |
---|
104 | |
---|
105 | tr_benc * tr_bencListAddList( tr_benc *, size_t reserveCount ); |
---|
106 | |
---|
107 | tr_benc * tr_bencListAddDict( tr_benc *, size_t reserveCount ); |
---|
108 | |
---|
109 | size_t tr_bencListSize( const tr_benc * list ); |
---|
110 | |
---|
111 | tr_benc * tr_bencListChild( tr_benc * list, size_t n ); |
---|
112 | |
---|
113 | /*** |
---|
114 | **** |
---|
115 | ***/ |
---|
116 | |
---|
117 | int tr_bencDictReserve( tr_benc *, size_t reserveCount ); |
---|
118 | |
---|
119 | int tr_bencDictRemove( tr_benc *, const char * key ); |
---|
120 | |
---|
121 | tr_benc * tr_bencDictAdd( tr_benc *, const char * key ); |
---|
122 | |
---|
123 | tr_benc * tr_bencDictAddDouble( tr_benc *, const char * key, double ); |
---|
124 | |
---|
125 | tr_benc * tr_bencDictAddInt( tr_benc *, const char * key, int64_t ); |
---|
126 | |
---|
127 | tr_benc * tr_bencDictAddStr( tr_benc *, const char * key, const char * ); |
---|
128 | |
---|
129 | tr_benc * tr_bencDictAddList( tr_benc *, const char * key, size_t reserve ); |
---|
130 | |
---|
131 | tr_benc * tr_bencDictAddDict( tr_benc *, const char * key, size_t reserve ); |
---|
132 | |
---|
133 | tr_benc * tr_bencDictAddRaw( tr_benc *, const char * key, |
---|
134 | const void * raw, size_t rawlen ); |
---|
135 | |
---|
136 | tr_benc* tr_bencDictFind( tr_benc *, const char * key ); |
---|
137 | |
---|
138 | tr_bool tr_bencDictFindList( tr_benc *, const char * key, tr_benc ** setme ); |
---|
139 | |
---|
140 | tr_bool tr_bencDictFindDict( tr_benc *, const char * key, tr_benc ** setme ); |
---|
141 | |
---|
142 | tr_bool tr_bencDictFindInt( tr_benc *, const char * key, int64_t * setme ); |
---|
143 | |
---|
144 | tr_bool tr_bencDictFindDouble( tr_benc *, const char * key, double * setme ); |
---|
145 | |
---|
146 | tr_bool tr_bencDictFindStr( tr_benc *, const char * key, const char ** setme ); |
---|
147 | |
---|
148 | tr_bool tr_bencDictFindRaw( tr_benc *, const char * key, |
---|
149 | const uint8_t ** setme_raw, size_t * setme_len ); |
---|
150 | |
---|
151 | /*** |
---|
152 | **** |
---|
153 | ***/ |
---|
154 | |
---|
155 | tr_bool tr_bencGetInt( const tr_benc * val, int64_t * setme ); |
---|
156 | |
---|
157 | tr_bool tr_bencGetStr( const tr_benc * val, const char ** setme ); |
---|
158 | |
---|
159 | tr_bool tr_bencIsType( const tr_benc *, int type ); |
---|
160 | |
---|
161 | |
---|
162 | #define tr_bencIsInt( b ) tr_bencIsType( ( b ), TYPE_INT ) |
---|
163 | #define tr_bencIsDict( b ) tr_bencIsType( ( b ), TYPE_DICT ) |
---|
164 | #define tr_bencIsList( b ) tr_bencIsType( ( b ), TYPE_LIST ) |
---|
165 | #define tr_bencIsString( b ) tr_bencIsType( ( b ), TYPE_STR ) |
---|
166 | |
---|
167 | /** |
---|
168 | *** Treat these as private -- they're only made public here |
---|
169 | *** so that the unit tests can find them |
---|
170 | **/ |
---|
171 | |
---|
172 | int tr_bencParseInt( const uint8_t * buf, |
---|
173 | const uint8_t * bufend, |
---|
174 | const uint8_t ** setme_end, |
---|
175 | int64_t * setme_val ); |
---|
176 | |
---|
177 | int tr_bencParseStr( const uint8_t * buf, |
---|
178 | const uint8_t * bufend, |
---|
179 | const uint8_t ** setme_end, |
---|
180 | const uint8_t ** setme_str, |
---|
181 | size_t * setme_strlen ); |
---|
182 | |
---|
183 | /** |
---|
184 | *** |
---|
185 | **/ |
---|
186 | |
---|
187 | void tr_bencMergeDicts( tr_benc * target, const tr_benc * source ); |
---|
188 | |
---|
189 | #endif |
---|