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