-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbencode.cpp
337 lines (256 loc) · 8 KB
/
bencode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/***
*
* BNBT Beta 8.0 - A C++ BitTorrent Tracker
* Copyright (C) 2003-2004 Trevor Hogan
*
* CBTT variations (C) 2003-2005 Harold Feit
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
***/
#include "atom.h"
#include "bencode.h"
string EncodeInt( const CAtomInt &x )
{
char pBuf[128];
memset( pBuf, 0, sizeof( char ) * 128 );
sprintf( pBuf, "%d", x.getValue( ) );
string strDest;
strDest += "i";
strDest += pBuf;
strDest += "e";
return strDest;
}
string EncodeLong( const CAtomLong &x )
{
char pBuf[128];
memset( pBuf, 0, sizeof( char ) * 128 );
#if defined( WIN32 )
sprintf( pBuf, "%I64d", x.getValue( ) );
#elif defined( __FREEBSD__ )
sprintf( pBuf, "%qd", x.getValue( ) );
#else
sprintf( pBuf, "%lld", x.getValue( ) );
#endif
string strDest;
strDest += "i";
strDest += pBuf;
strDest += "e";
return strDest;
}
string EncodeString( const CAtomString &x )
{
char pBuf[128];
memset( pBuf, 0, sizeof( char ) * 128 );
sprintf( pBuf, "%u", (unsigned int)x.getValue( ).size( ) );
string strDest;
strDest += pBuf;
strDest += ":";
strDest += x.getValue( );
return strDest;
}
string EncodeList( const CAtomList &x )
{
vector<CAtom *> *pv = x.getValuePtr( );
string strDest;
strDest += "l";
for( vector<CAtom *> :: iterator i = pv->begin( ); i != pv->end( ); i++ )
{
if( dynamic_cast<CAtomInt *>( *i ) )
strDest += EncodeInt( *dynamic_cast<CAtomInt *>( *i ) );
else if( dynamic_cast<CAtomLong *>( *i ) )
strDest += EncodeLong( *dynamic_cast<CAtomLong *>( *i ) );
else if( dynamic_cast<CAtomString *>( *i ) )
strDest += EncodeString( *dynamic_cast<CAtomString *>( *i ) );
else if( dynamic_cast<CAtomList *>( *i ) )
strDest += EncodeList( *dynamic_cast<CAtomList *>( *i ) );
else if( dynamic_cast<CAtomDicti *>( *i ) )
strDest += EncodeDicti( *dynamic_cast<CAtomDicti *>( *i ) );
}
strDest += "e";
return strDest;
}
string EncodeDicti( const CAtomDicti &x )
{
map<string, CAtom *> *pmapDicti = x.getValuePtr( );
string strDest;
strDest += "d";
for( map<string, CAtom *> :: iterator i = pmapDicti->begin( ); i != pmapDicti->end( ); i++ )
{
strDest += EncodeString( CAtomString( (*i).first ) );
if( dynamic_cast<CAtomInt *>( (*i).second ) )
strDest += EncodeInt( *dynamic_cast<CAtomInt *>( (*i).second ) );
else if( dynamic_cast<CAtomLong *>( (*i).second ) )
strDest += EncodeLong( *dynamic_cast<CAtomLong *>( (*i).second ) );
else if( dynamic_cast<CAtomString *>( (*i).second ) )
strDest += EncodeString( *dynamic_cast<CAtomString *>( (*i).second ) );
else if( dynamic_cast<CAtomList *>( (*i).second ) )
strDest += EncodeList( *dynamic_cast<CAtomList *>( (*i).second ) );
else if( dynamic_cast<CAtomDicti *>( (*i).second ) )
strDest += EncodeDicti( *dynamic_cast<CAtomDicti *>( (*i).second ) );
}
strDest += "e";
return strDest;
}
string Encode( CAtom *pAtom )
{
if( dynamic_cast<CAtomInt *>( pAtom ) )
return EncodeInt( *dynamic_cast<CAtomInt *>( pAtom ) );
else if( dynamic_cast<CAtomLong *>( pAtom ) )
return EncodeLong( *dynamic_cast<CAtomLong *>( pAtom ) );
else if( dynamic_cast<CAtomString *>( pAtom ) )
return EncodeString( *dynamic_cast<CAtomString *>( pAtom ) );
else if( dynamic_cast<CAtomList *>( pAtom ) )
return EncodeList( *dynamic_cast<CAtomList *>( pAtom ) );
else if( dynamic_cast<CAtomDicti *>( pAtom ) )
return EncodeDicti( *dynamic_cast<CAtomDicti *>( pAtom ) );
return string( );
}
/*
CAtomInt *DecodeInt( const string &x, unsigned long iStart )
{
string :: size_type iEnd = x.find( "e" );
if( iEnd == string :: npos )
{
UTIL_LogPrint( "error decoding int - couldn't find \"e\", halting decode\n" );
return NULL;
}
return new CAtomInt( atoi( x.substr( iStart + 1, iEnd - iStart - 1 ).c_str( ) ) );
}
*/
CAtomLong *DecodeLong( const string &x, unsigned long iStart )
{
string :: size_type iEnd = x.find( "e", iStart );
if( iEnd == string :: npos )
{
printf( "error decoding long - couldn't find \"e\", halting decode\n" );
return NULL;
}
int64 i;
#if defined( WIN32 )
sscanf( x.substr( iStart + 1, iEnd - iStart - 1 ).c_str( ), "%I64d", &i );
#elif defined( __FREEBSD__ )
sscanf( x.substr( iStart + 1, iEnd - iStart - 1 ).c_str( ), "%qd", &i );
#else
sscanf( x.substr( iStart + 1, iEnd - iStart - 1 ).c_str( ), "%lld", &i );
#endif
return new CAtomLong( i );
}
CAtomString *DecodeString( const string &x, unsigned long iStart )
{
string :: size_type iSplit = x.find_first_not_of( "1234567890", iStart );
if( iSplit == string :: npos )
{
printf( "error decoding string - couldn't find \":\", halting decode\n" );
return NULL;
}
return new CAtomString( x.substr( iSplit + 1, atoi( x.substr( iStart, iSplit - iStart ).c_str( ) ) ) );
}
CAtomList *DecodeList( const string &x, unsigned long iStart )
{
unsigned long i = iStart + 1;
CAtomList *pList = new CAtomList( );
while( i < x.size( ) && x[i] != 'e' )
{
CAtom *pAtom = Decode( x, i );
if( pAtom )
{
i += pAtom->EncodedLength( );
pList->addItem( pAtom );
}
else
{
printf( "error decoding list - error decoding list item, discarding list\n" );
delete pList;
return NULL;
}
}
return pList;
}
CAtomDicti *DecodeDicti( const string &x, unsigned long iStart )
{
unsigned long i = iStart + 1;
CAtomDicti *pDicti = new CAtomDicti( );
while( i < x.size( ) && x[i] != 'e' )
{
CAtom *pKey = Decode( x, i );
if( pKey && dynamic_cast<CAtomString *>( pKey ) )
{
i += pKey->EncodedLength( );
string strKey = pKey->toString( );
delete pKey;
if( i < x.size( ) )
{
CAtom *pValue = Decode( x, i );
if( pValue )
{
i += pValue->EncodedLength( );
pDicti->setItem( strKey, pValue );
}
else
{
printf( "error decoding dictionary - error decoding value, discarding dictionary\n" );
delete pDicti;
return NULL;
}
}
}
else
{
printf( "error decoding dictionary - error decoding key, discarding dictionary\n" );
delete pDicti;
return NULL;
}
}
return pDicti;
}
CAtom *Decode( const string &x, unsigned long iStart )
{
if( iStart < x.size( ) )
{
if( x[iStart] == 'i' )
return DecodeLong( x, iStart );
else if( isdigit( x[iStart] ) )
return DecodeString( x, iStart );
else if( x[iStart] == 'l' )
return DecodeList( x, iStart );
else if( x[iStart] == 'd' )
return DecodeDicti( x, iStart );
string temp = x.substr( iStart );
printf( "error decoding - found unexpected character %u, halting decode\n", (unsigned char)x[iStart] );
}
else
printf( "error decoding - out of range\n" );
return NULL;
}
CAtom *DecodeFile( const char *szFile )
{
FILE *pFile = NULL;
if( ( pFile = fopen( szFile, "rb" ) ) == NULL )
{
printf( "warning - unable to open %s for reading\n", szFile );
return NULL;
}
fseek( pFile, 0, SEEK_END );
unsigned long ulFileSize = ftell( pFile );
fseek( pFile, 0, SEEK_SET );
char *pData = (char *)malloc( sizeof( char ) * ulFileSize );
memset( pData, 0, sizeof( char ) * ulFileSize );
fread( (void *)pData, sizeof( char ), ulFileSize, pFile );
fclose( pFile );
string strFile( pData, ulFileSize );
free( pData );
return Decode( strFile );
}