projects
/
youtube-dl
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
[swfinterp] Better error message
[youtube-dl]
/
youtube_dl
/
swfinterp.py
diff --git
a/youtube_dl/swfinterp.py
b/youtube_dl/swfinterp.py
index 79d86152d7012648e5cd61471cd596624261d851..58da6c586ffd863d054626a638724f5781545320 100644
(file)
--- a/
youtube_dl/swfinterp.py
+++ b/
youtube_dl/swfinterp.py
@@
-2,12
+2,12
@@
from __future__ import unicode_literals
import collections
import io
import collections
import io
-import struct
import zlib
from .utils import (
compat_str,
ExtractorError,
import zlib
from .utils import (
compat_str,
ExtractorError,
+ struct_unpack,
)
)
@@
-23,17
+23,17
@@
def _extract_tags(file_contents):
file_contents[:1])
# Determine number of bits in framesize rectangle
file_contents[:1])
# Determine number of bits in framesize rectangle
- framesize_nbits = struct
.
unpack('!B', content[:1])[0] >> 3
+ framesize_nbits = struct
_
unpack('!B', content[:1])[0] >> 3
framesize_len = (5 + 4 * framesize_nbits + 7) // 8
pos = framesize_len + 2 + 2
while pos < len(content):
framesize_len = (5 + 4 * framesize_nbits + 7) // 8
pos = framesize_len + 2 + 2
while pos < len(content):
- header16 = struct
.
unpack('<H', content[pos:pos + 2])[0]
+ header16 = struct
_
unpack('<H', content[pos:pos + 2])[0]
pos += 2
tag_code = header16 >> 6
tag_len = header16 & 0x3f
if tag_len == 0x3f:
pos += 2
tag_code = header16 >> 6
tag_len = header16 & 0x3f
if tag_len == 0x3f:
- tag_len = struct
.
unpack('<I', content[pos:pos + 4])[0]
+ tag_len = struct
_
unpack('<I', content[pos:pos + 4])[0]
pos += 4
assert pos + tag_len <= len(content), \
('Tag %d ends at %d+%d - that\'s longer than the file (%d)'
pos += 4
assert pos + tag_len <= len(content), \
('Tag %d ends at %d+%d - that\'s longer than the file (%d)'
@@
-99,7
+99,7
@@
def _read_int(reader):
for _ in range(5):
buf = reader.read(1)
assert len(buf) == 1
for _ in range(5):
buf = reader.read(1)
assert len(buf) == 1
- b = struct
.
unpack('<B', buf)[0]
+ b = struct
_
unpack('<B', buf)[0]
res = res | ((b & 0x7f) << shift)
if b & 0x80 == 0:
break
res = res | ((b & 0x7f) << shift)
if b & 0x80 == 0:
break
@@
-111,7
+111,7
@@
def _u30(reader):
res = _read_int(reader)
assert res & 0xf0000000 == 0
return res
res = _read_int(reader)
assert res & 0xf0000000 == 0
return res
-u32 = _read_int
+
_
u32 = _read_int
def _s32(reader):
def _s32(reader):
@@
-125,7
+125,7
@@
def _s24(reader):
bs = reader.read(3)
assert len(bs) == 3
last_byte = b'\xff' if (ord(bs[2:3]) >= 0x80) else b'\x00'
bs = reader.read(3)
assert len(bs) == 3
last_byte = b'\xff' if (ord(bs[2:3]) >= 0x80) else b'\x00'
- return struct
.
unpack('<i', bs + last_byte)[0]
+ return struct
_
unpack('<i', bs + last_byte)[0]
def _read_string(reader):
def _read_string(reader):
@@
-144,12
+144,13
@@
def _read_bytes(count, reader):
def _read_byte(reader):
resb = _read_bytes(1, reader=reader)
def _read_byte(reader):
resb = _read_bytes(1, reader=reader)
- res = struct
.
unpack('<B', resb)[0]
+ res = struct
_
unpack('<B', resb)[0]
return res
class SWFInterpreter(object):
def __init__(self, file_contents):
return res
class SWFInterpreter(object):
def __init__(self, file_contents):
+ self._patched_functions = {}
code_tag = next(tag
for tag_code, tag in _extract_tags(file_contents)
if tag_code == 82)
code_tag = next(tag
for tag_code, tag in _extract_tags(file_contents)
if tag_code == 82)
@@
-212,6
+213,10
@@
class SWFInterpreter(object):
u30() # namespace_idx
name_idx = u30()
self.multinames.append(self.constant_strings[name_idx])
u30() # namespace_idx
name_idx = u30()
self.multinames.append(self.constant_strings[name_idx])
+ elif kind == 0x09:
+ name_idx = u30()
+ u30()
+ self.multinames.append(self.constant_strings[name_idx])
else:
self.multinames.append(_Multiname(kind))
for _c2 in range(MULTINAME_SIZES[kind]):
else:
self.multinames.append(_Multiname(kind))
for _c2 in range(MULTINAME_SIZES[kind]):
@@
-354,6
+359,9
@@
class SWFInterpreter(object):
assert p + code_reader.tell() == len(code_tag)
assert p + code_reader.tell() == len(code_tag)
+ def patch_function(self, avm_class, func_name, f):
+ self._patched_functions[(avm_class, func_name)] = f
+
def extract_class(self, class_name):
try:
return self._classes_by_name[class_name]
def extract_class(self, class_name):
try:
return self._classes_by_name[class_name]
@@
-361,6
+369,9
@@
class SWFInterpreter(object):
raise ExtractorError('Class %r not found' % class_name)
def extract_function(self, avm_class, func_name):
raise ExtractorError('Class %r not found' % class_name)
def extract_function(self, avm_class, func_name):
+ p = self._patched_functions.get((avm_class, func_name))
+ if p:
+ return p
if func_name in avm_class.method_pyfunctions:
return avm_class.method_pyfunctions[func_name]
if func_name in self._classes_by_name:
if func_name in avm_class.method_pyfunctions:
return avm_class.method_pyfunctions[func_name]
if func_name in self._classes_by_name:
@@
-470,8
+481,7
@@
class SWFInterpreter(object):
mname = self.multinames[index]
assert isinstance(obj, _AVMClass)
mname = self.multinames[index]
assert isinstance(obj, _AVMClass)
- construct_method = self.extract_function(
- obj, mname)
+
# We do not actually call the constructor for now;
# we just pretend it does nothing
stack.append(obj.make_object())
# We do not actually call the constructor for now;
# we just pretend it does nothing
stack.append(obj.make_object())
@@
-551,6
+561,11
@@
class SWFInterpreter(object):
obj = stack.pop()
assert isinstance(obj, list)
stack.append(len(obj))
obj = stack.pop()
assert isinstance(obj, list)
stack.append(len(obj))
+ elif isinstance(pname, compat_str): # Member access
+ obj = stack.pop()
+ assert isinstance(obj, (dict, _ScopeDict)), \
+ 'Accessing member %r on %r' % (pname, obj)
+ stack.append(obj[pname])
else: # Assume attribute access
idx = stack.pop()
assert isinstance(idx, int)
else: # Assume attribute access
idx = stack.pop()
assert isinstance(idx, int)