From 5df8cc96dc1096c002d974e60c94c0b85a7d9a2b Mon Sep 17 00:00:00 2001 From: hakril Date: Mon, 26 Nov 2018 22:39:31 +0100 Subject: [PATCH] enum mapper is now a FlagMapper + improve winfunc parsing --- ctypes_generation/func_parser.py | 9 ++++++++- ctypes_generation/generate.py | 5 +++++ ctypes_generation/winstruct.py | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ctypes_generation/func_parser.py b/ctypes_generation/func_parser.py index 8c6daa0..c937f30 100644 --- a/ctypes_generation/func_parser.py +++ b/ctypes_generation/func_parser.py @@ -45,6 +45,7 @@ class WinFunc(object): class WinFuncParser(Parser): known_io_info_type = ["__in", "__in_opt", "_In_", "_In_opt_", "_Inout_", "_Out_opt_", "_Out_", "_Reserved_", "_Inout_opt_", "__inout_opt", "__out", "__inout", "__deref_out", "_Outptr_"] + known_io_info_with_param = ["_Out_writes_bytes_", "_In_reads_bytes_opt_", "_In_reads_bytes_"] known_declarations = { "WINAPI" : "WINFUNCTYPE", "LDAPAPI" : "CFUNCTYPE" @@ -54,7 +55,13 @@ class WinFuncParser(Parser): def assert_argument_io_info(self): io_info = self.assert_token_type(NameToken) if io_info.value not in self.known_io_info_type: - raise ParsingError("Was expection IO_INFO got {0} instead".format(io_info)) + if io_info.value not in self.known_io_info_with_param: + raise ParsingError("Was expection IO_INFO got {0} instead".format(io_info)) + # Ignore IO infor params. + # Only 1 param accepted for now + self.assert_token_type(OpenParenthesisToken) + self.assert_token_type(NameToken) + self.assert_token_type(CloseParenthesisToken) return io_info def parse_func_arg(self, has_winapi): diff --git a/ctypes_generation/generate.py b/ctypes_generation/generate.py index 101400c..a30fb58 100644 --- a/ctypes_generation/generate.py +++ b/ctypes_generation/generate.py @@ -103,6 +103,11 @@ class DefinitionParsedFile(ParsedFile): def compute_imports_exports(self, data): for windef in data: self.add_exports(windef.name) # No dependancy check on rvalue for now + if "|" in windef.code: # Multi flags ? Allow cross-file dependancies + # Quick parsing: add all names in the imports + all_imports_words = re.findall(r"\b[A-Z_]\w+", windef.code) + real_deps = [word for word in all_imports_words if word not in self.exports] + self.add_imports(*real_deps) class NtStatusParsedFile(ParsedFile): PARSER = def_parser.NtStatusParser diff --git a/ctypes_generation/winstruct.py b/ctypes_generation/winstruct.py index e6f1a22..6cadc6c 100644 --- a/ctypes_generation/winstruct.py +++ b/ctypes_generation/winstruct.py @@ -173,7 +173,7 @@ class WinEnum(object): lines += ["class {0}(EnumType):".format(self.name)] lines += [" values = [{0}]".format(", ".join([name for i, name in self.fields]))] - lines += [" mapper = {{x:x for x in values}}".format(self.name)] + lines += [" mapper = FlagMapper(*values)".format(self.name)] for typedef_name, value in self.typedef.items(): str_value = self.name