]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - www/pages/torrent/client/bencode.py
1 # Written by Petru Paler, Uoti Urpala, Ross Cohen and John Hoffman
2 # see LICENSE.txt for license information
4 from types
import IntType
, LongType
, StringType
, ListType
, TupleType
, DictType
6 from types
import BooleanType
10 from types
import UnicodeType
13 from cStringIO
import StringIO
17 newf
= x
.index('e', f
)
25 elif x
[f
] == '0' and newf
!= f
+1:
29 def decode_string(x
, f
):
30 colon
= x
.index(':', f
)
33 except (OverflowError, ValueError):
35 if x
[f
] == '0' and colon
!= f
+1:
38 return (x
[colon
:colon
+n
], colon
+n
)
40 def decode_unicode(x
, f
):
41 s
, f
= decode_string(x
, f
+1)
42 return (s
.decode('UTF-8'),f
)
44 def decode_list(x
, f
):
47 v
, f
= decode_func
[x
[f
]](x
, f
)
51 def decode_dict(x
, f
):
55 k
, f
= decode_string(x
, f
)
59 r
[k
], f
= decode_func
[x
[f
]](x
, f
)
63 decode_func
['l'] = decode_list
64 decode_func
['d'] = decode_dict
65 decode_func
['i'] = decode_int
66 decode_func
['0'] = decode_string
67 decode_func
['1'] = decode_string
68 decode_func
['2'] = decode_string
69 decode_func
['3'] = decode_string
70 decode_func
['4'] = decode_string
71 decode_func
['5'] = decode_string
72 decode_func
['6'] = decode_string
73 decode_func
['7'] = decode_string
74 decode_func
['8'] = decode_string
75 decode_func
['9'] = decode_string
76 #decode_func['u'] = decode_unicode
78 def bdecode(x
, sloppy
= 0):
80 r
, l
= decode_func
[x
[0]](x
, 0)
81 # except (IndexError, KeyError):
82 except (IndexError, KeyError, ValueError):
83 raise ValueError, "bad bencoded data"
84 if not sloppy
and l
!= len(x
):
85 raise ValueError, "bad bencoded data"
100 bdecode('i341foo382e')
104 assert bdecode('i4e') == 4L
105 assert bdecode('i0e') == 0L
106 assert bdecode('i123456789e') == 123456789L
107 assert bdecode('i-10e') == -10L
129 bdecode('35208734823ljdahflajhdf')
134 bdecode('2:abfdjslhfld')
138 assert bdecode('0:') == ''
139 assert bdecode('3:abc') == 'abc'
140 assert bdecode('10:1234567890') == '1234567890'
151 assert bdecode('le') == []
153 bdecode('leanfdldjfh')
157 assert bdecode('l0:0:0:e') == ['', '', '']
159 bdecode('relwjhrlewjh')
163 assert bdecode('li1ei2ei3ee') == [1, 2, 3]
164 assert bdecode('l3:asd2:xye') == ['asd', 'xy']
165 assert bdecode('ll5:Alice3:Bobeli2ei3eee') == [['Alice', 'Bob'], [2, 3]]
176 assert bdecode('de') == {}
177 assert bdecode('d3:agei25e4:eyes4:bluee') == {'age': 25, 'eyes': 'blue'}
178 assert bdecode('d8:spam.mp3d6:author5:Alice6:lengthi100000eee') == {'spam.mp3': {'author': 'Alice', 'length': 100000}}
190 bdecode('d1:b0:1:a0:e')
195 bdecode('d1:a0:1:a0:e')
230 bencached_marker
= []
233 def __init__(self
, s
):
234 self
.marker
= bencached_marker
237 BencachedType
= type(Bencached('')) # insufficient, but good as a filter
239 def encode_bencached(x
,r
):
240 assert x
.marker
== bencached_marker
244 r
.extend(('i',str(x
),'e'))
246 def encode_bool(x
,r
):
249 def encode_string(x
,r
):
250 r
.extend((str(len(x
)),':',x
))
252 def encode_unicode(x
,r
):
254 encode_string(x
.encode('UTF-8'),r
)
256 def encode_list(x
,r
):
259 encode_func
[type(e
)](e
, r
)
262 def encode_dict(x
,r
):
267 r
.extend((str(len(k
)),':',k
))
268 encode_func
[type(v
)](v
, r
)
272 encode_func
[BencachedType
] = encode_bencached
273 encode_func
[IntType
] = encode_int
274 encode_func
[LongType
] = encode_int
275 encode_func
[StringType
] = encode_string
276 encode_func
[ListType
] = encode_list
277 encode_func
[TupleType
] = encode_list
278 encode_func
[DictType
] = encode_dict
280 encode_func
[BooleanType
] = encode_bool
282 encode_func
[UnicodeType
] = encode_unicode
287 encode_func
[type(x
)](x
, r
)
289 print "*** error *** could not encode type %s (value: %s)" % (type(x
), x
)
294 assert bencode(4) == 'i4e'
295 assert bencode(0) == 'i0e'
296 assert bencode(-10) == 'i-10e'
297 assert bencode(12345678901234567890L) == 'i12345678901234567890e'
298 assert bencode('') == '0:'
299 assert bencode('abc') == '3:abc'
300 assert bencode('1234567890') == '10:1234567890'
301 assert bencode([]) == 'le'
302 assert bencode([1, 2, 3]) == 'li1ei2ei3ee'
303 assert bencode([['Alice', 'Bob'], [2, 3]]) == 'll5:Alice3:Bobeli2ei3eee'
304 assert bencode({}) == 'de'
305 assert bencode({'age': 25, 'eyes': 'blue'}) == 'd3:agei25e4:eyes4:bluee'
306 assert bencode({'spam.mp3': {'author': 'Alice', 'length': 100000}}) == 'd8:spam.mp3d6:author5:Alice6:lengthi100000eee'
310 except AssertionError: