1212import importlib , os , pefile , yaml
1313
1414from configparser import ConfigParser
15- from typing import Any , Container , Optional , Tuple , Type , Union
16- from enum import Enum
15+ from typing import Any , Mapping , Optional , Tuple , Type , Union
1716
1817from unicorn import UC_ERR_READ_UNMAPPED , UC_ERR_FETCH_UNMAPPED
1918
@@ -31,49 +30,31 @@ def wrapper(*args, **kw):
3130
3231 return wrapper
3332
34- def enum_values ( e : Type [ Enum ] ) -> Container :
35- return e . __members__ . values ()
33+ def __name_to_enum ( name : str , mapping : Mapping [ str , T ], aliases : Mapping [ str , str ] = {} ) -> Optional [ T ] :
34+ key = name . casefold ()
3635
37- def ql_is_valid_ostype (ostype : QL_OS ) -> bool :
38- return ostype in enum_values (QL_OS )
36+ return mapping .get (aliases .get (key ) or key )
3937
40- def ql_is_valid_arch (arch : QL_ARCH ) -> bool :
41- return arch in enum_values (QL_ARCH )
42-
43- def __value_to_key (e : Type [Enum ], val : Any ) -> Optional [str ]:
44- key = e ._value2member_map_ [val ]
45-
46- return None if key is None else key .name
47-
48- def ostype_convert_str (ostype : QL_OS ) -> Optional [str ]:
49- return __value_to_key (QL_OS , ostype )
50-
51- def ostype_convert (ostype : str ) -> Optional [QL_OS ]:
38+ def os_convert (os : str ) -> Optional [QL_OS ]:
5239 alias_map = {
53- " darwin" : " macos" ,
40+ ' darwin' : ' macos'
5441 }
5542
56- return os_map .get (alias_map .get (ostype , ostype ))
57-
58- def arch_convert_str (arch : QL_ARCH ) -> Optional [str ]:
59- return __value_to_key (QL_ARCH , arch )
43+ return __name_to_enum (os , os_map , alias_map )
6044
6145def arch_convert (arch : str ) -> Optional [QL_ARCH ]:
6246 alias_map = {
63- " x86_64" : " x8664" ,
64- " riscv32" : " riscv" ,
47+ ' x86_64' : ' x8664' ,
48+ ' riscv32' : ' riscv'
6549 }
66-
67- return arch_map .get (alias_map .get (arch , arch ))
6850
69- def arch_os_convert (arch : QL_ARCH ) -> Optional [QL_OS ]:
70- return arch_os_map .get (arch )
51+ return __name_to_enum (arch , arch_map , alias_map )
7152
7253def debugger_convert (debugger : str ) -> Optional [QL_DEBUGGER ]:
73- return debugger_map . get (debugger )
54+ return __name_to_enum (debugger , debugger_map )
7455
75- def debugger_convert_str ( debugger_id : QL_DEBUGGER ) -> Optional [str ]:
76- return __value_to_key ( QL_DEBUGGER , debugger_id )
56+ def arch_os_convert ( arch : QL_ARCH ) -> Optional [QL_OS ]:
57+ return arch_os_map . get ( arch )
7758
7859# Call `function_name` in `module_name`.
7960# e.g. map_syscall in qiling.os.linux.map_syscall
@@ -368,7 +349,7 @@ def arch_setup(archtype: QL_ARCH, endian: QL_ENDIAN, thumb: bool, ql):
368349 }[archtype ]
369350
370351 qlarch_path = f'qiling.arch.{ module } '
371- qlarch_class = f'QlArch{ arch_convert_str ( archtype ) .upper ()} '
352+ qlarch_class = f'QlArch{ archtype . name .upper ()} '
372353
373354 obj = ql_get_module_function (qlarch_path , qlarch_class )
374355
@@ -377,7 +358,7 @@ def arch_setup(archtype: QL_ARCH, endian: QL_ENDIAN, thumb: bool, ql):
377358
378359# This function is extracted from os_setup (QlOsPosix) so I put it here.
379360def ql_syscall_mapping_function (ostype : QL_OS , archtype : QL_ARCH ):
380- qlos_name = ostype_convert_str ( ostype )
361+ qlos_name = ostype . name
381362 qlos_path = f'qiling.os.{ qlos_name .lower ()} .map_syscall'
382363 qlos_func = 'get_syscall_mapper'
383364
@@ -409,7 +390,7 @@ def profile_setup(ql, ostype: QL_OS, filename: Optional[str]):
409390
410391 else :
411392 qiling_home = os .path .dirname (os .path .abspath (__file__ ))
412- os_profile = os .path .join (qiling_home , 'profiles' , f'{ ostype_convert_str ( ostype ) .lower ()} .ql' )
393+ os_profile = os .path .join (qiling_home , 'profiles' , f'{ ostype . name .lower ()} .ql' )
413394
414395 profiles = [os_profile ]
415396
0 commit comments