Python module (submodule repositary), which provides content (video streams) from various online stream sources to corresponding Enigma2, Kodi, Plex plugins

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. # This code comes from youtube-dl: https://github.com/rg3/youtube-dl/blob/master/youtube_dl/swfinterp.py
  2. from __future__ import unicode_literals
  3. import collections
  4. import io
  5. import struct
  6. import zlib
  7. def _extract_tags(file_contents):
  8. if file_contents[1:3] != b'WS':
  9. print '[SWFInterpreter] Not an SWF file; header is %r' % file_contents[:3]
  10. if file_contents[:1] == b'C':
  11. content = zlib.decompress(file_contents[8:])
  12. else:
  13. raise NotImplementedError(
  14. 'Unsupported compression format %r' %
  15. file_contents[:1])
  16. # Determine number of bits in framesize rectangle
  17. framesize_nbits = struct.unpack('!B', content[:1])[0] >> 3
  18. framesize_len = (5 + 4 * framesize_nbits + 7) // 8
  19. pos = framesize_len + 2 + 2
  20. while pos < len(content):
  21. header16 = struct.unpack('<H', content[pos:pos + 2])[0]
  22. pos += 2
  23. tag_code = header16 >> 6
  24. tag_len = header16 & 0x3f
  25. if tag_len == 0x3f:
  26. tag_len = struct.unpack('<I', content[pos:pos + 4])[0]
  27. pos += 4
  28. assert pos + tag_len <= len(content), \
  29. ('Tag %d ends at %d+%d - that\'s longer than the file (%d)'
  30. % (tag_code, pos, tag_len, len(content)))
  31. yield (tag_code, content[pos:pos + tag_len])
  32. pos += tag_len
  33. class _AVMClass_Object(object):
  34. def __init__(self, avm_class):
  35. self.avm_class = avm_class
  36. def __repr__(self):
  37. return '%s#%x' % (self.avm_class.name, id(self))
  38. class _ScopeDict(dict):
  39. def __init__(self, avm_class):
  40. super(_ScopeDict, self).__init__()
  41. self.avm_class = avm_class
  42. def __repr__(self):
  43. return '%s__Scope(%s)' % (
  44. self.avm_class.name,
  45. super(_ScopeDict, self).__repr__())
  46. class _AVMClass(object):
  47. def __init__(self, name_idx, name, static_properties=None):
  48. self.name_idx = name_idx
  49. self.name = name
  50. self.method_names = {}
  51. self.method_idxs = {}
  52. self.methods = {}
  53. self.method_pyfunctions = {}
  54. self.static_properties = static_properties if static_properties else {}
  55. self.variables = _ScopeDict(self)
  56. self.constants = {}
  57. def make_object(self):
  58. return _AVMClass_Object(self)
  59. def __repr__(self):
  60. return '_AVMClass(%s)' % (self.name)
  61. def register_methods(self, methods):
  62. self.method_names.update(methods.items())
  63. self.method_idxs.update(dict(
  64. (idx, name)
  65. for name, idx in methods.items()))
  66. class _Multiname(object):
  67. def __init__(self, kind):
  68. self.kind = kind
  69. def __repr__(self):
  70. return '[MULTINAME kind: 0x%x]' % self.kind
  71. def _read_int(reader):
  72. res = 0
  73. shift = 0
  74. for _ in range(5):
  75. buf = reader.read(1)
  76. assert len(buf) == 1
  77. b = struct.unpack('<B', buf)[0]
  78. res = res | ((b & 0x7f) << shift)
  79. if b & 0x80 == 0:
  80. break
  81. shift += 7
  82. return res
  83. def _u30(reader):
  84. res = _read_int(reader)
  85. assert res & 0xf0000000 == 0
  86. return res
  87. _u32 = _read_int
  88. def _s32(reader):
  89. v = _read_int(reader)
  90. if v & 0x80000000 != 0:
  91. v = - ((v ^ 0xffffffff) + 1)
  92. return v
  93. def _s24(reader):
  94. bs = reader.read(3)
  95. assert len(bs) == 3
  96. last_byte = b'\xff' if (ord(bs[2:3]) >= 0x80) else b'\x00'
  97. return struct.unpack('<i', bs + last_byte)[0]
  98. def _read_string(reader):
  99. slen = _u30(reader)
  100. resb = reader.read(slen)
  101. assert len(resb) == slen
  102. return resb.decode('utf-8')
  103. def _read_bytes(count, reader):
  104. assert count >= 0
  105. resb = reader.read(count)
  106. assert len(resb) == count
  107. return resb
  108. def _read_byte(reader):
  109. resb = _read_bytes(1, reader=reader)
  110. res = struct.unpack('<B', resb)[0]
  111. return res
  112. StringClass = _AVMClass('(no name idx)', 'String')
  113. ByteArrayClass = _AVMClass('(no name idx)', 'ByteArray')
  114. TimerClass = _AVMClass('(no name idx)', 'Timer')
  115. TimerEventClass = _AVMClass('(no name idx)', 'TimerEvent', {'TIMER': 'timer'})
  116. _builtin_classes = {
  117. StringClass.name: StringClass,
  118. ByteArrayClass.name: ByteArrayClass,
  119. TimerClass.name: TimerClass,
  120. TimerEventClass.name: TimerEventClass,
  121. }
  122. class _Undefined(object):
  123. def __bool__(self):
  124. return False
  125. __nonzero__ = __bool__
  126. def __hash__(self):
  127. return 0
  128. def __str__(self):
  129. return 'undefined'
  130. __repr__ = __str__
  131. undefined = _Undefined()
  132. class SWFInterpreter(object):
  133. def __init__(self, file_contents):
  134. self._patched_functions = {
  135. (TimerClass, 'addEventListener'): lambda params: undefined,
  136. }
  137. code_tag = next(tag
  138. for tag_code, tag in _extract_tags(file_contents)
  139. if tag_code == 82)
  140. p = code_tag.index(b'\0', 4) + 1
  141. code_reader = io.BytesIO(code_tag[p:])
  142. # Parse ABC (AVM2 ByteCode)
  143. # Define a couple convenience methods
  144. u30 = lambda *args: _u30(*args, reader=code_reader)
  145. s32 = lambda *args: _s32(*args, reader=code_reader)
  146. u32 = lambda *args: _u32(*args, reader=code_reader)
  147. read_bytes = lambda *args: _read_bytes(*args, reader=code_reader)
  148. read_byte = lambda *args: _read_byte(*args, reader=code_reader)
  149. # minor_version + major_version
  150. read_bytes(2 + 2)
  151. # Constant pool
  152. int_count = u30()
  153. self.constant_ints = [0]
  154. for _c in range(1, int_count):
  155. self.constant_ints.append(s32())
  156. self.constant_uints = [0]
  157. uint_count = u30()
  158. for _c in range(1, uint_count):
  159. self.constant_uints.append(u32())
  160. double_count = u30()
  161. read_bytes(max(0, (double_count - 1)) * 8)
  162. string_count = u30()
  163. self.constant_strings = ['']
  164. for _c in range(1, string_count):
  165. s = _read_string(code_reader)
  166. self.constant_strings.append(s)
  167. namespace_count = u30()
  168. for _c in range(1, namespace_count):
  169. read_bytes(1) # kind
  170. u30() # name
  171. ns_set_count = u30()
  172. for _c in range(1, ns_set_count):
  173. count = u30()
  174. for _c2 in range(count):
  175. u30()
  176. multiname_count = u30()
  177. MULTINAME_SIZES = {
  178. 0x07: 2, # QName
  179. 0x0d: 2, # QNameA
  180. 0x0f: 1, # RTQName
  181. 0x10: 1, # RTQNameA
  182. 0x11: 0, # RTQNameL
  183. 0x12: 0, # RTQNameLA
  184. 0x09: 2, # Multiname
  185. 0x0e: 2, # MultinameA
  186. 0x1b: 1, # MultinameL
  187. 0x1c: 1, # MultinameLA
  188. }
  189. self.multinames = ['']
  190. for _c in range(1, multiname_count):
  191. kind = u30()
  192. assert kind in MULTINAME_SIZES, 'Invalid multiname kind %r' % kind
  193. if kind == 0x07:
  194. u30() # namespace_idx
  195. name_idx = u30()
  196. self.multinames.append(self.constant_strings[name_idx])
  197. elif kind == 0x09:
  198. name_idx = u30()
  199. u30()
  200. self.multinames.append(self.constant_strings[name_idx])
  201. else:
  202. self.multinames.append(_Multiname(kind))
  203. for _c2 in range(MULTINAME_SIZES[kind]):
  204. u30()
  205. # Methods
  206. method_count = u30()
  207. MethodInfo = collections.namedtuple(
  208. 'MethodInfo',
  209. ['NEED_ARGUMENTS', 'NEED_REST'])
  210. method_infos = []
  211. for method_id in range(method_count):
  212. param_count = u30()
  213. u30() # return type
  214. for _ in range(param_count):
  215. u30() # param type
  216. u30() # name index (always 0 for youtube)
  217. flags = read_byte()
  218. if flags & 0x08 != 0:
  219. # Options present
  220. option_count = u30()
  221. for c in range(option_count):
  222. u30() # val
  223. read_bytes(1) # kind
  224. if flags & 0x80 != 0:
  225. # Param names present
  226. for _ in range(param_count):
  227. u30() # param name
  228. mi = MethodInfo(flags & 0x01 != 0, flags & 0x04 != 0)
  229. method_infos.append(mi)
  230. # Metadata
  231. metadata_count = u30()
  232. for _c in range(metadata_count):
  233. u30() # name
  234. item_count = u30()
  235. for _c2 in range(item_count):
  236. u30() # key
  237. u30() # value
  238. def parse_traits_info():
  239. trait_name_idx = u30()
  240. kind_full = read_byte()
  241. kind = kind_full & 0x0f
  242. attrs = kind_full >> 4
  243. methods = {}
  244. constants = None
  245. if kind == 0x00: # Slot
  246. u30() # Slot id
  247. u30() # type_name_idx
  248. vindex = u30()
  249. if vindex != 0:
  250. read_byte() # vkind
  251. elif kind == 0x06: # Const
  252. u30() # Slot id
  253. u30() # type_name_idx
  254. vindex = u30()
  255. vkind = 'any'
  256. if vindex != 0:
  257. vkind = read_byte()
  258. if vkind == 0x03: # Constant_Int
  259. value = self.constant_ints[vindex]
  260. elif vkind == 0x04: # Constant_UInt
  261. value = self.constant_uints[vindex]
  262. else:
  263. return {}, None # Ignore silently for now
  264. constants = {self.multinames[trait_name_idx]: value}
  265. elif kind in (0x01, 0x02, 0x03): # Method / Getter / Setter
  266. u30() # disp_id
  267. method_idx = u30()
  268. methods[self.multinames[trait_name_idx]] = method_idx
  269. elif kind == 0x04: # Class
  270. u30() # slot_id
  271. u30() # classi
  272. elif kind == 0x05: # Function
  273. u30() # slot_id
  274. function_idx = u30()
  275. methods[function_idx] = self.multinames[trait_name_idx]
  276. else:
  277. print '[SWFInterpreter] Unsupported trait kind %d' % kind
  278. return None
  279. if attrs & 0x4 != 0: # Metadata present
  280. metadata_count = u30()
  281. for _c3 in range(metadata_count):
  282. u30() # metadata index
  283. return methods, constants
  284. # Classes
  285. class_count = u30()
  286. classes = []
  287. for class_id in range(class_count):
  288. name_idx = u30()
  289. cname = self.multinames[name_idx]
  290. avm_class = _AVMClass(name_idx, cname)
  291. classes.append(avm_class)
  292. u30() # super_name idx
  293. flags = read_byte()
  294. if flags & 0x08 != 0: # Protected namespace is present
  295. u30() # protected_ns_idx
  296. intrf_count = u30()
  297. for _c2 in range(intrf_count):
  298. u30()
  299. u30() # iinit
  300. trait_count = u30()
  301. for _c2 in range(trait_count):
  302. trait_methods, trait_constants = parse_traits_info()
  303. avm_class.register_methods(trait_methods)
  304. if trait_constants:
  305. avm_class.constants.update(trait_constants)
  306. assert len(classes) == class_count
  307. self._classes_by_name = dict((c.name, c) for c in classes)
  308. for avm_class in classes:
  309. avm_class.cinit_idx = u30()
  310. trait_count = u30()
  311. for _c2 in range(trait_count):
  312. trait_methods, trait_constants = parse_traits_info()
  313. avm_class.register_methods(trait_methods)
  314. if trait_constants:
  315. avm_class.constants.update(trait_constants)
  316. # Scripts
  317. script_count = u30()
  318. for _c in range(script_count):
  319. u30() # init
  320. trait_count = u30()
  321. for _c2 in range(trait_count):
  322. parse_traits_info()
  323. # Method bodies
  324. method_body_count = u30()
  325. Method = collections.namedtuple('Method', ['code', 'local_count'])
  326. self._all_methods = []
  327. for _c in range(method_body_count):
  328. method_idx = u30()
  329. u30() # max_stack
  330. local_count = u30()
  331. u30() # init_scope_depth
  332. u30() # max_scope_depth
  333. code_length = u30()
  334. code = read_bytes(code_length)
  335. m = Method(code, local_count)
  336. self._all_methods.append(m)
  337. for avm_class in classes:
  338. if method_idx in avm_class.method_idxs:
  339. avm_class.methods[avm_class.method_idxs[method_idx]] = m
  340. exception_count = u30()
  341. for _c2 in range(exception_count):
  342. u30() # from
  343. u30() # to
  344. u30() # target
  345. u30() # exc_type
  346. u30() # var_name
  347. trait_count = u30()
  348. for _c2 in range(trait_count):
  349. parse_traits_info()
  350. assert p + code_reader.tell() == len(code_tag)
  351. def patch_function(self, avm_class, func_name, f):
  352. self._patched_functions[(avm_class, func_name)] = f
  353. def extract_class(self, class_name, call_cinit=True):
  354. try:
  355. res = self._classes_by_name[class_name]
  356. except KeyError:
  357. print '[SWFInterpreter] Class %r not found' % class_name
  358. return None
  359. if call_cinit and hasattr(res, 'cinit_idx'):
  360. res.register_methods({'$cinit': res.cinit_idx})
  361. res.methods['$cinit'] = self._all_methods[res.cinit_idx]
  362. cinit = self.extract_function(res, '$cinit')
  363. cinit([])
  364. return res
  365. def extract_function(self, avm_class, func_name):
  366. p = self._patched_functions.get((avm_class, func_name))
  367. if p:
  368. return p
  369. if func_name in avm_class.method_pyfunctions:
  370. return avm_class.method_pyfunctions[func_name]
  371. if func_name in self._classes_by_name:
  372. return self._classes_by_name[func_name].make_object()
  373. if func_name not in avm_class.methods:
  374. print '[SWFInterpreter] Cannot find function %s.%s' % (
  375. avm_class.name, func_name)
  376. return None
  377. m = avm_class.methods[func_name]
  378. def resfunc(args):
  379. # Helper functions
  380. coder = io.BytesIO(m.code)
  381. s24 = lambda: _s24(coder)
  382. u30 = lambda: _u30(coder)
  383. registers = [avm_class.variables] + list(args) + [None] * m.local_count
  384. stack = []
  385. scopes = collections.deque([
  386. self._classes_by_name, avm_class.constants, avm_class.variables])
  387. while True:
  388. opcode = _read_byte(coder)
  389. if opcode == 9: # label
  390. pass # Spec says: "Do nothing."
  391. elif opcode == 16: # jump
  392. offset = s24()
  393. coder.seek(coder.tell() + offset)
  394. elif opcode == 17: # iftrue
  395. offset = s24()
  396. value = stack.pop()
  397. if value:
  398. coder.seek(coder.tell() + offset)
  399. elif opcode == 18: # iffalse
  400. offset = s24()
  401. value = stack.pop()
  402. if not value:
  403. coder.seek(coder.tell() + offset)
  404. elif opcode == 19: # ifeq
  405. offset = s24()
  406. value2 = stack.pop()
  407. value1 = stack.pop()
  408. if value2 == value1:
  409. coder.seek(coder.tell() + offset)
  410. elif opcode == 20: # ifne
  411. offset = s24()
  412. value2 = stack.pop()
  413. value1 = stack.pop()
  414. if value2 != value1:
  415. coder.seek(coder.tell() + offset)
  416. elif opcode == 21: # iflt
  417. offset = s24()
  418. value2 = stack.pop()
  419. value1 = stack.pop()
  420. if value1 < value2:
  421. coder.seek(coder.tell() + offset)
  422. elif opcode == 32: # pushnull
  423. stack.append(None)
  424. elif opcode == 33: # pushundefined
  425. stack.append(undefined)
  426. elif opcode == 36: # pushbyte
  427. v = _read_byte(coder)
  428. stack.append(v)
  429. elif opcode == 37: # pushshort
  430. v = u30()
  431. stack.append(v)
  432. elif opcode == 38: # pushtrue
  433. stack.append(True)
  434. elif opcode == 39: # pushfalse
  435. stack.append(False)
  436. elif opcode == 40: # pushnan
  437. stack.append(float('NaN'))
  438. elif opcode == 42: # dup
  439. value = stack[-1]
  440. stack.append(value)
  441. elif opcode == 44: # pushstring
  442. idx = u30()
  443. stack.append(self.constant_strings[idx])
  444. elif opcode == 48: # pushscope
  445. new_scope = stack.pop()
  446. scopes.append(new_scope)
  447. elif opcode == 66: # construct
  448. arg_count = u30()
  449. args = list(reversed(
  450. [stack.pop() for _ in range(arg_count)]))
  451. obj = stack.pop()
  452. res = obj.avm_class.make_object()
  453. stack.append(res)
  454. elif opcode == 70: # callproperty
  455. index = u30()
  456. mname = self.multinames[index]
  457. arg_count = u30()
  458. args = list(reversed(
  459. [stack.pop() for _ in range(arg_count)]))
  460. obj = stack.pop()
  461. if obj == StringClass:
  462. if mname == 'String':
  463. assert len(args) == 1
  464. assert isinstance(args[0], (
  465. int, unicode, _Undefined))
  466. if args[0] == undefined:
  467. res = 'undefined'
  468. else:
  469. res = unicode(args[0])
  470. stack.append(res)
  471. continue
  472. else:
  473. raise NotImplementedError(
  474. 'Function String.%s is not yet implemented'
  475. % mname)
  476. elif isinstance(obj, _AVMClass_Object):
  477. func = self.extract_function(obj.avm_class, mname)
  478. res = func(args)
  479. stack.append(res)
  480. continue
  481. elif isinstance(obj, _AVMClass):
  482. func = self.extract_function(obj, mname)
  483. res = func(args)
  484. stack.append(res)
  485. continue
  486. elif isinstance(obj, _ScopeDict):
  487. if mname in obj.avm_class.method_names:
  488. func = self.extract_function(obj.avm_class, mname)
  489. res = func(args)
  490. else:
  491. res = obj[mname]
  492. stack.append(res)
  493. continue
  494. elif isinstance(obj, unicode):
  495. if mname == 'split':
  496. assert len(args) == 1
  497. assert isinstance(args[0], unicode)
  498. if args[0] == '':
  499. res = list(obj)
  500. else:
  501. res = obj.split(args[0])
  502. stack.append(res)
  503. continue
  504. elif mname == 'charCodeAt':
  505. assert len(args) <= 1
  506. idx = 0 if len(args) == 0 else args[0]
  507. assert isinstance(idx, int)
  508. res = ord(obj[idx])
  509. stack.append(res)
  510. continue
  511. elif isinstance(obj, list):
  512. if mname == 'slice':
  513. assert len(args) == 1
  514. assert isinstance(args[0], int)
  515. res = obj[args[0]:]
  516. stack.append(res)
  517. continue
  518. elif mname == 'join':
  519. assert len(args) == 1
  520. assert isinstance(args[0], unicode)
  521. res = args[0].join(obj)
  522. stack.append(res)
  523. continue
  524. raise NotImplementedError(
  525. 'Unsupported property %r on %r'
  526. % (mname, obj))
  527. elif opcode == 71: # returnvoid
  528. res = undefined
  529. return res
  530. elif opcode == 72: # returnvalue
  531. res = stack.pop()
  532. return res
  533. elif opcode == 73: # constructsuper
  534. # Not yet implemented, just hope it works without it
  535. arg_count = u30()
  536. args = list(reversed(
  537. [stack.pop() for _ in range(arg_count)]))
  538. obj = stack.pop()
  539. elif opcode == 74: # constructproperty
  540. index = u30()
  541. arg_count = u30()
  542. args = list(reversed(
  543. [stack.pop() for _ in range(arg_count)]))
  544. obj = stack.pop()
  545. mname = self.multinames[index]
  546. assert isinstance(obj, _AVMClass)
  547. # We do not actually call the constructor for now;
  548. # we just pretend it does nothing
  549. stack.append(obj.make_object())
  550. elif opcode == 79: # callpropvoid
  551. index = u30()
  552. mname = self.multinames[index]
  553. arg_count = u30()
  554. args = list(reversed(
  555. [stack.pop() for _ in range(arg_count)]))
  556. obj = stack.pop()
  557. if isinstance(obj, _AVMClass_Object):
  558. func = self.extract_function(obj.avm_class, mname)
  559. res = func(args)
  560. assert res is undefined
  561. continue
  562. if isinstance(obj, _ScopeDict):
  563. assert mname in obj.avm_class.method_names
  564. func = self.extract_function(obj.avm_class, mname)
  565. res = func(args)
  566. assert res is undefined
  567. continue
  568. if mname == 'reverse':
  569. assert isinstance(obj, list)
  570. obj.reverse()
  571. else:
  572. raise NotImplementedError(
  573. 'Unsupported (void) property %r on %r'
  574. % (mname, obj))
  575. elif opcode == 86: # newarray
  576. arg_count = u30()
  577. arr = []
  578. for i in range(arg_count):
  579. arr.append(stack.pop())
  580. arr = arr[::-1]
  581. stack.append(arr)
  582. elif opcode == 93: # findpropstrict
  583. index = u30()
  584. mname = self.multinames[index]
  585. for s in reversed(scopes):
  586. if mname in s:
  587. res = s
  588. break
  589. else:
  590. res = scopes[0]
  591. if mname not in res and mname in _builtin_classes:
  592. stack.append(_builtin_classes[mname])
  593. else:
  594. stack.append(res[mname])
  595. elif opcode == 94: # findproperty
  596. index = u30()
  597. mname = self.multinames[index]
  598. for s in reversed(scopes):
  599. if mname in s:
  600. res = s
  601. break
  602. else:
  603. res = avm_class.variables
  604. stack.append(res)
  605. elif opcode == 96: # getlex
  606. index = u30()
  607. mname = self.multinames[index]
  608. for s in reversed(scopes):
  609. if mname in s:
  610. scope = s
  611. break
  612. else:
  613. scope = avm_class.variables
  614. if mname in scope:
  615. res = scope[mname]
  616. elif mname in _builtin_classes:
  617. res = _builtin_classes[mname]
  618. else:
  619. # Assume unitialized
  620. # TODO warn here
  621. res = undefined
  622. stack.append(res)
  623. elif opcode == 97: # setproperty
  624. index = u30()
  625. value = stack.pop()
  626. idx = self.multinames[index]
  627. if isinstance(idx, _Multiname):
  628. idx = stack.pop()
  629. obj = stack.pop()
  630. obj[idx] = value
  631. elif opcode == 98: # getlocal
  632. index = u30()
  633. stack.append(registers[index])
  634. elif opcode == 99: # setlocal
  635. index = u30()
  636. value = stack.pop()
  637. registers[index] = value
  638. elif opcode == 102: # getproperty
  639. index = u30()
  640. pname = self.multinames[index]
  641. if pname == 'length':
  642. obj = stack.pop()
  643. assert isinstance(obj, (unicode, list))
  644. stack.append(len(obj))
  645. elif isinstance(pname, unicode): # Member access
  646. obj = stack.pop()
  647. if isinstance(obj, _AVMClass):
  648. res = obj.static_properties[pname]
  649. stack.append(res)
  650. continue
  651. assert isinstance(obj, (dict, _ScopeDict)),\
  652. 'Accessing member %r on %r' % (pname, obj)
  653. res = obj.get(pname, undefined)
  654. stack.append(res)
  655. else: # Assume attribute access
  656. idx = stack.pop()
  657. assert isinstance(idx, int)
  658. obj = stack.pop()
  659. assert isinstance(obj, list)
  660. stack.append(obj[idx])
  661. elif opcode == 104: # initproperty
  662. index = u30()
  663. value = stack.pop()
  664. idx = self.multinames[index]
  665. if isinstance(idx, _Multiname):
  666. idx = stack.pop()
  667. obj = stack.pop()
  668. obj[idx] = value
  669. elif opcode == 115: # convert_
  670. value = stack.pop()
  671. intvalue = int(value)
  672. stack.append(intvalue)
  673. elif opcode == 128: # coerce
  674. u30()
  675. elif opcode == 130: # coerce_a
  676. value = stack.pop()
  677. # um, yes, it's any value
  678. stack.append(value)
  679. elif opcode == 133: # coerce_s
  680. assert isinstance(stack[-1], (type(None), unicode))
  681. elif opcode == 147: # decrement
  682. value = stack.pop()
  683. assert isinstance(value, int)
  684. stack.append(value - 1)
  685. elif opcode == 149: # typeof
  686. value = stack.pop()
  687. return {
  688. _Undefined: 'undefined',
  689. unicode: 'String',
  690. int: 'Number',
  691. float: 'Number',
  692. }[type(value)]
  693. elif opcode == 160: # add
  694. value2 = stack.pop()
  695. value1 = stack.pop()
  696. res = value1 + value2
  697. stack.append(res)
  698. elif opcode == 161: # subtract
  699. value2 = stack.pop()
  700. value1 = stack.pop()
  701. res = value1 - value2
  702. stack.append(res)
  703. elif opcode == 162: # multiply
  704. value2 = stack.pop()
  705. value1 = stack.pop()
  706. res = value1 * value2
  707. stack.append(res)
  708. elif opcode == 164: # modulo
  709. value2 = stack.pop()
  710. value1 = stack.pop()
  711. res = value1 % value2
  712. stack.append(res)
  713. elif opcode == 168: # bitand
  714. value2 = stack.pop()
  715. value1 = stack.pop()
  716. assert isinstance(value1, int)
  717. assert isinstance(value2, int)
  718. res = value1 & value2
  719. stack.append(res)
  720. elif opcode == 171: # equals
  721. value2 = stack.pop()
  722. value1 = stack.pop()
  723. result = value1 == value2
  724. stack.append(result)
  725. elif opcode == 175: # greaterequals
  726. value2 = stack.pop()
  727. value1 = stack.pop()
  728. result = value1 >= value2
  729. stack.append(result)
  730. elif opcode == 192: # increment_i
  731. value = stack.pop()
  732. assert isinstance(value, int)
  733. stack.append(value + 1)
  734. elif opcode == 208: # getlocal_0
  735. stack.append(registers[0])
  736. elif opcode == 209: # getlocal_1
  737. stack.append(registers[1])
  738. elif opcode == 210: # getlocal_2
  739. stack.append(registers[2])
  740. elif opcode == 211: # getlocal_3
  741. stack.append(registers[3])
  742. elif opcode == 212: # setlocal_0
  743. registers[0] = stack.pop()
  744. elif opcode == 213: # setlocal_1
  745. registers[1] = stack.pop()
  746. elif opcode == 214: # setlocal_2
  747. registers[2] = stack.pop()
  748. elif opcode == 215: # setlocal_3
  749. registers[3] = stack.pop()
  750. else:
  751. raise NotImplementedError(
  752. 'Unsupported opcode %d' % opcode)
  753. avm_class.method_pyfunctions[func_name] = resfunc
  754. return resfunc