]>
Commit | Line | Data |
---|---|---|
43d991f6 MT |
1 | #!/usr/bin/python |
2 | ||
9068dba1 | 3 | from __future__ import division |
43d991f6 | 4 | |
43d991f6 MT |
5 | ##### This is borrowed from the bittorrent client libary ##### |
6 | ||
7 | def decode_int(x, f): | |
8 | f += 1 | |
9 | newf = x.index('e', f) | |
10 | n = int(x[f:newf]) | |
11 | if x[f] == '-': | |
12 | if x[f + 1] == '0': | |
13 | raise ValueError | |
14 | elif x[f] == '0' and newf != f+1: | |
15 | raise ValueError | |
16 | return (n, newf+1) | |
17 | ||
18 | def decode_string(x, f): | |
19 | colon = x.index(':', f) | |
20 | n = int(x[f:colon]) | |
21 | if x[f] == '0' and colon != f+1: | |
22 | raise ValueError | |
23 | colon += 1 | |
24 | return (x[colon:colon+n], colon+n) | |
25 | ||
26 | def decode_list(x, f): | |
27 | r, f = [], f+1 | |
28 | while x[f] != 'e': | |
29 | v, f = decode_func[x[f]](x, f) | |
30 | r.append(v) | |
31 | return (r, f + 1) | |
32 | ||
33 | def decode_dict(x, f): | |
34 | r, f = {}, f+1 | |
35 | while x[f] != 'e': | |
36 | k, f = decode_string(x, f) | |
37 | r[k], f = decode_func[x[f]](x, f) | |
38 | return (r, f + 1) | |
39 | ||
40 | decode_func = {} | |
41 | decode_func['l'] = decode_list | |
42 | decode_func['d'] = decode_dict | |
43 | decode_func['i'] = decode_int | |
44 | decode_func['0'] = decode_string | |
45 | decode_func['1'] = decode_string | |
46 | decode_func['2'] = decode_string | |
47 | decode_func['3'] = decode_string | |
48 | decode_func['4'] = decode_string | |
49 | decode_func['5'] = decode_string | |
50 | decode_func['6'] = decode_string | |
51 | decode_func['7'] = decode_string | |
52 | decode_func['8'] = decode_string | |
53 | decode_func['9'] = decode_string | |
54 | ||
55 | def bdecode(x): | |
56 | try: | |
57 | r, l = decode_func[x[0]](x, 0) | |
58 | except (IndexError, KeyError, ValueError): | |
59 | raise Exception("not a valid bencoded string") | |
60 | if l != len(x): | |
61 | raise Exception("invalid bencoded value (data after valid prefix)") | |
62 | return r | |
63 | ||
64 | from types import StringType, IntType, LongType, DictType, ListType, TupleType | |
65 | ||
66 | ||
67 | class Bencached(object): | |
68 | ||
69 | __slots__ = ['bencoded'] | |
70 | ||
71 | def __init__(self, s): | |
72 | self.bencoded = s | |
73 | ||
74 | def encode_bencached(x,r): | |
75 | r.append(x.bencoded) | |
76 | ||
77 | def encode_int(x, r): | |
78 | r.extend(('i', str(x), 'e')) | |
79 | ||
80 | def encode_bool(x, r): | |
81 | if x: | |
82 | encode_int(1, r) | |
83 | else: | |
84 | encode_int(0, r) | |
85 | ||
86 | def encode_string(x, r): | |
87 | r.extend((str(len(x)), ':', x)) | |
88 | ||
89 | def encode_list(x, r): | |
90 | r.append('l') | |
91 | for i in x: | |
92 | encode_func[type(i)](i, r) | |
93 | r.append('e') | |
94 | ||
95 | def encode_dict(x,r): | |
96 | r.append('d') | |
97 | ilist = x.items() | |
98 | ilist.sort() | |
99 | for k, v in ilist: | |
100 | r.extend((str(len(k)), ':', k)) | |
101 | encode_func[type(v)](v, r) | |
102 | r.append('e') | |
103 | ||
104 | encode_func = {} | |
105 | encode_func[Bencached] = encode_bencached | |
106 | encode_func[IntType] = encode_int | |
107 | encode_func[LongType] = encode_int | |
108 | encode_func[StringType] = encode_string | |
109 | encode_func[ListType] = encode_list | |
110 | encode_func[TupleType] = encode_list | |
111 | encode_func[DictType] = encode_dict | |
112 | ||
113 | try: | |
114 | from types import BooleanType | |
115 | encode_func[BooleanType] = encode_bool | |
116 | except ImportError: | |
117 | pass | |
118 | ||
119 | def bencode(x): | |
120 | r = [] | |
121 | encode_func[type(x)](x, r) | |
122 | return ''.join(r) |