1 from __future__ import unicode_literals
14 def _extract_tags(file_contents):
15 if file_contents[1:3] != b'WS':
17 'Not an SWF file; header is %r' % file_contents[:3])
18 if file_contents[:1] == b'C':
19 content = zlib.decompress(file_contents[8:])
21 raise NotImplementedError(
22 'Unsupported compression format %r' %
25 # Determine number of bits in framesize rectangle
26 framesize_nbits = struct_unpack('!B', content[:1])[0] >> 3
27 framesize_len = (5 + 4 * framesize_nbits + 7) // 8
29 pos = framesize_len + 2 + 2
30 while pos < len(content):
31 header16 = struct_unpack('<H', content[pos:pos + 2])[0]
33 tag_code = header16 >> 6
34 tag_len = header16 & 0x3f
36 tag_len = struct_unpack('<I', content[pos:pos + 4])[0]
38 assert pos + tag_len <= len(content), \
39 ('Tag %d ends at %d+%d - that\'s longer than the file (%d)'
40 % (tag_code, pos, tag_len, len(content)))
41 yield (tag_code, content[pos:pos + tag_len])
45 class _AVMClass_Object(object):
46 def __init__(self, avm_class):
47 self.avm_class = avm_class
50 return '%s#%x' % (self.avm_class.name, id(self))
53 class _ScopeDict(dict):
54 def __init__(self, avm_class):
55 super(_ScopeDict, self).__init__()
56 self.avm_class = avm_class
59 return '%s__Scope(%s)' % (
61 super(_ScopeDict, self).__repr__())
64 class _AVMClass(object):
65 def __init__(self, name_idx, name):
66 self.name_idx = name_idx
68 self.method_names = {}
71 self.method_pyfunctions = {}
73 self.variables = _ScopeDict(self)
75 def make_object(self):
76 return _AVMClass_Object(self)
79 return '_AVMClass(%s)' % (self.name)
81 def register_methods(self, methods):
82 self.method_names.update(methods.items())
83 self.method_idxs.update(dict(
85 for name, idx in methods.items()))
88 class _Multiname(object):
89 def __init__(self, kind):
93 return '[MULTINAME kind: 0x%x]' % self.kind
96 def _read_int(reader):
102 b = struct_unpack('<B', buf)[0]
103 res = res | ((b & 0x7f) << shift)
111 res = _read_int(reader)
112 assert res & 0xf0000000 == 0
118 v = _read_int(reader)
119 if v & 0x80000000 != 0:
120 v = - ((v ^ 0xffffffff) + 1)
127 last_byte = b'\xff' if (ord(bs[2:3]) >= 0x80) else b'\x00'
128 return struct_unpack('<i', bs + last_byte)[0]
131 def _read_string(reader):
133 resb = reader.read(slen)
134 assert len(resb) == slen
135 return resb.decode('utf-8')
138 def _read_bytes(count, reader):
140 resb = reader.read(count)
141 assert len(resb) == count
145 def _read_byte(reader):
146 resb = _read_bytes(1, reader=reader)
147 res = struct_unpack('<B', resb)[0]
151 StringClass = _AVMClass('(no name idx)', 'String')
154 class SWFInterpreter(object):
155 def __init__(self, file_contents):
156 self._patched_functions = {}
158 for tag_code, tag in _extract_tags(file_contents)
160 p = code_tag.index(b'\0', 4) + 1
161 code_reader = io.BytesIO(code_tag[p:])
163 # Parse ABC (AVM2 ByteCode)
165 # Define a couple convenience methods
166 u30 = lambda *args: _u30(*args, reader=code_reader)
167 s32 = lambda *args: _s32(*args, reader=code_reader)
168 u32 = lambda *args: _u32(*args, reader=code_reader)
169 read_bytes = lambda *args: _read_bytes(*args, reader=code_reader)
170 read_byte = lambda *args: _read_byte(*args, reader=code_reader)
172 # minor_version + major_version
177 for _c in range(1, int_count):
180 for _c in range(1, uint_count):
183 read_bytes(max(0, (double_count - 1)) * 8)
185 self.constant_strings = ['']
186 for _c in range(1, string_count):
187 s = _read_string(code_reader)
188 self.constant_strings.append(s)
189 namespace_count = u30()
190 for _c in range(1, namespace_count):
194 for _c in range(1, ns_set_count):
196 for _c2 in range(count):
198 multiname_count = u30()
207 0x0e: 2, # MultinameA
208 0x1b: 1, # MultinameL
209 0x1c: 1, # MultinameLA
211 self.multinames = ['']
212 for _c in range(1, multiname_count):
214 assert kind in MULTINAME_SIZES, 'Invalid multiname kind %r' % kind
216 u30() # namespace_idx
218 self.multinames.append(self.constant_strings[name_idx])
222 self.multinames.append(self.constant_strings[name_idx])
224 self.multinames.append(_Multiname(kind))
225 for _c2 in range(MULTINAME_SIZES[kind]):
230 MethodInfo = collections.namedtuple(
232 ['NEED_ARGUMENTS', 'NEED_REST'])
234 for method_id in range(method_count):
237 for _ in range(param_count):
239 u30() # name index (always 0 for youtube)
241 if flags & 0x08 != 0:
244 for c in range(option_count):
247 if flags & 0x80 != 0:
248 # Param names present
249 for _ in range(param_count):
251 mi = MethodInfo(flags & 0x01 != 0, flags & 0x04 != 0)
252 method_infos.append(mi)
255 metadata_count = u30()
256 for _c in range(metadata_count):
259 for _c2 in range(item_count):
263 def parse_traits_info():
264 trait_name_idx = u30()
265 kind_full = read_byte()
266 kind = kind_full & 0x0f
267 attrs = kind_full >> 4
269 if kind in [0x00, 0x06]: # Slot or Const
271 u30() # type_name_idx
275 elif kind in [0x01, 0x02, 0x03]: # Method / Getter / Setter
278 methods[self.multinames[trait_name_idx]] = method_idx
279 elif kind == 0x04: # Class
282 elif kind == 0x05: # Function
285 methods[function_idx] = self.multinames[trait_name_idx]
287 raise ExtractorError('Unsupported trait kind %d' % kind)
289 if attrs & 0x4 != 0: # Metadata present
290 metadata_count = u30()
291 for _c3 in range(metadata_count):
292 u30() # metadata index
299 for class_id in range(class_count):
302 cname = self.multinames[name_idx]
303 avm_class = _AVMClass(name_idx, cname)
304 classes.append(avm_class)
306 u30() # super_name idx
308 if flags & 0x08 != 0: # Protected namespace is present
309 u30() # protected_ns_idx
311 for _c2 in range(intrf_count):
315 for _c2 in range(trait_count):
316 trait_methods = parse_traits_info()
317 avm_class.register_methods(trait_methods)
319 assert len(classes) == class_count
320 self._classes_by_name = dict((c.name, c) for c in classes)
322 for avm_class in classes:
325 for _c2 in range(trait_count):
326 trait_methods = parse_traits_info()
327 avm_class.register_methods(trait_methods)
331 for _c in range(script_count):
334 for _c2 in range(trait_count):
338 method_body_count = u30()
339 Method = collections.namedtuple('Method', ['code', 'local_count'])
340 for _c in range(method_body_count):
344 u30() # init_scope_depth
345 u30() # max_scope_depth
347 code = read_bytes(code_length)
348 for avm_class in classes:
349 if method_idx in avm_class.method_idxs:
350 m = Method(code, local_count)
351 avm_class.methods[avm_class.method_idxs[method_idx]] = m
352 exception_count = u30()
353 for _c2 in range(exception_count):
360 for _c2 in range(trait_count):
363 assert p + code_reader.tell() == len(code_tag)
365 def patch_function(self, avm_class, func_name, f):
366 self._patched_functions[(avm_class, func_name)] = f
368 def extract_class(self, class_name):
370 return self._classes_by_name[class_name]
372 raise ExtractorError('Class %r not found' % class_name)
374 def extract_function(self, avm_class, func_name):
375 p = self._patched_functions.get((avm_class, func_name))
378 if func_name in avm_class.method_pyfunctions:
379 return avm_class.method_pyfunctions[func_name]
380 if func_name in self._classes_by_name:
381 return self._classes_by_name[func_name].make_object()
382 if func_name not in avm_class.methods:
383 raise ExtractorError('Cannot find function %s.%s' % (
384 avm_class.name, func_name))
385 m = avm_class.methods[func_name]
389 coder = io.BytesIO(m.code)
390 s24 = lambda: _s24(coder)
391 u30 = lambda: _u30(coder)
393 registers = [avm_class.variables] + list(args) + [None] * m.local_count
395 scopes = collections.deque([
396 self._classes_by_name, avm_class.variables])
398 opcode = _read_byte(coder)
399 if opcode == 16: # jump
401 coder.seek(coder.tell() + offset)
402 elif opcode == 17: # iftrue
406 coder.seek(coder.tell() + offset)
407 elif opcode == 18: # iffalse
411 coder.seek(coder.tell() + offset)
412 elif opcode == 19: # ifeq
417 coder.seek(coder.tell() + offset)
418 elif opcode == 20: # ifne
423 coder.seek(coder.tell() + offset)
424 elif opcode == 32: # pushnull
426 elif opcode == 36: # pushbyte
427 v = _read_byte(coder)
429 elif opcode == 42: # dup
432 elif opcode == 44: # pushstring
434 stack.append(self.constant_strings[idx])
435 elif opcode == 48: # pushscope
436 new_scope = stack.pop()
437 scopes.append(new_scope)
438 elif opcode == 66: # construct
440 args = list(reversed(
441 [stack.pop() for _ in range(arg_count)]))
443 res = obj.avm_class.make_object()
445 elif opcode == 70: # callproperty
447 mname = self.multinames[index]
449 args = list(reversed(
450 [stack.pop() for _ in range(arg_count)]))
453 if isinstance(obj, _AVMClass_Object):
454 func = self.extract_function(obj.avm_class, mname)
458 elif isinstance(obj, _ScopeDict):
459 if mname in obj.avm_class.method_names:
460 func = self.extract_function(obj.avm_class, mname)
466 elif isinstance(obj, compat_str):
468 assert len(args) == 1
469 assert isinstance(args[0], compat_str)
473 res = obj.split(args[0])
476 elif isinstance(obj, list):
478 assert len(args) == 1
479 assert isinstance(args[0], int)
483 elif mname == 'join':
484 assert len(args) == 1
485 assert isinstance(args[0], compat_str)
486 res = args[0].join(obj)
489 elif obj == StringClass:
490 if mname == 'String':
491 assert len(args) == 1
492 assert isinstance(args[0], (int, compat_str))
493 res = compat_str(args[0])
497 raise NotImplementedError(
498 'Function String.%s is not yet implemented'
500 raise NotImplementedError(
501 'Unsupported property %r on %r'
503 elif opcode == 72: # returnvalue
506 elif opcode == 74: # constructproperty
509 args = list(reversed(
510 [stack.pop() for _ in range(arg_count)]))
513 mname = self.multinames[index]
514 assert isinstance(obj, _AVMClass)
516 # We do not actually call the constructor for now;
517 # we just pretend it does nothing
518 stack.append(obj.make_object())
519 elif opcode == 79: # callpropvoid
521 mname = self.multinames[index]
523 args = list(reversed(
524 [stack.pop() for _ in range(arg_count)]))
526 if mname == 'reverse':
527 assert isinstance(obj, list)
530 raise NotImplementedError(
531 'Unsupported (void) property %r on %r'
533 elif opcode == 86: # newarray
536 for i in range(arg_count):
537 arr.append(stack.pop())
540 elif opcode == 93: # findpropstrict
542 mname = self.multinames[index]
543 for s in reversed(scopes):
549 if mname not in res and mname == 'String':
550 stack.append(StringClass)
552 stack.append(res[mname])
553 elif opcode == 94: # findproperty
555 mname = self.multinames[index]
556 for s in reversed(scopes):
561 res = avm_class.variables
563 elif opcode == 96: # getlex
565 mname = self.multinames[index]
566 for s in reversed(scopes):
571 scope = avm_class.variables
572 # I cannot find where static variables are initialized
573 # so let's just return None
574 res = scope.get(mname)
576 elif opcode == 97: # setproperty
579 idx = self.multinames[index]
580 if isinstance(idx, _Multiname):
584 elif opcode == 98: # getlocal
586 stack.append(registers[index])
587 elif opcode == 99: # setlocal
590 registers[index] = value
591 elif opcode == 102: # getproperty
593 pname = self.multinames[index]
594 if pname == 'length':
596 assert isinstance(obj, (compat_str, list))
597 stack.append(len(obj))
598 elif isinstance(pname, compat_str): # Member access
600 assert isinstance(obj, (dict, _ScopeDict)), \
601 'Accessing member %r on %r' % (pname, obj)
602 stack.append(obj[pname])
603 else: # Assume attribute access
605 assert isinstance(idx, int)
607 assert isinstance(obj, list)
608 stack.append(obj[idx])
609 elif opcode == 115: # convert_
611 intvalue = int(value)
612 stack.append(intvalue)
613 elif opcode == 128: # coerce
615 elif opcode == 133: # coerce_s
616 assert isinstance(stack[-1], (type(None), compat_str))
617 elif opcode == 160: # add
620 res = value1 + value2
622 elif opcode == 161: # subtract
625 res = value1 - value2
627 elif opcode == 164: # modulo
630 res = value1 % value2
632 elif opcode == 171: # equals
635 result = value1 == value2
637 elif opcode == 175: # greaterequals
640 result = value1 >= value2
642 elif opcode == 208: # getlocal_0
643 stack.append(registers[0])
644 elif opcode == 209: # getlocal_1
645 stack.append(registers[1])
646 elif opcode == 210: # getlocal_2
647 stack.append(registers[2])
648 elif opcode == 211: # getlocal_3
649 stack.append(registers[3])
650 elif opcode == 212: # setlocal_0
651 registers[0] = stack.pop()
652 elif opcode == 213: # setlocal_1
653 registers[1] = stack.pop()
654 elif opcode == 214: # setlocal_2
655 registers[2] = stack.pop()
656 elif opcode == 215: # setlocal_3
657 registers[3] = stack.pop()
659 raise NotImplementedError(
660 'Unsupported opcode %d' % opcode)
662 avm_class.method_pyfunctions[func_name] = resfunc