From 26e3d4aaafcbc64fd81ea8049318eea7c47502f0 Mon Sep 17 00:00:00 2001 From: Logan Chien Date: Thu, 25 Jan 2018 23:37:04 +0800 Subject: [PATCH] vndk-def: Fix is_zipfile() for Python 3.5 Python 3.5 adopts a fancy algorithm to detect the zip file. It will search for the magic words in the file, i.e. magic word does not have to be at the beginning. This commit adds an extra check to make sure "PK", the magic word for zip file, is at the beginning of the file. Test: ./vndk_definition_tool.py apk-deps --system ... --vendor ... Change-Id: I3e633709bc39187ae807aef97c6e0396d02266ea --- .../definition-tool/vndk_definition_tool.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/vndk/tools/definition-tool/vndk_definition_tool.py b/vndk/tools/definition-tool/vndk_definition_tool.py index ef5156a7d..b20c7d844 100755 --- a/vndk/tools/definition-tool/vndk_definition_tool.py +++ b/vndk/tools/definition-tool/vndk_definition_tool.py @@ -882,8 +882,21 @@ class DexFileReader(object): @classmethod - def enumerate_dex_strings_apk(cls, apk_file): - with zipfile.ZipFile(apk_file, 'r') as zip_file: + def _read_first_bytes(cls, apk_file, num_bytes): + try: + with open(apk_file, 'rb') as fp: + return fp.read(num_bytes) + except IOError: + return b'' + + @classmethod + def is_zipfile(cls, apk_file_path): + magic = cls._read_first_bytes(apk_file_path, 2) + return magic == b'PK' and zipfile.is_zipfile(apk_file_path) + + @classmethod + def enumerate_dex_strings_apk(cls, apk_file_path): + with zipfile.ZipFile(apk_file_path, 'r') as zip_file: for name in cls.generate_classes_dex_names(): try: with zip_file.open(name) as dex_file: @@ -3186,7 +3199,7 @@ class ApkDepsCommand(ELFGraphCommand): def scan_apk_deps(self, libnames, system_dirs, vendor_dirs): for ap, path in self._enumerate_paths(system_dirs, vendor_dirs): - if not zipfile.is_zipfile(path): + if not DexFileReader.is_zipfile(path): continue libs = set() strs = set(DexFileReader.enumerate_dex_strings_apk(path))