swfinterp.py 28 KB

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