Skip to content

Commit fc2c8c8

Browse files
committed
Rearrange guess_emu_env
1 parent 6e74077 commit fc2c8c8

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

qiling/utils.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,19 @@ def ql_get_module_function(module_name: str, function_name: str):
9393

9494
return module_function
9595

96-
def ql_elf_parse_emu_env(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS], Optional[QL_ENDIAN]]:
96+
def __emu_env_from_pathname(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS], Optional[QL_ENDIAN]]:
97+
if os.path.isdir(path) and path.endswith('.kext'):
98+
return QL_ARCH.X8664, QL_OS.MACOS, QL_ENDIAN.EL
99+
100+
if os.path.isfile(path):
101+
_, ext = os.path.splitext(path)
102+
103+
if ext in ('.DOS_COM', '.DOS_MBR', '.DOS_EXE'):
104+
return QL_ARCH.A8086, QL_OS.DOS, QL_ENDIAN.EL
105+
106+
return None, None, None
107+
108+
def __emu_env_from_elf(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS], Optional[QL_ENDIAN]]:
97109
# instead of using full-blown elffile parsing, we perform a simple parsing to avoid
98110
# external dependencies for target systems that do not need them.
99111
#
@@ -194,7 +206,7 @@ def ql_elf_parse_emu_env(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS],
194206

195207
return archtype, ostype, archendian
196208

197-
def ql_macho_parse_emu_env(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS], Optional[QL_ENDIAN]]:
209+
def __emu_env_from_macho(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS], Optional[QL_ENDIAN]]:
198210
macho_macos_sig64 = b'\xcf\xfa\xed\xfe'
199211
macho_macos_sig32 = b'\xce\xfa\xed\xfe'
200212
macho_macos_fat = b'\xca\xfe\xba\xbe' # should be header for FAT
@@ -222,8 +234,9 @@ def ql_macho_parse_emu_env(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS
222234

223235
return arch, ostype, endian
224236

237+
def __emu_env_from_pe(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS], Optional[QL_ENDIAN]]:
238+
import pefile
225239

226-
def ql_pe_parse_emu_env(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS], Optional[QL_ENDIAN]]:
227240
try:
228241
pe = pefile.PE(path, fast_load=True)
229242
except:
@@ -261,31 +274,21 @@ def ql_pe_parse_emu_env(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS],
261274

262275
return arch, ostype, archendian
263276

264-
265277
def ql_guess_emu_env(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS], Optional[QL_ENDIAN]]:
266-
arch = None
267-
ostype = None
268-
endian = None
269-
270-
if os.path.isdir(path) and path.endswith('.kext'):
271-
return QL_ARCH.X8664, QL_OS.MACOS, QL_ENDIAN.EL
272-
273-
if os.path.isfile(path) and path.endswith('.DOS_COM'):
274-
return QL_ARCH.A8086, QL_OS.DOS, QL_ENDIAN.EL
275-
276-
if os.path.isfile(path) and path.endswith('.DOS_MBR'):
277-
return QL_ARCH.A8086, QL_OS.DOS, QL_ENDIAN.EL
278-
279-
if os.path.isfile(path) and path.endswith('.DOS_EXE'):
280-
return QL_ARCH.A8086, QL_OS.DOS, QL_ENDIAN.EL
281-
282-
arch, ostype, endian = ql_elf_parse_emu_env(path)
283-
284-
if arch is None or ostype is None or endian is None:
285-
arch, ostype, endian = ql_macho_parse_emu_env(path)
286-
287-
if arch is None or ostype is None or endian is None:
288-
arch, ostype, endian = ql_pe_parse_emu_env(path)
278+
guessing_methods = (
279+
__emu_env_from_pathname,
280+
__emu_env_from_elf,
281+
__emu_env_from_macho,
282+
__emu_env_from_pe
283+
)
284+
285+
for gm in guessing_methods:
286+
arch, ostype, endian = gm(path)
287+
288+
if None not in (arch, ostype, endian):
289+
break
290+
else:
291+
arch, ostype, endian = (None, ) * 3
289292

290293
return arch, ostype, endian
291294

0 commit comments

Comments
 (0)