MetaData Sharing
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

342 lines
8.6 KiB

  1. # Written by Petru Paler, Uoti Urpala, Ross Cohen and John Hoffman
  2. # see LICENSE.txt for license information
  3. # LICENSE.txt:
  4. # Unless otherwise noted, all files are released under the MIT
  5. # license, exceptions contain licensing information in them.
  6. #
  7. # Copyright (C) 2001-2002 Bram Cohen
  8. #
  9. # Permission is hereby granted, free of charge, to any person
  10. # obtaining a copy of this software and associated documentation files
  11. # (the "Software"), to deal in the Software without restriction,
  12. # including without limitation the rights to use, copy, modify, merge,
  13. # publish, distribute, sublicense, and/or sell copies of the Software,
  14. # and to permit persons to whom the Software is furnished to do so,
  15. # subject to the following conditions:
  16. #
  17. # The above copyright notice and this permission notice shall be
  18. # included in all copies or substantial portions of the Software.
  19. #
  20. # The Software is provided "AS IS", without warranty of any kind,
  21. # express or implied, including but not limited to the warranties of
  22. # merchantability, fitness for a particular purpose and
  23. # noninfringement. In no event shall the authors or copyright holders
  24. # be liable for any claim, damages or other liability, whether in an
  25. # action of contract, tort or otherwise, arising from, out of or in
  26. # connection with the Software or the use or other dealings in the
  27. # Software.
  28. try:
  29. from types import BooleanType
  30. except ImportError:
  31. BooleanType = None
  32. try:
  33. from types import UnicodeType
  34. except ImportError:
  35. UnicodeType = None
  36. from io import StringIO
  37. def decode_int(x, f):
  38. f += 1
  39. newf = x.index(b'e', f)
  40. n = int(x[f:newf])
  41. if x[f] == b'-'[0]:
  42. if x[f + 1] == b'0'[0]:
  43. raise ValueError
  44. elif x[f] == b'0'[0] and newf != f+1:
  45. raise ValueError
  46. return (n, newf+1)
  47. def decode_string(x, f):
  48. colon = x.index(b':', f)
  49. n = int(x[f:colon])
  50. if x[f] == b'0'[0] and colon != f+1:
  51. raise ValueError
  52. colon += 1
  53. return (x[colon:colon+n], colon+n)
  54. def decode_unicode(x, f):
  55. s, f = decode_string(x, f+1)
  56. return (s.decode('UTF-8'),f)
  57. def decode_list(x, f):
  58. r, f = [], f+1
  59. while x[f] != b'e'[0]:
  60. v, f = decode_func[x[f]](x, f)
  61. r.append(v)
  62. return (r, f + 1)
  63. def decode_dict(x, f):
  64. r, f = {}, f+1
  65. lastkey = ''
  66. while x[f] != b'e'[0]:
  67. k, f = decode_string(x, f)
  68. k = k.decode('us-ascii')
  69. if lastkey >= k:
  70. raise ValueError
  71. lastkey = k
  72. r[k], f = decode_func[x[f]](x, f)
  73. return (r, f + 1)
  74. decode_func = {}
  75. decode_func[b'l'[0]] = decode_list
  76. decode_func[b'd'[0]] = decode_dict
  77. decode_func[b'i'[0]] = decode_int
  78. decode_func[b'0'[0]] = decode_string
  79. decode_func[b'1'[0]] = decode_string
  80. decode_func[b'2'[0]] = decode_string
  81. decode_func[b'3'[0]] = decode_string
  82. decode_func[b'4'[0]] = decode_string
  83. decode_func[b'5'[0]] = decode_string
  84. decode_func[b'6'[0]] = decode_string
  85. decode_func[b'7'[0]] = decode_string
  86. decode_func[b'8'[0]] = decode_string
  87. decode_func[b'9'[0]] = decode_string
  88. #decode_func['u'[0]] = decode_unicode
  89. def bdecode(x, sloppy = 0):
  90. try:
  91. r, l = decode_func[x[0]](x, 0)
  92. # except (IndexError, KeyError):
  93. except (IndexError, KeyError, ValueError):
  94. raise ValueError("bad bencoded data")
  95. if not sloppy and l != len(x):
  96. raise ValueError("bad bencoded data")
  97. return r
  98. def test_bdecode():
  99. try:
  100. bdecode(b'0:0:')
  101. assert 0
  102. except ValueError:
  103. pass
  104. try:
  105. bdecode(b'ie')
  106. assert 0
  107. except ValueError:
  108. pass
  109. try:
  110. bdecode(b'i341foo382e')
  111. assert 0
  112. except ValueError:
  113. pass
  114. assert bdecode(b'i4e') == 4
  115. assert bdecode(b'i0e') == 0
  116. assert bdecode(b'i123456789e') == 123456789
  117. assert bdecode(b'i-10e') == -10
  118. try:
  119. bdecode(b'i-0e')
  120. assert 0
  121. except ValueError:
  122. pass
  123. try:
  124. bdecode(b'i123')
  125. assert 0
  126. except ValueError:
  127. pass
  128. try:
  129. bdecode(b'')
  130. assert 0
  131. except ValueError:
  132. pass
  133. try:
  134. bdecode('')
  135. assert 0
  136. except ValueError:
  137. pass
  138. try:
  139. bdecode(b'i6easd')
  140. assert 0
  141. except ValueError:
  142. pass
  143. try:
  144. bdecode(b'35208734823ljdahflajhdf')
  145. assert 0
  146. except ValueError:
  147. pass
  148. try:
  149. bdecode(b'2:abfdjslhfld')
  150. assert 0
  151. except ValueError:
  152. pass
  153. assert bdecode(b'0:') == b''
  154. assert bdecode(b'3:abc') == b'abc'
  155. assert bdecode(b'10:1234567890') == b'1234567890'
  156. try:
  157. bdecode(b'02:xy')
  158. assert 0
  159. except ValueError:
  160. pass
  161. try:
  162. bdecode(b'l')
  163. assert 0
  164. except ValueError:
  165. pass
  166. assert bdecode(b'le') == []
  167. try:
  168. bdecode(b'leanfdldjfh')
  169. assert 0
  170. except ValueError:
  171. pass
  172. assert bdecode(b'l0:0:0:e') == [b'', b'', b'']
  173. try:
  174. bdecode(b'relwjhrlewjh')
  175. assert 0
  176. except ValueError:
  177. pass
  178. assert bdecode(b'li1ei2ei3ee') == [1, 2, 3]
  179. assert bdecode(b'l3:asd2:xye') == [b'asd', b'xy']
  180. assert bdecode(b'll5:Alice3:Bobeli2ei3eee') == [[b'Alice', b'Bob'], [2, 3]]
  181. try:
  182. bdecode(b'd')
  183. assert 0
  184. except ValueError:
  185. pass
  186. try:
  187. bdecode(b'defoobar')
  188. assert 0
  189. except ValueError:
  190. pass
  191. assert bdecode(b'de') == {}
  192. assert bdecode(b'd3:agei25e4:eyes4:bluee') == {'age': 25, 'eyes': b'blue'}
  193. assert bdecode(b'd8:spam.mp3d6:author5:Alice6:lengthi100000eee') == {'spam.mp3': {'author': b'Alice', 'length': 100000}}
  194. try:
  195. bdecode(b'd3:fooe')
  196. assert 0
  197. except ValueError:
  198. pass
  199. try:
  200. bdecode(b'di1e0:e')
  201. assert 0
  202. except ValueError:
  203. pass
  204. try:
  205. bdecode(b'd1:b0:1:a0:e')
  206. assert 0
  207. except ValueError:
  208. pass
  209. try:
  210. bdecode(b'd1:a0:1:a0:e')
  211. assert 0
  212. except ValueError:
  213. pass
  214. try:
  215. bdecode(b'i03e')
  216. assert 0
  217. except ValueError:
  218. pass
  219. try:
  220. bdecode(b'l01:ae')
  221. assert 0
  222. except ValueError:
  223. pass
  224. try:
  225. bdecode(b'9999:x')
  226. assert 0
  227. except ValueError:
  228. pass
  229. try:
  230. bdecode(b'l0:')
  231. assert 0
  232. except ValueError:
  233. pass
  234. try:
  235. bdecode(b'd0:0:')
  236. assert 0
  237. except ValueError:
  238. pass
  239. try:
  240. bdecode(b'd0:')
  241. assert 0
  242. except ValueError:
  243. pass
  244. bencached_marker = []
  245. class Bencached:
  246. def __init__(self, s):
  247. self.marker = bencached_marker
  248. self.bencoded = s
  249. BencachedType = type(Bencached(b'')) # insufficient, but good as a filter
  250. def encode_bencached(x,r):
  251. assert x.marker == bencached_marker
  252. r.append(x.bencoded)
  253. def encode_int(x,r):
  254. r.append(b'i%de' % x)
  255. def encode_bool(x,r):
  256. encode_int(int(x),r)
  257. def encode_bytes(x,r):
  258. r.extend((b'%d:' % len(x),x))
  259. def encode_string(x,r):
  260. #r.append('u')
  261. encode_bytes(x.encode('UTF-8'),r)
  262. def encode_list(x,r):
  263. r.append(b'l')
  264. for e in x:
  265. encode_func[type(e)](e, r)
  266. r.append(b'e')
  267. def encode_dict(x,r):
  268. r.append(b'd')
  269. for k,v in sorted(x.items()):
  270. r.extend((b'%d:' % len(k),k.encode('UTF-8')))
  271. encode_func[type(v)](v, r)
  272. r.append(b'e')
  273. encode_func = {}
  274. encode_func[BencachedType] = encode_bencached
  275. encode_func[int] = encode_int
  276. encode_func[str] = encode_string
  277. encode_func[list] = encode_list
  278. encode_func[tuple] = encode_list
  279. encode_func[type({})] = encode_dict
  280. if BooleanType:
  281. encode_func[BooleanType] = encode_bool
  282. if UnicodeType:
  283. encode_func[UnicodeType] = encode_unicode
  284. def bencode(x):
  285. r = []
  286. try:
  287. encode_func[type(x)](x, r)
  288. except:
  289. raise ValueError("could not encode type %s (value: %s)" % (type(x), x))
  290. return b''.join(r)
  291. def test_bencode():
  292. assert bencode(4) == b'i4e'
  293. assert bencode(0) == b'i0e'
  294. assert bencode(-10) == b'i-10e'
  295. assert bencode(12345678901234567890) == b'i12345678901234567890e'
  296. assert bencode('') == b'0:'
  297. assert bencode('abc') == b'3:abc'
  298. assert bencode('1234567890') == b'10:1234567890'
  299. assert bencode([]) == b'le'
  300. assert bencode([1, 2, 3]) == b'li1ei2ei3ee'
  301. assert bencode([['Alice', 'Bob'], [2, 3]]) == b'll5:Alice3:Bobeli2ei3eee'
  302. assert bencode({}) == b'de'
  303. assert bencode({'age': 25, 'eyes': 'blue'}) == b'd3:agei25e4:eyes4:bluee'
  304. assert bencode({'spam.mp3': {'author': 'Alice', 'length': 100000}}) == b'd8:spam.mp3d6:author5:Alice6:lengthi100000eee'
  305. try:
  306. bencode({1: 'foo'})
  307. assert 0
  308. except (ValueError, AssertionError):
  309. pass
  310. try:
  311. import psyco
  312. psyco.bind(bdecode)
  313. psyco.bind(bencode)
  314. except ImportError:
  315. pass